emacs-which-key
emacs-which-key copied to clipboard
named prefix maps not possible?
I'm trying to give a descriptive name to a prefix map, and having problems doing it in any kind of elegant way. To describe it more easily, I'll explain the concrete behaviour I want ...
I have a keymap as-jump-map
full of bindings for jumping to files / buffers which I commonly need to access. I have this bound to both C-c j
and the key chord zj
(also jz
).
I want a sub-keymap as-jump-ruby-map
just for jumping to Ruby stuff. I have which-key-enable-extended-define-key
set to t
so that define-key
is advised, and this is already working fine in other circumstances. However if I do something like:
(defvar as-jump-ruby-map (make-sparse-keymap "Jump to Ruby")
"Adam's prefix keymap for quickly jumping to Ruby stuff")
(define-key as-jump-map "r" '("Ruby" . as-jump-ruby-map))
then which-key shows r -> +as-jump-ruby-map
rather than r -> Ruby
. I can partially fix this with
(which-key-add-key-based-replacements "C-c j r" "Ruby")
but then it doesn't work when as-jump-map
is triggered via the zj
keymap. Is there a way to do this cleanly I'm missing?
OK, it seems that
(push '((nil . "as-jump-ruby-map") . (nil . "Ruby")) which-key-replacement-alist)
works, although it would be nicer if the above worked out of the box.
Actually,
(define-key as-jump-map "r" '("Ruby" . as-jump-ruby-map))
is broken; it results in apply: Wrong type argument: commandp, as-jump-ruby-map
. In contrast this works:
(define-key as-jump-map "r" as-jump-ruby-map))
Is something wrong with which-key--process-define-key-args
when it's handling prefix keymaps?
Same problem here.
Actually, (define-key as-jump-map "r" '("Ruby" . as-jump-ruby-map)) is broken; it results in apply: Wrong type argument: commandp, as-jump-ruby-map. In contrast this works:
Your syntax is wrong. You need
(define-key as-jump-map "r" (cons "Ruby" as-jump-ruby-map))
to bind to the keymap and not the symbol as-jump-ruby-map
. In the latter case, emacs thinks you have a command named as-jump-ruby-map
, and you don't. If you did
(define-key as-jump-map "r" 'as-jump-ruby-map))
you would run into exactly the same problem.
By the way, there is another approach that will make your binding work. You can use define-prefix-command
as follows
(define-prefix-command 'test)
(define-key some-map "t" '("Test" . test))
The first line defines test
as a prefix map that can be used as a command for binding purposes.
By the way, there is another approach that will make your binding work. You can use
define-prefix-command
as follows(define-prefix-command 'test) (define-key some-map "t" '("Test" . test))
The first line defines
test
as a prefix map that can be used as a command for binding purposes.
I did exactly that, minimal example:
(setq rde-leader "C-SPC")
(global-set-key (kbd rde-leader) nil)
(define-prefix-command 'rde-global-map)
(global-set-key (kbd rde-leader) 'rde-global-map)
(define-key rde-global-map "b" '("Switch to buffer" . switch-to-buffer))
Now I press C-SPC
and see b -> switch-to-buffer
instead of b -> Switch to buffer
.
@minikN First, you're describing another issue. This issue is about naming prefix maps. Second, I just tried the steps you described and they work fine for me (I see "Switch to buffer" as expected).
@justbur Okay, for me it was the same issue, because it doesn't work with naming prefix maps (the same way) as well. It also doesn't work with using which-key-add-keymap-based-replacements
/ which-key-add-key-based-replacements
. The only thing that works is using
(push '((nil . "as-jump-ruby-map") . (nil . "Ruby")) which-key-replacement-alist)
Where should I start looking?
@minikN I'd say make sure which-key is up to date first. Try again, and if it doesn't work then post a config that reproduces the problem from a minimal setup (emacs -Q
if possible).
(define-prefix-command 'test) (define-key some-map "t" '("Test" . test))
I tested this and it had exactly the same problem as the original report:
(define-prefix-command 'as-jump-ruby)
(define-key as-jump-map "r" '("Ruby" . as-jump-ruby))
results in:
So I wrote a helper to hide my ugly hack behind something a bit nicer: https://github.com/aspiers/emacs/commit/fdea08f7f9041cc83b8435905d9c408ce5023ed2