reddit / emacs

Elisp function to determine the number of parent TODOs that a heading has

mack1070 · 2022-04-12 21:50
I'm trying to write a function that will allow my `org-agenda` to display TODO items indented by the number of parent TODOs it has. I know that you can use to indent tasks by level (tags-todo "example" ((org-agenda-overriding-header "example") (org-agenda-prefix-format '((tags . "%i %l"))))) Which produces something like this: TODO This task has 5 stars in the heading TODO This task has 6 stars in the heading I would like it to Produce something like this TODO This is a top level TODO TODO this is a subtask TODO this is a sub-sub task Has anyone tried to do something like this before? Essentially I'm trying to determine the number of TODOs that are above a given TODO not the number of headings
deltille · 2022-04-14 08:15
This is for org-agenda-custom-commands, right? I've been meaning to learn more about org-agenda's formatting code for a while, so this intrigued me to go digging and find no built-in answer for this. But let me know - does this do what you wanted? (defun d/count-todo-ancestors () (org-with-wide-buffer (length (let (x) (while (org-up-heading-safe) (when (org-entry-is-todo-p) (cl-pushnew (point) x))) x)))) (defun d/ancestor-todo-count-to-spaces () (format (format "%%-%ds" (d/count-todo-ancestors)) " ") ) (setq org-agenda-custom-commands '( ("t" "test" tags-todo "example" ((org-agenda-overriding-header "example") (org-agenda-prefix-format '((tags . "%(d/ancestor-todo-count-to-spaces)"))))) )) This is based on code I found in a few places, primarily org-get-outline-path and org-show-context. The latter less for its actual code, more for the explicit definition of 'ancestor' in org-show-context-detail docstring. This all hinges on org-agenda-prefix-format having a "%()" specifier to run arbitrary lisp. The "%l" specifier built into it adds spaces in a way I *think* is saner but gave me too much of a headache to figure out. At any rate, I'll let you test it out before rambling further.
mack1070 · 2022-04-15 22:19
That's correct! This works perfectly. Thank you. I did make one small tweak to how the string is generated (make-string d/count-todo-ancestors 32) It's super nice to have tasks and subtasks represented this way in the agenda, because I found going by level makes them indent too deeply in some cases
deltille · 2022-04-15 23:59
Oh thank you for make-string, that's what I was looking for to generate it in the first place but I didn't know the function for it :)