Joris Caravati via "General discussions about Org-mode."
<emacs-orgmode@gnu.org> writes:

> 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"))
>            (my/org-roam-archive-to-today))
>          ;; States configured with mandatory note
>          (when (member org-state '("CANCELLED" "READ"))
>            (add-to-list 'org-after-note-stored-hook 
> 'my/org-roam-archive-to-today))))
> #+end_src
>
> With `my/org-roam-archive-to-today' removing itself from
> `org-after-note-stored-hook'.
>
> Hopefully I did not miss an existing way to do this.

An easier way would be forcing note earlier by calling
`org-add-log-note' from your hook.

Although, removing heading from inside
`org-after-todo-state-change-hook' is a bad idea - Org mode does not
expect the heading to disappear from under the cursor when this hook is
executed. I recommend using `org-trigger-hook' instead.

So, your patch does not seem necessary to achieve what you want.

That said, it does make sense to add a hook after storing a log note,
but not at the very end, when point may not be at the note stored:

>    (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))

This will leave point in buffer where the note was invoked. For example,
in agenda buffer. I doubt that the users of this hook will expect such a
thing.

It will probably be better to run such new hooks right before (message "Note 
stored")
in `org-store-log-note'.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

Reply via email to