Highlight "#if 0" Region of C, C++

My vim using co-workers enjoy a useful feature that I haven't yet found in emacs. A common thing to do to comment out large sections of C or C++ code is to put an #if 0, #endif pair around them. Vim seems to know to change the code highlighting for those regions. Emacs does not. I searched google and came across this suggestion to use hideif.el, which kinda works, but I'm not satisfied. It doesn't automatically change the color of the text like commenting it out does, you have to run the hide-ifdefs to get it to change the color, and you have to re-run it if you do any more editing. I want it to work just like other font-locking in emacs, automatically. Does anyone know how to do this?

Comments

Zung! said…
The following is from my .emacs for my old snapshot of emacs 22. Note that it isn't perfect - you must recenter the buffer to get the highlighting to update after it's changed.
I'd rather have this functionality part of font-lock but this is really over my head.

;; ------------ C Mode
(defun my-cpp-highlight ()
(setq cpp-known-face '(background-color . "dim gray"))
(setq cpp-unknown-face 'default)
(setq cpp-face-type 'dark)
(setq cpp-known-writable 't)
(setq cpp-unknown-writable 't)
(setq cpp-edit-list
'((#("0" 0 1
(c-in-sws t fontified t))
(background-color . "dim gray")
nil both nil)
(#("1" 0 1
(c-in-sws t fontified t))
nil
(background-color . "dim gray")
both nil)))
(cpp-highlight-buffer t))

(defun my-c-mode-common-hook ()
(my-cpp-highlight)
)

(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

(defun my-c-mode-recenter ()
"Recenter buffer and refresh highlighting."
(interactive)
(recenter)
(cpp-highlight-buffer t))

(defun my-c-initialization-hook ()
(define-key c-mode-base-map "\C-l" 'my-c-mode-recenter))

(add-hook 'c-initialization-hook 'my-c-initialization-hook)

Popular posts from this blog

SystemVerilog Fork Disable "Gotchas"

'git revert' Is Not Equivalent To 'svn revert'

Put /etc Under Revision Control (with git)