Inilah cara berbeda untuk melakukan ini.
Saya mendefinisikan fungsi yang menghasilkan daftar semua direktori dalam hirarki direktori saat ini.
(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) "" ()))
Contohnya adalah dalam urutan:
(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")
Sekarang saya dapat menambahkan saran untuk hack-dir-local-variables
membuatnya "berpura-pura" bahwa kami mengunjungi file di bagian paling atas pohon, menerapkan pengaturan direktori-lokal, lalu turun satu tingkat, menerapkan pengaturan lagi, dan sebagainya.
(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
? .