Reputation: 43970
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.)
Upvotes: 20
Views: 1901
Reputation: 5668
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.
Upvotes: 3
Reputation:
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
Upvotes: 1
Reputation: 1741
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.
Upvotes: -3