Go to the first, previous, next, last section, table of contents.

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 t)

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 C-M-g (font-lock-fontify-block).

In certain major modes, C-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, C-M-g refontifies 16 lines above and below point.

With a prefix argument n, C-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 grayscale screen. The variable font-lock-display-type specifies whether Font Lock mode should use font styles, colors, or shades of gray to distinguish the various kinds of text. Emacs chooses the default value according to the characteristics of your display.

The variable font-lock-maximum-decoration specifies the preferred level of fontification for modes that provide multiple levels. The normal default is 1; larger numbers request more fontification, and some modes support levels as high as 3. These variables can also specify different numbers for particular major modes; for example, to use level 3 for C/C++ modes, and the default level otherwise, use this:

(setq font-lock-maximum-decoration
      '((c-mode . 3) (c++-mode . 3)))

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.


Go to the first, previous, next, last section, table of contents.