6
votes

I use a custom syntax highlighting theme for C in emacs, but I'm missing the possibility of highlighting function calls. For example:

int func(int foo)
{
    return foo;
}

void main()
{
    int bar = func(3);
}

Is there any way to highlight the call to "func" in this example? It doesn't matter if macros are highlighted too. Keywords like if, switch or sizeof should not match.

Thanks!

3

3 Answers

9
votes

The order of entries in the keyword list is significant. So if you put your entries after the ones that highlight keywords and function declarations, these won't be matched.

(font-lock-add-keywords 'c-mode
  '(("\\(\\w+\\)\\s-*\("
    (1 rumpsteak-font-lock-function-call-face)))
  t)

Alternatively, you can use a function instead of a regexp as the MATCHER. Overkill for your question if you've stated your requirements exactly, but useful in harder cases. Untested (typed directly in the browser, in fact, so I don't even guarantee balanced parentheses).

(defun rumpsteak-match-function-call (&optional limit)
  (while (and (search-forward-regexp "\\(\\w+\\)\\s-*\(" limit 'no-error)
              (not (save-match-data
                     (string-match c-keywords-regexp (match-string 1))))
              (not (save-excursion
                     (backward-char)
                     (forward-sexp)
                     (c-skip-whitespace-forward)
                     (or (eobp) (= ?\{ (char-after (point)))))))))
(font-lock-add-keywords 'c-mode
  '((rumpsteak-match-function-call
    (1 rumpsteak-font-lock-function-call-face))))
3
votes
(font-lock-add-keywords 'c-mode
                   '(("\\<\\([a-zA-Z_]*\\) *("  1 font-lock-keyword-face)))

in your .emacs. Replace font-lock-keyword-face by the one you want (M-X list-faces-display to get a list of predefined one).

0
votes

You can try Ctrl-s for searching or Ctrl-r for searching backward. Emacs will highlight your function for you.