I am working with the C-source code of Emacs using the built-in c-mode, and would like Emacs to automatically highlight functions and variables within comments with a different color: In the following example, how can I make pos-visible-in-window-p a different color?

/* See the doc-string for `pos-visible-in-window-p'.  */
  • Here's a start: (font-lock-add-keywords 'c-mode '(("`\\([a-z-]+\\)'" 1 'font-lock-function-name-face prepend))). This does not check if `foo' is in a comment, though. Commented Feb 6, 2016 at 21:24
  • @Constantine -- my best guess would be that a modification of the function c-font-lock-doc-comments might be appropriate. However, the method used by c-mode to achieve its highlighting is quite complex.
    – lawlist
    Commented Feb 6, 2016 at 21:39
  • @lawlist are you looking for a way to find functions and variables? or, highlight anything within `' is ok? In the letter case, @Constantine 's answer is good enough, no? Commented Feb 6, 2016 at 22:42
  • @Yasushi Shoji -- I have grown accustomed to how the emacs-lisp-mode highlights functions and variables within comments, and find that I really miss that visual behavior when working in c-mode. The answer of @Constantine is essentially a textbook example from font-lock class 101. I'm most interested in a spiffy modification of the syntactic functions used by c-mode to achieve the desired effect. I am already familiar with the find features for functions and variables.
    – lawlist
    Commented Feb 6, 2016 at 22:51
  • @lawlist: Yes, the doc-comment machinery seems like the right place. (If we're lucky, adding one more style (in addition to gtkdoc, autodoc, and javadoc) would be enough -- it would be nice to leave the logic of c-font-lock-doc-comments alone). Also, I just realized that one could modify bug-reference-mode to achieve something similar. (That way we could easily have clickable buttons that call 'describe-function` or describe-variable.) Commented Feb 7, 2016 at 0:08

1 Answer 1


This seems to work for me:

(require 'cc-mode)

(setq yashi-font-lock-doc-comments
  (let ((symbol "[a-zA-Z0-9_-]+"))
    `((,(concat "`" symbol "'")
       0 ,c-doc-markup-face-name prepend nil))

(setq yashi-font-lock-keywords
  `((,(lambda (limit)
        (c-font-lock-doc-comments "/\\*" limit

(add-hook 'c-mode-hook
          (lambda ()
            (setq c-doc-comment-style '((c-mode . yashi)))))
  • The following test was performed with both Emacs 24.5 and also with the master branch: I started with Emacs -Q. I pasted the answer into the *scratch* buffer and typed M-x eval-buffer RET. Then I opened a new buffer foo.c, which defaults to c-mode. I typed: /* See the doc-string for [backtick]pos-visible-in-window-p'. */ The function pos-visible-in-window-p did not receive any different color than the rest of the comment -- i.e., the function is font-lock-comment-face. I verified the face by placing my cursor on the function and typing: C-u C-x =
    – lawlist
    Commented Feb 7, 2016 at 6:13
  • @lawlist sorry about bugs. First of all I've only tested with Emacs master branch. Second, I thought C can't have a symbol foo-bar, so I committed - from symbol regexp. Third, I left /**\n as the start of comment. I've removed it too. Would you mind to test it again? Commented Feb 9, 2016 at 2:26
  • When using this method, the comment will be highlighted using font-lock-doc-face instead of font-lock-comment-face. This indicates that this method was intended to be used for doxygen-like comments, not for general highlighting inside comments. One drawback is that you can only have one doc-style, so you can't highlight symbols and doxygen comments. An alternative approach would be to use a plain font-lock rule that highlight symbols inside backtick-tick, that also check that they are inside comments using syntax-ppss. Commented Feb 11, 2016 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.