Bruno Cardoso <cardoso...@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.

>From 276f7755e0337631eb9a202a8275fcbe7f1cdfe8 Mon Sep 17 00:00:00 2001
Message-ID: <276f7755e0337631eb9a202a8275fcbe7f1cdfe8.1717586846.git.yanta...@posteo.net>
From: Ihor Radchenko <yanta...@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....@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

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

Reply via email to