unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#44328: 27.1; [PATCH] Add expand-abbrev-maybe condition key binding
@ 2020-10-30 14:32 Zhu Zihao
  2020-11-01 14:25 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 6+ messages in thread
From: Zhu Zihao @ 2020-10-30 14:32 UTC (permalink / raw)
  To: 44328

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


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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-conditonal-key-binding-expand-abbrev-maybe.patch --]
[-- Type: text/x-patch, Size: 2057 bytes --]

From 3df90b6c045d8f9e5e91890b44a96a9685e872ae Mon Sep 17 00:00:00 2001
From: Zhu Zihao <all_but_last@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


[-- Attachment #3.1: Type: text/plain, Size: 76 bytes --]


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

Zihao

[-- Attachment #3.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 515 bytes --]

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

* bug#44328: 27.1; [PATCH] Add expand-abbrev-maybe condition key binding
  2020-10-30 14:32 bug#44328: 27.1; [PATCH] Add expand-abbrev-maybe condition key binding Zhu Zihao
@ 2020-11-01 14:25 ` Lars Ingebrigtsen
  2020-11-02  5:41   ` bug#44328: " Zhu Zihao
  0 siblings, 1 reply; 6+ messages in thread
From: Lars Ingebrigtsen @ 2020-11-01 14:25 UTC (permalink / raw)
  To: Zhu Zihao; +Cc: 44328

Zhu Zihao <all_but_last@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





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

* bug#44328: Re: bug#44328: 27.1; [PATCH] Add expand-abbrev-maybe condition key binding
  2020-11-01 14:25 ` Lars Ingebrigtsen
@ 2020-11-02  5:41   ` Zhu Zihao
  2020-11-02 15:30     ` Lars Ingebrigtsen
  0 siblings, 1 reply; 6+ messages in thread
From: Zhu Zihao @ 2020-11-02  5:41 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: 44328

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

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@gnus.org> wrote:
>Zhu Zihao <all_but_last@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

[-- Attachment #2: Type: text/html, Size: 1919 bytes --]

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

* bug#44328: 27.1; [PATCH] Add expand-abbrev-maybe condition key binding
  2020-11-02  5:41   ` bug#44328: " Zhu Zihao
@ 2020-11-02 15:30     ` Lars Ingebrigtsen
  2020-11-03  4:40       ` Zhu Zihao
  0 siblings, 1 reply; 6+ messages in thread
From: Lars Ingebrigtsen @ 2020-11-02 15:30 UTC (permalink / raw)
  To: Zhu Zihao; +Cc: 44328

"Zhu Zihao" <all_but_last@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





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

* bug#44328: 27.1; [PATCH] Add expand-abbrev-maybe condition key binding
  2020-11-02 15:30     ` Lars Ingebrigtsen
@ 2020-11-03  4:40       ` Zhu Zihao
  2021-05-13 11:25         ` Lars Ingebrigtsen
  0 siblings, 1 reply; 6+ messages in thread
From: Zhu Zihao @ 2020-11-03  4:40 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: 44328

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


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@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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 515 bytes --]

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

* bug#44328: 27.1; [PATCH] Add expand-abbrev-maybe condition key binding
  2020-11-03  4:40       ` Zhu Zihao
@ 2021-05-13 11:25         ` Lars Ingebrigtsen
  0 siblings, 0 replies; 6+ messages in thread
From: Lars Ingebrigtsen @ 2021-05-13 11:25 UTC (permalink / raw)
  To: Zhu Zihao; +Cc: 44328

Zhu Zihao <all_but_last@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





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

end of thread, other threads:[~2021-05-13 11:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-30 14:32 bug#44328: 27.1; [PATCH] Add expand-abbrev-maybe condition key binding Zhu Zihao
2020-11-01 14:25 ` Lars Ingebrigtsen
2020-11-02  5:41   ` bug#44328: " Zhu Zihao
2020-11-02 15:30     ` Lars Ingebrigtsen
2020-11-03  4:40       ` Zhu Zihao
2021-05-13 11:25         ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).