我通常使用 4 个空格来缩进 C 程序,但为了与一些开源项目保持一致,有时我不得不改为 2 个空格缩进。
目前我的缩进样式是在我的 .emacs 文件中分配的
(setq c-basic-offset 4)
当我想处理那些 2-white-space 缩进项目时。我必须关闭我的 Emacs,修改值,然后重新开始。有没有更简单的方法来做到这一点?
非常感谢。
附言。 每次打开源文件都设置 c-basic-offset 变量太麻烦了,是否可以根据工作目录选择不同的值?
最佳答案
在您要自定义的目录中创建一个名为 .dir-locals.el
的文件,并编辑它以包含:
((c-mode . ((c-basic-offset . 4))))
注意:这是 Emacs 23.1 中的新功能。
这利用了 Per-Directory Local Variables .来自链接中的文档:
The
.dir-locals.el
file should hold a specially-constructed list. This list maps Emacs mode names (symbols) to alists; each alist specifies values for variables to use when the respective mode is turned on. The special mode name `nil' means that its alist applies to any mode. Instead of a mode name, you can specify a string that is a name of a subdirectory of the project's directory; then the corresponding alist applies to all the files in that subdirectory.Here's an example of a
.dir-locals.el
file:((nil . ((indent-tabs-mode . t) (tab-width . 4) (fill-column . 80))) (c-mode . ((c-file-style . "BSD"))) (java-mode . ((c-file-style . "BSD"))) ("src/imported" . ((nil . ((change-log-default-name . "ChangeLog.local"))))))
关于emacs - 为不同的项目设置不同的c-basic-offs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1824256/