2

Since doc-view-mode is very slow with the enabled linum-mode, I am trying to disable it for that mode. The same question has been answered almost 6 years ago: automatically disable a global minor mode for a specific major mode

Following the answer of phils, I have put the following in my .emacs file:

(define-global-minor-mode my-global-linum-mode global-linum-mode
(lambda ()
  (when (not (memq major-mode
                   (list 'doc-view-mode 'shell-mode)))
    (global-linum-mode))))
(my-global-linum-mode 1)
(add-hook 'doc-view-mode-hook 'my-inhibit-global-linum-mode)
(defun my-inhibit-global-linum-mode ()
  "Counter-act `global-linum-mode'."
  (add-hook 'after-change-major-mode-hook
            (lambda () (linum-mode 0))
            :append :local))

The problem is that I cannot make it work permanently. When I start a new buffer, the line numbers reappear in the buffer of doc-view-mode. Please help!

Community
  • 1
  • 1
honey_badger
  • 472
  • 3
  • 10

1 Answers1

1

Your problem is that your own globalized minor mode is invoking the global linum minor mode instead of the buffer-local linum minor mode!

You wanted to do this:

(define-global-minor-mode my-global-linum-mode linum-mode
  (lambda ()
    (when (not (memq major-mode
                     (list 'doc-view-mode 'shell-mode)))
      (linum-mode 1))))
(my-global-linum-mode 1)

I would suggest actually using derived-mode-p for your major-mode test:

(define-globalized-minor-mode my-global-linum-mode linum-mode
  (lambda ()
    (unless (or (minibufferp)
                (derived-mode-p 'doc-view-mode 'shell-mode))
      (linum-mode 1))))

n.b. define-globalized-minor-mode is the same thing as define-global-minor-mode, but I prefer the "globalized" naming as it's slightly more indicative of what it's for (i.e. to take a buffer-local minor mode, and create a new global minor mode which controls that buffer-local mode -- enabling it or disabling it in many buffers, en masse. A 'regular' global minor mode would not depend upon a buffer-local minor mode in this way, so the "globalized" terminology is helpful to differentiate this kind of mode from other global modes).

n.b. As you're using a custom globalized minor mode, you don't need any of the my-inhibit-global-linum-mode code. That was an entirely different approach, and you can remove it from your .emacs file.

phils
  • 71,335
  • 11
  • 153
  • 198