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