在emacs中突出显示C函数调用的语法

Rum*_*eak 6 c emacs syntax-highlighting

我在emacs中使用C语言的自定义语法高亮主题,但我错过了突出显示函数调用的可能性.例如:

int func(int foo)
{
    return foo;
}

void main()
{
    int bar = func(3);
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在这个例子中突出显示对"func"的调用?如果宏也被突出显示也没关系.if,switch或sizeof等关键字不匹配.

谢谢!

Gil*_*il' 9

关键字列表中的条目顺序很重要.因此,如果您将条目放在突出显示关键字和函数声明的条目之后,则这些条目将不匹配.

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

或者,您可以使用函数而不是正则表达式作为MATCHER.如果你已经完全陈述了你的要求,那么你的问题会有些过分,但在更难的情况下会有用.未经测试(直接在浏览器中输入,实际上,所以我甚至不保证平衡的括号).

(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))))
Run Code Online (Sandbox Code Playgroud)