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!

4

1 回答 1

1

您的问题是您自己的全球化次要模式正在调用全局linum 次要模式而不是缓冲区本地linum 次要模式!

你想这样做:

(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)

我建议实际derived-mode-p用于您的major-mode测试:

(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))))

nbdefine-globalized-minor-mode与 相同define-global-minor-mode,但我更喜欢“全球化”命名,因为它更能说明它的用途(即采用缓冲区本地次要模式,并创建一个新的全局次要模式来控制该缓冲区本地模式 - - 在许多缓冲区中启用或禁用它,整体。“常规”全局次要模式不会以这种方式依赖于缓冲区局部次要模式,因此“全球化”术语有助于将这种模式与其他模式区分开来全局模式)。

注意当您使用自定义全球化次要模式时,您不需要任何my-inhibit-global-linum-mode代码。那是一种完全不同的方法,您可以将其从 .emacs 文件中删除。

于 2017-01-30T20:34:41.487 回答