comparison lisp/font-lock.el @ 9487:f80b2a33df03

* font-lock.el: To make font-lock.el dumpable: (font-lock-display-type, font-lock-background-mode, font-lock-face-attributes): Default to nil. (font-lock-make-faces): New function. Set them. (font-lock.el): Add to after-init-hook if noninteractive or use it. (font-lock-maximum-size): New variable. (font-lock-mode): Use it to fontify only if buffer is within it. (font-lock-defaults, font-lock-defaults-alist): Item for syntax table. (font-lock-set-defaults): Set font-lock-syntax-table too. (lisp-font-lock-keywords-2): Fix CLisp end-of-keyword in regexp. (c-font-lock-keywords-*): Specific to ANSI C. (c++-font-lock-keywords-*): Specific to ANSI C++.
author Simon Marshall <simon@gnu.org>
date Wed, 12 Oct 1994 09:23:37 +0000
parents e21d4ebc3160
children 76ce1230257f
comparison
equal deleted inserted replaced
9486:66079477d0e6 9487:f80b2a33df03
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 23
24 24
25 ;;; Commentary: 25 ;;; Commentary:
26 26
27 ;; Font-lock-mode is a minor mode that causes your comments to be 27 ;; Font Lock mode is a minor mode that causes your comments to be displayed in
28 ;; displayed in one face, strings in another, reserved words in another, 28 ;; one face, strings in another, reserved words in another, and so on.
29 ;; documentation strings in another, and so on.
30 ;; 29 ;;
31 ;; Comments will be displayed in `font-lock-comment-face'. 30 ;; Comments will be displayed in `font-lock-comment-face'.
32 ;; Strings will be displayed in `font-lock-string-face'. 31 ;; Strings will be displayed in `font-lock-string-face'.
33 ;; Regexps are used to display selected patterns in other faces. 32 ;; Regexps are used to display selected patterns in other faces.
34 ;; 33 ;;
35 ;; To make the text you type be fontified, use M-x font-lock-mode. 34 ;; To make the text you type be fontified, use M-x font-lock-mode.
36 ;; When this minor mode is on, the fonts of the current line are 35 ;; When this minor mode is on, the faces of the current line are
37 ;; updated with every insertion or deletion. 36 ;; updated with every insertion or deletion.
38 ;; 37 ;;
39 ;; To define new reserved words or other patterns to highlight, use 38 ;; To turn Font Lock mode on automatically, add this to your .emacs file:
40 ;; the `font-lock-keywords' variable. This should be mode-local.
41 ;; 39 ;;
42 ;; To turn this on automatically, add this to your .emacs file: 40 ;; (add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock)
43 ;; 41 ;;
44 ;; (setq emacs-lisp-mode-hook 'turn-on-font-lock) 42 ;; On a Sparc2, `font-lock-fontify-buffer' takes about 10 seconds for a 120k
45 ;;
46 ;; On a Sparc2, the initial fontification takes about 10 seconds for a 120k
47 ;; file of C code using the default configuration, and about 25 seconds using 43 ;; file of C code using the default configuration, and about 25 seconds using
48 ;; the more extensive configuration, though times also depend on file contents. 44 ;; the more extensive configuration, though times also depend on file contents.
49 ;; You can speed this up substantially by removing some of the patterns that 45 ;; You can speed this up substantially by removing some of the patterns that
50 ;; are highlighted by default. Fontifying Lisp code is significantly faster, 46 ;; are highlighted by default. Fontifying Lisp code is significantly faster,
51 ;; because Lisp has a more regular syntax than C, so the expressions don't have 47 ;; because Lisp has a more regular syntax than C, so the expressions don't have
52 ;; to be as hairy. 48 ;; to be as hairy.
53 ;; 49 ;;
50 ;; If you add patterns for a new mode, say foo.el's `foo-mode', say in which
51 ;; you don't want syntactic fontification to occur, you can make Font Lock mode
52 ;; use your regexps when turning on Font Lock by adding to `foo-mode-hook':
53 ;;
54 ;; (add-hook 'foo-mode-hook
55 ;; '(lambda () (make-local-variable 'font-lock-defaults)
56 ;; (setq font-lock-defaults '(foo-font-lock-keywords t))))
57 ;;
54 ;; Nasty regexps of the form "bar\\(\\|lo\\)\\|f\\(oo\\|u\\(\\|bar\\)\\)\\|lo" 58 ;; Nasty regexps of the form "bar\\(\\|lo\\)\\|f\\(oo\\|u\\(\\|bar\\)\\)\\|lo"
55 ;; are made thusly: (make-regexp '("foo" "fu" "fubar" "bar" "barlo" "lo")) for 59 ;; are made thusly: (make-regexp '("foo" "fu" "fubar" "bar" "barlo" "lo")) for
56 ;; efficiency. See /pub/gnu/emacs/elisp-archive/functions/make-regexp.el.Z on 60 ;; efficiency. See /pub/gnu/emacs/elisp-archive/functions/make-regexp.el.Z on
57 ;; archive.cis.ohio-state.edu for this and other functions. 61 ;; archive.cis.ohio-state.edu for this and other functions.
58 62
78 82
79 (defvar font-lock-reference-face 'font-lock-reference-face 83 (defvar font-lock-reference-face 'font-lock-reference-face
80 "Face to use for references.") 84 "Face to use for references.")
81 85
82 (defvar font-lock-no-comments nil 86 (defvar font-lock-no-comments nil
83 "Non-nil means Font-Lock shouldn't check for comments or strings.") 87 "Non-nil means Font Lock should not fontify comments or strings.")
84 88
85 (make-variable-buffer-local 'font-lock-keywords) 89 (make-variable-buffer-local 'font-lock-keywords)
86 (defvar font-lock-keywords nil 90 (defvar font-lock-keywords nil
87 "*The keywords to highlight. 91 "*The keywords to highlight.
88 Elements should be of the form: 92 Elements should be of the form:
107 when you edit the buffer does not, since it considers text one line at a time. 111 when you edit the buffer does not, since it considers text one line at a time.
108 112
109 Be careful composing regexps for this list; 113 Be careful composing regexps for this list;
110 the wrong pattern can dramatically slow things down!") 114 the wrong pattern can dramatically slow things down!")
111 115
116 (defvar font-lock-defaults nil
117 "If set by a major mode, should be the defaults for Font Lock mode.
118 The value should look like the `cdr' of an item in `font-lock-defaults-alist'.")
119
120 (defvar font-lock-defaults-alist
121 '((bibtex-mode . (tex-font-lock-keywords))
122 (c++-c-mode . (c-font-lock-keywords nil nil ((?\_ . "w"))))
123 (c++-mode . (c++-font-lock-keywords nil nil ((?\_ . "w"))))
124 (c-mode . (c-font-lock-keywords nil nil ((?\_ . "w"))))
125 (emacs-lisp-mode . (lisp-font-lock-keywords))
126 (latex-mode . (tex-font-lock-keywords))
127 (lisp-mode . (lisp-font-lock-keywords))
128 (plain-tex-mode . (tex-font-lock-keywords))
129 (slitex-mode . (tex-font-lock-keywords))
130 (tex-mode . (tex-font-lock-keywords)))
131 "*Alist of default major mode and Font Lock defaults.
132 Each item should be a list of the form:
133 (MAJOR-MODE . (FONT-LOCK-KEYWORDS KEYWORDS-ONLY CASE-FOLD FONT-LOCK-SYNTAX))
134 where both MAJOR-MODE and FONT-LOCK-KEYWORDS are symbols. If KEYWORDS-ONLY is
135 non-nil, syntactic fontification (strings and comments) is not performed.
136 If CASE-FOLD is non-nil, the case of the keywords is ignored when fontifying.
137 FONT-LOCK-SYNTAX should be a list of cons pairs of the form (CHAR . STRING), it
138 is used to set the local Font Lock syntax table for keyword fontification.")
139
140 (defvar font-lock-maximum-size (* 100 1024)
141 "*If non-nil, the maximum size for buffers.
142 Only buffers less than are fontified when Font Lock mode is turned on.
143 If nil, means size is irrelevant.")
144
112 (defvar font-lock-keywords-case-fold-search nil 145 (defvar font-lock-keywords-case-fold-search nil
113 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.") 146 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.")
114 147
115 (defvar font-lock-syntax-table nil 148 (defvar font-lock-syntax-table nil
116 "*Non-nil means use this syntax table for fontifying. 149 "*Non-nil means use this syntax table for fontifying.
123 (defvar font-lock-mode-hook nil 156 (defvar font-lock-mode-hook nil
124 "Function or functions to run on entry to Font Lock mode.") 157 "Function or functions to run on entry to Font Lock mode.")
125 158
126 ;; Colour etc. support. 159 ;; Colour etc. support.
127 160
128 (defvar font-lock-display-type 161 (defvar font-lock-display-type nil
129 (let ((display-resource (x-get-resource ".displayType" "DisplayType")))
130 (cond (display-resource (intern (downcase display-resource)))
131 ((x-display-color-p) 'color)
132 ((x-display-grayscale-p) 'grayscale)
133 (t 'mono)))
134 "A symbol indicating the display Emacs is running under. 162 "A symbol indicating the display Emacs is running under.
135 The symbol should be one of `color', `grayscale' or `mono'. 163 The symbol should be one of `color', `grayscale' or `mono'.
136 If Emacs guesses this display attribute wrongly, either set this variable in 164 If Emacs guesses this display attribute wrongly, either set this variable in
137 your `~/.emacs' file, or set the resource `Emacs.displayType' 165 your `~/.emacs' or set the resource `Emacs.displayType' in your `~/.Xdefaults'.
138 in your `~/.Xdefaults' file.
139 See also `font-lock-background-mode' and `font-lock-face-attributes'.") 166 See also `font-lock-background-mode' and `font-lock-face-attributes'.")
140 167
141 (defvar font-lock-background-mode 168 (defvar font-lock-background-mode nil
142 (let ((bg-resource (x-get-resource ".backgroundMode" "BackgroundMode"))
143 (params (frame-parameters)))
144 (cond (bg-resource (intern (downcase bg-resource)))
145 ((or (string-equal (cdr (assq 'foreground-color params)) "white")
146 (string-equal (cdr (assq 'background-color params)) "black"))
147 'dark)
148 (t 'light)))
149 "A symbol indicating the Emacs background brightness. 169 "A symbol indicating the Emacs background brightness.
150 The symbol should be one of `light' or `dark'. 170 The symbol should be one of `light' or `dark'.
151 If Emacs guesses this frame attribute wrongly, either set this variable in 171 If Emacs guesses this frame attribute wrongly, either set this variable in
152 your `~/.emacs' file or set the resource `Emacs.backgroundMode' 172 your `~/.emacs' or set the resource `Emacs.backgroundMode' in your
153 in your `~/.Xdefaults' file. 173 `~/.Xdefaults'.
154 See also `font-lock-display-type' and `font-lock-face-attributes'.") 174 See also `font-lock-display-type' and `font-lock-face-attributes'.")
155 175
156 (defvar font-lock-face-attributes 176 (defvar font-lock-face-attributes nil
157 (let ((light-bg (eq font-lock-background-mode 'light)))
158 (cond ((memq font-lock-display-type '(mono monochrome))
159 ;; Emacs 19.25's font-lock defaults:
160 ;;'((font-lock-comment-face nil nil nil t nil)
161 ;; (font-lock-string-face nil nil nil nil t)
162 ;; (font-lock-keyword-face nil nil t nil nil)
163 ;; (font-lock-function-name-face nil nil t t nil)
164 ;; (font-lock-type-face nil nil nil t nil))
165 (list '(font-lock-comment-face nil nil t t nil)
166 '(font-lock-string-face nil nil nil t nil)
167 '(font-lock-keyword-face nil nil t nil nil)
168 (list 'font-lock-function-name-face
169 (cdr (assq 'background-color (frame-parameters)))
170 (cdr (assq 'foreground-color (frame-parameters)))
171 t nil nil)
172 '(font-lock-variable-name-face nil nil t t nil)
173 '(font-lock-type-face nil nil t nil t)
174 '(font-lock-reference-face nil nil t nil t)))
175 ((memq font-lock-display-type '(grayscale greyscale
176 grayshade greyshade))
177 (list (list 'font-lock-comment-face
178 (if light-bg "DimGray" "Gray80") nil t t nil)
179 (list 'font-lock-string-face
180 (if light-bg "Gray50" "LightGray") nil nil t nil)
181 (list 'font-lock-keyword-face
182 (if light-bg "DimGray" "Gray90") nil t nil nil)
183 (list 'font-lock-function-name-face
184 (cdr (assq 'background-color (frame-parameters)))
185 (cdr (assq 'foreground-color (frame-parameters)))
186 t nil nil)
187 (list 'font-lock-variable-name-face
188 (if light-bg "DimGray" "Gray90") nil t t nil)
189 (list 'font-lock-type-face
190 (if light-bg "DimGray" "Gray80") nil t nil t)))
191 (light-bg ; light colour background
192 '((font-lock-comment-face "Firebrick")
193 (font-lock-string-face "RosyBrown")
194 (font-lock-keyword-face "Purple")
195 (font-lock-function-name-face "Blue")
196 (font-lock-variable-name-face "DarkGoldenrod")
197 (font-lock-type-face "DarkOliveGreen")
198 (font-lock-reference-face "CadetBlue")))
199 (t ; dark colour background
200 '((font-lock-comment-face "OrangeRed")
201 (font-lock-string-face "LightSalmon")
202 (font-lock-keyword-face "LightSteelBlue")
203 (font-lock-function-name-face "LightSkyBlue")
204 (font-lock-variable-name-face "LightGoldenrod")
205 (font-lock-type-face "PaleGreen")
206 (font-lock-reference-face "Aquamarine")))))
207 "A list of default attributes to use for face attributes. 177 "A list of default attributes to use for face attributes.
208 Each element of the list should be of the form 178 Each element of the list should be of the form
209 179
210 (FACE FOREGROUND BACKGROUND BOLD-P ITALIC-P UNDERLINE-P) 180 (FACE FOREGROUND BACKGROUND BOLD-P ITALIC-P UNDERLINE-P)
211 181
225 See variables `font-lock-display-type' and `font-lock-background-mode'. 195 See variables `font-lock-display-type' and `font-lock-background-mode'.
226 196
227 Resources can be used to over-ride these face attributes. For example, the 197 Resources can be used to over-ride these face attributes. For example, the
228 resource `Emacs.font-lock-comment-face.attributeUnderline' can be used to 198 resource `Emacs.font-lock-comment-face.attributeUnderline' can be used to
229 specify the UNDERLINE-P attribute for face `font-lock-comment-face'.") 199 specify the UNDERLINE-P attribute for face `font-lock-comment-face'.")
200
201 (defun font-lock-make-faces ()
202 "Make faces from `font-lock-face-attributes'.
203 A default list is used if this is nil.
204 See `font-lock-make-face' and `list-faces-display'."
205 ;; We don't need to `setq' any of these variables, but the user can see what
206 ;; is being used if we do.
207 (if (null font-lock-display-type)
208 (setq font-lock-display-type
209 (let ((display-resource (x-get-resource ".displayType"
210 "DisplayType")))
211 (cond (display-resource (intern (downcase display-resource)))
212 ((x-display-color-p) 'color)
213 ((x-display-grayscale-p) 'grayscale)
214 (t 'mono)))))
215 (if (null font-lock-background-mode)
216 (setq font-lock-background-mode
217 (let ((bg-resource (x-get-resource ".backgroundMode"
218 "BackgroundMode"))
219 (params (frame-parameters)))
220 (cond (bg-resource (intern (downcase bg-resource)))
221 ((or (string-equal "white"
222 (downcase (cdr (assq 'foreground-color params))))
223 (string-equal "black"
224 (downcase (cdr (assq 'background-color params)))))
225 'dark)
226 (t 'light)))))
227 (if (null font-lock-face-attributes)
228 (setq font-lock-face-attributes
229 (let ((light-bg (eq font-lock-background-mode 'light)))
230 (cond ((memq font-lock-display-type '(mono monochrome))
231 ;; Emacs 19.25's font-lock defaults:
232 ;;'((font-lock-comment-face nil nil nil t nil)
233 ;; (font-lock-string-face nil nil nil nil t)
234 ;; (font-lock-keyword-face nil nil t nil nil)
235 ;; (font-lock-function-name-face nil nil t t nil)
236 ;; (font-lock-type-face nil nil nil t nil))
237 (list '(font-lock-comment-face nil nil t t nil)
238 '(font-lock-string-face nil nil nil t nil)
239 '(font-lock-keyword-face nil nil t nil nil)
240 (list
241 'font-lock-function-name-face
242 (cdr (assq 'background-color (frame-parameters)))
243 (cdr (assq 'foreground-color (frame-parameters)))
244 t nil nil)
245 '(font-lock-variable-name-face nil nil t t nil)
246 '(font-lock-type-face nil nil t nil t)
247 '(font-lock-reference-face nil nil t nil t)))
248 ((memq font-lock-display-type '(grayscale greyscale
249 grayshade greyshade))
250 (list
251 (list 'font-lock-comment-face
252 (if light-bg "DimGray" "Gray80") nil t t nil)
253 (list 'font-lock-string-face
254 (if light-bg "Gray50" "LightGray") nil nil t nil)
255 (list 'font-lock-keyword-face
256 (if light-bg "DimGray" "Gray90") nil t nil nil)
257 (list 'font-lock-function-name-face
258 (cdr (assq 'background-color (frame-parameters)))
259 (cdr (assq 'foreground-color (frame-parameters)))
260 t nil nil)
261 (list 'font-lock-variable-name-face
262 (if light-bg "DimGray" "Gray90") nil t t nil)
263 (list 'font-lock-type-face
264 (if light-bg "DimGray" "Gray80") nil t nil t)))
265 (light-bg ; light colour background
266 '((font-lock-comment-face "Firebrick")
267 (font-lock-string-face "RosyBrown")
268 (font-lock-keyword-face "Purple")
269 (font-lock-function-name-face "Blue")
270 (font-lock-variable-name-face "DarkGoldenrod")
271 (font-lock-type-face "DarkOliveGreen")
272 (font-lock-reference-face "CadetBlue")))
273 (t ; dark colour background
274 '((font-lock-comment-face "OrangeRed")
275 (font-lock-string-face "LightSalmon")
276 (font-lock-keyword-face "LightSteelBlue")
277 (font-lock-function-name-face "LightSkyBlue")
278 (font-lock-variable-name-face "LightGoldenrod")
279 (font-lock-type-face "PaleGreen")
280 (font-lock-reference-face "Aquamarine")))))))
281 (mapcar 'font-lock-make-face font-lock-face-attributes))
230 282
231 (defun font-lock-make-face (face-attributes) 283 (defun font-lock-make-face (face-attributes)
232 "Make a face from FACE-ATTRIBUTES. 284 "Make a face from FACE-ATTRIBUTES.
233 FACE-ATTRIBUTES should be like an element `font-lock-face-attributes', so that 285 FACE-ATTRIBUTES should be like an element `font-lock-face-attributes', so that
234 the face name is the first item in the list. A variable with the same name as 286 the face name is the first item in the list. A variable with the same name as
288 (concat "\\s>\\|" (regexp-quote comment-end)) 340 (concat "\\s>\\|" (regexp-quote comment-end))
289 "\\s>")) 341 "\\s>"))
290 (startline (point)) 342 (startline (point))
291 state prev prevstate) 343 state prev prevstate)
292 ;; Find the state at the line-beginning before START. 344 ;; Find the state at the line-beginning before START.
293 (if (eq (point) font-lock-cache-position) 345 (if (eq startline font-lock-cache-position)
294 (setq state font-lock-cache-state) 346 (setq state font-lock-cache-state)
295 ;; Find outermost containing sexp. 347 ;; Find outermost containing sexp.
296 (beginning-of-defun) 348 (beginning-of-defun)
297 ;; Find the state at STARTLINE. 349 ;; Find the state at STARTLINE.
298 (while (< (point) startline) 350 (while (< (point) startline)
303 (setq state (parse-partial-sexp (point) start nil nil state)) 355 (setq state (parse-partial-sexp (point) start nil nil state))
304 ;; If the region starts inside a string, show the extent of it. 356 ;; If the region starts inside a string, show the extent of it.
305 (if (nth 3 state) 357 (if (nth 3 state)
306 (let ((beg (point))) 358 (let ((beg (point)))
307 (while (and (re-search-forward "\\s\"" end 'move) 359 (while (and (re-search-forward "\\s\"" end 'move)
308 (nth 3 (parse-partial-sexp beg (point) 360 (nth 3 (parse-partial-sexp beg (point) nil nil
309 nil nil state)))) 361 state))))
310 (put-text-property beg (point) 'face font-lock-string-face) 362 (put-text-property beg (point) 'face font-lock-string-face)
311 (setq state (parse-partial-sexp beg (point) nil nil state)))) 363 (setq state (parse-partial-sexp beg (point) nil nil state))))
312 ;; Likewise for a comment. 364 ;; Likewise for a comment.
313 (if (or (nth 4 state) (nth 7 state)) 365 (if (or (nth 4 state) (nth 7 state))
314 (let ((beg (point))) 366 (let ((beg (point)))
315 (while (and (re-search-forward cend end 'move) 367 (while (and (re-search-forward cend end 'move)
316 (nth 3 (parse-partial-sexp beg (point) 368 (nth 3 (parse-partial-sexp beg (point) nil nil
317 nil nil state)))) 369 state))))
318 (put-text-property beg (point) 'face font-lock-comment-face) 370 (put-text-property beg (point) 'face font-lock-comment-face)
319 (setq state (parse-partial-sexp beg (point) nil nil state)))) 371 (setq state (parse-partial-sexp beg (point) nil nil state))))
320 ;; Find each interesting place between here and END. 372 ;; Find each interesting place between here and END.
321 (while (and (< (point) end) 373 (while (and (< (point) end)
322 (setq prev (point) prevstate state) 374 (setq prev (point) prevstate state)
324 (progn 376 (progn
325 ;; Clear out the fonts of what we skip over. 377 ;; Clear out the fonts of what we skip over.
326 (remove-text-properties prev (point) '(face nil)) 378 (remove-text-properties prev (point) '(face nil))
327 ;; Verify the state at that place 379 ;; Verify the state at that place
328 ;; so we don't get fooled by \" or \;. 380 ;; so we don't get fooled by \" or \;.
329 (setq state (parse-partial-sexp prev (point) 381 (setq state (parse-partial-sexp prev (point) nil nil
330 nil nil state)))) 382 state))))
331 (let ((here (point))) 383 (let ((here (point)))
332 (if (or (nth 4 state) (nth 7 state)) 384 (if (or (nth 4 state) (nth 7 state))
333 ;; We found a real comment start. 385 ;; We found a real comment start.
334 (let ((beg (match-beginning 0))) 386 (let ((beg (match-beginning 0)))
335 (goto-char beg) 387 (goto-char beg)
345 (put-text-property beg (point) 'face font-lock-comment-face) 397 (put-text-property beg (point) 'face font-lock-comment-face)
346 (setq state (parse-partial-sexp here (point) nil nil state))) 398 (setq state (parse-partial-sexp here (point) nil nil state)))
347 (if (nth 3 state) 399 (if (nth 3 state)
348 (let ((beg (match-beginning 0))) 400 (let ((beg (match-beginning 0)))
349 (while (and (re-search-forward "\\s\"" end 'move) 401 (while (and (re-search-forward "\\s\"" end 'move)
350 (nth 3 (parse-partial-sexp here (point) 402 (nth 3 (parse-partial-sexp here (point) nil nil
351 nil nil state)))) 403 state))))
352 (put-text-property beg (point) 'face font-lock-string-face) 404 (put-text-property beg (point) 'face font-lock-string-face)
353 (setq state (parse-partial-sexp here (point) 405 (setq state (parse-partial-sexp here (point) nil nil
354 nil nil state)))))) 406 state))))))
355 ;; Make sure PREV is non-nil after the loop 407 ;; Make sure PREV is non-nil after the loop
356 ;; only if it was set on the very last iteration. 408 ;; only if it was set on the very last iteration.
357 (setq prev nil)) 409 (setq prev nil))
358 (and prev 410 (and prev
359 (remove-text-properties prev end '(face nil))) 411 (remove-text-properties prev end '(face nil)))
519 571
520 The default Font Lock mode faces and their attributes are defined in the 572 The default Font Lock mode faces and their attributes are defined in the
521 variable `font-lock-face-attributes', and Font Lock mode default settings in 573 variable `font-lock-face-attributes', and Font Lock mode default settings in
522 the variable `font-lock-defaults-alist'. 574 the variable `font-lock-defaults-alist'.
523 575
524 When you turn Font Lock mode on/off, the buffer is fontified/defontified. 576 When you turn Font Lock mode on/off the buffer is fontified/defontified, though
525 To fontify a buffer without having newly typed text become fontified, you 577 fontification occurs only if the buffer is less than `font-lock-maximum-size'.
526 can use \\[font-lock-fontify-buffer]." 578 To fontify a buffer without turning on Font Lock mode, and regardless of buffer
579 size, you can use \\[font-lock-fontify-buffer]."
527 (interactive "P") 580 (interactive "P")
528 (let ((on-p (if arg (> (prefix-numeric-value arg) 0) (not font-lock-mode)))) 581 (let ((on-p (if arg (> (prefix-numeric-value arg) 0) (not font-lock-mode))))
529 (if (equal (buffer-name) " *Compiler Input*") ; hack for bytecomp... 582 (if (equal (buffer-name) " *Compiler Input*") ; hack for bytecomp...
530 (setq on-p nil)) 583 (setq on-p nil))
531 (if (not on-p) 584 (if (not on-p)
539 (make-local-variable 'after-revert-hook) 592 (make-local-variable 'after-revert-hook)
540 ;; If buffer is reverted, must clean up the state. 593 ;; If buffer is reverted, must clean up the state.
541 (add-hook 'before-revert-hook 'font-lock-revert-setup) 594 (add-hook 'before-revert-hook 'font-lock-revert-setup)
542 (add-hook 'after-revert-hook 'font-lock-revert-cleanup) 595 (add-hook 'after-revert-hook 'font-lock-revert-cleanup)
543 (run-hooks 'font-lock-mode-hook) 596 (run-hooks 'font-lock-mode-hook)
544 (or font-lock-fontified (font-lock-fontify-buffer))) 597 (cond (font-lock-fontified
598 nil)
599 ((or (null font-lock-maximum-size)
600 (> font-lock-maximum-size (buffer-size)))
601 (font-lock-fontify-buffer))
602 (font-lock-verbose
603 (message "Fontifying %s... buffer too big." (buffer-name)))))
545 (font-lock-fontified 604 (font-lock-fontified
546 (setq font-lock-fontified nil) 605 (setq font-lock-fontified nil)
547 (remove-hook 'before-revert-hook 'font-lock-revert-setup) 606 (remove-hook 'before-revert-hook 'font-lock-revert-setup)
548 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup) 607 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup)
549 (font-lock-unfontify-region (point-min) (point-max)))) 608 (font-lock-unfontify-region (point-min) (point-max))))
571 "Fontify the current buffer the way `font-lock-mode' would." 630 "Fontify the current buffer the way `font-lock-mode' would."
572 (interactive) 631 (interactive)
573 (let ((was-on font-lock-mode) 632 (let ((was-on font-lock-mode)
574 (verbose (or font-lock-verbose (interactive-p))) 633 (verbose (or font-lock-verbose (interactive-p)))
575 (modified (buffer-modified-p))) 634 (modified (buffer-modified-p)))
576 (make-local-variable 'font-lock-fontified) 635 (set (make-local-variable 'font-lock-fontified) nil)
577 (if verbose (message "Fontifying %s..." (buffer-name))) 636 (if verbose (message "Fontifying %s..." (buffer-name)))
578 ;; Turn it on to run hooks and get the right font-lock-keywords. 637 ;; Turn it on to run hooks and get the right `font-lock-keywords' etc.
579 (or was-on (font-lock-set-defaults)) 638 (or was-on (font-lock-set-defaults))
580 (condition-case nil 639 (condition-case nil
581 (save-excursion 640 (save-excursion
582 (font-lock-unfontify-region (point-min) (point-max)) 641 (font-lock-unfontify-region (point-min) (point-max))
583 (if (not font-lock-no-comments) 642 (if (not font-lock-no-comments)
584 (font-lock-fontify-region (point-min) (point-max) verbose)) 643 (font-lock-fontify-region (point-min) (point-max) verbose))
585 (font-lock-hack-keywords (point-min) (point-max) verbose) 644 (font-lock-hack-keywords (point-min) (point-max) verbose)
586 (setq font-lock-fontified t)) 645 (setq font-lock-fontified t))
587 ;; We don't restore the old fontification, so it's best to unfontify. 646 ;; We don't restore the old fontification, so it's best to unfontify.
588 (quit (font-lock-unfontify-region (point-min) (point-max)) 647 (quit (font-lock-unfontify-region (point-min) (point-max))))
589 (setq font-lock-fontified nil)))
590 (if verbose (message "Fontifying %s... %s." (buffer-name) 648 (if verbose (message "Fontifying %s... %s." (buffer-name)
591 (if font-lock-fontified "done" "aborted"))) 649 (if font-lock-fontified "done" "aborted")))
592 (and (buffer-modified-p) 650 (and (buffer-modified-p)
593 (not modified) 651 (not modified)
594 (set-buffer-modified-p nil)))) 652 (set-buffer-modified-p nil))))
634 "save-\\(excursion\\|match-data\\|restriction\\|window-excursion\\)\\|" 692 "save-\\(excursion\\|match-data\\|restriction\\|window-excursion\\)\\|"
635 "t\\(hrow\\|rack-mouse\\)\\|unwind-protect\\|while" 693 "t\\(hrow\\|rack-mouse\\)\\|unwind-protect\\|while"
636 "\\)[ \t\n]") 1) 694 "\\)[ \t\n]") 1)
637 ;; CLisp: 695 ;; CLisp:
638 ; ("when" "unless" "do" "flet" "labels" "return" "return-from") 696 ; ("when" "unless" "do" "flet" "labels" "return" "return-from")
639 '("(\\(do\\|flet\\|labels\\|return\\(\\|-from\\)\\|unless\\|when\\)\\>" 697 '("(\\(do\\|flet\\|labels\\|return\\(\\|-from\\)\\|unless\\|when\\)[ \t\n]"
640 . 1) 698 . 1)
641 ;; 699 ;;
642 ;; Fontify CLisp keywords. 700 ;; Fontify CLisp keywords.
643 '("\\s :\\([-a-zA-Z0-9]+\\)\\>" . 1) 701 '("\\s :\\([-a-zA-Z0-9]+\\)\\>" . 1)
644 ;; 702 ;;
663 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1 721 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
664 "Additional expressions to highlight in Lisp modes.") 722 "Additional expressions to highlight in Lisp modes.")
665 723
666 724
667 (defconst c-font-lock-keywords-1 nil 725 (defconst c-font-lock-keywords-1 nil
668 "For consideration as a value of `c-font-lock-keywords'. 726 "For consideration as a value of `c-font-lock-keywords'.
669 This does fairly subdued highlighting.") 727 This does fairly subdued highlighting.")
670 728
671 (defconst c-font-lock-keywords-2 nil 729 (defconst c-font-lock-keywords-2 nil
672 "For consideration as a value of `c-font-lock-keywords'. 730 "For consideration as a value of `c-font-lock-keywords'.
673 This does a lot more highlighting.") 731 This does a lot more highlighting.")
674 732
675 (defconst c++-font-lock-keywords-1 nil 733 (defconst c++-font-lock-keywords-1 nil
676 "For consideration as a value of `c++-font-lock-keywords'. 734 "For consideration as a value of `c++-font-lock-keywords'.
677 This does fairly subdued highlighting.") 735 This does fairly subdued highlighting.")
678 736
679 (defconst c++-font-lock-keywords-2 nil 737 (defconst c++-font-lock-keywords-2 nil
680 "For consideration as a value of `c++-font-lock-keywords'. 738 "For consideration as a value of `c++-font-lock-keywords'.
681 This does a lot more highlighting.") 739 This does a lot more highlighting.")
682 740
683 (let ((type-types 741 (let ((c-keywords
742 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while")
743 "break\\|continue\\|do\\|else\\|for\\|if\\|return\\|switch\\|while")
744 (c-type-types
684 ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum" 745 ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
685 ; "signed" "unsigned" "short" "long" "int" "char" "float" "double" 746 ; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
686 ; "void") 747 ; "void" "volatile" "const")
687 (concat "auto\\|char\\|double\\|e\\(num\\|xtern\\)\\|float\\|int\\|" 748 (concat "auto\\|c\\(har\\|onst\\)\\|double\\|e\\(num\\|xtern\\)\\|"
688 "long\\|register\\|s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|" 749 "float\\|int\\|long\\|register\\|"
689 "typedef\\|un\\(ion\\|signed\\)\\|void")) ; 4 ()s deep. 750 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|typedef\\|"
690 (c++-types 751 "un\\(ion\\|signed\\)\\|vo\\(id\\|latile\\)")) ; 6 ()s deep.
691 ; ("const" "class" "protected" "private" "public" "inline" "bool" 752 (c++-keywords
692 ; "virtual") 753 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while"
693 (concat "bool\\|c\\(lass\\|onst\\)\\|inline\\|" 754 ; "asm" "catch" "delete" "new" "operator" "sizeof" "this" "throw" "try"
694 "p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|virtual")) 755 ; "protected" "private" "public")
756 (concat "asm\\|break\\|c\\(atch\\|ontinue\\)\\|d\\(elete\\|o\\)\\|"
757 "else\\|for\\|if\\|new\\|operator\\|"
758 "p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|return\\|"
759 "s\\(izeof\\|witch\\)\\|t\\(h\\(is\\|row\\)\\|ry\\)\\|while"))
760 (c++-type-types
761 ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
762 ; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
763 ; "void" "volatile" "const" "class" "inline" "friend" "bool"
764 ; "virtual" "complex" "template")
765 (concat "auto\\|bool\\|c\\(har\\|lass\\|o\\(mplex\\|nst\\)\\)\\|"
766 "double\\|e\\(num\\|xtern\\)\\|f\\(loat\\|riend\\)\\|"
767 "in\\(line\\|t\\)\\|long\\|register\\|"
768 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|"
769 "t\\(emplate\\|ypedef\\)\\|un\\(ion\\|signed\\)\\|"
770 "v\\(irtual\\|o\\(id\\|latile\\)\\)")) ; 11 ()s deep.
695 (ctoken "[a-zA-Z0-9_:~]+")) 771 (ctoken "[a-zA-Z0-9_:~]+"))
696 (setq c-font-lock-keywords-1 772 (setq c-font-lock-keywords-1
697 (list 773 (list
698 ;; 774 ;;
699 ;; Fontify filenames in #include <...> preprocessor directives. 775 ;; Fontify filenames in #include <...> preprocessor directives.
713 (setq c-font-lock-keywords-2 789 (setq c-font-lock-keywords-2
714 (append c-font-lock-keywords-1 790 (append c-font-lock-keywords-1
715 (list 791 (list
716 ;; 792 ;;
717 ;; Fontify all storage classes and type specifiers (before declarations). 793 ;; Fontify all storage classes and type specifiers (before declarations).
718 (cons (concat "\\<\\(" type-types "\\)\\>") 'font-lock-type-face) 794 (cons (concat "\\<\\(" c-type-types "\\)\\>") 'font-lock-type-face)
719 ;; 795 ;;
720 ;; Fontify variable/structure name declarations and definitions, or 796 ;; Fontify variable/structure name declarations and definitions, or
721 ;; function name declarations (plus definitions with type on same line). 797 ;; function name declarations (plus definitions with type on same line).
722 (list (concat "\\<\\(" type-types "\\)[ \t*&]+" 798 (list (concat "\\<\\(" c-type-types "\\)[ \t*]+"
723 "\\(" ctoken "[ \t*&]+\\)*" 799 "\\(" ctoken "[ \t*]+\\)*"
724 "\\(" ctoken "\\)[ \t]*\\((\\)?") 800 "\\(" ctoken "\\)[ \t]*\\((\\)?")
725 7 801 9
726 '(if (match-beginning 8) 802 '(if (match-beginning 10)
727 'font-lock-function-name-face 803 font-lock-function-name-face
728 'font-lock-variable-name-face)) 804 font-lock-variable-name-face))
729 ;; Is highlighting above using (6 font-lock-type-face nil t) a good idea? 805 ;;
806 ;; Fontify function/variable name declarations at the start of the line.
807 ;; (Not everyone follows the GNU convention of function name at the start.)
808 (list (concat "^" ctoken "[ \t*]+"
809 "\\(" ctoken "[ \t*]+\\)*"
810 "\\(" ctoken "\\)[ \t]*\\((\\)?")
811 2
812 '(if (match-beginning 3)
813 font-lock-function-name-face
814 font-lock-variable-name-face))
730 ;; 815 ;;
731 ;; Fontify variable names declared with structures, or typedef names. 816 ;; Fontify variable names declared with structures, or typedef names.
732 '("}[ \t]*\\(\\sw+\\)[ \t]*[;,[]" 1 font-lock-variable-name-face) 817 '("}[ \t*]*\\(\\sw+\\)[ \t]*[;,[]" 1 font-lock-variable-name-face)
733 ;; 818 ;;
734 ;; Fontify all builtin keywords (except case and goto; see below). 819 ;; Fontify all builtin keywords (except case, default and goto; see below).
735 (cons (concat 820 (concat "\\<\\(" c-keywords "\\)\\>")
736 ; ("for" "while" "do" "return" "goto" "case" "break" "switch" 821 ;;
737 ; "if" "else" "default" "continue" "default") 822 ;; Fontify case/goto keywords and targets, and goto tags (incl "default:").
738 "\\<\\(break\\|continue\\|d\\(efault\\|o\\)\\|else\\|"
739 "for\\|if\\|return\\|switch\\|while\\)\\>")
740 'font-lock-keyword-face)
741 ;;
742 ;; Fontify case/goto keywords and targets, and goto tags.
743 '("\\<\\(case\\|goto\\)\\>[ \t]*\\([^ \t\n:;]+\\)?" 823 '("\\<\\(case\\|goto\\)\\>[ \t]*\\([^ \t\n:;]+\\)?"
744 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t)) 824 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
745 '("^[ \t]*\\(\\sw+\\)[ \t]*:" 1 font-lock-reference-face) 825 '("^[ \t]*\\(\\sw+\\)[ \t]*:" 1 font-lock-reference-face)
746 ))) 826 )))
747 827
748 (setq c++-font-lock-keywords-1 c-font-lock-keywords-1) 828 (setq c++-font-lock-keywords-1 c-font-lock-keywords-1)
749 (setq c++-font-lock-keywords-2 829 (setq c++-font-lock-keywords-2
750 (append 830 (append c++-font-lock-keywords-1
751 (list 831 (list
752 ;; 832 ;; We don't just add to the C keywords for subtle differences and speed.
753 ;; Fontify C++ type specifiers (before case targets/goto tags). 833 ;; See the above comments for `c-font-lock-keywords-2'.
754 (cons (concat "\\<\\(" c++-types "\\)\\>") 'font-lock-type-face) 834 (cons (concat "\\<\\(" c++-type-types "\\)\\>") 'font-lock-type-face)
755 ;; 835 (list (concat "\\<\\(" c++-type-types "\\)[ \t*&]+"
756 ;; Fontify C++ builtin keywords. 836 "\\(" ctoken "[ \t*&]+\\)*"
757 '("\\<\\(delete\\|new\\)\\>" . font-lock-keyword-face)) 837 "\\(" ctoken "\\)[ \t]*\\((\\)?")
758 c-font-lock-keywords-2)) 838 14
839 '(if (match-beginning 15)
840 font-lock-function-name-face
841 font-lock-variable-name-face))
842 (list (concat "^" ctoken "[ \t*]+"
843 "\\(" ctoken "[ \t*]+\\)*"
844 "\\(" ctoken "\\)[ \t]*\\((\\)?")
845 2
846 '(if (match-beginning 3)
847 font-lock-function-name-face
848 font-lock-variable-name-face))
849 '("}[ \t*]*\\(\\sw+\\)[ \t]*[;,[]" 1 font-lock-variable-name-face)
850 (concat "\\<\\(" c++-keywords "\\)\\>")
851 '("\\<\\(case\\|goto\\)\\>[ \t]*\\([^ \t\n:;]+\\)?"
852 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
853 '("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-reference-face))))
759 ) 854 )
760 855
761 ; default to the gaudier variety? 856 ; default to the gaudier variety?
762 (defvar c-font-lock-keywords c-font-lock-keywords-1 857 (defvar c-font-lock-keywords c-font-lock-keywords-1
763 "Additional expressions to highlight in C mode.") 858 "Additional expressions to highlight in C mode.")
776 '("[^\\\\]\\$\\([^$]*\\)\\$" 1 font-lock-string-face t) 871 '("[^\\\\]\\$\\([^$]*\\)\\$" 1 font-lock-string-face t)
777 ; '("\\$\\([^$]*\\)\\$" 1 font-lock-string-face t) 872 ; '("\\$\\([^$]*\\)\\$" 1 font-lock-string-face t)
778 ) 873 )
779 "Additional expressions to highlight in TeX mode.") 874 "Additional expressions to highlight in TeX mode.")
780 875
781 ;; There is no html-mode.el shipped with Emacs... Yet. 876 ;; There is no html-mode.el shipped with Emacs; `font-lock-defaults' entry
782 ;(defvar html-font-lock-keywords 877 ; would be: (html-font-lock-keywords nil t)
878 ;(defconst html-font-lock-keywords
783 ; '(("<!--[^>]*>" 0 font-lock-comment-face t) ; Comment. 879 ; '(("<!--[^>]*>" 0 font-lock-comment-face t) ; Comment.
784 ; ("</?\\sw+" . font-lock-type-face) ; Normal tag start. 880 ; ("</?\\sw+" . font-lock-type-face) ; Normal tag start.
785 ; (">" . font-lock-type-face) ; Normal tag end. 881 ; (">" . font-lock-type-face) ; Normal tag end.
786 ; ("<\\(/?\\(a\\|form\\|img\\|input\\)\\)\\>" ; Special tag name. 882 ; ("<\\(/?\\(a\\|form\\|img\\|input\\)\\)\\>" ; Special tag name.
787 ; 1 font-lock-function-name-face t) 883 ; 1 font-lock-function-name-face t)
788 ; ("\\<\\(\\sw+\\)[>=]" 1 font-lock-keyword-face)) ; Tag attribute. 884 ; ("\\<\\(\\sw+\\)[>=]" 1 font-lock-keyword-face)) ; Tag attribute.
789 ; "Additional expressions to highlight in HTML mode.") 885 ; "Additional expressions to highlight in HTML mode.")
790 886
791 (defvar font-lock-defaults-alist
792 '((bibtex-mode . (tex-font-lock-keywords))
793 (c++-c-mode . (c-font-lock-keywords))
794 (c++-mode . (c++-font-lock-keywords))
795 (c-mode . (c-font-lock-keywords))
796 (emacs-lisp-mode . (lisp-font-lock-keywords))
797 (html-mode . (html-font-lock-keywords))
798 (latex-mode . (tex-font-lock-keywords))
799 (lisp-mode . (lisp-font-lock-keywords))
800 (plain-tex-mode . (tex-font-lock-keywords))
801 (slitex-mode . (tex-font-lock-keywords))
802 (tex-mode . (tex-font-lock-keywords)))
803 "*Alist of major mode and Font Lock defaults.
804 Each item should be a cons pair of the form:
805 (MAJOR-MODE . (FONT-LOCK-KEYWORDS NOT-SYNTACTICALLY CASE-FOLD)
806 where both MAJOR-MODE and FONT-LOCK-KEYWORDS are symbols. If NOT-SYNTACTICALLY
807 is non-nil, syntactic fontification (strings and comments) is not performed.
808 If CASE-FOLD is non-nil, the case of the keywords is ignored when fontifying.")
809
810 (defvar font-lock-defaults nil
811 "If set by a major mode, this specifies the defaults for Font Lock mode.")
812
813 (defun font-lock-set-defaults () 887 (defun font-lock-set-defaults ()
814 "Set fontification defaults appropriately for this mode. 888 "Set fontification defaults appropriately for this mode.
815 Sets `font-lock-keywords', `font-lock-keywords-case-fold-search' and 889 Sets `font-lock-keywords', `font-lock-no-comments', `font-lock-syntax-table'
816 `font-lock-no-comments' using `font-lock-defaults-alist'. 890 and `font-lock-keywords-case-fold-search' using `font-lock-defaults-alist'."
817 Also sets `font-lock-syntax-table' for C and C++ modes." 891 (or font-lock-keywords ; if not already set.
818 ;; If font-lock-keywords is already set, assume the major mode
819 ;; has done exactly what it wants.
820 (or font-lock-keywords
821 (let ((defaults (or font-lock-defaults 892 (let ((defaults (or font-lock-defaults
822 (cdr (assq major-mode font-lock-defaults-alist))))) 893 (cdr (assq major-mode font-lock-defaults-alist)))))
823 ;; Keywords? 894 ;; Keywords?
824 (setq font-lock-keywords (eval (nth 0 defaults))) 895 (setq font-lock-keywords (eval (nth 0 defaults)))
825 ;; Syntactic? 896 ;; Syntactic?
827 (set (make-local-variable 'font-lock-no-comments) t)) 898 (set (make-local-variable 'font-lock-no-comments) t))
828 ;; Case fold? 899 ;; Case fold?
829 (if (nth 2 defaults) 900 (if (nth 2 defaults)
830 (set (make-local-variable 'font-lock-keywords-case-fold-search) t)) 901 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
831 ;; Syntax table? 902 ;; Syntax table?
832 (cond ((eq major-mode 'c-mode) 903 (if (nth 3 defaults)
833 (make-local-variable 'font-lock-syntax-table) 904 (let ((slist (nth 3 defaults)))
834 (setq font-lock-syntax-table (copy-syntax-table (syntax-table))) 905 (make-local-variable 'font-lock-syntax-table)
835 (modify-syntax-entry ?_ "w" font-lock-syntax-table)) 906 (setq font-lock-syntax-table (copy-syntax-table (syntax-table)))
836 ((eq major-mode 'c++-c-mode) 907 (while slist
837 (make-local-variable 'font-lock-syntax-table) 908 (modify-syntax-entry (car (car slist)) (cdr (car slist))
838 (setq font-lock-syntax-table (copy-syntax-table (syntax-table))) 909 font-lock-syntax-table)
839 (modify-syntax-entry ?_ "w" font-lock-syntax-table)))))) 910 (setq slist (cdr slist))))))))
840 911
841 ;; Install ourselves: 912 ;; Install ourselves:
842 913
843 (mapcar 'font-lock-make-face font-lock-face-attributes) 914 (if noninteractive
915 (add-hook 'after-init-hook 'font-lock-make-faces)
916 (font-lock-make-faces))
844 917
845 (or (assq 'font-lock-mode minor-mode-alist) 918 (or (assq 'font-lock-mode minor-mode-alist)
846 (setq minor-mode-alist (cons '(font-lock-mode " Font") minor-mode-alist))) 919 (setq minor-mode-alist (cons '(font-lock-mode " Font") minor-mode-alist)))
847 920
848 ;; Provide ourselves: 921 ;; Provide ourselves: