Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

c++ - Emacs cc-mode indentation problem with C++0x enum class

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the problem:

cc-mode relies somewhat on the assumption that keywords are single words. Adding support for enum_class instead of enum class would 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.)

(defun inside-class-enum-p (pos)
  "Checks if POS is within the braces of a C++ "enum class"."
  (ignore-errors
    (save-excursion
      (goto-char pos)
      (up-list -1)
      (backward-sexp 1)
      (looking-back "enum[ ]+class[ ]+[^}]+"))))

(defun align-enum-class (langelem)
  (if (inside-class-enum-p (c-langelem-pos langelem))
      0
    (c-lineup-topmost-intro-cont langelem)))

(defun align-enum-class-closing-brace (langelem)
  (if (inside-class-enum-p (c-langelem-pos langelem))
      '-
    '+))

(defun fix-enum-class ()
  "Setup `c++-mode' to better handle "class enum"."
  (add-to-list 'c-offsets-alist '(topmost-intro-cont . align-enum-class))
  (add-to-list 'c-offsets-alist
               '(statement-cont . align-enum-class-closing-brace)))

(add-hook 'c++-mode-hook 'fix-enum-class)

This is not heavily tested. ;)

How it works:

c-offsets-alist determines 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-p simply moves up to the previous brace and checks if the text before it is "enum class".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...