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

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

[elpa] externals/tempel bffdf5140c: Add org source block support (Fix #4


From: ELPA Syncer
Subject: [elpa] externals/tempel bffdf5140c: Add org source block support (Fix #48)
Date: Mon, 11 Apr 2022 08:57:50 -0400 (EDT)

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

    Add org source block support (Fix #48)
---
 tempel.el | 40 ++++++++++++++++++++++++++++++++--------
 1 file changed, 32 insertions(+), 8 deletions(-)

diff --git a/tempel.el b/tempel.el
index 3ee0932bde..46ddf91fce 100644
--- a/tempel.el
+++ b/tempel.el
@@ -89,6 +89,10 @@ must return a list of templates which apply to the buffer or 
context."
 If a file is modified, added or removed, reload the templates."
   :type 'boolean)
 
+(defcustom tempel-local-mode-hook (list  #'tempel-org-src-block-mode)
+  "Hooks which return the local mode at point, e.g., in Org source blocks."
+  :type 'hook)
+
 (defface tempel-field
   '((((class color) (min-colors 88) (background light))
      :background "#fdf0ff" :foreground "#541f4f")
@@ -452,16 +456,36 @@ This is meant to be a source in 
`tempel-template-sources'."
               tempel--path-templates (mapcan #'tempel--file-read files)))))
   (cl-loop
    for (modes plist . templates) in tempel--path-templates
-   if (and
-       (cl-loop for m in modes
-                thereis (or (derived-mode-p m) (eq m #'fundamental-mode)))
-       (or (not (plist-member plist :condition))
-           (save-excursion
-             (save-restriction
-               (save-match-data
-                 (eval (plist-get plist :condition) 'lexical))))))
+   if (tempel--condition-p modes plist)
    append templates))
 
+(defun tempel--condition-p (modes plist)
+  "Return non-nil if one of MODES matches and the PLIST condition is 
satisfied."
+  (and
+   (cl-loop
+    for m in modes thereis
+    (or (eq m #'fundamental-mode)
+        (provided-mode-derived-p
+         (or (run-hook-with-args-until-success 'tempel-local-mode-hook)
+             major-mode)
+         m)))
+   (or (not (plist-member plist :condition))
+       (save-excursion
+         (save-restriction
+           (save-match-data
+             (eval (plist-get plist :condition) 'lexical)))))))
+
+(declare-function org-element-context "org-element")
+(declare-function org-src-get-lang-mode "org-src")
+(defun tempel-org-src-block-mode ()
+  "Return Org source block language mode when inside a source block."
+  (when-let* (((derived-mode-p 'org-mode))
+              (element (org-element-context))
+              ((eq 'src-block (car-safe element))))
+    (if-let (lang (plist-get (cadr element) :language))
+        (org-src-get-lang-mode lang)
+      #'fundamental-mode)))
+
 (defun tempel--templates ()
   "Return templates for current mode."
   (let (result)



reply via email to

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