Fix previous change.
[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
9bfbb130 5;; Author: jwz, then rms, then sm <simon@gnu.ai.mit.edu>
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
63;; regexp (i.e., keywords and everything else) passes. The syntactic pass
64;; involves a syntax table and a syntax parsing function to determine the
65;; context of different parts of a region of text. It is necessary because
66;; generally strings and/or comments can span lines, and so the context of a
67;; given region is not necessarily apparent from the content of that region.
68;; Because the regexp pass only works within a given region, it is not
69;; generally appropriate for syntactic fontification. The regexp pass involves
70;; searching for given regexps (or calling given functions) within the given
71;; region. For each match of the regexp (or non-nil value of the called
72;; function), `face' text properties are added appropriately.
73
c1f2ffc8
SM
74;;; How Font Lock mode supports modes or is supported by modes:
75
76;; Modes that support Font Lock mode do so by defining one or more variables
77;; whose values specify the fontification. Font Lock mode knows of these
78;; variable names from (a) the buffer local variable `font-lock-defaults', if
79;; non-nil, or (b) the global variable `font-lock-defaults-alist', if the major
80;; mode has an entry. (Font Lock mode is set up via (a) where a mode's
81;; patterns are distributed with the mode's package library, and (b) where a
82;; mode's patterns are distributed with font-lock.el itself. An example of (a)
83;; is Pascal mode, an example of (b) is Lisp mode. Normally, the mechanism is
84;; (a); (b) is used where it is not clear which package library should contain
85;; the pattern definitions.) Font Lock mode chooses which variable to use for
86;; fontification based on `font-lock-maximum-decoration'.
56fcbd7e
SM
87;;
88;; Font Lock mode fontification behaviour can be modified in a number of ways.
89;; See the below comments and the comments distributed throughout this file.
c1f2ffc8
SM
90
91;;; Constructing patterns:
92
98f84f52
SM
93;; See the documentation for the variable `font-lock-keywords'.
94;;
95;; Nasty regexps of the form "bar\\(\\|lo\\)\\|f\\(oo\\|u\\(\\|bar\\)\\)\\|lo"
96;; are made thusly: (make-regexp '("foo" "fu" "fubar" "bar" "barlo" "lo")) for
97;; efficiency. See /pub/gnu/emacs/elisp-archive/functions/make-regexp.el.Z on
56fcbd7e 98;; archive.cis.ohio-state.edu for this and other functions not just by sm.
98f84f52 99
c1f2ffc8
SM
100;;; Adding patterns for modes that already support Font Lock:
101
102;; Though Font Lock highlighting patterns already exist for many modes, it's
103;; likely there's something that you want fontified that currently isn't, even
104;; at the maximum fontification level. You can add highlighting patterns via
105;; `font-lock-add-keywords'. For example, say in some C
106;; header file you #define the token `and' to expand to `&&', etc., to make
107;; your C code almost readable. In your ~/.emacs there could be:
98f84f52 108;;
c1f2ffc8 109;; (font-lock-add-keywords 'c-mode '("\\<\\(and\\|or\\|not\\)\\>"))
98f84f52 110;;
c1f2ffc8
SM
111;; Some modes provide specific ways to modify patterns based on the values of
112;; other variables. For example, additional C types can be specified via the
113;; variable `c-font-lock-extra-types'.
114
115;;; Adding patterns for modes that do not support Font Lock:
116
117;; Not all modes support Font Lock mode. If you (as a user of the mode) add
118;; patterns for a new mode, you must define in your ~/.emacs a variable or
119;; variables that specify regexp fontification. Then, you should indicate to
120;; Font Lock mode, via the mode hook setting `font-lock-defaults', exactly what
121;; support is required. For example, say Foo mode should have the following
122;; regexps fontified case-sensitively, and comments and strings should not be
123;; fontified automagically. In your ~/.emacs there could be:
98f84f52 124;;
c1f2ffc8
SM
125;; (defvar foo-font-lock-keywords
126;; '(("\\<\\(one\\|two\\|three\\)\\>" . font-lock-keyword-face)
127;; ("\\<\\(four\\|five\\|six\\)\\>" . font-lock-type-face))
128;; "Default expressions to highlight in Foo mode.")
98f84f52 129;;
c1f2ffc8
SM
130;; (add-hook 'foo-mode-hook
131;; (function (lambda ()
132;; (make-local-variable 'font-lock-defaults)
133;; (setq font-lock-defaults '(foo-font-lock-keywords t)))))
134
135;;; Adding Font Lock support for modes:
136
137;; Of course, it would be better that the mode already supports Font Lock mode.
138;; The package author would do something similar to above. The mode must
139;; define at the top-level a variable or variables that specify regexp
140;; fontification. Then, the mode command should indicate to Font Lock mode,
141;; via `font-lock-defaults', exactly what support is required. For example,
142;; say Bar mode should have the following regexps fontified case-insensitively,
143;; and comments and strings should be fontified automagically. In bar.el there
144;; could be:
98f84f52 145;;
c1f2ffc8
SM
146;; (defvar bar-font-lock-keywords
147;; '(("\\<\\(uno\\|due\\|tre\\)\\>" . font-lock-keyword-face)
148;; ("\\<\\(quattro\\|cinque\\|sei\\)\\>" . font-lock-type-face))
149;; "Default expressions to highlight in Bar mode.")
98f84f52 150;;
c1f2ffc8 151;; and within `bar-mode' there could be:
98f84f52 152;;
c1f2ffc8
SM
153;; (make-local-variable 'font-lock-defaults)
154;; (setq font-lock-defaults '(bar-font-lock-keywords nil t))
98f84f52 155\f
9bfbb130
SM
156;; What is fontification for? You might say, "It's to make my code look nice."
157;; I think it should be for adding information in the form of cues. These cues
158;; should provide you with enough information to both (a) distinguish between
159;; different items, and (b) identify the item meanings, without having to read
160;; the items and think about it. Therefore, fontification allows you to think
161;; less about, say, the structure of code, and more about, say, why the code
162;; doesn't work. Or maybe it allows you to think less and drift off to sleep.
163;;
164;; So, here are my opinions/advice/guidelines:
165;;
7d7d915a
SM
166;; - Highlight conceptual objects, such as function and variable names, and
167;; different objects types differently, i.e., (a) and (b) above, highlight
168;; function names differently to variable names.
169;; - Keep the faces distinct from each other as far as possible.
170;; i.e., (a) above.
9bfbb130
SM
171;; - Use the same face for the same conceptual object, across all modes.
172;; i.e., (b) above, all modes that have items that can be thought of as, say,
173;; keywords, should be highlighted with the same face, etc.
9bfbb130
SM
174;; - Make the face attributes fit the concept as far as possible.
175;; i.e., function names might be a bold colour such as blue, comments might
176;; be a bright colour such as red, character strings might be brown, because,
177;; err, strings are brown (that was not the reason, please believe me).
178;; - Don't use a non-nil OVERRIDE unless you have a good reason.
179;; Only use OVERRIDE for special things that are easy to define, such as the
180;; way `...' quotes are treated in strings and comments in Emacs Lisp mode.
181;; Don't use it to, say, highlight keywords in commented out code or strings.
182;; - Err, that's it.
a1eb1cf1 183\f
2e6a15fc
SM
184;;; Code:
185
55015061
SM
186(defgroup font-lock nil
187 "Font Lock mode text highlighting package."
188 :link '(custom-manual "(emacs)Font Lock")
189 :group 'faces)
190
191(defgroup font-lock-faces nil
192 "Font Lock mode faces."
193 :prefix "font-lock-"
194 :link '(custom-manual "(emacs)Font Lock")
195 :group 'font-lock)
196\f
9bfbb130
SM
197;; User variables.
198
55015061 199(defcustom font-lock-verbose (* 0 1024)
5aa29679 200 "*If non-nil, means show status messages for buffer fontification.
55015061
SM
201If a number, only buffers greater than this size have fontification messages."
202 :type '(radio (const :tag "Never" nil)
203 (const :tag "Always" t)
204 (integer :tag "Size"))
205 :group 'font-lock)
9bfbb130 206
55015061 207(defcustom font-lock-maximum-decoration t
2d9cdda8 208 "*Maximum decoration level for fontification.
9bfbb130
SM
209If nil, use the default decoration (typically the minimum available).
210If t, use the maximum decoration available.
211If a number, use that level of decoration (or if not available the maximum).
212If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
213where MAJOR-MODE is a symbol or t (meaning the default). For example:
5aa29679
RS
214 ((c-mode . t) (c++-mode . 2) (t . 1))
215means use the maximum decoration available for buffers in C mode, level 2
55015061
SM
216decoration for buffers in C++ mode, and level 1 decoration otherwise."
217 :type '(radio (const :tag "Default" nil)
218 (const :tag "Maximum" t)
219 (integer :tag "Level")
220 (repeat (cons (symbol :tag "Major Mode")
221 (radio (const :tag "Maximum" t)
222 (integer :tag "Level")))))
223 :group 'font-lock)
224
225(defcustom font-lock-maximum-size (* 250 1024)
2d9cdda8 226 "*Maximum size of a buffer for buffer fontification.
9bfbb130
SM
227Only buffers less than this can be fontified when Font Lock mode is turned on.
228If nil, means size is irrelevant.
229If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
230where MAJOR-MODE is a symbol or t (meaning the default). For example:
5aa29679
RS
231 ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576))
232means that the maximum size is 250K for buffers in C or C++ modes, one megabyte
55015061
SM
233for buffers in Rmail mode, and size is irrelevant otherwise."
234 :type '(radio (const :tag "None" nil)
235 (integer :tag "Size")
236 (repeat (cons (symbol :tag "Major Mode")
237 (integer :tag "Size"))))
238 :group 'font-lock)
9bfbb130
SM
239\f
240;; Fontification variables:
241
030f4a35 242(defvar font-lock-keywords nil
d46c21ec
SM
243 "*A list of the keywords to highlight.
244Each element should be of the form:
a1eb1cf1 245
826b2925
SM
246 MATCHER
247 (MATCHER . MATCH)
248 (MATCHER . FACENAME)
249 (MATCHER . HIGHLIGHT)
250 (MATCHER HIGHLIGHT ...)
a078558d 251 (eval . FORM)
a1eb1cf1 252
9bfbb130
SM
253where HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
254
a078558d
SM
255FORM is an expression, whose value should be a keyword element, evaluated when
256the keyword is (first) used in a buffer. This feature can be used to provide a
257keyword that can only be generated when Font Lock mode is actually turned on.
258
9bfbb130 259For highlighting single items, typically only MATCH-HIGHLIGHT is required.
d9f7b2d3 260However, if an item or (typically) items are to be highlighted following the
9bfbb130
SM
261instance of another item (the anchor) then MATCH-ANCHORED may be required.
262
263MATCH-HIGHLIGHT should be of the form:
264
265 (MATCH FACENAME OVERRIDE LAXMATCH)
266
267Where MATCHER can be either the regexp to search for, or the function name to
268call to make the search (called with one argument, the limit of the search).
2d63aa67
SM
269MATCH is the subexpression of MATCHER to be highlighted. MATCH can be
270calculated via the function `font-lock-keyword-depth'. FACENAME is an
9bfbb130 271expression whose value is the face name to use. FACENAME's default attributes
2d63aa67 272can be defined via the variable `font-lock-face-attributes'.
a1eb1cf1 273
2d63aa67 274OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification can
9bfbb130
SM
275be overwritten. If `keep', only parts not already fontified are highlighted.
276If `prepend' or `append', existing fontification is merged with the new, in
277which the new or existing fontification, respectively, takes precedence.
d9f7b2d3 278If LAXMATCH is non-nil, no error is signaled if there is no MATCH in MATCHER.
a1eb1cf1 279
9bfbb130
SM
280For example, an element of the form highlights (if not already highlighted):
281
876f2438
SM
282 \"\\\\\\=<foo\\\\\\=>\" Discrete occurrences of \"foo\" in the value of the
283 variable `font-lock-keyword-face'.
9bfbb130
SM
284 (\"fu\\\\(bar\\\\)\" . 1) Substring \"bar\" within all occurrences of \"fubar\" in
285 the value of `font-lock-keyword-face'.
286 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'.
287 (\"foo\\\\|bar\" 0 foo-bar-face t)
288 Occurrences of either \"foo\" or \"bar\" in the value
289 of `foo-bar-face', even if already highlighted.
290
291MATCH-ANCHORED should be of the form:
292
293 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
294
56fcbd7e 295Where MATCHER is as for MATCH-HIGHLIGHT with one exception; see below.
876f2438
SM
296PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
297the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be
298used to initialise before, and cleanup after, MATCHER is used. Typically,
299PRE-MATCH-FORM is used to move to some position relative to the original
300MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might
301be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER.
9bfbb130
SM
302
303For example, an element of the form highlights (if not already highlighted):
304
876f2438 305 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
9bfbb130 306
876f2438
SM
307 Discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
308 discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
309 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is
310 initially searched for starting from the end of the match of \"anchor\", and
311 searching for subsequent instance of \"anchor\" resumes from where searching
312 for \"item\" concluded.)
9bfbb130 313
56fcbd7e
SM
314The above-mentioned exception is as follows. The limit of the MATCHER search
315defaults to the end of the line after PRE-MATCH-FORM is evaluated.
316However, if PRE-MATCH-FORM returns a position greater than the position after
317PRE-MATCH-FORM is evaluated, that position is used as the limit of the search.
318It is generally a bad idea to return a position greater than the end of the
319line, i.e., cause the MATCHER search to span lines.
320
9bfbb130
SM
321Note that the MATCH-ANCHORED feature is experimental; in the future, we may
322replace it with other ways of providing this functionality.
323
a1eb1cf1
RS
324These regular expressions should not match text which spans lines. While
325\\[font-lock-fontify-buffer] handles multi-line patterns correctly, updating
326when you edit the buffer does not, since it considers text one line at a time.
327
2d63aa67
SM
328This variable is set by major modes via the variable `font-lock-defaults'.
329Be careful when composing regexps for this list; a poorly written pattern can
330dramatically slow things down!")
030f4a35 331
2e6a15fc
SM
332;; This variable is used by mode packages that support Font Lock mode by
333;; defining their own keywords to use for `font-lock-keywords'. (The mode
334;; command should make it buffer-local and set it to provide the set up.)
b89e1134 335(defvar font-lock-defaults nil
2d63aa67
SM
336 "Defaults for Font Lock mode specified by the major mode.
337Defaults should be of the form:
338
339 (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN ...)
340
341KEYWORDS may be a symbol (a variable or function whose value is the keywords to
342use for fontification) or a list of symbols. If KEYWORDS-ONLY is non-nil,
343syntactic fontification (strings and comments) is not performed.
344If CASE-FOLD is non-nil, the case of the keywords is ignored when fontifying.
345If SYNTAX-ALIST is non-nil, it should be a list of cons pairs of the form
346\(CHAR-OR-STRING . STRING) used to set the local Font Lock syntax table, for
347keyword and syntactic fontification (see `modify-syntax-entry').
348
349If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
350backwards outside any enclosing syntactic block, for syntactic fontification.
351Typical values are `beginning-of-line' (i.e., the start of the line is known to
352be outside a syntactic block), or `beginning-of-defun' for programming modes or
353`backward-paragraph' for textual modes (i.e., the mode-dependent function is
354known to move outside a syntactic block). If nil, the beginning of the buffer
355is used as a position outside of a syntactic block, in the worst case.
356
357These item elements are used by Font Lock mode to set the variables
358`font-lock-keywords', `font-lock-keywords-only',
359`font-lock-keywords-case-fold-search', `font-lock-syntax-table' and
360`font-lock-beginning-of-syntax-function', respectively.
361
362Further item elements are alists of the form (VARIABLE . VALUE) and are in no
363particular order. Each VARIABLE is made buffer-local before set to VALUE.
364
365Currently, appropriate variables include `font-lock-mark-block-function'.
366If this is non-nil, it should be a function with no args used to mark any
367enclosing block of text, for fontification via \\[font-lock-fontify-block].
368Typical values are `mark-defun' for programming modes or `mark-paragraph' for
369textual modes (i.e., the mode-dependent function is known to put point and mark
370around a text block relevant to that mode).
371
372Other variables include those for buffer-specialised fontification functions,
373`font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function',
374`font-lock-fontify-region-function', `font-lock-unfontify-region-function',
55015061 375`font-lock-inhibit-thing-lock' and `font-lock-maximum-size'.")
b89e1134 376
2e6a15fc 377;; This variable is used where font-lock.el itself supplies the keywords.
b89e1134 378(defvar font-lock-defaults-alist
2e6a15fc
SM
379 (let (;; We use `beginning-of-defun', rather than nil, for SYNTAX-BEGIN.
380 ;; Thus the calculation of the cache is usually faster but not
381 ;; infallible, so we risk mis-fontification. sm.
fb512de9 382 (c-mode-defaults
9bfbb130
SM
383 '((c-font-lock-keywords c-font-lock-keywords-1
384 c-font-lock-keywords-2 c-font-lock-keywords-3)
a078558d 385 nil nil ((?_ . "w")) beginning-of-defun
55015061
SM
386 ;; Obsoleted by Emacs 19.35 parse-partial-sexp's COMMENTSTOP.
387 ;(font-lock-comment-start-regexp . "/[*/]")
a078558d 388 (font-lock-mark-block-function . mark-defun)))
fb512de9
SM
389 (c++-mode-defaults
390 '((c++-font-lock-keywords c++-font-lock-keywords-1
9bfbb130 391 c++-font-lock-keywords-2 c++-font-lock-keywords-3)
55015061
SM
392 nil nil ((?_ . "w")) beginning-of-defun
393 ;; Obsoleted by Emacs 19.35 parse-partial-sexp's COMMENTSTOP.
394 ;(font-lock-comment-start-regexp . "/[*/]")
a078558d 395 (font-lock-mark-block-function . mark-defun)))
2e6a15fc
SM
396 (objc-mode-defaults
397 '((objc-font-lock-keywords objc-font-lock-keywords-1
398 objc-font-lock-keywords-2 objc-font-lock-keywords-3)
399 nil nil ((?_ . "w") (?$ . "w")) nil
55015061
SM
400 ;; Obsoleted by Emacs 19.35 parse-partial-sexp's COMMENTSTOP.
401 ;(font-lock-comment-start-regexp . "/[*/]")
2e6a15fc
SM
402 (font-lock-mark-block-function . mark-defun)))
403 (java-mode-defaults
404 '((java-font-lock-keywords java-font-lock-keywords-1
405 java-font-lock-keywords-2 java-font-lock-keywords-3)
406 nil nil ((?_ . "w") (?$ . "w") (?. . "w")) nil
55015061
SM
407 ;; Obsoleted by Emacs 19.35 parse-partial-sexp's COMMENTSTOP.
408 ;(font-lock-comment-start-regexp . "/[*/]")
2e6a15fc 409 (font-lock-mark-block-function . mark-defun)))
fb512de9 410 (lisp-mode-defaults
9bfbb130
SM
411 '((lisp-font-lock-keywords
412 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
5aa29679 413 nil nil (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
55015061
SM
414 ;; Obsoleted by Emacs 19.35 parse-partial-sexp's COMMENTSTOP.
415 ;(font-lock-comment-start-regexp . ";")
5aa29679 416 (font-lock-mark-block-function . mark-defun)))
d46c21ec 417 (scheme-mode-defaults
5aa29679
RS
418 '(scheme-font-lock-keywords
419 nil t (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
55015061
SM
420 ;; Obsoleted by Emacs 19.35 parse-partial-sexp's COMMENTSTOP.
421 ;(font-lock-comment-start-regexp . ";")
5aa29679 422 (font-lock-mark-block-function . mark-defun)))
d46c21ec 423 ;; For TeX modes we could use `backward-paragraph' for the same reason.
a078558d
SM
424 ;; But we don't, because paragraph breaks are arguably likely enough to
425 ;; occur within a genuine syntactic block to make it too risky.
426 ;; However, we do specify a MARK-BLOCK function as that cannot result
427 ;; in a mis-fontification even if it might not fontify enough. --sm.
98f84f52
SM
428 (tex-mode-defaults
429 '(tex-font-lock-keywords nil nil ((?$ . "\"")) nil
55015061
SM
430 ;; Obsoleted by Emacs 19.35 parse-partial-sexp's COMMENTSTOP.
431 ;(font-lock-comment-start-regexp . "%")
98f84f52 432 (font-lock-mark-block-function . mark-paragraph)))
d46c21ec 433 )
826b2925 434 (list
d46c21ec 435 (cons 'c-mode c-mode-defaults)
2e6a15fc
SM
436 (cons 'c++-mode c++-mode-defaults)
437 (cons 'objc-mode objc-mode-defaults)
438 (cons 'java-mode java-mode-defaults)
d46c21ec
SM
439 (cons 'emacs-lisp-mode lisp-mode-defaults)
440 (cons 'inferior-scheme-mode scheme-mode-defaults)
441 (cons 'latex-mode tex-mode-defaults)
442 (cons 'lisp-mode lisp-mode-defaults)
443 (cons 'lisp-interaction-mode lisp-mode-defaults)
444 (cons 'plain-tex-mode tex-mode-defaults)
445 (cons 'scheme-mode scheme-mode-defaults)
446 (cons 'scheme-interaction-mode scheme-mode-defaults)
447 (cons 'slitex-mode tex-mode-defaults)
448 (cons 'tex-mode tex-mode-defaults)))
2d63aa67 449 "Alist of fall-back Font Lock defaults for major modes.
b89e1134 450Each item should be a list of the form:
d46c21ec 451
2d63aa67 452 (MAJOR-MODE . FONT-LOCK-DEFAULTS)
a2b8e66b 453
2d63aa67
SM
454where MAJOR-MODE is a symbol and FONT-LOCK-DEFAULTS is a list of default
455settings. See the variable `font-lock-defaults', which takes precedence.")
d46c21ec 456
c1f2ffc8
SM
457(defvar font-lock-keywords-alist nil
458 "*Alist of `font-lock-keywords' local to a `major-mode'.
459This is normally set via `font-lock-add-keywords'.")
460
9bfbb130 461(defvar font-lock-keywords-only nil
d46c21ec
SM
462 "*Non-nil means Font Lock should not fontify comments or strings.
463This is normally set via `font-lock-defaults'.")
b89e1134 464
030f4a35 465(defvar font-lock-keywords-case-fold-search nil
d46c21ec
SM
466 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
467This is normally set via `font-lock-defaults'.")
030f4a35 468
b056da51 469(defvar font-lock-syntax-table nil
799761f0 470 "Non-nil means use this syntax table for fontifying.
d46c21ec
SM
471If this is nil, the major mode's syntax table is used.
472This is normally set via `font-lock-defaults'.")
473
9bfbb130
SM
474;; If this is nil, we only use the beginning of the buffer if we can't use
475;; `font-lock-cache-position' and `font-lock-cache-state'.
d46c21ec 476(defvar font-lock-beginning-of-syntax-function nil
9bfbb130 477 "*Non-nil means use this function to move back outside of a syntactic block.
a078558d
SM
478When called with no args it should leave point at the beginning of any
479enclosing syntactic block.
9bfbb130 480If this is nil, the beginning of the buffer is used (in the worst case).
d46c21ec 481This is normally set via `font-lock-defaults'.")
b056da51 482
a078558d
SM
483(defvar font-lock-mark-block-function nil
484 "*Non-nil means use this function to mark a block of text.
485When called with no args it should leave point at the beginning of any
486enclosing textual block and mark at the end.
487This is normally set via `font-lock-defaults'.")
488
55015061
SM
489;; Obsoleted by Emacs 19.35 parse-partial-sexp's COMMENTSTOP.
490;(defvar font-lock-comment-start-regexp nil
491; "*Regexp to match the start of a comment.
492;This need not discriminate between genuine comments and quoted comment
493;characters or comment characters within strings.
494;If nil, `comment-start-skip' is used instead; see that variable for more info.
495;This is normally set via `font-lock-defaults'.")
98f84f52 496
a2b8e66b
SM
497(defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
498 "Function to use for fontifying the buffer.
499This is normally set via `font-lock-defaults'.")
500
501(defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
502 "Function to use for unfontifying the buffer.
503This is used when turning off Font Lock mode.
504This is normally set via `font-lock-defaults'.")
505
506(defvar font-lock-fontify-region-function 'font-lock-default-fontify-region
507 "Function to use for fontifying a region.
508It should take two args, the beginning and end of the region, and an optional
509third arg VERBOSE. If non-nil, the function should print status messages.
510This is normally set via `font-lock-defaults'.")
511
512(defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
513 "Function to use for unfontifying a region.
514It should take two args, the beginning and end of the region.
515This is normally set via `font-lock-defaults'.")
516
517(defvar font-lock-inhibit-thing-lock nil
518 "List of Font Lock mode related modes that should not be turned on.
519Currently, valid mode names as `fast-lock-mode' and `lazy-lock-mode'.
520This is normally set via `font-lock-defaults'.")
521
1c626aaf 522(defvar font-lock-mode nil) ; Whether we are turned on/modeline.
9bfbb130 523(defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
826b2925 524
9bfbb130
SM
525;;;###autoload
526(defvar font-lock-mode-hook nil
527 "Function or functions to run on entry to Font Lock mode.")
030f4a35 528\f
5aa29679
RS
529;; Font Lock mode.
530
531(eval-when-compile
2e6a15fc 532 ;;
5aa29679 533 ;; We don't do this at the top-level as we only use non-autoloaded macros.
2e6a15fc
SM
534 (require 'cl)
535 ;;
536 ;; Borrowed from lazy-lock.el.
537 ;; We use this to preserve or protect things when modifying text properties.
538 (defmacro save-buffer-state (varlist &rest body)
539 "Bind variables according to VARLIST and eval BODY restoring buffer state."
540 (` (let* ((,@ (append varlist
55015061
SM
541 '((modified (buffer-modified-p)) (buffer-undo-list t)
542 (inhibit-read-only t) (inhibit-point-motion-hooks t)
2e6a15fc
SM
543 before-change-functions after-change-functions
544 deactivate-mark buffer-file-name buffer-file-truename))))
545 (,@ body)
546 (when (and (not modified) (buffer-modified-p))
547 (set-buffer-modified-p nil)))))
548 (put 'save-buffer-state 'lisp-indent-function 1))
030f4a35
RS
549
550;;;###autoload
551(defun font-lock-mode (&optional arg)
e595fa35 552 "Toggle Font Lock mode.
030f4a35
RS
553With arg, turn Font Lock mode on if and only if arg is positive.
554
555When Font Lock mode is enabled, text is fontified as you type it:
556
a1eb1cf1
RS
557 - Comments are displayed in `font-lock-comment-face';
558 - Strings are displayed in `font-lock-string-face';
559 - Certain other expressions are displayed in other faces according to the
560 value of the variable `font-lock-keywords'.
561
562You can enable Font Lock mode in any major mode automatically by turning on in
563the major mode's hook. For example, put in your ~/.emacs:
564
565 (add-hook 'c-mode-hook 'turn-on-font-lock)
566
a2b8e66b 567Alternatively, you can use Global Font Lock mode to automagically turn on Font
fd23afbe
SM
568Lock mode in buffers whose major mode supports it and whose major mode is one
569of `font-lock-global-modes'. For example, put in your ~/.emacs:
a2b8e66b
SM
570
571 (global-font-lock-mode t)
572
fd23afbe
SM
573There are a number of support modes that may be used to speed up Font Lock mode
574in various ways, specified via the variable `font-lock-support-mode'. Where
575major modes support different levels of fontification, you can use the variable
fb512de9 576`font-lock-maximum-decoration' to specify which level you generally prefer.
b89e1134
SM
577When you turn Font Lock mode on/off the buffer is fontified/defontified, though
578fontification occurs only if the buffer is less than `font-lock-maximum-size'.
7d7d915a 579
fd23afbe
SM
580For example, to specify that Font Lock mode use use Lazy Lock mode as a support
581mode and use maximum levels of fontification, put in your ~/.emacs:
582
583 (setq font-lock-support-mode 'lazy-lock-mode)
584 (setq font-lock-maximum-decoration t)
585
56fcbd7e
SM
586To add your own highlighting for some major mode, and modify the highlighting
587selected automatically via the variable `font-lock-maximum-decoration', you can
588use `font-lock-add-keywords'.
589
7d7d915a
SM
590To fontify a buffer, without turning on Font Lock mode and regardless of buffer
591size, you can use \\[font-lock-fontify-buffer].
a078558d
SM
592
593To fontify a block (the function or paragraph containing point, or a number of
594lines around point), perhaps because modification on the current line caused
fd23afbe
SM
595syntactic change on other lines, you can use \\[font-lock-fontify-block].
596
597The default Font Lock mode faces and their attributes are defined in the
598variable `font-lock-face-attributes', and Font Lock mode default settings in
599the variable `font-lock-defaults-alist'. You can set your own default settings
600for some mode, by setting a buffer local value for `font-lock-defaults', via
601its mode hook."
030f4a35 602 (interactive "P")
a2b8e66b
SM
603 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
604 ;; batch job) or if the buffer is invisible (the name starts with a space).
5aa29679 605 (let ((on-p (and (not noninteractive)
a2b8e66b
SM
606 (not (eq (aref (buffer-name) 0) ?\ ))
607 (if arg
608 (> (prefix-numeric-value arg) 0)
343204bd 609 (not font-lock-mode)))))
030f4a35 610 (set (make-local-variable 'font-lock-mode) on-p)
5aa29679
RS
611 ;; Turn on Font Lock mode.
612 (when on-p
2e6a15fc
SM
613 (make-local-hook 'after-change-functions)
614 (add-hook 'after-change-functions 'font-lock-after-change-function nil t)
5aa29679
RS
615 (font-lock-set-defaults)
616 (font-lock-turn-on-thing-lock)
617 (run-hooks 'font-lock-mode-hook)
618 ;; Fontify the buffer if we have to.
619 (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size)))
620 (cond (font-lock-fontified
621 nil)
622 ((or (null max-size) (> max-size (buffer-size)))
623 (font-lock-fontify-buffer))
624 (font-lock-verbose
625 (message "Fontifying %s...buffer too big" (buffer-name))))))
626 ;; Turn off Font Lock mode.
2e6a15fc 627 (unless on-p
5aa29679
RS
628 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
629 (font-lock-unfontify-buffer)
630 (font-lock-turn-off-thing-lock)
631 (font-lock-unset-defaults))
030f4a35
RS
632 (force-mode-line-update)))
633
a1eb1cf1
RS
634;;;###autoload
635(defun turn-on-font-lock ()
a465832f 636 "Turn on Font Lock mode conditionally.
fd23afbe 637Turn on only if the terminal can display it."
56fcbd7e
SM
638 (when (and (not font-lock-mode) window-system)
639 (font-lock-mode)))
c1f2ffc8
SM
640
641;;;###autoload
642(defun font-lock-add-keywords (major-mode keywords &optional append)
643 "Add highlighting KEYWORDS for MAJOR-MODE.
1c626aaf
SM
644MAJOR-MODE should be a symbol, the major mode command name, such as `c-mode'
645or nil. If nil, highlighting keywords are added for the current buffer.
c1f2ffc8
SM
646KEYWORDS should be a list; see the variable `font-lock-keywords'.
647By default they are added at the beginning of the current highlighting list.
648If optional argument APPEND is `set', they are used to replace the current
2d63aa67
SM
649highlighting list. If APPEND is any other non-nil value, they are added at the
650end of the current highlighting list.
c1f2ffc8
SM
651
652For example:
653
654 (font-lock-add-keywords 'c-mode
655 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
656 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . font-lock-keyword-face)))
657
658adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
2d63aa67
SM
659comments, and to fontify `and', `or' and `not' words as keywords.
660
661Note that some modes have specialised support for additional patterns, e.g.,
662see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
663`objc-font-lock-extra-types' and `java-font-lock-extra-types'."
c1f2ffc8
SM
664 (cond (major-mode
665 ;; If MAJOR-MODE is non-nil, add the KEYWORDS and APPEND spec to
666 ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
667 (let ((spec (cons keywords append)) cell)
668 (if (setq cell (assq major-mode font-lock-keywords-alist))
669 (setcdr cell (append (cdr cell) (list spec)))
670 (push (list major-mode spec) font-lock-keywords-alist))))
671 (font-lock-mode
672 ;; Otherwise if Font Lock mode is on, set or add the keywords now.
673 (if (eq append 'set)
674 (setq font-lock-keywords keywords)
675 (let ((old (if (eq (car-safe font-lock-keywords) t)
676 (cdr font-lock-keywords)
677 font-lock-keywords)))
678 (setq font-lock-keywords (if append
679 (append old keywords)
680 (append keywords old))))))))
a2b8e66b 681\f
2d63aa67
SM
682;;; Global Font Lock mode.
683
a2b8e66b 684;; A few people have hassled in the past for a way to make it easier to turn on
5aa29679
RS
685;; Font Lock mode, without the user needing to know for which modes s/he has to
686;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always
a2b8e66b
SM
687;; balked at that way, as I see it as just re-moulding the same problem in
688;; another form. That is; some person would still have to keep track of which
689;; modes (which may not even be distributed with Emacs) support Font Lock mode.
690;; The list would always be out of date. And that person might have to be me.
691
5aa29679
RS
692;; Implementation.
693;;
694;; In a previous discussion the following hack came to mind. It is a gross
695;; hack, but it generally works. We use the convention that major modes start
696;; by calling the function `kill-all-local-variables', which in turn runs
a2b8e66b
SM
697;; functions on the hook variable `change-major-mode-hook'. We attach our
698;; function `font-lock-change-major-mode' to that hook. Of course, when this
699;; hook is run, the major mode is in the process of being changed and we do not
700;; know what the final major mode will be. So, `font-lock-change-major-mode'
701;; only (a) notes the name of the current buffer, and (b) adds our function
5aa29679
RS
702;; `turn-on-font-lock-if-enabled' to the hook variables `find-file-hooks' and
703;; `post-command-hook' (for buffers that are not visiting files). By the time
704;; the functions on the first of these hooks to be run are run, the new major
705;; mode is assumed to be in place. This way we get a Font Lock function run
706;; when a major mode is turned on, without knowing major modes or their hooks.
707;;
a2b8e66b
SM
708;; Naturally this requires that (a) major modes run `kill-all-local-variables',
709;; as they are supposed to do, and (b) the major mode is in place after the
5aa29679
RS
710;; file is visited or the command that ran `kill-all-local-variables' has
711;; finished, whichever the sooner. Arguably, any major mode that does not
712;; follow the convension (a) is broken, and I can't think of any reason why (b)
713;; would not be met (except `gnudoit' on non-files). However, it is not clean.
714;;
a2b8e66b
SM
715;; Probably the cleanest solution is to have each major mode function run some
716;; hook, e.g., `major-mode-hook', but maybe implementing that change is
5aa29679
RS
717;; impractical. I am personally against making `setq' a macro or be advised,
718;; or have a special function such as `set-major-mode', but maybe someone can
719;; come up with another solution?
720
721;; User interface.
722;;
723;; Although Global Font Lock mode is a pseudo-mode, I think that the user
724;; interface should conform to the usual Emacs convention for modes, i.e., a
725;; command to toggle the feature (`global-font-lock-mode') with a variable for
726;; finer control of the mode's behaviour (`font-lock-global-modes').
727;;
2e6a15fc
SM
728;; The feature should not be enabled by loading font-lock.el, since other
729;; mechanisms for turning on Font Lock mode, such as M-x font-lock-mode RET or
730;; (add-hook 'c-mode-hook 'turn-on-font-lock), would cause Font Lock mode to be
731;; turned on everywhere. That would not be intuitive or informative because
732;; loading a file tells you nothing about the feature or how to control it. It
1c626aaf 733;; would also be contrary to the Principle of Least Surprise. sm.
a2b8e66b 734
2d9cdda8 735(defvar font-lock-buffers nil) ; For remembering buffers.
5aa29679 736(defvar global-font-lock-mode nil)
a078558d 737
55015061 738(defcustom font-lock-global-modes t
5aa29679 739 "*Modes for which Font Lock mode is automagically turned on.
a2b8e66b
SM
740Global Font Lock mode is controlled by the `global-font-lock-mode' command.
741If nil, means no modes have Font Lock mode automatically turned on.
742If t, all modes that support Font Lock mode have it automatically turned on.
5aa29679
RS
743If a list, it should be a list of `major-mode' symbol names for which Font Lock
744mode should be automatically turned on. The sense of the list is negated if it
745begins with `not'. For example:
746 (c-mode c++-mode)
55015061
SM
747means that Font Lock mode is turned on for buffers in C and C++ modes only."
748 :type '(radio (const :tag "None" nil)
749 (const :tag "All" t)
750 (repeat (symbol :tag "Major Mode")))
751 :group 'font-lock)
a2b8e66b
SM
752
753;;;###autoload
343204bd 754(defun global-font-lock-mode (&optional arg message)
a2b8e66b 755 "Toggle Global Font Lock mode.
343204bd
SM
756With prefix ARG, turn Global Font Lock mode on if and only if ARG is positive.
757Displays a message saying whether the mode is on or off if MESSAGE is non-nil.
758Returns the new status of Global Font Lock mode (non-nil means on).
a2b8e66b
SM
759
760When Global Font Lock mode is enabled, Font Lock mode is automagically
761turned on in a buffer if its major mode is one of `font-lock-global-modes'."
343204bd
SM
762 (interactive "P\np")
763 (let ((off-p (if arg
764 (<= (prefix-numeric-value arg) 0)
5aa29679 765 global-font-lock-mode)))
343204bd 766 (if off-p
5aa29679 767 (remove-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
6c0fc428 768 (add-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
5aa29679 769 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
2d9cdda8 770 (setq font-lock-buffers (buffer-list)))
5aa29679
RS
771 (when message
772 (message "Global Font Lock mode is now %s." (if off-p "OFF" "ON")))
773 (setq global-font-lock-mode (not off-p))))
a2b8e66b 774
a2b8e66b 775(defun font-lock-change-major-mode ()
5aa29679
RS
776 ;; Turn off Font Lock mode if it's on.
777 (when font-lock-mode
56fcbd7e 778 (font-lock-mode))
a2b8e66b 779 ;; Gross hack warning: Delicate readers should avert eyes now.
343204bd
SM
780 ;; Something is running `kill-all-local-variables', which generally means the
781 ;; major mode is being changed. Run `turn-on-font-lock-if-enabled' after the
5aa29679
RS
782 ;; file is visited or the current command has finished.
783 (when global-font-lock-mode
784 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
785 (add-to-list 'font-lock-buffers (current-buffer))))
a2b8e66b 786
a465832f 787(defun turn-on-font-lock-if-enabled ()
a2b8e66b 788 ;; Gross hack warning: Delicate readers should avert eyes now.
fd23afbe
SM
789 ;; Turn on Font Lock mode if it's supported by the major mode and enabled by
790 ;; the user.
a465832f 791 (remove-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
2d9cdda8 792 (while font-lock-buffers
1c626aaf
SM
793 (when (buffer-live-p (car font-lock-buffers))
794 (save-excursion
795 (set-buffer (car font-lock-buffers))
796 (when (and (or font-lock-defaults
fd23afbe
SM
797 (assq major-mode font-lock-defaults-alist))
798 (or (eq font-lock-global-modes t)
799 (if (eq (car-safe font-lock-global-modes) 'not)
800 (not (memq major-mode (cdr font-lock-global-modes)))
801 (memq major-mode font-lock-global-modes))))
1c626aaf
SM
802 (let (inhibit-quit)
803 (turn-on-font-lock)))))
2d9cdda8 804 (setq font-lock-buffers (cdr font-lock-buffers))))
a2b8e66b 805
5aa29679
RS
806(add-hook 'change-major-mode-hook 'font-lock-change-major-mode)
807
2d63aa67 808;;; End of Global Font Lock mode.
a2b8e66b 809\f
2d63aa67
SM
810;;; Font Lock Support mode.
811
5aa29679
RS
812;; This is the code used to interface font-lock.el with any of its add-on
813;; packages, and provide the user interface. Packages that have their own
814;; local buffer fontification functions (see below) may have to call
815;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
816;; themselves.
817
55015061 818(defcustom font-lock-support-mode nil
5aa29679
RS
819 "*Support mode for Font Lock mode.
820Support modes speed up Font Lock mode by being choosy about when fontification
821occurs. Known support modes are Fast Lock mode (symbol `fast-lock-mode') and
822Lazy Lock mode (symbol `lazy-lock-mode'). See those modes for more info.
823If nil, means support for Font Lock mode is never performed.
824If a symbol, use that support mode.
825If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
826where MAJOR-MODE is a symbol or t (meaning the default). For example:
827 ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
828means that Fast Lock mode is used to support Font Lock mode for buffers in C or
829C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
830
55015061
SM
831The value of this variable is used when Font Lock mode is turned on."
832 :type '(radio (const :tag "None" nil)
833 (const :tag "Fast Lock" fast-lock-mode)
834 (const :tag "Lazy Lock" lazy-lock-mode)
835 (repeat (cons (symbol :tag "Major Mode")
836 (radio (const :tag "Fast Lock" fast-lock-mode)
837 (const :tag "Lazy Lock" lazy-lock-mode)))))
838 :group 'font-lock)
5aa29679 839
2e6a15fc
SM
840(defvar fast-lock-mode nil)
841(defvar lazy-lock-mode nil)
842
5aa29679
RS
843(defun font-lock-turn-on-thing-lock ()
844 (let ((thing-mode (font-lock-value-in-major-mode font-lock-support-mode)))
845 (cond ((eq thing-mode 'fast-lock-mode)
846 (fast-lock-mode t))
847 ((eq thing-mode 'lazy-lock-mode)
848 (lazy-lock-mode t)))))
849
5aa29679
RS
850(defun font-lock-turn-off-thing-lock ()
851 (cond (fast-lock-mode
852 (fast-lock-mode nil))
853 (lazy-lock-mode
854 (lazy-lock-mode nil))))
855
856(defun font-lock-after-fontify-buffer ()
857 (cond (fast-lock-mode
858 (fast-lock-after-fontify-buffer))
859 (lazy-lock-mode
860 (lazy-lock-after-fontify-buffer))))
861
862(defun font-lock-after-unfontify-buffer ()
863 (cond (fast-lock-mode
864 (fast-lock-after-unfontify-buffer))
865 (lazy-lock-mode
866 (lazy-lock-after-unfontify-buffer))))
867
2d63aa67 868;;; End of Font Lock Support mode.
5aa29679 869\f
2d63aa67 870;;; Fontification functions.
a1eb1cf1 871
56fcbd7e
SM
872;; Rather than the function, e.g., `font-lock-fontify-region' containing the
873;; code to fontify a region, the function runs the function whose name is the
874;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally,
875;; the value of this variable is, e.g., `font-lock-default-fontify-region'
876;; which does contain the code to fontify a region. However, the value of the
877;; variable could be anything and thus, e.g., `font-lock-fontify-region' could
878;; do anything. The indirection of the fontification functions gives major
879;; modes the capability of modifying the way font-lock.el fontifies. Major
880;; modes can modify the values of, e.g., `font-lock-fontify-region-function',
881;; via the variable `font-lock-defaults'.
882;;
883;; For example, Rmail mode sets the variable `font-lock-defaults' so that
884;; font-lock.el uses its own function for buffer fontification. This function
885;; makes fontification be on a message-by-message basis and so visiting an
886;; RMAIL file is much faster. A clever implementation of the function might
887;; fontify the headers differently than the message body. (It should, and
888;; correspondingly for Mail mode, but I can't be bothered to do the work. Can
889;; you?) This hints at a more interesting use...
890;;
891;; Languages that contain text normally contained in different major modes
892;; could define their own fontification functions that treat text differently
893;; depending on its context. For example, Perl mode could arrange that here
894;; docs are fontified differently than Perl code. Or Yacc mode could fontify
895;; rules one way and C code another. Neat!
896;;
897;; A further reason to use the fontification indirection feature is when the
898;; default syntactual fontification, or the default fontification in general,
899;; is not flexible enough for a particular major mode. For example, perhaps
55015061
SM
900;; comments are just too hairy for `font-lock-fontify-syntactically-region' to
901;; cope with. You need to write your own version of that function, e.g.,
902;; `hairy-fontify-syntactically-region', and make your own version of
903;; `hairy-fontify-region' call that function before calling
56fcbd7e
SM
904;; `font-lock-fontify-keywords-region' for the normal regexp fontification
905;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el
906;; would call your region fontification function instead of its own. For
55015061 907;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line
56fcbd7e
SM
908;; directives correctly and cleanly. (It is the same problem as fontifying
909;; multi-line strings and comments; regexps are not appropriate for the job.)
910
a1eb1cf1 911;;;###autoload
030f4a35 912(defun font-lock-fontify-buffer ()
a1eb1cf1 913 "Fontify the current buffer the way `font-lock-mode' would."
030f4a35 914 (interactive)
a2b8e66b
SM
915 (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
916 (funcall font-lock-fontify-buffer-function)))
917
918(defun font-lock-unfontify-buffer ()
919 (funcall font-lock-unfontify-buffer-function))
920
921(defun font-lock-fontify-region (beg end &optional loudly)
922 (funcall font-lock-fontify-region-function beg end loudly))
923
924(defun font-lock-unfontify-region (beg end)
925 (funcall font-lock-unfontify-region-function beg end))
926
927(defun font-lock-default-fontify-buffer ()
5aa29679
RS
928 (let ((verbose (if (numberp font-lock-verbose)
929 (> (buffer-size) font-lock-verbose)
930 font-lock-verbose)))
56fcbd7e
SM
931 (when verbose
932 (message "Fontifying %s..." (buffer-name)))
7d7d915a 933 ;; Make sure we have the right `font-lock-keywords' etc.
56fcbd7e
SM
934 (unless font-lock-mode
935 (font-lock-set-defaults))
7d7d915a
SM
936 ;; Make sure we fontify etc. in the whole buffer.
937 (save-restriction
938 (widen)
939 (condition-case nil
940 (save-excursion
941 (save-match-data
942 (font-lock-fontify-region (point-min) (point-max) verbose)
271c888a 943 (font-lock-after-fontify-buffer)
7d7d915a
SM
944 (setq font-lock-fontified t)))
945 ;; We don't restore the old fontification, so it's best to unfontify.
a078558d 946 (quit (font-lock-unfontify-buffer))))
56fcbd7e
SM
947 ;; Make sure we undo `font-lock-keywords' etc.
948 (unless font-lock-mode
949 (font-lock-unset-defaults))
6c0fc428 950 (if verbose (message "Fontifying %s...%s" (buffer-name)
2e6a15fc 951 (if font-lock-fontified "done" "quit")))))
7d7d915a 952
a2b8e66b 953(defun font-lock-default-unfontify-buffer ()
56fcbd7e 954 ;; Make sure we unfontify etc. in the whole buffer.
a2b8e66b
SM
955 (save-restriction
956 (widen)
957 (font-lock-unfontify-region (point-min) (point-max))
271c888a 958 (font-lock-after-unfontify-buffer)
a2b8e66b 959 (setq font-lock-fontified nil)))
9bfbb130 960
a2b8e66b 961(defun font-lock-default-fontify-region (beg end loudly)
2e6a15fc 962 (save-buffer-state ((old-syntax-table (syntax-table)))
876f2438 963 (unwind-protect
a078558d
SM
964 (save-restriction
965 (widen)
876f2438 966 ;; Use the fontification syntax table, if any.
2e6a15fc
SM
967 (when font-lock-syntax-table
968 (set-syntax-table font-lock-syntax-table))
876f2438 969 ;; Now do the fontification.
2e6a15fc
SM
970 (font-lock-unfontify-region beg end)
971 (unless font-lock-keywords-only
876f2438
SM
972 (font-lock-fontify-syntactically-region beg end loudly))
973 (font-lock-fontify-keywords-region beg end loudly))
974 ;; Clean up.
2e6a15fc 975 (set-syntax-table old-syntax-table))))
9bfbb130
SM
976
977;; The following must be rethought, since keywords can override fontification.
978; ;; Now scan for keywords, but not if we are inside a comment now.
979; (or (and (not font-lock-keywords-only)
980; (let ((state (parse-partial-sexp beg end nil nil
981; font-lock-cache-state)))
982; (or (nth 4 state) (nth 7 state))))
983; (font-lock-fontify-keywords-region beg end))
984
a2b8e66b 985(defun font-lock-default-unfontify-region (beg end)
2e6a15fc
SM
986 (save-buffer-state nil
987 (remove-text-properties beg end '(face nil))))
9bfbb130
SM
988
989;; Called when any modification is made to buffer text.
990(defun font-lock-after-change-function (beg end old-len)
991 (save-excursion
992 (save-match-data
2e6a15fc 993 ;; Rescan between start of lines enclosing the region.
9bfbb130
SM
994 (font-lock-fontify-region
995 (progn (goto-char beg) (beginning-of-line) (point))
1c626aaf 996 (progn (goto-char end) (forward-line 1) (point))))))
a2b8e66b 997
a078558d
SM
998(defun font-lock-fontify-block (&optional arg)
999 "Fontify some lines the way `font-lock-fontify-buffer' would.
1000The lines could be a function or paragraph, or a specified number of lines.
a078558d 1001If ARG is given, fontify that many lines before and after point, or 16 lines if
a465832f
SM
1002no ARG is given and `font-lock-mark-block-function' is nil.
1003If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
1004delimit the region to fontify."
a078558d 1005 (interactive "P")
a465832f 1006 (let (font-lock-beginning-of-syntax-function deactivate-mark)
a078558d
SM
1007 ;; Make sure we have the right `font-lock-keywords' etc.
1008 (if (not font-lock-mode) (font-lock-set-defaults))
a2b8e66b
SM
1009 (save-excursion
1010 (save-match-data
1011 (condition-case error-data
a078558d
SM
1012 (if (or arg (not font-lock-mark-block-function))
1013 (let ((lines (if arg (prefix-numeric-value arg) 16)))
1014 (font-lock-fontify-region
1015 (save-excursion (forward-line (- lines)) (point))
1016 (save-excursion (forward-line lines) (point))))
1017 (funcall font-lock-mark-block-function)
1018 (font-lock-fontify-region (point) (mark)))
6c0fc428 1019 ((error quit) (message "Fontifying block...%s" error-data)))))))
a078558d 1020
94de9afe 1021(define-key facemenu-keymap "\M-g" 'font-lock-fontify-block)
2d63aa67
SM
1022
1023;;; End of Fontification functions.
9bfbb130 1024\f
2d63aa67 1025;;; Syntactic fontification functions.
9bfbb130 1026
a078558d
SM
1027;; These record the parse state at a particular position, always the start of a
1028;; line. Used to make `font-lock-fontify-syntactically-region' faster.
2e6a15fc
SM
1029;; Previously, `font-lock-cache-position' was just a buffer position. However,
1030;; under certain situations, this occasionally resulted in mis-fontification.
1c626aaf 1031;; I think the "situations" were deletion with Lazy Lock mode's deferral. sm.
a078558d 1032(defvar font-lock-cache-state nil)
2e6a15fc 1033(defvar font-lock-cache-position nil)
a078558d 1034
9bfbb130
SM
1035(defun font-lock-fontify-syntactically-region (start end &optional loudly)
1036 "Put proper face on each string and comment between START and END.
1037START should be at the beginning of a line."
55015061
SM
1038 (let ((cache (marker-position font-lock-cache-position))
1039 state string beg)
9bfbb130 1040 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
a078558d
SM
1041 (goto-char start)
1042 ;;
1043 ;; Find the state at the `beginning-of-line' before `start'.
2e6a15fc 1044 (if (eq start cache)
a078558d
SM
1045 ;; Use the cache for the state of `start'.
1046 (setq state font-lock-cache-state)
1047 ;; Find the state of `start'.
1048 (if (null font-lock-beginning-of-syntax-function)
1049 ;; Use the state at the previous cache position, if any, or
1050 ;; otherwise calculate from `point-min'.
2e6a15fc 1051 (if (or (null cache) (< start cache))
a078558d 1052 (setq state (parse-partial-sexp (point-min) start))
2e6a15fc
SM
1053 (setq state (parse-partial-sexp cache start nil nil
1054 font-lock-cache-state)))
a078558d
SM
1055 ;; Call the function to move outside any syntactic block.
1056 (funcall font-lock-beginning-of-syntax-function)
1057 (setq state (parse-partial-sexp (point) start)))
1058 ;; Cache the state and position of `start'.
2e6a15fc
SM
1059 (setq font-lock-cache-state state)
1060 (set-marker font-lock-cache-position start))
a078558d 1061 ;;
55015061
SM
1062 ;; If the region starts inside a string or comment, show the extent of it.
1063 (when (or (nth 3 state) (nth 4 state))
1064 (setq string (nth 3 state) beg (point))
1065 (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
1066 (put-text-property beg (point) 'face
1067 (if string
1068 font-lock-string-face
1069 font-lock-comment-face)))
a078558d
SM
1070 ;;
1071 ;; Find each interesting place between here and `end'.
1072 (while (and (< (point) end)
815eae1d 1073 (progn
55015061
SM
1074 (setq state (parse-partial-sexp (point) end nil nil state
1075 'syntax-table))
815eae1d 1076 (or (nth 3 state) (nth 4 state))))
55015061
SM
1077 (setq string (nth 3 state) beg (nth 8 state))
1078 (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
1079 (put-text-property beg (point) 'face
1080 (if string
1081 font-lock-string-face
1082 font-lock-comment-face)))))
2d63aa67
SM
1083
1084;;; End of Syntactic fontification functions.
9bfbb130
SM
1085\f
1086;;; Additional text property functions.
1087
1c626aaf
SM
1088;; The following text property functions should be builtins. This means they
1089;; should be written in C and put with all the other text property functions.
1090;; In the meantime, those that are used by font-lock.el are defined in Lisp
1091;; below and given a `font-lock-' prefix. Those that are not used are defined
1092;; in Lisp below and commented out. sm.
9bfbb130 1093
1c626aaf 1094(defun font-lock-prepend-text-property (start end prop value &optional object)
9bfbb130
SM
1095 "Prepend to one property of the text from START to END.
1096Arguments PROP and VALUE specify the property and value to prepend to the value
1c626aaf 1097already in place. The resulting property values are always lists.
9bfbb130
SM
1098Optional argument OBJECT is the string or buffer containing the text."
1099 (let ((val (if (listp value) value (list value))) next prev)
1100 (while (/= start end)
1101 (setq next (next-single-property-change start prop object end)
1102 prev (get-text-property start prop object))
1c626aaf
SM
1103 (put-text-property start next prop
1104 (append val (if (listp prev) prev (list prev)))
1105 object)
9bfbb130
SM
1106 (setq start next))))
1107
1c626aaf 1108(defun font-lock-append-text-property (start end prop value &optional object)
9bfbb130
SM
1109 "Append to one property of the text from START to END.
1110Arguments PROP and VALUE specify the property and value to append to the value
1c626aaf 1111already in place. The resulting property values are always lists.
9bfbb130
SM
1112Optional argument OBJECT is the string or buffer containing the text."
1113 (let ((val (if (listp value) value (list value))) next prev)
1114 (while (/= start end)
1115 (setq next (next-single-property-change start prop object end)
1116 prev (get-text-property start prop object))
1c626aaf
SM
1117 (put-text-property start next prop
1118 (append (if (listp prev) prev (list prev)) val)
1119 object)
9bfbb130 1120 (setq start next))))
1c626aaf
SM
1121
1122(defun font-lock-fillin-text-property (start end prop value &optional object)
1123 "Fill in one property of the text from START to END.
1124Arguments PROP and VALUE specify the property and value to put where none are
1125already in place. Therefore existing property values are not overwritten.
1126Optional argument OBJECT is the string or buffer containing the text."
1127 (let ((start (text-property-any start end prop nil object)) next)
1128 (while start
1129 (setq next (next-single-property-change start prop object end))
1130 (put-text-property start next prop value object)
1131 (setq start (text-property-any next end prop nil object)))))
1132
1133;; For completeness: this is to `remove-text-properties' as `put-text-property'
1134;; is to `add-text-properties', etc.
1135;(defun remove-text-property (start end property &optional object)
1136; "Remove a property from text from START to END.
1137;Argument PROPERTY is the property to remove.
1138;Optional argument OBJECT is the string or buffer containing the text.
1139;Return t if the property was actually removed, nil otherwise."
1140; (remove-text-properties start end (list property) object))
1141
1142;; For consistency: maybe this should be called `remove-single-property' like
1143;; `next-single-property-change' (not `next-single-text-property-change'), etc.
1144;(defun remove-single-text-property (start end prop value &optional object)
1145; "Remove a specific property value from text from START to END.
1146;Arguments PROP and VALUE specify the property and value to remove. The
1147;resulting property values are not equal to VALUE nor lists containing VALUE.
1148;Optional argument OBJECT is the string or buffer containing the text."
1149; (let ((start (text-property-not-all start end prop nil object)) next prev)
1150; (while start
1151; (setq next (next-single-property-change start prop object end)
1152; prev (get-text-property start prop object))
1153; (cond ((and (symbolp prev) (eq value prev))
1154; (remove-text-property start next prop object))
1155; ((and (listp prev) (memq value prev))
1156; (let ((new (delq value prev)))
1157; (cond ((null new)
1158; (remove-text-property start next prop object))
1159; ((= (length new) 1)
1160; (put-text-property start next prop (car new) object))
1161; (t
1162; (put-text-property start next prop new object))))))
1163; (setq start (text-property-not-all next end prop nil object)))))
2d63aa67
SM
1164
1165;;; End of Additional text property functions.
9bfbb130
SM
1166\f
1167;;; Regexp fontification functions.
1168
1169(defsubst font-lock-apply-highlight (highlight)
1170 "Apply HIGHLIGHT following a match.
1171HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
1172 (let* ((match (nth 0 highlight))
1173 (start (match-beginning match)) (end (match-end match))
1174 (override (nth 2 highlight)))
1175 (cond ((not start)
1176 ;; No match but we might not signal an error.
1177 (or (nth 3 highlight)
1178 (error "No match %d in highlight %S" match highlight)))
1179 ((not override)
1180 ;; Cannot override existing fontification.
1181 (or (text-property-not-all start end 'face nil)
1182 (put-text-property start end 'face (eval (nth 1 highlight)))))
1183 ((eq override t)
1184 ;; Override existing fontification.
1185 (put-text-property start end 'face (eval (nth 1 highlight))))
9bfbb130
SM
1186 ((eq override 'prepend)
1187 ;; Prepend to existing fontification.
1c626aaf 1188 (font-lock-prepend-text-property start end 'face (eval (nth 1 highlight))))
9bfbb130
SM
1189 ((eq override 'append)
1190 ;; Append to existing fontification.
1c626aaf 1191 (font-lock-append-text-property start end 'face (eval (nth 1 highlight))))
c1f2ffc8
SM
1192 ((eq override 'keep)
1193 ;; Keep existing fontification.
1c626aaf 1194 (font-lock-fillin-text-property start end 'face (eval (nth 1 highlight)))))))
9bfbb130
SM
1195
1196(defsubst font-lock-fontify-anchored-keywords (keywords limit)
1197 "Fontify according to KEYWORDS until LIMIT.
56fcbd7e
SM
1198KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1199LIMIT can be modified by the value of its PRE-MATCH-FORM."
1200 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1201 ;; Evaluate PRE-MATCH-FORM.
1202 (pre-match-value (eval (nth 1 keywords))))
1203 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1204 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1205 (setq limit pre-match-value)
1206 (save-excursion (end-of-line) (setq limit (point))))
9bfbb130 1207 (save-match-data
876f2438 1208 ;; Find an occurrence of `matcher' before `limit'.
9bfbb130
SM
1209 (while (if (stringp matcher)
1210 (re-search-forward matcher limit t)
1211 (funcall matcher limit))
876f2438 1212 ;; Apply each highlight to this instance of `matcher'.
9bfbb130
SM
1213 (setq highlights lowdarks)
1214 (while highlights
1215 (font-lock-apply-highlight (car highlights))
1216 (setq highlights (cdr highlights)))))
876f2438 1217 ;; Evaluate POST-MATCH-FORM.
9bfbb130
SM
1218 (eval (nth 2 keywords))))
1219
1220(defun font-lock-fontify-keywords-region (start end &optional loudly)
1221 "Fontify according to `font-lock-keywords' between START and END.
1222START should be at the beginning of a line."
55015061
SM
1223 (unless (eq (car-safe font-lock-keywords) t)
1224 (setq font-lock-keywords (font-lock-compile-keywords font-lock-keywords)))
9bfbb130 1225 (let ((case-fold-search font-lock-keywords-case-fold-search)
55015061 1226 (keywords (cdr font-lock-keywords))
9bfbb130 1227 (bufname (buffer-name)) (count 0)
876f2438
SM
1228 keyword matcher highlights)
1229 ;;
1230 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1231 (while keywords
1232 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
2e6a15fc 1233 (make-string (incf count) ?.)))
9bfbb130 1234 ;;
876f2438
SM
1235 ;; Find an occurrence of `matcher' from `start' to `end'.
1236 (setq keyword (car keywords) matcher (car keyword))
1237 (goto-char start)
1238 (while (if (stringp matcher)
1239 (re-search-forward matcher end t)
1240 (funcall matcher end))
1241 ;; Apply each highlight to this instance of `matcher', which may be
1242 ;; specific highlights or more keywords anchored to `matcher'.
1243 (setq highlights (cdr keyword))
1244 (while highlights
1245 (if (numberp (car (car highlights)))
1246 (font-lock-apply-highlight (car highlights))
1247 (font-lock-fontify-anchored-keywords (car highlights) end))
1248 (setq highlights (cdr highlights))))
1249 (setq keywords (cdr keywords)))))
2d63aa67
SM
1250
1251;;; End of Regexp fontification functions.
9bfbb130
SM
1252\f
1253;; Various functions.
1254
55015061
SM
1255(defun font-lock-compile-keywords (keywords)
1256 ;; Compile KEYWORDS into the form (t KEYWORD ...) where KEYWORD is of the
1257 ;; form (MATCHER HIGHLIGHT ...) as shown in `font-lock-keywords' doc string.
1258 (if (eq (car-safe keywords) t)
1259 keywords
1260 (cons t (mapcar 'font-lock-compile-keyword keywords))))
a2b8e66b
SM
1261
1262(defun font-lock-compile-keyword (keyword)
c1f2ffc8 1263 (cond ((nlistp keyword) ; MATCHER
a2b8e66b 1264 (list keyword '(0 font-lock-keyword-face)))
c1f2ffc8 1265 ((eq (car keyword) 'eval) ; (eval . FORM)
a2b8e66b 1266 (font-lock-compile-keyword (eval (cdr keyword))))
c1f2ffc8
SM
1267 ((eq (car-safe (cdr keyword)) 'quote) ; (MATCHER . 'FORM)
1268 ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote.
1269 (if (symbolp (nth 2 keyword))
1270 (list (car keyword) (list 0 (cdr keyword)))
1271 (font-lock-compile-keyword (cons (car keyword) (nth 2 keyword)))))
1272 ((numberp (cdr keyword)) ; (MATCHER . MATCH)
a2b8e66b 1273 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
c1f2ffc8 1274 ((symbolp (cdr keyword)) ; (MATCHER . FACENAME)
a2b8e66b 1275 (list (car keyword) (list 0 (cdr keyword))))
c1f2ffc8 1276 ((nlistp (nth 1 keyword)) ; (MATCHER . HIGHLIGHT)
a2b8e66b 1277 (list (car keyword) (cdr keyword)))
c1f2ffc8 1278 (t ; (MATCHER HIGHLIGHT ...)
a2b8e66b 1279 keyword)))
d46c21ec 1280
343204bd
SM
1281(defun font-lock-value-in-major-mode (alist)
1282 ;; Return value in ALIST for `major-mode', or ALIST if it is not an alist.
2e6a15fc 1283 ;; Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t.
343204bd
SM
1284 (if (consp alist)
1285 (cdr (or (assq major-mode alist) (assq t alist)))
1286 alist))
1287
d46c21ec
SM
1288(defun font-lock-choose-keywords (keywords level)
1289 ;; Return LEVELth element of KEYWORDS. A LEVEL of nil is equal to a
1290 ;; LEVEL of 0, a LEVEL of t is equal to (1- (length KEYWORDS)).
343204bd
SM
1291 (cond ((symbolp keywords)
1292 keywords)
1293 ((numberp level)
1294 (or (nth level keywords) (car (reverse keywords))))
1295 ((eq level t)
1296 (car (reverse keywords)))
1297 (t
1298 (car keywords))))
d46c21ec 1299
2e6a15fc
SM
1300(defvar font-lock-set-defaults nil) ; Whether we have set up defaults.
1301
d46c21ec
SM
1302(defun font-lock-set-defaults ()
1303 "Set fontification defaults appropriately for this mode.
a2b8e66b
SM
1304Sets various variables using `font-lock-defaults' (or, if nil, using
1305`font-lock-defaults-alist') and `font-lock-maximum-decoration'."
d46c21ec 1306 ;; Set fontification defaults.
a2b8e66b 1307 (make-local-variable 'font-lock-fontified)
2e6a15fc
SM
1308 ;; Set iff not previously set.
1309 (unless font-lock-set-defaults
1310 (set (make-local-variable 'font-lock-set-defaults) t)
1311 (set (make-local-variable 'font-lock-cache-state) nil)
1312 (set (make-local-variable 'font-lock-cache-position) (make-marker))
1313 (let* ((defaults (or font-lock-defaults
1314 (cdr (assq major-mode font-lock-defaults-alist))))
1315 (keywords
1316 (font-lock-choose-keywords (nth 0 defaults)
c1f2ffc8
SM
1317 (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1318 (local (cdr (assq major-mode font-lock-keywords-alist))))
2e6a15fc 1319 ;; Regexp fontification?
2d63aa67
SM
1320 (set (make-local-variable 'font-lock-keywords)
1321 (if (fboundp keywords) (funcall keywords) (eval keywords)))
c1f2ffc8
SM
1322 ;; Local fontification?
1323 (while local
1324 (font-lock-add-keywords nil (car (car local)) (cdr (car local)))
1325 (setq local (cdr local)))
2e6a15fc
SM
1326 ;; Syntactic fontification?
1327 (when (nth 1 defaults)
1328 (set (make-local-variable 'font-lock-keywords-only) t))
1329 ;; Case fold during regexp fontification?
1330 (when (nth 2 defaults)
1331 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
1332 ;; Syntax table for regexp and syntactic fontification?
1333 (when (nth 3 defaults)
1334 (let ((slist (nth 3 defaults)))
1335 (set (make-local-variable 'font-lock-syntax-table)
1336 (copy-syntax-table (syntax-table)))
1337 (while slist
1338 ;; The character to modify may be a single CHAR or a STRING.
1339 (let ((chars (if (numberp (car (car slist)))
1340 (list (car (car slist)))
1341 (mapcar 'identity (car (car slist)))))
1342 (syntax (cdr (car slist))))
1343 (while chars
1344 (modify-syntax-entry (car chars) syntax
1345 font-lock-syntax-table)
1346 (setq chars (cdr chars)))
1347 (setq slist (cdr slist))))))
1348 ;; Syntax function for syntactic fontification?
1349 (when (nth 4 defaults)
1350 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1351 (nth 4 defaults)))
1352 ;; Variable alist?
1353 (let ((alist (nthcdr 5 defaults)))
1354 (while alist
55015061
SM
1355 (let ((variable (car (car alist))) (value (cdr (car alist))))
1356 (unless (boundp variable)
1357 (setq variable nil))
1358 (set (make-local-variable variable) value)
1359 (setq alist (cdr alist))))))))
a2b8e66b
SM
1360
1361(defun font-lock-unset-defaults ()
1362 "Unset fontification defaults. See `font-lock-set-defaults'."
2e6a15fc
SM
1363 (setq font-lock-set-defaults nil
1364 font-lock-keywords nil
a2b8e66b
SM
1365 font-lock-keywords-only nil
1366 font-lock-keywords-case-fold-search nil
1367 font-lock-syntax-table nil
a078558d
SM
1368 font-lock-beginning-of-syntax-function nil)
1369 (let* ((defaults (or font-lock-defaults
1370 (cdr (assq major-mode font-lock-defaults-alist))))
1371 (alist (nthcdr 5 defaults)))
1372 (while alist
1373 (set (car (car alist)) (default-value (car (car alist))))
1374 (setq alist (cdr alist)))))
030f4a35 1375\f
2d63aa67 1376;;; Colour etc. support.
9bfbb130 1377
55015061
SM
1378;; Originally these variable values were face names such as `bold' etc.
1379;; Now we create our own faces, but we keep these variables for compatibility
1380;; and they give users another mechanism for changing face appearance.
1381;; We now allow a FACENAME in `font-lock-keywords' to be any expression that
1382;; returns a face. So the easiest thing is to continue using these variables,
1383;; rather than sometimes evaling FACENAME and sometimes not. sm.
1384(defvar font-lock-comment-face 'font-lock-comment-face
1385 "Face name to use for comments.")
1386
1387(defvar font-lock-string-face 'font-lock-string-face
1388 "Face name to use for strings.")
1389
1390(defvar font-lock-keyword-face 'font-lock-keyword-face
1391 "Face name to use for keywords.")
1392
1393(defvar font-lock-builtin-face 'font-lock-builtin-face
1394 "Face name to use for builtins.")
1395
1396(defvar font-lock-function-name-face 'font-lock-function-name-face
1397 "Face name to use for function names.")
1398
1399(defvar font-lock-variable-name-face 'font-lock-variable-name-face
1400 "Face name to use for variable names.")
1401
1402(defvar font-lock-type-face 'font-lock-type-face
1403 "Face name to use for type names.")
1404
1405(defvar font-lock-reference-face 'font-lock-reference-face
1406 "Face name to use for reference names.")
1407
1408(defvar font-lock-warning-face 'font-lock-warning-face
1409 "Face name to use for things that should stand out.")
1410
1411(defface font-lock-comment-face
1412 '((((class grayscale) (background light))
1413 (:foreground "DimGray" :bold t :italic t))
1414 (((class grayscale) (background dark))
1415 (:foreground "LightGray" :bold t :italic t))
1416 (((class color) (background light)) (:foreground "Firebrick"))
1417 (((class color) (background dark)) (:foreground "OrangeRed"))
1418 (t (:bold t :italic t)))
1419 "Font Lock mode face used to highlight comments."
1420 :group 'font-lock-faces)
1421
1422(defface font-lock-string-face
1423 '((((class grayscale) (background light)) (:foreground "DimGray" :italic t))
1424 (((class grayscale) (background dark)) (:foreground "LightGray" :italic t))
1425 (((class color) (background light)) (:foreground "RosyBrown"))
1426 (((class color) (background dark)) (:foreground "LightSalmon"))
1427 (t (:italic t)))
1428 "Font Lock mode face used to highlight strings."
1429 :group 'font-lock-faces)
1430
1431(defface font-lock-keyword-face
1432 '((((class grayscale) (background light)) (:foreground "LightGray" :bold t))
1433 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1434 (((class color) (background light)) (:foreground "Purple"))
1435 (((class color) (background dark)) (:foreground "Cyan"))
1436 (t (:bold t)))
1437 "Font Lock mode face used to highlight keywords."
1438 :group 'font-lock-faces)
1439
1440(defface font-lock-builtin-face
1441 '((((class grayscale) (background light)) (:foreground "LightGray" :bold t))
1442 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1443 (((class color) (background light)) (:foreground "Orchid"))
1444 (((class color) (background dark)) (:foreground "LightSteelBlue"))
1445 (t (:bold t)))
1446 "Font Lock mode face used to highlight builtins."
1447 :group 'font-lock-faces)
1448
1449(defface font-lock-function-name-face
1450 ;; Currently, Emacs/Custom does not support a :reverse or :invert spec.
1451 '((((class color) (background light)) (:foreground "Blue"))
1452 (((class color) (background dark)) (:foreground "LightSkyBlue"))
1453 (t ;(:reverse t :bold t)
1454 (:italic t :bold t)))
1455 "Font Lock mode face used to highlight function names."
1456 :group 'font-lock-faces)
1457
1458(defface font-lock-variable-name-face
1459 '((((class grayscale) (background light))
1460 (:foreground "Gray90" :bold t :italic t))
1461 (((class grayscale) (background dark))
1462 (:foreground "DimGray" :bold t :italic t))
1463 (((class color) (background light)) (:foreground "DarkGoldenrod"))
1464 (((class color) (background dark)) (:foreground "LightGoldenrod"))
1465 (t (:bold t :italic t)))
1466 "Font Lock mode face used to highlight variable names."
1467 :group 'font-lock-faces)
1468
1469(defface font-lock-type-face
1470 '((((class grayscale) (background light)) (:foreground "Gray90" :bold t))
1471 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1472 (((class color) (background light)) (:foreground "DarkOliveGreen"))
1473 (((class color) (background dark)) (:foreground "PaleGreen"))
1474 (t (:bold t :underline t)))
1475 "Font Lock mode face used to highlight types."
1476 :group 'font-lock-faces)
1477
1478(defface font-lock-reference-face
1479 '((((class grayscale) (background light))
1480 (:foreground "LightGray" :bold t :underline t))
1481 (((class grayscale) (background dark))
1482 (:foreground "Gray50" :bold t :underline t))
1483 (((class color) (background light)) (:foreground "CadetBlue"))
1484 (((class color) (background dark)) (:foreground "Aquamarine"))
1485 (t (:bold t :underline t)))
1486 "Font Lock mode face used to highlight references."
1487 :group 'font-lock-faces)
1488
1489(defface font-lock-warning-face
1490 ;; Currently, Emacs/Custom does not support a :reverse or :invert spec.
1491 '((((class color) (background light)) (:foreground "Red" :bold t))
1492 (((class color) (background dark)) (:foreground "Pink" :bold t))
1493 (t ;(:reverse t :bold t)
1494 (:italic t :bold t)))
1495 "Font Lock mode face used to highlight warnings."
1496 :group 'font-lock-faces)
2d63aa67
SM
1497
1498;;; End of Colour etc. support.
9bfbb130 1499\f
56fcbd7e
SM
1500;;; Menu support.
1501
1502;; This section of code is commented out because Emacs does not have real menu
1503;; buttons. (We can mimic them by putting "( ) " or "(X) " at the beginning of
1504;; the menu entry text, but with Xt it looks both ugly and embarrassingly
1505;; amateur.) If/When Emacs gets real menus buttons, put in menu-bar.el after
1506;; the entry for "Text Properties" something like:
1507;;
1508;; (define-key menu-bar-edit-menu [font-lock]
1509;; '("Syntax Highlighting" . font-lock-menu))
1510;;
1511;; and remove a single ";" from the beginning of each line in the rest of this
1512;; section. Probably the mechanism for telling the menu code what are menu
1513;; buttons and when they are on or off needs tweaking. I have assumed that the
1514;; mechanism is via `menu-toggle' and `menu-selected' symbol properties. sm.
1515
1516;;;;###autoload
1517;(progn
1518; ;; Make the Font Lock menu.
1519; (defvar font-lock-menu (make-sparse-keymap "Syntax Highlighting"))
1520; ;; Add the menu items in reverse order.
1521; (define-key font-lock-menu [fontify-less]
1522; '("Less In Current Buffer" . font-lock-fontify-less))
1523; (define-key font-lock-menu [fontify-more]
1524; '("More In Current Buffer" . font-lock-fontify-more))
1525; (define-key font-lock-menu [font-lock-sep]
1526; '("--"))
1527; (define-key font-lock-menu [font-lock-mode]
1528; '("In Current Buffer" . font-lock-mode))
1529; (define-key font-lock-menu [global-font-lock-mode]
1530; '("In All Buffers" . global-font-lock-mode)))
1531;
1532;;;;###autoload
1533;(progn
1534; ;; We put the appropriate `menu-enable' etc. symbol property values on when
1535; ;; font-lock.el is loaded, so we don't need to autoload the three variables.
1536; (put 'global-font-lock-mode 'menu-toggle t)
1537; (put 'font-lock-mode 'menu-toggle t)
1538; (put 'font-lock-fontify-more 'menu-enable '(identity))
1539; (put 'font-lock-fontify-less 'menu-enable '(identity)))
1540;
1541;;; Put the appropriate symbol property values on now. See above.
1542;(put 'global-font-lock-mode 'menu-selected 'global-font-lock-mode))
1543;(put 'font-lock-mode 'menu-selected 'font-lock-mode)
1544;(put 'font-lock-fontify-more 'menu-enable '(nth 2 font-lock-fontify-level))
1545;(put 'font-lock-fontify-less 'menu-enable '(nth 1 font-lock-fontify-level))
1546;
1547;(defvar font-lock-fontify-level nil) ; For less/more fontification.
1548;
1549;(defun font-lock-fontify-level (level)
1550; (let ((font-lock-maximum-decoration level))
1551; (when font-lock-mode
1552; (font-lock-mode))
1553; (font-lock-mode)
1554; (when font-lock-verbose
1555; (message "Fontifying %s... level %d" (buffer-name) level))))
1556;
1557;(defun font-lock-fontify-less ()
1558; "Fontify the current buffer with less decoration.
1559;See `font-lock-maximum-decoration'."
1560; (interactive)
1561; ;; Check in case we get called interactively.
1562; (if (nth 1 font-lock-fontify-level)
1563; (font-lock-fontify-level (1- (car font-lock-fontify-level)))
1564; (error "No less decoration")))
1565;
1566;(defun font-lock-fontify-more ()
1567; "Fontify the current buffer with more decoration.
1568;See `font-lock-maximum-decoration'."
1569; (interactive)
1570; ;; Check in case we get called interactively.
1571; (if (nth 2 font-lock-fontify-level)
1572; (font-lock-fontify-level (1+ (car font-lock-fontify-level)))
1573; (error "No more decoration")))
1574;
1575;;; This should be called by `font-lock-set-defaults'.
1576;(defun font-lock-set-menu ()
1577; ;; Activate less/more fontification entries if there are multiple levels for
1578; ;; the current buffer. Sets `font-lock-fontify-level' to be of the form
1579; ;; (CURRENT-LEVEL IS-LOWER-LEVEL-P IS-HIGHER-LEVEL-P) for menu activation.
1580; (let ((keywords (or (nth 0 font-lock-defaults)
1581; (nth 1 (assq major-mode font-lock-defaults-alist))))
1582; (level (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1583; (make-local-variable 'font-lock-fontify-level)
1584; (if (or (symbolp keywords) (= (length keywords) 1))
1585; (font-lock-unset-menu)
1586; (cond ((eq level t)
1587; (setq level (1- (length keywords))))
1588; ((or (null level) (zerop level))
1589; ;; The default level is usually, but not necessarily, level 1.
1590; (setq level (- (length keywords)
1591; (length (member (eval (car keywords))
1592; (mapcar 'eval (cdr keywords))))))))
1593; (setq font-lock-fontify-level (list level (> level 1)
1594; (< level (1- (length keywords))))))))
1595;
1596;;; This should be called by `font-lock-unset-defaults'.
1597;(defun font-lock-unset-menu ()
1598; ;; Deactivate less/more fontification entries.
1599; (setq font-lock-fontify-level nil))
2d63aa67
SM
1600
1601;;; End of Menu support.
56fcbd7e 1602\f
9bfbb130 1603;;; Various regexp information shared by several modes.
a1eb1cf1 1604;;; Information specific to a single mode should go in its load library.
030f4a35 1605
1c626aaf 1606;; Font Lock support for C, C++, Objective-C and Java modes will one day be in
56fcbd7e
SM
1607;; some cc-font.el (and required by cc-mode.el). However, the below function
1608;; should stay in font-lock.el, since it is used by other libraries. sm.
2e6a15fc 1609
c1f2ffc8 1610(defun font-lock-match-c-style-declaration-item-and-skip-to-next (limit)
2e6a15fc 1611 "Match, and move over, any declaration/definition item after point.
c1f2ffc8
SM
1612Matches after point, but ignores leading whitespace and `*' characters.
1613Does not move further than LIMIT.
1614
1615The expected syntax of a declaration/definition item is `word', possibly ending
1616with optional whitespace and a `('. Everything following the item (but
1617belonging to it) is expected to by skip-able by `scan-sexps', and items are
1618expected to be separated with a `,' and to be terminated with a `;'.
1619
1620Thus the regexp matches after point: word (
1621 ^^^^ ^
1622Where the match subexpressions are: 1 2
1623
1624The item is delimited by (match-beginning 1) and (match-end 1).
1625If (match-beginning 2) is non-nil, the item is followed by a `('.
1626
1627This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
1628 (when (looking-at "[ \t*]*\\(\\sw+\\)[ \t]*\\((\\)?")
2e6a15fc
SM
1629 (save-match-data
1630 (condition-case nil
1631 (save-restriction
1632 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
1633 (narrow-to-region (point-min) limit)
1634 (goto-char (match-end 1))
1635 ;; Move over any item value, etc., to the next item.
1636 (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)"))
1637 (goto-char (or (scan-sexps (point) 1) (point-max))))
1638 (goto-char (match-end 2)))
1639 (error t)))))
1640
2d63aa67
SM
1641(defun font-lock-keyword-depth (keyword)
1642 "Return the depth of KEYWORD regexp.
1643This means the number of parenthesized expressions."
1644 (let ((count 0) start)
1645 (while (string-match "\\\\(" keyword start)
55015061 1646 (setq count (1+ count) start (match-end 0)))
2d63aa67
SM
1647 count))
1648
2e6a15fc 1649
030f4a35 1650(defconst lisp-font-lock-keywords-1
2e6a15fc
SM
1651 (eval-when-compile
1652 (list
2d63aa67 1653 ;;
55015061
SM
1654 ;; Definitions.
1655 (list (concat "(\\(def\\("
1656 ;; Function declarations.
1657 "\\(advice\\|alias\\|"
1658 "ine-\\(derived-mode\\|function\\|skeleton\\)\\|"
1659 "macro\\|subst\\|un\\)\\|"
2e6a15fc 1660 ;; Variable declarations.
56fcbd7e 1661 "\\(const\\|custom\\|face\\|var\\)\\|"
2e6a15fc 1662 ;; Structure declarations.
55015061 1663 "\\(class\\|group\\|struct\\|type\\)"
2e6a15fc 1664 "\\)\\)\\>"
55015061 1665 ;; Any whitespace and defined object.
2e6a15fc
SM
1666 "[ \t'\(]*"
1667 "\\(\\sw+\\)?")
1668 '(1 font-lock-keyword-face)
55015061
SM
1669 '(7 (cond ((match-beginning 3) font-lock-function-name-face)
1670 ((match-beginning 5) font-lock-variable-name-face)
1671 (t font-lock-type-face))
2e6a15fc 1672 nil t))
2d63aa67
SM
1673 ;;
1674 ;; Emacs Lisp autoload cookies.
1675 '("^;;;\\(###\\)\\(autoload\\)\\>"
1676 (1 font-lock-reference-face prepend)
1677 (2 font-lock-warning-face prepend))
2e6a15fc 1678 ))
56fcbd7e 1679 "Subdued level highlighting for Lisp modes.")
030f4a35
RS
1680
1681(defconst lisp-font-lock-keywords-2
799761f0 1682 (append lisp-font-lock-keywords-1
2e6a15fc
SM
1683 (eval-when-compile
1684 (list
1685 ;;
2d63aa67 1686 ;; Control structures. Emacs Lisp forms.
2e6a15fc 1687 (cons (concat "(\\("
55015061
SM
1688; (make-regexp
1689; '("cond" "if" "while" "let\\*?" "prog[nv12*]?" "catch" "throw"
1690; "inline" "save-restriction" "save-excursion" "save-window-excursion"
1691; "save-selected-window" "save-match-data" "save-current-buffer"
1692; "unwind-protect" "condition-case" "track-mouse" "dont-compile"
1693; "eval-after-load" "eval-and-compile" "eval-when" "eval-when-compile"
1694; "with-output-to-temp-buffer" "with-timeout" "with-current-buffer"
1695; "with-temp-buffer" "with-temp-file"))
2d63aa67 1696 "c\\(atch\\|ond\\(\\|ition-case\\)\\)\\|dont-compile\\|"
2e6a15fc 1697 "eval-\\(a\\(fter-load\\|nd-compile\\)\\|"
2d63aa67
SM
1698 "when\\(\\|-compile\\)\\)\\|"
1699 "i\\(f\\|nline\\)\\|let\\*?\\|prog[nv12*]?\\|"
2e6a15fc
SM
1700 "save-\\(current-buffer\\|excursion\\|match-data\\|"
1701 "restriction\\|selected-window\\|window-excursion\\)\\|"
2d63aa67
SM
1702 "t\\(hrow\\|rack-mouse\\)\\|unwind-protect\\|"
1703 "w\\(hile\\|ith-\\(current-buffer\\|"
2e6a15fc
SM
1704 "output-to-temp-buffer\\|"
1705 "t\\(emp-\\(buffer\\|file\\)\\|imeout\\)\\)\\)"
1706 "\\)\\>")
1707 1)
1708 ;;
2d63aa67
SM
1709 ;; Control structures. Common Lisp forms.
1710 (cons (concat "(\\("
55015061
SM
1711; (make-regexp
1712; '("when" "unless" "case" "ecase" "typecase" "etypecase"
1713; "loop" "do\\*?" "dotimes" "dolist"
1714; "proclaim" "declaim" "declare"
1715; "lexical-let\\*?" "flet" "labels" "return" "return-from"))
2d63aa67
SM
1716 "case\\|d\\(ecla\\(im\\|re\\)\\|o\\(\\*?\\|"
1717 "list\\|times\\)\\)\\|e\\(case\\|typecase\\)\\|flet\\|"
1718 "l\\(abels\\|exical-let\\*?\\|oop\\)\\|proclaim\\|"
1719 "return\\(\\|-from\\)\\|typecase\\|unless\\|when"
1720 "\\)\\>")
1721 1)
1722 ;;
2e6a15fc
SM
1723 ;; Feature symbols as references.
1724 '("(\\(featurep\\|provide\\|require\\)\\>[ \t']*\\(\\sw+\\)?"
1725 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1726 ;;
1727 ;; Words inside \\[] tend to be for `substitute-command-keys'.
1728 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-reference-face prepend)
1729 ;;
1730 ;; Words inside `' tend to be symbol names.
1731 '("`\\(\\sw\\sw+\\)'" 1 font-lock-reference-face prepend)
1732 ;;
c1f2ffc8
SM
1733 ;; CLisp `:' keywords as builtins.
1734 '("\\<:\\sw\\sw+\\>" 0 font-lock-builtin-face)
2e6a15fc
SM
1735 ;;
1736 ;; ELisp and CLisp `&' keywords as types.
1737 '("\\<\\&\\sw+\\>" . font-lock-type-face)
1738 )))
fb512de9 1739 "Gaudy level highlighting for Lisp modes.")
030f4a35 1740
2e6a15fc 1741
fb512de9
SM
1742(defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
1743 "Default expressions to highlight in Lisp modes.")
030f4a35
RS
1744
1745
d46c21ec 1746(defvar scheme-font-lock-keywords
9bfbb130
SM
1747 (eval-when-compile
1748 (list
1749 ;;
1750 ;; Declarations. Hannes Haug <hannes.haug@student.uni-tuebingen.de> says
1751 ;; this works for SOS, STklos, SCOOPS, Meroon and Tiny CLOS.
1752 (list (concat "(\\(define\\("
1753 ;; Function names.
1754 "\\(\\|-\\(generic\\(\\|-procedure\\)\\|method\\)\\)\\|"
1755 ;; Macro names, as variable names. A bit dubious, this.
1756 "\\(-syntax\\)\\|"
1757 ;; Class names.
2e6a15fc 1758 "-class"
9bfbb130
SM
1759 "\\)\\)\\>"
1760 ;; Any whitespace and declared object.
1761 "[ \t]*(?"
1762 "\\(\\sw+\\)?")
1763 '(1 font-lock-keyword-face)
2e6a15fc 1764 '(7 (cond ((match-beginning 3) font-lock-function-name-face)
9bfbb130
SM
1765 ((match-beginning 6) font-lock-variable-name-face)
1766 (t font-lock-type-face))
1767 nil t))
1768 ;;
1769 ;; Control structures.
d46c21ec
SM
1770;(make-regexp '("begin" "call-with-current-continuation" "call/cc"
1771; "call-with-input-file" "call-with-output-file" "case" "cond"
9bfbb130
SM
1772; "do" "else" "for-each" "if" "lambda"
1773; "let\\*?" "let-syntax" "letrec" "letrec-syntax"
1774; ;; Hannes Haug <hannes.haug@student.uni-tuebingen.de> wants:
1775; "and" "or" "delay"
1776; ;; Stefan Monnier <stefan.monnier@epfl.ch> says don't bother:
d46c21ec
SM
1777; ;;"quasiquote" "quote" "unquote" "unquote-splicing"
1778; "map" "syntax" "syntax-rules"))
9bfbb130
SM
1779 (cons
1780 (concat "(\\("
1781 "and\\|begin\\|c\\(a\\(ll\\(-with-\\(current-continuation\\|"
1782 "input-file\\|output-file\\)\\|/cc\\)\\|se\\)\\|ond\\)\\|"
1783 "d\\(elay\\|o\\)\\|else\\|for-each\\|if\\|"
1784 "l\\(ambda\\|et\\(-syntax\\|\\*?\\|rec\\(\\|-syntax\\)\\)\\)\\|"
1785 "map\\|or\\|syntax\\(\\|-rules\\)"
1786 "\\)\\>") 1)
1787 ;;
1788 ;; David Fox <fox@graphics.cs.nyu.edu> for SOS/STklos class specifiers.
1789 '("\\<<\\sw+>\\>" . font-lock-type-face)
1790 ;;
1791 ;; Scheme `:' keywords as references.
1792 '("\\<:\\sw+\\>" . font-lock-reference-face)
1793 ))
2e6a15fc 1794 "Default expressions to highlight in Scheme modes.")
d46c21ec
SM
1795
1796
2e6a15fc
SM
1797(defvar tex-font-lock-keywords
1798; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
1799; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
1800; 2 font-lock-function-name-face)
1801; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
1802; 2 font-lock-reference-face)
1803; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
1804; ;; not be able to display those fonts.
1805; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep)
1806; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep)
1807; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face)
1808; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep))
1809 ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>.
1810 '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
1811 2 font-lock-function-name-face)
1812 ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
1813 2 font-lock-reference-face)
1814 ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face)
1815 "\\\\\\([a-zA-Z@]+\\|.\\)"
1816 ;; It seems a bit dubious to use `bold' and `italic' faces since we might
1817 ;; not be able to display those fonts.
1818 ;; LaTeX2e: \emph{This is emphasized}.
1819 ("\\\\emph{\\([^}]+\\)}" 1 'italic keep)
1820 ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...}
1821 ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}"
1822 3 (if (match-beginning 2) 'bold 'italic) keep)
1823 ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for good tables.
1824 ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)"
1825 3 (if (match-beginning 2) 'bold 'italic) keep))
1826 "Default expressions to highlight in TeX modes.")
1827\f
1828;;; User choices.
030f4a35 1829
c1f2ffc8
SM
1830;; These provide a means to fontify types not defined by the language. Those
1831;; types might be the user's own or they might be generally accepted and used.
55015061 1832;; Generally accepted types are used to provide default variable values.
c1f2ffc8 1833
2e6a15fc
SM
1834(defvar c-font-lock-extra-types '("FILE" "\\sw+_t")
1835 "*List of extra types to fontify in C mode.
55015061 1836Each list item should be a regexp not containing word-delimiters.
c1f2ffc8 1837For example, a value of (\"FILE\" \"\\\\sw+_t\") means the word FILE and words
55015061
SM
1838ending in _t are treated as type names.
1839
1840The value of this variable is used when Font Lock mode is turned on.")
9bfbb130 1841
55015061 1842(defvar c++-font-lock-extra-types '("string")
2e6a15fc 1843 "*List of extra types to fontify in C++ mode.
55015061
SM
1844Each list item should be a regexp not containing word-delimiters.
1845For example, a value of (\"string\") means the word string is treated as a type
1846name.
1847
1848The value of this variable is used when Font Lock mode is turned on.")
030f4a35 1849
2e6a15fc
SM
1850(defvar objc-font-lock-extra-types '("Class" "BOOL" "IMP" "SEL")
1851 "*List of extra types to fontify in Objective-C mode.
55015061 1852Each list item should be a regexp not containing word-delimiters.
c1f2ffc8 1853For example, a value of (\"Class\" \"BOOL\" \"IMP\" \"SEL\") means the words
55015061
SM
1854Class, BOOL, IMP and SEL are treated as type names.
1855
1856The value of this variable is used when Font Lock mode is turned on.")
1bd50840 1857
2e6a15fc
SM
1858(defvar java-font-lock-extra-types '("[A-Z\300-\326\330-\337]\\sw+")
1859 "*List of extra types to fontify in Java mode.
55015061 1860Each list item should be a regexp not containing word-delimiters.
c1f2ffc8 1861For example, a value of (\"[A-Z\300-\326\330-\337]\\\\sw+\") means capitalised
55015061
SM
1862words (and words conforming to the Java id spec) are treated as type names.
1863
1864The value of this variable is used when Font Lock mode is turned on.")
2e6a15fc
SM
1865\f
1866;;; C.
c1f2ffc8
SM
1867
1868;; [Murmur murmur murmur] Maestro, drum-roll please... [Murmur murmur murmur.]
1869;; Ahem. [Murmur murmur murmur] Lay-dees an Gennel-men. [Murmur murmur shhh!]
1870;; I am most proud and humbly honoured today [murmur murmur cough] to present
1871;; to you good people, the winner of the Second Millennium Award for The Most
1872;; Hairy Language Syntax. [Ahhh!] All rise please. [Shuffle shuffle
1873;; shuffle.] And a round of applause please. For... The C Language! [Roar.]
1874;;
1875;; Thank you... You are too kind... It is with a feeling of great privilege
1876;; and indeed emotion [sob] that I accept this award. It has been a long hard
1877;; road. But we know our destiny. And our future. For we must not rest.
1878;; There are more tokens to overload, more shoehorn, more methodologies. But
1879;; more is a plus! [Ha ha ha.] And more means plus! [Ho ho ho.] The future
1c626aaf 1880;; is C++! [Ohhh!] The Third Millennium Award... Will be ours! [Roar.]
c1f2ffc8 1881
2e6a15fc
SM
1882(defconst c-font-lock-keywords-1 nil
1883 "Subdued level highlighting for C mode.")
9bfbb130 1884
2e6a15fc
SM
1885(defconst c-font-lock-keywords-2 nil
1886 "Medium level highlighting for C mode.
1887See also `c-font-lock-extra-types'.")
1bd50840 1888
2e6a15fc
SM
1889(defconst c-font-lock-keywords-3 nil
1890 "Gaudy level highlighting for C mode.
1891See also `c-font-lock-extra-types'.")
9bfbb130 1892
2d63aa67
SM
1893(let* ((c-keywords
1894; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while")
1895 "break\\|continue\\|do\\|else\\|for\\|if\\|return\\|switch\\|while")
1896 (c-type-types
1897; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
1898; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
1899; "void" "volatile" "const")
1900 `(mapconcat 'identity
1901 (cons
1902 (,@ (concat "auto\\|c\\(har\\|onst\\)\\|double\\|"
1903 "e\\(num\\|xtern\\)\\|float\\|int\\|long\\|register\\|"
1904 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|typedef\\|"
1905 "un\\(ion\\|signed\\)\\|vo\\(id\\|latile\\)"))
1906 c-font-lock-extra-types)
1907 "\\|"))
1908 (c-type-depth `(font-lock-keyword-depth (,@ c-type-types)))
1909 )
f60f18ae
KH
1910 (setq c-font-lock-keywords-1
1911 (list
f60f18ae 1912 ;;
9bfbb130 1913 ;; These are all anchored at the beginning of line for speed.
2e6a15fc 1914 ;; Note that `c++-font-lock-keywords-1' depends on `c-font-lock-keywords-1'.
9bfbb130
SM
1915 ;;
1916 ;; Fontify function name definitions (GNU style; without type on line).
c1f2ffc8
SM
1917 '("^\\(\\sw+\\)[ \t]*(" 1 font-lock-function-name-face)
1918 ;;
1919 ;; Fontify error directives.
1920 '("^#[ \t]*error[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend)
9bfbb130
SM
1921 ;;
1922 ;; Fontify filenames in #include <...> preprocessor directives as strings.
c1f2ffc8 1923 '("^#[ \t]*\\(import\\|include\\)[ \t]+\\(<[^>\"\n]*>?\\)"
2e6a15fc 1924 2 font-lock-string-face)
f60f18ae 1925 ;;
a1eb1cf1 1926 ;; Fontify function macro names.
7d7d915a 1927 '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
f60f18ae 1928 ;;
5aa29679
RS
1929 ;; Fontify symbol names in #elif or #if ... defined preprocessor directives.
1930 '("^#[ \t]*\\(elif\\|if\\)\\>"
9bfbb130
SM
1931 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
1932 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t)))
1933 ;;
a1eb1cf1 1934 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
7d7d915a 1935 '("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?"
a1eb1cf1 1936 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t))
f60f18ae
KH
1937 ))
1938
1939 (setq c-font-lock-keywords-2
1940 (append c-font-lock-keywords-1
030f4a35 1941 (list
030f4a35 1942 ;;
9bfbb130 1943 ;; Simple regexps for speed.
b89e1134 1944 ;;
9bfbb130 1945 ;; Fontify all type specifiers.
c1f2ffc8
SM
1946 `(eval .
1947 (cons (concat "\\<\\(" (,@ c-type-types) "\\)\\>") 'font-lock-type-face))
030f4a35 1948 ;;
b89e1134 1949 ;; Fontify all builtin keywords (except case, default and goto; see below).
2e6a15fc 1950 (concat "\\<\\(" c-keywords "\\)\\>")
030f4a35 1951 ;;
9bfbb130 1952 ;; Fontify case/goto keywords and targets, and case default/goto tags.
56fcbd7e 1953 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
a1eb1cf1 1954 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2e6a15fc
SM
1955 ;; Anders Lindgren <andersl@csd.uu.se> points out that it is quicker to use
1956 ;; MATCH-ANCHORED to effectively anchor the regexp on the left.
1957 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:"
1958 (beginning-of-line) (end-of-line)
1959 (1 font-lock-reference-face)))
f60f18ae 1960 )))
1bd50840 1961
9bfbb130
SM
1962 (setq c-font-lock-keywords-3
1963 (append c-font-lock-keywords-2
1964 ;;
1965 ;; More complicated regexps for more complete highlighting for types.
1966 ;; We still have to fontify type specifiers individually, as C is so hairy.
1967 (list
1968 ;;
1969 ;; Fontify all storage classes and type specifiers, plus their items.
c1f2ffc8
SM
1970 `(eval .
1971 (list (concat "\\<\\(" (,@ c-type-types) "\\)\\>"
1972 "\\([ \t*&]+\\sw+\\>\\)*")
1973 ;; Fontify each declaration item.
2d63aa67
SM
1974 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
1975 ;; Start with point after all type specifiers.
1976 (list 'goto-char (list 'or (list 'match-beginning
1977 (+ (,@ c-type-depth) 2))
1978 '(match-end 1)))
1979 ;; Finish with point after first type specifier.
1980 '(goto-char (match-end 1))
1981 ;; Fontify as a variable or function name.
1982 '(1 (if (match-beginning 2)
1983 font-lock-function-name-face
1984 font-lock-variable-name-face)))))
9bfbb130
SM
1985 ;;
1986 ;; Fontify structures, or typedef names, plus their items.
1987 '("\\(}\\)[ \t*]*\\sw"
c1f2ffc8 1988 (font-lock-match-c-style-declaration-item-and-skip-to-next
9bfbb130 1989 (goto-char (match-end 1)) nil
c1f2ffc8 1990 (1 (if (match-beginning 2)
9bfbb130
SM
1991 font-lock-function-name-face
1992 font-lock-variable-name-face))))
1993 ;;
1994 ;; Fontify anything at beginning of line as a declaration or definition.
1995 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
1996 (1 font-lock-type-face)
c1f2ffc8 1997 (font-lock-match-c-style-declaration-item-and-skip-to-next
9bfbb130 1998 (goto-char (or (match-beginning 2) (match-end 1))) nil
c1f2ffc8 1999 (1 (if (match-beginning 2)
9bfbb130
SM
2000 font-lock-function-name-face
2001 font-lock-variable-name-face))))
2002 )))
2e6a15fc
SM
2003 )
2004
2005(defvar c-font-lock-keywords c-font-lock-keywords-1
2006 "Default expressions to highlight in C mode.
2007See also `c-font-lock-extra-types'.")
2008\f
2009;;; C++.
2010
2011(defconst c++-font-lock-keywords-1 nil
2012 "Subdued level highlighting for C++ mode.")
2013
2014(defconst c++-font-lock-keywords-2 nil
2015 "Medium level highlighting for C++ mode.
2016See also `c++-font-lock-extra-types'.")
2017
2018(defconst c++-font-lock-keywords-3 nil
2019 "Gaudy level highlighting for C++ mode.
2020See also `c++-font-lock-extra-types'.")
9bfbb130 2021
c1f2ffc8
SM
2022(defun font-lock-match-c++-style-declaration-item-and-skip-to-next (limit)
2023 ;; Regexp matches after point: word<word>::word (
2024 ;; ^^^^ ^^^^ ^^^^ ^
2025 ;; Where the match subexpressions are: 1 3 5 6
2026 ;;
2027 ;; Item is delimited by (match-beginning 1) and (match-end 1).
2028 ;; If (match-beginning 3) is non-nil, that part of the item incloses a `<>'.
2029 ;; If (match-beginning 5) is non-nil, that part of the item follows a `::'.
2030 ;; If (match-beginning 6) is non-nil, the item is followed by a `('.
2031 (when (looking-at (eval-when-compile
55015061
SM
2032 (concat
2033 ;; Skip any leading whitespace.
2034 "[ \t*&]*"
2035 ;; This is `c++-type-spec' from below. (Hint hint!)
2036 "\\(\\sw+\\)" ; The instance?
2037 "\\(<\\(\\sw+\\)[ \t*&]*>\\)?" ; Or template?
2038 "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)?" ; Or member?
2039 ;; Match any trailing parenthesis.
2040 "[ \t]*\\((\\)?")))
c1f2ffc8
SM
2041 (save-match-data
2042 (condition-case nil
2043 (save-restriction
2044 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
2045 (narrow-to-region (point-min) limit)
2046 (goto-char (match-end 1))
2047 ;; Move over any item value, etc., to the next item.
2048 (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)"))
2049 (goto-char (or (scan-sexps (point) 1) (point-max))))
2050 (goto-char (match-end 2)))
2051 (error t)))))
2052
2053(let* ((c++-keywords
2e6a15fc
SM
2054; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while"
2055; "asm" "catch" "delete" "new" "operator" "sizeof" "this" "throw" "try"
2e6a15fc
SM
2056; ;; Eric Hopper <hopper@omnifarious.mn.org> says these are new.
2057; "static_cast" "dynamic_cast" "const_cast" "reinterpret_cast")
c1f2ffc8
SM
2058 (concat "asm\\|break\\|c\\(atch\\|on\\(st_cast\\|tinue\\)\\)\\|"
2059 "d\\(elete\\|o\\|ynamic_cast\\)\\|else\\|for\\|if\\|new\\|"
2060 "operator\\|re\\(interpret_cast\\|turn\\)\\|"
2061 "s\\(izeof\\|tatic_cast\\|"
2062 "witch\\)\\|t\\(h\\(is\\|row\\)\\|ry\\)\\|while"))
2063 (c++-operators
2064 (mapconcat 'identity
2065 (mapcar 'regexp-quote
2066 ;; Taken from Stroustrup, minus keywords otherwise fontified.
2067 (sort '("+" "-" "*" "/" "%" "^" "&" "|" "~" "!" "=" "<" ">"
2068 "+=" "-=" "*=" "/=" "%=" "^=" "&=" "|=" "<<" ">>"
2069 ">>=" "<<=" "==" "!=" "<=" ">=" "&&" "||" "++" "--"
2070 "->*" "," "->" "[]" "()")
1c626aaf 2071 #'(lambda (a b) (> (length a) (length b)))))
c1f2ffc8
SM
2072 "\\|"))
2073 (c++-type-types
2e6a15fc
SM
2074; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
2075; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
c1f2ffc8 2076; "void" "volatile" "const" "inline" "friend" "bool"
2e6a15fc
SM
2077; "virtual" "complex" "template"
2078; ;; Eric Hopper <hopper@omnifarious.mn.org> says these are new.
2079; "namespace" "using")
c1f2ffc8
SM
2080 `(mapconcat 'identity
2081 (cons
2082 (,@ (concat "auto\\|bool\\|c\\(har\\|o\\(mplex\\|nst\\)\\)\\|"
2083 "double\\|e\\(num\\|xtern\\)\\|f\\(loat\\|riend\\)\\|"
2084 "in\\(line\\|t\\)\\|long\\|namespace\\|register\\|"
2085 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|"
2086 "t\\(emplate\\|ypedef\\)\\|"
2087 "u\\(n\\(ion\\|signed\\)\\|sing\\)\\|"
2088 "v\\(irtual\\|o\\(id\\|latile\\)\\)")) ; 12 ()s deep.
2089 c++-font-lock-extra-types)
2090 "\\|"))
55015061
SM
2091 ;;
2092 ;; A brave attempt to match templates following a type and/or match
2093 ;; class membership. See and sync the above function
2094 ;; `font-lock-match-c++-style-declaration-item-and-skip-to-next'.
2d63aa67 2095 (c++-type-suffix (concat "\\(<\\(\\sw+\\)[ \t*&]*>\\)?"
55015061
SM
2096 "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)?"))
2097 ;; If the string is a type, it may be followed by the cruft above.
2098 (c++-type-spec (concat "\\(\\sw+\\)\\>" c++-type-suffix))
2099 ;;
2100 ;; Parenthesis depth of user-defined types not forgetting their cruft.
2d63aa67
SM
2101 (c++-type-depth `(font-lock-keyword-depth
2102 (concat (,@ c++-type-types) (,@ c++-type-suffix))))
2d63aa67 2103 )
9bfbb130
SM
2104 (setq c++-font-lock-keywords-1
2105 (append
2106 ;;
2107 ;; The list `c-font-lock-keywords-1' less that for function names.
2108 (cdr c-font-lock-keywords-1)
9bfbb130 2109 (list
2e6a15fc 2110 ;;
c1f2ffc8
SM
2111 ;; Class names etc.
2112 (list (concat "\\<\\(class\\|public\\|private\\|protected\\)\\>[ \t]*"
2113 "\\(" c++-type-spec "\\)?")
2114 '(1 font-lock-type-face)
2115 '(3 (if (match-beginning 6)
2116 font-lock-type-face
2117 font-lock-function-name-face) nil t)
2118 '(5 font-lock-function-name-face nil t)
2119 '(7 font-lock-function-name-face nil t))
2120 ;;
2121 ;; Fontify function name definitions, possibly incorporating class names.
2122 (list (concat "^" c++-type-spec "[ \t]*(")
2123 '(1 (if (or (match-beginning 2) (match-beginning 4))
2124 font-lock-type-face
2125 font-lock-function-name-face))
2126 '(3 font-lock-function-name-face nil t)
2127 '(5 font-lock-function-name-face nil t))
9bfbb130
SM
2128 )))
2129
1bd50840 2130 (setq c++-font-lock-keywords-2
b89e1134 2131 (append c++-font-lock-keywords-1
a1eb1cf1 2132 (list
9bfbb130
SM
2133 ;;
2134 ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading.
c1f2ffc8
SM
2135 `(eval .
2136 (cons (concat "\\<\\(" (,@ c++-type-types) "\\)\\>")
2137 'font-lock-type-face))
9bfbb130 2138 ;;
c1f2ffc8
SM
2139 ;; Fontify operator overloading.
2140 (list (concat "\\<\\(operator\\)\\>[ \t]*\\(" c++-operators "\\)?")
2141 '(1 font-lock-keyword-face)
2142 '(2 font-lock-builtin-face nil t))
9bfbb130
SM
2143 ;;
2144 ;; Fontify case/goto keywords and targets, and case default/goto tags.
56fcbd7e 2145 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
b89e1134 2146 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2e6a15fc
SM
2147 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:\\($\\|[^:]\\)"
2148 (beginning-of-line) (end-of-line)
2149 (1 font-lock-reference-face)))
9bfbb130
SM
2150 ;;
2151 ;; Fontify other builtin keywords.
2152 (cons (concat "\\<\\(" c++-keywords "\\)\\>") 'font-lock-keyword-face)
2e6a15fc
SM
2153 ;;
2154 ;; Eric Hopper <hopper@omnifarious.mn.org> says `true' and `false' are new.
2155 '("\\<\\(false\\|true\\)\\>" . font-lock-reference-face)
9bfbb130
SM
2156 )))
2157
2158 (setq c++-font-lock-keywords-3
2159 (append c++-font-lock-keywords-2
2160 ;;
2161 ;; More complicated regexps for more complete highlighting for types.
2162 (list
2163 ;;
2164 ;; Fontify all storage classes and type specifiers, plus their items.
c1f2ffc8
SM
2165 `(eval .
2166 (list (concat "\\<\\(" (,@ c++-type-types) "\\)\\>" (,@ c++-type-suffix)
2167 "\\([ \t*&]+" (,@ c++-type-spec) "\\)*")
2168 ;; Fontify each declaration item.
2d63aa67
SM
2169 (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next
2170 ;; Start with point after all type specifiers.
2171 (list 'goto-char (list 'or (list 'match-beginning
2172 (+ (,@ c++-type-depth) 2))
2173 '(match-end 1)))
2174 ;; Finish with point after first type specifier.
2175 '(goto-char (match-end 1))
2176 ;; Fontify as a variable or function name.
2177 '(1 (cond ((or (match-beginning 2) (match-beginning 4))
2178 font-lock-type-face)
2179 ((match-beginning 6) font-lock-function-name-face)
2180 (t font-lock-variable-name-face)))
2181 '(3 font-lock-function-name-face nil t)
2182 '(5 (if (match-beginning 6)
2183 font-lock-function-name-face
2184 font-lock-variable-name-face) nil t))))
9bfbb130
SM
2185 ;;
2186 ;; Fontify structures, or typedef names, plus their items.
2187 '("\\(}\\)[ \t*]*\\sw"
2188 (font-lock-match-c++-style-declaration-item-and-skip-to-next
2189 (goto-char (match-end 1)) nil
c1f2ffc8 2190 (1 (if (match-beginning 6)
9bfbb130
SM
2191 font-lock-function-name-face
2192 font-lock-variable-name-face))))
2193 ;;
2194 ;; Fontify anything at beginning of line as a declaration or definition.
c1f2ffc8
SM
2195 (list (concat "^\\(" c++-type-spec "[ \t*&]*\\)+")
2196 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
2197 (goto-char (match-beginning 1))
2198 (goto-char (match-end 1))
2199 (1 (cond ((or (match-beginning 2) (match-beginning 4))
2200 font-lock-type-face)
2201 ((match-beginning 6) font-lock-function-name-face)
2202 (t font-lock-variable-name-face)))
2203 (3 font-lock-function-name-face nil t)
2204 (5 (if (match-beginning 6)
2205 font-lock-function-name-face
2206 font-lock-variable-name-face) nil t)))
9bfbb130 2207 )))
f60f18ae 2208 )
030f4a35 2209
fb512de9 2210(defvar c++-font-lock-keywords c++-font-lock-keywords-1
2e6a15fc
SM
2211 "Default expressions to highlight in C++ mode.
2212See also `c++-font-lock-extra-types'.")
2213\f
2214;;; Objective-C.
2215
2216(defconst objc-font-lock-keywords-1 nil
2217 "Subdued level highlighting for Objective-C mode.")
2218
2219(defconst objc-font-lock-keywords-2 nil
2220 "Medium level highlighting for Objective-C mode.
2221See also `objc-font-lock-extra-types'.")
2222
2223(defconst objc-font-lock-keywords-3 nil
2224 "Gaudy level highlighting for Objective-C mode.
2225See also `objc-font-lock-extra-types'.")
2226
2227;; Regexps written with help from Stephen Peters <speters@us.oracle.com> and
2228;; Jacques Duthen Prestataire <duthen@cegelec-red.fr>.
2d63aa67 2229(let* ((objc-keywords
2e6a15fc 2230; '("break" "continue" "do" "else" "for" "if" "return" "switch" "while"
2d63aa67
SM
2231; "sizeof" "self" "super")
2232 (concat "break\\|continue\\|do\\|else\\|for\\|if\\|return\\|"
2233 "s\\(elf\\|izeof\\|uper\\|witch\\)\\|while"))
2234 (objc-type-types
2235 `(mapconcat 'identity
2236 (cons
2237; '("auto" "extern" "register" "static" "typedef" "struct" "union"
2238; "enum" "signed" "unsigned" "short" "long" "int" "char"
2239; "float" "double" "void" "volatile" "const"
2240; "id" "oneway" "in" "out" "inout" "bycopy" "byref")
2241 (,@ (concat "auto\\|by\\(copy\\|ref\\)\\|c\\(har\\|onst\\)\\|"
2242 "double\\|e\\(num\\|xtern\\)\\|float\\|"
2243 "i\\([dn]\\|n\\(out\\|t\\)\\)\\|long\\|"
2244 "o\\(neway\\|ut\\)\\|register\\|s\\(hort\\|igned\\|"
2245 "t\\(atic\\|ruct\\)\\)\\|typedef\\|"
2246 "un\\(ion\\|signed\\)\\|vo\\(id\\|latile\\)"))
2247 objc-font-lock-extra-types)
2248 "\\|"))
2249 (objc-type-depth `(font-lock-keyword-depth (,@ objc-type-types)))
2250 )
2e6a15fc
SM
2251 (setq objc-font-lock-keywords-1
2252 (append
2253 ;;
2254 ;; The list `c-font-lock-keywords-1' less that for function names.
2255 (cdr c-font-lock-keywords-1)
2256 (list
2257 ;;
2258 ;; Fontify compiler directives.
c1f2ffc8
SM
2259 '("@\\(\\sw+\\)\\>"
2260 (1 font-lock-keyword-face)
2261 ("\\=[ \t:<(,]*\\(\\sw+\\)" nil nil
2e6a15fc
SM
2262 (1 font-lock-function-name-face)))
2263 ;;
2264 ;; Fontify method names and arguments. Oh Lordy!
2265 ;; First, on the same line as the function declaration.
2266 '("^[+-][ \t]*\\(PRIVATE\\)?[ \t]*\\((\\([^)\n]+\\))\\)?[ \t]*\\(\\sw+\\)"
2267 (1 font-lock-type-face nil t)
2268 (3 font-lock-type-face nil t)
2269 (4 font-lock-function-name-face)
2270 ("\\=[ \t]*\\(\\sw+\\)?:[ \t]*\\((\\([^)\n]+\\))\\)?[ \t]*\\(\\sw+\\)"
2271 nil nil
2272 (1 font-lock-function-name-face nil t)
2273 (3 font-lock-type-face nil t)
2274 (4 font-lock-variable-name-face)))
2275 ;; Second, on lines following the function declaration.
2276 '(":" ("^[ \t]*\\(\\sw+\\)?:[ \t]*\\((\\([^)\n]+\\))\\)?[ \t]*\\(\\sw+\\)"
2277 (beginning-of-line) (end-of-line)
2278 (1 font-lock-function-name-face nil t)
2279 (3 font-lock-type-face nil t)
2280 (4 font-lock-variable-name-face)))
2281 )))
030f4a35 2282
2e6a15fc
SM
2283 (setq objc-font-lock-keywords-2
2284 (append objc-font-lock-keywords-1
2285 (list
2286 ;;
2287 ;; Simple regexps for speed.
2288 ;;
2289 ;; Fontify all type specifiers.
c1f2ffc8
SM
2290 `(eval .
2291 (cons (concat "\\<\\(" (,@ objc-type-types) "\\)\\>")
2292 'font-lock-type-face))
2e6a15fc
SM
2293 ;;
2294 ;; Fontify all builtin keywords (except case, default and goto; see below).
2295 (concat "\\<\\(" objc-keywords "\\)\\>")
2296 ;;
2297 ;; Fontify case/goto keywords and targets, and case default/goto tags.
56fcbd7e 2298 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2e6a15fc
SM
2299 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2300 ;; Fontify tags iff sole statement on line, otherwise we detect selectors.
2301 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2302 (beginning-of-line) (end-of-line)
2303 (1 font-lock-reference-face)))
2304 ;;
2305 ;; Fontify null object pointers.
2306 '("\\<\\(Nil\\|nil\\)\\>" 1 font-lock-reference-face)
2307 )))
d46c21ec 2308
2e6a15fc
SM
2309 (setq objc-font-lock-keywords-3
2310 (append objc-font-lock-keywords-2
2311 ;;
2312 ;; More complicated regexps for more complete highlighting for types.
2313 ;; We still have to fontify type specifiers individually, as C is so hairy.
2314 (list
2315 ;;
2316 ;; Fontify all storage classes and type specifiers, plus their items.
c1f2ffc8
SM
2317 `(eval .
2318 (list (concat "\\<\\(" (,@ objc-type-types) "\\)\\>"
2319 "\\([ \t*&]+\\sw+\\>\\)*")
2320 ;; Fontify each declaration item.
2d63aa67
SM
2321 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2322 ;; Start with point after all type specifiers.
2323 (list 'goto-char (list 'or (list 'match-beginning
2324 (+ (,@ objc-type-depth) 2))
2325 '(match-end 1)))
2326 ;; Finish with point after first type specifier.
2327 '(goto-char (match-end 1))
2328 ;; Fontify as a variable or function name.
2329 '(1 (if (match-beginning 2)
2330 font-lock-function-name-face
2331 font-lock-variable-name-face)))))
2e6a15fc
SM
2332 ;;
2333 ;; Fontify structures, or typedef names, plus their items.
2334 '("\\(}\\)[ \t*]*\\sw"
c1f2ffc8 2335 (font-lock-match-c-style-declaration-item-and-skip-to-next
2e6a15fc 2336 (goto-char (match-end 1)) nil
c1f2ffc8 2337 (1 (if (match-beginning 2)
2e6a15fc
SM
2338 font-lock-function-name-face
2339 font-lock-variable-name-face))))
2340 ;;
2341 ;; Fontify anything at beginning of line as a declaration or definition.
2342 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2343 (1 font-lock-type-face)
c1f2ffc8 2344 (font-lock-match-c-style-declaration-item-and-skip-to-next
2e6a15fc 2345 (goto-char (or (match-beginning 2) (match-end 1))) nil
c1f2ffc8 2346 (1 (if (match-beginning 2)
2e6a15fc
SM
2347 font-lock-function-name-face
2348 font-lock-variable-name-face))))
2349 )))
2350 )
2351
2352(defvar objc-font-lock-keywords objc-font-lock-keywords-1
2353 "Default expressions to highlight in Objective-C mode.
2354See also `objc-font-lock-extra-types'.")
2355\f
2356;;; Java.
2357
2358(defconst java-font-lock-keywords-1 nil
2359 "Subdued level highlighting for Java mode.")
2360
2361(defconst java-font-lock-keywords-2 nil
2362 "Medium level highlighting for Java mode.
2363See also `java-font-lock-extra-types'.")
2364
2365(defconst java-font-lock-keywords-3 nil
2366 "Gaudy level highlighting for Java mode.
2367See also `java-font-lock-extra-types'.")
2368
2369;; Regexps written with help from Fred White <fwhite@bbn.com> and
2370;; Anders Lindgren <andersl@csd.uu.se>.
2d63aa67
SM
2371(let* ((java-keywords
2372 (concat "\\<\\("
2e6a15fc
SM
2373; '("catch" "do" "else" "super" "this" "finally" "for" "if"
2374;; ;; Anders Lindgren <andersl@csd.uu.se> says these have gone.
2375;; "cast" "byvalue" "future" "generic" "operator" "var"
2376;; "inner" "outer" "rest"
2377; "interface" "return" "switch" "throw" "try" "while")
2d63aa67
SM
2378 "catch\\|do\\|else\\|f\\(inally\\|or\\)\\|"
2379 "i\\(f\\|nterface\\)\\|return\\|s\\(uper\\|witch\\)\\|"
2380 "t\\(h\\(is\\|row\\)\\|ry\\)\\|while"
2381 "\\)\\>"))
2382 ;;
2383 ;; These are immediately followed by an object name.
2384 (java-minor-types
2385 (mapconcat 'identity
2386 '("boolean" "char" "byte" "short" "int" "long"
2387 "float" "double" "void")
2388 "\\|"))
2389 ;;
2390 ;; These are eventually followed by an object name.
2391 (java-major-types
2e6a15fc
SM
2392; '("abstract" "const" "final" "synchronized" "transient" "static"
2393;; ;; Anders Lindgren <andersl@csd.uu.se> says this has gone.
2394;; "threadsafe"
2395; "volatile" "public" "private" "protected" "native")
2d63aa67
SM
2396 (concat "abstract\\|const\\|final\\|native\\|"
2397 "p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|"
2398 "s\\(tatic\\|ynchronized\\)\\|transient\\|volatile"))
2399 ;;
2400 ;; Random types immediately followed by an object name.
2401 (java-other-types
2402 '(mapconcat 'identity (cons "\\sw+\\.\\sw+" java-font-lock-extra-types)
2403 "\\|"))
2404 (java-other-depth `(font-lock-keyword-depth (,@ java-other-types)))
2e6a15fc
SM
2405 )
2406 (setq java-font-lock-keywords-1
2407 (list
2408 ;;
2409 ;; Fontify class names.
2410 '("\\<\\(class\\)\\>[ \t]*\\(\\sw+\\)?"
2411 (1 font-lock-type-face) (2 font-lock-function-name-face nil t))
2412 ;;
2413 ;; Fontify package names in import directives.
2414 '("\\<\\(import\\|package\\)\\>[ \t]*\\(\\sw+\\)?"
2415 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2416 ))
2417
2418 (setq java-font-lock-keywords-2
2419 (append java-font-lock-keywords-1
2420 (list
2421 ;;
2422 ;; Fontify all builtin type specifiers.
2423 (cons (concat "\\<\\(" java-minor-types "\\|" java-major-types "\\)\\>")
2424 'font-lock-type-face)
2425 ;;
2426 ;; Fontify all builtin keywords (except below).
2427 (concat "\\<\\(" java-keywords "\\)\\>")
2428 ;;
2429 ;; Fontify keywords and targets, and case default/goto tags.
56fcbd7e 2430 (list "\\<\\(break\\|case\\|continue\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2e6a15fc
SM
2431 '(1 font-lock-keyword-face) '(2 font-lock-reference-face nil t))
2432 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:"
2433 (beginning-of-line) (end-of-line)
2434 (1 font-lock-reference-face)))
2435 ;;
2436 ;; Fontify keywords and types; the first can be followed by a type list.
2437 (list (concat "\\<\\("
2438 "implements\\|throws\\|"
2439 "\\(extends\\|instanceof\\|new\\)"
2440 "\\)\\>[ \t]*\\(\\sw+\\)?")
2441 '(1 font-lock-keyword-face) '(3 font-lock-type-face nil t)
2442 '("\\=[ \t]*,[ \t]*\\(\\sw+\\)"
2443 (if (match-beginning 2) (goto-char (match-end 2))) nil
2444 (1 font-lock-type-face)))
2445 ;;
2446 ;; Fontify all constants.
2447 '("\\<\\(false\\|null\\|true\\)\\>" . font-lock-reference-face)
2448 ;;
2449 ;; Javadoc tags within comments.
2450 '("@\\(author\\|exception\\|return\\|see\\|version\\)\\>"
2451 (1 font-lock-reference-face prepend))
2452 '("@\\(param\\)\\>[ \t]*\\(\\sw+\\)?"
2453 (1 font-lock-reference-face prepend)
2454 (2 font-lock-variable-name-face prepend t))
2455 )))
2456
2457 (setq java-font-lock-keywords-3
2458 (append java-font-lock-keywords-2
2459 ;;
2460 ;; More complicated regexps for more complete highlighting for types.
2461 ;; We still have to fontify type specifiers individually, as Java is hairy.
2462 (list
2463 ;;
2464 ;; Fontify random types in casts.
c1f2ffc8
SM
2465 `(eval .
2466 (list (concat "(\\(" (,@ java-other-types) "\\))"
2467 "[ \t]*\\(\\sw\\|[\"\(]\\)")
2468 ;; Fontify the type name.
2469 '(1 font-lock-type-face)))
2e6a15fc
SM
2470 ;;
2471 ;; Fontify random types immediately followed by an item or items.
c1f2ffc8
SM
2472 `(eval .
2473 (list (concat "\\<\\(" (,@ java-other-types) "\\)\\>"
2474 "\\([ \t]*\\[[ \t]*\\]\\)*"
2475 "[ \t]*\\sw")
2476 ;; Fontify the type name.
2477 '(1 font-lock-type-face)))
2478 `(eval .
2479 (list (concat "\\<\\(" (,@ java-other-types) "\\)\\>"
2480 "\\([ \t]*\\[[ \t]*\\]\\)*"
2481 "\\([ \t]*\\sw\\)")
2482 ;; Fontify each declaration item.
2d63aa67
SM
2483 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2484 ;; Start and finish with point after the type specifier.
2485 (list 'goto-char (list 'match-beginning
2486 (+ (,@ java-other-depth) 3)))
2487 (list 'goto-char (list 'match-beginning
2488 (+ (,@ java-other-depth) 3)))
2489 ;; Fontify as a variable or function name.
2490 '(1 (if (match-beginning 2)
2491 font-lock-function-name-face
2492 font-lock-variable-name-face)))))
2e6a15fc
SM
2493 ;;
2494 ;; Fontify those that are immediately followed by an item or items.
2495 (list (concat "\\<\\(" java-minor-types "\\)\\>"
2496 "\\([ \t]*\\[[ \t]*\\]\\)*")
2497 ;; Fontify each declaration item.
c1f2ffc8 2498 '(font-lock-match-c-style-declaration-item-and-skip-to-next
2e6a15fc
SM
2499 ;; Start and finish with point after the type specifier.
2500 nil (goto-char (match-end 0))
2501 ;; Fontify as a variable or function name.
c1f2ffc8 2502 (1 (if (match-beginning 2)
2e6a15fc
SM
2503 font-lock-function-name-face
2504 font-lock-variable-name-face))))
2505 ;;
2506 ;; Fontify those that are eventually followed by an item or items.
2507 (list (concat "\\<\\(" java-major-types "\\)\\>"
2508 "\\([ \t]+\\sw+\\>"
2509 "\\([ \t]*\\[[ \t]*\\]\\)*"
2510 "\\)*")
2511 ;; Fontify each declaration item.
c1f2ffc8 2512 '(font-lock-match-c-style-declaration-item-and-skip-to-next
2e6a15fc 2513 ;; Start with point after all type specifiers.
2d63aa67 2514 (goto-char (or (match-beginning 5) (match-end 1)))
2e6a15fc
SM
2515 ;; Finish with point after first type specifier.
2516 (goto-char (match-end 1))
2517 ;; Fontify as a variable or function name.
c1f2ffc8 2518 (1 (if (match-beginning 2)
2e6a15fc
SM
2519 font-lock-function-name-face
2520 font-lock-variable-name-face))))
2521 )))
2522 )
2523
2524(defvar java-font-lock-keywords java-font-lock-keywords-1
2525 "Default expressions to highlight in Java mode.
2526See also `java-font-lock-extra-types'.")
d46c21ec 2527\f
a1eb1cf1
RS
2528;; Install ourselves:
2529
5aa29679 2530(unless (assq 'font-lock-mode minor-mode-alist)
2e6a15fc 2531 (push '(font-lock-mode " Font") minor-mode-alist))
a1eb1cf1
RS
2532
2533;; Provide ourselves:
8f261d40 2534
030f4a35
RS
2535(provide 'font-lock)
2536
2537;;; font-lock.el ends here