20

Now that C++11 is out, I wondered if there are any tips on improving indentation support in Emacs when more and more code is migrated from C++98 to C++11.

Examples:

Here are some questionable indentions that I find myself working around:

struct m {
    int a;
    char b;
};

std::vector<m> foo { {1, 'a'},
        {2, 'b'},
            { 3, 'c'},
                { 4, 'd' }};

I'd prefer

std::vector<m> foo { {1, 'a'},
                     {2, 'b'},
                     { 3, 'c'},
                     { 4, 'd' }};

or even

std::vector<m> foo { {1, 'a'},
        {2, 'b'},
        { 3, 'c'},
        { 4, 'd' }};

for example.

Next one:

cout << 5
     << [](int a) {
    return 2 * a;
} (5);

I'd prefer

cout << 5
     << [](int a) {
            return 2 * a;
        } (5);

so that the block is indented relative to the lambda.

I find myself spending more time on indentation, which is annoying.

Are any packages or customizations that help to indent modern C++11 code?

(side note: I set up clang-format for Emacs, but I cannot get 100% compatibility which existing code and it also does not yet understand C++11 syntax very well. Still it is useful sometimes and sounds like a good idea for new projects.)

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
  • I also encountered a related issue [here](http://emacs.stackexchange.com/questions/14142/support-c11-syntax-in-c-mode). I wonder if you have found any solution. Thanks. – Hot.PxL Jul 21 '15 at 18:12
  • @Hot.PxL Not really. Migrating to clang-format is possibly the best approach. – Philipp Claßen Jul 21 '15 at 19:08
  • Thanks. I'm looking in the same way. Is there any way to suggest a number of initial indentation when I hit enter and creates a new line? Is `clang-format` only capable of indenting existing lines? – Hot.PxL Jul 22 '15 at 03:14
  • 1
    It would be helpful to see some concrete code examples that compare (a) how Emacs currently indents that code and (b) how you imagine it should be indented. – Thomas Dec 06 '15 at 11:34
  • @Thomas Toby added some examples, which illustrate the problematic cases. – Philipp Claßen Dec 07 '15 at 19:11

3 Answers3

3

I just manually installed the latest CC Mode 5.33 from SourceForge, which should cover most of what you are looking for:

C++11 should now be fully supported, along with some features of C++14:

  • Uniform initialisation
  • Lambda functions
  • Parameter packs
  • Raw strings
  • Separators in integer literals
  • ">>" as a double template ender
  • etc.

Here are the indentations I get for your examples:

struct m {
  int a;
  char b;
};

std::vector<m> foo { {1, 'a'},
                     {2, 'b'},
                     { 3, 'c'},
                     { 4, 'd' }};

and

cout << 5
     << [](int a) {
          return 2 * a;
        } (5);

I also recommend installing modern-c++-font-lock (e.g. via MELPA) as suggested in this SO answer.

Community
  • 1
  • 1
mindriot
  • 5,413
  • 1
  • 25
  • 34
1

Have a look at ClangFormat:

ClangFormat describes a set of tools that are built on top of LibFormat. It can support your workflow in a variety of ways including a standalone tool and editor integrations.

It is integrated in emacs:

(load "<path-to-clang>/tools/clang-format/clang-format.el")
(global-set-key [C-M-tab] 'clang-format-region)

You have many options to define your indent style for C++11 and beyond.

Clang-Format Style Options describes configurable formatting style options supported by LibFormat and ClangFormat.

Few examples:

  • AlignTrailingComments
  • AlwaysBreakTemplateDeclarations
  • etc.
  • While the link you provided may answer the question it is best to put the essential parts of your solution directly in your answer in case the page at the link expires in the future. – Kmeixner May 26 '16 at 20:34
  • Actually, I am using ClangFormat (see my last paragraph). Over the time, it will understand modern syntax more and more. – Philipp Claßen May 27 '16 at 08:20
-3

When I'm using emacs, the following setting is good enough for me to indent code:

; auto indent   
(setq-default indent-tabs-mode nil)
(setq-default tab-width 2)

(define-key global-map (kbd "RET") 'newline-and-indent)
(defun indent-buffer ()
  (interactive)
  (save-excursion
    (indent-region (point-min) (point-max) nil)))
(global-set-key [f12] 'indent-buffer)

Hope it helps.

tobe
  • 1,671
  • 2
  • 23
  • 38
  • 3
    I'm not looking for to be rude.. but it doesn't answer the topic. The guy(like me) is looking for support all C++11 syntax and it doesn't, right? – The Mask Mar 08 '14 at 17:12