Skip to content

Commit ed8a420

Browse files
Fix agenda sorting to behave the same as in Emacs orgmode.
1 parent 6d62725 commit ed8a420

File tree

4 files changed

+31
-22
lines changed

4 files changed

+31
-22
lines changed

lua/orgmode/agenda/views/agenda.lua

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,26 @@ local utils = require('orgmode.utils')
1111
---@return AgendaItem[]
1212
local function sort_agenda_items(agenda_items)
1313
table.sort(agenda_items, function(a, b)
14-
if a.is_today and a.is_same_day then
15-
if b.is_today and b.is_same_day then
14+
if a.is_same_day and b.is_same_day then
15+
if not a.headline_date.date_only and not b.headline_date.date_only then
1616
return a.headline_date:is_before(b.headline_date)
1717
end
18+
if not b.headline_date.date_only then
19+
return false
20+
end
1821
return true
1922
end
2023

21-
if b.is_today and b.is_same_day then
22-
if a.is_today and a.is_same_day then
23-
return a.headline_date:is_before(b.headline_date)
24+
if a.is_same_day and not b.is_same_day then
25+
if not a.headline_date.date_only or (b.headline_date:is_none() and not a.headline_date:is_none()) then
26+
return true
27+
end
28+
end
29+
30+
if not a.is_same_day and b.is_same_day then
31+
if not b.headline_date.date_only or (a.headline_date:is_none() and not b.headline_date:is_none()) then
32+
return false
2433
end
25-
return false
2634
end
2735

2836
if a.headline:get_priority_sort_value() ~= b.headline:get_priority_sort_value() then
@@ -33,14 +41,6 @@ local function sort_agenda_items(agenda_items)
3341
return a.headline_date:is_before(b.headline_date)
3442
end
3543

36-
if a.is_in_date_range and not b.is_in_date_range then
37-
return false
38-
end
39-
40-
if not a.is_in_date_range and b.is_in_date_range then
41-
return true
42-
end
43-
4444
return a.headline_date:is_before(b.headline_date)
4545
end)
4646
return agenda_items

lua/orgmode/parser/files.lua

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,14 @@ end
8484
---@return File[]
8585
function Files.all()
8686
Files.ensure_loaded()
87-
local files = vim.tbl_values(Files.orgfiles)
88-
table.sort(files, function(a, b)
89-
return a.category < b.category
90-
end)
91-
return files
87+
local valid_files = {}
88+
local filenames = config:get_all_files()
89+
for _, file in ipairs(filenames) do
90+
if Files.orgfiles[file] then
91+
table.insert(valid_files, Files.orgfiles[file])
92+
end
93+
end
94+
return valid_files
9295
end
9396

9497
---@return string[]

tests/minimal_init.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ parser_config.org = {
2222
}
2323

2424
require('orgmode').setup({
25-
org_agenda_files = vim.fn.getcwd() .. '/tests/plenary/fixtures/*',
25+
org_agenda_files = { vim.fn.getcwd() .. '/tests/plenary/fixtures/*' },
2626
org_default_notes_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/refile.org',
2727
})
2828
EOF

tests/plenary/notifications/notifications_spec.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ local config = require('orgmode.config')
77
local last_filename = nil
88

99
describe('Notifications', function()
10+
local default_org_agenda_files = vim.deepcopy(config.org_agenda_files)
1011
before_each(function()
1112
Files.loaded = true
1213
end)
1314
after_each(function()
1415
Files.loaded = false
16+
config.org_agenda_files = default_org_agenda_files
1517
end)
1618
it('should find headlines for notification', function()
17-
local filename = vim.fn.tempname()
19+
local filename = vim.fn.tempname() .. '.org'
20+
vim.fn.writefile({}, filename) -- make sure glob() reads it
1821
last_filename = filename
1922
local lines = {
2023
'* TODO I am the deadline task :OFFICE:',
@@ -27,6 +30,7 @@ describe('Notifications', function()
2730
' SCHEDULED: <2021-07-12 Mon 19:30>',
2831
}
2932
local orgfile = File.from_content(lines, 'work', filename)
33+
table.insert(config.opts.org_agenda_files, filename)
3034
Files.orgfiles[filename] = orgfile
3135
local notifications = Notifications:new()
3236
assert.are.same({}, notifications:get_tasks(Date.from_string('2021-07-11 Sun 12:30')))
@@ -78,7 +82,8 @@ describe('Notifications', function()
7882
},
7983
})
8084

81-
local filename = vim.fn.tempname()
85+
local filename = vim.fn.tempname() .. '.org' -- make sure glob() reads it
86+
vim.fn.writefile({}, filename)
8287
last_filename = filename
8388
local lines = {
8489
'* TODO I am the deadline task :OFFICE:',
@@ -91,6 +96,7 @@ describe('Notifications', function()
9196
' SCHEDULED: <2021-07-14 Wed 19:30>',
9297
}
9398
local orgfile = File.from_content(lines, 'work', filename)
99+
table.insert(config.opts.org_agenda_files, filename)
94100
Files.orgfiles[filename] = orgfile
95101
local notifications = Notifications:new()
96102
assert.are.same({}, notifications:get_tasks(Date.from_string('2021-07-13 Sun 12:30')))

0 commit comments

Comments
 (0)