Elisp: Font Lock Mode Basics

By Xah Lee. Date: . Last updated: .

Here's the essential things you need to know about font-lock-mode, for writing a major mode for syntax coloring.

  1. font-lock-mode is a buffer-local minor mode. (that is, it can be on or off for each buffer, independent of each other.)
  2. font-lock-mode is on by default for all buffers since ~2007.
  3. font-lock-mode is a high-level “API” to syntax color buffer. (low level is to add text properties to regions of text in buffer. Font lock mode does this for you. [see Elisp: Text Properties])
  4. font-lock-mode color text in two ways: ① “Syntactic fontification”: color comments and strings by looking up the delimiter characters for string and comments in syntax table, then color text between them. ② “Search based fontification”, by using regular expressions to seach text and color them. This is how keywords, function names, variable names, html title text, Markdown text, Org Mode text, etc, are colored.
  5. Syntactic fontification happens first. It finds comments and string and color them. Search-based fontification happens second. Once a text is colored, it is not changed. For example, if a text is colored as string or comment, subsequent search by regex for coloring will skip those parts.
  6. font-lock-mode need 2 things to do the coloring job. ① Syntax table. ② the value of font-lock-defaults. It uses those info to go thru buffer and do syntax coloring.
  7. Vast majority of programing language major modes do syntax color by: ① set up the proper syntax table. (in particular, the delimiter characters for string and for comment.) ② Set up proper value for font-lock-defaults.

(info "(elisp) Font Lock Mode")

[see Elisp: Syntax Table]

font-lock-defaults

(info "(elisp) Font Lock Basics")

Basic Example of Syntax Coloring with Font Lock Mode

Here's a basic example.

Suppose you have a basic language like HTML.

You want to color any string of the form <h1> and </h1>

And you want to color text between the tags. (assume there's no linebreak, and does not contain greater/less characters.)

Save the following in a file.

;; a simple major mode, myhtml-mode

(defvar myhtml-highlights nil "first element for `font-lock-defaults'")

(setq myhtml-highlights
      '(("<h1>\\|</h1>" . font-lock-function-name-face)
        ("<h1>\\([^<]+?\\)</h1>" . (1 font-lock-constant-face))))

(define-derived-mode myhtml-mode fundamental-mode "myhtml"
  "major mode for editing myhtml language code."
  (setq font-lock-defaults '(myhtml-highlights)))

Now, copy and paste the above code into a buffer, then Alt+x eval-buffer.

Now, type following code into a buffer:

<h1>something</h1>

Now, M-x myhtml-mode, you see words colored.

elisp font lock mode example 65235
major mode example

[see Elisp: How to Define Face]

[see Elisp: Syntax Table]

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys

ELisp

ELisp Examples

ELisp Write Major Mode


ELisp Write Major Mode

Basics

Package Name/Load

Syntax Table