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