Commit | Line | Data |
---|---|---|
876f2438 SM |
1 | ;;; font-lock.el --- Electric font lock mode |
2 | ||
799761f0 | 3 | ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc. |
030f4a35 | 4 | |
9bfbb130 | 5 | ;; Author: jwz, then rms, then sm <simon@gnu.ai.mit.edu> |
030f4a35 RS |
6 | ;; Maintainer: FSF |
7 | ;; Keywords: languages, faces | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
15 | ||
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. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
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. | |
030f4a35 | 25 | |
030f4a35 RS |
26 | ;;; Commentary: |
27 | ||
b89e1134 SM |
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. | |
030f4a35 RS |
30 | ;; |
31 | ;; Comments will be displayed in `font-lock-comment-face'. | |
32 | ;; Strings will be displayed in `font-lock-string-face'. | |
a1eb1cf1 | 33 | ;; Regexps are used to display selected patterns in other faces. |
030f4a35 | 34 | ;; |
9bfbb130 SM |
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. | |
030f4a35 | 38 | ;; |
b89e1134 | 39 | ;; To turn Font Lock mode on automatically, add this to your .emacs file: |
030f4a35 | 40 | ;; |
b89e1134 | 41 | ;; (add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock) |
030f4a35 | 42 | ;; |
9bfbb130 SM |
43 | ;; Fontification for a particular mode may be available in a number of levels |
44 | ;; of decoration. The higher the level, the more decoration, but the more time | |
45 | ;; it takes to fontify. See the variable `font-lock-maximum-decoration', and | |
46 | ;; also the variable `font-lock-maximum-size'. | |
47 | ||
b89e1134 SM |
48 | ;; If you add patterns for a new mode, say foo.el's `foo-mode', say in which |
49 | ;; you don't want syntactic fontification to occur, you can make Font Lock mode | |
50 | ;; use your regexps when turning on Font Lock by adding to `foo-mode-hook': | |
51 | ;; | |
52 | ;; (add-hook 'foo-mode-hook | |
53 | ;; '(lambda () (make-local-variable 'font-lock-defaults) | |
54 | ;; (setq font-lock-defaults '(foo-font-lock-keywords t)))) | |
55 | ;; | |
a1eb1cf1 RS |
56 | ;; Nasty regexps of the form "bar\\(\\|lo\\)\\|f\\(oo\\|u\\(\\|bar\\)\\)\\|lo" |
57 | ;; are made thusly: (make-regexp '("foo" "fu" "fubar" "bar" "barlo" "lo")) for | |
58 | ;; efficiency. See /pub/gnu/emacs/elisp-archive/functions/make-regexp.el.Z on | |
59 | ;; archive.cis.ohio-state.edu for this and other functions. | |
9bfbb130 SM |
60 | |
61 | ;; What is fontification for? You might say, "It's to make my code look nice." | |
62 | ;; I think it should be for adding information in the form of cues. These cues | |
63 | ;; should provide you with enough information to both (a) distinguish between | |
64 | ;; different items, and (b) identify the item meanings, without having to read | |
65 | ;; the items and think about it. Therefore, fontification allows you to think | |
66 | ;; less about, say, the structure of code, and more about, say, why the code | |
67 | ;; doesn't work. Or maybe it allows you to think less and drift off to sleep. | |
68 | ;; | |
69 | ;; So, here are my opinions/advice/guidelines: | |
70 | ;; | |
7d7d915a SM |
71 | ;; - Highlight conceptual objects, such as function and variable names, and |
72 | ;; different objects types differently, i.e., (a) and (b) above, highlight | |
73 | ;; function names differently to variable names. | |
74 | ;; - Keep the faces distinct from each other as far as possible. | |
75 | ;; i.e., (a) above. | |
9bfbb130 SM |
76 | ;; - Use the same face for the same conceptual object, across all modes. |
77 | ;; i.e., (b) above, all modes that have items that can be thought of as, say, | |
78 | ;; keywords, should be highlighted with the same face, etc. | |
9bfbb130 SM |
79 | ;; - Make the face attributes fit the concept as far as possible. |
80 | ;; i.e., function names might be a bold colour such as blue, comments might | |
81 | ;; be a bright colour such as red, character strings might be brown, because, | |
82 | ;; err, strings are brown (that was not the reason, please believe me). | |
83 | ;; - Don't use a non-nil OVERRIDE unless you have a good reason. | |
84 | ;; Only use OVERRIDE for special things that are easy to define, such as the | |
85 | ;; way `...' quotes are treated in strings and comments in Emacs Lisp mode. | |
86 | ;; Don't use it to, say, highlight keywords in commented out code or strings. | |
87 | ;; - Err, that's it. | |
a1eb1cf1 | 88 | \f |
9bfbb130 SM |
89 | ;; User variables. |
90 | ||
91 | (defvar font-lock-verbose t | |
92 | "*If non-nil, means show status messages when fontifying.") | |
93 | ||
94 | ;;;###autoload | |
95 | (defvar font-lock-maximum-decoration nil | |
96 | "*If non-nil, the maximum decoration level for fontifying. | |
97 | If nil, use the default decoration (typically the minimum available). | |
98 | If t, use the maximum decoration available. | |
99 | If a number, use that level of decoration (or if not available the maximum). | |
100 | If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL), | |
101 | where MAJOR-MODE is a symbol or t (meaning the default). For example: | |
102 | ((c++-mode . 2) (c-mode . t) (t . 1)) | |
103 | means use level 2 decoration for buffers in `c++-mode', the maximum decoration | |
104 | available for buffers in `c-mode', and level 1 decoration otherwise.") | |
030f4a35 | 105 | |
9bfbb130 SM |
106 | ;;;###autoload |
107 | (defvar font-lock-maximum-size (* 250 1024) | |
108 | "*If non-nil, the maximum size for buffers for fontifying. | |
109 | Only buffers less than this can be fontified when Font Lock mode is turned on. | |
110 | If nil, means size is irrelevant. | |
111 | If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE), | |
112 | where MAJOR-MODE is a symbol or t (meaning the default). For example: | |
113 | ((c++-mode . 256000) (c-mode . 256000) (rmail-mode . 1048576)) | |
114 | means that the maximum size is 250K for buffers in `c++-mode' or `c-mode', one | |
115 | megabyte for buffers in `rmail-mode', and size is irrelevant otherwise.") | |
116 | \f | |
117 | ;; Fontification variables: | |
118 | ||
119 | (defvar font-lock-comment-face 'font-lock-comment-face | |
a1eb1cf1 | 120 | "Face to use for comments.") |
030f4a35 | 121 | |
9bfbb130 | 122 | (defvar font-lock-string-face 'font-lock-string-face |
a1eb1cf1 | 123 | "Face to use for strings.") |
030f4a35 | 124 | |
9bfbb130 SM |
125 | (defvar font-lock-keyword-face 'font-lock-keyword-face |
126 | "Face to use for keywords.") | |
127 | ||
128 | (defvar font-lock-function-name-face 'font-lock-function-name-face | |
030f4a35 RS |
129 | "Face to use for function names.") |
130 | ||
9bfbb130 | 131 | (defvar font-lock-variable-name-face 'font-lock-variable-name-face |
a1eb1cf1 RS |
132 | "Face to use for variable names.") |
133 | ||
9bfbb130 SM |
134 | (defvar font-lock-type-face 'font-lock-type-face |
135 | "Face to use for type names.") | |
030f4a35 | 136 | |
9bfbb130 SM |
137 | (defvar font-lock-reference-face 'font-lock-reference-face |
138 | "Face to use for reference names.") | |
a1eb1cf1 | 139 | |
030f4a35 | 140 | (defvar font-lock-keywords nil |
d46c21ec SM |
141 | "*A list of the keywords to highlight. |
142 | Each element should be of the form: | |
a1eb1cf1 | 143 | |
826b2925 SM |
144 | MATCHER |
145 | (MATCHER . MATCH) | |
146 | (MATCHER . FACENAME) | |
147 | (MATCHER . HIGHLIGHT) | |
148 | (MATCHER HIGHLIGHT ...) | |
a1eb1cf1 | 149 | |
9bfbb130 SM |
150 | where HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED. |
151 | ||
152 | For highlighting single items, typically only MATCH-HIGHLIGHT is required. | |
d9f7b2d3 | 153 | However, if an item or (typically) items are to be highlighted following the |
9bfbb130 SM |
154 | instance of another item (the anchor) then MATCH-ANCHORED may be required. |
155 | ||
156 | MATCH-HIGHLIGHT should be of the form: | |
157 | ||
158 | (MATCH FACENAME OVERRIDE LAXMATCH) | |
159 | ||
160 | Where MATCHER can be either the regexp to search for, or the function name to | |
161 | call to make the search (called with one argument, the limit of the search). | |
162 | MATCH is the subexpression of MATCHER to be highlighted. FACENAME is an | |
163 | expression whose value is the face name to use. FACENAME's default attributes | |
164 | may be defined in `font-lock-face-attributes'. | |
a1eb1cf1 RS |
165 | |
166 | OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification may | |
9bfbb130 SM |
167 | be overwritten. If `keep', only parts not already fontified are highlighted. |
168 | If `prepend' or `append', existing fontification is merged with the new, in | |
169 | which the new or existing fontification, respectively, takes precedence. | |
d9f7b2d3 | 170 | If LAXMATCH is non-nil, no error is signaled if there is no MATCH in MATCHER. |
a1eb1cf1 | 171 | |
9bfbb130 SM |
172 | For example, an element of the form highlights (if not already highlighted): |
173 | ||
876f2438 SM |
174 | \"\\\\\\=<foo\\\\\\=>\" Discrete occurrences of \"foo\" in the value of the |
175 | variable `font-lock-keyword-face'. | |
9bfbb130 SM |
176 | (\"fu\\\\(bar\\\\)\" . 1) Substring \"bar\" within all occurrences of \"fubar\" in |
177 | the value of `font-lock-keyword-face'. | |
178 | (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of `fubar-face'. | |
179 | (\"foo\\\\|bar\" 0 foo-bar-face t) | |
180 | Occurrences of either \"foo\" or \"bar\" in the value | |
181 | of `foo-bar-face', even if already highlighted. | |
182 | ||
183 | MATCH-ANCHORED should be of the form: | |
184 | ||
185 | (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...) | |
186 | ||
876f2438 SM |
187 | Where MATCHER is as for MATCH-HIGHLIGHT with one exception. The limit of the |
188 | search is currently guaranteed to be (no greater than) the end of the line. | |
189 | PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after | |
190 | the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be | |
191 | used to initialise before, and cleanup after, MATCHER is used. Typically, | |
192 | PRE-MATCH-FORM is used to move to some position relative to the original | |
193 | MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might | |
194 | be used to move, before resuming with MATCH-ANCHORED's parent's MATCHER. | |
9bfbb130 SM |
195 | |
196 | For example, an element of the form highlights (if not already highlighted): | |
197 | ||
876f2438 | 198 | (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face) (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face))) |
9bfbb130 | 199 | |
876f2438 SM |
200 | Discrete occurrences of \"anchor\" in the value of `anchor-face', and subsequent |
201 | discrete occurrences of \"item\" (on the same line) in the value of `item-face'. | |
202 | (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is | |
203 | initially searched for starting from the end of the match of \"anchor\", and | |
204 | searching for subsequent instance of \"anchor\" resumes from where searching | |
205 | for \"item\" concluded.) | |
9bfbb130 SM |
206 | |
207 | Note that the MATCH-ANCHORED feature is experimental; in the future, we may | |
208 | replace it with other ways of providing this functionality. | |
209 | ||
a1eb1cf1 RS |
210 | These regular expressions should not match text which spans lines. While |
211 | \\[font-lock-fontify-buffer] handles multi-line patterns correctly, updating | |
212 | when you edit the buffer does not, since it considers text one line at a time. | |
213 | ||
9bfbb130 | 214 | Be very careful composing regexps for this list; |
a1eb1cf1 | 215 | the wrong pattern can dramatically slow things down!") |
9bfbb130 | 216 | (make-variable-buffer-local 'font-lock-keywords) |
030f4a35 | 217 | |
b89e1134 SM |
218 | (defvar font-lock-defaults nil |
219 | "If set by a major mode, should be the defaults for Font Lock mode. | |
9bfbb130 | 220 | The value should be like the `cdr' of an item in `font-lock-defaults-alist'.") |
b89e1134 SM |
221 | |
222 | (defvar font-lock-defaults-alist | |
9bfbb130 SM |
223 | (let (;; For C and Lisp modes we use `beginning-of-defun', rather than nil, |
224 | ;; for SYNTAX-BEGIN. Thus the calculation of the cache is usually | |
225 | ;; faster but not infallible, so we risk mis-fontification. --sm. | |
fb512de9 | 226 | (c-mode-defaults |
9bfbb130 SM |
227 | '((c-font-lock-keywords c-font-lock-keywords-1 |
228 | c-font-lock-keywords-2 c-font-lock-keywords-3) | |
229 | nil nil ((?_ . "w")) beginning-of-defun)) | |
fb512de9 SM |
230 | (c++-mode-defaults |
231 | '((c++-font-lock-keywords c++-font-lock-keywords-1 | |
9bfbb130 SM |
232 | c++-font-lock-keywords-2 c++-font-lock-keywords-3) |
233 | nil nil ((?_ . "w") (?~ . "w")) beginning-of-defun)) | |
fb512de9 | 234 | (lisp-mode-defaults |
9bfbb130 SM |
235 | '((lisp-font-lock-keywords |
236 | lisp-font-lock-keywords-1 lisp-font-lock-keywords-2) | |
237 | nil nil | |
238 | ((?: . "w") (?- . "w") (?* . "w") (?+ . "w") (?. . "w") (?< . "w") | |
239 | (?> . "w") (?= . "w") (?! . "w") (?? . "w") (?$ . "w") (?% . "w") | |
240 | (?_ . "w") (?& . "w") (?~ . "w") (?^ . "w") (?/ . "w")) | |
d46c21ec SM |
241 | beginning-of-defun)) |
242 | (scheme-mode-defaults | |
9bfbb130 SM |
243 | '(scheme-font-lock-keywords nil t |
244 | ((?: . "w") (?- . "w") (?* . "w") (?+ . "w") (?. . "w") (?< . "w") | |
245 | (?> . "w") (?= . "w") (?! . "w") (?? . "w") (?$ . "w") (?% . "w") | |
246 | (?_ . "w") (?& . "w") (?~ . "w") (?^ . "w") (?/ . "w")) | |
d46c21ec SM |
247 | beginning-of-defun)) |
248 | ;; For TeX modes we could use `backward-paragraph' for the same reason. | |
9bfbb130 | 249 | (tex-mode-defaults '(tex-font-lock-keywords nil nil ((?$ . "\"")))) |
d46c21ec | 250 | ) |
826b2925 | 251 | (list |
d46c21ec SM |
252 | (cons 'bibtex-mode tex-mode-defaults) |
253 | (cons 'c++-c-mode c-mode-defaults) | |
254 | (cons 'c++-mode c++-mode-defaults) | |
255 | (cons 'c-mode c-mode-defaults) | |
9bfbb130 | 256 | (cons 'elec-c-mode c-mode-defaults) |
d46c21ec SM |
257 | (cons 'emacs-lisp-mode lisp-mode-defaults) |
258 | (cons 'inferior-scheme-mode scheme-mode-defaults) | |
259 | (cons 'latex-mode tex-mode-defaults) | |
260 | (cons 'lisp-mode lisp-mode-defaults) | |
261 | (cons 'lisp-interaction-mode lisp-mode-defaults) | |
262 | (cons 'plain-tex-mode tex-mode-defaults) | |
263 | (cons 'scheme-mode scheme-mode-defaults) | |
264 | (cons 'scheme-interaction-mode scheme-mode-defaults) | |
265 | (cons 'slitex-mode tex-mode-defaults) | |
266 | (cons 'tex-mode tex-mode-defaults))) | |
9bfbb130 | 267 | "Alist of default major mode and Font Lock defaults. |
b89e1134 | 268 | Each item should be a list of the form: |
d46c21ec SM |
269 | |
270 | (MAJOR-MODE . (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN)) | |
271 | ||
9bfbb130 SM |
272 | where MAJOR-MODE is a symbol. KEYWORDS may be a symbol (a variable or function |
273 | whose value is the keywords to use for fontification) or a list of symbols. | |
274 | If KEYWORDS-ONLY is non-nil, syntactic fontification (strings and comments) is | |
275 | not performed. If CASE-FOLD is non-nil, the case of the keywords is ignored | |
276 | when fontifying. If SYNTAX-ALIST is non-nil, it should be a list of cons pairs | |
277 | of the form (CHAR . STRING) used to set the local Font Lock syntax table, for | |
278 | keyword and syntactic fontification (see `modify-syntax-entry'). | |
279 | ||
280 | If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move | |
281 | backwards outside any enclosing syntactic block, for syntactic fontification. | |
282 | Typical values are `beginning-of-line' (i.e., the start of the line is known to | |
283 | be outside a syntactic block), or `beginning-of-defun' for programming modes or | |
284 | `backward-paragraph' for textual modes (i.e., the mode-dependent function is | |
285 | known to move outside a syntactic block). If nil, the beginning of the buffer | |
286 | is used as a position outside of a syntactic block, in the worst case. | |
d46c21ec SM |
287 | |
288 | These item elements are used by Font Lock mode to set the variables | |
9bfbb130 | 289 | `font-lock-keywords', `font-lock-keywords-only', |
d46c21ec SM |
290 | `font-lock-keywords-case-fold-search', `font-lock-syntax-table' and |
291 | `font-lock-beginning-of-syntax-function', respectively.") | |
292 | ||
9bfbb130 | 293 | (defvar font-lock-keywords-only nil |
d46c21ec SM |
294 | "*Non-nil means Font Lock should not fontify comments or strings. |
295 | This is normally set via `font-lock-defaults'.") | |
b89e1134 | 296 | |
030f4a35 | 297 | (defvar font-lock-keywords-case-fold-search nil |
d46c21ec SM |
298 | "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive. |
299 | This is normally set via `font-lock-defaults'.") | |
030f4a35 | 300 | |
b056da51 | 301 | (defvar font-lock-syntax-table nil |
799761f0 | 302 | "Non-nil means use this syntax table for fontifying. |
d46c21ec SM |
303 | If this is nil, the major mode's syntax table is used. |
304 | This is normally set via `font-lock-defaults'.") | |
305 | ||
9bfbb130 SM |
306 | ;; If this is nil, we only use the beginning of the buffer if we can't use |
307 | ;; `font-lock-cache-position' and `font-lock-cache-state'. | |
d46c21ec | 308 | (defvar font-lock-beginning-of-syntax-function nil |
9bfbb130 SM |
309 | "*Non-nil means use this function to move back outside of a syntactic block. |
310 | If this is nil, the beginning of the buffer is used (in the worst case). | |
d46c21ec | 311 | This is normally set via `font-lock-defaults'.") |
b056da51 | 312 | |
9bfbb130 SM |
313 | ;; These record the parse state at a particular position, always the start of a |
314 | ;; line. Used to make `font-lock-fontify-syntactically-region' faster. | |
030f4a35 RS |
315 | (defvar font-lock-cache-position nil) |
316 | (defvar font-lock-cache-state nil) | |
317 | (make-variable-buffer-local 'font-lock-cache-position) | |
318 | (make-variable-buffer-local 'font-lock-cache-state) | |
319 | ||
9bfbb130 SM |
320 | (defvar font-lock-mode nil) ; For the modeline. |
321 | (defvar font-lock-fontified nil) ; Whether we have fontified the buffer. | |
322 | (put 'font-lock-fontified 'permanent-local t) | |
826b2925 | 323 | |
9bfbb130 SM |
324 | ;;;###autoload |
325 | (defvar font-lock-mode-hook nil | |
326 | "Function or functions to run on entry to Font Lock mode.") | |
030f4a35 | 327 | \f |
9bfbb130 | 328 | ;; User functions. |
030f4a35 RS |
329 | |
330 | ;;;###autoload | |
331 | (defun font-lock-mode (&optional arg) | |
e595fa35 | 332 | "Toggle Font Lock mode. |
030f4a35 RS |
333 | With arg, turn Font Lock mode on if and only if arg is positive. |
334 | ||
335 | When Font Lock mode is enabled, text is fontified as you type it: | |
336 | ||
a1eb1cf1 RS |
337 | - Comments are displayed in `font-lock-comment-face'; |
338 | - Strings are displayed in `font-lock-string-face'; | |
339 | - Certain other expressions are displayed in other faces according to the | |
340 | value of the variable `font-lock-keywords'. | |
341 | ||
342 | You can enable Font Lock mode in any major mode automatically by turning on in | |
343 | the major mode's hook. For example, put in your ~/.emacs: | |
344 | ||
345 | (add-hook 'c-mode-hook 'turn-on-font-lock) | |
346 | ||
347 | Or for any visited file with the following in your ~/.emacs: | |
348 | ||
349 | (add-hook 'find-file-hooks 'turn-on-font-lock) | |
350 | ||
351 | The default Font Lock mode faces and their attributes are defined in the | |
352 | variable `font-lock-face-attributes', and Font Lock mode default settings in | |
9bfbb130 SM |
353 | the variable `font-lock-defaults-alist'. You can set your own default settings |
354 | for some mode, by setting a buffer local value for `font-lock-defaults', via | |
355 | its mode hook. | |
030f4a35 | 356 | |
826b2925 | 357 | Where modes support different levels of fontification, you can use the variable |
fb512de9 | 358 | `font-lock-maximum-decoration' to specify which level you generally prefer. |
b89e1134 SM |
359 | When you turn Font Lock mode on/off the buffer is fontified/defontified, though |
360 | fontification occurs only if the buffer is less than `font-lock-maximum-size'. | |
7d7d915a SM |
361 | |
362 | To fontify a buffer, without turning on Font Lock mode and regardless of buffer | |
363 | size, you can use \\[font-lock-fontify-buffer]. | |
364 | To fontify a window, perhaps because modification on the current line caused | |
365 | syntactic change on other lines, you can use \\[font-lock-fontify-window]." | |
030f4a35 | 366 | (interactive "P") |
9bfbb130 SM |
367 | (let ((on-p (if arg (> (prefix-numeric-value arg) 0) (not font-lock-mode))) |
368 | (maximum-size (if (not (consp font-lock-maximum-size)) | |
369 | font-lock-maximum-size | |
370 | (cdr (or (assq major-mode font-lock-maximum-size) | |
371 | (assq t font-lock-maximum-size)))))) | |
7d7d915a SM |
372 | ;; Don't turn on Font Lock mode if we don't have a display (we're running a |
373 | ;; batch job) or if the buffer is invisible (the name starts with a space). | |
374 | (if (or noninteractive (eq (aref (buffer-name) 0) ?\ )) | |
030f4a35 | 375 | (setq on-p nil)) |
a1eb1cf1 | 376 | (if (not on-p) |
876f2438 SM |
377 | (remove-hook 'after-change-functions 'font-lock-after-change-function |
378 | t) | |
379 | (make-local-hook 'after-change-functions) | |
380 | (add-hook 'after-change-functions 'font-lock-after-change-function | |
381 | nil t)) | |
030f4a35 RS |
382 | (set (make-local-variable 'font-lock-mode) on-p) |
383 | (cond (on-p | |
384 | (font-lock-set-defaults) | |
876f2438 SM |
385 | (make-local-hook 'before-revert-hook) |
386 | (make-local-hook 'after-revert-hook) | |
7daa8d6b | 387 | ;; If buffer is reverted, must clean up the state. |
876f2438 SM |
388 | (add-hook 'before-revert-hook 'font-lock-revert-setup nil t) |
389 | (add-hook 'after-revert-hook 'font-lock-revert-cleanup nil t) | |
030f4a35 | 390 | (run-hooks 'font-lock-mode-hook) |
b89e1134 SM |
391 | (cond (font-lock-fontified |
392 | nil) | |
9bfbb130 | 393 | ((or (null maximum-size) (<= (buffer-size) maximum-size)) |
b89e1134 SM |
394 | (font-lock-fontify-buffer)) |
395 | (font-lock-verbose | |
396 | (message "Fontifying %s... buffer too big." (buffer-name))))) | |
030f4a35 RS |
397 | (font-lock-fontified |
398 | (setq font-lock-fontified nil) | |
876f2438 SM |
399 | (remove-hook 'before-revert-hook 'font-lock-revert-setup t) |
400 | (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t) | |
7d7d915a SM |
401 | ;; Make sure we unfontify etc. in the whole buffer. |
402 | (save-restriction | |
403 | (widen) | |
404 | (font-lock-unfontify-region (point-min) (point-max)) | |
405 | (font-lock-thing-lock-cleanup))) | |
799761f0 | 406 | (t |
876f2438 SM |
407 | (remove-hook 'before-revert-hook 'font-lock-revert-setup t) |
408 | (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t) | |
799761f0 | 409 | (font-lock-thing-lock-cleanup))) |
030f4a35 RS |
410 | (force-mode-line-update))) |
411 | ||
a1eb1cf1 RS |
412 | ;;;###autoload |
413 | (defun turn-on-font-lock () | |
2346238d RS |
414 | "Turn on Font Lock mode, if the terminal can display it." |
415 | (if window-system | |
416 | (font-lock-mode 1))) | |
a1eb1cf1 | 417 | |
a1eb1cf1 | 418 | ;;;###autoload |
030f4a35 | 419 | (defun font-lock-fontify-buffer () |
a1eb1cf1 | 420 | "Fontify the current buffer the way `font-lock-mode' would." |
030f4a35 | 421 | (interactive) |
9bfbb130 | 422 | (let ((verbose (and (or font-lock-verbose (interactive-p)) |
876f2438 | 423 | (not (zerop (buffer-size)))))) |
b89e1134 | 424 | (set (make-local-variable 'font-lock-fontified) nil) |
a1eb1cf1 | 425 | (if verbose (message "Fontifying %s..." (buffer-name))) |
7d7d915a SM |
426 | ;; Make sure we have the right `font-lock-keywords' etc. |
427 | (if (not font-lock-mode) (font-lock-set-defaults)) | |
428 | ;; Make sure we fontify etc. in the whole buffer. | |
429 | (save-restriction | |
430 | (widen) | |
431 | (condition-case nil | |
432 | (save-excursion | |
433 | (save-match-data | |
434 | (font-lock-fontify-region (point-min) (point-max) verbose) | |
435 | (setq font-lock-fontified t))) | |
436 | ;; We don't restore the old fontification, so it's best to unfontify. | |
437 | (quit (font-lock-unfontify-region (point-min) (point-max)))) | |
438 | (if verbose (message "Fontifying %s... %s." (buffer-name) | |
439 | (if font-lock-fontified "done" "aborted"))) | |
440 | (font-lock-after-fontify-buffer)))) | |
441 | ||
442 | (defun font-lock-fontify-window () | |
443 | "Fontify the current window the way `font-lock-mode' would." | |
444 | (interactive) | |
445 | (let ((font-lock-beginning-of-syntax-function nil)) | |
446 | (save-excursion | |
447 | (save-match-data | |
448 | (font-lock-fontify-region (window-start) (window-end)))))) | |
449 | ||
450 | (define-key global-map [?\C-\S-l] 'font-lock-fontify-window) | |
9bfbb130 SM |
451 | \f |
452 | ;; Fontification functions. | |
453 | ||
454 | ;; We use this wrapper. However, `font-lock-fontify-region' used to be the | |
455 | ;; name used for `font-lock-fontify-syntactically-region', so a change isn't | |
456 | ;; back-compatible. But you shouldn't be calling these directly, should you? | |
457 | (defun font-lock-fontify-region (beg end &optional loudly) | |
876f2438 SM |
458 | (let ((modified (buffer-modified-p)) |
459 | (buffer-undo-list t) (inhibit-read-only t) | |
460 | (old-syntax-table (syntax-table)) | |
7d7d915a | 461 | before-change-functions after-change-functions |
876f2438 SM |
462 | buffer-file-name buffer-file-truename) |
463 | (unwind-protect | |
464 | (progn | |
465 | ;; Use the fontification syntax table, if any. | |
466 | (if font-lock-syntax-table (set-syntax-table font-lock-syntax-table)) | |
467 | ;; Now do the fontification. | |
468 | (if font-lock-keywords-only | |
469 | (font-lock-unfontify-region beg end) | |
470 | (font-lock-fontify-syntactically-region beg end loudly)) | |
471 | (font-lock-fontify-keywords-region beg end loudly)) | |
472 | ;; Clean up. | |
473 | (set-syntax-table old-syntax-table) | |
474 | (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil))))) | |
9bfbb130 SM |
475 | |
476 | ;; The following must be rethought, since keywords can override fontification. | |
477 | ; ;; Now scan for keywords, but not if we are inside a comment now. | |
478 | ; (or (and (not font-lock-keywords-only) | |
479 | ; (let ((state (parse-partial-sexp beg end nil nil | |
480 | ; font-lock-cache-state))) | |
481 | ; (or (nth 4 state) (nth 7 state)))) | |
482 | ; (font-lock-fontify-keywords-region beg end)) | |
483 | ||
484 | (defun font-lock-unfontify-region (beg end) | |
485 | (let ((modified (buffer-modified-p)) | |
486 | (buffer-undo-list t) (inhibit-read-only t) | |
7d7d915a | 487 | before-change-functions after-change-functions |
9bfbb130 SM |
488 | buffer-file-name buffer-file-truename) |
489 | (remove-text-properties beg end '(face nil)) | |
876f2438 | 490 | (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))) |
9bfbb130 SM |
491 | |
492 | ;; Called when any modification is made to buffer text. | |
493 | (defun font-lock-after-change-function (beg end old-len) | |
494 | (save-excursion | |
495 | (save-match-data | |
496 | ;; Rescan between start of line from `beg' and start of line after `end'. | |
497 | (font-lock-fontify-region | |
498 | (progn (goto-char beg) (beginning-of-line) (point)) | |
499 | (progn (goto-char end) (forward-line 1) (point)))))) | |
500 | \f | |
501 | ;; Syntactic fontification functions. | |
502 | ||
503 | (defun font-lock-fontify-syntactically-region (start end &optional loudly) | |
504 | "Put proper face on each string and comment between START and END. | |
505 | START should be at the beginning of a line." | |
876f2438 | 506 | (let ((synstart (if comment-start-skip |
9bfbb130 SM |
507 | (concat "\\s\"\\|" comment-start-skip) |
508 | "\\s\"")) | |
509 | (comstart (if comment-start-skip | |
510 | (concat "\\s<\\|" comment-start-skip) | |
511 | "\\s<")) | |
9bfbb130 SM |
512 | state prev prevstate) |
513 | (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name))) | |
876f2438 SM |
514 | (save-restriction |
515 | (widen) | |
516 | (goto-char start) | |
517 | ;; | |
518 | ;; Find the state at the `beginning-of-line' before `start'. | |
519 | (if (eq start font-lock-cache-position) | |
520 | ;; Use the cache for the state of `start'. | |
521 | (setq state font-lock-cache-state) | |
522 | ;; Find the state of `start'. | |
523 | (if (null font-lock-beginning-of-syntax-function) | |
524 | ;; Use the state at the previous cache position, if any, or | |
525 | ;; otherwise calculate from `point-min'. | |
526 | (if (or (null font-lock-cache-position) | |
527 | (< start font-lock-cache-position)) | |
528 | (setq state (parse-partial-sexp (point-min) start)) | |
529 | (setq state (parse-partial-sexp font-lock-cache-position start | |
530 | nil nil font-lock-cache-state))) | |
531 | ;; Call the function to move outside any syntactic block. | |
532 | (funcall font-lock-beginning-of-syntax-function) | |
533 | (setq state (parse-partial-sexp (point) start))) | |
534 | ;; Cache the state and position of `start'. | |
535 | (setq font-lock-cache-state state | |
536 | font-lock-cache-position start)) | |
537 | ;; | |
538 | ;; If the region starts inside a string, show the extent of it. | |
539 | (if (nth 3 state) | |
540 | (let ((beg (point))) | |
541 | (while (and (re-search-forward "\\s\"" end 'move) | |
542 | (nth 3 (parse-partial-sexp beg (point) | |
543 | nil nil state)))) | |
544 | (put-text-property beg (point) 'face font-lock-string-face) | |
545 | (setq state (parse-partial-sexp beg (point) nil nil state)))) | |
546 | ;; | |
547 | ;; Likewise for a comment. | |
548 | (if (or (nth 4 state) (nth 7 state)) | |
549 | (let ((beg (point))) | |
550 | (save-restriction | |
551 | (narrow-to-region (point-min) end) | |
552 | (condition-case nil | |
553 | (progn | |
554 | (re-search-backward comstart (point-min) 'move) | |
555 | (forward-comment 1) | |
556 | ;; forward-comment skips all whitespace, | |
557 | ;; so go back to the real end of the comment. | |
558 | (skip-chars-backward " \t")) | |
559 | (error (goto-char end)))) | |
560 | (put-text-property beg (point) 'face font-lock-comment-face) | |
561 | (setq state (parse-partial-sexp beg (point) nil nil state)))) | |
562 | ;; | |
563 | ;; Find each interesting place between here and `end'. | |
564 | (while (and (< (point) end) | |
565 | (setq prev (point) prevstate state) | |
566 | (re-search-forward synstart end t) | |
567 | (progn | |
568 | ;; Clear out the fonts of what we skip over. | |
569 | (remove-text-properties prev (point) '(face nil)) | |
570 | ;; Verify the state at that place | |
571 | ;; so we don't get fooled by \" or \;. | |
572 | (setq state (parse-partial-sexp prev (point) | |
573 | nil nil state)))) | |
574 | (let ((here (point))) | |
575 | (if (or (nth 4 state) (nth 7 state)) | |
576 | ;; | |
577 | ;; We found a real comment start. | |
578 | (let ((beg (match-beginning 0))) | |
579 | (goto-char beg) | |
580 | (save-restriction | |
581 | (narrow-to-region (point-min) end) | |
582 | (condition-case nil | |
583 | (progn | |
584 | (forward-comment 1) | |
585 | ;; forward-comment skips all whitespace, | |
586 | ;; so go back to the real end of the comment. | |
587 | (skip-chars-backward " \t")) | |
588 | (error (goto-char end)))) | |
589 | (put-text-property beg (point) 'face | |
590 | font-lock-comment-face) | |
591 | (setq state (parse-partial-sexp here (point) nil nil state))) | |
592 | (if (nth 3 state) | |
9bfbb130 | 593 | ;; |
876f2438 | 594 | ;; We found a real string start. |
9bfbb130 | 595 | (let ((beg (match-beginning 0))) |
876f2438 SM |
596 | (while (and (re-search-forward "\\s\"" end 'move) |
597 | (nth 3 (parse-partial-sexp here (point) | |
598 | nil nil state)))) | |
599 | (put-text-property beg (point) 'face font-lock-string-face) | |
600 | (setq state (parse-partial-sexp here (point) | |
601 | nil nil state)))))) | |
602 | ;; | |
603 | ;; Make sure `prev' is non-nil after the loop | |
604 | ;; only if it was set on the very last iteration. | |
605 | (setq prev nil))) | |
606 | ;; | |
607 | ;; Clean up. | |
608 | (and prev (remove-text-properties prev end '(face nil))))) | |
9bfbb130 SM |
609 | \f |
610 | ;;; Additional text property functions. | |
611 | ||
612 | ;; The following three text property functions are not generally available (and | |
613 | ;; it's not certain that they should be) so they are inlined for speed. | |
614 | ;; The case for `fillin-text-property' is simple; it may or not be generally | |
615 | ;; useful. (Since it is used here, it is useful in at least one place.;-) | |
616 | ;; However, the case for `append-text-property' and `prepend-text-property' is | |
617 | ;; more complicated. Should they remove duplicate property values or not? If | |
618 | ;; so, should the first or last duplicate item remain? Or the one that was | |
619 | ;; added? In our implementation, the first duplicate remains. | |
620 | ||
621 | (defsubst font-lock-fillin-text-property (start end prop value &optional object) | |
622 | "Fill in one property of the text from START to END. | |
623 | Arguments PROP and VALUE specify the property and value to put where none are | |
624 | already in place. Therefore existing property values are not overwritten. | |
625 | Optional argument OBJECT is the string or buffer containing the text." | |
626 | (let ((start (text-property-any start end prop nil object)) next) | |
627 | (while start | |
628 | (setq next (next-single-property-change start prop object end)) | |
629 | (put-text-property start next prop value object) | |
630 | (setq start (text-property-any next end prop nil object))))) | |
631 | ||
632 | ;; This function (from simon's unique.el) is rewritten and inlined for speed. | |
633 | ;(defun unique (list function) | |
634 | ; "Uniquify LIST, deleting elements using FUNCTION. | |
635 | ;Return the list with subsequent duplicate items removed by side effects. | |
636 | ;FUNCTION is called with an element of LIST and a list of elements from LIST, | |
637 | ;and should return the list of elements with occurrences of the element removed, | |
638 | ;i.e., a function such as `delete' or `delq'. | |
639 | ;This function will work even if LIST is unsorted. See also `uniq'." | |
640 | ; (let ((list list)) | |
641 | ; (while list | |
642 | ; (setq list (setcdr list (funcall function (car list) (cdr list)))))) | |
643 | ; list) | |
644 | ||
645 | (defsubst font-lock-unique (list) | |
646 | "Uniquify LIST, deleting elements using `delq'. | |
647 | Return the list with subsequent duplicate items removed by side effects." | |
648 | (let ((list list)) | |
649 | (while list | |
650 | (setq list (setcdr list (delq (car list) (cdr list)))))) | |
651 | list) | |
652 | ||
653 | ;; A generalisation of `facemenu-add-face' for any property, but without the | |
654 | ;; removal of inactive faces via `facemenu-discard-redundant-faces' and special | |
655 | ;; treatment of `default'. Uses `unique' to remove duplicate property values. | |
656 | (defsubst font-lock-prepend-text-property (start end prop value &optional object) | |
657 | "Prepend to one property of the text from START to END. | |
658 | Arguments PROP and VALUE specify the property and value to prepend to the value | |
659 | already in place. The resulting property values are always lists, and unique. | |
660 | Optional argument OBJECT is the string or buffer containing the text." | |
661 | (let ((val (if (listp value) value (list value))) next prev) | |
662 | (while (/= start end) | |
663 | (setq next (next-single-property-change start prop object end) | |
664 | prev (get-text-property start prop object)) | |
665 | (put-text-property | |
666 | start next prop | |
667 | (font-lock-unique (append val (if (listp prev) prev (list prev)))) | |
668 | object) | |
669 | (setq start next)))) | |
670 | ||
671 | (defsubst font-lock-append-text-property (start end prop value &optional object) | |
672 | "Append to one property of the text from START to END. | |
673 | Arguments PROP and VALUE specify the property and value to append to the value | |
674 | already in place. The resulting property values are always lists, and unique. | |
675 | Optional argument OBJECT is the string or buffer containing the text." | |
676 | (let ((val (if (listp value) value (list value))) next prev) | |
677 | (while (/= start end) | |
678 | (setq next (next-single-property-change start prop object end) | |
679 | prev (get-text-property start prop object)) | |
680 | (put-text-property | |
681 | start next prop | |
682 | (font-lock-unique (append (if (listp prev) prev (list prev)) val)) | |
683 | object) | |
684 | (setq start next)))) | |
685 | \f | |
686 | ;;; Regexp fontification functions. | |
687 | ||
688 | (defsubst font-lock-apply-highlight (highlight) | |
689 | "Apply HIGHLIGHT following a match. | |
690 | HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'." | |
691 | (let* ((match (nth 0 highlight)) | |
692 | (start (match-beginning match)) (end (match-end match)) | |
693 | (override (nth 2 highlight))) | |
694 | (cond ((not start) | |
695 | ;; No match but we might not signal an error. | |
696 | (or (nth 3 highlight) | |
697 | (error "No match %d in highlight %S" match highlight))) | |
698 | ((not override) | |
699 | ;; Cannot override existing fontification. | |
700 | (or (text-property-not-all start end 'face nil) | |
701 | (put-text-property start end 'face (eval (nth 1 highlight))))) | |
702 | ((eq override t) | |
703 | ;; Override existing fontification. | |
704 | (put-text-property start end 'face (eval (nth 1 highlight)))) | |
705 | ((eq override 'keep) | |
706 | ;; Keep existing fontification. | |
707 | (font-lock-fillin-text-property start end 'face | |
708 | (eval (nth 1 highlight)))) | |
709 | ((eq override 'prepend) | |
710 | ;; Prepend to existing fontification. | |
711 | (font-lock-prepend-text-property start end 'face | |
712 | (eval (nth 1 highlight)))) | |
713 | ((eq override 'append) | |
714 | ;; Append to existing fontification. | |
715 | (font-lock-append-text-property start end 'face | |
716 | (eval (nth 1 highlight))))))) | |
717 | ||
718 | (defsubst font-lock-fontify-anchored-keywords (keywords limit) | |
719 | "Fontify according to KEYWORDS until LIMIT. | |
720 | KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords'." | |
721 | (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights) | |
876f2438 SM |
722 | ;; Until we come up with a cleaner solution, we make LIMIT the end of line. |
723 | (save-excursion (end-of-line) (setq limit (min limit (point)))) | |
724 | ;; Evaluate PRE-MATCH-FORM. | |
9bfbb130 SM |
725 | (eval (nth 1 keywords)) |
726 | (save-match-data | |
876f2438 | 727 | ;; Find an occurrence of `matcher' before `limit'. |
9bfbb130 SM |
728 | (while (if (stringp matcher) |
729 | (re-search-forward matcher limit t) | |
730 | (funcall matcher limit)) | |
876f2438 | 731 | ;; Apply each highlight to this instance of `matcher'. |
9bfbb130 SM |
732 | (setq highlights lowdarks) |
733 | (while highlights | |
734 | (font-lock-apply-highlight (car highlights)) | |
735 | (setq highlights (cdr highlights))))) | |
876f2438 | 736 | ;; Evaluate POST-MATCH-FORM. |
9bfbb130 SM |
737 | (eval (nth 2 keywords)))) |
738 | ||
739 | (defun font-lock-fontify-keywords-region (start end &optional loudly) | |
740 | "Fontify according to `font-lock-keywords' between START and END. | |
741 | START should be at the beginning of a line." | |
742 | (let ((case-fold-search font-lock-keywords-case-fold-search) | |
743 | (keywords (cdr (if (eq (car-safe font-lock-keywords) t) | |
744 | font-lock-keywords | |
745 | (font-lock-compile-keywords)))) | |
9bfbb130 | 746 | (bufname (buffer-name)) (count 0) |
876f2438 SM |
747 | keyword matcher highlights) |
748 | ;; | |
749 | ;; Fontify each item in `font-lock-keywords' from `start' to `end'. | |
750 | (while keywords | |
751 | (if loudly (message "Fontifying %s... (regexps..%s)" bufname | |
752 | (make-string (setq count (1+ count)) ?.))) | |
9bfbb130 | 753 | ;; |
876f2438 SM |
754 | ;; Find an occurrence of `matcher' from `start' to `end'. |
755 | (setq keyword (car keywords) matcher (car keyword)) | |
756 | (goto-char start) | |
757 | (while (if (stringp matcher) | |
758 | (re-search-forward matcher end t) | |
759 | (funcall matcher end)) | |
760 | ;; Apply each highlight to this instance of `matcher', which may be | |
761 | ;; specific highlights or more keywords anchored to `matcher'. | |
762 | (setq highlights (cdr keyword)) | |
763 | (while highlights | |
764 | (if (numberp (car (car highlights))) | |
765 | (font-lock-apply-highlight (car highlights)) | |
766 | (font-lock-fontify-anchored-keywords (car highlights) end)) | |
767 | (setq highlights (cdr highlights)))) | |
768 | (setq keywords (cdr keywords))))) | |
9bfbb130 SM |
769 | \f |
770 | ;; Various functions. | |
771 | ||
772 | ;; Turn off other related packages if they're on. I prefer a hook. --sm. | |
773 | ;; These explicit calls are easier to understand | |
774 | ;; because people know what they will do. | |
775 | ;; A hook is a mystery because it might do anything whatever. --rms. | |
776 | (defun font-lock-thing-lock-cleanup () | |
777 | (cond ((and (boundp 'fast-lock-mode) fast-lock-mode) | |
778 | (fast-lock-mode -1)) | |
779 | ((and (boundp 'lazy-lock-mode) lazy-lock-mode) | |
780 | (lazy-lock-mode -1)))) | |
781 | ||
782 | ;; Do something special for these packages after fontifying. I prefer a hook. | |
783 | (defun font-lock-after-fontify-buffer () | |
784 | (cond ((and (boundp 'fast-lock-mode) fast-lock-mode) | |
785 | (fast-lock-after-fontify-buffer)) | |
786 | ((and (boundp 'lazy-lock-mode) lazy-lock-mode) | |
787 | (lazy-lock-after-fontify-buffer)))) | |
788 | ||
789 | ;; If the buffer is about to be reverted, it won't be fontified afterward. | |
790 | (defun font-lock-revert-setup () | |
791 | (setq font-lock-fontified nil)) | |
792 | ||
793 | ;; If the buffer has just been reverted, normally that turns off | |
794 | ;; Font Lock mode. So turn the mode back on if necessary. | |
7d7d915a SM |
795 | (defalias 'font-lock-revert-cleanup |
796 | 'turn-on-font-lock) | |
9bfbb130 SM |
797 | |
798 | (defun font-lock-compile-keywords (&optional keywords) | |
799 | ;; Compile `font-lock-keywords' into the form (t KEYWORD ...) where KEYWORD | |
800 | ;; is the (MATCHER HIGHLIGHT ...) shown in the variable's doc string. | |
801 | (let ((keywords (or keywords font-lock-keywords))) | |
802 | (setq font-lock-keywords | |
803 | (if (eq (car-safe keywords) t) | |
804 | keywords | |
805 | (cons t | |
806 | (mapcar | |
807 | (function (lambda (item) | |
808 | (cond ((nlistp item) | |
809 | (list item '(0 font-lock-keyword-face))) | |
810 | ((numberp (cdr item)) | |
811 | (list (car item) (list (cdr item) 'font-lock-keyword-face))) | |
812 | ((symbolp (cdr item)) | |
813 | (list (car item) (list 0 (cdr item)))) | |
814 | ((nlistp (nth 1 item)) | |
815 | (list (car item) (cdr item))) | |
816 | (t | |
817 | item)))) | |
818 | keywords)))))) | |
d46c21ec SM |
819 | |
820 | (defun font-lock-choose-keywords (keywords level) | |
821 | ;; Return LEVELth element of KEYWORDS. A LEVEL of nil is equal to a | |
822 | ;; LEVEL of 0, a LEVEL of t is equal to (1- (length KEYWORDS)). | |
9bfbb130 SM |
823 | (let ((level (if (not (consp level)) |
824 | level | |
825 | (cdr (or (assq major-mode level) (assq t level)))))) | |
826 | (cond ((symbolp keywords) | |
827 | keywords) | |
828 | ((numberp level) | |
829 | (or (nth level keywords) (car (reverse keywords)))) | |
830 | ((eq level t) | |
831 | (car (reverse keywords))) | |
832 | (t | |
833 | (car keywords))))) | |
d46c21ec SM |
834 | |
835 | (defun font-lock-set-defaults () | |
836 | "Set fontification defaults appropriately for this mode. | |
9bfbb130 SM |
837 | Sets `font-lock-keywords', `font-lock-keywords-only', `font-lock-syntax-table', |
838 | `font-lock-beginning-of-syntax-function' and | |
839 | `font-lock-keywords-case-fold-search' using `font-lock-defaults' (or, if nil, | |
840 | using `font-lock-defaults-alist') and `font-lock-maximum-decoration'." | |
d46c21ec SM |
841 | ;; Set face defaults. |
842 | (font-lock-make-faces) | |
843 | ;; Set fontification defaults. | |
844 | (or font-lock-keywords | |
9bfbb130 SM |
845 | (let* ((defaults (or font-lock-defaults |
846 | (cdr (assq major-mode font-lock-defaults-alist)))) | |
847 | (keywords (font-lock-choose-keywords | |
848 | (nth 0 defaults) font-lock-maximum-decoration))) | |
7d7d915a | 849 | ;; Regexp fontification? |
9bfbb130 SM |
850 | (setq font-lock-keywords (if (fboundp keywords) |
851 | (funcall keywords) | |
852 | (eval keywords))) | |
7d7d915a | 853 | ;; Syntactic fontification? |
d46c21ec | 854 | (if (nth 1 defaults) |
9bfbb130 | 855 | (set (make-local-variable 'font-lock-keywords-only) t)) |
7d7d915a | 856 | ;; Case fold during regexp fontification? |
d46c21ec SM |
857 | (if (nth 2 defaults) |
858 | (set (make-local-variable 'font-lock-keywords-case-fold-search) t)) | |
7d7d915a | 859 | ;; Syntax table for regexp and syntactic fontification? |
d46c21ec SM |
860 | (if (nth 3 defaults) |
861 | (let ((slist (nth 3 defaults))) | |
862 | (set (make-local-variable 'font-lock-syntax-table) | |
863 | (copy-syntax-table (syntax-table))) | |
864 | (while slist | |
865 | (modify-syntax-entry (car (car slist)) (cdr (car slist)) | |
866 | font-lock-syntax-table) | |
867 | (setq slist (cdr slist))))) | |
7d7d915a | 868 | ;; Syntax function for syntactic fontification? |
d46c21ec SM |
869 | (if (nth 4 defaults) |
870 | (set (make-local-variable 'font-lock-beginning-of-syntax-function) | |
871 | (nth 4 defaults)))))) | |
030f4a35 | 872 | \f |
9bfbb130 SM |
873 | ;; Colour etc. support. |
874 | ||
875 | (defvar font-lock-display-type nil | |
876 | "A symbol indicating the display Emacs is running under. | |
877 | The symbol should be one of `color', `grayscale' or `mono'. | |
878 | If Emacs guesses this display attribute wrongly, either set this variable in | |
879 | your `~/.emacs' or set the resource `Emacs.displayType' in your `~/.Xdefaults'. | |
880 | See also `font-lock-background-mode' and `font-lock-face-attributes'.") | |
881 | ||
882 | (defvar font-lock-background-mode nil | |
883 | "A symbol indicating the Emacs background brightness. | |
884 | The symbol should be one of `light' or `dark'. | |
885 | If Emacs guesses this frame attribute wrongly, either set this variable in | |
886 | your `~/.emacs' or set the resource `Emacs.backgroundMode' in your | |
887 | `~/.Xdefaults'. | |
888 | See also `font-lock-display-type' and `font-lock-face-attributes'.") | |
889 | ||
890 | (defvar font-lock-face-attributes nil | |
891 | "A list of default attributes to use for face attributes. | |
892 | Each element of the list should be of the form | |
893 | ||
894 | (FACE FOREGROUND BACKGROUND BOLD-P ITALIC-P UNDERLINE-P) | |
895 | ||
896 | where FACE should be one of the face symbols `font-lock-comment-face', | |
897 | `font-lock-string-face', `font-lock-keyword-face', `font-lock-type-face', | |
898 | `font-lock-function-name-face', `font-lock-variable-name-face', and | |
899 | `font-lock-reference-face'. A form for each of these face symbols should be | |
900 | provided in the list, but other face symbols and attributes may be given and | |
901 | used in highlighting. See `font-lock-keywords'. | |
902 | ||
903 | Subsequent element items should be the attributes for the corresponding | |
904 | Font Lock mode faces. Attributes FOREGROUND and BACKGROUND should be strings | |
905 | \(default if nil), while BOLD-P, ITALIC-P, and UNDERLINE-P should specify the | |
906 | corresponding face attributes (yes if non-nil). | |
907 | ||
908 | Emacs uses default attributes based on display type and background brightness. | |
909 | See variables `font-lock-display-type' and `font-lock-background-mode'. | |
910 | ||
911 | Resources can be used to over-ride these face attributes. For example, the | |
912 | resource `Emacs.font-lock-comment-face.attributeUnderline' can be used to | |
913 | specify the UNDERLINE-P attribute for face `font-lock-comment-face'.") | |
914 | ||
915 | (defun font-lock-make-faces (&optional override) | |
916 | "Make faces from `font-lock-face-attributes'. | |
917 | A default list is used if this is nil. | |
918 | If optional OVERRIDE is non-nil, faces that already exist are reset. | |
919 | See `font-lock-make-face' and `list-faces-display'." | |
920 | ;; We don't need to `setq' any of these variables, but the user can see what | |
921 | ;; is being used if we do. | |
922 | (if (null font-lock-display-type) | |
923 | (setq font-lock-display-type | |
924 | (let ((display-resource (x-get-resource ".displayType" | |
925 | "DisplayType"))) | |
926 | (cond (display-resource (intern (downcase display-resource))) | |
927 | ((x-display-color-p) 'color) | |
928 | ((x-display-grayscale-p) 'grayscale) | |
929 | (t 'mono))))) | |
930 | (if (null font-lock-background-mode) | |
931 | (setq font-lock-background-mode | |
932 | (let ((bg-resource (x-get-resource ".backgroundMode" | |
933 | "BackgroundMode")) | |
934 | (params (frame-parameters))) | |
935 | (cond (bg-resource (intern (downcase bg-resource))) | |
03a84c34 RS |
936 | ((eq system-type 'ms-dos) |
937 | (if (string-match "light" | |
938 | (cdr (assq 'background-color params))) | |
939 | 'light | |
940 | 'dark)) | |
9bfbb130 SM |
941 | ((< (apply '+ (x-color-values |
942 | (cdr (assq 'background-color params)))) | |
943 | (/ (apply '+ (x-color-values "white")) 3)) | |
944 | 'dark) | |
945 | (t 'light))))) | |
946 | (if (null font-lock-face-attributes) | |
947 | (setq font-lock-face-attributes | |
948 | (let ((light-bg (eq font-lock-background-mode 'light))) | |
949 | (cond ((memq font-lock-display-type '(mono monochrome)) | |
950 | ;; Emacs 19.25's font-lock defaults: | |
951 | ;;'((font-lock-comment-face nil nil nil t nil) | |
952 | ;; (font-lock-string-face nil nil nil nil t) | |
953 | ;; (font-lock-keyword-face nil nil t nil nil) | |
954 | ;; (font-lock-function-name-face nil nil t t nil) | |
955 | ;; (font-lock-type-face nil nil nil t nil)) | |
956 | (list '(font-lock-comment-face nil nil t t nil) | |
957 | '(font-lock-string-face nil nil nil t nil) | |
958 | '(font-lock-keyword-face nil nil t nil nil) | |
959 | (list | |
960 | 'font-lock-function-name-face | |
961 | (cdr (assq 'background-color (frame-parameters))) | |
962 | (cdr (assq 'foreground-color (frame-parameters))) | |
963 | t nil nil) | |
964 | '(font-lock-variable-name-face nil nil t t nil) | |
965 | '(font-lock-type-face nil nil t nil t) | |
966 | '(font-lock-reference-face nil nil t nil t))) | |
967 | ((memq font-lock-display-type '(grayscale greyscale | |
968 | grayshade greyshade)) | |
969 | (list | |
970 | (list 'font-lock-comment-face | |
971 | nil (if light-bg "Gray80" "DimGray") t t nil) | |
972 | (list 'font-lock-string-face | |
973 | nil (if light-bg "Gray50" "LightGray") nil t nil) | |
974 | (list 'font-lock-keyword-face | |
975 | nil (if light-bg "Gray90" "DimGray") t nil nil) | |
976 | (list 'font-lock-function-name-face | |
977 | (cdr (assq 'background-color (frame-parameters))) | |
978 | (cdr (assq 'foreground-color (frame-parameters))) | |
979 | t nil nil) | |
980 | (list 'font-lock-variable-name-face | |
981 | nil (if light-bg "Gray90" "DimGray") t t nil) | |
982 | (list 'font-lock-type-face | |
983 | nil (if light-bg "Gray80" "DimGray") t nil t) | |
984 | (list 'font-lock-reference-face | |
985 | nil (if light-bg "LightGray" "Gray50") t nil t))) | |
986 | (light-bg ; light colour background | |
987 | '((font-lock-comment-face "Firebrick") | |
988 | (font-lock-string-face "RosyBrown") | |
989 | (font-lock-keyword-face "Purple") | |
990 | (font-lock-function-name-face "Blue") | |
991 | (font-lock-variable-name-face "DarkGoldenrod") | |
992 | (font-lock-type-face "DarkOliveGreen") | |
993 | (font-lock-reference-face "CadetBlue"))) | |
994 | (t ; dark colour background | |
995 | '((font-lock-comment-face "OrangeRed") | |
996 | (font-lock-string-face "LightSalmon") | |
997 | (font-lock-keyword-face "LightSteelBlue") | |
998 | (font-lock-function-name-face "LightSkyBlue") | |
999 | (font-lock-variable-name-face "LightGoldenrod") | |
1000 | (font-lock-type-face "PaleGreen") | |
1001 | (font-lock-reference-face "Aquamarine"))))))) | |
1002 | ;; Now make the faces if we have to. | |
1003 | (mapcar (function | |
1004 | (lambda (face-attributes) | |
1005 | (let ((face (nth 0 face-attributes))) | |
1006 | (cond (override | |
1007 | ;; We can stomp all over it anyway. Get outta my face! | |
1008 | (font-lock-make-face face-attributes)) | |
1009 | ((and (boundp face) (facep (symbol-value face))) | |
1010 | ;; The variable exists and is already bound to a face. | |
1011 | nil) | |
1012 | ((facep face) | |
1013 | ;; We already have a face so we bind the variable to it. | |
1014 | (set face face)) | |
1015 | (t | |
1016 | ;; No variable or no face. | |
1017 | (font-lock-make-face face-attributes)))))) | |
1018 | font-lock-face-attributes)) | |
1019 | ||
1020 | (defun font-lock-make-face (face-attributes) | |
1021 | "Make a face from FACE-ATTRIBUTES. | |
1022 | FACE-ATTRIBUTES should be like an element `font-lock-face-attributes', so that | |
1023 | the face name is the first item in the list. A variable with the same name as | |
1024 | the face is also set; its value is the face name." | |
1025 | (let* ((face (nth 0 face-attributes)) | |
1026 | (face-name (symbol-name face)) | |
1027 | (set-p (function (lambda (face-name resource) | |
1028 | (x-get-resource (concat face-name ".attribute" resource) | |
1029 | (concat "Face.Attribute" resource))))) | |
1030 | (on-p (function (lambda (face-name resource) | |
1031 | (let ((set (funcall set-p face-name resource))) | |
1032 | (and set (member (downcase set) '("on" "true")))))))) | |
1033 | (make-face face) | |
876f2438 | 1034 | (add-to-list 'facemenu-unlisted-faces face) |
9bfbb130 SM |
1035 | ;; Set attributes not set from X resources (and therefore `make-face'). |
1036 | (or (funcall set-p face-name "Foreground") | |
1037 | (condition-case nil | |
1038 | (set-face-foreground face (nth 1 face-attributes)) | |
1039 | (error nil))) | |
1040 | (or (funcall set-p face-name "Background") | |
1041 | (condition-case nil | |
1042 | (set-face-background face (nth 2 face-attributes)) | |
1043 | (error nil))) | |
1044 | (if (funcall set-p face-name "Bold") | |
1045 | (and (funcall on-p face-name "Bold") (make-face-bold face nil t)) | |
1046 | (and (nth 3 face-attributes) (make-face-bold face nil t))) | |
1047 | (if (funcall set-p face-name "Italic") | |
1048 | (and (funcall on-p face-name "Italic") (make-face-italic face nil t)) | |
1049 | (and (nth 4 face-attributes) (make-face-italic face nil t))) | |
1050 | (or (funcall set-p face-name "Underline") | |
1051 | (set-face-underline-p face (nth 5 face-attributes))) | |
1052 | (set face face))) | |
1053 | \f | |
1054 | ;;; Various regexp information shared by several modes. | |
a1eb1cf1 | 1055 | ;;; Information specific to a single mode should go in its load library. |
030f4a35 | 1056 | |
030f4a35 | 1057 | (defconst lisp-font-lock-keywords-1 |
a1eb1cf1 | 1058 | (list |
9bfbb130 | 1059 | ;; Anything not a variable or type declaration is fontified as a function. |
d46c21ec SM |
1060 | ;; It would be cleaner to allow preceding whitespace, but it would also be |
1061 | ;; about five times slower. | |
1062 | (list (concat "^(\\(def\\(" | |
1063 | ;; Variable declarations. | |
1064 | "\\(const\\(\\|ant\\)\\|ine-key\\(\\|-after\\)\\|var\\)\\|" | |
1065 | ;; Structure declarations. | |
1066 | "\\(class\\|struct\\|type\\)\\|" | |
1067 | ;; Everything else is a function declaration. | |
1068 | "\\([^ \t\n\(\)]+\\)" | |
1069 | "\\)\\)\\>" | |
1070 | ;; Any whitespace and declared object. | |
1071 | "[ \t'\(]*" | |
7d7d915a | 1072 | "\\(\\sw+\\)?") |
d46c21ec SM |
1073 | '(1 font-lock-keyword-face) |
1074 | '(8 (cond ((match-beginning 3) font-lock-variable-name-face) | |
1075 | ((match-beginning 6) font-lock-type-face) | |
1076 | (t font-lock-function-name-face)) | |
9bfbb130 SM |
1077 | nil t)) |
1078 | ) | |
7d7d915a | 1079 | "Subdued level highlighting for Lisp modes.") |
030f4a35 RS |
1080 | |
1081 | (defconst lisp-font-lock-keywords-2 | |
799761f0 | 1082 | (append lisp-font-lock-keywords-1 |
9bfbb130 SM |
1083 | (list |
1084 | ;; | |
1085 | ;; Control structures. ELisp and CLisp combined. | |
d46c21ec | 1086 | ; (make-regexp |
7d7d915a | 1087 | ; '("cond" "if" "while" "let\\*?" "prog[nv12*]?" "inline" "catch" "throw" |
d46c21ec SM |
1088 | ; "save-restriction" "save-excursion" "save-window-excursion" |
1089 | ; "save-selected-window" "save-match-data" "unwind-protect" | |
1090 | ; "condition-case" "track-mouse" | |
1091 | ; "eval-after-load" "eval-and-compile" "eval-when-compile" | |
1092 | ; "when" "unless" "do" "flet" "labels" "return" "return-from")) | |
9bfbb130 SM |
1093 | (cons |
1094 | (concat | |
1095 | "(\\(" | |
1096 | "\\(c\\(atch\\|ond\\(\\|ition-case\\)\\)\\|do\\|" | |
1097 | "eval-\\(a\\(fter-load\\|nd-compile\\)\\|when-compile\\)\\|flet\\|" | |
7d7d915a SM |
1098 | "i\\(f\\|nline\\)\\|l\\(abels\\|et\\*?\\)\\|prog[nv12*]?\\|" |
1099 | "return\\(\\|-from\\)\\|" | |
9bfbb130 SM |
1100 | "save-\\(excursion\\|match-data\\|restriction\\|selected-window\\|" |
1101 | "window-excursion\\)\\|t\\(hrow\\|rack-mouse\\)\\|" | |
1102 | "un\\(less\\|wind-protect\\)\\|wh\\(en\\|ile\\)\\)" | |
1103 | "\\)\\>") 1) | |
1104 | ;; | |
7d7d915a SM |
1105 | ;; Feature symbols as references. |
1106 | '("(\\(featurep\\|provide\\|require\\)\\>[ \t']*\\(\\sw+\\)?" | |
1107 | (1 font-lock-keyword-face) (2 font-lock-reference-face nil t)) | |
1108 | ;; | |
9bfbb130 SM |
1109 | ;; Words inside \\[] tend to be for `substitute-command-keys'. |
1110 | '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-reference-face prepend) | |
1111 | ;; | |
1112 | ;; Words inside `' tend to be symbol names. | |
1113 | '("`\\(\\sw\\sw+\\)'" 1 font-lock-reference-face prepend) | |
1114 | ;; | |
1115 | ;; CLisp `:' keywords as references. | |
1116 | '("\\<:\\sw+\\>" 0 font-lock-reference-face prepend) | |
1117 | ;; | |
1118 | ;; ELisp and CLisp `&' keywords as types. | |
1119 | '("\\<\\&\\(optional\\|rest\\|whole\\)\\>" . font-lock-type-face) | |
1120 | )) | |
fb512de9 | 1121 | "Gaudy level highlighting for Lisp modes.") |
030f4a35 | 1122 | |
fb512de9 SM |
1123 | (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1 |
1124 | "Default expressions to highlight in Lisp modes.") | |
030f4a35 RS |
1125 | |
1126 | ||
d46c21ec | 1127 | (defvar scheme-font-lock-keywords |
9bfbb130 SM |
1128 | (eval-when-compile |
1129 | (list | |
1130 | ;; | |
1131 | ;; Declarations. Hannes Haug <hannes.haug@student.uni-tuebingen.de> says | |
1132 | ;; this works for SOS, STklos, SCOOPS, Meroon and Tiny CLOS. | |
1133 | (list (concat "(\\(define\\(" | |
1134 | ;; Function names. | |
1135 | "\\(\\|-\\(generic\\(\\|-procedure\\)\\|method\\)\\)\\|" | |
1136 | ;; Macro names, as variable names. A bit dubious, this. | |
1137 | "\\(-syntax\\)\\|" | |
1138 | ;; Class names. | |
1139 | "\\(-class\\)" | |
1140 | "\\)\\)\\>" | |
1141 | ;; Any whitespace and declared object. | |
1142 | "[ \t]*(?" | |
1143 | "\\(\\sw+\\)?") | |
1144 | '(1 font-lock-keyword-face) | |
1145 | '(8 (cond ((match-beginning 3) font-lock-function-name-face) | |
1146 | ((match-beginning 6) font-lock-variable-name-face) | |
1147 | (t font-lock-type-face)) | |
1148 | nil t)) | |
1149 | ;; | |
1150 | ;; Control structures. | |
d46c21ec SM |
1151 | ;(make-regexp '("begin" "call-with-current-continuation" "call/cc" |
1152 | ; "call-with-input-file" "call-with-output-file" "case" "cond" | |
9bfbb130 SM |
1153 | ; "do" "else" "for-each" "if" "lambda" |
1154 | ; "let\\*?" "let-syntax" "letrec" "letrec-syntax" | |
1155 | ; ;; Hannes Haug <hannes.haug@student.uni-tuebingen.de> wants: | |
1156 | ; "and" "or" "delay" | |
1157 | ; ;; Stefan Monnier <stefan.monnier@epfl.ch> says don't bother: | |
d46c21ec SM |
1158 | ; ;;"quasiquote" "quote" "unquote" "unquote-splicing" |
1159 | ; "map" "syntax" "syntax-rules")) | |
9bfbb130 SM |
1160 | (cons |
1161 | (concat "(\\(" | |
1162 | "and\\|begin\\|c\\(a\\(ll\\(-with-\\(current-continuation\\|" | |
1163 | "input-file\\|output-file\\)\\|/cc\\)\\|se\\)\\|ond\\)\\|" | |
1164 | "d\\(elay\\|o\\)\\|else\\|for-each\\|if\\|" | |
1165 | "l\\(ambda\\|et\\(-syntax\\|\\*?\\|rec\\(\\|-syntax\\)\\)\\)\\|" | |
1166 | "map\\|or\\|syntax\\(\\|-rules\\)" | |
1167 | "\\)\\>") 1) | |
1168 | ;; | |
1169 | ;; David Fox <fox@graphics.cs.nyu.edu> for SOS/STklos class specifiers. | |
1170 | '("\\<<\\sw+>\\>" . font-lock-type-face) | |
1171 | ;; | |
1172 | ;; Scheme `:' keywords as references. | |
1173 | '("\\<:\\sw+\\>" . font-lock-reference-face) | |
1174 | )) | |
1175 | "Default expressions to highlight in Scheme modes.") | |
d46c21ec SM |
1176 | |
1177 | ||
030f4a35 | 1178 | (defconst c-font-lock-keywords-1 nil |
fb512de9 | 1179 | "Subdued level highlighting for C modes.") |
030f4a35 RS |
1180 | |
1181 | (defconst c-font-lock-keywords-2 nil | |
9bfbb130 SM |
1182 | "Medium level highlighting for C modes.") |
1183 | ||
1184 | (defconst c-font-lock-keywords-3 nil | |
fb512de9 | 1185 | "Gaudy level highlighting for C modes.") |
030f4a35 | 1186 | |
1bd50840 | 1187 | (defconst c++-font-lock-keywords-1 nil |
fb512de9 | 1188 | "Subdued level highlighting for C++ modes.") |
1bd50840 RS |
1189 | |
1190 | (defconst c++-font-lock-keywords-2 nil | |
9bfbb130 SM |
1191 | "Medium level highlighting for C++ modes.") |
1192 | ||
1193 | (defconst c++-font-lock-keywords-3 nil | |
fb512de9 | 1194 | "Gaudy level highlighting for C++ modes.") |
1bd50840 | 1195 | |
9bfbb130 SM |
1196 | (defun font-lock-match-c++-style-declaration-item-and-skip-to-next (limit) |
1197 | ;; Match, and move over, any declaration/definition item after point. | |
1198 | ;; The expect syntax of an item is "word" or "word::word", possibly ending | |
1199 | ;; with optional whitespace and a "(". Everything following the item (but | |
1200 | ;; belonging to it) is expected to by skip-able by `forward-sexp', and items | |
1201 | ;; are expected to be separated with a "," or ";". | |
1202 | (if (looking-at "[ \t*&]*\\(\\sw+\\)\\(::\\(\\sw+\\)\\)?[ \t]*\\((\\)?") | |
1203 | (save-match-data | |
1204 | (condition-case nil | |
1205 | (save-restriction | |
876f2438 SM |
1206 | ;; Restrict to the end of line, currently guaranteed to be LIMIT. |
1207 | (narrow-to-region (point-min) limit) | |
9bfbb130 SM |
1208 | (goto-char (match-end 1)) |
1209 | ;; Move over any item value, etc., to the next item. | |
1210 | (while (not (looking-at "[ \t]*\\([,;]\\|$\\)")) | |
1211 | (goto-char (or (scan-sexps (point) 1) (point-max)))) | |
1212 | (goto-char (match-end 0))) | |
1213 | (error t))))) | |
1214 | ||
b89e1134 SM |
1215 | (let ((c-keywords |
1216 | ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while") | |
1217 | "break\\|continue\\|do\\|else\\|for\\|if\\|return\\|switch\\|while") | |
1218 | (c-type-types | |
a1eb1cf1 RS |
1219 | ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum" |
1220 | ; "signed" "unsigned" "short" "long" "int" "char" "float" "double" | |
b89e1134 SM |
1221 | ; "void" "volatile" "const") |
1222 | (concat "auto\\|c\\(har\\|onst\\)\\|double\\|e\\(num\\|xtern\\)\\|" | |
1223 | "float\\|int\\|long\\|register\\|" | |
1224 | "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|typedef\\|" | |
1225 | "un\\(ion\\|signed\\)\\|vo\\(id\\|latile\\)")) ; 6 ()s deep. | |
1226 | (c++-keywords | |
1227 | ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while" | |
1228 | ; "asm" "catch" "delete" "new" "operator" "sizeof" "this" "throw" "try" | |
1229 | ; "protected" "private" "public") | |
1230 | (concat "asm\\|break\\|c\\(atch\\|ontinue\\)\\|d\\(elete\\|o\\)\\|" | |
9bfbb130 | 1231 | "else\\|for\\|if\\|new\\|" |
b89e1134 SM |
1232 | "p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|return\\|" |
1233 | "s\\(izeof\\|witch\\)\\|t\\(h\\(is\\|row\\)\\|ry\\)\\|while")) | |
1234 | (c++-type-types | |
1235 | ; ("auto" "extern" "register" "static" "typedef" "struct" "union" "enum" | |
1236 | ; "signed" "unsigned" "short" "long" "int" "char" "float" "double" | |
1237 | ; "void" "volatile" "const" "class" "inline" "friend" "bool" | |
1238 | ; "virtual" "complex" "template") | |
1239 | (concat "auto\\|bool\\|c\\(har\\|lass\\|o\\(mplex\\|nst\\)\\)\\|" | |
1240 | "double\\|e\\(num\\|xtern\\)\\|f\\(loat\\|riend\\)\\|" | |
1241 | "in\\(line\\|t\\)\\|long\\|register\\|" | |
1242 | "s\\(hort\\|igned\\|t\\(atic\\|ruct\\)\\)\\|" | |
1243 | "t\\(emplate\\|ypedef\\)\\|un\\(ion\\|signed\\)\\|" | |
1244 | "v\\(irtual\\|o\\(id\\|latile\\)\\)")) ; 11 ()s deep. | |
9bfbb130 | 1245 | ) |
f60f18ae KH |
1246 | (setq c-font-lock-keywords-1 |
1247 | (list | |
f60f18ae | 1248 | ;; |
9bfbb130 SM |
1249 | ;; These are all anchored at the beginning of line for speed. |
1250 | ;; | |
1251 | ;; Fontify function name definitions (GNU style; without type on line). | |
1252 | (list (concat "^\\(\\sw+\\)[ \t]*(") 1 'font-lock-function-name-face) | |
1253 | ;; | |
1254 | ;; Fontify filenames in #include <...> preprocessor directives as strings. | |
f60f18ae KH |
1255 | '("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face) |
1256 | ;; | |
a1eb1cf1 | 1257 | ;; Fontify function macro names. |
7d7d915a | 1258 | '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face) |
f60f18ae | 1259 | ;; |
9bfbb130 SM |
1260 | ;; Fontify symbol names in #if ... defined preprocessor directives. |
1261 | '("^#[ \t]*if\\>" | |
1262 | ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil | |
1263 | (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t))) | |
1264 | ;; | |
a1eb1cf1 | 1265 | ;; Fontify otherwise as symbol names, and the preprocessor directive names. |
7d7d915a | 1266 | '("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?" |
a1eb1cf1 | 1267 | (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t)) |
f60f18ae KH |
1268 | )) |
1269 | ||
1270 | (setq c-font-lock-keywords-2 | |
1271 | (append c-font-lock-keywords-1 | |
030f4a35 | 1272 | (list |
030f4a35 | 1273 | ;; |
9bfbb130 | 1274 | ;; Simple regexps for speed. |
b89e1134 | 1275 | ;; |
9bfbb130 SM |
1276 | ;; Fontify all type specifiers. |
1277 | (cons (concat "\\<\\(" c-type-types "\\)\\>") 'font-lock-type-face) | |
030f4a35 | 1278 | ;; |
b89e1134 | 1279 | ;; Fontify all builtin keywords (except case, default and goto; see below). |
9bfbb130 | 1280 | (cons (concat "\\<\\(" c-keywords "\\)\\>") 'font-lock-keyword-face) |
030f4a35 | 1281 | ;; |
9bfbb130 | 1282 | ;; Fontify case/goto keywords and targets, and case default/goto tags. |
7d7d915a | 1283 | '("\\<\\(case\\|goto\\)\\>[ \t]*\\(\\sw+\\)?" |
a1eb1cf1 RS |
1284 | (1 font-lock-keyword-face) (2 font-lock-reference-face nil t)) |
1285 | '("^[ \t]*\\(\\sw+\\)[ \t]*:" 1 font-lock-reference-face) | |
f60f18ae | 1286 | ))) |
1bd50840 | 1287 | |
9bfbb130 SM |
1288 | (setq c-font-lock-keywords-3 |
1289 | (append c-font-lock-keywords-2 | |
1290 | ;; | |
1291 | ;; More complicated regexps for more complete highlighting for types. | |
1292 | ;; We still have to fontify type specifiers individually, as C is so hairy. | |
1293 | (list | |
1294 | ;; | |
1295 | ;; Fontify all storage classes and type specifiers, plus their items. | |
1296 | (list (concat "\\<\\(" c-type-types "\\)\\>" | |
1297 | "\\([ \t*&]+\\sw+\\>\\)*") | |
1298 | ;; Fontify each declaration item. | |
1299 | '(font-lock-match-c++-style-declaration-item-and-skip-to-next | |
1300 | ;; Start with point after all type specifiers. | |
1301 | (goto-char (or (match-beginning 8) (match-end 1))) | |
1302 | ;; Finish with point after first type specifier. | |
1303 | (goto-char (match-end 1)) | |
1304 | ;; Fontify as a variable or function name. | |
1305 | (1 (if (match-beginning 4) | |
1306 | font-lock-function-name-face | |
1307 | font-lock-variable-name-face)))) | |
1308 | ;; | |
1309 | ;; Fontify structures, or typedef names, plus their items. | |
1310 | '("\\(}\\)[ \t*]*\\sw" | |
1311 | (font-lock-match-c++-style-declaration-item-and-skip-to-next | |
1312 | (goto-char (match-end 1)) nil | |
1313 | (1 (if (match-beginning 4) | |
1314 | font-lock-function-name-face | |
1315 | font-lock-variable-name-face)))) | |
1316 | ;; | |
1317 | ;; Fontify anything at beginning of line as a declaration or definition. | |
1318 | '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*" | |
1319 | (1 font-lock-type-face) | |
1320 | (font-lock-match-c++-style-declaration-item-and-skip-to-next | |
1321 | (goto-char (or (match-beginning 2) (match-end 1))) nil | |
1322 | (1 (if (match-beginning 4) | |
1323 | font-lock-function-name-face | |
1324 | font-lock-variable-name-face)))) | |
1325 | ))) | |
1326 | ||
1327 | (setq c++-font-lock-keywords-1 | |
1328 | (append | |
1329 | ;; | |
1330 | ;; The list `c-font-lock-keywords-1' less that for function names. | |
1331 | (cdr c-font-lock-keywords-1) | |
1332 | ;; | |
1333 | ;; Fontify function name definitions, possibly incorporating class name. | |
1334 | (list | |
1335 | '("^\\(\\sw+\\)\\(::\\(\\sw+\\)\\)?[ \t]*(" | |
1336 | (1 (if (match-beginning 2) | |
1337 | font-lock-type-face | |
1338 | font-lock-function-name-face)) | |
1339 | (3 (if (match-beginning 2) font-lock-function-name-face) nil t)) | |
1340 | ))) | |
1341 | ||
1bd50840 | 1342 | (setq c++-font-lock-keywords-2 |
b89e1134 | 1343 | (append c++-font-lock-keywords-1 |
a1eb1cf1 | 1344 | (list |
9bfbb130 SM |
1345 | ;; |
1346 | ;; The list `c-font-lock-keywords-2' for C++ plus operator overloading. | |
b89e1134 | 1347 | (cons (concat "\\<\\(" c++-type-types "\\)\\>") 'font-lock-type-face) |
9bfbb130 SM |
1348 | ;; |
1349 | ;; Fontify operator function name overloading. | |
1350 | '("\\<\\(operator\\)\\>[ \t]*\\([][)(><!=+-][][)(><!=+-]?\\)?" | |
1351 | (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t)) | |
1352 | ;; | |
1353 | ;; Fontify case/goto keywords and targets, and case default/goto tags. | |
7d7d915a | 1354 | '("\\<\\(case\\|goto\\)\\>[ \t]*\\(\\sw+\\)?" |
b89e1134 | 1355 | (1 font-lock-keyword-face) (2 font-lock-reference-face nil t)) |
9bfbb130 SM |
1356 | '("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-reference-face) |
1357 | ;; | |
1358 | ;; Fontify other builtin keywords. | |
1359 | (cons (concat "\\<\\(" c++-keywords "\\)\\>") 'font-lock-keyword-face) | |
1360 | ))) | |
1361 | ||
1362 | (setq c++-font-lock-keywords-3 | |
1363 | (append c++-font-lock-keywords-2 | |
1364 | ;; | |
1365 | ;; More complicated regexps for more complete highlighting for types. | |
1366 | (list | |
1367 | ;; | |
1368 | ;; Fontify all storage classes and type specifiers, plus their items. | |
1369 | (list (concat "\\<\\(" c++-type-types "\\)\\>" | |
1370 | "\\([ \t*&]+\\sw+\\>\\)*") | |
1371 | ;; Fontify each declaration item. | |
1372 | '(font-lock-match-c++-style-declaration-item-and-skip-to-next | |
1373 | ;; Start with point after all type specifiers. | |
1374 | (goto-char (or (match-beginning 13) (match-end 1))) | |
1375 | ;; Finish with point after first type specifier. | |
1376 | (goto-char (match-end 1)) | |
1377 | ;; Fontify as a variable or function name. | |
1378 | (1 (cond ((match-beginning 2) font-lock-type-face) | |
1379 | ((match-beginning 4) font-lock-function-name-face) | |
1380 | (t font-lock-variable-name-face))) | |
1381 | (3 (if (match-beginning 4) | |
1382 | font-lock-function-name-face | |
1383 | font-lock-variable-name-face) nil t))) | |
1384 | ;; | |
1385 | ;; Fontify structures, or typedef names, plus their items. | |
1386 | '("\\(}\\)[ \t*]*\\sw" | |
1387 | (font-lock-match-c++-style-declaration-item-and-skip-to-next | |
1388 | (goto-char (match-end 1)) nil | |
1389 | (1 (if (match-beginning 4) | |
1390 | font-lock-function-name-face | |
1391 | font-lock-variable-name-face)))) | |
1392 | ;; | |
1393 | ;; Fontify anything at beginning of line as a declaration or definition. | |
1394 | '("^\\(\\sw+\\)\\>\\([ \t*]+\\sw+\\>\\)*" | |
1395 | (1 font-lock-type-face) | |
1396 | (font-lock-match-c++-style-declaration-item-and-skip-to-next | |
1397 | (goto-char (or (match-beginning 2) (match-end 1))) nil | |
1398 | (1 (cond ((match-beginning 2) font-lock-type-face) | |
1399 | ((match-beginning 4) font-lock-function-name-face) | |
1400 | (t font-lock-variable-name-face))) | |
1401 | (3 (if (match-beginning 4) | |
1402 | font-lock-function-name-face | |
1403 | font-lock-variable-name-face) nil t))) | |
1404 | ))) | |
f60f18ae | 1405 | ) |
030f4a35 | 1406 | |
fb512de9 SM |
1407 | (defvar c-font-lock-keywords c-font-lock-keywords-1 |
1408 | "Default expressions to highlight in C mode.") | |
030f4a35 | 1409 | |
fb512de9 SM |
1410 | (defvar c++-font-lock-keywords c++-font-lock-keywords-1 |
1411 | "Default expressions to highlight in C++ mode.") | |
030f4a35 | 1412 | |
d46c21ec | 1413 | |
030f4a35 | 1414 | (defvar tex-font-lock-keywords |
d46c21ec SM |
1415 | ; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>. |
1416 | ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}" | |
1417 | ; 2 font-lock-function-name-face) | |
1418 | ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}" | |
1419 | ; 2 font-lock-reference-face) | |
1420 | ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might | |
1421 | ; ;; not be able to display those fonts. | |
1422 | ; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep) | |
1423 | ; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep) | |
1424 | ; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face) | |
1425 | ; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep)) | |
1426 | ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>. | |
826b2925 SM |
1427 | '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}" |
1428 | 2 font-lock-function-name-face) | |
1429 | ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}" | |
1430 | 2 font-lock-reference-face) | |
d46c21ec | 1431 | ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face) |
9bfbb130 | 1432 | "\\\\\\([a-zA-Z@]+\\|.\\)" |
826b2925 SM |
1433 | ;; It seems a bit dubious to use `bold' and `italic' faces since we might |
1434 | ;; not be able to display those fonts. | |
d46c21ec SM |
1435 | ;; LaTeX2e: \emph{This is emphasized}. |
1436 | ("\\\\emph{\\([^}]+\\)}" 1 'italic keep) | |
1437 | ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...} | |
1438 | ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}" | |
1439 | 3 (if (match-beginning 2) 'bold 'italic) keep) | |
1440 | ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for good tables. | |
1441 | ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)" | |
1442 | 3 (if (match-beginning 2) 'bold 'italic) keep)) | |
9bfbb130 | 1443 | "Default expressions to highlight in TeX modes.") |
d46c21ec | 1444 | \f |
a1eb1cf1 RS |
1445 | ;; Install ourselves: |
1446 | ||
a1eb1cf1 RS |
1447 | (or (assq 'font-lock-mode minor-mode-alist) |
1448 | (setq minor-mode-alist (cons '(font-lock-mode " Font") minor-mode-alist))) | |
1449 | ||
1450 | ;; Provide ourselves: | |
8f261d40 | 1451 | |
030f4a35 RS |
1452 | (provide 'font-lock) |
1453 | ||
1454 | ;;; font-lock.el ends here |