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