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