From 0f830c89eb82b0966e285b67099301079ff31d61 Mon Sep 17 00:00:00 2001 From: Allen Li Date: Sun, 16 Jun 2019 03:32:02 -0700 Subject: [PATCH] Fix defining inverse abbrevs on previous words * lisp/abbrev.el (inverse-add-abbrev): Skip trailing nonword characters when defining abbrev. * test/lisp/abbrev-tests.el (abbrev-edit-save-to-file-test): Add regression tests. --- lisp/abbrev.el | 5 ++++- test/lisp/abbrev-tests.el | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lisp/abbrev.el b/lisp/abbrev.el index aebf65e0f7..51604ed929 100644 --- a/lisp/abbrev.el +++ b/lisp/abbrev.el @@ -353,7 +353,10 @@ inverse-add-global-abbrev (defun inverse-add-abbrev (table type arg) (let (name exp start end) (save-excursion - (forward-word (1+ (- arg))) + (if (<= arg 0) + (forward-word (1+ (- arg))) + (forward-word (- arg)) + (forward-word)) (setq end (point)) (backward-word 1) (setq start (point) diff --git a/test/lisp/abbrev-tests.el b/test/lisp/abbrev-tests.el index 800c9aac33..33a5ec7dab 100644 --- a/test/lisp/abbrev-tests.el +++ b/test/lisp/abbrev-tests.el @@ -249,6 +249,34 @@ setup-test-abbrev-table (abbrev-expansion "s-a-t" ert-save-test-table))) (delete-file temp-test-file)))) +(ert-deftest inverse-add-abbrev-skips-trailing-nonword () + "Test that adding an inverse abbrev skips trailing nonword characters." + (let ((table (make-abbrev-table))) + (with-temp-buffer + (insert "some text foo ") + (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "bar"))) + (inverse-add-abbrev table "Global" 1))) + (should (string= (abbrev-expansion "foo" table) "bar")))) + +(ert-deftest inverse-add-abbrev-skips-trailing-nonword/postiive-arg () + "Test that adding an inverse abbrev skips trailing nonword characters." + (let ((table (make-abbrev-table))) + (with-temp-buffer + (insert "some text foo ") + (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "bar"))) + (inverse-add-abbrev table "Global" 2))) + (should (string= (abbrev-expansion "text" table) "bar")))) + +(ert-deftest inverse-add-abbrev-skips-trailing-nonword/negative-arg () + "Test that adding an inverse abbrev skips trailing nonword characters." + (let ((table (make-abbrev-table))) + (with-temp-buffer + (insert "some text foo") + (goto-char (point-min)) + (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "bar"))) + (inverse-add-abbrev table "Global" -1))) + (should (string= (abbrev-expansion "text" table) "bar")))) + (provide 'abbrev-tests) ;;; abbrev-tests.el ends here -- 2.22.0