-->

基于扩展Emacs的自动未成年人模式(Emacs auto-minor-mode based on

2019-08-04 07:20发布

我发现这个问题的话题有点,但有[在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 。 要使用此功能,你必须添加一个函数来钩,像这样

(add-hook 'temp-buffer-setup-hook #'my-func-to-set-minor-mode)

我的问题是双重的:

  1. 是否有更简单的方法来做到这一点,类似于主要模式?
  2. 如果不是,怎么会写的钩子函数?
    1. 它需要检查文件路径对正则表达式。
    2. 如果匹配,激活所需的模式(例如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))))

Answer 1:

此代码似乎给你想要的东西:

(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比较期间设置。



Answer 2:

由杰克逊三分球的答案似乎是一个非常强大的,可扩展的解决方案,但我一直在寻找更简单的东西。 下面的代码将使虚构hmmm-mode进行编辑时.hmmm文件:

(add-hook 'find-file-hook
          (lambda ()
            (when (string= (file-name-extension buffer-file-name) "hmmm")
              (hmmm-mode +1))))


文章来源: Emacs auto-minor-mode based on extension