emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Tim Landscheidt <tim@tim-landscheidt.de>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: emacs-orgmode@gnu.org
Subject: Re: How to execute Lisp code /after/ a capture template has been filled before it is finalized?
Date: Sun, 24 Dec 2023 13:16:48 +0000	[thread overview]
Message-ID: <87sf3rpwov.fsf@vagabond.tim-landscheidt.de> (raw)
In-Reply-To: <877cl4dc1j.fsf@localhost> (Ihor Radchenko's message of "Sat, 23 Dec 2023 18:11:20 +0000")

Ihor Radchenko <yantar92@posteo.net> wrote:

> […]

> %(...) expansion is executed _before_ capture template is fully
>  calculated. It is generally not designed to side-effect functions.

> You can instead use template-local hooks, introduced in Org 9.6. See
> https://git.savannah.gnu.org/cgit/emacs/org-mode.git/tree/etc/ORG-NEWS#n1270

I'm using Emacs 28.3, so that is something for the future.
But your pointer made me read the source and adjust my men-
tal picture: I had always thought that capturing opened a
window showing a narrowed section of the buffer, invoked
org-capture-mode-hook, then inserted the template and went
on from there.

Instead (CMIIW), the template is evaluated in a temporary
buffer, the result is pasted in the capture buffer, and then
org-capture-mode-hook is invoked.  That means inter alia
that org-capture-mode-hook can work on the "completed" tem-
plate.

For my problem, I thus first (partly) replicated Org 9.6's
behaviour by adding:

| (lambda nil
|        (if
|            (version< org-version "9.6")
|            (let
|                ((hook
|                  (org-capture-get :hook)))
|              (if
|                  (functionp hook)
|                  (funcall hook)
|                (mapc #'funcall hook)))))

to org-capture-mode-hook.

Then I thought about using org-table-map-tables to call
org-table-recalculate on all tables in the capture buffer to
avoid having to figure out where my table was.  But
org-table-map-tables unfortunately widens the buffer before
iterating, so this was no option.

Therefore, to keep it simple, I amended my template by add-
ing "%?" to the cell where the sum is put in and setting the
:hook property to org-table-recalculate.  (I also added
";%.2f" to the formula for formatting.)  In the end, this
works very nicely.

Thanks!
Tim


      reply	other threads:[~2023-12-24 13:17 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-23 17:38 How to execute Lisp code /after/ a capture template has been filled before it is finalized? Tim Landscheidt
2023-12-23 18:11 ` Ihor Radchenko
2023-12-24 13:16   ` Tim Landscheidt [this message]

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:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  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=87sf3rpwov.fsf@vagabond.tim-landscheidt.de \
    --to=tim@tim-landscheidt.de \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* 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

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

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