branch: externals/tempel
commit d20eeed637b6cd461bb5b06f5f01f515275a8c69
Author: Daniel Mendler <[email protected]>
Commit: Daniel Mendler <[email protected]>
Add support for (P ...) tag which always reads from the minibuffer
---
README.org | 2 ++
tempel.el | 19 +++++++++++--------
2 files changed, 13 insertions(+), 8 deletions(-)
diff --git a/README.org b/README.org
index 4fa80e2636..7be6886b00 100644
--- a/README.org
+++ b/README.org
@@ -330,6 +330,8 @@ docstring of ~tempel--element~ in tempel.el and originally
in
value is bound to ~NAME~ and updated dynamically. If ~NOINSERT~ is non-nil,
no
field is inserted and the minibuffer is used for prompting. For clarity, the
symbol ~noinsert~ should be used as argument.
+- ~(P PROMPT <NAME> <NOINSERT>)~ Works just like ~(p ...)~, but always reads
from
+ the minibuffer.
- ~(r PROMPT <NAME> <NOINSERT>)~: Like ~(p ..)~, but if there is a current
region,
it is placed here.
- ~(r> PROMPT <NAME> <NOINSERT>)~ Like ~(r ..)~, but it also indents the
region.
diff --git a/tempel.el b/tempel.el
index a68eb3230c..f330aba8a4 100644
--- a/tempel.el
+++ b/tempel.el
@@ -386,6 +386,8 @@ A template can consist of elements of several types:
NOINSERT is non-nil, no field is inserted and the minibuffer is used
for prompting. For clarity, the symbol `noinsert' should be used as
argument.
+- (P PROMPT <NAME> <NOINSERT>): Works just like (p ...), but always
+ reads from the minibuffer.
- (r PROMPT <NAME> <NOINSERT>): Like (p ..), but if there is a current
region, it is placed here.
- (r> PROMPT <NAME> <NOINSERT>): Like (r ..), but is also indents the
@@ -427,10 +429,11 @@ Use caution with templates which execute arbitrary code!"
(open-line 1)))
(`(s ,name) (tempel--field name))
(`(l . ,lst) (dolist (e lst) (tempel--element region e)))
- ((or 'p `(,(or 'p 'P) . ,rest)) (apply #'tempel--placeholder rest))
+ ((or 'p `(,(and tag (or 'p 'P)) . ,rest))
+ (apply #'tempel--placeholder (eq tag 'P) rest))
((or 'r 'r> `(,(or 'r 'r>) . ,rest))
(if (not region)
- (when-let* ((ov (apply #'tempel--placeholder rest))
+ (when-let* ((ov (apply #'tempel--placeholder nil rest))
((not rest))
(tempel-done-on-region))
(overlay-put ov 'tempel--enter #'tempel--done))
@@ -460,20 +463,20 @@ Use caution with templates which execute arbitrary code!"
(funcall hook elt fields))))
elt (cdar tempel--active)))
-(defun tempel--placeholder (&optional prompt name noinsert)
+(defun tempel--placeholder (read &optional prompt name noinsert)
"Handle placeholder element and add field with NAME.
-If NOINSERT is non-nil do not insert a field, only bind the value to NAME.
-PROMPT is the optional prompt/default value.
-If a field was added, return it."
+If READ is non-nil read a string from the minibuffer. If NOINSERT is
+non-nil do not insert a field, only bind the value to NAME. PROMPT is
+the optional prompt/default value. If a field was added, return it."
(let ((init
(cond
- ((and (stringp prompt) noinsert) (read-string prompt))
+ ((and (stringp prompt) (or noinsert read)) (read-string prompt))
((stringp prompt) prompt)
;; TEMPEL EXTENSION: Evaluate prompt
(t (eval prompt (cdar tempel--active))))))
(if noinsert
(progn (setf (alist-get name (cdar tempel--active)) init) nil)
- (tempel--field name init (stringp prompt)))))
+ (tempel--field name init (and (not read) (stringp prompt))))))
(defun tempel--insert (template region)
"Insert TEMPLATE given the current REGION."