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