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