I'm writing an extension to a major mode (coq-mode) that adds extra features, plus a few keybindings.

To makes it easy to disable or redefine these keybindings, I packaged them in a keymap. That keymap is tied to a myplugin-keybindings-minor-mode minor mode, which gets enabled in coq-mode-hook.

Most of these keybindings do not make sense outside of coq-mode. Yet when I open new buffers from a coq-mode buffer, myplugin-keybindings-minor-mode is enabled in the newly opened buffer.

How do I prevent this from happening? How do I tie my minor mode to coq-mode, so that myplugin-keybindings-minor-mode is never active when coq-mode isn't? Altenatively, which other approach should I use?


The minor mode is enabled by using (add-hook 'coq-mode-hook #'myplugin-initialize), and myplugin-initialize calls (myplugin-keybindings-mode 1).

EDIT2: You can see the problem I'm talking about by opening a diff-mode buffer (C-x f test.diff), and then running M-x shell from it. The shell buffer had the Diff-Auto-Refine mode on, inherited from the diff buffer. This isn't a problem for Diff-Auto-Refine, but it is for myplugin-keybindings

  • How do you "open new buffers from a coq-mode buffer" -- i.e., find-file, display-buffer, clone the buffer and incorporate everything from the original buffer, etc.? Is your minor-mode attached to a major-mode hook for all new buffers opening in that particular major-mode -- e.g., if major-mode xyz then (coq-mode 1)? Is your minor-mode global -- e.g., :global t -- instead of local, and that's the problem you wish to resolve?
    – lawlist
    Apr 5 '15 at 19:56
  • My minor mode is local, not global. New buffers are opened e.g. using find-file, or shell, or ido-switch-buffer.
    – Clément
    Apr 5 '15 at 20:03
  • Unless you have a global activation for your myplugin-keybindings-mode -- e.g., :global t or (define-globalized-minor-mode . . .), it should only be active in buffers that have a major-mode of coq-mode. Until you track down the problem, you can use (unless (eq major-mode 'coq-mode) . . .) as a wrap-around for your minor mode.
    – lawlist
    Apr 5 '15 at 20:06
  • @lawlist: I've edited the post. For an example of the behaviour I want to avoid, open a diff-mode buffer, and run M-x shell from it. The shell buffer will have Diff-Auto-Refine minor mode enabled, inherited from the diff buffer. The same happens with my keybindings mode, and I want to avoid that.
    – Clément
    Apr 5 '15 at 20:07
  • @lawlist Is that really the case? It's not a globalized minor mode, but it's definitely inherited by buffers opened from a buffer where it's already active.
    – Clément
    Apr 5 '15 at 20:08

How about something like this?:

(define-minor-mode myplugin-keybindings-mode
"A minor-mode for . . . . ."
  :init-value nil
  :lighter " myplugin"
  :keymap nil
  :global nil
  :group 'coq-mode
        (eq major-mode 'coq-mode))
      ;; *** insert additional stuff
      (when (called-interactively-p 'any)
        (message "Turned ON `myplugin-keybindings-mode`.")))
      ;; *** insert additional stuff
      (when (called-interactively-p 'any)
        (message "Turned OFF `myplugin-keybindings-mode`.")))))

(defun my-main-function ()
        (eq major-mode 'coq-mode))
    (message "Hello-world!")
    ;; insert more stuff
  • Thanks! That put me on the right track. I had messed up the order of the arguments, and had set init-value to t.
    – Clément
    Apr 5 '15 at 20:25

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.