Elisp: 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. (low level is to add text properties to regions of text in buffer. Font lock mode does this for you. [see Elisp: Text Properties])
font-lock-modecolor 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.
- 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.
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 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 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. This list, is a list of pairs. For each pair, the first part is a regex, the second part is the face to apply.
- Second element should be nil or t. nil means DO color by syntax table. Else, do not.
(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
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
Now, type following code into a buffer:
myhtml-mode, you see words colored.
[see Elisp: How to Define Face]
[see Elisp: Syntax Table]