1

I would like to override a key binding such that whenever the key combination is pressed a user-defined predicate function is evaluated and; if it returns non-nil, the function my-cmd is executed, otherwise default-cmd is executed (the one that would be executed if I pressed that key combination). In other words, something like this:

(define-key <SOME_MODE>-mode-map (kbd "C-c C-n")
  (lambda()
     (interactive)
     (if (my-predicate-function ...)
         (my-cmd)  ; execute my command
       (default-cmd))))  ; invoke the command that is CURRENTLY bound to C-c C-n

Requirements:

  • The command bound to the key (C-c C-n) depends on the current major (or even minor mode), so rebinding the command whenever the key is pressed is strongly encouraged
  • if possible, I would prefer the solution that work regardless of whether the configuration code (the solution to this problem) is executed before or after another piece of code that binds some command to the same key
Stefan
  • 27,908
  • 4
  • 53
  • 82
eold
  • 5,972
  • 11
  • 56
  • 75
  • Duplicate of [Conditionally change keybinding](http://stackoverflow.com/questions/16090517/elisp-conditionally-change-keybinding) and [Emacs key binding fallback](http://stackoverflow.com/questions/2494096/emacs-key-binding-fallback). – phils Apr 04 '14 at 01:06
  • Thanks, I don't know how I missed those... – eold Apr 05 '14 at 06:21

0 Answers0