6

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!

colding
  • 563
  • 1
  • 5
  • 15

2 Answers2

4

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

ClemPi
  • 316
  • 1
  • 4
  • ppindent "indents" by putting space between the '#' and the 'ifdef'. I think that makes it harder to read. I want to keep the '#ifdef' together and indent by putting whitespace before the '#'. – colding Feb 02 '12 at 22:48
  • Ok, I made some modification to the lisp file : [here](http://pastebin.com/uUqYpCua), I hope this time is what you need. – ClemPi Feb 03 '12 at 19:52
  • That is a lot closer to what I need. Now, without having tried it myself yet, how would it indent when the #ifdef is in-between normal code? Would it indent irrespective the surrounding code or would it follow the flow so to speak? – colding Feb 03 '12 at 21:57
  • I don't know, I just made this for you without testing it in many case. – ClemPi Feb 03 '12 at 22:02
  • OK, I'll take a closer look at it tomorrow when I've had som sleep. Thanks. – colding Feb 03 '12 at 22:40
  • Tried it. It's complaining about "if: Symbol's function definition is void: minusp". That's on line 98. How'd I fix that? – colding Feb 04 '12 at 11:35
  • OK, (require 'cl) did the trick. ppindent works well for ifdef'ed include statements and such, but it does not work when within code... Anyways, halfway there :-) – colding Feb 04 '12 at 11:50
3

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.

Community
  • 1
  • 1
Arafangion
  • 11,517
  • 1
  • 40
  • 72
  • 2
    I think "are supposed to start" is not correct. According to current standards, they are not. Maybe you want to replace it by "were supposed to start"? That doesn't mean that I don't agree with you on the point that they *should* start at the beginning from a consistency/maintainability point of view. – Niklas B. Feb 02 '12 at 22:36
  • @ruakh: Thanks. Damn, I need coffee. – Arafangion Feb 02 '12 at 22:42
  • It is not a duplicate of that question. What I want is different. The OP of the other question only wanted his #ifdefs to be intended as code, I want them to be intended as curly braces and the inclosed code to be indented one additional level as if they were curly braces. It will, especially for include statements, increase readability a lot. Well, at least in my eyes ;-) – colding Feb 02 '12 at 22:43
  • @colding: You may want to ensure that preprocessor indentation occurs independently of C indentation, and specify that in the question. :) Perhaps have #ifdef A #ifdef B void foo() { #ifdef C int c #endif... as an example? – Arafangion Feb 02 '12 at 22:46