2

It seems like cc-mode indentation is working rather well, however I seem to have run into a minor issue when creating a few enums in C++.

Emacs seems to handle almost all kinds of enums rather well now, even the "enum class" types, however, for some reason the following one seems broken:

class Test
{
public:
    enum t1
    {
        T0 = 1,
            T1,   <----- ?
            T2 = 3
            };
};

Removing the "t1" identifier fixes the "incorrect" indentation, but the identifier is still desirable in some cases.

So, the the "correct" indentation would be:

class Test
{
public:
    enum t1
    {
        T0 = 1,
        T1,
        T2 = 3
    };
};

Is there any way of fixing this, and do anyone know if this is the intended behavior?

This occurs with GNU Emacs 25.0.50.4, with accompanying cc-mode. Running with --no-init-file produces the same results, although with gnu-style indentation.

Malabarba
  • 22,878
  • 6
  • 78
  • 163
Xaldew
  • 1,181
  • 9
  • 20
  • I did not have any problem with your whole enum. I'm using Emacs 24.4.1. Do you have any configuration related to C++ or indentation? – Tu Do Jan 27 '15 at 15:34
  • I do have plenty of C++ configuration, but running with --no-init-file produces the same results, although with gnu-style indentation instead. I'll see if I can replicate it 24.4 later on at home. Otherwise, I guess it's a bug in cc-mode then? – Xaldew Jan 27 '15 at 15:46
  • Maybe there's in development changes. Also, I'm using `linux` style by default. – Tu Do Jan 27 '15 at 16:35
  • Not an answer to your question, but if you don't mind relying on clang, `clang-format` is a wonderful tool. A small description [here](http://emacs.stackexchange.com/questions/4175/format-code-like-in-eclipse/4188#4188). – Pradhan Jan 27 '15 at 23:03
  • Answered in depth on Stack Overflow http://stackoverflow.com/questions/6497374/emacs-cc-mode-indentation-problem-with-c0x-enum-class - along with reference code for a partial fix. – Greg Jun 10 '15 at 03:11

2 Answers2

2
  • If it happens with emacs -Q, file a bug report with M-x bug-report.

  • If you verify that this doesn't happen on Emacs 24.4, you can even:

    • Send an email to the dev list. It's likely that the person who inadvertently introduced the bug will immediately know what's happening.
    • Open the ChangeLog file and search for changes to the affected package. There should be something related to indentation in there, and it will be easy to spot.
Malabarba
  • 22,878
  • 6
  • 78
  • 163
0

check http://blog.binchen.org/posts/ccjava-code-indentation-in-emacs.html

Simply put, Emacs will assign indention according to the c-syntax-context which is the key to query global tree c-offsets-alist, the query result is the indention offset.

Then you need hack the function c-indent-line which is defined in /usr/share/emacs/24.3/lisp/progmodes/cc-cmds.el to print out the actual value of c-syntax-context.

As I mentioned in the article, "M-x c-set-offset" will also print the context. The reason I don't use it because it's NOT reliable. There is nothing more reliable than hacking the actual code.

chen bin
  • 4,781
  • 18
  • 36
  • Isn't it easier to hit `C-c C-s` (= `c-show-syntactic-information`) ? – politza Jan 28 '15 at 11:22
  • "M-x c-set-offset" will also give the value of c-syntax-context. as mentioned in article. It basically does the same thing as c-show-syntactic-information. I don't remember details now, but "c-set-offset" does NOT work reliably, looks the command you recommended get the same result as c-set-offset by testing the two case in my article. – chen bin Feb 01 '15 at 05:13
  • `There is nothing more reliable than hacking the actual code.` - Can't argue with that. – politza Feb 01 '15 at 06:14