Function: font-lock-add-keywords
font-lock-add-keywords is a byte-compiled function defined in
font-lock.el.gz.
Signature
(font-lock-add-keywords MODE KEYWORDS &optional HOW)
Documentation
Add highlighting KEYWORDS for MODE.
MODE should be a symbol, the major mode command name, such as c-mode
or nil. If nil, highlighting keywords are added for the current buffer.
KEYWORDS should be a list; see the variable font-lock-keywords.
By default they are added at the beginning of the current highlighting list.
If optional argument HOW is set, they are used to replace the current
highlighting list. If HOW is any other non-nil value, they are added at the
end of the current highlighting list.
For example:
(font-lock-add-keywords 'c-mode
'(("\\\\\\=<\\\\(FIXME\\\\):" 1 'font-lock-warning-face prepend)
("\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>" . 'font-lock-keyword-face)))
adds two fontification patterns for C mode, to fontify FIXME: words, even in
comments, and to fontify and, or and not words as keywords.
The above procedure will only add the keywords for C mode, not
for modes derived from C mode. To add them for derived modes too,
pass nil for MODE and add the call to c-mode-hook.
For example:
(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)))))
The above procedure may fail to add keywords to derived modes if some involved major mode does not follow the standard conventions. File a bug report if this happens, so the major mode can be corrected.
Note that some modes have specialized support for additional patterns, e.g.,
see the variables c-font-lock-extra-types, c++-font-lock-extra-types,
objc-font-lock-extra-types and java-font-lock-extra-types.
Probably introduced at or before Emacs version 20.1.
Aliases
mh-font-lock-add-keywords (obsolete since 29.1)
Source Code
;; Defined in /usr/src/emacs/lisp/font-lock.el.gz
(defun font-lock-add-keywords (mode keywords &optional how)
"Add highlighting KEYWORDS for MODE.
MODE should be a symbol, the major mode command name, such as `c-mode'
or nil. If nil, highlighting keywords are added for the current buffer.
KEYWORDS should be a list; see the variable `font-lock-keywords'.
By default they are added at the beginning of the current highlighting list.
If optional argument HOW is `set', they are used to replace the current
highlighting list. If HOW is any other non-nil value, they are added at the
end of the current highlighting list.
For example:
(font-lock-add-keywords \\='c-mode
\\='((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 \\='font-lock-warning-face prepend)
(\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . \\='font-lock-keyword-face)))
adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
comments, and to fontify `and', `or' and `not' words as keywords.
The above procedure will only add the keywords for C mode, not
for modes derived from C mode. To add them for derived modes too,
pass nil for MODE and add the call to `c-mode-hook'.
For example:
(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)))))
The above procedure may fail to add keywords to derived modes if
some involved major mode does not follow the standard conventions.
File a bug report if this happens, so the major mode can be corrected.
Note that some modes have specialized support for additional patterns, e.g.,
see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
`objc-font-lock-extra-types' and `java-font-lock-extra-types'."
(cond (mode
;; If MODE is non-nil, add the KEYWORDS and HOW spec to
;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
(let ((spec (cons keywords how)) cell)
(if (setq cell (assq mode font-lock-keywords-alist))
(if (eq how 'set)
(setcdr cell (list spec))
(setcdr cell (append (cdr cell) (list spec))))
(push (list mode spec) font-lock-keywords-alist)))
;; Make sure that `font-lock-removed-keywords-alist' does not
;; contain the new keywords.
(font-lock-update-removed-keyword-alist mode keywords how))
(t
(when (and font-lock-mode
(not (or font-lock-keywords font-lock-defaults)))
;; The major mode has not set any keywords, so when we enabled
;; font-lock-mode it only enabled the font-core.el part, not the
;; font-lock-mode-internal. Try again.
(font-lock-mode -1)
(setq-local font-lock-defaults '(nil t))
(font-lock-mode 1))
;; Otherwise set or add the keywords now.
;; This is a no-op if it has been done already in this buffer
;; for the correct major mode.
(font-lock-set-defaults)
(let ((was-compiled (eq (car font-lock-keywords) t)))
;; Bring back the user-level (uncompiled) keywords.
(if was-compiled
(setq font-lock-keywords (cadr font-lock-keywords)))
;; Now modify or replace them.
(if (eq how 'set)
(setq font-lock-keywords keywords)
(font-lock-remove-keywords nil keywords) ;to avoid duplicates
(let ((old (if (eq (car-safe font-lock-keywords) t)
(cdr font-lock-keywords)
font-lock-keywords)))
(setq font-lock-keywords (if how
(append old keywords)
(append keywords old)))))
;; If the keywords were compiled before, compile them again.
(if was-compiled
(setq font-lock-keywords
(font-lock-compile-keywords font-lock-keywords)))))))