GNU Emacs Manual. Node: Font Lock

PREVFaces UPFrames NEXTSupport Modes

15.14: Font Lock mode

Font Lock mode is a minor mode, always local to a particular buffer, which highlights (or ``fontifies'') using various faces 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.

The command M-x font-lock-mode turns Font Lock mode on or off according to the argument, and toggles the mode when it has no argument. The function turn-on-font-lock unconditionally enables Font Lock mode. This is useful in mode-hook functions. For example, to enable Font Lock mode whenever you edit a C file, you can do this:

(add-hook 'c-mode-hook 'turn-on-font-lock)

To turn on Font Lock mode automatically in all modes which support it, use the function global-font-lock-mode, like this:

(global-font-lock-mode 1)

In Font Lock mode, when you edit the text, the highlighting updates automatically in the line that you changed. Most changes don't affect the highlighting of subsequent lines, but occasionally they do. To rehighlight a range of lines, use the command M-g M-g (font-lock-fontify-block).

In certain major modes, M-g M-g refontifies the entire current function. (The variable font-lock-mark-block-function controls how to find the current function.) In other major modes, M-g M-g refontifies 16 lines above and below point.

With a prefix argument n, M-g M-g refontifies n lines above and below point, regardless of the mode.

To get the full benefit of Font Lock mode, you need to choose a default font which has bold, italic, and bold-italic variants; or else you need to have a color or gray-scale screen.

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 purposes of speed, some modes including C mode and Lisp mode rely on a special convention: an open-parenthesis in the leftmost column always defines the beginning of a defun, and is thus always outside any string or comment. (See Defuns.) If you don't follow this convention, then Font Lock mode can misfontify the text after an open-parenthesis 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.

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)))
PREVFaces UPFrames NEXTSupport Modes