emacs - 在 .dir-locals.el 中定义新变量

标签 emacs elisp

我是 emacs 的新手。如何在 .dir-locals.el 中定义和使用新变量?

这是我的 .dir-locals.el,但它不起作用。

(setq clang_args '("-isystem/usr/include/glib-2.0"
                   "-isystem/usr/lib/glib-2.0/include"))
((c-mode . ((company-clang-arguments . clang_args)
            (flycheck-clang-args . clang_args))))

这有效
((c-mode . ((company-clang-arguments . ("-isystem/usr/include/glib-2.0"
                                        "-isystem/usr/lib/glib-2.0/include"))
            (flycheck-clang-args . ("-isystem/usr/include/glib-2.0"
                                    "-isystem/usr/lib/glib-2.0/include")))))

根据来自 How do I set buffer local variable from Eval: in .dir-local.el? 的示例,我尝试了其他方法但失败了。
((c-mode . ((eval . (setq company-clang-arguments ("-isystem/usr/include/glib-2.0"
                                                   "-isystem/usr/lib/glib-2.0/include"))
            (eval . (setq flycheck-clang-args ("-isystem/usr/include/glib-2.0"
                                               "-isystem/usr/lib/glib-2.0/include"))))))


((c-mode . ((eval . (setq clang_args ("-isystem/usr/include/glib-2.0"
                                      "-isystem/usr/lib/glib-2.0/include"))
            (company-clang-arguments . clang_args)
            (flycheck-clang-args . clang_args))))

最佳答案

您可以使用特殊关键字 eval执行任意 lisp 代码,例如,定义一个临时变量。但是,您不能在后续的 alist 样式变量声明中使用该变量。也就是说,以下不起作用:

((c-mode . ((eval . (setq clang-args ("-isystem/usr/include/glib-2.0"
                                      "-isystem/usr/lib/glib-2.0/include"))
            (company-clang-arguments . clang-args)
            (flycheck-clang-args . clang-args))))

因为在设置时 clang-argssetq 中取得成功形式,下面的两个变量定义分配符号clang-argscompany-clang-argumentsflycheck-clang-args分别不是变量clang-args的值先前定义。
不过,有两个选项确实有效:

1) 两次使用相同的字符串文字:

((c-mode . ((company-clang-arguments . ("-isystem/usr/include/glib-2.0"
                                        "-isystem/usr/lib/glib-2.0/include"))
            (flycheck-clang-args . ("-isystem/usr/include/glib-2.0"
                                    "-isystem/usr/lib/glib-2.0/include")))))

2) 在 eval 中也进行实际的变量赋值形式:

((c-mode . ((eval . (let ((clang-args '("-isystem/usr/include/glib-2.0"
                                        "-isystem/usr/lib/glib-2.0/include")))
                      (setq company-clang-arguments clang-args
                            flycheck-clang-args clang-args))))))

在我看来,第一个更具可读性,而第二个可能更易于维护,因为只需在一个位置更改值。

附录:请注意以下评论 phils : 他的建议是更换setsetq-local在第二个选项中。这将导致以下结果:

((c-mode . ((eval . (let ((clang-args '("-isystem/usr/include/glib-2.0"
                                        "-isystem/usr/lib/glib-2.0/include")))
                      (setq-local company-clang-arguments clang-args
                                  flycheck-clang-args clang-args))))))

关于emacs - 在 .dir-locals.el 中定义新变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33063008/

相关文章:

regex - 在 lisp 中使用替换正则表达式的问题

正则表达式(PCRE 或 Emacs): Repetition of previously defined group

emacs - 无法向 SLIME 发送有趣的字符

Emacs 就地文件名/路径插入

emacs - elisp中如何确定操作系统?

elisp - 如何使用 elisp 更改缓冲区中的单词?

emacs - 在缓冲区中添加时间

linux - Emacs...作为您的默认 shell ?

emacs - Clojure 的线程在 Emacs clojure-repl 模式下不显示结果

emacs - 如何区分点的不同叠加