Global minor modes created with the define-globalized-minor-mode
1 macro are a bit tricky. The reason your code doesn't appear to do anything is that globalized modes utilise after-change-major-mode-hook
to activate the buffer-local minor mode that they control; and that hook runs immediately after the major mode's own hooks4.
各个模式可能会实现指定某种黑名单的自定义方法或防止在某些情况下启用该模式的其他方法,因此一般来说值得查看相关的M-x customize-group
包的选项来查看此类设施是否存在。然而,对于任何全球化的次要模式来说,目前我还没有一个很好的、干净的通用方法来实现这一目标。
遗憾的是MODE-enable-in-buffers
该宏定义的函数不执行相同的操作(with-current-buffer buf (if ,global-mode ...))
检查哪个是由全局模式函数执行的。如果是的话,你could只需使用 slime-repl-mode-hook 将全局模式变量设置为 buffer-local 和 nil。
A quick hack is to check2 what the turn-on
argument is for the globalized minor mode definition (in this instance it's centered-cursor-mode
itself3), and write some around advice to stop that from being evaluated for the modes in question.
(defadvice centered-cursor-mode (around my-centered-cursor-mode-turn-on-maybe)
(unless (memq major-mode
(list 'slime-repl-mode 'shell-mode))
ad-do-it))
(ad-activate 'centered-cursor-mode)
我们的一些东西cando(具有简单的可重用模式)是在启用缓冲区本地次要模式后立即再次禁用它。一个after-change-major-mode-hook
添加的功能APPEND
论证add-hook
将在全球化次要模式起作用后可靠地运行,因此我们可以执行以下操作:
(add-hook 'term-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))
1 or its alias define-global-minor-mode
which I feel should be avoided, due to the potential for confusion with "global" minor modes created with define-minor-mode
. "Globalized" minor modes, while still involving a global minor mode, work very differently in practice, so it is better to refer to them as "globalized" rather than "global".
2 C-hf define-globalized-minor-mode
RET shows that turn-on
is the third argument, and we check that in the mode definition with M-x find-function
RET global-centered-cursor-mode
RET.
3 with this approach, that fact is going to prevent you from ever enabling this minor mode with slime-repl-mode or shell-mode buffers, whereas a globalized minor mode with a separate turn-on function could still be invoked in its non-global form if you so desired.
4 https://stackoverflow.com/a/19295380/324105