elisp: Customizing Keywords
22.6.3 Customizing Search-Based Fontification
---------------------------------------------
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.
-- Function: font-lock-add-keywords mode keywords &optional how
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 to ‘font-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 to
‘font-lock-keywords’ in the current buffer. This way of calling
‘font-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 is ‘set’, they
are used to replace the value of ‘font-lock-keywords’. If HOW is
any other non-‘nil’ value, they are added at the end of
‘font-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’, and
‘java-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 setting
‘font-lock-keywords’.
-- Function: font-lock-remove-keywords mode keywords
This function removes KEYWORDS from ‘font-lock-keywords’ for the
current buffer or for major mode MODE. As in
‘font-lock-add-keywords’, MODE should be a major mode command name
or ‘nil’. All the caveats and requirements for
‘font-lock-add-keywords’ apply here too. The argument KEYWORDS
must exactly match the one used by the corresponding
‘font-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)))))