(read_filtered_event): Remove `register' qualifier because it
[bpt/emacs.git] / lisp / font-core.el
CommitLineData
f62be99b
CW
1;;; font-core.el --- Core interface to font-lock
2
30b940a7 3;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
aaef169d 4;; 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
f62be99b
CW
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
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
f62be99b
CW
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.
33Defaults should be of the form:
34
77f5a70d 35 (KEYWORDS [KEYWORDS-ONLY [CASE-FOLD [SYNTAX-ALIST [SYNTAX-BEGIN ...]]]])
f62be99b
CW
36
37KEYWORDS may be a symbol (a variable or function whose value is the keywords to
38use for fontification) or a list of symbols. If KEYWORDS-ONLY is non-nil,
39syntactic fontification (strings and comments) is not performed.
40If CASE-FOLD is non-nil, the case of the keywords is ignored when fontifying.
41If 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
43keyword and syntactic fontification (see `modify-syntax-entry').
44
45If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
46backwards outside any enclosing syntactic block, for syntactic fontification.
47Typical values are `beginning-of-line' (i.e., the start of the line is known to
48be 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
50known to move outside a syntactic block). If nil, the beginning of the buffer
51is used as a position outside of a syntactic block, in the worst case.
52
53These 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
58Further item elements are alists of the form (VARIABLE . VALUE) and are in no
59particular order. Each VARIABLE is made buffer-local before set to VALUE.
60
61Currently, appropriate variables include `font-lock-mark-block-function'.
62If this is non-nil, it should be a function with no args used to mark any
63enclosing block of text, for fontification via \\[font-lock-fontify-block].
64Typical values are `mark-defun' for programming modes or `mark-paragraph' for
65textual modes (i.e., the mode-dependent function is known to put point and mark
66around a text block relevant to that mode).
67
68Other variables include that for syntactic keyword fontification,
77f5a70d
LK
69`font-lock-syntactic-keywords' and those for buffer-specialized fontification
70functions, `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'.")
f62be99b
CW
73(make-variable-buffer-local 'font-lock-defaults)
74
ebd872c8 75(defvar font-lock-defaults-alist nil
f62be99b
CW
76 "Alist of fall-back Font Lock defaults for major modes.
77
f62be99b
CW
78Each item should be a list of the form:
79
80 (MAJOR-MODE . FONT-LOCK-DEFAULTS)
81
82where MAJOR-MODE is a symbol and FONT-LOCK-DEFAULTS is a list of default
83settings. See the variable `font-lock-defaults', which takes precedence.")
84(make-obsolete-variable 'font-lock-defaults-alist 'font-lock-defaults)
85
ef008321
AM
86(defvar font-lock-extend-region-function nil
87 "A function that determines the region to fontify after a change.
88
89This buffer-local variable is either nil, or is a function that determines the
90region to fontify. It is usually set by the major mode. The currently active
91font-lock after-change function calls this function after each buffer change.
92
93The function is given three parameters, the standard BEG, END, and OLD-LEN
94from after-change-functions. It should return either a cons of the beginning
95and end buffer positions \(in that order) of the region to fontify, or nil
96\(which directs the caller to fontify a default region). This function need
97not preserve point or the match-data, but must preserve the current
98restriction. The region it returns may start or end in the middle of a
99line.")
100(make-variable-buffer-local 'font-lock-extend-region-function)
101
102(defun font-lock-extend-region (beg end old-len)
103 "Determine the region to fontify after a buffer change.
104
105BEG END and OLD-LEN are the standard parameters from after-change-functions.
106The return value is either nil \(which directs the caller to chose the region
107itself), or a cons of the beginning and end \(in that order) of the region.
108The region returned may start or end in the middle of a line."
109 (if font-lock-extend-region-function
110 (save-match-data
111 (save-excursion
112 (funcall font-lock-extend-region-function beg end old-len)))))
113
7c4ef9b3
CW
114(defvar font-lock-function 'font-lock-default-function
115 "A function which is called when `font-lock-mode' is toggled.
116It will be passed one argument, which is the current value of
117`font-lock-mode'.")
7c4ef9b3 118
642b63e8
LT
119;; The mode for which font-lock was initialized, or nil if none.
120(defvar font-lock-mode-major-mode)
f62be99b
CW
121(define-minor-mode font-lock-mode
122 "Toggle Font Lock mode.
123With arg, turn Font Lock mode off if and only if arg is a non-positive
124number; if arg is nil, toggle Font Lock mode; anything else turns Font
125Lock on.
126\(Font Lock is also known as \"syntax highlighting\".)
127
128When Font Lock mode is enabled, text is fontified as you type it:
129
130 - Comments are displayed in `font-lock-comment-face';
131 - Strings are displayed in `font-lock-string-face';
132 - Certain other expressions are displayed in other faces according to the
133 value of the variable `font-lock-keywords'.
134
135To customize the faces (colors, fonts, etc.) used by Font Lock for
136fontifying different parts of buffer text, use \\[customize-face].
137
138You can enable Font Lock mode in any major mode automatically by turning on in
139the major mode's hook. For example, put in your ~/.emacs:
140
141 (add-hook 'c-mode-hook 'turn-on-font-lock)
142
143Alternatively, you can use Global Font Lock mode to automagically turn on Font
144Lock mode in buffers whose major mode supports it and whose major mode is one
145of `font-lock-global-modes'. For example, put in your ~/.emacs:
146
147 (global-font-lock-mode t)
148
ea5b829c
RF
149Where major modes support different levels of fontification, you can use
150the variable `font-lock-maximum-decoration' to specify which level you
151generally prefer. When you turn Font Lock mode on/off the buffer is
152fontified/defontified, though fontification occurs only if the buffer is
153less than `font-lock-maximum-size'.
f62be99b 154
ea5b829c 155For example, to use maximum levels of fontification, put in your ~/.emacs:
f62be99b 156
f62be99b
CW
157 (setq font-lock-maximum-decoration t)
158
159To add your own highlighting for some major mode, and modify the highlighting
160selected automatically via the variable `font-lock-maximum-decoration', you can
161use `font-lock-add-keywords'.
162
163To fontify a buffer, without turning on Font Lock mode and regardless of buffer
164size, you can use \\[font-lock-fontify-buffer].
165
166To fontify a block (the function or paragraph containing point, or a number of
167lines around point), perhaps because modification on the current line caused
168syntactic change on other lines, you can use \\[font-lock-fontify-block].
169
170See the variable `font-lock-defaults-alist' for the Font Lock mode default
171settings. You can set your own default settings for some mode, by setting a
7c4ef9b3
CW
172buffer local value for `font-lock-defaults', via its mode hook.
173
174The above is the default behavior of `font-lock-mode'; you may specify
175your own function which is called when `font-lock-mode' is toggled via
176`font-lock-function'. "
9324f671 177 nil nil nil
f62be99b
CW
178 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
179 ;; batch job) or if the buffer is invisible (the name starts with a space).
180 (when (or noninteractive (eq (aref (buffer-name) 0) ?\ ))
181 (setq font-lock-mode nil))
8e97108f
RS
182 (funcall font-lock-function font-lock-mode)
183 ;; Arrange to unfontify this buffer if we change major mode later.
184 (if font-lock-mode
2dc20ca8 185 (add-hook 'change-major-mode-hook 'font-lock-change-mode nil t)
642b63e8
LT
186 (remove-hook 'change-major-mode-hook 'font-lock-change-mode t))
187 (when font-lock-mode
188 (setq font-lock-mode-major-mode major-mode)))
2dc20ca8
RS
189
190;; Get rid of fontification for the old major mode.
191;; We do this when changing major modes.
192(defun font-lock-change-mode ()
40e2bbe9
SM
193 (font-lock-mode -1))
194
67c9f58d
RS
195(defun font-lock-defontify ()
196 "Clear out all `font-lock-face' properties in current buffer.
e350babd 197A major mode that uses `font-lock-face' properties might want to put
67c9f58d
RS
198this function onto `change-major-mode-hook'."
199 (let ((modp (buffer-modified-p))
200 (inhibit-read-only t))
201 (save-restriction
202 (widen)
203 (remove-list-of-text-properties (point-min) (point-max)
204 '(font-lock-face)))
bb125ff0 205 (restore-buffer-modified-p modp)))
67c9f58d 206
642b63e8 207(defvar font-lock-set-defaults)
40e2bbe9 208(defun font-lock-default-function (mode)
f62be99b 209 ;; Turn on Font Lock mode.
40e2bbe9 210 (when mode
1b6adddb
CW
211 (set (make-local-variable 'char-property-alias-alist)
212 (copy-tree char-property-alias-alist))
213 ;; Add `font-lock-face' as an alias for the `face' property.
214 (let ((elt (assq 'face char-property-alias-alist)))
215 (if elt
216 (unless (memq 'font-lock-face (cdr elt))
217 (setcdr elt (nconc (cdr elt) (list 'font-lock-face))))
e350babd 218 (push (list 'face 'font-lock-face) char-property-alias-alist))))
f62be99b 219 ;; Turn off Font Lock mode.
40e2bbe9 220 (unless mode
1b6adddb
CW
221 ;; Remove `font-lock-face' as an alias for the `face' property.
222 (set (make-local-variable 'char-property-alias-alist)
223 (copy-tree char-property-alias-alist))
224 (let ((elt (assq 'face char-property-alias-alist)))
225 (when elt
226 (setcdr elt (remq 'font-lock-face (cdr elt)))
227 (when (null (cdr elt))
e350babd
SM
228 (setq char-property-alias-alist
229 (delq elt char-property-alias-alist))))))
230
231 ;; Only do hard work if the mode has specified stuff in
232 ;; `font-lock-defaults'.
233 (when (or font-lock-defaults
642b63e8 234 (if (boundp 'font-lock-keywords) font-lock-keywords)
723c911c 235 (with-no-warnings
642b63e8
LT
236 (cdr (assq major-mode font-lock-defaults-alist)))
237 (and mode
238 (boundp 'font-lock-set-defaults)
239 font-lock-set-defaults
240 font-lock-mode-major-mode
241 (not (eq font-lock-mode-major-mode major-mode))))
e350babd 242 (font-lock-mode-internal mode)))
71296446 243
f62be99b
CW
244(defun turn-on-font-lock ()
245 "Turn on Font Lock mode (only if the terminal can display it)."
246 (unless font-lock-mode
247 (font-lock-mode)))
248
f62be99b
CW
249;;; Global Font Lock mode.
250
251;; A few people have hassled in the past for a way to make it easier to turn on
252;; Font Lock mode, without the user needing to know for which modes s/he has to
253;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always
254;; balked at that way, as I see it as just re-moulding the same problem in
255;; another form. That is; some person would still have to keep track of which
256;; modes (which may not even be distributed with Emacs) support Font Lock mode.
257;; The list would always be out of date. And that person might have to be me.
258
259;; Implementation.
260;;
261;; In a previous discussion the following hack came to mind. It is a gross
262;; hack, but it generally works. We use the convention that major modes start
263;; by calling the function `kill-all-local-variables', which in turn runs
264;; functions on the hook variable `change-major-mode-hook'. We attach our
265;; function `font-lock-change-major-mode' to that hook. Of course, when this
266;; hook is run, the major mode is in the process of being changed and we do not
267;; know what the final major mode will be. So, `font-lock-change-major-mode'
268;; only (a) notes the name of the current buffer, and (b) adds our function
21178fee
LT
269;; `turn-on-font-lock-if-enabled' to the hook variables
270;; `after-change-major-mode-hook' and `post-command-hook' (for modes
271;; that do not yet run `after-change-major-mode-hook'). By the time
f62be99b
CW
272;; the functions on the first of these hooks to be run are run, the new major
273;; mode is assumed to be in place. This way we get a Font Lock function run
274;; when a major mode is turned on, without knowing major modes or their hooks.
275;;
21178fee
LT
276;; Naturally this requires that major modes run `kill-all-local-variables'
277;; and `after-change-major-mode-hook', as they are supposed to. For modes
278;; that do not run `after-change-major-mode-hook' yet, `post-command-hook'
279;; takes care of things if the mode is set directly or indirectly by
280;; an interactive command; however, problems can occur if the mode is
281;; set by a timer or process: in that case, proper handling of Font Lock mode
282;; may be delayed until the next interactive command.
f62be99b
CW
283
284;; User interface.
285;;
286;; Although Global Font Lock mode is a pseudo-mode, I think that the user
287;; interface should conform to the usual Emacs convention for modes, i.e., a
288;; command to toggle the feature (`global-font-lock-mode') with a variable for
289;; finer control of the mode's behaviour (`font-lock-global-modes').
290;;
291;; The feature should not be enabled by loading font-lock.el, since other
292;; mechanisms for turning on Font Lock mode, such as M-x font-lock-mode RET or
293;; (add-hook 'c-mode-hook 'turn-on-font-lock), would cause Font Lock mode to be
294;; turned on everywhere. That would not be intuitive or informative because
295;; loading a file tells you nothing about the feature or how to control it. It
296;; would also be contrary to the Principle of Least Surprise. sm.
297
298(defcustom font-lock-global-modes t
299 "*Modes for which Font Lock mode is automagically turned on.
300Global Font Lock mode is controlled by the command `global-font-lock-mode'.
301If nil, means no modes have Font Lock mode automatically turned on.
302If t, all modes that support Font Lock mode have it automatically turned on.
303If a list, it should be a list of `major-mode' symbol names for which Font Lock
304mode should be automatically turned on. The sense of the list is negated if it
305begins with `not'. For example:
306 (c-mode c++-mode)
307means that Font Lock mode is turned on for buffers in C and C++ modes only."
308 :type '(choice (const :tag "none" nil)
309 (const :tag "all" t)
310 (set :menu-tag "mode specific" :tag "modes"
311 :value (not)
312 (const :tag "Except" not)
313 (repeat :inline t (symbol :tag "mode"))))
314 :group 'font-lock)
315
316(defun turn-on-font-lock-if-enabled ()
61fd2c68
CW
317 (unless (and (eq (car-safe font-lock-global-modes) 'not)
318 (memq major-mode (cdr font-lock-global-modes)))
f62be99b
CW
319 (let (inhibit-quit)
320 (turn-on-font-lock))))
321
3ff522d9
JL
322(define-global-minor-mode global-font-lock-mode
323 font-lock-mode turn-on-font-lock-if-enabled
324 :extra-args (dummy)
325 :initialize 'custom-initialize-safe-default
326 :init-value (not (or noninteractive emacs-basic-display))
327 :group 'font-lock
328 :version "22.1")
f62be99b
CW
329
330;;; End of Global Font Lock mode.
331
332(provide 'font-core)
333
30b940a7 334;; arch-tag: f8c286e1-02f7-41d9-b89b-1b67780aed71
f62be99b 335;;; font-core.el ends here