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?

  • Some screenshots would be nice... not everyone is familiar with Atom. – PythonNut Sep 19 '15 at 14:45
  • @PythonNut just added the screenshots – Hckr Sep 19 '15 at 14:59
  • ([a-z,A-Z,_]+[_]*[a-z,A-Z,1-9]*)+[(] seems okay for function calls. But I do not know how to add it as a new rule for cpp-mode – Hckr Sep 19 '15 at 18:11

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.

| improve this answer | |
  • 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 at 17:53

this seems to answer the member function bit of your question

(font-lock-add-keywords 'c++-mode
   "\\<[_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/

| improve this answer | |

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

;; C-Like
(dolist (mode-iter '(c-mode c++-mode glsl-mode java-mode javascript-mode rust-mode))
    '(("\\([~^&\|!<>=,.\\+*/%-]\\)" 0 'font-lock-operator-face keep)))
    '(("\\([\]\[}{)(:;]\\)" 0 'font-lock-delimit-face keep)))
  ;; functions
    '(("\\([_a-zA-Z][_a-zA-Z0-9]*\\)\s*(" 1 'font-lock-function-name-face keep))))
| improve this answer | |

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.