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