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 '(choice (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 '(choice (const :tag "default" nil)
218 (const :tag "maximum" t)
219 (integer :tag "level" 1)
220 (repeat :menu-tag "mode specific" :tag "mode specific"
222 (cons :tag "Instance"
225 (symbol :tag "name"))
226 (radio :tag "Decoration"
227 (const :tag "default" nil)
228 (const :tag "maximum" t)
229 (integer :tag "level" 1)))))
232 (defcustom font-lock-maximum-size (* 250 1024)
233 "*Maximum size of a buffer for buffer fontification.
234 Only buffers less than this can be fontified when Font Lock mode is turned on.
235 If nil, means size is irrelevant.
236 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
237 where MAJOR-MODE is a symbol or t (meaning the default). For example:
238 ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576))
239 means that the maximum size is 250K for buffers in C or C++ modes, one megabyte
240 for buffers in Rmail mode, and size is irrelevant otherwise."
241 :type '(choice (const :tag "none" nil)
242 (integer :tag "size")
243 (repeat :menu-tag "mode specific" :tag "mode specific"
245 (cons :tag "Instance"
248 (symbol :tag "name"))
250 (const :tag "none" nil)
251 (integer :tag "size")))))
254 ;; Fontification variables:
256 (defvar font-lock-keywords nil
257 "*A list of the keywords to highlight.
258 Each element should be of the form:
263 (MATCHER . HIGHLIGHT)
264 (MATCHER HIGHLIGHT ...)
267 where HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
269 FORM is an expression, whose value should be a keyword element, evaluated when
270 the keyword is (first) used in a buffer. This feature can be used to provide a
271 keyword that can only be generated when Font Lock mode is actually turned on.
273 For highlighting single items, typically only MATCH-HIGHLIGHT is required.
274 However, if an item or (typically) items are to be highlighted following the
275 instance of another item (the anchor) then MATCH-ANCHORED may be required.
277 MATCH-HIGHLIGHT should be of the form:
279 (MATCH FACENAME OVERRIDE LAXMATCH)
281 Where MATCHER can be either the regexp to search for, or the function name to
282 call to make the search (called with one argument, the limit of the search).
283 MATCH is the subexpression of MATCHER to be highlighted. MATCH can be
284 calculated via the function `font-lock-keyword-depth'. FACENAME is an
285 expression whose value is the face name to use. FACENAME's default attributes
286 can be defined via the variable `font-lock-face-attributes'.
288 OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification can
289 be overwritten. If `keep', only parts not already fontified are highlighted.
290 If `prepend' or `append', existing fontification is merged with the new, in
291 which the new or existing fontification, respectively, takes precedence.
292 If LAXMATCH is non-nil, no error is signaled if there is no MATCH in MATCHER.
294 For example, an element of the form highlights (if not already highlighted):
296 \"\\\\\\=<foo\\\\\\=>\" Discrete occurrences of \"foo\" in the value of the
297 variable `font-lock-keyword-face'.
298 (\"fu\\\\(bar\\\\)\" . 1) Substring \"bar\" within all occurrences of \"fubar\" in
299 the value of `font-lock-keyword-face'.
300 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'.
301 (\"foo\\\\|bar\" 0 foo-bar-face t)
302 Occurrences of either \"foo\" or \"bar\" in the value
303 of `foo-bar-face', even if already highlighted.
305 MATCH-ANCHORED should be of the form:
307 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
309 Where MATCHER is as for MATCH-HIGHLIGHT with one exception; see below.
310 PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
311 the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be
312 used to initialise before, and cleanup after, MATCHER is used. Typically,
313 PRE-MATCH-FORM is used to move to some position relative to the original
314 MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might
315 be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER.
317 For example, an element of the form highlights (if not already highlighted):
319 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
321 Discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
322 discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
323 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is
324 initially searched for starting from the end of the match of \"anchor\", and
325 searching for subsequent instance of \"anchor\" resumes from where searching
326 for \"item\" concluded.)
328 The above-mentioned exception is as follows. The limit of the MATCHER search
329 defaults to the end of the line after PRE-MATCH-FORM is evaluated.
330 However, if PRE-MATCH-FORM returns a position greater than the position after
331 PRE-MATCH-FORM is evaluated, that position is used as the limit of the search.
332 It is generally a bad idea to return a position greater than the end of the
333 line, i.e., cause the MATCHER search to span lines.
335 Note that the MATCH-ANCHORED feature is experimental; in the future, we may
336 replace it with other ways of providing this functionality.
338 These regular expressions should not match text which spans lines. While
339 \\[font-lock-fontify-buffer] handles multi-line patterns correctly, updating
340 when you edit the buffer does not, since it considers text one line at a time.
342 This variable is set by major modes via the variable `font-lock-defaults'.
343 Be careful when composing regexps for this list; a poorly written pattern can
344 dramatically slow things down!")
346 ;; This variable is used by mode packages that support Font Lock mode by
347 ;; defining their own keywords to use for `font-lock-keywords'. (The mode
348 ;; command should make it buffer-local and set it to provide the set up.)
349 (defvar font-lock-defaults nil
350 "Defaults for Font Lock mode specified by the major mode.
351 Defaults should be of the form:
353 (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN ...)
355 KEYWORDS may be a symbol (a variable or function whose value is the keywords to
356 use for fontification) or a list of symbols. If KEYWORDS-ONLY is non-nil,
357 syntactic fontification (strings and comments) is not performed.
358 If CASE-FOLD is non-nil, the case of the keywords is ignored when fontifying.
359 If SYNTAX-ALIST is non-nil, it should be a list of cons pairs of the form
360 \(CHAR-OR-STRING . STRING) used to set the local Font Lock syntax table, for
361 keyword and syntactic fontification (see `modify-syntax-entry').
363 If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
364 backwards outside any enclosing syntactic block, for syntactic fontification.
365 Typical values are `beginning-of-line' (i.e., the start of the line is known to
366 be outside a syntactic block), or `beginning-of-defun' for programming modes or
367 `backward-paragraph' for textual modes (i.e., the mode-dependent function is
368 known to move outside a syntactic block). If nil, the beginning of the buffer
369 is used as a position outside of a syntactic block, in the worst case.
371 These item elements are used by Font Lock mode to set the variables
372 `font-lock-keywords', `font-lock-keywords-only',
373 `font-lock-keywords-case-fold-search', `font-lock-syntax-table' and
374 `font-lock-beginning-of-syntax-function', respectively.
376 Further item elements are alists of the form (VARIABLE . VALUE) and are in no
377 particular order. Each VARIABLE is made buffer-local before set to VALUE.
379 Currently, appropriate variables include `font-lock-mark-block-function'.
380 If this is non-nil, it should be a function with no args used to mark any
381 enclosing block of text, for fontification via \\[font-lock-fontify-block].
382 Typical values are `mark-defun' for programming modes or `mark-paragraph' for
383 textual modes (i.e., the mode-dependent function is known to put point and mark
384 around a text block relevant to that mode).
386 Other variables include those for buffer-specialised fontification functions,
387 `font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function',
388 `font-lock-fontify-region-function', `font-lock-unfontify-region-function',
389 `font-lock-inhibit-thing-lock' and `font-lock-maximum-size'.")
391 ;; This variable is used where font-lock.el itself supplies the keywords.
392 (defvar font-lock-defaults-alist
393 (let (;; We use `beginning-of-defun', rather than nil, for SYNTAX-BEGIN.
394 ;; Thus the calculation of the cache is usually faster but not
395 ;; infallible, so we risk mis-fontification. sm.
397 '((c-font-lock-keywords c-font-lock-keywords-1
398 c-font-lock-keywords-2 c-font-lock-keywords-3)
399 nil nil ((?_ . "w")) beginning-of-defun
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 '((c++-font-lock-keywords c++-font-lock-keywords-1
405 c++-font-lock-keywords-2 c++-font-lock-keywords-3)
406 nil nil ((?_ . "w")) beginning-of-defun
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 '((objc-font-lock-keywords objc-font-lock-keywords-1
412 objc-font-lock-keywords-2 objc-font-lock-keywords-3)
413 nil nil ((?_ . "w") (?$ . "w")) nil
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)))
418 '((java-font-lock-keywords java-font-lock-keywords-1
419 java-font-lock-keywords-2 java-font-lock-keywords-3)
420 nil nil ((?_ . "w") (?$ . "w") (?. . "w")) nil
421 ;; Obsoleted by Emacs 19.35 parse-partial-sexp's COMMENTSTOP.
422 ;(font-lock-comment-start-regexp . "/[*/]")
423 (font-lock-mark-block-function . mark-defun)))
425 '((lisp-font-lock-keywords
426 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
427 nil nil (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
428 ;; Obsoleted by Emacs 19.35 parse-partial-sexp's COMMENTSTOP.
429 ;(font-lock-comment-start-regexp . ";")
430 (font-lock-mark-block-function . mark-defun)))
431 (scheme-mode-defaults
432 '(scheme-font-lock-keywords
433 nil t (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
434 ;; Obsoleted by Emacs 19.35 parse-partial-sexp's COMMENTSTOP.
435 ;(font-lock-comment-start-regexp . ";")
436 (font-lock-mark-block-function . mark-defun)))
437 ;; For TeX modes we could use `backward-paragraph' for the same reason.
438 ;; But we don't, because paragraph breaks are arguably likely enough to
439 ;; occur within a genuine syntactic block to make it too risky.
440 ;; However, we do specify a MARK-BLOCK function as that cannot result
441 ;; in a mis-fontification even if it might not fontify enough. --sm.
443 '(tex-font-lock-keywords nil nil ((?$ . "\"")) nil
444 ;; Obsoleted by Emacs 19.35 parse-partial-sexp's COMMENTSTOP.
445 ;(font-lock-comment-start-regexp . "%")
446 (font-lock-mark-block-function . mark-paragraph)))
449 (cons 'c-mode c-mode-defaults)
450 (cons 'c++-mode c++-mode-defaults)
451 (cons 'objc-mode objc-mode-defaults)
452 (cons 'java-mode java-mode-defaults)
453 (cons 'emacs-lisp-mode lisp-mode-defaults)
454 (cons 'inferior-scheme-mode scheme-mode-defaults)
455 (cons 'latex-mode tex-mode-defaults)
456 (cons 'lisp-mode lisp-mode-defaults)
457 (cons 'lisp-interaction-mode lisp-mode-defaults)
458 (cons 'plain-tex-mode tex-mode-defaults)
459 (cons 'scheme-mode scheme-mode-defaults)
460 (cons 'scheme-interaction-mode scheme-mode-defaults)
461 (cons 'slitex-mode tex-mode-defaults)
462 (cons 'tex-mode tex-mode-defaults)))
463 "Alist of fall-back Font Lock defaults for major modes.
464 Each item should be a list of the form:
466 (MAJOR-MODE . FONT-LOCK-DEFAULTS)
468 where MAJOR-MODE is a symbol and FONT-LOCK-DEFAULTS is a list of default
469 settings. See the variable `font-lock-defaults', which takes precedence.")
471 (defvar font-lock-keywords-alist nil
472 "*Alist of `font-lock-keywords' local to a `major-mode'.
473 This is normally set via `font-lock-add-keywords'.")
475 (defvar font-lock-keywords-only nil
476 "*Non-nil means Font Lock should not fontify comments or strings.
477 This is normally set via `font-lock-defaults'.")
479 (defvar font-lock-keywords-case-fold-search nil
480 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
481 This is normally set via `font-lock-defaults'.")
483 (defvar font-lock-syntax-table nil
484 "Non-nil means use this syntax table for fontifying.
485 If this is nil, the major mode's syntax table is used.
486 This is normally set via `font-lock-defaults'.")
488 ;; If this is nil, we only use the beginning of the buffer if we can't use
489 ;; `font-lock-cache-position' and `font-lock-cache-state'.
490 (defvar font-lock-beginning-of-syntax-function nil
491 "*Non-nil means use this function to move back outside of a syntactic block.
492 When called with no args it should leave point at the beginning of any
493 enclosing syntactic block.
494 If this is nil, the beginning of the buffer is used (in the worst case).
495 This is normally set via `font-lock-defaults'.")
497 (defvar font-lock-mark-block-function nil
498 "*Non-nil means use this function to mark a block of text.
499 When called with no args it should leave point at the beginning of any
500 enclosing textual block and mark at the end.
501 This is normally set via `font-lock-defaults'.")
503 ;; Obsoleted by Emacs 19.35 parse-partial-sexp's COMMENTSTOP.
504 ;(defvar font-lock-comment-start-regexp nil
505 ; "*Regexp to match the start of a comment.
506 ;This need not discriminate between genuine comments and quoted comment
507 ;characters or comment characters within strings.
508 ;If nil, `comment-start-skip' is used instead; see that variable for more info.
509 ;This is normally set via `font-lock-defaults'.")
511 (defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
512 "Function to use for fontifying the buffer.
513 This is normally set via `font-lock-defaults'.")
515 (defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
516 "Function to use for unfontifying the buffer.
517 This is used when turning off Font Lock mode.
518 This is normally set via `font-lock-defaults'.")
520 (defvar font-lock-fontify-region-function 'font-lock-default-fontify-region
521 "Function to use for fontifying a region.
522 It should take two args, the beginning and end of the region, and an optional
523 third arg VERBOSE. If non-nil, the function should print status messages.
524 This is normally set via `font-lock-defaults'.")
526 (defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
527 "Function to use for unfontifying a region.
528 It should take two args, the beginning and end of the region.
529 This is normally set via `font-lock-defaults'.")
531 (defvar font-lock-inhibit-thing-lock nil
532 "List of Font Lock mode related modes that should not be turned on.
533 Currently, valid mode names as `fast-lock-mode' and `lazy-lock-mode'.
534 This is normally set via `font-lock-defaults'.")
536 (defvar font-lock-mode nil) ; Whether we are turned on/modeline.
537 (defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
540 (defvar font-lock-mode-hook nil
541 "Function or functions to run on entry to Font Lock mode.")
547 ;; We don't do this at the top-level as we only use non-autoloaded macros.
550 ;; Borrowed from lazy-lock.el.
551 ;; We use this to preserve or protect things when modifying text properties.
552 (defmacro save-buffer-state (varlist &rest body)
553 "Bind variables according to VARLIST and eval BODY restoring buffer state."
554 (` (let* ((,@ (append varlist
555 '((modified (buffer-modified-p)) (buffer-undo-list t)
556 (inhibit-read-only t) (inhibit-point-motion-hooks t)
557 before-change-functions after-change-functions
558 deactivate-mark buffer-file-name buffer-file-truename))))
560 (when (and (not modified) (buffer-modified-p))
561 (set-buffer-modified-p nil)))))
562 (put 'save-buffer-state 'lisp-indent-function 1))
565 (defun font-lock-mode (&optional arg)
566 "Toggle Font Lock mode.
567 With arg, turn Font Lock mode on if and only if arg is positive.
569 When Font Lock mode is enabled, text is fontified as you type it:
571 - Comments are displayed in `font-lock-comment-face';
572 - Strings are displayed in `font-lock-string-face';
573 - Certain other expressions are displayed in other faces according to the
574 value of the variable `font-lock-keywords'.
576 You can enable Font Lock mode in any major mode automatically by turning on in
577 the major mode's hook. For example, put in your ~/.emacs:
579 (add-hook 'c-mode-hook 'turn-on-font-lock)
581 Alternatively, you can use Global Font Lock mode to automagically turn on Font
582 Lock mode in buffers whose major mode supports it and whose major mode is one
583 of `font-lock-global-modes'. For example, put in your ~/.emacs:
585 (global-font-lock-mode t)
587 There are a number of support modes that may be used to speed up Font Lock mode
588 in various ways, specified via the variable `font-lock-support-mode'. Where
589 major modes support different levels of fontification, you can use the variable
590 `font-lock-maximum-decoration' to specify which level you generally prefer.
591 When you turn Font Lock mode on/off the buffer is fontified/defontified, though
592 fontification occurs only if the buffer is less than `font-lock-maximum-size'.
594 For example, to specify that Font Lock mode use use Lazy Lock mode as a support
595 mode and use maximum levels of fontification, put in your ~/.emacs:
597 (setq font-lock-support-mode 'lazy-lock-mode)
598 (setq font-lock-maximum-decoration t)
600 To add your own highlighting for some major mode, and modify the highlighting
601 selected automatically via the variable `font-lock-maximum-decoration', you can
602 use `font-lock-add-keywords'.
604 To fontify a buffer, without turning on Font Lock mode and regardless of buffer
605 size, you can use \\[font-lock-fontify-buffer].
607 To fontify a block (the function or paragraph containing point, or a number of
608 lines around point), perhaps because modification on the current line caused
609 syntactic change on other lines, you can use \\[font-lock-fontify-block].
611 The default Font Lock mode faces and their attributes are defined in the
612 variable `font-lock-face-attributes', and Font Lock mode default settings in
613 the variable `font-lock-defaults-alist'. You can set your own default settings
614 for some mode, by setting a buffer local value for `font-lock-defaults', via
617 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
618 ;; batch job) or if the buffer is invisible (the name starts with a space).
619 (let ((on-p (and (not noninteractive)
620 (not (eq (aref (buffer-name) 0) ?\ ))
622 (> (prefix-numeric-value arg) 0)
623 (not font-lock-mode)))))
624 (set (make-local-variable 'font-lock-mode) on-p)
625 ;; Turn on Font Lock mode.
627 (make-local-hook 'after-change-functions)
628 (add-hook 'after-change-functions 'font-lock-after-change-function nil t)
629 (font-lock-set-defaults)
630 (font-lock-turn-on-thing-lock)
631 (run-hooks 'font-lock-mode-hook)
632 ;; Fontify the buffer if we have to.
633 (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size)))
634 (cond (font-lock-fontified
636 ((or (null max-size) (> max-size (buffer-size)))
637 (font-lock-fontify-buffer))
639 (message "Fontifying %s...buffer too big" (buffer-name))))))
640 ;; Turn off Font Lock mode.
642 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
643 (font-lock-unfontify-buffer)
644 (font-lock-turn-off-thing-lock)
645 (font-lock-unset-defaults))
646 (force-mode-line-update)))
649 (defun turn-on-font-lock ()
650 "Turn on Font Lock mode conditionally.
651 Turn on only if the terminal can display it."
652 (when (and (not font-lock-mode) window-system)
656 (defun font-lock-add-keywords (major-mode keywords &optional append)
657 "Add highlighting KEYWORDS for MAJOR-MODE.
658 MAJOR-MODE should be a symbol, the major mode command name, such as `c-mode'
659 or nil. If nil, highlighting keywords are added for the current buffer.
660 KEYWORDS should be a list; see the variable `font-lock-keywords'.
661 By default they are added at the beginning of the current highlighting list.
662 If optional argument APPEND is `set', they are used to replace the current
663 highlighting list. If APPEND is any other non-nil value, they are added at the
664 end of the current highlighting list.
668 (font-lock-add-keywords 'c-mode
669 '((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 font-lock-warning-face prepend)
670 (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . font-lock-keyword-face)))
672 adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
673 comments, and to fontify `and', `or' and `not' words as keywords.
675 Note that some modes have specialised support for additional patterns, e.g.,
676 see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
677 `objc-font-lock-extra-types' and `java-font-lock-extra-types'."
679 ;; If MAJOR-MODE is non-nil, add the KEYWORDS and APPEND spec to
680 ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
681 (let ((spec (cons keywords append)) cell)
682 (if (setq cell (assq major-mode font-lock-keywords-alist))
683 (setcdr cell (append (cdr cell) (list spec)))
684 (push (list major-mode spec) font-lock-keywords-alist))))
686 ;; Otherwise if Font Lock mode is on, set or add the keywords now.
688 (setq font-lock-keywords keywords)
689 (let ((old (if (eq (car-safe font-lock-keywords) t)
690 (cdr font-lock-keywords)
691 font-lock-keywords)))
692 (setq font-lock-keywords (if append
693 (append old keywords)
694 (append keywords old))))))))
696 ;;; Global Font Lock mode.
698 ;; A few people have hassled in the past for a way to make it easier to turn on
699 ;; Font Lock mode, without the user needing to know for which modes s/he has to
700 ;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always
701 ;; balked at that way, as I see it as just re-moulding the same problem in
702 ;; another form. That is; some person would still have to keep track of which
703 ;; modes (which may not even be distributed with Emacs) support Font Lock mode.
704 ;; The list would always be out of date. And that person might have to be me.
708 ;; In a previous discussion the following hack came to mind. It is a gross
709 ;; hack, but it generally works. We use the convention that major modes start
710 ;; by calling the function `kill-all-local-variables', which in turn runs
711 ;; functions on the hook variable `change-major-mode-hook'. We attach our
712 ;; function `font-lock-change-major-mode' to that hook. Of course, when this
713 ;; hook is run, the major mode is in the process of being changed and we do not
714 ;; know what the final major mode will be. So, `font-lock-change-major-mode'
715 ;; only (a) notes the name of the current buffer, and (b) adds our function
716 ;; `turn-on-font-lock-if-enabled' to the hook variables `find-file-hooks' and
717 ;; `post-command-hook' (for buffers that are not visiting files). By the time
718 ;; the functions on the first of these hooks to be run are run, the new major
719 ;; mode is assumed to be in place. This way we get a Font Lock function run
720 ;; when a major mode is turned on, without knowing major modes or their hooks.
722 ;; Naturally this requires that (a) major modes run `kill-all-local-variables',
723 ;; as they are supposed to do, and (b) the major mode is in place after the
724 ;; file is visited or the command that ran `kill-all-local-variables' has
725 ;; finished, whichever the sooner. Arguably, any major mode that does not
726 ;; follow the convension (a) is broken, and I can't think of any reason why (b)
727 ;; would not be met (except `gnudoit' on non-files). However, it is not clean.
729 ;; Probably the cleanest solution is to have each major mode function run some
730 ;; hook, e.g., `major-mode-hook', but maybe implementing that change is
731 ;; impractical. I am personally against making `setq' a macro or be advised,
732 ;; or have a special function such as `set-major-mode', but maybe someone can
733 ;; come up with another solution?
737 ;; Although Global Font Lock mode is a pseudo-mode, I think that the user
738 ;; interface should conform to the usual Emacs convention for modes, i.e., a
739 ;; command to toggle the feature (`global-font-lock-mode') with a variable for
740 ;; finer control of the mode's behaviour (`font-lock-global-modes').
742 ;; The feature should not be enabled by loading font-lock.el, since other
743 ;; mechanisms for turning on Font Lock mode, such as M-x font-lock-mode RET or
744 ;; (add-hook 'c-mode-hook 'turn-on-font-lock), would cause Font Lock mode to be
745 ;; turned on everywhere. That would not be intuitive or informative because
746 ;; loading a file tells you nothing about the feature or how to control it. It
747 ;; would also be contrary to the Principle of Least Surprise. sm.
749 (defvar font-lock-buffers nil) ; For remembering buffers.
750 (defvar global-font-lock-mode nil)
752 (defcustom font-lock-global-modes t
753 "*Modes for which Font Lock mode is automagically turned on.
754 Global Font Lock mode is controlled by the `global-font-lock-mode' command.
755 If nil, means no modes have Font Lock mode automatically turned on.
756 If t, all modes that support Font Lock mode have it automatically turned on.
757 If a list, it should be a list of `major-mode' symbol names for which Font Lock
758 mode should be automatically turned on. The sense of the list is negated if it
759 begins with `not'. For example:
761 means that Font Lock mode is turned on for buffers in C and C++ modes only."
762 :type '(choice (const :tag "none" nil)
764 (set :menu-tag "mode specific" :tag "modes"
766 (const :tag "Except" not)
767 (repeat :inline t (symbol :tag "mode"))))
771 (defun global-font-lock-mode (&optional arg message)
772 "Toggle Global Font Lock mode.
773 With prefix ARG, turn Global Font Lock mode on if and only if ARG is positive.
774 Displays a message saying whether the mode is on or off if MESSAGE is non-nil.
775 Returns the new status of Global Font Lock mode (non-nil means on).
777 When Global Font Lock mode is enabled, Font Lock mode is automagically
778 turned on in a buffer if its major mode is one of `font-lock-global-modes'."
781 (<= (prefix-numeric-value arg) 0)
782 global-font-lock-mode)))
784 (remove-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
785 (add-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
786 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
787 (setq font-lock-buffers (buffer-list)))
789 (message "Global Font Lock mode is now %s." (if off-p "OFF" "ON")))
790 (setq global-font-lock-mode (not off-p))))
792 (defun font-lock-change-major-mode ()
793 ;; Turn off Font Lock mode if it's on.
796 ;; Gross hack warning: Delicate readers should avert eyes now.
797 ;; Something is running `kill-all-local-variables', which generally means the
798 ;; major mode is being changed. Run `turn-on-font-lock-if-enabled' after the
799 ;; file is visited or the current command has finished.
800 (when global-font-lock-mode
801 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
802 (add-to-list 'font-lock-buffers (current-buffer))))
804 (defun turn-on-font-lock-if-enabled ()
805 ;; Gross hack warning: Delicate readers should avert eyes now.
806 ;; Turn on Font Lock mode if it's supported by the major mode and enabled by
808 (remove-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
809 (while font-lock-buffers
810 (when (buffer-live-p (car font-lock-buffers))
812 (set-buffer (car font-lock-buffers))
813 (when (and (or font-lock-defaults
814 (assq major-mode font-lock-defaults-alist))
815 (or (eq font-lock-global-modes t)
816 (if (eq (car-safe font-lock-global-modes) 'not)
817 (not (memq major-mode (cdr font-lock-global-modes)))
818 (memq major-mode font-lock-global-modes))))
820 (turn-on-font-lock)))))
821 (setq font-lock-buffers (cdr font-lock-buffers))))
823 (add-hook 'change-major-mode-hook 'font-lock-change-major-mode)
825 ;;; End of Global Font Lock mode.
827 ;;; Font Lock Support mode.
829 ;; This is the code used to interface font-lock.el with any of its add-on
830 ;; packages, and provide the user interface. Packages that have their own
831 ;; local buffer fontification functions (see below) may have to call
832 ;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
835 (defcustom font-lock-support-mode nil
836 "*Support mode for Font Lock mode.
837 Support modes speed up Font Lock mode by being choosy about when fontification
838 occurs. Known support modes are Fast Lock mode (symbol `fast-lock-mode') and
839 Lazy Lock mode (symbol `lazy-lock-mode'). See those modes for more info.
840 If nil, means support for Font Lock mode is never performed.
841 If a symbol, use that support mode.
842 If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
843 where MAJOR-MODE is a symbol or t (meaning the default). For example:
844 ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
845 means that Fast Lock mode is used to support Font Lock mode for buffers in C or
846 C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
848 The value of this variable is used when Font Lock mode is turned on."
849 :type '(choice (const :tag "none" nil)
850 (const :tag "fast lock" fast-lock-mode)
851 (const :tag "lazy lock" lazy-lock-mode)
852 (repeat :menu-tag "mode specific" :tag "mode specific"
853 :value ((t . lazy-lock-mode))
854 (cons :tag "Instance"
857 (symbol :tag "name"))
858 (radio :tag "Decoration"
859 (const :tag "fast lock" fast-lock-mode)
860 (const :tag "lazy lock" lazy-lock-mode)))
864 (defvar fast-lock-mode nil)
865 (defvar lazy-lock-mode nil)
867 (defun font-lock-turn-on-thing-lock ()
868 (let ((thing-mode (font-lock-value-in-major-mode font-lock-support-mode)))
869 (cond ((eq thing-mode 'fast-lock-mode)
871 ((eq thing-mode 'lazy-lock-mode)
872 (lazy-lock-mode t)))))
874 (defun font-lock-turn-off-thing-lock ()
875 (cond (fast-lock-mode
876 (fast-lock-mode nil))
878 (lazy-lock-mode nil))))
880 (defun font-lock-after-fontify-buffer ()
881 (cond (fast-lock-mode
882 (fast-lock-after-fontify-buffer))
884 (lazy-lock-after-fontify-buffer))))
886 (defun font-lock-after-unfontify-buffer ()
887 (cond (fast-lock-mode
888 (fast-lock-after-unfontify-buffer))
890 (lazy-lock-after-unfontify-buffer))))
892 ;;; End of Font Lock Support mode.
894 ;;; Fontification functions.
896 ;; Rather than the function, e.g., `font-lock-fontify-region' containing the
897 ;; code to fontify a region, the function runs the function whose name is the
898 ;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally,
899 ;; the value of this variable is, e.g., `font-lock-default-fontify-region'
900 ;; which does contain the code to fontify a region. However, the value of the
901 ;; variable could be anything and thus, e.g., `font-lock-fontify-region' could
902 ;; do anything. The indirection of the fontification functions gives major
903 ;; modes the capability of modifying the way font-lock.el fontifies. Major
904 ;; modes can modify the values of, e.g., `font-lock-fontify-region-function',
905 ;; via the variable `font-lock-defaults'.
907 ;; For example, Rmail mode sets the variable `font-lock-defaults' so that
908 ;; font-lock.el uses its own function for buffer fontification. This function
909 ;; makes fontification be on a message-by-message basis and so visiting an
910 ;; RMAIL file is much faster. A clever implementation of the function might
911 ;; fontify the headers differently than the message body. (It should, and
912 ;; correspondingly for Mail mode, but I can't be bothered to do the work. Can
913 ;; you?) This hints at a more interesting use...
915 ;; Languages that contain text normally contained in different major modes
916 ;; could define their own fontification functions that treat text differently
917 ;; depending on its context. For example, Perl mode could arrange that here
918 ;; docs are fontified differently than Perl code. Or Yacc mode could fontify
919 ;; rules one way and C code another. Neat!
921 ;; A further reason to use the fontification indirection feature is when the
922 ;; default syntactual fontification, or the default fontification in general,
923 ;; is not flexible enough for a particular major mode. For example, perhaps
924 ;; comments are just too hairy for `font-lock-fontify-syntactically-region' to
925 ;; cope with. You need to write your own version of that function, e.g.,
926 ;; `hairy-fontify-syntactically-region', and make your own version of
927 ;; `hairy-fontify-region' call that function before calling
928 ;; `font-lock-fontify-keywords-region' for the normal regexp fontification
929 ;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el
930 ;; would call your region fontification function instead of its own. For
931 ;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line
932 ;; directives correctly and cleanly. (It is the same problem as fontifying
933 ;; multi-line strings and comments; regexps are not appropriate for the job.)
936 (defun font-lock-fontify-buffer ()
937 "Fontify the current buffer the way `font-lock-mode' would."
939 (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
940 (funcall font-lock-fontify-buffer-function)))
942 (defun font-lock-unfontify-buffer ()
943 (funcall font-lock-unfontify-buffer-function))
945 (defun font-lock-fontify-region (beg end &optional loudly)
946 (funcall font-lock-fontify-region-function beg end loudly))
948 (defun font-lock-unfontify-region (beg end)
949 (funcall font-lock-unfontify-region-function beg end))
951 (defun font-lock-default-fontify-buffer ()
952 (let ((verbose (if (numberp font-lock-verbose)
953 (> (buffer-size) font-lock-verbose)
956 (message "Fontifying %s..." (buffer-name)))
957 ;; Make sure we have the right `font-lock-keywords' etc.
958 (unless font-lock-mode
959 (font-lock-set-defaults))
960 ;; Make sure we fontify etc. in the whole buffer.
966 (font-lock-fontify-region (point-min) (point-max) verbose)
967 (font-lock-after-fontify-buffer)
968 (setq font-lock-fontified t)))
969 ;; We don't restore the old fontification, so it's best to unfontify.
970 (quit (font-lock-unfontify-buffer))))
971 ;; Make sure we undo `font-lock-keywords' etc.
972 (unless font-lock-mode
973 (font-lock-unset-defaults))
974 (if verbose (message "Fontifying %s...%s" (buffer-name)
975 (if font-lock-fontified "done" "quit")))))
977 (defun font-lock-default-unfontify-buffer ()
978 ;; Make sure we unfontify etc. in the whole buffer.
981 (font-lock-unfontify-region (point-min) (point-max))
982 (font-lock-after-unfontify-buffer)
983 (setq font-lock-fontified nil)))
985 (defun font-lock-default-fontify-region (beg end loudly)
986 (save-buffer-state ((old-syntax-table (syntax-table)))
990 ;; Use the fontification syntax table, if any.
991 (when font-lock-syntax-table
992 (set-syntax-table font-lock-syntax-table))
993 ;; Now do the fontification.
994 (font-lock-unfontify-region beg end)
995 (unless font-lock-keywords-only
996 (font-lock-fontify-syntactically-region beg end loudly))
997 (font-lock-fontify-keywords-region beg end loudly))
999 (set-syntax-table old-syntax-table))))
1001 ;; The following must be rethought, since keywords can override fontification.
1002 ; ;; Now scan for keywords, but not if we are inside a comment now.
1003 ; (or (and (not font-lock-keywords-only)
1004 ; (let ((state (parse-partial-sexp beg end nil nil
1005 ; font-lock-cache-state)))
1006 ; (or (nth 4 state) (nth 7 state))))
1007 ; (font-lock-fontify-keywords-region beg end))
1009 (defun font-lock-default-unfontify-region (beg end)
1010 (save-buffer-state nil
1011 (remove-text-properties beg end '(face nil))))
1013 ;; Called when any modification is made to buffer text.
1014 (defun font-lock-after-change-function (beg end old-len)
1017 ;; Rescan between start of lines enclosing the region.
1018 (font-lock-fontify-region
1019 (progn (goto-char beg) (beginning-of-line) (point))
1020 (progn (goto-char end) (forward-line 1) (point))))))
1022 (defun font-lock-fontify-block (&optional arg)
1023 "Fontify some lines the way `font-lock-fontify-buffer' would.
1024 The lines could be a function or paragraph, or a specified number of lines.
1025 If ARG is given, fontify that many lines before and after point, or 16 lines if
1026 no ARG is given and `font-lock-mark-block-function' is nil.
1027 If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
1028 delimit the region to fontify."
1030 (let (font-lock-beginning-of-syntax-function deactivate-mark)
1031 ;; Make sure we have the right `font-lock-keywords' etc.
1032 (if (not font-lock-mode) (font-lock-set-defaults))
1035 (condition-case error-data
1036 (if (or arg (not font-lock-mark-block-function))
1037 (let ((lines (if arg (prefix-numeric-value arg) 16)))
1038 (font-lock-fontify-region
1039 (save-excursion (forward-line (- lines)) (point))
1040 (save-excursion (forward-line lines) (point))))
1041 (funcall font-lock-mark-block-function)
1042 (font-lock-fontify-region (point) (mark)))
1043 ((error quit) (message "Fontifying block...%s" error-data)))))))
1045 (define-key facemenu-keymap "\M-g" 'font-lock-fontify-block)
1047 ;;; End of Fontification functions.
1049 ;;; Syntactic fontification functions.
1051 ;; These record the parse state at a particular position, always the start of a
1052 ;; line. Used to make `font-lock-fontify-syntactically-region' faster.
1053 ;; Previously, `font-lock-cache-position' was just a buffer position. However,
1054 ;; under certain situations, this occasionally resulted in mis-fontification.
1055 ;; I think the "situations" were deletion with Lazy Lock mode's deferral. sm.
1056 (defvar font-lock-cache-state nil)
1057 (defvar font-lock-cache-position nil)
1059 (defun font-lock-fontify-syntactically-region (start end &optional loudly)
1060 "Put proper face on each string and comment between START and END.
1061 START should be at the beginning of a line."
1062 (let ((cache (marker-position font-lock-cache-position))
1064 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
1067 ;; Find the state at the `beginning-of-line' before `start'.
1068 (if (eq start cache)
1069 ;; Use the cache for the state of `start'.
1070 (setq state font-lock-cache-state)
1071 ;; Find the state of `start'.
1072 (if (null font-lock-beginning-of-syntax-function)
1073 ;; Use the state at the previous cache position, if any, or
1074 ;; otherwise calculate from `point-min'.
1075 (if (or (null cache) (< start cache))
1076 (setq state (parse-partial-sexp (point-min) start))
1077 (setq state (parse-partial-sexp cache start nil nil
1078 font-lock-cache-state)))
1079 ;; Call the function to move outside any syntactic block.
1080 (funcall font-lock-beginning-of-syntax-function)
1081 (setq state (parse-partial-sexp (point) start)))
1082 ;; Cache the state and position of `start'.
1083 (setq font-lock-cache-state state)
1084 (set-marker font-lock-cache-position start))
1086 ;; If the region starts inside a string or comment, show the extent of it.
1087 (when (or (nth 3 state) (nth 4 state))
1088 (setq string (nth 3 state) beg (point))
1089 (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
1090 (put-text-property beg (point) 'face
1092 font-lock-string-face
1093 font-lock-comment-face)))
1095 ;; Find each interesting place between here and `end'.
1096 (while (and (< (point) end)
1098 (setq state (parse-partial-sexp (point) end nil nil state
1100 (or (nth 3 state) (nth 4 state))))
1101 (setq string (nth 3 state) beg (nth 8 state))
1102 (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
1103 (put-text-property beg (point) 'face
1105 font-lock-string-face
1106 font-lock-comment-face)))))
1108 ;;; End of Syntactic fontification functions.
1110 ;;; Additional text property functions.
1112 ;; The following text property functions should be builtins. This means they
1113 ;; should be written in C and put with all the other text property functions.
1114 ;; In the meantime, those that are used by font-lock.el are defined in Lisp
1115 ;; below and given a `font-lock-' prefix. Those that are not used are defined
1116 ;; in Lisp below and commented out. sm.
1118 (defun font-lock-prepend-text-property (start end prop value &optional object)
1119 "Prepend to one property of the text from START to END.
1120 Arguments PROP and VALUE specify the property and value to prepend to the value
1121 already in place. The resulting property values are always lists.
1122 Optional argument OBJECT is the string or buffer containing the text."
1123 (let ((val (if (listp value) value (list value))) next prev)
1124 (while (/= start end)
1125 (setq next (next-single-property-change start prop object end)
1126 prev (get-text-property start prop object))
1127 (put-text-property start next prop
1128 (append val (if (listp prev) prev (list prev)))
1130 (setq start next))))
1132 (defun font-lock-append-text-property (start end prop value &optional object)
1133 "Append to one property of the text from START to END.
1134 Arguments PROP and VALUE specify the property and value to append to the value
1135 already in place. The resulting property values are always lists.
1136 Optional argument OBJECT is the string or buffer containing the text."
1137 (let ((val (if (listp value) value (list value))) next prev)
1138 (while (/= start end)
1139 (setq next (next-single-property-change start prop object end)
1140 prev (get-text-property start prop object))
1141 (put-text-property start next prop
1142 (append (if (listp prev) prev (list prev)) val)
1144 (setq start next))))
1146 (defun font-lock-fillin-text-property (start end prop value &optional object)
1147 "Fill in one property of the text from START to END.
1148 Arguments PROP and VALUE specify the property and value to put where none are
1149 already in place. Therefore existing property values are not overwritten.
1150 Optional argument OBJECT is the string or buffer containing the text."
1151 (let ((start (text-property-any start end prop nil object)) next)
1153 (setq next (next-single-property-change start prop object end))
1154 (put-text-property start next prop value object)
1155 (setq start (text-property-any next end prop nil object)))))
1157 ;; For completeness: this is to `remove-text-properties' as `put-text-property'
1158 ;; is to `add-text-properties', etc.
1159 ;(defun remove-text-property (start end property &optional object)
1160 ; "Remove a property from text from START to END.
1161 ;Argument PROPERTY is the property to remove.
1162 ;Optional argument OBJECT is the string or buffer containing the text.
1163 ;Return t if the property was actually removed, nil otherwise."
1164 ; (remove-text-properties start end (list property) object))
1166 ;; For consistency: maybe this should be called `remove-single-property' like
1167 ;; `next-single-property-change' (not `next-single-text-property-change'), etc.
1168 ;(defun remove-single-text-property (start end prop value &optional object)
1169 ; "Remove a specific property value from text from START to END.
1170 ;Arguments PROP and VALUE specify the property and value to remove. The
1171 ;resulting property values are not equal to VALUE nor lists containing VALUE.
1172 ;Optional argument OBJECT is the string or buffer containing the text."
1173 ; (let ((start (text-property-not-all start end prop nil object)) next prev)
1175 ; (setq next (next-single-property-change start prop object end)
1176 ; prev (get-text-property start prop object))
1177 ; (cond ((and (symbolp prev) (eq value prev))
1178 ; (remove-text-property start next prop object))
1179 ; ((and (listp prev) (memq value prev))
1180 ; (let ((new (delq value prev)))
1182 ; (remove-text-property start next prop object))
1183 ; ((= (length new) 1)
1184 ; (put-text-property start next prop (car new) object))
1186 ; (put-text-property start next prop new object))))))
1187 ; (setq start (text-property-not-all next end prop nil object)))))
1189 ;;; End of Additional text property functions.
1191 ;;; Regexp fontification functions.
1193 (defsubst font-lock-apply-highlight (highlight)
1194 "Apply HIGHLIGHT following a match.
1195 HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
1196 (let* ((match (nth 0 highlight))
1197 (start (match-beginning match)) (end (match-end match))
1198 (override (nth 2 highlight)))
1200 ;; No match but we might not signal an error.
1201 (or (nth 3 highlight)
1202 (error "No match %d in highlight %S" match highlight)))
1204 ;; Cannot override existing fontification.
1205 (or (text-property-not-all start end 'face nil)
1206 (put-text-property start end 'face (eval (nth 1 highlight)))))
1208 ;; Override existing fontification.
1209 (put-text-property start end 'face (eval (nth 1 highlight))))
1210 ((eq override 'prepend)
1211 ;; Prepend to existing fontification.
1212 (font-lock-prepend-text-property start end 'face (eval (nth 1 highlight))))
1213 ((eq override 'append)
1214 ;; Append to existing fontification.
1215 (font-lock-append-text-property start end 'face (eval (nth 1 highlight))))
1216 ((eq override 'keep)
1217 ;; Keep existing fontification.
1218 (font-lock-fillin-text-property start end 'face (eval (nth 1 highlight)))))))
1220 (defsubst font-lock-fontify-anchored-keywords (keywords limit)
1221 "Fontify according to KEYWORDS until LIMIT.
1222 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
1223 LIMIT can be modified by the value of its PRE-MATCH-FORM."
1224 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
1225 ;; Evaluate PRE-MATCH-FORM.
1226 (pre-match-value (eval (nth 1 keywords))))
1227 ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
1228 (if (and (numberp pre-match-value) (> pre-match-value (point)))
1229 (setq limit pre-match-value)
1230 (save-excursion (end-of-line) (setq limit (point))))
1232 ;; Find an occurrence of `matcher' before `limit'.
1233 (while (if (stringp matcher)
1234 (re-search-forward matcher limit t)
1235 (funcall matcher limit))
1236 ;; Apply each highlight to this instance of `matcher'.
1237 (setq highlights lowdarks)
1239 (font-lock-apply-highlight (car highlights))
1240 (setq highlights (cdr highlights)))))
1241 ;; Evaluate POST-MATCH-FORM.
1242 (eval (nth 2 keywords))))
1244 (defun font-lock-fontify-keywords-region (start end &optional loudly)
1245 "Fontify according to `font-lock-keywords' between START and END.
1246 START should be at the beginning of a line."
1247 (unless (eq (car-safe font-lock-keywords) t)
1248 (setq font-lock-keywords (font-lock-compile-keywords font-lock-keywords)))
1249 (let ((case-fold-search font-lock-keywords-case-fold-search)
1250 (keywords (cdr font-lock-keywords))
1251 (bufname (buffer-name)) (count 0)
1252 keyword matcher highlights)
1254 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1256 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
1257 (make-string (incf count) ?.)))
1259 ;; Find an occurrence of `matcher' from `start' to `end'.
1260 (setq keyword (car keywords) matcher (car keyword))
1262 (while (if (stringp matcher)
1263 (re-search-forward matcher end t)
1264 (funcall matcher end))
1265 ;; Apply each highlight to this instance of `matcher', which may be
1266 ;; specific highlights or more keywords anchored to `matcher'.
1267 (setq highlights (cdr keyword))
1269 (if (numberp (car (car highlights)))
1270 (font-lock-apply-highlight (car highlights))
1271 (font-lock-fontify-anchored-keywords (car highlights) end))
1272 (setq highlights (cdr highlights))))
1273 (setq keywords (cdr keywords)))))
1275 ;;; End of Regexp fontification functions.
1277 ;; Various functions.
1279 (defun font-lock-compile-keywords (keywords)
1280 ;; Compile KEYWORDS into the form (t KEYWORD ...) where KEYWORD is of the
1281 ;; form (MATCHER HIGHLIGHT ...) as shown in `font-lock-keywords' doc string.
1282 (if (eq (car-safe keywords) t)
1284 (cons t (mapcar 'font-lock-compile-keyword keywords))))
1286 (defun font-lock-compile-keyword (keyword)
1287 (cond ((nlistp keyword) ; MATCHER
1288 (list keyword '(0 font-lock-keyword-face)))
1289 ((eq (car keyword) 'eval) ; (eval . FORM)
1290 (font-lock-compile-keyword (eval (cdr keyword))))
1291 ((eq (car-safe (cdr keyword)) 'quote) ; (MATCHER . 'FORM)
1292 ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote.
1293 (if (symbolp (nth 2 keyword))
1294 (list (car keyword) (list 0 (cdr keyword)))
1295 (font-lock-compile-keyword (cons (car keyword) (nth 2 keyword)))))
1296 ((numberp (cdr keyword)) ; (MATCHER . MATCH)
1297 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
1298 ((symbolp (cdr keyword)) ; (MATCHER . FACENAME)
1299 (list (car keyword) (list 0 (cdr keyword))))
1300 ((nlistp (nth 1 keyword)) ; (MATCHER . HIGHLIGHT)
1301 (list (car keyword) (cdr keyword)))
1302 (t ; (MATCHER HIGHLIGHT ...)
1305 (defun font-lock-value-in-major-mode (alist)
1306 ;; Return value in ALIST for `major-mode', or ALIST if it is not an alist.
1307 ;; Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t.
1309 (cdr (or (assq major-mode alist) (assq t alist)))
1312 (defun font-lock-choose-keywords (keywords level)
1313 ;; Return LEVELth element of KEYWORDS. A LEVEL of nil is equal to a
1314 ;; LEVEL of 0, a LEVEL of t is equal to (1- (length KEYWORDS)).
1315 (cond ((symbolp keywords)
1318 (or (nth level keywords) (car (reverse keywords))))
1320 (car (reverse keywords)))
1324 (defvar font-lock-set-defaults nil) ; Whether we have set up defaults.
1326 (defun font-lock-set-defaults ()
1327 "Set fontification defaults appropriately for this mode.
1328 Sets various variables using `font-lock-defaults' (or, if nil, using
1329 `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
1330 ;; Set fontification defaults.
1331 (make-local-variable 'font-lock-fontified)
1332 ;; Set iff not previously set.
1333 (unless font-lock-set-defaults
1334 (set (make-local-variable 'font-lock-set-defaults) t)
1335 (set (make-local-variable 'font-lock-cache-state) nil)
1336 (set (make-local-variable 'font-lock-cache-position) (make-marker))
1337 (let* ((defaults (or font-lock-defaults
1338 (cdr (assq major-mode font-lock-defaults-alist))))
1340 (font-lock-choose-keywords (nth 0 defaults)
1341 (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1342 (local (cdr (assq major-mode font-lock-keywords-alist))))
1343 ;; Regexp fontification?
1344 (set (make-local-variable 'font-lock-keywords)
1345 (if (fboundp keywords) (funcall keywords) (eval keywords)))
1346 ;; Local fontification?
1348 (font-lock-add-keywords nil (car (car local)) (cdr (car local)))
1349 (setq local (cdr local)))
1350 ;; Syntactic fontification?
1351 (when (nth 1 defaults)
1352 (set (make-local-variable 'font-lock-keywords-only) t))
1353 ;; Case fold during regexp fontification?
1354 (when (nth 2 defaults)
1355 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
1356 ;; Syntax table for regexp and syntactic fontification?
1357 (when (nth 3 defaults)
1358 (let ((slist (nth 3 defaults)))
1359 (set (make-local-variable 'font-lock-syntax-table)
1360 (copy-syntax-table (syntax-table)))
1362 ;; The character to modify may be a single CHAR or a STRING.
1363 (let ((chars (if (numberp (car (car slist)))
1364 (list (car (car slist)))
1365 (mapcar 'identity (car (car slist)))))
1366 (syntax (cdr (car slist))))
1368 (modify-syntax-entry (car chars) syntax
1369 font-lock-syntax-table)
1370 (setq chars (cdr chars)))
1371 (setq slist (cdr slist))))))
1372 ;; Syntax function for syntactic fontification?
1373 (when (nth 4 defaults)
1374 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1377 (let ((alist (nthcdr 5 defaults)))
1379 (let ((variable (car (car alist))) (value (cdr (car alist))))
1380 (unless (boundp variable)
1381 (setq variable nil))
1382 (set (make-local-variable variable) value)
1383 (setq alist (cdr alist))))))))
1385 (defun font-lock-unset-defaults ()
1386 "Unset fontification defaults. See `font-lock-set-defaults'."
1387 (setq font-lock-set-defaults nil
1388 font-lock-keywords nil
1389 font-lock-keywords-only nil
1390 font-lock-keywords-case-fold-search nil
1391 font-lock-syntax-table nil
1392 font-lock-beginning-of-syntax-function nil)
1393 (let* ((defaults (or font-lock-defaults
1394 (cdr (assq major-mode font-lock-defaults-alist))))
1395 (alist (nthcdr 5 defaults)))
1397 (set (car (car alist)) (default-value (car (car alist))))
1398 (setq alist (cdr alist)))))
1400 ;;; Colour etc. support.
1402 ;; Originally these variable values were face names such as `bold' etc.
1403 ;; Now we create our own faces, but we keep these variables for compatibility
1404 ;; and they give users another mechanism for changing face appearance.
1405 ;; We now allow a FACENAME in `font-lock-keywords' to be any expression that
1406 ;; returns a face. So the easiest thing is to continue using these variables,
1407 ;; rather than sometimes evaling FACENAME and sometimes not. sm.
1408 (defvar font-lock-comment-face 'font-lock-comment-face
1409 "Face name to use for comments.")
1411 (defvar font-lock-string-face 'font-lock-string-face
1412 "Face name to use for strings.")
1414 (defvar font-lock-keyword-face 'font-lock-keyword-face
1415 "Face name to use for keywords.")
1417 (defvar font-lock-builtin-face 'font-lock-builtin-face
1418 "Face name to use for builtins.")
1420 (defvar font-lock-function-name-face 'font-lock-function-name-face
1421 "Face name to use for function names.")
1423 (defvar font-lock-variable-name-face 'font-lock-variable-name-face
1424 "Face name to use for variable names.")
1426 (defvar font-lock-type-face 'font-lock-type-face
1427 "Face name to use for type names.")
1429 (defvar font-lock-reference-face 'font-lock-reference-face
1430 "Face name to use for reference names.")
1432 (defvar font-lock-warning-face 'font-lock-warning-face
1433 "Face name to use for things that should stand out.")
1435 (defface font-lock-comment-face
1436 '((((class grayscale) (background light))
1437 (:foreground "DimGray" :bold t :italic t))
1438 (((class grayscale) (background dark))
1439 (:foreground "LightGray" :bold t :italic t))
1440 (((class color) (background light)) (:foreground "Firebrick"))
1441 (((class color) (background dark)) (:foreground "OrangeRed"))
1442 (t (:bold t :italic t)))
1443 "Font Lock mode face used to highlight comments."
1444 :group 'font-lock-faces)
1446 (defface font-lock-string-face
1447 '((((class grayscale) (background light)) (:foreground "DimGray" :italic t))
1448 (((class grayscale) (background dark)) (:foreground "LightGray" :italic t))
1449 (((class color) (background light)) (:foreground "RosyBrown"))
1450 (((class color) (background dark)) (:foreground "LightSalmon"))
1452 "Font Lock mode face used to highlight strings."
1453 :group 'font-lock-faces)
1455 (defface font-lock-keyword-face
1456 '((((class grayscale) (background light)) (:foreground "LightGray" :bold t))
1457 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1458 (((class color) (background light)) (:foreground "Purple"))
1459 (((class color) (background dark)) (:foreground "Cyan"))
1461 "Font Lock mode face used to highlight keywords."
1462 :group 'font-lock-faces)
1464 (defface font-lock-builtin-face
1465 '((((class grayscale) (background light)) (:foreground "LightGray" :bold t))
1466 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1467 (((class color) (background light)) (:foreground "Orchid"))
1468 (((class color) (background dark)) (:foreground "LightSteelBlue"))
1470 "Font Lock mode face used to highlight builtins."
1471 :group 'font-lock-faces)
1473 (defface font-lock-function-name-face
1474 ;; Currently, Emacs/Custom does not support a :reverse or :invert spec.
1475 '((((class color) (background light)) (:foreground "Blue"))
1476 (((class color) (background dark)) (:foreground "LightSkyBlue"))
1477 (t ;(:reverse t :bold t)
1478 (:italic t :bold t)))
1479 "Font Lock mode face used to highlight function names."
1480 :group 'font-lock-faces)
1482 (defface font-lock-variable-name-face
1483 '((((class grayscale) (background light))
1484 (:foreground "Gray90" :bold t :italic t))
1485 (((class grayscale) (background dark))
1486 (:foreground "DimGray" :bold t :italic t))
1487 (((class color) (background light)) (:foreground "DarkGoldenrod"))
1488 (((class color) (background dark)) (:foreground "LightGoldenrod"))
1489 (t (:bold t :italic t)))
1490 "Font Lock mode face used to highlight variable names."
1491 :group 'font-lock-faces)
1493 (defface font-lock-type-face
1494 '((((class grayscale) (background light)) (:foreground "Gray90" :bold t))
1495 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1496 (((class color) (background light)) (:foreground "DarkOliveGreen"))
1497 (((class color) (background dark)) (:foreground "PaleGreen"))
1498 (t (:bold t :underline t)))
1499 "Font Lock mode face used to highlight types."
1500 :group 'font-lock-faces)
1502 (defface font-lock-reference-face
1503 '((((class grayscale) (background light))
1504 (:foreground "LightGray" :bold t :underline t))
1505 (((class grayscale) (background dark))
1506 (:foreground "Gray50" :bold t :underline t))
1507 (((class color) (background light)) (:foreground "CadetBlue"))
1508 (((class color) (background dark)) (:foreground "Aquamarine"))
1509 (t (:bold t :underline t)))
1510 "Font Lock mode face used to highlight references."
1511 :group 'font-lock-faces)
1513 (defface font-lock-warning-face
1514 ;; Currently, Emacs/Custom does not support a :reverse or :invert spec.
1515 '((((class color) (background light)) (:foreground "Red" :bold t))
1516 (((class color) (background dark)) (:foreground "Pink" :bold t))
1517 (t ;(:reverse t :bold t)
1518 (:italic t :bold t)))
1519 "Font Lock mode face used to highlight warnings."
1520 :group 'font-lock-faces)
1522 ;;; End of Colour etc. support.
1526 ;; This section of code is commented out because Emacs does not have real menu
1527 ;; buttons. (We can mimic them by putting "( ) " or "(X) " at the beginning of
1528 ;; the menu entry text, but with Xt it looks both ugly and embarrassingly
1529 ;; amateur.) If/When Emacs gets real menus buttons, put in menu-bar.el after
1530 ;; the entry for "Text Properties" something like:
1532 ;; (define-key menu-bar-edit-menu [font-lock]
1533 ;; '("Syntax Highlighting" . font-lock-menu))
1535 ;; and remove a single ";" from the beginning of each line in the rest of this
1536 ;; section. Probably the mechanism for telling the menu code what are menu
1537 ;; buttons and when they are on or off needs tweaking. I have assumed that the
1538 ;; mechanism is via `menu-toggle' and `menu-selected' symbol properties. sm.
1542 ; ;; Make the Font Lock menu.
1543 ; (defvar font-lock-menu (make-sparse-keymap "Syntax Highlighting"))
1544 ; ;; Add the menu items in reverse order.
1545 ; (define-key font-lock-menu [fontify-less]
1546 ; '("Less In Current Buffer" . font-lock-fontify-less))
1547 ; (define-key font-lock-menu [fontify-more]
1548 ; '("More In Current Buffer" . font-lock-fontify-more))
1549 ; (define-key font-lock-menu [font-lock-sep]
1551 ; (define-key font-lock-menu [font-lock-mode]
1552 ; '("In Current Buffer" . font-lock-mode))
1553 ; (define-key font-lock-menu [global-font-lock-mode]
1554 ; '("In All Buffers" . global-font-lock-mode)))
1558 ; ;; We put the appropriate `menu-enable' etc. symbol property values on when
1559 ; ;; font-lock.el is loaded, so we don't need to autoload the three variables.
1560 ; (put 'global-font-lock-mode 'menu-toggle t)
1561 ; (put 'font-lock-mode 'menu-toggle t)
1562 ; (put 'font-lock-fontify-more 'menu-enable '(identity))
1563 ; (put 'font-lock-fontify-less 'menu-enable '(identity)))
1565 ;;; Put the appropriate symbol property values on now. See above.
1566 ;(put 'global-font-lock-mode 'menu-selected 'global-font-lock-mode))
1567 ;(put 'font-lock-mode 'menu-selected 'font-lock-mode)
1568 ;(put 'font-lock-fontify-more 'menu-enable '(nth 2 font-lock-fontify-level))
1569 ;(put 'font-lock-fontify-less 'menu-enable '(nth 1 font-lock-fontify-level))
1571 ;(defvar font-lock-fontify-level nil) ; For less/more fontification.
1573 ;(defun font-lock-fontify-level (level)
1574 ; (let ((font-lock-maximum-decoration level))
1575 ; (when font-lock-mode
1578 ; (when font-lock-verbose
1579 ; (message "Fontifying %s... level %d" (buffer-name) level))))
1581 ;(defun font-lock-fontify-less ()
1582 ; "Fontify the current buffer with less decoration.
1583 ;See `font-lock-maximum-decoration'."
1585 ; ;; Check in case we get called interactively.
1586 ; (if (nth 1 font-lock-fontify-level)
1587 ; (font-lock-fontify-level (1- (car font-lock-fontify-level)))
1588 ; (error "No less decoration")))
1590 ;(defun font-lock-fontify-more ()
1591 ; "Fontify the current buffer with more decoration.
1592 ;See `font-lock-maximum-decoration'."
1594 ; ;; Check in case we get called interactively.
1595 ; (if (nth 2 font-lock-fontify-level)
1596 ; (font-lock-fontify-level (1+ (car font-lock-fontify-level)))
1597 ; (error "No more decoration")))
1599 ;;; This should be called by `font-lock-set-defaults'.
1600 ;(defun font-lock-set-menu ()
1601 ; ;; Activate less/more fontification entries if there are multiple levels for
1602 ; ;; the current buffer. Sets `font-lock-fontify-level' to be of the form
1603 ; ;; (CURRENT-LEVEL IS-LOWER-LEVEL-P IS-HIGHER-LEVEL-P) for menu activation.
1604 ; (let ((keywords (or (nth 0 font-lock-defaults)
1605 ; (nth 1 (assq major-mode font-lock-defaults-alist))))
1606 ; (level (font-lock-value-in-major-mode font-lock-maximum-decoration)))
1607 ; (make-local-variable 'font-lock-fontify-level)
1608 ; (if (or (symbolp keywords) (= (length keywords) 1))
1609 ; (font-lock-unset-menu)
1610 ; (cond ((eq level t)
1611 ; (setq level (1- (length keywords))))
1612 ; ((or (null level) (zerop level))
1613 ; ;; The default level is usually, but not necessarily, level 1.
1614 ; (setq level (- (length keywords)
1615 ; (length (member (eval (car keywords))
1616 ; (mapcar 'eval (cdr keywords))))))))
1617 ; (setq font-lock-fontify-level (list level (> level 1)
1618 ; (< level (1- (length keywords))))))))
1620 ;;; This should be called by `font-lock-unset-defaults'.
1621 ;(defun font-lock-unset-menu ()
1622 ; ;; Deactivate less/more fontification entries.
1623 ; (setq font-lock-fontify-level nil))
1625 ;;; End of Menu support.
1627 ;;; Various regexp information shared by several modes.
1628 ;;; Information specific to a single mode should go in its load library.
1630 ;; Font Lock support for C, C++, Objective-C and Java modes will one day be in
1631 ;; some cc-font.el (and required by cc-mode.el). However, the below function
1632 ;; should stay in font-lock.el, since it is used by other libraries. sm.
1634 (defun font-lock-match-c-style-declaration-item-and-skip-to-next (limit)
1635 "Match, and move over, any declaration/definition item after point.
1636 Matches after point, but ignores leading whitespace and `*' characters.
1637 Does not move further than LIMIT.
1639 The expected syntax of a declaration/definition item is `word', possibly ending
1640 with optional whitespace and a `('. Everything following the item (but
1641 belonging to it) is expected to by skip-able by `scan-sexps', and items are
1642 expected to be separated with a `,' and to be terminated with a `;'.
1644 Thus the regexp matches after point: word (
1646 Where the match subexpressions are: 1 2
1648 The item is delimited by (match-beginning 1) and (match-end 1).
1649 If (match-beginning 2) is non-nil, the item is followed by a `('.
1651 This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
1652 (when (looking-at "[ \t*]*\\(\\sw+\\)[ \t]*\\((\\)?")
1656 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
1657 (narrow-to-region (point-min) limit)
1658 (goto-char (match-end 1))
1659 ;; Move over any item value, etc., to the next item.
1660 (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)"))
1661 (goto-char (or (scan-sexps (point) 1) (point-max))))
1662 (goto-char (match-end 2)))
1665 (defun font-lock-keyword-depth (keyword)
1666 "Return the depth of KEYWORD regexp.
1667 This means the number of parenthesized expressions."
1668 (let ((count 0) start)
1669 (while (string-match "\\\\(" keyword start)
1670 (setq count (1+ count) start (match-end 0)))
1674 (defconst lisp-font-lock-keywords-1
1679 (list (concat "(\\(def\\("
1680 ;; Function declarations.
1681 "\\(advice\\|alias\\|"
1682 "ine-\\(derived-mode\\|function\\|skeleton\\)\\|"
1683 "macro\\|subst\\|un\\)\\|"
1684 ;; Variable declarations.
1685 "\\(const\\|custom\\|face\\|var\\)\\|"
1686 ;; Structure declarations.
1687 "\\(class\\|group\\|struct\\|type\\)"
1689 ;; Any whitespace and defined object.
1692 '(1 font-lock-keyword-face)
1693 '(7 (cond ((match-beginning 3) font-lock-function-name-face)
1694 ((match-beginning 5) font-lock-variable-name-face)
1695 (t font-lock-type-face))
1698 ;; Emacs Lisp autoload cookies.
1699 '("^;;;\\(###\\)\\(autoload\\)\\>"
1700 (1 font-lock-reference-face prepend)
1701 (2 font-lock-warning-face prepend))
1703 "Subdued level highlighting for Lisp modes.")
1705 (defconst lisp-font-lock-keywords-2
1706 (append lisp-font-lock-keywords-1
1710 ;; Control structures. Emacs Lisp forms.
1711 (cons (concat "(\\("
1713 ; '("cond" "if" "while" "let\\*?" "prog[nv12*]?" "catch" "throw"
1714 ; "inline" "save-restriction" "save-excursion" "save-window-excursion"
1715 ; "save-selected-window" "save-match-data" "save-current-buffer"
1716 ; "unwind-protect" "condition-case" "track-mouse" "dont-compile"
1717 ; "eval-after-load" "eval-and-compile" "eval-when" "eval-when-compile"
1718 ; "with-output-to-temp-buffer" "with-timeout" "with-current-buffer"
1719 ; "with-temp-buffer" "with-temp-file"))
1720 "c\\(atch\\|ond\\(\\|ition-case\\)\\)\\|dont-compile\\|"
1721 "eval-\\(a\\(fter-load\\|nd-compile\\)\\|"
1722 "when\\(\\|-compile\\)\\)\\|"
1723 "i\\(f\\|nline\\)\\|let\\*?\\|prog[nv12*]?\\|"
1724 "save-\\(current-buffer\\|excursion\\|match-data\\|"
1725 "restriction\\|selected-window\\|window-excursion\\)\\|"
1726 "t\\(hrow\\|rack-mouse\\)\\|unwind-protect\\|"
1727 "w\\(hile\\|ith-\\(current-buffer\\|"
1728 "output-to-temp-buffer\\|"
1729 "t\\(emp-\\(buffer\\|file\\)\\|imeout\\)\\)\\)"
1733 ;; Control structures. Common Lisp forms.
1734 (cons (concat "(\\("
1736 ; '("when" "unless" "case" "ecase" "typecase" "etypecase"
1737 ; "loop" "do\\*?" "dotimes" "dolist"
1738 ; "proclaim" "declaim" "declare"
1739 ; "lexical-let\\*?" "flet" "labels" "return" "return-from"))
1740 "case\\|d\\(ecla\\(im\\|re\\)\\|o\\(\\*?\\|"
1741 "list\\|times\\)\\)\\|e\\(case\\|typecase\\)\\|flet\\|"
1742 "l\\(abels\\|exical-let\\*?\\|oop\\)\\|proclaim\\|"
1743 "return\\(\\|-from\\)\\|typecase\\|unless\\|when"
1747 ;; Feature symbols as references.
1748 '("(\\(featurep\\|provide\\|require\\)\\>[ \t']*\\(\\sw+\\)?"
1749 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1751 ;; Words inside \\[] tend to be for `substitute-command-keys'.
1752 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-reference-face prepend)
1754 ;; Words inside `' tend to be symbol names.
1755 '("`\\(\\sw\\sw+\\)'" 1 font-lock-reference-face prepend)
1757 ;; CLisp `:' keywords as builtins.
1758 '("\\<:\\sw\\sw+\\>" 0 font-lock-builtin-face)
1760 ;; ELisp and CLisp `&' keywords as types.
1761 '("\\<\\&\\sw+\\>" . font-lock-type-face)
1763 "Gaudy level highlighting for Lisp modes.")
1766 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
1767 "Default expressions to highlight in Lisp modes.")
1770 (defvar scheme-font-lock-keywords
1774 ;; Declarations. Hannes Haug <hannes.haug@student.uni-tuebingen.de> says
1775 ;; this works for SOS, STklos, SCOOPS, Meroon and Tiny CLOS.
1776 (list (concat "(\\(define\\("
1778 "\\(\\|-\\(generic\\(\\|-procedure\\)\\|method\\)\\)\\|"
1779 ;; Macro names, as variable names. A bit dubious, this.
1784 ;; Any whitespace and declared object.
1787 '(1 font-lock-keyword-face)
1788 '(7 (cond ((match-beginning 3) font-lock-function-name-face)
1789 ((match-beginning 6) font-lock-variable-name-face)
1790 (t font-lock-type-face))
1793 ;; Control structures.
1794 ;(make-regexp '("begin" "call-with-current-continuation" "call/cc"
1795 ; "call-with-input-file" "call-with-output-file" "case" "cond"
1796 ; "do" "else" "for-each" "if" "lambda"
1797 ; "let\\*?" "let-syntax" "letrec" "letrec-syntax"
1798 ; ;; Hannes Haug <hannes.haug@student.uni-tuebingen.de> wants:
1799 ; "and" "or" "delay"
1800 ; ;; Stefan Monnier <stefan.monnier@epfl.ch> says don't bother:
1801 ; ;;"quasiquote" "quote" "unquote" "unquote-splicing"
1802 ; "map" "syntax" "syntax-rules"))
1805 "and\\|begin\\|c\\(a\\(ll\\(-with-\\(current-continuation\\|"
1806 "input-file\\|output-file\\)\\|/cc\\)\\|se\\)\\|ond\\)\\|"
1807 "d\\(elay\\|o\\)\\|else\\|for-each\\|if\\|"
1808 "l\\(ambda\\|et\\(-syntax\\|\\*?\\|rec\\(\\|-syntax\\)\\)\\)\\|"
1809 "map\\|or\\|syntax\\(\\|-rules\\)"
1812 ;; David Fox <fox@graphics.cs.nyu.edu> for SOS/STklos class specifiers.
1813 '("\\<<\\sw+>\\>" . font-lock-type-face)
1815 ;; Scheme `:' keywords as references.
1816 '("\\<:\\sw+\\>" . font-lock-reference-face)
1818 "Default expressions to highlight in Scheme modes.")
1821 (defvar tex-font-lock-keywords
1822 ; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
1823 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
1824 ; 2 font-lock-function-name-face)
1825 ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
1826 ; 2 font-lock-reference-face)
1827 ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
1828 ; ;; not be able to display those fonts.
1829 ; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep)
1830 ; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep)
1831 ; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face)
1832 ; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep))
1833 ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>.
1834 '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
1835 2 font-lock-function-name-face)
1836 ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
1837 2 font-lock-reference-face)
1838 ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face)
1839 "\\\\\\([a-zA-Z@]+\\|.\\)"
1840 ;; It seems a bit dubious to use `bold' and `italic' faces since we might
1841 ;; not be able to display those fonts.
1842 ;; LaTeX2e: \emph{This is emphasized}.
1843 ("\\\\emph{\\([^}]+\\)}" 1 'italic keep)
1844 ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...}
1845 ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}"
1846 3 (if (match-beginning 2) 'bold 'italic) keep)
1847 ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for good tables.
1848 ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)"
1849 3 (if (match-beginning 2) 'bold 'italic) keep))
1850 "Default expressions to highlight in TeX modes.")
1854 ;; These provide a means to fontify types not defined by the language. Those
1855 ;; types might be the user's own or they might be generally accepted and used.
1856 ;; Generally accepted types are used to provide default variable values.
1858 (defvar c-font-lock-extra-types '("FILE" "\\sw+_t")
1859 "*List of extra types to fontify in C mode.
1860 Each list item should be a regexp not containing word-delimiters.
1861 For example, a value of (\"FILE\" \"\\\\sw+_t\") means the word FILE and words
1862 ending in _t are treated as type names.
1864 The value of this variable is used when Font Lock mode is turned on.")
1866 (defvar c++-font-lock-extra-types '("string")
1867 "*List of extra types to fontify in C++ mode.
1868 Each list item should be a regexp not containing word-delimiters.
1869 For example, a value of (\"string\") means the word string is treated as a type
1872 The value of this variable is used when Font Lock mode is turned on.")
1874 (defvar objc-font-lock-extra-types '("Class" "BOOL" "IMP" "SEL")
1875 "*List of extra types to fontify in Objective-C mode.
1876 Each list item should be a regexp not containing word-delimiters.
1877 For example, a value of (\"Class\" \"BOOL\" \"IMP\" \"SEL\") means the words
1878 Class, BOOL, IMP and SEL are treated as type names.
1880 The value of this variable is used when Font Lock mode is turned on.")
1882 (defvar java-font-lock-extra-types '("[A-Z\300-\326\330-\337]\\sw+")
1883 "*List of extra types to fontify in Java mode.
1884 Each list item should be a regexp not containing word-delimiters.
1885 For example, a value of (\"[A-Z\300-\326\330-\337]\\\\sw+\") means capitalised
1886 words (and words conforming to the Java id spec) are treated as type names.
1888 The value of this variable is used when Font Lock mode is turned on.")
1892 ;; [Murmur murmur murmur] Maestro, drum-roll please... [Murmur murmur murmur.]
1893 ;; Ahem. [Murmur murmur murmur] Lay-dees an Gennel-men. [Murmur murmur shhh!]
1894 ;; I am most proud and humbly honoured today [murmur murmur cough] to present
1895 ;; to you good people, the winner of the Second Millennium Award for The Most
1896 ;; Hairy Language Syntax. [Ahhh!] All rise please. [Shuffle shuffle
1897 ;; shuffle.] And a round of applause please. For... The C Language! [Roar.]
1899 ;; Thank you... You are too kind... It is with a feeling of great privilege
1900 ;; and indeed emotion [sob] that I accept this award. It has been a long hard
1901 ;; road. But we know our destiny. And our future. For we must not rest.
1902 ;; There are more tokens to overload, more shoehorn, more methodologies. But
1903 ;; more is a plus! [Ha ha ha.] And more means plus! [Ho ho ho.] The future
1904 ;; is C++! [Ohhh!] The Third Millennium Award... Will be ours! [Roar.]
1906 (defconst c-font-lock-keywords-1 nil
1907 "Subdued level highlighting for C mode.")
1909 (defconst c-font-lock-keywords-2 nil
1910 "Medium level highlighting for C mode.
1911 See also `c-font-lock-extra-types'.")
1913 (defconst c-font-lock-keywords-3 nil
1914 "Gaudy level highlighting for C mode.
1915 See also `c-font-lock-extra-types'.")
1918 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while")
1919 "break\\|continue\\|do\\|else\\|for\\|if\\|return\\|switch\\|while")
1921 ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
1922 ; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
1923 ; "void" "volatile" "const")
1924 `(mapconcat 'identity
1926 (,@ (concat "auto\\|c\\(har\\|onst\\)\\|double\\|"
1927 "e\\(num\\|xtern\\)\\|float\\|int\\|long\\|register\\|"
1928 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|typedef\\|"
1929 "un\\(ion\\|signed\\)\\|vo\\(id\\|latile\\)"))
1930 c-font-lock-extra-types)
1932 (c-type-depth `(font-lock-keyword-depth (,@ c-type-types)))
1934 (setq c-font-lock-keywords-1
1937 ;; These are all anchored at the beginning of line for speed.
1938 ;; Note that `c++-font-lock-keywords-1' depends on `c-font-lock-keywords-1'.
1940 ;; Fontify function name definitions (GNU style; without type on line).
1941 '("^\\(\\sw+\\)[ \t]*(" 1 font-lock-function-name-face)
1943 ;; Fontify error directives.
1944 '("^#[ \t]*error[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend)
1946 ;; Fontify filenames in #include <...> preprocessor directives as strings.
1947 '("^#[ \t]*\\(import\\|include\\)[ \t]+\\(<[^>\"\n]*>?\\)"
1948 2 font-lock-string-face)
1950 ;; Fontify function macro names.
1951 '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
1953 ;; Fontify symbol names in #elif or #if ... defined preprocessor directives.
1954 '("^#[ \t]*\\(elif\\|if\\)\\>"
1955 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
1956 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t)))
1958 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
1959 '("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?"
1960 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t))
1963 (setq c-font-lock-keywords-2
1964 (append c-font-lock-keywords-1
1967 ;; Simple regexps for speed.
1969 ;; Fontify all type specifiers.
1971 (cons (concat "\\<\\(" (,@ c-type-types) "\\)\\>") 'font-lock-type-face))
1973 ;; Fontify all builtin keywords (except case, default and goto; see below).
1974 (concat "\\<\\(" c-keywords "\\)\\>")
1976 ;; Fontify case/goto keywords and targets, and case default/goto tags.
1977 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
1978 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1979 ;; Anders Lindgren <andersl@csd.uu.se> points out that it is quicker to use
1980 ;; MATCH-ANCHORED to effectively anchor the regexp on the left.
1981 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:"
1982 (beginning-of-line) (end-of-line)
1983 (1 font-lock-reference-face)))
1986 (setq c-font-lock-keywords-3
1987 (append c-font-lock-keywords-2
1989 ;; More complicated regexps for more complete highlighting for types.
1990 ;; We still have to fontify type specifiers individually, as C is so hairy.
1993 ;; Fontify all storage classes and type specifiers, plus their items.
1995 (list (concat "\\<\\(" (,@ c-type-types) "\\)\\>"
1996 "\\([ \t*&]+\\sw+\\>\\)*")
1997 ;; Fontify each declaration item.
1998 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
1999 ;; Start with point after all type specifiers.
2000 (list 'goto-char (list 'or (list 'match-beginning
2001 (+ (,@ c-type-depth) 2))
2003 ;; Finish with point after first type specifier.
2004 '(goto-char (match-end 1))
2005 ;; Fontify as a variable or function name.
2006 '(1 (if (match-beginning 2)
2007 font-lock-function-name-face
2008 font-lock-variable-name-face)))))
2010 ;; Fontify structures, or typedef names, plus their items.
2011 '("\\(}\\)[ \t*]*\\sw"
2012 (font-lock-match-c-style-declaration-item-and-skip-to-next
2013 (goto-char (match-end 1)) nil
2014 (1 (if (match-beginning 2)
2015 font-lock-function-name-face
2016 font-lock-variable-name-face))))
2018 ;; Fontify anything at beginning of line as a declaration or definition.
2019 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2020 (1 font-lock-type-face)
2021 (font-lock-match-c-style-declaration-item-and-skip-to-next
2022 (goto-char (or (match-beginning 2) (match-end 1))) nil
2023 (1 (if (match-beginning 2)
2024 font-lock-function-name-face
2025 font-lock-variable-name-face))))
2029 (defvar c-font-lock-keywords c-font-lock-keywords-1
2030 "Default expressions to highlight in C mode.
2031 See also `c-font-lock-extra-types'.")
2035 (defconst c++-font-lock-keywords-1 nil
2036 "Subdued level highlighting for C++ mode.")
2038 (defconst c++-font-lock-keywords-2 nil
2039 "Medium level highlighting for C++ mode.
2040 See also `c++-font-lock-extra-types'.")
2042 (defconst c++-font-lock-keywords-3 nil
2043 "Gaudy level highlighting for C++ mode.
2044 See also `c++-font-lock-extra-types'.")
2046 (defun font-lock-match-c++-style-declaration-item-and-skip-to-next (limit)
2047 ;; Regexp matches after point: word<word>::word (
2049 ;; Where the match subexpressions are: 1 3 5 6
2051 ;; Item is delimited by (match-beginning 1) and (match-end 1).
2052 ;; If (match-beginning 3) is non-nil, that part of the item incloses a `<>'.
2053 ;; If (match-beginning 5) is non-nil, that part of the item follows a `::'.
2054 ;; If (match-beginning 6) is non-nil, the item is followed by a `('.
2055 (when (looking-at (eval-when-compile
2057 ;; Skip any leading whitespace.
2059 ;; This is `c++-type-spec' from below. (Hint hint!)
2060 "\\(\\sw+\\)" ; The instance?
2061 "\\(<\\(\\sw+\\)[ \t*&]*>\\)?" ; Or template?
2062 "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)?" ; Or member?
2063 ;; Match any trailing parenthesis.
2068 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
2069 (narrow-to-region (point-min) limit)
2070 (goto-char (match-end 1))
2071 ;; Move over any item value, etc., to the next item.
2072 (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|$\\)"))
2073 (goto-char (or (scan-sexps (point) 1) (point-max))))
2074 (goto-char (match-end 2)))
2077 (let* ((c++-keywords
2078 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while"
2079 ; "asm" "catch" "delete" "new" "operator" "sizeof" "this" "throw" "try"
2080 ; ;; Eric Hopper <hopper@omnifarious.mn.org> says these are new.
2081 ; "static_cast" "dynamic_cast" "const_cast" "reinterpret_cast")
2082 (concat "asm\\|break\\|c\\(atch\\|on\\(st_cast\\|tinue\\)\\)\\|"
2083 "d\\(elete\\|o\\|ynamic_cast\\)\\|else\\|for\\|if\\|new\\|"
2084 "operator\\|re\\(interpret_cast\\|turn\\)\\|"
2085 "s\\(izeof\\|tatic_cast\\|"
2086 "witch\\)\\|t\\(h\\(is\\|row\\)\\|ry\\)\\|while"))
2088 (mapconcat 'identity
2089 (mapcar 'regexp-quote
2090 ;; Taken from Stroustrup, minus keywords otherwise fontified.
2091 (sort '("+" "-" "*" "/" "%" "^" "&" "|" "~" "!" "=" "<" ">"
2092 "+=" "-=" "*=" "/=" "%=" "^=" "&=" "|=" "<<" ">>"
2093 ">>=" "<<=" "==" "!=" "<=" ">=" "&&" "||" "++" "--"
2094 "->*" "," "->" "[]" "()")
2095 #'(lambda (a b) (> (length a) (length b)))))
2098 ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
2099 ; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
2100 ; "void" "volatile" "const" "inline" "friend" "bool"
2101 ; "virtual" "complex" "template"
2102 ; ;; Eric Hopper <hopper@omnifarious.mn.org> says these are new.
2103 ; "namespace" "using")
2104 `(mapconcat 'identity
2106 (,@ (concat "auto\\|bool\\|c\\(har\\|o\\(mplex\\|nst\\)\\)\\|"
2107 "double\\|e\\(num\\|xtern\\)\\|f\\(loat\\|riend\\)\\|"
2108 "in\\(line\\|t\\)\\|long\\|namespace\\|register\\|"
2109 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|"
2110 "t\\(emplate\\|ypedef\\)\\|"
2111 "u\\(n\\(ion\\|signed\\)\\|sing\\)\\|"
2112 "v\\(irtual\\|o\\(id\\|latile\\)\\)")) ; 12 ()s deep.
2113 c++-font-lock-extra-types)
2116 ;; A brave attempt to match templates following a type and/or match
2117 ;; class membership. See and sync the above function
2118 ;; `font-lock-match-c++-style-declaration-item-and-skip-to-next'.
2119 (c++-type-suffix (concat "\\(<\\(\\sw+\\)[ \t*&]*>\\)?"
2120 "\\([ \t]*::[ \t*~]*\\(\\sw+\\)\\)?"))
2121 ;; If the string is a type, it may be followed by the cruft above.
2122 (c++-type-spec (concat "\\(\\sw+\\)\\>" c++-type-suffix))
2124 ;; Parenthesis depth of user-defined types not forgetting their cruft.
2125 (c++-type-depth `(font-lock-keyword-depth
2126 (concat (,@ c++-type-types) (,@ c++-type-suffix))))
2128 (setq c++-font-lock-keywords-1
2131 ;; The list `c-font-lock-keywords-1' less that for function names.
2132 (cdr c-font-lock-keywords-1)
2136 (list (concat "\\<\\(class\\|public\\|private\\|protected\\)\\>[ \t]*"
2137 "\\(" c++-type-spec "\\)?")
2138 '(1 font-lock-type-face)
2139 '(3 (if (match-beginning 6)
2141 font-lock-function-name-face) nil t)
2142 '(5 font-lock-function-name-face nil t)
2143 '(7 font-lock-function-name-face nil t))
2145 ;; Fontify function name definitions, possibly incorporating class names.
2146 (list (concat "^" c++-type-spec "[ \t]*(")
2147 '(1 (if (or (match-beginning 2) (match-beginning 4))
2149 font-lock-function-name-face))
2150 '(3 font-lock-function-name-face nil t)
2151 '(5 font-lock-function-name-face nil t))
2154 (setq c++-font-lock-keywords-2
2155 (append c++-font-lock-keywords-1
2158 ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading.
2160 (cons (concat "\\<\\(" (,@ c++-type-types) "\\)\\>")
2161 'font-lock-type-face))
2163 ;; Fontify operator overloading.
2164 (list (concat "\\<\\(operator\\)\\>[ \t]*\\(" c++-operators "\\)?")
2165 '(1 font-lock-keyword-face)
2166 '(2 font-lock-builtin-face nil t))
2168 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2169 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2170 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2171 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:\\($\\|[^:]\\)"
2172 (beginning-of-line) (end-of-line)
2173 (1 font-lock-reference-face)))
2175 ;; Fontify other builtin keywords.
2176 (cons (concat "\\<\\(" c++-keywords "\\)\\>") 'font-lock-keyword-face)
2178 ;; Eric Hopper <hopper@omnifarious.mn.org> says `true' and `false' are new.
2179 '("\\<\\(false\\|true\\)\\>" . font-lock-reference-face)
2182 (setq c++-font-lock-keywords-3
2183 (append c++-font-lock-keywords-2
2185 ;; More complicated regexps for more complete highlighting for types.
2188 ;; Fontify all storage classes and type specifiers, plus their items.
2190 (list (concat "\\<\\(" (,@ c++-type-types) "\\)\\>" (,@ c++-type-suffix)
2191 "\\([ \t*&]+" (,@ c++-type-spec) "\\)*")
2192 ;; Fontify each declaration item.
2193 (list 'font-lock-match-c++-style-declaration-item-and-skip-to-next
2194 ;; Start with point after all type specifiers.
2195 (list 'goto-char (list 'or (list 'match-beginning
2196 (+ (,@ c++-type-depth) 2))
2198 ;; Finish with point after first type specifier.
2199 '(goto-char (match-end 1))
2200 ;; Fontify as a variable or function name.
2201 '(1 (cond ((or (match-beginning 2) (match-beginning 4))
2202 font-lock-type-face)
2203 ((match-beginning 6) font-lock-function-name-face)
2204 (t font-lock-variable-name-face)))
2205 '(3 font-lock-function-name-face nil t)
2206 '(5 (if (match-beginning 6)
2207 font-lock-function-name-face
2208 font-lock-variable-name-face) nil t))))
2210 ;; Fontify structures, or typedef names, plus their items.
2211 '("\\(}\\)[ \t*]*\\sw"
2212 (font-lock-match-c++-style-declaration-item-and-skip-to-next
2213 (goto-char (match-end 1)) nil
2214 (1 (if (match-beginning 6)
2215 font-lock-function-name-face
2216 font-lock-variable-name-face))))
2218 ;; Fontify anything at beginning of line as a declaration or definition.
2219 (list (concat "^\\(" c++-type-spec "[ \t*&]*\\)+")
2220 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
2221 (goto-char (match-beginning 1))
2222 (goto-char (match-end 1))
2223 (1 (cond ((or (match-beginning 2) (match-beginning 4))
2224 font-lock-type-face)
2225 ((match-beginning 6) font-lock-function-name-face)
2226 (t font-lock-variable-name-face)))
2227 (3 font-lock-function-name-face nil t)
2228 (5 (if (match-beginning 6)
2229 font-lock-function-name-face
2230 font-lock-variable-name-face) nil t)))
2234 (defvar c++-font-lock-keywords c++-font-lock-keywords-1
2235 "Default expressions to highlight in C++ mode.
2236 See also `c++-font-lock-extra-types'.")
2240 (defconst objc-font-lock-keywords-1 nil
2241 "Subdued level highlighting for Objective-C mode.")
2243 (defconst objc-font-lock-keywords-2 nil
2244 "Medium level highlighting for Objective-C mode.
2245 See also `objc-font-lock-extra-types'.")
2247 (defconst objc-font-lock-keywords-3 nil
2248 "Gaudy level highlighting for Objective-C mode.
2249 See also `objc-font-lock-extra-types'.")
2251 ;; Regexps written with help from Stephen Peters <speters@us.oracle.com> and
2252 ;; Jacques Duthen Prestataire <duthen@cegelec-red.fr>.
2253 (let* ((objc-keywords
2254 ; '("break" "continue" "do" "else" "for" "if" "return" "switch" "while"
2255 ; "sizeof" "self" "super")
2256 (concat "break\\|continue\\|do\\|else\\|for\\|if\\|return\\|"
2257 "s\\(elf\\|izeof\\|uper\\|witch\\)\\|while"))
2259 `(mapconcat 'identity
2261 ; '("auto" "extern" "register" "static" "typedef" "struct" "union"
2262 ; "enum" "signed" "unsigned" "short" "long" "int" "char"
2263 ; "float" "double" "void" "volatile" "const"
2264 ; "id" "oneway" "in" "out" "inout" "bycopy" "byref")
2265 (,@ (concat "auto\\|by\\(copy\\|ref\\)\\|c\\(har\\|onst\\)\\|"
2266 "double\\|e\\(num\\|xtern\\)\\|float\\|"
2267 "i\\([dn]\\|n\\(out\\|t\\)\\)\\|long\\|"
2268 "o\\(neway\\|ut\\)\\|register\\|s\\(hort\\|igned\\|"
2269 "t\\(atic\\|ruct\\)\\)\\|typedef\\|"
2270 "un\\(ion\\|signed\\)\\|vo\\(id\\|latile\\)"))
2271 objc-font-lock-extra-types)
2273 (objc-type-depth `(font-lock-keyword-depth (,@ objc-type-types)))
2275 (setq objc-font-lock-keywords-1
2278 ;; The list `c-font-lock-keywords-1' less that for function names.
2279 (cdr c-font-lock-keywords-1)
2282 ;; Fontify compiler directives.
2284 (1 font-lock-keyword-face)
2285 ("\\=[ \t:<(,]*\\(\\sw+\\)" nil nil
2286 (1 font-lock-function-name-face)))
2288 ;; Fontify method names and arguments. Oh Lordy!
2289 ;; First, on the same line as the function declaration.
2290 '("^[+-][ \t]*\\(PRIVATE\\)?[ \t]*\\((\\([^)\n]+\\))\\)?[ \t]*\\(\\sw+\\)"
2291 (1 font-lock-type-face nil t)
2292 (3 font-lock-type-face nil t)
2293 (4 font-lock-function-name-face)
2294 ("\\=[ \t]*\\(\\sw+\\)?:[ \t]*\\((\\([^)\n]+\\))\\)?[ \t]*\\(\\sw+\\)"
2296 (1 font-lock-function-name-face nil t)
2297 (3 font-lock-type-face nil t)
2298 (4 font-lock-variable-name-face)))
2299 ;; Second, on lines following the function declaration.
2300 '(":" ("^[ \t]*\\(\\sw+\\)?:[ \t]*\\((\\([^)\n]+\\))\\)?[ \t]*\\(\\sw+\\)"
2301 (beginning-of-line) (end-of-line)
2302 (1 font-lock-function-name-face nil t)
2303 (3 font-lock-type-face nil t)
2304 (4 font-lock-variable-name-face)))
2307 (setq objc-font-lock-keywords-2
2308 (append objc-font-lock-keywords-1
2311 ;; Simple regexps for speed.
2313 ;; Fontify all type specifiers.
2315 (cons (concat "\\<\\(" (,@ objc-type-types) "\\)\\>")
2316 'font-lock-type-face))
2318 ;; Fontify all builtin keywords (except case, default and goto; see below).
2319 (concat "\\<\\(" objc-keywords "\\)\\>")
2321 ;; Fontify case/goto keywords and targets, and case default/goto tags.
2322 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2323 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2324 ;; Fontify tags iff sole statement on line, otherwise we detect selectors.
2325 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:[ \t]*$"
2326 (beginning-of-line) (end-of-line)
2327 (1 font-lock-reference-face)))
2329 ;; Fontify null object pointers.
2330 '("\\<\\(Nil\\|nil\\)\\>" 1 font-lock-reference-face)
2333 (setq objc-font-lock-keywords-3
2334 (append objc-font-lock-keywords-2
2336 ;; More complicated regexps for more complete highlighting for types.
2337 ;; We still have to fontify type specifiers individually, as C is so hairy.
2340 ;; Fontify all storage classes and type specifiers, plus their items.
2342 (list (concat "\\<\\(" (,@ objc-type-types) "\\)\\>"
2343 "\\([ \t*&]+\\sw+\\>\\)*")
2344 ;; Fontify each declaration item.
2345 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2346 ;; Start with point after all type specifiers.
2347 (list 'goto-char (list 'or (list 'match-beginning
2348 (+ (,@ objc-type-depth) 2))
2350 ;; Finish with point after first type specifier.
2351 '(goto-char (match-end 1))
2352 ;; Fontify as a variable or function name.
2353 '(1 (if (match-beginning 2)
2354 font-lock-function-name-face
2355 font-lock-variable-name-face)))))
2357 ;; Fontify structures, or typedef names, plus their items.
2358 '("\\(}\\)[ \t*]*\\sw"
2359 (font-lock-match-c-style-declaration-item-and-skip-to-next
2360 (goto-char (match-end 1)) nil
2361 (1 (if (match-beginning 2)
2362 font-lock-function-name-face
2363 font-lock-variable-name-face))))
2365 ;; Fontify anything at beginning of line as a declaration or definition.
2366 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
2367 (1 font-lock-type-face)
2368 (font-lock-match-c-style-declaration-item-and-skip-to-next
2369 (goto-char (or (match-beginning 2) (match-end 1))) nil
2370 (1 (if (match-beginning 2)
2371 font-lock-function-name-face
2372 font-lock-variable-name-face))))
2376 (defvar objc-font-lock-keywords objc-font-lock-keywords-1
2377 "Default expressions to highlight in Objective-C mode.
2378 See also `objc-font-lock-extra-types'.")
2382 (defconst java-font-lock-keywords-1 nil
2383 "Subdued level highlighting for Java mode.")
2385 (defconst java-font-lock-keywords-2 nil
2386 "Medium level highlighting for Java mode.
2387 See also `java-font-lock-extra-types'.")
2389 (defconst java-font-lock-keywords-3 nil
2390 "Gaudy level highlighting for Java mode.
2391 See also `java-font-lock-extra-types'.")
2393 ;; Regexps written with help from Fred White <fwhite@bbn.com> and
2394 ;; Anders Lindgren <andersl@csd.uu.se>.
2395 (let* ((java-keywords
2397 ; '("catch" "do" "else" "super" "this" "finally" "for" "if"
2398 ;; ;; Anders Lindgren <andersl@csd.uu.se> says these have gone.
2399 ;; "cast" "byvalue" "future" "generic" "operator" "var"
2400 ;; "inner" "outer" "rest"
2401 ; "interface" "return" "switch" "throw" "try" "while")
2402 "catch\\|do\\|else\\|f\\(inally\\|or\\)\\|"
2403 "i\\(f\\|nterface\\)\\|return\\|s\\(uper\\|witch\\)\\|"
2404 "t\\(h\\(is\\|row\\)\\|ry\\)\\|while"
2407 ;; These are immediately followed by an object name.
2409 (mapconcat 'identity
2410 '("boolean" "char" "byte" "short" "int" "long"
2411 "float" "double" "void")
2414 ;; These are eventually followed by an object name.
2416 ; '("abstract" "const" "final" "synchronized" "transient" "static"
2417 ;; ;; Anders Lindgren <andersl@csd.uu.se> says this has gone.
2419 ; "volatile" "public" "private" "protected" "native")
2420 (concat "abstract\\|const\\|final\\|native\\|"
2421 "p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|"
2422 "s\\(tatic\\|ynchronized\\)\\|transient\\|volatile"))
2424 ;; Random types immediately followed by an object name.
2426 '(mapconcat 'identity (cons "\\sw+\\.\\sw+" java-font-lock-extra-types)
2428 (java-other-depth `(font-lock-keyword-depth (,@ java-other-types)))
2430 (setq java-font-lock-keywords-1
2433 ;; Fontify class names.
2434 '("\\<\\(class\\)\\>[ \t]*\\(\\sw+\\)?"
2435 (1 font-lock-type-face) (2 font-lock-function-name-face nil t))
2437 ;; Fontify package names in import directives.
2438 '("\\<\\(import\\|package\\)\\>[ \t]*\\(\\sw+\\)?"
2439 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
2442 (setq java-font-lock-keywords-2
2443 (append java-font-lock-keywords-1
2446 ;; Fontify all builtin type specifiers.
2447 (cons (concat "\\<\\(" java-minor-types "\\|" java-major-types "\\)\\>")
2448 'font-lock-type-face)
2450 ;; Fontify all builtin keywords (except below).
2451 (concat "\\<\\(" java-keywords "\\)\\>")
2453 ;; Fontify keywords and targets, and case default/goto tags.
2454 (list "\\<\\(break\\|case\\|continue\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
2455 '(1 font-lock-keyword-face) '(2 font-lock-reference-face nil t))
2456 '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:"
2457 (beginning-of-line) (end-of-line)
2458 (1 font-lock-reference-face)))
2460 ;; Fontify keywords and types; the first can be followed by a type list.
2461 (list (concat "\\<\\("
2462 "implements\\|throws\\|"
2463 "\\(extends\\|instanceof\\|new\\)"
2464 "\\)\\>[ \t]*\\(\\sw+\\)?")
2465 '(1 font-lock-keyword-face) '(3 font-lock-type-face nil t)
2466 '("\\=[ \t]*,[ \t]*\\(\\sw+\\)"
2467 (if (match-beginning 2) (goto-char (match-end 2))) nil
2468 (1 font-lock-type-face)))
2470 ;; Fontify all constants.
2471 '("\\<\\(false\\|null\\|true\\)\\>" . font-lock-reference-face)
2473 ;; Javadoc tags within comments.
2474 '("@\\(author\\|exception\\|return\\|see\\|version\\)\\>"
2475 (1 font-lock-reference-face prepend))
2476 '("@\\(param\\)\\>[ \t]*\\(\\sw+\\)?"
2477 (1 font-lock-reference-face prepend)
2478 (2 font-lock-variable-name-face prepend t))
2481 (setq java-font-lock-keywords-3
2482 (append java-font-lock-keywords-2
2484 ;; More complicated regexps for more complete highlighting for types.
2485 ;; We still have to fontify type specifiers individually, as Java is hairy.
2488 ;; Fontify random types in casts.
2490 (list (concat "(\\(" (,@ java-other-types) "\\))"
2491 "[ \t]*\\(\\sw\\|[\"\(]\\)")
2492 ;; Fontify the type name.
2493 '(1 font-lock-type-face)))
2495 ;; Fontify random types immediately followed by an item or items.
2497 (list (concat "\\<\\(" (,@ java-other-types) "\\)\\>"
2498 "\\([ \t]*\\[[ \t]*\\]\\)*"
2500 ;; Fontify the type name.
2501 '(1 font-lock-type-face)))
2503 (list (concat "\\<\\(" (,@ java-other-types) "\\)\\>"
2504 "\\([ \t]*\\[[ \t]*\\]\\)*"
2506 ;; Fontify each declaration item.
2507 (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
2508 ;; Start and finish with point after the type specifier.
2509 (list 'goto-char (list 'match-beginning
2510 (+ (,@ java-other-depth) 3)))
2511 (list 'goto-char (list 'match-beginning
2512 (+ (,@ java-other-depth) 3)))
2513 ;; Fontify as a variable or function name.
2514 '(1 (if (match-beginning 2)
2515 font-lock-function-name-face
2516 font-lock-variable-name-face)))))
2518 ;; Fontify those that are immediately followed by an item or items.
2519 (list (concat "\\<\\(" java-minor-types "\\)\\>"
2520 "\\([ \t]*\\[[ \t]*\\]\\)*")
2521 ;; Fontify each declaration item.
2522 '(font-lock-match-c-style-declaration-item-and-skip-to-next
2523 ;; Start and finish with point after the type specifier.
2524 nil (goto-char (match-end 0))
2525 ;; Fontify as a variable or function name.
2526 (1 (if (match-beginning 2)
2527 font-lock-function-name-face
2528 font-lock-variable-name-face))))
2530 ;; Fontify those that are eventually followed by an item or items.
2531 (list (concat "\\<\\(" java-major-types "\\)\\>"
2533 "\\([ \t]*\\[[ \t]*\\]\\)*"
2535 ;; Fontify each declaration item.
2536 '(font-lock-match-c-style-declaration-item-and-skip-to-next
2537 ;; Start with point after all type specifiers.
2538 (goto-char (or (match-beginning 5) (match-end 1)))
2539 ;; Finish with point after first type specifier.
2540 (goto-char (match-end 1))
2541 ;; Fontify as a variable or function name.
2542 (1 (if (match-beginning 2)
2543 font-lock-function-name-face
2544 font-lock-variable-name-face))))
2548 (defvar java-font-lock-keywords java-font-lock-keywords-1
2549 "Default expressions to highlight in Java mode.
2550 See also `java-font-lock-extra-types'.")
2552 ;; Install ourselves:
2554 (unless (assq 'font-lock-mode minor-mode-alist)
2555 (push '(font-lock-mode " Font") minor-mode-alist))
2557 ;; Provide ourselves:
2559 (provide 'font-lock)
2561 ;;; font-lock.el ends here