orgmode icon indicating copy to clipboard operation
orgmode copied to clipboard

Configure group based sorting in agenda

Open gerazov opened this issue 3 years ago • 34 comments

Does this feature exist in Emacs orgmode core?

N/A

Orgmode link

No response

Feature value

The sorting of tasks in the agenda view seems a bit arbitrary - I get a mix of NEXT and TODO items, and the files they come from are also mixed even if all the tasks are for the same date. I couldn't find how sorting is done in the help file...

For e.g. I get:

понеделник 01 ноември 2021
  work:       Scheduled:  NEXT task name
  nehm:       Scheduled:  NEXT task name
  nehm:       Scheduled:  NEXT task name
  pros:       Scheduled:  NEXT task name
  work:       Scheduled:  NEXT task name
  nehm:       Scheduled:  NEXT task name
  nehm:       Scheduled:  TODO task name
  evoc:       Scheduled:  TODO task name
  nehm:       Scheduled:  NEXT task name

It would be very helpful if there is a way to group items based on keywords and files, and possibly set different priorities for sorting them in the agenda, e.g. DONE > DOING > NEXT > TODO :+1:

Additional context

No response

gerazov avatar Nov 02 '21 07:11 gerazov

Currently this logic is used: https://orgmode.org/manual/Sorting-of-agenda-items.html

But it seems this should be configurable with this: https://orgmode.org/worg/doc.html#org-agenda-sorting-strategy. It seems that there is no a specific setting to sort by the todo keyword. Both TODO and NEXT keywords are basically a TODO state, and orgmode allows sorting by TODO -> DONE or vice versa.

kristijanhusak avatar Nov 02 '21 08:11 kristijanhusak

Ok so it says:

For the daily/weekly agenda, the items for each day are sorted. The default order is to first collect all items containing an explicit time-of-day specification. These entries are shown at the beginning of the list, as a schedule for the day. After that, items remain grouped in categories, in the sequence given by org-agenda-files. Within each category, items are sorted by priority (see Priorities), which is composed of the base priority (2000 for priority ‘A’, 1000 for ‘B’, and 0 for ‘C’), plus additional increments for overdue scheduled or deadline items.

In orgmode.nvim the specific time todos are listed after the todos without a time of day, this is less useful, i.e. the emacs way sounds better.

And the items are not grouped in categories in my case, even if I pass an explicit list of files rather than `org/*' they don't get sorted - I get the same mix as before.

:+1: for sorting them by todo state too even if not emacs style :wink:

gerazov avatar Nov 02 '21 09:11 gerazov

Something like this (code here and from an article here) would be something that users would have to add manually, right? Based on the linked code, it appears that jethrokuan has items sorted based on their TODO state in separate files, then just has a title and list if items that come together to make up a given view.

This looks like it would be something like this, which would accommodate for not just this use-case but realistically any "custom agenda view" use case.

Not 100% sure of the state right now, but would something like this require changes to the core code (this repo.) @kristijanhusak?

levouh avatar Nov 02 '21 14:11 levouh

@levouh If we would add some similar configuration, then yeah, it would require some changes, and probably not so small ones. I'm not even sure what's possible with the custom agenda command, I would have to investigate. There is like an AgendaItem that basically wraps all logic around determining what and where to show, so that can be reused, but rendering itself would need changes.

kristijanhusak avatar Nov 02 '21 16:11 kristijanhusak

Makes sense, I'll see if I can't come up with a good summary before taking a stab at what the code might actually look like. Can open up a separate issue to track it as well, whichever you'd prefer.

The main start on the issue would be:

  1. Summary of how the functionality works in Emacs
  2. What is applicable change-wise here, and what changes should be a part of the core

levouh avatar Nov 02 '21 21:11 levouh

Makes sense, I'll see if I can't come up with a good summary before taking a stab at what the code might actually look like. Can open up a separate issue to track it as well, whichever you'd prefer.

The main start on the issue would be:

  1. Summary of how the functionality works in Emacs
  2. What is applicable change-wise here, and what changes should be a part of the core

Awesome, thanks! That will help tremendously. You can open up a separate enhancement issue, since this is related only to sorting.

kristijanhusak avatar Nov 02 '21 21:11 kristijanhusak

Ok, after some playing around with the sorting in agenda/init.lua I've finally fixed this :sweat_smile:

The problem was that the all the tasks were sorted based on their timestamp. This meant that date_only tasks were randomly sorted and all of them went before the tasks scheduled with times of day (I guess os.time() was defaulting to 00:00 AM).

Now tasks with time of day are only sorted and they come first in the agenda view, followed by the tasks set without.

Before (notice how the two tasks from ea without time of day get separated): image

After: image

One thing that I still want to improve is for the tasks to follow the order of the files as given to orgmode in the config. This would allow users to e.g. put their personal task org last so work tasks comes before or vice versa. To this end I've commented out the file sorting in parser/files.lua in Files.all(), but still the order is not the same as in the config - I guess it's the async reading of the files (every reload gives a different order in the agenda):

    vim.schedule_wrap(function(err, content)
      if err then
        return callback(nil)
      end
      return callback(File.from_content(content, category, path, ext == 'org_archive'))
    end)

gerazov avatar Nov 23 '21 22:11 gerazov

Ok the ordering is now fixed :love_you_gesture: and the files are still read asyncly.

Here's what I get for the same date (assuming work > ea > valence) image

gerazov avatar Nov 23 '21 23:11 gerazov

@gerazov can you double check how Emacs orgmode is doing the sorting? I think I adjusted it to work the same way as there, but maybe I missed something. If it's the same as there, lets go with some solution to allow configuring the sorting as a user, and leave this one as default. As a first (temporary) step we could have a sorting function exposed in the configuration, which you can override to your needs. Once https://github.com/nvim-orgmode/orgmode/issues/135 is done, this function would be removed or changed to adapt the new functionality.

kristijanhusak avatar Nov 24 '21 10:11 kristijanhusak

Yeah it's like that in the specs as far as I can tell (and it feels right too :sweat_smile: )

For the daily/weekly agenda, the items for each day are sorted. The default order is to first collect all items containing an explicit time-of-day specification. These entries are shown at the beginning of the list, as a schedule for the day. After that, items remain grouped in categories, in the sequence given by org-agenda-files. Within each category, items are sorted by priority (see Priorities), which is composed of the base priority (2000 for priority ‘A’, 1000 for ‘B’, and 0 for ‘C’), plus additional increments for overdue scheduled or deadline items.

Plus the randomization is minimized to within category listings, which is a bit weird since they are read from the same file :thinking:

I haven't added the priorities though for the no time-of-day entries, will do that and update. I'm not sure about the

additional increments for overdue of scheduled or deadline items.

It reads to me that first comes the overdue deadline, then the overdue scheduled item, then the priority items, then the regular items, no?

I can also implement this for the todo-s list.

gerazov avatar Nov 25 '21 12:11 gerazov

Ok, it's now taking deadlines into account, but deadlines are sorted before overdue scheduled items. I'm not sure how it is in emacs as the spec doesn't read very exact. Here's what I get for 3 deadlines (1 overdue, 1 today, 1 tomorrow), 1 overdue scheduled item and 1 today item:

image

I'm happy with this, how does it look to you guys?

gerazov avatar Dec 03 '21 22:12 gerazov

@levouh @gerazov I did some refactoring on agenda to allow easier introduction of custom commands. If you find any issues with current functionality let me know.

@gerazov you will have to rebase your sort PR because I split different agenda view in different files.

kristijanhusak avatar Dec 06 '21 13:12 kristijanhusak

Ok will do :+1:

I'm not sure about the functionality still - I'll have to take a closer look. I get mixing of scheduled and deadlines according to overdue date...

image

gerazov avatar Dec 06 '21 13:12 gerazov

If you want I'll look into this during this week, you just need to be a bit patient :) I want to make it 1 to 1 with Emacs orgmode so it's consistent.

kristijanhusak avatar Dec 06 '21 14:12 kristijanhusak

Yes please :sweat_smile: no hurry :wink:

gerazov avatar Dec 06 '21 14:12 gerazov

I did some refactoring on agenda to allow easier introduction of custom commands. If you find any issues with current functionality let me know

Will do. Once I'm done with the org-edit-special stuff I plan on aggregating my notes into #135 and we can go from there. This refactor looks like it will only help, so should be good to go, thanks @kristijanhusak.

levouh avatar Dec 06 '21 16:12 levouh

@gerazov I pushed a change that should make agenda sorting exactly the same as Emacs in orgmode, at least it was same in my tests. Please give it a test, and also compare it to the Emacs orgmode to confirm if it's really the same.

kristijanhusak avatar Dec 09 '21 21:12 kristijanhusak

Ok awesome :sunglasses: I'll give it a go!

gerazov avatar Dec 13 '21 13:12 gerazov

I get an empty agenda ...

image

Maybe it doesn't find my org files :thinking: - I list them in my config as such:

  org_agenda_files = {
      '~/org/work.org',
      '~/org/ea.org',
      '~/org/bme.org',
      '~/org/personal.org',
      },

gerazov avatar Dec 16 '21 07:12 gerazov

@gerazov did you do :TSUpdate org ? Parser got some updates recently.

kristijanhusak avatar Dec 16 '21 07:12 kristijanhusak

Tried that now - still same problem. It's fine if I use my fork, so I guess it isn't neovim doing funny business.

gerazov avatar Dec 16 '21 10:12 gerazov

@gerazov Try doing TSUninstall org and then TSInstall org, update wasn't sufficient for some people.

kristijanhusak avatar Dec 16 '21 11:12 kristijanhusak

Yup works! :pray:

The sorting is not ok though - the tasks that do not have a time of day stamp should be sorted by category first:

For the daily/weekly agenda, the items for each day are sorted. The default order is to first collect all items containing an explicit time-of-day specification. These entries are shown at the beginning of the list, as a schedule for the day. After that, items remain grouped in categories, in the sequence given by org-agenda-files. Within each category, items are sorted by priority (see Priorities), which is composed of the base priority (2000 for priority ‘A’, 1000 for ‘B’, and 0 for ‘C’), plus additional increments for overdue scheduled or deadline items.

gerazov avatar Dec 16 '21 11:12 gerazov

I would suggest to set up basic emacs installation and compare with that. I did 1 to 1 comparison while setting up the sorting. Maybe what is written and what really happens is not exactly the same.

kristijanhusak avatar Dec 16 '21 11:12 kristijanhusak

That doesn't seem right. Did you try with multiple categories?

Now the overdue items get sorted before today's items, which let's say is ok, but not ideal (and not following specs). Still, even with all the scheduled items for today I get a mix of categories. This is surely not useful (i.e. I can't imagine what are they prioritized with?) And things really gets messy once you have 10+ org files and the tasks from different categories get mixed.

Here's a glimpse (these are all tasks scheduled for today :sweat_smile:): image

I implemented category based sorting in my fork and it works well:

    -- 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

gerazov avatar Dec 16 '21 11:12 gerazov

Is this a screenshot from Neovim or emacs Orgmode? For me sorting by the schedule/deadline date has more sense then to sort by category. Category is just some kind of label to me, doesn't really give any value to the task itself. If I'm overdue on something, I'd like to see that first. I didn't think of this sorting functionality myself, as I said, I did 1 to 1 copy from Emacs orgmode. They have more options to offer beside this one, but this is the default (I think called "priority-up"). I think they also support setting up custom sorting function, so I'll probably expose something similar so you can override it as you wish.

kristijanhusak avatar Dec 16 '21 11:12 kristijanhusak

Yeah I can go with the overdue priority sorting, but the sorting for tasks scheduled for the same day (with no/equal priorities) should be category based no?

The screenshot is with neovim.

gerazov avatar Dec 16 '21 13:12 gerazov

I think it just uses the default sorting, which is as things appear in the files. It would be best to compare both of them since you have bunch of files, and if you find any differences let me know.

kristijanhusak avatar Dec 16 '21 15:12 kristijanhusak

I've given it another go - rebasing and squashing. I can't figure out how to apply the category sorting to the same date items only :sweat_smile:

The default sorting is actually the problem because the files are read asynchronously - this mixes them up. But, I would keep the async loading mosdef :+1:

gerazov avatar Dec 16 '21 15:12 gerazov

Ok installing emacs and configuring orgmode seems quite involved - that's how I came here in the first place :sweat_smile:

What I've done is create a minimum working example.

Here are three identical files having 4 tasks scheduled for today. I had to rename them to .md - GitHub doesn't like orgmode apparently :smile:

* TODO Task 1
  SCHEDULED: <2021-12-17 пет>
* TODO Task 2
  SCHEDULED: <2021-12-17 пет>
* TODO Task 3
  SCHEDULED: <2021-12-17 пет>
* TODO Task 4
  SCHEDULED: <2021-12-17 пет>

test1.md test2.md test3.md

Now when I run the agenda it shows:

image

I don't know if emacs reproduces this, but this mixing of categories and tasks within categories doesn't make sense. In fact, it's counter productive.

Also based on this MWE I'm not sure if it's the async loading doing it anymore - it seems random (yet refreshing it doesn't change the ordering) :thinking:

gerazov avatar Dec 17 '21 16:12 gerazov

I've made the category based sorting in the PR, but it follows the spec, i.e. sorts by category and then by overdueness. (within category I still get the weird mix but this doesn't bother me as the position of the task in the file is not that important).

image

I've tried making it work overdue > category but can't make it work ...

The expanded files have three more old tasks:

* TODO Task 1
  SCHEDULED: <2021-12-17 пет>
* TODO Task 2
  SCHEDULED: <2021-12-17 пет>
* TODO Task 3
  SCHEDULED: <2021-12-17 пет>
* TODO Task 4
  SCHEDULED: <2021-12-17 пет>
* TODO Task 5
  SCHEDULED: <2021-12-16 чет>
* TODO Task 6
  SCHEDULED: <2021-12-15 сре>
* TODO Task 7
  SCHEDULED: <2021-12-14 вто>

They are here: test1.md test2.md test3.md

gerazov avatar Dec 17 '21 16:12 gerazov

Ok with 7ba698a it now works based on overdues + categories:

image

gerazov avatar Dec 17 '21 19:12 gerazov

I've also added deadlines in the mix. At first it's mixing them with the regular items:

image

But with https://github.com/nvim-orgmode/orgmode/pull/187/commits/f6678a4b6a5efd09cad3d958665f79dd00942466 it's ok :love_you_gesture:

image

gerazov avatar Dec 17 '21 20:12 gerazov

@gerazov I tested your files with emacs orgmode, and you were right, it sorts by category in these cases. I pushed a fix where sorting falls back to the default order of headlines being loaded. Pull latest master and give it a test.

kristijanhusak avatar Dec 18 '21 15:12 kristijanhusak