If I have a key binding I want to apply to multiple keymaps, for example.

(evil-define-key 'visual c-mode-map (kbd "C-M-?") 'c-comment-from-c++)
(evil-define-key 'visual c++-mode-map (kbd "C-M-?") 'c-comment-from-c++)
(evil-define-key 'visual glsl-mode-map (kbd "C-M-?") 'c-comment-from-c++)

I tried putting the keymaps into a list and calling evil-define-key with each but it didn't work.


(dolist (this-map (list c-mode-map c++-mode-map glsl-mode-map))
  (evil-define-key 'visual this-map (kbd "C-M-?") 'c-comment-from-c++))

Is this possible?


(You don't say what you mean by "it didn't work". Error?)

I don't use Evil, and I don't have its source code. So this probably won't help. Anyway...

Googling a bit, this doc page says that evil-define-key is a macro, not a function, which means it doesn't necessarily evaluate all of its arguments, and it returns Lisp code (the macro expansion), which then gets evaluated.

Even though it's a macro, that doc page indicates that it does evaluate all of its args ("The remaining arguments [besides STATE] are like those of define-key", which is a function), so it's not too clear why it's a macro. Presumably it does something else. (Or maybe that statement is just false.)

On the other hand, that page also kinda suggests that the KEYMAP argument is not evaluated, because it says "Note that foo-mode-map is unquoted, and that this form is safe before foo-mode-map is loaded.". So to me at least, that doc is not too clear.

A guess is that KEYMAP is not evaluated, which would mean that evil-define-key expects a keymap variable, that is, a symbol, for KEYMAP. That would explain why your code wouldn't work: because you're passing the values of those keymap variables, not the variables (symbols) themselves, to evil-define-key.

(Note that define-key does not accept a keymap variable as arg; it accepts only a keymap, that is, the value of a keymap variable, as arg.)

If this guess is correct, you could try quoting the keymap symbols: instead of (list c-mode-map c++-mode-map glsl-mode-map), try '(c-mode-map c++-mode-map glsl-mode-map).

Another thing that suggests this interpretation is the doc string mention that you can pass a quoted symbol instead of a keymap or keymap symbol, in which case the quoted symbol indicates a mode. This is no proof, but it does suggest that the arg isn't evaluated first.

Just a wild guess. No doubt some Evil user or someone with the definition of evil-define-key will give you a good answer.

(This answer might also help a bit. Dunno.)

OK, I found the source code and took a very quick peek at the definition of macro evil-define-key.

It seems you can pass either an (unquoted) keymap variable (symbol) or its value, and that evaluation is delayed in either case. My quick peek didn't tell me just what the problem is with your code, however...

| improve this answer | |

In this kind of situation it's useful to use the function equivalent of whatever macro you're wrestling with, evil-define-key*. Note that this evaluates the keymap argument instead of deferring it to a later point, so I've adjusted your code to be run after the maps have been defined for c-mode and c++-mode:

(with-eval-after-load 'cc-mode
  (dolist (this-map (list c-mode-map c++-mode-map))
    (evil-define-key* 'visual this-map (kbd "C-M-?") 'c-comment-from-c++)))
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.