[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/tempel 3659036edb 1/2: Optional named field argument fo
From: |
ELPA Syncer |
Subject: |
[elpa] externals/tempel 3659036edb 1/2: Optional named field argument for custom user element hooks |
Date: |
Fri, 15 Nov 2024 03:58:58 -0500 (EST) |
branch: externals/tempel
commit 3659036edbc332746dec556d0dec69ac4c52dcac
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>
Optional named field argument for custom user element hooks
Fix #153
Co-authored-by: Taro Sato <okomestudio@gmail.com>
---
README.org | 23 +++++++++++++++++++++++
tempel.el | 8 +++++++-
2 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/README.org b/README.org
index e02d1be100..690fcd79c8 100644
--- a/README.org
+++ b/README.org
@@ -345,6 +345,29 @@ The following example templates uses the newly defined
include element.
(package (i header) r n n (i provide))
#+end_src
+If a custom user element needs an access to named fields, the hook function
+should take the second argument ~fields~, which refers to an alist that maps
the
+field name to its value in the current template. For example here we define a
+custom element ~*~ to repeat a template a number of times:
+
+#+begin_src emacs-lisp
+(defun tempel-repeat (elt fields)
+ (pcase elt
+ (`(* ,count . ,rest)
+ (cons 'l (cl-loop for i below (eval count fields) append rest)))))
+(add-to-list 'tempel-user-elements #'tempel-repeat)
+#+end_src
+
+The ~*~ custom element can be used to expand dynamic tables:
+
+#+begin_src emacs-lisp
+(table (p (read-number "Rows: ") rows noinsert)
+ (p (read-number "Cols: ") cols noinsert)
+ "| " (p " ") (* (1- cols) " | " (p " ")) " |" n
+ "|" (* cols "----|") n
+ (* rows "| " (p " ") (* (1- cols) " | " (p " ")) " |" n))
+#+end_src
+
* Adding template sources
Tempel offers a flexible mechanism for providing the templates, which are
diff --git a/tempel.el b/tempel.el
index 70b34e8135..9804a52033 100644
--- a/tempel.el
+++ b/tempel.el
@@ -366,7 +366,13 @@ Return the added field."
(indent-region (car region) (cdr region) nil))))
;; TEMPEL EXTENSION: Quit template immediately
('q (overlay-put (tempel--field st) 'tempel--enter #'tempel--done))
- (_ (if-let ((ret (run-hook-with-args-until-success 'tempel-user-elements
elt)))
+ (_ (if-let ((ret (run-hook-wrapped 'tempel-user-elements
+ (lambda (hook elt fields)
+ (condition-case nil
+ (funcall hook elt)
+ (wrong-number-of-arguments
+ (funcall hook elt fields))))
+ elt (cdr st))))
(tempel--element st region ret)
;; TEMPEL EXTENSION: Evaluate forms
(tempel--form st elt)))))