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