1. If PRE-MATCH-FORM returns a suitable number, use that as a value for LIMIT.
[emacs.git] / lisp / font-lock.el
blob380d41261ae7d7c0d64dcfa9b50bc963fe9219b9
1 ;;; font-lock.el --- Electric font lock mode
3 ;; Copyright (C) 1992, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
5 ;; Author: jwz, then rms, then sm <simon@gnu.ai.mit.edu>
6 ;; Maintainer: FSF
7 ;; Keywords: languages, faces
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;; Font Lock mode is a minor mode that causes your comments to be displayed in
29 ;; one face, strings in another, reserved words in another, and so on.
31 ;; Comments will be displayed in `font-lock-comment-face'.
32 ;; Strings will be displayed in `font-lock-string-face'.
33 ;; Regexps are used to display selected patterns in other faces.
35 ;; To make the text you type be fontified, use M-x font-lock-mode RET.
36 ;; When this minor mode is on, the faces of the current line are updated with
37 ;; every insertion or deletion.
39 ;; To turn Font Lock mode on automatically, add this to your ~/.emacs file:
41 ;; (add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock)
43 ;; Or if you want to turn Font Lock mode on in many modes:
45 ;; (global-font-lock-mode t)
47 ;; Fontification for a particular mode may be available in a number of levels
48 ;; of decoration. The higher the level, the more decoration, but the more time
49 ;; it takes to fontify. See the variable `font-lock-maximum-decoration', and
50 ;; also the variable `font-lock-maximum-size'. Support modes for Font Lock
51 ;; mode can be used to speed up Font Lock mode. See `font-lock-support-mode'.
53 ;;; How Font Lock mode fontifies:
55 ;; When Font Lock mode is turned on in a buffer, it (a) fontifies the entire
56 ;; buffer and (b) installs one of its fontification functions on one of the
57 ;; hook variables that are run by Emacs after every buffer change (i.e., an
58 ;; insertion or deletion). Fontification means the replacement of `face' text
59 ;; properties in a given region; Emacs displays text with these `face' text
60 ;; properties appropriately.
62 ;; Fontification normally involves syntactic (i.e., strings and comments) and
63 ;; regexp (i.e., keywords and everything else) passes. The syntactic pass
64 ;; involves a syntax table and a syntax parsing function to determine the
65 ;; context of different parts of a region of text. It is necessary because
66 ;; generally strings and/or comments can span lines, and so the context of a
67 ;; given region is not necessarily apparent from the content of that region.
68 ;; Because the regexp pass only works within a given region, it is not
69 ;; generally appropriate for syntactic fontification. The regexp pass involves
70 ;; searching for given regexps (or calling given functions) within the given
71 ;; region. For each match of the regexp (or non-nil value of the called
72 ;; function), `face' text properties are added appropriately.
74 ;;; How Font Lock mode supports modes or is supported by modes:
76 ;; Modes that support Font Lock mode do so by defining one or more variables
77 ;; whose values specify the fontification. Font Lock mode knows of these
78 ;; variable names from (a) the buffer local variable `font-lock-defaults', if
79 ;; non-nil, or (b) the global variable `font-lock-defaults-alist', if the major
80 ;; mode has an entry. (Font Lock mode is set up via (a) where a mode's
81 ;; patterns are distributed with the mode's package library, and (b) where a
82 ;; mode's patterns are distributed with font-lock.el itself. An example of (a)
83 ;; is Pascal mode, an example of (b) is Lisp mode. Normally, the mechanism is
84 ;; (a); (b) is used where it is not clear which package library should contain
85 ;; the pattern definitions.) Font Lock mode chooses which variable to use for
86 ;; fontification based on `font-lock-maximum-decoration'.
88 ;; Font Lock mode fontification behaviour can be modified in a number of ways.
89 ;; See the below comments and the comments distributed throughout this file.
91 ;;; Constructing patterns:
93 ;; See the documentation for the variable `font-lock-keywords'.
95 ;; Nasty regexps of the form "bar\\(\\|lo\\)\\|f\\(oo\\|u\\(\\|bar\\)\\)\\|lo"
96 ;; are made thusly: (make-regexp '("foo" "fu" "fubar" "bar" "barlo" "lo")) for
97 ;; efficiency. See /pub/gnu/emacs/elisp-archive/functions/make-regexp.el.Z on
98 ;; archive.cis.ohio-state.edu for this and other functions not just by sm.
100 ;;; Adding patterns for modes that already support Font Lock:
102 ;; Though Font Lock highlighting patterns already exist for many modes, it's
103 ;; likely there's something that you want fontified that currently isn't, even
104 ;; at the maximum fontification level. You can add highlighting patterns via
105 ;; `font-lock-add-keywords'. For example, say in some C
106 ;; header file you #define the token `and' to expand to `&&', etc., to make
107 ;; your C code almost readable. In your ~/.emacs there could be:
109 ;; (font-lock-add-keywords 'c-mode '("\\<\\(and\\|or\\|not\\)\\>"))
111 ;; Some modes provide specific ways to modify patterns based on the values of
112 ;; other variables. For example, additional C types can be specified via the
113 ;; variable `c-font-lock-extra-types'.
115 ;;; Adding patterns for modes that do not support Font Lock:
117 ;; Not all modes support Font Lock mode. If you (as a user of the mode) add
118 ;; patterns for a new mode, you must define in your ~/.emacs a variable or
119 ;; variables that specify regexp fontification. Then, you should indicate to
120 ;; Font Lock mode, via the mode hook setting `font-lock-defaults', exactly what
121 ;; support is required. For example, say Foo mode should have the following
122 ;; regexps fontified case-sensitively, and comments and strings should not be
123 ;; fontified automagically. In your ~/.emacs there could be:
125 ;; (defvar foo-font-lock-keywords
126 ;; '(("\\<\\(one\\|two\\|three\\)\\>" . font-lock-keyword-face)
127 ;; ("\\<\\(four\\|five\\|six\\)\\>" . font-lock-type-face))
128 ;; "Default expressions to highlight in Foo mode.")
130 ;; (add-hook 'foo-mode-hook
131 ;; (function (lambda ()
132 ;; (make-local-variable 'font-lock-defaults)
133 ;; (setq font-lock-defaults '(foo-font-lock-keywords t)))))
135 ;;; Adding Font Lock support for modes:
137 ;; Of course, it would be better that the mode already supports Font Lock mode.
138 ;; The package author would do something similar to above. The mode must
139 ;; define at the top-level a variable or variables that specify regexp
140 ;; fontification. Then, the mode command should indicate to Font Lock mode,
141 ;; via `font-lock-defaults', exactly what support is required. For example,
142 ;; say Bar mode should have the following regexps fontified case-insensitively,
143 ;; and comments and strings should be fontified automagically. In bar.el there
144 ;; could be:
146 ;; (defvar bar-font-lock-keywords
147 ;; '(("\\<\\(uno\\|due\\|tre\\)\\>" . font-lock-keyword-face)
148 ;; ("\\<\\(quattro\\|cinque\\|sei\\)\\>" . font-lock-type-face))
149 ;; "Default expressions to highlight in Bar mode.")
151 ;; and within `bar-mode' there could be:
153 ;; (make-local-variable 'font-lock-defaults)
154 ;; (setq font-lock-defaults '(bar-font-lock-keywords nil t))
156 ;; What is fontification for? You might say, "It's to make my code look nice."
157 ;; I think it should be for adding information in the form of cues. These cues
158 ;; should provide you with enough information to both (a) distinguish between
159 ;; different items, and (b) identify the item meanings, without having to read
160 ;; the items and think about it. Therefore, fontification allows you to think
161 ;; less about, say, the structure of code, and more about, say, why the code
162 ;; doesn't work. Or maybe it allows you to think less and drift off to sleep.
164 ;; So, here are my opinions/advice/guidelines:
166 ;; - Highlight conceptual objects, such as function and variable names, and
167 ;; different objects types differently, i.e., (a) and (b) above, highlight
168 ;; function names differently to variable names.
169 ;; - Keep the faces distinct from each other as far as possible.
170 ;; i.e., (a) above.
171 ;; - Use the same face for the same conceptual object, across all modes.
172 ;; i.e., (b) above, all modes that have items that can be thought of as, say,
173 ;; keywords, should be highlighted with the same face, etc.
174 ;; - Make the face attributes fit the concept as far as possible.
175 ;; i.e., function names might be a bold colour such as blue, comments might
176 ;; be a bright colour such as red, character strings might be brown, because,
177 ;; err, strings are brown (that was not the reason, please believe me).
178 ;; - Don't use a non-nil OVERRIDE unless you have a good reason.
179 ;; Only use OVERRIDE for special things that are easy to define, such as the
180 ;; way `...' quotes are treated in strings and comments in Emacs Lisp mode.
181 ;; Don't use it to, say, highlight keywords in commented out code or strings.
182 ;; - Err, that's it.
184 ;;; Code:
186 ;; User variables.
188 (defvar font-lock-verbose (* 0 1024)
189 "*If non-nil, means show status messages for buffer fontification.
190 If a number, only buffers greater than this size have fontification messages.")
192 ;;;###autoload
193 (defvar font-lock-maximum-decoration nil
194 "*Maximum decoration level for fontification.
195 If nil, use the default decoration (typically the minimum available).
196 If t, use the maximum decoration available.
197 If a number, use that level of decoration (or if not available the maximum).
198 If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
199 where MAJOR-MODE is a symbol or t (meaning the default). For example:
200 ((c-mode . t) (c++-mode . 2) (t . 1))
201 means use the maximum decoration available for buffers in C mode, level 2
202 decoration for buffers in C++ mode, and level 1 decoration otherwise.")
204 ;;;###autoload
205 (defvar font-lock-maximum-size (* 250 1024)
206 "*Maximum size of a buffer for buffer fontification.
207 Only buffers less than this can be fontified when Font Lock mode is turned on.
208 If nil, means size is irrelevant.
209 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
210 where MAJOR-MODE is a symbol or t (meaning the default). For example:
211 ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576))
212 means that the maximum size is 250K for buffers in C or C++ modes, one megabyte
213 for buffers in Rmail mode, and size is irrelevant otherwise.")
215 ;; Fontification variables:
217 ;; Originally these variable values were face names such as `bold' etc.
218 ;; Now we create our own faces, but we keep these variables for compatibility
219 ;; and they give users another mechanism for changing face appearance.
220 ;; We now allow a FACENAME in `font-lock-keywords' to be any expression that
221 ;; returns a face. So the easiest thing is to continue using these variables,
222 ;; rather than sometimes evaling FACENAME and sometimes not. sm.
223 (defvar font-lock-comment-face 'font-lock-comment-face
224 "Face to use for comments.")
226 (defvar font-lock-string-face 'font-lock-string-face
227 "Face to use for strings.")
229 (defvar font-lock-keyword-face 'font-lock-keyword-face
230 "Face to use for keywords.")
232 (defvar font-lock-builtin-face 'font-lock-builtin-face
233 "Face to use for builtins.")
235 (defvar font-lock-function-name-face 'font-lock-function-name-face
236 "Face to use for function names.")
238 (defvar font-lock-variable-name-face 'font-lock-variable-name-face
239 "Face to use for variable names.")
241 (defvar font-lock-type-face 'font-lock-type-face
242 "Face to use for type names.")
244 (defvar font-lock-reference-face 'font-lock-reference-face
245 "Face to use for reference names.")
247 (defvar font-lock-warning-face 'font-lock-warning-face
248 "Face to use for things that should stand out.")
250 (defvar font-lock-keywords nil
251 "*A list of the keywords to highlight.
252 Each element should be of the form:
254 MATCHER
255 (MATCHER . MATCH)
256 (MATCHER . FACENAME)
257 (MATCHER . HIGHLIGHT)
258 (MATCHER HIGHLIGHT ...)
259 (eval . FORM)
261 where HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
263 FORM is an expression, whose value should be a keyword element, evaluated when
264 the keyword is (first) used in a buffer. This feature can be used to provide a
265 keyword that can only be generated when Font Lock mode is actually turned on.
267 For highlighting single items, typically only MATCH-HIGHLIGHT is required.
268 However, if an item or (typically) items are to be highlighted following the
269 instance of another item (the anchor) then MATCH-ANCHORED may be required.
271 MATCH-HIGHLIGHT should be of the form:
273 (MATCH FACENAME OVERRIDE LAXMATCH)
275 Where MATCHER can be either the regexp to search for, or the function name to
276 call to make the search (called with one argument, the limit of the search).
277 MATCH is the subexpression of MATCHER to be highlighted. FACENAME is an
278 expression whose value is the face name to use. FACENAME's default attributes
279 may be defined in `font-lock-face-attributes'.
281 OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification may
282 be overwritten. If `keep', only parts not already fontified are highlighted.
283 If `prepend' or `append', existing fontification is merged with the new, in
284 which the new or existing fontification, respectively, takes precedence.
285 If LAXMATCH is non-nil, no error is signaled if there is no MATCH in MATCHER.
287 For example, an element of the form highlights (if not already highlighted):
289 \"\\\\\\=<foo\\\\\\=>\" Discrete occurrences of \"foo\" in the value of the
290 variable `font-lock-keyword-face'.
291 (\"fu\\\\(bar\\\\)\" . 1) Substring \"bar\" within all occurrences of \"fubar\" in
292 the value of `font-lock-keyword-face'.
293 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'.
294 (\"foo\\\\|bar\" 0 foo-bar-face t)
295 Occurrences of either \"foo\" or \"bar\" in the value
296 of `foo-bar-face', even if already highlighted.
298 MATCH-ANCHORED should be of the form:
300 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
302 Where MATCHER is as for MATCH-HIGHLIGHT with one exception; see below.
303 PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
304 the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be
305 used to initialise before, and cleanup after, MATCHER is used. Typically,
306 PRE-MATCH-FORM is used to move to some position relative to the original
307 MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might
308 be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER.
310 For example, an element of the form highlights (if not already highlighted):
312 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
314 Discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
315 discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
316 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is
317 initially searched for starting from the end of the match of \"anchor\", and
318 searching for subsequent instance of \"anchor\" resumes from where searching
319 for \"item\" concluded.)
321 The above-mentioned exception is as follows. The limit of the MATCHER search
322 defaults to the end of the line after PRE-MATCH-FORM is evaluated.
323 However, if PRE-MATCH-FORM returns a position greater than the position after
324 PRE-MATCH-FORM is evaluated, that position is used as the limit of the search.
325 It is generally a bad idea to return a position greater than the end of the
326 line, i.e., cause the MATCHER search to span lines.
328 Note that the MATCH-ANCHORED feature is experimental; in the future, we may
329 replace it with other ways of providing this functionality.
331 These regular expressions should not match text which spans lines. While
332 \\[font-lock-fontify-buffer] handles multi-line patterns correctly, updating
333 when you edit the buffer does not, since it considers text one line at a time.
335 Be very careful composing regexps for this list;
336 the wrong pattern can dramatically slow things down!")
337 (make-variable-buffer-local 'font-lock-keywords)
339 ;; This variable is used by mode packages that support Font Lock mode by
340 ;; defining their own keywords to use for `font-lock-keywords'. (The mode
341 ;; command should make it buffer-local and set it to provide the set up.)
342 (defvar font-lock-defaults nil
343 "If set by a major mode, should be the defaults for Font Lock mode.
344 The value should be like the `cdr' of an item in `font-lock-defaults-alist'.")
346 ;; This variable is used where font-lock.el itself supplies the keywords.
347 (defvar font-lock-defaults-alist
348 (let (;; We use `beginning-of-defun', rather than nil, for SYNTAX-BEGIN.
349 ;; Thus the calculation of the cache is usually faster but not
350 ;; infallible, so we risk mis-fontification. sm.
351 (c-mode-defaults
352 '((c-font-lock-keywords c-font-lock-keywords-1
353 c-font-lock-keywords-2 c-font-lock-keywords-3)
354 nil nil ((?_ . "w")) beginning-of-defun
355 (font-lock-comment-start-regexp . "/[*/]")
356 (font-lock-mark-block-function . mark-defun)))
357 (c++-mode-defaults
358 '((c++-font-lock-keywords c++-font-lock-keywords-1
359 c++-font-lock-keywords-2 c++-font-lock-keywords-3)
360 nil nil ((?_ . "w") (?~ . "w")) beginning-of-defun
361 (font-lock-comment-start-regexp . "/[*/]")
362 (font-lock-mark-block-function . mark-defun)))
363 (objc-mode-defaults
364 '((objc-font-lock-keywords objc-font-lock-keywords-1
365 objc-font-lock-keywords-2 objc-font-lock-keywords-3)
366 nil nil ((?_ . "w") (?$ . "w")) nil
367 (font-lock-comment-start-regexp . "/[*/]")
368 (font-lock-mark-block-function . mark-defun)))
369 (java-mode-defaults
370 '((java-font-lock-keywords java-font-lock-keywords-1
371 java-font-lock-keywords-2 java-font-lock-keywords-3)
372 nil nil ((?_ . "w") (?$ . "w") (?. . "w")) nil
373 (font-lock-comment-start-regexp . "/[*/]")
374 (font-lock-mark-block-function . mark-defun)))
375 (lisp-mode-defaults
376 '((lisp-font-lock-keywords
377 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
378 nil nil (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
379 (font-lock-comment-start-regexp . ";")
380 (font-lock-mark-block-function . mark-defun)))
381 (scheme-mode-defaults
382 '(scheme-font-lock-keywords
383 nil t (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
384 (font-lock-comment-start-regexp . ";")
385 (font-lock-mark-block-function . mark-defun)))
386 ;; For TeX modes we could use `backward-paragraph' for the same reason.
387 ;; But we don't, because paragraph breaks are arguably likely enough to
388 ;; occur within a genuine syntactic block to make it too risky.
389 ;; However, we do specify a MARK-BLOCK function as that cannot result
390 ;; in a mis-fontification even if it might not fontify enough. --sm.
391 (tex-mode-defaults
392 '(tex-font-lock-keywords nil nil ((?$ . "\"")) nil
393 (font-lock-comment-start-regexp . "%")
394 (font-lock-mark-block-function . mark-paragraph)))
396 (list
397 (cons 'c-mode c-mode-defaults)
398 (cons 'c++-mode c++-mode-defaults)
399 (cons 'objc-mode objc-mode-defaults)
400 (cons 'java-mode java-mode-defaults)
401 (cons 'emacs-lisp-mode lisp-mode-defaults)
402 (cons 'inferior-scheme-mode scheme-mode-defaults)
403 (cons 'latex-mode tex-mode-defaults)
404 (cons 'lisp-mode lisp-mode-defaults)
405 (cons 'lisp-interaction-mode lisp-mode-defaults)
406 (cons 'plain-tex-mode tex-mode-defaults)
407 (cons 'scheme-mode scheme-mode-defaults)
408 (cons 'scheme-interaction-mode scheme-mode-defaults)
409 (cons 'slitex-mode tex-mode-defaults)
410 (cons 'tex-mode tex-mode-defaults)))
411 "Alist of default major mode and Font Lock defaults.
412 Each item should be a list of the form:
414 (MAJOR-MODE . (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN
415 ...))
417 where MAJOR-MODE is a symbol. KEYWORDS may be a symbol (a variable or function
418 whose value is the keywords to use for fontification) or a list of symbols.
419 If KEYWORDS-ONLY is non-nil, syntactic fontification (strings and comments) is
420 not performed. If CASE-FOLD is non-nil, the case of the keywords is ignored
421 when fontifying. If SYNTAX-ALIST is non-nil, it should be a list of cons pairs
422 of the form (CHAR-OR-STRING . STRING) used to set the local Font Lock syntax
423 table, for keyword and syntactic fontification (see `modify-syntax-entry').
425 If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
426 backwards outside any enclosing syntactic block, for syntactic fontification.
427 Typical values are `beginning-of-line' (i.e., the start of the line is known to
428 be outside a syntactic block), or `beginning-of-defun' for programming modes or
429 `backward-paragraph' for textual modes (i.e., the mode-dependent function is
430 known to move outside a syntactic block). If nil, the beginning of the buffer
431 is used as a position outside of a syntactic block, in the worst case.
433 These item elements are used by Font Lock mode to set the variables
434 `font-lock-keywords', `font-lock-keywords-only',
435 `font-lock-keywords-case-fold-search', `font-lock-syntax-table' and
436 `font-lock-beginning-of-syntax-function', respectively.
438 Further item elements are alists of the form (VARIABLE . VALUE) and are in no
439 particular order. Each VARIABLE is made buffer-local before set to VALUE.
441 Currently, appropriate variables include `font-lock-mark-block-function'.
442 If this is non-nil, it should be a function with no args used to mark any
443 enclosing block of text, for fontification via \\[font-lock-fontify-block].
444 Typical values are `mark-defun' for programming modes or `mark-paragraph' for
445 textual modes (i.e., the mode-dependent function is known to put point and mark
446 around a text block relevant to that mode).
448 Other variables include those for buffer-specialised fontification functions,
449 `font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function',
450 `font-lock-fontify-region-function', `font-lock-unfontify-region-function',
451 `font-lock-comment-start-regexp', `font-lock-inhibit-thing-lock' and
452 `font-lock-maximum-size'.")
454 (defvar font-lock-keywords-alist nil
455 "*Alist of `font-lock-keywords' local to a `major-mode'.
456 This is normally set via `font-lock-add-keywords'.")
458 (defvar font-lock-keywords-only nil
459 "*Non-nil means Font Lock should not fontify comments or strings.
460 This is normally set via `font-lock-defaults'.")
462 (defvar font-lock-keywords-case-fold-search nil
463 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
464 This is normally set via `font-lock-defaults'.")
466 (defvar font-lock-syntax-table nil
467 "Non-nil means use this syntax table for fontifying.
468 If this is nil, the major mode's syntax table is used.
469 This is normally set via `font-lock-defaults'.")
471 ;; If this is nil, we only use the beginning of the buffer if we can't use
472 ;; `font-lock-cache-position' and `font-lock-cache-state'.
473 (defvar font-lock-beginning-of-syntax-function nil
474 "*Non-nil means use this function to move back outside of a syntactic block.
475 When called with no args it should leave point at the beginning of any
476 enclosing syntactic block.
477 If this is nil, the beginning of the buffer is used (in the worst case).
478 This is normally set via `font-lock-defaults'.")
480 (defvar font-lock-mark-block-function nil
481 "*Non-nil means use this function to mark a block of text.
482 When called with no args it should leave point at the beginning of any
483 enclosing textual block and mark at the end.
484 This is normally set via `font-lock-defaults'.")
486 (defvar font-lock-comment-start-regexp nil
487 "*Regexp to match the start of a comment.
488 This need not discriminate between genuine comments and quoted comment
489 characters or comment characters within strings.
490 If nil, `comment-start-skip' is used instead; see that variable for more info.
491 This is normally set via `font-lock-defaults'.")
493 (defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
494 "Function to use for fontifying the buffer.
495 This is normally set via `font-lock-defaults'.")
497 (defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
498 "Function to use for unfontifying the buffer.
499 This is used when turning off Font Lock mode.
500 This is normally set via `font-lock-defaults'.")
502 (defvar font-lock-fontify-region-function 'font-lock-default-fontify-region
503 "Function to use for fontifying a region.
504 It should take two args, the beginning and end of the region, and an optional
505 third arg VERBOSE. If non-nil, the function should print status messages.
506 This is normally set via `font-lock-defaults'.")
508 (defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
509 "Function to use for unfontifying a region.
510 It should take two args, the beginning and end of the region.
511 This is normally set via `font-lock-defaults'.")
513 (defvar font-lock-inhibit-thing-lock nil
514 "List of Font Lock mode related modes that should not be turned on.
515 Currently, valid mode names as `fast-lock-mode' and `lazy-lock-mode'.
516 This is normally set via `font-lock-defaults'.")
518 (defvar font-lock-mode nil) ; Whether we are turned on/modeline.
519 (defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
521 ;;;###autoload
522 (defvar font-lock-mode-hook nil
523 "Function or functions to run on entry to Font Lock mode.")
525 ;; Font Lock mode.
527 (eval-when-compile
529 ;; We don't do this at the top-level as we only use non-autoloaded macros.
530 (require 'cl)
532 ;; Borrowed from lazy-lock.el.
533 ;; We use this to preserve or protect things when modifying text properties.
534 (defmacro save-buffer-state (varlist &rest body)
535 "Bind variables according to VARLIST and eval BODY restoring buffer state."
536 (` (let* ((,@ (append varlist
537 '((modified (buffer-modified-p))
538 (inhibit-read-only t) (buffer-undo-list t)
539 before-change-functions after-change-functions
540 deactivate-mark buffer-file-name buffer-file-truename))))
541 (,@ body)
542 (when (and (not modified) (buffer-modified-p))
543 (set-buffer-modified-p nil)))))
544 (put 'save-buffer-state 'lisp-indent-function 1))
546 ;;;###autoload
547 (defun font-lock-mode (&optional arg)
548 "Toggle Font Lock mode.
549 With arg, turn Font Lock mode on if and only if arg is positive.
551 When Font Lock mode is enabled, text is fontified as you type it:
553 - Comments are displayed in `font-lock-comment-face';
554 - Strings are displayed in `font-lock-string-face';
555 - Certain other expressions are displayed in other faces according to the
556 value of the variable `font-lock-keywords'.
558 You can enable Font Lock mode in any major mode automatically by turning on in
559 the major mode's hook. For example, put in your ~/.emacs:
561 (add-hook 'c-mode-hook 'turn-on-font-lock)
563 Alternatively, you can use Global Font Lock mode to automagically turn on Font
564 Lock mode in buffers whose major mode supports it and whose major mode is one
565 of `font-lock-global-modes'. For example, put in your ~/.emacs:
567 (global-font-lock-mode t)
569 There are a number of support modes that may be used to speed up Font Lock mode
570 in various ways, specified via the variable `font-lock-support-mode'. Where
571 major modes support different levels of fontification, you can use the variable
572 `font-lock-maximum-decoration' to specify which level you generally prefer.
573 When you turn Font Lock mode on/off the buffer is fontified/defontified, though
574 fontification occurs only if the buffer is less than `font-lock-maximum-size'.
576 For example, to specify that Font Lock mode use use Lazy Lock mode as a support
577 mode and use maximum levels of fontification, put in your ~/.emacs:
579 (setq font-lock-support-mode 'lazy-lock-mode)
580 (setq font-lock-maximum-decoration t)
582 To add your own highlighting for some major mode, and modify the highlighting
583 selected automatically via the variable `font-lock-maximum-decoration', you can
584 use `font-lock-add-keywords'.
586 To fontify a buffer, without turning on Font Lock mode and regardless of buffer
587 size, you can use \\[font-lock-fontify-buffer].
589 To fontify a block (the function or paragraph containing point, or a number of
590 lines around point), perhaps because modification on the current line caused
591 syntactic change on other lines, you can use \\[font-lock-fontify-block].
593 The default Font Lock mode faces and their attributes are defined in the
594 variable `font-lock-face-attributes', and Font Lock mode default settings in
595 the variable `font-lock-defaults-alist'. You can set your own default settings
596 for some mode, by setting a buffer local value for `font-lock-defaults', via
597 its mode hook."
598 (interactive "P")
599 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
600 ;; batch job) or if the buffer is invisible (the name starts with a space).
601 (let ((on-p (and (not noninteractive)
602 (not (eq (aref (buffer-name) 0) ?\ ))
603 (if arg
604 (> (prefix-numeric-value arg) 0)
605 (not font-lock-mode)))))
606 (set (make-local-variable 'font-lock-mode) on-p)
607 ;; Turn on Font Lock mode.
608 (when on-p
609 (make-local-hook 'after-change-functions)
610 (add-hook 'after-change-functions 'font-lock-after-change-function nil t)
611 (font-lock-set-defaults)
612 (font-lock-turn-on-thing-lock)
613 (run-hooks 'font-lock-mode-hook)
614 ;; Fontify the buffer if we have to.
615 (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size)))
616 (cond (font-lock-fontified
617 nil)
618 ((or (null max-size) (> max-size (buffer-size)))
619 (font-lock-fontify-buffer))
620 (font-lock-verbose
621 (message "Fontifying %s...buffer too big" (buffer-name))))))
622 ;; Turn off Font Lock mode.
623 (unless on-p
624 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
625 (font-lock-unfontify-buffer)
626 (font-lock-turn-off-thing-lock)
627 (font-lock-unset-defaults))
628 (force-mode-line-update)))
630 ;;;###autoload
631 (defun turn-on-font-lock ()
632 "Turn on Font Lock mode conditionally.
633 Turn on only if the terminal can display it."
634 (when (and (not font-lock-mode) window-system)
635 (font-lock-mode)))
637 ;;;###autoload
638 (defun font-lock-add-keywords (major-mode keywords &optional append)
639 "Add highlighting KEYWORDS for MAJOR-MODE.
640 MAJOR-MODE should be a symbol, the major mode command name, such as `c-mode'
641 or nil. If nil, highlighting keywords are added for the current buffer.
642 KEYWORDS should be a list; see the variable `font-lock-keywords'.
643 By default they are added at the beginning of the current highlighting list.
644 If optional argument APPEND is `set', they are used to replace the current
645 highlighting list. If APPEND has any other value, e.g., t, they are added at
646 the end of the current highlighting list.
648 For example:
650 (font-lock-add-keywords 'c-mode
651 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
652 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . font-lock-keyword-face)))
654 adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
655 comments, and to fontify `and', `or' and `not' words as keywords."
656 (cond (major-mode
657 ;; If MAJOR-MODE is non-nil, add the KEYWORDS and APPEND spec to
658 ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
659 (let ((spec (cons keywords append)) cell)
660 (if (setq cell (assq major-mode font-lock-keywords-alist))
661 (setcdr cell (append (cdr cell) (list spec)))
662 (push (list major-mode spec) font-lock-keywords-alist))))
663 (font-lock-mode
664 ;; Otherwise if Font Lock mode is on, set or add the keywords now.
665 (if (eq append 'set)
666 (setq font-lock-keywords keywords)
667 (let ((old (if (eq (car-safe font-lock-keywords) t)
668 (cdr font-lock-keywords)
669 font-lock-keywords)))
670 (setq font-lock-keywords (if append
671 (append old keywords)
672 (append keywords old))))))))
674 ;; Global Font Lock mode.
676 ;; A few people have hassled in the past for a way to make it easier to turn on
677 ;; Font Lock mode, without the user needing to know for which modes s/he has to
678 ;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always
679 ;; balked at that way, as I see it as just re-moulding the same problem in
680 ;; another form. That is; some person would still have to keep track of which
681 ;; modes (which may not even be distributed with Emacs) support Font Lock mode.
682 ;; The list would always be out of date. And that person might have to be me.
684 ;; Implementation.
686 ;; In a previous discussion the following hack came to mind. It is a gross
687 ;; hack, but it generally works. We use the convention that major modes start
688 ;; by calling the function `kill-all-local-variables', which in turn runs
689 ;; functions on the hook variable `change-major-mode-hook'. We attach our
690 ;; function `font-lock-change-major-mode' to that hook. Of course, when this
691 ;; hook is run, the major mode is in the process of being changed and we do not
692 ;; know what the final major mode will be. So, `font-lock-change-major-mode'
693 ;; only (a) notes the name of the current buffer, and (b) adds our function
694 ;; `turn-on-font-lock-if-enabled' to the hook variables `find-file-hooks' and
695 ;; `post-command-hook' (for buffers that are not visiting files). By the time
696 ;; the functions on the first of these hooks to be run are run, the new major
697 ;; mode is assumed to be in place. This way we get a Font Lock function run
698 ;; when a major mode is turned on, without knowing major modes or their hooks.
700 ;; Naturally this requires that (a) major modes run `kill-all-local-variables',
701 ;; as they are supposed to do, and (b) the major mode is in place after the
702 ;; file is visited or the command that ran `kill-all-local-variables' has
703 ;; finished, whichever the sooner. Arguably, any major mode that does not
704 ;; follow the convension (a) is broken, and I can't think of any reason why (b)
705 ;; would not be met (except `gnudoit' on non-files). However, it is not clean.
707 ;; Probably the cleanest solution is to have each major mode function run some
708 ;; hook, e.g., `major-mode-hook', but maybe implementing that change is
709 ;; impractical. I am personally against making `setq' a macro or be advised,
710 ;; or have a special function such as `set-major-mode', but maybe someone can
711 ;; come up with another solution?
713 ;; User interface.
715 ;; Although Global Font Lock mode is a pseudo-mode, I think that the user
716 ;; interface should conform to the usual Emacs convention for modes, i.e., a
717 ;; command to toggle the feature (`global-font-lock-mode') with a variable for
718 ;; finer control of the mode's behaviour (`font-lock-global-modes').
720 ;; The feature should not be enabled by loading font-lock.el, since other
721 ;; mechanisms for turning on Font Lock mode, such as M-x font-lock-mode RET or
722 ;; (add-hook 'c-mode-hook 'turn-on-font-lock), would cause Font Lock mode to be
723 ;; turned on everywhere. That would not be intuitive or informative because
724 ;; loading a file tells you nothing about the feature or how to control it. It
725 ;; would also be contrary to the Principle of Least Surprise. sm.
727 (defvar font-lock-buffers nil) ; For remembering buffers.
728 (defvar global-font-lock-mode nil)
730 ;;;###autoload
731 (defvar font-lock-global-modes t
732 "*Modes for which Font Lock mode is automagically turned on.
733 Global Font Lock mode is controlled by the `global-font-lock-mode' command.
734 If nil, means no modes have Font Lock mode automatically turned on.
735 If t, all modes that support Font Lock mode have it automatically turned on.
736 If a list, it should be a list of `major-mode' symbol names for which Font Lock
737 mode should be automatically turned on. The sense of the list is negated if it
738 begins with `not'. For example:
739 (c-mode c++-mode)
740 means that Font Lock mode is turned on for buffers in C and C++ modes only.")
742 ;;;###autoload
743 (defun global-font-lock-mode (&optional arg message)
744 "Toggle Global Font Lock mode.
745 With prefix ARG, turn Global Font Lock mode on if and only if ARG is positive.
746 Displays a message saying whether the mode is on or off if MESSAGE is non-nil.
747 Returns the new status of Global Font Lock mode (non-nil means on).
749 When Global Font Lock mode is enabled, Font Lock mode is automagically
750 turned on in a buffer if its major mode is one of `font-lock-global-modes'."
751 (interactive "P\np")
752 (let ((off-p (if arg
753 (<= (prefix-numeric-value arg) 0)
754 global-font-lock-mode)))
755 (if off-p
756 (remove-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
757 (add-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
758 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
759 (setq font-lock-buffers (buffer-list)))
760 (when message
761 (message "Global Font Lock mode is now %s." (if off-p "OFF" "ON")))
762 (setq global-font-lock-mode (not off-p))))
764 (defun font-lock-change-major-mode ()
765 ;; Turn off Font Lock mode if it's on.
766 (when font-lock-mode
767 (font-lock-mode))
768 ;; Gross hack warning: Delicate readers should avert eyes now.
769 ;; Something is running `kill-all-local-variables', which generally means the
770 ;; major mode is being changed. Run `turn-on-font-lock-if-enabled' after the
771 ;; file is visited or the current command has finished.
772 (when global-font-lock-mode
773 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
774 (add-to-list 'font-lock-buffers (current-buffer))))
776 (defun turn-on-font-lock-if-enabled ()
777 ;; Gross hack warning: Delicate readers should avert eyes now.
778 ;; Turn on Font Lock mode if it's supported by the major mode and enabled by
779 ;; the user.
780 (remove-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
781 (while font-lock-buffers
782 (when (buffer-live-p (car font-lock-buffers))
783 (save-excursion
784 (set-buffer (car font-lock-buffers))
785 (when (and (or font-lock-defaults
786 (assq major-mode font-lock-defaults-alist))
787 (or (eq font-lock-global-modes t)
788 (if (eq (car-safe font-lock-global-modes) 'not)
789 (not (memq major-mode (cdr font-lock-global-modes)))
790 (memq major-mode font-lock-global-modes))))
791 (let (inhibit-quit)
792 (turn-on-font-lock)))))
793 (setq font-lock-buffers (cdr font-lock-buffers))))
795 (add-hook 'change-major-mode-hook 'font-lock-change-major-mode)
797 ;; End of Global Font Lock mode.
799 ;; Font Lock Support mode.
801 ;; This is the code used to interface font-lock.el with any of its add-on
802 ;; packages, and provide the user interface. Packages that have their own
803 ;; local buffer fontification functions (see below) may have to call
804 ;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
805 ;; themselves.
807 ;;;###autoload
808 (defvar font-lock-support-mode nil
809 "*Support mode for Font Lock mode.
810 Support modes speed up Font Lock mode by being choosy about when fontification
811 occurs. Known support modes are Fast Lock mode (symbol `fast-lock-mode') and
812 Lazy Lock mode (symbol `lazy-lock-mode'). See those modes for more info.
813 If nil, means support for Font Lock mode is never performed.
814 If a symbol, use that support mode.
815 If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
816 where MAJOR-MODE is a symbol or t (meaning the default). For example:
817 ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
818 means that Fast Lock mode is used to support Font Lock mode for buffers in C or
819 C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
821 The value of this variable is used when Font Lock mode is turned on.")
823 (defvar fast-lock-mode nil)
824 (defvar lazy-lock-mode nil)
826 (defun font-lock-turn-on-thing-lock ()
827 (let ((thing-mode (font-lock-value-in-major-mode font-lock-support-mode)))
828 (cond ((eq thing-mode 'fast-lock-mode)
829 (fast-lock-mode t))
830 ((eq thing-mode 'lazy-lock-mode)
831 (lazy-lock-mode t)))))
833 (defun font-lock-turn-off-thing-lock ()
834 (cond (fast-lock-mode
835 (fast-lock-mode nil))
836 (lazy-lock-mode
837 (lazy-lock-mode nil))))
839 (defun font-lock-after-fontify-buffer ()
840 (cond (fast-lock-mode
841 (fast-lock-after-fontify-buffer))
842 (lazy-lock-mode
843 (lazy-lock-after-fontify-buffer))))
845 (defun font-lock-after-unfontify-buffer ()
846 (cond (fast-lock-mode
847 (fast-lock-after-unfontify-buffer))
848 (lazy-lock-mode
849 (lazy-lock-after-unfontify-buffer))))
851 ;; End of Font Lock Support mode.
853 ;; Fontification functions.
855 ;; Rather than the function, e.g., `font-lock-fontify-region' containing the
856 ;; code to fontify a region, the function runs the function whose name is the
857 ;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally,
858 ;; the value of this variable is, e.g., `font-lock-default-fontify-region'
859 ;; which does contain the code to fontify a region. However, the value of the
860 ;; variable could be anything and thus, e.g., `font-lock-fontify-region' could
861 ;; do anything. The indirection of the fontification functions gives major
862 ;; modes the capability of modifying the way font-lock.el fontifies. Major
863 ;; modes can modify the values of, e.g., `font-lock-fontify-region-function',
864 ;; via the variable `font-lock-defaults'.
866 ;; For example, Rmail mode sets the variable `font-lock-defaults' so that
867 ;; font-lock.el uses its own function for buffer fontification. This function
868 ;; makes fontification be on a message-by-message basis and so visiting an
869 ;; RMAIL file is much faster. A clever implementation of the function might
870 ;; fontify the headers differently than the message body. (It should, and
871 ;; correspondingly for Mail mode, but I can't be bothered to do the work. Can
872 ;; you?) This hints at a more interesting use...
874 ;; Languages that contain text normally contained in different major modes
875 ;; could define their own fontification functions that treat text differently
876 ;; depending on its context. For example, Perl mode could arrange that here
877 ;; docs are fontified differently than Perl code. Or Yacc mode could fontify
878 ;; rules one way and C code another. Neat!
880 ;; A further reason to use the fontification indirection feature is when the
881 ;; default syntactual fontification, or the default fontification in general,
882 ;; is not flexible enough for a particular major mode. For example, perhaps
883 ;; comments are just too hairy for `font-lock-fontify-syntactically-region' and
884 ;; `font-lock-comment-start-regexp' to cope with. You need to write your own
885 ;; version of that function, e.g., `hairy-fontify-syntactically-region', and
886 ;; make your own version of `hairy-fontify-region' call it before calling
887 ;; `font-lock-fontify-keywords-region' for the normal regexp fontification
888 ;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el
889 ;; would call your region fontification function instead of its own. For
890 ;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line
891 ;; directives correctly and cleanly. (It is the same problem as fontifying
892 ;; multi-line strings and comments; regexps are not appropriate for the job.)
894 ;;;###autoload
895 (defun font-lock-fontify-buffer ()
896 "Fontify the current buffer the way `font-lock-mode' would."
897 (interactive)
898 (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
899 (funcall font-lock-fontify-buffer-function)))
901 (defun font-lock-unfontify-buffer ()
902 (funcall font-lock-unfontify-buffer-function))
904 (defun font-lock-fontify-region (beg end &optional loudly)
905 (funcall font-lock-fontify-region-function beg end loudly))
907 (defun font-lock-unfontify-region (beg end)
908 (funcall font-lock-unfontify-region-function beg end))
910 (defun font-lock-default-fontify-buffer ()
911 (let ((verbose (if (numberp font-lock-verbose)
912 (> (buffer-size) font-lock-verbose)
913 font-lock-verbose)))
914 (when verbose
915 (message "Fontifying %s..." (buffer-name)))
916 ;; Make sure we have the right `font-lock-keywords' etc.
917 (unless font-lock-mode
918 (font-lock-set-defaults))
919 ;; Make sure we fontify etc. in the whole buffer.
920 (save-restriction
921 (widen)
922 (condition-case nil
923 (save-excursion
924 (save-match-data
925 (font-lock-fontify-region (point-min) (point-max) verbose)
926 (font-lock-after-fontify-buffer)
927 (setq font-lock-fontified t)))
928 ;; We don't restore the old fontification, so it's best to unfontify.
929 (quit (font-lock-unfontify-buffer))))
930 ;; Make sure we undo `font-lock-keywords' etc.
931 (unless font-lock-mode
932 (font-lock-unset-defaults))
933 (if verbose (message "Fontifying %s...%s" (buffer-name)
934 (if font-lock-fontified "done" "quit")))))
936 (defun font-lock-default-unfontify-buffer ()
937 ;; Make sure we unfontify etc. in the whole buffer.
938 (save-restriction
939 (widen)
940 (font-lock-unfontify-region (point-min) (point-max))
941 (font-lock-after-unfontify-buffer)
942 (setq font-lock-fontified nil)))
944 (defun font-lock-default-fontify-region (beg end loudly)
945 (save-buffer-state ((old-syntax-table (syntax-table)))
946 (unwind-protect
947 (save-restriction
948 (widen)
949 ;; Use the fontification syntax table, if any.
950 (when font-lock-syntax-table
951 (set-syntax-table font-lock-syntax-table))
952 ;; Now do the fontification.
953 (font-lock-unfontify-region beg end)
954 (unless font-lock-keywords-only
955 (font-lock-fontify-syntactically-region beg end loudly))
956 (font-lock-fontify-keywords-region beg end loudly))
957 ;; Clean up.
958 (set-syntax-table old-syntax-table))))
960 ;; The following must be rethought, since keywords can override fontification.
961 ; ;; Now scan for keywords, but not if we are inside a comment now.
962 ; (or (and (not font-lock-keywords-only)
963 ; (let ((state (parse-partial-sexp beg end nil nil
964 ; font-lock-cache-state)))
965 ; (or (nth 4 state) (nth 7 state))))
966 ; (font-lock-fontify-keywords-region beg end))
968 (defun font-lock-default-unfontify-region (beg end)
969 (save-buffer-state nil
970 (remove-text-properties beg end '(face nil))))
972 ;; Called when any modification is made to buffer text.
973 (defun font-lock-after-change-function (beg end old-len)
974 (save-excursion
975 (save-match-data
976 ;; Rescan between start of lines enclosing the region.
977 (font-lock-fontify-region
978 (progn (goto-char beg) (beginning-of-line) (point))
979 (progn (goto-char end) (forward-line 1) (point))))))
981 (defun font-lock-fontify-block (&optional arg)
982 "Fontify some lines the way `font-lock-fontify-buffer' would.
983 The lines could be a function or paragraph, or a specified number of lines.
984 If ARG is given, fontify that many lines before and after point, or 16 lines if
985 no ARG is given and `font-lock-mark-block-function' is nil.
986 If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
987 delimit the region to fontify."
988 (interactive "P")
989 (let (font-lock-beginning-of-syntax-function deactivate-mark)
990 ;; Make sure we have the right `font-lock-keywords' etc.
991 (if (not font-lock-mode) (font-lock-set-defaults))
992 (save-excursion
993 (save-match-data
994 (condition-case error-data
995 (if (or arg (not font-lock-mark-block-function))
996 (let ((lines (if arg (prefix-numeric-value arg) 16)))
997 (font-lock-fontify-region
998 (save-excursion (forward-line (- lines)) (point))
999 (save-excursion (forward-line lines) (point))))
1000 (funcall font-lock-mark-block-function)
1001 (font-lock-fontify-region (point) (mark)))
1002 ((error quit) (message "Fontifying block...%s" error-data)))))))
1004 (define-key facemenu-keymap "\M-g" 'font-lock-fontify-block)
1006 ;; Syntactic fontification functions.
1008 ;; These record the parse state at a particular position, always the start of a
1009 ;; line. Used to make `font-lock-fontify-syntactically-region' faster.
1010 ;; Previously, `font-lock-cache-position' was just a buffer position. However,
1011 ;; under certain situations, this occasionally resulted in mis-fontification.
1012 ;; I think the "situations" were deletion with Lazy Lock mode's deferral. sm.
1013 (defvar font-lock-cache-state nil)
1014 (defvar font-lock-cache-position nil)
1016 (defun font-lock-fontify-syntactically-region (start end &optional loudly)
1017 "Put proper face on each string and comment between START and END.
1018 START should be at the beginning of a line."
1019 (let ((synstart (cond (font-lock-comment-start-regexp
1020 (concat "\\s\"\\|" font-lock-comment-start-regexp))
1021 (comment-start-skip
1022 (concat "\\s\"\\|" comment-start-skip))
1024 "\\s\"")))
1025 (cache (marker-position font-lock-cache-position))
1026 state prev here beg)
1027 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
1028 (goto-char start)
1030 ;; Find the state at the `beginning-of-line' before `start'.
1031 (if (eq start cache)
1032 ;; Use the cache for the state of `start'.
1033 (setq state font-lock-cache-state)
1034 ;; Find the state of `start'.
1035 (if (null font-lock-beginning-of-syntax-function)
1036 ;; Use the state at the previous cache position, if any, or
1037 ;; otherwise calculate from `point-min'.
1038 (if (or (null cache) (< start cache))
1039 (setq state (parse-partial-sexp (point-min) start))
1040 (setq state (parse-partial-sexp cache start nil nil
1041 font-lock-cache-state)))
1042 ;; Call the function to move outside any syntactic block.
1043 (funcall font-lock-beginning-of-syntax-function)
1044 (setq state (parse-partial-sexp (point) start)))
1045 ;; Cache the state and position of `start'.
1046 (setq font-lock-cache-state state)
1047 (set-marker font-lock-cache-position start))
1049 ;; If the region starts inside a string, show the extent of it.
1050 (when (nth 3 state)
1051 (setq here (point))
1052 (while (and (re-search-forward "\\s\"" end 'move)
1053 ;; Verify the state so we don't get fooled by quoting.
1054 (nth 3 (parse-partial-sexp here (point) nil nil state))))
1055 (put-text-property here (point) 'face font-lock-string-face)
1056 (setq state (parse-partial-sexp here (point) nil nil state)))
1058 ;; Likewise for a comment.
1059 (when (or (nth 4 state) (nth 7 state))
1060 (let ((comstart (cond (font-lock-comment-start-regexp
1061 font-lock-comment-start-regexp)
1062 (comment-start-skip
1063 (concat "\\s<\\|" comment-start-skip))
1065 "\\s<")))
1066 (count 1))
1067 (setq here (point))
1068 (condition-case nil
1069 (save-restriction
1070 (narrow-to-region (point-min) end)
1071 ;; Go back to the real start of the comment.
1072 (re-search-backward comstart)
1073 (forward-comment 1)
1074 ;; If there is more than one comment type, then the previous
1075 ;; comment start might not be the real comment start.
1076 ;; For example, in C++ code, `here' might be on a line following
1077 ;; a // comment that is actually within a /* */ comment.
1078 (while (<= (point) here)
1079 (goto-char here)
1080 (re-search-backward comstart nil nil (incf count))
1081 (forward-comment 1))
1082 ;; Go back to the real end of the comment.
1083 (skip-chars-backward " \t"))
1084 (error (goto-char end)))
1085 (put-text-property here (point) 'face font-lock-comment-face)
1086 (setq state (parse-partial-sexp here (point) nil nil state))))
1088 ;; Find each interesting place between here and `end'.
1089 (while (and (< (point) end)
1090 (setq prev (point))
1091 (re-search-forward synstart end t)
1092 (setq state (parse-partial-sexp prev (point) nil nil state)))
1093 (cond ((nth 3 state)
1095 ;; Found a real string start.
1096 (setq here (point) beg (match-beginning 0))
1097 (condition-case nil
1098 (save-restriction
1099 (narrow-to-region (point-min) end)
1100 (goto-char (scan-sexps beg 1)))
1101 (error (goto-char end)))
1102 (put-text-property beg (point) 'face font-lock-string-face)
1103 (setq state (parse-partial-sexp here (point) nil nil state)))
1104 ((or (nth 4 state) (nth 7 state))
1106 ;; Found a real comment start.
1107 (setq here (point) beg (or (match-end 1) (match-beginning 0)))
1108 (goto-char beg)
1109 (condition-case nil
1110 (save-restriction
1111 (narrow-to-region (point-min) end)
1112 (forward-comment 1)
1113 (skip-chars-backward " \t"))
1114 (error (goto-char end)))
1115 (put-text-property beg (point) 'face font-lock-comment-face)
1116 (setq state (parse-partial-sexp here (point) nil nil state)))))))
1118 ;;; Additional text property functions.
1120 ;; The following text property functions should be builtins. This means they
1121 ;; should be written in C and put with all the other text property functions.
1122 ;; In the meantime, those that are used by font-lock.el are defined in Lisp
1123 ;; below and given a `font-lock-' prefix. Those that are not used are defined
1124 ;; in Lisp below and commented out. sm.
1126 (defun font-lock-prepend-text-property (start end prop value &optional object)
1127 "Prepend to one property of the text from START to END.
1128 Arguments PROP and VALUE specify the property and value to prepend to the value
1129 already in place. The resulting property values are always lists.
1130 Optional argument OBJECT is the string or buffer containing the text."
1131 (let ((val (if (listp value) value (list value))) next prev)
1132 (while (/= start end)
1133 (setq next (next-single-property-change start prop object end)
1134 prev (get-text-property start prop object))
1135 (put-text-property start next prop
1136 (append val (if (listp prev) prev (list prev)))
1137 object)
1138 (setq start next))))
1140 (defun font-lock-append-text-property (start end prop value &optional object)
1141 "Append to one property of the text from START to END.
1142 Arguments PROP and VALUE specify the property and value to append to the value
1143 already in place. The resulting property values are always lists.
1144 Optional argument OBJECT is the string or buffer containing the text."
1145 (let ((val (if (listp value) value (list value))) next prev)
1146 (while (/= start end)
1147 (setq next (next-single-property-change start prop object end)
1148 prev (get-text-property start prop object))
1149 (put-text-property start next prop
1150 (append (if (listp prev) prev (list prev)) val)
1151 object)
1152 (setq start next))))
1154 (defun font-lock-fillin-text-property (start end prop value &optional object)
1155 "Fill in one property of the text from START to END.
1156 Arguments PROP and VALUE specify the property and value to put where none are
1157 already in place. Therefore existing property values are not overwritten.
1158 Optional argument OBJECT is the string or buffer containing the text."
1159 (let ((start (text-property-any start end prop nil object)) next)
1160 (while start
1161 (setq next (next-single-property-change start prop object end))
1162 (put-text-property start next prop value object)
1163 (setq start (text-property-any next end prop nil object)))))
1165 ;; For completeness: this is to `remove-text-properties' as `put-text-property'
1166 ;; is to `add-text-properties', etc.
1167 ;(defun remove-text-property (start end property &optional object)
1168 ; "Remove a property from text from START to END.
1169 ;Argument PROPERTY is the property to remove.
1170 ;Optional argument OBJECT is the string or buffer containing the text.
1171 ;Return t if the property was actually removed, nil otherwise."
1172 ; (remove-text-properties start end (list property) object))
1174 ;; For consistency: maybe this should be called `remove-single-property' like
1175 ;; `next-single-property-change' (not `next-single-text-property-change'), etc.
1176 ;(defun remove-single-text-property (start end prop value &optional object)
1177 ; "Remove a specific property value from text from START to END.
1178 ;Arguments PROP and VALUE specify the property and value to remove. The
1179 ;resulting property values are not equal to VALUE nor lists containing VALUE.
1180 ;Optional argument OBJECT is the string or buffer containing the text."
1181 ; (let ((start (text-property-not-all start end prop nil object)) next prev)
1182 ; (while start
1183 ; (setq next (next-single-property-change start prop object end)
1184 ; prev (get-text-property start prop object))
1185 ; (cond ((and (symbolp prev) (eq value prev))
1186 ; (remove-text-property start next prop object))
1187 ; ((and (listp prev) (memq value prev))
1188 ; (let ((new (delq value prev)))
1189 ; (cond ((null new)
1190 ; (remove-text-property start next prop object))
1191 ; ((= (length new) 1)
1192 ; (put-text-property start next prop (car new) object))
1193 ; (t
1194 ; (put-text-property start next prop new object))))))
1195 ; (setq start (text-property-not-all next end prop nil object)))))
1197 ;;; Regexp fontification functions.
1199 (defsubst font-lock-apply-highlight (highlight)
1200 "Apply HIGHLIGHT following a match.
1201 HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
1202 (let* ((match (nth 0 highlight))
1203 (start (match-beginning match)) (end (match-end match))
1204 (override (nth 2 highlight)))
1205 (cond ((not start)
1206 ;; No match but we might not signal an error.
1207 (or (nth 3 highlight)
1208 (error "No match %d in highlight %S" match highlight)))
1209 ((not override)
1210 ;; Cannot override existing fontification.
1211 (or (text-property-not-all start end 'face nil)
1212 (put-text-property start end 'face (eval (nth 1 highlight)))))
1213 ((eq override t)
1214 ;; Override existing fontification.
1215 (put-text-property start end 'face (eval (nth 1 highlight))))
1216 ((eq override 'prepend)
1217 ;; Prepend to existing fontification.
1218 (font-lock-prepend-text-property start end 'face (eval (nth 1 highlight))))
1219 ((eq override 'append)
1220 ;; Append to existing fontification.
1221 (font-lock-append-text-property start end 'face (eval (nth 1 highlight))))
1222 ((eq override 'keep)
1223 ;; Keep existing fontification.
1224 (font-lock-fillin-text-property start end 'face (eval (nth 1 highlight)))))))
1226 (defsubst font-lock-fontify-anchored-keywords (keywords limit)
1227 "Fontify according to KEYWORDS until LIMIT.
1228 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1229 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1230 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1231 ;; Evaluate PRE-MATCH-FORM.
1232 (pre-match-value (eval (nth 1 keywords))))
1233 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1234 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1235 (setq limit pre-match-value)
1236 (save-excursion (end-of-line) (setq limit (point))))
1237 (save-match-data
1238 ;; Find an occurrence of `matcher' before `limit'.
1239 (while (if (stringp matcher)
1240 (re-search-forward matcher limit t)
1241 (funcall matcher limit))
1242 ;; Apply each highlight to this instance of `matcher'.
1243 (setq highlights lowdarks)
1244 (while highlights
1245 (font-lock-apply-highlight (car highlights))
1246 (setq highlights (cdr highlights)))))
1247 ;; Evaluate POST-MATCH-FORM.
1248 (eval (nth 2 keywords))))
1250 (defun font-lock-fontify-keywords-region (start end &optional loudly)
1251 "Fontify according to `font-lock-keywords' between START and END.
1252 START should be at the beginning of a line."
1253 (let ((case-fold-search font-lock-keywords-case-fold-search)
1254 (keywords (cdr (if (eq (car-safe font-lock-keywords) t)
1255 font-lock-keywords
1256 (font-lock-compile-keywords))))
1257 (bufname (buffer-name)) (count 0)
1258 keyword matcher highlights)
1260 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1261 (while keywords
1262 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
1263 (make-string (incf count) ?.)))
1265 ;; Find an occurrence of `matcher' from `start' to `end'.
1266 (setq keyword (car keywords) matcher (car keyword))
1267 (goto-char start)
1268 (while (if (stringp matcher)
1269 (re-search-forward matcher end t)
1270 (funcall matcher end))
1271 ;; Apply each highlight to this instance of `matcher', which may be
1272 ;; specific highlights or more keywords anchored to `matcher'.
1273 (setq highlights (cdr keyword))
1274 (while highlights
1275 (if (numberp (car (car highlights)))
1276 (font-lock-apply-highlight (car highlights))
1277 (font-lock-fontify-anchored-keywords (car highlights) end))
1278 (setq highlights (cdr highlights))))
1279 (setq keywords (cdr keywords)))))
1281 ;; Various functions.
1283 (defun font-lock-compile-keywords (&optional keywords)
1284 ;; Compile `font-lock-keywords' into the form (t KEYWORD ...) where KEYWORD
1285 ;; is the (MATCHER HIGHLIGHT ...) shown in the variable's doc string.
1286 (let ((keywords (or keywords font-lock-keywords)))
1287 (setq font-lock-keywords
1288 (if (eq (car-safe keywords) t)
1289 keywords
1290 (cons t (mapcar 'font-lock-compile-keyword keywords))))))
1292 (defun font-lock-compile-keyword (keyword)
1293 (cond ((nlistp keyword) ; MATCHER
1294 (list keyword '(0 font-lock-keyword-face)))
1295 ((eq (car keyword) 'eval) ; (eval . FORM)
1296 (font-lock-compile-keyword (eval (cdr keyword))))
1297 ((eq (car-safe (cdr keyword)) 'quote) ; (MATCHER . 'FORM)
1298 ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote.
1299 (if (symbolp (nth 2 keyword))
1300 (list (car keyword) (list 0 (cdr keyword)))
1301 (font-lock-compile-keyword (cons (car keyword) (nth 2 keyword)))))
1302 ((numberp (cdr keyword)) ; (MATCHER . MATCH)
1303 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
1304 ((symbolp (cdr keyword)) ; (MATCHER . FACENAME)
1305 (list (car keyword) (list 0 (cdr keyword))))
1306 ((nlistp (nth 1 keyword)) ; (MATCHER . HIGHLIGHT)
1307 (list (car keyword) (cdr keyword)))
1308 (t ; (MATCHER HIGHLIGHT ...)
1309 keyword)))
1311 (defun font-lock-value-in-major-mode (alist)
1312 ;; Return value in ALIST for `major-mode', or ALIST if it is not an alist.
1313 ;; Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t.
1314 (if (consp alist)
1315 (cdr (or (assq major-mode alist) (assq t alist)))
1316 alist))
1318 (defun font-lock-choose-keywords (keywords level)
1319 ;; Return LEVELth element of KEYWORDS. A LEVEL of nil is equal to a
1320 ;; LEVEL of 0, a LEVEL of t is equal to (1- (length KEYWORDS)).
1321 (cond ((symbolp keywords)
1322 keywords)
1323 ((numberp level)
1324 (or (nth level keywords) (car (reverse keywords))))
1325 ((eq level t)
1326 (car (reverse keywords)))
1328 (car keywords))))
1330 (defvar font-lock-set-defaults nil) ; Whether we have set up defaults.
1332 (defun font-lock-set-defaults ()
1333 "Set fontification defaults appropriately for this mode.
1334 Sets various variables using `font-lock-defaults' (or, if nil, using
1335 `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
1336 ;; Set face defaults.
1337 (font-lock-make-faces)
1338 ;; Set fontification defaults.
1339 (make-local-variable 'font-lock-fontified)
1340 ;; Set iff not previously set.
1341 (unless font-lock-set-defaults
1342 (set (make-local-variable 'font-lock-set-defaults) t)
1343 (set (make-local-variable 'font-lock-cache-state) nil)
1344 (set (make-local-variable 'font-lock-cache-position) (make-marker))
1345 (let* ((defaults (or font-lock-defaults
1346 (cdr (assq major-mode font-lock-defaults-alist))))
1347 (keywords
1348 (font-lock-choose-keywords (nth 0 defaults)
1349 (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1350 (local (cdr (assq major-mode font-lock-keywords-alist))))
1351 ;; Regexp fontification?
1352 (setq font-lock-keywords (if (fboundp keywords)
1353 (funcall keywords)
1354 (eval keywords)))
1355 ;; Local fontification?
1356 (while local
1357 (font-lock-add-keywords nil (car (car local)) (cdr (car local)))
1358 (setq local (cdr local)))
1359 ;; Syntactic fontification?
1360 (when (nth 1 defaults)
1361 (set (make-local-variable 'font-lock-keywords-only) t))
1362 ;; Case fold during regexp fontification?
1363 (when (nth 2 defaults)
1364 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
1365 ;; Syntax table for regexp and syntactic fontification?
1366 (when (nth 3 defaults)
1367 (let ((slist (nth 3 defaults)))
1368 (set (make-local-variable 'font-lock-syntax-table)
1369 (copy-syntax-table (syntax-table)))
1370 (while slist
1371 ;; The character to modify may be a single CHAR or a STRING.
1372 (let ((chars (if (numberp (car (car slist)))
1373 (list (car (car slist)))
1374 (mapcar 'identity (car (car slist)))))
1375 (syntax (cdr (car slist))))
1376 (while chars
1377 (modify-syntax-entry (car chars) syntax
1378 font-lock-syntax-table)
1379 (setq chars (cdr chars)))
1380 (setq slist (cdr slist))))))
1381 ;; Syntax function for syntactic fontification?
1382 (when (nth 4 defaults)
1383 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1384 (nth 4 defaults)))
1385 ;; Variable alist?
1386 (let ((alist (nthcdr 5 defaults)))
1387 (while alist
1388 (set (make-local-variable (car (car alist))) (cdr (car alist)))
1389 (setq alist (cdr alist)))))))
1391 (defun font-lock-unset-defaults ()
1392 "Unset fontification defaults. See `font-lock-set-defaults'."
1393 (setq font-lock-set-defaults nil
1394 font-lock-keywords nil
1395 font-lock-keywords-only nil
1396 font-lock-keywords-case-fold-search nil
1397 font-lock-syntax-table nil
1398 font-lock-beginning-of-syntax-function nil)
1399 (let* ((defaults (or font-lock-defaults
1400 (cdr (assq major-mode font-lock-defaults-alist))))
1401 (alist (nthcdr 5 defaults)))
1402 (while alist
1403 (set (car (car alist)) (default-value (car (car alist))))
1404 (setq alist (cdr alist)))))
1406 ;; Colour etc. support.
1408 ;; This section of code is crying out for revision. Come on down, custom.el?
1410 ;; To begin with, `display-type' and `background-mode' are `frame-parameters'
1411 ;; so we don't have to calculate them here anymore. But all the face stuff
1412 ;; should be frame-local (and thus display-local) anyway. Because we're not
1413 ;; sure what support Emacs is going to have for general frame-local face
1414 ;; attributes, we leave this section of code as it is. For now. sm.
1416 (defvar font-lock-display-type nil
1417 "A symbol indicating the display Emacs is running under.
1418 The symbol should be one of `color', `grayscale' or `mono'.
1419 If Emacs guesses this display attribute wrongly, either set this variable in
1420 your `~/.emacs' or set the resource `Emacs.displayType' in your `~/.Xdefaults'.
1421 See also `font-lock-background-mode' and `font-lock-face-attributes'.")
1423 (defvar font-lock-background-mode nil
1424 "A symbol indicating the Emacs background brightness.
1425 The symbol should be one of `light' or `dark'.
1426 If Emacs guesses this frame attribute wrongly, either set this variable in
1427 your `~/.emacs' or set the resource `Emacs.backgroundMode' in your
1428 `~/.Xdefaults'.
1429 See also `font-lock-display-type' and `font-lock-face-attributes'.")
1431 (defvar font-lock-face-attributes nil
1432 "A list of default attributes to use for face attributes.
1433 Each element of the list should be of the form
1435 (FACE FOREGROUND BACKGROUND BOLD-P ITALIC-P UNDERLINE-P)
1437 where FACE could be one of the face symbols `font-lock-comment-face',
1438 `font-lock-string-face', `font-lock-keyword-face', `font-lock-builtin-face',
1439 `font-lock-type-face', `font-lock-function-name-face',
1440 `font-lock-variable-name-face', `font-lock-reference-face' and
1441 `font-lock-warning-face', or any other face symbols and attributes may be
1442 specified here and used in `font-lock-keywords'.
1444 Subsequent element items should be the attributes for the corresponding
1445 Font Lock mode faces. Attributes FOREGROUND and BACKGROUND should be strings
1446 \(default if nil), while BOLD-P, ITALIC-P, and UNDERLINE-P should specify the
1447 corresponding face attributes (yes if non-nil). For example:
1449 (setq font-lock-face-attributes '((font-lock-warning-face \"HotPink\" nil t t)
1450 (font-lock-comment-face \"Red\")))
1452 in your ~/.emacs makes a garish bold-italic warning face and red comment face.
1454 Emacs uses default attributes based on display type and background brightness.
1455 See variables `font-lock-display-type' and `font-lock-background-mode'.
1457 Resources can be used to over-ride these face attributes. For example, the
1458 resource `Emacs.font-lock-comment-face.attributeUnderline' can be used to
1459 specify the UNDERLINE-P attribute for face `font-lock-comment-face'.")
1461 (defun font-lock-make-faces (&optional override)
1462 "Make faces from `font-lock-face-attributes'.
1463 A default list is used if this is nil.
1464 If optional OVERRIDE is non-nil, faces that already exist are reset.
1465 See `font-lock-make-face' and `list-faces-display'."
1466 ;; We don't need to `setq' any of these variables, but the user can see what
1467 ;; is being used if we do.
1468 (unless font-lock-display-type
1469 (setq font-lock-display-type
1470 (let ((display-resource (x-get-resource ".displayType" "DisplayType")))
1471 (cond (display-resource (intern (downcase display-resource)))
1472 ((x-display-color-p) 'color)
1473 ((x-display-grayscale-p) 'grayscale)
1474 (t 'mono)))))
1475 (unless font-lock-background-mode
1476 (setq font-lock-background-mode
1477 (let ((bg-resource (x-get-resource ".backgroundMode" "BackgroundMode"))
1478 (params (frame-parameters)))
1479 (cond (bg-resource (intern (downcase bg-resource)))
1480 ((eq system-type 'ms-dos)
1481 (if (string-match "light" (cdr (assq 'background-color params)))
1482 'light
1483 'dark))
1484 ((< (apply '+ (x-color-values
1485 (cdr (assq 'background-color params))))
1486 (* (apply '+ (x-color-values "white")) .6))
1487 'dark)
1488 (t 'light)))))
1489 (let ((face-attributes
1490 (let ((light-bg (eq font-lock-background-mode 'light)))
1491 (cond ((memq font-lock-display-type '(mono monochrome))
1492 ;; Emacs 19.25's font-lock defaults:
1493 ;;'((font-lock-comment-face nil nil nil t nil)
1494 ;; (font-lock-string-face nil nil nil nil t)
1495 ;; (font-lock-keyword-face nil nil t nil nil)
1496 ;; (font-lock-function-name-face nil nil t t nil)
1497 ;; (font-lock-type-face nil nil nil t nil))
1498 (list '(font-lock-comment-face nil nil t t nil)
1499 '(font-lock-string-face nil nil nil t nil)
1500 '(font-lock-keyword-face nil nil t nil nil)
1501 '(font-lock-builtin-face nil nil t nil nil)
1502 (list
1503 'font-lock-function-name-face
1504 (cdr (assq 'background-color (frame-parameters)))
1505 (cdr (assq 'foreground-color (frame-parameters)))
1506 t nil nil)
1507 '(font-lock-variable-name-face nil nil t t nil)
1508 '(font-lock-type-face nil nil t nil t)
1509 '(font-lock-reference-face nil nil t nil t)
1510 (list
1511 'font-lock-warning-face
1512 (cdr (assq 'background-color (frame-parameters)))
1513 (cdr (assq 'foreground-color (frame-parameters)))
1514 t nil nil)))
1515 ((memq font-lock-display-type '(grayscale greyscale
1516 grayshade greyshade))
1517 (list
1518 (list 'font-lock-comment-face
1519 (if light-bg "DimGray" "LightGray") nil t t nil)
1520 (list 'font-lock-string-face
1521 (if light-bg "DimGray" "LightGray") nil nil t nil)
1522 (list 'font-lock-keyword-face
1523 nil (if light-bg "LightGray" "DimGray") t nil nil)
1524 (list 'font-lock-builtin-face
1525 nil (if light-bg "LightGray" "DimGray") t nil nil)
1526 (list 'font-lock-function-name-face
1527 (cdr (assq 'background-color (frame-parameters)))
1528 (cdr (assq 'foreground-color (frame-parameters)))
1529 t nil nil)
1530 (list 'font-lock-variable-name-face
1531 nil (if light-bg "Gray90" "DimGray") t t nil)
1532 (list 'font-lock-type-face
1533 nil (if light-bg "Gray80" "DimGray") t nil nil)
1534 (list 'font-lock-reference-face
1535 nil (if light-bg "LightGray" "Gray50") t nil t)
1536 (list 'font-lock-warning-face
1537 (cdr (assq 'background-color (frame-parameters)))
1538 (cdr (assq 'foreground-color (frame-parameters)))
1539 t nil nil)))
1540 (light-bg ; light colour background
1541 '((font-lock-comment-face "Firebrick")
1542 (font-lock-string-face "RosyBrown")
1543 (font-lock-keyword-face "Purple")
1544 (font-lock-builtin-face "Orchid")
1545 (font-lock-function-name-face "Blue")
1546 (font-lock-variable-name-face "DarkGoldenrod")
1547 (font-lock-type-face "DarkOliveGreen")
1548 (font-lock-reference-face "CadetBlue")
1549 (font-lock-warning-face "Red" nil t nil nil)))
1550 (t ; dark colour background
1551 '((font-lock-comment-face "OrangeRed")
1552 (font-lock-string-face "LightSalmon")
1553 (font-lock-keyword-face "Cyan")
1554 (font-lock-builtin-face "LightSteelBlue")
1555 (font-lock-function-name-face "LightSkyBlue")
1556 (font-lock-variable-name-face "LightGoldenrod")
1557 (font-lock-type-face "PaleGreen")
1558 (font-lock-reference-face "Aquamarine")
1559 (font-lock-warning-face "Pink" nil t nil nil)))))))
1560 (while face-attributes
1561 (unless (assq (car (car face-attributes)) font-lock-face-attributes)
1562 (push (car face-attributes) font-lock-face-attributes))
1563 (setq face-attributes (cdr face-attributes))))
1564 ;; Now make the faces if we have to.
1565 (mapcar (function
1566 (lambda (face-attributes)
1567 (let ((face (nth 0 face-attributes)))
1568 (cond (override
1569 ;; We can stomp all over it anyway. Get outta my face!
1570 (font-lock-make-face face-attributes))
1571 ((and (boundp face) (facep (symbol-value face)))
1572 ;; The variable exists and is already bound to a face.
1573 nil)
1574 ((facep face)
1575 ;; We already have a face so we bind the variable to it.
1576 (set face face))
1578 ;; No variable or no face.
1579 (font-lock-make-face face-attributes))))))
1580 font-lock-face-attributes))
1582 (defun font-lock-make-face (face-attributes)
1583 "Make a face from FACE-ATTRIBUTES.
1584 FACE-ATTRIBUTES should be like an element `font-lock-face-attributes', so that
1585 the face name is the first item in the list. A variable with the same name as
1586 the face is also set; its value is the face name."
1587 (let* ((face (nth 0 face-attributes))
1588 (face-name (symbol-name face))
1589 (set-p (function (lambda (face-name resource)
1590 (x-get-resource (concat face-name ".attribute" resource)
1591 (concat "Face.Attribute" resource)))))
1592 (on-p (function (lambda (face-name resource)
1593 (let ((set (funcall set-p face-name resource)))
1594 (and set (member (downcase set) '("on" "true"))))))))
1595 (make-face face)
1596 (add-to-list 'facemenu-unlisted-faces face)
1597 ;; Set attributes not set from X resources (and therefore `make-face').
1598 (or (funcall set-p face-name "Foreground")
1599 (condition-case nil
1600 (set-face-foreground face (nth 1 face-attributes))
1601 (error nil)))
1602 (or (funcall set-p face-name "Background")
1603 (condition-case nil
1604 (set-face-background face (nth 2 face-attributes))
1605 (error nil)))
1606 (if (funcall set-p face-name "Bold")
1607 (and (funcall on-p face-name "Bold") (make-face-bold face nil t))
1608 (and (nth 3 face-attributes) (make-face-bold face nil t)))
1609 (if (funcall set-p face-name "Italic")
1610 (and (funcall on-p face-name "Italic") (make-face-italic face nil t))
1611 (and (nth 4 face-attributes) (make-face-italic face nil t)))
1612 (or (funcall set-p face-name "Underline")
1613 (set-face-underline-p face (nth 5 face-attributes)))
1614 (set face face)))
1616 ;;; Menu support.
1618 ;; This section of code is commented out because Emacs does not have real menu
1619 ;; buttons. (We can mimic them by putting "( ) " or "(X) " at the beginning of
1620 ;; the menu entry text, but with Xt it looks both ugly and embarrassingly
1621 ;; amateur.) If/When Emacs gets real menus buttons, put in menu-bar.el after
1622 ;; the entry for "Text Properties" something like:
1624 ;; (define-key menu-bar-edit-menu [font-lock]
1625 ;; '("Syntax Highlighting" . font-lock-menu))
1627 ;; and remove a single ";" from the beginning of each line in the rest of this
1628 ;; section. Probably the mechanism for telling the menu code what are menu
1629 ;; buttons and when they are on or off needs tweaking. I have assumed that the
1630 ;; mechanism is via `menu-toggle' and `menu-selected' symbol properties. sm.
1632 ;;;;###autoload
1633 ;(progn
1634 ; ;; Make the Font Lock menu.
1635 ; (defvar font-lock-menu (make-sparse-keymap "Syntax Highlighting"))
1636 ; ;; Add the menu items in reverse order.
1637 ; (define-key font-lock-menu [fontify-less]
1638 ; '("Less In Current Buffer" . font-lock-fontify-less))
1639 ; (define-key font-lock-menu [fontify-more]
1640 ; '("More In Current Buffer" . font-lock-fontify-more))
1641 ; (define-key font-lock-menu [font-lock-sep]
1642 ; '("--"))
1643 ; (define-key font-lock-menu [font-lock-mode]
1644 ; '("In Current Buffer" . font-lock-mode))
1645 ; (define-key font-lock-menu [global-font-lock-mode]
1646 ; '("In All Buffers" . global-font-lock-mode)))
1648 ;;;;###autoload
1649 ;(progn
1650 ; ;; We put the appropriate `menu-enable' etc. symbol property values on when
1651 ; ;; font-lock.el is loaded, so we don't need to autoload the three variables.
1652 ; (put 'global-font-lock-mode 'menu-toggle t)
1653 ; (put 'font-lock-mode 'menu-toggle t)
1654 ; (put 'font-lock-fontify-more 'menu-enable '(identity))
1655 ; (put 'font-lock-fontify-less 'menu-enable '(identity)))
1657 ;;; Put the appropriate symbol property values on now. See above.
1658 ;(put 'global-font-lock-mode 'menu-selected 'global-font-lock-mode))
1659 ;(put 'font-lock-mode 'menu-selected 'font-lock-mode)
1660 ;(put 'font-lock-fontify-more 'menu-enable '(nth 2 font-lock-fontify-level))
1661 ;(put 'font-lock-fontify-less 'menu-enable '(nth 1 font-lock-fontify-level))
1663 ;(defvar font-lock-fontify-level nil) ; For less/more fontification.
1665 ;(defun font-lock-fontify-level (level)
1666 ; (let ((font-lock-maximum-decoration level))
1667 ; (when font-lock-mode
1668 ; (font-lock-mode))
1669 ; (font-lock-mode)
1670 ; (when font-lock-verbose
1671 ; (message "Fontifying %s... level %d" (buffer-name) level))))
1673 ;(defun font-lock-fontify-less ()
1674 ; "Fontify the current buffer with less decoration.
1675 ;See `font-lock-maximum-decoration'."
1676 ; (interactive)
1677 ; ;; Check in case we get called interactively.
1678 ; (if (nth 1 font-lock-fontify-level)
1679 ; (font-lock-fontify-level (1- (car font-lock-fontify-level)))
1680 ; (error "No less decoration")))
1682 ;(defun font-lock-fontify-more ()
1683 ; "Fontify the current buffer with more decoration.
1684 ;See `font-lock-maximum-decoration'."
1685 ; (interactive)
1686 ; ;; Check in case we get called interactively.
1687 ; (if (nth 2 font-lock-fontify-level)
1688 ; (font-lock-fontify-level (1+ (car font-lock-fontify-level)))
1689 ; (error "No more decoration")))
1691 ;;; This should be called by `font-lock-set-defaults'.
1692 ;(defun font-lock-set-menu ()
1693 ; ;; Activate less/more fontification entries if there are multiple levels for
1694 ; ;; the current buffer. Sets `font-lock-fontify-level' to be of the form
1695 ; ;; (CURRENT-LEVEL IS-LOWER-LEVEL-P IS-HIGHER-LEVEL-P) for menu activation.
1696 ; (let ((keywords (or (nth 0 font-lock-defaults)
1697 ; (nth 1 (assq major-mode font-lock-defaults-alist))))
1698 ; (level (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1699 ; (make-local-variable 'font-lock-fontify-level)
1700 ; (if (or (symbolp keywords) (= (length keywords) 1))
1701 ; (font-lock-unset-menu)
1702 ; (cond ((eq level t)
1703 ; (setq level (1- (length keywords))))
1704 ; ((or (null level) (zerop level))
1705 ; ;; The default level is usually, but not necessarily, level 1.
1706 ; (setq level (- (length keywords)
1707 ; (length (member (eval (car keywords))
1708 ; (mapcar 'eval (cdr keywords))))))))
1709 ; (setq font-lock-fontify-level (list level (> level 1)
1710 ; (< level (1- (length keywords))))))))
1712 ;;; This should be called by `font-lock-unset-defaults'.
1713 ;(defun font-lock-unset-menu ()
1714 ; ;; Deactivate less/more fontification entries.
1715 ; (setq font-lock-fontify-level nil))
1717 ;;; Various regexp information shared by several modes.
1718 ;;; Information specific to a single mode should go in its load library.
1720 ;; Font Lock support for C, C++, Objective-C and Java modes will one day be in
1721 ;; some cc-font.el (and required by cc-mode.el). However, the below function
1722 ;; should stay in font-lock.el, since it is used by other libraries. sm.
1724 (defun font-lock-match-c-style-declaration-item-and-skip-to-next (limit)
1725 "Match, and move over, any declaration/definition item after point.
1726 Matches after point, but ignores leading whitespace and `*' characters.
1727 Does not move further than LIMIT.
1729 The expected syntax of a declaration/definition item is `word', possibly ending
1730 with optional whitespace and a `('. Everything following the item (but
1731 belonging to it) is expected to by skip-able by `scan-sexps', and items are
1732 expected to be separated with a `,' and to be terminated with a `;'.
1734 Thus the regexp matches after point: word (
1735 ^^^^ ^
1736 Where the match subexpressions are: 1 2
1738 The item is delimited by (match-beginning 1) and (match-end 1).
1739 If (match-beginning 2) is non-nil, the item is followed by a `('.
1741 This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
1742 (when (looking-at "[ \t*]*\\(\\sw+\\)[ \t]*\\((\\)?")
1743 (save-match-data
1744 (condition-case nil
1745 (save-restriction
1746 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
1747 (narrow-to-region (point-min) limit)
1748 (goto-char (match-end 1))
1749 ;; Move over any item value, etc., to the next item.
1750 (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)"))
1751 (goto-char (or (scan-sexps (point) 1) (point-max))))
1752 (goto-char (match-end 2)))
1753 (error t)))))
1756 (defconst lisp-font-lock-keywords-1
1757 (eval-when-compile
1758 (list
1759 ;; Anything not a variable or type declaration is fontified as a function.
1760 ;; It would be cleaner to allow preceding whitespace, but it would also be
1761 ;; about five times slower.
1762 (list (concat "^(\\(def\\("
1763 ;; Variable declarations.
1764 "\\(const\\|custom\\|face\\|var\\)\\|"
1765 ;; Structure declarations.
1766 "\\(class\\|group\\|struct\\|type\\)\\|"
1767 ;; Everything else is a function declaration.
1768 "\\sw+"
1769 "\\)\\)\\>"
1770 ;; Any whitespace and declared object.
1771 "[ \t'\(]*"
1772 "\\(\\sw+\\)?")
1773 '(1 font-lock-keyword-face)
1774 '(5 (cond ((match-beginning 3) font-lock-variable-name-face)
1775 ((match-beginning 4) font-lock-type-face)
1776 (t font-lock-function-name-face))
1777 nil t))
1779 "Subdued level highlighting for Lisp modes.")
1781 (defconst lisp-font-lock-keywords-2
1782 (append lisp-font-lock-keywords-1
1783 (eval-when-compile
1784 (list
1786 ;; Control structures. Common ELisp and CLisp forms combined.
1787 ; (make-regexp
1788 ; '("cond" "if" "while" "let\\*?" "prog[nv12*]?" "inline" "catch" "throw"
1789 ; "save-restriction" "save-excursion" "save-window-excursion"
1790 ; "save-selected-window" "save-match-data" "save-current-buffer"
1791 ; "unwind-protect" "condition-case" "track-mouse" "dont-compile"
1792 ; "eval-after-load" "eval-and-compile" "eval-when-compile"
1793 ; "when" "unless" "do" "flet" "labels" "return" "return-from"
1794 ; "with-output-to-temp-buffer" "with-timeout" "with-current-buffer"
1795 ; "with-temp-buffer" "with-temp-file"))
1796 (cons (concat "(\\("
1797 "c\\(atch\\|ond\\(\\|ition-case\\)\\)\\|"
1798 "do\\(\\|nt-compile\\)\\|"
1799 "eval-\\(a\\(fter-load\\|nd-compile\\)\\|"
1800 "when-compile\\)\\|flet\\|i\\(f\\|nline\\)\\|"
1801 "l\\(abels\\|et\\*?\\)\\|prog[nv12*]?\\|"
1802 "return\\(\\|-from\\)\\|"
1803 "save-\\(current-buffer\\|excursion\\|match-data\\|"
1804 "restriction\\|selected-window\\|window-excursion\\)\\|"
1805 "t\\(hrow\\|rack-mouse\\)\\|un\\(less\\|wind-protect\\)\\|"
1806 "w\\(h\\(en\\|ile\\)\\|ith-\\(current-buffer\\|"
1807 "output-to-temp-buffer\\|"
1808 "t\\(emp-\\(buffer\\|file\\)\\|imeout\\)\\)\\)"
1809 "\\)\\>")
1812 ;; Feature symbols as references.
1813 '("(\\(featurep\\|provide\\|require\\)\\>[ \t']*\\(\\sw+\\)?"
1814 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1816 ;; Words inside \\[] tend to be for `substitute-command-keys'.
1817 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-reference-face prepend)
1819 ;; Words inside `' tend to be symbol names.
1820 '("`\\(\\sw\\sw+\\)'" 1 font-lock-reference-face prepend)
1822 ;; CLisp `:' keywords as builtins.
1823 '("\\<:\\sw\\sw+\\>" 0 font-lock-builtin-face)
1825 ;; ELisp and CLisp `&' keywords as types.
1826 '("\\<\\&\\sw+\\>" . font-lock-type-face)
1828 "Gaudy level highlighting for Lisp modes.")
1831 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
1832 "Default expressions to highlight in Lisp modes.")
1835 (defvar scheme-font-lock-keywords
1836 (eval-when-compile
1837 (list
1839 ;; Declarations. Hannes Haug <hannes.haug@student.uni-tuebingen.de> says
1840 ;; this works for SOS, STklos, SCOOPS, Meroon and Tiny CLOS.
1841 (list (concat "(\\(define\\("
1842 ;; Function names.
1843 "\\(\\|-\\(generic\\(\\|-procedure\\)\\|method\\)\\)\\|"
1844 ;; Macro names, as variable names. A bit dubious, this.
1845 "\\(-syntax\\)\\|"
1846 ;; Class names.
1847 "-class"
1848 "\\)\\)\\>"
1849 ;; Any whitespace and declared object.
1850 "[ \t]*(?"
1851 "\\(\\sw+\\)?")
1852 '(1 font-lock-keyword-face)
1853 '(7 (cond ((match-beginning 3) font-lock-function-name-face)
1854 ((match-beginning 6) font-lock-variable-name-face)
1855 (t font-lock-type-face))
1856 nil t))
1858 ;; Control structures.
1859 ;(make-regexp '("begin" "call-with-current-continuation" "call/cc"
1860 ; "call-with-input-file" "call-with-output-file" "case" "cond"
1861 ; "do" "else" "for-each" "if" "lambda"
1862 ; "let\\*?" "let-syntax" "letrec" "letrec-syntax"
1863 ; ;; Hannes Haug <hannes.haug@student.uni-tuebingen.de> wants:
1864 ; "and" "or" "delay"
1865 ; ;; Stefan Monnier <stefan.monnier@epfl.ch> says don't bother:
1866 ; ;;"quasiquote" "quote" "unquote" "unquote-splicing"
1867 ; "map" "syntax" "syntax-rules"))
1868 (cons
1869 (concat "(\\("
1870 "and\\|begin\\|c\\(a\\(ll\\(-with-\\(current-continuation\\|"
1871 "input-file\\|output-file\\)\\|/cc\\)\\|se\\)\\|ond\\)\\|"
1872 "d\\(elay\\|o\\)\\|else\\|for-each\\|if\\|"
1873 "l\\(ambda\\|et\\(-syntax\\|\\*?\\|rec\\(\\|-syntax\\)\\)\\)\\|"
1874 "map\\|or\\|syntax\\(\\|-rules\\)"
1875 "\\)\\>") 1)
1877 ;; David Fox <fox@graphics.cs.nyu.edu> for SOS/STklos class specifiers.
1878 '("\\<<\\sw+>\\>" . font-lock-type-face)
1880 ;; Scheme `:' keywords as references.
1881 '("\\<:\\sw+\\>" . font-lock-reference-face)
1883 "Default expressions to highlight in Scheme modes.")
1886 (defvar tex-font-lock-keywords
1887 ; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
1888 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
1889 ; 2 font-lock-function-name-face)
1890 ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
1891 ; 2 font-lock-reference-face)
1892 ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
1893 ; ;; not be able to display those fonts.
1894 ; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep)
1895 ; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep)
1896 ; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face)
1897 ; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep))
1898 ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>.
1899 '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
1900 2 font-lock-function-name-face)
1901 ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
1902 2 font-lock-reference-face)
1903 ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face)
1904 "\\\\\\([a-zA-Z@]+\\|.\\)"
1905 ;; It seems a bit dubious to use `bold' and `italic' faces since we might
1906 ;; not be able to display those fonts.
1907 ;; LaTeX2e: \emph{This is emphasized}.
1908 ("\\\\emph{\\([^}]+\\)}" 1 'italic keep)
1909 ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...}
1910 ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}"
1911 3 (if (match-beginning 2) 'bold 'italic) keep)
1912 ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for good tables.
1913 ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)"
1914 3 (if (match-beginning 2) 'bold 'italic) keep))
1915 "Default expressions to highlight in TeX modes.")
1917 ;;; User choices.
1919 ;; These provide a means to fontify types not defined by the language. Those
1920 ;; types might be the user's own or they might be generally accepted and used.
1921 ;; Generally excepted types are used to provide default variable values.
1923 (defvar c-font-lock-extra-types '("FILE" "\\sw+_t")
1924 "*List of extra types to fontify in C mode.
1925 Each list item should be a regexp without word-delimiters or parentheses.
1926 For example, a value of (\"FILE\" \"\\\\sw+_t\") means the word FILE and words
1927 ending in _t are treated as type names.")
1929 (defvar c++-font-lock-extra-types nil
1930 "*List of extra types to fontify in C++ mode.
1931 Each list item should be a regexp without word-delimiters or parentheses.
1932 For example, a value of (\"String\") means the word String is treated as a type
1933 name.")
1935 (defvar objc-font-lock-extra-types '("Class" "BOOL" "IMP" "SEL")
1936 "*List of extra types to fontify in Objective-C mode.
1937 Each list item should be a regexp without word-delimiters or parentheses.
1938 For example, a value of (\"Class\" \"BOOL\" \"IMP\" \"SEL\") means the words
1939 Class, BOOL, IMP and SEL are treated as type names.")
1941 (defvar java-font-lock-extra-types '("[A-Z\300-\326\330-\337]\\sw+")
1942 "*List of extra types to fontify in Java mode.
1943 Each list item should be a regexp without word-delimiters or parentheses.
1944 For example, a value of (\"[A-Z\300-\326\330-\337]\\\\sw+\") means capitalised
1945 words (and words conforming to the Java id spec) are treated as type names.")
1947 ;;; C.
1949 ;; [Murmur murmur murmur] Maestro, drum-roll please... [Murmur murmur murmur.]
1950 ;; Ahem. [Murmur murmur murmur] Lay-dees an Gennel-men. [Murmur murmur shhh!]
1951 ;; I am most proud and humbly honoured today [murmur murmur cough] to present
1952 ;; to you good people, the winner of the Second Millennium Award for The Most
1953 ;; Hairy Language Syntax. [Ahhh!] All rise please. [Shuffle shuffle
1954 ;; shuffle.] And a round of applause please. For... The C Language! [Roar.]
1956 ;; Thank you... You are too kind... It is with a feeling of great privilege
1957 ;; and indeed emotion [sob] that I accept this award. It has been a long hard
1958 ;; road. But we know our destiny. And our future. For we must not rest.
1959 ;; There are more tokens to overload, more shoehorn, more methodologies. But
1960 ;; more is a plus! [Ha ha ha.] And more means plus! [Ho ho ho.] The future
1961 ;; is C++! [Ohhh!] The Third Millennium Award... Will be ours! [Roar.]
1963 (defconst c-font-lock-keywords-1 nil
1964 "Subdued level highlighting for C mode.")
1966 (defconst c-font-lock-keywords-2 nil
1967 "Medium level highlighting for C mode.
1968 See also `c-font-lock-extra-types'.")
1970 (defconst c-font-lock-keywords-3 nil
1971 "Gaudy level highlighting for C mode.
1972 See also `c-font-lock-extra-types'.")
1974 (let ((c-keywords
1975 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while")
1976 "break\\|continue\\|do\\|else\\|for\\|if\\|return\\|switch\\|while")
1977 (c-type-types
1978 ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
1979 ; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
1980 ; "void" "volatile" "const")
1981 `(mapconcat 'identity
1982 (cons
1983 (,@ (concat "auto\\|c\\(har\\|onst\\)\\|double\\|" ; 6 ()s deep.
1984 "e\\(num\\|xtern\\)\\|float\\|int\\|long\\|register\\|"
1985 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|typedef\\|"
1986 "un\\(ion\\|signed\\)\\|vo\\(id\\|latile\\)"))
1987 c-font-lock-extra-types)
1988 "\\|"))
1990 (setq c-font-lock-keywords-1
1991 (list
1993 ;; These are all anchored at the beginning of line for speed.
1994 ;; Note that `c++-font-lock-keywords-1' depends on `c-font-lock-keywords-1'.
1996 ;; Fontify function name definitions (GNU style; without type on line).
1997 '("^\\(\\sw+\\)[ \t]*(" 1 font-lock-function-name-face)
1999 ;; Fontify error directives.
2000 '("^#[ \t]*error[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend)
2002 ;; Fontify filenames in #include <...> preprocessor directives as strings.
2003 '("^#[ \t]*\\(import\\|include\\)[ \t]+\\(<[^>\"\n]*>?\\)"
2004 2 font-lock-string-face)
2006 ;; Fontify function macro names.
2007 '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
2009 ;; Fontify symbol names in #elif or #if ... defined preprocessor directives.
2010 '("^#[ \t]*\\(elif\\|if\\)\\>"
2011 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
2012 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t)))
2014 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
2015 '("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?"
2016 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t))
2019 (setq c-font-lock-keywords-2
2020 (append c-font-lock-keywords-1
2021 (list
2023 ;; Simple regexps for speed.
2025 ;; Fontify all type specifiers.
2026 `(eval .
2027 (cons (concat "\\<\\(" (,@ c-type-types) "\\)\\>") 'font-lock-type-face))
2029 ;; Fontify all builtin keywords (except case, default and goto; see below).
2030 (concat "\\<\\(" c-keywords "\\)\\>")
2032 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2033 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2034 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2035 ;; Anders Lindgren <andersl@csd.uu.se> points out that it is quicker to use
2036 ;; MATCH-ANCHORED to effectively anchor the regexp on the left.
2037 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:"
2038 (beginning-of-line) (end-of-line)
2039 (1 font-lock-reference-face)))
2042 (setq c-font-lock-keywords-3
2043 (append c-font-lock-keywords-2
2045 ;; More complicated regexps for more complete highlighting for types.
2046 ;; We still have to fontify type specifiers individually, as C is so hairy.
2047 (list
2049 ;; Fontify all storage classes and type specifiers, plus their items.
2050 `(eval .
2051 (list (concat "\\<\\(" (,@ c-type-types) "\\)\\>"
2052 "\\([ \t*&]+\\sw+\\>\\)*")
2053 ;; Fontify each declaration item.
2054 '(font-lock-match-c-style-declaration-item-and-skip-to-next
2055 ;; Start with point after all type specifiers.
2056 (goto-char (or (match-beginning 8) (match-end 1)))
2057 ;; Finish with point after first type specifier.
2058 (goto-char (match-end 1))
2059 ;; Fontify as a variable or function name.
2060 (1 (if (match-beginning 2)
2061 font-lock-function-name-face
2062 font-lock-variable-name-face)))))
2064 ;; Fontify structures, or typedef names, plus their items.
2065 '("\\(}\\)[ \t*]*\\sw"
2066 (font-lock-match-c-style-declaration-item-and-skip-to-next
2067 (goto-char (match-end 1)) nil
2068 (1 (if (match-beginning 2)
2069 font-lock-function-name-face
2070 font-lock-variable-name-face))))
2072 ;; Fontify anything at beginning of line as a declaration or definition.
2073 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2074 (1 font-lock-type-face)
2075 (font-lock-match-c-style-declaration-item-and-skip-to-next
2076 (goto-char (or (match-beginning 2) (match-end 1))) nil
2077 (1 (if (match-beginning 2)
2078 font-lock-function-name-face
2079 font-lock-variable-name-face))))
2083 (defvar c-font-lock-keywords c-font-lock-keywords-1
2084 "Default expressions to highlight in C mode.
2085 See also `c-font-lock-extra-types'.")
2087 ;;; C++.
2089 (defconst c++-font-lock-keywords-1 nil
2090 "Subdued level highlighting for C++ mode.")
2092 (defconst c++-font-lock-keywords-2 nil
2093 "Medium level highlighting for C++ mode.
2094 See also `c++-font-lock-extra-types'.")
2096 (defconst c++-font-lock-keywords-3 nil
2097 "Gaudy level highlighting for C++ mode.
2098 See also `c++-font-lock-extra-types'.")
2100 (defun font-lock-match-c++-style-declaration-item-and-skip-to-next (limit)
2101 ;; Regexp matches after point: word<word>::word (
2102 ;; ^^^^ ^^^^ ^^^^ ^
2103 ;; Where the match subexpressions are: 1 3 5 6
2105 ;; Item is delimited by (match-beginning 1) and (match-end 1).
2106 ;; If (match-beginning 3) is non-nil, that part of the item incloses a `<>'.
2107 ;; If (match-beginning 5) is non-nil, that part of the item follows a `::'.
2108 ;; If (match-beginning 6) is non-nil, the item is followed by a `('.
2109 (when (looking-at (eval-when-compile
2110 (concat "[ \t*&]*\\(\\sw+\\)"
2111 "\\(<\\(\\sw+\\)[ \t*&]*>\\)?"
2112 "\\(::\\*?\\(\\sw+\\)\\)?"
2113 "[ \t]*\\((\\)?")))
2114 (save-match-data
2115 (condition-case nil
2116 (save-restriction
2117 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
2118 (narrow-to-region (point-min) limit)
2119 (goto-char (match-end 1))
2120 ;; Move over any item value, etc., to the next item.
2121 (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)"))
2122 (goto-char (or (scan-sexps (point) 1) (point-max))))
2123 (goto-char (match-end 2)))
2124 (error t)))))
2126 (let* ((c++-keywords
2127 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while"
2128 ; "asm" "catch" "delete" "new" "operator" "sizeof" "this" "throw" "try"
2129 ; ;; Eric Hopper <hopper@omnifarious.mn.org> says these are new.
2130 ; "static_cast" "dynamic_cast" "const_cast" "reinterpret_cast")
2131 (concat "asm\\|break\\|c\\(atch\\|on\\(st_cast\\|tinue\\)\\)\\|"
2132 "d\\(elete\\|o\\|ynamic_cast\\)\\|else\\|for\\|if\\|new\\|"
2133 "operator\\|re\\(interpret_cast\\|turn\\)\\|"
2134 "s\\(izeof\\|tatic_cast\\|"
2135 "witch\\)\\|t\\(h\\(is\\|row\\)\\|ry\\)\\|while"))
2136 (c++-operators
2137 (mapconcat 'identity
2138 (mapcar 'regexp-quote
2139 ;; Taken from Stroustrup, minus keywords otherwise fontified.
2140 (sort '("+" "-" "*" "/" "%" "^" "&" "|" "~" "!" "=" "<" ">"
2141 "+=" "-=" "*=" "/=" "%=" "^=" "&=" "|=" "<<" ">>"
2142 ">>=" "<<=" "==" "!=" "<=" ">=" "&&" "||" "++" "--"
2143 "->*" "," "->" "[]" "()")
2144 #'(lambda (a b) (> (length a) (length b)))))
2145 "\\|"))
2146 (c++-type-types
2147 ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
2148 ; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
2149 ; "void" "volatile" "const" "inline" "friend" "bool"
2150 ; "virtual" "complex" "template"
2151 ; ;; Eric Hopper <hopper@omnifarious.mn.org> says these are new.
2152 ; "namespace" "using")
2153 `(mapconcat 'identity
2154 (cons
2155 (,@ (concat "auto\\|bool\\|c\\(har\\|o\\(mplex\\|nst\\)\\)\\|"
2156 "double\\|e\\(num\\|xtern\\)\\|f\\(loat\\|riend\\)\\|"
2157 "in\\(line\\|t\\)\\|long\\|namespace\\|register\\|"
2158 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|"
2159 "t\\(emplate\\|ypedef\\)\\|"
2160 "u\\(n\\(ion\\|signed\\)\\|sing\\)\\|"
2161 "v\\(irtual\\|o\\(id\\|latile\\)\\)")) ; 12 ()s deep.
2162 c++-font-lock-extra-types)
2163 "\\|"))
2164 (c++-type-suffix "\\(<\\(\\sw+\\)[ \t*&]*>\\)?\\(::\\*?\\(\\sw+\\)\\)?")
2165 (c++-type-spec (concat "\\(\\sw+\\)\\>" c++-type-suffix))
2167 (setq c++-font-lock-keywords-1
2168 (append
2170 ;; The list `c-font-lock-keywords-1' less that for function names.
2171 (cdr c-font-lock-keywords-1)
2172 (list
2174 ;; Class names etc.
2175 (list (concat "\\<\\(class\\|public\\|private\\|protected\\)\\>[ \t]*"
2176 "\\(" c++-type-spec "\\)?")
2177 '(1 font-lock-type-face)
2178 '(3 (if (match-beginning 6)
2179 font-lock-type-face
2180 font-lock-function-name-face) nil t)
2181 '(5 font-lock-function-name-face nil t)
2182 '(7 font-lock-function-name-face nil t))
2184 ;; Fontify function name definitions, possibly incorporating class names.
2185 (list (concat "^" c++-type-spec "[ \t]*(")
2186 '(1 (if (or (match-beginning 2) (match-beginning 4))
2187 font-lock-type-face
2188 font-lock-function-name-face))
2189 '(3 font-lock-function-name-face nil t)
2190 '(5 font-lock-function-name-face nil t))
2193 (setq c++-font-lock-keywords-2
2194 (append c++-font-lock-keywords-1
2195 (list
2197 ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading.
2198 `(eval .
2199 (cons (concat "\\<\\(" (,@ c++-type-types) "\\)\\>")
2200 'font-lock-type-face))
2202 ;; Fontify operator overloading.
2203 (list (concat "\\<\\(operator\\)\\>[ \t]*\\(" c++-operators "\\)?")
2204 '(1 font-lock-keyword-face)
2205 '(2 font-lock-builtin-face nil t))
2207 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2208 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2209 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2210 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:\\($\\|[^:]\\)"
2211 (beginning-of-line) (end-of-line)
2212 (1 font-lock-reference-face)))
2214 ;; Fontify other builtin keywords.
2215 (cons (concat "\\<\\(" c++-keywords "\\)\\>") 'font-lock-keyword-face)
2217 ;; Eric Hopper <hopper@omnifarious.mn.org> says `true' and `false' are new.
2218 '("\\<\\(false\\|true\\)\\>" . font-lock-reference-face)
2221 (setq c++-font-lock-keywords-3
2222 (append c++-font-lock-keywords-2
2224 ;; More complicated regexps for more complete highlighting for types.
2225 (list
2227 ;; Fontify all storage classes and type specifiers, plus their items.
2228 `(eval .
2229 (list (concat "\\<\\(" (,@ c++-type-types) "\\)\\>" (,@ c++-type-suffix)
2230 "\\([ \t*&]+" (,@ c++-type-spec) "\\)*")
2231 ;; Fontify each declaration item.
2232 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
2233 ;; Start with point after all type specifiers.
2234 (goto-char (or (match-beginning 18) (match-end 1)))
2235 ;; Finish with point after first type specifier.
2236 (goto-char (match-end 1))
2237 ;; Fontify as a variable or function name.
2238 (1 (cond ((or (match-beginning 2) (match-beginning 4))
2239 font-lock-type-face)
2240 ((match-beginning 6) font-lock-function-name-face)
2241 (t font-lock-variable-name-face)))
2242 (3 font-lock-function-name-face nil t)
2243 (5 (if (match-beginning 6)
2244 font-lock-function-name-face
2245 font-lock-variable-name-face) nil t))))
2247 ;; Fontify structures, or typedef names, plus their items.
2248 '("\\(}\\)[ \t*]*\\sw"
2249 (font-lock-match-c++-style-declaration-item-and-skip-to-next
2250 (goto-char (match-end 1)) nil
2251 (1 (if (match-beginning 6)
2252 font-lock-function-name-face
2253 font-lock-variable-name-face))))
2255 ;; Fontify anything at beginning of line as a declaration or definition.
2256 (list (concat "^\\(" c++-type-spec "[ \t*&]*\\)+")
2257 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
2258 (goto-char (match-beginning 1))
2259 (goto-char (match-end 1))
2260 (1 (cond ((or (match-beginning 2) (match-beginning 4))
2261 font-lock-type-face)
2262 ((match-beginning 6) font-lock-function-name-face)
2263 (t font-lock-variable-name-face)))
2264 (3 font-lock-function-name-face nil t)
2265 (5 (if (match-beginning 6)
2266 font-lock-function-name-face
2267 font-lock-variable-name-face) nil t)))
2271 (defvar c++-font-lock-keywords c++-font-lock-keywords-1
2272 "Default expressions to highlight in C++ mode.
2273 See also `c++-font-lock-extra-types'.")
2275 ;;; Objective-C.
2277 (defconst objc-font-lock-keywords-1 nil
2278 "Subdued level highlighting for Objective-C mode.")
2280 (defconst objc-font-lock-keywords-2 nil
2281 "Medium level highlighting for Objective-C mode.
2282 See also `objc-font-lock-extra-types'.")
2284 (defconst objc-font-lock-keywords-3 nil
2285 "Gaudy level highlighting for Objective-C mode.
2286 See also `objc-font-lock-extra-types'.")
2288 ;; Regexps written with help from Stephen Peters <speters@us.oracle.com> and
2289 ;; Jacques Duthen Prestataire <duthen@cegelec-red.fr>.
2290 (let ((objc-keywords
2291 ; (make-regexp
2292 ; '("break" "continue" "do" "else" "for" "if" "return" "switch" "while"
2293 ; "sizeof" "self" "super"))
2294 (concat "break\\|continue\\|do\\|else\\|for\\|if\\|return\\|"
2295 "s\\(elf\\|izeof\\|uper\\|witch\\)\\|while"))
2296 (objc-type-types
2297 `(mapconcat 'identity
2298 (cons
2299 ; '("auto" "extern" "register" "static" "typedef" "struct" "union"
2300 ; "enum" "signed" "unsigned" "short" "long" "int" "char"
2301 ; "float" "double" "void" "volatile" "const"
2302 ; "id" "oneway" "in" "out" "inout" "bycopy" "byref")
2303 (,@ (concat "auto\\|by\\(copy\\|ref\\)\\|c\\(har\\|onst\\)\\|"
2304 "double\\|e\\(num\\|xtern\\)\\|float\\|"
2305 "i\\([dn]\\|n\\(out\\|t\\)\\)\\|long\\|"
2306 "o\\(neway\\|ut\\)\\|register\\|s\\(hort\\|igned\\|"
2307 "t\\(atic\\|ruct\\)\\)\\|typedef\\|"
2308 "un\\(ion\\|signed\\)\\|vo\\(id\\|latile\\)"))
2309 objc-font-lock-extra-types)
2310 "\\|"))
2312 (setq objc-font-lock-keywords-1
2313 (append
2315 ;; The list `c-font-lock-keywords-1' less that for function names.
2316 (cdr c-font-lock-keywords-1)
2317 (list
2319 ;; Fontify compiler directives.
2320 '("@\\(\\sw+\\)\\>"
2321 (1 font-lock-keyword-face)
2322 ("\\=[ \t:<(,]*\\(\\sw+\\)" nil nil
2323 (1 font-lock-function-name-face)))
2325 ;; Fontify method names and arguments. Oh Lordy!
2326 ;; First, on the same line as the function declaration.
2327 '("^[+-][ \t]*\\(PRIVATE\\)?[ \t]*\\((\\([^)\n]+\\))\\)?[ \t]*\\(\\sw+\\)"
2328 (1 font-lock-type-face nil t)
2329 (3 font-lock-type-face nil t)
2330 (4 font-lock-function-name-face)
2331 ("\\=[ \t]*\\(\\sw+\\)?:[ \t]*\\((\\([^)\n]+\\))\\)?[ \t]*\\(\\sw+\\)"
2332 nil nil
2333 (1 font-lock-function-name-face nil t)
2334 (3 font-lock-type-face nil t)
2335 (4 font-lock-variable-name-face)))
2336 ;; Second, on lines following the function declaration.
2337 '(":" ("^[ \t]*\\(\\sw+\\)?:[ \t]*\\((\\([^)\n]+\\))\\)?[ \t]*\\(\\sw+\\)"
2338 (beginning-of-line) (end-of-line)
2339 (1 font-lock-function-name-face nil t)
2340 (3 font-lock-type-face nil t)
2341 (4 font-lock-variable-name-face)))
2344 (setq objc-font-lock-keywords-2
2345 (append objc-font-lock-keywords-1
2346 (list
2348 ;; Simple regexps for speed.
2350 ;; Fontify all type specifiers.
2351 `(eval .
2352 (cons (concat "\\<\\(" (,@ objc-type-types) "\\)\\>")
2353 'font-lock-type-face))
2355 ;; Fontify all builtin keywords (except case, default and goto; see below).
2356 (concat "\\<\\(" objc-keywords "\\)\\>")
2358 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2359 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2360 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2361 ;; Fontify tags iff sole statement on line, otherwise we detect selectors.
2362 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2363 (beginning-of-line) (end-of-line)
2364 (1 font-lock-reference-face)))
2366 ;; Fontify null object pointers.
2367 '("\\<\\(Nil\\|nil\\)\\>" 1 font-lock-reference-face)
2370 (setq objc-font-lock-keywords-3
2371 (append objc-font-lock-keywords-2
2373 ;; More complicated regexps for more complete highlighting for types.
2374 ;; We still have to fontify type specifiers individually, as C is so hairy.
2375 (list
2377 ;; Fontify all storage classes and type specifiers, plus their items.
2378 `(eval .
2379 (list (concat "\\<\\(" (,@ objc-type-types) "\\)\\>"
2380 "\\([ \t*&]+\\sw+\\>\\)*")
2381 ;; Fontify each declaration item.
2382 '(font-lock-match-c-style-declaration-item-and-skip-to-next
2383 ;; Start with point after all type specifiers.
2384 (goto-char (or (match-beginning 2) (match-end 1)))
2385 ;; Finish with point after first type specifier.
2386 (goto-char (match-end 1))
2387 ;; Fontify as a variable or function name.
2388 (1 (if (match-beginning 2)
2389 font-lock-function-name-face
2390 font-lock-variable-name-face)))))
2392 ;; Fontify structures, or typedef names, plus their items.
2393 '("\\(}\\)[ \t*]*\\sw"
2394 (font-lock-match-c-style-declaration-item-and-skip-to-next
2395 (goto-char (match-end 1)) nil
2396 (1 (if (match-beginning 2)
2397 font-lock-function-name-face
2398 font-lock-variable-name-face))))
2400 ;; Fontify anything at beginning of line as a declaration or definition.
2401 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2402 (1 font-lock-type-face)
2403 (font-lock-match-c-style-declaration-item-and-skip-to-next
2404 (goto-char (or (match-beginning 2) (match-end 1))) nil
2405 (1 (if (match-beginning 2)
2406 font-lock-function-name-face
2407 font-lock-variable-name-face))))
2411 (defvar objc-font-lock-keywords objc-font-lock-keywords-1
2412 "Default expressions to highlight in Objective-C mode.
2413 See also `objc-font-lock-extra-types'.")
2415 ;;; Java.
2417 (defconst java-font-lock-keywords-1 nil
2418 "Subdued level highlighting for Java mode.")
2420 (defconst java-font-lock-keywords-2 nil
2421 "Medium level highlighting for Java mode.
2422 See also `java-font-lock-extra-types'.")
2424 (defconst java-font-lock-keywords-3 nil
2425 "Gaudy level highlighting for Java mode.
2426 See also `java-font-lock-extra-types'.")
2428 ;; Regexps written with help from Fred White <fwhite@bbn.com> and
2429 ;; Anders Lindgren <andersl@csd.uu.se>.
2430 (let ((java-keywords
2431 (concat "\\<\\("
2432 ; (make-regexp
2433 ; '("catch" "do" "else" "super" "this" "finally" "for" "if"
2434 ;; ;; Anders Lindgren <andersl@csd.uu.se> says these have gone.
2435 ;; "cast" "byvalue" "future" "generic" "operator" "var"
2436 ;; "inner" "outer" "rest"
2437 ; "interface" "return" "switch" "throw" "try" "while")
2438 "catch\\|do\\|else\\|f\\(inally\\|or\\)\\|"
2439 "i\\(f\\|nterface\\)\\|return\\|s\\(uper\\|witch\\)\\|"
2440 "t\\(h\\(is\\|row\\)\\|ry\\)\\|while"
2441 "\\)\\>"))
2443 ;; These are immediately followed by an object name.
2444 (java-minor-types
2445 (mapconcat 'identity
2446 '("boolean" "char" "byte" "short" "int" "long" "float" "double" "void")
2447 "\\|"))
2449 ;; These are eventually followed by an object name.
2450 (java-major-types
2451 ; (make-regexp
2452 ; '("abstract" "const" "final" "synchronized" "transient" "static"
2453 ;; ;; Anders Lindgren <andersl@csd.uu.se> says this has gone.
2454 ;; "threadsafe"
2455 ; "volatile" "public" "private" "protected" "native")
2456 (concat "abstract\\|const\\|final\\|native\\|"
2457 "p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|"
2458 "s\\(tatic\\|ynchronized\\)\\|transient\\|volatile"))
2460 ;; Random types immediately followed by an object name.
2461 (java-other-types
2462 '(mapconcat 'identity (cons "\\sw+\\.\\sw+" java-font-lock-extra-types)
2463 "\\|"))
2465 (setq java-font-lock-keywords-1
2466 (list
2468 ;; Fontify class names.
2469 '("\\<\\(class\\)\\>[ \t]*\\(\\sw+\\)?"
2470 (1 font-lock-type-face) (2 font-lock-function-name-face nil t))
2472 ;; Fontify package names in import directives.
2473 '("\\<\\(import\\|package\\)\\>[ \t]*\\(\\sw+\\)?"
2474 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2477 (setq java-font-lock-keywords-2
2478 (append java-font-lock-keywords-1
2479 (list
2481 ;; Fontify all builtin type specifiers.
2482 (cons (concat "\\<\\(" java-minor-types "\\|" java-major-types "\\)\\>")
2483 'font-lock-type-face)
2485 ;; Fontify all builtin keywords (except below).
2486 (concat "\\<\\(" java-keywords "\\)\\>")
2488 ;; Fontify keywords and targets, and case default/goto tags.
2489 (list "\\<\\(break\\|case\\|continue\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2490 '(1 font-lock-keyword-face) '(2 font-lock-reference-face nil t))
2491 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:"
2492 (beginning-of-line) (end-of-line)
2493 (1 font-lock-reference-face)))
2495 ;; Fontify keywords and types; the first can be followed by a type list.
2496 (list (concat "\\<\\("
2497 "implements\\|throws\\|"
2498 "\\(extends\\|instanceof\\|new\\)"
2499 "\\)\\>[ \t]*\\(\\sw+\\)?")
2500 '(1 font-lock-keyword-face) '(3 font-lock-type-face nil t)
2501 '("\\=[ \t]*,[ \t]*\\(\\sw+\\)"
2502 (if (match-beginning 2) (goto-char (match-end 2))) nil
2503 (1 font-lock-type-face)))
2505 ;; Fontify all constants.
2506 '("\\<\\(false\\|null\\|true\\)\\>" . font-lock-reference-face)
2508 ;; Javadoc tags within comments.
2509 '("@\\(author\\|exception\\|return\\|see\\|version\\)\\>"
2510 (1 font-lock-reference-face prepend))
2511 '("@\\(param\\)\\>[ \t]*\\(\\sw+\\)?"
2512 (1 font-lock-reference-face prepend)
2513 (2 font-lock-variable-name-face prepend t))
2516 (setq java-font-lock-keywords-3
2517 (append java-font-lock-keywords-2
2519 ;; More complicated regexps for more complete highlighting for types.
2520 ;; We still have to fontify type specifiers individually, as Java is hairy.
2521 (list
2523 ;; Fontify random types in casts.
2524 `(eval .
2525 (list (concat "(\\(" (,@ java-other-types) "\\))"
2526 "[ \t]*\\(\\sw\\|[\"\(]\\)")
2527 ;; Fontify the type name.
2528 '(1 font-lock-type-face)))
2530 ;; Fontify random types immediately followed by an item or items.
2531 `(eval .
2532 (list (concat "\\<\\(" (,@ java-other-types) "\\)\\>"
2533 "\\([ \t]*\\[[ \t]*\\]\\)*"
2534 "[ \t]*\\sw")
2535 ;; Fontify the type name.
2536 '(1 font-lock-type-face)))
2537 `(eval .
2538 (list (concat "\\<\\(" (,@ java-other-types) "\\)\\>"
2539 "\\([ \t]*\\[[ \t]*\\]\\)*"
2540 "\\([ \t]*\\sw\\)")
2541 ;; Fontify each declaration item.
2542 '(font-lock-match-c-style-declaration-item-and-skip-to-next
2543 ;; Start and finish with point after the type specifier.
2544 (goto-char (match-beginning 3)) (goto-char (match-beginning 3))
2545 ;; Fontify as a variable or function name.
2546 (1 (if (match-beginning 2)
2547 font-lock-function-name-face
2548 font-lock-variable-name-face)))))
2550 ;; Fontify those that are immediately followed by an item or items.
2551 (list (concat "\\<\\(" java-minor-types "\\)\\>"
2552 "\\([ \t]*\\[[ \t]*\\]\\)*")
2553 ;; Fontify each declaration item.
2554 '(font-lock-match-c-style-declaration-item-and-skip-to-next
2555 ;; Start and finish with point after the type specifier.
2556 nil (goto-char (match-end 0))
2557 ;; Fontify as a variable or function name.
2558 (1 (if (match-beginning 2)
2559 font-lock-function-name-face
2560 font-lock-variable-name-face))))
2562 ;; Fontify those that are eventually followed by an item or items.
2563 (list (concat "\\<\\(" java-major-types "\\)\\>"
2564 "\\([ \t]+\\sw+\\>"
2565 "\\([ \t]*\\[[ \t]*\\]\\)*"
2566 "\\)*")
2567 ;; Fontify each declaration item.
2568 '(font-lock-match-c-style-declaration-item-and-skip-to-next
2569 ;; Start with point after all type specifiers.
2570 (goto-char (or (match-beginning 2) (match-end 1)))
2571 ;; Finish with point after first type specifier.
2572 (goto-char (match-end 1))
2573 ;; Fontify as a variable or function name.
2574 (1 (if (match-beginning 2)
2575 font-lock-function-name-face
2576 font-lock-variable-name-face))))
2580 (defvar java-font-lock-keywords java-font-lock-keywords-1
2581 "Default expressions to highlight in Java mode.
2582 See also `java-font-lock-extra-types'.")
2584 ;; Install ourselves:
2586 (unless (assq 'font-lock-mode minor-mode-alist)
2587 (push '(font-lock-mode " Font") minor-mode-alist))
2589 ;; Provide ourselves:
2591 (provide 'font-lock)
2593 ;;; font-lock.el ends here