views:

487

answers:

2

I'm trying to get emacs to correctly format the "for each" construct in c++.

I want the braces to be lined up with the f in for in both of the following examples:

for each(Type a in b)
        { //^c^s shows substatement-open
            //... do stuff
        }

for( ; ; )
{ //^c^s shows substatement-open
    //... do stuff
}

In both cases, using [Ctrl-c Ctrl-s] (or [Ctrl-c Ctrl-o]) shows that both opening { characters are of type substatement-open and when viewing the c-offsets-alist I see that substatement-open is equal to 0.

How can I make emacs indent the for each() command similar to how it indents the for(;;) command?

+4  A: 

Presumably emacs is not recognising "for each" as c++ syntax (I don't either. Is this a microsoft extension? A preprocessor hack? New for the upcoming standard?). So there is no wonder that it is not formatting it "right".

You could hack the mode, or ask the maintainer of the same (I wouldn't expect a possitive response, though, unless this is going to be proper c++ Very Soon Now).


From the comments The answers to my parenthetical questions are:

  • The precise form the OP asks about is a Microsoft managed-c++ extension.
  • There is BOOST_FOREACH available now
  • The C++0x stand will include a container-ranged loop with a syntax like for( <type> <loopcounter> : <container> )

and from slink's answer it appears that the mode maintainer is supporting the Microsoft syntax in the latest versions...

dmckee
It looks like there's a range-based for loop in C++0x, but this isn't the syntax for it. http://en.wikipedia.org/wiki/C%2B%2B0x#Range-based_for-loop
Fred Larson
It's a MS Visual Studio extension: http://blogs.msdn.com/arich/archive/2004/09/08/227139.aspx
Alex B
http://www.boost.org/doc/libs/1_35_0/doc/html/foreach.htmlBoost for each is a portable alternative, and it should format correctly because it looks like valid C++ unlike 'for each'
justinhj
+2  A: 

For me--under Emacs 23.0.91--the c++-mode indents the for each block as it was a normal for loop.

Török Gábor
I'm using emacs 22.3.1 because that is the latest version on Windows. Great point though.
Alex B
I am using the latest version of cc-mode (5.31.6)
Alex B