merge trunk
[emacs.git] / lisp / font-core.el
blob5f8af5a5215b0f3b80050f64b676173c4e2f919e
1 ;;; font-core.el --- Core interface to font-lock
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 ;; Free Software Foundation, Inc.
7 ;; Maintainer: FSF
8 ;; Keywords: languages, faces
9 ;; Package: emacs
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Code:
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:
35 (KEYWORDS [KEYWORDS-ONLY [CASE-FOLD [SYNTAX-ALIST [SYNTAX-BEGIN ...]]]])
37 KEYWORDS may be a symbol (a variable or function whose value is the keywords
38 to use for fontification) or a list of symbols (specifying different levels
39 of fontification).
41 If KEYWORDS-ONLY is non-nil, syntactic fontification (strings and
42 comments) is not performed.
44 If CASE-FOLD is non-nil, the case of the keywords is ignored when fontifying.
46 If 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
48 keyword and syntactic fontification (see `modify-syntax-entry').
50 If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
51 backwards outside any enclosing syntactic block, for syntactic fontification.
52 Typical values are `beginning-of-line' (i.e., the start of the line is known to
53 be 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
55 known to move outside a syntactic block). If nil, the beginning of the buffer
56 is used as a position outside of a syntactic block, in the worst case.
58 \(See also Info node `(elisp)Font Lock Basics'.)
60 These 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.
65 Further item elements are alists of the form (VARIABLE . VALUE) and are in no
66 particular order. Each VARIABLE is made buffer-local before set to VALUE.
68 Currently, appropriate variables include `font-lock-mark-block-function'.
69 If this is non-nil, it should be a function with no args used to mark any
70 enclosing block of text, for fontification via \\[font-lock-fontify-block].
71 Typical values are `mark-defun' for programming modes or `mark-paragraph' for
72 textual modes (i.e., the mode-dependent function is known to put point and mark
73 around a text block relevant to that mode).
75 Other variables include that for syntactic keyword fontification,
76 `font-lock-syntactic-keywords' and those for buffer-specialized fontification
77 functions, `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'.")
80 ;;;###autoload
81 (put 'font-lock-defaults 'risky-local-variable t)
82 (make-variable-buffer-local 'font-lock-defaults)
84 (defvar font-lock-function 'font-lock-default-function
85 "A function which is called when `font-lock-mode' is toggled.
86 It will be passed one argument, which is the current value of
87 `font-lock-mode'.")
89 ;; The mode for which font-lock was initialized, or nil if none.
90 (defvar font-lock-major-mode)
91 (define-minor-mode font-lock-mode
92 "Toggle Font Lock mode.
93 With arg, turn Font Lock mode off if and only if arg is a non-positive
94 number; if arg is nil, toggle Font Lock mode; anything else turns Font
95 Lock on.
96 \(Font Lock is also known as \"syntax highlighting\".)
98 When Font Lock mode is enabled, text is fontified as you type it:
100 - Comments are displayed in `font-lock-comment-face';
101 - Strings are displayed in `font-lock-string-face';
102 - Certain other expressions are displayed in other faces according to the
103 value of the variable `font-lock-keywords'.
105 To customize the faces (colors, fonts, etc.) used by Font Lock for
106 fontifying different parts of buffer text, use \\[customize-face].
108 You can enable Font Lock mode in any major mode automatically by turning on in
109 the major mode's hook. For example, put in your ~/.emacs:
111 (add-hook 'c-mode-hook 'turn-on-font-lock)
113 Alternatively, you can use Global Font Lock mode to automagically turn on Font
114 Lock mode in buffers whose major mode supports it and whose major mode is one
115 of `font-lock-global-modes'. For example, put in your ~/.emacs:
117 (global-font-lock-mode t)
119 Where major modes support different levels of fontification, you can use
120 the variable `font-lock-maximum-decoration' to specify which level you
121 generally prefer. When you turn Font Lock mode on/off the buffer is
122 fontified/defontified, though fontification occurs only if the buffer is
123 less than `font-lock-maximum-size'.
125 To add your own highlighting for some major mode, and modify the highlighting
126 selected automatically via the variable `font-lock-maximum-decoration', you can
127 use `font-lock-add-keywords'.
129 To fontify a buffer, without turning on Font Lock mode and regardless of buffer
130 size, you can use \\[font-lock-fontify-buffer].
132 To fontify a block (the function or paragraph containing point, or a number of
133 lines around point), perhaps because modification on the current line caused
134 syntactic change on other lines, you can use \\[font-lock-fontify-block].
136 You can set your own default settings for some mode, by setting a
137 buffer local value for `font-lock-defaults', via its mode hook.
139 The above is the default behavior of `font-lock-mode'; you may specify
140 your own function which is called when `font-lock-mode' is toggled via
141 `font-lock-function'. "
142 nil nil nil
143 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
144 ;; batch job) or if the buffer is invisible (the name starts with a space).
145 (when (or noninteractive (eq (aref (buffer-name) 0) ?\s))
146 (setq font-lock-mode nil))
147 (funcall font-lock-function font-lock-mode)
148 ;; Arrange to unfontify this buffer if we change major mode later.
149 (if font-lock-mode
150 (add-hook 'change-major-mode-hook 'font-lock-change-mode nil t)
151 (remove-hook 'change-major-mode-hook 'font-lock-change-mode t)))
153 ;; Get rid of fontification for the old major mode.
154 ;; We do this when changing major modes.
155 (defun font-lock-change-mode ()
156 (font-lock-mode -1))
158 (defun font-lock-defontify ()
159 "Clear out all `font-lock-face' properties in current buffer.
160 A major mode that uses `font-lock-face' properties might want to put
161 this function onto `change-major-mode-hook'."
162 (let ((modp (buffer-modified-p))
163 (inhibit-read-only t))
164 (save-restriction
165 (widen)
166 (remove-list-of-text-properties (point-min) (point-max)
167 '(font-lock-face)))
168 (restore-buffer-modified-p modp)))
170 (defvar font-lock-set-defaults)
171 (defun font-lock-default-function (mode)
172 ;; Turn on Font Lock mode.
173 (when mode
174 (set (make-local-variable 'char-property-alias-alist)
175 (copy-tree char-property-alias-alist))
176 ;; Add `font-lock-face' as an alias for the `face' property.
177 (let ((elt (assq 'face char-property-alias-alist)))
178 (if elt
179 (unless (memq 'font-lock-face (cdr elt))
180 (setcdr elt (nconc (cdr elt) (list 'font-lock-face))))
181 (push (list 'face 'font-lock-face) char-property-alias-alist))))
182 ;; Turn off Font Lock mode.
183 (unless mode
184 ;; Remove `font-lock-face' as an alias for the `face' property.
185 (set (make-local-variable 'char-property-alias-alist)
186 (copy-tree char-property-alias-alist))
187 (let ((elt (assq 'face char-property-alias-alist)))
188 (when elt
189 (setcdr elt (remq 'font-lock-face (cdr elt)))
190 (when (null (cdr elt))
191 (setq char-property-alias-alist
192 (delq elt char-property-alias-alist))))))
194 ;; Only do hard work if the mode has specified stuff in
195 ;; `font-lock-defaults'.
196 (when (or font-lock-defaults
197 (if (boundp 'font-lock-keywords) font-lock-keywords)
198 (and mode
199 (boundp 'font-lock-set-defaults)
200 font-lock-set-defaults
201 font-lock-major-mode
202 (not (eq font-lock-major-mode major-mode))))
203 (font-lock-mode-internal mode)))
205 (defun turn-on-font-lock ()
206 "Turn on Font Lock mode (only if the terminal can display it)."
207 (unless font-lock-mode
208 (font-lock-mode)))
210 ;;; Global Font Lock mode.
212 ;; A few people have hassled in the past for a way to make it easier to turn on
213 ;; Font Lock mode, without the user needing to know for which modes s/he has to
214 ;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always
215 ;; balked at that way, as I see it as just re-moulding the same problem in
216 ;; another form. That is; some person would still have to keep track of which
217 ;; modes (which may not even be distributed with Emacs) support Font Lock mode.
218 ;; The list would always be out of date. And that person might have to be me.
220 ;; Implementation.
222 ;; In a previous discussion the following hack came to mind. It is a gross
223 ;; hack, but it generally works. We use the convention that major modes start
224 ;; by calling the function `kill-all-local-variables', which in turn runs
225 ;; functions on the hook variable `change-major-mode-hook'. We attach our
226 ;; function `font-lock-change-major-mode' to that hook. Of course, when this
227 ;; hook is run, the major mode is in the process of being changed and we do not
228 ;; know what the final major mode will be. So, `font-lock-change-major-mode'
229 ;; only (a) notes the name of the current buffer, and (b) adds our function
230 ;; `turn-on-font-lock-if-desired' to the hook variables
231 ;; `after-change-major-mode-hook' and `post-command-hook' (for modes
232 ;; that do not yet run `after-change-major-mode-hook'). By the time
233 ;; the functions on the first of these hooks to be run are run, the new major
234 ;; mode is assumed to be in place. This way we get a Font Lock function run
235 ;; when a major mode is turned on, without knowing major modes or their hooks.
237 ;; Naturally this requires that major modes run `kill-all-local-variables'
238 ;; and `after-change-major-mode-hook', as they are supposed to. For modes
239 ;; that do not run `after-change-major-mode-hook' yet, `post-command-hook'
240 ;; takes care of things if the mode is set directly or indirectly by
241 ;; an interactive command; however, problems can occur if the mode is
242 ;; set by a timer or process: in that case, proper handling of Font Lock mode
243 ;; may be delayed until the next interactive command.
245 ;; User interface.
247 ;; Although Global Font Lock mode is a pseudo-mode, I think that the user
248 ;; interface should conform to the usual Emacs convention for modes, i.e., a
249 ;; command to toggle the feature (`global-font-lock-mode') with a variable for
250 ;; finer control of the mode's behavior (`font-lock-global-modes').
252 ;; The feature should not be enabled by loading font-lock.el, since other
253 ;; mechanisms for turning on Font Lock mode, such as M-x font-lock-mode RET or
254 ;; (add-hook 'c-mode-hook 'turn-on-font-lock), would cause Font Lock mode to be
255 ;; turned on everywhere. That would not be intuitive or informative because
256 ;; loading a file tells you nothing about the feature or how to control it. It
257 ;; would also be contrary to the Principle of Least Surprise. sm.
259 (defcustom font-lock-global-modes t
260 "Modes for which Font Lock mode is automagically turned on.
261 Global Font Lock mode is controlled by the command `global-font-lock-mode'.
262 If nil, means no modes have Font Lock mode automatically turned on.
263 If t, all modes that support Font Lock mode have it automatically turned on.
264 If a list, it should be a list of `major-mode' symbol names for which Font Lock
265 mode should be automatically turned on. The sense of the list is negated if it
266 begins with `not'. For example:
267 (c-mode c++-mode)
268 means that Font Lock mode is turned on for buffers in C and C++ modes only."
269 :type '(choice (const :tag "none" nil)
270 (const :tag "all" t)
271 (set :menu-tag "mode specific" :tag "modes"
272 :value (not)
273 (const :tag "Except" not)
274 (repeat :inline t (symbol :tag "mode"))))
275 :group 'font-lock)
277 (defun turn-on-font-lock-if-desired ()
278 (when (cond ((eq font-lock-global-modes t)
280 ((eq (car-safe font-lock-global-modes) 'not)
281 (not (memq major-mode (cdr font-lock-global-modes))))
282 (t (memq major-mode font-lock-global-modes)))
283 (let (inhibit-quit)
284 (turn-on-font-lock))))
286 (define-globalized-minor-mode global-font-lock-mode
287 font-lock-mode turn-on-font-lock-if-desired
288 ;; What was this :extra-args thingy for? --Stef
289 ;; :extra-args (dummy)
290 :initialize 'custom-initialize-delay
291 :init-value (not (or noninteractive emacs-basic-display))
292 :group 'font-lock
293 :version "22.1")
295 ;;; End of Global Font Lock mode.
297 (provide 'font-core)
299 ;;; font-core.el ends here