Always include dispextern.h before cm.h.
[emacs.git] / lisp / font-lock.el
blob5d0afbddd9581cbcf9c08fc94d730fa5c4f7e307
1 ;;; font-lock.el --- Electric font lock mode
3 ;; Copyright (C) 1992, 1993, 1994, 1995 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
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;;; Commentary:
27 ;; Font Lock mode is a minor mode that causes your comments to be displayed in
28 ;; one face, strings in another, reserved words in another, and so on.
30 ;; Comments will be displayed in `font-lock-comment-face'.
31 ;; Strings will be displayed in `font-lock-string-face'.
32 ;; Regexps are used to display selected patterns in other faces.
34 ;; To make the text you type be fontified, use M-x font-lock-mode RET.
35 ;; When this minor mode is on, the faces of the current line are updated with
36 ;; every insertion or deletion.
38 ;; To turn Font Lock mode on automatically, add this to your .emacs file:
40 ;; (add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock)
42 ;; Fontification for a particular mode may be available in a number of levels
43 ;; of decoration. The higher the level, the more decoration, but the more time
44 ;; it takes to fontify. See the variable `font-lock-maximum-decoration', and
45 ;; also the variable `font-lock-maximum-size'.
47 ;; If you add patterns for a new mode, say foo.el's `foo-mode', say in which
48 ;; you don't want syntactic fontification to occur, you can make Font Lock mode
49 ;; use your regexps when turning on Font Lock by adding to `foo-mode-hook':
51 ;; (add-hook 'foo-mode-hook
52 ;; '(lambda () (make-local-variable 'font-lock-defaults)
53 ;; (setq font-lock-defaults '(foo-font-lock-keywords t))))
55 ;; Nasty regexps of the form "bar\\(\\|lo\\)\\|f\\(oo\\|u\\(\\|bar\\)\\)\\|lo"
56 ;; are made thusly: (make-regexp '("foo" "fu" "fubar" "bar" "barlo" "lo")) for
57 ;; efficiency. See /pub/gnu/emacs/elisp-archive/functions/make-regexp.el.Z on
58 ;; archive.cis.ohio-state.edu for this and other functions.
60 ;; What is fontification for? You might say, "It's to make my code look nice."
61 ;; I think it should be for adding information in the form of cues. These cues
62 ;; should provide you with enough information to both (a) distinguish between
63 ;; different items, and (b) identify the item meanings, without having to read
64 ;; the items and think about it. Therefore, fontification allows you to think
65 ;; less about, say, the structure of code, and more about, say, why the code
66 ;; doesn't work. Or maybe it allows you to think less and drift off to sleep.
68 ;; So, here are my opinions/advice/guidelines:
69 ;;
70 ;; - Use the same face for the same conceptual object, across all modes.
71 ;; i.e., (b) above, all modes that have items that can be thought of as, say,
72 ;; keywords, should be highlighted with the same face, etc.
73 ;; - Keep the faces distinct from each other as far as possible.
74 ;; i.e., (a) above.
75 ;; - Make the face attributes fit the concept as far as possible.
76 ;; i.e., function names might be a bold colour such as blue, comments might
77 ;; be a bright colour such as red, character strings might be brown, because,
78 ;; err, strings are brown (that was not the reason, please believe me).
79 ;; - Don't use a non-nil OVERRIDE unless you have a good reason.
80 ;; Only use OVERRIDE for special things that are easy to define, such as the
81 ;; way `...' quotes are treated in strings and comments in Emacs Lisp mode.
82 ;; Don't use it to, say, highlight keywords in commented out code or strings.
83 ;; - Err, that's it.
85 ;; User variables.
87 (defvar font-lock-verbose t
88 "*If non-nil, means show status messages when fontifying.")
90 ;;;###autoload
91 (defvar font-lock-maximum-decoration nil
92 "*If non-nil, the maximum decoration level for fontifying.
93 If nil, use the default decoration (typically the minimum available).
94 If t, use the maximum decoration available.
95 If a number, use that level of decoration (or if not available the maximum).
96 If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
97 where MAJOR-MODE is a symbol or t (meaning the default). For example:
98 ((c++-mode . 2) (c-mode . t) (t . 1))
99 means use level 2 decoration for buffers in `c++-mode', the maximum decoration
100 available for buffers in `c-mode', and level 1 decoration otherwise.")
102 ;;;###autoload
103 (defvar font-lock-maximum-size (* 250 1024)
104 "*If non-nil, the maximum size for buffers for fontifying.
105 Only buffers less than this can be fontified when Font Lock mode is turned on.
106 If nil, means size is irrelevant.
107 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
108 where MAJOR-MODE is a symbol or t (meaning the default). For example:
109 ((c++-mode . 256000) (c-mode . 256000) (rmail-mode . 1048576))
110 means that the maximum size is 250K for buffers in `c++-mode' or `c-mode', one
111 megabyte for buffers in `rmail-mode', and size is irrelevant otherwise.")
113 ;; Fontification variables:
115 (defvar font-lock-comment-face 'font-lock-comment-face
116 "Face to use for comments.")
118 (defvar font-lock-string-face 'font-lock-string-face
119 "Face to use for strings.")
121 (defvar font-lock-keyword-face 'font-lock-keyword-face
122 "Face to use for keywords.")
124 (defvar font-lock-function-name-face 'font-lock-function-name-face
125 "Face to use for function names.")
127 (defvar font-lock-variable-name-face 'font-lock-variable-name-face
128 "Face to use for variable names.")
130 (defvar font-lock-type-face 'font-lock-type-face
131 "Face to use for type names.")
133 (defvar font-lock-reference-face 'font-lock-reference-face
134 "Face to use for reference names.")
136 (defvar font-lock-keywords nil
137 "*A list of the keywords to highlight.
138 Each element should be of the form:
140 MATCHER
141 (MATCHER . MATCH)
142 (MATCHER . FACENAME)
143 (MATCHER . HIGHLIGHT)
144 (MATCHER HIGHLIGHT ...)
146 where HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
148 For highlighting single items, typically only MATCH-HIGHLIGHT is required.
149 However, if an item or (typically) items is to be hightlighted following the
150 instance of another item (the anchor) then MATCH-ANCHORED may be required.
152 MATCH-HIGHLIGHT should be of the form:
154 (MATCH FACENAME OVERRIDE LAXMATCH)
156 Where MATCHER can be either the regexp to search for, or the function name to
157 call to make the search (called with one argument, the limit of the search).
158 MATCH is the subexpression of MATCHER to be highlighted. FACENAME is an
159 expression whose value is the face name to use. FACENAME's default attributes
160 may be defined in `font-lock-face-attributes'.
162 OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification may
163 be overwritten. If `keep', only parts not already fontified are highlighted.
164 If `prepend' or `append', existing fontification is merged with the new, in
165 which the new or existing fontification, respectively, takes precedence.
166 If LAXMATCH is non-nil, no error is signalled if there is no MATCH in MATCHER.
168 For example, an element of the form highlights (if not already highlighted):
170 \"\\\\\\=<foo\\\\\\=>\" Discrete occurrences of \"foo\" in the value of the
171 variable `font-lock-keyword-face'.
172 (\"fu\\\\(bar\\\\)\" . 1) Substring \"bar\" within all occurrences of \"fubar\" in
173 the value of `font-lock-keyword-face'.
174 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'.
175 (\"foo\\\\|bar\" 0 foo-bar-face t)
176 Occurrences of either \"foo\" or \"bar\" in the value
177 of `foo-bar-face', even if already highlighted.
179 MATCH-ANCHORED should be of the form:
181 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
183 Where MATCHER is as for MATCH-HIGHLIGHT with one exception. The limit of the
184 search is currently guaranteed to be (no greater than) the end of the line.
185 PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
186 the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be
187 used to initialise before, and cleanup after, MATCHER is used. Typically,
188 PRE-MATCH-FORM is used to move to some position relative to the original
189 MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might
190 be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER.
192 For example, an element of the form highlights (if not already highlighted):
194 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
196 Discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
197 discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
198 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is
199 initially searched for starting from the end of the match of \"anchor\", and
200 searching for subsequent instance of \"anchor\" resumes from where searching
201 for \"item\" concluded.)
203 Note that the MATCH-ANCHORED feature is experimental; in the future, we may
204 replace it with other ways of providing this functionality.
206 These regular expressions should not match text which spans lines. While
207 \\[font-lock-fontify-buffer] handles multi-line patterns correctly, updating
208 when you edit the buffer does not, since it considers text one line at a time.
210 Be very careful composing regexps for this list;
211 the wrong pattern can dramatically slow things down!")
212 (make-variable-buffer-local 'font-lock-keywords)
214 (defvar font-lock-defaults nil
215 "If set by a major mode, should be the defaults for Font Lock mode.
216 The value should be like the `cdr' of an item in `font-lock-defaults-alist'.")
218 (defvar font-lock-defaults-alist
219 (let (;; For C and Lisp modes we use `beginning-of-defun', rather than nil,
220 ;; for SYNTAX-BEGIN. Thus the calculation of the cache is usually
221 ;; faster but not infallible, so we risk mis-fontification. --sm.
222 (c-mode-defaults
223 '((c-font-lock-keywords c-font-lock-keywords-1
224 c-font-lock-keywords-2 c-font-lock-keywords-3)
225 nil nil ((?_ . "w")) beginning-of-defun))
226 (c++-mode-defaults
227 '((c++-font-lock-keywords c++-font-lock-keywords-1
228 c++-font-lock-keywords-2 c++-font-lock-keywords-3)
229 nil nil ((?_ . "w") (?~ . "w")) beginning-of-defun))
230 (lisp-mode-defaults
231 '((lisp-font-lock-keywords
232 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
233 nil nil
234 ((?: . "w") (?- . "w") (?* . "w") (?+ . "w") (?. . "w") (?< . "w")
235 (?> . "w") (?= . "w") (?! . "w") (?? . "w") (?$ . "w") (?% . "w")
236 (?_ . "w") (?& . "w") (?~ . "w") (?^ . "w") (?/ . "w"))
237 beginning-of-defun))
238 (scheme-mode-defaults
239 '(scheme-font-lock-keywords nil t
240 ((?: . "w") (?- . "w") (?* . "w") (?+ . "w") (?. . "w") (?< . "w")
241 (?> . "w") (?= . "w") (?! . "w") (?? . "w") (?$ . "w") (?% . "w")
242 (?_ . "w") (?& . "w") (?~ . "w") (?^ . "w") (?/ . "w"))
243 beginning-of-defun))
244 ;; For TeX modes we could use `backward-paragraph' for the same reason.
245 (tex-mode-defaults '(tex-font-lock-keywords nil nil ((?$ . "\""))))
247 (list
248 (cons 'bibtex-mode tex-mode-defaults)
249 (cons 'c++-c-mode c-mode-defaults)
250 (cons 'c++-mode c++-mode-defaults)
251 (cons 'c-mode c-mode-defaults)
252 (cons 'elec-c-mode c-mode-defaults)
253 (cons 'emacs-lisp-mode lisp-mode-defaults)
254 (cons 'inferior-scheme-mode scheme-mode-defaults)
255 (cons 'latex-mode tex-mode-defaults)
256 (cons 'lisp-mode lisp-mode-defaults)
257 (cons 'lisp-interaction-mode lisp-mode-defaults)
258 (cons 'plain-tex-mode tex-mode-defaults)
259 (cons 'scheme-mode scheme-mode-defaults)
260 (cons 'scheme-interaction-mode scheme-mode-defaults)
261 (cons 'slitex-mode tex-mode-defaults)
262 (cons 'tex-mode tex-mode-defaults)))
263 "Alist of default major mode and Font Lock defaults.
264 Each item should be a list of the form:
266 (MAJOR-MODE . (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN))
268 where MAJOR-MODE is a symbol. KEYWORDS may be a symbol (a variable or function
269 whose value is the keywords to use for fontification) or a list of symbols.
270 If KEYWORDS-ONLY is non-nil, syntactic fontification (strings and comments) is
271 not performed. If CASE-FOLD is non-nil, the case of the keywords is ignored
272 when fontifying. If SYNTAX-ALIST is non-nil, it should be a list of cons pairs
273 of the form (CHAR . STRING) used to set the local Font Lock syntax table, for
274 keyword and syntactic fontification (see `modify-syntax-entry').
276 If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
277 backwards outside any enclosing syntactic block, for syntactic fontification.
278 Typical values are `beginning-of-line' (i.e., the start of the line is known to
279 be outside a syntactic block), or `beginning-of-defun' for programming modes or
280 `backward-paragraph' for textual modes (i.e., the mode-dependent function is
281 known to move outside a syntactic block). If nil, the beginning of the buffer
282 is used as a position outside of a syntactic block, in the worst case.
284 These item elements are used by Font Lock mode to set the variables
285 `font-lock-keywords', `font-lock-keywords-only',
286 `font-lock-keywords-case-fold-search', `font-lock-syntax-table' and
287 `font-lock-beginning-of-syntax-function', respectively.")
289 (defvar font-lock-keywords-only nil
290 "*Non-nil means Font Lock should not fontify comments or strings.
291 This is normally set via `font-lock-defaults'.")
293 (defvar font-lock-keywords-case-fold-search nil
294 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
295 This is normally set via `font-lock-defaults'.")
297 (defvar font-lock-syntax-table nil
298 "Non-nil means use this syntax table for fontifying.
299 If this is nil, the major mode's syntax table is used.
300 This is normally set via `font-lock-defaults'.")
302 ;; If this is nil, we only use the beginning of the buffer if we can't use
303 ;; `font-lock-cache-position' and `font-lock-cache-state'.
304 (defvar font-lock-beginning-of-syntax-function nil
305 "*Non-nil means use this function to move back outside of a syntactic block.
306 If this is nil, the beginning of the buffer is used (in the worst case).
307 This is normally set via `font-lock-defaults'.")
309 ;; These record the parse state at a particular position, always the start of a
310 ;; line. Used to make `font-lock-fontify-syntactically-region' faster.
311 (defvar font-lock-cache-position nil)
312 (defvar font-lock-cache-state nil)
313 (make-variable-buffer-local 'font-lock-cache-position)
314 (make-variable-buffer-local 'font-lock-cache-state)
316 (defvar font-lock-mode nil) ; For the modeline.
317 (defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
318 (put 'font-lock-fontified 'permanent-local t)
320 ;;;###autoload
321 (defvar font-lock-mode-hook nil
322 "Function or functions to run on entry to Font Lock mode.")
324 ;; User functions.
326 ;;;###autoload
327 (defun font-lock-mode (&optional arg)
328 "Toggle Font Lock mode.
329 With arg, turn Font Lock mode on if and only if arg is positive.
331 When Font Lock mode is enabled, text is fontified as you type it:
333 - Comments are displayed in `font-lock-comment-face';
334 - Strings are displayed in `font-lock-string-face';
335 - Certain other expressions are displayed in other faces according to the
336 value of the variable `font-lock-keywords'.
338 You can enable Font Lock mode in any major mode automatically by turning on in
339 the major mode's hook. For example, put in your ~/.emacs:
341 (add-hook 'c-mode-hook 'turn-on-font-lock)
343 Or for any visited file with the following in your ~/.emacs:
345 (add-hook 'find-file-hooks 'turn-on-font-lock)
347 The default Font Lock mode faces and their attributes are defined in the
348 variable `font-lock-face-attributes', and Font Lock mode default settings in
349 the variable `font-lock-defaults-alist'. You can set your own default settings
350 for some mode, by setting a buffer local value for `font-lock-defaults', via
351 its mode hook.
353 Where modes support different levels of fontification, you can use the variable
354 `font-lock-maximum-decoration' to specify which level you generally prefer.
355 When you turn Font Lock mode on/off the buffer is fontified/defontified, though
356 fontification occurs only if the buffer is less than `font-lock-maximum-size'.
357 To fontify a buffer without turning on Font Lock mode, and regardless of buffer
358 size, you can use \\[font-lock-fontify-buffer]."
359 (interactive "P")
360 (let ((on-p (if arg (> (prefix-numeric-value arg) 0) (not font-lock-mode)))
361 (maximum-size (if (not (consp font-lock-maximum-size))
362 font-lock-maximum-size
363 (cdr (or (assq major-mode font-lock-maximum-size)
364 (assq t font-lock-maximum-size))))))
365 (if (equal (buffer-name) " *Compiler Input*") ; hack for bytecomp...
366 (setq on-p nil))
367 (if (not on-p)
368 (remove-hook 'after-change-functions 'font-lock-after-change-function
370 (make-local-hook 'after-change-functions)
371 (add-hook 'after-change-functions 'font-lock-after-change-function
372 nil t))
373 (set (make-local-variable 'font-lock-mode) on-p)
374 (cond (on-p
375 (font-lock-set-defaults)
376 (make-local-hook 'before-revert-hook)
377 (make-local-hook 'after-revert-hook)
378 ;; If buffer is reverted, must clean up the state.
379 (add-hook 'before-revert-hook 'font-lock-revert-setup nil t)
380 (add-hook 'after-revert-hook 'font-lock-revert-cleanup nil t)
381 (run-hooks 'font-lock-mode-hook)
382 (cond (font-lock-fontified
383 nil)
384 ((or (null maximum-size) (<= (buffer-size) maximum-size))
385 (font-lock-fontify-buffer))
386 (font-lock-verbose
387 (message "Fontifying %s... buffer too big." (buffer-name)))))
388 (font-lock-fontified
389 (setq font-lock-fontified nil)
390 (remove-hook 'before-revert-hook 'font-lock-revert-setup t)
391 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t)
392 (font-lock-unfontify-region (point-min) (point-max))
393 (font-lock-thing-lock-cleanup))
395 (remove-hook 'before-revert-hook 'font-lock-revert-setup t)
396 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t)
397 (font-lock-thing-lock-cleanup)))
398 (force-mode-line-update)))
400 ;;;###autoload
401 (defun turn-on-font-lock ()
402 "Unconditionally turn on Font Lock mode."
403 (font-lock-mode 1))
405 ;;;###autoload
406 (defun font-lock-fontify-buffer ()
407 "Fontify the current buffer the way `font-lock-mode' would."
408 (interactive)
409 (let ((verbose (and (or font-lock-verbose (interactive-p))
410 (not (zerop (buffer-size))))))
411 (set (make-local-variable 'font-lock-fontified) nil)
412 (if verbose (message "Fontifying %s..." (buffer-name)))
413 ;; Turn it on to run hooks and get the right `font-lock-keywords' etc.
414 (or font-lock-mode (font-lock-set-defaults))
415 (condition-case nil
416 (save-excursion
417 (save-match-data
418 (font-lock-fontify-region (point-min) (point-max) verbose)
419 (setq font-lock-fontified t)))
420 ;; We don't restore the old fontification, so it's best to unfontify.
421 (quit (font-lock-unfontify-region (point-min) (point-max))))
422 (if verbose (message "Fontifying %s... %s." (buffer-name)
423 (if font-lock-fontified "done" "aborted")))
424 (font-lock-after-fontify-buffer)))
426 ;; Fontification functions.
428 ;; We use this wrapper. However, `font-lock-fontify-region' used to be the
429 ;; name used for `font-lock-fontify-syntactically-region', so a change isn't
430 ;; back-compatible. But you shouldn't be calling these directly, should you?
431 (defun font-lock-fontify-region (beg end &optional loudly)
432 (let ((modified (buffer-modified-p))
433 (buffer-undo-list t) (inhibit-read-only t)
434 (old-syntax-table (syntax-table))
435 buffer-file-name buffer-file-truename)
436 (unwind-protect
437 (progn
438 ;; Use the fontification syntax table, if any.
439 (if font-lock-syntax-table (set-syntax-table font-lock-syntax-table))
440 ;; Now do the fontification.
441 (if font-lock-keywords-only
442 (font-lock-unfontify-region beg end)
443 (font-lock-fontify-syntactically-region beg end loudly))
444 (font-lock-fontify-keywords-region beg end loudly))
445 ;; Clean up.
446 (set-syntax-table old-syntax-table)
447 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
449 ;; The following must be rethought, since keywords can override fontification.
450 ; ;; Now scan for keywords, but not if we are inside a comment now.
451 ; (or (and (not font-lock-keywords-only)
452 ; (let ((state (parse-partial-sexp beg end nil nil
453 ; font-lock-cache-state)))
454 ; (or (nth 4 state) (nth 7 state))))
455 ; (font-lock-fontify-keywords-region beg end))
457 (defun font-lock-unfontify-region (beg end)
458 (let ((modified (buffer-modified-p))
459 (buffer-undo-list t) (inhibit-read-only t)
460 buffer-file-name buffer-file-truename)
461 (remove-text-properties beg end '(face nil))
462 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil))))
464 ;; Called when any modification is made to buffer text.
465 (defun font-lock-after-change-function (beg end old-len)
466 (save-excursion
467 (save-match-data
468 ;; Rescan between start of line from `beg' and start of line after `end'.
469 (font-lock-fontify-region
470 (progn (goto-char beg) (beginning-of-line) (point))
471 (progn (goto-char end) (forward-line 1) (point))))))
473 ;; Syntactic fontification functions.
475 (defun font-lock-fontify-syntactically-region (start end &optional loudly)
476 "Put proper face on each string and comment between START and END.
477 START should be at the beginning of a line."
478 (let ((synstart (if comment-start-skip
479 (concat "\\s\"\\|" comment-start-skip)
480 "\\s\""))
481 (comstart (if comment-start-skip
482 (concat "\\s<\\|" comment-start-skip)
483 "\\s<"))
484 state prev prevstate)
485 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
486 (save-restriction
487 (widen)
488 (goto-char start)
490 ;; Find the state at the `beginning-of-line' before `start'.
491 (if (eq start font-lock-cache-position)
492 ;; Use the cache for the state of `start'.
493 (setq state font-lock-cache-state)
494 ;; Find the state of `start'.
495 (if (null font-lock-beginning-of-syntax-function)
496 ;; Use the state at the previous cache position, if any, or
497 ;; otherwise calculate from `point-min'.
498 (if (or (null font-lock-cache-position)
499 (< start font-lock-cache-position))
500 (setq state (parse-partial-sexp (point-min) start))
501 (setq state (parse-partial-sexp font-lock-cache-position start
502 nil nil font-lock-cache-state)))
503 ;; Call the function to move outside any syntactic block.
504 (funcall font-lock-beginning-of-syntax-function)
505 (setq state (parse-partial-sexp (point) start)))
506 ;; Cache the state and position of `start'.
507 (setq font-lock-cache-state state
508 font-lock-cache-position start))
510 ;; If the region starts inside a string, show the extent of it.
511 (if (nth 3 state)
512 (let ((beg (point)))
513 (while (and (re-search-forward "\\s\"" end 'move)
514 (nth 3 (parse-partial-sexp beg (point)
515 nil nil state))))
516 (put-text-property beg (point) 'face font-lock-string-face)
517 (setq state (parse-partial-sexp beg (point) nil nil state))))
519 ;; Likewise for a comment.
520 (if (or (nth 4 state) (nth 7 state))
521 (let ((beg (point)))
522 (save-restriction
523 (narrow-to-region (point-min) end)
524 (condition-case nil
525 (progn
526 (re-search-backward comstart (point-min) 'move)
527 (forward-comment 1)
528 ;; forward-comment skips all whitespace,
529 ;; so go back to the real end of the comment.
530 (skip-chars-backward " \t"))
531 (error (goto-char end))))
532 (put-text-property beg (point) 'face font-lock-comment-face)
533 (setq state (parse-partial-sexp beg (point) nil nil state))))
535 ;; Find each interesting place between here and `end'.
536 (while (and (< (point) end)
537 (setq prev (point) prevstate state)
538 (re-search-forward synstart end t)
539 (progn
540 ;; Clear out the fonts of what we skip over.
541 (remove-text-properties prev (point) '(face nil))
542 ;; Verify the state at that place
543 ;; so we don't get fooled by \" or \;.
544 (setq state (parse-partial-sexp prev (point)
545 nil nil state))))
546 (let ((here (point)))
547 (if (or (nth 4 state) (nth 7 state))
549 ;; We found a real comment start.
550 (let ((beg (match-beginning 0)))
551 (goto-char beg)
552 (save-restriction
553 (narrow-to-region (point-min) end)
554 (condition-case nil
555 (progn
556 (forward-comment 1)
557 ;; forward-comment skips all whitespace,
558 ;; so go back to the real end of the comment.
559 (skip-chars-backward " \t"))
560 (error (goto-char end))))
561 (put-text-property beg (point) 'face
562 font-lock-comment-face)
563 (setq state (parse-partial-sexp here (point) nil nil state)))
564 (if (nth 3 state)
566 ;; We found a real string start.
567 (let ((beg (match-beginning 0)))
568 (while (and (re-search-forward "\\s\"" end 'move)
569 (nth 3 (parse-partial-sexp here (point)
570 nil nil state))))
571 (put-text-property beg (point) 'face font-lock-string-face)
572 (setq state (parse-partial-sexp here (point)
573 nil nil state))))))
575 ;; Make sure `prev' is non-nil after the loop
576 ;; only if it was set on the very last iteration.
577 (setq prev nil)))
579 ;; Clean up.
580 (and prev (remove-text-properties prev end '(face nil)))))
582 ;;; Additional text property functions.
584 ;; The following three text property functions are not generally available (and
585 ;; it's not certain that they should be) so they are inlined for speed.
586 ;; The case for `fillin-text-property' is simple; it may or not be generally
587 ;; useful. (Since it is used here, it is useful in at least one place.;-)
588 ;; However, the case for `append-text-property' and `prepend-text-property' is
589 ;; more complicated. Should they remove duplicate property values or not? If
590 ;; so, should the first or last duplicate item remain? Or the one that was
591 ;; added? In our implementation, the first duplicate remains.
593 (defsubst font-lock-fillin-text-property (start end prop value &optional object)
594 "Fill in one property of the text from START to END.
595 Arguments PROP and VALUE specify the property and value to put where none are
596 already in place. Therefore existing property values are not overwritten.
597 Optional argument OBJECT is the string or buffer containing the text."
598 (let ((start (text-property-any start end prop nil object)) next)
599 (while start
600 (setq next (next-single-property-change start prop object end))
601 (put-text-property start next prop value object)
602 (setq start (text-property-any next end prop nil object)))))
604 ;; This function (from simon's unique.el) is rewritten and inlined for speed.
605 ;(defun unique (list function)
606 ; "Uniquify LIST, deleting elements using FUNCTION.
607 ;Return the list with subsequent duplicate items removed by side effects.
608 ;FUNCTION is called with an element of LIST and a list of elements from LIST,
609 ;and should return the list of elements with occurrences of the element removed,
610 ;i.e., a function such as `delete' or `delq'.
611 ;This function will work even if LIST is unsorted. See also `uniq'."
612 ; (let ((list list))
613 ; (while list
614 ; (setq list (setcdr list (funcall function (car list) (cdr list))))))
615 ; list)
617 (defsubst font-lock-unique (list)
618 "Uniquify LIST, deleting elements using `delq'.
619 Return the list with subsequent duplicate items removed by side effects."
620 (let ((list list))
621 (while list
622 (setq list (setcdr list (delq (car list) (cdr list))))))
623 list)
625 ;; A generalisation of `facemenu-add-face' for any property, but without the
626 ;; removal of inactive faces via `facemenu-discard-redundant-faces' and special
627 ;; treatment of `default'. Uses `unique' to remove duplicate property values.
628 (defsubst font-lock-prepend-text-property (start end prop value &optional object)
629 "Prepend to one property of the text from START to END.
630 Arguments PROP and VALUE specify the property and value to prepend to the value
631 already in place. The resulting property values are always lists, and unique.
632 Optional argument OBJECT is the string or buffer containing the text."
633 (let ((val (if (listp value) value (list value))) next prev)
634 (while (/= start end)
635 (setq next (next-single-property-change start prop object end)
636 prev (get-text-property start prop object))
637 (put-text-property
638 start next prop
639 (font-lock-unique (append val (if (listp prev) prev (list prev))))
640 object)
641 (setq start next))))
643 (defsubst font-lock-append-text-property (start end prop value &optional object)
644 "Append to one property of the text from START to END.
645 Arguments PROP and VALUE specify the property and value to append to the value
646 already in place. The resulting property values are always lists, and unique.
647 Optional argument OBJECT is the string or buffer containing the text."
648 (let ((val (if (listp value) value (list value))) next prev)
649 (while (/= start end)
650 (setq next (next-single-property-change start prop object end)
651 prev (get-text-property start prop object))
652 (put-text-property
653 start next prop
654 (font-lock-unique (append (if (listp prev) prev (list prev)) val))
655 object)
656 (setq start next))))
658 ;;; Regexp fontification functions.
660 (defsubst font-lock-apply-highlight (highlight)
661 "Apply HIGHLIGHT following a match.
662 HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
663 (let* ((match (nth 0 highlight))
664 (start (match-beginning match)) (end (match-end match))
665 (override (nth 2 highlight)))
666 (cond ((not start)
667 ;; No match but we might not signal an error.
668 (or (nth 3 highlight)
669 (error "No match %d in highlight %S" match highlight)))
670 ((not override)
671 ;; Cannot override existing fontification.
672 (or (text-property-not-all start end 'face nil)
673 (put-text-property start end 'face (eval (nth 1 highlight)))))
674 ((eq override t)
675 ;; Override existing fontification.
676 (put-text-property start end 'face (eval (nth 1 highlight))))
677 ((eq override 'keep)
678 ;; Keep existing fontification.
679 (font-lock-fillin-text-property start end 'face
680 (eval (nth 1 highlight))))
681 ((eq override 'prepend)
682 ;; Prepend to existing fontification.
683 (font-lock-prepend-text-property start end 'face
684 (eval (nth 1 highlight))))
685 ((eq override 'append)
686 ;; Append to existing fontification.
687 (font-lock-append-text-property start end 'face
688 (eval (nth 1 highlight)))))))
690 (defsubst font-lock-fontify-anchored-keywords (keywords limit)
691 "Fontify according to KEYWORDS until LIMIT.
692 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords'."
693 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights)
694 ;; Until we come up with a cleaner solution, we make LIMIT the end of line.
695 (save-excursion (end-of-line) (setq limit (min limit (point))))
696 ;; Evaluate PRE-MATCH-FORM.
697 (eval (nth 1 keywords))
698 (save-match-data
699 ;; Find an occurrence of `matcher' before `limit'.
700 (while (if (stringp matcher)
701 (re-search-forward matcher limit t)
702 (funcall matcher limit))
703 ;; Apply each highlight to this instance of `matcher'.
704 (setq highlights lowdarks)
705 (while highlights
706 (font-lock-apply-highlight (car highlights))
707 (setq highlights (cdr highlights)))))
708 ;; Evaluate POST-MATCH-FORM.
709 (eval (nth 2 keywords))))
711 (defun font-lock-fontify-keywords-region (start end &optional loudly)
712 "Fontify according to `font-lock-keywords' between START and END.
713 START should be at the beginning of a line."
714 (let ((case-fold-search font-lock-keywords-case-fold-search)
715 (keywords (cdr (if (eq (car-safe font-lock-keywords) t)
716 font-lock-keywords
717 (font-lock-compile-keywords))))
718 (bufname (buffer-name)) (count 0)
719 keyword matcher highlights)
721 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
722 (while keywords
723 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
724 (make-string (setq count (1+ count)) ?.)))
726 ;; Find an occurrence of `matcher' from `start' to `end'.
727 (setq keyword (car keywords) matcher (car keyword))
728 (goto-char start)
729 (while (if (stringp matcher)
730 (re-search-forward matcher end t)
731 (funcall matcher end))
732 ;; Apply each highlight to this instance of `matcher', which may be
733 ;; specific highlights or more keywords anchored to `matcher'.
734 (setq highlights (cdr keyword))
735 (while highlights
736 (if (numberp (car (car highlights)))
737 (font-lock-apply-highlight (car highlights))
738 (font-lock-fontify-anchored-keywords (car highlights) end))
739 (setq highlights (cdr highlights))))
740 (setq keywords (cdr keywords)))))
742 ;; Various functions.
744 ;; Turn off other related packages if they're on. I prefer a hook. --sm.
745 ;; These explicit calls are easier to understand
746 ;; because people know what they will do.
747 ;; A hook is a mystery because it might do anything whatever. --rms.
748 (defun font-lock-thing-lock-cleanup ()
749 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
750 (fast-lock-mode -1))
751 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
752 (lazy-lock-mode -1))))
754 ;; Do something special for these packages after fontifying. I prefer a hook.
755 (defun font-lock-after-fontify-buffer ()
756 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
757 (fast-lock-after-fontify-buffer))
758 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
759 (lazy-lock-after-fontify-buffer))))
761 ;; If the buffer is about to be reverted, it won't be fontified afterward.
762 (defun font-lock-revert-setup ()
763 (setq font-lock-fontified nil))
765 ;; If the buffer has just been reverted, normally that turns off
766 ;; Font Lock mode. So turn the mode back on if necessary.
767 (defalias 'font-lock-revert-cleanup 'turn-on-font-lock)
769 (defun font-lock-compile-keywords (&optional keywords)
770 ;; Compile `font-lock-keywords' into the form (t KEYWORD ...) where KEYWORD
771 ;; is the (MATCHER HIGHLIGHT ...) shown in the variable's doc string.
772 (let ((keywords (or keywords font-lock-keywords)))
773 (setq font-lock-keywords
774 (if (eq (car-safe keywords) t)
775 keywords
776 (cons t
777 (mapcar
778 (function (lambda (item)
779 (cond ((nlistp item)
780 (list item '(0 font-lock-keyword-face)))
781 ((numberp (cdr item))
782 (list (car item) (list (cdr item) 'font-lock-keyword-face)))
783 ((symbolp (cdr item))
784 (list (car item) (list 0 (cdr item))))
785 ((nlistp (nth 1 item))
786 (list (car item) (cdr item)))
788 item))))
789 keywords))))))
791 (defun font-lock-choose-keywords (keywords level)
792 ;; Return LEVELth element of KEYWORDS. A LEVEL of nil is equal to a
793 ;; LEVEL of 0, a LEVEL of t is equal to (1- (length KEYWORDS)).
794 (let ((level (if (not (consp level))
795 level
796 (cdr (or (assq major-mode level) (assq t level))))))
797 (cond ((symbolp keywords)
798 keywords)
799 ((numberp level)
800 (or (nth level keywords) (car (reverse keywords))))
801 ((eq level t)
802 (car (reverse keywords)))
804 (car keywords)))))
806 (defun font-lock-set-defaults ()
807 "Set fontification defaults appropriately for this mode.
808 Sets `font-lock-keywords', `font-lock-keywords-only', `font-lock-syntax-table',
809 `font-lock-beginning-of-syntax-function' and
810 `font-lock-keywords-case-fold-search' using `font-lock-defaults' (or, if nil,
811 using `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
812 ;; Set face defaults.
813 (font-lock-make-faces)
814 ;; Set fontification defaults.
815 (or font-lock-keywords
816 (let* ((defaults (or font-lock-defaults
817 (cdr (assq major-mode font-lock-defaults-alist))))
818 (keywords (font-lock-choose-keywords
819 (nth 0 defaults) font-lock-maximum-decoration)))
820 ;; Keywords?
821 (setq font-lock-keywords (if (fboundp keywords)
822 (funcall keywords)
823 (eval keywords)))
824 ;; Syntactic?
825 (if (nth 1 defaults)
826 (set (make-local-variable 'font-lock-keywords-only) t))
827 ;; Case fold?
828 (if (nth 2 defaults)
829 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
830 ;; Syntax table?
831 (if (nth 3 defaults)
832 (let ((slist (nth 3 defaults)))
833 (set (make-local-variable 'font-lock-syntax-table)
834 (copy-syntax-table (syntax-table)))
835 (while slist
836 (modify-syntax-entry (car (car slist)) (cdr (car slist))
837 font-lock-syntax-table)
838 (setq slist (cdr slist)))))
839 ;; Syntax function?
840 (if (nth 4 defaults)
841 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
842 (nth 4 defaults))))))
844 ;; Colour etc. support.
846 (defvar font-lock-display-type nil
847 "A symbol indicating the display Emacs is running under.
848 The symbol should be one of `color', `grayscale' or `mono'.
849 If Emacs guesses this display attribute wrongly, either set this variable in
850 your `~/.emacs' or set the resource `Emacs.displayType' in your `~/.Xdefaults'.
851 See also `font-lock-background-mode' and `font-lock-face-attributes'.")
853 (defvar font-lock-background-mode nil
854 "A symbol indicating the Emacs background brightness.
855 The symbol should be one of `light' or `dark'.
856 If Emacs guesses this frame attribute wrongly, either set this variable in
857 your `~/.emacs' or set the resource `Emacs.backgroundMode' in your
858 `~/.Xdefaults'.
859 See also `font-lock-display-type' and `font-lock-face-attributes'.")
861 (defvar font-lock-face-attributes nil
862 "A list of default attributes to use for face attributes.
863 Each element of the list should be of the form
865 (FACE FOREGROUND BACKGROUND BOLD-P ITALIC-P UNDERLINE-P)
867 where FACE should be one of the face symbols `font-lock-comment-face',
868 `font-lock-string-face', `font-lock-keyword-face', `font-lock-type-face',
869 `font-lock-function-name-face', `font-lock-variable-name-face', and
870 `font-lock-reference-face'. A form for each of these face symbols should be
871 provided in the list, but other face symbols and attributes may be given and
872 used in highlighting. See `font-lock-keywords'.
874 Subsequent element items should be the attributes for the corresponding
875 Font Lock mode faces. Attributes FOREGROUND and BACKGROUND should be strings
876 \(default if nil), while BOLD-P, ITALIC-P, and UNDERLINE-P should specify the
877 corresponding face attributes (yes if non-nil).
879 Emacs uses default attributes based on display type and background brightness.
880 See variables `font-lock-display-type' and `font-lock-background-mode'.
882 Resources can be used to over-ride these face attributes. For example, the
883 resource `Emacs.font-lock-comment-face.attributeUnderline' can be used to
884 specify the UNDERLINE-P attribute for face `font-lock-comment-face'.")
886 (defun font-lock-make-faces (&optional override)
887 "Make faces from `font-lock-face-attributes'.
888 A default list is used if this is nil.
889 If optional OVERRIDE is non-nil, faces that already exist are reset.
890 See `font-lock-make-face' and `list-faces-display'."
891 ;; We don't need to `setq' any of these variables, but the user can see what
892 ;; is being used if we do.
893 (if (null font-lock-display-type)
894 (setq font-lock-display-type
895 (let ((display-resource (x-get-resource ".displayType"
896 "DisplayType")))
897 (cond (display-resource (intern (downcase display-resource)))
898 ((x-display-color-p) 'color)
899 ((x-display-grayscale-p) 'grayscale)
900 (t 'mono)))))
901 (if (null font-lock-background-mode)
902 (setq font-lock-background-mode
903 (let ((bg-resource (x-get-resource ".backgroundMode"
904 "BackgroundMode"))
905 (params (frame-parameters)))
906 (cond (bg-resource (intern (downcase bg-resource)))
907 ((< (apply '+ (x-color-values
908 (cdr (assq 'background-color params))))
909 (/ (apply '+ (x-color-values "white")) 3))
910 'dark)
911 (t 'light)))))
912 (if (null font-lock-face-attributes)
913 (setq font-lock-face-attributes
914 (let ((light-bg (eq font-lock-background-mode 'light)))
915 (cond ((memq font-lock-display-type '(mono monochrome))
916 ;; Emacs 19.25's font-lock defaults:
917 ;;'((font-lock-comment-face nil nil nil t nil)
918 ;; (font-lock-string-face nil nil nil nil t)
919 ;; (font-lock-keyword-face nil nil t nil nil)
920 ;; (font-lock-function-name-face nil nil t t nil)
921 ;; (font-lock-type-face nil nil nil t nil))
922 (list '(font-lock-comment-face nil nil t t nil)
923 '(font-lock-string-face nil nil nil t nil)
924 '(font-lock-keyword-face nil nil t nil nil)
925 (list
926 'font-lock-function-name-face
927 (cdr (assq 'background-color (frame-parameters)))
928 (cdr (assq 'foreground-color (frame-parameters)))
929 t nil nil)
930 '(font-lock-variable-name-face nil nil t t nil)
931 '(font-lock-type-face nil nil t nil t)
932 '(font-lock-reference-face nil nil t nil t)))
933 ((memq font-lock-display-type '(grayscale greyscale
934 grayshade greyshade))
935 (list
936 (list 'font-lock-comment-face
937 nil (if light-bg "Gray80" "DimGray") t t nil)
938 (list 'font-lock-string-face
939 nil (if light-bg "Gray50" "LightGray") nil t nil)
940 (list 'font-lock-keyword-face
941 nil (if light-bg "Gray90" "DimGray") t nil nil)
942 (list 'font-lock-function-name-face
943 (cdr (assq 'background-color (frame-parameters)))
944 (cdr (assq 'foreground-color (frame-parameters)))
945 t nil nil)
946 (list 'font-lock-variable-name-face
947 nil (if light-bg "Gray90" "DimGray") t t nil)
948 (list 'font-lock-type-face
949 nil (if light-bg "Gray80" "DimGray") t nil t)
950 (list 'font-lock-reference-face
951 nil (if light-bg "LightGray" "Gray50") t nil t)))
952 (light-bg ; light colour background
953 '((font-lock-comment-face "Firebrick")
954 (font-lock-string-face "RosyBrown")
955 (font-lock-keyword-face "Purple")
956 (font-lock-function-name-face "Blue")
957 (font-lock-variable-name-face "DarkGoldenrod")
958 (font-lock-type-face "DarkOliveGreen")
959 (font-lock-reference-face "CadetBlue")))
960 (t ; dark colour background
961 '((font-lock-comment-face "OrangeRed")
962 (font-lock-string-face "LightSalmon")
963 (font-lock-keyword-face "LightSteelBlue")
964 (font-lock-function-name-face "LightSkyBlue")
965 (font-lock-variable-name-face "LightGoldenrod")
966 (font-lock-type-face "PaleGreen")
967 (font-lock-reference-face "Aquamarine")))))))
968 ;; Now make the faces if we have to.
969 (mapcar (function
970 (lambda (face-attributes)
971 (let ((face (nth 0 face-attributes)))
972 (cond (override
973 ;; We can stomp all over it anyway. Get outta my face!
974 (font-lock-make-face face-attributes))
975 ((and (boundp face) (facep (symbol-value face)))
976 ;; The variable exists and is already bound to a face.
977 nil)
978 ((facep face)
979 ;; We already have a face so we bind the variable to it.
980 (set face face))
982 ;; No variable or no face.
983 (font-lock-make-face face-attributes))))))
984 font-lock-face-attributes))
986 (defun font-lock-make-face (face-attributes)
987 "Make a face from FACE-ATTRIBUTES.
988 FACE-ATTRIBUTES should be like an element `font-lock-face-attributes', so that
989 the face name is the first item in the list. A variable with the same name as
990 the face is also set; its value is the face name."
991 (let* ((face (nth 0 face-attributes))
992 (face-name (symbol-name face))
993 (set-p (function (lambda (face-name resource)
994 (x-get-resource (concat face-name ".attribute" resource)
995 (concat "Face.Attribute" resource)))))
996 (on-p (function (lambda (face-name resource)
997 (let ((set (funcall set-p face-name resource)))
998 (and set (member (downcase set) '("on" "true"))))))))
999 (make-face face)
1000 (add-to-list 'facemenu-unlisted-faces face)
1001 ;; Set attributes not set from X resources (and therefore `make-face').
1002 (or (funcall set-p face-name "Foreground")
1003 (condition-case nil
1004 (set-face-foreground face (nth 1 face-attributes))
1005 (error nil)))
1006 (or (funcall set-p face-name "Background")
1007 (condition-case nil
1008 (set-face-background face (nth 2 face-attributes))
1009 (error nil)))
1010 (if (funcall set-p face-name "Bold")
1011 (and (funcall on-p face-name "Bold") (make-face-bold face nil t))
1012 (and (nth 3 face-attributes) (make-face-bold face nil t)))
1013 (if (funcall set-p face-name "Italic")
1014 (and (funcall on-p face-name "Italic") (make-face-italic face nil t))
1015 (and (nth 4 face-attributes) (make-face-italic face nil t)))
1016 (or (funcall set-p face-name "Underline")
1017 (set-face-underline-p face (nth 5 face-attributes)))
1018 (set face face)))
1020 ;;; Various regexp information shared by several modes.
1021 ;;; Information specific to a single mode should go in its load library.
1023 (defconst lisp-font-lock-keywords-1
1024 (list
1025 ;; Anything not a variable or type declaration is fontified as a function.
1026 ;; It would be cleaner to allow preceding whitespace, but it would also be
1027 ;; about five times slower.
1028 (list (concat "^(\\(def\\("
1029 ;; Variable declarations.
1030 "\\(const\\(\\|ant\\)\\|ine-key\\(\\|-after\\)\\|var\\)\\|"
1031 ;; Structure declarations.
1032 "\\(class\\|struct\\|type\\)\\|"
1033 ;; Everything else is a function declaration.
1034 "\\([^ \t\n\(\)]+\\)"
1035 "\\)\\)\\>"
1036 ;; Any whitespace and declared object.
1037 "[ \t'\(]*"
1038 "\\([^ \t\n\)]+\\)?")
1039 '(1 font-lock-keyword-face)
1040 '(8 (cond ((match-beginning 3) font-lock-variable-name-face)
1041 ((match-beginning 6) font-lock-type-face)
1042 (t font-lock-function-name-face))
1043 nil t))
1045 "Subdued level highlighting Lisp modes.")
1047 (defconst lisp-font-lock-keywords-2
1048 (append lisp-font-lock-keywords-1
1049 (list
1051 ;; Control structures. ELisp and CLisp combined.
1052 ; (make-regexp
1053 ; '("cond" "if" "while" "let\\*?" "prog[nv12*]?" "catch" "throw"
1054 ; "save-restriction" "save-excursion" "save-window-excursion"
1055 ; "save-selected-window" "save-match-data" "unwind-protect"
1056 ; "condition-case" "track-mouse"
1057 ; "eval-after-load" "eval-and-compile" "eval-when-compile"
1058 ; "when" "unless" "do" "flet" "labels" "return" "return-from"))
1059 (cons
1060 (concat
1061 "(\\("
1062 "\\(c\\(atch\\|ond\\(\\|ition-case\\)\\)\\|do\\|"
1063 "eval-\\(a\\(fter-load\\|nd-compile\\)\\|when-compile\\)\\|flet\\|"
1064 "if\\|l\\(abels\\|et\\*?\\)\\|prog[nv12*]?\\|return\\(\\|-from\\)\\|"
1065 "save-\\(excursion\\|match-data\\|restriction\\|selected-window\\|"
1066 "window-excursion\\)\\|t\\(hrow\\|rack-mouse\\)\\|"
1067 "un\\(less\\|wind-protect\\)\\|wh\\(en\\|ile\\)\\)"
1068 "\\)\\>") 1)
1070 ;; Words inside \\[] tend to be for `substitute-command-keys'.
1071 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-reference-face prepend)
1073 ;; Words inside `' tend to be symbol names.
1074 '("`\\(\\sw\\sw+\\)'" 1 font-lock-reference-face prepend)
1076 ;; CLisp `:' keywords as references.
1077 '("\\<:\\sw+\\>" 0 font-lock-reference-face prepend)
1079 ;; ELisp and CLisp `&' keywords as types.
1080 '("\\<\\&\\(optional\\|rest\\|whole\\)\\>" . font-lock-type-face)
1082 "Gaudy level highlighting for Lisp modes.")
1084 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
1085 "Default expressions to highlight in Lisp modes.")
1088 (defvar scheme-font-lock-keywords
1089 (eval-when-compile
1090 (list
1092 ;; Declarations. Hannes Haug <hannes.haug@student.uni-tuebingen.de> says
1093 ;; this works for SOS, STklos, SCOOPS, Meroon and Tiny CLOS.
1094 (list (concat "(\\(define\\("
1095 ;; Function names.
1096 "\\(\\|-\\(generic\\(\\|-procedure\\)\\|method\\)\\)\\|"
1097 ;; Macro names, as variable names. A bit dubious, this.
1098 "\\(-syntax\\)\\|"
1099 ;; Class names.
1100 "\\(-class\\)"
1101 "\\)\\)\\>"
1102 ;; Any whitespace and declared object.
1103 "[ \t]*(?"
1104 "\\(\\sw+\\)?")
1105 '(1 font-lock-keyword-face)
1106 '(8 (cond ((match-beginning 3) font-lock-function-name-face)
1107 ((match-beginning 6) font-lock-variable-name-face)
1108 (t font-lock-type-face))
1109 nil t))
1111 ;; Control structures.
1112 ;(make-regexp '("begin" "call-with-current-continuation" "call/cc"
1113 ; "call-with-input-file" "call-with-output-file" "case" "cond"
1114 ; "do" "else" "for-each" "if" "lambda"
1115 ; "let\\*?" "let-syntax" "letrec" "letrec-syntax"
1116 ; ;; Hannes Haug <hannes.haug@student.uni-tuebingen.de> wants:
1117 ; "and" "or" "delay"
1118 ; ;; Stefan Monnier <stefan.monnier@epfl.ch> says don't bother:
1119 ; ;;"quasiquote" "quote" "unquote" "unquote-splicing"
1120 ; "map" "syntax" "syntax-rules"))
1121 (cons
1122 (concat "(\\("
1123 "and\\|begin\\|c\\(a\\(ll\\(-with-\\(current-continuation\\|"
1124 "input-file\\|output-file\\)\\|/cc\\)\\|se\\)\\|ond\\)\\|"
1125 "d\\(elay\\|o\\)\\|else\\|for-each\\|if\\|"
1126 "l\\(ambda\\|et\\(-syntax\\|\\*?\\|rec\\(\\|-syntax\\)\\)\\)\\|"
1127 "map\\|or\\|syntax\\(\\|-rules\\)"
1128 "\\)\\>") 1)
1130 ;; David Fox <fox@graphics.cs.nyu.edu> for SOS/STklos class specifiers.
1131 '("\\<<\\sw+>\\>" . font-lock-type-face)
1133 ;; Scheme `:' keywords as references.
1134 '("\\<:\\sw+\\>" . font-lock-reference-face)
1136 "Default expressions to highlight in Scheme modes.")
1139 (defconst c-font-lock-keywords-1 nil
1140 "Subdued level highlighting for C modes.")
1142 (defconst c-font-lock-keywords-2 nil
1143 "Medium level highlighting for C modes.")
1145 (defconst c-font-lock-keywords-3 nil
1146 "Gaudy level highlighting for C modes.")
1148 (defconst c++-font-lock-keywords-1 nil
1149 "Subdued level highlighting for C++ modes.")
1151 (defconst c++-font-lock-keywords-2 nil
1152 "Medium level highlighting for C++ modes.")
1154 (defconst c++-font-lock-keywords-3 nil
1155 "Gaudy level highlighting for C++ modes.")
1157 (defun font-lock-match-c++-style-declaration-item-and-skip-to-next (limit)
1158 ;; Match, and move over, any declaration/definition item after point.
1159 ;; The expect syntax of an item is "word" or "word::word", possibly ending
1160 ;; with optional whitespace and a "(". Everything following the item (but
1161 ;; belonging to it) is expected to by skip-able by `forward-sexp', and items
1162 ;; are expected to be separated with a "," or ";".
1163 (if (looking-at "[ \t*&]*\\(\\sw+\\)\\(::\\(\\sw+\\)\\)?[ \t]*\\((\\)?")
1164 (save-match-data
1165 (condition-case nil
1166 (save-restriction
1167 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
1168 (narrow-to-region (point-min) limit)
1169 (goto-char (match-end 1))
1170 ;; Move over any item value, etc., to the next item.
1171 (while (not (looking-at "[ \t]*\\([,;]\\|$\\)"))
1172 (goto-char (or (scan-sexps (point) 1) (point-max))))
1173 (goto-char (match-end 0)))
1174 (error t)))))
1176 (let ((c-keywords
1177 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while")
1178 "break\\|continue\\|do\\|else\\|for\\|if\\|return\\|switch\\|while")
1179 (c-type-types
1180 ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
1181 ; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
1182 ; "void" "volatile" "const")
1183 (concat "auto\\|c\\(har\\|onst\\)\\|double\\|e\\(num\\|xtern\\)\\|"
1184 "float\\|int\\|long\\|register\\|"
1185 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|typedef\\|"
1186 "un\\(ion\\|signed\\)\\|vo\\(id\\|latile\\)")) ; 6 ()s deep.
1187 (c++-keywords
1188 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while"
1189 ; "asm" "catch" "delete" "new" "operator" "sizeof" "this" "throw" "try"
1190 ; "protected" "private" "public")
1191 (concat "asm\\|break\\|c\\(atch\\|ontinue\\)\\|d\\(elete\\|o\\)\\|"
1192 "else\\|for\\|if\\|new\\|"
1193 "p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|return\\|"
1194 "s\\(izeof\\|witch\\)\\|t\\(h\\(is\\|row\\)\\|ry\\)\\|while"))
1195 (c++-type-types
1196 ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
1197 ; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
1198 ; "void" "volatile" "const" "class" "inline" "friend" "bool"
1199 ; "virtual" "complex" "template")
1200 (concat "auto\\|bool\\|c\\(har\\|lass\\|o\\(mplex\\|nst\\)\\)\\|"
1201 "double\\|e\\(num\\|xtern\\)\\|f\\(loat\\|riend\\)\\|"
1202 "in\\(line\\|t\\)\\|long\\|register\\|"
1203 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|"
1204 "t\\(emplate\\|ypedef\\)\\|un\\(ion\\|signed\\)\\|"
1205 "v\\(irtual\\|o\\(id\\|latile\\)\\)")) ; 11 ()s deep.
1207 (setq c-font-lock-keywords-1
1208 (list
1210 ;; These are all anchored at the beginning of line for speed.
1212 ;; Fontify function name definitions (GNU style; without type on line).
1213 (list (concat "^\\(\\sw+\\)[ \t]*(") 1 'font-lock-function-name-face)
1215 ;; Fontify filenames in #include <...> preprocessor directives as strings.
1216 '("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face)
1218 ;; Fontify function macro names.
1219 '("^#[ \t]*define[ \t]+\\(\\(\\sw+\\)(\\)" 2 font-lock-function-name-face)
1221 ;; Fontify symbol names in #if ... defined preprocessor directives.
1222 '("^#[ \t]*if\\>"
1223 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
1224 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t)))
1226 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
1227 '("^\\(#[ \t]*[a-z]+\\)\\>[ \t]*\\(\\sw+\\)?"
1228 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t))
1231 (setq c-font-lock-keywords-2
1232 (append c-font-lock-keywords-1
1233 (list
1235 ;; Simple regexps for speed.
1237 ;; Fontify all type specifiers.
1238 (cons (concat "\\<\\(" c-type-types "\\)\\>") 'font-lock-type-face)
1240 ;; Fontify all builtin keywords (except case, default and goto; see below).
1241 (cons (concat "\\<\\(" c-keywords "\\)\\>") 'font-lock-keyword-face)
1243 ;; Fontify case/goto keywords and targets, and case default/goto tags.
1244 '("\\<\\(case\\|goto\\)\\>[ \t]*\\([^ \t\n:;]+\\)?"
1245 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1246 '("^[ \t]*\\(\\sw+\\)[ \t]*:" 1 font-lock-reference-face)
1249 (setq c-font-lock-keywords-3
1250 (append c-font-lock-keywords-2
1252 ;; More complicated regexps for more complete highlighting for types.
1253 ;; We still have to fontify type specifiers individually, as C is so hairy.
1254 (list
1256 ;; Fontify all storage classes and type specifiers, plus their items.
1257 (list (concat "\\<\\(" c-type-types "\\)\\>"
1258 "\\([ \t*&]+\\sw+\\>\\)*")
1259 ;; Fontify each declaration item.
1260 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
1261 ;; Start with point after all type specifiers.
1262 (goto-char (or (match-beginning 8) (match-end 1)))
1263 ;; Finish with point after first type specifier.
1264 (goto-char (match-end 1))
1265 ;; Fontify as a variable or function name.
1266 (1 (if (match-beginning 4)
1267 font-lock-function-name-face
1268 font-lock-variable-name-face))))
1270 ;; Fontify structures, or typedef names, plus their items.
1271 '("\\(}\\)[ \t*]*\\sw"
1272 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1273 (goto-char (match-end 1)) nil
1274 (1 (if (match-beginning 4)
1275 font-lock-function-name-face
1276 font-lock-variable-name-face))))
1278 ;; Fontify anything at beginning of line as a declaration or definition.
1279 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
1280 (1 font-lock-type-face)
1281 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1282 (goto-char (or (match-beginning 2) (match-end 1))) nil
1283 (1 (if (match-beginning 4)
1284 font-lock-function-name-face
1285 font-lock-variable-name-face))))
1288 (setq c++-font-lock-keywords-1
1289 (append
1291 ;; The list `c-font-lock-keywords-1' less that for function names.
1292 (cdr c-font-lock-keywords-1)
1294 ;; Fontify function name definitions, possibly incorporating class name.
1295 (list
1296 '("^\\(\\sw+\\)\\(::\\(\\sw+\\)\\)?[ \t]*("
1297 (1 (if (match-beginning 2)
1298 font-lock-type-face
1299 font-lock-function-name-face))
1300 (3 (if (match-beginning 2) font-lock-function-name-face) nil t))
1303 (setq c++-font-lock-keywords-2
1304 (append c++-font-lock-keywords-1
1305 (list
1307 ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading.
1308 (cons (concat "\\<\\(" c++-type-types "\\)\\>") 'font-lock-type-face)
1310 ;; Fontify operator function name overloading.
1311 '("\\<\\(operator\\)\\>[ \t]*\\([][)(><!=+-][][)(><!=+-]?\\)?"
1312 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
1314 ;; Fontify case/goto keywords and targets, and case default/goto tags.
1315 '("\\<\\(case\\|goto\\)\\>[ \t]*\\([^ \t\n:;]+\\)?"
1316 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1317 '("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-reference-face)
1319 ;; Fontify other builtin keywords.
1320 (cons (concat "\\<\\(" c++-keywords "\\)\\>") 'font-lock-keyword-face)
1323 (setq c++-font-lock-keywords-3
1324 (append c++-font-lock-keywords-2
1326 ;; More complicated regexps for more complete highlighting for types.
1327 (list
1329 ;; Fontify all storage classes and type specifiers, plus their items.
1330 (list (concat "\\<\\(" c++-type-types "\\)\\>"
1331 "\\([ \t*&]+\\sw+\\>\\)*")
1332 ;; Fontify each declaration item.
1333 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
1334 ;; Start with point after all type specifiers.
1335 (goto-char (or (match-beginning 13) (match-end 1)))
1336 ;; Finish with point after first type specifier.
1337 (goto-char (match-end 1))
1338 ;; Fontify as a variable or function name.
1339 (1 (cond ((match-beginning 2) font-lock-type-face)
1340 ((match-beginning 4) font-lock-function-name-face)
1341 (t font-lock-variable-name-face)))
1342 (3 (if (match-beginning 4)
1343 font-lock-function-name-face
1344 font-lock-variable-name-face) nil t)))
1346 ;; Fontify structures, or typedef names, plus their items.
1347 '("\\(}\\)[ \t*]*\\sw"
1348 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1349 (goto-char (match-end 1)) nil
1350 (1 (if (match-beginning 4)
1351 font-lock-function-name-face
1352 font-lock-variable-name-face))))
1354 ;; Fontify anything at beginning of line as a declaration or definition.
1355 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
1356 (1 font-lock-type-face)
1357 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1358 (goto-char (or (match-beginning 2) (match-end 1))) nil
1359 (1 (cond ((match-beginning 2) font-lock-type-face)
1360 ((match-beginning 4) font-lock-function-name-face)
1361 (t font-lock-variable-name-face)))
1362 (3 (if (match-beginning 4)
1363 font-lock-function-name-face
1364 font-lock-variable-name-face) nil t)))
1368 (defvar c-font-lock-keywords c-font-lock-keywords-1
1369 "Default expressions to highlight in C mode.")
1371 (defvar c++-font-lock-keywords c++-font-lock-keywords-1
1372 "Default expressions to highlight in C++ mode.")
1375 (defvar tex-font-lock-keywords
1376 ; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
1377 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
1378 ; 2 font-lock-function-name-face)
1379 ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
1380 ; 2 font-lock-reference-face)
1381 ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
1382 ; ;; not be able to display those fonts.
1383 ; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep)
1384 ; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep)
1385 ; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face)
1386 ; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep))
1387 ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>.
1388 '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
1389 2 font-lock-function-name-face)
1390 ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
1391 2 font-lock-reference-face)
1392 ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face)
1393 "\\\\\\([a-zA-Z@]+\\|.\\)"
1394 ;; It seems a bit dubious to use `bold' and `italic' faces since we might
1395 ;; not be able to display those fonts.
1396 ;; LaTeX2e: \emph{This is emphasized}.
1397 ("\\\\emph{\\([^}]+\\)}" 1 'italic keep)
1398 ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...}
1399 ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}"
1400 3 (if (match-beginning 2) 'bold 'italic) keep)
1401 ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for good tables.
1402 ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)"
1403 3 (if (match-beginning 2) 'bold 'italic) keep))
1404 "Default expressions to highlight in TeX modes.")
1406 ;; Install ourselves:
1408 (or (assq 'font-lock-mode minor-mode-alist)
1409 (setq minor-mode-alist (cons '(font-lock-mode " Font") minor-mode-alist)))
1411 ;; Provide ourselves:
1413 (provide 'font-lock)
1415 ;;; font-lock.el ends here