In this first post of my series about Font Locking I’ll give an overview of Emacs’ font locking system.
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.