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