emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [BUG] org-capture: %\N template expansion
@ 2024-05-23  1:06 Bruno Cardoso
  2024-05-23 11:13 ` Ihor Radchenko
  0 siblings, 1 reply; 6+ messages in thread
From: Bruno Cardoso @ 2024-05-23  1:06 UTC (permalink / raw)
  To: emacs-orgmode


Hi all,

I'm having trouble expanding the '%\N' template in org-capture.

For example, with the following template:

(add-to-list 'org-capture-templates
             '("t" "TEST" entry
               (file+headline "~/org/test.org" "TEST")
               "** TODO %?
:PROPERTIES:
:TITLE:    %^{TITLE}
:AUTHOR:   %^{AUTHOR}p
:END:
%\1 %\2"))

The properties are read correctly, but "%\1 %\2" expands to "%^A %^B".

Either escaping the backslashes ("%\\1 %\\2") or reading the template above from a file throws an error:

: org-capture: Capture abort: Wrong type argument: stringp, nil


Org mode version 9.7-pre (release_9.6.29-1474-g1c5492)


Best,

Bruno.


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

* Re: [BUG] org-capture: %\N template expansion
  2024-05-23  1:06 [BUG] org-capture: %\N template expansion Bruno Cardoso
@ 2024-05-23 11:13 ` Ihor Radchenko
  2024-05-23 12:37   ` Bruno Cardoso
  0 siblings, 1 reply; 6+ messages in thread
From: Ihor Radchenko @ 2024-05-23 11:13 UTC (permalink / raw)
  To: Bruno Cardoso; +Cc: emacs-orgmode

Bruno Cardoso <cardoso.bc@gmail.com> writes:

> I'm having trouble expanding the '%\N' template in org-capture.
>
> For example, with the following template:
>
> (add-to-list 'org-capture-templates
>              '("t" "TEST" entry
>                (file+headline "~/org/test.org" "TEST")
>                "** TODO %?
> :PROPERTIES:
> :TITLE:    %^{TITLE}
> :AUTHOR:   %^{AUTHOR}p
> :END:
> %\1 %\2"))
>
> The properties are read correctly, but "%\1 %\2" expands to "%^A %^B".

That's expected - you do need to escape \ in Elisp strings. \1 in string
is ^A (Ctrl-A) and \2 is ^B.

> Either escaping the backslashes ("%\\1 %\\2") or reading the template above from a file throws an error:
>
> : org-capture: Capture abort: Wrong type argument: stringp, nil

That's because you can only use %\N placeholder for %^{prompt} but not
for %^{prompt}X.

Try

(add-to-list 'org-capture-templates
             '("t" "TEST" entry
               (file+headline "~/org/test.org" "TEST")
               "** TODO %?
:PROPERTIES:
:TITLE:    %^{TITLE}
:AUTHOR:   %^{AUTHOR}
:END:
%\\1 %\\2"))

We can probably improve the error and the docstring to clarify this.

We may also make %^{prompt}X expansions to work with %\N, but it can
break the existing templates that are tuned to the current behaviour.

-- 
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] 6+ messages in thread

* Re: [BUG] org-capture: %\N template expansion
  2024-05-23 11:13 ` Ihor Radchenko
@ 2024-05-23 12:37   ` Bruno Cardoso
  2024-06-05 11:29     ` Ihor Radchenko
  0 siblings, 1 reply; 6+ messages in thread
From: Bruno Cardoso @ 2024-05-23 12:37 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode


On 2024-05-23, 11:13 +0000, Ihor Radchenko <yantar92@posteo.net> wrote:

> That's because you can only use %\N placeholder for %^{prompt} but not
> for %^{prompt}X.
> ...
> We can probably improve the error and the docstring to clarify this.

Thanks for the explanation, Ihor. I expected it to work in both cases, as the property prompt with value completion is very useful. I feel the error and the docstring can be confusing in this regard.

> We may also make %^{prompt}X expansions to work with %\N, but it can
> break the existing templates that are tuned to the current behaviour.

I see. Would it make sense to have another placeholder for '%^{prompt}X'?




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

* Re: [BUG] org-capture: %\N template expansion
  2024-05-23 12:37   ` Bruno Cardoso
@ 2024-06-05 11:29     ` Ihor Radchenko
  2024-06-05 22:58       ` Bruno Cardoso
  0 siblings, 1 reply; 6+ messages in thread
From: Ihor Radchenko @ 2024-06-05 11:29 UTC (permalink / raw)
  To: Bruno Cardoso; +Cc: emacs-orgmode

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

Bruno Cardoso <cardoso.bc@gmail.com> writes:

>> We may also make %^{prompt}X expansions to work with %\N, but it can
>> break the existing templates that are tuned to the current behaviour.
>
> I see. Would it make sense to have another placeholder for '%^{prompt}X'?

See the attached tentative patch.

I introduced a new placeholder - %\*N. It refers to _all_ the prompts,
unlike %\N. If you have better ideas about syntax, please share.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-capture-templates-New-placeholder-to-refer-to-pr.patch --]
[-- Type: text/x-patch, Size: 7069 bytes --]

From 276f7755e0337631eb9a202a8275fcbe7f1cdfe8 Mon Sep 17 00:00:00 2001
Message-ID: <276f7755e0337631eb9a202a8275fcbe7f1cdfe8.1717586846.git.yantar92@posteo.net>
From: Ihor Radchenko <yantar92@posteo.net>
Date: Wed, 5 Jun 2024 13:24:43 +0200
Subject: [PATCH] org-capture-templates: New placeholder to refer to
 %^{prompt}X answers

* lisp/org-capture.el (org-capture-fill-template): Support new
placeholder %\*N to insert answers to all prompts - %^{prompt} and
%^{prompt}X.
(org-capture-templates):
* etc/ORG-NEWS (New =%\*N= placeholder in ~org-capture-templates~):
* doc/org-manual.org (Template expansion): Document the new
placeholder.

Link: https://orgmode.org/list/87le40puvb.fsf@gmail.com
---
 doc/org-manual.org  |  8 ++++++--
 etc/ORG-NEWS        |  5 +++++
 lisp/org-capture.el | 50 ++++++++++++++++++++++++++++++++-------------
 3 files changed, 47 insertions(+), 16 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index 170eea506..fd842acca 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -8338,8 +8338,12 @@ **** Template expansion
 
 - =%\N= ::
 
-  Insert the text entered at the {{{var(N)}}}th =%^{PROMPT}=, where
-  {{{var(N)}}} is a number, starting from 1.
+  Insert the text entered at the {{{var(N)}}}th =%^{PROMPT}= (but not
+  =%^{PROMPT}X=), where {{{var(N)}}} is a number, starting from 1.
+
+- =%\*N= ::
+
+  Same as =%\N=, but include all the prompts.
 
 - =%?= ::
 
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 4b0b77ca8..52c6cb7b2 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -29,6 +29,11 @@ Please send Org bug reports to mailto:emacs-orgmode@gnu.org.
 # adding new customizations, or changing the interpretation of the
 # existing customizations.
 
+*** New =%\*N= placeholder in ~org-capture-templates~
+
+The new placeholder is like =%\N=, gives access not only to the
+=%^{prompt}= values, but also to =%^{prompt}X= values.
+
 *** The default value of ~org-babel-latex-process-alist~ is no longer taken from ~org-preview-latex-process-alist~
 
 The default value used to be pulled from =dvipng= process type from
diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index 34f9363e2..1dbe422d1 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -376,8 +376,10 @@ (defcustom org-capture-templates nil
               prompt/completions.  Default value and completions as in
               %^{prompt|default|...}X are allowed.
   %?          After completing the template, position cursor here.
-  %\\1 ... %\\N Insert the text entered at the nth %^{prompt}, where N
-              is a number, starting from 1.
+  %\\1 ... %\\N Insert the text entered at the nth %^{prompt} (but not
+              %^{prompt}X), where N is a number, starting from 1.
+  %\\*1...%\\*N Same as \\N, but for all the prompts, including
+              %^{prompt} and %^{prompt}X.
 
 Apart from these general escapes, you can access information specific to
 the link type that is created.  For example, calling `org-capture' in emails
@@ -1783,7 +1785,9 @@ (defun org-capture-fill-template (&optional template initial annotation)
       ;; completion in interactive prompts.
       (let ((org-inhibit-startup t)) (org-mode))
       (org-clone-local-variables buffer "\\`org-")
-      (let (strings)			; Stores interactive answers.
+      (let (strings			; Stores interactive answers.
+            strings-all                 ; ... include %^{prompt}X answers
+            )
 	(save-excursion
 	  (let ((regexp "%\\^\\(?:{\\([^}]*\\)}\\)?\\([CgGLptTuU]\\)?"))
 	    (while (re-search-forward regexp nil t)
@@ -1818,6 +1822,7 @@ (defun org-capture-fill-template (&optional template initial annotation)
 				     'org-tags-history))
 				  ":")))
 		       (when (org-string-nw-p ins)
+                         (push (concat ":" ins ":") strings-all)
 			 (unless (eq (char-before) ?:) (insert ":"))
 			 (insert ins)
 			 (unless (eq (char-after) ?:) (insert ":"))
@@ -1827,13 +1832,18 @@ (defun org-capture-fill-template (&optional template initial annotation)
 					 (lambda (s) (org-insert-link 0 s)))))
 		       (pcase org-capture--clipboards
 			 (`nil nil)
-			 (`(,value) (funcall insert-fun value))
+			 (`(,value)
+                          (funcall insert-fun value)
+                          (push value strings-all))
 			 (`(,first-value . ,_)
 			  (funcall insert-fun
-				   (read-string "Clipboard/kill value: "
-						first-value
-						'org-capture--clipboards
-						first-value)))
+                                   (let ((val
+				          (read-string "Clipboard/kill value: "
+						       first-value
+						       'org-capture--clipboards
+						       first-value)))
+                                     (push val strings-all)
+                                     val)))
 			 (_ (error "Invalid `org-capture--clipboards' value: %S"
 				   org-capture--clipboards)))))
 		    ("p"
@@ -1865,17 +1875,20 @@ (defun org-capture-fill-template (&optional template initial annotation)
 					 (if l (point-marker)
 					   (point-min-marker)))))))
 			    (value
-			     (org-read-property-value prompt pom default)))
-		       (org-set-property prompt value)))
+                             (org-read-property-value prompt pom default)))
+		       (org-set-property prompt value)
+                       (push value strings-all)))
 		    ((or "t" "T" "u" "U")
 		     ;; These are the date/time related ones.
 		     (let* ((upcase? (equal (upcase key) key))
 			    (org-end-time-was-given nil)
 			    (time (org-read-date upcase? t nil prompt)))
-		       (org-insert-timestamp
-			time (or org-time-was-given upcase?)
-			(member key '("u" "U"))
-			nil nil (list org-end-time-was-given))))
+                       (push
+			(org-insert-timestamp
+			 time (or org-time-was-given upcase?)
+			 (member key '("u" "U"))
+			 nil nil (list org-end-time-was-given))
+			strings-all)))
 		    (`nil
 		     ;; Load history list for current prompt.
 		     (setq org-capture--prompt-history
@@ -1885,6 +1898,7 @@ (defun org-capture-fill-template (&optional template initial annotation)
 			    completions
 			    nil nil nil 'org-capture--prompt-history default)
 			   strings)
+                     (push (car strings) strings-all)
 		     (insert (car strings))
 		     ;; Save updated history list for current prompt.
 		     (puthash prompt org-capture--prompt-history
@@ -1899,6 +1913,14 @@ (defun org-capture-fill-template (&optional template initial annotation)
 	    (unless (org-capture-escaped-%)
 	      (replace-match
 	       (nth (1- (string-to-number (match-string 1))) strings)
+	       nil t))))
+	;; Replace %*n escapes with nth %^{...} string.
+	(setq strings-all (nreverse strings-all))
+	(save-excursion
+	  (while (re-search-forward "%\\\\\\(\\*\\([1-9][0-9]*\\)\\)" nil t)
+	    (unless (org-capture-escaped-%)
+	      (replace-match
+	       (nth (1- (string-to-number (match-string 2))) strings-all)
 	       nil t)))))
       ;; Make sure there are no empty lines before the text, and that
       ;; it ends with a newline character or it is empty.
-- 
2.45.1


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


-- 
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 related	[flat|nested] 6+ messages in thread

* Re: [BUG] org-capture: %\N template expansion
  2024-06-05 11:29     ` Ihor Radchenko
@ 2024-06-05 22:58       ` Bruno Cardoso
  2024-06-06 15:24         ` Ihor Radchenko
  0 siblings, 1 reply; 6+ messages in thread
From: Bruno Cardoso @ 2024-06-05 22:58 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode


On 2024-06-05, 11:29 +0000, Ihor Radchenko <yantar92@posteo.net> wrote:

> See the attached tentative patch.
>
> I introduced a new placeholder - %\*N. It refers to _all_ the prompts,
> unlike %\N. If you have better ideas about syntax, please share.
>

Thank you, Ihor! I tested it both from `org-capture-templates' and from an Org template file and the patch worked as expected. I'm fine with the proposed syntax.

Best,

Bruno.


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

* Re: [BUG] org-capture: %\N template expansion
  2024-06-05 22:58       ` Bruno Cardoso
@ 2024-06-06 15:24         ` Ihor Radchenko
  0 siblings, 0 replies; 6+ messages in thread
From: Ihor Radchenko @ 2024-06-06 15:24 UTC (permalink / raw)
  To: Bruno Cardoso; +Cc: emacs-orgmode

Bruno Cardoso <cardoso.bc@gmail.com> writes:

> On 2024-06-05, 11:29 +0000, Ihor Radchenko <yantar92@posteo.net> wrote:
>
>> See the attached tentative patch.
>>
>> I introduced a new placeholder - %\*N. It refers to _all_ the prompts,
>> unlike %\N. If you have better ideas about syntax, please share.
>>
>
> Thank you, Ihor! I tested it both from `org-capture-templates' and from an Org template file and the patch worked as expected. I'm fine with the proposed syntax.

Applied, onto main.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=266bd1c4a

Fixed.

-- 
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] 6+ messages in thread

end of thread, other threads:[~2024-06-06 15:23 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-23  1:06 [BUG] org-capture: %\N template expansion Bruno Cardoso
2024-05-23 11:13 ` Ihor Radchenko
2024-05-23 12:37   ` Bruno Cardoso
2024-06-05 11:29     ` Ihor Radchenko
2024-06-05 22:58       ` Bruno Cardoso
2024-06-06 15:24         ` 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).