Reputation: 573
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
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
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