7

In atom, C++ files are well colored unlike emacs cpp-mode. Emacs does not highlight references to member variables, function calls or member functions. They all are uncolored.

The left is from emacs and the other is from atom:

Syntax coloring in emacs Syntax coloring in atom See, emacs does not syntax-color for variables and functions apart from their declarations.

Emacs does not utilize a semantic syntax highlighting system, neither does atom. I think such highlighting can be easily added using regexp like in atom.

Atom, for example, looks for parantheses to distinguish function calls from variables. Similarly it looks for a dot to distinguish member variables from the others.

I would like to add these coloring rules to emacs, however, I am just a beginner who does not know regexp and font-lock-mode.

Could you help me with this issue?

Hckr
  • 171
  • 1
  • 4

3 Answers3

12

The Emacs one is actually better. Here's why, The purpose of syntax highlighting in text editors is not being pretty, but to make important code structures stand out.

If you look at the Emacs sample, you'll see 'MyClass' being colored in the 'type name' color and 'obj' in 'variable' color, which makes the important information that you have one variable 'obj' of type 'MyClass' in the current scope really stand out.

If you happen to have other local variables, member variables or function parameters, you'll see them colored in similar way. I personally think this helps a lot because I can easily see where are these variables declared and where are they instantiated in a glance.

The logic behind Emacs' syntax highlighting is that it doesn't highlight where you reference(use) symbols, instead, it only highlights where symbols are defined or declared. These are much more important and informative then symbols being used. Let's face it, other than declarations and keywords, almost everything else is some symbols being referenced. Coloring these only makes the screen messier and the really important things less obvious.

finalpatch
  • 121
  • 1
  • 3
  • 1
    This must have been the best written "first post" in the history of Stack Exchange! I tip my hat! Welcome! – Lindydancer Feb 12 '16 at 12:20
  • I dig your comment, however, I find function calls inside other functions something that is worth highlighting but none of the emacs packages I came across does that. – SFbay007 Jan 14 '20 at 17:53
  • 1
    this is purely an opinion and does not answer the question. – 12Me21 Dec 15 '20 at 21:21
  • I don't see why this gets the highest vote, the question asks help to 'add' colors, not to change everything over to that like atom. You can have all those declarations color-coded in addition to the functions and variables being colored as well. Knowing easily where objects are declared is helpful, and knowing easily the sequences of operations is not less helpful either. – nougako Feb 23 '21 at 16:45
6

this seems to answer the member function bit of your question

(font-lock-add-keywords 'c++-mode
 `((,(concat
   "\\<[_a-zA-Z][_a-zA-Z0-9]*\\>"       ; Object identifier
   "\\s *"                              ; Optional white space
   "\\(?:\\.\\|->\\)"                   ; Member access
   "\\s *"                              ; Optional white space
   "\\<\\([_a-zA-Z][_a-zA-Z0-9]*\\)\\>" ; Member identifier
   "\\s *"                              ; Optional white space
   "(")                                 ; Paren for method invocation
   1 'font-lock-function-name-face t)))

source: https://www.reddit.com/r/emacs/comments/27eqwm/highlighting_c_member_function_calls/

1

Adding this to my init.el gives fairly good syntax highlighting for function calls and operators, it doesn't handle members though.

This highlights:

  • operators + - ... = * .. etc.
  • brackets [{()}] (as delimiters), as well as ;:
  • function_call().

Using simple regex, I find this gives nicer highlighting for C like languages.

;; C-Like
(dolist (mode-iter '(c-mode c++-mode glsl-mode java-mode javascript-mode rust-mode))
  (font-lock-add-keywords
    mode-iter
    '(("\\([~^&\|!<>=,.\\+*/%-]\\)" 0 'font-lock-operator-face keep)))
  (font-lock-add-keywords
    mode-iter
    '(("\\([\]\[}{)(:;]\\)" 0 'font-lock-delimit-face keep)))
  ;; functions
  (font-lock-add-keywords
    mode-iter
    '(("\\([_a-zA-Z][_a-zA-Z0-9]*\\)\s*(" 1 'font-lock-function-name-face keep))))
ideasman42
  • 8,375
  • 1
  • 28
  • 105
  • Can you please add a brief explanation of the three main components of the above code? E.g. what they are for etc. – nougako Feb 23 '21 at 16:47