0

One can use org-entry-is-done-p to check whether a headline is locally done:

(defun org-entry-is-done-p ()
  (member (org-get-todo-state) org-done-keywords))

So for example Task B is considered "done" using this methodology:

* Task A
** COMPLETE Task B
   CLOSED: [2021-03-08 Mon 15:51]
*** Task C

Question: How can one modify this function that it will consider Task C also done? That is, how can one make a function that checks whether the current headline or any of its parents is done?

0

1 Answer 1

2

You can use (outline-up-heading 1) to step up through the subtree headings, checking each one as you go. You are done when you've checked the top level of the subtree.

Here's an implementation:

#+begin_src elisp
(defun ndk/org-entry-is-hierarchically-done-p ()
  (catch 'exit
    (save-excursion
      (while t
        ;; if this is a DONE entry, return t
        (when (org-entry-is-done-p)
          (throw 'exit t))
        ;; if we are at top level, return nil
        (when (= (org-outline-level) 1)
          (throw 'exit nil))
        ;; if we cannot go up any further, return nil
        (when (not (outline-up-heading 1))
          (throw 'exit nil))))))

  #+end_src

We loop over all the levels. At each level, starting from the lowest and going up, we check if the state is DONE: if so, we break out of the loop immediately returning t. If not, we check whether we are at the top level and if so, break out of the loop returning nil. Otherwise we check whether we can go up a level: if not, we return nil; if yes, we try again at the new level.

6
  • Is there a way to acommodate for the case when the root heading doesn't have org-outline-level 1 (i.e. the root heading starts at a sublevel)?
    – George
    Commented Mar 9, 2021 at 5:45
  • See the revised code. I believe it fixes the bug.
    – NickD
    Commented Mar 9, 2021 at 6:11
  • The function gets stuck in the first when statement when the root level has an outline-level greater than 1.
    – George
    Commented Mar 9, 2021 at 17:15
  • I tested with the root level being at level 1 an also at level 2 and it worked in both cases. Can you post a sample document on which it does not work? You can edit your question and add it.
    – NickD
    Commented Mar 9, 2021 at 17:39
  • Apologies. My mistake.
    – George
    Commented Mar 9, 2021 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.