Understanding Hi-line-mode

Tony Aldon wondered how hi-line-mode worked so he looked into it. He was so enchanted with what he found that he decided to share it on reddit. That might not sound too interesting but hi-line-mode depends on two Emacs features, post-command-hook and overlays, that are interesting in their own right. Once you understand them, understanding hi-line-mode is trivial.

The post-command-hook variable, as its name suggests, contains a list of functions to run after every command. Since virtually everything Emacs does—including inserting characters into a buffer—is through a command, the hook gets run every time Emacs does something. In particular, hi-line-mode can be called after every command to update the current line’s highlighting.

The other important aspect of hi-line-mode is the overlay. This is pretty simple and allows you to, among other things, change the text face of a region in the buffer. In the case of hi-line-mode, it changes the background color of the current line to whatever color you’ve chosen.

Once you grok hooks and overlays, it’s easy to understand how hi-line-mode works. Aldon’s post is useful not because it explains how hi-line-mode works but because it explains post-command-hook and overlays. Those are things you can use in your own Elisp code.

This entry was posted in General and tagged . Bookmark the permalink.