使用.dir-locals.el来选择主要模式

时间:2014-07-26 02:47:40

标签: emacs elisp

当变量my:use-jinja-for-html-p为非零时,我想将jinja2-mode用于以“.html”结尾的文件。

我有以下文件:

.DIR-locals.el

((nil . ((my:use-jinja-for-html-p . t))))

blah.html

<html>

的.emacs

(defvar my:use-jinja-for-html-p nil
  "Use `jinja2-mode' if non-nil, otherwise `html-mode'.
Primarily for use in .dir-locals.el")

(defun my:maybe-choose-jinja2-mode ()
  (when my:use-jinja-for-html-p
    (jinja2-mode)))

(add-hook 'html-mode-hook 'my:maybe-choose-jinja2-mode)

my:maybe-choose-jinja2-mode在html-mode之后运行,但使用全局值my:use-jinja-for-html-p而不是.dir-locals.el中指定的文件本地值。

为什么my:maybe-chose-jinja2-mode不使用文件本地值?

回答完整性 - 请参阅详细信息

;; See http://stackoverflow.com/questions/5147060/ for explanation of
;; why we're creating a new hook.  tl;dr: wierd interaction between
;; `set-auto-mode' and `hack-local-variables'
(add-hook 'hack-local-variables-hook 'my:run-local-vars-mode-hook)
(defun my:run-local-vars-mode-hook ()
  "Run a hook for major-mode after processing local variables.
e.g. `python-mode-local-vars-hook',`emacs-lisp-mode-local-vars-hook'"
  (run-hooks (intern (format "%s-local-vars-hook" (symbol-name major-mode)))))

(add-hook 'html-mode-local-vars-hook 'my:maybe-choose-jinja2-mode)

0 个答案:

没有答案