(IT_write_glyphs): Move constant expression out of the loop.
[bpt/emacs.git] / lisp / font-lock.el
CommitLineData
876f2438
SM
1;;; font-lock.el --- Electric font lock mode
2
9c8de95c 3;; Copyright (C) 1992, 93, 94, 95, 96, 97, 1998 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)
461 nil nil ((?_ . "w") (?$ . "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
SM
1034 (with-temp-message
1035 (if verbose
1036 (format "Fontifying %s..." (buffer-name))
1037 (current-message))
1038 ;; Make sure we have the right `font-lock-keywords' etc.
1039 (unless font-lock-mode
1040 (font-lock-set-defaults))
1041 ;; Make sure we fontify etc. in the whole buffer.
1042 (save-restriction
1043 (widen)
1044 (condition-case nil
1045 (save-excursion
1046 (save-match-data
1047 (font-lock-fontify-region (point-min) (point-max) verbose)
1048 (font-lock-after-fontify-buffer)
1049 (setq font-lock-fontified t)))
1050 ;; We don't restore the old fontification, so it's best to unfontify.
1051 (quit (font-lock-unfontify-buffer))))
1052 ;; Make sure we undo `font-lock-keywords' etc.
1053 (unless font-lock-mode
1054 (font-lock-unset-defaults)))))
7d7d915a 1055
a2b8e66b 1056(defun font-lock-default-unfontify-buffer ()
56fcbd7e 1057 ;; Make sure we unfontify etc. in the whole buffer.
a2b8e66b
SM
1058 (save-restriction
1059 (widen)
1060 (font-lock-unfontify-region (point-min) (point-max))
271c888a 1061 (font-lock-after-unfontify-buffer)
a2b8e66b 1062 (setq font-lock-fontified nil)))
9bfbb130 1063
a2b8e66b 1064(defun font-lock-default-fontify-region (beg end loudly)
ac6e572c
SM
1065 (save-buffer-state
1066 ((parse-sexp-lookup-properties font-lock-syntactic-keywords)
1067 (old-syntax-table (syntax-table)))
876f2438 1068 (unwind-protect
a078558d
SM
1069 (save-restriction
1070 (widen)
876f2438 1071 ;; Use the fontification syntax table, if any.
2e6a15fc
SM
1072 (when font-lock-syntax-table
1073 (set-syntax-table font-lock-syntax-table))
876f2438 1074 ;; Now do the fontification.
2e6a15fc 1075 (font-lock-unfontify-region beg end)
ac6e572c
SM
1076 (when font-lock-syntactic-keywords
1077 (font-lock-fontify-syntactic-keywords-region beg end))
2e6a15fc 1078 (unless font-lock-keywords-only
876f2438
SM
1079 (font-lock-fontify-syntactically-region beg end loudly))
1080 (font-lock-fontify-keywords-region beg end loudly))
1081 ;; Clean up.
2e6a15fc 1082 (set-syntax-table old-syntax-table))))
9bfbb130
SM
1083
1084;; The following must be rethought, since keywords can override fontification.
1085; ;; Now scan for keywords, but not if we are inside a comment now.
1086; (or (and (not font-lock-keywords-only)
1087; (let ((state (parse-partial-sexp beg end nil nil
1088; font-lock-cache-state)))
1089; (or (nth 4 state) (nth 7 state))))
1090; (font-lock-fontify-keywords-region beg end))
1091
a2b8e66b 1092(defun font-lock-default-unfontify-region (beg end)
2e6a15fc 1093 (save-buffer-state nil
ac6e572c 1094 (remove-text-properties beg end '(face nil syntax-table nil))))
9bfbb130
SM
1095
1096;; Called when any modification is made to buffer text.
1097(defun font-lock-after-change-function (beg end old-len)
46f26fcf
SM
1098 (let ((inhibit-point-motion-hooks t))
1099 (save-excursion
1100 (save-match-data
1101 ;; Rescan between start of lines enclosing the region.
1102 (font-lock-fontify-region
1103 (progn (goto-char beg) (beginning-of-line) (point))
1104 (progn (goto-char end) (forward-line 1) (point)))))))
a2b8e66b 1105
a078558d
SM
1106(defun font-lock-fontify-block (&optional arg)
1107 "Fontify some lines the way `font-lock-fontify-buffer' would.
1108The lines could be a function or paragraph, or a specified number of lines.
a078558d 1109If ARG is given, fontify that many lines before and after point, or 16 lines if
a465832f
SM
1110no ARG is given and `font-lock-mark-block-function' is nil.
1111If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
1112delimit the region to fontify."
a078558d 1113 (interactive "P")
46f26fcf
SM
1114 (let ((inhibit-point-motion-hooks t) font-lock-beginning-of-syntax-function
1115 deactivate-mark)
a078558d
SM
1116 ;; Make sure we have the right `font-lock-keywords' etc.
1117 (if (not font-lock-mode) (font-lock-set-defaults))
a2b8e66b
SM
1118 (save-excursion
1119 (save-match-data
1120 (condition-case error-data
a078558d
SM
1121 (if (or arg (not font-lock-mark-block-function))
1122 (let ((lines (if arg (prefix-numeric-value arg) 16)))
1123 (font-lock-fontify-region
1124 (save-excursion (forward-line (- lines)) (point))
1125 (save-excursion (forward-line lines) (point))))
1126 (funcall font-lock-mark-block-function)
1127 (font-lock-fontify-region (point) (mark)))
6c0fc428 1128 ((error quit) (message "Fontifying block...%s" error-data)))))))
a078558d 1129
94de9afe 1130(define-key facemenu-keymap "\M-g" 'font-lock-fontify-block)
2d63aa67
SM
1131
1132;;; End of Fontification functions.
9bfbb130 1133\f
9bfbb130
SM
1134;;; Additional text property functions.
1135
1c626aaf
SM
1136;; The following text property functions should be builtins. This means they
1137;; should be written in C and put with all the other text property functions.
1138;; In the meantime, those that are used by font-lock.el are defined in Lisp
1139;; below and given a `font-lock-' prefix. Those that are not used are defined
1140;; in Lisp below and commented out. sm.
9bfbb130 1141
1c626aaf 1142(defun font-lock-prepend-text-property (start end prop value &optional object)
9bfbb130
SM
1143 "Prepend to one property of the text from START to END.
1144Arguments PROP and VALUE specify the property and value to prepend to the value
1c626aaf 1145already in place. The resulting property values are always lists.
9bfbb130
SM
1146Optional argument OBJECT is the string or buffer containing the text."
1147 (let ((val (if (listp value) value (list value))) next prev)
1148 (while (/= start end)
1149 (setq next (next-single-property-change start prop object end)
1150 prev (get-text-property start prop object))
1c626aaf
SM
1151 (put-text-property start next prop
1152 (append val (if (listp prev) prev (list prev)))
1153 object)
9bfbb130
SM
1154 (setq start next))))
1155
1c626aaf 1156(defun font-lock-append-text-property (start end prop value &optional object)
9bfbb130
SM
1157 "Append to one property of the text from START to END.
1158Arguments PROP and VALUE specify the property and value to append to the value
1c626aaf 1159already in place. The resulting property values are always lists.
9bfbb130
SM
1160Optional argument OBJECT is the string or buffer containing the text."
1161 (let ((val (if (listp value) value (list value))) next prev)
1162 (while (/= start end)
1163 (setq next (next-single-property-change start prop object end)
1164 prev (get-text-property start prop object))
1c626aaf
SM
1165 (put-text-property start next prop
1166 (append (if (listp prev) prev (list prev)) val)
1167 object)
9bfbb130 1168 (setq start next))))
1c626aaf
SM
1169
1170(defun font-lock-fillin-text-property (start end prop value &optional object)
1171 "Fill in one property of the text from START to END.
1172Arguments PROP and VALUE specify the property and value to put where none are
1173already in place. Therefore existing property values are not overwritten.
1174Optional argument OBJECT is the string or buffer containing the text."
1175 (let ((start (text-property-any start end prop nil object)) next)
1176 (while start
1177 (setq next (next-single-property-change start prop object end))
1178 (put-text-property start next prop value object)
1179 (setq start (text-property-any next end prop nil object)))))
1180
1181;; For completeness: this is to `remove-text-properties' as `put-text-property'
1182;; is to `add-text-properties', etc.
1183;(defun remove-text-property (start end property &optional object)
1184; "Remove a property from text from START to END.
1185;Argument PROPERTY is the property to remove.
1186;Optional argument OBJECT is the string or buffer containing the text.
1187;Return t if the property was actually removed, nil otherwise."
1188; (remove-text-properties start end (list property) object))
1189
1190;; For consistency: maybe this should be called `remove-single-property' like
1191;; `next-single-property-change' (not `next-single-text-property-change'), etc.
1192;(defun remove-single-text-property (start end prop value &optional object)
1193; "Remove a specific property value from text from START to END.
1194;Arguments PROP and VALUE specify the property and value to remove. The
1195;resulting property values are not equal to VALUE nor lists containing VALUE.
1196;Optional argument OBJECT is the string or buffer containing the text."
1197; (let ((start (text-property-not-all start end prop nil object)) next prev)
1198; (while start
1199; (setq next (next-single-property-change start prop object end)
1200; prev (get-text-property start prop object))
1201; (cond ((and (symbolp prev) (eq value prev))
1202; (remove-text-property start next prop object))
1203; ((and (listp prev) (memq value prev))
1204; (let ((new (delq value prev)))
1205; (cond ((null new)
1206; (remove-text-property start next prop object))
1207; ((= (length new) 1)
1208; (put-text-property start next prop (car new) object))
1209; (t
1210; (put-text-property start next prop new object))))))
1211; (setq start (text-property-not-all next end prop nil object)))))
2d63aa67
SM
1212
1213;;; End of Additional text property functions.
9bfbb130 1214\f
ac6e572c
SM
1215;;; Syntactic regexp fontification functions.
1216
1217;; These syntactic keyword pass functions are identical to those keyword pass
1218;; functions below, with the following exceptions; (a) they operate on
1219;; `font-lock-syntactic-keywords' of course, (b) they are all `defun' as speed
1220;; is less of an issue, (c) eval of property value does not occur JIT as speed
1221;; is less of an issue, (d) OVERRIDE cannot be `prepend' or `append' as it
1222;; makes no sense for `syntax-table' property values, (e) they do not do it
1223;; LOUDLY as it is not likely to be intensive.
1224
1225(defun font-lock-apply-syntactic-highlight (highlight)
1226 "Apply HIGHLIGHT following a match.
1227HIGHLIGHT should be of the form MATCH-HIGHLIGHT,
1228see `font-lock-syntactic-keywords'."
1229 (let* ((match (nth 0 highlight))
1230 (start (match-beginning match)) (end (match-end match))
1231 (value (nth 1 highlight))
1232 (override (nth 2 highlight)))
1233 (unless (numberp (car value))
1234 (setq value (eval value)))
1235 (cond ((not start)
1236 ;; No match but we might not signal an error.
1237 (or (nth 3 highlight)
1238 (error "No match %d in highlight %S" match highlight)))
1239 ((not override)
1240 ;; Cannot override existing fontification.
1241 (or (text-property-not-all start end 'syntax-table nil)
1242 (put-text-property start end 'syntax-table value)))
1243 ((eq override t)
1244 ;; Override existing fontification.
1245 (put-text-property start end 'syntax-table value))
1246 ((eq override 'keep)
1247 ;; Keep existing fontification.
1248 (font-lock-fillin-text-property start end 'syntax-table value)))))
1249
1250(defun font-lock-fontify-syntactic-anchored-keywords (keywords limit)
1251 "Fontify according to KEYWORDS until LIMIT.
1252KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1253LIMIT can be modified by the value of its PRE-MATCH-FORM."
1254 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1255 ;; Evaluate PRE-MATCH-FORM.
1256 (pre-match-value (eval (nth 1 keywords))))
1257 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1258 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1259 (setq limit pre-match-value)
1260 (save-excursion (end-of-line) (setq limit (point))))
1261 (save-match-data
1262 ;; Find an occurrence of `matcher' before `limit'.
1263 (while (if (stringp matcher)
1264 (re-search-forward matcher limit t)
1265 (funcall matcher limit))
1266 ;; Apply each highlight to this instance of `matcher'.
1267 (setq highlights lowdarks)
1268 (while highlights
1269 (font-lock-apply-syntactic-highlight (car highlights))
1270 (setq highlights (cdr highlights)))))
1271 ;; Evaluate POST-MATCH-FORM.
1272 (eval (nth 2 keywords))))
1273
1274(defun font-lock-fontify-syntactic-keywords-region (start end)
1275 "Fontify according to `font-lock-syntactic-keywords' between START and END.
1276START should be at the beginning of a line."
1277 ;; If `font-lock-syntactic-keywords' is a symbol, get the real keywords.
1278 (when (symbolp font-lock-syntactic-keywords)
1279 (setq font-lock-syntactic-keywords (font-lock-eval-keywords
1280 font-lock-syntactic-keywords)))
1281 ;; If `font-lock-syntactic-keywords' is not compiled, compile it.
1282 (unless (eq (car font-lock-syntactic-keywords) t)
1283 (setq font-lock-syntactic-keywords (font-lock-compile-keywords
1284 font-lock-syntactic-keywords)))
1285 ;; Get down to business.
1286 (let ((case-fold-search font-lock-keywords-case-fold-search)
1287 (keywords (cdr font-lock-syntactic-keywords))
1288 keyword matcher highlights)
1289 (while keywords
1290 ;; Find an occurrence of `matcher' from `start' to `end'.
1291 (setq keyword (car keywords) matcher (car keyword))
1292 (goto-char start)
1293 (while (if (stringp matcher)
1294 (re-search-forward matcher end t)
1295 (funcall matcher end))
1296 ;; Apply each highlight to this instance of `matcher', which may be
1297 ;; specific highlights or more keywords anchored to `matcher'.
1298 (setq highlights (cdr keyword))
1299 (while highlights
1300 (if (numberp (car (car highlights)))
1301 (font-lock-apply-syntactic-highlight (car highlights))
1302 (font-lock-fontify-syntactic-anchored-keywords (car highlights)
1303 end))
1304 (setq highlights (cdr highlights))))
1305 (setq keywords (cdr keywords)))))
1306
1307;;; End of Syntactic regexp fontification functions.
1308\f
1309;;; Syntactic fontification functions.
1310
1311;; These record the parse state at a particular position, always the start of a
1312;; line. Used to make `font-lock-fontify-syntactically-region' faster.
1313;; Previously, `font-lock-cache-position' was just a buffer position. However,
1314;; under certain situations, this occasionally resulted in mis-fontification.
1315;; I think the "situations" were deletion with Lazy Lock mode's deferral. sm.
1316(defvar font-lock-cache-state nil)
1317(defvar font-lock-cache-position nil)
1318
1319(defun font-lock-fontify-syntactically-region (start end &optional loudly)
1320 "Put proper face on each string and comment between START and END.
1321START should be at the beginning of a line."
1322 (let ((cache (marker-position font-lock-cache-position))
1323 state string beg)
1324 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
1325 (goto-char start)
1326 ;;
1327 ;; Find the state at the `beginning-of-line' before `start'.
1328 (if (eq start cache)
1329 ;; Use the cache for the state of `start'.
1330 (setq state font-lock-cache-state)
1331 ;; Find the state of `start'.
1332 (if (null font-lock-beginning-of-syntax-function)
1333 ;; Use the state at the previous cache position, if any, or
1334 ;; otherwise calculate from `point-min'.
1335 (if (or (null cache) (< start cache))
1336 (setq state (parse-partial-sexp (point-min) start))
1337 (setq state (parse-partial-sexp cache start nil nil
1338 font-lock-cache-state)))
1339 ;; Call the function to move outside any syntactic block.
1340 (funcall font-lock-beginning-of-syntax-function)
1341 (setq state (parse-partial-sexp (point) start)))
1342 ;; Cache the state and position of `start'.
1343 (setq font-lock-cache-state state)
1344 (set-marker font-lock-cache-position start))
1345 ;;
1346 ;; If the region starts inside a string or comment, show the extent of it.
1347 (when (or (nth 3 state) (nth 4 state))
1348 (setq string (nth 3 state) beg (point))
1349 (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
1350 (put-text-property beg (point) 'face
1351 (if string
1352 font-lock-string-face
1353 font-lock-comment-face)))
1354 ;;
1355 ;; Find each interesting place between here and `end'.
1356 (while (and (< (point) end)
1357 (progn
1358 (setq state (parse-partial-sexp (point) end nil nil state
1359 'syntax-table))
1360 (or (nth 3 state) (nth 4 state))))
1361 (setq string (nth 3 state) beg (nth 8 state))
1362 (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
1363 (put-text-property beg (point) 'face
1364 (if string
1365 font-lock-string-face
1366 font-lock-comment-face)))))
1367
1368;;; End of Syntactic fontification functions.
1369\f
1370;;; Keyword regexp fontification functions.
9bfbb130
SM
1371
1372(defsubst font-lock-apply-highlight (highlight)
1373 "Apply HIGHLIGHT following a match.
1374HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
1375 (let* ((match (nth 0 highlight))
1376 (start (match-beginning match)) (end (match-end match))
1377 (override (nth 2 highlight)))
1378 (cond ((not start)
1379 ;; No match but we might not signal an error.
1380 (or (nth 3 highlight)
1381 (error "No match %d in highlight %S" match highlight)))
1382 ((not override)
1383 ;; Cannot override existing fontification.
1384 (or (text-property-not-all start end 'face nil)
1385 (put-text-property start end 'face (eval (nth 1 highlight)))))
1386 ((eq override t)
1387 ;; Override existing fontification.
1388 (put-text-property start end 'face (eval (nth 1 highlight))))
9bfbb130
SM
1389 ((eq override 'prepend)
1390 ;; Prepend to existing fontification.
1c626aaf 1391 (font-lock-prepend-text-property start end 'face (eval (nth 1 highlight))))
9bfbb130
SM
1392 ((eq override 'append)
1393 ;; Append to existing fontification.
1c626aaf 1394 (font-lock-append-text-property start end 'face (eval (nth 1 highlight))))
c1f2ffc8
SM
1395 ((eq override 'keep)
1396 ;; Keep existing fontification.
1c626aaf 1397 (font-lock-fillin-text-property start end 'face (eval (nth 1 highlight)))))))
9bfbb130
SM
1398
1399(defsubst font-lock-fontify-anchored-keywords (keywords limit)
1400 "Fontify according to KEYWORDS until LIMIT.
56fcbd7e
SM
1401KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1402LIMIT can be modified by the value of its PRE-MATCH-FORM."
1403 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1404 ;; Evaluate PRE-MATCH-FORM.
1405 (pre-match-value (eval (nth 1 keywords))))
1406 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1407 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1408 (setq limit pre-match-value)
1409 (save-excursion (end-of-line) (setq limit (point))))
9bfbb130 1410 (save-match-data
876f2438 1411 ;; Find an occurrence of `matcher' before `limit'.
9bfbb130
SM
1412 (while (if (stringp matcher)
1413 (re-search-forward matcher limit t)
1414 (funcall matcher limit))
876f2438 1415 ;; Apply each highlight to this instance of `matcher'.
9bfbb130
SM
1416 (setq highlights lowdarks)
1417 (while highlights
1418 (font-lock-apply-highlight (car highlights))
1419 (setq highlights (cdr highlights)))))
876f2438 1420 ;; Evaluate POST-MATCH-FORM.
9bfbb130
SM
1421 (eval (nth 2 keywords))))
1422
1423(defun font-lock-fontify-keywords-region (start end &optional loudly)
1424 "Fontify according to `font-lock-keywords' between START and END.
1425START should be at the beginning of a line."
ac6e572c 1426 (unless (eq (car font-lock-keywords) t)
55015061 1427 (setq font-lock-keywords (font-lock-compile-keywords font-lock-keywords)))
9bfbb130 1428 (let ((case-fold-search font-lock-keywords-case-fold-search)
55015061 1429 (keywords (cdr font-lock-keywords))
9bfbb130 1430 (bufname (buffer-name)) (count 0)
876f2438
SM
1431 keyword matcher highlights)
1432 ;;
1433 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1434 (while keywords
1435 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
2e6a15fc 1436 (make-string (incf count) ?.)))
9bfbb130 1437 ;;
876f2438
SM
1438 ;; Find an occurrence of `matcher' from `start' to `end'.
1439 (setq keyword (car keywords) matcher (car keyword))
1440 (goto-char start)
1441 (while (if (stringp matcher)
1442 (re-search-forward matcher end t)
1443 (funcall matcher end))
1444 ;; Apply each highlight to this instance of `matcher', which may be
1445 ;; specific highlights or more keywords anchored to `matcher'.
1446 (setq highlights (cdr keyword))
1447 (while highlights
1448 (if (numberp (car (car highlights)))
1449 (font-lock-apply-highlight (car highlights))
1450 (font-lock-fontify-anchored-keywords (car highlights) end))
1451 (setq highlights (cdr highlights))))
1452 (setq keywords (cdr keywords)))))
2d63aa67 1453
ac6e572c 1454;;; End of Keyword regexp fontification functions.
9bfbb130
SM
1455\f
1456;; Various functions.
1457
55015061
SM
1458(defun font-lock-compile-keywords (keywords)
1459 ;; Compile KEYWORDS into the form (t KEYWORD ...) where KEYWORD is of the
1460 ;; form (MATCHER HIGHLIGHT ...) as shown in `font-lock-keywords' doc string.
1461 (if (eq (car-safe keywords) t)
1462 keywords
1463 (cons t (mapcar 'font-lock-compile-keyword keywords))))
a2b8e66b
SM
1464
1465(defun font-lock-compile-keyword (keyword)
c1f2ffc8 1466 (cond ((nlistp keyword) ; MATCHER
a2b8e66b 1467 (list keyword '(0 font-lock-keyword-face)))
c1f2ffc8 1468 ((eq (car keyword) 'eval) ; (eval . FORM)
a2b8e66b 1469 (font-lock-compile-keyword (eval (cdr keyword))))
c1f2ffc8
SM
1470 ((eq (car-safe (cdr keyword)) 'quote) ; (MATCHER . 'FORM)
1471 ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote.
1472 (if (symbolp (nth 2 keyword))
1473 (list (car keyword) (list 0 (cdr keyword)))
1474 (font-lock-compile-keyword (cons (car keyword) (nth 2 keyword)))))
1475 ((numberp (cdr keyword)) ; (MATCHER . MATCH)
a2b8e66b 1476 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
c1f2ffc8 1477 ((symbolp (cdr keyword)) ; (MATCHER . FACENAME)
a2b8e66b 1478 (list (car keyword) (list 0 (cdr keyword))))
c1f2ffc8 1479 ((nlistp (nth 1 keyword)) ; (MATCHER . HIGHLIGHT)
a2b8e66b 1480 (list (car keyword) (cdr keyword)))
c1f2ffc8 1481 (t ; (MATCHER HIGHLIGHT ...)
a2b8e66b 1482 keyword)))
d46c21ec 1483
ac6e572c
SM
1484(defun font-lock-eval-keywords (keywords)
1485 ;; Evalulate KEYWORDS if a function (funcall) or variable (eval) name.
46f26fcf
SM
1486 (if (listp keywords)
1487 keywords
1488 (font-lock-eval-keywords (if (fboundp keywords)
1489 (funcall keywords)
1490 (eval keywords)))))
ac6e572c 1491
343204bd
SM
1492(defun font-lock-value-in-major-mode (alist)
1493 ;; Return value in ALIST for `major-mode', or ALIST if it is not an alist.
2e6a15fc 1494 ;; Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t.
343204bd
SM
1495 (if (consp alist)
1496 (cdr (or (assq major-mode alist) (assq t alist)))
1497 alist))
1498
d46c21ec
SM
1499(defun font-lock-choose-keywords (keywords level)
1500 ;; Return LEVELth element of KEYWORDS. A LEVEL of nil is equal to a
1501 ;; LEVEL of 0, a LEVEL of t is equal to (1- (length KEYWORDS)).
343204bd
SM
1502 (cond ((symbolp keywords)
1503 keywords)
1504 ((numberp level)
1505 (or (nth level keywords) (car (reverse keywords))))
1506 ((eq level t)
1507 (car (reverse keywords)))
1508 (t
1509 (car keywords))))
d46c21ec 1510
2e6a15fc
SM
1511(defvar font-lock-set-defaults nil) ; Whether we have set up defaults.
1512
d46c21ec
SM
1513(defun font-lock-set-defaults ()
1514 "Set fontification defaults appropriately for this mode.
a2b8e66b
SM
1515Sets various variables using `font-lock-defaults' (or, if nil, using
1516`font-lock-defaults-alist') and `font-lock-maximum-decoration'."
d46c21ec 1517 ;; Set fontification defaults.
a2b8e66b 1518 (make-local-variable 'font-lock-fontified)
2e6a15fc
SM
1519 ;; Set iff not previously set.
1520 (unless font-lock-set-defaults
1521 (set (make-local-variable 'font-lock-set-defaults) t)
1522 (set (make-local-variable 'font-lock-cache-state) nil)
1523 (set (make-local-variable 'font-lock-cache-position) (make-marker))
1524 (let* ((defaults (or font-lock-defaults
1525 (cdr (assq major-mode font-lock-defaults-alist))))
1526 (keywords
1527 (font-lock-choose-keywords (nth 0 defaults)
c1f2ffc8
SM
1528 (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1529 (local (cdr (assq major-mode font-lock-keywords-alist))))
2e6a15fc 1530 ;; Regexp fontification?
2d63aa67 1531 (set (make-local-variable 'font-lock-keywords)
ac6e572c 1532 (font-lock-compile-keywords (font-lock-eval-keywords keywords)))
c1f2ffc8
SM
1533 ;; Local fontification?
1534 (while local
1535 (font-lock-add-keywords nil (car (car local)) (cdr (car local)))
1536 (setq local (cdr local)))
2e6a15fc
SM
1537 ;; Syntactic fontification?
1538 (when (nth 1 defaults)
1539 (set (make-local-variable 'font-lock-keywords-only) t))
1540 ;; Case fold during regexp fontification?
1541 (when (nth 2 defaults)
1542 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
1543 ;; Syntax table for regexp and syntactic fontification?
1544 (when (nth 3 defaults)
1545 (let ((slist (nth 3 defaults)))
1546 (set (make-local-variable 'font-lock-syntax-table)
1547 (copy-syntax-table (syntax-table)))
1548 (while slist
1549 ;; The character to modify may be a single CHAR or a STRING.
1550 (let ((chars (if (numberp (car (car slist)))
1551 (list (car (car slist)))
1552 (mapcar 'identity (car (car slist)))))
1553 (syntax (cdr (car slist))))
1554 (while chars
1555 (modify-syntax-entry (car chars) syntax
1556 font-lock-syntax-table)
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
2273c36b 2202(defcustom java-font-lock-extra-types '("[A-Z\300-\326\330-\337]\\sw+")
2e6a15fc 2203 "*List of extra types to fontify in Java mode.
55015061 2204Each list item should be a regexp not containing word-delimiters.
c1f2ffc8 2205For example, a value of (\"[A-Z\300-\326\330-\337]\\\\sw+\") means capitalised
55015061
SM
2206words (and words conforming to the Java id spec) are treated as type names.
2207
2273c36b
SM
2208The value of this variable is used when Font Lock mode is turned on."
2209 :type 'font-lock-extra-types-widget
2210 :group 'font-lock-extra-types)
2e6a15fc
SM
2211\f
2212;;; C.
c1f2ffc8
SM
2213
2214;; [Murmur murmur murmur] Maestro, drum-roll please... [Murmur murmur murmur.]
2215;; Ahem. [Murmur murmur murmur] Lay-dees an Gennel-men. [Murmur murmur shhh!]
2216;; I am most proud and humbly honoured today [murmur murmur cough] to present
2217;; to you good people, the winner of the Second Millennium Award for The Most
2218;; Hairy Language Syntax. [Ahhh!] All rise please. [Shuffle shuffle
2219;; shuffle.] And a round of applause please. For... The C Language! [Roar.]
2220;;
2221;; Thank you... You are too kind... It is with a feeling of great privilege
2222;; and indeed emotion [sob] that I accept this award. It has been a long hard
2223;; road. But we know our destiny. And our future. For we must not rest.
2224;; There are more tokens to overload, more shoehorn, more methodologies. But
2225;; more is a plus! [Ha ha ha.] And more means plus! [Ho ho ho.] The future
1c626aaf 2226;; is C++! [Ohhh!] The Third Millennium Award... Will be ours! [Roar.]
c1f2ffc8 2227
2e6a15fc
SM
2228(defconst c-font-lock-keywords-1 nil
2229 "Subdued level highlighting for C mode.")
9bfbb130 2230
2e6a15fc
SM
2231(defconst c-font-lock-keywords-2 nil
2232 "Medium level highlighting for C mode.
2233See also `c-font-lock-extra-types'.")
1bd50840 2234
2e6a15fc
SM
2235(defconst c-font-lock-keywords-3 nil
2236 "Gaudy level highlighting for C mode.
2237See also `c-font-lock-extra-types'.")
9bfbb130 2238
2d63aa67 2239(let* ((c-keywords
ac6e572c
SM
2240 (eval-when-compile
2241 (regexp-opt '("break" "continue" "do" "else" "for" "if" "return"
4fffc071
SM
2242 "switch" "while" "sizeof"
2243 ;; Type related, but we don't do anything special.
2244 "typedef" "extern" "auto" "register" "static"
2245 "volatile" "const") t)))
2246 (c-type-specs
2247 (eval-when-compile
2248 (regexp-opt '("enum" "struct" "union") t)))
2249 (c-type-specs-depth
2250 (regexp-opt-depth c-type-specs))
2251 (c-type-names
2d63aa67
SM
2252 `(mapconcat 'identity
2253 (cons
ac6e572c
SM
2254 (,@ (eval-when-compile
2255 (regexp-opt
4fffc071
SM
2256 '("char" "short" "int" "long" "signed" "unsigned"
2257 "float" "double" "void"))))
2d63aa67
SM
2258 c-font-lock-extra-types)
2259 "\\|"))
4fffc071
SM
2260 (c-type-names-depth
2261 `(regexp-opt-depth (,@ c-type-names)))
2d63aa67 2262 )
f60f18ae
KH
2263 (setq c-font-lock-keywords-1
2264 (list
f60f18ae 2265 ;;
9bfbb130 2266 ;; These are all anchored at the beginning of line for speed.
2e6a15fc 2267 ;; Note that `c++-font-lock-keywords-1' depends on `c-font-lock-keywords-1'.
9bfbb130
SM
2268 ;;
2269 ;; Fontify function name definitions (GNU style; without type on line).
c1f2ffc8
SM
2270 '("^\\(\\sw+\\)[ \t]*(" 1 font-lock-function-name-face)
2271 ;;
2272 ;; Fontify error directives.
2273 '("^#[ \t]*error[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend)
9bfbb130
SM
2274 ;;
2275 ;; Fontify filenames in #include <...> preprocessor directives as strings.
9c8de95c 2276 '("^#[ \t]*\\(import\\|include\\)[ \t]*\\(<[^>\"\n]*>?\\)"
2e6a15fc 2277 2 font-lock-string-face)
f60f18ae 2278 ;;
a1eb1cf1 2279 ;; Fontify function macro names.
7d7d915a 2280 '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
f60f18ae 2281 ;;
5aa29679
RS
2282 ;; Fontify symbol names in #elif or #if ... defined preprocessor directives.
2283 '("^#[ \t]*\\(elif\\|if\\)\\>"
9bfbb130 2284 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
b9dd672d 2285 (1 font-lock-builtin-face) (2 font-lock-variable-name-face nil t)))
9bfbb130 2286 ;;
a1eb1cf1 2287 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
b9dd672d
SM
2288 '("^#[ \t]*\\(\\sw+\\)\\>[ \t!]*\\(\\sw+\\)?"
2289 (1 font-lock-builtin-face) (2 font-lock-variable-name-face nil t))
f60f18ae
KH
2290 ))
2291
2292 (setq c-font-lock-keywords-2
2293 (append c-font-lock-keywords-1
030f4a35 2294 (list
030f4a35 2295 ;;
9bfbb130 2296 ;; Simple regexps for speed.
b89e1134 2297 ;;
4fffc071 2298 ;; Fontify all type names.
c1f2ffc8 2299 `(eval .
4fffc071 2300 (cons (concat "\\<\\(" (,@ c-type-names) "\\)\\>") 'font-lock-type-face))
030f4a35 2301 ;;
b89e1134 2302 ;; Fontify all builtin keywords (except case, default and goto; see below).
4fffc071 2303 (concat "\\<\\(" c-keywords "\\|" c-type-specs "\\)\\>")
030f4a35 2304 ;;
9bfbb130 2305 ;; Fontify case/goto keywords and targets, and case default/goto tags.
56fcbd7e 2306 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
8acf2292 2307 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
2e6a15fc
SM
2308 ;; Anders Lindgren <andersl@csd.uu.se> points out that it is quicker to use
2309 ;; MATCH-ANCHORED to effectively anchor the regexp on the left.
e0e277ff 2310 ;; This must come after the one for keywords and targets.
2e6a15fc
SM
2311 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:"
2312 (beginning-of-line) (end-of-line)
8acf2292 2313 (1 font-lock-constant-face)))
f60f18ae 2314 )))
1bd50840 2315
9bfbb130
SM
2316 (setq c-font-lock-keywords-3
2317 (append c-font-lock-keywords-2
2318 ;;
2319 ;; More complicated regexps for more complete highlighting for types.
2320 ;; We still have to fontify type specifiers individually, as C is so hairy.
2321 (list
2322 ;;
4fffc071 2323 ;; Fontify all storage types, plus their items.
c1f2ffc8 2324 `(eval .
4fffc071 2325 (list (concat "\\<\\(" (,@ c-type-names) "\\)\\>"
c1f2ffc8
SM
2326 "\\([ \t*&]+\\sw+\\>\\)*")
2327 ;; Fontify each declaration item.
2d63aa67
SM
2328 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2329 ;; Start with point after all type specifiers.
4fffc071
SM
2330 (list 'goto-char (list 'or
2331 (list 'match-beginning
2332 (+ (,@ c-type-names-depth) 2))
2d63aa67
SM
2333 '(match-end 1)))
2334 ;; Finish with point after first type specifier.
2335 '(goto-char (match-end 1))
2336 ;; Fontify as a variable or function name.
2337 '(1 (if (match-beginning 2)
2338 font-lock-function-name-face
2339 font-lock-variable-name-face)))))
9bfbb130 2340 ;;
4fffc071
SM
2341 ;; Fontify all storage specs and types, plus their items.
2342 `(eval .
2343 (list (concat "\\<\\(" (,@ c-type-specs) "\\)\\>"
2344 "[ \t]*\\(\\sw+\\)?")
2345 (list 1 'font-lock-keyword-face)
2346 (list (+ (,@ c-type-specs-depth) 2) 'font-lock-type-face nil t)
2347 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2348 nil nil
2349 ;; Fontify as a variable or function name.
2350 '(1 (if (match-beginning 2)
2351 font-lock-function-name-face
2352 font-lock-variable-name-face) nil t))))
2353 ;;
9bfbb130
SM
2354 ;; Fontify structures, or typedef names, plus their items.
2355 '("\\(}\\)[ \t*]*\\sw"
c1f2ffc8 2356 (font-lock-match-c-style-declaration-item-and-skip-to-next
9bfbb130 2357 (goto-char (match-end 1)) nil
4fffc071 2358 (1 font-lock-type-face)))
9bfbb130
SM
2359 ;;
2360 ;; Fontify anything at beginning of line as a declaration or definition.
2361 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2362 (1 font-lock-type-face)
c1f2ffc8 2363 (font-lock-match-c-style-declaration-item-and-skip-to-next
9bfbb130 2364 (goto-char (or (match-beginning 2) (match-end 1))) nil
c1f2ffc8 2365 (1 (if (match-beginning 2)
9bfbb130
SM
2366 font-lock-function-name-face
2367 font-lock-variable-name-face))))
2368 )))
2e6a15fc
SM
2369 )
2370
2371(defvar c-font-lock-keywords c-font-lock-keywords-1
2372 "Default expressions to highlight in C mode.
2373See also `c-font-lock-extra-types'.")
2374\f
2375;;; C++.
2376
2377(defconst c++-font-lock-keywords-1 nil
2378 "Subdued level highlighting for C++ mode.")
2379
2380(defconst c++-font-lock-keywords-2 nil
2381 "Medium level highlighting for C++ mode.
2382See also `c++-font-lock-extra-types'.")
2383
2384(defconst c++-font-lock-keywords-3 nil
2385 "Gaudy level highlighting for C++ mode.
2386See also `c++-font-lock-extra-types'.")
9bfbb130 2387
c1f2ffc8
SM
2388(defun font-lock-match-c++-style-declaration-item-and-skip-to-next (limit)
2389 ;; Regexp matches after point: word<word>::word (
2390 ;; ^^^^ ^^^^ ^^^^ ^
2391 ;; Where the match subexpressions are: 1 3 5 6
2392 ;;
2393 ;; Item is delimited by (match-beginning 1) and (match-end 1).
2394 ;; If (match-beginning 3) is non-nil, that part of the item incloses a `<>'.
2395 ;; If (match-beginning 5) is non-nil, that part of the item follows a `::'.
2396 ;; If (match-beginning 6) is non-nil, the item is followed by a `('.
2397 (when (looking-at (eval-when-compile
55015061
SM
2398 (concat
2399 ;; Skip any leading whitespace.
2400 "[ \t*&]*"
2401 ;; This is `c++-type-spec' from below. (Hint hint!)
2402 "\\(\\sw+\\)" ; The instance?
ac6e572c 2403 "\\([ \t]*<\\([^>\n]+\\)[ \t*&]*>\\)?" ; Or template?
844a6a46 2404 "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)*" ; Or member?
55015061
SM
2405 ;; Match any trailing parenthesis.
2406 "[ \t]*\\((\\)?")))
c1f2ffc8
SM
2407 (save-match-data
2408 (condition-case nil
2409 (save-restriction
2410 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
2411 (narrow-to-region (point-min) limit)
2412 (goto-char (match-end 1))
2413 ;; Move over any item value, etc., to the next item.
2414 (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)"))
2415 (goto-char (or (scan-sexps (point) 1) (point-max))))
2416 (goto-char (match-end 2)))
2417 (error t)))))
2418
2419(let* ((c++-keywords
ac6e572c
SM
2420 (eval-when-compile
2421 (regexp-opt
2422 '("break" "continue" "do" "else" "for" "if" "return" "switch"
e0e277ff 2423 "while" "asm" "catch" "delete" "new" "sizeof" "this" "throw" "try"
4fffc071
SM
2424 ;; Branko Cibej <branko.cibej@hermes.si> says this is new.
2425 "export"
2426 ;; Mark Mitchell <mmitchell@usa.net> says these are new.
2427 "mutable" "explicit"
2428 ;; Alain Picard <ap@abelard.apana.org.au> suggests treating these
2429 ;; as keywords not types.
2430 "typedef" "template"
2431 "extern" "auto" "register" "const" "volatile" "static"
2432 "inline" "friend" "virtual") t)))
c1f2ffc8 2433 (c++-operators
23a0e11b
SM
2434 (eval-when-compile
2435 (regexp-opt
2436 ;; Taken from Stroustrup, minus keywords otherwise fontified.
2437 '("+" "-" "*" "/" "%" "^" "&" "|" "~" "!" "=" "<" ">" "+=" "-="
2438 "*=" "/=" "%=" "^=" "&=" "|=" "<<" ">>" ">>=" "<<=" "==" "!="
2439 "<=" ">=" "&&" "||" "++" "--" "->*" "," "->" "[]" "()"))))
4fffc071
SM
2440 (c++-type-specs
2441 (eval-when-compile
2442 (regexp-opt
2443 '("class" "public" "private" "protected" "typename"
2444 "struct" "union" "enum" "namespace" "using"
2445 ;; Eric Hopper <hopper@omnifarious.mn.org> says these are new.
2446 "static_cast" "dynamic_cast" "const_cast" "reinterpret_cast") t)))
2447 (c++-type-specs-depth
2448 (regexp-opt-depth c++-type-specs))
2449 (c++-type-names
c1f2ffc8
SM
2450 `(mapconcat 'identity
2451 (cons
ac6e572c
SM
2452 (,@ (eval-when-compile
2453 (regexp-opt
4fffc071
SM
2454 '("signed" "unsigned" "short" "long"
2455 "int" "char" "float" "double" "void"
2456 "bool" "complex"))))
c1f2ffc8
SM
2457 c++-font-lock-extra-types)
2458 "\\|"))
4fffc071 2459 (c++-type-names-depth `(regexp-opt-depth (,@ c++-type-names)))
55015061
SM
2460 ;;
2461 ;; A brave attempt to match templates following a type and/or match
2462 ;; class membership. See and sync the above function
2463 ;; `font-lock-match-c++-style-declaration-item-and-skip-to-next'.
ac6e572c 2464 (c++-type-suffix (concat "\\([ \t]*<\\([^>\n]+\\)[ \t*&]*>\\)?"
844a6a46 2465 "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)*"))
4fffc071 2466 (c++-type-suffix-depth (regexp-opt-depth c++-type-suffix))
55015061
SM
2467 ;; If the string is a type, it may be followed by the cruft above.
2468 (c++-type-spec (concat "\\(\\sw+\\)\\>" c++-type-suffix))
4fffc071 2469 (c++-type-spec-depth (regexp-opt-depth c++-type-spec))
55015061
SM
2470 ;;
2471 ;; Parenthesis depth of user-defined types not forgetting their cruft.
ac6e572c 2472 (c++-type-depth `(regexp-opt-depth
4fffc071 2473 (concat (,@ c++-type-names) (,@ c++-type-suffix))))
2d63aa67 2474 )
9bfbb130
SM
2475 (setq c++-font-lock-keywords-1
2476 (append
2477 ;;
2478 ;; The list `c-font-lock-keywords-1' less that for function names.
2479 (cdr c-font-lock-keywords-1)
9bfbb130 2480 (list
2e6a15fc 2481 ;;
c1f2ffc8
SM
2482 ;; Fontify function name definitions, possibly incorporating class names.
2483 (list (concat "^" c++-type-spec "[ \t]*(")
2484 '(1 (if (or (match-beginning 2) (match-beginning 4))
2485 font-lock-type-face
2486 font-lock-function-name-face))
4fffc071 2487 '(3 font-lock-type-face nil t)
c1f2ffc8 2488 '(5 font-lock-function-name-face nil t))
9bfbb130
SM
2489 )))
2490
1bd50840 2491 (setq c++-font-lock-keywords-2
b89e1134 2492 (append c++-font-lock-keywords-1
a1eb1cf1 2493 (list
9bfbb130
SM
2494 ;;
2495 ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading.
c1f2ffc8 2496 `(eval .
4fffc071 2497 (cons (concat "\\<\\(" (,@ c++-type-names) "\\)\\>")
c1f2ffc8 2498 'font-lock-type-face))
9bfbb130 2499 ;;
c1f2ffc8
SM
2500 ;; Fontify operator overloading.
2501 (list (concat "\\<\\(operator\\)\\>[ \t]*\\(" c++-operators "\\)?")
2502 '(1 font-lock-keyword-face)
2503 '(2 font-lock-builtin-face nil t))
9bfbb130
SM
2504 ;;
2505 ;; Fontify case/goto keywords and targets, and case default/goto tags.
56fcbd7e 2506 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
8acf2292 2507 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
e0e277ff 2508 ;; This must come after the one for keywords and targets.
2e6a15fc
SM
2509 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:\\($\\|[^:]\\)"
2510 (beginning-of-line) (end-of-line)
8acf2292 2511 (1 font-lock-constant-face)))
9bfbb130
SM
2512 ;;
2513 ;; Fontify other builtin keywords.
4fffc071 2514 (concat "\\<\\(" c++-keywords "\\|" c++-type-specs "\\)\\>")
2e6a15fc
SM
2515 ;;
2516 ;; Eric Hopper <hopper@omnifarious.mn.org> says `true' and `false' are new.
8acf2292 2517 '("\\<\\(false\\|true\\)\\>" . font-lock-constant-face)
9bfbb130
SM
2518 )))
2519
2520 (setq c++-font-lock-keywords-3
2521 (append c++-font-lock-keywords-2
2522 ;;
2523 ;; More complicated regexps for more complete highlighting for types.
2524 (list
2525 ;;
2526 ;; Fontify all storage classes and type specifiers, plus their items.
c1f2ffc8 2527 `(eval .
4fffc071 2528 (list (concat "\\<\\(" (,@ c++-type-names) "\\)\\>" (,@ c++-type-suffix)
c1f2ffc8 2529 "\\([ \t*&]+" (,@ c++-type-spec) "\\)*")
4fffc071
SM
2530 ;; The name of any template type.
2531 (list (+ (,@ c++-type-names-depth) 3) 'font-lock-type-face nil t)
c1f2ffc8 2532 ;; Fontify each declaration item.
2d63aa67
SM
2533 (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next
2534 ;; Start with point after all type specifiers.
2535 (list 'goto-char (list 'or (list 'match-beginning
2536 (+ (,@ c++-type-depth) 2))
2537 '(match-end 1)))
2538 ;; Finish with point after first type specifier.
2539 '(goto-char (match-end 1))
2540 ;; Fontify as a variable or function name.
2541 '(1 (cond ((or (match-beginning 2) (match-beginning 4))
2542 font-lock-type-face)
2543 ((match-beginning 6) font-lock-function-name-face)
2544 (t font-lock-variable-name-face)))
4fffc071 2545 '(3 font-lock-type-face nil t)
2d63aa67
SM
2546 '(5 (if (match-beginning 6)
2547 font-lock-function-name-face
2548 font-lock-variable-name-face) nil t))))
9bfbb130 2549 ;;
4fffc071
SM
2550 ;; Fontify all storage specs and types, plus their items.
2551 `(eval .
2552 (list (concat "\\<" (,@ c++-type-specs) "\\>" (,@ c++-type-suffix)
2553 "[ \t]*\\(" (,@ c++-type-spec) "\\)?")
2554 ;; The name of any template type.
2555 (list (+ (,@ c++-type-specs-depth) 2) 'font-lock-type-face nil t)
2556 ;; The name of any type.
2557 (list (+ (,@ c++-type-specs-depth) (,@ c++-type-suffix-depth) 2)
2558 'font-lock-type-face nil t)
2559 ;; Fontify each declaration item.
2560 (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next
2561 ;; Start with point after all type specifiers.
2562 nil
2563 ;; Finish with point after first type specifier.
2564 nil
2565 ;; Fontify as a variable or function name.
2566 '(1 (cond ((or (match-beginning 2) (match-beginning 4))
2567 font-lock-type-face)
2568 ((match-beginning 6) font-lock-function-name-face)
2569 (t font-lock-variable-name-face)))
2570 '(3 font-lock-type-face nil t)
2571 '(5 (if (match-beginning 6)
2572 font-lock-function-name-face
2573 font-lock-variable-name-face) nil t))
2574 ))
2575 ;;
9bfbb130
SM
2576 ;; Fontify structures, or typedef names, plus their items.
2577 '("\\(}\\)[ \t*]*\\sw"
2578 (font-lock-match-c++-style-declaration-item-and-skip-to-next
2579 (goto-char (match-end 1)) nil
4fffc071 2580 (1 font-lock-type-face)))
9bfbb130
SM
2581 ;;
2582 ;; Fontify anything at beginning of line as a declaration or definition.
c1f2ffc8
SM
2583 (list (concat "^\\(" c++-type-spec "[ \t*&]*\\)+")
2584 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
2585 (goto-char (match-beginning 1))
2586 (goto-char (match-end 1))
2587 (1 (cond ((or (match-beginning 2) (match-beginning 4))
2588 font-lock-type-face)
2589 ((match-beginning 6) font-lock-function-name-face)
2590 (t font-lock-variable-name-face)))
4fffc071 2591 (3 font-lock-type-face nil t)
c1f2ffc8
SM
2592 (5 (if (match-beginning 6)
2593 font-lock-function-name-face
2594 font-lock-variable-name-face) nil t)))
9bfbb130 2595 )))
f60f18ae 2596 )
030f4a35 2597
fb512de9 2598(defvar c++-font-lock-keywords c++-font-lock-keywords-1
2e6a15fc
SM
2599 "Default expressions to highlight in C++ mode.
2600See also `c++-font-lock-extra-types'.")
2601\f
2602;;; Objective-C.
2603
2604(defconst objc-font-lock-keywords-1 nil
2605 "Subdued level highlighting for Objective-C mode.")
2606
2607(defconst objc-font-lock-keywords-2 nil
2608 "Medium level highlighting for Objective-C mode.
2609See also `objc-font-lock-extra-types'.")
2610
2611(defconst objc-font-lock-keywords-3 nil
2612 "Gaudy level highlighting for Objective-C mode.
2613See also `objc-font-lock-extra-types'.")
2614
2615;; Regexps written with help from Stephen Peters <speters@us.oracle.com> and
2616;; Jacques Duthen Prestataire <duthen@cegelec-red.fr>.
2d63aa67 2617(let* ((objc-keywords
ac6e572c
SM
2618 (eval-when-compile
2619 (regexp-opt '("break" "continue" "do" "else" "for" "if" "return"
4fffc071
SM
2620 "switch" "while" "sizeof" "self" "super"
2621 "typedef" "auto" "extern" "static"
2622 "volatile" "const") t)))
2623 (objc-type-specs
2624 (eval-when-compile
2625 (regexp-opt
2626 '("register" "struct" "union" "enum"
2627 "oneway" "in" "out" "inout" "bycopy" "byref") t)))
2628 (objc-type-specs-depth
2629 (regexp-opt-depth objc-type-specs))
2630 (objc-type-names
2d63aa67
SM
2631 `(mapconcat 'identity
2632 (cons
ac6e572c
SM
2633 (,@ (eval-when-compile
2634 (regexp-opt
4fffc071
SM
2635 '("signed" "unsigned" "short" "long"
2636 "int" "char" "float" "double" "void"
2637 "id"))))
2d63aa67
SM
2638 objc-font-lock-extra-types)
2639 "\\|"))
4fffc071
SM
2640 (objc-type-names-depth
2641 `(regexp-opt-depth (,@ objc-type-names)))
2d63aa67 2642 )
2e6a15fc
SM
2643 (setq objc-font-lock-keywords-1
2644 (append
2645 ;;
2646 ;; The list `c-font-lock-keywords-1' less that for function names.
2647 (cdr c-font-lock-keywords-1)
2648 (list
2649 ;;
2650 ;; Fontify compiler directives.
c1f2ffc8
SM
2651 '("@\\(\\sw+\\)\\>"
2652 (1 font-lock-keyword-face)
4fffc071
SM
2653 ("\\=[ \t:<,]*\\(\\sw+\\)" nil nil
2654 (1 font-lock-type-face)))
2e6a15fc
SM
2655 ;;
2656 ;; Fontify method names and arguments. Oh Lordy!
2657 ;; First, on the same line as the function declaration.
4fffc071
SM
2658 '("^[+-][ \t]*\\(PRIVATE\\>\\)?[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2659 (1 font-lock-keyword-face nil t)
2660 (3 font-lock-function-name-face)
2661 ("\\=[ \t]*\\(\\sw+\\)?:[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2e6a15fc
SM
2662 nil nil
2663 (1 font-lock-function-name-face nil t)
4fffc071 2664 (3 font-lock-variable-name-face)))
2e6a15fc 2665 ;; Second, on lines following the function declaration.
4fffc071 2666 '(":" ("^[ \t]*\\(\\sw+\\)?:[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2e6a15fc
SM
2667 (beginning-of-line) (end-of-line)
2668 (1 font-lock-function-name-face nil t)
4fffc071 2669 (3 font-lock-variable-name-face)))
2e6a15fc 2670 )))
030f4a35 2671
2e6a15fc
SM
2672 (setq objc-font-lock-keywords-2
2673 (append objc-font-lock-keywords-1
2674 (list
2675 ;;
2676 ;; Simple regexps for speed.
2677 ;;
2678 ;; Fontify all type specifiers.
c1f2ffc8 2679 `(eval .
4fffc071 2680 (cons (concat "\\<\\(" (,@ objc-type-names) "\\)\\>")
c1f2ffc8 2681 'font-lock-type-face))
2e6a15fc
SM
2682 ;;
2683 ;; Fontify all builtin keywords (except case, default and goto; see below).
4fffc071 2684 (concat "\\<\\(" objc-keywords "\\|" objc-type-specs "\\)\\>")
2e6a15fc
SM
2685 ;;
2686 ;; Fontify case/goto keywords and targets, and case default/goto tags.
56fcbd7e 2687 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
8acf2292 2688 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
2e6a15fc 2689 ;; Fontify tags iff sole statement on line, otherwise we detect selectors.
e0e277ff 2690 ;; This must come after the one for keywords and targets.
2e6a15fc
SM
2691 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2692 (beginning-of-line) (end-of-line)
8acf2292 2693 (1 font-lock-constant-face)))
2e6a15fc
SM
2694 ;;
2695 ;; Fontify null object pointers.
8acf2292 2696 '("\\<[Nn]il\\>" . font-lock-constant-face)
2e6a15fc 2697 )))
d46c21ec 2698
2e6a15fc
SM
2699 (setq objc-font-lock-keywords-3
2700 (append objc-font-lock-keywords-2
2701 ;;
2702 ;; More complicated regexps for more complete highlighting for types.
2703 ;; We still have to fontify type specifiers individually, as C is so hairy.
2704 (list
2705 ;;
2706 ;; Fontify all storage classes and type specifiers, plus their items.
c1f2ffc8 2707 `(eval .
4fffc071 2708 (list (concat "\\<\\(" (,@ objc-type-names) "\\)\\>"
c1f2ffc8
SM
2709 "\\([ \t*&]+\\sw+\\>\\)*")
2710 ;; Fontify each declaration item.
2d63aa67
SM
2711 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2712 ;; Start with point after all type specifiers.
4fffc071
SM
2713 (list 'goto-char
2714 (list 'or (list 'match-beginning
2715 (+ (,@ objc-type-names-depth) 2))
2716 '(match-end 1)))
2d63aa67
SM
2717 ;; Finish with point after first type specifier.
2718 '(goto-char (match-end 1))
2719 ;; Fontify as a variable or function name.
2720 '(1 (if (match-beginning 2)
2721 font-lock-function-name-face
2722 font-lock-variable-name-face)))))
2e6a15fc 2723 ;;
4fffc071
SM
2724 ;; Fontify all storage specs and types, plus their items.
2725 `(eval .
2726 (list (concat "\\<\\(" (,@ objc-type-specs) "[ \t]*\\)+\\>"
2727 "[ \t]*\\(\\sw+\\)?")
2728 ;; The name of any type.
2729 (list (+ (,@ objc-type-specs-depth) 2) 'font-lock-type-face nil t)
2730 ;; Fontify each declaration item.
2731 (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next
2732 nil nil
2733 ;; Fontify as a variable or function name.
2734 '(1 (if (match-beginning 2)
2735 font-lock-function-name-face
2736 font-lock-variable-name-face)))
2737 ))
2738 ;;
2e6a15fc
SM
2739 ;; Fontify structures, or typedef names, plus their items.
2740 '("\\(}\\)[ \t*]*\\sw"
c1f2ffc8 2741 (font-lock-match-c-style-declaration-item-and-skip-to-next
2e6a15fc 2742 (goto-char (match-end 1)) nil
4fffc071 2743 (1 font-lock-type-face)))
2e6a15fc
SM
2744 ;;
2745 ;; Fontify anything at beginning of line as a declaration or definition.
2746 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2747 (1 font-lock-type-face)
c1f2ffc8 2748 (font-lock-match-c-style-declaration-item-and-skip-to-next
2e6a15fc 2749 (goto-char (or (match-beginning 2) (match-end 1))) nil
c1f2ffc8 2750 (1 (if (match-beginning 2)
2e6a15fc
SM
2751 font-lock-function-name-face
2752 font-lock-variable-name-face))))
2753 )))
2754 )
2755
2756(defvar objc-font-lock-keywords objc-font-lock-keywords-1
2757 "Default expressions to highlight in Objective-C mode.
2758See also `objc-font-lock-extra-types'.")
2759\f
2760;;; Java.
2761
2762(defconst java-font-lock-keywords-1 nil
2763 "Subdued level highlighting for Java mode.")
2764
2765(defconst java-font-lock-keywords-2 nil
2766 "Medium level highlighting for Java mode.
2767See also `java-font-lock-extra-types'.")
2768
2769(defconst java-font-lock-keywords-3 nil
2770 "Gaudy level highlighting for Java mode.
2771See also `java-font-lock-extra-types'.")
2772
2773;; Regexps written with help from Fred White <fwhite@bbn.com> and
2774;; Anders Lindgren <andersl@csd.uu.se>.
2d63aa67 2775(let* ((java-keywords
ac6e572c
SM
2776 (eval-when-compile
2777 (regexp-opt
2778 '("catch" "do" "else" "super" "this" "finally" "for" "if"
2779 ;; Anders Lindgren <andersl@csd.uu.se> says these have gone.
2780 ;; "cast" "byvalue" "future" "generic" "operator" "var"
2781 ;; "inner" "outer" "rest"
2782 "interface" "return" "switch" "throw" "try" "while") t)))
2d63aa67
SM
2783 ;;
2784 ;; These are immediately followed by an object name.
2785 (java-minor-types
ac6e572c
SM
2786 (eval-when-compile
2787 (regexp-opt '("boolean" "char" "byte" "short" "int" "long"
2788 "float" "double" "void"))))
2d63aa67
SM
2789 ;;
2790 ;; These are eventually followed by an object name.
2791 (java-major-types
ac6e572c
SM
2792 (eval-when-compile
2793 (regexp-opt
2794 '("abstract" "const" "final" "synchronized" "transient" "static"
2795 ;; Anders Lindgren <andersl@csd.uu.se> says this has gone.
2796 ;; "threadsafe"
2797 "volatile" "public" "private" "protected" "native"))))
2d63aa67
SM
2798 ;;
2799 ;; Random types immediately followed by an object name.
2800 (java-other-types
2801 '(mapconcat 'identity (cons "\\sw+\\.\\sw+" java-font-lock-extra-types)
2802 "\\|"))
4fffc071
SM
2803 (java-other-depth
2804 `(regexp-opt-depth (,@ java-other-types)))
ac6e572c 2805 )
2e6a15fc
SM
2806 (setq java-font-lock-keywords-1
2807 (list
2808 ;;
2809 ;; Fontify class names.
2810 '("\\<\\(class\\)\\>[ \t]*\\(\\sw+\\)?"
2811 (1 font-lock-type-face) (2 font-lock-function-name-face nil t))
2812 ;;
2813 ;; Fontify package names in import directives.
2814 '("\\<\\(import\\|package\\)\\>[ \t]*\\(\\sw+\\)?"
8acf2292 2815 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
2e6a15fc
SM
2816 ))
2817
2818 (setq java-font-lock-keywords-2
2819 (append java-font-lock-keywords-1
2820 (list
2821 ;;
2822 ;; Fontify all builtin type specifiers.
b4b989a0 2823 (cons (concat "\\<\\(" java-minor-types "\\)\\>") 'font-lock-type-face)
2e6a15fc
SM
2824 ;;
2825 ;; Fontify all builtin keywords (except below).
4fffc071 2826 (concat "\\<\\(" java-keywords "\\|" java-major-types "\\)\\>")
2e6a15fc
SM
2827 ;;
2828 ;; Fontify keywords and targets, and case default/goto tags.
56fcbd7e 2829 (list "\\<\\(break\\|case\\|continue\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
8acf2292 2830 '(1 font-lock-keyword-face) '(2 font-lock-constant-face nil t))
e0e277ff 2831 ;; This must come after the one for keywords and targets.
2e6a15fc
SM
2832 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:"
2833 (beginning-of-line) (end-of-line)
8acf2292 2834 (1 font-lock-constant-face)))
2e6a15fc
SM
2835 ;;
2836 ;; Fontify keywords and types; the first can be followed by a type list.
2837 (list (concat "\\<\\("
2838 "implements\\|throws\\|"
2839 "\\(extends\\|instanceof\\|new\\)"
2840 "\\)\\>[ \t]*\\(\\sw+\\)?")
2841 '(1 font-lock-keyword-face) '(3 font-lock-type-face nil t)
2842 '("\\=[ \t]*,[ \t]*\\(\\sw+\\)"
2843 (if (match-beginning 2) (goto-char (match-end 2))) nil
2844 (1 font-lock-type-face)))
2845 ;;
2846 ;; Fontify all constants.
8acf2292 2847 '("\\<\\(false\\|null\\|true\\)\\>" . font-lock-constant-face)
2e6a15fc
SM
2848 ;;
2849 ;; Javadoc tags within comments.
2850 '("@\\(author\\|exception\\|return\\|see\\|version\\)\\>"
8acf2292 2851 (1 font-lock-constant-face prepend))
2e6a15fc 2852 '("@\\(param\\)\\>[ \t]*\\(\\sw+\\)?"
8acf2292 2853 (1 font-lock-constant-face prepend)
2e6a15fc
SM
2854 (2 font-lock-variable-name-face prepend t))
2855 )))
2856
2857 (setq java-font-lock-keywords-3
2858 (append java-font-lock-keywords-2
2859 ;;
2860 ;; More complicated regexps for more complete highlighting for types.
2861 ;; We still have to fontify type specifiers individually, as Java is hairy.
2862 (list
2863 ;;
2864 ;; Fontify random types in casts.
c1f2ffc8
SM
2865 `(eval .
2866 (list (concat "(\\(" (,@ java-other-types) "\\))"
2867 "[ \t]*\\(\\sw\\|[\"\(]\\)")
2868 ;; Fontify the type name.
2869 '(1 font-lock-type-face)))
2e6a15fc
SM
2870 ;;
2871 ;; Fontify random types immediately followed by an item or items.
c1f2ffc8
SM
2872 `(eval .
2873 (list (concat "\\<\\(" (,@ java-other-types) "\\)\\>"
2874 "\\([ \t]*\\[[ \t]*\\]\\)*"
2875 "[ \t]*\\sw")
2876 ;; Fontify the type name.
2877 '(1 font-lock-type-face)))
2878 `(eval .
2879 (list (concat "\\<\\(" (,@ java-other-types) "\\)\\>"
2880 "\\([ \t]*\\[[ \t]*\\]\\)*"
2881 "\\([ \t]*\\sw\\)")
2882 ;; Fontify each declaration item.
2d63aa67
SM
2883 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2884 ;; Start and finish with point after the type specifier.
2885 (list 'goto-char (list 'match-beginning
2886 (+ (,@ java-other-depth) 3)))
2887 (list 'goto-char (list 'match-beginning
2888 (+ (,@ java-other-depth) 3)))
2889 ;; Fontify as a variable or function name.
2890 '(1 (if (match-beginning 2)
2891 font-lock-function-name-face
2892 font-lock-variable-name-face)))))
2e6a15fc
SM
2893 ;;
2894 ;; Fontify those that are immediately followed by an item or items.
2895 (list (concat "\\<\\(" java-minor-types "\\)\\>"
2896 "\\([ \t]*\\[[ \t]*\\]\\)*")
2897 ;; Fontify each declaration item.
c1f2ffc8 2898 '(font-lock-match-c-style-declaration-item-and-skip-to-next
2e6a15fc
SM
2899 ;; Start and finish with point after the type specifier.
2900 nil (goto-char (match-end 0))
2901 ;; Fontify as a variable or function name.
c1f2ffc8 2902 (1 (if (match-beginning 2)
2e6a15fc
SM
2903 font-lock-function-name-face
2904 font-lock-variable-name-face))))
2905 ;;
2906 ;; Fontify those that are eventually followed by an item or items.
2907 (list (concat "\\<\\(" java-major-types "\\)\\>"
2908 "\\([ \t]+\\sw+\\>"
2909 "\\([ \t]*\\[[ \t]*\\]\\)*"
2910 "\\)*")
2911 ;; Fontify each declaration item.
c1f2ffc8 2912 '(font-lock-match-c-style-declaration-item-and-skip-to-next
2e6a15fc 2913 ;; Start with point after all type specifiers.
2d63aa67 2914 (goto-char (or (match-beginning 5) (match-end 1)))
2e6a15fc
SM
2915 ;; Finish with point after first type specifier.
2916 (goto-char (match-end 1))
2917 ;; Fontify as a variable or function name.
c1f2ffc8 2918 (1 (if (match-beginning 2)
2e6a15fc
SM
2919 font-lock-function-name-face
2920 font-lock-variable-name-face))))
2921 )))
2922 )
2923
2924(defvar java-font-lock-keywords java-font-lock-keywords-1
2925 "Default expressions to highlight in Java mode.
2926See also `java-font-lock-extra-types'.")
d46c21ec 2927\f
a1eb1cf1
RS
2928;; Install ourselves:
2929
5aa29679 2930(unless (assq 'font-lock-mode minor-mode-alist)
74167b21 2931 (push '(font-lock-mode nil) minor-mode-alist))
a1eb1cf1
RS
2932
2933;; Provide ourselves:
8f261d40 2934
030f4a35
RS
2935(provide 'font-lock)
2936
2937;;; font-lock.el ends here