emacs-elpa-diffs
[Top][All Lists]
Advanced

[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."



reply via email to

[Prev in Thread] Current Thread [Next in Thread]