reddit / emacs

Resetting .dir-locals.el

ilemming · 2022-07-27 20:28
Does anyone know a guaranteed way of resetting dir-locals for the entire project? I would change something in .dir-locals.el, the eval `(hack-dir-local-variables)` but it would set the new values for some buffers in the project, not all. I need to reset them project-wise.
ilemming · 2022-07-27 20:38
It looks like I can do `(dir-locals-read-from-dir (dired-current-directory))`, but then, I have to kill and re-open every buffer in the project. Evaling vars listed in .dir-locals.el shows old values until kill and I reopen the buffer.
JDRiverRun · 2022-07-28 02:12
“Another method of specifying directory-local variables is to define a group of variables/value pairs in a directory class, using the dir-locals-set-class-variables function; then, tell Emacs which directories correspond to the class by using the dir-locals-set-directory-class function. These function calls normally go in your initialization file (see The Emacs Initialization File). This method is useful when you can’t put .dir-locals.el in a directory for some reason. “ https://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html
Phil-Hudson · 2022-07-28 15:09
You don't have to kill and re-visit. `C-h f revert-buffer-quick RET` (assuming you're on Gnu Emacs 28.1 or later).
eleven_cupfuls · 2022-07-29 01:42
You can use `hack-dir-local-variables` if you loop over all the buffers in the project. I think this is really the only way to do it: (defun my-project:reload-dir-locals (proj) "Read values from the current project's .dir-locals file and apply them in all project file buffers as if opening those files for the first time. Signals an error if there is no current project." (interactive (list (project-current))) (unless proj (user-error "There doesn't seem to be a project here")) ;; Load the variables; they are stored buffer-locally, so... (hack-dir-local-variables) ;; Hold onto them... (let ((locals dir-local-variables-alist)) (dolist (buffer (buffer-list)) (with-current-buffer buffer (when (and (equal proj (project-current)) buffer-file-name) ;; transfer the loaded values to this buffer... (setq-local dir-local-variables-alist locals) ;; and apply them. (hack-local-variables-apply)))))) https://gitlab.com/woolsweater/dotemacs.d/-/blob/75c9ba7bbb4db09fbbab35715d19628fea1372e1/startup/builtin.el#L524-544 Of course you can replace project.el with projectile if you use that. I believe it has a function that returns all the project buffers.
ilemming · 2022-07-29 18:56
Thank you. I think that's exactly what I needed.