reddit / emacs

Is there a way to present a warning message when a key combination is redefined? So I have some kind of heads up that a conflict occured?

Status-Detective-783 · 2022-03-01 03:20
I'm newbie, and don't understand much lisp yet, but I took a lisp course 20 years ago. I'm not sure how all this key mapping works, but I was just curious if there was a way (or a package) that could do this. I might want to define some keys for a purpose, but it's also possible that some future package I install could overwrite my definition, right? [EDIT: Or one package clobbering another package.]
xtifr · 2022-03-01 03:39
From the Emacs manual, from the chapter on customizing keybindings: "Since most modes define their own key bindings, activating a mode might override your custom key bindings. A small number of keys are reserved for user-defined bindings, and should not be used by modes, so key bindings using those keys are safer in this regard. The reserved key sequences are those consisting of ‘C-c’ followed by a letter (either upper or lower case), and function keys <F5> through <F9> without modifiers."
washy99999 · 2022-03-01 07:35
org mode seems to use a few C-c sequences
0lMon · 2022-03-01 08:10
I have to say with the large amount of packages, I got problems with my keybindings. I tried to change Capslock to Hyper-key and use "H-" as my personal prefix. This works great unless I am in a terminal. I remember to got it working, but it was more of a hack. Then I saw someone mention "C-z" as prefix, because I never suspend or minimize emacs from inside emacs. So I use "C-z" as prefix for personal keybinding. This works without trouble and is close to "C-x" and "C-c". To directly "answer" the OPs question (it is more of a follow up question from me), it should be possible to advice `define-key` and check if a key is already in a keymap, right?
SlowValue · 2022-03-01 08:31
> Is there a way to present a warning message when a key combination is redefined? I can imagine an `:before` `advice` to `define-key` and `global-set-key` (and other functions maybe), which checks for similar keybindings in (all) other maps and logs an message. But one would get many false positives from this approach, because not all maps are active all the time, and maps get defined even tough never be active. Or one could check all maps of `(current-active-maps)` for duplicate keybindings, periodically or on mode switch. This idea surely needs some refinement. ;)
bhrgunatha · 2022-03-01 08:35
Lots of modes use `C-c C-<key>` too! That's why I chose `C-,` (and I will extend that to `C-.` when I need to) since they are also supposed to be available. In fact they are *virtually* unused except... funnily enough `org-mode` binds `C-,` so I have to unbind `C-,` from `org-mode-map` too \*sigh\*
h-m-b-tu · 2022-03-01 08:43
Check the command `key-binding`.
_viz_ · 2022-03-01 09:05
If I had a dollar every time one misinterprets that text from the manual, I believe I might be as rich as Ambani. Only C-c followed by LETTERS are reserved for users. So major and minor modes can use C-c C-letter all they want. And in particular, org mode abides by this rule excellently.
bhrgunatha · 2022-03-01 09:09
Perhaps I should have been clearer, sorry. My point wasn't that they shouldn't be using `C-C C-<key>` but that **because they do** I chose something other then `C-c` because it's more limited.
SlowValue · 2022-03-01 09:13
Documentation `key-binding`: > Return the binding for command KEY in current keymaps. could you elaborate what you intend with mentioning `key-binding`?
h-m-b-tu · 2022-03-01 09:56
With it you do not have to loop over `(current-active-maps)` since it returns the command which gets executed by the given KEY, like with `C-h k`.
tmting · 2022-03-01 09:57
A bit unrelated to your question, but if you are having trouble with keybindings I really recommend [this read](https://www.masteringemacs.org/article/mastering-key-bindings-emacs). It explains bit by bit about how emacs keybindings works and how to set them correctly. Also, if you use evil-mode, reading [evil-guide](https://github.com/noctuid/evil-guide) is really worth it as well, to understand how to configure things correctly.
Status-Detective-783 · 2022-03-01 13:12
Thanks!. I now see that in order to really answer my question, I'll probably need to dig into https://www.masteringemacs.org/article/mastering-key-bindings-emacs and dig into the lisp code, and probably look at some of the already existing packages that manage keymaps. What I'm looking for is some kind of "root" function for "binding keys" and can you... I'm very new to all this... can you somehow hook into that so you have priority to see whenever it's used (and you would then dispatch to the real `key-binding` to do it's job)? If so, then I could write my own hook into it, and then check to see if it's already in use whenever called by anyone. And if so, issue warnings, etc. I think it's just a global datastructure that anyone can change in any way they want. Or in other words, emacs doesn't use API's. So I kind of doubt that exists. Another strategy would be to take a snapshot of all keymaps, then require a package, then compare to the snapshot to see what's changed and go from there. This is all actually very advanced from where I'm at, and supposes I even know how to code in elisp. So it's mostly a curiosity as I dig into emacs general usage and mechanisms, and begin a lisp journey, to better understand emacs, layer by layer by layer!
Status-Detective-783 · 2022-03-01 13:23
That's VERY related to my question, thanks! I had read that article a little bit before, but I think it's actually precisely what I needed!
jimd0810 · 2022-03-02 00:48
I built a little package a while back that does this. Hopefully it could help :) https://github.com/JimDBh/ironkey
Status-Detective-783 · 2022-03-02 02:45
Cool! Thanks!
SlowValue · 2022-03-03 15:04
I had a different approach in mind. I wanted to test for any shadowed keybindings while a certain keymap stack is active. This check should test all keybindings in all current active keymaps. This check should run periodically and not on `define-key` calls. This approach because: when a `define-key` happens the keymap stack is not necessarily active.
h-m-b-tu · 2022-03-04 08:14
Then maybe `describe-key` can be of help since in the resulting help buffer it is displayed if the command is shadowed by another minor mode.
SlowValue · 2022-03-05 03:35
this sounds like the user has to do this manually
h-m-b-tu · 2022-03-06 09:36
I meant "take a look at the source of `describe-key` maybe that is of help to you since it has some of the functionality you described".