Commit | Line | Data |
---|---|---|
876f2438 SM |
1 | ;;; font-lock.el --- Electric font lock mode |
2 | ||
f9261020 | 3 | ;; Copyright (C) 1992, 93, 94, 95, 96, 97, 98, 1999, 2000, 2001 |
01b69148 | 4 | ;; Free Software Foundation, Inc. |
030f4a35 | 5 | |
0279f991 | 6 | ;; Author: jwz, then rms, then sm |
030f4a35 RS |
7 | ;; Maintainer: FSF |
8 | ;; Keywords: languages, faces | |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
12 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation; either version 2, or (at your option) | |
15 | ;; any later version. | |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
24 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 | ;; Boston, MA 02111-1307, USA. | |
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 | ;; |
a2b8e66b | 40 | ;; To turn Font Lock mode on automatically, add this to your ~/.emacs 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 | |
70 | ;; necessary because Emacs' syntax table is not powerful enough to describe all | |
71 | ;; the different syntactic constructs required by the sort of people who decide | |
72 | ;; that a single quote can be syntactic or not depending on the time of day. | |
73 | ;; (What sort of person could decide to overload the meaning of a quote?) | |
74 | ;; Obviously the syntactic keyword pass must occur before the syntactic pass. | |
75 | ;; | |
76 | ;; The syntactic pass places `face' text properties in the buffer according to | |
77 | ;; syntactic context, i.e., according to the buffer's syntax table and buffer | |
78 | ;; text's `syntax-table' text properties. It involves using a syntax parsing | |
79 | ;; function to determine the context of different parts of a region of text. A | |
80 | ;; syntax parsing function is necessary because generally strings and/or | |
81 | ;; comments can span lines, and so the context of a given region is not | |
82 | ;; necessarily apparent from the content of that region. Because the keyword | |
83 | ;; pass only works within a given region, it is not generally appropriate for | |
84 | ;; syntactic fontification. This is the first fontification pass that makes | |
85 | ;; changes visible to the user; it fontifies strings and comments. | |
86 | ;; | |
87 | ;; The keyword pass places `face' text properties in the buffer according to | |
88 | ;; the variable `font-lock-keywords'. It involves searching for given regexps | |
89 | ;; (or calling given search functions) within the given region. This is the | |
90 | ;; second fontification pass that makes changes visible to the user; it | |
91 | ;; fontifies language reserved words, etc. | |
92 | ;; | |
93 | ;; Oh, and the answer is, "Yes, obviously just about everything should be done | |
94 | ;; in a single syntactic pass, but the only syntactic parser available | |
95 | ;; understands only strings and comments." Perhaps one day someone will write | |
96 | ;; some syntactic parsers for common languages and a son-of-font-lock.el could | |
97 | ;; use them rather then relying so heavily on the keyword (regexp) pass. | |
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 | |
103 | ;; variable names from (a) the buffer local variable `font-lock-defaults', if | |
104 | ;; non-nil, or (b) the global variable `font-lock-defaults-alist', if the major | |
105 | ;; mode has an entry. (Font Lock mode is set up via (a) where a mode's | |
106 | ;; patterns are distributed with the mode's package library, and (b) where a | |
107 | ;; mode's patterns are distributed with font-lock.el itself. An example of (a) | |
108 | ;; is Pascal mode, an example of (b) is Lisp mode. Normally, the mechanism is | |
109 | ;; (a); (b) is used where it is not clear which package library should contain | |
110 | ;; the pattern definitions.) Font Lock mode chooses which variable to use for | |
111 | ;; fontification based on `font-lock-maximum-decoration'. | |
56fcbd7e SM |
112 | ;; |
113 | ;; Font Lock mode fontification behaviour can be modified in a number of ways. | |
114 | ;; See the below comments and the comments distributed throughout this file. | |
c1f2ffc8 SM |
115 | |
116 | ;;; Constructing patterns: | |
117 | ||
98f84f52 SM |
118 | ;; See the documentation for the variable `font-lock-keywords'. |
119 | ;; | |
ac6e572c SM |
120 | ;; Efficient regexps for use as MATCHERs for `font-lock-keywords' and |
121 | ;; `font-lock-syntactic-keywords' can be generated via the function | |
afa18a4e | 122 | ;; `regexp-opt'. |
98f84f52 | 123 | |
c1f2ffc8 SM |
124 | ;;; Adding patterns for modes that already support Font Lock: |
125 | ||
126 | ;; Though Font Lock highlighting patterns already exist for many modes, it's | |
127 | ;; likely there's something that you want fontified that currently isn't, even | |
128 | ;; at the maximum fontification level. You can add highlighting patterns via | |
129 | ;; `font-lock-add-keywords'. For example, say in some C | |
130 | ;; header file you #define the token `and' to expand to `&&', etc., to make | |
131 | ;; your C code almost readable. In your ~/.emacs there could be: | |
98f84f52 | 132 | ;; |
c1f2ffc8 | 133 | ;; (font-lock-add-keywords 'c-mode '("\\<\\(and\\|or\\|not\\)\\>")) |
98f84f52 | 134 | ;; |
c1f2ffc8 SM |
135 | ;; Some modes provide specific ways to modify patterns based on the values of |
136 | ;; other variables. For example, additional C types can be specified via the | |
137 | ;; variable `c-font-lock-extra-types'. | |
138 | ||
139 | ;;; Adding patterns for modes that do not support Font Lock: | |
140 | ||
141 | ;; Not all modes support Font Lock mode. If you (as a user of the mode) add | |
142 | ;; patterns for a new mode, you must define in your ~/.emacs a variable or | |
143 | ;; variables that specify regexp fontification. Then, you should indicate to | |
144 | ;; Font Lock mode, via the mode hook setting `font-lock-defaults', exactly what | |
145 | ;; support is required. For example, say Foo mode should have the following | |
146 | ;; regexps fontified case-sensitively, and comments and strings should not be | |
147 | ;; fontified automagically. In your ~/.emacs there could be: | |
98f84f52 | 148 | ;; |
c1f2ffc8 SM |
149 | ;; (defvar foo-font-lock-keywords |
150 | ;; '(("\\<\\(one\\|two\\|three\\)\\>" . font-lock-keyword-face) | |
151 | ;; ("\\<\\(four\\|five\\|six\\)\\>" . font-lock-type-face)) | |
152 | ;; "Default expressions to highlight in Foo mode.") | |
98f84f52 | 153 | ;; |
c1f2ffc8 SM |
154 | ;; (add-hook 'foo-mode-hook |
155 | ;; (function (lambda () | |
156 | ;; (make-local-variable 'font-lock-defaults) | |
157 | ;; (setq font-lock-defaults '(foo-font-lock-keywords t))))) | |
158 | ||
159 | ;;; Adding Font Lock support for modes: | |
160 | ||
161 | ;; Of course, it would be better that the mode already supports Font Lock mode. | |
162 | ;; The package author would do something similar to above. The mode must | |
163 | ;; define at the top-level a variable or variables that specify regexp | |
164 | ;; fontification. Then, the mode command should indicate to Font Lock mode, | |
165 | ;; via `font-lock-defaults', exactly what support is required. For example, | |
166 | ;; say Bar mode should have the following regexps fontified case-insensitively, | |
167 | ;; and comments and strings should be fontified automagically. In bar.el there | |
168 | ;; could be: | |
98f84f52 | 169 | ;; |
c1f2ffc8 SM |
170 | ;; (defvar bar-font-lock-keywords |
171 | ;; '(("\\<\\(uno\\|due\\|tre\\)\\>" . font-lock-keyword-face) | |
172 | ;; ("\\<\\(quattro\\|cinque\\|sei\\)\\>" . font-lock-type-face)) | |
173 | ;; "Default expressions to highlight in Bar mode.") | |
98f84f52 | 174 | ;; |
c1f2ffc8 | 175 | ;; and within `bar-mode' there could be: |
98f84f52 | 176 | ;; |
c1f2ffc8 SM |
177 | ;; (make-local-variable 'font-lock-defaults) |
178 | ;; (setq font-lock-defaults '(bar-font-lock-keywords nil t)) | |
98f84f52 | 179 | \f |
9bfbb130 SM |
180 | ;; What is fontification for? You might say, "It's to make my code look nice." |
181 | ;; I think it should be for adding information in the form of cues. These cues | |
182 | ;; should provide you with enough information to both (a) distinguish between | |
183 | ;; different items, and (b) identify the item meanings, without having to read | |
184 | ;; the items and think about it. Therefore, fontification allows you to think | |
185 | ;; less about, say, the structure of code, and more about, say, why the code | |
186 | ;; doesn't work. Or maybe it allows you to think less and drift off to sleep. | |
187 | ;; | |
188 | ;; So, here are my opinions/advice/guidelines: | |
5e9e032a | 189 | ;; |
7d7d915a SM |
190 | ;; - Highlight conceptual objects, such as function and variable names, and |
191 | ;; different objects types differently, i.e., (a) and (b) above, highlight | |
192 | ;; function names differently to variable names. | |
193 | ;; - Keep the faces distinct from each other as far as possible. | |
194 | ;; i.e., (a) above. | |
9bfbb130 SM |
195 | ;; - Use the same face for the same conceptual object, across all modes. |
196 | ;; i.e., (b) above, all modes that have items that can be thought of as, say, | |
197 | ;; keywords, should be highlighted with the same face, etc. | |
9bfbb130 SM |
198 | ;; - Make the face attributes fit the concept as far as possible. |
199 | ;; i.e., function names might be a bold colour such as blue, comments might | |
200 | ;; be a bright colour such as red, character strings might be brown, because, | |
201 | ;; err, strings are brown (that was not the reason, please believe me). | |
202 | ;; - Don't use a non-nil OVERRIDE unless you have a good reason. | |
203 | ;; Only use OVERRIDE for special things that are easy to define, such as the | |
204 | ;; way `...' quotes are treated in strings and comments in Emacs Lisp mode. | |
205 | ;; Don't use it to, say, highlight keywords in commented out code or strings. | |
206 | ;; - Err, that's it. | |
a1eb1cf1 | 207 | \f |
2e6a15fc SM |
208 | ;;; Code: |
209 | ||
8259bf10 SM |
210 | (require 'syntax) |
211 | ||
2273c36b | 212 | ;; Define core `font-lock' group. |
55015061 SM |
213 | (defgroup font-lock nil |
214 | "Font Lock mode text highlighting package." | |
215 | :link '(custom-manual "(emacs)Font Lock") | |
63e20d60 | 216 | :link '(custom-manual "(elisp)Font Lock Mode") |
55015061 SM |
217 | :group 'faces) |
218 | ||
2273c36b SM |
219 | (defgroup font-lock-highlighting-faces nil |
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) | |
227 | ||
ac6e572c | 228 | ;; Define support mode groups here to impose `font-lock' group order. |
2273c36b SM |
229 | (defgroup fast-lock nil |
230 | "Font Lock support mode to cache fontification." | |
231 | :link '(custom-manual "(emacs)Support Modes") | |
232 | :load 'fast-lock | |
233 | :group 'font-lock) | |
234 | ||
235 | (defgroup lazy-lock nil | |
236 | "Font Lock support mode to fontify lazily." | |
237 | :link '(custom-manual "(emacs)Support Modes") | |
238 | :load 'lazy-lock | |
55015061 | 239 | :group 'font-lock) |
e7f07c2c GM |
240 | |
241 | (defgroup jit-lock nil | |
242 | "Font Lock support mode to fontify just-in-time." | |
243 | :link '(custom-manual "(emacs)Support Modes") | |
63e20d60 | 244 | :version "21.1" |
faa57c0a | 245 | :load 'jit-lock |
e7f07c2c | 246 | :group 'font-lock) |
55015061 | 247 | \f |
9bfbb130 SM |
248 | ;; User variables. |
249 | ||
4fffc071 | 250 | (defcustom font-lock-maximum-size 256000 |
23a0e11b SM |
251 | "*Maximum size of a buffer for buffer fontification. |
252 | Only buffers less than this can be fontified when Font Lock mode is turned on. | |
253 | If nil, means size is irrelevant. | |
254 | If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE), | |
255 | where MAJOR-MODE is a symbol or t (meaning the default). For example: | |
256 | ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576)) | |
257 | means that the maximum size is 250K for buffers in C or C++ modes, one megabyte | |
258 | for buffers in Rmail mode, and size is irrelevant otherwise." | |
259 | :type '(choice (const :tag "none" nil) | |
260 | (integer :tag "size") | |
261 | (repeat :menu-tag "mode specific" :tag "mode specific" | |
262 | :value ((t . nil)) | |
263 | (cons :tag "Instance" | |
264 | (radio :tag "Mode" | |
265 | (const :tag "all" t) | |
266 | (symbol :tag "name")) | |
267 | (radio :tag "Size" | |
268 | (const :tag "none" nil) | |
269 | (integer :tag "size"))))) | |
55015061 | 270 | :group 'font-lock) |
9bfbb130 | 271 | |
55015061 | 272 | (defcustom font-lock-maximum-decoration t |
2d9cdda8 | 273 | "*Maximum decoration level for fontification. |
9bfbb130 SM |
274 | If nil, use the default decoration (typically the minimum available). |
275 | If t, use the maximum decoration available. | |
276 | If a number, use that level of decoration (or if not available the maximum). | |
277 | If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL), | |
278 | where MAJOR-MODE is a symbol or t (meaning the default). For example: | |
5aa29679 RS |
279 | ((c-mode . t) (c++-mode . 2) (t . 1)) |
280 | means use the maximum decoration available for buffers in C mode, level 2 | |
55015061 | 281 | decoration for buffers in C++ mode, and level 1 decoration otherwise." |
b34aa0a3 SM |
282 | :type '(choice (const :tag "default" nil) |
283 | (const :tag "maximum" t) | |
284 | (integer :tag "level" 1) | |
285 | (repeat :menu-tag "mode specific" :tag "mode specific" | |
286 | :value ((t . t)) | |
287 | (cons :tag "Instance" | |
288 | (radio :tag "Mode" | |
289 | (const :tag "all" t) | |
290 | (symbol :tag "name")) | |
291 | (radio :tag "Decoration" | |
292 | (const :tag "default" nil) | |
019f00d8 | 293 | (const :tag "maximum" t) |
b34aa0a3 | 294 | (integer :tag "level" 1))))) |
55015061 SM |
295 | :group 'font-lock) |
296 | ||
4fffc071 | 297 | (defcustom font-lock-verbose 0 |
23a0e11b SM |
298 | "*If non-nil, means show status messages for buffer fontification. |
299 | If a number, only buffers greater than this size have fontification messages." | |
300 | :type '(choice (const :tag "never" nil) | |
4fffc071 SM |
301 | (other :tag "always" t) |
302 | (integer :tag "size")) | |
55015061 | 303 | :group 'font-lock) |
9bfbb130 | 304 | \f |
1c172af4 SM |
305 | |
306 | ;; Originally these variable values were face names such as `bold' etc. | |
307 | ;; Now we create our own faces, but we keep these variables for compatibility | |
308 | ;; and they give users another mechanism for changing face appearance. | |
309 | ;; We now allow a FACENAME in `font-lock-keywords' to be any expression that | |
310 | ;; returns a face. So the easiest thing is to continue using these variables, | |
311 | ;; rather than sometimes evaling FACENAME and sometimes not. sm. | |
312 | (defvar font-lock-comment-face 'font-lock-comment-face | |
313 | "Face name to use for comments.") | |
314 | ||
315 | (defvar font-lock-string-face 'font-lock-string-face | |
316 | "Face name to use for strings.") | |
317 | ||
318 | (defvar font-lock-doc-face 'font-lock-doc-face | |
319 | "Face name to use for documentation.") | |
320 | ||
321 | (defvar font-lock-keyword-face 'font-lock-keyword-face | |
322 | "Face name to use for keywords.") | |
323 | ||
324 | (defvar font-lock-builtin-face 'font-lock-builtin-face | |
325 | "Face name to use for builtins.") | |
326 | ||
327 | (defvar font-lock-function-name-face 'font-lock-function-name-face | |
328 | "Face name to use for function names.") | |
329 | ||
330 | (defvar font-lock-variable-name-face 'font-lock-variable-name-face | |
331 | "Face name to use for variable names.") | |
332 | ||
333 | (defvar font-lock-type-face 'font-lock-type-face | |
334 | "Face name to use for type and class names.") | |
335 | ||
336 | (defvar font-lock-constant-face 'font-lock-constant-face | |
337 | "Face name to use for constant and label names.") | |
338 | ||
339 | (defvar font-lock-warning-face 'font-lock-warning-face | |
340 | "Face name to use for things that should stand out.") | |
341 | ||
342 | (defvar font-lock-reference-face 'font-lock-constant-face | |
343 | "This variable is obsolete. Use `font-lock-constant-face'.") | |
344 | ||
9bfbb130 SM |
345 | ;; Fontification variables: |
346 | ||
030f4a35 | 347 | (defvar font-lock-keywords nil |
ac6e572c | 348 | "A list of the keywords to highlight. |
caf0dd71 | 349 | Each element should have one of these forms: |
a1eb1cf1 | 350 | |
826b2925 SM |
351 | MATCHER |
352 | (MATCHER . MATCH) | |
353 | (MATCHER . FACENAME) | |
354 | (MATCHER . HIGHLIGHT) | |
355 | (MATCHER HIGHLIGHT ...) | |
a078558d | 356 | (eval . FORM) |
a1eb1cf1 | 357 | |
558371ba SM |
358 | where MATCHER can be either the regexp to search for, or the function name to |
359 | call to make the search (called with one argument, the limit of the search) and | |
360 | return non-nil if it succeeds (and set `match-data' appropriately). | |
361 | MATCHER regexps can be generated via the function `regexp-opt'. | |
9bfbb130 | 362 | |
a078558d SM |
363 | FORM is an expression, whose value should be a keyword element, evaluated when |
364 | the keyword is (first) used in a buffer. This feature can be used to provide a | |
365 | keyword that can only be generated when Font Lock mode is actually turned on. | |
366 | ||
558371ba SM |
367 | HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED. |
368 | ||
9c8de95c SM |
369 | For highlighting single items, for example each instance of the word \"foo\", |
370 | typically only MATCH-HIGHLIGHT is required. | |
d9f7b2d3 | 371 | However, if an item or (typically) items are to be highlighted following the |
9c8de95c SM |
372 | instance of another item (the anchor), for example each instance of the |
373 | word \"bar\" following the word \"anchor\" then MATCH-ANCHORED may be required. | |
9bfbb130 SM |
374 | |
375 | MATCH-HIGHLIGHT should be of the form: | |
376 | ||
377 | (MATCH FACENAME OVERRIDE LAXMATCH) | |
378 | ||
558371ba SM |
379 | MATCH is the subexpression of MATCHER to be highlighted. FACENAME is an |
380 | expression whose value is the face name to use. Face default attributes | |
381 | can be modified via \\[customize]. Instead of a face, FACENAME can | |
382 | evaluate to a property list of the form (face VAL1 PROP2 VAL2 PROP3 VAL3 ...) | |
383 | in which case all the listed text-properties will be set rather than | |
384 | just `face'. In such a case, you will most likely want to put those | |
385 | properties in `font-lock-extra-managed-props' or to override | |
386 | `font-lock-unfontify-region-function'. | |
a1eb1cf1 | 387 | |
2d63aa67 | 388 | OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification can |
9bfbb130 SM |
389 | be overwritten. If `keep', only parts not already fontified are highlighted. |
390 | If `prepend' or `append', existing fontification is merged with the new, in | |
391 | which the new or existing fontification, respectively, takes precedence. | |
d9f7b2d3 | 392 | If LAXMATCH is non-nil, no error is signaled if there is no MATCH in MATCHER. |
a1eb1cf1 | 393 | |
9bfbb130 SM |
394 | For example, an element of the form highlights (if not already highlighted): |
395 | ||
9c8de95c | 396 | \"\\\\\\=<foo\\\\\\=>\" discrete occurrences of \"foo\" in the value of the |
876f2438 | 397 | variable `font-lock-keyword-face'. |
9c8de95c | 398 | (\"fu\\\\(bar\\\\)\" . 1) substring \"bar\" within all occurrences of \"fubar\" in |
9bfbb130 SM |
399 | the value of `font-lock-keyword-face'. |
400 | (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'. | |
401 | (\"foo\\\\|bar\" 0 foo-bar-face t) | |
9c8de95c | 402 | occurrences of either \"foo\" or \"bar\" in the value |
9bfbb130 | 403 | of `foo-bar-face', even if already highlighted. |
844a6a46 | 404 | (fubar-match 1 fubar-face) |
9c8de95c | 405 | the first subexpression within all occurrences of |
844a6a46 SM |
406 | whatever the function `fubar-match' finds and matches |
407 | in the value of `fubar-face'. | |
9bfbb130 SM |
408 | |
409 | MATCH-ANCHORED should be of the form: | |
410 | ||
411 | (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...) | |
412 | ||
9c8de95c SM |
413 | where MATCHER is a regexp to search for or the function name to call to make |
414 | the search, as for MATCH-HIGHLIGHT above, but with one exception; see below. | |
876f2438 SM |
415 | PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after |
416 | the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be | |
417 | used to initialise before, and cleanup after, MATCHER is used. Typically, | |
418 | PRE-MATCH-FORM is used to move to some position relative to the original | |
419 | MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might | |
420 | be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER. | |
9bfbb130 SM |
421 | |
422 | For example, an element of the form highlights (if not already highlighted): | |
423 | ||
876f2438 | 424 | (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face))) |
9bfbb130 | 425 | |
9c8de95c | 426 | discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent |
876f2438 SM |
427 | discrete occurrences of \"item\" (on the same line) in the value of `item-face'. |
428 | (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is | |
429 | initially searched for starting from the end of the match of \"anchor\", and | |
430 | searching for subsequent instance of \"anchor\" resumes from where searching | |
431 | for \"item\" concluded.) | |
9bfbb130 | 432 | |
56fcbd7e SM |
433 | The above-mentioned exception is as follows. The limit of the MATCHER search |
434 | defaults to the end of the line after PRE-MATCH-FORM is evaluated. | |
435 | However, if PRE-MATCH-FORM returns a position greater than the position after | |
436 | PRE-MATCH-FORM is evaluated, that position is used as the limit of the search. | |
437 | It is generally a bad idea to return a position greater than the end of the | |
438 | line, i.e., cause the MATCHER search to span lines. | |
439 | ||
6e1d0d15 SM |
440 | These regular expressions can match text which spans lines, although |
441 | it is better to avoid it if possible since updating them while editing | |
442 | text is slower, and it is not guaranteed to be always correct when using | |
443 | support modes like jit-lock or lazy-lock. | |
a1eb1cf1 | 444 | |
2d63aa67 SM |
445 | This variable is set by major modes via the variable `font-lock-defaults'. |
446 | Be careful when composing regexps for this list; a poorly written pattern can | |
447 | dramatically slow things down!") | |
030f4a35 | 448 | |
2e6a15fc SM |
449 | ;; This variable is used by mode packages that support Font Lock mode by |
450 | ;; defining their own keywords to use for `font-lock-keywords'. (The mode | |
451 | ;; command should make it buffer-local and set it to provide the set up.) | |
b89e1134 | 452 | (defvar font-lock-defaults nil |
2d63aa67 SM |
453 | "Defaults for Font Lock mode specified by the major mode. |
454 | Defaults should be of the form: | |
455 | ||
456 | (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN ...) | |
457 | ||
458 | KEYWORDS may be a symbol (a variable or function whose value is the keywords to | |
459 | use for fontification) or a list of symbols. If KEYWORDS-ONLY is non-nil, | |
460 | syntactic fontification (strings and comments) is not performed. | |
461 | If CASE-FOLD is non-nil, the case of the keywords is ignored when fontifying. | |
462 | If SYNTAX-ALIST is non-nil, it should be a list of cons pairs of the form | |
463 | \(CHAR-OR-STRING . STRING) used to set the local Font Lock syntax table, for | |
464 | keyword and syntactic fontification (see `modify-syntax-entry'). | |
465 | ||
466 | If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move | |
467 | backwards outside any enclosing syntactic block, for syntactic fontification. | |
468 | Typical values are `beginning-of-line' (i.e., the start of the line is known to | |
469 | be outside a syntactic block), or `beginning-of-defun' for programming modes or | |
470 | `backward-paragraph' for textual modes (i.e., the mode-dependent function is | |
471 | known to move outside a syntactic block). If nil, the beginning of the buffer | |
472 | is used as a position outside of a syntactic block, in the worst case. | |
473 | ||
474 | These item elements are used by Font Lock mode to set the variables | |
475 | `font-lock-keywords', `font-lock-keywords-only', | |
476 | `font-lock-keywords-case-fold-search', `font-lock-syntax-table' and | |
477 | `font-lock-beginning-of-syntax-function', respectively. | |
478 | ||
479 | Further item elements are alists of the form (VARIABLE . VALUE) and are in no | |
480 | particular order. Each VARIABLE is made buffer-local before set to VALUE. | |
481 | ||
482 | Currently, appropriate variables include `font-lock-mark-block-function'. | |
483 | If this is non-nil, it should be a function with no args used to mark any | |
484 | enclosing block of text, for fontification via \\[font-lock-fontify-block]. | |
485 | Typical values are `mark-defun' for programming modes or `mark-paragraph' for | |
486 | textual modes (i.e., the mode-dependent function is known to put point and mark | |
487 | around a text block relevant to that mode). | |
488 | ||
79f238c9 SM |
489 | Other variables include that for syntactic keyword fontification, |
490 | `font-lock-syntactic-keywords' | |
491 | and those for buffer-specialised fontification functions, | |
2d63aa67 SM |
492 | `font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function', |
493 | `font-lock-fontify-region-function', `font-lock-unfontify-region-function', | |
55015061 | 494 | `font-lock-inhibit-thing-lock' and `font-lock-maximum-size'.") |
fd612dd9 SM |
495 | ;;;###autoload |
496 | (make-variable-buffer-local 'font-lock-defaults) | |
b89e1134 | 497 | |
2e6a15fc | 498 | ;; This variable is used where font-lock.el itself supplies the keywords. |
b89e1134 | 499 | (defvar font-lock-defaults-alist |
2e6a15fc SM |
500 | (let (;; We use `beginning-of-defun', rather than nil, for SYNTAX-BEGIN. |
501 | ;; Thus the calculation of the cache is usually faster but not | |
502 | ;; infallible, so we risk mis-fontification. sm. | |
fb512de9 | 503 | (c-mode-defaults |
9bfbb130 SM |
504 | '((c-font-lock-keywords c-font-lock-keywords-1 |
505 | c-font-lock-keywords-2 c-font-lock-keywords-3) | |
a078558d | 506 | nil nil ((?_ . "w")) beginning-of-defun |
8259bf10 SM |
507 | (font-lock-syntactic-face-function |
508 | . c-font-lock-syntactic-face-function) | |
a078558d | 509 | (font-lock-mark-block-function . mark-defun))) |
fb512de9 | 510 | (c++-mode-defaults |
019f00d8 | 511 | '((c++-font-lock-keywords c++-font-lock-keywords-1 |
9bfbb130 | 512 | c++-font-lock-keywords-2 c++-font-lock-keywords-3) |
55015061 | 513 | nil nil ((?_ . "w")) beginning-of-defun |
8259bf10 SM |
514 | (font-lock-syntactic-face-function |
515 | . c-font-lock-syntactic-face-function) | |
a078558d | 516 | (font-lock-mark-block-function . mark-defun))) |
2e6a15fc SM |
517 | (objc-mode-defaults |
518 | '((objc-font-lock-keywords objc-font-lock-keywords-1 | |
519 | objc-font-lock-keywords-2 objc-font-lock-keywords-3) | |
520 | nil nil ((?_ . "w") (?$ . "w")) nil | |
8259bf10 SM |
521 | (font-lock-syntactic-face-function |
522 | . c-font-lock-syntactic-face-function) | |
2e6a15fc SM |
523 | (font-lock-mark-block-function . mark-defun))) |
524 | (java-mode-defaults | |
525 | '((java-font-lock-keywords java-font-lock-keywords-1 | |
526 | java-font-lock-keywords-2 java-font-lock-keywords-3) | |
68c67d1f | 527 | nil nil ((?_ . "w") (?$ . "w")) nil |
8259bf10 | 528 | (font-lock-syntactic-face-function |
9aa40401 | 529 | . java-font-lock-syntactic-face-function) |
28a53bc1 | 530 | (font-lock-mark-block-function . mark-defun)))) |
826b2925 | 531 | (list |
d46c21ec | 532 | (cons 'c-mode c-mode-defaults) |
2e6a15fc SM |
533 | (cons 'c++-mode c++-mode-defaults) |
534 | (cons 'objc-mode objc-mode-defaults) | |
8259bf10 | 535 | (cons 'java-mode java-mode-defaults))) |
2d63aa67 | 536 | "Alist of fall-back Font Lock defaults for major modes. |
1c172af4 SM |
537 | |
538 | This variable should not be used any more. | |
539 | Set the buffer-local `font-lock-keywords' in the major mode instead. | |
540 | ||
b89e1134 | 541 | Each item should be a list of the form: |
d46c21ec | 542 | |
2d63aa67 | 543 | (MAJOR-MODE . FONT-LOCK-DEFAULTS) |
a2b8e66b | 544 | |
2d63aa67 SM |
545 | where MAJOR-MODE is a symbol and FONT-LOCK-DEFAULTS is a list of default |
546 | settings. See the variable `font-lock-defaults', which takes precedence.") | |
1c172af4 | 547 | (make-obsolete-variable 'font-lock-defaults-alist 'font-lock-defaults) |
d46c21ec | 548 | |
c1f2ffc8 SM |
549 | (defvar font-lock-keywords-alist nil |
550 | "*Alist of `font-lock-keywords' local to a `major-mode'. | |
76f5e2af GM |
551 | This is normally set via `font-lock-add-keywords' and |
552 | `font-lock-remove-keywords'.") | |
553 | ||
554 | (defvar font-lock-removed-keywords-alist nil | |
555 | "*Alist of `font-lock-keywords' removed from `major-mode'. | |
556 | This is normally set via `font-lock-add-keywords' and | |
557 | `font-lock-remove-keywords'.") | |
c1f2ffc8 | 558 | |
9bfbb130 | 559 | (defvar font-lock-keywords-only nil |
d46c21ec SM |
560 | "*Non-nil means Font Lock should not fontify comments or strings. |
561 | This is normally set via `font-lock-defaults'.") | |
b89e1134 | 562 | |
030f4a35 | 563 | (defvar font-lock-keywords-case-fold-search nil |
d46c21ec SM |
564 | "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive. |
565 | This is normally set via `font-lock-defaults'.") | |
1087f198 | 566 | (make-variable-buffer-local 'font-lock-keywords-case-fold-search) |
030f4a35 | 567 | |
c0a6a9fe SM |
568 | (defvar font-lock-syntactically-fontified 0 |
569 | "Point up to which `font-lock-syntactic-keywords' has been applied. | |
570 | If nil, this is ignored, in which case the syntactic fontification may | |
571 | sometimes be slightly incorrect.") | |
572 | (make-variable-buffer-local 'font-lock-syntactically-fontified) | |
573 | ||
1c172af4 SM |
574 | (defvar font-lock-syntactic-face-function |
575 | (lambda (state) | |
576 | (if (nth 3 state) font-lock-string-face font-lock-comment-face)) | |
577 | "Function to determine which face to use when fontifying syntactically. | |
578 | The function is called with a single parameter (the state as returned by | |
579 | `parse-partial-sexp' at the beginning of the region to highlight) and | |
580 | should return a face.") | |
581 | ||
ac6e572c SM |
582 | (defvar font-lock-syntactic-keywords nil |
583 | "A list of the syntactic keywords to highlight. | |
584 | Can be the list or the name of a function or variable whose value is the list. | |
585 | See `font-lock-keywords' for a description of the form of this list; | |
586 | the differences are listed below. MATCH-HIGHLIGHT should be of the form: | |
587 | ||
588 | (MATCH SYNTAX OVERRIDE LAXMATCH) | |
589 | ||
5299cb8e SM |
590 | where SYNTAX can be a string (as taken by `modify-syntax-entry'), a syntax |
591 | table, a cons cell (as returned by `string-to-syntax') or an expression whose | |
592 | value is such a form. OVERRIDE cannot be `prepend' or `append'. | |
ac6e572c | 593 | |
9c8de95c SM |
594 | For example, an element of the form highlights syntactically: |
595 | ||
5299cb8e | 596 | (\"\\\\$\\\\(#\\\\)\" 1 \".\") |
9c8de95c | 597 | |
5299cb8e SM |
598 | a hash character when following a dollar character, with a SYNTAX of |
599 | \".\" (meaning punctuation syntax). Assuming that the buffer syntax table does | |
9c8de95c SM |
600 | specify hash characters to have comment start syntax, the element will only |
601 | highlight hash characters that do not follow dollar characters as comments | |
602 | syntactically. | |
603 | ||
604 | (\"\\\\('\\\\).\\\\('\\\\)\" | |
5299cb8e SM |
605 | (1 \"\\\"\") |
606 | (2 \"\\\"\")) | |
9c8de95c | 607 | |
5299cb8e SM |
608 | both single quotes which surround a single character, with a SYNTAX of |
609 | \"\\\"\" (meaning string quote syntax). Assuming that the buffer syntax table | |
9c8de95c SM |
610 | does not specify single quotes to have quote syntax, the element will only |
611 | highlight single quotes of the form 'c' as strings syntactically. | |
612 | Other forms, such as foo'bar or 'fubar', will not be highlighted as strings. | |
613 | ||
ac6e572c SM |
614 | This is normally set via `font-lock-defaults'.") |
615 | ||
b056da51 | 616 | (defvar font-lock-syntax-table nil |
799761f0 | 617 | "Non-nil means use this syntax table for fontifying. |
d46c21ec SM |
618 | If this is nil, the major mode's syntax table is used. |
619 | This is normally set via `font-lock-defaults'.") | |
620 | ||
9bfbb130 SM |
621 | ;; If this is nil, we only use the beginning of the buffer if we can't use |
622 | ;; `font-lock-cache-position' and `font-lock-cache-state'. | |
d46c21ec | 623 | (defvar font-lock-beginning-of-syntax-function nil |
9bfbb130 | 624 | "*Non-nil means use this function to move back outside of a syntactic block. |
a078558d SM |
625 | When called with no args it should leave point at the beginning of any |
626 | enclosing syntactic block. | |
9bfbb130 | 627 | If this is nil, the beginning of the buffer is used (in the worst case). |
d46c21ec | 628 | This is normally set via `font-lock-defaults'.") |
b056da51 | 629 | |
a078558d SM |
630 | (defvar font-lock-mark-block-function nil |
631 | "*Non-nil means use this function to mark a block of text. | |
632 | When called with no args it should leave point at the beginning of any | |
633 | enclosing textual block and mark at the end. | |
634 | This is normally set via `font-lock-defaults'.") | |
635 | ||
a2b8e66b SM |
636 | (defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer |
637 | "Function to use for fontifying the buffer. | |
638 | This is normally set via `font-lock-defaults'.") | |
639 | ||
640 | (defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer | |
641 | "Function to use for unfontifying the buffer. | |
642 | This is used when turning off Font Lock mode. | |
643 | This is normally set via `font-lock-defaults'.") | |
644 | ||
645 | (defvar font-lock-fontify-region-function 'font-lock-default-fontify-region | |
646 | "Function to use for fontifying a region. | |
647 | It should take two args, the beginning and end of the region, and an optional | |
648 | third arg VERBOSE. If non-nil, the function should print status messages. | |
649 | This is normally set via `font-lock-defaults'.") | |
650 | ||
651 | (defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region | |
652 | "Function to use for unfontifying a region. | |
653 | It should take two args, the beginning and end of the region. | |
654 | This is normally set via `font-lock-defaults'.") | |
655 | ||
656 | (defvar font-lock-inhibit-thing-lock nil | |
657 | "List of Font Lock mode related modes that should not be turned on. | |
019f00d8 | 658 | Currently, valid mode names are `fast-lock-mode', `jit-lock-mode' and |
e7f07c2c | 659 | `lazy-lock-mode'. This is normally set via `font-lock-defaults'.") |
a2b8e66b | 660 | |
f9261020 | 661 | (defvar font-lock-multiline nil |
041470d2 SM |
662 | "Whether font-lock should cater to multiline keywords. |
663 | If nil, don't try to handle multiline patterns. | |
664 | If t, always handle multiline patterns. | |
665 | If `undecided', don't try to handle multiline patterns until you see one. | |
666 | Major/minor modes can set this variable if they know which option applies.") | |
667 | ||
9bfbb130 | 668 | (defvar font-lock-fontified nil) ; Whether we have fontified the buffer. |
030f4a35 | 669 | \f |
5aa29679 RS |
670 | ;; Font Lock mode. |
671 | ||
672 | (eval-when-compile | |
2e6a15fc | 673 | ;; |
5aa29679 | 674 | ;; We don't do this at the top-level as we only use non-autoloaded macros. |
2e6a15fc SM |
675 | (require 'cl) |
676 | ;; | |
677 | ;; Borrowed from lazy-lock.el. | |
678 | ;; We use this to preserve or protect things when modifying text properties. | |
679 | (defmacro save-buffer-state (varlist &rest body) | |
680 | "Bind variables according to VARLIST and eval BODY restoring buffer state." | |
d2251bbf SM |
681 | `(let* ,(append varlist |
682 | '((modified (buffer-modified-p)) (buffer-undo-list t) | |
683 | (inhibit-read-only t) (inhibit-point-motion-hooks t) | |
15d1caef | 684 | (inhibit-modification-hooks t) |
d2251bbf SM |
685 | deactivate-mark buffer-file-name buffer-file-truename)) |
686 | ,@body | |
687 | (when (and (not modified) (buffer-modified-p)) | |
688 | (set-buffer-modified-p nil)))) | |
e0e277ff | 689 | (put 'save-buffer-state 'lisp-indent-function 1) |
afa18a4e | 690 | (def-edebug-spec save-buffer-state let) |
e0e277ff SM |
691 | ;; |
692 | ;; Shut up the byte compiler. | |
1c172af4 | 693 | (defvar font-lock-face-attributes)) ; Obsolete but respected if set. |
030f4a35 RS |
694 | |
695 | ;;;###autoload | |
1c172af4 | 696 | (define-minor-mode font-lock-mode |
e595fa35 | 697 | "Toggle Font Lock mode. |
22cd6fd3 EZ |
698 | With arg, turn Font Lock mode off if and only if arg is a non-positive |
699 | number; if arg is nil, toggle Font Lock mode; anything else turns Font | |
700 | Lock on. | |
c0a6a9fe | 701 | \(Font Lock is also known as \"syntax highlighting\".) |
030f4a35 RS |
702 | |
703 | When Font Lock mode is enabled, text is fontified as you type it: | |
704 | ||
a1eb1cf1 RS |
705 | - Comments are displayed in `font-lock-comment-face'; |
706 | - Strings are displayed in `font-lock-string-face'; | |
707 | - Certain other expressions are displayed in other faces according to the | |
708 | value of the variable `font-lock-keywords'. | |
709 | ||
3b967bed EZ |
710 | To customize the faces (colors, fonts, etc.) used by Font Lock for |
711 | fontifying different parts of buffer text, use \\[customize-face]. | |
712 | ||
a1eb1cf1 RS |
713 | You can enable Font Lock mode in any major mode automatically by turning on in |
714 | the major mode's hook. For example, put in your ~/.emacs: | |
715 | ||
716 | (add-hook 'c-mode-hook 'turn-on-font-lock) | |
717 | ||
a2b8e66b | 718 | Alternatively, you can use Global Font Lock mode to automagically turn on Font |
fd23afbe SM |
719 | Lock mode in buffers whose major mode supports it and whose major mode is one |
720 | of `font-lock-global-modes'. For example, put in your ~/.emacs: | |
a2b8e66b SM |
721 | |
722 | (global-font-lock-mode t) | |
723 | ||
fd23afbe SM |
724 | There are a number of support modes that may be used to speed up Font Lock mode |
725 | in various ways, specified via the variable `font-lock-support-mode'. Where | |
726 | major modes support different levels of fontification, you can use the variable | |
fb512de9 | 727 | `font-lock-maximum-decoration' to specify which level you generally prefer. |
b89e1134 SM |
728 | When you turn Font Lock mode on/off the buffer is fontified/defontified, though |
729 | fontification occurs only if the buffer is less than `font-lock-maximum-size'. | |
7d7d915a | 730 | |
fd23afbe SM |
731 | For example, to specify that Font Lock mode use use Lazy Lock mode as a support |
732 | mode and use maximum levels of fontification, put in your ~/.emacs: | |
733 | ||
734 | (setq font-lock-support-mode 'lazy-lock-mode) | |
735 | (setq font-lock-maximum-decoration t) | |
736 | ||
56fcbd7e SM |
737 | To add your own highlighting for some major mode, and modify the highlighting |
738 | selected automatically via the variable `font-lock-maximum-decoration', you can | |
739 | use `font-lock-add-keywords'. | |
740 | ||
7d7d915a SM |
741 | To fontify a buffer, without turning on Font Lock mode and regardless of buffer |
742 | size, you can use \\[font-lock-fontify-buffer]. | |
a078558d SM |
743 | |
744 | To fontify a block (the function or paragraph containing point, or a number of | |
745 | lines around point), perhaps because modification on the current line caused | |
fd23afbe SM |
746 | syntactic change on other lines, you can use \\[font-lock-fontify-block]. |
747 | ||
2273c36b SM |
748 | See the variable `font-lock-defaults-alist' for the Font Lock mode default |
749 | settings. You can set your own default settings for some mode, by setting a | |
750 | buffer local value for `font-lock-defaults', via its mode hook." | |
1c172af4 | 751 | nil nil nil |
a2b8e66b SM |
752 | ;; Don't turn on Font Lock mode if we don't have a display (we're running a |
753 | ;; batch job) or if the buffer is invisible (the name starts with a space). | |
1c172af4 SM |
754 | (when (or noninteractive (eq (aref (buffer-name) 0) ?\ )) |
755 | (setq font-lock-mode nil)) | |
756 | ||
757 | ;; Turn on Font Lock mode. | |
758 | (when font-lock-mode | |
759 | (add-hook 'after-change-functions 'font-lock-after-change-function nil t) | |
760 | (font-lock-set-defaults) | |
761 | (font-lock-turn-on-thing-lock) | |
762 | ;; Fontify the buffer if we have to. | |
763 | (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size))) | |
764 | (cond (font-lock-fontified | |
765 | nil) | |
766 | ((or (null max-size) (> max-size (buffer-size))) | |
767 | (font-lock-fontify-buffer)) | |
768 | (font-lock-verbose | |
769 | (message "Fontifying %s...buffer size greater than font-lock-maximum-size" | |
770 | (buffer-name)))))) | |
771 | ;; Turn off Font Lock mode. | |
772 | (unless font-lock-mode | |
773 | (remove-hook 'after-change-functions 'font-lock-after-change-function t) | |
774 | (font-lock-unfontify-buffer) | |
8259bf10 | 775 | (font-lock-turn-off-thing-lock))) |
030f4a35 | 776 | |
a1eb1cf1 RS |
777 | ;;;###autoload |
778 | (defun turn-on-font-lock () | |
1c172af4 | 779 | "Turn on Font Lock mode (only if the terminal can display it)." |
01b69148 | 780 | (unless font-lock-mode |
56fcbd7e | 781 | (font-lock-mode))) |
c1f2ffc8 SM |
782 | |
783 | ;;;###autoload | |
041470d2 SM |
784 | (defun font-lock-add-keywords (mode keywords &optional append) |
785 | "Add highlighting KEYWORDS for MODE. | |
786 | MODE should be a symbol, the major mode command name, such as `c-mode' | |
1c626aaf | 787 | or nil. If nil, highlighting keywords are added for the current buffer. |
c1f2ffc8 SM |
788 | KEYWORDS should be a list; see the variable `font-lock-keywords'. |
789 | By default they are added at the beginning of the current highlighting list. | |
790 | If optional argument APPEND is `set', they are used to replace the current | |
2d63aa67 SM |
791 | highlighting list. If APPEND is any other non-nil value, they are added at the |
792 | end of the current highlighting list. | |
c1f2ffc8 SM |
793 | |
794 | For example: | |
795 | ||
796 | (font-lock-add-keywords 'c-mode | |
797 | '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend) | |
798 | (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . font-lock-keyword-face))) | |
799 | ||
800 | adds two fontification patterns for C mode, to fontify `FIXME:' words, even in | |
2d63aa67 SM |
801 | comments, and to fontify `and', `or' and `not' words as keywords. |
802 | ||
558371ba SM |
803 | When used from an elisp package (such as a minor mode), it is recommended |
804 | to use nil for MODE (and place the call in a loop or on a hook) to avoid | |
805 | subtle problems due to details of the implementation. | |
806 | ||
2d63aa67 SM |
807 | Note that some modes have specialised support for additional patterns, e.g., |
808 | see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types', | |
809 | `objc-font-lock-extra-types' and `java-font-lock-extra-types'." | |
041470d2 SM |
810 | (cond (mode |
811 | ;; If MODE is non-nil, add the KEYWORDS and APPEND spec to | |
c1f2ffc8 SM |
812 | ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them. |
813 | (let ((spec (cons keywords append)) cell) | |
041470d2 | 814 | (if (setq cell (assq mode font-lock-keywords-alist)) |
76f5e2af GM |
815 | (if (eq append 'set) |
816 | (setcdr cell (list spec)) | |
817 | (setcdr cell (append (cdr cell) (list spec)))) | |
818 | (push (list mode spec) font-lock-keywords-alist))) | |
819 | ;; Make sure that `font-lock-removed-keywords-alist' does not | |
820 | ;; contain the new keywords. | |
821 | (font-lock-update-removed-keyword-alist mode keywords append)) | |
6e1d0d15 SM |
822 | (t |
823 | ;; Otherwise set or add the keywords now. | |
824 | (font-lock-set-defaults) | |
825 | (if (eq append 'set) | |
826 | (setq font-lock-keywords keywords) | |
827 | (font-lock-remove-keywords nil keywords) ;to avoid duplicates | |
c1f2ffc8 SM |
828 | (let ((old (if (eq (car-safe font-lock-keywords) t) |
829 | (cdr font-lock-keywords) | |
830 | font-lock-keywords))) | |
831 | (setq font-lock-keywords (if append | |
832 | (append old keywords) | |
833 | (append keywords old)))))))) | |
3708dfe9 | 834 | |
6e1d0d15 | 835 | (defun font-lock-update-removed-keyword-alist (mode keywords append) |
76f5e2af | 836 | ;; Update `font-lock-removed-keywords-alist' when adding new |
6e1d0d15 | 837 | ;; KEYWORDS to MODE. |
76f5e2af GM |
838 | ;; |
839 | ;; When font-lock is enabled first all keywords in the list | |
840 | ;; `font-lock-keywords-alist' are added, then all keywords in the | |
841 | ;; list `font-lock-removed-keywords-alist' are removed. If a | |
842 | ;; keyword was once added, removed, and then added again it must be | |
843 | ;; removed from the removed-keywords list. Otherwise the second add | |
844 | ;; will not take effect. | |
6e1d0d15 | 845 | (let ((cell (assq mode font-lock-removed-keywords-alist))) |
76f5e2af GM |
846 | (if cell |
847 | (if (eq append 'set) | |
848 | ;; A new set of keywords is defined. Forget all about | |
849 | ;; our old keywords that should be removed. | |
850 | (setq font-lock-removed-keywords-alist | |
851 | (delq cell font-lock-removed-keywords-alist)) | |
852 | ;; Delete all previously removed keywords. | |
853 | (dolist (kword keywords) | |
854 | (setcdr cell (delete kword (cdr cell)))) | |
6e1d0d15 | 855 | ;; Delete the mode cell if empty. |
76f5e2af GM |
856 | (if (null (cdr cell)) |
857 | (setq font-lock-removed-keywords-alist | |
858 | (delq cell font-lock-removed-keywords-alist))))))) | |
859 | ||
860 | ;; Written by Anders Lindgren <andersl@andersl.com>. | |
861 | ;; | |
862 | ;; Case study: | |
863 | ;; (I) The keywords are removed from a major mode. | |
864 | ;; In this case the keyword could be local (i.e. added earlier by | |
865 | ;; `font-lock-add-keywords'), global, or both. | |
866 | ;; | |
867 | ;; (a) In the local case we remove the keywords from the variable | |
868 | ;; `font-lock-keywords-alist'. | |
869 | ;; | |
870 | ;; (b) The actual global keywords are not known at this time. | |
871 | ;; All keywords are added to `font-lock-removed-keywords-alist', | |
872 | ;; when font-lock is enabled those keywords are removed. | |
873 | ;; | |
874 | ;; Note that added keywords are taken out of the list of removed | |
875 | ;; keywords. This ensure correct operation when the same keyword | |
876 | ;; is added and removed several times. | |
877 | ;; | |
878 | ;; (II) The keywords are removed from the current buffer. | |
3708dfe9 | 879 | ;;;###autoload |
6e1d0d15 SM |
880 | (defun font-lock-remove-keywords (mode keywords) |
881 | "Remove highlighting KEYWORDS for MODE. | |
3708dfe9 | 882 | |
6e1d0d15 | 883 | MODE should be a symbol, the major mode command name, such as `c-mode' |
558371ba SM |
884 | or nil. If nil, highlighting keywords are removed for the current buffer. |
885 | ||
886 | When used from an elisp package (such as a minor mode), it is recommended | |
887 | to use nil for MODE (and place the call in a loop or on a hook) to avoid | |
888 | subtle problems due to details of the implementation." | |
6e1d0d15 SM |
889 | (cond (mode |
890 | ;; Remove one keyword at the time. | |
891 | (dolist (keyword keywords) | |
892 | (let ((top-cell (assq mode font-lock-keywords-alist))) | |
893 | ;; If MODE is non-nil, remove the KEYWORD from | |
76f5e2af GM |
894 | ;; `font-lock-keywords-alist'. |
895 | (when top-cell | |
896 | (dolist (keyword-list-append-pair (cdr top-cell)) | |
897 | ;; `keywords-list-append-pair' is a cons with a list of | |
898 | ;; keywords in the car top-cell and the original append | |
899 | ;; argument in the cdr top-cell. | |
900 | (setcar keyword-list-append-pair | |
901 | (delete keyword (car keyword-list-append-pair)))) | |
902 | ;; Remove keyword list/append pair when the keyword list | |
903 | ;; is empty and append doesn't specify `set'. (If it | |
904 | ;; should be deleted then previously deleted keywords | |
905 | ;; would appear again.) | |
906 | (let ((cell top-cell)) | |
907 | (while (cdr cell) | |
908 | (if (and (null (car (car (cdr cell)))) | |
909 | (not (eq (cdr (car (cdr cell))) 'set))) | |
910 | (setcdr cell (cdr (cdr cell))) | |
911 | (setq cell (cdr cell))))) | |
912 | ;; Final cleanup, remove major mode cell if last keyword | |
913 | ;; was deleted. | |
914 | (if (null (cdr top-cell)) | |
915 | (setq font-lock-keywords-alist | |
916 | (delq top-cell font-lock-keywords-alist)))) | |
917 | ;; Remember the keyword in case it is not local. | |
6e1d0d15 | 918 | (let ((cell (assq mode font-lock-removed-keywords-alist))) |
76f5e2af GM |
919 | (if cell |
920 | (unless (member keyword (cdr cell)) | |
921 | (nconc cell (list keyword))) | |
6e1d0d15 SM |
922 | (push (cons mode (list keyword)) |
923 | font-lock-removed-keywords-alist)))))) | |
924 | (t | |
925 | ;; Otherwise remove it immediately. | |
926 | (font-lock-set-defaults) | |
927 | (setq font-lock-keywords (copy-sequence font-lock-keywords)) | |
928 | (dolist (keyword keywords) | |
76f5e2af | 929 | (setq font-lock-keywords |
6e1d0d15 SM |
930 | (delete keyword |
931 | ;; The keywords might be compiled. | |
932 | (delete (font-lock-compile-keyword keyword) | |
933 | font-lock-keywords))))))) | |
a2b8e66b | 934 | \f |
2d63aa67 SM |
935 | ;;; Global Font Lock mode. |
936 | ||
a2b8e66b | 937 | ;; A few people have hassled in the past for a way to make it easier to turn on |
5aa29679 RS |
938 | ;; Font Lock mode, without the user needing to know for which modes s/he has to |
939 | ;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always | |
a2b8e66b SM |
940 | ;; balked at that way, as I see it as just re-moulding the same problem in |
941 | ;; another form. That is; some person would still have to keep track of which | |
942 | ;; modes (which may not even be distributed with Emacs) support Font Lock mode. | |
943 | ;; The list would always be out of date. And that person might have to be me. | |
944 | ||
5aa29679 RS |
945 | ;; Implementation. |
946 | ;; | |
947 | ;; In a previous discussion the following hack came to mind. It is a gross | |
948 | ;; hack, but it generally works. We use the convention that major modes start | |
949 | ;; by calling the function `kill-all-local-variables', which in turn runs | |
a2b8e66b SM |
950 | ;; functions on the hook variable `change-major-mode-hook'. We attach our |
951 | ;; function `font-lock-change-major-mode' to that hook. Of course, when this | |
952 | ;; hook is run, the major mode is in the process of being changed and we do not | |
953 | ;; know what the final major mode will be. So, `font-lock-change-major-mode' | |
954 | ;; only (a) notes the name of the current buffer, and (b) adds our function | |
5aa29679 RS |
955 | ;; `turn-on-font-lock-if-enabled' to the hook variables `find-file-hooks' and |
956 | ;; `post-command-hook' (for buffers that are not visiting files). By the time | |
957 | ;; the functions on the first of these hooks to be run are run, the new major | |
958 | ;; mode is assumed to be in place. This way we get a Font Lock function run | |
959 | ;; when a major mode is turned on, without knowing major modes or their hooks. | |
960 | ;; | |
a2b8e66b SM |
961 | ;; Naturally this requires that (a) major modes run `kill-all-local-variables', |
962 | ;; as they are supposed to do, and (b) the major mode is in place after the | |
5aa29679 RS |
963 | ;; file is visited or the command that ran `kill-all-local-variables' has |
964 | ;; finished, whichever the sooner. Arguably, any major mode that does not | |
965 | ;; follow the convension (a) is broken, and I can't think of any reason why (b) | |
966 | ;; would not be met (except `gnudoit' on non-files). However, it is not clean. | |
967 | ;; | |
a2b8e66b SM |
968 | ;; Probably the cleanest solution is to have each major mode function run some |
969 | ;; hook, e.g., `major-mode-hook', but maybe implementing that change is | |
5aa29679 RS |
970 | ;; impractical. I am personally against making `setq' a macro or be advised, |
971 | ;; or have a special function such as `set-major-mode', but maybe someone can | |
972 | ;; come up with another solution? | |
973 | ||
974 | ;; User interface. | |
975 | ;; | |
976 | ;; Although Global Font Lock mode is a pseudo-mode, I think that the user | |
977 | ;; interface should conform to the usual Emacs convention for modes, i.e., a | |
978 | ;; command to toggle the feature (`global-font-lock-mode') with a variable for | |
979 | ;; finer control of the mode's behaviour (`font-lock-global-modes'). | |
980 | ;; | |
2e6a15fc SM |
981 | ;; The feature should not be enabled by loading font-lock.el, since other |
982 | ;; mechanisms for turning on Font Lock mode, such as M-x font-lock-mode RET or | |
983 | ;; (add-hook 'c-mode-hook 'turn-on-font-lock), would cause Font Lock mode to be | |
984 | ;; turned on everywhere. That would not be intuitive or informative because | |
985 | ;; loading a file tells you nothing about the feature or how to control it. It | |
1c626aaf | 986 | ;; would also be contrary to the Principle of Least Surprise. sm. |
a2b8e66b | 987 | |
23a0e11b SM |
988 | (defcustom font-lock-global-modes t |
989 | "*Modes for which Font Lock mode is automagically turned on. | |
019f00d8 | 990 | Global Font Lock mode is controlled by the command `global-font-lock-mode'. |
23a0e11b SM |
991 | If nil, means no modes have Font Lock mode automatically turned on. |
992 | If t, all modes that support Font Lock mode have it automatically turned on. | |
993 | If a list, it should be a list of `major-mode' symbol names for which Font Lock | |
994 | mode should be automatically turned on. The sense of the list is negated if it | |
995 | begins with `not'. For example: | |
996 | (c-mode c++-mode) | |
997 | means that Font Lock mode is turned on for buffers in C and C++ modes only." | |
998 | :type '(choice (const :tag "none" nil) | |
999 | (const :tag "all" t) | |
1000 | (set :menu-tag "mode specific" :tag "modes" | |
1001 | :value (not) | |
1002 | (const :tag "Except" not) | |
1003 | (repeat :inline t (symbol :tag "mode")))) | |
1004 | :group 'font-lock) | |
1005 | ||
a465832f | 1006 | (defun turn-on-font-lock-if-enabled () |
ba22aeff SM |
1007 | (when (and (or font-lock-defaults |
1008 | (assq major-mode font-lock-defaults-alist)) | |
1009 | (or (eq font-lock-global-modes t) | |
1010 | (if (eq (car-safe font-lock-global-modes) 'not) | |
1011 | (not (memq major-mode (cdr font-lock-global-modes))) | |
1012 | (memq major-mode font-lock-global-modes)))) | |
1013 | (let (inhibit-quit) | |
1014 | (turn-on-font-lock)))) | |
1015 | ||
1016 | ;;;###autoload | |
1017 | (easy-mmode-define-global-mode | |
1018 | global-font-lock-mode font-lock-mode turn-on-font-lock-if-enabled | |
1019 | :extra-args (dummy)) | |
5aa29679 | 1020 | |
2d63aa67 | 1021 | ;;; End of Global Font Lock mode. |
a2b8e66b | 1022 | \f |
2d63aa67 SM |
1023 | ;;; Font Lock Support mode. |
1024 | ||
5aa29679 RS |
1025 | ;; This is the code used to interface font-lock.el with any of its add-on |
1026 | ;; packages, and provide the user interface. Packages that have their own | |
1027 | ;; local buffer fontification functions (see below) may have to call | |
1028 | ;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer' | |
1029 | ;; themselves. | |
1030 | ||
5f999b0b | 1031 | (defcustom font-lock-support-mode 'jit-lock-mode |
5aa29679 RS |
1032 | "*Support mode for Font Lock mode. |
1033 | Support modes speed up Font Lock mode by being choosy about when fontification | |
e7f07c2c GM |
1034 | occurs. Known support modes are Fast Lock mode (symbol `fast-lock-mode'), |
1035 | Lazy Lock mode (symbol `lazy-lock-mode'), and Just-in-time Lock mode (symbol | |
1036 | `jit-lock-mode'. See those modes for more info. | |
5aa29679 RS |
1037 | If nil, means support for Font Lock mode is never performed. |
1038 | If a symbol, use that support mode. | |
1039 | If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE), | |
1040 | where MAJOR-MODE is a symbol or t (meaning the default). For example: | |
1041 | ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode)) | |
1042 | means that Fast Lock mode is used to support Font Lock mode for buffers in C or | |
1043 | C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise. | |
1044 | ||
55015061 | 1045 | The value of this variable is used when Font Lock mode is turned on." |
b34aa0a3 SM |
1046 | :type '(choice (const :tag "none" nil) |
1047 | (const :tag "fast lock" fast-lock-mode) | |
1048 | (const :tag "lazy lock" lazy-lock-mode) | |
e7f07c2c | 1049 | (const :tag "jit lock" jit-lock-mode) |
b34aa0a3 | 1050 | (repeat :menu-tag "mode specific" :tag "mode specific" |
1c172af4 | 1051 | :value ((t . jit-lock-mode)) |
b34aa0a3 SM |
1052 | (cons :tag "Instance" |
1053 | (radio :tag "Mode" | |
1054 | (const :tag "all" t) | |
1055 | (symbol :tag "name")) | |
39df451b SM |
1056 | (radio :tag "Support" |
1057 | (const :tag "none" nil) | |
b34aa0a3 | 1058 | (const :tag "fast lock" fast-lock-mode) |
e7f07c2c GM |
1059 | (const :tag "lazy lock" lazy-lock-mode) |
1060 | (const :tag "JIT lock" jit-lock-mode))) | |
b34aa0a3 | 1061 | )) |
c69e5fcd | 1062 | :version "21.1" |
55015061 | 1063 | :group 'font-lock) |
5aa29679 | 1064 | |
2e6a15fc SM |
1065 | (defvar fast-lock-mode nil) |
1066 | (defvar lazy-lock-mode nil) | |
e7f07c2c | 1067 | (defvar jit-lock-mode nil) |
2e6a15fc | 1068 | |
5aa29679 RS |
1069 | (defun font-lock-turn-on-thing-lock () |
1070 | (let ((thing-mode (font-lock-value-in-major-mode font-lock-support-mode))) | |
1071 | (cond ((eq thing-mode 'fast-lock-mode) | |
1072 | (fast-lock-mode t)) | |
1073 | ((eq thing-mode 'lazy-lock-mode) | |
e7f07c2c GM |
1074 | (lazy-lock-mode t)) |
1075 | ((eq thing-mode 'jit-lock-mode) | |
28a53bc1 SM |
1076 | ;; Prepare for jit-lock |
1077 | (remove-hook 'after-change-functions | |
1078 | 'font-lock-after-change-function t) | |
1079 | (set (make-local-variable 'font-lock-fontify-buffer-function) | |
fd612dd9 | 1080 | 'jit-lock-refontify) |
28a53bc1 SM |
1081 | ;; Don't fontify eagerly (and don't abort is the buffer is large). |
1082 | (set (make-local-variable 'font-lock-fontified) t) | |
1083 | ;; Use jit-lock. | |
1084 | (jit-lock-register 'font-lock-fontify-region | |
1085 | (not font-lock-keywords-only)))))) | |
5aa29679 | 1086 | |
5aa29679 RS |
1087 | (defun font-lock-turn-off-thing-lock () |
1088 | (cond (fast-lock-mode | |
1c172af4 | 1089 | (fast-lock-mode -1)) |
e7f07c2c | 1090 | (jit-lock-mode |
28a53bc1 SM |
1091 | (jit-lock-unregister 'font-lock-fontify-region) |
1092 | ;; Reset local vars to the non-jit-lock case. | |
1093 | (kill-local-variable 'font-lock-fontify-buffer-function)) | |
5aa29679 | 1094 | (lazy-lock-mode |
1c172af4 | 1095 | (lazy-lock-mode -1)))) |
5aa29679 RS |
1096 | |
1097 | (defun font-lock-after-fontify-buffer () | |
1098 | (cond (fast-lock-mode | |
1099 | (fast-lock-after-fontify-buffer)) | |
13f0d185 SM |
1100 | ;; Useless now that jit-lock intercepts font-lock-fontify-buffer. -sm |
1101 | ;; (jit-lock-mode | |
1102 | ;; (jit-lock-after-fontify-buffer)) | |
5aa29679 RS |
1103 | (lazy-lock-mode |
1104 | (lazy-lock-after-fontify-buffer)))) | |
1105 | ||
1106 | (defun font-lock-after-unfontify-buffer () | |
1107 | (cond (fast-lock-mode | |
1108 | (fast-lock-after-unfontify-buffer)) | |
13f0d185 SM |
1109 | ;; Useless as well. It's only called when: |
1110 | ;; - turning off font-lock: it does not matter if we leave spurious | |
1111 | ;; `fontified' text props around since jit-lock-mode is also off. | |
1112 | ;; - font-lock-default-fontify-buffer fails: this is not run | |
1113 | ;; any more anyway. -sm | |
5e9e032a | 1114 | ;; |
13f0d185 SM |
1115 | ;; (jit-lock-mode |
1116 | ;; (jit-lock-after-unfontify-buffer)) | |
5aa29679 RS |
1117 | (lazy-lock-mode |
1118 | (lazy-lock-after-unfontify-buffer)))) | |
1119 | ||
2d63aa67 | 1120 | ;;; End of Font Lock Support mode. |
5aa29679 | 1121 | \f |
2d63aa67 | 1122 | ;;; Fontification functions. |
a1eb1cf1 | 1123 | |
56fcbd7e SM |
1124 | ;; Rather than the function, e.g., `font-lock-fontify-region' containing the |
1125 | ;; code to fontify a region, the function runs the function whose name is the | |
1126 | ;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally, | |
1127 | ;; the value of this variable is, e.g., `font-lock-default-fontify-region' | |
1128 | ;; which does contain the code to fontify a region. However, the value of the | |
1129 | ;; variable could be anything and thus, e.g., `font-lock-fontify-region' could | |
1130 | ;; do anything. The indirection of the fontification functions gives major | |
1131 | ;; modes the capability of modifying the way font-lock.el fontifies. Major | |
1132 | ;; modes can modify the values of, e.g., `font-lock-fontify-region-function', | |
1133 | ;; via the variable `font-lock-defaults'. | |
1134 | ;; | |
1135 | ;; For example, Rmail mode sets the variable `font-lock-defaults' so that | |
1136 | ;; font-lock.el uses its own function for buffer fontification. This function | |
1137 | ;; makes fontification be on a message-by-message basis and so visiting an | |
1138 | ;; RMAIL file is much faster. A clever implementation of the function might | |
1139 | ;; fontify the headers differently than the message body. (It should, and | |
1140 | ;; correspondingly for Mail mode, but I can't be bothered to do the work. Can | |
1141 | ;; you?) This hints at a more interesting use... | |
1142 | ;; | |
1143 | ;; Languages that contain text normally contained in different major modes | |
1144 | ;; could define their own fontification functions that treat text differently | |
1145 | ;; depending on its context. For example, Perl mode could arrange that here | |
1146 | ;; docs are fontified differently than Perl code. Or Yacc mode could fontify | |
1147 | ;; rules one way and C code another. Neat! | |
1148 | ;; | |
1149 | ;; A further reason to use the fontification indirection feature is when the | |
1150 | ;; default syntactual fontification, or the default fontification in general, | |
1151 | ;; is not flexible enough for a particular major mode. For example, perhaps | |
55015061 SM |
1152 | ;; comments are just too hairy for `font-lock-fontify-syntactically-region' to |
1153 | ;; cope with. You need to write your own version of that function, e.g., | |
1154 | ;; `hairy-fontify-syntactically-region', and make your own version of | |
1155 | ;; `hairy-fontify-region' call that function before calling | |
56fcbd7e SM |
1156 | ;; `font-lock-fontify-keywords-region' for the normal regexp fontification |
1157 | ;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el | |
1158 | ;; would call your region fontification function instead of its own. For | |
55015061 | 1159 | ;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line |
56fcbd7e SM |
1160 | ;; directives correctly and cleanly. (It is the same problem as fontifying |
1161 | ;; multi-line strings and comments; regexps are not appropriate for the job.) | |
1162 | ||
a1eb1cf1 | 1163 | ;;;###autoload |
030f4a35 | 1164 | (defun font-lock-fontify-buffer () |
019f00d8 | 1165 | "Fontify the current buffer the way the function `font-lock-mode' would." |
030f4a35 | 1166 | (interactive) |
a2b8e66b SM |
1167 | (let ((font-lock-verbose (or font-lock-verbose (interactive-p)))) |
1168 | (funcall font-lock-fontify-buffer-function))) | |
1169 | ||
1170 | (defun font-lock-unfontify-buffer () | |
1171 | (funcall font-lock-unfontify-buffer-function)) | |
1172 | ||
1173 | (defun font-lock-fontify-region (beg end &optional loudly) | |
1174 | (funcall font-lock-fontify-region-function beg end loudly)) | |
1175 | ||
1176 | (defun font-lock-unfontify-region (beg end) | |
1177 | (funcall font-lock-unfontify-region-function beg end)) | |
1178 | ||
1179 | (defun font-lock-default-fontify-buffer () | |
5aa29679 RS |
1180 | (let ((verbose (if (numberp font-lock-verbose) |
1181 | (> (buffer-size) font-lock-verbose) | |
1182 | font-lock-verbose))) | |
79f238c9 | 1183 | (with-temp-message |
1787769b SM |
1184 | (when verbose |
1185 | (format "Fontifying %s..." (buffer-name))) | |
79f238c9 SM |
1186 | ;; Make sure we have the right `font-lock-keywords' etc. |
1187 | (unless font-lock-mode | |
1188 | (font-lock-set-defaults)) | |
1189 | ;; Make sure we fontify etc. in the whole buffer. | |
1190 | (save-restriction | |
1191 | (widen) | |
1192 | (condition-case nil | |
1193 | (save-excursion | |
1194 | (save-match-data | |
1195 | (font-lock-fontify-region (point-min) (point-max) verbose) | |
1196 | (font-lock-after-fontify-buffer) | |
1197 | (setq font-lock-fontified t))) | |
1198 | ;; We don't restore the old fontification, so it's best to unfontify. | |
8259bf10 | 1199 | (quit (font-lock-unfontify-buffer))))))) |
7d7d915a | 1200 | |
a2b8e66b | 1201 | (defun font-lock-default-unfontify-buffer () |
56fcbd7e | 1202 | ;; Make sure we unfontify etc. in the whole buffer. |
a2b8e66b SM |
1203 | (save-restriction |
1204 | (widen) | |
1205 | (font-lock-unfontify-region (point-min) (point-max)) | |
271c888a | 1206 | (font-lock-after-unfontify-buffer) |
a2b8e66b | 1207 | (setq font-lock-fontified nil))) |
9bfbb130 | 1208 | |
a2b8e66b | 1209 | (defun font-lock-default-fontify-region (beg end loudly) |
ac6e572c SM |
1210 | (save-buffer-state |
1211 | ((parse-sexp-lookup-properties font-lock-syntactic-keywords) | |
1212 | (old-syntax-table (syntax-table))) | |
876f2438 | 1213 | (unwind-protect |
a078558d SM |
1214 | (save-restriction |
1215 | (widen) | |
876f2438 | 1216 | ;; Use the fontification syntax table, if any. |
2e6a15fc SM |
1217 | (when font-lock-syntax-table |
1218 | (set-syntax-table font-lock-syntax-table)) | |
fb0e4f8a RS |
1219 | ;; check to see if we should expand the beg/end area for |
1220 | ;; proper multiline matches | |
f9261020 GM |
1221 | (when (and font-lock-multiline |
1222 | (> beg (point-min)) | |
28a53bc1 SM |
1223 | (get-text-property (1- beg) 'font-lock-multiline)) |
1224 | ;; We are just after or in a multiline match. | |
1225 | (setq beg (or (previous-single-property-change | |
1226 | beg 'font-lock-multiline) | |
ba22aeff SM |
1227 | (point-min))) |
1228 | (goto-char beg) | |
1229 | (setq beg (line-beginning-position))) | |
111c181e GM |
1230 | (when font-lock-multiline |
1231 | (setq end (or (text-property-any end (point-max) | |
1232 | 'font-lock-multiline nil) | |
1233 | (point-max)))) | |
ba22aeff | 1234 | (goto-char end) |
724a330f | 1235 | (setq end (line-beginning-position 2)) |
876f2438 | 1236 | ;; Now do the fontification. |
2e6a15fc | 1237 | (font-lock-unfontify-region beg end) |
ac6e572c SM |
1238 | (when font-lock-syntactic-keywords |
1239 | (font-lock-fontify-syntactic-keywords-region beg end)) | |
2e6a15fc | 1240 | (unless font-lock-keywords-only |
876f2438 SM |
1241 | (font-lock-fontify-syntactically-region beg end loudly)) |
1242 | (font-lock-fontify-keywords-region beg end loudly)) | |
1243 | ;; Clean up. | |
2e6a15fc | 1244 | (set-syntax-table old-syntax-table)))) |
9bfbb130 SM |
1245 | |
1246 | ;; The following must be rethought, since keywords can override fontification. | |
1247 | ; ;; Now scan for keywords, but not if we are inside a comment now. | |
1248 | ; (or (and (not font-lock-keywords-only) | |
019f00d8 | 1249 | ; (let ((state (parse-partial-sexp beg end nil nil |
9bfbb130 SM |
1250 | ; font-lock-cache-state))) |
1251 | ; (or (nth 4 state) (nth 7 state)))) | |
1252 | ; (font-lock-fontify-keywords-region beg end)) | |
1253 | ||
8259bf10 SM |
1254 | (defvar font-lock-extra-managed-props nil |
1255 | "Additional text properties managed by font-lock. | |
1256 | This is used by `font-lock-default-unfontify-region' to decide | |
1257 | what properties to clear before refontifying a region.") | |
1258 | ||
a2b8e66b | 1259 | (defun font-lock-default-unfontify-region (beg end) |
2e6a15fc | 1260 | (save-buffer-state nil |
8259bf10 SM |
1261 | (remove-text-properties |
1262 | beg end (append | |
1263 | font-lock-extra-managed-props | |
1264 | (if font-lock-syntactic-keywords | |
1265 | '(face nil syntax-table nil font-lock-multiline nil) | |
1266 | '(face nil font-lock-multiline nil)))))) | |
9bfbb130 SM |
1267 | |
1268 | ;; Called when any modification is made to buffer text. | |
1269 | (defun font-lock-after-change-function (beg end old-len) | |
46f26fcf SM |
1270 | (let ((inhibit-point-motion-hooks t)) |
1271 | (save-excursion | |
1272 | (save-match-data | |
1273 | ;; Rescan between start of lines enclosing the region. | |
1274 | (font-lock-fontify-region | |
1275 | (progn (goto-char beg) (beginning-of-line) (point)) | |
1276 | (progn (goto-char end) (forward-line 1) (point))))))) | |
a2b8e66b | 1277 | |
a078558d SM |
1278 | (defun font-lock-fontify-block (&optional arg) |
1279 | "Fontify some lines the way `font-lock-fontify-buffer' would. | |
1280 | The lines could be a function or paragraph, or a specified number of lines. | |
a078558d | 1281 | If ARG is given, fontify that many lines before and after point, or 16 lines if |
a465832f SM |
1282 | no ARG is given and `font-lock-mark-block-function' is nil. |
1283 | If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to | |
1284 | delimit the region to fontify." | |
a078558d | 1285 | (interactive "P") |
46f26fcf SM |
1286 | (let ((inhibit-point-motion-hooks t) font-lock-beginning-of-syntax-function |
1287 | deactivate-mark) | |
a078558d SM |
1288 | ;; Make sure we have the right `font-lock-keywords' etc. |
1289 | (if (not font-lock-mode) (font-lock-set-defaults)) | |
a2b8e66b SM |
1290 | (save-excursion |
1291 | (save-match-data | |
1292 | (condition-case error-data | |
a078558d SM |
1293 | (if (or arg (not font-lock-mark-block-function)) |
1294 | (let ((lines (if arg (prefix-numeric-value arg) 16))) | |
1295 | (font-lock-fontify-region | |
1296 | (save-excursion (forward-line (- lines)) (point)) | |
1297 | (save-excursion (forward-line lines) (point)))) | |
1298 | (funcall font-lock-mark-block-function) | |
1299 | (font-lock-fontify-region (point) (mark))) | |
6c0fc428 | 1300 | ((error quit) (message "Fontifying block...%s" error-data))))))) |
a078558d | 1301 | |
94de9afe | 1302 | (define-key facemenu-keymap "\M-g" 'font-lock-fontify-block) |
2d63aa67 SM |
1303 | |
1304 | ;;; End of Fontification functions. | |
9bfbb130 | 1305 | \f |
9bfbb130 SM |
1306 | ;;; Additional text property functions. |
1307 | ||
1c626aaf SM |
1308 | ;; The following text property functions should be builtins. This means they |
1309 | ;; should be written in C and put with all the other text property functions. | |
1310 | ;; In the meantime, those that are used by font-lock.el are defined in Lisp | |
1311 | ;; below and given a `font-lock-' prefix. Those that are not used are defined | |
1312 | ;; in Lisp below and commented out. sm. | |
9bfbb130 | 1313 | |
1c626aaf | 1314 | (defun font-lock-prepend-text-property (start end prop value &optional object) |
9bfbb130 SM |
1315 | "Prepend to one property of the text from START to END. |
1316 | Arguments PROP and VALUE specify the property and value to prepend to the value | |
1c626aaf | 1317 | already in place. The resulting property values are always lists. |
9bfbb130 SM |
1318 | Optional argument OBJECT is the string or buffer containing the text." |
1319 | (let ((val (if (listp value) value (list value))) next prev) | |
1320 | (while (/= start end) | |
1321 | (setq next (next-single-property-change start prop object end) | |
1322 | prev (get-text-property start prop object)) | |
1c626aaf SM |
1323 | (put-text-property start next prop |
1324 | (append val (if (listp prev) prev (list prev))) | |
1325 | object) | |
9bfbb130 SM |
1326 | (setq start next)))) |
1327 | ||
1c626aaf | 1328 | (defun font-lock-append-text-property (start end prop value &optional object) |
9bfbb130 SM |
1329 | "Append to one property of the text from START to END. |
1330 | Arguments PROP and VALUE specify the property and value to append to the value | |
1c626aaf | 1331 | already in place. The resulting property values are always lists. |
9bfbb130 SM |
1332 | Optional argument OBJECT is the string or buffer containing the text." |
1333 | (let ((val (if (listp value) value (list value))) next prev) | |
1334 | (while (/= start end) | |
1335 | (setq next (next-single-property-change start prop object end) | |
1336 | prev (get-text-property start prop object)) | |
1c626aaf SM |
1337 | (put-text-property start next prop |
1338 | (append (if (listp prev) prev (list prev)) val) | |
1339 | object) | |
9bfbb130 | 1340 | (setq start next)))) |
1c626aaf SM |
1341 | |
1342 | (defun font-lock-fillin-text-property (start end prop value &optional object) | |
1343 | "Fill in one property of the text from START to END. | |
1344 | Arguments PROP and VALUE specify the property and value to put where none are | |
1345 | already in place. Therefore existing property values are not overwritten. | |
1346 | Optional argument OBJECT is the string or buffer containing the text." | |
1347 | (let ((start (text-property-any start end prop nil object)) next) | |
1348 | (while start | |
1349 | (setq next (next-single-property-change start prop object end)) | |
1350 | (put-text-property start next prop value object) | |
1351 | (setq start (text-property-any next end prop nil object))))) | |
1352 | ||
1353 | ;; For completeness: this is to `remove-text-properties' as `put-text-property' | |
1354 | ;; is to `add-text-properties', etc. | |
1355 | ;(defun remove-text-property (start end property &optional object) | |
1356 | ; "Remove a property from text from START to END. | |
1357 | ;Argument PROPERTY is the property to remove. | |
1358 | ;Optional argument OBJECT is the string or buffer containing the text. | |
1359 | ;Return t if the property was actually removed, nil otherwise." | |
1360 | ; (remove-text-properties start end (list property) object)) | |
1361 | ||
1362 | ;; For consistency: maybe this should be called `remove-single-property' like | |
1363 | ;; `next-single-property-change' (not `next-single-text-property-change'), etc. | |
1364 | ;(defun remove-single-text-property (start end prop value &optional object) | |
1365 | ; "Remove a specific property value from text from START to END. | |
1366 | ;Arguments PROP and VALUE specify the property and value to remove. The | |
1367 | ;resulting property values are not equal to VALUE nor lists containing VALUE. | |
1368 | ;Optional argument OBJECT is the string or buffer containing the text." | |
1369 | ; (let ((start (text-property-not-all start end prop nil object)) next prev) | |
1370 | ; (while start | |
1371 | ; (setq next (next-single-property-change start prop object end) | |
1372 | ; prev (get-text-property start prop object)) | |
1373 | ; (cond ((and (symbolp prev) (eq value prev)) | |
1374 | ; (remove-text-property start next prop object)) | |
1375 | ; ((and (listp prev) (memq value prev)) | |
1376 | ; (let ((new (delq value prev))) | |
1377 | ; (cond ((null new) | |
1378 | ; (remove-text-property start next prop object)) | |
1379 | ; ((= (length new) 1) | |
1380 | ; (put-text-property start next prop (car new) object)) | |
1381 | ; (t | |
1382 | ; (put-text-property start next prop new object)))))) | |
1383 | ; (setq start (text-property-not-all next end prop nil object))))) | |
2d63aa67 SM |
1384 | |
1385 | ;;; End of Additional text property functions. | |
9bfbb130 | 1386 | \f |
ac6e572c SM |
1387 | ;;; Syntactic regexp fontification functions. |
1388 | ||
1389 | ;; These syntactic keyword pass functions are identical to those keyword pass | |
1390 | ;; functions below, with the following exceptions; (a) they operate on | |
1391 | ;; `font-lock-syntactic-keywords' of course, (b) they are all `defun' as speed | |
1392 | ;; is less of an issue, (c) eval of property value does not occur JIT as speed | |
1393 | ;; is less of an issue, (d) OVERRIDE cannot be `prepend' or `append' as it | |
1394 | ;; makes no sense for `syntax-table' property values, (e) they do not do it | |
1395 | ;; LOUDLY as it is not likely to be intensive. | |
1396 | ||
1397 | (defun font-lock-apply-syntactic-highlight (highlight) | |
1398 | "Apply HIGHLIGHT following a match. | |
1399 | HIGHLIGHT should be of the form MATCH-HIGHLIGHT, | |
1400 | see `font-lock-syntactic-keywords'." | |
1401 | (let* ((match (nth 0 highlight)) | |
1402 | (start (match-beginning match)) (end (match-end match)) | |
1403 | (value (nth 1 highlight)) | |
1404 | (override (nth 2 highlight))) | |
8259bf10 SM |
1405 | (if (not start) |
1406 | ;; No match but we might not signal an error. | |
1407 | (or (nth 3 highlight) | |
1408 | (error "No match %d in highlight %S" match highlight)) | |
1409 | (when (and (consp value) (not (numberp (car value)))) | |
1410 | (setq value (eval value))) | |
1411 | (when (stringp value) (setq value (string-to-syntax value))) | |
1412 | ;; Flush the syntax-cache. I believe this is not necessary for | |
1413 | ;; font-lock's use of syntax-ppss, but I'm not 100% sure and it can | |
1414 | ;; still be necessary for other users of syntax-ppss anyway. | |
1415 | (syntax-ppss-after-change-function start) | |
1416 | (cond | |
1417 | ((not override) | |
1418 | ;; Cannot override existing fontification. | |
1419 | (or (text-property-not-all start end 'syntax-table nil) | |
1420 | (put-text-property start end 'syntax-table value))) | |
1421 | ((eq override t) | |
1422 | ;; Override existing fontification. | |
1423 | (put-text-property start end 'syntax-table value)) | |
1424 | ((eq override 'keep) | |
1425 | ;; Keep existing fontification. | |
1426 | (font-lock-fillin-text-property start end 'syntax-table value)))))) | |
ac6e572c SM |
1427 | |
1428 | (defun font-lock-fontify-syntactic-anchored-keywords (keywords limit) | |
1429 | "Fontify according to KEYWORDS until LIMIT. | |
1430 | KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords', | |
1431 | LIMIT can be modified by the value of its PRE-MATCH-FORM." | |
1432 | (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights | |
1433 | ;; Evaluate PRE-MATCH-FORM. | |
1434 | (pre-match-value (eval (nth 1 keywords)))) | |
1435 | ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line. | |
1436 | (if (and (numberp pre-match-value) (> pre-match-value (point))) | |
1437 | (setq limit pre-match-value) | |
6e1d0d15 | 1438 | (setq limit (line-end-position))) |
ac6e572c SM |
1439 | (save-match-data |
1440 | ;; Find an occurrence of `matcher' before `limit'. | |
1441 | (while (if (stringp matcher) | |
1442 | (re-search-forward matcher limit t) | |
1443 | (funcall matcher limit)) | |
1444 | ;; Apply each highlight to this instance of `matcher'. | |
1445 | (setq highlights lowdarks) | |
1446 | (while highlights | |
1447 | (font-lock-apply-syntactic-highlight (car highlights)) | |
1448 | (setq highlights (cdr highlights))))) | |
1449 | ;; Evaluate POST-MATCH-FORM. | |
1450 | (eval (nth 2 keywords)))) | |
1451 | ||
1452 | (defun font-lock-fontify-syntactic-keywords-region (start end) | |
1453 | "Fontify according to `font-lock-syntactic-keywords' between START and END. | |
1454 | START should be at the beginning of a line." | |
c0a6a9fe SM |
1455 | ;; Ensure the beginning of the file is properly syntactic-fontified. |
1456 | (when (and font-lock-syntactically-fontified | |
1457 | (< font-lock-syntactically-fontified start)) | |
1458 | (setq start (max font-lock-syntactically-fontified (point-min))) | |
1459 | (setq font-lock-syntactically-fontified end)) | |
ac6e572c SM |
1460 | ;; If `font-lock-syntactic-keywords' is a symbol, get the real keywords. |
1461 | (when (symbolp font-lock-syntactic-keywords) | |
1462 | (setq font-lock-syntactic-keywords (font-lock-eval-keywords | |
1463 | font-lock-syntactic-keywords))) | |
1464 | ;; If `font-lock-syntactic-keywords' is not compiled, compile it. | |
1465 | (unless (eq (car font-lock-syntactic-keywords) t) | |
1466 | (setq font-lock-syntactic-keywords (font-lock-compile-keywords | |
1467 | font-lock-syntactic-keywords))) | |
1468 | ;; Get down to business. | |
1469 | (let ((case-fold-search font-lock-keywords-case-fold-search) | |
1470 | (keywords (cdr font-lock-syntactic-keywords)) | |
1471 | keyword matcher highlights) | |
1472 | (while keywords | |
1473 | ;; Find an occurrence of `matcher' from `start' to `end'. | |
1474 | (setq keyword (car keywords) matcher (car keyword)) | |
1475 | (goto-char start) | |
1476 | (while (if (stringp matcher) | |
1477 | (re-search-forward matcher end t) | |
1478 | (funcall matcher end)) | |
1479 | ;; Apply each highlight to this instance of `matcher', which may be | |
1480 | ;; specific highlights or more keywords anchored to `matcher'. | |
1481 | (setq highlights (cdr keyword)) | |
1482 | (while highlights | |
1483 | (if (numberp (car (car highlights))) | |
1484 | (font-lock-apply-syntactic-highlight (car highlights)) | |
1485 | (font-lock-fontify-syntactic-anchored-keywords (car highlights) | |
1486 | end)) | |
1487 | (setq highlights (cdr highlights)))) | |
1488 | (setq keywords (cdr keywords))))) | |
1489 | ||
1490 | ;;; End of Syntactic regexp fontification functions. | |
1491 | \f | |
1492 | ;;; Syntactic fontification functions. | |
1493 | ||
1494 | ;; These record the parse state at a particular position, always the start of a | |
1495 | ;; line. Used to make `font-lock-fontify-syntactically-region' faster. | |
1496 | ;; Previously, `font-lock-cache-position' was just a buffer position. However, | |
1497 | ;; under certain situations, this occasionally resulted in mis-fontification. | |
1498 | ;; I think the "situations" were deletion with Lazy Lock mode's deferral. sm. | |
1499 | (defvar font-lock-cache-state nil) | |
1500 | (defvar font-lock-cache-position nil) | |
8259bf10 | 1501 | (defvar font-lock-ppss-stats '(0 . 0.0)) |
ac6e572c | 1502 | |
8259bf10 SM |
1503 | (defun font-lock-ppss (start) |
1504 | (let ((cache (marker-position font-lock-cache-position))) | |
1505 | (if (eq start cache) | |
1506 | ;; Use the cache for the state of `start'. | |
1507 | font-lock-cache-state | |
1508 | ;; Find the state of `start'. | |
1509 | (let ((state | |
1510 | (if (null font-lock-beginning-of-syntax-function) | |
1511 | ;; Use the state at the previous cache position, if any, or | |
1512 | ;; otherwise calculate from `point-min'. | |
1513 | (if (or (null cache) (< start cache)) | |
1514 | (progn | |
1515 | (incf (cdr font-lock-ppss-stats) (- start (point-min))) | |
1516 | (parse-partial-sexp (point-min) start)) | |
1517 | (incf (cdr font-lock-ppss-stats) (- start cache)) | |
1518 | (parse-partial-sexp cache start nil nil | |
1519 | font-lock-cache-state)) | |
1520 | ;; Call the function to move outside any syntactic block. | |
1521 | (funcall font-lock-beginning-of-syntax-function) | |
1522 | (incf (cdr font-lock-ppss-stats) (- start (point))) | |
1523 | (parse-partial-sexp (point) start)))) | |
1524 | (incf (car font-lock-ppss-stats)) | |
1525 | ;; Cache the state and position of `start'. | |
1526 | (setq font-lock-cache-state state) | |
1527 | (set-marker font-lock-cache-position start) | |
1528 | state)))) | |
1529 | ||
1530 | (elp-instrument-list '(font-lock-ppss)) | |
1531 | ||
1532 | (defun font-lock-fontify-syntactically-region (start end &optional loudly ppss) | |
ac6e572c SM |
1533 | "Put proper face on each string and comment between START and END. |
1534 | START should be at the beginning of a line." | |
8259bf10 | 1535 | (let (state face beg) |
ac6e572c SM |
1536 | (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name))) |
1537 | (goto-char start) | |
1538 | ;; | |
1539 | ;; Find the state at the `beginning-of-line' before `start'. | |
8259bf10 SM |
1540 | (setq state |
1541 | ;; We use both functions for benchmarking/tuning purposes. | |
1542 | ;; FIXME: this should be fixed before the release. | |
1543 | (or ppss (let ((state1 (font-lock-ppss start)) | |
1544 | (state2 (syntax-ppss start))) | |
1545 | state2))) | |
ac6e572c SM |
1546 | ;; |
1547 | ;; Find each interesting place between here and `end'. | |
8259bf10 SM |
1548 | (while |
1549 | (progn | |
1550 | (when (or (nth 3 state) (nth 4 state)) | |
1551 | (setq face (funcall font-lock-syntactic-face-function state)) | |
1552 | (setq beg (max (nth 8 state) start)) | |
1553 | (setq state (parse-partial-sexp (point) end nil nil state | |
1554 | 'syntax-table)) | |
1555 | (when face (put-text-property beg (point) 'face face))) | |
1556 | (setq state (parse-partial-sexp (point) end nil nil state | |
1557 | 'syntax-table)) | |
1558 | (< (point) end))))) | |
ac6e572c SM |
1559 | |
1560 | ;;; End of Syntactic fontification functions. | |
1561 | \f | |
1562 | ;;; Keyword regexp fontification functions. | |
9bfbb130 SM |
1563 | |
1564 | (defsubst font-lock-apply-highlight (highlight) | |
1565 | "Apply HIGHLIGHT following a match. | |
1566 | HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'." | |
1567 | (let* ((match (nth 0 highlight)) | |
1568 | (start (match-beginning match)) (end (match-end match)) | |
1569 | (override (nth 2 highlight))) | |
8259bf10 SM |
1570 | (if (not start) |
1571 | ;; No match but we might not signal an error. | |
1572 | (or (nth 3 highlight) | |
1573 | (error "No match %d in highlight %S" match highlight)) | |
1574 | (let ((val (eval (nth 1 highlight)))) | |
1575 | (when (eq (car-safe val) 'face) | |
1576 | (add-text-properties start end (cddr val)) | |
1577 | (setq val (cadr val))) | |
1578 | (cond | |
1579 | ((not override) | |
1580 | ;; Cannot override existing fontification. | |
1581 | (or (text-property-not-all start end 'face nil) | |
1582 | (put-text-property start end 'face val))) | |
1583 | ((eq override t) | |
1584 | ;; Override existing fontification. | |
1585 | (put-text-property start end 'face val)) | |
1586 | ((eq override 'prepend) | |
1587 | ;; Prepend to existing fontification. | |
1588 | (font-lock-prepend-text-property start end 'face val)) | |
1589 | ((eq override 'append) | |
1590 | ;; Append to existing fontification. | |
1591 | (font-lock-append-text-property start end 'face val)) | |
1592 | ((eq override 'keep) | |
1593 | ;; Keep existing fontification. | |
1594 | (font-lock-fillin-text-property start end 'face val))))))) | |
9bfbb130 SM |
1595 | |
1596 | (defsubst font-lock-fontify-anchored-keywords (keywords limit) | |
1597 | "Fontify according to KEYWORDS until LIMIT. | |
56fcbd7e SM |
1598 | KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords', |
1599 | LIMIT can be modified by the value of its PRE-MATCH-FORM." | |
1600 | (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights | |
fb0e4f8a | 1601 | (lead-start (match-beginning 0)) |
56fcbd7e SM |
1602 | ;; Evaluate PRE-MATCH-FORM. |
1603 | (pre-match-value (eval (nth 1 keywords)))) | |
1604 | ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line. | |
fb0e4f8a | 1605 | (if (not (and (numberp pre-match-value) (> pre-match-value (point)))) |
6e1d0d15 | 1606 | (setq limit (line-end-position)) |
fb0e4f8a | 1607 | (setq limit pre-match-value) |
ba22aeff | 1608 | (when (and font-lock-multiline (>= limit (line-beginning-position 2))) |
fb0e4f8a | 1609 | ;; this is a multiline anchored match |
ba22aeff SM |
1610 | ;; (setq font-lock-multiline t) |
1611 | (put-text-property (if (= limit (line-beginning-position 2)) | |
1612 | (1- limit) | |
1613 | (min lead-start (point))) | |
1614 | limit | |
28a53bc1 | 1615 | 'font-lock-multiline t))) |
9bfbb130 | 1616 | (save-match-data |
876f2438 | 1617 | ;; Find an occurrence of `matcher' before `limit'. |
afa18a4e SM |
1618 | (while (and (< (point) limit) |
1619 | (if (stringp matcher) | |
1620 | (re-search-forward matcher limit t) | |
1621 | (funcall matcher limit))) | |
876f2438 | 1622 | ;; Apply each highlight to this instance of `matcher'. |
9bfbb130 SM |
1623 | (setq highlights lowdarks) |
1624 | (while highlights | |
1625 | (font-lock-apply-highlight (car highlights)) | |
1626 | (setq highlights (cdr highlights))))) | |
876f2438 | 1627 | ;; Evaluate POST-MATCH-FORM. |
9bfbb130 SM |
1628 | (eval (nth 2 keywords)))) |
1629 | ||
1630 | (defun font-lock-fontify-keywords-region (start end &optional loudly) | |
1631 | "Fontify according to `font-lock-keywords' between START and END. | |
1632 | START should be at the beginning of a line." | |
ac6e572c | 1633 | (unless (eq (car font-lock-keywords) t) |
55015061 | 1634 | (setq font-lock-keywords (font-lock-compile-keywords font-lock-keywords))) |
9bfbb130 | 1635 | (let ((case-fold-search font-lock-keywords-case-fold-search) |
55015061 | 1636 | (keywords (cdr font-lock-keywords)) |
9bfbb130 | 1637 | (bufname (buffer-name)) (count 0) |
876f2438 SM |
1638 | keyword matcher highlights) |
1639 | ;; | |
1640 | ;; Fontify each item in `font-lock-keywords' from `start' to `end'. | |
1641 | (while keywords | |
1642 | (if loudly (message "Fontifying %s... (regexps..%s)" bufname | |
2e6a15fc | 1643 | (make-string (incf count) ?.))) |
9bfbb130 | 1644 | ;; |
876f2438 SM |
1645 | ;; Find an occurrence of `matcher' from `start' to `end'. |
1646 | (setq keyword (car keywords) matcher (car keyword)) | |
1647 | (goto-char start) | |
e7f07c2c GM |
1648 | (while (and (< (point) end) |
1649 | (if (stringp matcher) | |
1650 | (re-search-forward matcher end t) | |
1651 | (funcall matcher end))) | |
041470d2 | 1652 | (when (and font-lock-multiline |
ba22aeff SM |
1653 | (>= (point) |
1654 | (save-excursion (goto-char (match-beginning 0)) | |
1655 | (forward-line 1) (point)))) | |
fb0e4f8a | 1656 | ;; this is a multiline regexp match |
ba22aeff SM |
1657 | ;; (setq font-lock-multiline t) |
1658 | (put-text-property (if (= (point) | |
1659 | (save-excursion | |
1660 | (goto-char (match-beginning 0)) | |
1661 | (forward-line 1) (point))) | |
1662 | (1- (point)) | |
1663 | (match-beginning 0)) | |
1664 | (point) | |
fb0e4f8a | 1665 | 'font-lock-multiline t)) |
876f2438 SM |
1666 | ;; Apply each highlight to this instance of `matcher', which may be |
1667 | ;; specific highlights or more keywords anchored to `matcher'. | |
1668 | (setq highlights (cdr keyword)) | |
1669 | (while highlights | |
1670 | (if (numberp (car (car highlights))) | |
1671 | (font-lock-apply-highlight (car highlights)) | |
1672 | (font-lock-fontify-anchored-keywords (car highlights) end)) | |
1673 | (setq highlights (cdr highlights)))) | |
1674 | (setq keywords (cdr keywords))))) | |
2d63aa67 | 1675 | |
ac6e572c | 1676 | ;;; End of Keyword regexp fontification functions. |
9bfbb130 SM |
1677 | \f |
1678 | ;; Various functions. | |
1679 | ||
55015061 | 1680 | (defun font-lock-compile-keywords (keywords) |
019f00d8 DL |
1681 | "Compile KEYWORDS into the form (t KEYWORD ...). |
1682 | Here KEYWORD is of the form (MATCHER HIGHLIGHT ...) as shown in the | |
1683 | `font-lock-keywords' doc string." | |
55015061 SM |
1684 | (if (eq (car-safe keywords) t) |
1685 | keywords | |
1686 | (cons t (mapcar 'font-lock-compile-keyword keywords)))) | |
a2b8e66b SM |
1687 | |
1688 | (defun font-lock-compile-keyword (keyword) | |
c1f2ffc8 | 1689 | (cond ((nlistp keyword) ; MATCHER |
a2b8e66b | 1690 | (list keyword '(0 font-lock-keyword-face))) |
c1f2ffc8 | 1691 | ((eq (car keyword) 'eval) ; (eval . FORM) |
a2b8e66b | 1692 | (font-lock-compile-keyword (eval (cdr keyword)))) |
c1f2ffc8 SM |
1693 | ((eq (car-safe (cdr keyword)) 'quote) ; (MATCHER . 'FORM) |
1694 | ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote. | |
1695 | (if (symbolp (nth 2 keyword)) | |
1696 | (list (car keyword) (list 0 (cdr keyword))) | |
1697 | (font-lock-compile-keyword (cons (car keyword) (nth 2 keyword))))) | |
1698 | ((numberp (cdr keyword)) ; (MATCHER . MATCH) | |
a2b8e66b | 1699 | (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face))) |
c1f2ffc8 | 1700 | ((symbolp (cdr keyword)) ; (MATCHER . FACENAME) |
a2b8e66b | 1701 | (list (car keyword) (list 0 (cdr keyword)))) |
c1f2ffc8 | 1702 | ((nlistp (nth 1 keyword)) ; (MATCHER . HIGHLIGHT) |
a2b8e66b | 1703 | (list (car keyword) (cdr keyword))) |
c1f2ffc8 | 1704 | (t ; (MATCHER HIGHLIGHT ...) |
a2b8e66b | 1705 | keyword))) |
d46c21ec | 1706 | |
ac6e572c | 1707 | (defun font-lock-eval-keywords (keywords) |
019f00d8 | 1708 | "Evalulate KEYWORDS if a function (funcall) or variable (eval) name." |
46f26fcf SM |
1709 | (if (listp keywords) |
1710 | keywords | |
1711 | (font-lock-eval-keywords (if (fboundp keywords) | |
1712 | (funcall keywords) | |
1713 | (eval keywords))))) | |
ac6e572c | 1714 | |
343204bd | 1715 | (defun font-lock-value-in-major-mode (alist) |
019f00d8 DL |
1716 | "Return value in ALIST for `major-mode', or ALIST if it is not an alist. |
1717 | Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t." | |
343204bd SM |
1718 | (if (consp alist) |
1719 | (cdr (or (assq major-mode alist) (assq t alist))) | |
1720 | alist)) | |
1721 | ||
d46c21ec | 1722 | (defun font-lock-choose-keywords (keywords level) |
019f00d8 DL |
1723 | "Return LEVELth element of KEYWORDS. |
1724 | A LEVEL of nil is equal to a LEVEL of 0, a LEVEL of t is equal to | |
1725 | \(1- (length KEYWORDS))." | |
fd612dd9 | 1726 | (cond ((not (and (listp keywords) (symbolp (car keywords)))) |
343204bd SM |
1727 | keywords) |
1728 | ((numberp level) | |
1729 | (or (nth level keywords) (car (reverse keywords)))) | |
1730 | ((eq level t) | |
1731 | (car (reverse keywords))) | |
1732 | (t | |
1733 | (car keywords)))) | |
d46c21ec | 1734 | |
2e6a15fc SM |
1735 | (defvar font-lock-set-defaults nil) ; Whether we have set up defaults. |
1736 | ||
d46c21ec SM |
1737 | (defun font-lock-set-defaults () |
1738 | "Set fontification defaults appropriately for this mode. | |
a2b8e66b SM |
1739 | Sets various variables using `font-lock-defaults' (or, if nil, using |
1740 | `font-lock-defaults-alist') and `font-lock-maximum-decoration'." | |
6e1d0d15 | 1741 | ;; Set fontification defaults iff not previously set. |
2e6a15fc SM |
1742 | (unless font-lock-set-defaults |
1743 | (set (make-local-variable 'font-lock-set-defaults) t) | |
1744 | (set (make-local-variable 'font-lock-cache-state) nil) | |
1745 | (set (make-local-variable 'font-lock-cache-position) (make-marker)) | |
6e1d0d15 SM |
1746 | (make-local-variable 'font-lock-fontified) |
1747 | (make-local-variable 'font-lock-multiline) | |
2e6a15fc SM |
1748 | (let* ((defaults (or font-lock-defaults |
1749 | (cdr (assq major-mode font-lock-defaults-alist)))) | |
1750 | (keywords | |
1751 | (font-lock-choose-keywords (nth 0 defaults) | |
c1f2ffc8 | 1752 | (font-lock-value-in-major-mode font-lock-maximum-decoration))) |
76f5e2af GM |
1753 | (local (cdr (assq major-mode font-lock-keywords-alist))) |
1754 | (removed-keywords | |
1755 | (cdr-safe (assq major-mode font-lock-removed-keywords-alist)))) | |
2e6a15fc | 1756 | ;; Regexp fontification? |
2d63aa67 | 1757 | (set (make-local-variable 'font-lock-keywords) |
ac6e572c | 1758 | (font-lock-compile-keywords (font-lock-eval-keywords keywords))) |
c1f2ffc8 SM |
1759 | ;; Local fontification? |
1760 | (while local | |
1761 | (font-lock-add-keywords nil (car (car local)) (cdr (car local))) | |
1762 | (setq local (cdr local))) | |
76f5e2af GM |
1763 | (when removed-keywords |
1764 | (font-lock-remove-keywords nil removed-keywords)) | |
2e6a15fc SM |
1765 | ;; Syntactic fontification? |
1766 | (when (nth 1 defaults) | |
1767 | (set (make-local-variable 'font-lock-keywords-only) t)) | |
1768 | ;; Case fold during regexp fontification? | |
1769 | (when (nth 2 defaults) | |
1770 | (set (make-local-variable 'font-lock-keywords-case-fold-search) t)) | |
1771 | ;; Syntax table for regexp and syntactic fontification? | |
1772 | (when (nth 3 defaults) | |
8259bf10 SM |
1773 | (set (make-local-variable 'font-lock-syntax-table) |
1774 | (copy-syntax-table (syntax-table))) | |
1775 | (dolist (selem (nth 3 defaults)) | |
1776 | ;; The character to modify may be a single CHAR or a STRING. | |
1777 | (let ((syntax (cdr selem))) | |
1778 | (dolist (char (if (numberp (car selem)) | |
1779 | (list (car selem)) | |
1780 | (mapcar 'identity (car selem)))) | |
1781 | (modify-syntax-entry char syntax font-lock-syntax-table))))) | |
2e6a15fc SM |
1782 | ;; Syntax function for syntactic fontification? |
1783 | (when (nth 4 defaults) | |
1784 | (set (make-local-variable 'font-lock-beginning-of-syntax-function) | |
1785 | (nth 4 defaults))) | |
1786 | ;; Variable alist? | |
724a330f | 1787 | (dolist (x (nthcdr 5 defaults)) |
8259bf10 | 1788 | (set (make-local-variable (car x)) (cdr x)))))) |
030f4a35 | 1789 | \f |
2d63aa67 | 1790 | ;;; Colour etc. support. |
9bfbb130 | 1791 | |
2273c36b | 1792 | ;; Originally face attributes were specified via `font-lock-face-attributes'. |
23a0e11b | 1793 | ;; Users then changed the default face attributes by setting that variable. |
2273c36b SM |
1794 | ;; However, we try and be back-compatible and respect its value if set except |
1795 | ;; for faces where M-x customize has been used to save changes for the face. | |
1796 | (when (boundp 'font-lock-face-attributes) | |
1797 | (let ((face-attributes font-lock-face-attributes)) | |
1798 | (while face-attributes | |
1799 | (let* ((face-attribute (pop face-attributes)) | |
1800 | (face (car face-attribute))) | |
1801 | ;; Rustle up a `defface' SPEC from a `font-lock-face-attributes' entry. | |
1802 | (unless (get face 'saved-face) | |
1803 | (let ((foreground (nth 1 face-attribute)) | |
1804 | (background (nth 2 face-attribute)) | |
1805 | (bold-p (nth 3 face-attribute)) | |
1806 | (italic-p (nth 4 face-attribute)) | |
1807 | (underline-p (nth 5 face-attribute)) | |
1808 | face-spec) | |
1809 | (when foreground | |
1810 | (setq face-spec (cons ':foreground (cons foreground face-spec)))) | |
1811 | (when background | |
1812 | (setq face-spec (cons ':background (cons background face-spec)))) | |
1813 | (when bold-p | |
1814 | (setq face-spec (append '(:bold t) face-spec))) | |
1815 | (when italic-p | |
1816 | (setq face-spec (append '(:italic t) face-spec))) | |
1817 | (when underline-p | |
1818 | (setq face-spec (append '(:underline t) face-spec))) | |
1819 | (custom-declare-face face (list (list t face-spec)) nil))))))) | |
1820 | ||
1821 | ;; But now we do it the custom way. Note that `defface' will not overwrite any | |
1822 | ;; faces declared above via `custom-declare-face'. | |
55015061 | 1823 | (defface font-lock-comment-face |
8379c868 | 1824 | '((((type tty pc) (class color) (background light)) (:foreground "red")) |
ce6b1982 | 1825 | (((type tty pc) (class color) (background dark)) (:foreground "red1")) |
e7f07c2c | 1826 | (((class grayscale) (background light)) |
55015061 SM |
1827 | (:foreground "DimGray" :bold t :italic t)) |
1828 | (((class grayscale) (background dark)) | |
1829 | (:foreground "LightGray" :bold t :italic t)) | |
1830 | (((class color) (background light)) (:foreground "Firebrick")) | |
86b7fcbb | 1831 | (((class color) (background dark)) (:foreground "chocolate1")) |
55015061 SM |
1832 | (t (:bold t :italic t))) |
1833 | "Font Lock mode face used to highlight comments." | |
2273c36b | 1834 | :group 'font-lock-highlighting-faces) |
55015061 SM |
1835 | |
1836 | (defface font-lock-string-face | |
e7f07c2c GM |
1837 | '((((type tty) (class color)) (:foreground "green")) |
1838 | (((class grayscale) (background light)) (:foreground "DimGray" :italic t)) | |
55015061 SM |
1839 | (((class grayscale) (background dark)) (:foreground "LightGray" :italic t)) |
1840 | (((class color) (background light)) (:foreground "RosyBrown")) | |
1841 | (((class color) (background dark)) (:foreground "LightSalmon")) | |
1842 | (t (:italic t))) | |
1843 | "Font Lock mode face used to highlight strings." | |
2273c36b | 1844 | :group 'font-lock-highlighting-faces) |
55015061 | 1845 | |
1c172af4 SM |
1846 | (defface font-lock-doc-face |
1847 | '((t :inherit font-lock-string-face)) | |
1848 | "Font Lock mode face used to highlight documentation." | |
1849 | :group 'font-lock-highlighting-faces) | |
1850 | ||
55015061 | 1851 | (defface font-lock-keyword-face |
e7f07c2c GM |
1852 | '((((type tty) (class color)) (:foreground "cyan" :weight bold)) |
1853 | (((class grayscale) (background light)) (:foreground "LightGray" :bold t)) | |
55015061 SM |
1854 | (((class grayscale) (background dark)) (:foreground "DimGray" :bold t)) |
1855 | (((class color) (background light)) (:foreground "Purple")) | |
1856 | (((class color) (background dark)) (:foreground "Cyan")) | |
1857 | (t (:bold t))) | |
1858 | "Font Lock mode face used to highlight keywords." | |
2273c36b | 1859 | :group 'font-lock-highlighting-faces) |
55015061 SM |
1860 | |
1861 | (defface font-lock-builtin-face | |
e7f07c2c GM |
1862 | '((((type tty) (class color)) (:foreground "blue" :weight light)) |
1863 | (((class grayscale) (background light)) (:foreground "LightGray" :bold t)) | |
55015061 SM |
1864 | (((class grayscale) (background dark)) (:foreground "DimGray" :bold t)) |
1865 | (((class color) (background light)) (:foreground "Orchid")) | |
1866 | (((class color) (background dark)) (:foreground "LightSteelBlue")) | |
1867 | (t (:bold t))) | |
1868 | "Font Lock mode face used to highlight builtins." | |
2273c36b | 1869 | :group 'font-lock-highlighting-faces) |
55015061 SM |
1870 | |
1871 | (defface font-lock-function-name-face | |
e7f07c2c GM |
1872 | '((((type tty) (class color)) (:foreground "blue" :weight bold)) |
1873 | (((class color) (background light)) (:foreground "Blue")) | |
55015061 | 1874 | (((class color) (background dark)) (:foreground "LightSkyBlue")) |
ac6e572c | 1875 | (t (:inverse-video t :bold t))) |
55015061 | 1876 | "Font Lock mode face used to highlight function names." |
2273c36b | 1877 | :group 'font-lock-highlighting-faces) |
55015061 SM |
1878 | |
1879 | (defface font-lock-variable-name-face | |
e7f07c2c GM |
1880 | '((((type tty) (class color)) (:foreground "yellow" :weight light)) |
1881 | (((class grayscale) (background light)) | |
55015061 SM |
1882 | (:foreground "Gray90" :bold t :italic t)) |
1883 | (((class grayscale) (background dark)) | |
1884 | (:foreground "DimGray" :bold t :italic t)) | |
1885 | (((class color) (background light)) (:foreground "DarkGoldenrod")) | |
1886 | (((class color) (background dark)) (:foreground "LightGoldenrod")) | |
1887 | (t (:bold t :italic t))) | |
1888 | "Font Lock mode face used to highlight variable names." | |
2273c36b | 1889 | :group 'font-lock-highlighting-faces) |
55015061 SM |
1890 | |
1891 | (defface font-lock-type-face | |
e7f07c2c GM |
1892 | '((((type tty) (class color)) (:foreground "green")) |
1893 | (((class grayscale) (background light)) (:foreground "Gray90" :bold t)) | |
55015061 | 1894 | (((class grayscale) (background dark)) (:foreground "DimGray" :bold t)) |
46f26fcf | 1895 | (((class color) (background light)) (:foreground "ForestGreen")) |
55015061 SM |
1896 | (((class color) (background dark)) (:foreground "PaleGreen")) |
1897 | (t (:bold t :underline t))) | |
8acf2292 | 1898 | "Font Lock mode face used to highlight type and classes." |
2273c36b | 1899 | :group 'font-lock-highlighting-faces) |
55015061 | 1900 | |
8acf2292 | 1901 | (defface font-lock-constant-face |
e7f07c2c GM |
1902 | '((((type tty) (class color)) (:foreground "magenta")) |
1903 | (((class grayscale) (background light)) | |
55015061 SM |
1904 | (:foreground "LightGray" :bold t :underline t)) |
1905 | (((class grayscale) (background dark)) | |
1906 | (:foreground "Gray50" :bold t :underline t)) | |
1907 | (((class color) (background light)) (:foreground "CadetBlue")) | |
1908 | (((class color) (background dark)) (:foreground "Aquamarine")) | |
1909 | (t (:bold t :underline t))) | |
8acf2292 | 1910 | "Font Lock mode face used to highlight constants and labels." |
2273c36b | 1911 | :group 'font-lock-highlighting-faces) |
55015061 SM |
1912 | |
1913 | (defface font-lock-warning-face | |
e7f07c2c GM |
1914 | '((((type tty) (class color)) (:foreground "red")) |
1915 | (((class color) (background light)) (:foreground "Red" :bold t)) | |
55015061 | 1916 | (((class color) (background dark)) (:foreground "Pink" :bold t)) |
ac6e572c | 1917 | (t (:inverse-video t :bold t))) |
55015061 | 1918 | "Font Lock mode face used to highlight warnings." |
2273c36b | 1919 | :group 'font-lock-highlighting-faces) |
2d63aa67 SM |
1920 | |
1921 | ;;; End of Colour etc. support. | |
9bfbb130 | 1922 | \f |
56fcbd7e SM |
1923 | ;;; Menu support. |
1924 | ||
1925 | ;; This section of code is commented out because Emacs does not have real menu | |
1926 | ;; buttons. (We can mimic them by putting "( ) " or "(X) " at the beginning of | |
1927 | ;; the menu entry text, but with Xt it looks both ugly and embarrassingly | |
1928 | ;; amateur.) If/When Emacs gets real menus buttons, put in menu-bar.el after | |
1929 | ;; the entry for "Text Properties" something like: | |
1930 | ;; | |
1931 | ;; (define-key menu-bar-edit-menu [font-lock] | |
9c8de95c | 1932 | ;; (cons "Syntax Highlighting" font-lock-menu)) |
56fcbd7e SM |
1933 | ;; |
1934 | ;; and remove a single ";" from the beginning of each line in the rest of this | |
1935 | ;; section. Probably the mechanism for telling the menu code what are menu | |
1936 | ;; buttons and when they are on or off needs tweaking. I have assumed that the | |
1937 | ;; mechanism is via `menu-toggle' and `menu-selected' symbol properties. sm. | |
1938 | ||
1939 | ;;;;###autoload | |
1940 | ;(progn | |
1941 | ; ;; Make the Font Lock menu. | |
1942 | ; (defvar font-lock-menu (make-sparse-keymap "Syntax Highlighting")) | |
1943 | ; ;; Add the menu items in reverse order. | |
1944 | ; (define-key font-lock-menu [fontify-less] | |
1945 | ; '("Less In Current Buffer" . font-lock-fontify-less)) | |
1946 | ; (define-key font-lock-menu [fontify-more] | |
1947 | ; '("More In Current Buffer" . font-lock-fontify-more)) | |
1948 | ; (define-key font-lock-menu [font-lock-sep] | |
1949 | ; '("--")) | |
1950 | ; (define-key font-lock-menu [font-lock-mode] | |
1951 | ; '("In Current Buffer" . font-lock-mode)) | |
1952 | ; (define-key font-lock-menu [global-font-lock-mode] | |
1953 | ; '("In All Buffers" . global-font-lock-mode))) | |
1954 | ; | |
1955 | ;;;;###autoload | |
1956 | ;(progn | |
1957 | ; ;; We put the appropriate `menu-enable' etc. symbol property values on when | |
1958 | ; ;; font-lock.el is loaded, so we don't need to autoload the three variables. | |
1959 | ; (put 'global-font-lock-mode 'menu-toggle t) | |
1960 | ; (put 'font-lock-mode 'menu-toggle t) | |
1961 | ; (put 'font-lock-fontify-more 'menu-enable '(identity)) | |
1962 | ; (put 'font-lock-fontify-less 'menu-enable '(identity))) | |
1963 | ; | |
1964 | ;;; Put the appropriate symbol property values on now. See above. | |
9c8de95c | 1965 | ;(put 'global-font-lock-mode 'menu-selected 'global-font-lock-mode) |
56fcbd7e SM |
1966 | ;(put 'font-lock-mode 'menu-selected 'font-lock-mode) |
1967 | ;(put 'font-lock-fontify-more 'menu-enable '(nth 2 font-lock-fontify-level)) | |
1968 | ;(put 'font-lock-fontify-less 'menu-enable '(nth 1 font-lock-fontify-level)) | |
1969 | ; | |
1970 | ;(defvar font-lock-fontify-level nil) ; For less/more fontification. | |
1971 | ; | |
1972 | ;(defun font-lock-fontify-level (level) | |
1973 | ; (let ((font-lock-maximum-decoration level)) | |
1974 | ; (when font-lock-mode | |
1975 | ; (font-lock-mode)) | |
1976 | ; (font-lock-mode) | |
1977 | ; (when font-lock-verbose | |
1978 | ; (message "Fontifying %s... level %d" (buffer-name) level)))) | |
1979 | ; | |
1980 | ;(defun font-lock-fontify-less () | |
1981 | ; "Fontify the current buffer with less decoration. | |
1982 | ;See `font-lock-maximum-decoration'." | |
1983 | ; (interactive) | |
1984 | ; ;; Check in case we get called interactively. | |
1985 | ; (if (nth 1 font-lock-fontify-level) | |
1986 | ; (font-lock-fontify-level (1- (car font-lock-fontify-level))) | |
1987 | ; (error "No less decoration"))) | |
1988 | ; | |
1989 | ;(defun font-lock-fontify-more () | |
1990 | ; "Fontify the current buffer with more decoration. | |
1991 | ;See `font-lock-maximum-decoration'." | |
1992 | ; (interactive) | |
1993 | ; ;; Check in case we get called interactively. | |
1994 | ; (if (nth 2 font-lock-fontify-level) | |
1995 | ; (font-lock-fontify-level (1+ (car font-lock-fontify-level))) | |
1996 | ; (error "No more decoration"))) | |
1997 | ; | |
1998 | ;;; This should be called by `font-lock-set-defaults'. | |
1999 | ;(defun font-lock-set-menu () | |
2000 | ; ;; Activate less/more fontification entries if there are multiple levels for | |
2001 | ; ;; the current buffer. Sets `font-lock-fontify-level' to be of the form | |
2002 | ; ;; (CURRENT-LEVEL IS-LOWER-LEVEL-P IS-HIGHER-LEVEL-P) for menu activation. | |
2003 | ; (let ((keywords (or (nth 0 font-lock-defaults) | |
2004 | ; (nth 1 (assq major-mode font-lock-defaults-alist)))) | |
2005 | ; (level (font-lock-value-in-major-mode font-lock-maximum-decoration))) | |
2006 | ; (make-local-variable 'font-lock-fontify-level) | |
2007 | ; (if (or (symbolp keywords) (= (length keywords) 1)) | |
2008 | ; (font-lock-unset-menu) | |
2009 | ; (cond ((eq level t) | |
2010 | ; (setq level (1- (length keywords)))) | |
2011 | ; ((or (null level) (zerop level)) | |
2012 | ; ;; The default level is usually, but not necessarily, level 1. | |
2013 | ; (setq level (- (length keywords) | |
2014 | ; (length (member (eval (car keywords)) | |
2015 | ; (mapcar 'eval (cdr keywords)))))))) | |
2016 | ; (setq font-lock-fontify-level (list level (> level 1) | |
2017 | ; (< level (1- (length keywords)))))))) | |
2018 | ; | |
2019 | ;;; This should be called by `font-lock-unset-defaults'. | |
2020 | ;(defun font-lock-unset-menu () | |
2021 | ; ;; Deactivate less/more fontification entries. | |
2022 | ; (setq font-lock-fontify-level nil)) | |
2d63aa67 SM |
2023 | |
2024 | ;;; End of Menu support. | |
56fcbd7e | 2025 | \f |
9bfbb130 | 2026 | ;;; Various regexp information shared by several modes. |
a1eb1cf1 | 2027 | ;;; Information specific to a single mode should go in its load library. |
030f4a35 | 2028 | |
1c626aaf | 2029 | ;; Font Lock support for C, C++, Objective-C and Java modes will one day be in |
56fcbd7e SM |
2030 | ;; some cc-font.el (and required by cc-mode.el). However, the below function |
2031 | ;; should stay in font-lock.el, since it is used by other libraries. sm. | |
2e6a15fc | 2032 | |
c1f2ffc8 | 2033 | (defun font-lock-match-c-style-declaration-item-and-skip-to-next (limit) |
2e6a15fc | 2034 | "Match, and move over, any declaration/definition item after point. |
c1f2ffc8 SM |
2035 | Matches after point, but ignores leading whitespace and `*' characters. |
2036 | Does not move further than LIMIT. | |
2037 | ||
ac6e572c SM |
2038 | The expected syntax of a declaration/definition item is `word' (preceded by |
2039 | optional whitespace and `*' characters and proceeded by optional whitespace) | |
2040 | optionally followed by a `('. Everything following the item (but belonging to | |
2041 | it) is expected to by skip-able by `scan-sexps', and items are expected to be | |
2042 | separated with a `,' and to be terminated with a `;'. | |
c1f2ffc8 SM |
2043 | |
2044 | Thus the regexp matches after point: word ( | |
2045 | ^^^^ ^ | |
2046 | Where the match subexpressions are: 1 2 | |
2047 | ||
2048 | The item is delimited by (match-beginning 1) and (match-end 1). | |
2049 | If (match-beginning 2) is non-nil, the item is followed by a `('. | |
2050 | ||
2051 | This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item." | |
c1966bb4 | 2052 | (when (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?") |
8259bf10 SM |
2053 | (when (and (match-end 2) (> (- (match-end 2) (match-beginning 2)) 1)) |
2054 | ;; If `word' is followed by a double open-paren, it's probably | |
2055 | ;; a macro used for "int myfun P_ ((int arg1))". Let's go back one | |
2056 | ;; word to try and match `myfun' rather than `P_'. | |
2057 | (let ((pos (point))) | |
c1966bb4 | 2058 | (skip-chars-backward " \t\n") |
8259bf10 | 2059 | (skip-syntax-backward "w") |
c1966bb4 | 2060 | (unless (looking-at "\\(\\sw+\\)[ \t\n]*\\sw*_\\sw*[ \t\n]*\\((\\)?") |
8259bf10 SM |
2061 | ;; Looks like it was something else, so go back to where we |
2062 | ;; were and reset the match data by rematching. | |
2063 | (goto-char pos) | |
c1966bb4 | 2064 | (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?")))) |
2e6a15fc SM |
2065 | (save-match-data |
2066 | (condition-case nil | |
2067 | (save-restriction | |
c1966bb4 | 2068 | ;; Restrict to the LIMIT. |
2e6a15fc SM |
2069 | (narrow-to-region (point-min) limit) |
2070 | (goto-char (match-end 1)) | |
2071 | ;; Move over any item value, etc., to the next item. | |
c1966bb4 | 2072 | (while (not (looking-at "[ \t\n]*\\(\\(,\\)\\|;\\|\\'\\)")) |
2e6a15fc SM |
2073 | (goto-char (or (scan-sexps (point) 1) (point-max)))) |
2074 | (goto-char (match-end 2))) | |
2075 | (error t))))) | |
46f26fcf SM |
2076 | \f |
2077 | ;; Lisp. | |
2e6a15fc | 2078 | |
030f4a35 | 2079 | (defconst lisp-font-lock-keywords-1 |
2e6a15fc SM |
2080 | (eval-when-compile |
2081 | (list | |
2d63aa67 | 2082 | ;; |
55015061 SM |
2083 | ;; Definitions. |
2084 | (list (concat "(\\(def\\(" | |
2085 | ;; Function declarations. | |
7c5312b2 | 2086 | "\\(advice\\|varalias\\|alias\\|generic\\|macro\\*?\\|method\\|" |
4fffc071 | 2087 | "setf\\|subst\\*?\\|un\\*?\\|" |
1a5c5f2e | 2088 | "ine-\\(condition\\|\\(?:derived\\|minor\\)-mode\\|" |
4fffc071 | 2089 | "method-combination\\|setf-expander\\|skeleton\\|widget\\|" |
1a5c5f2e | 2090 | "function\\|\\(compiler\\|modify\\|symbol\\)-macro\\)\\)\\|" |
2e6a15fc | 2091 | ;; Variable declarations. |
4fffc071 | 2092 | "\\(const\\(ant\\)?\\|custom\\|face\\|parameter\\|var\\)\\|" |
2e6a15fc | 2093 | ;; Structure declarations. |
5bbc1cf7 | 2094 | "\\(class\\|group\\|package\\|struct\\|type\\)" |
2e6a15fc | 2095 | "\\)\\)\\>" |
55015061 | 2096 | ;; Any whitespace and defined object. |
2e6a15fc | 2097 | "[ \t'\(]*" |
11b42ef4 | 2098 | "\\(setf[ \t]+\\sw+)\\|\\sw+\\)?") |
2e6a15fc | 2099 | '(1 font-lock-keyword-face) |
ae659ad5 | 2100 | '(9 (cond ((match-beginning 3) font-lock-function-name-face) |
aaa15488 | 2101 | ((match-beginning 6) font-lock-variable-name-face) |
55015061 | 2102 | (t font-lock-type-face)) |
2e6a15fc | 2103 | nil t)) |
2d63aa67 SM |
2104 | ;; |
2105 | ;; Emacs Lisp autoload cookies. | |
731dd885 | 2106 | '("^;;;###\\(autoload\\)" 1 font-lock-warning-face prepend) |
2e6a15fc | 2107 | )) |
56fcbd7e | 2108 | "Subdued level highlighting for Lisp modes.") |
030f4a35 RS |
2109 | |
2110 | (defconst lisp-font-lock-keywords-2 | |
799761f0 | 2111 | (append lisp-font-lock-keywords-1 |
2e6a15fc SM |
2112 | (eval-when-compile |
2113 | (list | |
2114 | ;; | |
2d63aa67 | 2115 | ;; Control structures. Emacs Lisp forms. |
ac6e572c SM |
2116 | (cons (concat |
2117 | "(" (regexp-opt | |
4fffc071 | 2118 | '("cond" "if" "while" "let" "let*" |
ac6e572c | 2119 | "prog" "progn" "progv" "prog1" "prog2" "prog*" |
4fffc071 | 2120 | "inline" "lambda" "save-restriction" "save-excursion" |
ac6e572c SM |
2121 | "save-window-excursion" "save-selected-window" |
2122 | "save-match-data" "save-current-buffer" "unwind-protect" | |
9c8de95c | 2123 | "condition-case" "track-mouse" |
ac6e572c | 2124 | "eval-after-load" "eval-and-compile" "eval-when-compile" |
4fffc071 | 2125 | "eval-when" |
844a6a46 SM |
2126 | "with-current-buffer" "with-electric-help" |
2127 | "with-output-to-string" "with-output-to-temp-buffer" | |
79f238c9 | 2128 | "with-temp-buffer" "with-temp-file" "with-temp-message" |
844a6a46 | 2129 | "with-timeout") t) |
ac6e572c | 2130 | "\\>") |
2e6a15fc SM |
2131 | 1) |
2132 | ;; | |
2d63aa67 | 2133 | ;; Control structures. Common Lisp forms. |
ac6e572c SM |
2134 | (cons (concat |
2135 | "(" (regexp-opt | |
2136 | '("when" "unless" "case" "ecase" "typecase" "etypecase" | |
aaa15488 | 2137 | "ccase" "ctypecase" "handler-case" "handler-bind" |
ae659ad5 | 2138 | "restart-bind" "restart-case" "in-package" |
4fffc071 | 2139 | "cerror" "break" "ignore-errors" |
ae659ad5 | 2140 | "loop" "do" "do*" "dotimes" "dolist" "the" "locally" |
aaa15488 RS |
2141 | "proclaim" "declaim" "declare" "symbol-macrolet" |
2142 | "lexical-let" "lexical-let*" "flet" "labels" "compiler-let" | |
2143 | "destructuring-bind" "macrolet" "tagbody" "block" | |
ac6e572c SM |
2144 | "return" "return-from") t) |
2145 | "\\>") | |
2d63aa67 SM |
2146 | 1) |
2147 | ;; | |
4fffc071 SM |
2148 | ;; Exit/Feature symbols as constants. |
2149 | (list (concat "(\\(catch\\|throw\\|featurep\\|provide\\|require\\)\\>" | |
2150 | "[ \t']*\\(\\sw+\\)?") | |
2151 | '(1 font-lock-keyword-face) | |
2152 | '(2 font-lock-constant-face nil t)) | |
2153 | ;; | |
2154 | ;; Erroneous structures. | |
2155 | '("(\\(abort\\|assert\\|error\\|signal\\)\\>" 1 font-lock-warning-face) | |
2e6a15fc SM |
2156 | ;; |
2157 | ;; Words inside \\[] tend to be for `substitute-command-keys'. | |
8acf2292 | 2158 | '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-constant-face prepend) |
2e6a15fc SM |
2159 | ;; |
2160 | ;; Words inside `' tend to be symbol names. | |
8acf2292 | 2161 | '("`\\(\\sw\\sw+\\)'" 1 font-lock-constant-face prepend) |
2e6a15fc | 2162 | ;; |
9c8de95c | 2163 | ;; Constant values. |
c1f2ffc8 | 2164 | '("\\<:\\sw\\sw+\\>" 0 font-lock-builtin-face) |
2e6a15fc SM |
2165 | ;; |
2166 | ;; ELisp and CLisp `&' keywords as types. | |
f56f1421 | 2167 | '("\\&\\sw+\\>" . font-lock-type-face) |
5e9e032a SS |
2168 | ;; |
2169 | ;; CL `with-' and `do-' constructs | |
2170 | '("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face) | |
2e6a15fc | 2171 | ))) |
fb512de9 | 2172 | "Gaudy level highlighting for Lisp modes.") |
030f4a35 | 2173 | |
fb512de9 SM |
2174 | (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1 |
2175 | "Default expressions to highlight in Lisp modes.") | |
46f26fcf | 2176 | \f |
2e6a15fc | 2177 | ;;; User choices. |
030f4a35 | 2178 | |
c1f2ffc8 SM |
2179 | ;; These provide a means to fontify types not defined by the language. Those |
2180 | ;; types might be the user's own or they might be generally accepted and used. | |
55015061 | 2181 | ;; Generally accepted types are used to provide default variable values. |
c1f2ffc8 | 2182 | |
2273c36b SM |
2183 | (define-widget 'font-lock-extra-types-widget 'radio |
2184 | "Widget `:type' for members of the custom group `font-lock-extra-types'. | |
2185 | Members should `:load' the package `font-lock' to use this widget." | |
2186 | :args '((const :tag "none" nil) | |
46f26fcf | 2187 | (repeat :tag "types" regexp))) |
2273c36b | 2188 | |
31b482ee | 2189 | (defcustom c-font-lock-extra-types '("FILE" "\\sw+_t" "Lisp_Object") |
2e6a15fc | 2190 | "*List of extra types to fontify in C mode. |
55015061 | 2191 | Each list item should be a regexp not containing word-delimiters. |
c1f2ffc8 | 2192 | For example, a value of (\"FILE\" \"\\\\sw+_t\") means the word FILE and words |
55015061 SM |
2193 | ending in _t are treated as type names. |
2194 | ||
2273c36b SM |
2195 | The value of this variable is used when Font Lock mode is turned on." |
2196 | :type 'font-lock-extra-types-widget | |
2197 | :group 'font-lock-extra-types) | |
9bfbb130 | 2198 | |
b9dd672d | 2199 | (defcustom c++-font-lock-extra-types |
4fffc071 SM |
2200 | '("\\sw+_t" |
2201 | "\\([iof]\\|str\\)+stream\\(buf\\)?" "ios" | |
b9dd672d SM |
2202 | "string" "rope" |
2203 | "list" "slist" | |
2204 | "deque" "vector" "bit_vector" | |
2205 | "set" "multiset" | |
2206 | "map" "multimap" | |
2207 | "hash\\(_\\(m\\(ap\\|ulti\\(map\\|set\\)\\)\\|set\\)\\)?" | |
2208 | "stack" "queue" "priority_queue" | |
9dc09247 | 2209 | "type_info" |
4fffc071 SM |
2210 | "iterator" "const_iterator" "reverse_iterator" "const_reverse_iterator" |
2211 | "reference" "const_reference") | |
2e6a15fc | 2212 | "*List of extra types to fontify in C++ mode. |
55015061 | 2213 | Each list item should be a regexp not containing word-delimiters. |
b9dd672d SM |
2214 | For example, a value of (\"string\") means the word string is treated as a type |
2215 | name. | |
55015061 | 2216 | |
2273c36b SM |
2217 | The value of this variable is used when Font Lock mode is turned on." |
2218 | :type 'font-lock-extra-types-widget | |
2219 | :group 'font-lock-extra-types) | |
030f4a35 | 2220 | |
2273c36b | 2221 | (defcustom objc-font-lock-extra-types '("Class" "BOOL" "IMP" "SEL") |
2e6a15fc | 2222 | "*List of extra types to fontify in Objective-C mode. |
55015061 | 2223 | Each list item should be a regexp not containing word-delimiters. |
c1f2ffc8 | 2224 | For example, a value of (\"Class\" \"BOOL\" \"IMP\" \"SEL\") means the words |
55015061 SM |
2225 | Class, BOOL, IMP and SEL are treated as type names. |
2226 | ||
2273c36b SM |
2227 | The value of this variable is used when Font Lock mode is turned on." |
2228 | :type 'font-lock-extra-types-widget | |
2229 | :group 'font-lock-extra-types) | |
1bd50840 | 2230 | |
68c67d1f SM |
2231 | (defcustom java-font-lock-extra-types |
2232 | '("[A-Z\300-\326\330-\337]\\sw*[a-z]\\sw*") | |
2e6a15fc | 2233 | "*List of extra types to fontify in Java mode. |
55015061 | 2234 | Each list item should be a regexp not containing word-delimiters. |
68c67d1f | 2235 | For example, a value of (\"[A-Z\300-\326\330-\337]\\\\sw*[a-z]\\\\sw*\") means capitalised |
55015061 SM |
2236 | words (and words conforming to the Java id spec) are treated as type names. |
2237 | ||
2273c36b SM |
2238 | The value of this variable is used when Font Lock mode is turned on." |
2239 | :type 'font-lock-extra-types-widget | |
2240 | :group 'font-lock-extra-types) | |
2e6a15fc SM |
2241 | \f |
2242 | ;;; C. | |
c1f2ffc8 SM |
2243 | |
2244 | ;; [Murmur murmur murmur] Maestro, drum-roll please... [Murmur murmur murmur.] | |
2245 | ;; Ahem. [Murmur murmur murmur] Lay-dees an Gennel-men. [Murmur murmur shhh!] | |
2246 | ;; I am most proud and humbly honoured today [murmur murmur cough] to present | |
2247 | ;; to you good people, the winner of the Second Millennium Award for The Most | |
2248 | ;; Hairy Language Syntax. [Ahhh!] All rise please. [Shuffle shuffle | |
2249 | ;; shuffle.] And a round of applause please. For... The C Language! [Roar.] | |
2250 | ;; | |
2251 | ;; Thank you... You are too kind... It is with a feeling of great privilege | |
2252 | ;; and indeed emotion [sob] that I accept this award. It has been a long hard | |
2253 | ;; road. But we know our destiny. And our future. For we must not rest. | |
2254 | ;; There are more tokens to overload, more shoehorn, more methodologies. But | |
2255 | ;; more is a plus! [Ha ha ha.] And more means plus! [Ho ho ho.] The future | |
1c626aaf | 2256 | ;; is C++! [Ohhh!] The Third Millennium Award... Will be ours! [Roar.] |
c1f2ffc8 | 2257 | |
2d63aa67 | 2258 | (let* ((c-keywords |
ac6e572c SM |
2259 | (eval-when-compile |
2260 | (regexp-opt '("break" "continue" "do" "else" "for" "if" "return" | |
4fffc071 SM |
2261 | "switch" "while" "sizeof" |
2262 | ;; Type related, but we don't do anything special. | |
2263 | "typedef" "extern" "auto" "register" "static" | |
336911a1 SM |
2264 | "volatile" "const" |
2265 | ;; Dan Nicolaescu <done@gnu.org> says this is new. | |
fd612dd9 | 2266 | "restrict")))) |
4fffc071 SM |
2267 | (c-type-specs |
2268 | (eval-when-compile | |
fd612dd9 | 2269 | (regexp-opt '("enum" "struct" "union")))) |
4fffc071 SM |
2270 | (c-type-specs-depth |
2271 | (regexp-opt-depth c-type-specs)) | |
2272 | (c-type-names | |
2d63aa67 | 2273 | `(mapconcat 'identity |
019f00d8 | 2274 | (cons |
d2251bbf SM |
2275 | ,(eval-when-compile |
2276 | (regexp-opt | |
2277 | '("char" "short" "int" "long" "signed" "unsigned" | |
2278 | "float" "double" "void" "complex"))) | |
2d63aa67 SM |
2279 | c-font-lock-extra-types) |
2280 | "\\|")) | |
4fffc071 | 2281 | (c-type-names-depth |
d2251bbf | 2282 | `(regexp-opt-depth ,c-type-names)) |
76f5e2af GM |
2283 | (c-preprocessor-directives |
2284 | (eval-when-compile | |
2285 | (regexp-opt | |
2286 | '("define" "elif" "else" "endif" "error" "file" "if" "ifdef" | |
2287 | "ifndef" "include" "line" "pragma" "undef")))) | |
2288 | (c-preprocessor-directives-depth | |
d2251bbf | 2289 | (regexp-opt-depth c-preprocessor-directives))) |
8259bf10 | 2290 | (defconst c-font-lock-keywords-1 |
f60f18ae | 2291 | (list |
f60f18ae | 2292 | ;; |
9bfbb130 | 2293 | ;; These are all anchored at the beginning of line for speed. |
2e6a15fc | 2294 | ;; Note that `c++-font-lock-keywords-1' depends on `c-font-lock-keywords-1'. |
9bfbb130 SM |
2295 | ;; |
2296 | ;; Fontify function name definitions (GNU style; without type on line). | |
c1f2ffc8 SM |
2297 | '("^\\(\\sw+\\)[ \t]*(" 1 font-lock-function-name-face) |
2298 | ;; | |
2299 | ;; Fontify error directives. | |
2300 | '("^#[ \t]*error[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend) | |
9bfbb130 SM |
2301 | ;; |
2302 | ;; Fontify filenames in #include <...> preprocessor directives as strings. | |
9c8de95c | 2303 | '("^#[ \t]*\\(import\\|include\\)[ \t]*\\(<[^>\"\n]*>?\\)" |
2e6a15fc | 2304 | 2 font-lock-string-face) |
f60f18ae | 2305 | ;; |
a1eb1cf1 | 2306 | ;; Fontify function macro names. |
7d7d915a | 2307 | '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face) |
f60f18ae | 2308 | ;; |
5aa29679 RS |
2309 | ;; Fontify symbol names in #elif or #if ... defined preprocessor directives. |
2310 | '("^#[ \t]*\\(elif\\|if\\)\\>" | |
9bfbb130 | 2311 | ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil |
b9dd672d | 2312 | (1 font-lock-builtin-face) (2 font-lock-variable-name-face nil t))) |
9bfbb130 | 2313 | ;; |
a1eb1cf1 | 2314 | ;; Fontify otherwise as symbol names, and the preprocessor directive names. |
76f5e2af GM |
2315 | (list |
2316 | (concat "^#[ \t]*\\(" c-preprocessor-directives | |
2317 | "\\)\\>[ \t!]*\\(\\sw+\\)?") | |
2318 | '(1 font-lock-builtin-face) | |
2319 | (list (+ 2 c-preprocessor-directives-depth) | |
8259bf10 SM |
2320 | 'font-lock-variable-name-face nil t))) |
2321 | "Subdued level highlighting for C mode.") | |
f60f18ae | 2322 | |
8259bf10 | 2323 | (defconst c-font-lock-keywords-2 |
f60f18ae | 2324 | (append c-font-lock-keywords-1 |
030f4a35 | 2325 | (list |
030f4a35 | 2326 | ;; |
9bfbb130 | 2327 | ;; Simple regexps for speed. |
b89e1134 | 2328 | ;; |
4fffc071 | 2329 | ;; Fontify all type names. |
c1f2ffc8 | 2330 | `(eval . |
d2251bbf | 2331 | (cons (concat "\\<\\(" ,c-type-names "\\)\\>") 'font-lock-type-face)) |
030f4a35 | 2332 | ;; |
b89e1134 | 2333 | ;; Fontify all builtin keywords (except case, default and goto; see below). |
4fffc071 | 2334 | (concat "\\<\\(" c-keywords "\\|" c-type-specs "\\)\\>") |
030f4a35 | 2335 | ;; |
9bfbb130 | 2336 | ;; Fontify case/goto keywords and targets, and case default/goto tags. |
76f5e2af GM |
2337 | '("\\<\\(case\\|goto\\)\\>" |
2338 | (1 font-lock-keyword-face) | |
2339 | ("\\(-[0-9]+\\|\\sw+\\)" | |
2340 | ;; Return limit of search. | |
2341 | (save-excursion (skip-chars-forward "^:\n") (point)) | |
2342 | nil | |
2343 | (1 font-lock-constant-face nil t))) | |
8259bf10 SM |
2344 | ;; Anders Lindgren <andersl@andersl.com> points out that it is quicker |
2345 | ;; to use MATCH-ANCHORED to effectively anchor the regexp on the left. | |
2346 | ;; This must come after the one for keywords and targets. | |
2347 | '(":" ("\\(\\sw+\\)[ \t]*:" | |
2348 | (beginning-of-line) (end-of-line) | |
2349 | (1 font-lock-constant-face))) | |
2350 | )) | |
2351 | "Medium level highlighting for C mode. See also `c-font-lock-extra-types'.") | |
1bd50840 | 2352 | |
8259bf10 | 2353 | (defconst c-font-lock-keywords-3 |
9bfbb130 SM |
2354 | (append c-font-lock-keywords-2 |
2355 | ;; | |
2356 | ;; More complicated regexps for more complete highlighting for types. | |
2357 | ;; We still have to fontify type specifiers individually, as C is so hairy. | |
2358 | (list | |
2359 | ;; | |
4fffc071 | 2360 | ;; Fontify all storage types, plus their items. |
c1f2ffc8 | 2361 | `(eval . |
d2251bbf | 2362 | (list (concat "\\<\\(" ,c-type-names "\\)\\>" |
c1f2ffc8 SM |
2363 | "\\([ \t*&]+\\sw+\\>\\)*") |
2364 | ;; Fontify each declaration item. | |
2d63aa67 SM |
2365 | (list 'font-lock-match-c-style-declaration-item-and-skip-to-next |
2366 | ;; Start with point after all type specifiers. | |
4fffc071 SM |
2367 | (list 'goto-char (list 'or |
2368 | (list 'match-beginning | |
d2251bbf | 2369 | (+ ,c-type-names-depth 2)) |
2d63aa67 SM |
2370 | '(match-end 1))) |
2371 | ;; Finish with point after first type specifier. | |
2372 | '(goto-char (match-end 1)) | |
2373 | ;; Fontify as a variable or function name. | |
2374 | '(1 (if (match-beginning 2) | |
2375 | font-lock-function-name-face | |
2376 | font-lock-variable-name-face))))) | |
9bfbb130 | 2377 | ;; |
4fffc071 SM |
2378 | ;; Fontify all storage specs and types, plus their items. |
2379 | `(eval . | |
d2251bbf | 2380 | (list (concat "\\<\\(" ,c-type-specs "\\)\\>" |
4fffc071 | 2381 | "[ \t]*\\(\\sw+\\)?") |
8259bf10 SM |
2382 | (list 1 'font-lock-keyword-face) |
2383 | (list ,(+ c-type-specs-depth 2) 'font-lock-type-face nil t) | |
2384 | (list 'font-lock-match-c-style-declaration-item-and-skip-to-next | |
07fedaed | 2385 | nil |
b5e2c7a7 GM |
2386 | ;; Finish with point after the variable name if |
2387 | ;; there is one. | |
07fedaed | 2388 | `(if (match-end 2) |
b5e2c7a7 | 2389 | (goto-char (match-end 2))) |
4fffc071 SM |
2390 | ;; Fontify as a variable or function name. |
2391 | '(1 (if (match-beginning 2) | |
2392 | font-lock-function-name-face | |
2393 | font-lock-variable-name-face) nil t)))) | |
2394 | ;; | |
9bfbb130 SM |
2395 | ;; Fontify structures, or typedef names, plus their items. |
2396 | '("\\(}\\)[ \t*]*\\sw" | |
c1f2ffc8 | 2397 | (font-lock-match-c-style-declaration-item-and-skip-to-next |
9bfbb130 | 2398 | (goto-char (match-end 1)) nil |
4fffc071 | 2399 | (1 font-lock-type-face))) |
9bfbb130 SM |
2400 | ;; |
2401 | ;; Fontify anything at beginning of line as a declaration or definition. | |
2402 | '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*" | |
2403 | (1 font-lock-type-face) | |
c1f2ffc8 | 2404 | (font-lock-match-c-style-declaration-item-and-skip-to-next |
9bfbb130 | 2405 | (goto-char (or (match-beginning 2) (match-end 1))) nil |
c1f2ffc8 | 2406 | (1 (if (match-beginning 2) |
9bfbb130 SM |
2407 | font-lock-function-name-face |
2408 | font-lock-variable-name-face)))) | |
8259bf10 SM |
2409 | )) |
2410 | "Gaudy level highlighting for C mode. | |
2411 | See also `c-font-lock-extra-types'.")) | |
2412 | ||
2413 | (defun c-font-lock-syntactic-face-function (state) | |
2414 | (save-excursion | |
2415 | (if (nth 3 state) | |
2416 | ;; Check whether the string is properly terminated. | |
2417 | (let ((nstate (parse-partial-sexp (point) (line-end-position) | |
2418 | nil nil state 'syntax-table))) | |
2419 | (if (and (eolp) (not (nth 5 nstate)) (nth 3 nstate)) | |
2420 | ;; We're inside a string, at EOL and there was no \. | |
2421 | font-lock-warning-face | |
2422 | font-lock-string-face)) | |
2423 | (goto-char (nth 8 state)) | |
2424 | (if (looking-at "/\\*\\*\n") font-lock-doc-face font-lock-comment-face)))) | |
2e6a15fc SM |
2425 | |
2426 | (defvar c-font-lock-keywords c-font-lock-keywords-1 | |
2427 | "Default expressions to highlight in C mode. | |
2428 | See also `c-font-lock-extra-types'.") | |
2429 | \f | |
2430 | ;;; C++. | |
2431 | ||
c1f2ffc8 SM |
2432 | (defun font-lock-match-c++-style-declaration-item-and-skip-to-next (limit) |
2433 | ;; Regexp matches after point: word<word>::word ( | |
2434 | ;; ^^^^ ^^^^ ^^^^ ^ | |
2435 | ;; Where the match subexpressions are: 1 3 5 6 | |
2436 | ;; | |
2437 | ;; Item is delimited by (match-beginning 1) and (match-end 1). | |
2438 | ;; If (match-beginning 3) is non-nil, that part of the item incloses a `<>'. | |
2439 | ;; If (match-beginning 5) is non-nil, that part of the item follows a `::'. | |
2440 | ;; If (match-beginning 6) is non-nil, the item is followed by a `('. | |
2441 | (when (looking-at (eval-when-compile | |
55015061 SM |
2442 | (concat |
2443 | ;; Skip any leading whitespace. | |
2444 | "[ \t*&]*" | |
2445 | ;; This is `c++-type-spec' from below. (Hint hint!) | |
2446 | "\\(\\sw+\\)" ; The instance? | |
8259bf10 | 2447 | "\\([ \t]*<\\(\\(?:[^<>\n]\\|<[^>\n]+>\\)+\\)[ \t*&]*>\\)?" ; Or template? |
844a6a46 | 2448 | "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)*" ; Or member? |
55015061 SM |
2449 | ;; Match any trailing parenthesis. |
2450 | "[ \t]*\\((\\)?"))) | |
c1f2ffc8 SM |
2451 | (save-match-data |
2452 | (condition-case nil | |
2453 | (save-restriction | |
2454 | ;; Restrict to the end of line, currently guaranteed to be LIMIT. | |
2455 | (narrow-to-region (point-min) limit) | |
2456 | (goto-char (match-end 1)) | |
2457 | ;; Move over any item value, etc., to the next item. | |
2458 | (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)")) | |
2459 | (goto-char (or (scan-sexps (point) 1) (point-max)))) | |
2460 | (goto-char (match-end 2))) | |
2461 | (error t))))) | |
2462 | ||
76f5e2af GM |
2463 | (defun font-lock-match-c++-structor-declaration (limit) |
2464 | ;; Match C++ constructors and destructors inside class declarations. | |
2465 | (let ((res nil) | |
2466 | (regexp (concat "^\\s-+\\(\\(virtual\\|explicit\\)\\s-+\\)*~?\\(\\<" | |
2467 | (mapconcat 'identity | |
2468 | c++-font-lock-extra-types "\\|") | |
2469 | "\\>\\)\\s-*(" | |
2470 | ;; Don't match function pointer declarations, e.g.: | |
2471 | ;; Foo (*fptr)(); | |
2472 | "\\s-*[^*( \t]"))) | |
2473 | (while (progn (setq res (re-search-forward regexp limit t)) | |
2474 | (and res | |
2475 | (save-excursion | |
2476 | (beginning-of-line) | |
2477 | (save-match-data | |
2478 | (not (vectorp (c-at-toplevel-p)))))))) | |
2479 | res)) | |
2480 | ||
c1f2ffc8 | 2481 | (let* ((c++-keywords |
ac6e572c SM |
2482 | (eval-when-compile |
2483 | (regexp-opt | |
2484 | '("break" "continue" "do" "else" "for" "if" "return" "switch" | |
e0e277ff | 2485 | "while" "asm" "catch" "delete" "new" "sizeof" "this" "throw" "try" |
5d8b66eb | 2486 | "typeid" |
4fffc071 SM |
2487 | ;; Branko Cibej <branko.cibej@hermes.si> says this is new. |
2488 | "export" | |
2489 | ;; Mark Mitchell <mmitchell@usa.net> says these are new. | |
2490 | "mutable" "explicit" | |
2491 | ;; Alain Picard <ap@abelard.apana.org.au> suggests treating these | |
2492 | ;; as keywords not types. | |
2493 | "typedef" "template" | |
2494 | "extern" "auto" "register" "const" "volatile" "static" | |
7e5d8879 GM |
2495 | "inline" "friend" "virtual" |
2496 | ;; Standard C++ operator names. | |
2497 | "and" "and_eq" "bitand" "bitor" "compl" "not" "not_eq" | |
2498 | "or" "or_eq" "xor" "xor_eq")))) | |
c1f2ffc8 | 2499 | (c++-operators |
23a0e11b SM |
2500 | (eval-when-compile |
2501 | (regexp-opt | |
2502 | ;; Taken from Stroustrup, minus keywords otherwise fontified. | |
2503 | '("+" "-" "*" "/" "%" "^" "&" "|" "~" "!" "=" "<" ">" "+=" "-=" | |
2504 | "*=" "/=" "%=" "^=" "&=" "|=" "<<" ">>" ">>=" "<<=" "==" "!=" | |
2505 | "<=" ">=" "&&" "||" "++" "--" "->*" "," "->" "[]" "()")))) | |
4fffc071 SM |
2506 | (c++-type-specs |
2507 | (eval-when-compile | |
2508 | (regexp-opt | |
2509 | '("class" "public" "private" "protected" "typename" | |
2510 | "struct" "union" "enum" "namespace" "using" | |
2511 | ;; Eric Hopper <hopper@omnifarious.mn.org> says these are new. | |
2512 | "static_cast" "dynamic_cast" "const_cast" "reinterpret_cast") t))) | |
2513 | (c++-type-specs-depth | |
2514 | (regexp-opt-depth c++-type-specs)) | |
2515 | (c++-type-names | |
c1f2ffc8 | 2516 | `(mapconcat 'identity |
019f00d8 | 2517 | (cons |
d2251bbf SM |
2518 | ,(eval-when-compile |
2519 | (regexp-opt | |
2520 | '("signed" "unsigned" "short" "long" | |
2521 | "int" "char" "float" "double" "void" | |
2522 | "bool" "complex"))) | |
c1f2ffc8 SM |
2523 | c++-font-lock-extra-types) |
2524 | "\\|")) | |
d2251bbf | 2525 | (c++-type-names-depth `(regexp-opt-depth ,c++-type-names)) |
55015061 SM |
2526 | ;; |
2527 | ;; A brave attempt to match templates following a type and/or match | |
2528 | ;; class membership. See and sync the above function | |
2529 | ;; `font-lock-match-c++-style-declaration-item-and-skip-to-next'. | |
8259bf10 | 2530 | (c++-type-suffix (concat "\\([ \t]*<\\(\\(?:[^<>\n]\\|<[^>\n]+>\\)+\\)[ \t*&]*>\\)?" |
844a6a46 | 2531 | "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)*")) |
4fffc071 | 2532 | (c++-type-suffix-depth (regexp-opt-depth c++-type-suffix)) |
55015061 SM |
2533 | ;; If the string is a type, it may be followed by the cruft above. |
2534 | (c++-type-spec (concat "\\(\\sw+\\)\\>" c++-type-suffix)) | |
4fffc071 | 2535 | (c++-type-spec-depth (regexp-opt-depth c++-type-spec)) |
55015061 SM |
2536 | ;; |
2537 | ;; Parenthesis depth of user-defined types not forgetting their cruft. | |
ac6e572c | 2538 | (c++-type-depth `(regexp-opt-depth |
d2251bbf | 2539 | (concat ,c++-type-names ,c++-type-suffix))) |
2d63aa67 | 2540 | ) |
8259bf10 | 2541 | (defconst c++-font-lock-keywords-1 |
9bfbb130 SM |
2542 | (append |
2543 | ;; | |
2544 | ;; The list `c-font-lock-keywords-1' less that for function names. | |
2545 | (cdr c-font-lock-keywords-1) | |
9bfbb130 | 2546 | (list |
2e6a15fc | 2547 | ;; |
c1f2ffc8 SM |
2548 | ;; Fontify function name definitions, possibly incorporating class names. |
2549 | (list (concat "^" c++-type-spec "[ \t]*(") | |
2550 | '(1 (if (or (match-beginning 2) (match-beginning 4)) | |
2551 | font-lock-type-face | |
2552 | font-lock-function-name-face)) | |
4fffc071 | 2553 | '(3 font-lock-type-face nil t) |
c1f2ffc8 | 2554 | '(5 font-lock-function-name-face nil t)) |
8259bf10 SM |
2555 | )) |
2556 | "Subdued level highlighting for C++ mode.") | |
9bfbb130 | 2557 | |
8259bf10 | 2558 | (defconst c++-font-lock-keywords-2 |
b89e1134 | 2559 | (append c++-font-lock-keywords-1 |
a1eb1cf1 | 2560 | (list |
9bfbb130 SM |
2561 | ;; |
2562 | ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading. | |
c1f2ffc8 | 2563 | `(eval . |
d2251bbf | 2564 | (cons (concat "\\<\\(" ,c++-type-names "\\)\\>") |
c1f2ffc8 | 2565 | 'font-lock-type-face)) |
9bfbb130 | 2566 | ;; |
c1f2ffc8 SM |
2567 | ;; Fontify operator overloading. |
2568 | (list (concat "\\<\\(operator\\)\\>[ \t]*\\(" c++-operators "\\)?") | |
2569 | '(1 font-lock-keyword-face) | |
2570 | '(2 font-lock-builtin-face nil t)) | |
9bfbb130 SM |
2571 | ;; |
2572 | ;; Fontify case/goto keywords and targets, and case default/goto tags. | |
76f5e2af GM |
2573 | '("\\<\\(case\\|goto\\)\\>" |
2574 | (1 font-lock-keyword-face) | |
2575 | ("\\(-[0-9]+\\|\\sw+\\)[ \t]*\\(::\\)?" | |
2576 | ;; Return limit of search. | |
2577 | (save-excursion | |
2578 | (while (progn | |
2579 | (skip-chars-forward "^:\n") | |
2580 | (looking-at "::")) | |
2581 | (forward-char 2)) | |
2582 | (point)) | |
2583 | nil | |
2584 | (1 (if (match-beginning 2) | |
2585 | font-lock-type-face | |
2586 | font-lock-constant-face) nil t))) | |
e0e277ff | 2587 | ;; This must come after the one for keywords and targets. |
2e6a15fc SM |
2588 | '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:\\($\\|[^:]\\)" |
2589 | (beginning-of-line) (end-of-line) | |
8acf2292 | 2590 | (1 font-lock-constant-face))) |
9bfbb130 SM |
2591 | ;; |
2592 | ;; Fontify other builtin keywords. | |
4fffc071 | 2593 | (concat "\\<\\(" c++-keywords "\\|" c++-type-specs "\\)\\>") |
2e6a15fc SM |
2594 | ;; |
2595 | ;; Eric Hopper <hopper@omnifarious.mn.org> says `true' and `false' are new. | |
8acf2292 | 2596 | '("\\<\\(false\\|true\\)\\>" . font-lock-constant-face) |
8259bf10 SM |
2597 | )) |
2598 | "Medium level highlighting for C++ mode. | |
2599 | See also `c++-font-lock-extra-types'.") | |
9bfbb130 | 2600 | |
8259bf10 | 2601 | (defconst c++-font-lock-keywords-3 |
9bfbb130 SM |
2602 | (append c++-font-lock-keywords-2 |
2603 | ;; | |
2604 | ;; More complicated regexps for more complete highlighting for types. | |
2605 | (list | |
2606 | ;; | |
2607 | ;; Fontify all storage classes and type specifiers, plus their items. | |
c1f2ffc8 | 2608 | `(eval . |
d2251bbf SM |
2609 | (list (concat "\\<\\(" ,c++-type-names "\\)\\>" ,c++-type-suffix |
2610 | "\\([ \t*&]+" ,c++-type-spec "\\)*") | |
4fffc071 | 2611 | ;; The name of any template type. |
d2251bbf | 2612 | (list (+ ,c++-type-names-depth 3) 'font-lock-type-face nil t) |
c1f2ffc8 | 2613 | ;; Fontify each declaration item. |
2d63aa67 SM |
2614 | (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next |
2615 | ;; Start with point after all type specifiers. | |
2616 | (list 'goto-char (list 'or (list 'match-beginning | |
d2251bbf | 2617 | (+ ,c++-type-depth 2)) |
2d63aa67 SM |
2618 | '(match-end 1))) |
2619 | ;; Finish with point after first type specifier. | |
2620 | '(goto-char (match-end 1)) | |
2621 | ;; Fontify as a variable or function name. | |
2622 | '(1 (cond ((or (match-beginning 2) (match-beginning 4)) | |
2623 | font-lock-type-face) | |
68c67d1f SM |
2624 | ((and (match-beginning 6) (c-at-toplevel-p)) |
2625 | font-lock-function-name-face) | |
2626 | (t | |
2627 | font-lock-variable-name-face))) | |
4fffc071 | 2628 | '(3 font-lock-type-face nil t) |
2d63aa67 SM |
2629 | '(5 (if (match-beginning 6) |
2630 | font-lock-function-name-face | |
2631 | font-lock-variable-name-face) nil t)))) | |
9bfbb130 | 2632 | ;; |
4fffc071 SM |
2633 | ;; Fontify all storage specs and types, plus their items. |
2634 | `(eval . | |
d2251bbf SM |
2635 | (list (concat "\\<" ,c++-type-specs "\\>" ,c++-type-suffix |
2636 | "[ \t]*\\(" ,c++-type-spec "\\)?") | |
4fffc071 | 2637 | ;; The name of any template type. |
d2251bbf | 2638 | (list ,(+ c++-type-specs-depth 2) 'font-lock-type-face nil t) |
4fffc071 | 2639 | ;; The name of any type. |
d2251bbf | 2640 | (list (+ ,c++-type-specs-depth ,c++-type-suffix-depth 2) |
4fffc071 SM |
2641 | 'font-lock-type-face nil t) |
2642 | ;; Fontify each declaration item. | |
2643 | (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next | |
2644 | ;; Start with point after all type specifiers. | |
2645 | nil | |
2646 | ;; Finish with point after first type specifier. | |
2647 | nil | |
2648 | ;; Fontify as a variable or function name. | |
2649 | '(1 (cond ((or (match-beginning 2) (match-beginning 4)) | |
2650 | font-lock-type-face) | |
68c67d1f SM |
2651 | ((and (match-beginning 6) (c-at-toplevel-p)) |
2652 | font-lock-function-name-face) | |
2653 | (t | |
2654 | font-lock-variable-name-face))) | |
4fffc071 SM |
2655 | '(3 font-lock-type-face nil t) |
2656 | '(5 (if (match-beginning 6) | |
2657 | font-lock-function-name-face | |
2658 | font-lock-variable-name-face) nil t)) | |
2659 | )) | |
2660 | ;; | |
9bfbb130 SM |
2661 | ;; Fontify structures, or typedef names, plus their items. |
2662 | '("\\(}\\)[ \t*]*\\sw" | |
2663 | (font-lock-match-c++-style-declaration-item-and-skip-to-next | |
2664 | (goto-char (match-end 1)) nil | |
4fffc071 | 2665 | (1 font-lock-type-face))) |
9bfbb130 SM |
2666 | ;; |
2667 | ;; Fontify anything at beginning of line as a declaration or definition. | |
c1f2ffc8 SM |
2668 | (list (concat "^\\(" c++-type-spec "[ \t*&]*\\)+") |
2669 | '(font-lock-match-c++-style-declaration-item-and-skip-to-next | |
2670 | (goto-char (match-beginning 1)) | |
2671 | (goto-char (match-end 1)) | |
2672 | (1 (cond ((or (match-beginning 2) (match-beginning 4)) | |
2673 | font-lock-type-face) | |
2674 | ((match-beginning 6) font-lock-function-name-face) | |
2675 | (t font-lock-variable-name-face))) | |
4fffc071 | 2676 | (3 font-lock-type-face nil t) |
c1f2ffc8 SM |
2677 | (5 (if (match-beginning 6) |
2678 | font-lock-function-name-face | |
2679 | font-lock-variable-name-face) nil t))) | |
76f5e2af GM |
2680 | ;; |
2681 | ;; Fontify constructors and destructors inside class declarations. | |
2682 | '(font-lock-match-c++-structor-declaration | |
2683 | (3 font-lock-function-name-face t)) | |
8259bf10 SM |
2684 | )) |
2685 | "Gaudy level highlighting for C++ mode. | |
2686 | See also `c++-font-lock-extra-types'.") | |
f60f18ae | 2687 | ) |
030f4a35 | 2688 | |
fb512de9 | 2689 | (defvar c++-font-lock-keywords c++-font-lock-keywords-1 |
2e6a15fc SM |
2690 | "Default expressions to highlight in C++ mode. |
2691 | See also `c++-font-lock-extra-types'.") | |
2692 | \f | |
2693 | ;;; Objective-C. | |
2694 | ||
2e6a15fc SM |
2695 | ;; Regexps written with help from Stephen Peters <speters@us.oracle.com> and |
2696 | ;; Jacques Duthen Prestataire <duthen@cegelec-red.fr>. | |
2d63aa67 | 2697 | (let* ((objc-keywords |
ac6e572c SM |
2698 | (eval-when-compile |
2699 | (regexp-opt '("break" "continue" "do" "else" "for" "if" "return" | |
4fffc071 SM |
2700 | "switch" "while" "sizeof" "self" "super" |
2701 | "typedef" "auto" "extern" "static" | |
fd612dd9 | 2702 | "volatile" "const")))) |
4fffc071 SM |
2703 | (objc-type-specs |
2704 | (eval-when-compile | |
2705 | (regexp-opt | |
2706 | '("register" "struct" "union" "enum" | |
2707 | "oneway" "in" "out" "inout" "bycopy" "byref") t))) | |
2708 | (objc-type-specs-depth | |
2709 | (regexp-opt-depth objc-type-specs)) | |
2710 | (objc-type-names | |
2d63aa67 SM |
2711 | `(mapconcat 'identity |
2712 | (cons | |
d2251bbf SM |
2713 | ,(eval-when-compile |
2714 | (regexp-opt | |
2715 | '("signed" "unsigned" "short" "long" | |
2716 | "int" "char" "float" "double" "void" | |
2717 | "id"))) | |
2d63aa67 SM |
2718 | objc-font-lock-extra-types) |
2719 | "\\|")) | |
4fffc071 | 2720 | (objc-type-names-depth |
d2251bbf | 2721 | `(regexp-opt-depth ,objc-type-names)) |
2d63aa67 | 2722 | ) |
8259bf10 | 2723 | (defconst objc-font-lock-keywords-1 |
2e6a15fc SM |
2724 | (append |
2725 | ;; | |
2726 | ;; The list `c-font-lock-keywords-1' less that for function names. | |
2727 | (cdr c-font-lock-keywords-1) | |
2728 | (list | |
2729 | ;; | |
2730 | ;; Fontify compiler directives. | |
c1f2ffc8 SM |
2731 | '("@\\(\\sw+\\)\\>" |
2732 | (1 font-lock-keyword-face) | |
4fffc071 SM |
2733 | ("\\=[ \t:<,]*\\(\\sw+\\)" nil nil |
2734 | (1 font-lock-type-face))) | |
2e6a15fc SM |
2735 | ;; |
2736 | ;; Fontify method names and arguments. Oh Lordy! | |
2737 | ;; First, on the same line as the function declaration. | |
4fffc071 SM |
2738 | '("^[+-][ \t]*\\(PRIVATE\\>\\)?[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)" |
2739 | (1 font-lock-keyword-face nil t) | |
2740 | (3 font-lock-function-name-face) | |
2741 | ("\\=[ \t]*\\(\\sw+\\)?:[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)" | |
2e6a15fc SM |
2742 | nil nil |
2743 | (1 font-lock-function-name-face nil t) | |
4fffc071 | 2744 | (3 font-lock-variable-name-face))) |
2e6a15fc | 2745 | ;; Second, on lines following the function declaration. |
4fffc071 | 2746 | '(":" ("^[ \t]*\\(\\sw+\\)?:[ \t]*\\(([^)\n]+)\\)?[ \t]*\\(\\sw+\\)" |
2e6a15fc SM |
2747 | (beginning-of-line) (end-of-line) |
2748 | (1 font-lock-function-name-face nil t) | |
4fffc071 | 2749 | (3 font-lock-variable-name-face))) |
8259bf10 SM |
2750 | )) |
2751 | "Subdued level highlighting for Objective-C mode.") | |
030f4a35 | 2752 | |
8259bf10 | 2753 | (defconst objc-font-lock-keywords-2 |
2e6a15fc SM |
2754 | (append objc-font-lock-keywords-1 |
2755 | (list | |
2756 | ;; | |
2757 | ;; Simple regexps for speed. | |
2758 | ;; | |
2759 | ;; Fontify all type specifiers. | |
c1f2ffc8 | 2760 | `(eval . |
d2251bbf | 2761 | (cons (concat "\\<\\(" ,objc-type-names "\\)\\>") |
c1f2ffc8 | 2762 | 'font-lock-type-face)) |
2e6a15fc SM |
2763 | ;; |
2764 | ;; Fontify all builtin keywords (except case, default and goto; see below). | |
4fffc071 | 2765 | (concat "\\<\\(" objc-keywords "\\|" objc-type-specs "\\)\\>") |
2e6a15fc SM |
2766 | ;; |
2767 | ;; Fontify case/goto keywords and targets, and case default/goto tags. | |
56fcbd7e | 2768 | '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?" |
8acf2292 | 2769 | (1 font-lock-keyword-face) (2 font-lock-constant-face nil t)) |
2e6a15fc | 2770 | ;; Fontify tags iff sole statement on line, otherwise we detect selectors. |
e0e277ff | 2771 | ;; This must come after the one for keywords and targets. |
2e6a15fc SM |
2772 | '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$" |
2773 | (beginning-of-line) (end-of-line) | |
8acf2292 | 2774 | (1 font-lock-constant-face))) |
2e6a15fc SM |
2775 | ;; |
2776 | ;; Fontify null object pointers. | |
8acf2292 | 2777 | '("\\<[Nn]il\\>" . font-lock-constant-face) |
8259bf10 SM |
2778 | )) |
2779 | "Medium level highlighting for Objective-C mode. | |
2780 | See also `objc-font-lock-extra-types'.") | |
d46c21ec | 2781 | |
8259bf10 | 2782 | (defconst objc-font-lock-keywords-3 |
2e6a15fc SM |
2783 | (append objc-font-lock-keywords-2 |
2784 | ;; | |
2785 | ;; More complicated regexps for more complete highlighting for types. | |
2786 | ;; We still have to fontify type specifiers individually, as C is so hairy. | |
2787 | (list | |
2788 | ;; | |
2789 | ;; Fontify all storage classes and type specifiers, plus their items. | |
c1f2ffc8 | 2790 | `(eval . |
d2251bbf | 2791 | (list (concat "\\<\\(" ,objc-type-names "\\)\\>" |
c1f2ffc8 SM |
2792 | "\\([ \t*&]+\\sw+\\>\\)*") |
2793 | ;; Fontify each declaration item. | |
2d63aa67 SM |
2794 | (list 'font-lock-match-c-style-declaration-item-and-skip-to-next |
2795 | ;; Start with point after all type specifiers. | |
4fffc071 SM |
2796 | (list 'goto-char |
2797 | (list 'or (list 'match-beginning | |
d2251bbf | 2798 | (+ ,objc-type-names-depth 2)) |
4fffc071 | 2799 | '(match-end 1))) |
2d63aa67 SM |
2800 | ;; Finish with point after first type specifier. |
2801 | '(goto-char (match-end 1)) | |
2802 | ;; Fontify as a variable or function name. | |
2803 | '(1 (if (match-beginning 2) | |
2804 | font-lock-function-name-face | |
2805 | font-lock-variable-name-face))))) | |
2e6a15fc | 2806 | ;; |
4fffc071 SM |
2807 | ;; Fontify all storage specs and types, plus their items. |
2808 | `(eval . | |
d2251bbf | 2809 | (list (concat "\\<\\(" ,objc-type-specs "[ \t]*\\)+\\>" |
4fffc071 SM |
2810 | "[ \t]*\\(\\sw+\\)?") |
2811 | ;; The name of any type. | |
d2251bbf | 2812 | (list ,(+ objc-type-specs-depth 2) 'font-lock-type-face nil t) |
4fffc071 SM |
2813 | ;; Fontify each declaration item. |
2814 | (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next | |
2815 | nil nil | |
2816 | ;; Fontify as a variable or function name. | |
2817 | '(1 (if (match-beginning 2) | |
2818 | font-lock-function-name-face | |
2819 | font-lock-variable-name-face))) | |
2820 | )) | |
2821 | ;; | |
2e6a15fc SM |
2822 | ;; Fontify structures, or typedef names, plus their items. |
2823 | '("\\(}\\)[ \t*]*\\sw" | |
c1f2ffc8 | 2824 | (font-lock-match-c-style-declaration-item-and-skip-to-next |
2e6a15fc | 2825 | (goto-char (match-end 1)) nil |
4fffc071 | 2826 | (1 font-lock-type-face))) |
2e6a15fc SM |
2827 | ;; |
2828 | ;; Fontify anything at beginning of line as a declaration or definition. | |
2829 | '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*" | |
2830 | (1 font-lock-type-face) | |
c1f2ffc8 | 2831 | (font-lock-match-c-style-declaration-item-and-skip-to-next |
2e6a15fc | 2832 | (goto-char (or (match-beginning 2) (match-end 1))) nil |
c1f2ffc8 | 2833 | (1 (if (match-beginning 2) |
2e6a15fc SM |
2834 | font-lock-function-name-face |
2835 | font-lock-variable-name-face)))) | |
8259bf10 SM |
2836 | )) |
2837 | "Gaudy level highlighting for Objective-C mode. | |
2838 | See also `objc-font-lock-extra-types'.") | |
2e6a15fc SM |
2839 | ) |
2840 | ||
2841 | (defvar objc-font-lock-keywords objc-font-lock-keywords-1 | |
2842 | "Default expressions to highlight in Objective-C mode. | |
2843 | See also `objc-font-lock-extra-types'.") | |
2844 | \f | |
2845 | ;;; Java. | |
2846 | ||
4d80b2cc SM |
2847 | ;; Regexps written with help from Fred White <fwhite@bbn.com>, |
2848 | ;; Anders Lindgren <andersl@andersl.com> and Carl Manning <caroma@ai.mit.edu>. | |
2d63aa67 | 2849 | (let* ((java-keywords |
ac6e572c SM |
2850 | (eval-when-compile |
2851 | (regexp-opt | |
2852 | '("catch" "do" "else" "super" "this" "finally" "for" "if" | |
68c67d1f | 2853 | ;; Anders Lindgren <andersl@andersl.com> says these have gone. |
ac6e572c SM |
2854 | ;; "cast" "byvalue" "future" "generic" "operator" "var" |
2855 | ;; "inner" "outer" "rest" | |
68c67d1f | 2856 | "implements" "extends" "throws" "instanceof" "new" |
fd612dd9 | 2857 | "interface" "return" "switch" "throw" "try" "while")))) |
2d63aa67 | 2858 | ;; |
68c67d1f SM |
2859 | ;; Classes immediately followed by an object name. |
2860 | (java-type-names | |
2861 | `(mapconcat 'identity | |
8259bf10 | 2862 | (cons |
d2251bbf SM |
2863 | ,(eval-when-compile |
2864 | (regexp-opt '("boolean" "char" "byte" "short" "int" "long" | |
2865 | "float" "double" "void"))) | |
68c67d1f SM |
2866 | java-font-lock-extra-types) |
2867 | "\\|")) | |
d2251bbf | 2868 | (java-type-names-depth `(regexp-opt-depth ,java-type-names)) |
2d63aa67 SM |
2869 | ;; |
2870 | ;; These are eventually followed by an object name. | |
68c67d1f | 2871 | (java-type-specs |
ac6e572c SM |
2872 | (eval-when-compile |
2873 | (regexp-opt | |
2874 | '("abstract" "const" "final" "synchronized" "transient" "static" | |
68c67d1f | 2875 | ;; Anders Lindgren <andersl@andersl.com> says this has gone. |
ac6e572c | 2876 | ;; "threadsafe" |
b09f207a SM |
2877 | "volatile" "public" "private" "protected" "native" |
2878 | ;; Carl Manning <caroma@ai.mit.edu> says this is new. | |
2879 | "strictfp")))) | |
ac6e572c | 2880 | ) |
8259bf10 | 2881 | (defconst java-font-lock-keywords-1 |
2e6a15fc SM |
2882 | (list |
2883 | ;; | |
2884 | ;; Fontify class names. | |
2885 | '("\\<\\(class\\)\\>[ \t]*\\(\\sw+\\)?" | |
4d80b2cc | 2886 | (1 font-lock-keyword-face) (2 font-lock-type-face nil t)) |
2e6a15fc SM |
2887 | ;; |
2888 | ;; Fontify package names in import directives. | |
2889 | '("\\<\\(import\\|package\\)\\>[ \t]*\\(\\sw+\\)?" | |
68c67d1f SM |
2890 | (1 font-lock-keyword-face) |
2891 | (2 font-lock-constant-face nil t) | |
2892 | ("\\=\\.\\(\\*\\|\\sw+\\)" nil nil | |
2893 | (1 font-lock-constant-face nil t))) | |
8259bf10 SM |
2894 | ) |
2895 | "Subdued level highlighting for Java mode.") | |
2e6a15fc | 2896 | |
8259bf10 | 2897 | (defconst java-font-lock-keywords-2 |
2e6a15fc SM |
2898 | (append java-font-lock-keywords-1 |
2899 | (list | |
2900 | ;; | |
68c67d1f SM |
2901 | ;; Fontify class names. |
2902 | `(eval . | |
d2251bbf | 2903 | (cons (concat "\\<\\(" ,java-type-names "\\)\\>[^.]") |
4d80b2cc | 2904 | '(1 font-lock-type-face))) |
2e6a15fc SM |
2905 | ;; |
2906 | ;; Fontify all builtin keywords (except below). | |
68c67d1f | 2907 | (concat "\\<\\(" java-keywords "\\|" java-type-specs "\\)\\>") |
2e6a15fc SM |
2908 | ;; |
2909 | ;; Fontify keywords and targets, and case default/goto tags. | |
56fcbd7e | 2910 | (list "\\<\\(break\\|case\\|continue\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?" |
8acf2292 | 2911 | '(1 font-lock-keyword-face) '(2 font-lock-constant-face nil t)) |
e0e277ff | 2912 | ;; This must come after the one for keywords and targets. |
4d80b2cc | 2913 | '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$" |
2e6a15fc | 2914 | (beginning-of-line) (end-of-line) |
8acf2292 | 2915 | (1 font-lock-constant-face))) |
2e6a15fc | 2916 | ;; |
2e6a15fc | 2917 | ;; Fontify all constants. |
8acf2292 | 2918 | '("\\<\\(false\\|null\\|true\\)\\>" . font-lock-constant-face) |
2e6a15fc SM |
2919 | ;; |
2920 | ;; Javadoc tags within comments. | |
32038c7a GM |
2921 | (list |
2922 | (concat "@\\(" | |
2923 | "author\\|deprecated\\|exception" | |
2924 | "\\|link\\|return\\|see\\|serial\\|serialData\\|serialField" | |
2925 | "\\|since\\|throws" | |
2926 | "\\|version" | |
1bc7654a GM |
2927 | "\\)\\>") |
2928 | '(1 font-lock-constant-face prepend)) | |
2e6a15fc | 2929 | '("@\\(param\\)\\>[ \t]*\\(\\sw+\\)?" |
8acf2292 | 2930 | (1 font-lock-constant-face prepend) |
2e6a15fc | 2931 | (2 font-lock-variable-name-face prepend t)) |
340fe50f GM |
2932 | '("@\\(exception\\|throws\\)\\>[ \t]*\\(\\S-+\\)?" |
2933 | (1 font-lock-constant-face prepend) | |
2934 | (2 font-lock-type-face prepend t)) | |
8259bf10 SM |
2935 | )) |
2936 | "Medium level highlighting for Java mode. | |
2937 | See also `java-font-lock-extra-types'.") | |
2e6a15fc | 2938 | |
8259bf10 | 2939 | (defconst java-font-lock-keywords-3 |
2e6a15fc SM |
2940 | (append java-font-lock-keywords-2 |
2941 | ;; | |
2942 | ;; More complicated regexps for more complete highlighting for types. | |
2943 | ;; We still have to fontify type specifiers individually, as Java is hairy. | |
2944 | (list | |
2945 | ;; | |
2e6a15fc | 2946 | ;; Fontify random types immediately followed by an item or items. |
c1f2ffc8 | 2947 | `(eval . |
d2251bbf | 2948 | (list (concat "\\<\\(" ,java-type-names "\\)\\>" |
c1f2ffc8 SM |
2949 | "\\([ \t]*\\[[ \t]*\\]\\)*" |
2950 | "\\([ \t]*\\sw\\)") | |
2951 | ;; Fontify each declaration item. | |
2d63aa67 SM |
2952 | (list 'font-lock-match-c-style-declaration-item-and-skip-to-next |
2953 | ;; Start and finish with point after the type specifier. | |
2954 | (list 'goto-char (list 'match-beginning | |
d2251bbf | 2955 | (+ ,java-type-names-depth 3))) |
2d63aa67 | 2956 | (list 'goto-char (list 'match-beginning |
d2251bbf | 2957 | (+ ,java-type-names-depth 3))) |
2d63aa67 SM |
2958 | ;; Fontify as a variable or function name. |
2959 | '(1 (if (match-beginning 2) | |
2960 | font-lock-function-name-face | |
2961 | font-lock-variable-name-face))))) | |
2e6a15fc | 2962 | ;; |
2e6a15fc | 2963 | ;; Fontify those that are eventually followed by an item or items. |
68c67d1f | 2964 | (list (concat "\\<\\(" java-type-specs "\\)\\>" |
2e6a15fc SM |
2965 | "\\([ \t]+\\sw+\\>" |
2966 | "\\([ \t]*\\[[ \t]*\\]\\)*" | |
2967 | "\\)*") | |
2968 | ;; Fontify each declaration item. | |
c1f2ffc8 | 2969 | '(font-lock-match-c-style-declaration-item-and-skip-to-next |
2e6a15fc | 2970 | ;; Start with point after all type specifiers. |
2d63aa67 | 2971 | (goto-char (or (match-beginning 5) (match-end 1))) |
2e6a15fc SM |
2972 | ;; Finish with point after first type specifier. |
2973 | (goto-char (match-end 1)) | |
2974 | ;; Fontify as a variable or function name. | |
c1f2ffc8 | 2975 | (1 (if (match-beginning 2) |
2e6a15fc SM |
2976 | font-lock-function-name-face |
2977 | font-lock-variable-name-face)))) | |
8259bf10 SM |
2978 | )) |
2979 | "Gaudy level highlighting for Java mode. | |
2980 | See also `java-font-lock-extra-types'.") | |
2981 | ) | |
2e6a15fc SM |
2982 | |
2983 | (defvar java-font-lock-keywords java-font-lock-keywords-1 | |
2984 | "Default expressions to highlight in Java mode. | |
2985 | See also `java-font-lock-extra-types'.") | |
d46c21ec | 2986 | \f |
a1eb1cf1 | 2987 | ;; Provide ourselves: |
8f261d40 | 2988 | |
9aa40401 SM |
2989 | (defun java-font-lock-syntactic-face-function (state) |
2990 | (save-excursion | |
2991 | (if (nth 3 state) | |
2992 | ;; Check whether the string is properly terminated. | |
2993 | (let ((nstate (parse-partial-sexp (point) (line-end-position) | |
2994 | nil nil state 'syntax-table))) | |
2995 | (if (and (eolp) (nth 3 nstate)) | |
2996 | ;; We're inside a string, at EOL. The JLS says that: | |
2997 | ;; It is a compile-time error for a line terminator to | |
2998 | ;; appear after the opening " and before the closing | |
2999 | ;; matching ". | |
3000 | font-lock-warning-face | |
3001 | font-lock-string-face)) | |
3002 | (goto-char (nth 8 state)) | |
3003 | (if (looking-at "/\\*\\*") | |
3004 | font-lock-doc-face | |
3005 | font-lock-comment-face)))) | |
3006 | ||
030f4a35 | 3007 | (provide 'font-lock) |
54f73af3 GM |
3008 | |
3009 | (when (eq font-lock-support-mode 'jit-lock-mode) | |
3010 | (require 'jit-lock)) | |
030f4a35 RS |
3011 | |
3012 | ;;; font-lock.el ends here |