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