如何使emacs对待#ifdef和#endif如'{'和'}'?

时间:2012-02-02 22:22:03

标签: c++ c emacs indentation

我希望emacs将“#ifdef”与“缩进”相关的“{”和“#endif”一样对待“}”。像这样:

#ifdef __linux__
    #include <sys/socket.h>
#endif

int func(void)
{
    int foo = 0;

    #ifdef DO_STUFF
        foo = do_stuff();
    #endif

    return foo;
}

而不是:

#ifdef __linux__
#include <sys/socket.h>
#endif

int func(void)
{
    int foo = 0;

#ifdef DO_STUFF
    foo = do_stuff();
#endif

    return foo;
}

用“cpp-macro”搞乱不行。我该怎么办?谢谢!

2 个答案:

答案 0 :(得分:4)

您可以将此el文件用于emacs: http://www.emacswiki.org/emacs/ppindent.el

您可以在此处找到有关emacs缩进的许多信息:http://www.emacswiki.org/emacs/IndentingC

答案 1 :(得分:3)

预处理器注释应该从第一列开始,所以emacs在那里是正确的,但是现在编译器通常允许它们缩进。 (见Indenting #defines

那就是说,请参阅Indent preprocessor directives as C code in emacs进行讨论。事实上,我可能会尝试将此问题作为副本重复。

我同意关于该问题的一些评论,因为将预处理器视为块或词法范围是错误的,因此以与使用常规处理器相同的方式缩进它实际上是有害的C代码。

相关问题