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