I need a little help with some Emacs syntax highlighting troubles. I’d like to set the colour of all C/C++ operators. I am having trouble because “/” is used for division (which I want to colour) but also used to start comments (which I don’t want to colour myself, the default code colours them fine.) I can’t seem to figure out how to tell Emacs to only colour a single forward-slash when it is not surrounded by any other forward slashes.
This is what I have now:
(add-hook 'c-mode-common-hook (lambda ()
(font-lock-add-keywords nil
'(("[<>:&*=+^%!~,.?;/-]" 0 font-lock-warning-face t)))
))
But it won’t correctly highlight something like this:
i = 1 / 2
// Test
i=1/2//test
Of course if there’s already a way of assigning a colour/face to punctuation/operators and I’ve just missed it please let me know!
Each font long entry has the following format:
The
tin your rule corresponds to theOVERRIDEentry. If you drop it, your rule will not re-highlight something that already has been highlighted, for example comments and strings.There is no general package for highlighting all operators, but
cwarn-modewill highlight=and+=etc. that are embedded inside larger expressions, as well as the;in situations where it shouldn’t be, likeif (condition);. It’s implemented using functions rather than regexp:s, and I consider it a good reference if you plan to implement relatively advanced font-lock rules.