(term-exec-1): Run process on pty if possible.
[bpt/emacs.git] / lisp / font-lock.el
CommitLineData
876f2438
SM
1;;; font-lock.el --- Electric font lock mode
2
a2b8e66b 3;; Copyright (C) 1992, 1993, 1994, 1995, 1996 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
50;; also the variable `font-lock-maximum-size'.
51
b89e1134
SM
52;; If you add patterns for a new mode, say foo.el's `foo-mode', say in which
53;; you don't want syntactic fontification to occur, you can make Font Lock mode
54;; use your regexps when turning on Font Lock by adding to `foo-mode-hook':
55;;
56;; (add-hook 'foo-mode-hook
57;; '(lambda () (make-local-variable 'font-lock-defaults)
58;; (setq font-lock-defaults '(foo-font-lock-keywords t))))
59;;
a1eb1cf1
RS
60;; Nasty regexps of the form "bar\\(\\|lo\\)\\|f\\(oo\\|u\\(\\|bar\\)\\)\\|lo"
61;; are made thusly: (make-regexp '("foo" "fu" "fubar" "bar" "barlo" "lo")) for
62;; efficiency. See /pub/gnu/emacs/elisp-archive/functions/make-regexp.el.Z on
63;; archive.cis.ohio-state.edu for this and other functions.
9bfbb130
SM
64
65;; What is fontification for? You might say, "It's to make my code look nice."
66;; I think it should be for adding information in the form of cues. These cues
67;; should provide you with enough information to both (a) distinguish between
68;; different items, and (b) identify the item meanings, without having to read
69;; the items and think about it. Therefore, fontification allows you to think
70;; less about, say, the structure of code, and more about, say, why the code
71;; doesn't work. Or maybe it allows you to think less and drift off to sleep.
72;;
73;; So, here are my opinions/advice/guidelines:
74;;
7d7d915a
SM
75;; - Highlight conceptual objects, such as function and variable names, and
76;; different objects types differently, i.e., (a) and (b) above, highlight
77;; function names differently to variable names.
78;; - Keep the faces distinct from each other as far as possible.
79;; i.e., (a) above.
9bfbb130
SM
80;; - Use the same face for the same conceptual object, across all modes.
81;; i.e., (b) above, all modes that have items that can be thought of as, say,
82;; keywords, should be highlighted with the same face, etc.
9bfbb130
SM
83;; - Make the face attributes fit the concept as far as possible.
84;; i.e., function names might be a bold colour such as blue, comments might
85;; be a bright colour such as red, character strings might be brown, because,
86;; err, strings are brown (that was not the reason, please believe me).
87;; - Don't use a non-nil OVERRIDE unless you have a good reason.
88;; Only use OVERRIDE for special things that are easy to define, such as the
89;; way `...' quotes are treated in strings and comments in Emacs Lisp mode.
90;; Don't use it to, say, highlight keywords in commented out code or strings.
91;; - Err, that's it.
a1eb1cf1 92\f
9bfbb130
SM
93;; User variables.
94
95(defvar font-lock-verbose t
96 "*If non-nil, means show status messages when fontifying.")
97
98;;;###autoload
99(defvar font-lock-maximum-decoration nil
2d9cdda8 100 "*Maximum decoration level for fontification.
9bfbb130
SM
101If nil, use the default decoration (typically the minimum available).
102If t, use the maximum decoration available.
103If a number, use that level of decoration (or if not available the maximum).
104If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
105where MAJOR-MODE is a symbol or t (meaning the default). For example:
106 ((c++-mode . 2) (c-mode . t) (t . 1))
107means use level 2 decoration for buffers in `c++-mode', the maximum decoration
108available for buffers in `c-mode', and level 1 decoration otherwise.")
030f4a35 109
9bfbb130
SM
110;;;###autoload
111(defvar font-lock-maximum-size (* 250 1024)
2d9cdda8 112 "*Maximum size of a buffer for buffer fontification.
9bfbb130
SM
113Only buffers less than this can be fontified when Font Lock mode is turned on.
114If nil, means size is irrelevant.
115If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
116where MAJOR-MODE is a symbol or t (meaning the default). For example:
117 ((c++-mode . 256000) (c-mode . 256000) (rmail-mode . 1048576))
118means that the maximum size is 250K for buffers in `c++-mode' or `c-mode', one
119megabyte for buffers in `rmail-mode', and size is irrelevant otherwise.")
120\f
121;; Fontification variables:
122
123(defvar font-lock-comment-face 'font-lock-comment-face
a1eb1cf1 124 "Face to use for comments.")
030f4a35 125
9bfbb130 126(defvar font-lock-string-face 'font-lock-string-face
a1eb1cf1 127 "Face to use for strings.")
030f4a35 128
9bfbb130
SM
129(defvar font-lock-keyword-face 'font-lock-keyword-face
130 "Face to use for keywords.")
131
132(defvar font-lock-function-name-face 'font-lock-function-name-face
030f4a35
RS
133 "Face to use for function names.")
134
9bfbb130 135(defvar font-lock-variable-name-face 'font-lock-variable-name-face
a1eb1cf1
RS
136 "Face to use for variable names.")
137
9bfbb130
SM
138(defvar font-lock-type-face 'font-lock-type-face
139 "Face to use for type names.")
030f4a35 140
9bfbb130
SM
141(defvar font-lock-reference-face 'font-lock-reference-face
142 "Face to use for reference names.")
a1eb1cf1 143
030f4a35 144(defvar font-lock-keywords nil
d46c21ec
SM
145 "*A list of the keywords to highlight.
146Each element should be of the form:
a1eb1cf1 147
826b2925
SM
148 MATCHER
149 (MATCHER . MATCH)
150 (MATCHER . FACENAME)
151 (MATCHER . HIGHLIGHT)
152 (MATCHER HIGHLIGHT ...)
a078558d 153 (eval . FORM)
a1eb1cf1 154
9bfbb130
SM
155where HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
156
a078558d
SM
157FORM is an expression, whose value should be a keyword element, evaluated when
158the keyword is (first) used in a buffer. This feature can be used to provide a
159keyword that can only be generated when Font Lock mode is actually turned on.
160
9bfbb130 161For highlighting single items, typically only MATCH-HIGHLIGHT is required.
d9f7b2d3 162However, if an item or (typically) items are to be highlighted following the
9bfbb130
SM
163instance of another item (the anchor) then MATCH-ANCHORED may be required.
164
165MATCH-HIGHLIGHT should be of the form:
166
167 (MATCH FACENAME OVERRIDE LAXMATCH)
168
169Where MATCHER can be either the regexp to search for, or the function name to
170call to make the search (called with one argument, the limit of the search).
171MATCH is the subexpression of MATCHER to be highlighted. FACENAME is an
172expression whose value is the face name to use. FACENAME's default attributes
173may be defined in `font-lock-face-attributes'.
a1eb1cf1
RS
174
175OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification may
9bfbb130
SM
176be overwritten. If `keep', only parts not already fontified are highlighted.
177If `prepend' or `append', existing fontification is merged with the new, in
178which the new or existing fontification, respectively, takes precedence.
d9f7b2d3 179If LAXMATCH is non-nil, no error is signaled if there is no MATCH in MATCHER.
a1eb1cf1 180
9bfbb130
SM
181For example, an element of the form highlights (if not already highlighted):
182
876f2438
SM
183 \"\\\\\\=<foo\\\\\\=>\" Discrete occurrences of \"foo\" in the value of the
184 variable `font-lock-keyword-face'.
9bfbb130
SM
185 (\"fu\\\\(bar\\\\)\" . 1) Substring \"bar\" within all occurrences of \"fubar\" in
186 the value of `font-lock-keyword-face'.
187 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'.
188 (\"foo\\\\|bar\" 0 foo-bar-face t)
189 Occurrences of either \"foo\" or \"bar\" in the value
190 of `foo-bar-face', even if already highlighted.
191
192MATCH-ANCHORED should be of the form:
193
194 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
195
876f2438
SM
196Where MATCHER is as for MATCH-HIGHLIGHT with one exception. The limit of the
197search is currently guaranteed to be (no greater than) the end of the line.
198PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
199the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be
200used to initialise before, and cleanup after, MATCHER is used. Typically,
201PRE-MATCH-FORM is used to move to some position relative to the original
202MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might
203be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER.
9bfbb130
SM
204
205For example, an element of the form highlights (if not already highlighted):
206
876f2438 207 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
9bfbb130 208
876f2438
SM
209 Discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
210 discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
211 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is
212 initially searched for starting from the end of the match of \"anchor\", and
213 searching for subsequent instance of \"anchor\" resumes from where searching
214 for \"item\" concluded.)
9bfbb130
SM
215
216Note that the MATCH-ANCHORED feature is experimental; in the future, we may
217replace it with other ways of providing this functionality.
218
a1eb1cf1
RS
219These regular expressions should not match text which spans lines. While
220\\[font-lock-fontify-buffer] handles multi-line patterns correctly, updating
221when you edit the buffer does not, since it considers text one line at a time.
222
9bfbb130 223Be very careful composing regexps for this list;
a1eb1cf1 224the wrong pattern can dramatically slow things down!")
9bfbb130 225(make-variable-buffer-local 'font-lock-keywords)
030f4a35 226
b89e1134
SM
227(defvar font-lock-defaults nil
228 "If set by a major mode, should be the defaults for Font Lock mode.
9bfbb130 229The value should be like the `cdr' of an item in `font-lock-defaults-alist'.")
b89e1134
SM
230
231(defvar font-lock-defaults-alist
9bfbb130
SM
232 (let (;; For C and Lisp modes we use `beginning-of-defun', rather than nil,
233 ;; for SYNTAX-BEGIN. Thus the calculation of the cache is usually
234 ;; faster but not infallible, so we risk mis-fontification. --sm.
fb512de9 235 (c-mode-defaults
9bfbb130
SM
236 '((c-font-lock-keywords c-font-lock-keywords-1
237 c-font-lock-keywords-2 c-font-lock-keywords-3)
a078558d
SM
238 nil nil ((?_ . "w")) beginning-of-defun
239 (font-lock-mark-block-function . mark-defun)))
fb512de9
SM
240 (c++-mode-defaults
241 '((c++-font-lock-keywords c++-font-lock-keywords-1
9bfbb130 242 c++-font-lock-keywords-2 c++-font-lock-keywords-3)
a078558d
SM
243 nil nil ((?_ . "w") (?~ . "w")) beginning-of-defun
244 (font-lock-mark-block-function . mark-defun)))
fb512de9 245 (lisp-mode-defaults
9bfbb130
SM
246 '((lisp-font-lock-keywords
247 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
248 nil nil
249 ((?: . "w") (?- . "w") (?* . "w") (?+ . "w") (?. . "w") (?< . "w")
250 (?> . "w") (?= . "w") (?! . "w") (?? . "w") (?$ . "w") (?% . "w")
251 (?_ . "w") (?& . "w") (?~ . "w") (?^ . "w") (?/ . "w"))
a078558d 252 beginning-of-defun (font-lock-mark-block-function . mark-defun)))
d46c21ec 253 (scheme-mode-defaults
9bfbb130
SM
254 '(scheme-font-lock-keywords nil t
255 ((?: . "w") (?- . "w") (?* . "w") (?+ . "w") (?. . "w") (?< . "w")
256 (?> . "w") (?= . "w") (?! . "w") (?? . "w") (?$ . "w") (?% . "w")
257 (?_ . "w") (?& . "w") (?~ . "w") (?^ . "w") (?/ . "w"))
a078558d 258 beginning-of-defun (font-lock-mark-block-function . mark-defun)))
d46c21ec 259 ;; For TeX modes we could use `backward-paragraph' for the same reason.
a078558d
SM
260 ;; But we don't, because paragraph breaks are arguably likely enough to
261 ;; occur within a genuine syntactic block to make it too risky.
262 ;; However, we do specify a MARK-BLOCK function as that cannot result
263 ;; in a mis-fontification even if it might not fontify enough. --sm.
264 (tex-mode-defaults '(tex-font-lock-keywords nil nil ((?$ . "\"")) nil
265 (font-lock-mark-block-function . mark-paragraph)))
d46c21ec 266 )
826b2925 267 (list
d46c21ec
SM
268 (cons 'bibtex-mode tex-mode-defaults)
269 (cons 'c++-c-mode c-mode-defaults)
270 (cons 'c++-mode c++-mode-defaults)
271 (cons 'c-mode c-mode-defaults)
9bfbb130 272 (cons 'elec-c-mode c-mode-defaults)
d46c21ec
SM
273 (cons 'emacs-lisp-mode lisp-mode-defaults)
274 (cons 'inferior-scheme-mode scheme-mode-defaults)
275 (cons 'latex-mode tex-mode-defaults)
276 (cons 'lisp-mode lisp-mode-defaults)
277 (cons 'lisp-interaction-mode lisp-mode-defaults)
278 (cons 'plain-tex-mode tex-mode-defaults)
279 (cons 'scheme-mode scheme-mode-defaults)
280 (cons 'scheme-interaction-mode scheme-mode-defaults)
281 (cons 'slitex-mode tex-mode-defaults)
282 (cons 'tex-mode tex-mode-defaults)))
9bfbb130 283 "Alist of default major mode and Font Lock defaults.
b89e1134 284Each item should be a list of the form:
d46c21ec 285
a2b8e66b 286 (MAJOR-MODE . (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN
a078558d 287 ...))
d46c21ec 288
9bfbb130
SM
289where MAJOR-MODE is a symbol. KEYWORDS may be a symbol (a variable or function
290whose value is the keywords to use for fontification) or a list of symbols.
291If KEYWORDS-ONLY is non-nil, syntactic fontification (strings and comments) is
292not performed. If CASE-FOLD is non-nil, the case of the keywords is ignored
293when fontifying. If SYNTAX-ALIST is non-nil, it should be a list of cons pairs
294of the form (CHAR . STRING) used to set the local Font Lock syntax table, for
295keyword and syntactic fontification (see `modify-syntax-entry').
296
297If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
298backwards outside any enclosing syntactic block, for syntactic fontification.
299Typical values are `beginning-of-line' (i.e., the start of the line is known to
300be outside a syntactic block), or `beginning-of-defun' for programming modes or
301`backward-paragraph' for textual modes (i.e., the mode-dependent function is
302known to move outside a syntactic block). If nil, the beginning of the buffer
303is used as a position outside of a syntactic block, in the worst case.
d46c21ec
SM
304
305These item elements are used by Font Lock mode to set the variables
9bfbb130 306`font-lock-keywords', `font-lock-keywords-only',
d46c21ec 307`font-lock-keywords-case-fold-search', `font-lock-syntax-table' and
a2b8e66b
SM
308`font-lock-beginning-of-syntax-function', respectively.
309
a078558d
SM
310Further item elements are alists of the form (VARIABLE . VALUE) and are in no
311particular order. Each VARIABLE is made buffer-local before set to VALUE.
a2b8e66b 312
a078558d
SM
313Currently, appropriate variables include `font-lock-mark-block-function'.
314If this is non-nil, it should be a function with no args used to mark any
315enclosing block of text, for fontification via \\[font-lock-fontify-block].
316Typical values are `mark-defun' for programming modes or `mark-paragraph' for
317textual modes (i.e., the mode-dependent function is known to put point and mark
318around a text block relevant to that mode).
a2b8e66b 319
a078558d 320Other variables include those for buffer-specialised fontification functions,
a2b8e66b 321`font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function',
a078558d
SM
322`font-lock-fontify-region-function', `font-lock-unfontify-region-function' and
323`font-lock-inhibit-thing-lock'.")
d46c21ec 324
9bfbb130 325(defvar font-lock-keywords-only nil
d46c21ec
SM
326 "*Non-nil means Font Lock should not fontify comments or strings.
327This is normally set via `font-lock-defaults'.")
b89e1134 328
030f4a35 329(defvar font-lock-keywords-case-fold-search nil
d46c21ec
SM
330 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
331This is normally set via `font-lock-defaults'.")
030f4a35 332
b056da51 333(defvar font-lock-syntax-table nil
799761f0 334 "Non-nil means use this syntax table for fontifying.
d46c21ec
SM
335If this is nil, the major mode's syntax table is used.
336This is normally set via `font-lock-defaults'.")
337
9bfbb130
SM
338;; If this is nil, we only use the beginning of the buffer if we can't use
339;; `font-lock-cache-position' and `font-lock-cache-state'.
d46c21ec 340(defvar font-lock-beginning-of-syntax-function nil
9bfbb130 341 "*Non-nil means use this function to move back outside of a syntactic block.
a078558d
SM
342When called with no args it should leave point at the beginning of any
343enclosing syntactic block.
9bfbb130 344If this is nil, the beginning of the buffer is used (in the worst case).
d46c21ec 345This is normally set via `font-lock-defaults'.")
b056da51 346
a078558d
SM
347(defvar font-lock-mark-block-function nil
348 "*Non-nil means use this function to mark a block of text.
349When called with no args it should leave point at the beginning of any
350enclosing textual block and mark at the end.
351This is normally set via `font-lock-defaults'.")
352
a2b8e66b
SM
353(defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
354 "Function to use for fontifying the buffer.
355This is normally set via `font-lock-defaults'.")
356
357(defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
358 "Function to use for unfontifying the buffer.
359This is used when turning off Font Lock mode.
360This is normally set via `font-lock-defaults'.")
361
362(defvar font-lock-fontify-region-function 'font-lock-default-fontify-region
363 "Function to use for fontifying a region.
364It should take two args, the beginning and end of the region, and an optional
365third arg VERBOSE. If non-nil, the function should print status messages.
366This is normally set via `font-lock-defaults'.")
367
368(defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
369 "Function to use for unfontifying a region.
370It should take two args, the beginning and end of the region.
371This is normally set via `font-lock-defaults'.")
372
373(defvar font-lock-inhibit-thing-lock nil
374 "List of Font Lock mode related modes that should not be turned on.
375Currently, valid mode names as `fast-lock-mode' and `lazy-lock-mode'.
376This is normally set via `font-lock-defaults'.")
377
9bfbb130
SM
378(defvar font-lock-mode nil) ; For the modeline.
379(defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
380(put 'font-lock-fontified 'permanent-local t)
826b2925 381
9bfbb130
SM
382;;;###autoload
383(defvar font-lock-mode-hook nil
384 "Function or functions to run on entry to Font Lock mode.")
030f4a35 385\f
a2b8e66b 386;; User commands.
030f4a35
RS
387
388;;;###autoload
389(defun font-lock-mode (&optional arg)
e595fa35 390 "Toggle Font Lock mode.
030f4a35
RS
391With arg, turn Font Lock mode on if and only if arg is positive.
392
393When Font Lock mode is enabled, text is fontified as you type it:
394
a1eb1cf1
RS
395 - Comments are displayed in `font-lock-comment-face';
396 - Strings are displayed in `font-lock-string-face';
397 - Certain other expressions are displayed in other faces according to the
398 value of the variable `font-lock-keywords'.
399
400You can enable Font Lock mode in any major mode automatically by turning on in
401the major mode's hook. For example, put in your ~/.emacs:
402
403 (add-hook 'c-mode-hook 'turn-on-font-lock)
404
a2b8e66b
SM
405Alternatively, you can use Global Font Lock mode to automagically turn on Font
406Lock mode in buffers whose major mode supports it, or in buffers whose major
407mode is one of `font-lock-global-modes'. For example, put in your ~/.emacs:
408
409 (global-font-lock-mode t)
410
a1eb1cf1
RS
411The default Font Lock mode faces and their attributes are defined in the
412variable `font-lock-face-attributes', and Font Lock mode default settings in
9bfbb130
SM
413the variable `font-lock-defaults-alist'. You can set your own default settings
414for some mode, by setting a buffer local value for `font-lock-defaults', via
415its mode hook.
030f4a35 416
826b2925 417Where modes support different levels of fontification, you can use the variable
fb512de9 418`font-lock-maximum-decoration' to specify which level you generally prefer.
b89e1134
SM
419When you turn Font Lock mode on/off the buffer is fontified/defontified, though
420fontification occurs only if the buffer is less than `font-lock-maximum-size'.
7d7d915a
SM
421
422To fontify a buffer, without turning on Font Lock mode and regardless of buffer
423size, you can use \\[font-lock-fontify-buffer].
a078558d
SM
424
425To fontify a block (the function or paragraph containing point, or a number of
426lines around point), perhaps because modification on the current line caused
427syntactic change on other lines, you can use \\[font-lock-fontify-block]."
030f4a35 428 (interactive "P")
a2b8e66b
SM
429 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
430 ;; batch job) or if the buffer is invisible (the name starts with a space).
343204bd
SM
431 (let ((maximum-size (font-lock-value-in-major-mode font-lock-maximum-size))
432 (on-p (and (not noninteractive)
a2b8e66b
SM
433 (not (eq (aref (buffer-name) 0) ?\ ))
434 (if arg
435 (> (prefix-numeric-value arg) 0)
343204bd 436 (not font-lock-mode)))))
a1eb1cf1 437 (if (not on-p)
876f2438
SM
438 (remove-hook 'after-change-functions 'font-lock-after-change-function
439 t)
440 (make-local-hook 'after-change-functions)
441 (add-hook 'after-change-functions 'font-lock-after-change-function
442 nil t))
030f4a35
RS
443 (set (make-local-variable 'font-lock-mode) on-p)
444 (cond (on-p
445 (font-lock-set-defaults)
a2b8e66b 446 ;; If buffer is reverted, must clean up the state.
876f2438
SM
447 (make-local-hook 'before-revert-hook)
448 (make-local-hook 'after-revert-hook)
876f2438
SM
449 (add-hook 'before-revert-hook 'font-lock-revert-setup nil t)
450 (add-hook 'after-revert-hook 'font-lock-revert-cleanup nil t)
030f4a35 451 (run-hooks 'font-lock-mode-hook)
b89e1134
SM
452 (cond (font-lock-fontified
453 nil)
a2b8e66b
SM
454 ((or (null maximum-size) (<= (buffer-size) maximum-size)
455 (not (eq font-lock-fontify-buffer-function
456 (default-value
457 'font-lock-fontify-buffer-function))))
b89e1134
SM
458 (font-lock-fontify-buffer))
459 (font-lock-verbose
6c0fc428 460 (message "Fontifying %s...buffer too big" (buffer-name)))))
030f4a35 461 (font-lock-fontified
a2b8e66b 462 (font-lock-unfontify-buffer)
876f2438
SM
463 (remove-hook 'before-revert-hook 'font-lock-revert-setup t)
464 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t)
a2b8e66b
SM
465 (font-lock-thing-lock-cleanup)
466 (font-lock-unset-defaults))
799761f0 467 (t
876f2438
SM
468 (remove-hook 'before-revert-hook 'font-lock-revert-setup t)
469 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t)
a2b8e66b
SM
470 (font-lock-thing-lock-cleanup)
471 (font-lock-unset-defaults)))
030f4a35
RS
472 (force-mode-line-update)))
473
a1eb1cf1
RS
474;;;###autoload
475(defun turn-on-font-lock ()
a465832f
SM
476 "Turn on Font Lock mode conditionally.
477Turn on only if the buffer mode supports it and the terminal can display it."
478 (if (and window-system
479 (not font-lock-mode)
480 (or font-lock-defaults (assq major-mode font-lock-defaults-alist)))
481 (font-lock-mode t)))
a2b8e66b
SM
482\f
483;; Code for Global Font Lock mode.
484
485;; A few people have hassled in the past for a way to make it easier to turn on
486;; Font Lock mode, perhaps the same way hilit19.el/hl319.el does. I've always
487;; balked at that way, as I see it as just re-moulding the same problem in
488;; another form. That is; some person would still have to keep track of which
489;; modes (which may not even be distributed with Emacs) support Font Lock mode.
490;; The list would always be out of date. And that person might have to be me.
491
492;; In the latest of these discussions the following hack came to mind. It is a
493;; gross hack, but it generally works. We use the convention that major modes
494;; start by calling the function `kill-all-local-variables', which in turn runs
495;; functions on the hook variable `change-major-mode-hook'. We attach our
496;; function `font-lock-change-major-mode' to that hook. Of course, when this
497;; hook is run, the major mode is in the process of being changed and we do not
498;; know what the final major mode will be. So, `font-lock-change-major-mode'
499;; only (a) notes the name of the current buffer, and (b) adds our function
a465832f 500;; `turn-on-font-lock-if-enabled' to the hook variable `post-command-hook'.
a2b8e66b
SM
501;; By the time the functions on `post-command-hook' are run, the new major mode
502;; is assumed to be in place.
503
504;; Naturally this requires that (a) major modes run `kill-all-local-variables',
505;; as they are supposed to do, and (b) the major mode is in place after the
506;; command that ran `kill-all-local-variables' has finished. Arguably, any
507;; major mode that does not follow the convension (a) is broken, and I can't
508;; think of any reason why (b) would not be met. I don't know of any major
509;; modes that do not follow the convension (a), but I'm sure there are some
510;; obscure ones out there somewhere. Even if it works, it is still not clean.
511
512;; Probably the cleanest solution is to have each major mode function run some
513;; hook, e.g., `major-mode-hook', but maybe implementing that change is
514;; impractical. I am personally against making `setq' a macro or be advised
515;; (space'n'speed), or have a special function such as `set-major-mode' (a
516;; `major-mode-hook' is simpler), but maybe someone can come up with another
517;; solution? --sm.
518
2d9cdda8
SM
519(defvar font-lock-buffers nil) ; For remembering buffers.
520(defvar change-major-mode-hook nil) ; Make sure it's not void.
a078558d 521
a2b8e66b
SM
522;;;###autoload
523(defvar font-lock-global-modes t
2d9cdda8 524 "*Modes for which Font Lock mode is automatically turned on.
a2b8e66b
SM
525Global Font Lock mode is controlled by the `global-font-lock-mode' command.
526If nil, means no modes have Font Lock mode automatically turned on.
527If t, all modes that support Font Lock mode have it automatically turned on.
528If a list, each element should be a major mode symbol name such as `c-mode'.
529Font Lock is automatically turned on if the buffer major mode supports it and
530is in this list. The sense of the list is negated if it begins with `not'.")
531
532;;;###autoload
343204bd 533(defun global-font-lock-mode (&optional arg message)
a2b8e66b 534 "Toggle Global Font Lock mode.
343204bd
SM
535With prefix ARG, turn Global Font Lock mode on if and only if ARG is positive.
536Displays a message saying whether the mode is on or off if MESSAGE is non-nil.
537Returns the new status of Global Font Lock mode (non-nil means on).
a2b8e66b
SM
538
539When Global Font Lock mode is enabled, Font Lock mode is automagically
540turned on in a buffer if its major mode is one of `font-lock-global-modes'."
343204bd
SM
541 (interactive "P\np")
542 (let ((off-p (if arg
543 (<= (prefix-numeric-value arg) 0)
544 (memq 'font-lock-change-major-mode change-major-mode-hook))))
545 (if off-p
546 (remove-hook 'change-major-mode-hook 'font-lock-change-major-mode)
547 (add-hook 'change-major-mode-hook 'font-lock-change-major-mode)
548 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
6c0fc428
RS
549 ;; This is to make sure we fontify immediately
550 ;; when process filters (such as GUD) find files.
551 (add-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
2d9cdda8 552 (setq font-lock-buffers (buffer-list)))
343204bd
SM
553 (if message
554 (message "Global Font Lock mode is now %s." (if off-p "OFF" "ON")))
555 (not off-p)))
a2b8e66b 556
a2b8e66b
SM
557(defun font-lock-change-major-mode ()
558 ;; Gross hack warning: Delicate readers should avert eyes now.
343204bd
SM
559 ;; Something is running `kill-all-local-variables', which generally means the
560 ;; major mode is being changed. Run `turn-on-font-lock-if-enabled' after the
561 ;; current command has finished.
a465832f 562 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
2d9cdda8 563 (add-to-list 'font-lock-buffers (current-buffer)))
a2b8e66b 564
a465832f 565(defun turn-on-font-lock-if-enabled ()
a2b8e66b 566 ;; Gross hack warning: Delicate readers should avert eyes now.
a465832f
SM
567 ;; Turn on Font Lock mode if it's one of `font-lock-global-modes'.
568 (remove-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
2d9cdda8
SM
569 (while font-lock-buffers
570 (if (buffer-live-p (car font-lock-buffers))
a2b8e66b 571 (save-excursion
2d9cdda8 572 (set-buffer (car font-lock-buffers))
a465832f
SM
573 (if (or (eq font-lock-global-modes t)
574 (if (eq (car-safe font-lock-global-modes) 'not)
575 (not (memq major-mode (cdr font-lock-global-modes)))
576 (memq major-mode font-lock-global-modes)))
343204bd
SM
577 (let (inhibit-quit)
578 (turn-on-font-lock)))))
2d9cdda8 579 (setq font-lock-buffers (cdr font-lock-buffers))))
a2b8e66b
SM
580
581;; End of Global Font Lock mode.
582\f
583;; Fontification functions.
a1eb1cf1 584
a1eb1cf1 585;;;###autoload
030f4a35 586(defun font-lock-fontify-buffer ()
a1eb1cf1 587 "Fontify the current buffer the way `font-lock-mode' would."
030f4a35 588 (interactive)
a2b8e66b
SM
589 (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
590 (funcall font-lock-fontify-buffer-function)))
591
592(defun font-lock-unfontify-buffer ()
593 (funcall font-lock-unfontify-buffer-function))
594
595(defun font-lock-fontify-region (beg end &optional loudly)
596 (funcall font-lock-fontify-region-function beg end loudly))
597
598(defun font-lock-unfontify-region (beg end)
599 (funcall font-lock-unfontify-region-function beg end))
600
601(defun font-lock-default-fontify-buffer ()
602 (let ((verbose (and font-lock-verbose (> (buffer-size) 0))))
a1eb1cf1 603 (if verbose (message "Fontifying %s..." (buffer-name)))
7d7d915a
SM
604 ;; Make sure we have the right `font-lock-keywords' etc.
605 (if (not font-lock-mode) (font-lock-set-defaults))
606 ;; Make sure we fontify etc. in the whole buffer.
607 (save-restriction
608 (widen)
609 (condition-case nil
610 (save-excursion
611 (save-match-data
612 (font-lock-fontify-region (point-min) (point-max) verbose)
271c888a 613 (font-lock-after-fontify-buffer)
7d7d915a
SM
614 (setq font-lock-fontified t)))
615 ;; We don't restore the old fontification, so it's best to unfontify.
a078558d 616 (quit (font-lock-unfontify-buffer))))
6c0fc428 617 (if verbose (message "Fontifying %s...%s" (buffer-name)
a078558d 618 (if font-lock-fontified "done" "aborted")))))
7d7d915a 619
a2b8e66b
SM
620(defun font-lock-default-unfontify-buffer ()
621 (save-restriction
622 (widen)
623 (font-lock-unfontify-region (point-min) (point-max))
271c888a 624 (font-lock-after-unfontify-buffer)
a2b8e66b 625 (setq font-lock-fontified nil)))
9bfbb130
SM
626
627;; We use this wrapper. However, `font-lock-fontify-region' used to be the
628;; name used for `font-lock-fontify-syntactically-region', so a change isn't
629;; back-compatible. But you shouldn't be calling these directly, should you?
a2b8e66b 630(defun font-lock-default-fontify-region (beg end loudly)
876f2438
SM
631 (let ((modified (buffer-modified-p))
632 (buffer-undo-list t) (inhibit-read-only t)
633 (old-syntax-table (syntax-table))
7d7d915a 634 before-change-functions after-change-functions
876f2438
SM
635 buffer-file-name buffer-file-truename)
636 (unwind-protect
a078558d
SM
637 (save-restriction
638 (widen)
876f2438
SM
639 ;; Use the fontification syntax table, if any.
640 (if font-lock-syntax-table (set-syntax-table font-lock-syntax-table))
641 ;; Now do the fontification.
642 (if font-lock-keywords-only
643 (font-lock-unfontify-region beg end)
644 (font-lock-fontify-syntactically-region beg end loudly))
645 (font-lock-fontify-keywords-region beg end loudly))
646 ;; Clean up.
647 (set-syntax-table old-syntax-table)
648 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
9bfbb130
SM
649
650;; The following must be rethought, since keywords can override fontification.
651; ;; Now scan for keywords, but not if we are inside a comment now.
652; (or (and (not font-lock-keywords-only)
653; (let ((state (parse-partial-sexp beg end nil nil
654; font-lock-cache-state)))
655; (or (nth 4 state) (nth 7 state))))
656; (font-lock-fontify-keywords-region beg end))
657
a2b8e66b 658(defun font-lock-default-unfontify-region (beg end)
9bfbb130
SM
659 (let ((modified (buffer-modified-p))
660 (buffer-undo-list t) (inhibit-read-only t)
7d7d915a 661 before-change-functions after-change-functions
9bfbb130
SM
662 buffer-file-name buffer-file-truename)
663 (remove-text-properties beg end '(face nil))
876f2438 664 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil))))
9bfbb130
SM
665
666;; Called when any modification is made to buffer text.
667(defun font-lock-after-change-function (beg end old-len)
668 (save-excursion
669 (save-match-data
670 ;; Rescan between start of line from `beg' and start of line after `end'.
671 (font-lock-fontify-region
672 (progn (goto-char beg) (beginning-of-line) (point))
673 (progn (goto-char end) (forward-line 1) (point))))))
a2b8e66b 674
a078558d
SM
675(defun font-lock-fontify-block (&optional arg)
676 "Fontify some lines the way `font-lock-fontify-buffer' would.
677The lines could be a function or paragraph, or a specified number of lines.
a078558d 678If ARG is given, fontify that many lines before and after point, or 16 lines if
a465832f
SM
679no ARG is given and `font-lock-mark-block-function' is nil.
680If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
681delimit the region to fontify."
a078558d 682 (interactive "P")
a465832f 683 (let (font-lock-beginning-of-syntax-function deactivate-mark)
a078558d
SM
684 ;; Make sure we have the right `font-lock-keywords' etc.
685 (if (not font-lock-mode) (font-lock-set-defaults))
a2b8e66b
SM
686 (save-excursion
687 (save-match-data
688 (condition-case error-data
a078558d
SM
689 (if (or arg (not font-lock-mark-block-function))
690 (let ((lines (if arg (prefix-numeric-value arg) 16)))
691 (font-lock-fontify-region
692 (save-excursion (forward-line (- lines)) (point))
693 (save-excursion (forward-line lines) (point))))
694 (funcall font-lock-mark-block-function)
695 (font-lock-fontify-region (point) (mark)))
6c0fc428 696 ((error quit) (message "Fontifying block...%s" error-data)))))))
a078558d 697
94de9afe 698(define-key facemenu-keymap "\M-g" 'font-lock-fontify-block)
9bfbb130
SM
699\f
700;; Syntactic fontification functions.
701
a078558d
SM
702;; These record the parse state at a particular position, always the start of a
703;; line. Used to make `font-lock-fontify-syntactically-region' faster.
704(defvar font-lock-cache-position nil)
705(defvar font-lock-cache-state nil)
706(make-variable-buffer-local 'font-lock-cache-position)
707(make-variable-buffer-local 'font-lock-cache-state)
708
9bfbb130
SM
709(defun font-lock-fontify-syntactically-region (start end &optional loudly)
710 "Put proper face on each string and comment between START and END.
711START should be at the beginning of a line."
876f2438 712 (let ((synstart (if comment-start-skip
9bfbb130
SM
713 (concat "\\s\"\\|" comment-start-skip)
714 "\\s\""))
715 (comstart (if comment-start-skip
716 (concat "\\s<\\|" comment-start-skip)
717 "\\s<"))
9bfbb130
SM
718 state prev prevstate)
719 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
a078558d
SM
720 (goto-char start)
721 ;;
722 ;; Find the state at the `beginning-of-line' before `start'.
723 (if (eq start font-lock-cache-position)
724 ;; Use the cache for the state of `start'.
725 (setq state font-lock-cache-state)
726 ;; Find the state of `start'.
727 (if (null font-lock-beginning-of-syntax-function)
728 ;; Use the state at the previous cache position, if any, or
729 ;; otherwise calculate from `point-min'.
730 (if (or (null font-lock-cache-position)
731 (< start font-lock-cache-position))
732 (setq state (parse-partial-sexp (point-min) start))
733 (setq state (parse-partial-sexp font-lock-cache-position start
734 nil nil font-lock-cache-state)))
735 ;; Call the function to move outside any syntactic block.
736 (funcall font-lock-beginning-of-syntax-function)
737 (setq state (parse-partial-sexp (point) start)))
738 ;; Cache the state and position of `start'.
739 (setq font-lock-cache-state state
740 font-lock-cache-position start))
741 ;;
742 ;; If the region starts inside a string, show the extent of it.
743 (if (nth 3 state)
744 (let ((beg (point)))
745 (while (and (re-search-forward "\\s\"" end 'move)
746 (nth 3 (parse-partial-sexp beg (point) nil nil state))))
747 (put-text-property beg (point) 'face font-lock-string-face)
748 (setq state (parse-partial-sexp beg (point) nil nil state))))
749 ;;
750 ;; Likewise for a comment.
751 (if (or (nth 4 state) (nth 7 state))
752 (let ((beg (point)))
753 (save-restriction
754 (narrow-to-region (point-min) end)
755 (condition-case nil
756 (progn
757 (re-search-backward comstart (point-min) 'move)
758 (forward-comment 1)
759 ;; forward-comment skips all whitespace,
760 ;; so go back to the real end of the comment.
761 (skip-chars-backward " \t"))
762 (error (goto-char end))))
763 (put-text-property beg (point) 'face font-lock-comment-face)
764 (setq state (parse-partial-sexp beg (point) nil nil state))))
765 ;;
766 ;; Find each interesting place between here and `end'.
767 (while (and (< (point) end)
768 (setq prev (point) prevstate state)
769 (re-search-forward synstart end t)
770 (progn
771 ;; Clear out the fonts of what we skip over.
772 (remove-text-properties prev (point) '(face nil))
773 ;; Verify the state at that place
774 ;; so we don't get fooled by \" or \;.
775 (setq state (parse-partial-sexp prev (point)
776 nil nil state))))
777 (let ((here (point)))
778 (if (or (nth 4 state) (nth 7 state))
779 ;;
780 ;; We found a real comment start.
781 (let ((beg (match-beginning 0)))
782 (goto-char beg)
783 (save-restriction
784 (narrow-to-region (point-min) end)
785 (condition-case nil
786 (progn
787 (forward-comment 1)
788 ;; forward-comment skips all whitespace,
789 ;; so go back to the real end of the comment.
790 (skip-chars-backward " \t"))
791 (error (goto-char end))))
792 (put-text-property beg (point) 'face font-lock-comment-face)
793 (setq state (parse-partial-sexp here (point) nil nil state)))
794 (if (nth 3 state)
876f2438 795 ;;
a078558d 796 ;; We found a real string start.
876f2438 797 (let ((beg (match-beginning 0)))
a078558d
SM
798 (while (and (re-search-forward "\\s\"" end 'move)
799 (nth 3 (parse-partial-sexp here (point)
800 nil nil state))))
801 (put-text-property beg (point) 'face font-lock-string-face)
802 (setq state (parse-partial-sexp here (point)
803 nil nil state))))))
804 ;;
805 ;; Make sure `prev' is non-nil after the loop
806 ;; only if it was set on the very last iteration.
807 (setq prev nil))
876f2438
SM
808 ;;
809 ;; Clean up.
810 (and prev (remove-text-properties prev end '(face nil)))))
9bfbb130
SM
811\f
812;;; Additional text property functions.
813
814;; The following three text property functions are not generally available (and
815;; it's not certain that they should be) so they are inlined for speed.
816;; The case for `fillin-text-property' is simple; it may or not be generally
817;; useful. (Since it is used here, it is useful in at least one place.;-)
818;; However, the case for `append-text-property' and `prepend-text-property' is
819;; more complicated. Should they remove duplicate property values or not? If
820;; so, should the first or last duplicate item remain? Or the one that was
821;; added? In our implementation, the first duplicate remains.
822
823(defsubst font-lock-fillin-text-property (start end prop value &optional object)
824 "Fill in one property of the text from START to END.
825Arguments PROP and VALUE specify the property and value to put where none are
826already in place. Therefore existing property values are not overwritten.
827Optional argument OBJECT is the string or buffer containing the text."
828 (let ((start (text-property-any start end prop nil object)) next)
829 (while start
830 (setq next (next-single-property-change start prop object end))
831 (put-text-property start next prop value object)
832 (setq start (text-property-any next end prop nil object)))))
833
834;; This function (from simon's unique.el) is rewritten and inlined for speed.
835;(defun unique (list function)
836; "Uniquify LIST, deleting elements using FUNCTION.
837;Return the list with subsequent duplicate items removed by side effects.
838;FUNCTION is called with an element of LIST and a list of elements from LIST,
839;and should return the list of elements with occurrences of the element removed,
840;i.e., a function such as `delete' or `delq'.
841;This function will work even if LIST is unsorted. See also `uniq'."
842; (let ((list list))
843; (while list
844; (setq list (setcdr list (funcall function (car list) (cdr list))))))
845; list)
846
847(defsubst font-lock-unique (list)
848 "Uniquify LIST, deleting elements using `delq'.
849Return the list with subsequent duplicate items removed by side effects."
850 (let ((list list))
851 (while list
852 (setq list (setcdr list (delq (car list) (cdr list))))))
853 list)
854
855;; A generalisation of `facemenu-add-face' for any property, but without the
856;; removal of inactive faces via `facemenu-discard-redundant-faces' and special
857;; treatment of `default'. Uses `unique' to remove duplicate property values.
858(defsubst font-lock-prepend-text-property (start end prop value &optional object)
859 "Prepend to one property of the text from START to END.
860Arguments PROP and VALUE specify the property and value to prepend to the value
861already in place. The resulting property values are always lists, and unique.
862Optional argument OBJECT is the string or buffer containing the text."
863 (let ((val (if (listp value) value (list value))) next prev)
864 (while (/= start end)
865 (setq next (next-single-property-change start prop object end)
866 prev (get-text-property start prop object))
867 (put-text-property
868 start next prop
869 (font-lock-unique (append val (if (listp prev) prev (list prev))))
870 object)
871 (setq start next))))
872
873(defsubst font-lock-append-text-property (start end prop value &optional object)
874 "Append to one property of the text from START to END.
875Arguments PROP and VALUE specify the property and value to append to the value
876already in place. The resulting property values are always lists, and unique.
877Optional argument OBJECT is the string or buffer containing the text."
878 (let ((val (if (listp value) value (list value))) next prev)
879 (while (/= start end)
880 (setq next (next-single-property-change start prop object end)
881 prev (get-text-property start prop object))
882 (put-text-property
883 start next prop
884 (font-lock-unique (append (if (listp prev) prev (list prev)) val))
885 object)
886 (setq start next))))
887\f
888;;; Regexp fontification functions.
889
890(defsubst font-lock-apply-highlight (highlight)
891 "Apply HIGHLIGHT following a match.
892HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
893 (let* ((match (nth 0 highlight))
894 (start (match-beginning match)) (end (match-end match))
895 (override (nth 2 highlight)))
896 (cond ((not start)
897 ;; No match but we might not signal an error.
898 (or (nth 3 highlight)
899 (error "No match %d in highlight %S" match highlight)))
900 ((not override)
901 ;; Cannot override existing fontification.
902 (or (text-property-not-all start end 'face nil)
903 (put-text-property start end 'face (eval (nth 1 highlight)))))
904 ((eq override t)
905 ;; Override existing fontification.
906 (put-text-property start end 'face (eval (nth 1 highlight))))
907 ((eq override 'keep)
908 ;; Keep existing fontification.
909 (font-lock-fillin-text-property start end 'face
910 (eval (nth 1 highlight))))
911 ((eq override 'prepend)
912 ;; Prepend to existing fontification.
913 (font-lock-prepend-text-property start end 'face
914 (eval (nth 1 highlight))))
915 ((eq override 'append)
916 ;; Append to existing fontification.
917 (font-lock-append-text-property start end 'face
918 (eval (nth 1 highlight)))))))
919
920(defsubst font-lock-fontify-anchored-keywords (keywords limit)
921 "Fontify according to KEYWORDS until LIMIT.
922KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords'."
923 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights)
876f2438
SM
924 ;; Until we come up with a cleaner solution, we make LIMIT the end of line.
925 (save-excursion (end-of-line) (setq limit (min limit (point))))
926 ;; Evaluate PRE-MATCH-FORM.
9bfbb130
SM
927 (eval (nth 1 keywords))
928 (save-match-data
876f2438 929 ;; Find an occurrence of `matcher' before `limit'.
9bfbb130
SM
930 (while (if (stringp matcher)
931 (re-search-forward matcher limit t)
932 (funcall matcher limit))
876f2438 933 ;; Apply each highlight to this instance of `matcher'.
9bfbb130
SM
934 (setq highlights lowdarks)
935 (while highlights
936 (font-lock-apply-highlight (car highlights))
937 (setq highlights (cdr highlights)))))
876f2438 938 ;; Evaluate POST-MATCH-FORM.
9bfbb130
SM
939 (eval (nth 2 keywords))))
940
941(defun font-lock-fontify-keywords-region (start end &optional loudly)
942 "Fontify according to `font-lock-keywords' between START and END.
943START should be at the beginning of a line."
944 (let ((case-fold-search font-lock-keywords-case-fold-search)
945 (keywords (cdr (if (eq (car-safe font-lock-keywords) t)
946 font-lock-keywords
947 (font-lock-compile-keywords))))
9bfbb130 948 (bufname (buffer-name)) (count 0)
876f2438
SM
949 keyword matcher highlights)
950 ;;
951 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
952 (while keywords
953 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
954 (make-string (setq count (1+ count)) ?.)))
9bfbb130 955 ;;
876f2438
SM
956 ;; Find an occurrence of `matcher' from `start' to `end'.
957 (setq keyword (car keywords) matcher (car keyword))
958 (goto-char start)
959 (while (if (stringp matcher)
960 (re-search-forward matcher end t)
961 (funcall matcher end))
962 ;; Apply each highlight to this instance of `matcher', which may be
963 ;; specific highlights or more keywords anchored to `matcher'.
964 (setq highlights (cdr keyword))
965 (while highlights
966 (if (numberp (car (car highlights)))
967 (font-lock-apply-highlight (car highlights))
968 (font-lock-fontify-anchored-keywords (car highlights) end))
969 (setq highlights (cdr highlights))))
970 (setq keywords (cdr keywords)))))
9bfbb130
SM
971\f
972;; Various functions.
973
974;; Turn off other related packages if they're on. I prefer a hook. --sm.
975;; These explicit calls are easier to understand
976;; because people know what they will do.
977;; A hook is a mystery because it might do anything whatever. --rms.
978(defun font-lock-thing-lock-cleanup ()
979 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
980 (fast-lock-mode -1))
981 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
982 (lazy-lock-mode -1))))
983
271c888a 984;; Do something special for these packages after fontifying; I prefer a hook.
9bfbb130
SM
985(defun font-lock-after-fontify-buffer ()
986 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
987 (fast-lock-after-fontify-buffer))
988 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
989 (lazy-lock-after-fontify-buffer))))
990
271c888a
SM
991;; Do something special for these packages after unfontifying; I prefer a hook.
992(defun font-lock-after-unfontify-buffer ()
993 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
994 (fast-lock-after-unfontify-buffer))
995 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
996 (lazy-lock-after-unfontify-buffer))))
997
9bfbb130
SM
998;; If the buffer is about to be reverted, it won't be fontified afterward.
999(defun font-lock-revert-setup ()
1000 (setq font-lock-fontified nil))
1001
1002;; If the buffer has just been reverted, normally that turns off
1003;; Font Lock mode. So turn the mode back on if necessary.
7d7d915a
SM
1004(defalias 'font-lock-revert-cleanup
1005 'turn-on-font-lock)
9bfbb130
SM
1006
1007(defun font-lock-compile-keywords (&optional keywords)
1008 ;; Compile `font-lock-keywords' into the form (t KEYWORD ...) where KEYWORD
1009 ;; is the (MATCHER HIGHLIGHT ...) shown in the variable's doc string.
1010 (let ((keywords (or keywords font-lock-keywords)))
1011 (setq font-lock-keywords
a2b8e66b
SM
1012 (if (eq (car-safe keywords) t)
1013 keywords
1014 (cons t (mapcar 'font-lock-compile-keyword keywords))))))
1015
1016(defun font-lock-compile-keyword (keyword)
1017 (cond ((nlistp keyword) ; Just MATCHER
1018 (list keyword '(0 font-lock-keyword-face)))
1019 ((eq (car keyword) 'eval) ; Specified (eval . FORM)
1020 (font-lock-compile-keyword (eval (cdr keyword))))
1021 ((numberp (cdr keyword)) ; Specified (MATCHER . MATCH)
1022 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
1023 ((symbolp (cdr keyword)) ; Specified (MATCHER . FACENAME)
1024 (list (car keyword) (list 0 (cdr keyword))))
1025 ((nlistp (nth 1 keyword)) ; Specified (MATCHER . HIGHLIGHT)
1026 (list (car keyword) (cdr keyword)))
1027 (t ; Hopefully (MATCHER HIGHLIGHT ...)
1028 keyword)))
d46c21ec 1029
343204bd
SM
1030(defun font-lock-value-in-major-mode (alist)
1031 ;; Return value in ALIST for `major-mode', or ALIST if it is not an alist.
1032 ;; Alist structure is ((MAJOR-MODE . VALUE)) where MAJOR-MODE may be t.
1033 (if (consp alist)
1034 (cdr (or (assq major-mode alist) (assq t alist)))
1035 alist))
1036
d46c21ec
SM
1037(defun font-lock-choose-keywords (keywords level)
1038 ;; Return LEVELth element of KEYWORDS. A LEVEL of nil is equal to a
1039 ;; LEVEL of 0, a LEVEL of t is equal to (1- (length KEYWORDS)).
343204bd
SM
1040 (cond ((symbolp keywords)
1041 keywords)
1042 ((numberp level)
1043 (or (nth level keywords) (car (reverse keywords))))
1044 ((eq level t)
1045 (car (reverse keywords)))
1046 (t
1047 (car keywords))))
d46c21ec
SM
1048
1049(defun font-lock-set-defaults ()
1050 "Set fontification defaults appropriately for this mode.
a2b8e66b
SM
1051Sets various variables using `font-lock-defaults' (or, if nil, using
1052`font-lock-defaults-alist') and `font-lock-maximum-decoration'."
d46c21ec
SM
1053 ;; Set face defaults.
1054 (font-lock-make-faces)
1055 ;; Set fontification defaults.
a2b8e66b 1056 (make-local-variable 'font-lock-fontified)
2d9cdda8
SM
1057 (if (member font-lock-keywords '(nil (t)))
1058 (let* ((defaults (or font-lock-defaults
1059 (cdr (assq major-mode font-lock-defaults-alist))))
1060 (keywords
1061 (font-lock-choose-keywords (nth 0 defaults)
1062 (font-lock-value-in-major-mode font-lock-maximum-decoration))))
1063 ;; Regexp fontification?
1064 (setq font-lock-keywords (if (fboundp keywords)
1065 (funcall keywords)
1066 (eval keywords)))
1067 ;; Syntactic fontification?
1068 (if (nth 1 defaults)
1069 (set (make-local-variable 'font-lock-keywords-only) t))
1070 ;; Case fold during regexp fontification?
1071 (if (nth 2 defaults)
1072 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
1073 ;; Syntax table for regexp and syntactic fontification?
1074 (if (nth 3 defaults)
1075 (let ((slist (nth 3 defaults)))
1076 (set (make-local-variable 'font-lock-syntax-table)
1077 (copy-syntax-table (syntax-table)))
1078 (while slist
1079 (modify-syntax-entry (car (car slist)) (cdr (car slist))
1080 font-lock-syntax-table)
1081 (setq slist (cdr slist)))))
1082 ;; Syntax function for syntactic fontification?
1083 (if (nth 4 defaults)
1084 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1085 (nth 4 defaults)))
1086 ;; Variable alist?
1087 (let ((alist (nthcdr 5 defaults)))
1088 (while alist
1089 (set (make-local-variable (car (car alist))) (cdr (car alist)))
1090 (setq alist (cdr alist)))))))
a2b8e66b
SM
1091
1092(defun font-lock-unset-defaults ()
1093 "Unset fontification defaults. See `font-lock-set-defaults'."
1094 (setq font-lock-keywords nil
1095 font-lock-keywords-only nil
1096 font-lock-keywords-case-fold-search nil
1097 font-lock-syntax-table nil
a078558d
SM
1098 font-lock-beginning-of-syntax-function nil)
1099 (let* ((defaults (or font-lock-defaults
1100 (cdr (assq major-mode font-lock-defaults-alist))))
1101 (alist (nthcdr 5 defaults)))
1102 (while alist
1103 (set (car (car alist)) (default-value (car (car alist))))
1104 (setq alist (cdr alist)))))
030f4a35 1105\f
9bfbb130
SM
1106;; Colour etc. support.
1107
a078558d
SM
1108;; This section of code is crying out for revision.
1109
1110;; To begin with, `display-type' and `background-mode' are `frame-parameters'
1111;; so we don't have to calculate them here anymore. But all the face stuff
1112;; should be frame-local (and thus display-local) anyway. Because we're not
1113;; sure what support Emacs is going to have for general frame-local face
1114;; attributes, we leave this section of code as it is. For now. --sm.
1115
9bfbb130
SM
1116(defvar font-lock-display-type nil
1117 "A symbol indicating the display Emacs is running under.
1118The symbol should be one of `color', `grayscale' or `mono'.
1119If Emacs guesses this display attribute wrongly, either set this variable in
1120your `~/.emacs' or set the resource `Emacs.displayType' in your `~/.Xdefaults'.
1121See also `font-lock-background-mode' and `font-lock-face-attributes'.")
1122
1123(defvar font-lock-background-mode nil
1124 "A symbol indicating the Emacs background brightness.
1125The symbol should be one of `light' or `dark'.
1126If Emacs guesses this frame attribute wrongly, either set this variable in
1127your `~/.emacs' or set the resource `Emacs.backgroundMode' in your
1128`~/.Xdefaults'.
1129See also `font-lock-display-type' and `font-lock-face-attributes'.")
1130
1131(defvar font-lock-face-attributes nil
1132 "A list of default attributes to use for face attributes.
1133Each element of the list should be of the form
1134
1135 (FACE FOREGROUND BACKGROUND BOLD-P ITALIC-P UNDERLINE-P)
1136
1137where FACE should be one of the face symbols `font-lock-comment-face',
1138`font-lock-string-face', `font-lock-keyword-face', `font-lock-type-face',
1139`font-lock-function-name-face', `font-lock-variable-name-face', and
1140`font-lock-reference-face'. A form for each of these face symbols should be
1141provided in the list, but other face symbols and attributes may be given and
1142used in highlighting. See `font-lock-keywords'.
1143
1144Subsequent element items should be the attributes for the corresponding
1145Font Lock mode faces. Attributes FOREGROUND and BACKGROUND should be strings
1146\(default if nil), while BOLD-P, ITALIC-P, and UNDERLINE-P should specify the
1147corresponding face attributes (yes if non-nil).
1148
1149Emacs uses default attributes based on display type and background brightness.
1150See variables `font-lock-display-type' and `font-lock-background-mode'.
1151
1152Resources can be used to over-ride these face attributes. For example, the
1153resource `Emacs.font-lock-comment-face.attributeUnderline' can be used to
1154specify the UNDERLINE-P attribute for face `font-lock-comment-face'.")
1155
1156(defun font-lock-make-faces (&optional override)
1157 "Make faces from `font-lock-face-attributes'.
1158A default list is used if this is nil.
1159If optional OVERRIDE is non-nil, faces that already exist are reset.
1160See `font-lock-make-face' and `list-faces-display'."
1161 ;; We don't need to `setq' any of these variables, but the user can see what
1162 ;; is being used if we do.
1163 (if (null font-lock-display-type)
1164 (setq font-lock-display-type
1165 (let ((display-resource (x-get-resource ".displayType"
1166 "DisplayType")))
1167 (cond (display-resource (intern (downcase display-resource)))
1168 ((x-display-color-p) 'color)
1169 ((x-display-grayscale-p) 'grayscale)
1170 (t 'mono)))))
1171 (if (null font-lock-background-mode)
1172 (setq font-lock-background-mode
1173 (let ((bg-resource (x-get-resource ".backgroundMode"
1174 "BackgroundMode"))
1175 (params (frame-parameters)))
1176 (cond (bg-resource (intern (downcase bg-resource)))
03a84c34
RS
1177 ((eq system-type 'ms-dos)
1178 (if (string-match "light"
1179 (cdr (assq 'background-color params)))
1180 'light
1181 'dark))
9bfbb130
SM
1182 ((< (apply '+ (x-color-values
1183 (cdr (assq 'background-color params))))
f42b62e4 1184 (* (apply '+ (x-color-values "white")) .6))
9bfbb130
SM
1185 'dark)
1186 (t 'light)))))
1187 (if (null font-lock-face-attributes)
1188 (setq font-lock-face-attributes
1189 (let ((light-bg (eq font-lock-background-mode 'light)))
1190 (cond ((memq font-lock-display-type '(mono monochrome))
1191 ;; Emacs 19.25's font-lock defaults:
1192 ;;'((font-lock-comment-face nil nil nil t nil)
1193 ;; (font-lock-string-face nil nil nil nil t)
1194 ;; (font-lock-keyword-face nil nil t nil nil)
1195 ;; (font-lock-function-name-face nil nil t t nil)
1196 ;; (font-lock-type-face nil nil nil t nil))
1197 (list '(font-lock-comment-face nil nil t t nil)
1198 '(font-lock-string-face nil nil nil t nil)
1199 '(font-lock-keyword-face nil nil t nil nil)
1200 (list
1201 'font-lock-function-name-face
1202 (cdr (assq 'background-color (frame-parameters)))
1203 (cdr (assq 'foreground-color (frame-parameters)))
1204 t nil nil)
1205 '(font-lock-variable-name-face nil nil t t nil)
1206 '(font-lock-type-face nil nil t nil t)
1207 '(font-lock-reference-face nil nil t nil t)))
1208 ((memq font-lock-display-type '(grayscale greyscale
1209 grayshade greyshade))
1210 (list
1211 (list 'font-lock-comment-face
1212 nil (if light-bg "Gray80" "DimGray") t t nil)
1213 (list 'font-lock-string-face
1214 nil (if light-bg "Gray50" "LightGray") nil t nil)
1215 (list 'font-lock-keyword-face
1216 nil (if light-bg "Gray90" "DimGray") t nil nil)
1217 (list 'font-lock-function-name-face
1218 (cdr (assq 'background-color (frame-parameters)))
1219 (cdr (assq 'foreground-color (frame-parameters)))
1220 t nil nil)
1221 (list 'font-lock-variable-name-face
1222 nil (if light-bg "Gray90" "DimGray") t t nil)
1223 (list 'font-lock-type-face
1224 nil (if light-bg "Gray80" "DimGray") t nil t)
1225 (list 'font-lock-reference-face
1226 nil (if light-bg "LightGray" "Gray50") t nil t)))
1227 (light-bg ; light colour background
1228 '((font-lock-comment-face "Firebrick")
1229 (font-lock-string-face "RosyBrown")
1230 (font-lock-keyword-face "Purple")
1231 (font-lock-function-name-face "Blue")
1232 (font-lock-variable-name-face "DarkGoldenrod")
1233 (font-lock-type-face "DarkOliveGreen")
1234 (font-lock-reference-face "CadetBlue")))
1235 (t ; dark colour background
1236 '((font-lock-comment-face "OrangeRed")
1237 (font-lock-string-face "LightSalmon")
1238 (font-lock-keyword-face "LightSteelBlue")
1239 (font-lock-function-name-face "LightSkyBlue")
1240 (font-lock-variable-name-face "LightGoldenrod")
1241 (font-lock-type-face "PaleGreen")
1242 (font-lock-reference-face "Aquamarine")))))))
1243 ;; Now make the faces if we have to.
1244 (mapcar (function
1245 (lambda (face-attributes)
1246 (let ((face (nth 0 face-attributes)))
1247 (cond (override
1248 ;; We can stomp all over it anyway. Get outta my face!
1249 (font-lock-make-face face-attributes))
1250 ((and (boundp face) (facep (symbol-value face)))
1251 ;; The variable exists and is already bound to a face.
1252 nil)
1253 ((facep face)
1254 ;; We already have a face so we bind the variable to it.
1255 (set face face))
1256 (t
1257 ;; No variable or no face.
1258 (font-lock-make-face face-attributes))))))
1259 font-lock-face-attributes))
1260
1261(defun font-lock-make-face (face-attributes)
1262 "Make a face from FACE-ATTRIBUTES.
1263FACE-ATTRIBUTES should be like an element `font-lock-face-attributes', so that
1264the face name is the first item in the list. A variable with the same name as
1265the face is also set; its value is the face name."
1266 (let* ((face (nth 0 face-attributes))
1267 (face-name (symbol-name face))
1268 (set-p (function (lambda (face-name resource)
1269 (x-get-resource (concat face-name ".attribute" resource)
1270 (concat "Face.Attribute" resource)))))
1271 (on-p (function (lambda (face-name resource)
1272 (let ((set (funcall set-p face-name resource)))
1273 (and set (member (downcase set) '("on" "true"))))))))
1274 (make-face face)
876f2438 1275 (add-to-list 'facemenu-unlisted-faces face)
9bfbb130
SM
1276 ;; Set attributes not set from X resources (and therefore `make-face').
1277 (or (funcall set-p face-name "Foreground")
1278 (condition-case nil
1279 (set-face-foreground face (nth 1 face-attributes))
1280 (error nil)))
1281 (or (funcall set-p face-name "Background")
1282 (condition-case nil
1283 (set-face-background face (nth 2 face-attributes))
1284 (error nil)))
1285 (if (funcall set-p face-name "Bold")
1286 (and (funcall on-p face-name "Bold") (make-face-bold face nil t))
1287 (and (nth 3 face-attributes) (make-face-bold face nil t)))
1288 (if (funcall set-p face-name "Italic")
1289 (and (funcall on-p face-name "Italic") (make-face-italic face nil t))
1290 (and (nth 4 face-attributes) (make-face-italic face nil t)))
1291 (or (funcall set-p face-name "Underline")
1292 (set-face-underline-p face (nth 5 face-attributes)))
1293 (set face face)))
1294\f
1295;;; Various regexp information shared by several modes.
a1eb1cf1 1296;;; Information specific to a single mode should go in its load library.
030f4a35 1297
030f4a35 1298(defconst lisp-font-lock-keywords-1
a1eb1cf1 1299 (list
9bfbb130 1300 ;; Anything not a variable or type declaration is fontified as a function.
d46c21ec
SM
1301 ;; It would be cleaner to allow preceding whitespace, but it would also be
1302 ;; about five times slower.
1303 (list (concat "^(\\(def\\("
1304 ;; Variable declarations.
1305 "\\(const\\(\\|ant\\)\\|ine-key\\(\\|-after\\)\\|var\\)\\|"
1306 ;; Structure declarations.
1307 "\\(class\\|struct\\|type\\)\\|"
1308 ;; Everything else is a function declaration.
1309 "\\([^ \t\n\(\)]+\\)"
1310 "\\)\\)\\>"
1311 ;; Any whitespace and declared object.
1312 "[ \t'\(]*"
7d7d915a 1313 "\\(\\sw+\\)?")
d46c21ec
SM
1314 '(1 font-lock-keyword-face)
1315 '(8 (cond ((match-beginning 3) font-lock-variable-name-face)
1316 ((match-beginning 6) font-lock-type-face)
1317 (t font-lock-function-name-face))
9bfbb130
SM
1318 nil t))
1319 )
7d7d915a 1320 "Subdued level highlighting for Lisp modes.")
030f4a35
RS
1321
1322(defconst lisp-font-lock-keywords-2
799761f0 1323 (append lisp-font-lock-keywords-1
9bfbb130
SM
1324 (list
1325 ;;
1326 ;; Control structures. ELisp and CLisp combined.
d46c21ec 1327; (make-regexp
7d7d915a 1328; '("cond" "if" "while" "let\\*?" "prog[nv12*]?" "inline" "catch" "throw"
d46c21ec
SM
1329; "save-restriction" "save-excursion" "save-window-excursion"
1330; "save-selected-window" "save-match-data" "unwind-protect"
1331; "condition-case" "track-mouse"
1332; "eval-after-load" "eval-and-compile" "eval-when-compile"
a078558d
SM
1333; "when" "unless" "do" "flet" "labels" "return" "return-from"
1334; "with-output-to-temp-buffer" "with-timeout"))
9bfbb130
SM
1335 (cons
1336 (concat
1337 "(\\("
a078558d 1338 "c\\(atch\\|ond\\(\\|ition-case\\)\\)\\|do\\|"
9bfbb130 1339 "eval-\\(a\\(fter-load\\|nd-compile\\)\\|when-compile\\)\\|flet\\|"
7d7d915a 1340 "i\\(f\\|nline\\)\\|l\\(abels\\|et\\*?\\)\\|prog[nv12*]?\\|"
a078558d
SM
1341 "return\\(\\|-from\\)\\|save-\\(excursion\\|match-data\\|restriction\\|"
1342 "selected-window\\|window-excursion\\)\\|t\\(hrow\\|rack-mouse\\)\\|"
1343 "un\\(less\\|wind-protect\\)\\|"
1344 "w\\(h\\(en\\|ile\\)\\|ith-\\(output-to-temp-buffer\\|timeout\\)\\)"
9bfbb130
SM
1345 "\\)\\>") 1)
1346 ;;
7d7d915a
SM
1347 ;; Feature symbols as references.
1348 '("(\\(featurep\\|provide\\|require\\)\\>[ \t']*\\(\\sw+\\)?"
1349 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1350 ;;
9bfbb130
SM
1351 ;; Words inside \\[] tend to be for `substitute-command-keys'.
1352 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-reference-face prepend)
1353 ;;
1354 ;; Words inside `' tend to be symbol names.
1355 '("`\\(\\sw\\sw+\\)'" 1 font-lock-reference-face prepend)
1356 ;;
1357 ;; CLisp `:' keywords as references.
1358 '("\\<:\\sw+\\>" 0 font-lock-reference-face prepend)
1359 ;;
1360 ;; ELisp and CLisp `&' keywords as types.
343204bd 1361 '("\\<\\&\\sw+\\>" . font-lock-type-face)
9bfbb130 1362 ))
fb512de9 1363 "Gaudy level highlighting for Lisp modes.")
030f4a35 1364
fb512de9
SM
1365(defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
1366 "Default expressions to highlight in Lisp modes.")
030f4a35
RS
1367
1368
d46c21ec 1369(defvar scheme-font-lock-keywords
9bfbb130
SM
1370 (eval-when-compile
1371 (list
1372 ;;
1373 ;; Declarations. Hannes Haug <hannes.haug@student.uni-tuebingen.de> says
1374 ;; this works for SOS, STklos, SCOOPS, Meroon and Tiny CLOS.
1375 (list (concat "(\\(define\\("
1376 ;; Function names.
1377 "\\(\\|-\\(generic\\(\\|-procedure\\)\\|method\\)\\)\\|"
1378 ;; Macro names, as variable names. A bit dubious, this.
1379 "\\(-syntax\\)\\|"
1380 ;; Class names.
1381 "\\(-class\\)"
1382 "\\)\\)\\>"
1383 ;; Any whitespace and declared object.
1384 "[ \t]*(?"
1385 "\\(\\sw+\\)?")
1386 '(1 font-lock-keyword-face)
1387 '(8 (cond ((match-beginning 3) font-lock-function-name-face)
1388 ((match-beginning 6) font-lock-variable-name-face)
1389 (t font-lock-type-face))
1390 nil t))
1391 ;;
1392 ;; Control structures.
d46c21ec
SM
1393;(make-regexp '("begin" "call-with-current-continuation" "call/cc"
1394; "call-with-input-file" "call-with-output-file" "case" "cond"
9bfbb130
SM
1395; "do" "else" "for-each" "if" "lambda"
1396; "let\\*?" "let-syntax" "letrec" "letrec-syntax"
1397; ;; Hannes Haug <hannes.haug@student.uni-tuebingen.de> wants:
1398; "and" "or" "delay"
1399; ;; Stefan Monnier <stefan.monnier@epfl.ch> says don't bother:
d46c21ec
SM
1400; ;;"quasiquote" "quote" "unquote" "unquote-splicing"
1401; "map" "syntax" "syntax-rules"))
9bfbb130
SM
1402 (cons
1403 (concat "(\\("
1404 "and\\|begin\\|c\\(a\\(ll\\(-with-\\(current-continuation\\|"
1405 "input-file\\|output-file\\)\\|/cc\\)\\|se\\)\\|ond\\)\\|"
1406 "d\\(elay\\|o\\)\\|else\\|for-each\\|if\\|"
1407 "l\\(ambda\\|et\\(-syntax\\|\\*?\\|rec\\(\\|-syntax\\)\\)\\)\\|"
1408 "map\\|or\\|syntax\\(\\|-rules\\)"
1409 "\\)\\>") 1)
1410 ;;
1411 ;; David Fox <fox@graphics.cs.nyu.edu> for SOS/STklos class specifiers.
1412 '("\\<<\\sw+>\\>" . font-lock-type-face)
1413 ;;
1414 ;; Scheme `:' keywords as references.
1415 '("\\<:\\sw+\\>" . font-lock-reference-face)
1416 ))
1417"Default expressions to highlight in Scheme modes.")
d46c21ec
SM
1418
1419
030f4a35 1420(defconst c-font-lock-keywords-1 nil
fb512de9 1421 "Subdued level highlighting for C modes.")
030f4a35
RS
1422
1423(defconst c-font-lock-keywords-2 nil
9bfbb130
SM
1424 "Medium level highlighting for C modes.")
1425
1426(defconst c-font-lock-keywords-3 nil
fb512de9 1427 "Gaudy level highlighting for C modes.")
030f4a35 1428
1bd50840 1429(defconst c++-font-lock-keywords-1 nil
fb512de9 1430 "Subdued level highlighting for C++ modes.")
1bd50840
RS
1431
1432(defconst c++-font-lock-keywords-2 nil
9bfbb130
SM
1433 "Medium level highlighting for C++ modes.")
1434
1435(defconst c++-font-lock-keywords-3 nil
fb512de9 1436 "Gaudy level highlighting for C++ modes.")
1bd50840 1437
9bfbb130
SM
1438(defun font-lock-match-c++-style-declaration-item-and-skip-to-next (limit)
1439 ;; Match, and move over, any declaration/definition item after point.
1440 ;; The expect syntax of an item is "word" or "word::word", possibly ending
1441 ;; with optional whitespace and a "(". Everything following the item (but
1442 ;; belonging to it) is expected to by skip-able by `forward-sexp', and items
1443 ;; are expected to be separated with a "," or ";".
1444 (if (looking-at "[ \t*&]*\\(\\sw+\\)\\(::\\(\\sw+\\)\\)?[ \t]*\\((\\)?")
1445 (save-match-data
1446 (condition-case nil
1447 (save-restriction
876f2438
SM
1448 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
1449 (narrow-to-region (point-min) limit)
9bfbb130
SM
1450 (goto-char (match-end 1))
1451 ;; Move over any item value, etc., to the next item.
1452 (while (not (looking-at "[ \t]*\\([,;]\\|$\\)"))
1453 (goto-char (or (scan-sexps (point) 1) (point-max))))
1454 (goto-char (match-end 0)))
1455 (error t)))))
1456
b89e1134
SM
1457(let ((c-keywords
1458; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while")
1459 "break\\|continue\\|do\\|else\\|for\\|if\\|return\\|switch\\|while")
1460 (c-type-types
a1eb1cf1
RS
1461; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
1462; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
b89e1134
SM
1463; "void" "volatile" "const")
1464 (concat "auto\\|c\\(har\\|onst\\)\\|double\\|e\\(num\\|xtern\\)\\|"
1465 "float\\|int\\|long\\|register\\|"
1466 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|typedef\\|"
1467 "un\\(ion\\|signed\\)\\|vo\\(id\\|latile\\)")) ; 6 ()s deep.
1468 (c++-keywords
1469; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while"
1470; "asm" "catch" "delete" "new" "operator" "sizeof" "this" "throw" "try"
1471; "protected" "private" "public")
1472 (concat "asm\\|break\\|c\\(atch\\|ontinue\\)\\|d\\(elete\\|o\\)\\|"
9bfbb130 1473 "else\\|for\\|if\\|new\\|"
b89e1134
SM
1474 "p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|return\\|"
1475 "s\\(izeof\\|witch\\)\\|t\\(h\\(is\\|row\\)\\|ry\\)\\|while"))
1476 (c++-type-types
1477; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
1478; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
1479; "void" "volatile" "const" "class" "inline" "friend" "bool"
1480; "virtual" "complex" "template")
1481 (concat "auto\\|bool\\|c\\(har\\|lass\\|o\\(mplex\\|nst\\)\\)\\|"
1482 "double\\|e\\(num\\|xtern\\)\\|f\\(loat\\|riend\\)\\|"
1483 "in\\(line\\|t\\)\\|long\\|register\\|"
1484 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|"
1485 "t\\(emplate\\|ypedef\\)\\|un\\(ion\\|signed\\)\\|"
1486 "v\\(irtual\\|o\\(id\\|latile\\)\\)")) ; 11 ()s deep.
9bfbb130 1487 )
f60f18ae
KH
1488 (setq c-font-lock-keywords-1
1489 (list
f60f18ae 1490 ;;
9bfbb130
SM
1491 ;; These are all anchored at the beginning of line for speed.
1492 ;;
1493 ;; Fontify function name definitions (GNU style; without type on line).
1494 (list (concat "^\\(\\sw+\\)[ \t]*(") 1 'font-lock-function-name-face)
1495 ;;
1496 ;; Fontify filenames in #include <...> preprocessor directives as strings.
f60f18ae
KH
1497 '("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face)
1498 ;;
a1eb1cf1 1499 ;; Fontify function macro names.
7d7d915a 1500 '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
f60f18ae 1501 ;;
9bfbb130
SM
1502 ;; Fontify symbol names in #if ... defined preprocessor directives.
1503 '("^#[ \t]*if\\>"
1504 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
1505 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t)))
1506 ;;
a1eb1cf1 1507 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
7d7d915a 1508 '("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?"
a1eb1cf1 1509 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t))
f60f18ae
KH
1510 ))
1511
1512 (setq c-font-lock-keywords-2
1513 (append c-font-lock-keywords-1
030f4a35 1514 (list
030f4a35 1515 ;;
9bfbb130 1516 ;; Simple regexps for speed.
b89e1134 1517 ;;
9bfbb130
SM
1518 ;; Fontify all type specifiers.
1519 (cons (concat "\\<\\(" c-type-types "\\)\\>") 'font-lock-type-face)
030f4a35 1520 ;;
b89e1134 1521 ;; Fontify all builtin keywords (except case, default and goto; see below).
9bfbb130 1522 (cons (concat "\\<\\(" c-keywords "\\)\\>") 'font-lock-keyword-face)
030f4a35 1523 ;;
9bfbb130 1524 ;; Fontify case/goto keywords and targets, and case default/goto tags.
7d7d915a 1525 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(\\sw+\\)?"
a1eb1cf1
RS
1526 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1527 '("^[ \t]*\\(\\sw+\\)[ \t]*:" 1 font-lock-reference-face)
f60f18ae 1528 )))
1bd50840 1529
9bfbb130
SM
1530 (setq c-font-lock-keywords-3
1531 (append c-font-lock-keywords-2
1532 ;;
1533 ;; More complicated regexps for more complete highlighting for types.
1534 ;; We still have to fontify type specifiers individually, as C is so hairy.
1535 (list
1536 ;;
1537 ;; Fontify all storage classes and type specifiers, plus their items.
1538 (list (concat "\\<\\(" c-type-types "\\)\\>"
1539 "\\([ \t*&]+\\sw+\\>\\)*")
1540 ;; Fontify each declaration item.
1541 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
1542 ;; Start with point after all type specifiers.
1543 (goto-char (or (match-beginning 8) (match-end 1)))
1544 ;; Finish with point after first type specifier.
1545 (goto-char (match-end 1))
1546 ;; Fontify as a variable or function name.
1547 (1 (if (match-beginning 4)
1548 font-lock-function-name-face
1549 font-lock-variable-name-face))))
1550 ;;
1551 ;; Fontify structures, or typedef names, plus their items.
1552 '("\\(}\\)[ \t*]*\\sw"
1553 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1554 (goto-char (match-end 1)) nil
1555 (1 (if (match-beginning 4)
1556 font-lock-function-name-face
1557 font-lock-variable-name-face))))
1558 ;;
1559 ;; Fontify anything at beginning of line as a declaration or definition.
1560 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
1561 (1 font-lock-type-face)
1562 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1563 (goto-char (or (match-beginning 2) (match-end 1))) nil
1564 (1 (if (match-beginning 4)
1565 font-lock-function-name-face
1566 font-lock-variable-name-face))))
1567 )))
1568
1569 (setq c++-font-lock-keywords-1
1570 (append
1571 ;;
1572 ;; The list `c-font-lock-keywords-1' less that for function names.
1573 (cdr c-font-lock-keywords-1)
1574 ;;
1575 ;; Fontify function name definitions, possibly incorporating class name.
1576 (list
1577 '("^\\(\\sw+\\)\\(::\\(\\sw+\\)\\)?[ \t]*("
1578 (1 (if (match-beginning 2)
1579 font-lock-type-face
1580 font-lock-function-name-face))
1581 (3 (if (match-beginning 2) font-lock-function-name-face) nil t))
1582 )))
1583
1bd50840 1584 (setq c++-font-lock-keywords-2
b89e1134 1585 (append c++-font-lock-keywords-1
a1eb1cf1 1586 (list
9bfbb130
SM
1587 ;;
1588 ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading.
b89e1134 1589 (cons (concat "\\<\\(" c++-type-types "\\)\\>") 'font-lock-type-face)
9bfbb130
SM
1590 ;;
1591 ;; Fontify operator function name overloading.
1592 '("\\<\\(operator\\)\\>[ \t]*\\([][)(><!=+-][][)(><!=+-]?\\)?"
1593 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
1594 ;;
1595 ;; Fontify case/goto keywords and targets, and case default/goto tags.
7d7d915a 1596 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(\\sw+\\)?"
b89e1134 1597 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
9bfbb130
SM
1598 '("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-reference-face)
1599 ;;
1600 ;; Fontify other builtin keywords.
1601 (cons (concat "\\<\\(" c++-keywords "\\)\\>") 'font-lock-keyword-face)
1602 )))
1603
1604 (setq c++-font-lock-keywords-3
1605 (append c++-font-lock-keywords-2
1606 ;;
1607 ;; More complicated regexps for more complete highlighting for types.
1608 (list
1609 ;;
1610 ;; Fontify all storage classes and type specifiers, plus their items.
1611 (list (concat "\\<\\(" c++-type-types "\\)\\>"
1612 "\\([ \t*&]+\\sw+\\>\\)*")
1613 ;; Fontify each declaration item.
1614 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
1615 ;; Start with point after all type specifiers.
1616 (goto-char (or (match-beginning 13) (match-end 1)))
1617 ;; Finish with point after first type specifier.
1618 (goto-char (match-end 1))
1619 ;; Fontify as a variable or function name.
1620 (1 (cond ((match-beginning 2) font-lock-type-face)
1621 ((match-beginning 4) font-lock-function-name-face)
1622 (t font-lock-variable-name-face)))
1623 (3 (if (match-beginning 4)
1624 font-lock-function-name-face
1625 font-lock-variable-name-face) nil t)))
1626 ;;
1627 ;; Fontify structures, or typedef names, plus their items.
1628 '("\\(}\\)[ \t*]*\\sw"
1629 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1630 (goto-char (match-end 1)) nil
1631 (1 (if (match-beginning 4)
1632 font-lock-function-name-face
1633 font-lock-variable-name-face))))
1634 ;;
1635 ;; Fontify anything at beginning of line as a declaration or definition.
1636 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
1637 (1 font-lock-type-face)
1638 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1639 (goto-char (or (match-beginning 2) (match-end 1))) nil
1640 (1 (cond ((match-beginning 2) font-lock-type-face)
1641 ((match-beginning 4) font-lock-function-name-face)
1642 (t font-lock-variable-name-face)))
1643 (3 (if (match-beginning 4)
1644 font-lock-function-name-face
1645 font-lock-variable-name-face) nil t)))
1646 )))
f60f18ae 1647 )
030f4a35 1648
fb512de9
SM
1649(defvar c-font-lock-keywords c-font-lock-keywords-1
1650 "Default expressions to highlight in C mode.")
030f4a35 1651
fb512de9
SM
1652(defvar c++-font-lock-keywords c++-font-lock-keywords-1
1653 "Default expressions to highlight in C++ mode.")
030f4a35 1654
d46c21ec 1655
030f4a35 1656(defvar tex-font-lock-keywords
d46c21ec
SM
1657; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
1658; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
1659; 2 font-lock-function-name-face)
1660; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
1661; 2 font-lock-reference-face)
1662; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
1663; ;; not be able to display those fonts.
1664; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep)
1665; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep)
1666; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face)
1667; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep))
1668 ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>.
826b2925
SM
1669 '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
1670 2 font-lock-function-name-face)
1671 ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
1672 2 font-lock-reference-face)
d46c21ec 1673 ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face)
9bfbb130 1674 "\\\\\\([a-zA-Z@]+\\|.\\)"
826b2925
SM
1675 ;; It seems a bit dubious to use `bold' and `italic' faces since we might
1676 ;; not be able to display those fonts.
d46c21ec
SM
1677 ;; LaTeX2e: \emph{This is emphasized}.
1678 ("\\\\emph{\\([^}]+\\)}" 1 'italic keep)
1679 ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...}
1680 ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}"
1681 3 (if (match-beginning 2) 'bold 'italic) keep)
1682 ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for good tables.
1683 ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)"
1684 3 (if (match-beginning 2) 'bold 'italic) keep))
9bfbb130 1685 "Default expressions to highlight in TeX modes.")
d46c21ec 1686\f
a1eb1cf1
RS
1687;; Install ourselves:
1688
a1eb1cf1
RS
1689(or (assq 'font-lock-mode minor-mode-alist)
1690 (setq minor-mode-alist (cons '(font-lock-mode " Font") minor-mode-alist)))
1691
1692;; Provide ourselves:
8f261d40 1693
030f4a35
RS
1694(provide 'font-lock)
1695
1696;;; font-lock.el ends here