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