1

Suppose we have 3 mode, one such rule is like key map for mode 1 should always come before mode 2, and mode 3 should always come before mode 1, finally mode 3 should always come before other minor mode. Is there a easy way to do it? How is the order determined in emacs? Thank you for your help.

2

For a minor mode MY-MINOR-MODE, you can make the bindings of that mode override ALL others (major and minor modes) by advising load as below:

(defadvice load (after give-my-keybindings-priority)
  "Try to ensure that my keybindings always have priority."
  (when (not (eq (car (car minor-mode-map-alist)) 'MY-MINOR-MODE))
      (let ((mykeys (assq 'MY-MINOR-MODE minor-mode-map-alist)))
        (assq-delete-all 'MY-MINOR-MODE minor-mode-map-alist)
        (add-to-list 'minor-mode-map-alist mykeys))))
(ad-activate 'load)

Source


So if you want MINOR-MODE-A at the highest priority followed by MINOR-MODE-B, followed by MINOR-MODE-C, you would do:

(defadvice load (after give-my-keybindings-priority)
  "Try to ensure that the keymap priorities are in the following order:
 MINOR-MODE-A > MINOR-MODE-B > MINOR-MODE-C."
  (when (not (eq (car (car minor-mode-map-alist)) 'MINOR-MODE-C))
    (let ((mykeys (assq 'MINOR-MODE-C minor-mode-map-alist)))
      (assq-delete-all 'MINOR-MODE-C minor-mode-map-alist)
      (add-to-list 'minor-mode-map-alist mykeys)))

  (when (not (eq (car (car minor-mode-map-alist)) 'MINOR-MODE-B))
    (let ((mykeys (assq 'MINOR-MODE-B minor-mode-map-alist)))
      (assq-delete-all 'MINOR-MODE-B minor-mode-map-alist)
      (add-to-list 'minor-mode-map-alist mykeys)))

  ;; `when' block for the highest priority minor mode put at the very last 
  (when (not (eq (car (car minor-mode-map-alist)) 'MINOR-MODE-A))
    (let ((mykeys (assq 'MINOR-MODE-A minor-mode-map-alist)))
      (assq-delete-all 'MINOR-MODE-A minor-mode-map-alist)
      (add-to-list 'minor-mode-map-alist mykeys))))
(ad-activate 'load)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.