]> code.delx.au - gnu-emacs/blob - lisp/font-lock.el
* vc.el (vc-revert): Limit the length of the query string.
[gnu-emacs] / lisp / font-lock.el
1 ;;; font-lock.el --- Electric font lock mode
2
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 ;; 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 ;; Free Software Foundation, Inc.
6
7 ;; Author: jwz, then rms, then sm
8 ;; Maintainer: FSF
9 ;; Keywords: languages, faces
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; Font Lock mode is a minor mode that causes your comments to be displayed in
29 ;; one face, strings in another, reserved words in another, and so on.
30 ;;
31 ;; Comments will be displayed in `font-lock-comment-face'.
32 ;; Strings will be displayed in `font-lock-string-face'.
33 ;; Regexps are used to display selected patterns in other faces.
34 ;;
35 ;; To make the text you type be fontified, use M-x font-lock-mode RET.
36 ;; When this minor mode is on, the faces of the current line are updated with
37 ;; every insertion or deletion.
38 ;;
39 ;; To turn Font Lock mode on automatically, add this to your ~/.emacs file:
40 ;;
41 ;; (add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock)
42 ;;
43 ;; Or if you want to turn Font Lock mode on in many modes:
44 ;;
45 ;; (global-font-lock-mode t)
46 ;;
47 ;; Fontification for a particular mode may be available in a number of levels
48 ;; of decoration. The higher the level, the more decoration, but the more time
49 ;; it takes to fontify. See the variable `font-lock-maximum-decoration', and
50 ;; also the variable `font-lock-maximum-size'. Support modes for Font Lock
51 ;; mode can be used to speed up Font Lock mode. See `font-lock-support-mode'.
52 \f
53 ;;; How Font Lock mode fontifies:
54
55 ;; When Font Lock mode is turned on in a buffer, it (a) fontifies the entire
56 ;; buffer and (b) installs one of its fontification functions on one of the
57 ;; hook variables that are run by Emacs after every buffer change (i.e., an
58 ;; insertion or deletion). Fontification means the replacement of `face' text
59 ;; properties in a given region; Emacs displays text with these `face' text
60 ;; properties appropriately.
61 ;;
62 ;; Fontification normally involves syntactic (i.e., strings and comments) and
63 ;; regexp (i.e., keywords and everything else) passes. There are actually
64 ;; three passes; (a) the syntactic keyword pass, (b) the syntactic pass and (c)
65 ;; the keyword pass. Confused?
66 ;;
67 ;; The syntactic keyword pass places `syntax-table' text properties in the
68 ;; buffer according to the variable `font-lock-syntactic-keywords'. It is
69 ;; necessary because Emacs' syntax table is not powerful enough to describe all
70 ;; the different syntactic constructs required by the sort of people who decide
71 ;; that a single quote can be syntactic or not depending on the time of day.
72 ;; (What sort of person could decide to overload the meaning of a quote?)
73 ;; Obviously the syntactic keyword pass must occur before the syntactic pass.
74 ;;
75 ;; The syntactic pass places `face' text properties in the buffer according to
76 ;; syntactic context, i.e., according to the buffer's syntax table and buffer
77 ;; text's `syntax-table' text properties. It involves using a syntax parsing
78 ;; function to determine the context of different parts of a region of text. A
79 ;; syntax parsing function is necessary because generally strings and/or
80 ;; comments can span lines, and so the context of a given region is not
81 ;; necessarily apparent from the content of that region. Because the keyword
82 ;; pass only works within a given region, it is not generally appropriate for
83 ;; syntactic fontification. This is the first fontification pass that makes
84 ;; changes visible to the user; it fontifies strings and comments.
85 ;;
86 ;; The keyword pass places `face' text properties in the buffer according to
87 ;; the variable `font-lock-keywords'. It involves searching for given regexps
88 ;; (or calling given search functions) within the given region. This is the
89 ;; second fontification pass that makes changes visible to the user; it
90 ;; fontifies language reserved words, etc.
91 ;;
92 ;; Oh, and the answer is, "Yes, obviously just about everything should be done
93 ;; in a single syntactic pass, but the only syntactic parser available
94 ;; understands only strings and comments." Perhaps one day someone will write
95 ;; some syntactic parsers for common languages and a son-of-font-lock.el could
96 ;; use them rather then relying so heavily on the keyword (regexp) pass.
97
98 ;;; How Font Lock mode supports modes or is supported by modes:
99
100 ;; Modes that support Font Lock mode do so by defining one or more variables
101 ;; whose values specify the fontification. Font Lock mode knows of these
102 ;; variable names from (a) the buffer local variable `font-lock-defaults', if
103 ;; non-nil, or (b) the global variable `font-lock-defaults-alist', if the major
104 ;; mode has an entry. (Font Lock mode is set up via (a) where a mode's
105 ;; patterns are distributed with the mode's package library, and (b) where a
106 ;; mode's patterns are distributed with font-lock.el itself. An example of (a)
107 ;; is Pascal mode, an example of (b) is Lisp mode. Normally, the mechanism is
108 ;; (a); (b) is used where it is not clear which package library should contain
109 ;; the pattern definitions.) Font Lock mode chooses which variable to use for
110 ;; fontification based on `font-lock-maximum-decoration'.
111 ;;
112 ;; Font Lock mode fontification behavior can be modified in a number of ways.
113 ;; See the below comments and the comments distributed throughout this file.
114
115 ;;; Constructing patterns:
116
117 ;; See the documentation for the variable `font-lock-keywords'.
118 ;;
119 ;; Efficient regexps for use as MATCHERs for `font-lock-keywords' and
120 ;; `font-lock-syntactic-keywords' can be generated via the function
121 ;; `regexp-opt'.
122
123 ;;; Adding patterns for modes that already support Font Lock:
124
125 ;; Though Font Lock highlighting patterns already exist for many modes, it's
126 ;; likely there's something that you want fontified that currently isn't, even
127 ;; at the maximum fontification level. You can add highlighting patterns via
128 ;; `font-lock-add-keywords'. For example, say in some C
129 ;; header file you #define the token `and' to expand to `&&', etc., to make
130 ;; your C code almost readable. In your ~/.emacs there could be:
131 ;;
132 ;; (font-lock-add-keywords 'c-mode '("\\<\\(and\\|or\\|not\\)\\>"))
133 ;;
134 ;; Some modes provide specific ways to modify patterns based on the values of
135 ;; other variables. For example, additional C types can be specified via the
136 ;; variable `c-font-lock-extra-types'.
137
138 ;;; Adding patterns for modes that do not support Font Lock:
139
140 ;; Not all modes support Font Lock mode. If you (as a user of the mode) add
141 ;; patterns for a new mode, you must define in your ~/.emacs a variable or
142 ;; variables that specify regexp fontification. Then, you should indicate to
143 ;; Font Lock mode, via the mode hook setting `font-lock-defaults', exactly what
144 ;; support is required. For example, say Foo mode should have the following
145 ;; regexps fontified case-sensitively, and comments and strings should not be
146 ;; fontified automagically. In your ~/.emacs there could be:
147 ;;
148 ;; (defvar foo-font-lock-keywords
149 ;; '(("\\<\\(one\\|two\\|three\\)\\>" . font-lock-keyword-face)
150 ;; ("\\<\\(four\\|five\\|six\\)\\>" . font-lock-type-face))
151 ;; "Default expressions to highlight in Foo mode.")
152 ;;
153 ;; (add-hook 'foo-mode-hook
154 ;; (lambda ()
155 ;; (set (make-local-variable 'font-lock-defaults)
156 ;; '(foo-font-lock-keywords t))))
157
158 ;;; Adding Font Lock support for modes:
159
160 ;; Of course, it would be better that the mode already supports Font Lock mode.
161 ;; The package author would do something similar to above. The mode must
162 ;; define at the top-level a variable or variables that specify regexp
163 ;; fontification. Then, the mode command should indicate to Font Lock mode,
164 ;; via `font-lock-defaults', exactly what support is required. For example,
165 ;; say Bar mode should have the following regexps fontified case-insensitively,
166 ;; and comments and strings should be fontified automagically. In bar.el there
167 ;; could be:
168 ;;
169 ;; (defvar bar-font-lock-keywords
170 ;; '(("\\<\\(uno\\|due\\|tre\\)\\>" . font-lock-keyword-face)
171 ;; ("\\<\\(quattro\\|cinque\\|sei\\)\\>" . font-lock-type-face))
172 ;; "Default expressions to highlight in Bar mode.")
173 ;;
174 ;; and within `bar-mode' there could be:
175 ;;
176 ;; (set (make-local-variable 'font-lock-defaults)
177 ;; '(bar-font-lock-keywords nil t))
178 \f
179 ;; What is fontification for? You might say, "It's to make my code look nice."
180 ;; I think it should be for adding information in the form of cues. These cues
181 ;; should provide you with enough information to both (a) distinguish between
182 ;; different items, and (b) identify the item meanings, without having to read
183 ;; the items and think about it. Therefore, fontification allows you to think
184 ;; less about, say, the structure of code, and more about, say, why the code
185 ;; doesn't work. Or maybe it allows you to think less and drift off to sleep.
186 ;;
187 ;; So, here are my opinions/advice/guidelines:
188 ;;
189 ;; - Highlight conceptual objects, such as function and variable names, and
190 ;; different objects types differently, i.e., (a) and (b) above, highlight
191 ;; function names differently to variable names.
192 ;; - Keep the faces distinct from each other as far as possible.
193 ;; i.e., (a) above.
194 ;; - Use the same face for the same conceptual object, across all modes.
195 ;; i.e., (b) above, all modes that have items that can be thought of as, say,
196 ;; keywords, should be highlighted with the same face, etc.
197 ;; - Make the face attributes fit the concept as far as possible.
198 ;; i.e., function names might be a bold color such as blue, comments might
199 ;; be a bright color such as red, character strings might be brown, because,
200 ;; err, strings are brown (that was not the reason, please believe me).
201 ;; - Don't use a non-nil OVERRIDE unless you have a good reason.
202 ;; Only use OVERRIDE for special things that are easy to define, such as the
203 ;; way `...' quotes are treated in strings and comments in Emacs Lisp mode.
204 ;; Don't use it to, say, highlight keywords in commented out code or strings.
205 ;; - Err, that's it.
206 \f
207 ;;; Code:
208
209 (require 'syntax)
210
211 ;; Define core `font-lock' group.
212 (defgroup font-lock '((jit-lock custom-group))
213 "Font Lock mode text highlighting package."
214 :link '(custom-manual :tag "Emacs Manual" "(emacs)Font Lock")
215 :link '(custom-manual :tag "Elisp Manual" "(elisp)Font Lock Mode")
216 :group 'faces)
217
218 (defgroup font-lock-faces nil
219 "Faces for highlighting text."
220 :prefix "font-lock-"
221 :group 'font-lock)
222
223 (defgroup font-lock-extra-types nil
224 "Extra mode-specific type names for highlighting declarations."
225 :group 'font-lock)
226 \f
227 ;; User variables.
228
229 (defcustom font-lock-maximum-size 256000
230 "*Maximum size of a buffer for buffer fontification.
231 Only buffers less than this can be fontified when Font Lock mode is turned on.
232 If nil, means size is irrelevant.
233 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
234 where MAJOR-MODE is a symbol or t (meaning the default). For example:
235 ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576))
236 means that the maximum size is 250K for buffers in C or C++ modes, one megabyte
237 for buffers in Rmail mode, and size is irrelevant otherwise."
238 :type '(choice (const :tag "none" nil)
239 (integer :tag "size")
240 (repeat :menu-tag "mode specific" :tag "mode specific"
241 :value ((t . nil))
242 (cons :tag "Instance"
243 (radio :tag "Mode"
244 (const :tag "all" t)
245 (symbol :tag "name"))
246 (radio :tag "Size"
247 (const :tag "none" nil)
248 (integer :tag "size")))))
249 :group 'font-lock)
250
251 (defcustom font-lock-maximum-decoration t
252 "*Maximum decoration level for fontification.
253 If nil, use the default decoration (typically the minimum available).
254 If t, use the maximum decoration available.
255 If a number, use that level of decoration (or if not available the maximum).
256 If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
257 where MAJOR-MODE is a symbol or t (meaning the default). For example:
258 ((c-mode . t) (c++-mode . 2) (t . 1))
259 means use the maximum decoration available for buffers in C mode, level 2
260 decoration for buffers in C++ mode, and level 1 decoration otherwise."
261 :type '(choice (const :tag "default" nil)
262 (const :tag "maximum" t)
263 (integer :tag "level" 1)
264 (repeat :menu-tag "mode specific" :tag "mode specific"
265 :value ((t . t))
266 (cons :tag "Instance"
267 (radio :tag "Mode"
268 (const :tag "all" t)
269 (symbol :tag "name"))
270 (radio :tag "Decoration"
271 (const :tag "default" nil)
272 (const :tag "maximum" t)
273 (integer :tag "level" 1)))))
274 :group 'font-lock)
275
276 (defcustom font-lock-verbose 0
277 "*If non-nil, means show status messages for buffer fontification.
278 If a number, only buffers greater than this size have fontification messages."
279 :type '(choice (const :tag "never" nil)
280 (other :tag "always" t)
281 (integer :tag "size"))
282 :group 'font-lock)
283 \f
284
285 ;; Originally these variable values were face names such as `bold' etc.
286 ;; Now we create our own faces, but we keep these variables for compatibility
287 ;; and they give users another mechanism for changing face appearance.
288 ;; We now allow a FACENAME in `font-lock-keywords' to be any expression that
289 ;; returns a face. So the easiest thing is to continue using these variables,
290 ;; rather than sometimes evaling FACENAME and sometimes not. sm.
291
292 ;; Note that in new code, in the vast majority of cases there is no
293 ;; need to create variables that specify face names. Simply using
294 ;; faces directly is enough. Font-lock is not a template to be
295 ;; followed in this area.
296 (defvar font-lock-comment-face 'font-lock-comment-face
297 "Face name to use for comments.")
298
299 (defvar font-lock-comment-delimiter-face 'font-lock-comment-delimiter-face
300 "Face name to use for comment delimiters.")
301
302 (defvar font-lock-string-face 'font-lock-string-face
303 "Face name to use for strings.")
304
305 (defvar font-lock-doc-face 'font-lock-doc-face
306 "Face name to use for documentation.")
307
308 (defvar font-lock-keyword-face 'font-lock-keyword-face
309 "Face name to use for keywords.")
310
311 (defvar font-lock-builtin-face 'font-lock-builtin-face
312 "Face name to use for builtins.")
313
314 (defvar font-lock-function-name-face 'font-lock-function-name-face
315 "Face name to use for function names.")
316
317 (defvar font-lock-variable-name-face 'font-lock-variable-name-face
318 "Face name to use for variable names.")
319
320 (defvar font-lock-type-face 'font-lock-type-face
321 "Face name to use for type and class names.")
322
323 (defvar font-lock-constant-face 'font-lock-constant-face
324 "Face name to use for constant and label names.")
325
326 (defvar font-lock-warning-face 'font-lock-warning-face
327 "Face name to use for things that should stand out.")
328
329 (defvar font-lock-negation-char-face 'font-lock-negation-char-face
330 "Face name to use for easy to overlook negation.
331 This can be an \"!\" or the \"n\" in \"ifndef\".")
332
333 (defvar font-lock-preprocessor-face 'font-lock-preprocessor-face
334 "Face name to use for preprocessor directives.")
335
336 (defvar font-lock-reference-face 'font-lock-constant-face)
337 (make-obsolete-variable 'font-lock-reference-face 'font-lock-constant-face "20.3")
338
339 ;; Fontification variables:
340
341 (defvar font-lock-keywords nil
342 "A list of the keywords to highlight.
343 There are two kinds of values: user-level, and compiled.
344
345 A user-level keywords list is what a major mode or the user would
346 set up. Normally the list would come from `font-lock-defaults'.
347 through selection of a fontification level and evaluation of any
348 contained expressions. You can also alter it by calling
349 `font-lock-add-keywords' or `font-lock-remove-keywords' with MODE = nil.
350
351 Each element in a user-level keywords list should have one of these forms:
352
353 MATCHER
354 (MATCHER . SUBEXP)
355 (MATCHER . FACENAME)
356 (MATCHER . HIGHLIGHT)
357 (MATCHER HIGHLIGHT ...)
358 (eval . FORM)
359
360 where MATCHER can be either the regexp to search for, or the function name to
361 call to make the search (called with one argument, the limit of the search;
362 it should return non-nil, move point, and set `match-data' appropriately if
363 it succeeds; like `re-search-forward' would).
364 MATCHER regexps can be generated via the function `regexp-opt'.
365
366 FORM is an expression, whose value should be a keyword element, evaluated when
367 the keyword is (first) used in a buffer. This feature can be used to provide a
368 keyword that can only be generated when Font Lock mode is actually turned on.
369
370 HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
371
372 For highlighting single items, for example each instance of the word \"foo\",
373 typically only MATCH-HIGHLIGHT is required.
374 However, if an item or (typically) items are to be highlighted following the
375 instance of another item (the anchor), for example each instance of the
376 word \"bar\" following the word \"anchor\" then MATCH-ANCHORED may be required.
377
378 MATCH-HIGHLIGHT should be of the form:
379
380 (SUBEXP FACENAME [OVERRIDE [LAXMATCH]])
381
382 SUBEXP is the number of the subexpression of MATCHER to be highlighted.
383
384 FACENAME is an expression whose value is the face name to use.
385 Instead of a face, FACENAME can evaluate to a property list
386 of the form (face FACE PROP1 VAL1 PROP2 VAL2 ...)
387 in which case all the listed text-properties will be set rather than
388 just FACE. In such a case, you will most likely want to put those
389 properties in `font-lock-extra-managed-props' or to override
390 `font-lock-unfontify-region-function'.
391
392 OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification can
393 be overwritten. If `keep', only parts not already fontified are highlighted.
394 If `prepend' or `append', existing fontification is merged with the new, in
395 which the new or existing fontification, respectively, takes precedence.
396 If LAXMATCH is non-nil, that means don't signal an error if there is
397 no match for SUBEXP in MATCHER.
398
399 For example, an element of the form highlights (if not already highlighted):
400
401 \"\\\\\\=<foo\\\\\\=>\" discrete occurrences of \"foo\" in the value of the
402 variable `font-lock-keyword-face'.
403 (\"fu\\\\(bar\\\\)\" . 1) substring \"bar\" within all occurrences of \"fubar\" in
404 the value of `font-lock-keyword-face'.
405 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'.
406 (\"foo\\\\|bar\" 0 foo-bar-face t)
407 occurrences of either \"foo\" or \"bar\" in the value
408 of `foo-bar-face', even if already highlighted.
409 (fubar-match 1 fubar-face)
410 the first subexpression within all occurrences of
411 whatever the function `fubar-match' finds and matches
412 in the value of `fubar-face'.
413
414 MATCH-ANCHORED should be of the form:
415
416 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
417
418 where MATCHER is a regexp to search for or the function name to call to make
419 the search, as for MATCH-HIGHLIGHT above, but with one exception; see below.
420 PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
421 the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be
422 used to initialize before, and cleanup after, MATCHER is used. Typically,
423 PRE-MATCH-FORM is used to move to some position relative to the original
424 MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might
425 be used to move back, before resuming with MATCH-ANCHORED's parent's MATCHER.
426
427 For example, an element of the form highlights (if not already highlighted):
428
429 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
430
431 discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
432 discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
433 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is
434 initially searched for starting from the end of the match of \"anchor\", and
435 searching for subsequent instances of \"anchor\" resumes from where searching
436 for \"item\" concluded.)
437
438 The above-mentioned exception is as follows. The limit of the MATCHER search
439 defaults to the end of the line after PRE-MATCH-FORM is evaluated.
440 However, if PRE-MATCH-FORM returns a position greater than the position after
441 PRE-MATCH-FORM is evaluated, that position is used as the limit of the search.
442 It is generally a bad idea to return a position greater than the end of the
443 line, i.e., cause the MATCHER search to span lines.
444
445 These regular expressions can match text which spans lines, although
446 it is better to avoid it if possible since updating them while editing
447 text is slower, and it is not guaranteed to be always correct when using
448 support modes like jit-lock or lazy-lock.
449
450 This variable is set by major modes via the variable `font-lock-defaults'.
451 Be careful when composing regexps for this list; a poorly written pattern can
452 dramatically slow things down!
453
454 A compiled keywords list starts with t. It is produced internal
455 by `font-lock-compile-keywords' from a user-level keywords list.
456 Its second element is the user-level keywords list that was
457 compiled. The remaining elements have the same form as
458 user-level keywords, but normally their values have been
459 optimized.")
460
461 (defvar font-lock-keywords-alist nil
462 "Alist of additional `font-lock-keywords' elements for major modes.
463
464 Each element has the form (MODE KEYWORDS . HOW).
465 `font-lock-set-defaults' adds the elements in the list KEYWORDS to
466 `font-lock-keywords' when Font Lock is turned on in major mode MODE.
467
468 If HOW is nil, KEYWORDS are added at the beginning of
469 `font-lock-keywords'. If it is `set', they are used to replace the
470 value of `font-lock-keywords'. If HOW is any other non-nil value,
471 they are added at the end.
472
473 This is normally set via `font-lock-add-keywords' and
474 `font-lock-remove-keywords'.")
475 (put 'font-lock-keywords-alist 'risky-local-variable t)
476
477 (defvar font-lock-removed-keywords-alist nil
478 "Alist of `font-lock-keywords' elements to be removed for major modes.
479
480 Each element has the form (MODE . KEYWORDS). `font-lock-set-defaults'
481 removes the elements in the list KEYWORDS from `font-lock-keywords'
482 when Font Lock is turned on in major mode MODE.
483
484 This is normally set via `font-lock-add-keywords' and
485 `font-lock-remove-keywords'.")
486
487 (defvar font-lock-keywords-only nil
488 "*Non-nil means Font Lock should not fontify comments or strings.
489 This is normally set via `font-lock-defaults'.")
490
491 (defvar font-lock-keywords-case-fold-search nil
492 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
493 This is normally set via `font-lock-defaults'.")
494 (make-variable-buffer-local 'font-lock-keywords-case-fold-search)
495
496 (defvar font-lock-syntactically-fontified 0
497 "Point up to which `font-lock-syntactic-keywords' has been applied.
498 If nil, this is ignored, in which case the syntactic fontification may
499 sometimes be slightly incorrect.")
500 (make-variable-buffer-local 'font-lock-syntactically-fontified)
501
502 (defvar font-lock-syntactic-face-function
503 (lambda (state)
504 (if (nth 3 state) font-lock-string-face font-lock-comment-face))
505 "Function to determine which face to use when fontifying syntactically.
506 The function is called with a single parameter (the state as returned by
507 `parse-partial-sexp' at the beginning of the region to highlight) and
508 should return a face. This is normally set via `font-lock-defaults'.")
509
510 (defvar font-lock-syntactic-keywords nil
511 "A list of the syntactic keywords to put syntax properties on.
512 The value can be the list itself, or the name of a function or variable
513 whose value is the list.
514
515 See `font-lock-keywords' for a description of the form of this list;
516 only the differences are stated here. MATCH-HIGHLIGHT should be of the form:
517
518 (SUBEXP SYNTAX OVERRIDE LAXMATCH)
519
520 where SYNTAX can be a string (as taken by `modify-syntax-entry'), a syntax
521 table, a cons cell (as returned by `string-to-syntax') or an expression whose
522 value is such a form. OVERRIDE cannot be `prepend' or `append'.
523
524 Here are two examples of elements of `font-lock-syntactic-keywords'
525 and what they do:
526
527 (\"\\\\$\\\\(#\\\\)\" 1 \".\")
528
529 gives a hash character punctuation syntax (\".\") when following a
530 dollar-sign character. Hash characters in other contexts will still
531 follow whatever the syntax table says about the hash character.
532
533 (\"\\\\('\\\\).\\\\('\\\\)\"
534 (1 \"\\\"\")
535 (2 \"\\\"\"))
536
537 gives a pair single-quotes, which surround a single character, a SYNTAX of
538 \"\\\"\" (meaning string quote syntax). Single-quote characters in other
539 contexts will not be affected.
540
541 This is normally set via `font-lock-defaults'.")
542
543 (defvar font-lock-syntax-table nil
544 "Non-nil means use this syntax table for fontifying.
545 If this is nil, the major mode's syntax table is used.
546 This is normally set via `font-lock-defaults'.")
547
548 (defvar font-lock-beginning-of-syntax-function nil
549 "*Non-nil means use this function to move back outside all constructs.
550 When called with no args it should move point backward to a place which
551 is not in a string or comment and not within any bracket-pairs (or else,
552 a place such that any bracket-pairs outside it can be ignored for Emacs
553 syntax analysis and fontification).
554
555 If this is nil, Font Lock uses `syntax-begin-function' to move back
556 outside of any comment, string, or sexp. This variable is semi-obsolete;
557 we recommend setting `syntax-begin-function' instead.
558
559 This is normally set via `font-lock-defaults'.")
560
561 (defvar font-lock-mark-block-function nil
562 "*Non-nil means use this function to mark a block of text.
563 When called with no args it should leave point at the beginning of any
564 enclosing textual block and mark at the end.
565 This is normally set via `font-lock-defaults'.")
566
567 (defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
568 "Function to use for fontifying the buffer.
569 This is normally set via `font-lock-defaults'.")
570
571 (defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
572 "Function to use for unfontifying the buffer.
573 This is used when turning off Font Lock mode.
574 This is normally set via `font-lock-defaults'.")
575
576 (defvar font-lock-fontify-region-function 'font-lock-default-fontify-region
577 "Function to use for fontifying a region.
578 It should take two args, the beginning and end of the region, and an optional
579 third arg VERBOSE. If VERBOSE is non-nil, the function should print status
580 messages. This is normally set via `font-lock-defaults'.")
581
582 (defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
583 "Function to use for unfontifying a region.
584 It should take two args, the beginning and end of the region.
585 This is normally set via `font-lock-defaults'.")
586
587 (defvar font-lock-inhibit-thing-lock nil
588 "List of Font Lock mode related modes that should not be turned on.
589 Currently, valid mode names are `fast-lock-mode', `jit-lock-mode' and
590 `lazy-lock-mode'. This is normally set via `font-lock-defaults'.")
591
592 (defvar font-lock-multiline nil
593 "Whether font-lock should cater to multiline keywords.
594 If nil, don't try to handle multiline patterns.
595 If t, always handle multiline patterns.
596 If `undecided', don't try to handle multiline patterns until you see one.
597 Major/minor modes can set this variable if they know which option applies.")
598
599 (defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
600 \f
601 ;; Font Lock mode.
602
603 (eval-when-compile
604 ;;
605 ;; We don't do this at the top-level as we only use non-autoloaded macros.
606 (require 'cl)
607 ;;
608 ;; Borrowed from lazy-lock.el.
609 ;; We use this to preserve or protect things when modifying text properties.
610 (defmacro save-buffer-state (varlist &rest body)
611 "Bind variables according to VARLIST and eval BODY restoring buffer state."
612 (declare (indent 1) (debug let))
613 (let ((modified (make-symbol "modified")))
614 `(let* ,(append varlist
615 `((,modified (buffer-modified-p))
616 (buffer-undo-list t)
617 (inhibit-read-only t)
618 (inhibit-point-motion-hooks t)
619 (inhibit-modification-hooks t)
620 deactivate-mark
621 buffer-file-name
622 buffer-file-truename))
623 (unwind-protect
624 (progn
625 ,@body)
626 (unless ,modified
627 (restore-buffer-modified-p nil))))))
628 ;;
629 ;; Shut up the byte compiler.
630 (defvar font-lock-face-attributes)) ; Obsolete but respected if set.
631
632 (defun font-lock-mode-internal (arg)
633 ;; Turn on Font Lock mode.
634 (when arg
635 (add-hook 'after-change-functions 'font-lock-after-change-function t t)
636 (font-lock-set-defaults)
637 (font-lock-turn-on-thing-lock)
638 ;; Fontify the buffer if we have to.
639 (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size)))
640 (cond (font-lock-fontified
641 nil)
642 ((or (null max-size) (> max-size (buffer-size)))
643 (font-lock-fontify-buffer))
644 (font-lock-verbose
645 (message "Fontifying %s...buffer size greater than font-lock-maximum-size"
646 (buffer-name))))))
647 ;; Turn off Font Lock mode.
648 (unless font-lock-mode
649 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
650 (font-lock-unfontify-buffer)
651 (font-lock-turn-off-thing-lock)))
652
653 (defun font-lock-add-keywords (mode keywords &optional how)
654 "Add highlighting KEYWORDS for MODE.
655
656 MODE should be a symbol, the major mode command name, such as `c-mode'
657 or nil. If nil, highlighting keywords are added for the current buffer.
658 KEYWORDS should be a list; see the variable `font-lock-keywords'.
659 By default they are added at the beginning of the current highlighting list.
660 If optional argument HOW is `set', they are used to replace the current
661 highlighting list. If HOW is any other non-nil value, they are added at the
662 end of the current highlighting list.
663
664 For example:
665
666 (font-lock-add-keywords 'c-mode
667 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
668 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . font-lock-keyword-face)))
669
670 adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
671 comments, and to fontify `and', `or' and `not' words as keywords.
672
673 The above procedure will only add the keywords for C mode, not
674 for modes derived from C mode. To add them for derived modes too,
675 pass nil for MODE and add the call to c-mode-hook.
676
677 For example:
678
679 (add-hook 'c-mode-hook
680 (lambda ()
681 (font-lock-add-keywords nil
682 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
683 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" .
684 font-lock-keyword-face)))))
685
686 The above procedure may fail to add keywords to derived modes if
687 some involved major mode does not follow the standard conventions.
688 File a bug report if this happens, so the major mode can be corrected.
689
690 Note that some modes have specialized support for additional patterns, e.g.,
691 see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
692 `objc-font-lock-extra-types' and `java-font-lock-extra-types'."
693 (cond (mode
694 ;; If MODE is non-nil, add the KEYWORDS and HOW spec to
695 ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
696 (let ((spec (cons keywords how)) cell)
697 (if (setq cell (assq mode font-lock-keywords-alist))
698 (if (eq how 'set)
699 (setcdr cell (list spec))
700 (setcdr cell (append (cdr cell) (list spec))))
701 (push (list mode spec) font-lock-keywords-alist)))
702 ;; Make sure that `font-lock-removed-keywords-alist' does not
703 ;; contain the new keywords.
704 (font-lock-update-removed-keyword-alist mode keywords how))
705 (t
706 (when (and font-lock-mode
707 (not (or font-lock-keywords font-lock-defaults)))
708 ;; The major mode has not set any keywords, so when we enabled
709 ;; font-lock-mode it only enabled the font-core.el part, not the
710 ;; font-lock-mode-internal. Try again.
711 (font-lock-mode -1)
712 (set (make-local-variable 'font-lock-defaults) '(nil t))
713 (font-lock-mode 1))
714 ;; Otherwise set or add the keywords now.
715 ;; This is a no-op if it has been done already in this buffer
716 ;; for the correct major mode.
717 (font-lock-set-defaults)
718 (let ((was-compiled (eq (car font-lock-keywords) t)))
719 ;; Bring back the user-level (uncompiled) keywords.
720 (if was-compiled
721 (setq font-lock-keywords (cadr font-lock-keywords)))
722 ;; Now modify or replace them.
723 (if (eq how 'set)
724 (setq font-lock-keywords keywords)
725 (font-lock-remove-keywords nil keywords) ;to avoid duplicates
726 (let ((old (if (eq (car-safe font-lock-keywords) t)
727 (cdr font-lock-keywords)
728 font-lock-keywords)))
729 (setq font-lock-keywords (if how
730 (append old keywords)
731 (append keywords old)))))
732 ;; If the keywords were compiled before, compile them again.
733 (if was-compiled
734 (setq font-lock-keywords
735 (font-lock-compile-keywords font-lock-keywords)))))))
736
737 (defun font-lock-update-removed-keyword-alist (mode keywords how)
738 "Update `font-lock-removed-keywords-alist' when adding new KEYWORDS to MODE."
739 ;; When font-lock is enabled first all keywords in the list
740 ;; `font-lock-keywords-alist' are added, then all keywords in the
741 ;; list `font-lock-removed-keywords-alist' are removed. If a
742 ;; keyword was once added, removed, and then added again it must be
743 ;; removed from the removed-keywords list. Otherwise the second add
744 ;; will not take effect.
745 (let ((cell (assq mode font-lock-removed-keywords-alist)))
746 (if cell
747 (if (eq how 'set)
748 ;; A new set of keywords is defined. Forget all about
749 ;; our old keywords that should be removed.
750 (setq font-lock-removed-keywords-alist
751 (delq cell font-lock-removed-keywords-alist))
752 ;; Delete all previously removed keywords.
753 (dolist (kword keywords)
754 (setcdr cell (delete kword (cdr cell))))
755 ;; Delete the mode cell if empty.
756 (if (null (cdr cell))
757 (setq font-lock-removed-keywords-alist
758 (delq cell font-lock-removed-keywords-alist)))))))
759
760 ;; Written by Anders Lindgren <andersl@andersl.com>.
761 ;;
762 ;; Case study:
763 ;; (I) The keywords are removed from a major mode.
764 ;; In this case the keyword could be local (i.e. added earlier by
765 ;; `font-lock-add-keywords'), global, or both.
766 ;;
767 ;; (a) In the local case we remove the keywords from the variable
768 ;; `font-lock-keywords-alist'.
769 ;;
770 ;; (b) The actual global keywords are not known at this time.
771 ;; All keywords are added to `font-lock-removed-keywords-alist',
772 ;; when font-lock is enabled those keywords are removed.
773 ;;
774 ;; Note that added keywords are taken out of the list of removed
775 ;; keywords. This ensure correct operation when the same keyword
776 ;; is added and removed several times.
777 ;;
778 ;; (II) The keywords are removed from the current buffer.
779 (defun font-lock-remove-keywords (mode keywords)
780 "Remove highlighting KEYWORDS for MODE.
781
782 MODE should be a symbol, the major mode command name, such as `c-mode'
783 or nil. If nil, highlighting keywords are removed for the current buffer.
784
785 To make the removal apply to modes derived from MODE as well,
786 pass nil for MODE and add the call to MODE-hook. This may fail
787 for some derived modes if some involved major mode does not
788 follow the standard conventions. File a bug report if this
789 happens, so the major mode can be corrected."
790 (cond (mode
791 ;; Remove one keyword at the time.
792 (dolist (keyword keywords)
793 (let ((top-cell (assq mode font-lock-keywords-alist)))
794 ;; If MODE is non-nil, remove the KEYWORD from
795 ;; `font-lock-keywords-alist'.
796 (when top-cell
797 (dolist (keyword-list-how-pair (cdr top-cell))
798 ;; `keywords-list-how-pair' is a cons with a list of
799 ;; keywords in the car top-cell and the original how
800 ;; argument in the cdr top-cell.
801 (setcar keyword-list-how-pair
802 (delete keyword (car keyword-list-how-pair))))
803 ;; Remove keyword list/how pair when the keyword list
804 ;; is empty and how doesn't specify `set'. (If it
805 ;; should be deleted then previously deleted keywords
806 ;; would appear again.)
807 (let ((cell top-cell))
808 (while (cdr cell)
809 (if (and (null (car (car (cdr cell))))
810 (not (eq (cdr (car (cdr cell))) 'set)))
811 (setcdr cell (cdr (cdr cell)))
812 (setq cell (cdr cell)))))
813 ;; Final cleanup, remove major mode cell if last keyword
814 ;; was deleted.
815 (if (null (cdr top-cell))
816 (setq font-lock-keywords-alist
817 (delq top-cell font-lock-keywords-alist))))
818 ;; Remember the keyword in case it is not local.
819 (let ((cell (assq mode font-lock-removed-keywords-alist)))
820 (if cell
821 (unless (member keyword (cdr cell))
822 (nconc cell (list keyword)))
823 (push (cons mode (list keyword))
824 font-lock-removed-keywords-alist))))))
825 (t
826 ;; Otherwise remove it immediately.
827 (font-lock-set-defaults)
828 (let ((was-compiled (eq (car font-lock-keywords) t)))
829 ;; Bring back the user-level (uncompiled) keywords.
830 (if was-compiled
831 (setq font-lock-keywords (cadr font-lock-keywords)))
832
833 ;; Edit them.
834 (setq font-lock-keywords (copy-sequence font-lock-keywords))
835 (dolist (keyword keywords)
836 (setq font-lock-keywords
837 (delete keyword font-lock-keywords)))
838
839 ;; If the keywords were compiled before, compile them again.
840 (if was-compiled
841 (setq font-lock-keywords
842 (font-lock-compile-keywords font-lock-keywords)))))))
843 \f
844 ;;; Font Lock Support mode.
845
846 ;; This is the code used to interface font-lock.el with any of its add-on
847 ;; packages, and provide the user interface. Packages that have their own
848 ;; local buffer fontification functions (see below) may have to call
849 ;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
850 ;; themselves.
851
852 (defcustom font-lock-support-mode 'jit-lock-mode
853 "*Support mode for Font Lock mode.
854 Support modes speed up Font Lock mode by being choosy about when fontification
855 occurs. The default support mode, Just-in-time Lock mode (symbol
856 `jit-lock-mode'), is recommended.
857
858 Other, older support modes are Fast Lock mode (symbol `fast-lock-mode') and
859 Lazy Lock mode (symbol `lazy-lock-mode'). See those modes for more info.
860 However, they are no longer recommended, as Just-in-time Lock mode is better.
861
862 If nil, means support for Font Lock mode is never performed.
863 If a symbol, use that support mode.
864 If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
865 where MAJOR-MODE is a symbol or t (meaning the default). For example:
866 ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
867 means that Fast Lock mode is used to support Font Lock mode for buffers in C or
868 C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
869
870 The value of this variable is used when Font Lock mode is turned on."
871 :type '(choice (const :tag "none" nil)
872 (const :tag "fast lock" fast-lock-mode)
873 (const :tag "lazy lock" lazy-lock-mode)
874 (const :tag "jit lock" jit-lock-mode)
875 (repeat :menu-tag "mode specific" :tag "mode specific"
876 :value ((t . jit-lock-mode))
877 (cons :tag "Instance"
878 (radio :tag "Mode"
879 (const :tag "all" t)
880 (symbol :tag "name"))
881 (radio :tag "Support"
882 (const :tag "none" nil)
883 (const :tag "fast lock" fast-lock-mode)
884 (const :tag "lazy lock" lazy-lock-mode)
885 (const :tag "JIT lock" jit-lock-mode)))
886 ))
887 :version "21.1"
888 :group 'font-lock)
889
890 (defvar fast-lock-mode)
891 (defvar lazy-lock-mode)
892 (defvar jit-lock-mode)
893
894 (declare-function fast-lock-after-fontify-buffer "fast-lock")
895 (declare-function fast-lock-after-unfontify-buffer "fast-lock")
896 (declare-function fast-lock-mode "fast-lock")
897 (declare-function lazy-lock-after-fontify-buffer "lazy-lock")
898 (declare-function lazy-lock-after-unfontify-buffer "lazy-lock")
899 (declare-function lazy-lock-mode "lazy-lock")
900
901 (defun font-lock-turn-on-thing-lock ()
902 (let ((thing-mode (font-lock-value-in-major-mode font-lock-support-mode)))
903 (cond ((eq thing-mode 'fast-lock-mode)
904 (fast-lock-mode t))
905 ((eq thing-mode 'lazy-lock-mode)
906 (lazy-lock-mode t))
907 ((eq thing-mode 'jit-lock-mode)
908 ;; Prepare for jit-lock
909 (remove-hook 'after-change-functions
910 'font-lock-after-change-function t)
911 (set (make-local-variable 'font-lock-fontify-buffer-function)
912 'jit-lock-refontify)
913 ;; Don't fontify eagerly (and don't abort if the buffer is large).
914 (set (make-local-variable 'font-lock-fontified) t)
915 ;; Use jit-lock.
916 (jit-lock-register 'font-lock-fontify-region
917 (not font-lock-keywords-only))
918 ;; Tell jit-lock how we extend the region to refontify.
919 (add-hook 'jit-lock-after-change-extend-region-functions
920 'font-lock-extend-jit-lock-region-after-change
921 nil t)))))
922
923 (defun font-lock-turn-off-thing-lock ()
924 (cond ((bound-and-true-p fast-lock-mode)
925 (fast-lock-mode -1))
926 ((bound-and-true-p jit-lock-mode)
927 (jit-lock-unregister 'font-lock-fontify-region)
928 ;; Reset local vars to the non-jit-lock case.
929 (kill-local-variable 'font-lock-fontify-buffer-function))
930 ((bound-and-true-p lazy-lock-mode)
931 (lazy-lock-mode -1))))
932
933 (defun font-lock-after-fontify-buffer ()
934 (cond ((bound-and-true-p fast-lock-mode)
935 (fast-lock-after-fontify-buffer))
936 ;; Useless now that jit-lock intercepts font-lock-fontify-buffer. -sm
937 ;; (jit-lock-mode
938 ;; (jit-lock-after-fontify-buffer))
939 ((bound-and-true-p lazy-lock-mode)
940 (lazy-lock-after-fontify-buffer))))
941
942 (defun font-lock-after-unfontify-buffer ()
943 (cond ((bound-and-true-p fast-lock-mode)
944 (fast-lock-after-unfontify-buffer))
945 ;; Useless as well. It's only called when:
946 ;; - turning off font-lock: it does not matter if we leave spurious
947 ;; `fontified' text props around since jit-lock-mode is also off.
948 ;; - font-lock-default-fontify-buffer fails: this is not run
949 ;; any more anyway. -sm
950 ;;
951 ;; (jit-lock-mode
952 ;; (jit-lock-after-unfontify-buffer))
953 ((bound-and-true-p lazy-lock-mode)
954 (lazy-lock-after-unfontify-buffer))))
955
956 ;;; End of Font Lock Support mode.
957 \f
958 ;;; Fontification functions.
959
960 ;; Rather than the function, e.g., `font-lock-fontify-region' containing the
961 ;; code to fontify a region, the function runs the function whose name is the
962 ;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally,
963 ;; the value of this variable is, e.g., `font-lock-default-fontify-region'
964 ;; which does contain the code to fontify a region. However, the value of the
965 ;; variable could be anything and thus, e.g., `font-lock-fontify-region' could
966 ;; do anything. The indirection of the fontification functions gives major
967 ;; modes the capability of modifying the way font-lock.el fontifies. Major
968 ;; modes can modify the values of, e.g., `font-lock-fontify-region-function',
969 ;; via the variable `font-lock-defaults'.
970 ;;
971 ;; For example, Rmail mode sets the variable `font-lock-defaults' so that
972 ;; font-lock.el uses its own function for buffer fontification. This function
973 ;; makes fontification be on a message-by-message basis and so visiting an
974 ;; RMAIL file is much faster. A clever implementation of the function might
975 ;; fontify the headers differently than the message body. (It should, and
976 ;; correspondingly for Mail mode, but I can't be bothered to do the work. Can
977 ;; you?) This hints at a more interesting use...
978 ;;
979 ;; Languages that contain text normally contained in different major modes
980 ;; could define their own fontification functions that treat text differently
981 ;; depending on its context. For example, Perl mode could arrange that here
982 ;; docs are fontified differently than Perl code. Or Yacc mode could fontify
983 ;; rules one way and C code another. Neat!
984 ;;
985 ;; A further reason to use the fontification indirection feature is when the
986 ;; default syntactual fontification, or the default fontification in general,
987 ;; is not flexible enough for a particular major mode. For example, perhaps
988 ;; comments are just too hairy for `font-lock-fontify-syntactically-region' to
989 ;; cope with. You need to write your own version of that function, e.g.,
990 ;; `hairy-fontify-syntactically-region', and make your own version of
991 ;; `hairy-fontify-region' call that function before calling
992 ;; `font-lock-fontify-keywords-region' for the normal regexp fontification
993 ;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el
994 ;; would call your region fontification function instead of its own. For
995 ;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line
996 ;; directives correctly and cleanly. (It is the same problem as fontifying
997 ;; multi-line strings and comments; regexps are not appropriate for the job.)
998
999 (defvar font-lock-extend-after-change-region-function nil
1000 "A function that determines the region to refontify after a change.
1001
1002 This variable is either nil, or is a function that determines the
1003 region to refontify after a change.
1004 It is usually set by the major mode via `font-lock-defaults'.
1005 Font-lock calls this function after each buffer change.
1006
1007 The function is given three parameters, the standard BEG, END, and OLD-LEN
1008 from `after-change-functions'. It should return either a cons of the beginning
1009 and end buffer positions \(in that order) of the region to refontify, or nil
1010 \(which directs the caller to fontify a default region).
1011 This function should preserve the match-data.
1012 The region it returns may start or end in the middle of a line.")
1013 (make-variable-buffer-local 'font-lock-extend-after-change-region-function)
1014
1015 (defun font-lock-fontify-buffer ()
1016 "Fontify the current buffer the way the function `font-lock-mode' would."
1017 (interactive)
1018 (font-lock-set-defaults)
1019 (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
1020 (funcall font-lock-fontify-buffer-function)))
1021
1022 (defun font-lock-unfontify-buffer ()
1023 (funcall font-lock-unfontify-buffer-function))
1024
1025 (defun font-lock-fontify-region (beg end &optional loudly)
1026 (font-lock-set-defaults)
1027 (funcall font-lock-fontify-region-function beg end loudly))
1028
1029 (defun font-lock-unfontify-region (beg end)
1030 (save-buffer-state nil
1031 (funcall font-lock-unfontify-region-function beg end)))
1032
1033 (defun font-lock-default-fontify-buffer ()
1034 (let ((verbose (if (numberp font-lock-verbose)
1035 (> (buffer-size) font-lock-verbose)
1036 font-lock-verbose)))
1037 (with-temp-message
1038 (when verbose
1039 (format "Fontifying %s..." (buffer-name)))
1040 ;; Make sure we fontify etc. in the whole buffer.
1041 (save-restriction
1042 (widen)
1043 (condition-case nil
1044 (save-excursion
1045 (save-match-data
1046 (font-lock-fontify-region (point-min) (point-max) verbose)
1047 (font-lock-after-fontify-buffer)
1048 (setq font-lock-fontified t)))
1049 ;; We don't restore the old fontification, so it's best to unfontify.
1050 (quit (font-lock-unfontify-buffer)))))))
1051
1052 (defun font-lock-default-unfontify-buffer ()
1053 ;; Make sure we unfontify etc. in the whole buffer.
1054 (save-restriction
1055 (widen)
1056 (font-lock-unfontify-region (point-min) (point-max))
1057 (font-lock-after-unfontify-buffer)
1058 (setq font-lock-fontified nil)))
1059
1060 (defvar font-lock-dont-widen nil
1061 "If non-nil, font-lock will work on the non-widened buffer.
1062 Useful for things like RMAIL and Info where the whole buffer is not
1063 a very meaningful entity to highlight.")
1064
1065
1066 (defvar font-lock-beg) (defvar font-lock-end)
1067 (defvar font-lock-extend-region-functions
1068 '(font-lock-extend-region-wholelines
1069 ;; This use of font-lock-multiline property is unreliable but is just
1070 ;; a handy heuristic: in case you don't have a function that does
1071 ;; /identification/ of multiline elements, you may still occasionally
1072 ;; discover them by accident (or you may /identify/ them but not in all
1073 ;; cases), in which case the font-lock-multiline property can help make
1074 ;; sure you will properly *re*identify them during refontification.
1075 font-lock-extend-region-multiline)
1076 "Special hook run just before proceeding to fontify a region.
1077 This is used to allow major modes to help font-lock find safe buffer positions
1078 as beginning and end of the fontified region. Its most common use is to solve
1079 the problem of /identification/ of multiline elements by providing a function
1080 that tries to find such elements and move the boundaries such that they do
1081 not fall in the middle of one.
1082 Each function is called with no argument; it is expected to adjust the
1083 dynamically bound variables `font-lock-beg' and `font-lock-end'; and return
1084 non-nil if it did make such an adjustment.
1085 These functions are run in turn repeatedly until they all return nil.
1086 Put first the functions more likely to cause a change and cheaper to compute.")
1087 ;; Mark it as a special hook which doesn't use any global setting
1088 ;; (i.e. doesn't obey the element t in the buffer-local value).
1089 (make-variable-buffer-local 'font-lock-extend-region-functions)
1090
1091 (defun font-lock-extend-region-multiline ()
1092 "Move fontification boundaries away from any `font-lock-multiline' property."
1093 (let ((changed nil))
1094 (when (and (> font-lock-beg (point-min))
1095 (get-text-property (1- font-lock-beg) 'font-lock-multiline))
1096 (setq changed t)
1097 (setq font-lock-beg (or (previous-single-property-change
1098 font-lock-beg 'font-lock-multiline)
1099 (point-min))))
1100 ;;
1101 (when (get-text-property font-lock-end 'font-lock-multiline)
1102 (setq changed t)
1103 (setq font-lock-end (or (text-property-any font-lock-end (point-max)
1104 'font-lock-multiline nil)
1105 (point-max))))
1106 changed))
1107
1108 (defun font-lock-extend-region-wholelines ()
1109 "Move fontification boundaries to beginning of lines."
1110 (let ((changed nil))
1111 (goto-char font-lock-beg)
1112 (unless (bolp)
1113 (setq changed t font-lock-beg (line-beginning-position)))
1114 (goto-char font-lock-end)
1115 (unless (bolp)
1116 (unless (eq font-lock-end
1117 (setq font-lock-end (line-beginning-position 2)))
1118 (setq changed t)))
1119 changed))
1120
1121 (defun font-lock-default-fontify-region (beg end loudly)
1122 (save-buffer-state
1123 ((parse-sexp-lookup-properties
1124 (or parse-sexp-lookup-properties font-lock-syntactic-keywords))
1125 (old-syntax-table (syntax-table)))
1126 (unwind-protect
1127 (save-restriction
1128 (unless font-lock-dont-widen (widen))
1129 ;; Use the fontification syntax table, if any.
1130 (when font-lock-syntax-table
1131 (set-syntax-table font-lock-syntax-table))
1132 ;; Extend the region to fontify so that it starts and ends at
1133 ;; safe places.
1134 (let ((funs font-lock-extend-region-functions)
1135 (font-lock-beg beg)
1136 (font-lock-end end))
1137 (while funs
1138 (setq funs (if (or (not (funcall (car funs)))
1139 (eq funs font-lock-extend-region-functions))
1140 (cdr funs)
1141 ;; If there's been a change, we should go through
1142 ;; the list again since this new position may
1143 ;; warrant a different answer from one of the fun
1144 ;; we've already seen.
1145 font-lock-extend-region-functions)))
1146 (setq beg font-lock-beg end font-lock-end))
1147 ;; Now do the fontification.
1148 (font-lock-unfontify-region beg end)
1149 (when font-lock-syntactic-keywords
1150 (font-lock-fontify-syntactic-keywords-region beg end))
1151 (unless font-lock-keywords-only
1152 (font-lock-fontify-syntactically-region beg end loudly))
1153 (font-lock-fontify-keywords-region beg end loudly))
1154 ;; Clean up.
1155 (set-syntax-table old-syntax-table))))
1156
1157 ;; The following must be rethought, since keywords can override fontification.
1158 ;; ;; Now scan for keywords, but not if we are inside a comment now.
1159 ;; (or (and (not font-lock-keywords-only)
1160 ;; (let ((state (parse-partial-sexp beg end nil nil
1161 ;; font-lock-cache-state)))
1162 ;; (or (nth 4 state) (nth 7 state))))
1163 ;; (font-lock-fontify-keywords-region beg end))
1164
1165 (defvar font-lock-extra-managed-props nil
1166 "Additional text properties managed by font-lock.
1167 This is used by `font-lock-default-unfontify-region' to decide
1168 what properties to clear before refontifying a region.")
1169
1170 (defun font-lock-default-unfontify-region (beg end)
1171 (remove-list-of-text-properties
1172 beg end (append
1173 font-lock-extra-managed-props
1174 (if font-lock-syntactic-keywords
1175 '(syntax-table face font-lock-multiline)
1176 '(face font-lock-multiline)))))
1177
1178 ;; Called when any modification is made to buffer text.
1179 (defun font-lock-after-change-function (beg end old-len)
1180 (save-excursion
1181 (let ((inhibit-point-motion-hooks t)
1182 (inhibit-quit t)
1183 (region (if font-lock-extend-after-change-region-function
1184 (funcall font-lock-extend-after-change-region-function
1185 beg end old-len))))
1186 (save-match-data
1187 (if region
1188 ;; Fontify the region the major mode has specified.
1189 (setq beg (car region) end (cdr region))
1190 ;; Fontify the whole lines which enclose the region.
1191 ;; Actually, this is not needed because
1192 ;; font-lock-default-fontify-region already rounds up to a whole
1193 ;; number of lines.
1194 ;; (setq beg (progn (goto-char beg) (line-beginning-position))
1195 ;; end (progn (goto-char end) (line-beginning-position 2)))
1196 (unless (eq end (point-max))
1197 ;; Rounding up to a whole number of lines should include the
1198 ;; line right after `end'. Typical case: the first char of
1199 ;; the line was deleted. Or a \n was inserted in the middle
1200 ;; of a line.
1201 (setq end (1+ end))))
1202 (font-lock-fontify-region beg end)))))
1203
1204 (defvar jit-lock-start) (defvar jit-lock-end)
1205 (defun font-lock-extend-jit-lock-region-after-change (beg end old-len)
1206 "Function meant for `jit-lock-after-change-extend-region-functions'.
1207 This function does 2 things:
1208 - extend the region so that it not only includes the part that was modified
1209 but also the surrounding text whose highlighting may change as a consequence.
1210 - anticipate (part of) the region extension that will happen later in
1211 `font-lock-default-fontify-region', in order to avoid the need for
1212 double-redisplay in `jit-lock-fontify-now'."
1213 (save-excursion
1214 ;; First extend the region as font-lock-after-change-function would.
1215 (let ((region (if font-lock-extend-after-change-region-function
1216 (funcall font-lock-extend-after-change-region-function
1217 beg end old-len))))
1218 (if region
1219 (setq beg (min jit-lock-start (car region))
1220 end (max jit-lock-end (cdr region))))
1221 ;; Then extend the region obeying font-lock-multiline properties,
1222 ;; indicating which part of the buffer needs to be refontified.
1223 ;; !!! This is the *main* user of font-lock-multiline property !!!
1224 ;; font-lock-after-change-function could/should also do that, but it
1225 ;; doesn't need to because font-lock-default-fontify-region does
1226 ;; it anyway. Here OTOH we have no guarantee that
1227 ;; font-lock-default-fontify-region will be executed on this region
1228 ;; any time soon.
1229 ;; Note: contrary to font-lock-default-fontify-region, we do not do
1230 ;; any loop here because we are not looking for a safe spot: we just
1231 ;; mark the text whose appearance may need to change as a result of
1232 ;; the buffer modification.
1233 (when (and (> beg (point-min))
1234 (get-text-property (1- beg) 'font-lock-multiline))
1235 (setq beg (or (previous-single-property-change
1236 beg 'font-lock-multiline)
1237 (point-min))))
1238 (when (< end (point-max))
1239 (setq end
1240 (if (get-text-property end 'font-lock-multiline)
1241 (or (text-property-any end (point-max)
1242 'font-lock-multiline nil)
1243 (point-max))
1244 ;; Rounding up to a whole number of lines should include the
1245 ;; line right after `end'. Typical case: the first char of
1246 ;; the line was deleted. Or a \n was inserted in the middle
1247 ;; of a line.
1248 (1+ end))))
1249 ;; Finally, pre-enlarge the region to a whole number of lines, to try
1250 ;; and anticipate what font-lock-default-fontify-region will do, so as to
1251 ;; avoid double-redisplay.
1252 ;; We could just run `font-lock-extend-region-functions', but since
1253 ;; the only purpose is to avoid the double-redisplay, we prefer to
1254 ;; do here only the part that is cheap and most likely to be useful.
1255 (when (memq 'font-lock-extend-region-wholelines
1256 font-lock-extend-region-functions)
1257 (goto-char beg)
1258 (setq jit-lock-start (min jit-lock-start (line-beginning-position)))
1259 (goto-char end)
1260 (setq jit-lock-end
1261 (max jit-lock-end
1262 (if (bolp) (point) (line-beginning-position 2))))))))
1263
1264 (defun font-lock-fontify-block (&optional arg)
1265 "Fontify some lines the way `font-lock-fontify-buffer' would.
1266 The lines could be a function or paragraph, or a specified number of lines.
1267 If ARG is given, fontify that many lines before and after point, or 16 lines if
1268 no ARG is given and `font-lock-mark-block-function' is nil.
1269 If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
1270 delimit the region to fontify."
1271 (interactive "P")
1272 (let ((inhibit-point-motion-hooks t) font-lock-beginning-of-syntax-function
1273 deactivate-mark)
1274 ;; Make sure we have the right `font-lock-keywords' etc.
1275 (if (not font-lock-mode) (font-lock-set-defaults))
1276 (save-excursion
1277 (save-match-data
1278 (condition-case error-data
1279 (if (or arg (not font-lock-mark-block-function))
1280 (let ((lines (if arg (prefix-numeric-value arg) 16)))
1281 (font-lock-fontify-region
1282 (save-excursion (forward-line (- lines)) (point))
1283 (save-excursion (forward-line lines) (point))))
1284 (funcall font-lock-mark-block-function)
1285 (font-lock-fontify-region (point) (mark)))
1286 ((error quit) (message "Fontifying block...%s" error-data)))))))
1287
1288 ;;; End of Fontification functions.
1289 \f
1290 ;;; Additional text property functions.
1291
1292 ;; The following text property functions should be builtins. This means they
1293 ;; should be written in C and put with all the other text property functions.
1294 ;; In the meantime, those that are used by font-lock.el are defined in Lisp
1295 ;; below and given a `font-lock-' prefix. Those that are not used are defined
1296 ;; in Lisp below and commented out. sm.
1297
1298 (defun font-lock-prepend-text-property (start end prop value &optional object)
1299 "Prepend to one property of the text from START to END.
1300 Arguments PROP and VALUE specify the property and value to prepend to the value
1301 already in place. The resulting property values are always lists.
1302 Optional argument OBJECT is the string or buffer containing the text."
1303 (let ((val (if (listp value) value (list value))) next prev)
1304 (while (/= start end)
1305 (setq next (next-single-property-change start prop object end)
1306 prev (get-text-property start prop object))
1307 ;; Canonicalize old forms of face property.
1308 (and (memq prop '(face font-lock-face))
1309 (listp prev)
1310 (or (keywordp (car prev))
1311 (memq (car prev) '(foreground-color background-color)))
1312 (setq prev (list prev)))
1313 (put-text-property start next prop
1314 (append val (if (listp prev) prev (list prev)))
1315 object)
1316 (setq start next))))
1317
1318 (defun font-lock-append-text-property (start end prop value &optional object)
1319 "Append to one property of the text from START to END.
1320 Arguments PROP and VALUE specify the property and value to append to the value
1321 already in place. The resulting property values are always lists.
1322 Optional argument OBJECT is the string or buffer containing the text."
1323 (let ((val (if (listp value) value (list value))) next prev)
1324 (while (/= start end)
1325 (setq next (next-single-property-change start prop object end)
1326 prev (get-text-property start prop object))
1327 ;; Canonicalize old forms of face property.
1328 (and (memq prop '(face font-lock-face))
1329 (listp prev)
1330 (or (keywordp (car prev))
1331 (memq (car prev) '(foreground-color background-color)))
1332 (setq prev (list prev)))
1333 (put-text-property start next prop
1334 (append (if (listp prev) prev (list prev)) val)
1335 object)
1336 (setq start next))))
1337
1338 (defun font-lock-fillin-text-property (start end prop value &optional object)
1339 "Fill in one property of the text from START to END.
1340 Arguments PROP and VALUE specify the property and value to put where none are
1341 already in place. Therefore existing property values are not overwritten.
1342 Optional argument OBJECT is the string or buffer containing the text."
1343 (let ((start (text-property-any start end prop nil object)) next)
1344 (while start
1345 (setq next (next-single-property-change start prop object end))
1346 (put-text-property start next prop value object)
1347 (setq start (text-property-any next end prop nil object)))))
1348
1349 ;; For completeness: this is to `remove-text-properties' as `put-text-property'
1350 ;; is to `add-text-properties', etc.
1351 ;;(defun remove-text-property (start end property &optional object)
1352 ;; "Remove a property from text from START to END.
1353 ;;Argument PROPERTY is the property to remove.
1354 ;;Optional argument OBJECT is the string or buffer containing the text.
1355 ;;Return t if the property was actually removed, nil otherwise."
1356 ;; (remove-text-properties start end (list property) object))
1357
1358 ;; For consistency: maybe this should be called `remove-single-property' like
1359 ;; `next-single-property-change' (not `next-single-text-property-change'), etc.
1360 ;;(defun remove-single-text-property (start end prop value &optional object)
1361 ;; "Remove a specific property value from text from START to END.
1362 ;;Arguments PROP and VALUE specify the property and value to remove. The
1363 ;;resulting property values are not equal to VALUE nor lists containing VALUE.
1364 ;;Optional argument OBJECT is the string or buffer containing the text."
1365 ;; (let ((start (text-property-not-all start end prop nil object)) next prev)
1366 ;; (while start
1367 ;; (setq next (next-single-property-change start prop object end)
1368 ;; prev (get-text-property start prop object))
1369 ;; (cond ((and (symbolp prev) (eq value prev))
1370 ;; (remove-text-property start next prop object))
1371 ;; ((and (listp prev) (memq value prev))
1372 ;; (let ((new (delq value prev)))
1373 ;; (cond ((null new)
1374 ;; (remove-text-property start next prop object))
1375 ;; ((= (length new) 1)
1376 ;; (put-text-property start next prop (car new) object))
1377 ;; (t
1378 ;; (put-text-property start next prop new object))))))
1379 ;; (setq start (text-property-not-all next end prop nil object)))))
1380
1381 ;;; End of Additional text property functions.
1382 \f
1383 ;;; Syntactic regexp fontification functions.
1384
1385 ;; These syntactic keyword pass functions are identical to those keyword pass
1386 ;; functions below, with the following exceptions; (a) they operate on
1387 ;; `font-lock-syntactic-keywords' of course, (b) they are all `defun' as speed
1388 ;; is less of an issue, (c) eval of property value does not occur JIT as speed
1389 ;; is less of an issue, (d) OVERRIDE cannot be `prepend' or `append' as it
1390 ;; makes no sense for `syntax-table' property values, (e) they do not do it
1391 ;; LOUDLY as it is not likely to be intensive.
1392
1393 (defun font-lock-apply-syntactic-highlight (highlight)
1394 "Apply HIGHLIGHT following a match.
1395 HIGHLIGHT should be of the form MATCH-HIGHLIGHT,
1396 see `font-lock-syntactic-keywords'."
1397 (let* ((match (nth 0 highlight))
1398 (start (match-beginning match)) (end (match-end match))
1399 (value (nth 1 highlight))
1400 (override (nth 2 highlight)))
1401 (if (not start)
1402 ;; No match but we might not signal an error.
1403 (or (nth 3 highlight)
1404 (error "No match %d in highlight %S" match highlight))
1405 (when (and (consp value) (not (numberp (car value))))
1406 (setq value (eval value)))
1407 (when (stringp value) (setq value (string-to-syntax value)))
1408 ;; Flush the syntax-cache. I believe this is not necessary for
1409 ;; font-lock's use of syntax-ppss, but I'm not 100% sure and it can
1410 ;; still be necessary for other users of syntax-ppss anyway.
1411 (syntax-ppss-after-change-function start)
1412 (cond
1413 ((not override)
1414 ;; Cannot override existing fontification.
1415 (or (text-property-not-all start end 'syntax-table nil)
1416 (put-text-property start end 'syntax-table value)))
1417 ((eq override t)
1418 ;; Override existing fontification.
1419 (put-text-property start end 'syntax-table value))
1420 ((eq override 'keep)
1421 ;; Keep existing fontification.
1422 (font-lock-fillin-text-property start end 'syntax-table value))))))
1423
1424 (defun font-lock-fontify-syntactic-anchored-keywords (keywords limit)
1425 "Fontify according to KEYWORDS until LIMIT.
1426 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1427 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1428 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1429 ;; Evaluate PRE-MATCH-FORM.
1430 (pre-match-value (eval (nth 1 keywords))))
1431 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1432 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1433 (setq limit pre-match-value)
1434 (setq limit (line-end-position)))
1435 (save-match-data
1436 ;; Find an occurrence of `matcher' before `limit'.
1437 (while (if (stringp matcher)
1438 (re-search-forward matcher limit t)
1439 (funcall matcher limit))
1440 ;; Apply each highlight to this instance of `matcher'.
1441 (setq highlights lowdarks)
1442 (while highlights
1443 (font-lock-apply-syntactic-highlight (car highlights))
1444 (setq highlights (cdr highlights)))))
1445 ;; Evaluate POST-MATCH-FORM.
1446 (eval (nth 2 keywords))))
1447
1448 (defun font-lock-fontify-syntactic-keywords-region (start end)
1449 "Fontify according to `font-lock-syntactic-keywords' between START and END.
1450 START should be at the beginning of a line."
1451 ;; Ensure the beginning of the file is properly syntactic-fontified.
1452 (when (and font-lock-syntactically-fontified
1453 (< font-lock-syntactically-fontified start))
1454 (setq start (max font-lock-syntactically-fontified (point-min)))
1455 (setq font-lock-syntactically-fontified end))
1456 ;; If `font-lock-syntactic-keywords' is a symbol, get the real keywords.
1457 (when (symbolp font-lock-syntactic-keywords)
1458 (setq font-lock-syntactic-keywords (font-lock-eval-keywords
1459 font-lock-syntactic-keywords)))
1460 ;; If `font-lock-syntactic-keywords' is not compiled, compile it.
1461 (unless (eq (car font-lock-syntactic-keywords) t)
1462 (setq font-lock-syntactic-keywords (font-lock-compile-keywords
1463 font-lock-syntactic-keywords
1464 t)))
1465 ;; Get down to business.
1466 (let ((case-fold-search font-lock-keywords-case-fold-search)
1467 (keywords (cddr font-lock-syntactic-keywords))
1468 keyword matcher highlights)
1469 (while keywords
1470 ;; Find an occurrence of `matcher' from `start' to `end'.
1471 (setq keyword (car keywords) matcher (car keyword))
1472 (goto-char start)
1473 (while (if (stringp matcher)
1474 (re-search-forward matcher end t)
1475 (funcall matcher end))
1476 ;; Apply each highlight to this instance of `matcher', which may be
1477 ;; specific highlights or more keywords anchored to `matcher'.
1478 (setq highlights (cdr keyword))
1479 (while highlights
1480 (if (numberp (car (car highlights)))
1481 (font-lock-apply-syntactic-highlight (car highlights))
1482 (font-lock-fontify-syntactic-anchored-keywords (car highlights)
1483 end))
1484 (setq highlights (cdr highlights))))
1485 (setq keywords (cdr keywords)))))
1486
1487 ;;; End of Syntactic regexp fontification functions.
1488 \f
1489 ;;; Syntactic fontification functions.
1490
1491 (defvar font-lock-comment-start-skip nil
1492 "If non-nil, Font Lock mode uses this instead of `comment-start-skip'.")
1493
1494 (defvar font-lock-comment-end-skip nil
1495 "If non-nil, Font Lock mode uses this instead of `comment-end'.")
1496
1497 (defun font-lock-fontify-syntactically-region (start end &optional loudly ppss)
1498 "Put proper face on each string and comment between START and END.
1499 START should be at the beginning of a line."
1500 (let ((comment-end-regexp
1501 (or font-lock-comment-end-skip
1502 (regexp-quote
1503 (replace-regexp-in-string "^ *" "" comment-end))))
1504 state face beg)
1505 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
1506 (goto-char start)
1507 ;;
1508 ;; Find the `start' state.
1509 (setq state (or ppss (syntax-ppss start)))
1510 ;;
1511 ;; Find each interesting place between here and `end'.
1512 (while
1513 (progn
1514 (when (or (nth 3 state) (nth 4 state))
1515 (setq face (funcall font-lock-syntactic-face-function state))
1516 (setq beg (max (nth 8 state) start))
1517 (setq state (parse-partial-sexp (point) end nil nil state
1518 'syntax-table))
1519 (when face (put-text-property beg (point) 'face face))
1520 (when (and (eq face 'font-lock-comment-face)
1521 (or font-lock-comment-start-skip
1522 comment-start-skip))
1523 ;; Find the comment delimiters
1524 ;; and use font-lock-comment-delimiter-face for them.
1525 (save-excursion
1526 (goto-char beg)
1527 (if (looking-at (or font-lock-comment-start-skip
1528 comment-start-skip))
1529 (put-text-property beg (match-end 0) 'face
1530 font-lock-comment-delimiter-face)))
1531 (if (looking-back comment-end-regexp (point-at-bol) t)
1532 (put-text-property (match-beginning 0) (point) 'face
1533 font-lock-comment-delimiter-face))))
1534 (< (point) end))
1535 (setq state (parse-partial-sexp (point) end nil nil state
1536 'syntax-table)))))
1537
1538 ;;; End of Syntactic fontification functions.
1539 \f
1540 ;;; Keyword regexp fontification functions.
1541
1542 (defsubst font-lock-apply-highlight (highlight)
1543 "Apply HIGHLIGHT following a match.
1544 HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
1545 (let* ((match (nth 0 highlight))
1546 (start (match-beginning match)) (end (match-end match))
1547 (override (nth 2 highlight)))
1548 (if (not start)
1549 ;; No match but we might not signal an error.
1550 (or (nth 3 highlight)
1551 (error "No match %d in highlight %S" match highlight))
1552 (let ((val (eval (nth 1 highlight))))
1553 (when (eq (car-safe val) 'face)
1554 (add-text-properties start end (cddr val))
1555 (setq val (cadr val)))
1556 (cond
1557 ((not (or val (eq override t)))
1558 ;; If `val' is nil, don't do anything. It is important to do it
1559 ;; explicitly, because when adding nil via things like
1560 ;; font-lock-append-text-property, the property is actually
1561 ;; changed from <face> to (<face>) which is undesirable. --Stef
1562 nil)
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 val)))
1567 ((eq override t)
1568 ;; Override existing fontification.
1569 (put-text-property start end 'face val))
1570 ((eq override 'prepend)
1571 ;; Prepend to existing fontification.
1572 (font-lock-prepend-text-property start end 'face val))
1573 ((eq override 'append)
1574 ;; Append to existing fontification.
1575 (font-lock-append-text-property start end 'face val))
1576 ((eq override 'keep)
1577 ;; Keep existing fontification.
1578 (font-lock-fillin-text-property start end 'face val)))))))
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 (>= limit (line-beginning-position 2)))
1593 ;; this is a multiline anchored match
1594 ;; (setq font-lock-multiline t)
1595 (put-text-property (if (= limit (line-beginning-position 2))
1596 (1- limit)
1597 (min lead-start (point)))
1598 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 LOUDLY, if non-nil, allows progress-meter bar."
1618 (unless (eq (car font-lock-keywords) t)
1619 (setq font-lock-keywords
1620 (font-lock-compile-keywords font-lock-keywords)))
1621 (let ((case-fold-search font-lock-keywords-case-fold-search)
1622 (keywords (cddr font-lock-keywords))
1623 (bufname (buffer-name)) (count 0)
1624 (pos (make-marker))
1625 keyword matcher highlights)
1626 ;;
1627 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1628 (while keywords
1629 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
1630 (make-string (incf count) ?.)))
1631 ;;
1632 ;; Find an occurrence of `matcher' from `start' to `end'.
1633 (setq keyword (car keywords) matcher (car keyword))
1634 (goto-char start)
1635 (while (and (< (point) end)
1636 (if (stringp matcher)
1637 (re-search-forward matcher end t)
1638 (funcall matcher end))
1639 ;; Beware empty string matches since they will
1640 ;; loop indefinitely.
1641 (or (> (point) (match-beginning 0))
1642 (progn (forward-char 1) t)))
1643 (when (and font-lock-multiline
1644 (>= (point)
1645 (save-excursion (goto-char (match-beginning 0))
1646 (forward-line 1) (point))))
1647 ;; this is a multiline regexp match
1648 ;; (setq font-lock-multiline t)
1649 (put-text-property (if (= (point)
1650 (save-excursion
1651 (goto-char (match-beginning 0))
1652 (forward-line 1) (point)))
1653 (1- (point))
1654 (match-beginning 0))
1655 (point)
1656 'font-lock-multiline t))
1657 ;; Apply each highlight to this instance of `matcher', which may be
1658 ;; specific highlights or more keywords anchored to `matcher'.
1659 (setq highlights (cdr keyword))
1660 (while highlights
1661 (if (numberp (car (car highlights)))
1662 (font-lock-apply-highlight (car highlights))
1663 (set-marker pos (point))
1664 (font-lock-fontify-anchored-keywords (car highlights) end)
1665 ;; Ensure forward progress. `pos' is a marker because anchored
1666 ;; keyword may add/delete text (this happens e.g. in grep.el).
1667 (if (< (point) pos) (goto-char pos)))
1668 (setq highlights (cdr highlights))))
1669 (setq keywords (cdr keywords)))
1670 (set-marker pos nil)))
1671
1672 ;;; End of Keyword regexp fontification functions.
1673 \f
1674 ;; Various functions.
1675
1676 (defun font-lock-compile-keywords (keywords &optional syntactic-keywords)
1677 "Compile KEYWORDS into the form (t KEYWORDS COMPILED...)
1678 Here each COMPILED is of the form (MATCHER HIGHLIGHT ...) as shown in the
1679 `font-lock-keywords' doc string.
1680 If SYNTACTIC-KEYWORDS is non-nil, it means these keywords are used for
1681 `font-lock-syntactic-keywords' rather than for `font-lock-keywords'."
1682 (if (not font-lock-set-defaults)
1683 ;; This should never happen. But some external packages sometimes
1684 ;; call font-lock in unexpected and incorrect ways. It's important to
1685 ;; stop processing at this point, otherwise we may end up changing the
1686 ;; global value of font-lock-keywords and break highlighting in many
1687 ;; other buffers.
1688 (error "Font-lock trying to use keywords before setting them up"))
1689 (if (eq (car-safe keywords) t)
1690 keywords
1691 (setq keywords
1692 (cons t (cons keywords
1693 (mapcar 'font-lock-compile-keyword keywords))))
1694 (if (and (not syntactic-keywords)
1695 (let ((beg-function
1696 (or font-lock-beginning-of-syntax-function
1697 syntax-begin-function)))
1698 (or (eq beg-function 'beginning-of-defun)
1699 (get beg-function 'font-lock-syntax-paren-check)))
1700 (not beginning-of-defun-function))
1701 ;; Try to detect when a string or comment contains something that
1702 ;; looks like a defun and would thus confuse font-lock.
1703 (nconc keywords
1704 `((,(if defun-prompt-regexp
1705 (concat "^\\(?:" defun-prompt-regexp "\\)?\\s(")
1706 "^\\s(")
1707 (0
1708 (if (memq (get-text-property (match-beginning 0) 'face)
1709 '(font-lock-string-face font-lock-doc-face
1710 font-lock-comment-face))
1711 (list 'face font-lock-warning-face
1712 'help-echo "Looks like a toplevel defun: escape the parenthesis"))
1713 prepend)))))
1714 keywords))
1715
1716 (defun font-lock-compile-keyword (keyword)
1717 (cond ((nlistp keyword) ; MATCHER
1718 (list keyword '(0 font-lock-keyword-face)))
1719 ((eq (car keyword) 'eval) ; (eval . FORM)
1720 (font-lock-compile-keyword (eval (cdr keyword))))
1721 ((eq (car-safe (cdr keyword)) 'quote) ; (MATCHER . 'FORM)
1722 ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote.
1723 (if (symbolp (nth 2 keyword))
1724 (list (car keyword) (list 0 (cdr keyword)))
1725 (font-lock-compile-keyword (cons (car keyword) (nth 2 keyword)))))
1726 ((numberp (cdr keyword)) ; (MATCHER . MATCH)
1727 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
1728 ((symbolp (cdr keyword)) ; (MATCHER . FACENAME)
1729 (list (car keyword) (list 0 (cdr keyword))))
1730 ((nlistp (nth 1 keyword)) ; (MATCHER . HIGHLIGHT)
1731 (list (car keyword) (cdr keyword)))
1732 (t ; (MATCHER HIGHLIGHT ...)
1733 keyword)))
1734
1735 (defun font-lock-eval-keywords (keywords)
1736 "Evalulate KEYWORDS if a function (funcall) or variable (eval) name."
1737 (if (listp keywords)
1738 keywords
1739 (font-lock-eval-keywords (if (fboundp keywords)
1740 (funcall keywords)
1741 (eval keywords)))))
1742
1743 (defun font-lock-value-in-major-mode (alist)
1744 "Return value in ALIST for `major-mode', or ALIST if it is not an alist.
1745 Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t."
1746 (if (consp alist)
1747 (cdr (or (assq major-mode alist) (assq t alist)))
1748 alist))
1749
1750 (defun font-lock-choose-keywords (keywords level)
1751 "Return LEVELth element of KEYWORDS.
1752 A LEVEL of nil is equal to a LEVEL of 0, a LEVEL of t is equal to
1753 \(1- (length KEYWORDS))."
1754 (cond ((not (and (listp keywords) (symbolp (car keywords))))
1755 keywords)
1756 ((numberp level)
1757 (or (nth level keywords) (car (last keywords))))
1758 ((eq level t)
1759 (car (last keywords)))
1760 (t
1761 (car keywords))))
1762
1763 (defvar font-lock-set-defaults nil) ; Whether we have set up defaults.
1764
1765 (defvar font-lock-mode-major-mode)
1766 (defun font-lock-set-defaults ()
1767 "Set fontification defaults appropriately for this mode.
1768 Sets various variables using `font-lock-defaults' (or, if nil, using
1769 `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
1770 ;; Set fontification defaults if not previously set for correct major mode.
1771 (unless (and font-lock-set-defaults
1772 (eq font-lock-mode-major-mode major-mode))
1773 (setq font-lock-mode-major-mode major-mode)
1774 (set (make-local-variable 'font-lock-set-defaults) t)
1775 (make-local-variable 'font-lock-fontified)
1776 (make-local-variable 'font-lock-multiline)
1777 (let* ((defaults (or font-lock-defaults
1778 (cdr (assq major-mode
1779 (with-no-warnings
1780 font-lock-defaults-alist)))))
1781 (keywords
1782 (font-lock-choose-keywords (nth 0 defaults)
1783 (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1784 (local (cdr (assq major-mode font-lock-keywords-alist)))
1785 (removed-keywords
1786 (cdr-safe (assq major-mode font-lock-removed-keywords-alist))))
1787 (set (make-local-variable 'font-lock-defaults) defaults)
1788 ;; Syntactic fontification?
1789 (if (nth 1 defaults)
1790 (set (make-local-variable 'font-lock-keywords-only) t)
1791 (kill-local-variable 'font-lock-keywords-only))
1792 ;; Case fold during regexp fontification?
1793 (if (nth 2 defaults)
1794 (set (make-local-variable 'font-lock-keywords-case-fold-search) t)
1795 (kill-local-variable 'font-lock-keywords-case-fold-search))
1796 ;; Syntax table for regexp and syntactic fontification?
1797 (if (null (nth 3 defaults))
1798 (kill-local-variable 'font-lock-syntax-table)
1799 (set (make-local-variable 'font-lock-syntax-table)
1800 (copy-syntax-table (syntax-table)))
1801 (dolist (selem (nth 3 defaults))
1802 ;; The character to modify may be a single CHAR or a STRING.
1803 (let ((syntax (cdr selem)))
1804 (dolist (char (if (numberp (car selem))
1805 (list (car selem))
1806 (mapcar 'identity (car selem))))
1807 (modify-syntax-entry char syntax font-lock-syntax-table)))))
1808 ;; Syntax function for syntactic fontification?
1809 (if (nth 4 defaults)
1810 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1811 (nth 4 defaults))
1812 (kill-local-variable 'font-lock-beginning-of-syntax-function))
1813 ;; Variable alist?
1814 (dolist (x (nthcdr 5 defaults))
1815 (set (make-local-variable (car x)) (cdr x)))
1816 ;; Set up `font-lock-keywords' last because its value might depend
1817 ;; on other settings (e.g. font-lock-compile-keywords uses
1818 ;; font-lock-beginning-of-syntax-function).
1819 (set (make-local-variable 'font-lock-keywords)
1820 (font-lock-eval-keywords keywords))
1821 ;; Local fontification?
1822 (while local
1823 (font-lock-add-keywords nil (car (car local)) (cdr (car local)))
1824 (setq local (cdr local)))
1825 (when removed-keywords
1826 (font-lock-remove-keywords nil removed-keywords))
1827 ;; Now compile the keywords.
1828 (unless (eq (car font-lock-keywords) t)
1829 (setq font-lock-keywords
1830 (font-lock-compile-keywords font-lock-keywords))))))
1831 \f
1832 ;;; Color etc. support.
1833
1834 ;; Note that `defface' will not overwrite any faces declared above via
1835 ;; `custom-declare-face'.
1836 (defface font-lock-comment-face
1837 '((((class grayscale) (background light))
1838 (:foreground "DimGray" :weight bold :slant italic))
1839 (((class grayscale) (background dark))
1840 (:foreground "LightGray" :weight bold :slant italic))
1841 (((class color) (min-colors 88) (background light))
1842 (:foreground "Firebrick"))
1843 (((class color) (min-colors 88) (background dark))
1844 (:foreground "chocolate1"))
1845 (((class color) (min-colors 16) (background light))
1846 (:foreground "red"))
1847 (((class color) (min-colors 16) (background dark))
1848 (:foreground "red1"))
1849 (((class color) (min-colors 8) (background light))
1850 (:foreground "red"))
1851 (((class color) (min-colors 8) (background dark))
1852 )
1853 (t (:weight bold :slant italic)))
1854 "Font Lock mode face used to highlight comments."
1855 :group 'font-lock-faces)
1856
1857 (defface font-lock-comment-delimiter-face
1858 '((default :inherit font-lock-comment-face)
1859 (((class grayscale)))
1860 (((class color) (min-colors 16)))
1861 (((class color) (min-colors 8) (background light))
1862 :foreground "red")
1863 (((class color) (min-colors 8) (background dark))
1864 :foreground "red1"))
1865 "Font Lock mode face used to highlight comment delimiters."
1866 :group 'font-lock-faces)
1867
1868 (defface font-lock-string-face
1869 '((((class grayscale) (background light)) (:foreground "DimGray" :slant italic))
1870 (((class grayscale) (background dark)) (:foreground "LightGray" :slant italic))
1871 (((class color) (min-colors 88) (background light)) (:foreground "RosyBrown"))
1872 (((class color) (min-colors 88) (background dark)) (:foreground "LightSalmon"))
1873 (((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
1874 (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
1875 (((class color) (min-colors 8)) (:foreground "green"))
1876 (t (:slant italic)))
1877 "Font Lock mode face used to highlight strings."
1878 :group 'font-lock-faces)
1879
1880 (defface font-lock-doc-face
1881 '((t :inherit font-lock-string-face))
1882 "Font Lock mode face used to highlight documentation."
1883 :group 'font-lock-faces)
1884
1885 (defface font-lock-keyword-face
1886 '((((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
1887 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
1888 (((class color) (min-colors 88) (background light)) (:foreground "Purple"))
1889 (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
1890 (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
1891 (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
1892 (((class color) (min-colors 8)) (:foreground "cyan" :weight bold))
1893 (t (:weight bold)))
1894 "Font Lock mode face used to highlight keywords."
1895 :group 'font-lock-faces)
1896
1897 (defface font-lock-builtin-face
1898 '((((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
1899 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
1900 (((class color) (min-colors 88) (background light)) (:foreground "Orchid"))
1901 (((class color) (min-colors 88) (background dark)) (:foreground "LightSteelBlue"))
1902 (((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
1903 (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
1904 (((class color) (min-colors 8)) (:foreground "blue" :weight bold))
1905 (t (:weight bold)))
1906 "Font Lock mode face used to highlight builtins."
1907 :group 'font-lock-faces)
1908
1909 (defface font-lock-function-name-face
1910 '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
1911 (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
1912 (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
1913 (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
1914 (((class color) (min-colors 8)) (:foreground "blue" :weight bold))
1915 (t (:inverse-video t :weight bold)))
1916 "Font Lock mode face used to highlight function names."
1917 :group 'font-lock-faces)
1918
1919 (defface font-lock-variable-name-face
1920 '((((class grayscale) (background light))
1921 (:foreground "Gray90" :weight bold :slant italic))
1922 (((class grayscale) (background dark))
1923 (:foreground "DimGray" :weight bold :slant italic))
1924 (((class color) (min-colors 88) (background light)) (:foreground "DarkGoldenrod"))
1925 (((class color) (min-colors 88) (background dark)) (:foreground "LightGoldenrod"))
1926 (((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
1927 (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
1928 (((class color) (min-colors 8)) (:foreground "yellow" :weight light))
1929 (t (:weight bold :slant italic)))
1930 "Font Lock mode face used to highlight variable names."
1931 :group 'font-lock-faces)
1932
1933 (defface font-lock-type-face
1934 '((((class grayscale) (background light)) (:foreground "Gray90" :weight bold))
1935 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
1936 (((class color) (min-colors 88) (background light)) (:foreground "ForestGreen"))
1937 (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
1938 (((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
1939 (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
1940 (((class color) (min-colors 8)) (:foreground "green"))
1941 (t (:weight bold :underline t)))
1942 "Font Lock mode face used to highlight type and classes."
1943 :group 'font-lock-faces)
1944
1945 (defface font-lock-constant-face
1946 '((((class grayscale) (background light))
1947 (:foreground "LightGray" :weight bold :underline t))
1948 (((class grayscale) (background dark))
1949 (:foreground "Gray50" :weight bold :underline t))
1950 (((class color) (min-colors 88) (background light)) (:foreground "CadetBlue"))
1951 (((class color) (min-colors 88) (background dark)) (:foreground "Aquamarine"))
1952 (((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
1953 (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
1954 (((class color) (min-colors 8)) (:foreground "magenta"))
1955 (t (:weight bold :underline t)))
1956 "Font Lock mode face used to highlight constants and labels."
1957 :group 'font-lock-faces)
1958
1959 (defface font-lock-warning-face
1960 '((((class color) (min-colors 88) (background light)) (:foreground "Red1" :weight bold))
1961 (((class color) (min-colors 88) (background dark)) (:foreground "Pink" :weight bold))
1962 (((class color) (min-colors 16) (background light)) (:foreground "Red1" :weight bold))
1963 (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :weight bold))
1964 (((class color) (min-colors 8)) (:foreground "red"))
1965 (t (:inverse-video t :weight bold)))
1966 "Font Lock mode face used to highlight warnings."
1967 :group 'font-lock-faces)
1968
1969 (defface font-lock-negation-char-face
1970 '((t nil))
1971 "Font Lock mode face used to highlight easy to overlook negation."
1972 :group 'font-lock-faces)
1973
1974 (defface font-lock-preprocessor-face
1975 '((t :inherit font-lock-builtin-face))
1976 "Font Lock mode face used to highlight preprocessor directives."
1977 :group 'font-lock-faces)
1978
1979 (defface font-lock-regexp-grouping-backslash
1980 '((t :inherit bold))
1981 "Font Lock mode face for backslashes in Lisp regexp grouping constructs."
1982 :group 'font-lock-faces)
1983
1984 (defface font-lock-regexp-grouping-construct
1985 '((t :inherit bold))
1986 "Font Lock mode face used to highlight grouping constructs in Lisp regexps."
1987 :group 'font-lock-faces)
1988
1989 ;;; End of Color etc. support.
1990 \f
1991 ;;; Menu support.
1992
1993 ;; This section of code is commented out because Emacs does not have real menu
1994 ;; buttons. (We can mimic them by putting "( ) " or "(X) " at the beginning of
1995 ;; the menu entry text, but with Xt it looks both ugly and embarrassingly
1996 ;; amateur.) If/When Emacs gets real menus buttons, put in menu-bar.el after
1997 ;; the entry for "Text Properties" something like:
1998 ;;
1999 ;; (define-key menu-bar-edit-menu [font-lock]
2000 ;; (cons "Syntax Highlighting" font-lock-menu))
2001 ;;
2002 ;; and remove a single ";" from the beginning of each line in the rest of this
2003 ;; section. Probably the mechanism for telling the menu code what are menu
2004 ;; buttons and when they are on or off needs tweaking. I have assumed that the
2005 ;; mechanism is via `menu-toggle' and `menu-selected' symbol properties. sm.
2006
2007 ;;;;;###autoload
2008 ;;(progn
2009 ;; ;; Make the Font Lock menu.
2010 ;; (defvar font-lock-menu (make-sparse-keymap "Syntax Highlighting"))
2011 ;; ;; Add the menu items in reverse order.
2012 ;; (define-key font-lock-menu [fontify-less]
2013 ;; '("Less In Current Buffer" . font-lock-fontify-less))
2014 ;; (define-key font-lock-menu [fontify-more]
2015 ;; '("More In Current Buffer" . font-lock-fontify-more))
2016 ;; (define-key font-lock-menu [font-lock-sep]
2017 ;; '("--"))
2018 ;; (define-key font-lock-menu [font-lock-mode]
2019 ;; '("In Current Buffer" . font-lock-mode))
2020 ;; (define-key font-lock-menu [global-font-lock-mode]
2021 ;; '("In All Buffers" . global-font-lock-mode)))
2022 ;;
2023 ;;;;;###autoload
2024 ;;(progn
2025 ;; ;; We put the appropriate `menu-enable' etc. symbol property values on when
2026 ;; ;; font-lock.el is loaded, so we don't need to autoload the three variables.
2027 ;; (put 'global-font-lock-mode 'menu-toggle t)
2028 ;; (put 'font-lock-mode 'menu-toggle t)
2029 ;; (put 'font-lock-fontify-more 'menu-enable '(identity))
2030 ;; (put 'font-lock-fontify-less 'menu-enable '(identity)))
2031 ;;
2032 ;; ;; Put the appropriate symbol property values on now. See above.
2033 ;;(put 'global-font-lock-mode 'menu-selected 'global-font-lock-mode)
2034 ;;(put 'font-lock-mode 'menu-selected 'font-lock-mode)
2035 ;;(put 'font-lock-fontify-more 'menu-enable '(nth 2 font-lock-fontify-level))
2036 ;;(put 'font-lock-fontify-less 'menu-enable '(nth 1 font-lock-fontify-level))
2037 ;;
2038 ;;(defvar font-lock-fontify-level nil) ; For less/more fontification.
2039 ;;
2040 ;;(defun font-lock-fontify-level (level)
2041 ;; (let ((font-lock-maximum-decoration level))
2042 ;; (when font-lock-mode
2043 ;; (font-lock-mode))
2044 ;; (font-lock-mode)
2045 ;; (when font-lock-verbose
2046 ;; (message "Fontifying %s... level %d" (buffer-name) level))))
2047 ;;
2048 ;;(defun font-lock-fontify-less ()
2049 ;; "Fontify the current buffer with less decoration.
2050 ;;See `font-lock-maximum-decoration'."
2051 ;; (interactive)
2052 ;; ;; Check in case we get called interactively.
2053 ;; (if (nth 1 font-lock-fontify-level)
2054 ;; (font-lock-fontify-level (1- (car font-lock-fontify-level)))
2055 ;; (error "No less decoration")))
2056 ;;
2057 ;;(defun font-lock-fontify-more ()
2058 ;; "Fontify the current buffer with more decoration.
2059 ;;See `font-lock-maximum-decoration'."
2060 ;; (interactive)
2061 ;; ;; Check in case we get called interactively.
2062 ;; (if (nth 2 font-lock-fontify-level)
2063 ;; (font-lock-fontify-level (1+ (car font-lock-fontify-level)))
2064 ;; (error "No more decoration")))
2065 ;;
2066 ;; ;; This should be called by `font-lock-set-defaults'.
2067 ;;(defun font-lock-set-menu ()
2068 ;; ;; Activate less/more fontification entries if there are multiple levels for
2069 ;; ;; the current buffer. Sets `font-lock-fontify-level' to be of the form
2070 ;; ;; (CURRENT-LEVEL IS-LOWER-LEVEL-P IS-HIGHER-LEVEL-P) for menu activation.
2071 ;; (let ((keywords (or (nth 0 font-lock-defaults)
2072 ;; (nth 1 (assq major-mode font-lock-defaults-alist))))
2073 ;; (level (font-lock-value-in-major-mode font-lock-maximum-decoration)))
2074 ;; (make-local-variable 'font-lock-fontify-level)
2075 ;; (if (or (symbolp keywords) (= (length keywords) 1))
2076 ;; (font-lock-unset-menu)
2077 ;; (cond ((eq level t)
2078 ;; (setq level (1- (length keywords))))
2079 ;; ((or (null level) (zerop level))
2080 ;; ;; The default level is usually, but not necessarily, level 1.
2081 ;; (setq level (- (length keywords)
2082 ;; (length (member (eval (car keywords))
2083 ;; (mapcar 'eval (cdr keywords))))))))
2084 ;; (setq font-lock-fontify-level (list level (> level 1)
2085 ;; (< level (1- (length keywords))))))))
2086 ;;
2087 ;; ;; This should be called by `font-lock-unset-defaults'.
2088 ;;(defun font-lock-unset-menu ()
2089 ;; ;; Deactivate less/more fontification entries.
2090 ;; (setq font-lock-fontify-level nil))
2091
2092 ;;; End of Menu support.
2093 \f
2094 ;;; Various regexp information shared by several modes.
2095 ;; ;; Information specific to a single mode should go in its load library.
2096
2097 ;; Font Lock support for C, C++, Objective-C and Java modes is now in
2098 ;; cc-fonts.el (and required by cc-mode.el). However, the below function
2099 ;; should stay in font-lock.el, since it is used by other libraries. sm.
2100
2101 (defun font-lock-match-c-style-declaration-item-and-skip-to-next (limit)
2102 "Match, and move over, any declaration/definition item after point.
2103 Matches after point, but ignores leading whitespace and `*' characters.
2104 Does not move further than LIMIT.
2105
2106 The expected syntax of a declaration/definition item is `word' (preceded by
2107 optional whitespace and `*' characters and proceeded by optional whitespace)
2108 optionally followed by a `('. Everything following the item (but belonging to
2109 it) is expected to be skip-able by `scan-sexps', and items are expected to be
2110 separated with a `,' and to be terminated with a `;'.
2111
2112 Thus the regexp matches after point: word (
2113 ^^^^ ^
2114 Where the match subexpressions are: 1 2
2115
2116 The item is delimited by (match-beginning 1) and (match-end 1).
2117 If (match-beginning 2) is non-nil, the item is followed by a `('.
2118
2119 This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
2120 (when (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?")
2121 (when (and (match-end 2) (> (- (match-end 2) (match-beginning 2)) 1))
2122 ;; If `word' is followed by a double open-paren, it's probably
2123 ;; a macro used for "int myfun P_ ((int arg1))". Let's go back one
2124 ;; word to try and match `myfun' rather than `P_'.
2125 (let ((pos (point)))
2126 (skip-chars-backward " \t\n")
2127 (skip-syntax-backward "w")
2128 (unless (looking-at "\\(\\sw+\\)[ \t\n]*\\sw+[ \t\n]*\\(((?\\)?")
2129 ;; Looks like it was something else, so go back to where we
2130 ;; were and reset the match data by rematching.
2131 (goto-char pos)
2132 (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?"))))
2133 (save-match-data
2134 (condition-case nil
2135 (save-restriction
2136 ;; Restrict to the LIMIT.
2137 (narrow-to-region (point-min) limit)
2138 (goto-char (match-end 1))
2139 ;; Move over any item value, etc., to the next item.
2140 (while (not (looking-at "[ \t\n]*\\(\\(,\\)\\|;\\|\\'\\)"))
2141 (goto-char (or (scan-sexps (point) 1) (point-max))))
2142 (if (match-end 2)
2143 (goto-char (match-end 2))))
2144 (error t)))))
2145
2146 ;; C preprocessor(cpp) is used outside of C, C++ and Objective-C source file.
2147 ;; e.g. assembler code and GNU linker script in Linux kernel.
2148 ;; `cpp-font-lock-keywords' is handy for modes for the files.
2149 ;;
2150 ;; Here we cannot use `regexp-opt' because because regex-opt is not preloaded
2151 ;; while font-lock.el is preloaded to emacs. So values pre-calculated with
2152 ;; regexp-opt are used here.
2153
2154 ;; `cpp-font-lock-keywords-source-directives' is calculated from:
2155 ;;
2156 ;; (regexp-opt
2157 ;; '("define" "elif" "else" "endif" "error" "file" "if" "ifdef"
2158 ;; "ifndef" "import" "include" "line" "pragma" "undef" "warning"))
2159 ;;
2160 (defconst cpp-font-lock-keywords-source-directives
2161 "define\\|e\\(?:l\\(?:if\\|se\\)\\|ndif\\|rror\\)\\|file\\|i\\(?:f\\(?:n?def\\)?\\|mport\\|nclude\\)\\|line\\|pragma\\|undef\\|warning"
2162 "Regular expression used in `cpp-font-lock-keywords'.")
2163
2164 ;; `cpp-font-lock-keywords-source-depth' is calculated from:
2165 ;;
2166 ;; (regexp-opt-depth (regexp-opt
2167 ;; '("define" "elif" "else" "endif" "error" "file" "if" "ifdef"
2168 ;; "ifndef" "import" "include" "line" "pragma" "undef" "warning")))
2169 ;;
2170 (defconst cpp-font-lock-keywords-source-depth 0
2171 "An integer representing regular expression depth of `cpp-font-lock-keywords-source-directives'.
2172 Used in `cpp-font-lock-keywords'.")
2173
2174 (defconst cpp-font-lock-keywords
2175 (let* ((directives cpp-font-lock-keywords-source-directives)
2176 (directives-depth cpp-font-lock-keywords-source-depth))
2177 (list
2178 ;;
2179 ;; Fontify error directives.
2180 '("^#[ \t]*\\(?:error\\|warning\\)[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend)
2181 ;;
2182 ;; Fontify filenames in #include <...> preprocessor directives as strings.
2183 '("^#[ \t]*\\(?:import\\|include\\)[ \t]*\\(<[^>\"\n]*>?\\)"
2184 1 font-lock-string-face prepend)
2185 ;;
2186 ;; Fontify function macro names.
2187 '("^#[ \t]*define[ \t]+\\([[:alpha:]_][[:alnum:]_$]*\\)("
2188 (1 font-lock-function-name-face prepend)
2189 ;;
2190 ;; Macro arguments.
2191 ((lambda (limit)
2192 (re-search-forward
2193 "\\(?:\\([[:alpha:]_][[:alnum:]_]*\\)[,]?\\)"
2194 (or (save-excursion (re-search-forward ")" limit t))
2195 limit)
2196 t))
2197 nil nil (1 font-lock-variable-name-face prepend)))
2198 ;;
2199 ;; Fontify symbol names in #elif or #if ... defined preprocessor directives.
2200 '("^#[ \t]*\\(?:elif\\|if\\)\\>"
2201 ("\\<\\(defined\\)\\>[ \t]*(?\\([[:alpha:]_][[:alnum:]_]*\\)?" nil nil
2202 (1 font-lock-builtin-face prepend) (2 font-lock-variable-name-face prepend t)))
2203 ;;
2204 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
2205 (list
2206 (concat "^\\(#[ \t]*\\(?:" directives
2207 "\\)\\)\\>[ \t!]*\\([[:alpha:]_][[:alnum:]_]*\\)?")
2208 '(1 font-lock-preprocessor-face prepend)
2209 (list (+ 2 directives-depth)
2210 'font-lock-variable-name-face nil t))))
2211 "Font lock keywords for C preprocessor directives.
2212 `c-mode', `c++-mode' and `objc-mode' have their own font lock keywords
2213 for C preprocessor directives. This definition is for the other modes
2214 in which C preprocessor directives are used. e.g. `asm-mode' and
2215 `ld-script-mode'.")
2216
2217 \f
2218 ;; Lisp.
2219
2220 (defconst lisp-font-lock-keywords-1
2221 (eval-when-compile
2222 `(;; Definitions.
2223 (,(concat "(\\(def\\("
2224 ;; Function declarations.
2225 "\\(advice\\|alias\\|generic\\|macro\\*?\\|method\\|"
2226 "setf\\|subst\\*?\\|un\\*?\\|"
2227 "ine-\\(condition\\|"
2228 "\\(?:derived\\|\\(?:global\\(?:ized\\)?-\\)?minor\\|generic\\)-mode\\|"
2229 "method-combination\\|setf-expander\\|skeleton\\|widget\\|"
2230 "function\\|\\(compiler\\|modify\\|symbol\\)-macro\\)\\)\\|"
2231 ;; Variable declarations.
2232 "\\(const\\(ant\\)?\\|custom\\|varalias\\|face\\|parameter\\|var\\)\\|"
2233 ;; Structure declarations.
2234 "\\(class\\|group\\|theme\\|package\\|struct\\|type\\)"
2235 "\\)\\)\\>"
2236 ;; Any whitespace and defined object.
2237 "[ \t'\(]*"
2238 "\\(setf[ \t]+\\sw+)\\|\\sw+\\)?")
2239 (1 font-lock-keyword-face)
2240 (9 (cond ((match-beginning 3) font-lock-function-name-face)
2241 ((match-beginning 6) font-lock-variable-name-face)
2242 (t font-lock-type-face))
2243 nil t))
2244 ;; Emacs Lisp autoload cookies. Supports the slightly different
2245 ;; forms used by mh-e, calendar, etc.
2246 ("^;;;###\\([-a-z]*autoload\\)" 1 font-lock-warning-face prepend)
2247 ;; Regexp negated char group.
2248 ("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)))
2249 "Subdued level highlighting for Lisp modes.")
2250
2251 (defconst lisp-font-lock-keywords-2
2252 (append lisp-font-lock-keywords-1
2253 (eval-when-compile
2254 `(;; Control structures. Emacs Lisp forms.
2255 (,(concat
2256 "(" (regexp-opt
2257 '("cond" "if" "while" "while-no-input" "let" "let*"
2258 "prog" "progn" "progv" "prog1" "prog2" "prog*"
2259 "inline" "lambda" "save-restriction" "save-excursion"
2260 "save-window-excursion" "save-selected-window"
2261 "save-match-data" "save-current-buffer" "unwind-protect"
2262 "condition-case" "track-mouse"
2263 "eval-after-load" "eval-and-compile" "eval-when-compile"
2264 "eval-when" "eval-at-startup" "eval-next-after-load"
2265 "with-case-table" "with-category-table"
2266 "with-current-buffer" "with-electric-help"
2267 "with-local-quit" "with-no-warnings"
2268 "with-output-to-string" "with-output-to-temp-buffer"
2269 "with-selected-window" "with-selected-frame" "with-syntax-table"
2270 "with-temp-buffer" "with-temp-file" "with-temp-message"
2271 "with-timeout" "with-timeout-handler") t)
2272 "\\>")
2273 . 1)
2274 ;; Control structures. Common Lisp forms.
2275 (,(concat
2276 "(" (regexp-opt
2277 '("when" "unless" "case" "ecase" "typecase" "etypecase"
2278 "ccase" "ctypecase" "handler-case" "handler-bind"
2279 "restart-bind" "restart-case" "in-package"
2280 "break" "ignore-errors"
2281 "loop" "do" "do*" "dotimes" "dolist" "the" "locally"
2282 "proclaim" "declaim" "declare" "symbol-macrolet"
2283 "lexical-let" "lexical-let*" "flet" "labels" "compiler-let"
2284 "destructuring-bind" "macrolet" "tagbody" "block" "go"
2285 "multiple-value-bind" "multiple-value-prog1"
2286 "return" "return-from"
2287 "with-accessors" "with-compilation-unit"
2288 "with-condition-restarts" "with-hash-table-iterator"
2289 "with-input-from-string" "with-open-file"
2290 "with-open-stream" "with-output-to-string"
2291 "with-package-iterator" "with-simple-restart"
2292 "with-slots" "with-standard-io-syntax") t)
2293 "\\>")
2294 . 1)
2295 ;; Exit/Feature symbols as constants.
2296 (,(concat "(\\(catch\\|throw\\|featurep\\|provide\\|require\\)\\>"
2297 "[ \t']*\\(\\sw+\\)?")
2298 (1 font-lock-keyword-face)
2299 (2 font-lock-constant-face nil t))
2300 ;; Erroneous structures.
2301 ("(\\(abort\\|assert\\|warn\\|check-type\\|cerror\\|error\\|signal\\)\\>" 1 font-lock-warning-face)
2302 ;; Words inside \\[] tend to be for `substitute-command-keys'.
2303 ("\\\\\\\\\\[\\(\\sw+\\)\\]" 1 font-lock-constant-face prepend)
2304 ;; Words inside `' tend to be symbol names.
2305 ("`\\(\\sw\\sw+\\)'" 1 font-lock-constant-face prepend)
2306 ;; Constant values.
2307 ("\\<:\\sw+\\>" 0 font-lock-builtin-face)
2308 ;; ELisp and CLisp `&' keywords as types.
2309 ("\\<\\&\\sw+\\>" . font-lock-type-face)
2310 ;; ELisp regexp grouping constructs
2311 ((lambda (bound)
2312 (catch 'found
2313 ;; The following loop is needed to continue searching after matches
2314 ;; that do not occur in strings. The associated regexp matches one
2315 ;; of `\\\\' `\\(' `\\(?:' `\\|' `\\)'. `\\\\' has been included to
2316 ;; avoid highlighting, for example, `\\(' in `\\\\('.
2317 (while (re-search-forward "\\(\\\\\\\\\\)\\(?:\\(\\\\\\\\\\)\\|\\((\\(?:\\?[0-9]*:\\)?\\|[|)]\\)\\)" bound t)
2318 (unless (match-beginning 2)
2319 (let ((face (get-text-property (1- (point)) 'face)))
2320 (when (or (and (listp face)
2321 (memq 'font-lock-string-face face))
2322 (eq 'font-lock-string-face face))
2323 (throw 'found t)))))))
2324 (1 'font-lock-regexp-grouping-backslash prepend)
2325 (3 'font-lock-regexp-grouping-construct prepend))
2326 ;;; This is too general -- rms.
2327 ;;; A user complained that he has functions whose names start with `do'
2328 ;;; and that they get the wrong color.
2329 ;;; ;; CL `with-' and `do-' constructs
2330 ;;; ("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
2331 )))
2332 "Gaudy level highlighting for Lisp modes.")
2333
2334 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
2335 "Default expressions to highlight in Lisp modes.")
2336 \f
2337 (provide 'font-lock)
2338
2339 ;; arch-tag: 682327e4-64d8-4057-b20b-1fbb9f1fc54c
2340 ;;; font-lock.el ends here