If you use GNU Emacs to edit Cvstrac source code, you've probably been frustrated by its autoindenting behaviour when editing embedded HTML or SQL code. Emacs' syntax highlighting and indentation engine contains a primitive C parser that will attempt to colourise and indent the embedded HTML/SQL code according to C rules, with disastrous results.

Fortunately, there is an easy way to make Emacs ignore the embedded code entirely, by changing its syntax table to recognise @ as a single-line comment starter (like C++'s "//" comments). Add the following line to your c-mode-hook:

    (modify-syntax-entry ?@ "< b")

The < in the syntax descriptor says that @ is now a comment starter, whilst the b means that it is a C++ style comment (it ends with a newline). Emacs will treat any text from @ until the end of the line as a comment, and won't attempt to format it as C code. (It is also smart enough to ignore instances of @ which appear inside double-quoted string constants.)

It's even possible to go a step further and have Emacs highlight @ lines differently from ordinary comments:

   'c-mode '(
             ("^[ \\t]*\\(@\\)\\(.*\\)$"
              (1 font-lock-warning-face t nil)
              (2 font-lock-builtin-face t nil)))))

This will make the embedded code stand out, by highlighting the @ itself in a striking bold red, but displaying the embedded text in a more readable light steel blue (or whatever colours you've chosen for those standard faces).

6th February 2004