This article explores how to hook into Emacs’ syntactic analyses to implement context-sensitive fontification.
Emacs Font Locking
Font locking (aka syntax highlighting) is an essential part of any Emacs major mode. While Emacs has great facilities to implement syntax highlighting, these are not always easy to understand and implement.
This post is the start of an article series about font locking that aims to give a little overview about various font locking techniques in Emacs. In essence, it’s a summary of what I discovered and learned recently while refactoring and improving font locking in Puppet Mode.
As such, the examples throughout this series are about Puppet Mode. If you are not familiar with Puppet, you may want take a short look at the Puppet language reference first.
Emacs has two major facilities for syntax highlighting:
- Syntax Tables
Simple generic and stateless highlighting for strings and comments by classifying individual characters.
Most major 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 the target language.
This is the major workhorse of font lock, used by every major mode.
Both facilities are stateless, and cannot look at syntactic context. However,
by means of
syntax-propertize-function Emacs allows major modes to
hook into its syntactic analysis, to add arbitrary text properties and syntax
classifiers to buffer text.
This is the most powerful, but also the most tricky syntax highlighting feature
of Emacs. As such, it’s not frequently used, but sometimes it’s
necessary—Python Mode fontifies triple-quoted strings with
syntax-propertize-function—and often it makes the difference between good and
great syntax highlighting—
syntax-propertize-function helps Ruby Mode fontify
variable expansions inside double-quoted strings.