1

I would like to set/unset a property when entering a certain TODO state.

Example: Set the property :COOKIE_DATA: todo recursive automatically when the state is set to PROJ. So all the TODOs in project headings would be counted recursively, and not only TODOs which are direct children of the current tree (default).

How could this be done?

I do not want to set org-hierarchical-todo-statistics to activate recursive counting for every TODO globally.

1
  • 1
    Write a function to add (or delete) the property and add it to org-after-todo-state-change-hook. The function should check the current TODO state: if it is PROJ add the property, otherwise delete it. See the Property API section of the manual with C-h i g(org)Using the Property API for relevant functions.
    – NickD
    Commented Aug 5, 2021 at 13:25

1 Answer 1

2

Table of Contents

  1. The answer
  2. A minimal working example

The answer

As mentioned by @NickD, you can accomplish by writing a hook.

(defun my/org-set-cookie-data-when-state-changes-to-proj ()
  (interactive)
  (when (equal (org-get-todo-state) "PROJ")
    (org-set-property "COOKIE_DATA" "todo recursive")))
(add-hook 'org-after-todo-state-change-hook 'my/org-set-cookie-data-when-state-changes-to-proj)

A minimal working example

Consider the following minimal working exmaple

#+TODO: TODO(t) | DONE(d)
#+TODO: NOTPROJ(n) | PROJ(p)

* This is a heading
* This is another heading

After changing the states of the headings with org-todo (by default, bound to C-c C-t) , we get the following

#+TODO: TODO(t) | DONE(d)
#+TODO: NOTPROJ(n) | PROJ(p)

* PROJ This is a heading
:PROPERTIES:
:COOKIE_DATA: todo recursive
:END:

* DONE This is another heading
2
  • I don't know a lot of elisp yet, and had some reservations getting my hands wet. But this looks managable. Thank you for writing it out! How would I remove the property when changing back to "TODO"? Is there also a function for that? Commented Aug 14, 2021 at 15:23
  • 1
    Got it, its org-delete-property. I will try to edit the function to reset property when going back to TODO. -- It works :-) Commented Aug 14, 2021 at 15:34

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.