reddit / emacs

How to override doom keybinding in just one mode?

jmite · 2022-11-26 04:16
I asked this on the Doom discord but it didn't get much attention there. I'm wondering, is there a way to map a key to the leader map (not the local leader map), only in a specific mode? Basically, I want to customize what "SPC c c" does per mode, since usually I want to run an elisp command, not a command line program. So in LaTeX, I want "SPC c c" to do `#'TeX-command-run-all`, but not in other modes. Is there a way to do this? I tried the following but it didn't seem to work ``` (map! :after LaTeX :map LaTeX-mode-map :leader "c c" #'TeX-command-run-all) ```
prolog_junior · 2022-11-26 06:48
You can define a handler function that’ll query the buffer major mode & execute a function depending on that. Otherwise take a look at how doom sec does the SPC-m bindings as those change per major mode.
organic-bookworm · 2022-11-26 07:59
Here's a bit of an indirect answer. You could use command remapping. (map! :after LaTeX :map LaTeX-mode-map [remap compile] #'TeX-command-run-all) **Any** binding to `compile` command in **any** active keymap (including leader maps) will call `TeX-command-run-all` command, when `LaTeX-mode-map` is active.
jmite · 2022-11-26 10:41
Very cool, this seems like exactly what I want. Could you say a bit more about what's going on here? What is remap doing? Is "compile" a symbol internal to Doom? I suspect there's other things that would be handy to remap mode by mode.
organic-bookworm · 2022-11-26 13:04
See [Remapping Commands](https://www.gnu.org/software/emacs/manual/html_node/elisp/Remapping-Commands.html). You wanted to bind `SPC c c` keysequence to `TeX-command-run-all` command in `LaTeX-mode`. Doom binds `SPC c c` to `compile` command in `doom-leader-code-map` (use e.g. `M-x describe-key-briefly RET SPC c c` / `SPC h c SPC c c` to find out). The `[remap compile]` is a [**vector** representation of a special **key sequence**](https://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Sequences.html), which effectively refers to *all* key sequences bound to `compile` command. When buffer enters `LaTeX-mode`, the `LaTeX-mode-map` becomes active, and so the remapping, redirecting bindings to `compile` (e.g. `SPC c c` in `doom-leader-code-map`) to `TeX-command-run-all` command. This only affects keybindings. Direct calls (e.g. `M-x compile`) won't be remapped.
jmite · 2022-11-26 15:34
Very cool, thanks!