4

I want to archive an entry automatically when its todo state gets marked as done.

The problem is that you can set up lots of different states that mean done. So a function like this will only work if the done state is literally called DONE:

(defun channing/archive-when-done ()
    (when (equal (org-get-todo-state) "DONE") (org-archive-subtree-default)))
(add-hook 'org-after-todo-state-change-hook
    'channing/archive-when-done)

How can I detect if an entry is in the done state, regardless of the particular keyword?

1 Answer 1

4

You can detect if an org entry is done with the (undocumented) predicate function org-entry-is-done-p. It tests the current entry's todo keyword against org-done-keywords, which is also undocumented. (I discovered these gems after reinventing this particular wheel symbol-for-symbol.)

For your purposes, you could rewrite your function as:

(defun channing/archive-when-done ()
  "Archive current entry if it is marked as DONE (see `org-done-keywords')."
  (when (org-entry-is-done-p)
    (org-archive-subtree-default)))
4
  • I think there are problems with this, items get moved but sometimes I get a line like this left in my todo.org: - State "DONE" from "TODO" [2016-02-13 Sat 22:32] which should be with the moved item. Commented Feb 13, 2016 at 22:34
  • 1
    @ChanningWalton: hmm, well, for the narrow purposes of the question (how to detect if entry is done), then org-entry-is-done-p is what you need. As for the broader question of the extra material left in your org file, that's a bug in your archive functionality, unfortunately. Good luck tracking it down!
    – Dan
    Commented Feb 13, 2016 at 23:30
  • It seems I have some entertainment ahead of me! Commented Feb 13, 2016 at 23:31
  • 1
    @ChanningWalton: most certainly! The org innards are <cough> <cough> baroque, so I don't envy you the task.
    – Dan
    Commented Feb 13, 2016 at 23:33

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.