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