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