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 programming languages; in several languages, it can also recognize and properly highlight various other important constructs, such as 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:
This variable, like all the variables that control Font Lock mode, take effect whenever fontification is done; that is, potentially at any time.
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,
and others. The easiest way to find them all is to use M-x
customize-group <RET> font-lock-faces <RET>. You can then
use that customization buffer to customize the appearance of these
faces. See Face Customization.
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 for buffers above a certain size. 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 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.
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
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:
(add-hook 'c-mode-hook (lambda () (font-lock-add-keywords nil '(("\\<\\(FIXME\\):" 1 font-lock-warning-face t)))))
To remove keywords from the font-lock highlighting patterns, use the
font-lock-remove-keywords. See Search-based Fontification.
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; this type of Font Lock is called Just-In-Time (or JIT) Lock. You can control how JIT Lock behaves, including telling it to perform fontification while idle, by customizing variables in the customization group ‘jit-lock’. See Specific Customization.blog comments powered by Disqus