aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFederico Tedin2020-02-17 22:24:40 +0100
committerEli Zaretskii2020-02-21 11:15:00 +0200
commit97b8a78334d22a6b12cc0f922771baf67a4030bc (patch)
treeef00d7258184f9185b65acd54b0df05f0beec83c
parent41450a8ea5a156a34f6641a0768cadb174fa261c (diff)
downloademacs-97b8a78334d22a6b12cc0f922771baf67a4030bc.tar.gz
emacs-97b8a78334d22a6b12cc0f922771baf67a4030bc.zip
Allow tempo-define-template to reassign tags to new templates
* lisp/tempo.el (tempo-define-template): Update documentation string to mention that existing tags can be reassigned new templates. (tempo-add-tag): Allow reassigning tags to new templates. Additionally, invalidate tag collections in all buffers if the global tags list is being modified. (tempo-invalidate-collection): Allow invalidating tag collections in all buffers at the same time. * test/lisp/tempo-tests.el (tempo-define-tag-globally-test): Add a test to check that new templates plus tags can be defined from any buffer and then immediately used in other buffers. (tempo-overwrite-tag-test): Add a test to check that tags can be reassigned templates. * etc/NEWS: Announce changes in tempo.el. (Bug#39555)
-rw-r--r--etc/NEWS8
-rw-r--r--lisp/tempo.el31
-rw-r--r--test/lisp/tempo-tests.el39
3 files changed, 70 insertions, 8 deletions
diff --git a/etc/NEWS b/etc/NEWS
index 1a51a90636d..02798798367 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -131,6 +131,14 @@ supplied error message.
131*** New connection method "media", which allows accessing media devices 131*** New connection method "media", which allows accessing media devices
132like cell phones, tablets or cameras. 132like cell phones, tablets or cameras.
133 133
134** Tempo
135
136---
137*** 'tempo-define-template' can now re-assign templates to tags.
138Previously, assigning a new template to an already defined tag had no
139effect.
140
141
134** map.el 142** map.el
135 143
136*** Pcase 'map' pattern added keyword symbols abbreviation. 144*** Pcase 'map' pattern added keyword symbols abbreviation.
diff --git a/lisp/tempo.el b/lisp/tempo.el
index 9de5ac66c7d..2da90f08c87 100644
--- a/lisp/tempo.el
+++ b/lisp/tempo.el
@@ -220,7 +220,9 @@ list of elements in the template, TAG is the tag used for completion,
220DOCUMENTATION is the documentation string for the insertion command 220DOCUMENTATION is the documentation string for the insertion command
221created, and TAGLIST (a symbol) is the tag list that TAG (if provided) 221created, and TAGLIST (a symbol) is the tag list that TAG (if provided)
222should be added to. If TAGLIST is nil and TAG is non-nil, TAG is 222should be added to. If TAGLIST is nil and TAG is non-nil, TAG is
223added to `tempo-tags'. 223added to `tempo-tags'. If TAG already corresponds to a template in
224the tag list, modify the list so that TAG now corresponds to the newly
225defined template.
224 226
225The elements in ELEMENTS can be of several types: 227The elements in ELEMENTS can be of several types:
226 228
@@ -579,14 +581,20 @@ and insert the results."
579(defun tempo-add-tag (tag template &optional tag-list) 581(defun tempo-add-tag (tag template &optional tag-list)
580 "Add a template tag. 582 "Add a template tag.
581Add the TAG, that should complete to TEMPLATE to the list in TAG-LIST, 583Add the TAG, that should complete to TEMPLATE to the list in TAG-LIST,
582or to `tempo-tags' if TAG-LIST is nil." 584or to `tempo-tags' if TAG-LIST is nil. If TAG was already in the list,
585replace its template with TEMPLATE."
583 586
584 (interactive "sTag: \nCTemplate: ") 587 (interactive "sTag: \nCTemplate: ")
585 (if (null tag-list) 588 (if (null tag-list)
586 (setq tag-list 'tempo-tags)) 589 (setq tag-list 'tempo-tags))
587 (if (not (assoc tag (symbol-value tag-list))) 590 (let ((entry (assoc tag (symbol-value tag-list))))
588 (set tag-list (cons (cons tag template) (symbol-value tag-list)))) 591 (if entry
589 (tempo-invalidate-collection)) 592 ;; Tag is already in the list, assign a new template to it
593 (setcdr entry template)
594 ;; Tag is not present in the list, add it with its template
595 (set tag-list (cons (cons tag template) (symbol-value tag-list)))))
596 ;; Invalidate globally if we're modifying `tempo-tags'
597 (tempo-invalidate-collection (eq tag-list 'tempo-tags)))
590 598
591;;; 599;;;
592;;; tempo-use-tag-list 600;;; tempo-use-tag-list
@@ -609,10 +617,17 @@ COMPLETION-FUNCTION just sets `tempo-match-finder' locally."
609;;; 617;;;
610;;; tempo-invalidate-collection 618;;; tempo-invalidate-collection
611 619
612(defun tempo-invalidate-collection () 620(defun tempo-invalidate-collection (&optional global)
613 "Marks the tag collection as obsolete. 621 "Marks the tag collection as obsolete.
614Whenever it is needed again it will be rebuilt." 622Whenever it is needed again it will be rebuilt. If GLOBAL is non-nil,
615 (setq tempo-dirty-collection t)) 623mark the tag collection of all buffers as obsolete, not just the
624current one."
625 (if global
626 (dolist (buffer (buffer-list))
627 (with-current-buffer buffer
628 (when (assq 'tempo-dirty-collection (buffer-local-variables))
629 (setq tempo-dirty-collection t))))
630 (setq tempo-dirty-collection t)))
616 631
617;;; 632;;;
618;;; tempo-build-collection 633;;; tempo-build-collection
diff --git a/test/lisp/tempo-tests.el b/test/lisp/tempo-tests.el
index 0dd310b8531..bfe475910da 100644
--- a/test/lisp/tempo-tests.el
+++ b/test/lisp/tempo-tests.el
@@ -216,6 +216,45 @@
216 (tempo-complete-tag) 216 (tempo-complete-tag)
217 (should (equal (buffer-string) "Hello, World!")))) 217 (should (equal (buffer-string) "Hello, World!"))))
218 218
219(ert-deftest tempo-define-tag-globally-test ()
220 "Testing usage of a template tag defined from another buffer."
221 (tempo-define-template "test" '("Hello, World!") "hello")
222
223 (with-temp-buffer
224 ;; Use a tag in buffer 1
225 (insert "hello")
226 (tempo-complete-tag)
227 (should (equal (buffer-string) "Hello, World!"))
228 (erase-buffer)
229
230 ;; Collection should not be dirty
231 (should-not tempo-dirty-collection)
232
233 ;; Define a tag on buffer 2
234 (with-temp-buffer
235 (tempo-define-template "test2" '("Now expanded.") "mytag"))
236
237 ;; I should be able to use this template back in buffer 1
238 (insert "mytag")
239 (tempo-complete-tag)
240 (should (equal (buffer-string) "Now expanded."))))
241
242(ert-deftest tempo-overwrite-tag-test ()
243 "Testing ability to reassign templates to tags."
244 (with-temp-buffer
245 ;; Define a tag and use it
246 (tempo-define-template "test-tag-1" '("abc") "footag")
247 (insert "footag")
248 (tempo-complete-tag)
249 (should (equal (buffer-string) "abc"))
250 (erase-buffer)
251
252 ;; Define a new template with the same tag
253 (tempo-define-template "test-tag-2" '("xyz") "footag")
254 (insert "footag")
255 (tempo-complete-tag)
256 (should (equal (buffer-string) "xyz"))))
257
219(ert-deftest tempo-expand-partial-tag-test () 258(ert-deftest tempo-expand-partial-tag-test ()
220 "Testing expansion of a template with a tag, with a partial match." 259 "Testing expansion of a template with a tag, with a partial match."
221 (with-temp-buffer 260 (with-temp-buffer