Various docstring and commentary fixes, including
[emacs.git] / lisp / font-lock.el
blobcb72a6bc36e07f781d06ff45aa61aacc145b2118
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.org>
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. There are actually
64 ;; three passes; (a) the syntactic keyword pass, (b) the syntactic pass and (c)
65 ;; the keyword pass. Confused?
67 ;; The syntactic keyword pass places `syntax-table' text properties in the
68 ;; buffer according to the variable `font-lock-syntactic-keywords'. It is
69 ;; necessary because Emacs' syntax table is not powerful enough to describe all
70 ;; the different syntactic constructs required by the sort of people who decide
71 ;; that a single quote can be syntactic or not depending on the time of day.
72 ;; (What sort of person could decide to overload the meaning of a quote?)
73 ;; Obviously the syntactic keyword pass must occur before the syntactic pass.
75 ;; The syntactic pass places `face' text properties in the buffer according to
76 ;; syntactic context, i.e., according to the buffer's syntax table and buffer
77 ;; text's `syntax-table' text properties. It involves using a syntax parsing
78 ;; function to determine the context of different parts of a region of text. A
79 ;; syntax parsing function is necessary because generally strings and/or
80 ;; comments can span lines, and so the context of a given region is not
81 ;; necessarily apparent from the content of that region. Because the keyword
82 ;; pass only works within a given region, it is not generally appropriate for
83 ;; syntactic fontification. This is the first fontification pass that makes
84 ;; changes visible to the user; it fontifies strings and comments.
86 ;; The keyword pass places `face' text properties in the buffer according to
87 ;; the variable `font-lock-keywords'. It involves searching for given regexps
88 ;; (or calling given search functions) within the given region. This is the
89 ;; second fontification pass that makes changes visible to the user; it
90 ;; fontifies language reserved words, etc.
92 ;; Oh, and the answer is, "Yes, obviously just about everything should be done
93 ;; in a single syntactic pass, but the only syntactic parser available
94 ;; understands only strings and comments." Perhaps one day someone will write
95 ;; some syntactic parsers for common languages and a son-of-font-lock.el could
96 ;; use them rather then relying so heavily on the keyword (regexp) pass.
98 ;;; How Font Lock mode supports modes or is supported by modes:
100 ;; Modes that support Font Lock mode do so by defining one or more variables
101 ;; whose values specify the fontification. Font Lock mode knows of these
102 ;; variable names from (a) the buffer local variable `font-lock-defaults', if
103 ;; non-nil, or (b) the global variable `font-lock-defaults-alist', if the major
104 ;; mode has an entry. (Font Lock mode is set up via (a) where a mode's
105 ;; patterns are distributed with the mode's package library, and (b) where a
106 ;; mode's patterns are distributed with font-lock.el itself. An example of (a)
107 ;; is Pascal mode, an example of (b) is Lisp mode. Normally, the mechanism is
108 ;; (a); (b) is used where it is not clear which package library should contain
109 ;; the pattern definitions.) Font Lock mode chooses which variable to use for
110 ;; fontification based on `font-lock-maximum-decoration'.
112 ;; Font Lock mode fontification behaviour can be modified in a number of ways.
113 ;; See the below comments and the comments distributed throughout this file.
115 ;;; Constructing patterns:
117 ;; See the documentation for the variable `font-lock-keywords'.
119 ;; Efficient regexps for use as MATCHERs for `font-lock-keywords' and
120 ;; `font-lock-syntactic-keywords' can be generated via the function
121 ;; `regexp-opt', and their depth counted via the function `regexp-opt-depth'.
123 ;;; Adding patterns for modes that already support Font Lock:
125 ;; Though Font Lock highlighting patterns already exist for many modes, it's
126 ;; likely there's something that you want fontified that currently isn't, even
127 ;; at the maximum fontification level. You can add highlighting patterns via
128 ;; `font-lock-add-keywords'. For example, say in some C
129 ;; header file you #define the token `and' to expand to `&&', etc., to make
130 ;; your C code almost readable. In your ~/.emacs there could be:
132 ;; (font-lock-add-keywords 'c-mode '("\\<\\(and\\|or\\|not\\)\\>"))
134 ;; Some modes provide specific ways to modify patterns based on the values of
135 ;; other variables. For example, additional C types can be specified via the
136 ;; variable `c-font-lock-extra-types'.
138 ;;; Adding patterns for modes that do not support Font Lock:
140 ;; Not all modes support Font Lock mode. If you (as a user of the mode) add
141 ;; patterns for a new mode, you must define in your ~/.emacs a variable or
142 ;; variables that specify regexp fontification. Then, you should indicate to
143 ;; Font Lock mode, via the mode hook setting `font-lock-defaults', exactly what
144 ;; support is required. For example, say Foo mode should have the following
145 ;; regexps fontified case-sensitively, and comments and strings should not be
146 ;; fontified automagically. In your ~/.emacs there could be:
148 ;; (defvar foo-font-lock-keywords
149 ;; '(("\\<\\(one\\|two\\|three\\)\\>" . font-lock-keyword-face)
150 ;; ("\\<\\(four\\|five\\|six\\)\\>" . font-lock-type-face))
151 ;; "Default expressions to highlight in Foo mode.")
153 ;; (add-hook 'foo-mode-hook
154 ;; (function (lambda ()
155 ;; (make-local-variable 'font-lock-defaults)
156 ;; (setq font-lock-defaults '(foo-font-lock-keywords t)))))
158 ;;; Adding Font Lock support for modes:
160 ;; Of course, it would be better that the mode already supports Font Lock mode.
161 ;; The package author would do something similar to above. The mode must
162 ;; define at the top-level a variable or variables that specify regexp
163 ;; fontification. Then, the mode command should indicate to Font Lock mode,
164 ;; via `font-lock-defaults', exactly what support is required. For example,
165 ;; say Bar mode should have the following regexps fontified case-insensitively,
166 ;; and comments and strings should be fontified automagically. In bar.el there
167 ;; could be:
169 ;; (defvar bar-font-lock-keywords
170 ;; '(("\\<\\(uno\\|due\\|tre\\)\\>" . font-lock-keyword-face)
171 ;; ("\\<\\(quattro\\|cinque\\|sei\\)\\>" . font-lock-type-face))
172 ;; "Default expressions to highlight in Bar mode.")
174 ;; and within `bar-mode' there could be:
176 ;; (make-local-variable 'font-lock-defaults)
177 ;; (setq font-lock-defaults '(bar-font-lock-keywords nil t))
179 ;; What is fontification for? You might say, "It's to make my code look nice."
180 ;; I think it should be for adding information in the form of cues. These cues
181 ;; should provide you with enough information to both (a) distinguish between
182 ;; different items, and (b) identify the item meanings, without having to read
183 ;; the items and think about it. Therefore, fontification allows you to think
184 ;; less about, say, the structure of code, and more about, say, why the code
185 ;; doesn't work. Or maybe it allows you to think less and drift off to sleep.
187 ;; So, here are my opinions/advice/guidelines:
189 ;; - Highlight conceptual objects, such as function and variable names, and
190 ;; different objects types differently, i.e., (a) and (b) above, highlight
191 ;; function names differently to variable names.
192 ;; - Keep the faces distinct from each other as far as possible.
193 ;; i.e., (a) above.
194 ;; - Use the same face for the same conceptual object, across all modes.
195 ;; i.e., (b) above, all modes that have items that can be thought of as, say,
196 ;; keywords, should be highlighted with the same face, etc.
197 ;; - Make the face attributes fit the concept as far as possible.
198 ;; i.e., function names might be a bold colour such as blue, comments might
199 ;; be a bright colour such as red, character strings might be brown, because,
200 ;; err, strings are brown (that was not the reason, please believe me).
201 ;; - Don't use a non-nil OVERRIDE unless you have a good reason.
202 ;; Only use OVERRIDE for special things that are easy to define, such as the
203 ;; way `...' quotes are treated in strings and comments in Emacs Lisp mode.
204 ;; Don't use it to, say, highlight keywords in commented out code or strings.
205 ;; - Err, that's it.
207 ;;; Code:
209 ;; Define core `font-lock' group.
210 (defgroup font-lock nil
211 "Font Lock mode text highlighting package."
212 :link '(custom-manual "(emacs)Font Lock")
213 :group 'faces)
215 (defgroup font-lock-highlighting-faces nil
216 "Faces for highlighting text."
217 :prefix "font-lock-"
218 :group 'font-lock)
220 (defgroup font-lock-extra-types nil
221 "Extra mode-specific type names for highlighting declarations."
222 :group 'font-lock)
224 ;; Define support mode groups here to impose `font-lock' group order.
225 (defgroup fast-lock nil
226 "Font Lock support mode to cache fontification."
227 :link '(custom-manual "(emacs)Support Modes")
228 :load 'fast-lock
229 :group 'font-lock)
231 (defgroup lazy-lock nil
232 "Font Lock support mode to fontify lazily."
233 :link '(custom-manual "(emacs)Support Modes")
234 :load 'lazy-lock
235 :group 'font-lock)
237 ;; User variables.
239 (defcustom font-lock-maximum-size (* 250 1024)
240 "*Maximum size of a buffer for buffer fontification.
241 Only buffers less than this can be fontified when Font Lock mode is turned on.
242 If nil, means size is irrelevant.
243 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
244 where MAJOR-MODE is a symbol or t (meaning the default). For example:
245 ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576))
246 means that the maximum size is 250K for buffers in C or C++ modes, one megabyte
247 for buffers in Rmail mode, and size is irrelevant otherwise."
248 :type '(choice (const :tag "none" nil)
249 (integer :tag "size")
250 (repeat :menu-tag "mode specific" :tag "mode specific"
251 :value ((t . nil))
252 (cons :tag "Instance"
253 (radio :tag "Mode"
254 (const :tag "all" t)
255 (symbol :tag "name"))
256 (radio :tag "Size"
257 (const :tag "none" nil)
258 (integer :tag "size")))))
259 :group 'font-lock)
261 (defcustom font-lock-maximum-decoration t
262 "*Maximum decoration level for fontification.
263 If nil, use the default decoration (typically the minimum available).
264 If t, use the maximum decoration available.
265 If a number, use that level of decoration (or if not available the maximum).
266 If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
267 where MAJOR-MODE is a symbol or t (meaning the default). For example:
268 ((c-mode . t) (c++-mode . 2) (t . 1))
269 means use the maximum decoration available for buffers in C mode, level 2
270 decoration for buffers in C++ mode, and level 1 decoration otherwise."
271 :type '(choice (const :tag "default" nil)
272 (const :tag "maximum" t)
273 (integer :tag "level" 1)
274 (repeat :menu-tag "mode specific" :tag "mode specific"
275 :value ((t . t))
276 (cons :tag "Instance"
277 (radio :tag "Mode"
278 (const :tag "all" t)
279 (symbol :tag "name"))
280 (radio :tag "Decoration"
281 (const :tag "default" nil)
282 (const :tag "maximum" t)
283 (integer :tag "level" 1)))))
284 :group 'font-lock)
286 (defcustom font-lock-verbose (* 0 1024)
287 "*If non-nil, means show status messages for buffer fontification.
288 If a number, only buffers greater than this size have fontification messages."
289 :type '(choice (const :tag "never" nil)
290 (const :tag "always" t)
291 (integer :tag "size"))
292 :group 'font-lock)
294 ;; Fontification variables:
296 (defvar font-lock-keywords nil
297 "A list of the keywords to highlight.
298 Each element should be of the form:
300 MATCHER
301 (MATCHER . MATCH)
302 (MATCHER . FACENAME)
303 (MATCHER . HIGHLIGHT)
304 (MATCHER HIGHLIGHT ...)
305 (eval . FORM)
307 where HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
309 FORM is an expression, whose value should be a keyword element, evaluated when
310 the keyword is (first) used in a buffer. This feature can be used to provide a
311 keyword that can only be generated when Font Lock mode is actually turned on.
313 For highlighting single items, typically only MATCH-HIGHLIGHT is required.
314 However, if an item or (typically) items are to be highlighted following the
315 instance of another item (the anchor) then MATCH-ANCHORED may be required.
317 MATCH-HIGHLIGHT should be of the form:
319 (MATCH FACENAME OVERRIDE LAXMATCH)
321 Where MATCHER can be either the regexp to search for, or the function name to
322 call to make the search (called with one argument, the limit of the search) and
323 return non-nil if it succeeds (and set `match-data' appropriately).
324 MATCHER regexps can be generated via the function `regexp-opt'. MATCH is the
325 subexpression of MATCHER to be highlighted. MATCH can be calculated via the
326 function `regexp-opt-depth'. FACENAME is an expression whose value is the face
327 name to use. Face default attributes can be modified via \\[customize].
329 OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification can
330 be overwritten. If `keep', only parts not already fontified are highlighted.
331 If `prepend' or `append', existing fontification is merged with the new, in
332 which the new or existing fontification, respectively, takes precedence.
333 If LAXMATCH is non-nil, no error is signaled if there is no MATCH in MATCHER.
335 For example, an element of the form highlights (if not already highlighted):
337 \"\\\\\\=<foo\\\\\\=>\" Discrete occurrences of \"foo\" in the value of the
338 variable `font-lock-keyword-face'.
339 (\"fu\\\\(bar\\\\)\" . 1) Substring \"bar\" within all occurrences of \"fubar\" in
340 the value of `font-lock-keyword-face'.
341 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'.
342 (\"foo\\\\|bar\" 0 foo-bar-face t)
343 Occurrences of either \"foo\" or \"bar\" in the value
344 of `foo-bar-face', even if already highlighted.
345 (fubar-match 1 fubar-face)
346 The first subexpression within all occurrences of
347 whatever the function `fubar-match' finds and matches
348 in the value of `fubar-face'.
350 MATCH-ANCHORED should be of the form:
352 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
354 Where MATCHER is as for MATCH-HIGHLIGHT with one exception; see below.
355 PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
356 the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be
357 used to initialise before, and cleanup after, MATCHER is used. Typically,
358 PRE-MATCH-FORM is used to move to some position relative to the original
359 MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might
360 be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER.
362 For example, an element of the form highlights (if not already highlighted):
364 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
366 Discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
367 discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
368 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is
369 initially searched for starting from the end of the match of \"anchor\", and
370 searching for subsequent instance of \"anchor\" resumes from where searching
371 for \"item\" concluded.)
373 The above-mentioned exception is as follows. The limit of the MATCHER search
374 defaults to the end of the line after PRE-MATCH-FORM is evaluated.
375 However, if PRE-MATCH-FORM returns a position greater than the position after
376 PRE-MATCH-FORM is evaluated, that position is used as the limit of the search.
377 It is generally a bad idea to return a position greater than the end of the
378 line, i.e., cause the MATCHER search to span lines.
380 Note that the MATCH-ANCHORED feature is experimental; in the future, we may
381 replace it with other ways of providing this functionality.
383 These regular expressions should not match text which spans lines. While
384 \\[font-lock-fontify-buffer] handles multi-line patterns correctly, updating
385 when you edit the buffer does not, since it considers text one line at a time.
387 This variable is set by major modes via the variable `font-lock-defaults'.
388 Be careful when composing regexps for this list; a poorly written pattern can
389 dramatically slow things down!")
391 ;; This variable is used by mode packages that support Font Lock mode by
392 ;; defining their own keywords to use for `font-lock-keywords'. (The mode
393 ;; command should make it buffer-local and set it to provide the set up.)
394 (defvar font-lock-defaults nil
395 "Defaults for Font Lock mode specified by the major mode.
396 Defaults should be of the form:
398 (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN ...)
400 KEYWORDS may be a symbol (a variable or function whose value is the keywords to
401 use for fontification) or a list of symbols. If KEYWORDS-ONLY is non-nil,
402 syntactic fontification (strings and comments) is not performed.
403 If CASE-FOLD is non-nil, the case of the keywords is ignored when fontifying.
404 If SYNTAX-ALIST is non-nil, it should be a list of cons pairs of the form
405 \(CHAR-OR-STRING . STRING) used to set the local Font Lock syntax table, for
406 keyword and syntactic fontification (see `modify-syntax-entry').
408 If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
409 backwards outside any enclosing syntactic block, for syntactic fontification.
410 Typical values are `beginning-of-line' (i.e., the start of the line is known to
411 be outside a syntactic block), or `beginning-of-defun' for programming modes or
412 `backward-paragraph' for textual modes (i.e., the mode-dependent function is
413 known to move outside a syntactic block). If nil, the beginning of the buffer
414 is used as a position outside of a syntactic block, in the worst case.
416 These item elements are used by Font Lock mode to set the variables
417 `font-lock-keywords', `font-lock-keywords-only',
418 `font-lock-keywords-case-fold-search', `font-lock-syntax-table' and
419 `font-lock-beginning-of-syntax-function', respectively.
421 Further item elements are alists of the form (VARIABLE . VALUE) and are in no
422 particular order. Each VARIABLE is made buffer-local before set to VALUE.
424 Currently, appropriate variables include `font-lock-mark-block-function'.
425 If this is non-nil, it should be a function with no args used to mark any
426 enclosing block of text, for fontification via \\[font-lock-fontify-block].
427 Typical values are `mark-defun' for programming modes or `mark-paragraph' for
428 textual modes (i.e., the mode-dependent function is known to put point and mark
429 around a text block relevant to that mode).
431 Other variables include those for buffer-specialised fontification functions,
432 `font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function',
433 `font-lock-fontify-region-function', `font-lock-unfontify-region-function',
434 `font-lock-inhibit-thing-lock' and `font-lock-maximum-size'.")
436 ;; This variable is used where font-lock.el itself supplies the keywords.
437 (defvar font-lock-defaults-alist
438 (let (;; We use `beginning-of-defun', rather than nil, for SYNTAX-BEGIN.
439 ;; Thus the calculation of the cache is usually faster but not
440 ;; infallible, so we risk mis-fontification. sm.
441 (c-mode-defaults
442 '((c-font-lock-keywords c-font-lock-keywords-1
443 c-font-lock-keywords-2 c-font-lock-keywords-3)
444 nil nil ((?_ . "w")) beginning-of-defun
445 ;; Obsoleted by Emacs 20 parse-partial-sexp's COMMENTSTOP.
446 ;(font-lock-comment-start-regexp . "/[*/]")
447 (font-lock-mark-block-function . mark-defun)))
448 (c++-mode-defaults
449 '((c++-font-lock-keywords c++-font-lock-keywords-1
450 c++-font-lock-keywords-2 c++-font-lock-keywords-3)
451 nil nil ((?_ . "w")) beginning-of-defun
452 ;; Obsoleted by Emacs 20 parse-partial-sexp's COMMENTSTOP.
453 ;(font-lock-comment-start-regexp . "/[*/]")
454 (font-lock-mark-block-function . mark-defun)))
455 (objc-mode-defaults
456 '((objc-font-lock-keywords objc-font-lock-keywords-1
457 objc-font-lock-keywords-2 objc-font-lock-keywords-3)
458 nil nil ((?_ . "w") (?$ . "w")) nil
459 ;; Obsoleted by Emacs 20 parse-partial-sexp's COMMENTSTOP.
460 ;(font-lock-comment-start-regexp . "/[*/]")
461 (font-lock-mark-block-function . mark-defun)))
462 (java-mode-defaults
463 '((java-font-lock-keywords java-font-lock-keywords-1
464 java-font-lock-keywords-2 java-font-lock-keywords-3)
465 nil nil ((?_ . "w") (?$ . "w") (?. . "w")) nil
466 ;; Obsoleted by Emacs 20 parse-partial-sexp's COMMENTSTOP.
467 ;(font-lock-comment-start-regexp . "/[*/]")
468 (font-lock-mark-block-function . mark-defun)))
469 (lisp-mode-defaults
470 '((lisp-font-lock-keywords
471 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
472 nil nil (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
473 ;; Obsoleted by Emacs 20 parse-partial-sexp's COMMENTSTOP.
474 ;(font-lock-comment-start-regexp . ";")
475 (font-lock-mark-block-function . mark-defun)))
476 (scheme-mode-defaults
477 '((scheme-font-lock-keywords
478 scheme-font-lock-keywords-1 scheme-font-lock-keywords-2)
479 nil t (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
480 ;; Obsoleted by Emacs 20 parse-partial-sexp's COMMENTSTOP.
481 ;(font-lock-comment-start-regexp . ";")
482 (font-lock-mark-block-function . mark-defun)))
483 ;; For TeX modes we could use `backward-paragraph' for the same reason.
484 ;; But we don't, because paragraph breaks are arguably likely enough to
485 ;; occur within a genuine syntactic block to make it too risky.
486 ;; However, we do specify a MARK-BLOCK function as that cannot result
487 ;; in a mis-fontification even if it might not fontify enough. --sm.
488 (tex-mode-defaults
489 '((tex-font-lock-keywords
490 tex-font-lock-keywords-1 tex-font-lock-keywords-2)
491 nil nil ((?$ . "\"")) nil
492 ;; Obsoleted by Emacs 20 parse-partial-sexp's COMMENTSTOP.
493 ;(font-lock-comment-start-regexp . "%")
494 (font-lock-mark-block-function . mark-paragraph)))
496 (list
497 (cons 'c-mode c-mode-defaults)
498 (cons 'c++-mode c++-mode-defaults)
499 (cons 'objc-mode objc-mode-defaults)
500 (cons 'java-mode java-mode-defaults)
501 (cons 'emacs-lisp-mode lisp-mode-defaults)
502 (cons 'inferior-scheme-mode scheme-mode-defaults)
503 (cons 'latex-mode tex-mode-defaults)
504 (cons 'lisp-mode lisp-mode-defaults)
505 (cons 'lisp-interaction-mode lisp-mode-defaults)
506 (cons 'plain-tex-mode tex-mode-defaults)
507 (cons 'scheme-mode scheme-mode-defaults)
508 (cons 'scheme-interaction-mode scheme-mode-defaults)
509 (cons 'slitex-mode tex-mode-defaults)
510 (cons 'tex-mode tex-mode-defaults)))
511 "Alist of fall-back Font Lock defaults for major modes.
512 Each item should be a list of the form:
514 (MAJOR-MODE . FONT-LOCK-DEFAULTS)
516 where MAJOR-MODE is a symbol and FONT-LOCK-DEFAULTS is a list of default
517 settings. See the variable `font-lock-defaults', which takes precedence.")
519 (defvar font-lock-keywords-alist nil
520 "*Alist of `font-lock-keywords' local to a `major-mode'.
521 This is normally set via `font-lock-add-keywords'.")
523 (defvar font-lock-keywords-only nil
524 "*Non-nil means Font Lock should not fontify comments or strings.
525 This is normally set via `font-lock-defaults'.")
527 (defvar font-lock-keywords-case-fold-search nil
528 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
529 This is normally set via `font-lock-defaults'.")
531 (defvar font-lock-syntactic-keywords nil
532 "A list of the syntactic keywords to highlight.
533 Can be the list or the name of a function or variable whose value is the list.
534 See `font-lock-keywords' for a description of the form of this list;
535 the differences are listed below. MATCH-HIGHLIGHT should be of the form:
537 (MATCH SYNTAX OVERRIDE LAXMATCH)
539 where SYNTAX can be of the form (SYNTAX-CODE . MATCHING-CHAR), the name of a
540 syntax table, or an expression whose value is such a form or a syntax table.
541 OVERRIDE cannot be `prepend' or `append'.
543 This is normally set via `font-lock-defaults'.")
545 (defvar font-lock-syntax-table nil
546 "Non-nil means use this syntax table for fontifying.
547 If this is nil, the major mode's syntax table is used.
548 This is normally set via `font-lock-defaults'.")
550 ;; If this is nil, we only use the beginning of the buffer if we can't use
551 ;; `font-lock-cache-position' and `font-lock-cache-state'.
552 (defvar font-lock-beginning-of-syntax-function nil
553 "*Non-nil means use this function to move back outside of a syntactic block.
554 When called with no args it should leave point at the beginning of any
555 enclosing syntactic block.
556 If this is nil, the beginning of the buffer is used (in the worst case).
557 This is normally set via `font-lock-defaults'.")
559 (defvar font-lock-mark-block-function nil
560 "*Non-nil means use this function to mark a block of text.
561 When called with no args it should leave point at the beginning of any
562 enclosing textual block and mark at the end.
563 This is normally set via `font-lock-defaults'.")
565 ;; Obsoleted by Emacs 20 parse-partial-sexp's COMMENTSTOP.
566 ;(defvar font-lock-comment-start-regexp nil
567 ; "*Regexp to match the start of a comment.
568 ;This need not discriminate between genuine comments and quoted comment
569 ;characters or comment characters within strings.
570 ;If nil, `comment-start-skip' is used instead; see that variable for more info.
571 ;This is normally set via `font-lock-defaults'.")
573 (defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
574 "Function to use for fontifying the buffer.
575 This is normally set via `font-lock-defaults'.")
577 (defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
578 "Function to use for unfontifying the buffer.
579 This is used when turning off Font Lock mode.
580 This is normally set via `font-lock-defaults'.")
582 (defvar font-lock-fontify-region-function 'font-lock-default-fontify-region
583 "Function to use for fontifying a region.
584 It should take two args, the beginning and end of the region, and an optional
585 third arg VERBOSE. If non-nil, the function should print status messages.
586 This is normally set via `font-lock-defaults'.")
588 (defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
589 "Function to use for unfontifying a region.
590 It should take two args, the beginning and end of the region.
591 This is normally set via `font-lock-defaults'.")
593 (defvar font-lock-inhibit-thing-lock nil
594 "List of Font Lock mode related modes that should not be turned on.
595 Currently, valid mode names as `fast-lock-mode' and `lazy-lock-mode'.
596 This is normally set via `font-lock-defaults'.")
598 (defvar font-lock-mode nil) ; Whether we are turned on/modeline.
599 (defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
601 ;;;###autoload
602 (defvar font-lock-mode-hook nil
603 "Function or functions to run on entry to Font Lock mode.")
605 ;; Font Lock mode.
607 (eval-when-compile
609 ;; We don't do this at the top-level as we only use non-autoloaded macros.
610 (require 'cl)
612 ;; Borrowed from lazy-lock.el.
613 ;; We use this to preserve or protect things when modifying text properties.
614 (defmacro save-buffer-state (varlist &rest body)
615 "Bind variables according to VARLIST and eval BODY restoring buffer state."
616 (` (let* ((,@ (append varlist
617 '((modified (buffer-modified-p)) (buffer-undo-list t)
618 (inhibit-read-only t) (inhibit-point-motion-hooks t)
619 before-change-functions after-change-functions
620 deactivate-mark buffer-file-name buffer-file-truename))))
621 (,@ body)
622 (when (and (not modified) (buffer-modified-p))
623 (set-buffer-modified-p nil)))))
624 (put 'save-buffer-state 'lisp-indent-function 1)
626 ;; Shut up the byte compiler.
627 (defvar global-font-lock-mode) ; Now a defcustom.
628 (defvar font-lock-face-attributes) ; Obsolete but respected if set.
629 (defvar font-lock-string-face) ; Used in syntactic fontification.
630 (defvar font-lock-comment-face))
632 ;;;###autoload
633 (defun font-lock-mode (&optional arg)
634 "Toggle Font Lock mode.
635 With arg, turn Font Lock mode on if and only if arg is positive.
637 When Font Lock mode is enabled, text is fontified as you type it:
639 - Comments are displayed in `font-lock-comment-face';
640 - Strings are displayed in `font-lock-string-face';
641 - Certain other expressions are displayed in other faces according to the
642 value of the variable `font-lock-keywords'.
644 You can enable Font Lock mode in any major mode automatically by turning on in
645 the major mode's hook. For example, put in your ~/.emacs:
647 (add-hook 'c-mode-hook 'turn-on-font-lock)
649 Alternatively, you can use Global Font Lock mode to automagically turn on Font
650 Lock mode in buffers whose major mode supports it and whose major mode is one
651 of `font-lock-global-modes'. For example, put in your ~/.emacs:
653 (global-font-lock-mode t)
655 There are a number of support modes that may be used to speed up Font Lock mode
656 in various ways, specified via the variable `font-lock-support-mode'. Where
657 major modes support different levels of fontification, you can use the variable
658 `font-lock-maximum-decoration' to specify which level you generally prefer.
659 When you turn Font Lock mode on/off the buffer is fontified/defontified, though
660 fontification occurs only if the buffer is less than `font-lock-maximum-size'.
662 For example, to specify that Font Lock mode use use Lazy Lock mode as a support
663 mode and use maximum levels of fontification, put in your ~/.emacs:
665 (setq font-lock-support-mode 'lazy-lock-mode)
666 (setq font-lock-maximum-decoration t)
668 To add your own highlighting for some major mode, and modify the highlighting
669 selected automatically via the variable `font-lock-maximum-decoration', you can
670 use `font-lock-add-keywords'.
672 To fontify a buffer, without turning on Font Lock mode and regardless of buffer
673 size, you can use \\[font-lock-fontify-buffer].
675 To fontify a block (the function or paragraph containing point, or a number of
676 lines around point), perhaps because modification on the current line caused
677 syntactic change on other lines, you can use \\[font-lock-fontify-block].
679 See the variable `font-lock-defaults-alist' for the Font Lock mode default
680 settings. You can set your own default settings for some mode, by setting a
681 buffer local value for `font-lock-defaults', via its mode hook."
682 (interactive "P")
683 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
684 ;; batch job) or if the buffer is invisible (the name starts with a space).
685 (let ((on-p (and (not noninteractive)
686 (not (eq (aref (buffer-name) 0) ?\ ))
687 (if arg
688 (> (prefix-numeric-value arg) 0)
689 (not font-lock-mode)))))
690 (set (make-local-variable 'font-lock-mode) on-p)
691 ;; Turn on Font Lock mode.
692 (when on-p
693 (make-local-hook 'after-change-functions)
694 (add-hook 'after-change-functions 'font-lock-after-change-function nil t)
695 (font-lock-set-defaults)
696 (font-lock-turn-on-thing-lock)
697 (run-hooks 'font-lock-mode-hook)
698 ;; Fontify the buffer if we have to.
699 (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size)))
700 (cond (font-lock-fontified
701 nil)
702 ((or (null max-size) (> max-size (buffer-size)))
703 (font-lock-fontify-buffer))
704 (font-lock-verbose
705 (message "Fontifying %s...buffer too big" (buffer-name))))))
706 ;; Turn off Font Lock mode.
707 (unless on-p
708 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
709 (font-lock-unfontify-buffer)
710 (font-lock-turn-off-thing-lock)
711 (font-lock-unset-defaults))
712 (force-mode-line-update)))
714 ;;;###autoload
715 (defun turn-on-font-lock ()
716 "Turn on Font Lock mode conditionally.
717 Turn on only if the terminal can display it."
718 (when (and (not font-lock-mode) window-system)
719 (font-lock-mode)))
721 ;;;###autoload
722 (defun font-lock-add-keywords (major-mode keywords &optional append)
723 "Add highlighting KEYWORDS for MAJOR-MODE.
724 MAJOR-MODE should be a symbol, the major mode command name, such as `c-mode'
725 or nil. If nil, highlighting keywords are added for the current buffer.
726 KEYWORDS should be a list; see the variable `font-lock-keywords'.
727 By default they are added at the beginning of the current highlighting list.
728 If optional argument APPEND is `set', they are used to replace the current
729 highlighting list. If APPEND is any other non-nil value, they are added at the
730 end of the current highlighting list.
732 For example:
734 (font-lock-add-keywords 'c-mode
735 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
736 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . font-lock-keyword-face)))
738 adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
739 comments, and to fontify `and', `or' and `not' words as keywords.
741 Note that some modes have specialised support for additional patterns, e.g.,
742 see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
743 `objc-font-lock-extra-types' and `java-font-lock-extra-types'."
744 (cond (major-mode
745 ;; If MAJOR-MODE is non-nil, add the KEYWORDS and APPEND spec to
746 ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
747 (let ((spec (cons keywords append)) cell)
748 (if (setq cell (assq major-mode font-lock-keywords-alist))
749 (setcdr cell (append (cdr cell) (list spec)))
750 (push (list major-mode spec) font-lock-keywords-alist))))
751 (font-lock-mode
752 ;; Otherwise if Font Lock mode is on, set or add the keywords now.
753 (if (eq append 'set)
754 (setq font-lock-keywords keywords)
755 (let ((old (if (eq (car-safe font-lock-keywords) t)
756 (cdr font-lock-keywords)
757 font-lock-keywords)))
758 (setq font-lock-keywords (if append
759 (append old keywords)
760 (append keywords old))))))))
762 ;;; Global Font Lock mode.
764 ;; A few people have hassled in the past for a way to make it easier to turn on
765 ;; Font Lock mode, without the user needing to know for which modes s/he has to
766 ;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always
767 ;; balked at that way, as I see it as just re-moulding the same problem in
768 ;; another form. That is; some person would still have to keep track of which
769 ;; modes (which may not even be distributed with Emacs) support Font Lock mode.
770 ;; The list would always be out of date. And that person might have to be me.
772 ;; Implementation.
774 ;; In a previous discussion the following hack came to mind. It is a gross
775 ;; hack, but it generally works. We use the convention that major modes start
776 ;; by calling the function `kill-all-local-variables', which in turn runs
777 ;; functions on the hook variable `change-major-mode-hook'. We attach our
778 ;; function `font-lock-change-major-mode' to that hook. Of course, when this
779 ;; hook is run, the major mode is in the process of being changed and we do not
780 ;; know what the final major mode will be. So, `font-lock-change-major-mode'
781 ;; only (a) notes the name of the current buffer, and (b) adds our function
782 ;; `turn-on-font-lock-if-enabled' to the hook variables `find-file-hooks' and
783 ;; `post-command-hook' (for buffers that are not visiting files). By the time
784 ;; the functions on the first of these hooks to be run are run, the new major
785 ;; mode is assumed to be in place. This way we get a Font Lock function run
786 ;; when a major mode is turned on, without knowing major modes or their hooks.
788 ;; Naturally this requires that (a) major modes run `kill-all-local-variables',
789 ;; as they are supposed to do, and (b) the major mode is in place after the
790 ;; file is visited or the command that ran `kill-all-local-variables' has
791 ;; finished, whichever the sooner. Arguably, any major mode that does not
792 ;; follow the convension (a) is broken, and I can't think of any reason why (b)
793 ;; would not be met (except `gnudoit' on non-files). However, it is not clean.
795 ;; Probably the cleanest solution is to have each major mode function run some
796 ;; hook, e.g., `major-mode-hook', but maybe implementing that change is
797 ;; impractical. I am personally against making `setq' a macro or be advised,
798 ;; or have a special function such as `set-major-mode', but maybe someone can
799 ;; come up with another solution?
801 ;; User interface.
803 ;; Although Global Font Lock mode is a pseudo-mode, I think that the user
804 ;; interface should conform to the usual Emacs convention for modes, i.e., a
805 ;; command to toggle the feature (`global-font-lock-mode') with a variable for
806 ;; finer control of the mode's behaviour (`font-lock-global-modes').
808 ;; The feature should not be enabled by loading font-lock.el, since other
809 ;; mechanisms for turning on Font Lock mode, such as M-x font-lock-mode RET or
810 ;; (add-hook 'c-mode-hook 'turn-on-font-lock), would cause Font Lock mode to be
811 ;; turned on everywhere. That would not be intuitive or informative because
812 ;; loading a file tells you nothing about the feature or how to control it. It
813 ;; would also be contrary to the Principle of Least Surprise. sm.
815 (defvar font-lock-buffers nil) ; For remembering buffers.
817 ;;;###autoload
818 (defun global-font-lock-mode (&optional arg message)
819 "Toggle Global Font Lock mode.
820 With prefix ARG, turn Global Font Lock mode on if and only if ARG is positive.
821 Displays a message saying whether the mode is on or off if MESSAGE is non-nil.
822 Returns the new status of Global Font Lock mode (non-nil means on).
824 When Global Font Lock mode is enabled, Font Lock mode is automagically
825 turned on in a buffer if its major mode is one of `font-lock-global-modes'."
826 (interactive "P\np")
827 (let ((on-p (if arg
828 (> (prefix-numeric-value arg) 0)
829 (not global-font-lock-mode))))
830 (cond (on-p
831 (add-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
832 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
833 (setq font-lock-buffers (buffer-list)))
835 (remove-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
836 (mapcar (function (lambda (buffer)
837 (with-current-buffer buffer
838 (when font-lock-mode
839 (font-lock-mode)))))
840 (buffer-list))))
841 (when message
842 (message "Global Font Lock mode %s." (if on-p "enabled" "disabled")))
843 (setq global-font-lock-mode on-p)))
845 ;; Naughty hack. This variable was originally a `defvar' to keep track of
846 ;; whether Global Font Lock mode was turned on or not. As a `defcustom' with
847 ;; special `:set' and `:require' forms, we can provide custom mode control.
848 (defcustom global-font-lock-mode nil
849 "Toggle Global Font Lock mode.
850 When Global Font Lock mode is enabled, Font Lock mode is automagically
851 turned on in a buffer if its major mode is one of `font-lock-global-modes'.
852 You must modify via \\[customize] for this variable to have an effect."
853 :set (lambda (symbol value)
854 (global-font-lock-mode (or value 0)))
855 :type 'boolean
856 :group 'font-lock
857 :require 'font-lock)
859 (defcustom font-lock-global-modes t
860 "*Modes for which Font Lock mode is automagically turned on.
861 Global Font Lock mode is controlled by the `global-font-lock-mode' command.
862 If nil, means no modes have Font Lock mode automatically turned on.
863 If t, all modes that support Font Lock mode have it automatically turned on.
864 If a list, it should be a list of `major-mode' symbol names for which Font Lock
865 mode should be automatically turned on. The sense of the list is negated if it
866 begins with `not'. For example:
867 (c-mode c++-mode)
868 means that Font Lock mode is turned on for buffers in C and C++ modes only."
869 :type '(choice (const :tag "none" nil)
870 (const :tag "all" t)
871 (set :menu-tag "mode specific" :tag "modes"
872 :value (not)
873 (const :tag "Except" not)
874 (repeat :inline t (symbol :tag "mode"))))
875 :group 'font-lock)
877 (defun font-lock-change-major-mode ()
878 ;; Turn off Font Lock mode if it's on.
879 (when font-lock-mode
880 (font-lock-mode))
881 ;; Gross hack warning: Delicate readers should avert eyes now.
882 ;; Something is running `kill-all-local-variables', which generally means the
883 ;; major mode is being changed. Run `turn-on-font-lock-if-enabled' after the
884 ;; file is visited or the current command has finished.
885 (when global-font-lock-mode
886 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
887 (add-to-list 'font-lock-buffers (current-buffer))))
889 (defun turn-on-font-lock-if-enabled ()
890 ;; Gross hack warning: Delicate readers should avert eyes now.
891 ;; Turn on Font Lock mode if it's supported by the major mode and enabled by
892 ;; the user.
893 (remove-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
894 (while font-lock-buffers
895 (when (buffer-live-p (car font-lock-buffers))
896 (save-excursion
897 (set-buffer (car font-lock-buffers))
898 (when (and (or font-lock-defaults
899 (assq major-mode font-lock-defaults-alist))
900 (or (eq font-lock-global-modes t)
901 (if (eq (car-safe font-lock-global-modes) 'not)
902 (not (memq major-mode (cdr font-lock-global-modes)))
903 (memq major-mode font-lock-global-modes))))
904 (let (inhibit-quit)
905 (turn-on-font-lock)))))
906 (setq font-lock-buffers (cdr font-lock-buffers))))
908 (add-hook 'change-major-mode-hook 'font-lock-change-major-mode)
910 ;;; End of Global Font Lock mode.
912 ;;; Font Lock Support mode.
914 ;; This is the code used to interface font-lock.el with any of its add-on
915 ;; packages, and provide the user interface. Packages that have their own
916 ;; local buffer fontification functions (see below) may have to call
917 ;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
918 ;; themselves.
920 (defcustom font-lock-support-mode nil
921 "*Support mode for Font Lock mode.
922 Support modes speed up Font Lock mode by being choosy about when fontification
923 occurs. Known support modes are Fast Lock mode (symbol `fast-lock-mode') and
924 Lazy Lock mode (symbol `lazy-lock-mode'). See those modes for more info.
925 If nil, means support for Font Lock mode is never performed.
926 If a symbol, use that support mode.
927 If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
928 where MAJOR-MODE is a symbol or t (meaning the default). For example:
929 ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
930 means that Fast Lock mode is used to support Font Lock mode for buffers in C or
931 C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
933 The value of this variable is used when Font Lock mode is turned on."
934 :type '(choice (const :tag "none" nil)
935 (const :tag "fast lock" fast-lock-mode)
936 (const :tag "lazy lock" lazy-lock-mode)
937 (repeat :menu-tag "mode specific" :tag "mode specific"
938 :value ((t . lazy-lock-mode))
939 (cons :tag "Instance"
940 (radio :tag "Mode"
941 (const :tag "all" t)
942 (symbol :tag "name"))
943 (radio :tag "Decoration"
944 (const :tag "fast lock" fast-lock-mode)
945 (const :tag "lazy lock" lazy-lock-mode)))
947 :group 'font-lock)
949 (defvar fast-lock-mode nil)
950 (defvar lazy-lock-mode nil)
952 (defun font-lock-turn-on-thing-lock ()
953 (let ((thing-mode (font-lock-value-in-major-mode font-lock-support-mode)))
954 (cond ((eq thing-mode 'fast-lock-mode)
955 (fast-lock-mode t))
956 ((eq thing-mode 'lazy-lock-mode)
957 (lazy-lock-mode t)))))
959 (defun font-lock-turn-off-thing-lock ()
960 (cond (fast-lock-mode
961 (fast-lock-mode nil))
962 (lazy-lock-mode
963 (lazy-lock-mode nil))))
965 (defun font-lock-after-fontify-buffer ()
966 (cond (fast-lock-mode
967 (fast-lock-after-fontify-buffer))
968 (lazy-lock-mode
969 (lazy-lock-after-fontify-buffer))))
971 (defun font-lock-after-unfontify-buffer ()
972 (cond (fast-lock-mode
973 (fast-lock-after-unfontify-buffer))
974 (lazy-lock-mode
975 (lazy-lock-after-unfontify-buffer))))
977 ;;; End of Font Lock Support mode.
979 ;;; Fontification functions.
981 ;; Rather than the function, e.g., `font-lock-fontify-region' containing the
982 ;; code to fontify a region, the function runs the function whose name is the
983 ;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally,
984 ;; the value of this variable is, e.g., `font-lock-default-fontify-region'
985 ;; which does contain the code to fontify a region. However, the value of the
986 ;; variable could be anything and thus, e.g., `font-lock-fontify-region' could
987 ;; do anything. The indirection of the fontification functions gives major
988 ;; modes the capability of modifying the way font-lock.el fontifies. Major
989 ;; modes can modify the values of, e.g., `font-lock-fontify-region-function',
990 ;; via the variable `font-lock-defaults'.
992 ;; For example, Rmail mode sets the variable `font-lock-defaults' so that
993 ;; font-lock.el uses its own function for buffer fontification. This function
994 ;; makes fontification be on a message-by-message basis and so visiting an
995 ;; RMAIL file is much faster. A clever implementation of the function might
996 ;; fontify the headers differently than the message body. (It should, and
997 ;; correspondingly for Mail mode, but I can't be bothered to do the work. Can
998 ;; you?) This hints at a more interesting use...
1000 ;; Languages that contain text normally contained in different major modes
1001 ;; could define their own fontification functions that treat text differently
1002 ;; depending on its context. For example, Perl mode could arrange that here
1003 ;; docs are fontified differently than Perl code. Or Yacc mode could fontify
1004 ;; rules one way and C code another. Neat!
1006 ;; A further reason to use the fontification indirection feature is when the
1007 ;; default syntactual fontification, or the default fontification in general,
1008 ;; is not flexible enough for a particular major mode. For example, perhaps
1009 ;; comments are just too hairy for `font-lock-fontify-syntactically-region' to
1010 ;; cope with. You need to write your own version of that function, e.g.,
1011 ;; `hairy-fontify-syntactically-region', and make your own version of
1012 ;; `hairy-fontify-region' call that function before calling
1013 ;; `font-lock-fontify-keywords-region' for the normal regexp fontification
1014 ;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el
1015 ;; would call your region fontification function instead of its own. For
1016 ;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line
1017 ;; directives correctly and cleanly. (It is the same problem as fontifying
1018 ;; multi-line strings and comments; regexps are not appropriate for the job.)
1020 ;;;###autoload
1021 (defun font-lock-fontify-buffer ()
1022 "Fontify the current buffer the way `font-lock-mode' would."
1023 (interactive)
1024 (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
1025 (funcall font-lock-fontify-buffer-function)))
1027 (defun font-lock-unfontify-buffer ()
1028 (funcall font-lock-unfontify-buffer-function))
1030 (defun font-lock-fontify-region (beg end &optional loudly)
1031 (funcall font-lock-fontify-region-function beg end loudly))
1033 (defun font-lock-unfontify-region (beg end)
1034 (funcall font-lock-unfontify-region-function beg end))
1036 (defun font-lock-default-fontify-buffer ()
1037 (let ((verbose (if (numberp font-lock-verbose)
1038 (> (buffer-size) font-lock-verbose)
1039 font-lock-verbose)))
1040 (when verbose
1041 (message "Fontifying %s..." (buffer-name)))
1042 ;; Make sure we have the right `font-lock-keywords' etc.
1043 (unless font-lock-mode
1044 (font-lock-set-defaults))
1045 ;; Make sure we fontify etc. in the whole buffer.
1046 (save-restriction
1047 (widen)
1048 (condition-case nil
1049 (save-excursion
1050 (save-match-data
1051 (font-lock-fontify-region (point-min) (point-max) verbose)
1052 (font-lock-after-fontify-buffer)
1053 (setq font-lock-fontified t)))
1054 ;; We don't restore the old fontification, so it's best to unfontify.
1055 (quit (font-lock-unfontify-buffer))))
1056 ;; Make sure we undo `font-lock-keywords' etc.
1057 (unless font-lock-mode
1058 (font-lock-unset-defaults))
1059 (if verbose (message "Fontifying %s...%s" (buffer-name)
1060 (if font-lock-fontified "done" "quit")))))
1062 (defun font-lock-default-unfontify-buffer ()
1063 ;; Make sure we unfontify etc. in the whole buffer.
1064 (save-restriction
1065 (widen)
1066 (font-lock-unfontify-region (point-min) (point-max))
1067 (font-lock-after-unfontify-buffer)
1068 (setq font-lock-fontified nil)))
1070 (defun font-lock-default-fontify-region (beg end loudly)
1071 (save-buffer-state
1072 ((parse-sexp-lookup-properties font-lock-syntactic-keywords)
1073 (old-syntax-table (syntax-table)))
1074 (unwind-protect
1075 (save-restriction
1076 (widen)
1077 ;; Use the fontification syntax table, if any.
1078 (when font-lock-syntax-table
1079 (set-syntax-table font-lock-syntax-table))
1080 ;; Now do the fontification.
1081 (font-lock-unfontify-region beg end)
1082 (when font-lock-syntactic-keywords
1083 (font-lock-fontify-syntactic-keywords-region beg end))
1084 (unless font-lock-keywords-only
1085 (font-lock-fontify-syntactically-region beg end loudly))
1086 (font-lock-fontify-keywords-region beg end loudly))
1087 ;; Clean up.
1088 (set-syntax-table old-syntax-table))))
1090 ;; The following must be rethought, since keywords can override fontification.
1091 ; ;; Now scan for keywords, but not if we are inside a comment now.
1092 ; (or (and (not font-lock-keywords-only)
1093 ; (let ((state (parse-partial-sexp beg end nil nil
1094 ; font-lock-cache-state)))
1095 ; (or (nth 4 state) (nth 7 state))))
1096 ; (font-lock-fontify-keywords-region beg end))
1098 (defun font-lock-default-unfontify-region (beg end)
1099 (save-buffer-state nil
1100 (remove-text-properties beg end '(face nil syntax-table nil))))
1102 ;; Called when any modification is made to buffer text.
1103 (defun font-lock-after-change-function (beg end old-len)
1104 (let ((inhibit-point-motion-hooks t))
1105 (save-excursion
1106 (save-match-data
1107 ;; Rescan between start of lines enclosing the region.
1108 (font-lock-fontify-region
1109 (progn (goto-char beg) (beginning-of-line) (point))
1110 (progn (goto-char end) (forward-line 1) (point)))))))
1112 (defun font-lock-fontify-block (&optional arg)
1113 "Fontify some lines the way `font-lock-fontify-buffer' would.
1114 The lines could be a function or paragraph, or a specified number of lines.
1115 If ARG is given, fontify that many lines before and after point, or 16 lines if
1116 no ARG is given and `font-lock-mark-block-function' is nil.
1117 If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
1118 delimit the region to fontify."
1119 (interactive "P")
1120 (let ((inhibit-point-motion-hooks t) font-lock-beginning-of-syntax-function
1121 deactivate-mark)
1122 ;; Make sure we have the right `font-lock-keywords' etc.
1123 (if (not font-lock-mode) (font-lock-set-defaults))
1124 (save-excursion
1125 (save-match-data
1126 (condition-case error-data
1127 (if (or arg (not font-lock-mark-block-function))
1128 (let ((lines (if arg (prefix-numeric-value arg) 16)))
1129 (font-lock-fontify-region
1130 (save-excursion (forward-line (- lines)) (point))
1131 (save-excursion (forward-line lines) (point))))
1132 (funcall font-lock-mark-block-function)
1133 (font-lock-fontify-region (point) (mark)))
1134 ((error quit) (message "Fontifying block...%s" error-data)))))))
1136 (define-key facemenu-keymap "\M-g" 'font-lock-fontify-block)
1138 ;;; End of Fontification functions.
1140 ;;; Additional text property functions.
1142 ;; The following text property functions should be builtins. This means they
1143 ;; should be written in C and put with all the other text property functions.
1144 ;; In the meantime, those that are used by font-lock.el are defined in Lisp
1145 ;; below and given a `font-lock-' prefix. Those that are not used are defined
1146 ;; in Lisp below and commented out. sm.
1148 (defun font-lock-prepend-text-property (start end prop value &optional object)
1149 "Prepend to one property of the text from START to END.
1150 Arguments PROP and VALUE specify the property and value to prepend to the value
1151 already in place. The resulting property values are always lists.
1152 Optional argument OBJECT is the string or buffer containing the text."
1153 (let ((val (if (listp value) value (list value))) next prev)
1154 (while (/= start end)
1155 (setq next (next-single-property-change start prop object end)
1156 prev (get-text-property start prop object))
1157 (put-text-property start next prop
1158 (append val (if (listp prev) prev (list prev)))
1159 object)
1160 (setq start next))))
1162 (defun font-lock-append-text-property (start end prop value &optional object)
1163 "Append to one property of the text from START to END.
1164 Arguments PROP and VALUE specify the property and value to append to the value
1165 already in place. The resulting property values are always lists.
1166 Optional argument OBJECT is the string or buffer containing the text."
1167 (let ((val (if (listp value) value (list value))) next prev)
1168 (while (/= start end)
1169 (setq next (next-single-property-change start prop object end)
1170 prev (get-text-property start prop object))
1171 (put-text-property start next prop
1172 (append (if (listp prev) prev (list prev)) val)
1173 object)
1174 (setq start next))))
1176 (defun font-lock-fillin-text-property (start end prop value &optional object)
1177 "Fill in one property of the text from START to END.
1178 Arguments PROP and VALUE specify the property and value to put where none are
1179 already in place. Therefore existing property values are not overwritten.
1180 Optional argument OBJECT is the string or buffer containing the text."
1181 (let ((start (text-property-any start end prop nil object)) next)
1182 (while start
1183 (setq next (next-single-property-change start prop object end))
1184 (put-text-property start next prop value object)
1185 (setq start (text-property-any next end prop nil object)))))
1187 ;; For completeness: this is to `remove-text-properties' as `put-text-property'
1188 ;; is to `add-text-properties', etc.
1189 ;(defun remove-text-property (start end property &optional object)
1190 ; "Remove a property from text from START to END.
1191 ;Argument PROPERTY is the property to remove.
1192 ;Optional argument OBJECT is the string or buffer containing the text.
1193 ;Return t if the property was actually removed, nil otherwise."
1194 ; (remove-text-properties start end (list property) object))
1196 ;; For consistency: maybe this should be called `remove-single-property' like
1197 ;; `next-single-property-change' (not `next-single-text-property-change'), etc.
1198 ;(defun remove-single-text-property (start end prop value &optional object)
1199 ; "Remove a specific property value from text from START to END.
1200 ;Arguments PROP and VALUE specify the property and value to remove. The
1201 ;resulting property values are not equal to VALUE nor lists containing VALUE.
1202 ;Optional argument OBJECT is the string or buffer containing the text."
1203 ; (let ((start (text-property-not-all start end prop nil object)) next prev)
1204 ; (while start
1205 ; (setq next (next-single-property-change start prop object end)
1206 ; prev (get-text-property start prop object))
1207 ; (cond ((and (symbolp prev) (eq value prev))
1208 ; (remove-text-property start next prop object))
1209 ; ((and (listp prev) (memq value prev))
1210 ; (let ((new (delq value prev)))
1211 ; (cond ((null new)
1212 ; (remove-text-property start next prop object))
1213 ; ((= (length new) 1)
1214 ; (put-text-property start next prop (car new) object))
1215 ; (t
1216 ; (put-text-property start next prop new object))))))
1217 ; (setq start (text-property-not-all next end prop nil object)))))
1219 ;;; End of Additional text property functions.
1221 ;;; Syntactic regexp fontification functions.
1223 ;; These syntactic keyword pass functions are identical to those keyword pass
1224 ;; functions below, with the following exceptions; (a) they operate on
1225 ;; `font-lock-syntactic-keywords' of course, (b) they are all `defun' as speed
1226 ;; is less of an issue, (c) eval of property value does not occur JIT as speed
1227 ;; is less of an issue, (d) OVERRIDE cannot be `prepend' or `append' as it
1228 ;; makes no sense for `syntax-table' property values, (e) they do not do it
1229 ;; LOUDLY as it is not likely to be intensive.
1231 (defun font-lock-apply-syntactic-highlight (highlight)
1232 "Apply HIGHLIGHT following a match.
1233 HIGHLIGHT should be of the form MATCH-HIGHLIGHT,
1234 see `font-lock-syntactic-keywords'."
1235 (let* ((match (nth 0 highlight))
1236 (start (match-beginning match)) (end (match-end match))
1237 (value (nth 1 highlight))
1238 (override (nth 2 highlight)))
1239 (unless (numberp (car value))
1240 (setq value (eval value)))
1241 (cond ((not start)
1242 ;; No match but we might not signal an error.
1243 (or (nth 3 highlight)
1244 (error "No match %d in highlight %S" match highlight)))
1245 ((not override)
1246 ;; Cannot override existing fontification.
1247 (or (text-property-not-all start end 'syntax-table nil)
1248 (put-text-property start end 'syntax-table value)))
1249 ((eq override t)
1250 ;; Override existing fontification.
1251 (put-text-property start end 'syntax-table value))
1252 ((eq override 'keep)
1253 ;; Keep existing fontification.
1254 (font-lock-fillin-text-property start end 'syntax-table value)))))
1256 (defun font-lock-fontify-syntactic-anchored-keywords (keywords limit)
1257 "Fontify according to KEYWORDS until LIMIT.
1258 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1259 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1260 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1261 ;; Evaluate PRE-MATCH-FORM.
1262 (pre-match-value (eval (nth 1 keywords))))
1263 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1264 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1265 (setq limit pre-match-value)
1266 (save-excursion (end-of-line) (setq limit (point))))
1267 (save-match-data
1268 ;; Find an occurrence of `matcher' before `limit'.
1269 (while (if (stringp matcher)
1270 (re-search-forward matcher limit t)
1271 (funcall matcher limit))
1272 ;; Apply each highlight to this instance of `matcher'.
1273 (setq highlights lowdarks)
1274 (while highlights
1275 (font-lock-apply-syntactic-highlight (car highlights))
1276 (setq highlights (cdr highlights)))))
1277 ;; Evaluate POST-MATCH-FORM.
1278 (eval (nth 2 keywords))))
1280 (defun font-lock-fontify-syntactic-keywords-region (start end)
1281 "Fontify according to `font-lock-syntactic-keywords' between START and END.
1282 START should be at the beginning of a line."
1283 ;; If `font-lock-syntactic-keywords' is a symbol, get the real keywords.
1284 (when (symbolp font-lock-syntactic-keywords)
1285 (setq font-lock-syntactic-keywords (font-lock-eval-keywords
1286 font-lock-syntactic-keywords)))
1287 ;; If `font-lock-syntactic-keywords' is not compiled, compile it.
1288 (unless (eq (car font-lock-syntactic-keywords) t)
1289 (setq font-lock-syntactic-keywords (font-lock-compile-keywords
1290 font-lock-syntactic-keywords)))
1291 ;; Get down to business.
1292 (let ((case-fold-search font-lock-keywords-case-fold-search)
1293 (keywords (cdr font-lock-syntactic-keywords))
1294 keyword matcher highlights)
1295 (while keywords
1296 ;; Find an occurrence of `matcher' from `start' to `end'.
1297 (setq keyword (car keywords) matcher (car keyword))
1298 (goto-char start)
1299 (while (if (stringp matcher)
1300 (re-search-forward matcher end t)
1301 (funcall matcher end))
1302 ;; Apply each highlight to this instance of `matcher', which may be
1303 ;; specific highlights or more keywords anchored to `matcher'.
1304 (setq highlights (cdr keyword))
1305 (while highlights
1306 (if (numberp (car (car highlights)))
1307 (font-lock-apply-syntactic-highlight (car highlights))
1308 (font-lock-fontify-syntactic-anchored-keywords (car highlights)
1309 end))
1310 (setq highlights (cdr highlights))))
1311 (setq keywords (cdr keywords)))))
1313 ;;; End of Syntactic regexp fontification functions.
1315 ;;; Syntactic fontification functions.
1317 ;; These record the parse state at a particular position, always the start of a
1318 ;; line. Used to make `font-lock-fontify-syntactically-region' faster.
1319 ;; Previously, `font-lock-cache-position' was just a buffer position. However,
1320 ;; under certain situations, this occasionally resulted in mis-fontification.
1321 ;; I think the "situations" were deletion with Lazy Lock mode's deferral. sm.
1322 (defvar font-lock-cache-state nil)
1323 (defvar font-lock-cache-position nil)
1325 (defun font-lock-fontify-syntactically-region (start end &optional loudly)
1326 "Put proper face on each string and comment between START and END.
1327 START should be at the beginning of a line."
1328 (let ((cache (marker-position font-lock-cache-position))
1329 state string beg)
1330 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
1331 (goto-char start)
1333 ;; Find the state at the `beginning-of-line' before `start'.
1334 (if (eq start cache)
1335 ;; Use the cache for the state of `start'.
1336 (setq state font-lock-cache-state)
1337 ;; Find the state of `start'.
1338 (if (null font-lock-beginning-of-syntax-function)
1339 ;; Use the state at the previous cache position, if any, or
1340 ;; otherwise calculate from `point-min'.
1341 (if (or (null cache) (< start cache))
1342 (setq state (parse-partial-sexp (point-min) start))
1343 (setq state (parse-partial-sexp cache start nil nil
1344 font-lock-cache-state)))
1345 ;; Call the function to move outside any syntactic block.
1346 (funcall font-lock-beginning-of-syntax-function)
1347 (setq state (parse-partial-sexp (point) start)))
1348 ;; Cache the state and position of `start'.
1349 (setq font-lock-cache-state state)
1350 (set-marker font-lock-cache-position start))
1352 ;; If the region starts inside a string or comment, show the extent of it.
1353 (when (or (nth 3 state) (nth 4 state))
1354 (setq string (nth 3 state) beg (point))
1355 (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
1356 (put-text-property beg (point) 'face
1357 (if string
1358 font-lock-string-face
1359 font-lock-comment-face)))
1361 ;; Find each interesting place between here and `end'.
1362 (while (and (< (point) end)
1363 (progn
1364 (setq state (parse-partial-sexp (point) end nil nil state
1365 'syntax-table))
1366 (or (nth 3 state) (nth 4 state))))
1367 (setq string (nth 3 state) beg (nth 8 state))
1368 (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
1369 (put-text-property beg (point) 'face
1370 (if string
1371 font-lock-string-face
1372 font-lock-comment-face)))))
1374 ;;; End of Syntactic fontification functions.
1376 ;;; Keyword regexp fontification functions.
1378 (defsubst font-lock-apply-highlight (highlight)
1379 "Apply HIGHLIGHT following a match.
1380 HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
1381 (let* ((match (nth 0 highlight))
1382 (start (match-beginning match)) (end (match-end match))
1383 (override (nth 2 highlight)))
1384 (cond ((not start)
1385 ;; No match but we might not signal an error.
1386 (or (nth 3 highlight)
1387 (error "No match %d in highlight %S" match highlight)))
1388 ((not override)
1389 ;; Cannot override existing fontification.
1390 (or (text-property-not-all start end 'face nil)
1391 (put-text-property start end 'face (eval (nth 1 highlight)))))
1392 ((eq override t)
1393 ;; Override existing fontification.
1394 (put-text-property start end 'face (eval (nth 1 highlight))))
1395 ((eq override 'prepend)
1396 ;; Prepend to existing fontification.
1397 (font-lock-prepend-text-property start end 'face (eval (nth 1 highlight))))
1398 ((eq override 'append)
1399 ;; Append to existing fontification.
1400 (font-lock-append-text-property start end 'face (eval (nth 1 highlight))))
1401 ((eq override 'keep)
1402 ;; Keep existing fontification.
1403 (font-lock-fillin-text-property start end 'face (eval (nth 1 highlight)))))))
1405 (defsubst font-lock-fontify-anchored-keywords (keywords limit)
1406 "Fontify according to KEYWORDS until LIMIT.
1407 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1408 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1409 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1410 ;; Evaluate PRE-MATCH-FORM.
1411 (pre-match-value (eval (nth 1 keywords))))
1412 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1413 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1414 (setq limit pre-match-value)
1415 (save-excursion (end-of-line) (setq limit (point))))
1416 (save-match-data
1417 ;; Find an occurrence of `matcher' before `limit'.
1418 (while (if (stringp matcher)
1419 (re-search-forward matcher limit t)
1420 (funcall matcher limit))
1421 ;; Apply each highlight to this instance of `matcher'.
1422 (setq highlights lowdarks)
1423 (while highlights
1424 (font-lock-apply-highlight (car highlights))
1425 (setq highlights (cdr highlights)))))
1426 ;; Evaluate POST-MATCH-FORM.
1427 (eval (nth 2 keywords))))
1429 (defun font-lock-fontify-keywords-region (start end &optional loudly)
1430 "Fontify according to `font-lock-keywords' between START and END.
1431 START should be at the beginning of a line."
1432 (unless (eq (car font-lock-keywords) t)
1433 (setq font-lock-keywords (font-lock-compile-keywords font-lock-keywords)))
1434 (let ((case-fold-search font-lock-keywords-case-fold-search)
1435 (keywords (cdr font-lock-keywords))
1436 (bufname (buffer-name)) (count 0)
1437 keyword matcher highlights)
1439 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1440 (while keywords
1441 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
1442 (make-string (incf count) ?.)))
1444 ;; Find an occurrence of `matcher' from `start' to `end'.
1445 (setq keyword (car keywords) matcher (car keyword))
1446 (goto-char start)
1447 (while (if (stringp matcher)
1448 (re-search-forward matcher end t)
1449 (funcall matcher end))
1450 ;; Apply each highlight to this instance of `matcher', which may be
1451 ;; specific highlights or more keywords anchored to `matcher'.
1452 (setq highlights (cdr keyword))
1453 (while highlights
1454 (if (numberp (car (car highlights)))
1455 (font-lock-apply-highlight (car highlights))
1456 (font-lock-fontify-anchored-keywords (car highlights) end))
1457 (setq highlights (cdr highlights))))
1458 (setq keywords (cdr keywords)))))
1460 ;;; End of Keyword regexp fontification functions.
1462 ;; Various functions.
1464 (defun font-lock-compile-keywords (keywords)
1465 ;; Compile KEYWORDS into the form (t KEYWORD ...) where KEYWORD is of the
1466 ;; form (MATCHER HIGHLIGHT ...) as shown in `font-lock-keywords' doc string.
1467 (if (eq (car-safe keywords) t)
1468 keywords
1469 (cons t (mapcar 'font-lock-compile-keyword keywords))))
1471 (defun font-lock-compile-keyword (keyword)
1472 (cond ((nlistp keyword) ; MATCHER
1473 (list keyword '(0 font-lock-keyword-face)))
1474 ((eq (car keyword) 'eval) ; (eval . FORM)
1475 (font-lock-compile-keyword (eval (cdr keyword))))
1476 ((eq (car-safe (cdr keyword)) 'quote) ; (MATCHER . 'FORM)
1477 ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote.
1478 (if (symbolp (nth 2 keyword))
1479 (list (car keyword) (list 0 (cdr keyword)))
1480 (font-lock-compile-keyword (cons (car keyword) (nth 2 keyword)))))
1481 ((numberp (cdr keyword)) ; (MATCHER . MATCH)
1482 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
1483 ((symbolp (cdr keyword)) ; (MATCHER . FACENAME)
1484 (list (car keyword) (list 0 (cdr keyword))))
1485 ((nlistp (nth 1 keyword)) ; (MATCHER . HIGHLIGHT)
1486 (list (car keyword) (cdr keyword)))
1487 (t ; (MATCHER HIGHLIGHT ...)
1488 keyword)))
1490 (defun font-lock-eval-keywords (keywords)
1491 ;; Evalulate KEYWORDS if a function (funcall) or variable (eval) name.
1492 (if (listp keywords)
1493 keywords
1494 (font-lock-eval-keywords (if (fboundp keywords)
1495 (funcall keywords)
1496 (eval keywords)))))
1498 (defun font-lock-value-in-major-mode (alist)
1499 ;; Return value in ALIST for `major-mode', or ALIST if it is not an alist.
1500 ;; Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t.
1501 (if (consp alist)
1502 (cdr (or (assq major-mode alist) (assq t alist)))
1503 alist))
1505 (defun font-lock-choose-keywords (keywords level)
1506 ;; Return LEVELth element of KEYWORDS. A LEVEL of nil is equal to a
1507 ;; LEVEL of 0, a LEVEL of t is equal to (1- (length KEYWORDS)).
1508 (cond ((symbolp keywords)
1509 keywords)
1510 ((numberp level)
1511 (or (nth level keywords) (car (reverse keywords))))
1512 ((eq level t)
1513 (car (reverse keywords)))
1515 (car keywords))))
1517 (defvar font-lock-set-defaults nil) ; Whether we have set up defaults.
1519 (defun font-lock-set-defaults ()
1520 "Set fontification defaults appropriately for this mode.
1521 Sets various variables using `font-lock-defaults' (or, if nil, using
1522 `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
1523 ;; Set fontification defaults.
1524 (make-local-variable 'font-lock-fontified)
1525 ;; Set iff not previously set.
1526 (unless font-lock-set-defaults
1527 (set (make-local-variable 'font-lock-set-defaults) t)
1528 (set (make-local-variable 'font-lock-cache-state) nil)
1529 (set (make-local-variable 'font-lock-cache-position) (make-marker))
1530 (let* ((defaults (or font-lock-defaults
1531 (cdr (assq major-mode font-lock-defaults-alist))))
1532 (keywords
1533 (font-lock-choose-keywords (nth 0 defaults)
1534 (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1535 (local (cdr (assq major-mode font-lock-keywords-alist))))
1536 ;; Regexp fontification?
1537 (set (make-local-variable 'font-lock-keywords)
1538 (font-lock-compile-keywords (font-lock-eval-keywords keywords)))
1539 ;; Local fontification?
1540 (while local
1541 (font-lock-add-keywords nil (car (car local)) (cdr (car local)))
1542 (setq local (cdr local)))
1543 ;; Syntactic fontification?
1544 (when (nth 1 defaults)
1545 (set (make-local-variable 'font-lock-keywords-only) t))
1546 ;; Case fold during regexp fontification?
1547 (when (nth 2 defaults)
1548 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
1549 ;; Syntax table for regexp and syntactic fontification?
1550 (when (nth 3 defaults)
1551 (let ((slist (nth 3 defaults)))
1552 (set (make-local-variable 'font-lock-syntax-table)
1553 (copy-syntax-table (syntax-table)))
1554 (while slist
1555 ;; The character to modify may be a single CHAR or a STRING.
1556 (let ((chars (if (numberp (car (car slist)))
1557 (list (car (car slist)))
1558 (mapcar 'identity (car (car slist)))))
1559 (syntax (cdr (car slist))))
1560 (while chars
1561 (modify-syntax-entry (car chars) syntax
1562 font-lock-syntax-table)
1563 (setq chars (cdr chars)))
1564 (setq slist (cdr slist))))))
1565 ;; Syntax function for syntactic fontification?
1566 (when (nth 4 defaults)
1567 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1568 (nth 4 defaults)))
1569 ;; Variable alist?
1570 (let ((alist (nthcdr 5 defaults)))
1571 (while alist
1572 (let ((variable (car (car alist))) (value (cdr (car alist))))
1573 (unless (boundp variable)
1574 (set variable nil))
1575 (set (make-local-variable variable) value)
1576 (setq alist (cdr alist))))))))
1578 (defun font-lock-unset-defaults ()
1579 "Unset fontification defaults. See `font-lock-set-defaults'."
1580 (setq font-lock-set-defaults nil
1581 font-lock-keywords nil
1582 font-lock-keywords-only nil
1583 font-lock-keywords-case-fold-search nil
1584 font-lock-syntax-table nil
1585 font-lock-beginning-of-syntax-function nil)
1586 (let* ((defaults (or font-lock-defaults
1587 (cdr (assq major-mode font-lock-defaults-alist))))
1588 (alist (nthcdr 5 defaults)))
1589 (while alist
1590 (set (car (car alist)) (default-value (car (car alist))))
1591 (setq alist (cdr alist)))))
1593 ;;; Colour etc. support.
1595 ;; Originally these variable values were face names such as `bold' etc.
1596 ;; Now we create our own faces, but we keep these variables for compatibility
1597 ;; and they give users another mechanism for changing face appearance.
1598 ;; We now allow a FACENAME in `font-lock-keywords' to be any expression that
1599 ;; returns a face. So the easiest thing is to continue using these variables,
1600 ;; rather than sometimes evaling FACENAME and sometimes not. sm.
1601 (defvar font-lock-comment-face 'font-lock-comment-face
1602 "Face name to use for comments.")
1604 (defvar font-lock-string-face 'font-lock-string-face
1605 "Face name to use for strings.")
1607 (defvar font-lock-keyword-face 'font-lock-keyword-face
1608 "Face name to use for keywords.")
1610 (defvar font-lock-builtin-face 'font-lock-builtin-face
1611 "Face name to use for builtins.")
1613 (defvar font-lock-function-name-face 'font-lock-function-name-face
1614 "Face name to use for function names.")
1616 (defvar font-lock-variable-name-face 'font-lock-variable-name-face
1617 "Face name to use for variable names.")
1619 (defvar font-lock-type-face 'font-lock-type-face
1620 "Face name to use for type names.")
1622 (defvar font-lock-reference-face 'font-lock-reference-face
1623 "Face name to use for reference names.")
1625 (defvar font-lock-warning-face 'font-lock-warning-face
1626 "Face name to use for things that should stand out.")
1628 ;; Originally face attributes were specified via `font-lock-face-attributes'.
1629 ;; Users then changed the default face attributes by setting that variable.
1630 ;; However, we try and be back-compatible and respect its value if set except
1631 ;; for faces where M-x customize has been used to save changes for the face.
1632 (when (boundp 'font-lock-face-attributes)
1633 (let ((face-attributes font-lock-face-attributes))
1634 (while face-attributes
1635 (let* ((face-attribute (pop face-attributes))
1636 (face (car face-attribute)))
1637 ;; Rustle up a `defface' SPEC from a `font-lock-face-attributes' entry.
1638 (unless (get face 'saved-face)
1639 (let ((foreground (nth 1 face-attribute))
1640 (background (nth 2 face-attribute))
1641 (bold-p (nth 3 face-attribute))
1642 (italic-p (nth 4 face-attribute))
1643 (underline-p (nth 5 face-attribute))
1644 face-spec)
1645 (when foreground
1646 (setq face-spec (cons ':foreground (cons foreground face-spec))))
1647 (when background
1648 (setq face-spec (cons ':background (cons background face-spec))))
1649 (when bold-p
1650 (setq face-spec (append '(:bold t) face-spec)))
1651 (when italic-p
1652 (setq face-spec (append '(:italic t) face-spec)))
1653 (when underline-p
1654 (setq face-spec (append '(:underline t) face-spec)))
1655 (custom-declare-face face (list (list t face-spec)) nil)))))))
1657 ;; But now we do it the custom way. Note that `defface' will not overwrite any
1658 ;; faces declared above via `custom-declare-face'.
1659 (defface font-lock-comment-face
1660 '((((class grayscale) (background light))
1661 (:foreground "DimGray" :bold t :italic t))
1662 (((class grayscale) (background dark))
1663 (:foreground "LightGray" :bold t :italic t))
1664 (((class color) (background light)) (:foreground "Firebrick"))
1665 (((class color) (background dark)) (:foreground "OrangeRed"))
1666 (t (:bold t :italic t)))
1667 "Font Lock mode face used to highlight comments."
1668 :group 'font-lock-highlighting-faces)
1670 (defface font-lock-string-face
1671 '((((class grayscale) (background light)) (:foreground "DimGray" :italic t))
1672 (((class grayscale) (background dark)) (:foreground "LightGray" :italic t))
1673 (((class color) (background light)) (:foreground "RosyBrown"))
1674 (((class color) (background dark)) (:foreground "LightSalmon"))
1675 (t (:italic t)))
1676 "Font Lock mode face used to highlight strings."
1677 :group 'font-lock-highlighting-faces)
1679 (defface font-lock-keyword-face
1680 '((((class grayscale) (background light)) (:foreground "LightGray" :bold t))
1681 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1682 (((class color) (background light)) (:foreground "Purple"))
1683 (((class color) (background dark)) (:foreground "Cyan"))
1684 (t (:bold t)))
1685 "Font Lock mode face used to highlight keywords."
1686 :group 'font-lock-highlighting-faces)
1688 (defface font-lock-builtin-face
1689 '((((class grayscale) (background light)) (:foreground "LightGray" :bold t))
1690 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1691 (((class color) (background light)) (:foreground "Orchid"))
1692 (((class color) (background dark)) (:foreground "LightSteelBlue"))
1693 (t (:bold t)))
1694 "Font Lock mode face used to highlight builtins."
1695 :group 'font-lock-highlighting-faces)
1697 (defface font-lock-function-name-face
1698 '((((class color) (background light)) (:foreground "Blue"))
1699 (((class color) (background dark)) (:foreground "LightSkyBlue"))
1700 (t (:inverse-video t :bold t)))
1701 "Font Lock mode face used to highlight function names."
1702 :group 'font-lock-highlighting-faces)
1704 (defface font-lock-variable-name-face
1705 '((((class grayscale) (background light))
1706 (:foreground "Gray90" :bold t :italic t))
1707 (((class grayscale) (background dark))
1708 (:foreground "DimGray" :bold t :italic t))
1709 (((class color) (background light)) (:foreground "DarkGoldenrod"))
1710 (((class color) (background dark)) (:foreground "LightGoldenrod"))
1711 (t (:bold t :italic t)))
1712 "Font Lock mode face used to highlight variable names."
1713 :group 'font-lock-highlighting-faces)
1715 (defface font-lock-type-face
1716 '((((class grayscale) (background light)) (:foreground "Gray90" :bold t))
1717 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1718 (((class color) (background light)) (:foreground "ForestGreen"))
1719 (((class color) (background dark)) (:foreground "PaleGreen"))
1720 (t (:bold t :underline t)))
1721 "Font Lock mode face used to highlight types."
1722 :group 'font-lock-highlighting-faces)
1724 (defface font-lock-reference-face
1725 '((((class grayscale) (background light))
1726 (:foreground "LightGray" :bold t :underline t))
1727 (((class grayscale) (background dark))
1728 (:foreground "Gray50" :bold t :underline t))
1729 (((class color) (background light)) (:foreground "CadetBlue"))
1730 (((class color) (background dark)) (:foreground "Aquamarine"))
1731 (t (:bold t :underline t)))
1732 "Font Lock mode face used to highlight references."
1733 :group 'font-lock-highlighting-faces)
1735 (defface font-lock-warning-face
1736 '((((class color) (background light)) (:foreground "Red" :bold t))
1737 (((class color) (background dark)) (:foreground "Pink" :bold t))
1738 (t (:inverse-video t :bold t)))
1739 "Font Lock mode face used to highlight warnings."
1740 :group 'font-lock-highlighting-faces)
1742 ;;; End of Colour etc. support.
1744 ;;; Menu support.
1746 ;; This section of code is commented out because Emacs does not have real menu
1747 ;; buttons. (We can mimic them by putting "( ) " or "(X) " at the beginning of
1748 ;; the menu entry text, but with Xt it looks both ugly and embarrassingly
1749 ;; amateur.) If/When Emacs gets real menus buttons, put in menu-bar.el after
1750 ;; the entry for "Text Properties" something like:
1752 ;; (define-key menu-bar-edit-menu [font-lock]
1753 ;; '("Syntax Highlighting" . font-lock-menu))
1755 ;; and remove a single ";" from the beginning of each line in the rest of this
1756 ;; section. Probably the mechanism for telling the menu code what are menu
1757 ;; buttons and when they are on or off needs tweaking. I have assumed that the
1758 ;; mechanism is via `menu-toggle' and `menu-selected' symbol properties. sm.
1760 ;;;;###autoload
1761 ;(progn
1762 ; ;; Make the Font Lock menu.
1763 ; (defvar font-lock-menu (make-sparse-keymap "Syntax Highlighting"))
1764 ; ;; Add the menu items in reverse order.
1765 ; (define-key font-lock-menu [fontify-less]
1766 ; '("Less In Current Buffer" . font-lock-fontify-less))
1767 ; (define-key font-lock-menu [fontify-more]
1768 ; '("More In Current Buffer" . font-lock-fontify-more))
1769 ; (define-key font-lock-menu [font-lock-sep]
1770 ; '("--"))
1771 ; (define-key font-lock-menu [font-lock-mode]
1772 ; '("In Current Buffer" . font-lock-mode))
1773 ; (define-key font-lock-menu [global-font-lock-mode]
1774 ; '("In All Buffers" . global-font-lock-mode)))
1776 ;;;;###autoload
1777 ;(progn
1778 ; ;; We put the appropriate `menu-enable' etc. symbol property values on when
1779 ; ;; font-lock.el is loaded, so we don't need to autoload the three variables.
1780 ; (put 'global-font-lock-mode 'menu-toggle t)
1781 ; (put 'font-lock-mode 'menu-toggle t)
1782 ; (put 'font-lock-fontify-more 'menu-enable '(identity))
1783 ; (put 'font-lock-fontify-less 'menu-enable '(identity)))
1785 ;;; Put the appropriate symbol property values on now. See above.
1786 ;(put 'global-font-lock-mode 'menu-selected 'global-font-lock-mode))
1787 ;(put 'font-lock-mode 'menu-selected 'font-lock-mode)
1788 ;(put 'font-lock-fontify-more 'menu-enable '(nth 2 font-lock-fontify-level))
1789 ;(put 'font-lock-fontify-less 'menu-enable '(nth 1 font-lock-fontify-level))
1791 ;(defvar font-lock-fontify-level nil) ; For less/more fontification.
1793 ;(defun font-lock-fontify-level (level)
1794 ; (let ((font-lock-maximum-decoration level))
1795 ; (when font-lock-mode
1796 ; (font-lock-mode))
1797 ; (font-lock-mode)
1798 ; (when font-lock-verbose
1799 ; (message "Fontifying %s... level %d" (buffer-name) level))))
1801 ;(defun font-lock-fontify-less ()
1802 ; "Fontify the current buffer with less decoration.
1803 ;See `font-lock-maximum-decoration'."
1804 ; (interactive)
1805 ; ;; Check in case we get called interactively.
1806 ; (if (nth 1 font-lock-fontify-level)
1807 ; (font-lock-fontify-level (1- (car font-lock-fontify-level)))
1808 ; (error "No less decoration")))
1810 ;(defun font-lock-fontify-more ()
1811 ; "Fontify the current buffer with more decoration.
1812 ;See `font-lock-maximum-decoration'."
1813 ; (interactive)
1814 ; ;; Check in case we get called interactively.
1815 ; (if (nth 2 font-lock-fontify-level)
1816 ; (font-lock-fontify-level (1+ (car font-lock-fontify-level)))
1817 ; (error "No more decoration")))
1819 ;;; This should be called by `font-lock-set-defaults'.
1820 ;(defun font-lock-set-menu ()
1821 ; ;; Activate less/more fontification entries if there are multiple levels for
1822 ; ;; the current buffer. Sets `font-lock-fontify-level' to be of the form
1823 ; ;; (CURRENT-LEVEL IS-LOWER-LEVEL-P IS-HIGHER-LEVEL-P) for menu activation.
1824 ; (let ((keywords (or (nth 0 font-lock-defaults)
1825 ; (nth 1 (assq major-mode font-lock-defaults-alist))))
1826 ; (level (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1827 ; (make-local-variable 'font-lock-fontify-level)
1828 ; (if (or (symbolp keywords) (= (length keywords) 1))
1829 ; (font-lock-unset-menu)
1830 ; (cond ((eq level t)
1831 ; (setq level (1- (length keywords))))
1832 ; ((or (null level) (zerop level))
1833 ; ;; The default level is usually, but not necessarily, level 1.
1834 ; (setq level (- (length keywords)
1835 ; (length (member (eval (car keywords))
1836 ; (mapcar 'eval (cdr keywords))))))))
1837 ; (setq font-lock-fontify-level (list level (> level 1)
1838 ; (< level (1- (length keywords))))))))
1840 ;;; This should be called by `font-lock-unset-defaults'.
1841 ;(defun font-lock-unset-menu ()
1842 ; ;; Deactivate less/more fontification entries.
1843 ; (setq font-lock-fontify-level nil))
1845 ;;; End of Menu support.
1847 ;;; Various regexp information shared by several modes.
1848 ;;; Information specific to a single mode should go in its load library.
1850 ;; Font Lock support for C, C++, Objective-C and Java modes will one day be in
1851 ;; some cc-font.el (and required by cc-mode.el). However, the below function
1852 ;; should stay in font-lock.el, since it is used by other libraries. sm.
1854 (defun font-lock-match-c-style-declaration-item-and-skip-to-next (limit)
1855 "Match, and move over, any declaration/definition item after point.
1856 Matches after point, but ignores leading whitespace and `*' characters.
1857 Does not move further than LIMIT.
1859 The expected syntax of a declaration/definition item is `word' (preceded by
1860 optional whitespace and `*' characters and proceeded by optional whitespace)
1861 optionally followed by a `('. Everything following the item (but belonging to
1862 it) is expected to by skip-able by `scan-sexps', and items are expected to be
1863 separated with a `,' and to be terminated with a `;'.
1865 Thus the regexp matches after point: word (
1866 ^^^^ ^
1867 Where the match subexpressions are: 1 2
1869 The item is delimited by (match-beginning 1) and (match-end 1).
1870 If (match-beginning 2) is non-nil, the item is followed by a `('.
1872 This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
1873 (when (looking-at "[ \t*]*\\(\\sw+\\)[ \t]*\\((\\)?")
1874 (save-match-data
1875 (condition-case nil
1876 (save-restriction
1877 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
1878 (narrow-to-region (point-min) limit)
1879 (goto-char (match-end 1))
1880 ;; Move over any item value, etc., to the next item.
1881 (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)"))
1882 (goto-char (or (scan-sexps (point) 1) (point-max))))
1883 (goto-char (match-end 2)))
1884 (error t)))))
1886 ;; Lisp.
1888 (defconst lisp-font-lock-keywords-1
1889 (eval-when-compile
1890 (list
1892 ;; Definitions.
1893 (list (concat "(\\(def\\("
1894 ;; Function declarations.
1895 "\\(advice\\|alias\\|method\\|"
1896 "ine-\\(derived-mode\\|function\\|skeleton\\|widget\\)\\|"
1897 "macro\\|subst\\|un\\)\\|"
1898 ;; Variable declarations.
1899 "\\(const\\|custom\\|face\\|var\\)\\|"
1900 ;; Structure declarations.
1901 "\\(class\\|group\\|struct\\|type\\)"
1902 "\\)\\)\\>"
1903 ;; Any whitespace and defined object.
1904 "[ \t'\(]*"
1905 "\\(\\sw+\\)?")
1906 '(1 font-lock-keyword-face)
1907 '(7 (cond ((match-beginning 3) font-lock-function-name-face)
1908 ((match-beginning 5) font-lock-variable-name-face)
1909 (t font-lock-type-face))
1910 nil t))
1912 ;; Emacs Lisp autoload cookies.
1913 '("^;;;\\(###\\)\\(autoload\\)\\>"
1914 (1 font-lock-reference-face prepend)
1915 (2 font-lock-warning-face prepend))
1917 "Subdued level highlighting for Lisp modes.")
1919 (defconst lisp-font-lock-keywords-2
1920 (append lisp-font-lock-keywords-1
1921 (eval-when-compile
1922 (list
1924 ;; Control structures. Emacs Lisp forms.
1925 (cons (concat
1926 "(" (regexp-opt
1927 '("cond" "if" "while" "catch" "throw" "let" "let*"
1928 "prog" "progn" "progv" "prog1" "prog2" "prog*"
1929 "inline" "save-restriction" "save-excursion"
1930 "save-window-excursion" "save-selected-window"
1931 "save-match-data" "save-current-buffer" "unwind-protect"
1932 "condition-case" "track-mouse" "dont-compile"
1933 "eval-after-load" "eval-and-compile" "eval-when-compile"
1934 "eval-when"
1935 "with-current-buffer" "with-electric-help"
1936 "with-output-to-string" "with-output-to-temp-buffer"
1937 "with-temp-buffer" "with-temp-file"
1938 "with-timeout") t)
1939 "\\>")
1942 ;; Control structures. Common Lisp forms.
1943 (cons (concat
1944 "(" (regexp-opt
1945 '("when" "unless" "case" "ecase" "typecase" "etypecase"
1946 "loop" "do" "do*" "dotimes" "dolist"
1947 "proclaim" "declaim" "declare"
1948 "lexical-let" "lexical-let*" "flet" "labels"
1949 "return" "return-from") t)
1950 "\\>")
1953 ;; Feature symbols as references.
1954 '("(\\(featurep\\|provide\\|require\\)\\>[ \t']*\\(\\sw+\\)?"
1955 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1957 ;; Words inside \\[] tend to be for `substitute-command-keys'.
1958 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-reference-face prepend)
1960 ;; Words inside `' tend to be symbol names.
1961 '("`\\(\\sw\\sw+\\)'" 1 font-lock-reference-face prepend)
1963 ;; CLisp `:' keywords as builtins.
1964 '("\\<:\\sw\\sw+\\>" 0 font-lock-builtin-face)
1966 ;; ELisp and CLisp `&' keywords as types.
1967 '("\\<\\&\\sw+\\>" . font-lock-type-face)
1969 "Gaudy level highlighting for Lisp modes.")
1971 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
1972 "Default expressions to highlight in Lisp modes.")
1974 ;; Scheme.
1976 (defconst scheme-font-lock-keywords-1
1977 (eval-when-compile
1978 (list
1980 ;; Declarations. Hannes Haug <hannes.haug@student.uni-tuebingen.de> says
1981 ;; this works for SOS, STklos, SCOOPS, Meroon and Tiny CLOS.
1982 (list (concat "(\\(define\\("
1983 ;; Function names.
1984 "\\(\\|-\\(generic\\(\\|-procedure\\)\\|method\\)\\)\\|"
1985 ;; Macro names, as variable names. A bit dubious, this.
1986 "\\(-syntax\\)\\|"
1987 ;; Class names.
1988 "-class"
1989 "\\)\\)\\>"
1990 ;; Any whitespace and declared object.
1991 "[ \t]*(?"
1992 "\\(\\sw+\\)?")
1993 '(1 font-lock-keyword-face)
1994 '(7 (cond ((match-beginning 3) font-lock-function-name-face)
1995 ((match-beginning 6) font-lock-variable-name-face)
1996 (t font-lock-type-face))
1997 nil t))
1999 "Subdued expressions to highlight in Scheme modes.")
2001 (defconst scheme-font-lock-keywords-2
2002 (append scheme-font-lock-keywords-1
2003 (eval-when-compile
2004 (list
2006 ;; Control structures.
2007 (cons
2008 (concat
2009 "(" (regexp-opt
2010 '("begin" "call-with-current-continuation" "call/cc"
2011 "call-with-input-file" "call-with-output-file" "case" "cond"
2012 "do" "else" "for-each" "if" "lambda"
2013 "let" "let*" "let-syntax" "letrec" "letrec-syntax"
2014 ;; Hannes Haug <hannes.haug@student.uni-tuebingen.de> wants:
2015 "and" "or" "delay"
2016 ;; Stefan Monnier <stefan.monnier@epfl.ch> says don't bother:
2017 ;;"quasiquote" "quote" "unquote" "unquote-splicing"
2018 "map" "syntax" "syntax-rules") t)
2019 "\\>") 1)
2021 ;; David Fox <fox@graphics.cs.nyu.edu> for SOS/STklos class specifiers.
2022 '("\\<<\\sw+>\\>" . font-lock-type-face)
2024 ;; Scheme `:' keywords as references.
2025 '("\\<:\\sw+\\>" . font-lock-reference-face)
2027 "Gaudy expressions to highlight in Scheme modes.")
2029 (defvar scheme-font-lock-keywords scheme-font-lock-keywords-1
2030 "Default expressions to highlight in Scheme modes.")
2032 ;; TeX.
2034 ;(defvar tex-font-lock-keywords
2035 ; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
2036 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
2037 ; 2 font-lock-function-name-face)
2038 ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
2039 ; 2 font-lock-reference-face)
2040 ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
2041 ; ;; not be able to display those fonts.
2042 ; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep)
2043 ; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep)
2044 ; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face)
2045 ; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep))
2046 ; ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>.
2047 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
2048 ; 2 font-lock-function-name-face)
2049 ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
2050 ; 2 font-lock-reference-face)
2051 ; ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face)
2052 ; "\\\\\\([a-zA-Z@]+\\|.\\)"
2053 ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
2054 ; ;; not be able to display those fonts.
2055 ; ;; LaTeX2e: \emph{This is emphasized}.
2056 ; ("\\\\emph{\\([^}]+\\)}" 1 'italic keep)
2057 ; ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...}
2058 ; ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}"
2059 ; 3 (if (match-beginning 2) 'bold 'italic) keep)
2060 ; ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables.
2061 ; ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)"
2062 ; 3 (if (match-beginning 2) 'bold 'italic) keep))
2064 ;; Rewritten with the help of Alexandra Bac <abac@welcome.disi.unige.it>.
2065 (defconst tex-font-lock-keywords-1
2066 (eval-when-compile
2067 (let* (;;
2068 ;; Names of commands whose arg should be fontified as heading, etc.
2069 (headings (regexp-opt '("title" "begin" "end") t))
2070 ;; These commands have optional args.
2071 (headings-opt (regexp-opt
2072 '("chapter" "part"
2073 "section" "subsection" "subsubsection"
2074 "section*" "subsection*" "subsubsection*"
2075 "paragraph" "subparagraph" "subsubparagraph"
2076 "paragraph*" "subparagraph*" "subsubparagraph*"
2077 "newcommand" "renewcommand" "newenvironment"
2078 "newtheorem"
2079 "newcommand*" "renewcommand*" "newenvironment*"
2080 "newtheorem*")
2082 (variables (regexp-opt
2083 '("newcounter" "newcounter*" "setcounter" "addtocounter"
2084 "setlength" "addtolength" "settowidth")
2086 (includes (regexp-opt
2087 '("input" "include" "includeonly" "bibliography"
2088 "epsfig" "psfig" "epsf")
2090 (includes-opt (regexp-opt
2091 '("nofiles" "usepackage"
2092 "includegraphics" "includegraphics*")
2094 ;; Miscellany.
2095 (slash "\\\\")
2096 (opt "\\(\\[[^]]*\\]\\)?")
2097 (arg "{\\([^}]+\\)")
2098 (opt-depth (regexp-opt-depth opt))
2099 (arg-depth (regexp-opt-depth arg))
2101 (list
2103 ;; Heading args.
2104 (list (concat slash headings arg)
2105 (+ (regexp-opt-depth headings) arg-depth)
2106 'font-lock-function-name-face)
2107 (list (concat slash headings-opt opt arg)
2108 (+ (regexp-opt-depth headings-opt) opt-depth arg-depth)
2109 'font-lock-function-name-face)
2111 ;; Variable args.
2112 (list (concat slash variables arg)
2113 (+ (regexp-opt-depth variables) arg-depth)
2114 'font-lock-variable-name-face)
2116 ;; Include args.
2117 (list (concat slash includes arg)
2118 (+ (regexp-opt-depth includes) arg-depth)
2119 'font-lock-builtin-face)
2120 (list (concat slash includes-opt opt arg)
2121 (+ (regexp-opt-depth includes-opt) opt-depth arg-depth)
2122 'font-lock-builtin-face)
2124 ;; Definitions. I think.
2125 '("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)"
2126 1 font-lock-function-name-face)
2128 "Subdued expressions to highlight in TeX modes.")
2130 (defconst tex-font-lock-keywords-2
2131 (append tex-font-lock-keywords-1
2132 (eval-when-compile
2133 (let* (;;
2134 ;; Names of commands whose arg should be fontified with fonts.
2135 (bold (regexp-opt '("bf" "textbf" "textsc" "textup"
2136 "boldsymbol" "pmb") t))
2137 (italic (regexp-opt '("it" "textit" "textsl" "emph") t))
2138 (type (regexp-opt '("texttt" "textmd" "textrm" "textsf") t))
2140 ;; Names of commands whose arg should be fontified as a citation.
2141 (citations (regexp-opt
2142 '("label" "ref" "pageref" "vref" "eqref")
2144 (citations-opt (regexp-opt
2145 '("cite" "caption" "index" "glossary"
2146 "footnote" "footnotemark" "footnotetext")
2149 ;; Names of commands that should be fontified.
2150 (specials (regexp-opt
2151 '("\\"
2152 "linebreak" "nolinebreak" "pagebreak" "nopagebreak"
2153 "newline" "newpage" "clearpage" "cleardoublepage"
2154 "displaybreak" "allowdisplaybreaks" "enlargethispage")
2156 (general "\\([a-zA-Z@]+\\**\\|[^ \t\n]\\)")
2158 ;; Miscellany.
2159 (slash "\\\\")
2160 (opt "\\(\\[[^]]*\\]\\)?")
2161 (arg "{\\([^}]+\\)")
2162 (opt-depth (regexp-opt-depth opt))
2163 (arg-depth (regexp-opt-depth arg))
2165 (list
2167 ;; Citation args.
2168 (list (concat slash citations arg)
2169 (+ (regexp-opt-depth citations) arg-depth)
2170 'font-lock-reference-face)
2171 (list (concat slash citations-opt opt arg)
2172 (+ (regexp-opt-depth citations-opt) opt-depth arg-depth)
2173 'font-lock-reference-face)
2175 ;; Command names, special and general.
2176 (cons (concat slash specials) 'font-lock-warning-face)
2177 (concat slash general)
2179 ;; Font environments. It seems a bit dubious to use `bold' etc. faces
2180 ;; since we might not be able to display those fonts.
2181 (list (concat slash bold arg)
2182 (+ (regexp-opt-depth bold) arg-depth)
2183 '(quote bold) 'keep)
2184 (list (concat slash italic arg)
2185 (+ (regexp-opt-depth italic) arg-depth)
2186 '(quote italic) 'keep)
2187 (list (concat slash type arg)
2188 (+ (regexp-opt-depth type) arg-depth)
2189 '(quote bold-italic) 'keep)
2191 ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables.
2192 (list (concat "\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>"
2193 "\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)")
2194 3 '(if (match-beginning 2) 'bold 'italic) 'keep)
2195 ))))
2196 "Gaudy expressions to highlight in TeX modes.")
2198 (defvar tex-font-lock-keywords tex-font-lock-keywords-1
2199 "Default expressions to highlight in TeX modes.")
2201 ;;; User choices.
2203 ;; These provide a means to fontify types not defined by the language. Those
2204 ;; types might be the user's own or they might be generally accepted and used.
2205 ;; Generally accepted types are used to provide default variable values.
2207 (define-widget 'font-lock-extra-types-widget 'radio
2208 "Widget `:type' for members of the custom group `font-lock-extra-types'.
2209 Members should `:load' the package `font-lock' to use this widget."
2210 :args '((const :tag "none" nil)
2211 (repeat :tag "types" regexp)))
2213 (defcustom c-font-lock-extra-types '("FILE" "\\sw+_t")
2214 "*List of extra types to fontify in C mode.
2215 Each list item should be a regexp not containing word-delimiters.
2216 For example, a value of (\"FILE\" \"\\\\sw+_t\") means the word FILE and words
2217 ending in _t are treated as type names.
2219 The value of this variable is used when Font Lock mode is turned on."
2220 :type 'font-lock-extra-types-widget
2221 :group 'font-lock-extra-types)
2223 (defcustom c++-font-lock-extra-types
2224 '("[io]?\\(f\\|str\\)?stream\\(buf\\)?" "ios"
2225 "string" "rope"
2226 "list" "slist"
2227 "deque" "vector" "bit_vector"
2228 "set" "multiset"
2229 "map" "multimap"
2230 "hash\\(_\\(m\\(ap\\|ulti\\(map\\|set\\)\\)\\|set\\)\\)?"
2231 "stack" "queue" "priority_queue"
2232 "iterator" "const_iterator" "reverse_iterator" "const_reverse_iterator")
2233 "*List of extra types to fontify in C++ mode.
2234 Each list item should be a regexp not containing word-delimiters.
2235 For example, a value of (\"string\") means the word string is treated as a type
2236 name.
2238 The value of this variable is used when Font Lock mode is turned on."
2239 :type 'font-lock-extra-types-widget
2240 :group 'font-lock-extra-types)
2242 (defcustom objc-font-lock-extra-types '("Class" "BOOL" "IMP" "SEL")
2243 "*List of extra types to fontify in Objective-C mode.
2244 Each list item should be a regexp not containing word-delimiters.
2245 For example, a value of (\"Class\" \"BOOL\" \"IMP\" \"SEL\") means the words
2246 Class, BOOL, IMP and SEL are treated as type names.
2248 The value of this variable is used when Font Lock mode is turned on."
2249 :type 'font-lock-extra-types-widget
2250 :group 'font-lock-extra-types)
2252 (defcustom java-font-lock-extra-types '("[A-Z\300-\326\330-\337]\\sw+")
2253 "*List of extra types to fontify in Java mode.
2254 Each list item should be a regexp not containing word-delimiters.
2255 For example, a value of (\"[A-Z\300-\326\330-\337]\\\\sw+\") means capitalised
2256 words (and words conforming to the Java id spec) are treated as type names.
2258 The value of this variable is used when Font Lock mode is turned on."
2259 :type 'font-lock-extra-types-widget
2260 :group 'font-lock-extra-types)
2262 ;;; C.
2264 ;; [Murmur murmur murmur] Maestro, drum-roll please... [Murmur murmur murmur.]
2265 ;; Ahem. [Murmur murmur murmur] Lay-dees an Gennel-men. [Murmur murmur shhh!]
2266 ;; I am most proud and humbly honoured today [murmur murmur cough] to present
2267 ;; to you good people, the winner of the Second Millennium Award for The Most
2268 ;; Hairy Language Syntax. [Ahhh!] All rise please. [Shuffle shuffle
2269 ;; shuffle.] And a round of applause please. For... The C Language! [Roar.]
2271 ;; Thank you... You are too kind... It is with a feeling of great privilege
2272 ;; and indeed emotion [sob] that I accept this award. It has been a long hard
2273 ;; road. But we know our destiny. And our future. For we must not rest.
2274 ;; There are more tokens to overload, more shoehorn, more methodologies. But
2275 ;; more is a plus! [Ha ha ha.] And more means plus! [Ho ho ho.] The future
2276 ;; is C++! [Ohhh!] The Third Millennium Award... Will be ours! [Roar.]
2278 (defconst c-font-lock-keywords-1 nil
2279 "Subdued level highlighting for C mode.")
2281 (defconst c-font-lock-keywords-2 nil
2282 "Medium level highlighting for C mode.
2283 See also `c-font-lock-extra-types'.")
2285 (defconst c-font-lock-keywords-3 nil
2286 "Gaudy level highlighting for C mode.
2287 See also `c-font-lock-extra-types'.")
2289 (let* ((c-keywords
2290 (eval-when-compile
2291 (regexp-opt '("break" "continue" "do" "else" "for" "if" "return"
2292 "switch" "while") t)))
2293 (c-type-types
2294 `(mapconcat 'identity
2295 (cons
2296 (,@ (eval-when-compile
2297 (regexp-opt
2298 '("auto" "extern" "register" "static" "typedef" "struct"
2299 "union" "enum" "signed" "unsigned" "short" "long"
2300 "int" "char" "float" "double" "void" "volatile" "const"))))
2301 c-font-lock-extra-types)
2302 "\\|"))
2303 (c-type-depth `(regexp-opt-depth (,@ c-type-types)))
2305 (setq c-font-lock-keywords-1
2306 (list
2308 ;; These are all anchored at the beginning of line for speed.
2309 ;; Note that `c++-font-lock-keywords-1' depends on `c-font-lock-keywords-1'.
2311 ;; Fontify function name definitions (GNU style; without type on line).
2312 '("^\\(\\sw+\\)[ \t]*(" 1 font-lock-function-name-face)
2314 ;; Fontify error directives.
2315 '("^#[ \t]*error[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend)
2317 ;; Fontify filenames in #include <...> preprocessor directives as strings.
2318 '("^#[ \t]*\\(import\\|include\\)[ \t]+\\(<[^>\"\n]*>?\\)"
2319 2 font-lock-string-face)
2321 ;; Fontify function macro names.
2322 '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
2324 ;; Fontify symbol names in #elif or #if ... defined preprocessor directives.
2325 '("^#[ \t]*\\(elif\\|if\\)\\>"
2326 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
2327 (1 font-lock-builtin-face) (2 font-lock-variable-name-face nil t)))
2329 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
2330 '("^#[ \t]*\\(\\sw+\\)\\>[ \t!]*\\(\\sw+\\)?"
2331 (1 font-lock-builtin-face) (2 font-lock-variable-name-face nil t))
2334 (setq c-font-lock-keywords-2
2335 (append c-font-lock-keywords-1
2336 (list
2338 ;; Simple regexps for speed.
2340 ;; Fontify all type specifiers.
2341 `(eval .
2342 (cons (concat "\\<\\(" (,@ c-type-types) "\\)\\>") 'font-lock-type-face))
2344 ;; Fontify all builtin keywords (except case, default and goto; see below).
2345 (concat "\\<" c-keywords "\\>")
2347 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2348 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2349 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2350 ;; Anders Lindgren <andersl@csd.uu.se> points out that it is quicker to use
2351 ;; MATCH-ANCHORED to effectively anchor the regexp on the left.
2352 ;; This must come after the one for keywords and targets.
2353 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:"
2354 (beginning-of-line) (end-of-line)
2355 (1 font-lock-reference-face)))
2358 (setq c-font-lock-keywords-3
2359 (append c-font-lock-keywords-2
2361 ;; More complicated regexps for more complete highlighting for types.
2362 ;; We still have to fontify type specifiers individually, as C is so hairy.
2363 (list
2365 ;; Fontify all storage classes and type specifiers, plus their items.
2366 `(eval .
2367 (list (concat "\\<\\(" (,@ c-type-types) "\\)\\>"
2368 "\\([ \t*&]+\\sw+\\>\\)*")
2369 ;; Fontify each declaration item.
2370 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2371 ;; Start with point after all type specifiers.
2372 (list 'goto-char (list 'or (list 'match-beginning
2373 (+ (,@ c-type-depth) 2))
2374 '(match-end 1)))
2375 ;; Finish with point after first type specifier.
2376 '(goto-char (match-end 1))
2377 ;; Fontify as a variable or function name.
2378 '(1 (if (match-beginning 2)
2379 font-lock-function-name-face
2380 font-lock-variable-name-face)))))
2382 ;; Fontify structures, or typedef names, plus their items.
2383 '("\\(}\\)[ \t*]*\\sw"
2384 (font-lock-match-c-style-declaration-item-and-skip-to-next
2385 (goto-char (match-end 1)) nil
2386 (1 (if (match-beginning 2)
2387 font-lock-function-name-face
2388 font-lock-variable-name-face))))
2390 ;; Fontify anything at beginning of line as a declaration or definition.
2391 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2392 (1 font-lock-type-face)
2393 (font-lock-match-c-style-declaration-item-and-skip-to-next
2394 (goto-char (or (match-beginning 2) (match-end 1))) nil
2395 (1 (if (match-beginning 2)
2396 font-lock-function-name-face
2397 font-lock-variable-name-face))))
2401 (defvar c-font-lock-keywords c-font-lock-keywords-1
2402 "Default expressions to highlight in C mode.
2403 See also `c-font-lock-extra-types'.")
2405 ;;; C++.
2407 (defconst c++-font-lock-keywords-1 nil
2408 "Subdued level highlighting for C++ mode.")
2410 (defconst c++-font-lock-keywords-2 nil
2411 "Medium level highlighting for C++ mode.
2412 See also `c++-font-lock-extra-types'.")
2414 (defconst c++-font-lock-keywords-3 nil
2415 "Gaudy level highlighting for C++ mode.
2416 See also `c++-font-lock-extra-types'.")
2418 (defun font-lock-match-c++-style-declaration-item-and-skip-to-next (limit)
2419 ;; Regexp matches after point: word<word>::word (
2420 ;; ^^^^ ^^^^ ^^^^ ^
2421 ;; Where the match subexpressions are: 1 3 5 6
2423 ;; Item is delimited by (match-beginning 1) and (match-end 1).
2424 ;; If (match-beginning 3) is non-nil, that part of the item incloses a `<>'.
2425 ;; If (match-beginning 5) is non-nil, that part of the item follows a `::'.
2426 ;; If (match-beginning 6) is non-nil, the item is followed by a `('.
2427 (when (looking-at (eval-when-compile
2428 (concat
2429 ;; Skip any leading whitespace.
2430 "[ \t*&]*"
2431 ;; This is `c++-type-spec' from below. (Hint hint!)
2432 "\\(\\sw+\\)" ; The instance?
2433 "\\([ \t]*<\\([^>\n]+\\)[ \t*&]*>\\)?" ; Or template?
2434 "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)*" ; Or member?
2435 ;; Match any trailing parenthesis.
2436 "[ \t]*\\((\\)?")))
2437 (save-match-data
2438 (condition-case nil
2439 (save-restriction
2440 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
2441 (narrow-to-region (point-min) limit)
2442 (goto-char (match-end 1))
2443 ;; Move over any item value, etc., to the next item.
2444 (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)"))
2445 (goto-char (or (scan-sexps (point) 1) (point-max))))
2446 (goto-char (match-end 2)))
2447 (error t)))))
2449 (let* ((c++-keywords
2450 (eval-when-compile
2451 (regexp-opt
2452 '("break" "continue" "do" "else" "for" "if" "return" "switch"
2453 "while" "asm" "catch" "delete" "new" "sizeof" "this" "throw" "try"
2454 ;; Eric Hopper <hopper@omnifarious.mn.org> says these are new.
2455 "static_cast" "dynamic_cast" "const_cast" "reinterpret_cast") t)))
2456 (c++-operators
2457 (eval-when-compile
2458 (regexp-opt
2459 ;; Taken from Stroustrup, minus keywords otherwise fontified.
2460 '("+" "-" "*" "/" "%" "^" "&" "|" "~" "!" "=" "<" ">" "+=" "-="
2461 "*=" "/=" "%=" "^=" "&=" "|=" "<<" ">>" ">>=" "<<=" "==" "!="
2462 "<=" ">=" "&&" "||" "++" "--" "->*" "," "->" "[]" "()"))))
2463 (c++-type-types
2464 `(mapconcat 'identity
2465 (cons
2466 (,@ (eval-when-compile
2467 (regexp-opt
2468 '("auto" "extern" "register" "static" "typedef" "struct"
2469 "union" "enum" "signed" "unsigned" "short" "long"
2470 "int" "char" "float" "double" "void" "volatile" "const"
2471 "inline" "friend" "bool" "virtual" "complex" "template"
2472 "namespace" "using"
2473 ;; Mark Mitchell <mmitchell@usa.net> says these are new.
2474 "explicit" "mutable"))))
2475 c++-font-lock-extra-types)
2476 "\\|"))
2478 ;; A brave attempt to match templates following a type and/or match
2479 ;; class membership. See and sync the above function
2480 ;; `font-lock-match-c++-style-declaration-item-and-skip-to-next'.
2481 (c++-type-suffix (concat "\\([ \t]*<\\([^>\n]+\\)[ \t*&]*>\\)?"
2482 "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)*"))
2483 ;; If the string is a type, it may be followed by the cruft above.
2484 (c++-type-spec (concat "\\(\\sw+\\)\\>" c++-type-suffix))
2486 ;; Parenthesis depth of user-defined types not forgetting their cruft.
2487 (c++-type-depth `(regexp-opt-depth
2488 (concat (,@ c++-type-types) (,@ c++-type-suffix))))
2490 (setq c++-font-lock-keywords-1
2491 (append
2493 ;; The list `c-font-lock-keywords-1' less that for function names.
2494 (cdr c-font-lock-keywords-1)
2495 (list
2497 ;; Class names etc.
2498 (list (concat "\\<\\(class\\|public\\|private\\|protected\\)\\>[ \t]*"
2499 "\\(" c++-type-spec "\\)?")
2500 '(1 font-lock-type-face)
2501 '(3 (if (match-beginning 6)
2502 font-lock-type-face
2503 font-lock-function-name-face) nil t)
2504 '(5 font-lock-function-name-face nil t)
2505 '(7 font-lock-function-name-face nil t))
2507 ;; Fontify function name definitions, possibly incorporating class names.
2508 (list (concat "^" c++-type-spec "[ \t]*(")
2509 '(1 (if (or (match-beginning 2) (match-beginning 4))
2510 font-lock-type-face
2511 font-lock-function-name-face))
2512 '(3 font-lock-function-name-face nil t)
2513 '(5 font-lock-function-name-face nil t))
2516 (setq c++-font-lock-keywords-2
2517 (append c++-font-lock-keywords-1
2518 (list
2520 ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading.
2521 `(eval .
2522 (cons (concat "\\<\\(" (,@ c++-type-types) "\\)\\>")
2523 'font-lock-type-face))
2525 ;; Fontify operator overloading.
2526 (list (concat "\\<\\(operator\\)\\>[ \t]*\\(" c++-operators "\\)?")
2527 '(1 font-lock-keyword-face)
2528 '(2 font-lock-builtin-face nil t))
2530 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2531 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2532 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2533 ;; This must come after the one for keywords and targets.
2534 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:\\($\\|[^:]\\)"
2535 (beginning-of-line) (end-of-line)
2536 (1 font-lock-reference-face)))
2538 ;; Fontify other builtin keywords.
2539 (concat "\\<" c++-keywords "\\>")
2541 ;; Eric Hopper <hopper@omnifarious.mn.org> says `true' and `false' are new.
2542 '("\\<\\(false\\|true\\)\\>" . font-lock-reference-face)
2545 (setq c++-font-lock-keywords-3
2546 (append c++-font-lock-keywords-2
2548 ;; More complicated regexps for more complete highlighting for types.
2549 (list
2551 ;; Fontify all storage classes and type specifiers, plus their items.
2552 `(eval .
2553 (list (concat "\\<\\(" (,@ c++-type-types) "\\)\\>" (,@ c++-type-suffix)
2554 "\\([ \t*&]+" (,@ c++-type-spec) "\\)*")
2555 ;; Fontify each declaration item.
2556 (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next
2557 ;; Start with point after all type specifiers.
2558 (list 'goto-char (list 'or (list 'match-beginning
2559 (+ (,@ c++-type-depth) 2))
2560 '(match-end 1)))
2561 ;; Finish with point after first type specifier.
2562 '(goto-char (match-end 1))
2563 ;; Fontify as a variable or function name.
2564 '(1 (cond ((or (match-beginning 2) (match-beginning 4))
2565 font-lock-type-face)
2566 ((match-beginning 6) font-lock-function-name-face)
2567 (t font-lock-variable-name-face)))
2568 '(3 font-lock-function-name-face nil t)
2569 '(5 (if (match-beginning 6)
2570 font-lock-function-name-face
2571 font-lock-variable-name-face) nil t))))
2573 ;; Fontify structures, or typedef names, plus their items.
2574 '("\\(}\\)[ \t*]*\\sw"
2575 (font-lock-match-c++-style-declaration-item-and-skip-to-next
2576 (goto-char (match-end 1)) nil
2577 (1 (if (match-beginning 6)
2578 font-lock-function-name-face
2579 font-lock-variable-name-face))))
2581 ;; Fontify anything at beginning of line as a declaration or definition.
2582 (list (concat "^\\(" c++-type-spec "[ \t*&]*\\)+")
2583 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
2584 (goto-char (match-beginning 1))
2585 (goto-char (match-end 1))
2586 (1 (cond ((or (match-beginning 2) (match-beginning 4))
2587 font-lock-type-face)
2588 ((match-beginning 6) font-lock-function-name-face)
2589 (t font-lock-variable-name-face)))
2590 (3 font-lock-function-name-face nil t)
2591 (5 (if (match-beginning 6)
2592 font-lock-function-name-face
2593 font-lock-variable-name-face) nil t)))
2597 (defvar c++-font-lock-keywords c++-font-lock-keywords-1
2598 "Default expressions to highlight in C++ mode.
2599 See also `c++-font-lock-extra-types'.")
2601 ;;; Objective-C.
2603 (defconst objc-font-lock-keywords-1 nil
2604 "Subdued level highlighting for Objective-C mode.")
2606 (defconst objc-font-lock-keywords-2 nil
2607 "Medium level highlighting for Objective-C mode.
2608 See also `objc-font-lock-extra-types'.")
2610 (defconst objc-font-lock-keywords-3 nil
2611 "Gaudy level highlighting for Objective-C mode.
2612 See also `objc-font-lock-extra-types'.")
2614 ;; Regexps written with help from Stephen Peters <speters@us.oracle.com> and
2615 ;; Jacques Duthen Prestataire <duthen@cegelec-red.fr>.
2616 (let* ((objc-keywords
2617 (eval-when-compile
2618 (regexp-opt '("break" "continue" "do" "else" "for" "if" "return"
2619 "switch" "while" "sizeof" "self" "super") t)))
2620 (objc-type-types
2621 `(mapconcat 'identity
2622 (cons
2623 (,@ (eval-when-compile
2624 (regexp-opt
2625 '("auto" "extern" "register" "static" "typedef" "struct"
2626 "union" "enum" "signed" "unsigned" "short" "long"
2627 "int" "char" "float" "double" "void" "volatile" "const"
2628 "id" "oneway" "in" "out" "inout" "bycopy" "byref"))))
2629 objc-font-lock-extra-types)
2630 "\\|"))
2631 (objc-type-depth `(regexp-opt-depth (,@ objc-type-types)))
2633 (setq objc-font-lock-keywords-1
2634 (append
2636 ;; The list `c-font-lock-keywords-1' less that for function names.
2637 (cdr c-font-lock-keywords-1)
2638 (list
2640 ;; Fontify compiler directives.
2641 '("@\\(\\sw+\\)\\>"
2642 (1 font-lock-keyword-face)
2643 ("\\=[ \t:<(,]*\\(\\sw+\\)" nil nil
2644 (1 font-lock-function-name-face)))
2646 ;; Fontify method names and arguments. Oh Lordy!
2647 ;; First, on the same line as the function declaration.
2648 '("^[+-][ \t]*\\(PRIVATE\\)?[ \t]*\\((\\([^)\n]+\\))\\)?[ \t]*\\(\\sw+\\)"
2649 (1 font-lock-type-face nil t)
2650 (3 font-lock-type-face nil t)
2651 (4 font-lock-function-name-face)
2652 ("\\=[ \t]*\\(\\sw+\\)?:[ \t]*\\((\\([^)\n]+\\))\\)?[ \t]*\\(\\sw+\\)"
2653 nil nil
2654 (1 font-lock-function-name-face nil t)
2655 (3 font-lock-type-face nil t)
2656 (4 font-lock-variable-name-face)))
2657 ;; Second, on lines following the function declaration.
2658 '(":" ("^[ \t]*\\(\\sw+\\)?:[ \t]*\\((\\([^)\n]+\\))\\)?[ \t]*\\(\\sw+\\)"
2659 (beginning-of-line) (end-of-line)
2660 (1 font-lock-function-name-face nil t)
2661 (3 font-lock-type-face nil t)
2662 (4 font-lock-variable-name-face)))
2665 (setq objc-font-lock-keywords-2
2666 (append objc-font-lock-keywords-1
2667 (list
2669 ;; Simple regexps for speed.
2671 ;; Fontify all type specifiers.
2672 `(eval .
2673 (cons (concat "\\<\\(" (,@ objc-type-types) "\\)\\>")
2674 'font-lock-type-face))
2676 ;; Fontify all builtin keywords (except case, default and goto; see below).
2677 (concat "\\<" objc-keywords "\\>")
2679 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2680 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2681 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2682 ;; Fontify tags iff sole statement on line, otherwise we detect selectors.
2683 ;; This must come after the one for keywords and targets.
2684 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2685 (beginning-of-line) (end-of-line)
2686 (1 font-lock-reference-face)))
2688 ;; Fontify null object pointers.
2689 '("\\<[Nn]il\\>" . font-lock-reference-face)
2692 (setq objc-font-lock-keywords-3
2693 (append objc-font-lock-keywords-2
2695 ;; More complicated regexps for more complete highlighting for types.
2696 ;; We still have to fontify type specifiers individually, as C is so hairy.
2697 (list
2699 ;; Fontify all storage classes and type specifiers, plus their items.
2700 `(eval .
2701 (list (concat "\\<\\(" (,@ objc-type-types) "\\)\\>"
2702 "\\([ \t*&]+\\sw+\\>\\)*")
2703 ;; Fontify each declaration item.
2704 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2705 ;; Start with point after all type specifiers.
2706 (list 'goto-char (list 'or (list 'match-beginning
2707 (+ (,@ objc-type-depth) 2))
2708 '(match-end 1)))
2709 ;; Finish with point after first type specifier.
2710 '(goto-char (match-end 1))
2711 ;; Fontify as a variable or function name.
2712 '(1 (if (match-beginning 2)
2713 font-lock-function-name-face
2714 font-lock-variable-name-face)))))
2716 ;; Fontify structures, or typedef names, plus their items.
2717 '("\\(}\\)[ \t*]*\\sw"
2718 (font-lock-match-c-style-declaration-item-and-skip-to-next
2719 (goto-char (match-end 1)) nil
2720 (1 (if (match-beginning 2)
2721 font-lock-function-name-face
2722 font-lock-variable-name-face))))
2724 ;; Fontify anything at beginning of line as a declaration or definition.
2725 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2726 (1 font-lock-type-face)
2727 (font-lock-match-c-style-declaration-item-and-skip-to-next
2728 (goto-char (or (match-beginning 2) (match-end 1))) nil
2729 (1 (if (match-beginning 2)
2730 font-lock-function-name-face
2731 font-lock-variable-name-face))))
2735 (defvar objc-font-lock-keywords objc-font-lock-keywords-1
2736 "Default expressions to highlight in Objective-C mode.
2737 See also `objc-font-lock-extra-types'.")
2739 ;;; Java.
2741 (defconst java-font-lock-keywords-1 nil
2742 "Subdued level highlighting for Java mode.")
2744 (defconst java-font-lock-keywords-2 nil
2745 "Medium level highlighting for Java mode.
2746 See also `java-font-lock-extra-types'.")
2748 (defconst java-font-lock-keywords-3 nil
2749 "Gaudy level highlighting for Java mode.
2750 See also `java-font-lock-extra-types'.")
2752 ;; Regexps written with help from Fred White <fwhite@bbn.com> and
2753 ;; Anders Lindgren <andersl@csd.uu.se>.
2754 (let* ((java-keywords
2755 (eval-when-compile
2756 (regexp-opt
2757 '("catch" "do" "else" "super" "this" "finally" "for" "if"
2758 ;; Anders Lindgren <andersl@csd.uu.se> says these have gone.
2759 ;; "cast" "byvalue" "future" "generic" "operator" "var"
2760 ;; "inner" "outer" "rest"
2761 "interface" "return" "switch" "throw" "try" "while") t)))
2763 ;; These are immediately followed by an object name.
2764 (java-minor-types
2765 (eval-when-compile
2766 (regexp-opt '("boolean" "char" "byte" "short" "int" "long"
2767 "float" "double" "void"))))
2769 ;; These are eventually followed by an object name.
2770 (java-major-types
2771 (eval-when-compile
2772 (regexp-opt
2773 '("abstract" "const" "final" "synchronized" "transient" "static"
2774 ;; Anders Lindgren <andersl@csd.uu.se> says this has gone.
2775 ;; "threadsafe"
2776 "volatile" "public" "private" "protected" "native"))))
2778 ;; Random types immediately followed by an object name.
2779 (java-other-types
2780 '(mapconcat 'identity (cons "\\sw+\\.\\sw+" java-font-lock-extra-types)
2781 "\\|"))
2782 (java-other-depth `(regexp-opt-depth (,@ java-other-types)))
2784 (setq java-font-lock-keywords-1
2785 (list
2787 ;; Fontify class names.
2788 '("\\<\\(class\\)\\>[ \t]*\\(\\sw+\\)?"
2789 (1 font-lock-type-face) (2 font-lock-function-name-face nil t))
2791 ;; Fontify package names in import directives.
2792 '("\\<\\(import\\|package\\)\\>[ \t]*\\(\\sw+\\)?"
2793 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2796 (setq java-font-lock-keywords-2
2797 (append java-font-lock-keywords-1
2798 (list
2800 ;; Fontify all builtin type specifiers.
2801 (cons (concat "\\<\\(" java-minor-types "\\|" java-major-types "\\)\\>")
2802 'font-lock-type-face)
2804 ;; Fontify all builtin keywords (except below).
2805 (concat "\\<" java-keywords "\\>")
2807 ;; Fontify keywords and targets, and case default/goto tags.
2808 (list "\\<\\(break\\|case\\|continue\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2809 '(1 font-lock-keyword-face) '(2 font-lock-reference-face nil t))
2810 ;; This must come after the one for keywords and targets.
2811 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:"
2812 (beginning-of-line) (end-of-line)
2813 (1 font-lock-reference-face)))
2815 ;; Fontify keywords and types; the first can be followed by a type list.
2816 (list (concat "\\<\\("
2817 "implements\\|throws\\|"
2818 "\\(extends\\|instanceof\\|new\\)"
2819 "\\)\\>[ \t]*\\(\\sw+\\)?")
2820 '(1 font-lock-keyword-face) '(3 font-lock-type-face nil t)
2821 '("\\=[ \t]*,[ \t]*\\(\\sw+\\)"
2822 (if (match-beginning 2) (goto-char (match-end 2))) nil
2823 (1 font-lock-type-face)))
2825 ;; Fontify all constants.
2826 '("\\<\\(false\\|null\\|true\\)\\>" . font-lock-reference-face)
2828 ;; Javadoc tags within comments.
2829 '("@\\(author\\|exception\\|return\\|see\\|version\\)\\>"
2830 (1 font-lock-reference-face prepend))
2831 '("@\\(param\\)\\>[ \t]*\\(\\sw+\\)?"
2832 (1 font-lock-reference-face prepend)
2833 (2 font-lock-variable-name-face prepend t))
2836 (setq java-font-lock-keywords-3
2837 (append java-font-lock-keywords-2
2839 ;; More complicated regexps for more complete highlighting for types.
2840 ;; We still have to fontify type specifiers individually, as Java is hairy.
2841 (list
2843 ;; Fontify random types in casts.
2844 `(eval .
2845 (list (concat "(\\(" (,@ java-other-types) "\\))"
2846 "[ \t]*\\(\\sw\\|[\"\(]\\)")
2847 ;; Fontify the type name.
2848 '(1 font-lock-type-face)))
2850 ;; Fontify random types immediately followed by an item or items.
2851 `(eval .
2852 (list (concat "\\<\\(" (,@ java-other-types) "\\)\\>"
2853 "\\([ \t]*\\[[ \t]*\\]\\)*"
2854 "[ \t]*\\sw")
2855 ;; Fontify the type name.
2856 '(1 font-lock-type-face)))
2857 `(eval .
2858 (list (concat "\\<\\(" (,@ java-other-types) "\\)\\>"
2859 "\\([ \t]*\\[[ \t]*\\]\\)*"
2860 "\\([ \t]*\\sw\\)")
2861 ;; Fontify each declaration item.
2862 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2863 ;; Start and finish with point after the type specifier.
2864 (list 'goto-char (list 'match-beginning
2865 (+ (,@ java-other-depth) 3)))
2866 (list 'goto-char (list 'match-beginning
2867 (+ (,@ java-other-depth) 3)))
2868 ;; Fontify as a variable or function name.
2869 '(1 (if (match-beginning 2)
2870 font-lock-function-name-face
2871 font-lock-variable-name-face)))))
2873 ;; Fontify those that are immediately followed by an item or items.
2874 (list (concat "\\<\\(" java-minor-types "\\)\\>"
2875 "\\([ \t]*\\[[ \t]*\\]\\)*")
2876 ;; Fontify each declaration item.
2877 '(font-lock-match-c-style-declaration-item-and-skip-to-next
2878 ;; Start and finish with point after the type specifier.
2879 nil (goto-char (match-end 0))
2880 ;; Fontify as a variable or function name.
2881 (1 (if (match-beginning 2)
2882 font-lock-function-name-face
2883 font-lock-variable-name-face))))
2885 ;; Fontify those that are eventually followed by an item or items.
2886 (list (concat "\\<\\(" java-major-types "\\)\\>"
2887 "\\([ \t]+\\sw+\\>"
2888 "\\([ \t]*\\[[ \t]*\\]\\)*"
2889 "\\)*")
2890 ;; Fontify each declaration item.
2891 '(font-lock-match-c-style-declaration-item-and-skip-to-next
2892 ;; Start with point after all type specifiers.
2893 (goto-char (or (match-beginning 5) (match-end 1)))
2894 ;; Finish with point after first type specifier.
2895 (goto-char (match-end 1))
2896 ;; Fontify as a variable or function name.
2897 (1 (if (match-beginning 2)
2898 font-lock-function-name-face
2899 font-lock-variable-name-face))))
2903 (defvar java-font-lock-keywords java-font-lock-keywords-1
2904 "Default expressions to highlight in Java mode.
2905 See also `java-font-lock-extra-types'.")
2907 ;; Install ourselves:
2909 (unless (assq 'font-lock-mode minor-mode-alist)
2910 (push '(font-lock-mode nil) minor-mode-alist))
2912 ;; Provide ourselves:
2914 (provide 'font-lock)
2916 ;;; font-lock.el ends here