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")?

Flow
  • 564
  • 6
  • 17

2 Answers2

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")))
         )))
Flow
  • 564
  • 6
  • 17
-2

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

Konstantin Morenko
  • 1,407
  • 9
  • 19
  • 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 05 '16 at 13:23