.dir-locals.el应该咋写?

((emacs-lisp-mode . ((no-byte-compile t))))

OR

((emacs-lisp-mode
  (no-byte-compile t)))

OR

((emacs-lisp-mode
  (no-byte-compile . t)))

这三种写法有什么不一样?哪种是错的?

1,2 都错

M-xadd-dir-local-variable 然后你就明白了

1 个赞

File Local Variable 和 Directory Local Variable,尽量用专门的命令输入,不要手写,无论你清不清楚 Syntax。

我在工程下建立了.dir-locals.el文件,代码如下

((nil . ((eval . (flycheck-clang-include-path 
                     (list xx xxxx) )))))

这么写没有问题,但改成下面的写法

(setq mylist '())
(setq mylist (push xxx mylist))
....

((nil . ((eval . (flycheck-clang-include-path 
                     mylist)))))

报下面的错误

(wrong-type-argument listp setq)

mylist已经是list了呀,这是怎么回事?

因為不能直接写代碼。

((nil . ((eval . (progn
                    (defvar mylist '())
                    (add-to-list 'mylist xxx)
                    (flycheck-clang-include-path 
                      mylist))))))

哦,看不太懂这个写法,nil . 还有 eval . 代表什么意思?

解釋起來太麻煩,就當是魔法好了。eval 总知道是啥吧,nil 就是默认行为。

已经可以了,我在.dir-locals.el中写了一个遍历,把指定的文件夹下所有含头文件的目录都找出来

((nil . ((eval . (progn
                   (defvar headers '())
                   (defun add-c-header (header-path)
                     (if (not (member header-path headers))
                         (add-to-list 'headers header-path)))
                   (defun recursive-access-dir (dir-path)
                     (dolist (file (directory-files dir-path))
                       (let ((file-full-path (expand-file-name file dir-path)))
                         (if (file-directory-p file-full-path)
                             (if (not (string-match "^[.].*" file))
                                 (recursive-access-dir file-full-path)
                               )
                           (if (string-match ".*[.]h$" file)
                               (add-c-header dir-path))
                           )
                         )
                       ))

                   (recursive-access-dir "~/work/project/julius/")
                   (setq flycheck-clang-include-path headers))))))

生成到custom.el中的内容如下

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(safe-local-variable-values
   (quote
    ((eval progn
           (defvar headers
             (quote nil))
           (defun add-c-header
               (header-path)
             (if
                 (not
                  (member header-path headers))
                 (add-to-list
                  (quote headers)
                  header-path)))
           (defun recursive-access-dir
               (dir-path)
             (dolist
                 (file
                  (directory-files dir-path))
               (let
                   ((file-full-path
                     (expand-file-name file dir-path)))
                 (if
                     (file-directory-p file-full-path)
                     (if
                         (not
                          (string-match "^[.].*" file))
                         (recursive-access-dir file-full-path))
                   (if
                       (string-match ".*[.]h$" file)
                       (add-c-header dir-path))))))
           (recursive-access-dir "~/work/project/julius/")
           (setq flycheck-clang-include-path headers))))))

现在遇到一个新问题

flycheck-clang-include-path的路径都设置正确了

上图,/Users/dongguo/work/project/julius/libjulius/include/这个路径已经包含在flycheck-clang-include-path中

我用的是lsp ccls,<julius/juliuslib.h> 这个文件就在/Users/dongguo/work/project/julius/libjulius/include/目录下,为什么flycheck还是报找不到?

50

06

ccls不使用flycheck-clang-include-path,你应该明确地禁用它,否则,这个检查器可能会意外使用:

(setq-default flycheck-disabled-checkers’(c / c ++ - clang c / c ++ - cppcheck c / c ++ - gcc))

lsp-ui-flycheck会照顾你所有的包括路径

顺便说一句,你应该先阅读手册 lsp mode · MaskRay/ccls Wiki · GitHub

(你应该创建一个新帖子,因为ccls在这里是偏离主题的)

好的,谢谢,我明天有时间,看一下。

(关于ccls若遇到新问题,我再开一个新贴子)

在 .dir-locals.el 里写那么大段的代码并不妥。

首先 .dir-locals.el 就不是一个安全放置代码的地方,否则 Emacs 也不会询问是否执行,然后记录到 custom.el。

其次,头文件是哪里需要用的,为何不在需要用的时侯再找。放在 .dir-locals.el 里,打开任意文件(例如 README.txt)都会执行,没必要。

哦,我没有这么写了,只是想了解一下.dir-locals.el是干什么用的。