[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/tempel 91f6978ddd 60/82: Use Tempo PROMPT as default va
From: |
ELPA Syncer |
Subject: |
[elpa] externals/tempel 91f6978ddd 60/82: Use Tempo PROMPT as default value |
Date: |
Sun, 9 Jan 2022 20:58:45 -0500 (EST) |
branch: externals/tempel
commit 91f6978ddd853b9c5ff181010c64a225f4fa84c7
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>
Use Tempo PROMPT as default value
---
README.org | 12 +++++++-----
tempel.el | 44 ++++++++++++++++++++------------------------
2 files changed, 27 insertions(+), 29 deletions(-)
diff --git a/README.org b/README.org
index 2e9b284ef8..d5823859ad 100644
--- a/README.org
+++ b/README.org
@@ -110,21 +110,23 @@ important ones here:
- "string" Inserts a string literal.
- ~p~ Inserts an unnamed placeholder field.
- - ~r~ Inserts the current region.
- - ~(s NAME)~ Inserts a named field.
- ~n~ Inserts a newline.
- ~>~ Indents with ~indent-according-to-mode~.
+ - ~r~ Inserts the current region.
- ~r>~ The region, but indented.
- ~n>~ Inserts a newline and indents.
- ~&~ Insert newline if there is only whitespace between line start and point.
- ~%~ Insert newline if there is only whitespace between point and line end.
- ~o~ Like ~%~ but leaves the point before newline.
+ - ~(s NAME)~ Inserts a named field.
+ - ~(p DEFAULT <NAME> <NONINS>)~ Insert a named field with a default value.
+ If ~NOINS~ is non-nil, do not insert and only bind the variable.
+ - ~(r DEFAULT <NAME> <NOINS>)~ Insert region or act like ~(p ...)~.
+ - ~(r> DEFAULT <NAME> <NOINS>)~ Act like ~(r ...)~, but indent region.
Furthermore Tempel supports syntax extensions:
- - ~(q PROMPT NAME)~ Query the user via ~read-string~ and store the result in
variable ~NAME~.
- - ~(q (FORM ...) NAME)~ Execute ~FORM~ and store the result in variable
~NAME~.
- - ~(p (FORM ...) <NAME>)~ Execute ~FORM~ and insert the result, optionally
bind to ~NAME~.
+ - ~(p FORM <NAME> <NONINS>)~ Like ~p~ described above, but ~FORM~ is
evaluated.
- ~(FORM ...)~ Other Lisp forms are evaluated. Named fields are lexically
bound.
Use caution with templates which execute arbitrary code!
diff --git a/tempel.el b/tempel.el
index f7f34f1a95..ff3785565d 100644
--- a/tempel.el
+++ b/tempel.el
@@ -117,8 +117,8 @@ may be named with `tempel--name' or carry an evaluatable
Lisp expression
(defun tempel--print-element (elt)
"Return string representation of template ELT."
(pcase elt
+ ('nil nil)
((pred stringp) elt)
- ((or 'nil `(q . ,_)) nil)
(`(s ,name) (symbol-name name))
(`(,(or 'p 'P) ,_ ,name . ,noinsert)
(and (not (car noinsert)) (symbol-name name)))
@@ -228,12 +228,6 @@ INIT is the optional initial input."
(overlay-put ov 'tempel--form form)
(push ov (car st)))))
-(defun tempel--query (st prompt name)
- "Read input with PROMPT and assign to binding NAME in ST."
- (setf (alist-get name (cdr st)) (if (stringp prompt)
- (read-string prompt)
- (eval prompt 'lexical-binding))))
-
(defun tempel--element (st region elt)
"Add template ELT to ST given the REGION."
(pcase elt
@@ -248,24 +242,26 @@ INIT is the optional initial input."
(insert "\n")))
('o (unless (or (eolp) (save-excursion (re-search-forward "\\=\\s-*$" nil
t)))
(open-line 1)))
- ('p (tempel--field st))
(`(s ,name) (tempel--field st name))
- ;; LEGACY: (r ...) and (r> ...) is legacy syntax from Tempo, use r instead.
- ((or 'r `(r . ,_)) (if region (goto-char (cdr region)) (tempel--field st)))
- ((or 'r> `(r> . ,_)) (if (not region) (tempel--field st)
- (goto-char (cdr region))
- (indent-region (car region) (cdr region) nil)))
- ;; LEGACY: (p ...) and (P ...) is legacy syntax from Tempo, use q, s, or p
instead.
- ;; TEMPEL EXTENSION: (p (FORM...) <NAME>)
- (`(,(or 'p 'P) ,prompt . ,rest)
- (if (cadr rest)
- (tempel--query st prompt (car rest))
- (tempel--field st (car rest) (and (consp prompt)
- (eval prompt 'lexical)))))
- ;; TEMPEL EXTENSION: Query from minibuffer, (q "Prompt: " name), (q
(FORM...) name)
- (`(q ,prompt ,name) (tempel--query st prompt name))
- ;; TEMPEL EXTENSION: Evaluate forms
- (_ (tempel--form st elt))))
+ ((or 'p `(,(or 'p 'P) . ,rest)) (apply #'tempel--placeholder st rest))
+ ((or 'r 'r> `(,(or 'r 'r>) . ,rest))
+ (if (not region) (apply #'tempel--placeholder st rest)
+ (goto-char (cdr region))
+ (when (eq (or (car-safe elt) elt) 'r>)
+ (indent-region (car region) (cdr region) nil))))
+ (_ (tempel--form st elt)))) ;; TEMPEL EXTENSION: Evaluate forms
+
+(defun tempel--placeholder (st &optional prompt name noinsert)
+ "Handle placeholder element and add field with NAME to ST.
+If NOINSERT is non-nil do not insert a field, only bind the value to NAME.
+PROMPT is the optional prompt/default value."
+ (setq prompt
+ (if (and (stringp prompt) noinsert) (read-string prompt)
+ ;; TEMPEL EXTENSION: Evaluate prompt
+ (eval prompt 'lexical)))
+ (if noinsert
+ (setf (alist-get name (cdr st)) prompt)
+ (tempel--field st name prompt)))
(defun tempel--insert (template region)
"Insert TEMPLATE given the current REGION."
- [elpa] externals/tempel 57bbc5e681 78/82: README update, (continued)
- [elpa] externals/tempel 57bbc5e681 78/82: README update, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel ac3df540a4 69/82: Remove underlines from faces, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 7cf25bd795 71/82: Simplify templates data structure at load time, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 69f7cb3212 06/82: Reimplement without Tempo for more flexibility, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel fbef94d77d 48/82: Minor cleanup, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 01e7430343 27/82: Add docstrings, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel a03c36c8dd 02/82: Improve behavior when templates are nested, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel db6847a7f9 56/82: Make temple-key a macro, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 0e618cd169 23/82: Do not sort overlays, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 04d4f6c055 55/82: Use named key function, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 91f6978ddd 60/82: Use Tempo PROMPT as default value,
ELPA Syncer <=
- [elpa] externals/tempel e20f982dc5 25/82: Remove global variable tempel--state, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel fa99fb7f96 34/82: README: Update keybindings, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel c4cd6e0f69 73/82: Rename tempel-expand -> tempel-complete, add tempel-expand (Fix #13), ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 8f8588985a 04/82: Simplify template markers, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel d9efb707b3 20/82: Improve undo behavior, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 4bc75c1aba 52/82: Use the more subtle face colors from Modus, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 52d8556c05 57/82: Improve annotation function, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 675e759bde 64/82: Add special prompt treatment, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel e6aa1892af 14/82: README: Simplify template, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 2ba940809a 42/82: Add temple-abort (Fix #2), ELPA Syncer, 2022/01/09