* align.el:
[bpt/emacs.git] / 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 set via the function `font-lock-set-defaults', based on
494 the CASE-FOLD argument of `font-lock-defaults'.")
495 (make-variable-buffer-local 'font-lock-keywords-case-fold-search)
496
497 (defvar font-lock-syntactically-fontified 0
498 "Point up to which `font-lock-syntactic-keywords' has been applied.
499 If nil, this is ignored, in which case the syntactic fontification may
500 sometimes be slightly incorrect.")
501 (make-variable-buffer-local 'font-lock-syntactically-fontified)
502
503 (defvar font-lock-syntactic-face-function
504 (lambda (state)
505 (if (nth 3 state) font-lock-string-face font-lock-comment-face))
506 "Function to determine which face to use when fontifying syntactically.
507 The function is called with a single parameter (the state as returned by
508 `parse-partial-sexp' at the beginning of the region to highlight) and
509 should return a face. This is normally set via `font-lock-defaults'.")
510
511 (defvar font-lock-syntactic-keywords nil
512 "A list of the syntactic keywords to put syntax properties on.
513 The value can be the list itself, or the name of a function or variable
514 whose value is the list.
515
516 See `font-lock-keywords' for a description of the form of this list;
517 only the differences are stated here. MATCH-HIGHLIGHT should be of the form:
518
519 (SUBEXP SYNTAX OVERRIDE LAXMATCH)
520
521 where SYNTAX can be a string (as taken by `modify-syntax-entry'), a syntax
522 table, a cons cell (as returned by `string-to-syntax') or an expression whose
523 value is such a form. OVERRIDE cannot be `prepend' or `append'.
524
525 Here are two examples of elements of `font-lock-syntactic-keywords'
526 and what they do:
527
528 (\"\\\\$\\\\(#\\\\)\" 1 \".\")
529
530 gives a hash character punctuation syntax (\".\") when following a
531 dollar-sign character. Hash characters in other contexts will still
532 follow whatever the syntax table says about the hash character.
533
534 (\"\\\\('\\\\).\\\\('\\\\)\"
535 (1 \"\\\"\")
536 (2 \"\\\"\"))
537
538 gives a pair single-quotes, which surround a single character, a SYNTAX of
539 \"\\\"\" (meaning string quote syntax). Single-quote characters in other
540 contexts will not be affected.
541
542 This is normally set via `font-lock-defaults'.")
543
544 (defvar font-lock-syntax-table nil
545 "Non-nil means use this syntax table for fontifying.
546 If this is nil, the major mode's syntax table is used.
547 This is normally set via `font-lock-defaults'.")
548
549 (defvar font-lock-beginning-of-syntax-function nil
550 "*Non-nil means use this function to move back outside all constructs.
551 When called with no args it should move point backward to a place which
552 is not in a string or comment and not within any bracket-pairs (or else,
553 a place such that any bracket-pairs outside it can be ignored for Emacs
554 syntax analysis and fontification).
555
556 If this is nil, Font Lock uses `syntax-begin-function' to move back
557 outside of any comment, string, or sexp. This variable is semi-obsolete;
558 we recommend setting `syntax-begin-function' instead.
559
560 This is normally set via `font-lock-defaults'.")
561
562 (defvar font-lock-mark-block-function nil
563 "*Non-nil means use this function to mark a block of text.
564 When called with no args it should leave point at the beginning of any
565 enclosing textual block and mark at the end.
566 This is normally set via `font-lock-defaults'.")
567
568 (defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
569 "Function to use for fontifying the buffer.
570 This is normally set via `font-lock-defaults'.")
571
572 (defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
573 "Function to use for unfontifying the buffer.
574 This is used when turning off Font Lock mode.
575 This is normally set via `font-lock-defaults'.")
576
577 (defvar font-lock-fontify-region-function 'font-lock-default-fontify-region
578 "Function to use for fontifying a region.
579 It should take two args, the beginning and end of the region, and an optional
580 third arg VERBOSE. If VERBOSE is non-nil, the function should print status
581 messages. This is normally set via `font-lock-defaults'.")
582
583 (defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
584 "Function to use for unfontifying a region.
585 It should take two args, the beginning and end of the region.
586 This is normally set via `font-lock-defaults'.")
587
588 (defvar font-lock-inhibit-thing-lock nil
589 "List of Font Lock mode related modes that should not be turned on.
590 Currently, valid mode names are `fast-lock-mode', `jit-lock-mode' and
591 `lazy-lock-mode'. This is normally set via `font-lock-defaults'.")
592
593 (defvar font-lock-multiline nil
594 "Whether font-lock should cater to multiline keywords.
595 If nil, don't try to handle multiline patterns.
596 If t, always handle multiline patterns.
597 If `undecided', don't try to handle multiline patterns until you see one.
598 Major/minor modes can set this variable if they know which option applies.")
599
600 (defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
601 \f
602 ;; Font Lock mode.
603
604 (eval-when-compile
605 ;;
606 ;; We don't do this at the top-level as we only use non-autoloaded macros.
607 (require 'cl)
608 ;;
609 ;; Borrowed from lazy-lock.el.
610 ;; We use this to preserve or protect things when modifying text properties.
611 (defmacro save-buffer-state (varlist &rest body)
612 "Bind variables according to VARLIST and eval BODY restoring buffer state."
613 (declare (indent 1) (debug let))
614 (let ((modified (make-symbol "modified")))
615 `(let* ,(append varlist
616 `((,modified (buffer-modified-p))
617 (buffer-undo-list t)
618 (inhibit-read-only t)
619 (inhibit-point-motion-hooks t)
620 (inhibit-modification-hooks t)
621 deactivate-mark
622 buffer-file-name
623 buffer-file-truename))
624 (unwind-protect
625 (progn
626 ,@body)
627 (unless ,modified
628 (restore-buffer-modified-p nil))))))
629 ;;
630 ;; Shut up the byte compiler.
631 (defvar font-lock-face-attributes)) ; Obsolete but respected if set.
632
633 (defun font-lock-mode-internal (arg)
634 ;; Turn on Font Lock mode.
635 (when arg
636 (add-hook 'after-change-functions 'font-lock-after-change-function t t)
637 (font-lock-set-defaults)
638 (font-lock-turn-on-thing-lock)
639 ;; Fontify the buffer if we have to.
640 (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size)))
641 (cond (font-lock-fontified
642 nil)
643 ((or (null max-size) (> max-size (buffer-size)))
644 (font-lock-fontify-buffer))
645 (font-lock-verbose
646 (message "Fontifying %s...buffer size greater than font-lock-maximum-size"
647 (buffer-name))))))
648 ;; Turn off Font Lock mode.
649 (unless font-lock-mode
650 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
651 (font-lock-unfontify-buffer)
652 (font-lock-turn-off-thing-lock)))
653
654 (defun font-lock-add-keywords (mode keywords &optional how)
655 "Add highlighting KEYWORDS for MODE.
656
657 MODE should be a symbol, the major mode command name, such as `c-mode'
658 or nil. If nil, highlighting keywords are added for the current buffer.
659 KEYWORDS should be a list; see the variable `font-lock-keywords'.
660 By default they are added at the beginning of the current highlighting list.
661 If optional argument HOW is `set', they are used to replace the current
662 highlighting list. If HOW is any other non-nil value, they are added at the
663 end of the current highlighting list.
664
665 For example:
666
667 (font-lock-add-keywords 'c-mode
668 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
669 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . font-lock-keyword-face)))
670
671 adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
672 comments, and to fontify `and', `or' and `not' words as keywords.
673
674 The above procedure will only add the keywords for C mode, not
675 for modes derived from C mode. To add them for derived modes too,
676 pass nil for MODE and add the call to c-mode-hook.
677
678 For example:
679
680 (add-hook 'c-mode-hook
681 (lambda ()
682 (font-lock-add-keywords nil
683 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
684 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" .
685 font-lock-keyword-face)))))
686
687 The above procedure may fail to add keywords to derived modes if
688 some involved major mode does not follow the standard conventions.
689 File a bug report if this happens, so the major mode can be corrected.
690
691 Note that some modes have specialized support for additional patterns, e.g.,
692 see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
693 `objc-font-lock-extra-types' and `java-font-lock-extra-types'."
694 (cond (mode
695 ;; If MODE is non-nil, add the KEYWORDS and HOW spec to
696 ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
697 (let ((spec (cons keywords how)) cell)
698 (if (setq cell (assq mode font-lock-keywords-alist))
699 (if (eq how 'set)
700 (setcdr cell (list spec))
701 (setcdr cell (append (cdr cell) (list spec))))
702 (push (list mode spec) font-lock-keywords-alist)))
703 ;; Make sure that `font-lock-removed-keywords-alist' does not
704 ;; contain the new keywords.
705 (font-lock-update-removed-keyword-alist mode keywords how))
706 (t
707 (when (and font-lock-mode
708 (not (or font-lock-keywords font-lock-defaults)))
709 ;; The major mode has not set any keywords, so when we enabled
710 ;; font-lock-mode it only enabled the font-core.el part, not the
711 ;; font-lock-mode-internal. Try again.
712 (font-lock-mode -1)
713 (set (make-local-variable 'font-lock-defaults) '(nil t))
714 (font-lock-mode 1))
715 ;; Otherwise set or add the keywords now.
716 ;; This is a no-op if it has been done already in this buffer
717 ;; for the correct major mode.
718 (font-lock-set-defaults)
719 (let ((was-compiled (eq (car font-lock-keywords) t)))
720 ;; Bring back the user-level (uncompiled) keywords.
721 (if was-compiled
722 (setq font-lock-keywords (cadr font-lock-keywords)))
723 ;; Now modify or replace them.
724 (if (eq how 'set)
725 (setq font-lock-keywords keywords)
726 (font-lock-remove-keywords nil keywords) ;to avoid duplicates
727 (let ((old (if (eq (car-safe font-lock-keywords) t)
728 (cdr font-lock-keywords)
729 font-lock-keywords)))
730 (setq font-lock-keywords (if how
731 (append old keywords)
732 (append keywords old)))))
733 ;; If the keywords were compiled before, compile them again.
734 (if was-compiled
735 (setq font-lock-keywords
736 (font-lock-compile-keywords font-lock-keywords)))))))
737
738 (defun font-lock-update-removed-keyword-alist (mode keywords how)
739 "Update `font-lock-removed-keywords-alist' when adding new KEYWORDS to MODE."
740 ;; When font-lock is enabled first all keywords in the list
741 ;; `font-lock-keywords-alist' are added, then all keywords in the
742 ;; list `font-lock-removed-keywords-alist' are removed. If a
743 ;; keyword was once added, removed, and then added again it must be
744 ;; removed from the removed-keywords list. Otherwise the second add
745 ;; will not take effect.
746 (let ((cell (assq mode font-lock-removed-keywords-alist)))
747 (if cell
748 (if (eq how 'set)
749 ;; A new set of keywords is defined. Forget all about
750 ;; our old keywords that should be removed.
751 (setq font-lock-removed-keywords-alist
752 (delq cell font-lock-removed-keywords-alist))
753 ;; Delete all previously removed keywords.
754 (dolist (kword keywords)
755 (setcdr cell (delete kword (cdr cell))))
756 ;; Delete the mode cell if empty.
757 (if (null (cdr cell))
758 (setq font-lock-removed-keywords-alist
759 (delq cell font-lock-removed-keywords-alist)))))))
760
761 ;; Written by Anders Lindgren <andersl@andersl.com>.
762 ;;
763 ;; Case study:
764 ;; (I) The keywords are removed from a major mode.
765 ;; In this case the keyword could be local (i.e. added earlier by
766 ;; `font-lock-add-keywords'), global, or both.
767 ;;
768 ;; (a) In the local case we remove the keywords from the variable
769 ;; `font-lock-keywords-alist'.
770 ;;
771 ;; (b) The actual global keywords are not known at this time.
772 ;; All keywords are added to `font-lock-removed-keywords-alist',
773 ;; when font-lock is enabled those keywords are removed.
774 ;;
775 ;; Note that added keywords are taken out of the list of removed
776 ;; keywords. This ensure correct operation when the same keyword
777 ;; is added and removed several times.
778 ;;
779 ;; (II) The keywords are removed from the current buffer.
780 (defun font-lock-remove-keywords (mode keywords)
781 "Remove highlighting KEYWORDS for MODE.
782
783 MODE should be a symbol, the major mode command name, such as `c-mode'
784 or nil. If nil, highlighting keywords are removed for the current buffer.
785
786 To make the removal apply to modes derived from MODE as well,
787 pass nil for MODE and add the call to MODE-hook. This may fail
788 for some derived modes if some involved major mode does not
789 follow the standard conventions. File a bug report if this
790 happens, so the major mode can be corrected."
791 (cond (mode
792 ;; Remove one keyword at the time.
793 (dolist (keyword keywords)
794 (let ((top-cell (assq mode font-lock-keywords-alist)))
795 ;; If MODE is non-nil, remove the KEYWORD from
796 ;; `font-lock-keywords-alist'.
797 (when top-cell
798 (dolist (keyword-list-how-pair (cdr top-cell))
799 ;; `keywords-list-how-pair' is a cons with a list of
800 ;; keywords in the car top-cell and the original how
801 ;; argument in the cdr top-cell.
802 (setcar keyword-list-how-pair
803 (delete keyword (car keyword-list-how-pair))))
804 ;; Remove keyword list/how pair when the keyword list
805 ;; is empty and how doesn't specify `set'. (If it
806 ;; should be deleted then previously deleted keywords
807 ;; would appear again.)
808 (let ((cell top-cell))
809 (while (cdr cell)
810 (if (and (null (car (car (cdr cell))))
811 (not (eq (cdr (car (cdr cell))) 'set)))
812 (setcdr cell (cdr (cdr cell)))
813 (setq cell (cdr cell)))))
814 ;; Final cleanup, remove major mode cell if last keyword
815 ;; was deleted.
816 (if (null (cdr top-cell))
817 (setq font-lock-keywords-alist
818 (delq top-cell font-lock-keywords-alist))))
819 ;; Remember the keyword in case it is not local.
820 (let ((cell (assq mode font-lock-removed-keywords-alist)))
821 (if cell
822 (unless (member keyword (cdr cell))
823 (nconc cell (list keyword)))
824 (push (cons mode (list keyword))
825 font-lock-removed-keywords-alist))))))
826 (t
827 ;; Otherwise remove it immediately.
828 (font-lock-set-defaults)
829 (let ((was-compiled (eq (car font-lock-keywords) t)))
830 ;; Bring back the user-level (uncompiled) keywords.
831 (if was-compiled
832 (setq font-lock-keywords (cadr font-lock-keywords)))
833
834 ;; Edit them.
835 (setq font-lock-keywords (copy-sequence font-lock-keywords))
836 (dolist (keyword keywords)
837 (setq font-lock-keywords
838 (delete keyword font-lock-keywords)))
839
840 ;; If the keywords were compiled before, compile them again.
841 (if was-compiled
842 (setq font-lock-keywords
843 (font-lock-compile-keywords font-lock-keywords)))))))
844 \f
845 ;;; Font Lock Support mode.
846
847 ;; This is the code used to interface font-lock.el with any of its add-on
848 ;; packages, and provide the user interface. Packages that have their own
849 ;; local buffer fontification functions (see below) may have to call
850 ;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
851 ;; themselves.
852
853 (defcustom font-lock-support-mode 'jit-lock-mode
854 "Support mode for Font Lock mode.
855 Support modes speed up Font Lock mode by being choosy about when fontification
856 occurs. The default support mode, Just-in-time Lock mode (symbol
857 `jit-lock-mode'), is recommended.
858
859 Other, older support modes are Fast Lock mode (symbol `fast-lock-mode') and
860 Lazy Lock mode (symbol `lazy-lock-mode'). See those modes for more info.
861 However, they are no longer recommended, as Just-in-time Lock mode is better.
862
863 If nil, means support for Font Lock mode is never performed.
864 If a symbol, use that support mode.
865 If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
866 where MAJOR-MODE is a symbol or t (meaning the default). For example:
867 ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
868 means that Fast Lock mode is used to support Font Lock mode for buffers in C or
869 C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
870
871 The value of this variable is used when Font Lock mode is turned on."
872 :type '(choice (const :tag "none" nil)
873 (const :tag "fast lock" fast-lock-mode)
874 (const :tag "lazy lock" lazy-lock-mode)
875 (const :tag "jit lock" jit-lock-mode)
876 (repeat :menu-tag "mode specific" :tag "mode specific"
877 :value ((t . jit-lock-mode))
878 (cons :tag "Instance"
879 (radio :tag "Mode"
880 (const :tag "all" t)
881 (symbol :tag "name"))
882 (radio :tag "Support"
883 (const :tag "none" nil)
884 (const :tag "fast lock" fast-lock-mode)
885 (const :tag "lazy lock" lazy-lock-mode)
886 (const :tag "JIT lock" jit-lock-mode)))
887 ))
888 :version "21.1"
889 :group 'font-lock)
890
891 (defvar fast-lock-mode)
892 (defvar lazy-lock-mode)
893 (defvar jit-lock-mode)
894
895 (declare-function fast-lock-after-fontify-buffer "fast-lock")
896 (declare-function fast-lock-after-unfontify-buffer "fast-lock")
897 (declare-function fast-lock-mode "fast-lock")
898 (declare-function lazy-lock-after-fontify-buffer "lazy-lock")
899 (declare-function lazy-lock-after-unfontify-buffer "lazy-lock")
900 (declare-function lazy-lock-mode "lazy-lock")
901
902 (defun font-lock-turn-on-thing-lock ()
903 (let ((thing-mode (font-lock-value-in-major-mode font-lock-support-mode)))
904 (cond ((eq thing-mode 'fast-lock-mode)
905 (fast-lock-mode t))
906 ((eq thing-mode 'lazy-lock-mode)
907 (lazy-lock-mode t))
908 ((eq thing-mode 'jit-lock-mode)
909 ;; Prepare for jit-lock
910 (remove-hook 'after-change-functions
911 'font-lock-after-change-function t)
912 (set (make-local-variable 'font-lock-fontify-buffer-function)
913 'jit-lock-refontify)
914 ;; Don't fontify eagerly (and don't abort if the buffer is large).
915 (set (make-local-variable 'font-lock-fontified) t)
916 ;; Use jit-lock.
917 (jit-lock-register 'font-lock-fontify-region
918 (not font-lock-keywords-only))
919 ;; Tell jit-lock how we extend the region to refontify.
920 (add-hook 'jit-lock-after-change-extend-region-functions
921 'font-lock-extend-jit-lock-region-after-change
922 nil t)))))
923
924 (defun font-lock-turn-off-thing-lock ()
925 (cond ((bound-and-true-p fast-lock-mode)
926 (fast-lock-mode -1))
927 ((bound-and-true-p jit-lock-mode)
928 (jit-lock-unregister 'font-lock-fontify-region)
929 ;; Reset local vars to the non-jit-lock case.
930 (kill-local-variable 'font-lock-fontify-buffer-function))
931 ((bound-and-true-p lazy-lock-mode)
932 (lazy-lock-mode -1))))
933
934 (defun font-lock-after-fontify-buffer ()
935 (cond ((bound-and-true-p fast-lock-mode)
936 (fast-lock-after-fontify-buffer))
937 ;; Useless now that jit-lock intercepts font-lock-fontify-buffer. -sm
938 ;; (jit-lock-mode
939 ;; (jit-lock-after-fontify-buffer))
940 ((bound-and-true-p lazy-lock-mode)
941 (lazy-lock-after-fontify-buffer))))
942
943 (defun font-lock-after-unfontify-buffer ()
944 (cond ((bound-and-true-p fast-lock-mode)
945 (fast-lock-after-unfontify-buffer))
946 ;; Useless as well. It's only called when:
947 ;; - turning off font-lock: it does not matter if we leave spurious
948 ;; `fontified' text props around since jit-lock-mode is also off.
949 ;; - font-lock-default-fontify-buffer fails: this is not run
950 ;; any more anyway. -sm
951 ;;
952 ;; (jit-lock-mode
953 ;; (jit-lock-after-unfontify-buffer))
954 ((bound-and-true-p lazy-lock-mode)
955 (lazy-lock-after-unfontify-buffer))))
956
957 ;;; End of Font Lock Support mode.
958 \f
959 ;;; Fontification functions.
960
961 ;; Rather than the function, e.g., `font-lock-fontify-region' containing the
962 ;; code to fontify a region, the function runs the function whose name is the
963 ;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally,
964 ;; the value of this variable is, e.g., `font-lock-default-fontify-region'
965 ;; which does contain the code to fontify a region. However, the value of the
966 ;; variable could be anything and thus, e.g., `font-lock-fontify-region' could
967 ;; do anything. The indirection of the fontification functions gives major
968 ;; modes the capability of modifying the way font-lock.el fontifies. Major
969 ;; modes can modify the values of, e.g., `font-lock-fontify-region-function',
970 ;; via the variable `font-lock-defaults'.
971 ;;
972 ;; For example, Rmail mode sets the variable `font-lock-defaults' so that
973 ;; font-lock.el uses its own function for buffer fontification. This function
974 ;; makes fontification be on a message-by-message basis and so visiting an
975 ;; RMAIL file is much faster. A clever implementation of the function might
976 ;; fontify the headers differently than the message body. (It should, and
977 ;; correspondingly for Mail mode, but I can't be bothered to do the work. Can
978 ;; you?) This hints at a more interesting use...
979 ;;
980 ;; Languages that contain text normally contained in different major modes
981 ;; could define their own fontification functions that treat text differently
982 ;; depending on its context. For example, Perl mode could arrange that here
983 ;; docs are fontified differently than Perl code. Or Yacc mode could fontify
984 ;; rules one way and C code another. Neat!
985 ;;
986 ;; A further reason to use the fontification indirection feature is when the
987 ;; default syntactual fontification, or the default fontification in general,
988 ;; is not flexible enough for a particular major mode. For example, perhaps
989 ;; comments are just too hairy for `font-lock-fontify-syntactically-region' to
990 ;; cope with. You need to write your own version of that function, e.g.,
991 ;; `hairy-fontify-syntactically-region', and make your own version of
992 ;; `hairy-fontify-region' call that function before calling
993 ;; `font-lock-fontify-keywords-region' for the normal regexp fontification
994 ;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el
995 ;; would call your region fontification function instead of its own. For
996 ;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line
997 ;; directives correctly and cleanly. (It is the same problem as fontifying
998 ;; multi-line strings and comments; regexps are not appropriate for the job.)
999
1000 (defvar font-lock-extend-after-change-region-function nil
1001 "A function that determines the region to refontify after a change.
1002
1003 This variable is either nil, or is a function that determines the
1004 region to refontify after a change.
1005 It is usually set by the major mode via `font-lock-defaults'.
1006 Font-lock calls this function after each buffer change.
1007
1008 The function is given three parameters, the standard BEG, END, and OLD-LEN
1009 from `after-change-functions'. It should return either a cons of the beginning
1010 and end buffer positions \(in that order) of the region to refontify, or nil
1011 \(which directs the caller to fontify a default region).
1012 This function should preserve the match-data.
1013 The region it returns may start or end in the middle of a line.")
1014 (make-variable-buffer-local 'font-lock-extend-after-change-region-function)
1015
1016 (defun font-lock-fontify-buffer ()
1017 "Fontify the current buffer the way the function `font-lock-mode' would."
1018 (interactive)
1019 (font-lock-set-defaults)
1020 (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
1021 (funcall font-lock-fontify-buffer-function)))
1022
1023 (defun font-lock-unfontify-buffer ()
1024 (funcall font-lock-unfontify-buffer-function))
1025
1026 (defun font-lock-fontify-region (beg end &optional loudly)
1027 (font-lock-set-defaults)
1028 (funcall font-lock-fontify-region-function beg end loudly))
1029
1030 (defun font-lock-unfontify-region (beg end)
1031 (save-buffer-state nil
1032 (funcall font-lock-unfontify-region-function beg end)))
1033
1034 (defun font-lock-default-fontify-buffer ()
1035 (let ((verbose (if (numberp font-lock-verbose)
1036 (> (buffer-size) font-lock-verbose)
1037 font-lock-verbose)))
1038 (with-temp-message
1039 (when verbose
1040 (format "Fontifying %s..." (buffer-name)))
1041 ;; Make sure we fontify etc. in the whole buffer.
1042 (save-restriction
1043 (widen)
1044 (condition-case nil
1045 (save-excursion
1046 (save-match-data
1047 (font-lock-fontify-region (point-min) (point-max) verbose)
1048 (font-lock-after-fontify-buffer)
1049 (setq font-lock-fontified t)))
1050 ;; We don't restore the old fontification, so it's best to unfontify.
1051 (quit (font-lock-unfontify-buffer)))))))
1052
1053 (defun font-lock-default-unfontify-buffer ()
1054 ;; Make sure we unfontify etc. in the whole buffer.
1055 (save-restriction
1056 (widen)
1057 (font-lock-unfontify-region (point-min) (point-max))
1058 (font-lock-after-unfontify-buffer)
1059 (setq font-lock-fontified nil)))
1060
1061 (defvar font-lock-dont-widen nil
1062 "If non-nil, font-lock will work on the non-widened buffer.
1063 Useful for things like RMAIL and Info where the whole buffer is not
1064 a very meaningful entity to highlight.")
1065
1066
1067 (defvar font-lock-beg) (defvar font-lock-end)
1068 (defvar font-lock-extend-region-functions
1069 '(font-lock-extend-region-wholelines
1070 ;; This use of font-lock-multiline property is unreliable but is just
1071 ;; a handy heuristic: in case you don't have a function that does
1072 ;; /identification/ of multiline elements, you may still occasionally
1073 ;; discover them by accident (or you may /identify/ them but not in all
1074 ;; cases), in which case the font-lock-multiline property can help make
1075 ;; sure you will properly *re*identify them during refontification.
1076 font-lock-extend-region-multiline)
1077 "Special hook run just before proceeding to fontify a region.
1078 This is used to allow major modes to help font-lock find safe buffer positions
1079 as beginning and end of the fontified region. Its most common use is to solve
1080 the problem of /identification/ of multiline elements by providing a function
1081 that tries to find such elements and move the boundaries such that they do
1082 not fall in the middle of one.
1083 Each function is called with no argument; it is expected to adjust the
1084 dynamically bound variables `font-lock-beg' and `font-lock-end'; and return
1085 non-nil if it did make such an adjustment.
1086 These functions are run in turn repeatedly until they all return nil.
1087 Put first the functions more likely to cause a change and cheaper to compute.")
1088 ;; Mark it as a special hook which doesn't use any global setting
1089 ;; (i.e. doesn't obey the element t in the buffer-local value).
1090 (make-variable-buffer-local 'font-lock-extend-region-functions)
1091
1092 (defun font-lock-extend-region-multiline ()
1093 "Move fontification boundaries away from any `font-lock-multiline' property."
1094 (let ((changed nil))
1095 (when (and (> font-lock-beg (point-min))
1096 (get-text-property (1- font-lock-beg) 'font-lock-multiline))
1097 (setq changed t)
1098 (setq font-lock-beg (or (previous-single-property-change
1099 font-lock-beg 'font-lock-multiline)
1100 (point-min))))
1101 ;;
1102 (when (get-text-property font-lock-end 'font-lock-multiline)
1103 (setq changed t)
1104 (setq font-lock-end (or (text-property-any font-lock-end (point-max)
1105 'font-lock-multiline nil)
1106 (point-max))))
1107 changed))
1108
1109 (defun font-lock-extend-region-wholelines ()
1110 "Move fontification boundaries to beginning of lines."
1111 (let ((changed nil))
1112 (goto-char font-lock-beg)
1113 (unless (bolp)
1114 (setq changed t font-lock-beg (line-beginning-position)))
1115 (goto-char font-lock-end)
1116 (unless (bolp)
1117 (unless (eq font-lock-end
1118 (setq font-lock-end (line-beginning-position 2)))
1119 (setq changed t)))
1120 changed))
1121
1122 (defun font-lock-default-fontify-region (beg end loudly)
1123 (save-buffer-state
1124 ((parse-sexp-lookup-properties
1125 (or parse-sexp-lookup-properties font-lock-syntactic-keywords))
1126 (old-syntax-table (syntax-table)))
1127 (unwind-protect
1128 (save-restriction
1129 (unless font-lock-dont-widen (widen))
1130 ;; Use the fontification syntax table, if any.
1131 (when font-lock-syntax-table
1132 (set-syntax-table font-lock-syntax-table))
1133 ;; Extend the region to fontify so that it starts and ends at
1134 ;; safe places.
1135 (let ((funs font-lock-extend-region-functions)
1136 (font-lock-beg beg)
1137 (font-lock-end end))
1138 (while funs
1139 (setq funs (if (or (not (funcall (car funs)))
1140 (eq funs font-lock-extend-region-functions))
1141 (cdr funs)
1142 ;; If there's been a change, we should go through
1143 ;; the list again since this new position may
1144 ;; warrant a different answer from one of the fun
1145 ;; we've already seen.
1146 font-lock-extend-region-functions)))
1147 (setq beg font-lock-beg end font-lock-end))
1148 ;; Now do the fontification.
1149 (font-lock-unfontify-region beg end)
1150 (when font-lock-syntactic-keywords
1151 (font-lock-fontify-syntactic-keywords-region beg end))
1152 (unless font-lock-keywords-only
1153 (font-lock-fontify-syntactically-region beg end loudly))
1154 (font-lock-fontify-keywords-region beg end loudly))
1155 ;; Clean up.
1156 (set-syntax-table old-syntax-table))))
1157
1158 ;; The following must be rethought, since keywords can override fontification.
1159 ;; ;; Now scan for keywords, but not if we are inside a comment now.
1160 ;; (or (and (not font-lock-keywords-only)
1161 ;; (let ((state (parse-partial-sexp beg end nil nil
1162 ;; font-lock-cache-state)))
1163 ;; (or (nth 4 state) (nth 7 state))))
1164 ;; (font-lock-fontify-keywords-region beg end))
1165
1166 (defvar font-lock-extra-managed-props nil
1167 "Additional text properties managed by font-lock.
1168 This is used by `font-lock-default-unfontify-region' to decide
1169 what properties to clear before refontifying a region.")
1170
1171 (defun font-lock-default-unfontify-region (beg end)
1172 (remove-list-of-text-properties
1173 beg end (append
1174 font-lock-extra-managed-props
1175 (if font-lock-syntactic-keywords
1176 '(syntax-table face font-lock-multiline)
1177 '(face font-lock-multiline)))))
1178
1179 ;; Called when any modification is made to buffer text.
1180 (defun font-lock-after-change-function (beg end old-len)
1181 (save-excursion
1182 (let ((inhibit-point-motion-hooks t)
1183 (inhibit-quit t)
1184 (region (if font-lock-extend-after-change-region-function
1185 (funcall font-lock-extend-after-change-region-function
1186 beg end old-len))))
1187 (save-match-data
1188 (if region
1189 ;; Fontify the region the major mode has specified.
1190 (setq beg (car region) end (cdr region))
1191 ;; Fontify the whole lines which enclose the region.
1192 ;; Actually, this is not needed because
1193 ;; font-lock-default-fontify-region already rounds up to a whole
1194 ;; number of lines.
1195 ;; (setq beg (progn (goto-char beg) (line-beginning-position))
1196 ;; end (progn (goto-char end) (line-beginning-position 2)))
1197 (unless (eq end (point-max))
1198 ;; Rounding up to a whole number of lines should include the
1199 ;; line right after `end'. Typical case: the first char of
1200 ;; the line was deleted. Or a \n was inserted in the middle
1201 ;; of a line.
1202 (setq end (1+ end))))
1203 (font-lock-fontify-region beg end)))))
1204
1205 (defvar jit-lock-start) (defvar jit-lock-end)
1206 (defun font-lock-extend-jit-lock-region-after-change (beg end old-len)
1207 "Function meant for `jit-lock-after-change-extend-region-functions'.
1208 This function does 2 things:
1209 - extend the region so that it not only includes the part that was modified
1210 but also the surrounding text whose highlighting may change as a consequence.
1211 - anticipate (part of) the region extension that will happen later in
1212 `font-lock-default-fontify-region', in order to avoid the need for
1213 double-redisplay in `jit-lock-fontify-now'."
1214 (save-excursion
1215 ;; First extend the region as font-lock-after-change-function would.
1216 (let ((region (if font-lock-extend-after-change-region-function
1217 (funcall font-lock-extend-after-change-region-function
1218 beg end old-len))))
1219 (if region
1220 (setq beg (min jit-lock-start (car region))
1221 end (max jit-lock-end (cdr region))))
1222 ;; Then extend the region obeying font-lock-multiline properties,
1223 ;; indicating which part of the buffer needs to be refontified.
1224 ;; !!! This is the *main* user of font-lock-multiline property !!!
1225 ;; font-lock-after-change-function could/should also do that, but it
1226 ;; doesn't need to because font-lock-default-fontify-region does
1227 ;; it anyway. Here OTOH we have no guarantee that
1228 ;; font-lock-default-fontify-region will be executed on this region
1229 ;; any time soon.
1230 ;; Note: contrary to font-lock-default-fontify-region, we do not do
1231 ;; any loop here because we are not looking for a safe spot: we just
1232 ;; mark the text whose appearance may need to change as a result of
1233 ;; the buffer modification.
1234 (when (and (> beg (point-min))
1235 (get-text-property (1- beg) 'font-lock-multiline))
1236 (setq beg (or (previous-single-property-change
1237 beg 'font-lock-multiline)
1238 (point-min))))
1239 (when (< end (point-max))
1240 (setq end
1241 (if (get-text-property end 'font-lock-multiline)
1242 (or (text-property-any end (point-max)
1243 'font-lock-multiline nil)
1244 (point-max))
1245 ;; Rounding up to a whole number of lines should include the
1246 ;; line right after `end'. Typical case: the first char of
1247 ;; the line was deleted. Or a \n was inserted in the middle
1248 ;; of a line.
1249 (1+ end))))
1250 ;; Finally, pre-enlarge the region to a whole number of lines, to try
1251 ;; and anticipate what font-lock-default-fontify-region will do, so as to
1252 ;; avoid double-redisplay.
1253 ;; We could just run `font-lock-extend-region-functions', but since
1254 ;; the only purpose is to avoid the double-redisplay, we prefer to
1255 ;; do here only the part that is cheap and most likely to be useful.
1256 (when (memq 'font-lock-extend-region-wholelines
1257 font-lock-extend-region-functions)
1258 (goto-char beg)
1259 (setq jit-lock-start (min jit-lock-start (line-beginning-position)))
1260 (goto-char end)
1261 (setq jit-lock-end
1262 (max jit-lock-end
1263 (if (bolp) (point) (line-beginning-position 2))))))))
1264
1265 (defun font-lock-fontify-block (&optional arg)
1266 "Fontify some lines the way `font-lock-fontify-buffer' would.
1267 The lines could be a function or paragraph, or a specified number of lines.
1268 If ARG is given, fontify that many lines before and after point, or 16 lines if
1269 no ARG is given and `font-lock-mark-block-function' is nil.
1270 If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
1271 delimit the region to fontify."
1272 (interactive "P")
1273 (let ((inhibit-point-motion-hooks t) font-lock-beginning-of-syntax-function
1274 deactivate-mark)
1275 ;; Make sure we have the right `font-lock-keywords' etc.
1276 (if (not font-lock-mode) (font-lock-set-defaults))
1277 (save-excursion
1278 (save-match-data
1279 (condition-case error-data
1280 (if (or arg (not font-lock-mark-block-function))
1281 (let ((lines (if arg (prefix-numeric-value arg) 16)))
1282 (font-lock-fontify-region
1283 (save-excursion (forward-line (- lines)) (point))
1284 (save-excursion (forward-line lines) (point))))
1285 (funcall font-lock-mark-block-function)
1286 (font-lock-fontify-region (point) (mark)))
1287 ((error quit) (message "Fontifying block...%s" error-data)))))))
1288
1289 ;;; End of Fontification functions.
1290 \f
1291 ;;; Additional text property functions.
1292
1293 ;; The following text property functions should be builtins. This means they
1294 ;; should be written in C and put with all the other text property functions.
1295 ;; In the meantime, those that are used by font-lock.el are defined in Lisp
1296 ;; below and given a `font-lock-' prefix. Those that are not used are defined
1297 ;; in Lisp below and commented out. sm.
1298
1299 (defun font-lock-prepend-text-property (start end prop value &optional object)
1300 "Prepend to one property of the text from START to END.
1301 Arguments PROP and VALUE specify the property and value to prepend to the value
1302 already in place. The resulting property values are always lists.
1303 Optional argument OBJECT is the string or buffer containing the text."
1304 (let ((val (if (listp value) value (list value))) next prev)
1305 (while (/= start end)
1306 (setq next (next-single-property-change start prop object end)
1307 prev (get-text-property start prop object))
1308 ;; Canonicalize old forms of face property.
1309 (and (memq prop '(face font-lock-face))
1310 (listp prev)
1311 (or (keywordp (car prev))
1312 (memq (car prev) '(foreground-color background-color)))
1313 (setq prev (list prev)))
1314 (put-text-property start next prop
1315 (append val (if (listp prev) prev (list prev)))
1316 object)
1317 (setq start next))))
1318
1319 (defun font-lock-append-text-property (start end prop value &optional object)
1320 "Append to one property of the text from START to END.
1321 Arguments PROP and VALUE specify the property and value to append to the value
1322 already in place. The resulting property values are always lists.
1323 Optional argument OBJECT is the string or buffer containing the text."
1324 (let ((val (if (listp value) value (list value))) next prev)
1325 (while (/= start end)
1326 (setq next (next-single-property-change start prop object end)
1327 prev (get-text-property start prop object))
1328 ;; Canonicalize old forms of face property.
1329 (and (memq prop '(face font-lock-face))
1330 (listp prev)
1331 (or (keywordp (car prev))
1332 (memq (car prev) '(foreground-color background-color)))
1333 (setq prev (list prev)))
1334 (put-text-property start next prop
1335 (append (if (listp prev) prev (list prev)) val)
1336 object)
1337 (setq start next))))
1338
1339 (defun font-lock-fillin-text-property (start end prop value &optional object)
1340 "Fill in one property of the text from START to END.
1341 Arguments PROP and VALUE specify the property and value to put where none are
1342 already in place. Therefore existing property values are not overwritten.
1343 Optional argument OBJECT is the string or buffer containing the text."
1344 (let ((start (text-property-any start end prop nil object)) next)
1345 (while start
1346 (setq next (next-single-property-change start prop object end))
1347 (put-text-property start next prop value object)
1348 (setq start (text-property-any next end prop nil object)))))
1349
1350 ;; For completeness: this is to `remove-text-properties' as `put-text-property'
1351 ;; is to `add-text-properties', etc.
1352 ;;(defun remove-text-property (start end property &optional object)
1353 ;; "Remove a property from text from START to END.
1354 ;;Argument PROPERTY is the property to remove.
1355 ;;Optional argument OBJECT is the string or buffer containing the text.
1356 ;;Return t if the property was actually removed, nil otherwise."
1357 ;; (remove-text-properties start end (list property) object))
1358
1359 ;; For consistency: maybe this should be called `remove-single-property' like
1360 ;; `next-single-property-change' (not `next-single-text-property-change'), etc.
1361 ;;(defun remove-single-text-property (start end prop value &optional object)
1362 ;; "Remove a specific property value from text from START to END.
1363 ;;Arguments PROP and VALUE specify the property and value to remove. The
1364 ;;resulting property values are not equal to VALUE nor lists containing VALUE.
1365 ;;Optional argument OBJECT is the string or buffer containing the text."
1366 ;; (let ((start (text-property-not-all start end prop nil object)) next prev)
1367 ;; (while start
1368 ;; (setq next (next-single-property-change start prop object end)
1369 ;; prev (get-text-property start prop object))
1370 ;; (cond ((and (symbolp prev) (eq value prev))
1371 ;; (remove-text-property start next prop object))
1372 ;; ((and (listp prev) (memq value prev))
1373 ;; (let ((new (delq value prev)))
1374 ;; (cond ((null new)
1375 ;; (remove-text-property start next prop object))
1376 ;; ((= (length new) 1)
1377 ;; (put-text-property start next prop (car new) object))
1378 ;; (t
1379 ;; (put-text-property start next prop new object))))))
1380 ;; (setq start (text-property-not-all next end prop nil object)))))
1381
1382 ;;; End of Additional text property functions.
1383 \f
1384 ;;; Syntactic regexp fontification functions.
1385
1386 ;; These syntactic keyword pass functions are identical to those keyword pass
1387 ;; functions below, with the following exceptions; (a) they operate on
1388 ;; `font-lock-syntactic-keywords' of course, (b) they are all `defun' as speed
1389 ;; is less of an issue, (c) eval of property value does not occur JIT as speed
1390 ;; is less of an issue, (d) OVERRIDE cannot be `prepend' or `append' as it
1391 ;; makes no sense for `syntax-table' property values, (e) they do not do it
1392 ;; LOUDLY as it is not likely to be intensive.
1393
1394 (defun font-lock-apply-syntactic-highlight (highlight)
1395 "Apply HIGHLIGHT following a match.
1396 HIGHLIGHT should be of the form MATCH-HIGHLIGHT,
1397 see `font-lock-syntactic-keywords'."
1398 (let* ((match (nth 0 highlight))
1399 (start (match-beginning match)) (end (match-end match))
1400 (value (nth 1 highlight))
1401 (override (nth 2 highlight)))
1402 (if (not start)
1403 ;; No match but we might not signal an error.
1404 (or (nth 3 highlight)
1405 (error "No match %d in highlight %S" match highlight))
1406 (when (and (consp value) (not (numberp (car value))))
1407 (setq value (eval value)))
1408 (when (stringp value) (setq value (string-to-syntax value)))
1409 ;; Flush the syntax-cache. I believe this is not necessary for
1410 ;; font-lock's use of syntax-ppss, but I'm not 100% sure and it can
1411 ;; still be necessary for other users of syntax-ppss anyway.
1412 (syntax-ppss-after-change-function start)
1413 (cond
1414 ((not override)
1415 ;; Cannot override existing fontification.
1416 (or (text-property-not-all start end 'syntax-table nil)
1417 (put-text-property start end 'syntax-table value)))
1418 ((eq override t)
1419 ;; Override existing fontification.
1420 (put-text-property start end 'syntax-table value))
1421 ((eq override 'keep)
1422 ;; Keep existing fontification.
1423 (font-lock-fillin-text-property start end 'syntax-table value))))))
1424
1425 (defun font-lock-fontify-syntactic-anchored-keywords (keywords limit)
1426 "Fontify according to KEYWORDS until LIMIT.
1427 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1428 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1429 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1430 ;; Evaluate PRE-MATCH-FORM.
1431 (pre-match-value (eval (nth 1 keywords))))
1432 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1433 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1434 (setq limit pre-match-value)
1435 (setq limit (line-end-position)))
1436 (save-match-data
1437 ;; Find an occurrence of `matcher' before `limit'.
1438 (while (if (stringp matcher)
1439 (re-search-forward matcher limit t)
1440 (funcall matcher limit))
1441 ;; Apply each highlight to this instance of `matcher'.
1442 (setq highlights lowdarks)
1443 (while highlights
1444 (font-lock-apply-syntactic-highlight (car highlights))
1445 (setq highlights (cdr highlights)))))
1446 ;; Evaluate POST-MATCH-FORM.
1447 (eval (nth 2 keywords))))
1448
1449 (defun font-lock-fontify-syntactic-keywords-region (start end)
1450 "Fontify according to `font-lock-syntactic-keywords' between START and END.
1451 START should be at the beginning of a line."
1452 ;; Ensure the beginning of the file is properly syntactic-fontified.
1453 (when (and font-lock-syntactically-fontified
1454 (< font-lock-syntactically-fontified start))
1455 (setq start (max font-lock-syntactically-fontified (point-min)))
1456 (setq font-lock-syntactically-fontified end))
1457 ;; If `font-lock-syntactic-keywords' is a symbol, get the real keywords.
1458 (when (symbolp font-lock-syntactic-keywords)
1459 (setq font-lock-syntactic-keywords (font-lock-eval-keywords
1460 font-lock-syntactic-keywords)))
1461 ;; If `font-lock-syntactic-keywords' is not compiled, compile it.
1462 (unless (eq (car font-lock-syntactic-keywords) t)
1463 (setq font-lock-syntactic-keywords (font-lock-compile-keywords
1464 font-lock-syntactic-keywords
1465 t)))
1466 ;; Get down to business.
1467 (let ((case-fold-search font-lock-keywords-case-fold-search)
1468 (keywords (cddr font-lock-syntactic-keywords))
1469 keyword matcher highlights)
1470 (while keywords
1471 ;; Find an occurrence of `matcher' from `start' to `end'.
1472 (setq keyword (car keywords) matcher (car keyword))
1473 (goto-char start)
1474 (while (if (stringp matcher)
1475 (re-search-forward matcher end t)
1476 (funcall matcher end))
1477 ;; Apply each highlight to this instance of `matcher', which may be
1478 ;; specific highlights or more keywords anchored to `matcher'.
1479 (setq highlights (cdr keyword))
1480 (while highlights
1481 (if (numberp (car (car highlights)))
1482 (font-lock-apply-syntactic-highlight (car highlights))
1483 (font-lock-fontify-syntactic-anchored-keywords (car highlights)
1484 end))
1485 (setq highlights (cdr highlights))))
1486 (setq keywords (cdr keywords)))))
1487
1488 ;;; End of Syntactic regexp fontification functions.
1489 \f
1490 ;;; Syntactic fontification functions.
1491
1492 (defvar font-lock-comment-start-skip nil
1493 "If non-nil, Font Lock mode uses this instead of `comment-start-skip'.")
1494
1495 (defvar font-lock-comment-end-skip nil
1496 "If non-nil, Font Lock mode uses this instead of `comment-end'.")
1497
1498 (defun font-lock-fontify-syntactically-region (start end &optional loudly ppss)
1499 "Put proper face on each string and comment between START and END.
1500 START should be at the beginning of a line."
1501 (let ((comment-end-regexp
1502 (or font-lock-comment-end-skip
1503 (regexp-quote
1504 (replace-regexp-in-string "^ *" "" comment-end))))
1505 state face beg)
1506 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
1507 (goto-char start)
1508 ;;
1509 ;; Find the `start' state.
1510 (setq state (or ppss (syntax-ppss start)))
1511 ;;
1512 ;; Find each interesting place between here and `end'.
1513 (while
1514 (progn
1515 (when (or (nth 3 state) (nth 4 state))
1516 (setq face (funcall font-lock-syntactic-face-function state))
1517 (setq beg (max (nth 8 state) start))
1518 (setq state (parse-partial-sexp (point) end nil nil state
1519 'syntax-table))
1520 (when face (put-text-property beg (point) 'face face))
1521 (when (and (eq face 'font-lock-comment-face)
1522 (or font-lock-comment-start-skip
1523 comment-start-skip))
1524 ;; Find the comment delimiters
1525 ;; and use font-lock-comment-delimiter-face for them.
1526 (save-excursion
1527 (goto-char beg)
1528 (if (looking-at (or font-lock-comment-start-skip
1529 comment-start-skip))
1530 (put-text-property beg (match-end 0) 'face
1531 font-lock-comment-delimiter-face)))
1532 (if (looking-back comment-end-regexp (point-at-bol) t)
1533 (put-text-property (match-beginning 0) (point) 'face
1534 font-lock-comment-delimiter-face))))
1535 (< (point) end))
1536 (setq state (parse-partial-sexp (point) end nil nil state
1537 'syntax-table)))))
1538
1539 ;;; End of Syntactic fontification functions.
1540 \f
1541 ;;; Keyword regexp fontification functions.
1542
1543 (defsubst font-lock-apply-highlight (highlight)
1544 "Apply HIGHLIGHT following a match.
1545 HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
1546 (let* ((match (nth 0 highlight))
1547 (start (match-beginning match)) (end (match-end match))
1548 (override (nth 2 highlight)))
1549 (if (not start)
1550 ;; No match but we might not signal an error.
1551 (or (nth 3 highlight)
1552 (error "No match %d in highlight %S" match highlight))
1553 (let ((val (eval (nth 1 highlight))))
1554 (when (eq (car-safe val) 'face)
1555 (add-text-properties start end (cddr val))
1556 (setq val (cadr val)))
1557 (cond
1558 ((not (or val (eq override t)))
1559 ;; If `val' is nil, don't do anything. It is important to do it
1560 ;; explicitly, because when adding nil via things like
1561 ;; font-lock-append-text-property, the property is actually
1562 ;; changed from <face> to (<face>) which is undesirable. --Stef
1563 nil)
1564 ((not override)
1565 ;; Cannot override existing fontification.
1566 (or (text-property-not-all start end 'face nil)
1567 (put-text-property start end 'face val)))
1568 ((eq override t)
1569 ;; Override existing fontification.
1570 (put-text-property start end 'face val))
1571 ((eq override 'prepend)
1572 ;; Prepend to existing fontification.
1573 (font-lock-prepend-text-property start end 'face val))
1574 ((eq override 'append)
1575 ;; Append to existing fontification.
1576 (font-lock-append-text-property start end 'face val))
1577 ((eq override 'keep)
1578 ;; Keep existing fontification.
1579 (font-lock-fillin-text-property start end 'face val)))))))
1580
1581 (defsubst font-lock-fontify-anchored-keywords (keywords limit)
1582 "Fontify according to KEYWORDS until LIMIT.
1583 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1584 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1585 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1586 (lead-start (match-beginning 0))
1587 ;; Evaluate PRE-MATCH-FORM.
1588 (pre-match-value (eval (nth 1 keywords))))
1589 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1590 (if (not (and (numberp pre-match-value) (> pre-match-value (point))))
1591 (setq limit (line-end-position))
1592 (setq limit pre-match-value)
1593 (when (and font-lock-multiline (>= limit (line-beginning-position 2)))
1594 ;; this is a multiline anchored match
1595 ;; (setq font-lock-multiline t)
1596 (put-text-property (if (= limit (line-beginning-position 2))
1597 (1- limit)
1598 (min lead-start (point)))
1599 limit
1600 'font-lock-multiline t)))
1601 (save-match-data
1602 ;; Find an occurrence of `matcher' before `limit'.
1603 (while (and (< (point) limit)
1604 (if (stringp matcher)
1605 (re-search-forward matcher limit t)
1606 (funcall matcher limit)))
1607 ;; Apply each highlight to this instance of `matcher'.
1608 (setq highlights lowdarks)
1609 (while highlights
1610 (font-lock-apply-highlight (car highlights))
1611 (setq highlights (cdr highlights)))))
1612 ;; Evaluate POST-MATCH-FORM.
1613 (eval (nth 2 keywords))))
1614
1615 (defun font-lock-fontify-keywords-region (start end &optional loudly)
1616 "Fontify according to `font-lock-keywords' between START and END.
1617 START should be at the beginning of a line.
1618 LOUDLY, if non-nil, allows progress-meter bar."
1619 (unless (eq (car font-lock-keywords) t)
1620 (setq font-lock-keywords
1621 (font-lock-compile-keywords font-lock-keywords)))
1622 (let ((case-fold-search font-lock-keywords-case-fold-search)
1623 (keywords (cddr font-lock-keywords))
1624 (bufname (buffer-name)) (count 0)
1625 (pos (make-marker))
1626 keyword matcher highlights)
1627 ;;
1628 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1629 (while keywords
1630 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
1631 (make-string (incf count) ?.)))
1632 ;;
1633 ;; Find an occurrence of `matcher' from `start' to `end'.
1634 (setq keyword (car keywords) matcher (car keyword))
1635 (goto-char start)
1636 (while (and (< (point) end)
1637 (if (stringp matcher)
1638 (re-search-forward matcher end t)
1639 (funcall matcher end))
1640 ;; Beware empty string matches since they will
1641 ;; loop indefinitely.
1642 (or (> (point) (match-beginning 0))
1643 (progn (forward-char 1) t)))
1644 (when (and font-lock-multiline
1645 (>= (point)
1646 (save-excursion (goto-char (match-beginning 0))
1647 (forward-line 1) (point))))
1648 ;; this is a multiline regexp match
1649 ;; (setq font-lock-multiline t)
1650 (put-text-property (if (= (point)
1651 (save-excursion
1652 (goto-char (match-beginning 0))
1653 (forward-line 1) (point)))
1654 (1- (point))
1655 (match-beginning 0))
1656 (point)
1657 'font-lock-multiline t))
1658 ;; Apply each highlight to this instance of `matcher', which may be
1659 ;; specific highlights or more keywords anchored to `matcher'.
1660 (setq highlights (cdr keyword))
1661 (while highlights
1662 (if (numberp (car (car highlights)))
1663 (font-lock-apply-highlight (car highlights))
1664 (set-marker pos (point))
1665 (font-lock-fontify-anchored-keywords (car highlights) end)
1666 ;; Ensure forward progress. `pos' is a marker because anchored
1667 ;; keyword may add/delete text (this happens e.g. in grep.el).
1668 (if (< (point) pos) (goto-char pos)))
1669 (setq highlights (cdr highlights))))
1670 (setq keywords (cdr keywords)))
1671 (set-marker pos nil)))
1672
1673 ;;; End of Keyword regexp fontification functions.
1674 \f
1675 ;; Various functions.
1676
1677 (defun font-lock-compile-keywords (keywords &optional syntactic-keywords)
1678 "Compile KEYWORDS into the form (t KEYWORDS COMPILED...)
1679 Here each COMPILED is of the form (MATCHER HIGHLIGHT ...) as shown in the
1680 `font-lock-keywords' doc string.
1681 If SYNTACTIC-KEYWORDS is non-nil, it means these keywords are used for
1682 `font-lock-syntactic-keywords' rather than for `font-lock-keywords'."
1683 (if (not font-lock-set-defaults)
1684 ;; This should never happen. But some external packages sometimes
1685 ;; call font-lock in unexpected and incorrect ways. It's important to
1686 ;; stop processing at this point, otherwise we may end up changing the
1687 ;; global value of font-lock-keywords and break highlighting in many
1688 ;; other buffers.
1689 (error "Font-lock trying to use keywords before setting them up"))
1690 (if (eq (car-safe keywords) t)
1691 keywords
1692 (setq keywords
1693 (cons t (cons keywords
1694 (mapcar 'font-lock-compile-keyword keywords))))
1695 (if (and (not syntactic-keywords)
1696 (let ((beg-function
1697 (or font-lock-beginning-of-syntax-function
1698 syntax-begin-function)))
1699 (or (eq beg-function 'beginning-of-defun)
1700 (get beg-function 'font-lock-syntax-paren-check)))
1701 (not beginning-of-defun-function))
1702 ;; Try to detect when a string or comment contains something that
1703 ;; looks like a defun and would thus confuse font-lock.
1704 (nconc keywords
1705 `((,(if defun-prompt-regexp
1706 (concat "^\\(?:" defun-prompt-regexp "\\)?\\s(")
1707 "^\\s(")
1708 (0
1709 (if (memq (get-text-property (match-beginning 0) 'face)
1710 '(font-lock-string-face font-lock-doc-face
1711 font-lock-comment-face))
1712 (list 'face font-lock-warning-face
1713 'help-echo "Looks like a toplevel defun: escape the parenthesis"))
1714 prepend)))))
1715 keywords))
1716
1717 (defun font-lock-compile-keyword (keyword)
1718 (cond ((nlistp keyword) ; MATCHER
1719 (list keyword '(0 font-lock-keyword-face)))
1720 ((eq (car keyword) 'eval) ; (eval . FORM)
1721 (font-lock-compile-keyword (eval (cdr keyword))))
1722 ((eq (car-safe (cdr keyword)) 'quote) ; (MATCHER . 'FORM)
1723 ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote.
1724 (if (symbolp (nth 2 keyword))
1725 (list (car keyword) (list 0 (cdr keyword)))
1726 (font-lock-compile-keyword (cons (car keyword) (nth 2 keyword)))))
1727 ((numberp (cdr keyword)) ; (MATCHER . MATCH)
1728 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
1729 ((symbolp (cdr keyword)) ; (MATCHER . FACENAME)
1730 (list (car keyword) (list 0 (cdr keyword))))
1731 ((nlistp (nth 1 keyword)) ; (MATCHER . HIGHLIGHT)
1732 (list (car keyword) (cdr keyword)))
1733 (t ; (MATCHER HIGHLIGHT ...)
1734 keyword)))
1735
1736 (defun font-lock-eval-keywords (keywords)
1737 "Evalulate KEYWORDS if a function (funcall) or variable (eval) name."
1738 (if (listp keywords)
1739 keywords
1740 (font-lock-eval-keywords (if (fboundp keywords)
1741 (funcall keywords)
1742 (eval keywords)))))
1743
1744 (defun font-lock-value-in-major-mode (alist)
1745 "Return value in ALIST for `major-mode', or ALIST if it is not an alist.
1746 Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t."
1747 (if (consp alist)
1748 (cdr (or (assq major-mode alist) (assq t alist)))
1749 alist))
1750
1751 (defun font-lock-choose-keywords (keywords level)
1752 "Return LEVELth element of KEYWORDS.
1753 A LEVEL of nil is equal to a LEVEL of 0, a LEVEL of t is equal to
1754 \(1- (length KEYWORDS))."
1755 (cond ((not (and (listp keywords) (symbolp (car keywords))))
1756 keywords)
1757 ((numberp level)
1758 (or (nth level keywords) (car (last keywords))))
1759 ((eq level t)
1760 (car (last keywords)))
1761 (t
1762 (car keywords))))
1763
1764 (defvar font-lock-set-defaults nil) ; Whether we have set up defaults.
1765
1766 (defvar font-lock-mode-major-mode)
1767 (defun font-lock-set-defaults ()
1768 "Set fontification defaults appropriately for this mode.
1769 Sets various variables using `font-lock-defaults' (or, if nil, using
1770 `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
1771 ;; Set fontification defaults if not previously set for correct major mode.
1772 (unless (and font-lock-set-defaults
1773 (eq font-lock-mode-major-mode major-mode))
1774 (setq font-lock-mode-major-mode major-mode)
1775 (set (make-local-variable 'font-lock-set-defaults) t)
1776 (make-local-variable 'font-lock-fontified)
1777 (make-local-variable 'font-lock-multiline)
1778 (let* ((defaults (or font-lock-defaults
1779 (cdr (assq major-mode
1780 (with-no-warnings
1781 font-lock-defaults-alist)))))
1782 (keywords
1783 (font-lock-choose-keywords (nth 0 defaults)
1784 (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1785 (local (cdr (assq major-mode font-lock-keywords-alist)))
1786 (removed-keywords
1787 (cdr-safe (assq major-mode font-lock-removed-keywords-alist))))
1788 (set (make-local-variable 'font-lock-defaults) defaults)
1789 ;; Syntactic fontification?
1790 (if (nth 1 defaults)
1791 (set (make-local-variable 'font-lock-keywords-only) t)
1792 (kill-local-variable 'font-lock-keywords-only))
1793 ;; Case fold during regexp fontification?
1794 (if (nth 2 defaults)
1795 (set (make-local-variable 'font-lock-keywords-case-fold-search) t)
1796 (kill-local-variable 'font-lock-keywords-case-fold-search))
1797 ;; Syntax table for regexp and syntactic fontification?
1798 (if (null (nth 3 defaults))
1799 (kill-local-variable 'font-lock-syntax-table)
1800 (set (make-local-variable 'font-lock-syntax-table)
1801 (copy-syntax-table (syntax-table)))
1802 (dolist (selem (nth 3 defaults))
1803 ;; The character to modify may be a single CHAR or a STRING.
1804 (let ((syntax (cdr selem)))
1805 (dolist (char (if (numberp (car selem))
1806 (list (car selem))
1807 (mapcar 'identity (car selem))))
1808 (modify-syntax-entry char syntax font-lock-syntax-table)))))
1809 ;; Syntax function for syntactic fontification?
1810 (if (nth 4 defaults)
1811 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1812 (nth 4 defaults))
1813 (kill-local-variable 'font-lock-beginning-of-syntax-function))
1814 ;; Variable alist?
1815 (dolist (x (nthcdr 5 defaults))
1816 (set (make-local-variable (car x)) (cdr x)))
1817 ;; Set up `font-lock-keywords' last because its value might depend
1818 ;; on other settings (e.g. font-lock-compile-keywords uses
1819 ;; font-lock-beginning-of-syntax-function).
1820 (set (make-local-variable 'font-lock-keywords)
1821 (font-lock-eval-keywords keywords))
1822 ;; Local fontification?
1823 (while local
1824 (font-lock-add-keywords nil (car (car local)) (cdr (car local)))
1825 (setq local (cdr local)))
1826 (when removed-keywords
1827 (font-lock-remove-keywords nil removed-keywords))
1828 ;; Now compile the keywords.
1829 (unless (eq (car font-lock-keywords) t)
1830 (setq font-lock-keywords
1831 (font-lock-compile-keywords font-lock-keywords))))))
1832 \f
1833 ;;; Color etc. support.
1834
1835 ;; Note that `defface' will not overwrite any faces declared above via
1836 ;; `custom-declare-face'.
1837 (defface font-lock-comment-face
1838 '((((class grayscale) (background light))
1839 (:foreground "DimGray" :weight bold :slant italic))
1840 (((class grayscale) (background dark))
1841 (:foreground "LightGray" :weight bold :slant italic))
1842 (((class color) (min-colors 88) (background light))
1843 (:foreground "Firebrick"))
1844 (((class color) (min-colors 88) (background dark))
1845 (:foreground "chocolate1"))
1846 (((class color) (min-colors 16) (background light))
1847 (:foreground "red"))
1848 (((class color) (min-colors 16) (background dark))
1849 (:foreground "red1"))
1850 (((class color) (min-colors 8) (background light))
1851 (:foreground "red"))
1852 (((class color) (min-colors 8) (background dark))
1853 )
1854 (t (:weight bold :slant italic)))
1855 "Font Lock mode face used to highlight comments."
1856 :group 'font-lock-faces)
1857
1858 (defface font-lock-comment-delimiter-face
1859 '((default :inherit font-lock-comment-face)
1860 (((class grayscale)))
1861 (((class color) (min-colors 16)))
1862 (((class color) (min-colors 8) (background light))
1863 :foreground "red")
1864 (((class color) (min-colors 8) (background dark))
1865 :foreground "red1"))
1866 "Font Lock mode face used to highlight comment delimiters."
1867 :group 'font-lock-faces)
1868
1869 (defface font-lock-string-face
1870 '((((class grayscale) (background light)) (:foreground "DimGray" :slant italic))
1871 (((class grayscale) (background dark)) (:foreground "LightGray" :slant italic))
1872 (((class color) (min-colors 88) (background light)) (:foreground "RosyBrown"))
1873 (((class color) (min-colors 88) (background dark)) (:foreground "LightSalmon"))
1874 (((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
1875 (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
1876 (((class color) (min-colors 8)) (:foreground "green"))
1877 (t (:slant italic)))
1878 "Font Lock mode face used to highlight strings."
1879 :group 'font-lock-faces)
1880
1881 (defface font-lock-doc-face
1882 '((t :inherit font-lock-string-face))
1883 "Font Lock mode face used to highlight documentation."
1884 :group 'font-lock-faces)
1885
1886 (defface font-lock-keyword-face
1887 '((((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
1888 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
1889 (((class color) (min-colors 88) (background light)) (:foreground "Purple"))
1890 (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
1891 (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
1892 (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
1893 (((class color) (min-colors 8)) (:foreground "cyan" :weight bold))
1894 (t (:weight bold)))
1895 "Font Lock mode face used to highlight keywords."
1896 :group 'font-lock-faces)
1897
1898 (defface font-lock-builtin-face
1899 '((((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
1900 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
1901 (((class color) (min-colors 88) (background light)) (:foreground "Orchid"))
1902 (((class color) (min-colors 88) (background dark)) (:foreground "LightSteelBlue"))
1903 (((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
1904 (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
1905 (((class color) (min-colors 8)) (:foreground "blue" :weight bold))
1906 (t (:weight bold)))
1907 "Font Lock mode face used to highlight builtins."
1908 :group 'font-lock-faces)
1909
1910 (defface font-lock-function-name-face
1911 '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
1912 (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
1913 (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
1914 (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
1915 (((class color) (min-colors 8)) (:foreground "blue" :weight bold))
1916 (t (:inverse-video t :weight bold)))
1917 "Font Lock mode face used to highlight function names."
1918 :group 'font-lock-faces)
1919
1920 (defface font-lock-variable-name-face
1921 '((((class grayscale) (background light))
1922 (:foreground "Gray90" :weight bold :slant italic))
1923 (((class grayscale) (background dark))
1924 (:foreground "DimGray" :weight bold :slant italic))
1925 (((class color) (min-colors 88) (background light)) (:foreground "DarkGoldenrod"))
1926 (((class color) (min-colors 88) (background dark)) (:foreground "LightGoldenrod"))
1927 (((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
1928 (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
1929 (((class color) (min-colors 8)) (:foreground "yellow" :weight light))
1930 (t (:weight bold :slant italic)))
1931 "Font Lock mode face used to highlight variable names."
1932 :group 'font-lock-faces)
1933
1934 (defface font-lock-type-face
1935 '((((class grayscale) (background light)) (:foreground "Gray90" :weight bold))
1936 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
1937 (((class color) (min-colors 88) (background light)) (:foreground "ForestGreen"))
1938 (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
1939 (((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
1940 (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
1941 (((class color) (min-colors 8)) (:foreground "green"))
1942 (t (:weight bold :underline t)))
1943 "Font Lock mode face used to highlight type and classes."
1944 :group 'font-lock-faces)
1945
1946 (defface font-lock-constant-face
1947 '((((class grayscale) (background light))
1948 (:foreground "LightGray" :weight bold :underline t))
1949 (((class grayscale) (background dark))
1950 (:foreground "Gray50" :weight bold :underline t))
1951 (((class color) (min-colors 88) (background light)) (:foreground "CadetBlue"))
1952 (((class color) (min-colors 88) (background dark)) (:foreground "Aquamarine"))
1953 (((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
1954 (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
1955 (((class color) (min-colors 8)) (:foreground "magenta"))
1956 (t (:weight bold :underline t)))
1957 "Font Lock mode face used to highlight constants and labels."
1958 :group 'font-lock-faces)
1959
1960 (defface font-lock-warning-face
1961 '((((class color) (min-colors 88) (background light)) (:foreground "Red1" :weight bold))
1962 (((class color) (min-colors 88) (background dark)) (:foreground "Pink" :weight bold))
1963 (((class color) (min-colors 16) (background light)) (:foreground "Red1" :weight bold))
1964 (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :weight bold))
1965 (((class color) (min-colors 8)) (:foreground "red"))
1966 (t (:inverse-video t :weight bold)))
1967 "Font Lock mode face used to highlight warnings."
1968 :group 'font-lock-faces)
1969
1970 (defface font-lock-negation-char-face
1971 '((t nil))
1972 "Font Lock mode face used to highlight easy to overlook negation."
1973 :group 'font-lock-faces)
1974
1975 (defface font-lock-preprocessor-face
1976 '((t :inherit font-lock-builtin-face))
1977 "Font Lock mode face used to highlight preprocessor directives."
1978 :group 'font-lock-faces)
1979
1980 (defface font-lock-regexp-grouping-backslash
1981 '((t :inherit bold))
1982 "Font Lock mode face for backslashes in Lisp regexp grouping constructs."
1983 :group 'font-lock-faces)
1984
1985 (defface font-lock-regexp-grouping-construct
1986 '((t :inherit bold))
1987 "Font Lock mode face used to highlight grouping constructs in Lisp regexps."
1988 :group 'font-lock-faces)
1989
1990 ;;; End of Color etc. support.
1991 \f
1992 ;;; Menu support.
1993
1994 ;; This section of code is commented out because Emacs does not have real menu
1995 ;; buttons. (We can mimic them by putting "( ) " or "(X) " at the beginning of
1996 ;; the menu entry text, but with Xt it looks both ugly and embarrassingly
1997 ;; amateur.) If/When Emacs gets real menus buttons, put in menu-bar.el after
1998 ;; the entry for "Text Properties" something like:
1999 ;;
2000 ;; (define-key menu-bar-edit-menu [font-lock]
2001 ;; (cons "Syntax Highlighting" font-lock-menu))
2002 ;;
2003 ;; and remove a single ";" from the beginning of each line in the rest of this
2004 ;; section. Probably the mechanism for telling the menu code what are menu
2005 ;; buttons and when they are on or off needs tweaking. I have assumed that the
2006 ;; mechanism is via `menu-toggle' and `menu-selected' symbol properties. sm.
2007
2008 ;;;;;###autoload
2009 ;;(progn
2010 ;; ;; Make the Font Lock menu.
2011 ;; (defvar font-lock-menu (make-sparse-keymap "Syntax Highlighting"))
2012 ;; ;; Add the menu items in reverse order.
2013 ;; (define-key font-lock-menu [fontify-less]
2014 ;; '("Less In Current Buffer" . font-lock-fontify-less))
2015 ;; (define-key font-lock-menu [fontify-more]
2016 ;; '("More In Current Buffer" . font-lock-fontify-more))
2017 ;; (define-key font-lock-menu [font-lock-sep]
2018 ;; '("--"))
2019 ;; (define-key font-lock-menu [font-lock-mode]
2020 ;; '("In Current Buffer" . font-lock-mode))
2021 ;; (define-key font-lock-menu [global-font-lock-mode]
2022 ;; '("In All Buffers" . global-font-lock-mode)))
2023 ;;
2024 ;;;;;###autoload
2025 ;;(progn
2026 ;; ;; We put the appropriate `menu-enable' etc. symbol property values on when
2027 ;; ;; font-lock.el is loaded, so we don't need to autoload the three variables.
2028 ;; (put 'global-font-lock-mode 'menu-toggle t)
2029 ;; (put 'font-lock-mode 'menu-toggle t)
2030 ;; (put 'font-lock-fontify-more 'menu-enable '(identity))
2031 ;; (put 'font-lock-fontify-less 'menu-enable '(identity)))
2032 ;;
2033 ;; ;; Put the appropriate symbol property values on now. See above.
2034 ;;(put 'global-font-lock-mode 'menu-selected 'global-font-lock-mode)
2035 ;;(put 'font-lock-mode 'menu-selected 'font-lock-mode)
2036 ;;(put 'font-lock-fontify-more 'menu-enable '(nth 2 font-lock-fontify-level))
2037 ;;(put 'font-lock-fontify-less 'menu-enable '(nth 1 font-lock-fontify-level))
2038 ;;
2039 ;;(defvar font-lock-fontify-level nil) ; For less/more fontification.
2040 ;;
2041 ;;(defun font-lock-fontify-level (level)
2042 ;; (let ((font-lock-maximum-decoration level))
2043 ;; (when font-lock-mode
2044 ;; (font-lock-mode))
2045 ;; (font-lock-mode)
2046 ;; (when font-lock-verbose
2047 ;; (message "Fontifying %s... level %d" (buffer-name) level))))
2048 ;;
2049 ;;(defun font-lock-fontify-less ()
2050 ;; "Fontify the current buffer with less decoration.
2051 ;;See `font-lock-maximum-decoration'."
2052 ;; (interactive)
2053 ;; ;; Check in case we get called interactively.
2054 ;; (if (nth 1 font-lock-fontify-level)
2055 ;; (font-lock-fontify-level (1- (car font-lock-fontify-level)))
2056 ;; (error "No less decoration")))
2057 ;;
2058 ;;(defun font-lock-fontify-more ()
2059 ;; "Fontify the current buffer with more decoration.
2060 ;;See `font-lock-maximum-decoration'."
2061 ;; (interactive)
2062 ;; ;; Check in case we get called interactively.
2063 ;; (if (nth 2 font-lock-fontify-level)
2064 ;; (font-lock-fontify-level (1+ (car font-lock-fontify-level)))
2065 ;; (error "No more decoration")))
2066 ;;
2067 ;; ;; This should be called by `font-lock-set-defaults'.
2068 ;;(defun font-lock-set-menu ()
2069 ;; ;; Activate less/more fontification entries if there are multiple levels for
2070 ;; ;; the current buffer. Sets `font-lock-fontify-level' to be of the form
2071 ;; ;; (CURRENT-LEVEL IS-LOWER-LEVEL-P IS-HIGHER-LEVEL-P) for menu activation.
2072 ;; (let ((keywords (or (nth 0 font-lock-defaults)
2073 ;; (nth 1 (assq major-mode font-lock-defaults-alist))))
2074 ;; (level (font-lock-value-in-major-mode font-lock-maximum-decoration)))
2075 ;; (make-local-variable 'font-lock-fontify-level)
2076 ;; (if (or (symbolp keywords) (= (length keywords) 1))
2077 ;; (font-lock-unset-menu)
2078 ;; (cond ((eq level t)
2079 ;; (setq level (1- (length keywords))))
2080 ;; ((or (null level) (zerop level))
2081 ;; ;; The default level is usually, but not necessarily, level 1.
2082 ;; (setq level (- (length keywords)
2083 ;; (length (member (eval (car keywords))
2084 ;; (mapcar 'eval (cdr keywords))))))))
2085 ;; (setq font-lock-fontify-level (list level (> level 1)
2086 ;; (< level (1- (length keywords))))))))
2087 ;;
2088 ;; ;; This should be called by `font-lock-unset-defaults'.
2089 ;;(defun font-lock-unset-menu ()
2090 ;; ;; Deactivate less/more fontification entries.
2091 ;; (setq font-lock-fontify-level nil))
2092
2093 ;;; End of Menu support.
2094 \f
2095 ;;; Various regexp information shared by several modes.
2096 ;; ;; Information specific to a single mode should go in its load library.
2097
2098 ;; Font Lock support for C, C++, Objective-C and Java modes is now in
2099 ;; cc-fonts.el (and required by cc-mode.el). However, the below function
2100 ;; should stay in font-lock.el, since it is used by other libraries. sm.
2101
2102 (defun font-lock-match-c-style-declaration-item-and-skip-to-next (limit)
2103 "Match, and move over, any declaration/definition item after point.
2104 Matches after point, but ignores leading whitespace and `*' characters.
2105 Does not move further than LIMIT.
2106
2107 The expected syntax of a declaration/definition item is `word' (preceded by
2108 optional whitespace and `*' characters and proceeded by optional whitespace)
2109 optionally followed by a `('. Everything following the item (but belonging to
2110 it) is expected to be skip-able by `scan-sexps', and items are expected to be
2111 separated with a `,' and to be terminated with a `;'.
2112
2113 Thus the regexp matches after point: word (
2114 ^^^^ ^
2115 Where the match subexpressions are: 1 2
2116
2117 The item is delimited by (match-beginning 1) and (match-end 1).
2118 If (match-beginning 2) is non-nil, the item is followed by a `('.
2119
2120 This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
2121 (when (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?")
2122 (when (and (match-end 2) (> (- (match-end 2) (match-beginning 2)) 1))
2123 ;; If `word' is followed by a double open-paren, it's probably
2124 ;; a macro used for "int myfun P_ ((int arg1))". Let's go back one
2125 ;; word to try and match `myfun' rather than `P_'.
2126 (let ((pos (point)))
2127 (skip-chars-backward " \t\n")
2128 (skip-syntax-backward "w")
2129 (unless (looking-at "\\(\\sw+\\)[ \t\n]*\\sw+[ \t\n]*\\(((?\\)?")
2130 ;; Looks like it was something else, so go back to where we
2131 ;; were and reset the match data by rematching.
2132 (goto-char pos)
2133 (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?"))))
2134 (save-match-data
2135 (condition-case nil
2136 (save-restriction
2137 ;; Restrict to the LIMIT.
2138 (narrow-to-region (point-min) limit)
2139 (goto-char (match-end 1))
2140 ;; Move over any item value, etc., to the next item.
2141 (while (not (looking-at "[ \t\n]*\\(\\(,\\)\\|;\\|\\'\\)"))
2142 (goto-char (or (scan-sexps (point) 1) (point-max))))
2143 (if (match-end 2)
2144 (goto-char (match-end 2))))
2145 (error t)))))
2146
2147 ;; C preprocessor(cpp) is used outside of C, C++ and Objective-C source file.
2148 ;; e.g. assembler code and GNU linker script in Linux kernel.
2149 ;; `cpp-font-lock-keywords' is handy for modes for the files.
2150 ;;
2151 ;; Here we cannot use `regexp-opt' because because regex-opt is not preloaded
2152 ;; while font-lock.el is preloaded to emacs. So values pre-calculated with
2153 ;; regexp-opt are used here.
2154
2155 ;; `cpp-font-lock-keywords-source-directives' is calculated from:
2156 ;;
2157 ;; (regexp-opt
2158 ;; '("define" "elif" "else" "endif" "error" "file" "if" "ifdef"
2159 ;; "ifndef" "import" "include" "line" "pragma" "undef" "warning"))
2160 ;;
2161 (defconst cpp-font-lock-keywords-source-directives
2162 "define\\|e\\(?:l\\(?:if\\|se\\)\\|ndif\\|rror\\)\\|file\\|i\\(?:f\\(?:n?def\\)?\\|mport\\|nclude\\)\\|line\\|pragma\\|undef\\|warning"
2163 "Regular expression used in `cpp-font-lock-keywords'.")
2164
2165 ;; `cpp-font-lock-keywords-source-depth' is calculated from:
2166 ;;
2167 ;; (regexp-opt-depth (regexp-opt
2168 ;; '("define" "elif" "else" "endif" "error" "file" "if" "ifdef"
2169 ;; "ifndef" "import" "include" "line" "pragma" "undef" "warning")))
2170 ;;
2171 (defconst cpp-font-lock-keywords-source-depth 0
2172 "An integer representing regular expression depth of `cpp-font-lock-keywords-source-directives'.
2173 Used in `cpp-font-lock-keywords'.")
2174
2175 (defconst cpp-font-lock-keywords
2176 (let* ((directives cpp-font-lock-keywords-source-directives)
2177 (directives-depth cpp-font-lock-keywords-source-depth))
2178 (list
2179 ;;
2180 ;; Fontify error directives.
2181 '("^#[ \t]*\\(?:error\\|warning\\)[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend)
2182 ;;
2183 ;; Fontify filenames in #include <...> preprocessor directives as strings.
2184 '("^#[ \t]*\\(?:import\\|include\\)[ \t]*\\(<[^>\"\n]*>?\\)"
2185 1 font-lock-string-face prepend)
2186 ;;
2187 ;; Fontify function macro names.
2188 '("^#[ \t]*define[ \t]+\\([[:alpha:]_][[:alnum:]_$]*\\)("
2189 (1 font-lock-function-name-face prepend)
2190 ;;
2191 ;; Macro arguments.
2192 ((lambda (limit)
2193 (re-search-forward
2194 "\\(?:\\([[:alpha:]_][[:alnum:]_]*\\)[,]?\\)"
2195 (or (save-excursion (re-search-forward ")" limit t))
2196 limit)
2197 t))
2198 nil nil (1 font-lock-variable-name-face prepend)))
2199 ;;
2200 ;; Fontify symbol names in #elif or #if ... defined preprocessor directives.
2201 '("^#[ \t]*\\(?:elif\\|if\\)\\>"
2202 ("\\<\\(defined\\)\\>[ \t]*(?\\([[:alpha:]_][[:alnum:]_]*\\)?" nil nil
2203 (1 font-lock-builtin-face prepend) (2 font-lock-variable-name-face prepend t)))
2204 ;;
2205 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
2206 (list
2207 (concat "^\\(#[ \t]*\\(?:" directives
2208 "\\)\\)\\>[ \t!]*\\([[:alpha:]_][[:alnum:]_]*\\)?")
2209 '(1 font-lock-preprocessor-face prepend)
2210 (list (+ 2 directives-depth)
2211 'font-lock-variable-name-face nil t))))
2212 "Font lock keywords for C preprocessor directives.
2213 `c-mode', `c++-mode' and `objc-mode' have their own font lock keywords
2214 for C preprocessor directives. This definition is for the other modes
2215 in which C preprocessor directives are used. e.g. `asm-mode' and
2216 `ld-script-mode'.")
2217
2218 \f
2219 ;; Lisp.
2220
2221 (defconst lisp-font-lock-keywords-1
2222 (eval-when-compile
2223 `(;; Definitions.
2224 (,(concat "(\\(def\\("
2225 ;; Function declarations.
2226 "\\(advice\\|alias\\|generic\\|macro\\*?\\|method\\|"
2227 "setf\\|subst\\*?\\|un\\*?\\|"
2228 "ine-\\(condition\\|"
2229 "\\(?:derived\\|\\(?:global\\(?:ized\\)?-\\)?minor\\|generic\\)-mode\\|"
2230 "method-combination\\|setf-expander\\|skeleton\\|widget\\|"
2231 "function\\|\\(compiler\\|modify\\|symbol\\)-macro\\)\\)\\|"
2232 ;; Variable declarations.
2233 "\\(const\\(ant\\)?\\|custom\\|varalias\\|face\\|parameter\\|var\\)\\|"
2234 ;; Structure declarations.
2235 "\\(class\\|group\\|theme\\|package\\|struct\\|type\\)"
2236 "\\)\\)\\>"
2237 ;; Any whitespace and defined object.
2238 "[ \t'\(]*"
2239 "\\(setf[ \t]+\\sw+)\\|\\sw+\\)?")
2240 (1 font-lock-keyword-face)
2241 (9 (cond ((match-beginning 3) font-lock-function-name-face)
2242 ((match-beginning 6) font-lock-variable-name-face)
2243 (t font-lock-type-face))
2244 nil t))
2245 ;; Emacs Lisp autoload cookies. Supports the slightly different
2246 ;; forms used by mh-e, calendar, etc.
2247 ("^;;;###\\([-a-z]*autoload\\)" 1 font-lock-warning-face prepend)
2248 ;; Regexp negated char group.
2249 ("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)))
2250 "Subdued level highlighting for Lisp modes.")
2251
2252 (defconst lisp-font-lock-keywords-2
2253 (append lisp-font-lock-keywords-1
2254 (eval-when-compile
2255 `(;; Control structures. Emacs Lisp forms.
2256 (,(concat
2257 "(" (regexp-opt
2258 '("cond" "if" "while" "while-no-input" "let" "let*"
2259 "prog" "progn" "progv" "prog1" "prog2" "prog*"
2260 "inline" "lambda" "save-restriction" "save-excursion"
2261 "save-selected-window" "save-window-excursion"
2262 "save-match-data" "save-current-buffer"
2263 "unwind-protect" "condition-case" "track-mouse"
2264 "eval-after-load" "eval-and-compile" "eval-when-compile"
2265 "eval-when" "eval-at-startup" "eval-next-after-load"
2266 "with-case-table" "with-category-table"
2267 "with-current-buffer" "with-electric-help"
2268 "with-local-quit" "with-no-warnings"
2269 "with-output-to-string" "with-output-to-temp-buffer"
2270 "with-selected-window" "with-selected-frame" "with-syntax-table"
2271 "with-temp-buffer" "with-temp-file" "with-temp-message"
2272 "with-timeout" "with-timeout-handler") t)
2273 "\\>")
2274 . 1)
2275 ;; Control structures. Common Lisp forms.
2276 (,(concat
2277 "(" (regexp-opt
2278 '("when" "unless" "case" "ecase" "typecase" "etypecase"
2279 "ccase" "ctypecase" "handler-case" "handler-bind"
2280 "restart-bind" "restart-case" "in-package"
2281 "break" "ignore-errors"
2282 "loop" "do" "do*" "dotimes" "dolist" "the" "locally"
2283 "proclaim" "declaim" "declare" "symbol-macrolet"
2284 "lexical-let" "lexical-let*" "flet" "labels" "compiler-let"
2285 "destructuring-bind" "macrolet" "tagbody" "block" "go"
2286 "multiple-value-bind" "multiple-value-prog1"
2287 "return" "return-from"
2288 "with-accessors" "with-compilation-unit"
2289 "with-condition-restarts" "with-hash-table-iterator"
2290 "with-input-from-string" "with-open-file"
2291 "with-open-stream" "with-output-to-string"
2292 "with-package-iterator" "with-simple-restart"
2293 "with-slots" "with-standard-io-syntax") t)
2294 "\\>")
2295 . 1)
2296 ;; Exit/Feature symbols as constants.
2297 (,(concat "(\\(catch\\|throw\\|featurep\\|provide\\|require\\)\\>"
2298 "[ \t']*\\(\\sw+\\)?")
2299 (1 font-lock-keyword-face)
2300 (2 font-lock-constant-face nil t))
2301 ;; Erroneous structures.
2302 ("(\\(abort\\|assert\\|warn\\|check-type\\|cerror\\|error\\|signal\\)\\>" 1 font-lock-warning-face)
2303 ;; Words inside \\[] tend to be for `substitute-command-keys'.
2304 ("\\\\\\\\\\[\\(\\sw+\\)\\]" 1 font-lock-constant-face prepend)
2305 ;; Words inside `' tend to be symbol names.
2306 ("`\\(\\sw\\sw+\\)'" 1 font-lock-constant-face prepend)
2307 ;; Constant values.
2308 ("\\<:\\sw+\\>" 0 font-lock-builtin-face)
2309 ;; ELisp and CLisp `&' keywords as types.
2310 ("\\<\\&\\sw+\\>" . font-lock-type-face)
2311 ;; ELisp regexp grouping constructs
2312 ((lambda (bound)
2313 (catch 'found
2314 ;; The following loop is needed to continue searching after matches
2315 ;; that do not occur in strings. The associated regexp matches one
2316 ;; of `\\\\' `\\(' `\\(?:' `\\|' `\\)'. `\\\\' has been included to
2317 ;; avoid highlighting, for example, `\\(' in `\\\\('.
2318 (while (re-search-forward "\\(\\\\\\\\\\)\\(?:\\(\\\\\\\\\\)\\|\\((\\(?:\\?[0-9]*:\\)?\\|[|)]\\)\\)" bound t)
2319 (unless (match-beginning 2)
2320 (let ((face (get-text-property (1- (point)) 'face)))
2321 (when (or (and (listp face)
2322 (memq 'font-lock-string-face face))
2323 (eq 'font-lock-string-face face))
2324 (throw 'found t)))))))
2325 (1 'font-lock-regexp-grouping-backslash prepend)
2326 (3 'font-lock-regexp-grouping-construct prepend))
2327 ;;; This is too general -- rms.
2328 ;;; A user complained that he has functions whose names start with `do'
2329 ;;; and that they get the wrong color.
2330 ;;; ;; CL `with-' and `do-' constructs
2331 ;;; ("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
2332 )))
2333 "Gaudy level highlighting for Lisp modes.")
2334
2335 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
2336 "Default expressions to highlight in Lisp modes.")
2337 \f
2338 (provide 'font-lock)
2339
2340 ;; arch-tag: 682327e4-64d8-4057-b20b-1fbb9f1fc54c
2341 ;;; font-lock.el ends here