jwiegley / use-package

A use-package declaration for simplifying your .emacs
https://jwiegley.github.io/use-package
GNU General Public License v3.0
4.42k stars 260 forks source link

Improve `bind-keymap` lazy-loading for better interaction with `which-key/describe-personal-keybindings` #876

Closed minad closed 1 year ago

minad commented 4 years ago

If the following configuration is used, the projectile-command-map is loaded lazily.

(use-package projectile
 :bind-keymap ("C-x p" . projectile-command-map))

Unfortunately this interacts badly with the which-key view, which shows p -> lambda on the first key press of C-x. On the second keypress of C-x, which-key shows p -> +prefix.

This can be worked around by using a different lazy loading scheme, as follows:

(use-package projectile
  :bind ("C-x p" . projectile-command-map-lazy)
  :config
  (defun projectile-command-map-lazy ()
    (interactive)
    (bind-key "C-x p" #'projectile-command-map projectile-mode-map)
    (setq unread-command-events
          (mapcar (lambda (ev) (cons t ev))
                  (listify-key-sequence (kbd "C-x p")))))

Then which-key shows p -> projectile-command-map-lazy on the first key press of C-x. On the second keypress of C-x, it shows p -> +projectile-command-map as if it wouldn't have been loaded lazily.

This means the code which is being generated for :bind-keymap can be improved. I am reporting this for now as a reminder also to myself. If there is interest (@jwiegley), I can try to work on a patch for use-package-bind-key.el.

Related issues:

EDIT: This issue also affects describe-personal-keybindings which shows #<keymap> and #<lambda> due to the lazy loading.

jwiegley commented 4 years ago

Sounds like exactly the sort of Elisp magic that use-package was created to abstract!

minad commented 1 year ago

It seems autoloading keymaps is also possible directly, see for example https://github.com/protesilaos/tmr/blob/46048fa826b3e7ffcf56c09949ac2f6d52ae5d6b/tmr.el#L607-L608