Subject: bug#60530: 28.2; Add tools to find keymaps that bind a given command

From: Ruijie Yu <[email-masked]>
Date: Mon, 20 Feb 2023 15:07:09 +0800

Sean Devlin <[email-masked]> writes:

> Hi folks,
>
> There is a third-party package "helpful"
> (https://github.com/Wilfred/helpful) that provides (in their words) "an
> alternative to the built-in Emacs help that provides much more
> contextual information".
>
> One nice feature their describe-command replacement provides is a list
> of bindings for the given command for all known keymaps. For example,
> for find-file it might generate a list like the following:
>
> ctl-x-map C-f
> embark-become-file+buffer-map f
> embark-file-map RET
> embark-file-map f
> global-map <open>
> global-map C-x C-f
> term-raw-map C-c C-f
>
> [...]
>
> It's nice to know exactly which keymaps contain a binding. It gives you
> a better idea of where best to tweak a configuration, and it can give
> you new ideas about how and where a command might be used.

I saw this message long ago, but recently I came across a situation
where I wanted to know if a command was bound in *some* keymap whose
name I didn't know, and this would be quite a helpful feature for me
personally (pun intended).

> I looked in helpful.el, and they use some non-trivial logic to generate
> their list. I think this functionality should come built-in with Emacs.

Looking in their source, I think (with some handwaving) the logic boils
down to iterating over all keymap objects inside `obarray', and checking
whether the command of interest is actively bound (that is, not
shadowed, etc) in each keymap.  Some other things are happening inside
helpful.el as well, probably regarding other components in the helpful
buffer.

> I think Emacs should provide low-level functions to generate a list of
> bindings similar to the above and a command to describe the bindings. It
> might also be nice to expose the information in describe-command like
> helpful.el does.

If there is interest, maybe I can take a look inside help-fns.el and see
if I can figure out how to add new contents in the *Help* buffer, and
then try to come up with a patchset, with the goal of (correctly and)
concisely implement command-keymap lookups without depending on dash and
f.  This might take a while, though, as some other things of mine are
prioritized over coding ATM.

In addition, this feature probably needs to be guarded behind a boolean
defcustom flag so that people who don't want this addition don't pay for
the cost.

Thoughts?

--
Best,

RY