Add arch taglines
[bpt/emacs.git] / lisp / font-core.el
CommitLineData
f62be99b
CW
1;;; font-core.el --- Core interface to font-lock
2
ebd872c8 3;; Copyright (C) 1992, 93, 94, 95, 96, 97, 98, 1999, 2000, 2001, 02, 2003
f62be99b
CW
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.
33Defaults should be of the form:
34
35 (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN ...)
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,
69`font-lock-syntactic-keywords'
f7abda1e 70and those for buffer-specialized fontification functions,
f62be99b
CW
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
ebd872c8 76(defvar font-lock-defaults-alist nil
f62be99b
CW
77 "Alist of fall-back Font Lock defaults for major modes.
78
f62be99b
CW
79Each item should be a list of the form:
80
81 (MAJOR-MODE . FONT-LOCK-DEFAULTS)
82
83where MAJOR-MODE is a symbol and FONT-LOCK-DEFAULTS is a list of default
84settings. See the variable `font-lock-defaults', which takes precedence.")
85(make-obsolete-variable 'font-lock-defaults-alist 'font-lock-defaults)
86
7c4ef9b3
CW
87(defvar font-lock-function 'font-lock-default-function
88 "A function which is called when `font-lock-mode' is toggled.
89It will be passed one argument, which is the current value of
90`font-lock-mode'.")
7c4ef9b3 91
f62be99b
CW
92(define-minor-mode font-lock-mode
93 "Toggle Font Lock mode.
94With arg, turn Font Lock mode off if and only if arg is a non-positive
95number; if arg is nil, toggle Font Lock mode; anything else turns Font
96Lock on.
97\(Font Lock is also known as \"syntax highlighting\".)
98
99When Font Lock mode is enabled, text is fontified as you type it:
100
101 - Comments are displayed in `font-lock-comment-face';
102 - Strings are displayed in `font-lock-string-face';
103 - Certain other expressions are displayed in other faces according to the
104 value of the variable `font-lock-keywords'.
105
106To customize the faces (colors, fonts, etc.) used by Font Lock for
107fontifying different parts of buffer text, use \\[customize-face].
108
109You can enable Font Lock mode in any major mode automatically by turning on in
110the major mode's hook. For example, put in your ~/.emacs:
111
112 (add-hook 'c-mode-hook 'turn-on-font-lock)
113
114Alternatively, you can use Global Font Lock mode to automagically turn on Font
115Lock mode in buffers whose major mode supports it and whose major mode is one
116of `font-lock-global-modes'. For example, put in your ~/.emacs:
117
118 (global-font-lock-mode t)
119
120There are a number of support modes that may be used to speed up Font Lock mode
121in various ways, specified via the variable `font-lock-support-mode'. Where
122major modes support different levels of fontification, you can use the variable
123`font-lock-maximum-decoration' to specify which level you generally prefer.
124When you turn Font Lock mode on/off the buffer is fontified/defontified, though
125fontification occurs only if the buffer is less than `font-lock-maximum-size'.
126
127For example, to specify that Font Lock mode use use Lazy Lock mode as a support
128mode and use maximum levels of fontification, put in your ~/.emacs:
129
130 (setq font-lock-support-mode 'lazy-lock-mode)
131 (setq font-lock-maximum-decoration t)
132
133To add your own highlighting for some major mode, and modify the highlighting
134selected automatically via the variable `font-lock-maximum-decoration', you can
135use `font-lock-add-keywords'.
136
137To fontify a buffer, without turning on Font Lock mode and regardless of buffer
138size, you can use \\[font-lock-fontify-buffer].
139
140To fontify a block (the function or paragraph containing point, or a number of
141lines around point), perhaps because modification on the current line caused
142syntactic change on other lines, you can use \\[font-lock-fontify-block].
143
144See the variable `font-lock-defaults-alist' for the Font Lock mode default
145settings. You can set your own default settings for some mode, by setting a
7c4ef9b3
CW
146buffer local value for `font-lock-defaults', via its mode hook.
147
148The above is the default behavior of `font-lock-mode'; you may specify
149your own function which is called when `font-lock-mode' is toggled via
150`font-lock-function'. "
f62be99b
CW
151 nil nil nil
152 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
153 ;; batch job) or if the buffer is invisible (the name starts with a space).
154 (when (or noninteractive (eq (aref (buffer-name) 0) ?\ ))
155 (setq font-lock-mode nil))
8e97108f
RS
156 (funcall font-lock-function font-lock-mode)
157 ;; Arrange to unfontify this buffer if we change major mode later.
158 (if font-lock-mode
2dc20ca8
RS
159 (add-hook 'change-major-mode-hook 'font-lock-change-mode nil t)
160 (remove-hook 'change-major-mode-hook 'font-lock-change-mode t)))
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 ()
40e2bbe9
SM
165 (font-lock-mode -1))
166
67c9f58d
RS
167(defun font-lock-defontify ()
168 "Clear out all `font-lock-face' properties in current buffer.
e350babd 169A major mode that uses `font-lock-face' properties might want to put
67c9f58d
RS
170this 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)))
bb125ff0 177 (restore-buffer-modified-p modp)))
67c9f58d 178
40e2bbe9 179(defun font-lock-default-function (mode)
f62be99b 180 ;; Turn on Font Lock mode.
40e2bbe9 181 (when mode
1b6adddb
CW
182 (set (make-local-variable 'char-property-alias-alist)
183 (copy-tree char-property-alias-alist))
184 ;; Add `font-lock-face' as an alias for the `face' property.
185 (let ((elt (assq 'face char-property-alias-alist)))
186 (if elt
187 (unless (memq 'font-lock-face (cdr elt))
188 (setcdr elt (nconc (cdr elt) (list 'font-lock-face))))
e350babd 189 (push (list 'face 'font-lock-face) char-property-alias-alist))))
f62be99b 190 ;; Turn off Font Lock mode.
40e2bbe9 191 (unless mode
1b6adddb
CW
192 ;; Remove `font-lock-face' as an alias for the `face' property.
193 (set (make-local-variable 'char-property-alias-alist)
194 (copy-tree char-property-alias-alist))
195 (let ((elt (assq 'face char-property-alias-alist)))
196 (when elt
197 (setcdr elt (remq 'font-lock-face (cdr elt)))
198 (when (null (cdr elt))
e350babd
SM
199 (setq char-property-alias-alist
200 (delq elt char-property-alias-alist))))))
201
202 ;; Only do hard work if the mode has specified stuff in
203 ;; `font-lock-defaults'.
204 (when (or font-lock-defaults
205 (cdr (assq major-mode font-lock-defaults-alist)))
206 (font-lock-mode-internal mode)))
71296446 207
f62be99b
CW
208(defun turn-on-font-lock ()
209 "Turn on Font Lock mode (only if the terminal can display it)."
210 (unless font-lock-mode
211 (font-lock-mode)))
212
f62be99b
CW
213;;; Global Font Lock mode.
214
215;; A few people have hassled in the past for a way to make it easier to turn on
216;; Font Lock mode, without the user needing to know for which modes s/he has to
217;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always
218;; balked at that way, as I see it as just re-moulding the same problem in
219;; another form. That is; some person would still have to keep track of which
220;; modes (which may not even be distributed with Emacs) support Font Lock mode.
221;; The list would always be out of date. And that person might have to be me.
222
223;; Implementation.
224;;
225;; In a previous discussion the following hack came to mind. It is a gross
226;; hack, but it generally works. We use the convention that major modes start
227;; by calling the function `kill-all-local-variables', which in turn runs
228;; functions on the hook variable `change-major-mode-hook'. We attach our
229;; function `font-lock-change-major-mode' to that hook. Of course, when this
230;; hook is run, the major mode is in the process of being changed and we do not
231;; know what the final major mode will be. So, `font-lock-change-major-mode'
232;; only (a) notes the name of the current buffer, and (b) adds our function
1a4914f3 233;; `turn-on-font-lock-if-enabled' to the hook variables `find-file-hook' and
f62be99b
CW
234;; `post-command-hook' (for buffers that are not visiting files). By the time
235;; the functions on the first of these hooks to be run are run, the new major
236;; mode is assumed to be in place. This way we get a Font Lock function run
237;; when a major mode is turned on, without knowing major modes or their hooks.
238;;
239;; Naturally this requires that (a) major modes run `kill-all-local-variables',
240;; as they are supposed to do, and (b) the major mode is in place after the
241;; file is visited or the command that ran `kill-all-local-variables' has
242;; finished, whichever the sooner. Arguably, any major mode that does not
243;; follow the convension (a) is broken, and I can't think of any reason why (b)
244;; would not be met (except `gnudoit' on non-files). However, it is not clean.
245;;
246;; Probably the cleanest solution is to have each major mode function run some
247;; hook, e.g., `major-mode-hook', but maybe implementing that change is
248;; impractical. I am personally against making `setq' a macro or be advised,
249;; or have a special function such as `set-major-mode', but maybe someone can
250;; come up with another solution?
251
252;; User interface.
253;;
254;; Although Global Font Lock mode is a pseudo-mode, I think that the user
255;; interface should conform to the usual Emacs convention for modes, i.e., a
256;; command to toggle the feature (`global-font-lock-mode') with a variable for
257;; finer control of the mode's behaviour (`font-lock-global-modes').
258;;
259;; The feature should not be enabled by loading font-lock.el, since other
260;; mechanisms for turning on Font Lock mode, such as M-x font-lock-mode RET or
261;; (add-hook 'c-mode-hook 'turn-on-font-lock), would cause Font Lock mode to be
262;; turned on everywhere. That would not be intuitive or informative because
263;; loading a file tells you nothing about the feature or how to control it. It
264;; would also be contrary to the Principle of Least Surprise. sm.
265
266(defcustom font-lock-global-modes t
267 "*Modes for which Font Lock mode is automagically turned on.
268Global Font Lock mode is controlled by the command `global-font-lock-mode'.
269If nil, means no modes have Font Lock mode automatically turned on.
270If t, all modes that support Font Lock mode have it automatically turned on.
271If a list, it should be a list of `major-mode' symbol names for which Font Lock
272mode should be automatically turned on. The sense of the list is negated if it
273begins with `not'. For example:
274 (c-mode c++-mode)
275means that Font Lock mode is turned on for buffers in C and C++ modes only."
276 :type '(choice (const :tag "none" nil)
277 (const :tag "all" t)
278 (set :menu-tag "mode specific" :tag "modes"
279 :value (not)
280 (const :tag "Except" not)
281 (repeat :inline t (symbol :tag "mode"))))
282 :group 'font-lock)
283
284(defun turn-on-font-lock-if-enabled ()
61fd2c68
CW
285 (unless (and (eq (car-safe font-lock-global-modes) 'not)
286 (memq major-mode (cdr font-lock-global-modes)))
f62be99b
CW
287 (let (inhibit-quit)
288 (turn-on-font-lock))))
289
290(easy-mmode-define-global-mode
291 global-font-lock-mode font-lock-mode turn-on-font-lock-if-enabled
292 :extra-args (dummy))
293
294;;; End of Global Font Lock mode.
295
296(provide 'font-core)
297
298;;; font-core.el ends here
299
ab5796a9 300;;; arch-tag: f8c286e1-02f7-41d9-b89b-1b67780aed71