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

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

[elpa] externals/tempel c4cd6e0f69 73/82: Rename tempel-expand -> tempel


From: ELPA Syncer
Subject: [elpa] externals/tempel c4cd6e0f69 73/82: Rename tempel-expand -> tempel-complete, add tempel-expand (Fix #13)
Date: Sun, 9 Jan 2022 20:58:46 -0500 (EST)

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

    Rename tempel-expand -> tempel-complete, add tempel-expand (Fix #13)
    
    tempel-expand only triggers on exactly matching template names.
---
 README.org | 27 +++++++++++++++----------
 tempel.el  | 68 ++++++++++++++++++++++++++++++++++++++++++++------------------
 2 files changed, 64 insertions(+), 31 deletions(-)

diff --git a/README.org b/README.org
index f912069025..be01060d89 100644
--- a/README.org
+++ b/README.org
@@ -13,19 +13,24 @@ Tempel is a tiny template package for Emacs, which uses the 
syntax of the Emacs
 Tempo library. Tempo is an ancient temple. It is 27 years old, but still in 
good
 shape since it successfully resisted change over the decades. However it may
 look a bit dusty here and there. Therefore I present to you, Tempel, a
-modernized implementation of Tempo, in the form of two commands:
-
-+ ~tempel-expand~ expands a template at point in the buffer. If called
-  non-interactively the function behaves like a completion-at-point-function
-  (Capf). You may want to give my [[https://github.com/minad/corfu][Corfu]] 
completion at point UI a try.
+modernized implementation of Tempo, in the form of three commands:
+
++ ~tempel-complete~ completes a template name at point in the buffer and
+  subsequently expands the template. If called non-interactively the function 
behaves like a Capf and can be added
+  to ~completion-at-point-functions~.
++ ~tempel-expand~ expands an exactly matching template name at point in the 
buffer.
+  If called non-interactively the function behaves like a Capf and can be added
+  to ~completion-at-point-functions~.
 + ~tempel-insert~ selects a template by name and insert it into the current 
buffer.
 
-After inserting the template you can move between the visible template fields
-with the keys ~M-{~, ~M-}~ or ~C-up/down~ which are normally bound to
-~forward/backward-paragraph~. Tempel temporarily remaps these commands to
-~tempel-next~ and ~tempel-previous~. The key bindings are defined in the 
~tempel-map~
-keymap. You can customize them there. As soon as you move before (behind) the
-first (last) field, the fields are finalized.
+For the completion at point commands ~tempel-complete~ and ~tempel-expand~, 
you may
+want to give my [[https://github.com/minad/corfu][Corfu]] completion at point 
popup UI a try. After inserting the
+template you can move between the visible template fields with the keys ~M-{~, 
~M-}~
+or ~C-up/down~ which are normally bound to ~forward/backward-paragraph~. Tempel
+temporarily remaps these commands to ~tempel-next~ and ~tempel-previous~. The 
key
+bindings are defined in the ~tempel-map~ keymap. You can customize them there. 
As
+soon as you move before (behind) the first (last) field, the fields are
+finalized.
 
 Note that this package is not a competitor to the mature and widely used
 YASnippet library. Try Tempel only if you like small and simple packages. With
diff --git a/tempel.el b/tempel.el
index 1db5349c43..d35de96042 100644
--- a/tempel.el
+++ b/tempel.el
@@ -28,10 +28,12 @@
 ;; format is compatible with the template format of the Emacs Tempo
 ;; library. Your templates are stored in the `tempel-file' (by default
 ;; the file "templates" in the `user-emacs-directory'). Bind the
-;; commands `tempel-expand' or `tempel-insert' to some keys in your user
-;; configuration. You can jump with the keys M-{ and M-} from field to
-;; field. `tempel-expands' works best with the Corfu completion UI,
-;; while `tempel-insert' uses `completing-read' under the hood.
+;; commands `tempel-complete', `tempel-expand' or `tempel-insert' to
+;; some keys in your user configuration. You can jump with the keys M-{
+;; and M-} from field to field. `tempel-complete' and `tempel-expand'
+;; work best with the Corfu completion UI, while `tempel-insert' uses
+;; `completing-read' under the hood. You can also use `tempel-complete'
+;; and `tempel-expand' as `completion-at-point-functions'.
 
 ;;; Code:
 
@@ -58,8 +60,8 @@
   "Annotation width for `tempel-insert'."
   :type '(choice (const nil integer)))
 
-(defcustom tempel-expand-annotation 20
-  "Annotation width for `tempel-expand'."
+(defcustom tempel-complete-annotation 20
+  "Annotation width for `tempel-complete'."
   :type '(choice (const nil integer)))
 
 (defface tempel-field
@@ -152,6 +154,16 @@ WIDTH, SEP and ELLIPSIS configure the formatting."
                           'face 'completions-annotations))
              width 0 ?\s ellipsis))))
 
+(defun tempel--exit (templates region name status)
+  "Exit function for completion for template NAME and STATUS.
+TEMPLATES is the list of templates.
+REGION are the current region bouns"
+  (unless (eq status 'exact)
+    (when-let* ((sym (intern-soft name))
+                (template (alist-get sym templates)))
+      (delete-region (max (point-min) (- (point) (length name))) (point))
+      (tempel--insert template region))))
+
 (defun tempel--field-modified (ov after beg end &optional _len)
   "Update field overlay OV.
 AFTER is non-nil after the modification.
@@ -396,16 +408,38 @@ PROMPT is the optional prompt/default value."
   ;; TODO disable only the topmost template?
   (while tempel--active (tempel--disable)))
 
+(defun tempel--interactive (capf)
+  "Complete with CAPF."
+  (let ((completion-at-point-functions (list capf))
+        completion-cycle-threshold)
+    (tempel--save)
+    (or (completion-at-point) (user-error "%s: No completions" capf))))
+
 ;;;###autoload
 (defun tempel-expand (&optional interactive)
-  "Complete template at point.
+  "Expand exactly matching template name at point.
+If INTERACTIVE is nil the function acts like a capf."
+  (interactive (list t))
+  (if interactive
+      (tempel--interactive #'tempel-exact)
+    (when-let* ((templates (tempel--templates))
+                (bounds (bounds-of-thing-at-point 'symbol))
+                (name (buffer-substring-no-properties
+                       (car bounds) (cdr bounds)))
+                (sym (intern-soft name))
+                (template (assq sym templates)))
+      (setq templates (list template))
+      (list (car bounds) (cdr bounds) templates
+            :exclusive 'no
+            :exit-function (apply-partially #'tempel--exit templates nil)))))
+
+;;;###autoload
+(defun tempel-complete (&optional interactive)
+  "Complete template name at point and expand.
 If INTERACTIVE is nil the function acts like a capf."
   (interactive (list t))
   (if interactive
-      (let (completion-cycle-threshold
-            (completion-at-point-functions (list #'tempel-expand)))
-        (tempel--save)
-        (or (completion-at-point) (user-error "Tempel: No completions")))
+      (tempel--interactive #'tempel-complete)
     (when-let (templates (tempel--templates))
       (let* ((region (tempel--region))
              (bounds (or (and (not region) (bounds-of-thing-at-point 'symbol))
@@ -413,17 +447,11 @@ If INTERACTIVE is nil the function acts like a capf."
         (list (car bounds) (cdr bounds) templates
               :exclusive 'no
               :company-kind (lambda (_) 'snippet)
-              :exit-function
-              (lambda (name status)
-                (unless (eq status 'exact)
-                  (when-let* ((sym (intern-soft name))
-                              (template (alist-get sym templates)))
-                    (delete-region (max (point-min) (- (point) (length name))) 
(point))
-                    (tempel--insert template region))))
+              :exit-function (apply-partially #'tempel--exit templates region)
               :annotation-function
-              (and tempel-expand-annotation
+              (and tempel-complete-annotation
                    (apply-partially #'tempel--annotate
-                                    templates tempel-expand-annotation nil " 
")))))))
+                                    templates tempel-complete-annotation nil " 
")))))))
 
 ;;;###autoload
 (defun tempel-insert (name)



reply via email to

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