19.3 Font Lock mode
Font Lock mode is a minor mode, always local to a particular buffer,
which highlights (or “fontifies”) the buffer contents according to
the syntax of the text you are editing. It can recognize comments and
strings in most languages; in several languages, it can also recognize
and properly highlight various other important constructs—for
example, names of functions being defined or reserved keywords.
Some special modes, such as Occur mode and Info mode, have completely
specialized ways of assigning fonts for Font Lock mode.
Font Lock mode is turned on by default in all modes which support it.
You can toggle font-lock for each buffer with the command M-x
font-lock-mode. Using a positive argument unconditionally turns Font
Lock mode on, and a negative or zero argument turns it off.
If you do not wish Font Lock mode to be turned on by default,
customize the variable global-font-lock-mode
using the Customize
interface (see Easy Customization), or use the function
global-font-lock-mode
in your .emacs file, like this:
(global-font-lock-mode 0)
If you have disabled Global Font Lock mode, you can still enable font
lock for specific major modes by adding the function
turn-on-font-lock
to the mode hooks (see Hooks). For
example, to enable Font Lock mode for editing C files, you can do this:
(add-hook 'c-mode-hook 'turn-on-font-lock)
Font Lock mode uses several specifically named faces to do its job,
including font-lock-string-face
, font-lock-comment-face
,
and others. The easiest way to find them all is to use
M-x customize-group <RET> font-lock-faces <RET>.
To change the colors or the fonts used by Font Lock mode to fontify
different parts of text, just change these faces. There are
two ways to do it:
- Invoke M-x set-face-foreground or M-x set-face-background
to change the colors of a particular face used by Font Lock.
See Faces. The command M-x list-faces-display displays all
the faces currently known to Emacs, including those used by Font Lock.
- Customize the faces interactively with M-x customize-face, as
described in Face Customization.
The variable font-lock-maximum-decoration
specifies the
preferred level of fontification, for modes that provide multiple
levels. Level 1 is the least amount of fontification; some modes
support levels as high as 3. The normal default is “as high as
possible.” You can specify an integer, which applies to all modes, or
you can specify different numbers for particular major modes; for
example, to use level 1 for C/C++ modes, and the default level
otherwise, use this:
(setq font-lock-maximum-decoration
'((c-mode . 1) (c++-mode . 1)))
Fontification can be too slow for large buffers, so you can suppress
it. The variable font-lock-maximum-size
specifies a buffer size,
beyond which buffer fontification is suppressed.
Comment and string fontification (or “syntactic” fontification)
relies on analysis of the syntactic structure of the buffer text. For
the sake of speed, some modes, including C mode and Lisp mode,
rely on a special convention: an open-parenthesis or open-brace in the
leftmost column always defines the beginning of a defun, and is
thus always outside any string or comment. (See Left Margin Paren.) If you don't follow this convention, Font Lock mode can
misfontify the text that follows an open-parenthesis or open-brace in
the leftmost column that is inside a string or comment.
The variable font-lock-beginning-of-syntax-function
(always
buffer-local) specifies how Font Lock mode can find a position
guaranteed to be outside any comment or string. In modes which use the
leftmost column parenthesis convention, the default value of the variable
is beginning-of-defun
—that tells Font Lock mode to use the
convention. If you set this variable to nil
, Font Lock no longer
relies on the convention. This avoids incorrect results, but the price
is that, in some cases, fontification for a changed text must rescan
buffer text from the beginning of the buffer. This can considerably
slow down redisplay while scrolling, particularly if you are close to
the end of a large buffer.
Font Lock highlighting patterns already exist for many modes, but you
may want to fontify additional patterns. You can use the function
font-lock-add-keywords
, to add your own highlighting patterns for
a particular mode. For example, to highlight ‘FIXME:’ words in C
comments, use this:
(font-lock-add-keywords
'c-mode
'(("\\<\\(FIXME\\):" 1 font-lock-warning-face t)))
To remove keywords from the font-lock highlighting patterns, use the
function font-lock-remove-keywords
. See Search-based Fontification, for
documentation of the format of this list.
Fontifying large buffers can take a long time. To avoid large
delays when a file is visited, Emacs fontifies only the visible
portion of a buffer. As you scroll through the buffer, each portion
that becomes visible is fontified as soon as it is displayed. The
parts of the buffer that are not displayed are fontified
“stealthily,” in the background, i.e. when Emacs is idle. You can
control this background fontification, also called Just-In-Time
(or JIT) Lock, by customizing variables in the customization
group ‘jit-lock’. See Specific Customization.