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