Reputation: 2091
I have my own user defined key-bindings as described here: Globally override key binding in Emacs
Whenever I load a new major mode, say OrgMode, I have some of my bindings over-written to fit my needs in that specific mode. But then when I load another major mode, which have its own over-writes, they stay put even if I'm not in a buffer with that major mode anymore.
For example
(define-key custom-keys-mode-map (kbd "C-p") 'some-cool-function)
(add-hook 'org-mode-hook
(lambda ()
(define-key custom-keys-mode-map (kbd "C-p") 'org-cool-function )))
(add-hook 'sunrise-mode-hook
(lambda ()
(define-key custom-keys-mode-map (kbd "C-p") 'sunrise-cool-function )))
At first I use C-p to execute my cool, default, function. After I load Org-Mode I use C-p to execute "org-cool-function", and when I load Sunrise-Commander, C-p executes "sunrise-cool-function".
But then when I go back to an Org-Mode file, C-p still tries to execute "sunrise-cool-function" instead of "org-cool-function".
Hope I'm clear:)
Upvotes: 1
Views: 625
Reputation: 2091
I've read a couple more threads which led me to believe there is another way, which I'm currently using:.
Currently I'm using this:
;;; Disable Custom keys for specific major modes.
(define-global-minor-mode my-custom-keys-mode custom-keys-mode
(lambda ()
(when (not (memq major-mode (list 'sr-mode)))
(custom-keys-mode))))
(my-custom-keys-mode 1)
which pretty much suits my needs at the moment.
Upvotes: 0
Reputation: 73345
Minor mode maps supersede major mode maps which supersede the global map.
So a few options are:
Do not include these bindings in your custom minor mode map at all. Make your preferred binding a plain global binding, and let the major modes over-ride it as required.
Create additional minor modes to take precedence over your existing minor mode, and enable them in the corresponding major modes. Each minor mode's position in minor-mode-map-alist
determines precedence when looking up key bindings, so you need these additional modes to be defined after your current mode is defined (meaning that they will appear earlier in the alist), and of course keep them in that order if updating that list dynamically.
Leave everything the way it is, but bind these keys to custom functions which check the major mode and act accordingly. You may or may not need to take care of passing on prefix arguments if you take this approach.
Upvotes: 3
Reputation: 9262
The behavior you see is what is expected from the code. It goes as follows:
some-cool-function
gets assignedorg-cool-function
gets assignedsunrise-commander
sunrise-cool-function
gets assignedYour problem comes from trying to set a global property from a local event.
You should use the org-mode-map
instead of custom-keys-mode-map
to put your C-p
keybinding. This will set it once and for all for every buffer with org content:
(define-key custom-keys-mode-map (kbd "C-p") 'some-cool-function)
(eval-after-load "org"
'(define-key org-mode-map (kbd "C-p") 'org-cool-function))
Upvotes: 6