[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/tempel ad71c25632 32/82: Add syntax extension (p (FORM.
From: |
ELPA Syncer |
Subject: |
[elpa] externals/tempel ad71c25632 32/82: Add syntax extension (p (FORM...) <NAME>) |
Date: |
Sun, 9 Jan 2022 20:58:43 -0500 (EST) |
branch: externals/tempel
commit ad71c256327c872a8ec8fe5d15dfc41c24a42907
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>
Add syntax extension (p (FORM...) <NAME>)
---
README.org | 11 ++++++-----
tempel.el | 35 ++++++++++++++++++-----------------
2 files changed, 24 insertions(+), 22 deletions(-)
diff --git a/README.org b/README.org
index 7b79213349..01d92e2e1b 100644
--- a/README.org
+++ b/README.org
@@ -113,10 +113,11 @@ important ones here:
- ~%~ Insert newline if there is only whitespace between point and line end.
- ~o~ Like ~%~ but leaves the point before newline.
-Furthermore Tempel supports two syntax extensions:
+Furthermore Tempel supports syntax extensions:
- ~(q PROMPT NAME)~ Query the user via ~read-string~ and store the result in
variable ~NAME~.
- - ~(q (form ...) NAME)~ Query the user using ~FORM~ and store the result in
variable ~NAME~.
- Use caution with templates which execute code!
- - ~(form ...)~ Other Lisp forms are evaluated. Named fields are lexically
bound.
- Use caution with templates which execute code!
+ - ~(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~.
+ - ~(FORM ...)~ Other Lisp forms are evaluated. Named fields are lexically
bound.
+
+Use caution with templates which execute code!
diff --git a/tempel.el b/tempel.el
index 1a7e69ddc5..a50645c986 100644
--- a/tempel.el
+++ b/tempel.el
@@ -153,22 +153,22 @@ BEG and END are the boundaries of the modification."
(insert str)
(move-overlay other (overlay-start other) (point))))))))))
-(defun tempel--field (st)
- "Add template field to ST."
+(defun tempel--field (st &optional name init)
+ "Add template field to ST.
+NAME is the optional field name.
+INIT is the optional initial input."
(let ((ov (make-overlay (point) (point))))
+ (push ov (car st))
(overlay-put ov 'face 'tempel-field)
(overlay-put ov 'before-string #(" " 0 1 (display (space :width (1)) face
tempel-field)))
(overlay-put ov 'modification-hooks (list #'tempel--update-field))
(overlay-put ov 'insert-behind-hooks (list #'tempel--update-field))
(overlay-put ov 'tempel--state st)
- (push ov (car st))
- ov))
-
-(defun tempel--named (st name)
- "Add new template field NAME to ST."
- (let ((ov (tempel--field st)))
- (overlay-put ov 'tempel--name name)
- (when-let (str (alist-get name (cdr st)))
+ (when name
+ (overlay-put ov 'tempel--name name)
+ (when init
+ (setf (alist-get name (cdr st)) init)))
+ (when-let (str (or init (and name (alist-get name (cdr st)))))
(insert str)
(move-overlay ov (overlay-start ov) (point)))))
@@ -207,7 +207,7 @@ BEG and END are the boundaries of the modification."
(save-excursion (re-search-forward "\\=\\s-*$" nil t)))
(open-line 1)))
('p (tempel--field st))
- (`(s ,name) (tempel--named st name))
+ (`(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> . ,_))
@@ -215,14 +215,15 @@ BEG and END are the boundaries of the modification."
(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.
+ ;; EXTENSION: (p (FORM...) <NAME>)
(`(,(or 'p 'P) ,prompt . ,rest)
- (cond
- ((cadr rest) (tempel--query st prompt (car rest)))
- ((car rest) (tempel--named st (car rest)))
- (t (tempel--field st))))
- ;; EXTENSION: Query from minibuffer, Tempel extension!
+ (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))
- ;; EXTENSION: Evaluate forms, Tempel extension!
+ ;; TEMPEL EXTENSION: Evaluate forms
(_ (tempel--form st element))))
(defun tempel--insert (templates name region)
- [elpa] externals/tempel 460f8fe806 81/82: Fix the MELPA issues, (continued)
- [elpa] externals/tempel 460f8fe806 81/82: Fix the MELPA issues, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel aec4f10253 82/82: Addition to GNU ELPA, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel d4bd649645 59/82: Minor renaming, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 2cff879181 30/82: Do not call modification hooks from forms, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 1222c59b30 39/82: Update README, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 07d16fded7 67/82: Update fields when new field is inserted, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel f0df571f66 17/82: Respect derived modes, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel bf95b3f1c2 31/82: Add support for (q (FORM ...) var), ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 64f896d782 44/82: Fix jump to first field, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 9948c6889d 43/82: Add tempel-form-prefix and tempel-field-prefix, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel ad71c25632 32/82: Add syntax extension (p (FORM...) <NAME>),
ELPA Syncer <=
- [elpa] externals/tempel 5dbdc76eb2 74/82: Specify the completion category tempel, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 65a3eb0458 76/82: Improve abbreviation expansion, ELPA Syncer, 2022/01/09
- [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