(jit-lock): Move group declaration from font-lock.el.
[bpt/emacs.git] / lisp / font-lock.el
CommitLineData
876f2438
SM
1;;; font-lock.el --- Electric font lock mode
2
1fed50c4 3;; Copyright (C) 1992, 93, 94, 95, 96, 97, 98, 1999, 2000, 2001, 02, 2003
01b69148 4;; Free Software Foundation, Inc.
030f4a35 5
0279f991 6;; Author: jwz, then rms, then sm
030f4a35
RS
7;; Maintainer: FSF
8;; Keywords: languages, faces
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b578f267
EN
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
030f4a35 26
030f4a35
RS
27;;; Commentary:
28
b89e1134
SM
29;; Font Lock mode is a minor mode that causes your comments to be displayed in
30;; one face, strings in another, reserved words in another, and so on.
030f4a35
RS
31;;
32;; Comments will be displayed in `font-lock-comment-face'.
33;; Strings will be displayed in `font-lock-string-face'.
a1eb1cf1 34;; Regexps are used to display selected patterns in other faces.
030f4a35 35;;
9bfbb130
SM
36;; To make the text you type be fontified, use M-x font-lock-mode RET.
37;; When this minor mode is on, the faces of the current line are updated with
38;; every insertion or deletion.
030f4a35 39;;
a2b8e66b 40;; To turn Font Lock mode on automatically, add this to your ~/.emacs file:
030f4a35 41;;
b89e1134 42;; (add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock)
030f4a35 43;;
a2b8e66b
SM
44;; Or if you want to turn Font Lock mode on in many modes:
45;;
46;; (global-font-lock-mode t)
47;;
9bfbb130
SM
48;; Fontification for a particular mode may be available in a number of levels
49;; of decoration. The higher the level, the more decoration, but the more time
50;; it takes to fontify. See the variable `font-lock-maximum-decoration', and
5aa29679
RS
51;; also the variable `font-lock-maximum-size'. Support modes for Font Lock
52;; mode can be used to speed up Font Lock mode. See `font-lock-support-mode'.
98f84f52 53\f
56fcbd7e
SM
54;;; How Font Lock mode fontifies:
55
56;; When Font Lock mode is turned on in a buffer, it (a) fontifies the entire
57;; buffer and (b) installs one of its fontification functions on one of the
58;; hook variables that are run by Emacs after every buffer change (i.e., an
59;; insertion or deletion). Fontification means the replacement of `face' text
60;; properties in a given region; Emacs displays text with these `face' text
61;; properties appropriately.
62;;
63;; Fontification normally involves syntactic (i.e., strings and comments) and
ac6e572c
SM
64;; regexp (i.e., keywords and everything else) passes. There are actually
65;; three passes; (a) the syntactic keyword pass, (b) the syntactic pass and (c)
66;; the keyword pass. Confused?
67;;
68;; The syntactic keyword pass places `syntax-table' text properties in the
69;; buffer according to the variable `font-lock-syntactic-keywords'. It is
70;; necessary because Emacs' syntax table is not powerful enough to describe all
71;; the different syntactic constructs required by the sort of people who decide
72;; that a single quote can be syntactic or not depending on the time of day.
73;; (What sort of person could decide to overload the meaning of a quote?)
74;; Obviously the syntactic keyword pass must occur before the syntactic pass.
75;;
76;; The syntactic pass places `face' text properties in the buffer according to
77;; syntactic context, i.e., according to the buffer's syntax table and buffer
78;; text's `syntax-table' text properties. It involves using a syntax parsing
79;; function to determine the context of different parts of a region of text. A
80;; syntax parsing function is necessary because generally strings and/or
81;; comments can span lines, and so the context of a given region is not
82;; necessarily apparent from the content of that region. Because the keyword
83;; pass only works within a given region, it is not generally appropriate for
84;; syntactic fontification. This is the first fontification pass that makes
85;; changes visible to the user; it fontifies strings and comments.
86;;
87;; The keyword pass places `face' text properties in the buffer according to
88;; the variable `font-lock-keywords'. It involves searching for given regexps
89;; (or calling given search functions) within the given region. This is the
90;; second fontification pass that makes changes visible to the user; it
91;; fontifies language reserved words, etc.
92;;
93;; Oh, and the answer is, "Yes, obviously just about everything should be done
94;; in a single syntactic pass, but the only syntactic parser available
95;; understands only strings and comments." Perhaps one day someone will write
96;; some syntactic parsers for common languages and a son-of-font-lock.el could
97;; use them rather then relying so heavily on the keyword (regexp) pass.
56fcbd7e 98
c1f2ffc8
SM
99;;; How Font Lock mode supports modes or is supported by modes:
100
101;; Modes that support Font Lock mode do so by defining one or more variables
102;; whose values specify the fontification. Font Lock mode knows of these
103;; variable names from (a) the buffer local variable `font-lock-defaults', if
104;; non-nil, or (b) the global variable `font-lock-defaults-alist', if the major
105;; mode has an entry. (Font Lock mode is set up via (a) where a mode's
106;; patterns are distributed with the mode's package library, and (b) where a
107;; mode's patterns are distributed with font-lock.el itself. An example of (a)
108;; is Pascal mode, an example of (b) is Lisp mode. Normally, the mechanism is
109;; (a); (b) is used where it is not clear which package library should contain
110;; the pattern definitions.) Font Lock mode chooses which variable to use for
111;; fontification based on `font-lock-maximum-decoration'.
56fcbd7e
SM
112;;
113;; Font Lock mode fontification behaviour can be modified in a number of ways.
114;; See the below comments and the comments distributed throughout this file.
c1f2ffc8
SM
115
116;;; Constructing patterns:
117
98f84f52
SM
118;; See the documentation for the variable `font-lock-keywords'.
119;;
ac6e572c
SM
120;; Efficient regexps for use as MATCHERs for `font-lock-keywords' and
121;; `font-lock-syntactic-keywords' can be generated via the function
afa18a4e 122;; `regexp-opt'.
98f84f52 123
c1f2ffc8
SM
124;;; Adding patterns for modes that already support Font Lock:
125
126;; Though Font Lock highlighting patterns already exist for many modes, it's
127;; likely there's something that you want fontified that currently isn't, even
128;; at the maximum fontification level. You can add highlighting patterns via
129;; `font-lock-add-keywords'. For example, say in some C
130;; header file you #define the token `and' to expand to `&&', etc., to make
131;; your C code almost readable. In your ~/.emacs there could be:
98f84f52 132;;
c1f2ffc8 133;; (font-lock-add-keywords 'c-mode '("\\<\\(and\\|or\\|not\\)\\>"))
98f84f52 134;;
c1f2ffc8
SM
135;; Some modes provide specific ways to modify patterns based on the values of
136;; other variables. For example, additional C types can be specified via the
137;; variable `c-font-lock-extra-types'.
138
139;;; Adding patterns for modes that do not support Font Lock:
140
141;; Not all modes support Font Lock mode. If you (as a user of the mode) add
142;; patterns for a new mode, you must define in your ~/.emacs a variable or
143;; variables that specify regexp fontification. Then, you should indicate to
144;; Font Lock mode, via the mode hook setting `font-lock-defaults', exactly what
145;; support is required. For example, say Foo mode should have the following
146;; regexps fontified case-sensitively, and comments and strings should not be
147;; fontified automagically. In your ~/.emacs there could be:
98f84f52 148;;
c1f2ffc8
SM
149;; (defvar foo-font-lock-keywords
150;; '(("\\<\\(one\\|two\\|three\\)\\>" . font-lock-keyword-face)
151;; ("\\<\\(four\\|five\\|six\\)\\>" . font-lock-type-face))
152;; "Default expressions to highlight in Foo mode.")
98f84f52 153;;
c1f2ffc8 154;; (add-hook 'foo-mode-hook
e9476ca8
CW
155;; (lambda ()
156;; (make-local-variable 'font-lock-defaults)
157;; (setq font-lock-defaults '(foo-font-lock-keywords t))))
c1f2ffc8
SM
158
159;;; Adding Font Lock support for modes:
160
161;; Of course, it would be better that the mode already supports Font Lock mode.
162;; The package author would do something similar to above. The mode must
163;; define at the top-level a variable or variables that specify regexp
164;; fontification. Then, the mode command should indicate to Font Lock mode,
165;; via `font-lock-defaults', exactly what support is required. For example,
166;; say Bar mode should have the following regexps fontified case-insensitively,
167;; and comments and strings should be fontified automagically. In bar.el there
168;; could be:
98f84f52 169;;
c1f2ffc8
SM
170;; (defvar bar-font-lock-keywords
171;; '(("\\<\\(uno\\|due\\|tre\\)\\>" . font-lock-keyword-face)
172;; ("\\<\\(quattro\\|cinque\\|sei\\)\\>" . font-lock-type-face))
173;; "Default expressions to highlight in Bar mode.")
98f84f52 174;;
c1f2ffc8 175;; and within `bar-mode' there could be:
98f84f52 176;;
c1f2ffc8
SM
177;; (make-local-variable 'font-lock-defaults)
178;; (setq font-lock-defaults '(bar-font-lock-keywords nil t))
98f84f52 179\f
9bfbb130
SM
180;; What is fontification for? You might say, "It's to make my code look nice."
181;; I think it should be for adding information in the form of cues. These cues
182;; should provide you with enough information to both (a) distinguish between
183;; different items, and (b) identify the item meanings, without having to read
184;; the items and think about it. Therefore, fontification allows you to think
185;; less about, say, the structure of code, and more about, say, why the code
186;; doesn't work. Or maybe it allows you to think less and drift off to sleep.
187;;
188;; So, here are my opinions/advice/guidelines:
5e9e032a 189;;
7d7d915a
SM
190;; - Highlight conceptual objects, such as function and variable names, and
191;; different objects types differently, i.e., (a) and (b) above, highlight
192;; function names differently to variable names.
193;; - Keep the faces distinct from each other as far as possible.
194;; i.e., (a) above.
9bfbb130
SM
195;; - Use the same face for the same conceptual object, across all modes.
196;; i.e., (b) above, all modes that have items that can be thought of as, say,
197;; keywords, should be highlighted with the same face, etc.
9bfbb130
SM
198;; - Make the face attributes fit the concept as far as possible.
199;; i.e., function names might be a bold colour such as blue, comments might
200;; be a bright colour such as red, character strings might be brown, because,
201;; err, strings are brown (that was not the reason, please believe me).
202;; - Don't use a non-nil OVERRIDE unless you have a good reason.
203;; Only use OVERRIDE for special things that are easy to define, such as the
204;; way `...' quotes are treated in strings and comments in Emacs Lisp mode.
205;; Don't use it to, say, highlight keywords in commented out code or strings.
206;; - Err, that's it.
a1eb1cf1 207\f
2e6a15fc
SM
208;;; Code:
209
8259bf10
SM
210(require 'syntax)
211
2273c36b 212;; Define core `font-lock' group.
55015061
SM
213(defgroup font-lock nil
214 "Font Lock mode text highlighting package."
215 :link '(custom-manual "(emacs)Font Lock")
63e20d60 216 :link '(custom-manual "(elisp)Font Lock Mode")
55015061
SM
217 :group 'faces)
218
2273c36b
SM
219(defgroup font-lock-highlighting-faces nil
220 "Faces for highlighting text."
55015061 221 :prefix "font-lock-"
2273c36b
SM
222 :group 'font-lock)
223
224(defgroup font-lock-extra-types nil
225 "Extra mode-specific type names for highlighting declarations."
226 :group 'font-lock)
227
ac6e572c 228;; Define support mode groups here to impose `font-lock' group order.
2273c36b
SM
229(defgroup fast-lock nil
230 "Font Lock support mode to cache fontification."
231 :link '(custom-manual "(emacs)Support Modes")
232 :load 'fast-lock
233 :group 'font-lock)
234
235(defgroup lazy-lock nil
236 "Font Lock support mode to fontify lazily."
237 :link '(custom-manual "(emacs)Support Modes")
238 :load 'lazy-lock
55015061 239 :group 'font-lock)
e7f07c2c
GM
240
241(defgroup jit-lock nil
242 "Font Lock support mode to fontify just-in-time."
243 :link '(custom-manual "(emacs)Support Modes")
63e20d60 244 :version "21.1"
faa57c0a 245 :load 'jit-lock
e7f07c2c 246 :group 'font-lock)
55015061 247\f
9bfbb130
SM
248;; User variables.
249
4fffc071 250(defcustom font-lock-maximum-size 256000
23a0e11b
SM
251 "*Maximum size of a buffer for buffer fontification.
252Only buffers less than this can be fontified when Font Lock mode is turned on.
253If nil, means size is irrelevant.
254If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
255where MAJOR-MODE is a symbol or t (meaning the default). For example:
256 ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576))
257means that the maximum size is 250K for buffers in C or C++ modes, one megabyte
258for buffers in Rmail mode, and size is irrelevant otherwise."
259 :type '(choice (const :tag "none" nil)
260 (integer :tag "size")
261 (repeat :menu-tag "mode specific" :tag "mode specific"
262 :value ((t . nil))
263 (cons :tag "Instance"
264 (radio :tag "Mode"
265 (const :tag "all" t)
266 (symbol :tag "name"))
267 (radio :tag "Size"
268 (const :tag "none" nil)
269 (integer :tag "size")))))
55015061 270 :group 'font-lock)
9bfbb130 271
55015061 272(defcustom font-lock-maximum-decoration t
2d9cdda8 273 "*Maximum decoration level for fontification.
9bfbb130
SM
274If nil, use the default decoration (typically the minimum available).
275If t, use the maximum decoration available.
276If a number, use that level of decoration (or if not available the maximum).
277If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
278where MAJOR-MODE is a symbol or t (meaning the default). For example:
5aa29679
RS
279 ((c-mode . t) (c++-mode . 2) (t . 1))
280means use the maximum decoration available for buffers in C mode, level 2
55015061 281decoration for buffers in C++ mode, and level 1 decoration otherwise."
b34aa0a3
SM
282 :type '(choice (const :tag "default" nil)
283 (const :tag "maximum" t)
284 (integer :tag "level" 1)
285 (repeat :menu-tag "mode specific" :tag "mode specific"
286 :value ((t . t))
287 (cons :tag "Instance"
288 (radio :tag "Mode"
289 (const :tag "all" t)
290 (symbol :tag "name"))
291 (radio :tag "Decoration"
292 (const :tag "default" nil)
019f00d8 293 (const :tag "maximum" t)
b34aa0a3 294 (integer :tag "level" 1)))))
55015061
SM
295 :group 'font-lock)
296
4fffc071 297(defcustom font-lock-verbose 0
23a0e11b
SM
298 "*If non-nil, means show status messages for buffer fontification.
299If a number, only buffers greater than this size have fontification messages."
300 :type '(choice (const :tag "never" nil)
4fffc071
SM
301 (other :tag "always" t)
302 (integer :tag "size"))
55015061 303 :group 'font-lock)
9bfbb130 304\f
1c172af4
SM
305
306;; Originally these variable values were face names such as `bold' etc.
307;; Now we create our own faces, but we keep these variables for compatibility
308;; and they give users another mechanism for changing face appearance.
309;; We now allow a FACENAME in `font-lock-keywords' to be any expression that
310;; returns a face. So the easiest thing is to continue using these variables,
311;; rather than sometimes evaling FACENAME and sometimes not. sm.
312(defvar font-lock-comment-face 'font-lock-comment-face
313 "Face name to use for comments.")
314
315(defvar font-lock-string-face 'font-lock-string-face
316 "Face name to use for strings.")
317
318(defvar font-lock-doc-face 'font-lock-doc-face
319 "Face name to use for documentation.")
320
321(defvar font-lock-keyword-face 'font-lock-keyword-face
322 "Face name to use for keywords.")
323
324(defvar font-lock-builtin-face 'font-lock-builtin-face
325 "Face name to use for builtins.")
326
327(defvar font-lock-function-name-face 'font-lock-function-name-face
328 "Face name to use for function names.")
329
330(defvar font-lock-variable-name-face 'font-lock-variable-name-face
331 "Face name to use for variable names.")
332
333(defvar font-lock-type-face 'font-lock-type-face
334 "Face name to use for type and class names.")
335
336(defvar font-lock-constant-face 'font-lock-constant-face
337 "Face name to use for constant and label names.")
338
339(defvar font-lock-warning-face 'font-lock-warning-face
340 "Face name to use for things that should stand out.")
341
d654cab8
SM
342(defvar font-lock-preprocessor-face 'font-lock-preprocessor-face
343 "Face name to use for preprocessor directives.")
344
9087b271
JB
345(defvar font-lock-reference-face 'font-lock-constant-face)
346(make-obsolete-variable 'font-lock-reference-face 'font-lock-constant-face)
1c172af4 347
9bfbb130
SM
348;; Fontification variables:
349
030f4a35 350(defvar font-lock-keywords nil
ac6e572c 351 "A list of the keywords to highlight.
caf0dd71 352Each element should have one of these forms:
a1eb1cf1 353
826b2925
SM
354 MATCHER
355 (MATCHER . MATCH)
356 (MATCHER . FACENAME)
357 (MATCHER . HIGHLIGHT)
358 (MATCHER HIGHLIGHT ...)
a078558d 359 (eval . FORM)
a1eb1cf1 360
558371ba
SM
361where MATCHER can be either the regexp to search for, or the function name to
362call to make the search (called with one argument, the limit of the search) and
363return non-nil if it succeeds (and set `match-data' appropriately).
364MATCHER regexps can be generated via the function `regexp-opt'.
9bfbb130 365
a078558d
SM
366FORM is an expression, whose value should be a keyword element, evaluated when
367the keyword is (first) used in a buffer. This feature can be used to provide a
368keyword that can only be generated when Font Lock mode is actually turned on.
369
558371ba
SM
370HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
371
9c8de95c
SM
372For highlighting single items, for example each instance of the word \"foo\",
373typically only MATCH-HIGHLIGHT is required.
d9f7b2d3 374However, if an item or (typically) items are to be highlighted following the
9c8de95c
SM
375instance of another item (the anchor), for example each instance of the
376word \"bar\" following the word \"anchor\" then MATCH-ANCHORED may be required.
9bfbb130
SM
377
378MATCH-HIGHLIGHT should be of the form:
379
380 (MATCH FACENAME OVERRIDE LAXMATCH)
381
558371ba
SM
382MATCH is the subexpression of MATCHER to be highlighted. FACENAME is an
383expression whose value is the face name to use. Face default attributes
384can be modified via \\[customize]. Instead of a face, FACENAME can
385evaluate to a property list of the form (face VAL1 PROP2 VAL2 PROP3 VAL3 ...)
386in which case all the listed text-properties will be set rather than
387just `face'. In such a case, you will most likely want to put those
388properties in `font-lock-extra-managed-props' or to override
389`font-lock-unfontify-region-function'.
a1eb1cf1 390
2d63aa67 391OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification can
9bfbb130
SM
392be overwritten. If `keep', only parts not already fontified are highlighted.
393If `prepend' or `append', existing fontification is merged with the new, in
394which the new or existing fontification, respectively, takes precedence.
d9f7b2d3 395If LAXMATCH is non-nil, no error is signaled if there is no MATCH in MATCHER.
a1eb1cf1 396
9bfbb130
SM
397For example, an element of the form highlights (if not already highlighted):
398
9c8de95c 399 \"\\\\\\=<foo\\\\\\=>\" discrete occurrences of \"foo\" in the value of the
876f2438 400 variable `font-lock-keyword-face'.
9c8de95c 401 (\"fu\\\\(bar\\\\)\" . 1) substring \"bar\" within all occurrences of \"fubar\" in
9bfbb130
SM
402 the value of `font-lock-keyword-face'.
403 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'.
404 (\"foo\\\\|bar\" 0 foo-bar-face t)
9c8de95c 405 occurrences of either \"foo\" or \"bar\" in the value
9bfbb130 406 of `foo-bar-face', even if already highlighted.
844a6a46 407 (fubar-match 1 fubar-face)
9c8de95c 408 the first subexpression within all occurrences of
844a6a46
SM
409 whatever the function `fubar-match' finds and matches
410 in the value of `fubar-face'.
9bfbb130
SM
411
412MATCH-ANCHORED should be of the form:
413
414 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
415
9c8de95c
SM
416where MATCHER is a regexp to search for or the function name to call to make
417the search, as for MATCH-HIGHLIGHT above, but with one exception; see below.
876f2438
SM
418PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
419the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be
420used to initialise before, and cleanup after, MATCHER is used. Typically,
421PRE-MATCH-FORM is used to move to some position relative to the original
422MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might
423be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER.
9bfbb130
SM
424
425For example, an element of the form highlights (if not already highlighted):
426
876f2438 427 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
9bfbb130 428
9c8de95c 429 discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
876f2438
SM
430 discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
431 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is
432 initially searched for starting from the end of the match of \"anchor\", and
433 searching for subsequent instance of \"anchor\" resumes from where searching
434 for \"item\" concluded.)
9bfbb130 435
56fcbd7e
SM
436The above-mentioned exception is as follows. The limit of the MATCHER search
437defaults to the end of the line after PRE-MATCH-FORM is evaluated.
438However, if PRE-MATCH-FORM returns a position greater than the position after
439PRE-MATCH-FORM is evaluated, that position is used as the limit of the search.
440It is generally a bad idea to return a position greater than the end of the
441line, i.e., cause the MATCHER search to span lines.
442
6e1d0d15
SM
443These regular expressions can match text which spans lines, although
444it is better to avoid it if possible since updating them while editing
445text is slower, and it is not guaranteed to be always correct when using
446support modes like jit-lock or lazy-lock.
a1eb1cf1 447
2d63aa67
SM
448This variable is set by major modes via the variable `font-lock-defaults'.
449Be careful when composing regexps for this list; a poorly written pattern can
450dramatically slow things down!")
030f4a35 451
c1f2ffc8
SM
452(defvar font-lock-keywords-alist nil
453 "*Alist of `font-lock-keywords' local to a `major-mode'.
76f5e2af
GM
454This is normally set via `font-lock-add-keywords' and
455`font-lock-remove-keywords'.")
456
457(defvar font-lock-removed-keywords-alist nil
458 "*Alist of `font-lock-keywords' removed from `major-mode'.
459This is normally set via `font-lock-add-keywords' and
460`font-lock-remove-keywords'.")
c1f2ffc8 461
9bfbb130 462(defvar font-lock-keywords-only nil
d46c21ec
SM
463 "*Non-nil means Font Lock should not fontify comments or strings.
464This is normally set via `font-lock-defaults'.")
b89e1134 465
030f4a35 466(defvar font-lock-keywords-case-fold-search nil
d46c21ec
SM
467 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
468This is normally set via `font-lock-defaults'.")
1087f198 469(make-variable-buffer-local 'font-lock-keywords-case-fold-search)
030f4a35 470
c0a6a9fe
SM
471(defvar font-lock-syntactically-fontified 0
472 "Point up to which `font-lock-syntactic-keywords' has been applied.
473If nil, this is ignored, in which case the syntactic fontification may
474sometimes be slightly incorrect.")
475(make-variable-buffer-local 'font-lock-syntactically-fontified)
476
1c172af4
SM
477(defvar font-lock-syntactic-face-function
478 (lambda (state)
479 (if (nth 3 state) font-lock-string-face font-lock-comment-face))
480 "Function to determine which face to use when fontifying syntactically.
481The function is called with a single parameter (the state as returned by
482`parse-partial-sexp' at the beginning of the region to highlight) and
483should return a face.")
484
ac6e572c
SM
485(defvar font-lock-syntactic-keywords nil
486 "A list of the syntactic keywords to highlight.
487Can be the list or the name of a function or variable whose value is the list.
488See `font-lock-keywords' for a description of the form of this list;
489the differences are listed below. MATCH-HIGHLIGHT should be of the form:
490
491 (MATCH SYNTAX OVERRIDE LAXMATCH)
492
5299cb8e
SM
493where SYNTAX can be a string (as taken by `modify-syntax-entry'), a syntax
494table, a cons cell (as returned by `string-to-syntax') or an expression whose
495value is such a form. OVERRIDE cannot be `prepend' or `append'.
ac6e572c 496
9c8de95c
SM
497For example, an element of the form highlights syntactically:
498
5299cb8e 499 (\"\\\\$\\\\(#\\\\)\" 1 \".\")
9c8de95c 500
5299cb8e
SM
501 a hash character when following a dollar character, with a SYNTAX of
502 \".\" (meaning punctuation syntax). Assuming that the buffer syntax table does
9c8de95c
SM
503 specify hash characters to have comment start syntax, the element will only
504 highlight hash characters that do not follow dollar characters as comments
505 syntactically.
506
507 (\"\\\\('\\\\).\\\\('\\\\)\"
5299cb8e
SM
508 (1 \"\\\"\")
509 (2 \"\\\"\"))
9c8de95c 510
5299cb8e
SM
511 both single quotes which surround a single character, with a SYNTAX of
512 \"\\\"\" (meaning string quote syntax). Assuming that the buffer syntax table
9c8de95c
SM
513 does not specify single quotes to have quote syntax, the element will only
514 highlight single quotes of the form 'c' as strings syntactically.
515 Other forms, such as foo'bar or 'fubar', will not be highlighted as strings.
516
ac6e572c
SM
517This is normally set via `font-lock-defaults'.")
518
b056da51 519(defvar font-lock-syntax-table nil
799761f0 520 "Non-nil means use this syntax table for fontifying.
d46c21ec
SM
521If this is nil, the major mode's syntax table is used.
522This is normally set via `font-lock-defaults'.")
523
524(defvar font-lock-beginning-of-syntax-function nil
fc896a92
RS
525 "*Non-nil means use this function to move back outside all constructs.
526When called with no args it should move point backward to a place which
527is not in a string or comment and not within any bracket-pairs (or else,
528a place such that any bracket-pairs outside it can be ignored for Emacs
529syntax analysis and fontification).
530
531If this is nil, the beginning of the buffer is used, which is
532always correct but tends to be slow.
47fc2db3 533This is normally set via `font-lock-defaults'.
fc896a92
RS
534This variable is semi-obsolete; we recommend setting
535`syntax-begin-function' instead.")
b056da51 536
a078558d
SM
537(defvar font-lock-mark-block-function nil
538 "*Non-nil means use this function to mark a block of text.
539When called with no args it should leave point at the beginning of any
540enclosing textual block and mark at the end.
541This is normally set via `font-lock-defaults'.")
542
a2b8e66b
SM
543(defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
544 "Function to use for fontifying the buffer.
545This is normally set via `font-lock-defaults'.")
546
547(defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
548 "Function to use for unfontifying the buffer.
549This is used when turning off Font Lock mode.
550This is normally set via `font-lock-defaults'.")
551
552(defvar font-lock-fontify-region-function 'font-lock-default-fontify-region
553 "Function to use for fontifying a region.
554It should take two args, the beginning and end of the region, and an optional
555third arg VERBOSE. If non-nil, the function should print status messages.
556This is normally set via `font-lock-defaults'.")
557
558(defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
559 "Function to use for unfontifying a region.
560It should take two args, the beginning and end of the region.
561This is normally set via `font-lock-defaults'.")
562
563(defvar font-lock-inhibit-thing-lock nil
564 "List of Font Lock mode related modes that should not be turned on.
019f00d8 565Currently, valid mode names are `fast-lock-mode', `jit-lock-mode' and
e7f07c2c 566`lazy-lock-mode'. This is normally set via `font-lock-defaults'.")
a2b8e66b 567
f8115798
SM
568(defvar font-lock-multiline nil
569 "Whether font-lock should cater to multiline keywords.
570If nil, don't try to handle multiline patterns.
571If t, always handle multiline patterns.
572If `undecided', don't try to handle multiline patterns until you see one.
573Major/minor modes can set this variable if they know which option applies.")
574
575(defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
030f4a35 576\f
5aa29679
RS
577;; Font Lock mode.
578
579(eval-when-compile
2e6a15fc 580 ;;
5aa29679 581 ;; We don't do this at the top-level as we only use non-autoloaded macros.
2e6a15fc
SM
582 (require 'cl)
583 ;;
584 ;; Borrowed from lazy-lock.el.
585 ;; We use this to preserve or protect things when modifying text properties.
586 (defmacro save-buffer-state (varlist &rest body)
587 "Bind variables according to VARLIST and eval BODY restoring buffer state."
7b7a23f4
EZ
588 (let ((modified (make-symbol "modified")))
589 `(let* ,(append varlist
886c13f1 590 `((,modified (buffer-modified-p))
7b7a23f4
EZ
591 (buffer-undo-list t)
592 (inhibit-read-only t)
593 (inhibit-point-motion-hooks t)
594 (inhibit-modification-hooks t)
595 deactivate-mark
596 buffer-file-name
597 buffer-file-truename))
598 (progn
599 ,@body)
600 (unless ,modified
601 (restore-buffer-modified-p nil)))))
e0e277ff 602 (put 'save-buffer-state 'lisp-indent-function 1)
afa18a4e 603 (def-edebug-spec save-buffer-state let)
e0e277ff
SM
604 ;;
605 ;; Shut up the byte compiler.
1c172af4 606 (defvar font-lock-face-attributes)) ; Obsolete but respected if set.
030f4a35 607
f8115798
SM
608;;;###autoload
609(defun font-lock-mode-internal (arg)
610 ;; Turn on Font Lock mode.
611 (when arg
612 (add-hook 'after-change-functions 'font-lock-after-change-function t t)
613 (font-lock-set-defaults)
614 (font-lock-turn-on-thing-lock)
615 ;; Fontify the buffer if we have to.
616 (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size)))
617 (cond (font-lock-fontified
618 nil)
619 ((or (null max-size) (> max-size (buffer-size)))
620 (font-lock-fontify-buffer))
621 (font-lock-verbose
622 (message "Fontifying %s...buffer size greater than font-lock-maximum-size"
623 (buffer-name))))))
624 ;; Turn off Font Lock mode.
625 (unless font-lock-mode
626 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
627 (font-lock-unfontify-buffer)
628 (font-lock-turn-off-thing-lock)))
629
c1f2ffc8 630;;;###autoload
041470d2
SM
631(defun font-lock-add-keywords (mode keywords &optional append)
632 "Add highlighting KEYWORDS for MODE.
633MODE should be a symbol, the major mode command name, such as `c-mode'
1c626aaf 634or nil. If nil, highlighting keywords are added for the current buffer.
c1f2ffc8
SM
635KEYWORDS should be a list; see the variable `font-lock-keywords'.
636By default they are added at the beginning of the current highlighting list.
637If optional argument APPEND is `set', they are used to replace the current
2d63aa67
SM
638highlighting list. If APPEND is any other non-nil value, they are added at the
639end of the current highlighting list.
c1f2ffc8
SM
640
641For example:
642
643 (font-lock-add-keywords 'c-mode
644 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
645 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . font-lock-keyword-face)))
646
647adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
2d63aa67
SM
648comments, and to fontify `and', `or' and `not' words as keywords.
649
558371ba
SM
650When used from an elisp package (such as a minor mode), it is recommended
651to use nil for MODE (and place the call in a loop or on a hook) to avoid
652subtle problems due to details of the implementation.
653
8c2d6a44 654Note that some modes have specialized support for additional patterns, e.g.,
2d63aa67
SM
655see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
656`objc-font-lock-extra-types' and `java-font-lock-extra-types'."
041470d2
SM
657 (cond (mode
658 ;; If MODE is non-nil, add the KEYWORDS and APPEND spec to
c1f2ffc8
SM
659 ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
660 (let ((spec (cons keywords append)) cell)
041470d2 661 (if (setq cell (assq mode font-lock-keywords-alist))
76f5e2af
GM
662 (if (eq append 'set)
663 (setcdr cell (list spec))
664 (setcdr cell (append (cdr cell) (list spec))))
665 (push (list mode spec) font-lock-keywords-alist)))
666 ;; Make sure that `font-lock-removed-keywords-alist' does not
667 ;; contain the new keywords.
668 (font-lock-update-removed-keyword-alist mode keywords append))
6e1d0d15
SM
669 (t
670 ;; Otherwise set or add the keywords now.
671 (font-lock-set-defaults)
672 (if (eq append 'set)
673 (setq font-lock-keywords keywords)
674 (font-lock-remove-keywords nil keywords) ;to avoid duplicates
c1f2ffc8
SM
675 (let ((old (if (eq (car-safe font-lock-keywords) t)
676 (cdr font-lock-keywords)
677 font-lock-keywords)))
678 (setq font-lock-keywords (if append
679 (append old keywords)
680 (append keywords old))))))))
3708dfe9 681
6e1d0d15 682(defun font-lock-update-removed-keyword-alist (mode keywords append)
76f5e2af 683 ;; Update `font-lock-removed-keywords-alist' when adding new
6e1d0d15 684 ;; KEYWORDS to MODE.
76f5e2af
GM
685 ;;
686 ;; When font-lock is enabled first all keywords in the list
687 ;; `font-lock-keywords-alist' are added, then all keywords in the
688 ;; list `font-lock-removed-keywords-alist' are removed. If a
689 ;; keyword was once added, removed, and then added again it must be
690 ;; removed from the removed-keywords list. Otherwise the second add
691 ;; will not take effect.
6e1d0d15 692 (let ((cell (assq mode font-lock-removed-keywords-alist)))
76f5e2af
GM
693 (if cell
694 (if (eq append 'set)
695 ;; A new set of keywords is defined. Forget all about
696 ;; our old keywords that should be removed.
697 (setq font-lock-removed-keywords-alist
698 (delq cell font-lock-removed-keywords-alist))
699 ;; Delete all previously removed keywords.
700 (dolist (kword keywords)
701 (setcdr cell (delete kword (cdr cell))))
6e1d0d15 702 ;; Delete the mode cell if empty.
76f5e2af
GM
703 (if (null (cdr cell))
704 (setq font-lock-removed-keywords-alist
705 (delq cell font-lock-removed-keywords-alist)))))))
706
707;; Written by Anders Lindgren <andersl@andersl.com>.
708;;
709;; Case study:
710;; (I) The keywords are removed from a major mode.
711;; In this case the keyword could be local (i.e. added earlier by
712;; `font-lock-add-keywords'), global, or both.
713;;
714;; (a) In the local case we remove the keywords from the variable
715;; `font-lock-keywords-alist'.
716;;
717;; (b) The actual global keywords are not known at this time.
718;; All keywords are added to `font-lock-removed-keywords-alist',
719;; when font-lock is enabled those keywords are removed.
720;;
721;; Note that added keywords are taken out of the list of removed
722;; keywords. This ensure correct operation when the same keyword
723;; is added and removed several times.
724;;
725;; (II) The keywords are removed from the current buffer.
3708dfe9 726;;;###autoload
6e1d0d15
SM
727(defun font-lock-remove-keywords (mode keywords)
728 "Remove highlighting KEYWORDS for MODE.
3708dfe9 729
6e1d0d15 730MODE should be a symbol, the major mode command name, such as `c-mode'
558371ba
SM
731or nil. If nil, highlighting keywords are removed for the current buffer.
732
733When used from an elisp package (such as a minor mode), it is recommended
734to use nil for MODE (and place the call in a loop or on a hook) to avoid
735subtle problems due to details of the implementation."
6e1d0d15
SM
736 (cond (mode
737 ;; Remove one keyword at the time.
738 (dolist (keyword keywords)
739 (let ((top-cell (assq mode font-lock-keywords-alist)))
740 ;; If MODE is non-nil, remove the KEYWORD from
76f5e2af
GM
741 ;; `font-lock-keywords-alist'.
742 (when top-cell
743 (dolist (keyword-list-append-pair (cdr top-cell))
744 ;; `keywords-list-append-pair' is a cons with a list of
745 ;; keywords in the car top-cell and the original append
746 ;; argument in the cdr top-cell.
747 (setcar keyword-list-append-pair
748 (delete keyword (car keyword-list-append-pair))))
749 ;; Remove keyword list/append pair when the keyword list
750 ;; is empty and append doesn't specify `set'. (If it
751 ;; should be deleted then previously deleted keywords
752 ;; would appear again.)
753 (let ((cell top-cell))
754 (while (cdr cell)
755 (if (and (null (car (car (cdr cell))))
756 (not (eq (cdr (car (cdr cell))) 'set)))
757 (setcdr cell (cdr (cdr cell)))
758 (setq cell (cdr cell)))))
759 ;; Final cleanup, remove major mode cell if last keyword
760 ;; was deleted.
761 (if (null (cdr top-cell))
762 (setq font-lock-keywords-alist
763 (delq top-cell font-lock-keywords-alist))))
764 ;; Remember the keyword in case it is not local.
6e1d0d15 765 (let ((cell (assq mode font-lock-removed-keywords-alist)))
76f5e2af
GM
766 (if cell
767 (unless (member keyword (cdr cell))
768 (nconc cell (list keyword)))
6e1d0d15
SM
769 (push (cons mode (list keyword))
770 font-lock-removed-keywords-alist))))))
771 (t
772 ;; Otherwise remove it immediately.
773 (font-lock-set-defaults)
774 (setq font-lock-keywords (copy-sequence font-lock-keywords))
775 (dolist (keyword keywords)
76f5e2af 776 (setq font-lock-keywords
6e1d0d15
SM
777 (delete keyword
778 ;; The keywords might be compiled.
779 (delete (font-lock-compile-keyword keyword)
780 font-lock-keywords)))))))
a2b8e66b 781\f
2d63aa67
SM
782;;; Font Lock Support mode.
783
5aa29679
RS
784;; This is the code used to interface font-lock.el with any of its add-on
785;; packages, and provide the user interface. Packages that have their own
786;; local buffer fontification functions (see below) may have to call
787;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
788;; themselves.
789
5f999b0b 790(defcustom font-lock-support-mode 'jit-lock-mode
5aa29679
RS
791 "*Support mode for Font Lock mode.
792Support modes speed up Font Lock mode by being choosy about when fontification
e7f07c2c
GM
793occurs. Known support modes are Fast Lock mode (symbol `fast-lock-mode'),
794Lazy Lock mode (symbol `lazy-lock-mode'), and Just-in-time Lock mode (symbol
795`jit-lock-mode'. See those modes for more info.
5aa29679
RS
796If nil, means support for Font Lock mode is never performed.
797If a symbol, use that support mode.
798If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
799where MAJOR-MODE is a symbol or t (meaning the default). For example:
800 ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
801means that Fast Lock mode is used to support Font Lock mode for buffers in C or
802C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
803
55015061 804The value of this variable is used when Font Lock mode is turned on."
b34aa0a3
SM
805 :type '(choice (const :tag "none" nil)
806 (const :tag "fast lock" fast-lock-mode)
807 (const :tag "lazy lock" lazy-lock-mode)
e7f07c2c 808 (const :tag "jit lock" jit-lock-mode)
b34aa0a3 809 (repeat :menu-tag "mode specific" :tag "mode specific"
1c172af4 810 :value ((t . jit-lock-mode))
b34aa0a3
SM
811 (cons :tag "Instance"
812 (radio :tag "Mode"
813 (const :tag "all" t)
814 (symbol :tag "name"))
39df451b
SM
815 (radio :tag "Support"
816 (const :tag "none" nil)
b34aa0a3 817 (const :tag "fast lock" fast-lock-mode)
e7f07c2c
GM
818 (const :tag "lazy lock" lazy-lock-mode)
819 (const :tag "JIT lock" jit-lock-mode)))
b34aa0a3 820 ))
c69e5fcd 821 :version "21.1"
55015061 822 :group 'font-lock)
5aa29679 823
7f0d55f2
SM
824(defvar fast-lock-mode)
825(defvar lazy-lock-mode)
826(defvar jit-lock-mode)
2e6a15fc 827
5aa29679
RS
828(defun font-lock-turn-on-thing-lock ()
829 (let ((thing-mode (font-lock-value-in-major-mode font-lock-support-mode)))
830 (cond ((eq thing-mode 'fast-lock-mode)
831 (fast-lock-mode t))
832 ((eq thing-mode 'lazy-lock-mode)
e7f07c2c
GM
833 (lazy-lock-mode t))
834 ((eq thing-mode 'jit-lock-mode)
28a53bc1
SM
835 ;; Prepare for jit-lock
836 (remove-hook 'after-change-functions
837 'font-lock-after-change-function t)
838 (set (make-local-variable 'font-lock-fontify-buffer-function)
fd612dd9 839 'jit-lock-refontify)
28a53bc1
SM
840 ;; Don't fontify eagerly (and don't abort is the buffer is large).
841 (set (make-local-variable 'font-lock-fontified) t)
842 ;; Use jit-lock.
843 (jit-lock-register 'font-lock-fontify-region
844 (not font-lock-keywords-only))))))
5aa29679 845
5aa29679 846(defun font-lock-turn-off-thing-lock ()
7f0d55f2 847 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
1c172af4 848 (fast-lock-mode -1))
7f0d55f2 849 ((and (boundp 'jit-lock-mode) jit-lock-mode)
28a53bc1
SM
850 (jit-lock-unregister 'font-lock-fontify-region)
851 ;; Reset local vars to the non-jit-lock case.
852 (kill-local-variable 'font-lock-fontify-buffer-function))
7f0d55f2 853 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
1c172af4 854 (lazy-lock-mode -1))))
5aa29679
RS
855
856(defun font-lock-after-fontify-buffer ()
7f0d55f2 857 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
5aa29679 858 (fast-lock-after-fontify-buffer))
13f0d185
SM
859 ;; Useless now that jit-lock intercepts font-lock-fontify-buffer. -sm
860 ;; (jit-lock-mode
861 ;; (jit-lock-after-fontify-buffer))
7f0d55f2 862 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
5aa29679
RS
863 (lazy-lock-after-fontify-buffer))))
864
865(defun font-lock-after-unfontify-buffer ()
7f0d55f2 866 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
5aa29679 867 (fast-lock-after-unfontify-buffer))
13f0d185
SM
868 ;; Useless as well. It's only called when:
869 ;; - turning off font-lock: it does not matter if we leave spurious
870 ;; `fontified' text props around since jit-lock-mode is also off.
871 ;; - font-lock-default-fontify-buffer fails: this is not run
872 ;; any more anyway. -sm
5e9e032a 873 ;;
13f0d185
SM
874 ;; (jit-lock-mode
875 ;; (jit-lock-after-unfontify-buffer))
7f0d55f2 876 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
5aa29679
RS
877 (lazy-lock-after-unfontify-buffer))))
878
2d63aa67 879;;; End of Font Lock Support mode.
5aa29679 880\f
2d63aa67 881;;; Fontification functions.
a1eb1cf1 882
56fcbd7e
SM
883;; Rather than the function, e.g., `font-lock-fontify-region' containing the
884;; code to fontify a region, the function runs the function whose name is the
885;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally,
886;; the value of this variable is, e.g., `font-lock-default-fontify-region'
887;; which does contain the code to fontify a region. However, the value of the
888;; variable could be anything and thus, e.g., `font-lock-fontify-region' could
889;; do anything. The indirection of the fontification functions gives major
890;; modes the capability of modifying the way font-lock.el fontifies. Major
891;; modes can modify the values of, e.g., `font-lock-fontify-region-function',
892;; via the variable `font-lock-defaults'.
893;;
894;; For example, Rmail mode sets the variable `font-lock-defaults' so that
895;; font-lock.el uses its own function for buffer fontification. This function
896;; makes fontification be on a message-by-message basis and so visiting an
897;; RMAIL file is much faster. A clever implementation of the function might
898;; fontify the headers differently than the message body. (It should, and
899;; correspondingly for Mail mode, but I can't be bothered to do the work. Can
900;; you?) This hints at a more interesting use...
901;;
902;; Languages that contain text normally contained in different major modes
903;; could define their own fontification functions that treat text differently
904;; depending on its context. For example, Perl mode could arrange that here
905;; docs are fontified differently than Perl code. Or Yacc mode could fontify
906;; rules one way and C code another. Neat!
907;;
908;; A further reason to use the fontification indirection feature is when the
909;; default syntactual fontification, or the default fontification in general,
910;; is not flexible enough for a particular major mode. For example, perhaps
55015061
SM
911;; comments are just too hairy for `font-lock-fontify-syntactically-region' to
912;; cope with. You need to write your own version of that function, e.g.,
913;; `hairy-fontify-syntactically-region', and make your own version of
914;; `hairy-fontify-region' call that function before calling
56fcbd7e
SM
915;; `font-lock-fontify-keywords-region' for the normal regexp fontification
916;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el
917;; would call your region fontification function instead of its own. For
55015061 918;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line
56fcbd7e
SM
919;; directives correctly and cleanly. (It is the same problem as fontifying
920;; multi-line strings and comments; regexps are not appropriate for the job.)
921
a1eb1cf1 922;;;###autoload
030f4a35 923(defun font-lock-fontify-buffer ()
019f00d8 924 "Fontify the current buffer the way the function `font-lock-mode' would."
030f4a35 925 (interactive)
a2b8e66b
SM
926 (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
927 (funcall font-lock-fontify-buffer-function)))
928
929(defun font-lock-unfontify-buffer ()
930 (funcall font-lock-unfontify-buffer-function))
931
932(defun font-lock-fontify-region (beg end &optional loudly)
933 (funcall font-lock-fontify-region-function beg end loudly))
934
935(defun font-lock-unfontify-region (beg end)
936 (funcall font-lock-unfontify-region-function beg end))
937
938(defun font-lock-default-fontify-buffer ()
5aa29679
RS
939 (let ((verbose (if (numberp font-lock-verbose)
940 (> (buffer-size) font-lock-verbose)
941 font-lock-verbose)))
79f238c9 942 (with-temp-message
1787769b
SM
943 (when verbose
944 (format "Fontifying %s..." (buffer-name)))
79f238c9
SM
945 ;; Make sure we have the right `font-lock-keywords' etc.
946 (unless font-lock-mode
947 (font-lock-set-defaults))
948 ;; Make sure we fontify etc. in the whole buffer.
949 (save-restriction
950 (widen)
951 (condition-case nil
952 (save-excursion
953 (save-match-data
954 (font-lock-fontify-region (point-min) (point-max) verbose)
955 (font-lock-after-fontify-buffer)
956 (setq font-lock-fontified t)))
957 ;; We don't restore the old fontification, so it's best to unfontify.
8259bf10 958 (quit (font-lock-unfontify-buffer)))))))
7d7d915a 959
a2b8e66b 960(defun font-lock-default-unfontify-buffer ()
56fcbd7e 961 ;; Make sure we unfontify etc. in the whole buffer.
a2b8e66b
SM
962 (save-restriction
963 (widen)
964 (font-lock-unfontify-region (point-min) (point-max))
271c888a 965 (font-lock-after-unfontify-buffer)
a2b8e66b 966 (setq font-lock-fontified nil)))
9bfbb130 967
7f0d55f2
SM
968(defvar font-lock-dont-widen nil
969 "If non-nil, font-lock will work on the non-widened buffer.
970Useful for things like RMAIL and Info where the whole buffer is not
971a very meaningful entity to highlight.")
972
a2b8e66b 973(defun font-lock-default-fontify-region (beg end loudly)
ac6e572c
SM
974 (save-buffer-state
975 ((parse-sexp-lookup-properties font-lock-syntactic-keywords)
976 (old-syntax-table (syntax-table)))
876f2438 977 (unwind-protect
a078558d 978 (save-restriction
7f0d55f2 979 (unless font-lock-dont-widen (widen))
876f2438 980 ;; Use the fontification syntax table, if any.
2e6a15fc
SM
981 (when font-lock-syntax-table
982 (set-syntax-table font-lock-syntax-table))
fb0e4f8a
RS
983 ;; check to see if we should expand the beg/end area for
984 ;; proper multiline matches
f9261020
GM
985 (when (and font-lock-multiline
986 (> beg (point-min))
28a53bc1
SM
987 (get-text-property (1- beg) 'font-lock-multiline))
988 ;; We are just after or in a multiline match.
989 (setq beg (or (previous-single-property-change
990 beg 'font-lock-multiline)
ba22aeff
SM
991 (point-min)))
992 (goto-char beg)
993 (setq beg (line-beginning-position)))
111c181e
GM
994 (when font-lock-multiline
995 (setq end (or (text-property-any end (point-max)
996 'font-lock-multiline nil)
997 (point-max))))
ba22aeff 998 (goto-char end)
724a330f 999 (setq end (line-beginning-position 2))
876f2438 1000 ;; Now do the fontification.
2e6a15fc 1001 (font-lock-unfontify-region beg end)
ac6e572c
SM
1002 (when font-lock-syntactic-keywords
1003 (font-lock-fontify-syntactic-keywords-region beg end))
2e6a15fc 1004 (unless font-lock-keywords-only
876f2438
SM
1005 (font-lock-fontify-syntactically-region beg end loudly))
1006 (font-lock-fontify-keywords-region beg end loudly))
1007 ;; Clean up.
2e6a15fc 1008 (set-syntax-table old-syntax-table))))
9bfbb130
SM
1009
1010;; The following must be rethought, since keywords can override fontification.
1011; ;; Now scan for keywords, but not if we are inside a comment now.
1012; (or (and (not font-lock-keywords-only)
019f00d8 1013; (let ((state (parse-partial-sexp beg end nil nil
9bfbb130
SM
1014; font-lock-cache-state)))
1015; (or (nth 4 state) (nth 7 state))))
1016; (font-lock-fontify-keywords-region beg end))
1017
8259bf10
SM
1018(defvar font-lock-extra-managed-props nil
1019 "Additional text properties managed by font-lock.
1020This is used by `font-lock-default-unfontify-region' to decide
4c02ca46 1021what properties to clear before refontifying a region.")
8259bf10 1022
a2b8e66b 1023(defun font-lock-default-unfontify-region (beg end)
2e6a15fc 1024 (save-buffer-state nil
4c02ca46 1025 (remove-list-of-text-properties
8259bf10
SM
1026 beg end (append
1027 font-lock-extra-managed-props
1028 (if font-lock-syntactic-keywords
4c02ca46
SM
1029 '(syntax-table face font-lock-multiline)
1030 '(face font-lock-multiline))))))
9bfbb130
SM
1031
1032;; Called when any modification is made to buffer text.
1033(defun font-lock-after-change-function (beg end old-len)
14e2791a
RS
1034 (let ((inhibit-point-motion-hooks t)
1035 (inhibit-quit t))
46f26fcf
SM
1036 (save-excursion
1037 (save-match-data
1038 ;; Rescan between start of lines enclosing the region.
1039 (font-lock-fontify-region
1040 (progn (goto-char beg) (beginning-of-line) (point))
1041 (progn (goto-char end) (forward-line 1) (point)))))))
a2b8e66b 1042
a078558d
SM
1043(defun font-lock-fontify-block (&optional arg)
1044 "Fontify some lines the way `font-lock-fontify-buffer' would.
1045The lines could be a function or paragraph, or a specified number of lines.
a078558d 1046If ARG is given, fontify that many lines before and after point, or 16 lines if
a465832f
SM
1047no ARG is given and `font-lock-mark-block-function' is nil.
1048If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
1049delimit the region to fontify."
a078558d 1050 (interactive "P")
46f26fcf
SM
1051 (let ((inhibit-point-motion-hooks t) font-lock-beginning-of-syntax-function
1052 deactivate-mark)
a078558d
SM
1053 ;; Make sure we have the right `font-lock-keywords' etc.
1054 (if (not font-lock-mode) (font-lock-set-defaults))
a2b8e66b
SM
1055 (save-excursion
1056 (save-match-data
1057 (condition-case error-data
a078558d
SM
1058 (if (or arg (not font-lock-mark-block-function))
1059 (let ((lines (if arg (prefix-numeric-value arg) 16)))
1060 (font-lock-fontify-region
1061 (save-excursion (forward-line (- lines)) (point))
1062 (save-excursion (forward-line lines) (point))))
1063 (funcall font-lock-mark-block-function)
1064 (font-lock-fontify-region (point) (mark)))
6c0fc428 1065 ((error quit) (message "Fontifying block...%s" error-data)))))))
a078558d 1066
7f0d55f2
SM
1067(if (boundp 'facemenu-keymap)
1068 (define-key facemenu-keymap "\M-g" 'font-lock-fontify-block))
2d63aa67
SM
1069
1070;;; End of Fontification functions.
9bfbb130 1071\f
9bfbb130
SM
1072;;; Additional text property functions.
1073
1c626aaf
SM
1074;; The following text property functions should be builtins. This means they
1075;; should be written in C and put with all the other text property functions.
1076;; In the meantime, those that are used by font-lock.el are defined in Lisp
1077;; below and given a `font-lock-' prefix. Those that are not used are defined
1078;; in Lisp below and commented out. sm.
9bfbb130 1079
1c626aaf 1080(defun font-lock-prepend-text-property (start end prop value &optional object)
9bfbb130
SM
1081 "Prepend to one property of the text from START to END.
1082Arguments PROP and VALUE specify the property and value to prepend to the value
1c626aaf 1083already in place. The resulting property values are always lists.
9bfbb130
SM
1084Optional argument OBJECT is the string or buffer containing the text."
1085 (let ((val (if (listp value) value (list value))) next prev)
1086 (while (/= start end)
1087 (setq next (next-single-property-change start prop object end)
1088 prev (get-text-property start prop object))
1c626aaf
SM
1089 (put-text-property start next prop
1090 (append val (if (listp prev) prev (list prev)))
1091 object)
9bfbb130
SM
1092 (setq start next))))
1093
1c626aaf 1094(defun font-lock-append-text-property (start end prop value &optional object)
9bfbb130
SM
1095 "Append to one property of the text from START to END.
1096Arguments PROP and VALUE specify the property and value to append to the value
1c626aaf 1097already in place. The resulting property values are always lists.
9bfbb130
SM
1098Optional argument OBJECT is the string or buffer containing the text."
1099 (let ((val (if (listp value) value (list value))) next prev)
1100 (while (/= start end)
1101 (setq next (next-single-property-change start prop object end)
1102 prev (get-text-property start prop object))
1c626aaf
SM
1103 (put-text-property start next prop
1104 (append (if (listp prev) prev (list prev)) val)
1105 object)
9bfbb130 1106 (setq start next))))
1c626aaf
SM
1107
1108(defun font-lock-fillin-text-property (start end prop value &optional object)
1109 "Fill in one property of the text from START to END.
1110Arguments PROP and VALUE specify the property and value to put where none are
1111already in place. Therefore existing property values are not overwritten.
1112Optional argument OBJECT is the string or buffer containing the text."
1113 (let ((start (text-property-any start end prop nil object)) next)
1114 (while start
1115 (setq next (next-single-property-change start prop object end))
1116 (put-text-property start next prop value object)
1117 (setq start (text-property-any next end prop nil object)))))
1118
1119;; For completeness: this is to `remove-text-properties' as `put-text-property'
1120;; is to `add-text-properties', etc.
1121;(defun remove-text-property (start end property &optional object)
1122; "Remove a property from text from START to END.
1123;Argument PROPERTY is the property to remove.
1124;Optional argument OBJECT is the string or buffer containing the text.
1125;Return t if the property was actually removed, nil otherwise."
1126; (remove-text-properties start end (list property) object))
1127
1128;; For consistency: maybe this should be called `remove-single-property' like
1129;; `next-single-property-change' (not `next-single-text-property-change'), etc.
1130;(defun remove-single-text-property (start end prop value &optional object)
1131; "Remove a specific property value from text from START to END.
1132;Arguments PROP and VALUE specify the property and value to remove. The
1133;resulting property values are not equal to VALUE nor lists containing VALUE.
1134;Optional argument OBJECT is the string or buffer containing the text."
1135; (let ((start (text-property-not-all start end prop nil object)) next prev)
1136; (while start
1137; (setq next (next-single-property-change start prop object end)
1138; prev (get-text-property start prop object))
1139; (cond ((and (symbolp prev) (eq value prev))
1140; (remove-text-property start next prop object))
1141; ((and (listp prev) (memq value prev))
1142; (let ((new (delq value prev)))
1143; (cond ((null new)
1144; (remove-text-property start next prop object))
1145; ((= (length new) 1)
1146; (put-text-property start next prop (car new) object))
1147; (t
1148; (put-text-property start next prop new object))))))
1149; (setq start (text-property-not-all next end prop nil object)))))
2d63aa67
SM
1150
1151;;; End of Additional text property functions.
9bfbb130 1152\f
ac6e572c
SM
1153;;; Syntactic regexp fontification functions.
1154
1155;; These syntactic keyword pass functions are identical to those keyword pass
1156;; functions below, with the following exceptions; (a) they operate on
1157;; `font-lock-syntactic-keywords' of course, (b) they are all `defun' as speed
1158;; is less of an issue, (c) eval of property value does not occur JIT as speed
1159;; is less of an issue, (d) OVERRIDE cannot be `prepend' or `append' as it
1160;; makes no sense for `syntax-table' property values, (e) they do not do it
1161;; LOUDLY as it is not likely to be intensive.
1162
1163(defun font-lock-apply-syntactic-highlight (highlight)
1164 "Apply HIGHLIGHT following a match.
1165HIGHLIGHT should be of the form MATCH-HIGHLIGHT,
1166see `font-lock-syntactic-keywords'."
1167 (let* ((match (nth 0 highlight))
1168 (start (match-beginning match)) (end (match-end match))
1169 (value (nth 1 highlight))
1170 (override (nth 2 highlight)))
8259bf10
SM
1171 (if (not start)
1172 ;; No match but we might not signal an error.
1173 (or (nth 3 highlight)
1174 (error "No match %d in highlight %S" match highlight))
1175 (when (and (consp value) (not (numberp (car value))))
1176 (setq value (eval value)))
1177 (when (stringp value) (setq value (string-to-syntax value)))
1178 ;; Flush the syntax-cache. I believe this is not necessary for
1179 ;; font-lock's use of syntax-ppss, but I'm not 100% sure and it can
1180 ;; still be necessary for other users of syntax-ppss anyway.
1181 (syntax-ppss-after-change-function start)
1182 (cond
1183 ((not override)
1184 ;; Cannot override existing fontification.
1185 (or (text-property-not-all start end 'syntax-table nil)
1186 (put-text-property start end 'syntax-table value)))
1187 ((eq override t)
1188 ;; Override existing fontification.
1189 (put-text-property start end 'syntax-table value))
1190 ((eq override 'keep)
1191 ;; Keep existing fontification.
1192 (font-lock-fillin-text-property start end 'syntax-table value))))))
ac6e572c
SM
1193
1194(defun font-lock-fontify-syntactic-anchored-keywords (keywords limit)
1195 "Fontify according to KEYWORDS until LIMIT.
1196KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1197LIMIT can be modified by the value of its PRE-MATCH-FORM."
1198 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1199 ;; Evaluate PRE-MATCH-FORM.
1200 (pre-match-value (eval (nth 1 keywords))))
1201 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1202 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1203 (setq limit pre-match-value)
6e1d0d15 1204 (setq limit (line-end-position)))
ac6e572c
SM
1205 (save-match-data
1206 ;; Find an occurrence of `matcher' before `limit'.
1207 (while (if (stringp matcher)
1208 (re-search-forward matcher limit t)
1209 (funcall matcher limit))
1210 ;; Apply each highlight to this instance of `matcher'.
1211 (setq highlights lowdarks)
1212 (while highlights
1213 (font-lock-apply-syntactic-highlight (car highlights))
1214 (setq highlights (cdr highlights)))))
1215 ;; Evaluate POST-MATCH-FORM.
1216 (eval (nth 2 keywords))))
1217
1218(defun font-lock-fontify-syntactic-keywords-region (start end)
1219 "Fontify according to `font-lock-syntactic-keywords' between START and END.
1220START should be at the beginning of a line."
c0a6a9fe
SM
1221 ;; Ensure the beginning of the file is properly syntactic-fontified.
1222 (when (and font-lock-syntactically-fontified
1223 (< font-lock-syntactically-fontified start))
1224 (setq start (max font-lock-syntactically-fontified (point-min)))
1225 (setq font-lock-syntactically-fontified end))
ac6e572c
SM
1226 ;; If `font-lock-syntactic-keywords' is a symbol, get the real keywords.
1227 (when (symbolp font-lock-syntactic-keywords)
1228 (setq font-lock-syntactic-keywords (font-lock-eval-keywords
1229 font-lock-syntactic-keywords)))
1230 ;; If `font-lock-syntactic-keywords' is not compiled, compile it.
1231 (unless (eq (car font-lock-syntactic-keywords) t)
1232 (setq font-lock-syntactic-keywords (font-lock-compile-keywords
1233 font-lock-syntactic-keywords)))
1234 ;; Get down to business.
1235 (let ((case-fold-search font-lock-keywords-case-fold-search)
1236 (keywords (cdr font-lock-syntactic-keywords))
1237 keyword matcher highlights)
1238 (while keywords
1239 ;; Find an occurrence of `matcher' from `start' to `end'.
1240 (setq keyword (car keywords) matcher (car keyword))
1241 (goto-char start)
1242 (while (if (stringp matcher)
1243 (re-search-forward matcher end t)
1244 (funcall matcher end))
1245 ;; Apply each highlight to this instance of `matcher', which may be
1246 ;; specific highlights or more keywords anchored to `matcher'.
1247 (setq highlights (cdr keyword))
1248 (while highlights
1249 (if (numberp (car (car highlights)))
1250 (font-lock-apply-syntactic-highlight (car highlights))
1251 (font-lock-fontify-syntactic-anchored-keywords (car highlights)
1252 end))
1253 (setq highlights (cdr highlights))))
1254 (setq keywords (cdr keywords)))))
1255
1256;;; End of Syntactic regexp fontification functions.
1257\f
1258;;; Syntactic fontification functions.
1259
8259bf10 1260(defun font-lock-fontify-syntactically-region (start end &optional loudly ppss)
ac6e572c
SM
1261 "Put proper face on each string and comment between START and END.
1262START should be at the beginning of a line."
8259bf10 1263 (let (state face beg)
ac6e572c
SM
1264 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
1265 (goto-char start)
1266 ;;
1267 ;; Find the state at the `beginning-of-line' before `start'.
47fc2db3 1268 (setq state (or ppss (syntax-ppss start)))
ac6e572c
SM
1269 ;;
1270 ;; Find each interesting place between here and `end'.
8259bf10
SM
1271 (while
1272 (progn
1273 (when (or (nth 3 state) (nth 4 state))
1274 (setq face (funcall font-lock-syntactic-face-function state))
1275 (setq beg (max (nth 8 state) start))
1276 (setq state (parse-partial-sexp (point) end nil nil state
1277 'syntax-table))
1278 (when face (put-text-property beg (point) 'face face)))
1279 (setq state (parse-partial-sexp (point) end nil nil state
1280 'syntax-table))
1281 (< (point) end)))))
ac6e572c
SM
1282
1283;;; End of Syntactic fontification functions.
1284\f
1285;;; Keyword regexp fontification functions.
9bfbb130
SM
1286
1287(defsubst font-lock-apply-highlight (highlight)
1288 "Apply HIGHLIGHT following a match.
1289HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
1290 (let* ((match (nth 0 highlight))
1291 (start (match-beginning match)) (end (match-end match))
1292 (override (nth 2 highlight)))
8259bf10
SM
1293 (if (not start)
1294 ;; No match but we might not signal an error.
1295 (or (nth 3 highlight)
1296 (error "No match %d in highlight %S" match highlight))
1297 (let ((val (eval (nth 1 highlight))))
1298 (when (eq (car-safe val) 'face)
1299 (add-text-properties start end (cddr val))
1300 (setq val (cadr val)))
1301 (cond
1302 ((not override)
1303 ;; Cannot override existing fontification.
1304 (or (text-property-not-all start end 'face nil)
1305 (put-text-property start end 'face val)))
1306 ((eq override t)
1307 ;; Override existing fontification.
1308 (put-text-property start end 'face val))
1309 ((eq override 'prepend)
1310 ;; Prepend to existing fontification.
1311 (font-lock-prepend-text-property start end 'face val))
1312 ((eq override 'append)
1313 ;; Append to existing fontification.
1314 (font-lock-append-text-property start end 'face val))
1315 ((eq override 'keep)
1316 ;; Keep existing fontification.
1317 (font-lock-fillin-text-property start end 'face val)))))))
9bfbb130
SM
1318
1319(defsubst font-lock-fontify-anchored-keywords (keywords limit)
1320 "Fontify according to KEYWORDS until LIMIT.
56fcbd7e
SM
1321KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1322LIMIT can be modified by the value of its PRE-MATCH-FORM."
1323 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
fb0e4f8a 1324 (lead-start (match-beginning 0))
56fcbd7e
SM
1325 ;; Evaluate PRE-MATCH-FORM.
1326 (pre-match-value (eval (nth 1 keywords))))
1327 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
fb0e4f8a 1328 (if (not (and (numberp pre-match-value) (> pre-match-value (point))))
6e1d0d15 1329 (setq limit (line-end-position))
fb0e4f8a 1330 (setq limit pre-match-value)
ba22aeff 1331 (when (and font-lock-multiline (>= limit (line-beginning-position 2)))
fb0e4f8a 1332 ;; this is a multiline anchored match
ba22aeff
SM
1333 ;; (setq font-lock-multiline t)
1334 (put-text-property (if (= limit (line-beginning-position 2))
1335 (1- limit)
1336 (min lead-start (point)))
1337 limit
28a53bc1 1338 'font-lock-multiline t)))
9bfbb130 1339 (save-match-data
876f2438 1340 ;; Find an occurrence of `matcher' before `limit'.
afa18a4e
SM
1341 (while (and (< (point) limit)
1342 (if (stringp matcher)
1343 (re-search-forward matcher limit t)
1344 (funcall matcher limit)))
876f2438 1345 ;; Apply each highlight to this instance of `matcher'.
9bfbb130
SM
1346 (setq highlights lowdarks)
1347 (while highlights
1348 (font-lock-apply-highlight (car highlights))
1349 (setq highlights (cdr highlights)))))
876f2438 1350 ;; Evaluate POST-MATCH-FORM.
9bfbb130
SM
1351 (eval (nth 2 keywords))))
1352
1353(defun font-lock-fontify-keywords-region (start end &optional loudly)
1354 "Fontify according to `font-lock-keywords' between START and END.
7f0d55f2
SM
1355START should be at the beginning of a line.
1356LOUDLY, if non-nil, allows progress-meter bar."
ac6e572c 1357 (unless (eq (car font-lock-keywords) t)
2d5eba0e
SM
1358 (setq font-lock-keywords
1359 (font-lock-compile-keywords font-lock-keywords t)))
9bfbb130 1360 (let ((case-fold-search font-lock-keywords-case-fold-search)
55015061 1361 (keywords (cdr font-lock-keywords))
9bfbb130 1362 (bufname (buffer-name)) (count 0)
876f2438
SM
1363 keyword matcher highlights)
1364 ;;
1365 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1366 (while keywords
1367 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
2e6a15fc 1368 (make-string (incf count) ?.)))
9bfbb130 1369 ;;
876f2438
SM
1370 ;; Find an occurrence of `matcher' from `start' to `end'.
1371 (setq keyword (car keywords) matcher (car keyword))
1372 (goto-char start)
e7f07c2c
GM
1373 (while (and (< (point) end)
1374 (if (stringp matcher)
1375 (re-search-forward matcher end t)
1376 (funcall matcher end)))
041470d2 1377 (when (and font-lock-multiline
ba22aeff
SM
1378 (>= (point)
1379 (save-excursion (goto-char (match-beginning 0))
1380 (forward-line 1) (point))))
fb0e4f8a 1381 ;; this is a multiline regexp match
ba22aeff
SM
1382 ;; (setq font-lock-multiline t)
1383 (put-text-property (if (= (point)
1384 (save-excursion
1385 (goto-char (match-beginning 0))
1386 (forward-line 1) (point)))
1387 (1- (point))
1388 (match-beginning 0))
1389 (point)
fb0e4f8a 1390 'font-lock-multiline t))
876f2438
SM
1391 ;; Apply each highlight to this instance of `matcher', which may be
1392 ;; specific highlights or more keywords anchored to `matcher'.
1393 (setq highlights (cdr keyword))
1394 (while highlights
1395 (if (numberp (car (car highlights)))
1396 (font-lock-apply-highlight (car highlights))
1397 (font-lock-fontify-anchored-keywords (car highlights) end))
1398 (setq highlights (cdr highlights))))
1399 (setq keywords (cdr keywords)))))
2d63aa67 1400
ac6e572c 1401;;; End of Keyword regexp fontification functions.
9bfbb130
SM
1402\f
1403;; Various functions.
1404
2d5eba0e 1405(defun font-lock-compile-keywords (keywords &optional regexp)
019f00d8
DL
1406 "Compile KEYWORDS into the form (t KEYWORD ...).
1407Here KEYWORD is of the form (MATCHER HIGHLIGHT ...) as shown in the
2d5eba0e
SM
1408`font-lock-keywords' doc string.
1409If REGEXP is non-nil, it means these keywords are used for
1410`font-lock-keywords' rather than for `font-lock-syntactic-keywords'."
55015061
SM
1411 (if (eq (car-safe keywords) t)
1412 keywords
2d5eba0e
SM
1413 (setq keywords (cons t (mapcar 'font-lock-compile-keyword keywords)))
1414 (if (and regexp
1415 (eq (or syntax-begin-function
1416 font-lock-beginning-of-syntax-function)
1417 'beginning-of-defun)
1418 (not beginning-of-defun-function))
1419 ;; Try to detect when a string or comment contains something that
1420 ;; looks like a defun and would thus confuse font-lock.
1421 (nconc keywords
1422 `((,(if defun-prompt-regexp
1423 (concat "^\\(?:" defun-prompt-regexp "\\)?\\s(")
1424 "^\\s(")
1425 (0
47fc2db3 1426 (if (memq (get-text-property (1- (point)) 'face)
2d5eba0e
SM
1427 '(font-lock-string-face font-lock-doc-face
1428 font-lock-comment-face))
1429 font-lock-warning-face)
1430 prepend)))))
1431 keywords))
a2b8e66b
SM
1432
1433(defun font-lock-compile-keyword (keyword)
c1f2ffc8 1434 (cond ((nlistp keyword) ; MATCHER
a2b8e66b 1435 (list keyword '(0 font-lock-keyword-face)))
c1f2ffc8 1436 ((eq (car keyword) 'eval) ; (eval . FORM)
a2b8e66b 1437 (font-lock-compile-keyword (eval (cdr keyword))))
c1f2ffc8
SM
1438 ((eq (car-safe (cdr keyword)) 'quote) ; (MATCHER . 'FORM)
1439 ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote.
1440 (if (symbolp (nth 2 keyword))
1441 (list (car keyword) (list 0 (cdr keyword)))
1442 (font-lock-compile-keyword (cons (car keyword) (nth 2 keyword)))))
1443 ((numberp (cdr keyword)) ; (MATCHER . MATCH)
a2b8e66b 1444 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
c1f2ffc8 1445 ((symbolp (cdr keyword)) ; (MATCHER . FACENAME)
a2b8e66b 1446 (list (car keyword) (list 0 (cdr keyword))))
c1f2ffc8 1447 ((nlistp (nth 1 keyword)) ; (MATCHER . HIGHLIGHT)
a2b8e66b 1448 (list (car keyword) (cdr keyword)))
c1f2ffc8 1449 (t ; (MATCHER HIGHLIGHT ...)
a2b8e66b 1450 keyword)))
d46c21ec 1451
ac6e572c 1452(defun font-lock-eval-keywords (keywords)
019f00d8 1453 "Evalulate KEYWORDS if a function (funcall) or variable (eval) name."
46f26fcf
SM
1454 (if (listp keywords)
1455 keywords
1456 (font-lock-eval-keywords (if (fboundp keywords)
1457 (funcall keywords)
1458 (eval keywords)))))
ac6e572c 1459
343204bd 1460(defun font-lock-value-in-major-mode (alist)
019f00d8
DL
1461 "Return value in ALIST for `major-mode', or ALIST if it is not an alist.
1462Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t."
343204bd
SM
1463 (if (consp alist)
1464 (cdr (or (assq major-mode alist) (assq t alist)))
1465 alist))
1466
d46c21ec 1467(defun font-lock-choose-keywords (keywords level)
019f00d8
DL
1468 "Return LEVELth element of KEYWORDS.
1469A LEVEL of nil is equal to a LEVEL of 0, a LEVEL of t is equal to
1470\(1- (length KEYWORDS))."
fd612dd9 1471 (cond ((not (and (listp keywords) (symbolp (car keywords))))
343204bd
SM
1472 keywords)
1473 ((numberp level)
1474 (or (nth level keywords) (car (reverse keywords))))
1475 ((eq level t)
1476 (car (reverse keywords)))
1477 (t
1478 (car keywords))))
d46c21ec 1479
f8115798
SM
1480(defvar font-lock-set-defaults nil) ; Whether we have set up defaults.
1481
1482(defun font-lock-set-defaults ()
1483 "Set fontification defaults appropriately for this mode.
1484Sets various variables using `font-lock-defaults' (or, if nil, using
1485`font-lock-defaults-alist') and `font-lock-maximum-decoration'."
1486 ;; Set fontification defaults iff not previously set.
1487 (unless font-lock-set-defaults
1488 (set (make-local-variable 'font-lock-set-defaults) t)
1489 (make-local-variable 'font-lock-fontified)
1490 (make-local-variable 'font-lock-multiline)
1491 (let* ((defaults (or font-lock-defaults
1492 (cdr (assq major-mode font-lock-defaults-alist))))
1493 (keywords
1494 (font-lock-choose-keywords (nth 0 defaults)
1495 (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1496 (local (cdr (assq major-mode font-lock-keywords-alist)))
1497 (removed-keywords
1498 (cdr-safe (assq major-mode font-lock-removed-keywords-alist))))
1499 (set (make-local-variable 'font-lock-defaults) defaults)
1500 ;; Syntactic fontification?
1501 (when (nth 1 defaults)
1502 (set (make-local-variable 'font-lock-keywords-only) t))
1503 ;; Case fold during regexp fontification?
1504 (when (nth 2 defaults)
1505 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
1506 ;; Syntax table for regexp and syntactic fontification?
1507 (when (nth 3 defaults)
1508 (set (make-local-variable 'font-lock-syntax-table)
1509 (copy-syntax-table (syntax-table)))
1510 (dolist (selem (nth 3 defaults))
1511 ;; The character to modify may be a single CHAR or a STRING.
1512 (let ((syntax (cdr selem)))
1513 (dolist (char (if (numberp (car selem))
1514 (list (car selem))
1515 (mapcar 'identity (car selem))))
1516 (modify-syntax-entry char syntax font-lock-syntax-table)))))
1517 ;; Syntax function for syntactic fontification?
1518 (when (nth 4 defaults)
1519 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1520 (nth 4 defaults)))
1521 ;; Variable alist?
1522 (dolist (x (nthcdr 5 defaults))
1523 (set (make-local-variable (car x)) (cdr x)))
1524 ;; Setup `font-lock-keywords' last because its value might depend
1525 ;; on other settings (e.g. font-lock-compile-keywords uses
1526 ;; font-lock-beginning-of-syntax-function).
1527 (set (make-local-variable 'font-lock-keywords)
1528 (font-lock-compile-keywords (font-lock-eval-keywords keywords) t))
1529 ;; Local fontification?
1530 (while local
1531 (font-lock-add-keywords nil (car (car local)) (cdr (car local)))
1532 (setq local (cdr local)))
1533 (when removed-keywords
1534 (font-lock-remove-keywords nil removed-keywords)))))
030f4a35 1535\f
2d63aa67 1536;;; Colour etc. support.
9bfbb130 1537
2273c36b 1538;; Originally face attributes were specified via `font-lock-face-attributes'.
23a0e11b 1539;; Users then changed the default face attributes by setting that variable.
2273c36b
SM
1540;; However, we try and be back-compatible and respect its value if set except
1541;; for faces where M-x customize has been used to save changes for the face.
1542(when (boundp 'font-lock-face-attributes)
1543 (let ((face-attributes font-lock-face-attributes))
1544 (while face-attributes
1545 (let* ((face-attribute (pop face-attributes))
1546 (face (car face-attribute)))
1547 ;; Rustle up a `defface' SPEC from a `font-lock-face-attributes' entry.
1548 (unless (get face 'saved-face)
1549 (let ((foreground (nth 1 face-attribute))
1550 (background (nth 2 face-attribute))
1551 (bold-p (nth 3 face-attribute))
1552 (italic-p (nth 4 face-attribute))
1553 (underline-p (nth 5 face-attribute))
1554 face-spec)
1555 (when foreground
1556 (setq face-spec (cons ':foreground (cons foreground face-spec))))
1557 (when background
1558 (setq face-spec (cons ':background (cons background face-spec))))
1559 (when bold-p
0cccb49d 1560 (setq face-spec (append '(:weight bold) face-spec)))
2273c36b 1561 (when italic-p
0cccb49d 1562 (setq face-spec (append '(:slant italic) face-spec)))
2273c36b
SM
1563 (when underline-p
1564 (setq face-spec (append '(:underline t) face-spec)))
1565 (custom-declare-face face (list (list t face-spec)) nil)))))))
1566
1567;; But now we do it the custom way. Note that `defface' will not overwrite any
1568;; faces declared above via `custom-declare-face'.
55015061 1569(defface font-lock-comment-face
8379c868 1570 '((((type tty pc) (class color) (background light)) (:foreground "red"))
ce6b1982 1571 (((type tty pc) (class color) (background dark)) (:foreground "red1"))
e7f07c2c 1572 (((class grayscale) (background light))
0cccb49d 1573 (:foreground "DimGray" :weight bold :slant italic))
55015061 1574 (((class grayscale) (background dark))
0cccb49d 1575 (:foreground "LightGray" :weight bold :slant italic))
55015061 1576 (((class color) (background light)) (:foreground "Firebrick"))
86b7fcbb 1577 (((class color) (background dark)) (:foreground "chocolate1"))
0cccb49d 1578 (t (:weight bold :slant italic)))
55015061 1579 "Font Lock mode face used to highlight comments."
2273c36b 1580 :group 'font-lock-highlighting-faces)
55015061
SM
1581
1582(defface font-lock-string-face
e7f07c2c 1583 '((((type tty) (class color)) (:foreground "green"))
0cccb49d
RS
1584 (((class grayscale) (background light)) (:foreground "DimGray" :slant italic))
1585 (((class grayscale) (background dark)) (:foreground "LightGray" :slant italic))
55015061
SM
1586 (((class color) (background light)) (:foreground "RosyBrown"))
1587 (((class color) (background dark)) (:foreground "LightSalmon"))
0cccb49d 1588 (t (:slant italic)))
55015061 1589 "Font Lock mode face used to highlight strings."
2273c36b 1590 :group 'font-lock-highlighting-faces)
55015061 1591
1c172af4
SM
1592(defface font-lock-doc-face
1593 '((t :inherit font-lock-string-face))
1594 "Font Lock mode face used to highlight documentation."
1595 :group 'font-lock-highlighting-faces)
1596
55015061 1597(defface font-lock-keyword-face
e7f07c2c 1598 '((((type tty) (class color)) (:foreground "cyan" :weight bold))
0cccb49d
RS
1599 (((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
1600 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
55015061
SM
1601 (((class color) (background light)) (:foreground "Purple"))
1602 (((class color) (background dark)) (:foreground "Cyan"))
0cccb49d 1603 (t (:weight bold)))
55015061 1604 "Font Lock mode face used to highlight keywords."
2273c36b 1605 :group 'font-lock-highlighting-faces)
55015061
SM
1606
1607(defface font-lock-builtin-face
e7f07c2c 1608 '((((type tty) (class color)) (:foreground "blue" :weight light))
0cccb49d
RS
1609 (((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
1610 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
55015061
SM
1611 (((class color) (background light)) (:foreground "Orchid"))
1612 (((class color) (background dark)) (:foreground "LightSteelBlue"))
0cccb49d 1613 (t (:weight bold)))
55015061 1614 "Font Lock mode face used to highlight builtins."
2273c36b 1615 :group 'font-lock-highlighting-faces)
55015061
SM
1616
1617(defface font-lock-function-name-face
e7f07c2c
GM
1618 '((((type tty) (class color)) (:foreground "blue" :weight bold))
1619 (((class color) (background light)) (:foreground "Blue"))
55015061 1620 (((class color) (background dark)) (:foreground "LightSkyBlue"))
0cccb49d 1621 (t (:inverse-video t :weight bold)))
55015061 1622 "Font Lock mode face used to highlight function names."
2273c36b 1623 :group 'font-lock-highlighting-faces)
55015061
SM
1624
1625(defface font-lock-variable-name-face
e7f07c2c
GM
1626 '((((type tty) (class color)) (:foreground "yellow" :weight light))
1627 (((class grayscale) (background light))
0cccb49d 1628 (:foreground "Gray90" :weight bold :slant italic))
55015061 1629 (((class grayscale) (background dark))
0cccb49d 1630 (:foreground "DimGray" :weight bold :slant italic))
55015061
SM
1631 (((class color) (background light)) (:foreground "DarkGoldenrod"))
1632 (((class color) (background dark)) (:foreground "LightGoldenrod"))
0cccb49d 1633 (t (:weight bold :slant italic)))
55015061 1634 "Font Lock mode face used to highlight variable names."
2273c36b 1635 :group 'font-lock-highlighting-faces)
55015061
SM
1636
1637(defface font-lock-type-face
e7f07c2c 1638 '((((type tty) (class color)) (:foreground "green"))
0cccb49d
RS
1639 (((class grayscale) (background light)) (:foreground "Gray90" :weight bold))
1640 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
46f26fcf 1641 (((class color) (background light)) (:foreground "ForestGreen"))
55015061 1642 (((class color) (background dark)) (:foreground "PaleGreen"))
0cccb49d 1643 (t (:weight bold :underline t)))
8acf2292 1644 "Font Lock mode face used to highlight type and classes."
2273c36b 1645 :group 'font-lock-highlighting-faces)
55015061 1646
8acf2292 1647(defface font-lock-constant-face
e7f07c2c
GM
1648 '((((type tty) (class color)) (:foreground "magenta"))
1649 (((class grayscale) (background light))
0cccb49d 1650 (:foreground "LightGray" :weight bold :underline t))
55015061 1651 (((class grayscale) (background dark))
0cccb49d 1652 (:foreground "Gray50" :weight bold :underline t))
55015061
SM
1653 (((class color) (background light)) (:foreground "CadetBlue"))
1654 (((class color) (background dark)) (:foreground "Aquamarine"))
0cccb49d 1655 (t (:weight bold :underline t)))
8acf2292 1656 "Font Lock mode face used to highlight constants and labels."
2273c36b 1657 :group 'font-lock-highlighting-faces)
55015061
SM
1658
1659(defface font-lock-warning-face
e7f07c2c 1660 '((((type tty) (class color)) (:foreground "red"))
0cccb49d
RS
1661 (((class color) (background light)) (:foreground "Red" :weight bold))
1662 (((class color) (background dark)) (:foreground "Pink" :weight bold))
1663 (t (:inverse-video t :weight bold)))
55015061 1664 "Font Lock mode face used to highlight warnings."
2273c36b 1665 :group 'font-lock-highlighting-faces)
2d63aa67 1666
d654cab8
SM
1667(defface font-lock-preprocessor-face
1668 '((t :inherit 'font-lock-builtin-face))
1669 "Font Lock mode face used to highlight preprocessor directives."
1670 :group 'font-lock-highlighting-faces)
1671
2d63aa67 1672;;; End of Colour etc. support.
9bfbb130 1673\f
56fcbd7e
SM
1674;;; Menu support.
1675
1676;; This section of code is commented out because Emacs does not have real menu
1677;; buttons. (We can mimic them by putting "( ) " or "(X) " at the beginning of
1678;; the menu entry text, but with Xt it looks both ugly and embarrassingly
1679;; amateur.) If/When Emacs gets real menus buttons, put in menu-bar.el after
1680;; the entry for "Text Properties" something like:
1681;;
1682;; (define-key menu-bar-edit-menu [font-lock]
9c8de95c 1683;; (cons "Syntax Highlighting" font-lock-menu))
56fcbd7e
SM
1684;;
1685;; and remove a single ";" from the beginning of each line in the rest of this
1686;; section. Probably the mechanism for telling the menu code what are menu
1687;; buttons and when they are on or off needs tweaking. I have assumed that the
1688;; mechanism is via `menu-toggle' and `menu-selected' symbol properties. sm.
1689
1690;;;;###autoload
1691;(progn
1692; ;; Make the Font Lock menu.
1693; (defvar font-lock-menu (make-sparse-keymap "Syntax Highlighting"))
1694; ;; Add the menu items in reverse order.
1695; (define-key font-lock-menu [fontify-less]
1696; '("Less In Current Buffer" . font-lock-fontify-less))
1697; (define-key font-lock-menu [fontify-more]
1698; '("More In Current Buffer" . font-lock-fontify-more))
1699; (define-key font-lock-menu [font-lock-sep]
1700; '("--"))
1701; (define-key font-lock-menu [font-lock-mode]
1702; '("In Current Buffer" . font-lock-mode))
1703; (define-key font-lock-menu [global-font-lock-mode]
1704; '("In All Buffers" . global-font-lock-mode)))
1705;
1706;;;;###autoload
1707;(progn
1708; ;; We put the appropriate `menu-enable' etc. symbol property values on when
1709; ;; font-lock.el is loaded, so we don't need to autoload the three variables.
1710; (put 'global-font-lock-mode 'menu-toggle t)
1711; (put 'font-lock-mode 'menu-toggle t)
1712; (put 'font-lock-fontify-more 'menu-enable '(identity))
1713; (put 'font-lock-fontify-less 'menu-enable '(identity)))
1714;
1715;;; Put the appropriate symbol property values on now. See above.
9c8de95c 1716;(put 'global-font-lock-mode 'menu-selected 'global-font-lock-mode)
56fcbd7e
SM
1717;(put 'font-lock-mode 'menu-selected 'font-lock-mode)
1718;(put 'font-lock-fontify-more 'menu-enable '(nth 2 font-lock-fontify-level))
1719;(put 'font-lock-fontify-less 'menu-enable '(nth 1 font-lock-fontify-level))
1720;
1721;(defvar font-lock-fontify-level nil) ; For less/more fontification.
1722;
1723;(defun font-lock-fontify-level (level)
1724; (let ((font-lock-maximum-decoration level))
1725; (when font-lock-mode
1726; (font-lock-mode))
1727; (font-lock-mode)
1728; (when font-lock-verbose
1729; (message "Fontifying %s... level %d" (buffer-name) level))))
1730;
1731;(defun font-lock-fontify-less ()
1732; "Fontify the current buffer with less decoration.
1733;See `font-lock-maximum-decoration'."
1734; (interactive)
1735; ;; Check in case we get called interactively.
1736; (if (nth 1 font-lock-fontify-level)
1737; (font-lock-fontify-level (1- (car font-lock-fontify-level)))
1738; (error "No less decoration")))
1739;
1740;(defun font-lock-fontify-more ()
1741; "Fontify the current buffer with more decoration.
1742;See `font-lock-maximum-decoration'."
1743; (interactive)
1744; ;; Check in case we get called interactively.
1745; (if (nth 2 font-lock-fontify-level)
1746; (font-lock-fontify-level (1+ (car font-lock-fontify-level)))
1747; (error "No more decoration")))
1748;
1749;;; This should be called by `font-lock-set-defaults'.
1750;(defun font-lock-set-menu ()
1751; ;; Activate less/more fontification entries if there are multiple levels for
1752; ;; the current buffer. Sets `font-lock-fontify-level' to be of the form
1753; ;; (CURRENT-LEVEL IS-LOWER-LEVEL-P IS-HIGHER-LEVEL-P) for menu activation.
1754; (let ((keywords (or (nth 0 font-lock-defaults)
1755; (nth 1 (assq major-mode font-lock-defaults-alist))))
1756; (level (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1757; (make-local-variable 'font-lock-fontify-level)
1758; (if (or (symbolp keywords) (= (length keywords) 1))
1759; (font-lock-unset-menu)
1760; (cond ((eq level t)
1761; (setq level (1- (length keywords))))
1762; ((or (null level) (zerop level))
1763; ;; The default level is usually, but not necessarily, level 1.
1764; (setq level (- (length keywords)
1765; (length (member (eval (car keywords))
1766; (mapcar 'eval (cdr keywords))))))))
1767; (setq font-lock-fontify-level (list level (> level 1)
1768; (< level (1- (length keywords))))))))
1769;
1770;;; This should be called by `font-lock-unset-defaults'.
1771;(defun font-lock-unset-menu ()
1772; ;; Deactivate less/more fontification entries.
1773; (setq font-lock-fontify-level nil))
2d63aa67
SM
1774
1775;;; End of Menu support.
56fcbd7e 1776\f
9bfbb130 1777;;; Various regexp information shared by several modes.
a1eb1cf1 1778;;; Information specific to a single mode should go in its load library.
030f4a35 1779
1fed50c4
SM
1780;; Font Lock support for C, C++, Objective-C and Java modes is now in
1781;; cc-fonts.el (and required by cc-mode.el). However, the below function
56fcbd7e 1782;; should stay in font-lock.el, since it is used by other libraries. sm.
2e6a15fc 1783
c1f2ffc8 1784(defun font-lock-match-c-style-declaration-item-and-skip-to-next (limit)
2e6a15fc 1785 "Match, and move over, any declaration/definition item after point.
c1f2ffc8
SM
1786Matches after point, but ignores leading whitespace and `*' characters.
1787Does not move further than LIMIT.
1788
ac6e572c
SM
1789The expected syntax of a declaration/definition item is `word' (preceded by
1790optional whitespace and `*' characters and proceeded by optional whitespace)
1791optionally followed by a `('. Everything following the item (but belonging to
4cac2fc3 1792it) is expected to be skip-able by `scan-sexps', and items are expected to be
ac6e572c 1793separated with a `,' and to be terminated with a `;'.
c1f2ffc8
SM
1794
1795Thus the regexp matches after point: word (
1796 ^^^^ ^
1797Where the match subexpressions are: 1 2
1798
1799The item is delimited by (match-beginning 1) and (match-end 1).
1800If (match-beginning 2) is non-nil, the item is followed by a `('.
1801
1802This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
c1966bb4 1803 (when (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?")
8259bf10
SM
1804 (when (and (match-end 2) (> (- (match-end 2) (match-beginning 2)) 1))
1805 ;; If `word' is followed by a double open-paren, it's probably
1806 ;; a macro used for "int myfun P_ ((int arg1))". Let's go back one
1807 ;; word to try and match `myfun' rather than `P_'.
1808 (let ((pos (point)))
c1966bb4 1809 (skip-chars-backward " \t\n")
8259bf10 1810 (skip-syntax-backward "w")
4cac2fc3 1811 (unless (looking-at "\\(\\sw+\\)[ \t\n]*\\sw+[ \t\n]*\\(((?\\)?")
8259bf10
SM
1812 ;; Looks like it was something else, so go back to where we
1813 ;; were and reset the match data by rematching.
1814 (goto-char pos)
c1966bb4 1815 (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?"))))
2e6a15fc
SM
1816 (save-match-data
1817 (condition-case nil
1818 (save-restriction
c1966bb4 1819 ;; Restrict to the LIMIT.
2e6a15fc
SM
1820 (narrow-to-region (point-min) limit)
1821 (goto-char (match-end 1))
1822 ;; Move over any item value, etc., to the next item.
c1966bb4 1823 (while (not (looking-at "[ \t\n]*\\(\\(,\\)\\|;\\|\\'\\)"))
2e6a15fc
SM
1824 (goto-char (or (scan-sexps (point) 1) (point-max))))
1825 (goto-char (match-end 2)))
1826 (error t)))))
46f26fcf
SM
1827\f
1828;; Lisp.
2e6a15fc 1829
030f4a35 1830(defconst lisp-font-lock-keywords-1
2e6a15fc
SM
1831 (eval-when-compile
1832 (list
2d63aa67 1833 ;;
55015061
SM
1834 ;; Definitions.
1835 (list (concat "(\\(def\\("
1836 ;; Function declarations.
7c5312b2 1837 "\\(advice\\|varalias\\|alias\\|generic\\|macro\\*?\\|method\\|"
4fffc071 1838 "setf\\|subst\\*?\\|un\\*?\\|"
1a5c5f2e 1839 "ine-\\(condition\\|\\(?:derived\\|minor\\)-mode\\|"
4fffc071 1840 "method-combination\\|setf-expander\\|skeleton\\|widget\\|"
1a5c5f2e 1841 "function\\|\\(compiler\\|modify\\|symbol\\)-macro\\)\\)\\|"
2e6a15fc 1842 ;; Variable declarations.
4fffc071 1843 "\\(const\\(ant\\)?\\|custom\\|face\\|parameter\\|var\\)\\|"
2e6a15fc 1844 ;; Structure declarations.
e2cd29bd 1845 "\\(class\\|group\\|theme\\|package\\|struct\\|type\\)"
2e6a15fc 1846 "\\)\\)\\>"
55015061 1847 ;; Any whitespace and defined object.
2e6a15fc 1848 "[ \t'\(]*"
11b42ef4 1849 "\\(setf[ \t]+\\sw+)\\|\\sw+\\)?")
2e6a15fc 1850 '(1 font-lock-keyword-face)
ae659ad5 1851 '(9 (cond ((match-beginning 3) font-lock-function-name-face)
aaa15488 1852 ((match-beginning 6) font-lock-variable-name-face)
55015061 1853 (t font-lock-type-face))
2e6a15fc 1854 nil t))
2d63aa67
SM
1855 ;;
1856 ;; Emacs Lisp autoload cookies.
731dd885 1857 '("^;;;###\\(autoload\\)" 1 font-lock-warning-face prepend)
2e6a15fc 1858 ))
56fcbd7e 1859 "Subdued level highlighting for Lisp modes.")
030f4a35
RS
1860
1861(defconst lisp-font-lock-keywords-2
799761f0 1862 (append lisp-font-lock-keywords-1
2e6a15fc
SM
1863 (eval-when-compile
1864 (list
1865 ;;
2d63aa67 1866 ;; Control structures. Emacs Lisp forms.
ac6e572c
SM
1867 (cons (concat
1868 "(" (regexp-opt
4fffc071 1869 '("cond" "if" "while" "let" "let*"
ac6e572c 1870 "prog" "progn" "progv" "prog1" "prog2" "prog*"
4fffc071 1871 "inline" "lambda" "save-restriction" "save-excursion"
ac6e572c
SM
1872 "save-window-excursion" "save-selected-window"
1873 "save-match-data" "save-current-buffer" "unwind-protect"
9c8de95c 1874 "condition-case" "track-mouse"
ac6e572c 1875 "eval-after-load" "eval-and-compile" "eval-when-compile"
4fffc071 1876 "eval-when"
844a6a46
SM
1877 "with-current-buffer" "with-electric-help"
1878 "with-output-to-string" "with-output-to-temp-buffer"
79f238c9 1879 "with-temp-buffer" "with-temp-file" "with-temp-message"
844a6a46 1880 "with-timeout") t)
ac6e572c 1881 "\\>")
2e6a15fc
SM
1882 1)
1883 ;;
2d63aa67 1884 ;; Control structures. Common Lisp forms.
ac6e572c
SM
1885 (cons (concat
1886 "(" (regexp-opt
1887 '("when" "unless" "case" "ecase" "typecase" "etypecase"
aaa15488 1888 "ccase" "ctypecase" "handler-case" "handler-bind"
ae659ad5 1889 "restart-bind" "restart-case" "in-package"
4fffc071 1890 "cerror" "break" "ignore-errors"
ae659ad5 1891 "loop" "do" "do*" "dotimes" "dolist" "the" "locally"
aaa15488
RS
1892 "proclaim" "declaim" "declare" "symbol-macrolet"
1893 "lexical-let" "lexical-let*" "flet" "labels" "compiler-let"
1894 "destructuring-bind" "macrolet" "tagbody" "block"
ac6e572c
SM
1895 "return" "return-from") t)
1896 "\\>")
2d63aa67
SM
1897 1)
1898 ;;
4fffc071
SM
1899 ;; Exit/Feature symbols as constants.
1900 (list (concat "(\\(catch\\|throw\\|featurep\\|provide\\|require\\)\\>"
1901 "[ \t']*\\(\\sw+\\)?")
1902 '(1 font-lock-keyword-face)
1903 '(2 font-lock-constant-face nil t))
1904 ;;
1905 ;; Erroneous structures.
1906 '("(\\(abort\\|assert\\|error\\|signal\\)\\>" 1 font-lock-warning-face)
2e6a15fc
SM
1907 ;;
1908 ;; Words inside \\[] tend to be for `substitute-command-keys'.
8acf2292 1909 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-constant-face prepend)
2e6a15fc
SM
1910 ;;
1911 ;; Words inside `' tend to be symbol names.
8acf2292 1912 '("`\\(\\sw\\sw+\\)'" 1 font-lock-constant-face prepend)
2e6a15fc 1913 ;;
9c8de95c 1914 ;; Constant values.
6c4bb7f6 1915 '("\\<:\\sw+\\>" 0 font-lock-builtin-face)
2e6a15fc
SM
1916 ;;
1917 ;; ELisp and CLisp `&' keywords as types.
f56f1421 1918 '("\\&\\sw+\\>" . font-lock-type-face)
5e9e032a
SS
1919 ;;
1920 ;; CL `with-' and `do-' constructs
1921 '("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
2e6a15fc 1922 )))
fb512de9 1923 "Gaudy level highlighting for Lisp modes.")
030f4a35 1924
fb512de9
SM
1925(defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
1926 "Default expressions to highlight in Lisp modes.")
46f26fcf 1927\f
030f4a35 1928(provide 'font-lock)
54f73af3
GM
1929
1930(when (eq font-lock-support-mode 'jit-lock-mode)
1931 (require 'jit-lock))
030f4a35 1932
ab5796a9 1933;;; arch-tag: 682327e4-64d8-4057-b20b-1fbb9f1fc54c
030f4a35 1934;;; font-lock.el ends here