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>
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)
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.
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:
70 ;; - Highlight conceptual objects, such as function and variable names, and
71 ;; different objects types differently, i.e., (a) and (b) above, highlight
72 ;; function names differently to variable names.
73 ;; - Keep the faces distinct from each other as far as possible.
75 ;; - Use the same face for the same conceptual object, across all modes.
76 ;; i.e., (b) above, all modes that have items that can be thought of as, say,
77 ;; keywords, should be highlighted with the same face, etc.
78 ;; - Make the face attributes fit the concept as far as possible.
79 ;; i.e., function names might be a bold colour such as blue, comments might
80 ;; be a bright colour such as red, character strings might be brown, because,
81 ;; err, strings are brown (that was not the reason, please believe me).
82 ;; - Don't use a non-nil OVERRIDE unless you have a good reason.
83 ;; Only use OVERRIDE for special things that are easy to define, such as the
84 ;; way `...' quotes are treated in strings and comments in Emacs Lisp mode.
85 ;; Don't use it to, say, highlight keywords in commented out code or strings.
90 (defvar font-lock-verbose t
91 "*If non-nil, means show status messages when fontifying.")
94 (defvar font-lock-maximum-decoration nil
95 "*If non-nil, the maximum decoration level for fontifying.
96 If nil, use the default decoration (typically the minimum available).
97 If t, use the maximum decoration available.
98 If a number, use that level of decoration (or if not available the maximum).
99 If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
100 where MAJOR-MODE is a symbol or t (meaning the default). For example:
101 ((c++-mode . 2) (c-mode . t) (t . 1))
102 means use level 2 decoration for buffers in `c++-mode', the maximum decoration
103 available for buffers in `c-mode', and level 1 decoration otherwise.")
106 (defvar font-lock-maximum-size (* 250 1024)
107 "*If non-nil, the maximum size for buffers for fontifying.
108 Only buffers less than this can be fontified when Font Lock mode is turned on.
109 If nil, means size is irrelevant.
110 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
111 where MAJOR-MODE is a symbol or t (meaning the default). For example:
112 ((c++-mode . 256000) (c-mode . 256000) (rmail-mode . 1048576))
113 means that the maximum size is 250K for buffers in `c++-mode' or `c-mode', one
114 megabyte for buffers in `rmail-mode', and size is irrelevant otherwise.")
116 ;; Fontification variables:
118 (defvar font-lock-comment-face 'font-lock-comment-face
119 "Face to use for comments.")
121 (defvar font-lock-string-face 'font-lock-string-face
122 "Face to use for strings.")
124 (defvar font-lock-keyword-face 'font-lock-keyword-face
125 "Face to use for keywords.")
127 (defvar font-lock-function-name-face 'font-lock-function-name-face
128 "Face to use for function names.")
130 (defvar font-lock-variable-name-face 'font-lock-variable-name-face
131 "Face to use for variable names.")
133 (defvar font-lock-type-face 'font-lock-type-face
134 "Face to use for type names.")
136 (defvar font-lock-reference-face 'font-lock-reference-face
137 "Face to use for reference names.")
139 (defvar font-lock-keywords nil
140 "*A list of the keywords to highlight.
141 Each element should be of the form:
146 (MATCHER . HIGHLIGHT)
147 (MATCHER HIGHLIGHT ...)
149 where HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
151 For highlighting single items, typically only MATCH-HIGHLIGHT is required.
152 However, if an item or (typically) items is to be hightlighted following the
153 instance of another item (the anchor) then MATCH-ANCHORED may be required.
155 MATCH-HIGHLIGHT should be of the form:
157 (MATCH FACENAME OVERRIDE LAXMATCH)
159 Where MATCHER can be either the regexp to search for, or the function name to
160 call to make the search (called with one argument, the limit of the search).
161 MATCH is the subexpression of MATCHER to be highlighted. FACENAME is an
162 expression whose value is the face name to use. FACENAME's default attributes
163 may be defined in `font-lock-face-attributes'.
165 OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification may
166 be overwritten. If `keep', only parts not already fontified are highlighted.
167 If `prepend' or `append', existing fontification is merged with the new, in
168 which the new or existing fontification, respectively, takes precedence.
169 If LAXMATCH is non-nil, no error is signalled if there is no MATCH in MATCHER.
171 For example, an element of the form highlights (if not already highlighted):
173 \"\\\\\\=<foo\\\\\\=>\" Discrete occurrences of \"foo\" in the value of the
174 variable `font-lock-keyword-face'.
175 (\"fu\\\\(bar\\\\)\" . 1) Substring \"bar\" within all occurrences of \"fubar\" in
176 the value of `font-lock-keyword-face'.
177 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'.
178 (\"foo\\\\|bar\" 0 foo-bar-face t)
179 Occurrences of either \"foo\" or \"bar\" in the value
180 of `foo-bar-face', even if already highlighted.
182 MATCH-ANCHORED should be of the form:
184 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
186 Where MATCHER is as for MATCH-HIGHLIGHT with one exception. The limit of the
187 search is currently guaranteed to be (no greater than) the end of the line.
188 PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
189 the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be
190 used to initialise before, and cleanup after, MATCHER is used. Typically,
191 PRE-MATCH-FORM is used to move to some position relative to the original
192 MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might
193 be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER.
195 For example, an element of the form highlights (if not already highlighted):
197 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
199 Discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
200 discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
201 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is
202 initially searched for starting from the end of the match of \"anchor\", and
203 searching for subsequent instance of \"anchor\" resumes from where searching
204 for \"item\" concluded.)
206 Note that the MATCH-ANCHORED feature is experimental; in the future, we may
207 replace it with other ways of providing this functionality.
209 These regular expressions should not match text which spans lines. While
210 \\[font-lock-fontify-buffer] handles multi-line patterns correctly, updating
211 when you edit the buffer does not, since it considers text one line at a time.
213 Be very careful composing regexps for this list;
214 the wrong pattern can dramatically slow things down!")
215 (make-variable-buffer-local 'font-lock-keywords)
217 (defvar font-lock-defaults nil
218 "If set by a major mode, should be the defaults for Font Lock mode.
219 The value should be like the `cdr' of an item in `font-lock-defaults-alist'.")
221 (defvar font-lock-defaults-alist
222 (let (;; For C and Lisp modes we use `beginning-of-defun', rather than nil,
223 ;; for SYNTAX-BEGIN. Thus the calculation of the cache is usually
224 ;; faster but not infallible, so we risk mis-fontification. --sm.
226 '((c-font-lock-keywords c-font-lock-keywords-1
227 c-font-lock-keywords-2 c-font-lock-keywords-3)
228 nil nil ((?_ . "w")) beginning-of-defun))
230 '((c++-font-lock-keywords c++-font-lock-keywords-1
231 c++-font-lock-keywords-2 c++-font-lock-keywords-3)
232 nil nil ((?_ . "w") (?~ . "w")) beginning-of-defun))
234 '((lisp-font-lock-keywords
235 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
237 ((?: . "w") (?- . "w") (?* . "w") (?+ . "w") (?. . "w") (?< . "w")
238 (?> . "w") (?= . "w") (?! . "w") (?? . "w") (?$ . "w") (?% . "w")
239 (?_ . "w") (?& . "w") (?~ . "w") (?^ . "w") (?/ . "w"))
241 (scheme-mode-defaults
242 '(scheme-font-lock-keywords nil t
243 ((?: . "w") (?- . "w") (?* . "w") (?+ . "w") (?. . "w") (?< . "w")
244 (?> . "w") (?= . "w") (?! . "w") (?? . "w") (?$ . "w") (?% . "w")
245 (?_ . "w") (?& . "w") (?~ . "w") (?^ . "w") (?/ . "w"))
247 ;; For TeX modes we could use `backward-paragraph' for the same reason.
248 (tex-mode-defaults '(tex-font-lock-keywords nil nil ((?$ . "\""))))
251 (cons 'bibtex-mode tex-mode-defaults)
252 (cons 'c++-c-mode c-mode-defaults)
253 (cons 'c++-mode c++-mode-defaults)
254 (cons 'c-mode c-mode-defaults)
255 (cons 'elec-c-mode c-mode-defaults)
256 (cons 'emacs-lisp-mode lisp-mode-defaults)
257 (cons 'inferior-scheme-mode scheme-mode-defaults)
258 (cons 'latex-mode tex-mode-defaults)
259 (cons 'lisp-mode lisp-mode-defaults)
260 (cons 'lisp-interaction-mode lisp-mode-defaults)
261 (cons 'plain-tex-mode tex-mode-defaults)
262 (cons 'scheme-mode scheme-mode-defaults)
263 (cons 'scheme-interaction-mode scheme-mode-defaults)
264 (cons 'slitex-mode tex-mode-defaults)
265 (cons 'tex-mode tex-mode-defaults)))
266 "Alist of default major mode and Font Lock defaults.
267 Each item should be a list of the form:
269 (MAJOR-MODE . (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN))
271 where MAJOR-MODE is a symbol. KEYWORDS may be a symbol (a variable or function
272 whose value is the keywords to use for fontification) or a list of symbols.
273 If KEYWORDS-ONLY is non-nil, syntactic fontification (strings and comments) is
274 not performed. If CASE-FOLD is non-nil, the case of the keywords is ignored
275 when fontifying. If SYNTAX-ALIST is non-nil, it should be a list of cons pairs
276 of the form (CHAR . STRING) used to set the local Font Lock syntax table, for
277 keyword and syntactic fontification (see `modify-syntax-entry').
279 If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
280 backwards outside any enclosing syntactic block, for syntactic fontification.
281 Typical values are `beginning-of-line' (i.e., the start of the line is known to
282 be outside a syntactic block), or `beginning-of-defun' for programming modes or
283 `backward-paragraph' for textual modes (i.e., the mode-dependent function is
284 known to move outside a syntactic block). If nil, the beginning of the buffer
285 is used as a position outside of a syntactic block, in the worst case.
287 These item elements are used by Font Lock mode to set the variables
288 `font-lock-keywords', `font-lock-keywords-only',
289 `font-lock-keywords-case-fold-search', `font-lock-syntax-table' and
290 `font-lock-beginning-of-syntax-function', respectively.")
292 (defvar font-lock-keywords-only nil
293 "*Non-nil means Font Lock should not fontify comments or strings.
294 This is normally set via `font-lock-defaults'.")
296 (defvar font-lock-keywords-case-fold-search nil
297 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
298 This is normally set via `font-lock-defaults'.")
300 (defvar font-lock-syntax-table nil
301 "Non-nil means use this syntax table for fontifying.
302 If this is nil, the major mode's syntax table is used.
303 This is normally set via `font-lock-defaults'.")
305 ;; If this is nil, we only use the beginning of the buffer if we can't use
306 ;; `font-lock-cache-position' and `font-lock-cache-state'.
307 (defvar font-lock-beginning-of-syntax-function nil
308 "*Non-nil means use this function to move back outside of a syntactic block.
309 If this is nil, the beginning of the buffer is used (in the worst case).
310 This is normally set via `font-lock-defaults'.")
312 ;; These record the parse state at a particular position, always the start of a
313 ;; line. Used to make `font-lock-fontify-syntactically-region' faster.
314 (defvar font-lock-cache-position nil)
315 (defvar font-lock-cache-state nil)
316 (make-variable-buffer-local 'font-lock-cache-position)
317 (make-variable-buffer-local 'font-lock-cache-state)
319 (defvar font-lock-mode nil) ; For the modeline.
320 (defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
321 (put 'font-lock-fontified 'permanent-local t)
324 (defvar font-lock-mode-hook nil
325 "Function or functions to run on entry to Font Lock mode.")
330 (defun font-lock-mode (&optional arg)
331 "Toggle Font Lock mode.
332 With arg, turn Font Lock mode on if and only if arg is positive.
334 When Font Lock mode is enabled, text is fontified as you type it:
336 - Comments are displayed in `font-lock-comment-face';
337 - Strings are displayed in `font-lock-string-face';
338 - Certain other expressions are displayed in other faces according to the
339 value of the variable `font-lock-keywords'.
341 You can enable Font Lock mode in any major mode automatically by turning on in
342 the major mode's hook. For example, put in your ~/.emacs:
344 (add-hook 'c-mode-hook 'turn-on-font-lock)
346 Or for any visited file with the following in your ~/.emacs:
348 (add-hook 'find-file-hooks 'turn-on-font-lock)
350 The default Font Lock mode faces and their attributes are defined in the
351 variable `font-lock-face-attributes', and Font Lock mode default settings in
352 the variable `font-lock-defaults-alist'. You can set your own default settings
353 for some mode, by setting a buffer local value for `font-lock-defaults', via
356 Where modes support different levels of fontification, you can use the variable
357 `font-lock-maximum-decoration' to specify which level you generally prefer.
358 When you turn Font Lock mode on/off the buffer is fontified/defontified, though
359 fontification occurs only if the buffer is less than `font-lock-maximum-size'.
361 To fontify a buffer, without turning on Font Lock mode and regardless of buffer
362 size, you can use \\[font-lock-fontify-buffer].
363 To fontify a window, perhaps because modification on the current line caused
364 syntactic change on other lines, you can use \\[font-lock-fontify-window]."
366 (let ((on-p (if arg (> (prefix-numeric-value arg) 0) (not font-lock-mode)))
367 (maximum-size (if (not (consp font-lock-maximum-size))
368 font-lock-maximum-size
369 (cdr (or (assq major-mode font-lock-maximum-size)
370 (assq t font-lock-maximum-size))))))
371 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
372 ;; batch job) or if the buffer is invisible (the name starts with a space).
373 (if (or noninteractive (eq (aref (buffer-name) 0) ?\ ))
376 (remove-hook 'after-change-functions 'font-lock-after-change-function
378 (make-local-hook 'after-change-functions)
379 (add-hook 'after-change-functions 'font-lock-after-change-function
381 (set (make-local-variable 'font-lock-mode) on-p)
383 (font-lock-set-defaults)
384 (make-local-hook 'before-revert-hook)
385 (make-local-hook 'after-revert-hook)
386 ;; If buffer is reverted, must clean up the state.
387 (add-hook 'before-revert-hook 'font-lock-revert-setup nil t)
388 (add-hook 'after-revert-hook 'font-lock-revert-cleanup nil t)
389 (run-hooks 'font-lock-mode-hook)
390 (cond (font-lock-fontified
392 ((or (null maximum-size) (<= (buffer-size) maximum-size))
393 (font-lock-fontify-buffer))
395 (message "Fontifying %s... buffer too big." (buffer-name)))))
397 (setq font-lock-fontified nil)
398 (remove-hook 'before-revert-hook 'font-lock-revert-setup t)
399 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t)
400 ;; Make sure we unfontify etc. in the whole buffer.
403 (font-lock-unfontify-region (point-min) (point-max))
404 (font-lock-thing-lock-cleanup)))
406 (remove-hook 'before-revert-hook 'font-lock-revert-setup t)
407 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t)
408 (font-lock-thing-lock-cleanup)))
409 (force-mode-line-update)))
412 (defun turn-on-font-lock ()
413 "Unconditionally turn on Font Lock mode."
417 (defun font-lock-fontify-buffer ()
418 "Fontify the current buffer the way `font-lock-mode' would."
420 (let ((verbose (and (or font-lock-verbose (interactive-p))
421 (not (zerop (buffer-size))))))
422 (set (make-local-variable 'font-lock-fontified) nil)
423 (if verbose (message "Fontifying %s..." (buffer-name)))
424 ;; Make sure we have the right `font-lock-keywords' etc.
425 (if (not font-lock-mode) (font-lock-set-defaults))
426 ;; Make sure we fontify etc. in the whole buffer.
432 (font-lock-fontify-region (point-min) (point-max) verbose)
433 (setq font-lock-fontified t)))
434 ;; We don't restore the old fontification, so it's best to unfontify.
435 (quit (font-lock-unfontify-region (point-min) (point-max))))
436 (if verbose (message "Fontifying %s... %s." (buffer-name)
437 (if font-lock-fontified "done" "aborted")))
438 (font-lock-after-fontify-buffer))))
440 (defun font-lock-fontify-window ()
441 "Fontify the current window the way `font-lock-mode' would."
443 (let ((font-lock-beginning-of-syntax-function nil))
446 (font-lock-fontify-region (window-start) (window-end))))))
448 (define-key global-map [?\C-\S-l] 'font-lock-fontify-window)
450 ;; Fontification functions.
452 ;; We use this wrapper. However, `font-lock-fontify-region' used to be the
453 ;; name used for `font-lock-fontify-syntactically-region', so a change isn't
454 ;; back-compatible. But you shouldn't be calling these directly, should you?
455 (defun font-lock-fontify-region (beg end &optional loudly)
456 (let ((modified (buffer-modified-p))
457 (buffer-undo-list t) (inhibit-read-only t)
458 (old-syntax-table (syntax-table))
459 before-change-functions after-change-functions
460 buffer-file-name buffer-file-truename)
463 ;; Use the fontification syntax table, if any.
464 (if font-lock-syntax-table (set-syntax-table font-lock-syntax-table))
465 ;; Now do the fontification.
466 (if font-lock-keywords-only
467 (font-lock-unfontify-region beg end)
468 (font-lock-fontify-syntactically-region beg end loudly))
469 (font-lock-fontify-keywords-region beg end loudly))
471 (set-syntax-table old-syntax-table)
472 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
474 ;; The following must be rethought, since keywords can override fontification.
475 ; ;; Now scan for keywords, but not if we are inside a comment now.
476 ; (or (and (not font-lock-keywords-only)
477 ; (let ((state (parse-partial-sexp beg end nil nil
478 ; font-lock-cache-state)))
479 ; (or (nth 4 state) (nth 7 state))))
480 ; (font-lock-fontify-keywords-region beg end))
482 (defun font-lock-unfontify-region (beg end)
483 (let ((modified (buffer-modified-p))
484 (buffer-undo-list t) (inhibit-read-only t)
485 before-change-functions after-change-functions
486 buffer-file-name buffer-file-truename)
487 (remove-text-properties beg end '(face nil))
488 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil))))
490 ;; Called when any modification is made to buffer text.
491 (defun font-lock-after-change-function (beg end old-len)
494 ;; Rescan between start of line from `beg' and start of line after `end'.
495 (font-lock-fontify-region
496 (progn (goto-char beg) (beginning-of-line) (point))
497 (progn (goto-char end) (forward-line 1) (point))))))
499 ;; Syntactic fontification functions.
501 (defun font-lock-fontify-syntactically-region (start end &optional loudly)
502 "Put proper face on each string and comment between START and END.
503 START should be at the beginning of a line."
504 (let ((synstart (if comment-start-skip
505 (concat "\\s\"\\|" comment-start-skip)
507 (comstart (if comment-start-skip
508 (concat "\\s<\\|" comment-start-skip)
510 state prev prevstate)
511 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
516 ;; Find the state at the `beginning-of-line' before `start'.
517 (if (eq start font-lock-cache-position)
518 ;; Use the cache for the state of `start'.
519 (setq state font-lock-cache-state)
520 ;; Find the state of `start'.
521 (if (null font-lock-beginning-of-syntax-function)
522 ;; Use the state at the previous cache position, if any, or
523 ;; otherwise calculate from `point-min'.
524 (if (or (null font-lock-cache-position)
525 (< start font-lock-cache-position))
526 (setq state (parse-partial-sexp (point-min) start))
527 (setq state (parse-partial-sexp font-lock-cache-position start
528 nil nil font-lock-cache-state)))
529 ;; Call the function to move outside any syntactic block.
530 (funcall font-lock-beginning-of-syntax-function)
531 (setq state (parse-partial-sexp (point) start)))
532 ;; Cache the state and position of `start'.
533 (setq font-lock-cache-state state
534 font-lock-cache-position start))
536 ;; If the region starts inside a string, show the extent of it.
539 (while (and (re-search-forward "\\s\"" end 'move)
540 (nth 3 (parse-partial-sexp beg (point)
542 (put-text-property beg (point) 'face font-lock-string-face)
543 (setq state (parse-partial-sexp beg (point) nil nil state))))
545 ;; Likewise for a comment.
546 (if (or (nth 4 state) (nth 7 state))
549 (narrow-to-region (point-min) end)
552 (re-search-backward comstart (point-min) 'move)
554 ;; forward-comment skips all whitespace,
555 ;; so go back to the real end of the comment.
556 (skip-chars-backward " \t"))
557 (error (goto-char end))))
558 (put-text-property beg (point) 'face font-lock-comment-face)
559 (setq state (parse-partial-sexp beg (point) nil nil state))))
561 ;; Find each interesting place between here and `end'.
562 (while (and (< (point) end)
563 (setq prev (point) prevstate state)
564 (re-search-forward synstart end t)
566 ;; Clear out the fonts of what we skip over.
567 (remove-text-properties prev (point) '(face nil))
568 ;; Verify the state at that place
569 ;; so we don't get fooled by \" or \;.
570 (setq state (parse-partial-sexp prev (point)
572 (let ((here (point)))
573 (if (or (nth 4 state) (nth 7 state))
575 ;; We found a real comment start.
576 (let ((beg (match-beginning 0)))
579 (narrow-to-region (point-min) end)
583 ;; forward-comment skips all whitespace,
584 ;; so go back to the real end of the comment.
585 (skip-chars-backward " \t"))
586 (error (goto-char end))))
587 (put-text-property beg (point) 'face
588 font-lock-comment-face)
589 (setq state (parse-partial-sexp here (point) nil nil state)))
592 ;; We found a real string start.
593 (let ((beg (match-beginning 0)))
594 (while (and (re-search-forward "\\s\"" end 'move)
595 (nth 3 (parse-partial-sexp here (point)
597 (put-text-property beg (point) 'face font-lock-string-face)
598 (setq state (parse-partial-sexp here (point)
601 ;; Make sure `prev' is non-nil after the loop
602 ;; only if it was set on the very last iteration.
606 (and prev (remove-text-properties prev end '(face nil)))))
608 ;;; Additional text property functions.
610 ;; The following three text property functions are not generally available (and
611 ;; it's not certain that they should be) so they are inlined for speed.
612 ;; The case for `fillin-text-property' is simple; it may or not be generally
613 ;; useful. (Since it is used here, it is useful in at least one place.;-)
614 ;; However, the case for `append-text-property' and `prepend-text-property' is
615 ;; more complicated. Should they remove duplicate property values or not? If
616 ;; so, should the first or last duplicate item remain? Or the one that was
617 ;; added? In our implementation, the first duplicate remains.
619 (defsubst font-lock-fillin-text-property (start end prop value &optional object)
620 "Fill in one property of the text from START to END.
621 Arguments PROP and VALUE specify the property and value to put where none are
622 already in place. Therefore existing property values are not overwritten.
623 Optional argument OBJECT is the string or buffer containing the text."
624 (let ((start (text-property-any start end prop nil object)) next)
626 (setq next (next-single-property-change start prop object end))
627 (put-text-property start next prop value object)
628 (setq start (text-property-any next end prop nil object)))))
630 ;; This function (from simon's unique.el) is rewritten and inlined for speed.
631 ;(defun unique (list function)
632 ; "Uniquify LIST, deleting elements using FUNCTION.
633 ;Return the list with subsequent duplicate items removed by side effects.
634 ;FUNCTION is called with an element of LIST and a list of elements from LIST,
635 ;and should return the list of elements with occurrences of the element removed,
636 ;i.e., a function such as `delete' or `delq'.
637 ;This function will work even if LIST is unsorted. See also `uniq'."
640 ; (setq list (setcdr list (funcall function (car list) (cdr list))))))
643 (defsubst font-lock-unique (list)
644 "Uniquify LIST, deleting elements using `delq'.
645 Return the list with subsequent duplicate items removed by side effects."
648 (setq list (setcdr list (delq (car list) (cdr list))))))
651 ;; A generalisation of `facemenu-add-face' for any property, but without the
652 ;; removal of inactive faces via `facemenu-discard-redundant-faces' and special
653 ;; treatment of `default'. Uses `unique' to remove duplicate property values.
654 (defsubst font-lock-prepend-text-property (start end prop value &optional object)
655 "Prepend to one property of the text from START to END.
656 Arguments PROP and VALUE specify the property and value to prepend to the value
657 already in place. The resulting property values are always lists, and unique.
658 Optional argument OBJECT is the string or buffer containing the text."
659 (let ((val (if (listp value) value (list value))) next prev)
660 (while (/= start end)
661 (setq next (next-single-property-change start prop object end)
662 prev (get-text-property start prop object))
665 (font-lock-unique (append val (if (listp prev) prev (list prev))))
669 (defsubst font-lock-append-text-property (start end prop value &optional object)
670 "Append to one property of the text from START to END.
671 Arguments PROP and VALUE specify the property and value to append to the value
672 already in place. The resulting property values are always lists, and unique.
673 Optional argument OBJECT is the string or buffer containing the text."
674 (let ((val (if (listp value) value (list value))) next prev)
675 (while (/= start end)
676 (setq next (next-single-property-change start prop object end)
677 prev (get-text-property start prop object))
680 (font-lock-unique (append (if (listp prev) prev (list prev)) val))
684 ;;; Regexp fontification functions.
686 (defsubst font-lock-apply-highlight (highlight)
687 "Apply HIGHLIGHT following a match.
688 HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
689 (let* ((match (nth 0 highlight))
690 (start (match-beginning match)) (end (match-end match))
691 (override (nth 2 highlight)))
693 ;; No match but we might not signal an error.
694 (or (nth 3 highlight)
695 (error "No match %d in highlight %S" match highlight)))
697 ;; Cannot override existing fontification.
698 (or (text-property-not-all start end 'face nil)
699 (put-text-property start end 'face (eval (nth 1 highlight)))))
701 ;; Override existing fontification.
702 (put-text-property start end 'face (eval (nth 1 highlight))))
704 ;; Keep existing fontification.
705 (font-lock-fillin-text-property start end 'face
706 (eval (nth 1 highlight))))
707 ((eq override 'prepend)
708 ;; Prepend to existing fontification.
709 (font-lock-prepend-text-property start end 'face
710 (eval (nth 1 highlight))))
711 ((eq override 'append)
712 ;; Append to existing fontification.
713 (font-lock-append-text-property start end 'face
714 (eval (nth 1 highlight)))))))
716 (defsubst font-lock-fontify-anchored-keywords (keywords limit)
717 "Fontify according to KEYWORDS until LIMIT.
718 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords'."
719 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights)
720 ;; Until we come up with a cleaner solution, we make LIMIT the end of line.
721 (save-excursion (end-of-line) (setq limit (min limit (point))))
722 ;; Evaluate PRE-MATCH-FORM.
723 (eval (nth 1 keywords))
725 ;; Find an occurrence of `matcher' before `limit'.
726 (while (if (stringp matcher)
727 (re-search-forward matcher limit t)
728 (funcall matcher limit))
729 ;; Apply each highlight to this instance of `matcher'.
730 (setq highlights lowdarks)
732 (font-lock-apply-highlight (car highlights))
733 (setq highlights (cdr highlights)))))
734 ;; Evaluate POST-MATCH-FORM.
735 (eval (nth 2 keywords))))
737 (defun font-lock-fontify-keywords-region (start end &optional loudly)
738 "Fontify according to `font-lock-keywords' between START and END.
739 START should be at the beginning of a line."
740 (let ((case-fold-search font-lock-keywords-case-fold-search)
741 (keywords (cdr (if (eq (car-safe font-lock-keywords) t)
743 (font-lock-compile-keywords))))
744 (bufname (buffer-name)) (count 0)
745 keyword matcher highlights)
747 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
749 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
750 (make-string (setq count (1+ count)) ?.)))
752 ;; Find an occurrence of `matcher' from `start' to `end'.
753 (setq keyword (car keywords) matcher (car keyword))
755 (while (if (stringp matcher)
756 (re-search-forward matcher end t)
757 (funcall matcher end))
758 ;; Apply each highlight to this instance of `matcher', which may be
759 ;; specific highlights or more keywords anchored to `matcher'.
760 (setq highlights (cdr keyword))
762 (if (numberp (car (car highlights)))
763 (font-lock-apply-highlight (car highlights))
764 (font-lock-fontify-anchored-keywords (car highlights) end))
765 (setq highlights (cdr highlights))))
766 (setq keywords (cdr keywords)))))
768 ;; Various functions.
770 ;; Turn off other related packages if they're on. I prefer a hook. --sm.
771 ;; These explicit calls are easier to understand
772 ;; because people know what they will do.
773 ;; A hook is a mystery because it might do anything whatever. --rms.
774 (defun font-lock-thing-lock-cleanup ()
775 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
777 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
778 (lazy-lock-mode -1))))
780 ;; Do something special for these packages after fontifying. I prefer a hook.
781 (defun font-lock-after-fontify-buffer ()
782 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
783 (fast-lock-after-fontify-buffer))
784 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
785 (lazy-lock-after-fontify-buffer))))
787 ;; If the buffer is about to be reverted, it won't be fontified afterward.
788 (defun font-lock-revert-setup ()
789 (setq font-lock-fontified nil))
791 ;; If the buffer has just been reverted, normally that turns off
792 ;; Font Lock mode. So turn the mode back on if necessary.
793 (defalias 'font-lock-revert-cleanup
796 (defun font-lock-compile-keywords (&optional keywords)
797 ;; Compile `font-lock-keywords' into the form (t KEYWORD ...) where KEYWORD
798 ;; is the (MATCHER HIGHLIGHT ...) shown in the variable's doc string.
799 (let ((keywords (or keywords font-lock-keywords)))
800 (setq font-lock-keywords
801 (if (eq (car-safe keywords) t)
805 (function (lambda (item)
807 (list item '(0 font-lock-keyword-face)))
808 ((numberp (cdr item))
809 (list (car item) (list (cdr item) 'font-lock-keyword-face)))
810 ((symbolp (cdr item))
811 (list (car item) (list 0 (cdr item))))
812 ((nlistp (nth 1 item))
813 (list (car item) (cdr item)))
818 (defun font-lock-choose-keywords (keywords level)
819 ;; Return LEVELth element of KEYWORDS. A LEVEL of nil is equal to a
820 ;; LEVEL of 0, a LEVEL of t is equal to (1- (length KEYWORDS)).
821 (let ((level (if (not (consp level))
823 (cdr (or (assq major-mode level) (assq t level))))))
824 (cond ((symbolp keywords)
827 (or (nth level keywords) (car (reverse keywords))))
829 (car (reverse keywords)))
833 (defun font-lock-set-defaults ()
834 "Set fontification defaults appropriately for this mode.
835 Sets `font-lock-keywords', `font-lock-keywords-only', `font-lock-syntax-table',
836 `font-lock-beginning-of-syntax-function' and
837 `font-lock-keywords-case-fold-search' using `font-lock-defaults' (or, if nil,
838 using `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
839 ;; Set face defaults.
840 (font-lock-make-faces)
841 ;; Set fontification defaults.
842 (or font-lock-keywords
843 (let* ((defaults (or font-lock-defaults
844 (cdr (assq major-mode font-lock-defaults-alist))))
845 (keywords (font-lock-choose-keywords
846 (nth 0 defaults) font-lock-maximum-decoration)))
847 ;; Regexp fontification?
848 (setq font-lock-keywords (if (fboundp keywords)
851 ;; Syntactic fontification?
853 (set (make-local-variable 'font-lock-keywords-only) t))
854 ;; Case fold during regexp fontification?
856 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
857 ;; Syntax table for regexp and syntactic fontification?
859 (let ((slist (nth 3 defaults)))
860 (set (make-local-variable 'font-lock-syntax-table)
861 (copy-syntax-table (syntax-table)))
863 (modify-syntax-entry (car (car slist)) (cdr (car slist))
864 font-lock-syntax-table)
865 (setq slist (cdr slist)))))
866 ;; Syntax function for syntactic fontification?
868 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
869 (nth 4 defaults))))))
871 ;; Colour etc. support.
873 (defvar font-lock-display-type nil
874 "A symbol indicating the display Emacs is running under.
875 The symbol should be one of `color', `grayscale' or `mono'.
876 If Emacs guesses this display attribute wrongly, either set this variable in
877 your `~/.emacs' or set the resource `Emacs.displayType' in your `~/.Xdefaults'.
878 See also `font-lock-background-mode' and `font-lock-face-attributes'.")
880 (defvar font-lock-background-mode nil
881 "A symbol indicating the Emacs background brightness.
882 The symbol should be one of `light' or `dark'.
883 If Emacs guesses this frame attribute wrongly, either set this variable in
884 your `~/.emacs' or set the resource `Emacs.backgroundMode' in your
886 See also `font-lock-display-type' and `font-lock-face-attributes'.")
888 (defvar font-lock-face-attributes nil
889 "A list of default attributes to use for face attributes.
890 Each element of the list should be of the form
892 (FACE FOREGROUND BACKGROUND BOLD-P ITALIC-P UNDERLINE-P)
894 where FACE should be one of the face symbols `font-lock-comment-face',
895 `font-lock-string-face', `font-lock-keyword-face', `font-lock-type-face',
896 `font-lock-function-name-face', `font-lock-variable-name-face', and
897 `font-lock-reference-face'. A form for each of these face symbols should be
898 provided in the list, but other face symbols and attributes may be given and
899 used in highlighting. See `font-lock-keywords'.
901 Subsequent element items should be the attributes for the corresponding
902 Font Lock mode faces. Attributes FOREGROUND and BACKGROUND should be strings
903 \(default if nil), while BOLD-P, ITALIC-P, and UNDERLINE-P should specify the
904 corresponding face attributes (yes if non-nil).
906 Emacs uses default attributes based on display type and background brightness.
907 See variables `font-lock-display-type' and `font-lock-background-mode'.
909 Resources can be used to over-ride these face attributes. For example, the
910 resource `Emacs.font-lock-comment-face.attributeUnderline' can be used to
911 specify the UNDERLINE-P attribute for face `font-lock-comment-face'.")
913 (defun font-lock-make-faces (&optional override)
914 "Make faces from `font-lock-face-attributes'.
915 A default list is used if this is nil.
916 If optional OVERRIDE is non-nil, faces that already exist are reset.
917 See `font-lock-make-face' and `list-faces-display'."
918 ;; We don't need to `setq' any of these variables, but the user can see what
919 ;; is being used if we do.
920 (if (null font-lock-display-type)
921 (setq font-lock-display-type
922 (let ((display-resource (x-get-resource ".displayType"
924 (cond (display-resource (intern (downcase display-resource)))
925 ((x-display-color-p) 'color)
926 ((x-display-grayscale-p) 'grayscale)
928 (if (null font-lock-background-mode)
929 (setq font-lock-background-mode
930 (let ((bg-resource (x-get-resource ".backgroundMode"
932 (params (frame-parameters)))
933 (cond (bg-resource (intern (downcase bg-resource)))
934 ((eq system-type 'ms-dos)
935 (if (string-match "light"
936 (cdr (assq 'background-color params)))
939 ((< (apply '+ (x-color-values
940 (cdr (assq 'background-color params))))
941 (/ (apply '+ (x-color-values "white")) 3))
944 (if (null font-lock-face-attributes)
945 (setq font-lock-face-attributes
946 (let ((light-bg (eq font-lock-background-mode 'light)))
947 (cond ((memq font-lock-display-type '(mono monochrome))
948 ;; Emacs 19.25's font-lock defaults:
949 ;;'((font-lock-comment-face nil nil nil t nil)
950 ;; (font-lock-string-face nil nil nil nil t)
951 ;; (font-lock-keyword-face nil nil t nil nil)
952 ;; (font-lock-function-name-face nil nil t t nil)
953 ;; (font-lock-type-face nil nil nil t nil))
954 (list '(font-lock-comment-face nil nil t t nil)
955 '(font-lock-string-face nil nil nil t nil)
956 '(font-lock-keyword-face nil nil t nil nil)
958 'font-lock-function-name-face
959 (cdr (assq 'background-color (frame-parameters)))
960 (cdr (assq 'foreground-color (frame-parameters)))
962 '(font-lock-variable-name-face nil nil t t nil)
963 '(font-lock-type-face nil nil t nil t)
964 '(font-lock-reference-face nil nil t nil t)))
965 ((memq font-lock-display-type '(grayscale greyscale
966 grayshade greyshade))
968 (list 'font-lock-comment-face
969 nil (if light-bg "Gray80" "DimGray") t t nil)
970 (list 'font-lock-string-face
971 nil (if light-bg "Gray50" "LightGray") nil t nil)
972 (list 'font-lock-keyword-face
973 nil (if light-bg "Gray90" "DimGray") t nil nil)
974 (list 'font-lock-function-name-face
975 (cdr (assq 'background-color (frame-parameters)))
976 (cdr (assq 'foreground-color (frame-parameters)))
978 (list 'font-lock-variable-name-face
979 nil (if light-bg "Gray90" "DimGray") t t nil)
980 (list 'font-lock-type-face
981 nil (if light-bg "Gray80" "DimGray") t nil t)
982 (list 'font-lock-reference-face
983 nil (if light-bg "LightGray" "Gray50") t nil t)))
984 (light-bg ; light colour background
985 '((font-lock-comment-face "Firebrick")
986 (font-lock-string-face "RosyBrown")
987 (font-lock-keyword-face "Purple")
988 (font-lock-function-name-face "Blue")
989 (font-lock-variable-name-face "DarkGoldenrod")
990 (font-lock-type-face "DarkOliveGreen")
991 (font-lock-reference-face "CadetBlue")))
992 (t ; dark colour background
993 '((font-lock-comment-face "OrangeRed")
994 (font-lock-string-face "LightSalmon")
995 (font-lock-keyword-face "LightSteelBlue")
996 (font-lock-function-name-face "LightSkyBlue")
997 (font-lock-variable-name-face "LightGoldenrod")
998 (font-lock-type-face "PaleGreen")
999 (font-lock-reference-face "Aquamarine")))))))
1000 ;; Now make the faces if we have to.
1002 (lambda (face-attributes)
1003 (let ((face (nth 0 face-attributes)))
1005 ;; We can stomp all over it anyway. Get outta my face!
1006 (font-lock-make-face face-attributes))
1007 ((and (boundp face) (facep (symbol-value face)))
1008 ;; The variable exists and is already bound to a face.
1011 ;; We already have a face so we bind the variable to it.
1014 ;; No variable or no face.
1015 (font-lock-make-face face-attributes))))))
1016 font-lock-face-attributes))
1018 (defun font-lock-make-face (face-attributes)
1019 "Make a face from FACE-ATTRIBUTES.
1020 FACE-ATTRIBUTES should be like an element `font-lock-face-attributes', so that
1021 the face name is the first item in the list. A variable with the same name as
1022 the face is also set; its value is the face name."
1023 (let* ((face (nth 0 face-attributes))
1024 (face-name (symbol-name face))
1025 (set-p (function (lambda (face-name resource)
1026 (x-get-resource (concat face-name ".attribute" resource)
1027 (concat "Face.Attribute" resource)))))
1028 (on-p (function (lambda (face-name resource)
1029 (let ((set (funcall set-p face-name resource)))
1030 (and set (member (downcase set) '("on" "true"))))))))
1032 (add-to-list 'facemenu-unlisted-faces face)
1033 ;; Set attributes not set from X resources (and therefore `make-face').
1034 (or (funcall set-p face-name "Foreground")
1036 (set-face-foreground face (nth 1 face-attributes))
1038 (or (funcall set-p face-name "Background")
1040 (set-face-background face (nth 2 face-attributes))
1042 (if (funcall set-p face-name "Bold")
1043 (and (funcall on-p face-name "Bold") (make-face-bold face nil t))
1044 (and (nth 3 face-attributes) (make-face-bold face nil t)))
1045 (if (funcall set-p face-name "Italic")
1046 (and (funcall on-p face-name "Italic") (make-face-italic face nil t))
1047 (and (nth 4 face-attributes) (make-face-italic face nil t)))
1048 (or (funcall set-p face-name "Underline")
1049 (set-face-underline-p face (nth 5 face-attributes)))
1052 ;;; Various regexp information shared by several modes.
1053 ;;; Information specific to a single mode should go in its load library.
1055 (defconst lisp-font-lock-keywords-1
1057 ;; Anything not a variable or type declaration is fontified as a function.
1058 ;; It would be cleaner to allow preceding whitespace, but it would also be
1059 ;; about five times slower.
1060 (list (concat "^(\\(def\\("
1061 ;; Variable declarations.
1062 "\\(const\\(\\|ant\\)\\|ine-key\\(\\|-after\\)\\|var\\)\\|"
1063 ;; Structure declarations.
1064 "\\(class\\|struct\\|type\\)\\|"
1065 ;; Everything else is a function declaration.
1066 "\\([^ \t\n\(\)]+\\)"
1068 ;; Any whitespace and declared object.
1071 '(1 font-lock-keyword-face)
1072 '(8 (cond ((match-beginning 3) font-lock-variable-name-face)
1073 ((match-beginning 6) font-lock-type-face)
1074 (t font-lock-function-name-face))
1077 "Subdued level highlighting for Lisp modes.")
1079 (defconst lisp-font-lock-keywords-2
1080 (append lisp-font-lock-keywords-1
1083 ;; Control structures. ELisp and CLisp combined.
1085 ; '("cond" "if" "while" "let\\*?" "prog[nv12*]?" "inline" "catch" "throw"
1086 ; "save-restriction" "save-excursion" "save-window-excursion"
1087 ; "save-selected-window" "save-match-data" "unwind-protect"
1088 ; "condition-case" "track-mouse"
1089 ; "eval-after-load" "eval-and-compile" "eval-when-compile"
1090 ; "when" "unless" "do" "flet" "labels" "return" "return-from"))
1094 "\\(c\\(atch\\|ond\\(\\|ition-case\\)\\)\\|do\\|"
1095 "eval-\\(a\\(fter-load\\|nd-compile\\)\\|when-compile\\)\\|flet\\|"
1096 "i\\(f\\|nline\\)\\|l\\(abels\\|et\\*?\\)\\|prog[nv12*]?\\|"
1097 "return\\(\\|-from\\)\\|"
1098 "save-\\(excursion\\|match-data\\|restriction\\|selected-window\\|"
1099 "window-excursion\\)\\|t\\(hrow\\|rack-mouse\\)\\|"
1100 "un\\(less\\|wind-protect\\)\\|wh\\(en\\|ile\\)\\)"
1103 ;; Feature symbols as references.
1104 '("(\\(featurep\\|provide\\|require\\)\\>[ \t']*\\(\\sw+\\)?"
1105 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1107 ;; Words inside \\[] tend to be for `substitute-command-keys'.
1108 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-reference-face prepend)
1110 ;; Words inside `' tend to be symbol names.
1111 '("`\\(\\sw\\sw+\\)'" 1 font-lock-reference-face prepend)
1113 ;; CLisp `:' keywords as references.
1114 '("\\<:\\sw+\\>" 0 font-lock-reference-face prepend)
1116 ;; ELisp and CLisp `&' keywords as types.
1117 '("\\<\\&\\(optional\\|rest\\|whole\\)\\>" . font-lock-type-face)
1119 "Gaudy level highlighting for Lisp modes.")
1121 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
1122 "Default expressions to highlight in Lisp modes.")
1125 (defvar scheme-font-lock-keywords
1129 ;; Declarations. Hannes Haug <hannes.haug@student.uni-tuebingen.de> says
1130 ;; this works for SOS, STklos, SCOOPS, Meroon and Tiny CLOS.
1131 (list (concat "(\\(define\\("
1133 "\\(\\|-\\(generic\\(\\|-procedure\\)\\|method\\)\\)\\|"
1134 ;; Macro names, as variable names. A bit dubious, this.
1139 ;; Any whitespace and declared object.
1142 '(1 font-lock-keyword-face)
1143 '(8 (cond ((match-beginning 3) font-lock-function-name-face)
1144 ((match-beginning 6) font-lock-variable-name-face)
1145 (t font-lock-type-face))
1148 ;; Control structures.
1149 ;(make-regexp '("begin" "call-with-current-continuation" "call/cc"
1150 ; "call-with-input-file" "call-with-output-file" "case" "cond"
1151 ; "do" "else" "for-each" "if" "lambda"
1152 ; "let\\*?" "let-syntax" "letrec" "letrec-syntax"
1153 ; ;; Hannes Haug <hannes.haug@student.uni-tuebingen.de> wants:
1154 ; "and" "or" "delay"
1155 ; ;; Stefan Monnier <stefan.monnier@epfl.ch> says don't bother:
1156 ; ;;"quasiquote" "quote" "unquote" "unquote-splicing"
1157 ; "map" "syntax" "syntax-rules"))
1160 "and\\|begin\\|c\\(a\\(ll\\(-with-\\(current-continuation\\|"
1161 "input-file\\|output-file\\)\\|/cc\\)\\|se\\)\\|ond\\)\\|"
1162 "d\\(elay\\|o\\)\\|else\\|for-each\\|if\\|"
1163 "l\\(ambda\\|et\\(-syntax\\|\\*?\\|rec\\(\\|-syntax\\)\\)\\)\\|"
1164 "map\\|or\\|syntax\\(\\|-rules\\)"
1167 ;; David Fox <fox@graphics.cs.nyu.edu> for SOS/STklos class specifiers.
1168 '("\\<<\\sw+>\\>" . font-lock-type-face)
1170 ;; Scheme `:' keywords as references.
1171 '("\\<:\\sw+\\>" . font-lock-reference-face)
1173 "Default expressions to highlight in Scheme modes.")
1176 (defconst c-font-lock-keywords-1 nil
1177 "Subdued level highlighting for C modes.")
1179 (defconst c-font-lock-keywords-2 nil
1180 "Medium level highlighting for C modes.")
1182 (defconst c-font-lock-keywords-3 nil
1183 "Gaudy level highlighting for C modes.")
1185 (defconst c++-font-lock-keywords-1 nil
1186 "Subdued level highlighting for C++ modes.")
1188 (defconst c++-font-lock-keywords-2 nil
1189 "Medium level highlighting for C++ modes.")
1191 (defconst c++-font-lock-keywords-3 nil
1192 "Gaudy level highlighting for C++ modes.")
1194 (defun font-lock-match-c++-style-declaration-item-and-skip-to-next (limit)
1195 ;; Match, and move over, any declaration/definition item after point.
1196 ;; The expect syntax of an item is "word" or "word::word", possibly ending
1197 ;; with optional whitespace and a "(". Everything following the item (but
1198 ;; belonging to it) is expected to by skip-able by `forward-sexp', and items
1199 ;; are expected to be separated with a "," or ";".
1200 (if (looking-at "[ \t*&]*\\(\\sw+\\)\\(::\\(\\sw+\\)\\)?[ \t]*\\((\\)?")
1204 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
1205 (narrow-to-region (point-min) limit)
1206 (goto-char (match-end 1))
1207 ;; Move over any item value, etc., to the next item.
1208 (while (not (looking-at "[ \t]*\\([,;]\\|$\\)"))
1209 (goto-char (or (scan-sexps (point) 1) (point-max))))
1210 (goto-char (match-end 0)))
1214 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while")
1215 "break\\|continue\\|do\\|else\\|for\\|if\\|return\\|switch\\|while")
1217 ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
1218 ; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
1219 ; "void" "volatile" "const")
1220 (concat "auto\\|c\\(har\\|onst\\)\\|double\\|e\\(num\\|xtern\\)\\|"
1221 "float\\|int\\|long\\|register\\|"
1222 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|typedef\\|"
1223 "un\\(ion\\|signed\\)\\|vo\\(id\\|latile\\)")) ; 6 ()s deep.
1225 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while"
1226 ; "asm" "catch" "delete" "new" "operator" "sizeof" "this" "throw" "try"
1227 ; "protected" "private" "public")
1228 (concat "asm\\|break\\|c\\(atch\\|ontinue\\)\\|d\\(elete\\|o\\)\\|"
1229 "else\\|for\\|if\\|new\\|"
1230 "p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|return\\|"
1231 "s\\(izeof\\|witch\\)\\|t\\(h\\(is\\|row\\)\\|ry\\)\\|while"))
1233 ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
1234 ; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
1235 ; "void" "volatile" "const" "class" "inline" "friend" "bool"
1236 ; "virtual" "complex" "template")
1237 (concat "auto\\|bool\\|c\\(har\\|lass\\|o\\(mplex\\|nst\\)\\)\\|"
1238 "double\\|e\\(num\\|xtern\\)\\|f\\(loat\\|riend\\)\\|"
1239 "in\\(line\\|t\\)\\|long\\|register\\|"
1240 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|"
1241 "t\\(emplate\\|ypedef\\)\\|un\\(ion\\|signed\\)\\|"
1242 "v\\(irtual\\|o\\(id\\|latile\\)\\)")) ; 11 ()s deep.
1244 (setq c-font-lock-keywords-1
1247 ;; These are all anchored at the beginning of line for speed.
1249 ;; Fontify function name definitions (GNU style; without type on line).
1250 (list (concat "^\\(\\sw+\\)[ \t]*(") 1 'font-lock-function-name-face)
1252 ;; Fontify filenames in #include <...> preprocessor directives as strings.
1253 '("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face)
1255 ;; Fontify function macro names.
1256 '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
1258 ;; Fontify symbol names in #if ... defined preprocessor directives.
1260 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
1261 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t)))
1263 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
1264 '("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?"
1265 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t))
1268 (setq c-font-lock-keywords-2
1269 (append c-font-lock-keywords-1
1272 ;; Simple regexps for speed.
1274 ;; Fontify all type specifiers.
1275 (cons (concat "\\<\\(" c-type-types "\\)\\>") 'font-lock-type-face)
1277 ;; Fontify all builtin keywords (except case, default and goto; see below).
1278 (cons (concat "\\<\\(" c-keywords "\\)\\>") 'font-lock-keyword-face)
1280 ;; Fontify case/goto keywords and targets, and case default/goto tags.
1281 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(\\sw+\\)?"
1282 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1283 '("^[ \t]*\\(\\sw+\\)[ \t]*:" 1 font-lock-reference-face)
1286 (setq c-font-lock-keywords-3
1287 (append c-font-lock-keywords-2
1289 ;; More complicated regexps for more complete highlighting for types.
1290 ;; We still have to fontify type specifiers individually, as C is so hairy.
1293 ;; Fontify all storage classes and type specifiers, plus their items.
1294 (list (concat "\\<\\(" c-type-types "\\)\\>"
1295 "\\([ \t*&]+\\sw+\\>\\)*")
1296 ;; Fontify each declaration item.
1297 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
1298 ;; Start with point after all type specifiers.
1299 (goto-char (or (match-beginning 8) (match-end 1)))
1300 ;; Finish with point after first type specifier.
1301 (goto-char (match-end 1))
1302 ;; Fontify as a variable or function name.
1303 (1 (if (match-beginning 4)
1304 font-lock-function-name-face
1305 font-lock-variable-name-face))))
1307 ;; Fontify structures, or typedef names, plus their items.
1308 '("\\(}\\)[ \t*]*\\sw"
1309 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1310 (goto-char (match-end 1)) nil
1311 (1 (if (match-beginning 4)
1312 font-lock-function-name-face
1313 font-lock-variable-name-face))))
1315 ;; Fontify anything at beginning of line as a declaration or definition.
1316 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
1317 (1 font-lock-type-face)
1318 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1319 (goto-char (or (match-beginning 2) (match-end 1))) nil
1320 (1 (if (match-beginning 4)
1321 font-lock-function-name-face
1322 font-lock-variable-name-face))))
1325 (setq c++-font-lock-keywords-1
1328 ;; The list `c-font-lock-keywords-1' less that for function names.
1329 (cdr c-font-lock-keywords-1)
1331 ;; Fontify function name definitions, possibly incorporating class name.
1333 '("^\\(\\sw+\\)\\(::\\(\\sw+\\)\\)?[ \t]*("
1334 (1 (if (match-beginning 2)
1336 font-lock-function-name-face))
1337 (3 (if (match-beginning 2) font-lock-function-name-face) nil t))
1340 (setq c++-font-lock-keywords-2
1341 (append c++-font-lock-keywords-1
1344 ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading.
1345 (cons (concat "\\<\\(" c++-type-types "\\)\\>") 'font-lock-type-face)
1347 ;; Fontify operator function name overloading.
1348 '("\\<\\(operator\\)\\>[ \t]*\\([][)(><!=+-][][)(><!=+-]?\\)?"
1349 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
1351 ;; Fontify case/goto keywords and targets, and case default/goto tags.
1352 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(\\sw+\\)?"
1353 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1354 '("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-reference-face)
1356 ;; Fontify other builtin keywords.
1357 (cons (concat "\\<\\(" c++-keywords "\\)\\>") 'font-lock-keyword-face)
1360 (setq c++-font-lock-keywords-3
1361 (append c++-font-lock-keywords-2
1363 ;; More complicated regexps for more complete highlighting for types.
1366 ;; Fontify all storage classes and type specifiers, plus their items.
1367 (list (concat "\\<\\(" c++-type-types "\\)\\>"
1368 "\\([ \t*&]+\\sw+\\>\\)*")
1369 ;; Fontify each declaration item.
1370 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
1371 ;; Start with point after all type specifiers.
1372 (goto-char (or (match-beginning 13) (match-end 1)))
1373 ;; Finish with point after first type specifier.
1374 (goto-char (match-end 1))
1375 ;; Fontify as a variable or function name.
1376 (1 (cond ((match-beginning 2) font-lock-type-face)
1377 ((match-beginning 4) font-lock-function-name-face)
1378 (t font-lock-variable-name-face)))
1379 (3 (if (match-beginning 4)
1380 font-lock-function-name-face
1381 font-lock-variable-name-face) nil t)))
1383 ;; Fontify structures, or typedef names, plus their items.
1384 '("\\(}\\)[ \t*]*\\sw"
1385 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1386 (goto-char (match-end 1)) nil
1387 (1 (if (match-beginning 4)
1388 font-lock-function-name-face
1389 font-lock-variable-name-face))))
1391 ;; Fontify anything at beginning of line as a declaration or definition.
1392 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
1393 (1 font-lock-type-face)
1394 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1395 (goto-char (or (match-beginning 2) (match-end 1))) nil
1396 (1 (cond ((match-beginning 2) font-lock-type-face)
1397 ((match-beginning 4) font-lock-function-name-face)
1398 (t font-lock-variable-name-face)))
1399 (3 (if (match-beginning 4)
1400 font-lock-function-name-face
1401 font-lock-variable-name-face) nil t)))
1405 (defvar c-font-lock-keywords c-font-lock-keywords-1
1406 "Default expressions to highlight in C mode.")
1408 (defvar c++-font-lock-keywords c++-font-lock-keywords-1
1409 "Default expressions to highlight in C++ mode.")
1412 (defvar tex-font-lock-keywords
1413 ; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
1414 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
1415 ; 2 font-lock-function-name-face)
1416 ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
1417 ; 2 font-lock-reference-face)
1418 ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
1419 ; ;; not be able to display those fonts.
1420 ; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep)
1421 ; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep)
1422 ; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face)
1423 ; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep))
1424 ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>.
1425 '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
1426 2 font-lock-function-name-face)
1427 ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
1428 2 font-lock-reference-face)
1429 ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face)
1430 "\\\\\\([a-zA-Z@]+\\|.\\)"
1431 ;; It seems a bit dubious to use `bold' and `italic' faces since we might
1432 ;; not be able to display those fonts.
1433 ;; LaTeX2e: \emph{This is emphasized}.
1434 ("\\\\emph{\\([^}]+\\)}" 1 'italic keep)
1435 ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...}
1436 ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}"
1437 3 (if (match-beginning 2) 'bold 'italic) keep)
1438 ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for good tables.
1439 ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)"
1440 3 (if (match-beginning 2) 'bold 'italic) keep))
1441 "Default expressions to highlight in TeX modes.")
1443 ;; Install ourselves:
1445 (or (assq 'font-lock-mode minor-mode-alist)
1446 (setq minor-mode-alist (cons '(font-lock-mode " Font") minor-mode-alist)))
1448 ;; Provide ourselves:
1450 (provide 'font-lock)
1452 ;;; font-lock.el ends here