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