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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/tempel 7786837248 1/4: Add tempel-template-sources


From: ELPA Syncer
Subject: [elpa] externals/tempel 7786837248 1/4: Add tempel-template-sources
Date: Mon, 10 Jan 2022 12:57:45 -0500 (EST)

branch: externals/tempel
commit 77868372482a3766bf41b902027ff6aa85739bf0
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>

    Add tempel-template-sources
---
 tempel.el | 75 +++++++++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 49 insertions(+), 26 deletions(-)

diff --git a/tempel.el b/tempel.el
index 19ef7d513f..b4228e34b0 100644
--- a/tempel.el
+++ b/tempel.el
@@ -66,11 +66,18 @@
   :type '(choice (const nil integer)))
 
 (defcustom tempel-user-elements nil
-  "List of user element functions.
-The function take a template element as argument and must return either
+  "List of user element handler functions.
+The functions take a template element as argument and must return either
 nil or a new template element, which is subsequently evaluated."
   :type 'hook)
 
+(defcustom tempel-template-sources
+  (list 'tempel-local-templates #'tempel--file-templates)
+  "List of template sources.
+A source can either be a function or a variable symbol. The functions
+must return a list of templates which apply to the buffer or context."
+  :type 'hook)
+
 (defface tempel-field
   '((((class color) (min-colors 88) (background light))
      :background "#fdf0ff" :foreground "#541f4f")
@@ -95,10 +102,13 @@ nil or a new template element, which is subsequently 
evaluated."
     (t :inherit highlight :slant italic))
   "Face used for default values.")
 
-(defvar tempel--templates nil
+(defvar-local tempel-local-templates nil
+  "List of templates which apply to the current buffer.")
+
+(defvar tempel--file-templates nil
   "Templates loaded from the `tempel-file'.")
 
-(defvar tempel--modified nil
+(defvar tempel--file-modified nil
   "Modification time of `tempel-file' at the last load.")
 
 (defvar tempel--history nil
@@ -121,22 +131,6 @@ may be named with `tempel--name' or carry an evaluatable 
Lisp expression
     map)
   "Keymap to navigate across template markers.")
 
-(defun tempel--load (file)
-  "Load templates from FILE."
-  (with-temp-buffer
-    (insert "(\n")
-    (insert-file-contents file)
-    (goto-char (point-max))
-    (insert "\n)")
-    (goto-char (point-min))
-    (let ((templates (read (current-buffer))) result)
-      (while (and templates (symbolp (car templates)))
-        (let ((mode (pop templates)) list)
-          (while (and templates (consp (car templates)))
-            (push (pop templates) list))
-          (push (cons mode (nreverse list)) result)))
-      result)))
-
 (defun tempel--print-element (elt)
   "Return string representation of template ELT."
   (pcase elt
@@ -357,18 +351,47 @@ PROMPT is the optional prompt/default value."
       (when (and (buffer-modified-p) (y-or-n-p (format "Save file %s? " 
tempel-file)))
         (save-buffer buf)))))
 
-(defun tempel--templates ()
-  "Return templates for current mode."
+(defun tempel--file-read (file)
+  "Load templates from FILE."
+  (with-temp-buffer
+    (insert "(\n")
+    (insert-file-contents file)
+    (goto-char (point-max))
+    (insert "\n)")
+    (goto-char (point-min))
+    (let ((templates (read (current-buffer))) result)
+      (while (and templates (symbolp (car templates)))
+        (let ((mode (pop templates)) list)
+          (while (and templates (consp (car templates)))
+            (push (pop templates) list))
+          (push (cons mode (nreverse list)) result)))
+      result)))
+
+(defun tempel--file-templates ()
+  "Return templates defined in `tempel-file'."
   (let ((mod (time-convert (file-attribute-modification-time
                             (file-attributes tempel-file))
                            'integer)))
-    (unless (equal tempel--modified mod)
-      (setq tempel--templates (tempel--load tempel-file)
-            tempel--modified mod)))
-  (cl-loop for x in tempel--templates
+    (unless (equal tempel--file-modified mod)
+      (setq tempel--file-templates (tempel--file-read tempel-file)
+            tempel--file-modified mod)))
+  (cl-loop for x in tempel--file-templates
            if (or (derived-mode-p (car x)) (eq (car x) 'fundamental-mode))
            append (cdr x)))
 
+(defun tempel--templates ()
+  "Return templates for current mode."
+  (let (result)
+    (run-hook-wrapped
+     'tempel-template-sources
+     (lambda (fun)
+       (cond
+        ((functionp fun) (setq result (append result (funcall fun))))
+        ((boundp fun) (setq result (append result (symbol-value fun))))
+        (t (error "Template source is not a function or a variable: %S" fun)))
+       nil))
+    result))
+
 (defun tempel--region ()
   "Return region bounds."
   (when (use-region-p)



reply via email to

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