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