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