File: font-lock.el.html
Font Lock mode is a minor mode that causes your comments to be displayed in one face, strings in another, reserved words in another, and so on.
Comments will be displayed in font-lock-comment-face.
Strings will be displayed in font-lock-string-face.
Regexps are used to display selected patterns in other faces.
To make the text you type be fontified, use M-x font-lock-mode RET. When this minor mode is on, the faces of the current line are updated with every insertion or deletion.
To turn Font Lock mode on automatically, add this to your init file:
(add-hook 'emacs-lisp-mode-hook #'turn-on-font-lock)
Or if you want to turn Font Lock mode on in many modes:
(global-font-lock-mode t)
Fontification for a particular mode may be available in a number of levels
of decoration. The higher the level, the more decoration, but the more time
it takes to fontify. See the variable font-lock-maximum-decoration.
Support modes for Font Lock mode can be used to speed up Font Lock
mode. See font-lock-support-mode.
;; How Font Lock mode fontifies:
When Font Lock mode is turned on in a buffer, it (a) fontifies the entire
buffer and (b) installs one of its fontification functions on one of the
hook variables that are run by Emacs after every buffer change (i.e., an
insertion or deletion). Fontification means the replacement of face text
properties in a given region; Emacs displays text with these face text
properties appropriately.
Fontification normally involves syntactic (i.e., strings and comments) and regexp (i.e., keywords and everything else) passes. There are actually three passes; (a) the syntactic keyword pass, (b) the syntactic pass and (c) the keyword pass. Confused?
The syntactic keyword pass places syntax-table text properties in the
buffer according to the variable font-lock-syntactic-keywords. It is
necessary because Emacs's syntax table is not powerful enough to describe all
the different syntactic constructs required by the sort of people who decide
that a single quote can be syntactic or not depending on the time of day.
(What sort of person could decide to overload the meaning of a quote?)
Obviously the syntactic keyword pass must occur before the syntactic pass.
The syntactic pass places face text properties in the buffer according to
syntactic context, i.e., according to the buffer's syntax table and buffer
text's syntax-table text properties. It involves using a syntax parsing
function to determine the context of different parts of a region of text. A
syntax parsing function is necessary because generally strings and/or
comments can span lines, and so the context of a given region is not
necessarily apparent from the content of that region. Because the keyword
pass only works within a given region, it is not generally appropriate for
syntactic fontification. This is the first fontification pass that makes
changes visible to the user; it fontifies strings and comments.
The keyword pass places face text properties in the buffer according to
the variable font-lock-keywords. It involves searching for given regexps
(or calling given search functions) within the given region. This is the
second fontification pass that makes changes visible to the user; it
fontifies language reserved words, etc.
Oh, and the answer is, "Yes, obviously just about everything should be done
in a single syntactic pass, but the only syntactic parser available
understands only strings and comments." Perhaps one day someone will write
some syntactic parsers for common languages and a son-of-font-lock.el could
use them rather then relying so heavily on the keyword (regexp) pass.
;; How Font Lock mode supports modes or is supported by modes:
Modes that support Font Lock mode do so by defining one or more variables
whose values specify the fontification. Font Lock mode knows of these
variable names from the buffer local variable font-lock-defaults.
(Font Lock mode is set up via (a) where a mode's patterns are
distributed with the mode's package library, and (b) where a mode's
patterns are distributed with font-lock.el itself. An example of (a)
is Pascal mode, an example of (b) is Lisp mode. Normally, the mechanism is
(a); (b) is used where it is not clear which package library should contain
the pattern definitions.) Font Lock mode chooses which variable to use for
fontification based on font-lock-maximum-decoration.
Font Lock mode fontification behavior can be modified in a number of ways. See the below comments and the comments distributed throughout this file.
;; Constructing patterns:
See the documentation for the variable font-lock-keywords.
Efficient regexps for use as MATCHERs for font-lock-keywords and
font-lock-syntactic-keywords can be generated via the function
regexp-opt.
;; Adding patterns for modes that already support Font Lock:
Though Font Lock highlighting patterns already exist for many modes, it's
likely there's something that you want fontified that currently isn't, even
at the maximum fontification level. You can add highlighting patterns via
font-lock-add-keywords. For example, say in some C
header file you #define the token and to expand to &&, etc., to make
your C code almost readable. In your ~/.emacs there could be:
(font-lock-add-keywords 'c-mode '("\\\\<\\\\(and\\\\|or\\\\|not\\\\)\\\\>"))
Some modes provide specific ways to modify patterns based on the values of
other variables. For example, additional C types can be specified via the
variable c-font-lock-extra-types.
;; Adding patterns for modes that do not support Font Lock:
Not all modes support Font Lock mode. If you (as a user of the mode) add
patterns for a new mode, you must define in your ~/.emacs a variable or
variables that specify regexp fontification. Then, you should indicate to
Font Lock mode, via the mode hook setting font-lock-defaults, exactly what
support is required. For example, say Foo mode should have the following
regexps fontified case-sensitively, and comments and strings should not be
fontified automagically. In your ~/.emacs there could be:
(defvar foo-font-lock-keywords
'(("\\\\<\\\\(one\\\\|two\\\\|three\\\\)\\\\>" . 'font-lock-keyword-face)
("\\\\<\\\\(four\\\\|five\\\\|six\\\\)\\\\>" . 'font-lock-type-face))
"Default expressions to highlight in Foo mode.")
(add-hook 'foo-mode-hook
(lambda ()
(setq-local font-lock-defaults
'(foo-font-lock-keywords t))))
;; Adding Font Lock support for modes:
Of course, it would be better that the mode already supports Font Lock mode.
The package author would do something similar to above. The mode must
define at the top-level a variable or variables that specify regexp
fontification. Then, the mode command should indicate to Font Lock mode,
via font-lock-defaults, exactly what support is required. For example,
say Bar mode should have the following regexps fontified case-insensitively,
and comments and strings should be fontified automagically. In bar.el there
could be:
(defvar bar-font-lock-keywords
'(("\\\\<\\\\(uno\\\\|due\\\\|tre\\\\)\\\\>" . 'font-lock-keyword-face)
("\\\\<\\\\(quattro\\\\|cinque\\\\|sei\\\\)\\\\>" . 'font-lock-type-face))
"Default expressions to highlight in Bar mode.")
and within bar-mode there could be:
(setq-local font-lock-defaults
'(bar-font-lock-keywords nil t))
What is fontification for? You might say, "It's to make my code look nice."
I think it should be for adding information in the form of cues. These cues
should provide you with enough information to both (a) distinguish between
different items, and (b) identify the item meanings, without having to read
the items and think about it. Therefore, fontification allows you to think
less about, say, the structure of code, and more about, say, why the code
doesn't work. Or maybe it allows you to think less and drift off to sleep.
So, here are my opinions/advice/guidelines:
- Highlight conceptual objects, such as function and variable names, and
different objects types differently, i.e., (a) and (b) above, highlight
function names differently to variable names.
- Keep the faces distinct from each other as far as possible.
i.e., (a) above.
- Use the same face for the same conceptual object, across all modes.
i.e., (b) above, all modes that have items that can be thought of as, say,
keywords, should be highlighted with the same face, etc.
- Make the face attributes fit the concept as far as possible.
i.e., function names might be a bold color such as blue, comments might
be a bright color such as red, character strings might be brown, because,
err, strings are brown (that was not the reason, please believe me).
- Don't use a non-nil OVERRIDE unless you have a good reason.
Only use OVERRIDE for special things that are easy to define, such as the
way ... quotes are treated in strings and comments in Emacs Lisp mode.
Don't use it to, say, highlight keywords in commented out code or strings.
- Err, that's it.
Defined variables (46)
cpp-font-lock-keywords | Font lock keywords for C preprocessor directives. |
cpp-font-lock-keywords-source-depth | Regular expression depth of ‘cpp-font-lock-keywords-source-directives’. |
cpp-font-lock-keywords-source-directives | Regular expression used in ‘cpp-font-lock-keywords’. |
font-lock-builtin-face | Face name to use for builtins. |
font-lock-comment-delimiter-face | Face name to use for comment delimiters. |
font-lock-comment-end-skip | If non-nil, Font Lock mode uses this instead of ‘comment-end-skip’. |
font-lock-comment-face | Face name to use for comments. |
font-lock-comment-start-skip | If non-nil, Font Lock mode uses this instead of ‘comment-start-skip’. |
font-lock-constant-face | Face name to use for constant and label names. |
font-lock-doc-face | Face name to use for documentation. |
font-lock-doc-markup-face | Face name to use for documentation mark-up. |
font-lock-dont-widen | If non-nil, font-lock will work on the non-widened buffer. |
font-lock-ensure-function | Function to make sure a region has been fontified. |
font-lock-extend-after-change-region-function | A function that determines the region to refontify after a change. |
font-lock-extend-region-functions | Special hook run just before proceeding to fontify a region. |
font-lock-extra-managed-props | Additional text properties managed by font-lock. |
font-lock-flush-function | Function to use to mark a region for refontification. |
font-lock-fontify-buffer-function | Function to use for fontifying the buffer. |
font-lock-fontify-region-function | Function to use for fontifying a region. |
font-lock-fontify-syntactically-function | Function to use for syntactically fontifying a region. |
font-lock-function-name-face | Face name to use for function names. |
font-lock-ignore | Rules to selectively disable fontifications due to ‘font-lock-keywords’. |
font-lock-keyword-face | Face name to use for keywords. |
font-lock-keywords | A list of keywords and corresponding font-lock highlighting rules. |
font-lock-keywords-alist | Alist of additional ‘font-lock-keywords’ elements for major modes. |
font-lock-keywords-case-fold-search | Non-nil means the patterns in ‘font-lock-keywords’ are case-insensitive. |
font-lock-keywords-only | Non-nil means Font Lock should not use syntactic fontifications. |
font-lock-major-mode | Major mode for which the font-lock settings have been setup. |
font-lock-mark-block-function | Non-nil means use this function to mark a block of text. |
font-lock-maximum-decoration | Maximum decoration level for fontification. |
font-lock-multiline | Whether font-lock should cater to multiline keywords. |
font-lock-negation-char-face | Face name to use for easy to overlook negation. |
font-lock-preprocessor-face | Face name to use for preprocessor directives. |
font-lock-removed-keywords-alist | Alist of ‘font-lock-keywords’ elements to be removed for major modes. |
font-lock-string-face | Face name to use for strings. |
font-lock-support-mode | Support mode for Font Lock mode. |
font-lock-syntactic-face-function | Function to determine which face to use when fontifying syntactically. |
font-lock-syntactic-keywords | A list of the syntactic keywords to put syntax properties on. |
font-lock-syntactically-fontified | Point up to which ‘font-lock-syntactic-keywords’ has been applied. |
font-lock-syntax-table | Non-nil means use this syntax table for fontifying. |
font-lock-type-face | Face name to use for type and class names. |
font-lock-unfontify-buffer-function | Function to use for unfontifying the buffer. |
font-lock-unfontify-region-function | Function to use for unfontifying a region. |
font-lock-variable-name-face | Face name to use for variable names. |
font-lock-verbose | If non-nil, means show status messages for buffer fontification. |
font-lock-warning-face | Face name to use for things that should stand out. |
Defined functions (46)
Defined faces (28)
font-lock-bracket-face | Font Lock mode face used to highlight brackets, braces, and parens. |
font-lock-builtin-face | Font Lock mode face used to highlight builtins. |
font-lock-comment-delimiter-face | Font Lock mode face used to highlight comment delimiters. |
font-lock-comment-face | Font Lock mode face used to highlight comments. |
font-lock-constant-face | Font Lock mode face used to highlight constants and labels. |
font-lock-delimiter-face | Font Lock mode face used to highlight delimiters. What exactly is a delimiter depends on the major mode, but usually these are characters like comma, colon, and semi-colon. |
font-lock-doc-face | Font Lock mode face used to highlight documentation embedded in program code. It is typically used for special documentation comments or strings. |
font-lock-doc-markup-face | Font Lock mode face used to highlight embedded documentation mark-up. It is meant for mark-up elements in text that uses ‘font-lock-doc-face’, such as the constructs of Haddock, Javadoc and similar systems. |
font-lock-escape-face | Font Lock mode face used to highlight escape sequences in strings. |
font-lock-function-call-face | Font Lock mode face used to highlight function calls. |
font-lock-function-name-face | Font Lock mode face used to highlight function names. |
font-lock-keyword-face | Font Lock mode face used to highlight keywords. |
font-lock-misc-punctuation-face | Font Lock mode face used to highlight miscellaneous punctuation. |
font-lock-negation-char-face | Font Lock mode face used to highlight easy to overlook negation. |
font-lock-number-face | Font Lock mode face used to highlight numbers. |
font-lock-operator-face | Font Lock mode face used to highlight operators. |
font-lock-preprocessor-face | Font Lock mode face used to highlight preprocessor directives. |
font-lock-property-name-face | Font Lock mode face used to highlight properties of an object. For example, the declaration of fields in a struct. |
font-lock-property-use-face | Font Lock mode face used to highlight property references. For example, property lookup of fields in a struct. |
font-lock-punctuation-face | Font Lock mode face used to highlight punctuation characters. |
font-lock-regexp-face | Font Lock mode face used to highlight regexp literals. |
font-lock-regexp-grouping-backslash | Font Lock mode face for backslashes in Lisp regexp grouping constructs. |
font-lock-regexp-grouping-construct | Font Lock mode face used to highlight grouping constructs in Lisp regexps. |
font-lock-string-face | Font Lock mode face used to highlight strings. |
font-lock-type-face | Font Lock mode face used to highlight type and class names. |
font-lock-variable-name-face | Font Lock mode face used to highlight variable names. |
font-lock-variable-use-face | Font Lock mode face used to highlight variable references. |
font-lock-warning-face | Font Lock mode face used to highlight warnings. |