如何让 emacs 像'{'和 '}' 一样对待 #ifdef 和 #endif?

How do I make emacs treat #ifdef and #endif like '{' and '}'?

本文关键字:一样 #endif #ifdef emacs      更新时间:2023-10-16

我希望emacs对待"#ifdef"就像对待缩进的"{"answers"#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宏"并不能起到什么作用。我该怎么做?谢谢

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

你可以在这里找到许多关于emacs缩进的信息:http://www.emacswiki.org/emacs/IndentingC

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

也就是说,有关这方面的讨论,请参阅emacs中的C代码缩进预处理器指令。事实上,我可能会尝试将这个问题作为重复问题来结束。

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