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