I have a minor mode. If that mode is active and the user hits DEL, I
want to do some action, but only if some condition holds. If the
condition holds and the action is executed I want to do nothing more
after that. But if the condition fails, I don’t want to do anything
and let the default DEL action execute.
Not sure how I could solve this. But I guess I could do it in two ways:
1)
I could rebind the DEL key to a function in the minor mode and then
check if the conditions holds ot not. But then how do I know what the
default command to DEL is?
2)
I could add a pre command hook like this. Execute the command and then
break the chain. But how do I break the chain?
(add-hook 'pre-command-hook
(lambda()
(when (equal last-input-event 'backspace)
;; Do something and then stop (do not execute the
;; command that backspace is bound to)
)))
In what way would you solve it? Thanks!
The way to do this is to temporarily disable your minor mode, then look up the key binding.
Pretend that you’ve bound
'do-thingyto DEL. Then this would do the trick (assuming the condition you want to trigger off is(equal last-input-event 'backspace):Note: This behavior assumes you have set up your key bindings the standard way a minor-mode would. Specifically, you should add your keymap to the variable
minor-mode-map-alistby adding an element(my-minor-mode . my-minor-mode-keymap). That’s how the aboveletstatement works, it looks up the binding you want with your mode temporarily disabled.If you use
define-minor-modeto define your minor mode, the keymap gets set up the “right way” automatically.