Skip to content

Commit 88c71f3

Browse files
fix(agenda): make spans work like emacs (#915)
Currently, view month and view day both shift the cursor to and start the view's span from the start day (Monday by default). In emacs, all view changes take into account the agenda date that the cursor is on, and then choose the beginning date accordingly. These changes aim to mimic emacs behavior by 1. modifying all view changes to stop moving the cursor 2. displaying the W number in all spans types 3. change the first day of the view to be the first day of the span Reuse existing function OrgDate:start_of(span) Ensure start is on user configured start week for initial week view as well Co-authored-by: Kristijan Husak <[email protected]>
1 parent 52ff9cf commit 88c71f3

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

Diff for: lua/orgmode/agenda/types/agenda.lua

+18-8
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,18 @@ function OrgAgendaType:change_span(span)
133133
return
134134
end
135135
end
136+
local from = nil
137+
if self.view and self.view:is_in_range() then
138+
local agenda_line = self:get_line(vim.fn.line('.'))
139+
local metadata = agenda_line and agenda_line.metadata or {}
140+
---@type OrgDate
141+
local cursor_date = metadata.agenda_day or (metadata.agenda_item and metadata.agenda_item.date)
142+
if cursor_date and type(span) == 'string' then
143+
from = cursor_date:start_of(span)
144+
end
145+
end
136146
self.span = span
137-
self:_set_date_range()
147+
self:_set_date_range(from)
138148
return self
139149
end
140150

@@ -430,11 +440,13 @@ end
430440
function OrgAgendaType:_set_date_range(from)
431441
local span = self.span
432442
from = from or self.from
433-
local is_week = span == 'week' or span == '7'
434-
if is_week and self.start_on_weekday then
435-
from = from:set_isoweekday(self.start_on_weekday)
436-
end
437443

444+
if self.start_on_weekday then
445+
local is_week = span == 'week' or span == '7'
446+
if is_week and from:is_same(Date.today(), 'week') then
447+
from = from:set_isoweekday(self.start_on_weekday)
448+
end
449+
end
438450
local to = nil
439451
local modifier = { [span] = 1 }
440452
if type(span) == 'number' then
@@ -463,9 +475,7 @@ function OrgAgendaType:_get_title()
463475
span = string.format('%d days', span)
464476
end
465477
local span_number = ''
466-
if span == 'week' then
467-
span_number = string.format(' (W%s)', self.from:get_week_number())
468-
end
478+
span_number = string.format(' (W%s)', self.from:format('%V'))
469479
return utils.capitalize(span) .. '-agenda' .. span_number .. ':'
470480
end
471481

0 commit comments

Comments
 (0)