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