如何在 emacs 启动时默认启用非全局次要模式? [英] How to enable a non-global minor mode by default, on emacs startup?

查看:22
本文介绍了如何在 emacs 启动时默认启用非全局次要模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想每次启动 emacs 时都启用 rainbow-mode,而不是必须使用 M-x Rainbow-mode.

我想我在 .emacs 文件中放入了一些命令.

我尝试了以下所有方法,但都没有奏效:

(需要 'rainbow-mode)(彩虹模式初始化)(全局彩虹模式)

更一般地说,我如何在启动时自动加载任何模式/包?

解决方案

rainbow-mode 不是全局次要模式,因此需要在每个缓冲区的基础上启用它.

我只将它用于 CSS,所以我有:

(add-hook 'css-mode-hook 'my-css-mode-hook)(defun my-css-mode-hook()(彩虹模式 1))

如果您真正希望它是全局的,无处不在,您可以轻松地自己定义一个全局次要模式:

(define-globalized-minor-mode my-global-rainbow-mode Rainbow-mode(lambda () (彩虹模式 1)))(我的全球彩虹模式 1)

您可以向该 (lambda () (rainbow-mode 1)) 函数(将在每个缓冲区中评估)添加任意逻辑,以决定是否实际上为给定的缓冲区调用 (rainbow-mode 1) ,所以如果您对 elisp 感到满意,那么您可以轻松扩展这种方法来满足您对相关模式的特定要求.


<块引用>

更一般地说,我如何在启动时自动加载任何模式/包?

它可能会有所不同,但我所展示的方法足以满足大多数次要模式:无论何时启用 MODE(即某些特定的其他模式名称),您都希望它们启用,在这种情况下,您可以根据 css-mode-hook 示例使用 MODE-hook 变量(将始终可用);或者您希望永久启用该模式,在这种情况下,全局次要模式是一个好方法(因为您可以全局打开和关闭它).一些次要模式默认是全局的(或提供全局变体),但您可以根据 my-global-rainbow-mode 示例创建自己的模式.

另请注意,模式可以从其他模式派生,在这种情况下,所有相关的MODE-hook钩子将被运行(对于详情请参阅 https://stackoverflow.com/a/19295380/324105).一个常见的用例是使用 prog-mode-hook所有从它派生的编程模式(大多数编程模式)启用所需的功能.

请记住,许多(希望是大多数)库和包将提供使用说明.如果找不到文档,请务必尝试 M-x find-library 访问库文件,然后通读顶部的注释.通常有一个非常有用的评论"部分,有时这是最终用户文档的主要来源,并解释如何启用其功能.

I want to enable rainbow-mode everytime I start emacs, rather than having to use M-x rainbow-mode.

I guess there is some command I put in my .emacs file.

I tried all of the following, but none of them worked:

(require 'rainbow-mode)   

(rainbow-mode initialize)

(global-rainbow-mode)

More generally, how do I load any mode/package automatically on startup?

解决方案

rainbow-mode isn't a global minor mode, so it needs to be enabled on a per-buffer basis.

I only use it for CSS, so I have:

(add-hook 'css-mode-hook 'my-css-mode-hook)
(defun my-css-mode-hook ()
  (rainbow-mode 1))

If you genuinely want it to be global, everywhere, you can easily define a global minor mode yourself:

(define-globalized-minor-mode my-global-rainbow-mode rainbow-mode
  (lambda () (rainbow-mode 1)))

(my-global-rainbow-mode 1)

You can add any arbitrary logic to that (lambda () (rainbow-mode 1)) function (which will be evaluated in every buffer) in order to decide whether or not to actually call (rainbow-mode 1) for a given buffer, so if you're comfortable with elisp then you can easily extend this approach to cover your specific requirements for the mode in question.


More generally, how do I load any mode/package automatically on startup?

It can vary, but the approaches I've shown would suffice for most minor modes: Either you want them enabled whenever MODE is enabled (being some specific other mode name), in which case you can use the MODE-hook variable (which will always be available) as per the css-mode-hook example; or else you want the mode enabled permanently, in which case a global minor mode is a good approach (because you can toggle it on and off globally). Some minor modes are global by default (or provide global variants), but you can create your own if necessary, as per the my-global-rainbow-mode example.

Also be aware that modes can be derived from other modes, in which case all relevant MODE-hook hooks will be run (for details see https://stackoverflow.com/a/19295380/324105). A common use-case is to use prog-mode-hook to enable functionality wanted for all the programming modes which are derived from it (which is most programming modes).

Remember that many (hopefully most) libraries and packages will provide usage instructions. If you can't find documentation, be sure to try M-x find-library to visit the library file, and then read through the comments at the top. There is often a very informative "Commentary" section, and sometimes this is the primary source of end-user documentation, and explain how to enable its functionality.

这篇关于如何在 emacs 启动时默认启用非全局次要模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆