Emacs Font Locking

Emacs has great facilities to add font locking (aka syntax highlighting) to a buffer in a mode. This series of posts about font locking aims to give an introduction into the different font locking techniques in Emacs, and shares what I discovered and learned while refactoring and improving font locking in Puppet Mode.

As such, I use examples of Puppet Mode and Puppet language throughout this series. If you lack familiarity with Puppet please take a short look at the Puppet language reference first.

Overview

Emacs has two main facilities for syntax highlighting which I cover in the first two articles of this series:

Syntax Tables

Simple generic and stateless highlighting for strings and comments by classifying single characters. Most modes make use of this basic facility for strings and comments.

Font Lock Keywords

Stateless highlighting based on regular expressions for specific syntactic elements of a language. Every major mode uses it to highlight keywords, etc.

Both cannot track state or look at syntactic context.

For stateful or context-dependent highlighting Emacs enables modes to hook into its syntactic analysis phase with syntax-propertize-function to add arbitrary text properties and syntax classifiers to buffer text. Few modes use this facility—it’s tricky and complicated—but sometimes no other way exists1 and often makes the difference between good and great syntax highlighting2. I talk about this feature in the last post of this series.

Articles

  1. Python Mode uses this facility to fontify triple-quoted strings. ↩︎

  2. Ruby mode uses this feature to fontify variable expansions inside double-quoted strings. ↩︎