将预处理程序指令缩进为emacs中的C代码

Joc*_*nde 4 c emacs coding-style indentation

默认情况下,Emacs不会缩进预处理器代码.我知道它的历史根源已经过时了.

但是,拥有大量#ifdef unindented的代码很难阅读.

所以我想让emacs自动缩进给我这样的东西:

void myfunc() {
    int foo;

    #ifdef BAR
    printf(foo);
    #endif

    return foo;
}
Run Code Online (Sandbox Code Playgroud)

而不是我现在得到的:

void myfunc() {
    int foo;

#ifdef BAR
    printf(foo);
#endif

    return foo;
}
Run Code Online (Sandbox Code Playgroud)

这个问题的任何线索你emacs黑客:)?

Rin*_*g Ø 12

您可以简单地告诉Emacs向预处理器线添加偏移量.

  • 将光标(point)放在预处理器行中
  • 然后按C-c C-o(control-c control -o)
  • 迷你应该说Syntactic symbol to change:,
  • 键入cpp-macro,按Enter
  • 输入新的偏移量(通常为数字0)

然后TAB每个预处理器行上的a应正确缩进.(或M-xindent-region...).

要永久更改设置,您可以在.emacs文件中添加所需的行.
复制先前输入的命令的简单方法是c-x ESC ESC使用箭头键查找(c-set-offset ...)Elisp命令.

那应该是

(c-set-offset (quote cpp-macro) 0 nil)
Run Code Online (Sandbox Code Playgroud)

  • 实际上,就是这样,但为了获得我想要的行为,我需要将偏移设置为0,而不是3. (2认同)