Highlighting keywords such as TODO, FIXME, NOTE, BUG, and others (often referred to as tags, codetags, or tokens) enhances workflow by making key annotations more visible. This allows developers to quickly identify tasks, warnings, and notes within the code, reducing the time spent searching for unfinished work or potential issues.
This article outlines an Elisp code that highlights these codetags.
(There are packages like hl-todo and comment-tags that can highlight these codetags for those who need a more feature-rich solution. However, they contain hundreds of lines of code, which is excessive if your only goal is to just highlight codetags. While these packages likely offer additional features, such as navigating to the next codetag, the Elisp code in this article provides a much simpler solution for those who just want to highlight them.)
Elisp code to highlight codetags
To highlight these codetags, you can use the following Emacs Lisp code:
(defvar highlight-codetags-keywords
'(("\\<\\(TODO\\|FIXME\\|BUG\\|XXX\\)\\>" 1 font-lock-warning-face prepend)
("\\<\\(NOTE\\|HACK\\)\\>" 1 font-lock-doc-face prepend)))
(define-minor-mode highlight-codetags-local-mode
"Highlight codetags like TODO, FIXME..."
:global nil
(if highlight-codetags-local-mode
(font-lock-add-keywords nil highlight-codetags-keywords)
(font-lock-remove-keywords nil highlight-codetags-keywords))
;; Fontify the current buffer
(when (bound-and-true-p font-lock-mode)
(if (fboundp 'font-lock-flush)
(font-lock-flush)
(with-no-warnings (font-lock-fontify-buffer)))))
Code language: Lisp (lisp)
To apply codetag highlighting across all programming modes, add highlight-codetags-local-mode to the prog-mode-hook:
(add-hook 'prog-mode-hook #'highlight-codetags-local-mode)
Code language: Lisp (lisp)
If you call highlight-codetags-local-mode interactively, you can toggle the highlighting of codetags on and off.
Customizations
If desired (though not required), you can further customize the Elisp code:
- You can customize the highlighting by substituting font-lock-warning-face or font-lock-doc-face with any other face of your choice. (You can view all available faces by executing the command:
M-x list-faces-display
) - Additionally, you can add more keywords to the regular expression.
For instance, to add the MAYBE codetag to the\\<\\(NOTE\\|HACK\\)\\>
pattern, simply append\\|MAYBE
before the closing parenthesis\\)
:\\<\\(NOTE\\|HACK\\|MAYBE\\)>
.
Conslusion
This simple configuration enhances keyword visibility in Emacs, making it easier to track important annotations while editing source code.