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