Support for ON_CLEAR when the state is removed from a task

This commit is contained in:
Bram Schoenmakers 2022-11-26 19:55:13 +01:00
parent 41dbfc90ac
commit 0ba591d952
2 changed files with 40 additions and 12 deletions

View file

@ -127,17 +127,30 @@ My vertico-repeat setup.
* Execute code on task state changes :emacs:org:
#+begin_src org :tangle gists/execute-code-on-task-state-change.org
The function below executes Lisp forms stored in properties of a TODO item, when it reaches a certain state. The function is supposed to be added to the =org-after-todo-state-change-hook=.
The function below executes Lisp forms stored in properties of a task, when it changes to a certain state. The function is supposed to be added to the =org-after-todo-state-change-hook=.
The property should be named =ON_<STATE>= where =STATE= is a state defined in =org-todo-keywords=. See also the example task below (open this file in [[https://apps.bram85.nl/gitea/bram/gists/raw/branch/main/gists/execute-code-on-task-state-change.org][raw mode]] to see the properties).
,#+begin_src elisp
(defun my/task-state-event-handler ()
"Called as part of org-after-todo-state-change-hook, which looks
for a property in the todo item at point and looks for properties
containing code that needs to be executed."
"Execute a Lisp form attached to the task whose state is being changed.
(when-let* ((event-property-name (concat "ON_" org-state))
When this function is added to org-after-todo-state-change-hook,
it looks for a Lisp form stored in the property called ON_<STATE>
where STATE is the new state of the todo item. When the state is
cleared, ON_CLEAR will be used.
Example:
,,* TODO Example task
:PROPERTIES:
:ON_PROGRESS: (message \"Busy!\")
:ON_DONE: (message \"Done!\")
:ON_CLEAR: (message \"No state.\")
:END:"
(when-let* ((state (or org-state "CLEAR"))
(event-property-name (concat "ON_" state))
(code-string (cdr (assoc event-property-name
(org-entry-properties))))
(code (car (read-from-string code-string))))
@ -149,7 +162,8 @@ My vertico-repeat setup.
,* TODO Example task
:PROPERTIES:
:ON_PROGRESS: (message "Busy!")
:ON_DONE: (message "Done!")
:ON_DONE: (message "Done!")
:ON_CLEAR: (message "No state.")
:END:
#+end_src

View file

@ -1,14 +1,27 @@
The function below executes Lisp forms stored in properties of a TODO item, when it reaches a certain state. The function is supposed to be added to the =org-after-todo-state-change-hook=.
The function below executes Lisp forms stored in properties of a task, when it changes to a certain state. The function is supposed to be added to the =org-after-todo-state-change-hook=.
The property should be named =ON_<STATE>= where =STATE= is a state defined in =org-todo-keywords=. See also the example task below (open this file in [[https://apps.bram85.nl/gitea/bram/gists/raw/branch/main/gists/execute-code-on-task-state-change.org][raw mode]] to see the properties).
#+begin_src elisp
(defun my/task-state-event-handler ()
"Called as part of org-after-todo-state-change-hook, which looks
for a property in the todo item at point and looks for properties
containing code that needs to be executed."
"Execute a Lisp form attached to the task whose state is being changed.
(when-let* ((event-property-name (concat "ON_" org-state))
When this function is added to org-after-todo-state-change-hook,
it looks for a Lisp form stored in the property called ON_<STATE>
where STATE is the new state of the todo item. When the state is
cleared, ON_CLEAR will be used.
Example:
,* TODO Example task
:PROPERTIES:
:ON_PROGRESS: (message \"Busy!\")
:ON_DONE: (message \"Done!\")
:ON_CLEAR: (message \"No state.\")
:END:"
(when-let* ((state (or org-state "CLEAR"))
(event-property-name (concat "ON_" state))
(code-string (cdr (assoc event-property-name
(org-entry-properties))))
(code (car (read-from-string code-string))))
@ -20,5 +33,6 @@ The property should be named =ON_<STATE>= where =STATE= is a state defined in =o
* TODO Example task
:PROPERTIES:
:ON_PROGRESS: (message "Busy!")
:ON_DONE: (message "Done!")
:ON_DONE: (message "Done!")
:ON_CLEAR: (message "No state.")
:END: