此函数将重新读取dir-locals文件并为当前缓冲区设置新值:
(defun my-reload-dir-locals-for-current-buffer ()
"reload dir locals for the current buffer"
(interactive)
(let ((enable-local-variables :all))
(hack-dir-local-variables-non-file-buffer)))
如果要为当前缓冲区目录中的每个缓冲区重新加载dir-locals:
(defun my-reload-dir-locals-for-all-buffer-in-this-directory ()
"For every buffer with the same `default-directory` as the
current buffer's, reload dir-locals."
(interactive)
(let ((dir default-directory))
(dolist (buffer (buffer-list))
(with-current-buffer buffer
(when (equal default-directory dir))
(my-reload-dir-locals-for-current-buffer)))))
通过after-save-hook
在.dir-locals.el缓冲区中添加一个dir-locals文件,您可以在每次保存dir-locals文件时刷新所有dir locals 。
(add-hook 'emacs-lisp-mode-hook
(defun enable-autoreload-for-dir-locals ()
(when (and (buffer-file-name)
(equal dir-locals-file
(file-name-nondirectory (buffer-file-name))))
(add-hook (make-variable-buffer-local 'after-save-hook)
'my-reload-dir-locals-for-all-buffer-in-this-directory))))
当然,这可能会带来意想不到的后果,因为它将重置变量值,而不管它们是否已在其他位置更改,因此使用风险自负。
buffer-list
。