首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于扩展的Emacs自动小模式

基于扩展的Emacs自动小模式
EN

Stack Overflow用户
提问于 2012-12-19 04:57:17
回答 2查看 3.6K关注 0票数 22

我在这个主题上找到了一些this question,但是在emacs中是否有一种方法可以根据扩展设置一个次要模式(或列表)?例如,很容易发现主要的模式可以像这样被操纵

代码语言:javascript
复制
(add-to-list 'auto-mode-alist '("\\.notes\\'" . text-mode))

我最想做的是

代码语言:javascript
复制
(add-to-list 'auto-minor-mode-alist '("\\.notes\\'" . auto-fill-mode))

链接问题的接受答案提到钩子,特别是temp-buffer-setup-hook。要使用这一点,您必须向钩子中添加如下函数

代码语言:javascript
复制
(add-hook 'temp-buffer-setup-hook #'my-func-to-set-minor-mode)

我的问题有两方面:

  1. 是否有一种与主要模式类似的更容易做到这一点的方法?
  2. 如果不是,如何为钩子编写函数?
    1. 它需要根据正则表达式检查文件路径。
    2. 如果匹配,则激活所需的模式(例如auto-fill-mode)。

虚弱而又笨手笨脚的试图解决问题:

代码语言:javascript
复制
;; 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))))
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-19 05:53:18

这段代码似乎给出了您想要的结果:

代码语言:javascript
复制
(defvar auto-minor-mode-alist ()
  "Alist of filename patterns vs correpsonding minor mode functions, see `auto-mode-alist'
All elements of this alist are checked, meaning you can enable multiple minor modes for the same regexp.")

(defun enable-minor-mode-based-on-extension ()
  "Check file name against `auto-minor-mode-alist' to enable minor modes
the checking happens for all pairs in auto-minor-mode-alist"
  (when buffer-file-name
    (let ((name (file-name-sans-versions buffer-file-name))
          (remote-id (file-remote-p buffer-file-name))
          (case-fold-search auto-mode-case-fold)
          (alist auto-minor-mode-alist))
      ;; Remove remote file name identification.
      (when (and (stringp remote-id)
                 (string-match-p (regexp-quote remote-id) name))
        (setq name (substring name (match-end 0))))
      (while (and alist (caar alist) (cdar alist))
        (if (string-match-p (caar alist) name)
            (funcall (cdar alist) 1))
        (setq alist (cdr alist))))))

(add-hook 'find-file-hook #'enable-minor-mode-based-on-extension)

注意:比较是用string-match-p完成的,它在比较期间遵循case-fold-search设置。

票数 14
EN

Stack Overflow用户

发布于 2016-09-23 03:42:45

Trey的答案似乎是一个非常健壮和可扩展的解决方案,但我正在寻找更简单的解决方案。以下代码将在编辑hmmm-mode文件时启用虚构的.hmmm

代码语言:javascript
复制
(add-hook 'find-file-hook
          (lambda ()
            (when (string= (file-name-extension buffer-file-name) "hmmm")
              (hmmm-mode +1))))
票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13945782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档