As you already know—if you read my blog before—I use Emacs as my primary editor, for C, C++, Python, LaTeX, etc., and I’ve grown fond of the clunky ol’ piece of software. Still, once in a while, I need an extra, potentially weird customization.
In my case, I have a series of macros that control conditional compilation (such as described in a previous post). Suppose I have set of macros BEGIN_UNIT_TEST_ONLY, END_UNIT_TEST_ONLY and UNIT_TEST_ONLY that start a unit-test specific block of code, ends it, and enables only a single statement, respectively.
These are UGLY_MACROs and I want them to poke conspicuously out of the source code. So, How do I tell Emacs to display them in a special way, using bright ugly colors?
The first thing to do if you don’t want to reuse an existing face customization (such as font-lock-comment-face, for example) is to create one. Let’s start with a new face descriptor initialized with a default color. In my case, I wanted it to inherit all properties of normal text except for its color. You add:
(make-face 'font-lock-special-macro-face) (set-face-foreground 'font-lock-special-macro-face "pink")
This creates a new face named font-lock-special-macro-face set to pink. Or whatever color you’d like. I used magenta finally, because although it is ugly, it’s still better than pink in my color scheme.
We proceed to add regular expressions to the list of keywords and associate each regexp with a face.
(defun add-custom-keyw() "adds a few special keywords for c and c++ modes" ; (font-lock-add-keywords nil '( ("\\<\\(UNIT_TEST_ONLY\\)" . 'font-lock-special-macro-face ) ("\\<\\(BEGIN_UNIT_TEST_ONLY\\)" . 'font-lock-special-macro-face ) ("\\<\\(END_UNIT_TEST_ONLY\\)" . 'font-lock-special-macro-face) ; more of those would go here ) ) )
Which we hook to the initialization of C and C++ (minor) modes:
(add-hook 'c++-mode-hook 'add-custom-keyw) (add-hook 'c-mode-hook 'add-custom-keyw)
and voilà! we’re done. When the c- and c++-mode initialize, they will run add-custom-keyw, and they will activate with the desired face. You can enjoy the result:
Thanks to the people of #emacs on Freenode.org for hinting be to the right solution. Emacs programming isn’t the easiest of all. Fortunately, there’s google and the Emacs wiki.