all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#36243: 26.2; defining inverse abbrevs include space
@ 2019-06-16 10:35 Allen Li
  2019-06-17 14:44 ` Noam Postavsky
  0 siblings, 1 reply; 5+ messages in thread
From: Allen Li @ 2019-06-16 10:35 UTC (permalink / raw)
  To: 36243

[-- Attachment #1: Type: text/plain, Size: 1086 bytes --]

Defining an inverse abbrev in a buffer like so (@ is point):

some text foo @

C-x a i l find outer otter RET

defines an abbrev "foo " -> "find outer otter"

This abbrev is impossible to expand because it ends in a non-word
character.

It would be more convenient if the inverse abbrev definition commands
skips trailing non-word characters, e.g. in the example above defining
"foo" -> "find outer otter".  The use case for this is that I often type
something that I think should be an abbrev and upon finding that it is
not defined, I must delete the previous character before using
inverse-add-{mode,global}-abbrev.

Note that this bug affects all positive prefix arguments to
inverse-add-{mode,global}-abbrev, e.g.:

some text: .... foo @

C-u 2 C-x a i l will attempt to define an abbrev "text: .... " -> "blah",
which is completely useless.

I have attached a patch fixing this, with regression tests.

In GNU Emacs 26.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.8)
 of 2019-04-12 built on juergen
Windowing system distributor 'The X.Org Foundation', version 11.0.12005000

[-- Attachment #2: 0001-Fix-defining-inverse-abbrevs-on-previous-words.patch --]
[-- Type: text/x-patch, Size: 2754 bytes --]

From 0f830c89eb82b0966e285b67099301079ff31d61 Mon Sep 17 00:00:00 2001
From: Allen Li <darkfeline@felesatra.moe>
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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* bug#36243: 26.2; defining inverse abbrevs include space
  2019-06-16 10:35 bug#36243: 26.2; defining inverse abbrevs include space Allen Li
@ 2019-06-17 14:44 ` Noam Postavsky
  2019-06-17 19:23   ` Allen Li
  0 siblings, 1 reply; 5+ messages in thread
From: Noam Postavsky @ 2019-06-17 14:44 UTC (permalink / raw)
  To: Allen Li; +Cc: 36243

Allen Li <darkfeline@felesatra.moe> writes:

> Defining an inverse abbrev in a buffer like so (@ is point):
>
> some text foo @
>
> C-x a i l find outer otter RET
>
> defines an abbrev "foo " -> "find outer otter"

> * lisp/abbrev.el (inverse-add-abbrev): Skip trailing nonword
> characters when defining 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)

This seems like a somewhat obfuscated way of skipping nonword
characters.  How about using skip-syntax-backward instead?





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#36243: 26.2; defining inverse abbrevs include space
  2019-06-17 14:44 ` Noam Postavsky
@ 2019-06-17 19:23   ` Allen Li
  2019-06-21 18:30     ` Allen Li
  2019-06-22 23:35     ` Noam Postavsky
  0 siblings, 2 replies; 5+ messages in thread
From: Allen Li @ 2019-06-17 19:23 UTC (permalink / raw)
  To: Noam Postavsky; +Cc: 36243

[-- Attachment #1: Type: text/plain, Size: 939 bytes --]

Thanks for suggesting skip-syntax-backward, see new patch.

On Mon, Jun 17, 2019 at 7:44 AM Noam Postavsky <npostavs@gmail.com> wrote:
>
> Allen Li <darkfeline@felesatra.moe> writes:
>
> > Defining an inverse abbrev in a buffer like so (@ is point):
> >
> > some text foo @
> >
> > C-x a i l find outer otter RET
> >
> > defines an abbrev "foo " -> "find outer otter"
>
> > * lisp/abbrev.el (inverse-add-abbrev): Skip trailing nonword
> > characters when defining 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)
>
> This seems like a somewhat obfuscated way of skipping nonword
> characters.  How about using skip-syntax-backward instead?

[-- Attachment #2: 0001-Fix-defining-inverse-abbrevs-on-previous-words.patch --]
[-- Type: text/x-patch, Size: 2621 bytes --]

From 91d4c9328d6a17b77a8d3a86495b1e17fe363d1c Mon Sep 17 00:00:00 2001
From: Allen Li <darkfeline@felesatra.moe>
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            |  1 +
 test/lisp/abbrev-tests.el | 28 ++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/lisp/abbrev.el b/lisp/abbrev.el
index aebf65e0f7..c0854b99fe 100644
--- a/lisp/abbrev.el
+++ b/lisp/abbrev.el
@@ -354,6 +354,7 @@ inverse-add-abbrev
   (let (name exp start end)
     (save-excursion
       (forward-word (1+ (- arg)))
+      (skip-syntax-backward "^w")
       (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.410.gd8fdbe21b5-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* bug#36243: 26.2; defining inverse abbrevs include space
  2019-06-17 19:23   ` Allen Li
@ 2019-06-21 18:30     ` Allen Li
  2019-06-22 23:35     ` Noam Postavsky
  1 sibling, 0 replies; 5+ messages in thread
From: Allen Li @ 2019-06-21 18:30 UTC (permalink / raw)
  To: Noam Postavsky; +Cc: 36243

Any update on this?

On Mon, Jun 17, 2019 at 7:23 PM Allen Li <darkfeline@felesatra.moe> wrote:
>
> Thanks for suggesting skip-syntax-backward, see new patch.
>
> On Mon, Jun 17, 2019 at 7:44 AM Noam Postavsky <npostavs@gmail.com> wrote:
> >
> > Allen Li <darkfeline@felesatra.moe> writes:
> >
> > > Defining an inverse abbrev in a buffer like so (@ is point):
> > >
> > > some text foo @
> > >
> > > C-x a i l find outer otter RET
> > >
> > > defines an abbrev "foo " -> "find outer otter"
> >
> > > * lisp/abbrev.el (inverse-add-abbrev): Skip trailing nonword
> > > characters when defining 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)
> >
> > This seems like a somewhat obfuscated way of skipping nonword
> > characters.  How about using skip-syntax-backward instead?





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#36243: 26.2; defining inverse abbrevs include space
  2019-06-17 19:23   ` Allen Li
  2019-06-21 18:30     ` Allen Li
@ 2019-06-22 23:35     ` Noam Postavsky
  1 sibling, 0 replies; 5+ messages in thread
From: Noam Postavsky @ 2019-06-22 23:35 UTC (permalink / raw)
  To: Allen Li; +Cc: 36243

tags 36243 fixed
close 36243 27.1
quit

Allen Li <darkfeline@felesatra.moe> writes:

> Thanks for suggesting skip-syntax-backward, see new patch.

> @@ -354,6 +354,7 @@ inverse-add-abbrev
>    (let (name exp start end)
>      (save-excursion
>        (forward-word (1+ (- arg)))
> +      (skip-syntax-backward "^w")
>        (setq end (point))
>        (backward-word 1)
>        (setq start (point)

Perfect, thanks, pushed to master.

2db75262c7 2019-06-22T19:25:44-04:00 "Fix defining inverse abbrevs on previous words (Bug#36243)"
https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=2db75262c7395483d1fa9a0c9d93dd3e4d534e1f






^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-06-22 23:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-06-16 10:35 bug#36243: 26.2; defining inverse abbrevs include space Allen Li
2019-06-17 14:44 ` Noam Postavsky
2019-06-17 19:23   ` Allen Li
2019-06-21 18:30     ` Allen Li
2019-06-22 23:35     ` Noam Postavsky

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.