diff --git a/lua/orgmode/agenda/views/agenda.lua b/lua/orgmode/agenda/views/agenda.lua index 57bda81c6..9a5917f88 100644 --- a/lua/orgmode/agenda/views/agenda.lua +++ b/lua/orgmode/agenda/views/agenda.lua @@ -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 @@ -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 @@ -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 diff --git a/lua/orgmode/config/init.lua b/lua/orgmode/config/init.lua index 6cb90b67e..db42e16b1 100644 --- a/lua/orgmode/config/init.lua +++ b/lua/orgmode/config/init.lua @@ -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 = {}