C++ Syntax Highlighting for GNU Emacs/XEmacs

Several years ago the C++ syntax highlighting support in Emacs was pretty terrible.  The XEmacs support was particularly atrocious (and still may be).  To remedy that, I wrote my own syntax highlighting package for XEmacs originally, and then I ported it to GNU Emacs.  Back then I cared a lot more about the schism between XEmacs and Gnu Emacs.  Writing this package is one of the major impetuses for getting me to care a whole lot less about the politics of free software.

Circa 2002 there could be little doubt that XEmacs was technically superior to GNU Emacs.  Additionally, the project ran in a very democratic fashion with Stephen Turnbull acting as an elected leader who was rather open to input from XEmacs developers.  My C++ font locking package was mailed to the XEmacs developers list by a user, and I was invited to submit the code to XEmacs — kinda.  As it stood, my code was better than the default font-locking code (aka syntax highlighting), and I wanted to improve it further, particularly adding better support for templates, and some other hairy constructs.  However, the code was desired for integration after the next release.  For about 6 months the next release was planned  to be just days away — but it never came.

During this time, I was trying to determine how to extract as much speed as possible from elisp, and generally pretty disappointed in my inability to extract the speed I wanted.  One thing I learned during the course of my testing was that GNU Emacs was about 5x faster than XEmacs when it came to syntax highlighting.  Unlike XEmacs, GNU Emacs had little interest in my font-locking package.  Their plan was to enhance cc-mode to include font-locking and have all of the language specific stuff in cc-mode.  That made sense to me at least.  During the time I was waiting for XEmacs to have a release, I believe the GNU Emacs project had several point releases that continued improving speed and stability.  While the code inside GNU Emacs was far less elegant than XEmacs, the GNU developers were far more effective with what they had.

Based on these frustrations, I simply switched to Emacs, used my own C++ syntax highlighting successfully, and waited for the day when Xemacs or GNU Emacs had better C++ support than my own package.  It was about 2005 that a beta version of GNU emacs included a cc-mode that had C++ support equivalent to my own.  I switched, and cpp-font-lock has not been maintained since.  I don’t know the status of XEmacs, and I probably never will as I have no use for that project any longer.  For that matter, I don’t even use GNU Emacs that much any more, I now program with Eclipse, and when a better Latex environment comes along, I will probably only use Emacs for shell scripting.

Cpp-Font-Lock

Included below is the cpp-font-lock package to provide better syntax highlighting for C++ in circa 2002 Emacsen. 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 defined type usage
  3. Usage expressions (assignments and function calls)

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).

Comments are closed.