Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax highlighting of C function calls in emacs

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!

like image 348
Rumpsteak Avatar asked Dec 20 '10 14:12

Rumpsteak


2 Answers

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))))
like image 161
Gilles 'SO- stop being evil' Avatar answered Oct 18 '22 01:10

Gilles 'SO- stop being evil'


(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).

like image 44
AProgrammer Avatar answered Oct 18 '22 01:10

AProgrammer