colding
colding

Reputation: 573

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!

Upvotes: 6

Views: 1786

Answers (2)

Arafangion
Arafangion

Reputation: 11940

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.

Upvotes: 4

ClemPi
ClemPi

Reputation: 326

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

Upvotes: 5

Related Questions