简体   繁体   中英

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. 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. How would I do it? Thanks!

You can use this el file for emacs : http://www.emacswiki.org/emacs/ppindent.el

You can find many info about emacs indentation here : 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. (See Indenting #defines )

That said, see Indent preprocessor directives as C code in emacs for a discussion about this. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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