GNU bug report logs - #44328
27.1; [PATCH] Add expand-abbrev-maybe condition key binding

Previous Next

Package: emacs;

Reported by: Zhu Zihao <all_but_last <at> 163.com>

Date: Fri, 30 Oct 2020 14:49:02 UTC

Severity: normal

Tags: patch, wontfix

Found in version 27.1

Done: Lars Ingebrigtsen <larsi <at> gnus.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 44328 in the body.
You can then email your comments to 44328 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#44328; Package emacs. (Fri, 30 Oct 2020 14:49:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Zhu Zihao <all_but_last <at> 163.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Fri, 30 Oct 2020 14:49:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Zhu Zihao <all_but_last <at> 163.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 27.1; [PATCH] Add expand-abbrev-maybe condition key binding
Date: Fri, 30 Oct 2020 22:32:32 +0800
[Message part 1 (text/plain, inline)]
This patch add expand-abbrev-maybe. A conditional keybinding inspired
from YASnippet.

When bound to something like "TAB", it will expand abbrev if there's
some at point, it will do original binding(e.g. indent) if there's no
abbrev.

This is helpful for somebody doesn't like aggressive expand strategy in
abbrev-mode, but still doesn't want to give a exclusive keybinding for
expand-abbrev.

[0001-Add-conditonal-key-binding-expand-abbrev-maybe.patch (text/x-patch, inline)]
From 3df90b6c045d8f9e5e91890b44a96a9685e872ae Mon Sep 17 00:00:00 2001
From: Zhu Zihao <all_but_last <at> 163.com>
Date: Fri, 30 Oct 2020 22:24:54 +0800
Subject: [PATCH] Add conditonal key binding expand-abbrev-maybe

A binding which will expand any existing abbrev at point and act like
original keybinding when there's none abbrev at point.

* lisp/abbrev.el (expand-abbrev-maybe): New variable.
* doc/emacs/abbrevs.texi (Abbrev Concepts): New vindex "expand-abbrev-maybe".
---
 doc/emacs/abbrevs.texi | 6 ++++++
 lisp/abbrev.el         | 7 +++++++
 2 files changed, 13 insertions(+)

diff --git a/doc/emacs/abbrevs.texi b/doc/emacs/abbrevs.texi
index e3766aae9e..2d7b4e1698 100644
--- a/doc/emacs/abbrevs.texi
+++ b/doc/emacs/abbrevs.texi
@@ -210,6 +210,12 @@ Expanding Abbrevs
 unexpand-abbrev} to cancel the last expansion without deleting the
 terminating character.
 
+@vindex expand-abbrev-maybe
+  If you don't want to check expand for every @code{self-insert-command}
+like Abbrev mode. There's a variable describing a conditional key definition
+You can bind it to some key like @kbd{TAB}. It' ll expand abbrev if
+there's some abbrev at point, and act like usual @kbd{TAB} if there's not.
+
 @findex expand-region-abbrevs
   @kbd{M-x expand-region-abbrevs} searches through the region for defined
 abbrevs, and for each one found offers to replace it with its expansion.
diff --git a/lisp/abbrev.el b/lisp/abbrev.el
index f35c637eed..7d4bb9621f 100644
--- a/lisp/abbrev.el
+++ b/lisp/abbrev.el
@@ -978,6 +978,13 @@ expand-abbrev
       (if abbrev-suggest
           (abbrev--suggest-maybe-suggest))))
 
+(defconst expand-abbrev-maybe
+  '(menu-item "" expand-abbrev
+    :filter (lambda (cmd) (and (abbrev--before-point) cmd)))
+  "A conditional key definition for `expand-abbrev'.
+When this was bound, it will expand abbrev at point if there're any possible
+abbrev.")
+
 (defun abbrev--default-expand ()
   "Default function to use for `abbrev-expand-function'.
 This also respects the obsolete wrapper hook `abbrev-expand-functions'.
-- 
2.29.1

[Message part 3 (text/plain, inline)]
-- 
Retrieve my PGP public key: https://meta.sr.ht/~citreu.pgp

Zihao
[signature.asc (application/pgp-signature, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#44328; Package emacs. (Sun, 01 Nov 2020 14:26:02 GMT) Full text and rfc822 format available.

Message #8 received at 44328 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Zhu Zihao <all_but_last <at> 163.com>
Cc: 44328 <at> debbugs.gnu.org
Subject: Re: bug#44328: 27.1; [PATCH] Add expand-abbrev-maybe condition key
 binding
Date: Sun, 01 Nov 2020 15:25:21 +0100
Zhu Zihao <all_but_last <at> 163.com> writes:

> +@vindex expand-abbrev-maybe
> +  If you don't want to check expand for every @code{self-insert-command}
> +like Abbrev mode. There's a variable describing a conditional key definition
> +You can bind it to some key like @kbd{TAB}. It' ll expand abbrev if
> +there's some abbrev at point, and act like usual @kbd{TAB} if there's not.

[...]

> +(defconst expand-abbrev-maybe
> +  '(menu-item "" expand-abbrev
> +    :filter (lambda (cmd) (and (abbrev--before-point) cmd)))
> +  "A conditional key definition for `expand-abbrev'.
> +When this was bound, it will expand abbrev at point if there're any possible
> +abbrev.")

I'm afraid I don't understand the meaning of this constant, or what this
is trying to achieve.  You talk about adding a new key binding, which
should presumably refer to a new command (i.e. function)?

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#44328; Package emacs. (Mon, 02 Nov 2020 05:43:01 GMT) Full text and rfc822 format available.

Message #11 received at 44328 <at> debbugs.gnu.org (full text, mbox):

From: "Zhu Zihao" <all_but_last <at> 163.com>
To: "Lars Ingebrigtsen" <larsi <at> gnus.org>
Cc: 44328 <at> debbugs.gnu.org
Subject: Re:Re: bug#44328: 27.1; [PATCH] Add expand-abbrev-maybe condition
 key binding
Date: Mon, 2 Nov 2020 13:41:37 +0800 (CST)
[Message part 1 (text/plain, inline)]
Actually, it's a menu-item definition, described in https://www.gnu.org/software/emacs/manual/html_node/elisp/Extended-Menu-Items.html



It can be bound to a keymap and use it like key bindings, it also support some dynamic features which normal keybinding can't do, according to manual.


















At 2020-11-01 22:25:21, "Lars Ingebrigtsen" <larsi <at> gnus.org> wrote:
>Zhu Zihao <all_but_last <at> 163.com> writes:
>
>> +@vindex expand-abbrev-maybe
>> +  If you don't want to check expand for every @code{self-insert-command}
>> +like Abbrev mode. There's a variable describing a conditional key definition
>> +You can bind it to some key like @kbd{TAB}. It' ll expand abbrev if
>> +there's some abbrev at point, and act like usual @kbd{TAB} if there's not.
>
>[...]
>
>> +(defconst expand-abbrev-maybe
>> +  '(menu-item "" expand-abbrev
>> +    :filter (lambda (cmd) (and (abbrev--before-point) cmd)))
>> +  "A conditional key definition for `expand-abbrev'.
>> +When this was bound, it will expand abbrev at point if there're any possible
>> +abbrev.")
>
>I'm afraid I don't understand the meaning of this constant, or what this
>is trying to achieve.  You talk about adding a new key binding, which
>should presumably refer to a new command (i.e. function)?
>
>-- 
>(domestic pets only, the antidote for overdose, milk.)
>   bloggy blog: http://lars.ingebrigtsen.no
[Message part 2 (text/html, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#44328; Package emacs. (Mon, 02 Nov 2020 15:31:01 GMT) Full text and rfc822 format available.

Message #14 received at 44328 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: "Zhu Zihao" <all_but_last <at> 163.com>
Cc: 44328 <at> debbugs.gnu.org
Subject: Re: bug#44328: 27.1; [PATCH] Add expand-abbrev-maybe condition key
 binding
Date: Mon, 02 Nov 2020 16:30:00 +0100
"Zhu Zihao" <all_but_last <at> 163.com> writes:

> Actually, it's a menu-item definition, described in
> https://www.gnu.org/software/emacs/manual/html_node/elisp/Extended-Menu-Items.html
>
> It can be bound to a keymap and use it like key bindings, it also support some
> dynamic features which normal keybinding can't do, according to manual.

OK, but a normal command would surely make more sense here?

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#44328; Package emacs. (Tue, 03 Nov 2020 04:42:01 GMT) Full text and rfc822 format available.

Message #17 received at 44328 <at> debbugs.gnu.org (full text, mbox):

From: Zhu Zihao <all_but_last <at> 163.com>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: 44328 <at> debbugs.gnu.org
Subject: Re: bug#44328: 27.1; [PATCH] Add expand-abbrev-maybe condition key
 binding
Date: Tue, 03 Nov 2020 12:40:52 +0800
[Message part 1 (text/plain, inline)]
I'm afraid that a normal command can't do this. Please see following
example.

```
(defvar my-override-map (make-sparse-keymap))

(define-minor-mode my-keymap-override-mode
  ""
  :keymap my-override-map)

(defconst expand-abbrev-maybe
  '(menu-item "" expand-abbrev
    :filter (lambda (cmd) (and (abbrev--before-point) cmd))))

(define-key my-override-map (kbd "C-'") expand-abbrev-maybe)

(global-set-key (kbd "C-'") #'eww)

(define-abbrev emacs-lisp-mode-abbrev-table "lb" ";; -*- lexical-binding: t -*-")
```

I first create a minor mode for keymap overriding, then set the global
key C-' to eww. And bind expand-abbrev-maybe to the keymap of minor mode
my-keymap-override-mode.

And I define a abbrev "lb" in Elisp mode. then enable minor mode
my-override-map in Elisp mode. If my cursor is following "lb"(like
"lb|"), it will expand the abbrev, otherwise it will execute eww.

Now I change the key binding of C-' in global map(change to gnus). the
behaviour of keybinding in my-override-map will become "Expand abbrev if
possible otherwise execute gnus" 

Lars Ingebrigtsen writes:

> "Zhu Zihao" <all_but_last <at> 163.com> writes:
>
>> Actually, it's a menu-item definition, described in
>> https://www.gnu.org/software/emacs/manual/html_node/elisp/Extended-Menu-Items.html
>>
>> It can be bound to a keymap and use it like key bindings, it also support some
>> dynamic features which normal keybinding can't do, according to manual.
>
> OK, but a normal command would surely make more sense here?


-- 
Retrieve my PGP public key: https://meta.sr.ht/~citreu.pgp

Zihao
[signature.asc (application/pgp-signature, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#44328; Package emacs. (Thu, 13 May 2021 11:27:01 GMT) Full text and rfc822 format available.

Message #20 received at 44328 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Zhu Zihao <all_but_last <at> 163.com>
Cc: 44328 <at> debbugs.gnu.org
Subject: Re: bug#44328: 27.1; [PATCH] Add expand-abbrev-maybe condition key
 binding
Date: Thu, 13 May 2021 13:25:52 +0200
Zhu Zihao <all_but_last <at> 163.com> writes:

> And I define a abbrev "lb" in Elisp mode. then enable minor mode
> my-override-map in Elisp mode. If my cursor is following "lb"(like
> "lb|"), it will expand the abbrev, otherwise it will execute eww.
>
> Now I change the key binding of C-' in global map(change to gnus). the
> behaviour of keybinding in my-override-map will become "Expand abbrev if
> possible otherwise execute gnus" 

Ah, I understand what you mean now -- you want a key that's normally
bound to a different command, but if it's after an abbreviation, it
should expand the abbreviation instead.

Yes, that menu trick does work for that use case, but you could also use
a command like:

(defun my-expand ()
  (interactive)
  (if (abbrev--before-point)
      (expand-abbrev)
    (call-interactively
     (lookup-key (delq my-override-map (current-active-maps))
		 (this-command-keys)))))

In any case, I don't think these sort of "chained" keystrokes are very
common -- I think most users would find them somewhat confusing, so I
don't think adding something like this to Emacs would be appropriate.
So I'm closing this bug report.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Added tag(s) wontfix. Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Thu, 13 May 2021 11:27:02 GMT) Full text and rfc822 format available.

bug closed, send any further explanations to 44328 <at> debbugs.gnu.org and Zhu Zihao <all_but_last <at> 163.com> Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Thu, 13 May 2021 11:27:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Fri, 11 Jun 2021 11:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 2 years and 314 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.