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