branch: externals/tempel
commit db24ec0c9e964c59075225f315ce0cfea5520df6
Author: Daniel Mendler <[email protected]>
Commit: Daniel Mendler <[email protected]>
Reevaluate forms when arbitrary fields are modified
See discussion in #2
---
tempel.el | 39 ++++++++++++++++++++++-----------------
1 file changed, 22 insertions(+), 17 deletions(-)
diff --git a/tempel.el b/tempel.el
index 1fe4de76cc..cc90affe52 100644
--- a/tempel.el
+++ b/tempel.el
@@ -113,11 +113,12 @@ WIDTH, SEP and ELLIPSIS configure the formatting."
(defun tempel--replace-field (ov str)
"Replace OV content with STR."
- (let ((beg (overlay-start ov)))
- (goto-char beg)
- (delete-char (- (overlay-end ov) beg))
- (insert str)
- (move-overlay ov beg (point))))
+ (save-excursion
+ (let ((beg (overlay-start ov)))
+ (goto-char beg)
+ (delete-char (- (overlay-end ov) beg))
+ (insert str)
+ (move-overlay ov beg (point)))))
(defun tempel--update-field (ov after beg end &optional _len)
"Update field overlay OV.
@@ -132,17 +133,21 @@ BEG and END are the boundaries of the modification."
;; with an invalid undo state after tempel-done.
(with-silent-modifications
(move-overlay ov (overlay-start ov) (max end (overlay-end ov)))
- (when-let (name (overlay-get ov 'tempel--name))
- (let ((state (overlay-get ov 'tempel--state))
- (str (buffer-substring-no-properties (overlay-start ov)
(overlay-end ov))))
- (setf (alist-get name (cddr state)) str)
- ;; Update other fields
- (dolist (other (alist-get name (car state)))
- (unless (eq other ov)
- (tempel--replace-field other str)))
- ;; Update evaluated forms
- (dolist (other (cadr state))
- (tempel--replace-field other (eval (overlay-get other
'tempel--form) (cddr state))))))))))
+ (let ((state (overlay-get ov 'tempel--state)))
+ ;; Update other named fields
+ (when-let (name (overlay-get ov 'tempel--name))
+ (let ((str (buffer-substring-no-properties (overlay-start ov)
(overlay-end ov))))
+ (setf (alist-get name (cddr state)) str)
+ (dolist (other (alist-get name (car state)))
+ (unless (eq other ov)
+ (tempel--replace-field other str)))))
+ ;; Update evaluated form fields
+ (dolist (other (cadr state))
+ (tempel--replace-field
+ other
+ (save-excursion
+ (goto-char (overlay-start other))
+ (eval (overlay-get other 'tempel--form) (cddr state))))))))))
(defun tempel--field (&optional face)
"Create template field with FACE."
@@ -152,6 +157,7 @@ BEG and END are the boundaries of the modification."
(overlay-put ov 'before-string (propertize " " 'face face 'display '(space
:width (1))))
(overlay-put ov 'modification-hooks (list #'tempel--update-field))
(overlay-put ov 'insert-behind-hooks (list #'tempel--update-field))
+ (overlay-put ov 'tempel--state tempel--state)
(push ov tempel--overlays)
ov))
@@ -159,7 +165,6 @@ BEG and END are the boundaries of the modification."
"Create template field named NAME."
(let ((ov (tempel--field)))
(overlay-put ov 'tempel--name name)
- (overlay-put ov 'tempel--state tempel--state)
(push ov (alist-get name (car tempel--state)))
(when-let (str (alist-get name (cddr tempel--state)))
(insert str)