Skip to content

Added category sorting #187

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 11 commits into from
41 changes: 41 additions & 0 deletions lua/orgmode/agenda/views/agenda.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,21 @@ local AgendaItem = require('orgmode.agenda.agenda_item')
local AgendaFilter = require('orgmode.agenda.filter')
local utils = require('orgmode.utils')

--@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_same_day and b.is_same_day then
if not a.headline_date.date_only and not b.headline_date.date_only then
Expand All @@ -18,9 +30,24 @@ local function sort_agenda_items(agenda_items)
if not a.headline_date.date_only then
return true
end
if not b.headline_date.date_only then
return false
end
-- if both are date only check if one is a deadline
if a.headline_date:is_deadline() and not b.headline_date:is_deadline() then
return true
end
if not a.headline_date:is_deadline() and b.headline_date:is_deadline() then
return false
end
-- if both are the same check 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
return false
end


if a.is_same_day and not b.is_same_day then
if not a.headline_date.date_only or (b.headline_date:is_none() and not a.headline_date:is_none()) then
return true
Expand All @@ -37,6 +64,20 @@ local function sort_agenda_items(agenda_items)
return a.headline:get_priority_sort_value() > b.headline:get_priority_sort_value()
end

-- if both are on the same day
if a.headline_date:is_same(b.headline_date) then
-- check deadline
if a.headline_date:is_deadline() and not b.headline_date:is_deadline() then
return true
end
if not a.headline_date:is_deadline() and b.headline_date:is_deadline() then
return false
end
-- check 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
end
return a.headline_date:is_before(b.headline_date)
end)
return agenda_items
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