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