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