(enriched-face-ans): Delete special treatment for fg:... and bg:... faces.
[emacs.git] / lisp / font-lock.el
blob3ebe23ba872e5ae312e83577f958c46e822b819f
1 ;;; font-lock.el --- Electric font lock mode
3 ;; Copyright (C) 1992, 93, 94, 95, 96, 97, 98, 1999, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Author: jwz, then rms, then sm
7 ;; Maintainer: FSF
8 ;; Keywords: languages, faces
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; Font Lock mode is a minor mode that causes your comments to be displayed in
30 ;; one face, strings in another, reserved words in another, and so on.
32 ;; Comments will be displayed in `font-lock-comment-face'.
33 ;; Strings will be displayed in `font-lock-string-face'.
34 ;; Regexps are used to display selected patterns in other faces.
36 ;; To make the text you type be fontified, use M-x font-lock-mode RET.
37 ;; When this minor mode is on, the faces of the current line are updated with
38 ;; every insertion or deletion.
40 ;; To turn Font Lock mode on automatically, add this to your ~/.emacs file:
42 ;; (add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock)
44 ;; Or if you want to turn Font Lock mode on in many modes:
46 ;; (global-font-lock-mode t)
48 ;; Fontification for a particular mode may be available in a number of levels
49 ;; of decoration. The higher the level, the more decoration, but the more time
50 ;; it takes to fontify. See the variable `font-lock-maximum-decoration', and
51 ;; also the variable `font-lock-maximum-size'. Support modes for Font Lock
52 ;; mode can be used to speed up Font Lock mode. See `font-lock-support-mode'.
54 ;;; How Font Lock mode fontifies:
56 ;; When Font Lock mode is turned on in a buffer, it (a) fontifies the entire
57 ;; buffer and (b) installs one of its fontification functions on one of the
58 ;; hook variables that are run by Emacs after every buffer change (i.e., an
59 ;; insertion or deletion). Fontification means the replacement of `face' text
60 ;; properties in a given region; Emacs displays text with these `face' text
61 ;; properties appropriately.
63 ;; Fontification normally involves syntactic (i.e., strings and comments) and
64 ;; regexp (i.e., keywords and everything else) passes. There are actually
65 ;; three passes; (a) the syntactic keyword pass, (b) the syntactic pass and (c)
66 ;; the keyword pass. Confused?
68 ;; The syntactic keyword pass places `syntax-table' text properties in the
69 ;; buffer according to the variable `font-lock-syntactic-keywords'. It is
70 ;; necessary because Emacs' syntax table is not powerful enough to describe all
71 ;; the different syntactic constructs required by the sort of people who decide
72 ;; that a single quote can be syntactic or not depending on the time of day.
73 ;; (What sort of person could decide to overload the meaning of a quote?)
74 ;; Obviously the syntactic keyword pass must occur before the syntactic pass.
76 ;; The syntactic pass places `face' text properties in the buffer according to
77 ;; syntactic context, i.e., according to the buffer's syntax table and buffer
78 ;; text's `syntax-table' text properties. It involves using a syntax parsing
79 ;; function to determine the context of different parts of a region of text. A
80 ;; syntax parsing function is necessary because generally strings and/or
81 ;; comments can span lines, and so the context of a given region is not
82 ;; necessarily apparent from the content of that region. Because the keyword
83 ;; pass only works within a given region, it is not generally appropriate for
84 ;; syntactic fontification. This is the first fontification pass that makes
85 ;; changes visible to the user; it fontifies strings and comments.
87 ;; The keyword pass places `face' text properties in the buffer according to
88 ;; the variable `font-lock-keywords'. It involves searching for given regexps
89 ;; (or calling given search functions) within the given region. This is the
90 ;; second fontification pass that makes changes visible to the user; it
91 ;; fontifies language reserved words, etc.
93 ;; Oh, and the answer is, "Yes, obviously just about everything should be done
94 ;; in a single syntactic pass, but the only syntactic parser available
95 ;; understands only strings and comments." Perhaps one day someone will write
96 ;; some syntactic parsers for common languages and a son-of-font-lock.el could
97 ;; use them rather then relying so heavily on the keyword (regexp) pass.
99 ;;; How Font Lock mode supports modes or is supported by modes:
101 ;; Modes that support Font Lock mode do so by defining one or more variables
102 ;; whose values specify the fontification. Font Lock mode knows of these
103 ;; variable names from (a) the buffer local variable `font-lock-defaults', if
104 ;; non-nil, or (b) the global variable `font-lock-defaults-alist', if the major
105 ;; mode has an entry. (Font Lock mode is set up via (a) where a mode's
106 ;; patterns are distributed with the mode's package library, and (b) where a
107 ;; mode's patterns are distributed with font-lock.el itself. An example of (a)
108 ;; is Pascal mode, an example of (b) is Lisp mode. Normally, the mechanism is
109 ;; (a); (b) is used where it is not clear which package library should contain
110 ;; the pattern definitions.) Font Lock mode chooses which variable to use for
111 ;; fontification based on `font-lock-maximum-decoration'.
113 ;; Font Lock mode fontification behaviour can be modified in a number of ways.
114 ;; See the below comments and the comments distributed throughout this file.
116 ;;; Constructing patterns:
118 ;; See the documentation for the variable `font-lock-keywords'.
120 ;; Efficient regexps for use as MATCHERs for `font-lock-keywords' and
121 ;; `font-lock-syntactic-keywords' can be generated via the function
122 ;; `regexp-opt'.
124 ;;; Adding patterns for modes that already support Font Lock:
126 ;; Though Font Lock highlighting patterns already exist for many modes, it's
127 ;; likely there's something that you want fontified that currently isn't, even
128 ;; at the maximum fontification level. You can add highlighting patterns via
129 ;; `font-lock-add-keywords'. For example, say in some C
130 ;; header file you #define the token `and' to expand to `&&', etc., to make
131 ;; your C code almost readable. In your ~/.emacs there could be:
133 ;; (font-lock-add-keywords 'c-mode '("\\<\\(and\\|or\\|not\\)\\>"))
135 ;; Some modes provide specific ways to modify patterns based on the values of
136 ;; other variables. For example, additional C types can be specified via the
137 ;; variable `c-font-lock-extra-types'.
139 ;;; Adding patterns for modes that do not support Font Lock:
141 ;; Not all modes support Font Lock mode. If you (as a user of the mode) add
142 ;; patterns for a new mode, you must define in your ~/.emacs a variable or
143 ;; variables that specify regexp fontification. Then, you should indicate to
144 ;; Font Lock mode, via the mode hook setting `font-lock-defaults', exactly what
145 ;; support is required. For example, say Foo mode should have the following
146 ;; regexps fontified case-sensitively, and comments and strings should not be
147 ;; fontified automagically. In your ~/.emacs there could be:
149 ;; (defvar foo-font-lock-keywords
150 ;; '(("\\<\\(one\\|two\\|three\\)\\>" . font-lock-keyword-face)
151 ;; ("\\<\\(four\\|five\\|six\\)\\>" . font-lock-type-face))
152 ;; "Default expressions to highlight in Foo mode.")
154 ;; (add-hook 'foo-mode-hook
155 ;; (function (lambda ()
156 ;; (make-local-variable 'font-lock-defaults)
157 ;; (setq font-lock-defaults '(foo-font-lock-keywords t)))))
159 ;;; Adding Font Lock support for modes:
161 ;; Of course, it would be better that the mode already supports Font Lock mode.
162 ;; The package author would do something similar to above. The mode must
163 ;; define at the top-level a variable or variables that specify regexp
164 ;; fontification. Then, the mode command should indicate to Font Lock mode,
165 ;; via `font-lock-defaults', exactly what support is required. For example,
166 ;; say Bar mode should have the following regexps fontified case-insensitively,
167 ;; and comments and strings should be fontified automagically. In bar.el there
168 ;; could be:
170 ;; (defvar bar-font-lock-keywords
171 ;; '(("\\<\\(uno\\|due\\|tre\\)\\>" . font-lock-keyword-face)
172 ;; ("\\<\\(quattro\\|cinque\\|sei\\)\\>" . font-lock-type-face))
173 ;; "Default expressions to highlight in Bar mode.")
175 ;; and within `bar-mode' there could be:
177 ;; (make-local-variable 'font-lock-defaults)
178 ;; (setq font-lock-defaults '(bar-font-lock-keywords nil t))
180 ;; What is fontification for? You might say, "It's to make my code look nice."
181 ;; I think it should be for adding information in the form of cues. These cues
182 ;; should provide you with enough information to both (a) distinguish between
183 ;; different items, and (b) identify the item meanings, without having to read
184 ;; the items and think about it. Therefore, fontification allows you to think
185 ;; less about, say, the structure of code, and more about, say, why the code
186 ;; doesn't work. Or maybe it allows you to think less and drift off to sleep.
188 ;; So, here are my opinions/advice/guidelines:
190 ;; - Highlight conceptual objects, such as function and variable names, and
191 ;; different objects types differently, i.e., (a) and (b) above, highlight
192 ;; function names differently to variable names.
193 ;; - Keep the faces distinct from each other as far as possible.
194 ;; i.e., (a) above.
195 ;; - Use the same face for the same conceptual object, across all modes.
196 ;; i.e., (b) above, all modes that have items that can be thought of as, say,
197 ;; keywords, should be highlighted with the same face, etc.
198 ;; - Make the face attributes fit the concept as far as possible.
199 ;; i.e., function names might be a bold colour such as blue, comments might
200 ;; be a bright colour such as red, character strings might be brown, because,
201 ;; err, strings are brown (that was not the reason, please believe me).
202 ;; - Don't use a non-nil OVERRIDE unless you have a good reason.
203 ;; Only use OVERRIDE for special things that are easy to define, such as the
204 ;; way `...' quotes are treated in strings and comments in Emacs Lisp mode.
205 ;; Don't use it to, say, highlight keywords in commented out code or strings.
206 ;; - Err, that's it.
208 ;;; Code:
210 (require 'syntax)
212 ;; Define core `font-lock' group.
213 (defgroup font-lock nil
214 "Font Lock mode text highlighting package."
215 :link '(custom-manual "(emacs)Font Lock")
216 :link '(custom-manual "(elisp)Font Lock Mode")
217 :group 'faces)
219 (defgroup font-lock-highlighting-faces nil
220 "Faces for highlighting text."
221 :prefix "font-lock-"
222 :group 'font-lock)
224 (defgroup font-lock-extra-types nil
225 "Extra mode-specific type names for highlighting declarations."
226 :group 'font-lock)
228 ;; Define support mode groups here to impose `font-lock' group order.
229 (defgroup fast-lock nil
230 "Font Lock support mode to cache fontification."
231 :link '(custom-manual "(emacs)Support Modes")
232 :load 'fast-lock
233 :group 'font-lock)
235 (defgroup lazy-lock nil
236 "Font Lock support mode to fontify lazily."
237 :link '(custom-manual "(emacs)Support Modes")
238 :load 'lazy-lock
239 :group 'font-lock)
241 (defgroup jit-lock nil
242 "Font Lock support mode to fontify just-in-time."
243 :link '(custom-manual "(emacs)Support Modes")
244 :version "21.1"
245 :load 'jit-lock
246 :group 'font-lock)
248 ;; User variables.
250 (defcustom font-lock-maximum-size 256000
251 "*Maximum size of a buffer for buffer fontification.
252 Only buffers less than this can be fontified when Font Lock mode is turned on.
253 If nil, means size is irrelevant.
254 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
255 where MAJOR-MODE is a symbol or t (meaning the default). For example:
256 ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576))
257 means that the maximum size is 250K for buffers in C or C++ modes, one megabyte
258 for buffers in Rmail mode, and size is irrelevant otherwise."
259 :type '(choice (const :tag "none" nil)
260 (integer :tag "size")
261 (repeat :menu-tag "mode specific" :tag "mode specific"
262 :value ((t . nil))
263 (cons :tag "Instance"
264 (radio :tag "Mode"
265 (const :tag "all" t)
266 (symbol :tag "name"))
267 (radio :tag "Size"
268 (const :tag "none" nil)
269 (integer :tag "size")))))
270 :group 'font-lock)
272 (defcustom font-lock-maximum-decoration t
273 "*Maximum decoration level for fontification.
274 If nil, use the default decoration (typically the minimum available).
275 If t, use the maximum decoration available.
276 If a number, use that level of decoration (or if not available the maximum).
277 If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
278 where MAJOR-MODE is a symbol or t (meaning the default). For example:
279 ((c-mode . t) (c++-mode . 2) (t . 1))
280 means use the maximum decoration available for buffers in C mode, level 2
281 decoration for buffers in C++ mode, and level 1 decoration otherwise."
282 :type '(choice (const :tag "default" nil)
283 (const :tag "maximum" t)
284 (integer :tag "level" 1)
285 (repeat :menu-tag "mode specific" :tag "mode specific"
286 :value ((t . t))
287 (cons :tag "Instance"
288 (radio :tag "Mode"
289 (const :tag "all" t)
290 (symbol :tag "name"))
291 (radio :tag "Decoration"
292 (const :tag "default" nil)
293 (const :tag "maximum" t)
294 (integer :tag "level" 1)))))
295 :group 'font-lock)
297 (defcustom font-lock-verbose 0
298 "*If non-nil, means show status messages for buffer fontification.
299 If a number, only buffers greater than this size have fontification messages."
300 :type '(choice (const :tag "never" nil)
301 (other :tag "always" t)
302 (integer :tag "size"))
303 :group 'font-lock)
306 ;; Originally these variable values were face names such as `bold' etc.
307 ;; Now we create our own faces, but we keep these variables for compatibility
308 ;; and they give users another mechanism for changing face appearance.
309 ;; We now allow a FACENAME in `font-lock-keywords' to be any expression that
310 ;; returns a face. So the easiest thing is to continue using these variables,
311 ;; rather than sometimes evaling FACENAME and sometimes not. sm.
312 (defvar font-lock-comment-face 'font-lock-comment-face
313 "Face name to use for comments.")
315 (defvar font-lock-string-face 'font-lock-string-face
316 "Face name to use for strings.")
318 (defvar font-lock-doc-face 'font-lock-doc-face
319 "Face name to use for documentation.")
321 (defvar font-lock-keyword-face 'font-lock-keyword-face
322 "Face name to use for keywords.")
324 (defvar font-lock-builtin-face 'font-lock-builtin-face
325 "Face name to use for builtins.")
327 (defvar font-lock-function-name-face 'font-lock-function-name-face
328 "Face name to use for function names.")
330 (defvar font-lock-variable-name-face 'font-lock-variable-name-face
331 "Face name to use for variable names.")
333 (defvar font-lock-type-face 'font-lock-type-face
334 "Face name to use for type and class names.")
336 (defvar font-lock-constant-face 'font-lock-constant-face
337 "Face name to use for constant and label names.")
339 (defvar font-lock-warning-face 'font-lock-warning-face
340 "Face name to use for things that should stand out.")
342 (defvar font-lock-reference-face 'font-lock-constant-face
343 "This variable is obsolete. Use `font-lock-constant-face'.")
345 ;; Fontification variables:
347 (defvar font-lock-keywords nil
348 "A list of the keywords to highlight.
349 Each element should have one of these forms:
351 MATCHER
352 (MATCHER . MATCH)
353 (MATCHER . FACENAME)
354 (MATCHER . HIGHLIGHT)
355 (MATCHER HIGHLIGHT ...)
356 (eval . FORM)
358 where MATCHER can be either the regexp to search for, or the function name to
359 call to make the search (called with one argument, the limit of the search) and
360 return non-nil if it succeeds (and set `match-data' appropriately).
361 MATCHER regexps can be generated via the function `regexp-opt'.
363 FORM is an expression, whose value should be a keyword element, evaluated when
364 the keyword is (first) used in a buffer. This feature can be used to provide a
365 keyword that can only be generated when Font Lock mode is actually turned on.
367 HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
369 For highlighting single items, for example each instance of the word \"foo\",
370 typically only MATCH-HIGHLIGHT is required.
371 However, if an item or (typically) items are to be highlighted following the
372 instance of another item (the anchor), for example each instance of the
373 word \"bar\" following the word \"anchor\" then MATCH-ANCHORED may be required.
375 MATCH-HIGHLIGHT should be of the form:
377 (MATCH FACENAME OVERRIDE LAXMATCH)
379 MATCH is the subexpression of MATCHER to be highlighted. FACENAME is an
380 expression whose value is the face name to use. Face default attributes
381 can be modified via \\[customize]. Instead of a face, FACENAME can
382 evaluate to a property list of the form (face VAL1 PROP2 VAL2 PROP3 VAL3 ...)
383 in which case all the listed text-properties will be set rather than
384 just `face'. In such a case, you will most likely want to put those
385 properties in `font-lock-extra-managed-props' or to override
386 `font-lock-unfontify-region-function'.
388 OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification can
389 be overwritten. If `keep', only parts not already fontified are highlighted.
390 If `prepend' or `append', existing fontification is merged with the new, in
391 which the new or existing fontification, respectively, takes precedence.
392 If LAXMATCH is non-nil, no error is signaled if there is no MATCH in MATCHER.
394 For example, an element of the form highlights (if not already highlighted):
396 \"\\\\\\=<foo\\\\\\=>\" discrete occurrences of \"foo\" in the value of the
397 variable `font-lock-keyword-face'.
398 (\"fu\\\\(bar\\\\)\" . 1) substring \"bar\" within all occurrences of \"fubar\" in
399 the value of `font-lock-keyword-face'.
400 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'.
401 (\"foo\\\\|bar\" 0 foo-bar-face t)
402 occurrences of either \"foo\" or \"bar\" in the value
403 of `foo-bar-face', even if already highlighted.
404 (fubar-match 1 fubar-face)
405 the first subexpression within all occurrences of
406 whatever the function `fubar-match' finds and matches
407 in the value of `fubar-face'.
409 MATCH-ANCHORED should be of the form:
411 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
413 where MATCHER is a regexp to search for or the function name to call to make
414 the search, as for MATCH-HIGHLIGHT above, but with one exception; see below.
415 PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
416 the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be
417 used to initialise before, and cleanup after, MATCHER is used. Typically,
418 PRE-MATCH-FORM is used to move to some position relative to the original
419 MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might
420 be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER.
422 For example, an element of the form highlights (if not already highlighted):
424 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
426 discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
427 discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
428 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is
429 initially searched for starting from the end of the match of \"anchor\", and
430 searching for subsequent instance of \"anchor\" resumes from where searching
431 for \"item\" concluded.)
433 The above-mentioned exception is as follows. The limit of the MATCHER search
434 defaults to the end of the line after PRE-MATCH-FORM is evaluated.
435 However, if PRE-MATCH-FORM returns a position greater than the position after
436 PRE-MATCH-FORM is evaluated, that position is used as the limit of the search.
437 It is generally a bad idea to return a position greater than the end of the
438 line, i.e., cause the MATCHER search to span lines.
440 These regular expressions can match text which spans lines, although
441 it is better to avoid it if possible since updating them while editing
442 text is slower, and it is not guaranteed to be always correct when using
443 support modes like jit-lock or lazy-lock.
445 This variable is set by major modes via the variable `font-lock-defaults'.
446 Be careful when composing regexps for this list; a poorly written pattern can
447 dramatically slow things down!")
449 ;; This variable is used by mode packages that support Font Lock mode by
450 ;; defining their own keywords to use for `font-lock-keywords'. (The mode
451 ;; command should make it buffer-local and set it to provide the set up.)
452 (defvar font-lock-defaults nil
453 "Defaults for Font Lock mode specified by the major mode.
454 Defaults should be of the form:
456 (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN ...)
458 KEYWORDS may be a symbol (a variable or function whose value is the keywords to
459 use for fontification) or a list of symbols. If KEYWORDS-ONLY is non-nil,
460 syntactic fontification (strings and comments) is not performed.
461 If CASE-FOLD is non-nil, the case of the keywords is ignored when fontifying.
462 If SYNTAX-ALIST is non-nil, it should be a list of cons pairs of the form
463 \(CHAR-OR-STRING . STRING) used to set the local Font Lock syntax table, for
464 keyword and syntactic fontification (see `modify-syntax-entry').
466 If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
467 backwards outside any enclosing syntactic block, for syntactic fontification.
468 Typical values are `beginning-of-line' (i.e., the start of the line is known to
469 be outside a syntactic block), or `beginning-of-defun' for programming modes or
470 `backward-paragraph' for textual modes (i.e., the mode-dependent function is
471 known to move outside a syntactic block). If nil, the beginning of the buffer
472 is used as a position outside of a syntactic block, in the worst case.
474 These item elements are used by Font Lock mode to set the variables
475 `font-lock-keywords', `font-lock-keywords-only',
476 `font-lock-keywords-case-fold-search', `font-lock-syntax-table' and
477 `font-lock-beginning-of-syntax-function', respectively.
479 Further item elements are alists of the form (VARIABLE . VALUE) and are in no
480 particular order. Each VARIABLE is made buffer-local before set to VALUE.
482 Currently, appropriate variables include `font-lock-mark-block-function'.
483 If this is non-nil, it should be a function with no args used to mark any
484 enclosing block of text, for fontification via \\[font-lock-fontify-block].
485 Typical values are `mark-defun' for programming modes or `mark-paragraph' for
486 textual modes (i.e., the mode-dependent function is known to put point and mark
487 around a text block relevant to that mode).
489 Other variables include that for syntactic keyword fontification,
490 `font-lock-syntactic-keywords'
491 and those for buffer-specialised fontification functions,
492 `font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function',
493 `font-lock-fontify-region-function', `font-lock-unfontify-region-function',
494 `font-lock-inhibit-thing-lock' and `font-lock-maximum-size'.")
495 ;;;###autoload
496 (make-variable-buffer-local 'font-lock-defaults)
498 ;; This variable is used where font-lock.el itself supplies the keywords.
499 (defvar font-lock-defaults-alist
500 (let (;; We use `beginning-of-defun', rather than nil, for SYNTAX-BEGIN.
501 ;; Thus the calculation of the cache is usually faster but not
502 ;; infallible, so we risk mis-fontification. sm.
503 (c-mode-defaults
504 '((c-font-lock-keywords c-font-lock-keywords-1
505 c-font-lock-keywords-2 c-font-lock-keywords-3)
506 nil nil ((?_ . "w")) beginning-of-defun
507 (font-lock-syntactic-face-function
508 . c-font-lock-syntactic-face-function)
509 (font-lock-mark-block-function . mark-defun)))
510 (c++-mode-defaults
511 '((c++-font-lock-keywords c++-font-lock-keywords-1
512 c++-font-lock-keywords-2 c++-font-lock-keywords-3)
513 nil nil ((?_ . "w")) beginning-of-defun
514 (font-lock-syntactic-face-function
515 . c-font-lock-syntactic-face-function)
516 (font-lock-mark-block-function . mark-defun)))
517 (objc-mode-defaults
518 '((objc-font-lock-keywords objc-font-lock-keywords-1
519 objc-font-lock-keywords-2 objc-font-lock-keywords-3)
520 nil nil ((?_ . "w") (?$ . "w")) nil
521 (font-lock-syntactic-face-function
522 . c-font-lock-syntactic-face-function)
523 (font-lock-mark-block-function . mark-defun)))
524 (java-mode-defaults
525 '((java-font-lock-keywords java-font-lock-keywords-1
526 java-font-lock-keywords-2 java-font-lock-keywords-3)
527 nil nil ((?_ . "w") (?$ . "w")) nil
528 (font-lock-syntactic-face-function
529 . java-font-lock-syntactic-face-function)
530 (font-lock-mark-block-function . mark-defun))))
531 (list
532 (cons 'c-mode c-mode-defaults)
533 (cons 'c++-mode c++-mode-defaults)
534 (cons 'objc-mode objc-mode-defaults)
535 (cons 'java-mode java-mode-defaults)))
536 "Alist of fall-back Font Lock defaults for major modes.
538 This variable should not be used any more.
539 Set the buffer-local `font-lock-keywords' in the major mode instead.
541 Each item should be a list of the form:
543 (MAJOR-MODE . FONT-LOCK-DEFAULTS)
545 where MAJOR-MODE is a symbol and FONT-LOCK-DEFAULTS is a list of default
546 settings. See the variable `font-lock-defaults', which takes precedence.")
547 (make-obsolete-variable 'font-lock-defaults-alist 'font-lock-defaults)
549 (defvar font-lock-keywords-alist nil
550 "*Alist of `font-lock-keywords' local to a `major-mode'.
551 This is normally set via `font-lock-add-keywords' and
552 `font-lock-remove-keywords'.")
554 (defvar font-lock-removed-keywords-alist nil
555 "*Alist of `font-lock-keywords' removed from `major-mode'.
556 This is normally set via `font-lock-add-keywords' and
557 `font-lock-remove-keywords'.")
559 (defvar font-lock-keywords-only nil
560 "*Non-nil means Font Lock should not fontify comments or strings.
561 This is normally set via `font-lock-defaults'.")
563 (defvar font-lock-keywords-case-fold-search nil
564 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
565 This is normally set via `font-lock-defaults'.")
566 (make-variable-buffer-local 'font-lock-keywords-case-fold-search)
568 (defvar font-lock-syntactically-fontified 0
569 "Point up to which `font-lock-syntactic-keywords' has been applied.
570 If nil, this is ignored, in which case the syntactic fontification may
571 sometimes be slightly incorrect.")
572 (make-variable-buffer-local 'font-lock-syntactically-fontified)
574 (defvar font-lock-syntactic-face-function
575 (lambda (state)
576 (if (nth 3 state) font-lock-string-face font-lock-comment-face))
577 "Function to determine which face to use when fontifying syntactically.
578 The function is called with a single parameter (the state as returned by
579 `parse-partial-sexp' at the beginning of the region to highlight) and
580 should return a face.")
582 (defvar font-lock-syntactic-keywords nil
583 "A list of the syntactic keywords to highlight.
584 Can be the list or the name of a function or variable whose value is the list.
585 See `font-lock-keywords' for a description of the form of this list;
586 the differences are listed below. MATCH-HIGHLIGHT should be of the form:
588 (MATCH SYNTAX OVERRIDE LAXMATCH)
590 where SYNTAX can be a string (as taken by `modify-syntax-entry'), a syntax
591 table, a cons cell (as returned by `string-to-syntax') or an expression whose
592 value is such a form. OVERRIDE cannot be `prepend' or `append'.
594 For example, an element of the form highlights syntactically:
596 (\"\\\\$\\\\(#\\\\)\" 1 \".\")
598 a hash character when following a dollar character, with a SYNTAX of
599 \".\" (meaning punctuation syntax). Assuming that the buffer syntax table does
600 specify hash characters to have comment start syntax, the element will only
601 highlight hash characters that do not follow dollar characters as comments
602 syntactically.
604 (\"\\\\('\\\\).\\\\('\\\\)\"
605 (1 \"\\\"\")
606 (2 \"\\\"\"))
608 both single quotes which surround a single character, with a SYNTAX of
609 \"\\\"\" (meaning string quote syntax). Assuming that the buffer syntax table
610 does not specify single quotes to have quote syntax, the element will only
611 highlight single quotes of the form 'c' as strings syntactically.
612 Other forms, such as foo'bar or 'fubar', will not be highlighted as strings.
614 This is normally set via `font-lock-defaults'.")
616 (defvar font-lock-syntax-table nil
617 "Non-nil means use this syntax table for fontifying.
618 If this is nil, the major mode's syntax table is used.
619 This is normally set via `font-lock-defaults'.")
621 (defvar font-lock-beginning-of-syntax-function nil
622 "*Non-nil means use this function to move back outside of a syntactic block.
623 When called with no args it should leave point at the beginning of any
624 enclosing syntactic block.
625 If this is nil, the beginning of the buffer is used (in the worst case).
626 This is normally set via `font-lock-defaults'.
627 It is preferable to set `syntax-begin-function' instead.")
629 (defvar font-lock-mark-block-function nil
630 "*Non-nil means use this function to mark a block of text.
631 When called with no args it should leave point at the beginning of any
632 enclosing textual block and mark at the end.
633 This is normally set via `font-lock-defaults'.")
635 (defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
636 "Function to use for fontifying the buffer.
637 This is normally set via `font-lock-defaults'.")
639 (defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
640 "Function to use for unfontifying the buffer.
641 This is used when turning off Font Lock mode.
642 This is normally set via `font-lock-defaults'.")
644 (defvar font-lock-fontify-region-function 'font-lock-default-fontify-region
645 "Function to use for fontifying a region.
646 It should take two args, the beginning and end of the region, and an optional
647 third arg VERBOSE. If non-nil, the function should print status messages.
648 This is normally set via `font-lock-defaults'.")
650 (defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
651 "Function to use for unfontifying a region.
652 It should take two args, the beginning and end of the region.
653 This is normally set via `font-lock-defaults'.")
655 (defvar font-lock-inhibit-thing-lock nil
656 "List of Font Lock mode related modes that should not be turned on.
657 Currently, valid mode names are `fast-lock-mode', `jit-lock-mode' and
658 `lazy-lock-mode'. This is normally set via `font-lock-defaults'.")
660 (defvar font-lock-multiline nil
661 "Whether font-lock should cater to multiline keywords.
662 If nil, don't try to handle multiline patterns.
663 If t, always handle multiline patterns.
664 If `undecided', don't try to handle multiline patterns until you see one.
665 Major/minor modes can set this variable if they know which option applies.")
667 (defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
669 ;; Font Lock mode.
671 (eval-when-compile
673 ;; We don't do this at the top-level as we only use non-autoloaded macros.
674 (require 'cl)
676 ;; Borrowed from lazy-lock.el.
677 ;; We use this to preserve or protect things when modifying text properties.
678 (defmacro save-buffer-state (varlist &rest body)
679 "Bind variables according to VARLIST and eval BODY restoring buffer state."
680 (let ((modified (make-symbol "modified")))
681 `(let* ,(append varlist
682 `((,modified (buffer-modified-p))
683 (buffer-undo-list t)
684 (inhibit-read-only t)
685 (inhibit-point-motion-hooks t)
686 (inhibit-modification-hooks t)
687 deactivate-mark
688 buffer-file-name
689 buffer-file-truename))
690 (progn
691 ,@body)
692 (unless ,modified
693 (restore-buffer-modified-p nil)))))
694 (put 'save-buffer-state 'lisp-indent-function 1)
695 (def-edebug-spec save-buffer-state let)
697 ;; Shut up the byte compiler.
698 (defvar font-lock-face-attributes)) ; Obsolete but respected if set.
700 ;;;###autoload
701 (define-minor-mode font-lock-mode
702 "Toggle Font Lock mode.
703 With arg, turn Font Lock mode off if and only if arg is a non-positive
704 number; if arg is nil, toggle Font Lock mode; anything else turns Font
705 Lock on.
706 \(Font Lock is also known as \"syntax highlighting\".)
708 When Font Lock mode is enabled, text is fontified as you type it:
710 - Comments are displayed in `font-lock-comment-face';
711 - Strings are displayed in `font-lock-string-face';
712 - Certain other expressions are displayed in other faces according to the
713 value of the variable `font-lock-keywords'.
715 To customize the faces (colors, fonts, etc.) used by Font Lock for
716 fontifying different parts of buffer text, use \\[customize-face].
718 You can enable Font Lock mode in any major mode automatically by turning on in
719 the major mode's hook. For example, put in your ~/.emacs:
721 (add-hook 'c-mode-hook 'turn-on-font-lock)
723 Alternatively, you can use Global Font Lock mode to automagically turn on Font
724 Lock mode in buffers whose major mode supports it and whose major mode is one
725 of `font-lock-global-modes'. For example, put in your ~/.emacs:
727 (global-font-lock-mode t)
729 There are a number of support modes that may be used to speed up Font Lock mode
730 in various ways, specified via the variable `font-lock-support-mode'. Where
731 major modes support different levels of fontification, you can use the variable
732 `font-lock-maximum-decoration' to specify which level you generally prefer.
733 When you turn Font Lock mode on/off the buffer is fontified/defontified, though
734 fontification occurs only if the buffer is less than `font-lock-maximum-size'.
736 For example, to specify that Font Lock mode use use Lazy Lock mode as a support
737 mode and use maximum levels of fontification, put in your ~/.emacs:
739 (setq font-lock-support-mode 'lazy-lock-mode)
740 (setq font-lock-maximum-decoration t)
742 To add your own highlighting for some major mode, and modify the highlighting
743 selected automatically via the variable `font-lock-maximum-decoration', you can
744 use `font-lock-add-keywords'.
746 To fontify a buffer, without turning on Font Lock mode and regardless of buffer
747 size, you can use \\[font-lock-fontify-buffer].
749 To fontify a block (the function or paragraph containing point, or a number of
750 lines around point), perhaps because modification on the current line caused
751 syntactic change on other lines, you can use \\[font-lock-fontify-block].
753 See the variable `font-lock-defaults-alist' for the Font Lock mode default
754 settings. You can set your own default settings for some mode, by setting a
755 buffer local value for `font-lock-defaults', via its mode hook."
756 nil nil nil
757 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
758 ;; batch job) or if the buffer is invisible (the name starts with a space).
759 (when (or noninteractive (eq (aref (buffer-name) 0) ?\ ))
760 (setq font-lock-mode nil))
762 ;; Turn on Font Lock mode.
763 (when font-lock-mode
764 (add-hook 'after-change-functions 'font-lock-after-change-function t t)
765 (font-lock-set-defaults)
766 (font-lock-turn-on-thing-lock)
767 ;; Fontify the buffer if we have to.
768 (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size)))
769 (cond (font-lock-fontified
770 nil)
771 ((or (null max-size) (> max-size (buffer-size)))
772 (font-lock-fontify-buffer))
773 (font-lock-verbose
774 (message "Fontifying %s...buffer size greater than font-lock-maximum-size"
775 (buffer-name))))))
776 ;; Turn off Font Lock mode.
777 (unless font-lock-mode
778 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
779 (font-lock-unfontify-buffer)
780 (font-lock-turn-off-thing-lock)))
782 ;;;###autoload
783 (defun turn-on-font-lock ()
784 "Turn on Font Lock mode (only if the terminal can display it)."
785 (unless font-lock-mode
786 (font-lock-mode)))
788 ;;;###autoload
789 (defun font-lock-add-keywords (mode keywords &optional append)
790 "Add highlighting KEYWORDS for MODE.
791 MODE should be a symbol, the major mode command name, such as `c-mode'
792 or nil. If nil, highlighting keywords are added for the current buffer.
793 KEYWORDS should be a list; see the variable `font-lock-keywords'.
794 By default they are added at the beginning of the current highlighting list.
795 If optional argument APPEND is `set', they are used to replace the current
796 highlighting list. If APPEND is any other non-nil value, they are added at the
797 end of the current highlighting list.
799 For example:
801 (font-lock-add-keywords 'c-mode
802 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
803 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . font-lock-keyword-face)))
805 adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
806 comments, and to fontify `and', `or' and `not' words as keywords.
808 When used from an elisp package (such as a minor mode), it is recommended
809 to use nil for MODE (and place the call in a loop or on a hook) to avoid
810 subtle problems due to details of the implementation.
812 Note that some modes have specialised support for additional patterns, e.g.,
813 see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
814 `objc-font-lock-extra-types' and `java-font-lock-extra-types'."
815 (cond (mode
816 ;; If MODE is non-nil, add the KEYWORDS and APPEND spec to
817 ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
818 (let ((spec (cons keywords append)) cell)
819 (if (setq cell (assq mode font-lock-keywords-alist))
820 (if (eq append 'set)
821 (setcdr cell (list spec))
822 (setcdr cell (append (cdr cell) (list spec))))
823 (push (list mode spec) font-lock-keywords-alist)))
824 ;; Make sure that `font-lock-removed-keywords-alist' does not
825 ;; contain the new keywords.
826 (font-lock-update-removed-keyword-alist mode keywords append))
828 ;; Otherwise set or add the keywords now.
829 (font-lock-set-defaults)
830 (if (eq append 'set)
831 (setq font-lock-keywords keywords)
832 (font-lock-remove-keywords nil keywords) ;to avoid duplicates
833 (let ((old (if (eq (car-safe font-lock-keywords) t)
834 (cdr font-lock-keywords)
835 font-lock-keywords)))
836 (setq font-lock-keywords (if append
837 (append old keywords)
838 (append keywords old))))))))
840 (defun font-lock-update-removed-keyword-alist (mode keywords append)
841 ;; Update `font-lock-removed-keywords-alist' when adding new
842 ;; KEYWORDS to MODE.
844 ;; When font-lock is enabled first all keywords in the list
845 ;; `font-lock-keywords-alist' are added, then all keywords in the
846 ;; list `font-lock-removed-keywords-alist' are removed. If a
847 ;; keyword was once added, removed, and then added again it must be
848 ;; removed from the removed-keywords list. Otherwise the second add
849 ;; will not take effect.
850 (let ((cell (assq mode font-lock-removed-keywords-alist)))
851 (if cell
852 (if (eq append 'set)
853 ;; A new set of keywords is defined. Forget all about
854 ;; our old keywords that should be removed.
855 (setq font-lock-removed-keywords-alist
856 (delq cell font-lock-removed-keywords-alist))
857 ;; Delete all previously removed keywords.
858 (dolist (kword keywords)
859 (setcdr cell (delete kword (cdr cell))))
860 ;; Delete the mode cell if empty.
861 (if (null (cdr cell))
862 (setq font-lock-removed-keywords-alist
863 (delq cell font-lock-removed-keywords-alist)))))))
865 ;; Written by Anders Lindgren <andersl@andersl.com>.
867 ;; Case study:
868 ;; (I) The keywords are removed from a major mode.
869 ;; In this case the keyword could be local (i.e. added earlier by
870 ;; `font-lock-add-keywords'), global, or both.
872 ;; (a) In the local case we remove the keywords from the variable
873 ;; `font-lock-keywords-alist'.
875 ;; (b) The actual global keywords are not known at this time.
876 ;; All keywords are added to `font-lock-removed-keywords-alist',
877 ;; when font-lock is enabled those keywords are removed.
879 ;; Note that added keywords are taken out of the list of removed
880 ;; keywords. This ensure correct operation when the same keyword
881 ;; is added and removed several times.
883 ;; (II) The keywords are removed from the current buffer.
884 ;;;###autoload
885 (defun font-lock-remove-keywords (mode keywords)
886 "Remove highlighting KEYWORDS for MODE.
888 MODE should be a symbol, the major mode command name, such as `c-mode'
889 or nil. If nil, highlighting keywords are removed for the current buffer.
891 When used from an elisp package (such as a minor mode), it is recommended
892 to use nil for MODE (and place the call in a loop or on a hook) to avoid
893 subtle problems due to details of the implementation."
894 (cond (mode
895 ;; Remove one keyword at the time.
896 (dolist (keyword keywords)
897 (let ((top-cell (assq mode font-lock-keywords-alist)))
898 ;; If MODE is non-nil, remove the KEYWORD from
899 ;; `font-lock-keywords-alist'.
900 (when top-cell
901 (dolist (keyword-list-append-pair (cdr top-cell))
902 ;; `keywords-list-append-pair' is a cons with a list of
903 ;; keywords in the car top-cell and the original append
904 ;; argument in the cdr top-cell.
905 (setcar keyword-list-append-pair
906 (delete keyword (car keyword-list-append-pair))))
907 ;; Remove keyword list/append pair when the keyword list
908 ;; is empty and append doesn't specify `set'. (If it
909 ;; should be deleted then previously deleted keywords
910 ;; would appear again.)
911 (let ((cell top-cell))
912 (while (cdr cell)
913 (if (and (null (car (car (cdr cell))))
914 (not (eq (cdr (car (cdr cell))) 'set)))
915 (setcdr cell (cdr (cdr cell)))
916 (setq cell (cdr cell)))))
917 ;; Final cleanup, remove major mode cell if last keyword
918 ;; was deleted.
919 (if (null (cdr top-cell))
920 (setq font-lock-keywords-alist
921 (delq top-cell font-lock-keywords-alist))))
922 ;; Remember the keyword in case it is not local.
923 (let ((cell (assq mode font-lock-removed-keywords-alist)))
924 (if cell
925 (unless (member keyword (cdr cell))
926 (nconc cell (list keyword)))
927 (push (cons mode (list keyword))
928 font-lock-removed-keywords-alist))))))
930 ;; Otherwise remove it immediately.
931 (font-lock-set-defaults)
932 (setq font-lock-keywords (copy-sequence font-lock-keywords))
933 (dolist (keyword keywords)
934 (setq font-lock-keywords
935 (delete keyword
936 ;; The keywords might be compiled.
937 (delete (font-lock-compile-keyword keyword)
938 font-lock-keywords)))))))
940 ;;; Global Font Lock mode.
942 ;; A few people have hassled in the past for a way to make it easier to turn on
943 ;; Font Lock mode, without the user needing to know for which modes s/he has to
944 ;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always
945 ;; balked at that way, as I see it as just re-moulding the same problem in
946 ;; another form. That is; some person would still have to keep track of which
947 ;; modes (which may not even be distributed with Emacs) support Font Lock mode.
948 ;; The list would always be out of date. And that person might have to be me.
950 ;; Implementation.
952 ;; In a previous discussion the following hack came to mind. It is a gross
953 ;; hack, but it generally works. We use the convention that major modes start
954 ;; by calling the function `kill-all-local-variables', which in turn runs
955 ;; functions on the hook variable `change-major-mode-hook'. We attach our
956 ;; function `font-lock-change-major-mode' to that hook. Of course, when this
957 ;; hook is run, the major mode is in the process of being changed and we do not
958 ;; know what the final major mode will be. So, `font-lock-change-major-mode'
959 ;; only (a) notes the name of the current buffer, and (b) adds our function
960 ;; `turn-on-font-lock-if-enabled' to the hook variables `find-file-hooks' and
961 ;; `post-command-hook' (for buffers that are not visiting files). By the time
962 ;; the functions on the first of these hooks to be run are run, the new major
963 ;; mode is assumed to be in place. This way we get a Font Lock function run
964 ;; when a major mode is turned on, without knowing major modes or their hooks.
966 ;; Naturally this requires that (a) major modes run `kill-all-local-variables',
967 ;; as they are supposed to do, and (b) the major mode is in place after the
968 ;; file is visited or the command that ran `kill-all-local-variables' has
969 ;; finished, whichever the sooner. Arguably, any major mode that does not
970 ;; follow the convension (a) is broken, and I can't think of any reason why (b)
971 ;; would not be met (except `gnudoit' on non-files). However, it is not clean.
973 ;; Probably the cleanest solution is to have each major mode function run some
974 ;; hook, e.g., `major-mode-hook', but maybe implementing that change is
975 ;; impractical. I am personally against making `setq' a macro or be advised,
976 ;; or have a special function such as `set-major-mode', but maybe someone can
977 ;; come up with another solution?
979 ;; User interface.
981 ;; Although Global Font Lock mode is a pseudo-mode, I think that the user
982 ;; interface should conform to the usual Emacs convention for modes, i.e., a
983 ;; command to toggle the feature (`global-font-lock-mode') with a variable for
984 ;; finer control of the mode's behaviour (`font-lock-global-modes').
986 ;; The feature should not be enabled by loading font-lock.el, since other
987 ;; mechanisms for turning on Font Lock mode, such as M-x font-lock-mode RET or
988 ;; (add-hook 'c-mode-hook 'turn-on-font-lock), would cause Font Lock mode to be
989 ;; turned on everywhere. That would not be intuitive or informative because
990 ;; loading a file tells you nothing about the feature or how to control it. It
991 ;; would also be contrary to the Principle of Least Surprise. sm.
993 (defcustom font-lock-global-modes t
994 "*Modes for which Font Lock mode is automagically turned on.
995 Global Font Lock mode is controlled by the command `global-font-lock-mode'.
996 If nil, means no modes have Font Lock mode automatically turned on.
997 If t, all modes that support Font Lock mode have it automatically turned on.
998 If a list, it should be a list of `major-mode' symbol names for which Font Lock
999 mode should be automatically turned on. The sense of the list is negated if it
1000 begins with `not'. For example:
1001 (c-mode c++-mode)
1002 means that Font Lock mode is turned on for buffers in C and C++ modes only."
1003 :type '(choice (const :tag "none" nil)
1004 (const :tag "all" t)
1005 (set :menu-tag "mode specific" :tag "modes"
1006 :value (not)
1007 (const :tag "Except" not)
1008 (repeat :inline t (symbol :tag "mode"))))
1009 :group 'font-lock)
1011 (defun turn-on-font-lock-if-enabled ()
1012 (when (and (or font-lock-defaults
1013 (assq major-mode font-lock-defaults-alist))
1014 (or (eq font-lock-global-modes t)
1015 (if (eq (car-safe font-lock-global-modes) 'not)
1016 (not (memq major-mode (cdr font-lock-global-modes)))
1017 (memq major-mode font-lock-global-modes))))
1018 (let (inhibit-quit)
1019 (turn-on-font-lock))))
1021 ;;;###autoload
1022 (easy-mmode-define-global-mode
1023 global-font-lock-mode font-lock-mode turn-on-font-lock-if-enabled
1024 :extra-args (dummy))
1026 ;;; End of Global Font Lock mode.
1028 ;;; Font Lock Support mode.
1030 ;; This is the code used to interface font-lock.el with any of its add-on
1031 ;; packages, and provide the user interface. Packages that have their own
1032 ;; local buffer fontification functions (see below) may have to call
1033 ;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
1034 ;; themselves.
1036 (defcustom font-lock-support-mode 'jit-lock-mode
1037 "*Support mode for Font Lock mode.
1038 Support modes speed up Font Lock mode by being choosy about when fontification
1039 occurs. Known support modes are Fast Lock mode (symbol `fast-lock-mode'),
1040 Lazy Lock mode (symbol `lazy-lock-mode'), and Just-in-time Lock mode (symbol
1041 `jit-lock-mode'. See those modes for more info.
1042 If nil, means support for Font Lock mode is never performed.
1043 If a symbol, use that support mode.
1044 If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
1045 where MAJOR-MODE is a symbol or t (meaning the default). For example:
1046 ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
1047 means that Fast Lock mode is used to support Font Lock mode for buffers in C or
1048 C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
1050 The value of this variable is used when Font Lock mode is turned on."
1051 :type '(choice (const :tag "none" nil)
1052 (const :tag "fast lock" fast-lock-mode)
1053 (const :tag "lazy lock" lazy-lock-mode)
1054 (const :tag "jit lock" jit-lock-mode)
1055 (repeat :menu-tag "mode specific" :tag "mode specific"
1056 :value ((t . jit-lock-mode))
1057 (cons :tag "Instance"
1058 (radio :tag "Mode"
1059 (const :tag "all" t)
1060 (symbol :tag "name"))
1061 (radio :tag "Support"
1062 (const :tag "none" nil)
1063 (const :tag "fast lock" fast-lock-mode)
1064 (const :tag "lazy lock" lazy-lock-mode)
1065 (const :tag "JIT lock" jit-lock-mode)))
1067 :version "21.1"
1068 :group 'font-lock)
1070 (defvar fast-lock-mode nil)
1071 (defvar lazy-lock-mode nil)
1072 (defvar jit-lock-mode nil)
1074 (defun font-lock-turn-on-thing-lock ()
1075 (let ((thing-mode (font-lock-value-in-major-mode font-lock-support-mode)))
1076 (cond ((eq thing-mode 'fast-lock-mode)
1077 (fast-lock-mode t))
1078 ((eq thing-mode 'lazy-lock-mode)
1079 (lazy-lock-mode t))
1080 ((eq thing-mode 'jit-lock-mode)
1081 ;; Prepare for jit-lock
1082 (remove-hook 'after-change-functions
1083 'font-lock-after-change-function t)
1084 (set (make-local-variable 'font-lock-fontify-buffer-function)
1085 'jit-lock-refontify)
1086 ;; Don't fontify eagerly (and don't abort is the buffer is large).
1087 (set (make-local-variable 'font-lock-fontified) t)
1088 ;; Use jit-lock.
1089 (jit-lock-register 'font-lock-fontify-region
1090 (not font-lock-keywords-only))))))
1092 (defun font-lock-turn-off-thing-lock ()
1093 (cond (fast-lock-mode
1094 (fast-lock-mode -1))
1095 (jit-lock-mode
1096 (jit-lock-unregister 'font-lock-fontify-region)
1097 ;; Reset local vars to the non-jit-lock case.
1098 (kill-local-variable 'font-lock-fontify-buffer-function))
1099 (lazy-lock-mode
1100 (lazy-lock-mode -1))))
1102 (defun font-lock-after-fontify-buffer ()
1103 (cond (fast-lock-mode
1104 (fast-lock-after-fontify-buffer))
1105 ;; Useless now that jit-lock intercepts font-lock-fontify-buffer. -sm
1106 ;; (jit-lock-mode
1107 ;; (jit-lock-after-fontify-buffer))
1108 (lazy-lock-mode
1109 (lazy-lock-after-fontify-buffer))))
1111 (defun font-lock-after-unfontify-buffer ()
1112 (cond (fast-lock-mode
1113 (fast-lock-after-unfontify-buffer))
1114 ;; Useless as well. It's only called when:
1115 ;; - turning off font-lock: it does not matter if we leave spurious
1116 ;; `fontified' text props around since jit-lock-mode is also off.
1117 ;; - font-lock-default-fontify-buffer fails: this is not run
1118 ;; any more anyway. -sm
1120 ;; (jit-lock-mode
1121 ;; (jit-lock-after-unfontify-buffer))
1122 (lazy-lock-mode
1123 (lazy-lock-after-unfontify-buffer))))
1125 ;;; End of Font Lock Support mode.
1127 ;;; Fontification functions.
1129 ;; Rather than the function, e.g., `font-lock-fontify-region' containing the
1130 ;; code to fontify a region, the function runs the function whose name is the
1131 ;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally,
1132 ;; the value of this variable is, e.g., `font-lock-default-fontify-region'
1133 ;; which does contain the code to fontify a region. However, the value of the
1134 ;; variable could be anything and thus, e.g., `font-lock-fontify-region' could
1135 ;; do anything. The indirection of the fontification functions gives major
1136 ;; modes the capability of modifying the way font-lock.el fontifies. Major
1137 ;; modes can modify the values of, e.g., `font-lock-fontify-region-function',
1138 ;; via the variable `font-lock-defaults'.
1140 ;; For example, Rmail mode sets the variable `font-lock-defaults' so that
1141 ;; font-lock.el uses its own function for buffer fontification. This function
1142 ;; makes fontification be on a message-by-message basis and so visiting an
1143 ;; RMAIL file is much faster. A clever implementation of the function might
1144 ;; fontify the headers differently than the message body. (It should, and
1145 ;; correspondingly for Mail mode, but I can't be bothered to do the work. Can
1146 ;; you?) This hints at a more interesting use...
1148 ;; Languages that contain text normally contained in different major modes
1149 ;; could define their own fontification functions that treat text differently
1150 ;; depending on its context. For example, Perl mode could arrange that here
1151 ;; docs are fontified differently than Perl code. Or Yacc mode could fontify
1152 ;; rules one way and C code another. Neat!
1154 ;; A further reason to use the fontification indirection feature is when the
1155 ;; default syntactual fontification, or the default fontification in general,
1156 ;; is not flexible enough for a particular major mode. For example, perhaps
1157 ;; comments are just too hairy for `font-lock-fontify-syntactically-region' to
1158 ;; cope with. You need to write your own version of that function, e.g.,
1159 ;; `hairy-fontify-syntactically-region', and make your own version of
1160 ;; `hairy-fontify-region' call that function before calling
1161 ;; `font-lock-fontify-keywords-region' for the normal regexp fontification
1162 ;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el
1163 ;; would call your region fontification function instead of its own. For
1164 ;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line
1165 ;; directives correctly and cleanly. (It is the same problem as fontifying
1166 ;; multi-line strings and comments; regexps are not appropriate for the job.)
1168 ;;;###autoload
1169 (defun font-lock-fontify-buffer ()
1170 "Fontify the current buffer the way the function `font-lock-mode' would."
1171 (interactive)
1172 (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
1173 (funcall font-lock-fontify-buffer-function)))
1175 (defun font-lock-unfontify-buffer ()
1176 (funcall font-lock-unfontify-buffer-function))
1178 (defun font-lock-fontify-region (beg end &optional loudly)
1179 (funcall font-lock-fontify-region-function beg end loudly))
1181 (defun font-lock-unfontify-region (beg end)
1182 (funcall font-lock-unfontify-region-function beg end))
1184 (defun font-lock-default-fontify-buffer ()
1185 (let ((verbose (if (numberp font-lock-verbose)
1186 (> (buffer-size) font-lock-verbose)
1187 font-lock-verbose)))
1188 (with-temp-message
1189 (when verbose
1190 (format "Fontifying %s..." (buffer-name)))
1191 ;; Make sure we have the right `font-lock-keywords' etc.
1192 (unless font-lock-mode
1193 (font-lock-set-defaults))
1194 ;; Make sure we fontify etc. in the whole buffer.
1195 (save-restriction
1196 (widen)
1197 (condition-case nil
1198 (save-excursion
1199 (save-match-data
1200 (font-lock-fontify-region (point-min) (point-max) verbose)
1201 (font-lock-after-fontify-buffer)
1202 (setq font-lock-fontified t)))
1203 ;; We don't restore the old fontification, so it's best to unfontify.
1204 (quit (font-lock-unfontify-buffer)))))))
1206 (defun font-lock-default-unfontify-buffer ()
1207 ;; Make sure we unfontify etc. in the whole buffer.
1208 (save-restriction
1209 (widen)
1210 (font-lock-unfontify-region (point-min) (point-max))
1211 (font-lock-after-unfontify-buffer)
1212 (setq font-lock-fontified nil)))
1214 (defun font-lock-default-fontify-region (beg end loudly)
1215 (save-buffer-state
1216 ((parse-sexp-lookup-properties font-lock-syntactic-keywords)
1217 (old-syntax-table (syntax-table)))
1218 (unwind-protect
1219 (save-restriction
1220 (widen)
1221 ;; Use the fontification syntax table, if any.
1222 (when font-lock-syntax-table
1223 (set-syntax-table font-lock-syntax-table))
1224 ;; check to see if we should expand the beg/end area for
1225 ;; proper multiline matches
1226 (when (and font-lock-multiline
1227 (> beg (point-min))
1228 (get-text-property (1- beg) 'font-lock-multiline))
1229 ;; We are just after or in a multiline match.
1230 (setq beg (or (previous-single-property-change
1231 beg 'font-lock-multiline)
1232 (point-min)))
1233 (goto-char beg)
1234 (setq beg (line-beginning-position)))
1235 (when font-lock-multiline
1236 (setq end (or (text-property-any end (point-max)
1237 'font-lock-multiline nil)
1238 (point-max))))
1239 (goto-char end)
1240 (setq end (line-beginning-position 2))
1241 ;; Now do the fontification.
1242 (font-lock-unfontify-region beg end)
1243 (when font-lock-syntactic-keywords
1244 (font-lock-fontify-syntactic-keywords-region beg end))
1245 (unless font-lock-keywords-only
1246 (font-lock-fontify-syntactically-region beg end loudly))
1247 (font-lock-fontify-keywords-region beg end loudly))
1248 ;; Clean up.
1249 (set-syntax-table old-syntax-table))))
1251 ;; The following must be rethought, since keywords can override fontification.
1252 ; ;; Now scan for keywords, but not if we are inside a comment now.
1253 ; (or (and (not font-lock-keywords-only)
1254 ; (let ((state (parse-partial-sexp beg end nil nil
1255 ; font-lock-cache-state)))
1256 ; (or (nth 4 state) (nth 7 state))))
1257 ; (font-lock-fontify-keywords-region beg end))
1259 (defvar font-lock-extra-managed-props nil
1260 "Additional text properties managed by font-lock.
1261 This is used by `font-lock-default-unfontify-region' to decide
1262 what properties to clear before refontifying a region.
1263 Since it is more or less directly passed to `remove-text-properties',
1264 it should have the shape of a property list (i.e. every other element
1265 is ignored).")
1267 (defun font-lock-default-unfontify-region (beg end)
1268 (save-buffer-state nil
1269 (remove-text-properties
1270 beg end (append
1271 font-lock-extra-managed-props
1272 (if font-lock-syntactic-keywords
1273 '(face nil syntax-table nil font-lock-multiline nil)
1274 '(face nil font-lock-multiline nil))))))
1276 ;; Called when any modification is made to buffer text.
1277 (defun font-lock-after-change-function (beg end old-len)
1278 (let ((inhibit-point-motion-hooks t))
1279 (save-excursion
1280 (save-match-data
1281 ;; Rescan between start of lines enclosing the region.
1282 (font-lock-fontify-region
1283 (progn (goto-char beg) (beginning-of-line) (point))
1284 (progn (goto-char end) (forward-line 1) (point)))))))
1286 (defun font-lock-fontify-block (&optional arg)
1287 "Fontify some lines the way `font-lock-fontify-buffer' would.
1288 The lines could be a function or paragraph, or a specified number of lines.
1289 If ARG is given, fontify that many lines before and after point, or 16 lines if
1290 no ARG is given and `font-lock-mark-block-function' is nil.
1291 If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
1292 delimit the region to fontify."
1293 (interactive "P")
1294 (let ((inhibit-point-motion-hooks t) font-lock-beginning-of-syntax-function
1295 deactivate-mark)
1296 ;; Make sure we have the right `font-lock-keywords' etc.
1297 (if (not font-lock-mode) (font-lock-set-defaults))
1298 (save-excursion
1299 (save-match-data
1300 (condition-case error-data
1301 (if (or arg (not font-lock-mark-block-function))
1302 (let ((lines (if arg (prefix-numeric-value arg) 16)))
1303 (font-lock-fontify-region
1304 (save-excursion (forward-line (- lines)) (point))
1305 (save-excursion (forward-line lines) (point))))
1306 (funcall font-lock-mark-block-function)
1307 (font-lock-fontify-region (point) (mark)))
1308 ((error quit) (message "Fontifying block...%s" error-data)))))))
1310 (define-key facemenu-keymap "\M-g" 'font-lock-fontify-block)
1312 ;;; End of Fontification functions.
1314 ;;; Additional text property functions.
1316 ;; The following text property functions should be builtins. This means they
1317 ;; should be written in C and put with all the other text property functions.
1318 ;; In the meantime, those that are used by font-lock.el are defined in Lisp
1319 ;; below and given a `font-lock-' prefix. Those that are not used are defined
1320 ;; in Lisp below and commented out. sm.
1322 (defun font-lock-prepend-text-property (start end prop value &optional object)
1323 "Prepend to one property of the text from START to END.
1324 Arguments PROP and VALUE specify the property and value to prepend to the value
1325 already in place. The resulting property values are always lists.
1326 Optional argument OBJECT is the string or buffer containing the text."
1327 (let ((val (if (listp value) value (list value))) next prev)
1328 (while (/= start end)
1329 (setq next (next-single-property-change start prop object end)
1330 prev (get-text-property start prop object))
1331 (put-text-property start next prop
1332 (append val (if (listp prev) prev (list prev)))
1333 object)
1334 (setq start next))))
1336 (defun font-lock-append-text-property (start end prop value &optional object)
1337 "Append to one property of the text from START to END.
1338 Arguments PROP and VALUE specify the property and value to append to the value
1339 already in place. The resulting property values are always lists.
1340 Optional argument OBJECT is the string or buffer containing the text."
1341 (let ((val (if (listp value) value (list value))) next prev)
1342 (while (/= start end)
1343 (setq next (next-single-property-change start prop object end)
1344 prev (get-text-property start prop object))
1345 (put-text-property start next prop
1346 (append (if (listp prev) prev (list prev)) val)
1347 object)
1348 (setq start next))))
1350 (defun font-lock-fillin-text-property (start end prop value &optional object)
1351 "Fill in one property of the text from START to END.
1352 Arguments PROP and VALUE specify the property and value to put where none are
1353 already in place. Therefore existing property values are not overwritten.
1354 Optional argument OBJECT is the string or buffer containing the text."
1355 (let ((start (text-property-any start end prop nil object)) next)
1356 (while start
1357 (setq next (next-single-property-change start prop object end))
1358 (put-text-property start next prop value object)
1359 (setq start (text-property-any next end prop nil object)))))
1361 ;; For completeness: this is to `remove-text-properties' as `put-text-property'
1362 ;; is to `add-text-properties', etc.
1363 ;(defun remove-text-property (start end property &optional object)
1364 ; "Remove a property from text from START to END.
1365 ;Argument PROPERTY is the property to remove.
1366 ;Optional argument OBJECT is the string or buffer containing the text.
1367 ;Return t if the property was actually removed, nil otherwise."
1368 ; (remove-text-properties start end (list property) object))
1370 ;; For consistency: maybe this should be called `remove-single-property' like
1371 ;; `next-single-property-change' (not `next-single-text-property-change'), etc.
1372 ;(defun remove-single-text-property (start end prop value &optional object)
1373 ; "Remove a specific property value from text from START to END.
1374 ;Arguments PROP and VALUE specify the property and value to remove. The
1375 ;resulting property values are not equal to VALUE nor lists containing VALUE.
1376 ;Optional argument OBJECT is the string or buffer containing the text."
1377 ; (let ((start (text-property-not-all start end prop nil object)) next prev)
1378 ; (while start
1379 ; (setq next (next-single-property-change start prop object end)
1380 ; prev (get-text-property start prop object))
1381 ; (cond ((and (symbolp prev) (eq value prev))
1382 ; (remove-text-property start next prop object))
1383 ; ((and (listp prev) (memq value prev))
1384 ; (let ((new (delq value prev)))
1385 ; (cond ((null new)
1386 ; (remove-text-property start next prop object))
1387 ; ((= (length new) 1)
1388 ; (put-text-property start next prop (car new) object))
1389 ; (t
1390 ; (put-text-property start next prop new object))))))
1391 ; (setq start (text-property-not-all next end prop nil object)))))
1393 ;;; End of Additional text property functions.
1395 ;;; Syntactic regexp fontification functions.
1397 ;; These syntactic keyword pass functions are identical to those keyword pass
1398 ;; functions below, with the following exceptions; (a) they operate on
1399 ;; `font-lock-syntactic-keywords' of course, (b) they are all `defun' as speed
1400 ;; is less of an issue, (c) eval of property value does not occur JIT as speed
1401 ;; is less of an issue, (d) OVERRIDE cannot be `prepend' or `append' as it
1402 ;; makes no sense for `syntax-table' property values, (e) they do not do it
1403 ;; LOUDLY as it is not likely to be intensive.
1405 (defun font-lock-apply-syntactic-highlight (highlight)
1406 "Apply HIGHLIGHT following a match.
1407 HIGHLIGHT should be of the form MATCH-HIGHLIGHT,
1408 see `font-lock-syntactic-keywords'."
1409 (let* ((match (nth 0 highlight))
1410 (start (match-beginning match)) (end (match-end match))
1411 (value (nth 1 highlight))
1412 (override (nth 2 highlight)))
1413 (if (not start)
1414 ;; No match but we might not signal an error.
1415 (or (nth 3 highlight)
1416 (error "No match %d in highlight %S" match highlight))
1417 (when (and (consp value) (not (numberp (car value))))
1418 (setq value (eval value)))
1419 (when (stringp value) (setq value (string-to-syntax value)))
1420 ;; Flush the syntax-cache. I believe this is not necessary for
1421 ;; font-lock's use of syntax-ppss, but I'm not 100% sure and it can
1422 ;; still be necessary for other users of syntax-ppss anyway.
1423 (syntax-ppss-after-change-function start)
1424 (cond
1425 ((not override)
1426 ;; Cannot override existing fontification.
1427 (or (text-property-not-all start end 'syntax-table nil)
1428 (put-text-property start end 'syntax-table value)))
1429 ((eq override t)
1430 ;; Override existing fontification.
1431 (put-text-property start end 'syntax-table value))
1432 ((eq override 'keep)
1433 ;; Keep existing fontification.
1434 (font-lock-fillin-text-property start end 'syntax-table value))))))
1436 (defun font-lock-fontify-syntactic-anchored-keywords (keywords limit)
1437 "Fontify according to KEYWORDS until LIMIT.
1438 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1439 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1440 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1441 ;; Evaluate PRE-MATCH-FORM.
1442 (pre-match-value (eval (nth 1 keywords))))
1443 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1444 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1445 (setq limit pre-match-value)
1446 (setq limit (line-end-position)))
1447 (save-match-data
1448 ;; Find an occurrence of `matcher' before `limit'.
1449 (while (if (stringp matcher)
1450 (re-search-forward matcher limit t)
1451 (funcall matcher limit))
1452 ;; Apply each highlight to this instance of `matcher'.
1453 (setq highlights lowdarks)
1454 (while highlights
1455 (font-lock-apply-syntactic-highlight (car highlights))
1456 (setq highlights (cdr highlights)))))
1457 ;; Evaluate POST-MATCH-FORM.
1458 (eval (nth 2 keywords))))
1460 (defun font-lock-fontify-syntactic-keywords-region (start end)
1461 "Fontify according to `font-lock-syntactic-keywords' between START and END.
1462 START should be at the beginning of a line."
1463 ;; Ensure the beginning of the file is properly syntactic-fontified.
1464 (when (and font-lock-syntactically-fontified
1465 (< font-lock-syntactically-fontified start))
1466 (setq start (max font-lock-syntactically-fontified (point-min)))
1467 (setq font-lock-syntactically-fontified end))
1468 ;; If `font-lock-syntactic-keywords' is a symbol, get the real keywords.
1469 (when (symbolp font-lock-syntactic-keywords)
1470 (setq font-lock-syntactic-keywords (font-lock-eval-keywords
1471 font-lock-syntactic-keywords)))
1472 ;; If `font-lock-syntactic-keywords' is not compiled, compile it.
1473 (unless (eq (car font-lock-syntactic-keywords) t)
1474 (setq font-lock-syntactic-keywords (font-lock-compile-keywords
1475 font-lock-syntactic-keywords)))
1476 ;; Get down to business.
1477 (let ((case-fold-search font-lock-keywords-case-fold-search)
1478 (keywords (cdr font-lock-syntactic-keywords))
1479 keyword matcher highlights)
1480 (while keywords
1481 ;; Find an occurrence of `matcher' from `start' to `end'.
1482 (setq keyword (car keywords) matcher (car keyword))
1483 (goto-char start)
1484 (while (if (stringp matcher)
1485 (re-search-forward matcher end t)
1486 (funcall matcher end))
1487 ;; Apply each highlight to this instance of `matcher', which may be
1488 ;; specific highlights or more keywords anchored to `matcher'.
1489 (setq highlights (cdr keyword))
1490 (while highlights
1491 (if (numberp (car (car highlights)))
1492 (font-lock-apply-syntactic-highlight (car highlights))
1493 (font-lock-fontify-syntactic-anchored-keywords (car highlights)
1494 end))
1495 (setq highlights (cdr highlights))))
1496 (setq keywords (cdr keywords)))))
1498 ;;; End of Syntactic regexp fontification functions.
1500 ;;; Syntactic fontification functions.
1502 (defun font-lock-fontify-syntactically-region (start end &optional loudly ppss)
1503 "Put proper face on each string and comment between START and END.
1504 START should be at the beginning of a line."
1505 (let (state face beg)
1506 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
1507 (goto-char start)
1509 ;; Find the state at the `beginning-of-line' before `start'.
1510 (setq state (or ppss (syntax-ppss start)))
1512 ;; Find each interesting place between here and `end'.
1513 (while
1514 (progn
1515 (when (or (nth 3 state) (nth 4 state))
1516 (setq face (funcall font-lock-syntactic-face-function state))
1517 (setq beg (max (nth 8 state) start))
1518 (setq state (parse-partial-sexp (point) end nil nil state
1519 'syntax-table))
1520 (when face (put-text-property beg (point) 'face face)))
1521 (setq state (parse-partial-sexp (point) end nil nil state
1522 'syntax-table))
1523 (< (point) end)))))
1525 ;;; End of Syntactic fontification functions.
1527 ;;; Keyword regexp fontification functions.
1529 (defsubst font-lock-apply-highlight (highlight)
1530 "Apply HIGHLIGHT following a match.
1531 HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
1532 (let* ((match (nth 0 highlight))
1533 (start (match-beginning match)) (end (match-end match))
1534 (override (nth 2 highlight)))
1535 (if (not start)
1536 ;; No match but we might not signal an error.
1537 (or (nth 3 highlight)
1538 (error "No match %d in highlight %S" match highlight))
1539 (let ((val (eval (nth 1 highlight))))
1540 (when (eq (car-safe val) 'face)
1541 (add-text-properties start end (cddr val))
1542 (setq val (cadr val)))
1543 (cond
1544 ((not override)
1545 ;; Cannot override existing fontification.
1546 (or (text-property-not-all start end 'face nil)
1547 (put-text-property start end 'face val)))
1548 ((eq override t)
1549 ;; Override existing fontification.
1550 (put-text-property start end 'face val))
1551 ((eq override 'prepend)
1552 ;; Prepend to existing fontification.
1553 (font-lock-prepend-text-property start end 'face val))
1554 ((eq override 'append)
1555 ;; Append to existing fontification.
1556 (font-lock-append-text-property start end 'face val))
1557 ((eq override 'keep)
1558 ;; Keep existing fontification.
1559 (font-lock-fillin-text-property start end 'face val)))))))
1561 (defsubst font-lock-fontify-anchored-keywords (keywords limit)
1562 "Fontify according to KEYWORDS until LIMIT.
1563 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1564 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1565 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1566 (lead-start (match-beginning 0))
1567 ;; Evaluate PRE-MATCH-FORM.
1568 (pre-match-value (eval (nth 1 keywords))))
1569 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1570 (if (not (and (numberp pre-match-value) (> pre-match-value (point))))
1571 (setq limit (line-end-position))
1572 (setq limit pre-match-value)
1573 (when (and font-lock-multiline (>= limit (line-beginning-position 2)))
1574 ;; this is a multiline anchored match
1575 ;; (setq font-lock-multiline t)
1576 (put-text-property (if (= limit (line-beginning-position 2))
1577 (1- limit)
1578 (min lead-start (point)))
1579 limit
1580 'font-lock-multiline t)))
1581 (save-match-data
1582 ;; Find an occurrence of `matcher' before `limit'.
1583 (while (and (< (point) limit)
1584 (if (stringp matcher)
1585 (re-search-forward matcher limit t)
1586 (funcall matcher limit)))
1587 ;; Apply each highlight to this instance of `matcher'.
1588 (setq highlights lowdarks)
1589 (while highlights
1590 (font-lock-apply-highlight (car highlights))
1591 (setq highlights (cdr highlights)))))
1592 ;; Evaluate POST-MATCH-FORM.
1593 (eval (nth 2 keywords))))
1595 (defun font-lock-fontify-keywords-region (start end &optional loudly)
1596 "Fontify according to `font-lock-keywords' between START and END.
1597 START should be at the beginning of a line."
1598 (unless (eq (car font-lock-keywords) t)
1599 (setq font-lock-keywords
1600 (font-lock-compile-keywords font-lock-keywords t)))
1601 (let ((case-fold-search font-lock-keywords-case-fold-search)
1602 (keywords (cdr font-lock-keywords))
1603 (bufname (buffer-name)) (count 0)
1604 keyword matcher highlights)
1606 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1607 (while keywords
1608 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
1609 (make-string (incf count) ?.)))
1611 ;; Find an occurrence of `matcher' from `start' to `end'.
1612 (setq keyword (car keywords) matcher (car keyword))
1613 (goto-char start)
1614 (while (and (< (point) end)
1615 (if (stringp matcher)
1616 (re-search-forward matcher end t)
1617 (funcall matcher end)))
1618 (when (and font-lock-multiline
1619 (>= (point)
1620 (save-excursion (goto-char (match-beginning 0))
1621 (forward-line 1) (point))))
1622 ;; this is a multiline regexp match
1623 ;; (setq font-lock-multiline t)
1624 (put-text-property (if (= (point)
1625 (save-excursion
1626 (goto-char (match-beginning 0))
1627 (forward-line 1) (point)))
1628 (1- (point))
1629 (match-beginning 0))
1630 (point)
1631 'font-lock-multiline t))
1632 ;; Apply each highlight to this instance of `matcher', which may be
1633 ;; specific highlights or more keywords anchored to `matcher'.
1634 (setq highlights (cdr keyword))
1635 (while highlights
1636 (if (numberp (car (car highlights)))
1637 (font-lock-apply-highlight (car highlights))
1638 (font-lock-fontify-anchored-keywords (car highlights) end))
1639 (setq highlights (cdr highlights))))
1640 (setq keywords (cdr keywords)))))
1642 ;;; End of Keyword regexp fontification functions.
1644 ;; Various functions.
1646 (defun font-lock-compile-keywords (keywords &optional regexp)
1647 "Compile KEYWORDS into the form (t KEYWORD ...).
1648 Here KEYWORD is of the form (MATCHER HIGHLIGHT ...) as shown in the
1649 `font-lock-keywords' doc string.
1650 If REGEXP is non-nil, it means these keywords are used for
1651 `font-lock-keywords' rather than for `font-lock-syntactic-keywords'."
1652 (if (eq (car-safe keywords) t)
1653 keywords
1654 (setq keywords (cons t (mapcar 'font-lock-compile-keyword keywords)))
1655 (if (and regexp
1656 (eq (or syntax-begin-function
1657 font-lock-beginning-of-syntax-function)
1658 'beginning-of-defun)
1659 (not beginning-of-defun-function))
1660 ;; Try to detect when a string or comment contains something that
1661 ;; looks like a defun and would thus confuse font-lock.
1662 (nconc keywords
1663 `((,(if defun-prompt-regexp
1664 (concat "^\\(?:" defun-prompt-regexp "\\)?\\s(")
1665 "^\\s(")
1667 (if (memq (get-text-property (1- (point)) 'face)
1668 '(font-lock-string-face font-lock-doc-face
1669 font-lock-comment-face))
1670 font-lock-warning-face)
1671 prepend)))))
1672 keywords))
1674 (defun font-lock-compile-keyword (keyword)
1675 (cond ((nlistp keyword) ; MATCHER
1676 (list keyword '(0 font-lock-keyword-face)))
1677 ((eq (car keyword) 'eval) ; (eval . FORM)
1678 (font-lock-compile-keyword (eval (cdr keyword))))
1679 ((eq (car-safe (cdr keyword)) 'quote) ; (MATCHER . 'FORM)
1680 ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote.
1681 (if (symbolp (nth 2 keyword))
1682 (list (car keyword) (list 0 (cdr keyword)))
1683 (font-lock-compile-keyword (cons (car keyword) (nth 2 keyword)))))
1684 ((numberp (cdr keyword)) ; (MATCHER . MATCH)
1685 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
1686 ((symbolp (cdr keyword)) ; (MATCHER . FACENAME)
1687 (list (car keyword) (list 0 (cdr keyword))))
1688 ((nlistp (nth 1 keyword)) ; (MATCHER . HIGHLIGHT)
1689 (list (car keyword) (cdr keyword)))
1690 (t ; (MATCHER HIGHLIGHT ...)
1691 keyword)))
1693 (defun font-lock-eval-keywords (keywords)
1694 "Evalulate KEYWORDS if a function (funcall) or variable (eval) name."
1695 (if (listp keywords)
1696 keywords
1697 (font-lock-eval-keywords (if (fboundp keywords)
1698 (funcall keywords)
1699 (eval keywords)))))
1701 (defun font-lock-value-in-major-mode (alist)
1702 "Return value in ALIST for `major-mode', or ALIST if it is not an alist.
1703 Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t."
1704 (if (consp alist)
1705 (cdr (or (assq major-mode alist) (assq t alist)))
1706 alist))
1708 (defun font-lock-choose-keywords (keywords level)
1709 "Return LEVELth element of KEYWORDS.
1710 A LEVEL of nil is equal to a LEVEL of 0, a LEVEL of t is equal to
1711 \(1- (length KEYWORDS))."
1712 (cond ((not (and (listp keywords) (symbolp (car keywords))))
1713 keywords)
1714 ((numberp level)
1715 (or (nth level keywords) (car (reverse keywords))))
1716 ((eq level t)
1717 (car (reverse keywords)))
1719 (car keywords))))
1721 (defvar font-lock-set-defaults nil) ; Whether we have set up defaults.
1723 (defun font-lock-set-defaults ()
1724 "Set fontification defaults appropriately for this mode.
1725 Sets various variables using `font-lock-defaults' (or, if nil, using
1726 `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
1727 ;; Set fontification defaults iff not previously set.
1728 (unless font-lock-set-defaults
1729 (set (make-local-variable 'font-lock-set-defaults) t)
1730 (make-local-variable 'font-lock-fontified)
1731 (make-local-variable 'font-lock-multiline)
1732 (let* ((defaults (or font-lock-defaults
1733 (cdr (assq major-mode font-lock-defaults-alist))))
1734 (keywords
1735 (font-lock-choose-keywords (nth 0 defaults)
1736 (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1737 (local (cdr (assq major-mode font-lock-keywords-alist)))
1738 (removed-keywords
1739 (cdr-safe (assq major-mode font-lock-removed-keywords-alist))))
1740 ;; Syntactic fontification?
1741 (when (nth 1 defaults)
1742 (set (make-local-variable 'font-lock-keywords-only) t))
1743 ;; Case fold during regexp fontification?
1744 (when (nth 2 defaults)
1745 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
1746 ;; Syntax table for regexp and syntactic fontification?
1747 (when (nth 3 defaults)
1748 (set (make-local-variable 'font-lock-syntax-table)
1749 (copy-syntax-table (syntax-table)))
1750 (dolist (selem (nth 3 defaults))
1751 ;; The character to modify may be a single CHAR or a STRING.
1752 (let ((syntax (cdr selem)))
1753 (dolist (char (if (numberp (car selem))
1754 (list (car selem))
1755 (mapcar 'identity (car selem))))
1756 (modify-syntax-entry char syntax font-lock-syntax-table)))))
1757 ;; Syntax function for syntactic fontification?
1758 (when (nth 4 defaults)
1759 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1760 (nth 4 defaults)))
1761 ;; Variable alist?
1762 (dolist (x (nthcdr 5 defaults))
1763 (set (make-local-variable (car x)) (cdr x)))
1764 ;; Setup `font-lock-keywords' last because its value might depend
1765 ;; on other settings (e.g. font-lock-compile-keywords uses
1766 ;; font-lock-beginning-of-syntax-function).
1767 (set (make-local-variable 'font-lock-keywords)
1768 (font-lock-compile-keywords (font-lock-eval-keywords keywords) t))
1769 ;; Local fontification?
1770 (while local
1771 (font-lock-add-keywords nil (car (car local)) (cdr (car local)))
1772 (setq local (cdr local)))
1773 (when removed-keywords
1774 (font-lock-remove-keywords nil removed-keywords)))))
1776 ;;; Colour etc. support.
1778 ;; Originally face attributes were specified via `font-lock-face-attributes'.
1779 ;; Users then changed the default face attributes by setting that variable.
1780 ;; However, we try and be back-compatible and respect its value if set except
1781 ;; for faces where M-x customize has been used to save changes for the face.
1782 (when (boundp 'font-lock-face-attributes)
1783 (let ((face-attributes font-lock-face-attributes))
1784 (while face-attributes
1785 (let* ((face-attribute (pop face-attributes))
1786 (face (car face-attribute)))
1787 ;; Rustle up a `defface' SPEC from a `font-lock-face-attributes' entry.
1788 (unless (get face 'saved-face)
1789 (let ((foreground (nth 1 face-attribute))
1790 (background (nth 2 face-attribute))
1791 (bold-p (nth 3 face-attribute))
1792 (italic-p (nth 4 face-attribute))
1793 (underline-p (nth 5 face-attribute))
1794 face-spec)
1795 (when foreground
1796 (setq face-spec (cons ':foreground (cons foreground face-spec))))
1797 (when background
1798 (setq face-spec (cons ':background (cons background face-spec))))
1799 (when bold-p
1800 (setq face-spec (append '(:weight bold) face-spec)))
1801 (when italic-p
1802 (setq face-spec (append '(:slant italic) face-spec)))
1803 (when underline-p
1804 (setq face-spec (append '(:underline t) face-spec)))
1805 (custom-declare-face face (list (list t face-spec)) nil)))))))
1807 ;; But now we do it the custom way. Note that `defface' will not overwrite any
1808 ;; faces declared above via `custom-declare-face'.
1809 (defface font-lock-comment-face
1810 '((((type tty pc) (class color) (background light)) (:foreground "red"))
1811 (((type tty pc) (class color) (background dark)) (:foreground "red1"))
1812 (((class grayscale) (background light))
1813 (:foreground "DimGray" :weight bold :slant italic))
1814 (((class grayscale) (background dark))
1815 (:foreground "LightGray" :weight bold :slant italic))
1816 (((class color) (background light)) (:foreground "Firebrick"))
1817 (((class color) (background dark)) (:foreground "chocolate1"))
1818 (t (:weight bold :slant italic)))
1819 "Font Lock mode face used to highlight comments."
1820 :group 'font-lock-highlighting-faces)
1822 (defface font-lock-string-face
1823 '((((type tty) (class color)) (:foreground "green"))
1824 (((class grayscale) (background light)) (:foreground "DimGray" :slant italic))
1825 (((class grayscale) (background dark)) (:foreground "LightGray" :slant italic))
1826 (((class color) (background light)) (:foreground "RosyBrown"))
1827 (((class color) (background dark)) (:foreground "LightSalmon"))
1828 (t (:slant italic)))
1829 "Font Lock mode face used to highlight strings."
1830 :group 'font-lock-highlighting-faces)
1832 (defface font-lock-doc-face
1833 '((t :inherit font-lock-string-face))
1834 "Font Lock mode face used to highlight documentation."
1835 :group 'font-lock-highlighting-faces)
1837 (defface font-lock-keyword-face
1838 '((((type tty) (class color)) (:foreground "cyan" :weight bold))
1839 (((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
1840 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
1841 (((class color) (background light)) (:foreground "Purple"))
1842 (((class color) (background dark)) (:foreground "Cyan"))
1843 (t (:weight bold)))
1844 "Font Lock mode face used to highlight keywords."
1845 :group 'font-lock-highlighting-faces)
1847 (defface font-lock-builtin-face
1848 '((((type tty) (class color)) (:foreground "blue" :weight light))
1849 (((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
1850 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
1851 (((class color) (background light)) (:foreground "Orchid"))
1852 (((class color) (background dark)) (:foreground "LightSteelBlue"))
1853 (t (:weight bold)))
1854 "Font Lock mode face used to highlight builtins."
1855 :group 'font-lock-highlighting-faces)
1857 (defface font-lock-function-name-face
1858 '((((type tty) (class color)) (:foreground "blue" :weight bold))
1859 (((class color) (background light)) (:foreground "Blue"))
1860 (((class color) (background dark)) (:foreground "LightSkyBlue"))
1861 (t (:inverse-video t :weight bold)))
1862 "Font Lock mode face used to highlight function names."
1863 :group 'font-lock-highlighting-faces)
1865 (defface font-lock-variable-name-face
1866 '((((type tty) (class color)) (:foreground "yellow" :weight light))
1867 (((class grayscale) (background light))
1868 (:foreground "Gray90" :weight bold :slant italic))
1869 (((class grayscale) (background dark))
1870 (:foreground "DimGray" :weight bold :slant italic))
1871 (((class color) (background light)) (:foreground "DarkGoldenrod"))
1872 (((class color) (background dark)) (:foreground "LightGoldenrod"))
1873 (t (:weight bold :slant italic)))
1874 "Font Lock mode face used to highlight variable names."
1875 :group 'font-lock-highlighting-faces)
1877 (defface font-lock-type-face
1878 '((((type tty) (class color)) (:foreground "green"))
1879 (((class grayscale) (background light)) (:foreground "Gray90" :weight bold))
1880 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
1881 (((class color) (background light)) (:foreground "ForestGreen"))
1882 (((class color) (background dark)) (:foreground "PaleGreen"))
1883 (t (:weight bold :underline t)))
1884 "Font Lock mode face used to highlight type and classes."
1885 :group 'font-lock-highlighting-faces)
1887 (defface font-lock-constant-face
1888 '((((type tty) (class color)) (:foreground "magenta"))
1889 (((class grayscale) (background light))
1890 (:foreground "LightGray" :weight bold :underline t))
1891 (((class grayscale) (background dark))
1892 (:foreground "Gray50" :weight bold :underline t))
1893 (((class color) (background light)) (:foreground "CadetBlue"))
1894 (((class color) (background dark)) (:foreground "Aquamarine"))
1895 (t (:weight bold :underline t)))
1896 "Font Lock mode face used to highlight constants and labels."
1897 :group 'font-lock-highlighting-faces)
1899 (defface font-lock-warning-face
1900 '((((type tty) (class color)) (:foreground "red"))
1901 (((class color) (background light)) (:foreground "Red" :weight bold))
1902 (((class color) (background dark)) (:foreground "Pink" :weight bold))
1903 (t (:inverse-video t :weight bold)))
1904 "Font Lock mode face used to highlight warnings."
1905 :group 'font-lock-highlighting-faces)
1907 ;;; End of Colour etc. support.
1909 ;;; Menu support.
1911 ;; This section of code is commented out because Emacs does not have real menu
1912 ;; buttons. (We can mimic them by putting "( ) " or "(X) " at the beginning of
1913 ;; the menu entry text, but with Xt it looks both ugly and embarrassingly
1914 ;; amateur.) If/When Emacs gets real menus buttons, put in menu-bar.el after
1915 ;; the entry for "Text Properties" something like:
1917 ;; (define-key menu-bar-edit-menu [font-lock]
1918 ;; (cons "Syntax Highlighting" font-lock-menu))
1920 ;; and remove a single ";" from the beginning of each line in the rest of this
1921 ;; section. Probably the mechanism for telling the menu code what are menu
1922 ;; buttons and when they are on or off needs tweaking. I have assumed that the
1923 ;; mechanism is via `menu-toggle' and `menu-selected' symbol properties. sm.
1925 ;;;;###autoload
1926 ;(progn
1927 ; ;; Make the Font Lock menu.
1928 ; (defvar font-lock-menu (make-sparse-keymap "Syntax Highlighting"))
1929 ; ;; Add the menu items in reverse order.
1930 ; (define-key font-lock-menu [fontify-less]
1931 ; '("Less In Current Buffer" . font-lock-fontify-less))
1932 ; (define-key font-lock-menu [fontify-more]
1933 ; '("More In Current Buffer" . font-lock-fontify-more))
1934 ; (define-key font-lock-menu [font-lock-sep]
1935 ; '("--"))
1936 ; (define-key font-lock-menu [font-lock-mode]
1937 ; '("In Current Buffer" . font-lock-mode))
1938 ; (define-key font-lock-menu [global-font-lock-mode]
1939 ; '("In All Buffers" . global-font-lock-mode)))
1941 ;;;;###autoload
1942 ;(progn
1943 ; ;; We put the appropriate `menu-enable' etc. symbol property values on when
1944 ; ;; font-lock.el is loaded, so we don't need to autoload the three variables.
1945 ; (put 'global-font-lock-mode 'menu-toggle t)
1946 ; (put 'font-lock-mode 'menu-toggle t)
1947 ; (put 'font-lock-fontify-more 'menu-enable '(identity))
1948 ; (put 'font-lock-fontify-less 'menu-enable '(identity)))
1950 ;;; Put the appropriate symbol property values on now. See above.
1951 ;(put 'global-font-lock-mode 'menu-selected 'global-font-lock-mode)
1952 ;(put 'font-lock-mode 'menu-selected 'font-lock-mode)
1953 ;(put 'font-lock-fontify-more 'menu-enable '(nth 2 font-lock-fontify-level))
1954 ;(put 'font-lock-fontify-less 'menu-enable '(nth 1 font-lock-fontify-level))
1956 ;(defvar font-lock-fontify-level nil) ; For less/more fontification.
1958 ;(defun font-lock-fontify-level (level)
1959 ; (let ((font-lock-maximum-decoration level))
1960 ; (when font-lock-mode
1961 ; (font-lock-mode))
1962 ; (font-lock-mode)
1963 ; (when font-lock-verbose
1964 ; (message "Fontifying %s... level %d" (buffer-name) level))))
1966 ;(defun font-lock-fontify-less ()
1967 ; "Fontify the current buffer with less decoration.
1968 ;See `font-lock-maximum-decoration'."
1969 ; (interactive)
1970 ; ;; Check in case we get called interactively.
1971 ; (if (nth 1 font-lock-fontify-level)
1972 ; (font-lock-fontify-level (1- (car font-lock-fontify-level)))
1973 ; (error "No less decoration")))
1975 ;(defun font-lock-fontify-more ()
1976 ; "Fontify the current buffer with more decoration.
1977 ;See `font-lock-maximum-decoration'."
1978 ; (interactive)
1979 ; ;; Check in case we get called interactively.
1980 ; (if (nth 2 font-lock-fontify-level)
1981 ; (font-lock-fontify-level (1+ (car font-lock-fontify-level)))
1982 ; (error "No more decoration")))
1984 ;;; This should be called by `font-lock-set-defaults'.
1985 ;(defun font-lock-set-menu ()
1986 ; ;; Activate less/more fontification entries if there are multiple levels for
1987 ; ;; the current buffer. Sets `font-lock-fontify-level' to be of the form
1988 ; ;; (CURRENT-LEVEL IS-LOWER-LEVEL-P IS-HIGHER-LEVEL-P) for menu activation.
1989 ; (let ((keywords (or (nth 0 font-lock-defaults)
1990 ; (nth 1 (assq major-mode font-lock-defaults-alist))))
1991 ; (level (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1992 ; (make-local-variable 'font-lock-fontify-level)
1993 ; (if (or (symbolp keywords) (= (length keywords) 1))
1994 ; (font-lock-unset-menu)
1995 ; (cond ((eq level t)
1996 ; (setq level (1- (length keywords))))
1997 ; ((or (null level) (zerop level))
1998 ; ;; The default level is usually, but not necessarily, level 1.
1999 ; (setq level (- (length keywords)
2000 ; (length (member (eval (car keywords))
2001 ; (mapcar 'eval (cdr keywords))))))))
2002 ; (setq font-lock-fontify-level (list level (> level 1)
2003 ; (< level (1- (length keywords))))))))
2005 ;;; This should be called by `font-lock-unset-defaults'.
2006 ;(defun font-lock-unset-menu ()
2007 ; ;; Deactivate less/more fontification entries.
2008 ; (setq font-lock-fontify-level nil))
2010 ;;; End of Menu support.
2012 ;;; Various regexp information shared by several modes.
2013 ;;; Information specific to a single mode should go in its load library.
2015 ;; Font Lock support for C, C++, Objective-C and Java modes will one day be in
2016 ;; some cc-font.el (and required by cc-mode.el). However, the below function
2017 ;; should stay in font-lock.el, since it is used by other libraries. sm.
2019 (defun font-lock-match-c-style-declaration-item-and-skip-to-next (limit)
2020 "Match, and move over, any declaration/definition item after point.
2021 Matches after point, but ignores leading whitespace and `*' characters.
2022 Does not move further than LIMIT.
2024 The expected syntax of a declaration/definition item is `word' (preceded by
2025 optional whitespace and `*' characters and proceeded by optional whitespace)
2026 optionally followed by a `('. Everything following the item (but belonging to
2027 it) is expected to by skip-able by `scan-sexps', and items are expected to be
2028 separated with a `,' and to be terminated with a `;'.
2030 Thus the regexp matches after point: word (
2031 ^^^^ ^
2032 Where the match subexpressions are: 1 2
2034 The item is delimited by (match-beginning 1) and (match-end 1).
2035 If (match-beginning 2) is non-nil, the item is followed by a `('.
2037 This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
2038 (when (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?")
2039 (when (and (match-end 2) (> (- (match-end 2) (match-beginning 2)) 1))
2040 ;; If `word' is followed by a double open-paren, it's probably
2041 ;; a macro used for "int myfun P_ ((int arg1))". Let's go back one
2042 ;; word to try and match `myfun' rather than `P_'.
2043 (let ((pos (point)))
2044 (skip-chars-backward " \t\n")
2045 (skip-syntax-backward "w")
2046 (unless (looking-at "\\(\\sw+\\)[ \t\n]*\\sw*_\\sw*[ \t\n]*\\((\\)?")
2047 ;; Looks like it was something else, so go back to where we
2048 ;; were and reset the match data by rematching.
2049 (goto-char pos)
2050 (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?"))))
2051 (save-match-data
2052 (condition-case nil
2053 (save-restriction
2054 ;; Restrict to the LIMIT.
2055 (narrow-to-region (point-min) limit)
2056 (goto-char (match-end 1))
2057 ;; Move over any item value, etc., to the next item.
2058 (while (not (looking-at "[ \t\n]*\\(\\(,\\)\\|;\\|\\'\\)"))
2059 (goto-char (or (scan-sexps (point) 1) (point-max))))
2060 (goto-char (match-end 2)))
2061 (error t)))))
2063 ;; Lisp.
2065 (defconst lisp-font-lock-keywords-1
2066 (eval-when-compile
2067 (list
2069 ;; Definitions.
2070 (list (concat "(\\(def\\("
2071 ;; Function declarations.
2072 "\\(advice\\|varalias\\|alias\\|generic\\|macro\\*?\\|method\\|"
2073 "setf\\|subst\\*?\\|un\\*?\\|"
2074 "ine-\\(condition\\|\\(?:derived\\|minor\\)-mode\\|"
2075 "method-combination\\|setf-expander\\|skeleton\\|widget\\|"
2076 "function\\|\\(compiler\\|modify\\|symbol\\)-macro\\)\\)\\|"
2077 ;; Variable declarations.
2078 "\\(const\\(ant\\)?\\|custom\\|face\\|parameter\\|var\\)\\|"
2079 ;; Structure declarations.
2080 "\\(class\\|group\\|package\\|struct\\|type\\)"
2081 "\\)\\)\\>"
2082 ;; Any whitespace and defined object.
2083 "[ \t'\(]*"
2084 "\\(setf[ \t]+\\sw+)\\|\\sw+\\)?")
2085 '(1 font-lock-keyword-face)
2086 '(9 (cond ((match-beginning 3) font-lock-function-name-face)
2087 ((match-beginning 6) font-lock-variable-name-face)
2088 (t font-lock-type-face))
2089 nil t))
2091 ;; Emacs Lisp autoload cookies.
2092 '("^;;;###\\(autoload\\)" 1 font-lock-warning-face prepend)
2094 "Subdued level highlighting for Lisp modes.")
2096 (defconst lisp-font-lock-keywords-2
2097 (append lisp-font-lock-keywords-1
2098 (eval-when-compile
2099 (list
2101 ;; Control structures. Emacs Lisp forms.
2102 (cons (concat
2103 "(" (regexp-opt
2104 '("cond" "if" "while" "let" "let*"
2105 "prog" "progn" "progv" "prog1" "prog2" "prog*"
2106 "inline" "lambda" "save-restriction" "save-excursion"
2107 "save-window-excursion" "save-selected-window"
2108 "save-match-data" "save-current-buffer" "unwind-protect"
2109 "condition-case" "track-mouse"
2110 "eval-after-load" "eval-and-compile" "eval-when-compile"
2111 "eval-when"
2112 "with-current-buffer" "with-electric-help"
2113 "with-output-to-string" "with-output-to-temp-buffer"
2114 "with-temp-buffer" "with-temp-file" "with-temp-message"
2115 "with-timeout") t)
2116 "\\>")
2119 ;; Control structures. Common Lisp forms.
2120 (cons (concat
2121 "(" (regexp-opt
2122 '("when" "unless" "case" "ecase" "typecase" "etypecase"
2123 "ccase" "ctypecase" "handler-case" "handler-bind"
2124 "restart-bind" "restart-case" "in-package"
2125 "cerror" "break" "ignore-errors"
2126 "loop" "do" "do*" "dotimes" "dolist" "the" "locally"
2127 "proclaim" "declaim" "declare" "symbol-macrolet"
2128 "lexical-let" "lexical-let*" "flet" "labels" "compiler-let"
2129 "destructuring-bind" "macrolet" "tagbody" "block"
2130 "return" "return-from") t)
2131 "\\>")
2134 ;; Exit/Feature symbols as constants.
2135 (list (concat "(\\(catch\\|throw\\|featurep\\|provide\\|require\\)\\>"
2136 "[ \t']*\\(\\sw+\\)?")
2137 '(1 font-lock-keyword-face)
2138 '(2 font-lock-constant-face nil t))
2140 ;; Erroneous structures.
2141 '("(\\(abort\\|assert\\|error\\|signal\\)\\>" 1 font-lock-warning-face)
2143 ;; Words inside \\[] tend to be for `substitute-command-keys'.
2144 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-constant-face prepend)
2146 ;; Words inside `' tend to be symbol names.
2147 '("`\\(\\sw\\sw+\\)'" 1 font-lock-constant-face prepend)
2149 ;; Constant values.
2150 '("\\<:\\sw+\\>" 0 font-lock-builtin-face)
2152 ;; ELisp and CLisp `&' keywords as types.
2153 '("\\&\\sw+\\>" . font-lock-type-face)
2155 ;; CL `with-' and `do-' constructs
2156 '("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
2158 "Gaudy level highlighting for Lisp modes.")
2160 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
2161 "Default expressions to highlight in Lisp modes.")
2163 ;;; User choices.
2165 ;; These provide a means to fontify types not defined by the language. Those
2166 ;; types might be the user's own or they might be generally accepted and used.
2167 ;; Generally accepted types are used to provide default variable values.
2169 (define-widget 'font-lock-extra-types-widget 'radio
2170 "Widget `:type' for members of the custom group `font-lock-extra-types'.
2171 Members should `:load' the package `font-lock' to use this widget."
2172 :args '((const :tag "none" nil)
2173 (repeat :tag "types" regexp)))
2175 (defcustom c-font-lock-extra-types '("FILE" "\\sw+_t" "Lisp_Object")
2176 "*List of extra types to fontify in C mode.
2177 Each list item should be a regexp not containing word-delimiters.
2178 For example, a value of (\"FILE\" \"\\\\sw+_t\") means the word FILE and words
2179 ending in _t are treated as type names.
2181 The value of this variable is used when Font Lock mode is turned on."
2182 :type 'font-lock-extra-types-widget
2183 :group 'font-lock-extra-types)
2185 (defcustom c++-font-lock-extra-types
2186 '("\\sw+_t"
2187 "\\([iof]\\|str\\)+stream\\(buf\\)?" "ios"
2188 "string" "rope"
2189 "list" "slist"
2190 "deque" "vector" "bit_vector"
2191 "set" "multiset"
2192 "map" "multimap"
2193 "hash\\(_\\(m\\(ap\\|ulti\\(map\\|set\\)\\)\\|set\\)\\)?"
2194 "stack" "queue" "priority_queue"
2195 "type_info"
2196 "iterator" "const_iterator" "reverse_iterator" "const_reverse_iterator"
2197 "reference" "const_reference")
2198 "*List of extra types to fontify in C++ mode.
2199 Each list item should be a regexp not containing word-delimiters.
2200 For example, a value of (\"string\") means the word string is treated as a type
2201 name.
2203 The value of this variable is used when Font Lock mode is turned on."
2204 :type 'font-lock-extra-types-widget
2205 :group 'font-lock-extra-types)
2207 (defcustom objc-font-lock-extra-types '("Class" "BOOL" "IMP" "SEL")
2208 "*List of extra types to fontify in Objective-C mode.
2209 Each list item should be a regexp not containing word-delimiters.
2210 For example, a value of (\"Class\" \"BOOL\" \"IMP\" \"SEL\") means the words
2211 Class, BOOL, IMP and SEL are treated as type names.
2213 The value of this variable is used when Font Lock mode is turned on."
2214 :type 'font-lock-extra-types-widget
2215 :group 'font-lock-extra-types)
2217 (defcustom java-font-lock-extra-types
2218 '("[A-Z\300-\326\330-\337]\\sw*[a-z]\\sw*" "URL")
2219 "*List of extra types to fontify in Java mode.
2220 Each list item should be a regexp not containing word-delimiters.
2221 For example, a value of (\"[A-Z\300-\326\330-\337]\\\\sw*[a-z]\\\\sw*\" \"URL\") means
2222 capitalised words (that conform to the Java id spec) and URL are treated as
2223 type names.
2225 The value of this variable is used when Font Lock mode is turned on."
2226 :type 'font-lock-extra-types-widget
2227 :group 'font-lock-extra-types)
2229 ;;; C.
2231 ;; [Murmur murmur murmur] Maestro, drum-roll please... [Murmur murmur murmur.]
2232 ;; Ahem. [Murmur murmur murmur] Lay-dees an Gennel-men. [Murmur murmur shhh!]
2233 ;; I am most proud and humbly honoured today [murmur murmur cough] to present
2234 ;; to you good people, the winner of the Second Millennium Award for The Most
2235 ;; Hairy Language Syntax. [Ahhh!] All rise please. [Shuffle shuffle
2236 ;; shuffle.] And a round of applause please. For... The C Language! [Roar.]
2238 ;; Thank you... You are too kind... It is with a feeling of great privilege
2239 ;; and indeed emotion [sob] that I accept this award. It has been a long hard
2240 ;; road. But we know our destiny. And our future. For we must not rest.
2241 ;; There are more tokens to overload, more shoehorn, more methodologies. But
2242 ;; more is a plus! [Ha ha ha.] And more means plus! [Ho ho ho.] The future
2243 ;; is C++! [Ohhh!] The Third Millennium Award... Will be ours! [Roar.]
2245 (let* ((c-keywords
2246 (eval-when-compile
2247 (regexp-opt '("break" "continue" "do" "else" "for" "if" "return"
2248 "switch" "while" "sizeof"
2249 ;; Type related, but we don't do anything special.
2250 "typedef" "extern" "auto" "register" "static"
2251 "volatile" "const"
2252 ;; Dan Nicolaescu <done@gnu.org> says this is new.
2253 "restrict"
2254 ;; Henrik Enberg <henrik@enberg.org> says this is new.
2255 "inline"))))
2256 (c-type-specs
2257 (eval-when-compile
2258 (regexp-opt '("enum" "struct" "union"))))
2259 (c-type-specs-depth
2260 (regexp-opt-depth c-type-specs))
2261 (c-type-names
2262 `(mapconcat 'identity
2263 (cons
2264 ,(eval-when-compile
2265 (regexp-opt
2266 '("char" "short" "int" "long" "signed" "unsigned"
2267 "float" "double" "void" "complex"
2268 ;; Henrik Enberg <henrik@enberg.org> says these are new.
2269 "_Complex" "_Imaginary" "_Bool")))
2270 c-font-lock-extra-types)
2271 "\\|"))
2272 (c-type-names-depth
2273 `(regexp-opt-depth ,c-type-names))
2274 (c-preprocessor-directives
2275 (eval-when-compile
2276 (regexp-opt
2277 '("define" "elif" "else" "endif" "error" "file" "if" "ifdef"
2278 "ifndef" "include" "line" "pragma" "undef"))))
2279 (c-preprocessor-directives-depth
2280 (regexp-opt-depth c-preprocessor-directives)))
2281 (defconst c-font-lock-keywords-1
2282 (list
2284 ;; These are all anchored at the beginning of line for speed.
2285 ;; Note that `c++-font-lock-keywords-1' depends on `c-font-lock-keywords-1'.
2287 ;; Fontify function name definitions (GNU style; without type on line).
2288 '("^\\(\\sw+\\)[ \t]*(" 1 font-lock-function-name-face)
2290 ;; Fontify error directives.
2291 '("^#[ \t]*error[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend)
2293 ;; Fontify filenames in #include <...> preprocessor directives as strings.
2294 '("^#[ \t]*\\(import\\|include\\)[ \t]*\\(<[^>\"\n]*>?\\)"
2295 2 font-lock-string-face)
2297 ;; Fontify function macro names.
2298 '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
2300 ;; Fontify symbol names in #elif or #if ... defined preprocessor directives.
2301 '("^#[ \t]*\\(elif\\|if\\)\\>"
2302 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
2303 (1 font-lock-builtin-face) (2 font-lock-variable-name-face nil t)))
2305 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
2306 (list
2307 (concat "^#[ \t]*\\(" c-preprocessor-directives
2308 "\\)\\>[ \t!]*\\(\\sw+\\)?")
2309 '(1 font-lock-builtin-face)
2310 (list (+ 2 c-preprocessor-directives-depth)
2311 'font-lock-variable-name-face nil t)))
2312 "Subdued level highlighting for C mode.")
2314 (defconst c-font-lock-keywords-2
2315 (append c-font-lock-keywords-1
2316 (list
2318 ;; Simple regexps for speed.
2320 ;; Fontify all type names.
2321 `(eval .
2322 (cons (concat "\\<\\(" ,c-type-names "\\)\\>") 'font-lock-type-face))
2324 ;; Fontify all builtin keywords (except case, default and goto; see below).
2325 (concat "\\<\\(" c-keywords "\\|" c-type-specs "\\)\\>")
2327 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2328 '("\\<\\(case\\|goto\\)\\>"
2329 (1 font-lock-keyword-face)
2330 ("\\(-[0-9]+\\|\\sw+\\)"
2331 ;; Return limit of search.
2332 (save-excursion (skip-chars-forward "^:\n") (point))
2334 (1 font-lock-constant-face nil t)))
2335 ;; Anders Lindgren <andersl@andersl.com> points out that it is quicker
2336 ;; to use MATCH-ANCHORED to effectively anchor the regexp on the left.
2337 ;; This must come after the one for keywords and targets.
2338 ;; Note: the lack of `:' in the first char-range prevents `bar' from being
2339 ;; highlighted in "foo: bar:". But adding `:' would break cases like
2340 ;; "test1 ? test2 ? foo : bar : baz".
2341 '(":" ("\\(?:^\\|[{};]\\)[ \t]*\\(\\sw+\\)[ \t]*:"
2342 (beginning-of-line) (end-of-line)
2343 (1 font-lock-constant-face)))
2345 "Medium level highlighting for C mode. See also `c-font-lock-extra-types'.")
2347 (defconst c-font-lock-keywords-3
2348 (append c-font-lock-keywords-2
2350 ;; More complicated regexps for more complete highlighting for types.
2351 ;; We still have to fontify type specifiers individually, as C is so hairy.
2352 (list
2354 ;; Fontify all storage types, plus their items.
2355 `(eval .
2356 (list (concat "\\<\\(" ,c-type-names "\\)\\>"
2357 "\\([ \t*&]+\\sw+\\>\\)*")
2358 ;; Fontify each declaration item.
2359 `(font-lock-match-c-style-declaration-item-and-skip-to-next
2360 ;; Start with point after all type specifiers.
2361 (prog1 (progn (skip-chars-forward "^;{}") (point))
2362 (goto-char (or (match-beginning
2363 ,(+ ,c-type-names-depth 2))
2364 (match-end 1))))
2365 ;; Finish with point after first type specifier.
2366 (goto-char (match-end 1))
2367 ;; Fontify as a variable or function name.
2368 (1 (if (match-beginning 2)
2369 font-lock-function-name-face
2370 font-lock-variable-name-face)))))
2372 ;; Fontify all storage specs and types, plus their items.
2373 `(,(concat "\\<\\(" c-type-specs "\\)\\>" "[ \t]*\\(\\sw+\\)?")
2374 (1 font-lock-keyword-face)
2375 (,(+ c-type-specs-depth 2) font-lock-type-face nil t)
2376 (font-lock-match-c-style-declaration-item-and-skip-to-next
2377 (save-excursion (skip-chars-forward "^;{}") (point))
2378 ;; Finish with point after the variable name if
2379 ;; there is one.
2380 (if (match-end 2)
2381 (goto-char (match-end 2)))
2382 ;; Fontify as a variable or function name.
2383 (1 (if (match-beginning 2)
2384 font-lock-function-name-face
2385 font-lock-variable-name-face) nil t)))
2387 ;; Fontify structures, or typedef names, plus their items.
2388 '("\\(}\\)[ \t*]*\\sw"
2389 (font-lock-match-c-style-declaration-item-and-skip-to-next
2390 (prog1 (progn (skip-chars-forward "^;{}") (point))
2391 (goto-char (match-end 1))) nil
2392 (1 font-lock-type-face)))
2394 ;; Fontify anything at beginning of line as a declaration or definition.
2395 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2396 (1 font-lock-type-face)
2397 (font-lock-match-c-style-declaration-item-and-skip-to-next
2398 (prog1 (progn (skip-chars-forward "^;{}") (point))
2399 (goto-char (or (match-beginning 2) (match-end 1)))) nil
2400 (1 (if (match-beginning 2)
2401 font-lock-function-name-face
2402 font-lock-variable-name-face))))
2404 "Gaudy level highlighting for C mode.
2405 See also `c-font-lock-extra-types'."))
2407 (defun c-font-lock-syntactic-face-function (state)
2408 (save-excursion
2409 (if (nth 3 state)
2410 ;; Check whether the string is properly terminated.
2411 (let ((nstate (parse-partial-sexp (point) (line-end-position)
2412 nil nil state 'syntax-table)))
2413 (if (and (eolp) (not (nth 5 nstate)) (nth 3 nstate))
2414 ;; We're inside a string, at EOL and there was no \.
2415 font-lock-warning-face
2416 font-lock-string-face))
2417 (goto-char (nth 8 state))
2418 ;; `doxygen' uses /*! while others use /**.
2419 (if (looking-at "/\\*[*!]\n")
2420 font-lock-doc-face font-lock-comment-face))))
2422 (defvar c-font-lock-keywords c-font-lock-keywords-1
2423 "Default expressions to highlight in C mode.
2424 See also `c-font-lock-extra-types'.")
2426 ;;; C++.
2428 (defun font-lock-match-c++-style-declaration-item-and-skip-to-next (limit)
2429 ;; Regexp matches after point: word<word>::word (
2430 ;; ^^^^ ^^^^ ^^^^ ^
2431 ;; Where the match subexpressions are: 1 3 5 6
2433 ;; Item is delimited by (match-beginning 1) and (match-end 1).
2434 ;; If (match-beginning 3) is non-nil, that part of the item incloses a `<>'.
2435 ;; If (match-beginning 5) is non-nil, that part of the item follows a `::'.
2436 ;; If (match-beginning 6) is non-nil, the item is followed by a `('.
2437 (when (looking-at (eval-when-compile
2438 (concat
2439 ;; Skip any leading whitespace.
2440 "[ \t\n*&]*"
2441 ;; This is `c++-type-spec' from below. (Hint hint!)
2442 "\\(\\sw+\\)" ; The instance?
2443 "\\([ \t\n]*<\\(\\(?:[^<>]\\|<[^>]+>\\)+\\)[ \t\n*&]*>\\)?" ; Or template?
2444 "\\([ \t\n]*::[ \t\n*~]*\\(\\sw+\\)\\)*" ; Or member?
2445 ;; Match any trailing parenthesis.
2446 "[ \t\n]*\\((\\)?")))
2447 (save-match-data
2448 (condition-case nil
2449 (save-restriction
2450 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
2451 (narrow-to-region (point-min) limit)
2452 (goto-char (match-end 1))
2453 ;; Move over any item value, etc., to the next item.
2454 (while (not (looking-at "[ \t\n]*\\(\\(,\\)\\|;\\|\\'\\)"))
2455 (goto-char (or (scan-sexps (point) 1) (point-max))))
2456 (goto-char (match-end 2)))
2457 (error t)))))
2459 (defun font-lock-match-c++-structor-declaration (limit)
2460 ;; Match C++ constructors and destructors inside class declarations.
2461 (let ((res nil)
2462 (regexp (concat "^\\s-+\\(\\(virtual\\|explicit\\)\\s-+\\)*~?\\(\\<"
2463 (mapconcat 'identity
2464 c++-font-lock-extra-types "\\|")
2465 "\\>\\)\\s-*("
2466 ;; Don't match function pointer declarations, e.g.:
2467 ;; Foo (*fptr)();
2468 "\\s-*[^*( \t]")))
2469 (while (progn (setq res (re-search-forward regexp limit t))
2470 (and res
2471 (save-excursion
2472 (beginning-of-line)
2473 (save-match-data
2474 (not (vectorp (c-at-toplevel-p))))))))
2475 res))
2477 (let* ((c++-keywords
2478 (eval-when-compile
2479 (regexp-opt
2480 '("break" "continue" "do" "else" "for" "if" "return" "switch"
2481 "while" "asm" "catch" "delete" "new" "sizeof" "this" "throw" "try"
2482 "typeid"
2483 ;; Branko Cibej <branko.cibej@hermes.si> says this is new.
2484 "export"
2485 ;; Mark Mitchell <mmitchell@usa.net> says these are new.
2486 "mutable" "explicit"
2487 ;; Alain Picard <ap@abelard.apana.org.au> suggests treating these
2488 ;; as keywords not types.
2489 "typedef" "template"
2490 "extern" "auto" "register" "const" "volatile" "static"
2491 "inline" "friend" "virtual"
2492 ;; Standard C++ operator names.
2493 "and" "and_eq" "bitand" "bitor" "compl" "not" "not_eq"
2494 "or" "or_eq" "xor" "xor_eq"))))
2495 (c++-operators
2496 (eval-when-compile
2497 (regexp-opt
2498 ;; Taken from Stroustrup, minus keywords otherwise fontified.
2499 '("+" "-" "*" "/" "%" "^" "&" "|" "~" "!" "=" "<" ">" "+=" "-="
2500 "*=" "/=" "%=" "^=" "&=" "|=" "<<" ">>" ">>=" "<<=" "==" "!="
2501 "<=" ">=" "&&" "||" "++" "--" "->*" "," "->" "[]" "()"))))
2502 (c++-type-specs
2503 (eval-when-compile
2504 (regexp-opt
2505 '("class" "public" "private" "protected" "typename"
2506 "struct" "union" "enum" "namespace" "using"
2507 ;; Eric Hopper <hopper@omnifarious.mn.org> says these are new.
2508 "static_cast" "dynamic_cast" "const_cast" "reinterpret_cast") t)))
2509 (c++-type-specs-depth
2510 (regexp-opt-depth c++-type-specs))
2511 (c++-type-names
2512 `(mapconcat 'identity
2513 (cons
2514 ,(eval-when-compile
2515 (regexp-opt
2516 '("signed" "unsigned" "short" "long"
2517 "int" "char" "float" "double" "void"
2518 "bool" "complex")))
2519 c++-font-lock-extra-types)
2520 "\\|"))
2521 (c++-type-names-depth `(regexp-opt-depth ,c++-type-names))
2523 ;; A brave attempt to match templates following a type and/or match
2524 ;; class membership. See and sync the above function
2525 ;; `font-lock-match-c++-style-declaration-item-and-skip-to-next'.
2526 (c++-type-suffix (concat "\\([ \t]*<\\(\\(?:[^<>\n]\\|<[^>\n]+>\\)+\\)[ \t*&]*>\\)?"
2527 "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)*"))
2528 (c++-type-suffix-depth (regexp-opt-depth c++-type-suffix))
2529 ;; If the string is a type, it may be followed by the cruft above.
2530 (c++-type-spec (concat "\\(\\sw+\\)\\>" c++-type-suffix))
2531 (c++-type-spec-depth (regexp-opt-depth c++-type-spec))
2533 ;; Parenthesis depth of user-defined types not forgetting their cruft.
2534 (c++-type-depth `(regexp-opt-depth
2535 (concat ,c++-type-names ,c++-type-suffix)))
2537 (defconst c++-font-lock-keywords-1
2538 (append
2540 ;; The list `c-font-lock-keywords-1' less that for function names.
2541 (cdr c-font-lock-keywords-1)
2542 (list
2544 ;; Fontify function name definitions, possibly incorporating class names.
2545 (list (concat "^" c++-type-spec "[ \t]*(")
2546 '(1 (if (or (match-beginning 2) (match-beginning 4))
2547 font-lock-type-face
2548 font-lock-function-name-face))
2549 '(3 font-lock-type-face nil t)
2550 '(5 font-lock-function-name-face nil t))
2552 "Subdued level highlighting for C++ mode.")
2554 (defconst c++-font-lock-keywords-2
2555 (append c++-font-lock-keywords-1
2556 (list
2558 ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading.
2559 `(eval .
2560 (cons (concat "\\<\\(" ,c++-type-names "\\)\\>")
2561 'font-lock-type-face))
2563 ;; Fontify operator overloading.
2564 (list (concat "\\<\\(operator\\)\\>[ \t]*\\(" c++-operators "\\)?")
2565 '(1 font-lock-keyword-face)
2566 '(2 font-lock-builtin-face nil t))
2568 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2569 '("\\<\\(case\\|goto\\)\\>"
2570 (1 font-lock-keyword-face)
2571 ("\\(-[0-9]+\\|\\sw+\\)[ \t]*\\(::\\)?"
2572 ;; Return limit of search.
2573 (save-excursion
2574 (while (progn
2575 (skip-chars-forward "^:\n")
2576 (looking-at "::"))
2577 (forward-char 2))
2578 (point))
2580 (1 (if (match-beginning 2)
2581 font-lock-type-face
2582 font-lock-constant-face) nil t)))
2583 ;; This must come after the one for keywords and targets.
2584 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:\\($\\|[^:]\\)"
2585 (beginning-of-line) (end-of-line)
2586 (1 font-lock-constant-face)))
2588 ;; Fontify other builtin keywords.
2589 (concat "\\<\\(" c++-keywords "\\|" c++-type-specs "\\)\\>")
2591 ;; Eric Hopper <hopper@omnifarious.mn.org> says `true' and `false' are new.
2592 '("\\<\\(false\\|true\\)\\>" . font-lock-constant-face)
2594 "Medium level highlighting for C++ mode.
2595 See also `c++-font-lock-extra-types'.")
2597 (defconst c++-font-lock-keywords-3
2598 (append c++-font-lock-keywords-2
2600 ;; More complicated regexps for more complete highlighting for types.
2601 (list
2603 ;; Fontify all storage classes and type specifiers, plus their items.
2604 `(eval .
2605 (list (concat "\\<\\(" ,c++-type-names "\\)\\>" ,c++-type-suffix
2606 "\\([ \t*&]+" ,c++-type-spec "\\)*")
2607 ;; The name of any template type.
2608 `(,(+ ,c++-type-names-depth 3) font-lock-type-face nil t)
2609 ;; Fontify each declaration item.
2610 `(font-lock-match-c++-style-declaration-item-and-skip-to-next
2611 ;; Start with point after all type specifiers.
2612 (prog1 (progn (skip-chars-forward "^;{}") (point))
2613 (goto-char (or (match-beginning
2614 ,(+ ,c++-type-depth 2))
2615 (match-end 1))))
2616 ;; Finish with point after first type specifier.
2617 (goto-char (match-end 1))
2618 ;; Fontify as a variable or function name.
2619 (1 (cond ((or (match-beginning 2) (match-beginning 4))
2620 font-lock-type-face)
2621 ((and (match-beginning 6) (c-at-toplevel-p))
2622 font-lock-function-name-face)
2624 font-lock-variable-name-face)))
2625 (3 font-lock-type-face nil t)
2626 (5 (if (match-beginning 6)
2627 font-lock-function-name-face
2628 font-lock-variable-name-face) nil t))))
2630 ;; Fontify all storage specs and types, plus their items.
2631 `(,(concat "\\<" c++-type-specs "\\>" c++-type-suffix
2632 "[ \t]*\\(" c++-type-spec "\\)?")
2633 ;; The name of any template type.
2634 (,(+ c++-type-specs-depth 2) 'font-lock-type-face nil t)
2635 ;; The name of any type.
2636 (,(+ c++-type-specs-depth c++-type-suffix-depth 2)
2637 font-lock-type-face nil t)
2638 ;; Fontify each declaration item.
2639 (font-lock-match-c++-style-declaration-item-and-skip-to-next
2640 ;; Start with point after all type specifiers.
2641 (save-excursion (skip-chars-forward "^;{}") (point))
2642 ;; Finish with point after first type specifier.
2644 ;; Fontify as a variable or function name.
2645 (1 (cond ((or (match-beginning 2) (match-beginning 4))
2646 font-lock-type-face)
2647 ((and (match-beginning 6) (c-at-toplevel-p))
2648 font-lock-function-name-face)
2650 font-lock-variable-name-face)))
2651 (3 font-lock-type-face nil t)
2652 (5 (if (match-beginning 6)
2653 font-lock-function-name-face
2654 font-lock-variable-name-face) nil t)))
2656 ;; Fontify structures, or typedef names, plus their items.
2657 '("\\(}\\)[ \t*]*\\sw"
2658 (font-lock-match-c++-style-declaration-item-and-skip-to-next
2659 (prog1 (progn (skip-chars-forward "^;{}") (point))
2660 (goto-char (match-end 1))) nil
2661 (1 font-lock-type-face)))
2663 ;; Fontify anything at beginning of line as a declaration or definition.
2664 `(,(concat "^\\(" c++-type-spec "[ \t*&]*\\)+")
2665 (font-lock-match-c++-style-declaration-item-and-skip-to-next
2666 (prog1 (progn (skip-chars-forward "^;{}") (point))
2667 (goto-char (match-beginning 1)))
2668 (goto-char (match-end 1))
2669 (1 (cond ((or (match-beginning 2) (match-beginning 4))
2670 font-lock-type-face)
2671 ((match-beginning 6) font-lock-function-name-face)
2672 (t font-lock-variable-name-face)))
2673 (3 font-lock-type-face nil t)
2674 (5 (if (match-beginning 6)
2675 font-lock-function-name-face
2676 font-lock-variable-name-face) nil t)))
2678 ;; Fontify constructors and destructors inside class declarations.
2679 '(font-lock-match-c++-structor-declaration
2680 (3 font-lock-function-name-face t))
2682 "Gaudy level highlighting for C++ mode.
2683 See also `c++-font-lock-extra-types'.")
2686 (defvar c++-font-lock-keywords c++-font-lock-keywords-1
2687 "Default expressions to highlight in C++ mode.
2688 See also `c++-font-lock-extra-types'.")
2690 ;;; Objective-C.
2692 ;; Regexps written with help from Stephen Peters <speters@us.oracle.com> and
2693 ;; Jacques Duthen Prestataire <duthen@cegelec-red.fr>.
2694 (let* ((objc-keywords
2695 (eval-when-compile
2696 (regexp-opt '("break" "continue" "do" "else" "for" "if" "return"
2697 "switch" "while" "sizeof" "self" "super"
2698 "typedef" "auto" "extern" "static"
2699 "volatile" "const"))))
2700 (objc-type-specs
2701 (eval-when-compile
2702 (regexp-opt
2703 '("register" "struct" "union" "enum"
2704 "oneway" "in" "out" "inout" "bycopy" "byref") t)))
2705 (objc-type-specs-depth
2706 (regexp-opt-depth objc-type-specs))
2707 (objc-type-names
2708 `(mapconcat 'identity
2709 (cons
2710 ,(eval-when-compile
2711 (regexp-opt
2712 '("signed" "unsigned" "short" "long"
2713 "int" "char" "float" "double" "void"
2714 "id")))
2715 objc-font-lock-extra-types)
2716 "\\|"))
2717 (objc-type-names-depth
2718 `(regexp-opt-depth ,objc-type-names))
2720 (defconst objc-font-lock-keywords-1
2721 (append
2723 ;; The list `c-font-lock-keywords-1' less that for function names.
2724 (cdr c-font-lock-keywords-1)
2725 (list
2727 ;; Fontify compiler directives.
2728 '("@\\(\\sw+\\)\\>"
2729 (1 font-lock-keyword-face)
2730 ("\\=[ \t:<,]*\\(\\sw+\\)" nil nil
2731 (1 font-lock-type-face)))
2733 ;; Fontify method names and arguments. Oh Lordy!
2734 ;; First, on the same line as the function declaration.
2735 '("^[+-][ \t]*\\(PRIVATE\\>\\)?[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2736 (1 font-lock-keyword-face nil t)
2737 (3 font-lock-function-name-face)
2738 ("\\=[ \t]*\\(\\sw+\\)?:[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2739 nil nil
2740 (1 font-lock-function-name-face nil t)
2741 (3 font-lock-variable-name-face)))
2742 ;; Second, on lines following the function declaration.
2743 '(":" ("^[ \t]*\\(\\sw+\\)?:[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2744 (beginning-of-line) (end-of-line)
2745 (1 font-lock-function-name-face nil t)
2746 (3 font-lock-variable-name-face)))
2748 "Subdued level highlighting for Objective-C mode.")
2750 (defconst objc-font-lock-keywords-2
2751 (append objc-font-lock-keywords-1
2752 (list
2754 ;; Simple regexps for speed.
2756 ;; Fontify all type specifiers.
2757 `(eval .
2758 (cons (concat "\\<\\(" ,objc-type-names "\\)\\>")
2759 'font-lock-type-face))
2761 ;; Fontify all builtin keywords (except case, default and goto; see below).
2762 (concat "\\<\\(" objc-keywords "\\|" objc-type-specs "\\)\\>")
2764 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2765 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2766 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
2767 ;; Fontify tags iff sole statement on line, otherwise we detect selectors.
2768 ;; This must come after the one for keywords and targets.
2769 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2770 (beginning-of-line) (end-of-line)
2771 (1 font-lock-constant-face)))
2773 ;; Fontify null object pointers.
2774 '("\\<[Nn]il\\>" . font-lock-constant-face)
2776 "Medium level highlighting for Objective-C mode.
2777 See also `objc-font-lock-extra-types'.")
2779 (defconst objc-font-lock-keywords-3
2780 (append objc-font-lock-keywords-2
2782 ;; More complicated regexps for more complete highlighting for types.
2783 ;; We still have to fontify type specifiers individually, as C is so hairy.
2784 (list
2786 ;; Fontify all storage classes and type specifiers, plus their items.
2787 `(eval .
2788 (list (concat "\\<\\(" ,objc-type-names "\\)\\>"
2789 "\\([ \t*&]+\\sw+\\>\\)*")
2790 ;; Fontify each declaration item.
2791 `(font-lock-match-c-style-declaration-item-and-skip-to-next
2792 ;; Start with point after all type specifiers.
2793 (prog1 (progn (skip-chars-forward "^;{}") (point))
2794 (goto-char (or (match-beginning
2795 ,(+ ,objc-type-names-depth 2))
2796 (match-end 1))))
2797 ;; Finish with point after first type specifier.
2798 (goto-char (match-end 1))
2799 ;; Fontify as a variable or function name.
2800 (1 (if (match-beginning 2)
2801 font-lock-function-name-face
2802 font-lock-variable-name-face)))))
2804 ;; Fontify all storage specs and types, plus their items.
2805 `(,(concat "\\<\\(" objc-type-specs "[ \t]*\\)+\\>" "[ \t]*\\(\\sw+\\)?")
2806 ;; The name of any type.
2807 (,(+ objc-type-specs-depth 2) font-lock-type-face nil t)
2808 ;; Fontify each declaration item.
2809 (font-lock-match-c++-style-declaration-item-and-skip-to-next
2810 (save-excursion (skip-chars-forward "^;{}") (point)) nil
2811 ;; Fontify as a variable or function name.
2812 (1 (if (match-beginning 2)
2813 font-lock-function-name-face
2814 font-lock-variable-name-face))))
2816 ;; Fontify structures, or typedef names, plus their items.
2817 '("\\(}\\)[ \t*]*\\sw"
2818 (font-lock-match-c-style-declaration-item-and-skip-to-next
2819 (prog1 (progn (skip-chars-forward "^;{}") (point))
2820 (goto-char (match-end 1))) nil
2821 (1 font-lock-type-face)))
2823 ;; Fontify anything at beginning of line as a declaration or definition.
2824 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2825 (1 font-lock-type-face)
2826 (font-lock-match-c-style-declaration-item-and-skip-to-next
2827 (prog1 (progn (skip-chars-forward "^;{}") (point))
2828 (goto-char (or (match-beginning 2) (match-end 1)))) nil
2829 (1 (if (match-beginning 2)
2830 font-lock-function-name-face
2831 font-lock-variable-name-face))))
2833 "Gaudy level highlighting for Objective-C mode.
2834 See also `objc-font-lock-extra-types'.")
2837 (defvar objc-font-lock-keywords objc-font-lock-keywords-1
2838 "Default expressions to highlight in Objective-C mode.
2839 See also `objc-font-lock-extra-types'.")
2841 ;;; Java.
2843 ;; Regexps written with help from Fred White <fwhite@bbn.com>,
2844 ;; Anders Lindgren <andersl@andersl.com> and Carl Manning <caroma@ai.mit.edu>.
2845 (let* ((java-keywords
2846 (eval-when-compile
2847 (regexp-opt
2848 '("catch" "do" "else" "super" "this" "finally" "for" "if"
2849 ;; Anders Lindgren <andersl@andersl.com> says these have gone.
2850 ;; "cast" "byvalue" "future" "generic" "operator" "var"
2851 ;; "inner" "outer" "rest"
2852 "implements" "extends" "throws" "instanceof" "new"
2853 "interface" "return" "switch" "throw" "try" "while"))))
2855 ;; Classes immediately followed by an object name.
2856 (java-type-names
2857 `(mapconcat 'identity
2858 (cons
2859 ,(eval-when-compile
2860 (regexp-opt '("boolean" "char" "byte" "short" "int" "long"
2861 "float" "double" "void")))
2862 java-font-lock-extra-types)
2863 "\\|"))
2864 (java-type-names-depth `(regexp-opt-depth ,java-type-names))
2866 ;; These are eventually followed by an object name.
2867 (java-type-specs
2868 (eval-when-compile
2869 (regexp-opt
2870 '("abstract" "const" "final" "synchronized" "transient" "static"
2871 ;; Anders Lindgren <andersl@andersl.com> says this has gone.
2872 ;; "threadsafe"
2873 "volatile" "public" "private" "protected" "native"
2874 ;; Carl Manning <caroma@ai.mit.edu> says this is new.
2875 "strictfp"))))
2877 (defconst java-font-lock-keywords-1
2878 (list
2880 ;; Fontify class names.
2881 '("\\<\\(class\\)\\>[ \t]*\\(\\sw+\\)?"
2882 (1 font-lock-keyword-face) (2 font-lock-type-face nil t))
2884 ;; Fontify package names in import directives.
2885 '("\\<\\(import\\|package\\)\\>[ \t]*\\(\\sw+\\)?"
2886 (1 font-lock-keyword-face)
2887 (2 font-lock-constant-face nil t)
2888 ("\\=\\.\\(\\*\\|\\sw+\\)" nil nil
2889 (1 font-lock-constant-face nil t)))
2891 "Subdued level highlighting for Java mode.")
2893 (defconst java-font-lock-keywords-2
2894 (append java-font-lock-keywords-1
2895 (list
2897 ;; Fontify class names.
2898 `(eval .
2899 (cons (concat "\\<\\(" ,java-type-names "\\)\\>[^.]")
2900 '(1 font-lock-type-face)))
2902 ;; Fontify all builtin keywords (except below).
2903 (concat "\\<\\(" java-keywords "\\|" java-type-specs "\\)\\>")
2905 ;; Fontify keywords and targets, and case default/goto tags.
2906 (list "\\<\\(break\\|case\\|continue\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2907 '(1 font-lock-keyword-face) '(2 font-lock-constant-face nil t))
2908 ;; This must come after the one for keywords and targets.
2909 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2910 (beginning-of-line) (end-of-line)
2911 (1 font-lock-constant-face)))
2913 ;; Fontify all constants.
2914 '("\\<\\(false\\|null\\|true\\)\\>" . font-lock-constant-face)
2916 ;; Javadoc tags within comments.
2917 (list
2918 (concat "@\\("
2919 "author\\|deprecated\\|exception"
2920 "\\|link\\|return\\|see\\|serial\\|serialData\\|serialField"
2921 "\\|since\\|throws"
2922 "\\|version"
2923 "\\)\\>")
2924 '(1 font-lock-constant-face prepend))
2925 '("@\\(param\\)\\>[ \t]*\\(\\sw+\\)?"
2926 (1 font-lock-constant-face prepend)
2927 (2 font-lock-variable-name-face prepend t))
2928 '("@\\(exception\\|throws\\)\\>[ \t]*\\(\\S-+\\)?"
2929 (1 font-lock-constant-face prepend)
2930 (2 font-lock-type-face prepend t))
2932 "Medium level highlighting for Java mode.
2933 See also `java-font-lock-extra-types'.")
2935 (defconst java-font-lock-keywords-3
2936 (append java-font-lock-keywords-2
2938 ;; More complicated regexps for more complete highlighting for types.
2939 ;; We still have to fontify type specifiers individually, as Java is hairy.
2940 (list
2942 ;; Fontify random types immediately followed by an item or items.
2943 `(eval .
2944 (list (concat "\\<\\(" ,java-type-names "\\)\\>"
2945 "\\([ \t]*\\[[ \t]*\\]\\)*"
2946 "\\([ \t]*\\sw\\)")
2947 ;; Fontify each declaration item.
2948 `(font-lock-match-c-style-declaration-item-and-skip-to-next
2949 ;; Start and finish with point after the type specifier.
2950 (prog1 (progn (skip-chars-forward "^;{}") (point))
2951 (goto-char (match-beginning ,(+ ,java-type-names-depth 3))))
2952 (goto-char (match-beginning ,(+ ,java-type-names-depth 3)))
2953 ;; Fontify as a variable or function name.
2954 (1 (if (match-beginning 2)
2955 font-lock-function-name-face
2956 font-lock-variable-name-face)))))
2958 ;; Fontify those that are eventually followed by an item or items.
2959 `(,(concat "\\<\\(" java-type-specs "\\)\\>"
2960 "\\([ \t]+\\sw+\\>"
2961 "\\([ \t]*\\[[ \t]*\\]\\)*"
2962 "\\)*")
2963 ;; Fontify each declaration item.
2964 (font-lock-match-c-style-declaration-item-and-skip-to-next
2965 ;; Start with point after all type specifiers.
2966 (prog1 (progn (skip-chars-forward "^;{}") (point))
2967 (goto-char (or (match-beginning 5) (match-end 1))))
2968 ;; Finish with point after first type specifier.
2969 (goto-char (match-end 1))
2970 ;; Fontify as a variable or function name.
2971 (1 (if (match-beginning 2)
2972 font-lock-function-name-face
2973 font-lock-variable-name-face))))
2975 "Gaudy level highlighting for Java mode.
2976 See also `java-font-lock-extra-types'.")
2979 (defvar java-font-lock-keywords java-font-lock-keywords-1
2980 "Default expressions to highlight in Java mode.
2981 See also `java-font-lock-extra-types'.")
2983 ;; Provide ourselves:
2985 (defun java-font-lock-syntactic-face-function (state)
2986 (save-excursion
2987 (if (nth 3 state)
2988 ;; Check whether the string is properly terminated.
2989 (let ((nstate (parse-partial-sexp (point) (line-end-position)
2990 nil nil state 'syntax-table)))
2991 (if (and (eolp) (nth 3 nstate))
2992 ;; We're inside a string, at EOL. The JLS says that:
2993 ;; It is a compile-time error for a line terminator to
2994 ;; appear after the opening " and before the closing
2995 ;; matching ".
2996 font-lock-warning-face
2997 font-lock-string-face))
2998 (goto-char (nth 8 state))
2999 (if (looking-at "/\\*\\*")
3000 font-lock-doc-face
3001 font-lock-comment-face))))
3003 (provide 'font-lock)
3005 (when (eq font-lock-support-mode 'jit-lock-mode)
3006 (require 'jit-lock))
3008 ;;; font-lock.el ends here