Emacs Redux

Return to the Essence of Text Editing

Highlight Comment Annotations

| Comments

Programming code is often filled with comment annotations indicating stuff that should be done in the future.

1
2
3
# REFACTOR: Decouple and clean up this crap.

# crappy code omitted

Normally Emacs doesn’t highlight such comment annotations, unless you’re using some minor mode like fic-mode. I find such mode overkill given the fact we can cook a pretty decent solution in just about 5 lines of code:

1
2
3
4
5
6
7
8
9
(defun font-lock-comment-annotations ()
  "Highlight a bunch of well known comment annotations.

This functions should be added to the hooks of major modes for programming."
  (font-lock-add-keywords
   nil '(("\\<\\(FIX\\(ME\\)?\\|TODO\\|OPTIMIZE\\|HACK\\|REFACTOR\\):"
          1 font-lock-warning-face t))))

(add-hook 'prog-mode-hook 'font-lock-comment-annotations)

And that’s all there is to it. This code is not perfect, since it would highlight FIXME: everywhere in the source code (as opposed to only in comments), but it’s extremely highly unlikely that it’ll appear outside of the source comments anyways.

As usual Prelude users get this functionally for free out-of-the-box.

Comments