Search code examples
emacselispauto-indent

How can I fix emacs indentation of C++ initializers?


Emacs doesn't properly indent C++ class definitions for allocators that have initializers with colons in them. I think that this is because lines with colons are left-indented in many cases.

I would like to fix this.

Here is an example of what I am talking about.

EMACS indents the code like this:

class demo {
    int x;
    demo(){
    }
 demo(int y):x(y){
    };
};

But it should really indent it like this:

class demo {
    int x;
    demo(){
    }
    demo(int y):x(y){
    };
};

Is there a way to fix this behavior? Presumably we need some elisp...

Thanks!


Solution

  • Emacs (at least version 23) doesn't do this in C mode, but it does in C++ mode since in C the part before the colon can only be a label. Make sure you're in C++ mode (M-x c++-mode).