简体   繁体   English

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

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

I'd like emacs to treat "#ifdef" exactly like "{" and "#endif" like "}" in relation to indentation. 我希望emacs将“#ifdef”与缩进相对应,就像“{”和“#endif”一样对待“}”。 Like so: 像这样:

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

int func(void)
{
    int foo = 0;

    #ifdef DO_STUFF
        foo = do_stuff();
    #endif

    return foo;
}

instead of: 代替:

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

int func(void)
{
    int foo = 0;

#ifdef DO_STUFF
    foo = do_stuff();
#endif

    return foo;
}

Messing around with "cpp-macro" doesn't do the trick. 用“cpp-macro”搞乱是不行的。 How would I do it? 我该怎么办? Thanks! 谢谢!

You can use this el file for emacs : http://www.emacswiki.org/emacs/ppindent.el 您可以将此el文件用于emacs: http//www.emacswiki.org/emacs/ppindent.el

You can find many info about emacs indentation here : http://www.emacswiki.org/emacs/IndentingC 您可以在此处找到有关emacs缩进的许多信息: http//www.emacswiki.org/emacs/IndentingC

Preprocessor comments are were supposed to start in the first column, so emacs is correct there, however these days compilers typically allow them to be indented. 预处理 意见 被认为在第一列开始,所以Emacs是正确的存在,但这些天的编译器通常允许它们被缩进。 (See Indenting #defines ) (参见缩进#defines

That said, see Indent preprocessor directives as C code in emacs for a discussion about this. 也就是说,请参阅emacs中的Indent预处理程序指令作为C代码,以便对此进行讨论。 Infact, I might try and close this question as a duplicate of that. 事实上,我可能会尝试将此问题作为副本重复。

I agree with some of the comments on that issue, in that it is a mistake to think of the preprocessor as being block or lexically scoped, so it is actually harmful to indent it in the same way as you do with the regular C code. 我同意关于该问题的一些评论,因为将预处理器视为块或词法范围是错误的,因此以与使用常规C代码相同的方式缩进它实际上是有害的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM