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