I want to use jinja2-mode for files ending ".html" when the variable my:use-jinja-for-html-p
is non-nil.
I have the following files:
.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
runs after html-mode, but uses the global value of my:use-jinja-for-html-p
instead of the file-local value specified in .dir-locals.el
.
Why doesn't my:maybe-chose-jinja2-mode
use the file-local value?
Answer for Completeness - see dupe for detail
;; 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)