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