emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Joris Caravati via "General discussions about Org-mode." <emacs-orgmode@gnu.org>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: emacs-orgmode list <emacs-orgmode@gnu.org>
Subject: Re: [PATCH] Add org-after-note-stored-hook
Date: Sun, 12 May 2024 22:12:41 +0200	[thread overview]
Message-ID: <m25xviokli.fsf@kyon.home> (raw)
In-Reply-To: <8734qouw77.fsf@localhost> (Ihor Radchenko's message of "Sat, 11 May 2024 16:53:16 +0000")

[-- Attachment #1: Type: text/plain, Size: 1490 bytes --]

Ihor Radchenko <yantar92@posteo.net> writes:
> 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.

Thanks for your help! I switched to `org-trigger-hook' without issue.

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

I remember trying that before adding the hook (and I tried again today)
but the problem with adding the note directly is that I only seem to
manage storing the note where the task was before being archived,
eg. with this (or any variants I could think of while making sense of
what note functions do):

#+begin_src elisp
  (when (member (plist-get properties ':to) '("CANCELLED" "READ"))

which I find logical, since `org-store-log-note' is only called after
=C-c C-c= is pressed, whereas the archival function is called just after
the note buffer is created. So I still struggle to see how I could do
without the hook (maybe if the archival function would return the
position of the task after moving it, but that seems more complicated
than just using the hook).

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

A patch modified to match the suggested location for the `run-hooks' is

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-lisp-org.el-Add-org-after-note-stored-hook.patch --]
[-- Type: text/x-patch, Size: 2233 bytes --]

From 779d6b85acf9c30d7230bffccb2f98764372034a Mon Sep 17 00:00:00 2001
From: Joris Caravati <tyx@sula.io>
Date: Sun, 12 May 2024 21:29:52 +0200
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  | 4 ++++
 2 files changed, 8 insertions(+)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 87b72ad12..4b7636765 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -1569,6 +1569,10 @@ optional argument =NEW-HEADING-CONTAINER= specifies where in the
 buffer it will be added.  If not specified, new headings are created
 at level 1 at the end of the accessible part of the buffer, as before.
+*** New hook [[doc::org-after-note-stored-hook][org-after-note-stored-hook]]
+This new hook runs when a note has been stored.
 ** Miscellaneous
 *** =org-crypt.el= now applies initial visibility settings to decrypted entries
diff --git a/lisp/org.el b/lisp/org.el
index 598b4ca23..64f6d07ee 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -1990,6 +1990,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.
@@ -10845,6 +10848,7 @@ items are State notes."
                (unless (string-empty-p line)
 	         (indent-line-to ind)
 	         (insert-and-inherit line))))
+           (run-hooks 'org-after-note-stored-hook)
 	   (message "Note stored")
 	   (org-back-to-heading t))))))
   ;; Don't add undo information when called from `org-agenda-todo'.

  reply	other threads:[~2024-05-12 20:13 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-02 22:13 [PATCH] Add org-after-note-stored-hook Joris Caravati via General discussions about Org-mode.
2024-05-09  8:20 ` Joris Caravati via General discussions about Org-mode.
2024-05-09 11:38   ` Ihor Radchenko
2024-05-11 16:53 ` Ihor Radchenko
2024-05-12 20:12   ` Joris Caravati via General discussions about Org-mode. [this message]
2024-05-18 11:26     ` Ihor Radchenko
2024-05-20 19:46       ` Joris Caravati via General discussions about Org-mode.
2024-05-21 11:37         ` Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m25xviokli.fsf@kyon.home \
    --to=emacs-orgmode@gnu.org \
    --cc=tyx+org@sula.io \
    --cc=yantar92@posteo.net \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).