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