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:
Constructs not fully supported:
Download cpp-font-lock.el and place it in a subdirectory of your home directory named elisp.
((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.(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.
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.