[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)
- [elpa] externals/tempel 69f7cb3212 06/82: Reimplement without Tempo for more flexibility, (continued)
- [elpa] externals/tempel 69f7cb3212 06/82: Reimplement without Tempo for more flexibility, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel fbef94d77d 48/82: Minor cleanup, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 01e7430343 27/82: Add docstrings, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel a03c36c8dd 02/82: Improve behavior when templates are nested, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel db6847a7f9 56/82: Make temple-key a macro, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 0e618cd169 23/82: Do not sort overlays, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 04d4f6c055 55/82: Use named key function, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 91f6978ddd 60/82: Use Tempo PROMPT as default value, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel e20f982dc5 25/82: Remove global variable tempel--state, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel fa99fb7f96 34/82: README: Update keybindings, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel c4cd6e0f69 73/82: Rename tempel-expand -> tempel-complete, add tempel-expand (Fix #13),
ELPA Syncer <=
- [elpa] externals/tempel 8f8588985a 04/82: Simplify template markers, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel d9efb707b3 20/82: Improve undo behavior, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 4bc75c1aba 52/82: Use the more subtle face colors from Modus, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 52d8556c05 57/82: Improve annotation function, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 675e759bde 64/82: Add special prompt treatment, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel e6aa1892af 14/82: README: Simplify template, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 2ba940809a 42/82: Add temple-abort (Fix #2), ELPA Syncer, 2022/01/09
- [elpa] externals/tempel 5521546f48 10/82: README: Add header, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel d2d5566282 65/82: Improve description of PROMPT/DEFAULT value, ELPA Syncer, 2022/01/09
- [elpa] externals/tempel e529887d5d 70/82: tempel-expand: Fix exit function, ELPA Syncer, 2022/01/09