I am using the standard (shipped) Emacs C++ mode but I have a slight itch that I am looking to get scratched. How would I go about properly highlighting the types inside of a sizeof and the casts in C++?
For example:
-
A Cast
Type * pointer = reinterpret_cast <Type *> (original); -
Sizeof
std::cout << sizeof (Type) << "\n";
Add these expressions to your
.emacs, or evaluate them withM-:.Sizeof (that’s the easier of the two cases)
The regex highlights any combination (indicated by the bracket expression
[...]; regarding syntax, please see the note below) of alphanumeric, whitespace and asterisk within parentheses and preceded bysizeof.The number
1tells emacs to only highlight the first subexpression (marked by\\(...\\)) using the facefont-lock-type-face;tmeans overriding any previous highlighting.You can see and change the available faces with
M-x customize group [RET] font-lock-faces [RET].C++ style casts
I’m not sure which typename you want to be highlighted – the “original” or the one to cast into. This highlighter marks both:
Again, ‘1
and2` select the corresponding subexpressions.Please note: The regexes for the typenames do not perfectly conform to C++ syntax. For example, emacs will happily highlight nonsense like
sizeof(int * 32).Also, my solution doesn’t take into account the problem Pavel mentioned in the comment on your question; that you could also use
sizeofon variables, which would need different highlighting. I don’t think this possible, short of implementing a complete C parser in the font lock code.