]> code.delx.au - gnu-emacs/blob - lisp/font-core.el
(Flogxor): Fix typo in docstring.
[gnu-emacs] / lisp / font-core.el
1 ;;; font-core.el --- Core interface to font-lock
2
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, 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' and those for buffer-specialized fontification
70 functions, `font-lock-fontify-buffer-function',
71 `font-lock-unfontify-buffer-function', `font-lock-fontify-region-function',
72 `font-lock-unfontify-region-function', and `font-lock-inhibit-thing-lock'.")
73 (make-variable-buffer-local 'font-lock-defaults)
74
75 (defvar font-lock-defaults-alist nil
76 "Alist of fall-back Font Lock defaults for major modes.
77
78 Each item should be a list of the form:
79
80 (MAJOR-MODE . FONT-LOCK-DEFAULTS)
81
82 where MAJOR-MODE is a symbol and FONT-LOCK-DEFAULTS is a list of default
83 settings. See the variable `font-lock-defaults', which takes precedence.")
84 (make-obsolete-variable 'font-lock-defaults-alist 'font-lock-defaults)
85
86 (defvar font-lock-function 'font-lock-default-function
87 "A function which is called when `font-lock-mode' is toggled.
88 It will be passed one argument, which is the current value of
89 `font-lock-mode'.")
90
91 ;; The mode for which font-lock was initialized, or nil if none.
92 (defvar font-lock-mode-major-mode)
93 (define-minor-mode font-lock-mode
94 "Toggle Font Lock mode.
95 With arg, turn Font Lock mode off if and only if arg is a non-positive
96 number; if arg is nil, toggle Font Lock mode; anything else turns Font
97 Lock on.
98 \(Font Lock is also known as \"syntax highlighting\".)
99
100 When Font Lock mode is enabled, text is fontified as you type it:
101
102 - Comments are displayed in `font-lock-comment-face';
103 - Strings are displayed in `font-lock-string-face';
104 - Certain other expressions are displayed in other faces according to the
105 value of the variable `font-lock-keywords'.
106
107 To customize the faces (colors, fonts, etc.) used by Font Lock for
108 fontifying different parts of buffer text, use \\[customize-face].
109
110 You can enable Font Lock mode in any major mode automatically by turning on in
111 the major mode's hook. For example, put in your ~/.emacs:
112
113 (add-hook 'c-mode-hook 'turn-on-font-lock)
114
115 Alternatively, you can use Global Font Lock mode to automagically turn on Font
116 Lock mode in buffers whose major mode supports it and whose major mode is one
117 of `font-lock-global-modes'. For example, put in your ~/.emacs:
118
119 (global-font-lock-mode t)
120
121 Where major modes support different levels of fontification, you can use
122 the variable `font-lock-maximum-decoration' to specify which level you
123 generally prefer. When you turn Font Lock mode on/off the buffer is
124 fontified/defontified, though fontification occurs only if the buffer is
125 less than `font-lock-maximum-size'.
126
127 For example, to use maximum levels of fontification, put in your ~/.emacs:
128
129 (setq font-lock-maximum-decoration t)
130
131 To add your own highlighting for some major mode, and modify the highlighting
132 selected automatically via the variable `font-lock-maximum-decoration', you can
133 use `font-lock-add-keywords'.
134
135 To fontify a buffer, without turning on Font Lock mode and regardless of buffer
136 size, you can use \\[font-lock-fontify-buffer].
137
138 To fontify a block (the function or paragraph containing point, or a number of
139 lines around point), perhaps because modification on the current line caused
140 syntactic change on other lines, you can use \\[font-lock-fontify-block].
141
142 See the variable `font-lock-defaults-alist' for the Font Lock mode default
143 settings. You can set your own default settings for some mode, by setting a
144 buffer local value for `font-lock-defaults', via its mode hook.
145
146 The above is the default behavior of `font-lock-mode'; you may specify
147 your own function which is called when `font-lock-mode' is toggled via
148 `font-lock-function'. "
149 nil nil nil
150 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
151 ;; batch job) or if the buffer is invisible (the name starts with a space).
152 (when (or noninteractive (eq (aref (buffer-name) 0) ?\ ))
153 (setq font-lock-mode nil))
154 (funcall font-lock-function font-lock-mode)
155 ;; Arrange to unfontify this buffer if we change major mode later.
156 (if font-lock-mode
157 (add-hook 'change-major-mode-hook 'font-lock-change-mode nil t)
158 (remove-hook 'change-major-mode-hook 'font-lock-change-mode t))
159 (when font-lock-mode
160 (setq font-lock-mode-major-mode major-mode)))
161
162 ;; Get rid of fontification for the old major mode.
163 ;; We do this when changing major modes.
164 (defun font-lock-change-mode ()
165 (font-lock-mode -1))
166
167 (defun font-lock-defontify ()
168 "Clear out all `font-lock-face' properties in current buffer.
169 A major mode that uses `font-lock-face' properties might want to put
170 this function onto `change-major-mode-hook'."
171 (let ((modp (buffer-modified-p))
172 (inhibit-read-only t))
173 (save-restriction
174 (widen)
175 (remove-list-of-text-properties (point-min) (point-max)
176 '(font-lock-face)))
177 (restore-buffer-modified-p modp)))
178
179 (defvar font-lock-set-defaults)
180 (defun font-lock-default-function (mode)
181 ;; Turn on Font Lock mode.
182 (when mode
183 (set (make-local-variable 'char-property-alias-alist)
184 (copy-tree char-property-alias-alist))
185 ;; Add `font-lock-face' as an alias for the `face' property.
186 (let ((elt (assq 'face char-property-alias-alist)))
187 (if elt
188 (unless (memq 'font-lock-face (cdr elt))
189 (setcdr elt (nconc (cdr elt) (list 'font-lock-face))))
190 (push (list 'face 'font-lock-face) char-property-alias-alist))))
191 ;; Turn off Font Lock mode.
192 (unless mode
193 ;; Remove `font-lock-face' as an alias for the `face' property.
194 (set (make-local-variable 'char-property-alias-alist)
195 (copy-tree char-property-alias-alist))
196 (let ((elt (assq 'face char-property-alias-alist)))
197 (when elt
198 (setcdr elt (remq 'font-lock-face (cdr elt)))
199 (when (null (cdr elt))
200 (setq char-property-alias-alist
201 (delq elt char-property-alias-alist))))))
202
203 ;; Only do hard work if the mode has specified stuff in
204 ;; `font-lock-defaults'.
205 (when (or font-lock-defaults
206 (if (boundp 'font-lock-keywords) font-lock-keywords)
207 (with-no-warnings
208 (cdr (assq major-mode font-lock-defaults-alist)))
209 (and mode
210 (boundp 'font-lock-set-defaults)
211 font-lock-set-defaults
212 font-lock-mode-major-mode
213 (not (eq font-lock-mode-major-mode major-mode))))
214 (font-lock-mode-internal mode)))
215
216 (defun turn-on-font-lock ()
217 "Turn on Font Lock mode (only if the terminal can display it)."
218 (unless font-lock-mode
219 (font-lock-mode)))
220
221 ;;; Global Font Lock mode.
222
223 ;; A few people have hassled in the past for a way to make it easier to turn on
224 ;; Font Lock mode, without the user needing to know for which modes s/he has to
225 ;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always
226 ;; balked at that way, as I see it as just re-moulding the same problem in
227 ;; another form. That is; some person would still have to keep track of which
228 ;; modes (which may not even be distributed with Emacs) support Font Lock mode.
229 ;; The list would always be out of date. And that person might have to be me.
230
231 ;; Implementation.
232 ;;
233 ;; In a previous discussion the following hack came to mind. It is a gross
234 ;; hack, but it generally works. We use the convention that major modes start
235 ;; by calling the function `kill-all-local-variables', which in turn runs
236 ;; functions on the hook variable `change-major-mode-hook'. We attach our
237 ;; function `font-lock-change-major-mode' to that hook. Of course, when this
238 ;; hook is run, the major mode is in the process of being changed and we do not
239 ;; know what the final major mode will be. So, `font-lock-change-major-mode'
240 ;; only (a) notes the name of the current buffer, and (b) adds our function
241 ;; `turn-on-font-lock-if-enabled' to the hook variables
242 ;; `after-change-major-mode-hook' and `post-command-hook' (for modes
243 ;; that do not yet run `after-change-major-mode-hook'). By the time
244 ;; the functions on the first of these hooks to be run are run, the new major
245 ;; mode is assumed to be in place. This way we get a Font Lock function run
246 ;; when a major mode is turned on, without knowing major modes or their hooks.
247 ;;
248 ;; Naturally this requires that major modes run `kill-all-local-variables'
249 ;; and `after-change-major-mode-hook', as they are supposed to. For modes
250 ;; that do not run `after-change-major-mode-hook' yet, `post-command-hook'
251 ;; takes care of things if the mode is set directly or indirectly by
252 ;; an interactive command; however, problems can occur if the mode is
253 ;; set by a timer or process: in that case, proper handling of Font Lock mode
254 ;; may be delayed until the next interactive command.
255
256 ;; User interface.
257 ;;
258 ;; Although Global Font Lock mode is a pseudo-mode, I think that the user
259 ;; interface should conform to the usual Emacs convention for modes, i.e., a
260 ;; command to toggle the feature (`global-font-lock-mode') with a variable for
261 ;; finer control of the mode's behaviour (`font-lock-global-modes').
262 ;;
263 ;; The feature should not be enabled by loading font-lock.el, since other
264 ;; mechanisms for turning on Font Lock mode, such as M-x font-lock-mode RET or
265 ;; (add-hook 'c-mode-hook 'turn-on-font-lock), would cause Font Lock mode to be
266 ;; turned on everywhere. That would not be intuitive or informative because
267 ;; loading a file tells you nothing about the feature or how to control it. It
268 ;; would also be contrary to the Principle of Least Surprise. sm.
269
270 (defcustom font-lock-global-modes t
271 "*Modes for which Font Lock mode is automagically turned on.
272 Global Font Lock mode is controlled by the command `global-font-lock-mode'.
273 If nil, means no modes have Font Lock mode automatically turned on.
274 If t, all modes that support Font Lock mode have it automatically turned on.
275 If a list, it should be a list of `major-mode' symbol names for which Font Lock
276 mode should be automatically turned on. The sense of the list is negated if it
277 begins with `not'. For example:
278 (c-mode c++-mode)
279 means that Font Lock mode is turned on for buffers in C and C++ modes only."
280 :type '(choice (const :tag "none" nil)
281 (const :tag "all" t)
282 (set :menu-tag "mode specific" :tag "modes"
283 :value (not)
284 (const :tag "Except" not)
285 (repeat :inline t (symbol :tag "mode"))))
286 :group 'font-lock)
287
288 (defun turn-on-font-lock-if-enabled ()
289 (unless (and (eq (car-safe font-lock-global-modes) 'not)
290 (memq major-mode (cdr font-lock-global-modes)))
291 (let (inhibit-quit)
292 (turn-on-font-lock))))
293
294 (define-global-minor-mode global-font-lock-mode
295 font-lock-mode turn-on-font-lock-if-enabled
296 :extra-args (dummy)
297 :initialize 'custom-initialize-safe-default
298 :init-value (not (or noninteractive emacs-basic-display))
299 :group 'font-lock
300 :version "22.1")
301
302 ;;; End of Global Font Lock mode.
303
304 (provide 'font-core)
305
306 ;; arch-tag: f8c286e1-02f7-41d9-b89b-1b67780aed71
307 ;;; font-core.el ends here