How can I access the path to the current directory in an emacs directory variable file?

Viewed 9150

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**))))
7 Answers

I've found the locate-dominating-file procedure, which comes out-of-the-box with Emacs, useful to retreive the current directory of a known file. The example below sets the guix-directory variable to the topmost directory of the project containing a .dir-locals.el file.

((nil . ((eval . (setq guix-directory
                       (locate-dominating-file default-directory
                                               ".dir-locals.el"))))))

It's not a safe .dir-locals.el setting, due to relying on eval, but it gets the job done.

Related