emacs - 为不同的项目设置不同的c-basic-offs

标签 emacs

我通常使用 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/

相关文章:

emacs - 在 Chrome OS 的终端中安装 Emacs

emacs - Emacs可以在后台运行命令吗

haskell - 用于 haskell 的 org-babel 不适用于 eval haskell block

json - 如何将 emacs 连接到 json 服务?

xcode - Emacs 与 XCode 对话

Emacs 组织模式发布议程

python - emacs 交互式 python shell 显示没有反馈

emacs - 为什么我在 Windows 7 上运行的 Cygwin 中的 Emacs 总是创建崩溃转储?

emacs - http-post-simple.el -- (lambda (field) ...) 引用为 ' rather than with #'

emacs ido-ignore-目录和文件不忽略所有列表