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