(#includes): Allow compilation with only Xaw.
[emacs.git] / lisp / font-lock.el
blob7da2c0f56524bdc616887c3251ed2acb60b7ab7e
1 ;;; font-lock.el --- Electric font lock mode
3 ;; Copyright (C) 1992-1999 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 :link '(custom-manual "(elisp)Font Lock Mode")
214 :group 'faces)
216 (defgroup font-lock-highlighting-faces nil
217 "Faces for highlighting text."
218 :prefix "font-lock-"
219 :group 'font-lock)
221 (defgroup font-lock-extra-types nil
222 "Extra mode-specific type names for highlighting declarations."
223 :group 'font-lock)
225 ;; Define support mode groups here to impose `font-lock' group order.
226 (defgroup fast-lock nil
227 "Font Lock support mode to cache fontification."
228 :link '(custom-manual "(emacs)Support Modes")
229 :load 'fast-lock
230 :group 'font-lock)
232 (defgroup lazy-lock nil
233 "Font Lock support mode to fontify lazily."
234 :link '(custom-manual "(emacs)Support Modes")
235 :load 'lazy-lock
236 :group 'font-lock)
238 (defgroup jit-lock nil
239 "Font Lock support mode to fontify just-in-time."
240 :link '(custom-manual "(emacs)Support Modes")
241 :version "21.1"
242 :load 'jit-lock
243 :group 'font-lock)
245 ;; User variables.
247 (defcustom font-lock-maximum-size 256000
248 "*Maximum size of a buffer for buffer fontification.
249 Only buffers less than this can be fontified when Font Lock mode is turned on.
250 If nil, means size is irrelevant.
251 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
252 where MAJOR-MODE is a symbol or t (meaning the default). For example:
253 ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576))
254 means that the maximum size is 250K for buffers in C or C++ modes, one megabyte
255 for buffers in Rmail mode, and size is irrelevant otherwise."
256 :type '(choice (const :tag "none" nil)
257 (integer :tag "size")
258 (repeat :menu-tag "mode specific" :tag "mode specific"
259 :value ((t . nil))
260 (cons :tag "Instance"
261 (radio :tag "Mode"
262 (const :tag "all" t)
263 (symbol :tag "name"))
264 (radio :tag "Size"
265 (const :tag "none" nil)
266 (integer :tag "size")))))
267 :group 'font-lock)
269 (defcustom font-lock-maximum-decoration t
270 "*Maximum decoration level for fontification.
271 If nil, use the default decoration (typically the minimum available).
272 If t, use the maximum decoration available.
273 If a number, use that level of decoration (or if not available the maximum).
274 If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
275 where MAJOR-MODE is a symbol or t (meaning the default). For example:
276 ((c-mode . t) (c++-mode . 2) (t . 1))
277 means use the maximum decoration available for buffers in C mode, level 2
278 decoration for buffers in C++ mode, and level 1 decoration otherwise."
279 :type '(choice (const :tag "default" nil)
280 (const :tag "maximum" t)
281 (integer :tag "level" 1)
282 (repeat :menu-tag "mode specific" :tag "mode specific"
283 :value ((t . t))
284 (cons :tag "Instance"
285 (radio :tag "Mode"
286 (const :tag "all" t)
287 (symbol :tag "name"))
288 (radio :tag "Decoration"
289 (const :tag "default" nil)
290 (const :tag "maximum" t)
291 (integer :tag "level" 1)))))
292 :group 'font-lock)
294 (defcustom font-lock-verbose 0
295 "*If non-nil, means show status messages for buffer fontification.
296 If a number, only buffers greater than this size have fontification messages."
297 :type '(choice (const :tag "never" nil)
298 (other :tag "always" t)
299 (integer :tag "size"))
300 :group 'font-lock)
302 ;; Fontification variables:
304 (defvar font-lock-keywords nil
305 "A list of the keywords to highlight.
306 Each element should have one of these forms:
308 MATCHER
309 (MATCHER . MATCH)
310 (MATCHER . FACENAME)
311 (MATCHER . HIGHLIGHT)
312 (MATCHER HIGHLIGHT ...)
313 (eval . FORM)
315 where HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
317 FORM is an expression, whose value should be a keyword element, evaluated when
318 the keyword is (first) used in a buffer. This feature can be used to provide a
319 keyword that can only be generated when Font Lock mode is actually turned on.
321 For highlighting single items, for example each instance of the word \"foo\",
322 typically only MATCH-HIGHLIGHT is required.
323 However, if an item or (typically) items are to be highlighted following the
324 instance of another item (the anchor), for example each instance of the
325 word \"bar\" following the word \"anchor\" then MATCH-ANCHORED may be required.
327 MATCH-HIGHLIGHT should be of the form:
329 (MATCH FACENAME OVERRIDE LAXMATCH)
331 where MATCHER can be either the regexp to search for, or the function name to
332 call to make the search (called with one argument, the limit of the search) and
333 return non-nil if it succeeds (and set `match-data' appropriately).
334 MATCHER regexps can be generated via the function `regexp-opt'. MATCH is the
335 subexpression of MATCHER to be highlighted. MATCH can be calculated via the
336 function `regexp-opt-depth'. FACENAME is an expression whose value is the face
337 name to use. Face default attributes can be modified via \\[customize].
339 OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification can
340 be overwritten. If `keep', only parts not already fontified are highlighted.
341 If `prepend' or `append', existing fontification is merged with the new, in
342 which the new or existing fontification, respectively, takes precedence.
343 If LAXMATCH is non-nil, no error is signaled if there is no MATCH in MATCHER.
345 For example, an element of the form highlights (if not already highlighted):
347 \"\\\\\\=<foo\\\\\\=>\" discrete occurrences of \"foo\" in the value of the
348 variable `font-lock-keyword-face'.
349 (\"fu\\\\(bar\\\\)\" . 1) substring \"bar\" within all occurrences of \"fubar\" in
350 the value of `font-lock-keyword-face'.
351 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'.
352 (\"foo\\\\|bar\" 0 foo-bar-face t)
353 occurrences of either \"foo\" or \"bar\" in the value
354 of `foo-bar-face', even if already highlighted.
355 (fubar-match 1 fubar-face)
356 the first subexpression within all occurrences of
357 whatever the function `fubar-match' finds and matches
358 in the value of `fubar-face'.
360 MATCH-ANCHORED should be of the form:
362 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
364 where MATCHER is a regexp to search for or the function name to call to make
365 the search, as for MATCH-HIGHLIGHT above, but with one exception; see below.
366 PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
367 the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be
368 used to initialise before, and cleanup after, MATCHER is used. Typically,
369 PRE-MATCH-FORM is used to move to some position relative to the original
370 MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might
371 be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER.
373 For example, an element of the form highlights (if not already highlighted):
375 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
377 discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
378 discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
379 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is
380 initially searched for starting from the end of the match of \"anchor\", and
381 searching for subsequent instance of \"anchor\" resumes from where searching
382 for \"item\" concluded.)
384 The above-mentioned exception is as follows. The limit of the MATCHER search
385 defaults to the end of the line after PRE-MATCH-FORM is evaluated.
386 However, if PRE-MATCH-FORM returns a position greater than the position after
387 PRE-MATCH-FORM is evaluated, that position is used as the limit of the search.
388 It is generally a bad idea to return a position greater than the end of the
389 line, i.e., cause the MATCHER search to span lines.
391 These regular expressions should not match text which spans lines. While
392 \\[font-lock-fontify-buffer] handles multi-line patterns correctly, updating
393 when you edit the buffer does not, since it considers text one line at a time.
395 This variable is set by major modes via the variable `font-lock-defaults'.
396 Be careful when composing regexps for this list; a poorly written pattern can
397 dramatically slow things down!")
399 ;; This variable is used by mode packages that support Font Lock mode by
400 ;; defining their own keywords to use for `font-lock-keywords'. (The mode
401 ;; command should make it buffer-local and set it to provide the set up.)
402 (defvar font-lock-defaults nil
403 "Defaults for Font Lock mode specified by the major mode.
404 Defaults should be of the form:
406 (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN ...)
408 KEYWORDS may be a symbol (a variable or function whose value is the keywords to
409 use for fontification) or a list of symbols. If KEYWORDS-ONLY is non-nil,
410 syntactic fontification (strings and comments) is not performed.
411 If CASE-FOLD is non-nil, the case of the keywords is ignored when fontifying.
412 If SYNTAX-ALIST is non-nil, it should be a list of cons pairs of the form
413 \(CHAR-OR-STRING . STRING) used to set the local Font Lock syntax table, for
414 keyword and syntactic fontification (see `modify-syntax-entry').
416 If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
417 backwards outside any enclosing syntactic block, for syntactic fontification.
418 Typical values are `beginning-of-line' (i.e., the start of the line is known to
419 be outside a syntactic block), or `beginning-of-defun' for programming modes or
420 `backward-paragraph' for textual modes (i.e., the mode-dependent function is
421 known to move outside a syntactic block). If nil, the beginning of the buffer
422 is used as a position outside of a syntactic block, in the worst case.
424 These item elements are used by Font Lock mode to set the variables
425 `font-lock-keywords', `font-lock-keywords-only',
426 `font-lock-keywords-case-fold-search', `font-lock-syntax-table' and
427 `font-lock-beginning-of-syntax-function', respectively.
429 Further item elements are alists of the form (VARIABLE . VALUE) and are in no
430 particular order. Each VARIABLE is made buffer-local before set to VALUE.
432 Currently, appropriate variables include `font-lock-mark-block-function'.
433 If this is non-nil, it should be a function with no args used to mark any
434 enclosing block of text, for fontification via \\[font-lock-fontify-block].
435 Typical values are `mark-defun' for programming modes or `mark-paragraph' for
436 textual modes (i.e., the mode-dependent function is known to put point and mark
437 around a text block relevant to that mode).
439 Other variables include that for syntactic keyword fontification,
440 `font-lock-syntactic-keywords'
441 and those for buffer-specialised fontification functions,
442 `font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function',
443 `font-lock-fontify-region-function', `font-lock-unfontify-region-function',
444 `font-lock-inhibit-thing-lock' and `font-lock-maximum-size'.")
446 ;; This variable is used where font-lock.el itself supplies the keywords.
447 (defvar font-lock-defaults-alist
448 (let (;; We use `beginning-of-defun', rather than nil, for SYNTAX-BEGIN.
449 ;; Thus the calculation of the cache is usually faster but not
450 ;; infallible, so we risk mis-fontification. sm.
451 (c-mode-defaults
452 '((c-font-lock-keywords c-font-lock-keywords-1
453 c-font-lock-keywords-2 c-font-lock-keywords-3)
454 nil nil ((?_ . "w")) beginning-of-defun
455 (font-lock-mark-block-function . mark-defun)))
456 (c++-mode-defaults
457 '((c++-font-lock-keywords c++-font-lock-keywords-1
458 c++-font-lock-keywords-2 c++-font-lock-keywords-3)
459 nil nil ((?_ . "w")) beginning-of-defun
460 (font-lock-mark-block-function . mark-defun)))
461 (objc-mode-defaults
462 '((objc-font-lock-keywords objc-font-lock-keywords-1
463 objc-font-lock-keywords-2 objc-font-lock-keywords-3)
464 nil nil ((?_ . "w") (?$ . "w")) nil
465 (font-lock-mark-block-function . mark-defun)))
466 (java-mode-defaults
467 '((java-font-lock-keywords java-font-lock-keywords-1
468 java-font-lock-keywords-2 java-font-lock-keywords-3)
469 nil nil ((?_ . "w") (?$ . "w")) nil
470 (font-lock-mark-block-function . mark-defun)))
471 (lisp-mode-defaults
472 '((lisp-font-lock-keywords
473 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
474 nil nil (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
475 (font-lock-mark-block-function . mark-defun)))
476 ;; For TeX modes we could use `backward-paragraph' for the same reason.
477 ;; But we don't, because paragraph breaks are arguably likely enough to
478 ;; occur within a genuine syntactic block to make it too risky.
479 ;; However, we do specify a MARK-BLOCK function as that cannot result
480 ;; in a mis-fontification even if it might not fontify enough. sm.
481 (tex-mode-defaults
482 '((tex-font-lock-keywords
483 tex-font-lock-keywords-1 tex-font-lock-keywords-2)
484 nil nil ((?$ . "\"")) nil
485 (font-lock-mark-block-function . mark-paragraph)))
487 (list
488 (cons 'c-mode c-mode-defaults)
489 (cons 'c++-mode c++-mode-defaults)
490 (cons 'objc-mode objc-mode-defaults)
491 (cons 'java-mode java-mode-defaults)
492 (cons 'emacs-lisp-mode lisp-mode-defaults)
493 (cons 'latex-mode tex-mode-defaults)
494 (cons 'lisp-mode lisp-mode-defaults)
495 (cons 'lisp-interaction-mode lisp-mode-defaults)
496 (cons 'plain-tex-mode tex-mode-defaults)
497 (cons 'slitex-mode tex-mode-defaults)
498 (cons 'tex-mode tex-mode-defaults)))
499 "Alist of fall-back Font Lock defaults for major modes.
500 Each item should be a list of the form:
502 (MAJOR-MODE . FONT-LOCK-DEFAULTS)
504 where MAJOR-MODE is a symbol and FONT-LOCK-DEFAULTS is a list of default
505 settings. See the variable `font-lock-defaults', which takes precedence.")
507 (defvar font-lock-keywords-alist nil
508 "*Alist of `font-lock-keywords' local to a `major-mode'.
509 This is normally set via `font-lock-add-keywords'.")
511 (defvar font-lock-keywords-only nil
512 "*Non-nil means Font Lock should not fontify comments or strings.
513 This is normally set via `font-lock-defaults'.")
515 (defvar font-lock-keywords-case-fold-search nil
516 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
517 This is normally set via `font-lock-defaults'.")
519 (defvar font-lock-syntactic-keywords nil
520 "A list of the syntactic keywords to highlight.
521 Can be the list or the name of a function or variable whose value is the list.
522 See `font-lock-keywords' for a description of the form of this list;
523 the differences are listed below. MATCH-HIGHLIGHT should be of the form:
525 (MATCH SYNTAX OVERRIDE LAXMATCH)
527 where SYNTAX can be of the form (SYNTAX-CODE . MATCHING-CHAR), the name of a
528 syntax table, or an expression whose value is such a form or a syntax table.
529 OVERRIDE cannot be `prepend' or `append'.
531 For example, an element of the form highlights syntactically:
533 (\"\\\\$\\\\(#\\\\)\" 1 (1 . nil))
535 a hash character when following a dollar character, with a SYNTAX-CODE of
536 1 (meaning punctuation syntax). Assuming that the buffer syntax table does
537 specify hash characters to have comment start syntax, the element will only
538 highlight hash characters that do not follow dollar characters as comments
539 syntactically.
541 (\"\\\\('\\\\).\\\\('\\\\)\"
542 (1 (7 . ?'))
543 (2 (7 . ?')))
545 both single quotes which surround a single character, with a SYNTAX-CODE of
546 7 (meaning string quote syntax) and a MATCHING-CHAR of a single quote (meaning
547 a single quote matches a single quote). Assuming that the buffer syntax table
548 does not specify single quotes to have quote syntax, the element will only
549 highlight single quotes of the form 'c' as strings syntactically.
550 Other forms, such as foo'bar or 'fubar', will not be highlighted as strings.
552 This is normally set via `font-lock-defaults'.")
554 (defvar font-lock-syntax-table nil
555 "Non-nil means use this syntax table for fontifying.
556 If this is nil, the major mode's syntax table is used.
557 This is normally set via `font-lock-defaults'.")
559 ;; If this is nil, we only use the beginning of the buffer if we can't use
560 ;; `font-lock-cache-position' and `font-lock-cache-state'.
561 (defvar font-lock-beginning-of-syntax-function nil
562 "*Non-nil means use this function to move back outside of a syntactic block.
563 When called with no args it should leave point at the beginning of any
564 enclosing syntactic block.
565 If this is nil, the beginning of the buffer is used (in the worst case).
566 This is normally set via `font-lock-defaults'.")
568 (defvar font-lock-mark-block-function nil
569 "*Non-nil means use this function to mark a block of text.
570 When called with no args it should leave point at the beginning of any
571 enclosing textual block and mark at the end.
572 This is normally set via `font-lock-defaults'.")
574 (defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
575 "Function to use for fontifying the buffer.
576 This is normally set via `font-lock-defaults'.")
578 (defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
579 "Function to use for unfontifying the buffer.
580 This is used when turning off Font Lock mode.
581 This is normally set via `font-lock-defaults'.")
583 (defvar font-lock-fontify-region-function 'font-lock-default-fontify-region
584 "Function to use for fontifying a region.
585 It should take two args, the beginning and end of the region, and an optional
586 third arg VERBOSE. If non-nil, the function should print status messages.
587 This is normally set via `font-lock-defaults'.")
589 (defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
590 "Function to use for unfontifying a region.
591 It should take two args, the beginning and end of the region.
592 This is normally set via `font-lock-defaults'.")
594 (defvar font-lock-inhibit-thing-lock nil
595 "List of Font Lock mode related modes that should not be turned on.
596 Currently, valid mode names are `fast-lock-mode', `jit-lock-mode' and
597 `lazy-lock-mode'. This is normally set via `font-lock-defaults'.")
599 (defvar font-lock-multiline 'undecided
600 "Whether font-lock should cater to multiline keywords.
601 If nil, don't try to handle multiline patterns.
602 If t, always handle multiline patterns.
603 If `undecided', don't try to handle multiline patterns until you see one.
604 Major/minor modes can set this variable if they know which option applies.")
606 (defvar font-lock-mode nil) ; Whether we are turned on/modeline.
607 (defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
609 ;;;###autoload
610 (defvar font-lock-mode-hook nil
611 "Function or functions to run on entry to Font Lock mode.")
613 ;; Font Lock mode.
615 (eval-when-compile
617 ;; We don't do this at the top-level as we only use non-autoloaded macros.
618 (require 'cl)
620 ;; Borrowed from lazy-lock.el.
621 ;; We use this to preserve or protect things when modifying text properties.
622 (defmacro save-buffer-state (varlist &rest body)
623 "Bind variables according to VARLIST and eval BODY restoring buffer state."
624 (` (let* ((,@ (append varlist
625 '((modified (buffer-modified-p)) (buffer-undo-list t)
626 (inhibit-read-only t) (inhibit-point-motion-hooks t)
627 before-change-functions after-change-functions
628 deactivate-mark buffer-file-name buffer-file-truename))))
629 (,@ body)
630 (when (and (not modified) (buffer-modified-p))
631 (set-buffer-modified-p nil)))))
632 (put 'save-buffer-state 'lisp-indent-function 1)
634 ;; Shut up the byte compiler.
635 (defvar global-font-lock-mode) ; Now a defcustom.
636 (defvar font-lock-face-attributes) ; Obsolete but respected if set.
637 (defvar font-lock-string-face) ; Used in syntactic fontification.
638 (defvar font-lock-comment-face))
640 ;;;###autoload
641 (defun font-lock-mode (&optional arg)
642 "Toggle Font Lock mode.
643 With arg, turn Font Lock mode on if and only if arg is positive.
645 When Font Lock mode is enabled, text is fontified as you type it:
647 - Comments are displayed in `font-lock-comment-face';
648 - Strings are displayed in `font-lock-string-face';
649 - Certain other expressions are displayed in other faces according to the
650 value of the variable `font-lock-keywords'.
652 You can enable Font Lock mode in any major mode automatically by turning on in
653 the major mode's hook. For example, put in your ~/.emacs:
655 (add-hook 'c-mode-hook 'turn-on-font-lock)
657 Alternatively, you can use Global Font Lock mode to automagically turn on Font
658 Lock mode in buffers whose major mode supports it and whose major mode is one
659 of `font-lock-global-modes'. For example, put in your ~/.emacs:
661 (global-font-lock-mode t)
663 There are a number of support modes that may be used to speed up Font Lock mode
664 in various ways, specified via the variable `font-lock-support-mode'. Where
665 major modes support different levels of fontification, you can use the variable
666 `font-lock-maximum-decoration' to specify which level you generally prefer.
667 When you turn Font Lock mode on/off the buffer is fontified/defontified, though
668 fontification occurs only if the buffer is less than `font-lock-maximum-size'.
670 For example, to specify that Font Lock mode use use Lazy Lock mode as a support
671 mode and use maximum levels of fontification, put in your ~/.emacs:
673 (setq font-lock-support-mode 'lazy-lock-mode)
674 (setq font-lock-maximum-decoration t)
676 To add your own highlighting for some major mode, and modify the highlighting
677 selected automatically via the variable `font-lock-maximum-decoration', you can
678 use `font-lock-add-keywords'.
680 To fontify a buffer, without turning on Font Lock mode and regardless of buffer
681 size, you can use \\[font-lock-fontify-buffer].
683 To fontify a block (the function or paragraph containing point, or a number of
684 lines around point), perhaps because modification on the current line caused
685 syntactic change on other lines, you can use \\[font-lock-fontify-block].
687 See the variable `font-lock-defaults-alist' for the Font Lock mode default
688 settings. You can set your own default settings for some mode, by setting a
689 buffer local value for `font-lock-defaults', via its mode hook."
690 (interactive "P")
691 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
692 ;; batch job) or if the buffer is invisible (the name starts with a space).
693 (let ((on-p (and (not noninteractive)
694 (not (eq (aref (buffer-name) 0) ?\ ))
695 (if arg
696 (> (prefix-numeric-value arg) 0)
697 (not font-lock-mode)))))
698 (set (make-local-variable 'font-lock-mode) on-p)
699 ;; Turn on Font Lock mode.
700 (when on-p
701 (make-local-hook 'after-change-functions)
702 (add-hook 'after-change-functions 'font-lock-after-change-function nil t)
703 (font-lock-set-defaults)
704 (font-lock-turn-on-thing-lock)
705 (run-hooks 'font-lock-mode-hook)
706 ;; Fontify the buffer if we have to.
707 (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size)))
708 (cond (font-lock-fontified
709 nil)
710 ((or (null max-size) (> max-size (buffer-size)))
711 (font-lock-fontify-buffer))
712 (font-lock-verbose
713 (message "Fontifying %s...buffer too big" (buffer-name))))))
714 ;; Turn off Font Lock mode.
715 (unless on-p
716 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
717 (font-lock-unfontify-buffer)
718 (font-lock-turn-off-thing-lock)
719 (font-lock-unset-defaults))
720 (force-mode-line-update)))
722 ;;;###autoload
723 (defun turn-on-font-lock ()
724 "Turn on Font Lock mode conditionally.
725 Turn on only if the terminal can display it."
726 (when (and (not font-lock-mode) (or window-system (tty-display-color-p)))
727 (font-lock-mode)))
729 ;;;###autoload
730 (defun font-lock-add-keywords (mode keywords &optional append)
731 "Add highlighting KEYWORDS for MODE.
732 MODE should be a symbol, the major mode command name, such as `c-mode'
733 or nil. If nil, highlighting keywords are added for the current buffer.
734 KEYWORDS should be a list; see the variable `font-lock-keywords'.
735 By default they are added at the beginning of the current highlighting list.
736 If optional argument APPEND is `set', they are used to replace the current
737 highlighting list. If APPEND is any other non-nil value, they are added at the
738 end of the current highlighting list.
740 For example:
742 (font-lock-add-keywords 'c-mode
743 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
744 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . font-lock-keyword-face)))
746 adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
747 comments, and to fontify `and', `or' and `not' words as keywords.
749 Note that some modes have specialised support for additional patterns, e.g.,
750 see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
751 `objc-font-lock-extra-types' and `java-font-lock-extra-types'."
752 (cond (mode
753 ;; If MODE is non-nil, add the KEYWORDS and APPEND spec to
754 ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
755 (let ((spec (cons keywords append)) cell)
756 (if (setq cell (assq mode font-lock-keywords-alist))
757 (setcdr cell (append (cdr cell) (list spec)))
758 (push (list mode spec) font-lock-keywords-alist))))
759 (font-lock-mode
760 ;; Otherwise if Font Lock mode is on, set or add the keywords now.
761 (if (eq append 'set)
762 (setq font-lock-keywords keywords)
763 (font-lock-remove-keywords nil keywords)
764 (let ((old (if (eq (car-safe font-lock-keywords) t)
765 (cdr font-lock-keywords)
766 font-lock-keywords)))
767 (setq font-lock-keywords (if append
768 (append old keywords)
769 (append keywords old))))))))
771 ;;;###autoload
772 (defun font-lock-remove-keywords (mode keywords)
773 "Remove highlighting KEYWORDS from the current buffer.
774 A non-nil MODE is currently unsupported."
775 (setq font-lock-keywords (copy-list font-lock-keywords))
776 (dolist (keyword keywords)
777 (setq font-lock-keywords
778 (delete keyword
779 (delete (font-lock-compile-keyword keyword)
780 font-lock-keywords)))))
783 ;;; Global Font Lock mode.
785 ;; A few people have hassled in the past for a way to make it easier to turn on
786 ;; Font Lock mode, without the user needing to know for which modes s/he has to
787 ;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always
788 ;; balked at that way, as I see it as just re-moulding the same problem in
789 ;; another form. That is; some person would still have to keep track of which
790 ;; modes (which may not even be distributed with Emacs) support Font Lock mode.
791 ;; The list would always be out of date. And that person might have to be me.
793 ;; Implementation.
795 ;; In a previous discussion the following hack came to mind. It is a gross
796 ;; hack, but it generally works. We use the convention that major modes start
797 ;; by calling the function `kill-all-local-variables', which in turn runs
798 ;; functions on the hook variable `change-major-mode-hook'. We attach our
799 ;; function `font-lock-change-major-mode' to that hook. Of course, when this
800 ;; hook is run, the major mode is in the process of being changed and we do not
801 ;; know what the final major mode will be. So, `font-lock-change-major-mode'
802 ;; only (a) notes the name of the current buffer, and (b) adds our function
803 ;; `turn-on-font-lock-if-enabled' to the hook variables `find-file-hooks' and
804 ;; `post-command-hook' (for buffers that are not visiting files). By the time
805 ;; the functions on the first of these hooks to be run are run, the new major
806 ;; mode is assumed to be in place. This way we get a Font Lock function run
807 ;; when a major mode is turned on, without knowing major modes or their hooks.
809 ;; Naturally this requires that (a) major modes run `kill-all-local-variables',
810 ;; as they are supposed to do, and (b) the major mode is in place after the
811 ;; file is visited or the command that ran `kill-all-local-variables' has
812 ;; finished, whichever the sooner. Arguably, any major mode that does not
813 ;; follow the convension (a) is broken, and I can't think of any reason why (b)
814 ;; would not be met (except `gnudoit' on non-files). However, it is not clean.
816 ;; Probably the cleanest solution is to have each major mode function run some
817 ;; hook, e.g., `major-mode-hook', but maybe implementing that change is
818 ;; impractical. I am personally against making `setq' a macro or be advised,
819 ;; or have a special function such as `set-major-mode', but maybe someone can
820 ;; come up with another solution?
822 ;; User interface.
824 ;; Although Global Font Lock mode is a pseudo-mode, I think that the user
825 ;; interface should conform to the usual Emacs convention for modes, i.e., a
826 ;; command to toggle the feature (`global-font-lock-mode') with a variable for
827 ;; finer control of the mode's behaviour (`font-lock-global-modes').
829 ;; The feature should not be enabled by loading font-lock.el, since other
830 ;; mechanisms for turning on Font Lock mode, such as M-x font-lock-mode RET or
831 ;; (add-hook 'c-mode-hook 'turn-on-font-lock), would cause Font Lock mode to be
832 ;; turned on everywhere. That would not be intuitive or informative because
833 ;; loading a file tells you nothing about the feature or how to control it. It
834 ;; would also be contrary to the Principle of Least Surprise. sm.
836 (defvar font-lock-buffers nil) ; For remembering buffers.
838 ;;;###autoload
839 (defun global-font-lock-mode (&optional arg message)
840 "Toggle Global Font Lock mode.
841 With prefix ARG, turn Global Font Lock mode on if and only if ARG is positive.
842 Displays a message saying whether the mode is on or off if MESSAGE is non-nil.
843 Returns the new status of Global Font Lock mode (non-nil means on).
845 When Global Font Lock mode is enabled, Font Lock mode is automagically
846 turned on in a buffer if its major mode is one of `font-lock-global-modes'."
847 (interactive "P\np")
848 (let ((on-p (if arg
849 (> (prefix-numeric-value arg) 0)
850 (not global-font-lock-mode))))
851 (cond (on-p
852 (add-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
853 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
854 (setq font-lock-buffers (buffer-list)))
856 (remove-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
857 (mapcar (function (lambda (buffer)
858 (with-current-buffer buffer
859 (when font-lock-mode
860 (font-lock-mode)))))
861 (buffer-list))))
862 (when message
863 (message "Global Font Lock mode %s." (if on-p "enabled" "disabled")))
864 (setq global-font-lock-mode on-p)))
866 ;; This variable was originally a `defvar' to keep track of
867 ;; whether Global Font Lock mode was turned on or not. As a `defcustom' with
868 ;; special `:set' and `:require' forms, we can provide custom mode control.
869 ;;;###autoload
870 (defcustom global-font-lock-mode nil
871 "Toggle Global Font Lock mode.
872 When Global Font Lock mode is enabled, Font Lock mode is automagically
873 turned on in a buffer if its major mode is one of `font-lock-global-modes'.
874 Setting this variable directly does not take effect;
875 use either \\[customize] or the function `global-font-lock-mode'."
876 :set (lambda (symbol value)
877 (global-font-lock-mode (or value 0)))
878 :initialize 'custom-initialize-default
879 :type 'boolean
880 :group 'font-lock
881 :require 'font-lock)
883 (defcustom font-lock-global-modes t
884 "*Modes for which Font Lock mode is automagically turned on.
885 Global Font Lock mode is controlled by the command `global-font-lock-mode'.
886 If nil, means no modes have Font Lock mode automatically turned on.
887 If t, all modes that support Font Lock mode have it automatically turned on.
888 If a list, it should be a list of `major-mode' symbol names for which Font Lock
889 mode should be automatically turned on. The sense of the list is negated if it
890 begins with `not'. For example:
891 (c-mode c++-mode)
892 means that Font Lock mode is turned on for buffers in C and C++ modes only."
893 :type '(choice (const :tag "none" nil)
894 (const :tag "all" t)
895 (set :menu-tag "mode specific" :tag "modes"
896 :value (not)
897 (const :tag "Except" not)
898 (repeat :inline t (symbol :tag "mode"))))
899 :group 'font-lock)
901 (defun font-lock-change-major-mode ()
902 ;; Turn off Font Lock mode if it's on.
903 (when font-lock-mode
904 (font-lock-mode))
905 ;; Gross hack warning: Delicate readers should avert eyes now.
906 ;; Something is running `kill-all-local-variables', which generally means the
907 ;; major mode is being changed. Run `turn-on-font-lock-if-enabled' after the
908 ;; file is visited or the current command has finished.
909 (when global-font-lock-mode
910 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
911 (add-to-list 'font-lock-buffers (current-buffer))))
913 (defun turn-on-font-lock-if-enabled ()
914 ;; Gross hack warning: Delicate readers should avert eyes now.
915 ;; Turn on Font Lock mode if it's supported by the major mode and enabled by
916 ;; the user.
917 (remove-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
918 (while font-lock-buffers
919 (when (buffer-live-p (car font-lock-buffers))
920 (save-excursion
921 (set-buffer (car font-lock-buffers))
922 (when (and (or font-lock-defaults
923 (assq major-mode font-lock-defaults-alist))
924 (or (eq font-lock-global-modes t)
925 (if (eq (car-safe font-lock-global-modes) 'not)
926 (not (memq major-mode (cdr font-lock-global-modes)))
927 (memq major-mode font-lock-global-modes))))
928 (let (inhibit-quit)
929 (turn-on-font-lock)))))
930 (setq font-lock-buffers (cdr font-lock-buffers))))
932 (add-hook 'change-major-mode-hook 'font-lock-change-major-mode)
934 ;;; End of Global Font Lock mode.
936 ;;; Font Lock Support mode.
938 ;; This is the code used to interface font-lock.el with any of its add-on
939 ;; packages, and provide the user interface. Packages that have their own
940 ;; local buffer fontification functions (see below) may have to call
941 ;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
942 ;; themselves.
944 (defcustom font-lock-support-mode 'jit-lock-mode
945 "*Support mode for Font Lock mode.
946 Support modes speed up Font Lock mode by being choosy about when fontification
947 occurs. Known support modes are Fast Lock mode (symbol `fast-lock-mode'),
948 Lazy Lock mode (symbol `lazy-lock-mode'), and Just-in-time Lock mode (symbol
949 `jit-lock-mode'. See those modes for more info.
950 If nil, means support for Font Lock mode is never performed.
951 If a symbol, use that support mode.
952 If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
953 where MAJOR-MODE is a symbol or t (meaning the default). For example:
954 ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
955 means that Fast Lock mode is used to support Font Lock mode for buffers in C or
956 C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
958 The value of this variable is used when Font Lock mode is turned on."
959 :type '(choice (const :tag "none" nil)
960 (const :tag "fast lock" fast-lock-mode)
961 (const :tag "lazy lock" lazy-lock-mode)
962 (const :tag "jit lock" jit-lock-mode)
963 (repeat :menu-tag "mode specific" :tag "mode specific"
964 :value ((t . lazy-lock-mode))
965 (cons :tag "Instance"
966 (radio :tag "Mode"
967 (const :tag "all" t)
968 (symbol :tag "name"))
969 (radio :tag "Support"
970 (const :tag "none" nil)
971 (const :tag "fast lock" fast-lock-mode)
972 (const :tag "lazy lock" lazy-lock-mode)
973 (const :tag "JIT lock" jit-lock-mode)))
975 :group 'font-lock)
977 (defvar fast-lock-mode nil)
978 (defvar lazy-lock-mode nil)
979 (defvar jit-lock-mode nil)
981 (defun font-lock-turn-on-thing-lock ()
982 (let ((thing-mode (font-lock-value-in-major-mode font-lock-support-mode)))
983 (cond ((eq thing-mode 'fast-lock-mode)
984 (fast-lock-mode t))
985 ((eq thing-mode 'lazy-lock-mode)
986 (lazy-lock-mode t))
987 ((eq thing-mode 'jit-lock-mode)
988 (jit-lock-mode t)))))
990 (defun font-lock-turn-off-thing-lock ()
991 (cond (fast-lock-mode
992 (fast-lock-mode nil))
993 (jit-lock-mode
994 (jit-lock-mode nil))
995 (lazy-lock-mode
996 (lazy-lock-mode nil))))
998 (defun font-lock-after-fontify-buffer ()
999 (cond (fast-lock-mode
1000 (fast-lock-after-fontify-buffer))
1001 (jit-lock-mode
1002 (jit-lock-after-fontify-buffer))
1003 (lazy-lock-mode
1004 (lazy-lock-after-fontify-buffer))))
1006 (defun font-lock-after-unfontify-buffer ()
1007 (cond (fast-lock-mode
1008 (fast-lock-after-unfontify-buffer))
1009 (jit-lock-mode
1010 (jit-lock-after-unfontify-buffer))
1011 (lazy-lock-mode
1012 (lazy-lock-after-unfontify-buffer))))
1014 ;;; End of Font Lock Support mode.
1016 ;;; Fontification functions.
1018 ;; Rather than the function, e.g., `font-lock-fontify-region' containing the
1019 ;; code to fontify a region, the function runs the function whose name is the
1020 ;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally,
1021 ;; the value of this variable is, e.g., `font-lock-default-fontify-region'
1022 ;; which does contain the code to fontify a region. However, the value of the
1023 ;; variable could be anything and thus, e.g., `font-lock-fontify-region' could
1024 ;; do anything. The indirection of the fontification functions gives major
1025 ;; modes the capability of modifying the way font-lock.el fontifies. Major
1026 ;; modes can modify the values of, e.g., `font-lock-fontify-region-function',
1027 ;; via the variable `font-lock-defaults'.
1029 ;; For example, Rmail mode sets the variable `font-lock-defaults' so that
1030 ;; font-lock.el uses its own function for buffer fontification. This function
1031 ;; makes fontification be on a message-by-message basis and so visiting an
1032 ;; RMAIL file is much faster. A clever implementation of the function might
1033 ;; fontify the headers differently than the message body. (It should, and
1034 ;; correspondingly for Mail mode, but I can't be bothered to do the work. Can
1035 ;; you?) This hints at a more interesting use...
1037 ;; Languages that contain text normally contained in different major modes
1038 ;; could define their own fontification functions that treat text differently
1039 ;; depending on its context. For example, Perl mode could arrange that here
1040 ;; docs are fontified differently than Perl code. Or Yacc mode could fontify
1041 ;; rules one way and C code another. Neat!
1043 ;; A further reason to use the fontification indirection feature is when the
1044 ;; default syntactual fontification, or the default fontification in general,
1045 ;; is not flexible enough for a particular major mode. For example, perhaps
1046 ;; comments are just too hairy for `font-lock-fontify-syntactically-region' to
1047 ;; cope with. You need to write your own version of that function, e.g.,
1048 ;; `hairy-fontify-syntactically-region', and make your own version of
1049 ;; `hairy-fontify-region' call that function before calling
1050 ;; `font-lock-fontify-keywords-region' for the normal regexp fontification
1051 ;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el
1052 ;; would call your region fontification function instead of its own. For
1053 ;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line
1054 ;; directives correctly and cleanly. (It is the same problem as fontifying
1055 ;; multi-line strings and comments; regexps are not appropriate for the job.)
1057 ;;;###autoload
1058 (defun font-lock-fontify-buffer ()
1059 "Fontify the current buffer the way the function `font-lock-mode' would."
1060 (interactive)
1061 (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
1062 (funcall font-lock-fontify-buffer-function)))
1064 (defun font-lock-unfontify-buffer ()
1065 (funcall font-lock-unfontify-buffer-function))
1067 (defun font-lock-fontify-region (beg end &optional loudly)
1068 (funcall font-lock-fontify-region-function beg end loudly))
1070 (defun font-lock-unfontify-region (beg end)
1071 (funcall font-lock-unfontify-region-function beg end))
1073 (defun font-lock-default-fontify-buffer ()
1074 (let ((verbose (if (numberp font-lock-verbose)
1075 (> (buffer-size) font-lock-verbose)
1076 font-lock-verbose)))
1077 (with-temp-message
1078 (when verbose
1079 (format "Fontifying %s..." (buffer-name)))
1080 ;; Make sure we have the right `font-lock-keywords' etc.
1081 (unless font-lock-mode
1082 (font-lock-set-defaults))
1083 ;; Make sure we fontify etc. in the whole buffer.
1084 (save-restriction
1085 (widen)
1086 (condition-case nil
1087 (save-excursion
1088 (save-match-data
1089 (font-lock-fontify-region (point-min) (point-max) verbose)
1090 (font-lock-after-fontify-buffer)
1091 (setq font-lock-fontified t)))
1092 ;; We don't restore the old fontification, so it's best to unfontify.
1093 (quit (font-lock-unfontify-buffer))))
1094 ;; Make sure we undo `font-lock-keywords' etc.
1095 (unless font-lock-mode
1096 (font-lock-unset-defaults)))))
1098 (defun font-lock-default-unfontify-buffer ()
1099 ;; Make sure we unfontify etc. in the whole buffer.
1100 (save-restriction
1101 (widen)
1102 (font-lock-unfontify-region (point-min) (point-max))
1103 (font-lock-after-unfontify-buffer)
1104 (setq font-lock-fontified nil)))
1106 (defun font-lock-default-fontify-region (beg end loudly)
1107 (save-buffer-state
1108 ((parse-sexp-lookup-properties font-lock-syntactic-keywords)
1109 (old-syntax-table (syntax-table)))
1110 (unwind-protect
1111 (save-restriction
1112 (widen)
1113 ;; Use the fontification syntax table, if any.
1114 (when font-lock-syntax-table
1115 (set-syntax-table font-lock-syntax-table))
1116 ;; check to see if we should expand the beg/end area for
1117 ;; proper multiline matches
1118 (setq beg (if (get-text-property beg 'font-lock-multiline)
1119 ;; if the text-property is non-nil, (1+ beg)
1120 ;; is valid. We need to use (1+ beg) for the
1121 ;; case where (get-text-property (1- beg)) is nil
1122 ;; in which case we want to keep BEG but
1123 ;; previous-single-property-change will return
1124 ;; the previous change (if any) rather than
1125 ;; the one at BEG.
1126 (or (previous-single-property-change
1127 (1+ beg) 'font-lock-multiline)
1128 (point-min))
1129 beg))
1130 (setq end (or (text-property-any end (point-max)
1131 'font-lock-multiline nil)
1132 (point-max)))
1133 ;; Now do the fontification.
1134 (font-lock-unfontify-region beg end)
1135 (when font-lock-syntactic-keywords
1136 (font-lock-fontify-syntactic-keywords-region beg end))
1137 (unless font-lock-keywords-only
1138 (font-lock-fontify-syntactically-region beg end loudly))
1139 (font-lock-fontify-keywords-region beg end loudly))
1140 ;; Clean up.
1141 (set-syntax-table old-syntax-table))))
1143 ;; The following must be rethought, since keywords can override fontification.
1144 ; ;; Now scan for keywords, but not if we are inside a comment now.
1145 ; (or (and (not font-lock-keywords-only)
1146 ; (let ((state (parse-partial-sexp beg end nil nil
1147 ; font-lock-cache-state)))
1148 ; (or (nth 4 state) (nth 7 state))))
1149 ; (font-lock-fontify-keywords-region beg end))
1151 (defun font-lock-default-unfontify-region (beg end)
1152 (save-buffer-state nil
1153 (remove-text-properties beg end
1154 (if font-lock-syntactic-keywords
1155 '(face nil syntax-table nil font-lock-multiline nil)
1156 '(face nil font-lock-multiline nil)))))
1158 ;; Called when any modification is made to buffer text.
1159 (defun font-lock-after-change-function (beg end old-len)
1160 (let ((inhibit-point-motion-hooks t))
1161 (save-excursion
1162 (save-match-data
1163 ;; Rescan between start of lines enclosing the region.
1164 (font-lock-fontify-region
1165 (progn (goto-char beg) (beginning-of-line) (point))
1166 (progn (goto-char end) (forward-line 1) (point)))))))
1168 (defun font-lock-fontify-block (&optional arg)
1169 "Fontify some lines the way `font-lock-fontify-buffer' would.
1170 The lines could be a function or paragraph, or a specified number of lines.
1171 If ARG is given, fontify that many lines before and after point, or 16 lines if
1172 no ARG is given and `font-lock-mark-block-function' is nil.
1173 If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
1174 delimit the region to fontify."
1175 (interactive "P")
1176 (let ((inhibit-point-motion-hooks t) font-lock-beginning-of-syntax-function
1177 deactivate-mark)
1178 ;; Make sure we have the right `font-lock-keywords' etc.
1179 (if (not font-lock-mode) (font-lock-set-defaults))
1180 (save-excursion
1181 (save-match-data
1182 (condition-case error-data
1183 (if (or arg (not font-lock-mark-block-function))
1184 (let ((lines (if arg (prefix-numeric-value arg) 16)))
1185 (font-lock-fontify-region
1186 (save-excursion (forward-line (- lines)) (point))
1187 (save-excursion (forward-line lines) (point))))
1188 (funcall font-lock-mark-block-function)
1189 (font-lock-fontify-region (point) (mark)))
1190 ((error quit) (message "Fontifying block...%s" error-data)))))))
1192 (define-key facemenu-keymap "\M-g" 'font-lock-fontify-block)
1194 ;;; End of Fontification functions.
1196 ;;; Additional text property functions.
1198 ;; The following text property functions should be builtins. This means they
1199 ;; should be written in C and put with all the other text property functions.
1200 ;; In the meantime, those that are used by font-lock.el are defined in Lisp
1201 ;; below and given a `font-lock-' prefix. Those that are not used are defined
1202 ;; in Lisp below and commented out. sm.
1204 (defun font-lock-prepend-text-property (start end prop value &optional object)
1205 "Prepend to one property of the text from START to END.
1206 Arguments PROP and VALUE specify the property and value to prepend to the value
1207 already in place. The resulting property values are always lists.
1208 Optional argument OBJECT is the string or buffer containing the text."
1209 (let ((val (if (listp value) value (list value))) next prev)
1210 (while (/= start end)
1211 (setq next (next-single-property-change start prop object end)
1212 prev (get-text-property start prop object))
1213 (put-text-property start next prop
1214 (append val (if (listp prev) prev (list prev)))
1215 object)
1216 (setq start next))))
1218 (defun font-lock-append-text-property (start end prop value &optional object)
1219 "Append to one property of the text from START to END.
1220 Arguments PROP and VALUE specify the property and value to append to the value
1221 already in place. The resulting property values are always lists.
1222 Optional argument OBJECT is the string or buffer containing the text."
1223 (let ((val (if (listp value) value (list value))) next prev)
1224 (while (/= start end)
1225 (setq next (next-single-property-change start prop object end)
1226 prev (get-text-property start prop object))
1227 (put-text-property start next prop
1228 (append (if (listp prev) prev (list prev)) val)
1229 object)
1230 (setq start next))))
1232 (defun font-lock-fillin-text-property (start end prop value &optional object)
1233 "Fill in one property of the text from START to END.
1234 Arguments PROP and VALUE specify the property and value to put where none are
1235 already in place. Therefore existing property values are not overwritten.
1236 Optional argument OBJECT is the string or buffer containing the text."
1237 (let ((start (text-property-any start end prop nil object)) next)
1238 (while start
1239 (setq next (next-single-property-change start prop object end))
1240 (put-text-property start next prop value object)
1241 (setq start (text-property-any next end prop nil object)))))
1243 ;; For completeness: this is to `remove-text-properties' as `put-text-property'
1244 ;; is to `add-text-properties', etc.
1245 ;(defun remove-text-property (start end property &optional object)
1246 ; "Remove a property from text from START to END.
1247 ;Argument PROPERTY is the property to remove.
1248 ;Optional argument OBJECT is the string or buffer containing the text.
1249 ;Return t if the property was actually removed, nil otherwise."
1250 ; (remove-text-properties start end (list property) object))
1252 ;; For consistency: maybe this should be called `remove-single-property' like
1253 ;; `next-single-property-change' (not `next-single-text-property-change'), etc.
1254 ;(defun remove-single-text-property (start end prop value &optional object)
1255 ; "Remove a specific property value from text from START to END.
1256 ;Arguments PROP and VALUE specify the property and value to remove. The
1257 ;resulting property values are not equal to VALUE nor lists containing VALUE.
1258 ;Optional argument OBJECT is the string or buffer containing the text."
1259 ; (let ((start (text-property-not-all start end prop nil object)) next prev)
1260 ; (while start
1261 ; (setq next (next-single-property-change start prop object end)
1262 ; prev (get-text-property start prop object))
1263 ; (cond ((and (symbolp prev) (eq value prev))
1264 ; (remove-text-property start next prop object))
1265 ; ((and (listp prev) (memq value prev))
1266 ; (let ((new (delq value prev)))
1267 ; (cond ((null new)
1268 ; (remove-text-property start next prop object))
1269 ; ((= (length new) 1)
1270 ; (put-text-property start next prop (car new) object))
1271 ; (t
1272 ; (put-text-property start next prop new object))))))
1273 ; (setq start (text-property-not-all next end prop nil object)))))
1275 ;;; End of Additional text property functions.
1277 ;;; Syntactic regexp fontification functions.
1279 ;; These syntactic keyword pass functions are identical to those keyword pass
1280 ;; functions below, with the following exceptions; (a) they operate on
1281 ;; `font-lock-syntactic-keywords' of course, (b) they are all `defun' as speed
1282 ;; is less of an issue, (c) eval of property value does not occur JIT as speed
1283 ;; is less of an issue, (d) OVERRIDE cannot be `prepend' or `append' as it
1284 ;; makes no sense for `syntax-table' property values, (e) they do not do it
1285 ;; LOUDLY as it is not likely to be intensive.
1287 (defun font-lock-apply-syntactic-highlight (highlight)
1288 "Apply HIGHLIGHT following a match.
1289 HIGHLIGHT should be of the form MATCH-HIGHLIGHT,
1290 see `font-lock-syntactic-keywords'."
1291 (let* ((match (nth 0 highlight))
1292 (start (match-beginning match)) (end (match-end match))
1293 (value (nth 1 highlight))
1294 (override (nth 2 highlight)))
1295 (unless (numberp (car-safe value))
1296 (setq value (eval value)))
1297 (cond ((not start)
1298 ;; No match but we might not signal an error.
1299 (or (nth 3 highlight)
1300 (error "No match %d in highlight %S" match highlight)))
1301 ((not override)
1302 ;; Cannot override existing fontification.
1303 (or (text-property-not-all start end 'syntax-table nil)
1304 (put-text-property start end 'syntax-table value)))
1305 ((eq override t)
1306 ;; Override existing fontification.
1307 (put-text-property start end 'syntax-table value))
1308 ((eq override 'keep)
1309 ;; Keep existing fontification.
1310 (font-lock-fillin-text-property start end 'syntax-table value)))))
1312 (defun font-lock-fontify-syntactic-anchored-keywords (keywords limit)
1313 "Fontify according to KEYWORDS until LIMIT.
1314 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1315 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1316 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1317 ;; Evaluate PRE-MATCH-FORM.
1318 (pre-match-value (eval (nth 1 keywords))))
1319 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1320 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1321 (setq limit pre-match-value)
1322 (save-excursion (end-of-line) (setq limit (point))))
1323 (save-match-data
1324 ;; Find an occurrence of `matcher' before `limit'.
1325 (while (if (stringp matcher)
1326 (re-search-forward matcher limit t)
1327 (funcall matcher limit))
1328 ;; Apply each highlight to this instance of `matcher'.
1329 (setq highlights lowdarks)
1330 (while highlights
1331 (font-lock-apply-syntactic-highlight (car highlights))
1332 (setq highlights (cdr highlights)))))
1333 ;; Evaluate POST-MATCH-FORM.
1334 (eval (nth 2 keywords))))
1336 (defun font-lock-fontify-syntactic-keywords-region (start end)
1337 "Fontify according to `font-lock-syntactic-keywords' between START and END.
1338 START should be at the beginning of a line."
1339 ;; If `font-lock-syntactic-keywords' is a symbol, get the real keywords.
1340 (when (symbolp font-lock-syntactic-keywords)
1341 (setq font-lock-syntactic-keywords (font-lock-eval-keywords
1342 font-lock-syntactic-keywords)))
1343 ;; If `font-lock-syntactic-keywords' is not compiled, compile it.
1344 (unless (eq (car font-lock-syntactic-keywords) t)
1345 (setq font-lock-syntactic-keywords (font-lock-compile-keywords
1346 font-lock-syntactic-keywords)))
1347 ;; Get down to business.
1348 (let ((case-fold-search font-lock-keywords-case-fold-search)
1349 (keywords (cdr font-lock-syntactic-keywords))
1350 keyword matcher highlights)
1351 (while keywords
1352 ;; Find an occurrence of `matcher' from `start' to `end'.
1353 (setq keyword (car keywords) matcher (car keyword))
1354 (goto-char start)
1355 (while (if (stringp matcher)
1356 (re-search-forward matcher end t)
1357 (funcall matcher end))
1358 ;; Apply each highlight to this instance of `matcher', which may be
1359 ;; specific highlights or more keywords anchored to `matcher'.
1360 (setq highlights (cdr keyword))
1361 (while highlights
1362 (if (numberp (car (car highlights)))
1363 (font-lock-apply-syntactic-highlight (car highlights))
1364 (font-lock-fontify-syntactic-anchored-keywords (car highlights)
1365 end))
1366 (setq highlights (cdr highlights))))
1367 (setq keywords (cdr keywords)))))
1369 ;;; End of Syntactic regexp fontification functions.
1371 ;;; Syntactic fontification functions.
1373 ;; These record the parse state at a particular position, always the start of a
1374 ;; line. Used to make `font-lock-fontify-syntactically-region' faster.
1375 ;; Previously, `font-lock-cache-position' was just a buffer position. However,
1376 ;; under certain situations, this occasionally resulted in mis-fontification.
1377 ;; I think the "situations" were deletion with Lazy Lock mode's deferral. sm.
1378 (defvar font-lock-cache-state nil)
1379 (defvar font-lock-cache-position nil)
1381 (defun font-lock-fontify-syntactically-region (start end &optional loudly)
1382 "Put proper face on each string and comment between START and END.
1383 START should be at the beginning of a line."
1384 (let ((cache (marker-position font-lock-cache-position))
1385 state string beg)
1386 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
1387 (goto-char start)
1389 ;; Find the state at the `beginning-of-line' before `start'.
1390 (if (eq start cache)
1391 ;; Use the cache for the state of `start'.
1392 (setq state font-lock-cache-state)
1393 ;; Find the state of `start'.
1394 (if (null font-lock-beginning-of-syntax-function)
1395 ;; Use the state at the previous cache position, if any, or
1396 ;; otherwise calculate from `point-min'.
1397 (if (or (null cache) (< start cache))
1398 (setq state (parse-partial-sexp (point-min) start))
1399 (setq state (parse-partial-sexp cache start nil nil
1400 font-lock-cache-state)))
1401 ;; Call the function to move outside any syntactic block.
1402 (funcall font-lock-beginning-of-syntax-function)
1403 (setq state (parse-partial-sexp (point) start)))
1404 ;; Cache the state and position of `start'.
1405 (setq font-lock-cache-state state)
1406 (set-marker font-lock-cache-position start))
1408 ;; If the region starts inside a string or comment, show the extent of it.
1409 (when (or (nth 3 state) (nth 4 state))
1410 (setq string (nth 3 state) beg (point))
1411 (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
1412 (put-text-property beg (point) 'face
1413 (if string
1414 font-lock-string-face
1415 font-lock-comment-face)))
1417 ;; Find each interesting place between here and `end'.
1418 (while (and (< (point) end)
1419 (progn
1420 (setq state (parse-partial-sexp (point) end nil nil state
1421 'syntax-table))
1422 (or (nth 3 state) (nth 4 state))))
1423 (setq string (nth 3 state) beg (nth 8 state))
1424 (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
1425 (put-text-property beg (point) 'face
1426 (if string
1427 font-lock-string-face
1428 font-lock-comment-face)))))
1430 ;;; End of Syntactic fontification functions.
1432 ;;; Keyword regexp fontification functions.
1434 (defsubst font-lock-apply-highlight (highlight)
1435 "Apply HIGHLIGHT following a match.
1436 HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
1437 (let* ((match (nth 0 highlight))
1438 (start (match-beginning match)) (end (match-end match))
1439 (override (nth 2 highlight)))
1440 (cond ((not start)
1441 ;; No match but we might not signal an error.
1442 (or (nth 3 highlight)
1443 (error "No match %d in highlight %S" match highlight)))
1444 ((not override)
1445 ;; Cannot override existing fontification.
1446 (or (text-property-not-all start end 'face nil)
1447 (put-text-property start end 'face (eval (nth 1 highlight)))))
1448 ((eq override t)
1449 ;; Override existing fontification.
1450 (put-text-property start end 'face (eval (nth 1 highlight))))
1451 ((eq override 'prepend)
1452 ;; Prepend to existing fontification.
1453 (font-lock-prepend-text-property start end 'face (eval (nth 1 highlight))))
1454 ((eq override 'append)
1455 ;; Append to existing fontification.
1456 (font-lock-append-text-property start end 'face (eval (nth 1 highlight))))
1457 ((eq override 'keep)
1458 ;; Keep existing fontification.
1459 (font-lock-fillin-text-property start end 'face (eval (nth 1 highlight)))))))
1461 (defsubst font-lock-fontify-anchored-keywords (keywords limit)
1462 "Fontify according to KEYWORDS until LIMIT.
1463 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1464 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1465 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1466 (lead-start (match-beginning 0))
1467 ;; Evaluate PRE-MATCH-FORM.
1468 (pre-match-value (eval (nth 1 keywords))))
1469 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1470 (if (not (and (numberp pre-match-value) (> pre-match-value (point))))
1471 (save-excursion (end-of-line) (setq limit (point)))
1472 (setq limit pre-match-value)
1473 (when (and font-lock-multiline
1474 (funcall (if (eq font-lock-multiline t) '>= '>)
1475 pre-match-value
1476 (save-excursion (forward-line 1) (point))))
1477 ;; this is a multiline anchored match
1478 (set (make-local-variable 'font-lock-multiline) t)
1479 (put-text-property (point) limit 'font-lock-multiline t)))
1480 (save-match-data
1481 ;; Find an occurrence of `matcher' before `limit'.
1482 (while (if (stringp matcher)
1483 (re-search-forward matcher limit t)
1484 (funcall matcher limit))
1485 ;; Apply each highlight to this instance of `matcher'.
1486 (setq highlights lowdarks)
1487 (while highlights
1488 (font-lock-apply-highlight (car highlights))
1489 (setq highlights (cdr highlights)))))
1490 ;; Evaluate POST-MATCH-FORM.
1491 (eval (nth 2 keywords))))
1493 (defun font-lock-fontify-keywords-region (start end &optional loudly)
1494 "Fontify according to `font-lock-keywords' between START and END.
1495 START should be at the beginning of a line."
1496 (unless (eq (car font-lock-keywords) t)
1497 (setq font-lock-keywords (font-lock-compile-keywords font-lock-keywords)))
1498 (let ((case-fold-search font-lock-keywords-case-fold-search)
1499 (keywords (cdr font-lock-keywords))
1500 (bufname (buffer-name)) (count 0)
1501 keyword matcher highlights)
1503 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1504 (while keywords
1505 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
1506 (make-string (incf count) ?.)))
1508 ;; Find an occurrence of `matcher' from `start' to `end'.
1509 (setq keyword (car keywords) matcher (car keyword))
1510 (goto-char start)
1511 (while (and (< (point) end)
1512 (if (stringp matcher)
1513 (re-search-forward matcher end t)
1514 (funcall matcher end)))
1515 (when (and font-lock-multiline
1516 (match-beginning 0)
1517 (funcall (if (eq font-lock-multiline t) '>= '>)
1518 (point)
1519 (save-excursion (goto-char (match-beginning 0))
1520 (forward-line 1) (point))))
1521 ;; this is a multiline regexp match
1522 (set (make-local-variable 'font-lock-multiline) t)
1523 (put-text-property (match-beginning 0) (point)
1524 'font-lock-multiline t))
1525 ;; Apply each highlight to this instance of `matcher', which may be
1526 ;; specific highlights or more keywords anchored to `matcher'.
1527 (setq highlights (cdr keyword))
1528 (while highlights
1529 (if (numberp (car (car highlights)))
1530 (font-lock-apply-highlight (car highlights))
1531 (font-lock-fontify-anchored-keywords (car highlights) end))
1532 (setq highlights (cdr highlights))))
1533 (setq keywords (cdr keywords)))))
1535 ;;; End of Keyword regexp fontification functions.
1537 ;; Various functions.
1539 (defun font-lock-compile-keywords (keywords)
1540 "Compile KEYWORDS into the form (t KEYWORD ...).
1541 Here KEYWORD is of the form (MATCHER HIGHLIGHT ...) as shown in the
1542 `font-lock-keywords' doc string."
1543 (if (eq (car-safe keywords) t)
1544 keywords
1545 (cons t (mapcar 'font-lock-compile-keyword keywords))))
1547 (defun font-lock-compile-keyword (keyword)
1548 (cond ((nlistp keyword) ; MATCHER
1549 (list keyword '(0 font-lock-keyword-face)))
1550 ((eq (car keyword) 'eval) ; (eval . FORM)
1551 (font-lock-compile-keyword (eval (cdr keyword))))
1552 ((eq (car-safe (cdr keyword)) 'quote) ; (MATCHER . 'FORM)
1553 ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote.
1554 (if (symbolp (nth 2 keyword))
1555 (list (car keyword) (list 0 (cdr keyword)))
1556 (font-lock-compile-keyword (cons (car keyword) (nth 2 keyword)))))
1557 ((numberp (cdr keyword)) ; (MATCHER . MATCH)
1558 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
1559 ((symbolp (cdr keyword)) ; (MATCHER . FACENAME)
1560 (list (car keyword) (list 0 (cdr keyword))))
1561 ((nlistp (nth 1 keyword)) ; (MATCHER . HIGHLIGHT)
1562 (list (car keyword) (cdr keyword)))
1563 (t ; (MATCHER HIGHLIGHT ...)
1564 keyword)))
1566 (defun font-lock-eval-keywords (keywords)
1567 "Evalulate KEYWORDS if a function (funcall) or variable (eval) name."
1568 (if (listp keywords)
1569 keywords
1570 (font-lock-eval-keywords (if (fboundp keywords)
1571 (funcall keywords)
1572 (eval keywords)))))
1574 (defun font-lock-value-in-major-mode (alist)
1575 "Return value in ALIST for `major-mode', or ALIST if it is not an alist.
1576 Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t."
1577 (if (consp alist)
1578 (cdr (or (assq major-mode alist) (assq t alist)))
1579 alist))
1581 (defun font-lock-choose-keywords (keywords level)
1582 "Return LEVELth element of KEYWORDS.
1583 A LEVEL of nil is equal to a LEVEL of 0, a LEVEL of t is equal to
1584 \(1- (length KEYWORDS))."
1585 (cond ((symbolp keywords)
1586 keywords)
1587 ((numberp level)
1588 (or (nth level keywords) (car (reverse keywords))))
1589 ((eq level t)
1590 (car (reverse keywords)))
1592 (car keywords))))
1594 (defvar font-lock-set-defaults nil) ; Whether we have set up defaults.
1596 (defun font-lock-set-defaults ()
1597 "Set fontification defaults appropriately for this mode.
1598 Sets various variables using `font-lock-defaults' (or, if nil, using
1599 `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
1600 ;; Set fontification defaults.
1601 (make-local-variable 'font-lock-fontified)
1602 ;; Set iff not previously set.
1603 (unless font-lock-set-defaults
1604 (set (make-local-variable 'font-lock-set-defaults) t)
1605 (set (make-local-variable 'font-lock-cache-state) nil)
1606 (set (make-local-variable 'font-lock-cache-position) (make-marker))
1607 (let* ((defaults (or font-lock-defaults
1608 (cdr (assq major-mode font-lock-defaults-alist))))
1609 (keywords
1610 (font-lock-choose-keywords (nth 0 defaults)
1611 (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1612 (local (cdr (assq major-mode font-lock-keywords-alist))))
1613 ;; Regexp fontification?
1614 (set (make-local-variable 'font-lock-keywords)
1615 (font-lock-compile-keywords (font-lock-eval-keywords keywords)))
1616 ;; Local fontification?
1617 (while local
1618 (font-lock-add-keywords nil (car (car local)) (cdr (car local)))
1619 (setq local (cdr local)))
1620 ;; Syntactic fontification?
1621 (when (nth 1 defaults)
1622 (set (make-local-variable 'font-lock-keywords-only) t))
1623 ;; Case fold during regexp fontification?
1624 (when (nth 2 defaults)
1625 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
1626 ;; Syntax table for regexp and syntactic fontification?
1627 (when (nth 3 defaults)
1628 (let ((slist (nth 3 defaults)))
1629 (set (make-local-variable 'font-lock-syntax-table)
1630 (copy-syntax-table (syntax-table)))
1631 (while slist
1632 ;; The character to modify may be a single CHAR or a STRING.
1633 (let ((chars (if (numberp (car (car slist)))
1634 (list (car (car slist)))
1635 (mapcar 'identity (car (car slist)))))
1636 (syntax (cdr (car slist))))
1637 (while chars
1638 (modify-syntax-entry (car chars) syntax font-lock-syntax-table)
1639 (setq chars (cdr chars)))
1640 (setq slist (cdr slist))))))
1641 ;; Syntax function for syntactic fontification?
1642 (when (nth 4 defaults)
1643 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1644 (nth 4 defaults)))
1645 ;; Variable alist?
1646 (let ((alist (nthcdr 5 defaults)))
1647 (while alist
1648 (let ((variable (car (car alist))) (value (cdr (car alist))))
1649 (unless (boundp variable)
1650 (set variable nil))
1651 (set (make-local-variable variable) value)
1652 (setq alist (cdr alist))))))))
1654 (defun font-lock-unset-defaults ()
1655 "Unset fontification defaults. See function `font-lock-set-defaults'."
1656 (setq font-lock-set-defaults nil
1657 font-lock-keywords nil
1658 font-lock-keywords-only nil
1659 font-lock-keywords-case-fold-search nil
1660 font-lock-syntax-table nil
1661 font-lock-beginning-of-syntax-function nil)
1662 (let* ((defaults (or font-lock-defaults
1663 (cdr (assq major-mode font-lock-defaults-alist))))
1664 (alist (nthcdr 5 defaults)))
1665 (while alist
1666 (set (car (car alist)) (default-value (car (car alist))))
1667 (setq alist (cdr alist)))))
1669 ;;; Colour etc. support.
1671 ;; Originally these variable values were face names such as `bold' etc.
1672 ;; Now we create our own faces, but we keep these variables for compatibility
1673 ;; and they give users another mechanism for changing face appearance.
1674 ;; We now allow a FACENAME in `font-lock-keywords' to be any expression that
1675 ;; returns a face. So the easiest thing is to continue using these variables,
1676 ;; rather than sometimes evaling FACENAME and sometimes not. sm.
1677 (defvar font-lock-comment-face 'font-lock-comment-face
1678 "Face name to use for comments.")
1680 (defvar font-lock-string-face 'font-lock-string-face
1681 "Face name to use for strings.")
1683 (defvar font-lock-keyword-face 'font-lock-keyword-face
1684 "Face name to use for keywords.")
1686 (defvar font-lock-builtin-face 'font-lock-builtin-face
1687 "Face name to use for builtins.")
1689 (defvar font-lock-function-name-face 'font-lock-function-name-face
1690 "Face name to use for function names.")
1692 (defvar font-lock-variable-name-face 'font-lock-variable-name-face
1693 "Face name to use for variable names.")
1695 (defvar font-lock-type-face 'font-lock-type-face
1696 "Face name to use for type and class names.")
1698 (defvar font-lock-constant-face 'font-lock-constant-face
1699 "Face name to use for constant and label names.")
1701 (defvar font-lock-warning-face 'font-lock-warning-face
1702 "Face name to use for things that should stand out.")
1704 (defvar font-lock-reference-face 'font-lock-constant-face
1705 "This variable is obsolete. Use `font-lock-constant-face'.")
1707 ;; Originally face attributes were specified via `font-lock-face-attributes'.
1708 ;; Users then changed the default face attributes by setting that variable.
1709 ;; However, we try and be back-compatible and respect its value if set except
1710 ;; for faces where M-x customize has been used to save changes for the face.
1711 (when (boundp 'font-lock-face-attributes)
1712 (let ((face-attributes font-lock-face-attributes))
1713 (while face-attributes
1714 (let* ((face-attribute (pop face-attributes))
1715 (face (car face-attribute)))
1716 ;; Rustle up a `defface' SPEC from a `font-lock-face-attributes' entry.
1717 (unless (get face 'saved-face)
1718 (let ((foreground (nth 1 face-attribute))
1719 (background (nth 2 face-attribute))
1720 (bold-p (nth 3 face-attribute))
1721 (italic-p (nth 4 face-attribute))
1722 (underline-p (nth 5 face-attribute))
1723 face-spec)
1724 (when foreground
1725 (setq face-spec (cons ':foreground (cons foreground face-spec))))
1726 (when background
1727 (setq face-spec (cons ':background (cons background face-spec))))
1728 (when bold-p
1729 (setq face-spec (append '(:bold t) face-spec)))
1730 (when italic-p
1731 (setq face-spec (append '(:italic t) face-spec)))
1732 (when underline-p
1733 (setq face-spec (append '(:underline t) face-spec)))
1734 (custom-declare-face face (list (list t face-spec)) nil)))))))
1736 ;; But now we do it the custom way. Note that `defface' will not overwrite any
1737 ;; faces declared above via `custom-declare-face'.
1738 (defface font-lock-comment-face
1739 '((((type tty) (class color)) (:foreground "red"))
1740 (((class grayscale) (background light))
1741 (:foreground "DimGray" :bold t :italic t))
1742 (((class grayscale) (background dark))
1743 (:foreground "LightGray" :bold t :italic t))
1744 (((class color) (background light)) (:foreground "Firebrick"))
1745 (((class color) (background dark)) (:foreground "OrangeRed"))
1746 (t (:bold t :italic t)))
1747 "Font Lock mode face used to highlight comments."
1748 :group 'font-lock-highlighting-faces)
1750 (defface font-lock-string-face
1751 '((((type tty) (class color)) (:foreground "green"))
1752 (((class grayscale) (background light)) (:foreground "DimGray" :italic t))
1753 (((class grayscale) (background dark)) (:foreground "LightGray" :italic t))
1754 (((class color) (background light)) (:foreground "RosyBrown"))
1755 (((class color) (background dark)) (:foreground "LightSalmon"))
1756 (t (:italic t)))
1757 "Font Lock mode face used to highlight strings."
1758 :group 'font-lock-highlighting-faces)
1760 (defface font-lock-keyword-face
1761 '((((type tty) (class color)) (:foreground "cyan" :weight bold))
1762 (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
1763 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1764 (((class color) (background light)) (:foreground "Purple"))
1765 (((class color) (background dark)) (:foreground "Cyan"))
1766 (t (:bold t)))
1767 "Font Lock mode face used to highlight keywords."
1768 :group 'font-lock-highlighting-faces)
1770 (defface font-lock-builtin-face
1771 '((((type tty) (class color)) (:foreground "blue" :weight light))
1772 (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
1773 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1774 (((class color) (background light)) (:foreground "Orchid"))
1775 (((class color) (background dark)) (:foreground "LightSteelBlue"))
1776 (t (:bold t)))
1777 "Font Lock mode face used to highlight builtins."
1778 :group 'font-lock-highlighting-faces)
1780 (defface font-lock-function-name-face
1781 '((((type tty) (class color)) (:foreground "blue" :weight bold))
1782 (((class color) (background light)) (:foreground "Blue"))
1783 (((class color) (background dark)) (:foreground "LightSkyBlue"))
1784 (t (:inverse-video t :bold t)))
1785 "Font Lock mode face used to highlight function names."
1786 :group 'font-lock-highlighting-faces)
1788 (defface font-lock-variable-name-face
1789 '((((type tty) (class color)) (:foreground "yellow" :weight light))
1790 (((class grayscale) (background light))
1791 (:foreground "Gray90" :bold t :italic t))
1792 (((class grayscale) (background dark))
1793 (:foreground "DimGray" :bold t :italic t))
1794 (((class color) (background light)) (:foreground "DarkGoldenrod"))
1795 (((class color) (background dark)) (:foreground "LightGoldenrod"))
1796 (t (:bold t :italic t)))
1797 "Font Lock mode face used to highlight variable names."
1798 :group 'font-lock-highlighting-faces)
1800 (defface font-lock-type-face
1801 '((((type tty) (class color)) (:foreground "green"))
1802 (((class grayscale) (background light)) (:foreground "Gray90" :bold t))
1803 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1804 (((class color) (background light)) (:foreground "ForestGreen"))
1805 (((class color) (background dark)) (:foreground "PaleGreen"))
1806 (t (:bold t :underline t)))
1807 "Font Lock mode face used to highlight type and classes."
1808 :group 'font-lock-highlighting-faces)
1810 (defface font-lock-constant-face
1811 '((((type tty) (class color)) (:foreground "magenta"))
1812 (((class grayscale) (background light))
1813 (:foreground "LightGray" :bold t :underline t))
1814 (((class grayscale) (background dark))
1815 (:foreground "Gray50" :bold t :underline t))
1816 (((class color) (background light)) (:foreground "CadetBlue"))
1817 (((class color) (background dark)) (:foreground "Aquamarine"))
1818 (t (:bold t :underline t)))
1819 "Font Lock mode face used to highlight constants and labels."
1820 :group 'font-lock-highlighting-faces)
1822 (defface font-lock-warning-face
1823 '((((type tty) (class color)) (:foreground "red"))
1824 (((class color) (background light)) (:foreground "Red" :bold t))
1825 (((class color) (background dark)) (:foreground "Pink" :bold t))
1826 (t (:inverse-video t :bold t)))
1827 "Font Lock mode face used to highlight warnings."
1828 :group 'font-lock-highlighting-faces)
1830 ;;; End of Colour etc. support.
1832 ;;; Menu support.
1834 ;; This section of code is commented out because Emacs does not have real menu
1835 ;; buttons. (We can mimic them by putting "( ) " or "(X) " at the beginning of
1836 ;; the menu entry text, but with Xt it looks both ugly and embarrassingly
1837 ;; amateur.) If/When Emacs gets real menus buttons, put in menu-bar.el after
1838 ;; the entry for "Text Properties" something like:
1840 ;; (define-key menu-bar-edit-menu [font-lock]
1841 ;; (cons "Syntax Highlighting" font-lock-menu))
1843 ;; and remove a single ";" from the beginning of each line in the rest of this
1844 ;; section. Probably the mechanism for telling the menu code what are menu
1845 ;; buttons and when they are on or off needs tweaking. I have assumed that the
1846 ;; mechanism is via `menu-toggle' and `menu-selected' symbol properties. sm.
1848 ;;;;###autoload
1849 ;(progn
1850 ; ;; Make the Font Lock menu.
1851 ; (defvar font-lock-menu (make-sparse-keymap "Syntax Highlighting"))
1852 ; ;; Add the menu items in reverse order.
1853 ; (define-key font-lock-menu [fontify-less]
1854 ; '("Less In Current Buffer" . font-lock-fontify-less))
1855 ; (define-key font-lock-menu [fontify-more]
1856 ; '("More In Current Buffer" . font-lock-fontify-more))
1857 ; (define-key font-lock-menu [font-lock-sep]
1858 ; '("--"))
1859 ; (define-key font-lock-menu [font-lock-mode]
1860 ; '("In Current Buffer" . font-lock-mode))
1861 ; (define-key font-lock-menu [global-font-lock-mode]
1862 ; '("In All Buffers" . global-font-lock-mode)))
1864 ;;;;###autoload
1865 ;(progn
1866 ; ;; We put the appropriate `menu-enable' etc. symbol property values on when
1867 ; ;; font-lock.el is loaded, so we don't need to autoload the three variables.
1868 ; (put 'global-font-lock-mode 'menu-toggle t)
1869 ; (put 'font-lock-mode 'menu-toggle t)
1870 ; (put 'font-lock-fontify-more 'menu-enable '(identity))
1871 ; (put 'font-lock-fontify-less 'menu-enable '(identity)))
1873 ;;; Put the appropriate symbol property values on now. See above.
1874 ;(put 'global-font-lock-mode 'menu-selected 'global-font-lock-mode)
1875 ;(put 'font-lock-mode 'menu-selected 'font-lock-mode)
1876 ;(put 'font-lock-fontify-more 'menu-enable '(nth 2 font-lock-fontify-level))
1877 ;(put 'font-lock-fontify-less 'menu-enable '(nth 1 font-lock-fontify-level))
1879 ;(defvar font-lock-fontify-level nil) ; For less/more fontification.
1881 ;(defun font-lock-fontify-level (level)
1882 ; (let ((font-lock-maximum-decoration level))
1883 ; (when font-lock-mode
1884 ; (font-lock-mode))
1885 ; (font-lock-mode)
1886 ; (when font-lock-verbose
1887 ; (message "Fontifying %s... level %d" (buffer-name) level))))
1889 ;(defun font-lock-fontify-less ()
1890 ; "Fontify the current buffer with less decoration.
1891 ;See `font-lock-maximum-decoration'."
1892 ; (interactive)
1893 ; ;; Check in case we get called interactively.
1894 ; (if (nth 1 font-lock-fontify-level)
1895 ; (font-lock-fontify-level (1- (car font-lock-fontify-level)))
1896 ; (error "No less decoration")))
1898 ;(defun font-lock-fontify-more ()
1899 ; "Fontify the current buffer with more decoration.
1900 ;See `font-lock-maximum-decoration'."
1901 ; (interactive)
1902 ; ;; Check in case we get called interactively.
1903 ; (if (nth 2 font-lock-fontify-level)
1904 ; (font-lock-fontify-level (1+ (car font-lock-fontify-level)))
1905 ; (error "No more decoration")))
1907 ;;; This should be called by `font-lock-set-defaults'.
1908 ;(defun font-lock-set-menu ()
1909 ; ;; Activate less/more fontification entries if there are multiple levels for
1910 ; ;; the current buffer. Sets `font-lock-fontify-level' to be of the form
1911 ; ;; (CURRENT-LEVEL IS-LOWER-LEVEL-P IS-HIGHER-LEVEL-P) for menu activation.
1912 ; (let ((keywords (or (nth 0 font-lock-defaults)
1913 ; (nth 1 (assq major-mode font-lock-defaults-alist))))
1914 ; (level (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1915 ; (make-local-variable 'font-lock-fontify-level)
1916 ; (if (or (symbolp keywords) (= (length keywords) 1))
1917 ; (font-lock-unset-menu)
1918 ; (cond ((eq level t)
1919 ; (setq level (1- (length keywords))))
1920 ; ((or (null level) (zerop level))
1921 ; ;; The default level is usually, but not necessarily, level 1.
1922 ; (setq level (- (length keywords)
1923 ; (length (member (eval (car keywords))
1924 ; (mapcar 'eval (cdr keywords))))))))
1925 ; (setq font-lock-fontify-level (list level (> level 1)
1926 ; (< level (1- (length keywords))))))))
1928 ;;; This should be called by `font-lock-unset-defaults'.
1929 ;(defun font-lock-unset-menu ()
1930 ; ;; Deactivate less/more fontification entries.
1931 ; (setq font-lock-fontify-level nil))
1933 ;;; End of Menu support.
1935 ;;; Various regexp information shared by several modes.
1936 ;;; Information specific to a single mode should go in its load library.
1938 ;; Font Lock support for C, C++, Objective-C and Java modes will one day be in
1939 ;; some cc-font.el (and required by cc-mode.el). However, the below function
1940 ;; should stay in font-lock.el, since it is used by other libraries. sm.
1942 (defun font-lock-match-c-style-declaration-item-and-skip-to-next (limit)
1943 "Match, and move over, any declaration/definition item after point.
1944 Matches after point, but ignores leading whitespace and `*' characters.
1945 Does not move further than LIMIT.
1947 The expected syntax of a declaration/definition item is `word' (preceded by
1948 optional whitespace and `*' characters and proceeded by optional whitespace)
1949 optionally followed by a `('. Everything following the item (but belonging to
1950 it) is expected to by skip-able by `scan-sexps', and items are expected to be
1951 separated with a `,' and to be terminated with a `;'.
1953 Thus the regexp matches after point: word (
1954 ^^^^ ^
1955 Where the match subexpressions are: 1 2
1957 The item is delimited by (match-beginning 1) and (match-end 1).
1958 If (match-beginning 2) is non-nil, the item is followed by a `('.
1960 This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
1961 (when (looking-at "[ \t*]*\\(\\sw+\\)[ \t]*\\((\\)?")
1962 (save-match-data
1963 (condition-case nil
1964 (save-restriction
1965 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
1966 (narrow-to-region (point-min) limit)
1967 (goto-char (match-end 1))
1968 ;; Move over any item value, etc., to the next item.
1969 (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)"))
1970 (goto-char (or (scan-sexps (point) 1) (point-max))))
1971 (goto-char (match-end 2)))
1972 (error t)))))
1974 ;; Lisp.
1976 (defconst lisp-font-lock-keywords-1
1977 (eval-when-compile
1978 (list
1980 ;; Definitions.
1981 (list (concat "(\\(def\\("
1982 ;; Function declarations.
1983 "\\(advice\\|alias\\|generic\\|macro\\*?\\|method\\|"
1984 "setf\\|subst\\*?\\|un\\*?\\|"
1985 "ine-\\(condition\\|derived-mode\\|function\\|"
1986 "method-combination\\|setf-expander\\|skeleton\\|widget\\|"
1987 "\\(compiler\\|modify\\|symbol\\)-macro\\)\\)\\|"
1988 ;; Variable declarations.
1989 "\\(const\\(ant\\)?\\|custom\\|face\\|parameter\\|var\\)\\|"
1990 ;; Structure declarations.
1991 "\\(class\\|group\\|package\\|struct\\|type\\)"
1992 "\\)\\)\\>"
1993 ;; Any whitespace and defined object.
1994 "[ \t'\(]*"
1995 "\\(\\sw+\\)?")
1996 '(1 font-lock-keyword-face)
1997 '(9 (cond ((match-beginning 3) font-lock-function-name-face)
1998 ((match-beginning 6) font-lock-variable-name-face)
1999 (t font-lock-type-face))
2000 nil t))
2002 ;; Emacs Lisp autoload cookies.
2003 '("^;;;###\\(autoload\\)" 1 font-lock-warning-face prepend)
2005 "Subdued level highlighting for Lisp modes.")
2007 (defconst lisp-font-lock-keywords-2
2008 (append lisp-font-lock-keywords-1
2009 (eval-when-compile
2010 (list
2012 ;; Control structures. Emacs Lisp forms.
2013 (cons (concat
2014 "(" (regexp-opt
2015 '("cond" "if" "while" "let" "let*"
2016 "prog" "progn" "progv" "prog1" "prog2" "prog*"
2017 "inline" "lambda" "save-restriction" "save-excursion"
2018 "save-window-excursion" "save-selected-window"
2019 "save-match-data" "save-current-buffer" "unwind-protect"
2020 "condition-case" "track-mouse"
2021 "eval-after-load" "eval-and-compile" "eval-when-compile"
2022 "eval-when"
2023 "with-current-buffer" "with-electric-help"
2024 "with-output-to-string" "with-output-to-temp-buffer"
2025 "with-temp-buffer" "with-temp-file" "with-temp-message"
2026 "with-timeout") t)
2027 "\\>")
2030 ;; Control structures. Common Lisp forms.
2031 (cons (concat
2032 "(" (regexp-opt
2033 '("when" "unless" "case" "ecase" "typecase" "etypecase"
2034 "ccase" "ctypecase" "handler-case" "handler-bind"
2035 "restart-bind" "restart-case" "in-package"
2036 "cerror" "break" "ignore-errors"
2037 "loop" "do" "do*" "dotimes" "dolist" "the" "locally"
2038 "proclaim" "declaim" "declare" "symbol-macrolet"
2039 "lexical-let" "lexical-let*" "flet" "labels" "compiler-let"
2040 "destructuring-bind" "macrolet" "tagbody" "block"
2041 "return" "return-from") t)
2042 "\\>")
2045 ;; Exit/Feature symbols as constants.
2046 (list (concat "(\\(catch\\|throw\\|featurep\\|provide\\|require\\)\\>"
2047 "[ \t']*\\(\\sw+\\)?")
2048 '(1 font-lock-keyword-face)
2049 '(2 font-lock-constant-face nil t))
2051 ;; Erroneous structures.
2052 '("(\\(abort\\|assert\\|error\\|signal\\)\\>" 1 font-lock-warning-face)
2054 ;; Words inside \\[] tend to be for `substitute-command-keys'.
2055 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-constant-face prepend)
2057 ;; Words inside `' tend to be symbol names.
2058 '("`\\(\\sw\\sw+\\)'" 1 font-lock-constant-face prepend)
2060 ;; Constant values.
2061 '("\\<:\\sw\\sw+\\>" 0 font-lock-builtin-face)
2063 ;; ELisp and CLisp `&' keywords as types.
2064 '("\\&\\sw+\\>" . font-lock-type-face)
2066 "Gaudy level highlighting for Lisp modes.")
2068 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
2069 "Default expressions to highlight in Lisp modes.")
2071 ;; TeX.
2073 ;(defvar tex-font-lock-keywords
2074 ; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
2075 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
2076 ; 2 font-lock-function-name-face)
2077 ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
2078 ; 2 font-lock-constant-face)
2079 ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
2080 ; ;; not be able to display those fonts.
2081 ; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep)
2082 ; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep)
2083 ; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face)
2084 ; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep))
2085 ; ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>.
2086 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
2087 ; 2 font-lock-function-name-face)
2088 ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
2089 ; 2 font-lock-constant-face)
2090 ; ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face)
2091 ; "\\\\\\([a-zA-Z@]+\\|.\\)"
2092 ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
2093 ; ;; not be able to display those fonts.
2094 ; ;; LaTeX2e: \emph{This is emphasized}.
2095 ; ("\\\\emph{\\([^}]+\\)}" 1 'italic keep)
2096 ; ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...}
2097 ; ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}"
2098 ; 3 (if (match-beginning 2) 'bold 'italic) keep)
2099 ; ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables.
2100 ; ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)"
2101 ; 3 (if (match-beginning 2) 'bold 'italic) keep))
2103 ;; Rewritten with the help of Alexandra Bac <abac@welcome.disi.unige.it>.
2104 (defconst tex-font-lock-keywords-1
2105 (eval-when-compile
2106 (let* (;;
2107 ;; Names of commands whose arg should be fontified as heading, etc.
2108 (headings (regexp-opt '("title" "begin" "end") t))
2109 ;; These commands have optional args.
2110 (headings-opt (regexp-opt
2111 '("chapter" "part"
2112 "section" "subsection" "subsubsection"
2113 "section*" "subsection*" "subsubsection*"
2114 "paragraph" "subparagraph" "subsubparagraph"
2115 "paragraph*" "subparagraph*" "subsubparagraph*"
2116 "newcommand" "renewcommand" "newenvironment"
2117 "newtheorem"
2118 "newcommand*" "renewcommand*" "newenvironment*"
2119 "newtheorem*")
2121 (variables (regexp-opt
2122 '("newcounter" "newcounter*" "setcounter" "addtocounter"
2123 "setlength" "addtolength" "settowidth")
2125 (includes (regexp-opt
2126 '("input" "include" "includeonly" "bibliography"
2127 "epsfig" "psfig" "epsf")
2129 (includes-opt (regexp-opt
2130 '("nofiles" "usepackage"
2131 "includegraphics" "includegraphics*")
2133 ;; Miscellany.
2134 (slash "\\\\")
2135 (opt "\\(\\[[^]]*\\]\\)?")
2136 (arg "{\\([^}]+\\)")
2137 (opt-depth (regexp-opt-depth opt))
2138 (arg-depth (regexp-opt-depth arg))
2140 (list
2142 ;; Heading args.
2143 (list (concat slash headings arg)
2144 (+ (regexp-opt-depth headings) arg-depth)
2145 'font-lock-function-name-face)
2146 (list (concat slash headings-opt opt arg)
2147 (+ (regexp-opt-depth headings-opt) opt-depth arg-depth)
2148 'font-lock-function-name-face)
2150 ;; Variable args.
2151 (list (concat slash variables arg)
2152 (+ (regexp-opt-depth variables) arg-depth)
2153 'font-lock-variable-name-face)
2155 ;; Include args.
2156 (list (concat slash includes arg)
2157 (+ (regexp-opt-depth includes) arg-depth)
2158 'font-lock-builtin-face)
2159 (list (concat slash includes-opt opt arg)
2160 (+ (regexp-opt-depth includes-opt) opt-depth arg-depth)
2161 'font-lock-builtin-face)
2163 ;; Definitions. I think.
2164 '("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)"
2165 1 font-lock-function-name-face)
2167 "Subdued expressions to highlight in TeX modes.")
2169 (defconst tex-font-lock-keywords-2
2170 (append tex-font-lock-keywords-1
2171 (eval-when-compile
2172 (let* (;;
2173 ;; Names of commands whose arg should be fontified with fonts.
2174 (bold (regexp-opt '("bf" "textbf" "textsc" "textup"
2175 "boldsymbol" "pmb") t))
2176 (italic (regexp-opt '("it" "textit" "textsl" "emph") t))
2177 (type (regexp-opt '("texttt" "textmd" "textrm" "textsf") t))
2179 ;; Names of commands whose arg should be fontified as a citation.
2180 (citations (regexp-opt
2181 '("label" "ref" "pageref" "vref" "eqref")
2183 (citations-opt (regexp-opt
2184 '("cite" "nocite" "caption" "index" "glossary"
2185 "footnote" "footnotemark" "footnotetext")
2188 ;; Names of commands that should be fontified.
2189 (specials (regexp-opt
2190 '("\\"
2191 "linebreak" "nolinebreak" "pagebreak" "nopagebreak"
2192 "newline" "newpage" "clearpage" "cleardoublepage"
2193 "displaybreak" "allowdisplaybreaks" "enlargethispage")
2195 (general "\\([a-zA-Z@]+\\**\\|[^ \t\n]\\)")
2197 ;; Miscellany.
2198 (slash "\\\\")
2199 (opt "\\(\\[[^]]*\\]\\)?")
2200 (arg "{\\([^}]+\\)")
2201 (opt-depth (regexp-opt-depth opt))
2202 (arg-depth (regexp-opt-depth arg))
2204 (list
2206 ;; Citation args.
2207 (list (concat slash citations arg)
2208 (+ (regexp-opt-depth citations) arg-depth)
2209 'font-lock-constant-face)
2210 (list (concat slash citations-opt opt arg)
2211 (+ (regexp-opt-depth citations-opt) opt-depth arg-depth)
2212 'font-lock-constant-face)
2214 ;; Command names, special and general.
2215 (cons (concat slash specials) 'font-lock-warning-face)
2216 (concat slash general)
2218 ;; Font environments. It seems a bit dubious to use `bold' etc. faces
2219 ;; since we might not be able to display those fonts.
2220 (list (concat slash bold arg)
2221 (+ (regexp-opt-depth bold) arg-depth)
2222 '(quote bold) 'keep)
2223 (list (concat slash italic arg)
2224 (+ (regexp-opt-depth italic) arg-depth)
2225 '(quote italic) 'keep)
2226 (list (concat slash type arg)
2227 (+ (regexp-opt-depth type) arg-depth)
2228 '(quote bold-italic) 'keep)
2230 ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables.
2231 (list (concat "\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>"
2232 "\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)")
2233 3 '(if (match-beginning 2) 'bold 'italic) 'keep)
2234 ))))
2235 "Gaudy expressions to highlight in TeX modes.")
2237 (defvar tex-font-lock-keywords tex-font-lock-keywords-1
2238 "Default expressions to highlight in TeX modes.")
2240 ;;; User choices.
2242 ;; These provide a means to fontify types not defined by the language. Those
2243 ;; types might be the user's own or they might be generally accepted and used.
2244 ;; Generally accepted types are used to provide default variable values.
2246 (define-widget 'font-lock-extra-types-widget 'radio
2247 "Widget `:type' for members of the custom group `font-lock-extra-types'.
2248 Members should `:load' the package `font-lock' to use this widget."
2249 :args '((const :tag "none" nil)
2250 (repeat :tag "types" regexp)))
2252 (defcustom c-font-lock-extra-types '("FILE" "\\sw+_t")
2253 "*List of extra types to fontify in C mode.
2254 Each list item should be a regexp not containing word-delimiters.
2255 For example, a value of (\"FILE\" \"\\\\sw+_t\") means the word FILE and words
2256 ending in _t 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 (defcustom c++-font-lock-extra-types
2263 '("\\sw+_t"
2264 "\\([iof]\\|str\\)+stream\\(buf\\)?" "ios"
2265 "string" "rope"
2266 "list" "slist"
2267 "deque" "vector" "bit_vector"
2268 "set" "multiset"
2269 "map" "multimap"
2270 "hash\\(_\\(m\\(ap\\|ulti\\(map\\|set\\)\\)\\|set\\)\\)?"
2271 "stack" "queue" "priority_queue"
2272 "type_info"
2273 "iterator" "const_iterator" "reverse_iterator" "const_reverse_iterator"
2274 "reference" "const_reference")
2275 "*List of extra types to fontify in C++ mode.
2276 Each list item should be a regexp not containing word-delimiters.
2277 For example, a value of (\"string\") means the word string is treated as a type
2278 name.
2280 The value of this variable is used when Font Lock mode is turned on."
2281 :type 'font-lock-extra-types-widget
2282 :group 'font-lock-extra-types)
2284 (defcustom objc-font-lock-extra-types '("Class" "BOOL" "IMP" "SEL")
2285 "*List of extra types to fontify in Objective-C mode.
2286 Each list item should be a regexp not containing word-delimiters.
2287 For example, a value of (\"Class\" \"BOOL\" \"IMP\" \"SEL\") means the words
2288 Class, BOOL, IMP and SEL are treated as type names.
2290 The value of this variable is used when Font Lock mode is turned on."
2291 :type 'font-lock-extra-types-widget
2292 :group 'font-lock-extra-types)
2294 (defcustom java-font-lock-extra-types
2295 '("[A-Z\300-\326\330-\337]\\sw*[a-z]\\sw*")
2296 "*List of extra types to fontify in Java mode.
2297 Each list item should be a regexp not containing word-delimiters.
2298 For example, a value of (\"[A-Z\300-\326\330-\337]\\\\sw*[a-z]\\\\sw*\") means capitalised
2299 words (and words conforming to the Java id spec) are treated as type names.
2301 The value of this variable is used when Font Lock mode is turned on."
2302 :type 'font-lock-extra-types-widget
2303 :group 'font-lock-extra-types)
2305 ;;; C.
2307 ;; [Murmur murmur murmur] Maestro, drum-roll please... [Murmur murmur murmur.]
2308 ;; Ahem. [Murmur murmur murmur] Lay-dees an Gennel-men. [Murmur murmur shhh!]
2309 ;; I am most proud and humbly honoured today [murmur murmur cough] to present
2310 ;; to you good people, the winner of the Second Millennium Award for The Most
2311 ;; Hairy Language Syntax. [Ahhh!] All rise please. [Shuffle shuffle
2312 ;; shuffle.] And a round of applause please. For... The C Language! [Roar.]
2314 ;; Thank you... You are too kind... It is with a feeling of great privilege
2315 ;; and indeed emotion [sob] that I accept this award. It has been a long hard
2316 ;; road. But we know our destiny. And our future. For we must not rest.
2317 ;; There are more tokens to overload, more shoehorn, more methodologies. But
2318 ;; more is a plus! [Ha ha ha.] And more means plus! [Ho ho ho.] The future
2319 ;; is C++! [Ohhh!] The Third Millennium Award... Will be ours! [Roar.]
2321 (defconst c-font-lock-keywords-1 nil
2322 "Subdued level highlighting for C mode.")
2324 (defconst c-font-lock-keywords-2 nil
2325 "Medium level highlighting for C mode.
2326 See also `c-font-lock-extra-types'.")
2328 (defconst c-font-lock-keywords-3 nil
2329 "Gaudy level highlighting for C mode.
2330 See also `c-font-lock-extra-types'.")
2332 (let* ((c-keywords
2333 (eval-when-compile
2334 (regexp-opt '("break" "continue" "do" "else" "for" "if" "return"
2335 "switch" "while" "sizeof"
2336 ;; Type related, but we don't do anything special.
2337 "typedef" "extern" "auto" "register" "static"
2338 "volatile" "const"
2339 ;; Dan Nicolaescu <done@gnu.org> says this is new.
2340 "restrict") t)))
2341 (c-type-specs
2342 (eval-when-compile
2343 (regexp-opt '("enum" "struct" "union") t)))
2344 (c-type-specs-depth
2345 (regexp-opt-depth c-type-specs))
2346 (c-type-names
2347 `(mapconcat 'identity
2348 (cons
2349 (,@ (eval-when-compile
2350 (regexp-opt
2351 '("char" "short" "int" "long" "signed" "unsigned"
2352 "float" "double" "void" "complex"))))
2353 c-font-lock-extra-types)
2354 "\\|"))
2355 (c-type-names-depth
2356 `(regexp-opt-depth (,@ c-type-names)))
2358 (setq c-font-lock-keywords-1
2359 (list
2361 ;; These are all anchored at the beginning of line for speed.
2362 ;; Note that `c++-font-lock-keywords-1' depends on `c-font-lock-keywords-1'.
2364 ;; Fontify function name definitions (GNU style; without type on line).
2365 '("^\\(\\sw+\\)[ \t]*(" 1 font-lock-function-name-face)
2367 ;; Fontify error directives.
2368 '("^#[ \t]*error[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend)
2370 ;; Fontify filenames in #include <...> preprocessor directives as strings.
2371 '("^#[ \t]*\\(import\\|include\\)[ \t]*\\(<[^>\"\n]*>?\\)"
2372 2 font-lock-string-face)
2374 ;; Fontify function macro names.
2375 '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
2377 ;; Fontify symbol names in #elif or #if ... defined preprocessor directives.
2378 '("^#[ \t]*\\(elif\\|if\\)\\>"
2379 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
2380 (1 font-lock-builtin-face) (2 font-lock-variable-name-face nil t)))
2382 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
2383 '("^#[ \t]*\\(\\sw+\\)\\>[ \t!]*\\(\\sw+\\)?"
2384 (1 font-lock-builtin-face) (2 font-lock-variable-name-face nil t))
2387 (setq c-font-lock-keywords-2
2388 (append c-font-lock-keywords-1
2389 (list
2391 ;; Simple regexps for speed.
2393 ;; Fontify all type names.
2394 `(eval .
2395 (cons (concat "\\<\\(" (,@ c-type-names) "\\)\\>") 'font-lock-type-face))
2397 ;; Fontify all builtin keywords (except case, default and goto; see below).
2398 (concat "\\<\\(" c-keywords "\\|" c-type-specs "\\)\\>")
2400 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2401 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2402 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
2403 ;; Anders Lindgren <andersl@andersl.com> points out that it is quicker to
2404 ;; use MATCH-ANCHORED to effectively anchor the regexp on the left.
2405 ;; This must come after the one for keywords and targets.
2406 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2407 (beginning-of-line) (end-of-line)
2408 (1 font-lock-constant-face)))
2411 (setq c-font-lock-keywords-3
2412 (append c-font-lock-keywords-2
2414 ;; More complicated regexps for more complete highlighting for types.
2415 ;; We still have to fontify type specifiers individually, as C is so hairy.
2416 (list
2418 ;; Fontify all storage types, plus their items.
2419 `(eval .
2420 (list (concat "\\<\\(" (,@ c-type-names) "\\)\\>"
2421 "\\([ \t*&]+\\sw+\\>\\)*")
2422 ;; Fontify each declaration item.
2423 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2424 ;; Start with point after all type specifiers.
2425 (list 'goto-char (list 'or
2426 (list 'match-beginning
2427 (+ (,@ c-type-names-depth) 2))
2428 '(match-end 1)))
2429 ;; Finish with point after first type specifier.
2430 '(goto-char (match-end 1))
2431 ;; Fontify as a variable or function name.
2432 '(1 (if (match-beginning 2)
2433 font-lock-function-name-face
2434 font-lock-variable-name-face)))))
2436 ;; Fontify all storage specs and types, plus their items.
2437 `(eval .
2438 (list (concat "\\<\\(" (,@ c-type-specs) "\\)\\>"
2439 "[ \t]*\\(\\sw+\\)?")
2440 (list 1 'font-lock-keyword-face)
2441 (list (+ (,@ c-type-specs-depth) 2) 'font-lock-type-face nil t)
2442 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2443 nil nil
2444 ;; Fontify as a variable or function name.
2445 '(1 (if (match-beginning 2)
2446 font-lock-function-name-face
2447 font-lock-variable-name-face) nil t))))
2449 ;; Fontify structures, or typedef names, plus their items.
2450 '("\\(}\\)[ \t*]*\\sw"
2451 (font-lock-match-c-style-declaration-item-and-skip-to-next
2452 (goto-char (match-end 1)) nil
2453 (1 font-lock-type-face)))
2455 ;; Fontify anything at beginning of line as a declaration or definition.
2456 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2457 (1 font-lock-type-face)
2458 (font-lock-match-c-style-declaration-item-and-skip-to-next
2459 (goto-char (or (match-beginning 2) (match-end 1))) nil
2460 (1 (if (match-beginning 2)
2461 font-lock-function-name-face
2462 font-lock-variable-name-face))))
2466 (defvar c-font-lock-keywords c-font-lock-keywords-1
2467 "Default expressions to highlight in C mode.
2468 See also `c-font-lock-extra-types'.")
2470 ;;; C++.
2472 (defconst c++-font-lock-keywords-1 nil
2473 "Subdued level highlighting for C++ mode.")
2475 (defconst c++-font-lock-keywords-2 nil
2476 "Medium level highlighting for C++ mode.
2477 See also `c++-font-lock-extra-types'.")
2479 (defconst c++-font-lock-keywords-3 nil
2480 "Gaudy level highlighting for C++ mode.
2481 See also `c++-font-lock-extra-types'.")
2483 (defun font-lock-match-c++-style-declaration-item-and-skip-to-next (limit)
2484 ;; Regexp matches after point: word<word>::word (
2485 ;; ^^^^ ^^^^ ^^^^ ^
2486 ;; Where the match subexpressions are: 1 3 5 6
2488 ;; Item is delimited by (match-beginning 1) and (match-end 1).
2489 ;; If (match-beginning 3) is non-nil, that part of the item incloses a `<>'.
2490 ;; If (match-beginning 5) is non-nil, that part of the item follows a `::'.
2491 ;; If (match-beginning 6) is non-nil, the item is followed by a `('.
2492 (when (looking-at (eval-when-compile
2493 (concat
2494 ;; Skip any leading whitespace.
2495 "[ \t*&]*"
2496 ;; This is `c++-type-spec' from below. (Hint hint!)
2497 "\\(\\sw+\\)" ; The instance?
2498 "\\([ \t]*<\\([^>\n]+\\)[ \t*&]*>\\)?" ; Or template?
2499 "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)*" ; Or member?
2500 ;; Match any trailing parenthesis.
2501 "[ \t]*\\((\\)?")))
2502 (save-match-data
2503 (condition-case nil
2504 (save-restriction
2505 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
2506 (narrow-to-region (point-min) limit)
2507 (goto-char (match-end 1))
2508 ;; Move over any item value, etc., to the next item.
2509 (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)"))
2510 (goto-char (or (scan-sexps (point) 1) (point-max))))
2511 (goto-char (match-end 2)))
2512 (error t)))))
2514 (let* ((c++-keywords
2515 (eval-when-compile
2516 (regexp-opt
2517 '("break" "continue" "do" "else" "for" "if" "return" "switch"
2518 "while" "asm" "catch" "delete" "new" "sizeof" "this" "throw" "try"
2519 "typeid"
2520 ;; Branko Cibej <branko.cibej@hermes.si> says this is new.
2521 "export"
2522 ;; Mark Mitchell <mmitchell@usa.net> says these are new.
2523 "mutable" "explicit"
2524 ;; Alain Picard <ap@abelard.apana.org.au> suggests treating these
2525 ;; as keywords not types.
2526 "typedef" "template"
2527 "extern" "auto" "register" "const" "volatile" "static"
2528 "inline" "friend" "virtual") t)))
2529 (c++-operators
2530 (eval-when-compile
2531 (regexp-opt
2532 ;; Taken from Stroustrup, minus keywords otherwise fontified.
2533 '("+" "-" "*" "/" "%" "^" "&" "|" "~" "!" "=" "<" ">" "+=" "-="
2534 "*=" "/=" "%=" "^=" "&=" "|=" "<<" ">>" ">>=" "<<=" "==" "!="
2535 "<=" ">=" "&&" "||" "++" "--" "->*" "," "->" "[]" "()"))))
2536 (c++-type-specs
2537 (eval-when-compile
2538 (regexp-opt
2539 '("class" "public" "private" "protected" "typename"
2540 "struct" "union" "enum" "namespace" "using"
2541 ;; Eric Hopper <hopper@omnifarious.mn.org> says these are new.
2542 "static_cast" "dynamic_cast" "const_cast" "reinterpret_cast") t)))
2543 (c++-type-specs-depth
2544 (regexp-opt-depth c++-type-specs))
2545 (c++-type-names
2546 `(mapconcat 'identity
2547 (cons
2548 (,@ (eval-when-compile
2549 (regexp-opt
2550 '("signed" "unsigned" "short" "long"
2551 "int" "char" "float" "double" "void"
2552 "bool" "complex"))))
2553 c++-font-lock-extra-types)
2554 "\\|"))
2555 (c++-type-names-depth `(regexp-opt-depth (,@ c++-type-names)))
2557 ;; A brave attempt to match templates following a type and/or match
2558 ;; class membership. See and sync the above function
2559 ;; `font-lock-match-c++-style-declaration-item-and-skip-to-next'.
2560 (c++-type-suffix (concat "\\([ \t]*<\\([^>\n]+\\)[ \t*&]*>\\)?"
2561 "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)*"))
2562 (c++-type-suffix-depth (regexp-opt-depth c++-type-suffix))
2563 ;; If the string is a type, it may be followed by the cruft above.
2564 (c++-type-spec (concat "\\(\\sw+\\)\\>" c++-type-suffix))
2565 (c++-type-spec-depth (regexp-opt-depth c++-type-spec))
2567 ;; Parenthesis depth of user-defined types not forgetting their cruft.
2568 (c++-type-depth `(regexp-opt-depth
2569 (concat (,@ c++-type-names) (,@ c++-type-suffix))))
2571 (setq c++-font-lock-keywords-1
2572 (append
2574 ;; The list `c-font-lock-keywords-1' less that for function names.
2575 (cdr c-font-lock-keywords-1)
2576 (list
2578 ;; Fontify function name definitions, possibly incorporating class names.
2579 (list (concat "^" c++-type-spec "[ \t]*(")
2580 '(1 (if (or (match-beginning 2) (match-beginning 4))
2581 font-lock-type-face
2582 font-lock-function-name-face))
2583 '(3 font-lock-type-face nil t)
2584 '(5 font-lock-function-name-face nil t))
2587 (setq c++-font-lock-keywords-2
2588 (append c++-font-lock-keywords-1
2589 (list
2591 ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading.
2592 `(eval .
2593 (cons (concat "\\<\\(" (,@ c++-type-names) "\\)\\>")
2594 'font-lock-type-face))
2596 ;; Fontify operator overloading.
2597 (list (concat "\\<\\(operator\\)\\>[ \t]*\\(" c++-operators "\\)?")
2598 '(1 font-lock-keyword-face)
2599 '(2 font-lock-builtin-face nil t))
2601 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2602 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2603 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
2604 ;; This must come after the one for keywords and targets.
2605 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:\\($\\|[^:]\\)"
2606 (beginning-of-line) (end-of-line)
2607 (1 font-lock-constant-face)))
2609 ;; Fontify other builtin keywords.
2610 (concat "\\<\\(" c++-keywords "\\|" c++-type-specs "\\)\\>")
2612 ;; Eric Hopper <hopper@omnifarious.mn.org> says `true' and `false' are new.
2613 '("\\<\\(false\\|true\\)\\>" . font-lock-constant-face)
2616 (setq c++-font-lock-keywords-3
2617 (append c++-font-lock-keywords-2
2619 ;; More complicated regexps for more complete highlighting for types.
2620 (list
2622 ;; Fontify all storage classes and type specifiers, plus their items.
2623 `(eval .
2624 (list (concat "\\<\\(" (,@ c++-type-names) "\\)\\>" (,@ c++-type-suffix)
2625 "\\([ \t*&]+" (,@ c++-type-spec) "\\)*")
2626 ;; The name of any template type.
2627 (list (+ (,@ c++-type-names-depth) 3) 'font-lock-type-face nil t)
2628 ;; Fontify each declaration item.
2629 (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next
2630 ;; Start with point after all type specifiers.
2631 (list 'goto-char (list 'or (list 'match-beginning
2632 (+ (,@ c++-type-depth) 2))
2633 '(match-end 1)))
2634 ;; Finish with point after first type specifier.
2635 '(goto-char (match-end 1))
2636 ;; Fontify as a variable or function name.
2637 '(1 (cond ((or (match-beginning 2) (match-beginning 4))
2638 font-lock-type-face)
2639 ((and (match-beginning 6) (c-at-toplevel-p))
2640 font-lock-function-name-face)
2642 font-lock-variable-name-face)))
2643 '(3 font-lock-type-face nil t)
2644 '(5 (if (match-beginning 6)
2645 font-lock-function-name-face
2646 font-lock-variable-name-face) nil t))))
2648 ;; Fontify all storage specs and types, plus their items.
2649 `(eval .
2650 (list (concat "\\<" (,@ c++-type-specs) "\\>" (,@ c++-type-suffix)
2651 "[ \t]*\\(" (,@ c++-type-spec) "\\)?")
2652 ;; The name of any template type.
2653 (list (+ (,@ c++-type-specs-depth) 2) 'font-lock-type-face nil t)
2654 ;; The name of any type.
2655 (list (+ (,@ c++-type-specs-depth) (,@ c++-type-suffix-depth) 2)
2656 'font-lock-type-face nil t)
2657 ;; Fontify each declaration item.
2658 (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next
2659 ;; Start with point after all type specifiers.
2661 ;; Finish with point after first type specifier.
2663 ;; Fontify as a variable or function name.
2664 '(1 (cond ((or (match-beginning 2) (match-beginning 4))
2665 font-lock-type-face)
2666 ((and (match-beginning 6) (c-at-toplevel-p))
2667 font-lock-function-name-face)
2669 font-lock-variable-name-face)))
2670 '(3 font-lock-type-face nil t)
2671 '(5 (if (match-beginning 6)
2672 font-lock-function-name-face
2673 font-lock-variable-name-face) nil t))
2676 ;; Fontify structures, or typedef names, plus their items.
2677 '("\\(}\\)[ \t*]*\\sw"
2678 (font-lock-match-c++-style-declaration-item-and-skip-to-next
2679 (goto-char (match-end 1)) nil
2680 (1 font-lock-type-face)))
2682 ;; Fontify anything at beginning of line as a declaration or definition.
2683 (list (concat "^\\(" c++-type-spec "[ \t*&]*\\)+")
2684 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
2685 (goto-char (match-beginning 1))
2686 (goto-char (match-end 1))
2687 (1 (cond ((or (match-beginning 2) (match-beginning 4))
2688 font-lock-type-face)
2689 ((match-beginning 6) font-lock-function-name-face)
2690 (t font-lock-variable-name-face)))
2691 (3 font-lock-type-face nil t)
2692 (5 (if (match-beginning 6)
2693 font-lock-function-name-face
2694 font-lock-variable-name-face) nil t)))
2698 (defvar c++-font-lock-keywords c++-font-lock-keywords-1
2699 "Default expressions to highlight in C++ mode.
2700 See also `c++-font-lock-extra-types'.")
2702 ;;; Objective-C.
2704 (defconst objc-font-lock-keywords-1 nil
2705 "Subdued level highlighting for Objective-C mode.")
2707 (defconst objc-font-lock-keywords-2 nil
2708 "Medium level highlighting for Objective-C mode.
2709 See also `objc-font-lock-extra-types'.")
2711 (defconst objc-font-lock-keywords-3 nil
2712 "Gaudy level highlighting for Objective-C mode.
2713 See also `objc-font-lock-extra-types'.")
2715 ;; Regexps written with help from Stephen Peters <speters@us.oracle.com> and
2716 ;; Jacques Duthen Prestataire <duthen@cegelec-red.fr>.
2717 (let* ((objc-keywords
2718 (eval-when-compile
2719 (regexp-opt '("break" "continue" "do" "else" "for" "if" "return"
2720 "switch" "while" "sizeof" "self" "super"
2721 "typedef" "auto" "extern" "static"
2722 "volatile" "const") t)))
2723 (objc-type-specs
2724 (eval-when-compile
2725 (regexp-opt
2726 '("register" "struct" "union" "enum"
2727 "oneway" "in" "out" "inout" "bycopy" "byref") t)))
2728 (objc-type-specs-depth
2729 (regexp-opt-depth objc-type-specs))
2730 (objc-type-names
2731 `(mapconcat 'identity
2732 (cons
2733 (,@ (eval-when-compile
2734 (regexp-opt
2735 '("signed" "unsigned" "short" "long"
2736 "int" "char" "float" "double" "void"
2737 "id"))))
2738 objc-font-lock-extra-types)
2739 "\\|"))
2740 (objc-type-names-depth
2741 `(regexp-opt-depth (,@ objc-type-names)))
2743 (setq objc-font-lock-keywords-1
2744 (append
2746 ;; The list `c-font-lock-keywords-1' less that for function names.
2747 (cdr c-font-lock-keywords-1)
2748 (list
2750 ;; Fontify compiler directives.
2751 '("@\\(\\sw+\\)\\>"
2752 (1 font-lock-keyword-face)
2753 ("\\=[ \t:<,]*\\(\\sw+\\)" nil nil
2754 (1 font-lock-type-face)))
2756 ;; Fontify method names and arguments. Oh Lordy!
2757 ;; First, on the same line as the function declaration.
2758 '("^[+-][ \t]*\\(PRIVATE\\>\\)?[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2759 (1 font-lock-keyword-face nil t)
2760 (3 font-lock-function-name-face)
2761 ("\\=[ \t]*\\(\\sw+\\)?:[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2762 nil nil
2763 (1 font-lock-function-name-face nil t)
2764 (3 font-lock-variable-name-face)))
2765 ;; Second, on lines following the function declaration.
2766 '(":" ("^[ \t]*\\(\\sw+\\)?:[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2767 (beginning-of-line) (end-of-line)
2768 (1 font-lock-function-name-face nil t)
2769 (3 font-lock-variable-name-face)))
2772 (setq objc-font-lock-keywords-2
2773 (append objc-font-lock-keywords-1
2774 (list
2776 ;; Simple regexps for speed.
2778 ;; Fontify all type specifiers.
2779 `(eval .
2780 (cons (concat "\\<\\(" (,@ objc-type-names) "\\)\\>")
2781 'font-lock-type-face))
2783 ;; Fontify all builtin keywords (except case, default and goto; see below).
2784 (concat "\\<\\(" objc-keywords "\\|" objc-type-specs "\\)\\>")
2786 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2787 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2788 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
2789 ;; Fontify tags iff sole statement on line, otherwise we detect selectors.
2790 ;; This must come after the one for keywords and targets.
2791 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2792 (beginning-of-line) (end-of-line)
2793 (1 font-lock-constant-face)))
2795 ;; Fontify null object pointers.
2796 '("\\<[Nn]il\\>" . font-lock-constant-face)
2799 (setq objc-font-lock-keywords-3
2800 (append objc-font-lock-keywords-2
2802 ;; More complicated regexps for more complete highlighting for types.
2803 ;; We still have to fontify type specifiers individually, as C is so hairy.
2804 (list
2806 ;; Fontify all storage classes and type specifiers, plus their items.
2807 `(eval .
2808 (list (concat "\\<\\(" (,@ objc-type-names) "\\)\\>"
2809 "\\([ \t*&]+\\sw+\\>\\)*")
2810 ;; Fontify each declaration item.
2811 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2812 ;; Start with point after all type specifiers.
2813 (list 'goto-char
2814 (list 'or (list 'match-beginning
2815 (+ (,@ objc-type-names-depth) 2))
2816 '(match-end 1)))
2817 ;; Finish with point after first type specifier.
2818 '(goto-char (match-end 1))
2819 ;; Fontify as a variable or function name.
2820 '(1 (if (match-beginning 2)
2821 font-lock-function-name-face
2822 font-lock-variable-name-face)))))
2824 ;; Fontify all storage specs and types, plus their items.
2825 `(eval .
2826 (list (concat "\\<\\(" (,@ objc-type-specs) "[ \t]*\\)+\\>"
2827 "[ \t]*\\(\\sw+\\)?")
2828 ;; The name of any type.
2829 (list (+ (,@ objc-type-specs-depth) 2) 'font-lock-type-face nil t)
2830 ;; Fontify each declaration item.
2831 (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next
2832 nil nil
2833 ;; Fontify as a variable or function name.
2834 '(1 (if (match-beginning 2)
2835 font-lock-function-name-face
2836 font-lock-variable-name-face)))
2839 ;; Fontify structures, or typedef names, plus their items.
2840 '("\\(}\\)[ \t*]*\\sw"
2841 (font-lock-match-c-style-declaration-item-and-skip-to-next
2842 (goto-char (match-end 1)) nil
2843 (1 font-lock-type-face)))
2845 ;; Fontify anything at beginning of line as a declaration or definition.
2846 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2847 (1 font-lock-type-face)
2848 (font-lock-match-c-style-declaration-item-and-skip-to-next
2849 (goto-char (or (match-beginning 2) (match-end 1))) nil
2850 (1 (if (match-beginning 2)
2851 font-lock-function-name-face
2852 font-lock-variable-name-face))))
2856 (defvar objc-font-lock-keywords objc-font-lock-keywords-1
2857 "Default expressions to highlight in Objective-C mode.
2858 See also `objc-font-lock-extra-types'.")
2860 ;;; Java.
2862 (defconst java-font-lock-keywords-1 nil
2863 "Subdued level highlighting for Java mode.")
2865 (defconst java-font-lock-keywords-2 nil
2866 "Medium level highlighting for Java mode.
2867 See also `java-font-lock-extra-types'.")
2869 (defconst java-font-lock-keywords-3 nil
2870 "Gaudy level highlighting for Java mode.
2871 See also `java-font-lock-extra-types'.")
2873 ;; Regexps written with help from Fred White <fwhite@bbn.com>,
2874 ;; Anders Lindgren <andersl@andersl.com> and Carl Manning <caroma@ai.mit.edu>.
2875 (let* ((java-keywords
2876 (eval-when-compile
2877 (regexp-opt
2878 '("catch" "do" "else" "super" "this" "finally" "for" "if"
2879 ;; Anders Lindgren <andersl@andersl.com> says these have gone.
2880 ;; "cast" "byvalue" "future" "generic" "operator" "var"
2881 ;; "inner" "outer" "rest"
2882 "implements" "extends" "throws" "instanceof" "new"
2883 "interface" "return" "switch" "throw" "try" "while") t)))
2885 ;; Classes immediately followed by an object name.
2886 (java-type-names
2887 `(mapconcat 'identity
2888 (cons
2889 (,@ (eval-when-compile
2890 (regexp-opt '("boolean" "char" "byte" "short" "int" "long"
2891 "float" "double" "void"))))
2892 java-font-lock-extra-types)
2893 "\\|"))
2894 (java-type-names-depth `(regexp-opt-depth (,@ java-type-names)))
2896 ;; These are eventually followed by an object name.
2897 (java-type-specs
2898 (eval-when-compile
2899 (regexp-opt
2900 '("abstract" "const" "final" "synchronized" "transient" "static"
2901 ;; Anders Lindgren <andersl@andersl.com> says this has gone.
2902 ;; "threadsafe"
2903 "volatile" "public" "private" "protected" "native"
2904 ;; Carl Manning <caroma@ai.mit.edu> says this is new.
2905 "strictfp"))))
2907 (setq java-font-lock-keywords-1
2908 (list
2910 ;; Fontify class names.
2911 '("\\<\\(class\\)\\>[ \t]*\\(\\sw+\\)?"
2912 (1 font-lock-keyword-face) (2 font-lock-type-face nil t))
2914 ;; Fontify package names in import directives.
2915 '("\\<\\(import\\|package\\)\\>[ \t]*\\(\\sw+\\)?"
2916 (1 font-lock-keyword-face)
2917 (2 font-lock-constant-face nil t)
2918 ("\\=\\.\\(\\*\\|\\sw+\\)" nil nil
2919 (1 font-lock-constant-face nil t)))
2922 (setq java-font-lock-keywords-2
2923 (append java-font-lock-keywords-1
2924 (list
2926 ;; Fontify class names.
2927 `(eval .
2928 (cons (concat "\\<\\(" (,@ java-type-names) "\\)\\>[^.]")
2929 '(1 font-lock-type-face)))
2931 ;; Fontify all builtin keywords (except below).
2932 (concat "\\<\\(" java-keywords "\\|" java-type-specs "\\)\\>")
2934 ;; Fontify keywords and targets, and case default/goto tags.
2935 (list "\\<\\(break\\|case\\|continue\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2936 '(1 font-lock-keyword-face) '(2 font-lock-constant-face nil t))
2937 ;; This must come after the one for keywords and targets.
2938 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2939 (beginning-of-line) (end-of-line)
2940 (1 font-lock-constant-face)))
2942 ;; Fontify all constants.
2943 '("\\<\\(false\\|null\\|true\\)\\>" . font-lock-constant-face)
2945 ;; Javadoc tags within comments.
2946 '("@\\(author\\|exception\\|return\\|see\\|version\\)\\>"
2947 (1 font-lock-constant-face prepend))
2948 '("@\\(param\\)\\>[ \t]*\\(\\sw+\\)?"
2949 (1 font-lock-constant-face prepend)
2950 (2 font-lock-variable-name-face prepend t))
2953 (setq java-font-lock-keywords-3
2954 (append java-font-lock-keywords-2
2956 ;; More complicated regexps for more complete highlighting for types.
2957 ;; We still have to fontify type specifiers individually, as Java is hairy.
2958 (list
2960 ;; Fontify random types immediately followed by an item or items.
2961 `(eval .
2962 (list (concat "\\<\\(" (,@ java-type-names) "\\)\\>"
2963 "\\([ \t]*\\[[ \t]*\\]\\)*"
2964 "\\([ \t]*\\sw\\)")
2965 ;; Fontify each declaration item.
2966 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2967 ;; Start and finish with point after the type specifier.
2968 (list 'goto-char (list 'match-beginning
2969 (+ (,@ java-type-names-depth) 3)))
2970 (list 'goto-char (list 'match-beginning
2971 (+ (,@ java-type-names-depth) 3)))
2972 ;; Fontify as a variable or function name.
2973 '(1 (if (match-beginning 2)
2974 font-lock-function-name-face
2975 font-lock-variable-name-face)))))
2977 ;; Fontify those that are eventually followed by an item or items.
2978 (list (concat "\\<\\(" java-type-specs "\\)\\>"
2979 "\\([ \t]+\\sw+\\>"
2980 "\\([ \t]*\\[[ \t]*\\]\\)*"
2981 "\\)*")
2982 ;; Fontify each declaration item.
2983 '(font-lock-match-c-style-declaration-item-and-skip-to-next
2984 ;; Start with point after all type specifiers.
2985 (goto-char (or (match-beginning 5) (match-end 1)))
2986 ;; Finish with point after first type specifier.
2987 (goto-char (match-end 1))
2988 ;; Fontify as a variable or function name.
2989 (1 (if (match-beginning 2)
2990 font-lock-function-name-face
2991 font-lock-variable-name-face))))
2995 (defvar java-font-lock-keywords java-font-lock-keywords-1
2996 "Default expressions to highlight in Java mode.
2997 See also `java-font-lock-extra-types'.")
2999 ;; Install ourselves:
3001 (unless (assq 'font-lock-mode minor-mode-alist)
3002 (push '(font-lock-mode nil) minor-mode-alist))
3004 ;; Provide ourselves:
3006 (provide 'font-lock)
3008 ;;; font-lock.el ends here