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