justbur / emacs-which-key

Emacs package that displays available keybindings in popup
GNU General Public License v3.0
1.73k stars 87 forks source link

Make "<prefix> C-h" display which key and prompt for command at the same time #351

Open antrmn opened 1 year ago

antrmn commented 1 year ago

Let's assume the following parameters:

(setopt which-key-show-early-on-C-h t 
             which-key-use-C-h-commands t)

If you call - for example - C-c C-h two different things may happen:

  1. The "which key" panel is displayed
  2. The "which key" panel is displayed and it prompts for a command in the which-key-C-h-map

The outcome may vary depending on whether the which-key panel is displayed before (case 2) or after (case 1) C-h is typed.

This inconsistency can become frustrating when one wants to quickly use different keybinding-help related commands (like describe-prefix-bindings, embark-prefix-help-commands or some custom defined command).

I think that introducing a choice to prompt for a command without hiding the which key panel would be really useful.

antrmn commented 1 year ago

Currently, I'm achieving this feature by means of the following code:

(use-package which-key
  :custom
  (which-key-mode t)
  (which-key-show-early-on-C-h 'prompt))

(defun my/which-key--before-C-h-dispatch ()
  (when (and (not (which-key--popup-showing-p))
             (eq which-key-show-early-on-C-h 'prompt))
    (which-key--create-buffer-and-show (kbd (which-key--current-key-string)))
    (setq unread-command-events (listify-key-sequence
                                 (kbd (which-key--current-key-string))))))
(advice-add 'which-key-C-h-dispatch :before #'my/which-key--before-C-h-dispatch)

Basically, if which-key-show-early-on-C-h is set with the 'prompt symbol, then a which-key window gets shown instantly after the invocation of which-key-C-h-dispatch and the same key sequence is fed again by means of the unread-command-events variable.

I'm showing this code just to give an idea of the feature. It seems to work but since the package does contain some timer based mechanisms i don't know if there are some gimmicks that I'm ignoring right now by displaying the which key windows instantly.

justbur commented 1 year ago

You're right that the behavior of C-h depends on whether the which-key buffer is showing when it's pressed, but I'm not sure of the purpose of your advice function. It appears to do the same thing as the first condition in which-key-C-h-dispatch as long as 'prompt is used.

The original purpose of using C-h to show the which-key buffer was to have a way to turn off the automatic display time and allow for it to be shown "on demand". If this is how it's used, the timing issue you mention is not a problem. In other words, this is not the intended setup for this option (see the docstring of which-key-show-early-on-C-h).

That being said, I may be able to help if you can describe more precisely what you are trying to achieve with this option.

antrmn commented 1 year ago

Thank you for your help. I'll try to describe my issue with some gifs, I hope to be clear.

I do use which-key with the default behaviour: its window automatically pops out 1 second after a prefix key.

I often would like to invoke describe-prefix-bindings command in order to get a more detailed view of the available commands under a specific keymap. This command is under which-key-C-h-map by default.

Here is what happens when I try to type C-x C-h a few times:

2023-07-13 16-00-46

I don't know why the which key window appears earlier than expected, but this leads the C-x C-h behaviour to be rather inconsistent: sometimes it shows the which-key window, sometimes it prompts for a command.

This is the bahaviour of C-x C-h with the patch:

2023-07-13 16-24-07

This way, describe-prefix-bindings can always be called with <prefixes> C-h C-h, regardless of whether the which-key window was already shown or not. This applies to any other command that I could add to the which-key-C-h-map.