Remove CVS merge cookie left in.
[emacs.git] / lisp / font-lock.el
blob463c94e21277a43211b89fd5d96e4b34c99c1965
1 ;;; font-lock.el --- Electric font lock mode
3 ;; Copyright (C) 1992, 93, 94, 95, 96, 97, 98, 1999, 2000
4 ;; Free Software Foundation, Inc.
6 ;; Author: jwz, then rms, then sm
7 ;; Maintainer: FSF
8 ;; Keywords: languages, faces
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; Font Lock mode is a minor mode that causes your comments to be displayed in
30 ;; one face, strings in another, reserved words in another, and so on.
32 ;; Comments will be displayed in `font-lock-comment-face'.
33 ;; Strings will be displayed in `font-lock-string-face'.
34 ;; Regexps are used to display selected patterns in other faces.
36 ;; To make the text you type be fontified, use M-x font-lock-mode RET.
37 ;; When this minor mode is on, the faces of the current line are updated with
38 ;; every insertion or deletion.
40 ;; To turn Font Lock mode on automatically, add this to your ~/.emacs file:
42 ;; (add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock)
44 ;; Or if you want to turn Font Lock mode on in many modes:
46 ;; (global-font-lock-mode t)
48 ;; Fontification for a particular mode may be available in a number of levels
49 ;; of decoration. The higher the level, the more decoration, but the more time
50 ;; it takes to fontify. See the variable `font-lock-maximum-decoration', and
51 ;; also the variable `font-lock-maximum-size'. Support modes for Font Lock
52 ;; mode can be used to speed up Font Lock mode. See `font-lock-support-mode'.
54 ;;; How Font Lock mode fontifies:
56 ;; When Font Lock mode is turned on in a buffer, it (a) fontifies the entire
57 ;; buffer and (b) installs one of its fontification functions on one of the
58 ;; hook variables that are run by Emacs after every buffer change (i.e., an
59 ;; insertion or deletion). Fontification means the replacement of `face' text
60 ;; properties in a given region; Emacs displays text with these `face' text
61 ;; properties appropriately.
63 ;; Fontification normally involves syntactic (i.e., strings and comments) and
64 ;; regexp (i.e., keywords and everything else) passes. There are actually
65 ;; three passes; (a) the syntactic keyword pass, (b) the syntactic pass and (c)
66 ;; the keyword pass. Confused?
68 ;; The syntactic keyword pass places `syntax-table' text properties in the
69 ;; buffer according to the variable `font-lock-syntactic-keywords'. It is
70 ;; necessary because Emacs' syntax table is not powerful enough to describe all
71 ;; the different syntactic constructs required by the sort of people who decide
72 ;; that a single quote can be syntactic or not depending on the time of day.
73 ;; (What sort of person could decide to overload the meaning of a quote?)
74 ;; Obviously the syntactic keyword pass must occur before the syntactic pass.
76 ;; The syntactic pass places `face' text properties in the buffer according to
77 ;; syntactic context, i.e., according to the buffer's syntax table and buffer
78 ;; text's `syntax-table' text properties. It involves using a syntax parsing
79 ;; function to determine the context of different parts of a region of text. A
80 ;; syntax parsing function is necessary because generally strings and/or
81 ;; comments can span lines, and so the context of a given region is not
82 ;; necessarily apparent from the content of that region. Because the keyword
83 ;; pass only works within a given region, it is not generally appropriate for
84 ;; syntactic fontification. This is the first fontification pass that makes
85 ;; changes visible to the user; it fontifies strings and comments.
87 ;; The keyword pass places `face' text properties in the buffer according to
88 ;; the variable `font-lock-keywords'. It involves searching for given regexps
89 ;; (or calling given search functions) within the given region. This is the
90 ;; second fontification pass that makes changes visible to the user; it
91 ;; fontifies language reserved words, etc.
93 ;; Oh, and the answer is, "Yes, obviously just about everything should be done
94 ;; in a single syntactic pass, but the only syntactic parser available
95 ;; understands only strings and comments." Perhaps one day someone will write
96 ;; some syntactic parsers for common languages and a son-of-font-lock.el could
97 ;; use them rather then relying so heavily on the keyword (regexp) pass.
99 ;;; How Font Lock mode supports modes or is supported by modes:
101 ;; Modes that support Font Lock mode do so by defining one or more variables
102 ;; whose values specify the fontification. Font Lock mode knows of these
103 ;; variable names from (a) the buffer local variable `font-lock-defaults', if
104 ;; non-nil, or (b) the global variable `font-lock-defaults-alist', if the major
105 ;; mode has an entry. (Font Lock mode is set up via (a) where a mode's
106 ;; patterns are distributed with the mode's package library, and (b) where a
107 ;; mode's patterns are distributed with font-lock.el itself. An example of (a)
108 ;; is Pascal mode, an example of (b) is Lisp mode. Normally, the mechanism is
109 ;; (a); (b) is used where it is not clear which package library should contain
110 ;; the pattern definitions.) Font Lock mode chooses which variable to use for
111 ;; fontification based on `font-lock-maximum-decoration'.
113 ;; Font Lock mode fontification behaviour can be modified in a number of ways.
114 ;; See the below comments and the comments distributed throughout this file.
116 ;;; Constructing patterns:
118 ;; See the documentation for the variable `font-lock-keywords'.
120 ;; Efficient regexps for use as MATCHERs for `font-lock-keywords' and
121 ;; `font-lock-syntactic-keywords' can be generated via the function
122 ;; `regexp-opt'.
124 ;;; Adding patterns for modes that already support Font Lock:
126 ;; Though Font Lock highlighting patterns already exist for many modes, it's
127 ;; likely there's something that you want fontified that currently isn't, even
128 ;; at the maximum fontification level. You can add highlighting patterns via
129 ;; `font-lock-add-keywords'. For example, say in some C
130 ;; header file you #define the token `and' to expand to `&&', etc., to make
131 ;; your C code almost readable. In your ~/.emacs there could be:
133 ;; (font-lock-add-keywords 'c-mode '("\\<\\(and\\|or\\|not\\)\\>"))
135 ;; Some modes provide specific ways to modify patterns based on the values of
136 ;; other variables. For example, additional C types can be specified via the
137 ;; variable `c-font-lock-extra-types'.
139 ;;; Adding patterns for modes that do not support Font Lock:
141 ;; Not all modes support Font Lock mode. If you (as a user of the mode) add
142 ;; patterns for a new mode, you must define in your ~/.emacs a variable or
143 ;; variables that specify regexp fontification. Then, you should indicate to
144 ;; Font Lock mode, via the mode hook setting `font-lock-defaults', exactly what
145 ;; support is required. For example, say Foo mode should have the following
146 ;; regexps fontified case-sensitively, and comments and strings should not be
147 ;; fontified automagically. In your ~/.emacs there could be:
149 ;; (defvar foo-font-lock-keywords
150 ;; '(("\\<\\(one\\|two\\|three\\)\\>" . font-lock-keyword-face)
151 ;; ("\\<\\(four\\|five\\|six\\)\\>" . font-lock-type-face))
152 ;; "Default expressions to highlight in Foo mode.")
154 ;; (add-hook 'foo-mode-hook
155 ;; (function (lambda ()
156 ;; (make-local-variable 'font-lock-defaults)
157 ;; (setq font-lock-defaults '(foo-font-lock-keywords t)))))
159 ;;; Adding Font Lock support for modes:
161 ;; Of course, it would be better that the mode already supports Font Lock mode.
162 ;; The package author would do something similar to above. The mode must
163 ;; define at the top-level a variable or variables that specify regexp
164 ;; fontification. Then, the mode command should indicate to Font Lock mode,
165 ;; via `font-lock-defaults', exactly what support is required. For example,
166 ;; say Bar mode should have the following regexps fontified case-insensitively,
167 ;; and comments and strings should be fontified automagically. In bar.el there
168 ;; could be:
170 ;; (defvar bar-font-lock-keywords
171 ;; '(("\\<\\(uno\\|due\\|tre\\)\\>" . font-lock-keyword-face)
172 ;; ("\\<\\(quattro\\|cinque\\|sei\\)\\>" . font-lock-type-face))
173 ;; "Default expressions to highlight in Bar mode.")
175 ;; and within `bar-mode' there could be:
177 ;; (make-local-variable 'font-lock-defaults)
178 ;; (setq font-lock-defaults '(bar-font-lock-keywords nil t))
180 ;; What is fontification for? You might say, "It's to make my code look nice."
181 ;; I think it should be for adding information in the form of cues. These cues
182 ;; should provide you with enough information to both (a) distinguish between
183 ;; different items, and (b) identify the item meanings, without having to read
184 ;; the items and think about it. Therefore, fontification allows you to think
185 ;; less about, say, the structure of code, and more about, say, why the code
186 ;; doesn't work. Or maybe it allows you to think less and drift off to sleep.
188 ;; So, here are my opinions/advice/guidelines:
190 ;; - Highlight conceptual objects, such as function and variable names, and
191 ;; different objects types differently, i.e., (a) and (b) above, highlight
192 ;; function names differently to variable names.
193 ;; - Keep the faces distinct from each other as far as possible.
194 ;; i.e., (a) above.
195 ;; - Use the same face for the same conceptual object, across all modes.
196 ;; i.e., (b) above, all modes that have items that can be thought of as, say,
197 ;; keywords, should be highlighted with the same face, etc.
198 ;; - Make the face attributes fit the concept as far as possible.
199 ;; i.e., function names might be a bold colour such as blue, comments might
200 ;; be a bright colour such as red, character strings might be brown, because,
201 ;; err, strings are brown (that was not the reason, please believe me).
202 ;; - Don't use a non-nil OVERRIDE unless you have a good reason.
203 ;; Only use OVERRIDE for special things that are easy to define, such as the
204 ;; way `...' quotes are treated in strings and comments in Emacs Lisp mode.
205 ;; Don't use it to, say, highlight keywords in commented out code or strings.
206 ;; - Err, that's it.
208 ;;; Code:
210 ;; Define core `font-lock' group.
211 (defgroup font-lock nil
212 "Font Lock mode text highlighting package."
213 :link '(custom-manual "(emacs)Font Lock")
214 :link '(custom-manual "(elisp)Font Lock Mode")
215 :group 'faces)
217 (defgroup font-lock-highlighting-faces nil
218 "Faces for highlighting text."
219 :prefix "font-lock-"
220 :group 'font-lock)
222 (defgroup font-lock-extra-types nil
223 "Extra mode-specific type names for highlighting declarations."
224 :group 'font-lock)
226 ;; Define support mode groups here to impose `font-lock' group order.
227 (defgroup fast-lock nil
228 "Font Lock support mode to cache fontification."
229 :link '(custom-manual "(emacs)Support Modes")
230 :load 'fast-lock
231 :group 'font-lock)
233 (defgroup lazy-lock nil
234 "Font Lock support mode to fontify lazily."
235 :link '(custom-manual "(emacs)Support Modes")
236 :load 'lazy-lock
237 :group 'font-lock)
239 (defgroup jit-lock nil
240 "Font Lock support mode to fontify just-in-time."
241 :link '(custom-manual "(emacs)Support Modes")
242 :version "21.1"
243 :load 'jit-lock
244 :group 'font-lock)
246 ;; User variables.
248 (defcustom font-lock-maximum-size 256000
249 "*Maximum size of a buffer for buffer fontification.
250 Only buffers less than this can be fontified when Font Lock mode is turned on.
251 If nil, means size is irrelevant.
252 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
253 where MAJOR-MODE is a symbol or t (meaning the default). For example:
254 ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576))
255 means that the maximum size is 250K for buffers in C or C++ modes, one megabyte
256 for buffers in Rmail mode, and size is irrelevant otherwise."
257 :type '(choice (const :tag "none" nil)
258 (integer :tag "size")
259 (repeat :menu-tag "mode specific" :tag "mode specific"
260 :value ((t . nil))
261 (cons :tag "Instance"
262 (radio :tag "Mode"
263 (const :tag "all" t)
264 (symbol :tag "name"))
265 (radio :tag "Size"
266 (const :tag "none" nil)
267 (integer :tag "size")))))
268 :group 'font-lock)
270 (defcustom font-lock-maximum-decoration t
271 "*Maximum decoration level for fontification.
272 If nil, use the default decoration (typically the minimum available).
273 If t, use the maximum decoration available.
274 If a number, use that level of decoration (or if not available the maximum).
275 If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
276 where MAJOR-MODE is a symbol or t (meaning the default). For example:
277 ((c-mode . t) (c++-mode . 2) (t . 1))
278 means use the maximum decoration available for buffers in C mode, level 2
279 decoration for buffers in C++ mode, and level 1 decoration otherwise."
280 :type '(choice (const :tag "default" nil)
281 (const :tag "maximum" t)
282 (integer :tag "level" 1)
283 (repeat :menu-tag "mode specific" :tag "mode specific"
284 :value ((t . t))
285 (cons :tag "Instance"
286 (radio :tag "Mode"
287 (const :tag "all" t)
288 (symbol :tag "name"))
289 (radio :tag "Decoration"
290 (const :tag "default" nil)
291 (const :tag "maximum" t)
292 (integer :tag "level" 1)))))
293 :group 'font-lock)
295 (defcustom font-lock-verbose 0
296 "*If non-nil, means show status messages for buffer fontification.
297 If a number, only buffers greater than this size have fontification messages."
298 :type '(choice (const :tag "never" nil)
299 (other :tag "always" t)
300 (integer :tag "size"))
301 :group 'font-lock)
303 ;; Fontification variables:
305 (defvar font-lock-keywords nil
306 "A list of the keywords to highlight.
307 Each element should have one of these forms:
309 MATCHER
310 (MATCHER . MATCH)
311 (MATCHER . FACENAME)
312 (MATCHER . HIGHLIGHT)
313 (MATCHER HIGHLIGHT ...)
314 (eval . FORM)
316 where HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
318 FORM is an expression, whose value should be a keyword element, evaluated when
319 the keyword is (first) used in a buffer. This feature can be used to provide a
320 keyword that can only be generated when Font Lock mode is actually turned on.
322 For highlighting single items, for example each instance of the word \"foo\",
323 typically only MATCH-HIGHLIGHT is required.
324 However, if an item or (typically) items are to be highlighted following the
325 instance of another item (the anchor), for example each instance of the
326 word \"bar\" following the word \"anchor\" then MATCH-ANCHORED may be required.
328 MATCH-HIGHLIGHT should be of the form:
330 (MATCH FACENAME OVERRIDE LAXMATCH)
332 where MATCHER can be either the regexp to search for, or the function name to
333 call to make the search (called with one argument, the limit of the search) and
334 return non-nil if it succeeds (and set `match-data' appropriately).
335 MATCHER regexps can be generated via the function `regexp-opt'. MATCH is
336 the subexpression of MATCHER to be highlighted. FACENAME is an expression
337 whose value is the face name to use. Face default attributes can be
338 modified via \\[customize].
340 OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification can
341 be overwritten. If `keep', only parts not already fontified are highlighted.
342 If `prepend' or `append', existing fontification is merged with the new, in
343 which the new or existing fontification, respectively, takes precedence.
344 If LAXMATCH is non-nil, no error is signaled if there is no MATCH in MATCHER.
346 For example, an element of the form highlights (if not already highlighted):
348 \"\\\\\\=<foo\\\\\\=>\" discrete occurrences of \"foo\" in the value of the
349 variable `font-lock-keyword-face'.
350 (\"fu\\\\(bar\\\\)\" . 1) substring \"bar\" within all occurrences of \"fubar\" in
351 the value of `font-lock-keyword-face'.
352 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'.
353 (\"foo\\\\|bar\" 0 foo-bar-face t)
354 occurrences of either \"foo\" or \"bar\" in the value
355 of `foo-bar-face', even if already highlighted.
356 (fubar-match 1 fubar-face)
357 the first subexpression within all occurrences of
358 whatever the function `fubar-match' finds and matches
359 in the value of `fubar-face'.
361 MATCH-ANCHORED should be of the form:
363 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
365 where MATCHER is a regexp to search for or the function name to call to make
366 the search, as for MATCH-HIGHLIGHT above, but with one exception; see below.
367 PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
368 the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be
369 used to initialise before, and cleanup after, MATCHER is used. Typically,
370 PRE-MATCH-FORM is used to move to some position relative to the original
371 MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might
372 be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER.
374 For example, an element of the form highlights (if not already highlighted):
376 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
378 discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
379 discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
380 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is
381 initially searched for starting from the end of the match of \"anchor\", and
382 searching for subsequent instance of \"anchor\" resumes from where searching
383 for \"item\" concluded.)
385 The above-mentioned exception is as follows. The limit of the MATCHER search
386 defaults to the end of the line after PRE-MATCH-FORM is evaluated.
387 However, if PRE-MATCH-FORM returns a position greater than the position after
388 PRE-MATCH-FORM is evaluated, that position is used as the limit of the search.
389 It is generally a bad idea to return a position greater than the end of the
390 line, i.e., cause the MATCHER search to span lines.
392 These regular expressions can match text which spans lines, although
393 it is better to avoid it if possible since updating them while editing
394 text is slower, and it is not guaranteed to be always correct when using
395 support modes like jit-lock or lazy-lock.
397 This variable is set by major modes via the variable `font-lock-defaults'.
398 Be careful when composing regexps for this list; a poorly written pattern can
399 dramatically slow things down!")
401 ;; This variable is used by mode packages that support Font Lock mode by
402 ;; defining their own keywords to use for `font-lock-keywords'. (The mode
403 ;; command should make it buffer-local and set it to provide the set up.)
404 (defvar font-lock-defaults nil
405 "Defaults for Font Lock mode specified by the major mode.
406 Defaults should be of the form:
408 (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN ...)
410 KEYWORDS may be a symbol (a variable or function whose value is the keywords to
411 use for fontification) or a list of symbols. If KEYWORDS-ONLY is non-nil,
412 syntactic fontification (strings and comments) is not performed.
413 If CASE-FOLD is non-nil, the case of the keywords is ignored when fontifying.
414 If SYNTAX-ALIST is non-nil, it should be a list of cons pairs of the form
415 \(CHAR-OR-STRING . STRING) used to set the local Font Lock syntax table, for
416 keyword and syntactic fontification (see `modify-syntax-entry').
418 If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
419 backwards outside any enclosing syntactic block, for syntactic fontification.
420 Typical values are `beginning-of-line' (i.e., the start of the line is known to
421 be outside a syntactic block), or `beginning-of-defun' for programming modes or
422 `backward-paragraph' for textual modes (i.e., the mode-dependent function is
423 known to move outside a syntactic block). If nil, the beginning of the buffer
424 is used as a position outside of a syntactic block, in the worst case.
426 These item elements are used by Font Lock mode to set the variables
427 `font-lock-keywords', `font-lock-keywords-only',
428 `font-lock-keywords-case-fold-search', `font-lock-syntax-table' and
429 `font-lock-beginning-of-syntax-function', respectively.
431 Further item elements are alists of the form (VARIABLE . VALUE) and are in no
432 particular order. Each VARIABLE is made buffer-local before set to VALUE.
434 Currently, appropriate variables include `font-lock-mark-block-function'.
435 If this is non-nil, it should be a function with no args used to mark any
436 enclosing block of text, for fontification via \\[font-lock-fontify-block].
437 Typical values are `mark-defun' for programming modes or `mark-paragraph' for
438 textual modes (i.e., the mode-dependent function is known to put point and mark
439 around a text block relevant to that mode).
441 Other variables include that for syntactic keyword fontification,
442 `font-lock-syntactic-keywords'
443 and those for buffer-specialised fontification functions,
444 `font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function',
445 `font-lock-fontify-region-function', `font-lock-unfontify-region-function',
446 `font-lock-inhibit-thing-lock' and `font-lock-maximum-size'.")
447 ;;;###autoload
448 (make-variable-buffer-local 'font-lock-defaults)
450 ;; This variable is used where font-lock.el itself supplies the keywords.
451 (defvar font-lock-defaults-alist
452 (let (;; We use `beginning-of-defun', rather than nil, for SYNTAX-BEGIN.
453 ;; Thus the calculation of the cache is usually faster but not
454 ;; infallible, so we risk mis-fontification. sm.
455 (c-mode-defaults
456 '((c-font-lock-keywords c-font-lock-keywords-1
457 c-font-lock-keywords-2 c-font-lock-keywords-3)
458 nil nil ((?_ . "w")) beginning-of-defun
459 (font-lock-mark-block-function . mark-defun)))
460 (c++-mode-defaults
461 '((c++-font-lock-keywords c++-font-lock-keywords-1
462 c++-font-lock-keywords-2 c++-font-lock-keywords-3)
463 nil nil ((?_ . "w")) beginning-of-defun
464 (font-lock-mark-block-function . mark-defun)))
465 (objc-mode-defaults
466 '((objc-font-lock-keywords objc-font-lock-keywords-1
467 objc-font-lock-keywords-2 objc-font-lock-keywords-3)
468 nil nil ((?_ . "w") (?$ . "w")) nil
469 (font-lock-mark-block-function . mark-defun)))
470 (java-mode-defaults
471 '((java-font-lock-keywords java-font-lock-keywords-1
472 java-font-lock-keywords-2 java-font-lock-keywords-3)
473 nil nil ((?_ . "w") (?$ . "w")) nil
474 (font-lock-mark-block-function . mark-defun)))
475 (lisp-mode-defaults
476 '((lisp-font-lock-keywords
477 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
478 nil nil (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
479 (font-lock-mark-block-function . mark-defun))))
480 (list
481 (cons 'c-mode c-mode-defaults)
482 (cons 'c++-mode c++-mode-defaults)
483 (cons 'objc-mode objc-mode-defaults)
484 (cons 'java-mode java-mode-defaults)
485 (cons 'emacs-lisp-mode lisp-mode-defaults)
486 (cons 'lisp-mode lisp-mode-defaults)
487 (cons 'lisp-interaction-mode lisp-mode-defaults)))
488 "Alist of fall-back Font Lock defaults for major modes.
489 Each item should be a list of the form:
491 (MAJOR-MODE . FONT-LOCK-DEFAULTS)
493 where MAJOR-MODE is a symbol and FONT-LOCK-DEFAULTS is a list of default
494 settings. See the variable `font-lock-defaults', which takes precedence.")
496 (defvar font-lock-keywords-alist nil
497 "*Alist of `font-lock-keywords' local to a `major-mode'.
498 This is normally set via `font-lock-add-keywords' and
499 `font-lock-remove-keywords'.")
501 (defvar font-lock-removed-keywords-alist nil
502 "*Alist of `font-lock-keywords' removed from `major-mode'.
503 This is normally set via `font-lock-add-keywords' and
504 `font-lock-remove-keywords'.")
506 (defvar font-lock-keywords-only nil
507 "*Non-nil means Font Lock should not fontify comments or strings.
508 This is normally set via `font-lock-defaults'.")
510 (defvar font-lock-keywords-case-fold-search nil
511 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
512 This is normally set via `font-lock-defaults'.")
514 (defvar font-lock-syntactic-keywords nil
515 "A list of the syntactic keywords to highlight.
516 Can be the list or the name of a function or variable whose value is the list.
517 See `font-lock-keywords' for a description of the form of this list;
518 the differences are listed below. MATCH-HIGHLIGHT should be of the form:
520 (MATCH SYNTAX OVERRIDE LAXMATCH)
522 where SYNTAX can be of the form (SYNTAX-CODE . MATCHING-CHAR) (see
523 also `string-to-syntax'), the name of a syntax table, or an expression
524 whose value is such a form or a syntax table. OVERRIDE cannot be
525 `prepend' or `append'.
527 For example, an element of the form highlights syntactically:
529 (\"\\\\$\\\\(#\\\\)\" 1 (1 . nil))
531 a hash character when following a dollar character, with a SYNTAX-CODE of
532 1 (meaning punctuation syntax). Assuming that the buffer syntax table does
533 specify hash characters to have comment start syntax, the element will only
534 highlight hash characters that do not follow dollar characters as comments
535 syntactically.
537 (\"\\\\('\\\\).\\\\('\\\\)\"
538 (1 (7 . ?'))
539 (2 (7 . ?')))
541 both single quotes which surround a single character, with a SYNTAX-CODE of
542 7 (meaning string quote syntax) and a MATCHING-CHAR of a single quote (meaning
543 a single quote matches a single quote). Assuming that the buffer syntax table
544 does not specify single quotes to have quote syntax, the element will only
545 highlight single quotes of the form 'c' as strings syntactically.
546 Other forms, such as foo'bar or 'fubar', will not be highlighted as strings.
548 This is normally set via `font-lock-defaults'.")
550 (defvar font-lock-syntax-table nil
551 "Non-nil means use this syntax table for fontifying.
552 If this is nil, the major mode's syntax table is used.
553 This is normally set via `font-lock-defaults'.")
555 ;; If this is nil, we only use the beginning of the buffer if we can't use
556 ;; `font-lock-cache-position' and `font-lock-cache-state'.
557 (defvar font-lock-beginning-of-syntax-function nil
558 "*Non-nil means use this function to move back outside of a syntactic block.
559 When called with no args it should leave point at the beginning of any
560 enclosing syntactic block.
561 If this is nil, the beginning of the buffer is used (in the worst case).
562 This is normally set via `font-lock-defaults'.")
564 (defvar font-lock-mark-block-function nil
565 "*Non-nil means use this function to mark a block of text.
566 When called with no args it should leave point at the beginning of any
567 enclosing textual block and mark at the end.
568 This is normally set via `font-lock-defaults'.")
570 (defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
571 "Function to use for fontifying the buffer.
572 This is normally set via `font-lock-defaults'.")
574 (defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
575 "Function to use for unfontifying the buffer.
576 This is used when turning off Font Lock mode.
577 This is normally set via `font-lock-defaults'.")
579 (defvar font-lock-fontify-region-function 'font-lock-default-fontify-region
580 "Function to use for fontifying a region.
581 It should take two args, the beginning and end of the region, and an optional
582 third arg VERBOSE. If non-nil, the function should print status messages.
583 This is normally set via `font-lock-defaults'.")
585 (defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
586 "Function to use for unfontifying a region.
587 It should take two args, the beginning and end of the region.
588 This is normally set via `font-lock-defaults'.")
590 (defvar font-lock-inhibit-thing-lock nil
591 "List of Font Lock mode related modes that should not be turned on.
592 Currently, valid mode names are `fast-lock-mode', `jit-lock-mode' and
593 `lazy-lock-mode'. This is normally set via `font-lock-defaults'.")
595 (defvar font-lock-multiline 'undecided
596 "Whether font-lock should cater to multiline keywords.
597 If nil, don't try to handle multiline patterns.
598 If t, always handle multiline patterns.
599 If `undecided', don't try to handle multiline patterns until you see one.
600 Major/minor modes can set this variable if they know which option applies.")
602 (defvar font-lock-mode nil) ; Whether we are turned on/modeline.
603 (defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
605 ;;;###autoload
606 (defvar font-lock-mode-hook nil
607 "Function or functions to run on entry to Font Lock mode.")
609 ;; Font Lock mode.
611 (eval-when-compile
613 ;; We don't do this at the top-level as we only use non-autoloaded macros.
614 (require 'cl)
616 ;; Borrowed from lazy-lock.el.
617 ;; We use this to preserve or protect things when modifying text properties.
618 (defmacro save-buffer-state (varlist &rest body)
619 "Bind variables according to VARLIST and eval BODY restoring buffer state."
620 `(let* ,(append varlist
621 '((modified (buffer-modified-p)) (buffer-undo-list t)
622 (inhibit-read-only t) (inhibit-point-motion-hooks t)
623 before-change-functions after-change-functions
624 deactivate-mark buffer-file-name buffer-file-truename))
625 ,@body
626 (when (and (not modified) (buffer-modified-p))
627 (set-buffer-modified-p nil))))
628 (put 'save-buffer-state 'lisp-indent-function 1)
629 (def-edebug-spec save-buffer-state let)
631 ;; Shut up the byte compiler.
632 (defvar global-font-lock-mode) ; Now a defcustom.
633 (defvar font-lock-face-attributes) ; Obsolete but respected if set.
634 (defvar font-lock-string-face) ; Used in syntactic fontification.
635 (defvar font-lock-comment-face))
637 ;;;###autoload
638 (defun font-lock-mode (&optional arg)
639 "Toggle Font Lock mode.
640 With arg, turn Font Lock mode on if and only if arg is positive.
641 (Font Lock is also known as \"syntax highlighting\".)
643 When Font Lock mode is enabled, text is fontified as you type it:
645 - Comments are displayed in `font-lock-comment-face';
646 - Strings are displayed in `font-lock-string-face';
647 - Certain other expressions are displayed in other faces according to the
648 value of the variable `font-lock-keywords'.
650 To customize the faces (colors, fonts, etc.) used by Font Lock for
651 fontifying different parts of buffer text, use \\[customize-face].
653 You can enable Font Lock mode in any major mode automatically by turning on in
654 the major mode's hook. For example, put in your ~/.emacs:
656 (add-hook 'c-mode-hook 'turn-on-font-lock)
658 Alternatively, you can use Global Font Lock mode to automagically turn on Font
659 Lock mode in buffers whose major mode supports it and whose major mode is one
660 of `font-lock-global-modes'. For example, put in your ~/.emacs:
662 (global-font-lock-mode t)
664 There are a number of support modes that may be used to speed up Font Lock mode
665 in various ways, specified via the variable `font-lock-support-mode'. Where
666 major modes support different levels of fontification, you can use the variable
667 `font-lock-maximum-decoration' to specify which level you generally prefer.
668 When you turn Font Lock mode on/off the buffer is fontified/defontified, though
669 fontification occurs only if the buffer is less than `font-lock-maximum-size'.
671 For example, to specify that Font Lock mode use use Lazy Lock mode as a support
672 mode and use maximum levels of fontification, put in your ~/.emacs:
674 (setq font-lock-support-mode 'lazy-lock-mode)
675 (setq font-lock-maximum-decoration t)
677 To add your own highlighting for some major mode, and modify the highlighting
678 selected automatically via the variable `font-lock-maximum-decoration', you can
679 use `font-lock-add-keywords'.
681 To fontify a buffer, without turning on Font Lock mode and regardless of buffer
682 size, you can use \\[font-lock-fontify-buffer].
684 To fontify a block (the function or paragraph containing point, or a number of
685 lines around point), perhaps because modification on the current line caused
686 syntactic change on other lines, you can use \\[font-lock-fontify-block].
688 See the variable `font-lock-defaults-alist' for the Font Lock mode default
689 settings. You can set your own default settings for some mode, by setting a
690 buffer local value for `font-lock-defaults', via its mode hook."
691 (interactive "P")
692 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
693 ;; batch job) or if the buffer is invisible (the name starts with a space).
694 (let ((on-p (and (not noninteractive)
695 (not (eq (aref (buffer-name) 0) ?\ ))
696 (if arg
697 (> (prefix-numeric-value arg) 0)
698 (not font-lock-mode)))))
699 (set (make-local-variable 'font-lock-mode) on-p)
700 ;; Turn on Font Lock mode.
701 (when on-p
702 (make-local-hook 'after-change-functions)
703 (add-hook 'after-change-functions 'font-lock-after-change-function nil t)
704 (font-lock-set-defaults)
705 (font-lock-turn-on-thing-lock)
706 (run-hooks 'font-lock-mode-hook)
707 ;; Fontify the buffer if we have to.
708 (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size)))
709 (cond (font-lock-fontified
710 nil)
711 ((or (null max-size) (> max-size (buffer-size)))
712 (font-lock-fontify-buffer))
713 (font-lock-verbose
714 (message "Fontifying %s...buffer size greater than font-lock-maximum-size"
715 (buffer-name))))))
716 ;; Turn off Font Lock mode.
717 (unless on-p
718 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
719 (font-lock-unfontify-buffer)
720 (font-lock-turn-off-thing-lock)
721 (font-lock-unset-defaults))
722 (force-mode-line-update)))
724 ;;;###autoload
725 (defun turn-on-font-lock ()
726 "Turn on Font Lock mode conditionally.
727 Turn on only if the terminal can display it."
728 (unless font-lock-mode
729 (font-lock-mode)))
731 ;;;###autoload
732 (defun font-lock-add-keywords (mode keywords &optional append)
733 "Add highlighting KEYWORDS for MODE.
734 MODE should be a symbol, the major mode command name, such as `c-mode'
735 or nil. If nil, highlighting keywords are added for the current buffer.
736 KEYWORDS should be a list; see the variable `font-lock-keywords'.
737 By default they are added at the beginning of the current highlighting list.
738 If optional argument APPEND is `set', they are used to replace the current
739 highlighting list. If APPEND is any other non-nil value, they are added at the
740 end of the current highlighting list.
742 For example:
744 (font-lock-add-keywords 'c-mode
745 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
746 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . font-lock-keyword-face)))
748 adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
749 comments, and to fontify `and', `or' and `not' words as keywords.
751 Note that some modes have specialised support for additional patterns, e.g.,
752 see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
753 `objc-font-lock-extra-types' and `java-font-lock-extra-types'."
754 (cond (mode
755 ;; If MODE is non-nil, add the KEYWORDS and APPEND spec to
756 ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
757 (let ((spec (cons keywords append)) cell)
758 (if (setq cell (assq mode font-lock-keywords-alist))
759 (if (eq append 'set)
760 (setcdr cell (list spec))
761 (setcdr cell (append (cdr cell) (list spec))))
762 (push (list mode spec) font-lock-keywords-alist)))
763 ;; Make sure that `font-lock-removed-keywords-alist' does not
764 ;; contain the new keywords.
765 (font-lock-update-removed-keyword-alist mode keywords append))
767 ;; Otherwise set or add the keywords now.
768 (font-lock-set-defaults)
769 (if (eq append 'set)
770 (setq font-lock-keywords keywords)
771 (font-lock-remove-keywords nil keywords) ;to avoid duplicates
772 (let ((old (if (eq (car-safe font-lock-keywords) t)
773 (cdr font-lock-keywords)
774 font-lock-keywords)))
775 (setq font-lock-keywords (if append
776 (append old keywords)
777 (append keywords old))))))))
779 (defun font-lock-update-removed-keyword-alist (mode keywords append)
780 ;; Update `font-lock-removed-keywords-alist' when adding new
781 ;; KEYWORDS to MODE.
783 ;; When font-lock is enabled first all keywords in the list
784 ;; `font-lock-keywords-alist' are added, then all keywords in the
785 ;; list `font-lock-removed-keywords-alist' are removed. If a
786 ;; keyword was once added, removed, and then added again it must be
787 ;; removed from the removed-keywords list. Otherwise the second add
788 ;; will not take effect.
789 (let ((cell (assq mode font-lock-removed-keywords-alist)))
790 (if cell
791 (if (eq append 'set)
792 ;; A new set of keywords is defined. Forget all about
793 ;; our old keywords that should be removed.
794 (setq font-lock-removed-keywords-alist
795 (delq cell font-lock-removed-keywords-alist))
796 ;; Delete all previously removed keywords.
797 (dolist (kword keywords)
798 (setcdr cell (delete kword (cdr cell))))
799 ;; Delete the mode cell if empty.
800 (if (null (cdr cell))
801 (setq font-lock-removed-keywords-alist
802 (delq cell font-lock-removed-keywords-alist)))))))
804 ;; Written by Anders Lindgren <andersl@andersl.com>.
806 ;; Case study:
807 ;; (I) The keywords are removed from a major mode.
808 ;; In this case the keyword could be local (i.e. added earlier by
809 ;; `font-lock-add-keywords'), global, or both.
811 ;; (a) In the local case we remove the keywords from the variable
812 ;; `font-lock-keywords-alist'.
814 ;; (b) The actual global keywords are not known at this time.
815 ;; All keywords are added to `font-lock-removed-keywords-alist',
816 ;; when font-lock is enabled those keywords are removed.
818 ;; Note that added keywords are taken out of the list of removed
819 ;; keywords. This ensure correct operation when the same keyword
820 ;; is added and removed several times.
822 ;; (II) The keywords are removed from the current buffer.
823 ;;;###autoload
824 (defun font-lock-remove-keywords (mode keywords)
825 "Remove highlighting KEYWORDS for MODE.
827 MODE should be a symbol, the major mode command name, such as `c-mode'
828 or nil. If nil, highlighting keywords are removed for the current buffer."
829 (cond (mode
830 ;; Remove one keyword at the time.
831 (dolist (keyword keywords)
832 (let ((top-cell (assq mode font-lock-keywords-alist)))
833 ;; If MODE is non-nil, remove the KEYWORD from
834 ;; `font-lock-keywords-alist'.
835 (when top-cell
836 (dolist (keyword-list-append-pair (cdr top-cell))
837 ;; `keywords-list-append-pair' is a cons with a list of
838 ;; keywords in the car top-cell and the original append
839 ;; argument in the cdr top-cell.
840 (setcar keyword-list-append-pair
841 (delete keyword (car keyword-list-append-pair))))
842 ;; Remove keyword list/append pair when the keyword list
843 ;; is empty and append doesn't specify `set'. (If it
844 ;; should be deleted then previously deleted keywords
845 ;; would appear again.)
846 (let ((cell top-cell))
847 (while (cdr cell)
848 (if (and (null (car (car (cdr cell))))
849 (not (eq (cdr (car (cdr cell))) 'set)))
850 (setcdr cell (cdr (cdr cell)))
851 (setq cell (cdr cell)))))
852 ;; Final cleanup, remove major mode cell if last keyword
853 ;; was deleted.
854 (if (null (cdr top-cell))
855 (setq font-lock-keywords-alist
856 (delq top-cell font-lock-keywords-alist))))
857 ;; Remember the keyword in case it is not local.
858 (let ((cell (assq mode font-lock-removed-keywords-alist)))
859 (if cell
860 (unless (member keyword (cdr cell))
861 (nconc cell (list keyword)))
862 (push (cons mode (list keyword))
863 font-lock-removed-keywords-alist))))))
865 ;; Otherwise remove it immediately.
866 (font-lock-set-defaults)
867 (setq font-lock-keywords (copy-sequence font-lock-keywords))
868 (dolist (keyword keywords)
869 (setq font-lock-keywords
870 (delete keyword
871 ;; The keywords might be compiled.
872 (delete (font-lock-compile-keyword keyword)
873 font-lock-keywords)))))))
875 ;;; Global Font Lock mode.
877 ;; A few people have hassled in the past for a way to make it easier to turn on
878 ;; Font Lock mode, without the user needing to know for which modes s/he has to
879 ;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always
880 ;; balked at that way, as I see it as just re-moulding the same problem in
881 ;; another form. That is; some person would still have to keep track of which
882 ;; modes (which may not even be distributed with Emacs) support Font Lock mode.
883 ;; The list would always be out of date. And that person might have to be me.
885 ;; Implementation.
887 ;; In a previous discussion the following hack came to mind. It is a gross
888 ;; hack, but it generally works. We use the convention that major modes start
889 ;; by calling the function `kill-all-local-variables', which in turn runs
890 ;; functions on the hook variable `change-major-mode-hook'. We attach our
891 ;; function `font-lock-change-major-mode' to that hook. Of course, when this
892 ;; hook is run, the major mode is in the process of being changed and we do not
893 ;; know what the final major mode will be. So, `font-lock-change-major-mode'
894 ;; only (a) notes the name of the current buffer, and (b) adds our function
895 ;; `turn-on-font-lock-if-enabled' to the hook variables `find-file-hooks' and
896 ;; `post-command-hook' (for buffers that are not visiting files). By the time
897 ;; the functions on the first of these hooks to be run are run, the new major
898 ;; mode is assumed to be in place. This way we get a Font Lock function run
899 ;; when a major mode is turned on, without knowing major modes or their hooks.
901 ;; Naturally this requires that (a) major modes run `kill-all-local-variables',
902 ;; as they are supposed to do, and (b) the major mode is in place after the
903 ;; file is visited or the command that ran `kill-all-local-variables' has
904 ;; finished, whichever the sooner. Arguably, any major mode that does not
905 ;; follow the convension (a) is broken, and I can't think of any reason why (b)
906 ;; would not be met (except `gnudoit' on non-files). However, it is not clean.
908 ;; Probably the cleanest solution is to have each major mode function run some
909 ;; hook, e.g., `major-mode-hook', but maybe implementing that change is
910 ;; impractical. I am personally against making `setq' a macro or be advised,
911 ;; or have a special function such as `set-major-mode', but maybe someone can
912 ;; come up with another solution?
914 ;; User interface.
916 ;; Although Global Font Lock mode is a pseudo-mode, I think that the user
917 ;; interface should conform to the usual Emacs convention for modes, i.e., a
918 ;; command to toggle the feature (`global-font-lock-mode') with a variable for
919 ;; finer control of the mode's behaviour (`font-lock-global-modes').
921 ;; The feature should not be enabled by loading font-lock.el, since other
922 ;; mechanisms for turning on Font Lock mode, such as M-x font-lock-mode RET or
923 ;; (add-hook 'c-mode-hook 'turn-on-font-lock), would cause Font Lock mode to be
924 ;; turned on everywhere. That would not be intuitive or informative because
925 ;; loading a file tells you nothing about the feature or how to control it. It
926 ;; would also be contrary to the Principle of Least Surprise. sm.
928 (defvar font-lock-buffers nil) ; For remembering buffers.
930 ;;;###autoload
931 (defun global-font-lock-mode (&optional arg message)
932 "Toggle Global Font Lock mode.
933 (Font Lock is also known as \"syntax highlighting\".)
934 With prefix ARG, turn Global Font Lock mode on if and only if ARG is positive.
935 Displays a message saying whether the mode is on or off if MESSAGE is non-nil.
936 Returns the new status of Global Font Lock mode (non-nil means on).
938 When Global Font Lock mode is enabled, Font Lock mode is automagically
939 turned on in a buffer if its major mode is one of `font-lock-global-modes'.
941 To customize the faces (colors, fonts, etc.) used by Font Lock for
942 highlighting different parts of buffer text, use \\[customize-face]."
943 (interactive "P\np")
944 (let ((on-p (if arg
945 (> (prefix-numeric-value arg) 0)
946 (not global-font-lock-mode))))
947 (cond (on-p
948 (add-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
949 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
950 (setq font-lock-buffers (buffer-list)))
952 (remove-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
953 (mapc (function (lambda (buffer)
954 (with-current-buffer buffer
955 (when font-lock-mode
956 (font-lock-mode)))))
957 (buffer-list))))
958 (when message
959 (message "Global Font Lock mode %s." (if on-p "enabled" "disabled")))
960 (setq global-font-lock-mode on-p)))
962 ;; This variable was originally a `defvar' to keep track of
963 ;; whether Global Font Lock mode was turned on or not. As a `defcustom' with
964 ;; special `:set' and `:require' forms, we can provide custom mode control.
965 ;;;###autoload
966 (defcustom global-font-lock-mode nil
967 "Toggle Global Font Lock mode.
968 When Global Font Lock mode is enabled, Font Lock mode is automagically
969 turned on in a buffer if its major mode is one of `font-lock-global-modes'.
970 Setting this variable directly does not take effect;
971 use either \\[customize] or the function `global-font-lock-mode'."
972 :set (lambda (symbol value)
973 (global-font-lock-mode (or value 0)))
974 :initialize 'custom-initialize-default
975 :type 'boolean
976 :group 'font-lock
977 :require 'font-lock)
979 (defcustom font-lock-global-modes t
980 "*Modes for which Font Lock mode is automagically turned on.
981 Global Font Lock mode is controlled by the command `global-font-lock-mode'.
982 If nil, means no modes have Font Lock mode automatically turned on.
983 If t, all modes that support Font Lock mode have it automatically turned on.
984 If a list, it should be a list of `major-mode' symbol names for which Font Lock
985 mode should be automatically turned on. The sense of the list is negated if it
986 begins with `not'. For example:
987 (c-mode c++-mode)
988 means that Font Lock mode is turned on for buffers in C and C++ modes only."
989 :type '(choice (const :tag "none" nil)
990 (const :tag "all" t)
991 (set :menu-tag "mode specific" :tag "modes"
992 :value (not)
993 (const :tag "Except" not)
994 (repeat :inline t (symbol :tag "mode"))))
995 :group 'font-lock)
997 (defun font-lock-change-major-mode ()
998 ;; Turn off Font Lock mode if it's on.
999 (when font-lock-mode
1000 (font-lock-mode))
1001 ;; Gross hack warning: Delicate readers should avert eyes now.
1002 ;; Something is running `kill-all-local-variables', which generally means the
1003 ;; major mode is being changed. Run `turn-on-font-lock-if-enabled' after the
1004 ;; file is visited or the current command has finished.
1005 (when global-font-lock-mode
1006 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
1007 (add-to-list 'font-lock-buffers (current-buffer))))
1009 (defun turn-on-font-lock-if-enabled ()
1010 ;; Gross hack warning: Delicate readers should avert eyes now.
1011 ;; Turn on Font Lock mode if it's supported by the major mode and enabled by
1012 ;; the user.
1013 (remove-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
1014 (while font-lock-buffers
1015 (when (buffer-live-p (car font-lock-buffers))
1016 (save-excursion
1017 (set-buffer (car font-lock-buffers))
1018 (when (and (or font-lock-defaults
1019 (assq major-mode font-lock-defaults-alist))
1020 (or (eq font-lock-global-modes t)
1021 (if (eq (car-safe font-lock-global-modes) 'not)
1022 (not (memq major-mode (cdr font-lock-global-modes)))
1023 (memq major-mode font-lock-global-modes))))
1024 (let (inhibit-quit)
1025 (turn-on-font-lock)))))
1026 (setq font-lock-buffers (cdr font-lock-buffers))))
1028 (add-hook 'change-major-mode-hook 'font-lock-change-major-mode)
1030 ;;; End of Global Font Lock mode.
1032 ;;; Font Lock Support mode.
1034 ;; This is the code used to interface font-lock.el with any of its add-on
1035 ;; packages, and provide the user interface. Packages that have their own
1036 ;; local buffer fontification functions (see below) may have to call
1037 ;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
1038 ;; themselves.
1040 (defcustom font-lock-support-mode 'jit-lock-mode
1041 "*Support mode for Font Lock mode.
1042 Support modes speed up Font Lock mode by being choosy about when fontification
1043 occurs. Known support modes are Fast Lock mode (symbol `fast-lock-mode'),
1044 Lazy Lock mode (symbol `lazy-lock-mode'), and Just-in-time Lock mode (symbol
1045 `jit-lock-mode'. See those modes for more info.
1046 If nil, means support for Font Lock mode is never performed.
1047 If a symbol, use that support mode.
1048 If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
1049 where MAJOR-MODE is a symbol or t (meaning the default). For example:
1050 ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
1051 means that Fast Lock mode is used to support Font Lock mode for buffers in C or
1052 C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
1054 The value of this variable is used when Font Lock mode is turned on."
1055 :type '(choice (const :tag "none" nil)
1056 (const :tag "fast lock" fast-lock-mode)
1057 (const :tag "lazy lock" lazy-lock-mode)
1058 (const :tag "jit lock" jit-lock-mode)
1059 (repeat :menu-tag "mode specific" :tag "mode specific"
1060 :value ((t . lazy-lock-mode))
1061 (cons :tag "Instance"
1062 (radio :tag "Mode"
1063 (const :tag "all" t)
1064 (symbol :tag "name"))
1065 (radio :tag "Support"
1066 (const :tag "none" nil)
1067 (const :tag "fast lock" fast-lock-mode)
1068 (const :tag "lazy lock" lazy-lock-mode)
1069 (const :tag "JIT lock" jit-lock-mode)))
1071 :version "21.1"
1072 :group 'font-lock)
1074 (defvar fast-lock-mode nil)
1075 (defvar lazy-lock-mode nil)
1076 (defvar jit-lock-mode nil)
1078 (defun font-lock-turn-on-thing-lock ()
1079 (let ((thing-mode (font-lock-value-in-major-mode font-lock-support-mode)))
1080 (cond ((eq thing-mode 'fast-lock-mode)
1081 (fast-lock-mode t))
1082 ((eq thing-mode 'lazy-lock-mode)
1083 (lazy-lock-mode t))
1084 ((eq thing-mode 'jit-lock-mode)
1085 ;; Prepare for jit-lock
1086 (remove-hook 'after-change-functions
1087 'font-lock-after-change-function t)
1088 (set (make-local-variable 'font-lock-fontify-buffer-function)
1089 'jit-lock-refontify)
1090 ;; Don't fontify eagerly (and don't abort is the buffer is large).
1091 (set (make-local-variable 'font-lock-fontified) t)
1092 ;; Use jit-lock.
1093 (jit-lock-register 'font-lock-fontify-region
1094 (not font-lock-keywords-only))))))
1096 (defun font-lock-turn-off-thing-lock ()
1097 (cond (fast-lock-mode
1098 (fast-lock-mode nil))
1099 (jit-lock-mode
1100 (jit-lock-unregister 'font-lock-fontify-region)
1101 ;; Reset local vars to the non-jit-lock case.
1102 (kill-local-variable 'font-lock-fontify-buffer-function))
1103 (lazy-lock-mode
1104 (lazy-lock-mode nil))))
1106 (defun font-lock-after-fontify-buffer ()
1107 (cond (fast-lock-mode
1108 (fast-lock-after-fontify-buffer))
1109 ;; Useless now that jit-lock intercepts font-lock-fontify-buffer. -sm
1110 ;; (jit-lock-mode
1111 ;; (jit-lock-after-fontify-buffer))
1112 (lazy-lock-mode
1113 (lazy-lock-after-fontify-buffer))))
1115 (defun font-lock-after-unfontify-buffer ()
1116 (cond (fast-lock-mode
1117 (fast-lock-after-unfontify-buffer))
1118 ;; Useless as well. It's only called when:
1119 ;; - turning off font-lock: it does not matter if we leave spurious
1120 ;; `fontified' text props around since jit-lock-mode is also off.
1121 ;; - font-lock-default-fontify-buffer fails: this is not run
1122 ;; any more anyway. -sm
1124 ;; (jit-lock-mode
1125 ;; (jit-lock-after-unfontify-buffer))
1126 (lazy-lock-mode
1127 (lazy-lock-after-unfontify-buffer))))
1129 ;;; End of Font Lock Support mode.
1131 ;;; Fontification functions.
1133 ;; Rather than the function, e.g., `font-lock-fontify-region' containing the
1134 ;; code to fontify a region, the function runs the function whose name is the
1135 ;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally,
1136 ;; the value of this variable is, e.g., `font-lock-default-fontify-region'
1137 ;; which does contain the code to fontify a region. However, the value of the
1138 ;; variable could be anything and thus, e.g., `font-lock-fontify-region' could
1139 ;; do anything. The indirection of the fontification functions gives major
1140 ;; modes the capability of modifying the way font-lock.el fontifies. Major
1141 ;; modes can modify the values of, e.g., `font-lock-fontify-region-function',
1142 ;; via the variable `font-lock-defaults'.
1144 ;; For example, Rmail mode sets the variable `font-lock-defaults' so that
1145 ;; font-lock.el uses its own function for buffer fontification. This function
1146 ;; makes fontification be on a message-by-message basis and so visiting an
1147 ;; RMAIL file is much faster. A clever implementation of the function might
1148 ;; fontify the headers differently than the message body. (It should, and
1149 ;; correspondingly for Mail mode, but I can't be bothered to do the work. Can
1150 ;; you?) This hints at a more interesting use...
1152 ;; Languages that contain text normally contained in different major modes
1153 ;; could define their own fontification functions that treat text differently
1154 ;; depending on its context. For example, Perl mode could arrange that here
1155 ;; docs are fontified differently than Perl code. Or Yacc mode could fontify
1156 ;; rules one way and C code another. Neat!
1158 ;; A further reason to use the fontification indirection feature is when the
1159 ;; default syntactual fontification, or the default fontification in general,
1160 ;; is not flexible enough for a particular major mode. For example, perhaps
1161 ;; comments are just too hairy for `font-lock-fontify-syntactically-region' to
1162 ;; cope with. You need to write your own version of that function, e.g.,
1163 ;; `hairy-fontify-syntactically-region', and make your own version of
1164 ;; `hairy-fontify-region' call that function before calling
1165 ;; `font-lock-fontify-keywords-region' for the normal regexp fontification
1166 ;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el
1167 ;; would call your region fontification function instead of its own. For
1168 ;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line
1169 ;; directives correctly and cleanly. (It is the same problem as fontifying
1170 ;; multi-line strings and comments; regexps are not appropriate for the job.)
1172 ;;;###autoload
1173 (defun font-lock-fontify-buffer ()
1174 "Fontify the current buffer the way the function `font-lock-mode' would."
1175 (interactive)
1176 (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
1177 (funcall font-lock-fontify-buffer-function)))
1179 (defun font-lock-unfontify-buffer ()
1180 (funcall font-lock-unfontify-buffer-function))
1182 (defun font-lock-fontify-region (beg end &optional loudly)
1183 (funcall font-lock-fontify-region-function beg end loudly))
1185 (defun font-lock-unfontify-region (beg end)
1186 (funcall font-lock-unfontify-region-function beg end))
1188 (defun font-lock-default-fontify-buffer ()
1189 (let ((verbose (if (numberp font-lock-verbose)
1190 (> (buffer-size) font-lock-verbose)
1191 font-lock-verbose)))
1192 (with-temp-message
1193 (when verbose
1194 (format "Fontifying %s..." (buffer-name)))
1195 ;; Make sure we have the right `font-lock-keywords' etc.
1196 (unless font-lock-mode
1197 (font-lock-set-defaults))
1198 ;; Make sure we fontify etc. in the whole buffer.
1199 (save-restriction
1200 (widen)
1201 (condition-case nil
1202 (save-excursion
1203 (save-match-data
1204 (font-lock-fontify-region (point-min) (point-max) verbose)
1205 (font-lock-after-fontify-buffer)
1206 (setq font-lock-fontified t)))
1207 ;; We don't restore the old fontification, so it's best to unfontify.
1208 (quit (font-lock-unfontify-buffer))))
1209 ;; Make sure we undo `font-lock-keywords' etc.
1210 (unless font-lock-mode
1211 (font-lock-unset-defaults)))))
1213 (defun font-lock-default-unfontify-buffer ()
1214 ;; Make sure we unfontify etc. in the whole buffer.
1215 (save-restriction
1216 (widen)
1217 (font-lock-unfontify-region (point-min) (point-max))
1218 (font-lock-after-unfontify-buffer)
1219 (setq font-lock-fontified nil)))
1221 (defun font-lock-default-fontify-region (beg end loudly)
1222 (save-buffer-state
1223 ((parse-sexp-lookup-properties font-lock-syntactic-keywords)
1224 (old-syntax-table (syntax-table)))
1225 (unwind-protect
1226 (save-restriction
1227 (widen)
1228 ;; Use the fontification syntax table, if any.
1229 (when font-lock-syntax-table
1230 (set-syntax-table font-lock-syntax-table))
1231 ;; check to see if we should expand the beg/end area for
1232 ;; proper multiline matches
1233 (when (and (> beg (point-min))
1234 (get-text-property (1- beg) 'font-lock-multiline))
1235 ;; We are just after or in a multiline match.
1236 (setq beg (or (previous-single-property-change
1237 beg 'font-lock-multiline)
1238 (point-min))))
1239 (setq end (or (text-property-any end (point-max)
1240 'font-lock-multiline nil)
1241 (point-max)))
1242 ;; Now do the fontification.
1243 (font-lock-unfontify-region beg end)
1244 (when font-lock-syntactic-keywords
1245 (font-lock-fontify-syntactic-keywords-region beg end))
1246 (unless font-lock-keywords-only
1247 (font-lock-fontify-syntactically-region beg end loudly))
1248 (font-lock-fontify-keywords-region beg end loudly))
1249 ;; Clean up.
1250 (set-syntax-table old-syntax-table))))
1252 ;; The following must be rethought, since keywords can override fontification.
1253 ; ;; Now scan for keywords, but not if we are inside a comment now.
1254 ; (or (and (not font-lock-keywords-only)
1255 ; (let ((state (parse-partial-sexp beg end nil nil
1256 ; font-lock-cache-state)))
1257 ; (or (nth 4 state) (nth 7 state))))
1258 ; (font-lock-fontify-keywords-region beg end))
1260 (defun font-lock-default-unfontify-region (beg end)
1261 (save-buffer-state nil
1262 (remove-text-properties beg end
1263 (if font-lock-syntactic-keywords
1264 '(face nil syntax-table nil font-lock-multiline nil)
1265 '(face nil font-lock-multiline nil)))))
1267 ;; Called when any modification is made to buffer text.
1268 (defun font-lock-after-change-function (beg end old-len)
1269 (let ((inhibit-point-motion-hooks t))
1270 (save-excursion
1271 (save-match-data
1272 ;; Rescan between start of lines enclosing the region.
1273 (font-lock-fontify-region
1274 (progn (goto-char beg) (beginning-of-line) (point))
1275 (progn (goto-char end) (forward-line 1) (point)))))))
1277 (defun font-lock-fontify-block (&optional arg)
1278 "Fontify some lines the way `font-lock-fontify-buffer' would.
1279 The lines could be a function or paragraph, or a specified number of lines.
1280 If ARG is given, fontify that many lines before and after point, or 16 lines if
1281 no ARG is given and `font-lock-mark-block-function' is nil.
1282 If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
1283 delimit the region to fontify."
1284 (interactive "P")
1285 (let ((inhibit-point-motion-hooks t) font-lock-beginning-of-syntax-function
1286 deactivate-mark)
1287 ;; Make sure we have the right `font-lock-keywords' etc.
1288 (if (not font-lock-mode) (font-lock-set-defaults))
1289 (save-excursion
1290 (save-match-data
1291 (condition-case error-data
1292 (if (or arg (not font-lock-mark-block-function))
1293 (let ((lines (if arg (prefix-numeric-value arg) 16)))
1294 (font-lock-fontify-region
1295 (save-excursion (forward-line (- lines)) (point))
1296 (save-excursion (forward-line lines) (point))))
1297 (funcall font-lock-mark-block-function)
1298 (font-lock-fontify-region (point) (mark)))
1299 ((error quit) (message "Fontifying block...%s" error-data)))))))
1301 (define-key facemenu-keymap "\M-g" 'font-lock-fontify-block)
1303 ;;; End of Fontification functions.
1305 ;;; Additional text property functions.
1307 ;; The following text property functions should be builtins. This means they
1308 ;; should be written in C and put with all the other text property functions.
1309 ;; In the meantime, those that are used by font-lock.el are defined in Lisp
1310 ;; below and given a `font-lock-' prefix. Those that are not used are defined
1311 ;; in Lisp below and commented out. sm.
1313 (defun font-lock-prepend-text-property (start end prop value &optional object)
1314 "Prepend to one property of the text from START to END.
1315 Arguments PROP and VALUE specify the property and value to prepend to the value
1316 already in place. The resulting property values are always lists.
1317 Optional argument OBJECT is the string or buffer containing the text."
1318 (let ((val (if (listp value) value (list value))) next prev)
1319 (while (/= start end)
1320 (setq next (next-single-property-change start prop object end)
1321 prev (get-text-property start prop object))
1322 (put-text-property start next prop
1323 (append val (if (listp prev) prev (list prev)))
1324 object)
1325 (setq start next))))
1327 (defun font-lock-append-text-property (start end prop value &optional object)
1328 "Append to one property of the text from START to END.
1329 Arguments PROP and VALUE specify the property and value to append to the value
1330 already in place. The resulting property values are always lists.
1331 Optional argument OBJECT is the string or buffer containing the text."
1332 (let ((val (if (listp value) value (list value))) next prev)
1333 (while (/= start end)
1334 (setq next (next-single-property-change start prop object end)
1335 prev (get-text-property start prop object))
1336 (put-text-property start next prop
1337 (append (if (listp prev) prev (list prev)) val)
1338 object)
1339 (setq start next))))
1341 (defun font-lock-fillin-text-property (start end prop value &optional object)
1342 "Fill in one property of the text from START to END.
1343 Arguments PROP and VALUE specify the property and value to put where none are
1344 already in place. Therefore existing property values are not overwritten.
1345 Optional argument OBJECT is the string or buffer containing the text."
1346 (let ((start (text-property-any start end prop nil object)) next)
1347 (while start
1348 (setq next (next-single-property-change start prop object end))
1349 (put-text-property start next prop value object)
1350 (setq start (text-property-any next end prop nil object)))))
1352 ;; For completeness: this is to `remove-text-properties' as `put-text-property'
1353 ;; is to `add-text-properties', etc.
1354 ;(defun remove-text-property (start end property &optional object)
1355 ; "Remove a property from text from START to END.
1356 ;Argument PROPERTY is the property to remove.
1357 ;Optional argument OBJECT is the string or buffer containing the text.
1358 ;Return t if the property was actually removed, nil otherwise."
1359 ; (remove-text-properties start end (list property) object))
1361 ;; For consistency: maybe this should be called `remove-single-property' like
1362 ;; `next-single-property-change' (not `next-single-text-property-change'), etc.
1363 ;(defun remove-single-text-property (start end prop value &optional object)
1364 ; "Remove a specific property value from text from START to END.
1365 ;Arguments PROP and VALUE specify the property and value to remove. The
1366 ;resulting property values are not equal to VALUE nor lists containing VALUE.
1367 ;Optional argument OBJECT is the string or buffer containing the text."
1368 ; (let ((start (text-property-not-all start end prop nil object)) next prev)
1369 ; (while start
1370 ; (setq next (next-single-property-change start prop object end)
1371 ; prev (get-text-property start prop object))
1372 ; (cond ((and (symbolp prev) (eq value prev))
1373 ; (remove-text-property start next prop object))
1374 ; ((and (listp prev) (memq value prev))
1375 ; (let ((new (delq value prev)))
1376 ; (cond ((null new)
1377 ; (remove-text-property start next prop object))
1378 ; ((= (length new) 1)
1379 ; (put-text-property start next prop (car new) object))
1380 ; (t
1381 ; (put-text-property start next prop new object))))))
1382 ; (setq start (text-property-not-all next end prop nil object)))))
1384 ;;; End of Additional text property functions.
1386 ;;; Syntactic regexp fontification functions.
1388 ;; These syntactic keyword pass functions are identical to those keyword pass
1389 ;; functions below, with the following exceptions; (a) they operate on
1390 ;; `font-lock-syntactic-keywords' of course, (b) they are all `defun' as speed
1391 ;; is less of an issue, (c) eval of property value does not occur JIT as speed
1392 ;; is less of an issue, (d) OVERRIDE cannot be `prepend' or `append' as it
1393 ;; makes no sense for `syntax-table' property values, (e) they do not do it
1394 ;; LOUDLY as it is not likely to be intensive.
1396 (defun font-lock-apply-syntactic-highlight (highlight)
1397 "Apply HIGHLIGHT following a match.
1398 HIGHLIGHT should be of the form MATCH-HIGHLIGHT,
1399 see `font-lock-syntactic-keywords'."
1400 (let* ((match (nth 0 highlight))
1401 (start (match-beginning match)) (end (match-end match))
1402 (value (nth 1 highlight))
1403 (override (nth 2 highlight)))
1404 (cond ((stringp value)
1405 (setq value (string-to-syntax value)))
1406 ((not (numberp (car-safe value)))
1407 (setq value (eval value))))
1408 (cond ((not start)
1409 ;; No match but we might not signal an error.
1410 (or (nth 3 highlight)
1411 (error "No match %d in highlight %S" match highlight)))
1412 ((not override)
1413 ;; Cannot override existing fontification.
1414 (or (text-property-not-all start end 'syntax-table nil)
1415 (put-text-property start end 'syntax-table value)))
1416 ((eq override t)
1417 ;; Override existing fontification.
1418 (put-text-property start end 'syntax-table value))
1419 ((eq override 'keep)
1420 ;; Keep existing fontification.
1421 (font-lock-fillin-text-property start end 'syntax-table value)))))
1423 (defun font-lock-fontify-syntactic-anchored-keywords (keywords limit)
1424 "Fontify according to KEYWORDS until LIMIT.
1425 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1426 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1427 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1428 ;; Evaluate PRE-MATCH-FORM.
1429 (pre-match-value (eval (nth 1 keywords))))
1430 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1431 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1432 (setq limit pre-match-value)
1433 (setq limit (line-end-position)))
1434 (save-match-data
1435 ;; Find an occurrence of `matcher' before `limit'.
1436 (while (if (stringp matcher)
1437 (re-search-forward matcher limit t)
1438 (funcall matcher limit))
1439 ;; Apply each highlight to this instance of `matcher'.
1440 (setq highlights lowdarks)
1441 (while highlights
1442 (font-lock-apply-syntactic-highlight (car highlights))
1443 (setq highlights (cdr highlights)))))
1444 ;; Evaluate POST-MATCH-FORM.
1445 (eval (nth 2 keywords))))
1447 (defun font-lock-fontify-syntactic-keywords-region (start end)
1448 "Fontify according to `font-lock-syntactic-keywords' between START and END.
1449 START should be at the beginning of a line."
1450 ;; If `font-lock-syntactic-keywords' is a symbol, get the real keywords.
1451 (when (symbolp font-lock-syntactic-keywords)
1452 (setq font-lock-syntactic-keywords (font-lock-eval-keywords
1453 font-lock-syntactic-keywords)))
1454 ;; If `font-lock-syntactic-keywords' is not compiled, compile it.
1455 (unless (eq (car font-lock-syntactic-keywords) t)
1456 (setq font-lock-syntactic-keywords (font-lock-compile-keywords
1457 font-lock-syntactic-keywords)))
1458 ;; Get down to business.
1459 (let ((case-fold-search font-lock-keywords-case-fold-search)
1460 (keywords (cdr font-lock-syntactic-keywords))
1461 keyword matcher highlights)
1462 (while keywords
1463 ;; Find an occurrence of `matcher' from `start' to `end'.
1464 (setq keyword (car keywords) matcher (car keyword))
1465 (goto-char start)
1466 (while (if (stringp matcher)
1467 (re-search-forward matcher end t)
1468 (funcall matcher end))
1469 ;; Apply each highlight to this instance of `matcher', which may be
1470 ;; specific highlights or more keywords anchored to `matcher'.
1471 (setq highlights (cdr keyword))
1472 (while highlights
1473 (if (numberp (car (car highlights)))
1474 (font-lock-apply-syntactic-highlight (car highlights))
1475 (font-lock-fontify-syntactic-anchored-keywords (car highlights)
1476 end))
1477 (setq highlights (cdr highlights))))
1478 (setq keywords (cdr keywords)))))
1480 ;;; End of Syntactic regexp fontification functions.
1482 ;;; Syntactic fontification functions.
1484 ;; These record the parse state at a particular position, always the start of a
1485 ;; line. Used to make `font-lock-fontify-syntactically-region' faster.
1486 ;; Previously, `font-lock-cache-position' was just a buffer position. However,
1487 ;; under certain situations, this occasionally resulted in mis-fontification.
1488 ;; I think the "situations" were deletion with Lazy Lock mode's deferral. sm.
1489 (defvar font-lock-cache-state nil)
1490 (defvar font-lock-cache-position nil)
1492 (defun font-lock-fontify-syntactically-region (start end &optional loudly)
1493 "Put proper face on each string and comment between START and END.
1494 START should be at the beginning of a line."
1495 (let ((cache (marker-position font-lock-cache-position))
1496 state string beg)
1497 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
1498 (goto-char start)
1500 ;; Find the state at the `beginning-of-line' before `start'.
1501 (if (eq start cache)
1502 ;; Use the cache for the state of `start'.
1503 (setq state font-lock-cache-state)
1504 ;; Find the state of `start'.
1505 (if (null font-lock-beginning-of-syntax-function)
1506 ;; Use the state at the previous cache position, if any, or
1507 ;; otherwise calculate from `point-min'.
1508 (if (or (null cache) (< start cache))
1509 (setq state (parse-partial-sexp (point-min) start))
1510 (setq state (parse-partial-sexp cache start nil nil
1511 font-lock-cache-state)))
1512 ;; Call the function to move outside any syntactic block.
1513 (funcall font-lock-beginning-of-syntax-function)
1514 (setq state (parse-partial-sexp (point) start)))
1515 ;; Cache the state and position of `start'.
1516 (setq font-lock-cache-state state)
1517 (set-marker font-lock-cache-position start))
1519 ;; If the region starts inside a string or comment, show the extent of it.
1520 (when (or (nth 3 state) (nth 4 state))
1521 (setq string (nth 3 state) beg (point))
1522 (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
1523 (put-text-property beg (point) 'face
1524 (if string
1525 font-lock-string-face
1526 font-lock-comment-face)))
1528 ;; Find each interesting place between here and `end'.
1529 (while (and (< (point) end)
1530 (progn
1531 (setq state (parse-partial-sexp (point) end nil nil state
1532 'syntax-table))
1533 (or (nth 3 state) (nth 4 state))))
1534 (setq string (nth 3 state) beg (nth 8 state))
1535 (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
1536 (put-text-property beg (point) 'face
1537 (if string
1538 font-lock-string-face
1539 font-lock-comment-face)))))
1541 ;;; End of Syntactic fontification functions.
1543 ;;; Keyword regexp fontification functions.
1545 (defsubst font-lock-apply-highlight (highlight)
1546 "Apply HIGHLIGHT following a match.
1547 HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
1548 (let* ((match (nth 0 highlight))
1549 (start (match-beginning match)) (end (match-end match))
1550 (override (nth 2 highlight)))
1551 (cond ((not start)
1552 ;; No match but we might not signal an error.
1553 (or (nth 3 highlight)
1554 (error "No match %d in highlight %S" match highlight)))
1555 ((not override)
1556 ;; Cannot override existing fontification.
1557 (or (text-property-not-all start end 'face nil)
1558 (put-text-property start end 'face (eval (nth 1 highlight)))))
1559 ((eq override t)
1560 ;; Override existing fontification.
1561 (put-text-property start end 'face (eval (nth 1 highlight))))
1562 ((eq override 'prepend)
1563 ;; Prepend to existing fontification.
1564 (font-lock-prepend-text-property start end 'face (eval (nth 1 highlight))))
1565 ((eq override 'append)
1566 ;; Append to existing fontification.
1567 (font-lock-append-text-property start end 'face (eval (nth 1 highlight))))
1568 ((eq override 'keep)
1569 ;; Keep existing fontification.
1570 (font-lock-fillin-text-property start end 'face (eval (nth 1 highlight)))))))
1572 (defsubst font-lock-fontify-anchored-keywords (keywords limit)
1573 "Fontify according to KEYWORDS until LIMIT.
1574 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1575 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1576 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1577 (lead-start (match-beginning 0))
1578 ;; Evaluate PRE-MATCH-FORM.
1579 (pre-match-value (eval (nth 1 keywords))))
1580 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1581 (if (not (and (numberp pre-match-value) (> pre-match-value (point))))
1582 (setq limit (line-end-position))
1583 (setq limit pre-match-value)
1584 (when (and font-lock-multiline
1585 (funcall (if (eq font-lock-multiline t) '>= '>)
1586 pre-match-value
1587 (line-beginning-position 2)))
1588 ;; this is a multiline anchored match
1589 (setq font-lock-multiline t)
1590 (put-text-property (min lead-start (point)) limit
1591 'font-lock-multiline t)))
1592 (save-match-data
1593 ;; Find an occurrence of `matcher' before `limit'.
1594 (while (and (< (point) limit)
1595 (if (stringp matcher)
1596 (re-search-forward matcher limit t)
1597 (funcall matcher limit)))
1598 ;; Apply each highlight to this instance of `matcher'.
1599 (setq highlights lowdarks)
1600 (while highlights
1601 (font-lock-apply-highlight (car highlights))
1602 (setq highlights (cdr highlights)))))
1603 ;; Evaluate POST-MATCH-FORM.
1604 (eval (nth 2 keywords))))
1606 (defun font-lock-fontify-keywords-region (start end &optional loudly)
1607 "Fontify according to `font-lock-keywords' between START and END.
1608 START should be at the beginning of a line."
1609 (unless (eq (car font-lock-keywords) t)
1610 (setq font-lock-keywords (font-lock-compile-keywords font-lock-keywords)))
1611 (let ((case-fold-search font-lock-keywords-case-fold-search)
1612 (keywords (cdr font-lock-keywords))
1613 (bufname (buffer-name)) (count 0)
1614 keyword matcher highlights)
1616 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1617 (while keywords
1618 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
1619 (make-string (incf count) ?.)))
1621 ;; Find an occurrence of `matcher' from `start' to `end'.
1622 (setq keyword (car keywords) matcher (car keyword))
1623 (goto-char start)
1624 (while (and (< (point) end)
1625 (if (stringp matcher)
1626 (re-search-forward matcher end t)
1627 (funcall matcher end)))
1628 (when (and font-lock-multiline
1629 (match-beginning 0)
1630 (funcall (if (eq font-lock-multiline t) '>= '>)
1631 (point)
1632 (save-excursion (goto-char (match-beginning 0))
1633 (forward-line 1) (point))))
1634 ;; this is a multiline regexp match
1635 (setq font-lock-multiline t)
1636 (put-text-property (match-beginning 0) (point)
1637 'font-lock-multiline t))
1638 ;; Apply each highlight to this instance of `matcher', which may be
1639 ;; specific highlights or more keywords anchored to `matcher'.
1640 (setq highlights (cdr keyword))
1641 (while highlights
1642 (if (numberp (car (car highlights)))
1643 (font-lock-apply-highlight (car highlights))
1644 (font-lock-fontify-anchored-keywords (car highlights) end))
1645 (setq highlights (cdr highlights))))
1646 (setq keywords (cdr keywords)))))
1648 ;;; End of Keyword regexp fontification functions.
1650 ;; Various functions.
1652 (defun font-lock-compile-keywords (keywords)
1653 "Compile KEYWORDS into the form (t KEYWORD ...).
1654 Here KEYWORD is of the form (MATCHER HIGHLIGHT ...) as shown in the
1655 `font-lock-keywords' doc string."
1656 (if (eq (car-safe keywords) t)
1657 keywords
1658 (cons t (mapcar 'font-lock-compile-keyword keywords))))
1660 (defun font-lock-compile-keyword (keyword)
1661 (cond ((nlistp keyword) ; MATCHER
1662 (list keyword '(0 font-lock-keyword-face)))
1663 ((eq (car keyword) 'eval) ; (eval . FORM)
1664 (font-lock-compile-keyword (eval (cdr keyword))))
1665 ((eq (car-safe (cdr keyword)) 'quote) ; (MATCHER . 'FORM)
1666 ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote.
1667 (if (symbolp (nth 2 keyword))
1668 (list (car keyword) (list 0 (cdr keyword)))
1669 (font-lock-compile-keyword (cons (car keyword) (nth 2 keyword)))))
1670 ((numberp (cdr keyword)) ; (MATCHER . MATCH)
1671 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
1672 ((symbolp (cdr keyword)) ; (MATCHER . FACENAME)
1673 (list (car keyword) (list 0 (cdr keyword))))
1674 ((nlistp (nth 1 keyword)) ; (MATCHER . HIGHLIGHT)
1675 (list (car keyword) (cdr keyword)))
1676 (t ; (MATCHER HIGHLIGHT ...)
1677 keyword)))
1679 (defun font-lock-eval-keywords (keywords)
1680 "Evalulate KEYWORDS if a function (funcall) or variable (eval) name."
1681 (if (listp keywords)
1682 keywords
1683 (font-lock-eval-keywords (if (fboundp keywords)
1684 (funcall keywords)
1685 (eval keywords)))))
1687 (defun font-lock-value-in-major-mode (alist)
1688 "Return value in ALIST for `major-mode', or ALIST if it is not an alist.
1689 Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t."
1690 (if (consp alist)
1691 (cdr (or (assq major-mode alist) (assq t alist)))
1692 alist))
1694 (defun font-lock-choose-keywords (keywords level)
1695 "Return LEVELth element of KEYWORDS.
1696 A LEVEL of nil is equal to a LEVEL of 0, a LEVEL of t is equal to
1697 \(1- (length KEYWORDS))."
1698 (cond ((not (and (listp keywords) (symbolp (car keywords))))
1699 keywords)
1700 ((numberp level)
1701 (or (nth level keywords) (car (reverse keywords))))
1702 ((eq level t)
1703 (car (reverse keywords)))
1705 (car keywords))))
1707 (defvar font-lock-set-defaults nil) ; Whether we have set up defaults.
1709 (defun font-lock-set-defaults ()
1710 "Set fontification defaults appropriately for this mode.
1711 Sets various variables using `font-lock-defaults' (or, if nil, using
1712 `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
1713 ;; Set fontification defaults iff not previously set.
1714 (unless font-lock-set-defaults
1715 (set (make-local-variable 'font-lock-set-defaults) t)
1716 (set (make-local-variable 'font-lock-cache-state) nil)
1717 (set (make-local-variable 'font-lock-cache-position) (make-marker))
1718 (make-local-variable 'font-lock-fontified)
1719 (make-local-variable 'font-lock-multiline)
1720 (let* ((defaults (or font-lock-defaults
1721 (cdr (assq major-mode font-lock-defaults-alist))))
1722 (keywords
1723 (font-lock-choose-keywords (nth 0 defaults)
1724 (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1725 (local (cdr (assq major-mode font-lock-keywords-alist)))
1726 (removed-keywords
1727 (cdr-safe (assq major-mode font-lock-removed-keywords-alist))))
1728 ;; Regexp fontification?
1729 (set (make-local-variable 'font-lock-keywords)
1730 (font-lock-compile-keywords (font-lock-eval-keywords keywords)))
1731 ;; Local fontification?
1732 (while local
1733 (font-lock-add-keywords nil (car (car local)) (cdr (car local)))
1734 (setq local (cdr local)))
1735 (when removed-keywords
1736 (font-lock-remove-keywords nil removed-keywords))
1737 ;; Syntactic fontification?
1738 (when (nth 1 defaults)
1739 (set (make-local-variable 'font-lock-keywords-only) t))
1740 ;; Case fold during regexp fontification?
1741 (when (nth 2 defaults)
1742 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
1743 ;; Syntax table for regexp and syntactic fontification?
1744 (when (nth 3 defaults)
1745 (let ((slist (nth 3 defaults)))
1746 (set (make-local-variable 'font-lock-syntax-table)
1747 (copy-syntax-table (syntax-table)))
1748 (while slist
1749 ;; The character to modify may be a single CHAR or a STRING.
1750 (let ((chars (if (numberp (car (car slist)))
1751 (list (car (car slist)))
1752 (mapcar 'identity (car (car slist)))))
1753 (syntax (cdr (car slist))))
1754 (while chars
1755 (modify-syntax-entry (car chars) syntax font-lock-syntax-table)
1756 (setq chars (cdr chars)))
1757 (setq slist (cdr slist))))))
1758 ;; Syntax function for syntactic fontification?
1759 (when (nth 4 defaults)
1760 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1761 (nth 4 defaults)))
1762 ;; Variable alist?
1763 (let ((alist (nthcdr 5 defaults)))
1764 (while alist
1765 (let ((variable (car (car alist))) (value (cdr (car alist))))
1766 (unless (boundp variable)
1767 (set variable nil))
1768 (set (make-local-variable variable) value)
1769 (setq alist (cdr alist))))))))
1771 (defun font-lock-unset-defaults ()
1772 "Unset fontification defaults. See function `font-lock-set-defaults'."
1773 (setq font-lock-set-defaults nil
1774 font-lock-keywords nil
1775 font-lock-keywords-only nil
1776 font-lock-keywords-case-fold-search nil
1777 font-lock-syntax-table nil
1778 font-lock-beginning-of-syntax-function nil)
1779 (let* ((defaults (or font-lock-defaults
1780 (cdr (assq major-mode font-lock-defaults-alist))))
1781 (alist (nthcdr 5 defaults)))
1782 (while alist
1783 (set (car (car alist)) (default-value (car (car alist))))
1784 (setq alist (cdr alist)))))
1786 ;;; Colour etc. support.
1788 ;; Originally these variable values were face names such as `bold' etc.
1789 ;; Now we create our own faces, but we keep these variables for compatibility
1790 ;; and they give users another mechanism for changing face appearance.
1791 ;; We now allow a FACENAME in `font-lock-keywords' to be any expression that
1792 ;; returns a face. So the easiest thing is to continue using these variables,
1793 ;; rather than sometimes evaling FACENAME and sometimes not. sm.
1794 (defvar font-lock-comment-face 'font-lock-comment-face
1795 "Face name to use for comments.")
1797 (defvar font-lock-string-face 'font-lock-string-face
1798 "Face name to use for strings.")
1800 (defvar font-lock-keyword-face 'font-lock-keyword-face
1801 "Face name to use for keywords.")
1803 (defvar font-lock-builtin-face 'font-lock-builtin-face
1804 "Face name to use for builtins.")
1806 (defvar font-lock-function-name-face 'font-lock-function-name-face
1807 "Face name to use for function names.")
1809 (defvar font-lock-variable-name-face 'font-lock-variable-name-face
1810 "Face name to use for variable names.")
1812 (defvar font-lock-type-face 'font-lock-type-face
1813 "Face name to use for type and class names.")
1815 (defvar font-lock-constant-face 'font-lock-constant-face
1816 "Face name to use for constant and label names.")
1818 (defvar font-lock-warning-face 'font-lock-warning-face
1819 "Face name to use for things that should stand out.")
1821 (defvar font-lock-reference-face 'font-lock-constant-face
1822 "This variable is obsolete. Use `font-lock-constant-face'.")
1824 ;; Originally face attributes were specified via `font-lock-face-attributes'.
1825 ;; Users then changed the default face attributes by setting that variable.
1826 ;; However, we try and be back-compatible and respect its value if set except
1827 ;; for faces where M-x customize has been used to save changes for the face.
1828 (when (boundp 'font-lock-face-attributes)
1829 (let ((face-attributes font-lock-face-attributes))
1830 (while face-attributes
1831 (let* ((face-attribute (pop face-attributes))
1832 (face (car face-attribute)))
1833 ;; Rustle up a `defface' SPEC from a `font-lock-face-attributes' entry.
1834 (unless (get face 'saved-face)
1835 (let ((foreground (nth 1 face-attribute))
1836 (background (nth 2 face-attribute))
1837 (bold-p (nth 3 face-attribute))
1838 (italic-p (nth 4 face-attribute))
1839 (underline-p (nth 5 face-attribute))
1840 face-spec)
1841 (when foreground
1842 (setq face-spec (cons ':foreground (cons foreground face-spec))))
1843 (when background
1844 (setq face-spec (cons ':background (cons background face-spec))))
1845 (when bold-p
1846 (setq face-spec (append '(:bold t) face-spec)))
1847 (when italic-p
1848 (setq face-spec (append '(:italic t) face-spec)))
1849 (when underline-p
1850 (setq face-spec (append '(:underline t) face-spec)))
1851 (custom-declare-face face (list (list t face-spec)) nil)))))))
1853 ;; But now we do it the custom way. Note that `defface' will not overwrite any
1854 ;; faces declared above via `custom-declare-face'.
1855 (defface font-lock-comment-face
1856 '((((type tty) (class color)) (:foreground "red"))
1857 (((class grayscale) (background light))
1858 (:foreground "DimGray" :bold t :italic t))
1859 (((class grayscale) (background dark))
1860 (:foreground "LightGray" :bold t :italic t))
1861 (((class color) (background light)) (:foreground "Firebrick"))
1862 (((class color) (background dark)) (:foreground "OrangeRed"))
1863 (t (:bold t :italic t)))
1864 "Font Lock mode face used to highlight comments."
1865 :group 'font-lock-highlighting-faces)
1867 (defface font-lock-string-face
1868 '((((type tty) (class color)) (:foreground "green"))
1869 (((class grayscale) (background light)) (:foreground "DimGray" :italic t))
1870 (((class grayscale) (background dark)) (:foreground "LightGray" :italic t))
1871 (((class color) (background light)) (:foreground "RosyBrown"))
1872 (((class color) (background dark)) (:foreground "LightSalmon"))
1873 (t (:italic t)))
1874 "Font Lock mode face used to highlight strings."
1875 :group 'font-lock-highlighting-faces)
1877 (defface font-lock-keyword-face
1878 '((((type tty) (class color)) (:foreground "cyan" :weight bold))
1879 (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
1880 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1881 (((class color) (background light)) (:foreground "Purple"))
1882 (((class color) (background dark)) (:foreground "Cyan"))
1883 (t (:bold t)))
1884 "Font Lock mode face used to highlight keywords."
1885 :group 'font-lock-highlighting-faces)
1887 (defface font-lock-builtin-face
1888 '((((type tty) (class color)) (:foreground "blue" :weight light))
1889 (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
1890 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1891 (((class color) (background light)) (:foreground "Orchid"))
1892 (((class color) (background dark)) (:foreground "LightSteelBlue"))
1893 (t (:bold t)))
1894 "Font Lock mode face used to highlight builtins."
1895 :group 'font-lock-highlighting-faces)
1897 (defface font-lock-function-name-face
1898 '((((type tty) (class color)) (:foreground "blue" :weight bold))
1899 (((class color) (background light)) (:foreground "Blue"))
1900 (((class color) (background dark)) (:foreground "LightSkyBlue"))
1901 (t (:inverse-video t :bold t)))
1902 "Font Lock mode face used to highlight function names."
1903 :group 'font-lock-highlighting-faces)
1905 (defface font-lock-variable-name-face
1906 '((((type tty) (class color)) (:foreground "yellow" :weight light))
1907 (((class grayscale) (background light))
1908 (:foreground "Gray90" :bold t :italic t))
1909 (((class grayscale) (background dark))
1910 (:foreground "DimGray" :bold t :italic t))
1911 (((class color) (background light)) (:foreground "DarkGoldenrod"))
1912 (((class color) (background dark)) (:foreground "LightGoldenrod"))
1913 (t (:bold t :italic t)))
1914 "Font Lock mode face used to highlight variable names."
1915 :group 'font-lock-highlighting-faces)
1917 (defface font-lock-type-face
1918 '((((type tty) (class color)) (:foreground "green"))
1919 (((class grayscale) (background light)) (:foreground "Gray90" :bold t))
1920 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1921 (((class color) (background light)) (:foreground "ForestGreen"))
1922 (((class color) (background dark)) (:foreground "PaleGreen"))
1923 (t (:bold t :underline t)))
1924 "Font Lock mode face used to highlight type and classes."
1925 :group 'font-lock-highlighting-faces)
1927 (defface font-lock-constant-face
1928 '((((type tty) (class color)) (:foreground "magenta"))
1929 (((class grayscale) (background light))
1930 (:foreground "LightGray" :bold t :underline t))
1931 (((class grayscale) (background dark))
1932 (:foreground "Gray50" :bold t :underline t))
1933 (((class color) (background light)) (:foreground "CadetBlue"))
1934 (((class color) (background dark)) (:foreground "Aquamarine"))
1935 (t (:bold t :underline t)))
1936 "Font Lock mode face used to highlight constants and labels."
1937 :group 'font-lock-highlighting-faces)
1939 (defface font-lock-warning-face
1940 '((((type tty) (class color)) (:foreground "red"))
1941 (((class color) (background light)) (:foreground "Red" :bold t))
1942 (((class color) (background dark)) (:foreground "Pink" :bold t))
1943 (t (:inverse-video t :bold t)))
1944 "Font Lock mode face used to highlight warnings."
1945 :group 'font-lock-highlighting-faces)
1947 ;;; End of Colour etc. support.
1949 ;;; Menu support.
1951 ;; This section of code is commented out because Emacs does not have real menu
1952 ;; buttons. (We can mimic them by putting "( ) " or "(X) " at the beginning of
1953 ;; the menu entry text, but with Xt it looks both ugly and embarrassingly
1954 ;; amateur.) If/When Emacs gets real menus buttons, put in menu-bar.el after
1955 ;; the entry for "Text Properties" something like:
1957 ;; (define-key menu-bar-edit-menu [font-lock]
1958 ;; (cons "Syntax Highlighting" font-lock-menu))
1960 ;; and remove a single ";" from the beginning of each line in the rest of this
1961 ;; section. Probably the mechanism for telling the menu code what are menu
1962 ;; buttons and when they are on or off needs tweaking. I have assumed that the
1963 ;; mechanism is via `menu-toggle' and `menu-selected' symbol properties. sm.
1965 ;;;;###autoload
1966 ;(progn
1967 ; ;; Make the Font Lock menu.
1968 ; (defvar font-lock-menu (make-sparse-keymap "Syntax Highlighting"))
1969 ; ;; Add the menu items in reverse order.
1970 ; (define-key font-lock-menu [fontify-less]
1971 ; '("Less In Current Buffer" . font-lock-fontify-less))
1972 ; (define-key font-lock-menu [fontify-more]
1973 ; '("More In Current Buffer" . font-lock-fontify-more))
1974 ; (define-key font-lock-menu [font-lock-sep]
1975 ; '("--"))
1976 ; (define-key font-lock-menu [font-lock-mode]
1977 ; '("In Current Buffer" . font-lock-mode))
1978 ; (define-key font-lock-menu [global-font-lock-mode]
1979 ; '("In All Buffers" . global-font-lock-mode)))
1981 ;;;;###autoload
1982 ;(progn
1983 ; ;; We put the appropriate `menu-enable' etc. symbol property values on when
1984 ; ;; font-lock.el is loaded, so we don't need to autoload the three variables.
1985 ; (put 'global-font-lock-mode 'menu-toggle t)
1986 ; (put 'font-lock-mode 'menu-toggle t)
1987 ; (put 'font-lock-fontify-more 'menu-enable '(identity))
1988 ; (put 'font-lock-fontify-less 'menu-enable '(identity)))
1990 ;;; Put the appropriate symbol property values on now. See above.
1991 ;(put 'global-font-lock-mode 'menu-selected 'global-font-lock-mode)
1992 ;(put 'font-lock-mode 'menu-selected 'font-lock-mode)
1993 ;(put 'font-lock-fontify-more 'menu-enable '(nth 2 font-lock-fontify-level))
1994 ;(put 'font-lock-fontify-less 'menu-enable '(nth 1 font-lock-fontify-level))
1996 ;(defvar font-lock-fontify-level nil) ; For less/more fontification.
1998 ;(defun font-lock-fontify-level (level)
1999 ; (let ((font-lock-maximum-decoration level))
2000 ; (when font-lock-mode
2001 ; (font-lock-mode))
2002 ; (font-lock-mode)
2003 ; (when font-lock-verbose
2004 ; (message "Fontifying %s... level %d" (buffer-name) level))))
2006 ;(defun font-lock-fontify-less ()
2007 ; "Fontify the current buffer with less decoration.
2008 ;See `font-lock-maximum-decoration'."
2009 ; (interactive)
2010 ; ;; Check in case we get called interactively.
2011 ; (if (nth 1 font-lock-fontify-level)
2012 ; (font-lock-fontify-level (1- (car font-lock-fontify-level)))
2013 ; (error "No less decoration")))
2015 ;(defun font-lock-fontify-more ()
2016 ; "Fontify the current buffer with more decoration.
2017 ;See `font-lock-maximum-decoration'."
2018 ; (interactive)
2019 ; ;; Check in case we get called interactively.
2020 ; (if (nth 2 font-lock-fontify-level)
2021 ; (font-lock-fontify-level (1+ (car font-lock-fontify-level)))
2022 ; (error "No more decoration")))
2024 ;;; This should be called by `font-lock-set-defaults'.
2025 ;(defun font-lock-set-menu ()
2026 ; ;; Activate less/more fontification entries if there are multiple levels for
2027 ; ;; the current buffer. Sets `font-lock-fontify-level' to be of the form
2028 ; ;; (CURRENT-LEVEL IS-LOWER-LEVEL-P IS-HIGHER-LEVEL-P) for menu activation.
2029 ; (let ((keywords (or (nth 0 font-lock-defaults)
2030 ; (nth 1 (assq major-mode font-lock-defaults-alist))))
2031 ; (level (font-lock-value-in-major-mode font-lock-maximum-decoration)))
2032 ; (make-local-variable 'font-lock-fontify-level)
2033 ; (if (or (symbolp keywords) (= (length keywords) 1))
2034 ; (font-lock-unset-menu)
2035 ; (cond ((eq level t)
2036 ; (setq level (1- (length keywords))))
2037 ; ((or (null level) (zerop level))
2038 ; ;; The default level is usually, but not necessarily, level 1.
2039 ; (setq level (- (length keywords)
2040 ; (length (member (eval (car keywords))
2041 ; (mapcar 'eval (cdr keywords))))))))
2042 ; (setq font-lock-fontify-level (list level (> level 1)
2043 ; (< level (1- (length keywords))))))))
2045 ;;; This should be called by `font-lock-unset-defaults'.
2046 ;(defun font-lock-unset-menu ()
2047 ; ;; Deactivate less/more fontification entries.
2048 ; (setq font-lock-fontify-level nil))
2050 ;;; End of Menu support.
2052 ;;; Various regexp information shared by several modes.
2053 ;;; Information specific to a single mode should go in its load library.
2055 ;; Font Lock support for C, C++, Objective-C and Java modes will one day be in
2056 ;; some cc-font.el (and required by cc-mode.el). However, the below function
2057 ;; should stay in font-lock.el, since it is used by other libraries. sm.
2059 (defun font-lock-match-c-style-declaration-item-and-skip-to-next (limit)
2060 "Match, and move over, any declaration/definition item after point.
2061 Matches after point, but ignores leading whitespace and `*' characters.
2062 Does not move further than LIMIT.
2064 The expected syntax of a declaration/definition item is `word' (preceded by
2065 optional whitespace and `*' characters and proceeded by optional whitespace)
2066 optionally followed by a `('. Everything following the item (but belonging to
2067 it) is expected to by skip-able by `scan-sexps', and items are expected to be
2068 separated with a `,' and to be terminated with a `;'.
2070 Thus the regexp matches after point: word (
2071 ^^^^ ^
2072 Where the match subexpressions are: 1 2
2074 The item is delimited by (match-beginning 1) and (match-end 1).
2075 If (match-beginning 2) is non-nil, the item is followed by a `('.
2077 This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
2078 (when (looking-at "[ \t*]*\\(\\sw+\\)[ \t]*\\((\\)?")
2079 (save-match-data
2080 (condition-case nil
2081 (save-restriction
2082 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
2083 (narrow-to-region (point-min) limit)
2084 (goto-char (match-end 1))
2085 ;; Move over any item value, etc., to the next item.
2086 (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)"))
2087 (goto-char (or (scan-sexps (point) 1) (point-max))))
2088 (goto-char (match-end 2)))
2089 (error t)))))
2091 ;; Lisp.
2093 (defconst lisp-font-lock-keywords-1
2094 (eval-when-compile
2095 (list
2097 ;; Definitions.
2098 (list (concat "(\\(def\\("
2099 ;; Function declarations.
2100 "\\(advice\\|alias\\|generic\\|macro\\*?\\|method\\|"
2101 "setf\\|subst\\*?\\|un\\*?\\|"
2102 "ine-\\(condition\\|\\(?:derived\\|minor\\)-mode\\|"
2103 "method-combination\\|setf-expander\\|skeleton\\|widget\\|"
2104 "function\\|\\(compiler\\|modify\\|symbol\\)-macro\\)\\)\\|"
2105 ;; Variable declarations.
2106 "\\(const\\(ant\\)?\\|custom\\|face\\|parameter\\|var\\)\\|"
2107 ;; Structure declarations.
2108 "\\(class\\|group\\|package\\|struct\\|type\\)"
2109 "\\)\\)\\>"
2110 ;; Any whitespace and defined object.
2111 "[ \t'\(]*"
2112 "\\(setf[ \t]+\\sw+)\\|\\sw+\\)?")
2113 '(1 font-lock-keyword-face)
2114 '(9 (cond ((match-beginning 3) font-lock-function-name-face)
2115 ((match-beginning 6) font-lock-variable-name-face)
2116 (t font-lock-type-face))
2117 nil t))
2119 ;; Emacs Lisp autoload cookies.
2120 '("^;;;###\\(autoload\\)" 1 font-lock-warning-face prepend)
2122 "Subdued level highlighting for Lisp modes.")
2124 (defconst lisp-font-lock-keywords-2
2125 (append lisp-font-lock-keywords-1
2126 (eval-when-compile
2127 (list
2129 ;; Control structures. Emacs Lisp forms.
2130 (cons (concat
2131 "(" (regexp-opt
2132 '("cond" "if" "while" "let" "let*"
2133 "prog" "progn" "progv" "prog1" "prog2" "prog*"
2134 "inline" "lambda" "save-restriction" "save-excursion"
2135 "save-window-excursion" "save-selected-window"
2136 "save-match-data" "save-current-buffer" "unwind-protect"
2137 "condition-case" "track-mouse"
2138 "eval-after-load" "eval-and-compile" "eval-when-compile"
2139 "eval-when"
2140 "with-current-buffer" "with-electric-help"
2141 "with-output-to-string" "with-output-to-temp-buffer"
2142 "with-temp-buffer" "with-temp-file" "with-temp-message"
2143 "with-timeout") t)
2144 "\\>")
2147 ;; Control structures. Common Lisp forms.
2148 (cons (concat
2149 "(" (regexp-opt
2150 '("when" "unless" "case" "ecase" "typecase" "etypecase"
2151 "ccase" "ctypecase" "handler-case" "handler-bind"
2152 "restart-bind" "restart-case" "in-package"
2153 "cerror" "break" "ignore-errors"
2154 "loop" "do" "do*" "dotimes" "dolist" "the" "locally"
2155 "proclaim" "declaim" "declare" "symbol-macrolet"
2156 "lexical-let" "lexical-let*" "flet" "labels" "compiler-let"
2157 "destructuring-bind" "macrolet" "tagbody" "block"
2158 "return" "return-from") t)
2159 "\\>")
2162 ;; Exit/Feature symbols as constants.
2163 (list (concat "(\\(catch\\|throw\\|featurep\\|provide\\|require\\)\\>"
2164 "[ \t']*\\(\\sw+\\)?")
2165 '(1 font-lock-keyword-face)
2166 '(2 font-lock-constant-face nil t))
2168 ;; Erroneous structures.
2169 '("(\\(abort\\|assert\\|error\\|signal\\)\\>" 1 font-lock-warning-face)
2171 ;; Words inside \\[] tend to be for `substitute-command-keys'.
2172 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-constant-face prepend)
2174 ;; Words inside `' tend to be symbol names.
2175 '("`\\(\\sw\\sw+\\)'" 1 font-lock-constant-face prepend)
2177 ;; Constant values.
2178 '("\\<:\\sw\\sw+\\>" 0 font-lock-builtin-face)
2180 ;; ELisp and CLisp `&' keywords as types.
2181 '("\\&\\sw+\\>" . font-lock-type-face)
2183 ;; CL `with-' and `do-' constructs
2184 '("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
2186 "Gaudy level highlighting for Lisp modes.")
2188 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
2189 "Default expressions to highlight in Lisp modes.")
2191 ;;; User choices.
2193 ;; These provide a means to fontify types not defined by the language. Those
2194 ;; types might be the user's own or they might be generally accepted and used.
2195 ;; Generally accepted types are used to provide default variable values.
2197 (define-widget 'font-lock-extra-types-widget 'radio
2198 "Widget `:type' for members of the custom group `font-lock-extra-types'.
2199 Members should `:load' the package `font-lock' to use this widget."
2200 :args '((const :tag "none" nil)
2201 (repeat :tag "types" regexp)))
2203 (defcustom c-font-lock-extra-types '("FILE" "\\sw+_t")
2204 "*List of extra types to fontify in C mode.
2205 Each list item should be a regexp not containing word-delimiters.
2206 For example, a value of (\"FILE\" \"\\\\sw+_t\") means the word FILE and words
2207 ending in _t are treated as type names.
2209 The value of this variable is used when Font Lock mode is turned on."
2210 :type 'font-lock-extra-types-widget
2211 :group 'font-lock-extra-types)
2213 (defcustom c++-font-lock-extra-types
2214 '("\\sw+_t"
2215 "\\([iof]\\|str\\)+stream\\(buf\\)?" "ios"
2216 "string" "rope"
2217 "list" "slist"
2218 "deque" "vector" "bit_vector"
2219 "set" "multiset"
2220 "map" "multimap"
2221 "hash\\(_\\(m\\(ap\\|ulti\\(map\\|set\\)\\)\\|set\\)\\)?"
2222 "stack" "queue" "priority_queue"
2223 "type_info"
2224 "iterator" "const_iterator" "reverse_iterator" "const_reverse_iterator"
2225 "reference" "const_reference")
2226 "*List of extra types to fontify in C++ mode.
2227 Each list item should be a regexp not containing word-delimiters.
2228 For example, a value of (\"string\") means the word string is treated as a type
2229 name.
2231 The value of this variable is used when Font Lock mode is turned on."
2232 :type 'font-lock-extra-types-widget
2233 :group 'font-lock-extra-types)
2235 (defcustom objc-font-lock-extra-types '("Class" "BOOL" "IMP" "SEL")
2236 "*List of extra types to fontify in Objective-C mode.
2237 Each list item should be a regexp not containing word-delimiters.
2238 For example, a value of (\"Class\" \"BOOL\" \"IMP\" \"SEL\") means the words
2239 Class, BOOL, IMP and SEL are treated as type names.
2241 The value of this variable is used when Font Lock mode is turned on."
2242 :type 'font-lock-extra-types-widget
2243 :group 'font-lock-extra-types)
2245 (defcustom java-font-lock-extra-types
2246 '("[A-Z\300-\326\330-\337]\\sw*[a-z]\\sw*")
2247 "*List of extra types to fontify in Java mode.
2248 Each list item should be a regexp not containing word-delimiters.
2249 For example, a value of (\"[A-Z\300-\326\330-\337]\\\\sw*[a-z]\\\\sw*\") means capitalised
2250 words (and words conforming to the Java id spec) are treated as type names.
2252 The value of this variable is used when Font Lock mode is turned on."
2253 :type 'font-lock-extra-types-widget
2254 :group 'font-lock-extra-types)
2256 ;;; C.
2258 ;; [Murmur murmur murmur] Maestro, drum-roll please... [Murmur murmur murmur.]
2259 ;; Ahem. [Murmur murmur murmur] Lay-dees an Gennel-men. [Murmur murmur shhh!]
2260 ;; I am most proud and humbly honoured today [murmur murmur cough] to present
2261 ;; to you good people, the winner of the Second Millennium Award for The Most
2262 ;; Hairy Language Syntax. [Ahhh!] All rise please. [Shuffle shuffle
2263 ;; shuffle.] And a round of applause please. For... The C Language! [Roar.]
2265 ;; Thank you... You are too kind... It is with a feeling of great privilege
2266 ;; and indeed emotion [sob] that I accept this award. It has been a long hard
2267 ;; road. But we know our destiny. And our future. For we must not rest.
2268 ;; There are more tokens to overload, more shoehorn, more methodologies. But
2269 ;; more is a plus! [Ha ha ha.] And more means plus! [Ho ho ho.] The future
2270 ;; is C++! [Ohhh!] The Third Millennium Award... Will be ours! [Roar.]
2272 (defconst c-font-lock-keywords-1 nil
2273 "Subdued level highlighting for C mode.")
2275 (defconst c-font-lock-keywords-2 nil
2276 "Medium level highlighting for C mode.
2277 See also `c-font-lock-extra-types'.")
2279 (defconst c-font-lock-keywords-3 nil
2280 "Gaudy level highlighting for C mode.
2281 See also `c-font-lock-extra-types'.")
2283 (let* ((c-keywords
2284 (eval-when-compile
2285 (regexp-opt '("break" "continue" "do" "else" "for" "if" "return"
2286 "switch" "while" "sizeof"
2287 ;; Type related, but we don't do anything special.
2288 "typedef" "extern" "auto" "register" "static"
2289 "volatile" "const"
2290 ;; Dan Nicolaescu <done@gnu.org> says this is new.
2291 "restrict"))))
2292 (c-type-specs
2293 (eval-when-compile
2294 (regexp-opt '("enum" "struct" "union"))))
2295 (c-type-specs-depth
2296 (regexp-opt-depth c-type-specs))
2297 (c-type-names
2298 `(mapconcat 'identity
2299 (cons
2300 ,(eval-when-compile
2301 (regexp-opt
2302 '("char" "short" "int" "long" "signed" "unsigned"
2303 "float" "double" "void" "complex")))
2304 c-font-lock-extra-types)
2305 "\\|"))
2306 (c-type-names-depth
2307 `(regexp-opt-depth ,c-type-names))
2308 (c-preprocessor-directives
2309 (eval-when-compile
2310 (regexp-opt
2311 '("define" "elif" "else" "endif" "error" "file" "if" "ifdef"
2312 "ifndef" "include" "line" "pragma" "undef"))))
2313 (c-preprocessor-directives-depth
2314 (regexp-opt-depth c-preprocessor-directives)))
2315 (setq c-font-lock-keywords-1
2316 (list
2318 ;; These are all anchored at the beginning of line for speed.
2319 ;; Note that `c++-font-lock-keywords-1' depends on `c-font-lock-keywords-1'.
2321 ;; Fontify function name definitions (GNU style; without type on line).
2322 '("^\\(\\sw+\\)[ \t]*(" 1 font-lock-function-name-face)
2324 ;; Fontify error directives.
2325 '("^#[ \t]*error[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend)
2327 ;; Fontify filenames in #include <...> preprocessor directives as strings.
2328 '("^#[ \t]*\\(import\\|include\\)[ \t]*\\(<[^>\"\n]*>?\\)"
2329 2 font-lock-string-face)
2331 ;; Fontify function macro names.
2332 '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
2334 ;; Fontify symbol names in #elif or #if ... defined preprocessor directives.
2335 '("^#[ \t]*\\(elif\\|if\\)\\>"
2336 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
2337 (1 font-lock-builtin-face) (2 font-lock-variable-name-face nil t)))
2339 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
2340 (list
2341 (concat "^#[ \t]*\\(" c-preprocessor-directives
2342 "\\)\\>[ \t!]*\\(\\sw+\\)?")
2343 '(1 font-lock-builtin-face)
2344 (list (+ 2 c-preprocessor-directives-depth)
2345 'font-lock-variable-name-face nil t))))
2347 (setq c-font-lock-keywords-2
2348 (append c-font-lock-keywords-1
2349 (list
2351 ;; Simple regexps for speed.
2353 ;; Fontify all type names.
2354 `(eval .
2355 (cons (concat "\\<\\(" ,c-type-names "\\)\\>") 'font-lock-type-face))
2357 ;; Fontify all builtin keywords (except case, default and goto; see below).
2358 (concat "\\<\\(" c-keywords "\\|" c-type-specs "\\)\\>")
2360 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2361 '("\\<\\(case\\|goto\\)\\>"
2362 (1 font-lock-keyword-face)
2363 ("\\(-[0-9]+\\|\\sw+\\)"
2364 ;; Return limit of search.
2365 (save-excursion (skip-chars-forward "^:\n") (point))
2367 (1 font-lock-constant-face nil t)))
2368 ;; Anders Lindgren <andersl@andersl.com> points out that it is quicker to
2369 ;; use MATCH-ANCHORED to effectively anchor the regexp on the left.
2370 ;; This must come after the one for keywords and targets.
2371 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2372 (beginning-of-line) (end-of-line)
2373 (1 font-lock-constant-face)))
2376 (setq c-font-lock-keywords-3
2377 (append c-font-lock-keywords-2
2379 ;; More complicated regexps for more complete highlighting for types.
2380 ;; We still have to fontify type specifiers individually, as C is so hairy.
2381 (list
2383 ;; Fontify all storage types, plus their items.
2384 `(eval .
2385 (list (concat "\\<\\(" ,c-type-names "\\)\\>"
2386 "\\([ \t*&]+\\sw+\\>\\)*")
2387 ;; Fontify each declaration item.
2388 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2389 ;; Start with point after all type specifiers.
2390 (list 'goto-char (list 'or
2391 (list 'match-beginning
2392 (+ ,c-type-names-depth 2))
2393 '(match-end 1)))
2394 ;; Finish with point after first type specifier.
2395 '(goto-char (match-end 1))
2396 ;; Fontify as a variable or function name.
2397 '(1 (if (match-beginning 2)
2398 font-lock-function-name-face
2399 font-lock-variable-name-face)))))
2401 ;; Fontify all storage specs and types, plus their items.
2402 `(eval .
2403 (list (concat "\\<\\(" ,c-type-specs "\\)\\>"
2404 "[ \t]*\\(\\sw+\\)?")
2405 (list 1 'font-lock-keyword-face)
2406 (list ,(+ c-type-specs-depth 2) 'font-lock-type-face nil t)
2407 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2408 nil nil
2409 ;; Fontify as a variable or function name.
2410 '(1 (if (match-beginning 2)
2411 font-lock-function-name-face
2412 font-lock-variable-name-face) nil t))))
2414 ;; Fontify structures, or typedef names, plus their items.
2415 '("\\(}\\)[ \t*]*\\sw"
2416 (font-lock-match-c-style-declaration-item-and-skip-to-next
2417 (goto-char (match-end 1)) nil
2418 (1 font-lock-type-face)))
2420 ;; Fontify anything at beginning of line as a declaration or definition.
2421 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2422 (1 font-lock-type-face)
2423 (font-lock-match-c-style-declaration-item-and-skip-to-next
2424 (goto-char (or (match-beginning 2) (match-end 1))) nil
2425 (1 (if (match-beginning 2)
2426 font-lock-function-name-face
2427 font-lock-variable-name-face))))
2431 (defvar c-font-lock-keywords c-font-lock-keywords-1
2432 "Default expressions to highlight in C mode.
2433 See also `c-font-lock-extra-types'.")
2435 ;;; C++.
2437 (defconst c++-font-lock-keywords-1 nil
2438 "Subdued level highlighting for C++ mode.")
2440 (defconst c++-font-lock-keywords-2 nil
2441 "Medium level highlighting for C++ mode.
2442 See also `c++-font-lock-extra-types'.")
2444 (defconst c++-font-lock-keywords-3 nil
2445 "Gaudy level highlighting for C++ mode.
2446 See also `c++-font-lock-extra-types'.")
2448 (defun font-lock-match-c++-style-declaration-item-and-skip-to-next (limit)
2449 ;; Regexp matches after point: word<word>::word (
2450 ;; ^^^^ ^^^^ ^^^^ ^
2451 ;; Where the match subexpressions are: 1 3 5 6
2453 ;; Item is delimited by (match-beginning 1) and (match-end 1).
2454 ;; If (match-beginning 3) is non-nil, that part of the item incloses a `<>'.
2455 ;; If (match-beginning 5) is non-nil, that part of the item follows a `::'.
2456 ;; If (match-beginning 6) is non-nil, the item is followed by a `('.
2457 (when (looking-at (eval-when-compile
2458 (concat
2459 ;; Skip any leading whitespace.
2460 "[ \t*&]*"
2461 ;; This is `c++-type-spec' from below. (Hint hint!)
2462 "\\(\\sw+\\)" ; The instance?
2463 "\\([ \t]*<\\([^>\n]+\\)[ \t*&]*>\\)?" ; Or template?
2464 "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)*" ; Or member?
2465 ;; Match any trailing parenthesis.
2466 "[ \t]*\\((\\)?")))
2467 (save-match-data
2468 (condition-case nil
2469 (save-restriction
2470 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
2471 (narrow-to-region (point-min) limit)
2472 (goto-char (match-end 1))
2473 ;; Move over any item value, etc., to the next item.
2474 (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)"))
2475 (goto-char (or (scan-sexps (point) 1) (point-max))))
2476 (goto-char (match-end 2)))
2477 (error t)))))
2479 (defun font-lock-match-c++-structor-declaration (limit)
2480 ;; Match C++ constructors and destructors inside class declarations.
2481 (let ((res nil)
2482 (regexp (concat "^\\s-+\\(\\(virtual\\|explicit\\)\\s-+\\)*~?\\(\\<"
2483 (mapconcat 'identity
2484 c++-font-lock-extra-types "\\|")
2485 "\\>\\)\\s-*("
2486 ;; Don't match function pointer declarations, e.g.:
2487 ;; Foo (*fptr)();
2488 "\\s-*[^*( \t]")))
2489 (while (progn (setq res (re-search-forward regexp limit t))
2490 (and res
2491 (save-excursion
2492 (beginning-of-line)
2493 (save-match-data
2494 (not (vectorp (c-at-toplevel-p))))))))
2495 res))
2497 (let* ((c++-keywords
2498 (eval-when-compile
2499 (regexp-opt
2500 '("break" "continue" "do" "else" "for" "if" "return" "switch"
2501 "while" "asm" "catch" "delete" "new" "sizeof" "this" "throw" "try"
2502 "typeid"
2503 ;; Branko Cibej <branko.cibej@hermes.si> says this is new.
2504 "export"
2505 ;; Mark Mitchell <mmitchell@usa.net> says these are new.
2506 "mutable" "explicit"
2507 ;; Alain Picard <ap@abelard.apana.org.au> suggests treating these
2508 ;; as keywords not types.
2509 "typedef" "template"
2510 "extern" "auto" "register" "const" "volatile" "static"
2511 "inline" "friend" "virtual"))))
2512 (c++-operators
2513 (eval-when-compile
2514 (regexp-opt
2515 ;; Taken from Stroustrup, minus keywords otherwise fontified.
2516 '("+" "-" "*" "/" "%" "^" "&" "|" "~" "!" "=" "<" ">" "+=" "-="
2517 "*=" "/=" "%=" "^=" "&=" "|=" "<<" ">>" ">>=" "<<=" "==" "!="
2518 "<=" ">=" "&&" "||" "++" "--" "->*" "," "->" "[]" "()"))))
2519 (c++-type-specs
2520 (eval-when-compile
2521 (regexp-opt
2522 '("class" "public" "private" "protected" "typename"
2523 "struct" "union" "enum" "namespace" "using"
2524 ;; Eric Hopper <hopper@omnifarious.mn.org> says these are new.
2525 "static_cast" "dynamic_cast" "const_cast" "reinterpret_cast") t)))
2526 (c++-type-specs-depth
2527 (regexp-opt-depth c++-type-specs))
2528 (c++-type-names
2529 `(mapconcat 'identity
2530 (cons
2531 ,(eval-when-compile
2532 (regexp-opt
2533 '("signed" "unsigned" "short" "long"
2534 "int" "char" "float" "double" "void"
2535 "bool" "complex")))
2536 c++-font-lock-extra-types)
2537 "\\|"))
2538 (c++-type-names-depth `(regexp-opt-depth ,c++-type-names))
2540 ;; A brave attempt to match templates following a type and/or match
2541 ;; class membership. See and sync the above function
2542 ;; `font-lock-match-c++-style-declaration-item-and-skip-to-next'.
2543 (c++-type-suffix (concat "\\([ \t]*<\\([^>\n]+\\)[ \t*&]*>\\)?"
2544 "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)*"))
2545 (c++-type-suffix-depth (regexp-opt-depth c++-type-suffix))
2546 ;; If the string is a type, it may be followed by the cruft above.
2547 (c++-type-spec (concat "\\(\\sw+\\)\\>" c++-type-suffix))
2548 (c++-type-spec-depth (regexp-opt-depth c++-type-spec))
2550 ;; Parenthesis depth of user-defined types not forgetting their cruft.
2551 (c++-type-depth `(regexp-opt-depth
2552 (concat ,c++-type-names ,c++-type-suffix)))
2554 (setq c++-font-lock-keywords-1
2555 (append
2557 ;; The list `c-font-lock-keywords-1' less that for function names.
2558 (cdr c-font-lock-keywords-1)
2559 (list
2561 ;; Fontify function name definitions, possibly incorporating class names.
2562 (list (concat "^" c++-type-spec "[ \t]*(")
2563 '(1 (if (or (match-beginning 2) (match-beginning 4))
2564 font-lock-type-face
2565 font-lock-function-name-face))
2566 '(3 font-lock-type-face nil t)
2567 '(5 font-lock-function-name-face nil t))
2570 (setq c++-font-lock-keywords-2
2571 (append c++-font-lock-keywords-1
2572 (list
2574 ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading.
2575 `(eval .
2576 (cons (concat "\\<\\(" ,c++-type-names "\\)\\>")
2577 'font-lock-type-face))
2579 ;; Fontify operator overloading.
2580 (list (concat "\\<\\(operator\\)\\>[ \t]*\\(" c++-operators "\\)?")
2581 '(1 font-lock-keyword-face)
2582 '(2 font-lock-builtin-face nil t))
2584 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2585 '("\\<\\(case\\|goto\\)\\>"
2586 (1 font-lock-keyword-face)
2587 ("\\(-[0-9]+\\|\\sw+\\)[ \t]*\\(::\\)?"
2588 ;; Return limit of search.
2589 (save-excursion
2590 (while (progn
2591 (skip-chars-forward "^:\n")
2592 (looking-at "::"))
2593 (forward-char 2))
2594 (point))
2596 (1 (if (match-beginning 2)
2597 font-lock-type-face
2598 font-lock-constant-face) nil t)))
2599 ;; This must come after the one for keywords and targets.
2600 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:\\($\\|[^:]\\)"
2601 (beginning-of-line) (end-of-line)
2602 (1 font-lock-constant-face)))
2604 ;; Fontify other builtin keywords.
2605 (concat "\\<\\(" c++-keywords "\\|" c++-type-specs "\\)\\>")
2607 ;; Eric Hopper <hopper@omnifarious.mn.org> says `true' and `false' are new.
2608 '("\\<\\(false\\|true\\)\\>" . font-lock-constant-face)
2611 (setq c++-font-lock-keywords-3
2612 (append c++-font-lock-keywords-2
2614 ;; More complicated regexps for more complete highlighting for types.
2615 (list
2617 ;; Fontify all storage classes and type specifiers, plus their items.
2618 `(eval .
2619 (list (concat "\\<\\(" ,c++-type-names "\\)\\>" ,c++-type-suffix
2620 "\\([ \t*&]+" ,c++-type-spec "\\)*")
2621 ;; The name of any template type.
2622 (list (+ ,c++-type-names-depth 3) 'font-lock-type-face nil t)
2623 ;; Fontify each declaration item.
2624 (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next
2625 ;; Start with point after all type specifiers.
2626 (list 'goto-char (list 'or (list 'match-beginning
2627 (+ ,c++-type-depth 2))
2628 '(match-end 1)))
2629 ;; Finish with point after first type specifier.
2630 '(goto-char (match-end 1))
2631 ;; Fontify as a variable or function name.
2632 '(1 (cond ((or (match-beginning 2) (match-beginning 4))
2633 font-lock-type-face)
2634 ((and (match-beginning 6) (c-at-toplevel-p))
2635 font-lock-function-name-face)
2637 font-lock-variable-name-face)))
2638 '(3 font-lock-type-face nil t)
2639 '(5 (if (match-beginning 6)
2640 font-lock-function-name-face
2641 font-lock-variable-name-face) nil t))))
2643 ;; Fontify all storage specs and types, plus their items.
2644 `(eval .
2645 (list (concat "\\<" ,c++-type-specs "\\>" ,c++-type-suffix
2646 "[ \t]*\\(" ,c++-type-spec "\\)?")
2647 ;; The name of any template type.
2648 (list ,(+ c++-type-specs-depth 2) 'font-lock-type-face nil t)
2649 ;; The name of any type.
2650 (list (+ ,c++-type-specs-depth ,c++-type-suffix-depth 2)
2651 'font-lock-type-face nil t)
2652 ;; Fontify each declaration item.
2653 (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next
2654 ;; Start with point after all type specifiers.
2656 ;; Finish with point after first type specifier.
2658 ;; Fontify as a variable or function name.
2659 '(1 (cond ((or (match-beginning 2) (match-beginning 4))
2660 font-lock-type-face)
2661 ((and (match-beginning 6) (c-at-toplevel-p))
2662 font-lock-function-name-face)
2664 font-lock-variable-name-face)))
2665 '(3 font-lock-type-face nil t)
2666 '(5 (if (match-beginning 6)
2667 font-lock-function-name-face
2668 font-lock-variable-name-face) nil t))
2671 ;; Fontify structures, or typedef names, plus their items.
2672 '("\\(}\\)[ \t*]*\\sw"
2673 (font-lock-match-c++-style-declaration-item-and-skip-to-next
2674 (goto-char (match-end 1)) nil
2675 (1 font-lock-type-face)))
2677 ;; Fontify anything at beginning of line as a declaration or definition.
2678 (list (concat "^\\(" c++-type-spec "[ \t*&]*\\)+")
2679 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
2680 (goto-char (match-beginning 1))
2681 (goto-char (match-end 1))
2682 (1 (cond ((or (match-beginning 2) (match-beginning 4))
2683 font-lock-type-face)
2684 ((match-beginning 6) font-lock-function-name-face)
2685 (t font-lock-variable-name-face)))
2686 (3 font-lock-type-face nil t)
2687 (5 (if (match-beginning 6)
2688 font-lock-function-name-face
2689 font-lock-variable-name-face) nil t)))
2691 ;; Fontify constructors and destructors inside class declarations.
2692 '(font-lock-match-c++-structor-declaration
2693 (3 font-lock-function-name-face t))
2697 (defvar c++-font-lock-keywords c++-font-lock-keywords-1
2698 "Default expressions to highlight in C++ mode.
2699 See also `c++-font-lock-extra-types'.")
2701 ;;; Objective-C.
2703 (defconst objc-font-lock-keywords-1 nil
2704 "Subdued level highlighting for Objective-C mode.")
2706 (defconst objc-font-lock-keywords-2 nil
2707 "Medium level highlighting for Objective-C mode.
2708 See also `objc-font-lock-extra-types'.")
2710 (defconst objc-font-lock-keywords-3 nil
2711 "Gaudy level highlighting for Objective-C mode.
2712 See also `objc-font-lock-extra-types'.")
2714 ;; Regexps written with help from Stephen Peters <speters@us.oracle.com> and
2715 ;; Jacques Duthen Prestataire <duthen@cegelec-red.fr>.
2716 (let* ((objc-keywords
2717 (eval-when-compile
2718 (regexp-opt '("break" "continue" "do" "else" "for" "if" "return"
2719 "switch" "while" "sizeof" "self" "super"
2720 "typedef" "auto" "extern" "static"
2721 "volatile" "const"))))
2722 (objc-type-specs
2723 (eval-when-compile
2724 (regexp-opt
2725 '("register" "struct" "union" "enum"
2726 "oneway" "in" "out" "inout" "bycopy" "byref") t)))
2727 (objc-type-specs-depth
2728 (regexp-opt-depth objc-type-specs))
2729 (objc-type-names
2730 `(mapconcat 'identity
2731 (cons
2732 ,(eval-when-compile
2733 (regexp-opt
2734 '("signed" "unsigned" "short" "long"
2735 "int" "char" "float" "double" "void"
2736 "id")))
2737 objc-font-lock-extra-types)
2738 "\\|"))
2739 (objc-type-names-depth
2740 `(regexp-opt-depth ,objc-type-names))
2742 (setq objc-font-lock-keywords-1
2743 (append
2745 ;; The list `c-font-lock-keywords-1' less that for function names.
2746 (cdr c-font-lock-keywords-1)
2747 (list
2749 ;; Fontify compiler directives.
2750 '("@\\(\\sw+\\)\\>"
2751 (1 font-lock-keyword-face)
2752 ("\\=[ \t:<,]*\\(\\sw+\\)" nil nil
2753 (1 font-lock-type-face)))
2755 ;; Fontify method names and arguments. Oh Lordy!
2756 ;; First, on the same line as the function declaration.
2757 '("^[+-][ \t]*\\(PRIVATE\\>\\)?[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2758 (1 font-lock-keyword-face nil t)
2759 (3 font-lock-function-name-face)
2760 ("\\=[ \t]*\\(\\sw+\\)?:[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2761 nil nil
2762 (1 font-lock-function-name-face nil t)
2763 (3 font-lock-variable-name-face)))
2764 ;; Second, on lines following the function declaration.
2765 '(":" ("^[ \t]*\\(\\sw+\\)?:[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2766 (beginning-of-line) (end-of-line)
2767 (1 font-lock-function-name-face nil t)
2768 (3 font-lock-variable-name-face)))
2771 (setq objc-font-lock-keywords-2
2772 (append objc-font-lock-keywords-1
2773 (list
2775 ;; Simple regexps for speed.
2777 ;; Fontify all type specifiers.
2778 `(eval .
2779 (cons (concat "\\<\\(" ,objc-type-names "\\)\\>")
2780 'font-lock-type-face))
2782 ;; Fontify all builtin keywords (except case, default and goto; see below).
2783 (concat "\\<\\(" objc-keywords "\\|" objc-type-specs "\\)\\>")
2785 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2786 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2787 (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
2788 ;; Fontify tags iff sole statement on line, otherwise we detect selectors.
2789 ;; This must come after the one for keywords and targets.
2790 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2791 (beginning-of-line) (end-of-line)
2792 (1 font-lock-constant-face)))
2794 ;; Fontify null object pointers.
2795 '("\\<[Nn]il\\>" . font-lock-constant-face)
2798 (setq objc-font-lock-keywords-3
2799 (append objc-font-lock-keywords-2
2801 ;; More complicated regexps for more complete highlighting for types.
2802 ;; We still have to fontify type specifiers individually, as C is so hairy.
2803 (list
2805 ;; Fontify all storage classes and type specifiers, plus their items.
2806 `(eval .
2807 (list (concat "\\<\\(" ,objc-type-names "\\)\\>"
2808 "\\([ \t*&]+\\sw+\\>\\)*")
2809 ;; Fontify each declaration item.
2810 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2811 ;; Start with point after all type specifiers.
2812 (list 'goto-char
2813 (list 'or (list 'match-beginning
2814 (+ ,objc-type-names-depth 2))
2815 '(match-end 1)))
2816 ;; Finish with point after first type specifier.
2817 '(goto-char (match-end 1))
2818 ;; Fontify as a variable or function name.
2819 '(1 (if (match-beginning 2)
2820 font-lock-function-name-face
2821 font-lock-variable-name-face)))))
2823 ;; Fontify all storage specs and types, plus their items.
2824 `(eval .
2825 (list (concat "\\<\\(" ,objc-type-specs "[ \t]*\\)+\\>"
2826 "[ \t]*\\(\\sw+\\)?")
2827 ;; The name of any type.
2828 (list ,(+ objc-type-specs-depth 2) 'font-lock-type-face nil t)
2829 ;; Fontify each declaration item.
2830 (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next
2831 nil nil
2832 ;; Fontify as a variable or function name.
2833 '(1 (if (match-beginning 2)
2834 font-lock-function-name-face
2835 font-lock-variable-name-face)))
2838 ;; Fontify structures, or typedef names, plus their items.
2839 '("\\(}\\)[ \t*]*\\sw"
2840 (font-lock-match-c-style-declaration-item-and-skip-to-next
2841 (goto-char (match-end 1)) nil
2842 (1 font-lock-type-face)))
2844 ;; Fontify anything at beginning of line as a declaration or definition.
2845 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2846 (1 font-lock-type-face)
2847 (font-lock-match-c-style-declaration-item-and-skip-to-next
2848 (goto-char (or (match-beginning 2) (match-end 1))) nil
2849 (1 (if (match-beginning 2)
2850 font-lock-function-name-face
2851 font-lock-variable-name-face))))
2855 (defvar objc-font-lock-keywords objc-font-lock-keywords-1
2856 "Default expressions to highlight in Objective-C mode.
2857 See also `objc-font-lock-extra-types'.")
2859 ;;; Java.
2861 (defconst java-font-lock-keywords-1 nil
2862 "Subdued level highlighting for Java mode.")
2864 (defconst java-font-lock-keywords-2 nil
2865 "Medium level highlighting for Java mode.
2866 See also `java-font-lock-extra-types'.")
2868 (defconst java-font-lock-keywords-3 nil
2869 "Gaudy level highlighting for Java mode.
2870 See also `java-font-lock-extra-types'.")
2872 ;; Regexps written with help from Fred White <fwhite@bbn.com>,
2873 ;; Anders Lindgren <andersl@andersl.com> and Carl Manning <caroma@ai.mit.edu>.
2874 (let* ((java-keywords
2875 (eval-when-compile
2876 (regexp-opt
2877 '("catch" "do" "else" "super" "this" "finally" "for" "if"
2878 ;; Anders Lindgren <andersl@andersl.com> says these have gone.
2879 ;; "cast" "byvalue" "future" "generic" "operator" "var"
2880 ;; "inner" "outer" "rest"
2881 "implements" "extends" "throws" "instanceof" "new"
2882 "interface" "return" "switch" "throw" "try" "while"))))
2884 ;; Classes immediately followed by an object name.
2885 (java-type-names
2886 `(mapconcat 'identity
2887 (cons
2888 ,(eval-when-compile
2889 (regexp-opt '("boolean" "char" "byte" "short" "int" "long"
2890 "float" "double" "void")))
2891 java-font-lock-extra-types)
2892 "\\|"))
2893 (java-type-names-depth `(regexp-opt-depth ,java-type-names))
2895 ;; These are eventually followed by an object name.
2896 (java-type-specs
2897 (eval-when-compile
2898 (regexp-opt
2899 '("abstract" "const" "final" "synchronized" "transient" "static"
2900 ;; Anders Lindgren <andersl@andersl.com> says this has gone.
2901 ;; "threadsafe"
2902 "volatile" "public" "private" "protected" "native"
2903 ;; Carl Manning <caroma@ai.mit.edu> says this is new.
2904 "strictfp"))))
2906 (setq java-font-lock-keywords-1
2907 (list
2909 ;; Fontify class names.
2910 '("\\<\\(class\\)\\>[ \t]*\\(\\sw+\\)?"
2911 (1 font-lock-keyword-face) (2 font-lock-type-face nil t))
2913 ;; Fontify package names in import directives.
2914 '("\\<\\(import\\|package\\)\\>[ \t]*\\(\\sw+\\)?"
2915 (1 font-lock-keyword-face)
2916 (2 font-lock-constant-face nil t)
2917 ("\\=\\.\\(\\*\\|\\sw+\\)" nil nil
2918 (1 font-lock-constant-face nil t)))
2921 (setq java-font-lock-keywords-2
2922 (append java-font-lock-keywords-1
2923 (list
2925 ;; Fontify class names.
2926 `(eval .
2927 (cons (concat "\\<\\(" ,java-type-names "\\)\\>[^.]")
2928 '(1 font-lock-type-face)))
2930 ;; Fontify all builtin keywords (except below).
2931 (concat "\\<\\(" java-keywords "\\|" java-type-specs "\\)\\>")
2933 ;; Fontify keywords and targets, and case default/goto tags.
2934 (list "\\<\\(break\\|case\\|continue\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2935 '(1 font-lock-keyword-face) '(2 font-lock-constant-face nil t))
2936 ;; This must come after the one for keywords and targets.
2937 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2938 (beginning-of-line) (end-of-line)
2939 (1 font-lock-constant-face)))
2941 ;; Fontify all constants.
2942 '("\\<\\(false\\|null\\|true\\)\\>" . font-lock-constant-face)
2944 ;; Javadoc tags within comments.
2945 (list
2946 (concat "@\\("
2947 "author\\|deprecated\\|exception"
2948 "\\|link\\|return\\|see\\|serial\\|serialData\\|serialField"
2949 "\\|since\\|throws"
2950 "\\|version"
2951 "\\)\\>"))
2952 '("@\\(param\\)\\>[ \t]*\\(\\sw+\\)?"
2953 (1 font-lock-constant-face prepend)
2954 (2 font-lock-variable-name-face prepend t))
2955 '("@\\(exception\\|throws\\)\\>[ \t]*\\(\\S-+\\)?"
2956 (1 font-lock-constant-face prepend)
2957 (2 font-lock-type-face prepend t))
2960 (setq java-font-lock-keywords-3
2961 (append java-font-lock-keywords-2
2963 ;; More complicated regexps for more complete highlighting for types.
2964 ;; We still have to fontify type specifiers individually, as Java is hairy.
2965 (list
2967 ;; Fontify random types immediately followed by an item or items.
2968 `(eval .
2969 (list (concat "\\<\\(" ,java-type-names "\\)\\>"
2970 "\\([ \t]*\\[[ \t]*\\]\\)*"
2971 "\\([ \t]*\\sw\\)")
2972 ;; Fontify each declaration item.
2973 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2974 ;; Start and finish with point after the type specifier.
2975 (list 'goto-char (list 'match-beginning
2976 (+ ,java-type-names-depth 3)))
2977 (list 'goto-char (list 'match-beginning
2978 (+ ,java-type-names-depth 3)))
2979 ;; Fontify as a variable or function name.
2980 '(1 (if (match-beginning 2)
2981 font-lock-function-name-face
2982 font-lock-variable-name-face)))))
2984 ;; Fontify those that are eventually followed by an item or items.
2985 (list (concat "\\<\\(" java-type-specs "\\)\\>"
2986 "\\([ \t]+\\sw+\\>"
2987 "\\([ \t]*\\[[ \t]*\\]\\)*"
2988 "\\)*")
2989 ;; Fontify each declaration item.
2990 '(font-lock-match-c-style-declaration-item-and-skip-to-next
2991 ;; Start with point after all type specifiers.
2992 (goto-char (or (match-beginning 5) (match-end 1)))
2993 ;; Finish with point after first type specifier.
2994 (goto-char (match-end 1))
2995 ;; Fontify as a variable or function name.
2996 (1 (if (match-beginning 2)
2997 font-lock-function-name-face
2998 font-lock-variable-name-face))))
3002 (defvar java-font-lock-keywords java-font-lock-keywords-1
3003 "Default expressions to highlight in Java mode.
3004 See also `java-font-lock-extra-types'.")
3006 ;; Install ourselves:
3008 ;; Useful for the popup-menu for mouse-3 on the modeline.
3009 (unless (assq 'font-lock-mode minor-mode-alist)
3010 (push '(font-lock-mode nil) minor-mode-alist))
3012 ;; Provide ourselves:
3014 (provide 'font-lock)
3015 (require 'jit-lock)
3017 ;;; font-lock.el ends here