Add gist for an event handler for org mode tasks

This commit is contained in:
Bram Schoenmakers 2022-11-26 14:57:13 +01:00
parent 529c9e28fa
commit 22bdd3d76d
2 changed files with 55 additions and 0 deletions

View file

@ -124,6 +124,36 @@ My vertico-repeat setup.
(minibuffer-setup . vertico-repeat-save))
#+end_src
* 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 property should be named =ON_<STATE>= where =STATE= is a state defined in =org-todo-keywords=. See also the example task below.
,#+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."
(if-let* ((state org-state)
(event-property-name (concat "ON_" state))
(properties (org-entry-properties))
(code-string (cdr (assoc event-property-name properties)))
(code (car (read-from-string code-string))))
(org-eval code)))
(add-hook 'org-after-todo-state-change-hook 'my/task-state-event-handler)
,#+end_src
,* TODO Example task
:PROPERTIES:
:ON_PROGRESS: (message "Busy!")
:ON_DONE: (message "Done!")
:END:
#+end_src
* Meta
** License

View file

@ -0,0 +1,25 @@
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 property should be named =ON_<STATE>= where =STATE= is a state defined in =org-todo-keywords=. See also the example task below.
#+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."
(if-let* ((state org-state)
(event-property-name (concat "ON_" state))
(properties (org-entry-properties))
(code-string (cdr (assoc event-property-name properties)))
(code (car (read-from-string code-string))))
(org-eval code)))
(add-hook 'org-after-todo-state-change-hook 'my/task-state-event-handler)
#+end_src
* TODO Example task
:PROPERTIES:
:ON_PROGRESS: (message "Busy!")
:ON_DONE: (message "Done!")
:END: