Commit | Line | Data |
---|---|---|
f62be99b CW |
1 | ;;; font-core.el --- Core interface to font-lock |
2 | ||
3 | ;; Copyright (C) 1992, 93, 94, 95, 96, 97, 98, 1999, 2000, 2001, 2002 | |
4 | ;; Free Software Foundation, Inc. | |
5 | ||
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 the | |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
25 | ||
26 | ;;; Code: | |
27 | ||
28 | ;; This variable is used by mode packages that support Font Lock mode by | |
29 | ;; defining their own keywords to use for `font-lock-keywords'. (The mode | |
30 | ;; command should make it buffer-local and set it to provide the set up.) | |
31 | (defvar font-lock-defaults nil | |
32 | "Defaults for Font Lock mode specified by the major mode. | |
33 | Defaults should be of the form: | |
34 | ||
35 | (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN ...) | |
36 | ||
37 | KEYWORDS may be a symbol (a variable or function whose value is the keywords to | |
38 | use for fontification) or a list of symbols. If KEYWORDS-ONLY is non-nil, | |
39 | syntactic fontification (strings and comments) is not performed. | |
40 | If CASE-FOLD is non-nil, the case of the keywords is ignored when fontifying. | |
41 | If SYNTAX-ALIST is non-nil, it should be a list of cons pairs of the form | |
42 | \(CHAR-OR-STRING . STRING) used to set the local Font Lock syntax table, for | |
43 | keyword and syntactic fontification (see `modify-syntax-entry'). | |
44 | ||
45 | If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move | |
46 | backwards outside any enclosing syntactic block, for syntactic fontification. | |
47 | Typical values are `beginning-of-line' (i.e., the start of the line is known to | |
48 | be outside a syntactic block), or `beginning-of-defun' for programming modes or | |
49 | `backward-paragraph' for textual modes (i.e., the mode-dependent function is | |
50 | known to move outside a syntactic block). If nil, the beginning of the buffer | |
51 | is used as a position outside of a syntactic block, in the worst case. | |
52 | ||
53 | These item elements are used by Font Lock mode to set the variables | |
54 | `font-lock-keywords', `font-lock-keywords-only', | |
55 | `font-lock-keywords-case-fold-search', `font-lock-syntax-table' and | |
56 | `font-lock-beginning-of-syntax-function', respectively. | |
57 | ||
58 | Further item elements are alists of the form (VARIABLE . VALUE) and are in no | |
59 | particular order. Each VARIABLE is made buffer-local before set to VALUE. | |
60 | ||
61 | Currently, appropriate variables include `font-lock-mark-block-function'. | |
62 | If this is non-nil, it should be a function with no args used to mark any | |
63 | enclosing block of text, for fontification via \\[font-lock-fontify-block]. | |
64 | Typical values are `mark-defun' for programming modes or `mark-paragraph' for | |
65 | textual modes (i.e., the mode-dependent function is known to put point and mark | |
66 | around a text block relevant to that mode). | |
67 | ||
68 | Other variables include that for syntactic keyword fontification, | |
69 | `font-lock-syntactic-keywords' | |
70 | and those for buffer-specialised fontification functions, | |
71 | `font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function', | |
72 | `font-lock-fontify-region-function', `font-lock-unfontify-region-function', | |
73 | `font-lock-inhibit-thing-lock' and `font-lock-maximum-size'.") | |
74 | (make-variable-buffer-local 'font-lock-defaults) | |
75 | ||
76 | ;; This variable is used where font-lock.el itself supplies the | |
77 | ;; keywords. Really, this shouldn't need to be in font-core.el, but | |
78 | ;; we can't avoid it. In the future, this stuff will hopefully be | |
79 | ;; moved to cc-mode itself. | |
80 | (defvar font-lock-defaults-alist | |
81 | (let (;; We use `beginning-of-defun', rather than nil, for SYNTAX-BEGIN. | |
82 | ;; Thus the calculation of the cache is usually faster but not | |
83 | ;; infallible, so we risk mis-fontification. sm. | |
84 | (c-mode-defaults | |
85 | '((c-font-lock-keywords c-font-lock-keywords-1 | |
86 | c-font-lock-keywords-2 c-font-lock-keywords-3) | |
87 | nil nil ((?_ . "w")) beginning-of-defun | |
88 | (font-lock-syntactic-face-function | |
89 | . c-font-lock-syntactic-face-function) | |
90 | (font-lock-mark-block-function . mark-defun))) | |
91 | (c++-mode-defaults | |
92 | '((c++-font-lock-keywords c++-font-lock-keywords-1 | |
93 | c++-font-lock-keywords-2 c++-font-lock-keywords-3) | |
94 | nil nil ((?_ . "w")) beginning-of-defun | |
95 | (font-lock-syntactic-face-function | |
96 | . c-font-lock-syntactic-face-function) | |
97 | (font-lock-mark-block-function . mark-defun))) | |
98 | (objc-mode-defaults | |
99 | '((objc-font-lock-keywords objc-font-lock-keywords-1 | |
100 | objc-font-lock-keywords-2 objc-font-lock-keywords-3) | |
101 | nil nil ((?_ . "w") (?$ . "w")) nil | |
102 | (font-lock-syntactic-face-function | |
103 | . c-font-lock-syntactic-face-function) | |
104 | (font-lock-mark-block-function . mark-defun))) | |
105 | (java-mode-defaults | |
106 | '((java-font-lock-keywords java-font-lock-keywords-1 | |
107 | java-font-lock-keywords-2 java-font-lock-keywords-3) | |
108 | nil nil ((?_ . "w") (?$ . "w")) nil | |
109 | (font-lock-syntactic-face-function | |
110 | . java-font-lock-syntactic-face-function) | |
111 | (font-lock-mark-block-function . mark-defun)))) | |
112 | (list | |
113 | (cons 'c-mode c-mode-defaults) | |
114 | (cons 'c++-mode c++-mode-defaults) | |
115 | (cons 'objc-mode objc-mode-defaults) | |
116 | (cons 'java-mode java-mode-defaults))) | |
117 | "Alist of fall-back Font Lock defaults for major modes. | |
118 | ||
119 | This variable should not be used any more. | |
120 | Set the buffer-local `font-lock-keywords' in the major mode instead. | |
121 | ||
122 | Each item should be a list of the form: | |
123 | ||
124 | (MAJOR-MODE . FONT-LOCK-DEFAULTS) | |
125 | ||
126 | where MAJOR-MODE is a symbol and FONT-LOCK-DEFAULTS is a list of default | |
127 | settings. See the variable `font-lock-defaults', which takes precedence.") | |
128 | (make-obsolete-variable 'font-lock-defaults-alist 'font-lock-defaults) | |
129 | ||
130 | (defvar font-lock-multiline nil | |
131 | "Whether font-lock should cater to multiline keywords. | |
132 | If nil, don't try to handle multiline patterns. | |
133 | If t, always handle multiline patterns. | |
134 | If `undecided', don't try to handle multiline patterns until you see one. | |
135 | Major/minor modes can set this variable if they know which option applies.") | |
136 | ||
137 | (defvar font-lock-fontified nil) ; Whether we have fontified the buffer. | |
138 | ||
7c4ef9b3 CW |
139 | (defvar font-lock-function 'font-lock-default-function |
140 | "A function which is called when `font-lock-mode' is toggled. | |
141 | It will be passed one argument, which is the current value of | |
142 | `font-lock-mode'.") | |
3e119830 | 143 | (make-variable-buffer-local 'font-lock-function) |
7c4ef9b3 | 144 | |
f62be99b CW |
145 | (define-minor-mode font-lock-mode |
146 | "Toggle Font Lock mode. | |
147 | With arg, turn Font Lock mode off if and only if arg is a non-positive | |
148 | number; if arg is nil, toggle Font Lock mode; anything else turns Font | |
149 | Lock on. | |
150 | \(Font Lock is also known as \"syntax highlighting\".) | |
151 | ||
152 | When Font Lock mode is enabled, text is fontified as you type it: | |
153 | ||
154 | - Comments are displayed in `font-lock-comment-face'; | |
155 | - Strings are displayed in `font-lock-string-face'; | |
156 | - Certain other expressions are displayed in other faces according to the | |
157 | value of the variable `font-lock-keywords'. | |
158 | ||
159 | To customize the faces (colors, fonts, etc.) used by Font Lock for | |
160 | fontifying different parts of buffer text, use \\[customize-face]. | |
161 | ||
162 | You can enable Font Lock mode in any major mode automatically by turning on in | |
163 | the major mode's hook. For example, put in your ~/.emacs: | |
164 | ||
165 | (add-hook 'c-mode-hook 'turn-on-font-lock) | |
166 | ||
167 | Alternatively, you can use Global Font Lock mode to automagically turn on Font | |
168 | Lock mode in buffers whose major mode supports it and whose major mode is one | |
169 | of `font-lock-global-modes'. For example, put in your ~/.emacs: | |
170 | ||
171 | (global-font-lock-mode t) | |
172 | ||
173 | There are a number of support modes that may be used to speed up Font Lock mode | |
174 | in various ways, specified via the variable `font-lock-support-mode'. Where | |
175 | major modes support different levels of fontification, you can use the variable | |
176 | `font-lock-maximum-decoration' to specify which level you generally prefer. | |
177 | When you turn Font Lock mode on/off the buffer is fontified/defontified, though | |
178 | fontification occurs only if the buffer is less than `font-lock-maximum-size'. | |
179 | ||
180 | For example, to specify that Font Lock mode use use Lazy Lock mode as a support | |
181 | mode and use maximum levels of fontification, put in your ~/.emacs: | |
182 | ||
183 | (setq font-lock-support-mode 'lazy-lock-mode) | |
184 | (setq font-lock-maximum-decoration t) | |
185 | ||
186 | To add your own highlighting for some major mode, and modify the highlighting | |
187 | selected automatically via the variable `font-lock-maximum-decoration', you can | |
188 | use `font-lock-add-keywords'. | |
189 | ||
190 | To fontify a buffer, without turning on Font Lock mode and regardless of buffer | |
191 | size, you can use \\[font-lock-fontify-buffer]. | |
192 | ||
193 | To fontify a block (the function or paragraph containing point, or a number of | |
194 | lines around point), perhaps because modification on the current line caused | |
195 | syntactic change on other lines, you can use \\[font-lock-fontify-block]. | |
196 | ||
197 | See the variable `font-lock-defaults-alist' for the Font Lock mode default | |
198 | settings. You can set your own default settings for some mode, by setting a | |
7c4ef9b3 CW |
199 | buffer local value for `font-lock-defaults', via its mode hook. |
200 | ||
201 | The above is the default behavior of `font-lock-mode'; you may specify | |
202 | your own function which is called when `font-lock-mode' is toggled via | |
203 | `font-lock-function'. " | |
f62be99b CW |
204 | nil nil nil |
205 | ;; Don't turn on Font Lock mode if we don't have a display (we're running a | |
206 | ;; batch job) or if the buffer is invisible (the name starts with a space). | |
207 | (when (or noninteractive (eq (aref (buffer-name) 0) ?\ )) | |
208 | (setq font-lock-mode nil)) | |
8e97108f RS |
209 | (funcall font-lock-function font-lock-mode) |
210 | ;; Arrange to unfontify this buffer if we change major mode later. | |
211 | (if font-lock-mode | |
5a705496 RS |
212 | (add-hook 'change-major-mode-hook 'font-lock-unfontify-buffer nil t) |
213 | (remove-hook 'change-major-mode-hook 'font-lock-unfontify-buffer t))) | |
f62be99b | 214 | |
7c4ef9b3 | 215 | (defun font-lock-default-function (font-lock-mode) |
f62be99b CW |
216 | ;; Turn on Font Lock mode. |
217 | (when font-lock-mode | |
218 | (font-lock-set-defaults) | |
1b6adddb CW |
219 | (set (make-local-variable 'char-property-alias-alist) |
220 | (copy-tree char-property-alias-alist)) | |
221 | ;; Add `font-lock-face' as an alias for the `face' property. | |
222 | (let ((elt (assq 'face char-property-alias-alist))) | |
223 | (if elt | |
224 | (unless (memq 'font-lock-face (cdr elt)) | |
225 | (setcdr elt (nconc (cdr elt) (list 'font-lock-face)))) | |
226 | (push (list 'face 'font-lock-face) char-property-alias-alist))) | |
227 | ;; Only do hard work if the mode has specified stuff in | |
228 | ;; `font-lock-defaults'. | |
61fd2c68 | 229 | (when font-lock-defaults |
f62be99b CW |
230 | (add-hook 'after-change-functions 'font-lock-after-change-function t t) |
231 | (font-lock-turn-on-thing-lock) | |
232 | ;; Fontify the buffer if we have to. | |
233 | (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size))) | |
234 | (cond (font-lock-fontified | |
235 | nil) | |
236 | ((or (null max-size) (> max-size (buffer-size))) | |
237 | (font-lock-fontify-buffer)) | |
238 | (font-lock-verbose | |
239 | (message "Fontifying %s...buffer size greater than font-lock-maximum-size" | |
240 | (buffer-name))))))) | |
241 | ;; Turn off Font Lock mode. | |
242 | (unless font-lock-mode | |
1b6adddb CW |
243 | ;; Remove `font-lock-face' as an alias for the `face' property. |
244 | (set (make-local-variable 'char-property-alias-alist) | |
245 | (copy-tree char-property-alias-alist)) | |
246 | (let ((elt (assq 'face char-property-alias-alist))) | |
247 | (when elt | |
248 | (setcdr elt (remq 'font-lock-face (cdr elt))) | |
249 | (when (null (cdr elt)) | |
250 | (setq char-property-alias-alist (delq elt char-property-alias-alist))))) | |
61fd2c68 | 251 | (when font-lock-defaults |
f62be99b CW |
252 | (remove-hook 'after-change-functions 'font-lock-after-change-function t) |
253 | (font-lock-unfontify-buffer) | |
254 | (font-lock-turn-off-thing-lock)))) | |
255 | ||
256 | (defun turn-on-font-lock () | |
257 | "Turn on Font Lock mode (only if the terminal can display it)." | |
258 | (unless font-lock-mode | |
259 | (font-lock-mode))) | |
260 | ||
261 | (defvar font-lock-set-defaults nil) ; Whether we have set up defaults. | |
262 | ||
263 | (defun font-lock-set-defaults () | |
264 | "Set fontification defaults appropriately for this mode. | |
265 | Sets various variables using `font-lock-defaults' (or, if nil, using | |
266 | `font-lock-defaults-alist') and `font-lock-maximum-decoration'." | |
267 | (unless font-lock-set-defaults | |
268 | (set (make-local-variable 'font-lock-set-defaults) t) | |
269 | (make-local-variable 'font-lock-fontified) | |
270 | (make-local-variable 'font-lock-multiline) | |
660acebe | 271 | (let ((defaults (or font-lock-defaults |
33260712 | 272 | (cdr (assq major-mode font-lock-defaults-alist))))) |
61fd2c68 | 273 | (when defaults |
660acebe CW |
274 | (require 'font-lock) |
275 | (font-lock-set-defaults-1))))) | |
f62be99b CW |
276 | |
277 | ;;; Global Font Lock mode. | |
278 | ||
279 | ;; A few people have hassled in the past for a way to make it easier to turn on | |
280 | ;; Font Lock mode, without the user needing to know for which modes s/he has to | |
281 | ;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always | |
282 | ;; balked at that way, as I see it as just re-moulding the same problem in | |
283 | ;; another form. That is; some person would still have to keep track of which | |
284 | ;; modes (which may not even be distributed with Emacs) support Font Lock mode. | |
285 | ;; The list would always be out of date. And that person might have to be me. | |
286 | ||
287 | ;; Implementation. | |
288 | ;; | |
289 | ;; In a previous discussion the following hack came to mind. It is a gross | |
290 | ;; hack, but it generally works. We use the convention that major modes start | |
291 | ;; by calling the function `kill-all-local-variables', which in turn runs | |
292 | ;; functions on the hook variable `change-major-mode-hook'. We attach our | |
293 | ;; function `font-lock-change-major-mode' to that hook. Of course, when this | |
294 | ;; hook is run, the major mode is in the process of being changed and we do not | |
295 | ;; know what the final major mode will be. So, `font-lock-change-major-mode' | |
296 | ;; only (a) notes the name of the current buffer, and (b) adds our function | |
1a4914f3 | 297 | ;; `turn-on-font-lock-if-enabled' to the hook variables `find-file-hook' and |
f62be99b CW |
298 | ;; `post-command-hook' (for buffers that are not visiting files). By the time |
299 | ;; the functions on the first of these hooks to be run are run, the new major | |
300 | ;; mode is assumed to be in place. This way we get a Font Lock function run | |
301 | ;; when a major mode is turned on, without knowing major modes or their hooks. | |
302 | ;; | |
303 | ;; Naturally this requires that (a) major modes run `kill-all-local-variables', | |
304 | ;; as they are supposed to do, and (b) the major mode is in place after the | |
305 | ;; file is visited or the command that ran `kill-all-local-variables' has | |
306 | ;; finished, whichever the sooner. Arguably, any major mode that does not | |
307 | ;; follow the convension (a) is broken, and I can't think of any reason why (b) | |
308 | ;; would not be met (except `gnudoit' on non-files). However, it is not clean. | |
309 | ;; | |
310 | ;; Probably the cleanest solution is to have each major mode function run some | |
311 | ;; hook, e.g., `major-mode-hook', but maybe implementing that change is | |
312 | ;; impractical. I am personally against making `setq' a macro or be advised, | |
313 | ;; or have a special function such as `set-major-mode', but maybe someone can | |
314 | ;; come up with another solution? | |
315 | ||
316 | ;; User interface. | |
317 | ;; | |
318 | ;; Although Global Font Lock mode is a pseudo-mode, I think that the user | |
319 | ;; interface should conform to the usual Emacs convention for modes, i.e., a | |
320 | ;; command to toggle the feature (`global-font-lock-mode') with a variable for | |
321 | ;; finer control of the mode's behaviour (`font-lock-global-modes'). | |
322 | ;; | |
323 | ;; The feature should not be enabled by loading font-lock.el, since other | |
324 | ;; mechanisms for turning on Font Lock mode, such as M-x font-lock-mode RET or | |
325 | ;; (add-hook 'c-mode-hook 'turn-on-font-lock), would cause Font Lock mode to be | |
326 | ;; turned on everywhere. That would not be intuitive or informative because | |
327 | ;; loading a file tells you nothing about the feature or how to control it. It | |
328 | ;; would also be contrary to the Principle of Least Surprise. sm. | |
329 | ||
330 | (defcustom font-lock-global-modes t | |
331 | "*Modes for which Font Lock mode is automagically turned on. | |
332 | Global Font Lock mode is controlled by the command `global-font-lock-mode'. | |
333 | If nil, means no modes have Font Lock mode automatically turned on. | |
334 | If t, all modes that support Font Lock mode have it automatically turned on. | |
335 | If a list, it should be a list of `major-mode' symbol names for which Font Lock | |
336 | mode should be automatically turned on. The sense of the list is negated if it | |
337 | begins with `not'. For example: | |
338 | (c-mode c++-mode) | |
339 | means that Font Lock mode is turned on for buffers in C and C++ modes only." | |
340 | :type '(choice (const :tag "none" nil) | |
341 | (const :tag "all" t) | |
342 | (set :menu-tag "mode specific" :tag "modes" | |
343 | :value (not) | |
344 | (const :tag "Except" not) | |
345 | (repeat :inline t (symbol :tag "mode")))) | |
346 | :group 'font-lock) | |
347 | ||
348 | (defun turn-on-font-lock-if-enabled () | |
61fd2c68 CW |
349 | (unless (and (eq (car-safe font-lock-global-modes) 'not) |
350 | (memq major-mode (cdr font-lock-global-modes))) | |
f62be99b CW |
351 | (let (inhibit-quit) |
352 | (turn-on-font-lock)))) | |
353 | ||
354 | (easy-mmode-define-global-mode | |
355 | global-font-lock-mode font-lock-mode turn-on-font-lock-if-enabled | |
356 | :extra-args (dummy)) | |
357 | ||
358 | ;;; End of Global Font Lock mode. | |
359 | ||
360 | (provide 'font-core) | |
361 | ||
362 | ;;; font-core.el ends here | |
363 |