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)
This commit is contained in:
Federico Tedin 2020-02-17 22:24:40 +01:00 committed by Eli Zaretskii
parent 41450a8ea5
commit 97b8a78334
3 changed files with 70 additions and 8 deletions

View file

@ -131,6 +131,14 @@ supplied error message.
*** New connection method "media", which allows accessing media devices
like cell phones, tablets or cameras.
** Tempo
---
*** 'tempo-define-template' can now re-assign templates to tags.
Previously, assigning a new template to an already defined tag had no
effect.
** map.el
*** Pcase 'map' pattern added keyword symbols abbreviation.

View file

@ -220,7 +220,9 @@ list of elements in the template, TAG is the tag used for completion,
DOCUMENTATION is the documentation string for the insertion command
created, and TAGLIST (a symbol) is the tag list that TAG (if provided)
should be added to. If TAGLIST is nil and TAG is non-nil, TAG is
added to `tempo-tags'.
added to `tempo-tags'. If TAG already corresponds to a template in
the tag list, modify the list so that TAG now corresponds to the newly
defined template.
The elements in ELEMENTS can be of several types:
@ -579,14 +581,20 @@ and insert the results."
(defun tempo-add-tag (tag template &optional tag-list)
"Add a template tag.
Add the TAG, that should complete to TEMPLATE to the list in TAG-LIST,
or to `tempo-tags' if TAG-LIST is nil."
or to `tempo-tags' if TAG-LIST is nil. If TAG was already in the list,
replace its template with TEMPLATE."
(interactive "sTag: \nCTemplate: ")
(if (null tag-list)
(setq tag-list 'tempo-tags))
(if (not (assoc tag (symbol-value tag-list)))
(set tag-list (cons (cons tag template) (symbol-value tag-list))))
(tempo-invalidate-collection))
(let ((entry (assoc tag (symbol-value tag-list))))
(if entry
;; Tag is already in the list, assign a new template to it
(setcdr entry template)
;; Tag is not present in the list, add it with its template
(set tag-list (cons (cons tag template) (symbol-value tag-list)))))
;; Invalidate globally if we're modifying `tempo-tags'
(tempo-invalidate-collection (eq tag-list 'tempo-tags)))
;;;
;;; tempo-use-tag-list
@ -609,10 +617,17 @@ COMPLETION-FUNCTION just sets `tempo-match-finder' locally."
;;;
;;; tempo-invalidate-collection
(defun tempo-invalidate-collection ()
(defun tempo-invalidate-collection (&optional global)
"Marks the tag collection as obsolete.
Whenever it is needed again it will be rebuilt."
(setq tempo-dirty-collection t))
Whenever it is needed again it will be rebuilt. If GLOBAL is non-nil,
mark the tag collection of all buffers as obsolete, not just the
current one."
(if global
(dolist (buffer (buffer-list))
(with-current-buffer buffer
(when (assq 'tempo-dirty-collection (buffer-local-variables))
(setq tempo-dirty-collection t))))
(setq tempo-dirty-collection t)))
;;;
;;; tempo-build-collection

View file

@ -216,6 +216,45 @@
(tempo-complete-tag)
(should (equal (buffer-string) "Hello, World!"))))
(ert-deftest tempo-define-tag-globally-test ()
"Testing usage of a template tag defined from another buffer."
(tempo-define-template "test" '("Hello, World!") "hello")
(with-temp-buffer
;; Use a tag in buffer 1
(insert "hello")
(tempo-complete-tag)
(should (equal (buffer-string) "Hello, World!"))
(erase-buffer)
;; Collection should not be dirty
(should-not tempo-dirty-collection)
;; Define a tag on buffer 2
(with-temp-buffer
(tempo-define-template "test2" '("Now expanded.") "mytag"))
;; I should be able to use this template back in buffer 1
(insert "mytag")
(tempo-complete-tag)
(should (equal (buffer-string) "Now expanded."))))
(ert-deftest tempo-overwrite-tag-test ()
"Testing ability to reassign templates to tags."
(with-temp-buffer
;; Define a tag and use it
(tempo-define-template "test-tag-1" '("abc") "footag")
(insert "footag")
(tempo-complete-tag)
(should (equal (buffer-string) "abc"))
(erase-buffer)
;; Define a new template with the same tag
(tempo-define-template "test-tag-2" '("xyz") "footag")
(insert "footag")
(tempo-complete-tag)
(should (equal (buffer-string) "xyz"))))
(ert-deftest tempo-expand-partial-tag-test ()
"Testing expansion of a template with a tag, with a partial match."
(with-temp-buffer