C++ Syntax Highlighting in GNU Emacs/XEmacs

Included below is the cpp-font-lock package to better syntax highlighting for C++ in emacs. The code is adapted from the GNU emacs C++ font locking support (font-lock.el), but includes much improved support for C++ constructs.

Supported constructs include:

  1. Class member function declarations and definitions
  2. Free function declarations and definitions
  3. Class, struct, typedef, and enum definitions
  4. Preprocessor directives and macro definitions
  5. All types specified in Standard C++
  6. All literals (integer, floating point, and string literals)
  7. All C++ keywords

Constructs not fully supported:

  1. Variable declarations (member and free)
  2. User definied type usage
  3. Usage expressions (assignments and function calls)
  4. Usage of font-lock default fonts

Installation Instructions

Download cpp-font-lock.el and place it in a subdirectory of your home directory named elisp.

Using CPP Font Lock

For most people, simply adding the following lines to their .emacs or ~/.xemacs/custom.el will be enough
((add-to-list 'load-path (expand-file-name "~/elisp/"))
(require 'cpp-font-lock)
However, if you do not have cc-mode installed or enabled, this will not work. You must enable cc-mode by adding this to your config (before loading cpp-font-lock btw):
(autoload 'c++-mode "cc-mode" "C++ Editing Mode" t)

;; To associate *.h files with c++ mode use the following line
(add-to-list 'auto-mode-alist '("\\.h$" . c++-mode))

Cpp Font Lock will add itself to the c++-mode-hook automatically if its loaded correctly. Obviously if you override that hook instead of using add-hook, you may succeed in disabling CPP Font Lock, but as long as you use add-hook or otherwise avoid clobbering your c++-mode-hook, CPP Font lock should work now.

If you would like to override the color scheme I use (see below for reasons why you should not do that), the easiest thing to do is setup the font-lock faces and add the following line to your emacs configuration
(cpp-font-lock-use-font-lock-faces t)
If you would like to see an example of overloaded font-lock faces, please check out my .emacs file.

Cpp Font Lock 0.4.0

Color schemes and the eyes

It is possible to overload the font colorings in cpp-font-lock with whatever you prefer, however the default is no longer the Emacs font-lock default. Quite simply, the default color schemes in GNU Emacs and XEmacs are pretty terrible. I've spent a bit of time finding color schemes that don't overburden the eyes with excessive contrast. Nevertheless, I do provide support for both light and dark backgrounds, even though you should only be using a dark background (in fact I strongly suggest something like gray25 or #001C00, definitely not black). If you are new to Emacs, what you really should do is download my .emacs and use it as is -- or at least in terms of color schemes.

Compatibility with older versions

I support Emacs 21.1 and Xemacs 21.4 only. No older versions are supported. With Emacs, this is mostly because I'm new to it, and I may eventually support other versions. With XEmacs I'm tired of being jerked around by the developers. XEmacs release schedule is completely unpredictable, and their support for their currently released products is nothing special. I am migrating away from XEmacs due to my frustrations (you vote with your feet). Though I recignize their product is still technically superior, and the project runs in a more democratic fashion, their quality lacks, and really that is the deciding factor as far as I am concerned.