Emacs Lisp: Font Lock Mode Basics
Here's the essential things you need to know about
font-lock-mode, for writing a major mode for syntax coloring.
font-lock-modeis a buffer-local minor mode. (that is, it can be on or off for each buffer, independent of each other.)
font-lock-modeis on by default for all buffers since ~2007.
font-lock-modeis a high-level “API” to syntax color buffer. (lower level is to add text properties for different strings.)
font-lock-modecolor 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.
- 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.
font-lock-modeneed 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.
- 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.
- font-lock-defaults is a buffer local variable. (buffer local variable means, each buffer has its own “copy”, may have different values, independent of each other.)
- font-lock-defaults variable is designed as a config for the purpose of syntax coloring. When
font-lock-modeis on, it will use the value of font-lock-defaults to color buffer.
- font-lock-defaults's value should be a list. If it's nil (nil is empty list), no coloring will happen, not even syntax table based one.
- First element of the list should be a list of keywords (regexes) to color. We set to nil because we don't need it here. (nil is empty list)
- Second element should be t or nil. t means don't color by syntax table. We do want to color by syntax table, namely the comments syntax (and string syntax), so we use nil (or, don't provide any argument)
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
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
Now, type following code into a buffer:
myhtml-mode, you see words colored.
Writing Major Mode Topics
- How to Write a Emacs Major Mode for Syntax Coloring
- Emacs Lisp: html6-mode
- Emacs Lisp: Font Lock Mode Basics
- Emacs Lisp: How to Define Face
- Emacs Lisp: How to Color Comment in Major Mode
- Emacs Lisp: How to Write Comment Command in Major Mode
- Emacs Lisp: How to Write Your Own Comment Command from Scratch
- Emacs Lisp: How to Write Keyword Completion Command
- Emacs Lisp: How to Create Keymap for Major Mode
- Emacs Lisp: Create Abbrev and Templates for Major Mode
- Emacs Lisp: Overlay Highlighting
- Emacs: Lookup Google, Dictionary, Documentation
- Emacs Lisp: How to Name Your Major Mode
- Emacs Lisp: What's “feature”?
- Emacs Lisp: require, load, load-file, autoload, feature, Explained