Skip to content

Agenda sort #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
79a0c78
if no HH:MM then sort after
bgerazov Nov 22, 2021
ad47fe0
reverse logic
bgerazov Nov 22, 2021
fde2780
moved check to sort function
bgerazov Nov 22, 2021
ae6415d
added the same to reversed case
bgerazov Nov 22, 2021
f58d3b1
added proper reference
bgerazov Nov 22, 2021
42d7229
added lines below
bgerazov Nov 22, 2021
30dcd34
last try
bgerazov Nov 22, 2021
0ac91a7
sorting date onlys first
bgerazov Nov 23, 2021
f6539b6
changed true to false
bgerazov Nov 23, 2021
d314998
sort function doesn't work
bgerazov Nov 23, 2021
ca76fd1
separate date_only from timed events before sorting
bgerazov Nov 23, 2021
ea0b924
concatenate tables before insert
bgerazov Nov 23, 2021
24935c9
concatenate agenda items
bgerazov Nov 23, 2021
4afd6bc
agenda items concatenation fix
bgerazov Nov 23, 2021
390636c
commented out file sorting
bgerazov Nov 23, 2021
471673a
sort not same days at end
bgerazov Nov 23, 2021
b7720ed
same day and date_only
bgerazov Nov 23, 2021
def02ca
same day correctly referenced
bgerazov Nov 23, 2021
7a7fb0f
agenda_item.is_same_day
bgerazov Nov 23, 2021
3001ab6
sorting not_same_day last
bgerazov Nov 23, 2021
3b1231d
not same day
bgerazov Nov 23, 2021
6040df2
working on sorting tasks
bgerazov Nov 23, 2021
365815d
access category through headline
bgerazov Nov 23, 2021
d305438
fix bug
bgerazov Nov 23, 2021
a25b71b
refactoring everything to the sort function
bgerazov Nov 27, 2021
a86f62c
reverse time of day based sorting
bgerazov Nov 28, 2021
7b9c4a2
print some debug output
bgerazov Nov 28, 2021
3394c94
added debug printing function
bgerazov Nov 28, 2021
ea486af
fix utils.tprint
bgerazov Nov 28, 2021
bbfd631
fix tprint
bgerazov Nov 28, 2021
cc0d1e2
reduce printing
bgerazov Nov 28, 2021
c5e8eb2
minor fix
bgerazov Nov 28, 2021
9898771
print bool
bgerazov Nov 28, 2021
baf1dc1
no printing
bgerazov Nov 29, 2021
a7b3987
added deadline logic to sort
bgerazov Dec 3, 2021
3ef9f81
overdue > deadline
bgerazov Dec 3, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 70 additions & 12 deletions lua/orgmode/agenda/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,104 @@ local Search = require('orgmode.parser.search')
local AgendaFilter = require('orgmode.agenda.filter')
local hl_map = agenda_highlights.get_agenda_hl_map()

--@return table[]
local function get_category_inds()
local files = config:get_all_files()
local categories = config:get_categories(files)
local category_inds = {}
for i, category in ipairs(categories) do
category_inds[category] = i
end
return category_inds
end

---@param agenda_items AgendaItem[]
---@return AgendaItem[]
local function sort_agenda_items(agenda_items)
local category_inds = get_category_inds()

table.sort(agenda_items, function(a, b)
if a.is_today and a.is_same_day then
-- sort items with a time of day in order of scheduling
if not a.headline_date.date_only and not b.headline_date.date_only then
if a.is_today and a.is_same_day then
if b.is_today and b.is_same_day then
return a.headline_date:is_before(b.headline_date)
end
return true
end

if b.is_today and b.is_same_day then
return a.headline_date:is_before(b.headline_date)
if a.is_today and a.is_same_day then
return a.headline_date:is_before(b.headline_date)
end
return false
end
return true
end

if b.is_today and b.is_same_day then
if a.is_today and a.is_same_day then
return a.headline_date:is_before(b.headline_date)
end
-- sort items with a time of day before ones that have a date only
-- true means sort a before b
if not a.headline_date.date_only and b.headline_date.date_only then
return true
end
if a.headline_date.date_only and not b.headline_date.date_only then
return false
end

-- else both items are date only, sort in this order:
-- category > priority > overdue deadline > overdue schedule >
-- > today deadline > today schedule

-- if different categories sort by category
if a.headline:get_category() ~= b.headline:get_category() then
return category_inds[a.headline:get_category()] < category_inds[b.headline:get_category()]
end

-- if different priorities sort by priority
if a.headline:get_priority_sort_value() ~= b.headline:get_priority_sort_value() then
return a.headline:get_priority_sort_value() > b.headline:get_priority_sort_value()
end

if a.headline:has_priority() and b.headline:has_priority() then
return a.headline_date:is_before(b.headline_date)
-- overdue > today > future
if a.is_today and a.is_same_day then
if b.is_today and not b.is_same_day then
return a.headline_date:is_before(b.headline_date)
end
end

if b.is_today and b.is_same_day then
if a.is_today and not a.is_same_day then
return a.headline_date:is_before(b.headline_date)
end
end

if a.is_in_date_range and not b.is_in_date_range then
return false
end

if not a.is_in_date_range and b.is_in_date_range then
return true
end

-- if same due sort by deadline
if a.headline_date:is_deadline() and not b.headline_date:is_deadline() then
return true
elseif not a.headline_date:is_deadline() and b.headline_date:is_deadline() then
return false
-- either both are deadlines or both are not deadlines
-- overdue deadline and overdue schedule > deadline today and schedule today
end

return a.headline_date:is_before(b.headline_date)
end)
return agenda_items
end

local function sort_agenda_items_categories(agenda_items, category_inds)
table.sort(agenda_items, function(a, b)
return category_inds[a.headline:get_category()] < category_inds[b.headline:get_category()]
end)
return agenda_items
end

local function sort_todos(todos)
table.sort(todos, function(a, b)
if a:get_priority_sort_value() ~= b:get_priority_sort_value() then
Expand Down Expand Up @@ -461,10 +520,9 @@ function Agenda:agenda()
for _, item in ipairs(headline_dates) do
local agenda_item = AgendaItem:new(item.headline_date, item.headline, day)
if agenda_item.is_valid and self.filters:matches(item.headline) then
table.insert(date.agenda_items, agenda_item)
table.insert(date.agenda_items, agenda_item)
end
end

date.agenda_items = sort_agenda_items(date.agenda_items)

table.insert(agenda_days, date)
Expand Down
10 changes: 10 additions & 0 deletions lua/orgmode/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ function Config:_deprecation_notify(opts)
end
end

---@return string[]
function Config:get_categories(files)
local categories = {}
for _, item in ipairs(files) do
local category = vim.fn.fnamemodify(item, ':t:r')
table.insert(categories, category)
end
return categories
end

---@return string[]
function Config:get_all_files()
local all_filenames = {}
Expand Down
7 changes: 4 additions & 3 deletions lua/orgmode/parser/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ end
function Files.all()
Files.ensure_loaded()
local files = vim.tbl_values(Files.orgfiles)
table.sort(files, function(a, b)
return a.category < b.category
end)
-- this sorts files alphabetically, I'm not sure if it's useful
-- table.sort(files, function(a, b)
-- return a.category < b.category
-- end)
return files
end

Expand Down
27 changes: 27 additions & 0 deletions lua/orgmode/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,31 @@ function utils.promisify(fn)
return fn
end

---debug printing a table
---source https://stackoverflow.com/questions/41942289/display-contents-of-tables-in-lua
function utils.tprint(tbl, indent)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this and use vim.inspect(tbl) instead.

if not indent then indent = 0 end
local toprint = string.rep(" ", indent) .. "{\r\n"
indent = indent + 2
for k, v in pairs(tbl) do
toprint = toprint .. string.rep(" ", indent)
if (type(k) == "number") then
toprint = toprint .. "[" .. k .. "] = "
elseif (type(k) == "string") then
toprint = toprint .. k .. "= "
end
if (type(v) == "number") then
toprint = toprint .. v .. ",\r\n"
elseif (type(v) == "string") then
toprint = toprint .. "\"" .. v .. "\",\r\n"
elseif (type(v) == "table") then
toprint = toprint .. utils.tprint(v, indent + 2) .. ",\r\n"
else
toprint = toprint .. "\"" .. tostring(v) .. "\",\r\n"
end
end
toprint = toprint .. string.rep(" ", indent-2) .. "}"
return toprint
end

return utils