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