3

I'd like to highlight function calls in C code, eg:

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

However this will highlight switch (, if (, while ( ... etc. as well as text in comments.

Besides explicitly excluding all keywords in the regex above, is there a way to only highlight text which does not already have any highlighting applied?

5

Yes, you can tell font-lock to apply some syntax highlighting only if the matched text is not already highlighted by font-lock.

To do this, you use keep for OVERRIDE in the match expression:

(MATCHER SUBEXP FACESPEC [OVERRIDE [LAXMATCH]])

See the Elisp manual, node Search-based Fontification. There you will see OVERRIDE described this way:

If OVERRIDE is t, this element can override existing fontification made by previous elements of font-lock-keywords.

If it is keep, then each character is fontified if it has not been fontified already by some other element.

If it is prepend, the face specified by FACESPEC is added to the beginning of the font-lock-face property.

If it is append, the face is added to the end of the font-lock-face property.

  • Works! solution: (font-lock-add-keywords c-mode `((,(concat "\\([_a-zA-Z][_a-zA-Z0-9]*\\)" "\\s *" "(") 1 'font-lock-function-name-face keep)))) – ideasman42 Dec 3 '17 at 0:17

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.