Next: Other Font Lock Variables, Previous: Search-based Fontification, Up: Font Lock Mode
You can use font-lock-add-keywords
to add additional
search-based fontification rules to a major mode, and
font-lock-remove-keywords
to remove rules.
This function adds highlighting keywords, for the current buffer or for major mode mode. The argument keywords should be a list with the same format as the variable
font-lock-keywords
.If mode is a symbol which is a major mode command name, such as
c-mode
, the effect is that enabling Font Lock mode in mode will add keywords tofont-lock-keywords
. Calling with a non-nil
value of mode is correct only in your ~/.emacs file.If mode is
nil
, this function adds keywords tofont-lock-keywords
in the current buffer. This way of callingfont-lock-add-keywords
is usually used in mode hook functions.By default, keywords are added at the beginning of
font-lock-keywords
. If the optional argument how isset
, they are used to replace the value offont-lock-keywords
. If how is any other non-nil
value, they are added at the end offont-lock-keywords
.Some modes provide specialized support you can use in additional highlighting patterns. See the variables
c-font-lock-extra-types
,c++-font-lock-extra-types
, andjava-font-lock-extra-types
, for example.Warning: Major mode commands must not call
font-lock-add-keywords
under any circumstances, either directly or indirectly, except through their mode hooks. (Doing so would lead to incorrect behavior for some minor modes.) They should set up their rules for search-based fontification by settingfont-lock-keywords
.
This function removes keywords from
font-lock-keywords
for the current buffer or for major mode mode. As infont-lock-add-keywords
, mode should be a major mode command name ornil
. All the caveats and requirements forfont-lock-add-keywords
apply here too. The argument keywords must exactly match the one used by the correspondingfont-lock-add-keywords
.
For example, the following code adds two fontification patterns for C mode: one to fontify the word ‘FIXME’, even in comments, and another to fontify the words ‘and’, ‘or’ and ‘not’ as keywords.
(font-lock-add-keywords 'c-mode '(("\\<\\(FIXME\\):" 1 font-lock-warning-face prepend) ("\\<\\(and\\|or\\|not\\)\\>" . font-lock-keyword-face)))
This example affects only C mode proper. To add the same patterns to C mode and all modes derived from it, do this instead:
(add-hook 'c-mode-hook (lambda () (font-lock-add-keywords nil '(("\\<\\(FIXME\\):" 1 font-lock-warning-face prepend) ("\\<\\(and\\|or\\|not\\)\\>" . font-lock-keyword-face)))))