Emacs Lisp: Font Lock Mode Basics

By Xah Lee. Date: .

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. (lower level is to add text properties for different strings.)
  4. font-lock-mode color text in two ways: ① by syntactic parsing based on the syntax table. This basically means, lookup the delimiter characters for string in syntax table, and color text betwen it in buffer. Same for comment. ② search by regular expressions. 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 searh 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 characters for string and character for comment.) ② Set up proper value for font-lock-defaults.

(info "(elisp) Font Lock Mode")

〔►see Emacs Lisp: Syntax Table Tutorial


(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 string inside them. (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 call eval-buffer.

Now, type following code into a buffer:


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

elisp font lock mode example 65235
major mode example

〔►see Emacs Lisp: How to Define Face

〔►see Emacs Lisp: Syntax Table Tutorial

Writing Major Mode Topics

  1. How to Write a Emacs Major Mode for Syntax Coloring
  2. Emacs Lisp: html6-mode
  3. Emacs Lisp: Font Lock Mode Basics
  4. Emacs Lisp: How to Define Face
  5. Emacs Lisp: How to Color Comment in Major Mode
  6. Emacs Lisp: How to Write Comment Command in Major Mode
  7. Emacs Lisp: How to Write Your Own Comment Command from Scratch
  8. Emacs Lisp: How to Write Keyword Completion Command
  9. Emacs Lisp: How to Create Keymap for Major Mode
  10. Emacs Lisp: Create Abbrev and Templates for Major Mode
  11. Emacs Lisp: Overlay Highlighting
  12. Emacs: Lookup Google, Dictionary, Documentation

  1. Emacs Lisp: How to Name Your Major Mode
  2. Emacs Lisp: What's “feature”?
  3. Emacs Lisp: require, load, load-file, autoload, feature, Explained

Syntax Table

  1. Emacs Lisp: Syntax Table Tutorial
  2. Emacs Lisp: How to Find Syntax of a Character?
  3. Emacs Lisp: How to Modify Syntax Table Temporarily
  4. Emacs Lisp: How to Determine If Cursor is Inside String or Comment
  5. Emacs Lisp: Find Matching Bracket Character
Like it? Buy Xah Emacs Tutorial. Thanks.