emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [BUG] folding error during capture
@ 2022-05-01 20:51 Skip Collins
  2022-05-02  0:17 ` [PATCH] " Ihor Radchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Skip Collins @ 2022-05-01 20:51 UTC (permalink / raw)
  To: emacs-org list

Recently, I get an error message when invoking custom capture
templates like this one:
("n" "Note" entry
      (file "~/Documents/org/beorg/capture.org")
      "* %^{note} :note: %(org-set-property \"Created\"
(org-time-stamp-inactive '(16)))" :immediate-finish t)

I invoke the template from the capture dispatcher:
C-c c n

The minibuffer prompts for the note text as expected:
0    note:

But the pre-populated *Capture* window contains an error message:
*  :note: %![Error: (error Calling ‘org-fold-core-region’ with missing SPEC)]
:PROPERTIES:...

In the recent past, I was able to invoke functions like
org-set-property in a capture template. Did the interface change?


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

* [PATCH] Re: [BUG] folding error during capture
  2022-05-01 20:51 [BUG] folding error during capture Skip Collins
@ 2022-05-02  0:17 ` Ihor Radchenko
  2022-05-02  1:36   ` Skip Collins
  2022-06-09  8:16   ` Ihor Radchenko
  0 siblings, 2 replies; 5+ messages in thread
From: Ihor Radchenko @ 2022-05-02  0:17 UTC (permalink / raw)
  To: Skip Collins; +Cc: emacs-org list

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

Skip Collins <skip.collins@gmail.com> writes:

> Recently, I get an error message when invoking custom capture
> templates like this one:
> ("n" "Note" entry
>       (file "~/Documents/org/beorg/capture.org")
>       "* %^{note} :note: %(org-set-property \"Created\"
> (org-time-stamp-inactive '(16)))" :immediate-finish t)
>
> ...
> But the pre-populated *Capture* window contains an error message:
> *  :note: %![Error: (error Calling ‘org-fold-core-region’ with missing SPEC)]
> :PROPERTIES:...

Thanks for the report!
This is quite a hacky capture template...
What you are seeing happens because template expansion happens in
fundamental-mode and org-set-property does not work anymore outside
org-mode.

I'd say that setting org-mode during template expansion is reasonable.

The fix is attached.

I do not push the fix just yet. If anyone think that enabling org-mode
during template expansion is undesired, please let me know.

Best,
Ihor


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-capture-Expand-sexps-in-template-with-Org-mode-b.patch --]
[-- Type: text/x-patch, Size: 2387 bytes --]

From 797dfd0c82c68696d8534a9b3103d0b3c622bee6 Mon Sep 17 00:00:00 2001
Message-Id: <797dfd0c82c68696d8534a9b3103d0b3c622bee6.1651450441.git.yantar92@gmail.com>
From: Ihor Radchenko <yantar92@gmail.com>
Date: Mon, 2 May 2022 08:11:28 +0800
Subject: [PATCH] org-capture: Expand sexps in template with Org mode being
 active

* lisp/org-capture.el (org-capture-fill-template): Enable `org-mode'
in the temporary template expansion buffer.  Update docstring
accordingly.
(org-capture-templates): Clarify that %(sexp) expansion happens in a
temporary Org mode buffer.

Fixes https://orgmode.org/list/CABUh-776V-_+_JAZwcKQm9ATcs0WUV9SmGwjooGFbt=CtwQj8g@mail.gmail.com
---
 lisp/org-capture.el | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index 068e3eda2..9b17fa978 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -309,6 +309,8 @@ (defcustom org-capture-templates nil
               introduced with %[pathname] are expanded this way.
               Since this happens after expanding non-interactive
               %-escapes, those can be used to fill the expression.
+              The evaluation happens with Org mode set as major mode
+              in a temporary buffer.
   %<...>      The result of `format-time-string' on the ... format
               specification.
   %t          Time stamp, date only.  The time stamp is the current
@@ -1572,7 +1574,9 @@ (defun org-capture-fill-template (&optional template initial annotation)
   "Fill a TEMPLATE and return the filled template as a string.
 The template may still contain \"%?\" for cursor positioning.
 INITIAL content and/or ANNOTATION may be specified, but will be overridden
-by their respective `org-store-link-plist' properties if present."
+by their respective `org-store-link-plist' properties if present.
+
+Expansion occurs in a temporary Org mode buffer."
   (let* ((template (or template (org-capture-get :template)))
 	 (buffer (org-capture-get :buffer))
 	 (file (buffer-file-name (or (buffer-base-buffer buffer) buffer)))
@@ -1645,6 +1649,7 @@ (defun org-capture-fill-template (&optional template initial annotation)
       (setq buffer-file-name nil)
       (setq mark-active nil)
       (insert template)
+      (org-mode)
       (goto-char (point-min))
       ;; %[] insert contents of a file.
       (save-excursion
-- 
2.35.1


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

* Re: [PATCH] Re: [BUG] folding error during capture
  2022-05-02  0:17 ` [PATCH] " Ihor Radchenko
@ 2022-05-02  1:36   ` Skip Collins
  2022-05-02  2:05     ` Ihor Radchenko
  2022-06-09  8:16   ` Ihor Radchenko
  1 sibling, 1 reply; 5+ messages in thread
From: Skip Collins @ 2022-05-02  1:36 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-org list

> > ("n" "Note" entry
> >       (file "~/Documents/org/beorg/capture.org")
> >       "* %^{note} :note: %(org-set-property \"Created\"
> > (org-time-stamp-inactive '(16)))" :immediate-finish t)
> ...
> This is quite a hacky capture template...

I'll take that as a compliment.

The general ability to set properties during capture seems very
reasonable. It might be best to have a specific interface as an
alternative to my hack. The use of %(EXP) in capture templates is
intended to insert a string returned by the elisp EXP. I bent the
rules slightly to set the Created property. There already exists a way
to set property values in capture templates via %^{PROP|default}p, but
that results in a user prompt. Perhaps a new template construct like
%^{PROP|value}v could be introduced in order to set a property value
without a prompt. Then I could use %^{Created|%u}v in the template.


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

* Re: [PATCH] Re: [BUG] folding error during capture
  2022-05-02  1:36   ` Skip Collins
@ 2022-05-02  2:05     ` Ihor Radchenko
  0 siblings, 0 replies; 5+ messages in thread
From: Ihor Radchenko @ 2022-05-02  2:05 UTC (permalink / raw)
  To: Skip Collins; +Cc: emacs-org list

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

Skip Collins <skip.collins@gmail.com> writes:

>> > ("n" "Note" entry
>> >       (file "~/Documents/org/beorg/capture.org")
>> >       "* %^{note} :note: %(org-set-property \"Created\"
>> > (org-time-stamp-inactive '(16)))" :immediate-finish t)
>> ...
>> This is quite a hacky capture template...
>
> I'll take that as a compliment.

It was not. Your template expansion only works by accident because of
internal details of implementation of org-capture-fill-template.

In particular, org-time-stamp-inactive not only returns a timestamp, but
also _inserts_ it at point (see the screenshot of interim buffer state
while expanding your template). The fact that this inserted timestamp
gets deleted is simply because (1) org-capture-fill template does not
expect the buffer to be changed by other functions; (2) org-capture-fill
internally marks %(sexp) with sticky text property and deletes
everything marked with that property at the end of expansion; (3)
org-time-stamp-inactive respects text property stickiness.


[-- Attachment #2: 2022-05-02_09-56.png --]
[-- Type: image/png, Size: 6165 bytes --]

[-- Attachment #3: Type: text/plain, Size: 807 bytes --]


> The general ability to set properties during capture seems very
> reasonable. It might be best to have a specific interface as an
> alternative to my hack. The use of %(EXP) in capture templates is
> intended to insert a string returned by the elisp EXP. I bent the
> rules slightly to set the Created property. There already exists a way
> to set property values in capture templates via %^{PROP|default}p, but
> that results in a user prompt. Perhaps a new template construct like
> %^{PROP|value}v could be introduced in order to set a property value
> without a prompt. Then I could use %^{Created|%u}v in the template.

What about the template below?

("n" "Note" entry
(file "~/Documents/org/beorg/capture.org")
"* %^{note} :note:
:PROPERTIES:
:CREATED: %u
:END:
"
:immediate-finish t)

Best,
Ihor

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

* Re: [PATCH] Re: [BUG] folding error during capture
  2022-05-02  0:17 ` [PATCH] " Ihor Radchenko
  2022-05-02  1:36   ` Skip Collins
@ 2022-06-09  8:16   ` Ihor Radchenko
  1 sibling, 0 replies; 5+ messages in thread
From: Ihor Radchenko @ 2022-06-09  8:16 UTC (permalink / raw)
  To: Skip Collins; +Cc: emacs-org list

Ihor Radchenko <yantar92@gmail.com> writes:

> The fix is attached.
>
> I do not push the fix just yet. If anyone think that enabling org-mode
> during template expansion is undesired, please let me know.

Applied onto main via 0e7033e0c.
Not on bugfix as it is not a critical issue.

Best,
Ihor


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

end of thread, other threads:[~2022-06-09  8:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-01 20:51 [BUG] folding error during capture Skip Collins
2022-05-02  0:17 ` [PATCH] " Ihor Radchenko
2022-05-02  1:36   ` Skip Collins
2022-05-02  2:05     ` Ihor Radchenko
2022-06-09  8:16   ` Ihor Radchenko

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