I found 这个问题 https://stackoverflow.com/questions/1299193/in-emacs-how-to-automatically-enable-a-minor-mode-based-on-buffer-name有点关于这个主题,但是有没有一种方法[在 emacs 中]可以基于以下设置设置次要模式(或其列表)扩大?例如,很容易发现主要模式可以像这样操纵
(add-to-list 'auto-mode-alist '("\\.notes\\'" . text-mode))
我最希望能够做的是
(add-to-list 'auto-minor-mode-alist '("\\.notes\\'" . auto-fill-mode))
链接问题的接受答案提到了钩子,特别是temp-buffer-setup-hook http://www.gnu.org/savannah-checkouts/gnu/emacs/manual/html_node/elisp/Temporary-Displays.html#Temporary-Displays。要使用它,您必须像这样向钩子添加一个函数
(add-hook 'temp-buffer-setup-hook #'my-func-to-set-minor-mode)
我的问题有两个:
- 有没有更简单的方法来做到这一点,类似于主要模式?
- If not, how would one write the function for the hook?
- 它需要根据正则表达式检查文件路径。
- 如果匹配,则激活所需的模式(例如
auto-fill-mode
).
解决方案的尝试是微弱且有缺陷的:
;; Enables the given minor mode for the current buffer it it matches regex
;; my-pair is a cons cell (regular-expression . minor-mode)
(defun enable-minor-mode (my-pair)
(if buffer-file-name ; If we are visiting a file,
;; and the filename matches our regular expression,
(if (string-match (car my-pair) buffer-file-name)
(funcall (cdr my-pair))))) ; enable the minor mode
; used as
(add-hook 'temp-buffer-setup-hook
(lambda ()
(enable-minor-mode '("\\.notes\\'" . auto-fill-mode))))
Trey Jackson 的答案似乎是一个非常强大且可扩展的解决方案,但我一直在寻找更简单的东西。以下代码将启用虚构的hmmm-mode
编辑时.hmmm
files:
(add-hook 'find-file-hook
(lambda ()
(when (string= (file-name-extension buffer-file-name) "hmmm")
(hmmm-mode +1))))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)