【发布时间】:2012-12-06 09:58:16
【问题描述】:
我发现this question 有点关于这个话题,但是[在 emacs 中] 有没有办法根据 extension 设置一个次要模式(或其列表)?例如,很容易发现可以像这样操纵主要模式
(add-to-list 'auto-mode-alist '("\\.notes\\'" . text-mode))
而我最想做的是
(add-to-list 'auto-minor-mode-alist '("\\.notes\\'" . auto-fill-mode))
链接问题的接受答案提到了钩子,特别是temp-buffer-setup-hook。要使用它,您必须像这样向钩子添加一个函数
(add-hook 'temp-buffer-setup-hook #'my-func-to-set-minor-mode)
我的问题有两个:
- 有没有更简单的方法来做到这一点,类似于主要模式?
- 如果不是,如何为钩子编写函数?
- 它需要根据正则表达式检查文件路径。
- 如果匹配,则激活所需模式(例如
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))))
【问题讨论】:
-
查看
C-h v auto-mode-alist时,扩展名通常写为"\\.notes\\'"。请参阅最后的单个引用。 -
我会被诅咒的。我在打字时错过了这一点。可能完全解释了尾随的反斜杠 -_-