gists/gists/evaluate-code-on-task-state...

1.5 KiB

The function below evaluates 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 raw mode to see the properties).

  (defun my/task-state-event-handler ()
    "Evaluate a Lisp form attached to the task whose state is being changed.

  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))))
      (org-eval code)))

  (add-hook 'org-after-todo-state-change-hook 'my/task-state-event-handler)

TODO Example task