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