20

I often find myself in the situation where I'm told to put absolute paths as directory local variable. For example cmake-ide's cmake-ide-project-dir and cmake-ide-build-dir. Of course this is not very portable.

So instead of

.dir-locals.el:

((nil . ((cmake-ide-project-dir . "/home/user/code/project"))))

I want something like

((nil . ((cmake-ide-project-dir . directory-of-current-dir-locals-file))))

How can I define such a variable directory-of-current-dir-locals-file? And how would I set for example cmake-ide-build-dir, which is typically (concat directory-of-current-dir-locals-file "build")?

2 Answers 2

9

My solution so far (based on this Stackoverflow answer):

.dir-locals.el:

;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")

((nil . ((eval . (set (make-local-variable 'my-project-path)
                      (file-name-directory
                       (let ((d (dir-locals-find-file ".")))
                         (if (stringp d) d (car d))))))
         (cmake-ide-project-dir . my-project-path)
         (eval . (setq cmake-ide-build-dir (concat my-project-path "build")))
         )))
2
  • 5
    You can use also (locate-dominating-file default-directory ".dir-local.el")
    – ibizaman
    Oct 26, 2017 at 5:24
  • 1
    @ibizaman It should be ".dir-locals.el": (locate-dominating-file default-directory ".dir-locals.el") Apr 5, 2021 at 19:36
-2

You could define variable with (defvar directory-of-current-dir-locals-file "/home/user/code/project").

1
  • 2
    Sorry maybe my question was not clear. But the idea was to not use absolute paths. Since you know you want to set the variable to something relative to the .dir-locals.el file.
    – Flow
    Aug 5, 2016 at 13:23

Your Answer

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

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