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