3

I just recently found the "rainbow-delimiters-mode" package and found it quite useful, however I seem to have run into a snag with it when using it in C-like languages:

Naturally, I'd prefer to enable it for all programming modes, so I added the following to my config:

(add-hook 'prog-mode-hook #'rainbow-delimiters-mode)

This apparently worked fine for everything but C-like languages.

I guess cc-mode is derived from prog-mode, hence prog-mode-hook is run before cc-mode, overriding some settings. The cc-mode settings I want are basically the ones in this snippet:

(defun my-cc-mode-common-hook ()
  "Setup common utilities for all C-like modes."
  (setq-local c-doc-comment-style
        '((java-mode . javadoc)
            (pike-mode . autodoc)
            (c-mode    . javadoc)
            (c++-mode  . javadoc)))
  (face-remap-add-relative 'font-lock-doc-face
           :foreground (face-foreground font-lock-comment-face)))
(add-hook 'c-mode-common-hook 'my-cc-mode-common-hook)

However, with the rainbow-delimiters-mode in prog-mode-hook, the above snippet breaks. I thought I could fix this by temporarily disabling the mode at the beginning of the hook and then re-enable it again at the end:

(defun my-cc-mode-common-hook ()
  (rainbow-delimiters-mode 0)
   ;; ...
  (rainbow-delimiter-mode))

This doesn't work at all however, so I guess I have to do something else to enable the configuration in cc-mode to fix this issue. Anyone have any ideas?

Also, simply disabling rainbow-delimiters-mode doesn't re-enable the cc-mode settings I listed above, that is:

(defun my-cc-mode-common-hook ()
  (rainbow-delimiters-mode 0)
   ;; ...
  )

Doesn't work, at least not on it's own.

I should note that it is possible to fix this by not having rainbow-delimiters-mode in the prog-mode-hook and instead place it in c-mode-common-hook or any other major-mode hook. It does however seem like a hassle to add it to every new programming mode instead of fixing it where it breaks.

Xaldew
  • 1,231
  • 9
  • 21
  • Thanks for the clarification on the question. Now all I need to do is replicate your problem ;-) – stsquad Apr 30 '15 at 14:13
  • 1
    I can replicate this, I haven't figured out why this occurs though. A less than ideal solution, but a working solution. Is to change your prog mode hook function to enable rainbow-delimiters mode on a 1 second timer. That way the c-mode-common-hook runs first. That fixed it for me, but I'd rather someone chime in on an actual solution to the problem, not a work around. – Jordon Biondo Apr 30 '15 at 19:14

1 Answers1

0

Seems like I ran into a rather simple solution. Apparently, the c-doc-comment-style variable should be initialized before any mode-hooks are allowed to run.

Also, according to the documentation, setting this variable automatically makes it buffer-local, so it isn't necessary to set the variable inside the hook.

Thus, the solution boils down to moving the setter for c-doc-comment-style:

(defun my-cc-mode-common-hook ()
  "Setup common utilities for all C-like modes."
  (face-remap-add-relative 'font-lock-doc-face
           :foreground (face-foreground font-lock-comment-face)))

(setq c-doc-comment-style '((java-mode . javadoc)
                        (pike-mode . autodoc)
                        (c-mode    . javadoc)
                        (c++-mode  . javadoc)))
Xaldew
  • 1,231
  • 9
  • 21