1 ;;; font-lock.el --- Electric font lock mode
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996 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 ;; If you add patterns for a new mode, say foo.el's `foo-mode', say in which
54 ;; you don't want syntactic fontification to occur, you can make Font Lock mode
55 ;; use your regexps when turning on Font Lock by adding to `foo-mode-hook':
57 ;; (add-hook 'foo-mode-hook
58 ;; '(lambda () (make-local-variable 'font-lock-defaults)
59 ;; (setq font-lock-defaults '(foo-font-lock-keywords t))))
61 ;; Nasty regexps of the form "bar\\(\\|lo\\)\\|f\\(oo\\|u\\(\\|bar\\)\\)\\|lo"
62 ;; are made thusly: (make-regexp '("foo" "fu" "fubar" "bar" "barlo" "lo")) for
63 ;; efficiency. See /pub/gnu/emacs/elisp-archive/functions/make-regexp.el.Z on
64 ;; archive.cis.ohio-state.edu for this and other functions.
66 ;; What is fontification for? You might say, "It's to make my code look nice."
67 ;; I think it should be for adding information in the form of cues. These cues
68 ;; should provide you with enough information to both (a) distinguish between
69 ;; different items, and (b) identify the item meanings, without having to read
70 ;; the items and think about it. Therefore, fontification allows you to think
71 ;; less about, say, the structure of code, and more about, say, why the code
72 ;; doesn't work. Or maybe it allows you to think less and drift off to sleep.
74 ;; So, here are my opinions/advice/guidelines:
76 ;; - Highlight conceptual objects, such as function and variable names, and
77 ;; different objects types differently, i.e., (a) and (b) above, highlight
78 ;; function names differently to variable names.
79 ;; - Keep the faces distinct from each other as far as possible.
81 ;; - Use the same face for the same conceptual object, across all modes.
82 ;; i.e., (b) above, all modes that have items that can be thought of as, say,
83 ;; keywords, should be highlighted with the same face, etc.
84 ;; - Make the face attributes fit the concept as far as possible.
85 ;; i.e., function names might be a bold colour such as blue, comments might
86 ;; be a bright colour such as red, character strings might be brown, because,
87 ;; err, strings are brown (that was not the reason, please believe me).
88 ;; - Don't use a non-nil OVERRIDE unless you have a good reason.
89 ;; Only use OVERRIDE for special things that are easy to define, such as the
90 ;; way `...' quotes are treated in strings and comments in Emacs Lisp mode.
91 ;; Don't use it to, say, highlight keywords in commented out code or strings.
96 (defvar font-lock-verbose (* 0 1024)
97 "*If non-nil, means show status messages for buffer fontification.
98 If a number, only buffers greater than this size have fontification messages.")
101 (defvar font-lock-maximum-decoration nil
102 "*Maximum decoration level for fontification.
103 If nil, use the default decoration (typically the minimum available).
104 If t, use the maximum decoration available.
105 If a number, use that level of decoration (or if not available the maximum).
106 If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
107 where MAJOR-MODE is a symbol or t (meaning the default). For example:
108 ((c-mode . t) (c++-mode . 2) (t . 1))
109 means use the maximum decoration available for buffers in C mode, level 2
110 decoration for buffers in C++ mode, and level 1 decoration otherwise.")
113 (defvar font-lock-maximum-size (* 250 1024)
114 "*Maximum size of a buffer for buffer fontification.
115 Only buffers less than this can be fontified when Font Lock mode is turned on.
116 If nil, means size is irrelevant.
117 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
118 where MAJOR-MODE is a symbol or t (meaning the default). For example:
119 ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576))
120 means that the maximum size is 250K for buffers in C or C++ modes, one megabyte
121 for buffers in Rmail mode, and size is irrelevant otherwise.")
123 ;; Fontification variables:
125 (defvar font-lock-comment-face 'font-lock-comment-face
126 "Face to use for comments.")
128 (defvar font-lock-string-face 'font-lock-string-face
129 "Face to use for strings.")
131 (defvar font-lock-keyword-face 'font-lock-keyword-face
132 "Face to use for keywords.")
134 (defvar font-lock-function-name-face 'font-lock-function-name-face
135 "Face to use for function names.")
137 (defvar font-lock-variable-name-face 'font-lock-variable-name-face
138 "Face to use for variable names.")
140 (defvar font-lock-type-face 'font-lock-type-face
141 "Face to use for type names.")
143 (defvar font-lock-reference-face 'font-lock-reference-face
144 "Face to use for reference names.")
146 (defvar font-lock-keywords nil
147 "*A list of the keywords to highlight.
148 Each element should be of the form:
153 (MATCHER . HIGHLIGHT)
154 (MATCHER HIGHLIGHT ...)
157 where HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
159 FORM is an expression, whose value should be a keyword element, evaluated when
160 the keyword is (first) used in a buffer. This feature can be used to provide a
161 keyword that can only be generated when Font Lock mode is actually turned on.
163 For highlighting single items, typically only MATCH-HIGHLIGHT is required.
164 However, if an item or (typically) items are to be highlighted following the
165 instance of another item (the anchor) then MATCH-ANCHORED may be required.
167 MATCH-HIGHLIGHT should be of the form:
169 (MATCH FACENAME OVERRIDE LAXMATCH)
171 Where MATCHER can be either the regexp to search for, or the function name to
172 call to make the search (called with one argument, the limit of the search).
173 MATCH is the subexpression of MATCHER to be highlighted. FACENAME is an
174 expression whose value is the face name to use. FACENAME's default attributes
175 may be defined in `font-lock-face-attributes'.
177 OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification may
178 be overwritten. If `keep', only parts not already fontified are highlighted.
179 If `prepend' or `append', existing fontification is merged with the new, in
180 which the new or existing fontification, respectively, takes precedence.
181 If LAXMATCH is non-nil, no error is signaled if there is no MATCH in MATCHER.
183 For example, an element of the form highlights (if not already highlighted):
185 \"\\\\\\=<foo\\\\\\=>\" Discrete occurrences of \"foo\" in the value of the
186 variable `font-lock-keyword-face'.
187 (\"fu\\\\(bar\\\\)\" . 1) Substring \"bar\" within all occurrences of \"fubar\" in
188 the value of `font-lock-keyword-face'.
189 (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'.
190 (\"foo\\\\|bar\" 0 foo-bar-face t)
191 Occurrences of either \"foo\" or \"bar\" in the value
192 of `foo-bar-face', even if already highlighted.
194 MATCH-ANCHORED should be of the form:
196 (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
198 Where MATCHER is as for MATCH-HIGHLIGHT with one exception. The limit of the
199 search is currently guaranteed to be (no greater than) the end of the line.
200 PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
201 the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be
202 used to initialise before, and cleanup after, MATCHER is used. Typically,
203 PRE-MATCH-FORM is used to move to some position relative to the original
204 MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might
205 be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER.
207 For example, an element of the form highlights (if not already highlighted):
209 (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
211 Discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent
212 discrete occurrences of \"item\" (on the same line) in the value of `item-face'.
213 (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is
214 initially searched for starting from the end of the match of \"anchor\", and
215 searching for subsequent instance of \"anchor\" resumes from where searching
216 for \"item\" concluded.)
218 Note that the MATCH-ANCHORED feature is experimental; in the future, we may
219 replace it with other ways of providing this functionality.
221 These regular expressions should not match text which spans lines. While
222 \\[font-lock-fontify-buffer] handles multi-line patterns correctly, updating
223 when you edit the buffer does not, since it considers text one line at a time.
225 Be very careful composing regexps for this list;
226 the wrong pattern can dramatically slow things down!")
227 (make-variable-buffer-local 'font-lock-keywords)
229 (defvar font-lock-defaults nil
230 "If set by a major mode, should be the defaults for Font Lock mode.
231 The value should be like the `cdr' of an item in `font-lock-defaults-alist'.")
233 (defvar font-lock-defaults-alist
234 (let (;; For C and Lisp modes we use `beginning-of-defun', rather than nil,
235 ;; for SYNTAX-BEGIN. Thus the calculation of the cache is usually
236 ;; faster but not infallible, so we risk mis-fontification. --sm.
238 '((c-font-lock-keywords c-font-lock-keywords-1
239 c-font-lock-keywords-2 c-font-lock-keywords-3)
240 nil nil ((?_ . "w")) beginning-of-defun
241 (font-lock-mark-block-function . mark-defun)))
243 '((c++-font-lock-keywords c++-font-lock-keywords-1
244 c++-font-lock-keywords-2 c++-font-lock-keywords-3)
245 nil nil ((?_ . "w") (?~ . "w")) beginning-of-defun
246 (font-lock-mark-block-function . mark-defun)))
248 '((lisp-font-lock-keywords
249 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
250 nil nil (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
251 (font-lock-mark-block-function . mark-defun)))
252 (scheme-mode-defaults
253 '(scheme-font-lock-keywords
254 nil t (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
255 (font-lock-mark-block-function . mark-defun)))
256 ;; For TeX modes we could use `backward-paragraph' for the same reason.
257 ;; But we don't, because paragraph breaks are arguably likely enough to
258 ;; occur within a genuine syntactic block to make it too risky.
259 ;; However, we do specify a MARK-BLOCK function as that cannot result
260 ;; in a mis-fontification even if it might not fontify enough. --sm.
261 (tex-mode-defaults '(tex-font-lock-keywords nil nil ((?$ . "\"")) nil
262 (font-lock-mark-block-function . mark-paragraph)))
265 (cons 'bibtex-mode tex-mode-defaults)
266 (cons 'c++-c-mode c-mode-defaults)
267 (cons 'c++-mode c++-mode-defaults)
268 (cons 'c-mode c-mode-defaults)
269 (cons 'elec-c-mode c-mode-defaults)
270 (cons 'emacs-lisp-mode lisp-mode-defaults)
271 (cons 'inferior-scheme-mode scheme-mode-defaults)
272 (cons 'latex-mode tex-mode-defaults)
273 (cons 'lisp-mode lisp-mode-defaults)
274 (cons 'lisp-interaction-mode lisp-mode-defaults)
275 (cons 'plain-tex-mode tex-mode-defaults)
276 (cons 'scheme-mode scheme-mode-defaults)
277 (cons 'scheme-interaction-mode scheme-mode-defaults)
278 (cons 'slitex-mode tex-mode-defaults)
279 (cons 'tex-mode tex-mode-defaults)))
280 "Alist of default major mode and Font Lock defaults.
281 Each item should be a list of the form:
283 (MAJOR-MODE . (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN
286 where MAJOR-MODE is a symbol. KEYWORDS may be a symbol (a variable or function
287 whose value is the keywords to use for fontification) or a list of symbols.
288 If KEYWORDS-ONLY is non-nil, syntactic fontification (strings and comments) is
289 not performed. If CASE-FOLD is non-nil, the case of the keywords is ignored
290 when fontifying. If SYNTAX-ALIST is non-nil, it should be a list of cons pairs
291 of the form (CHAR-OR-STRING . STRING) used to set the local Font Lock syntax
292 table, for keyword and syntactic fontification (see `modify-syntax-entry').
294 If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
295 backwards outside any enclosing syntactic block, for syntactic fontification.
296 Typical values are `beginning-of-line' (i.e., the start of the line is known to
297 be outside a syntactic block), or `beginning-of-defun' for programming modes or
298 `backward-paragraph' for textual modes (i.e., the mode-dependent function is
299 known to move outside a syntactic block). If nil, the beginning of the buffer
300 is used as a position outside of a syntactic block, in the worst case.
302 These item elements are used by Font Lock mode to set the variables
303 `font-lock-keywords', `font-lock-keywords-only',
304 `font-lock-keywords-case-fold-search', `font-lock-syntax-table' and
305 `font-lock-beginning-of-syntax-function', respectively.
307 Further item elements are alists of the form (VARIABLE . VALUE) and are in no
308 particular order. Each VARIABLE is made buffer-local before set to VALUE.
310 Currently, appropriate variables include `font-lock-mark-block-function'.
311 If this is non-nil, it should be a function with no args used to mark any
312 enclosing block of text, for fontification via \\[font-lock-fontify-block].
313 Typical values are `mark-defun' for programming modes or `mark-paragraph' for
314 textual modes (i.e., the mode-dependent function is known to put point and mark
315 around a text block relevant to that mode).
317 Other variables include those for buffer-specialised fontification functions,
318 `font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function',
319 `font-lock-fontify-region-function', `font-lock-unfontify-region-function',
320 `font-lock-maximum-size' and `font-lock-inhibit-thing-lock'.")
322 (defvar font-lock-keywords-only nil
323 "*Non-nil means Font Lock should not fontify comments or strings.
324 This is normally set via `font-lock-defaults'.")
326 (defvar font-lock-keywords-case-fold-search nil
327 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
328 This is normally set via `font-lock-defaults'.")
330 (defvar font-lock-syntax-table nil
331 "Non-nil means use this syntax table for fontifying.
332 If this is nil, the major mode's syntax table is used.
333 This is normally set via `font-lock-defaults'.")
335 ;; If this is nil, we only use the beginning of the buffer if we can't use
336 ;; `font-lock-cache-position' and `font-lock-cache-state'.
337 (defvar font-lock-beginning-of-syntax-function nil
338 "*Non-nil means use this function to move back outside of a syntactic block.
339 When called with no args it should leave point at the beginning of any
340 enclosing syntactic block.
341 If this is nil, the beginning of the buffer is used (in the worst case).
342 This is normally set via `font-lock-defaults'.")
344 (defvar font-lock-mark-block-function nil
345 "*Non-nil means use this function to mark a block of text.
346 When called with no args it should leave point at the beginning of any
347 enclosing textual block and mark at the end.
348 This is normally set via `font-lock-defaults'.")
350 (defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
351 "Function to use for fontifying the buffer.
352 This is normally set via `font-lock-defaults'.")
354 (defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
355 "Function to use for unfontifying the buffer.
356 This is used when turning off Font Lock mode.
357 This is normally set via `font-lock-defaults'.")
359 (defvar font-lock-fontify-region-function 'font-lock-default-fontify-region
360 "Function to use for fontifying a region.
361 It should take two args, the beginning and end of the region, and an optional
362 third arg VERBOSE. If non-nil, the function should print status messages.
363 This is normally set via `font-lock-defaults'.")
365 (defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
366 "Function to use for unfontifying a region.
367 It should take two args, the beginning and end of the region.
368 This is normally set via `font-lock-defaults'.")
370 (defvar font-lock-inhibit-thing-lock nil
371 "List of Font Lock mode related modes that should not be turned on.
372 Currently, valid mode names as `fast-lock-mode' and `lazy-lock-mode'.
373 This is normally set via `font-lock-defaults'.")
375 (defvar font-lock-mode nil) ; For the modeline.
376 (defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
379 (defvar font-lock-mode-hook nil
380 "Function or functions to run on entry to Font Lock mode.")
385 ;; We don't do this at the top-level as we only use non-autoloaded macros.
389 (defun font-lock-mode (&optional arg)
390 "Toggle Font Lock mode.
391 With arg, turn Font Lock mode on if and only if arg is positive.
393 When Font Lock mode is enabled, text is fontified as you type it:
395 - Comments are displayed in `font-lock-comment-face';
396 - Strings are displayed in `font-lock-string-face';
397 - Certain other expressions are displayed in other faces according to the
398 value of the variable `font-lock-keywords'.
400 You can enable Font Lock mode in any major mode automatically by turning on in
401 the major mode's hook. For example, put in your ~/.emacs:
403 (add-hook 'c-mode-hook 'turn-on-font-lock)
405 Alternatively, you can use Global Font Lock mode to automagically turn on Font
406 Lock mode in buffers whose major mode supports it and whose major mode is one
407 of `font-lock-global-modes'. For example, put in your ~/.emacs:
409 (global-font-lock-mode t)
411 There are a number of support modes that may be used to speed up Font Lock mode
412 in various ways, specified via the variable `font-lock-support-mode'. Where
413 major modes support different levels of fontification, you can use the variable
414 `font-lock-maximum-decoration' to specify which level you generally prefer.
415 When you turn Font Lock mode on/off the buffer is fontified/defontified, though
416 fontification occurs only if the buffer is less than `font-lock-maximum-size'.
418 For example, to specify that Font Lock mode use use Lazy Lock mode as a support
419 mode and use maximum levels of fontification, put in your ~/.emacs:
421 (setq font-lock-support-mode 'lazy-lock-mode)
422 (setq font-lock-maximum-decoration t)
424 To fontify a buffer, without turning on Font Lock mode and regardless of buffer
425 size, you can use \\[font-lock-fontify-buffer].
427 To fontify a block (the function or paragraph containing point, or a number of
428 lines around point), perhaps because modification on the current line caused
429 syntactic change on other lines, you can use \\[font-lock-fontify-block].
431 The default Font Lock mode faces and their attributes are defined in the
432 variable `font-lock-face-attributes', and Font Lock mode default settings in
433 the variable `font-lock-defaults-alist'. You can set your own default settings
434 for some mode, by setting a buffer local value for `font-lock-defaults', via
437 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
438 ;; batch job) or if the buffer is invisible (the name starts with a space).
439 (let ((on-p (and (not noninteractive)
440 (not (eq (aref (buffer-name) 0) ?\ ))
442 (> (prefix-numeric-value arg) 0)
443 (not font-lock-mode)))))
444 (set (make-local-variable 'font-lock-mode) on-p)
445 ;; Turn on Font Lock mode.
447 (make-local-hook 'after-change-functions)
448 (add-hook 'after-change-functions 'font-lock-after-change-function nil t)
449 (font-lock-set-defaults)
450 (font-lock-turn-on-thing-lock)
451 (run-hooks 'font-lock-mode-hook)
452 ;; Fontify the buffer if we have to.
453 (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size)))
454 (cond (font-lock-fontified
456 ((or (null max-size) (> max-size (buffer-size)))
457 (font-lock-fontify-buffer))
459 (message "Fontifying %s...buffer too big" (buffer-name))))))
460 ;; Turn off Font Lock mode.
462 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
463 (font-lock-unfontify-buffer)
464 (font-lock-turn-off-thing-lock)
465 (font-lock-unset-defaults))
466 (force-mode-line-update)))
469 (defun turn-on-font-lock ()
470 "Turn on Font Lock mode conditionally.
471 Turn on only if the terminal can display it."
475 ;; Global Font Lock mode.
477 ;; A few people have hassled in the past for a way to make it easier to turn on
478 ;; Font Lock mode, without the user needing to know for which modes s/he has to
479 ;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always
480 ;; balked at that way, as I see it as just re-moulding the same problem in
481 ;; another form. That is; some person would still have to keep track of which
482 ;; modes (which may not even be distributed with Emacs) support Font Lock mode.
483 ;; The list would always be out of date. And that person might have to be me.
487 ;; In a previous discussion the following hack came to mind. It is a gross
488 ;; hack, but it generally works. We use the convention that major modes start
489 ;; by calling the function `kill-all-local-variables', which in turn runs
490 ;; functions on the hook variable `change-major-mode-hook'. We attach our
491 ;; function `font-lock-change-major-mode' to that hook. Of course, when this
492 ;; hook is run, the major mode is in the process of being changed and we do not
493 ;; know what the final major mode will be. So, `font-lock-change-major-mode'
494 ;; only (a) notes the name of the current buffer, and (b) adds our function
495 ;; `turn-on-font-lock-if-enabled' to the hook variables `find-file-hooks' and
496 ;; `post-command-hook' (for buffers that are not visiting files). By the time
497 ;; the functions on the first of these hooks to be run are run, the new major
498 ;; mode is assumed to be in place. This way we get a Font Lock function run
499 ;; when a major mode is turned on, without knowing major modes or their hooks.
501 ;; Naturally this requires that (a) major modes run `kill-all-local-variables',
502 ;; as they are supposed to do, and (b) the major mode is in place after the
503 ;; file is visited or the command that ran `kill-all-local-variables' has
504 ;; finished, whichever the sooner. Arguably, any major mode that does not
505 ;; follow the convension (a) is broken, and I can't think of any reason why (b)
506 ;; would not be met (except `gnudoit' on non-files). However, it is not clean.
508 ;; Probably the cleanest solution is to have each major mode function run some
509 ;; hook, e.g., `major-mode-hook', but maybe implementing that change is
510 ;; impractical. I am personally against making `setq' a macro or be advised,
511 ;; or have a special function such as `set-major-mode', but maybe someone can
512 ;; come up with another solution?
516 ;; Although Global Font Lock mode is a pseudo-mode, I think that the user
517 ;; interface should conform to the usual Emacs convention for modes, i.e., a
518 ;; command to toggle the feature (`global-font-lock-mode') with a variable for
519 ;; finer control of the mode's behaviour (`font-lock-global-modes').
521 ;; I don't think it is better that the feature be enabled via a variable, since
522 ;; it does not conform to the usual convention. I don't think the feature
523 ;; should be enabled by loading font-lock.el, since other mechanisms such as
524 ;; M-x font-lock-mode RET or (add-hook 'c-mode-hook 'turn-on-font-lock) would
525 ;; cause Font Lock mode to be turned on everywhere, and it is not intuitive or
526 ;; informative because loading a file tells you nothing about the feature or
527 ;; how to control it. It would be contrary to the Principle of Least Surprise.
529 (defvar font-lock-buffers nil) ; For remembering buffers.
530 (defvar global-font-lock-mode nil)
533 (defvar font-lock-global-modes t
534 "*Modes for which Font Lock mode is automagically turned on.
535 Global Font Lock mode is controlled by the `global-font-lock-mode' command.
536 If nil, means no modes have Font Lock mode automatically turned on.
537 If t, all modes that support Font Lock mode have it automatically turned on.
538 If a list, it should be a list of `major-mode' symbol names for which Font Lock
539 mode should be automatically turned on. The sense of the list is negated if it
540 begins with `not'. For example:
542 means that Font Lock mode is turned on for buffers in C and C++ modes only.")
545 (defun global-font-lock-mode (&optional arg message)
546 "Toggle Global Font Lock mode.
547 With prefix ARG, turn Global Font Lock mode on if and only if ARG is positive.
548 Displays a message saying whether the mode is on or off if MESSAGE is non-nil.
549 Returns the new status of Global Font Lock mode (non-nil means on).
551 When Global Font Lock mode is enabled, Font Lock mode is automagically
552 turned on in a buffer if its major mode is one of `font-lock-global-modes'."
555 (<= (prefix-numeric-value arg) 0)
556 global-font-lock-mode)))
558 (remove-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
559 (add-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
560 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
561 (setq font-lock-buffers (buffer-list)))
563 (message "Global Font Lock mode is now %s." (if off-p "OFF" "ON")))
564 (setq global-font-lock-mode (not off-p))))
566 (defun font-lock-change-major-mode ()
567 ;; Turn off Font Lock mode if it's on.
569 (font-lock-mode nil))
570 ;; Gross hack warning: Delicate readers should avert eyes now.
571 ;; Something is running `kill-all-local-variables', which generally means the
572 ;; major mode is being changed. Run `turn-on-font-lock-if-enabled' after the
573 ;; file is visited or the current command has finished.
574 (when global-font-lock-mode
575 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
576 (add-to-list 'font-lock-buffers (current-buffer))))
578 (defun turn-on-font-lock-if-enabled ()
579 ;; Gross hack warning: Delicate readers should avert eyes now.
580 ;; Turn on Font Lock mode if it's supported by the major mode and enabled by
582 (remove-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
583 (while font-lock-buffers
584 (if (buffer-live-p (car font-lock-buffers))
586 (set-buffer (car font-lock-buffers))
587 (if (and (or font-lock-defaults
588 (assq major-mode font-lock-defaults-alist))
589 (or (eq font-lock-global-modes t)
590 (if (eq (car-safe font-lock-global-modes) 'not)
591 (not (memq major-mode (cdr font-lock-global-modes)))
592 (memq major-mode font-lock-global-modes))))
594 (turn-on-font-lock)))))
595 (setq font-lock-buffers (cdr font-lock-buffers))))
597 (add-hook 'change-major-mode-hook 'font-lock-change-major-mode)
599 ;; End of Global Font Lock mode.
601 ;; Font Lock Support mode.
603 ;; This is the code used to interface font-lock.el with any of its add-on
604 ;; packages, and provide the user interface. Packages that have their own
605 ;; local buffer fontification functions (see below) may have to call
606 ;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
610 (defvar font-lock-support-mode nil
611 "*Support mode for Font Lock mode.
612 Support modes speed up Font Lock mode by being choosy about when fontification
613 occurs. Known support modes are Fast Lock mode (symbol `fast-lock-mode') and
614 Lazy Lock mode (symbol `lazy-lock-mode'). See those modes for more info.
615 If nil, means support for Font Lock mode is never performed.
616 If a symbol, use that support mode.
617 If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
618 where MAJOR-MODE is a symbol or t (meaning the default). For example:
619 ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
620 means that Fast Lock mode is used to support Font Lock mode for buffers in C or
621 C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
623 The value of this variable is used when Font Lock mode is turned on.")
625 (defun font-lock-turn-on-thing-lock ()
626 (let ((thing-mode (font-lock-value-in-major-mode font-lock-support-mode)))
627 (cond ((eq thing-mode 'fast-lock-mode)
629 ((eq thing-mode 'lazy-lock-mode)
630 (lazy-lock-mode t)))))
632 (defvar fast-lock-mode nil)
633 (defvar lazy-lock-mode nil)
635 (defun font-lock-turn-off-thing-lock ()
636 (cond (fast-lock-mode
637 (fast-lock-mode nil))
639 (lazy-lock-mode nil))))
641 (defun font-lock-after-fontify-buffer ()
642 (cond (fast-lock-mode
643 (fast-lock-after-fontify-buffer))
645 (lazy-lock-after-fontify-buffer))))
647 (defun font-lock-after-unfontify-buffer ()
648 (cond (fast-lock-mode
649 (fast-lock-after-unfontify-buffer))
651 (lazy-lock-after-unfontify-buffer))))
653 ;; End of Font Lock Support mode.
655 ;; Fontification functions.
658 (defun font-lock-fontify-buffer ()
659 "Fontify the current buffer the way `font-lock-mode' would."
661 (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
662 (funcall font-lock-fontify-buffer-function)))
664 (defun font-lock-unfontify-buffer ()
665 (funcall font-lock-unfontify-buffer-function))
667 (defun font-lock-fontify-region (beg end &optional loudly)
668 (funcall font-lock-fontify-region-function beg end loudly))
670 (defun font-lock-unfontify-region (beg end)
671 (funcall font-lock-unfontify-region-function beg end))
673 (defun font-lock-default-fontify-buffer ()
674 (let ((verbose (if (numberp font-lock-verbose)
675 (> (buffer-size) font-lock-verbose)
677 (if verbose (message "Fontifying %s..." (buffer-name)))
678 ;; Make sure we have the right `font-lock-keywords' etc.
679 (if (not font-lock-mode) (font-lock-set-defaults))
680 ;; Make sure we fontify etc. in the whole buffer.
686 (font-lock-fontify-region (point-min) (point-max) verbose)
687 (font-lock-after-fontify-buffer)
688 (setq font-lock-fontified t)))
689 ;; We don't restore the old fontification, so it's best to unfontify.
690 (quit (font-lock-unfontify-buffer))))
691 (if verbose (message "Fontifying %s...%s" (buffer-name)
692 (if font-lock-fontified "done" "aborted")))))
694 (defun font-lock-default-unfontify-buffer ()
697 (font-lock-unfontify-region (point-min) (point-max))
698 (font-lock-after-unfontify-buffer)
699 (setq font-lock-fontified nil)))
701 ;; We use this wrapper. However, `font-lock-fontify-region' used to be the
702 ;; name used for `font-lock-fontify-syntactically-region', so a change isn't
703 ;; back-compatible. But you shouldn't be calling these directly, should you?
704 (defun font-lock-default-fontify-region (beg end loudly)
705 (let ((modified (buffer-modified-p))
706 (buffer-undo-list t) (inhibit-read-only t)
707 (old-syntax-table (syntax-table))
708 before-change-functions after-change-functions
709 buffer-file-name buffer-file-truename)
713 ;; Use the fontification syntax table, if any.
714 (if font-lock-syntax-table (set-syntax-table font-lock-syntax-table))
715 ;; Now do the fontification.
716 (if font-lock-keywords-only
717 (font-lock-unfontify-region beg end)
718 (font-lock-fontify-syntactically-region beg end loudly))
719 (font-lock-fontify-keywords-region beg end loudly))
721 (set-syntax-table old-syntax-table)
722 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
724 ;; The following must be rethought, since keywords can override fontification.
725 ; ;; Now scan for keywords, but not if we are inside a comment now.
726 ; (or (and (not font-lock-keywords-only)
727 ; (let ((state (parse-partial-sexp beg end nil nil
728 ; font-lock-cache-state)))
729 ; (or (nth 4 state) (nth 7 state))))
730 ; (font-lock-fontify-keywords-region beg end))
732 (defun font-lock-default-unfontify-region (beg end)
733 (let ((modified (buffer-modified-p))
734 (buffer-undo-list t) (inhibit-read-only t)
735 before-change-functions after-change-functions
736 buffer-file-name buffer-file-truename)
737 (remove-text-properties beg end '(face nil))
738 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil))))
740 ;; Called when any modification is made to buffer text.
741 (defun font-lock-after-change-function (beg end old-len)
744 ;; Rescan between start of line from `beg' and start of line after `end'.
745 (font-lock-fontify-region
746 (progn (goto-char beg) (beginning-of-line) (point))
747 (progn (goto-char end) (forward-line 1) (point))))))
749 (defun font-lock-fontify-block (&optional arg)
750 "Fontify some lines the way `font-lock-fontify-buffer' would.
751 The lines could be a function or paragraph, or a specified number of lines.
752 If ARG is given, fontify that many lines before and after point, or 16 lines if
753 no ARG is given and `font-lock-mark-block-function' is nil.
754 If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
755 delimit the region to fontify."
757 (let (font-lock-beginning-of-syntax-function deactivate-mark)
758 ;; Make sure we have the right `font-lock-keywords' etc.
759 (if (not font-lock-mode) (font-lock-set-defaults))
762 (condition-case error-data
763 (if (or arg (not font-lock-mark-block-function))
764 (let ((lines (if arg (prefix-numeric-value arg) 16)))
765 (font-lock-fontify-region
766 (save-excursion (forward-line (- lines)) (point))
767 (save-excursion (forward-line lines) (point))))
768 (funcall font-lock-mark-block-function)
769 (font-lock-fontify-region (point) (mark)))
770 ((error quit) (message "Fontifying block...%s" error-data)))))))
772 (define-key facemenu-keymap "\M-g" 'font-lock-fontify-block)
774 ;; Syntactic fontification functions.
776 ;; These record the parse state at a particular position, always the start of a
777 ;; line. Used to make `font-lock-fontify-syntactically-region' faster.
778 (defvar font-lock-cache-position nil)
779 (defvar font-lock-cache-state nil)
780 (make-variable-buffer-local 'font-lock-cache-position)
781 (make-variable-buffer-local 'font-lock-cache-state)
783 (defun font-lock-fontify-syntactically-region (start end &optional loudly)
784 "Put proper face on each string and comment between START and END.
785 START should be at the beginning of a line."
786 (let ((synstart (if comment-start-skip
787 (concat "\\s\"\\|" comment-start-skip)
789 (comstart (if comment-start-skip
790 (concat "\\s<\\|" comment-start-skip)
792 state prev prevstate)
793 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
796 ;; Find the state at the `beginning-of-line' before `start'.
797 (if (eq start font-lock-cache-position)
798 ;; Use the cache for the state of `start'.
799 (setq state font-lock-cache-state)
800 ;; Find the state of `start'.
801 (if (null font-lock-beginning-of-syntax-function)
802 ;; Use the state at the previous cache position, if any, or
803 ;; otherwise calculate from `point-min'.
804 (if (or (null font-lock-cache-position)
805 (< start font-lock-cache-position))
806 (setq state (parse-partial-sexp (point-min) start))
807 (setq state (parse-partial-sexp font-lock-cache-position start
808 nil nil font-lock-cache-state)))
809 ;; Call the function to move outside any syntactic block.
810 (funcall font-lock-beginning-of-syntax-function)
811 (setq state (parse-partial-sexp (point) start)))
812 ;; Cache the state and position of `start'.
813 (setq font-lock-cache-state state
814 font-lock-cache-position start))
816 ;; If the region starts inside a string, show the extent of it.
819 (while (and (re-search-forward "\\s\"" end 'move)
820 (nth 3 (parse-partial-sexp beg (point) nil nil state))))
821 (put-text-property beg (point) 'face font-lock-string-face)
822 (setq state (parse-partial-sexp beg (point) nil nil state))))
824 ;; Likewise for a comment.
825 (if (or (nth 4 state) (nth 7 state))
828 (narrow-to-region (point-min) end)
831 (re-search-backward comstart (point-min) 'move)
833 ;; forward-comment skips all whitespace,
834 ;; so go back to the real end of the comment.
835 (skip-chars-backward " \t"))
836 (error (goto-char end))))
837 (put-text-property beg (point) 'face font-lock-comment-face)
838 (setq state (parse-partial-sexp beg (point) nil nil state))))
840 ;; Find each interesting place between here and `end'.
841 (while (and (< (point) end)
842 (setq prev (point) prevstate state)
843 (re-search-forward synstart end t)
845 ;; Clear out the fonts of what we skip over.
846 (remove-text-properties prev (point) '(face nil))
847 ;; Verify the state at that place
848 ;; so we don't get fooled by \" or \;.
849 (setq state (parse-partial-sexp prev (point)
851 (let ((here (point)))
852 (if (or (nth 4 state) (nth 7 state))
854 ;; We found a real comment start.
855 (let ((beg (match-beginning 0)))
858 (narrow-to-region (point-min) end)
862 ;; forward-comment skips all whitespace,
863 ;; so go back to the real end of the comment.
864 (skip-chars-backward " \t"))
865 (error (goto-char end))))
866 (put-text-property beg (point) 'face font-lock-comment-face)
867 (setq state (parse-partial-sexp here (point) nil nil state)))
870 ;; We found a real string start.
871 (let ((beg (match-beginning 0)))
872 (while (and (re-search-forward "\\s\"" end 'move)
873 (nth 3 (parse-partial-sexp here (point)
875 (put-text-property beg (point) 'face font-lock-string-face)
876 (setq state (parse-partial-sexp here (point)
879 ;; Make sure `prev' is non-nil after the loop
880 ;; only if it was set on the very last iteration.
884 (and prev (remove-text-properties prev end '(face nil)))))
886 ;;; Additional text property functions.
888 ;; The following three text property functions are not generally available (and
889 ;; it's not certain that they should be) so they are inlined for speed.
890 ;; The case for `fillin-text-property' is simple; it may or not be generally
891 ;; useful. (Since it is used here, it is useful in at least one place.;-)
892 ;; However, the case for `append-text-property' and `prepend-text-property' is
893 ;; more complicated. Should they remove duplicate property values or not? If
894 ;; so, should the first or last duplicate item remain? Or the one that was
895 ;; added? In our implementation, the first duplicate remains.
897 (defsubst font-lock-fillin-text-property (start end prop value &optional object)
898 "Fill in one property of the text from START to END.
899 Arguments PROP and VALUE specify the property and value to put where none are
900 already in place. Therefore existing property values are not overwritten.
901 Optional argument OBJECT is the string or buffer containing the text."
902 (let ((start (text-property-any start end prop nil object)) next)
904 (setq next (next-single-property-change start prop object end))
905 (put-text-property start next prop value object)
906 (setq start (text-property-any next end prop nil object)))))
908 ;; This function (from simon's unique.el) is rewritten and inlined for speed.
909 ;(defun unique (list function)
910 ; "Uniquify LIST, deleting elements using FUNCTION.
911 ;Return the list with subsequent duplicate items removed by side effects.
912 ;FUNCTION is called with an element of LIST and a list of elements from LIST,
913 ;and should return the list of elements with occurrences of the element removed,
914 ;i.e., a function such as `delete' or `delq'.
915 ;This function will work even if LIST is unsorted. See also `uniq'."
918 ; (setq list (setcdr list (funcall function (car list) (cdr list))))))
921 (defsubst font-lock-unique (list)
922 "Uniquify LIST, deleting elements using `delq'.
923 Return the list with subsequent duplicate items removed by side effects."
926 (setq list (setcdr list (delq (car list) (cdr list))))))
929 ;; A generalisation of `facemenu-add-face' for any property, but without the
930 ;; removal of inactive faces via `facemenu-discard-redundant-faces' and special
931 ;; treatment of `default'. Uses `unique' to remove duplicate property values.
932 (defsubst font-lock-prepend-text-property (start end prop value &optional object)
933 "Prepend to one property of the text from START to END.
934 Arguments PROP and VALUE specify the property and value to prepend to the value
935 already in place. The resulting property values are always lists, and unique.
936 Optional argument OBJECT is the string or buffer containing the text."
937 (let ((val (if (listp value) value (list value))) next prev)
938 (while (/= start end)
939 (setq next (next-single-property-change start prop object end)
940 prev (get-text-property start prop object))
943 (font-lock-unique (append val (if (listp prev) prev (list prev))))
947 (defsubst font-lock-append-text-property (start end prop value &optional object)
948 "Append to one property of the text from START to END.
949 Arguments PROP and VALUE specify the property and value to append to the value
950 already in place. The resulting property values are always lists, and unique.
951 Optional argument OBJECT is the string or buffer containing the text."
952 (let ((val (if (listp value) value (list value))) next prev)
953 (while (/= start end)
954 (setq next (next-single-property-change start prop object end)
955 prev (get-text-property start prop object))
958 (font-lock-unique (append (if (listp prev) prev (list prev)) val))
962 ;;; Regexp fontification functions.
964 (defsubst font-lock-apply-highlight (highlight)
965 "Apply HIGHLIGHT following a match.
966 HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
967 (let* ((match (nth 0 highlight))
968 (start (match-beginning match)) (end (match-end match))
969 (override (nth 2 highlight)))
971 ;; No match but we might not signal an error.
972 (or (nth 3 highlight)
973 (error "No match %d in highlight %S" match highlight)))
975 ;; Cannot override existing fontification.
976 (or (text-property-not-all start end 'face nil)
977 (put-text-property start end 'face (eval (nth 1 highlight)))))
979 ;; Override existing fontification.
980 (put-text-property start end 'face (eval (nth 1 highlight))))
982 ;; Keep existing fontification.
983 (font-lock-fillin-text-property start end 'face
984 (eval (nth 1 highlight))))
985 ((eq override 'prepend)
986 ;; Prepend to existing fontification.
987 (font-lock-prepend-text-property start end 'face
988 (eval (nth 1 highlight))))
989 ((eq override 'append)
990 ;; Append to existing fontification.
991 (font-lock-append-text-property start end 'face
992 (eval (nth 1 highlight)))))))
994 (defsubst font-lock-fontify-anchored-keywords (keywords limit)
995 "Fontify according to KEYWORDS until LIMIT.
996 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords'."
997 (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights)
998 ;; Until we come up with a cleaner solution, we make LIMIT the end of line.
999 (save-excursion (end-of-line) (setq limit (min limit (point))))
1000 ;; Evaluate PRE-MATCH-FORM.
1001 (eval (nth 1 keywords))
1003 ;; Find an occurrence of `matcher' before `limit'.
1004 (while (if (stringp matcher)
1005 (re-search-forward matcher limit t)
1006 (funcall matcher limit))
1007 ;; Apply each highlight to this instance of `matcher'.
1008 (setq highlights lowdarks)
1010 (font-lock-apply-highlight (car highlights))
1011 (setq highlights (cdr highlights)))))
1012 ;; Evaluate POST-MATCH-FORM.
1013 (eval (nth 2 keywords))))
1015 (defun font-lock-fontify-keywords-region (start end &optional loudly)
1016 "Fontify according to `font-lock-keywords' between START and END.
1017 START should be at the beginning of a line."
1018 (let ((case-fold-search font-lock-keywords-case-fold-search)
1019 (keywords (cdr (if (eq (car-safe font-lock-keywords) t)
1021 (font-lock-compile-keywords))))
1022 (bufname (buffer-name)) (count 0)
1023 keyword matcher highlights)
1025 ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
1027 (if loudly (message "Fontifying %s... (regexps..%s)" bufname
1028 (make-string (setq count (1+ count)) ?.)))
1030 ;; Find an occurrence of `matcher' from `start' to `end'.
1031 (setq keyword (car keywords) matcher (car keyword))
1033 (while (if (stringp matcher)
1034 (re-search-forward matcher end t)
1035 (funcall matcher end))
1036 ;; Apply each highlight to this instance of `matcher', which may be
1037 ;; specific highlights or more keywords anchored to `matcher'.
1038 (setq highlights (cdr keyword))
1040 (if (numberp (car (car highlights)))
1041 (font-lock-apply-highlight (car highlights))
1042 (font-lock-fontify-anchored-keywords (car highlights) end))
1043 (setq highlights (cdr highlights))))
1044 (setq keywords (cdr keywords)))))
1046 ;; Various functions.
1048 (defun font-lock-compile-keywords (&optional keywords)
1049 ;; Compile `font-lock-keywords' into the form (t KEYWORD ...) where KEYWORD
1050 ;; is the (MATCHER HIGHLIGHT ...) shown in the variable's doc string.
1051 (let ((keywords (or keywords font-lock-keywords)))
1052 (setq font-lock-keywords
1053 (if (eq (car-safe keywords) t)
1055 (cons t (mapcar 'font-lock-compile-keyword keywords))))))
1057 (defun font-lock-compile-keyword (keyword)
1058 (cond ((nlistp keyword) ; Just MATCHER
1059 (list keyword '(0 font-lock-keyword-face)))
1060 ((eq (car keyword) 'eval) ; Specified (eval . FORM)
1061 (font-lock-compile-keyword (eval (cdr keyword))))
1062 ((numberp (cdr keyword)) ; Specified (MATCHER . MATCH)
1063 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
1064 ((symbolp (cdr keyword)) ; Specified (MATCHER . FACENAME)
1065 (list (car keyword) (list 0 (cdr keyword))))
1066 ((nlistp (nth 1 keyword)) ; Specified (MATCHER . HIGHLIGHT)
1067 (list (car keyword) (cdr keyword)))
1068 (t ; Hopefully (MATCHER HIGHLIGHT ...)
1071 (defun font-lock-value-in-major-mode (alist)
1072 ;; Return value in ALIST for `major-mode', or ALIST if it is not an alist.
1073 ;; Alist structure is ((MAJOR-MODE . VALUE)) where MAJOR-MODE may be t.
1075 (cdr (or (assq major-mode alist) (assq t alist)))
1078 (defun font-lock-choose-keywords (keywords level)
1079 ;; Return LEVELth element of KEYWORDS. A LEVEL of nil is equal to a
1080 ;; LEVEL of 0, a LEVEL of t is equal to (1- (length KEYWORDS)).
1081 (cond ((symbolp keywords)
1084 (or (nth level keywords) (car (reverse keywords))))
1086 (car (reverse keywords)))
1090 (defun font-lock-set-defaults ()
1091 "Set fontification defaults appropriately for this mode.
1092 Sets various variables using `font-lock-defaults' (or, if nil, using
1093 `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
1094 ;; Set face defaults.
1095 (font-lock-make-faces)
1096 ;; Set fontification defaults.
1097 (make-local-variable 'font-lock-fontified)
1098 (if (member font-lock-keywords '(nil (t)))
1099 (let* ((defaults (or font-lock-defaults
1100 (cdr (assq major-mode font-lock-defaults-alist))))
1102 (font-lock-choose-keywords (nth 0 defaults)
1103 (font-lock-value-in-major-mode font-lock-maximum-decoration))))
1104 ;; Regexp fontification?
1105 (setq font-lock-keywords (if (fboundp keywords)
1108 ;; Syntactic fontification?
1109 (if (nth 1 defaults)
1110 (set (make-local-variable 'font-lock-keywords-only) t))
1111 ;; Case fold during regexp fontification?
1112 (if (nth 2 defaults)
1113 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
1114 ;; Syntax table for regexp and syntactic fontification?
1115 (if (nth 3 defaults)
1116 (let ((slist (nth 3 defaults)))
1117 (set (make-local-variable 'font-lock-syntax-table)
1118 (copy-syntax-table (syntax-table)))
1120 ;; The character to modify may be a single CHAR or a STRING.
1121 (let ((chars (if (numberp (car (car slist)))
1122 (list (car (car slist)))
1123 (mapcar 'identity (car (car slist)))))
1124 (syntax (cdr (car slist))))
1126 (modify-syntax-entry (car chars) syntax
1127 font-lock-syntax-table)
1128 (setq chars (cdr chars)))
1129 (setq slist (cdr slist))))))
1130 ;; Syntax function for syntactic fontification?
1131 (if (nth 4 defaults)
1132 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1135 (let ((alist (nthcdr 5 defaults)))
1137 (set (make-local-variable (car (car alist))) (cdr (car alist)))
1138 (setq alist (cdr alist)))))))
1140 (defun font-lock-unset-defaults ()
1141 "Unset fontification defaults. See `font-lock-set-defaults'."
1142 (setq font-lock-keywords nil
1143 font-lock-keywords-only nil
1144 font-lock-keywords-case-fold-search nil
1145 font-lock-syntax-table nil
1146 font-lock-beginning-of-syntax-function nil)
1147 (let* ((defaults (or font-lock-defaults
1148 (cdr (assq major-mode font-lock-defaults-alist))))
1149 (alist (nthcdr 5 defaults)))
1151 (set (car (car alist)) (default-value (car (car alist))))
1152 (setq alist (cdr alist)))))
1154 ;; Colour etc. support.
1156 ;; This section of code is crying out for revision.
1158 ;; To begin with, `display-type' and `background-mode' are `frame-parameters'
1159 ;; so we don't have to calculate them here anymore. But all the face stuff
1160 ;; should be frame-local (and thus display-local) anyway. Because we're not
1161 ;; sure what support Emacs is going to have for general frame-local face
1162 ;; attributes, we leave this section of code as it is. For now. --sm.
1164 (defvar font-lock-display-type nil
1165 "A symbol indicating the display Emacs is running under.
1166 The symbol should be one of `color', `grayscale' or `mono'.
1167 If Emacs guesses this display attribute wrongly, either set this variable in
1168 your `~/.emacs' or set the resource `Emacs.displayType' in your `~/.Xdefaults'.
1169 See also `font-lock-background-mode' and `font-lock-face-attributes'.")
1171 (defvar font-lock-background-mode nil
1172 "A symbol indicating the Emacs background brightness.
1173 The symbol should be one of `light' or `dark'.
1174 If Emacs guesses this frame attribute wrongly, either set this variable in
1175 your `~/.emacs' or set the resource `Emacs.backgroundMode' in your
1177 See also `font-lock-display-type' and `font-lock-face-attributes'.")
1179 (defvar font-lock-face-attributes nil
1180 "A list of default attributes to use for face attributes.
1181 Each element of the list should be of the form
1183 (FACE FOREGROUND BACKGROUND BOLD-P ITALIC-P UNDERLINE-P)
1185 where FACE should be one of the face symbols `font-lock-comment-face',
1186 `font-lock-string-face', `font-lock-keyword-face', `font-lock-type-face',
1187 `font-lock-function-name-face', `font-lock-variable-name-face', and
1188 `font-lock-reference-face'. A form for each of these face symbols should be
1189 provided in the list, but other face symbols and attributes may be given and
1190 used in highlighting. See `font-lock-keywords'.
1192 Subsequent element items should be the attributes for the corresponding
1193 Font Lock mode faces. Attributes FOREGROUND and BACKGROUND should be strings
1194 \(default if nil), while BOLD-P, ITALIC-P, and UNDERLINE-P should specify the
1195 corresponding face attributes (yes if non-nil).
1197 Emacs uses default attributes based on display type and background brightness.
1198 See variables `font-lock-display-type' and `font-lock-background-mode'.
1200 Resources can be used to over-ride these face attributes. For example, the
1201 resource `Emacs.font-lock-comment-face.attributeUnderline' can be used to
1202 specify the UNDERLINE-P attribute for face `font-lock-comment-face'.")
1204 (defun font-lock-make-faces (&optional override)
1205 "Make faces from `font-lock-face-attributes'.
1206 A default list is used if this is nil.
1207 If optional OVERRIDE is non-nil, faces that already exist are reset.
1208 See `font-lock-make-face' and `list-faces-display'."
1209 ;; We don't need to `setq' any of these variables, but the user can see what
1210 ;; is being used if we do.
1211 (if (null font-lock-display-type)
1212 (setq font-lock-display-type
1213 (let ((display-resource (x-get-resource ".displayType"
1215 (cond (display-resource (intern (downcase display-resource)))
1216 ((x-display-color-p) 'color)
1217 ((x-display-grayscale-p) 'grayscale)
1219 (if (null font-lock-background-mode)
1220 (setq font-lock-background-mode
1221 (let ((bg-resource (x-get-resource ".backgroundMode"
1223 (params (frame-parameters)))
1224 (cond (bg-resource (intern (downcase bg-resource)))
1225 ((eq system-type 'ms-dos)
1226 (if (string-match "light"
1227 (cdr (assq 'background-color params)))
1230 ((< (apply '+ (x-color-values
1231 (cdr (assq 'background-color params))))
1232 (* (apply '+ (x-color-values "white")) .6))
1235 (if (null font-lock-face-attributes)
1236 (setq font-lock-face-attributes
1237 (let ((light-bg (eq font-lock-background-mode 'light)))
1238 (cond ((memq font-lock-display-type '(mono monochrome))
1239 ;; Emacs 19.25's font-lock defaults:
1240 ;;'((font-lock-comment-face nil nil nil t nil)
1241 ;; (font-lock-string-face nil nil nil nil t)
1242 ;; (font-lock-keyword-face nil nil t nil nil)
1243 ;; (font-lock-function-name-face nil nil t t nil)
1244 ;; (font-lock-type-face nil nil nil t nil))
1245 (list '(font-lock-comment-face nil nil t t nil)
1246 '(font-lock-string-face nil nil nil t nil)
1247 '(font-lock-keyword-face nil nil t nil nil)
1249 'font-lock-function-name-face
1250 (cdr (assq 'background-color (frame-parameters)))
1251 (cdr (assq 'foreground-color (frame-parameters)))
1253 '(font-lock-variable-name-face nil nil t t nil)
1254 '(font-lock-type-face nil nil t nil t)
1255 '(font-lock-reference-face nil nil t nil t)))
1256 ((memq font-lock-display-type '(grayscale greyscale
1257 grayshade greyshade))
1259 (list 'font-lock-comment-face
1260 nil (if light-bg "Gray80" "DimGray") t t nil)
1261 (list 'font-lock-string-face
1262 nil (if light-bg "Gray50" "LightGray") nil t nil)
1263 (list 'font-lock-keyword-face
1264 nil (if light-bg "Gray90" "DimGray") t nil nil)
1265 (list 'font-lock-function-name-face
1266 (cdr (assq 'background-color (frame-parameters)))
1267 (cdr (assq 'foreground-color (frame-parameters)))
1269 (list 'font-lock-variable-name-face
1270 nil (if light-bg "Gray90" "DimGray") t t nil)
1271 (list 'font-lock-type-face
1272 nil (if light-bg "Gray80" "DimGray") t nil t)
1273 (list 'font-lock-reference-face
1274 nil (if light-bg "LightGray" "Gray50") t nil t)))
1275 (light-bg ; light colour background
1276 '((font-lock-comment-face "Firebrick")
1277 (font-lock-string-face "RosyBrown")
1278 (font-lock-keyword-face "Purple")
1279 (font-lock-function-name-face "Blue")
1280 (font-lock-variable-name-face "DarkGoldenrod")
1281 (font-lock-type-face "DarkOliveGreen")
1282 (font-lock-reference-face "CadetBlue")))
1283 (t ; dark colour background
1284 '((font-lock-comment-face "OrangeRed")
1285 (font-lock-string-face "LightSalmon")
1286 (font-lock-keyword-face "LightSteelBlue")
1287 (font-lock-function-name-face "LightSkyBlue")
1288 (font-lock-variable-name-face "LightGoldenrod")
1289 (font-lock-type-face "PaleGreen")
1290 (font-lock-reference-face "Aquamarine")))))))
1291 ;; Now make the faces if we have to.
1293 (lambda (face-attributes)
1294 (let ((face (nth 0 face-attributes)))
1296 ;; We can stomp all over it anyway. Get outta my face!
1297 (font-lock-make-face face-attributes))
1298 ((and (boundp face) (facep (symbol-value face)))
1299 ;; The variable exists and is already bound to a face.
1302 ;; We already have a face so we bind the variable to it.
1305 ;; No variable or no face.
1306 (font-lock-make-face face-attributes))))))
1307 font-lock-face-attributes))
1309 (defun font-lock-make-face (face-attributes)
1310 "Make a face from FACE-ATTRIBUTES.
1311 FACE-ATTRIBUTES should be like an element `font-lock-face-attributes', so that
1312 the face name is the first item in the list. A variable with the same name as
1313 the face is also set; its value is the face name."
1314 (let* ((face (nth 0 face-attributes))
1315 (face-name (symbol-name face))
1316 (set-p (function (lambda (face-name resource)
1317 (x-get-resource (concat face-name ".attribute" resource)
1318 (concat "Face.Attribute" resource)))))
1319 (on-p (function (lambda (face-name resource)
1320 (let ((set (funcall set-p face-name resource)))
1321 (and set (member (downcase set) '("on" "true"))))))))
1323 (add-to-list 'facemenu-unlisted-faces face)
1324 ;; Set attributes not set from X resources (and therefore `make-face').
1325 (or (funcall set-p face-name "Foreground")
1327 (set-face-foreground face (nth 1 face-attributes))
1329 (or (funcall set-p face-name "Background")
1331 (set-face-background face (nth 2 face-attributes))
1333 (if (funcall set-p face-name "Bold")
1334 (and (funcall on-p face-name "Bold") (make-face-bold face nil t))
1335 (and (nth 3 face-attributes) (make-face-bold face nil t)))
1336 (if (funcall set-p face-name "Italic")
1337 (and (funcall on-p face-name "Italic") (make-face-italic face nil t))
1338 (and (nth 4 face-attributes) (make-face-italic face nil t)))
1339 (or (funcall set-p face-name "Underline")
1340 (set-face-underline-p face (nth 5 face-attributes)))
1343 ;;; Various regexp information shared by several modes.
1344 ;;; Information specific to a single mode should go in its load library.
1346 (defconst lisp-font-lock-keywords-1
1348 ;; Anything not a variable or type declaration is fontified as a function.
1349 ;; It would be cleaner to allow preceding whitespace, but it would also be
1350 ;; about five times slower.
1351 (list (concat "^(\\(def\\("
1352 ;; Variable declarations.
1353 "\\(const\\(\\|ant\\)\\|ine-key\\(\\|-after\\)\\|var\\)\\|"
1354 ;; Structure declarations.
1355 "\\(class\\|struct\\|type\\)\\|"
1356 ;; Everything else is a function declaration.
1357 "\\([^ \t\n\(\)]+\\)"
1359 ;; Any whitespace and declared object.
1362 '(1 font-lock-keyword-face)
1363 '(8 (cond ((match-beginning 3) font-lock-variable-name-face)
1364 ((match-beginning 6) font-lock-type-face)
1365 (t font-lock-function-name-face))
1368 "Subdued level highlighting for Lisp modes.")
1370 (defconst lisp-font-lock-keywords-2
1371 (append lisp-font-lock-keywords-1
1374 ;; Control structures. ELisp and CLisp combined.
1376 ; '("cond" "if" "while" "let\\*?" "prog[nv12*]?" "inline" "catch" "throw"
1377 ; "save-restriction" "save-excursion" "save-window-excursion"
1378 ; "save-selected-window" "save-match-data" "unwind-protect"
1379 ; "condition-case" "track-mouse"
1380 ; "eval-after-load" "eval-and-compile" "eval-when-compile"
1381 ; "when" "unless" "do" "flet" "labels" "return" "return-from"
1382 ; "with-output-to-temp-buffer" "with-timeout"))
1386 "c\\(atch\\|ond\\(\\|ition-case\\)\\)\\|do\\|"
1387 "eval-\\(a\\(fter-load\\|nd-compile\\)\\|when-compile\\)\\|flet\\|"
1388 "i\\(f\\|nline\\)\\|l\\(abels\\|et\\*?\\)\\|prog[nv12*]?\\|"
1389 "return\\(\\|-from\\)\\|save-\\(excursion\\|match-data\\|restriction\\|"
1390 "selected-window\\|window-excursion\\)\\|t\\(hrow\\|rack-mouse\\)\\|"
1391 "un\\(less\\|wind-protect\\)\\|"
1392 "w\\(h\\(en\\|ile\\)\\|ith-\\(output-to-temp-buffer\\|timeout\\)\\)"
1395 ;; Feature symbols as references.
1396 '("(\\(featurep\\|provide\\|require\\)\\>[ \t']*\\(\\sw+\\)?"
1397 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1399 ;; Words inside \\[] tend to be for `substitute-command-keys'.
1400 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-reference-face prepend)
1402 ;; Words inside `' tend to be symbol names.
1403 '("`\\(\\sw\\sw+\\)'" 1 font-lock-reference-face prepend)
1405 ;; CLisp `:' keywords as references.
1406 '("\\<:\\sw+\\>" 0 font-lock-reference-face prepend)
1408 ;; ELisp and CLisp `&' keywords as types.
1409 '("\\<\\&\\sw+\\>" . font-lock-type-face)
1411 "Gaudy level highlighting for Lisp modes.")
1413 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
1414 "Default expressions to highlight in Lisp modes.")
1417 (defvar scheme-font-lock-keywords
1421 ;; Declarations. Hannes Haug <hannes.haug@student.uni-tuebingen.de> says
1422 ;; this works for SOS, STklos, SCOOPS, Meroon and Tiny CLOS.
1423 (list (concat "(\\(define\\("
1425 "\\(\\|-\\(generic\\(\\|-procedure\\)\\|method\\)\\)\\|"
1426 ;; Macro names, as variable names. A bit dubious, this.
1431 ;; Any whitespace and declared object.
1434 '(1 font-lock-keyword-face)
1435 '(8 (cond ((match-beginning 3) font-lock-function-name-face)
1436 ((match-beginning 6) font-lock-variable-name-face)
1437 (t font-lock-type-face))
1440 ;; Control structures.
1441 ;(make-regexp '("begin" "call-with-current-continuation" "call/cc"
1442 ; "call-with-input-file" "call-with-output-file" "case" "cond"
1443 ; "do" "else" "for-each" "if" "lambda"
1444 ; "let\\*?" "let-syntax" "letrec" "letrec-syntax"
1445 ; ;; Hannes Haug <hannes.haug@student.uni-tuebingen.de> wants:
1446 ; "and" "or" "delay"
1447 ; ;; Stefan Monnier <stefan.monnier@epfl.ch> says don't bother:
1448 ; ;;"quasiquote" "quote" "unquote" "unquote-splicing"
1449 ; "map" "syntax" "syntax-rules"))
1452 "and\\|begin\\|c\\(a\\(ll\\(-with-\\(current-continuation\\|"
1453 "input-file\\|output-file\\)\\|/cc\\)\\|se\\)\\|ond\\)\\|"
1454 "d\\(elay\\|o\\)\\|else\\|for-each\\|if\\|"
1455 "l\\(ambda\\|et\\(-syntax\\|\\*?\\|rec\\(\\|-syntax\\)\\)\\)\\|"
1456 "map\\|or\\|syntax\\(\\|-rules\\)"
1459 ;; David Fox <fox@graphics.cs.nyu.edu> for SOS/STklos class specifiers.
1460 '("\\<<\\sw+>\\>" . font-lock-type-face)
1462 ;; Scheme `:' keywords as references.
1463 '("\\<:\\sw+\\>" . font-lock-reference-face)
1465 "Default expressions to highlight in Scheme modes.")
1468 (defconst c-font-lock-keywords-1 nil
1469 "Subdued level highlighting for C modes.")
1471 (defconst c-font-lock-keywords-2 nil
1472 "Medium level highlighting for C modes.")
1474 (defconst c-font-lock-keywords-3 nil
1475 "Gaudy level highlighting for C modes.")
1477 (defconst c++-font-lock-keywords-1 nil
1478 "Subdued level highlighting for C++ modes.")
1480 (defconst c++-font-lock-keywords-2 nil
1481 "Medium level highlighting for C++ modes.")
1483 (defconst c++-font-lock-keywords-3 nil
1484 "Gaudy level highlighting for C++ modes.")
1486 (defun font-lock-match-c++-style-declaration-item-and-skip-to-next (limit)
1487 ;; Match, and move over, any declaration/definition item after point.
1488 ;; The expect syntax of an item is "word" or "word::word", possibly ending
1489 ;; with optional whitespace and a "(". Everything following the item (but
1490 ;; belonging to it) is expected to by skip-able by `forward-sexp', and items
1491 ;; are expected to be separated with a "," or ";".
1492 (if (looking-at "[ \t*&]*\\(\\sw+\\)\\(::\\(\\sw+\\)\\)?[ \t]*\\((\\)?")
1496 ;; Restrict to the end of line, currently guaranteed to be LIMIT.
1497 (narrow-to-region (point-min) limit)
1498 (goto-char (match-end 1))
1499 ;; Move over any item value, etc., to the next item.
1500 (while (not (looking-at "[ \t]*\\([,;]\\|$\\)"))
1501 (goto-char (or (scan-sexps (point) 1) (point-max))))
1502 (goto-char (match-end 0)))
1506 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while")
1507 "break\\|continue\\|do\\|else\\|for\\|if\\|return\\|switch\\|while")
1509 ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
1510 ; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
1511 ; "void" "volatile" "const")
1512 (concat "auto\\|c\\(har\\|onst\\)\\|double\\|e\\(num\\|xtern\\)\\|"
1513 "float\\|int\\|long\\|register\\|"
1514 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|typedef\\|"
1515 "un\\(ion\\|signed\\)\\|vo\\(id\\|latile\\)")) ; 6 ()s deep.
1517 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while"
1518 ; "asm" "catch" "delete" "new" "operator" "sizeof" "this" "throw" "try"
1519 ; "protected" "private" "public")
1520 (concat "asm\\|break\\|c\\(atch\\|ontinue\\)\\|d\\(elete\\|o\\)\\|"
1521 "else\\|for\\|if\\|new\\|"
1522 "p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|return\\|"
1523 "s\\(izeof\\|witch\\)\\|t\\(h\\(is\\|row\\)\\|ry\\)\\|while"))
1525 ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum"
1526 ; "signed" "unsigned" "short" "long" "int" "char" "float" "double"
1527 ; "void" "volatile" "const" "class" "inline" "friend" "bool"
1528 ; "virtual" "complex" "template")
1529 (concat "auto\\|bool\\|c\\(har\\|lass\\|o\\(mplex\\|nst\\)\\)\\|"
1530 "double\\|e\\(num\\|xtern\\)\\|f\\(loat\\|riend\\)\\|"
1531 "in\\(line\\|t\\)\\|long\\|register\\|"
1532 "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|"
1533 "t\\(emplate\\|ypedef\\)\\|un\\(ion\\|signed\\)\\|"
1534 "v\\(irtual\\|o\\(id\\|latile\\)\\)")) ; 11 ()s deep.
1536 (setq c-font-lock-keywords-1
1539 ;; These are all anchored at the beginning of line for speed.
1541 ;; Fontify function name definitions (GNU style; without type on line).
1542 (list (concat "^\\(\\sw+\\)[ \t]*(") 1 'font-lock-function-name-face)
1544 ;; Fontify filenames in #include <...> preprocessor directives as strings.
1545 '("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face)
1547 ;; Fontify function macro names.
1548 '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
1550 ;; Fontify symbol names in #elif or #if ... defined preprocessor directives.
1551 '("^#[ \t]*\\(elif\\|if\\)\\>"
1552 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
1553 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t)))
1555 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
1556 '("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?"
1557 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t))
1560 (setq c-font-lock-keywords-2
1561 (append c-font-lock-keywords-1
1564 ;; Simple regexps for speed.
1566 ;; Fontify all type specifiers.
1567 (cons (concat "\\<\\(" c-type-types "\\)\\>") 'font-lock-type-face)
1569 ;; Fontify all builtin keywords (except case, default and goto; see below).
1570 (cons (concat "\\<\\(" c-keywords "\\)\\>") 'font-lock-keyword-face)
1572 ;; Fontify case/goto keywords and targets, and case default/goto tags.
1573 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(\\sw+\\)?"
1574 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1575 '("^[ \t]*\\(\\sw+\\)[ \t]*:" 1 font-lock-reference-face)
1578 (setq c-font-lock-keywords-3
1579 (append c-font-lock-keywords-2
1581 ;; More complicated regexps for more complete highlighting for types.
1582 ;; We still have to fontify type specifiers individually, as C is so hairy.
1585 ;; Fontify all storage classes and type specifiers, plus their items.
1586 (list (concat "\\<\\(" c-type-types "\\)\\>"
1587 "\\([ \t*&]+\\sw+\\>\\)*")
1588 ;; Fontify each declaration item.
1589 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
1590 ;; Start with point after all type specifiers.
1591 (goto-char (or (match-beginning 8) (match-end 1)))
1592 ;; Finish with point after first type specifier.
1593 (goto-char (match-end 1))
1594 ;; Fontify as a variable or function name.
1595 (1 (if (match-beginning 4)
1596 font-lock-function-name-face
1597 font-lock-variable-name-face))))
1599 ;; Fontify structures, or typedef names, plus their items.
1600 '("\\(}\\)[ \t*]*\\sw"
1601 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1602 (goto-char (match-end 1)) nil
1603 (1 (if (match-beginning 4)
1604 font-lock-function-name-face
1605 font-lock-variable-name-face))))
1607 ;; Fontify anything at beginning of line as a declaration or definition.
1608 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
1609 (1 font-lock-type-face)
1610 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1611 (goto-char (or (match-beginning 2) (match-end 1))) nil
1612 (1 (if (match-beginning 4)
1613 font-lock-function-name-face
1614 font-lock-variable-name-face))))
1617 (setq c++-font-lock-keywords-1
1620 ;; The list `c-font-lock-keywords-1' less that for function names.
1621 (cdr c-font-lock-keywords-1)
1623 ;; Fontify function name definitions, possibly incorporating class name.
1625 '("^\\(\\sw+\\)\\(::\\(\\sw+\\)\\)?[ \t]*("
1626 (1 (if (match-beginning 2)
1628 font-lock-function-name-face))
1629 (3 font-lock-function-name-face nil t))
1632 (setq c++-font-lock-keywords-2
1633 (append c++-font-lock-keywords-1
1636 ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading.
1637 (cons (concat "\\<\\(" c++-type-types "\\)\\>") 'font-lock-type-face)
1639 ;; Fontify operator function name overloading.
1640 '("\\<\\(operator\\)\\>[ \t]*\\([[(><!=+-][])><=+-]?\\)?"
1641 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
1643 ;; Fontify case/goto keywords and targets, and case default/goto tags.
1644 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(\\sw+\\)?"
1645 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1646 '("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-reference-face)
1648 ;; Fontify other builtin keywords.
1649 (cons (concat "\\<\\(" c++-keywords "\\)\\>") 'font-lock-keyword-face)
1652 (setq c++-font-lock-keywords-3
1653 (append c++-font-lock-keywords-2
1655 ;; More complicated regexps for more complete highlighting for types.
1658 ;; Fontify all storage classes and type specifiers, plus their items.
1659 (list (concat "\\<\\(" c++-type-types "\\)\\>"
1660 "\\([ \t*&]+\\sw+\\>\\)*")
1661 ;; Fontify each declaration item.
1662 '(font-lock-match-c++-style-declaration-item-and-skip-to-next
1663 ;; Start with point after all type specifiers.
1664 (goto-char (or (match-beginning 13) (match-end 1)))
1665 ;; Finish with point after first type specifier.
1666 (goto-char (match-end 1))
1667 ;; Fontify as a variable or function name.
1668 (1 (cond ((match-beginning 2) font-lock-type-face)
1669 ((match-beginning 4) font-lock-function-name-face)
1670 (t font-lock-variable-name-face)))
1671 (3 (if (match-beginning 4)
1672 font-lock-function-name-face
1673 font-lock-variable-name-face) nil t)))
1675 ;; Fontify structures, or typedef names, plus their items.
1676 '("\\(}\\)[ \t*]*\\sw"
1677 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1678 (goto-char (match-end 1)) nil
1679 (1 (if (match-beginning 4)
1680 font-lock-function-name-face
1681 font-lock-variable-name-face))))
1683 ;; Fontify anything at beginning of line as a declaration or definition.
1684 '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*"
1685 (1 font-lock-type-face)
1686 (font-lock-match-c++-style-declaration-item-and-skip-to-next
1687 (goto-char (or (match-beginning 2) (match-end 1))) nil
1688 (1 (cond ((match-beginning 2) font-lock-type-face)
1689 ((match-beginning 4) font-lock-function-name-face)
1690 (t font-lock-variable-name-face)))
1691 (3 (if (match-beginning 4)
1692 font-lock-function-name-face
1693 font-lock-variable-name-face) nil t)))
1697 (defvar c-font-lock-keywords c-font-lock-keywords-1
1698 "Default expressions to highlight in C mode.")
1700 (defvar c++-font-lock-keywords c++-font-lock-keywords-1
1701 "Default expressions to highlight in C++ mode.")
1704 (defvar tex-font-lock-keywords
1705 ; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
1706 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
1707 ; 2 font-lock-function-name-face)
1708 ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
1709 ; 2 font-lock-reference-face)
1710 ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
1711 ; ;; not be able to display those fonts.
1712 ; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep)
1713 ; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep)
1714 ; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face)
1715 ; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep))
1716 ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>.
1717 '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
1718 2 font-lock-function-name-face)
1719 ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
1720 2 font-lock-reference-face)
1721 ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face)
1722 "\\\\\\([a-zA-Z@]+\\|.\\)"
1723 ;; It seems a bit dubious to use `bold' and `italic' faces since we might
1724 ;; not be able to display those fonts.
1725 ;; LaTeX2e: \emph{This is emphasized}.
1726 ("\\\\emph{\\([^}]+\\)}" 1 'italic keep)
1727 ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...}
1728 ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}"
1729 3 (if (match-beginning 2) 'bold 'italic) keep)
1730 ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for good tables.
1731 ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)"
1732 3 (if (match-beginning 2) 'bold 'italic) keep))
1733 "Default expressions to highlight in TeX modes.")
1735 ;; Install ourselves:
1737 (unless (assq 'font-lock-mode minor-mode-alist)
1738 (setq minor-mode-alist (cons '(font-lock-mode " Font") minor-mode-alist)))
1740 ;; Provide ourselves:
1742 (provide 'font-lock)
1744 ;;; font-lock.el ends here