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: ① by syntactic parsing based on the syntax table. This basically means, lookup the delimiter characters for string in syntax table, and color text between 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 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 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.
- Second element should be nil or t. nil means DO color by syntax table. Else, do not.
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.
Writing Major Mode
- How to Write a Emacs Major Mode for Syntax Coloring
- Elisp: html6-mode
- Elisp: Font Lock Mode Basics
- Elisp: How to Define Face
- Elisp: How to Color Comment in Major Mode
- Elisp: How to Write Comment Command in Major Mode
- Elisp: How to Write Your Own Comment Command from Scratch
- Elisp: How to Write Keyword Completion Command
- Elisp: How to Create Keymap for Major Mode
- Elisp: Create Abbrev and Templates for Major Mode
- Elisp: Text Properties
- Elisp: Overlay Highlighting
- Emacs: Lookup Google, Dictionary, Documentation
- Elisp: Syntax Table Tutorial
Patreon me $5. Ask me question on patreon