4

Question

Pretty straightforward: How to highlight constexpr in cc-mode (editing C++ code)?

Example

If I use const as in the following example

const double = 3.14

const gets highlighted as a keyword(1) (i.e., in pink), while if I write

constexpr double = 3.14

constexpr gets highlighted as all-faces (i.e., no highlighting at all).

Ideally, a simple solution like writing

(add-to-keywords "constexpr") ;; pseudo-code

in my .emacs without tinkering with the code of cc-mode would be the best for me.

Addendum

This question might be related: Change the way CC-mode font-lock the C++11 auto syntax.

Note

(1) I think that it is a keyword because placing the pointer on the word and doing M-x customize-face RET I get: Customize face (default ``font-lock-keyword-face'): and pressing RET I get redirected to the “Easy Customization” wizard.

3

The incantation you want is:

(font-lock-add-keywords 'c++-mode
                        '(("constexpr" . 'font-lock-keyword-face)))
  • 1
    It's dotted pair syntax for a cons cell. If you leave out the "." the keywords get interpreted differently. See the help for font-lock-keywords for what a "non-dotted" list would mean. – erikstokes Mar 2 '15 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.