10

I'm using company-mode for completion in C++ code. To tell clang backend of company where the include files for the current project are I have to use the following .dir-locals.el file

((c++-mode (eval setq company-clang-arguments (append 
                                               company-clang-arguments
                                               '("-I/full/path/to/project/root/include"))))

I have to specify the full path always. Is there a way to replace full/path/to/project/root with a variable that stores the path the the .dir-locals.el file?

I tried

 (c++-mode (eval setq company-clang-arguments (append
                                               company-clang-arguments
                                               (list concat "-I" default-directory "src")))))

but since default-directory is evaluated in the file that I'm opening it doesn't return to path the the project root but some other path inside the project.

kain88
  • 825
  • 7
  • 19
  • See also https://emacs.stackexchange.com/questions/26093/how-can-i-set-directory-local-variable-in-relative-to-dir-locals-el-file-locati – Flow Nov 26 '22 at 13:28

1 Answers1

6

I found the solution with projectile. It has a function projectile-project-root which can be used to get the project path.

I can noe use the following in .dir-locals.el and it will still work when I move the project or use it on another machine.

((nil . ((eval . (progn
                   (require 'projectile)
                   (setq company-clang-arguments (delete-dups (append
                                                  company-clang-arguments
                                                  (list (concat "-I" (projectile-project-root) "src"))))))))))

Only downside is that it you need projectile and that the project has to be a projectile project (not much of a problem since every folder with git/bzr/hg/... is a valid project).

update

As pointed out in the comments it is also possible to use

(locate-dominating-file default-directory ".dir-locals.el")

instead of projectile-project-root.

kain88
  • 825
  • 7
  • 19
  • 3
    `(locate-dominating-file default-directory ".dir-locals.el")` may also be an option, maybe even abbreviated into a function. – politza Jun 07 '15 at 20:30
  • This is pretty much what `projectile-project-root` does. Thanks for the tip though. – kain88 Jun 08 '15 at 07:29
  • @politza: I don't think that works: it will pick up the closest `.dir-locals.el`, not the one that the form comes from. – Clément Jul 20 '16 at 02:32
  • I think that using `dir-locals-file` variable instead of ".dir-locals.el" string is more appropriate – sivakov512 Jan 15 '17 at 05:37