I've been archiving tasks automatically using
`org-after-todo-state-change-hook' but I've been recently bit with a
note (entering a todo state configured with '@') being placed where the
task was before its archival.

This patch aims to offer a way to defer the archival after the
note is stored. Actually, I am using it like this:

#+begin_src elisp
(add-to-list 'org-after-todo-state-change-hook
       (lambda ()
         ;; States configured without mandatory note
         (when (member org-state '("DONE"))
         ;; States configured with mandatory note
         (when (member org-state '("CANCELLED" "READ"))
           (add-to-list 'org-after-note-stored-hook 

With `my/org-roam-archive-to-today' removing itself from

Hopefully I did not miss an existing way to do this.


>From 18de09a3aa08e3d06f180165530cbaeeccdf3ccf Mon Sep 17 00:00:00 2001
From: Joris Caravati <t...@sula.io>
Date: Tue, 2 Jan 2024 22:50:32 +0100
Subject: [PATCH] lisp/org.el: Add `org-after-note-stored-hook'

* lisp/org.el: Add `org-after-note-stored-hook' which is called at the
end of the `org-store-log-note' function.
* etc/ORG-NEWS: Document the new hook.

This change allows customization after a note is taken. One case where
it is useful is when one wants to move a task after a state change but
cannot do so in `org-after-todo-state-change' because the new state is
configured to take a note (with '@' in `org-todo-keywords').

Setting this hook in `org-after-todo-state-change' allows to defer the
move after the note is taken and prevents the note to be placed where
the task was before being moved.

 etc/ORG-NEWS | 4 ++++
 lisp/org.el  | 6 +++++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index c54473f55..911e8ffeb 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -742,6 +742,10 @@ Completion is enabled for links to man pages added using ~org-insert-link~:
 =C-c C-l man RET emacscl TAB= to get =emacsclient=.  Of course, the ~ol-man~
 library should be loaded first.
+*** New hook [[doc::org-after-note-stored-hook][org-after-note-stored-hook]]
+This new hook runs when a note has been stored.
 ** New functions and changes in function arguments
 *** ~org-fold-hide-drawer-all~ is now interactive
diff --git a/lisp/org.el b/lisp/org.el
index 6e6e075b4..fad21d8ba 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -1944,6 +1944,9 @@ Lisp variable `org-state'."
   :group 'org-todo
   :type 'hook)
+(defcustom org-after-note-stored-hook nil
+  "Hook which is run after a note was stored")
 (defvar org-blocker-hook nil
   "Hook for functions that are allowed to block a state change.
@@ -10729,7 +10732,8 @@ items are State notes."
   (with-current-buffer (marker-buffer org-log-note-return-to)
     (goto-char org-log-note-return-to))
   (move-marker org-log-note-return-to nil)
-  (when org-log-post-message (message "%s" org-log-post-message)))
+  (when org-log-post-message (message "%s" org-log-post-message))
+  (run-hooks 'org-after-note-stored-hook))
 (defun org-remove-empty-drawer-at (pos)
   "Remove an empty drawer at position POS.

Reply via email to