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.

1

1 Answer 1

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.

4
  • 3
    (locate-dominating-file default-directory ".dir-locals.el") may also be an option, maybe even abbreviated into a function.
    – politza
    Commented Jun 7, 2015 at 20:30
  • This is pretty much what projectile-project-root does. Thanks for the tip though.
    – kain88
    Commented Jun 8, 2015 at 7: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
    Commented Jul 20, 2016 at 2:32
  • I think that using dir-locals-file variable instead of ".dir-locals.el" string is more appropriate
    – sivakov512
    Commented Jan 15, 2017 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.