(auto-mode-alist): Add *.perl and fix the *.<rev> case.
[bpt/emacs.git] / lisp / font-lock.el
CommitLineData
876f2438
SM
1;;; font-lock.el --- Electric font lock mode
2
f9261020 3;; Copyright (C) 1992, 93, 94, 95, 96, 97, 98, 1999, 2000, 2001
01b69148 4;; Free Software Foundation, Inc.
030f4a35 5
0279f991 6;; Author: jwz, then rms, then sm
030f4a35
RS
7;; Maintainer: FSF
8;; Keywords: languages, faces
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b578f267
EN
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
030f4a35 26
030f4a35
RS
27;;; Commentary:
28
b89e1134
SM
29;; Font Lock mode is a minor mode that causes your comments to be displayed in
30;; one face, strings in another, reserved words in another, and so on.
030f4a35
RS
31;;
32;; Comments will be displayed in `font-lock-comment-face'.
33;; Strings will be displayed in `font-lock-string-face'.
a1eb1cf1 34;; Regexps are used to display selected patterns in other faces.
030f4a35 35;;
9bfbb130
SM
36;; To make the text you type be fontified, use M-x font-lock-mode RET.
37;; When this minor mode is on, the faces of the current line are updated with
38;; every insertion or deletion.
030f4a35 39;;
a2b8e66b 40;; To turn Font Lock mode on automatically, add this to your ~/.emacs file:
030f4a35 41;;
b89e1134 42;; (add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock)
030f4a35 43;;
a2b8e66b
SM
44;; Or if you want to turn Font Lock mode on in many modes:
45;;
46;; (global-font-lock-mode t)
47;;
9bfbb130
SM
48;; Fontification for a particular mode may be available in a number of levels
49;; of decoration. The higher the level, the more decoration, but the more time
50;; it takes to fontify. See the variable `font-lock-maximum-decoration', and
5aa29679
RS
51;; also the variable `font-lock-maximum-size'. Support modes for Font Lock
52;; mode can be used to speed up Font Lock mode. See `font-lock-support-mode'.
98f84f52 53\f
56fcbd7e
SM
54;;; How Font Lock mode fontifies:
55
56;; When Font Lock mode is turned on in a buffer, it (a) fontifies the entire
57;; buffer and (b) installs one of its fontification functions on one of the
58;; hook variables that are run by Emacs after every buffer change (i.e., an
59;; insertion or deletion). Fontification means the replacement of `face' text
60;; properties in a given region; Emacs displays text with these `face' text
61;; properties appropriately.
62;;
63;; Fontification normally involves syntactic (i.e., strings and comments) and
ac6e572c
SM
64;; regexp (i.e., keywords and everything else) passes. There are actually
65;; three passes; (a) the syntactic keyword pass, (b) the syntactic pass and (c)
66;; the keyword pass. Confused?
67;;
68;; The syntactic keyword pass places `syntax-table' text properties in the
69;; buffer according to the variable `font-lock-syntactic-keywords'. It is
70;; necessary because Emacs' syntax table is not powerful enough to describe all
71;; the different syntactic constructs required by the sort of people who decide
72;; that a single quote can be syntactic or not depending on the time of day.
73;; (What sort of person could decide to overload the meaning of a quote?)
74;; Obviously the syntactic keyword pass must occur before the syntactic pass.
75;;
76;; The syntactic pass places `face' text properties in the buffer according to
77;; syntactic context, i.e., according to the buffer's syntax table and buffer
78;; text's `syntax-table' text properties. It involves using a syntax parsing
79;; function to determine the context of different parts of a region of text. A
80;; syntax parsing function is necessary because generally strings and/or
81;; comments can span lines, and so the context of a given region is not
82;; necessarily apparent from the content of that region. Because the keyword
83;; pass only works within a given region, it is not generally appropriate for
84;; syntactic fontification. This is the first fontification pass that makes
85;; changes visible to the user; it fontifies strings and comments.
86;;
87;; The keyword pass places `face' text properties in the buffer according to
88;; the variable `font-lock-keywords'. It involves searching for given regexps
89;; (or calling given search functions) within the given region. This is the
90;; second fontification pass that makes changes visible to the user; it
91;; fontifies language reserved words, etc.
92;;
93;; Oh, and the answer is, "Yes, obviously just about everything should be done
94;; in a single syntactic pass, but the only syntactic parser available
95;; understands only strings and comments." Perhaps one day someone will write
96;; some syntactic parsers for common languages and a son-of-font-lock.el could
97;; use them rather then relying so heavily on the keyword (regexp) pass.
56fcbd7e 98
c1f2ffc8
SM
99;;; How Font Lock mode supports modes or is supported by modes:
100
101;; Modes that support Font Lock mode do so by defining one or more variables
102;; whose values specify the fontification. Font Lock mode knows of these
103;; variable names from (a) the buffer local variable `font-lock-defaults', if
104;; non-nil, or (b) the global variable `font-lock-defaults-alist', if the major
105;; mode has an entry. (Font Lock mode is set up via (a) where a mode's
106;; patterns are distributed with the mode's package library, and (b) where a
107;; mode's patterns are distributed with font-lock.el itself. An example of (a)
108;; is Pascal mode, an example of (b) is Lisp mode. Normally, the mechanism is
109;; (a); (b) is used where it is not clear which package library should contain
110;; the pattern definitions.) Font Lock mode chooses which variable to use for
111;; fontification based on `font-lock-maximum-decoration'.
56fcbd7e
SM
112;;
113;; Font Lock mode fontification behaviour can be modified in a number of ways.
114;; See the below comments and the comments distributed throughout this file.
c1f2ffc8
SM
115
116;;; Constructing patterns:
117
98f84f52
SM
118;; See the documentation for the variable `font-lock-keywords'.
119;;
ac6e572c
SM
120;; Efficient regexps for use as MATCHERs for `font-lock-keywords' and
121;; `font-lock-syntactic-keywords' can be generated via the function
afa18a4e 122;; `regexp-opt'.
98f84f52 123
c1f2ffc8
SM
124;;; Adding patterns for modes that already support Font Lock:
125
126;; Though Font Lock highlighting patterns already exist for many modes, it's
127;; likely there's something that you want fontified that currently isn't, even
128;; at the maximum fontification level. You can add highlighting patterns via
129;; `font-lock-add-keywords'. For example, say in some C
130;; header file you #define the token `and' to expand to `&&', etc., to make
131;; your C code almost readable. In your ~/.emacs there could be:
98f84f52 132;;
c1f2ffc8 133;; (font-lock-add-keywords 'c-mode '("\\<\\(and\\|or\\|not\\)\\>"))
98f84f52 134;;
c1f2ffc8
SM
135;; Some modes provide specific ways to modify patterns based on the values of
136;; other variables. For example, additional C types can be specified via the
137;; variable `c-font-lock-extra-types'.
138
139;;; Adding patterns for modes that do not support Font Lock:
140
141;; Not all modes support Font Lock mode. If you (as a user of the mode) add
142;; patterns for a new mode, you must define in your ~/.emacs a variable or
143;; variables that specify regexp fontification. Then, you should indicate to
144;; Font Lock mode, via the mode hook setting `font-lock-defaults', exactly what
145;; support is required. For example, say Foo mode should have the following
146;; regexps fontified case-sensitively, and comments and strings should not be
147;; fontified automagically. In your ~/.emacs there could be:
98f84f52 148;;
c1f2ffc8
SM
149;; (defvar foo-font-lock-keywords
150;; '(("\\<\\(one\\|two\\|three\\)\\>" . font-lock-keyword-face)
151;; ("\\<\\(four\\|five\\|six\\)\\>" . font-lock-type-face))
152;; "Default expressions to highlight in Foo mode.")
98f84f52 153;;
c1f2ffc8
SM
154;; (add-hook 'foo-mode-hook
155;; (function (lambda ()
156;; (make-local-variable 'font-lock-defaults)
157;; (setq font-lock-defaults '(foo-font-lock-keywords t)))))
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
2273c36b 210;; Define core `font-lock' group.
55015061
SM
211(defgroup font-lock nil
212 "Font Lock mode text highlighting package."
213 :link '(custom-manual "(emacs)Font Lock")
63e20d60 214 :link '(custom-manual "(elisp)Font Lock Mode")
55015061
SM
215 :group 'faces)
216
2273c36b
SM
217(defgroup font-lock-highlighting-faces nil
218 "Faces for highlighting text."
55015061 219 :prefix "font-lock-"
2273c36b
SM
220 :group 'font-lock)
221
222(defgroup font-lock-extra-types nil
223 "Extra mode-specific type names for highlighting declarations."
224 :group 'font-lock)
225
ac6e572c 226;; Define support mode groups here to impose `font-lock' group order.
2273c36b
SM
227(defgroup fast-lock nil
228 "Font Lock support mode to cache fontification."
229 :link '(custom-manual "(emacs)Support Modes")
230 :load 'fast-lock
231 :group 'font-lock)
232
233(defgroup lazy-lock nil
234 "Font Lock support mode to fontify lazily."
235 :link '(custom-manual "(emacs)Support Modes")
236 :load 'lazy-lock
55015061 237 :group 'font-lock)
e7f07c2c
GM
238
239(defgroup jit-lock nil
240 "Font Lock support mode to fontify just-in-time."
241 :link '(custom-manual "(emacs)Support Modes")
63e20d60 242 :version "21.1"
faa57c0a 243 :load 'jit-lock
e7f07c2c 244 :group 'font-lock)
55015061 245\f
9bfbb130
SM
246;; User variables.
247
4fffc071 248(defcustom font-lock-maximum-size 256000
23a0e11b
SM
249 "*Maximum size of a buffer for buffer fontification.
250Only buffers less than this can be fontified when Font Lock mode is turned on.
251If nil, means size is irrelevant.
252If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
253where MAJOR-MODE is a symbol or t (meaning the default). For example:
254 ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576))
255means that the maximum size is 250K for buffers in C or C++ modes, one megabyte
256for buffers in Rmail mode, and size is irrelevant otherwise."
257 :type '(choice (const :tag "none" nil)
258 (integer :tag "size")
259 (repeat :menu-tag "mode specific" :tag "mode specific"
260 :value ((t . nil))
261 (cons :tag "Instance"
262 (radio :tag "Mode"
263 (const :tag "all" t)
264 (symbol :tag "name"))
265 (radio :tag "Size"
266 (const :tag "none" nil)
267 (integer :tag "size")))))
55015061 268 :group 'font-lock)
9bfbb130 269
55015061 270(defcustom font-lock-maximum-decoration t
2d9cdda8 271 "*Maximum decoration level for fontification.
9bfbb130
SM
272If nil, use the default decoration (typically the minimum available).
273If t, use the maximum decoration available.
274If a number, use that level of decoration (or if not available the maximum).
275If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
276where MAJOR-MODE is a symbol or t (meaning the default). For example:
5aa29679
RS
277 ((c-mode . t) (c++-mode . 2) (t . 1))
278means use the maximum decoration available for buffers in C mode, level 2
55015061 279decoration for buffers in C++ mode, and level 1 decoration otherwise."
b34aa0a3
SM
280 :type '(choice (const :tag "default" nil)
281 (const :tag "maximum" t)
282 (integer :tag "level" 1)
283 (repeat :menu-tag "mode specific" :tag "mode specific"
284 :value ((t . t))
285 (cons :tag "Instance"
286 (radio :tag "Mode"
287 (const :tag "all" t)
288 (symbol :tag "name"))
289 (radio :tag "Decoration"
290 (const :tag "default" nil)
019f00d8 291 (const :tag "maximum" t)
b34aa0a3 292 (integer :tag "level" 1)))))
55015061
SM
293 :group 'font-lock)
294
4fffc071 295(defcustom font-lock-verbose 0
23a0e11b
SM
296 "*If non-nil, means show status messages for buffer fontification.
297If a number, only buffers greater than this size have fontification messages."
298 :type '(choice (const :tag "never" nil)
4fffc071
SM
299 (other :tag "always" t)
300 (integer :tag "size"))
55015061 301 :group 'font-lock)
9bfbb130 302\f
1c172af4
SM
303
304;; Originally these variable values were face names such as `bold' etc.
305;; Now we create our own faces, but we keep these variables for compatibility
306;; and they give users another mechanism for changing face appearance.
307;; We now allow a FACENAME in `font-lock-keywords' to be any expression that
308;; returns a face. So the easiest thing is to continue using these variables,
309;; rather than sometimes evaling FACENAME and sometimes not. sm.
310(defvar font-lock-comment-face 'font-lock-comment-face
311 "Face name to use for comments.")
312
313(defvar font-lock-string-face 'font-lock-string-face
314 "Face name to use for strings.")
315
316(defvar font-lock-doc-face 'font-lock-doc-face
317 "Face name to use for documentation.")
318
319(defvar font-lock-keyword-face 'font-lock-keyword-face
320 "Face name to use for keywords.")
321
322(defvar font-lock-builtin-face 'font-lock-builtin-face
323 "Face name to use for builtins.")
324
325(defvar font-lock-function-name-face 'font-lock-function-name-face
326 "Face name to use for function names.")
327
328(defvar font-lock-variable-name-face 'font-lock-variable-name-face
329 "Face name to use for variable names.")
330
331(defvar font-lock-type-face 'font-lock-type-face
332 "Face name to use for type and class names.")
333
334(defvar font-lock-constant-face 'font-lock-constant-face
335 "Face name to use for constant and label names.")
336
337(defvar font-lock-warning-face 'font-lock-warning-face
338 "Face name to use for things that should stand out.")
339
340(defvar font-lock-reference-face 'font-lock-constant-face
341 "This variable is obsolete. Use `font-lock-constant-face'.")
342
9bfbb130
SM
343;; Fontification variables:
344
030f4a35 345(defvar font-lock-keywords nil
ac6e572c 346 "A list of the keywords to highlight.
caf0dd71 347Each element should have one of these forms:
a1eb1cf1 348
826b2925
SM
349 MATCHER
350 (MATCHER . MATCH)
351 (MATCHER . FACENAME)
352 (MATCHER . HIGHLIGHT)
353 (MATCHER HIGHLIGHT ...)
a078558d 354 (eval . FORM)
a1eb1cf1 355
9bfbb130
SM
356where HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
357
a078558d
SM
358FORM is an expression, whose value should be a keyword element, evaluated when
359the keyword is (first) used in a buffer. This feature can be used to provide a
360keyword that can only be generated when Font Lock mode is actually turned on.
361
9c8de95c
SM
362For highlighting single items, for example each instance of the word \"foo\",
363typically only MATCH-HIGHLIGHT is required.
d9f7b2d3 364However, if an item or (typically) items are to be highlighted following the
9c8de95c
SM
365instance of another item (the anchor), for example each instance of the
366word \"bar\" following the word \"anchor\" then MATCH-ANCHORED may be required.
9bfbb130
SM
367
368MATCH-HIGHLIGHT should be of the form:
369
370 (MATCH FACENAME OVERRIDE LAXMATCH)
371
caf0dd71 372where MATCHER can be either the regexp to search for, or the function name to
844a6a46
SM
373call to make the search (called with one argument, the limit of the search) and
374return non-nil if it succeeds (and set `match-data' appropriately).
afa18a4e
SM
375MATCHER regexps can be generated via the function `regexp-opt'. MATCH is
376the subexpression of MATCHER to be highlighted. FACENAME is an expression
377whose value is the face name to use. Face default attributes can be
378modified via \\[customize].
a1eb1cf1 379
2d63aa67 380OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification can
9bfbb130
SM
381be overwritten. If `keep', only parts not already fontified are highlighted.
382If `prepend' or `append', existing fontification is merged with the new, in
383which the new or existing fontification, respectively, takes precedence.
d9f7b2d3 384If LAXMATCH is non-nil, no error is signaled if there is no MATCH in MATCHER.
a1eb1cf1 385
9bfbb130
SM
386For example, an element of the form highlights (if not already highlighted):
387
9c8de95c 388 \"\\\\\\=<foo\\\\\\=>\" discrete occurrences of \"foo\" in the value of the
876f2438 389 variable `font-lock-keyword-face'.
9c8de95c 390 (\"fu\\\\(bar\\\\)\" . 1) substring \"bar\" within all occurrences of \"fubar\" in
9bfbb130
SM
391 the value of `font-lock-keyword-face'.
392 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'.
393 (\"foo\\\\|bar\" 0 foo-bar-face t)
9c8de95c 394 occurrences of either \"foo\" or \"bar\" in the value
9bfbb130 395 of `foo-bar-face', even if already highlighted.
844a6a46 396 (fubar-match 1 fubar-face)
9c8de95c 397 the first subexpression within all occurrences of
844a6a46
SM
398 whatever the function `fubar-match' finds and matches
399 in the value of `fubar-face'.
9bfbb130
SM
400
401MATCH-ANCHORED should be of the form:
402
403 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
404
9c8de95c
SM
405where MATCHER is a regexp to search for or the function name to call to make
406the search, as for MATCH-HIGHLIGHT above, but with one exception; see below.
876f2438
SM
407PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
408the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be
409used to initialise before, and cleanup after, MATCHER is used. Typically,
410PRE-MATCH-FORM is used to move to some position relative to the original
411MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might
412be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER.
9bfbb130
SM
413
414For example, an element of the form highlights (if not already highlighted):
415
876f2438 416 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
9bfbb130 417
9c8de95c 418 discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
876f2438
SM
419 discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
420 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is
421 initially searched for starting from the end of the match of \"anchor\", and
422 searching for subsequent instance of \"anchor\" resumes from where searching
423 for \"item\" concluded.)
9bfbb130 424
56fcbd7e
SM
425The above-mentioned exception is as follows. The limit of the MATCHER search
426defaults to the end of the line after PRE-MATCH-FORM is evaluated.
427However, if PRE-MATCH-FORM returns a position greater than the position after
428PRE-MATCH-FORM is evaluated, that position is used as the limit of the search.
429It is generally a bad idea to return a position greater than the end of the
430line, i.e., cause the MATCHER search to span lines.
431
6e1d0d15
SM
432These regular expressions can match text which spans lines, although
433it is better to avoid it if possible since updating them while editing
434text is slower, and it is not guaranteed to be always correct when using
435support modes like jit-lock or lazy-lock.
a1eb1cf1 436
2d63aa67
SM
437This variable is set by major modes via the variable `font-lock-defaults'.
438Be careful when composing regexps for this list; a poorly written pattern can
439dramatically slow things down!")
030f4a35 440
2e6a15fc
SM
441;; This variable is used by mode packages that support Font Lock mode by
442;; defining their own keywords to use for `font-lock-keywords'. (The mode
443;; command should make it buffer-local and set it to provide the set up.)
b89e1134 444(defvar font-lock-defaults nil
2d63aa67
SM
445 "Defaults for Font Lock mode specified by the major mode.
446Defaults should be of the form:
447
448 (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN ...)
449
450KEYWORDS may be a symbol (a variable or function whose value is the keywords to
451use for fontification) or a list of symbols. If KEYWORDS-ONLY is non-nil,
452syntactic fontification (strings and comments) is not performed.
453If CASE-FOLD is non-nil, the case of the keywords is ignored when fontifying.
454If SYNTAX-ALIST is non-nil, it should be a list of cons pairs of the form
455\(CHAR-OR-STRING . STRING) used to set the local Font Lock syntax table, for
456keyword and syntactic fontification (see `modify-syntax-entry').
457
458If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
459backwards outside any enclosing syntactic block, for syntactic fontification.
460Typical values are `beginning-of-line' (i.e., the start of the line is known to
461be outside a syntactic block), or `beginning-of-defun' for programming modes or
462`backward-paragraph' for textual modes (i.e., the mode-dependent function is
463known to move outside a syntactic block). If nil, the beginning of the buffer
464is used as a position outside of a syntactic block, in the worst case.
465
466These item elements are used by Font Lock mode to set the variables
467`font-lock-keywords', `font-lock-keywords-only',
468`font-lock-keywords-case-fold-search', `font-lock-syntax-table' and
469`font-lock-beginning-of-syntax-function', respectively.
470
471Further item elements are alists of the form (VARIABLE . VALUE) and are in no
472particular order. Each VARIABLE is made buffer-local before set to VALUE.
473
474Currently, appropriate variables include `font-lock-mark-block-function'.
475If this is non-nil, it should be a function with no args used to mark any
476enclosing block of text, for fontification via \\[font-lock-fontify-block].
477Typical values are `mark-defun' for programming modes or `mark-paragraph' for
478textual modes (i.e., the mode-dependent function is known to put point and mark
479around a text block relevant to that mode).
480
79f238c9
SM
481Other variables include that for syntactic keyword fontification,
482`font-lock-syntactic-keywords'
483and those for buffer-specialised fontification functions,
2d63aa67
SM
484`font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function',
485`font-lock-fontify-region-function', `font-lock-unfontify-region-function',
55015061 486`font-lock-inhibit-thing-lock' and `font-lock-maximum-size'.")
fd612dd9
SM
487;;;###autoload
488(make-variable-buffer-local 'font-lock-defaults)
b89e1134 489
2e6a15fc 490;; This variable is used where font-lock.el itself supplies the keywords.
b89e1134 491(defvar font-lock-defaults-alist
2e6a15fc
SM
492 (let (;; We use `beginning-of-defun', rather than nil, for SYNTAX-BEGIN.
493 ;; Thus the calculation of the cache is usually faster but not
494 ;; infallible, so we risk mis-fontification. sm.
fb512de9 495 (c-mode-defaults
9bfbb130
SM
496 '((c-font-lock-keywords c-font-lock-keywords-1
497 c-font-lock-keywords-2 c-font-lock-keywords-3)
a078558d
SM
498 nil nil ((?_ . "w")) beginning-of-defun
499 (font-lock-mark-block-function . mark-defun)))
fb512de9 500 (c++-mode-defaults
019f00d8 501 '((c++-font-lock-keywords c++-font-lock-keywords-1
9bfbb130 502 c++-font-lock-keywords-2 c++-font-lock-keywords-3)
55015061 503 nil nil ((?_ . "w")) beginning-of-defun
a078558d 504 (font-lock-mark-block-function . mark-defun)))
2e6a15fc
SM
505 (objc-mode-defaults
506 '((objc-font-lock-keywords objc-font-lock-keywords-1
507 objc-font-lock-keywords-2 objc-font-lock-keywords-3)
508 nil nil ((?_ . "w") (?$ . "w")) nil
2e6a15fc
SM
509 (font-lock-mark-block-function . mark-defun)))
510 (java-mode-defaults
511 '((java-font-lock-keywords java-font-lock-keywords-1
512 java-font-lock-keywords-2 java-font-lock-keywords-3)
68c67d1f 513 nil nil ((?_ . "w") (?$ . "w")) nil
2e6a15fc 514 (font-lock-mark-block-function . mark-defun)))
fb512de9 515 (lisp-mode-defaults
9bfbb130
SM
516 '((lisp-font-lock-keywords
517 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
5aa29679 518 nil nil (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
28a53bc1 519 (font-lock-mark-block-function . mark-defun))))
826b2925 520 (list
d46c21ec 521 (cons 'c-mode c-mode-defaults)
2e6a15fc
SM
522 (cons 'c++-mode c++-mode-defaults)
523 (cons 'objc-mode objc-mode-defaults)
524 (cons 'java-mode java-mode-defaults)
d46c21ec 525 (cons 'emacs-lisp-mode lisp-mode-defaults)
d46c21ec 526 (cons 'lisp-mode lisp-mode-defaults)
28a53bc1 527 (cons 'lisp-interaction-mode lisp-mode-defaults)))
2d63aa67 528 "Alist of fall-back Font Lock defaults for major modes.
1c172af4
SM
529
530This variable should not be used any more.
531Set the buffer-local `font-lock-keywords' in the major mode instead.
532
b89e1134 533Each item should be a list of the form:
d46c21ec 534
2d63aa67 535 (MAJOR-MODE . FONT-LOCK-DEFAULTS)
a2b8e66b 536
2d63aa67
SM
537where MAJOR-MODE is a symbol and FONT-LOCK-DEFAULTS is a list of default
538settings. See the variable `font-lock-defaults', which takes precedence.")
1c172af4 539(make-obsolete-variable 'font-lock-defaults-alist 'font-lock-defaults)
d46c21ec 540
c1f2ffc8
SM
541(defvar font-lock-keywords-alist nil
542 "*Alist of `font-lock-keywords' local to a `major-mode'.
76f5e2af
GM
543This is normally set via `font-lock-add-keywords' and
544`font-lock-remove-keywords'.")
545
546(defvar font-lock-removed-keywords-alist nil
547 "*Alist of `font-lock-keywords' removed from `major-mode'.
548This is normally set via `font-lock-add-keywords' and
549`font-lock-remove-keywords'.")
c1f2ffc8 550
9bfbb130 551(defvar font-lock-keywords-only nil
d46c21ec
SM
552 "*Non-nil means Font Lock should not fontify comments or strings.
553This is normally set via `font-lock-defaults'.")
b89e1134 554
030f4a35 555(defvar font-lock-keywords-case-fold-search nil
d46c21ec
SM
556 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
557This is normally set via `font-lock-defaults'.")
1087f198 558(make-variable-buffer-local 'font-lock-keywords-case-fold-search)
030f4a35 559
c0a6a9fe
SM
560(defvar font-lock-syntactically-fontified 0
561 "Point up to which `font-lock-syntactic-keywords' has been applied.
562If nil, this is ignored, in which case the syntactic fontification may
563sometimes be slightly incorrect.")
564(make-variable-buffer-local 'font-lock-syntactically-fontified)
565
1c172af4
SM
566(defvar font-lock-syntactic-face-function
567 (lambda (state)
568 (if (nth 3 state) font-lock-string-face font-lock-comment-face))
569 "Function to determine which face to use when fontifying syntactically.
570The function is called with a single parameter (the state as returned by
571`parse-partial-sexp' at the beginning of the region to highlight) and
572should return a face.")
573
ac6e572c
SM
574(defvar font-lock-syntactic-keywords nil
575 "A list of the syntactic keywords to highlight.
576Can be the list or the name of a function or variable whose value is the list.
577See `font-lock-keywords' for a description of the form of this list;
578the differences are listed below. MATCH-HIGHLIGHT should be of the form:
579
580 (MATCH SYNTAX OVERRIDE LAXMATCH)
581
5299cb8e
SM
582where SYNTAX can be a string (as taken by `modify-syntax-entry'), a syntax
583table, a cons cell (as returned by `string-to-syntax') or an expression whose
584value is such a form. OVERRIDE cannot be `prepend' or `append'.
ac6e572c 585
9c8de95c
SM
586For example, an element of the form highlights syntactically:
587
5299cb8e 588 (\"\\\\$\\\\(#\\\\)\" 1 \".\")
9c8de95c 589
5299cb8e
SM
590 a hash character when following a dollar character, with a SYNTAX of
591 \".\" (meaning punctuation syntax). Assuming that the buffer syntax table does
9c8de95c
SM
592 specify hash characters to have comment start syntax, the element will only
593 highlight hash characters that do not follow dollar characters as comments
594 syntactically.
595
596 (\"\\\\('\\\\).\\\\('\\\\)\"
5299cb8e
SM
597 (1 \"\\\"\")
598 (2 \"\\\"\"))
9c8de95c 599
5299cb8e
SM
600 both single quotes which surround a single character, with a SYNTAX of
601 \"\\\"\" (meaning string quote syntax). Assuming that the buffer syntax table
9c8de95c
SM
602 does not specify single quotes to have quote syntax, the element will only
603 highlight single quotes of the form 'c' as strings syntactically.
604 Other forms, such as foo'bar or 'fubar', will not be highlighted as strings.
605
ac6e572c
SM
606This is normally set via `font-lock-defaults'.")
607
b056da51 608(defvar font-lock-syntax-table nil
799761f0 609 "Non-nil means use this syntax table for fontifying.
d46c21ec
SM
610If this is nil, the major mode's syntax table is used.
611This is normally set via `font-lock-defaults'.")
612
9bfbb130
SM
613;; If this is nil, we only use the beginning of the buffer if we can't use
614;; `font-lock-cache-position' and `font-lock-cache-state'.
d46c21ec 615(defvar font-lock-beginning-of-syntax-function nil
9bfbb130 616 "*Non-nil means use this function to move back outside of a syntactic block.
a078558d
SM
617When called with no args it should leave point at the beginning of any
618enclosing syntactic block.
9bfbb130 619If this is nil, the beginning of the buffer is used (in the worst case).
d46c21ec 620This is normally set via `font-lock-defaults'.")
b056da51 621
a078558d
SM
622(defvar font-lock-mark-block-function nil
623 "*Non-nil means use this function to mark a block of text.
624When called with no args it should leave point at the beginning of any
625enclosing textual block and mark at the end.
626This is normally set via `font-lock-defaults'.")
627
a2b8e66b
SM
628(defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
629 "Function to use for fontifying the buffer.
630This is normally set via `font-lock-defaults'.")
631
632(defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
633 "Function to use for unfontifying the buffer.
634This is used when turning off Font Lock mode.
635This is normally set via `font-lock-defaults'.")
636
637(defvar font-lock-fontify-region-function 'font-lock-default-fontify-region
638 "Function to use for fontifying a region.
639It should take two args, the beginning and end of the region, and an optional
640third arg VERBOSE. If non-nil, the function should print status messages.
641This is normally set via `font-lock-defaults'.")
642
643(defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
644 "Function to use for unfontifying a region.
645It should take two args, the beginning and end of the region.
646This is normally set via `font-lock-defaults'.")
647
648(defvar font-lock-inhibit-thing-lock nil
649 "List of Font Lock mode related modes that should not be turned on.
019f00d8 650Currently, valid mode names are `fast-lock-mode', `jit-lock-mode' and
e7f07c2c 651`lazy-lock-mode'. This is normally set via `font-lock-defaults'.")
a2b8e66b 652
f9261020 653(defvar font-lock-multiline nil
041470d2
SM
654 "Whether font-lock should cater to multiline keywords.
655If nil, don't try to handle multiline patterns.
656If t, always handle multiline patterns.
657If `undecided', don't try to handle multiline patterns until you see one.
658Major/minor modes can set this variable if they know which option applies.")
659
9bfbb130 660(defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
030f4a35 661\f
5aa29679
RS
662;; Font Lock mode.
663
664(eval-when-compile
2e6a15fc 665 ;;
5aa29679 666 ;; We don't do this at the top-level as we only use non-autoloaded macros.
2e6a15fc
SM
667 (require 'cl)
668 ;;
669 ;; Borrowed from lazy-lock.el.
670 ;; We use this to preserve or protect things when modifying text properties.
671 (defmacro save-buffer-state (varlist &rest body)
672 "Bind variables according to VARLIST and eval BODY restoring buffer state."
d2251bbf
SM
673 `(let* ,(append varlist
674 '((modified (buffer-modified-p)) (buffer-undo-list t)
675 (inhibit-read-only t) (inhibit-point-motion-hooks t)
15d1caef 676 (inhibit-modification-hooks t)
d2251bbf
SM
677 deactivate-mark buffer-file-name buffer-file-truename))
678 ,@body
679 (when (and (not modified) (buffer-modified-p))
680 (set-buffer-modified-p nil))))
e0e277ff 681 (put 'save-buffer-state 'lisp-indent-function 1)
afa18a4e 682 (def-edebug-spec save-buffer-state let)
e0e277ff
SM
683 ;;
684 ;; Shut up the byte compiler.
1c172af4 685 (defvar font-lock-face-attributes)) ; Obsolete but respected if set.
030f4a35
RS
686
687;;;###autoload
1c172af4 688(define-minor-mode font-lock-mode
e595fa35 689 "Toggle Font Lock mode.
22cd6fd3
EZ
690With arg, turn Font Lock mode off if and only if arg is a non-positive
691number; if arg is nil, toggle Font Lock mode; anything else turns Font
692Lock on.
c0a6a9fe 693\(Font Lock is also known as \"syntax highlighting\".)
030f4a35
RS
694
695When Font Lock mode is enabled, text is fontified as you type it:
696
a1eb1cf1
RS
697 - Comments are displayed in `font-lock-comment-face';
698 - Strings are displayed in `font-lock-string-face';
699 - Certain other expressions are displayed in other faces according to the
700 value of the variable `font-lock-keywords'.
701
3b967bed
EZ
702To customize the faces (colors, fonts, etc.) used by Font Lock for
703fontifying different parts of buffer text, use \\[customize-face].
704
a1eb1cf1
RS
705You can enable Font Lock mode in any major mode automatically by turning on in
706the major mode's hook. For example, put in your ~/.emacs:
707
708 (add-hook 'c-mode-hook 'turn-on-font-lock)
709
a2b8e66b 710Alternatively, you can use Global Font Lock mode to automagically turn on Font
fd23afbe
SM
711Lock mode in buffers whose major mode supports it and whose major mode is one
712of `font-lock-global-modes'. For example, put in your ~/.emacs:
a2b8e66b
SM
713
714 (global-font-lock-mode t)
715
fd23afbe
SM
716There are a number of support modes that may be used to speed up Font Lock mode
717in various ways, specified via the variable `font-lock-support-mode'. Where
718major modes support different levels of fontification, you can use the variable
fb512de9 719`font-lock-maximum-decoration' to specify which level you generally prefer.
b89e1134
SM
720When you turn Font Lock mode on/off the buffer is fontified/defontified, though
721fontification occurs only if the buffer is less than `font-lock-maximum-size'.
7d7d915a 722
fd23afbe
SM
723For example, to specify that Font Lock mode use use Lazy Lock mode as a support
724mode and use maximum levels of fontification, put in your ~/.emacs:
725
726 (setq font-lock-support-mode 'lazy-lock-mode)
727 (setq font-lock-maximum-decoration t)
728
56fcbd7e
SM
729To add your own highlighting for some major mode, and modify the highlighting
730selected automatically via the variable `font-lock-maximum-decoration', you can
731use `font-lock-add-keywords'.
732
7d7d915a
SM
733To fontify a buffer, without turning on Font Lock mode and regardless of buffer
734size, you can use \\[font-lock-fontify-buffer].
a078558d
SM
735
736To fontify a block (the function or paragraph containing point, or a number of
737lines around point), perhaps because modification on the current line caused
fd23afbe
SM
738syntactic change on other lines, you can use \\[font-lock-fontify-block].
739
2273c36b
SM
740See the variable `font-lock-defaults-alist' for the Font Lock mode default
741settings. You can set your own default settings for some mode, by setting a
742buffer local value for `font-lock-defaults', via its mode hook."
1c172af4 743 nil nil nil
a2b8e66b
SM
744 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
745 ;; batch job) or if the buffer is invisible (the name starts with a space).
1c172af4
SM
746 (when (or noninteractive (eq (aref (buffer-name) 0) ?\ ))
747 (setq font-lock-mode nil))
748
749 ;; Turn on Font Lock mode.
750 (when font-lock-mode
751 (add-hook 'after-change-functions 'font-lock-after-change-function nil t)
752 (font-lock-set-defaults)
753 (font-lock-turn-on-thing-lock)
754 ;; Fontify the buffer if we have to.
755 (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size)))
756 (cond (font-lock-fontified
757 nil)
758 ((or (null max-size) (> max-size (buffer-size)))
759 (font-lock-fontify-buffer))
760 (font-lock-verbose
761 (message "Fontifying %s...buffer size greater than font-lock-maximum-size"
762 (buffer-name))))))
763 ;; Turn off Font Lock mode.
764 (unless font-lock-mode
765 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
766 (font-lock-unfontify-buffer)
767 (font-lock-turn-off-thing-lock)
768 (font-lock-unset-defaults)))
030f4a35 769
a1eb1cf1
RS
770;;;###autoload
771(defun turn-on-font-lock ()
1c172af4 772 "Turn on Font Lock mode (only if the terminal can display it)."
01b69148 773 (unless font-lock-mode
56fcbd7e 774 (font-lock-mode)))
c1f2ffc8
SM
775
776;;;###autoload
041470d2
SM
777(defun font-lock-add-keywords (mode keywords &optional append)
778 "Add highlighting KEYWORDS for MODE.
779MODE should be a symbol, the major mode command name, such as `c-mode'
1c626aaf 780or nil. If nil, highlighting keywords are added for the current buffer.
c1f2ffc8
SM
781KEYWORDS should be a list; see the variable `font-lock-keywords'.
782By default they are added at the beginning of the current highlighting list.
783If optional argument APPEND is `set', they are used to replace the current
2d63aa67
SM
784highlighting list. If APPEND is any other non-nil value, they are added at the
785end of the current highlighting list.
c1f2ffc8
SM
786
787For example:
788
789 (font-lock-add-keywords 'c-mode
790 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
791 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . font-lock-keyword-face)))
792
793adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
2d63aa67
SM
794comments, and to fontify `and', `or' and `not' words as keywords.
795
796Note that some modes have specialised support for additional patterns, e.g.,
797see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
798`objc-font-lock-extra-types' and `java-font-lock-extra-types'."
041470d2
SM
799 (cond (mode
800 ;; If MODE is non-nil, add the KEYWORDS and APPEND spec to
c1f2ffc8
SM
801 ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
802 (let ((spec (cons keywords append)) cell)
041470d2 803 (if (setq cell (assq mode font-lock-keywords-alist))
76f5e2af
GM
804 (if (eq append 'set)
805 (setcdr cell (list spec))
806 (setcdr cell (append (cdr cell) (list spec))))
807 (push (list mode spec) font-lock-keywords-alist)))
808 ;; Make sure that `font-lock-removed-keywords-alist' does not
809 ;; contain the new keywords.
810 (font-lock-update-removed-keyword-alist mode keywords append))
6e1d0d15
SM
811 (t
812 ;; Otherwise set or add the keywords now.
813 (font-lock-set-defaults)
814 (if (eq append 'set)
815 (setq font-lock-keywords keywords)
816 (font-lock-remove-keywords nil keywords) ;to avoid duplicates
c1f2ffc8
SM
817 (let ((old (if (eq (car-safe font-lock-keywords) t)
818 (cdr font-lock-keywords)
819 font-lock-keywords)))
820 (setq font-lock-keywords (if append
821 (append old keywords)
822 (append keywords old))))))))
3708dfe9 823
6e1d0d15 824(defun font-lock-update-removed-keyword-alist (mode keywords append)
76f5e2af 825 ;; Update `font-lock-removed-keywords-alist' when adding new
6e1d0d15 826 ;; KEYWORDS to MODE.
76f5e2af
GM
827 ;;
828 ;; When font-lock is enabled first all keywords in the list
829 ;; `font-lock-keywords-alist' are added, then all keywords in the
830 ;; list `font-lock-removed-keywords-alist' are removed. If a
831 ;; keyword was once added, removed, and then added again it must be
832 ;; removed from the removed-keywords list. Otherwise the second add
833 ;; will not take effect.
6e1d0d15 834 (let ((cell (assq mode font-lock-removed-keywords-alist)))
76f5e2af
GM
835 (if cell
836 (if (eq append 'set)
837 ;; A new set of keywords is defined. Forget all about
838 ;; our old keywords that should be removed.
839 (setq font-lock-removed-keywords-alist
840 (delq cell font-lock-removed-keywords-alist))
841 ;; Delete all previously removed keywords.
842 (dolist (kword keywords)
843 (setcdr cell (delete kword (cdr cell))))
6e1d0d15 844 ;; Delete the mode cell if empty.
76f5e2af
GM
845 (if (null (cdr cell))
846 (setq font-lock-removed-keywords-alist
847 (delq cell font-lock-removed-keywords-alist)))))))
848
849;; Written by Anders Lindgren <andersl@andersl.com>.
850;;
851;; Case study:
852;; (I) The keywords are removed from a major mode.
853;; In this case the keyword could be local (i.e. added earlier by
854;; `font-lock-add-keywords'), global, or both.
855;;
856;; (a) In the local case we remove the keywords from the variable
857;; `font-lock-keywords-alist'.
858;;
859;; (b) The actual global keywords are not known at this time.
860;; All keywords are added to `font-lock-removed-keywords-alist',
861;; when font-lock is enabled those keywords are removed.
862;;
863;; Note that added keywords are taken out of the list of removed
864;; keywords. This ensure correct operation when the same keyword
865;; is added and removed several times.
866;;
867;; (II) The keywords are removed from the current buffer.
3708dfe9 868;;;###autoload
6e1d0d15
SM
869(defun font-lock-remove-keywords (mode keywords)
870 "Remove highlighting KEYWORDS for MODE.
3708dfe9 871
6e1d0d15 872MODE should be a symbol, the major mode command name, such as `c-mode'
76f5e2af 873or nil. If nil, highlighting keywords are removed for the current buffer."
6e1d0d15
SM
874 (cond (mode
875 ;; Remove one keyword at the time.
876 (dolist (keyword keywords)
877 (let ((top-cell (assq mode font-lock-keywords-alist)))
878 ;; If MODE is non-nil, remove the KEYWORD from
76f5e2af
GM
879 ;; `font-lock-keywords-alist'.
880 (when top-cell
881 (dolist (keyword-list-append-pair (cdr top-cell))
882 ;; `keywords-list-append-pair' is a cons with a list of
883 ;; keywords in the car top-cell and the original append
884 ;; argument in the cdr top-cell.
885 (setcar keyword-list-append-pair
886 (delete keyword (car keyword-list-append-pair))))
887 ;; Remove keyword list/append pair when the keyword list
888 ;; is empty and append doesn't specify `set'. (If it
889 ;; should be deleted then previously deleted keywords
890 ;; would appear again.)
891 (let ((cell top-cell))
892 (while (cdr cell)
893 (if (and (null (car (car (cdr cell))))
894 (not (eq (cdr (car (cdr cell))) 'set)))
895 (setcdr cell (cdr (cdr cell)))
896 (setq cell (cdr cell)))))
897 ;; Final cleanup, remove major mode cell if last keyword
898 ;; was deleted.
899 (if (null (cdr top-cell))
900 (setq font-lock-keywords-alist
901 (delq top-cell font-lock-keywords-alist))))
902 ;; Remember the keyword in case it is not local.
6e1d0d15 903 (let ((cell (assq mode font-lock-removed-keywords-alist)))
76f5e2af
GM
904 (if cell
905 (unless (member keyword (cdr cell))
906 (nconc cell (list keyword)))
6e1d0d15
SM
907 (push (cons mode (list keyword))
908 font-lock-removed-keywords-alist))))))
909 (t
910 ;; Otherwise remove it immediately.
911 (font-lock-set-defaults)
912 (setq font-lock-keywords (copy-sequence font-lock-keywords))
913 (dolist (keyword keywords)
76f5e2af 914 (setq font-lock-keywords
6e1d0d15
SM
915 (delete keyword
916 ;; The keywords might be compiled.
917 (delete (font-lock-compile-keyword keyword)
918 font-lock-keywords)))))))
a2b8e66b 919\f
2d63aa67
SM
920;;; Global Font Lock mode.
921
a2b8e66b 922;; A few people have hassled in the past for a way to make it easier to turn on
5aa29679
RS
923;; Font Lock mode, without the user needing to know for which modes s/he has to
924;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always
a2b8e66b
SM
925;; balked at that way, as I see it as just re-moulding the same problem in
926;; another form. That is; some person would still have to keep track of which
927;; modes (which may not even be distributed with Emacs) support Font Lock mode.
928;; The list would always be out of date. And that person might have to be me.
929
5aa29679
RS
930;; Implementation.
931;;
932;; In a previous discussion the following hack came to mind. It is a gross
933;; hack, but it generally works. We use the convention that major modes start
934;; by calling the function `kill-all-local-variables', which in turn runs
a2b8e66b
SM
935;; functions on the hook variable `change-major-mode-hook'. We attach our
936;; function `font-lock-change-major-mode' to that hook. Of course, when this
937;; hook is run, the major mode is in the process of being changed and we do not
938;; know what the final major mode will be. So, `font-lock-change-major-mode'
939;; only (a) notes the name of the current buffer, and (b) adds our function
5aa29679
RS
940;; `turn-on-font-lock-if-enabled' to the hook variables `find-file-hooks' and
941;; `post-command-hook' (for buffers that are not visiting files). By the time
942;; the functions on the first of these hooks to be run are run, the new major
943;; mode is assumed to be in place. This way we get a Font Lock function run
944;; when a major mode is turned on, without knowing major modes or their hooks.
945;;
a2b8e66b
SM
946;; Naturally this requires that (a) major modes run `kill-all-local-variables',
947;; as they are supposed to do, and (b) the major mode is in place after the
5aa29679
RS
948;; file is visited or the command that ran `kill-all-local-variables' has
949;; finished, whichever the sooner. Arguably, any major mode that does not
950;; follow the convension (a) is broken, and I can't think of any reason why (b)
951;; would not be met (except `gnudoit' on non-files). However, it is not clean.
952;;
a2b8e66b
SM
953;; Probably the cleanest solution is to have each major mode function run some
954;; hook, e.g., `major-mode-hook', but maybe implementing that change is
5aa29679
RS
955;; impractical. I am personally against making `setq' a macro or be advised,
956;; or have a special function such as `set-major-mode', but maybe someone can
957;; come up with another solution?
958
959;; User interface.
960;;
961;; Although Global Font Lock mode is a pseudo-mode, I think that the user
962;; interface should conform to the usual Emacs convention for modes, i.e., a
963;; command to toggle the feature (`global-font-lock-mode') with a variable for
964;; finer control of the mode's behaviour (`font-lock-global-modes').
965;;
2e6a15fc
SM
966;; The feature should not be enabled by loading font-lock.el, since other
967;; mechanisms for turning on Font Lock mode, such as M-x font-lock-mode RET or
968;; (add-hook 'c-mode-hook 'turn-on-font-lock), would cause Font Lock mode to be
969;; turned on everywhere. That would not be intuitive or informative because
970;; loading a file tells you nothing about the feature or how to control it. It
1c626aaf 971;; would also be contrary to the Principle of Least Surprise. sm.
a2b8e66b 972
23a0e11b
SM
973(defcustom font-lock-global-modes t
974 "*Modes for which Font Lock mode is automagically turned on.
019f00d8 975Global Font Lock mode is controlled by the command `global-font-lock-mode'.
23a0e11b
SM
976If nil, means no modes have Font Lock mode automatically turned on.
977If t, all modes that support Font Lock mode have it automatically turned on.
978If a list, it should be a list of `major-mode' symbol names for which Font Lock
979mode should be automatically turned on. The sense of the list is negated if it
980begins with `not'. For example:
981 (c-mode c++-mode)
982means that Font Lock mode is turned on for buffers in C and C++ modes only."
983 :type '(choice (const :tag "none" nil)
984 (const :tag "all" t)
985 (set :menu-tag "mode specific" :tag "modes"
986 :value (not)
987 (const :tag "Except" not)
988 (repeat :inline t (symbol :tag "mode"))))
989 :group 'font-lock)
990
a465832f 991(defun turn-on-font-lock-if-enabled ()
ba22aeff
SM
992 (when (and (or font-lock-defaults
993 (assq major-mode font-lock-defaults-alist))
994 (or (eq font-lock-global-modes t)
995 (if (eq (car-safe font-lock-global-modes) 'not)
996 (not (memq major-mode (cdr font-lock-global-modes)))
997 (memq major-mode font-lock-global-modes))))
998 (let (inhibit-quit)
999 (turn-on-font-lock))))
1000
1001;;;###autoload
1002(easy-mmode-define-global-mode
1003 global-font-lock-mode font-lock-mode turn-on-font-lock-if-enabled
1004 :extra-args (dummy))
5aa29679 1005
2d63aa67 1006;;; End of Global Font Lock mode.
a2b8e66b 1007\f
2d63aa67
SM
1008;;; Font Lock Support mode.
1009
5aa29679
RS
1010;; This is the code used to interface font-lock.el with any of its add-on
1011;; packages, and provide the user interface. Packages that have their own
1012;; local buffer fontification functions (see below) may have to call
1013;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
1014;; themselves.
1015
5f999b0b 1016(defcustom font-lock-support-mode 'jit-lock-mode
5aa29679
RS
1017 "*Support mode for Font Lock mode.
1018Support modes speed up Font Lock mode by being choosy about when fontification
e7f07c2c
GM
1019occurs. Known support modes are Fast Lock mode (symbol `fast-lock-mode'),
1020Lazy Lock mode (symbol `lazy-lock-mode'), and Just-in-time Lock mode (symbol
1021`jit-lock-mode'. See those modes for more info.
5aa29679
RS
1022If nil, means support for Font Lock mode is never performed.
1023If a symbol, use that support mode.
1024If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
1025where MAJOR-MODE is a symbol or t (meaning the default). For example:
1026 ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
1027means that Fast Lock mode is used to support Font Lock mode for buffers in C or
1028C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
1029
55015061 1030The value of this variable is used when Font Lock mode is turned on."
b34aa0a3
SM
1031 :type '(choice (const :tag "none" nil)
1032 (const :tag "fast lock" fast-lock-mode)
1033 (const :tag "lazy lock" lazy-lock-mode)
e7f07c2c 1034 (const :tag "jit lock" jit-lock-mode)
b34aa0a3 1035 (repeat :menu-tag "mode specific" :tag "mode specific"
1c172af4 1036 :value ((t . jit-lock-mode))
b34aa0a3
SM
1037 (cons :tag "Instance"
1038 (radio :tag "Mode"
1039 (const :tag "all" t)
1040 (symbol :tag "name"))
39df451b
SM
1041 (radio :tag "Support"
1042 (const :tag "none" nil)
b34aa0a3 1043 (const :tag "fast lock" fast-lock-mode)
e7f07c2c
GM
1044 (const :tag "lazy lock" lazy-lock-mode)
1045 (const :tag "JIT lock" jit-lock-mode)))
b34aa0a3 1046 ))
c69e5fcd 1047 :version "21.1"
55015061 1048 :group 'font-lock)
5aa29679 1049
2e6a15fc
SM
1050(defvar fast-lock-mode nil)
1051(defvar lazy-lock-mode nil)
e7f07c2c 1052(defvar jit-lock-mode nil)
2e6a15fc 1053
5aa29679
RS
1054(defun font-lock-turn-on-thing-lock ()
1055 (let ((thing-mode (font-lock-value-in-major-mode font-lock-support-mode)))
1056 (cond ((eq thing-mode 'fast-lock-mode)
1057 (fast-lock-mode t))
1058 ((eq thing-mode 'lazy-lock-mode)
e7f07c2c
GM
1059 (lazy-lock-mode t))
1060 ((eq thing-mode 'jit-lock-mode)
28a53bc1
SM
1061 ;; Prepare for jit-lock
1062 (remove-hook 'after-change-functions
1063 'font-lock-after-change-function t)
1064 (set (make-local-variable 'font-lock-fontify-buffer-function)
fd612dd9 1065 'jit-lock-refontify)
28a53bc1
SM
1066 ;; Don't fontify eagerly (and don't abort is the buffer is large).
1067 (set (make-local-variable 'font-lock-fontified) t)
1068 ;; Use jit-lock.
1069 (jit-lock-register 'font-lock-fontify-region
1070 (not font-lock-keywords-only))))))
5aa29679 1071
5aa29679
RS
1072(defun font-lock-turn-off-thing-lock ()
1073 (cond (fast-lock-mode
1c172af4 1074 (fast-lock-mode -1))
e7f07c2c 1075 (jit-lock-mode
28a53bc1
SM
1076 (jit-lock-unregister 'font-lock-fontify-region)
1077 ;; Reset local vars to the non-jit-lock case.
1078 (kill-local-variable 'font-lock-fontify-buffer-function))
5aa29679 1079 (lazy-lock-mode
1c172af4 1080 (lazy-lock-mode -1))))
5aa29679
RS
1081
1082(defun font-lock-after-fontify-buffer ()
1083 (cond (fast-lock-mode
1084 (fast-lock-after-fontify-buffer))
13f0d185
SM
1085 ;; Useless now that jit-lock intercepts font-lock-fontify-buffer. -sm
1086 ;; (jit-lock-mode
1087 ;; (jit-lock-after-fontify-buffer))
5aa29679
RS
1088 (lazy-lock-mode
1089 (lazy-lock-after-fontify-buffer))))
1090
1091(defun font-lock-after-unfontify-buffer ()
1092 (cond (fast-lock-mode
1093 (fast-lock-after-unfontify-buffer))
13f0d185
SM
1094 ;; Useless as well. It's only called when:
1095 ;; - turning off font-lock: it does not matter if we leave spurious
1096 ;; `fontified' text props around since jit-lock-mode is also off.
1097 ;; - font-lock-default-fontify-buffer fails: this is not run
1098 ;; any more anyway. -sm
5e9e032a 1099 ;;
13f0d185
SM
1100 ;; (jit-lock-mode
1101 ;; (jit-lock-after-unfontify-buffer))
5aa29679
RS
1102 (lazy-lock-mode
1103 (lazy-lock-after-unfontify-buffer))))
1104
2d63aa67 1105;;; End of Font Lock Support mode.
5aa29679 1106\f
2d63aa67 1107;;; Fontification functions.
a1eb1cf1 1108
56fcbd7e
SM
1109;; Rather than the function, e.g., `font-lock-fontify-region' containing the
1110;; code to fontify a region, the function runs the function whose name is the
1111;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally,
1112;; the value of this variable is, e.g., `font-lock-default-fontify-region'
1113;; which does contain the code to fontify a region. However, the value of the
1114;; variable could be anything and thus, e.g., `font-lock-fontify-region' could
1115;; do anything. The indirection of the fontification functions gives major
1116;; modes the capability of modifying the way font-lock.el fontifies. Major
1117;; modes can modify the values of, e.g., `font-lock-fontify-region-function',
1118;; via the variable `font-lock-defaults'.
1119;;
1120;; For example, Rmail mode sets the variable `font-lock-defaults' so that
1121;; font-lock.el uses its own function for buffer fontification. This function
1122;; makes fontification be on a message-by-message basis and so visiting an
1123;; RMAIL file is much faster. A clever implementation of the function might
1124;; fontify the headers differently than the message body. (It should, and
1125;; correspondingly for Mail mode, but I can't be bothered to do the work. Can
1126;; you?) This hints at a more interesting use...
1127;;
1128;; Languages that contain text normally contained in different major modes
1129;; could define their own fontification functions that treat text differently
1130;; depending on its context. For example, Perl mode could arrange that here
1131;; docs are fontified differently than Perl code. Or Yacc mode could fontify
1132;; rules one way and C code another. Neat!
1133;;
1134;; A further reason to use the fontification indirection feature is when the
1135;; default syntactual fontification, or the default fontification in general,
1136;; is not flexible enough for a particular major mode. For example, perhaps
55015061
SM
1137;; comments are just too hairy for `font-lock-fontify-syntactically-region' to
1138;; cope with. You need to write your own version of that function, e.g.,
1139;; `hairy-fontify-syntactically-region', and make your own version of
1140;; `hairy-fontify-region' call that function before calling
56fcbd7e
SM
1141;; `font-lock-fontify-keywords-region' for the normal regexp fontification
1142;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el
1143;; would call your region fontification function instead of its own. For
55015061 1144;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line
56fcbd7e
SM
1145;; directives correctly and cleanly. (It is the same problem as fontifying
1146;; multi-line strings and comments; regexps are not appropriate for the job.)
1147
a1eb1cf1 1148;;;###autoload
030f4a35 1149(defun font-lock-fontify-buffer ()
019f00d8 1150 "Fontify the current buffer the way the function `font-lock-mode' would."
030f4a35 1151 (interactive)
a2b8e66b
SM
1152 (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
1153 (funcall font-lock-fontify-buffer-function)))
1154
1155(defun font-lock-unfontify-buffer ()
1156 (funcall font-lock-unfontify-buffer-function))
1157
1158(defun font-lock-fontify-region (beg end &optional loudly)
1159 (funcall font-lock-fontify-region-function beg end loudly))
1160
1161(defun font-lock-unfontify-region (beg end)
1162 (funcall font-lock-unfontify-region-function beg end))
1163
1164(defun font-lock-default-fontify-buffer ()
5aa29679
RS
1165 (let ((verbose (if (numberp font-lock-verbose)
1166 (> (buffer-size) font-lock-verbose)
1167 font-lock-verbose)))
79f238c9 1168 (with-temp-message
1787769b
SM
1169 (when verbose
1170 (format "Fontifying %s..." (buffer-name)))
79f238c9
SM
1171 ;; Make sure we have the right `font-lock-keywords' etc.
1172 (unless font-lock-mode
1173 (font-lock-set-defaults))
1174 ;; Make sure we fontify etc. in the whole buffer.
1175 (save-restriction
1176 (widen)
1177 (condition-case nil
1178 (save-excursion
1179 (save-match-data
1180 (font-lock-fontify-region (point-min) (point-max) verbose)
1181 (font-lock-after-fontify-buffer)
1182 (setq font-lock-fontified t)))
1183 ;; We don't restore the old fontification, so it's best to unfontify.
1184 (quit (font-lock-unfontify-buffer))))
1185 ;; Make sure we undo `font-lock-keywords' etc.
1186 (unless font-lock-mode
1187 (font-lock-unset-defaults)))))
7d7d915a 1188
a2b8e66b 1189(defun font-lock-default-unfontify-buffer ()
56fcbd7e 1190 ;; Make sure we unfontify etc. in the whole buffer.
a2b8e66b
SM
1191 (save-restriction
1192 (widen)
1193 (font-lock-unfontify-region (point-min) (point-max))
271c888a 1194 (font-lock-after-unfontify-buffer)
a2b8e66b 1195 (setq font-lock-fontified nil)))
9bfbb130 1196
a2b8e66b 1197(defun font-lock-default-fontify-region (beg end loudly)
ac6e572c
SM
1198 (save-buffer-state
1199 ((parse-sexp-lookup-properties font-lock-syntactic-keywords)
1200 (old-syntax-table (syntax-table)))
876f2438 1201 (unwind-protect
a078558d
SM
1202 (save-restriction
1203 (widen)
876f2438 1204 ;; Use the fontification syntax table, if any.
2e6a15fc
SM
1205 (when font-lock-syntax-table
1206 (set-syntax-table font-lock-syntax-table))
fb0e4f8a
RS
1207 ;; check to see if we should expand the beg/end area for
1208 ;; proper multiline matches
f9261020
GM
1209 (when (and font-lock-multiline
1210 (> beg (point-min))
28a53bc1
SM
1211 (get-text-property (1- beg) 'font-lock-multiline))
1212 ;; We are just after or in a multiline match.
1213 (setq beg (or (previous-single-property-change
1214 beg 'font-lock-multiline)
ba22aeff
SM
1215 (point-min)))
1216 (goto-char beg)
1217 (setq beg (line-beginning-position)))
111c181e
GM
1218 (when font-lock-multiline
1219 (setq end (or (text-property-any end (point-max)
1220 'font-lock-multiline nil)
1221 (point-max))))
ba22aeff 1222 (goto-char end)
724a330f 1223 (setq end (line-beginning-position 2))
876f2438 1224 ;; Now do the fontification.
2e6a15fc 1225 (font-lock-unfontify-region beg end)
ac6e572c
SM
1226 (when font-lock-syntactic-keywords
1227 (font-lock-fontify-syntactic-keywords-region beg end))
2e6a15fc 1228 (unless font-lock-keywords-only
876f2438
SM
1229 (font-lock-fontify-syntactically-region beg end loudly))
1230 (font-lock-fontify-keywords-region beg end loudly))
1231 ;; Clean up.
2e6a15fc 1232 (set-syntax-table old-syntax-table))))
9bfbb130
SM
1233
1234;; The following must be rethought, since keywords can override fontification.
1235; ;; Now scan for keywords, but not if we are inside a comment now.
1236; (or (and (not font-lock-keywords-only)
019f00d8 1237; (let ((state (parse-partial-sexp beg end nil nil
9bfbb130
SM
1238; font-lock-cache-state)))
1239; (or (nth 4 state) (nth 7 state))))
1240; (font-lock-fontify-keywords-region beg end))
1241
a2b8e66b 1242(defun font-lock-default-unfontify-region (beg end)
2e6a15fc 1243 (save-buffer-state nil
fb0e4f8a
RS
1244 (remove-text-properties beg end
1245 (if font-lock-syntactic-keywords
1246 '(face nil syntax-table nil font-lock-multiline nil)
1247 '(face nil font-lock-multiline nil)))))
9bfbb130
SM
1248
1249;; Called when any modification is made to buffer text.
1250(defun font-lock-after-change-function (beg end old-len)
46f26fcf
SM
1251 (let ((inhibit-point-motion-hooks t))
1252 (save-excursion
1253 (save-match-data
1254 ;; Rescan between start of lines enclosing the region.
1255 (font-lock-fontify-region
1256 (progn (goto-char beg) (beginning-of-line) (point))
1257 (progn (goto-char end) (forward-line 1) (point)))))))
a2b8e66b 1258
a078558d
SM
1259(defun font-lock-fontify-block (&optional arg)
1260 "Fontify some lines the way `font-lock-fontify-buffer' would.
1261The lines could be a function or paragraph, or a specified number of lines.
a078558d 1262If ARG is given, fontify that many lines before and after point, or 16 lines if
a465832f
SM
1263no ARG is given and `font-lock-mark-block-function' is nil.
1264If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
1265delimit the region to fontify."
a078558d 1266 (interactive "P")
46f26fcf
SM
1267 (let ((inhibit-point-motion-hooks t) font-lock-beginning-of-syntax-function
1268 deactivate-mark)
a078558d
SM
1269 ;; Make sure we have the right `font-lock-keywords' etc.
1270 (if (not font-lock-mode) (font-lock-set-defaults))
a2b8e66b
SM
1271 (save-excursion
1272 (save-match-data
1273 (condition-case error-data
a078558d
SM
1274 (if (or arg (not font-lock-mark-block-function))
1275 (let ((lines (if arg (prefix-numeric-value arg) 16)))
1276 (font-lock-fontify-region
1277 (save-excursion (forward-line (- lines)) (point))
1278 (save-excursion (forward-line lines) (point))))
1279 (funcall font-lock-mark-block-function)
1280 (font-lock-fontify-region (point) (mark)))
6c0fc428 1281 ((error quit) (message "Fontifying block...%s" error-data)))))))
a078558d 1282
94de9afe 1283(define-key facemenu-keymap "\M-g" 'font-lock-fontify-block)
2d63aa67
SM
1284
1285;;; End of Fontification functions.
9bfbb130 1286\f
9bfbb130
SM
1287;;; Additional text property functions.
1288
1c626aaf
SM
1289;; The following text property functions should be builtins. This means they
1290;; should be written in C and put with all the other text property functions.
1291;; In the meantime, those that are used by font-lock.el are defined in Lisp
1292;; below and given a `font-lock-' prefix. Those that are not used are defined
1293;; in Lisp below and commented out. sm.
9bfbb130 1294
1c626aaf 1295(defun font-lock-prepend-text-property (start end prop value &optional object)
9bfbb130
SM
1296 "Prepend to one property of the text from START to END.
1297Arguments PROP and VALUE specify the property and value to prepend to the value
1c626aaf 1298already in place. The resulting property values are always lists.
9bfbb130
SM
1299Optional argument OBJECT is the string or buffer containing the text."
1300 (let ((val (if (listp value) value (list value))) next prev)
1301 (while (/= start end)
1302 (setq next (next-single-property-change start prop object end)
1303 prev (get-text-property start prop object))
1c626aaf
SM
1304 (put-text-property start next prop
1305 (append val (if (listp prev) prev (list prev)))
1306 object)
9bfbb130
SM
1307 (setq start next))))
1308
1c626aaf 1309(defun font-lock-append-text-property (start end prop value &optional object)
9bfbb130
SM
1310 "Append to one property of the text from START to END.
1311Arguments PROP and VALUE specify the property and value to append to the value
1c626aaf 1312already in place. The resulting property values are always lists.
9bfbb130
SM
1313Optional argument OBJECT is the string or buffer containing the text."
1314 (let ((val (if (listp value) value (list value))) next prev)
1315 (while (/= start end)
1316 (setq next (next-single-property-change start prop object end)
1317 prev (get-text-property start prop object))
1c626aaf
SM
1318 (put-text-property start next prop
1319 (append (if (listp prev) prev (list prev)) val)
1320 object)
9bfbb130 1321 (setq start next))))
1c626aaf
SM
1322
1323(defun font-lock-fillin-text-property (start end prop value &optional object)
1324 "Fill in one property of the text from START to END.
1325Arguments PROP and VALUE specify the property and value to put where none are
1326already in place. Therefore existing property values are not overwritten.
1327Optional argument OBJECT is the string or buffer containing the text."
1328 (let ((start (text-property-any start end prop nil object)) next)
1329 (while start
1330 (setq next (next-single-property-change start prop object end))
1331 (put-text-property start next prop value object)
1332 (setq start (text-property-any next end prop nil object)))))
1333
1334;; For completeness: this is to `remove-text-properties' as `put-text-property'
1335;; is to `add-text-properties', etc.
1336;(defun remove-text-property (start end property &optional object)
1337; "Remove a property from text from START to END.
1338;Argument PROPERTY is the property to remove.
1339;Optional argument OBJECT is the string or buffer containing the text.
1340;Return t if the property was actually removed, nil otherwise."
1341; (remove-text-properties start end (list property) object))
1342
1343;; For consistency: maybe this should be called `remove-single-property' like
1344;; `next-single-property-change' (not `next-single-text-property-change'), etc.
1345;(defun remove-single-text-property (start end prop value &optional object)
1346; "Remove a specific property value from text from START to END.
1347;Arguments PROP and VALUE specify the property and value to remove. The
1348;resulting property values are not equal to VALUE nor lists containing VALUE.
1349;Optional argument OBJECT is the string or buffer containing the text."
1350; (let ((start (text-property-not-all start end prop nil object)) next prev)
1351; (while start
1352; (setq next (next-single-property-change start prop object end)
1353; prev (get-text-property start prop object))
1354; (cond ((and (symbolp prev) (eq value prev))
1355; (remove-text-property start next prop object))
1356; ((and (listp prev) (memq value prev))
1357; (let ((new (delq value prev)))
1358; (cond ((null new)
1359; (remove-text-property start next prop object))
1360; ((= (length new) 1)
1361; (put-text-property start next prop (car new) object))
1362; (t
1363; (put-text-property start next prop new object))))))
1364; (setq start (text-property-not-all next end prop nil object)))))
2d63aa67
SM
1365
1366;;; End of Additional text property functions.
9bfbb130 1367\f
ac6e572c
SM
1368;;; Syntactic regexp fontification functions.
1369
1370;; These syntactic keyword pass functions are identical to those keyword pass
1371;; functions below, with the following exceptions; (a) they operate on
1372;; `font-lock-syntactic-keywords' of course, (b) they are all `defun' as speed
1373;; is less of an issue, (c) eval of property value does not occur JIT as speed
1374;; is less of an issue, (d) OVERRIDE cannot be `prepend' or `append' as it
1375;; makes no sense for `syntax-table' property values, (e) they do not do it
1376;; LOUDLY as it is not likely to be intensive.
1377
1378(defun font-lock-apply-syntactic-highlight (highlight)
1379 "Apply HIGHLIGHT following a match.
1380HIGHLIGHT should be of the form MATCH-HIGHLIGHT,
1381see `font-lock-syntactic-keywords'."
1382 (let* ((match (nth 0 highlight))
1383 (start (match-beginning match)) (end (match-end match))
1384 (value (nth 1 highlight))
1385 (override (nth 2 highlight)))
1c172af4
SM
1386 (when (and (consp value) (not (numberp (car value))))
1387 (setq value (eval value)))
1388 (when (stringp value) (setq value (string-to-syntax value)))
ac6e572c
SM
1389 (cond ((not start)
1390 ;; No match but we might not signal an error.
1391 (or (nth 3 highlight)
1392 (error "No match %d in highlight %S" match highlight)))
1393 ((not override)
1394 ;; Cannot override existing fontification.
1395 (or (text-property-not-all start end 'syntax-table nil)
1396 (put-text-property start end 'syntax-table value)))
1397 ((eq override t)
1398 ;; Override existing fontification.
1399 (put-text-property start end 'syntax-table value))
1400 ((eq override 'keep)
1401 ;; Keep existing fontification.
1402 (font-lock-fillin-text-property start end 'syntax-table value)))))
1403
1404(defun font-lock-fontify-syntactic-anchored-keywords (keywords limit)
1405 "Fontify according to KEYWORDS until LIMIT.
1406KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1407LIMIT can be modified by the value of its PRE-MATCH-FORM."
1408 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1409 ;; Evaluate PRE-MATCH-FORM.
1410 (pre-match-value (eval (nth 1 keywords))))
1411 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1412 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1413 (setq limit pre-match-value)
6e1d0d15 1414 (setq limit (line-end-position)))
ac6e572c
SM
1415 (save-match-data
1416 ;; Find an occurrence of `matcher' before `limit'.
1417 (while (if (stringp matcher)
1418 (re-search-forward matcher limit t)
1419 (funcall matcher limit))
1420 ;; Apply each highlight to this instance of `matcher'.
1421 (setq highlights lowdarks)
1422 (while highlights
1423 (font-lock-apply-syntactic-highlight (car highlights))
1424 (setq highlights (cdr highlights)))))
1425 ;; Evaluate POST-MATCH-FORM.
1426 (eval (nth 2 keywords))))
1427
1428(defun font-lock-fontify-syntactic-keywords-region (start end)
1429 "Fontify according to `font-lock-syntactic-keywords' between START and END.
1430START should be at the beginning of a line."
c0a6a9fe
SM
1431 ;; Ensure the beginning of the file is properly syntactic-fontified.
1432 (when (and font-lock-syntactically-fontified
1433 (< font-lock-syntactically-fontified start))
1434 (setq start (max font-lock-syntactically-fontified (point-min)))
1435 (setq font-lock-syntactically-fontified end))
ac6e572c
SM
1436 ;; If `font-lock-syntactic-keywords' is a symbol, get the real keywords.
1437 (when (symbolp font-lock-syntactic-keywords)
1438 (setq font-lock-syntactic-keywords (font-lock-eval-keywords
1439 font-lock-syntactic-keywords)))
1440 ;; If `font-lock-syntactic-keywords' is not compiled, compile it.
1441 (unless (eq (car font-lock-syntactic-keywords) t)
1442 (setq font-lock-syntactic-keywords (font-lock-compile-keywords
1443 font-lock-syntactic-keywords)))
1444 ;; Get down to business.
1445 (let ((case-fold-search font-lock-keywords-case-fold-search)
1446 (keywords (cdr font-lock-syntactic-keywords))
1447 keyword matcher highlights)
1448 (while keywords
1449 ;; Find an occurrence of `matcher' from `start' to `end'.
1450 (setq keyword (car keywords) matcher (car keyword))
1451 (goto-char start)
1452 (while (if (stringp matcher)
1453 (re-search-forward matcher end t)
1454 (funcall matcher end))
1455 ;; Apply each highlight to this instance of `matcher', which may be
1456 ;; specific highlights or more keywords anchored to `matcher'.
1457 (setq highlights (cdr keyword))
1458 (while highlights
1459 (if (numberp (car (car highlights)))
1460 (font-lock-apply-syntactic-highlight (car highlights))
1461 (font-lock-fontify-syntactic-anchored-keywords (car highlights)
1462 end))
1463 (setq highlights (cdr highlights))))
1464 (setq keywords (cdr keywords)))))
1465
1466;;; End of Syntactic regexp fontification functions.
1467\f
1468;;; Syntactic fontification functions.
1469
1470;; These record the parse state at a particular position, always the start of a
1471;; line. Used to make `font-lock-fontify-syntactically-region' faster.
1472;; Previously, `font-lock-cache-position' was just a buffer position. However,
1473;; under certain situations, this occasionally resulted in mis-fontification.
1474;; I think the "situations" were deletion with Lazy Lock mode's deferral. sm.
1475(defvar font-lock-cache-state nil)
1476(defvar font-lock-cache-position nil)
1477
1478(defun font-lock-fontify-syntactically-region (start end &optional loudly)
1479 "Put proper face on each string and comment between START and END.
1480START should be at the beginning of a line."
1481 (let ((cache (marker-position font-lock-cache-position))
1c172af4 1482 state face beg)
ac6e572c
SM
1483 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
1484 (goto-char start)
1485 ;;
1486 ;; Find the state at the `beginning-of-line' before `start'.
1487 (if (eq start cache)
1488 ;; Use the cache for the state of `start'.
1489 (setq state font-lock-cache-state)
1490 ;; Find the state of `start'.
1491 (if (null font-lock-beginning-of-syntax-function)
1492 ;; Use the state at the previous cache position, if any, or
1493 ;; otherwise calculate from `point-min'.
1494 (if (or (null cache) (< start cache))
1495 (setq state (parse-partial-sexp (point-min) start))
1496 (setq state (parse-partial-sexp cache start nil nil
1497 font-lock-cache-state)))
1498 ;; Call the function to move outside any syntactic block.
1499 (funcall font-lock-beginning-of-syntax-function)
1500 (setq state (parse-partial-sexp (point) start)))
1501 ;; Cache the state and position of `start'.
1502 (setq font-lock-cache-state state)
1503 (set-marker font-lock-cache-position start))
1504 ;;
1505 ;; If the region starts inside a string or comment, show the extent of it.
1506 (when (or (nth 3 state) (nth 4 state))
1c172af4 1507 (setq face (funcall font-lock-syntactic-face-function state) beg (point))
ac6e572c 1508 (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
1c172af4 1509 (put-text-property beg (point) 'face face))
ac6e572c
SM
1510 ;;
1511 ;; Find each interesting place between here and `end'.
1512 (while (and (< (point) end)
1513 (progn
1514 (setq state (parse-partial-sexp (point) end nil nil state
1515 'syntax-table))
1516 (or (nth 3 state) (nth 4 state))))
1c172af4
SM
1517 (setq face (funcall font-lock-syntactic-face-function state)
1518 beg (nth 8 state))
ac6e572c 1519 (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
1c172af4 1520 (put-text-property beg (point) 'face face))))
ac6e572c
SM
1521
1522;;; End of Syntactic fontification functions.
1523\f
1524;;; Keyword regexp fontification functions.
9bfbb130
SM
1525
1526(defsubst font-lock-apply-highlight (highlight)
1527 "Apply HIGHLIGHT following a match.
1528HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
1529 (let* ((match (nth 0 highlight))
1530 (start (match-beginning match)) (end (match-end match))
1531 (override (nth 2 highlight)))
1532 (cond ((not start)
1533 ;; No match but we might not signal an error.
1534 (or (nth 3 highlight)
1535 (error "No match %d in highlight %S" match highlight)))
1536 ((not override)
1537 ;; Cannot override existing fontification.
1538 (or (text-property-not-all start end 'face nil)
1539 (put-text-property start end 'face (eval (nth 1 highlight)))))
1540 ((eq override t)
1541 ;; Override existing fontification.
1542 (put-text-property start end 'face (eval (nth 1 highlight))))
9bfbb130
SM
1543 ((eq override 'prepend)
1544 ;; Prepend to existing fontification.
1c626aaf 1545 (font-lock-prepend-text-property start end 'face (eval (nth 1 highlight))))
9bfbb130
SM
1546 ((eq override 'append)
1547 ;; Append to existing fontification.
1c626aaf 1548 (font-lock-append-text-property start end 'face (eval (nth 1 highlight))))
c1f2ffc8
SM
1549 ((eq override 'keep)
1550 ;; Keep existing fontification.
1c626aaf 1551 (font-lock-fillin-text-property start end 'face (eval (nth 1 highlight)))))))
9bfbb130
SM
1552
1553(defsubst font-lock-fontify-anchored-keywords (keywords limit)
1554 "Fontify according to KEYWORDS until LIMIT.
56fcbd7e
SM
1555KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1556LIMIT can be modified by the value of its PRE-MATCH-FORM."
1557 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
fb0e4f8a 1558 (lead-start (match-beginning 0))
56fcbd7e
SM
1559 ;; Evaluate PRE-MATCH-FORM.
1560 (pre-match-value (eval (nth 1 keywords))))
1561 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
fb0e4f8a 1562 (if (not (and (numberp pre-match-value) (> pre-match-value (point))))
6e1d0d15 1563 (setq limit (line-end-position))
fb0e4f8a 1564 (setq limit pre-match-value)
ba22aeff 1565 (when (and font-lock-multiline (>= limit (line-beginning-position 2)))
fb0e4f8a 1566 ;; this is a multiline anchored match
ba22aeff
SM
1567 ;; (setq font-lock-multiline t)
1568 (put-text-property (if (= limit (line-beginning-position 2))
1569 (1- limit)
1570 (min lead-start (point)))
1571 limit
28a53bc1 1572 'font-lock-multiline t)))
9bfbb130 1573 (save-match-data
876f2438 1574 ;; Find an occurrence of `matcher' before `limit'.
afa18a4e
SM
1575 (while (and (< (point) limit)
1576 (if (stringp matcher)
1577 (re-search-forward matcher limit t)
1578 (funcall matcher limit)))
876f2438 1579 ;; Apply each highlight to this instance of `matcher'.
9bfbb130
SM
1580 (setq highlights lowdarks)
1581 (while highlights
1582 (font-lock-apply-highlight (car highlights))
1583 (setq highlights (cdr highlights)))))
876f2438 1584 ;; Evaluate POST-MATCH-FORM.
9bfbb130
SM
1585 (eval (nth 2 keywords))))
1586
1587(defun font-lock-fontify-keywords-region (start end &optional loudly)
1588 "Fontify according to `font-lock-keywords' between START and END.
1589START should be at the beginning of a line."
ac6e572c 1590 (unless (eq (car font-lock-keywords) t)
55015061 1591 (setq font-lock-keywords (font-lock-compile-keywords font-lock-keywords)))
9bfbb130 1592 (let ((case-fold-search font-lock-keywords-case-fold-search)
55015061 1593 (keywords (cdr font-lock-keywords))
9bfbb130 1594 (bufname (buffer-name)) (count 0)
876f2438
SM
1595 keyword matcher highlights)
1596 ;;
1597 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1598 (while keywords
1599 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
2e6a15fc 1600 (make-string (incf count) ?.)))
9bfbb130 1601 ;;
876f2438
SM
1602 ;; Find an occurrence of `matcher' from `start' to `end'.
1603 (setq keyword (car keywords) matcher (car keyword))
1604 (goto-char start)
e7f07c2c
GM
1605 (while (and (< (point) end)
1606 (if (stringp matcher)
1607 (re-search-forward matcher end t)
1608 (funcall matcher end)))
041470d2
SM
1609 (when (and font-lock-multiline
1610 (match-beginning 0)
ba22aeff
SM
1611 (>= (point)
1612 (save-excursion (goto-char (match-beginning 0))
1613 (forward-line 1) (point))))
fb0e4f8a 1614 ;; this is a multiline regexp match
ba22aeff
SM
1615 ;; (setq font-lock-multiline t)
1616 (put-text-property (if (= (point)
1617 (save-excursion
1618 (goto-char (match-beginning 0))
1619 (forward-line 1) (point)))
1620 (1- (point))
1621 (match-beginning 0))
1622 (point)
fb0e4f8a 1623 'font-lock-multiline t))
876f2438
SM
1624 ;; Apply each highlight to this instance of `matcher', which may be
1625 ;; specific highlights or more keywords anchored to `matcher'.
1626 (setq highlights (cdr keyword))
1627 (while highlights
1628 (if (numberp (car (car highlights)))
1629 (font-lock-apply-highlight (car highlights))
1630 (font-lock-fontify-anchored-keywords (car highlights) end))
1631 (setq highlights (cdr highlights))))
1632 (setq keywords (cdr keywords)))))
2d63aa67 1633
ac6e572c 1634;;; End of Keyword regexp fontification functions.
9bfbb130
SM
1635\f
1636;; Various functions.
1637
55015061 1638(defun font-lock-compile-keywords (keywords)
019f00d8
DL
1639 "Compile KEYWORDS into the form (t KEYWORD ...).
1640Here KEYWORD is of the form (MATCHER HIGHLIGHT ...) as shown in the
1641`font-lock-keywords' doc string."
55015061
SM
1642 (if (eq (car-safe keywords) t)
1643 keywords
1644 (cons t (mapcar 'font-lock-compile-keyword keywords))))
a2b8e66b
SM
1645
1646(defun font-lock-compile-keyword (keyword)
c1f2ffc8 1647 (cond ((nlistp keyword) ; MATCHER
a2b8e66b 1648 (list keyword '(0 font-lock-keyword-face)))
c1f2ffc8 1649 ((eq (car keyword) 'eval) ; (eval . FORM)
a2b8e66b 1650 (font-lock-compile-keyword (eval (cdr keyword))))
c1f2ffc8
SM
1651 ((eq (car-safe (cdr keyword)) 'quote) ; (MATCHER . 'FORM)
1652 ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote.
1653 (if (symbolp (nth 2 keyword))
1654 (list (car keyword) (list 0 (cdr keyword)))
1655 (font-lock-compile-keyword (cons (car keyword) (nth 2 keyword)))))
1656 ((numberp (cdr keyword)) ; (MATCHER . MATCH)
a2b8e66b 1657 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
c1f2ffc8 1658 ((symbolp (cdr keyword)) ; (MATCHER . FACENAME)
a2b8e66b 1659 (list (car keyword) (list 0 (cdr keyword))))
c1f2ffc8 1660 ((nlistp (nth 1 keyword)) ; (MATCHER . HIGHLIGHT)
a2b8e66b 1661 (list (car keyword) (cdr keyword)))
c1f2ffc8 1662 (t ; (MATCHER HIGHLIGHT ...)
a2b8e66b 1663 keyword)))
d46c21ec 1664
ac6e572c 1665(defun font-lock-eval-keywords (keywords)
019f00d8 1666 "Evalulate KEYWORDS if a function (funcall) or variable (eval) name."
46f26fcf
SM
1667 (if (listp keywords)
1668 keywords
1669 (font-lock-eval-keywords (if (fboundp keywords)
1670 (funcall keywords)
1671 (eval keywords)))))
ac6e572c 1672
343204bd 1673(defun font-lock-value-in-major-mode (alist)
019f00d8
DL
1674 "Return value in ALIST for `major-mode', or ALIST if it is not an alist.
1675Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t."
343204bd
SM
1676 (if (consp alist)
1677 (cdr (or (assq major-mode alist) (assq t alist)))
1678 alist))
1679
d46c21ec 1680(defun font-lock-choose-keywords (keywords level)
019f00d8
DL
1681 "Return LEVELth element of KEYWORDS.
1682A LEVEL of nil is equal to a LEVEL of 0, a LEVEL of t is equal to
1683\(1- (length KEYWORDS))."
fd612dd9 1684 (cond ((not (and (listp keywords) (symbolp (car keywords))))
343204bd
SM
1685 keywords)
1686 ((numberp level)
1687 (or (nth level keywords) (car (reverse keywords))))
1688 ((eq level t)
1689 (car (reverse keywords)))
1690 (t
1691 (car keywords))))
d46c21ec 1692
2e6a15fc
SM
1693(defvar font-lock-set-defaults nil) ; Whether we have set up defaults.
1694
d46c21ec
SM
1695(defun font-lock-set-defaults ()
1696 "Set fontification defaults appropriately for this mode.
a2b8e66b
SM
1697Sets various variables using `font-lock-defaults' (or, if nil, using
1698`font-lock-defaults-alist') and `font-lock-maximum-decoration'."
6e1d0d15 1699 ;; Set fontification defaults iff not previously set.
2e6a15fc
SM
1700 (unless font-lock-set-defaults
1701 (set (make-local-variable 'font-lock-set-defaults) t)
1702 (set (make-local-variable 'font-lock-cache-state) nil)
1703 (set (make-local-variable 'font-lock-cache-position) (make-marker))
6e1d0d15
SM
1704 (make-local-variable 'font-lock-fontified)
1705 (make-local-variable 'font-lock-multiline)
2e6a15fc
SM
1706 (let* ((defaults (or font-lock-defaults
1707 (cdr (assq major-mode font-lock-defaults-alist))))
1708 (keywords
1709 (font-lock-choose-keywords (nth 0 defaults)
c1f2ffc8 1710 (font-lock-value-in-major-mode font-lock-maximum-decoration)))
76f5e2af
GM
1711 (local (cdr (assq major-mode font-lock-keywords-alist)))
1712 (removed-keywords
1713 (cdr-safe (assq major-mode font-lock-removed-keywords-alist))))
2e6a15fc 1714 ;; Regexp fontification?
2d63aa67 1715 (set (make-local-variable 'font-lock-keywords)
ac6e572c 1716 (font-lock-compile-keywords (font-lock-eval-keywords keywords)))
c1f2ffc8
SM
1717 ;; Local fontification?
1718 (while local
1719 (font-lock-add-keywords nil (car (car local)) (cdr (car local)))
1720 (setq local (cdr local)))
76f5e2af
GM
1721 (when removed-keywords
1722 (font-lock-remove-keywords nil removed-keywords))
2e6a15fc
SM
1723 ;; Syntactic fontification?
1724 (when (nth 1 defaults)
1725 (set (make-local-variable 'font-lock-keywords-only) t))
1726 ;; Case fold during regexp fontification?
1727 (when (nth 2 defaults)
1728 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
1729 ;; Syntax table for regexp and syntactic fontification?
1730 (when (nth 3 defaults)
1731 (let ((slist (nth 3 defaults)))
1732 (set (make-local-variable 'font-lock-syntax-table)
1733 (copy-syntax-table (syntax-table)))
1734 (while slist
1735 ;; The character to modify may be a single CHAR or a STRING.
1736 (let ((chars (if (numberp (car (car slist)))
1737 (list (car (car slist)))
1738 (mapcar 'identity (car (car slist)))))
1739 (syntax (cdr (car slist))))
1740 (while chars
68c67d1f 1741 (modify-syntax-entry (car chars) syntax font-lock-syntax-table)
2e6a15fc
SM
1742 (setq chars (cdr chars)))
1743 (setq slist (cdr slist))))))
1744 ;; Syntax function for syntactic fontification?
1745 (when (nth 4 defaults)
1746 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1747 (nth 4 defaults)))
1748 ;; Variable alist?
724a330f
SM
1749 (dolist (x (nthcdr 5 defaults))
1750 (let ((variable (car x)) (value (cdr x)))
1751 (unless (boundp variable)
1752 (set variable nil)) ;why ?
1753 (set (make-local-variable variable) value))))))
a2b8e66b
SM
1754
1755(defun font-lock-unset-defaults ()
019f00d8 1756 "Unset fontification defaults. See function `font-lock-set-defaults'."
2e6a15fc
SM
1757 (setq font-lock-set-defaults nil
1758 font-lock-keywords nil
a2b8e66b
SM
1759 font-lock-keywords-only nil
1760 font-lock-keywords-case-fold-search nil
1761 font-lock-syntax-table nil
a078558d
SM
1762 font-lock-beginning-of-syntax-function nil)
1763 (let* ((defaults (or font-lock-defaults
1764 (cdr (assq major-mode font-lock-defaults-alist))))
1765 (alist (nthcdr 5 defaults)))
1766 (while alist
1767 (set (car (car alist)) (default-value (car (car alist))))
1768 (setq alist (cdr alist)))))
030f4a35 1769\f
2d63aa67 1770;;; Colour etc. support.
9bfbb130 1771
2273c36b 1772;; Originally face attributes were specified via `font-lock-face-attributes'.
23a0e11b 1773;; Users then changed the default face attributes by setting that variable.
2273c36b
SM
1774;; However, we try and be back-compatible and respect its value if set except
1775;; for faces where M-x customize has been used to save changes for the face.
1776(when (boundp 'font-lock-face-attributes)
1777 (let ((face-attributes font-lock-face-attributes))
1778 (while face-attributes
1779 (let* ((face-attribute (pop face-attributes))
1780 (face (car face-attribute)))
1781 ;; Rustle up a `defface' SPEC from a `font-lock-face-attributes' entry.
1782 (unless (get face 'saved-face)
1783 (let ((foreground (nth 1 face-attribute))
1784 (background (nth 2 face-attribute))
1785 (bold-p (nth 3 face-attribute))
1786 (italic-p (nth 4 face-attribute))
1787 (underline-p (nth 5 face-attribute))
1788 face-spec)
1789 (when foreground
1790 (setq face-spec (cons ':foreground (cons foreground face-spec))))
1791 (when background
1792 (setq face-spec (cons ':background (cons background face-spec))))
1793 (when bold-p
1794 (setq face-spec (append '(:bold t) face-spec)))
1795 (when italic-p
1796 (setq face-spec (append '(:italic t) face-spec)))
1797 (when underline-p
1798 (setq face-spec (append '(:underline t) face-spec)))
1799 (custom-declare-face face (list (list t face-spec)) nil)))))))
1800
1801;; But now we do it the custom way. Note that `defface' will not overwrite any
1802;; faces declared above via `custom-declare-face'.
55015061 1803(defface font-lock-comment-face
8379c868
EZ
1804 '((((type tty pc) (class color) (background light)) (:foreground "red"))
1805 (((type tty pc) (class color) (background dark)) (:foreground "lightred"))
e7f07c2c 1806 (((class grayscale) (background light))
55015061
SM
1807 (:foreground "DimGray" :bold t :italic t))
1808 (((class grayscale) (background dark))
1809 (:foreground "LightGray" :bold t :italic t))
1810 (((class color) (background light)) (:foreground "Firebrick"))
86b7fcbb 1811 (((class color) (background dark)) (:foreground "chocolate1"))
55015061
SM
1812 (t (:bold t :italic t)))
1813 "Font Lock mode face used to highlight comments."
2273c36b 1814 :group 'font-lock-highlighting-faces)
55015061
SM
1815
1816(defface font-lock-string-face
e7f07c2c
GM
1817 '((((type tty) (class color)) (:foreground "green"))
1818 (((class grayscale) (background light)) (:foreground "DimGray" :italic t))
55015061
SM
1819 (((class grayscale) (background dark)) (:foreground "LightGray" :italic t))
1820 (((class color) (background light)) (:foreground "RosyBrown"))
1821 (((class color) (background dark)) (:foreground "LightSalmon"))
1822 (t (:italic t)))
1823 "Font Lock mode face used to highlight strings."
2273c36b 1824 :group 'font-lock-highlighting-faces)
55015061 1825
1c172af4
SM
1826(defface font-lock-doc-face
1827 '((t :inherit font-lock-string-face))
1828 "Font Lock mode face used to highlight documentation."
1829 :group 'font-lock-highlighting-faces)
1830
55015061 1831(defface font-lock-keyword-face
e7f07c2c
GM
1832 '((((type tty) (class color)) (:foreground "cyan" :weight bold))
1833 (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
55015061
SM
1834 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1835 (((class color) (background light)) (:foreground "Purple"))
1836 (((class color) (background dark)) (:foreground "Cyan"))
1837 (t (:bold t)))
1838 "Font Lock mode face used to highlight keywords."
2273c36b 1839 :group 'font-lock-highlighting-faces)
55015061
SM
1840
1841(defface font-lock-builtin-face
e7f07c2c
GM
1842 '((((type tty) (class color)) (:foreground "blue" :weight light))
1843 (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
55015061
SM
1844 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1845 (((class color) (background light)) (:foreground "Orchid"))
1846 (((class color) (background dark)) (:foreground "LightSteelBlue"))
1847 (t (:bold t)))
1848 "Font Lock mode face used to highlight builtins."
2273c36b 1849 :group 'font-lock-highlighting-faces)
55015061
SM
1850
1851(defface font-lock-function-name-face
e7f07c2c
GM
1852 '((((type tty) (class color)) (:foreground "blue" :weight bold))
1853 (((class color) (background light)) (:foreground "Blue"))
55015061 1854 (((class color) (background dark)) (:foreground "LightSkyBlue"))
ac6e572c 1855 (t (:inverse-video t :bold t)))
55015061 1856 "Font Lock mode face used to highlight function names."
2273c36b 1857 :group 'font-lock-highlighting-faces)
55015061
SM
1858
1859(defface font-lock-variable-name-face
e7f07c2c
GM
1860 '((((type tty) (class color)) (:foreground "yellow" :weight light))
1861 (((class grayscale) (background light))
55015061
SM
1862 (:foreground "Gray90" :bold t :italic t))
1863 (((class grayscale) (background dark))
1864 (:foreground "DimGray" :bold t :italic t))
1865 (((class color) (background light)) (:foreground "DarkGoldenrod"))
1866 (((class color) (background dark)) (:foreground "LightGoldenrod"))
1867 (t (:bold t :italic t)))
1868 "Font Lock mode face used to highlight variable names."
2273c36b 1869 :group 'font-lock-highlighting-faces)
55015061
SM
1870
1871(defface font-lock-type-face
e7f07c2c
GM
1872 '((((type tty) (class color)) (:foreground "green"))
1873 (((class grayscale) (background light)) (:foreground "Gray90" :bold t))
55015061 1874 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
46f26fcf 1875 (((class color) (background light)) (:foreground "ForestGreen"))
55015061
SM
1876 (((class color) (background dark)) (:foreground "PaleGreen"))
1877 (t (:bold t :underline t)))
8acf2292 1878 "Font Lock mode face used to highlight type and classes."
2273c36b 1879 :group 'font-lock-highlighting-faces)
55015061 1880
8acf2292 1881(defface font-lock-constant-face
e7f07c2c
GM
1882 '((((type tty) (class color)) (:foreground "magenta"))
1883 (((class grayscale) (background light))
55015061
SM
1884 (:foreground "LightGray" :bold t :underline t))
1885 (((class grayscale) (background dark))
1886 (:foreground "Gray50" :bold t :underline t))
1887 (((class color) (background light)) (:foreground "CadetBlue"))
1888 (((class color) (background dark)) (:foreground "Aquamarine"))
1889 (t (:bold t :underline t)))
8acf2292 1890 "Font Lock mode face used to highlight constants and labels."
2273c36b 1891 :group 'font-lock-highlighting-faces)
55015061
SM
1892
1893(defface font-lock-warning-face
e7f07c2c
GM
1894 '((((type tty) (class color)) (:foreground "red"))
1895 (((class color) (background light)) (:foreground "Red" :bold t))
55015061 1896 (((class color) (background dark)) (:foreground "Pink" :bold t))
ac6e572c 1897 (t (:inverse-video t :bold t)))
55015061 1898 "Font Lock mode face used to highlight warnings."
2273c36b 1899 :group 'font-lock-highlighting-faces)
2d63aa67
SM
1900
1901;;; End of Colour etc. support.
9bfbb130 1902\f
56fcbd7e
SM
1903;;; Menu support.
1904
1905;; This section of code is commented out because Emacs does not have real menu
1906;; buttons. (We can mimic them by putting "( ) " or "(X) " at the beginning of
1907;; the menu entry text, but with Xt it looks both ugly and embarrassingly
1908;; amateur.) If/When Emacs gets real menus buttons, put in menu-bar.el after
1909;; the entry for "Text Properties" something like:
1910;;
1911;; (define-key menu-bar-edit-menu [font-lock]
9c8de95c 1912;; (cons "Syntax Highlighting" font-lock-menu))
56fcbd7e
SM
1913;;
1914;; and remove a single ";" from the beginning of each line in the rest of this
1915;; section. Probably the mechanism for telling the menu code what are menu
1916;; buttons and when they are on or off needs tweaking. I have assumed that the
1917;; mechanism is via `menu-toggle' and `menu-selected' symbol properties. sm.
1918
1919;;;;###autoload
1920;(progn
1921; ;; Make the Font Lock menu.
1922; (defvar font-lock-menu (make-sparse-keymap "Syntax Highlighting"))
1923; ;; Add the menu items in reverse order.
1924; (define-key font-lock-menu [fontify-less]
1925; '("Less In Current Buffer" . font-lock-fontify-less))
1926; (define-key font-lock-menu [fontify-more]
1927; '("More In Current Buffer" . font-lock-fontify-more))
1928; (define-key font-lock-menu [font-lock-sep]
1929; '("--"))
1930; (define-key font-lock-menu [font-lock-mode]
1931; '("In Current Buffer" . font-lock-mode))
1932; (define-key font-lock-menu [global-font-lock-mode]
1933; '("In All Buffers" . global-font-lock-mode)))
1934;
1935;;;;###autoload
1936;(progn
1937; ;; We put the appropriate `menu-enable' etc. symbol property values on when
1938; ;; font-lock.el is loaded, so we don't need to autoload the three variables.
1939; (put 'global-font-lock-mode 'menu-toggle t)
1940; (put 'font-lock-mode 'menu-toggle t)
1941; (put 'font-lock-fontify-more 'menu-enable '(identity))
1942; (put 'font-lock-fontify-less 'menu-enable '(identity)))
1943;
1944;;; Put the appropriate symbol property values on now. See above.
9c8de95c 1945;(put 'global-font-lock-mode 'menu-selected 'global-font-lock-mode)
56fcbd7e
SM
1946;(put 'font-lock-mode 'menu-selected 'font-lock-mode)
1947;(put 'font-lock-fontify-more 'menu-enable '(nth 2 font-lock-fontify-level))
1948;(put 'font-lock-fontify-less 'menu-enable '(nth 1 font-lock-fontify-level))
1949;
1950;(defvar font-lock-fontify-level nil) ; For less/more fontification.
1951;
1952;(defun font-lock-fontify-level (level)
1953; (let ((font-lock-maximum-decoration level))
1954; (when font-lock-mode
1955; (font-lock-mode))
1956; (font-lock-mode)
1957; (when font-lock-verbose
1958; (message "Fontifying %s... level %d" (buffer-name) level))))
1959;
1960;(defun font-lock-fontify-less ()
1961; "Fontify the current buffer with less decoration.
1962;See `font-lock-maximum-decoration'."
1963; (interactive)
1964; ;; Check in case we get called interactively.
1965; (if (nth 1 font-lock-fontify-level)
1966; (font-lock-fontify-level (1- (car font-lock-fontify-level)))
1967; (error "No less decoration")))
1968;
1969;(defun font-lock-fontify-more ()
1970; "Fontify the current buffer with more decoration.
1971;See `font-lock-maximum-decoration'."
1972; (interactive)
1973; ;; Check in case we get called interactively.
1974; (if (nth 2 font-lock-fontify-level)
1975; (font-lock-fontify-level (1+ (car font-lock-fontify-level)))
1976; (error "No more decoration")))
1977;
1978;;; This should be called by `font-lock-set-defaults'.
1979;(defun font-lock-set-menu ()
1980; ;; Activate less/more fontification entries if there are multiple levels for
1981; ;; the current buffer. Sets `font-lock-fontify-level' to be of the form
1982; ;; (CURRENT-LEVEL IS-LOWER-LEVEL-P IS-HIGHER-LEVEL-P) for menu activation.
1983; (let ((keywords (or (nth 0 font-lock-defaults)
1984; (nth 1 (assq major-mode font-lock-defaults-alist))))
1985; (level (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1986; (make-local-variable 'font-lock-fontify-level)
1987; (if (or (symbolp keywords) (= (length keywords) 1))
1988; (font-lock-unset-menu)
1989; (cond ((eq level t)
1990; (setq level (1- (length keywords))))
1991; ((or (null level) (zerop level))
1992; ;; The default level is usually, but not necessarily, level 1.
1993; (setq level (- (length keywords)
1994; (length (member (eval (car keywords))
1995; (mapcar 'eval (cdr keywords))))))))
1996; (setq font-lock-fontify-level (list level (> level 1)
1997; (< level (1- (length keywords))))))))
1998;
1999;;; This should be called by `font-lock-unset-defaults'.
2000;(defun font-lock-unset-menu ()
2001; ;; Deactivate less/more fontification entries.
2002; (setq font-lock-fontify-level nil))
2d63aa67
SM
2003
2004;;; End of Menu support.
56fcbd7e 2005\f
9bfbb130 2006;;; Various regexp information shared by several modes.
a1eb1cf1 2007;;; Information specific to a single mode should go in its load library.
030f4a35 2008
1c626aaf 2009;; Font Lock support for C, C++, Objective-C and Java modes will one day be in
56fcbd7e
SM
2010;; some cc-font.el (and required by cc-mode.el). However, the below function
2011;; should stay in font-lock.el, since it is used by other libraries. sm.
2e6a15fc 2012
c1f2ffc8 2013(defun font-lock-match-c-style-declaration-item-and-skip-to-next (limit)
2e6a15fc 2014 "Match, and move over, any declaration/definition item after point.
c1f2ffc8
SM
2015Matches after point, but ignores leading whitespace and `*' characters.
2016Does not move further than LIMIT.
2017
ac6e572c
SM
2018The expected syntax of a declaration/definition item is `word' (preceded by
2019optional whitespace and `*' characters and proceeded by optional whitespace)
2020optionally followed by a `('. Everything following the item (but belonging to
2021it) is expected to by skip-able by `scan-sexps', and items are expected to be
2022separated with a `,' and to be terminated with a `;'.
c1f2ffc8
SM
2023
2024Thus the regexp matches after point: word (
2025 ^^^^ ^
2026Where the match subexpressions are: 1 2
2027
2028The item is delimited by (match-beginning 1) and (match-end 1).
2029If (match-beginning 2) is non-nil, the item is followed by a `('.
2030
2031This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
2032 (when (looking-at "[ \t*]*\\(\\sw+\\)[ \t]*\\((\\)?")
2e6a15fc
SM
2033 (save-match-data
2034 (condition-case nil
2035 (save-restriction
2036 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
2037 (narrow-to-region (point-min) limit)
2038 (goto-char (match-end 1))
2039 ;; Move over any item value, etc., to the next item.
2040 (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)"))
2041 (goto-char (or (scan-sexps (point) 1) (point-max))))
2042 (goto-char (match-end 2)))
2043 (error t)))))
46f26fcf
SM
2044\f
2045;; Lisp.
2e6a15fc 2046
030f4a35 2047(defconst lisp-font-lock-keywords-1
2e6a15fc
SM
2048 (eval-when-compile
2049 (list
2d63aa67 2050 ;;
55015061
SM
2051 ;; Definitions.
2052 (list (concat "(\\(def\\("
2053 ;; Function declarations.
9c8de95c 2054 "\\(advice\\|alias\\|generic\\|macro\\*?\\|method\\|"
4fffc071 2055 "setf\\|subst\\*?\\|un\\*?\\|"
1a5c5f2e 2056 "ine-\\(condition\\|\\(?:derived\\|minor\\)-mode\\|"
4fffc071 2057 "method-combination\\|setf-expander\\|skeleton\\|widget\\|"
1a5c5f2e 2058 "function\\|\\(compiler\\|modify\\|symbol\\)-macro\\)\\)\\|"
2e6a15fc 2059 ;; Variable declarations.
4fffc071 2060 "\\(const\\(ant\\)?\\|custom\\|face\\|parameter\\|var\\)\\|"
2e6a15fc 2061 ;; Structure declarations.
5bbc1cf7 2062 "\\(class\\|group\\|package\\|struct\\|type\\)"
2e6a15fc 2063 "\\)\\)\\>"
55015061 2064 ;; Any whitespace and defined object.
2e6a15fc 2065 "[ \t'\(]*"
11b42ef4 2066 "\\(setf[ \t]+\\sw+)\\|\\sw+\\)?")
2e6a15fc 2067 '(1 font-lock-keyword-face)
ae659ad5 2068 '(9 (cond ((match-beginning 3) font-lock-function-name-face)
aaa15488 2069 ((match-beginning 6) font-lock-variable-name-face)
55015061 2070 (t font-lock-type-face))
2e6a15fc 2071 nil t))
2d63aa67
SM
2072 ;;
2073 ;; Emacs Lisp autoload cookies.
731dd885 2074 '("^;;;###\\(autoload\\)" 1 font-lock-warning-face prepend)
2e6a15fc 2075 ))
56fcbd7e 2076 "Subdued level highlighting for Lisp modes.")
030f4a35
RS
2077
2078(defconst lisp-font-lock-keywords-2
799761f0 2079 (append lisp-font-lock-keywords-1
2e6a15fc
SM
2080 (eval-when-compile
2081 (list
2082 ;;
2d63aa67 2083 ;; Control structures. Emacs Lisp forms.
ac6e572c
SM
2084 (cons (concat
2085 "(" (regexp-opt
4fffc071 2086 '("cond" "if" "while" "let" "let*"
ac6e572c 2087 "prog" "progn" "progv" "prog1" "prog2" "prog*"
4fffc071 2088 "inline" "lambda" "save-restriction" "save-excursion"
ac6e572c
SM
2089 "save-window-excursion" "save-selected-window"
2090 "save-match-data" "save-current-buffer" "unwind-protect"
9c8de95c 2091 "condition-case" "track-mouse"
ac6e572c 2092 "eval-after-load" "eval-and-compile" "eval-when-compile"
4fffc071 2093 "eval-when"
844a6a46
SM
2094 "with-current-buffer" "with-electric-help"
2095 "with-output-to-string" "with-output-to-temp-buffer"
79f238c9 2096 "with-temp-buffer" "with-temp-file" "with-temp-message"
844a6a46 2097 "with-timeout") t)
ac6e572c 2098 "\\>")
2e6a15fc
SM
2099 1)
2100 ;;
2d63aa67 2101 ;; Control structures. Common Lisp forms.
ac6e572c
SM
2102 (cons (concat
2103 "(" (regexp-opt
2104 '("when" "unless" "case" "ecase" "typecase" "etypecase"
aaa15488 2105 "ccase" "ctypecase" "handler-case" "handler-bind"
ae659ad5 2106 "restart-bind" "restart-case" "in-package"
4fffc071 2107 "cerror" "break" "ignore-errors"
ae659ad5 2108 "loop" "do" "do*" "dotimes" "dolist" "the" "locally"
aaa15488
RS
2109 "proclaim" "declaim" "declare" "symbol-macrolet"
2110 "lexical-let" "lexical-let*" "flet" "labels" "compiler-let"
2111 "destructuring-bind" "macrolet" "tagbody" "block"
ac6e572c
SM
2112 "return" "return-from") t)
2113 "\\>")
2d63aa67
SM
2114 1)
2115 ;;
4fffc071
SM
2116 ;; Exit/Feature symbols as constants.
2117 (list (concat "(\\(catch\\|throw\\|featurep\\|provide\\|require\\)\\>"
2118 "[ \t']*\\(\\sw+\\)?")
2119 '(1 font-lock-keyword-face)
2120 '(2 font-lock-constant-face nil t))
2121 ;;
2122 ;; Erroneous structures.
2123 '("(\\(abort\\|assert\\|error\\|signal\\)\\>" 1 font-lock-warning-face)
2e6a15fc
SM
2124 ;;
2125 ;; Words inside \\[] tend to be for `substitute-command-keys'.
8acf2292 2126 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-constant-face prepend)
2e6a15fc
SM
2127 ;;
2128 ;; Words inside `' tend to be symbol names.
8acf2292 2129 '("`\\(\\sw\\sw+\\)'" 1 font-lock-constant-face prepend)
2e6a15fc 2130 ;;
9c8de95c 2131 ;; Constant values.
c1f2ffc8 2132 '("\\<:\\sw\\sw+\\>" 0 font-lock-builtin-face)
2e6a15fc
SM
2133 ;;
2134 ;; ELisp and CLisp `&' keywords as types.
f56f1421 2135 '("\\&\\sw+\\>" . font-lock-type-face)
5e9e032a
SS
2136 ;;
2137 ;; CL `with-' and `do-' constructs
2138 '("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
2e6a15fc 2139 )))
fb512de9 2140 "Gaudy level highlighting for Lisp modes.")
030f4a35 2141
fb512de9
SM
2142(defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
2143 "Default expressions to highlight in Lisp modes.")
46f26fcf 2144\f
2e6a15fc 2145;;; User choices.
030f4a35 2146
c1f2ffc8
SM
2147;; These provide a means to fontify types not defined by the language. Those
2148;; types might be the user's own or they might be generally accepted and used.
55015061 2149;; Generally accepted types are used to provide default variable values.
c1f2ffc8 2150
2273c36b
SM
2151(define-widget 'font-lock-extra-types-widget 'radio
2152 "Widget `:type' for members of the custom group `font-lock-extra-types'.
2153Members should `:load' the package `font-lock' to use this widget."
2154 :args '((const :tag "none" nil)
46f26fcf 2155 (repeat :tag "types" regexp)))
2273c36b 2156
31b482ee 2157(defcustom c-font-lock-extra-types '("FILE" "\\sw+_t" "Lisp_Object")
2e6a15fc 2158 "*List of extra types to fontify in C mode.
55015061 2159Each list item should be a regexp not containing word-delimiters.
c1f2ffc8 2160For example, a value of (\"FILE\" \"\\\\sw+_t\") means the word FILE and words
55015061
SM
2161ending in _t are treated as type names.
2162
2273c36b
SM
2163The value of this variable is used when Font Lock mode is turned on."
2164 :type 'font-lock-extra-types-widget
2165 :group 'font-lock-extra-types)
9bfbb130 2166
b9dd672d 2167(defcustom c++-font-lock-extra-types
4fffc071
SM
2168 '("\\sw+_t"
2169 "\\([iof]\\|str\\)+stream\\(buf\\)?" "ios"
b9dd672d
SM
2170 "string" "rope"
2171 "list" "slist"
2172 "deque" "vector" "bit_vector"
2173 "set" "multiset"
2174 "map" "multimap"
2175 "hash\\(_\\(m\\(ap\\|ulti\\(map\\|set\\)\\)\\|set\\)\\)?"
2176 "stack" "queue" "priority_queue"
9dc09247 2177 "type_info"
4fffc071
SM
2178 "iterator" "const_iterator" "reverse_iterator" "const_reverse_iterator"
2179 "reference" "const_reference")
2e6a15fc 2180 "*List of extra types to fontify in C++ mode.
55015061 2181Each list item should be a regexp not containing word-delimiters.
b9dd672d
SM
2182For example, a value of (\"string\") means the word string is treated as a type
2183name.
55015061 2184
2273c36b
SM
2185The value of this variable is used when Font Lock mode is turned on."
2186 :type 'font-lock-extra-types-widget
2187 :group 'font-lock-extra-types)
030f4a35 2188
2273c36b 2189(defcustom objc-font-lock-extra-types '("Class" "BOOL" "IMP" "SEL")
2e6a15fc 2190 "*List of extra types to fontify in Objective-C mode.
55015061 2191Each list item should be a regexp not containing word-delimiters.
c1f2ffc8 2192For example, a value of (\"Class\" \"BOOL\" \"IMP\" \"SEL\") means the words
55015061
SM
2193Class, BOOL, IMP and SEL are treated as type names.
2194
2273c36b
SM
2195The value of this variable is used when Font Lock mode is turned on."
2196 :type 'font-lock-extra-types-widget
2197 :group 'font-lock-extra-types)
1bd50840 2198
68c67d1f
SM
2199(defcustom java-font-lock-extra-types
2200 '("[A-Z\300-\326\330-\337]\\sw*[a-z]\\sw*")
2e6a15fc 2201 "*List of extra types to fontify in Java mode.
55015061 2202Each list item should be a regexp not containing word-delimiters.
68c67d1f 2203For example, a value of (\"[A-Z\300-\326\330-\337]\\\\sw*[a-z]\\\\sw*\") means capitalised
55015061
SM
2204words (and words conforming to the Java id spec) are treated as type names.
2205
2273c36b
SM
2206The value of this variable is used when Font Lock mode is turned on."
2207 :type 'font-lock-extra-types-widget
2208 :group 'font-lock-extra-types)
2e6a15fc
SM
2209\f
2210;;; C.
c1f2ffc8
SM
2211
2212;; [Murmur murmur murmur] Maestro, drum-roll please... [Murmur murmur murmur.]
2213;; Ahem. [Murmur murmur murmur] Lay-dees an Gennel-men. [Murmur murmur shhh!]
2214;; I am most proud and humbly honoured today [murmur murmur cough] to present
2215;; to you good people, the winner of the Second Millennium Award for The Most
2216;; Hairy Language Syntax. [Ahhh!] All rise please. [Shuffle shuffle
2217;; shuffle.] And a round of applause please. For... The C Language! [Roar.]
2218;;
2219;; Thank you... You are too kind... It is with a feeling of great privilege
2220;; and indeed emotion [sob] that I accept this award. It has been a long hard
2221;; road. But we know our destiny. And our future. For we must not rest.
2222;; There are more tokens to overload, more shoehorn, more methodologies. But
2223;; more is a plus! [Ha ha ha.] And more means plus! [Ho ho ho.] The future
1c626aaf 2224;; is C++! [Ohhh!] The Third Millennium Award... Will be ours! [Roar.]
c1f2ffc8 2225
2e6a15fc
SM
2226(defconst c-font-lock-keywords-1 nil
2227 "Subdued level highlighting for C mode.")
9bfbb130 2228
2e6a15fc
SM
2229(defconst c-font-lock-keywords-2 nil
2230 "Medium level highlighting for C mode.
2231See also `c-font-lock-extra-types'.")
1bd50840 2232
2e6a15fc
SM
2233(defconst c-font-lock-keywords-3 nil
2234 "Gaudy level highlighting for C mode.
2235See also `c-font-lock-extra-types'.")
9bfbb130 2236
2d63aa67 2237(let* ((c-keywords
ac6e572c
SM
2238 (eval-when-compile
2239 (regexp-opt '("break" "continue" "do" "else" "for" "if" "return"
4fffc071
SM
2240 "switch" "while" "sizeof"
2241 ;; Type related, but we don't do anything special.
2242 "typedef" "extern" "auto" "register" "static"
336911a1
SM
2243 "volatile" "const"
2244 ;; Dan Nicolaescu <done@gnu.org> says this is new.
fd612dd9 2245 "restrict"))))
4fffc071
SM
2246 (c-type-specs
2247 (eval-when-compile
fd612dd9 2248 (regexp-opt '("enum" "struct" "union"))))
4fffc071
SM
2249 (c-type-specs-depth
2250 (regexp-opt-depth c-type-specs))
2251 (c-type-names
2d63aa67 2252 `(mapconcat 'identity
019f00d8 2253 (cons
d2251bbf
SM
2254 ,(eval-when-compile
2255 (regexp-opt
2256 '("char" "short" "int" "long" "signed" "unsigned"
2257 "float" "double" "void" "complex")))
2d63aa67
SM
2258 c-font-lock-extra-types)
2259 "\\|"))
4fffc071 2260 (c-type-names-depth
d2251bbf 2261 `(regexp-opt-depth ,c-type-names))
76f5e2af
GM
2262 (c-preprocessor-directives
2263 (eval-when-compile
2264 (regexp-opt
2265 '("define" "elif" "else" "endif" "error" "file" "if" "ifdef"
2266 "ifndef" "include" "line" "pragma" "undef"))))
2267 (c-preprocessor-directives-depth
d2251bbf 2268 (regexp-opt-depth c-preprocessor-directives)))
f60f18ae
KH
2269 (setq c-font-lock-keywords-1
2270 (list
f60f18ae 2271 ;;
9bfbb130 2272 ;; These are all anchored at the beginning of line for speed.
2e6a15fc 2273 ;; Note that `c++-font-lock-keywords-1' depends on `c-font-lock-keywords-1'.
9bfbb130
SM
2274 ;;
2275 ;; Fontify function name definitions (GNU style; without type on line).
c1f2ffc8
SM
2276 '("^\\(\\sw+\\)[ \t]*(" 1 font-lock-function-name-face)
2277 ;;
2278 ;; Fontify error directives.
2279 '("^#[ \t]*error[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend)
9bfbb130
SM
2280 ;;
2281 ;; Fontify filenames in #include <...> preprocessor directives as strings.
9c8de95c 2282 '("^#[ \t]*\\(import\\|include\\)[ \t]*\\(<[^>\"\n]*>?\\)"
2e6a15fc 2283 2 font-lock-string-face)
f60f18ae 2284 ;;
a1eb1cf1 2285 ;; Fontify function macro names.
7d7d915a 2286 '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
f60f18ae 2287 ;;
5aa29679
RS
2288 ;; Fontify symbol names in #elif or #if ... defined preprocessor directives.
2289 '("^#[ \t]*\\(elif\\|if\\)\\>"
9bfbb130 2290 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
b9dd672d 2291 (1 font-lock-builtin-face) (2 font-lock-variable-name-face nil t)))
9bfbb130 2292 ;;
a1eb1cf1 2293 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
76f5e2af
GM
2294 (list
2295 (concat "^#[ \t]*\\(" c-preprocessor-directives
2296 "\\)\\>[ \t!]*\\(\\sw+\\)?")
2297 '(1 font-lock-builtin-face)
2298 (list (+ 2 c-preprocessor-directives-depth)
d2251bbf 2299 'font-lock-variable-name-face nil t))))
f60f18ae
KH
2300
2301 (setq c-font-lock-keywords-2
2302 (append c-font-lock-keywords-1
030f4a35 2303 (list
030f4a35 2304 ;;
9bfbb130 2305 ;; Simple regexps for speed.
b89e1134 2306 ;;
4fffc071 2307 ;; Fontify all type names.
c1f2ffc8 2308 `(eval .
d2251bbf 2309 (cons (concat "\\<\\(" ,c-type-names "\\)\\>") 'font-lock-type-face))
030f4a35 2310 ;;
b89e1134 2311 ;; Fontify all builtin keywords (except case, default and goto; see below).
4fffc071 2312 (concat "\\<\\(" c-keywords "\\|" c-type-specs "\\)\\>")
030f4a35 2313 ;;
9bfbb130 2314 ;; Fontify case/goto keywords and targets, and case default/goto tags.
76f5e2af
GM
2315 '("\\<\\(case\\|goto\\)\\>"
2316 (1 font-lock-keyword-face)
2317 ("\\(-[0-9]+\\|\\sw+\\)"
2318 ;; Return limit of search.
2319 (save-excursion (skip-chars-forward "^:\n") (point))
2320 nil
2321 (1 font-lock-constant-face nil t)))
68c67d1f
SM
2322 ;; Anders Lindgren <andersl@andersl.com> points out that it is quicker to
2323 ;; use MATCH-ANCHORED to effectively anchor the regexp on the left.
e0e277ff 2324 ;; This must come after the one for keywords and targets.
4d80b2cc 2325 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2e6a15fc 2326 (beginning-of-line) (end-of-line)
8acf2292 2327 (1 font-lock-constant-face)))
f60f18ae 2328 )))
1bd50840 2329
9bfbb130
SM
2330 (setq c-font-lock-keywords-3
2331 (append c-font-lock-keywords-2
2332 ;;
2333 ;; More complicated regexps for more complete highlighting for types.
2334 ;; We still have to fontify type specifiers individually, as C is so hairy.
2335 (list
2336 ;;
4fffc071 2337 ;; Fontify all storage types, plus their items.
c1f2ffc8 2338 `(eval .
d2251bbf 2339 (list (concat "\\<\\(" ,c-type-names "\\)\\>"
c1f2ffc8
SM
2340 "\\([ \t*&]+\\sw+\\>\\)*")
2341 ;; Fontify each declaration item.
2d63aa67
SM
2342 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2343 ;; Start with point after all type specifiers.
4fffc071
SM
2344 (list 'goto-char (list 'or
2345 (list 'match-beginning
d2251bbf 2346 (+ ,c-type-names-depth 2))
2d63aa67
SM
2347 '(match-end 1)))
2348 ;; Finish with point after first type specifier.
2349 '(goto-char (match-end 1))
2350 ;; Fontify as a variable or function name.
2351 '(1 (if (match-beginning 2)
2352 font-lock-function-name-face
2353 font-lock-variable-name-face)))))
9bfbb130 2354 ;;
4fffc071
SM
2355 ;; Fontify all storage specs and types, plus their items.
2356 `(eval .
d2251bbf 2357 (list (concat "\\<\\(" ,c-type-specs "\\)\\>"
4fffc071
SM
2358 "[ \t]*\\(\\sw+\\)?")
2359 (list 1 'font-lock-keyword-face)
d2251bbf 2360 (list ,(+ c-type-specs-depth 2) 'font-lock-type-face nil t)
4fffc071
SM
2361 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2362 nil nil
2363 ;; Fontify as a variable or function name.
2364 '(1 (if (match-beginning 2)
2365 font-lock-function-name-face
2366 font-lock-variable-name-face) nil t))))
2367 ;;
9bfbb130
SM
2368 ;; Fontify structures, or typedef names, plus their items.
2369 '("\\(}\\)[ \t*]*\\sw"
c1f2ffc8 2370 (font-lock-match-c-style-declaration-item-and-skip-to-next
9bfbb130 2371 (goto-char (match-end 1)) nil
4fffc071 2372 (1 font-lock-type-face)))
9bfbb130
SM
2373 ;;
2374 ;; Fontify anything at beginning of line as a declaration or definition.
2375 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2376 (1 font-lock-type-face)
c1f2ffc8 2377 (font-lock-match-c-style-declaration-item-and-skip-to-next
9bfbb130 2378 (goto-char (or (match-beginning 2) (match-end 1))) nil
c1f2ffc8 2379 (1 (if (match-beginning 2)
9bfbb130
SM
2380 font-lock-function-name-face
2381 font-lock-variable-name-face))))
2382 )))
2e6a15fc
SM
2383 )
2384
2385(defvar c-font-lock-keywords c-font-lock-keywords-1
2386 "Default expressions to highlight in C mode.
2387See also `c-font-lock-extra-types'.")
2388\f
2389;;; C++.
2390
2391(defconst c++-font-lock-keywords-1 nil
2392 "Subdued level highlighting for C++ mode.")
2393
2394(defconst c++-font-lock-keywords-2 nil
2395 "Medium level highlighting for C++ mode.
2396See also `c++-font-lock-extra-types'.")
2397
2398(defconst c++-font-lock-keywords-3 nil
2399 "Gaudy level highlighting for C++ mode.
2400See also `c++-font-lock-extra-types'.")
9bfbb130 2401
c1f2ffc8
SM
2402(defun font-lock-match-c++-style-declaration-item-and-skip-to-next (limit)
2403 ;; Regexp matches after point: word<word>::word (
2404 ;; ^^^^ ^^^^ ^^^^ ^
2405 ;; Where the match subexpressions are: 1 3 5 6
2406 ;;
2407 ;; Item is delimited by (match-beginning 1) and (match-end 1).
2408 ;; If (match-beginning 3) is non-nil, that part of the item incloses a `<>'.
2409 ;; If (match-beginning 5) is non-nil, that part of the item follows a `::'.
2410 ;; If (match-beginning 6) is non-nil, the item is followed by a `('.
2411 (when (looking-at (eval-when-compile
55015061
SM
2412 (concat
2413 ;; Skip any leading whitespace.
2414 "[ \t*&]*"
2415 ;; This is `c++-type-spec' from below. (Hint hint!)
2416 "\\(\\sw+\\)" ; The instance?
ac6e572c 2417 "\\([ \t]*<\\([^>\n]+\\)[ \t*&]*>\\)?" ; Or template?
844a6a46 2418 "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)*" ; Or member?
55015061
SM
2419 ;; Match any trailing parenthesis.
2420 "[ \t]*\\((\\)?")))
c1f2ffc8
SM
2421 (save-match-data
2422 (condition-case nil
2423 (save-restriction
2424 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
2425 (narrow-to-region (point-min) limit)
2426 (goto-char (match-end 1))
2427 ;; Move over any item value, etc., to the next item.
2428 (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)"))
2429 (goto-char (or (scan-sexps (point) 1) (point-max))))
2430 (goto-char (match-end 2)))
2431 (error t)))))
2432
76f5e2af
GM
2433(defun font-lock-match-c++-structor-declaration (limit)
2434 ;; Match C++ constructors and destructors inside class declarations.
2435 (let ((res nil)
2436 (regexp (concat "^\\s-+\\(\\(virtual\\|explicit\\)\\s-+\\)*~?\\(\\<"
2437 (mapconcat 'identity
2438 c++-font-lock-extra-types "\\|")
2439 "\\>\\)\\s-*("
2440 ;; Don't match function pointer declarations, e.g.:
2441 ;; Foo (*fptr)();
2442 "\\s-*[^*( \t]")))
2443 (while (progn (setq res (re-search-forward regexp limit t))
2444 (and res
2445 (save-excursion
2446 (beginning-of-line)
2447 (save-match-data
2448 (not (vectorp (c-at-toplevel-p))))))))
2449 res))
2450
c1f2ffc8 2451(let* ((c++-keywords
ac6e572c
SM
2452 (eval-when-compile
2453 (regexp-opt
2454 '("break" "continue" "do" "else" "for" "if" "return" "switch"
e0e277ff 2455 "while" "asm" "catch" "delete" "new" "sizeof" "this" "throw" "try"
5d8b66eb 2456 "typeid"
4fffc071
SM
2457 ;; Branko Cibej <branko.cibej@hermes.si> says this is new.
2458 "export"
2459 ;; Mark Mitchell <mmitchell@usa.net> says these are new.
2460 "mutable" "explicit"
2461 ;; Alain Picard <ap@abelard.apana.org.au> suggests treating these
2462 ;; as keywords not types.
2463 "typedef" "template"
2464 "extern" "auto" "register" "const" "volatile" "static"
fd612dd9 2465 "inline" "friend" "virtual"))))
c1f2ffc8 2466 (c++-operators
23a0e11b
SM
2467 (eval-when-compile
2468 (regexp-opt
2469 ;; Taken from Stroustrup, minus keywords otherwise fontified.
2470 '("+" "-" "*" "/" "%" "^" "&" "|" "~" "!" "=" "<" ">" "+=" "-="
2471 "*=" "/=" "%=" "^=" "&=" "|=" "<<" ">>" ">>=" "<<=" "==" "!="
2472 "<=" ">=" "&&" "||" "++" "--" "->*" "," "->" "[]" "()"))))
4fffc071
SM
2473 (c++-type-specs
2474 (eval-when-compile
2475 (regexp-opt
2476 '("class" "public" "private" "protected" "typename"
2477 "struct" "union" "enum" "namespace" "using"
2478 ;; Eric Hopper <hopper@omnifarious.mn.org> says these are new.
2479 "static_cast" "dynamic_cast" "const_cast" "reinterpret_cast") t)))
2480 (c++-type-specs-depth
2481 (regexp-opt-depth c++-type-specs))
2482 (c++-type-names
c1f2ffc8 2483 `(mapconcat 'identity
019f00d8 2484 (cons
d2251bbf
SM
2485 ,(eval-when-compile
2486 (regexp-opt
2487 '("signed" "unsigned" "short" "long"
2488 "int" "char" "float" "double" "void"
2489 "bool" "complex")))
c1f2ffc8
SM
2490 c++-font-lock-extra-types)
2491 "\\|"))
d2251bbf 2492 (c++-type-names-depth `(regexp-opt-depth ,c++-type-names))
55015061
SM
2493 ;;
2494 ;; A brave attempt to match templates following a type and/or match
2495 ;; class membership. See and sync the above function
2496 ;; `font-lock-match-c++-style-declaration-item-and-skip-to-next'.
ac6e572c 2497 (c++-type-suffix (concat "\\([ \t]*<\\([^>\n]+\\)[ \t*&]*>\\)?"
844a6a46 2498 "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)*"))
4fffc071 2499 (c++-type-suffix-depth (regexp-opt-depth c++-type-suffix))
55015061
SM
2500 ;; If the string is a type, it may be followed by the cruft above.
2501 (c++-type-spec (concat "\\(\\sw+\\)\\>" c++-type-suffix))
4fffc071 2502 (c++-type-spec-depth (regexp-opt-depth c++-type-spec))
55015061
SM
2503 ;;
2504 ;; Parenthesis depth of user-defined types not forgetting their cruft.
ac6e572c 2505 (c++-type-depth `(regexp-opt-depth
d2251bbf 2506 (concat ,c++-type-names ,c++-type-suffix)))
2d63aa67 2507 )
9bfbb130
SM
2508 (setq c++-font-lock-keywords-1
2509 (append
2510 ;;
2511 ;; The list `c-font-lock-keywords-1' less that for function names.
2512 (cdr c-font-lock-keywords-1)
9bfbb130 2513 (list
2e6a15fc 2514 ;;
c1f2ffc8
SM
2515 ;; Fontify function name definitions, possibly incorporating class names.
2516 (list (concat "^" c++-type-spec "[ \t]*(")
2517 '(1 (if (or (match-beginning 2) (match-beginning 4))
2518 font-lock-type-face
2519 font-lock-function-name-face))
4fffc071 2520 '(3 font-lock-type-face nil t)
c1f2ffc8 2521 '(5 font-lock-function-name-face nil t))
9bfbb130
SM
2522 )))
2523
1bd50840 2524 (setq c++-font-lock-keywords-2
b89e1134 2525 (append c++-font-lock-keywords-1
a1eb1cf1 2526 (list
9bfbb130
SM
2527 ;;
2528 ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading.
c1f2ffc8 2529 `(eval .
d2251bbf 2530 (cons (concat "\\<\\(" ,c++-type-names "\\)\\>")
c1f2ffc8 2531 'font-lock-type-face))
9bfbb130 2532 ;;
c1f2ffc8
SM
2533 ;; Fontify operator overloading.
2534 (list (concat "\\<\\(operator\\)\\>[ \t]*\\(" c++-operators "\\)?")
2535 '(1 font-lock-keyword-face)
2536 '(2 font-lock-builtin-face nil t))
9bfbb130
SM
2537 ;;
2538 ;; Fontify case/goto keywords and targets, and case default/goto tags.
76f5e2af
GM
2539 '("\\<\\(case\\|goto\\)\\>"
2540 (1 font-lock-keyword-face)
2541 ("\\(-[0-9]+\\|\\sw+\\)[ \t]*\\(::\\)?"
2542 ;; Return limit of search.
2543 (save-excursion
2544 (while (progn
2545 (skip-chars-forward "^:\n")
2546 (looking-at "::"))
2547 (forward-char 2))
2548 (point))
2549 nil
2550 (1 (if (match-beginning 2)
2551 font-lock-type-face
2552 font-lock-constant-face) nil t)))
e0e277ff 2553 ;; This must come after the one for keywords and targets.
2e6a15fc
SM
2554 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:\\($\\|[^:]\\)"
2555 (beginning-of-line) (end-of-line)
8acf2292 2556 (1 font-lock-constant-face)))
9bfbb130
SM
2557 ;;
2558 ;; Fontify other builtin keywords.
4fffc071 2559 (concat "\\<\\(" c++-keywords "\\|" c++-type-specs "\\)\\>")
2e6a15fc
SM
2560 ;;
2561 ;; Eric Hopper <hopper@omnifarious.mn.org> says `true' and `false' are new.
8acf2292 2562 '("\\<\\(false\\|true\\)\\>" . font-lock-constant-face)
9bfbb130
SM
2563 )))
2564
2565 (setq c++-font-lock-keywords-3
2566 (append c++-font-lock-keywords-2
2567 ;;
2568 ;; More complicated regexps for more complete highlighting for types.
2569 (list
2570 ;;
2571 ;; Fontify all storage classes and type specifiers, plus their items.
c1f2ffc8 2572 `(eval .
d2251bbf
SM
2573 (list (concat "\\<\\(" ,c++-type-names "\\)\\>" ,c++-type-suffix
2574 "\\([ \t*&]+" ,c++-type-spec "\\)*")
4fffc071 2575 ;; The name of any template type.
d2251bbf 2576 (list (+ ,c++-type-names-depth 3) 'font-lock-type-face nil t)
c1f2ffc8 2577 ;; Fontify each declaration item.
2d63aa67
SM
2578 (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next
2579 ;; Start with point after all type specifiers.
2580 (list 'goto-char (list 'or (list 'match-beginning
d2251bbf 2581 (+ ,c++-type-depth 2))
2d63aa67
SM
2582 '(match-end 1)))
2583 ;; Finish with point after first type specifier.
2584 '(goto-char (match-end 1))
2585 ;; Fontify as a variable or function name.
2586 '(1 (cond ((or (match-beginning 2) (match-beginning 4))
2587 font-lock-type-face)
68c67d1f
SM
2588 ((and (match-beginning 6) (c-at-toplevel-p))
2589 font-lock-function-name-face)
2590 (t
2591 font-lock-variable-name-face)))
4fffc071 2592 '(3 font-lock-type-face nil t)
2d63aa67
SM
2593 '(5 (if (match-beginning 6)
2594 font-lock-function-name-face
2595 font-lock-variable-name-face) nil t))))
9bfbb130 2596 ;;
4fffc071
SM
2597 ;; Fontify all storage specs and types, plus their items.
2598 `(eval .
d2251bbf
SM
2599 (list (concat "\\<" ,c++-type-specs "\\>" ,c++-type-suffix
2600 "[ \t]*\\(" ,c++-type-spec "\\)?")
4fffc071 2601 ;; The name of any template type.
d2251bbf 2602 (list ,(+ c++-type-specs-depth 2) 'font-lock-type-face nil t)
4fffc071 2603 ;; The name of any type.
d2251bbf 2604 (list (+ ,c++-type-specs-depth ,c++-type-suffix-depth 2)
4fffc071
SM
2605 'font-lock-type-face nil t)
2606 ;; Fontify each declaration item.
2607 (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next
2608 ;; Start with point after all type specifiers.
2609 nil
2610 ;; Finish with point after first type specifier.
2611 nil
2612 ;; Fontify as a variable or function name.
2613 '(1 (cond ((or (match-beginning 2) (match-beginning 4))
2614 font-lock-type-face)
68c67d1f
SM
2615 ((and (match-beginning 6) (c-at-toplevel-p))
2616 font-lock-function-name-face)
2617 (t
2618 font-lock-variable-name-face)))
4fffc071
SM
2619 '(3 font-lock-type-face nil t)
2620 '(5 (if (match-beginning 6)
2621 font-lock-function-name-face
2622 font-lock-variable-name-face) nil t))
2623 ))
2624 ;;
9bfbb130
SM
2625 ;; Fontify structures, or typedef names, plus their items.
2626 '("\\(}\\)[ \t*]*\\sw"
2627 (font-lock-match-c++-style-declaration-item-and-skip-to-next
2628 (goto-char (match-end 1)) nil
4fffc071 2629 (1 font-lock-type-face)))
9bfbb130
SM
2630 ;;
2631 ;; Fontify anything at beginning of line as a declaration or definition.
c1f2ffc8
SM
2632 (list (concat "^\\(" c++-type-spec "[ \t*&]*\\)+")
2633 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
2634 (goto-char (match-beginning 1))
2635 (goto-char (match-end 1))
2636 (1 (cond ((or (match-beginning 2) (match-beginning 4))
2637 font-lock-type-face)
2638 ((match-beginning 6) font-lock-function-name-face)
2639 (t font-lock-variable-name-face)))
4fffc071 2640 (3 font-lock-type-face nil t)
c1f2ffc8
SM
2641 (5 (if (match-beginning 6)
2642 font-lock-function-name-face
2643 font-lock-variable-name-face) nil t)))
76f5e2af
GM
2644 ;;
2645 ;; Fontify constructors and destructors inside class declarations.
2646 '(font-lock-match-c++-structor-declaration
2647 (3 font-lock-function-name-face t))
9bfbb130 2648 )))
f60f18ae 2649 )
030f4a35 2650
fb512de9 2651(defvar c++-font-lock-keywords c++-font-lock-keywords-1
2e6a15fc
SM
2652 "Default expressions to highlight in C++ mode.
2653See also `c++-font-lock-extra-types'.")
2654\f
2655;;; Objective-C.
2656
2657(defconst objc-font-lock-keywords-1 nil
2658 "Subdued level highlighting for Objective-C mode.")
2659
2660(defconst objc-font-lock-keywords-2 nil
2661 "Medium level highlighting for Objective-C mode.
2662See also `objc-font-lock-extra-types'.")
2663
2664(defconst objc-font-lock-keywords-3 nil
2665 "Gaudy level highlighting for Objective-C mode.
2666See also `objc-font-lock-extra-types'.")
2667
2668;; Regexps written with help from Stephen Peters <speters@us.oracle.com> and
2669;; Jacques Duthen Prestataire <duthen@cegelec-red.fr>.
2d63aa67 2670(let* ((objc-keywords
ac6e572c
SM
2671 (eval-when-compile
2672 (regexp-opt '("break" "continue" "do" "else" "for" "if" "return"
4fffc071
SM
2673 "switch" "while" "sizeof" "self" "super"
2674 "typedef" "auto" "extern" "static"
fd612dd9 2675 "volatile" "const"))))
4fffc071
SM
2676 (objc-type-specs
2677 (eval-when-compile
2678 (regexp-opt
2679 '("register" "struct" "union" "enum"
2680 "oneway" "in" "out" "inout" "bycopy" "byref") t)))
2681 (objc-type-specs-depth
2682 (regexp-opt-depth objc-type-specs))
2683 (objc-type-names
2d63aa67
SM
2684 `(mapconcat 'identity
2685 (cons
d2251bbf
SM
2686 ,(eval-when-compile
2687 (regexp-opt
2688 '("signed" "unsigned" "short" "long"
2689 "int" "char" "float" "double" "void"
2690 "id")))
2d63aa67
SM
2691 objc-font-lock-extra-types)
2692 "\\|"))
4fffc071 2693 (objc-type-names-depth
d2251bbf 2694 `(regexp-opt-depth ,objc-type-names))
2d63aa67 2695 )
2e6a15fc
SM
2696 (setq objc-font-lock-keywords-1
2697 (append
2698 ;;
2699 ;; The list `c-font-lock-keywords-1' less that for function names.
2700 (cdr c-font-lock-keywords-1)
2701 (list
2702 ;;
2703 ;; Fontify compiler directives.
c1f2ffc8
SM
2704 '("@\\(\\sw+\\)\\>"
2705 (1 font-lock-keyword-face)
4fffc071
SM
2706 ("\\=[ \t:<,]*\\(\\sw+\\)" nil nil
2707 (1 font-lock-type-face)))
2e6a15fc
SM
2708 ;;
2709 ;; Fontify method names and arguments. Oh Lordy!
2710 ;; First, on the same line as the function declaration.
4fffc071
SM
2711 '("^[+-][ \t]*\\(PRIVATE\\>\\)?[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2712 (1 font-lock-keyword-face nil t)
2713 (3 font-lock-function-name-face)
2714 ("\\=[ \t]*\\(\\sw+\\)?:[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2e6a15fc
SM
2715 nil nil
2716 (1 font-lock-function-name-face nil t)
4fffc071 2717 (3 font-lock-variable-name-face)))
2e6a15fc 2718 ;; Second, on lines following the function declaration.
4fffc071 2719 '(":" ("^[ \t]*\\(\\sw+\\)?:[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2e6a15fc
SM
2720 (beginning-of-line) (end-of-line)
2721 (1 font-lock-function-name-face nil t)
4fffc071 2722 (3 font-lock-variable-name-face)))
2e6a15fc 2723 )))
030f4a35 2724
2e6a15fc
SM
2725 (setq objc-font-lock-keywords-2
2726 (append objc-font-lock-keywords-1
2727 (list
2728 ;;
2729 ;; Simple regexps for speed.
2730 ;;
2731 ;; Fontify all type specifiers.
c1f2ffc8 2732 `(eval .
d2251bbf 2733 (cons (concat "\\<\\(" ,objc-type-names "\\)\\>")
c1f2ffc8 2734 'font-lock-type-face))
2e6a15fc
SM
2735 ;;
2736 ;; Fontify all builtin keywords (except case, default and goto; see below).
4fffc071 2737 (concat "\\<\\(" objc-keywords "\\|" objc-type-specs "\\)\\>")
2e6a15fc
SM
2738 ;;
2739 ;; Fontify case/goto keywords and targets, and case default/goto tags.
56fcbd7e 2740 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
8acf2292 2741 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
2e6a15fc 2742 ;; Fontify tags iff sole statement on line, otherwise we detect selectors.
e0e277ff 2743 ;; This must come after the one for keywords and targets.
2e6a15fc
SM
2744 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2745 (beginning-of-line) (end-of-line)
8acf2292 2746 (1 font-lock-constant-face)))
2e6a15fc
SM
2747 ;;
2748 ;; Fontify null object pointers.
8acf2292 2749 '("\\<[Nn]il\\>" . font-lock-constant-face)
2e6a15fc 2750 )))
d46c21ec 2751
2e6a15fc
SM
2752 (setq objc-font-lock-keywords-3
2753 (append objc-font-lock-keywords-2
2754 ;;
2755 ;; More complicated regexps for more complete highlighting for types.
2756 ;; We still have to fontify type specifiers individually, as C is so hairy.
2757 (list
2758 ;;
2759 ;; Fontify all storage classes and type specifiers, plus their items.
c1f2ffc8 2760 `(eval .
d2251bbf 2761 (list (concat "\\<\\(" ,objc-type-names "\\)\\>"
c1f2ffc8
SM
2762 "\\([ \t*&]+\\sw+\\>\\)*")
2763 ;; Fontify each declaration item.
2d63aa67
SM
2764 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2765 ;; Start with point after all type specifiers.
4fffc071
SM
2766 (list 'goto-char
2767 (list 'or (list 'match-beginning
d2251bbf 2768 (+ ,objc-type-names-depth 2))
4fffc071 2769 '(match-end 1)))
2d63aa67
SM
2770 ;; Finish with point after first type specifier.
2771 '(goto-char (match-end 1))
2772 ;; Fontify as a variable or function name.
2773 '(1 (if (match-beginning 2)
2774 font-lock-function-name-face
2775 font-lock-variable-name-face)))))
2e6a15fc 2776 ;;
4fffc071
SM
2777 ;; Fontify all storage specs and types, plus their items.
2778 `(eval .
d2251bbf 2779 (list (concat "\\<\\(" ,objc-type-specs "[ \t]*\\)+\\>"
4fffc071
SM
2780 "[ \t]*\\(\\sw+\\)?")
2781 ;; The name of any type.
d2251bbf 2782 (list ,(+ objc-type-specs-depth 2) 'font-lock-type-face nil t)
4fffc071
SM
2783 ;; Fontify each declaration item.
2784 (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next
2785 nil nil
2786 ;; Fontify as a variable or function name.
2787 '(1 (if (match-beginning 2)
2788 font-lock-function-name-face
2789 font-lock-variable-name-face)))
2790 ))
2791 ;;
2e6a15fc
SM
2792 ;; Fontify structures, or typedef names, plus their items.
2793 '("\\(}\\)[ \t*]*\\sw"
c1f2ffc8 2794 (font-lock-match-c-style-declaration-item-and-skip-to-next
2e6a15fc 2795 (goto-char (match-end 1)) nil
4fffc071 2796 (1 font-lock-type-face)))
2e6a15fc
SM
2797 ;;
2798 ;; Fontify anything at beginning of line as a declaration or definition.
2799 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2800 (1 font-lock-type-face)
c1f2ffc8 2801 (font-lock-match-c-style-declaration-item-and-skip-to-next
2e6a15fc 2802 (goto-char (or (match-beginning 2) (match-end 1))) nil
c1f2ffc8 2803 (1 (if (match-beginning 2)
2e6a15fc
SM
2804 font-lock-function-name-face
2805 font-lock-variable-name-face))))
2806 )))
2807 )
2808
2809(defvar objc-font-lock-keywords objc-font-lock-keywords-1
2810 "Default expressions to highlight in Objective-C mode.
2811See also `objc-font-lock-extra-types'.")
2812\f
2813;;; Java.
2814
2815(defconst java-font-lock-keywords-1 nil
2816 "Subdued level highlighting for Java mode.")
2817
2818(defconst java-font-lock-keywords-2 nil
2819 "Medium level highlighting for Java mode.
2820See also `java-font-lock-extra-types'.")
2821
2822(defconst java-font-lock-keywords-3 nil
2823 "Gaudy level highlighting for Java mode.
2824See also `java-font-lock-extra-types'.")
2825
4d80b2cc
SM
2826;; Regexps written with help from Fred White <fwhite@bbn.com>,
2827;; Anders Lindgren <andersl@andersl.com> and Carl Manning <caroma@ai.mit.edu>.
2d63aa67 2828(let* ((java-keywords
ac6e572c
SM
2829 (eval-when-compile
2830 (regexp-opt
2831 '("catch" "do" "else" "super" "this" "finally" "for" "if"
68c67d1f 2832 ;; Anders Lindgren <andersl@andersl.com> says these have gone.
ac6e572c
SM
2833 ;; "cast" "byvalue" "future" "generic" "operator" "var"
2834 ;; "inner" "outer" "rest"
68c67d1f 2835 "implements" "extends" "throws" "instanceof" "new"
fd612dd9 2836 "interface" "return" "switch" "throw" "try" "while"))))
2d63aa67 2837 ;;
68c67d1f
SM
2838 ;; Classes immediately followed by an object name.
2839 (java-type-names
2840 `(mapconcat 'identity
019f00d8 2841 (cons
d2251bbf
SM
2842 ,(eval-when-compile
2843 (regexp-opt '("boolean" "char" "byte" "short" "int" "long"
2844 "float" "double" "void")))
68c67d1f
SM
2845 java-font-lock-extra-types)
2846 "\\|"))
d2251bbf 2847 (java-type-names-depth `(regexp-opt-depth ,java-type-names))
2d63aa67
SM
2848 ;;
2849 ;; These are eventually followed by an object name.
68c67d1f 2850 (java-type-specs
ac6e572c
SM
2851 (eval-when-compile
2852 (regexp-opt
2853 '("abstract" "const" "final" "synchronized" "transient" "static"
68c67d1f 2854 ;; Anders Lindgren <andersl@andersl.com> says this has gone.
ac6e572c 2855 ;; "threadsafe"
b09f207a
SM
2856 "volatile" "public" "private" "protected" "native"
2857 ;; Carl Manning <caroma@ai.mit.edu> says this is new.
2858 "strictfp"))))
ac6e572c 2859 )
2e6a15fc
SM
2860 (setq java-font-lock-keywords-1
2861 (list
2862 ;;
2863 ;; Fontify class names.
2864 '("\\<\\(class\\)\\>[ \t]*\\(\\sw+\\)?"
4d80b2cc 2865 (1 font-lock-keyword-face) (2 font-lock-type-face nil t))
2e6a15fc
SM
2866 ;;
2867 ;; Fontify package names in import directives.
2868 '("\\<\\(import\\|package\\)\\>[ \t]*\\(\\sw+\\)?"
68c67d1f
SM
2869 (1 font-lock-keyword-face)
2870 (2 font-lock-constant-face nil t)
2871 ("\\=\\.\\(\\*\\|\\sw+\\)" nil nil
2872 (1 font-lock-constant-face nil t)))
2e6a15fc
SM
2873 ))
2874
2875 (setq java-font-lock-keywords-2
2876 (append java-font-lock-keywords-1
2877 (list
2878 ;;
68c67d1f
SM
2879 ;; Fontify class names.
2880 `(eval .
d2251bbf 2881 (cons (concat "\\<\\(" ,java-type-names "\\)\\>[^.]")
4d80b2cc 2882 '(1 font-lock-type-face)))
2e6a15fc
SM
2883 ;;
2884 ;; Fontify all builtin keywords (except below).
68c67d1f 2885 (concat "\\<\\(" java-keywords "\\|" java-type-specs "\\)\\>")
2e6a15fc
SM
2886 ;;
2887 ;; Fontify keywords and targets, and case default/goto tags.
56fcbd7e 2888 (list "\\<\\(break\\|case\\|continue\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
8acf2292 2889 '(1 font-lock-keyword-face) '(2 font-lock-constant-face nil t))
e0e277ff 2890 ;; This must come after the one for keywords and targets.
4d80b2cc 2891 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2e6a15fc 2892 (beginning-of-line) (end-of-line)
8acf2292 2893 (1 font-lock-constant-face)))
2e6a15fc 2894 ;;
2e6a15fc 2895 ;; Fontify all constants.
8acf2292 2896 '("\\<\\(false\\|null\\|true\\)\\>" . font-lock-constant-face)
2e6a15fc
SM
2897 ;;
2898 ;; Javadoc tags within comments.
32038c7a
GM
2899 (list
2900 (concat "@\\("
2901 "author\\|deprecated\\|exception"
2902 "\\|link\\|return\\|see\\|serial\\|serialData\\|serialField"
2903 "\\|since\\|throws"
2904 "\\|version"
1bc7654a
GM
2905 "\\)\\>")
2906 '(1 font-lock-constant-face prepend))
2e6a15fc 2907 '("@\\(param\\)\\>[ \t]*\\(\\sw+\\)?"
8acf2292 2908 (1 font-lock-constant-face prepend)
2e6a15fc 2909 (2 font-lock-variable-name-face prepend t))
340fe50f
GM
2910 '("@\\(exception\\|throws\\)\\>[ \t]*\\(\\S-+\\)?"
2911 (1 font-lock-constant-face prepend)
2912 (2 font-lock-type-face prepend t))
2e6a15fc
SM
2913 )))
2914
2915 (setq java-font-lock-keywords-3
2916 (append java-font-lock-keywords-2
2917 ;;
2918 ;; More complicated regexps for more complete highlighting for types.
2919 ;; We still have to fontify type specifiers individually, as Java is hairy.
2920 (list
2921 ;;
2e6a15fc 2922 ;; Fontify random types immediately followed by an item or items.
c1f2ffc8 2923 `(eval .
d2251bbf 2924 (list (concat "\\<\\(" ,java-type-names "\\)\\>"
c1f2ffc8
SM
2925 "\\([ \t]*\\[[ \t]*\\]\\)*"
2926 "\\([ \t]*\\sw\\)")
2927 ;; Fontify each declaration item.
2d63aa67
SM
2928 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2929 ;; Start and finish with point after the type specifier.
2930 (list 'goto-char (list 'match-beginning
d2251bbf 2931 (+ ,java-type-names-depth 3)))
2d63aa67 2932 (list 'goto-char (list 'match-beginning
d2251bbf 2933 (+ ,java-type-names-depth 3)))
2d63aa67
SM
2934 ;; Fontify as a variable or function name.
2935 '(1 (if (match-beginning 2)
2936 font-lock-function-name-face
2937 font-lock-variable-name-face)))))
2e6a15fc 2938 ;;
2e6a15fc 2939 ;; Fontify those that are eventually followed by an item or items.
68c67d1f 2940 (list (concat "\\<\\(" java-type-specs "\\)\\>"
2e6a15fc
SM
2941 "\\([ \t]+\\sw+\\>"
2942 "\\([ \t]*\\[[ \t]*\\]\\)*"
2943 "\\)*")
2944 ;; Fontify each declaration item.
c1f2ffc8 2945 '(font-lock-match-c-style-declaration-item-and-skip-to-next
2e6a15fc 2946 ;; Start with point after all type specifiers.
2d63aa67 2947 (goto-char (or (match-beginning 5) (match-end 1)))
2e6a15fc
SM
2948 ;; Finish with point after first type specifier.
2949 (goto-char (match-end 1))
2950 ;; Fontify as a variable or function name.
c1f2ffc8 2951 (1 (if (match-beginning 2)
2e6a15fc
SM
2952 font-lock-function-name-face
2953 font-lock-variable-name-face))))
2954 )))
2955 )
2956
2957(defvar java-font-lock-keywords java-font-lock-keywords-1
2958 "Default expressions to highlight in Java mode.
2959See also `java-font-lock-extra-types'.")
d46c21ec 2960\f
a1eb1cf1 2961;; Provide ourselves:
8f261d40 2962
030f4a35 2963(provide 'font-lock)
8e5b2bfe 2964(require 'jit-lock)
030f4a35
RS
2965
2966;;; font-lock.el ends here