I'm running Emacs 23.3.1 (Ubuntu, Oneiric package) and emacs doesn't appear to understand any of the new C++11 keywords, constexpr, thread_local, etc. Also it doesn't understand that '>>' is now permitted in template parameters, or the new 'enum class' syntax. Is there an updated or alternative module somewhere? Or failing that, some settings to make emacs more C++11 friendly in the mean time?
-
Note that `thread_local` is not yet supported by G++: http://gcc.gnu.org/projects/cxx0x.html The gcc extension `__thread` is similar but does not invoke constructors or destructors IIRC. – bdonlan Dec 18 '11 at 03:37
-
I had the same problem with vim, which have some syntax files I tuned. Must be the same for emacs I think :) – Jaffa Dec 20 '11 at 02:13
-
Emacs 26.1 highlights C++11-introduced keywords. I removed all hacks to get it. Of course, if you want C++20-introduced keywords to work, read on. – legends2k Dec 13 '19 at 10:35
6 Answers
Well, I'm using 24.1. Some C++98 keywords are missing, and all new C++11 keywords. It does not even fontify number constants. It seems as if c++-mode hasn't been updated for a decade.
I'm using the following code for a long time now, and recently added C++11 keywords. Try putting it in your .emacs; it should fill some holes.
(require 'font-lock)
(defun --copy-face (new-face face)
"Define NEW-FACE from existing FACE."
(copy-face face new-face)
(eval `(defvar ,new-face nil))
(set new-face new-face))
(--copy-face 'font-lock-label-face ; labels, case, public, private, proteced, namespace-tags
'font-lock-keyword-face)
(--copy-face 'font-lock-doc-markup-face ; comment markups such as Javadoc-tags
'font-lock-doc-face)
(--copy-face 'font-lock-doc-string-face ; comment markups
'font-lock-comment-face)
(global-font-lock-mode t)
(setq font-lock-maximum-decoration t)
(add-hook 'c++-mode-hook
'(lambda()
(font-lock-add-keywords
nil '(;; complete some fundamental keywords
("\\<\\(void\\|unsigned\\|signed\\|char\\|short\\|bool\\|int\\|long\\|float\\|double\\)\\>" . font-lock-keyword-face)
;; add the new C++11 keywords
("\\<\\(alignof\\|alignas\\|constexpr\\|decltype\\|noexcept\\|nullptr\\|static_assert\\|thread_local\\|override\\|final\\)\\>" . font-lock-keyword-face)
("\\<\\(char[0-9]+_t\\)\\>" . font-lock-keyword-face)
;; PREPROCESSOR_CONSTANT
("\\<[A-Z]+[A-Z_]+\\>" . font-lock-constant-face)
;; hexadecimal numbers
("\\<0[xX][0-9A-Fa-f]+\\>" . font-lock-constant-face)
;; integer/float/scientific numbers
("\\<[\\-+]*[0-9]*\\.?[0-9]+\\([ulUL]+\\|[eE][\\-+]?[0-9]+\\)?\\>" . font-lock-constant-face)
;; user-types (customize!)
("\\<[A-Za-z_]+[A-Za-z_0-9]*_\\(t\\|type\\|ptr\\)\\>" . font-lock-type-face)
("\\<\\(xstring\\|xchar\\)\\>" . font-lock-type-face)
))
) t)
Hope this helps.

- 7,568
- 4
- 43
- 34
-
5This is fantastic! Thank you for this. I'll be using this from now on. Have you thought about contributing this back to the project? – Jack Morrison Dec 21 '12 at 16:35
-
Maybe... however, who maintains cc-mode? The first author is RMS (1985), the last Alan Mackenzie (2003). I'll sent a mail to gnu.org; let's see what they say. – Andreas Spindler May 22 '13 at 08:10
-
1@AndreasSpindler: The last edit was most certainly not in 2003 -- the latest checkin to source was 2 weeks ago by Alan Mackenzie, see [the changelog](http://cc-mode.hg.sourceforge.net/hgweb/cc-mode/cc-mode/log). – Adam Rosenfield May 22 '13 at 19:41
-
Agreed. CC-Mode is under active development and Alan is very active on the mailing list: bug-cc-mode@gnu.org and there's a list archive here: http://news.gmane.org/gmane.emacs.cc-mode.general – MadScientist May 22 '13 at 20:16
-
2Ok, I saw this wrong in `cc-mode.el`. In the meantime Alan answered to my mail, saying that a small number of C++11 features have been added to the (yet unreleased) repository branch "C++11-0-1". I offered him my help, and he agreed. However, he also warned me that CC-Mode is not easy to understand... so it could take some time to translate the rather pragmatic hook-code into the real thing. – Andreas Spindler May 23 '13 at 13:36
-
Any support for the new raw string literal syntax? Emacs is having real trouble with something like `R"( a "quot'd" string )";` – Mike Weller May 31 '13 at 14:07
-
I was more worried about lambdas indentation and coloring. For syntax highlighting, there an easy add keyword stuff. I use it for numbers: `(font-lock-add-keywords 'c++-mode '(("\\(-?[0-9]+[.eE]?[0-9]*[f]?\\)" 1 font-lock-warning-face append)))` – v.oddou Nov 15 '13 at 03:26
-
1
-
Doesn't highlight variables properly when they're introduced with auto. I used ` ("auto ?&? +\\(\\w+\\)" 1 'font-lock-variable-name-face)`. – Átila Neves Nov 06 '14 at 13:51
-
1Hey, everyone-who-sees-this-question-or-answer: I'm maintaining the code on this commentary [here](https://github.com/FelipeLema/cpp1x-minor-mode) Pull requests welcome – Felipe Lema Mar 01 '16 at 14:06
According to a request by Mike Weller here an updated version for C++11 strings literals (incl. user-defined literals).
(add-hook
'c++-mode-hook
'(lambda()
;; We could place some regexes into `c-mode-common-hook', but note that their evaluation order
;; matters.
(font-lock-add-keywords
nil '(;; complete some fundamental keywords
("\\<\\(void\\|unsigned\\|signed\\|char\\|short\\|bool\\|int\\|long\\|float\\|double\\)\\>" . font-lock-keyword-face)
;; namespace names and tags - these are rendered as constants by cc-mode
("\\<\\(\\w+::\\)" . font-lock-function-name-face)
;; new C++11 keywords
("\\<\\(alignof\\|alignas\\|constexpr\\|decltype\\|noexcept\\|nullptr\\|static_assert\\|thread_local\\|override\\|final\\)\\>" . font-lock-keyword-face)
("\\<\\(char16_t\\|char32_t\\)\\>" . font-lock-keyword-face)
;; PREPROCESSOR_CONSTANT, PREPROCESSORCONSTANT
("\\<[A-Z]*_[A-Z_]+\\>" . font-lock-constant-face)
("\\<[A-Z]\\{3,\\}\\>" . font-lock-constant-face)
;; hexadecimal numbers
("\\<0[xX][0-9A-Fa-f]+\\>" . font-lock-constant-face)
;; integer/float/scientific numbers
("\\<[\\-+]*[0-9]*\\.?[0-9]+\\([ulUL]+\\|[eE][\\-+]?[0-9]+\\)?\\>" . font-lock-constant-face)
;; c++11 string literals
;; L"wide string"
;; L"wide string with UNICODE codepoint: \u2018"
;; u8"UTF-8 string", u"UTF-16 string", U"UTF-32 string"
("\\<\\([LuU8]+\\)\".*?\"" 1 font-lock-keyword-face)
;; R"(user-defined literal)"
;; R"( a "quot'd" string )"
;; R"delimiter(The String Data" )delimiter"
;; R"delimiter((a-z))delimiter" is equivalent to "(a-z)"
("\\(\\<[uU8]*R\"[^\\s-\\\\()]\\{0,16\\}(\\)" 1 font-lock-keyword-face t) ; start delimiter
( "\\<[uU8]*R\"[^\\s-\\\\()]\\{0,16\\}(\\(.*?\\))[^\\s-\\\\()]\\{0,16\\}\"" 1 font-lock-string-face t) ; actual string
( "\\<[uU8]*R\"[^\\s-\\\\()]\\{0,16\\}(.*?\\()[^\\s-\\\\()]\\{0,16\\}\"\\)" 1 font-lock-keyword-face t) ; end delimiter
;; user-defined types (rather project-specific)
("\\<[A-Za-z_]+[A-Za-z_0-9]*_\\(type\\|ptr\\)\\>" . font-lock-type-face)
("\\<\\(xstring\\|xchar\\)\\>" . font-lock-type-face)
))
) t)
In the above implementation of user-defined strings literals, the delimiter tags are marked up separately as font-lock-keyword-face
; another option would be font-lock-constant-face
. This implementation is not as efficient as it could be; but it works and does not slow down Emacs. Note that the regexps for user-defined strings literals have not been "stolen" from somehere; so I hope they work. Any comments are welcome.
If you like to fontify the whole literal string as font-lock-string-face
- including the delimiters - replace the three regexps by just one. Like this one:
.
.
("\\<\\([uU8]*R\"[^\\s-\\\\()]\\{0,16\\}(.*?)[^\\s-\\\\()]\\{0,16\\}\"\\)\\>" 1 font-lock-string-face t)
Have fun.

- 7,568
- 4
- 43
- 34
-
I like it this. However, it does not highlight multi-line string literals correctly. . – Maxim Egorushkin Jun 18 '13 at 14:39
-
Okay, it is because `.` does not match newline. Fixed it by replacing all `.*` with `[[:ascii:][:nonascii:]]*` – Maxim Egorushkin Jun 18 '13 at 15:19
-
Still, I don't understand why it highlights some string quoting symbols with red. [See this](http://i41.tinypic.com/16ld3iw.jpg). – Maxim Egorushkin Jun 18 '13 at 15:35
-
@MaximYegorushkin: you know you can close your string at the end of the line, reopen it on the next line, and its concatenated at compile time. – v.oddou Nov 15 '13 at 03:30
Have a look at the package : "Modern C++" font-lock for Emacs. It is also available on Melpa.
Syntax highlighting support for "Modern C++" - until C++17 and Technical Specification. This package aims to provide a simple highlight of the C++ language without dependency.
It is recommended to use it in addition with the c++-mode major mode for extra highlighting (user defined types, functions, etc.) and indentation.
I am the maintainer of this minor mode. Any feedback is appreciated.
Replacing Andreas' floating point regexp with this will improve hilighting of floats.
integer/float/scientific literals
("\\<[-+]?[0-9]*\\.?[0-9]+\\([uUlL]+\\|[eE][-+]?[0-9]+\\)?[fFlL]?\\>" . font-lock-constant-face)
Hope that helps someone.

- 53
- 1
- 5
I've checked trunk version, cc-mode
hasn't been updated yet, and AFAIK there's no alternative. If you really want it, but don't want to get your hands dirty, you should pay someone to implement it for you...

- 2,055
- 15
- 26
For me, the two most pressing pain points with font-locking of modern C++ code have been
- the fact that
auto
is highlighted as a keyword (and not a type) and thus the following identifier would normally not higlight as a variable declaration, and - that highlighting simply goes crazy when presented with some code (for an example, try rtags'
src/ClangIndexer.cpp
) and then e.g fails to highlight top-level constructs such as function definitions.
After a bit of experimentation, I arrived at a solution that works well for me and addresses both points.
The first one is achieved by modifying lisp/progmodes/cc-langs.el
(copying to one's load-path
and then modifying also works) to remove "auto"
from
(c-lang-defconst c-modifier-kwds
"Keywords that can prefix normal declarations of identifiers
and add it to c++-font-lock-extra-types
(e.g. via Customize).
For the second one, emptying c++-font-lock-extra-types
(except for keeping "auto"
) helps.

- 1
- 1