Emacs cc-mode does not appear to yet recognize the type-safe enum class introduced in C++0x. The result I get is a double indentation for second, third, etc enums:
enum class Color {
Blue,
Red,
Orange,
Green
};
What I would like is:
enum class Color {
Blue,
Red,
Orange,
Green
};
Can you recommend a good command to add to .emacs which will make cc-mode treat enum class the same way it treats the plain old enum?
This is the problem:
cc-mode relies somewhat on the assumption that keywords are single words. Adding support for
enum_classinstead ofenum classwould just be a matter of changing a few regexps.Instead Emacs treats this as a class. The correct way of solving this would be teaching Emacs that this is an enum. But that’s beyond the scope of an answer.
This is the hack:
So we’ll modify the existing indentation to behave differently in this one case. (Code available for tinkering in this gist.)
This is not heavily tested. 😉
How it works:
c-offsets-alistdetermines the indentation for different positions in the syntax tree. It can be assigned constants or functions.These two functions find out whether the current position is inside the
enum class {...}. If that’s the case, they return 0 or ‘-, which cc-mode interprets as an indentation depth. If it isn’t, they return the default value.inside-class-enum-psimply moves up to the previous brace and checks if the text before it is “enum class”.