1 ;;; font-lock.el --- decorating source files with fonts/colors based on syntax
3 ;; Copyright (C) 1992-1995, 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Amdahl Corporation.
5 ;; Copyright (C) 1996, 2000, 2001 Ben Wing.
7 ;; Author: Jamie Zawinski <jwz@jwz.org>, for the LISPM Preservation Society.
8 ;; Minimally merged with FSF 19.34 by Barry Warsaw <bwarsaw@python.org>
9 ;; Then (partially) synched with FSF 19.30, leading to:
11 ;; Next Author: Simon Marshall <simon@gnu.ai.mit.edu>
12 ;; Latest XEmacs Author: Ben Wing
13 ;; Maintainer: XEmacs Development Team
14 ;; Keywords: languages, faces
16 ;; This file is part of XEmacs.
18 ;; XEmacs is free software; you can redistribute it and/or modify it
19 ;; under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation; either version 2, or (at your option)
23 ;; XEmacs is distributed in the hope that it will be useful, but
24 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 ;; General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with XEmacs; see the file COPYING. If not, write to the
30 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
31 ;; Boston, MA 02111-1307, USA.
33 ;;; Synched up with: FSF 19.30 except for the code to initialize the faces.
37 ;; Font-lock-mode is a minor mode that causes your comments to be
38 ;; displayed in one face, strings in another, reserved words in another,
39 ;; documentation strings in another, and so on.
41 ;; Comments will be displayed in `font-lock-comment-face'.
42 ;; Strings will be displayed in `font-lock-string-face'.
43 ;; Doc strings will be displayed in `font-lock-doc-string-face'.
44 ;; Function and variable names (in their defining forms) will be
45 ;; displayed in `font-lock-function-name-face'.
46 ;; Reserved words will be displayed in `font-lock-keyword-face'.
48 ;; Don't let the name fool you: you can highlight things using different
49 ;; colors or background stipples instead of fonts, though that is not the
50 ;; default. See the variables `font-lock-use-colors' and
51 ;; `font-lock-use-fonts' for broad control over this, or see the
52 ;; documentation on faces and how to change their attributes for
53 ;; fine-grained control.
55 ;; To make the text you type be fontified, use M-x font-lock-mode. When
56 ;; this minor mode is on, the fonts of the current line will be updated
57 ;; with every insertion or deletion.
59 ;; By default, font-lock will automatically put newly loaded files
60 ;; into font-lock-mode if it knows about the file's mode. See the
61 ;; variables `font-lock-auto-fontify', `font-lock-mode-enable-list',
62 ;; and `font-lock-mode-disable-list' for control over this.
64 ;; The `font-lock-keywords' variable defines other patterns to highlight.
65 ;; The default font-lock-mode-hook sets it to the value of the variables
66 ;; lisp-font-lock-keywords, c-font-lock-keywords, etc, as appropriate.
67 ;; The easiest way to change the highlighting patterns is to change the
68 ;; values of c-font-lock-keywords and related variables. See the doc
69 ;; string of the variable `font-lock-keywords' for the appropriate syntax.
71 ;; The default value for `lisp-font-lock-keywords' is the value of the variable
72 ;; `lisp-font-lock-keywords-1'. You may like `lisp-font-lock-keywords-2'
73 ;; better; it highlights many more words, but is slower and makes your buffers
74 ;; be very visually noisy.
76 ;; The same is true of `c-font-lock-keywords-1' and `c-font-lock-keywords-2';
77 ;; the former is subdued, the latter is loud.
79 ;; You can make font-lock default to the gaudier variety of keyword
80 ;; highlighting by setting the variable `font-lock-maximum-decoration'
81 ;; before loading font-lock, or by calling the functions
82 ;; `font-lock-use-default-maximal-decoration' or
83 ;; `font-lock-use-default-minimal-decoration'.
85 ;; On a Sparc10, the initial fontification takes about 6 seconds for a typical
86 ;; 140k file of C code, using the default configuration. The actual speed
87 ;; depends heavily on the type of code in the file, and how many non-syntactic
88 ;; patterns match; for example, Xlib.h takes 23 seconds for 101k, because many
89 ;; patterns match in it. You can speed this up substantially by removing some
90 ;; of the patterns that are highlighted by default. Fontifying lisp code is
91 ;; significantly faster, because lisp has a more regular syntax than C, so the
92 ;; regular expressions don't have to be as complicated.
94 ;; It's called font-lock-mode here because on the Lispms it was called
95 ;; "Electric Font Lock Mode." It was called that because there was an older
96 ;; mode called "Electric Caps Lock Mode" which had the function of causing all
97 ;; of your source code to be in upper case except for strings and comments,
98 ;; without you having to blip the caps lock key by hand all the time (thus the
99 ;; "electric", as in `electric-c-brace'.)
101 ;; See also the related packages `fast-lock' and `lazy-lock'. Both
102 ;; attempt to speed up the initial fontification. `fast-lock' saves
103 ;; the fontification info when you exit Emacs and reloads it next time
104 ;; you load the file, so that the file doesn't have to be fontified
105 ;; again. `lazy-lock' does "lazy" fontification -- i.e. it only
106 ;; fontifies the text as it becomes visible rather than fontifying
107 ;; the whole file when it's first loaded in.
109 ;; Further comments from the FSF:
111 ;; Nasty regexps of the form "bar\\(\\|lo\\)\\|f\\(oo\\|u\\(\\|bar\\)\\)\\|lo"
112 ;; are made thusly: (regexp-opt '("foo" "fu" "fubar" "bar" "barlo" "lo")) for
115 ;; What is fontification for? You might say, "It's to make my code look nice."
116 ;; I think it should be for adding information in the form of cues. These cues
117 ;; should provide you with enough information to both (a) distinguish between
118 ;; different items, and (b) identify the item meanings, without having to read
119 ;; the items and think about it. Therefore, fontification allows you to think
120 ;; less about, say, the structure of code, and more about, say, why the code
121 ;; doesn't work. Or maybe it allows you to think less and drift off to sleep.
123 ;; So, here are my opinions/advice/guidelines:
125 ;; - Use the same face for the same conceptual object, across all modes.
126 ;; i.e., (b) above, all modes that have items that can be thought of as, say,
127 ;; keywords, should be highlighted with the same face, etc.
128 ;; - Keep the faces distinct from each other as far as possible.
130 ;; - Make the face attributes fit the concept as far as possible.
131 ;; i.e., function names might be a bold color such as blue, comments might
132 ;; be a bright color such as red, character strings might be brown, because,
133 ;; err, strings are brown (that was not the reason, please believe me).
134 ;; - Don't use a non-nil OVERRIDE unless you have a good reason.
135 ;; Only use OVERRIDE for special things that are easy to define, such as the
136 ;; way `...' quotes are treated in strings and comments in Emacs Lisp mode.
137 ;; Don't use it to, say, highlight keywords in commented out code or strings.
143 (require 'fontl-hooks)
145 ;;;;;;;;;;;;;;;;;;;;;; user variables ;;;;;;;;;;;;;;;;;;;;;;
147 (defgroup font-lock nil
148 "Decorate source files with fonts/colors based on syntax.
149 Font-lock-mode is a minor mode that causes your comments to be
150 displayed in one face, strings in another, reserved words in another,
151 documentation strings in another, and so on.
153 Comments will be displayed in `font-lock-comment-face'.
154 Strings will be displayed in `font-lock-string-face'.
155 Doc strings will be displayed in `font-lock-doc-string-face'.
156 Function and variable names (in their defining forms) will be displayed
157 in `font-lock-function-name-face'.
158 Reserved words will be displayed in `font-lock-keyword-face'.
159 Preprocessor conditionals will be displayed in `font-lock-preprocessor-face'."
162 (defgroup font-lock-faces nil
163 "Faces used by the font-lock package."
168 (defcustom font-lock-verbose t
169 "*If non-nil, means show status messages when fontifying.
170 See also `font-lock-message-threshold'."
174 (defcustom font-lock-message-threshold 6000
175 "*Minimum size of region being fontified for status messages to appear.
177 The size is measured in characters. This affects `font-lock-fontify-region'
178 but not `font-lock-fontify-buffer'. (In other words, when you first visit
179 a file and it gets fontified, you will see status messages no matter what
180 size the file is. However, if you do something else like paste a
181 chunk of text, you will see status messages only if the changed region is
184 Note that setting `font-lock-verbose' to nil disables the status
190 (defcustom font-lock-auto-fontify t
191 "*Whether font-lock should automatically fontify files as they're loaded.
192 This will only happen if font-lock has fontifying keywords for the major
193 mode of the file. You can get finer-grained control over auto-fontification
194 by using this variable in combination with `font-lock-mode-enable-list' or
195 `font-lock-mode-disable-list'."
200 (defcustom font-lock-mode-enable-list nil
201 "*List of modes to auto-fontify, if `font-lock-auto-fontify' is nil."
202 :type '(repeat (symbol :tag "Mode"))
206 (defcustom font-lock-mode-disable-list nil
207 "*List of modes not to auto-fontify, if `font-lock-auto-fontify' is t."
208 :type '(repeat (symbol :tag "Mode"))
212 (defcustom font-lock-use-colors '(color)
213 "*Specification for when Font Lock will set up color defaults.
214 Normally this should be '(color), meaning that Font Lock will set up
215 color defaults that are only used on color displays. Set this to nil
216 if you don't want Font Lock to set up color defaults at all. This
219 -- a list of valid tags, meaning that the color defaults will be used
220 when all of the tags apply. (e.g. '(color x))
221 -- a list whose first element is 'or and whose remaining elements are
222 lists of valid tags, meaning that the defaults will be used when
223 any of the tag lists apply.
224 -- nil, meaning that the defaults should not be set up at all.
226 \(If you specify face values in your init file, they will override any
227 that Font Lock specifies, regardless of whether you specify the face
228 values before or after loading Font Lock.)
230 See also `font-lock-use-fonts'. If you want more control over the faces
231 used for fontification, see the documentation of `font-lock-mode' for
238 (defcustom font-lock-use-fonts '(or (mono) (grayscale))
239 "*Specification for when Font Lock will set up non-color defaults.
241 Normally this should be '(or (mono) (grayscale)), meaning that Font
242 Lock will set up non-color defaults that are only used on either mono
243 or grayscale displays. Set this to nil if you don't want Font Lock to
244 set up non-color defaults at all. This should be one of
246 -- a list of valid tags, meaning that the non-color defaults will be used
247 when all of the tags apply. (e.g. '(grayscale x))
248 -- a list whose first element is 'or and whose remaining elements are
249 lists of valid tags, meaning that the defaults will be used when
250 any of the tag lists apply.
251 -- nil, meaning that the defaults should not be set up at all.
253 \(If you specify face values in your init file, they will override any
254 that Font Lock specifies, regardless of whether you specify the face
255 values before or after loading Font Lock.)
257 See also `font-lock-use-colors'. If you want more control over the faces
258 used for fontification, see the documentation of `font-lock-mode' for
264 (defcustom font-lock-maximum-decoration t
265 "*If non-nil, the maximum decoration level for fontifying.
266 If nil, use the minimum decoration (equivalent to level 0).
267 If t, use the maximum decoration available.
268 If a number, use that level of decoration (or if not available the maximum).
269 If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
270 where MAJOR-MODE is a symbol or t (meaning the default). For example:
271 ((c++-mode . 2) (c-mode . t) (t . 1))
272 means use level 2 decoration for buffers in `c++-mode', the maximum decoration
273 available for buffers in `c-mode', and level 1 decoration otherwise."
274 :type '(choice (const :tag "default" nil)
275 (const :tag "maximum" t)
276 (integer :tag "level" 1)
277 (repeat :menu-tag "mode specific" :tag "mode specific"
279 (cons :tag "Instance"
282 (symbol :tag "name"))
283 (radio :tag "Decoration"
284 (const :tag "default" nil)
285 (const :tag "maximum" t)
286 (integer :tag "level" 1)))))
290 (define-obsolete-variable-alias 'font-lock-use-maximal-decoration
291 'font-lock-maximum-decoration)
294 (defcustom font-lock-maximum-size (* 250 1024)
295 "*If non-nil, the maximum size for buffers for fontifying.
296 Only buffers less than this can be fontified when Font Lock mode is turned on.
297 If nil, means size is irrelevant.
298 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
299 where MAJOR-MODE is a symbol or t (meaning the default). For example:
300 ((c++-mode . 256000) (c-mode . 256000) (rmail-mode . 1048576))
301 means that the maximum size is 250K for buffers in `c++-mode' or `c-mode', one
302 megabyte for buffers in `rmail-mode', and size is irrelevant otherwise."
303 :type '(choice (const :tag "none" nil)
304 (integer :tag "size")
305 (repeat :menu-tag "mode specific" :tag "mode specific"
307 (cons :tag "Instance"
310 (symbol :tag "name"))
312 (const :tag "none" nil)
313 (integer :tag "size")))))
317 (defcustom font-lock-fontify-string-delimiters nil
318 "*If non-nil, apply font-lock-string-face to string delimiters as well as
319 string text when fontifying."
323 ;; Fontification variables:
326 (defvar font-lock-keywords nil
327 "A list defining the keywords for `font-lock-mode' to highlight.
329 FONT-LOCK-KEYWORDS := List of FONT-LOCK-FORM's.
331 FONT-LOCK-FORM :== MATCHER
333 | (MATCHER . FACE-FORM)
334 | (MATCHER . HIGHLIGHT)
335 | (MATCHER HIGHLIGHT ...)
338 MATCHER :== A string containing a regexp.
339 | A variable containing a regexp to search for.
340 | A function to call to make the search.
341 It is called with one arg, the limit of the search,
342 and should leave MATCH results in the XEmacs global
345 MATCH :== An integer match subexpression number from MATCHER.
347 FACE-FORM :== The symbol naming a defined face.
348 | Expression whos value is the face name to use. If you
349 want FACE-FORM to be a symbol that evaluates to a face,
350 use a form like \"(progn sym)\".
352 HIGHLIGHT :== MATCH-HIGHLIGHT
355 FORM :== Expression returning a FONT-LOCK-FORM, evaluated when
356 the FONT-LOCK-FORM is first used in a buffer. This
357 feature can be used to provide a FONT-LOCK-FORM that
358 can only be generated when Font Lock mode is actually
361 MATCH-HIGHLIGHT :== (MATCH FACE-FORM OVERRIDE LAXMATCH)
363 OVERRIDE :== t - overwrite existing fontification
364 | 'keep - only parts not already fontified are
366 | 'prepend - merge faces, this fontification has
367 precedence over existing
368 | 'append - merge faces, existing fontification has
372 LAXMATCH :== If non-nil, no error is signalled if there is no MATCH
375 MATCH-ANCHORED :== (ANCHOR-MATCHER PRE-MATCH-FORM \\
376 POST-MATCH-FORM MATCH-HIGHLIGHT ...)
378 ANCHOR-MATCHER :== Like a MATCHER, except that the limit of the search
379 defaults to the end of the line after PRE-MATCH-FORM
380 is evaluated. However, if PRE-MATCH-FORM returns a
381 position greater than the end of the line, that
382 position is used as the limit of the search. It is
383 generally a bad idea to return a position greater than
384 the end of the line, i.e., cause the ANCHOR-MATCHER
385 search to span lines.
387 PRE-MATCH-FORM :== Evaluated before the ANCHOR-MATCHER is used, therefore
388 can be used to initialize before, ANCHOR-MATCHER is
389 used. Typically, PRE-MATCH-FORM is used to move to
390 some position relative to the original MATCHER, before
391 starting with the ANCHOR-MATCHER.
393 POST-MATCH-FORM :== Like PRE-MATCH-FORM, but used to clean up after the
394 ANCHOR-MATCHER. It might be used to move, before
395 resuming with MATCH-ANCHORED's parent's MATCHER.
397 For example, an element of the first form highlights (if not already highlighted):
399 \"\\\\\\=<foo\\\\\\=>\" Discrete occurrences of \"foo\" in the value
400 of the variable `font-lock-keyword-face'.
402 (\"fu\\\\(bar\\\\)\" . 1) Substring \"bar\" within all occurrences of
403 \"fubar\" in the value of
404 `font-lock-keyword-face'.
406 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of
409 (\"foo\\\\|bar\" 0 foo-bar-face t) Occurrences of either \"foo\" or \"bar\" in the
410 value of `foo-bar-face', even if already
413 (fubar-match 1 fubar-face) The first subexpression within all
414 occurrences of whatever the function
415 `fubar-match' finds and matches in the value
418 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
419 -------------- --------------- ------------ --- --- -------------
421 MATCHER | ANCHOR-MATCHER | +------+ MATCH-HIGHLIGHT
422 MATCH-HIGHLIGHT PRE-MATCH-FORM |
425 Discrete occurrences of \"anchor\" in the value of `anchor-face', and
426 subsequent discrete occurrences of \"item\" (on the same line) in the value
427 of `item-face'. (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil.
428 Therefore \"item\" is initially searched for starting from the end of the
429 match of \"anchor\", and searching for subsequent instance of \"anchor\"
430 resumes from where searching for \"item\" concluded.)
432 For highlighting single items, typically only MATCH-HIGHLIGHT is required.
433 However, if an item or (typically) several items are to be highlighted
434 following the instance of another item (the anchor) then MATCH-ANCHORED may be
437 These regular expressions should not match text which spans lines. While
438 \\[font-lock-fontify-buffer] handles multi-line patterns correctly, updating when you
439 edit the buffer does not, since it considers text one line at a time.
441 Be very careful composing regexps for this list; the wrong pattern can
442 dramatically slow things down!
445 (defvar font-lock-keywords-alist nil
446 "Alist of additional `font-lock-keywords' elements for major modes.
448 Each element has the form (MODE KEYWORDS . HOW).
449 `font-lock-set-defaults' adds the elements in the list KEYWORDS to
450 `font-lock-keywords' when Font Lock is turned on in major mode MODE.
452 If HOW is nil, KEYWORDS are added at the beginning of
453 `font-lock-keywords'. If it is `set', they are used to replace the
454 value of `font-lock-keywords'. If HOW is any other non-nil value,
455 they are added at the end.
457 This is normally set via `font-lock-add-keywords' and
458 `font-lock-remove-keywords'.")
460 (defvar font-lock-removed-keywords-alist nil
461 "Alist of `font-lock-keywords' elements to be removed for major modes.
463 Each element has the form (MODE . KEYWORDS). `font-lock-set-defaults'
464 removes the elements in the list KEYWORDS from `font-lock-keywords'
465 when Font Lock is turned on in major mode MODE.
467 This is normally set via `font-lock-add-keywords' and
468 `font-lock-remove-keywords'.")
471 (make-variable-buffer-local 'font-lock-keywords)
474 (defvar font-lock-syntactic-keywords nil
475 "A list of the syntactic keywords to highlight.
476 Can be the list or the name of a function or variable whose value is the list.
477 See `font-lock-keywords' for a description of the form of this list;
478 the differences are listed below. MATCH-HIGHLIGHT should be of the form:
480 (MATCH SYNTAX OVERRIDE LAXMATCH)
482 where SYNTAX can be of the form (SYNTAX-CODE . MATCHING-CHAR), the name of a
483 syntax table, or an expression whose value is such a form or a syntax table.
484 OVERRIDE cannot be `prepend' or `append'.
486 For example, an element of the form highlights syntactically:
488 (\"\\\\$\\\\(#\\\\)\" 1 (1 . nil))
490 a hash character when following a dollar character, with a SYNTAX-CODE of
491 1 (meaning punctuation syntax). Assuming that the buffer syntax table does
492 specify hash characters to have comment start syntax, the element will only
493 highlight hash characters that do not follow dollar characters as comments
496 (\"\\\\('\\\\).\\\\('\\\\)\"
500 both single quotes which surround a single character, with a SYNTAX-CODE of
501 7 (meaning string quote syntax) and a MATCHING-CHAR of a single quote (meaning
502 a single quote matches a single quote). Assuming that the buffer syntax table
503 does not specify single quotes to have quote syntax, the element will only
504 highlight single quotes of the form 'c' as strings syntactically.
505 Other forms, such as foo'bar or 'fubar', will not be highlighted as strings.
507 This is normally set via `font-lock-defaults'."
510 (make-variable-buffer-local 'font-lock-syntactic-keywords)
512 (defvar font-lock-defaults nil
513 "The defaults font Font Lock mode for the current buffer.
514 Normally, do not set this directly. If you are writing a major mode,
515 put a property of `font-lock-defaults' on the major-mode symbol with
520 \(KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN)
522 KEYWORDS may be a symbol (a variable or function whose value is the keywords
523 to use for fontification) or a list of symbols. If KEYWORDS-ONLY is non-nil,
524 syntactic fontification (strings and comments) is not performed. If CASE-FOLD
525 is non-nil, the case of the keywords is ignored when fontifying. If
526 SYNTAX-ALIST is non-nil, it should be a list of cons pairs of the form (CHAR
527 . STRING) used to set the local Font Lock syntax table, for keyword and
528 syntactic fontification (see `modify-syntax-entry').
530 If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
531 backwards outside any enclosing syntactic block, for syntactic fontification.
532 Typical values are `beginning-of-line' (i.e., the start of the line is known to
533 be outside a syntactic block), or `beginning-of-defun' for programming modes or
534 `backward-paragraph' for textual modes (i.e., the mode-dependent function is
535 known to move outside a syntactic block). If nil, the beginning of the buffer
536 is used as a position outside of a syntactic block, in the worst case.
538 These item elements are used by Font Lock mode to set the variables
539 `font-lock-keywords', `font-lock-keywords-only',
540 `font-lock-keywords-case-fold-search', `font-lock-syntax-table' and
541 `font-lock-beginning-of-syntax-function', respectively.
543 Alternatively, if the value is a symbol, it should name a major mode,
544 and the defaults for that mode will apply.")
545 (make-variable-buffer-local 'font-lock-defaults)
547 ;; FSF uses `font-lock-defaults-alist' and expects the major mode to
548 ;; set a value for `font-lock-defaults', but I don't like either of
549 ;; these -- requiring the mode to set `font-lock-defaults' makes it
550 ;; impossible to have defaults for a minor mode, and using an alist is
551 ;; generally a bad idea for information that really should be
552 ;; decentralized. (Who knows what strange modes might want
555 (defvar font-lock-keywords-only nil
556 "Non-nil means Font Lock should not do syntactic fontification.
557 This is normally set via `font-lock-defaults'.
559 This should be nil for all ``language'' modes, but other modes, like
560 dired, do not have anything useful in the syntax tables (no comment
561 or string delimiters, etc) and so there is no need to use them and
562 this variable should have a value of t.
564 You should not set this variable directly; its value is computed
565 from `font-lock-defaults', or (if that does not specify anything)
566 by examining the syntax table to see whether it appears to contain
568 (make-variable-buffer-local 'font-lock-keywords-only)
570 (defvar font-lock-keywords-case-fold-search nil
571 "Whether the strings in `font-lock-keywords' should be case-folded.
572 This variable is automatically buffer-local, as the correct value depends
573 on the language in use.")
574 (make-variable-buffer-local 'font-lock-keywords-case-fold-search)
576 (defvar font-lock-after-fontify-buffer-hook nil
577 "Function or functions to run after completion of font-lock-fontify-buffer.")
579 (defvar font-lock-syntax-table nil
580 "Non-nil means use this syntax table for fontifying.
581 If this is nil, the major mode's syntax table is used.
582 This is normally set via `font-lock-defaults'.")
583 (make-variable-buffer-local 'font-lock-syntax-table)
585 ;; These record the parse state at a particular position, always the start of a
586 ;; line. Used to make `font-lock-fontify-syntactically-region' faster.
587 ;; Previously, `font-lock-cache-position' was just a buffer position. However,
588 ;; under certain situations, this occasionally resulted in mis-fontification.
589 ;; I think the "situations" were deletion with Lazy Lock mode's deferral. sm.
590 (defvar font-lock-cache-state nil)
591 (defvar font-lock-cache-position nil)
592 (make-variable-buffer-local 'font-lock-cache-state)
593 (make-variable-buffer-local 'font-lock-cache-position)
595 ;; If this is nil, we only use the beginning of the buffer if we can't use
596 ;; `font-lock-cache-position' and `font-lock-cache-state'.
597 (defvar font-lock-beginning-of-syntax-function nil
598 "Non-nil means use this function to move back outside of a syntactic block.
599 If this is nil, the beginning of the buffer is used (in the worst case).
600 This is normally set via `font-lock-defaults'.")
601 (make-variable-buffer-local 'font-lock-beginning-of-syntax-function)
603 (defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
604 "Function to use for fontifying the buffer.
605 This is normally set via `font-lock-defaults'.")
607 (defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
608 "Function to use for unfontifying the buffer.
609 This is used when turning off Font Lock mode.
610 This is normally set via `font-lock-defaults'.")
612 (defvar font-lock-fontify-region-function 'font-lock-default-fontify-region
613 "Function to use for fontifying a region.
614 It should take two args, the beginning and end of the region, and an optional
615 third arg VERBOSE. If non-nil, the function should print status messages.
616 This is normally set via `font-lock-defaults'.")
618 (defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
619 "Function to use for unfontifying a region.
620 It should take two args, the beginning and end of the region.
621 This is normally set via `font-lock-defaults'.")
623 (defvar font-lock-inhibit-thing-lock nil
624 "List of Font Lock mode related modes that should not be turned on.
625 Currently, valid mode names as `fast-lock-mode' and `lazy-lock-mode'.
626 This is normally set via `font-lock-defaults'.")
629 (defcustom font-lock-mode nil ;; customized for the option menu. dverna
630 "Non nil means `font-lock-mode' is on"
633 :initialize 'custom-initialize-default
635 :set #'(lambda (var val) (font-lock-mode (or val 0)))
638 (defvar font-lock-fontified nil) ; whether we have hacked this buffer
639 (put 'font-lock-fontified 'permanent-local t)
642 (defvar font-lock-mode-hook nil
643 "Function or functions to run on entry to font-lock-mode.")
645 ; whether font-lock-set-defaults has already been run.
646 (defvar font-lock-defaults-computed nil)
647 (make-variable-buffer-local 'font-lock-defaults-computed)
650 ;;; Initialization of faces.
652 ;; #### barf gag retch. Horrid FSF lossage that we need to
653 ;; keep around for compatibility with font-lock-keywords that
654 ;; forget to properly quote their faces. I tried just let-binding
655 ;; them when we eval the face expression, but that fails because
656 ;; some files actually use the variables directly in their init code
657 ;; without quoting them. --ben
658 (defvar font-lock-comment-face 'font-lock-comment-face
659 "This variable should not be set.
660 It is present only for horrid FSF compatibility reasons.
661 The corresponding face should be set using `edit-faces' or the
662 `set-face-*' functions.")
663 (defvar font-lock-doc-string-face 'font-lock-doc-string-face
664 "This variable should not be set.
665 It is present only for horrid FSF compatibility reasons.
666 The corresponding face should be set using `edit-faces' or the
667 `set-face-*' functions.")
669 (define-compatible-variable-alias
670 'font-lock-doc-face 'font-lock-doc-string-face)
671 (defvar font-lock-string-face 'font-lock-string-face
672 "This variable should not be set.
673 It is present only for horrid FSF compatibility reasons.
674 The corresponding face should be set using `edit-faces' or the
675 `set-face-*' functions.")
676 (defvar font-lock-keyword-face 'font-lock-keyword-face
677 "This variable should not be set.
678 It is present only for horrid FSF compatibility reasons.
679 The corresponding face should be set using `edit-faces' or the
680 `set-face-*' functions.")
681 (defvar font-lock-builtin-face 'font-lock-builtin-face
682 "This variable should not be set.
683 It is present only for horrid FSF compatibility reasons.
684 The corresponding face should be set using `edit-faces' or the
685 `set-face-*' functions.")
686 (defvar font-lock-function-name-face 'font-lock-function-name-face
687 "This variable should not be set.
688 It is present only for horrid FSF compatibility reasons.
689 The corresponding face should be set using `edit-faces' or the
690 `set-face-*' functions.")
691 (defvar font-lock-variable-name-face 'font-lock-variable-name-face
692 "This variable should not be set.
693 It is present only for horrid FSF compatibility reasons.
694 The corresponding face should be set using `edit-faces' or the
695 `set-face-*' functions.")
696 (defvar font-lock-type-face 'font-lock-type-face
697 "This variable should not be set.
698 It is present only for horrid FSF compatibility reasons.
699 The corresponding face should be set using `edit-faces' or the
700 `set-face-*' functions.")
701 (defvar font-lock-constant-face 'font-lock-constant-face
702 "This variable should not be set.
703 It is present only for horrid FSF compatibility reasons.
704 The corresponding face should be set using `edit-faces' or the
705 `set-face-*' functions.")
706 (defvar font-lock-reference-face 'font-lock-reference-face
707 "This variable should not be set.
708 It is present only for horrid FSF compatibility reasons.
709 The corresponding face should be set using `edit-faces' or the
710 `set-face-*' functions.")
711 (defvar font-lock-preprocessor-face 'font-lock-preprocessor-face
712 "This variable should not be set.
713 It is present only for horrid FSF compatibility reasons.
714 The corresponding face should be set using `edit-faces' or the
715 `set-face-*' functions.")
716 (defvar font-lock-warning-face 'font-lock-warning-face
717 "This variable should not be set.
718 It is present only for horrid FSF compatibility reasons.
719 The corresponding face should be set using `edit-faces' or the
720 `set-face-*' functions.")
722 (defconst font-lock-face-list
723 '(font-lock-comment-face
724 font-lock-string-face
725 font-lock-doc-string-face
726 font-lock-keyword-face
727 font-lock-builtin-face
728 font-lock-function-name-face
729 font-lock-variable-name-face
731 font-lock-constant-face
732 font-lock-reference-face
733 font-lock-preprocessor-face
734 font-lock-warning-face))
736 (defface font-lock-comment-face
737 '((((class color) (background dark)) (:foreground "gray80"))
738 ;; blue4 is hardly different from black on windows.
739 (((class color) (background light) (type mswindows)) (:foreground "blue"))
740 (((class color) (background light)) (:foreground "blue4"))
741 (((class grayscale) (background light))
742 (:foreground "DimGray" :bold t :italic t))
743 (((class grayscale) (background dark))
744 (:foreground "LightGray" :bold t :italic t))
746 "Font Lock mode face used to highlight comments."
747 :group 'font-lock-faces)
749 (defface font-lock-string-face
750 '((((class color) (background dark)) (:foreground "tan"))
751 (((class color) (background light)) (:foreground "green4"))
752 (((class grayscale) (background light)) (:foreground "DimGray" :italic t))
753 (((class grayscale) (background dark)) (:foreground "LightGray" :italic t))
755 "Font Lock mode face used to highlight strings."
756 :group 'font-lock-faces)
758 (defface font-lock-doc-string-face
759 '((((class color) (background dark)) (:foreground "light coral"))
760 (((class color) (background light)) (:foreground "green4"))
762 "Font Lock mode face used to highlight documentation strings.
763 This is currently supported only in Lisp-like modes, which are those
764 with \"lisp\" or \"scheme\" in their name. You can explicitly make
765 a mode Lisp-like by putting a non-nil `font-lock-lisp-like' property
766 on the major mode's symbol."
767 :group 'font-lock-faces)
769 (defface font-lock-keyword-face
770 '((((class color) (background dark)) (:foreground "cyan"))
771 ;; red4 is hardly different from black on windows.
772 (((class color) (background light) (type mswindows)) (:foreground "red"))
773 (((class color) (background light)) (:foreground "red4"))
774 (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
775 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
777 "Font Lock mode face used to highlight keywords."
778 :group 'font-lock-faces)
780 (defface font-lock-builtin-face
781 '((((class color) (background light)) (:foreground "Purple"))
782 (((class color) (background dark)) (:foreground "Cyan"))
783 (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
784 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
786 "Font Lock mode face used to highlight builtins."
787 :group 'font-lock-faces)
789 (defface font-lock-function-name-face
790 '((((class color) (background dark)) (:foreground "aquamarine"))
791 ;; brown4 is hardly different from black on windows.
792 ;; I changed it to red because IMO it's pointless and ugly to
793 ;; use a million slightly different colors for niggly syntactic
794 ;; differences. --ben
795 (((class color) (background light) (type mswindows)) (:foreground "red"))
796 (((class color) (background light)) (:foreground "brown4"))
797 (t (:bold t :underline t)))
798 "Font Lock mode face used to highlight function names."
799 :group 'font-lock-faces)
801 (defface font-lock-variable-name-face
802 '((((class color) (background dark)) (:foreground "cyan3"))
803 (((class color) (background light)) (:foreground "magenta4"))
804 (((class grayscale) (background light))
805 (:foreground "Gray90" :bold t :italic t))
806 (((class grayscale) (background dark))
807 (:foreground "DimGray" :bold t :italic t))
809 "Font Lock mode face used to highlight variable names."
810 :group 'font-lock-faces)
812 (defface font-lock-type-face
813 '((((class color) (background dark)) (:foreground "wheat"))
814 (((class color) (background light)) (:foreground "steelblue"))
815 (((class grayscale) (background light)) (:foreground "Gray90" :bold t))
816 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
818 "Font Lock mode face used to highlight types."
819 :group 'font-lock-faces)
821 (defface font-lock-constant-face
822 '((((class color) (background light)) (:foreground "CadetBlue"))
823 (((class color) (background dark)) (:foreground "Aquamarine"))
824 (((class grayscale) (background light))
825 (:foreground "LightGray" :bold t :underline t))
826 (((class grayscale) (background dark))
827 (:foreground "Gray50" :bold t :underline t))
828 (t (:bold t :underline t)))
829 "Font Lock mode face used to highlight constants and labels."
830 :group 'font-lock-faces)
832 (defface font-lock-reference-face
833 '((((class color) (background dark)) (:foreground "cadetblue2"))
834 (((class color) (background light)) (:foreground "red3"))
835 (((class grayscale) (background light))
836 (:foreground "LightGray" :bold t :underline t))
837 (((class grayscale) (background dark))
838 (:foreground "Gray50" :bold t :underline t)))
839 "Font Lock mode face used to highlight references."
840 :group 'font-lock-faces)
842 (defface font-lock-preprocessor-face
843 '((((class color) (background dark)) (:foreground "steelblue1"))
844 (((class color) (background light)) (:foreground "blue3"))
846 "Font Lock Mode face used to highlight preprocessor conditionals."
847 :group 'font-lock-faces)
849 (defface font-lock-warning-face
850 '((((class color) (background light)) (:foreground "Red" :bold t))
851 (((class color) (background dark)) (:foreground "Pink" :bold t))
852 (t (:inverse-video t :bold t)))
853 "Font Lock mode face used to highlight warnings."
854 :group 'font-lock-faces)
856 (defun font-lock-recompute-variables ()
857 ;; Is this a Draconian thing to do?
858 (mapc #'(lambda (buffer)
859 (with-current-buffer buffer
861 (font-lock-set-defaults t)))
864 ;; Backwards-compatible crud.
866 (defun font-lock-reset-all-faces ()
867 (dolist (face font-lock-face-list)
868 (face-spec-set face (get face 'face-defface-spec))))
870 (defun font-lock-use-default-fonts ()
871 "Reset the font-lock faces to a default set of fonts."
874 (font-lock-reset-all-faces))
876 (defun font-lock-use-default-colors ()
877 "Reset the font-lock faces to a default set of colors."
880 (font-lock-reset-all-faces))
882 (defun font-lock-use-default-minimal-decoration ()
883 "Reset the font-lock patterns to a fast, minimal set of decorations."
884 (and font-lock-maximum-decoration
885 (setq font-lock-maximum-decoration nil)
886 (font-lock-recompute-variables)))
888 (defun font-lock-use-default-maximal-decoration ()
889 "Reset the font-lock patterns to a larger set of decorations."
890 (and (not (eq t font-lock-maximum-decoration))
891 (setq font-lock-maximum-decoration t)
892 (font-lock-recompute-variables)))
894 (defun font-lock-add-keywords (mode keywords &optional how)
895 "Add highlighting KEYWORDS for MODE.
897 MODE should be a symbol, the major mode command name, such as `c-mode'
898 or nil. If nil, highlighting keywords are added for the current buffer.
899 KEYWORDS should be a list; see the variable `font-lock-keywords'.
900 By default they are added at the beginning of the current highlighting list.
901 If optional argument HOW is `set', they are used to replace the current
902 highlighting list. If HOW is any other non-nil value, they are added at the
903 end of the current highlighting list.
907 (font-lock-add-keywords 'c-mode
908 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
909 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . font-lock-keyword-face)))
911 adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
912 comments, and to fontify `and', `or' and `not' words as keywords.
914 The above procedure will only add the keywords for C mode, not
915 for modes derived from C mode. To add them for derived modes too,
916 pass nil for MODE and add the call to c-mode-hook.
920 (add-hook 'c-mode-hook
922 (font-lock-add-keywords nil
923 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
924 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" .
925 font-lock-keyword-face)))))
927 The above procedure may fail to add keywords to derived modes if
928 some involved major mode does not follow the standard conventions.
929 File a bug report if this happens, so the major mode can be corrected.
931 Note that some modes have specialized support for additional patterns, e.g.,
932 see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
933 `objc-font-lock-extra-types' and `java-font-lock-extra-types'."
935 ;; If MODE is non-nil, add the KEYWORDS and HOW spec to
936 ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
937 (let ((spec (cons keywords how)) cell)
938 (if (setq cell (assq mode font-lock-keywords-alist))
940 (setcdr cell (list spec))
941 (setcdr cell (append (cdr cell) (list spec))))
942 (push (list mode spec) font-lock-keywords-alist)))
943 ;; Make sure that `font-lock-removed-keywords-alist' does not
944 ;; contain the new keywords.
945 (font-lock-update-removed-keyword-alist mode keywords how))
947 ;; Otherwise set or add the keywords now.
948 ;; This is a no-op if it has been done already in this buffer
949 ;; for the correct major mode.
950 (font-lock-set-defaults)
951 (let ((was-compiled (eq (car font-lock-keywords) t)))
952 ;; Bring back the user-level (uncompiled) keywords.
954 (setq font-lock-keywords (cadr font-lock-keywords)))
955 ;; Now modify or replace them.
957 (setq font-lock-keywords keywords)
958 (font-lock-remove-keywords nil keywords) ;to avoid duplicates
959 (let ((old (if (eq (car-safe font-lock-keywords) t)
960 (cdr font-lock-keywords)
961 font-lock-keywords)))
962 (setq font-lock-keywords (if how
963 (append old keywords)
964 (append keywords old)))))
965 ;; If the keywords were compiled before, compile them again.
967 (setq font-lock-keywords
968 (font-lock-compile-keywords font-lock-keywords)))))))
970 (defun font-lock-update-removed-keyword-alist (mode keywords how)
971 "Update `font-lock-removed-keywords-alist' when adding new KEYWORDS to MODE."
972 ;; When font-lock is enabled first all keywords in the list
973 ;; `font-lock-keywords-alist' are added, then all keywords in the
974 ;; list `font-lock-removed-keywords-alist' are removed. If a
975 ;; keyword was once added, removed, and then added again it must be
976 ;; removed from the removed-keywords list. Otherwise the second add
977 ;; will not take effect.
978 (let ((cell (assq mode font-lock-removed-keywords-alist)))
981 ;; A new set of keywords is defined. Forget all about
982 ;; our old keywords that should be removed.
983 (setq font-lock-removed-keywords-alist
984 (delq cell font-lock-removed-keywords-alist))
985 ;; Delete all previously removed keywords.
986 (dolist (kword keywords)
987 (setcdr cell (delete kword (cdr cell))))
988 ;; Delete the mode cell if empty.
989 (if (null (cdr cell))
990 (setq font-lock-removed-keywords-alist
991 (delq cell font-lock-removed-keywords-alist)))))))
993 ;; Written by Anders Lindgren <andersl@andersl.com>.
996 ;; (I) The keywords are removed from a major mode.
997 ;; In this case the keyword could be local (i.e. added earlier by
998 ;; `font-lock-add-keywords'), global, or both.
1000 ;; (a) In the local case we remove the keywords from the variable
1001 ;; `font-lock-keywords-alist'.
1003 ;; (b) The actual global keywords are not known at this time.
1004 ;; All keywords are added to `font-lock-removed-keywords-alist',
1005 ;; when font-lock is enabled those keywords are removed.
1007 ;; Note that added keywords are taken out of the list of removed
1008 ;; keywords. This ensure correct operation when the same keyword
1009 ;; is added and removed several times.
1011 ;; (II) The keywords are removed from the current buffer.
1012 (defun font-lock-remove-keywords (mode keywords)
1013 "Remove highlighting KEYWORDS for MODE.
1015 MODE should be a symbol, the major mode command name, such as `c-mode'
1016 or nil. If nil, highlighting keywords are removed for the current buffer.
1018 To make the removal apply to modes derived from MODE as well,
1019 pass nil for MODE and add the call to MODE-hook. This may fail
1020 for some derived modes if some involved major mode does not
1021 follow the standard conventions. File a bug report if this
1022 happens, so the major mode can be corrected."
1024 ;; Remove one keyword at the time.
1025 (dolist (keyword keywords)
1026 (let ((top-cell (assq mode font-lock-keywords-alist)))
1027 ;; If MODE is non-nil, remove the KEYWORD from
1028 ;; `font-lock-keywords-alist'.
1030 (dolist (keyword-list-how-pair (cdr top-cell))
1031 ;; `keywords-list-how-pair' is a cons with a list of
1032 ;; keywords in the car top-cell and the original how
1033 ;; argument in the cdr top-cell.
1034 (setcar keyword-list-how-pair
1035 (delete keyword (car keyword-list-how-pair))))
1036 ;; Remove keyword list/how pair when the keyword list
1037 ;; is empty and how doesn't specify `set'. (If it
1038 ;; should be deleted then previously deleted keywords
1039 ;; would appear again.)
1040 (let ((cell top-cell))
1042 (if (and (null (car (car (cdr cell))))
1043 (not (eq (cdr (car (cdr cell))) 'set)))
1044 (setcdr cell (cdr (cdr cell)))
1045 (setq cell (cdr cell)))))
1046 ;; Final cleanup, remove major mode cell if last keyword
1048 (if (null (cdr top-cell))
1049 (setq font-lock-keywords-alist
1050 (delq top-cell font-lock-keywords-alist))))
1051 ;; Remember the keyword in case it is not local.
1052 (let ((cell (assq mode font-lock-removed-keywords-alist)))
1054 (unless (member keyword (cdr cell))
1055 (nconc cell (list keyword)))
1056 (push (cons mode (list keyword))
1057 font-lock-removed-keywords-alist))))))
1059 ;; Otherwise remove it immediately.
1060 (font-lock-set-defaults)
1061 (let ((was-compiled (eq (car font-lock-keywords) t)))
1062 ;; Bring back the user-level (uncompiled) keywords.
1064 (setq font-lock-keywords (cadr font-lock-keywords)))
1067 (setq font-lock-keywords (copy-sequence font-lock-keywords))
1068 (dolist (keyword keywords)
1069 (setq font-lock-keywords
1070 (delete keyword font-lock-keywords)))
1072 ;; If the keywords were compiled before, compile them again.
1074 (setq font-lock-keywords
1075 (font-lock-compile-keywords font-lock-keywords)))))))
1077 ;;;;;;;;;;;;;;;;;;;;;; actual code ;;;;;;;;;;;;;;;;;;;;;;
1079 ;;; To fontify the whole buffer by language syntax, we go through it a
1080 ;;; character at a time, creating extents on the boundary of each syntactic
1081 ;;; unit (that is, one extent for each block comment, one for each line
1082 ;;; comment, one for each string, etc.) This is done with the C function
1083 ;;; syntactically-sectionize. It's in C for speed (the speed of lisp function
1084 ;;; calls was a real bottleneck for this task since it involves examining each
1085 ;;; character in turn.)
1087 ;;; Then we make a second pass, to fontify the buffer based on other patterns
1088 ;;; specified by regexp. When we find a match for a region of text, we need
1089 ;;; to change the fonts on those characters. This is done with the
1090 ;;; put-text-property function, which knows how to efficiently share extents.
1091 ;;; Conceptually, we are attaching some particular face to each of the
1092 ;;; characters in a range, but the implementation of this involves creating
1093 ;;; extents, or resizing existing ones.
1095 ;;; Each time a modification happens to a line, we re-fontify the entire line.
1096 ;;; We do this by first removing the extents (text properties) on the line,
1097 ;;; and then doing the syntactic and keyword passes again on that line. (More
1098 ;;; generally, each modified region is extended to include the preceding and
1099 ;;; following BOL or EOL.)
1101 ;;; This means that, as the user types, we repeatedly go back to the beginning
1102 ;;; of the line, doing more work the longer the line gets. This doesn't cost
1103 ;;; much in practice, and if we don't, then we incorrectly fontify things when,
1104 ;;; for example, inserting spaces into `intfoo () {}'.
1108 ;; The user level functions
1111 (defun font-lock-mode (&optional arg)
1112 "Toggle Font Lock Mode.
1113 With arg, turn font-lock mode on if and only if arg is positive.
1115 When Font Lock mode is enabled, text is fontified as you type it:
1117 - Comments are displayed in `font-lock-comment-face';
1118 - Strings are displayed in `font-lock-string-face';
1119 - Documentation strings (in Lisp-like languages) are displayed in
1120 `font-lock-doc-string-face';
1121 - Language keywords (\"reserved words\") are displayed in
1122 `font-lock-keyword-face';
1123 - Function names in their defining form are displayed in
1124 `font-lock-function-name-face';
1125 - Variable names in their defining form are displayed in
1126 `font-lock-variable-name-face';
1127 - Type names are displayed in `font-lock-type-face';
1128 - References appearing in help files and the like are displayed
1129 in `font-lock-reference-face';
1130 - Preprocessor declarations are displayed in
1131 `font-lock-preprocessor-face';
1135 - Certain other expressions are displayed in other faces according
1136 to the value of the variable `font-lock-keywords'.
1138 Where modes support different levels of fontification, you can use the variable
1139 `font-lock-maximum-decoration' to specify which level you generally prefer.
1140 When you turn Font Lock mode on/off the buffer is fontified/defontified, though
1141 fontification occurs only if the buffer is less than `font-lock-maximum-size'.
1142 To fontify a buffer without turning on Font Lock mode, and regardless of buffer
1143 size, you can use \\[font-lock-fontify-buffer].
1145 See the variable `font-lock-keywords' for customization."
1147 (let ((on-p (if arg (> (prefix-numeric-value arg) 0) (not font-lock-mode)))
1148 (maximum-size (if (not (consp font-lock-maximum-size))
1149 font-lock-maximum-size
1150 (cdr (or (assq major-mode font-lock-maximum-size)
1151 (assq t font-lock-maximum-size))))))
1152 ;; Font-lock mode will refuse to turn itself on if in batch mode
1153 ;; to avoid potential (probably not actual, though) slowdown. We
1154 ;; used to try to "be nice" by avoiding doing this in temporary
1155 ;; buffers. But with the deferral code we don't need this, and it
1156 ;; definitely screws some things up.
1157 (if (noninteractive)
1160 (make-local-hook 'after-change-functions)
1161 (add-hook 'after-change-functions
1162 'font-lock-after-change-function nil t)
1163 (add-hook 'pre-idle-hook 'font-lock-pre-idle-hook))
1165 (remove-hook 'after-change-functions
1166 'font-lock-after-change-function t)
1167 (setq font-lock-defaults-computed nil
1168 font-lock-keywords nil)
1169 ;; We have no business doing this here, since
1170 ;; pre-idle-hook is global. Other buffers may
1171 ;; still be in font-lock mode. -dkindred@cs.cmu.edu
1172 ;; (remove-hook 'pre-idle-hook 'font-lock-pre-idle-hook)
1174 (set (make-local-variable 'font-lock-mode) on-p)
1176 (font-lock-set-defaults-1)
1177 (run-hooks 'font-lock-mode-hook)
1178 (cond (font-lock-fontified
1180 ((or (null maximum-size) (<= (buffer-size) maximum-size))
1181 (font-lock-fontify-buffer))
1183 (progress-feedback-with-label
1185 "Fontifying %s... buffer too big." 'abort
1187 (font-lock-fontified
1188 (setq font-lock-fontified nil)
1189 (font-lock-unfontify-region (point-min) (point-max))
1190 (font-lock-thing-lock-cleanup))
1192 (font-lock-thing-lock-cleanup)))
1195 ;; For init-file hooks
1197 (defun turn-on-font-lock ()
1198 "Unconditionally turn on Font Lock mode."
1203 (defun turn-off-font-lock ()
1204 "Unconditionally turn off Font Lock mode."
1210 ;; support for add-keywords, global-font-lock-mode and
1211 ;; font-lock-support-mode (unified support for various *-lock modes).
1214 ;; Fontification functions.
1216 ;; We first define some defsubsts to encapsulate the way we add
1217 ;; faces to a region of text. I am planning on modifying the
1218 ;; text-property mechanism so that multiple independent classes
1219 ;; of text properties can exist. That way, for example, ediff's
1220 ;; face text properties don't interfere with font lock's face
1221 ;; text properties. Due to the XEmacs implementation of text
1222 ;; properties in terms of extents, doing this is fairly trivial:
1223 ;; instead of using the `text-prop' property, you just use a
1224 ;; specified property.
1226 (defsubst font-lock-set-face (start end face)
1227 ;; Set the face on the characters in the range.
1228 (put-nonduplicable-text-property start end 'face face)
1229 (put-nonduplicable-text-property start end 'font-lock t))
1231 (defsubst font-lock-remove-face (start end)
1232 ;; Remove any syntax highlighting on the characters in the range.
1233 (put-nonduplicable-text-property start end 'face nil)
1234 (put-nonduplicable-text-property start end 'font-lock nil)
1235 (if lookup-syntax-properties
1236 (put-nonduplicable-text-property start end 'syntax-table nil)))
1238 (defsubst font-lock-set-syntax (start end syntax)
1239 ;; Set the face on the characters in the range.
1240 (put-nonduplicable-text-property start end 'syntax-table syntax)
1241 (put-nonduplicable-text-property start end 'font-lock t))
1243 (defsubst font-lock-any-faces-p (start end)
1244 ;; Return non-nil if we've put any syntax highlighting on
1245 ;; the characters in the range.
1247 ;; used to look for 'text-prop property, but this has problems if
1248 ;; you put any other text properties in the vicinity. Simon
1249 ;; Marshall suggested looking for the 'face property (this is what
1250 ;; FSF Emacs does) but that's equally bogus. Only reliable way is
1251 ;; for font-lock to specially mark its extents.
1253 ;; FSF's (equivalent) definition of this defsubst would be
1254 ;; (text-property-not-all start end 'font-lock nil)
1256 ;; Perhaps our `map-extents' is faster than our definition
1257 ;; of `text-property-not-all'. #### If so, `text-property-not-all'
1258 ;; should be fixed ...
1260 (map-extents 'extent-property (current-buffer) start (1- end) 'font-lock))
1263 ;; Fontification functions.
1265 ;; Rather than the function, e.g., `font-lock-fontify-region' containing the
1266 ;; code to fontify a region, the function runs the function whose name is the
1267 ;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally,
1268 ;; the value of this variable is, e.g., `font-lock-default-fontify-region'
1269 ;; which does contain the code to fontify a region. However, the value of the
1270 ;; variable could be anything and thus, e.g., `font-lock-fontify-region' could
1271 ;; do anything. The indirection of the fontification functions gives major
1272 ;; modes the capability of modifying the way font-lock.el fontifies. Major
1273 ;; modes can modify the values of, e.g., `font-lock-fontify-region-function',
1274 ;; via the variable `font-lock-defaults'.
1276 ;; For example, Rmail mode sets the variable `font-lock-defaults' so that
1277 ;; font-lock.el uses its own function for buffer fontification. This function
1278 ;; makes fontification be on a message-by-message basis and so visiting an
1279 ;; RMAIL file is much faster. A clever implementation of the function might
1280 ;; fontify the headers differently than the message body. (It should, and
1281 ;; correspondingly for Mail mode, but I can't be bothered to do the work. Can
1282 ;; you?) This hints at a more interesting use...
1284 ;; Languages that contain text normally contained in different major modes
1285 ;; could define their own fontification functions that treat text differently
1286 ;; depending on its context. For example, Perl mode could arrange that here
1287 ;; docs are fontified differently than Perl code. Or Yacc mode could fontify
1288 ;; rules one way and C code another. Neat!
1290 ;; A further reason to use the fontification indirection feature is when the
1291 ;; default syntactual fontification, or the default fontification in general,
1292 ;; is not flexible enough for a particular major mode. For example, perhaps
1293 ;; comments are just too hairy for `font-lock-fontify-syntactically-region' to
1294 ;; cope with. You need to write your own version of that function, e.g.,
1295 ;; `hairy-fontify-syntactically-region', and make your own version of
1296 ;; `hairy-fontify-region' call that function before calling
1297 ;; `font-lock-fontify-keywords-region' for the normal regexp fontification
1298 ;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el
1299 ;; would call your region fontification function instead of its own. For
1300 ;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line
1301 ;; directives correctly and cleanly. (It is the same problem as fontifying
1302 ;; multi-line strings and comments; regexps are not appropriate for the job.)
1305 (defun font-lock-fontify-buffer ()
1306 "Fontify the current buffer the way `font-lock-mode' would.
1307 See `font-lock-mode' for details.
1309 This can take a while for large buffers."
1311 (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
1312 (funcall font-lock-fontify-buffer-function)))
1314 (defun font-lock-unfontify-buffer ()
1315 (funcall font-lock-unfontify-buffer-function))
1317 (defun font-lock-fontify-region (beg end &optional loudly)
1318 (funcall font-lock-fontify-region-function beg end loudly))
1320 (defun font-lock-unfontify-region (beg end &optional loudly)
1321 (funcall font-lock-unfontify-region-function beg end loudly))
1323 (defun font-lock-default-fontify-buffer ()
1325 ;; if we don't widen, then the C code will fail to
1326 ;; realize that we're inside a comment.
1329 (let ((was-on font-lock-mode)
1330 (font-lock-verbose (or font-lock-verbose (interactive-p)))
1331 (font-lock-message-threshold 0)
1333 ;; Turn it on to run hooks and get the right font-lock-keywords.
1334 (or was-on (font-lock-mode 1))
1335 (font-lock-unfontify-region (point-min) (point-max) t)
1336 ;; (buffer-syntactic-context-flush-cache)
1338 ;; If a ^G is typed during fontification, abort the fontification, but
1339 ;; return normally (do not signal.) This is to make it easy to abort
1340 ;; fontification if it's taking a long time, without also causing the
1341 ;; buffer not to pop up. If a real abort is desired, the user can ^G
1344 ;; Possibly this should happen down in font-lock-fontify-region instead
1345 ;; of here, but since that happens from the after-change-hook (meaning
1346 ;; much more frequently) I'm afraid of the bad consequences of stealing
1347 ;; the interrupt character at inopportune times.
1351 (font-lock-fontify-region (point-min) (point-max)))
1355 (or was-on ; turn it off if it was off.
1356 (let ((font-lock-fontified nil)) ; kludge to prevent defontification
1357 (font-lock-mode 0)))
1358 (set (make-local-variable 'font-lock-fontified) t)
1359 (when (and aborted font-lock-verbose)
1360 (progress-feedback-with-label 'font-lock "Fontifying %s... aborted."
1361 'abort (buffer-name))))
1362 (run-hooks 'font-lock-after-fontify-buffer-hook)))
1364 (defun font-lock-default-unfontify-buffer ()
1365 (font-lock-unfontify-region (point-min) (point-max))
1366 (set (make-local-variable 'font-lock-fontified) nil))
1368 ;; This used to be `font-lock-fontify-region', and before that,
1369 ;; `font-lock-fontify-region' used to be the name used for what is now
1370 ;; `font-lock-fontify-syntactically-region'.
1371 (defun font-lock-default-fontify-region (beg end &optional loudly)
1372 (let ((modified (buffer-modified-p))
1373 (buffer-undo-list t) (inhibit-read-only t)
1374 (old-syntax-table (syntax-table))
1375 buffer-file-name buffer-file-truename)
1378 ;; Use the fontification syntax table, if any.
1379 (if font-lock-syntax-table (set-syntax-table font-lock-syntax-table))
1380 ;; Now do the fontification.
1381 (font-lock-unfontify-region beg end)
1382 (when font-lock-syntactic-keywords
1383 (font-lock-fontify-syntactic-keywords-region beg end))
1384 (unless font-lock-keywords-only
1385 (font-lock-fontify-syntactically-region beg end loudly))
1386 (font-lock-fontify-keywords-region beg end loudly))
1388 (set-syntax-table old-syntax-table)
1389 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
1391 ;; The following must be rethought, since keywords can override fontification.
1392 ; ;; Now scan for keywords, but not if we are inside a comment now.
1393 ; (or (and (not font-lock-keywords-only)
1394 ; (let ((state (parse-partial-sexp beg end nil nil
1395 ; font-lock-cache-state)))
1396 ; (or (nth 4 state) (nth 7 state))))
1397 ; (font-lock-fontify-keywords-region beg end))
1399 (defun font-lock-default-unfontify-region (beg end &optional maybe-loudly)
1400 (when (and maybe-loudly font-lock-verbose
1401 (>= (- end beg) font-lock-message-threshold))
1402 (progress-feedback-with-label 'font-lock "Fontifying %s..." 0
1404 (let ((modified (buffer-modified-p))
1405 (buffer-undo-list t) (inhibit-read-only t)
1406 buffer-file-name buffer-file-truename)
1407 (font-lock-remove-face beg end)
1408 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil))))
1410 ;; Following is the original FSF version (similar to our original
1411 ;; version, before the deferred stuff was added).
1413 ;; I think that lazy-lock v2 tries to do something similar.
1414 ;; Those efforts should be merged.
1416 ;; Called when any modification is made to buffer text.
1417 ;(defun font-lock-after-change-function (beg end old-len)
1420 ; ;; Rescan between start of line from `beg' and start of line after `end'.
1421 ; (font-lock-fontify-region
1422 ; (progn (goto-char beg) (beginning-of-line) (point))
1423 ; (progn (goto-char end) (forward-line 1) (point))))))
1425 (defvar font-lock-always-fontify-immediately nil
1426 "Set this to non-nil to disable font-lock deferral.
1427 Otherwise, changes to existing text will not be processed until the
1428 next redisplay cycle, avoiding excessive fontification when many
1429 buffer modifications are performed or a buffer is reverted.")
1431 ;; list of buffers in which there is a pending change.
1432 (defvar font-lock-pending-buffer-table (make-hash-table :weakness 'key))
1433 ;; table used to keep track of ranges needing fontification.
1434 (defvar font-lock-range-table (make-range-table))
1436 (defun font-lock-pre-idle-hook ()
1437 (condition-case font-lock-error
1438 (if (> (hash-table-count font-lock-pending-buffer-table) 0)
1439 (font-lock-fontify-pending-extents))
1440 (error (warn "Error caught in `font-lock-pre-idle-hook': %s"
1443 ;;; called when any modification is made to buffer text. This function
1444 ;;; remembers the changed ranges until the next redisplay, at which point
1445 ;;; the extents are merged and pruned, and the resulting ranges fontified.
1446 ;;; This function could easily be adapted to other after-change-functions.
1448 (defun font-lock-after-change-function (beg end old-len)
1449 (when font-lock-mode
1450 ;; treat deletions as if the following character (or previous, if
1451 ;; there is no following) were inserted. (also use the previous
1452 ;; character at end of line. this avoids a problem when you
1453 ;; insert a comment on the line before a line of code: if we use
1454 ;; the following char, then when you hit backspace, the following
1455 ;; line of code turns the comment color.) this is a bit of a hack
1456 ;; but allows us to use text properties for everything.
1458 (cond ((not (save-excursion (goto-char end) (eolp)))
1459 (setq end (1+ end)))
1460 ((/= beg (point-min)) (setq beg (1- beg)))
1462 (put-text-property beg end 'font-lock-pending t)
1463 (puthash (current-buffer) t font-lock-pending-buffer-table)
1464 (if font-lock-always-fontify-immediately
1465 (font-lock-fontify-pending-extents))))
1467 (defun font-lock-fontify-pending-extents ()
1468 ;; ah, the beauty of mapping functions.
1469 ;; this function is actually shorter than the old version, which handled
1470 ;; only one buffer and one contiguous region!
1473 #'(lambda (buffer dummy)
1474 ;; remove first, to avoid infinite reprocessing if error
1475 (remhash buffer font-lock-pending-buffer-table)
1476 (when (buffer-live-p buffer)
1477 (clear-range-table font-lock-range-table)
1478 (with-current-buffer buffer
1481 ;; if we don't widen, then the C code in
1482 ;; syntactically-sectionize will fail to realize that
1483 ;; we're inside a comment. #### We don't actually use
1484 ;; syntactically-sectionize any more. Do we still
1487 (let ((zmacs-region-stays
1488 zmacs-region-stays)) ; protect from change!
1490 #'(lambda (ex dummy-maparg)
1491 ;; first expand the ranges to full lines,
1492 ;; because that is what will be fontified;
1493 ;; then use a range table to merge the
1494 ;; ranges. (we could also do this simply using
1495 ;; text properties. the range table code was
1496 ;; here from a previous version of this code
1497 ;; and works just as well.)
1498 (let* ((beg (extent-start-position ex))
1499 (end (extent-end-position ex))
1500 (beg (progn (goto-char beg)
1503 (end (progn (goto-char end)
1506 (put-range-table beg end t
1507 font-lock-range-table)))
1508 nil nil nil nil nil 'font-lock-pending t)
1509 ;; clear all pending extents first in case of error below.
1510 (put-text-property (point-min) (point-max)
1511 'font-lock-pending nil)
1513 #'(lambda (beg end val)
1514 ;; This creates some unnecessary progress gauges.
1515 ;; (if (and (= beg (point-min))
1516 ;; (= end (point-max)))
1517 ;; (font-lock-fontify-buffer)
1518 ;; (font-lock-fontify-region beg end)))
1519 (font-lock-fontify-region beg end))
1520 font-lock-range-table)))))))
1521 font-lock-pending-buffer-table)))
1523 ;; Syntactic fontification functions.
1525 (defun font-lock-lisp-like (mode)
1526 ;; Note: (or (get mode 'font-lock-lisp-like) (string-match ...)) is
1527 ;; not enough because the property needs to be able to specify a nil
1529 (if (plist-member (symbol-plist mode) 'font-lock-lisp-like)
1530 (get mode 'font-lock-lisp-like)
1531 ;; If the property is not specified, guess. Similar logic exists
1532 ;; in add-log, but I think this encompasses more modes.
1533 (string-match "lisp\\|scheme" (symbol-name mode))))
1535 ;; fontify-syntactically-region used to use syntactically-sectionize, which
1536 ;; was supposedly much faster than the FSF version because it was written in
1537 ;; C. However, the FSF version uses parse-partial-sexp, which is also
1538 ;; written in C, and the benchmarking I did showed the
1539 ;; syntactically-sectionize code to be slower overall. So here's the
1540 ;; FSF version, modified to support font-lock-doc-string-face.
1541 ;; -- mct 2000-12-29
1542 ;; #### Andy conditionally reverted Matt's change when we were experimenting
1543 ;; with making lookup-syntax-properties an optional feature. I don't see how
1544 ;; this code relates to lookup-syntax-properties, though. I wonder if the
1545 ;; bug is in our (?) version of parse-partial-sexp. Andy says no. Of course,
1546 ;; Matt benchmarked ... WTF knows? sjt 2002-09-28
1547 (defun font-lock-fontify-syntactically-region (start end &optional loudly)
1548 "Put proper face on each string and comment between START and END.
1549 START should be at the beginning of a line. Optional argument LOUDLY
1550 is currently ignored."
1551 (if font-lock-keywords-only
1554 ;; #### Shouldn't this just be using 'loudly??
1555 (when (and font-lock-verbose
1556 (>= (- end start) font-lock-message-threshold))
1557 (progress-feedback-with-label 'font-lock
1558 "Fontifying %s... (syntactically)" 5
1562 (let ((lisp-like (font-lock-lisp-like major-mode))
1563 (cache (marker-position font-lock-cache-position))
1564 state string beg depth)
1566 ;; Find the state at the `beginning-of-line' before `start'.
1567 (if (eq start cache)
1568 ;; Use the cache for the state of `start'.
1569 (setq state font-lock-cache-state)
1570 ;; Find the state of `start'.
1571 (if (null font-lock-beginning-of-syntax-function)
1572 ;; Use the state at the previous cache position, if any, or
1573 ;; otherwise calculate from `point-min'.
1574 (if (or (null cache) (< start cache))
1575 (setq state (parse-partial-sexp (point-min) start))
1576 (setq state (parse-partial-sexp cache start nil nil
1577 font-lock-cache-state)))
1578 ;; Call the function to move outside any syntactic block.
1579 (funcall font-lock-beginning-of-syntax-function)
1580 (setq state (parse-partial-sexp (point) start)))
1581 ;; Cache the state and position of `start'.
1582 (setq font-lock-cache-state state)
1583 (set-marker font-lock-cache-position start))
1585 ;; If the region starts inside a string or comment, show the extent of it.
1586 (when (or (nth 3 state) (nth 4 state))
1587 (setq string (nth 3 state) beg (point))
1588 (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
1589 (font-lock-set-face beg (point) (if string
1590 font-lock-string-face
1591 font-lock-comment-face)))
1593 ;; Find each interesting place between here and `end'.
1594 (while (and (< (point) end)
1596 (setq state (parse-partial-sexp (point) end nil nil state
1598 (or (nth 3 state) (nth 4 state))))
1599 (setq depth (nth 0 state) string (nth 3 state) beg (nth 8 state))
1600 (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
1602 ;; #### It would be nice if we handled Python and other
1603 ;; non-Lisp languages with docstrings correctly.
1604 (let ((face (if (and lisp-like (= depth 1))
1605 'font-lock-doc-string-face
1606 'font-lock-string-face)))
1607 (if font-lock-fontify-string-delimiters
1608 (font-lock-set-face beg (point) face)
1609 (font-lock-set-face (+ beg 1) (- (point) 1) face)))
1610 (font-lock-set-face beg (point)
1611 font-lock-comment-face))))))
1613 ;;; Additional text property functions.
1615 ;; The following three text property functions are not generally available (and
1616 ;; it's not certain that they should be) so they are inlined for speed.
1617 ;; The case for `fillin-text-property' is simple; it may or not be generally
1618 ;; useful. (Since it is used here, it is useful in at least one place.;-)
1619 ;; However, the case for `append-text-property' and `prepend-text-property' is
1620 ;; more complicated. Should they remove duplicate property values or not? If
1621 ;; so, should the first or last duplicate item remain? Or the one that was
1622 ;; added? In our implementation, the first duplicate remains.
1624 ;; XEmacs: modified all these functions to use
1625 ;; `put-nonduplicable-text-property' instead of `put-text-property', and
1626 ;; the first one to take both SETPROP and MARKPROP, in accordance with the
1627 ;; changed definitions of `font-lock-any-faces-p' and `font-lock-set-face'.
1629 (defsubst font-lock-fillin-text-property (start end setprop markprop value &optional object)
1630 "Fill in one property of the text from START to END.
1631 Arguments PROP and VALUE specify the property and value to put where none are
1632 already in place. Therefore existing property values are not overwritten.
1633 Optional argument OBJECT is the string or buffer containing the text."
1634 (let ((start (text-property-any start end markprop nil object)) next)
1636 (setq next (next-single-property-change start markprop object end))
1637 (put-nonduplicable-text-property start next setprop value object)
1638 (put-nonduplicable-text-property start next markprop value object)
1639 (setq start (text-property-any next end markprop nil object)))))
1641 ;; This function (from simon's unique.el) is rewritten and inlined for speed.
1642 ;(defun unique (list function)
1643 ; "Uniquify LIST, deleting elements using FUNCTION.
1644 ;Return the list with subsequent duplicate items removed by side effects.
1645 ;FUNCTION is called with an element of LIST and a list of elements from LIST,
1646 ;and should return the list of elements with occurrences of the element removed,
1647 ;i.e., a function such as `delete' or `delq'.
1648 ;This function will work even if LIST is unsorted. See also `uniq'."
1649 ; (let ((list list))
1651 ; (setq list (setcdr list (funcall function (car list) (cdr list))))))
1654 (defsubst font-lock-unique (list)
1655 "Uniquify LIST, deleting elements using `delq'.
1656 Return the list with subsequent duplicate items removed by side effects."
1659 (setq list (setcdr list (delq (car list) (cdr list))))))
1662 ;; A generalisation of `facemenu-add-face' for any property, but without the
1663 ;; removal of inactive faces via `facemenu-discard-redundant-faces' and special
1664 ;; treatment of `default'. Uses `unique' to remove duplicate property values.
1665 (defsubst font-lock-prepend-text-property (start end prop value &optional object)
1666 "Prepend to one property of the text from START to END.
1667 Arguments PROP and VALUE specify the property and value to prepend to the value
1668 already in place. The resulting property values are always lists, and unique.
1669 Optional argument OBJECT is the string or buffer containing the text."
1670 (let ((val (if (listp value) value (list value))) next prev)
1671 (while (/= start end)
1672 (setq next (next-single-property-change start prop object end)
1673 prev (get-text-property start prop object))
1676 (font-lock-unique (append val (if (listp prev) prev (list prev))))
1678 (setq start next))))
1680 (defsubst font-lock-append-text-property (start end prop value &optional object)
1681 "Append to one property of the text from START to END.
1682 Arguments PROP and VALUE specify the property and value to append to the value
1683 already in place. The resulting property values are always lists, and unique.
1684 Optional argument OBJECT is the string or buffer containing the text."
1685 (let ((val (if (listp value) value (list value))) next prev)
1686 (while (/= start end)
1687 (setq next (next-single-property-change start prop object end)
1688 prev (get-text-property start prop object))
1691 (font-lock-unique (append (if (listp prev) prev (list prev)) val))
1693 (setq start next))))
1695 ;;; Syntactic regexp fontification functions (taken from FSF Emacs 20.7.1)
1697 ;; These syntactic keyword pass functions are identical to those keyword pass
1698 ;; functions below, with the following exceptions; (a) they operate on
1699 ;; `font-lock-syntactic-keywords' of course, (b) they are all `defun' as speed
1700 ;; is less of an issue, (c) eval of property value does not occur JIT as speed
1701 ;; is less of an issue, (d) OVERRIDE cannot be `prepend' or `append' as it
1702 ;; makes no sense for `syntax-table' property values, (e) they do not do it
1703 ;; LOUDLY as it is not likely to be intensive.
1705 (defun font-lock-apply-syntactic-highlight (highlight)
1706 "Apply HIGHLIGHT following a match.
1707 HIGHLIGHT should be of the form MATCH-HIGHLIGHT,
1708 see `font-lock-syntactic-keywords'."
1709 (let* ((match (nth 0 highlight))
1710 (start (match-beginning match)) (end (match-end match))
1711 (value (nth 1 highlight))
1712 (override (nth 2 highlight)))
1713 (unless (numberp (car-safe value))
1714 (setq value (eval value)))
1716 ;; No match but we might not signal an error.
1717 (or (nth 3 highlight)
1718 (error "No match %d in highlight %S" match highlight)))
1720 ;; Cannot override existing fontification.
1721 (or (map-extents 'extent-property (current-buffer)
1722 start end 'syntax-table)
1723 (font-lock-set-syntax start end value)))
1725 ;; Override existing fontification.
1726 (font-lock-set-syntax start end value))
1727 ((eq override 'keep)
1728 ;; Keep existing fontification.
1729 (font-lock-fillin-text-property start end
1730 'syntax-table 'font-lock value)))))
1732 (defun font-lock-fontify-syntactic-anchored-keywords (keywords limit)
1733 "Fontify according to KEYWORDS until LIMIT.
1734 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1735 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1736 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1737 ;; Evaluate PRE-MATCH-FORM.
1738 (pre-match-value (eval (nth 1 keywords))))
1739 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1740 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1741 (setq limit pre-match-value)
1742 (save-excursion (end-of-line) (setq limit (point))))
1744 ;; Find an occurrence of `matcher' before `limit'.
1745 (while (if (stringp matcher)
1746 (re-search-forward matcher limit t)
1747 (funcall matcher limit))
1748 ;; Apply each highlight to this instance of `matcher'.
1749 (setq highlights lowdarks)
1751 (font-lock-apply-syntactic-highlight (car highlights))
1752 (setq highlights (cdr highlights)))))
1753 ;; Evaluate POST-MATCH-FORM.
1754 (eval (nth 2 keywords))))
1756 (defun font-lock-fontify-syntactic-keywords-region (start end)
1757 "Fontify according to `font-lock-syntactic-keywords' between START and END.
1758 START should be at the beginning of a line."
1759 ;; ;; If `font-lock-syntactic-keywords' is a symbol, get the real keywords.
1760 (when (symbolp font-lock-syntactic-keywords)
1761 (setq font-lock-syntactic-keywords (font-lock-eval-keywords
1762 font-lock-syntactic-keywords)))
1763 ;; If `font-lock-syntactic-keywords' is not compiled, compile it.
1764 (unless (eq (car font-lock-syntactic-keywords) t)
1765 (setq font-lock-syntactic-keywords (font-lock-compile-keywords
1766 font-lock-syntactic-keywords)))
1767 ;; Get down to business.
1768 (let ((case-fold-search font-lock-keywords-case-fold-search)
1769 (keywords (cdr font-lock-syntactic-keywords))
1770 keyword matcher highlights)
1772 ;; Find an occurrence of `matcher' from `start' to `end'.
1773 (setq keyword (car keywords) matcher (car keyword))
1775 (while (if (stringp matcher)
1776 (re-search-forward matcher end t)
1777 (funcall matcher end))
1778 ;; Apply each highlight to this instance of `matcher', which may be
1779 ;; specific highlights or more keywords anchored to `matcher'.
1780 (setq highlights (cdr keyword))
1782 (if (numberp (car (car highlights)))
1783 (font-lock-apply-syntactic-highlight (car highlights))
1784 (font-lock-fontify-syntactic-anchored-keywords (car highlights)
1786 (setq highlights (cdr highlights))))
1787 (setq keywords (cdr keywords)))))
1789 ;;; Regexp fontification functions.
1791 (defsubst font-lock-apply-highlight (highlight)
1792 "Apply HIGHLIGHT following a match.
1793 HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
1794 (let* ((match (nth 0 highlight))
1795 (start (match-beginning match)) (end (match-end match))
1796 (override (nth 2 highlight)))
1797 (let ((newface (nth 1 highlight)))
1798 (or (symbolp newface)
1799 (setq newface (eval newface)))
1801 ;; No match but we might not signal an error.
1802 (or (nth 3 highlight)
1803 (error "No match %d in highlight %S" match highlight)))
1806 ;; Cannot override existing fontification.
1807 (or (font-lock-any-faces-p start end)
1808 (font-lock-set-face start end newface)))
1810 ;; Override existing fontification.
1811 (font-lock-set-face start end newface))
1812 ((eq override 'keep)
1813 ;; Keep existing fontification.
1814 (font-lock-fillin-text-property start end 'face 'font-lock
1816 ((eq override 'prepend)
1817 ;; Prepend to existing fontification.
1818 (font-lock-prepend-text-property start end 'face newface))
1819 ((eq override 'append)
1820 ;; Append to existing fontification.
1821 (font-lock-append-text-property start end 'face newface))))))
1823 (defsubst font-lock-fontify-anchored-keywords (keywords limit)
1824 "Fontify according to KEYWORDS until LIMIT.
1825 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1826 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1827 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1828 ;; Evaluate PRE-MATCH-FORM.
1829 (pre-match-value (eval (nth 1 keywords))))
1830 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1831 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1832 (setq limit pre-match-value)
1833 (save-excursion (end-of-line) (setq limit (point))))
1835 ;; Find an occurrence of `matcher' before `limit'.
1836 (while (if (stringp matcher)
1837 (re-search-forward matcher limit t)
1838 (funcall matcher limit))
1839 ;; Apply each highlight to this instance of `matcher'.
1840 (setq highlights lowdarks)
1842 (font-lock-apply-highlight (car highlights))
1843 (setq highlights (cdr highlights)))))
1844 ;; Evaluate POST-MATCH-FORM.
1845 (eval (nth 2 keywords))))
1847 (defun font-lock-fontify-keywords-region (start end &optional loudvar)
1848 "Fontify according to `font-lock-keywords' between START and END.
1849 START should be at the beginning of a line."
1850 (let ((loudly (and font-lock-verbose
1851 (>= (- end start) font-lock-message-threshold))))
1852 (unless (eq (car-safe font-lock-keywords) t)
1853 (setq font-lock-keywords
1854 (font-lock-compile-keywords font-lock-keywords)))
1855 (let* ((case-fold-search font-lock-keywords-case-fold-search)
1856 (keywords (cdr font-lock-keywords))
1857 (bufname (buffer-name))
1858 (progress 5) (old-progress 5)
1860 (nkeywords (length keywords))
1861 keyword matcher highlights)
1863 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1864 ;; In order to measure progress accurately we need to know how
1865 ;; many keywords we have and how big the region is. Then progress
1866 ;; is ((pos - start)/ (end - start) * nkeywords
1867 ;; + iteration / nkeywords) * 100
1870 ;; Find an occurrence of `matcher' from `start' to `end'.
1871 (setq keyword (car keywords) matcher (car keyword))
1873 (while (and (< (point) end)
1874 (if (stringp matcher)
1875 (re-search-forward matcher end t)
1876 (funcall matcher end)))
1877 ;; calculate progress
1879 (+ (/ (* (- (point) start) 95) (* (- end start) nkeywords))
1880 (/ (* iter 95) nkeywords) 5))
1881 (when (and loudly (> progress old-progress))
1882 (progress-feedback-with-label 'font-lock
1883 "Fontifying %s... (regexps)"
1885 (setq old-progress progress)
1886 ;; Apply each highlight to this instance of `matcher', which may be
1887 ;; specific highlights or more keywords anchored to `matcher'.
1888 (setq highlights (cdr keyword))
1890 (if (numberp (car (car highlights)))
1891 (let ((end (match-end (car (car highlights)))))
1892 (font-lock-apply-highlight (car highlights))
1893 ;; restart search just after the end of the
1894 ;; keyword so keywords can share bracketing
1896 (and end (goto-char end)))
1897 (font-lock-fontify-anchored-keywords (car highlights) end))
1898 (setq highlights (cdr highlights))))
1899 (setq iter (1+ iter))
1900 (setq keywords (cdr keywords))))
1902 (progress-feedback-with-label 'font-lock "Fontifying %s... " 100
1906 ;; Various functions.
1908 ;; Turn off other related packages if they're on. I prefer a hook. --sm.
1909 ;; These explicit calls are easier to understand
1910 ;; because people know what they will do.
1911 ;; A hook is a mystery because it might do anything whatever. --rms.
1912 (defun font-lock-thing-lock-cleanup ()
1913 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
1914 (fast-lock-mode -1))
1915 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
1916 (lazy-lock-mode -1))
1917 ((and (boundp 'lazy-shot-mode) lazy-shot-mode)
1918 (lazy-shot-mode -1))))
1920 ;; Do something special for these packages after fontifying. I prefer a hook.
1921 (defun font-lock-after-fontify-buffer ()
1922 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
1923 (fast-lock-after-fontify-buffer))
1924 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
1925 (lazy-lock-after-fontify-buffer))))
1928 ;; Various functions.
1930 (defun font-lock-compile-keywords (keywords)
1931 "Compile KEYWORDS (a list) and return the list of compiled keywords.
1932 Each keyword has the form (MATCHER HIGHLIGHT ...). See `font-lock-keywords'."
1933 (if (eq (car-safe keywords) t)
1935 (cons t (mapcar 'font-lock-compile-keyword keywords))))
1937 (defun font-lock-compile-keyword (keyword)
1938 (cond ((nlistp keyword) ; Just MATCHER
1939 (list keyword '(0 font-lock-keyword-face)))
1940 ((eq (car keyword) 'eval) ; Specified (eval . FORM)
1941 (font-lock-compile-keyword (eval (cdr keyword))))
1942 ((numberp (cdr keyword)) ; Specified (MATCHER . MATCH)
1943 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
1944 ((symbolp (cdr keyword)) ; Specified (MATCHER . FACENAME)
1945 (list (car keyword) (list 0 (cdr keyword))))
1946 ((nlistp (nth 1 keyword)) ; Specified (MATCHER . HIGHLIGHT)
1947 (list (car keyword) (cdr keyword)))
1948 (t ; Hopefully (MATCHER HIGHLIGHT ...)
1951 (defun font-lock-eval-keywords (keywords)
1952 "Evaluate KEYWORDS if a function (funcall) or variable (eval) name."
1953 (if (listp keywords)
1955 (font-lock-eval-keywords (if (fboundp keywords)
1959 (defun font-lock-choose-keywords (keywords level)
1960 ;; Return LEVELth element of KEYWORDS. A LEVEL of nil is equal to a
1961 ;; LEVEL of 0, a LEVEL of t is equal to (1- (length KEYWORDS)).
1962 (let ((level (if (not (consp level))
1964 (cdr (or (assq major-mode level) (assq t level))))))
1965 (cond ((symbolp keywords)
1968 (or (nth level keywords) (car (reverse keywords))))
1970 (car (reverse keywords)))
1975 ;;; Determining which set of font-lock keywords to use.
1977 (defun font-lock-find-font-lock-defaults (modesym)
1978 ;; Get the defaults based on the major mode.
1980 ;; I want a do-while loop!
1982 (setq raw-defaults (get modesym 'font-lock-defaults))
1983 (and raw-defaults (symbolp raw-defaults)
1984 (setq modesym raw-defaults)))
1988 (defun font-lock-examine-syntax-table ()
1989 ; Computes the value of font-lock-keywords-only for this buffer.
1990 (if (eq (syntax-table) (standard-syntax-table))
1991 ;; Assume that modes which haven't bothered to install their own
1992 ;; syntax table don't do anything syntactically interesting.
1993 ;; Really, the standard-syntax-table shouldn't have comments and
1994 ;; strings in it, but changing that now might break things.
1996 ;; else map over the syntax table looking for strings or comments.
1999 (if (fboundp 'map-syntax-table)
2002 #'(lambda (key value)
2003 (memq (char-syntax-from-code value)
2004 '(?\" ?\< ?\> ?\$)))
2007 (let ((i (1- (length (syntax-table)))))
2009 (if (memq (char-syntax i) '(?\" ?\< ?\> ?\$))
2010 (setq got-one t i 0))
2012 (set (make-local-variable 'font-lock-keywords-only) (not got-one)))))
2014 ;; font-lock-set-defaults is in fontl-hooks.el.
2017 (defun font-lock-set-defaults-1 (&optional explicit-defaults)
2018 ;; does everything that font-lock-set-defaults does except
2019 ;; enable font-lock-mode. This is called by `font-lock-mode'.
2020 ;; Note that the return value is used!
2022 (if (and font-lock-defaults-computed (not explicit-defaults))
2026 (or font-lock-keywords
2027 (let* ((defaults (or (and (not (eq t explicit-defaults))
2029 ;; in case modes decide to set
2030 ;; `font-lock-defaults' themselves,
2033 (font-lock-find-font-lock-defaults major-mode)))
2034 (keywords (font-lock-choose-keywords
2035 (nth 0 defaults) font-lock-maximum-decoration)))
2038 (setq font-lock-keywords (if (fboundp keywords)
2041 (or font-lock-keywords
2043 ;; try to look for a variable `foo-mode-font-lock-keywords',
2045 (let ((major (symbol-name major-mode))
2047 (if (stringp n) (setq n (intern-soft n)))
2052 (setq font-lock-keywords
2054 (or (funcall try (get major-mode 'font-lock-keywords))
2055 (funcall try (concat major "-font-lock-keywords"))
2056 (funcall try (and (string-match "-mode\\'" major)
2059 (match-beginning 0))
2060 "-font-lock-keywords")))
2061 'font-lock-keywords)))))
2064 (if (>= (length defaults) 3)
2065 (setq font-lock-keywords-case-fold-search (nth 2 defaults))
2067 ;; look for a property 'font-lock-keywords-case-fold-search on
2068 ;; the major-mode symbol.
2069 (let* ((nonexist (make-symbol ""))
2070 (value (get major-mode 'font-lock-keywords-case-fold-search
2072 (if (not (eq nonexist value))
2073 (setq font-lock-keywords-case-fold-search value))))
2076 (if (>= (length defaults) 2)
2077 (setq font-lock-keywords-only (nth 1 defaults))
2079 ;; cleverly examine the syntax table.
2080 (font-lock-examine-syntax-table))
2083 (if (nth 3 defaults)
2084 (let ((slist (nth 3 defaults)))
2085 (setq font-lock-syntax-table
2086 (copy-syntax-table (syntax-table)))
2088 (modify-syntax-entry (car (car slist)) (cdr (car slist))
2089 font-lock-syntax-table)
2090 (setq slist (cdr slist)))))
2094 (setq font-lock-beginning-of-syntax-function
2098 ;; defaults not specified at all, so use `beginning-of-defun'.
2099 (setq font-lock-beginning-of-syntax-function
2100 'beginning-of-defun)))))
2102 (setq font-lock-cache-position (make-marker))
2103 (setq font-lock-defaults-computed t)))
2106 ;;;;;;;;;;;;;;;;;;;;;; keywords ;;;;;;;;;;;;;;;;;;;;;;
2108 ;;; Various major-mode interfaces.
2109 ;;; Probably these should go in with the source of the respective major modes.
2111 ;; The defaults and keywords listed here should perhaps be moved into
2112 ;; mode-specific files.
2114 ;; For C and Lisp modes we use `beginning-of-defun', rather than nil,
2115 ;; for SYNTAX-BEGIN. Thus the calculation of the cache is usually
2116 ;; faster but not infallible, so we risk mis-fontification. --sm.
2118 (put 'c-mode 'font-lock-defaults
2119 '((c-font-lock-keywords
2120 c-font-lock-keywords-1 c-font-lock-keywords-2 c-font-lock-keywords-3)
2121 nil nil ((?_ . "w")) beginning-of-defun))
2122 (put 'c++-c-mode 'font-lock-defaults 'c-mode)
2123 (put 'elec-c-mode 'font-lock-defaults 'c-mode)
2125 (put 'c++-mode 'font-lock-defaults
2126 '((c++-font-lock-keywords
2127 c++-font-lock-keywords-1 c++-font-lock-keywords-2
2128 c++-font-lock-keywords-3)
2129 nil nil ((?_ . "w") (?~ . "w")) beginning-of-defun))
2131 (put 'java-mode 'font-lock-defaults
2132 '((java-font-lock-keywords
2133 java-font-lock-keywords-1 java-font-lock-keywords-2
2134 java-font-lock-keywords-3)
2135 nil nil ((?_ . "w")) beginning-of-defun
2136 (font-lock-mark-block-function . mark-defun)))
2138 (put 'lisp-mode 'font-lock-defaults
2139 '((lisp-font-lock-keywords
2140 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
2142 ((?: . "w") (?- . "w") (?* . "w") (?+ . "w") (?. . "w") (?< . "w")
2143 (?> . "w") (?= . "w") (?! . "w") (?? . "w") (?$ . "w") (?% . "w")
2144 (?_ . "w") (?& . "w") (?~ . "w") (?^ . "w") (?/ . "w"))
2145 beginning-of-defun))
2146 (put 'emacs-lisp-mode 'font-lock-defaults 'lisp-mode)
2147 (put 'lisp-interaction-mode 'font-lock-defaults 'lisp-mode)
2149 (put 'scheme-mode 'font-lock-defaults
2150 '(scheme-font-lock-keywords
2152 ((?: . "w") (?- . "w") (?* . "w") (?+ . "w") (?. . "w") (?< . "w")
2153 (?> . "w") (?= . "w") (?! . "w") (?? . "w") (?$ . "w") (?% . "w")
2154 (?_ . "w") (?& . "w") (?~ . "w") (?^ . "w") (?/ . "w"))
2155 beginning-of-defun))
2156 (put 'inferior-scheme-mode 'font-lock-defaults 'scheme-mode)
2157 (put 'scheme-interaction-mode 'font-lock-defaults 'scheme-mode)
2159 (put 'tex-mode 'font-lock-defaults
2160 ;; For TeX modes we could use `backward-paragraph' for the same reason.
2161 '(tex-font-lock-keywords nil nil ((?$ . "\""))))
2162 ;; the nine billion names of TeX mode...
2163 (put 'bibtex-mode 'font-lock-defaults 'tex-mode)
2164 (put 'plain-tex-mode 'font-lock-defaults 'tex-mode)
2165 (put 'slitex-tex-mode 'font-lock-defaults 'tex-mode)
2166 (put 'SliTeX-mode 'font-lock-defaults 'tex-mode)
2167 (put 'slitex-mode 'font-lock-defaults 'tex-mode)
2168 (put 'latex-tex-mode 'font-lock-defaults 'tex-mode)
2169 (put 'LaTex-tex-mode 'font-lock-defaults 'tex-mode)
2170 (put 'latex-mode 'font-lock-defaults 'tex-mode)
2171 (put 'LaTeX-mode 'font-lock-defaults 'tex-mode)
2172 (put 'japanese-LaTeX-mode 'font-lock-defaults 'tex-mode)
2173 (put 'japanese-SliTeX-mode 'font-lock-defaults 'tex-mode)
2174 (put 'FoilTeX-mode 'font-lock-defaults 'tex-mode)
2175 (put 'LATeX-MoDe 'font-lock-defaults 'tex-mode)
2176 (put 'lATEx-mODe 'font-lock-defaults 'tex-mode)
2177 ;; ok, this is getting a bit silly ...
2178 (put 'eDOm-xETAl 'font-lock-defaults 'tex-mode)
2180 ;;; Various regexp information shared by several modes.
2181 ;;; Information specific to a single mode should go in its load library.
2183 (defconst lisp-font-lock-keywords-1
2185 ;; Anything not a variable or type declaration is fontified as a function.
2186 ;; It would be cleaner to allow preceding whitespace, but it would also be
2187 ;; about five times slower.
2188 (list (concat "^(\\(def\\("
2189 ;; Variable declarations.
2190 "\\(const\\(\\|ant\\)\\|ine-key\\(\\|-after\\)\\|var\\|custom\\)\\|"
2191 ;; Structure declarations.
2192 "\\(class\\|struct\\|type\\)\\|"
2193 ;; Everything else is a function declaration.
2194 "\\([^ \t\n\(\)]+\\)"
2196 ;; Any whitespace and declared object.
2198 "\\([^ \t\n\(\)]+\\)?")
2199 '(1 font-lock-keyword-face)
2200 '(8 (cond ((match-beginning 3) 'font-lock-variable-name-face)
2201 ((match-beginning 6) 'font-lock-type-face)
2202 (t 'font-lock-function-name-face))
2205 "Subdued level highlighting Lisp modes.")
2207 (defconst lisp-font-lock-keywords-2
2208 (append lisp-font-lock-keywords-1
2211 ;; Control structures. ELisp and CLisp combined.
2216 ;; beginning of generated stuff
2217 ;; to regenerate, use the regexp-opt below, then delete the outermost
2218 ;; grouping, then use the macro below to break up the string.
2220 ;; '("cond" "if" "while" "let" "let*" "prog" "progn" "prog1"
2221 ;; "prog2" "progv" "catch" "throw" "save-restriction"
2222 ;; "save-excursion" "save-window-excursion"
2223 ;; "save-current-buffer" "with-current-buffer"
2224 ;; "save-selected-window" "with-selected-window"
2225 ;; "save-selected-frame" "with-selected-frame"
2226 ;; "with-temp-file" "with-temp-buffer" "with-output-to-string"
2227 ;; "with-string-as-buffer-contents"
2228 ;; "save-match-data" "unwind-protect" "call-with-condition-handler"
2229 ;; "condition-case" "track-mouse" "autoload"
2230 ;; "eval-after-load" "eval-and-compile" "eval-when-compile"
2231 ;; "when" "unless" "do" "dolist" "dotimes" "flet" "labels"
2232 ;; "lambda" "block" "return" "return-from" "loop") t)
2233 ;; (setq last-kbd-macro
2234 ;; (read-kbd-macro "\" C-7 C-1 <right> C-r \\\\| 3*<right> \" RET"))
2235 "autoload\\|block\\|c\\(?:a\\(?:ll-with-condition-handler\\|tch\\)\\|"
2236 "ond\\(?:ition-case\\)?\\)\\|do\\(?:list\\|times\\)?\\|"
2237 "eval-\\(?:a\\(?:fter-load\\|nd-compile\\)\\|when-compile\\)\\|flet\\|"
2238 "if\\|l\\(?:a\\(?:bels\\|mbda\\)\\|et\\*?\\|oop\\)\\|prog[12nv]?\\|"
2239 "return\\(?:-from\\)?\\|save-\\(?:current-buffer\\|excursion\\|"
2240 "match-data\\|restriction\\|selected-\\(?:frame\\|window\\)\\|"
2241 "window-excursion\\)\\|t\\(?:hrow\\|rack-mouse\\)\\|un\\(?:less\\|"
2242 "wind-protect\\)\\|w\\(?:h\\(?:en\\|ile\\)\\|ith-\\(?:current-buffer\\|"
2243 "output-to-string\\|s\\(?:elected-\\(?:frame\\|window\\)\\|"
2244 "tring-as-buffer-contents\\)\\|temp-\\(?:buffer\\|file\\)\\)\\)"
2245 ;; end of generated stuff
2248 ;; Feature symbols as references.
2249 '("(\\(featurep\\|provide\\|require\\)\\>[ \t']*\\(\\sw+\\)?"
2250 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2252 ;; Words inside \\[] tend to be for `substitute-command-keys'.
2253 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-reference-face prepend)
2255 ;; Words inside `' tend to be symbol names.
2256 '("`\\(\\sw\\sw+\\)'" 1 font-lock-reference-face prepend)
2258 ;; CLisp `:' keywords as references.
2259 '("\\<:\\sw+\\>" 0 font-lock-reference-face prepend)
2261 ;; ELisp and CLisp `&' keywords as types.
2262 '("\\<\\&\\(optional\\|rest\\|whole\\)\\>" . font-lock-type-face)
2264 "Gaudy level highlighting for Lisp modes.")
2266 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
2267 "Default expressions to highlight in Lisp modes.")
2269 ;; The previous version, before replacing it with the FSF version.
2270 ;(defconst lisp-font-lock-keywords-1 (purecopy
2272 ; ;; highlight defining forms. This doesn't work too nicely for
2273 ; ;; (defun (setf foo) ...) but it does work for (defvar foo) which
2274 ; ;; is more important.
2275 ; ("^(def[-a-z]+\\s +\\([^ \t\n\)]+\\)" 1 font-lock-function-name-face)
2277 ; ;; highlight CL keywords (three clauses seems faster than one)
2278 ; ("\\s :\\(\\(\\sw\\|\\s_\\)+\\)\\>" . 1)
2279 ; ("(:\\(\\(\\sw\\|\\s_\\)+\\)\\>" . 1)
2280 ; ("':\\(\\(\\sw\\|\\s_\\)+\\)\\>" . 1)
2282 ; ;; this is highlights things like (def* (setf foo) (bar baz)), but may
2283 ; ;; be slower (I haven't really thought about it)
2284 ;; ("^(def[-a-z]+\\s +\\(\\s(\\S)*\\s)\\|\\S(\\S *\\)"
2285 ;; 1 font-lock-function-name-face)
2287 ; "For consideration as a value of `lisp-font-lock-keywords'.
2288 ;This does fairly subdued highlighting.")
2290 ;(defconst lisp-font-lock-keywords-2 (purecopy
2291 ; (append lisp-font-lock-keywords-1
2293 ; ;; Highlight control structures
2294 ; ("(\\(cond\\|if\\|when\\|unless\\|[ec]?\\(type\\)?case\\)[ \t\n]" . 1)
2295 ; ("(\\(while\\|do\\|let\\*?\\|flet\\|labels\\|prog[nv12*]?\\)[ \t\n]" . 1)
2296 ; ("(\\(do\\*\\|dotimes\\|dolist\\|loop\\)[ \t\n]" . 1)
2297 ; ("(\\(catch\\|\\throw\\|block\\|return\\|return-from\\)[ \t\n]" . 1)
2298 ; ("(\\(save-restriction\\|save-window-restriction\\)[ \t\n]" . 1)
2299 ; ("(\\(save-excursion\\|unwind-protect\\|condition-case\\)[ \t\n]" . 1)
2301 ; ;; highlight function names in emacs-lisp docstrings (in the syntax
2302 ; ;; that substitute-command-keys understands.)
2303 ; ("\\\\\\\\\\[\\([^]\\\n]+\\)]" 1 font-lock-keyword-face t)
2305 ; ;; highlight words inside `' which tend to be function names
2306 ; ("`\\([-a-zA-Z0-9_][-a-zA-Z0-9_][-a-zA-Z0-9_.]+\\)'"
2307 ; 1 font-lock-keyword-face t)
2309 ; "For consideration as a value of `lisp-font-lock-keywords'.
2311 ;This does a lot more highlighting.")
2313 (defvar scheme-font-lock-keywords
2317 ;; Declarations. Hannes Haug <hannes.haug@student.uni-tuebingen.de> says
2318 ;; this works for SOS, STklos, SCOOPS, Meroon and Tiny CLOS.
2319 (list (concat "(\\(define\\("
2321 "\\(\\|-\\(generic\\(\\|-procedure\\)\\|method\\)\\)\\|"
2322 ;; Macro names, as variable names. A bit dubious, this.
2327 ;; Any whitespace and declared object.
2330 '(1 font-lock-keyword-face)
2331 '(8 (cond ((match-beginning 3) 'font-lock-function-name-face)
2332 ((match-beginning 6) 'font-lock-variable-name-face)
2333 (t 'font-lock-type-face))
2336 ;; Control structures.
2337 ;(regexp-opt '("begin" "call-with-current-continuation" "call/cc"
2338 ; "call-with-input-file" "call-with-output-file" "case" "cond"
2339 ; "do" "else" "for-each" "if" "lambda"
2340 ; "let\\*?" "let-syntax" "letrec" "letrec-syntax"
2341 ; ;; Hannes Haug <hannes.haug@student.uni-tuebingen.de> wants:
2342 ; "and" "or" "delay"
2343 ; ;; Stefan Monnier <stefan.monnier@epfl.ch> says don't bother:
2344 ; ;;"quasiquote" "quote" "unquote" "unquote-splicing"
2345 ; "map" "syntax" "syntax-rules"))
2348 "and\\|begin\\|c\\(a\\(ll\\(-with-\\(current-continuation\\|"
2349 "input-file\\|output-file\\)\\|/cc\\)\\|se\\)\\|ond\\)\\|"
2350 "d\\(elay\\|o\\)\\|else\\|for-each\\|if\\|"
2351 "l\\(ambda\\|et\\(-syntax\\|\\*?\\|rec\\(\\|-syntax\\)\\)\\)\\|"
2352 "map\\|or\\|syntax\\(\\|-rules\\)"
2355 ;; David Fox <fox@graphics.cs.nyu.edu> for SOS/STklos class specifiers.
2356 '("\\<<\\sw+>\\>" . font-lock-type-face)
2358 ;; Scheme `:' keywords as references.
2359 '("\\<:\\sw+\\>" . font-lock-reference-face)
2361 "Default expressions to highlight in Scheme modes.")
2363 ;; The previous version, before replacing it with the FSF version.
2364 ;(defconst scheme-font-lock-keywords (purecopy
2365 ; '(("(define[ \t]+(?\\([^ \t\n\)]+\\)" 1 font-lock-function-name-face)
2366 ; ("(\\(cond\\|lambda\\|begin\\|if\\|else\\|case\\|do\\)[ \t\n]" . 1)
2367 ; ("(\\(\\|letrec\\|let\\*?\\|set!\\|and\\|or\\)[ \t\n]" . 1)
2368 ; ("(\\(quote\\|unquote\\|quasiquote\\|unquote-splicing\\)[ \t\n]" . 1)
2369 ; ("(\\(syntax\\|syntax-rules\\|define-syntax\\|let-syntax\\|letrec-syntax\\)[ \t\n]" . 1)))
2370 ; "Expressions to highlight in Scheme buffers.")
2372 (defconst c-font-lock-keywords-1 nil
2373 "Subdued level highlighting for C modes.")
2375 (defconst c-font-lock-keywords-2 nil
2376 "Medium level highlighting for C modes.")
2378 (defconst c-font-lock-keywords-3 nil
2379 "Gaudy level highlighting for C modes.")
2381 (defconst c++-font-lock-keywords-1 nil
2382 "Subdued level highlighting for C++ modes.")
2384 (defconst c++-font-lock-keywords-2 nil
2385 "Medium level highlighting for C++ modes.")
2387 (defconst c++-font-lock-keywords-3 nil
2388 "Gaudy level highlighting for C++ modes.")
2390 (defun font-lock-match-c++-style-declaration-item-and-skip-to-next (limit)
2391 ;; Match, and move over, any declaration/definition item after point.
2392 ;; The expect syntax of an item is "word" or "word::word", possibly ending
2393 ;; with optional whitespace and a "(". Everything following the item (but
2394 ;; belonging to it) is expected to by skip-able by `forward-sexp', and items
2395 ;; are expected to be separated with a "," or ";".
2396 (if (looking-at "[ \t*&]*\\(\\(?:\\sw\\|\\s_\\)+\\)\\(::\\(\\(?:\\sw\\|\\s_\\)+\\)\\)?[ \t]*\\((\\)?")
2400 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
2401 (narrow-to-region (point-min) limit)
2402 (goto-char (match-end 1))
2403 ;; Move over any item value, etc., to the next item.
2404 (while (not (looking-at "[ \t]*\\([,;]\\|$\\)"))
2405 (goto-char (or (scan-sexps (point) 1) (point-max))))
2406 (goto-char (match-end 0)))
2410 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while")
2411 "break\\|continue\\|do\\|else\\|for\\|if\\|return\\|switch\\|while")
2413 ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
2414 ; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
2415 ; "void" "volatile" "const")
2416 (concat "auto\\|c\\(har\\|onst\\)\\|double\\|e\\(num\\|xtern\\)\\|"
2417 "float\\|int\\|long\\|register\\|"
2418 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|typedef\\|"
2419 "un\\(ion\\|signed\\)\\|vo\\(id\\|latile\\)")) ; 6 ()s deep.
2421 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while"
2422 ; "asm" "catch" "delete" "new" "operator" "sizeof" "this" "throw" "try"
2423 ; "protected" "private" "public" "const_cast" "dynamic_cast" "reinterpret_cast"
2424 ; "static_cast" "and" "bitor" "or" "xor" "compl" "bitand" "and_eq"
2425 ; "or_eq" "xor_eq" "not" "not_eq" "typeid" "false" "true")
2426 (concat "a\\(nd\\(\\|_eq\\)\\|sm\\)\\|"
2427 "b\\(it\\(or\\|and\\)\\|reak\\)\\|"
2428 "c\\(atch\\|o\\(mpl\\|n\\(tinue\\|st_cast\\)\\)\\)\\|"
2429 "d\\(elete\\|o\\|ynamic_cast\\)\\|"
2431 "f\\(alse\\|or\\)\\|if\\|"
2432 "n\\(ew\\|ot\\(\\|_eq\\)\\)\\|"
2433 "p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|"
2435 "re\\(interpret_cast\\|turn\\)\\|"
2436 "s\\(izeof\\|tatic_cast\\|witch\\)\\|"
2437 "t\\(h\\(is\\|row\\)\\|r\\(ue\\|y\\)\\|ypeid\\)\\|"
2438 "xor\\(\\|_eq\\)\\|while"))
2440 ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
2441 ; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
2442 ; "void" "volatile" "const" "class" "inline" "friend" "bool"
2443 ; "virtual" "complex" "template" "explicit" "mutable" "export" "namespace"
2444 ; "using" "typename" "wchar_t")
2445 (concat "auto\\|bool\\|c\\(har\\|lass\\|o\\(mplex\\|nst\\)\\)\\|"
2447 "e\\(num\\|x\\(p\\(licit\\|ort\\)\\|tern\\)\\)\\|"
2448 "f\\(loat\\|riend\\)\\|"
2449 "in\\(line\\|t\\)\\|long\\|mutable\\|namespace\\|register\\|"
2450 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|"
2451 "t\\(emplate\\|ype\\(def\\|name\\)\\)\\|"
2452 "u\\(\\(n\\(ion\\|signed\\)\\|sing\\)\\)\\|"
2453 "v\\(irtual\\|o\\(id\\|latile\\)\\)\\|"
2454 "wchar_t")) ; 11 ()s deep.
2455 (ctoken "\\(\\sw\\|\\s_\\|[:~*&]\\)+")
2457 (setq c-font-lock-keywords-1
2460 ;; These are all anchored at the beginning of line for speed.
2462 ;; Fontify function name definitions (GNU style; without type on line).
2464 ;; In FSF this has the simpler definition of "\\sw+" for ctoken.
2465 ;; I'm not sure if ours is more correct.
2466 ;; This is a subset of the next rule, and is slower when present. --dmoore
2467 ;; (list (concat "^\\(" ctoken "\\)[ \t]*(") 1 'font-lock-function-name-face)
2469 ;; fontify the names of functions being defined.
2470 ;; FSF doesn't have this but I think it should be fast for us because
2471 ;; our regexp routines are more intelligent than FSF's about handling
2472 ;; anchored-at-newline. (When I added this hack in regex.c, it halved
2473 ;; the time to do the regexp phase of font-lock for a C file!) Not
2474 ;; including this discriminates against those who don't follow the
2475 ;; GNU coding style. --ben
2476 ;; x?x?x?y?z should always be: (x(xx?)?)?y?z --dmoore
2479 "\\(" ctoken "[ \t]+\\)" ; type specs; there can be no
2481 "\\(" ctoken "[ \t]+\\)" ; more than 3 tokens, right?
2482 "\\(" ctoken "[ \t]+\\)"
2484 "\\([*&]+[ \t]*\\)?" ; pointer
2485 "\\(" ctoken "\\)[ \t]*(") ; name
2486 10 'font-lock-function-name-face)
2488 ;; This is faster but not by much. I don't see why not.
2489 ;(list (concat "^\\(" ctoken "\\)[ \t]*(") 1 'font-lock-function-name-face)
2491 ;; Added next two; they're both jolly-good fastmatch candidates so
2492 ;; should be fast. --ben
2494 ;; Fontify structure names (in structure definition form).
2495 (list (concat "^\\(typedef[ \t]+struct\\|struct\\|static[ \t]+struct\\)"
2496 "[ \t]+\\(" ctoken "\\)[ \t]*\\(\{\\|$\\)")
2497 2 'font-lock-function-name-face)
2499 ;; Fontify case clauses. This is fast because its anchored on the left.
2500 '("case[ \t]+\\(\\(\\sw\\|\\s_\\)+\\)[ \t]+:". 1)
2502 '("\\<\\(default\\):". 1)
2503 ;; Fontify filenames in #include <...> preprocessor directives as strings.
2504 '("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face)
2506 ;; Fontify function macro names.
2507 '("^#[ \t]*define[ \t]+\\(\\(\\sw+\\)(\\)" 2 font-lock-function-name-face)
2509 ;; Fontify symbol names in #if ... defined preprocessor directives.
2511 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
2512 (1 font-lock-preprocessor-face) (2 font-lock-variable-name-face nil t)))
2514 ;; Fontify symbol names in #elif ... defined preprocessor directives.
2516 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
2517 (1 font-lock-preprocessor-face) (2 font-lock-variable-name-face nil t)))
2519 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
2520 '("^\\(#[ \t]*[a-z]+\\)\\>[ \t]*\\(\\sw+\\)?"
2521 (1 font-lock-preprocessor-face) (2 font-lock-variable-name-face nil t))
2524 (setq c-font-lock-keywords-2
2525 (append c-font-lock-keywords-1
2528 ;; Simple regexps for speed.
2530 ;; Fontify all type specifiers.
2531 (cons (concat "\\<\\(" c-type-types "\\)\\>") 'font-lock-type-face)
2533 ;; Fontify all builtin keywords (except case, default and goto; see below).
2534 (cons (concat "\\<\\(" c-keywords "\\)\\>") 'font-lock-keyword-face)
2536 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2537 '("\\<\\(case\\|goto\\)\\>[ \t]*\\([^ \t\n:;]+\\)?"
2538 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2539 '("^[ \t]*\\(\\sw+\\)[ \t]*:" 1 font-lock-reference-face)
2542 (setq c-font-lock-keywords-3
2543 (append c-font-lock-keywords-2
2545 ;; More complicated regexps for more complete highlighting for types.
2546 ;; We still have to fontify type specifiers individually, as C is so hairy.
2549 ;; Fontify all storage classes and type specifiers, plus their items.
2550 (list (concat "\\<\\(" c-type-types "\\)\\>"
2551 "\\([ \t*&]+\\sw+\\>\\)*")
2552 ;; Fontify each declaration item.
2553 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
2554 ;; Start with point after all type specifiers.
2555 (goto-char (or (match-beginning 8) (match-end 1)))
2556 ;; Finish with point after first type specifier.
2557 (goto-char (match-end 1))
2558 ;; Fontify as a variable or function name.
2559 (1 (if (match-beginning 4)
2560 font-lock-function-name-face
2561 font-lock-variable-name-face))))
2563 ;; Fontify structures, or typedef names, plus their items.
2564 '("\\(}\\)[ \t*]*\\sw"
2565 (font-lock-match-c++-style-declaration-item-and-skip-to-next
2566 (goto-char (match-end 1)) nil
2567 (1 (if (match-beginning 4)
2568 font-lock-function-name-face
2569 font-lock-variable-name-face))))
2571 ;; Fontify anything at beginning of line as a declaration or definition.
2572 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2573 (1 font-lock-type-face)
2574 (font-lock-match-c++-style-declaration-item-and-skip-to-next
2575 (goto-char (or (match-beginning 2) (match-end 1))) nil
2576 (1 (if (match-beginning 4)
2577 font-lock-function-name-face
2578 font-lock-variable-name-face))))
2581 (setq c++-font-lock-keywords-1
2584 ;; The list `c-font-lock-keywords-1' less that for function names.
2585 ;; the simple function form regexp has been removed. --dmoore
2586 ;;(cdr c-font-lock-keywords-1)
2587 c-font-lock-keywords-1
2589 ;; Fontify function name definitions, possibly incorporating class name.
2591 '("^\\(\\sw+\\)\\(::\\(\\sw+\\)\\)?[ \t]*("
2592 (1 (if (match-beginning 2)
2594 font-lock-function-name-face))
2595 (3 (if (match-beginning 2) font-lock-function-name-face) nil t))
2598 (setq c++-font-lock-keywords-2
2599 (append c++-font-lock-keywords-1
2602 ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading.
2603 (cons (concat "\\<\\(" c++-type-types "\\)\\>") 'font-lock-type-face)
2605 ;; Fontify operator function name overloading.
2606 '("\\<\\(operator\\)\\>[ \t]*\\([][)(><!=+-][][)(><!=+-]?\\)?"
2607 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
2609 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2610 '("\\<\\(case\\|goto\\)\\>[ \t]*\\([^ \t\n:;]+\\)?"
2611 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2612 '("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-reference-face)
2614 ;; Fontify other builtin keywords.
2615 (cons (concat "\\<\\(" c++-keywords "\\)\\>") 'font-lock-keyword-face)
2618 (setq c++-font-lock-keywords-3
2619 (append c++-font-lock-keywords-2
2621 ;; More complicated regexps for more complete highlighting for types.
2624 ;; Fontify all storage classes and type specifiers, plus their items.
2625 (list (concat "\\<\\(" c++-type-types "\\)\\>"
2626 "\\([ \t*&]+\\sw+\\>\\)*")
2627 ;; Fontify each declaration item.
2628 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
2629 ;; Start with point after all type specifiers.
2630 (goto-char (or (match-beginning 13) (match-end 1)))
2631 ;; Finish with point after first type specifier.
2632 (goto-char (match-end 1))
2633 ;; Fontify as a variable or function name.
2634 (1 (cond ((match-beginning 2) 'font-lock-type-face)
2635 ((match-beginning 4) 'font-lock-function-name-face)
2636 (t 'font-lock-variable-name-face)))
2637 (3 (if (match-beginning 4)
2638 'font-lock-function-name-face
2639 'font-lock-variable-name-face) nil t)))
2641 ;; Fontify structures, or typedef names, plus their items.
2642 '("\\(}\\)[ \t*]*\\sw"
2643 (font-lock-match-c++-style-declaration-item-and-skip-to-next
2644 (goto-char (match-end 1)) nil
2645 (1 (if (match-beginning 4)
2646 font-lock-function-name-face
2647 font-lock-variable-name-face))))
2649 ;; Fontify anything at beginning of line as a declaration or definition.
2650 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2651 (1 font-lock-type-face)
2652 (font-lock-match-c++-style-declaration-item-and-skip-to-next
2653 (goto-char (or (match-beginning 2) (match-end 1))) nil
2654 (1 (cond ((match-beginning 2) 'font-lock-type-face)
2655 ((match-beginning 4) 'font-lock-function-name-face)
2656 (t 'font-lock-variable-name-face)))
2657 (3 (if (match-beginning 4)
2658 'font-lock-function-name-face
2659 'font-lock-variable-name-face) nil t)))
2663 (defvar c-font-lock-keywords c-font-lock-keywords-1
2664 "Default expressions to highlight in C mode.")
2666 (defvar c++-font-lock-keywords c++-font-lock-keywords-1
2667 "Default expressions to highlight in C++ mode.")
2671 ;; Java support has been written by XEmacs people, and it's apparently
2672 ;; totally divergent from the FSF. I don't know if it's better or
2673 ;; worse, so I'm leaving it in until someone convinces me the FSF
2674 ;; version is better. --hniksic
2676 (defconst java-font-lock-keywords-1 nil
2677 "For consideration as a value of `java-font-lock-keywords'.
2678 This does fairly subdued highlighting.")
2680 (defconst java-font-lock-keywords-2 nil
2681 "For consideration as a value of `java-font-lock-keywords'.
2682 This adds highlighting of types and identifier names.")
2684 (defconst java-font-lock-keywords-3 nil
2685 "For consideration as a value of `java-font-lock-keywords'.
2686 This adds highlighting of Java documentation tags, such as @see.")
2688 (defvar java-font-lock-type-regexp
2689 (concat "\\<\\(boolean\\|byte\\|char\\|double\\|float\\|int"
2690 "\\|long\\|short\\|void\\)\\>")
2691 "Regexp which should match a primitive type.")
2693 (defvar java-font-lock-identifier-regexp
2694 (let ((letter "a-zA-Z_$\300-\326\330-\366\370-\377")
2696 (concat "\\<\\([" letter "][" letter digit "]*\\)\\>"))
2697 "Regexp which should match all Java identifiers.")
2699 (defvar java-font-lock-class-name-regexp
2700 (let ((capital-letter "A-Z\300-\326\330-\337")
2701 (letter "a-zA-Z_$\300-\326\330-\366\370-\377")
2703 (concat "\\<\\([" capital-letter "][" letter digit "]*\\)\\>"))
2704 "Regexp which should match a class or an interface name.
2705 The name is assumed to begin with a capital letter.")
2707 (let ((java-modifier-regexp
2708 (concat "\\<\\(abstract\\|const\\|final\\|native\\|"
2709 "private\\|protected\\|public\\|"
2710 "static\\|synchronized\\|transient\\|volatile\\)\\>")))
2712 ;; Basic font-lock support:
2713 (setq java-font-lock-keywords-1
2720 "break\\|byvalue\\|"
2721 "case\\|cast\\|catch\\|class\\|continue\\|"
2722 "do\\|else\\|enum\\|extends\\|"
2723 "finally\\|for\\|future\\|"
2725 "if\\|implements\\|import\\|"
2726 "instanceof\\|interface\\|"
2727 "new\\|package\\|return\\|switch\\|"
2728 "throws?\\|try\\|while\\)\\>")
2729 1 'font-lock-keyword-face)
2732 (list java-modifier-regexp 1 font-lock-type-face)
2734 ;; Special constants:
2735 '("\\<\\(this\\|super\\)\\>" (1 font-lock-reference-face))
2736 '("\\<\\(false\\|null\\|true\\)\\>" (1 font-lock-keyword-face))
2739 (list (concat "\\<\\(class\\|interface\\)\\>\\s *"
2740 java-font-lock-identifier-regexp)
2741 2 'font-lock-function-name-face)
2743 ;; Package declarations:
2744 (list (concat "\\<\\(package\\|import\\)\\>\\s *"
2745 java-font-lock-identifier-regexp)
2746 '(2 font-lock-reference-face)
2748 "\\=\\.\\(" java-font-lock-identifier-regexp "\\)")
2749 nil nil '(1 (if (equal (char-after (match-end 0)) ?.)
2750 'font-lock-reference-face
2751 'font-lock-type-face))))
2755 "^\\s *\\(" java-modifier-regexp "\\s +\\)*"
2756 java-font-lock-class-name-regexp "\\s *\(")
2758 '(condition-case nil
2760 (goto-char (scan-sexps (- (match-end 0) 1) 1))
2761 (parse-partial-sexp (point) (point-max) nil t)
2762 (and (looking-at "\\($\\|\\<throws\\>\\|{\\)")
2763 'font-lock-function-name-face))
2764 (error 'font-lock-function-name-face))))
2767 (list (concat "\\(" java-font-lock-type-regexp "\\|"
2768 java-font-lock-class-name-regexp "\\)"
2769 "\\s *\\(\\[\\s *\\]\\s *\\)*"
2770 java-font-lock-identifier-regexp "\\s *\(")
2772 'font-lock-function-name-face)
2777 (concat "^\\s *" java-font-lock-identifier-regexp "\\s *:")
2778 '(beginning-of-line) '(end-of-line)
2779 '(1 font-lock-reference-face)))
2781 ;; `break' and continue' destination labels:
2782 (list (concat "\\<\\(break\\|continue\\)\\>\\s *"
2783 java-font-lock-identifier-regexp)
2784 2 'font-lock-reference-face)
2787 ;; In Java, any constant expression is allowed.
2788 '("\\<case\\>\\s *\\(.*\\):" 1 font-lock-reference-face)))
2790 ;; Types and declared variable names:
2791 (setq java-font-lock-keywords-2
2794 java-font-lock-keywords-1
2796 ;; Keywords followed by a type:
2797 (list (concat "\\<\\(extends\\|instanceof\\|new\\)\\>\\s *"
2798 java-font-lock-identifier-regexp)
2799 '(2 (if (equal (char-after (match-end 0)) ?.)
2800 'font-lock-reference-face 'font-lock-type-face))
2801 (list (concat "\\=\\." java-font-lock-identifier-regexp)
2802 '(goto-char (match-end 0)) nil
2803 '(1 (if (equal (char-after (match-end 0)) ?.)
2804 'font-lock-reference-face 'font-lock-type-face))))
2806 ;; Keywords followed by a type list:
2807 (list (concat "\\<\\(implements\\|throws\\)\\>\\ s*"
2808 java-font-lock-identifier-regexp)
2809 '(2 (if (equal (char-after (match-end 0)) ?.)
2810 font-lock-reference-face font-lock-type-face))
2811 (list (concat "\\=\\(\\.\\|\\s *\\(,\\)\\s *\\)"
2812 java-font-lock-identifier-regexp)
2813 '(goto-char (match-end 0)) nil
2814 '(3 (if (equal (char-after (match-end 0)) ?.)
2815 font-lock-reference-face font-lock-type-face))))
2817 ;; primitive types, can't be confused with anything else.
2818 (list java-font-lock-type-regexp
2819 '(1 font-lock-type-face)
2820 '(font-lock-match-java-declarations
2821 (goto-char (match-end 0))
2822 (goto-char (match-end 0))
2823 (0 font-lock-variable-name-face)))
2825 ;; Declarations, class types and capitalized variables:
2827 ;; Declarations are easy to recognize. Capitalized words
2828 ;; followed by a closing parenthesis are treated as casts if they
2829 ;; also are followed by an expression. Expressions beginning with
2830 ;; a unary numerical operator, e.g. +, can't be cast to an object
2833 ;; The path of a fully qualified type, e.g. java.lang.Foo, is
2834 ;; fontified in the reference face.
2836 ;; An access to a static field, e.g. System.out.println, is
2837 ;; not fontified since it can't be distinguished from the
2838 ;; usage of a capitalized variable, e.g. Foo.out.println.
2840 (list (concat java-font-lock-class-name-regexp
2841 "\\s *\\(\\[\\s *\\]\\s *\\)*"
2842 "\\(\\<\\|$\\|)\\s *\\([\(\"]\\|\\<\\)\\)")
2843 '(1 (save-match-data
2846 (match-beginning 3))
2847 (if (not (looking-at "\\<instanceof\\>"))
2848 'font-lock-type-face))))
2849 (list (concat "\\=" java-font-lock-identifier-regexp "\\.")
2851 (goto-char (match-beginning 0))
2852 (while (or (= (preceding-char) ?.)
2853 (= (char-syntax (preceding-char)) ?w))
2855 '(goto-char (match-end 0))
2856 '(1 font-lock-reference-face)
2857 '(0 nil)) ; Workaround for bug in XEmacs.
2858 '(font-lock-match-java-declarations
2859 (goto-char (match-end 1))
2860 (goto-char (match-end 0))
2861 (1 font-lock-variable-name-face))))))
2863 ;; Modifier keywords and Java doc tags
2864 (setq java-font-lock-keywords-3
2869 ;; These must come first or the Modifiers from keywords-1 will
2870 ;; catch them. We don't want to use override fontification here
2871 ;; because then these terms will be fontified within comments.
2872 ("\\<private\\>" 0 font-lock-string-face)
2873 ("\\<protected\\>" 0 font-lock-preprocessor-face)
2874 ("\\<public\\>" 0 font-lock-reference-face))
2875 java-font-lock-keywords-2
2880 '("@\\(author\\|deprecated\\|exception\\|throws\\|param\\|return\\|see\\|since\\|version\\|serial\\|serialData\\|serialField\\)\\s "
2881 0 font-lock-keyword-face t)
2883 ;; Doc tag - Parameter identifiers
2884 (list (concat "@param\\s +" java-font-lock-identifier-regexp)
2885 1 'font-lock-variable-name-face t)
2887 ;; Doc tag - Exception types
2888 (list (concat "@\\(exception\\|throws\\)\\s +"
2889 java-font-lock-identifier-regexp)
2890 '(2 (if (equal (char-after (match-end 0)) ?.)
2891 font-lock-reference-face font-lock-type-face) t)
2892 (list (concat "\\=\\." java-font-lock-identifier-regexp)
2893 '(goto-char (match-end 0)) nil
2894 '(1 (if (equal (char-after (match-end 0)) ?.)
2895 'font-lock-reference-face 'font-lock-type-face) t)))
2897 ;; Doc tag - Cross-references, usually to methods
2898 '("@see\\s +\\(\\S *[^][ \t\n\r\f(){},.;:]\\)"
2899 1 font-lock-function-name-face t)
2901 ;; Doc tag - docRoot (1.3)
2902 '("\\({ *@docRoot *}\\)"
2903 0 font-lock-keyword-face t)
2904 ;; Doc tag - beaninfo, unofficial but widely used, even by Sun
2906 0 font-lock-keyword-face t)
2908 '("{ *@link\\(?:plain\\)?\\s +\\([^}]+\\)}"
2909 0 font-lock-keyword-face t)
2911 '("{ *@link\\(?:plain\\)?\\s +\\(\\(\\S +\\)\\|\\(\\S +\\s +\\S +\\)\\) *}"
2912 1 font-lock-function-name-face t)
2917 (defvar java-font-lock-keywords java-font-lock-keywords-1
2918 "Additional expressions to highlight in Java mode.")
2920 ;; Match and move over any declaration/definition item after
2921 ;; point. Does not match items which look like a type declaration
2922 ;; (primitive types and class names, i.e. capitalized words.)
2923 ;; Should the variable name be followed by a comma, we reposition
2924 ;; the cursor to fontify more identifiers.
2925 (defun font-lock-match-java-declarations (limit)
2926 "Match and skip over variable definitions."
2928 (narrow-to-region (point-min) limit)
2930 (if (looking-at "\\s *\\(\\[\\s *\\]\\s *\\)*")
2931 (goto-char (match-end 0)))
2933 (looking-at java-font-lock-identifier-regexp)
2935 (not (string-match java-font-lock-type-regexp
2936 (buffer-substring (match-beginning 1)
2940 (goto-char (match-beginning 1))
2942 (concat java-font-lock-class-name-regexp
2943 "\\s *\\(\\[\\s *\\]\\s *\\)*\\<")))))
2947 (goto-char (match-end 0))
2948 ;; Note: Both `scan-sexps' and the second goto-char can
2949 ;; generate an error which is caught by the
2950 ;; `condition-case' expression.
2951 (while (not (looking-at "\\s *\\(\\(,\\)\\|;\\|$\\)"))
2952 (goto-char (or (scan-sexps (point) 1) (point-max))))
2953 (goto-char (match-end 2))) ; non-nil
2957 (defvar tex-font-lock-keywords
2958 ; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
2959 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
2960 ; 2 font-lock-function-name-face)
2961 ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
2962 ; 2 font-lock-reference-face)
2963 ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
2964 ; ;; not be able to display those fonts.
2965 ; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep)
2966 ; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep)
2967 ; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face)
2968 ; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep))
2969 ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>.
2970 '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
2971 2 font-lock-function-name-face)
2972 ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
2973 2 font-lock-reference-face)
2974 ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face)
2975 "\\\\\\([a-zA-Z@]+\\|.\\)"
2976 ;; It seems a bit dubious to use `bold' and `italic' faces since we might
2977 ;; not be able to display those fonts.
2978 ;; LaTeX2e: \emph{This is emphasized}.
2979 ("\\\\emph{\\([^}]+\\)}" 1 'italic keep)
2980 ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...}
2981 ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}"
2982 3 (if (match-beginning 2) 'bold 'italic) keep)
2983 ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for good tables.
2984 ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)"
2985 3 (if (match-beginning 2) 'bold 'italic) keep))
2986 "Default expressions to highlight in TeX modes.")
2988 (defconst ksh-font-lock-keywords
2990 '("\\(^\\|[^\$\\\]\\)#.*" . font-lock-comment-face)
2991 '("\\<\\(if\\|then\\|else\\|elif\\|fi\\|case\\|esac\\|for\\|do\\|done\\|foreach\\|in\\|end\\|select\\|while\\|repeat\\|time\\|function\\|until\\|exec\\|command\\|coproc\\|noglob\\|nohup\\|nocorrect\\|source\\|autoload\\|alias\\|unalias\\|export\\|set\\|echo\\|eval\\|cd\\|log\\|compctl\\)\\>" . font-lock-keyword-face)
2992 '("\\<\\[\\[.*\\]\\]\\>" . font-lock-type-face)
2993 '("\$\(.*\)" . font-lock-type-face)
2995 "Additional expressions to highlight in ksh-mode.")
2997 (defconst sh-font-lock-keywords
2999 '("\\(^\\|[^\$\\\]\\)#.*" . font-lock-comment-face)
3000 '("\\<\\(if\\|then\\|else\\|elif\\|fi\\|case\\|esac\\|for\\|do\\|done\\|in\\|while\\|exec\\|export\\|set\\|echo\\|eval\\|cd\\)\\>" . font-lock-keyword-face)
3001 '("\\[.*\\]" . font-lock-type-face)
3002 '("`.*`" . font-lock-type-face)
3004 "Additional expressions to highlight in sh-mode.")
3007 ;; Install ourselves:
3009 (add-hook 'find-file-hooks 'font-lock-set-defaults t)
3012 (add-minor-mode 'font-lock-mode " Font")
3014 ;; Provide ourselves:
3016 (provide 'font-lock)
3018 ;;; font-lock.el ends here