4

I've been fiddling around a bit with CC-mode lately and figured that since I prefer to comment my functions/classes with javadoc-like syntax, I'd like to use the built-in comment highlighting provided by the corresponding c-doc-comment-style. This works rather well, it highlights something like this rather easily:

/** @brief This is function foo.
    @param a this is a parameter.
    @return I return this.
*/
int foo(unsigned a) {...}

However, I noticed that the comment suddenly changed color as well to a default one I don't really like. Any idea how to change that color back to the one defined by the current theme instead?

3

After fiddling a bit longer with this, I think I figured it out. Adding the following snippet to my CC-mode init-configuration fixed this:

(defun my-cc-init-hook ()
  "Initialization hook for CC-mode runs before any other hooks."
  (setq c-doc-comment-style
    '((java-mode . javadoc)
      (pike-mode . autodoc)
      (c-mode    . javadoc)
      (c++-mode  . javadoc)))
  (set-face-foreground 'font-lock-doc-face
               (face-foreground font-lock-comment-face)))
(add-hook 'c-initialization-hook 'my-cc-init-hook)

This basically copies the foreground color of the comment face to the "doc"-face that the javadoc style defines. Any improvements to this is appreciated!

1

You can just add the mapping to the comment style list:

(add-to-list 'c-doc-comment-style '(c++-mode . javadoc))

This is more portable and just extends the list instead of replacing it.

  • 1
    There's no need for the hook: the call to add-to-list could stand alone in the init file. The hook will run every time you enter c++-mode, but add-to-list only adds to the list if the entry is not already there. In other words, it'll do it's thing the first time you enter c++-mode, but do nothing over and over again for each subsequent time you enter c++-mode. – Dan Feb 21 '17 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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