all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* I'd like to advise a /keybinding/, how do I do it?
@ 2021-04-28  5:25 Marcin Borkowski
  2021-04-28  7:08 ` Omar Polo
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Marcin Borkowski @ 2021-04-28  5:25 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hello everyone,

what I'd like to do is to "advise a keybinding", IOW, advise whatever
command a given key is bound to.  How do I get the name of the command
given a key, and taking into account the current major mode, minor modes
etc.?

I tried to look at the source code of `describe-key-briefly', but it is
pretty dense, and maybe there's a better way than diving into that
rabbit hole.

The rationale is that I'm thinking about writing a minor mode which
could prevent a "finishing" action until the buffer is ready.
A "finishing" action is often bound to C-c C-c (sending an email and
commiting something to Git come to mind), so I'd like to advise whatever
C-c C-c calls to check the buffer for occurrences of a string like TODO.

Of course, a more robust way would be to have a list of various major
modes and their "finishing" commands, but I think the idea with just
looking up C-c C-c is cute (even if not very robust), and I was
wondering if it can be done in a simple way, even as a toy.

Any ideas?

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: I'd like to advise a /keybinding/, how do I do it?
  2021-04-28  5:25 I'd like to advise a /keybinding/, how do I do it? Marcin Borkowski
@ 2021-04-28  7:08 ` Omar Polo
  2021-04-28  9:21   ` Philip Kaludercic
  2021-04-28 11:44 ` Eli Zaretskii
  2021-04-28 14:33 ` Stefan Monnier
  2 siblings, 1 reply; 6+ messages in thread
From: Omar Polo @ 2021-04-28  7:08 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: help-gnu-emacs


Marcin Borkowski <mbork@mbork.pl> writes:

> Hello everyone,
>
> what I'd like to do is to "advise a keybinding", IOW, advise whatever
> command a given key is bound to.  How do I get the name of the command
> given a key, and taking into account the current major mode, minor modes
> etc.?
>
> I tried to look at the source code of `describe-key-briefly', but it is
> pretty dense, and maybe there's a better way than diving into that
> rabbit hole.
>
> The rationale is that I'm thinking about writing a minor mode which
> could prevent a "finishing" action until the buffer is ready.
> A "finishing" action is often bound to C-c C-c (sending an email and
> commiting something to Git come to mind), so I'd like to advise whatever
> C-c C-c calls to check the buffer for occurrences of a string like TODO.
>
> Of course, a more robust way would be to have a list of various major
> modes and their "finishing" commands, but I think the idea with just
> looking up C-c C-c is cute (even if not very robust), and I was
> wondering if it can be done in a simple way, even as a toy.
>
> Any ideas?

Probably post-command-hook?  I don't know how to read the current
keybinding, but if it's possible then you can run code in the
post-command-hook and check there

HTH



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

* Re: I'd like to advise a /keybinding/, how do I do it?
  2021-04-28  7:08 ` Omar Polo
@ 2021-04-28  9:21   ` Philip Kaludercic
  0 siblings, 0 replies; 6+ messages in thread
From: Philip Kaludercic @ 2021-04-28  9:21 UTC (permalink / raw)
  To: Omar Polo; +Cc: help-gnu-emacs

Omar Polo <op@omarpolo.com> writes:

> Marcin Borkowski <mbork@mbork.pl> writes:
>
>> Hello everyone,
>>
>> what I'd like to do is to "advise a keybinding", IOW, advise whatever
>> command a given key is bound to.  How do I get the name of the command
>> given a key, and taking into account the current major mode, minor modes
>> etc.?
>>
>> I tried to look at the source code of `describe-key-briefly', but it is
>> pretty dense, and maybe there's a better way than diving into that
>> rabbit hole.
>>
>> The rationale is that I'm thinking about writing a minor mode which
>> could prevent a "finishing" action until the buffer is ready.
>> A "finishing" action is often bound to C-c C-c (sending an email and
>> commiting something to Git come to mind), so I'd like to advise whatever
>> C-c C-c calls to check the buffer for occurrences of a string like TODO.
>>
>> Of course, a more robust way would be to have a list of various major
>> modes and their "finishing" commands, but I think the idea with just
>> looking up C-c C-c is cute (even if not very robust), and I was
>> wondering if it can be done in a simple way, even as a toy.
>>
>> Any ideas?
>
> Probably post-command-hook?  I don't know how to read the current
> keybinding, but if it's possible then you can run code in the
> post-command-hook and check there

Do you mean something like (this-command-keys)? But yes, an advice
system could be built that checks an alist or hash-table in
pre/post-command-hook on what to execute.

> HTH
>
>

-- 
	Philip K.



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

* Re: I'd like to advise a /keybinding/, how do I do it?
  2021-04-28  5:25 I'd like to advise a /keybinding/, how do I do it? Marcin Borkowski
  2021-04-28  7:08 ` Omar Polo
@ 2021-04-28 11:44 ` Eli Zaretskii
  2021-04-28 14:33 ` Stefan Monnier
  2 siblings, 0 replies; 6+ messages in thread
From: Eli Zaretskii @ 2021-04-28 11:44 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Marcin Borkowski <mbork@mbork.pl>
> Date: Wed, 28 Apr 2021 07:25:14 +0200
> 
> what I'd like to do is to "advise a keybinding", IOW, advise whatever
> command a given key is bound to.  How do I get the name of the command
> given a key, and taking into account the current major mode, minor modes
> etc.?

Is key-binding (the function) what you want?



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

* Re: I'd like to advise a /keybinding/, how do I do it?
  2021-04-28  5:25 I'd like to advise a /keybinding/, how do I do it? Marcin Borkowski
  2021-04-28  7:08 ` Omar Polo
  2021-04-28 11:44 ` Eli Zaretskii
@ 2021-04-28 14:33 ` Stefan Monnier
  2021-04-29  0:29   ` Bob Newell
  2 siblings, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2021-04-28 14:33 UTC (permalink / raw)
  To: help-gnu-emacs

> The rationale is that I'm thinking about writing a minor mode which
> could prevent a "finishing" action until the buffer is ready.
> A "finishing" action is often bound to C-c C-c (sending an email and
> commiting something to Git come to mind), so I'd like to advise whatever
> C-c C-c calls to check the buffer for occurrences of a string like TODO.

"advising the key-binding" would imply changing by side-effect the
underlying major mode's keymap (BTW: yes, it can be done, by defining
`lookup-key` as an lvalue (e.g. with gv-define-setter) after which you
can use `add-function` to modify a key binding).

Or you could do it by advising the command bound to the particular key
(in which case it'll also affect invocations of that command that don't
go through this key binding, e.g. via `M-x`).

Or you could do it with a minor-mode map (i.e. without modifying any
major mode keymap or command), where you bind `C-c C-c` to a command
that either beeps or delegates to the command that it hid (e.g. which
you could find for example with (let ((my-new-minor-mode nil))
(key-binding (this-command-keys))))

Or you could do it with a `pre-command-hook` which checks
`this-command-keys`.

Or the minor-mode map could use a "filtered menu-item" conditional
key-binding.

Or ...


        Stefan




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

* Re: I'd like to advise a /keybinding/, how do I do it?
  2021-04-28 14:33 ` Stefan Monnier
@ 2021-04-29  0:29   ` Bob Newell
  0 siblings, 0 replies; 6+ messages in thread
From: Bob Newell @ 2021-04-29  0:29 UTC (permalink / raw)
  To: help-gnu-emacs


>> A "finishing" action is often bound to C-c C-c (sending an email and
>> commiting something to Git come to mind), so I'd like to advise whatever
>> C-c C-c calls to check the buffer for occurrences of a string like TODO.

> Or you could do it by advising the command bound to the particular key

I've done this when I want the underlying command to be
affected in all cases. For instance I don't wish send out an
email until I've verified that I have a network connection, so
I advise "message-send" and "message-send-and-exit" rather
than the associated keystrokes.

But be at least a little wary. Sometimes the keystrokes fool
you about the underlying function. In my Gnus reply buffer
right now, C-h k on C-c C-c shows 'orgtbl-ctrl-c-ctrl-c'.

> Or you could do it with a minor-mode map (i.e. without modifying any

I've done this on several occasions when I don't want to mess
with the use of a command outside of a particular minor
mode. That's a less common case, I think, but still very
useful for certain things.

-- 
Bob Newell
Honolulu, Hawai`i

- Via GNU/Linux/Emacs/Gnus/BBDB



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

end of thread, other threads:[~2021-04-29  0:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-04-28  5:25 I'd like to advise a /keybinding/, how do I do it? Marcin Borkowski
2021-04-28  7:08 ` Omar Polo
2021-04-28  9:21   ` Philip Kaludercic
2021-04-28 11:44 ` Eli Zaretskii
2021-04-28 14:33 ` Stefan Monnier
2021-04-29  0:29   ` Bob Newell

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.