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