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