Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buffer-locally overriding minor-mode key bindings in Emacs

Tags:

emacs

I want to use a minor mode which rebinds a major-mode key that I definitely want to keep. How can I rebind the key without deleting it from the minor-mode map globally? I know I can use define-key for that, but I would like to keep the binding for other buffers/major modes.

Can anyone help?

like image 683
sebhofer Avatar asked Oct 27 '12 17:10

sebhofer


People also ask

What is Emacs minor mode?

A minor mode is an optional editing mode that alters the behavior of Emacs in some well-defined way. Unlike major modes, any number of minor modes can be in effect at any time. Some minor modes are buffer-local, and can be turned on (enabled) in certain buffers and off (disabled) in others.

How do I create a minor mode in Emacs?

Creating a new minor mode is really easy, it's just a matter of understanding Emacs' conventions. Mode names should end in -mode and the command for toggling the mode should be the same name. They keymap for the mode should be called mode -map and the mode's toggle hook should be called mode -hook .


1 Answers

It's a bit cumbersome to do. You can do something like:

(add-hook '<major-mode>-hook
  (lambda ()
    (let ((oldmap (cdr (assoc '<minor-mode> minor-mode-map-alist)))
          (newmap (make-sparse-keymap)))
      (set-keymap-parent newmap oldmap)
      (define-key newmap [<thekeyIwanttohide>] nil)
      (make-local-variable 'minor-mode-overriding-map-alist)
      (push `(<minor-mode> . ,newmap) minor-mode-overriding-map-alist))))
like image 157
Stefan Avatar answered Oct 19 '22 19:10

Stefan