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