Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

named prefix maps not possible? #253

Open
aspiers opened this issue Jul 4, 2020 · 11 comments
Open

named prefix maps not possible? #253

aspiers opened this issue Jul 4, 2020 · 11 comments

Comments

@aspiers
Copy link

aspiers commented Jul 4, 2020

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?

@aspiers
Copy link
Author

aspiers commented Jul 4, 2020

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.

@aspiers
Copy link
Author

aspiers commented Jul 4, 2020

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?

@minikN
Copy link

minikN commented Dec 5, 2021

Same problem here.

@justbur
Copy link
Owner

justbur commented Dec 5, 2021

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.

@justbur
Copy link
Owner

justbur commented Dec 5, 2021

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.

@minikN
Copy link

minikN commented Dec 5, 2021

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.

@justbur
Copy link
Owner

justbur commented Dec 5, 2021

@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).

@minikN
Copy link

minikN commented Dec 5, 2021

@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?

@justbur
Copy link
Owner

justbur commented Dec 5, 2021

@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).

@aspiers
Copy link
Author

aspiers commented Aug 11, 2022

(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:

image

@aspiers
Copy link
Author

aspiers commented Aug 11, 2022

So I wrote a helper to hide my ugly hack behind something a bit nicer: aspiers/emacs@fdea08f

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants