自动禁用特定主要模式的全局次要模式

发布于 2024-11-26 15:14:49 字数 531 浏览 2 评论 0原文

我已经全局激活了居中光标模式,如下所示:

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

它工作正常,但有一些主要模式我想自动禁用它。例如 slime-repl 和 shell。

还有另一个问题处理同样的问题,但有另一种次要模式。不幸的是,答案仅提供了此特定次要模式(全局智能选项卡模式)的解决方法,它不适用于居中光标模式。

我尝试了这个钩子,但没有效果。变量不会改变。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你拒绝所有暧昧 2024-12-03 15:14:49

使用 define-globalized-minor-mode1 宏创建的全局次要模式有点棘手。您的代码似乎没有执行任何操作的原因是全球化模式利用 after-change-major-mode-hook 来激活它们控制的缓冲区本地次要模式;并且该钩子在主模式自己的钩子4之后立即运行。

各个模式可能会实现指定某种黑名单的自定义方法或防止在某些情况下启用该模式的其他方法,因此一般来说值得查看相关的 Mxcustomize-group 选项来查看该包是否存在此类设施。然而,对于任何全球化的次要模式来说,目前我还没有一个很好的、干净的通用方法来实现这一目标。

遗憾的是该宏定义的 MODE-enable-in-buffers 函数没有执行相同的操作(with-current-buffer buf (if ,global-mode ...) ) 检查哪个是由全局模式函数执行的。如果确实如此,您可以简单地使用 slime-repl-mode-hook 将全局模式变量设置为 buffer-local 和 nil。

一个快速技巧是检查2全球化次要模式定义的turn-on参数是什么(在本例中是centered-cursor-mode 本身3),并写一些周围的建议,以阻止对相关模式进行评估。

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

我们可以做的事情(使用简单的可重用模式)是在启用缓冲区本地次要模式后立即再次禁用它。使用 APPEND 参数添加到 add-hookafter-change-major-mode-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或其别名define-global-minor-mode,我认为应该避免,因为可能与“混淆”全局”次要模式创建定义次要模式。 “全球化”次要模式虽然仍然涉及全局次要模式,但在实践中的工作方式非常不同,因此最好将它们称为“全球化”而不是“全局”。

2 Chf define-globalized-minor-mode RET 显示 turn-on 是第三个参数,我们在模式定义中使用 Mx find-function RET 检查它全局中心光标模式 RET

3 使用这种方法,这一事实将阻止您使用 slime-repl-mode 或 shell-mode 缓冲区启用此次要模式,而具有单独打开功能的全球化次要模式可以如果您愿意,仍然可以以其非全局形式调用。

4 https://stackoverflow.com/a/19295380/324105

Global minor modes created with the define-globalized-minor-mode1 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.

Individual modes may implement custom ways of specifying some kind of black list or other method of preventing the mode from being enabled in certain circumstances, so in general it would be worth looking at the relevant M-x customize-group options for the package to see if such facilities exist. However, a nice clean general way of achieving this for ANY globalized minor mode is eluding me for the moment.

It's a shame that the MODE-enable-in-buffers function defined by that macro doesn't do the same (with-current-buffer buf (if ,global-mode ...)) check which is performed by the global mode function. If it did, you could simply use slime-repl-mode-hook to make the global mode variable buffer-local and 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)

Something we can do (with an easy re-usable pattern) is immediately disable the buffer-local minor mode again after it has been enabled. An after-change-major-mode-hook function added with the APPEND argument to add-hook will reliably run after the globalized minor mode has acted, and so we can do things like:

(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

﹂绝世的画 2024-12-03 15:14:49

我制作了一个新的全局次要模式,它在某些模式下不会被激活。 lambda 是在每个新缓冲区中调用以激活次要模式的函数。这是例外的正确地方。

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

它应该适用于所有其他全局次要模式。只需复制定义 global-xxx-mode 并做出正确的例外即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文