Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6100115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:20:58+00:00 2026-05-23T13:20:58+00:00

Emacs cc-mode does not appear to yet recognize the type-safe enum class introduced in

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:20:58+00:00Added an answer on May 23, 2026 at 1:20 pm

    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[ \t]+class[ \t]+[^}]+"))))
    
    (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”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The question does not refer to some Vim-mode in Emacs, but to Vim inside
Does anybody know a good Emacs mode to edit JSON? An app I am
Is there a mode in emacs that does syntax highlighting for the R programming
The Emacs cperl-mode seems to get confused less than perl-mode, but the Skittles effect
I have been using nxml-mode to edit XHTML. However, it does not work very
When I run emacsclient it does not respond to mouse clicks. My main Emacs
I'm a user of Aquamacs under OS X, which by default does not recognize
Excuse my emacs newbiness here, but does anybody know how to get around this?
When using ido-mode in emacs, it tends to get in my way when I'm
What is the Emacs mode or package that highlights Lisp forms changing the color

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.