Print test timings unconditionally
[emacs.git] / lisp / font-lock.el
blob0ed94bd0e8bc7e7635d0adb5f45ed477f83a3b0f
1 ;;; font-lock.el --- Electric font lock mode
3 ;; Copyright (C) 1992-2018 Free Software Foundation, Inc.
5 ;; Author: Jamie Zawinski
6 ;; Richard Stallman
7 ;; Stefan Monnier
8 ;; Maintainer: emacs-devel@gnu.org
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 <https://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 init 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-lib))
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 (define-obsolete-variable-alias
344 '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
368 function name to call to make the search (called with one
369 argument, the limit of the search; it should return non-nil, move
370 point, and set `match-data' appropriately if it succeeds; like
371 `re-search-forward' would). MATCHER regexps can be generated via
372 the function `regexp-opt'.
374 FORM is an expression, whose value should be a keyword element,
375 evaluated when the keyword is (first) used in a buffer. This
376 feature can be used to provide a keyword that can only be
377 generated when Font Lock mode is actually turned on.
379 HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
381 For highlighting single items, for example each instance of the
382 word \"foo\", typically only MATCH-HIGHLIGHT is required.
383 However, if an item or (typically) items are to be highlighted
384 following the instance of another item (the anchor), for example
385 each instance of the word \"bar\" following the word \"anchor\"
386 then MATCH-ANCHORED may be required.
388 MATCH-HIGHLIGHT should be of the form:
390 (SUBEXP FACENAME [OVERRIDE [LAXMATCH]])
392 SUBEXP is the number of the subexpression of MATCHER to be
393 highlighted.
395 FACENAME is an expression whose value is the face name to use.
396 Instead of a face, FACENAME can evaluate to a property list of
397 the form (face FACE PROP1 VAL1 PROP2 VAL2 ...) in which case all
398 the listed text-properties will be set rather than just FACE. In
399 such a case, you will most likely want to put those properties in
400 `font-lock-extra-managed-props' or to override
401 `font-lock-unfontify-region-function'.
403 OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing
404 fontification can be overwritten. If `keep', only parts not
405 already fontified are highlighted. If `prepend' or `append',
406 existing fontification is merged with the new, in which the new
407 or existing fontification, respectively, takes precedence. If
408 LAXMATCH is non-nil, that means don't signal an error if there is
409 no match for SUBEXP in MATCHER.
411 For example, an element of the form highlights (if not already
412 highlighted):
414 \"\\\\\\=<foo\\\\\\=>\"
415 Discrete occurrences of \"foo\" in the value of the variable
416 `font-lock-keyword-face'.
418 (\"fu\\\\(bar\\\\)\" . 1)
419 Substring \"bar\" within all occurrences of \"fubar\" in the
420 value of `font-lock-keyword-face'.
422 (\"fubar\" . fubar-face)
423 Occurrences of \"fubar\" in the value of `fubar-face'.
425 (\"foo\\\\|bar\" 0 foo-bar-face t)
426 Occurrences of either \"foo\" or \"bar\" in the value of
427 `foo-bar-face', even if already highlighted.
429 (fubar-match 1 fubar-face)
430 The first subexpression within all occurrences of whatever the
431 function `fubar-match' finds and matches in the value of
432 `fubar-face'.
434 MATCH-ANCHORED should be of the form:
436 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
438 where MATCHER is a regexp to search for or the function name to
439 call to make the search, as for MATCH-HIGHLIGHT above, but with
440 one exception; see below. PRE-MATCH-FORM and POST-MATCH-FORM are
441 evaluated before the first, and after the last, instance
442 MATCH-ANCHORED's MATCHER is used. Therefore they can be used to
443 initialize before, and cleanup after, MATCHER is used.
444 Typically, PRE-MATCH-FORM is used to move to some position
445 relative to the original MATCHER, before starting with
446 MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might be used to move
447 back, before resuming with MATCH-ANCHORED's parent's MATCHER.
449 For example, an element of the form highlights (if not already
450 highlighted):
452 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face)
453 (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
455 Discrete occurrences of \"anchor\" in the value of
456 `anchor-face', and subsequent discrete occurrences of
457 \"item\" (on the same line) in the value of `item-face'.
458 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore
459 \"item\" is initially searched for starting from the end of the
460 match of \"anchor\", and searching for subsequent instances of
461 \"anchor\" resumes from where searching for \"item\" concluded.)
463 The above-mentioned exception is as follows. The limit of the
464 MATCHER search defaults to the end of the line after
465 PRE-MATCH-FORM is evaluated. However, if PRE-MATCH-FORM returns
466 a position greater than the position after PRE-MATCH-FORM is
467 evaluated, that position is used as the limit of the search. It
468 is generally a bad idea to return a position greater than the end
469 of the line, i.e., cause the MATCHER search to span lines.
471 These regular expressions can match text which spans lines,
472 although it is better to avoid it if possible since updating them
473 while editing text is slower, and it is not guaranteed to be
474 always correct when using support modes like jit-lock or
475 lazy-lock.
477 This variable is set by major modes via the variable
478 `font-lock-defaults'. Be careful when composing regexps for this
479 list; a poorly written pattern can dramatically slow things down!
481 A compiled keywords list starts with t. It is produced
482 internally by `font-lock-compile-keywords' from a user-level
483 keywords list. Its second element is the user-level keywords
484 list that was compiled. The remaining elements have the same
485 form as user-level keywords, but normally their values have been
486 optimized.")
488 (defvar font-lock-keywords-alist nil
489 "Alist of additional `font-lock-keywords' elements for major modes.
491 Each element has the form (MODE KEYWORDS . HOW).
492 Function `font-lock-set-defaults' adds the elements in the list KEYWORDS to
493 `font-lock-keywords' when Font Lock is turned on in major mode MODE.
495 If HOW is nil, KEYWORDS are added at the beginning of
496 `font-lock-keywords'. If it is `set', they are used to replace the
497 value of `font-lock-keywords'. If HOW is any other non-nil value,
498 they are added at the end.
500 This is normally set via `font-lock-add-keywords' and
501 `font-lock-remove-keywords'.")
502 (put 'font-lock-keywords-alist 'risky-local-variable t)
504 (defvar font-lock-removed-keywords-alist nil
505 "Alist of `font-lock-keywords' elements to be removed for major modes.
507 Each element has the form (MODE . KEYWORDS). Function `font-lock-set-defaults'
508 removes the elements in the list KEYWORDS from `font-lock-keywords'
509 when Font Lock is turned on in major mode MODE.
511 This is normally set via `font-lock-add-keywords' and
512 `font-lock-remove-keywords'.")
514 (defvar font-lock-keywords-only nil
515 "Non-nil means Font Lock should not fontify comments or strings.
516 This is normally set via `font-lock-defaults'.")
518 (defvar font-lock-keywords-case-fold-search nil
519 "Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
520 This is set via the function `font-lock-set-defaults', based on
521 the CASE-FOLD argument of `font-lock-defaults'.")
522 (make-variable-buffer-local 'font-lock-keywords-case-fold-search)
524 (defvar font-lock-syntactically-fontified 0
525 "Point up to which `font-lock-syntactic-keywords' has been applied.
526 If nil, this is ignored, in which case the syntactic fontification may
527 sometimes be slightly incorrect.")
528 (make-variable-buffer-local 'font-lock-syntactically-fontified)
530 (defvar font-lock-syntactic-face-function
531 (lambda (state)
532 (if (nth 3 state) font-lock-string-face font-lock-comment-face))
533 "Function to determine which face to use when fontifying syntactically.
534 The function is called with a single parameter (the state as returned by
535 `parse-partial-sexp' at the beginning of the region to highlight) and
536 should return a face. This is normally set via `font-lock-defaults'.")
538 (defvar font-lock-syntactic-keywords nil
539 "A list of the syntactic keywords to put syntax properties on.
540 The value can be the list itself, or the name of a function or variable
541 whose value is the list.
543 See `font-lock-keywords' for a description of the form of this list;
544 only the differences are stated here. MATCH-HIGHLIGHT should be of the form:
546 (SUBEXP SYNTAX OVERRIDE LAXMATCH)
548 where SYNTAX can be a string (as taken by `modify-syntax-entry'), a syntax
549 table, a cons cell (as returned by `string-to-syntax') or an expression whose
550 value is such a form. OVERRIDE cannot be `prepend' or `append'.
552 Here are two examples of elements of `font-lock-syntactic-keywords'
553 and what they do:
555 (\"\\\\$\\\\(#\\\\)\" 1 \".\")
557 gives a hash character punctuation syntax (\".\") when following a
558 dollar-sign character. Hash characters in other contexts will still
559 follow whatever the syntax table says about the hash character.
561 (\"\\\\(\\='\\\\).\\\\(\\='\\\\)\"
562 (1 \"\\\"\")
563 (2 \"\\\"\"))
565 gives a pair of apostrophes, which surround a single character, a
566 SYNTAX of \"\\\"\" (meaning string quote syntax). Apostrophes in other
568 contexts will not be affected.
570 This is normally set via `font-lock-defaults'.")
571 (make-obsolete-variable 'font-lock-syntactic-keywords
572 'syntax-propertize-function "24.1")
574 (defvar font-lock-syntax-table nil
575 "Non-nil means use this syntax table for fontifying.
576 If this is nil, the major mode's syntax table is used.
577 This is normally set via `font-lock-defaults'.")
579 (defvar font-lock-mark-block-function nil
580 "Non-nil means use this function to mark a block of text.
581 When called with no args it should leave point at the beginning of any
582 enclosing textual block and mark at the end.
583 This is normally set via `font-lock-defaults'.")
585 (defvar font-lock-fontify-buffer-function #'font-lock-default-fontify-buffer
586 "Function to use for fontifying the buffer.
587 This is normally set via `font-lock-defaults'.")
589 (defvar font-lock-unfontify-buffer-function #'font-lock-default-unfontify-buffer
590 "Function to use for unfontifying the buffer.
591 This is used when turning off Font Lock mode.
592 This is normally set via `font-lock-defaults'.")
594 (defvar font-lock-fontify-region-function #'font-lock-default-fontify-region
595 "Function to use for fontifying a region.
596 It should take two args, the beginning and end of the region, and an optional
597 third arg VERBOSE. If VERBOSE is non-nil, the function should print status
598 messages. This is normally set via `font-lock-defaults'.
599 If it fontifies a larger region, it should ideally return a list of the form
600 \(jit-lock-bounds BEG . END) indicating the bounds of the region actually
601 fontified.")
603 (defvar font-lock-unfontify-region-function #'font-lock-default-unfontify-region
604 "Function to use for unfontifying a region.
605 It should take two args, the beginning and end of the region.
606 This is normally set via `font-lock-defaults'.")
608 (defvar font-lock-inhibit-thing-lock nil
609 "List of Font Lock mode related modes that should not be turned on.
610 Currently, valid mode names are `fast-lock-mode', `jit-lock-mode' and
611 `lazy-lock-mode'. This is normally set via `font-lock-defaults'.")
612 (make-obsolete-variable 'font-lock-inhibit-thing-lock nil "25.1")
614 (defvar-local font-lock-multiline nil
615 "Whether font-lock should cater to multiline keywords.
616 If nil, don't try to handle multiline patterns.
617 If t, always handle multiline patterns.
618 If `undecided', don't try to handle multiline patterns until you see one.
619 Major/minor modes can set this variable if they know which option applies.")
621 (defvar-local font-lock-fontified nil) ; Whether we have fontified the buffer.
623 ;; Font Lock mode.
625 (eval-when-compile
627 ;; Borrowed from lazy-lock.el.
628 ;; We use this to preserve or protect things when modifying text properties.
629 (defmacro save-buffer-state (&rest body)
630 "Bind variables according to VARLIST and eval BODY restoring buffer state."
631 (declare (indent 0) (debug t))
632 `(let ((inhibit-point-motion-hooks t))
633 (with-silent-modifications
634 ,@body))))
636 (defvar-local font-lock-set-defaults nil) ; Whether we have set up defaults.
638 (defun font-lock-specified-p (mode)
639 "Return non-nil if the current buffer is ready for fontification.
640 The MODE argument, if non-nil, means Font Lock mode is about to
641 be enabled."
642 (or font-lock-defaults
643 (and (boundp 'font-lock-keywords)
644 font-lock-keywords)
645 (and mode
646 font-lock-set-defaults
647 font-lock-major-mode
648 (not (eq font-lock-major-mode major-mode)))))
650 (defun font-lock-initial-fontify ()
651 ;; The first fontification after turning the mode on. This must
652 ;; only be called after the mode hooks have been run.
653 (when (and font-lock-mode
654 (font-lock-specified-p t))
655 (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size)))
656 (cond (font-lock-fontified
657 nil)
658 ((or (null max-size) (> max-size (buffer-size)))
659 (font-lock-fontify-buffer))
660 (font-lock-verbose
661 (message "Fontifying %s...buffer size greater than font-lock-maximum-size"
662 (buffer-name)))))))
664 (defun font-lock-mode-internal (arg)
665 ;; Turn on Font Lock mode.
666 (when arg
667 (add-hook 'after-change-functions #'font-lock-after-change-function t t)
668 (font-lock-set-defaults)
669 (font-lock-turn-on-thing-lock))
670 ;; Turn off Font Lock mode.
671 (unless font-lock-mode
672 (remove-hook 'after-change-functions #'font-lock-after-change-function t)
673 (font-lock-unfontify-buffer)
674 (font-lock-turn-off-thing-lock)))
676 (defun font-lock-add-keywords (mode keywords &optional how)
677 "Add highlighting KEYWORDS for MODE.
679 MODE should be a symbol, the major mode command name, such as `c-mode'
680 or nil. If nil, highlighting keywords are added for the current buffer.
681 KEYWORDS should be a list; see the variable `font-lock-keywords'.
682 By default they are added at the beginning of the current highlighting list.
683 If optional argument HOW is `set', they are used to replace the current
684 highlighting list. If HOW is any other non-nil value, they are added at the
685 end of the current highlighting list.
687 For example:
689 (font-lock-add-keywords \\='c-mode
690 \\='((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 \\='font-lock-warning-face prepend)
691 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . \\='font-lock-keyword-face)))
693 adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
694 comments, and to fontify `and', `or' and `not' words as keywords.
696 The above procedure will only add the keywords for C mode, not
697 for modes derived from C mode. To add them for derived modes too,
698 pass nil for MODE and add the call to c-mode-hook.
700 For example:
702 (add-hook \\='c-mode-hook
703 (lambda ()
704 (font-lock-add-keywords nil
705 \\='((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 \\='font-lock-warning-face prepend)
706 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" .
707 \\='font-lock-keyword-face)))))
709 The above procedure may fail to add keywords to derived modes if
710 some involved major mode does not follow the standard conventions.
711 File a bug report if this happens, so the major mode can be corrected.
713 Note that some modes have specialized support for additional patterns, e.g.,
714 see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
715 `objc-font-lock-extra-types' and `java-font-lock-extra-types'."
716 (cond (mode
717 ;; If MODE is non-nil, add the KEYWORDS and HOW spec to
718 ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
719 (let ((spec (cons keywords how)) cell)
720 (if (setq cell (assq mode font-lock-keywords-alist))
721 (if (eq how 'set)
722 (setcdr cell (list spec))
723 (setcdr cell (append (cdr cell) (list spec))))
724 (push (list mode spec) font-lock-keywords-alist)))
725 ;; Make sure that `font-lock-removed-keywords-alist' does not
726 ;; contain the new keywords.
727 (font-lock-update-removed-keyword-alist mode keywords how))
729 (when (and font-lock-mode
730 (not (or font-lock-keywords font-lock-defaults)))
731 ;; The major mode has not set any keywords, so when we enabled
732 ;; font-lock-mode it only enabled the font-core.el part, not the
733 ;; font-lock-mode-internal. Try again.
734 (font-lock-mode -1)
735 (set (make-local-variable 'font-lock-defaults) '(nil t))
736 (font-lock-mode 1))
737 ;; Otherwise set or add the keywords now.
738 ;; This is a no-op if it has been done already in this buffer
739 ;; for the correct major mode.
740 (font-lock-set-defaults)
741 (let ((was-compiled (eq (car font-lock-keywords) t)))
742 ;; Bring back the user-level (uncompiled) keywords.
743 (if was-compiled
744 (setq font-lock-keywords (cadr font-lock-keywords)))
745 ;; Now modify or replace them.
746 (if (eq how 'set)
747 (setq font-lock-keywords keywords)
748 (font-lock-remove-keywords nil keywords) ;to avoid duplicates
749 (let ((old (if (eq (car-safe font-lock-keywords) t)
750 (cdr font-lock-keywords)
751 font-lock-keywords)))
752 (setq font-lock-keywords (if how
753 (append old keywords)
754 (append keywords old)))))
755 ;; If the keywords were compiled before, compile them again.
756 (if was-compiled
757 (setq font-lock-keywords
758 (font-lock-compile-keywords font-lock-keywords)))))))
760 (defun font-lock-update-removed-keyword-alist (mode keywords how)
761 "Update `font-lock-removed-keywords-alist' when adding new KEYWORDS to MODE."
762 ;; When font-lock is enabled first all keywords in the list
763 ;; `font-lock-keywords-alist' are added, then all keywords in the
764 ;; list `font-lock-removed-keywords-alist' are removed. If a
765 ;; keyword was once added, removed, and then added again it must be
766 ;; removed from the removed-keywords list. Otherwise the second add
767 ;; will not take effect.
768 (let ((cell (assq mode font-lock-removed-keywords-alist)))
769 (if cell
770 (if (eq how 'set)
771 ;; A new set of keywords is defined. Forget all about
772 ;; our old keywords that should be removed.
773 (setq font-lock-removed-keywords-alist
774 (delq cell font-lock-removed-keywords-alist))
775 ;; Delete all previously removed keywords.
776 (dolist (kword keywords)
777 (setcdr cell (delete kword (cdr cell))))
778 ;; Delete the mode cell if empty.
779 (if (null (cdr cell))
780 (setq font-lock-removed-keywords-alist
781 (delq cell font-lock-removed-keywords-alist)))))))
783 ;; Written by Anders Lindgren
785 ;; Case study:
786 ;; (I) The keywords are removed from a major mode.
787 ;; In this case the keyword could be local (i.e. added earlier by
788 ;; `font-lock-add-keywords'), global, or both.
790 ;; (a) In the local case we remove the keywords from the variable
791 ;; `font-lock-keywords-alist'.
793 ;; (b) The actual global keywords are not known at this time.
794 ;; All keywords are added to `font-lock-removed-keywords-alist',
795 ;; when font-lock is enabled those keywords are removed.
797 ;; Note that added keywords are taken out of the list of removed
798 ;; keywords. This ensure correct operation when the same keyword
799 ;; is added and removed several times.
801 ;; (II) The keywords are removed from the current buffer.
802 (defun font-lock-remove-keywords (mode keywords)
803 "Remove highlighting KEYWORDS for MODE.
805 MODE should be a symbol, the major mode command name, such as
806 `c-mode' or nil. If nil, highlighting keywords are removed for
807 the current buffer.
809 For a description of KEYWORDS, see `font-lock-add-keywords'.
811 To make the removal apply to modes derived from MODE as well,
812 pass nil for MODE and add the call to MODE-hook. This may fail
813 for some derived modes if some involved major mode does not
814 follow the standard conventions. File a bug report if this
815 happens, so the major mode can be corrected."
816 (cond (mode
817 ;; Remove one keyword at the time.
818 (dolist (keyword keywords)
819 (let ((top-cell (assq mode font-lock-keywords-alist)))
820 ;; If MODE is non-nil, remove the KEYWORD from
821 ;; `font-lock-keywords-alist'.
822 (when top-cell
823 (dolist (keyword-list-how-pair (cdr top-cell))
824 ;; `keywords-list-how-pair' is a cons with a list of
825 ;; keywords in the car top-cell and the original how
826 ;; argument in the cdr top-cell.
827 (setcar keyword-list-how-pair
828 (delete keyword (car keyword-list-how-pair))))
829 ;; Remove keyword list/how pair when the keyword list
830 ;; is empty and how doesn't specify `set'. (If it
831 ;; should be deleted then previously deleted keywords
832 ;; would appear again.)
833 (let ((cell top-cell))
834 (while (cdr cell)
835 (if (and (null (car (car (cdr cell))))
836 (not (eq (cdr (car (cdr cell))) 'set)))
837 (setcdr cell (cdr (cdr cell)))
838 (setq cell (cdr cell)))))
839 ;; Final cleanup, remove major mode cell if last keyword
840 ;; was deleted.
841 (if (null (cdr top-cell))
842 (setq font-lock-keywords-alist
843 (delq top-cell font-lock-keywords-alist))))
844 ;; Remember the keyword in case it is not local.
845 (let ((cell (assq mode font-lock-removed-keywords-alist)))
846 (if cell
847 (unless (member keyword (cdr cell))
848 (nconc cell (list keyword)))
849 (push (cons mode (list keyword))
850 font-lock-removed-keywords-alist))))))
852 ;; Otherwise remove it immediately.
853 (font-lock-set-defaults)
854 (let ((was-compiled (eq (car font-lock-keywords) t)))
855 ;; Bring back the user-level (uncompiled) keywords.
856 (if was-compiled
857 (setq font-lock-keywords (cadr font-lock-keywords)))
859 ;; Edit them.
860 (setq font-lock-keywords (copy-sequence font-lock-keywords))
861 (dolist (keyword keywords)
862 (setq font-lock-keywords
863 (delete keyword font-lock-keywords)))
865 ;; If the keywords were compiled before, compile them again.
866 (if was-compiled
867 (setq font-lock-keywords
868 (font-lock-compile-keywords font-lock-keywords)))))))
870 ;;; Font Lock Support mode.
872 ;; This is the code used to interface font-lock.el with any of its add-on
873 ;; packages, and provide the user interface. Packages that have their own
874 ;; local buffer fontification functions (see below) may have to call
875 ;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
876 ;; themselves.
878 (defcustom font-lock-support-mode 'jit-lock-mode
879 "Support mode for Font Lock mode.
880 Support modes speed up Font Lock mode by being choosy about when fontification
881 occurs. The default support mode, Just-in-time Lock mode (symbol
882 `jit-lock-mode'), is recommended.
884 Other, older support modes are Fast Lock mode (symbol `fast-lock-mode') and
885 Lazy Lock mode (symbol `lazy-lock-mode'). See those modes for more info.
886 However, they are no longer recommended, as Just-in-time Lock mode is better.
888 If nil, means support for Font Lock mode is never performed.
889 If a symbol, use that support mode.
890 If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
891 where MAJOR-MODE is a symbol or t (meaning the default). For example:
892 ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
893 means that Fast Lock mode is used to support Font Lock mode for buffers in C or
894 C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
896 The value of this variable is used when Font Lock mode is turned on."
897 :type '(choice (const :tag "none" nil)
898 (const :tag "fast lock" fast-lock-mode)
899 (const :tag "lazy lock" lazy-lock-mode)
900 (const :tag "jit lock" jit-lock-mode)
901 (repeat :menu-tag "mode specific" :tag "mode specific"
902 :value ((t . jit-lock-mode))
903 (cons :tag "Instance"
904 (radio :tag "Mode"
905 (const :tag "all" t)
906 (symbol :tag "name"))
907 (radio :tag "Support"
908 (const :tag "none" nil)
909 (const :tag "fast lock" fast-lock-mode)
910 (const :tag "lazy lock" lazy-lock-mode)
911 (const :tag "JIT lock" jit-lock-mode)))
913 :version "21.1"
914 :group 'font-lock)
916 (defvar fast-lock-mode)
917 (defvar lazy-lock-mode)
918 (defvar jit-lock-mode)
920 (declare-function fast-lock-after-fontify-buffer "fast-lock")
921 (declare-function fast-lock-after-unfontify-buffer "fast-lock")
922 (declare-function fast-lock-mode "fast-lock")
923 (declare-function lazy-lock-after-fontify-buffer "lazy-lock")
924 (declare-function lazy-lock-after-unfontify-buffer "lazy-lock")
925 (declare-function lazy-lock-mode "lazy-lock")
927 (defun font-lock-turn-on-thing-lock ()
928 (pcase (font-lock-value-in-major-mode font-lock-support-mode)
929 (`fast-lock-mode (fast-lock-mode t))
930 (`lazy-lock-mode (lazy-lock-mode t))
931 (`jit-lock-mode
932 ;; Prepare for jit-lock
933 (remove-hook 'after-change-functions
934 #'font-lock-after-change-function t)
935 (set (make-local-variable 'font-lock-flush-function)
936 #'jit-lock-refontify)
937 (set (make-local-variable 'font-lock-ensure-function)
938 #'jit-lock-fontify-now)
939 ;; Prevent font-lock-fontify-buffer from fontifying eagerly the whole
940 ;; buffer. This is important for things like CWarn mode which
941 ;; adds/removes a few keywords and does a refontify (which takes ages on
942 ;; large files).
943 (set (make-local-variable 'font-lock-fontify-buffer-function)
944 #'jit-lock-refontify)
945 ;; Don't fontify eagerly (and don't abort if the buffer is large).
946 (set (make-local-variable 'font-lock-fontified) t)
947 ;; Use jit-lock.
948 (jit-lock-register #'font-lock-fontify-region
949 (not font-lock-keywords-only))
950 ;; Tell jit-lock how we extend the region to refontify.
951 (add-hook 'jit-lock-after-change-extend-region-functions
952 #'font-lock-extend-jit-lock-region-after-change
953 nil t))))
955 (defun font-lock-turn-off-thing-lock ()
956 (cond ((bound-and-true-p fast-lock-mode)
957 (fast-lock-mode -1))
958 ((bound-and-true-p jit-lock-mode)
959 (jit-lock-unregister 'font-lock-fontify-region)
960 ;; Reset local vars to the non-jit-lock case.
961 (kill-local-variable 'font-lock-fontify-buffer-function))
962 ((bound-and-true-p lazy-lock-mode)
963 (lazy-lock-mode -1))))
965 (defun font-lock-after-fontify-buffer ()
966 (cond ((bound-and-true-p fast-lock-mode)
967 (fast-lock-after-fontify-buffer))
968 ;; Useless now that jit-lock intercepts font-lock-fontify-buffer. -sm
969 ;; (jit-lock-mode
970 ;; (jit-lock-after-fontify-buffer))
971 ((bound-and-true-p lazy-lock-mode)
972 (lazy-lock-after-fontify-buffer))))
974 (defun font-lock-after-unfontify-buffer ()
975 (cond ((bound-and-true-p fast-lock-mode)
976 (fast-lock-after-unfontify-buffer))
977 ;; Useless as well. It's only called when:
978 ;; - turning off font-lock: it does not matter if we leave spurious
979 ;; `fontified' text props around since jit-lock-mode is also off.
980 ;; - font-lock-default-fontify-buffer fails: this is not run
981 ;; any more anyway. -sm
983 ;; (jit-lock-mode
984 ;; (jit-lock-after-unfontify-buffer))
985 ((bound-and-true-p lazy-lock-mode)
986 (lazy-lock-after-unfontify-buffer))))
988 ;;; End of Font Lock Support mode.
990 ;;; Fontification functions.
992 ;; Rather than the function, e.g., `font-lock-fontify-region' containing the
993 ;; code to fontify a region, the function runs the function whose name is the
994 ;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally,
995 ;; the value of this variable is, e.g., `font-lock-default-fontify-region'
996 ;; which does contain the code to fontify a region. However, the value of the
997 ;; variable could be anything and thus, e.g., `font-lock-fontify-region' could
998 ;; do anything. The indirection of the fontification functions gives major
999 ;; modes the capability of modifying the way font-lock.el fontifies. Major
1000 ;; modes can modify the values of, e.g., `font-lock-fontify-region-function',
1001 ;; via the variable `font-lock-defaults'.
1003 ;; For example, Rmail mode sets the variable `font-lock-defaults' so that
1004 ;; font-lock.el uses its own function for buffer fontification. This function
1005 ;; makes fontification be on a message-by-message basis and so visiting an
1006 ;; RMAIL file is much faster. A clever implementation of the function might
1007 ;; fontify the headers differently than the message body. (It should, and
1008 ;; correspondingly for Mail mode, but I can't be bothered to do the work. Can
1009 ;; you?) This hints at a more interesting use...
1011 ;; Languages that contain text normally contained in different major modes
1012 ;; could define their own fontification functions that treat text differently
1013 ;; depending on its context. For example, Perl mode could arrange that here
1014 ;; docs are fontified differently than Perl code. Or Yacc mode could fontify
1015 ;; rules one way and C code another. Neat!
1017 ;; A further reason to use the fontification indirection feature is when the
1018 ;; default syntactic fontification, or the default fontification in general,
1019 ;; is not flexible enough for a particular major mode. For example, perhaps
1020 ;; comments are just too hairy for `font-lock-fontify-syntactically-region' to
1021 ;; cope with. You need to write your own version of that function, e.g.,
1022 ;; `hairy-fontify-syntactically-region', and make your own version of
1023 ;; `hairy-fontify-region' call that function before calling
1024 ;; `font-lock-fontify-keywords-region' for the normal regexp fontification
1025 ;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el
1026 ;; would call your region fontification function instead of its own. For
1027 ;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line
1028 ;; directives correctly and cleanly. (It is the same problem as fontifying
1029 ;; multi-line strings and comments; regexps are not appropriate for the job.)
1031 (defvar font-lock-extend-after-change-region-function nil
1032 "A function that determines the region to refontify after a change.
1034 This variable is either nil, or is a function that determines the
1035 region to refontify after a change.
1036 It is usually set by the major mode via `font-lock-defaults'.
1037 Font-lock calls this function after each buffer change.
1039 The function is given three parameters, the standard BEG, END, and OLD-LEN
1040 from `after-change-functions'. It should return either a cons of the beginning
1041 and end buffer positions \(in that order) of the region to refontify, or nil
1042 \(which directs the caller to fontify a default region).
1043 This function should preserve the match-data.
1044 The region it returns may start or end in the middle of a line.")
1045 (make-variable-buffer-local 'font-lock-extend-after-change-region-function)
1047 (defun font-lock-fontify-buffer (&optional interactively)
1048 "Fontify the current buffer the way the function `font-lock-mode' would."
1049 (declare
1050 ;; When called from Lisp, this function is a big mess. The caller usually
1051 ;; expects one of the following behaviors:
1052 ;; - refresh the highlighting (because the font-lock-keywords have been
1053 ;; changed).
1054 ;; - apply font-lock highlighting even if font-lock-mode is not enabled.
1055 ;; - reset the highlighting rules because font-lock-defaults
1056 ;; has been changed (and then rehighlight everything).
1057 ;; Of course, this function doesn't do all of the above in all situations
1058 ;; (e.g. depending on whether jit-lock is in use) and it can't guess what
1059 ;; the caller wants.
1060 (interactive-only "use `font-lock-ensure' or `font-lock-flush' instead."))
1061 (interactive "p")
1062 (font-lock-set-defaults)
1063 (let ((font-lock-verbose (or font-lock-verbose interactively)))
1064 (funcall font-lock-fontify-buffer-function)))
1066 (defun font-lock-unfontify-buffer ()
1067 (funcall font-lock-unfontify-buffer-function))
1069 (defun font-lock-fontify-region (beg end &optional loudly)
1070 "Fontify the text between BEG and END.
1071 If LOUDLY is non-nil, print status messages while fontifying.
1072 This works by calling `font-lock-fontify-region-function'."
1073 (font-lock-set-defaults)
1074 (funcall font-lock-fontify-region-function beg end loudly))
1076 (defun font-lock-unfontify-region (beg end)
1077 "Unfontify the text between BEG and END.
1078 This works by calling `font-lock-unfontify-region-function'."
1079 (save-buffer-state
1080 (funcall font-lock-unfontify-region-function beg end)))
1082 (defvar font-lock-flush-function #'font-lock-after-change-function
1083 "Function to use to mark a region for refontification.
1084 Called with two arguments BEG and END.")
1086 (defun font-lock-flush (&optional beg end)
1087 "Declare the region BEG...END's fontification as out-of-date.
1088 If the region is not specified, it defaults to the entire
1089 accessible portion of the current buffer."
1090 (and font-lock-mode
1091 font-lock-fontified
1092 (funcall font-lock-flush-function
1093 (or beg (point-min)) (or end (point-max)))))
1095 (defvar font-lock-ensure-function
1096 (lambda (_beg _end)
1097 (unless font-lock-fontified
1098 (font-lock-default-fontify-buffer)
1099 (unless font-lock-mode
1100 ;; If font-lock is not enabled, we don't have the hooks in place to
1101 ;; track modifications, so a subsequent call to font-lock-ensure can't
1102 ;; assume that the fontification is still valid.
1103 (setq font-lock-fontified nil))))
1104 "Function to make sure a region has been fontified.
1105 Called with two arguments BEG and END.")
1107 (defun font-lock-ensure (&optional beg end)
1108 "Make sure the region BEG...END has been fontified.
1109 If the region is not specified, it defaults to the entire accessible
1110 portion of the buffer."
1111 (font-lock-set-defaults)
1112 (funcall font-lock-ensure-function
1113 (or beg (point-min)) (or end (point-max))))
1115 (defun font-lock-default-fontify-buffer ()
1116 "Fontify the whole buffer using `font-lock-fontify-region-function'."
1117 (let ((verbose (if (numberp font-lock-verbose)
1118 (> (buffer-size) font-lock-verbose)
1119 font-lock-verbose)))
1120 (with-temp-message
1121 (when verbose
1122 (format "Fontifying %s..." (buffer-name)))
1123 ;; Make sure we fontify etc. in the whole buffer.
1124 (save-restriction
1125 (unless font-lock-dont-widen (widen))
1126 (condition-case nil
1127 (save-excursion
1128 (save-match-data
1129 (font-lock-fontify-region (point-min) (point-max) verbose)
1130 (font-lock-after-fontify-buffer)
1131 (setq font-lock-fontified t)))
1132 ;; We don't restore the old fontification, so it's best to unfontify.
1133 (quit (font-lock-unfontify-buffer)))))))
1135 (defun font-lock-default-unfontify-buffer ()
1136 "Unfontify the whole buffer using `font-lock-unfontify-region-function'."
1137 ;; Make sure we unfontify etc. in the whole buffer.
1138 (save-restriction
1139 (widen)
1140 (font-lock-unfontify-region (point-min) (point-max))
1141 (font-lock-after-unfontify-buffer)
1142 (setq font-lock-fontified nil)))
1144 (defvar font-lock-dont-widen nil
1145 "If non-nil, font-lock will work on the non-widened buffer.
1146 Useful for things like RMAIL and Info where the whole buffer is not
1147 a very meaningful entity to highlight.")
1150 (defvar font-lock-beg) (defvar font-lock-end)
1151 (defvar font-lock-extend-region-functions
1152 '(font-lock-extend-region-wholelines
1153 ;; This use of font-lock-multiline property is unreliable but is just
1154 ;; a handy heuristic: in case you don't have a function that does
1155 ;; /identification/ of multiline elements, you may still occasionally
1156 ;; discover them by accident (or you may /identify/ them but not in all
1157 ;; cases), in which case the font-lock-multiline property can help make
1158 ;; sure you will properly *re*identify them during refontification.
1159 font-lock-extend-region-multiline)
1160 "Special hook run just before proceeding to fontify a region.
1161 This is used to allow major modes to help font-lock find safe buffer positions
1162 as beginning and end of the fontified region. Its most common use is to solve
1163 the problem of /identification/ of multiline elements by providing a function
1164 that tries to find such elements and move the boundaries such that they do
1165 not fall in the middle of one.
1166 Each function is called with no argument; it is expected to adjust the
1167 dynamically bound variables `font-lock-beg' and `font-lock-end'; and return
1168 non-nil if it did make such an adjustment.
1169 These functions are run in turn repeatedly until they all return nil.
1170 Put first the functions more likely to cause a change and cheaper to compute.")
1171 ;; Mark it as a special hook which doesn't use any global setting
1172 ;; (i.e. doesn't obey the element t in the buffer-local value).
1173 (make-variable-buffer-local 'font-lock-extend-region-functions)
1175 (defun font-lock-extend-region-multiline ()
1176 "Move fontification boundaries away from any `font-lock-multiline' property."
1177 (let ((changed nil))
1178 (when (and (> font-lock-beg (point-min))
1179 (get-text-property (1- font-lock-beg) 'font-lock-multiline))
1180 (setq changed t)
1181 (setq font-lock-beg (or (previous-single-property-change
1182 font-lock-beg 'font-lock-multiline)
1183 (point-min))))
1185 (when (get-text-property font-lock-end 'font-lock-multiline)
1186 (setq changed t)
1187 (setq font-lock-end (or (text-property-any font-lock-end (point-max)
1188 'font-lock-multiline nil)
1189 (point-max))))
1190 changed))
1192 (defun font-lock-extend-region-wholelines ()
1193 "Move fontification boundaries to beginning of lines."
1194 (let ((changed nil))
1195 (goto-char font-lock-beg)
1196 (unless (bolp)
1197 (setq changed t font-lock-beg
1198 (let ((inhibit-field-text-motion t))
1199 (line-beginning-position))))
1200 (goto-char font-lock-end)
1201 (unless (bolp)
1202 (unless (eq font-lock-end
1203 (setq font-lock-end (line-beginning-position 2)))
1204 (setq changed t)))
1205 changed))
1207 (defun font-lock-default-fontify-region (beg end loudly)
1208 "Fontify the text between BEG and END.
1209 If LOUDLY is non-nil, print status messages while fontifying.
1210 This function is the default `font-lock-fontify-region-function'."
1211 (save-buffer-state
1212 ;; Use the fontification syntax table, if any.
1213 (with-syntax-table (or font-lock-syntax-table (syntax-table))
1214 (save-restriction
1215 (unless font-lock-dont-widen (widen))
1216 ;; Extend the region to fontify so that it starts and ends at
1217 ;; safe places.
1218 (let ((funs font-lock-extend-region-functions)
1219 (font-lock-beg beg)
1220 (font-lock-end end))
1221 (while funs
1222 (setq funs (if (or (not (funcall (car funs)))
1223 (eq funs font-lock-extend-region-functions))
1224 (cdr funs)
1225 ;; If there's been a change, we should go through
1226 ;; the list again since this new position may
1227 ;; warrant a different answer from one of the fun
1228 ;; we've already seen.
1229 font-lock-extend-region-functions)))
1230 (setq beg font-lock-beg end font-lock-end))
1231 ;; Now do the fontification.
1232 (font-lock-unfontify-region beg end)
1233 (when (and font-lock-syntactic-keywords
1234 (null syntax-propertize-function))
1235 ;; Ensure the beginning of the file is properly syntactic-fontified.
1236 (let ((start beg))
1237 (when (< font-lock-syntactically-fontified start)
1238 (setq start (max font-lock-syntactically-fontified (point-min)))
1239 (setq font-lock-syntactically-fontified end))
1240 (font-lock-fontify-syntactic-keywords-region start end)))
1241 (unless font-lock-keywords-only
1242 (font-lock-fontify-syntactically-region beg end loudly))
1243 (font-lock-fontify-keywords-region beg end loudly)
1244 `(jit-lock-bounds ,beg . ,end)))))
1246 ;; The following must be rethought, since keywords can override fontification.
1247 ;; ;; Now scan for keywords, but not if we are inside a comment now.
1248 ;; (or (and (not font-lock-keywords-only)
1249 ;; (let ((state (parse-partial-sexp beg end nil nil
1250 ;; font-lock-cache-state)))
1251 ;; (or (nth 4 state) (nth 7 state))))
1252 ;; (font-lock-fontify-keywords-region beg end))
1254 (defvar font-lock-extra-managed-props nil
1255 "Additional text properties managed by font-lock.
1256 This is used by `font-lock-default-unfontify-region' to decide
1257 what properties to clear before refontifying a region.")
1259 (defun font-lock-default-unfontify-region (beg end)
1260 "Unfontify the text between BEG and END.
1261 This function is the default `font-lock-unfontify-region-function'."
1262 (remove-list-of-text-properties
1263 beg end (append
1264 font-lock-extra-managed-props
1265 (if font-lock-syntactic-keywords
1266 '(syntax-table face font-lock-multiline)
1267 '(face font-lock-multiline)))))
1269 ;; Called when any modification is made to buffer text.
1270 (defun font-lock-after-change-function (beg end &optional old-len)
1271 (save-excursion
1272 (let ((inhibit-point-motion-hooks t)
1273 (inhibit-quit t)
1274 (region (if font-lock-extend-after-change-region-function
1275 (funcall font-lock-extend-after-change-region-function
1276 beg end old-len))))
1277 (save-match-data
1278 (if region
1279 ;; Fontify the region the major mode has specified.
1280 (setq beg (car region) end (cdr region))
1281 ;; Fontify the whole lines which enclose the region.
1282 ;; Actually, this is not needed because
1283 ;; font-lock-default-fontify-region already rounds up to a whole
1284 ;; number of lines.
1285 ;; (setq beg (progn (goto-char beg) (line-beginning-position))
1286 ;; end (progn (goto-char end) (line-beginning-position 2)))
1287 (unless (eq end (point-max))
1288 ;; Rounding up to a whole number of lines should include the
1289 ;; line right after `end'. Typical case: the first char of
1290 ;; the line was deleted. Or a \n was inserted in the middle
1291 ;; of a line.
1292 (setq end (1+ end))))
1293 (font-lock-fontify-region beg end)))))
1295 (defvar jit-lock-start) (defvar jit-lock-end)
1296 (defun font-lock-extend-jit-lock-region-after-change (beg end old-len)
1297 "Function meant for `jit-lock-after-change-extend-region-functions'.
1298 This function does 2 things:
1299 - extend the region so that it not only includes the part that was modified
1300 but also the surrounding text whose highlighting may change as a consequence.
1301 - anticipate (part of) the region extension that will happen later in
1302 `font-lock-default-fontify-region', in order to avoid the need for
1303 double-redisplay in `jit-lock-fontify-now'."
1304 (save-excursion
1305 ;; First extend the region as font-lock-after-change-function would.
1306 (let ((region (if font-lock-extend-after-change-region-function
1307 (funcall font-lock-extend-after-change-region-function
1308 beg end old-len))))
1309 (if region
1310 (setq beg (min jit-lock-start (car region))
1311 end (max jit-lock-end (cdr region))))
1312 ;; Then extend the region obeying font-lock-multiline properties,
1313 ;; indicating which part of the buffer needs to be refontified.
1314 ;; !!! This is the *main* user of font-lock-multiline property !!!
1315 ;; font-lock-after-change-function could/should also do that, but it
1316 ;; doesn't need to because font-lock-default-fontify-region does
1317 ;; it anyway. Here OTOH we have no guarantee that
1318 ;; font-lock-default-fontify-region will be executed on this region
1319 ;; any time soon.
1320 ;; Note: contrary to font-lock-default-fontify-region, we do not do
1321 ;; any loop here because we are not looking for a safe spot: we just
1322 ;; mark the text whose appearance may need to change as a result of
1323 ;; the buffer modification.
1324 (when (and (> beg (point-min))
1325 (get-text-property (1- beg) 'font-lock-multiline))
1326 (setq beg (or (previous-single-property-change
1327 beg 'font-lock-multiline)
1328 (point-min))))
1329 (when (< end (point-max))
1330 (setq end
1331 (cond
1332 ((get-text-property end 'font-lock-multiline)
1333 (or (text-property-any end (point-max)
1334 'font-lock-multiline nil)
1335 (point-max)))
1336 ;; If `end' has been set by the function above, don't corrupt it.
1337 (font-lock-extend-after-change-region-function end)
1338 ;; Rounding up to a whole number of lines should include the
1339 ;; line right after `end'. Typical case: the first char of
1340 ;; the line was deleted. Or a \n was inserted in the middle
1341 ;; of a line.
1342 (t (1+ end)))))
1343 ;; Finally, pre-enlarge the region to a whole number of lines, to try
1344 ;; and anticipate what font-lock-default-fontify-region will do, so as to
1345 ;; avoid double-redisplay.
1346 ;; We could just run `font-lock-extend-region-functions', but since
1347 ;; the only purpose is to avoid the double-redisplay, we prefer to
1348 ;; do here only the part that is cheap and most likely to be useful.
1349 (when (memq 'font-lock-extend-region-wholelines
1350 font-lock-extend-region-functions)
1351 (goto-char beg)
1352 (setq beg (min jit-lock-start (line-beginning-position)))
1353 (goto-char end)
1354 (setq end
1355 (max jit-lock-end
1356 (if (bolp) (point) (line-beginning-position 2)))))
1357 (setq jit-lock-start beg
1358 jit-lock-end end))))
1360 (defun font-lock-fontify-block (&optional arg)
1361 "Fontify some lines the way `font-lock-fontify-buffer' would.
1362 The lines could be a function or paragraph, or a specified number of lines.
1363 If ARG is given, fontify that many lines before and after point, or 16 lines if
1364 no ARG is given and `font-lock-mark-block-function' is nil.
1365 If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
1366 delimit the region to fontify."
1367 (interactive "P")
1368 (let ((inhibit-point-motion-hooks t)
1369 deactivate-mark)
1370 ;; Make sure we have the right `font-lock-keywords' etc.
1371 (if (not font-lock-mode) (font-lock-set-defaults))
1372 (save-mark-and-excursion
1373 (save-match-data
1374 (condition-case error-data
1375 (if (or arg (not font-lock-mark-block-function))
1376 (let ((lines (if arg (prefix-numeric-value arg) 16)))
1377 (font-lock-fontify-region
1378 (save-excursion (forward-line (- lines)) (point))
1379 (save-excursion (forward-line lines) (point))))
1380 (funcall font-lock-mark-block-function)
1381 (font-lock-fontify-region (point) (mark)))
1382 ((error quit) (message "Fontifying block...%s" error-data)))))))
1384 ;;; End of Fontification functions.
1386 ;;; Additional text property functions.
1388 ;; The following text property functions should be builtins. This means they
1389 ;; should be written in C and put with all the other text property functions.
1390 ;; In the meantime, those that are used by font-lock.el are defined in Lisp
1391 ;; below and given a `font-lock-' prefix. Those that are not used are defined
1392 ;; in Lisp below and commented out. sm.
1394 (defun font-lock-prepend-text-property (start end prop value &optional object)
1395 "Prepend to one property of the text from START to END.
1396 Arguments PROP and VALUE specify the property and value to prepend to the value
1397 already in place. The resulting property values are always lists.
1398 Optional argument OBJECT is the string or buffer containing the text."
1399 (let ((val (if (listp value) value (list value))) next prev)
1400 (while (/= start end)
1401 (setq next (next-single-property-change start prop object end)
1402 prev (get-text-property start prop object))
1403 ;; Canonicalize old forms of face property.
1404 (and (memq prop '(face font-lock-face))
1405 (listp prev)
1406 (or (keywordp (car prev))
1407 (memq (car prev) '(foreground-color background-color)))
1408 (setq prev (list prev)))
1409 (put-text-property start next prop
1410 (append val (if (listp prev) prev (list prev)))
1411 object)
1412 (setq start next))))
1414 (defun font-lock-append-text-property (start end prop value &optional object)
1415 "Append to one property of the text from START to END.
1416 Arguments PROP and VALUE specify the property and value to append to the value
1417 already in place. The resulting property values are always lists.
1418 Optional argument OBJECT is the string or buffer containing the text."
1419 (let ((val (if (listp value) value (list value))) next prev)
1420 (while (/= start end)
1421 (setq next (next-single-property-change start prop object end)
1422 prev (get-text-property start prop object))
1423 ;; Canonicalize old forms of face property.
1424 (and (memq prop '(face font-lock-face))
1425 (listp prev)
1426 (or (keywordp (car prev))
1427 (memq (car prev) '(foreground-color background-color)))
1428 (setq prev (list prev)))
1429 (put-text-property start next prop
1430 (append (if (listp prev) prev (list prev)) val)
1431 object)
1432 (setq start next))))
1434 (defun font-lock-fillin-text-property (start end prop value &optional object)
1435 "Fill in one property of the text from START to END.
1436 Arguments PROP and VALUE specify the property and value to put where none are
1437 already in place. Therefore existing property values are not overwritten.
1438 Optional argument OBJECT is the string or buffer containing the text."
1439 (let ((start (text-property-any start end prop nil object)) next)
1440 (while start
1441 (setq next (next-single-property-change start prop object end))
1442 (put-text-property start next prop value object)
1443 (setq start (text-property-any next end prop nil object)))))
1445 (defun font-lock--remove-face-from-text-property (start
1447 prop value &optional object)
1448 "Remove a specific property value from text from START to END.
1449 Arguments PROP and VALUE specify the property and value to remove. The
1450 resulting property values are not `eq' to VALUE nor lists containing VALUE.
1451 Optional argument OBJECT is the string or buffer containing the text."
1452 (let ((start (text-property-not-all start end prop nil object)) next prev)
1453 (while start
1454 (setq next (next-single-property-change start prop object end)
1455 prev (get-text-property start prop object))
1456 (cond ((or (atom prev)
1457 (keywordp (car prev))
1458 (eq (car prev) 'foreground-color)
1459 (eq (car prev) 'background-color))
1460 (when (eq value prev)
1461 (remove-list-of-text-properties start next (list prop) object)))
1462 ((memq value prev) ;Assume prev is not dotted.
1463 (let ((new (remq value prev)))
1464 (cond ((null new)
1465 (remove-list-of-text-properties start next (list prop)
1466 object))
1467 ((= (length new) 1)
1468 (put-text-property start next prop (car new) object))
1470 (put-text-property start next prop new object))))))
1471 (setq start (text-property-not-all next end prop nil object)))))
1473 ;;; End of Additional text property functions.
1475 ;;; Syntactic regexp fontification functions.
1477 ;; These syntactic keyword pass functions are identical to those keyword pass
1478 ;; functions below, with the following exceptions; (a) they operate on
1479 ;; `font-lock-syntactic-keywords' of course, (b) they are all `defun' as speed
1480 ;; is less of an issue, (c) eval of property value does not occur JIT as speed
1481 ;; is less of an issue, (d) OVERRIDE cannot be `prepend' or `append' as it
1482 ;; makes no sense for `syntax-table' property values, (e) they do not do it
1483 ;; LOUDLY as it is not likely to be intensive.
1485 (defun font-lock-apply-syntactic-highlight (highlight)
1486 "Apply HIGHLIGHT following a match.
1487 HIGHLIGHT should be of the form MATCH-HIGHLIGHT,
1488 see `font-lock-syntactic-keywords'."
1489 (let* ((match (nth 0 highlight))
1490 (start (match-beginning match)) (end (match-end match))
1491 (value (nth 1 highlight))
1492 (override (nth 2 highlight)))
1493 (if (not start)
1494 ;; No match but we might not signal an error.
1495 (or (nth 3 highlight)
1496 (error "No match %d in highlight %S" match highlight))
1497 (when (and (consp value) (not (numberp (car value))))
1498 (setq value (eval value)))
1499 (when (stringp value) (setq value (string-to-syntax value)))
1500 ;; Flush the syntax-cache. I believe this is not necessary for
1501 ;; font-lock's use of syntax-ppss, but I'm not 100% sure and it can
1502 ;; still be necessary for other users of syntax-ppss anyway.
1503 (syntax-ppss-after-change-function start)
1504 (cond
1505 ((not override)
1506 ;; Cannot override existing fontification.
1507 (or (text-property-not-all start end 'syntax-table nil)
1508 (put-text-property start end 'syntax-table value)))
1509 ((eq override t)
1510 ;; Override existing fontification.
1511 (put-text-property start end 'syntax-table value))
1512 ((eq override 'keep)
1513 ;; Keep existing fontification.
1514 (font-lock-fillin-text-property start end 'syntax-table value))))))
1516 (defun font-lock-fontify-syntactic-anchored-keywords (keywords limit)
1517 "Fontify according to KEYWORDS until LIMIT.
1518 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1519 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1520 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1521 ;; Evaluate PRE-MATCH-FORM.
1522 (pre-match-value (eval (nth 1 keywords))))
1523 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1524 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1525 (setq limit pre-match-value)
1526 (setq limit (line-end-position)))
1527 (save-match-data
1528 ;; Find an occurrence of `matcher' before `limit'.
1529 (while (if (stringp matcher)
1530 (re-search-forward matcher limit t)
1531 (funcall matcher limit))
1532 ;; Apply each highlight to this instance of `matcher'.
1533 (setq highlights lowdarks)
1534 (while highlights
1535 (font-lock-apply-syntactic-highlight (car highlights))
1536 (setq highlights (cdr highlights)))))
1537 ;; Evaluate POST-MATCH-FORM.
1538 (eval (nth 2 keywords))))
1540 (defun font-lock-fontify-syntactic-keywords-region (start end)
1541 "Fontify according to `font-lock-syntactic-keywords' between START and END.
1542 START should be at the beginning of a line."
1543 (unless parse-sexp-lookup-properties
1544 ;; We wouldn't go through so much trouble if we didn't intend to use those
1545 ;; properties, would we?
1546 (set (make-local-variable 'parse-sexp-lookup-properties) t))
1547 ;; If `font-lock-syntactic-keywords' is a symbol, get the real keywords.
1548 (when (symbolp font-lock-syntactic-keywords)
1549 (setq font-lock-syntactic-keywords (font-lock-eval-keywords
1550 font-lock-syntactic-keywords)))
1551 ;; If `font-lock-syntactic-keywords' is not compiled, compile it.
1552 (unless (eq (car font-lock-syntactic-keywords) t)
1553 (setq font-lock-syntactic-keywords (font-lock-compile-keywords
1554 font-lock-syntactic-keywords
1555 t)))
1556 ;; Get down to business.
1557 (let ((case-fold-search font-lock-keywords-case-fold-search)
1558 (keywords (cddr font-lock-syntactic-keywords))
1559 keyword matcher highlights)
1560 (while keywords
1561 ;; Find an occurrence of `matcher' from `start' to `end'.
1562 (setq keyword (car keywords) matcher (car keyword))
1563 (goto-char start)
1564 (while (and (< (point) end)
1565 (if (stringp matcher)
1566 (re-search-forward matcher end t)
1567 (funcall matcher end)))
1568 ;; Apply each highlight to this instance of `matcher', which may be
1569 ;; specific highlights or more keywords anchored to `matcher'.
1570 (setq highlights (cdr keyword))
1571 (while highlights
1572 (if (numberp (car (car highlights)))
1573 (font-lock-apply-syntactic-highlight (car highlights))
1574 (font-lock-fontify-syntactic-anchored-keywords (car highlights)
1575 end))
1576 (setq highlights (cdr highlights))))
1577 (setq keywords (cdr keywords)))))
1579 ;;; End of Syntactic regexp fontification functions.
1581 ;;; Syntactic fontification functions.
1583 (defvar font-lock-comment-start-skip nil
1584 "If non-nil, Font Lock mode uses this instead of `comment-start-skip'.")
1586 (defvar font-lock-comment-end-skip nil
1587 "If non-nil, Font Lock mode uses this instead of `comment-end'.")
1589 (defun font-lock-fontify-syntactically-region (start end &optional loudly)
1590 "Put proper face on each string and comment between START and END.
1591 START should be at the beginning of a line."
1592 (syntax-propertize end) ; Apply any needed syntax-table properties.
1593 (with-syntax-table (or syntax-ppss-table (syntax-table))
1594 (let ((comment-end-regexp
1595 (or font-lock-comment-end-skip
1596 (regexp-quote
1597 (replace-regexp-in-string "^ *" "" comment-end))))
1598 ;; Find the `start' state.
1599 (state (syntax-ppss start))
1600 face beg)
1601 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
1603 ;; Find each interesting place between here and `end'.
1604 (while
1605 (progn
1606 (when (or (nth 3 state) (nth 4 state))
1607 (setq face (funcall font-lock-syntactic-face-function state))
1608 (setq beg (max (nth 8 state) start))
1609 (setq state (parse-partial-sexp (point) end nil nil state
1610 'syntax-table))
1611 (when face (put-text-property beg (point) 'face face))
1612 (when (and (eq face 'font-lock-comment-face)
1613 (or font-lock-comment-start-skip
1614 comment-start-skip))
1615 ;; Find the comment delimiters
1616 ;; and use font-lock-comment-delimiter-face for them.
1617 (save-excursion
1618 (goto-char beg)
1619 (if (looking-at (or font-lock-comment-start-skip
1620 comment-start-skip))
1621 (put-text-property beg (match-end 0) 'face
1622 font-lock-comment-delimiter-face)))
1623 (if (looking-back comment-end-regexp (point-at-bol) t)
1624 (put-text-property (match-beginning 0) (point) 'face
1625 font-lock-comment-delimiter-face))))
1626 (< (point) end))
1627 (setq state (parse-partial-sexp (point) end nil nil state
1628 'syntax-table))))))
1630 ;;; End of Syntactic fontification functions.
1632 ;;; Keyword regexp fontification functions.
1634 (defsubst font-lock-apply-highlight (highlight)
1635 "Apply HIGHLIGHT following a match.
1636 HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
1637 (let* ((match (nth 0 highlight))
1638 (start (match-beginning match)) (end (match-end match))
1639 (override (nth 2 highlight)))
1640 (if (not start)
1641 ;; No match but we might not signal an error.
1642 (or (nth 3 highlight)
1643 (error "No match %d in highlight %S" match highlight))
1644 (let ((val (eval (nth 1 highlight))))
1645 (when (eq (car-safe val) 'face)
1646 (add-text-properties start end (cddr val))
1647 (setq val (cadr val)))
1648 (cond
1649 ((not (or val (eq override t)))
1650 ;; If `val' is nil, don't do anything. It is important to do it
1651 ;; explicitly, because when adding nil via things like
1652 ;; font-lock-append-text-property, the property is actually
1653 ;; changed from <face> to (<face>) which is undesirable. --Stef
1654 nil)
1655 ((not override)
1656 ;; Cannot override existing fontification.
1657 (or (text-property-not-all start end 'face nil)
1658 (put-text-property start end 'face val)))
1659 ((eq override t)
1660 ;; Override existing fontification.
1661 (put-text-property start end 'face val))
1662 ((eq override 'prepend)
1663 ;; Prepend to existing fontification.
1664 (font-lock-prepend-text-property start end 'face val))
1665 ((eq override 'append)
1666 ;; Append to existing fontification.
1667 (font-lock-append-text-property start end 'face val))
1668 ((eq override 'keep)
1669 ;; Keep existing fontification.
1670 (font-lock-fillin-text-property start end 'face val)))))))
1672 (defsubst font-lock-fontify-anchored-keywords (keywords limit)
1673 "Fontify according to KEYWORDS until LIMIT.
1674 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1675 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1676 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1677 (lead-start (match-beginning 0))
1678 ;; Evaluate PRE-MATCH-FORM.
1679 (pre-match-value (eval (nth 1 keywords))))
1680 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1681 (if (not (and (numberp pre-match-value) (> pre-match-value (point))))
1682 (setq limit (line-end-position))
1683 (setq limit pre-match-value)
1684 (when (and font-lock-multiline (>= limit (line-beginning-position 2)))
1685 ;; this is a multiline anchored match
1686 ;; (setq font-lock-multiline t)
1687 (put-text-property (if (= limit (line-beginning-position 2))
1688 (1- limit)
1689 (min lead-start (point)))
1690 limit
1691 'font-lock-multiline t)))
1692 (save-match-data
1693 ;; Find an occurrence of `matcher' before `limit'.
1694 (while (and (< (point) limit)
1695 (if (stringp matcher)
1696 (re-search-forward matcher limit t)
1697 (funcall matcher limit)))
1698 ;; Apply each highlight to this instance of `matcher'.
1699 (setq highlights lowdarks)
1700 (while highlights
1701 (font-lock-apply-highlight (car highlights))
1702 (setq highlights (cdr highlights)))))
1703 ;; Evaluate POST-MATCH-FORM.
1704 (eval (nth 2 keywords))))
1706 (defun font-lock-fontify-keywords-region (start end &optional loudly)
1707 "Fontify according to `font-lock-keywords' between START and END.
1708 START should be at the beginning of a line.
1709 LOUDLY, if non-nil, allows progress-meter bar."
1710 (unless (eq (car font-lock-keywords) t)
1711 (setq font-lock-keywords
1712 (font-lock-compile-keywords font-lock-keywords)))
1713 (let ((case-fold-search font-lock-keywords-case-fold-search)
1714 (keywords (cddr font-lock-keywords))
1715 (bufname (buffer-name)) (count 0)
1716 (pos (make-marker))
1717 keyword matcher highlights)
1719 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1720 (while keywords
1721 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
1722 (make-string (cl-incf count) ?.)))
1724 ;; Find an occurrence of `matcher' from `start' to `end'.
1725 (setq keyword (car keywords) matcher (car keyword))
1726 (goto-char start)
1727 (while (and (< (point) end)
1728 (if (stringp matcher)
1729 (re-search-forward matcher end t)
1730 (funcall matcher end))
1731 ;; Beware empty string matches since they will
1732 ;; loop indefinitely.
1733 (or (> (point) (match-beginning 0))
1734 (progn (forward-char 1) t)))
1735 (when (and font-lock-multiline
1736 (>= (point)
1737 (save-excursion (goto-char (match-beginning 0))
1738 (forward-line 1) (point))))
1739 ;; this is a multiline regexp match
1740 ;; (setq font-lock-multiline t)
1741 (put-text-property (if (= (point)
1742 (save-excursion
1743 (goto-char (match-beginning 0))
1744 (forward-line 1) (point)))
1745 (1- (point))
1746 (match-beginning 0))
1747 (point)
1748 'font-lock-multiline t))
1749 ;; Apply each highlight to this instance of `matcher', which may be
1750 ;; specific highlights or more keywords anchored to `matcher'.
1751 (setq highlights (cdr keyword))
1752 (while highlights
1753 (if (numberp (car (car highlights)))
1754 (font-lock-apply-highlight (car highlights))
1755 (set-marker pos (point))
1756 (font-lock-fontify-anchored-keywords (car highlights) end)
1757 ;; Ensure forward progress. `pos' is a marker because anchored
1758 ;; keyword may add/delete text (this happens e.g. in grep.el).
1759 (if (< (point) pos) (goto-char pos)))
1760 (setq highlights (cdr highlights))))
1761 (setq keywords (cdr keywords)))
1762 (set-marker pos nil)))
1764 ;;; End of Keyword regexp fontification functions.
1766 ;; Various functions.
1768 (defun font-lock-compile-keywords (keywords &optional syntactic-keywords)
1769 "Compile KEYWORDS into the form (t KEYWORDS COMPILED...)
1770 Here each COMPILED is of the form (MATCHER HIGHLIGHT ...) as shown in the
1771 `font-lock-keywords' doc string.
1772 If SYNTACTIC-KEYWORDS is non-nil, it means these keywords are used for
1773 `font-lock-syntactic-keywords' rather than for `font-lock-keywords'."
1774 (if (not font-lock-set-defaults)
1775 ;; This should never happen. But some external packages sometimes
1776 ;; call font-lock in unexpected and incorrect ways. It's important to
1777 ;; stop processing at this point, otherwise we may end up changing the
1778 ;; global value of font-lock-keywords and break highlighting in many
1779 ;; other buffers.
1780 (error "Font-lock trying to use keywords before setting them up"))
1781 (if (eq (car-safe keywords) t)
1782 keywords
1783 (setq keywords
1784 (cons t (cons keywords
1785 (mapcar #'font-lock-compile-keyword keywords))))
1786 (if (and (not syntactic-keywords)
1787 (let ((beg-function syntax-begin-function))
1788 (or (eq beg-function #'beginning-of-defun)
1789 (if (symbolp beg-function)
1790 (get beg-function 'font-lock-syntax-paren-check))))
1791 (not beginning-of-defun-function))
1792 ;; Try to detect when a string or comment contains something that
1793 ;; looks like a defun and would thus confuse font-lock.
1794 (nconc keywords
1795 `((,(if defun-prompt-regexp
1796 (concat "^\\(?:" defun-prompt-regexp "\\)?\\s(")
1797 "^\\s(")
1799 (if (memq (get-text-property (match-beginning 0) 'face)
1800 '(font-lock-string-face font-lock-doc-face
1801 font-lock-comment-face))
1802 (list 'face font-lock-warning-face
1803 'help-echo "Looks like a toplevel defun: escape the parenthesis"))
1804 prepend)))))
1805 keywords))
1807 (defun font-lock-compile-keyword (keyword)
1808 (cond ((or (functionp keyword) (nlistp keyword)) ; MATCHER
1809 (list keyword '(0 font-lock-keyword-face)))
1810 ((eq (car keyword) 'eval) ; (eval . FORM)
1811 (font-lock-compile-keyword (eval (cdr keyword))))
1812 ((eq (car-safe (cdr keyword)) 'quote) ; (MATCHER . 'FORM)
1813 ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote.
1814 (if (symbolp (nth 2 keyword))
1815 (list (car keyword) (list 0 (cdr keyword)))
1816 (font-lock-compile-keyword (cons (car keyword) (nth 2 keyword)))))
1817 ((numberp (cdr keyword)) ; (MATCHER . MATCH)
1818 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
1819 ((symbolp (cdr keyword)) ; (MATCHER . FACENAME)
1820 (list (car keyword) (list 0 (cdr keyword))))
1821 ((nlistp (nth 1 keyword)) ; (MATCHER . HIGHLIGHT)
1822 (list (car keyword) (cdr keyword)))
1823 (t ; (MATCHER HIGHLIGHT ...)
1824 keyword)))
1826 (defun font-lock-eval-keywords (keywords)
1827 "Evaluate KEYWORDS if a function (funcall) or variable (eval) name."
1828 (if (listp keywords)
1829 keywords
1830 (font-lock-eval-keywords (if (fboundp keywords)
1831 (funcall keywords)
1832 (eval keywords)))))
1834 (defun font-lock-value-in-major-mode (values)
1835 "If VALUES is a list, use `major-mode' as a key and return the `assq' value.
1836 VALUES should then be an alist on the form ((MAJOR-MODE . VALUE) ...) where
1837 MAJOR-MODE may be t.
1838 If VALUES isn't a list, return VALUES."
1839 (if (consp values)
1840 (cdr (or (assq major-mode values) (assq t values)))
1841 values))
1843 (defun font-lock-choose-keywords (keywords level)
1844 "Return LEVELth element of KEYWORDS.
1845 A LEVEL of nil is equal to a LEVEL of 0, a LEVEL of t is equal to
1846 \(1- (length KEYWORDS))."
1847 (cond ((not (and (listp keywords) (symbolp (car keywords))))
1848 keywords)
1849 ((numberp level)
1850 (or (nth level keywords) (car (last keywords))))
1851 ((eq level t)
1852 (car (last keywords)))
1854 (car keywords))))
1856 (defun font-lock-refresh-defaults ()
1857 "Restart fontification in current buffer after recomputing from defaults.
1858 Recompute fontification variables using `font-lock-defaults' and
1859 `font-lock-maximum-decoration'. Then restart fontification.
1861 Use this function when you have changed any of the above
1862 variables directly.
1864 Note: This function will erase modifications done by
1865 `font-lock-add-keywords' or `font-lock-remove-keywords', but will
1866 preserve `hi-lock-mode' highlighting patterns."
1867 (font-lock-mode -1)
1868 (kill-local-variable 'font-lock-set-defaults)
1869 (font-lock-mode 1))
1871 (defvar font-lock-major-mode nil
1872 "Major mode for which the font-lock settings have been setup.")
1873 (make-variable-buffer-local 'font-lock-major-mode)
1875 (defun font-lock-set-defaults ()
1876 "Set fontification defaults appropriately for this mode.
1877 Sets various variables using `font-lock-defaults' and
1878 `font-lock-maximum-decoration'."
1879 ;; Set fontification defaults if not previously set for correct major mode.
1880 (unless (and font-lock-set-defaults
1881 (eq font-lock-major-mode major-mode))
1882 (setq font-lock-major-mode major-mode)
1883 (setq font-lock-set-defaults t)
1884 (let* ((defaults font-lock-defaults)
1885 (keywords
1886 (font-lock-choose-keywords (nth 0 defaults)
1887 (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1888 (local (cdr (assq major-mode font-lock-keywords-alist)))
1889 (removed-keywords
1890 (cdr-safe (assq major-mode font-lock-removed-keywords-alist))))
1891 ;; Syntactic fontification?
1892 (if (nth 1 defaults)
1893 (set (make-local-variable 'font-lock-keywords-only) t)
1894 (kill-local-variable 'font-lock-keywords-only))
1895 ;; Case fold during regexp fontification?
1896 (if (nth 2 defaults)
1897 (set (make-local-variable 'font-lock-keywords-case-fold-search) t)
1898 (kill-local-variable 'font-lock-keywords-case-fold-search))
1899 ;; Syntax table for regexp and syntactic fontification?
1900 (if (null (nth 3 defaults))
1901 (kill-local-variable 'font-lock-syntax-table)
1902 (set (make-local-variable 'font-lock-syntax-table)
1903 (copy-syntax-table (syntax-table)))
1904 (dolist (selem (nth 3 defaults))
1905 ;; The character to modify may be a single CHAR or a STRING.
1906 (let ((syntax (cdr selem)))
1907 (dolist (char (if (numberp (car selem))
1908 (list (car selem))
1909 (mapcar #'identity (car selem))))
1910 (modify-syntax-entry char syntax font-lock-syntax-table)))))
1911 ;; (nth 4 defaults) used to hold `font-lock-beginning-of-syntax-function',
1912 ;; but that was removed in 25.1, so if it's a cons cell, we assume that
1913 ;; it's part of the variable alist.
1914 ;; Variable alist?
1915 (dolist (x (nthcdr (if (consp (nth 4 defaults)) 4 5) defaults))
1916 (set (make-local-variable (car x)) (cdr x)))
1917 ;; Set up `font-lock-keywords' last because its value might depend
1918 ;; on other settings.
1919 (set (make-local-variable 'font-lock-keywords)
1920 (font-lock-eval-keywords keywords))
1921 ;; Local fontification?
1922 (while local
1923 (font-lock-add-keywords nil (car (car local)) (cdr (car local)))
1924 (setq local (cdr local)))
1925 (when removed-keywords
1926 (font-lock-remove-keywords nil removed-keywords))
1927 ;; Now compile the keywords.
1928 (unless (eq (car font-lock-keywords) t)
1929 (setq font-lock-keywords
1930 (font-lock-compile-keywords font-lock-keywords))))
1931 (font-lock-flush)))
1933 ;;; Color etc. support.
1935 ;; Note that `defface' will not overwrite any faces declared above via
1936 ;; `custom-declare-face'.
1937 (defface font-lock-comment-face
1938 '((((class grayscale) (background light))
1939 :foreground "DimGray" :weight bold :slant italic)
1940 (((class grayscale) (background dark))
1941 :foreground "LightGray" :weight bold :slant italic)
1942 (((class color) (min-colors 88) (background light))
1943 :foreground "Firebrick")
1944 (((class color) (min-colors 88) (background dark))
1945 :foreground "chocolate1")
1946 (((class color) (min-colors 16) (background light))
1947 :foreground "red")
1948 (((class color) (min-colors 16) (background dark))
1949 :foreground "red1")
1950 (((class color) (min-colors 8) (background light))
1951 :foreground "red")
1952 (((class color) (min-colors 8) (background dark))
1953 :foreground "yellow")
1954 (t :weight bold :slant italic))
1955 "Font Lock mode face used to highlight comments."
1956 :group 'font-lock-faces)
1958 (defface font-lock-comment-delimiter-face
1959 '((default :inherit font-lock-comment-face))
1960 "Font Lock mode face used to highlight comment delimiters."
1961 :group 'font-lock-faces)
1963 (defface font-lock-string-face
1964 '((((class grayscale) (background light)) :foreground "DimGray" :slant italic)
1965 (((class grayscale) (background dark)) :foreground "LightGray" :slant italic)
1966 (((class color) (min-colors 88) (background light)) :foreground "VioletRed4")
1967 (((class color) (min-colors 88) (background dark)) :foreground "LightSalmon")
1968 (((class color) (min-colors 16) (background light)) :foreground "RosyBrown")
1969 (((class color) (min-colors 16) (background dark)) :foreground "LightSalmon")
1970 (((class color) (min-colors 8)) :foreground "green")
1971 (t :slant italic))
1972 "Font Lock mode face used to highlight strings."
1973 :group 'font-lock-faces)
1975 (defface font-lock-doc-face
1976 '((t :inherit font-lock-string-face))
1977 "Font Lock mode face used to highlight documentation."
1978 :group 'font-lock-faces)
1980 (defface font-lock-keyword-face
1981 '((((class grayscale) (background light)) :foreground "LightGray" :weight bold)
1982 (((class grayscale) (background dark)) :foreground "DimGray" :weight bold)
1983 (((class color) (min-colors 88) (background light)) :foreground "Purple")
1984 (((class color) (min-colors 88) (background dark)) :foreground "Cyan1")
1985 (((class color) (min-colors 16) (background light)) :foreground "Purple")
1986 (((class color) (min-colors 16) (background dark)) :foreground "Cyan")
1987 (((class color) (min-colors 8)) :foreground "cyan" :weight bold)
1988 (t :weight bold))
1989 "Font Lock mode face used to highlight keywords."
1990 :group 'font-lock-faces)
1992 (defface font-lock-builtin-face
1993 '((((class grayscale) (background light)) :foreground "LightGray" :weight bold)
1994 (((class grayscale) (background dark)) :foreground "DimGray" :weight bold)
1995 (((class color) (min-colors 88) (background light)) :foreground "dark slate blue")
1996 (((class color) (min-colors 88) (background dark)) :foreground "LightSteelBlue")
1997 (((class color) (min-colors 16) (background light)) :foreground "Orchid")
1998 (((class color) (min-colors 16) (background dark)) :foreground "LightSteelBlue")
1999 (((class color) (min-colors 8)) :foreground "blue" :weight bold)
2000 (t :weight bold))
2001 "Font Lock mode face used to highlight builtins."
2002 :group 'font-lock-faces)
2004 (defface font-lock-function-name-face
2005 '((((class color) (min-colors 88) (background light)) :foreground "Blue1")
2006 (((class color) (min-colors 88) (background dark)) :foreground "LightSkyBlue")
2007 (((class color) (min-colors 16) (background light)) :foreground "Blue")
2008 (((class color) (min-colors 16) (background dark)) :foreground "LightSkyBlue")
2009 (((class color) (min-colors 8)) :foreground "blue" :weight bold)
2010 (t :inverse-video t :weight bold))
2011 "Font Lock mode face used to highlight function names."
2012 :group 'font-lock-faces)
2014 (defface font-lock-variable-name-face
2015 '((((class grayscale) (background light))
2016 :foreground "Gray90" :weight bold :slant italic)
2017 (((class grayscale) (background dark))
2018 :foreground "DimGray" :weight bold :slant italic)
2019 (((class color) (min-colors 88) (background light)) :foreground "sienna")
2020 (((class color) (min-colors 88) (background dark)) :foreground "LightGoldenrod")
2021 (((class color) (min-colors 16) (background light)) :foreground "DarkGoldenrod")
2022 (((class color) (min-colors 16) (background dark)) :foreground "LightGoldenrod")
2023 (((class color) (min-colors 8)) :foreground "yellow" :weight light)
2024 (t :weight bold :slant italic))
2025 "Font Lock mode face used to highlight variable names."
2026 :group 'font-lock-faces)
2028 (defface font-lock-type-face
2029 '((((class grayscale) (background light)) :foreground "Gray90" :weight bold)
2030 (((class grayscale) (background dark)) :foreground "DimGray" :weight bold)
2031 (((class color) (min-colors 88) (background light)) :foreground "ForestGreen")
2032 (((class color) (min-colors 88) (background dark)) :foreground "PaleGreen")
2033 (((class color) (min-colors 16) (background light)) :foreground "ForestGreen")
2034 (((class color) (min-colors 16) (background dark)) :foreground "PaleGreen")
2035 (((class color) (min-colors 8)) :foreground "green")
2036 (t :weight bold :underline t))
2037 "Font Lock mode face used to highlight type and classes."
2038 :group 'font-lock-faces)
2040 (defface font-lock-constant-face
2041 '((((class grayscale) (background light))
2042 :foreground "LightGray" :weight bold :underline t)
2043 (((class grayscale) (background dark))
2044 :foreground "Gray50" :weight bold :underline t)
2045 (((class color) (min-colors 88) (background light)) :foreground "dark cyan")
2046 (((class color) (min-colors 88) (background dark)) :foreground "Aquamarine")
2047 (((class color) (min-colors 16) (background light)) :foreground "CadetBlue")
2048 (((class color) (min-colors 16) (background dark)) :foreground "Aquamarine")
2049 (((class color) (min-colors 8)) :foreground "magenta")
2050 (t :weight bold :underline t))
2051 "Font Lock mode face used to highlight constants and labels."
2052 :group 'font-lock-faces)
2054 (defface font-lock-warning-face
2055 '((t :inherit error))
2056 "Font Lock mode face used to highlight warnings."
2057 :group 'font-lock-faces)
2059 (defface font-lock-negation-char-face
2060 '((t nil))
2061 "Font Lock mode face used to highlight easy to overlook negation."
2062 :group 'font-lock-faces)
2064 (defface font-lock-preprocessor-face
2065 '((t :inherit font-lock-builtin-face))
2066 "Font Lock mode face used to highlight preprocessor directives."
2067 :group 'font-lock-faces)
2069 (defface font-lock-regexp-grouping-backslash
2070 '((t :inherit bold))
2071 "Font Lock mode face for backslashes in Lisp regexp grouping constructs."
2072 :group 'font-lock-faces)
2074 (defface font-lock-regexp-grouping-construct
2075 '((t :inherit bold))
2076 "Font Lock mode face used to highlight grouping constructs in Lisp regexps."
2077 :group 'font-lock-faces)
2079 ;;; End of Color etc. support.
2081 ;;; Menu support.
2083 ;; This section of code is commented out because Emacs does not have real menu
2084 ;; buttons. (We can mimic them by putting "( ) " or "(X) " at the beginning of
2085 ;; the menu entry text, but with Xt it looks both ugly and embarrassingly
2086 ;; amateur.) If/When Emacs gets real menus buttons, put in menu-bar.el after
2087 ;; the entry for "Text Properties" something like:
2089 ;; (define-key menu-bar-edit-menu [font-lock]
2090 ;; (cons "Syntax Highlighting" font-lock-menu))
2092 ;; and remove a single ";" from the beginning of each line in the rest of this
2093 ;; section. Probably the mechanism for telling the menu code what are menu
2094 ;; buttons and when they are on or off needs tweaking. I have assumed that the
2095 ;; mechanism is via `menu-toggle' and `menu-selected' symbol properties. sm.
2097 ;;;;;###autoload
2098 ;;(progn
2099 ;; ;; Make the Font Lock menu.
2100 ;; (defvar font-lock-menu (make-sparse-keymap "Syntax Highlighting"))
2101 ;; ;; Add the menu items in reverse order.
2102 ;; (define-key font-lock-menu [fontify-less]
2103 ;; '("Less In Current Buffer" . font-lock-fontify-less))
2104 ;; (define-key font-lock-menu [fontify-more]
2105 ;; '("More In Current Buffer" . font-lock-fontify-more))
2106 ;; (define-key font-lock-menu [font-lock-sep]
2107 ;; '("--"))
2108 ;; (define-key font-lock-menu [font-lock-mode]
2109 ;; '("In Current Buffer" . font-lock-mode))
2110 ;; (define-key font-lock-menu [global-font-lock-mode]
2111 ;; '("In All Buffers" . global-font-lock-mode)))
2113 ;;;;;###autoload
2114 ;;(progn
2115 ;; ;; We put the appropriate `menu-enable' etc. symbol property values on when
2116 ;; ;; font-lock.el is loaded, so we don't need to autoload the three variables.
2117 ;; (put 'global-font-lock-mode 'menu-toggle t)
2118 ;; (put 'font-lock-mode 'menu-toggle t)
2119 ;; (put 'font-lock-fontify-more 'menu-enable '(identity))
2120 ;; (put 'font-lock-fontify-less 'menu-enable '(identity)))
2122 ;; ;; Put the appropriate symbol property values on now. See above.
2123 ;;(put 'global-font-lock-mode 'menu-selected 'global-font-lock-mode)
2124 ;;(put 'font-lock-mode 'menu-selected 'font-lock-mode)
2125 ;;(put 'font-lock-fontify-more 'menu-enable '(nth 2 font-lock-fontify-level))
2126 ;;(put 'font-lock-fontify-less 'menu-enable '(nth 1 font-lock-fontify-level))
2128 ;;(defvar font-lock-fontify-level nil) ; For less/more fontification.
2130 ;;(defun font-lock-fontify-level (level)
2131 ;; (let ((font-lock-maximum-decoration level))
2132 ;; (when font-lock-mode
2133 ;; (font-lock-mode))
2134 ;; (font-lock-mode)
2135 ;; (when font-lock-verbose
2136 ;; (message "Fontifying %s... level %d" (buffer-name) level))))
2138 ;;(defun font-lock-fontify-less ()
2139 ;; "Fontify the current buffer with less decoration.
2140 ;;See `font-lock-maximum-decoration'."
2141 ;; (interactive)
2142 ;; ;; Check in case we get called interactively.
2143 ;; (if (nth 1 font-lock-fontify-level)
2144 ;; (font-lock-fontify-level (1- (car font-lock-fontify-level)))
2145 ;; (error "No less decoration")))
2147 ;;(defun font-lock-fontify-more ()
2148 ;; "Fontify the current buffer with more decoration.
2149 ;;See `font-lock-maximum-decoration'."
2150 ;; (interactive)
2151 ;; ;; Check in case we get called interactively.
2152 ;; (if (nth 2 font-lock-fontify-level)
2153 ;; (font-lock-fontify-level (1+ (car font-lock-fontify-level)))
2154 ;; (error "No more decoration")))
2156 ;; ;; This should be called by `font-lock-set-defaults'.
2157 ;;(defun font-lock-set-menu ()
2158 ;; ;; Activate less/more fontification entries if there are multiple levels for
2159 ;; ;; the current buffer. Sets `font-lock-fontify-level' to be of the form
2160 ;; ;; (CURRENT-LEVEL IS-LOWER-LEVEL-P IS-HIGHER-LEVEL-P) for menu activation.
2161 ;; (let ((keywords (nth 0 font-lock-defaults))
2162 ;; (level (font-lock-value-in-major-mode font-lock-maximum-decoration)))
2163 ;; (make-local-variable 'font-lock-fontify-level)
2164 ;; (if (or (symbolp keywords) (= (length keywords) 1))
2165 ;; (font-lock-unset-menu)
2166 ;; (cond ((eq level t)
2167 ;; (setq level (1- (length keywords))))
2168 ;; ((or (null level) (zerop level))
2169 ;; ;; The default level is usually, but not necessarily, level 1.
2170 ;; (setq level (- (length keywords)
2171 ;; (length (member (eval (car keywords))
2172 ;; (mapcar #'eval (cdr keywords))))))))
2173 ;; (setq font-lock-fontify-level (list level (> level 1)
2174 ;; (< level (1- (length keywords))))))))
2176 ;; ;; This should be called by `font-lock-unset-defaults'.
2177 ;;(defun font-lock-unset-menu ()
2178 ;; ;; Deactivate less/more fontification entries.
2179 ;; (setq font-lock-fontify-level nil))
2181 ;;; End of Menu support.
2183 ;;; Various regexp information shared by several modes.
2184 ;; ;; Information specific to a single mode should go in its load library.
2186 ;; Font Lock support for C, C++, Objective-C and Java modes is now in
2187 ;; cc-fonts.el (and required by cc-mode.el). However, the below function
2188 ;; should stay in font-lock.el, since it is used by other libraries. sm.
2190 (defun font-lock-match-c-style-declaration-item-and-skip-to-next (limit)
2191 "Match, and move over, any declaration/definition item after point.
2192 Matches after point, but ignores leading whitespace and `*' characters.
2193 Does not move further than LIMIT.
2195 The expected syntax of a declaration/definition item is `word' (preceded by
2196 optional whitespace and `*' characters and proceeded by optional whitespace)
2197 optionally followed by a `('. Everything following the item (but belonging to
2198 it) is expected to be skip-able by `scan-sexps', and items are expected to be
2199 separated with a `,' and to be terminated with a `;'.
2201 Thus the regexp matches after point: word (
2202 ^^^^ ^
2203 Where the match subexpressions are: 1 2
2205 The item is delimited by (match-beginning 1) and (match-end 1).
2206 If (match-beginning 2) is non-nil, the item is followed by a `('.
2208 This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
2209 (when (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?")
2210 (when (and (match-end 2) (> (- (match-end 2) (match-beginning 2)) 1))
2211 ;; If `word' is followed by a double open-paren, it's probably
2212 ;; a macro used for "int myfun P_ ((int arg1))". Let's go back one
2213 ;; word to try and match `myfun' rather than `P_'.
2214 (let ((pos (point)))
2215 (skip-chars-backward " \t\n")
2216 (skip-syntax-backward "w")
2217 (unless (looking-at "\\(\\sw+\\)[ \t\n]*\\sw+[ \t\n]*\\(((?\\)?")
2218 ;; Looks like it was something else, so go back to where we
2219 ;; were and reset the match data by rematching.
2220 (goto-char pos)
2221 (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?"))))
2222 (save-match-data
2223 (condition-case nil
2224 (save-restriction
2225 ;; Restrict to the LIMIT.
2226 (narrow-to-region (point-min) limit)
2227 (goto-char (match-end 1))
2228 ;; Move over any item value, etc., to the next item.
2229 (while (not (looking-at "[ \t\n]*\\(\\(,\\)\\|;\\|\\'\\)"))
2230 (goto-char (or (scan-sexps (point) 1) (point-max))))
2231 (if (match-end 2)
2232 (goto-char (match-end 2))))
2233 (error t)))))
2235 ;; C preprocessor(cpp) is used outside of C, C++ and Objective-C source file.
2236 ;; e.g. assembler code and GNU linker script in Linux kernel.
2237 ;; `cpp-font-lock-keywords' is handy for modes for the files.
2239 ;; Here we cannot use `regexp-opt' because because regex-opt is not preloaded
2240 ;; while font-lock.el is preloaded to emacs. So values pre-calculated with
2241 ;; regexp-opt are used here.
2243 ;; `cpp-font-lock-keywords-source-directives' is calculated from:
2245 ;; (regexp-opt
2246 ;; '("define" "elif" "else" "endif" "error" "file" "if" "ifdef"
2247 ;; "ifndef" "import" "include" "line" "pragma" "undef" "warning"))
2249 (defconst cpp-font-lock-keywords-source-directives
2250 "define\\|e\\(?:l\\(?:if\\|se\\)\\|ndif\\|rror\\)\\|file\\|i\\(?:f\\(?:n?def\\)?\\|mport\\|nclude\\)\\|line\\|pragma\\|undef\\|warning"
2251 "Regular expression used in `cpp-font-lock-keywords'.")
2253 ;; `cpp-font-lock-keywords-source-depth' is calculated from:
2255 ;; (regexp-opt-depth (regexp-opt
2256 ;; '("define" "elif" "else" "endif" "error" "file" "if" "ifdef"
2257 ;; "ifndef" "import" "include" "line" "pragma" "undef" "warning")))
2259 (defconst cpp-font-lock-keywords-source-depth 0
2260 "An integer representing regular expression depth of `cpp-font-lock-keywords-source-directives'.
2261 Used in `cpp-font-lock-keywords'.")
2263 (defconst cpp-font-lock-keywords
2264 (let* ((directives cpp-font-lock-keywords-source-directives)
2265 (directives-depth cpp-font-lock-keywords-source-depth))
2266 (list
2268 ;; Fontify error directives.
2269 '("^#[ \t]*\\(?:error\\|warning\\)[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend)
2271 ;; Fontify filenames in #include <...> preprocessor directives as strings.
2272 '("^#[ \t]*\\(?:import\\|include\\)[ \t]*\\(<[^>\"\n]*>?\\)"
2273 1 font-lock-string-face prepend)
2275 ;; Fontify function macro names.
2276 '("^#[ \t]*define[ \t]+\\([[:alpha:]_][[:alnum:]_$]*\\)("
2277 (1 font-lock-function-name-face prepend)
2279 ;; Macro arguments.
2280 ((lambda (limit)
2281 (re-search-forward
2282 "\\(?:\\([[:alpha:]_][[:alnum:]_]*\\)[,]?\\)"
2283 (or (save-excursion (re-search-forward ")" limit t))
2284 limit)
2286 nil nil (1 font-lock-variable-name-face prepend)))
2288 ;; Fontify symbol names in #elif or #if ... defined preprocessor directives.
2289 '("^#[ \t]*\\(?:elif\\|if\\)\\>"
2290 ("\\<\\(defined\\)\\>[ \t]*(?\\([[:alpha:]_][[:alnum:]_]*\\)?" nil nil
2291 (1 font-lock-builtin-face prepend) (2 font-lock-variable-name-face prepend t)))
2293 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
2294 (list
2295 (concat "^\\(#[ \t]*\\(?:" directives
2296 "\\)\\)\\>[ \t!]*\\([[:alpha:]_][[:alnum:]_]*\\)?")
2297 '(1 font-lock-preprocessor-face prepend)
2298 (list (+ 2 directives-depth)
2299 'font-lock-variable-name-face nil t))))
2300 "Font lock keywords for C preprocessor directives.
2301 `c-mode', `c++-mode' and `objc-mode' have their own font lock keywords
2302 for C preprocessor directives. This definition is for the other modes
2303 in which C preprocessor directives are used. e.g. `asm-mode' and
2304 `ld-script-mode'.")
2306 (provide 'font-lock)
2308 ;;; font-lock.el ends here