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