2008-12-10

highlighting "TODO", "FIXME" and friends

When doing some programming with emacs, I like to highlight certain words that are not language keywords, but still have some special significance to me.

The emacs manual mentions this really useful trick:

(add-hook 'c-mode-common-hook
               (lambda ()
                (font-lock-add-keywords nil
                 '(("\\<\\(FIXME\\|TODO\\|BUG\\):" 1 font-lock-warning-face t)))))
This adds a hook function, a function that will be called when we open a C-file, so whenever there is a 'FIXME:', it gets a different color, so it stands out:
/* FIXME: check for foo */
Some notes:
  • I am using c-mode-common-hook instead of the c-mode-hook (as the emacs manual does); this means that this will work for all C-mode languages - C/C++/Java/Objective-C/...
  • I also added some extra keywords (TODO, BUG); you can of course add your own keywords there as well. It might help to use M-x re-builder (the Emacs regular expression builder), as Emacs regular expressions can be quite tricky to get right...

7 comments:

SETH said...

Hrm, I tried this with js2-mode and it didn't work. They were still the comment face.

Any help?

djcb said...

@Seth: something like:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(add-hook 'js2-mode-hook
(lambda()
(font-lock-add-keywords nil
'(("\\<\\(FIXME\\|TODO\\|XXX+\\|BUG\\):"
1 font-lock-warning-face prepend)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

should work, but it could be that the font-lock-warning-face happens to be the same color as the normal comment; instead, you could do something like:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(set-face-underline 'font-lock-warning-face "yellow")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
so the warnings (TODO:/FIXME:) get yellow underlines. Also, note that the pattern needs the ':' (so FIXME:, not FIXME)

SETH said...

The prepend did it.

Thanks! Great site.

Anonymous said...

See also http://members.iinet.net.au/~bethandmark/elisp/highlight-fixmes-mode.el and http://members.iinet.net.au/~bethandmark/elisp/highlight-long-lines-mode.el

Anonymous said...

That's a really nice trick. It works very well.

I was trying to define different faces for different words (e.g., FIXME in red, TODO in blue and so forth). To do that I've tried to use some of the faces listed with the "list-faces-display" command instead of the "font-lock-warning-face". However, it didn't work -- I don't get any highlighting. Am I only supposed to use "font-lock-warning-face"?

Any suggestion is more than welcome.

Thanks very much!

djcb said...

@Anonymous: something like:

------------------------------------
(font-lock-add-keywords nil
'(("\\<\\(FIXME\\|TODO\\)"
1 font-lock-warning-face prepend)))

(font-lock-add-keywords nil
'(("\\<\\(FOO\\|BAR\\)"
1 font-lock-pseudo-keyword-face prepend)))
------------------------------

should work to get different faces for FIXME, TODO and FOO, BAR.

Jason said...

Really should just add this to prog-mode-hook - then it just works with everything.