emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* How to execute Lisp code /after/ a capture template has been filled before it is finalized?
@ 2023-12-23 17:38 Tim Landscheidt
  2023-12-23 18:11 ` Ihor Radchenko
  0 siblings, 1 reply; 3+ messages in thread
From: Tim Landscheidt @ 2023-12-23 17:38 UTC (permalink / raw)
  To: emacs-orgmode

Hi,

I (want to) have a capture template that fills a table and
then (re-)calculates a cell based on a formula (and aligns
the table):

| - Note taken on %U \\\\
|   | Coin | Count
|   |-------+--------|
|   | 0.01 | %^{Number of € 0.01 coins|0} |
|   | 0.02 | %^{Number of € 0.02 coins|0} |
|   | 0.05 | %^{Number of € 0.05 coins|0} |
|   | 0.10 | %^{Number of € 0.10 coins|0} |
|   | 0.20 | %^{Number of € 0.20 coins|0} |
|   | 0.50 | %^{Number of € 0.50 coins|0} |
|   | 1.00 | %^{Number of € 1.00 coins|0} |
|   | 2.00 | %^{Number of € 2.00 coins|0} |
|   |-------+--------|
|   |      | |
|   #+TBLFM: @>$>=(@I$1..@II$1)*(@I$2..@II$2)

If I naively append "%(org-table-recalculate t)" to that
template, the expression gets evaluated immediately when I
start the capture and the text:

| %![Error: (user-error Not at a table)]

gets appended to the capture buffer /before/ I get prompted
for the first field.

Looking at the hooks for org-capture-mode, I also thought
about recalculating the table at finalizing the capture (*1)
and tried:

| %(progn (message "org-capture-prepare-finalize-hook = %S" org-capture-prepare-finalize-hook) (add-hook 'org-capture-prepare-finalize-hook (lambda nil (message "Hook called.")) 0 t) (message "org-capture-prepare-finalize-hook = %S" org-capture-prepare-finalize-hook) "")

but to my surprise, org-capture-prepare-finalize-hook is nil
prior to the add-hook call, has stored the function after
the add-hook call, but in the interactive capture buffer is
nil again (and no message is printed in *Messages*).

How can I evaluate Lisp code after all prompts have been
answered?

TIA,
Tim

(*1)   I would prefer not doing this, as in this case I
       could not see the result prior to finalizing.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How to execute Lisp code /after/ a capture template has been filled before it is finalized?
  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
  0 siblings, 1 reply; 3+ messages in thread
From: Ihor Radchenko @ 2023-12-23 18:11 UTC (permalink / raw)
  To: Tim Landscheidt; +Cc: emacs-orgmode

Tim Landscheidt <tim@tim-landscheidt.de> writes:

> I (want to) have a capture template that fills a table and
> then (re-)calculates a cell based on a formula (and aligns
> the table):
> ...
> If I naively append "%(org-table-recalculate t)" to that
> template, the expression gets evaluated immediately when I
> start the capture and the text:
>
> | %![Error: (user-error Not at a table)]
>
> gets appended to the capture buffer /before/ I get prompted
> for the first field.
>
> Looking at the hooks for org-capture-mode, I also thought
> about recalculating the table at finalizing the capture (*1)
> and tried:

> | %(progn (message "org-capture-prepare-finalize-hook = %S" org-capture-prepare-finalize-hook) (add-hook 'org-capture-prepare-finalize-hook (lambda nil (message "Hook called.")) 0 t) (message "org-capture-prepare-finalize-hook = %S" org-capture-prepare-finalize-hook) "")

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

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How to execute Lisp code /after/ a capture template has been filled before it is finalized?
  2023-12-23 18:11 ` Ihor Radchenko
@ 2023-12-24 13:16   ` Tim Landscheidt
  0 siblings, 0 replies; 3+ messages in thread
From: Tim Landscheidt @ 2023-12-24 13:16 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-12-24 13:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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

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