Fix a typo in doc/lispref/anti.texi.
[emacs.git] / lisp / font-lock.el
blobb65828ca1d7673de2a59536e2a9f2cf15b205dc0
1 ;;; font-lock.el --- Electric font lock mode
3 ;; Copyright (C) 1992-2012 Free Software Foundation, Inc.
5 ;; Author: Jamie Zawinski
6 ;; Richard Stallman
7 ;; Stefan Monnier
8 ;; Maintainer: FSF
9 ;; Keywords: languages, faces
10 ;; Package: emacs
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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's 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 the buffer local variable `font-lock-defaults'.
104 ;; (Font Lock mode is set up via (a) where a mode's patterns are
105 ;; distributed with the mode's package library, and (b) where a mode's
106 ;; 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 behavior 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'.
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 ;; (lambda ()
155 ;; (set (make-local-variable 'font-lock-defaults)
156 ;; '(foo-font-lock-keywords t))))
158 ;;; Adding Font Lock support for modes:
160 ;; Of course, it would be better that the mode already supports Font Lock mode.
161 ;; The package author would do something similar to above. The mode must
162 ;; define at the top-level a variable or variables that specify regexp
163 ;; fontification. Then, the mode command should indicate to Font Lock mode,
164 ;; via `font-lock-defaults', exactly what support is required. For example,
165 ;; say Bar mode should have the following regexps fontified case-insensitively,
166 ;; and comments and strings should be fontified automagically. In bar.el there
167 ;; could be:
169 ;; (defvar bar-font-lock-keywords
170 ;; '(("\\<\\(uno\\|due\\|tre\\)\\>" . font-lock-keyword-face)
171 ;; ("\\<\\(quattro\\|cinque\\|sei\\)\\>" . font-lock-type-face))
172 ;; "Default expressions to highlight in Bar mode.")
174 ;; and within `bar-mode' there could be:
176 ;; (set (make-local-variable 'font-lock-defaults)
177 ;; '(bar-font-lock-keywords nil t))
179 ;; What is fontification for? You might say, "It's to make my code look nice."
180 ;; I think it should be for adding information in the form of cues. These cues
181 ;; should provide you with enough information to both (a) distinguish between
182 ;; different items, and (b) identify the item meanings, without having to read
183 ;; the items and think about it. Therefore, fontification allows you to think
184 ;; less about, say, the structure of code, and more about, say, why the code
185 ;; doesn't work. Or maybe it allows you to think less and drift off to sleep.
187 ;; So, here are my opinions/advice/guidelines:
189 ;; - Highlight conceptual objects, such as function and variable names, and
190 ;; different objects types differently, i.e., (a) and (b) above, highlight
191 ;; function names differently to variable names.
192 ;; - Keep the faces distinct from each other as far as possible.
193 ;; i.e., (a) above.
194 ;; - Use the same face for the same conceptual object, across all modes.
195 ;; i.e., (b) above, all modes that have items that can be thought of as, say,
196 ;; keywords, should be highlighted with the same face, etc.
197 ;; - Make the face attributes fit the concept as far as possible.
198 ;; i.e., function names might be a bold color such as blue, comments might
199 ;; be a bright color such as red, character strings might be brown, because,
200 ;; err, strings are brown (that was not the reason, please believe me).
201 ;; - Don't use a non-nil OVERRIDE unless you have a good reason.
202 ;; Only use OVERRIDE for special things that are easy to define, such as the
203 ;; way `...' quotes are treated in strings and comments in Emacs Lisp mode.
204 ;; Don't use it to, say, highlight keywords in commented out code or strings.
205 ;; - Err, that's it.
207 ;;; Code:
209 (require 'syntax)
210 (eval-when-compile (require 'cl))
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 :tag "Emacs Manual" "(emacs)Font Lock")
216 :link '(custom-manual :tag "Elisp Manual" "(elisp)Font Lock Mode")
217 :group 'faces)
219 (defgroup font-lock-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 ;; User variables.
230 (defcustom font-lock-maximum-size 256000
231 "Maximum buffer size for unsupported buffer fontification.
232 When `font-lock-support-mode' is nil, only buffers smaller than
233 this are fontified. This variable has no effect if a Font Lock
234 support mode (usually `jit-lock-mode') is enabled.
236 If nil, means size is irrelevant.
237 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
238 where MAJOR-MODE is a symbol or t (meaning the default). For example:
239 ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576))
240 means that the maximum size is 250K for buffers in C or C++ modes, one megabyte
241 for buffers in Rmail mode, and size is irrelevant otherwise."
242 :type '(choice (const :tag "none" nil)
243 (integer :tag "size")
244 (repeat :menu-tag "mode specific" :tag "mode specific"
245 :value ((t . nil))
246 (cons :tag "Instance"
247 (radio :tag "Mode"
248 (const :tag "all" t)
249 (symbol :tag "name"))
250 (radio :tag "Size"
251 (const :tag "none" nil)
252 (integer :tag "size")))))
253 :group 'font-lock)
254 (make-obsolete-variable 'font-lock-maximum-size nil "24.1")
256 (defcustom font-lock-maximum-decoration t
257 "Maximum decoration level for fontification.
258 If nil, use the default decoration (typically the minimum available).
259 If t, use the maximum decoration available.
260 If a number, use that level of decoration (or if not available the maximum).
261 The higher the number, the more decoration is done.
262 If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
263 where MAJOR-MODE is a symbol or t (meaning the default). For example:
264 ((c-mode . t) (c++-mode . 2) (t . 1))
265 means use the maximum decoration available for buffers in C mode, level 2
266 decoration for buffers in C++ mode, and level 1 decoration otherwise."
267 :type '(choice (const :tag "default" nil)
268 (const :tag "maximum" t)
269 (integer :tag "level" 1)
270 (repeat :menu-tag "mode specific" :tag "mode specific"
271 :value ((t . t))
272 (cons :tag "Instance"
273 (radio :tag "Mode"
274 (const :tag "all" t)
275 (symbol :tag "name"))
276 (radio :tag "Decoration"
277 (const :tag "default" nil)
278 (const :tag "maximum" t)
279 (integer :tag "level" 1)))))
280 :group 'font-lock)
282 (defcustom font-lock-verbose nil
283 "If non-nil, means show status messages for buffer fontification.
284 If a number, only buffers greater than this size have fontification messages."
285 :type '(choice (const :tag "never" nil)
286 (other :tag "always" t)
287 (integer :tag "size"))
288 :group 'font-lock
289 :version "24.1")
292 ;; Originally these variable values were face names such as `bold' etc.
293 ;; Now we create our own faces, but we keep these variables for compatibility
294 ;; and they give users another mechanism for changing face appearance.
295 ;; We now allow a FACENAME in `font-lock-keywords' to be any expression that
296 ;; returns a face. So the easiest thing is to continue using these variables,
297 ;; rather than sometimes evalling FACENAME and sometimes not. sm.
299 ;; Note that in new code, in the vast majority of cases there is no
300 ;; need to create variables that specify face names. Simply using
301 ;; faces directly is enough. Font-lock is not a template to be
302 ;; followed in this area.
303 (defvar font-lock-comment-face 'font-lock-comment-face
304 "Face name to use for comments.")
306 (defvar font-lock-comment-delimiter-face 'font-lock-comment-delimiter-face
307 "Face name to use for comment delimiters.")
309 (defvar font-lock-string-face 'font-lock-string-face
310 "Face name to use for strings.")
312 (defvar font-lock-doc-face 'font-lock-doc-face
313 "Face name to use for documentation.")
315 (defvar font-lock-keyword-face 'font-lock-keyword-face
316 "Face name to use for keywords.")
318 (defvar font-lock-builtin-face 'font-lock-builtin-face
319 "Face name to use for builtins.")
321 (defvar font-lock-function-name-face 'font-lock-function-name-face
322 "Face name to use for function names.")
324 (defvar font-lock-variable-name-face 'font-lock-variable-name-face
325 "Face name to use for variable names.")
327 (defvar font-lock-type-face 'font-lock-type-face
328 "Face name to use for type and class names.")
330 (defvar font-lock-constant-face 'font-lock-constant-face
331 "Face name to use for constant and label names.")
333 (defvar font-lock-warning-face 'font-lock-warning-face
334 "Face name to use for things that should stand out.")
336 (defvar font-lock-negation-char-face 'font-lock-negation-char-face
337 "Face name to use for easy to overlook negation.
338 This can be an \"!\" or the \"n\" in \"ifndef\".")
340 (defvar font-lock-preprocessor-face 'font-lock-preprocessor-face
341 "Face name to use for preprocessor directives.")
343 (defvar font-lock-reference-face 'font-lock-constant-face)
344 (make-obsolete-variable 'font-lock-reference-face 'font-lock-constant-face "20.3")
346 ;; Fontification variables:
348 (defvar font-lock-keywords nil
349 "A list of the keywords to highlight.
350 There are two kinds of values: user-level, and compiled.
352 A user-level keywords list is what a major mode or the user would
353 set up. Normally the list would come from `font-lock-defaults'.
354 through selection of a fontification level and evaluation of any
355 contained expressions. You can also alter it by calling
356 `font-lock-add-keywords' or `font-lock-remove-keywords' with MODE = nil.
358 Each element in a user-level keywords list should have one of these forms:
360 MATCHER
361 (MATCHER . SUBEXP)
362 (MATCHER . FACENAME)
363 (MATCHER . HIGHLIGHT)
364 (MATCHER HIGHLIGHT ...)
365 (eval . FORM)
367 where MATCHER can be either the regexp to search for, or the function name to
368 call to make the search (called with one argument, the limit of the search;
369 it should return non-nil, move point, and set `match-data' appropriately if
370 it succeeds; like `re-search-forward' would).
371 MATCHER regexps can be generated via the function `regexp-opt'.
373 FORM is an expression, whose value should be a keyword element, evaluated when
374 the keyword is (first) used in a buffer. This feature can be used to provide a
375 keyword that can only be generated when Font Lock mode is actually turned on.
377 HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
379 For highlighting single items, for example each instance of the word \"foo\",
380 typically only MATCH-HIGHLIGHT is required.
381 However, if an item or (typically) items are to be highlighted following the
382 instance of another item (the anchor), for example each instance of the
383 word \"bar\" following the word \"anchor\" then MATCH-ANCHORED may be required.
385 MATCH-HIGHLIGHT should be of the form:
387 (SUBEXP FACENAME [OVERRIDE [LAXMATCH]])
389 SUBEXP is the number of the subexpression of MATCHER to be highlighted.
391 FACENAME is an expression whose value is the face name to use.
392 Instead of a face, FACENAME can evaluate to a property list
393 of the form (face FACE PROP1 VAL1 PROP2 VAL2 ...)
394 in which case all the listed text-properties will be set rather than
395 just FACE. In such a case, you will most likely want to put those
396 properties in `font-lock-extra-managed-props' or to override
397 `font-lock-unfontify-region-function'.
399 OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification can
400 be overwritten. If `keep', only parts not already fontified are highlighted.
401 If `prepend' or `append', existing fontification is merged with the new, in
402 which the new or existing fontification, respectively, takes precedence.
403 If LAXMATCH is non-nil, that means don't signal an error if there is
404 no match for SUBEXP in MATCHER.
406 For example, an element of the form highlights (if not already highlighted):
408 \"\\\\\\=<foo\\\\\\=>\" discrete occurrences of \"foo\" in the value of the
409 variable `font-lock-keyword-face'.
410 (\"fu\\\\(bar\\\\)\" . 1) substring \"bar\" within all occurrences of \"fubar\" in
411 the value of `font-lock-keyword-face'.
412 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'.
413 (\"foo\\\\|bar\" 0 foo-bar-face t)
414 occurrences of either \"foo\" or \"bar\" in the value
415 of `foo-bar-face', even if already highlighted.
416 (fubar-match 1 fubar-face)
417 the first subexpression within all occurrences of
418 whatever the function `fubar-match' finds and matches
419 in the value of `fubar-face'.
421 MATCH-ANCHORED should be of the form:
423 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
425 where MATCHER is a regexp to search for or the function name to call to make
426 the search, as for MATCH-HIGHLIGHT above, but with one exception; see below.
427 PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
428 the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be
429 used to initialize before, and cleanup after, MATCHER is used. Typically,
430 PRE-MATCH-FORM is used to move to some position relative to the original
431 MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might
432 be used to move back, before resuming with MATCH-ANCHORED's parent's MATCHER.
434 For example, an element of the form highlights (if not already highlighted):
436 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
438 discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
439 discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
440 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is
441 initially searched for starting from the end of the match of \"anchor\", and
442 searching for subsequent instances of \"anchor\" resumes from where searching
443 for \"item\" concluded.)
445 The above-mentioned exception is as follows. The limit of the MATCHER search
446 defaults to the end of the line after PRE-MATCH-FORM is evaluated.
447 However, if PRE-MATCH-FORM returns a position greater than the position after
448 PRE-MATCH-FORM is evaluated, that position is used as the limit of the search.
449 It is generally a bad idea to return a position greater than the end of the
450 line, i.e., cause the MATCHER search to span lines.
452 These regular expressions can match text which spans lines, although
453 it is better to avoid it if possible since updating them while editing
454 text is slower, and it is not guaranteed to be always correct when using
455 support modes like jit-lock or lazy-lock.
457 This variable is set by major modes via the variable `font-lock-defaults'.
458 Be careful when composing regexps for this list; a poorly written pattern can
459 dramatically slow things down!
461 A compiled keywords list starts with t. It is produced internal
462 by `font-lock-compile-keywords' from a user-level keywords list.
463 Its second element is the user-level keywords list that was
464 compiled. The remaining elements have the same form as
465 user-level keywords, but normally their values have been
466 optimized.")
468 (defvar font-lock-keywords-alist nil
469 "Alist of additional `font-lock-keywords' elements for major modes.
471 Each element has the form (MODE KEYWORDS . HOW).
472 `font-lock-set-defaults' adds the elements in the list KEYWORDS to
473 `font-lock-keywords' when Font Lock is turned on in major mode MODE.
475 If HOW is nil, KEYWORDS are added at the beginning of
476 `font-lock-keywords'. If it is `set', they are used to replace the
477 value of `font-lock-keywords'. If HOW is any other non-nil value,
478 they are added at the end.
480 This is normally set via `font-lock-add-keywords' and
481 `font-lock-remove-keywords'.")
482 (put 'font-lock-keywords-alist 'risky-local-variable t)
484 (defvar font-lock-removed-keywords-alist nil
485 "Alist of `font-lock-keywords' elements to be removed for major modes.
487 Each element has the form (MODE . KEYWORDS). `font-lock-set-defaults'
488 removes the elements in the list KEYWORDS from `font-lock-keywords'
489 when Font Lock is turned on in major mode MODE.
491 This is normally set via `font-lock-add-keywords' and
492 `font-lock-remove-keywords'.")
494 (defvar font-lock-keywords-only nil
495 "*Non-nil means Font Lock should not fontify comments or strings.
496 This is normally set via `font-lock-defaults'.")
498 (defvar font-lock-keywords-case-fold-search nil
499 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
500 This is set via the function `font-lock-set-defaults', based on
501 the CASE-FOLD argument of `font-lock-defaults'.")
502 (make-variable-buffer-local 'font-lock-keywords-case-fold-search)
504 (defvar font-lock-syntactically-fontified 0
505 "Point up to which `font-lock-syntactic-keywords' has been applied.
506 If nil, this is ignored, in which case the syntactic fontification may
507 sometimes be slightly incorrect.")
508 (make-variable-buffer-local 'font-lock-syntactically-fontified)
510 (defvar font-lock-syntactic-face-function
511 (lambda (state)
512 (if (nth 3 state) font-lock-string-face font-lock-comment-face))
513 "Function to determine which face to use when fontifying syntactically.
514 The function is called with a single parameter (the state as returned by
515 `parse-partial-sexp' at the beginning of the region to highlight) and
516 should return a face. This is normally set via `font-lock-defaults'.")
518 (defvar font-lock-syntactic-keywords nil
519 "A list of the syntactic keywords to put syntax properties on.
520 The value can be the list itself, or the name of a function or variable
521 whose value is the list.
523 See `font-lock-keywords' for a description of the form of this list;
524 only the differences are stated here. MATCH-HIGHLIGHT should be of the form:
526 (SUBEXP SYNTAX OVERRIDE LAXMATCH)
528 where SYNTAX can be a string (as taken by `modify-syntax-entry'), a syntax
529 table, a cons cell (as returned by `string-to-syntax') or an expression whose
530 value is such a form. OVERRIDE cannot be `prepend' or `append'.
532 Here are two examples of elements of `font-lock-syntactic-keywords'
533 and what they do:
535 (\"\\\\$\\\\(#\\\\)\" 1 \".\")
537 gives a hash character punctuation syntax (\".\") when following a
538 dollar-sign character. Hash characters in other contexts will still
539 follow whatever the syntax table says about the hash character.
541 (\"\\\\('\\\\).\\\\('\\\\)\"
542 (1 \"\\\"\")
543 (2 \"\\\"\"))
545 gives a pair single-quotes, which surround a single character, a SYNTAX of
546 \"\\\"\" (meaning string quote syntax). Single-quote characters in other
547 contexts will not be affected.
549 This is normally set via `font-lock-defaults'.")
550 (make-obsolete-variable 'font-lock-syntactic-keywords
551 'syntax-propertize-function "24.1")
553 (defvar font-lock-syntax-table nil
554 "Non-nil means use this syntax table for fontifying.
555 If this is nil, the major mode's syntax table is used.
556 This is normally set via `font-lock-defaults'.")
558 (defvar font-lock-beginning-of-syntax-function nil
559 "*Non-nil means use this function to move back outside all constructs.
560 When called with no args it should move point backward to a place which
561 is not in a string or comment and not within any bracket-pairs (or else,
562 a place such that any bracket-pairs outside it can be ignored for Emacs
563 syntax analysis and fontification).
565 If this is nil, Font Lock uses `syntax-begin-function' to move back
566 outside of any comment, string, or sexp. This variable is semi-obsolete;
567 we recommend setting `syntax-begin-function' instead.
569 This is normally set via `font-lock-defaults'.")
570 (make-obsolete-variable 'font-lock-beginning-of-syntax-function
571 'syntax-begin-function "23.3" 'set)
573 (defvar font-lock-mark-block-function nil
574 "*Non-nil means use this function to mark a block of text.
575 When called with no args it should leave point at the beginning of any
576 enclosing textual block and mark at the end.
577 This is normally set via `font-lock-defaults'.")
579 (defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
580 "Function to use for fontifying the buffer.
581 This is normally set via `font-lock-defaults'.")
583 (defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
584 "Function to use for unfontifying the buffer.
585 This is used when turning off Font Lock mode.
586 This is normally set via `font-lock-defaults'.")
588 (defvar font-lock-fontify-region-function 'font-lock-default-fontify-region
589 "Function to use for fontifying a region.
590 It should take two args, the beginning and end of the region, and an optional
591 third arg VERBOSE. If VERBOSE is non-nil, the function should print status
592 messages. This is normally set via `font-lock-defaults'.")
594 (defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
595 "Function to use for unfontifying a region.
596 It should take two args, the beginning and end of the region.
597 This is normally set via `font-lock-defaults'.")
599 (defvar font-lock-inhibit-thing-lock nil
600 "List of Font Lock mode related modes that should not be turned on.
601 Currently, valid mode names are `fast-lock-mode', `jit-lock-mode' and
602 `lazy-lock-mode'. This is normally set via `font-lock-defaults'.")
604 (defvar font-lock-multiline nil
605 "Whether font-lock should cater to multiline keywords.
606 If nil, don't try to handle multiline patterns.
607 If t, always handle multiline patterns.
608 If `undecided', don't try to handle multiline patterns until you see one.
609 Major/minor modes can set this variable if they know which option applies.")
611 (defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
613 ;; Font Lock mode.
615 (eval-when-compile
617 ;; We don't do this at the top-level as we only use non-autoloaded macros.
618 (require 'cl)
620 ;; Borrowed from lazy-lock.el.
621 ;; We use this to preserve or protect things when modifying text properties.
622 (defmacro save-buffer-state (&rest body)
623 "Bind variables according to VARLIST and eval BODY restoring buffer state."
624 (declare (indent 0) (debug t))
625 `(let ((inhibit-point-motion-hooks t))
626 (with-silent-modifications
627 ,@body)))
629 ;; Shut up the byte compiler.
630 (defvar font-lock-face-attributes)) ; Obsolete but respected if set.
632 (defun font-lock-specified-p (mode)
633 "Return non-nil if the current buffer is ready for fontification.
634 The MODE argument, if non-nil, means Font Lock mode is about to
635 be enabled."
636 (or font-lock-defaults
637 (and (boundp 'font-lock-keywords)
638 font-lock-keywords)
639 (and mode
640 (boundp 'font-lock-set-defaults)
641 font-lock-set-defaults
642 font-lock-major-mode
643 (not (eq font-lock-major-mode major-mode)))))
645 (defun font-lock-initial-fontify ()
646 ;; The first fontification after turning the mode on. This must
647 ;; only be called after the mode hooks have been run.
648 (when (and font-lock-mode
649 (font-lock-specified-p t))
650 (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size)))
651 (cond (font-lock-fontified
652 nil)
653 ((or (null max-size) (> max-size (buffer-size)))
654 (font-lock-fontify-buffer))
655 (font-lock-verbose
656 (message "Fontifying %s...buffer size greater than font-lock-maximum-size"
657 (buffer-name)))))))
659 (defun font-lock-mode-internal (arg)
660 ;; Turn on Font Lock mode.
661 (when arg
662 (add-hook 'after-change-functions 'font-lock-after-change-function t t)
663 (font-lock-set-defaults)
664 (font-lock-turn-on-thing-lock))
665 ;; Turn off Font Lock mode.
666 (unless font-lock-mode
667 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
668 (font-lock-unfontify-buffer)
669 (font-lock-turn-off-thing-lock)))
671 (defun font-lock-add-keywords (mode keywords &optional how)
672 "Add highlighting KEYWORDS for MODE.
674 MODE should be a symbol, the major mode command name, such as `c-mode'
675 or nil. If nil, highlighting keywords are added for the current buffer.
676 KEYWORDS should be a list; see the variable `font-lock-keywords'.
677 By default they are added at the beginning of the current highlighting list.
678 If optional argument HOW is `set', they are used to replace the current
679 highlighting list. If HOW is any other non-nil value, they are added at the
680 end of the current highlighting list.
682 For example:
684 (font-lock-add-keywords 'c-mode
685 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
686 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . font-lock-keyword-face)))
688 adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
689 comments, and to fontify `and', `or' and `not' words as keywords.
691 The above procedure will only add the keywords for C mode, not
692 for modes derived from C mode. To add them for derived modes too,
693 pass nil for MODE and add the call to c-mode-hook.
695 For example:
697 (add-hook 'c-mode-hook
698 (lambda ()
699 (font-lock-add-keywords nil
700 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
701 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" .
702 font-lock-keyword-face)))))
704 The above procedure may fail to add keywords to derived modes if
705 some involved major mode does not follow the standard conventions.
706 File a bug report if this happens, so the major mode can be corrected.
708 Note that some modes have specialized support for additional patterns, e.g.,
709 see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
710 `objc-font-lock-extra-types' and `java-font-lock-extra-types'."
711 (cond (mode
712 ;; If MODE is non-nil, add the KEYWORDS and HOW spec to
713 ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
714 (let ((spec (cons keywords how)) cell)
715 (if (setq cell (assq mode font-lock-keywords-alist))
716 (if (eq how 'set)
717 (setcdr cell (list spec))
718 (setcdr cell (append (cdr cell) (list spec))))
719 (push (list mode spec) font-lock-keywords-alist)))
720 ;; Make sure that `font-lock-removed-keywords-alist' does not
721 ;; contain the new keywords.
722 (font-lock-update-removed-keyword-alist mode keywords how))
724 (when (and font-lock-mode
725 (not (or font-lock-keywords font-lock-defaults)))
726 ;; The major mode has not set any keywords, so when we enabled
727 ;; font-lock-mode it only enabled the font-core.el part, not the
728 ;; font-lock-mode-internal. Try again.
729 (font-lock-mode -1)
730 (set (make-local-variable 'font-lock-defaults) '(nil t))
731 (font-lock-mode 1))
732 ;; Otherwise set or add the keywords now.
733 ;; This is a no-op if it has been done already in this buffer
734 ;; for the correct major mode.
735 (font-lock-set-defaults)
736 (let ((was-compiled (eq (car font-lock-keywords) t)))
737 ;; Bring back the user-level (uncompiled) keywords.
738 (if was-compiled
739 (setq font-lock-keywords (cadr font-lock-keywords)))
740 ;; Now modify or replace them.
741 (if (eq how 'set)
742 (setq font-lock-keywords keywords)
743 (font-lock-remove-keywords nil keywords) ;to avoid duplicates
744 (let ((old (if (eq (car-safe font-lock-keywords) t)
745 (cdr font-lock-keywords)
746 font-lock-keywords)))
747 (setq font-lock-keywords (if how
748 (append old keywords)
749 (append keywords old)))))
750 ;; If the keywords were compiled before, compile them again.
751 (if was-compiled
752 (setq font-lock-keywords
753 (font-lock-compile-keywords font-lock-keywords)))))))
755 (defun font-lock-update-removed-keyword-alist (mode keywords how)
756 "Update `font-lock-removed-keywords-alist' when adding new KEYWORDS to MODE."
757 ;; When font-lock is enabled first all keywords in the list
758 ;; `font-lock-keywords-alist' are added, then all keywords in the
759 ;; list `font-lock-removed-keywords-alist' are removed. If a
760 ;; keyword was once added, removed, and then added again it must be
761 ;; removed from the removed-keywords list. Otherwise the second add
762 ;; will not take effect.
763 (let ((cell (assq mode font-lock-removed-keywords-alist)))
764 (if cell
765 (if (eq how 'set)
766 ;; A new set of keywords is defined. Forget all about
767 ;; our old keywords that should be removed.
768 (setq font-lock-removed-keywords-alist
769 (delq cell font-lock-removed-keywords-alist))
770 ;; Delete all previously removed keywords.
771 (dolist (kword keywords)
772 (setcdr cell (delete kword (cdr cell))))
773 ;; Delete the mode cell if empty.
774 (if (null (cdr cell))
775 (setq font-lock-removed-keywords-alist
776 (delq cell font-lock-removed-keywords-alist)))))))
778 ;; Written by Anders Lindgren <andersl@andersl.com>.
780 ;; Case study:
781 ;; (I) The keywords are removed from a major mode.
782 ;; In this case the keyword could be local (i.e. added earlier by
783 ;; `font-lock-add-keywords'), global, or both.
785 ;; (a) In the local case we remove the keywords from the variable
786 ;; `font-lock-keywords-alist'.
788 ;; (b) The actual global keywords are not known at this time.
789 ;; All keywords are added to `font-lock-removed-keywords-alist',
790 ;; when font-lock is enabled those keywords are removed.
792 ;; Note that added keywords are taken out of the list of removed
793 ;; keywords. This ensure correct operation when the same keyword
794 ;; is added and removed several times.
796 ;; (II) The keywords are removed from the current buffer.
797 (defun font-lock-remove-keywords (mode keywords)
798 "Remove highlighting KEYWORDS for MODE.
800 MODE should be a symbol, the major mode command name, such as `c-mode'
801 or nil. If nil, highlighting keywords are removed for the current buffer.
803 To make the removal apply to modes derived from MODE as well,
804 pass nil for MODE and add the call to MODE-hook. This may fail
805 for some derived modes if some involved major mode does not
806 follow the standard conventions. File a bug report if this
807 happens, so the major mode can be corrected."
808 (cond (mode
809 ;; Remove one keyword at the time.
810 (dolist (keyword keywords)
811 (let ((top-cell (assq mode font-lock-keywords-alist)))
812 ;; If MODE is non-nil, remove the KEYWORD from
813 ;; `font-lock-keywords-alist'.
814 (when top-cell
815 (dolist (keyword-list-how-pair (cdr top-cell))
816 ;; `keywords-list-how-pair' is a cons with a list of
817 ;; keywords in the car top-cell and the original how
818 ;; argument in the cdr top-cell.
819 (setcar keyword-list-how-pair
820 (delete keyword (car keyword-list-how-pair))))
821 ;; Remove keyword list/how pair when the keyword list
822 ;; is empty and how doesn't specify `set'. (If it
823 ;; should be deleted then previously deleted keywords
824 ;; would appear again.)
825 (let ((cell top-cell))
826 (while (cdr cell)
827 (if (and (null (car (car (cdr cell))))
828 (not (eq (cdr (car (cdr cell))) 'set)))
829 (setcdr cell (cdr (cdr cell)))
830 (setq cell (cdr cell)))))
831 ;; Final cleanup, remove major mode cell if last keyword
832 ;; was deleted.
833 (if (null (cdr top-cell))
834 (setq font-lock-keywords-alist
835 (delq top-cell font-lock-keywords-alist))))
836 ;; Remember the keyword in case it is not local.
837 (let ((cell (assq mode font-lock-removed-keywords-alist)))
838 (if cell
839 (unless (member keyword (cdr cell))
840 (nconc cell (list keyword)))
841 (push (cons mode (list keyword))
842 font-lock-removed-keywords-alist))))))
844 ;; Otherwise remove it immediately.
845 (font-lock-set-defaults)
846 (let ((was-compiled (eq (car font-lock-keywords) t)))
847 ;; Bring back the user-level (uncompiled) keywords.
848 (if was-compiled
849 (setq font-lock-keywords (cadr font-lock-keywords)))
851 ;; Edit them.
852 (setq font-lock-keywords (copy-sequence font-lock-keywords))
853 (dolist (keyword keywords)
854 (setq font-lock-keywords
855 (delete keyword font-lock-keywords)))
857 ;; If the keywords were compiled before, compile them again.
858 (if was-compiled
859 (setq font-lock-keywords
860 (font-lock-compile-keywords font-lock-keywords)))))))
862 ;;; Font Lock Support mode.
864 ;; This is the code used to interface font-lock.el with any of its add-on
865 ;; packages, and provide the user interface. Packages that have their own
866 ;; local buffer fontification functions (see below) may have to call
867 ;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
868 ;; themselves.
870 (defcustom font-lock-support-mode 'jit-lock-mode
871 "Support mode for Font Lock mode.
872 Support modes speed up Font Lock mode by being choosy about when fontification
873 occurs. The default support mode, Just-in-time Lock mode (symbol
874 `jit-lock-mode'), is recommended.
876 Other, older support modes are Fast Lock mode (symbol `fast-lock-mode') and
877 Lazy Lock mode (symbol `lazy-lock-mode'). See those modes for more info.
878 However, they are no longer recommended, as Just-in-time Lock mode is better.
880 If nil, means support for Font Lock mode is never performed.
881 If a symbol, use that support mode.
882 If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
883 where MAJOR-MODE is a symbol or t (meaning the default). For example:
884 ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
885 means that Fast Lock mode is used to support Font Lock mode for buffers in C or
886 C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
888 The value of this variable is used when Font Lock mode is turned on."
889 :type '(choice (const :tag "none" nil)
890 (const :tag "fast lock" fast-lock-mode)
891 (const :tag "lazy lock" lazy-lock-mode)
892 (const :tag "jit lock" jit-lock-mode)
893 (repeat :menu-tag "mode specific" :tag "mode specific"
894 :value ((t . jit-lock-mode))
895 (cons :tag "Instance"
896 (radio :tag "Mode"
897 (const :tag "all" t)
898 (symbol :tag "name"))
899 (radio :tag "Support"
900 (const :tag "none" nil)
901 (const :tag "fast lock" fast-lock-mode)
902 (const :tag "lazy lock" lazy-lock-mode)
903 (const :tag "JIT lock" jit-lock-mode)))
905 :version "21.1"
906 :group 'font-lock)
908 (defvar fast-lock-mode)
909 (defvar lazy-lock-mode)
910 (defvar jit-lock-mode)
912 (declare-function fast-lock-after-fontify-buffer "fast-lock")
913 (declare-function fast-lock-after-unfontify-buffer "fast-lock")
914 (declare-function fast-lock-mode "fast-lock")
915 (declare-function lazy-lock-after-fontify-buffer "lazy-lock")
916 (declare-function lazy-lock-after-unfontify-buffer "lazy-lock")
917 (declare-function lazy-lock-mode "lazy-lock")
919 (defun font-lock-turn-on-thing-lock ()
920 (case (font-lock-value-in-major-mode font-lock-support-mode)
921 (fast-lock-mode (fast-lock-mode t))
922 (lazy-lock-mode (lazy-lock-mode t))
923 (jit-lock-mode
924 ;; Prepare for jit-lock
925 (remove-hook 'after-change-functions
926 'font-lock-after-change-function t)
927 (set (make-local-variable 'font-lock-fontify-buffer-function)
928 'jit-lock-refontify)
929 ;; Don't fontify eagerly (and don't abort if the buffer is large).
930 (set (make-local-variable 'font-lock-fontified) t)
931 ;; Use jit-lock.
932 (jit-lock-register 'font-lock-fontify-region
933 (not font-lock-keywords-only))
934 ;; Tell jit-lock how we extend the region to refontify.
935 (add-hook 'jit-lock-after-change-extend-region-functions
936 'font-lock-extend-jit-lock-region-after-change
937 nil t))))
939 (defun font-lock-turn-off-thing-lock ()
940 (cond ((bound-and-true-p fast-lock-mode)
941 (fast-lock-mode -1))
942 ((bound-and-true-p jit-lock-mode)
943 (jit-lock-unregister 'font-lock-fontify-region)
944 ;; Reset local vars to the non-jit-lock case.
945 (kill-local-variable 'font-lock-fontify-buffer-function))
946 ((bound-and-true-p lazy-lock-mode)
947 (lazy-lock-mode -1))))
949 (defun font-lock-after-fontify-buffer ()
950 (cond ((bound-and-true-p fast-lock-mode)
951 (fast-lock-after-fontify-buffer))
952 ;; Useless now that jit-lock intercepts font-lock-fontify-buffer. -sm
953 ;; (jit-lock-mode
954 ;; (jit-lock-after-fontify-buffer))
955 ((bound-and-true-p lazy-lock-mode)
956 (lazy-lock-after-fontify-buffer))))
958 (defun font-lock-after-unfontify-buffer ()
959 (cond ((bound-and-true-p fast-lock-mode)
960 (fast-lock-after-unfontify-buffer))
961 ;; Useless as well. It's only called when:
962 ;; - turning off font-lock: it does not matter if we leave spurious
963 ;; `fontified' text props around since jit-lock-mode is also off.
964 ;; - font-lock-default-fontify-buffer fails: this is not run
965 ;; any more anyway. -sm
967 ;; (jit-lock-mode
968 ;; (jit-lock-after-unfontify-buffer))
969 ((bound-and-true-p lazy-lock-mode)
970 (lazy-lock-after-unfontify-buffer))))
972 ;;; End of Font Lock Support mode.
974 ;;; Fontification functions.
976 ;; Rather than the function, e.g., `font-lock-fontify-region' containing the
977 ;; code to fontify a region, the function runs the function whose name is the
978 ;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally,
979 ;; the value of this variable is, e.g., `font-lock-default-fontify-region'
980 ;; which does contain the code to fontify a region. However, the value of the
981 ;; variable could be anything and thus, e.g., `font-lock-fontify-region' could
982 ;; do anything. The indirection of the fontification functions gives major
983 ;; modes the capability of modifying the way font-lock.el fontifies. Major
984 ;; modes can modify the values of, e.g., `font-lock-fontify-region-function',
985 ;; via the variable `font-lock-defaults'.
987 ;; For example, Rmail mode sets the variable `font-lock-defaults' so that
988 ;; font-lock.el uses its own function for buffer fontification. This function
989 ;; makes fontification be on a message-by-message basis and so visiting an
990 ;; RMAIL file is much faster. A clever implementation of the function might
991 ;; fontify the headers differently than the message body. (It should, and
992 ;; correspondingly for Mail mode, but I can't be bothered to do the work. Can
993 ;; you?) This hints at a more interesting use...
995 ;; Languages that contain text normally contained in different major modes
996 ;; could define their own fontification functions that treat text differently
997 ;; depending on its context. For example, Perl mode could arrange that here
998 ;; docs are fontified differently than Perl code. Or Yacc mode could fontify
999 ;; rules one way and C code another. Neat!
1001 ;; A further reason to use the fontification indirection feature is when the
1002 ;; default syntactic fontification, or the default fontification in general,
1003 ;; is not flexible enough for a particular major mode. For example, perhaps
1004 ;; comments are just too hairy for `font-lock-fontify-syntactically-region' to
1005 ;; cope with. You need to write your own version of that function, e.g.,
1006 ;; `hairy-fontify-syntactically-region', and make your own version of
1007 ;; `hairy-fontify-region' call that function before calling
1008 ;; `font-lock-fontify-keywords-region' for the normal regexp fontification
1009 ;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el
1010 ;; would call your region fontification function instead of its own. For
1011 ;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line
1012 ;; directives correctly and cleanly. (It is the same problem as fontifying
1013 ;; multi-line strings and comments; regexps are not appropriate for the job.)
1015 (defvar font-lock-extend-after-change-region-function nil
1016 "A function that determines the region to refontify after a change.
1018 This variable is either nil, or is a function that determines the
1019 region to refontify after a change.
1020 It is usually set by the major mode via `font-lock-defaults'.
1021 Font-lock calls this function after each buffer change.
1023 The function is given three parameters, the standard BEG, END, and OLD-LEN
1024 from `after-change-functions'. It should return either a cons of the beginning
1025 and end buffer positions \(in that order) of the region to refontify, or nil
1026 \(which directs the caller to fontify a default region).
1027 This function should preserve the match-data.
1028 The region it returns may start or end in the middle of a line.")
1029 (make-variable-buffer-local 'font-lock-extend-after-change-region-function)
1031 (defun font-lock-fontify-buffer ()
1032 "Fontify the current buffer the way the function `font-lock-mode' would."
1033 (interactive)
1034 (font-lock-set-defaults)
1035 (let ((font-lock-verbose (or font-lock-verbose
1036 (called-interactively-p 'interactive))))
1037 (funcall font-lock-fontify-buffer-function)))
1039 (defun font-lock-unfontify-buffer ()
1040 (funcall font-lock-unfontify-buffer-function))
1042 (defun font-lock-fontify-region (beg end &optional loudly)
1043 "Fontify the text between BEG and END.
1044 If LOUDLY is non-nil, print status messages while fontifying.
1045 This works by calling `font-lock-fontify-region-function'."
1046 (font-lock-set-defaults)
1047 (funcall font-lock-fontify-region-function beg end loudly))
1049 (defun font-lock-unfontify-region (beg end)
1050 "Unfontify the text between BEG and END.
1051 This works by calling `font-lock-unfontify-region-function'."
1052 (save-buffer-state
1053 (funcall font-lock-unfontify-region-function beg end)))
1055 (defun font-lock-default-fontify-buffer ()
1056 "Fontify the whole buffer using `font-lock-fontify-region-function'."
1057 (let ((verbose (if (numberp font-lock-verbose)
1058 (> (buffer-size) font-lock-verbose)
1059 font-lock-verbose)))
1060 (with-temp-message
1061 (when verbose
1062 (format "Fontifying %s..." (buffer-name)))
1063 ;; Make sure we fontify etc. in the whole buffer.
1064 (save-restriction
1065 (widen)
1066 (condition-case nil
1067 (save-excursion
1068 (save-match-data
1069 (font-lock-fontify-region (point-min) (point-max) verbose)
1070 (font-lock-after-fontify-buffer)
1071 (setq font-lock-fontified t)))
1072 ;; We don't restore the old fontification, so it's best to unfontify.
1073 (quit (font-lock-unfontify-buffer)))))))
1075 (defun font-lock-default-unfontify-buffer ()
1076 "Unfontify the whole buffer using `font-lock-unfontify-region-function'."
1077 ;; Make sure we unfontify etc. in the whole buffer.
1078 (save-restriction
1079 (widen)
1080 (font-lock-unfontify-region (point-min) (point-max))
1081 (font-lock-after-unfontify-buffer)
1082 (setq font-lock-fontified nil)))
1084 (defvar font-lock-dont-widen nil
1085 "If non-nil, font-lock will work on the non-widened buffer.
1086 Useful for things like RMAIL and Info where the whole buffer is not
1087 a very meaningful entity to highlight.")
1090 (defvar font-lock-beg) (defvar font-lock-end)
1091 (defvar font-lock-extend-region-functions
1092 '(font-lock-extend-region-wholelines
1093 ;; This use of font-lock-multiline property is unreliable but is just
1094 ;; a handy heuristic: in case you don't have a function that does
1095 ;; /identification/ of multiline elements, you may still occasionally
1096 ;; discover them by accident (or you may /identify/ them but not in all
1097 ;; cases), in which case the font-lock-multiline property can help make
1098 ;; sure you will properly *re*identify them during refontification.
1099 font-lock-extend-region-multiline)
1100 "Special hook run just before proceeding to fontify a region.
1101 This is used to allow major modes to help font-lock find safe buffer positions
1102 as beginning and end of the fontified region. Its most common use is to solve
1103 the problem of /identification/ of multiline elements by providing a function
1104 that tries to find such elements and move the boundaries such that they do
1105 not fall in the middle of one.
1106 Each function is called with no argument; it is expected to adjust the
1107 dynamically bound variables `font-lock-beg' and `font-lock-end'; and return
1108 non-nil if it did make such an adjustment.
1109 These functions are run in turn repeatedly until they all return nil.
1110 Put first the functions more likely to cause a change and cheaper to compute.")
1111 ;; Mark it as a special hook which doesn't use any global setting
1112 ;; (i.e. doesn't obey the element t in the buffer-local value).
1113 (make-variable-buffer-local 'font-lock-extend-region-functions)
1115 (defun font-lock-extend-region-multiline ()
1116 "Move fontification boundaries away from any `font-lock-multiline' property."
1117 (let ((changed nil))
1118 (when (and (> font-lock-beg (point-min))
1119 (get-text-property (1- font-lock-beg) 'font-lock-multiline))
1120 (setq changed t)
1121 (setq font-lock-beg (or (previous-single-property-change
1122 font-lock-beg 'font-lock-multiline)
1123 (point-min))))
1125 (when (get-text-property font-lock-end 'font-lock-multiline)
1126 (setq changed t)
1127 (setq font-lock-end (or (text-property-any font-lock-end (point-max)
1128 'font-lock-multiline nil)
1129 (point-max))))
1130 changed))
1132 (defun font-lock-extend-region-wholelines ()
1133 "Move fontification boundaries to beginning of lines."
1134 (let ((changed nil))
1135 (goto-char font-lock-beg)
1136 (unless (bolp)
1137 (setq changed t font-lock-beg (line-beginning-position)))
1138 (goto-char font-lock-end)
1139 (unless (bolp)
1140 (unless (eq font-lock-end
1141 (setq font-lock-end (line-beginning-position 2)))
1142 (setq changed t)))
1143 changed))
1145 (defun font-lock-default-fontify-region (beg end loudly)
1146 "Fontify the text between BEG and END.
1147 If LOUDLY is non-nil, print status messages while fontifying.
1148 This function is the default `font-lock-fontify-region-function'."
1149 (save-buffer-state
1150 ;; Use the fontification syntax table, if any.
1151 (with-syntax-table (or font-lock-syntax-table (syntax-table))
1152 (save-restriction
1153 (unless font-lock-dont-widen (widen))
1154 ;; Extend the region to fontify so that it starts and ends at
1155 ;; safe places.
1156 (let ((funs font-lock-extend-region-functions)
1157 (font-lock-beg beg)
1158 (font-lock-end end))
1159 (while funs
1160 (setq funs (if (or (not (funcall (car funs)))
1161 (eq funs font-lock-extend-region-functions))
1162 (cdr funs)
1163 ;; If there's been a change, we should go through
1164 ;; the list again since this new position may
1165 ;; warrant a different answer from one of the fun
1166 ;; we've already seen.
1167 font-lock-extend-region-functions)))
1168 (setq beg font-lock-beg end font-lock-end))
1169 ;; Now do the fontification.
1170 (font-lock-unfontify-region beg end)
1171 (when (and font-lock-syntactic-keywords
1172 (null syntax-propertize-function))
1173 ;; Ensure the beginning of the file is properly syntactic-fontified.
1174 (let ((start beg))
1175 (when (< font-lock-syntactically-fontified start)
1176 (setq start (max font-lock-syntactically-fontified (point-min)))
1177 (setq font-lock-syntactically-fontified end))
1178 (font-lock-fontify-syntactic-keywords-region start end)))
1179 (unless font-lock-keywords-only
1180 (font-lock-fontify-syntactically-region beg end loudly))
1181 (font-lock-fontify-keywords-region beg end loudly)))))
1183 ;; The following must be rethought, since keywords can override fontification.
1184 ;; ;; Now scan for keywords, but not if we are inside a comment now.
1185 ;; (or (and (not font-lock-keywords-only)
1186 ;; (let ((state (parse-partial-sexp beg end nil nil
1187 ;; font-lock-cache-state)))
1188 ;; (or (nth 4 state) (nth 7 state))))
1189 ;; (font-lock-fontify-keywords-region beg end))
1191 (defvar font-lock-extra-managed-props nil
1192 "Additional text properties managed by font-lock.
1193 This is used by `font-lock-default-unfontify-region' to decide
1194 what properties to clear before refontifying a region.")
1196 (defun font-lock-default-unfontify-region (beg end)
1197 "Unfontify the text between BEG and END.
1198 This function is the default `font-lock-unfontify-region-function'."
1199 (remove-list-of-text-properties
1200 beg end (append
1201 font-lock-extra-managed-props
1202 (if font-lock-syntactic-keywords
1203 '(syntax-table face font-lock-multiline)
1204 '(face font-lock-multiline)))))
1206 ;; Called when any modification is made to buffer text.
1207 (defun font-lock-after-change-function (beg end old-len)
1208 (save-excursion
1209 (let ((inhibit-point-motion-hooks t)
1210 (inhibit-quit t)
1211 (region (if font-lock-extend-after-change-region-function
1212 (funcall font-lock-extend-after-change-region-function
1213 beg end old-len))))
1214 (save-match-data
1215 (if region
1216 ;; Fontify the region the major mode has specified.
1217 (setq beg (car region) end (cdr region))
1218 ;; Fontify the whole lines which enclose the region.
1219 ;; Actually, this is not needed because
1220 ;; font-lock-default-fontify-region already rounds up to a whole
1221 ;; number of lines.
1222 ;; (setq beg (progn (goto-char beg) (line-beginning-position))
1223 ;; end (progn (goto-char end) (line-beginning-position 2)))
1224 (unless (eq end (point-max))
1225 ;; Rounding up to a whole number of lines should include the
1226 ;; line right after `end'. Typical case: the first char of
1227 ;; the line was deleted. Or a \n was inserted in the middle
1228 ;; of a line.
1229 (setq end (1+ end))))
1230 (font-lock-fontify-region beg end)))))
1232 (defvar jit-lock-start) (defvar jit-lock-end)
1233 (defun font-lock-extend-jit-lock-region-after-change (beg end old-len)
1234 "Function meant for `jit-lock-after-change-extend-region-functions'.
1235 This function does 2 things:
1236 - extend the region so that it not only includes the part that was modified
1237 but also the surrounding text whose highlighting may change as a consequence.
1238 - anticipate (part of) the region extension that will happen later in
1239 `font-lock-default-fontify-region', in order to avoid the need for
1240 double-redisplay in `jit-lock-fontify-now'."
1241 (save-excursion
1242 ;; First extend the region as font-lock-after-change-function would.
1243 (let ((region (if font-lock-extend-after-change-region-function
1244 (funcall font-lock-extend-after-change-region-function
1245 beg end old-len))))
1246 (if region
1247 (setq beg (min jit-lock-start (car region))
1248 end (max jit-lock-end (cdr region))))
1249 ;; Then extend the region obeying font-lock-multiline properties,
1250 ;; indicating which part of the buffer needs to be refontified.
1251 ;; !!! This is the *main* user of font-lock-multiline property !!!
1252 ;; font-lock-after-change-function could/should also do that, but it
1253 ;; doesn't need to because font-lock-default-fontify-region does
1254 ;; it anyway. Here OTOH we have no guarantee that
1255 ;; font-lock-default-fontify-region will be executed on this region
1256 ;; any time soon.
1257 ;; Note: contrary to font-lock-default-fontify-region, we do not do
1258 ;; any loop here because we are not looking for a safe spot: we just
1259 ;; mark the text whose appearance may need to change as a result of
1260 ;; the buffer modification.
1261 (when (and (> beg (point-min))
1262 (get-text-property (1- beg) 'font-lock-multiline))
1263 (setq beg (or (previous-single-property-change
1264 beg 'font-lock-multiline)
1265 (point-min))))
1266 (when (< end (point-max))
1267 (setq end
1268 (if (get-text-property end 'font-lock-multiline)
1269 (or (text-property-any end (point-max)
1270 'font-lock-multiline nil)
1271 (point-max))
1272 ;; Rounding up to a whole number of lines should include the
1273 ;; line right after `end'. Typical case: the first char of
1274 ;; the line was deleted. Or a \n was inserted in the middle
1275 ;; of a line.
1276 (1+ end))))
1277 ;; Finally, pre-enlarge the region to a whole number of lines, to try
1278 ;; and anticipate what font-lock-default-fontify-region will do, so as to
1279 ;; avoid double-redisplay.
1280 ;; We could just run `font-lock-extend-region-functions', but since
1281 ;; the only purpose is to avoid the double-redisplay, we prefer to
1282 ;; do here only the part that is cheap and most likely to be useful.
1283 (when (memq 'font-lock-extend-region-wholelines
1284 font-lock-extend-region-functions)
1285 (goto-char beg)
1286 (setq jit-lock-start (min jit-lock-start (line-beginning-position)))
1287 (goto-char end)
1288 (setq jit-lock-end
1289 (max jit-lock-end
1290 (if (bolp) (point) (line-beginning-position 2))))))))
1292 (defun font-lock-fontify-block (&optional arg)
1293 "Fontify some lines the way `font-lock-fontify-buffer' would.
1294 The lines could be a function or paragraph, or a specified number of lines.
1295 If ARG is given, fontify that many lines before and after point, or 16 lines if
1296 no ARG is given and `font-lock-mark-block-function' is nil.
1297 If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
1298 delimit the region to fontify."
1299 (interactive "P")
1300 (let ((inhibit-point-motion-hooks t) font-lock-beginning-of-syntax-function
1301 deactivate-mark)
1302 ;; Make sure we have the right `font-lock-keywords' etc.
1303 (if (not font-lock-mode) (font-lock-set-defaults))
1304 (save-excursion
1305 (save-match-data
1306 (condition-case error-data
1307 (if (or arg (not font-lock-mark-block-function))
1308 (let ((lines (if arg (prefix-numeric-value arg) 16)))
1309 (font-lock-fontify-region
1310 (save-excursion (forward-line (- lines)) (point))
1311 (save-excursion (forward-line lines) (point))))
1312 (funcall font-lock-mark-block-function)
1313 (font-lock-fontify-region (point) (mark)))
1314 ((error quit) (message "Fontifying block...%s" error-data)))))))
1316 ;;; End of Fontification functions.
1318 ;;; Additional text property functions.
1320 ;; The following text property functions should be builtins. This means they
1321 ;; should be written in C and put with all the other text property functions.
1322 ;; In the meantime, those that are used by font-lock.el are defined in Lisp
1323 ;; below and given a `font-lock-' prefix. Those that are not used are defined
1324 ;; in Lisp below and commented out. sm.
1326 (defun font-lock-prepend-text-property (start end prop value &optional object)
1327 "Prepend to one property of the text from START to END.
1328 Arguments PROP and VALUE specify the property and value to prepend to the value
1329 already in place. The resulting property values are always lists.
1330 Optional argument OBJECT is the string or buffer containing the text."
1331 (let ((val (if (listp value) value (list value))) next prev)
1332 (while (/= start end)
1333 (setq next (next-single-property-change start prop object end)
1334 prev (get-text-property start prop object))
1335 ;; Canonicalize old forms of face property.
1336 (and (memq prop '(face font-lock-face))
1337 (listp prev)
1338 (or (keywordp (car prev))
1339 (memq (car prev) '(foreground-color background-color)))
1340 (setq prev (list prev)))
1341 (put-text-property start next prop
1342 (append val (if (listp prev) prev (list prev)))
1343 object)
1344 (setq start next))))
1346 (defun font-lock-append-text-property (start end prop value &optional object)
1347 "Append to one property of the text from START to END.
1348 Arguments PROP and VALUE specify the property and value to append to the value
1349 already in place. The resulting property values are always lists.
1350 Optional argument OBJECT is the string or buffer containing the text."
1351 (let ((val (if (listp value) value (list value))) next prev)
1352 (while (/= start end)
1353 (setq next (next-single-property-change start prop object end)
1354 prev (get-text-property start prop object))
1355 ;; Canonicalize old forms of face property.
1356 (and (memq prop '(face font-lock-face))
1357 (listp prev)
1358 (or (keywordp (car prev))
1359 (memq (car prev) '(foreground-color background-color)))
1360 (setq prev (list prev)))
1361 (put-text-property start next prop
1362 (append (if (listp prev) prev (list prev)) val)
1363 object)
1364 (setq start next))))
1366 (defun font-lock-fillin-text-property (start end prop value &optional object)
1367 "Fill in one property of the text from START to END.
1368 Arguments PROP and VALUE specify the property and value to put where none are
1369 already in place. Therefore existing property values are not overwritten.
1370 Optional argument OBJECT is the string or buffer containing the text."
1371 (let ((start (text-property-any start end prop nil object)) next)
1372 (while start
1373 (setq next (next-single-property-change start prop object end))
1374 (put-text-property start next prop value object)
1375 (setq start (text-property-any next end prop nil object)))))
1377 ;; For completeness: this is to `remove-text-properties' as `put-text-property'
1378 ;; is to `add-text-properties', etc.
1379 ;;(defun remove-text-property (start end property &optional object)
1380 ;; "Remove a property from text from START to END.
1381 ;;Argument PROPERTY is the property to remove.
1382 ;;Optional argument OBJECT is the string or buffer containing the text.
1383 ;;Return t if the property was actually removed, nil otherwise."
1384 ;; (remove-text-properties start end (list property) object))
1386 ;; For consistency: maybe this should be called `remove-single-property' like
1387 ;; `next-single-property-change' (not `next-single-text-property-change'), etc.
1388 ;;(defun remove-single-text-property (start end prop value &optional object)
1389 ;; "Remove a specific property value from text from START to END.
1390 ;;Arguments PROP and VALUE specify the property and value to remove. The
1391 ;;resulting property values are not equal to VALUE nor lists containing VALUE.
1392 ;;Optional argument OBJECT is the string or buffer containing the text."
1393 ;; (let ((start (text-property-not-all start end prop nil object)) next prev)
1394 ;; (while start
1395 ;; (setq next (next-single-property-change start prop object end)
1396 ;; prev (get-text-property start prop object))
1397 ;; (cond ((and (symbolp prev) (eq value prev))
1398 ;; (remove-text-property start next prop object))
1399 ;; ((and (listp prev) (memq value prev))
1400 ;; (let ((new (delq value prev)))
1401 ;; (cond ((null new)
1402 ;; (remove-text-property start next prop object))
1403 ;; ((= (length new) 1)
1404 ;; (put-text-property start next prop (car new) object))
1405 ;; (t
1406 ;; (put-text-property start next prop new object))))))
1407 ;; (setq start (text-property-not-all next end prop nil object)))))
1409 ;;; End of Additional text property functions.
1411 ;;; Syntactic regexp fontification functions.
1413 ;; These syntactic keyword pass functions are identical to those keyword pass
1414 ;; functions below, with the following exceptions; (a) they operate on
1415 ;; `font-lock-syntactic-keywords' of course, (b) they are all `defun' as speed
1416 ;; is less of an issue, (c) eval of property value does not occur JIT as speed
1417 ;; is less of an issue, (d) OVERRIDE cannot be `prepend' or `append' as it
1418 ;; makes no sense for `syntax-table' property values, (e) they do not do it
1419 ;; LOUDLY as it is not likely to be intensive.
1421 (defun font-lock-apply-syntactic-highlight (highlight)
1422 "Apply HIGHLIGHT following a match.
1423 HIGHLIGHT should be of the form MATCH-HIGHLIGHT,
1424 see `font-lock-syntactic-keywords'."
1425 (let* ((match (nth 0 highlight))
1426 (start (match-beginning match)) (end (match-end match))
1427 (value (nth 1 highlight))
1428 (override (nth 2 highlight)))
1429 (if (not start)
1430 ;; No match but we might not signal an error.
1431 (or (nth 3 highlight)
1432 (error "No match %d in highlight %S" match highlight))
1433 (when (and (consp value) (not (numberp (car value))))
1434 (setq value (eval value)))
1435 (when (stringp value) (setq value (string-to-syntax value)))
1436 ;; Flush the syntax-cache. I believe this is not necessary for
1437 ;; font-lock's use of syntax-ppss, but I'm not 100% sure and it can
1438 ;; still be necessary for other users of syntax-ppss anyway.
1439 (syntax-ppss-after-change-function start)
1440 (cond
1441 ((not override)
1442 ;; Cannot override existing fontification.
1443 (or (text-property-not-all start end 'syntax-table nil)
1444 (put-text-property start end 'syntax-table value)))
1445 ((eq override t)
1446 ;; Override existing fontification.
1447 (put-text-property start end 'syntax-table value))
1448 ((eq override 'keep)
1449 ;; Keep existing fontification.
1450 (font-lock-fillin-text-property start end 'syntax-table value))))))
1452 (defun font-lock-fontify-syntactic-anchored-keywords (keywords limit)
1453 "Fontify according to KEYWORDS until LIMIT.
1454 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1455 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1456 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1457 ;; Evaluate PRE-MATCH-FORM.
1458 (pre-match-value (eval (nth 1 keywords))))
1459 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1460 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1461 (setq limit pre-match-value)
1462 (setq limit (line-end-position)))
1463 (save-match-data
1464 ;; Find an occurrence of `matcher' before `limit'.
1465 (while (if (stringp matcher)
1466 (re-search-forward matcher limit t)
1467 (funcall matcher limit))
1468 ;; Apply each highlight to this instance of `matcher'.
1469 (setq highlights lowdarks)
1470 (while highlights
1471 (font-lock-apply-syntactic-highlight (car highlights))
1472 (setq highlights (cdr highlights)))))
1473 ;; Evaluate POST-MATCH-FORM.
1474 (eval (nth 2 keywords))))
1476 (defun font-lock-fontify-syntactic-keywords-region (start end)
1477 "Fontify according to `font-lock-syntactic-keywords' between START and END.
1478 START should be at the beginning of a line."
1479 (unless parse-sexp-lookup-properties
1480 ;; We wouldn't go through so much trouble if we didn't intend to use those
1481 ;; properties, would we?
1482 (set (make-local-variable 'parse-sexp-lookup-properties) t))
1483 ;; If `font-lock-syntactic-keywords' is a symbol, get the real keywords.
1484 (when (symbolp font-lock-syntactic-keywords)
1485 (setq font-lock-syntactic-keywords (font-lock-eval-keywords
1486 font-lock-syntactic-keywords)))
1487 ;; If `font-lock-syntactic-keywords' is not compiled, compile it.
1488 (unless (eq (car font-lock-syntactic-keywords) t)
1489 (setq font-lock-syntactic-keywords (font-lock-compile-keywords
1490 font-lock-syntactic-keywords
1491 t)))
1492 ;; Get down to business.
1493 (let ((case-fold-search font-lock-keywords-case-fold-search)
1494 (keywords (cddr font-lock-syntactic-keywords))
1495 keyword matcher highlights)
1496 (while keywords
1497 ;; Find an occurrence of `matcher' from `start' to `end'.
1498 (setq keyword (car keywords) matcher (car keyword))
1499 (goto-char start)
1500 (while (and (< (point) end)
1501 (if (stringp matcher)
1502 (re-search-forward matcher end t)
1503 (funcall matcher end)))
1504 ;; Apply each highlight to this instance of `matcher', which may be
1505 ;; specific highlights or more keywords anchored to `matcher'.
1506 (setq highlights (cdr keyword))
1507 (while highlights
1508 (if (numberp (car (car highlights)))
1509 (font-lock-apply-syntactic-highlight (car highlights))
1510 (font-lock-fontify-syntactic-anchored-keywords (car highlights)
1511 end))
1512 (setq highlights (cdr highlights))))
1513 (setq keywords (cdr keywords)))))
1515 ;;; End of Syntactic regexp fontification functions.
1517 ;;; Syntactic fontification functions.
1519 (defvar font-lock-comment-start-skip nil
1520 "If non-nil, Font Lock mode uses this instead of `comment-start-skip'.")
1522 (defvar font-lock-comment-end-skip nil
1523 "If non-nil, Font Lock mode uses this instead of `comment-end'.")
1525 (defun font-lock-fontify-syntactically-region (start end &optional loudly)
1526 "Put proper face on each string and comment between START and END.
1527 START should be at the beginning of a line."
1528 (syntax-propertize end) ; Apply any needed syntax-table properties.
1529 (let ((comment-end-regexp
1530 (or font-lock-comment-end-skip
1531 (regexp-quote
1532 (replace-regexp-in-string "^ *" "" comment-end))))
1533 ;; Find the `start' state.
1534 (state (syntax-ppss start))
1535 face beg)
1536 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
1538 ;; Find each interesting place between here and `end'.
1539 (while
1540 (progn
1541 (when (or (nth 3 state) (nth 4 state))
1542 (setq face (funcall font-lock-syntactic-face-function state))
1543 (setq beg (max (nth 8 state) start))
1544 (setq state (parse-partial-sexp (point) end nil nil state
1545 'syntax-table))
1546 (when face (put-text-property beg (point) 'face face))
1547 (when (and (eq face 'font-lock-comment-face)
1548 (or font-lock-comment-start-skip
1549 comment-start-skip))
1550 ;; Find the comment delimiters
1551 ;; and use font-lock-comment-delimiter-face for them.
1552 (save-excursion
1553 (goto-char beg)
1554 (if (looking-at (or font-lock-comment-start-skip
1555 comment-start-skip))
1556 (put-text-property beg (match-end 0) 'face
1557 font-lock-comment-delimiter-face)))
1558 (if (looking-back comment-end-regexp (point-at-bol) t)
1559 (put-text-property (match-beginning 0) (point) 'face
1560 font-lock-comment-delimiter-face))))
1561 (< (point) end))
1562 (setq state (parse-partial-sexp (point) end nil nil state
1563 'syntax-table)))))
1565 ;;; End of Syntactic fontification functions.
1567 ;;; Keyword regexp fontification functions.
1569 (defsubst font-lock-apply-highlight (highlight)
1570 "Apply HIGHLIGHT following a match.
1571 HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
1572 (let* ((match (nth 0 highlight))
1573 (start (match-beginning match)) (end (match-end match))
1574 (override (nth 2 highlight)))
1575 (if (not start)
1576 ;; No match but we might not signal an error.
1577 (or (nth 3 highlight)
1578 (error "No match %d in highlight %S" match highlight))
1579 (let ((val (eval (nth 1 highlight))))
1580 (when (eq (car-safe val) 'face)
1581 (add-text-properties start end (cddr val))
1582 (setq val (cadr val)))
1583 (cond
1584 ((not (or val (eq override t)))
1585 ;; If `val' is nil, don't do anything. It is important to do it
1586 ;; explicitly, because when adding nil via things like
1587 ;; font-lock-append-text-property, the property is actually
1588 ;; changed from <face> to (<face>) which is undesirable. --Stef
1589 nil)
1590 ((not override)
1591 ;; Cannot override existing fontification.
1592 (or (text-property-not-all start end 'face nil)
1593 (put-text-property start end 'face val)))
1594 ((eq override t)
1595 ;; Override existing fontification.
1596 (put-text-property start end 'face val))
1597 ((eq override 'prepend)
1598 ;; Prepend to existing fontification.
1599 (font-lock-prepend-text-property start end 'face val))
1600 ((eq override 'append)
1601 ;; Append to existing fontification.
1602 (font-lock-append-text-property start end 'face val))
1603 ((eq override 'keep)
1604 ;; Keep existing fontification.
1605 (font-lock-fillin-text-property start end 'face val)))))))
1607 (defsubst font-lock-fontify-anchored-keywords (keywords limit)
1608 "Fontify according to KEYWORDS until LIMIT.
1609 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1610 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1611 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1612 (lead-start (match-beginning 0))
1613 ;; Evaluate PRE-MATCH-FORM.
1614 (pre-match-value (eval (nth 1 keywords))))
1615 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1616 (if (not (and (numberp pre-match-value) (> pre-match-value (point))))
1617 (setq limit (line-end-position))
1618 (setq limit pre-match-value)
1619 (when (and font-lock-multiline (>= limit (line-beginning-position 2)))
1620 ;; this is a multiline anchored match
1621 ;; (setq font-lock-multiline t)
1622 (put-text-property (if (= limit (line-beginning-position 2))
1623 (1- limit)
1624 (min lead-start (point)))
1625 limit
1626 'font-lock-multiline t)))
1627 (save-match-data
1628 ;; Find an occurrence of `matcher' before `limit'.
1629 (while (and (< (point) limit)
1630 (if (stringp matcher)
1631 (re-search-forward matcher limit t)
1632 (funcall matcher limit)))
1633 ;; Apply each highlight to this instance of `matcher'.
1634 (setq highlights lowdarks)
1635 (while highlights
1636 (font-lock-apply-highlight (car highlights))
1637 (setq highlights (cdr highlights)))))
1638 ;; Evaluate POST-MATCH-FORM.
1639 (eval (nth 2 keywords))))
1641 (defun font-lock-fontify-keywords-region (start end &optional loudly)
1642 "Fontify according to `font-lock-keywords' between START and END.
1643 START should be at the beginning of a line.
1644 LOUDLY, if non-nil, allows progress-meter bar."
1645 (unless (eq (car font-lock-keywords) t)
1646 (setq font-lock-keywords
1647 (font-lock-compile-keywords font-lock-keywords)))
1648 (let ((case-fold-search font-lock-keywords-case-fold-search)
1649 (keywords (cddr font-lock-keywords))
1650 (bufname (buffer-name)) (count 0)
1651 (pos (make-marker))
1652 keyword matcher highlights)
1654 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1655 (while keywords
1656 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
1657 (make-string (incf count) ?.)))
1659 ;; Find an occurrence of `matcher' from `start' to `end'.
1660 (setq keyword (car keywords) matcher (car keyword))
1661 (goto-char start)
1662 (while (and (< (point) end)
1663 (if (stringp matcher)
1664 (re-search-forward matcher end t)
1665 (funcall matcher end))
1666 ;; Beware empty string matches since they will
1667 ;; loop indefinitely.
1668 (or (> (point) (match-beginning 0))
1669 (progn (forward-char 1) t)))
1670 (when (and font-lock-multiline
1671 (>= (point)
1672 (save-excursion (goto-char (match-beginning 0))
1673 (forward-line 1) (point))))
1674 ;; this is a multiline regexp match
1675 ;; (setq font-lock-multiline t)
1676 (put-text-property (if (= (point)
1677 (save-excursion
1678 (goto-char (match-beginning 0))
1679 (forward-line 1) (point)))
1680 (1- (point))
1681 (match-beginning 0))
1682 (point)
1683 'font-lock-multiline t))
1684 ;; Apply each highlight to this instance of `matcher', which may be
1685 ;; specific highlights or more keywords anchored to `matcher'.
1686 (setq highlights (cdr keyword))
1687 (while highlights
1688 (if (numberp (car (car highlights)))
1689 (font-lock-apply-highlight (car highlights))
1690 (set-marker pos (point))
1691 (font-lock-fontify-anchored-keywords (car highlights) end)
1692 ;; Ensure forward progress. `pos' is a marker because anchored
1693 ;; keyword may add/delete text (this happens e.g. in grep.el).
1694 (if (< (point) pos) (goto-char pos)))
1695 (setq highlights (cdr highlights))))
1696 (setq keywords (cdr keywords)))
1697 (set-marker pos nil)))
1699 ;;; End of Keyword regexp fontification functions.
1701 ;; Various functions.
1703 (defun font-lock-compile-keywords (keywords &optional syntactic-keywords)
1704 "Compile KEYWORDS into the form (t KEYWORDS COMPILED...)
1705 Here each COMPILED is of the form (MATCHER HIGHLIGHT ...) as shown in the
1706 `font-lock-keywords' doc string.
1707 If SYNTACTIC-KEYWORDS is non-nil, it means these keywords are used for
1708 `font-lock-syntactic-keywords' rather than for `font-lock-keywords'."
1709 (if (not font-lock-set-defaults)
1710 ;; This should never happen. But some external packages sometimes
1711 ;; call font-lock in unexpected and incorrect ways. It's important to
1712 ;; stop processing at this point, otherwise we may end up changing the
1713 ;; global value of font-lock-keywords and break highlighting in many
1714 ;; other buffers.
1715 (error "Font-lock trying to use keywords before setting them up"))
1716 (if (eq (car-safe keywords) t)
1717 keywords
1718 (setq keywords
1719 (cons t (cons keywords
1720 (mapcar 'font-lock-compile-keyword keywords))))
1721 (if (and (not syntactic-keywords)
1722 (let ((beg-function
1723 (or font-lock-beginning-of-syntax-function
1724 syntax-begin-function)))
1725 (or (eq beg-function 'beginning-of-defun)
1726 (get beg-function 'font-lock-syntax-paren-check)))
1727 (not beginning-of-defun-function))
1728 ;; Try to detect when a string or comment contains something that
1729 ;; looks like a defun and would thus confuse font-lock.
1730 (nconc keywords
1731 `((,(if defun-prompt-regexp
1732 (concat "^\\(?:" defun-prompt-regexp "\\)?\\s(")
1733 "^\\s(")
1735 (if (memq (get-text-property (match-beginning 0) 'face)
1736 '(font-lock-string-face font-lock-doc-face
1737 font-lock-comment-face))
1738 (list 'face font-lock-warning-face
1739 'help-echo "Looks like a toplevel defun: escape the parenthesis"))
1740 prepend)))))
1741 keywords))
1743 (defun font-lock-compile-keyword (keyword)
1744 (cond ((nlistp keyword) ; MATCHER
1745 (list keyword '(0 font-lock-keyword-face)))
1746 ((eq (car keyword) 'eval) ; (eval . FORM)
1747 (font-lock-compile-keyword (eval (cdr keyword))))
1748 ((eq (car-safe (cdr keyword)) 'quote) ; (MATCHER . 'FORM)
1749 ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote.
1750 (if (symbolp (nth 2 keyword))
1751 (list (car keyword) (list 0 (cdr keyword)))
1752 (font-lock-compile-keyword (cons (car keyword) (nth 2 keyword)))))
1753 ((numberp (cdr keyword)) ; (MATCHER . MATCH)
1754 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
1755 ((symbolp (cdr keyword)) ; (MATCHER . FACENAME)
1756 (list (car keyword) (list 0 (cdr keyword))))
1757 ((nlistp (nth 1 keyword)) ; (MATCHER . HIGHLIGHT)
1758 (list (car keyword) (cdr keyword)))
1759 (t ; (MATCHER HIGHLIGHT ...)
1760 keyword)))
1762 (defun font-lock-eval-keywords (keywords)
1763 "Evaluate KEYWORDS if a function (funcall) or variable (eval) name."
1764 (if (listp keywords)
1765 keywords
1766 (font-lock-eval-keywords (if (fboundp keywords)
1767 (funcall keywords)
1768 (eval keywords)))))
1770 (defun font-lock-value-in-major-mode (alist)
1771 "Return value in ALIST for `major-mode', or ALIST if it is not an alist.
1772 Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t."
1773 (if (consp alist)
1774 (cdr (or (assq major-mode alist) (assq t alist)))
1775 alist))
1777 (defun font-lock-choose-keywords (keywords level)
1778 "Return LEVELth element of KEYWORDS.
1779 A LEVEL of nil is equal to a LEVEL of 0, a LEVEL of t is equal to
1780 \(1- (length KEYWORDS))."
1781 (cond ((not (and (listp keywords) (symbolp (car keywords))))
1782 keywords)
1783 ((numberp level)
1784 (or (nth level keywords) (car (last keywords))))
1785 ((eq level t)
1786 (car (last keywords)))
1788 (car keywords))))
1790 (defvar font-lock-set-defaults nil) ; Whether we have set up defaults.
1792 (defun font-lock-refresh-defaults ()
1793 "Restart fontification in current buffer after recomputing from defaults.
1794 Recompute fontification variables using `font-lock-defaults' and
1795 `font-lock-maximum-decoration'. Then restart fontification.
1797 Use this function when you have changed any of the above
1798 variables directly.
1800 Note: This function will erase modifications done by
1801 `font-lock-add-keywords' or `font-lock-remove-keywords', but will
1802 preserve `hi-lock-mode' highlighting patterns."
1803 (font-lock-mode -1)
1804 (kill-local-variable 'font-lock-set-defaults)
1805 (font-lock-mode 1))
1807 (defvar font-lock-major-mode nil
1808 "Major mode for which the font-lock settings have been setup.")
1809 (make-variable-buffer-local 'font-lock-major-mode)
1811 (defun font-lock-set-defaults ()
1812 "Set fontification defaults appropriately for this mode.
1813 Sets various variables using `font-lock-defaults' and
1814 `font-lock-maximum-decoration'."
1815 ;; Set fontification defaults if not previously set for correct major mode.
1816 (unless (and font-lock-set-defaults
1817 (eq font-lock-major-mode major-mode))
1818 (setq font-lock-major-mode major-mode)
1819 (set (make-local-variable 'font-lock-set-defaults) t)
1820 (make-local-variable 'font-lock-fontified)
1821 (make-local-variable 'font-lock-multiline)
1822 (let* ((defaults font-lock-defaults)
1823 (keywords
1824 (font-lock-choose-keywords (nth 0 defaults)
1825 (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1826 (local (cdr (assq major-mode font-lock-keywords-alist)))
1827 (removed-keywords
1828 (cdr-safe (assq major-mode font-lock-removed-keywords-alist))))
1829 (set (make-local-variable 'font-lock-defaults) defaults)
1830 ;; Syntactic fontification?
1831 (if (nth 1 defaults)
1832 (set (make-local-variable 'font-lock-keywords-only) t)
1833 (kill-local-variable 'font-lock-keywords-only))
1834 ;; Case fold during regexp fontification?
1835 (if (nth 2 defaults)
1836 (set (make-local-variable 'font-lock-keywords-case-fold-search) t)
1837 (kill-local-variable 'font-lock-keywords-case-fold-search))
1838 ;; Syntax table for regexp and syntactic fontification?
1839 (if (null (nth 3 defaults))
1840 (kill-local-variable 'font-lock-syntax-table)
1841 (set (make-local-variable 'font-lock-syntax-table)
1842 (copy-syntax-table (syntax-table)))
1843 (dolist (selem (nth 3 defaults))
1844 ;; The character to modify may be a single CHAR or a STRING.
1845 (let ((syntax (cdr selem)))
1846 (dolist (char (if (numberp (car selem))
1847 (list (car selem))
1848 (mapcar 'identity (car selem))))
1849 (modify-syntax-entry char syntax font-lock-syntax-table)))))
1850 ;; Syntax function for syntactic fontification?
1851 (if (nth 4 defaults)
1852 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1853 (nth 4 defaults))
1854 (kill-local-variable 'font-lock-beginning-of-syntax-function))
1855 ;; Variable alist?
1856 (dolist (x (nthcdr 5 defaults))
1857 (set (make-local-variable (car x)) (cdr x)))
1858 ;; Set up `font-lock-keywords' last because its value might depend
1859 ;; on other settings (e.g. font-lock-compile-keywords uses
1860 ;; font-lock-beginning-of-syntax-function).
1861 (set (make-local-variable 'font-lock-keywords)
1862 (font-lock-eval-keywords keywords))
1863 ;; Local fontification?
1864 (while local
1865 (font-lock-add-keywords nil (car (car local)) (cdr (car local)))
1866 (setq local (cdr local)))
1867 (when removed-keywords
1868 (font-lock-remove-keywords nil removed-keywords))
1869 ;; Now compile the keywords.
1870 (unless (eq (car font-lock-keywords) t)
1871 (setq font-lock-keywords
1872 (font-lock-compile-keywords font-lock-keywords))))))
1874 ;;; Color etc. support.
1876 ;; Note that `defface' will not overwrite any faces declared above via
1877 ;; `custom-declare-face'.
1878 (defface font-lock-comment-face
1879 '((((class grayscale) (background light))
1880 (:foreground "DimGray" :weight bold :slant italic))
1881 (((class grayscale) (background dark))
1882 (:foreground "LightGray" :weight bold :slant italic))
1883 (((class color) (min-colors 88) (background light))
1884 (:foreground "Firebrick"))
1885 (((class color) (min-colors 88) (background dark))
1886 (:foreground "chocolate1"))
1887 (((class color) (min-colors 16) (background light))
1888 (:foreground "red"))
1889 (((class color) (min-colors 16) (background dark))
1890 (:foreground "red1"))
1891 (((class color) (min-colors 8) (background light))
1892 (:foreground "red"))
1893 (((class color) (min-colors 8) (background dark))
1894 (:foreground "yellow"))
1895 (t (:weight bold :slant italic)))
1896 "Font Lock mode face used to highlight comments."
1897 :group 'font-lock-faces)
1899 (defface font-lock-comment-delimiter-face
1900 '((default :inherit font-lock-comment-face))
1901 "Font Lock mode face used to highlight comment delimiters."
1902 :group 'font-lock-faces)
1904 (defface font-lock-string-face
1905 '((((class grayscale) (background light)) (:foreground "DimGray" :slant italic))
1906 (((class grayscale) (background dark)) (:foreground "LightGray" :slant italic))
1907 (((class color) (min-colors 88) (background light)) (:foreground "VioletRed4"))
1908 (((class color) (min-colors 88) (background dark)) (:foreground "LightSalmon"))
1909 (((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
1910 (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
1911 (((class color) (min-colors 8)) (:foreground "green"))
1912 (t (:slant italic)))
1913 "Font Lock mode face used to highlight strings."
1914 :group 'font-lock-faces)
1916 (defface font-lock-doc-face
1917 '((t :inherit font-lock-string-face))
1918 "Font Lock mode face used to highlight documentation."
1919 :group 'font-lock-faces)
1921 (defface font-lock-keyword-face
1922 '((((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
1923 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
1924 (((class color) (min-colors 88) (background light)) (:foreground "Purple"))
1925 (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
1926 (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
1927 (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
1928 (((class color) (min-colors 8)) (:foreground "cyan" :weight bold))
1929 (t (:weight bold)))
1930 "Font Lock mode face used to highlight keywords."
1931 :group 'font-lock-faces)
1933 (defface font-lock-builtin-face
1934 '((((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
1935 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
1936 (((class color) (min-colors 88) (background light)) (:foreground "dark slate blue"))
1937 (((class color) (min-colors 88) (background dark)) (:foreground "LightSteelBlue"))
1938 (((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
1939 (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
1940 (((class color) (min-colors 8)) (:foreground "blue" :weight bold))
1941 (t (:weight bold)))
1942 "Font Lock mode face used to highlight builtins."
1943 :group 'font-lock-faces)
1945 (defface font-lock-function-name-face
1946 '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
1947 (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
1948 (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
1949 (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
1950 (((class color) (min-colors 8)) (:foreground "blue" :weight bold))
1951 (t (:inverse-video t :weight bold)))
1952 "Font Lock mode face used to highlight function names."
1953 :group 'font-lock-faces)
1955 (defface font-lock-variable-name-face
1956 '((((class grayscale) (background light))
1957 (:foreground "Gray90" :weight bold :slant italic))
1958 (((class grayscale) (background dark))
1959 (:foreground "DimGray" :weight bold :slant italic))
1960 (((class color) (min-colors 88) (background light)) (:foreground "sienna"))
1961 (((class color) (min-colors 88) (background dark)) (:foreground "LightGoldenrod"))
1962 (((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
1963 (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
1964 (((class color) (min-colors 8)) (:foreground "yellow" :weight light))
1965 (t (:weight bold :slant italic)))
1966 "Font Lock mode face used to highlight variable names."
1967 :group 'font-lock-faces)
1969 (defface font-lock-type-face
1970 '((((class grayscale) (background light)) (:foreground "Gray90" :weight bold))
1971 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
1972 (((class color) (min-colors 88) (background light)) (:foreground "ForestGreen"))
1973 (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
1974 (((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
1975 (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
1976 (((class color) (min-colors 8)) (:foreground "green"))
1977 (t (:weight bold :underline t)))
1978 "Font Lock mode face used to highlight type and classes."
1979 :group 'font-lock-faces)
1981 (defface font-lock-constant-face
1982 '((((class grayscale) (background light))
1983 (:foreground "LightGray" :weight bold :underline t))
1984 (((class grayscale) (background dark))
1985 (:foreground "Gray50" :weight bold :underline t))
1986 (((class color) (min-colors 88) (background light)) (:foreground "dark cyan"))
1987 (((class color) (min-colors 88) (background dark)) (:foreground "Aquamarine"))
1988 (((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
1989 (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
1990 (((class color) (min-colors 8)) (:foreground "magenta"))
1991 (t (:weight bold :underline t)))
1992 "Font Lock mode face used to highlight constants and labels."
1993 :group 'font-lock-faces)
1995 (defface font-lock-warning-face
1996 '((t :inherit error))
1997 "Font Lock mode face used to highlight warnings."
1998 :group 'font-lock-faces)
2000 (defface font-lock-negation-char-face
2001 '((t nil))
2002 "Font Lock mode face used to highlight easy to overlook negation."
2003 :group 'font-lock-faces)
2005 (defface font-lock-preprocessor-face
2006 '((t :inherit font-lock-builtin-face))
2007 "Font Lock mode face used to highlight preprocessor directives."
2008 :group 'font-lock-faces)
2010 (defface font-lock-regexp-grouping-backslash
2011 '((t :inherit bold))
2012 "Font Lock mode face for backslashes in Lisp regexp grouping constructs."
2013 :group 'font-lock-faces)
2015 (defface font-lock-regexp-grouping-construct
2016 '((t :inherit bold))
2017 "Font Lock mode face used to highlight grouping constructs in Lisp regexps."
2018 :group 'font-lock-faces)
2020 ;;; End of Color etc. support.
2022 ;;; Menu support.
2024 ;; This section of code is commented out because Emacs does not have real menu
2025 ;; buttons. (We can mimic them by putting "( ) " or "(X) " at the beginning of
2026 ;; the menu entry text, but with Xt it looks both ugly and embarrassingly
2027 ;; amateur.) If/When Emacs gets real menus buttons, put in menu-bar.el after
2028 ;; the entry for "Text Properties" something like:
2030 ;; (define-key menu-bar-edit-menu [font-lock]
2031 ;; (cons "Syntax Highlighting" font-lock-menu))
2033 ;; and remove a single ";" from the beginning of each line in the rest of this
2034 ;; section. Probably the mechanism for telling the menu code what are menu
2035 ;; buttons and when they are on or off needs tweaking. I have assumed that the
2036 ;; mechanism is via `menu-toggle' and `menu-selected' symbol properties. sm.
2038 ;;;;;###autoload
2039 ;;(progn
2040 ;; ;; Make the Font Lock menu.
2041 ;; (defvar font-lock-menu (make-sparse-keymap "Syntax Highlighting"))
2042 ;; ;; Add the menu items in reverse order.
2043 ;; (define-key font-lock-menu [fontify-less]
2044 ;; '("Less In Current Buffer" . font-lock-fontify-less))
2045 ;; (define-key font-lock-menu [fontify-more]
2046 ;; '("More In Current Buffer" . font-lock-fontify-more))
2047 ;; (define-key font-lock-menu [font-lock-sep]
2048 ;; '("--"))
2049 ;; (define-key font-lock-menu [font-lock-mode]
2050 ;; '("In Current Buffer" . font-lock-mode))
2051 ;; (define-key font-lock-menu [global-font-lock-mode]
2052 ;; '("In All Buffers" . global-font-lock-mode)))
2054 ;;;;;###autoload
2055 ;;(progn
2056 ;; ;; We put the appropriate `menu-enable' etc. symbol property values on when
2057 ;; ;; font-lock.el is loaded, so we don't need to autoload the three variables.
2058 ;; (put 'global-font-lock-mode 'menu-toggle t)
2059 ;; (put 'font-lock-mode 'menu-toggle t)
2060 ;; (put 'font-lock-fontify-more 'menu-enable '(identity))
2061 ;; (put 'font-lock-fontify-less 'menu-enable '(identity)))
2063 ;; ;; Put the appropriate symbol property values on now. See above.
2064 ;;(put 'global-font-lock-mode 'menu-selected 'global-font-lock-mode)
2065 ;;(put 'font-lock-mode 'menu-selected 'font-lock-mode)
2066 ;;(put 'font-lock-fontify-more 'menu-enable '(nth 2 font-lock-fontify-level))
2067 ;;(put 'font-lock-fontify-less 'menu-enable '(nth 1 font-lock-fontify-level))
2069 ;;(defvar font-lock-fontify-level nil) ; For less/more fontification.
2071 ;;(defun font-lock-fontify-level (level)
2072 ;; (let ((font-lock-maximum-decoration level))
2073 ;; (when font-lock-mode
2074 ;; (font-lock-mode))
2075 ;; (font-lock-mode)
2076 ;; (when font-lock-verbose
2077 ;; (message "Fontifying %s... level %d" (buffer-name) level))))
2079 ;;(defun font-lock-fontify-less ()
2080 ;; "Fontify the current buffer with less decoration.
2081 ;;See `font-lock-maximum-decoration'."
2082 ;; (interactive)
2083 ;; ;; Check in case we get called interactively.
2084 ;; (if (nth 1 font-lock-fontify-level)
2085 ;; (font-lock-fontify-level (1- (car font-lock-fontify-level)))
2086 ;; (error "No less decoration")))
2088 ;;(defun font-lock-fontify-more ()
2089 ;; "Fontify the current buffer with more decoration.
2090 ;;See `font-lock-maximum-decoration'."
2091 ;; (interactive)
2092 ;; ;; Check in case we get called interactively.
2093 ;; (if (nth 2 font-lock-fontify-level)
2094 ;; (font-lock-fontify-level (1+ (car font-lock-fontify-level)))
2095 ;; (error "No more decoration")))
2097 ;; ;; This should be called by `font-lock-set-defaults'.
2098 ;;(defun font-lock-set-menu ()
2099 ;; ;; Activate less/more fontification entries if there are multiple levels for
2100 ;; ;; the current buffer. Sets `font-lock-fontify-level' to be of the form
2101 ;; ;; (CURRENT-LEVEL IS-LOWER-LEVEL-P IS-HIGHER-LEVEL-P) for menu activation.
2102 ;; (let ((keywords (nth 0 font-lock-defaults))
2103 ;; (level (font-lock-value-in-major-mode font-lock-maximum-decoration)))
2104 ;; (make-local-variable 'font-lock-fontify-level)
2105 ;; (if (or (symbolp keywords) (= (length keywords) 1))
2106 ;; (font-lock-unset-menu)
2107 ;; (cond ((eq level t)
2108 ;; (setq level (1- (length keywords))))
2109 ;; ((or (null level) (zerop level))
2110 ;; ;; The default level is usually, but not necessarily, level 1.
2111 ;; (setq level (- (length keywords)
2112 ;; (length (member (eval (car keywords))
2113 ;; (mapcar 'eval (cdr keywords))))))))
2114 ;; (setq font-lock-fontify-level (list level (> level 1)
2115 ;; (< level (1- (length keywords))))))))
2117 ;; ;; This should be called by `font-lock-unset-defaults'.
2118 ;;(defun font-lock-unset-menu ()
2119 ;; ;; Deactivate less/more fontification entries.
2120 ;; (setq font-lock-fontify-level nil))
2122 ;;; End of Menu support.
2124 ;;; Various regexp information shared by several modes.
2125 ;; ;; Information specific to a single mode should go in its load library.
2127 ;; Font Lock support for C, C++, Objective-C and Java modes is now in
2128 ;; cc-fonts.el (and required by cc-mode.el). However, the below function
2129 ;; should stay in font-lock.el, since it is used by other libraries. sm.
2131 (defun font-lock-match-c-style-declaration-item-and-skip-to-next (limit)
2132 "Match, and move over, any declaration/definition item after point.
2133 Matches after point, but ignores leading whitespace and `*' characters.
2134 Does not move further than LIMIT.
2136 The expected syntax of a declaration/definition item is `word' (preceded by
2137 optional whitespace and `*' characters and proceeded by optional whitespace)
2138 optionally followed by a `('. Everything following the item (but belonging to
2139 it) is expected to be skip-able by `scan-sexps', and items are expected to be
2140 separated with a `,' and to be terminated with a `;'.
2142 Thus the regexp matches after point: word (
2143 ^^^^ ^
2144 Where the match subexpressions are: 1 2
2146 The item is delimited by (match-beginning 1) and (match-end 1).
2147 If (match-beginning 2) is non-nil, the item is followed by a `('.
2149 This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
2150 (when (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?")
2151 (when (and (match-end 2) (> (- (match-end 2) (match-beginning 2)) 1))
2152 ;; If `word' is followed by a double open-paren, it's probably
2153 ;; a macro used for "int myfun P_ ((int arg1))". Let's go back one
2154 ;; word to try and match `myfun' rather than `P_'.
2155 (let ((pos (point)))
2156 (skip-chars-backward " \t\n")
2157 (skip-syntax-backward "w")
2158 (unless (looking-at "\\(\\sw+\\)[ \t\n]*\\sw+[ \t\n]*\\(((?\\)?")
2159 ;; Looks like it was something else, so go back to where we
2160 ;; were and reset the match data by rematching.
2161 (goto-char pos)
2162 (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?"))))
2163 (save-match-data
2164 (condition-case nil
2165 (save-restriction
2166 ;; Restrict to the LIMIT.
2167 (narrow-to-region (point-min) limit)
2168 (goto-char (match-end 1))
2169 ;; Move over any item value, etc., to the next item.
2170 (while (not (looking-at "[ \t\n]*\\(\\(,\\)\\|;\\|\\'\\)"))
2171 (goto-char (or (scan-sexps (point) 1) (point-max))))
2172 (if (match-end 2)
2173 (goto-char (match-end 2))))
2174 (error t)))))
2176 ;; C preprocessor(cpp) is used outside of C, C++ and Objective-C source file.
2177 ;; e.g. assembler code and GNU linker script in Linux kernel.
2178 ;; `cpp-font-lock-keywords' is handy for modes for the files.
2180 ;; Here we cannot use `regexp-opt' because because regex-opt is not preloaded
2181 ;; while font-lock.el is preloaded to emacs. So values pre-calculated with
2182 ;; regexp-opt are used here.
2184 ;; `cpp-font-lock-keywords-source-directives' is calculated from:
2186 ;; (regexp-opt
2187 ;; '("define" "elif" "else" "endif" "error" "file" "if" "ifdef"
2188 ;; "ifndef" "import" "include" "line" "pragma" "undef" "warning"))
2190 (defconst cpp-font-lock-keywords-source-directives
2191 "define\\|e\\(?:l\\(?:if\\|se\\)\\|ndif\\|rror\\)\\|file\\|i\\(?:f\\(?:n?def\\)?\\|mport\\|nclude\\)\\|line\\|pragma\\|undef\\|warning"
2192 "Regular expression used in `cpp-font-lock-keywords'.")
2194 ;; `cpp-font-lock-keywords-source-depth' is calculated from:
2196 ;; (regexp-opt-depth (regexp-opt
2197 ;; '("define" "elif" "else" "endif" "error" "file" "if" "ifdef"
2198 ;; "ifndef" "import" "include" "line" "pragma" "undef" "warning")))
2200 (defconst cpp-font-lock-keywords-source-depth 0
2201 "An integer representing regular expression depth of `cpp-font-lock-keywords-source-directives'.
2202 Used in `cpp-font-lock-keywords'.")
2204 (defconst cpp-font-lock-keywords
2205 (let* ((directives cpp-font-lock-keywords-source-directives)
2206 (directives-depth cpp-font-lock-keywords-source-depth))
2207 (list
2209 ;; Fontify error directives.
2210 '("^#[ \t]*\\(?:error\\|warning\\)[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend)
2212 ;; Fontify filenames in #include <...> preprocessor directives as strings.
2213 '("^#[ \t]*\\(?:import\\|include\\)[ \t]*\\(<[^>\"\n]*>?\\)"
2214 1 font-lock-string-face prepend)
2216 ;; Fontify function macro names.
2217 '("^#[ \t]*define[ \t]+\\([[:alpha:]_][[:alnum:]_$]*\\)("
2218 (1 font-lock-function-name-face prepend)
2220 ;; Macro arguments.
2221 ((lambda (limit)
2222 (re-search-forward
2223 "\\(?:\\([[:alpha:]_][[:alnum:]_]*\\)[,]?\\)"
2224 (or (save-excursion (re-search-forward ")" limit t))
2225 limit)
2227 nil nil (1 font-lock-variable-name-face prepend)))
2229 ;; Fontify symbol names in #elif or #if ... defined preprocessor directives.
2230 '("^#[ \t]*\\(?:elif\\|if\\)\\>"
2231 ("\\<\\(defined\\)\\>[ \t]*(?\\([[:alpha:]_][[:alnum:]_]*\\)?" nil nil
2232 (1 font-lock-builtin-face prepend) (2 font-lock-variable-name-face prepend t)))
2234 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
2235 (list
2236 (concat "^\\(#[ \t]*\\(?:" directives
2237 "\\)\\)\\>[ \t!]*\\([[:alpha:]_][[:alnum:]_]*\\)?")
2238 '(1 font-lock-preprocessor-face prepend)
2239 (list (+ 2 directives-depth)
2240 'font-lock-variable-name-face nil t))))
2241 "Font lock keywords for C preprocessor directives.
2242 `c-mode', `c++-mode' and `objc-mode' have their own font lock keywords
2243 for C preprocessor directives. This definition is for the other modes
2244 in which C preprocessor directives are used. e.g. `asm-mode' and
2245 `ld-script-mode'.")
2248 ;; Lisp.
2250 (defconst lisp-font-lock-keywords-1
2251 (eval-when-compile
2252 `(;; Definitions.
2253 (,(concat "(\\(def\\("
2254 ;; Function declarations.
2255 "\\(advice\\|alias\\|generic\\|macro\\*?\\|method\\|"
2256 "setf\\|subst\\*?\\|un\\*?\\|"
2257 "ine-\\(condition\\|"
2258 "\\(?:derived\\|\\(?:global\\(?:ized\\)?-\\)?minor\\|generic\\)-mode\\|"
2259 "method-combination\\|setf-expander\\|skeleton\\|widget\\|"
2260 "function\\|\\(compiler\\|modify\\|symbol\\)-macro\\)\\)\\|"
2261 ;; Variable declarations.
2262 "\\(const\\(ant\\)?\\|custom\\|varalias\\|face\\|parameter\\|var\\)\\|"
2263 ;; Structure declarations.
2264 "\\(class\\|group\\|theme\\|package\\|struct\\|type\\)"
2265 "\\)\\)\\>"
2266 ;; Any whitespace and defined object.
2267 "[ \t'\(]*"
2268 "\\(setf[ \t]+\\sw+\\|\\sw+\\)?")
2269 (1 font-lock-keyword-face)
2270 (9 (cond ((match-beginning 3) font-lock-function-name-face)
2271 ((match-beginning 6) font-lock-variable-name-face)
2272 (t font-lock-type-face))
2273 nil t))
2274 ;; Emacs Lisp autoload cookies. Supports the slightly different
2275 ;; forms used by mh-e, calendar, etc.
2276 ("^;;;###\\([-a-z]*autoload\\)" 1 font-lock-warning-face prepend)
2277 ;; Regexp negated char group.
2278 ("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)))
2279 "Subdued level highlighting for Lisp modes.")
2281 (defconst lisp-font-lock-keywords-2
2282 (append lisp-font-lock-keywords-1
2283 (eval-when-compile
2284 `(;; Control structures. Emacs Lisp forms.
2285 (,(concat
2286 "(" (regexp-opt
2287 '("cond" "if" "while" "while-no-input" "let" "let*" "letrec"
2288 "prog" "progn" "progv" "prog1" "prog2" "prog*"
2289 "inline" "lambda" "save-restriction" "save-excursion"
2290 "save-selected-window" "save-window-excursion"
2291 "save-match-data" "save-current-buffer"
2292 "combine-after-change-calls" "unwind-protect"
2293 "condition-case" "condition-case-unless-debug"
2294 "track-mouse" "eval-after-load" "eval-and-compile"
2295 "eval-when-compile" "eval-when" "eval-next-after-load"
2296 "with-case-table" "with-category-table"
2297 "with-current-buffer" "with-demoted-errors"
2298 "with-electric-help"
2299 "with-local-quit" "with-no-warnings"
2300 "with-output-to-string" "with-output-to-temp-buffer"
2301 "with-selected-window" "with-selected-frame"
2302 "with-silent-modifications" "with-syntax-table"
2303 "with-temp-buffer" "with-temp-file" "with-temp-message"
2304 "with-timeout" "with-timeout-handler" "with-wrapper-hook") t)
2305 "\\>")
2306 . 1)
2307 ;; Control structures. Common Lisp forms.
2308 (,(concat
2309 "(" (regexp-opt
2310 '("when" "unless" "case" "ecase" "typecase" "etypecase"
2311 "ccase" "ctypecase" "handler-case" "handler-bind"
2312 "restart-bind" "restart-case" "in-package"
2313 "break" "ignore-errors"
2314 "loop" "do" "do*" "dotimes" "dolist" "the" "locally"
2315 "proclaim" "declaim" "declare" "symbol-macrolet" "letf"
2316 "lexical-let" "lexical-let*" "flet" "labels" "compiler-let"
2317 "destructuring-bind" "macrolet" "tagbody" "block" "go"
2318 "multiple-value-bind" "multiple-value-prog1"
2319 "return" "return-from"
2320 "with-accessors" "with-compilation-unit"
2321 "with-condition-restarts" "with-hash-table-iterator"
2322 "with-input-from-string" "with-open-file"
2323 "with-open-stream" "with-output-to-string"
2324 "with-package-iterator" "with-simple-restart"
2325 "with-slots" "with-standard-io-syntax") t)
2326 "\\>")
2327 . 1)
2328 ;; Exit/Feature symbols as constants.
2329 (,(concat "(\\(catch\\|throw\\|featurep\\|provide\\|require\\)\\>"
2330 "[ \t']*\\(\\sw+\\)?")
2331 (1 font-lock-keyword-face)
2332 (2 font-lock-constant-face nil t))
2333 ;; Erroneous structures.
2334 ("(\\(abort\\|assert\\|warn\\|check-type\\|cerror\\|error\\|signal\\)\\>" 1 font-lock-warning-face)
2335 ;; Words inside \\[] tend to be for `substitute-command-keys'.
2336 ("\\\\\\\\\\[\\(\\sw+\\)\\]" 1 font-lock-constant-face prepend)
2337 ;; Words inside `' tend to be symbol names.
2338 ("`\\(\\sw\\sw+\\)'" 1 font-lock-constant-face prepend)
2339 ;; Constant values.
2340 ("\\<:\\sw+\\>" 0 font-lock-builtin-face)
2341 ;; ELisp and CLisp `&' keywords as types.
2342 ("\\<\\&\\sw+\\>" . font-lock-type-face)
2343 ;; ELisp regexp grouping constructs
2344 ((lambda (bound)
2345 (catch 'found
2346 ;; The following loop is needed to continue searching after matches
2347 ;; that do not occur in strings. The associated regexp matches one
2348 ;; of `\\\\' `\\(' `\\(?:' `\\|' `\\)'. `\\\\' has been included to
2349 ;; avoid highlighting, for example, `\\(' in `\\\\('.
2350 (while (re-search-forward "\\(\\\\\\\\\\)\\(?:\\(\\\\\\\\\\)\\|\\((\\(?:\\?[0-9]*:\\)?\\|[|)]\\)\\)" bound t)
2351 (unless (match-beginning 2)
2352 (let ((face (get-text-property (1- (point)) 'face)))
2353 (when (or (and (listp face)
2354 (memq 'font-lock-string-face face))
2355 (eq 'font-lock-string-face face))
2356 (throw 'found t)))))))
2357 (1 'font-lock-regexp-grouping-backslash prepend)
2358 (3 'font-lock-regexp-grouping-construct prepend))
2359 ;;; This is too general -- rms.
2360 ;;; A user complained that he has functions whose names start with `do'
2361 ;;; and that they get the wrong color.
2362 ;;; ;; CL `with-' and `do-' constructs
2363 ;;; ("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
2365 "Gaudy level highlighting for Lisp modes.")
2367 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
2368 "Default expressions to highlight in Lisp modes.")
2370 (provide 'font-lock)
2372 ;;; font-lock.el ends here