justbur / emacs-which-key

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

which-key-add-keymap-based-replacements with key sequences #275

Closed JAremko closed 3 years ago

JAremko commented 3 years ago

It would be cool if which-key-add-keymap-based-replacements worked for key sequences, especially major mode specific ones since which-key stores the replacements in the single global variable and Spacemacs has a lot of them.

This actually works(surprisingly) but only visually: (which-key-add-keymap-based-replacements emacs-lisp-mode-map "SPC mt" "test")

The problem is that it collides with "SPC" (or any other leading kbd) in Evil mode and prevents you from typing. And adding it "the usual way" results in bad prefix error.

Maybe it ca be fixed by adding unused key code that will serve as a root for all fake which-key keys in the map so it won't collide with anything user defined?

JAremko commented 3 years ago

For example (define-key (current-global-map) [123456789] '(WHAT . (IS . (GOING . ON)))) binds fine. This also works (message "%s" (lookup-key (current-global-map) [123456789]))

So can which-key choose a large random number and store key sequences under it?

justbur commented 3 years ago

The problem is that it collides with "SPC" (or any other leading kbd) in Evil mode and prevents you from typing. And adding it "the usual way" results in bad prefix error.

That shouldn't happen I don't think. Can you give me a small non-spacemacs example to test?

JAremko commented 3 years ago

Yeah it was my mistake. Sorry.

Btw it works fine for evil-leader

(setq inhibit-startup-screen t)
(require 'package)
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/"))
(package-initialize)
(package-refresh-contents)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun testfun ()
  (interactive)
  (message "success!"))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(unless (package-installed-p 'evil)
  (package-install 'evil)
  (package-install 'evil-leader))

(unless (package-installed-p 'evil-leader)
  (package-install 'evil-leader))

(unless (package-installed-p 'which-key)
  (package-install 'which-key))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'which-key)
(which-key-mode)
(which-key-setup-side-window-bottom)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'evil)
(evil-mode 1)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'evil-leader)
(global-evil-leader-mode)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(evil-leader/set-leader "SPC")
(evil-leader/set-key-for-mode 'emacs-lisp-mode "a b c" 'testfun)

(which-key-add-keymap-based-replacements
  evil-leader--default-map "a b" '("replacement"))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(emacs-lisp-mode)

Too bad making it mode specific would require digging into evil-leader--mode-maps - not related to Spacemacs, it uses different system.

justbur commented 3 years ago

Should be possible outside evil-leader too. What you’re asking for is a version that is aware of evil keymaps, meaning it understands how evil binds to a mode in a keymap.

On Thu, Dec 17, 2020 at 8:28 PM Eugene Yaremenko notifications@github.com wrote:

Yeah it was my mistake. Sorry.

Btw it works fine for evil-leader

(setq inhibit-startup-screen t) (require 'package) (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/")) (package-initialize) (package-refresh-contents) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun testfun () (interactive) (message "success!")) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (unless (package-installed-p 'evil) (package-install 'evil) (package-install 'evil-leader))

(unless (package-installed-p 'evil-leader) (package-install 'evil-leader))

(unless (package-installed-p 'which-key) (package-install 'which-key)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (require 'which-key) (which-key-mode) (which-key-setup-side-window-bottom) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (require 'evil) (evil-mode 1) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (require 'evil-leader) (global-evil-leader-mode) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (evil-leader/set-leader "SPC") (evil-leader/set-key-for-mode 'emacs-lisp-mode "a b c" 'testfun)

(which-key-add-keymap-based-replacements evil-leader--default-map "a b" '("replacement")) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (emacs-lisp-mode)

Too bad making it mode specific would require digging into evil-leader--mode-maps

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/justbur/emacs-which-key/issues/275#issuecomment-747809385, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQ3E7VZZPOJYR45CPZIHYTSVKV4VANCNFSM4UND577A .

JAremko commented 3 years ago

@justbur it's simpler to make it work for Spacemacs since it has a defvar for every mode map. But evil-leader can use some kind of integration yeah. It's not that straightforward.