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