(set-visited-file-name, file-expand-wildcards): Fix docstring.
[emacs.git] / lisp / font-lock.el
blob044c414d84ebdd166f2c04855a7b81f9fd0255e2
1 ;;; font-lock.el --- Electric font lock mode
3 ;; Copyright (C) 1992, 93, 94, 95, 96, 97, 98, 1999, 2000, 2001, 02, 2003, 2004
4 ;; Free Software Foundation, Inc.
6 ;; Author: jwz, then rms, then sm
7 ;; Maintainer: FSF
8 ;; Keywords: languages, faces
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; Font Lock mode is a minor mode that causes your comments to be displayed in
30 ;; one face, strings in another, reserved words in another, and so on.
32 ;; Comments will be displayed in `font-lock-comment-face'.
33 ;; Strings will be displayed in `font-lock-string-face'.
34 ;; Regexps are used to display selected patterns in other faces.
36 ;; To make the text you type be fontified, use M-x font-lock-mode RET.
37 ;; When this minor mode is on, the faces of the current line are updated with
38 ;; every insertion or deletion.
40 ;; To turn Font Lock mode on automatically, add this to your ~/.emacs file:
42 ;; (add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock)
44 ;; Or if you want to turn Font Lock mode on in many modes:
46 ;; (global-font-lock-mode t)
48 ;; Fontification for a particular mode may be available in a number of levels
49 ;; of decoration. The higher the level, the more decoration, but the more time
50 ;; it takes to fontify. See the variable `font-lock-maximum-decoration', and
51 ;; also the variable `font-lock-maximum-size'. Support modes for Font Lock
52 ;; mode can be used to speed up Font Lock mode. See `font-lock-support-mode'.
54 ;;; How Font Lock mode fontifies:
56 ;; When Font Lock mode is turned on in a buffer, it (a) fontifies the entire
57 ;; buffer and (b) installs one of its fontification functions on one of the
58 ;; hook variables that are run by Emacs after every buffer change (i.e., an
59 ;; insertion or deletion). Fontification means the replacement of `face' text
60 ;; properties in a given region; Emacs displays text with these `face' text
61 ;; properties appropriately.
63 ;; Fontification normally involves syntactic (i.e., strings and comments) and
64 ;; regexp (i.e., keywords and everything else) passes. There are actually
65 ;; three passes; (a) the syntactic keyword pass, (b) the syntactic pass and (c)
66 ;; the keyword pass. Confused?
68 ;; The syntactic keyword pass places `syntax-table' text properties in the
69 ;; buffer according to the variable `font-lock-syntactic-keywords'. It is
70 ;; necessary because Emacs' syntax table is not powerful enough to describe all
71 ;; the different syntactic constructs required by the sort of people who decide
72 ;; that a single quote can be syntactic or not depending on the time of day.
73 ;; (What sort of person could decide to overload the meaning of a quote?)
74 ;; Obviously the syntactic keyword pass must occur before the syntactic pass.
76 ;; The syntactic pass places `face' text properties in the buffer according to
77 ;; syntactic context, i.e., according to the buffer's syntax table and buffer
78 ;; text's `syntax-table' text properties. It involves using a syntax parsing
79 ;; function to determine the context of different parts of a region of text. A
80 ;; syntax parsing function is necessary because generally strings and/or
81 ;; comments can span lines, and so the context of a given region is not
82 ;; necessarily apparent from the content of that region. Because the keyword
83 ;; pass only works within a given region, it is not generally appropriate for
84 ;; syntactic fontification. This is the first fontification pass that makes
85 ;; changes visible to the user; it fontifies strings and comments.
87 ;; The keyword pass places `face' text properties in the buffer according to
88 ;; the variable `font-lock-keywords'. It involves searching for given regexps
89 ;; (or calling given search functions) within the given region. This is the
90 ;; second fontification pass that makes changes visible to the user; it
91 ;; fontifies language reserved words, etc.
93 ;; Oh, and the answer is, "Yes, obviously just about everything should be done
94 ;; in a single syntactic pass, but the only syntactic parser available
95 ;; understands only strings and comments." Perhaps one day someone will write
96 ;; some syntactic parsers for common languages and a son-of-font-lock.el could
97 ;; use them rather then relying so heavily on the keyword (regexp) pass.
99 ;;; How Font Lock mode supports modes or is supported by modes:
101 ;; Modes that support Font Lock mode do so by defining one or more variables
102 ;; whose values specify the fontification. Font Lock mode knows of these
103 ;; variable names from (a) the buffer local variable `font-lock-defaults', if
104 ;; non-nil, or (b) the global variable `font-lock-defaults-alist', if the major
105 ;; mode has an entry. (Font Lock mode is set up via (a) where a mode's
106 ;; patterns are distributed with the mode's package library, and (b) where a
107 ;; mode's patterns are distributed with font-lock.el itself. An example of (a)
108 ;; is Pascal mode, an example of (b) is Lisp mode. Normally, the mechanism is
109 ;; (a); (b) is used where it is not clear which package library should contain
110 ;; the pattern definitions.) Font Lock mode chooses which variable to use for
111 ;; fontification based on `font-lock-maximum-decoration'.
113 ;; Font Lock mode fontification behaviour can be modified in a number of ways.
114 ;; See the below comments and the comments distributed throughout this file.
116 ;;; Constructing patterns:
118 ;; See the documentation for the variable `font-lock-keywords'.
120 ;; Efficient regexps for use as MATCHERs for `font-lock-keywords' and
121 ;; `font-lock-syntactic-keywords' can be generated via the function
122 ;; `regexp-opt'.
124 ;;; Adding patterns for modes that already support Font Lock:
126 ;; Though Font Lock highlighting patterns already exist for many modes, it's
127 ;; likely there's something that you want fontified that currently isn't, even
128 ;; at the maximum fontification level. You can add highlighting patterns via
129 ;; `font-lock-add-keywords'. For example, say in some C
130 ;; header file you #define the token `and' to expand to `&&', etc., to make
131 ;; your C code almost readable. In your ~/.emacs there could be:
133 ;; (font-lock-add-keywords 'c-mode '("\\<\\(and\\|or\\|not\\)\\>"))
135 ;; Some modes provide specific ways to modify patterns based on the values of
136 ;; other variables. For example, additional C types can be specified via the
137 ;; variable `c-font-lock-extra-types'.
139 ;;; Adding patterns for modes that do not support Font Lock:
141 ;; Not all modes support Font Lock mode. If you (as a user of the mode) add
142 ;; patterns for a new mode, you must define in your ~/.emacs a variable or
143 ;; variables that specify regexp fontification. Then, you should indicate to
144 ;; Font Lock mode, via the mode hook setting `font-lock-defaults', exactly what
145 ;; support is required. For example, say Foo mode should have the following
146 ;; regexps fontified case-sensitively, and comments and strings should not be
147 ;; fontified automagically. In your ~/.emacs there could be:
149 ;; (defvar foo-font-lock-keywords
150 ;; '(("\\<\\(one\\|two\\|three\\)\\>" . font-lock-keyword-face)
151 ;; ("\\<\\(four\\|five\\|six\\)\\>" . font-lock-type-face))
152 ;; "Default expressions to highlight in Foo mode.")
154 ;; (add-hook 'foo-mode-hook
155 ;; (lambda ()
156 ;; (make-local-variable 'font-lock-defaults)
157 ;; (setq font-lock-defaults '(foo-font-lock-keywords t))))
159 ;;; Adding Font Lock support for modes:
161 ;; Of course, it would be better that the mode already supports Font Lock mode.
162 ;; The package author would do something similar to above. The mode must
163 ;; define at the top-level a variable or variables that specify regexp
164 ;; fontification. Then, the mode command should indicate to Font Lock mode,
165 ;; via `font-lock-defaults', exactly what support is required. For example,
166 ;; say Bar mode should have the following regexps fontified case-insensitively,
167 ;; and comments and strings should be fontified automagically. In bar.el there
168 ;; could be:
170 ;; (defvar bar-font-lock-keywords
171 ;; '(("\\<\\(uno\\|due\\|tre\\)\\>" . font-lock-keyword-face)
172 ;; ("\\<\\(quattro\\|cinque\\|sei\\)\\>" . font-lock-type-face))
173 ;; "Default expressions to highlight in Bar mode.")
175 ;; and within `bar-mode' there could be:
177 ;; (make-local-variable 'font-lock-defaults)
178 ;; (setq font-lock-defaults '(bar-font-lock-keywords nil t))
180 ;; What is fontification for? You might say, "It's to make my code look nice."
181 ;; I think it should be for adding information in the form of cues. These cues
182 ;; should provide you with enough information to both (a) distinguish between
183 ;; different items, and (b) identify the item meanings, without having to read
184 ;; the items and think about it. Therefore, fontification allows you to think
185 ;; less about, say, the structure of code, and more about, say, why the code
186 ;; doesn't work. Or maybe it allows you to think less and drift off to sleep.
188 ;; So, here are my opinions/advice/guidelines:
190 ;; - Highlight conceptual objects, such as function and variable names, and
191 ;; different objects types differently, i.e., (a) and (b) above, highlight
192 ;; function names differently to variable names.
193 ;; - Keep the faces distinct from each other as far as possible.
194 ;; i.e., (a) above.
195 ;; - Use the same face for the same conceptual object, across all modes.
196 ;; i.e., (b) above, all modes that have items that can be thought of as, say,
197 ;; keywords, should be highlighted with the same face, etc.
198 ;; - Make the face attributes fit the concept as far as possible.
199 ;; i.e., function names might be a bold colour such as blue, comments might
200 ;; be a bright colour such as red, character strings might be brown, because,
201 ;; err, strings are brown (that was not the reason, please believe me).
202 ;; - Don't use a non-nil OVERRIDE unless you have a good reason.
203 ;; Only use OVERRIDE for special things that are easy to define, such as the
204 ;; way `...' quotes are treated in strings and comments in Emacs Lisp mode.
205 ;; Don't use it to, say, highlight keywords in commented out code or strings.
206 ;; - Err, that's it.
208 ;;; Code:
210 (require 'syntax)
212 ;; Define core `font-lock' group.
213 (defgroup font-lock '((jit-lock custom-group))
214 "Font Lock mode text highlighting package."
215 :link '(custom-manual "(emacs)Font Lock")
216 :link '(custom-manual "(elisp)Font Lock Mode")
217 :group 'faces)
219 (defgroup font-lock-highlighting-faces nil
220 "Faces for highlighting text."
221 :prefix "font-lock-"
222 :group 'font-lock)
224 (defgroup font-lock-extra-types nil
225 "Extra mode-specific type names for highlighting declarations."
226 :group 'font-lock)
228 ;; Define support mode groups here to impose `font-lock' group order.
229 (defgroup fast-lock nil
230 "Font Lock support mode to cache fontification."
231 :load 'fast-lock
232 :group 'font-lock)
234 (defgroup lazy-lock nil
235 "Font Lock support mode to fontify lazily."
236 :load 'lazy-lock
237 :group 'font-lock)
239 ;; User variables.
241 (defcustom font-lock-maximum-size 256000
242 "*Maximum size of a buffer for buffer fontification.
243 Only buffers less than this can be fontified when Font Lock mode is turned on.
244 If nil, means size is irrelevant.
245 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
246 where MAJOR-MODE is a symbol or t (meaning the default). For example:
247 ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576))
248 means that the maximum size is 250K for buffers in C or C++ modes, one megabyte
249 for buffers in Rmail mode, and size is irrelevant otherwise."
250 :type '(choice (const :tag "none" nil)
251 (integer :tag "size")
252 (repeat :menu-tag "mode specific" :tag "mode specific"
253 :value ((t . nil))
254 (cons :tag "Instance"
255 (radio :tag "Mode"
256 (const :tag "all" t)
257 (symbol :tag "name"))
258 (radio :tag "Size"
259 (const :tag "none" nil)
260 (integer :tag "size")))))
261 :group 'font-lock)
263 (defcustom font-lock-maximum-decoration t
264 "*Maximum decoration level for fontification.
265 If nil, use the default decoration (typically the minimum available).
266 If t, use the maximum decoration available.
267 If a number, use that level of decoration (or if not available the maximum).
268 If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
269 where MAJOR-MODE is a symbol or t (meaning the default). For example:
270 ((c-mode . t) (c++-mode . 2) (t . 1))
271 means use the maximum decoration available for buffers in C mode, level 2
272 decoration for buffers in C++ mode, and level 1 decoration otherwise."
273 :type '(choice (const :tag "default" nil)
274 (const :tag "maximum" t)
275 (integer :tag "level" 1)
276 (repeat :menu-tag "mode specific" :tag "mode specific"
277 :value ((t . t))
278 (cons :tag "Instance"
279 (radio :tag "Mode"
280 (const :tag "all" t)
281 (symbol :tag "name"))
282 (radio :tag "Decoration"
283 (const :tag "default" nil)
284 (const :tag "maximum" t)
285 (integer :tag "level" 1)))))
286 :group 'font-lock)
288 (defcustom font-lock-verbose 0
289 "*If non-nil, means show status messages for buffer fontification.
290 If a number, only buffers greater than this size have fontification messages."
291 :type '(choice (const :tag "never" nil)
292 (other :tag "always" t)
293 (integer :tag "size"))
294 :group 'font-lock)
297 ;; Originally these variable values were face names such as `bold' etc.
298 ;; Now we create our own faces, but we keep these variables for compatibility
299 ;; and they give users another mechanism for changing face appearance.
300 ;; We now allow a FACENAME in `font-lock-keywords' to be any expression that
301 ;; returns a face. So the easiest thing is to continue using these variables,
302 ;; rather than sometimes evaling FACENAME and sometimes not. sm.
303 (defvar font-lock-comment-face 'font-lock-comment-face
304 "Face name to use for comments.")
306 (defvar font-lock-string-face 'font-lock-string-face
307 "Face name to use for strings.")
309 (defvar font-lock-doc-face 'font-lock-doc-face
310 "Face name to use for documentation.")
312 (defvar font-lock-keyword-face 'font-lock-keyword-face
313 "Face name to use for keywords.")
315 (defvar font-lock-builtin-face 'font-lock-builtin-face
316 "Face name to use for builtins.")
318 (defvar font-lock-function-name-face 'font-lock-function-name-face
319 "Face name to use for function names.")
321 (defvar font-lock-variable-name-face 'font-lock-variable-name-face
322 "Face name to use for variable names.")
324 (defvar font-lock-type-face 'font-lock-type-face
325 "Face name to use for type and class names.")
327 (defvar font-lock-constant-face 'font-lock-constant-face
328 "Face name to use for constant and label names.")
330 (defvar font-lock-warning-face 'font-lock-warning-face
331 "Face name to use for things that should stand out.")
333 (defvar font-lock-preprocessor-face 'font-lock-preprocessor-face
334 "Face name to use for preprocessor directives.")
336 (defvar font-lock-reference-face 'font-lock-constant-face)
337 (make-obsolete-variable 'font-lock-reference-face 'font-lock-constant-face)
339 ;; Fontification variables:
341 (defvar font-lock-keywords nil
342 "A list of the keywords to highlight.
343 Each element should have one of these forms:
345 MATCHER
346 (MATCHER . MATCH)
347 (MATCHER . FACENAME)
348 (MATCHER . HIGHLIGHT)
349 (MATCHER HIGHLIGHT ...)
350 (eval . FORM)
352 where MATCHER can be either the regexp to search for, or the function name to
353 call to make the search (called with one argument, the limit of the search) and
354 return non-nil if it succeeds (and set `match-data' appropriately).
355 MATCHER regexps can be generated via the function `regexp-opt'.
357 FORM is an expression, whose value should be a keyword element, evaluated when
358 the keyword is (first) used in a buffer. This feature can be used to provide a
359 keyword that can only be generated when Font Lock mode is actually turned on.
361 HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
363 For highlighting single items, for example each instance of the word \"foo\",
364 typically only MATCH-HIGHLIGHT is required.
365 However, if an item or (typically) items are to be highlighted following the
366 instance of another item (the anchor), for example each instance of the
367 word \"bar\" following the word \"anchor\" then MATCH-ANCHORED may be required.
369 MATCH-HIGHLIGHT should be of the form:
371 (MATCH FACENAME OVERRIDE LAXMATCH)
373 MATCH is the subexpression of MATCHER to be highlighted. FACENAME is an
374 expression whose value is the face name to use. Face default attributes
375 can be modified via \\[customize]. Instead of a face, FACENAME can
376 evaluate to a property list of the form (face VAL1 PROP2 VAL2 PROP3 VAL3 ...)
377 in which case all the listed text-properties will be set rather than
378 just `face'. In such a case, you will most likely want to put those
379 properties in `font-lock-extra-managed-props' or to override
380 `font-lock-unfontify-region-function'.
382 OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification can
383 be overwritten. If `keep', only parts not already fontified are highlighted.
384 If `prepend' or `append', existing fontification is merged with the new, in
385 which the new or existing fontification, respectively, takes precedence.
386 If LAXMATCH is non-nil, no error is signaled if there is no MATCH in MATCHER.
388 For example, an element of the form highlights (if not already highlighted):
390 \"\\\\\\=<foo\\\\\\=>\" discrete occurrences of \"foo\" in the value of the
391 variable `font-lock-keyword-face'.
392 (\"fu\\\\(bar\\\\)\" . 1) substring \"bar\" within all occurrences of \"fubar\" in
393 the value of `font-lock-keyword-face'.
394 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'.
395 (\"foo\\\\|bar\" 0 foo-bar-face t)
396 occurrences of either \"foo\" or \"bar\" in the value
397 of `foo-bar-face', even if already highlighted.
398 (fubar-match 1 fubar-face)
399 the first subexpression within all occurrences of
400 whatever the function `fubar-match' finds and matches
401 in the value of `fubar-face'.
403 MATCH-ANCHORED should be of the form:
405 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
407 where MATCHER is a regexp to search for or the function name to call to make
408 the search, as for MATCH-HIGHLIGHT above, but with one exception; see below.
409 PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
410 the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be
411 used to initialise before, and cleanup after, MATCHER is used. Typically,
412 PRE-MATCH-FORM is used to move to some position relative to the original
413 MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might
414 be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER.
416 For example, an element of the form highlights (if not already highlighted):
418 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
420 discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
421 discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
422 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is
423 initially searched for starting from the end of the match of \"anchor\", and
424 searching for subsequent instance of \"anchor\" resumes from where searching
425 for \"item\" concluded.)
427 The above-mentioned exception is as follows. The limit of the MATCHER search
428 defaults to the end of the line after PRE-MATCH-FORM is evaluated.
429 However, if PRE-MATCH-FORM returns a position greater than the position after
430 PRE-MATCH-FORM is evaluated, that position is used as the limit of the search.
431 It is generally a bad idea to return a position greater than the end of the
432 line, i.e., cause the MATCHER search to span lines.
434 These regular expressions can match text which spans lines, although
435 it is better to avoid it if possible since updating them while editing
436 text is slower, and it is not guaranteed to be always correct when using
437 support modes like jit-lock or lazy-lock.
439 This variable is set by major modes via the variable `font-lock-defaults'.
440 Be careful when composing regexps for this list; a poorly written pattern can
441 dramatically slow things down!")
443 (defvar font-lock-keywords-alist nil
444 "*Alist of `font-lock-keywords' local to a `major-mode'.
445 This is normally set via `font-lock-add-keywords' and
446 `font-lock-remove-keywords'.")
448 (defvar font-lock-removed-keywords-alist nil
449 "*Alist of `font-lock-keywords' removed from `major-mode'.
450 This is normally set via `font-lock-add-keywords' and
451 `font-lock-remove-keywords'.")
453 (defvar font-lock-keywords-only nil
454 "*Non-nil means Font Lock should not fontify comments or strings.
455 This is normally set via `font-lock-defaults'.")
457 (defvar font-lock-keywords-case-fold-search nil
458 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
459 This is normally set via `font-lock-defaults'.")
460 (make-variable-buffer-local 'font-lock-keywords-case-fold-search)
462 (defvar font-lock-syntactically-fontified 0
463 "Point up to which `font-lock-syntactic-keywords' has been applied.
464 If nil, this is ignored, in which case the syntactic fontification may
465 sometimes be slightly incorrect.")
466 (make-variable-buffer-local 'font-lock-syntactically-fontified)
468 (defvar font-lock-syntactic-face-function
469 (lambda (state)
470 (if (nth 3 state) font-lock-string-face font-lock-comment-face))
471 "Function to determine which face to use when fontifying syntactically.
472 The function is called with a single parameter (the state as returned by
473 `parse-partial-sexp' at the beginning of the region to highlight) and
474 should return a face.")
476 (defvar font-lock-syntactic-keywords nil
477 "A list of the syntactic keywords to highlight.
478 Can be the list or the name of a function or variable whose value is the list.
479 See `font-lock-keywords' for a description of the form of this list;
480 the differences are listed below. MATCH-HIGHLIGHT should be of the form:
482 (MATCH SYNTAX OVERRIDE LAXMATCH)
484 where SYNTAX can be a string (as taken by `modify-syntax-entry'), a syntax
485 table, a cons cell (as returned by `string-to-syntax') or an expression whose
486 value is such a form. OVERRIDE cannot be `prepend' or `append'.
488 For example, an element of the form highlights syntactically:
490 (\"\\\\$\\\\(#\\\\)\" 1 \".\")
492 a hash character when following a dollar character, with a SYNTAX of
493 \".\" (meaning punctuation syntax). Assuming that the buffer syntax table does
494 specify hash characters to have comment start syntax, the element will only
495 highlight hash characters that do not follow dollar characters as comments
496 syntactically.
498 (\"\\\\('\\\\).\\\\('\\\\)\"
499 (1 \"\\\"\")
500 (2 \"\\\"\"))
502 both single quotes which surround a single character, with a SYNTAX of
503 \"\\\"\" (meaning string quote syntax). Assuming that the buffer syntax table
504 does not specify single quotes to have quote syntax, the element will only
505 highlight single quotes of the form 'c' as strings syntactically.
506 Other forms, such as foo'bar or 'fubar', will not be highlighted as strings.
508 This is normally set via `font-lock-defaults'.")
510 (defvar font-lock-syntax-table nil
511 "Non-nil means use this syntax table for fontifying.
512 If this is nil, the major mode's syntax table is used.
513 This is normally set via `font-lock-defaults'.")
515 (defvar font-lock-beginning-of-syntax-function nil
516 "*Non-nil means use this function to move back outside all constructs.
517 When called with no args it should move point backward to a place which
518 is not in a string or comment and not within any bracket-pairs (or else,
519 a place such that any bracket-pairs outside it can be ignored for Emacs
520 syntax analysis and fontification).
522 If this is nil, the beginning of the buffer is used, which is
523 always correct but tends to be slow.
524 This is normally set via `font-lock-defaults'.
525 This variable is semi-obsolete; we recommend setting
526 `syntax-begin-function' instead.")
528 (defvar font-lock-mark-block-function nil
529 "*Non-nil means use this function to mark a block of text.
530 When called with no args it should leave point at the beginning of any
531 enclosing textual block and mark at the end.
532 This is normally set via `font-lock-defaults'.")
534 (defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
535 "Function to use for fontifying the buffer.
536 This is normally set via `font-lock-defaults'.")
538 (defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
539 "Function to use for unfontifying the buffer.
540 This is used when turning off Font Lock mode.
541 This is normally set via `font-lock-defaults'.")
543 (defvar font-lock-fontify-region-function 'font-lock-default-fontify-region
544 "Function to use for fontifying a region.
545 It should take two args, the beginning and end of the region, and an optional
546 third arg VERBOSE. If non-nil, the function should print status messages.
547 This is normally set via `font-lock-defaults'.")
549 (defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
550 "Function to use for unfontifying a region.
551 It should take two args, the beginning and end of the region.
552 This is normally set via `font-lock-defaults'.")
554 (defvar font-lock-inhibit-thing-lock nil
555 "List of Font Lock mode related modes that should not be turned on.
556 Currently, valid mode names are `fast-lock-mode', `jit-lock-mode' and
557 `lazy-lock-mode'. This is normally set via `font-lock-defaults'.")
559 (defvar font-lock-multiline nil
560 "Whether font-lock should cater to multiline keywords.
561 If nil, don't try to handle multiline patterns.
562 If t, always handle multiline patterns.
563 If `undecided', don't try to handle multiline patterns until you see one.
564 Major/minor modes can set this variable if they know which option applies.")
566 (defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
568 ;; Font Lock mode.
570 (eval-when-compile
572 ;; We don't do this at the top-level as we only use non-autoloaded macros.
573 (require 'cl)
575 ;; Borrowed from lazy-lock.el.
576 ;; We use this to preserve or protect things when modifying text properties.
577 (defmacro save-buffer-state (varlist &rest body)
578 "Bind variables according to VARLIST and eval BODY restoring buffer state."
579 (let ((modified (make-symbol "modified")))
580 `(let* ,(append varlist
581 `((,modified (buffer-modified-p))
582 (buffer-undo-list t)
583 (inhibit-read-only t)
584 (inhibit-point-motion-hooks t)
585 (inhibit-modification-hooks t)
586 deactivate-mark
587 buffer-file-name
588 buffer-file-truename))
589 (progn
590 ,@body)
591 (unless ,modified
592 (restore-buffer-modified-p nil)))))
593 (put 'save-buffer-state 'lisp-indent-function 1)
594 (def-edebug-spec save-buffer-state let)
596 ;; Shut up the byte compiler.
597 (defvar font-lock-face-attributes)) ; Obsolete but respected if set.
599 ;;;###autoload
600 (defun font-lock-mode-internal (arg)
601 ;; Turn on Font Lock mode.
602 (when arg
603 (add-hook 'after-change-functions 'font-lock-after-change-function t t)
604 (font-lock-set-defaults)
605 (font-lock-turn-on-thing-lock)
606 ;; Fontify the buffer if we have to.
607 (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size)))
608 (cond (font-lock-fontified
609 nil)
610 ((or (null max-size) (> max-size (buffer-size)))
611 (font-lock-fontify-buffer))
612 (font-lock-verbose
613 (message "Fontifying %s...buffer size greater than font-lock-maximum-size"
614 (buffer-name))))))
615 ;; Turn off Font Lock mode.
616 (unless font-lock-mode
617 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
618 (font-lock-unfontify-buffer)
619 (font-lock-turn-off-thing-lock)))
621 ;;;###autoload
622 (defun font-lock-add-keywords (mode keywords &optional append)
623 "Add highlighting KEYWORDS for MODE.
624 MODE should be a symbol, the major mode command name, such as `c-mode'
625 or nil. If nil, highlighting keywords are added for the current buffer.
626 KEYWORDS should be a list; see the variable `font-lock-keywords'.
627 By default they are added at the beginning of the current highlighting list.
628 If optional argument APPEND is `set', they are used to replace the current
629 highlighting list. If APPEND is any other non-nil value, they are added at the
630 end of the current highlighting list.
632 For example:
634 (font-lock-add-keywords 'c-mode
635 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
636 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . font-lock-keyword-face)))
638 adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
639 comments, and to fontify `and', `or' and `not' words as keywords.
641 When used from an elisp package (such as a minor mode), it is recommended
642 to use nil for MODE (and place the call in a loop or on a hook) to avoid
643 subtle problems due to details of the implementation.
645 Note that some modes have specialized support for additional patterns, e.g.,
646 see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
647 `objc-font-lock-extra-types' and `java-font-lock-extra-types'."
648 (cond (mode
649 ;; If MODE is non-nil, add the KEYWORDS and APPEND spec to
650 ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
651 (let ((spec (cons keywords append)) cell)
652 (if (setq cell (assq mode font-lock-keywords-alist))
653 (if (eq append 'set)
654 (setcdr cell (list spec))
655 (setcdr cell (append (cdr cell) (list spec))))
656 (push (list mode spec) font-lock-keywords-alist)))
657 ;; Make sure that `font-lock-removed-keywords-alist' does not
658 ;; contain the new keywords.
659 (font-lock-update-removed-keyword-alist mode keywords append))
661 ;; Otherwise set or add the keywords now.
662 (font-lock-set-defaults)
663 (if (eq append 'set)
664 (setq font-lock-keywords keywords)
665 (font-lock-remove-keywords nil keywords) ;to avoid duplicates
666 (let ((old (if (eq (car-safe font-lock-keywords) t)
667 (cdr font-lock-keywords)
668 font-lock-keywords)))
669 (setq font-lock-keywords (if append
670 (append old keywords)
671 (append keywords old))))))))
673 (defun font-lock-update-removed-keyword-alist (mode keywords append)
674 ;; Update `font-lock-removed-keywords-alist' when adding new
675 ;; KEYWORDS to MODE.
677 ;; When font-lock is enabled first all keywords in the list
678 ;; `font-lock-keywords-alist' are added, then all keywords in the
679 ;; list `font-lock-removed-keywords-alist' are removed. If a
680 ;; keyword was once added, removed, and then added again it must be
681 ;; removed from the removed-keywords list. Otherwise the second add
682 ;; will not take effect.
683 (let ((cell (assq mode font-lock-removed-keywords-alist)))
684 (if cell
685 (if (eq append 'set)
686 ;; A new set of keywords is defined. Forget all about
687 ;; our old keywords that should be removed.
688 (setq font-lock-removed-keywords-alist
689 (delq cell font-lock-removed-keywords-alist))
690 ;; Delete all previously removed keywords.
691 (dolist (kword keywords)
692 (setcdr cell (delete kword (cdr cell))))
693 ;; Delete the mode cell if empty.
694 (if (null (cdr cell))
695 (setq font-lock-removed-keywords-alist
696 (delq cell font-lock-removed-keywords-alist)))))))
698 ;; Written by Anders Lindgren <andersl@andersl.com>.
700 ;; Case study:
701 ;; (I) The keywords are removed from a major mode.
702 ;; In this case the keyword could be local (i.e. added earlier by
703 ;; `font-lock-add-keywords'), global, or both.
705 ;; (a) In the local case we remove the keywords from the variable
706 ;; `font-lock-keywords-alist'.
708 ;; (b) The actual global keywords are not known at this time.
709 ;; All keywords are added to `font-lock-removed-keywords-alist',
710 ;; when font-lock is enabled those keywords are removed.
712 ;; Note that added keywords are taken out of the list of removed
713 ;; keywords. This ensure correct operation when the same keyword
714 ;; is added and removed several times.
716 ;; (II) The keywords are removed from the current buffer.
717 ;;;###autoload
718 (defun font-lock-remove-keywords (mode keywords)
719 "Remove highlighting KEYWORDS for MODE.
721 MODE should be a symbol, the major mode command name, such as `c-mode'
722 or nil. If nil, highlighting keywords are removed for the current buffer.
724 When used from an elisp package (such as a minor mode), it is recommended
725 to use nil for MODE (and place the call in a loop or on a hook) to avoid
726 subtle problems due to details of the implementation."
727 (cond (mode
728 ;; Remove one keyword at the time.
729 (dolist (keyword keywords)
730 (let ((top-cell (assq mode font-lock-keywords-alist)))
731 ;; If MODE is non-nil, remove the KEYWORD from
732 ;; `font-lock-keywords-alist'.
733 (when top-cell
734 (dolist (keyword-list-append-pair (cdr top-cell))
735 ;; `keywords-list-append-pair' is a cons with a list of
736 ;; keywords in the car top-cell and the original append
737 ;; argument in the cdr top-cell.
738 (setcar keyword-list-append-pair
739 (delete keyword (car keyword-list-append-pair))))
740 ;; Remove keyword list/append pair when the keyword list
741 ;; is empty and append doesn't specify `set'. (If it
742 ;; should be deleted then previously deleted keywords
743 ;; would appear again.)
744 (let ((cell top-cell))
745 (while (cdr cell)
746 (if (and (null (car (car (cdr cell))))
747 (not (eq (cdr (car (cdr cell))) 'set)))
748 (setcdr cell (cdr (cdr cell)))
749 (setq cell (cdr cell)))))
750 ;; Final cleanup, remove major mode cell if last keyword
751 ;; was deleted.
752 (if (null (cdr top-cell))
753 (setq font-lock-keywords-alist
754 (delq top-cell font-lock-keywords-alist))))
755 ;; Remember the keyword in case it is not local.
756 (let ((cell (assq mode font-lock-removed-keywords-alist)))
757 (if cell
758 (unless (member keyword (cdr cell))
759 (nconc cell (list keyword)))
760 (push (cons mode (list keyword))
761 font-lock-removed-keywords-alist))))))
763 ;; Otherwise remove it immediately.
764 (font-lock-set-defaults)
765 (setq font-lock-keywords (copy-sequence font-lock-keywords))
766 (dolist (keyword keywords)
767 (setq font-lock-keywords
768 (delete keyword
769 ;; The keywords might be compiled.
770 (delete (font-lock-compile-keyword keyword)
771 font-lock-keywords)))))))
773 ;;; Font Lock Support mode.
775 ;; This is the code used to interface font-lock.el with any of its add-on
776 ;; packages, and provide the user interface. Packages that have their own
777 ;; local buffer fontification functions (see below) may have to call
778 ;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
779 ;; themselves.
781 (defcustom font-lock-support-mode 'jit-lock-mode
782 "*Support mode for Font Lock mode.
783 Support modes speed up Font Lock mode by being choosy about when fontification
784 occurs. Known support modes are Fast Lock mode (symbol `fast-lock-mode'),
785 Lazy Lock mode (symbol `lazy-lock-mode'), and Just-in-time Lock mode (symbol
786 `jit-lock-mode'. See those modes for more info.
787 If nil, means support for Font Lock mode is never performed.
788 If a symbol, use that support mode.
789 If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
790 where MAJOR-MODE is a symbol or t (meaning the default). For example:
791 ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
792 means that Fast Lock mode is used to support Font Lock mode for buffers in C or
793 C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
795 The value of this variable is used when Font Lock mode is turned on."
796 :type '(choice (const :tag "none" nil)
797 (const :tag "fast lock" fast-lock-mode)
798 (const :tag "lazy lock" lazy-lock-mode)
799 (const :tag "jit lock" jit-lock-mode)
800 (repeat :menu-tag "mode specific" :tag "mode specific"
801 :value ((t . jit-lock-mode))
802 (cons :tag "Instance"
803 (radio :tag "Mode"
804 (const :tag "all" t)
805 (symbol :tag "name"))
806 (radio :tag "Support"
807 (const :tag "none" nil)
808 (const :tag "fast lock" fast-lock-mode)
809 (const :tag "lazy lock" lazy-lock-mode)
810 (const :tag "JIT lock" jit-lock-mode)))
812 :version "21.1"
813 :group 'font-lock)
815 (defvar fast-lock-mode)
816 (defvar lazy-lock-mode)
817 (defvar jit-lock-mode)
819 (defun font-lock-turn-on-thing-lock ()
820 (let ((thing-mode (font-lock-value-in-major-mode font-lock-support-mode)))
821 (cond ((eq thing-mode 'fast-lock-mode)
822 (fast-lock-mode t))
823 ((eq thing-mode 'lazy-lock-mode)
824 (lazy-lock-mode t))
825 ((eq thing-mode 'jit-lock-mode)
826 ;; Prepare for jit-lock
827 (remove-hook 'after-change-functions
828 'font-lock-after-change-function t)
829 (set (make-local-variable 'font-lock-fontify-buffer-function)
830 'jit-lock-refontify)
831 ;; Don't fontify eagerly (and don't abort is the buffer is large).
832 (set (make-local-variable 'font-lock-fontified) t)
833 ;; Use jit-lock.
834 (jit-lock-register 'font-lock-fontify-region
835 (not font-lock-keywords-only))))))
837 (defun font-lock-turn-off-thing-lock ()
838 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
839 (fast-lock-mode -1))
840 ((and (boundp 'jit-lock-mode) jit-lock-mode)
841 (jit-lock-unregister 'font-lock-fontify-region)
842 ;; Reset local vars to the non-jit-lock case.
843 (kill-local-variable 'font-lock-fontify-buffer-function))
844 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
845 (lazy-lock-mode -1))))
847 (defun font-lock-after-fontify-buffer ()
848 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
849 (fast-lock-after-fontify-buffer))
850 ;; Useless now that jit-lock intercepts font-lock-fontify-buffer. -sm
851 ;; (jit-lock-mode
852 ;; (jit-lock-after-fontify-buffer))
853 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
854 (lazy-lock-after-fontify-buffer))))
856 (defun font-lock-after-unfontify-buffer ()
857 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
858 (fast-lock-after-unfontify-buffer))
859 ;; Useless as well. It's only called when:
860 ;; - turning off font-lock: it does not matter if we leave spurious
861 ;; `fontified' text props around since jit-lock-mode is also off.
862 ;; - font-lock-default-fontify-buffer fails: this is not run
863 ;; any more anyway. -sm
865 ;; (jit-lock-mode
866 ;; (jit-lock-after-unfontify-buffer))
867 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
868 (lazy-lock-after-unfontify-buffer))))
870 ;;; End of Font Lock Support mode.
872 ;;; Fontification functions.
874 ;; Rather than the function, e.g., `font-lock-fontify-region' containing the
875 ;; code to fontify a region, the function runs the function whose name is the
876 ;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally,
877 ;; the value of this variable is, e.g., `font-lock-default-fontify-region'
878 ;; which does contain the code to fontify a region. However, the value of the
879 ;; variable could be anything and thus, e.g., `font-lock-fontify-region' could
880 ;; do anything. The indirection of the fontification functions gives major
881 ;; modes the capability of modifying the way font-lock.el fontifies. Major
882 ;; modes can modify the values of, e.g., `font-lock-fontify-region-function',
883 ;; via the variable `font-lock-defaults'.
885 ;; For example, Rmail mode sets the variable `font-lock-defaults' so that
886 ;; font-lock.el uses its own function for buffer fontification. This function
887 ;; makes fontification be on a message-by-message basis and so visiting an
888 ;; RMAIL file is much faster. A clever implementation of the function might
889 ;; fontify the headers differently than the message body. (It should, and
890 ;; correspondingly for Mail mode, but I can't be bothered to do the work. Can
891 ;; you?) This hints at a more interesting use...
893 ;; Languages that contain text normally contained in different major modes
894 ;; could define their own fontification functions that treat text differently
895 ;; depending on its context. For example, Perl mode could arrange that here
896 ;; docs are fontified differently than Perl code. Or Yacc mode could fontify
897 ;; rules one way and C code another. Neat!
899 ;; A further reason to use the fontification indirection feature is when the
900 ;; default syntactual fontification, or the default fontification in general,
901 ;; is not flexible enough for a particular major mode. For example, perhaps
902 ;; comments are just too hairy for `font-lock-fontify-syntactically-region' to
903 ;; cope with. You need to write your own version of that function, e.g.,
904 ;; `hairy-fontify-syntactically-region', and make your own version of
905 ;; `hairy-fontify-region' call that function before calling
906 ;; `font-lock-fontify-keywords-region' for the normal regexp fontification
907 ;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el
908 ;; would call your region fontification function instead of its own. For
909 ;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line
910 ;; directives correctly and cleanly. (It is the same problem as fontifying
911 ;; multi-line strings and comments; regexps are not appropriate for the job.)
913 ;;;###autoload
914 (defun font-lock-fontify-buffer ()
915 "Fontify the current buffer the way the function `font-lock-mode' would."
916 (interactive)
917 (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
918 (funcall font-lock-fontify-buffer-function)))
920 (defun font-lock-unfontify-buffer ()
921 (funcall font-lock-unfontify-buffer-function))
923 (defun font-lock-fontify-region (beg end &optional loudly)
924 (funcall font-lock-fontify-region-function beg end loudly))
926 (defun font-lock-unfontify-region (beg end)
927 (funcall font-lock-unfontify-region-function beg end))
929 (defun font-lock-default-fontify-buffer ()
930 (let ((verbose (if (numberp font-lock-verbose)
931 (> (buffer-size) font-lock-verbose)
932 font-lock-verbose)))
933 (with-temp-message
934 (when verbose
935 (format "Fontifying %s..." (buffer-name)))
936 ;; Make sure we have the right `font-lock-keywords' etc.
937 (unless font-lock-mode
938 (font-lock-set-defaults))
939 ;; Make sure we fontify etc. in the whole buffer.
940 (save-restriction
941 (widen)
942 (condition-case nil
943 (save-excursion
944 (save-match-data
945 (font-lock-fontify-region (point-min) (point-max) verbose)
946 (font-lock-after-fontify-buffer)
947 (setq font-lock-fontified t)))
948 ;; We don't restore the old fontification, so it's best to unfontify.
949 (quit (font-lock-unfontify-buffer)))))))
951 (defun font-lock-default-unfontify-buffer ()
952 ;; Make sure we unfontify etc. in the whole buffer.
953 (save-restriction
954 (widen)
955 (font-lock-unfontify-region (point-min) (point-max))
956 (font-lock-after-unfontify-buffer)
957 (setq font-lock-fontified nil)))
959 (defvar font-lock-dont-widen nil
960 "If non-nil, font-lock will work on the non-widened buffer.
961 Useful for things like RMAIL and Info where the whole buffer is not
962 a very meaningful entity to highlight.")
964 (defun font-lock-default-fontify-region (beg end loudly)
965 (save-buffer-state
966 ((parse-sexp-lookup-properties font-lock-syntactic-keywords)
967 (old-syntax-table (syntax-table)))
968 (unwind-protect
969 (save-restriction
970 (unless font-lock-dont-widen (widen))
971 ;; Use the fontification syntax table, if any.
972 (when font-lock-syntax-table
973 (set-syntax-table font-lock-syntax-table))
974 ;; check to see if we should expand the beg/end area for
975 ;; proper multiline matches
976 (when (and font-lock-multiline
977 (> beg (point-min))
978 (get-text-property (1- beg) 'font-lock-multiline))
979 ;; We are just after or in a multiline match.
980 (setq beg (or (previous-single-property-change
981 beg 'font-lock-multiline)
982 (point-min)))
983 (goto-char beg)
984 (setq beg (line-beginning-position)))
985 (when font-lock-multiline
986 (setq end (or (text-property-any end (point-max)
987 'font-lock-multiline nil)
988 (point-max))))
989 (goto-char end)
990 (setq end (line-beginning-position 2))
991 ;; Now do the fontification.
992 (font-lock-unfontify-region beg end)
993 (when font-lock-syntactic-keywords
994 (font-lock-fontify-syntactic-keywords-region beg end))
995 (unless font-lock-keywords-only
996 (font-lock-fontify-syntactically-region beg end loudly))
997 (font-lock-fontify-keywords-region beg end loudly))
998 ;; Clean up.
999 (set-syntax-table old-syntax-table))))
1001 ;; The following must be rethought, since keywords can override fontification.
1002 ; ;; Now scan for keywords, but not if we are inside a comment now.
1003 ; (or (and (not font-lock-keywords-only)
1004 ; (let ((state (parse-partial-sexp beg end nil nil
1005 ; font-lock-cache-state)))
1006 ; (or (nth 4 state) (nth 7 state))))
1007 ; (font-lock-fontify-keywords-region beg end))
1009 (defvar font-lock-extra-managed-props nil
1010 "Additional text properties managed by font-lock.
1011 This is used by `font-lock-default-unfontify-region' to decide
1012 what properties to clear before refontifying a region.")
1014 (defun font-lock-default-unfontify-region (beg end)
1015 (save-buffer-state nil
1016 (remove-list-of-text-properties
1017 beg end (append
1018 font-lock-extra-managed-props
1019 (if font-lock-syntactic-keywords
1020 '(syntax-table face font-lock-multiline)
1021 '(face font-lock-multiline))))))
1023 ;; Called when any modification is made to buffer text.
1024 (defun font-lock-after-change-function (beg end old-len)
1025 (let ((inhibit-point-motion-hooks t)
1026 (inhibit-quit t))
1027 (save-excursion
1028 (save-match-data
1029 ;; Rescan between start of lines enclosing the region.
1030 (font-lock-fontify-region
1031 (progn (goto-char beg) (beginning-of-line) (point))
1032 (progn (goto-char end) (forward-line 1) (point)))))))
1034 (defun font-lock-fontify-block (&optional arg)
1035 "Fontify some lines the way `font-lock-fontify-buffer' would.
1036 The lines could be a function or paragraph, or a specified number of lines.
1037 If ARG is given, fontify that many lines before and after point, or 16 lines if
1038 no ARG is given and `font-lock-mark-block-function' is nil.
1039 If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
1040 delimit the region to fontify."
1041 (interactive "P")
1042 (let ((inhibit-point-motion-hooks t) font-lock-beginning-of-syntax-function
1043 deactivate-mark)
1044 ;; Make sure we have the right `font-lock-keywords' etc.
1045 (if (not font-lock-mode) (font-lock-set-defaults))
1046 (save-excursion
1047 (save-match-data
1048 (condition-case error-data
1049 (if (or arg (not font-lock-mark-block-function))
1050 (let ((lines (if arg (prefix-numeric-value arg) 16)))
1051 (font-lock-fontify-region
1052 (save-excursion (forward-line (- lines)) (point))
1053 (save-excursion (forward-line lines) (point))))
1054 (funcall font-lock-mark-block-function)
1055 (font-lock-fontify-region (point) (mark)))
1056 ((error quit) (message "Fontifying block...%s" error-data)))))))
1058 (if (boundp 'facemenu-keymap)
1059 (define-key facemenu-keymap "\M-g" 'font-lock-fontify-block))
1061 ;;; End of Fontification functions.
1063 ;;; Additional text property functions.
1065 ;; The following text property functions should be builtins. This means they
1066 ;; should be written in C and put with all the other text property functions.
1067 ;; In the meantime, those that are used by font-lock.el are defined in Lisp
1068 ;; below and given a `font-lock-' prefix. Those that are not used are defined
1069 ;; in Lisp below and commented out. sm.
1071 (defun font-lock-prepend-text-property (start end prop value &optional object)
1072 "Prepend to one property of the text from START to END.
1073 Arguments PROP and VALUE specify the property and value to prepend to the value
1074 already in place. The resulting property values are always lists.
1075 Optional argument OBJECT is the string or buffer containing the text."
1076 (let ((val (if (listp value) value (list value))) next prev)
1077 (while (/= start end)
1078 (setq next (next-single-property-change start prop object end)
1079 prev (get-text-property start prop object))
1080 (put-text-property start next prop
1081 (append val (if (listp prev) prev (list prev)))
1082 object)
1083 (setq start next))))
1085 (defun font-lock-append-text-property (start end prop value &optional object)
1086 "Append to one property of the text from START to END.
1087 Arguments PROP and VALUE specify the property and value to append to the value
1088 already in place. The resulting property values are always lists.
1089 Optional argument OBJECT is the string or buffer containing the text."
1090 (let ((val (if (listp value) value (list value))) next prev)
1091 (while (/= start end)
1092 (setq next (next-single-property-change start prop object end)
1093 prev (get-text-property start prop object))
1094 (put-text-property start next prop
1095 (append (if (listp prev) prev (list prev)) val)
1096 object)
1097 (setq start next))))
1099 (defun font-lock-fillin-text-property (start end prop value &optional object)
1100 "Fill in one property of the text from START to END.
1101 Arguments PROP and VALUE specify the property and value to put where none are
1102 already in place. Therefore existing property values are not overwritten.
1103 Optional argument OBJECT is the string or buffer containing the text."
1104 (let ((start (text-property-any start end prop nil object)) next)
1105 (while start
1106 (setq next (next-single-property-change start prop object end))
1107 (put-text-property start next prop value object)
1108 (setq start (text-property-any next end prop nil object)))))
1110 ;; For completeness: this is to `remove-text-properties' as `put-text-property'
1111 ;; is to `add-text-properties', etc.
1112 ;(defun remove-text-property (start end property &optional object)
1113 ; "Remove a property from text from START to END.
1114 ;Argument PROPERTY is the property to remove.
1115 ;Optional argument OBJECT is the string or buffer containing the text.
1116 ;Return t if the property was actually removed, nil otherwise."
1117 ; (remove-text-properties start end (list property) object))
1119 ;; For consistency: maybe this should be called `remove-single-property' like
1120 ;; `next-single-property-change' (not `next-single-text-property-change'), etc.
1121 ;(defun remove-single-text-property (start end prop value &optional object)
1122 ; "Remove a specific property value from text from START to END.
1123 ;Arguments PROP and VALUE specify the property and value to remove. The
1124 ;resulting property values are not equal to VALUE nor lists containing VALUE.
1125 ;Optional argument OBJECT is the string or buffer containing the text."
1126 ; (let ((start (text-property-not-all start end prop nil object)) next prev)
1127 ; (while start
1128 ; (setq next (next-single-property-change start prop object end)
1129 ; prev (get-text-property start prop object))
1130 ; (cond ((and (symbolp prev) (eq value prev))
1131 ; (remove-text-property start next prop object))
1132 ; ((and (listp prev) (memq value prev))
1133 ; (let ((new (delq value prev)))
1134 ; (cond ((null new)
1135 ; (remove-text-property start next prop object))
1136 ; ((= (length new) 1)
1137 ; (put-text-property start next prop (car new) object))
1138 ; (t
1139 ; (put-text-property start next prop new object))))))
1140 ; (setq start (text-property-not-all next end prop nil object)))))
1142 ;;; End of Additional text property functions.
1144 ;;; Syntactic regexp fontification functions.
1146 ;; These syntactic keyword pass functions are identical to those keyword pass
1147 ;; functions below, with the following exceptions; (a) they operate on
1148 ;; `font-lock-syntactic-keywords' of course, (b) they are all `defun' as speed
1149 ;; is less of an issue, (c) eval of property value does not occur JIT as speed
1150 ;; is less of an issue, (d) OVERRIDE cannot be `prepend' or `append' as it
1151 ;; makes no sense for `syntax-table' property values, (e) they do not do it
1152 ;; LOUDLY as it is not likely to be intensive.
1154 (defun font-lock-apply-syntactic-highlight (highlight)
1155 "Apply HIGHLIGHT following a match.
1156 HIGHLIGHT should be of the form MATCH-HIGHLIGHT,
1157 see `font-lock-syntactic-keywords'."
1158 (let* ((match (nth 0 highlight))
1159 (start (match-beginning match)) (end (match-end match))
1160 (value (nth 1 highlight))
1161 (override (nth 2 highlight)))
1162 (if (not start)
1163 ;; No match but we might not signal an error.
1164 (or (nth 3 highlight)
1165 (error "No match %d in highlight %S" match highlight))
1166 (when (and (consp value) (not (numberp (car value))))
1167 (setq value (eval value)))
1168 (when (stringp value) (setq value (string-to-syntax value)))
1169 ;; Flush the syntax-cache. I believe this is not necessary for
1170 ;; font-lock's use of syntax-ppss, but I'm not 100% sure and it can
1171 ;; still be necessary for other users of syntax-ppss anyway.
1172 (syntax-ppss-after-change-function start)
1173 (cond
1174 ((not override)
1175 ;; Cannot override existing fontification.
1176 (or (text-property-not-all start end 'syntax-table nil)
1177 (put-text-property start end 'syntax-table value)))
1178 ((eq override t)
1179 ;; Override existing fontification.
1180 (put-text-property start end 'syntax-table value))
1181 ((eq override 'keep)
1182 ;; Keep existing fontification.
1183 (font-lock-fillin-text-property start end 'syntax-table value))))))
1185 (defun font-lock-fontify-syntactic-anchored-keywords (keywords limit)
1186 "Fontify according to KEYWORDS until LIMIT.
1187 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1188 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1189 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1190 ;; Evaluate PRE-MATCH-FORM.
1191 (pre-match-value (eval (nth 1 keywords))))
1192 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1193 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1194 (setq limit pre-match-value)
1195 (setq limit (line-end-position)))
1196 (save-match-data
1197 ;; Find an occurrence of `matcher' before `limit'.
1198 (while (if (stringp matcher)
1199 (re-search-forward matcher limit t)
1200 (funcall matcher limit))
1201 ;; Apply each highlight to this instance of `matcher'.
1202 (setq highlights lowdarks)
1203 (while highlights
1204 (font-lock-apply-syntactic-highlight (car highlights))
1205 (setq highlights (cdr highlights)))))
1206 ;; Evaluate POST-MATCH-FORM.
1207 (eval (nth 2 keywords))))
1209 (defun font-lock-fontify-syntactic-keywords-region (start end)
1210 "Fontify according to `font-lock-syntactic-keywords' between START and END.
1211 START should be at the beginning of a line."
1212 ;; Ensure the beginning of the file is properly syntactic-fontified.
1213 (when (and font-lock-syntactically-fontified
1214 (< font-lock-syntactically-fontified start))
1215 (setq start (max font-lock-syntactically-fontified (point-min)))
1216 (setq font-lock-syntactically-fontified end))
1217 ;; If `font-lock-syntactic-keywords' is a symbol, get the real keywords.
1218 (when (symbolp font-lock-syntactic-keywords)
1219 (setq font-lock-syntactic-keywords (font-lock-eval-keywords
1220 font-lock-syntactic-keywords)))
1221 ;; If `font-lock-syntactic-keywords' is not compiled, compile it.
1222 (unless (eq (car font-lock-syntactic-keywords) t)
1223 (setq font-lock-syntactic-keywords (font-lock-compile-keywords
1224 font-lock-syntactic-keywords)))
1225 ;; Get down to business.
1226 (let ((case-fold-search font-lock-keywords-case-fold-search)
1227 (keywords (cdr font-lock-syntactic-keywords))
1228 keyword matcher highlights)
1229 (while keywords
1230 ;; Find an occurrence of `matcher' from `start' to `end'.
1231 (setq keyword (car keywords) matcher (car keyword))
1232 (goto-char start)
1233 (while (if (stringp matcher)
1234 (re-search-forward matcher end t)
1235 (funcall matcher end))
1236 ;; Apply each highlight to this instance of `matcher', which may be
1237 ;; specific highlights or more keywords anchored to `matcher'.
1238 (setq highlights (cdr keyword))
1239 (while highlights
1240 (if (numberp (car (car highlights)))
1241 (font-lock-apply-syntactic-highlight (car highlights))
1242 (font-lock-fontify-syntactic-anchored-keywords (car highlights)
1243 end))
1244 (setq highlights (cdr highlights))))
1245 (setq keywords (cdr keywords)))))
1247 ;;; End of Syntactic regexp fontification functions.
1249 ;;; Syntactic fontification functions.
1251 (defun font-lock-fontify-syntactically-region (start end &optional loudly ppss)
1252 "Put proper face on each string and comment between START and END.
1253 START should be at the beginning of a line."
1254 (let (state face beg)
1255 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
1256 (goto-char start)
1258 ;; Find the state at the `beginning-of-line' before `start'.
1259 (setq state (or ppss (syntax-ppss start)))
1261 ;; Find each interesting place between here and `end'.
1262 (while
1263 (progn
1264 (when (or (nth 3 state) (nth 4 state))
1265 (setq face (funcall font-lock-syntactic-face-function state))
1266 (setq beg (max (nth 8 state) start))
1267 (setq state (parse-partial-sexp (point) end nil nil state
1268 'syntax-table))
1269 (when face (put-text-property beg (point) 'face face)))
1270 (setq state (parse-partial-sexp (point) end nil nil state
1271 'syntax-table))
1272 (< (point) end)))))
1274 ;;; End of Syntactic fontification functions.
1276 ;;; Keyword regexp fontification functions.
1278 (defsubst font-lock-apply-highlight (highlight)
1279 "Apply HIGHLIGHT following a match.
1280 HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
1281 (let* ((match (nth 0 highlight))
1282 (start (match-beginning match)) (end (match-end match))
1283 (override (nth 2 highlight)))
1284 (if (not start)
1285 ;; No match but we might not signal an error.
1286 (or (nth 3 highlight)
1287 (error "No match %d in highlight %S" match highlight))
1288 (let ((val (eval (nth 1 highlight))))
1289 (when (eq (car-safe val) 'face)
1290 (add-text-properties start end (cddr val))
1291 (setq val (cadr val)))
1292 (cond
1293 ((not override)
1294 ;; Cannot override existing fontification.
1295 (or (text-property-not-all start end 'face nil)
1296 (put-text-property start end 'face val)))
1297 ((eq override t)
1298 ;; Override existing fontification.
1299 (put-text-property start end 'face val))
1300 ((eq override 'prepend)
1301 ;; Prepend to existing fontification.
1302 (font-lock-prepend-text-property start end 'face val))
1303 ((eq override 'append)
1304 ;; Append to existing fontification.
1305 (font-lock-append-text-property start end 'face val))
1306 ((eq override 'keep)
1307 ;; Keep existing fontification.
1308 (font-lock-fillin-text-property start end 'face val)))))))
1310 (defsubst font-lock-fontify-anchored-keywords (keywords limit)
1311 "Fontify according to KEYWORDS until LIMIT.
1312 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1313 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1314 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1315 (lead-start (match-beginning 0))
1316 ;; Evaluate PRE-MATCH-FORM.
1317 (pre-match-value (eval (nth 1 keywords))))
1318 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1319 (if (not (and (numberp pre-match-value) (> pre-match-value (point))))
1320 (setq limit (line-end-position))
1321 (setq limit pre-match-value)
1322 (when (and font-lock-multiline (>= limit (line-beginning-position 2)))
1323 ;; this is a multiline anchored match
1324 ;; (setq font-lock-multiline t)
1325 (put-text-property (if (= limit (line-beginning-position 2))
1326 (1- limit)
1327 (min lead-start (point)))
1328 limit
1329 'font-lock-multiline t)))
1330 (save-match-data
1331 ;; Find an occurrence of `matcher' before `limit'.
1332 (while (and (< (point) limit)
1333 (if (stringp matcher)
1334 (re-search-forward matcher limit t)
1335 (funcall matcher limit)))
1336 ;; Apply each highlight to this instance of `matcher'.
1337 (setq highlights lowdarks)
1338 (while highlights
1339 (font-lock-apply-highlight (car highlights))
1340 (setq highlights (cdr highlights)))))
1341 ;; Evaluate POST-MATCH-FORM.
1342 (eval (nth 2 keywords))))
1344 (defun font-lock-fontify-keywords-region (start end &optional loudly)
1345 "Fontify according to `font-lock-keywords' between START and END.
1346 START should be at the beginning of a line.
1347 LOUDLY, if non-nil, allows progress-meter bar."
1348 (unless (eq (car font-lock-keywords) t)
1349 (setq font-lock-keywords
1350 (font-lock-compile-keywords font-lock-keywords t)))
1351 (let ((case-fold-search font-lock-keywords-case-fold-search)
1352 (keywords (cdr font-lock-keywords))
1353 (bufname (buffer-name)) (count 0)
1354 keyword matcher highlights)
1356 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1357 (while keywords
1358 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
1359 (make-string (incf count) ?.)))
1361 ;; Find an occurrence of `matcher' from `start' to `end'.
1362 (setq keyword (car keywords) matcher (car keyword))
1363 (goto-char start)
1364 (while (and (< (point) end)
1365 (if (stringp matcher)
1366 (re-search-forward matcher end t)
1367 (funcall matcher end)))
1368 (when (and font-lock-multiline
1369 (>= (point)
1370 (save-excursion (goto-char (match-beginning 0))
1371 (forward-line 1) (point))))
1372 ;; this is a multiline regexp match
1373 ;; (setq font-lock-multiline t)
1374 (put-text-property (if (= (point)
1375 (save-excursion
1376 (goto-char (match-beginning 0))
1377 (forward-line 1) (point)))
1378 (1- (point))
1379 (match-beginning 0))
1380 (point)
1381 'font-lock-multiline t))
1382 ;; Apply each highlight to this instance of `matcher', which may be
1383 ;; specific highlights or more keywords anchored to `matcher'.
1384 (setq highlights (cdr keyword))
1385 (while highlights
1386 (if (numberp (car (car highlights)))
1387 (font-lock-apply-highlight (car highlights))
1388 (font-lock-fontify-anchored-keywords (car highlights) end))
1389 (setq highlights (cdr highlights))))
1390 (setq keywords (cdr keywords)))))
1392 ;;; End of Keyword regexp fontification functions.
1394 ;; Various functions.
1396 (defun font-lock-compile-keywords (keywords &optional regexp)
1397 "Compile KEYWORDS into the form (t KEYWORD ...).
1398 Here KEYWORD is of the form (MATCHER HIGHLIGHT ...) as shown in the
1399 `font-lock-keywords' doc string.
1400 If REGEXP is non-nil, it means these keywords are used for
1401 `font-lock-keywords' rather than for `font-lock-syntactic-keywords'."
1402 (if (eq (car-safe keywords) t)
1403 keywords
1404 (setq keywords (cons t (mapcar 'font-lock-compile-keyword keywords)))
1405 (if (and regexp
1406 (eq (or syntax-begin-function
1407 font-lock-beginning-of-syntax-function)
1408 'beginning-of-defun)
1409 (not beginning-of-defun-function))
1410 ;; Try to detect when a string or comment contains something that
1411 ;; looks like a defun and would thus confuse font-lock.
1412 (nconc keywords
1413 `((,(if defun-prompt-regexp
1414 (concat "^\\(?:" defun-prompt-regexp "\\)?\\s(")
1415 "^\\s(")
1417 (if (memq (get-text-property (1- (point)) 'face)
1418 '(font-lock-string-face font-lock-doc-face
1419 font-lock-comment-face))
1420 font-lock-warning-face)
1421 prepend)))))
1422 keywords))
1424 (defun font-lock-compile-keyword (keyword)
1425 (cond ((nlistp keyword) ; MATCHER
1426 (list keyword '(0 font-lock-keyword-face)))
1427 ((eq (car keyword) 'eval) ; (eval . FORM)
1428 (font-lock-compile-keyword (eval (cdr keyword))))
1429 ((eq (car-safe (cdr keyword)) 'quote) ; (MATCHER . 'FORM)
1430 ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote.
1431 (if (symbolp (nth 2 keyword))
1432 (list (car keyword) (list 0 (cdr keyword)))
1433 (font-lock-compile-keyword (cons (car keyword) (nth 2 keyword)))))
1434 ((numberp (cdr keyword)) ; (MATCHER . MATCH)
1435 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
1436 ((symbolp (cdr keyword)) ; (MATCHER . FACENAME)
1437 (list (car keyword) (list 0 (cdr keyword))))
1438 ((nlistp (nth 1 keyword)) ; (MATCHER . HIGHLIGHT)
1439 (list (car keyword) (cdr keyword)))
1440 (t ; (MATCHER HIGHLIGHT ...)
1441 keyword)))
1443 (defun font-lock-eval-keywords (keywords)
1444 "Evalulate KEYWORDS if a function (funcall) or variable (eval) name."
1445 (if (listp keywords)
1446 keywords
1447 (font-lock-eval-keywords (if (fboundp keywords)
1448 (funcall keywords)
1449 (eval keywords)))))
1451 (defun font-lock-value-in-major-mode (alist)
1452 "Return value in ALIST for `major-mode', or ALIST if it is not an alist.
1453 Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t."
1454 (if (consp alist)
1455 (cdr (or (assq major-mode alist) (assq t alist)))
1456 alist))
1458 (defun font-lock-choose-keywords (keywords level)
1459 "Return LEVELth element of KEYWORDS.
1460 A LEVEL of nil is equal to a LEVEL of 0, a LEVEL of t is equal to
1461 \(1- (length KEYWORDS))."
1462 (cond ((not (and (listp keywords) (symbolp (car keywords))))
1463 keywords)
1464 ((numberp level)
1465 (or (nth level keywords) (car (reverse keywords))))
1466 ((eq level t)
1467 (car (reverse keywords)))
1469 (car keywords))))
1471 (defvar font-lock-set-defaults nil) ; Whether we have set up defaults.
1473 (defun font-lock-set-defaults ()
1474 "Set fontification defaults appropriately for this mode.
1475 Sets various variables using `font-lock-defaults' (or, if nil, using
1476 `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
1477 ;; Set fontification defaults iff not previously set.
1478 (unless font-lock-set-defaults
1479 (set (make-local-variable 'font-lock-set-defaults) t)
1480 (make-local-variable 'font-lock-fontified)
1481 (make-local-variable 'font-lock-multiline)
1482 (let* ((defaults (or font-lock-defaults
1483 (cdr (assq major-mode font-lock-defaults-alist))))
1484 (keywords
1485 (font-lock-choose-keywords (nth 0 defaults)
1486 (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1487 (local (cdr (assq major-mode font-lock-keywords-alist)))
1488 (removed-keywords
1489 (cdr-safe (assq major-mode font-lock-removed-keywords-alist))))
1490 (set (make-local-variable 'font-lock-defaults) defaults)
1491 ;; Syntactic fontification?
1492 (when (nth 1 defaults)
1493 (set (make-local-variable 'font-lock-keywords-only) t))
1494 ;; Case fold during regexp fontification?
1495 (when (nth 2 defaults)
1496 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
1497 ;; Syntax table for regexp and syntactic fontification?
1498 (when (nth 3 defaults)
1499 (set (make-local-variable 'font-lock-syntax-table)
1500 (copy-syntax-table (syntax-table)))
1501 (dolist (selem (nth 3 defaults))
1502 ;; The character to modify may be a single CHAR or a STRING.
1503 (let ((syntax (cdr selem)))
1504 (dolist (char (if (numberp (car selem))
1505 (list (car selem))
1506 (mapcar 'identity (car selem))))
1507 (modify-syntax-entry char syntax font-lock-syntax-table)))))
1508 ;; Syntax function for syntactic fontification?
1509 (when (nth 4 defaults)
1510 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1511 (nth 4 defaults)))
1512 ;; Variable alist?
1513 (dolist (x (nthcdr 5 defaults))
1514 (set (make-local-variable (car x)) (cdr x)))
1515 ;; Setup `font-lock-keywords' last because its value might depend
1516 ;; on other settings (e.g. font-lock-compile-keywords uses
1517 ;; font-lock-beginning-of-syntax-function).
1518 (set (make-local-variable 'font-lock-keywords)
1519 (font-lock-compile-keywords (font-lock-eval-keywords keywords) t))
1520 ;; Local fontification?
1521 (while local
1522 (font-lock-add-keywords nil (car (car local)) (cdr (car local)))
1523 (setq local (cdr local)))
1524 (when removed-keywords
1525 (font-lock-remove-keywords nil removed-keywords)))))
1527 ;;; Colour etc. support.
1529 ;; Originally face attributes were specified via `font-lock-face-attributes'.
1530 ;; Users then changed the default face attributes by setting that variable.
1531 ;; However, we try and be back-compatible and respect its value if set except
1532 ;; for faces where M-x customize has been used to save changes for the face.
1533 (when (boundp 'font-lock-face-attributes)
1534 (let ((face-attributes font-lock-face-attributes))
1535 (while face-attributes
1536 (let* ((face-attribute (pop face-attributes))
1537 (face (car face-attribute)))
1538 ;; Rustle up a `defface' SPEC from a `font-lock-face-attributes' entry.
1539 (unless (get face 'saved-face)
1540 (let ((foreground (nth 1 face-attribute))
1541 (background (nth 2 face-attribute))
1542 (bold-p (nth 3 face-attribute))
1543 (italic-p (nth 4 face-attribute))
1544 (underline-p (nth 5 face-attribute))
1545 face-spec)
1546 (when foreground
1547 (setq face-spec (cons ':foreground (cons foreground face-spec))))
1548 (when background
1549 (setq face-spec (cons ':background (cons background face-spec))))
1550 (when bold-p
1551 (setq face-spec (append '(:weight bold) face-spec)))
1552 (when italic-p
1553 (setq face-spec (append '(:slant italic) face-spec)))
1554 (when underline-p
1555 (setq face-spec (append '(:underline t) face-spec)))
1556 (custom-declare-face face (list (list t face-spec)) nil)))))))
1558 ;; But now we do it the custom way. Note that `defface' will not overwrite any
1559 ;; faces declared above via `custom-declare-face'.
1560 (defface font-lock-comment-face
1561 '((((class grayscale) (background light))
1562 (:foreground "DimGray" :weight bold :slant italic))
1563 (((class grayscale) (background dark))
1564 (:foreground "LightGray" :weight bold :slant italic))
1565 (((class color) (min-colors 88) (background light))
1566 (:foreground "Firebrick"))
1567 (((class color) (min-colors 88) (background dark))
1568 (:foreground "chocolate1"))
1569 (((class color) (min-colors 16) (background light))
1570 (:foreground "red"))
1571 (((class color) (min-colors 16) (background dark))
1572 (:foreground "red1"))
1573 (((class color) (min-colors 8) (background light))
1574 (:foreground "red"))
1575 (((class color) (min-colors 8) (background dark))
1576 (:foreground "red1"))
1577 (t (:weight bold :slant italic)))
1578 "Font Lock mode face used to highlight comments."
1579 :group 'font-lock-highlighting-faces)
1581 (defface font-lock-string-face
1582 '((((class grayscale) (background light)) (:foreground "DimGray" :slant italic))
1583 (((class grayscale) (background dark)) (:foreground "LightGray" :slant italic))
1584 (((class color) (min-colors 88) (background light)) (:foreground "RosyBrown"))
1585 (((class color) (min-colors 88) (background dark)) (:foreground "LightSalmon"))
1586 (((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
1587 (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
1588 (((class color) (min-colors 8)) (:foreground "green"))
1589 (t (:slant italic)))
1590 "Font Lock mode face used to highlight strings."
1591 :group 'font-lock-highlighting-faces)
1593 (defface font-lock-doc-face
1594 '((t :inherit font-lock-string-face))
1595 "Font Lock mode face used to highlight documentation."
1596 :group 'font-lock-highlighting-faces)
1598 (defface font-lock-keyword-face
1599 '((((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
1600 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
1601 (((class color) (min-colors 88) (background light)) (:foreground "Purple"))
1602 (((class color) (min-colors 88) (background dark)) (:foreground "Cyan"))
1603 (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
1604 (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
1605 (((class color) (min-colors 8)) (:foreground "cyan" :weight bold))
1606 (t (:weight bold)))
1607 "Font Lock mode face used to highlight keywords."
1608 :group 'font-lock-highlighting-faces)
1610 (defface font-lock-builtin-face
1611 '((((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
1612 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
1613 (((class color) (min-colors 88) (background light)) (:foreground "Orchid"))
1614 (((class color) (min-colors 88) (background dark)) (:foreground "LightSteelBlue"))
1615 (((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
1616 (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
1617 (((class color) (min-colors 8)) (:foreground "blue" :weight bold))
1618 (t (:weight bold)))
1619 "Font Lock mode face used to highlight builtins."
1620 :group 'font-lock-highlighting-faces)
1622 (defface font-lock-function-name-face
1623 '((((class color) (min-colors 88) (background light)) (:foreground "Blue"))
1624 (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
1625 (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
1626 (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
1627 (((class color) (min-colors 8)) (:foreground "blue" :weight bold))
1628 (t (:inverse-video t :weight bold)))
1629 "Font Lock mode face used to highlight function names."
1630 :group 'font-lock-highlighting-faces)
1632 (defface font-lock-variable-name-face
1633 '((((class grayscale) (background light))
1634 (:foreground "Gray90" :weight bold :slant italic))
1635 (((class grayscale) (background dark))
1636 (:foreground "DimGray" :weight bold :slant italic))
1637 (((class color) (min-colors 88) (background light)) (:foreground "DarkGoldenrod"))
1638 (((class color) (min-colors 88) (background dark)) (:foreground "LightGoldenrod"))
1639 (((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
1640 (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
1641 (((class color) (min-colors 8)) (:foreground "yellow" :weight light))
1642 (t (:weight bold :slant italic)))
1643 "Font Lock mode face used to highlight variable names."
1644 :group 'font-lock-highlighting-faces)
1646 (defface font-lock-type-face
1647 '((((class grayscale) (background light)) (:foreground "Gray90" :weight bold))
1648 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
1649 (((class color) (min-colors 88) (background light)) (:foreground "ForestGreen"))
1650 (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
1651 (((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
1652 (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
1653 (((class color) (min-colors 8)) (:foreground "green"))
1654 (t (:weight bold :underline t)))
1655 "Font Lock mode face used to highlight type and classes."
1656 :group 'font-lock-highlighting-faces)
1658 (defface font-lock-constant-face
1659 '((((class grayscale) (background light))
1660 (:foreground "LightGray" :weight bold :underline t))
1661 (((class grayscale) (background dark))
1662 (:foreground "Gray50" :weight bold :underline t))
1663 (((class color) (min-colors 88) (background light)) (:foreground "CadetBlue"))
1664 (((class color) (min-colors 88) (background dark)) (:foreground "Aquamarine"))
1665 (((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
1666 (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
1667 (((class color) (min-colors 8)) (:foreground "magenta"))
1668 (t (:weight bold :underline t)))
1669 "Font Lock mode face used to highlight constants and labels."
1670 :group 'font-lock-highlighting-faces)
1672 (defface font-lock-warning-face
1673 '((((class color) (min-colors 88) (background light)) (:foreground "Red" :weight bold))
1674 (((class color) (min-colors 88) (background dark)) (:foreground "Pink" :weight bold))
1675 (((class color) (min-colors 16) (background light)) (:foreground "Red" :weight bold))
1676 (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :weight bold))
1677 (((class color) (min-colors 8)) (:foreground "red"))
1678 (t (:inverse-video t :weight bold)))
1679 "Font Lock mode face used to highlight warnings."
1680 :group 'font-lock-highlighting-faces)
1682 (defface font-lock-preprocessor-face
1683 '((t :inherit font-lock-builtin-face))
1684 "Font Lock mode face used to highlight preprocessor directives."
1685 :group 'font-lock-highlighting-faces)
1687 ;;; End of Colour etc. support.
1689 ;;; Menu support.
1691 ;; This section of code is commented out because Emacs does not have real menu
1692 ;; buttons. (We can mimic them by putting "( ) " or "(X) " at the beginning of
1693 ;; the menu entry text, but with Xt it looks both ugly and embarrassingly
1694 ;; amateur.) If/When Emacs gets real menus buttons, put in menu-bar.el after
1695 ;; the entry for "Text Properties" something like:
1697 ;; (define-key menu-bar-edit-menu [font-lock]
1698 ;; (cons "Syntax Highlighting" font-lock-menu))
1700 ;; and remove a single ";" from the beginning of each line in the rest of this
1701 ;; section. Probably the mechanism for telling the menu code what are menu
1702 ;; buttons and when they are on or off needs tweaking. I have assumed that the
1703 ;; mechanism is via `menu-toggle' and `menu-selected' symbol properties. sm.
1705 ;;;;###autoload
1706 ;(progn
1707 ; ;; Make the Font Lock menu.
1708 ; (defvar font-lock-menu (make-sparse-keymap "Syntax Highlighting"))
1709 ; ;; Add the menu items in reverse order.
1710 ; (define-key font-lock-menu [fontify-less]
1711 ; '("Less In Current Buffer" . font-lock-fontify-less))
1712 ; (define-key font-lock-menu [fontify-more]
1713 ; '("More In Current Buffer" . font-lock-fontify-more))
1714 ; (define-key font-lock-menu [font-lock-sep]
1715 ; '("--"))
1716 ; (define-key font-lock-menu [font-lock-mode]
1717 ; '("In Current Buffer" . font-lock-mode))
1718 ; (define-key font-lock-menu [global-font-lock-mode]
1719 ; '("In All Buffers" . global-font-lock-mode)))
1721 ;;;;###autoload
1722 ;(progn
1723 ; ;; We put the appropriate `menu-enable' etc. symbol property values on when
1724 ; ;; font-lock.el is loaded, so we don't need to autoload the three variables.
1725 ; (put 'global-font-lock-mode 'menu-toggle t)
1726 ; (put 'font-lock-mode 'menu-toggle t)
1727 ; (put 'font-lock-fontify-more 'menu-enable '(identity))
1728 ; (put 'font-lock-fontify-less 'menu-enable '(identity)))
1730 ;;; Put the appropriate symbol property values on now. See above.
1731 ;(put 'global-font-lock-mode 'menu-selected 'global-font-lock-mode)
1732 ;(put 'font-lock-mode 'menu-selected 'font-lock-mode)
1733 ;(put 'font-lock-fontify-more 'menu-enable '(nth 2 font-lock-fontify-level))
1734 ;(put 'font-lock-fontify-less 'menu-enable '(nth 1 font-lock-fontify-level))
1736 ;(defvar font-lock-fontify-level nil) ; For less/more fontification.
1738 ;(defun font-lock-fontify-level (level)
1739 ; (let ((font-lock-maximum-decoration level))
1740 ; (when font-lock-mode
1741 ; (font-lock-mode))
1742 ; (font-lock-mode)
1743 ; (when font-lock-verbose
1744 ; (message "Fontifying %s... level %d" (buffer-name) level))))
1746 ;(defun font-lock-fontify-less ()
1747 ; "Fontify the current buffer with less decoration.
1748 ;See `font-lock-maximum-decoration'."
1749 ; (interactive)
1750 ; ;; Check in case we get called interactively.
1751 ; (if (nth 1 font-lock-fontify-level)
1752 ; (font-lock-fontify-level (1- (car font-lock-fontify-level)))
1753 ; (error "No less decoration")))
1755 ;(defun font-lock-fontify-more ()
1756 ; "Fontify the current buffer with more decoration.
1757 ;See `font-lock-maximum-decoration'."
1758 ; (interactive)
1759 ; ;; Check in case we get called interactively.
1760 ; (if (nth 2 font-lock-fontify-level)
1761 ; (font-lock-fontify-level (1+ (car font-lock-fontify-level)))
1762 ; (error "No more decoration")))
1764 ;;; This should be called by `font-lock-set-defaults'.
1765 ;(defun font-lock-set-menu ()
1766 ; ;; Activate less/more fontification entries if there are multiple levels for
1767 ; ;; the current buffer. Sets `font-lock-fontify-level' to be of the form
1768 ; ;; (CURRENT-LEVEL IS-LOWER-LEVEL-P IS-HIGHER-LEVEL-P) for menu activation.
1769 ; (let ((keywords (or (nth 0 font-lock-defaults)
1770 ; (nth 1 (assq major-mode font-lock-defaults-alist))))
1771 ; (level (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1772 ; (make-local-variable 'font-lock-fontify-level)
1773 ; (if (or (symbolp keywords) (= (length keywords) 1))
1774 ; (font-lock-unset-menu)
1775 ; (cond ((eq level t)
1776 ; (setq level (1- (length keywords))))
1777 ; ((or (null level) (zerop level))
1778 ; ;; The default level is usually, but not necessarily, level 1.
1779 ; (setq level (- (length keywords)
1780 ; (length (member (eval (car keywords))
1781 ; (mapcar 'eval (cdr keywords))))))))
1782 ; (setq font-lock-fontify-level (list level (> level 1)
1783 ; (< level (1- (length keywords))))))))
1785 ;;; This should be called by `font-lock-unset-defaults'.
1786 ;(defun font-lock-unset-menu ()
1787 ; ;; Deactivate less/more fontification entries.
1788 ; (setq font-lock-fontify-level nil))
1790 ;;; End of Menu support.
1792 ;;; Various regexp information shared by several modes.
1793 ;;; Information specific to a single mode should go in its load library.
1795 ;; Font Lock support for C, C++, Objective-C and Java modes is now in
1796 ;; cc-fonts.el (and required by cc-mode.el). However, the below function
1797 ;; should stay in font-lock.el, since it is used by other libraries. sm.
1799 (defun font-lock-match-c-style-declaration-item-and-skip-to-next (limit)
1800 "Match, and move over, any declaration/definition item after point.
1801 Matches after point, but ignores leading whitespace and `*' characters.
1802 Does not move further than LIMIT.
1804 The expected syntax of a declaration/definition item is `word' (preceded by
1805 optional whitespace and `*' characters and proceeded by optional whitespace)
1806 optionally followed by a `('. Everything following the item (but belonging to
1807 it) is expected to be skip-able by `scan-sexps', and items are expected to be
1808 separated with a `,' and to be terminated with a `;'.
1810 Thus the regexp matches after point: word (
1811 ^^^^ ^
1812 Where the match subexpressions are: 1 2
1814 The item is delimited by (match-beginning 1) and (match-end 1).
1815 If (match-beginning 2) is non-nil, the item is followed by a `('.
1817 This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
1818 (when (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?")
1819 (when (and (match-end 2) (> (- (match-end 2) (match-beginning 2)) 1))
1820 ;; If `word' is followed by a double open-paren, it's probably
1821 ;; a macro used for "int myfun P_ ((int arg1))". Let's go back one
1822 ;; word to try and match `myfun' rather than `P_'.
1823 (let ((pos (point)))
1824 (skip-chars-backward " \t\n")
1825 (skip-syntax-backward "w")
1826 (unless (looking-at "\\(\\sw+\\)[ \t\n]*\\sw+[ \t\n]*\\(((?\\)?")
1827 ;; Looks like it was something else, so go back to where we
1828 ;; were and reset the match data by rematching.
1829 (goto-char pos)
1830 (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?"))))
1831 (save-match-data
1832 (condition-case nil
1833 (save-restriction
1834 ;; Restrict to the LIMIT.
1835 (narrow-to-region (point-min) limit)
1836 (goto-char (match-end 1))
1837 ;; Move over any item value, etc., to the next item.
1838 (while (not (looking-at "[ \t\n]*\\(\\(,\\)\\|;\\|\\'\\)"))
1839 (goto-char (or (scan-sexps (point) 1) (point-max))))
1840 (goto-char (match-end 2)))
1841 (error t)))))
1843 ;; Lisp.
1845 (defconst lisp-font-lock-keywords-1
1846 (eval-when-compile
1847 (list
1849 ;; Definitions.
1850 (list (concat "(\\(def\\("
1851 ;; Function declarations.
1852 "\\(advice\\|varalias\\|alias\\|generic\\|macro\\*?\\|method\\|"
1853 "setf\\|subst\\*?\\|un\\*?\\|"
1854 "ine-\\(condition\\|\\(?:derived\\|minor\\)-mode\\|"
1855 "method-combination\\|setf-expander\\|skeleton\\|widget\\|"
1856 "function\\|\\(compiler\\|modify\\|symbol\\)-macro\\)\\)\\|"
1857 ;; Variable declarations.
1858 "\\(const\\(ant\\)?\\|custom\\|face\\|parameter\\|var\\)\\|"
1859 ;; Structure declarations.
1860 "\\(class\\|group\\|theme\\|package\\|struct\\|type\\)"
1861 "\\)\\)\\>"
1862 ;; Any whitespace and defined object.
1863 "[ \t'\(]*"
1864 "\\(setf[ \t]+\\sw+)\\|\\sw+\\)?")
1865 '(1 font-lock-keyword-face)
1866 '(9 (cond ((match-beginning 3) font-lock-function-name-face)
1867 ((match-beginning 6) font-lock-variable-name-face)
1868 (t font-lock-type-face))
1869 nil t))
1871 ;; Emacs Lisp autoload cookies.
1872 '("^;;;###\\(autoload\\)" 1 font-lock-warning-face prepend)
1874 "Subdued level highlighting for Lisp modes.")
1876 (defconst lisp-font-lock-keywords-2
1877 (append lisp-font-lock-keywords-1
1878 (eval-when-compile
1879 (list
1881 ;; Control structures. Emacs Lisp forms.
1882 (cons (concat
1883 "(" (regexp-opt
1884 '("cond" "if" "while" "let" "let*"
1885 "prog" "progn" "progv" "prog1" "prog2" "prog*"
1886 "inline" "lambda" "save-restriction" "save-excursion"
1887 "save-window-excursion" "save-selected-window"
1888 "save-match-data" "save-current-buffer" "unwind-protect"
1889 "condition-case" "track-mouse"
1890 "eval-after-load" "eval-and-compile" "eval-when-compile"
1891 "eval-when"
1892 "with-category-table"
1893 "with-current-buffer" "with-electric-help"
1894 "with-local-quit" "with-no-warnings"
1895 "with-output-to-string" "with-output-to-temp-buffer"
1896 "with-selected-window" "with-syntax-table"
1897 "with-temp-buffer" "with-temp-file" "with-temp-message"
1898 "with-timeout" "with-timeout-handler") t)
1899 "\\>")
1902 ;; Control structures. Common Lisp forms.
1903 (cons (concat
1904 "(" (regexp-opt
1905 '("when" "unless" "case" "ecase" "typecase" "etypecase"
1906 "ccase" "ctypecase" "handler-case" "handler-bind"
1907 "restart-bind" "restart-case" "in-package"
1908 "cerror" "break" "ignore-errors"
1909 "loop" "do" "do*" "dotimes" "dolist" "the" "locally"
1910 "proclaim" "declaim" "declare" "symbol-macrolet"
1911 "lexical-let" "lexical-let*" "flet" "labels" "compiler-let"
1912 "destructuring-bind" "macrolet" "tagbody" "block"
1913 "return" "return-from"
1914 "with-accessors" "with-compilation-unit"
1915 "with-condition-restarts" "with-hash-table-iterator"
1916 "with-input-from-string" "with-open-file"
1917 "with-open-stream" "with-output-to-string"
1918 "with-package-iterator" "with-simple-restart"
1919 "with-slots" "with-standard-io-syntax") t)
1920 "\\>")
1923 ;; Exit/Feature symbols as constants.
1924 (list (concat "(\\(catch\\|throw\\|featurep\\|provide\\|require\\)\\>"
1925 "[ \t']*\\(\\sw+\\)?")
1926 '(1 font-lock-keyword-face)
1927 '(2 font-lock-constant-face nil t))
1929 ;; Erroneous structures.
1930 '("(\\(abort\\|assert\\|error\\|signal\\)\\>" 1 font-lock-warning-face)
1932 ;; Words inside \\[] tend to be for `substitute-command-keys'.
1933 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-constant-face prepend)
1935 ;; Words inside `' tend to be symbol names.
1936 '("`\\(\\sw\\sw+\\)'" 1 font-lock-constant-face prepend)
1938 ;; Constant values.
1939 '("\\<:\\sw+\\>" 0 font-lock-builtin-face)
1941 ;; ELisp and CLisp `&' keywords as types.
1942 '("\\&\\sw+\\>" . font-lock-type-face)
1944 ;;; This is too general -- rms.
1945 ;;; A user complained that he has functions whose names start with `do'
1946 ;;; and that they get the wrong color.
1947 ;;; ;; CL `with-' and `do-' constructs
1948 ;;; '("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
1950 "Gaudy level highlighting for Lisp modes.")
1952 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
1953 "Default expressions to highlight in Lisp modes.")
1955 (provide 'font-lock)
1957 ;;; arch-tag: 682327e4-64d8-4057-b20b-1fbb9f1fc54c
1958 ;;; font-lock.el ends here