Skip to content

Commit 8e3edfa

Browse files
Tweak agenda sorting to respect loading order.
1 parent 3728e8b commit 8e3edfa

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

lua/orgmode/agenda/agenda_item.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ local AgendaItem = {}
2525
---@param headline_date Date single date in a headline
2626
---@param headline Section
2727
---@param date Date date for which item should be rendered
28-
function AgendaItem:new(headline_date, headline, date)
28+
---@param index number
29+
function AgendaItem:new(headline_date, headline, date, index)
2930
local opts = {}
3031
opts.headline_date = headline_date
3132
opts.headline = headline
3233
opts.date = date
34+
opts.index = index or 1
3335
opts.is_valid = false
3436
opts.is_today = date:is_today()
3537
opts.is_same_day = headline_date:is_same(date, 'day') or headline_date:repeats_on(date)

lua/orgmode/agenda/views/agenda.lua

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ local AgendaItem = require('orgmode.agenda.agenda_item')
77
local AgendaFilter = require('orgmode.agenda.filter')
88
local utils = require('orgmode.utils')
99

10+
local function sort_by_date_or_category(a, b)
11+
if not a.headline_date:is_same(b.headline_date) then
12+
return a.headline_date:is_before(b.headline_date)
13+
end
14+
return a.index < b.index
15+
end
16+
1017
---@param agenda_items AgendaItem[]
1118
---@return AgendaItem[]
1219
local function sort_agenda_items(agenda_items)
@@ -18,7 +25,10 @@ local function sort_agenda_items(agenda_items)
1825
if not a.headline_date.date_only then
1926
return true
2027
end
21-
return false
28+
if not b.headline_date.date_only then
29+
return false
30+
end
31+
return sort_by_date_or_category(a, b)
2232
end
2333

2434
if a.is_same_day and not b.is_same_day then
@@ -37,7 +47,7 @@ local function sort_agenda_items(agenda_items)
3747
return a.headline:get_priority_sort_value() > b.headline:get_priority_sort_value()
3848
end
3949

40-
return a.headline_date:is_before(b.headline_date)
50+
return sort_by_date_or_category(a, b)
4151
end)
4252
return agenda_items
4353
end
@@ -139,8 +149,8 @@ function AgendaView:_build_items()
139149
for _, day in ipairs(dates) do
140150
local date = { day = day, agenda_items = {} }
141151

142-
for _, item in ipairs(headline_dates) do
143-
local agenda_item = AgendaItem:new(item.headline_date, item.headline, day)
152+
for index, item in ipairs(headline_dates) do
153+
local agenda_item = AgendaItem:new(item.headline_date, item.headline, day, index)
144154
if agenda_item.is_valid and self.filters:matches(item.headline) then
145155
table.insert(date.agenda_items, agenda_item)
146156
end

lua/orgmode/parser/file.lua

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ function File:get_opened_headlines()
8181
return not item:is_archived()
8282
end, self.sections)
8383

84-
table.sort(headlines, function(a, b)
85-
return a:get_priority_sort_value() > b:get_priority_sort_value()
86-
end)
87-
8884
return headlines
8985
end
9086

0 commit comments

Comments
 (0)