1 ;;; font-lock.el --- Electric font lock mode
3 ;; Copyright (C) 1992-1999 Free Software Foundation, Inc.
5 ;; Author: jwz, then rms, then sm <simon@gnu.org>
7 ;; Keywords: languages, faces
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Font Lock mode is a minor mode that causes your comments to be displayed in
29 ;; one face, strings in another, reserved words in another, and so on.
31 ;; Comments will be displayed in `font-lock-comment-face'.
32 ;; Strings will be displayed in `font-lock-string-face'.
33 ;; Regexps are used to display selected patterns in other faces.
35 ;; To make the text you type be fontified, use M-x font-lock-mode RET.
36 ;; When this minor mode is on, the faces of the current line are updated with
37 ;; every insertion or deletion.
39 ;; To turn Font Lock mode on automatically, add this to your ~/.emacs file:
41 ;; (add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock)
43 ;; Or if you want to turn Font Lock mode on in many modes:
45 ;; (global-font-lock-mode t)
47 ;; Fontification for a particular mode may be available in a number of levels
48 ;; of decoration. The higher the level, the more decoration, but the more time
49 ;; it takes to fontify. See the variable `font-lock-maximum-decoration', and
50 ;; also the variable `font-lock-maximum-size'. Support modes for Font Lock
51 ;; mode can be used to speed up Font Lock mode. See `font-lock-support-mode'.
53 ;;; How Font Lock mode fontifies:
55 ;; When Font Lock mode is turned on in a buffer, it (a) fontifies the entire
56 ;; buffer and (b) installs one of its fontification functions on one of the
57 ;; hook variables that are run by Emacs after every buffer change (i.e., an
58 ;; insertion or deletion). Fontification means the replacement of `face' text
59 ;; properties in a given region; Emacs displays text with these `face' text
60 ;; properties appropriately.
62 ;; Fontification normally involves syntactic (i.e., strings and comments) and
63 ;; regexp (i.e., keywords and everything else) passes. There are actually
64 ;; three passes; (a) the syntactic keyword pass, (b) the syntactic pass and (c)
65 ;; the keyword pass. Confused?
67 ;; The syntactic keyword pass places `syntax-table' text properties in the
68 ;; buffer according to the variable `font-lock-syntactic-keywords'. It is
69 ;; necessary because Emacs' syntax table is not powerful enough to describe all
70 ;; the different syntactic constructs required by the sort of people who decide
71 ;; that a single quote can be syntactic or not depending on the time of day.
72 ;; (What sort of person could decide to overload the meaning of a quote?)
73 ;; Obviously the syntactic keyword pass must occur before the syntactic pass.
75 ;; The syntactic pass places `face' text properties in the buffer according to
76 ;; syntactic context, i.e., according to the buffer's syntax table and buffer
77 ;; text's `syntax-table' text properties. It involves using a syntax parsing
78 ;; function to determine the context of different parts of a region of text. A
79 ;; syntax parsing function is necessary because generally strings and/or
80 ;; comments can span lines, and so the context of a given region is not
81 ;; necessarily apparent from the content of that region. Because the keyword
82 ;; pass only works within a given region, it is not generally appropriate for
83 ;; syntactic fontification. This is the first fontification pass that makes
84 ;; changes visible to the user; it fontifies strings and comments.
86 ;; The keyword pass places `face' text properties in the buffer according to
87 ;; the variable `font-lock-keywords'. It involves searching for given regexps
88 ;; (or calling given search functions) within the given region. This is the
89 ;; second fontification pass that makes changes visible to the user; it
90 ;; fontifies language reserved words, etc.
92 ;; Oh, and the answer is, "Yes, obviously just about everything should be done
93 ;; in a single syntactic pass, but the only syntactic parser available
94 ;; understands only strings and comments." Perhaps one day someone will write
95 ;; some syntactic parsers for common languages and a son-of-font-lock.el could
96 ;; use them rather then relying so heavily on the keyword (regexp) pass.
98 ;;; How Font Lock mode supports modes or is supported by modes:
100 ;; Modes that support Font Lock mode do so by defining one or more variables
101 ;; whose values specify the fontification. Font Lock mode knows of these
102 ;; variable names from (a) the buffer local variable `font-lock-defaults', if
103 ;; non-nil, or (b) the global variable `font-lock-defaults-alist', if the major
104 ;; mode has an entry. (Font Lock mode is set up via (a) where a mode's
105 ;; patterns are distributed with the mode's package library, and (b) where a
106 ;; mode's patterns are distributed with font-lock.el itself. An example of (a)
107 ;; is Pascal mode, an example of (b) is Lisp mode. Normally, the mechanism is
108 ;; (a); (b) is used where it is not clear which package library should contain
109 ;; the pattern definitions.) Font Lock mode chooses which variable to use for
110 ;; fontification based on `font-lock-maximum-decoration'.
112 ;; Font Lock mode fontification behaviour can be modified in a number of ways.
113 ;; See the below comments and the comments distributed throughout this file.
115 ;;; Constructing patterns:
117 ;; See the documentation for the variable `font-lock-keywords'.
119 ;; Efficient regexps for use as MATCHERs for `font-lock-keywords' and
120 ;; `font-lock-syntactic-keywords' can be generated via the function
121 ;; `regexp-opt', and their depth counted via the function `regexp-opt-depth'.
123 ;;; Adding patterns for modes that already support Font Lock:
125 ;; Though Font Lock highlighting patterns already exist for many modes, it's
126 ;; likely there's something that you want fontified that currently isn't, even
127 ;; at the maximum fontification level. You can add highlighting patterns via
128 ;; `font-lock-add-keywords'. For example, say in some C
129 ;; header file you #define the token `and' to expand to `&&', etc., to make
130 ;; your C code almost readable. In your ~/.emacs there could be:
132 ;; (font-lock-add-keywords 'c-mode '("\\<\\(and\\|or\\|not\\)\\>"))
134 ;; Some modes provide specific ways to modify patterns based on the values of
135 ;; other variables. For example, additional C types can be specified via the
136 ;; variable `c-font-lock-extra-types'.
138 ;;; Adding patterns for modes that do not support Font Lock:
140 ;; Not all modes support Font Lock mode. If you (as a user of the mode) add
141 ;; patterns for a new mode, you must define in your ~/.emacs a variable or
142 ;; variables that specify regexp fontification. Then, you should indicate to
143 ;; Font Lock mode, via the mode hook setting `font-lock-defaults', exactly what
144 ;; support is required. For example, say Foo mode should have the following
145 ;; regexps fontified case-sensitively, and comments and strings should not be
146 ;; fontified automagically. In your ~/.emacs there could be:
148 ;; (defvar foo-font-lock-keywords
149 ;; '(("\\<\\(one\\|two\\|three\\)\\>" . font-lock-keyword-face)
150 ;; ("\\<\\(four\\|five\\|six\\)\\>" . font-lock-type-face))
151 ;; "Default expressions to highlight in Foo mode.")
153 ;; (add-hook 'foo-mode-hook
154 ;; (function (lambda ()
155 ;; (make-local-variable 'font-lock-defaults)
156 ;; (setq font-lock-defaults '(foo-font-lock-keywords t)))))
158 ;;; Adding Font Lock support for modes:
160 ;; Of course, it would be better that the mode already supports Font Lock mode.
161 ;; The package author would do something similar to above. The mode must
162 ;; define at the top-level a variable or variables that specify regexp
163 ;; fontification. Then, the mode command should indicate to Font Lock mode,
164 ;; via `font-lock-defaults', exactly what support is required. For example,
165 ;; say Bar mode should have the following regexps fontified case-insensitively,
166 ;; and comments and strings should be fontified automagically. In bar.el there
169 ;; (defvar bar-font-lock-keywords
170 ;; '(("\\<\\(uno\\|due\\|tre\\)\\>" . font-lock-keyword-face)
171 ;; ("\\<\\(quattro\\|cinque\\|sei\\)\\>" . font-lock-type-face))
172 ;; "Default expressions to highlight in Bar mode.")
174 ;; and within `bar-mode' there could be:
176 ;; (make-local-variable 'font-lock-defaults)
177 ;; (setq font-lock-defaults '(bar-font-lock-keywords nil t))
179 ;; What is fontification for? You might say, "It's to make my code look nice."
180 ;; I think it should be for adding information in the form of cues. These cues
181 ;; should provide you with enough information to both (a) distinguish between
182 ;; different items, and (b) identify the item meanings, without having to read
183 ;; the items and think about it. Therefore, fontification allows you to think
184 ;; less about, say, the structure of code, and more about, say, why the code
185 ;; doesn't work. Or maybe it allows you to think less and drift off to sleep.
187 ;; So, here are my opinions/advice/guidelines:
189 ;; - Highlight conceptual objects, such as function and variable names, and
190 ;; different objects types differently, i.e., (a) and (b) above, highlight
191 ;; function names differently to variable names.
192 ;; - Keep the faces distinct from each other as far as possible.
194 ;; - Use the same face for the same conceptual object, across all modes.
195 ;; i.e., (b) above, all modes that have items that can be thought of as, say,
196 ;; keywords, should be highlighted with the same face, etc.
197 ;; - Make the face attributes fit the concept as far as possible.
198 ;; i.e., function names might be a bold colour such as blue, comments might
199 ;; be a bright colour such as red, character strings might be brown, because,
200 ;; err, strings are brown (that was not the reason, please believe me).
201 ;; - Don't use a non-nil OVERRIDE unless you have a good reason.
202 ;; Only use OVERRIDE for special things that are easy to define, such as the
203 ;; way `...' quotes are treated in strings and comments in Emacs Lisp mode.
204 ;; Don't use it to, say, highlight keywords in commented out code or strings.
209 ;; Define core `font-lock' group.
210 (defgroup font-lock nil
211 "Font Lock mode text highlighting package."
212 :link
'(custom-manual "(emacs)Font Lock")
213 :link
'(custom-manual "(elisp)Font Lock Mode")
216 (defgroup font-lock-highlighting-faces nil
217 "Faces for highlighting text."
221 (defgroup font-lock-extra-types nil
222 "Extra mode-specific type names for highlighting declarations."
225 ;; Define support mode groups here to impose `font-lock' group order.
226 (defgroup fast-lock nil
227 "Font Lock support mode to cache fontification."
228 :link
'(custom-manual "(emacs)Support Modes")
232 (defgroup lazy-lock nil
233 "Font Lock support mode to fontify lazily."
234 :link
'(custom-manual "(emacs)Support Modes")
238 (defgroup jit-lock nil
239 "Font Lock support mode to fontify just-in-time."
240 :link
'(custom-manual "(emacs)Support Modes")
247 (defcustom font-lock-maximum-size
256000
248 "*Maximum size of a buffer for buffer fontification.
249 Only buffers less than this can be fontified when Font Lock mode is turned on.
250 If nil, means size is irrelevant.
251 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
252 where MAJOR-MODE is a symbol or t (meaning the default). For example:
253 ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576))
254 means that the maximum size is 250K for buffers in C or C++ modes, one megabyte
255 for buffers in Rmail mode, and size is irrelevant otherwise."
256 :type
'(choice (const :tag
"none" nil
)
257 (integer :tag
"size")
258 (repeat :menu-tag
"mode specific" :tag
"mode specific"
260 (cons :tag
"Instance"
263 (symbol :tag
"name"))
265 (const :tag
"none" nil
)
266 (integer :tag
"size")))))
269 (defcustom font-lock-maximum-decoration t
270 "*Maximum decoration level for fontification.
271 If nil, use the default decoration (typically the minimum available).
272 If t, use the maximum decoration available.
273 If a number, use that level of decoration (or if not available the maximum).
274 If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
275 where MAJOR-MODE is a symbol or t (meaning the default). For example:
276 ((c-mode . t) (c++-mode . 2) (t . 1))
277 means use the maximum decoration available for buffers in C mode, level 2
278 decoration for buffers in C++ mode, and level 1 decoration otherwise."
279 :type
'(choice (const :tag
"default" nil
)
280 (const :tag
"maximum" t
)
281 (integer :tag
"level" 1)
282 (repeat :menu-tag
"mode specific" :tag
"mode specific"
284 (cons :tag
"Instance"
287 (symbol :tag
"name"))
288 (radio :tag
"Decoration"
289 (const :tag
"default" nil
)
290 (const :tag
"maximum" t
)
291 (integer :tag
"level" 1)))))
294 (defcustom font-lock-verbose
0
295 "*If non-nil, means show status messages for buffer fontification.
296 If a number, only buffers greater than this size have fontification messages."
297 :type
'(choice (const :tag
"never" nil
)
298 (other :tag
"always" t
)
299 (integer :tag
"size"))
302 ;; Fontification variables:
304 (defvar font-lock-keywords nil
305 "A list of the keywords to highlight.
306 Each element should have one of these forms:
311 (MATCHER . HIGHLIGHT)
312 (MATCHER HIGHLIGHT ...)
315 where HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
317 FORM is an expression, whose value should be a keyword element, evaluated when
318 the keyword is (first) used in a buffer. This feature can be used to provide a
319 keyword that can only be generated when Font Lock mode is actually turned on.
321 For highlighting single items, for example each instance of the word \"foo\",
322 typically only MATCH-HIGHLIGHT is required.
323 However, if an item or (typically) items are to be highlighted following the
324 instance of another item (the anchor), for example each instance of the
325 word \"bar\" following the word \"anchor\" then MATCH-ANCHORED may be required.
327 MATCH-HIGHLIGHT should be of the form:
329 (MATCH FACENAME OVERRIDE LAXMATCH)
331 where MATCHER can be either the regexp to search for, or the function name to
332 call to make the search (called with one argument, the limit of the search) and
333 return non-nil if it succeeds (and set `match-data' appropriately).
334 MATCHER regexps can be generated via the function `regexp-opt'. MATCH is the
335 subexpression of MATCHER to be highlighted. MATCH can be calculated via the
336 function `regexp-opt-depth'. FACENAME is an expression whose value is the face
337 name to use. Face default attributes can be modified via \\[customize].
339 OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification can
340 be overwritten. If `keep', only parts not already fontified are highlighted.
341 If `prepend' or `append', existing fontification is merged with the new, in
342 which the new or existing fontification, respectively, takes precedence.
343 If LAXMATCH is non-nil, no error is signaled if there is no MATCH in MATCHER.
345 For example, an element of the form highlights (if not already highlighted):
347 \"\\\\\\=<foo\\\\\\=>\" discrete occurrences of \"foo\" in the value of the
348 variable `font-lock-keyword-face'.
349 (\"fu\\\\(bar\\\\)\" . 1) substring \"bar\" within all occurrences of \"fubar\" in
350 the value of `font-lock-keyword-face'.
351 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'.
352 (\"foo\\\\|bar\" 0 foo-bar-face t)
353 occurrences of either \"foo\" or \"bar\" in the value
354 of `foo-bar-face', even if already highlighted.
355 (fubar-match 1 fubar-face)
356 the first subexpression within all occurrences of
357 whatever the function `fubar-match' finds and matches
358 in the value of `fubar-face'.
360 MATCH-ANCHORED should be of the form:
362 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
364 where MATCHER is a regexp to search for or the function name to call to make
365 the search, as for MATCH-HIGHLIGHT above, but with one exception; see below.
366 PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
367 the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be
368 used to initialise before, and cleanup after, MATCHER is used. Typically,
369 PRE-MATCH-FORM is used to move to some position relative to the original
370 MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might
371 be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER.
373 For example, an element of the form highlights (if not already highlighted):
375 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
377 discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
378 discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
379 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is
380 initially searched for starting from the end of the match of \"anchor\", and
381 searching for subsequent instance of \"anchor\" resumes from where searching
382 for \"item\" concluded.)
384 The above-mentioned exception is as follows. The limit of the MATCHER search
385 defaults to the end of the line after PRE-MATCH-FORM is evaluated.
386 However, if PRE-MATCH-FORM returns a position greater than the position after
387 PRE-MATCH-FORM is evaluated, that position is used as the limit of the search.
388 It is generally a bad idea to return a position greater than the end of the
389 line, i.e., cause the MATCHER search to span lines.
391 These regular expressions should not match text which spans lines. While
392 \\[font-lock-fontify-buffer] handles multi-line patterns correctly, updating
393 when you edit the buffer does not, since it considers text one line at a time.
395 This variable is set by major modes via the variable `font-lock-defaults'.
396 Be careful when composing regexps for this list; a poorly written pattern can
397 dramatically slow things down!")
399 ;; This variable is used by mode packages that support Font Lock mode by
400 ;; defining their own keywords to use for `font-lock-keywords'. (The mode
401 ;; command should make it buffer-local and set it to provide the set up.)
402 (defvar font-lock-defaults nil
403 "Defaults for Font Lock mode specified by the major mode.
404 Defaults should be of the form:
406 (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN ...)
408 KEYWORDS may be a symbol (a variable or function whose value is the keywords to
409 use for fontification) or a list of symbols. If KEYWORDS-ONLY is non-nil,
410 syntactic fontification (strings and comments) is not performed.
411 If CASE-FOLD is non-nil, the case of the keywords is ignored when fontifying.
412 If SYNTAX-ALIST is non-nil, it should be a list of cons pairs of the form
413 \(CHAR-OR-STRING . STRING) used to set the local Font Lock syntax table, for
414 keyword and syntactic fontification (see `modify-syntax-entry').
416 If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
417 backwards outside any enclosing syntactic block, for syntactic fontification.
418 Typical values are `beginning-of-line' (i.e., the start of the line is known to
419 be outside a syntactic block), or `beginning-of-defun' for programming modes or
420 `backward-paragraph' for textual modes (i.e., the mode-dependent function is
421 known to move outside a syntactic block). If nil, the beginning of the buffer
422 is used as a position outside of a syntactic block, in the worst case.
424 These item elements are used by Font Lock mode to set the variables
425 `font-lock-keywords', `font-lock-keywords-only',
426 `font-lock-keywords-case-fold-search', `font-lock-syntax-table' and
427 `font-lock-beginning-of-syntax-function', respectively.
429 Further item elements are alists of the form (VARIABLE . VALUE) and are in no
430 particular order. Each VARIABLE is made buffer-local before set to VALUE.
432 Currently, appropriate variables include `font-lock-mark-block-function'.
433 If this is non-nil, it should be a function with no args used to mark any
434 enclosing block of text, for fontification via \\[font-lock-fontify-block].
435 Typical values are `mark-defun' for programming modes or `mark-paragraph' for
436 textual modes (i.e., the mode-dependent function is known to put point and mark
437 around a text block relevant to that mode).
439 Other variables include that for syntactic keyword fontification,
440 `font-lock-syntactic-keywords'
441 and those for buffer-specialised fontification functions,
442 `font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function',
443 `font-lock-fontify-region-function', `font-lock-unfontify-region-function',
444 `font-lock-inhibit-thing-lock' and `font-lock-maximum-size'.")
446 ;; This variable is used where font-lock.el itself supplies the keywords.
447 (defvar font-lock-defaults-alist
448 (let (;; We use `beginning-of-defun', rather than nil, for SYNTAX-BEGIN.
449 ;; Thus the calculation of the cache is usually faster but not
450 ;; infallible, so we risk mis-fontification. sm.
452 '((c-font-lock-keywords c-font-lock-keywords-1
453 c-font-lock-keywords-2 c-font-lock-keywords-3
)
454 nil nil
((?_ .
"w")) beginning-of-defun
455 (font-lock-mark-block-function . mark-defun
)))
457 '((c++-font-lock-keywords c
++-font-lock-keywords-1
458 c
++-font-lock-keywords-2 c
++-font-lock-keywords-3
)
459 nil nil
((?_ .
"w")) beginning-of-defun
460 (font-lock-mark-block-function . mark-defun
)))
462 '((objc-font-lock-keywords objc-font-lock-keywords-1
463 objc-font-lock-keywords-2 objc-font-lock-keywords-3
)
464 nil nil
((?_ .
"w") (?$ .
"w")) nil
465 (font-lock-mark-block-function . mark-defun
)))
467 '((java-font-lock-keywords java-font-lock-keywords-1
468 java-font-lock-keywords-2 java-font-lock-keywords-3
)
469 nil nil
((?_ .
"w") (?$ .
"w")) nil
470 (font-lock-mark-block-function . mark-defun
)))
472 '((lisp-font-lock-keywords
473 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2
)
474 nil nil
(("+-*/.<>=!?$%_&~^:" .
"w")) beginning-of-defun
475 (font-lock-mark-block-function . mark-defun
)))
476 ;; For TeX modes we could use `backward-paragraph' for the same reason.
477 ;; But we don't, because paragraph breaks are arguably likely enough to
478 ;; occur within a genuine syntactic block to make it too risky.
479 ;; However, we do specify a MARK-BLOCK function as that cannot result
480 ;; in a mis-fontification even if it might not fontify enough. sm.
482 '((tex-font-lock-keywords
483 tex-font-lock-keywords-1 tex-font-lock-keywords-2
)
484 nil nil
((?$ .
"\"")) nil
485 (font-lock-mark-block-function . mark-paragraph
)))
488 (cons 'c-mode c-mode-defaults
)
489 (cons 'c
++-mode c
++-mode-defaults
)
490 (cons 'objc-mode objc-mode-defaults
)
491 (cons 'java-mode java-mode-defaults
)
492 (cons 'emacs-lisp-mode lisp-mode-defaults
)
493 (cons 'latex-mode tex-mode-defaults
)
494 (cons 'lisp-mode lisp-mode-defaults
)
495 (cons 'lisp-interaction-mode lisp-mode-defaults
)
496 (cons 'plain-tex-mode tex-mode-defaults
)
497 (cons 'slitex-mode tex-mode-defaults
)
498 (cons 'tex-mode tex-mode-defaults
)))
499 "Alist of fall-back Font Lock defaults for major modes.
500 Each item should be a list of the form:
502 (MAJOR-MODE . FONT-LOCK-DEFAULTS)
504 where MAJOR-MODE is a symbol and FONT-LOCK-DEFAULTS is a list of default
505 settings. See the variable `font-lock-defaults', which takes precedence.")
507 (defvar font-lock-keywords-alist nil
508 "*Alist of `font-lock-keywords' local to a `major-mode'.
509 This is normally set via `font-lock-add-keywords' and
510 `font-lock-remove-keywords'.")
512 (defvar font-lock-removed-keywords-alist nil
513 "*Alist of `font-lock-keywords' removed from `major-mode'.
514 This is normally set via `font-lock-add-keywords' and
515 `font-lock-remove-keywords'.")
517 (defvar font-lock-keywords-only nil
518 "*Non-nil means Font Lock should not fontify comments or strings.
519 This is normally set via `font-lock-defaults'.")
521 (defvar font-lock-keywords-case-fold-search nil
522 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
523 This is normally set via `font-lock-defaults'.")
525 (defvar font-lock-syntactic-keywords nil
526 "A list of the syntactic keywords to highlight.
527 Can be the list or the name of a function or variable whose value is the list.
528 See `font-lock-keywords' for a description of the form of this list;
529 the differences are listed below. MATCH-HIGHLIGHT should be of the form:
531 (MATCH SYNTAX OVERRIDE LAXMATCH)
533 where SYNTAX can be of the form (SYNTAX-CODE . MATCHING-CHAR), the name of a
534 syntax table, or an expression whose value is such a form or a syntax table.
535 OVERRIDE cannot be `prepend' or `append'.
537 For example, an element of the form highlights syntactically:
539 (\"\\\\$\\\\(#\\\\)\" 1 (1 . nil))
541 a hash character when following a dollar character, with a SYNTAX-CODE of
542 1 (meaning punctuation syntax). Assuming that the buffer syntax table does
543 specify hash characters to have comment start syntax, the element will only
544 highlight hash characters that do not follow dollar characters as comments
547 (\"\\\\('\\\\).\\\\('\\\\)\"
551 both single quotes which surround a single character, with a SYNTAX-CODE of
552 7 (meaning string quote syntax) and a MATCHING-CHAR of a single quote (meaning
553 a single quote matches a single quote). Assuming that the buffer syntax table
554 does not specify single quotes to have quote syntax, the element will only
555 highlight single quotes of the form 'c' as strings syntactically.
556 Other forms, such as foo'bar or 'fubar', will not be highlighted as strings.
558 This is normally set via `font-lock-defaults'.")
560 (defvar font-lock-syntax-table nil
561 "Non-nil means use this syntax table for fontifying.
562 If this is nil, the major mode's syntax table is used.
563 This is normally set via `font-lock-defaults'.")
565 ;; If this is nil, we only use the beginning of the buffer if we can't use
566 ;; `font-lock-cache-position' and `font-lock-cache-state'.
567 (defvar font-lock-beginning-of-syntax-function nil
568 "*Non-nil means use this function to move back outside of a syntactic block.
569 When called with no args it should leave point at the beginning of any
570 enclosing syntactic block.
571 If this is nil, the beginning of the buffer is used (in the worst case).
572 This is normally set via `font-lock-defaults'.")
574 (defvar font-lock-mark-block-function nil
575 "*Non-nil means use this function to mark a block of text.
576 When called with no args it should leave point at the beginning of any
577 enclosing textual block and mark at the end.
578 This is normally set via `font-lock-defaults'.")
580 (defvar font-lock-fontify-buffer-function
'font-lock-default-fontify-buffer
581 "Function to use for fontifying the buffer.
582 This is normally set via `font-lock-defaults'.")
584 (defvar font-lock-unfontify-buffer-function
'font-lock-default-unfontify-buffer
585 "Function to use for unfontifying the buffer.
586 This is used when turning off Font Lock mode.
587 This is normally set via `font-lock-defaults'.")
589 (defvar font-lock-fontify-region-function
'font-lock-default-fontify-region
590 "Function to use for fontifying a region.
591 It should take two args, the beginning and end of the region, and an optional
592 third arg VERBOSE. If non-nil, the function should print status messages.
593 This is normally set via `font-lock-defaults'.")
595 (defvar font-lock-unfontify-region-function
'font-lock-default-unfontify-region
596 "Function to use for unfontifying a region.
597 It should take two args, the beginning and end of the region.
598 This is normally set via `font-lock-defaults'.")
600 (defvar font-lock-inhibit-thing-lock nil
601 "List of Font Lock mode related modes that should not be turned on.
602 Currently, valid mode names are `fast-lock-mode', `jit-lock-mode' and
603 `lazy-lock-mode'. This is normally set via `font-lock-defaults'.")
605 (defvar font-lock-multiline
'undecided
606 "Whether font-lock should cater to multiline keywords.
607 If nil, don't try to handle multiline patterns.
608 If t, always handle multiline patterns.
609 If `undecided', don't try to handle multiline patterns until you see one.
610 Major/minor modes can set this variable if they know which option applies.")
612 (defvar font-lock-mode nil
) ; Whether we are turned on/modeline.
613 (defvar font-lock-fontified nil
) ; Whether we have fontified the buffer.
616 (defvar font-lock-mode-hook nil
617 "Function or functions to run on entry to Font Lock mode.")
623 ;; We don't do this at the top-level as we only use non-autoloaded macros.
626 ;; Borrowed from lazy-lock.el.
627 ;; We use this to preserve or protect things when modifying text properties.
628 (defmacro save-buffer-state
(varlist &rest body
)
629 "Bind variables according to VARLIST and eval BODY restoring buffer state."
630 (` (let* ((,@ (append varlist
631 '((modified (buffer-modified-p)) (buffer-undo-list t
)
632 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
633 before-change-functions after-change-functions
634 deactivate-mark buffer-file-name buffer-file-truename
))))
636 (when (and (not modified
) (buffer-modified-p))
637 (set-buffer-modified-p nil
)))))
638 (put 'save-buffer-state
'lisp-indent-function
1)
640 ;; Shut up the byte compiler.
641 (defvar global-font-lock-mode
) ; Now a defcustom.
642 (defvar font-lock-face-attributes
) ; Obsolete but respected if set.
643 (defvar font-lock-string-face
) ; Used in syntactic fontification.
644 (defvar font-lock-comment-face
))
647 (defun font-lock-mode (&optional arg
)
648 "Toggle Font Lock mode.
649 With arg, turn Font Lock mode on if and only if arg is positive.
651 When Font Lock mode is enabled, text is fontified as you type it:
653 - Comments are displayed in `font-lock-comment-face';
654 - Strings are displayed in `font-lock-string-face';
655 - Certain other expressions are displayed in other faces according to the
656 value of the variable `font-lock-keywords'.
658 You can enable Font Lock mode in any major mode automatically by turning on in
659 the major mode's hook. For example, put in your ~/.emacs:
661 (add-hook 'c-mode-hook 'turn-on-font-lock)
663 Alternatively, you can use Global Font Lock mode to automagically turn on Font
664 Lock mode in buffers whose major mode supports it and whose major mode is one
665 of `font-lock-global-modes'. For example, put in your ~/.emacs:
667 (global-font-lock-mode t)
669 There are a number of support modes that may be used to speed up Font Lock mode
670 in various ways, specified via the variable `font-lock-support-mode'. Where
671 major modes support different levels of fontification, you can use the variable
672 `font-lock-maximum-decoration' to specify which level you generally prefer.
673 When you turn Font Lock mode on/off the buffer is fontified/defontified, though
674 fontification occurs only if the buffer is less than `font-lock-maximum-size'.
676 For example, to specify that Font Lock mode use use Lazy Lock mode as a support
677 mode and use maximum levels of fontification, put in your ~/.emacs:
679 (setq font-lock-support-mode 'lazy-lock-mode)
680 (setq font-lock-maximum-decoration t)
682 To add your own highlighting for some major mode, and modify the highlighting
683 selected automatically via the variable `font-lock-maximum-decoration', you can
684 use `font-lock-add-keywords'.
686 To fontify a buffer, without turning on Font Lock mode and regardless of buffer
687 size, you can use \\[font-lock-fontify-buffer].
689 To fontify a block (the function or paragraph containing point, or a number of
690 lines around point), perhaps because modification on the current line caused
691 syntactic change on other lines, you can use \\[font-lock-fontify-block].
693 See the variable `font-lock-defaults-alist' for the Font Lock mode default
694 settings. You can set your own default settings for some mode, by setting a
695 buffer local value for `font-lock-defaults', via its mode hook."
697 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
698 ;; batch job) or if the buffer is invisible (the name starts with a space).
699 (let ((on-p (and (not noninteractive
)
700 (not (eq (aref (buffer-name) 0) ?\
))
702 (> (prefix-numeric-value arg
) 0)
703 (not font-lock-mode
)))))
704 (set (make-local-variable 'font-lock-mode
) on-p
)
705 ;; Turn on Font Lock mode.
707 (make-local-hook 'after-change-functions
)
708 (add-hook 'after-change-functions
'font-lock-after-change-function nil t
)
709 (font-lock-set-defaults)
710 (font-lock-turn-on-thing-lock)
711 (run-hooks 'font-lock-mode-hook
)
712 ;; Fontify the buffer if we have to.
713 (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size
)))
714 (cond (font-lock-fontified
716 ((or (null max-size
) (> max-size
(buffer-size)))
717 (font-lock-fontify-buffer))
719 (message "Fontifying %s...buffer too big" (buffer-name))))))
720 ;; Turn off Font Lock mode.
722 (remove-hook 'after-change-functions
'font-lock-after-change-function t
)
723 (font-lock-unfontify-buffer)
724 (font-lock-turn-off-thing-lock)
725 (font-lock-unset-defaults))
726 (force-mode-line-update)))
729 (defun turn-on-font-lock ()
730 "Turn on Font Lock mode conditionally.
731 Turn on only if the terminal can display it."
732 (when (and (not font-lock-mode
) (or window-system
(tty-display-color-p)))
736 (defun font-lock-add-keywords (mode keywords
&optional append
)
737 "Add highlighting KEYWORDS for MODE.
738 MODE should be a symbol, the major mode command name, such as `c-mode'
739 or nil. If nil, highlighting keywords are added for the current buffer.
740 KEYWORDS should be a list; see the variable `font-lock-keywords'.
741 By default they are added at the beginning of the current highlighting list.
742 If optional argument APPEND is `set', they are used to replace the current
743 highlighting list. If APPEND is any other non-nil value, they are added at the
744 end of the current highlighting list.
748 (font-lock-add-keywords 'c-mode
749 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
750 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . font-lock-keyword-face)))
752 adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
753 comments, and to fontify `and', `or' and `not' words as keywords.
755 Note that some modes have specialised support for additional patterns, e.g.,
756 see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
757 `objc-font-lock-extra-types' and `java-font-lock-extra-types'."
759 ;; If MODE is non-nil, add the KEYWORDS and APPEND spec to
760 ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
761 (let ((spec (cons keywords append
)) cell
)
762 (if (setq cell
(assq mode font-lock-keywords-alist
))
764 (setcdr cell
(list spec
))
765 (setcdr cell
(append (cdr cell
) (list spec
))))
766 (push (list mode spec
) font-lock-keywords-alist
)))
767 ;; Make sure that `font-lock-removed-keywords-alist' does not
768 ;; contain the new keywords.
769 (font-lock-update-removed-keyword-alist mode keywords append
))
771 ;; Otherwise if Font Lock mode is on, set or add the keywords now.
773 (setq font-lock-keywords keywords
)
774 (font-lock-remove-keywords mode keywords
)
775 (let ((old (if (eq (car-safe font-lock-keywords
) t
)
776 (cdr font-lock-keywords
)
777 font-lock-keywords
)))
778 (setq font-lock-keywords
(if append
779 (append old keywords
)
780 (append keywords old
))))))))
782 (defun font-lock-update-removed-keyword-alist (major-mode keywords append
)
783 ;; Update `font-lock-removed-keywords-alist' when adding new
784 ;; KEYWORDS to MAJOR-MODE.
786 ;; When font-lock is enabled first all keywords in the list
787 ;; `font-lock-keywords-alist' are added, then all keywords in the
788 ;; list `font-lock-removed-keywords-alist' are removed. If a
789 ;; keyword was once added, removed, and then added again it must be
790 ;; removed from the removed-keywords list. Otherwise the second add
791 ;; will not take effect.
792 (let ((cell (assq major-mode font-lock-removed-keywords-alist
)))
795 ;; A new set of keywords is defined. Forget all about
796 ;; our old keywords that should be removed.
797 (setq font-lock-removed-keywords-alist
798 (delq cell font-lock-removed-keywords-alist
))
799 ;; Delete all previously removed keywords.
800 (dolist (kword keywords
)
801 (setcdr cell
(delete kword
(cdr cell
))))
802 ;; Delete the major-mode cell if empty.
803 (if (null (cdr cell
))
804 (setq font-lock-removed-keywords-alist
805 (delq cell font-lock-removed-keywords-alist
)))))))
807 ;; Written by Anders Lindgren <andersl@andersl.com>.
810 ;; (I) The keywords are removed from a major mode.
811 ;; In this case the keyword could be local (i.e. added earlier by
812 ;; `font-lock-add-keywords'), global, or both.
814 ;; (a) In the local case we remove the keywords from the variable
815 ;; `font-lock-keywords-alist'.
817 ;; (b) The actual global keywords are not known at this time.
818 ;; All keywords are added to `font-lock-removed-keywords-alist',
819 ;; when font-lock is enabled those keywords are removed.
821 ;; Note that added keywords are taken out of the list of removed
822 ;; keywords. This ensure correct operation when the same keyword
823 ;; is added and removed several times.
825 ;; (II) The keywords are removed from the current buffer.
827 (defun font-lock-remove-keywords (major-mode keywords
)
828 "Remove highlighting KEYWORDS for MAJOR-MODE.
830 MAJOR-MODE should be a symbol, the major mode command name, such as `c-mode'
831 or nil. If nil, highlighting keywords are removed for the current buffer."
832 (dolist (keyword keywords
)
833 ;; Remove one keyword at the time.
835 (let ((top-cell (assq major-mode font-lock-keywords-alist
)))
836 ;; If MAJOR-MODE is non-nil, remove the KEYWORD from
837 ;; `font-lock-keywords-alist'.
839 (dolist (keyword-list-append-pair (cdr top-cell
))
840 ;; `keywords-list-append-pair' is a cons with a list of
841 ;; keywords in the car top-cell and the original append
842 ;; argument in the cdr top-cell.
843 (setcar keyword-list-append-pair
844 (delete keyword
(car keyword-list-append-pair
))))
845 ;; Remove keyword list/append pair when the keyword list
846 ;; is empty and append doesn't specify `set'. (If it
847 ;; should be deleted then previously deleted keywords
848 ;; would appear again.)
849 (let ((cell top-cell
))
851 (if (and (null (car (car (cdr cell
))))
852 (not (eq (cdr (car (cdr cell
))) 'set
)))
853 (setcdr cell
(cdr (cdr cell
)))
854 (setq cell
(cdr cell
)))))
855 ;; Final cleanup, remove major mode cell if last keyword
857 (if (null (cdr top-cell
))
858 (setq font-lock-keywords-alist
859 (delq top-cell font-lock-keywords-alist
))))
860 ;; Remember the keyword in case it is not local.
861 (let ((cell (assq major-mode font-lock-removed-keywords-alist
)))
863 (unless (member keyword
(cdr cell
))
864 (nconc cell
(list keyword
)))
865 (push (cons major-mode
(list keyword
))
866 font-lock-removed-keywords-alist
)))))
868 ;; Otherwise if Font Lock mode is on, remove it immediately.
869 (setq font-lock-keywords
(delete keyword font-lock-keywords
))
870 ;; The keywords might be compiled.
871 (setq font-lock-keywords
872 (delete (font-lock-compile-keyword keyword
)
873 font-lock-keywords
))))))
875 ;;; Global Font Lock mode.
877 ;; A few people have hassled in the past for a way to make it easier to turn on
878 ;; Font Lock mode, without the user needing to know for which modes s/he has to
879 ;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always
880 ;; balked at that way, as I see it as just re-moulding the same problem in
881 ;; another form. That is; some person would still have to keep track of which
882 ;; modes (which may not even be distributed with Emacs) support Font Lock mode.
883 ;; The list would always be out of date. And that person might have to be me.
887 ;; In a previous discussion the following hack came to mind. It is a gross
888 ;; hack, but it generally works. We use the convention that major modes start
889 ;; by calling the function `kill-all-local-variables', which in turn runs
890 ;; functions on the hook variable `change-major-mode-hook'. We attach our
891 ;; function `font-lock-change-major-mode' to that hook. Of course, when this
892 ;; hook is run, the major mode is in the process of being changed and we do not
893 ;; know what the final major mode will be. So, `font-lock-change-major-mode'
894 ;; only (a) notes the name of the current buffer, and (b) adds our function
895 ;; `turn-on-font-lock-if-enabled' to the hook variables `find-file-hooks' and
896 ;; `post-command-hook' (for buffers that are not visiting files). By the time
897 ;; the functions on the first of these hooks to be run are run, the new major
898 ;; mode is assumed to be in place. This way we get a Font Lock function run
899 ;; when a major mode is turned on, without knowing major modes or their hooks.
901 ;; Naturally this requires that (a) major modes run `kill-all-local-variables',
902 ;; as they are supposed to do, and (b) the major mode is in place after the
903 ;; file is visited or the command that ran `kill-all-local-variables' has
904 ;; finished, whichever the sooner. Arguably, any major mode that does not
905 ;; follow the convension (a) is broken, and I can't think of any reason why (b)
906 ;; would not be met (except `gnudoit' on non-files). However, it is not clean.
908 ;; Probably the cleanest solution is to have each major mode function run some
909 ;; hook, e.g., `major-mode-hook', but maybe implementing that change is
910 ;; impractical. I am personally against making `setq' a macro or be advised,
911 ;; or have a special function such as `set-major-mode', but maybe someone can
912 ;; come up with another solution?
916 ;; Although Global Font Lock mode is a pseudo-mode, I think that the user
917 ;; interface should conform to the usual Emacs convention for modes, i.e., a
918 ;; command to toggle the feature (`global-font-lock-mode') with a variable for
919 ;; finer control of the mode's behaviour (`font-lock-global-modes').
921 ;; The feature should not be enabled by loading font-lock.el, since other
922 ;; mechanisms for turning on Font Lock mode, such as M-x font-lock-mode RET or
923 ;; (add-hook 'c-mode-hook 'turn-on-font-lock), would cause Font Lock mode to be
924 ;; turned on everywhere. That would not be intuitive or informative because
925 ;; loading a file tells you nothing about the feature or how to control it. It
926 ;; would also be contrary to the Principle of Least Surprise. sm.
928 (defvar font-lock-buffers nil
) ; For remembering buffers.
931 (defun global-font-lock-mode (&optional arg message
)
932 "Toggle Global Font Lock mode.
933 With prefix ARG, turn Global Font Lock mode on if and only if ARG is positive.
934 Displays a message saying whether the mode is on or off if MESSAGE is non-nil.
935 Returns the new status of Global Font Lock mode (non-nil means on).
937 When Global Font Lock mode is enabled, Font Lock mode is automagically
938 turned on in a buffer if its major mode is one of `font-lock-global-modes'."
941 (> (prefix-numeric-value arg
) 0)
942 (not global-font-lock-mode
))))
944 (add-hook 'find-file-hooks
'turn-on-font-lock-if-enabled
)
945 (add-hook 'post-command-hook
'turn-on-font-lock-if-enabled
)
946 (setq font-lock-buffers
(buffer-list)))
948 (remove-hook 'find-file-hooks
'turn-on-font-lock-if-enabled
)
949 (mapcar (function (lambda (buffer)
950 (with-current-buffer buffer
955 (message "Global Font Lock mode %s." (if on-p
"enabled" "disabled")))
956 (setq global-font-lock-mode on-p
)))
958 ;; This variable was originally a `defvar' to keep track of
959 ;; whether Global Font Lock mode was turned on or not. As a `defcustom' with
960 ;; special `:set' and `:require' forms, we can provide custom mode control.
962 (defcustom global-font-lock-mode nil
963 "Toggle Global Font Lock mode.
964 When Global Font Lock mode is enabled, Font Lock mode is automagically
965 turned on in a buffer if its major mode is one of `font-lock-global-modes'.
966 Setting this variable directly does not take effect;
967 use either \\[customize] or the function `global-font-lock-mode'."
968 :set
(lambda (symbol value
)
969 (global-font-lock-mode (or value
0)))
970 :initialize
'custom-initialize-default
975 (defcustom font-lock-global-modes t
976 "*Modes for which Font Lock mode is automagically turned on.
977 Global Font Lock mode is controlled by the command `global-font-lock-mode'.
978 If nil, means no modes have Font Lock mode automatically turned on.
979 If t, all modes that support Font Lock mode have it automatically turned on.
980 If a list, it should be a list of `major-mode' symbol names for which Font Lock
981 mode should be automatically turned on. The sense of the list is negated if it
982 begins with `not'. For example:
984 means that Font Lock mode is turned on for buffers in C and C++ modes only."
985 :type
'(choice (const :tag
"none" nil
)
987 (set :menu-tag
"mode specific" :tag
"modes"
989 (const :tag
"Except" not
)
990 (repeat :inline t
(symbol :tag
"mode"))))
993 (defun font-lock-change-major-mode ()
994 ;; Turn off Font Lock mode if it's on.
997 ;; Gross hack warning: Delicate readers should avert eyes now.
998 ;; Something is running `kill-all-local-variables', which generally means the
999 ;; major mode is being changed. Run `turn-on-font-lock-if-enabled' after the
1000 ;; file is visited or the current command has finished.
1001 (when global-font-lock-mode
1002 (add-hook 'post-command-hook
'turn-on-font-lock-if-enabled
)
1003 (add-to-list 'font-lock-buffers
(current-buffer))))
1005 (defun turn-on-font-lock-if-enabled ()
1006 ;; Gross hack warning: Delicate readers should avert eyes now.
1007 ;; Turn on Font Lock mode if it's supported by the major mode and enabled by
1009 (remove-hook 'post-command-hook
'turn-on-font-lock-if-enabled
)
1010 (while font-lock-buffers
1011 (when (buffer-live-p (car font-lock-buffers
))
1013 (set-buffer (car font-lock-buffers
))
1014 (when (and (or font-lock-defaults
1015 (assq major-mode font-lock-defaults-alist
))
1016 (or (eq font-lock-global-modes t
)
1017 (if (eq (car-safe font-lock-global-modes
) 'not
)
1018 (not (memq major-mode
(cdr font-lock-global-modes
)))
1019 (memq major-mode font-lock-global-modes
))))
1021 (turn-on-font-lock)))))
1022 (setq font-lock-buffers
(cdr font-lock-buffers
))))
1024 (add-hook 'change-major-mode-hook
'font-lock-change-major-mode
)
1026 ;;; End of Global Font Lock mode.
1028 ;;; Font Lock Support mode.
1030 ;; This is the code used to interface font-lock.el with any of its add-on
1031 ;; packages, and provide the user interface. Packages that have their own
1032 ;; local buffer fontification functions (see below) may have to call
1033 ;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
1036 (defcustom font-lock-support-mode
'jit-lock-mode
1037 "*Support mode for Font Lock mode.
1038 Support modes speed up Font Lock mode by being choosy about when fontification
1039 occurs. Known support modes are Fast Lock mode (symbol `fast-lock-mode'),
1040 Lazy Lock mode (symbol `lazy-lock-mode'), and Just-in-time Lock mode (symbol
1041 `jit-lock-mode'. See those modes for more info.
1042 If nil, means support for Font Lock mode is never performed.
1043 If a symbol, use that support mode.
1044 If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
1045 where MAJOR-MODE is a symbol or t (meaning the default). For example:
1046 ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
1047 means that Fast Lock mode is used to support Font Lock mode for buffers in C or
1048 C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
1050 The value of this variable is used when Font Lock mode is turned on."
1051 :type
'(choice (const :tag
"none" nil
)
1052 (const :tag
"fast lock" fast-lock-mode
)
1053 (const :tag
"lazy lock" lazy-lock-mode
)
1054 (const :tag
"jit lock" jit-lock-mode
)
1055 (repeat :menu-tag
"mode specific" :tag
"mode specific"
1056 :value
((t . lazy-lock-mode
))
1057 (cons :tag
"Instance"
1059 (const :tag
"all" t
)
1060 (symbol :tag
"name"))
1061 (radio :tag
"Support"
1062 (const :tag
"none" nil
)
1063 (const :tag
"fast lock" fast-lock-mode
)
1064 (const :tag
"lazy lock" lazy-lock-mode
)
1065 (const :tag
"JIT lock" jit-lock-mode
)))
1069 (defvar fast-lock-mode nil
)
1070 (defvar lazy-lock-mode nil
)
1071 (defvar jit-lock-mode nil
)
1073 (defun font-lock-turn-on-thing-lock ()
1074 (let ((thing-mode (font-lock-value-in-major-mode font-lock-support-mode
)))
1075 (cond ((eq thing-mode
'fast-lock-mode
)
1077 ((eq thing-mode
'lazy-lock-mode
)
1079 ((eq thing-mode
'jit-lock-mode
)
1080 (jit-lock-mode t
)))))
1082 (defun font-lock-turn-off-thing-lock ()
1083 (cond (fast-lock-mode
1084 (fast-lock-mode nil
))
1086 (jit-lock-mode nil
))
1088 (lazy-lock-mode nil
))))
1090 (defun font-lock-after-fontify-buffer ()
1091 (cond (fast-lock-mode
1092 (fast-lock-after-fontify-buffer))
1094 (jit-lock-after-fontify-buffer))
1096 (lazy-lock-after-fontify-buffer))))
1098 (defun font-lock-after-unfontify-buffer ()
1099 (cond (fast-lock-mode
1100 (fast-lock-after-unfontify-buffer))
1102 (jit-lock-after-unfontify-buffer))
1104 (lazy-lock-after-unfontify-buffer))))
1106 ;;; End of Font Lock Support mode.
1108 ;;; Fontification functions.
1110 ;; Rather than the function, e.g., `font-lock-fontify-region' containing the
1111 ;; code to fontify a region, the function runs the function whose name is the
1112 ;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally,
1113 ;; the value of this variable is, e.g., `font-lock-default-fontify-region'
1114 ;; which does contain the code to fontify a region. However, the value of the
1115 ;; variable could be anything and thus, e.g., `font-lock-fontify-region' could
1116 ;; do anything. The indirection of the fontification functions gives major
1117 ;; modes the capability of modifying the way font-lock.el fontifies. Major
1118 ;; modes can modify the values of, e.g., `font-lock-fontify-region-function',
1119 ;; via the variable `font-lock-defaults'.
1121 ;; For example, Rmail mode sets the variable `font-lock-defaults' so that
1122 ;; font-lock.el uses its own function for buffer fontification. This function
1123 ;; makes fontification be on a message-by-message basis and so visiting an
1124 ;; RMAIL file is much faster. A clever implementation of the function might
1125 ;; fontify the headers differently than the message body. (It should, and
1126 ;; correspondingly for Mail mode, but I can't be bothered to do the work. Can
1127 ;; you?) This hints at a more interesting use...
1129 ;; Languages that contain text normally contained in different major modes
1130 ;; could define their own fontification functions that treat text differently
1131 ;; depending on its context. For example, Perl mode could arrange that here
1132 ;; docs are fontified differently than Perl code. Or Yacc mode could fontify
1133 ;; rules one way and C code another. Neat!
1135 ;; A further reason to use the fontification indirection feature is when the
1136 ;; default syntactual fontification, or the default fontification in general,
1137 ;; is not flexible enough for a particular major mode. For example, perhaps
1138 ;; comments are just too hairy for `font-lock-fontify-syntactically-region' to
1139 ;; cope with. You need to write your own version of that function, e.g.,
1140 ;; `hairy-fontify-syntactically-region', and make your own version of
1141 ;; `hairy-fontify-region' call that function before calling
1142 ;; `font-lock-fontify-keywords-region' for the normal regexp fontification
1143 ;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el
1144 ;; would call your region fontification function instead of its own. For
1145 ;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line
1146 ;; directives correctly and cleanly. (It is the same problem as fontifying
1147 ;; multi-line strings and comments; regexps are not appropriate for the job.)
1150 (defun font-lock-fontify-buffer ()
1151 "Fontify the current buffer the way the function `font-lock-mode' would."
1153 (let ((font-lock-verbose (or font-lock-verbose
(interactive-p))))
1154 (funcall font-lock-fontify-buffer-function
)))
1156 (defun font-lock-unfontify-buffer ()
1157 (funcall font-lock-unfontify-buffer-function
))
1159 (defun font-lock-fontify-region (beg end
&optional loudly
)
1160 (funcall font-lock-fontify-region-function beg end loudly
))
1162 (defun font-lock-unfontify-region (beg end
)
1163 (funcall font-lock-unfontify-region-function beg end
))
1165 (defun font-lock-default-fontify-buffer ()
1166 (let ((verbose (if (numberp font-lock-verbose
)
1167 (> (buffer-size) font-lock-verbose
)
1168 font-lock-verbose
)))
1171 (format "Fontifying %s..." (buffer-name)))
1172 ;; Make sure we have the right `font-lock-keywords' etc.
1173 (unless font-lock-mode
1174 (font-lock-set-defaults))
1175 ;; Make sure we fontify etc. in the whole buffer.
1181 (font-lock-fontify-region (point-min) (point-max) verbose
)
1182 (font-lock-after-fontify-buffer)
1183 (setq font-lock-fontified t
)))
1184 ;; We don't restore the old fontification, so it's best to unfontify.
1185 (quit (font-lock-unfontify-buffer))))
1186 ;; Make sure we undo `font-lock-keywords' etc.
1187 (unless font-lock-mode
1188 (font-lock-unset-defaults)))))
1190 (defun font-lock-default-unfontify-buffer ()
1191 ;; Make sure we unfontify etc. in the whole buffer.
1194 (font-lock-unfontify-region (point-min) (point-max))
1195 (font-lock-after-unfontify-buffer)
1196 (setq font-lock-fontified nil
)))
1198 (defun font-lock-default-fontify-region (beg end loudly
)
1200 ((parse-sexp-lookup-properties font-lock-syntactic-keywords
)
1201 (old-syntax-table (syntax-table)))
1205 ;; Use the fontification syntax table, if any.
1206 (when font-lock-syntax-table
1207 (set-syntax-table font-lock-syntax-table
))
1208 ;; check to see if we should expand the beg/end area for
1209 ;; proper multiline matches
1210 (setq beg
(if (get-text-property beg
'font-lock-multiline
)
1211 ;; if the text-property is non-nil, (1+ beg)
1212 ;; is valid. We need to use (1+ beg) for the
1213 ;; case where (get-text-property (1- beg)) is nil
1214 ;; in which case we want to keep BEG but
1215 ;; previous-single-property-change will return
1216 ;; the previous change (if any) rather than
1218 (or (previous-single-property-change
1219 (1+ beg
) 'font-lock-multiline
)
1222 (setq end
(or (text-property-any end
(point-max)
1223 'font-lock-multiline nil
)
1225 ;; Now do the fontification.
1226 (font-lock-unfontify-region beg end
)
1227 (when font-lock-syntactic-keywords
1228 (font-lock-fontify-syntactic-keywords-region beg end
))
1229 (unless font-lock-keywords-only
1230 (font-lock-fontify-syntactically-region beg end loudly
))
1231 (font-lock-fontify-keywords-region beg end loudly
))
1233 (set-syntax-table old-syntax-table
))))
1235 ;; The following must be rethought, since keywords can override fontification.
1236 ; ;; Now scan for keywords, but not if we are inside a comment now.
1237 ; (or (and (not font-lock-keywords-only)
1238 ; (let ((state (parse-partial-sexp beg end nil nil
1239 ; font-lock-cache-state)))
1240 ; (or (nth 4 state) (nth 7 state))))
1241 ; (font-lock-fontify-keywords-region beg end))
1243 (defun font-lock-default-unfontify-region (beg end
)
1244 (save-buffer-state nil
1245 (remove-text-properties beg end
1246 (if font-lock-syntactic-keywords
1247 '(face nil syntax-table nil font-lock-multiline nil
)
1248 '(face nil font-lock-multiline nil
)))))
1250 ;; Called when any modification is made to buffer text.
1251 (defun font-lock-after-change-function (beg end old-len
)
1252 (let ((inhibit-point-motion-hooks t
))
1255 ;; Rescan between start of lines enclosing the region.
1256 (font-lock-fontify-region
1257 (progn (goto-char beg
) (beginning-of-line) (point))
1258 (progn (goto-char end
) (forward-line 1) (point)))))))
1260 (defun font-lock-fontify-block (&optional arg
)
1261 "Fontify some lines the way `font-lock-fontify-buffer' would.
1262 The lines could be a function or paragraph, or a specified number of lines.
1263 If ARG is given, fontify that many lines before and after point, or 16 lines if
1264 no ARG is given and `font-lock-mark-block-function' is nil.
1265 If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
1266 delimit the region to fontify."
1268 (let ((inhibit-point-motion-hooks t
) font-lock-beginning-of-syntax-function
1270 ;; Make sure we have the right `font-lock-keywords' etc.
1271 (if (not font-lock-mode
) (font-lock-set-defaults))
1274 (condition-case error-data
1275 (if (or arg
(not font-lock-mark-block-function
))
1276 (let ((lines (if arg
(prefix-numeric-value arg
) 16)))
1277 (font-lock-fontify-region
1278 (save-excursion (forward-line (- lines
)) (point))
1279 (save-excursion (forward-line lines
) (point))))
1280 (funcall font-lock-mark-block-function
)
1281 (font-lock-fontify-region (point) (mark)))
1282 ((error quit
) (message "Fontifying block...%s" error-data
)))))))
1284 (define-key facemenu-keymap
"\M-g" 'font-lock-fontify-block
)
1286 ;;; End of Fontification functions.
1288 ;;; Additional text property functions.
1290 ;; The following text property functions should be builtins. This means they
1291 ;; should be written in C and put with all the other text property functions.
1292 ;; In the meantime, those that are used by font-lock.el are defined in Lisp
1293 ;; below and given a `font-lock-' prefix. Those that are not used are defined
1294 ;; in Lisp below and commented out. sm.
1296 (defun font-lock-prepend-text-property (start end prop value
&optional object
)
1297 "Prepend to one property of the text from START to END.
1298 Arguments PROP and VALUE specify the property and value to prepend to the value
1299 already in place. The resulting property values are always lists.
1300 Optional argument OBJECT is the string or buffer containing the text."
1301 (let ((val (if (listp value
) value
(list value
))) next prev
)
1302 (while (/= start end
)
1303 (setq next
(next-single-property-change start prop object end
)
1304 prev
(get-text-property start prop object
))
1305 (put-text-property start next prop
1306 (append val
(if (listp prev
) prev
(list prev
)))
1308 (setq start next
))))
1310 (defun font-lock-append-text-property (start end prop value
&optional object
)
1311 "Append to one property of the text from START to END.
1312 Arguments PROP and VALUE specify the property and value to append to the value
1313 already in place. The resulting property values are always lists.
1314 Optional argument OBJECT is the string or buffer containing the text."
1315 (let ((val (if (listp value
) value
(list value
))) next prev
)
1316 (while (/= start end
)
1317 (setq next
(next-single-property-change start prop object end
)
1318 prev
(get-text-property start prop object
))
1319 (put-text-property start next prop
1320 (append (if (listp prev
) prev
(list prev
)) val
)
1322 (setq start next
))))
1324 (defun font-lock-fillin-text-property (start end prop value
&optional object
)
1325 "Fill in one property of the text from START to END.
1326 Arguments PROP and VALUE specify the property and value to put where none are
1327 already in place. Therefore existing property values are not overwritten.
1328 Optional argument OBJECT is the string or buffer containing the text."
1329 (let ((start (text-property-any start end prop nil object
)) next
)
1331 (setq next
(next-single-property-change start prop object end
))
1332 (put-text-property start next prop value object
)
1333 (setq start
(text-property-any next end prop nil object
)))))
1335 ;; For completeness: this is to `remove-text-properties' as `put-text-property'
1336 ;; is to `add-text-properties', etc.
1337 ;(defun remove-text-property (start end property &optional object)
1338 ; "Remove a property from text from START to END.
1339 ;Argument PROPERTY is the property to remove.
1340 ;Optional argument OBJECT is the string or buffer containing the text.
1341 ;Return t if the property was actually removed, nil otherwise."
1342 ; (remove-text-properties start end (list property) object))
1344 ;; For consistency: maybe this should be called `remove-single-property' like
1345 ;; `next-single-property-change' (not `next-single-text-property-change'), etc.
1346 ;(defun remove-single-text-property (start end prop value &optional object)
1347 ; "Remove a specific property value from text from START to END.
1348 ;Arguments PROP and VALUE specify the property and value to remove. The
1349 ;resulting property values are not equal to VALUE nor lists containing VALUE.
1350 ;Optional argument OBJECT is the string or buffer containing the text."
1351 ; (let ((start (text-property-not-all start end prop nil object)) next prev)
1353 ; (setq next (next-single-property-change start prop object end)
1354 ; prev (get-text-property start prop object))
1355 ; (cond ((and (symbolp prev) (eq value prev))
1356 ; (remove-text-property start next prop object))
1357 ; ((and (listp prev) (memq value prev))
1358 ; (let ((new (delq value prev)))
1360 ; (remove-text-property start next prop object))
1361 ; ((= (length new) 1)
1362 ; (put-text-property start next prop (car new) object))
1364 ; (put-text-property start next prop new object))))))
1365 ; (setq start (text-property-not-all next end prop nil object)))))
1367 ;;; End of Additional text property functions.
1369 ;;; Syntactic regexp fontification functions.
1371 ;; These syntactic keyword pass functions are identical to those keyword pass
1372 ;; functions below, with the following exceptions; (a) they operate on
1373 ;; `font-lock-syntactic-keywords' of course, (b) they are all `defun' as speed
1374 ;; is less of an issue, (c) eval of property value does not occur JIT as speed
1375 ;; is less of an issue, (d) OVERRIDE cannot be `prepend' or `append' as it
1376 ;; makes no sense for `syntax-table' property values, (e) they do not do it
1377 ;; LOUDLY as it is not likely to be intensive.
1379 (defun font-lock-apply-syntactic-highlight (highlight)
1380 "Apply HIGHLIGHT following a match.
1381 HIGHLIGHT should be of the form MATCH-HIGHLIGHT,
1382 see `font-lock-syntactic-keywords'."
1383 (let* ((match (nth 0 highlight
))
1384 (start (match-beginning match
)) (end (match-end match
))
1385 (value (nth 1 highlight
))
1386 (override (nth 2 highlight
)))
1387 (unless (numberp (car-safe value
))
1388 (setq value
(eval value
)))
1390 ;; No match but we might not signal an error.
1391 (or (nth 3 highlight
)
1392 (error "No match %d in highlight %S" match highlight
)))
1394 ;; Cannot override existing fontification.
1395 (or (text-property-not-all start end
'syntax-table nil
)
1396 (put-text-property start end
'syntax-table value
)))
1398 ;; Override existing fontification.
1399 (put-text-property start end
'syntax-table value
))
1400 ((eq override
'keep
)
1401 ;; Keep existing fontification.
1402 (font-lock-fillin-text-property start end
'syntax-table value
)))))
1404 (defun font-lock-fontify-syntactic-anchored-keywords (keywords limit
)
1405 "Fontify according to KEYWORDS until LIMIT.
1406 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1407 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1408 (let ((matcher (nth 0 keywords
)) (lowdarks (nthcdr 3 keywords
)) highlights
1409 ;; Evaluate PRE-MATCH-FORM.
1410 (pre-match-value (eval (nth 1 keywords
))))
1411 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1412 (if (and (numberp pre-match-value
) (> pre-match-value
(point)))
1413 (setq limit pre-match-value
)
1414 (save-excursion (end-of-line) (setq limit
(point))))
1416 ;; Find an occurrence of `matcher' before `limit'.
1417 (while (if (stringp matcher
)
1418 (re-search-forward matcher limit t
)
1419 (funcall matcher limit
))
1420 ;; Apply each highlight to this instance of `matcher'.
1421 (setq highlights lowdarks
)
1423 (font-lock-apply-syntactic-highlight (car highlights
))
1424 (setq highlights
(cdr highlights
)))))
1425 ;; Evaluate POST-MATCH-FORM.
1426 (eval (nth 2 keywords
))))
1428 (defun font-lock-fontify-syntactic-keywords-region (start end
)
1429 "Fontify according to `font-lock-syntactic-keywords' between START and END.
1430 START should be at the beginning of a line."
1431 ;; If `font-lock-syntactic-keywords' is a symbol, get the real keywords.
1432 (when (symbolp font-lock-syntactic-keywords
)
1433 (setq font-lock-syntactic-keywords
(font-lock-eval-keywords
1434 font-lock-syntactic-keywords
)))
1435 ;; If `font-lock-syntactic-keywords' is not compiled, compile it.
1436 (unless (eq (car font-lock-syntactic-keywords
) t
)
1437 (setq font-lock-syntactic-keywords
(font-lock-compile-keywords
1438 font-lock-syntactic-keywords
)))
1439 ;; Get down to business.
1440 (let ((case-fold-search font-lock-keywords-case-fold-search
)
1441 (keywords (cdr font-lock-syntactic-keywords
))
1442 keyword matcher highlights
)
1444 ;; Find an occurrence of `matcher' from `start' to `end'.
1445 (setq keyword
(car keywords
) matcher
(car keyword
))
1447 (while (if (stringp matcher
)
1448 (re-search-forward matcher end t
)
1449 (funcall matcher end
))
1450 ;; Apply each highlight to this instance of `matcher', which may be
1451 ;; specific highlights or more keywords anchored to `matcher'.
1452 (setq highlights
(cdr keyword
))
1454 (if (numberp (car (car highlights
)))
1455 (font-lock-apply-syntactic-highlight (car highlights
))
1456 (font-lock-fontify-syntactic-anchored-keywords (car highlights
)
1458 (setq highlights
(cdr highlights
))))
1459 (setq keywords
(cdr keywords
)))))
1461 ;;; End of Syntactic regexp fontification functions.
1463 ;;; Syntactic fontification functions.
1465 ;; These record the parse state at a particular position, always the start of a
1466 ;; line. Used to make `font-lock-fontify-syntactically-region' faster.
1467 ;; Previously, `font-lock-cache-position' was just a buffer position. However,
1468 ;; under certain situations, this occasionally resulted in mis-fontification.
1469 ;; I think the "situations" were deletion with Lazy Lock mode's deferral. sm.
1470 (defvar font-lock-cache-state nil
)
1471 (defvar font-lock-cache-position nil
)
1473 (defun font-lock-fontify-syntactically-region (start end
&optional loudly
)
1474 "Put proper face on each string and comment between START and END.
1475 START should be at the beginning of a line."
1476 (let ((cache (marker-position font-lock-cache-position
))
1478 (if loudly
(message "Fontifying %s... (syntactically...)" (buffer-name)))
1481 ;; Find the state at the `beginning-of-line' before `start'.
1482 (if (eq start cache
)
1483 ;; Use the cache for the state of `start'.
1484 (setq state font-lock-cache-state
)
1485 ;; Find the state of `start'.
1486 (if (null font-lock-beginning-of-syntax-function
)
1487 ;; Use the state at the previous cache position, if any, or
1488 ;; otherwise calculate from `point-min'.
1489 (if (or (null cache
) (< start cache
))
1490 (setq state
(parse-partial-sexp (point-min) start
))
1491 (setq state
(parse-partial-sexp cache start nil nil
1492 font-lock-cache-state
)))
1493 ;; Call the function to move outside any syntactic block.
1494 (funcall font-lock-beginning-of-syntax-function
)
1495 (setq state
(parse-partial-sexp (point) start
)))
1496 ;; Cache the state and position of `start'.
1497 (setq font-lock-cache-state state
)
1498 (set-marker font-lock-cache-position start
))
1500 ;; If the region starts inside a string or comment, show the extent of it.
1501 (when (or (nth 3 state
) (nth 4 state
))
1502 (setq string
(nth 3 state
) beg
(point))
1503 (setq state
(parse-partial-sexp (point) end nil nil state
'syntax-table
))
1504 (put-text-property beg
(point) 'face
1506 font-lock-string-face
1507 font-lock-comment-face
)))
1509 ;; Find each interesting place between here and `end'.
1510 (while (and (< (point) end
)
1512 (setq state
(parse-partial-sexp (point) end nil nil state
1514 (or (nth 3 state
) (nth 4 state
))))
1515 (setq string
(nth 3 state
) beg
(nth 8 state
))
1516 (setq state
(parse-partial-sexp (point) end nil nil state
'syntax-table
))
1517 (put-text-property beg
(point) 'face
1519 font-lock-string-face
1520 font-lock-comment-face
)))))
1522 ;;; End of Syntactic fontification functions.
1524 ;;; Keyword regexp fontification functions.
1526 (defsubst font-lock-apply-highlight
(highlight)
1527 "Apply HIGHLIGHT following a match.
1528 HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
1529 (let* ((match (nth 0 highlight
))
1530 (start (match-beginning match
)) (end (match-end match
))
1531 (override (nth 2 highlight
)))
1533 ;; No match but we might not signal an error.
1534 (or (nth 3 highlight
)
1535 (error "No match %d in highlight %S" match highlight
)))
1537 ;; Cannot override existing fontification.
1538 (or (text-property-not-all start end
'face nil
)
1539 (put-text-property start end
'face
(eval (nth 1 highlight
)))))
1541 ;; Override existing fontification.
1542 (put-text-property start end
'face
(eval (nth 1 highlight
))))
1543 ((eq override
'prepend
)
1544 ;; Prepend to existing fontification.
1545 (font-lock-prepend-text-property start end
'face
(eval (nth 1 highlight
))))
1546 ((eq override
'append
)
1547 ;; Append to existing fontification.
1548 (font-lock-append-text-property start end
'face
(eval (nth 1 highlight
))))
1549 ((eq override
'keep
)
1550 ;; Keep existing fontification.
1551 (font-lock-fillin-text-property start end
'face
(eval (nth 1 highlight
)))))))
1553 (defsubst font-lock-fontify-anchored-keywords
(keywords limit
)
1554 "Fontify according to KEYWORDS until LIMIT.
1555 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1556 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1557 (let ((matcher (nth 0 keywords
)) (lowdarks (nthcdr 3 keywords
)) highlights
1558 (lead-start (match-beginning 0))
1559 ;; Evaluate PRE-MATCH-FORM.
1560 (pre-match-value (eval (nth 1 keywords
))))
1561 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1562 (if (not (and (numberp pre-match-value
) (> pre-match-value
(point))))
1563 (save-excursion (end-of-line) (setq limit
(point)))
1564 (setq limit pre-match-value
)
1565 (when (and font-lock-multiline
1566 (funcall (if (eq font-lock-multiline t
) '>= '>)
1568 (save-excursion (forward-line 1) (point))))
1569 ;; this is a multiline anchored match
1570 (set (make-local-variable 'font-lock-multiline
) t
)
1571 (put-text-property (point) limit
'font-lock-multiline t
)))
1573 ;; Find an occurrence of `matcher' before `limit'.
1574 (while (if (stringp matcher
)
1575 (re-search-forward matcher limit t
)
1576 (funcall matcher limit
))
1577 ;; Apply each highlight to this instance of `matcher'.
1578 (setq highlights lowdarks
)
1580 (font-lock-apply-highlight (car highlights
))
1581 (setq highlights
(cdr highlights
)))))
1582 ;; Evaluate POST-MATCH-FORM.
1583 (eval (nth 2 keywords
))))
1585 (defun font-lock-fontify-keywords-region (start end
&optional loudly
)
1586 "Fontify according to `font-lock-keywords' between START and END.
1587 START should be at the beginning of a line."
1588 (unless (eq (car font-lock-keywords
) t
)
1589 (setq font-lock-keywords
(font-lock-compile-keywords font-lock-keywords
)))
1590 (let ((case-fold-search font-lock-keywords-case-fold-search
)
1591 (keywords (cdr font-lock-keywords
))
1592 (bufname (buffer-name)) (count 0)
1593 keyword matcher highlights
)
1595 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1597 (if loudly
(message "Fontifying %s... (regexps..%s)" bufname
1598 (make-string (incf count
) ?.
)))
1600 ;; Find an occurrence of `matcher' from `start' to `end'.
1601 (setq keyword
(car keywords
) matcher
(car keyword
))
1603 (while (and (< (point) end
)
1604 (if (stringp matcher
)
1605 (re-search-forward matcher end t
)
1606 (funcall matcher end
)))
1607 (when (and font-lock-multiline
1609 (funcall (if (eq font-lock-multiline t
) '>= '>)
1611 (save-excursion (goto-char (match-beginning 0))
1612 (forward-line 1) (point))))
1613 ;; this is a multiline regexp match
1614 (set (make-local-variable 'font-lock-multiline
) t
)
1615 (put-text-property (match-beginning 0) (point)
1616 'font-lock-multiline t
))
1617 ;; Apply each highlight to this instance of `matcher', which may be
1618 ;; specific highlights or more keywords anchored to `matcher'.
1619 (setq highlights
(cdr keyword
))
1621 (if (numberp (car (car highlights
)))
1622 (font-lock-apply-highlight (car highlights
))
1623 (font-lock-fontify-anchored-keywords (car highlights
) end
))
1624 (setq highlights
(cdr highlights
))))
1625 (setq keywords
(cdr keywords
)))))
1627 ;;; End of Keyword regexp fontification functions.
1629 ;; Various functions.
1631 (defun font-lock-compile-keywords (keywords)
1632 "Compile KEYWORDS into the form (t KEYWORD ...).
1633 Here KEYWORD is of the form (MATCHER HIGHLIGHT ...) as shown in the
1634 `font-lock-keywords' doc string."
1635 (if (eq (car-safe keywords
) t
)
1637 (cons t
(mapcar 'font-lock-compile-keyword keywords
))))
1639 (defun font-lock-compile-keyword (keyword)
1640 (cond ((nlistp keyword
) ; MATCHER
1641 (list keyword
'(0 font-lock-keyword-face
)))
1642 ((eq (car keyword
) 'eval
) ; (eval . FORM)
1643 (font-lock-compile-keyword (eval (cdr keyword
))))
1644 ((eq (car-safe (cdr keyword
)) 'quote
) ; (MATCHER . 'FORM)
1645 ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote.
1646 (if (symbolp (nth 2 keyword
))
1647 (list (car keyword
) (list 0 (cdr keyword
)))
1648 (font-lock-compile-keyword (cons (car keyword
) (nth 2 keyword
)))))
1649 ((numberp (cdr keyword
)) ; (MATCHER . MATCH)
1650 (list (car keyword
) (list (cdr keyword
) 'font-lock-keyword-face
)))
1651 ((symbolp (cdr keyword
)) ; (MATCHER . FACENAME)
1652 (list (car keyword
) (list 0 (cdr keyword
))))
1653 ((nlistp (nth 1 keyword
)) ; (MATCHER . HIGHLIGHT)
1654 (list (car keyword
) (cdr keyword
)))
1655 (t ; (MATCHER HIGHLIGHT ...)
1658 (defun font-lock-eval-keywords (keywords)
1659 "Evalulate KEYWORDS if a function (funcall) or variable (eval) name."
1660 (if (listp keywords
)
1662 (font-lock-eval-keywords (if (fboundp keywords
)
1666 (defun font-lock-value-in-major-mode (alist)
1667 "Return value in ALIST for `major-mode', or ALIST if it is not an alist.
1668 Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t."
1670 (cdr (or (assq major-mode alist
) (assq t alist
)))
1673 (defun font-lock-choose-keywords (keywords level
)
1674 "Return LEVELth element of KEYWORDS.
1675 A LEVEL of nil is equal to a LEVEL of 0, a LEVEL of t is equal to
1676 \(1- (length KEYWORDS))."
1677 (cond ((symbolp keywords
)
1680 (or (nth level keywords
) (car (reverse keywords
))))
1682 (car (reverse keywords
)))
1686 (defvar font-lock-set-defaults nil
) ; Whether we have set up defaults.
1688 (defun font-lock-set-defaults ()
1689 "Set fontification defaults appropriately for this mode.
1690 Sets various variables using `font-lock-defaults' (or, if nil, using
1691 `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
1692 ;; Set fontification defaults.
1693 (make-local-variable 'font-lock-fontified
)
1694 ;; Set iff not previously set.
1695 (unless font-lock-set-defaults
1696 (set (make-local-variable 'font-lock-set-defaults
) t
)
1697 (set (make-local-variable 'font-lock-cache-state
) nil
)
1698 (set (make-local-variable 'font-lock-cache-position
) (make-marker))
1699 (let* ((defaults (or font-lock-defaults
1700 (cdr (assq major-mode font-lock-defaults-alist
))))
1702 (font-lock-choose-keywords (nth 0 defaults
)
1703 (font-lock-value-in-major-mode font-lock-maximum-decoration
)))
1704 (local (cdr (assq major-mode font-lock-keywords-alist
)))
1706 (cdr-safe (assq major-mode font-lock-removed-keywords-alist
))))
1707 ;; Regexp fontification?
1708 (set (make-local-variable 'font-lock-keywords
)
1709 (font-lock-compile-keywords (font-lock-eval-keywords keywords
)))
1710 ;; Local fontification?
1712 (font-lock-add-keywords nil
(car (car local
)) (cdr (car local
)))
1713 (setq local
(cdr local
)))
1714 (when removed-keywords
1715 (font-lock-remove-keywords nil removed-keywords
))
1716 ;; Syntactic fontification?
1717 (when (nth 1 defaults
)
1718 (set (make-local-variable 'font-lock-keywords-only
) t
))
1719 ;; Case fold during regexp fontification?
1720 (when (nth 2 defaults
)
1721 (set (make-local-variable 'font-lock-keywords-case-fold-search
) t
))
1722 ;; Syntax table for regexp and syntactic fontification?
1723 (when (nth 3 defaults
)
1724 (let ((slist (nth 3 defaults
)))
1725 (set (make-local-variable 'font-lock-syntax-table
)
1726 (copy-syntax-table (syntax-table)))
1728 ;; The character to modify may be a single CHAR or a STRING.
1729 (let ((chars (if (numberp (car (car slist
)))
1730 (list (car (car slist
)))
1731 (mapcar 'identity
(car (car slist
)))))
1732 (syntax (cdr (car slist
))))
1734 (modify-syntax-entry (car chars
) syntax font-lock-syntax-table
)
1735 (setq chars
(cdr chars
)))
1736 (setq slist
(cdr slist
))))))
1737 ;; Syntax function for syntactic fontification?
1738 (when (nth 4 defaults
)
1739 (set (make-local-variable 'font-lock-beginning-of-syntax-function
)
1742 (let ((alist (nthcdr 5 defaults
)))
1744 (let ((variable (car (car alist
))) (value (cdr (car alist
))))
1745 (unless (boundp variable
)
1747 (set (make-local-variable variable
) value
)
1748 (setq alist
(cdr alist
))))))))
1750 (defun font-lock-unset-defaults ()
1751 "Unset fontification defaults. See function `font-lock-set-defaults'."
1752 (setq font-lock-set-defaults nil
1753 font-lock-keywords nil
1754 font-lock-keywords-only nil
1755 font-lock-keywords-case-fold-search nil
1756 font-lock-syntax-table nil
1757 font-lock-beginning-of-syntax-function nil
)
1758 (let* ((defaults (or font-lock-defaults
1759 (cdr (assq major-mode font-lock-defaults-alist
))))
1760 (alist (nthcdr 5 defaults
)))
1762 (set (car (car alist
)) (default-value (car (car alist
))))
1763 (setq alist
(cdr alist
)))))
1765 ;;; Colour etc. support.
1767 ;; Originally these variable values were face names such as `bold' etc.
1768 ;; Now we create our own faces, but we keep these variables for compatibility
1769 ;; and they give users another mechanism for changing face appearance.
1770 ;; We now allow a FACENAME in `font-lock-keywords' to be any expression that
1771 ;; returns a face. So the easiest thing is to continue using these variables,
1772 ;; rather than sometimes evaling FACENAME and sometimes not. sm.
1773 (defvar font-lock-comment-face
'font-lock-comment-face
1774 "Face name to use for comments.")
1776 (defvar font-lock-string-face
'font-lock-string-face
1777 "Face name to use for strings.")
1779 (defvar font-lock-keyword-face
'font-lock-keyword-face
1780 "Face name to use for keywords.")
1782 (defvar font-lock-builtin-face
'font-lock-builtin-face
1783 "Face name to use for builtins.")
1785 (defvar font-lock-function-name-face
'font-lock-function-name-face
1786 "Face name to use for function names.")
1788 (defvar font-lock-variable-name-face
'font-lock-variable-name-face
1789 "Face name to use for variable names.")
1791 (defvar font-lock-type-face
'font-lock-type-face
1792 "Face name to use for type and class names.")
1794 (defvar font-lock-constant-face
'font-lock-constant-face
1795 "Face name to use for constant and label names.")
1797 (defvar font-lock-warning-face
'font-lock-warning-face
1798 "Face name to use for things that should stand out.")
1800 (defvar font-lock-reference-face
'font-lock-constant-face
1801 "This variable is obsolete. Use `font-lock-constant-face'.")
1803 ;; Originally face attributes were specified via `font-lock-face-attributes'.
1804 ;; Users then changed the default face attributes by setting that variable.
1805 ;; However, we try and be back-compatible and respect its value if set except
1806 ;; for faces where M-x customize has been used to save changes for the face.
1807 (when (boundp 'font-lock-face-attributes
)
1808 (let ((face-attributes font-lock-face-attributes
))
1809 (while face-attributes
1810 (let* ((face-attribute (pop face-attributes
))
1811 (face (car face-attribute
)))
1812 ;; Rustle up a `defface' SPEC from a `font-lock-face-attributes' entry.
1813 (unless (get face
'saved-face
)
1814 (let ((foreground (nth 1 face-attribute
))
1815 (background (nth 2 face-attribute
))
1816 (bold-p (nth 3 face-attribute
))
1817 (italic-p (nth 4 face-attribute
))
1818 (underline-p (nth 5 face-attribute
))
1821 (setq face-spec
(cons ':foreground
(cons foreground face-spec
))))
1823 (setq face-spec
(cons ':background
(cons background face-spec
))))
1825 (setq face-spec
(append '(:bold t
) face-spec
)))
1827 (setq face-spec
(append '(:italic t
) face-spec
)))
1829 (setq face-spec
(append '(:underline t
) face-spec
)))
1830 (custom-declare-face face
(list (list t face-spec
)) nil
)))))))
1832 ;; But now we do it the custom way. Note that `defface' will not overwrite any
1833 ;; faces declared above via `custom-declare-face'.
1834 (defface font-lock-comment-face
1835 '((((type tty
) (class color
)) (:foreground
"red"))
1836 (((class grayscale
) (background light
))
1837 (:foreground
"DimGray" :bold t
:italic t
))
1838 (((class grayscale
) (background dark
))
1839 (:foreground
"LightGray" :bold t
:italic t
))
1840 (((class color
) (background light
)) (:foreground
"Firebrick"))
1841 (((class color
) (background dark
)) (:foreground
"OrangeRed"))
1842 (t (:bold t
:italic t
)))
1843 "Font Lock mode face used to highlight comments."
1844 :group
'font-lock-highlighting-faces
)
1846 (defface font-lock-string-face
1847 '((((type tty
) (class color
)) (:foreground
"green"))
1848 (((class grayscale
) (background light
)) (:foreground
"DimGray" :italic t
))
1849 (((class grayscale
) (background dark
)) (:foreground
"LightGray" :italic t
))
1850 (((class color
) (background light
)) (:foreground
"RosyBrown"))
1851 (((class color
) (background dark
)) (:foreground
"LightSalmon"))
1853 "Font Lock mode face used to highlight strings."
1854 :group
'font-lock-highlighting-faces
)
1856 (defface font-lock-keyword-face
1857 '((((type tty
) (class color
)) (:foreground
"cyan" :weight bold
))
1858 (((class grayscale
) (background light
)) (:foreground
"LightGray" :bold t
))
1859 (((class grayscale
) (background dark
)) (:foreground
"DimGray" :bold t
))
1860 (((class color
) (background light
)) (:foreground
"Purple"))
1861 (((class color
) (background dark
)) (:foreground
"Cyan"))
1863 "Font Lock mode face used to highlight keywords."
1864 :group
'font-lock-highlighting-faces
)
1866 (defface font-lock-builtin-face
1867 '((((type tty
) (class color
)) (:foreground
"blue" :weight light
))
1868 (((class grayscale
) (background light
)) (:foreground
"LightGray" :bold t
))
1869 (((class grayscale
) (background dark
)) (:foreground
"DimGray" :bold t
))
1870 (((class color
) (background light
)) (:foreground
"Orchid"))
1871 (((class color
) (background dark
)) (:foreground
"LightSteelBlue"))
1873 "Font Lock mode face used to highlight builtins."
1874 :group
'font-lock-highlighting-faces
)
1876 (defface font-lock-function-name-face
1877 '((((type tty
) (class color
)) (:foreground
"blue" :weight bold
))
1878 (((class color
) (background light
)) (:foreground
"Blue"))
1879 (((class color
) (background dark
)) (:foreground
"LightSkyBlue"))
1880 (t (:inverse-video t
:bold t
)))
1881 "Font Lock mode face used to highlight function names."
1882 :group
'font-lock-highlighting-faces
)
1884 (defface font-lock-variable-name-face
1885 '((((type tty
) (class color
)) (:foreground
"yellow" :weight light
))
1886 (((class grayscale
) (background light
))
1887 (:foreground
"Gray90" :bold t
:italic t
))
1888 (((class grayscale
) (background dark
))
1889 (:foreground
"DimGray" :bold t
:italic t
))
1890 (((class color
) (background light
)) (:foreground
"DarkGoldenrod"))
1891 (((class color
) (background dark
)) (:foreground
"LightGoldenrod"))
1892 (t (:bold t
:italic t
)))
1893 "Font Lock mode face used to highlight variable names."
1894 :group
'font-lock-highlighting-faces
)
1896 (defface font-lock-type-face
1897 '((((type tty
) (class color
)) (:foreground
"green"))
1898 (((class grayscale
) (background light
)) (:foreground
"Gray90" :bold t
))
1899 (((class grayscale
) (background dark
)) (:foreground
"DimGray" :bold t
))
1900 (((class color
) (background light
)) (:foreground
"ForestGreen"))
1901 (((class color
) (background dark
)) (:foreground
"PaleGreen"))
1902 (t (:bold t
:underline t
)))
1903 "Font Lock mode face used to highlight type and classes."
1904 :group
'font-lock-highlighting-faces
)
1906 (defface font-lock-constant-face
1907 '((((type tty
) (class color
)) (:foreground
"magenta"))
1908 (((class grayscale
) (background light
))
1909 (:foreground
"LightGray" :bold t
:underline t
))
1910 (((class grayscale
) (background dark
))
1911 (:foreground
"Gray50" :bold t
:underline t
))
1912 (((class color
) (background light
)) (:foreground
"CadetBlue"))
1913 (((class color
) (background dark
)) (:foreground
"Aquamarine"))
1914 (t (:bold t
:underline t
)))
1915 "Font Lock mode face used to highlight constants and labels."
1916 :group
'font-lock-highlighting-faces
)
1918 (defface font-lock-warning-face
1919 '((((type tty
) (class color
)) (:foreground
"red"))
1920 (((class color
) (background light
)) (:foreground
"Red" :bold t
))
1921 (((class color
) (background dark
)) (:foreground
"Pink" :bold t
))
1922 (t (:inverse-video t
:bold t
)))
1923 "Font Lock mode face used to highlight warnings."
1924 :group
'font-lock-highlighting-faces
)
1926 ;;; End of Colour etc. support.
1930 ;; This section of code is commented out because Emacs does not have real menu
1931 ;; buttons. (We can mimic them by putting "( ) " or "(X) " at the beginning of
1932 ;; the menu entry text, but with Xt it looks both ugly and embarrassingly
1933 ;; amateur.) If/When Emacs gets real menus buttons, put in menu-bar.el after
1934 ;; the entry for "Text Properties" something like:
1936 ;; (define-key menu-bar-edit-menu [font-lock]
1937 ;; (cons "Syntax Highlighting" font-lock-menu))
1939 ;; and remove a single ";" from the beginning of each line in the rest of this
1940 ;; section. Probably the mechanism for telling the menu code what are menu
1941 ;; buttons and when they are on or off needs tweaking. I have assumed that the
1942 ;; mechanism is via `menu-toggle' and `menu-selected' symbol properties. sm.
1946 ; ;; Make the Font Lock menu.
1947 ; (defvar font-lock-menu (make-sparse-keymap "Syntax Highlighting"))
1948 ; ;; Add the menu items in reverse order.
1949 ; (define-key font-lock-menu [fontify-less]
1950 ; '("Less In Current Buffer" . font-lock-fontify-less))
1951 ; (define-key font-lock-menu [fontify-more]
1952 ; '("More In Current Buffer" . font-lock-fontify-more))
1953 ; (define-key font-lock-menu [font-lock-sep]
1955 ; (define-key font-lock-menu [font-lock-mode]
1956 ; '("In Current Buffer" . font-lock-mode))
1957 ; (define-key font-lock-menu [global-font-lock-mode]
1958 ; '("In All Buffers" . global-font-lock-mode)))
1962 ; ;; We put the appropriate `menu-enable' etc. symbol property values on when
1963 ; ;; font-lock.el is loaded, so we don't need to autoload the three variables.
1964 ; (put 'global-font-lock-mode 'menu-toggle t)
1965 ; (put 'font-lock-mode 'menu-toggle t)
1966 ; (put 'font-lock-fontify-more 'menu-enable '(identity))
1967 ; (put 'font-lock-fontify-less 'menu-enable '(identity)))
1969 ;;; Put the appropriate symbol property values on now. See above.
1970 ;(put 'global-font-lock-mode 'menu-selected 'global-font-lock-mode)
1971 ;(put 'font-lock-mode 'menu-selected 'font-lock-mode)
1972 ;(put 'font-lock-fontify-more 'menu-enable '(nth 2 font-lock-fontify-level))
1973 ;(put 'font-lock-fontify-less 'menu-enable '(nth 1 font-lock-fontify-level))
1975 ;(defvar font-lock-fontify-level nil) ; For less/more fontification.
1977 ;(defun font-lock-fontify-level (level)
1978 ; (let ((font-lock-maximum-decoration level))
1979 ; (when font-lock-mode
1982 ; (when font-lock-verbose
1983 ; (message "Fontifying %s... level %d" (buffer-name) level))))
1985 ;(defun font-lock-fontify-less ()
1986 ; "Fontify the current buffer with less decoration.
1987 ;See `font-lock-maximum-decoration'."
1989 ; ;; Check in case we get called interactively.
1990 ; (if (nth 1 font-lock-fontify-level)
1991 ; (font-lock-fontify-level (1- (car font-lock-fontify-level)))
1992 ; (error "No less decoration")))
1994 ;(defun font-lock-fontify-more ()
1995 ; "Fontify the current buffer with more decoration.
1996 ;See `font-lock-maximum-decoration'."
1998 ; ;; Check in case we get called interactively.
1999 ; (if (nth 2 font-lock-fontify-level)
2000 ; (font-lock-fontify-level (1+ (car font-lock-fontify-level)))
2001 ; (error "No more decoration")))
2003 ;;; This should be called by `font-lock-set-defaults'.
2004 ;(defun font-lock-set-menu ()
2005 ; ;; Activate less/more fontification entries if there are multiple levels for
2006 ; ;; the current buffer. Sets `font-lock-fontify-level' to be of the form
2007 ; ;; (CURRENT-LEVEL IS-LOWER-LEVEL-P IS-HIGHER-LEVEL-P) for menu activation.
2008 ; (let ((keywords (or (nth 0 font-lock-defaults)
2009 ; (nth 1 (assq major-mode font-lock-defaults-alist))))
2010 ; (level (font-lock-value-in-major-mode font-lock-maximum-decoration)))
2011 ; (make-local-variable 'font-lock-fontify-level)
2012 ; (if (or (symbolp keywords) (= (length keywords) 1))
2013 ; (font-lock-unset-menu)
2014 ; (cond ((eq level t)
2015 ; (setq level (1- (length keywords))))
2016 ; ((or (null level) (zerop level))
2017 ; ;; The default level is usually, but not necessarily, level 1.
2018 ; (setq level (- (length keywords)
2019 ; (length (member (eval (car keywords))
2020 ; (mapcar 'eval (cdr keywords))))))))
2021 ; (setq font-lock-fontify-level (list level (> level 1)
2022 ; (< level (1- (length keywords))))))))
2024 ;;; This should be called by `font-lock-unset-defaults'.
2025 ;(defun font-lock-unset-menu ()
2026 ; ;; Deactivate less/more fontification entries.
2027 ; (setq font-lock-fontify-level nil))
2029 ;;; End of Menu support.
2031 ;;; Various regexp information shared by several modes.
2032 ;;; Information specific to a single mode should go in its load library.
2034 ;; Font Lock support for C, C++, Objective-C and Java modes will one day be in
2035 ;; some cc-font.el (and required by cc-mode.el). However, the below function
2036 ;; should stay in font-lock.el, since it is used by other libraries. sm.
2038 (defun font-lock-match-c-style-declaration-item-and-skip-to-next (limit)
2039 "Match, and move over, any declaration/definition item after point.
2040 Matches after point, but ignores leading whitespace and `*' characters.
2041 Does not move further than LIMIT.
2043 The expected syntax of a declaration/definition item is `word' (preceded by
2044 optional whitespace and `*' characters and proceeded by optional whitespace)
2045 optionally followed by a `('. Everything following the item (but belonging to
2046 it) is expected to by skip-able by `scan-sexps', and items are expected to be
2047 separated with a `,' and to be terminated with a `;'.
2049 Thus the regexp matches after point: word (
2051 Where the match subexpressions are: 1 2
2053 The item is delimited by (match-beginning 1) and (match-end 1).
2054 If (match-beginning 2) is non-nil, the item is followed by a `('.
2056 This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
2057 (when (looking-at "[ \t*]*\\(\\sw+\\)[ \t]*\\((\\)?")
2061 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
2062 (narrow-to-region (point-min) limit
)
2063 (goto-char (match-end 1))
2064 ;; Move over any item value, etc., to the next item.
2065 (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)"))
2066 (goto-char (or (scan-sexps (point) 1) (point-max))))
2067 (goto-char (match-end 2)))
2072 (defconst lisp-font-lock-keywords-1
2077 (list (concat "(\\(def\\("
2078 ;; Function declarations.
2079 "\\(advice\\|alias\\|generic\\|macro\\*?\\|method\\|"
2080 "setf\\|subst\\*?\\|un\\*?\\|"
2081 "ine-\\(condition\\|derived-mode\\|function\\|"
2082 "method-combination\\|setf-expander\\|skeleton\\|widget\\|"
2083 "\\(compiler\\|modify\\|symbol\\)-macro\\)\\)\\|"
2084 ;; Variable declarations.
2085 "\\(const\\(ant\\)?\\|custom\\|face\\|parameter\\|var\\)\\|"
2086 ;; Structure declarations.
2087 "\\(class\\|group\\|package\\|struct\\|type\\)"
2089 ;; Any whitespace and defined object.
2092 '(1 font-lock-keyword-face
)
2093 '(9 (cond ((match-beginning 3) font-lock-function-name-face
)
2094 ((match-beginning 6) font-lock-variable-name-face
)
2095 (t font-lock-type-face
))
2098 ;; Emacs Lisp autoload cookies.
2099 '("^;;;###\\(autoload\\)" 1 font-lock-warning-face prepend
)
2101 "Subdued level highlighting for Lisp modes.")
2103 (defconst lisp-font-lock-keywords-2
2104 (append lisp-font-lock-keywords-1
2108 ;; Control structures. Emacs Lisp forms.
2111 '("cond" "if" "while" "let" "let*"
2112 "prog" "progn" "progv" "prog1" "prog2" "prog*"
2113 "inline" "lambda" "save-restriction" "save-excursion"
2114 "save-window-excursion" "save-selected-window"
2115 "save-match-data" "save-current-buffer" "unwind-protect"
2116 "condition-case" "track-mouse"
2117 "eval-after-load" "eval-and-compile" "eval-when-compile"
2119 "with-current-buffer" "with-electric-help"
2120 "with-output-to-string" "with-output-to-temp-buffer"
2121 "with-temp-buffer" "with-temp-file" "with-temp-message"
2126 ;; Control structures. Common Lisp forms.
2129 '("when" "unless" "case" "ecase" "typecase" "etypecase"
2130 "ccase" "ctypecase" "handler-case" "handler-bind"
2131 "restart-bind" "restart-case" "in-package"
2132 "cerror" "break" "ignore-errors"
2133 "loop" "do" "do*" "dotimes" "dolist" "the" "locally"
2134 "proclaim" "declaim" "declare" "symbol-macrolet"
2135 "lexical-let" "lexical-let*" "flet" "labels" "compiler-let"
2136 "destructuring-bind" "macrolet" "tagbody" "block"
2137 "return" "return-from") t
)
2141 ;; Exit/Feature symbols as constants.
2142 (list (concat "(\\(catch\\|throw\\|featurep\\|provide\\|require\\)\\>"
2143 "[ \t']*\\(\\sw+\\)?")
2144 '(1 font-lock-keyword-face
)
2145 '(2 font-lock-constant-face nil t
))
2147 ;; Erroneous structures.
2148 '("(\\(abort\\|assert\\|error\\|signal\\)\\>" 1 font-lock-warning-face
)
2150 ;; Words inside \\[] tend to be for `substitute-command-keys'.
2151 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-constant-face prepend
)
2153 ;; Words inside `' tend to be symbol names.
2154 '("`\\(\\sw\\sw+\\)'" 1 font-lock-constant-face prepend
)
2157 '("\\<:\\sw\\sw+\\>" 0 font-lock-builtin-face
)
2159 ;; ELisp and CLisp `&' keywords as types.
2160 '("\\&\\sw+\\>" . font-lock-type-face
)
2162 "Gaudy level highlighting for Lisp modes.")
2164 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
2165 "Default expressions to highlight in Lisp modes.")
2169 ;(defvar tex-font-lock-keywords
2170 ; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
2171 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
2172 ; 2 font-lock-function-name-face)
2173 ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
2174 ; 2 font-lock-constant-face)
2175 ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
2176 ; ;; not be able to display those fonts.
2177 ; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep)
2178 ; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep)
2179 ; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face)
2180 ; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep))
2181 ; ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>.
2182 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
2183 ; 2 font-lock-function-name-face)
2184 ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
2185 ; 2 font-lock-constant-face)
2186 ; ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face)
2187 ; "\\\\\\([a-zA-Z@]+\\|.\\)"
2188 ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
2189 ; ;; not be able to display those fonts.
2190 ; ;; LaTeX2e: \emph{This is emphasized}.
2191 ; ("\\\\emph{\\([^}]+\\)}" 1 'italic keep)
2192 ; ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...}
2193 ; ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}"
2194 ; 3 (if (match-beginning 2) 'bold 'italic) keep)
2195 ; ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables.
2196 ; ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)"
2197 ; 3 (if (match-beginning 2) 'bold 'italic) keep))
2199 ;; Rewritten with the help of Alexandra Bac <abac@welcome.disi.unige.it>.
2200 (defconst tex-font-lock-keywords-1
2203 ;; Names of commands whose arg should be fontified as heading, etc.
2204 (headings (regexp-opt '("title" "begin" "end") t
))
2205 ;; These commands have optional args.
2206 (headings-opt (regexp-opt
2208 "section" "subsection" "subsubsection"
2209 "section*" "subsection*" "subsubsection*"
2210 "paragraph" "subparagraph" "subsubparagraph"
2211 "paragraph*" "subparagraph*" "subsubparagraph*"
2212 "newcommand" "renewcommand" "newenvironment"
2214 "newcommand*" "renewcommand*" "newenvironment*"
2217 (variables (regexp-opt
2218 '("newcounter" "newcounter*" "setcounter" "addtocounter"
2219 "setlength" "addtolength" "settowidth")
2221 (includes (regexp-opt
2222 '("input" "include" "includeonly" "bibliography"
2223 "epsfig" "psfig" "epsf")
2225 (includes-opt (regexp-opt
2226 '("nofiles" "usepackage"
2227 "includegraphics" "includegraphics*")
2231 (opt "\\(\\[[^]]*\\]\\)?")
2232 (arg "{\\([^}]+\\)")
2233 (opt-depth (regexp-opt-depth opt
))
2234 (arg-depth (regexp-opt-depth arg
))
2239 (list (concat slash headings arg
)
2240 (+ (regexp-opt-depth headings
) arg-depth
)
2241 'font-lock-function-name-face
)
2242 (list (concat slash headings-opt opt arg
)
2243 (+ (regexp-opt-depth headings-opt
) opt-depth arg-depth
)
2244 'font-lock-function-name-face
)
2247 (list (concat slash variables arg
)
2248 (+ (regexp-opt-depth variables
) arg-depth
)
2249 'font-lock-variable-name-face
)
2252 (list (concat slash includes arg
)
2253 (+ (regexp-opt-depth includes
) arg-depth
)
2254 'font-lock-builtin-face
)
2255 (list (concat slash includes-opt opt arg
)
2256 (+ (regexp-opt-depth includes-opt
) opt-depth arg-depth
)
2257 'font-lock-builtin-face
)
2259 ;; Definitions. I think.
2260 '("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)"
2261 1 font-lock-function-name-face
)
2263 "Subdued expressions to highlight in TeX modes.")
2265 (defconst tex-font-lock-keywords-2
2266 (append tex-font-lock-keywords-1
2269 ;; Names of commands whose arg should be fontified with fonts.
2270 (bold (regexp-opt '("bf" "textbf" "textsc" "textup"
2271 "boldsymbol" "pmb") t
))
2272 (italic (regexp-opt '("it" "textit" "textsl" "emph") t
))
2273 (type (regexp-opt '("texttt" "textmd" "textrm" "textsf") t
))
2275 ;; Names of commands whose arg should be fontified as a citation.
2276 (citations (regexp-opt
2277 '("label" "ref" "pageref" "vref" "eqref")
2279 (citations-opt (regexp-opt
2280 '("cite" "nocite" "caption" "index" "glossary"
2281 "footnote" "footnotemark" "footnotetext")
2284 ;; Names of commands that should be fontified.
2285 (specials (regexp-opt
2287 "linebreak" "nolinebreak" "pagebreak" "nopagebreak"
2288 "newline" "newpage" "clearpage" "cleardoublepage"
2289 "displaybreak" "allowdisplaybreaks" "enlargethispage")
2291 (general "\\([a-zA-Z@]+\\**\\|[^ \t\n]\\)")
2295 (opt "\\(\\[[^]]*\\]\\)?")
2296 (arg "{\\([^}]+\\)")
2297 (opt-depth (regexp-opt-depth opt
))
2298 (arg-depth (regexp-opt-depth arg
))
2303 (list (concat slash citations arg
)
2304 (+ (regexp-opt-depth citations
) arg-depth
)
2305 'font-lock-constant-face
)
2306 (list (concat slash citations-opt opt arg
)
2307 (+ (regexp-opt-depth citations-opt
) opt-depth arg-depth
)
2308 'font-lock-constant-face
)
2310 ;; Command names, special and general.
2311 (cons (concat slash specials
) 'font-lock-warning-face
)
2312 (concat slash general
)
2314 ;; Font environments. It seems a bit dubious to use `bold' etc. faces
2315 ;; since we might not be able to display those fonts.
2316 (list (concat slash bold arg
)
2317 (+ (regexp-opt-depth bold
) arg-depth
)
2318 '(quote bold
) 'keep
)
2319 (list (concat slash italic arg
)
2320 (+ (regexp-opt-depth italic
) arg-depth
)
2321 '(quote italic
) 'keep
)
2322 (list (concat slash type arg
)
2323 (+ (regexp-opt-depth type
) arg-depth
)
2324 '(quote bold-italic
) 'keep
)
2326 ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables.
2327 (list (concat "\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>"
2328 "\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)")
2329 3 '(if (match-beginning 2) 'bold
'italic
) 'keep
)
2331 "Gaudy expressions to highlight in TeX modes.")
2333 (defvar tex-font-lock-keywords tex-font-lock-keywords-1
2334 "Default expressions to highlight in TeX modes.")
2338 ;; These provide a means to fontify types not defined by the language. Those
2339 ;; types might be the user's own or they might be generally accepted and used.
2340 ;; Generally accepted types are used to provide default variable values.
2342 (define-widget 'font-lock-extra-types-widget
'radio
2343 "Widget `:type' for members of the custom group `font-lock-extra-types'.
2344 Members should `:load' the package `font-lock' to use this widget."
2345 :args
'((const :tag
"none" nil
)
2346 (repeat :tag
"types" regexp
)))
2348 (defcustom c-font-lock-extra-types
'("FILE" "\\sw+_t")
2349 "*List of extra types to fontify in C mode.
2350 Each list item should be a regexp not containing word-delimiters.
2351 For example, a value of (\"FILE\" \"\\\\sw+_t\") means the word FILE and words
2352 ending in _t are treated as type names.
2354 The value of this variable is used when Font Lock mode is turned on."
2355 :type
'font-lock-extra-types-widget
2356 :group
'font-lock-extra-types
)
2358 (defcustom c
++-font-lock-extra-types
2360 "\\([iof]\\|str\\)+stream\\(buf\\)?" "ios"
2363 "deque" "vector" "bit_vector"
2366 "hash\\(_\\(m\\(ap\\|ulti\\(map\\|set\\)\\)\\|set\\)\\)?"
2367 "stack" "queue" "priority_queue"
2369 "iterator" "const_iterator" "reverse_iterator" "const_reverse_iterator"
2370 "reference" "const_reference")
2371 "*List of extra types to fontify in C++ mode.
2372 Each list item should be a regexp not containing word-delimiters.
2373 For example, a value of (\"string\") means the word string is treated as a type
2376 The value of this variable is used when Font Lock mode is turned on."
2377 :type
'font-lock-extra-types-widget
2378 :group
'font-lock-extra-types
)
2380 (defcustom objc-font-lock-extra-types
'("Class" "BOOL" "IMP" "SEL")
2381 "*List of extra types to fontify in Objective-C mode.
2382 Each list item should be a regexp not containing word-delimiters.
2383 For example, a value of (\"Class\" \"BOOL\" \"IMP\" \"SEL\") means the words
2384 Class, BOOL, IMP and SEL are treated as type names.
2386 The value of this variable is used when Font Lock mode is turned on."
2387 :type
'font-lock-extra-types-widget
2388 :group
'font-lock-extra-types
)
2390 (defcustom java-font-lock-extra-types
2391 '("[A-Z\300-\326\330-\337]\\sw*[a-z]\\sw*")
2392 "*List of extra types to fontify in Java mode.
2393 Each list item should be a regexp not containing word-delimiters.
2394 For example, a value of (\"[A-Z\300-\326\330-\337]\\\\sw*[a-z]\\\\sw*\") means capitalised
2395 words (and words conforming to the Java id spec) are treated as type names.
2397 The value of this variable is used when Font Lock mode is turned on."
2398 :type
'font-lock-extra-types-widget
2399 :group
'font-lock-extra-types
)
2403 ;; [Murmur murmur murmur] Maestro, drum-roll please... [Murmur murmur murmur.]
2404 ;; Ahem. [Murmur murmur murmur] Lay-dees an Gennel-men. [Murmur murmur shhh!]
2405 ;; I am most proud and humbly honoured today [murmur murmur cough] to present
2406 ;; to you good people, the winner of the Second Millennium Award for The Most
2407 ;; Hairy Language Syntax. [Ahhh!] All rise please. [Shuffle shuffle
2408 ;; shuffle.] And a round of applause please. For... The C Language! [Roar.]
2410 ;; Thank you... You are too kind... It is with a feeling of great privilege
2411 ;; and indeed emotion [sob] that I accept this award. It has been a long hard
2412 ;; road. But we know our destiny. And our future. For we must not rest.
2413 ;; There are more tokens to overload, more shoehorn, more methodologies. But
2414 ;; more is a plus! [Ha ha ha.] And more means plus! [Ho ho ho.] The future
2415 ;; is C++! [Ohhh!] The Third Millennium Award... Will be ours! [Roar.]
2417 (defconst c-font-lock-keywords-1 nil
2418 "Subdued level highlighting for C mode.")
2420 (defconst c-font-lock-keywords-2 nil
2421 "Medium level highlighting for C mode.
2422 See also `c-font-lock-extra-types'.")
2424 (defconst c-font-lock-keywords-3 nil
2425 "Gaudy level highlighting for C mode.
2426 See also `c-font-lock-extra-types'.")
2430 (regexp-opt '("break" "continue" "do" "else" "for" "if" "return"
2431 "switch" "while" "sizeof"
2432 ;; Type related, but we don't do anything special.
2433 "typedef" "extern" "auto" "register" "static"
2435 ;; Dan Nicolaescu <done@gnu.org> says this is new.
2439 (regexp-opt '("enum" "struct" "union") t
)))
2441 (regexp-opt-depth c-type-specs
))
2443 `(mapconcat 'identity
2445 (,@ (eval-when-compile
2447 '("char" "short" "int" "long" "signed" "unsigned"
2448 "float" "double" "void" "complex"))))
2449 c-font-lock-extra-types
)
2452 `(regexp-opt-depth (,@ c-type-names
)))
2453 (c-preprocessor-directives
2456 '("define" "elif" "else" "endif" "error" "file" "if" "ifdef"
2457 "ifndef" "include" "line" "pragma" "undef"))))
2458 (c-preprocessor-directives-depth
2459 (regexp-opt-depth c-preprocessor-directives
))
2461 (setq c-font-lock-keywords-1
2464 ;; These are all anchored at the beginning of line for speed.
2465 ;; Note that `c++-font-lock-keywords-1' depends on `c-font-lock-keywords-1'.
2467 ;; Fontify function name definitions (GNU style; without type on line).
2468 '("^\\(\\sw+\\)[ \t]*(" 1 font-lock-function-name-face
)
2470 ;; Fontify error directives.
2471 '("^#[ \t]*error[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend
)
2473 ;; Fontify filenames in #include <...> preprocessor directives as strings.
2474 '("^#[ \t]*\\(import\\|include\\)[ \t]*\\(<[^>\"\n]*>?\\)"
2475 2 font-lock-string-face
)
2477 ;; Fontify function macro names.
2478 '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face
)
2480 ;; Fontify symbol names in #elif or #if ... defined preprocessor directives.
2481 '("^#[ \t]*\\(elif\\|if\\)\\>"
2482 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
2483 (1 font-lock-builtin-face
) (2 font-lock-variable-name-face nil t
)))
2485 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
2487 (concat "^#[ \t]*\\(" c-preprocessor-directives
2488 "\\)\\>[ \t!]*\\(\\sw+\\)?")
2489 '(1 font-lock-builtin-face
)
2490 (list (+ 2 c-preprocessor-directives-depth
)
2491 'font-lock-variable-name-face nil t
))
2494 (setq c-font-lock-keywords-2
2495 (append c-font-lock-keywords-1
2498 ;; Simple regexps for speed.
2500 ;; Fontify all type names.
2502 (cons (concat "\\<\\(" (,@ c-type-names
) "\\)\\>") 'font-lock-type-face
))
2504 ;; Fontify all builtin keywords (except case, default and goto; see below).
2505 (concat "\\<\\(" c-keywords
"\\|" c-type-specs
"\\)\\>")
2507 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2508 '("\\<\\(case\\|goto\\)\\>"
2509 (1 font-lock-keyword-face
)
2510 ("\\(-[0-9]+\\|\\sw+\\)"
2511 ;; Return limit of search.
2512 (save-excursion (skip-chars-forward "^:\n") (point))
2514 (1 font-lock-constant-face nil t
)))
2515 ;; Anders Lindgren <andersl@andersl.com> points out that it is quicker to
2516 ;; use MATCH-ANCHORED to effectively anchor the regexp on the left.
2517 ;; This must come after the one for keywords and targets.
2518 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2519 (beginning-of-line) (end-of-line)
2520 (1 font-lock-constant-face
)))
2523 (setq c-font-lock-keywords-3
2524 (append c-font-lock-keywords-2
2526 ;; More complicated regexps for more complete highlighting for types.
2527 ;; We still have to fontify type specifiers individually, as C is so hairy.
2530 ;; Fontify all storage types, plus their items.
2532 (list (concat "\\<\\(" (,@ c-type-names
) "\\)\\>"
2533 "\\([ \t*&]+\\sw+\\>\\)*")
2534 ;; Fontify each declaration item.
2535 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2536 ;; Start with point after all type specifiers.
2537 (list 'goto-char
(list 'or
2538 (list 'match-beginning
2539 (+ (,@ c-type-names-depth
) 2))
2541 ;; Finish with point after first type specifier.
2542 '(goto-char (match-end 1))
2543 ;; Fontify as a variable or function name.
2544 '(1 (if (match-beginning 2)
2545 font-lock-function-name-face
2546 font-lock-variable-name-face
)))))
2548 ;; Fontify all storage specs and types, plus their items.
2550 (list (concat "\\<\\(" (,@ c-type-specs
) "\\)\\>"
2551 "[ \t]*\\(\\sw+\\)?")
2552 (list 1 'font-lock-keyword-face
)
2553 (list (+ (,@ c-type-specs-depth
) 2) 'font-lock-type-face nil t
)
2554 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2556 ;; Fontify as a variable or function name.
2557 '(1 (if (match-beginning 2)
2558 font-lock-function-name-face
2559 font-lock-variable-name-face
) nil t
))))
2561 ;; Fontify structures, or typedef names, plus their items.
2562 '("\\(}\\)[ \t*]*\\sw"
2563 (font-lock-match-c-style-declaration-item-and-skip-to-next
2564 (goto-char (match-end 1)) nil
2565 (1 font-lock-type-face
)))
2567 ;; Fontify anything at beginning of line as a declaration or definition.
2568 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2569 (1 font-lock-type-face
)
2570 (font-lock-match-c-style-declaration-item-and-skip-to-next
2571 (goto-char (or (match-beginning 2) (match-end 1))) nil
2572 (1 (if (match-beginning 2)
2573 font-lock-function-name-face
2574 font-lock-variable-name-face
))))
2578 (defvar c-font-lock-keywords c-font-lock-keywords-1
2579 "Default expressions to highlight in C mode.
2580 See also `c-font-lock-extra-types'.")
2584 (defconst c
++-font-lock-keywords-1 nil
2585 "Subdued level highlighting for C++ mode.")
2587 (defconst c
++-font-lock-keywords-2 nil
2588 "Medium level highlighting for C++ mode.
2589 See also `c++-font-lock-extra-types'.")
2591 (defconst c
++-font-lock-keywords-3 nil
2592 "Gaudy level highlighting for C++ mode.
2593 See also `c++-font-lock-extra-types'.")
2595 (defun font-lock-match-c++-style-declaration-item-and-skip-to-next
(limit)
2596 ;; Regexp matches after point: word<word>::word (
2598 ;; Where the match subexpressions are: 1 3 5 6
2600 ;; Item is delimited by (match-beginning 1) and (match-end 1).
2601 ;; If (match-beginning 3) is non-nil, that part of the item incloses a `<>'.
2602 ;; If (match-beginning 5) is non-nil, that part of the item follows a `::'.
2603 ;; If (match-beginning 6) is non-nil, the item is followed by a `('.
2604 (when (looking-at (eval-when-compile
2606 ;; Skip any leading whitespace.
2608 ;; This is `c++-type-spec' from below. (Hint hint!)
2609 "\\(\\sw+\\)" ; The instance?
2610 "\\([ \t]*<\\([^>\n]+\\)[ \t*&]*>\\)?" ; Or template?
2611 "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)*" ; Or member?
2612 ;; Match any trailing parenthesis.
2617 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
2618 (narrow-to-region (point-min) limit
)
2619 (goto-char (match-end 1))
2620 ;; Move over any item value, etc., to the next item.
2621 (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)"))
2622 (goto-char (or (scan-sexps (point) 1) (point-max))))
2623 (goto-char (match-end 2)))
2626 (defun font-lock-match-c++-structor-declaration
(limit)
2627 ;; Match C++ constructors and destructors inside class declarations.
2629 (regexp (concat "^\\s-+\\(\\(virtual\\|explicit\\)\\s-+\\)*~?\\(\\<"
2630 (mapconcat 'identity
2631 c
++-font-lock-extra-types
"\\|")
2633 ;; Don't match function pointer declarations, e.g.:
2636 (while (progn (setq res
(re-search-forward regexp limit t
))
2641 (not (vectorp (c-at-toplevel-p))))))))
2644 (let* ((c++-keywords
2647 '("break" "continue" "do" "else" "for" "if" "return" "switch"
2648 "while" "asm" "catch" "delete" "new" "sizeof" "this" "throw" "try"
2650 ;; Branko Cibej <branko.cibej@hermes.si> says this is new.
2652 ;; Mark Mitchell <mmitchell@usa.net> says these are new.
2653 "mutable" "explicit"
2654 ;; Alain Picard <ap@abelard.apana.org.au> suggests treating these
2655 ;; as keywords not types.
2656 "typedef" "template"
2657 "extern" "auto" "register" "const" "volatile" "static"
2658 "inline" "friend" "virtual") t
)))
2662 ;; Taken from Stroustrup, minus keywords otherwise fontified.
2663 '("+" "-" "*" "/" "%" "^" "&" "|" "~" "!" "=" "<" ">" "+=" "-="
2664 "*=" "/=" "%=" "^=" "&=" "|=" "<<" ">>" ">>=" "<<=" "==" "!="
2665 "<=" ">=" "&&" "||" "++" "--" "->*" "," "->" "[]" "()"))))
2669 '("class" "public" "private" "protected" "typename"
2670 "struct" "union" "enum" "namespace" "using"
2671 ;; Eric Hopper <hopper@omnifarious.mn.org> says these are new.
2672 "static_cast" "dynamic_cast" "const_cast" "reinterpret_cast") t
)))
2673 (c++-type-specs-depth
2674 (regexp-opt-depth c
++-type-specs
))
2676 `(mapconcat 'identity
2678 (,@ (eval-when-compile
2680 '("signed" "unsigned" "short" "long"
2681 "int" "char" "float" "double" "void"
2682 "bool" "complex"))))
2683 c
++-font-lock-extra-types
)
2685 (c++-type-names-depth
`(regexp-opt-depth (,@ c
++-type-names
)))
2687 ;; A brave attempt to match templates following a type and/or match
2688 ;; class membership. See and sync the above function
2689 ;; `font-lock-match-c++-style-declaration-item-and-skip-to-next'.
2690 (c++-type-suffix
(concat "\\([ \t]*<\\([^>\n]+\\)[ \t*&]*>\\)?"
2691 "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)*"))
2692 (c++-type-suffix-depth
(regexp-opt-depth c
++-type-suffix
))
2693 ;; If the string is a type, it may be followed by the cruft above.
2694 (c++-type-spec
(concat "\\(\\sw+\\)\\>" c
++-type-suffix
))
2695 (c++-type-spec-depth
(regexp-opt-depth c
++-type-spec
))
2697 ;; Parenthesis depth of user-defined types not forgetting their cruft.
2698 (c++-type-depth
`(regexp-opt-depth
2699 (concat (,@ c
++-type-names
) (,@ c
++-type-suffix
))))
2701 (setq c
++-font-lock-keywords-1
2704 ;; The list `c-font-lock-keywords-1' less that for function names.
2705 (cdr c-font-lock-keywords-1
)
2708 ;; Fontify function name definitions, possibly incorporating class names.
2709 (list (concat "^" c
++-type-spec
"[ \t]*(")
2710 '(1 (if (or (match-beginning 2) (match-beginning 4))
2712 font-lock-function-name-face
))
2713 '(3 font-lock-type-face nil t
)
2714 '(5 font-lock-function-name-face nil t
))
2717 (setq c
++-font-lock-keywords-2
2718 (append c
++-font-lock-keywords-1
2721 ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading.
2723 (cons (concat "\\<\\(" (,@ c
++-type-names
) "\\)\\>")
2724 'font-lock-type-face
))
2726 ;; Fontify operator overloading.
2727 (list (concat "\\<\\(operator\\)\\>[ \t]*\\(" c
++-operators
"\\)?")
2728 '(1 font-lock-keyword-face
)
2729 '(2 font-lock-builtin-face nil t
))
2731 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2732 '("\\<\\(case\\|goto\\)\\>"
2733 (1 font-lock-keyword-face
)
2734 ("\\(-[0-9]+\\|\\sw+\\)[ \t]*\\(::\\)?"
2735 ;; Return limit of search.
2738 (skip-chars-forward "^:\n")
2743 (1 (if (match-beginning 2)
2745 font-lock-constant-face
) nil t
)))
2746 ;; This must come after the one for keywords and targets.
2747 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:\\($\\|[^:]\\)"
2748 (beginning-of-line) (end-of-line)
2749 (1 font-lock-constant-face
)))
2751 ;; Fontify other builtin keywords.
2752 (concat "\\<\\(" c
++-keywords
"\\|" c
++-type-specs
"\\)\\>")
2754 ;; Eric Hopper <hopper@omnifarious.mn.org> says `true' and `false' are new.
2755 '("\\<\\(false\\|true\\)\\>" . font-lock-constant-face
)
2758 (setq c
++-font-lock-keywords-3
2759 (append c
++-font-lock-keywords-2
2761 ;; More complicated regexps for more complete highlighting for types.
2764 ;; Fontify all storage classes and type specifiers, plus their items.
2766 (list (concat "\\<\\(" (,@ c
++-type-names
) "\\)\\>" (,@ c
++-type-suffix
)
2767 "\\([ \t*&]+" (,@ c
++-type-spec
) "\\)*")
2768 ;; The name of any template type.
2769 (list (+ (,@ c
++-type-names-depth
) 3) 'font-lock-type-face nil t
)
2770 ;; Fontify each declaration item.
2771 (list 'font-lock-match-c
++-style-declaration-item-and-skip-to-next
2772 ;; Start with point after all type specifiers.
2773 (list 'goto-char
(list 'or
(list 'match-beginning
2774 (+ (,@ c
++-type-depth
) 2))
2776 ;; Finish with point after first type specifier.
2777 '(goto-char (match-end 1))
2778 ;; Fontify as a variable or function name.
2779 '(1 (cond ((or (match-beginning 2) (match-beginning 4))
2780 font-lock-type-face
)
2781 ((and (match-beginning 6) (c-at-toplevel-p))
2782 font-lock-function-name-face
)
2784 font-lock-variable-name-face
)))
2785 '(3 font-lock-type-face nil t
)
2786 '(5 (if (match-beginning 6)
2787 font-lock-function-name-face
2788 font-lock-variable-name-face
) nil t
))))
2790 ;; Fontify all storage specs and types, plus their items.
2792 (list (concat "\\<" (,@ c
++-type-specs
) "\\>" (,@ c
++-type-suffix
)
2793 "[ \t]*\\(" (,@ c
++-type-spec
) "\\)?")
2794 ;; The name of any template type.
2795 (list (+ (,@ c
++-type-specs-depth
) 2) 'font-lock-type-face nil t
)
2796 ;; The name of any type.
2797 (list (+ (,@ c
++-type-specs-depth
) (,@ c
++-type-suffix-depth
) 2)
2798 'font-lock-type-face nil t
)
2799 ;; Fontify each declaration item.
2800 (list 'font-lock-match-c
++-style-declaration-item-and-skip-to-next
2801 ;; Start with point after all type specifiers.
2803 ;; Finish with point after first type specifier.
2805 ;; Fontify as a variable or function name.
2806 '(1 (cond ((or (match-beginning 2) (match-beginning 4))
2807 font-lock-type-face
)
2808 ((and (match-beginning 6) (c-at-toplevel-p))
2809 font-lock-function-name-face
)
2811 font-lock-variable-name-face
)))
2812 '(3 font-lock-type-face nil t
)
2813 '(5 (if (match-beginning 6)
2814 font-lock-function-name-face
2815 font-lock-variable-name-face
) nil t
))
2818 ;; Fontify structures, or typedef names, plus their items.
2819 '("\\(}\\)[ \t*]*\\sw"
2820 (font-lock-match-c++-style-declaration-item-and-skip-to-next
2821 (goto-char (match-end 1)) nil
2822 (1 font-lock-type-face
)))
2824 ;; Fontify anything at beginning of line as a declaration or definition.
2825 (list (concat "^\\(" c
++-type-spec
"[ \t*&]*\\)+")
2826 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
2827 (goto-char (match-beginning 1))
2828 (goto-char (match-end 1))
2829 (1 (cond ((or (match-beginning 2) (match-beginning 4))
2830 font-lock-type-face
)
2831 ((match-beginning 6) font-lock-function-name-face
)
2832 (t font-lock-variable-name-face
)))
2833 (3 font-lock-type-face nil t
)
2834 (5 (if (match-beginning 6)
2835 font-lock-function-name-face
2836 font-lock-variable-name-face
) nil t
)))
2838 ;; Fontify constructors and destructors inside class declarations.
2839 '(font-lock-match-c++-structor-declaration
2840 (3 font-lock-function-name-face t
))
2844 (defvar c
++-font-lock-keywords c
++-font-lock-keywords-1
2845 "Default expressions to highlight in C++ mode.
2846 See also `c++-font-lock-extra-types'.")
2850 (defconst objc-font-lock-keywords-1 nil
2851 "Subdued level highlighting for Objective-C mode.")
2853 (defconst objc-font-lock-keywords-2 nil
2854 "Medium level highlighting for Objective-C mode.
2855 See also `objc-font-lock-extra-types'.")
2857 (defconst objc-font-lock-keywords-3 nil
2858 "Gaudy level highlighting for Objective-C mode.
2859 See also `objc-font-lock-extra-types'.")
2861 ;; Regexps written with help from Stephen Peters <speters@us.oracle.com> and
2862 ;; Jacques Duthen Prestataire <duthen@cegelec-red.fr>.
2863 (let* ((objc-keywords
2865 (regexp-opt '("break" "continue" "do" "else" "for" "if" "return"
2866 "switch" "while" "sizeof" "self" "super"
2867 "typedef" "auto" "extern" "static"
2868 "volatile" "const") t
)))
2872 '("register" "struct" "union" "enum"
2873 "oneway" "in" "out" "inout" "bycopy" "byref") t
)))
2874 (objc-type-specs-depth
2875 (regexp-opt-depth objc-type-specs
))
2877 `(mapconcat 'identity
2879 (,@ (eval-when-compile
2881 '("signed" "unsigned" "short" "long"
2882 "int" "char" "float" "double" "void"
2884 objc-font-lock-extra-types
)
2886 (objc-type-names-depth
2887 `(regexp-opt-depth (,@ objc-type-names
)))
2889 (setq objc-font-lock-keywords-1
2892 ;; The list `c-font-lock-keywords-1' less that for function names.
2893 (cdr c-font-lock-keywords-1
)
2896 ;; Fontify compiler directives.
2898 (1 font-lock-keyword-face
)
2899 ("\\=[ \t:<,]*\\(\\sw+\\)" nil nil
2900 (1 font-lock-type-face
)))
2902 ;; Fontify method names and arguments. Oh Lordy!
2903 ;; First, on the same line as the function declaration.
2904 '("^[+-][ \t]*\\(PRIVATE\\>\\)?[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2905 (1 font-lock-keyword-face nil t
)
2906 (3 font-lock-function-name-face
)
2907 ("\\=[ \t]*\\(\\sw+\\)?:[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2909 (1 font-lock-function-name-face nil t
)
2910 (3 font-lock-variable-name-face
)))
2911 ;; Second, on lines following the function declaration.
2912 '(":" ("^[ \t]*\\(\\sw+\\)?:[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)"
2913 (beginning-of-line) (end-of-line)
2914 (1 font-lock-function-name-face nil t
)
2915 (3 font-lock-variable-name-face
)))
2918 (setq objc-font-lock-keywords-2
2919 (append objc-font-lock-keywords-1
2922 ;; Simple regexps for speed.
2924 ;; Fontify all type specifiers.
2926 (cons (concat "\\<\\(" (,@ objc-type-names
) "\\)\\>")
2927 'font-lock-type-face
))
2929 ;; Fontify all builtin keywords (except case, default and goto; see below).
2930 (concat "\\<\\(" objc-keywords
"\\|" objc-type-specs
"\\)\\>")
2932 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2933 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2934 (1 font-lock-keyword-face
) (2 font-lock-constant-face nil t
))
2935 ;; Fontify tags iff sole statement on line, otherwise we detect selectors.
2936 ;; This must come after the one for keywords and targets.
2937 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2938 (beginning-of-line) (end-of-line)
2939 (1 font-lock-constant-face
)))
2941 ;; Fontify null object pointers.
2942 '("\\<[Nn]il\\>" . font-lock-constant-face
)
2945 (setq objc-font-lock-keywords-3
2946 (append objc-font-lock-keywords-2
2948 ;; More complicated regexps for more complete highlighting for types.
2949 ;; We still have to fontify type specifiers individually, as C is so hairy.
2952 ;; Fontify all storage classes and type specifiers, plus their items.
2954 (list (concat "\\<\\(" (,@ objc-type-names
) "\\)\\>"
2955 "\\([ \t*&]+\\sw+\\>\\)*")
2956 ;; Fontify each declaration item.
2957 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2958 ;; Start with point after all type specifiers.
2960 (list 'or
(list 'match-beginning
2961 (+ (,@ objc-type-names-depth
) 2))
2963 ;; Finish with point after first type specifier.
2964 '(goto-char (match-end 1))
2965 ;; Fontify as a variable or function name.
2966 '(1 (if (match-beginning 2)
2967 font-lock-function-name-face
2968 font-lock-variable-name-face
)))))
2970 ;; Fontify all storage specs and types, plus their items.
2972 (list (concat "\\<\\(" (,@ objc-type-specs
) "[ \t]*\\)+\\>"
2973 "[ \t]*\\(\\sw+\\)?")
2974 ;; The name of any type.
2975 (list (+ (,@ objc-type-specs-depth
) 2) 'font-lock-type-face nil t
)
2976 ;; Fontify each declaration item.
2977 (list 'font-lock-match-c
++-style-declaration-item-and-skip-to-next
2979 ;; Fontify as a variable or function name.
2980 '(1 (if (match-beginning 2)
2981 font-lock-function-name-face
2982 font-lock-variable-name-face
)))
2985 ;; Fontify structures, or typedef names, plus their items.
2986 '("\\(}\\)[ \t*]*\\sw"
2987 (font-lock-match-c-style-declaration-item-and-skip-to-next
2988 (goto-char (match-end 1)) nil
2989 (1 font-lock-type-face
)))
2991 ;; Fontify anything at beginning of line as a declaration or definition.
2992 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2993 (1 font-lock-type-face
)
2994 (font-lock-match-c-style-declaration-item-and-skip-to-next
2995 (goto-char (or (match-beginning 2) (match-end 1))) nil
2996 (1 (if (match-beginning 2)
2997 font-lock-function-name-face
2998 font-lock-variable-name-face
))))
3002 (defvar objc-font-lock-keywords objc-font-lock-keywords-1
3003 "Default expressions to highlight in Objective-C mode.
3004 See also `objc-font-lock-extra-types'.")
3008 (defconst java-font-lock-keywords-1 nil
3009 "Subdued level highlighting for Java mode.")
3011 (defconst java-font-lock-keywords-2 nil
3012 "Medium level highlighting for Java mode.
3013 See also `java-font-lock-extra-types'.")
3015 (defconst java-font-lock-keywords-3 nil
3016 "Gaudy level highlighting for Java mode.
3017 See also `java-font-lock-extra-types'.")
3019 ;; Regexps written with help from Fred White <fwhite@bbn.com>,
3020 ;; Anders Lindgren <andersl@andersl.com> and Carl Manning <caroma@ai.mit.edu>.
3021 (let* ((java-keywords
3024 '("catch" "do" "else" "super" "this" "finally" "for" "if"
3025 ;; Anders Lindgren <andersl@andersl.com> says these have gone.
3026 ;; "cast" "byvalue" "future" "generic" "operator" "var"
3027 ;; "inner" "outer" "rest"
3028 "implements" "extends" "throws" "instanceof" "new"
3029 "interface" "return" "switch" "throw" "try" "while") t
)))
3031 ;; Classes immediately followed by an object name.
3033 `(mapconcat 'identity
3035 (,@ (eval-when-compile
3036 (regexp-opt '("boolean" "char" "byte" "short" "int" "long"
3037 "float" "double" "void"))))
3038 java-font-lock-extra-types
)
3040 (java-type-names-depth `(regexp-opt-depth (,@ java-type-names
)))
3042 ;; These are eventually followed by an object name.
3046 '("abstract" "const" "final" "synchronized" "transient" "static"
3047 ;; Anders Lindgren <andersl@andersl.com> says this has gone.
3049 "volatile" "public" "private" "protected" "native"
3050 ;; Carl Manning <caroma@ai.mit.edu> says this is new.
3053 (setq java-font-lock-keywords-1
3056 ;; Fontify class names.
3057 '("\\<\\(class\\)\\>[ \t]*\\(\\sw+\\)?"
3058 (1 font-lock-keyword-face
) (2 font-lock-type-face nil t
))
3060 ;; Fontify package names in import directives.
3061 '("\\<\\(import\\|package\\)\\>[ \t]*\\(\\sw+\\)?"
3062 (1 font-lock-keyword-face
)
3063 (2 font-lock-constant-face nil t
)
3064 ("\\=\\.\\(\\*\\|\\sw+\\)" nil nil
3065 (1 font-lock-constant-face nil t
)))
3068 (setq java-font-lock-keywords-2
3069 (append java-font-lock-keywords-1
3072 ;; Fontify class names.
3074 (cons (concat "\\<\\(" (,@ java-type-names
) "\\)\\>[^.]")
3075 '(1 font-lock-type-face
)))
3077 ;; Fontify all builtin keywords (except below).
3078 (concat "\\<\\(" java-keywords
"\\|" java-type-specs
"\\)\\>")
3080 ;; Fontify keywords and targets, and case default/goto tags.
3081 (list "\\<\\(break\\|case\\|continue\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
3082 '(1 font-lock-keyword-face
) '(2 font-lock-constant-face nil t
))
3083 ;; This must come after the one for keywords and targets.
3084 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
3085 (beginning-of-line) (end-of-line)
3086 (1 font-lock-constant-face
)))
3088 ;; Fontify all constants.
3089 '("\\<\\(false\\|null\\|true\\)\\>" . font-lock-constant-face
)
3091 ;; Javadoc tags within comments.
3092 '("@\\(author\\|deprecated\\|link\\|return\\|see\\|version\\)\\>"
3093 (1 font-lock-constant-face prepend
))
3094 '("@\\(param\\)\\>[ \t]*\\(\\sw+\\)?"
3095 (1 font-lock-constant-face prepend
)
3096 (2 font-lock-variable-name-face prepend t
))
3097 '("@\\(exception\\|throws\\)\\>[ \t]*\\(\\S-+\\)?"
3098 (1 font-lock-constant-face prepend
)
3099 (2 font-lock-type-face prepend t
))
3102 (setq java-font-lock-keywords-3
3103 (append java-font-lock-keywords-2
3105 ;; More complicated regexps for more complete highlighting for types.
3106 ;; We still have to fontify type specifiers individually, as Java is hairy.
3109 ;; Fontify random types immediately followed by an item or items.
3111 (list (concat "\\<\\(" (,@ java-type-names
) "\\)\\>"
3112 "\\([ \t]*\\[[ \t]*\\]\\)*"
3114 ;; Fontify each declaration item.
3115 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
3116 ;; Start and finish with point after the type specifier.
3117 (list 'goto-char
(list 'match-beginning
3118 (+ (,@ java-type-names-depth
) 3)))
3119 (list 'goto-char
(list 'match-beginning
3120 (+ (,@ java-type-names-depth
) 3)))
3121 ;; Fontify as a variable or function name.
3122 '(1 (if (match-beginning 2)
3123 font-lock-function-name-face
3124 font-lock-variable-name-face
)))))
3126 ;; Fontify those that are eventually followed by an item or items.
3127 (list (concat "\\<\\(" java-type-specs
"\\)\\>"
3129 "\\([ \t]*\\[[ \t]*\\]\\)*"
3131 ;; Fontify each declaration item.
3132 '(font-lock-match-c-style-declaration-item-and-skip-to-next
3133 ;; Start with point after all type specifiers.
3134 (goto-char (or (match-beginning 5) (match-end 1)))
3135 ;; Finish with point after first type specifier.
3136 (goto-char (match-end 1))
3137 ;; Fontify as a variable or function name.
3138 (1 (if (match-beginning 2)
3139 font-lock-function-name-face
3140 font-lock-variable-name-face
))))
3144 (defvar java-font-lock-keywords java-font-lock-keywords-1
3145 "Default expressions to highlight in Java mode.
3146 See also `java-font-lock-extra-types'.")
3148 ;; Install ourselves:
3150 (unless (assq 'font-lock-mode minor-mode-alist
)
3151 (push '(font-lock-mode nil
) minor-mode-alist
))
3153 ;; Provide ourselves:
3155 (provide 'font-lock
)
3157 ;;; font-lock.el ends here