如何相对于.dir-locals.el文件位置设置目录局部变量?


15

我经常遇到被告知将相对路径作为目录局部变量的情况。例如cmake-idecmake-ide-project-dircmake-ide-build-dir。当然,这不是很方便。

所以代替

.dir-locals.el

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

我想要类似的东西

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

如何定义这样的变量directory-of-current-dir-locals-file?举例来说cmake-ide-build-dir,我该如何设置(concat directory-of-current-dir-locals-file "build")

Answers:


7

到目前为止,我的解决方案(基于此Stackoverflow答案):

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

4
您还可以使用(locate-dominating-file default-directory ".dir-local.el")
ibizaman

0

您可以使用定义变量(defvar directory-of-current-dir-locals-file "/home/user/code/project")


2
抱歉,我的问题不清楚。但想法是使用绝对路径。既然您知道要将变量设置为相对于.dir-locals.el文件的某种值。
流量
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.