(tcl-do-fill-paragraph): New function.
[emacs.git] / lisp / font-lock.el
blob51cdb033d283f370c004973f1c74831d109ed506
1 ;; Electric Font Lock Mode
2 ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
4 ;; Author: jwz, then rms and sm <simon@gnu.ai.mit.edu>
5 ;; Maintainer: FSF
6 ;; Keywords: languages, faces
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;; Commentary:
26 ;; Font Lock mode is a minor mode that causes your comments to be displayed in
27 ;; one face, strings in another, reserved words in another, and so on.
29 ;; Comments will be displayed in `font-lock-comment-face'.
30 ;; Strings will be displayed in `font-lock-string-face'.
31 ;; Regexps are used to display selected patterns in other faces.
33 ;; To make the text you type be fontified, use M-x font-lock-mode.
34 ;; When this minor mode is on, the faces of the current line are
35 ;; updated with every insertion or deletion.
37 ;; To turn Font Lock mode on automatically, add this to your .emacs file:
39 ;; (add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock)
41 ;; On a Sparc2, `font-lock-fontify-buffer' takes about 10 seconds for a 120k
42 ;; file of C code using the default configuration, and about 25 seconds using
43 ;; the more extensive configuration, though times also depend on file contents.
44 ;; You can speed this up substantially by removing some of the patterns that
45 ;; are highlighted by default. Fontifying Lisp code is significantly faster,
46 ;; because Lisp has a more regular syntax than C, so the expressions don't have
47 ;; to be as hairy.
49 ;; If you add patterns for a new mode, say foo.el's `foo-mode', say in which
50 ;; you don't want syntactic fontification to occur, you can make Font Lock mode
51 ;; use your regexps when turning on Font Lock by adding to `foo-mode-hook':
53 ;; (add-hook 'foo-mode-hook
54 ;; '(lambda () (make-local-variable 'font-lock-defaults)
55 ;; (setq font-lock-defaults '(foo-font-lock-keywords t))))
57 ;; Nasty regexps of the form "bar\\(\\|lo\\)\\|f\\(oo\\|u\\(\\|bar\\)\\)\\|lo"
58 ;; are made thusly: (make-regexp '("foo" "fu" "fubar" "bar" "barlo" "lo")) for
59 ;; efficiency. See /pub/gnu/emacs/elisp-archive/functions/make-regexp.el.Z on
60 ;; archive.cis.ohio-state.edu for this and other functions.
62 ;;; Code:
64 (defvar font-lock-comment-face 'font-lock-comment-face
65 "Face to use for comments.")
67 (defvar font-lock-string-face 'font-lock-string-face
68 "Face to use for strings.")
70 (defvar font-lock-function-name-face 'font-lock-function-name-face
71 "Face to use for function names.")
73 (defvar font-lock-variable-name-face 'font-lock-variable-name-face
74 "Face to use for variable names.")
76 (defvar font-lock-keyword-face 'font-lock-keyword-face
77 "Face to use for keywords.")
79 (defvar font-lock-type-face 'font-lock-type-face
80 "Face to use for data types.")
82 (defvar font-lock-reference-face 'font-lock-reference-face
83 "Face to use for references.")
85 (defvar font-lock-no-comments nil
86 "Non-nil means Font Lock should not fontify comments or strings.")
88 (make-variable-buffer-local 'font-lock-keywords)
89 (defvar font-lock-keywords nil
90 "*The keywords to highlight.
91 Elements should be of the form:
93 MATCHER
94 (MATCHER . MATCH)
95 (MATCHER . FACENAME)
96 (MATCHER . HIGHLIGHT)
97 (MATCHER HIGHLIGHT ...)
99 where HIGHLIGHT should be of the form (MATCH FACENAME OVERRIDE LAXMATCH).
100 MATCHER can be either the regexp to search for, or the function name to call to
101 make the search (called with one argument, the limit of the search). MATCH is
102 the subexpression of MATCHER to be highlighted. FACENAME is an expression
103 whose value is the face name to use. FACENAME's default attributes may be
104 defined in `font-lock-face-attributes'.
106 OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification may
107 be overriden. If `keep', only parts not already fontified are highlighted.
108 If LAXMATCH is non-nil, no error is signalled if there is no MATCH in MATCHER.
110 These regular expressions should not match text which spans lines. While
111 \\[font-lock-fontify-buffer] handles multi-line patterns correctly, updating
112 when you edit the buffer does not, since it considers text one line at a time.
114 Be careful composing regexps for this list;
115 the wrong pattern can dramatically slow things down!")
117 (defvar font-lock-defaults nil
118 "If set by a major mode, should be the defaults for Font Lock mode.
119 The value should look like the `cdr' of an item in `font-lock-defaults-alist'.")
121 (defvar font-lock-defaults-alist
122 (let ((tex-mode-defaults '(tex-font-lock-keywords nil nil ((?$ . "\""))))
123 (c-mode-defaults
124 '((c-font-lock-keywords c-font-lock-keywords-1 c-font-lock-keywords-2)
125 nil nil ((?_ . "w"))))
126 (c++-mode-defaults
127 '((c++-font-lock-keywords c++-font-lock-keywords-1
128 c++-font-lock-keywords-2)
129 nil nil ((?_ . "w"))))
130 (lisp-mode-defaults
131 '((lisp-font-lock-keywords lisp-font-lock-keywords-1
132 lisp-font-lock-keywords-2)
133 nil nil ((?: . "w") (?- . "w") (?* . "w")))))
134 (list
135 (cons 'bibtex-mode tex-mode-defaults)
136 (cons 'c++-c-mode c-mode-defaults)
137 (cons 'c++-mode c++-mode-defaults)
138 (cons 'c-mode c-mode-defaults)
139 (cons 'emacs-lisp-mode lisp-mode-defaults)
140 (cons 'latex-mode tex-mode-defaults)
141 (cons 'lisp-mode lisp-mode-defaults)
142 (cons 'plain-tex-mode tex-mode-defaults)
143 (cons 'scheme-mode lisp-mode-defaults)
144 (cons 'slitex-mode tex-mode-defaults)
145 (cons 'tex-mode tex-mode-defaults)))
146 "*Alist of default major mode and Font Lock defaults.
147 Each item should be a list of the form:
148 (MAJOR-MODE . (FONT-LOCK-KEYWORDS KEYWORDS-ONLY CASE-FOLD FONT-LOCK-SYNTAX))
149 where MAJOR-MODE is a symbol, and FONT-LOCK-KEYWORDS may be a symbol or a list
150 of symbols. If KEYWORDS-ONLY is non-nil, syntactic fontification (strings and
151 comments) is not performed. If CASE-FOLD is non-nil, the case of the keywords
152 is ignored when fontifying. FONT-LOCK-SYNTAX should be a list of cons pairs of
153 the form (CHAR . STRING), it is used to set the local Font Lock syntax table
154 for keyword fontification.")
156 (defvar font-lock-keywords-case-fold-search nil
157 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.")
159 (defvar font-lock-syntax-table nil
160 "Non-nil means use this syntax table for fontifying.
161 If this is nil, the major mode's syntax table is used.")
163 (defvar font-lock-verbose t
164 "*Non-nil means `font-lock-fontify-buffer' should print status messages.")
166 ;;;###autoload
167 (defvar font-lock-maximum-decoration nil
168 "Non-nil means use the maximum decoration for fontifying.
169 If nil, use the default decoration (typically the minimum available).
170 If t, use the maximum decoration available.
171 If a number, use that level of decoration (or if not available the maximum).")
173 (defvar font-lock-maximum-size
174 (if font-lock-maximum-decoration (* 150 1024) (* 300 1024))
175 "*If non-nil, the maximum size for buffers.
176 Only buffers less than this can be fontified when Font Lock mode is turned on.
177 If nil, means size is irrelevant.")
179 ;;;###autoload
180 (defvar font-lock-mode-hook nil
181 "Function or functions to run on entry to Font Lock mode.")
183 ;; Colour etc. support.
185 (defvar font-lock-display-type nil
186 "A symbol indicating the display Emacs is running under.
187 The symbol should be one of `color', `grayscale' or `mono'.
188 If Emacs guesses this display attribute wrongly, either set this variable in
189 your `~/.emacs' or set the resource `Emacs.displayType' in your `~/.Xdefaults'.
190 See also `font-lock-background-mode' and `font-lock-face-attributes'.")
192 (defvar font-lock-background-mode nil
193 "A symbol indicating the Emacs background brightness.
194 The symbol should be one of `light' or `dark'.
195 If Emacs guesses this frame attribute wrongly, either set this variable in
196 your `~/.emacs' or set the resource `Emacs.backgroundMode' in your
197 `~/.Xdefaults'.
198 See also `font-lock-display-type' and `font-lock-face-attributes'.")
200 (defvar font-lock-face-attributes nil
201 "A list of default attributes to use for face attributes.
202 Each element of the list should be of the form
204 (FACE FOREGROUND BACKGROUND BOLD-P ITALIC-P UNDERLINE-P)
206 where FACE should be one of the face symbols `font-lock-comment-face',
207 `font-lock-string-face', `font-lock-keyword-face', `font-lock-type-face',
208 `font-lock-function-name-face', `font-lock-variable-name-face', and
209 `font-lock-reference-face'. A form for each of these face symbols should be
210 provided in the list, but other face symbols and attributes may be given and
211 used in highlighting. See `font-lock-keywords'.
213 Subsequent element items should be the attributes for the corresponding
214 Font Lock mode faces. Attributes FOREGROUND and BACKGROUND should be strings
215 \(default if nil), while BOLD-P, ITALIC-P, and UNDERLINE-P should specify the
216 corresponding face attributes (yes if non-nil).
218 Emacs uses default attributes based on display type and background brightness.
219 See variables `font-lock-display-type' and `font-lock-background-mode'.
221 Resources can be used to over-ride these face attributes. For example, the
222 resource `Emacs.font-lock-comment-face.attributeUnderline' can be used to
223 specify the UNDERLINE-P attribute for face `font-lock-comment-face'.")
225 (defun font-lock-make-faces (&optional override)
226 "Make faces from `font-lock-face-attributes'.
227 A default list is used if this is nil.
228 If optional OVERRIDE is non-nil, faces that already exist are reset.
229 See `font-lock-make-face' and `list-faces-display'."
230 ;; We don't need to `setq' any of these variables, but the user can see what
231 ;; is being used if we do.
232 (if (null font-lock-display-type)
233 (setq font-lock-display-type
234 (let ((display-resource (x-get-resource ".displayType"
235 "DisplayType")))
236 (cond (display-resource (intern (downcase display-resource)))
237 ((x-display-color-p) 'color)
238 ((x-display-grayscale-p) 'grayscale)
239 (t 'mono)))))
240 (if (null font-lock-background-mode)
241 (setq font-lock-background-mode
242 (let ((bg-resource (x-get-resource ".backgroundMode"
243 "BackgroundMode"))
244 (params (frame-parameters)))
245 (cond (bg-resource (intern (downcase bg-resource)))
246 ((< (apply '+ (x-color-values
247 (cdr (assq 'background-color params))))
248 (/ (apply '+ (x-color-values "white")) 3))
249 'dark)
250 (t 'light)))))
251 (if (null font-lock-face-attributes)
252 (setq font-lock-face-attributes
253 (let ((light-bg (eq font-lock-background-mode 'light)))
254 (cond ((memq font-lock-display-type '(mono monochrome))
255 ;; Emacs 19.25's font-lock defaults:
256 ;;'((font-lock-comment-face nil nil nil t nil)
257 ;; (font-lock-string-face nil nil nil nil t)
258 ;; (font-lock-keyword-face nil nil t nil nil)
259 ;; (font-lock-function-name-face nil nil t t nil)
260 ;; (font-lock-type-face nil nil nil t nil))
261 (list '(font-lock-comment-face nil nil t t nil)
262 '(font-lock-string-face nil nil nil t nil)
263 '(font-lock-keyword-face nil nil t nil nil)
264 (list
265 'font-lock-function-name-face
266 (cdr (assq 'background-color (frame-parameters)))
267 (cdr (assq 'foreground-color (frame-parameters)))
268 t nil nil)
269 '(font-lock-variable-name-face nil nil t t nil)
270 '(font-lock-type-face nil nil t nil t)
271 '(font-lock-reference-face nil nil t nil t)))
272 ((memq font-lock-display-type '(grayscale greyscale
273 grayshade greyshade))
274 (list
275 (list 'font-lock-comment-face
276 nil (if light-bg "Gray80" "DimGray") t t nil)
277 (list 'font-lock-string-face
278 nil (if light-bg "Gray50" "LightGray") nil t nil)
279 (list 'font-lock-keyword-face
280 nil (if light-bg "Gray90" "DimGray") t nil nil)
281 (list 'font-lock-function-name-face
282 (cdr (assq 'background-color (frame-parameters)))
283 (cdr (assq 'foreground-color (frame-parameters)))
284 t nil nil)
285 (list 'font-lock-variable-name-face
286 nil (if light-bg "Gray90" "DimGray") t t nil)
287 (list 'font-lock-type-face
288 nil (if light-bg "Gray80" "DimGray") t nil t)
289 (list 'font-lock-reference-face
290 nil (if light-bg "LightGray" "Gray50") t nil t)))
291 (light-bg ; light colour background
292 '((font-lock-comment-face "Firebrick")
293 (font-lock-string-face "RosyBrown")
294 (font-lock-keyword-face "Purple")
295 (font-lock-function-name-face "Blue")
296 (font-lock-variable-name-face "DarkGoldenrod")
297 (font-lock-type-face "DarkOliveGreen")
298 (font-lock-reference-face "CadetBlue")))
299 (t ; dark colour background
300 '((font-lock-comment-face "OrangeRed")
301 (font-lock-string-face "LightSalmon")
302 (font-lock-keyword-face "LightSteelBlue")
303 (font-lock-function-name-face "LightSkyBlue")
304 (font-lock-variable-name-face "LightGoldenrod")
305 (font-lock-type-face "PaleGreen")
306 (font-lock-reference-face "Aquamarine")))))))
307 ;; Now make the faces if we have to.
308 (mapcar (function (lambda (face-attributes)
309 (let ((face (nth 0 face-attributes)))
310 (if (and (not override) (facep face))
311 ;; The face exists. Only set the variable if it's nil.
312 (if (or (not (boundp face)) (symbol-value face))
313 (set face face))
314 ;; The face doesn't exist or we can stomp all over it anyway.
315 (font-lock-make-face face-attributes)))))
316 font-lock-face-attributes))
318 (defun font-lock-make-face (face-attributes)
319 "Make a face from FACE-ATTRIBUTES.
320 FACE-ATTRIBUTES should be like an element `font-lock-face-attributes', so that
321 the face name is the first item in the list. A variable with the same name as
322 the face is also set; its value is the face name."
323 (let* ((face (nth 0 face-attributes))
324 (face-name (symbol-name face))
325 (set-p (function (lambda (face-name resource)
326 (x-get-resource (concat face-name ".attribute" resource)
327 (concat "Face.Attribute" resource)))))
328 (on-p (function (lambda (face-name resource)
329 (let ((set (funcall set-p face-name resource)))
330 (and set (member (downcase set) '("on" "true"))))))))
331 (make-face face)
332 ;; Set attributes not set from X resources (and therefore `make-face').
333 (or (funcall set-p face-name "Foreground")
334 (condition-case nil
335 (set-face-foreground face (nth 1 face-attributes))
336 (error nil)))
337 (or (funcall set-p face-name "Background")
338 (condition-case nil
339 (set-face-background face (nth 2 face-attributes))
340 (error nil)))
341 (if (funcall set-p face-name "Bold")
342 (and (funcall on-p face-name "Bold") (make-face-bold face nil t))
343 (and (nth 3 face-attributes) (make-face-bold face nil t)))
344 (if (funcall set-p face-name "Italic")
345 (and (funcall on-p face-name "Italic") (make-face-italic face nil t))
346 (and (nth 4 face-attributes) (make-face-italic face nil t)))
347 (or (funcall set-p face-name "Underline")
348 (set-face-underline-p face (nth 5 face-attributes)))
349 (set face face)))
351 ;; Fontification.
353 ;; These variables record, for each buffer, the parse state at a particular
354 ;; position, always the start of a line. Used to make font-lock-fontify-region
355 ;; faster.
356 (defvar font-lock-cache-position nil)
357 (defvar font-lock-cache-state nil)
358 (make-variable-buffer-local 'font-lock-cache-position)
359 (make-variable-buffer-local 'font-lock-cache-state)
361 (defun font-lock-fontify-region (start end &optional loudly)
362 "Put proper face on each string and comment between START and END."
363 (save-excursion
364 (save-restriction
365 (widen)
366 (goto-char start)
367 (beginning-of-line)
368 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
369 (let ((inhibit-read-only t) (buffer-undo-list t) (buffer-file-name)
370 (modified (buffer-modified-p))
371 (old-syntax (syntax-table))
372 (synstart (if comment-start-skip
373 (concat "\\s\"\\|" comment-start-skip)
374 "\\s\""))
375 (comstart (if comment-start-skip
376 (concat "\\s<\\|" comment-start-skip)
377 "\\s<"))
378 (startline (point))
379 state prev prevstate)
380 (unwind-protect
381 (progn
382 (if font-lock-syntax-table
383 (set-syntax-table font-lock-syntax-table))
384 ;; Find the state at the line-beginning before START.
385 (if (eq startline font-lock-cache-position)
386 (setq state font-lock-cache-state)
387 ;; Find outermost containing sexp.
388 (beginning-of-defun)
389 ;; Find the state at STARTLINE.
390 (while (< (point) startline)
391 (setq state (parse-partial-sexp (point) startline 0)))
392 (setq font-lock-cache-state state
393 font-lock-cache-position (point)))
394 ;; Now find the state precisely at START.
395 (setq state (parse-partial-sexp (point) start nil nil state))
396 ;; If the region starts inside a string, show the extent of it.
397 (if (nth 3 state)
398 (let ((beg (point)))
399 (while (and (re-search-forward "\\s\"" end 'move)
400 (nth 3 (parse-partial-sexp beg (point) nil nil
401 state))))
402 (put-text-property beg (point) 'face font-lock-string-face)
403 (setq state (parse-partial-sexp beg (point)
404 nil nil state))))
405 ;; Likewise for a comment.
406 (if (or (nth 4 state) (nth 7 state))
407 (let ((beg (point)))
408 (save-restriction
409 (narrow-to-region (point-min) end)
410 (condition-case nil
411 (progn
412 (re-search-backward comstart (point-min) 'move)
413 (forward-comment 1)
414 ;; forward-comment skips all whitespace,
415 ;; so go back to the real end of the comment.
416 (skip-chars-backward " \t"))
417 (error (goto-char end))))
418 (put-text-property beg (point) 'face
419 font-lock-comment-face)
420 (setq state (parse-partial-sexp beg (point)
421 nil nil state))))
422 ;; Find each interesting place between here and END.
423 (while (and (< (point) end)
424 (setq prev (point) prevstate state)
425 (re-search-forward synstart end t)
426 (progn
427 ;; Clear out the fonts of what we skip over.
428 (remove-text-properties prev (point) '(face nil))
429 ;; Verify the state at that place
430 ;; so we don't get fooled by \" or \;.
431 (setq state (parse-partial-sexp prev (point)
432 nil nil state))))
433 (let ((here (point)))
434 (if (or (nth 4 state) (nth 7 state))
435 ;; We found a real comment start.
436 (let ((beg (match-beginning 0)))
437 (goto-char beg)
438 (save-restriction
439 (narrow-to-region (point-min) end)
440 (condition-case nil
441 (progn
442 (forward-comment 1)
443 ;; forward-comment skips all whitespace,
444 ;; so go back to the real end of the comment.
445 (skip-chars-backward " \t"))
446 (error (goto-char end))))
447 (put-text-property beg (point) 'face
448 font-lock-comment-face)
449 (setq state (parse-partial-sexp here (point)
450 nil nil state)))
451 (if (nth 3 state)
452 (let ((beg (match-beginning 0)))
453 (while (and (re-search-forward "\\s\"" end 'move)
454 (nth 3 (parse-partial-sexp
455 here (point) nil nil state))))
456 (put-text-property beg (point) 'face
457 font-lock-string-face)
458 (setq state (parse-partial-sexp here (point)
459 nil nil state))))))
460 ;; Make sure PREV is non-nil after the loop
461 ;; only if it was set on the very last iteration.
462 (setq prev nil)))
463 (set-syntax-table old-syntax)
464 (and prev
465 (remove-text-properties prev end '(face nil)))
466 (and (buffer-modified-p)
467 (not modified)
468 (set-buffer-modified-p nil)))))))
471 (defun font-lock-unfontify-region (beg end)
472 (let ((modified (buffer-modified-p))
473 (buffer-undo-list t) (inhibit-read-only t) (buffer-file-name))
474 (remove-text-properties beg end '(face nil))
475 (and (buffer-modified-p)
476 (not modified)
477 (set-buffer-modified-p nil))))
479 ;; Called when any modification is made to buffer text.
480 (defun font-lock-after-change-function (beg end old-len)
481 (save-excursion
482 (save-match-data
483 ;; Discard the cache info if text before it has changed.
484 (and font-lock-cache-position
485 (> font-lock-cache-position beg)
486 (setq font-lock-cache-position nil))
487 ;; Rescan between start of line from `beg' and start of line after `end'.
488 (goto-char beg)
489 (beginning-of-line)
490 (setq beg (point))
491 (goto-char end)
492 (forward-line 1)
493 (setq end (point))
494 ;; First scan for strings and comments.
495 ;; Must scan from line start in case of
496 ;; inserting space into `intfoo () {}', and after widened.
497 (if font-lock-no-comments
498 (font-lock-unfontify-region beg end)
499 (font-lock-fontify-region beg end))
500 ;; Now scan for keywords.
501 (font-lock-hack-keywords beg end))))
503 ;; The following must be rethought, since keywords can override fontification.
504 ; ;; Now scan for keywords, but not if we are inside a comment now.
505 ; (or (and (not font-lock-no-comments)
506 ; (let ((state (parse-partial-sexp beg end nil nil
507 ; font-lock-cache-state)))
508 ; (or (nth 4 state) (nth 7 state))))
509 ; (font-lock-hack-keywords beg end))
511 ;;; Fontifying arbitrary patterns
513 (defun font-lock-compile-keywords (&optional keywords)
514 ;; Compile `font-lock-keywords' into the form (t KEYWORD ...) where KEYWORD
515 ;; is the (MATCHER HIGHLIGHT ...) shown in the variable's doc string.
516 (let ((keywords (or keywords font-lock-keywords)))
517 (setq font-lock-keywords
518 (if (eq (car-safe keywords) t)
519 keywords
520 (cons t
521 (mapcar
522 (function (lambda (item)
523 (cond ((nlistp item)
524 (list item '(0 font-lock-keyword-face)))
525 ((numberp (cdr item))
526 (list (car item) (list (cdr item) 'font-lock-keyword-face)))
527 ((symbolp (cdr item))
528 (list (car item) (list 0 (cdr item))))
529 ((nlistp (nth 1 item))
530 (list (car item) (cdr item)))
532 item))))
533 keywords))))))
535 (defsubst font-lock-apply-highlight (highlight)
536 "Apply HIGHLIGHT following a match. See `font-lock-keywords'."
537 (let* ((match (nth 0 highlight))
538 (beg (match-beginning match)) (end (match-end match))
539 (override (nth 2 highlight)))
540 (cond ((not beg)
541 ;; No match but we might not signal an error
542 (or (nth 3 highlight) (error "Highlight %S failed" highlight)))
543 ((and (not override) (text-property-not-all beg end 'face nil))
544 ;; Can't override and already fontified
545 nil)
546 ((not (eq override 'keep))
547 ;; Can override but need not keep existing fontification
548 (put-text-property beg end 'face (eval (nth 1 highlight))))
550 ;; Can override but must keep existing fontification
551 (let ((pos (text-property-any beg end 'face nil)) next
552 (face (eval (nth 1 highlight))))
553 (while pos
554 (setq next (next-single-property-change pos 'face nil end))
555 (put-text-property pos next 'face face)
556 (setq pos (text-property-any next end 'face nil))))))))
558 (defun font-lock-hack-keywords (start end &optional loudly)
559 "Fontify according to `font-lock-keywords' between START and END."
560 (let ((case-fold-search font-lock-keywords-case-fold-search)
561 (keywords (cdr (if (eq (car-safe font-lock-keywords) t)
562 font-lock-keywords
563 (font-lock-compile-keywords))))
564 (count 0)
565 (inhibit-read-only t) (buffer-undo-list t) (buffer-file-name)
566 (modified (buffer-modified-p))
567 (old-syntax (syntax-table))
568 (bufname (buffer-name)))
569 (unwind-protect
570 (let (keyword matcher highlights)
571 (if loudly (message "Fontifying %s... (regexps...)" bufname))
572 (if font-lock-syntax-table (set-syntax-table font-lock-syntax-table))
573 (while keywords
574 (setq keyword (car keywords) keywords (cdr keywords)
575 matcher (car keyword) highlights (cdr keyword))
576 (goto-char start)
577 (while (if (stringp matcher)
578 (re-search-forward matcher end t)
579 (funcall matcher end))
580 (mapcar 'font-lock-apply-highlight highlights))
581 (if loudly (message "Fontifying %s... (regexps...%s)" bufname
582 (make-string (setq count (1+ count)) ?.)))))
583 (set-syntax-table old-syntax)
584 (and (buffer-modified-p)
585 (not modified)
586 (set-buffer-modified-p nil)))))
588 ;; The user level functions
590 (defvar font-lock-mode nil) ; for modeline
592 (defvar font-lock-fontified nil) ; whether we have hacked this buffer
593 (put 'font-lock-fontified 'permanent-local t)
595 ;;;###autoload
596 (defun font-lock-mode (&optional arg)
597 "Toggle Font Lock mode.
598 With arg, turn Font Lock mode on if and only if arg is positive.
600 When Font Lock mode is enabled, text is fontified as you type it:
602 - Comments are displayed in `font-lock-comment-face';
603 - Strings are displayed in `font-lock-string-face';
604 - Certain other expressions are displayed in other faces according to the
605 value of the variable `font-lock-keywords'.
607 You can enable Font Lock mode in any major mode automatically by turning on in
608 the major mode's hook. For example, put in your ~/.emacs:
610 (add-hook 'c-mode-hook 'turn-on-font-lock)
612 Or for any visited file with the following in your ~/.emacs:
614 (add-hook 'find-file-hooks 'turn-on-font-lock)
616 The default Font Lock mode faces and their attributes are defined in the
617 variable `font-lock-face-attributes', and Font Lock mode default settings in
618 the variable `font-lock-defaults-alist'.
620 Where modes support different levels of fontification, you can use the variable
621 `font-lock-maximum-decoration' to specify which level you generally prefer.
622 When you turn Font Lock mode on/off the buffer is fontified/defontified, though
623 fontification occurs only if the buffer is less than `font-lock-maximum-size'.
624 To fontify a buffer without turning on Font Lock mode, and regardless of buffer
625 size, you can use \\[font-lock-fontify-buffer]."
626 (interactive "P")
627 (let ((on-p (if arg (> (prefix-numeric-value arg) 0) (not font-lock-mode))))
628 (if (equal (buffer-name) " *Compiler Input*") ; hack for bytecomp...
629 (setq on-p nil))
630 (if (not on-p)
631 (remove-hook 'after-change-functions 'font-lock-after-change-function)
632 (make-local-variable 'after-change-functions)
633 (add-hook 'after-change-functions 'font-lock-after-change-function))
634 (set (make-local-variable 'font-lock-mode) on-p)
635 (cond (on-p
636 (font-lock-set-defaults)
637 (make-local-variable 'before-revert-hook)
638 (make-local-variable 'after-revert-hook)
639 ;; If buffer is reverted, must clean up the state.
640 (add-hook 'before-revert-hook 'font-lock-revert-setup)
641 (add-hook 'after-revert-hook 'font-lock-revert-cleanup)
642 (run-hooks 'font-lock-mode-hook)
643 (cond (font-lock-fontified
644 nil)
645 ((or (null font-lock-maximum-size)
646 (> font-lock-maximum-size (buffer-size)))
647 (font-lock-fontify-buffer))
648 (font-lock-verbose
649 (message "Fontifying %s... buffer too big." (buffer-name)))))
650 (font-lock-fontified
651 (setq font-lock-fontified nil)
652 (remove-hook 'before-revert-hook 'font-lock-revert-setup)
653 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup)
654 (font-lock-unfontify-region (point-min) (point-max))
655 (font-lock-thing-lock-cleanup))
657 (remove-hook 'before-revert-hook 'font-lock-revert-setup)
658 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup)
659 (font-lock-thing-lock-cleanup)))
660 (force-mode-line-update)))
662 ;;;###autoload
663 (defun turn-on-font-lock ()
664 "Unconditionally turn on Font Lock mode."
665 (font-lock-mode 1))
667 ;; Turn off other related packages if they're on. I prefer a hook. --sm.
668 ;; These explicit calls are easier to understand
669 ;; because people know what they will do.
670 ;; A hook is a mystery because it might do anything whatever. --rms.
671 (defun font-lock-thing-lock-cleanup ()
672 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
673 (fast-lock-mode -1))
674 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
675 (lazy-lock-mode -1))))
677 ;; Do something special for these packages after fontifying. I prefer a hook.
678 (defun font-lock-after-fontify-buffer ()
679 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
680 (fast-lock-after-fontify-buffer))
681 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
682 (lazy-lock-after-fontify-buffer))))
684 ;; If the buffer is about to be reverted, it won't be fontified afterward.
685 (defun font-lock-revert-setup ()
686 (setq font-lock-fontified nil))
688 ;; If the buffer has just been reverted, normally that turns off
689 ;; Font Lock mode. So turn the mode back on if necessary.
690 (defun font-lock-revert-cleanup ()
691 (font-lock-mode 1))
693 ;;;###autoload
694 (defun font-lock-fontify-buffer ()
695 "Fontify the current buffer the way `font-lock-mode' would."
696 (interactive)
697 (let ((was-on font-lock-mode)
698 (verbose (and (or font-lock-verbose (interactive-p))
699 (not (zerop (buffer-size)))))
700 (modified (buffer-modified-p)))
701 (set (make-local-variable 'font-lock-fontified) nil)
702 (if verbose (message "Fontifying %s..." (buffer-name)))
703 ;; Turn it on to run hooks and get the right `font-lock-keywords' etc.
704 (or was-on (font-lock-set-defaults))
705 (condition-case nil
706 (save-excursion
707 (if font-lock-no-comments
708 (font-lock-unfontify-region (point-min) (point-max))
709 (font-lock-fontify-region (point-min) (point-max) verbose))
710 (font-lock-hack-keywords (point-min) (point-max) verbose)
711 (setq font-lock-fontified t))
712 ;; We don't restore the old fontification, so it's best to unfontify.
713 (quit (font-lock-unfontify-region (point-min) (point-max))))
714 (if verbose (message "Fontifying %s... %s." (buffer-name)
715 (if font-lock-fontified "done" "aborted")))
716 (and (buffer-modified-p)
717 (not modified)
718 (set-buffer-modified-p nil))
719 (font-lock-after-fontify-buffer)))
721 ;;; Various information shared by several modes.
722 ;;; Information specific to a single mode should go in its load library.
724 (defconst lisp-font-lock-keywords-1
725 (list
726 ;; highlight defining forms. This doesn't work too nicely for
727 ;; (defun (setf foo) ...) but it does work for (defvar foo) which
728 ;; is more important.
729 (list (concat "^(\\(def\\(const\\|ine-key\\(\\|-after\\)\\|var\\)\\)\\>"
730 "[ \t']*\\([^ \t\n\(\)]+\\)?")
731 '(1 font-lock-keyword-face) '(4 font-lock-variable-name-face nil t))
732 (list (concat "^(\\(def[^ \t\n\(\)]+\\|eval-"
733 "\\(a\\(fter-load\\|nd-compile\\)\\|when-compile\\)\\)\\>"
734 "[ \t']*\\([^ \t\n\(\)]+\\)?")
735 '(1 font-lock-keyword-face) '(4 font-lock-function-name-face nil t))
737 "Subdued level highlighting Lisp modes.")
739 (defconst lisp-font-lock-keywords-2
740 (append lisp-font-lock-keywords-1
741 (let ((word-char "[-+a-zA-Z0-9_:*]"))
742 (list
744 ;; Control structures. ELisp and CLisp combined.
745 ; ("cond" "if" "while" "let\\*?" "prog[nv12*]?" "catch" "throw"
746 ; "save-restriction" "save-excursion" "save-window-excursion"
747 ; "save-selected-window" "save-match-data" "unwind-protect"
748 ; "condition-case" "track-mouse"
749 ; "when" "unless" "do" "flet" "labels" "return" "return-from")
750 (cons
751 (concat
752 "(\\("
753 "c\\(atch\\|ond\\(\\|ition-case\\)\\)\\|do\\|flet\\|if\\|"
754 "l\\(abels\\|et\\*?\\)\\|prog[nv12*]?\\|return\\(\\|-from\\)\\|"
755 "save-\\(excursion\\|match-data\\|restriction\\|"
756 "selected-window\\|window-excursion\\)\\|t\\(hrow\\|rack-mouse\\)\\|"
757 "un\\(less\\|wind-protect\\)\\|wh\\(en\\|ile\\)"
758 "\\)\\>") 1)
760 ;; Function names in emacs-lisp docstrings (in the syntax that
761 ;; `substitute-command-keys' understands).
762 (list (concat "\\\\\\\\\\[\\(" word-char "+\\)]")
763 1 font-lock-reference-face t)
765 ;; Words inside `' which tend to be symbol names.
766 (list (concat "`\\(" word-char word-char "+\\)'")
767 1 'font-lock-reference-face t)
769 ;; CLisp `:' keywords as references.
770 (list (concat "\\<:" word-char "+\\>") 0 font-lock-reference-face t)
772 ;; ELisp and CLisp `&' keywords as types.
773 '("\\&\\(optional\\|rest\\|whole\\)\\>" . font-lock-type-face)
775 "Gaudy level highlighting for Lisp modes.")
777 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
778 "Default expressions to highlight in Lisp modes.")
781 (defconst c-font-lock-keywords-1 nil
782 "Subdued level highlighting for C modes.")
784 (defconst c-font-lock-keywords-2 nil
785 "Gaudy level highlighting for C modes.")
787 (defconst c++-font-lock-keywords-1 nil
788 "Subdued level highlighting for C++ modes.")
790 (defconst c++-font-lock-keywords-2 nil
791 "Gaudy level highlighting for C++ modes.")
793 (let ((c-keywords
794 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while")
795 "break\\|continue\\|do\\|else\\|for\\|if\\|return\\|switch\\|while")
796 (c-type-types
797 ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
798 ; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
799 ; "void" "volatile" "const")
800 (concat "auto\\|c\\(har\\|onst\\)\\|double\\|e\\(num\\|xtern\\)\\|"
801 "float\\|int\\|long\\|register\\|"
802 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|typedef\\|"
803 "un\\(ion\\|signed\\)\\|vo\\(id\\|latile\\)")) ; 6 ()s deep.
804 (c++-keywords
805 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while"
806 ; "asm" "catch" "delete" "new" "operator" "sizeof" "this" "throw" "try"
807 ; "protected" "private" "public")
808 (concat "asm\\|break\\|c\\(atch\\|ontinue\\)\\|d\\(elete\\|o\\)\\|"
809 "else\\|for\\|if\\|new\\|operator\\|"
810 "p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|return\\|"
811 "s\\(izeof\\|witch\\)\\|t\\(h\\(is\\|row\\)\\|ry\\)\\|while"))
812 (c++-type-types
813 ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
814 ; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
815 ; "void" "volatile" "const" "class" "inline" "friend" "bool"
816 ; "virtual" "complex" "template")
817 (concat "auto\\|bool\\|c\\(har\\|lass\\|o\\(mplex\\|nst\\)\\)\\|"
818 "double\\|e\\(num\\|xtern\\)\\|f\\(loat\\|riend\\)\\|"
819 "in\\(line\\|t\\)\\|long\\|register\\|"
820 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|"
821 "t\\(emplate\\|ypedef\\)\\|un\\(ion\\|signed\\)\\|"
822 "v\\(irtual\\|o\\(id\\|latile\\)\\)")) ; 11 ()s deep.
823 (ctoken "[a-zA-Z0-9_:~]+"))
824 (setq c-font-lock-keywords-1
825 (list
827 ;; Fontify filenames in #include <...> preprocessor directives.
828 '("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face)
830 ;; Fontify function macro names.
831 '("^#[ \t]*define[ \t]+\\(\\(\\sw+\\)(\\)" 2 font-lock-function-name-face)
833 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
834 '("^\\(#[ \t]*[a-z]+\\)\\>[ \t]*\\(\\sw+\\)?"
835 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t))
837 ;; Fontify function name definitions (without type on line).
838 (list (concat "^\\(" ctoken "\\)[ \t]*(") 1 'font-lock-function-name-face)
841 (setq c-font-lock-keywords-2
842 (append c-font-lock-keywords-1
843 (list
845 ;; Fontify all storage classes and type specifiers (before declarations).
846 (cons (concat "\\<\\(" c-type-types "\\)\\>") 'font-lock-type-face)
848 ;; Fontify variable/structure name declarations and definitions, or
849 ;; function name declarations (plus definitions with type on same line).
850 (list (concat "\\<\\(" c-type-types "\\)[ \t*]+"
851 "\\(" ctoken "[ \t*]+\\)*"
852 "\\(" ctoken "\\)[ \t]*\\((\\)?")
854 '(if (match-beginning 10)
855 font-lock-function-name-face
856 font-lock-variable-name-face))
858 ;; Fontify function/variable name declarations at the start of the line.
859 ;; (Not everyone follows the GNU convention of function name at the start.)
860 (list (concat "^" ctoken "[ \t*]+"
861 "\\(" ctoken "[ \t*]+\\)*"
862 "\\(" ctoken "\\)[ \t]*\\((\\)?")
864 '(if (match-beginning 3)
865 font-lock-function-name-face
866 font-lock-variable-name-face))
868 ;; Fontify variable names declared with structures, or typedef names.
869 '("}[ \t*]*\\(\\sw+\\)[ \t]*[;,[]" 1 font-lock-variable-name-face)
871 ;; Fontify all builtin keywords (except case, default and goto; see below).
872 (concat "\\<\\(" c-keywords "\\)\\>")
874 ;; Fontify case/goto keywords and targets, and goto tags (incl "default:").
875 '("\\<\\(case\\|goto\\)\\>[ \t]*\\([^ \t\n:;]+\\)?"
876 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
877 '("^[ \t]*\\(\\sw+\\)[ \t]*:" 1 font-lock-reference-face)
880 (setq c++-font-lock-keywords-1 c-font-lock-keywords-1)
881 (setq c++-font-lock-keywords-2
882 (append c++-font-lock-keywords-1
883 (list
884 ;; We don't just add to the C keywords for subtle differences and speed.
885 ;; See the above comments for `c-font-lock-keywords-2'.
886 (cons (concat "\\<\\(" c++-type-types "\\)\\>") 'font-lock-type-face)
887 (list (concat "\\<\\(" c++-type-types "\\)[ \t*&]+"
888 "\\(" ctoken "[ \t*&]+\\)*"
889 "\\(" ctoken "\\)[ \t]*\\((\\)?")
891 '(if (match-beginning 15)
892 font-lock-function-name-face
893 font-lock-variable-name-face))
894 (list (concat "^" ctoken "[ \t*]+"
895 "\\(" ctoken "[ \t*]+\\)*"
896 "\\(" ctoken "\\)[ \t]*\\((\\)?")
898 '(if (match-beginning 3)
899 font-lock-function-name-face
900 font-lock-variable-name-face))
901 '("}[ \t*]*\\(\\sw+\\)[ \t]*[;,[]" 1 font-lock-variable-name-face)
902 (concat "\\<\\(" c++-keywords "\\)\\>")
903 '("\\<\\(case\\|goto\\)\\>[ \t]*\\([^ \t\n:;]+\\)?"
904 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
905 '("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-reference-face))))
908 (defvar c-font-lock-keywords c-font-lock-keywords-1
909 "Default expressions to highlight in C mode.")
911 (defvar c++-font-lock-keywords c++-font-lock-keywords-1
912 "Default expressions to highlight in C++ mode.")
914 (defvar tex-font-lock-keywords
915 ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
916 '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
917 2 font-lock-function-name-face)
918 ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
919 2 font-lock-reference-face)
920 ;; It seems a bit dubious to use `bold' and `italic' faces since we might
921 ;; not be able to display those fonts.
922 ("{\\\\bf\\([^}]+\\)}" 1 'bold keep)
923 ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep)
924 ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face)
925 ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep))
926 "Additional expressions to highlight in TeX mode.")
928 (defun font-lock-choose-keywords (keywords level)
929 ;; Return evaled LEVELth element of KEYWORDS. A LEVEL of nil is equal to a
930 ;; LEVEL of 0, a LEVEL of t is equal to (1- (length KEYWORDS)).
931 (cond ((symbolp keywords)
932 keywords)
933 ((numberp level)
934 (or (nth level keywords) (car (reverse keywords))))
935 ((eq level t)
936 (car (reverse keywords)))
938 (car keywords))))
940 (defun font-lock-set-defaults ()
941 "Set fontification defaults appropriately for this mode.
942 Sets `font-lock-keywords', `font-lock-no-comments', `font-lock-syntax-table'
943 and `font-lock-keywords-case-fold-search' using `font-lock-defaults' (or, if
944 nil, using `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
945 ;; Set face defaults.
946 (font-lock-make-faces)
947 ;; Set fontification defaults.
948 (or font-lock-keywords
949 (let ((defaults (or font-lock-defaults
950 (cdr (assq major-mode font-lock-defaults-alist)))))
951 ;; Keywords?
952 (setq font-lock-keywords
953 (font-lock-compile-keywords
954 (eval (font-lock-choose-keywords (nth 0 defaults)
955 font-lock-maximum-decoration))))
956 ;; Syntactic?
957 (if (nth 1 defaults)
958 (set (make-local-variable 'font-lock-no-comments) t))
959 ;; Case fold?
960 (if (nth 2 defaults)
961 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
962 ;; Syntax table?
963 (if (nth 3 defaults)
964 (let ((slist (nth 3 defaults)))
965 (set (make-local-variable 'font-lock-syntax-table)
966 (copy-syntax-table (syntax-table)))
967 (while slist
968 (modify-syntax-entry (car (car slist)) (cdr (car slist))
969 font-lock-syntax-table)
970 (setq slist (cdr slist))))))))
972 ;; Install ourselves:
974 (or (assq 'font-lock-mode minor-mode-alist)
975 (setq minor-mode-alist (cons '(font-lock-mode " Font") minor-mode-alist)))
977 ;; Provide ourselves:
979 (provide 'font-lock)
981 ;;; font-lock.el ends here