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