From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Federico Tedin Newsgroups: gmane.emacs.bugs Subject: bug#39555: [PATCH] Allow tempo-define-template to reassign tags to new templates Date: Mon, 10 Feb 2020 23:34:04 +0100 Message-ID: <877e0u9in7.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="15594"; mail-complaints-to="usenet@ciao.gmane.io" To: 39555@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Feb 10 23:35:46 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1j1Heb-0003wQ-He for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 10 Feb 2020 23:35:45 +0100 Original-Received: from localhost ([::1]:40626 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j1Hea-0005Lu-Eu for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 10 Feb 2020 17:35:44 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42482) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j1Hdv-0004qL-TY for bug-gnu-emacs@gnu.org; Mon, 10 Feb 2020 17:35:06 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j1Hdu-0000ag-1L for bug-gnu-emacs@gnu.org; Mon, 10 Feb 2020 17:35:03 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:49704) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1j1Hdt-0000a6-RV for bug-gnu-emacs@gnu.org; Mon, 10 Feb 2020 17:35:01 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1j1Hdt-0008CK-NO for bug-gnu-emacs@gnu.org; Mon, 10 Feb 2020 17:35:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Federico Tedin Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 10 Feb 2020 22:35:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 39555 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.158137406031461 (code B ref -1); Mon, 10 Feb 2020 22:35:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 10 Feb 2020 22:34:20 +0000 Original-Received: from localhost ([127.0.0.1]:55677 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1j1HdE-0008BN-CY for submit@debbugs.gnu.org; Mon, 10 Feb 2020 17:34:20 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:56275) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1j1HdC-0008BE-PH for submit@debbugs.gnu.org; Mon, 10 Feb 2020 17:34:19 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:41652) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j1Hd9-00049Q-65 for bug-gnu-emacs@gnu.org; Mon, 10 Feb 2020 17:34:18 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j1Hd7-00081y-L7 for bug-gnu-emacs@gnu.org; Mon, 10 Feb 2020 17:34:15 -0500 Original-Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]:56093) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1j1Hd7-000802-9y for bug-gnu-emacs@gnu.org; Mon, 10 Feb 2020 17:34:13 -0500 Original-Received: by mail-wm1-x341.google.com with SMTP id q9so1066034wmj.5 for ; Mon, 10 Feb 2020 14:34:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version; bh=zfqTBjceWMlcOfvRX6cyMOH0dxV3Z6q+I66bm1ObztM=; b=nVxwbwK1eu0oGyidjNe2Bw6repmRPquuNJpGnNGoWwhWOrC73DmDfcN3fXug1mehIi N4dmM3QVYDod4/DPdmtAbw5Zo2h1EQ15M2eorBkvZfagSmzu60/W5lC+2GwFZQV3ETNe 8x3AAxjTj2ou8GHXZDl8oAqkFIfuoX6b28cg9TrCoIzyjloo95V3iPa3SNPUzvJDPqmJ lUrkbZtNOhrAMn5aMvTOknXGULNxpJcAhjW/INO9evU7qfIGSL+SkR85h96rt1qeVVUS Qs9Ygh7zqeg2TnfEcszL1kXqxjLjaujqE++0mh8siadMu9mYmqvsIawFrkbN4UJAgLgq lj2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version; bh=zfqTBjceWMlcOfvRX6cyMOH0dxV3Z6q+I66bm1ObztM=; b=gwbeyU9l0dKn5GVPdpY5DRNweOUxfKp5YcTQimqMeQuJ/vEzw5976pQoGhy/05AGBm LzUybPipoJqQfZdo0TkBTNkGHwpq9Uxfr7aSE2YN7S07h6P6jCHRuyeS0jgwx3eRDhmw +J0oxVp0txvlc5lI80ztyeum8IXnpDkQVyLRLGjbbzKBUe7vAYL2QBBsIalfiMpZ+y/U j0jrPhBoRjiNpspoAalmJq8zhPe1WISZmEkXt8ZLkN9b2p0kst/nBiTTe4VzeKZVwvQt kDsl9GaBEb54OUE2/DCgHkvtpGSMRFkIR/lywCbtt5GRVb+JT3BA/XOU+YYUCr4nDh6C qcww== X-Gm-Message-State: APjAAAUAlRF5t7l2bjEcHiNGB0R8wP85fBqisV2X07y1kk2F3REomu4T j36yzzGV8y9fpa5HSQuv/vxXmPSL X-Google-Smtp-Source: APXvYqz1cP+/BX5tgqEeGMP1CNBdMLbuqQTs70c6A1v2LJgLszeVIuPr6ozUi/2/MZ3gdYq01BLJrA== X-Received: by 2002:a1c:f717:: with SMTP id v23mr1222906wmh.172.1581374051375; Mon, 10 Feb 2020 14:34:11 -0800 (PST) Original-Received: from lead (ip-109-41-1-62.web.vodafone.de. [109.41.1.62]) by smtp.gmail.com with ESMTPSA id f62sm972691wmf.36.2020.02.10.14.34.05 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 10 Feb 2020 14:34:06 -0800 (PST) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:175914 Archived-At: --=-=-= Content-Type: text/plain (This may apply as a bug and as a wishlist item at the same time - not sure) Two problems I've found using tempo.el are: 1) If I'm using templates + tags (with `tempo-complete-tag') in a buffer, and I define new templates + tags from another buffer, I can't immediately use them in the original buffer (I have to do M-: (setq tempo-dirty-collection t) ). 2) If I want to assign a different template to an already existing tag, I have to remove the tag manually from the tags collection. These two problems can become a bit annoying specially when writing/trying out new templates with tags. For someone learning about tempo.el for the first time, I can imagine they could be a problem as well. To fix these, I'm attaching a patch with some changes. In `tempo-invalidate-collection', I decided to loop over every buffer to check if `tempo-dirty-collection' has a local value, and only set it to t if it does. I am not sure if this is the ideal way of setting a buffer-local variable to a certain value for all buffers that have bound a local value to it. Feedback is welcome. - Fede --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=tempo.patch Content-Description: patch >From d5495b81919b4dbe48a7d81f4eb89bb5d4d4a45b Mon Sep 17 00:00:00 2001 From: Federico Tedin Date: Mon, 10 Feb 2020 23:18:16 +0100 Subject: [PATCH 1/1] 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. --- lisp/tempo.el | 31 +++++++++++++++++++++++-------- test/lisp/tempo-tests.el | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/lisp/tempo.el b/lisp/tempo.el index 9de5ac66c7..3d19fc135b 100644 --- a/lisp/tempo.el +++ b/lisp/tempo.el @@ -220,7 +220,9 @@ tempo-define-template 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 @@ tempo-backward-mark (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 @@ tempo-use-tag-list ;;; ;;; 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. When 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 diff --git a/test/lisp/tempo-tests.el b/test/lisp/tempo-tests.el index 0dd310b853..bfe475910d 100644 --- a/test/lisp/tempo-tests.el +++ b/test/lisp/tempo-tests.el @@ -216,6 +216,45 @@ tempo-expand-tag-test (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 -- 2.17.1 --=-=-=--