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