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