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