这是一种不同的方法。
我定义了一个函数,该函数生成当前目录层次结构中所有目录的列表。
(defun file-name-directory-nesting-helper (name previous-name accumulator)
(if (string= name previous-name)
accumulator ; stop when names stop changing (at the top)
(file-name-directory-nesting-helper
(directory-file-name (file-name-directory name))
name
(cons name accumulator))))
(defun file-name-directory-nesting (name)
(file-name-directory-nesting-helper (expand-file-name name) "" ()))
一个例子如下:
(file-name-directory-nesting "/foo/bar/baz/quux/foo.el")
;; => ("/" "/foo" "/foo/bar" "/foo/bar/baz" "/foo/bar/baz/quux" "/foo/bar/baz/quux/foo.el")
现在,我可以添加建议以hack-dir-local-variables
使其“假装”我们正在树的顶部访问文件,应用目录本地设置,然后下一级,再次应用设置,依此类推。
(defun hack-dir-local-variables-chained-advice (orig)
"Apply dir-local settings from the whole directory hierarchy,
from the top down."
(let ((original-buffer-file-name (buffer-file-name))
(nesting (file-name-directory-nesting (or (buffer-file-name)
default-directory))))
(unwind-protect
(dolist (name nesting)
;; make it look like we're in a directory higher up in the
;; hierarchy; note that the file we're "visiting" does not
;; have to exist
(setq buffer-file-name (expand-file-name "ignored" name))
(funcall orig))
;; cleanup
(setq buffer-file-name original-buffer-file-name))))
(advice-add 'hack-dir-local-variables :around
#'hack-dir-local-variables-chained-advice)
.dir-locals
?。