How can I access the path to the current directory in an emacs directory variable file?
elisp, emacs
Solution
I asked myself the same question and found no solution on the web, so I think this answer may help. Actually, it turns out we can reuse `dir-locals-find-file` to get the directory containing the `.dir-locals.el` file. So here's what I found for, e.g, setting up an aspell personal dictionary dedicated to a whole directory:
((nil . ((eval . (setq ispell-personal-dictionary
(expand-file-name
".aspell_words"
(file-name-directory
(let ((d (dir-locals-find-file ".")))
(if (stringp d) d (car d))))))))))
Also, it seems entries are evaluated in the order they are specified, so the following code should work:
((nil . ((eval . (set (make-local-variable 'my-project-path)
(file-name-directory
(let ((d (dir-locals-find-file ".")))
(if (stringp d) d (car d))))))
(eval . (message "Project directory set to `%s'." my-project-path)))))
Emacs will complain about unsafe local variables (due to the `eval` construct), yet one can still permanently mark it safe.
Update: Since Emacs ≥ 26.3 (and maybe older versions as well), it appears that one needs to use `(dir-locals-find-file "./")` instead of `(dir-locals-find-file ".")`.
Problem
According to the Emacs documentation, Directory Variables apply to all files below a directory that contains an `.dir-locals.el` file. How can I, in that file, set a variable to the full path that contains the file? For example: ``` ((nil . ((indent-tabs-mode . t) (my-project-path **THIS_DIRECTORY**)))) ```