2

I have implemented the solution of Yasushi Shoji in a previous thread -- Colorizing `functions/variables' within comments in `c-mode' -- to highlight Lisp function/variable names in c-mode comments that begin with a back-tick and end with a single quote:

/* See the doc-string for `pos-visible-in-window-p'.  */

The function c-font-lock-doc-comments unfortunately does away with the ability to highlight the comment beginning/ending with font-lock-comment-delimiter-face, which is a member of c-literal-faces. What happens is that the entire comment (except for the function/variable) gets highlighted with c-doc-face-name.

The unsophisticated approach is to redefine c-font-lock-doc-comments by removing or commenting out the offending code:

(c-put-font-lock-face region-beg region-end c-doc-face-name)

However, doing so will break the intended functionality for the three (3) other forms of predefined c-doc-comment-style -- i.e., javadoc, autodoc, and gtkdoc.

I am looking for a more sophisticated means to preserve the font-lock-comment-delimiter-face for comment beginning/ending and preserve the font-lock-comment-face for everything except the function/variable that is highlighted with Yasushi's solution.

The prior solution of Yasushi is as follows:

(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
          yashi-font-lock-doc-comments)
        ))))


(add-hook 'c-mode-hook
          (lambda ()
            (setq c-doc-comment-style '((c-mode . yashi)))))
0

1 Answer 1

4

You are using the wrong tool. The c-doc system is used to highlight doxygen-style comments, which is why it changes the face from font-lock-comment-face to font-lock-doc-face.

Another drawback is that you can only have one doc style, so if you use it to highlight symbols, you can't use it for anything else.

Below is code to add a plain font-lock keyword, with the twist that the symbol must be in a comment.

(defun my-highlight-symbol-font-lock-keywords-matcher (limit)
  "Search for quoted symbols in comments.
Match group 0 is the entire construct, 1 the symbol."
  (let (res)
    (while
        (and (setq res (re-search-forward "`\\([a-zA-Z0-9_-]+\\)'" limit t))
             (not (nth 4 (syntax-ppss)))))  ; Continue, unless in a comment
    res))

(defun my-highlight-symbol-activate ()
  "Highlight symbols quoted in BACKTICK-TICK in comments."
  (font-lock-add-keywords nil
                          '((my-highlight-symbol-font-lock-keywords-matcher
                             (1 font-lock-constant-face prepend)))))

(add-hook 'c-mode-common-hook #'my-highlight-symbol-activate)
4
  • Thank you very much -- this achieves exactly what I was looking for. :)
    – lawlist
    Commented Feb 11, 2016 at 17:03
  • You're welcome! See you around! Commented Feb 11, 2016 at 19:25
  • You could use (not (eql (syntax-ppss-context (syntax-ppss)) 'comment)) to make it easier to understand without a comment. Commented Feb 11, 2016 at 20:46
  • @JordonBiondo, thanks, I didn't know about this function. I agree, using it would make the code easier to read without a comment. Commented Feb 12, 2016 at 6:22

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.