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