Search code examples
emacsread-eval-print-loopslime

automatically disable a global minor mode for a specific major mode


I have centered-cursor-mode activated globaly, like this:

(require 'centered-cursor-mode)
(global-centered-cursor-mode 1)

It works fine, but there are some major modes where I would like to disable it automatically. For example slime-repl and shell.

There is another question dealing with the same problem, but another minor mode. Unfortunately the answers only offer workarounds for this specific minor mode (global-smart-tab-mode), that doesn't work with centered-cursor-mode.

I tried this hook, but it has no effect. The variable doesn't change.

(eval-after-load "slime"
  (progn
    (add-hook 'slime-repl-mode-hook (lambda ()
                                      (set (make-local-variable 'centered-cursor-mode) nil)))
    (slime-setup '(slime-repl slime-autodoc))))

Solution

  • I made a new global minor mode, that doesn't get activated in certain modes. The lambda is the function that gets called in every new buffer to activate the minor mode. That is the right place to make exceptions.

    (require 'centered-cursor-mode)
    
    (define-global-minor-mode my-global-centered-cursor-mode centered-cursor-mode
      (lambda ()
        (when (not (memq major-mode
                         (list 'slime-repl-mode 'shell-mode)))
          (centered-cursor-mode))))
    
    (my-global-centered-cursor-mode 1)
    

    It should work for every other global minor mode. Just copy the definition global-xxx-mode and make the right exceptions.