(Search-based Fontification): Fix typo.
[bpt/emacs.git] / lispref / modes.texi
CommitLineData
a44af9f2
RS
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
f8cecb20 3@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2003
177c0ea7 4@c Free Software Foundation, Inc.
a44af9f2
RS
5@c See the file elisp.texi for copying conditions.
6@setfilename ../info/modes
3ebe0346 7@node Modes, Documentation, Keymaps, Top
a44af9f2
RS
8@chapter Major and Minor Modes
9@cindex mode
10
11 A @dfn{mode} is a set of definitions that customize Emacs and can be
12turned on and off while you edit. There are two varieties of modes:
13@dfn{major modes}, which are mutually exclusive and used for editing
14particular kinds of text, and @dfn{minor modes}, which provide features
15that users can enable individually.
16
17 This chapter describes how to write both major and minor modes, how to
18indicate them in the mode line, and how they run hooks supplied by the
19user. For related topics such as keymaps and syntax tables, see
20@ref{Keymaps}, and @ref{Syntax Tables}.
21
22@menu
23* Major Modes:: Defining major modes.
24* Minor Modes:: Defining minor modes.
25* Mode Line Format:: Customizing the text that appears in the mode line.
f9f59935
RS
26* Imenu:: How a mode can provide a menu
27 of definitions in the buffer.
28* Font Lock Mode:: How modes can highlight text according to syntax.
f730cc62
LH
29* Desktop Save Mode:: How modes can have buffer state saved between
30 Emacs sessions.
a44af9f2
RS
31* Hooks:: How to use hooks; how to write code that provides hooks.
32@end menu
33
34@node Major Modes
35@section Major Modes
36@cindex major mode
37@cindex Fundamental mode
38
39 Major modes specialize Emacs for editing particular kinds of text.
22c94cf2
RS
40Each buffer has only one major mode at a time. For each major mode
41there is a function to switch to that mode in the current buffer; its
42name should end in @samp{-mode}. These functions work by setting
43buffer-local variable bindings and other data associated with the
44buffer, such as a local keymap. The effect lasts until you switch
45to another major mode in the same buffer.
a44af9f2
RS
46
47 The least specialized major mode is called @dfn{Fundamental mode}.
48This mode has no mode-specific definitions or variable settings, so each
49Emacs command behaves in its default manner, and each option is in its
50default state. All other major modes redefine various keys and options.
51For example, Lisp Interaction mode provides special key bindings for
969fe9b5 52@kbd{C-j} (@code{eval-print-last-sexp}), @key{TAB}
a44af9f2
RS
53(@code{lisp-indent-line}), and other keys.
54
55 When you need to write several editing commands to help you perform a
56specialized editing task, creating a new major mode is usually a good
57idea. In practice, writing a major mode is easy (in contrast to
58writing a minor mode, which is often difficult).
59
60 If the new mode is similar to an old one, it is often unwise to modify
61the old one to serve two purposes, since it may become harder to use and
62maintain. Instead, copy and rename an existing major mode definition
63and alter the copy---or define a @dfn{derived mode} (@pxref{Derived
64Modes}). For example, Rmail Edit mode, which is in
a40d4712
PR
65@file{emacs/lisp/mail/rmailedit.el}, is a major mode that is very similar to
66Text mode except that it provides two additional commands. Its
67definition is distinct from that of Text mode, but uses that of Text mode.
a44af9f2 68
5858d11f 69 Even if the new mode is not an obvious derivative of any other mode,
0b41c65c
SM
70it is convenient to use @code{define-derived-mode} with a @code{nil}
71parent argument, since it automatically enforces the most important
72coding conventions for you.
5858d11f 73
44607ed4
RS
74@findex define-generic-mode
75 For a very simple programming language major mode that handles
76comments and fontification, you can use @code{define-generic-mode}
77in @file{generic.el}.
78
969fe9b5
RS
79 Rmail Edit mode offers an example of changing the major mode
80temporarily for a buffer, so it can be edited in a different way (with
1911e6e5 81ordinary Emacs commands rather than Rmail commands). In such cases, the
8241495d 82temporary major mode usually provides a command to switch back to the
969fe9b5
RS
83buffer's usual mode (Rmail mode, in this case). You might be tempted to
84present the temporary redefinitions inside a recursive edit and restore
85the usual ones when the user exits; but this is a bad idea because it
86constrains the user's options when it is done in more than one buffer:
87recursive edits must be exited most-recently-entered first. Using an
88alternative major mode avoids this limitation. @xref{Recursive
a44af9f2
RS
89Editing}.
90
8241495d
RS
91 The standard GNU Emacs Lisp library directory tree contains the code
92for several major modes, in files such as @file{text-mode.el},
a44af9f2 93@file{texinfo.el}, @file{lisp-mode.el}, @file{c-mode.el}, and
a40d4712
PR
94@file{rmail.el}. They are found in various subdirectories of the
95@file{lisp} directory. You can study these libraries to see how modes
96are written. Text mode is perhaps the simplest major mode aside from
a44af9f2
RS
97Fundamental mode. Rmail mode is a complicated and specialized mode.
98
99@menu
100* Major Mode Conventions:: Coding conventions for keymaps, etc.
101* Example Major Modes:: Text mode and Lisp modes.
102* Auto Major Mode:: How Emacs chooses the major mode automatically.
103* Mode Help:: Finding out how to use a mode.
177c0ea7 104* Derived Modes:: Defining a new major mode based on another major
a44af9f2
RS
105 mode.
106@end menu
107
108@node Major Mode Conventions
109@subsection Major Mode Conventions
110
111 The code for existing major modes follows various coding conventions,
112including conventions for local keymap and syntax table initialization,
113global names, and hooks. Please follow these conventions when you
008c5e26
RS
114define a new major mode.
115
116 This list of conventions is only partial, because each major mode
117should aim for consistency in general with other Emacs major modes.
118This makes Emacs as a whole more coherent. It is impossible to list
119here all the possible points where this issue might come up; if the
120Emacs developers point out an area where your major mode deviates from
121the usual conventions, please make it compatible.
a44af9f2
RS
122
123@itemize @bullet
124@item
125Define a command whose name ends in @samp{-mode}, with no arguments,
126that switches to the new mode in the current buffer. This command
969fe9b5
RS
127should set up the keymap, syntax table, and buffer-local variables in an
128existing buffer, without changing the buffer's contents.
a44af9f2
RS
129
130@item
de9f0bd9 131Write a documentation string for this command that describes the
a44af9f2
RS
132special commands available in this mode. @kbd{C-h m}
133(@code{describe-mode}) in your mode will display this string.
134
135The documentation string may include the special documentation
136substrings, @samp{\[@var{command}]}, @samp{\@{@var{keymap}@}}, and
86494bd5 137@samp{\<@var{keymap}>}, which enable the documentation to adapt
a44af9f2
RS
138automatically to the user's own key bindings. @xref{Keys in
139Documentation}.
140
141@item
142The major mode command should start by calling
969fe9b5
RS
143@code{kill-all-local-variables}. This is what gets rid of the
144buffer-local variables of the major mode previously in effect.
a44af9f2
RS
145
146@item
147The major mode command should set the variable @code{major-mode} to the
148major mode command symbol. This is how @code{describe-mode} discovers
149which documentation to print.
150
151@item
152The major mode command should set the variable @code{mode-name} to the
969fe9b5
RS
153``pretty'' name of the mode, as a string. This string appears in the
154mode line.
a44af9f2
RS
155
156@item
157@cindex functions in modes
158Since all global names are in the same name space, all the global
159variables, constants, and functions that are part of the mode should
160have names that start with the major mode name (or with an abbreviation
a4b12c74 161of it if the name is long). @xref{Coding Conventions}.
a44af9f2 162
008c5e26
RS
163@item
164In a major mode for editing some kind of structured text, such as a
165programming language, indentation of text according to structure is
166probably useful. So the mode should set @code{indent-line-function}
167to a suitable function, and probably customize other variables
168for indentation.
169
a44af9f2
RS
170@item
171@cindex keymaps in modes
172The major mode should usually have its own keymap, which is used as the
969fe9b5
RS
173local keymap in all buffers in that mode. The major mode command should
174call @code{use-local-map} to install this local map. @xref{Active
175Keymaps}, for more information.
a44af9f2 176
969fe9b5 177This keymap should be stored permanently in a global variable named
a44af9f2 178@code{@var{modename}-mode-map}. Normally the library that defines the
de9f0bd9 179mode sets this variable.
a44af9f2 180
23ce41fc
RS
181@xref{Tips for Defining}, for advice about how to write the code to set
182up the mode's keymap variable.
183
a4b12c74
RS
184@item
185The key sequences bound in a major mode keymap should usually start with
969fe9b5 186@kbd{C-c}, followed by a control character, a digit, or @kbd{@{},
a4b12c74
RS
187@kbd{@}}, @kbd{<}, @kbd{>}, @kbd{:} or @kbd{;}. The other punctuation
188characters are reserved for minor modes, and ordinary letters are
189reserved for users.
190
191It is reasonable for a major mode to rebind a key sequence with a
192standard meaning, if it implements a command that does ``the same job''
193in a way that fits the major mode better. For example, a major mode for
194editing a programming language might redefine @kbd{C-M-a} to ``move to
195the beginning of a function'' in a way that works better for that
196language.
197
198Major modes such as Dired or Rmail that do not allow self-insertion of
199text can reasonably redefine letters and other printing characters as
200editing commands. Dired and Rmail both do this.
201
c2e903c0
RS
202@item
203Major modes must not define @key{RET} to do anything other than insert
204a newline. The command to insert a newline and then indent is
008c5e26
RS
205@kbd{C-j}. Please keep this distinction uniform for all major modes.
206
207@item
2a233172 208Major modes should not alter options that are primarily a matter of user
008c5e26
RS
209preference, such as whether Auto-Fill mode is enabled. Leave this to
210each user to decide. However, a major mode should customize other
211variables so that Auto-Fill mode will work usefully @emph{if} the user
212decides to use it.
c2e903c0 213
a44af9f2
RS
214@item
215@cindex syntax tables in modes
216The mode may have its own syntax table or may share one with other
217related modes. If it has its own syntax table, it should store this in
de9f0bd9 218a variable named @code{@var{modename}-mode-syntax-table}. @xref{Syntax
a44af9f2
RS
219Tables}.
220
be9345cf
RS
221@item
222If the mode handles a language that has a syntax for comments, it should
223set the variables that define the comment syntax. @xref{Options for
224Comments,, Options Controlling Comments, emacs, The GNU Emacs Manual}.
225
a44af9f2
RS
226@item
227@cindex abbrev tables in modes
228The mode may have its own abbrev table or may share one with other
229related modes. If it has its own abbrev table, it should store this in
230a variable named @code{@var{modename}-mode-abbrev-table}. @xref{Abbrev
231Tables}.
232
be9345cf 233@item
be9345cf
RS
234The mode should specify how to do highlighting for Font Lock mode, by
235setting up a buffer-local value for the variable
969fe9b5 236@code{font-lock-defaults} (@pxref{Font Lock Mode}).
be9345cf
RS
237
238@item
be9345cf
RS
239The mode should specify how Imenu should find the definitions or
240sections of a buffer, by setting up a buffer-local value for the
10ee4e90
LK
241variable @code{imenu-generic-expression}, for the pair of variables
242@code{imenu-prev-index-position-function} and
243@code{imenu-extract-index-name-function}, or for the variable
969fe9b5 244@code{imenu-create-index-function} (@pxref{Imenu}).
be9345cf 245
de9f0bd9 246@item
969fe9b5
RS
247Use @code{defvar} or @code{defcustom} to set mode-related variables, so
248that they are not reinitialized if they already have a value. (Such
249reinitialization could discard customizations made by the user.)
de9f0bd9 250
a44af9f2
RS
251@item
252@cindex buffer-local variables in modes
253To make a buffer-local binding for an Emacs customization variable, use
254@code{make-local-variable} in the major mode command, not
255@code{make-variable-buffer-local}. The latter function would make the
256variable local to every buffer in which it is subsequently set, which
257would affect buffers that do not use this mode. It is undesirable for a
258mode to have such global effects. @xref{Buffer-Local Variables}.
259
177c0ea7 260With rare exceptions, the only reasonable way to use
a40d4712
PR
261@code{make-variable-buffer-local} in a Lisp package is for a variable
262which is used only within that package. Using it on a variable used by
263other packages would interfere with them.
a44af9f2
RS
264
265@item
266@cindex mode hook
267@cindex major mode hook
268Each major mode should have a @dfn{mode hook} named
269@code{@var{modename}-mode-hook}. The major mode command should run that
fdba9ef4 270hook, with @code{run-mode-hooks}, as the very last thing it
a4b12c74 271does. @xref{Hooks}.
a44af9f2
RS
272
273@item
fdba9ef4
RS
274The major mode command may start by calling some other major mode
275command (called the @dfn{parent mode}) and then alter some of its
276settings. A mode that does this is called a @dfn{derived mode}. The
277recommended way to define one is to use @code{define-derived-mode},
278but this is not required. Such a mode should use
279@code{delay-mode-hooks} around its entire body, including the call to
280the parent mode command and the final call to @code{run-mode-hooks}.
06862374 281(Using @code{define-derived-mode} does this automatically.)
a44af9f2
RS
282
283@item
284If something special should be done if the user switches a buffer from
f9f59935 285this mode to any other major mode, this mode can set up a buffer-local
969fe9b5 286value for @code{change-major-mode-hook} (@pxref{Creating Buffer-Local}).
a44af9f2
RS
287
288@item
289If this mode is appropriate only for specially-prepared text, then the
290major mode command symbol should have a property named @code{mode-class}
291with value @code{special}, put on as follows:
292
ace41f7d 293@kindex mode-class @r{(property)}
a44af9f2
RS
294@cindex @code{special}
295@example
296(put 'funny-mode 'mode-class 'special)
297@end example
298
299@noindent
8241495d 300This tells Emacs that new buffers created while the current buffer is in
a44af9f2
RS
301Funny mode should not inherit Funny mode. Modes such as Dired, Rmail,
302and Buffer List use this feature.
303
304@item
305If you want to make the new mode the default for files with certain
306recognizable names, add an element to @code{auto-mode-alist} to select
307the mode for those file names. If you define the mode command to
308autoload, you should add this element in the same file that calls
309@code{autoload}. Otherwise, it is sufficient to add the element in the
310file that contains the mode definition. @xref{Auto Major Mode}.
311
312@item
a44af9f2
RS
313In the documentation, you should provide a sample @code{autoload} form
314and an example of how to add to @code{auto-mode-alist}, that users can
a40d4712 315include in their init files (@pxref{Init File}).
a44af9f2
RS
316
317@item
318@cindex mode loading
de9f0bd9 319The top-level forms in the file defining the mode should be written so
a44af9f2
RS
320that they may be evaluated more than once without adverse consequences.
321Even if you never load the file more than once, someone else will.
322@end itemize
323
a44af9f2
RS
324@node Example Major Modes
325@subsection Major Mode Examples
326
327 Text mode is perhaps the simplest mode besides Fundamental mode.
328Here are excerpts from @file{text-mode.el} that illustrate many of
329the conventions listed above:
330
331@smallexample
332@group
333;; @r{Create mode-specific tables.}
177c0ea7 334(defvar text-mode-syntax-table nil
a44af9f2
RS
335 "Syntax table used while in text mode.")
336@end group
337
338@group
339(if text-mode-syntax-table
340 () ; @r{Do not change the table if it is already set up.}
341 (setq text-mode-syntax-table (make-syntax-table))
342 (modify-syntax-entry ?\" ". " text-mode-syntax-table)
343 (modify-syntax-entry ?\\ ". " text-mode-syntax-table)
344 (modify-syntax-entry ?' "w " text-mode-syntax-table))
345@end group
346
347@group
348(defvar text-mode-abbrev-table nil
349 "Abbrev table used while in text mode.")
350(define-abbrev-table 'text-mode-abbrev-table ())
351@end group
352
353@group
a40d4712
PR
354(defvar text-mode-map nil ; @r{Create a mode-specific keymap.}
355 "Keymap for Text mode.
356Many other modes, such as Mail mode, Outline mode and Indented Text mode,
357inherit all the commands defined in this map.")
a44af9f2
RS
358
359(if text-mode-map
360 () ; @r{Do not change the keymap if it is already set up.}
361 (setq text-mode-map (make-sparse-keymap))
a40d4712 362 (define-key text-mode-map "\e\t" 'ispell-complete-word)
a9f0a989 363 (define-key text-mode-map "\t" 'indent-relative)
a44af9f2
RS
364 (define-key text-mode-map "\es" 'center-line)
365 (define-key text-mode-map "\eS" 'center-paragraph))
366@end group
367@end smallexample
368
fdba9ef4 369 This was formerly the complete major mode function definition for Text mode:
a44af9f2
RS
370
371@smallexample
372@group
373(defun text-mode ()
29b677db 374 "Major mode for editing text intended for humans to read...
a44af9f2
RS
375 Special commands: \\@{text-mode-map@}
376@end group
377@group
378Turning on text-mode runs the hook `text-mode-hook'."
379 (interactive)
380 (kill-all-local-variables)
969fe9b5 381 (use-local-map text-mode-map)
a44af9f2
RS
382@end group
383@group
a44af9f2
RS
384 (setq local-abbrev-table text-mode-abbrev-table)
385 (set-syntax-table text-mode-syntax-table)
969fe9b5
RS
386@end group
387@group
388 (make-local-variable 'paragraph-start)
389 (setq paragraph-start (concat "[ \t]*$\\|" page-delimiter))
390 (make-local-variable 'paragraph-separate)
391 (setq paragraph-separate paragraph-start)
a40d4712
PR
392 (make-local-variable 'indent-line-function)
393 (setq indent-line-function 'indent-relative-maybe)
969fe9b5
RS
394@end group
395@group
396 (setq mode-name "Text")
397 (setq major-mode 'text-mode)
fdba9ef4 398 (run-mode-hooks 'text-mode-hook)) ; @r{Finally, this permits the user to}
a44af9f2
RS
399 ; @r{customize the mode with a hook.}
400@end group
401@end smallexample
402
403@cindex @file{lisp-mode.el}
404 The three Lisp modes (Lisp mode, Emacs Lisp mode, and Lisp
405Interaction mode) have more features than Text mode and the code is
406correspondingly more complicated. Here are excerpts from
407@file{lisp-mode.el} that illustrate how these modes are written.
408
409@cindex syntax table example
410@smallexample
411@group
412;; @r{Create mode-specific table variables.}
177c0ea7 413(defvar lisp-mode-syntax-table nil "")
a44af9f2
RS
414(defvar emacs-lisp-mode-syntax-table nil "")
415(defvar lisp-mode-abbrev-table nil "")
416@end group
417
418@group
419(if (not emacs-lisp-mode-syntax-table) ; @r{Do not change the table}
420 ; @r{if it is already set.}
421 (let ((i 0))
422 (setq emacs-lisp-mode-syntax-table (make-syntax-table))
423@end group
424
425@group
426 ;; @r{Set syntax of chars up to 0 to class of chars that are}
427 ;; @r{part of symbol names but not words.}
ad800164 428 ;; @r{(The number 0 is @code{48} in the @acronym{ASCII} character set.)}
177c0ea7 429 (while (< i ?0)
a44af9f2
RS
430 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
431 (setq i (1+ i)))
432 @dots{}
433@end group
434@group
435 ;; @r{Set the syntax for other characters.}
436 (modify-syntax-entry ? " " emacs-lisp-mode-syntax-table)
437 (modify-syntax-entry ?\t " " emacs-lisp-mode-syntax-table)
438 @dots{}
439@end group
440@group
441 (modify-syntax-entry ?\( "() " emacs-lisp-mode-syntax-table)
442 (modify-syntax-entry ?\) ")( " emacs-lisp-mode-syntax-table)
443 @dots{}))
444;; @r{Create an abbrev table for lisp-mode.}
445(define-abbrev-table 'lisp-mode-abbrev-table ())
446@end group
447@end smallexample
448
449 Much code is shared among the three Lisp modes. The following
450function sets various variables; it is called by each of the major Lisp
451mode functions:
452
453@smallexample
454@group
455(defun lisp-mode-variables (lisp-syntax)
a44af9f2 456 (cond (lisp-syntax
969fe9b5 457 (set-syntax-table lisp-mode-syntax-table)))
a44af9f2 458 (setq local-abbrev-table lisp-mode-abbrev-table)
a9f0a989 459 @dots{}
a44af9f2
RS
460@end group
461@end smallexample
462
463 Functions such as @code{forward-paragraph} use the value of the
464@code{paragraph-start} variable. Since Lisp code is different from
465ordinary text, the @code{paragraph-start} variable needs to be set
466specially to handle Lisp. Also, comments are indented in a special
467fashion in Lisp and the Lisp modes need their own mode-specific
468@code{comment-indent-function}. The code to set these variables is the
469rest of @code{lisp-mode-variables}.
470
471@smallexample
472@group
473 (make-local-variable 'paragraph-start)
969fe9b5
RS
474 (setq paragraph-start (concat page-delimiter "\\|$" ))
475 (make-local-variable 'paragraph-separate)
476 (setq paragraph-separate paragraph-start)
a44af9f2
RS
477 @dots{}
478@end group
479@group
480 (make-local-variable 'comment-indent-function)
481 (setq comment-indent-function 'lisp-comment-indent))
a40d4712 482 @dots{}
a44af9f2
RS
483@end group
484@end smallexample
485
486 Each of the different Lisp modes has a slightly different keymap. For
f9f59935 487example, Lisp mode binds @kbd{C-c C-z} to @code{run-lisp}, but the other
a44af9f2 488Lisp modes do not. However, all Lisp modes have some commands in
969fe9b5 489common. The following code sets up the common commands:
a44af9f2
RS
490
491@smallexample
492@group
969fe9b5
RS
493(defvar shared-lisp-mode-map ()
494 "Keymap for commands shared by all sorts of Lisp modes.")
495
496(if shared-lisp-mode-map
497 ()
498 (setq shared-lisp-mode-map (make-sparse-keymap))
499 (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp)
500 (define-key shared-lisp-mode-map "\177"
501 'backward-delete-char-untabify))
a44af9f2
RS
502@end group
503@end smallexample
504
969fe9b5
RS
505@noindent
506And here is the code to set up the keymap for Lisp mode:
a44af9f2
RS
507
508@smallexample
509@group
969fe9b5 510(defvar lisp-mode-map ()
29b677db 511 "Keymap for ordinary Lisp mode...")
969fe9b5
RS
512
513(if lisp-mode-map
a44af9f2 514 ()
969fe9b5
RS
515 (setq lisp-mode-map (make-sparse-keymap))
516 (set-keymap-parent lisp-mode-map shared-lisp-mode-map)
517 (define-key lisp-mode-map "\e\C-x" 'lisp-eval-defun)
518 (define-key lisp-mode-map "\C-c\C-z" 'run-lisp))
a44af9f2
RS
519@end group
520@end smallexample
521
522 Finally, here is the complete major mode function definition for
177c0ea7 523Lisp mode.
a44af9f2
RS
524
525@smallexample
526@group
969fe9b5
RS
527(defun lisp-mode ()
528 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
a44af9f2
RS
529Commands:
530Delete converts tabs to spaces as it moves back.
531Blank lines separate paragraphs. Semicolons start comments.
969fe9b5
RS
532\\@{lisp-mode-map@}
533Note that `run-lisp' may be used either to start an inferior Lisp job
534or to switch back to an existing one.
a44af9f2 535@end group
969fe9b5 536
a44af9f2 537@group
969fe9b5
RS
538Entry to this mode calls the value of `lisp-mode-hook'
539if that value is non-nil."
a44af9f2
RS
540 (interactive)
541 (kill-all-local-variables)
a44af9f2
RS
542@end group
543@group
969fe9b5
RS
544 (use-local-map lisp-mode-map) ; @r{Select the mode's keymap.}
545 (setq major-mode 'lisp-mode) ; @r{This is how @code{describe-mode}}
a44af9f2 546 ; @r{finds out what to describe.}
969fe9b5
RS
547 (setq mode-name "Lisp") ; @r{This goes into the mode line.}
548 (lisp-mode-variables t) ; @r{This defines various variables.}
549@end group
550@group
551 (setq imenu-case-fold-search t)
552 (set-syntax-table lisp-mode-syntax-table)
fdba9ef4 553 (run-mode-hooks 'lisp-mode-hook)) ; @r{This permits the user to use a}
a44af9f2
RS
554 ; @r{hook to customize the mode.}
555@end group
556@end smallexample
557
558@node Auto Major Mode
559@subsection How Emacs Chooses a Major Mode
560
561 Based on information in the file name or in the file itself, Emacs
562automatically selects a major mode for the new buffer when a file is
969fe9b5 563visited. It also processes local variables specified in the file text.
a44af9f2
RS
564
565@deffn Command fundamental-mode
566 Fundamental mode is a major mode that is not specialized for anything
567in particular. Other major modes are defined in effect by comparison
568with this one---their definitions say what to change, starting from
569Fundamental mode. The @code{fundamental-mode} function does @emph{not}
570run any hooks; you're not supposed to customize it. (If you want Emacs
571to behave differently in Fundamental mode, change the @emph{global}
572state of Emacs.)
573@end deffn
574
575@deffn Command normal-mode &optional find-file
969fe9b5 576This function establishes the proper major mode and buffer-local variable
a44af9f2
RS
577bindings for the current buffer. First it calls @code{set-auto-mode},
578then it runs @code{hack-local-variables} to parse, and bind or
969fe9b5 579evaluate as appropriate, the file's local variables.
a44af9f2 580
969fe9b5
RS
581If the @var{find-file} argument to @code{normal-mode} is non-@code{nil},
582@code{normal-mode} assumes that the @code{find-file} function is calling
583it. In this case, it may process a local variables list at the end of
584the file and in the @samp{-*-} line. The variable
585@code{enable-local-variables} controls whether to do so. @xref{File
586variables, , Local Variables in Files, emacs, The GNU Emacs Manual}, for
587the syntax of the local variables section of a file.
a44af9f2 588
bfe721d1 589If you run @code{normal-mode} interactively, the argument
a44af9f2
RS
590@var{find-file} is normally @code{nil}. In this case,
591@code{normal-mode} unconditionally processes any local variables list.
a44af9f2
RS
592
593@cindex file mode specification error
bfe721d1 594@code{normal-mode} uses @code{condition-case} around the call to the
a44af9f2
RS
595major mode function, so errors are caught and reported as a @samp{File
596mode specification error}, followed by the original error message.
597@end deffn
598
a44af9f2
RS
599@defun set-auto-mode
600@cindex visited file mode
601 This function selects the major mode that is appropriate for the
602current buffer. It may base its decision on the value of the @w{@samp{-*-}}
76352dc1
RS
603line, on the visited file name (using @code{auto-mode-alist}), on the
604@w{@samp{#!}} line (using @code{interpreter-mode-alist}), or on the
969fe9b5 605file's local variables list. However, this function does not look for
a44af9f2
RS
606the @samp{mode:} local variable near the end of a file; the
607@code{hack-local-variables} function does that. @xref{Choosing Modes, ,
608How Major Modes are Chosen, emacs, The GNU Emacs Manual}.
609@end defun
610
177c0ea7 611@defopt default-major-mode
969fe9b5 612This variable holds the default major mode for new buffers. The
a44af9f2
RS
613standard value is @code{fundamental-mode}.
614
969fe9b5 615If the value of @code{default-major-mode} is @code{nil}, Emacs uses
a44af9f2 616the (previously) current buffer's major mode for the major mode of a new
f9f59935 617buffer. However, if that major mode symbol has a @code{mode-class}
a44af9f2
RS
618property with value @code{special}, then it is not used for new buffers;
619Fundamental mode is used instead. The modes that have this property are
620those such as Dired and Rmail that are useful only with text that has
621been specially prepared.
622@end defopt
623
22697dac
KH
624@defun set-buffer-major-mode buffer
625This function sets the major mode of @var{buffer} to the value of
626@code{default-major-mode}. If that variable is @code{nil}, it uses
627the current buffer's major mode (if that is suitable).
628
629The low-level primitives for creating buffers do not use this function,
bfe721d1
KH
630but medium-level commands such as @code{switch-to-buffer} and
631@code{find-file-noselect} use it whenever they create buffers.
22697dac
KH
632@end defun
633
a44af9f2
RS
634@defvar initial-major-mode
635@cindex @samp{*scratch*}
636The value of this variable determines the major mode of the initial
637@samp{*scratch*} buffer. The value should be a symbol that is a major
f9f59935 638mode command. The default value is @code{lisp-interaction-mode}.
a44af9f2
RS
639@end defvar
640
641@defvar auto-mode-alist
642This variable contains an association list of file name patterns
643(regular expressions; @pxref{Regular Expressions}) and corresponding
f9f59935
RS
644major mode commands. Usually, the file name patterns test for suffixes,
645such as @samp{.el} and @samp{.c}, but this need not be the case. An
646ordinary element of the alist looks like @code{(@var{regexp} .
a44af9f2
RS
647@var{mode-function})}.
648
649For example,
650
651@smallexample
652@group
969fe9b5 653(("\\`/tmp/fol/" . text-mode)
24675e99
RS
654 ("\\.texinfo\\'" . texinfo-mode)
655 ("\\.texi\\'" . texinfo-mode)
a44af9f2
RS
656@end group
657@group
24675e99 658 ("\\.el\\'" . emacs-lisp-mode)
177c0ea7 659 ("\\.c\\'" . c-mode)
24675e99 660 ("\\.h\\'" . c-mode)
a44af9f2
RS
661 @dots{})
662@end group
663@end smallexample
664
665When you visit a file whose expanded file name (@pxref{File Name
666Expansion}) matches a @var{regexp}, @code{set-auto-mode} calls the
667corresponding @var{mode-function}. This feature enables Emacs to select
668the proper major mode for most files.
669
670If an element of @code{auto-mode-alist} has the form @code{(@var{regexp}
671@var{function} t)}, then after calling @var{function}, Emacs searches
672@code{auto-mode-alist} again for a match against the portion of the file
969fe9b5
RS
673name that did not match before. This feature is useful for
674uncompression packages: an entry of the form @code{("\\.gz\\'"
675@var{function} t)} can uncompress the file and then put the uncompressed
676file in the proper mode according to the name sans @samp{.gz}.
a44af9f2
RS
677
678Here is an example of how to prepend several pattern pairs to
679@code{auto-mode-alist}. (You might use this sort of expression in your
a40d4712 680init file.)
a44af9f2
RS
681
682@smallexample
683@group
684(setq auto-mode-alist
177c0ea7 685 (append
f9f59935 686 ;; @r{File name (within directory) starts with a dot.}
177c0ea7 687 '(("/\\.[^/]*\\'" . fundamental-mode)
de9f0bd9 688 ;; @r{File name has no dot.}
177c0ea7 689 ("[^\\./]*\\'" . fundamental-mode)
de9f0bd9 690 ;; @r{File name ends in @samp{.C}.}
24675e99 691 ("\\.C\\'" . c++-mode))
a44af9f2
RS
692 auto-mode-alist))
693@end group
694@end smallexample
695@end defvar
696
697@defvar interpreter-mode-alist
f9f59935 698This variable specifies major modes to use for scripts that specify a
86494bd5 699command interpreter in a @samp{#!} line. Its value is a list of
a44af9f2
RS
700elements of the form @code{(@var{interpreter} . @var{mode})}; for
701example, @code{("perl" . perl-mode)} is one element present by default.
702The element says to use mode @var{mode} if the file specifies
f9f59935
RS
703an interpreter which matches @var{interpreter}. The value of
704@var{interpreter} is actually a regular expression.
a44af9f2 705
de9f0bd9
RS
706This variable is applicable only when the @code{auto-mode-alist} does
707not indicate which major mode to use.
a44af9f2
RS
708@end defvar
709
a44af9f2
RS
710@node Mode Help
711@subsection Getting Help about a Major Mode
712@cindex mode help
713@cindex help for major mode
714@cindex documentation for major mode
715
716 The @code{describe-mode} function is used to provide information
717about major modes. It is normally called with @kbd{C-h m}. The
718@code{describe-mode} function uses the value of @code{major-mode},
719which is why every major mode function needs to set the
720@code{major-mode} variable.
721
722@deffn Command describe-mode
723This function displays the documentation of the current major mode.
724
725The @code{describe-mode} function calls the @code{documentation}
726function using the value of @code{major-mode} as an argument. Thus, it
727displays the documentation string of the major mode function.
728(@xref{Accessing Documentation}.)
729@end deffn
730
731@defvar major-mode
732This variable holds the symbol for the current buffer's major mode.
de9f0bd9 733This symbol should have a function definition that is the command to
a44af9f2 734switch to that major mode. The @code{describe-mode} function uses the
de9f0bd9 735documentation string of the function as the documentation of the major
a44af9f2
RS
736mode.
737@end defvar
738
739@node Derived Modes
740@subsection Defining Derived Modes
741
742 It's often useful to define a new major mode in terms of an existing
743one. An easy way to do this is to use @code{define-derived-mode}.
744
de9f0bd9 745@defmac define-derived-mode variant parent name docstring body@dots{}
a44af9f2 746This construct defines @var{variant} as a major mode command, using
de9f0bd9 747@var{name} as the string form of the mode name.
a44af9f2 748
de9f0bd9
RS
749The new command @var{variant} is defined to call the function
750@var{parent}, then override certain aspects of that parent mode:
a44af9f2 751
177c0ea7 752@itemize @bullet
a44af9f2
RS
753@item
754The new mode has its own keymap, named @code{@var{variant}-map}.
755@code{define-derived-mode} initializes this map to inherit from
756@code{@var{parent}-map}, if it is not already set.
757
758@item
de9f0bd9 759The new mode has its own syntax table, kept in the variable
a44af9f2 760@code{@var{variant}-syntax-table}.
177c0ea7 761@code{define-derived-mode} initializes this variable by copying
a44af9f2
RS
762@code{@var{parent}-syntax-table}, if it is not already set.
763
764@item
de9f0bd9 765The new mode has its own abbrev table, kept in the variable
a44af9f2 766@code{@var{variant}-abbrev-table}.
177c0ea7 767@code{define-derived-mode} initializes this variable by copying
a44af9f2
RS
768@code{@var{parent}-abbrev-table}, if it is not already set.
769
770@item
771The new mode has its own mode hook, @code{@var{variant}-hook},
772which it runs in standard fashion as the very last thing that it does.
177c0ea7 773(The new mode also runs the mode hook of @var{parent} as part
a44af9f2
RS
774of calling @var{parent}.)
775@end itemize
776
777In addition, you can specify how to override other aspects of
de9f0bd9 778@var{parent} with @var{body}. The command @var{variant}
177c0ea7 779evaluates the forms in @var{body} after setting up all its usual
a44af9f2
RS
780overrides, just before running @code{@var{variant}-hook}.
781
782The argument @var{docstring} specifies the documentation string for the
783new mode. If you omit @var{docstring}, @code{define-derived-mode}
784generates a documentation string.
785
786Here is a hypothetical example:
787
788@example
789(define-derived-mode hypertext-mode
790 text-mode "Hypertext"
791 "Major mode for hypertext.
792\\@{hypertext-mode-map@}"
793 (setq case-fold-search nil))
794
795(define-key hypertext-mode-map
796 [down-mouse-3] 'do-hyper-link)
797@end example
f140458b
RS
798
799Do not write an @code{interactive} spec in the definition;
800@code{define-derived-mode} does that automatically.
a44af9f2
RS
801@end defmac
802
803@node Minor Modes
804@section Minor Modes
805@cindex minor mode
806
807 A @dfn{minor mode} provides features that users may enable or disable
808independently of the choice of major mode. Minor modes can be enabled
809individually or in combination. Minor modes would be better named
969fe9b5
RS
810``generally available, optional feature modes,'' except that such a name
811would be unwieldy.
a44af9f2 812
8241495d
RS
813 A minor mode is not usually meant as a variation of a single major mode.
814Usually they are general and can apply to many major modes. For
969fe9b5 815example, Auto Fill mode works with any major mode that permits text
a44af9f2
RS
816insertion. To be general, a minor mode must be effectively independent
817of the things major modes do.
818
819 A minor mode is often much more difficult to implement than a major
820mode. One reason is that you should be able to activate and deactivate
de9f0bd9
RS
821minor modes in any order. A minor mode should be able to have its
822desired effect regardless of the major mode and regardless of the other
823minor modes in effect.
a44af9f2
RS
824
825 Often the biggest problem in implementing a minor mode is finding a
826way to insert the necessary hook into the rest of Emacs. Minor mode
bfe721d1 827keymaps make this easier than it used to be.
a44af9f2 828
fdba9ef4
RS
829@defvar minor-mode-list
830The value of this variable is a list of all minor mode commands.
831@end defvar
832
a44af9f2
RS
833@menu
834* Minor Mode Conventions:: Tips for writing a minor mode.
835* Keymaps and Minor Modes:: How a minor mode can have its own keymap.
2468d0c0 836* Defining Minor Modes:: A convenient facility for defining minor modes.
a44af9f2
RS
837@end menu
838
839@node Minor Mode Conventions
840@subsection Conventions for Writing Minor Modes
841@cindex minor mode conventions
842@cindex conventions for writing minor modes
843
844 There are conventions for writing minor modes just as there are for
845major modes. Several of the major mode conventions apply to minor
846modes as well: those regarding the name of the mode initialization
847function, the names of global symbols, and the use of keymaps and
848other tables.
849
850 In addition, there are several conventions that are specific to
851minor modes.
852
853@itemize @bullet
854@item
855@cindex mode variable
969fe9b5
RS
856Make a variable whose name ends in @samp{-mode} to control the minor
857mode. We call this the @dfn{mode variable}. The minor mode command
858should set this variable (@code{nil} to disable; anything else to
1911e6e5 859enable).
969fe9b5 860
8241495d 861If possible, implement the mode so that setting the variable
969fe9b5 862automatically enables or disables the mode. Then the minor mode command
1911e6e5 863does not need to do anything except set the variable.
a44af9f2
RS
864
865This variable is used in conjunction with the @code{minor-mode-alist} to
866display the minor mode name in the mode line. It can also enable
867or disable a minor mode keymap. Individual commands or hooks can also
868check the variable's value.
869
870If you want the minor mode to be enabled separately in each buffer,
871make the variable buffer-local.
872
873@item
874Define a command whose name is the same as the mode variable.
875Its job is to enable and disable the mode by setting the variable.
876
877The command should accept one optional argument. If the argument is
8cd4f018
RS
878@code{nil}, it should toggle the mode (turn it on if it is off, and
879off if it is on). It should turn the mode on if the argument is a
880positive integer, the symbol @code{t}, or a list whose @sc{car} is one
881of those. It should turn the mode off if the argument is a negative
882integer or zero, the symbol @code{-}, or a list whose @sc{car} is one
883of those. The meaning of other arguments is not specified.
a44af9f2 884
bfe721d1
KH
885Here is an example taken from the definition of @code{transient-mark-mode}.
886It shows the use of @code{transient-mark-mode} as a variable that enables or
de9f0bd9
RS
887disables the mode's behavior, and also shows the proper way to toggle,
888enable or disable the minor mode based on the raw prefix argument value.
a44af9f2
RS
889
890@smallexample
891@group
bfe721d1
KH
892(setq transient-mark-mode
893 (if (null arg) (not transient-mark-mode)
a44af9f2
RS
894 (> (prefix-numeric-value arg) 0)))
895@end group
896@end smallexample
897
898@item
899Add an element to @code{minor-mode-alist} for each minor mode
969fe9b5
RS
900(@pxref{Mode Line Variables}), if you want to indicate the minor mode in
901the mode line. This element should be a list of the following form:
a44af9f2
RS
902
903@smallexample
904(@var{mode-variable} @var{string})
905@end smallexample
906
de9f0bd9 907Here @var{mode-variable} is the variable that controls enabling of the
a44af9f2
RS
908minor mode, and @var{string} is a short string, starting with a space,
909to represent the mode in the mode line. These strings must be short so
910that there is room for several of them at once.
911
912When you add an element to @code{minor-mode-alist}, use @code{assq} to
913check for an existing element, to avoid duplication. For example:
914
915@smallexample
916@group
a40d4712
PR
917(unless (assq 'leif-mode minor-mode-alist)
918 (setq minor-mode-alist
919 (cons '(leif-mode " Leif") minor-mode-alist)))
a44af9f2
RS
920@end group
921@end smallexample
a44af9f2 922
a40d4712
PR
923@noindent
924or like this, using @code{add-to-list} (@pxref{Setting Variables}):
925
926@smallexample
927@group
928(add-to-list 'minor-mode-alist '(leif-mode " Leif"))
929@end group
930@end smallexample
931@end itemize
1911e6e5 932
8241495d
RS
933 Global minor modes distributed with Emacs should if possible support
934enabling and disabling via Custom (@pxref{Customization}). To do this,
935the first step is to define the mode variable with @code{defcustom}, and
936specify @code{:type boolean}.
937
938 If just setting the variable is not sufficient to enable the mode, you
939should also specify a @code{:set} method which enables the mode by
940invoke the mode command. Note in the variable's documentation string that
941setting the variable other than via Custom may not take effect.
942
943 Also mark the definition with an autoload cookie (@pxref{Autoload}),
944and specify a @code{:require} so that customizing the variable will load
945the library that defines the mode. This will copy suitable definitions
946into @file{loaddefs.el} so that users can use @code{customize-option} to
947enable the mode. For example:
948
949@smallexample
950@group
951
952;;;###autoload
953(defcustom msb-mode nil
954 "Toggle msb-mode.
955Setting this variable directly does not take effect;
956use either \\[customize] or the function `msb-mode'."
957 :set (lambda (symbol value)
958 (msb-mode (or value 0)))
959 :initialize 'custom-initialize-default
960 :version "20.4"
961 :type 'boolean
962 :group 'msb
963 :require 'msb)
964@end group
965@end smallexample
966
a44af9f2
RS
967@node Keymaps and Minor Modes
968@subsection Keymaps and Minor Modes
969
bfe721d1
KH
970 Each minor mode can have its own keymap, which is active when the mode
971is enabled. To set up a keymap for a minor mode, add an element to the
972alist @code{minor-mode-map-alist}. @xref{Active Keymaps}.
a44af9f2
RS
973
974@cindex @code{self-insert-command}, minor modes
f9f59935 975 One use of minor mode keymaps is to modify the behavior of certain
a44af9f2
RS
976self-inserting characters so that they do something else as well as
977self-insert. In general, this is the only way to do that, since the
978facilities for customizing @code{self-insert-command} are limited to
979special cases (designed for abbrevs and Auto Fill mode). (Do not try
980substituting your own definition of @code{self-insert-command} for the
981standard one. The editor command loop handles this function specially.)
982
a4b12c74
RS
983The key sequences bound in a minor mode should consist of @kbd{C-c}
984followed by a punctuation character @emph{other than} @kbd{@{},
8241495d 985@kbd{@}}, @kbd{<}, @kbd{>}, @kbd{:}, and @kbd{;}. (Those few punctuation
a4b12c74
RS
986characters are reserved for major modes.)
987
2468d0c0
DL
988@node Defining Minor Modes
989@subsection Defining Minor Modes
f9f59935 990
2468d0c0
DL
991 The macro @code{define-minor-mode} offers a convenient way of
992implementing a mode in one self-contained definition. It supports only
8241495d 993buffer-local minor modes, not global ones.
f9f59935 994
fdba9ef4 995@defmac define-minor-mode mode doc [init-value [lighter [keymap keyword-args... body...]]]
2468d0c0 996@tindex define-minor-mode
fdba9ef4
RS
997This macro defines a new minor mode whose name is @var{mode} (a
998symbol). It defines a command named @var{mode} to toggle the minor
2468d0c0
DL
999mode, with @var{doc} as its documentation string. It also defines a
1000variable named @var{mode}, which is set to @code{t} or @code{nil} by
1001enabling or disabling the mode. The variable is initialized to
1002@var{init-value}.
f9f59935 1003
fdba9ef4 1004The string @var{lighter} says what to display in the mode line
f9f59935
RS
1005when the mode is enabled; if it is @code{nil}, the mode is not displayed
1006in the mode line.
1007
1008The optional argument @var{keymap} specifies the keymap for the minor mode.
1009It can be a variable name, whose value is the keymap, or it can be an alist
1010specifying bindings in this form:
1011
1012@example
1013(@var{key-sequence} . @var{definition})
1014@end example
fdba9ef4
RS
1015
1016The @var{keyword-args} consist of keywords followed by corresponding
1017values. A few keywords have special meanings:
1018
1019@table @code
1020@item :global @var{global}
1021If non-@code{nil} specifies that the minor mode should be global.
1022By default, minor modes are buffer-local.
1023
1024@item :init-value @var{init-value}
1025This is equivalent to specifying @var{init-value} positionally.
1026
1027@item :lighter @var{lighter}
1028This is equivalent to specifying @var{lighter} positionally.
1029
1030@item :keymap @var{keymap}
1031This is equivalent to specifying @var{keymap} positionally.
1032@end table
1033
1034Any other keyword arguments are passed passed directly to the
1035@code{defcustom} generated for the variable @var{mode}.
1036
1037The command named @var{mode} finishes by executing the @var{body} forms,
1038if any, after it has performed the standard actions such as setting
1039the variable named @var{mode}.
f9f59935
RS
1040@end defmac
1041
fdba9ef4
RS
1042@findex easy-mmode-define-minor-mode
1043 The name @code{easy-mmode-define-minor-mode} is an alias
1044for this macro.
1045
2468d0c0 1046 Here is an example of using @code{define-minor-mode}:
f9f59935
RS
1047
1048@smallexample
2468d0c0 1049(define-minor-mode hungry-mode
969fe9b5 1050 "Toggle Hungry mode.
177c0ea7 1051With no argument, this command toggles the mode.
f9f59935
RS
1052Non-null prefix argument turns on the mode.
1053Null prefix argument turns off the mode.
1054
1055When Hungry mode is enabled, the control delete key
1056gobbles all preceding whitespace except the last.
1057See the command \\[hungry-electric-delete]."
1058 ;; The initial value.
1059 nil
1060 ;; The indicator for the mode line.
1061 " Hungry"
1062 ;; The minor mode bindings.
1063 '(("\C-\^?" . hungry-electric-delete)
1064 ("\C-\M-\^?"
177c0ea7 1065 . (lambda ()
f9f59935 1066 (interactive)
fdba9ef4
RS
1067 (hungry-electric-delete t))))
1068 :group 'hunger)
f9f59935
RS
1069@end smallexample
1070
1071@noindent
1072This defines a minor mode named ``Hungry mode'', a command named
1073@code{hungry-mode} to toggle it, a variable named @code{hungry-mode}
1074which indicates whether the mode is enabled, and a variable named
1075@code{hungry-mode-map} which holds the keymap that is active when the
1076mode is enabled. It initializes the keymap with key bindings for
fdba9ef4
RS
1077@kbd{C-@key{DEL}} and @kbd{C-M-@key{DEL}}. It puts the variable
1078@code{hungry-mode} into custom group @code{hunger}. There are no
1079@var{body} forms---many minor modes don't need any.
f9f59935 1080
fdba9ef4 1081 Here's an equivalent way to write it:
2468d0c0 1082
fdba9ef4
RS
1083@smallexample
1084(define-minor-mode hungry-mode
1085 "Toggle Hungry mode.
1086With no argument, this command toggles the mode.
1087Non-null prefix argument turns on the mode.
1088Null prefix argument turns off the mode.
1089
1090When Hungry mode is enabled, the control delete key
1091gobbles all preceding whitespace except the last.
1092See the command \\[hungry-electric-delete]."
1093 ;; The initial value.
1094 :initial-value nil
1095 ;; The indicator for the mode line.
1096 :lighter " Hungry"
1097 ;; The minor mode bindings.
1098 :keymap
1099 '(("\C-\^?" . hungry-electric-delete)
1100 ("\C-\M-\^?"
1101 . (lambda ()
1102 (interactive)
1103 (hungry-electric-delete t))))
1104 :group 'hunger)
1105@end smallexample
2468d0c0 1106
a44af9f2 1107@node Mode Line Format
06862374 1108@section Mode-Line Format
a44af9f2
RS
1109@cindex mode line
1110
8241495d
RS
1111 Each Emacs window (aside from minibuffer windows) typically has a mode
1112line at the bottom, which displays status information about the buffer
1113displayed in the window. The mode line contains information about the
1114buffer, such as its name, associated file, depth of recursive editing,
1115and major and minor modes. A window can also have a @dfn{header
1116line}, which is much like the mode line but appears at the top of the
1117window (starting in Emacs 21).
a44af9f2 1118
8241495d
RS
1119 This section describes how to control the contents of the mode line
1120and header line. We include it in this chapter because much of the
a44af9f2
RS
1121information displayed in the mode line relates to the enabled major and
1122minor modes.
1123
1124 @code{mode-line-format} is a buffer-local variable that holds a
1125template used to display the mode line of the current buffer. All
8241495d
RS
1126windows for the same buffer use the same @code{mode-line-format}, so
1127their mode lines appear the same---except for scrolling percentages, and
1128line and column numbers, since those depend on point and on how the
1129window is scrolled. @code{header-line-format} is used likewise for
1130header lines.
1131
8e0f7b5b
RS
1132 For efficiency, Emacs does not recompute the mode line and header
1133line of a window in every redisplay. It does so when circumstances
1134appear to call for it---for instance, if you change the window
1135configuration, switch buffers, narrow or widen the buffer, scroll, or
1136change the buffer's modification status. If you modify any of the
1137variables referenced by @code{mode-line-format} (@pxref{Mode Line
1138Variables}), or any other variables and data structures that affect
1139how text is displayed (@pxref{Display}), you may want to force an
1140update of the mode line so as to display the new information or
1141display it in the new way.
a44af9f2
RS
1142
1143@c Emacs 19 feature
0ad8cce8 1144@defun force-mode-line-update &optional all
8241495d 1145Force redisplay of the current buffer's mode line and header line.
8e0f7b5b 1146The next redisplay will update the mode line and header line based on
0ad8cce8
LK
1147the latest values of all relevant variables. With optional
1148non-@code{nil} @var{all}, force redisplay of all mode lines and header
1149lines.
8e0f7b5b
RS
1150
1151This function also forces recomputation of the menu bar menus
1152and the frame title.
a44af9f2
RS
1153@end defun
1154
1155 The mode line is usually displayed in inverse video; see
1156@code{mode-line-inverse-video} in @ref{Inverse Video}.
1157
a1112b37
RS
1158 A window that is just one line tall does not display either a mode
1159line or a header line, even if the variables call for one. A window
1160that is two lines tall cannot display both a mode line and a header
1161line at once; if the variables call for both, only the mode line
1162actually appears.
1163
a44af9f2
RS
1164@menu
1165* Mode Line Data:: The data structure that controls the mode line.
1166* Mode Line Variables:: Variables used in that data structure.
1167* %-Constructs:: Putting information into a mode line.
8241495d
RS
1168* Properties in Mode:: Using text properties in the mode line.
1169* Header Lines:: Like a mode line, but at the top.
3ebe0346 1170* Emulating Mode Line:: Formatting text as the mode line would.
a44af9f2
RS
1171@end menu
1172
1173@node Mode Line Data
1174@subsection The Data Structure of the Mode Line
06862374 1175@cindex mode-line construct
a44af9f2 1176
06862374 1177 The mode-line contents are controlled by a data structure of lists,
a40d4712 1178strings, symbols, and numbers kept in buffer-local variables. The data
06862374
LK
1179structure is called a @dfn{mode-line construct}, and it is built in
1180recursive fashion out of simpler mode-line constructs. The same data
a40d4712
PR
1181structure is used for constructing frame titles (@pxref{Frame Titles})
1182and header lines (@pxref{Header Lines}).
a44af9f2
RS
1183
1184@defvar mode-line-format
06862374
LK
1185The value of this variable is a mode-line construct with overall
1186responsibility for the mode-line format. The value of this variable
1187controls which other variables are used to form the mode-line text, and
a44af9f2 1188where they appear.
8241495d
RS
1189
1190If you set this variable to @code{nil} in a buffer, that buffer does not
1191have a mode line. (This feature was added in Emacs 21.)
a44af9f2
RS
1192@end defvar
1193
06862374 1194 A mode-line construct may be as simple as a fixed string of text, but
a44af9f2 1195it usually specifies how to use other variables to construct the text.
06862374 1196Many of these variables are themselves defined to have mode-line
a44af9f2
RS
1197constructs as their values.
1198
1199 The default value of @code{mode-line-format} incorporates the values
06862374
LK
1200of variables such as @code{mode-line-position} and
1201@code{mode-line-modes} (which in turn incorporates the values of the
1202variables @code{mode-name} and @code{minor-mode-alist}). Because of
1203this, very few modes need to alter @code{mode-line-format} itself. For
1204most purposes, it is sufficient to alter some of the variables that
1205@code{mode-line-format} either directly or indirectly refers to.
1206
1207 A mode-line construct may be a list, a symbol, or a string. If the
de9f0bd9 1208value is a list, each element may be a list, a symbol, or a string.
a44af9f2 1209
8241495d
RS
1210 The mode line can display various faces, if the strings that control
1211it have the @code{face} property. @xref{Properties in Mode}. In
1212addition, the face @code{mode-line} is used as a default for the whole
1213mode line (@pxref{Standard Faces}).
1214
a44af9f2
RS
1215@table @code
1216@cindex percent symbol in mode line
1217@item @var{string}
06862374 1218A string as a mode-line construct is displayed verbatim in the mode line
bfe721d1 1219except for @dfn{@code{%}-constructs}. Decimal digits after the @samp{%}
a44af9f2
RS
1220specify the field width for space filling on the right (i.e., the data
1221is left justified). @xref{%-Constructs}.
1222
1223@item @var{symbol}
06862374
LK
1224A symbol as a mode-line construct stands for its value. The value of
1225@var{symbol} is used as a mode-line construct, in place of @var{symbol}.
8241495d 1226However, the symbols @code{t} and @code{nil} are ignored, as is any
de9f0bd9 1227symbol whose value is void.
a44af9f2
RS
1228
1229There is one exception: if the value of @var{symbol} is a string, it is
de9f0bd9 1230displayed verbatim: the @code{%}-constructs are not recognized.
a44af9f2 1231
a232a240
LK
1232Unless @var{symbol} is marked as ``risky'' (i.e., it has a
1233non-@code{nil} @code{risky-local-variable} property), all properties in
1234any strings, as well as all @code{:eval} and @code{:propertize} forms in
1235the value of that symbol will be ignored.
1236
a44af9f2 1237@item (@var{string} @var{rest}@dots{}) @r{or} (@var{list} @var{rest}@dots{})
de9f0bd9
RS
1238A list whose first element is a string or list means to process all the
1239elements recursively and concatenate the results. This is the most
06862374 1240common form of mode-line construct.
a44af9f2 1241
8241495d
RS
1242@item (:eval @var{form})
1243A list whose first element is the symbol @code{:eval} says to evaluate
1244@var{form}, and use the result as a string to display.
1245(This feature is new as of Emacs 21.)
1246
06862374
LK
1247@item (:propertize @var{elt} @var{props}@dots{})
1248A list whose first element is the symbol @code{:propertize} says to
1249process the mode-line construct @var{elt} recursively and add the text
1250properties specified by @var{props} to the result. The argument
1251@var{props} should consist of zero or more pairs @var{text-property}
1252@var{value}. (This feature is new as of Emacs 21.4.)
1253@c FIXME: This might be Emacs 21.5.
1254
a44af9f2 1255@item (@var{symbol} @var{then} @var{else})
8241495d
RS
1256A list whose first element is a symbol that is not a keyword specifies a
1257conditional. Its meaning depends on the value of @var{symbol}. If the
1258value is non-@code{nil}, the second element, @var{then}, is processed
06862374 1259recursively as a mode-line element. But if the value of @var{symbol} is
8241495d 1260@code{nil}, the third element, @var{else}, is processed recursively.
06862374 1261You may omit @var{else}; then the mode-line element displays nothing if
8241495d 1262the value of @var{symbol} is @code{nil}.
a44af9f2
RS
1263
1264@item (@var{width} @var{rest}@dots{})
1265A list whose first element is an integer specifies truncation or
1266padding of the results of @var{rest}. The remaining elements
06862374 1267@var{rest} are processed recursively as mode-line constructs and
a44af9f2
RS
1268concatenated together. Then the result is space filled (if
1269@var{width} is positive) or truncated (to @minus{}@var{width} columns,
1270if @var{width} is negative) on the right.
1271
1272For example, the usual way to show what percentage of a buffer is above
de9f0bd9 1273the top of the window is to use a list like this: @code{(-3 "%p")}.
a44af9f2
RS
1274@end table
1275
1276 If you do alter @code{mode-line-format} itself, the new value should
de9f0bd9
RS
1277use the same variables that appear in the default value (@pxref{Mode
1278Line Variables}), rather than duplicating their contents or displaying
1279the information in another fashion. This way, customizations made by
bfe721d1
KH
1280the user or by Lisp programs (such as @code{display-time} and major
1281modes) via changes to those variables remain effective.
a44af9f2
RS
1282
1283@cindex Shell mode @code{mode-line-format}
1284 Here is an example of a @code{mode-line-format} that might be
969fe9b5 1285useful for @code{shell-mode}, since it contains the host name and default
a44af9f2
RS
1286directory.
1287
1288@example
1289@group
1290(setq mode-line-format
969fe9b5
RS
1291 (list "-"
1292 'mode-line-mule-info
a44af9f2 1293 'mode-line-modified
969fe9b5 1294 'mode-line-frame-identification
177c0ea7 1295 "%b--"
a44af9f2 1296@end group
f9f59935
RS
1297@group
1298 ;; @r{Note that this is evaluated while making the list.}
06862374 1299 ;; @r{It makes a mode-line construct which is just a string.}
f9f59935
RS
1300 (getenv "HOST")
1301@end group
177c0ea7 1302 ":"
a44af9f2
RS
1303 'default-directory
1304 " "
1305 'global-mode-string
de9f0bd9 1306 " %[("
8241495d 1307 '(:eval (mode-line-mode-name))
177c0ea7
JB
1308 'mode-line-process
1309 'minor-mode-alist
1310 "%n"
969fe9b5 1311 ")%]--"
a44af9f2 1312@group
969fe9b5 1313 '(which-func-mode ("" which-func-format "--"))
bfe721d1 1314 '(line-number-mode "L%l--")
969fe9b5 1315 '(column-number-mode "C%c--")
a232a240 1316 '(-3 "%p")
a44af9f2
RS
1317 "-%-"))
1318@end group
1319@end example
1320
1911e6e5
RS
1321@noindent
1322(The variables @code{line-number-mode}, @code{column-number-mode}
1323and @code{which-func-mode} enable particular minor modes; as usual,
1324these variable names are also the minor mode command names.)
1325
a44af9f2
RS
1326@node Mode Line Variables
1327@subsection Variables Used in the Mode Line
1328
1329 This section describes variables incorporated by the
1330standard value of @code{mode-line-format} into the text of the mode
1331line. There is nothing inherently special about these variables; any
1332other variables could have the same effects on the mode line if
1333@code{mode-line-format} were changed to use them.
1334
969fe9b5
RS
1335@defvar mode-line-mule-info
1336This variable holds the value of the mode-line construct that displays
1337information about the language environment, buffer coding system, and
a9f0a989 1338current input method. @xref{Non-ASCII Characters}.
969fe9b5
RS
1339@end defvar
1340
a44af9f2 1341@defvar mode-line-modified
de9f0bd9 1342This variable holds the value of the mode-line construct that displays
a44af9f2
RS
1343whether the current buffer is modified.
1344
969fe9b5
RS
1345The default value of @code{mode-line-modified} is @code{("%1*%1+")}.
1346This means that the mode line displays @samp{**} if the buffer is
1347modified, @samp{--} if the buffer is not modified, @samp{%%} if the
1348buffer is read only, and @samp{%*} if the buffer is read only and
1349modified.
a44af9f2
RS
1350
1351Changing this variable does not force an update of the mode line.
1352@end defvar
1353
969fe9b5
RS
1354@defvar mode-line-frame-identification
1355This variable identifies the current frame. The default value is
06862374
LK
1356@code{" "} if you are using a window system which can show multiple
1357frames, or @code{"-%F "} on an ordinary terminal which shows only one
969fe9b5
RS
1358frame at a time.
1359@end defvar
1360
a44af9f2 1361@defvar mode-line-buffer-identification
de9f0bd9 1362This variable identifies the buffer being displayed in the window. Its
ebc6903b
RS
1363default value is @code{("%12b")}, which displays the buffer name, padded
1364with spaces to at least 12 columns.
a44af9f2
RS
1365@end defvar
1366
06862374
LK
1367@defvar mode-line-position
1368This variable indicates the position in the buffer. Here is a
1369simplified version of its default value. The actual default value
1370also specifies addition of the @code{help-echo} text property.
a44af9f2 1371
06862374
LK
1372@example
1373@group
a232a240 1374((-3 "%p")
06862374
LK
1375 (size-indication-mode (8 " of %I"))
1376@end group
1377@group
1378 (line-number-mode
1379 ((column-number-mode
1380 (10 " (%l,%c)")
1381 (6 " L%l")))
1382 ((column-number-mode
1383 (5 " C%c")))))
1384@end group
1385@end example
1386
1387This means that @code{mode-line-position} displays at least the buffer
1388percentage and possibly the buffer size, the line number and the column
1389number.
1390@end defvar
1391
1392@defvar vc-mode
1393The variable @code{vc-mode}, buffer-local in each buffer, records
1394whether the buffer's visited file is maintained with version control,
1395and, if so, which kind. Its value is a string that appears in the mode
1396line, or @code{nil} for no version control.
a44af9f2
RS
1397@end defvar
1398
06862374
LK
1399@defvar mode-line-modes
1400This variable displays the buffer's major and minor modes. Here is a
1401simplified version of its default value. The real default value also
1402specifies addition of text properties.
1403
1404@example
1405@group
1406("%[(" mode-name
1407 mode-line-process minor-mode-alist
1408 "%n" ")%]--")
1409@end group
1410@end example
1411
1412So @code{mode-line-modes} normally also displays the recursive editing
1413level, information on the process status and whether narrowing is in
1414effect.
1415@end defvar
1416
1417 The following three variables are used in @code{mode-line-modes}:
1418
a44af9f2 1419@defvar mode-name
de9f0bd9 1420This buffer-local variable holds the ``pretty'' name of the current
a44af9f2
RS
1421buffer's major mode. Each major mode should set this variable so that the
1422mode name will appear in the mode line.
1423@end defvar
1424
06862374
LK
1425@defvar mode-line-process
1426This buffer-local variable contains the mode-line information on process
1427status in modes used for communicating with subprocesses. It is
1428displayed immediately following the major mode name, with no intervening
1429space. For example, its value in the @samp{*shell*} buffer is
1430@code{(":%s")}, which allows the shell to display its status along
1431with the major mode as: @samp{(Shell:run)}. Normally this variable
1432is @code{nil}.
1433@end defvar
1434
a44af9f2 1435@defvar minor-mode-alist
de9f0bd9 1436This variable holds an association list whose elements specify how the
a44af9f2
RS
1437mode line should indicate that a minor mode is active. Each element of
1438the @code{minor-mode-alist} should be a two-element list:
1439
1440@example
1441(@var{minor-mode-variable} @var{mode-line-string})
1442@end example
1443
06862374
LK
1444More generally, @var{mode-line-string} can be any mode-line spec. It
1445appears in the mode line when the value of @var{minor-mode-variable}
1446is non-@code{nil}, and not otherwise. These strings should begin with
a44af9f2 1447spaces so that they don't run together. Conventionally, the
06862374
LK
1448@var{minor-mode-variable} for a specific mode is set to a
1449non-@code{nil} value when that minor mode is activated.
a44af9f2 1450
f9f59935
RS
1451@code{minor-mode-alist} itself is not buffer-local. Each variable
1452mentioned in the alist should be buffer-local if its minor mode can be
1453enabled separately in each buffer.
a44af9f2
RS
1454@end defvar
1455
06862374 1456@defvar global-mode-string
1074a881
JB
1457This variable holds a mode-line spec that, by default, appears in the
1458mode line just after the @code{which-func-mode} minor mode if set,
1459else after @code{mode-line-modes}. The command @code{display-time}
06862374 1460sets @code{global-mode-string} to refer to the variable
1074a881
JB
1461@code{display-time-string}, which holds a string containing the time
1462and load information.
a40d4712 1463
06862374
LK
1464The @samp{%M} construct substitutes the value of
1465@code{global-mode-string}, but that is obsolete, since the variable is
1466included in the mode line from @code{mode-line-format}.
a40d4712
PR
1467@end defvar
1468
1469 The variable @code{default-mode-line-format} is where
1470@code{mode-line-format} usually gets its value:
1471
a44af9f2 1472@defvar default-mode-line-format
de9f0bd9 1473This variable holds the default @code{mode-line-format} for buffers
a44af9f2
RS
1474that do not override it. This is the same as @code{(default-value
1475'mode-line-format)}.
1476
06862374
LK
1477Here is a simplified version of the default value of
1478@code{default-mode-line-format}. The real default value also
1479specifies addition of text properties.
a44af9f2
RS
1480
1481@example
1482@group
f9f59935
RS
1483("-"
1484 mode-line-mule-info
a44af9f2 1485 mode-line-modified
f9f59935 1486 mode-line-frame-identification
a44af9f2 1487 mode-line-buffer-identification
f9f59935 1488@end group
a44af9f2 1489 " "
06862374
LK
1490 mode-line-position
1491 (vc-mode vc-mode)
1492 " "
f9f59935 1493@group
06862374 1494 mode-line-modes
f9f59935 1495 (which-func-mode ("" which-func-format "--"))
06862374 1496 (global-mode-string ("--" global-mode-string))
a44af9f2
RS
1497 "-%-")
1498@end group
1499@end example
1500@end defvar
1501
1502@node %-Constructs
1503@subsection @code{%}-Constructs in the Mode Line
1504
1505 The following table lists the recognized @code{%}-constructs and what
de9f0bd9
RS
1506they mean. In any construct except @samp{%%}, you can add a decimal
1507integer after the @samp{%} to specify how many characters to display.
a44af9f2
RS
1508
1509@table @code
1510@item %b
1511The current buffer name, obtained with the @code{buffer-name} function.
1512@xref{Buffer Names}.
1513
a40d4712
PR
1514@item %c
1515The current column number of point.
1516
a44af9f2
RS
1517@item %f
1518The visited file name, obtained with the @code{buffer-file-name}
1519function. @xref{Buffer File Name}.
1520
22697dac 1521@item %F
969fe9b5
RS
1522The title (only on a window system) or the name of the selected frame.
1523@xref{Window Frame Parameters}.
22697dac 1524
08622028
LK
1525@item %i
1526The size of the accessible part of the current buffer; basically
1527@code{(- (point-max) (point-min))}.
1528
1529@item %I
1530Like @samp{%i}, but the size is printed in a more readable way by using
1531@samp{k} for 10^3, @samp{M} for 10^6, @samp{G} for 10^9, etc., to
1532abbreviate.
1533
22697dac 1534@item %l
8241495d
RS
1535The current line number of point, counting within the accessible portion
1536of the buffer.
22697dac 1537
a40d4712
PR
1538@item %n
1539@samp{Narrow} when narrowing is in effect; nothing otherwise (see
1540@code{narrow-to-region} in @ref{Narrowing}).
1541
1542@item %p
1543The percentage of the buffer text above the @strong{top} of window, or
1544@samp{Top}, @samp{Bottom} or @samp{All}. Note that the default
1545mode-line specification truncates this to three characters.
1546
1547@item %P
1548The percentage of the buffer text that is above the @strong{bottom} of
1549the window (which includes the text visible in the window, as well as
1550the text above the top), plus @samp{Top} if the top of the buffer is
1551visible on screen; or @samp{Bottom} or @samp{All}.
1552
1553@item %s
1554The status of the subprocess belonging to the current buffer, obtained with
1555@code{process-status}. @xref{Process Information}.
1556
1557@item %t
1558Whether the visited file is a text file or a binary file. This is a
1559meaningful distinction only on certain operating systems (@pxref{MS-DOS
1560File Types}).
1561
a44af9f2
RS
1562@item %*
1563@samp{%} if the buffer is read only (see @code{buffer-read-only}); @*
1564@samp{*} if the buffer is modified (see @code{buffer-modified-p}); @*
1565@samp{-} otherwise. @xref{Buffer Modification}.
1566
1567@item %+
22697dac
KH
1568@samp{*} if the buffer is modified (see @code{buffer-modified-p}); @*
1569@samp{%} if the buffer is read only (see @code{buffer-read-only}); @*
1570@samp{-} otherwise. This differs from @samp{%*} only for a modified
1571read-only buffer. @xref{Buffer Modification}.
1572
1573@item %&
de9f0bd9 1574@samp{*} if the buffer is modified, and @samp{-} otherwise.
a44af9f2 1575
a44af9f2
RS
1576@item %[
1577An indication of the depth of recursive editing levels (not counting
1578minibuffer levels): one @samp{[} for each editing level.
1579@xref{Recursive Editing}.
1580
1581@item %]
1582One @samp{]} for each recursive editing level (not counting minibuffer
1583levels).
1584
a40d4712
PR
1585@item %-
1586Dashes sufficient to fill the remainder of the mode line.
1587
a44af9f2
RS
1588@item %%
1589The character @samp{%}---this is how to include a literal @samp{%} in a
1590string in which @code{%}-constructs are allowed.
a44af9f2
RS
1591@end table
1592
1593The following two @code{%}-constructs are still supported, but they are
1594obsolete, since you can get the same results with the variables
1595@code{mode-name} and @code{global-mode-string}.
1596
1597@table @code
1598@item %m
1599The value of @code{mode-name}.
1600
1601@item %M
1602The value of @code{global-mode-string}. Currently, only
1603@code{display-time} modifies the value of @code{global-mode-string}.
1604@end table
1605
8241495d
RS
1606@node Properties in Mode
1607@subsection Properties in the Mode Line
06862374 1608@cindex text properties in the mode line
8241495d
RS
1609
1610 Starting in Emacs 21, certain text properties are meaningful in the
1611mode line. The @code{face} property affects the appearance of text; the
1612@code{help-echo} property associate help strings with the text, and
ce75fd23 1613@code{local-map} can make the text mouse-sensitive.
8241495d 1614
06862374 1615 There are four ways to specify text properties for text in the mode
8241495d
RS
1616line:
1617
1618@enumerate
1619@item
06862374
LK
1620Put a string with a text property directly into the mode-line data
1621structure.
1622
1623@item
1624Put a text property on a mode-line %-construct such as @samp{%12b}; then
1625the expansion of the %-construct will have that same text property.
8241495d
RS
1626
1627@item
06862374
LK
1628Use a @code{(:propertize @var{elt} @var{props}@dots{})} construct to
1629give @var{elt} a text property specified by @var{props}.
8241495d
RS
1630
1631@item
1632Use a list containing @code{:eval @var{form}} in the mode-line data
06862374
LK
1633structure, and make @var{form} evaluate to a string that has a text
1634property.
8241495d
RS
1635@end enumerate
1636
ce75fd23 1637 You use the @code{local-map} property to specify a keymap. Like any
8241495d 1638keymap, it can bind character keys and function keys; but that has no
a40d4712 1639effect, since it is impossible to move point into the mode line. This
8241495d
RS
1640keymap can only take real effect for mouse clicks.
1641
1642@node Header Lines
1643@subsection Window Header Lines
1644@cindex header line (of a window)
1645@cindex window header line
1646
1647 Starting in Emacs 21, a window can have a @dfn{header line} at the
1648top, just as it can have a mode line at the bottom. The header line
06862374 1649feature works just like the mode-line feature, except that it's
8241495d
RS
1650controlled by different variables.
1651
1652@tindex header-line-format
1653@defvar header-line-format
1654This variable, local in every buffer, specifies how to display the
1655header line, for windows displaying the buffer. The format of the value
13ede7fc 1656is the same as for @code{mode-line-format} (@pxref{Mode Line Data}).
8241495d
RS
1657@end defvar
1658
1659@tindex default-header-line-format
1660@defvar default-header-line-format
1661This variable holds the default @code{header-line-format} for buffers
1662that do not override it. This is the same as @code{(default-value
1663'header-line-format)}.
1664
1665It is normally @code{nil}, so that ordinary buffers have no header line.
1666@end defvar
1667
bda7c6dd 1668@node Emulating Mode Line
06862374 1669@subsection Emulating Mode-Line Formatting
bda7c6dd
RS
1670
1671 You can use the function @code{format-mode-line} to compute
1672the text that would appear in a mode line or header line
06862374 1673based on certain mode-line specification.
bda7c6dd
RS
1674
1675@defun format-mode-line &optional format window no-props
1676This function formats a line of text according to @var{format} as if
1677it were generating the mode line for @var{window}, but instead of
1678displaying the text in the mode line or the header line, it returns
1679the text as a string.
1680
1681If @var{format} is @code{nil}, that means to use
1682@code{mode-line-format} and return the text that would appear in the
1683mode line. If @var{format} is @code{t}, that means to use
1684@code{header-line-format} so as to return the text that would appear
1685in the header line (@code{""} if the window has no header line).
1686The argument @var{window} defaults to the selected window.
1687
1688The value string normally has text properties that correspond to the
1689faces, keymaps, etc., that the mode line would have. If
1690@var{no-props} is non-@code{nil}, the value has no text properties.
1691@end defun
1692
f9f59935
RS
1693@node Imenu
1694@section Imenu
1695
1696@cindex Imenu
969fe9b5
RS
1697 @dfn{Imenu} is a feature that lets users select a definition or
1698section in the buffer, from a menu which lists all of them, to go
5a5d2aec
RS
1699directly to that location in the buffer. Imenu works by constructing
1700a buffer index which lists the names and buffer positions of the
a40d4712 1701definitions, or other named portions of the buffer; then the user can
5a5d2aec
RS
1702choose one of them and move point to it. The user-level commands for
1703using Imenu are described in the Emacs Manual (@pxref{Imenu,, Imenu,
1704emacs, the Emacs Manual}). This section explains how to customize
1705Imenu's method of finding definitions or buffer portions for a
a40d4712 1706particular major mode.
969fe9b5
RS
1707
1708 The usual and simplest way is to set the variable
1709@code{imenu-generic-expression}:
f9f59935
RS
1710
1711@defvar imenu-generic-expression
10ee4e90
LK
1712This variable, if non-@code{nil}, is a list that specifies regular
1713expressions for finding definitions for Imenu. Simple elements of
1714@code{imenu-generic-expression} look like this:
f9f59935
RS
1715
1716@example
10ee4e90 1717(@var{menu-title} @var{regexp} @var{index})
f9f59935
RS
1718@end example
1719
1720Here, if @var{menu-title} is non-@code{nil}, it says that the matches
1721for this element should go in a submenu of the buffer index;
1722@var{menu-title} itself specifies the name for the submenu. If
1723@var{menu-title} is @code{nil}, the matches for this element go directly
1724in the top level of the buffer index.
1725
1726The second item in the list, @var{regexp}, is a regular expression
10ee4e90
LK
1727(@pxref{Regular Expressions}); anything in the buffer that it matches
1728is considered a definition, something to mention in the buffer index.
1729The third item, @var{index}, is a non-negative integer that indicates
1730which subexpression in @var{regexp} matches the definition's name.
f9f59935
RS
1731
1732An element can also look like this:
1733
1734@example
1735(@var{menu-title} @var{regexp} @var{index} @var{function} @var{arguments}@dots{})
1736@end example
1737
10ee4e90
LK
1738Like in the previous case, each match for this element creates an
1739index item. However, if this index item is selected by the user, it
1740calls @var{function} with arguments consisting of the item name, the
1741buffer position, and @var{arguments}.
f9f59935 1742
10ee4e90
LK
1743For Emacs Lisp mode, @code{imenu-generic-expression} could look like
1744this:
f9f59935 1745
a9f0a989 1746@c should probably use imenu-syntax-alist and \\sw rather than [-A-Za-z0-9+]
f9f59935
RS
1747@example
1748@group
1749((nil "^\\s-*(def\\(un\\|subst\\|macro\\|advice\\)\
1750\\s-+\\([-A-Za-z0-9+]+\\)" 2)
1751@end group
1752@group
1753 ("*Vars*" "^\\s-*(def\\(var\\|const\\)\
1754\\s-+\\([-A-Za-z0-9+]+\\)" 2)
1755@end group
1756@group
1757 ("*Types*"
a9f0a989
RS
1758 "^\\s-*\
1759(def\\(type\\|struct\\|class\\|ine-condition\\)\
f9f59935
RS
1760\\s-+\\([-A-Za-z0-9+]+\\)" 2))
1761@end group
1762@end example
1763
969fe9b5 1764Setting this variable makes it buffer-local in the current buffer.
f9f59935
RS
1765@end defvar
1766
1767@defvar imenu-case-fold-search
10ee4e90
LK
1768This variable controls whether matching against the regular
1769expressions in the value of @code{imenu-generic-expression} is
1770case-sensitive: @code{t}, the default, means matching should ignore
1771case.
a9f0a989
RS
1772
1773Setting this variable makes it buffer-local in the current buffer.
1774@end defvar
1775
1776@defvar imenu-syntax-alist
1777This variable is an alist of syntax table modifiers to use while
1911e6e5
RS
1778processing @code{imenu-generic-expression}, to override the syntax table
1779of the current buffer. Each element should have this form:
a9f0a989
RS
1780
1781@example
1782(@var{characters} . @var{syntax-description})
1783@end example
1784
1785The @sc{car}, @var{characters}, can be either a character or a string.
1786The element says to give that character or characters the syntax
1787specified by @var{syntax-description}, which is passed to
1788@code{modify-syntax-entry} (@pxref{Syntax Table Functions}).
1789
1790This feature is typically used to give word syntax to characters which
1791normally have symbol syntax, and thus to simplify
1792@code{imenu-generic-expression} and speed up matching.
1793For example, Fortran mode uses it this way:
1794
1795@example
5a5d2aec 1796(setq imenu-syntax-alist '(("_$" . "w")))
a9f0a989
RS
1797@end example
1798
10ee4e90
LK
1799The @code{imenu-generic-expression} regular expressions can then use
1800@samp{\\sw+} instead of @samp{\\(\\sw\\|\\s_\\)+}. Note that this
1801technique may be inconvenient when the mode needs to limit the initial
1802character of a name to a smaller set of characters than are allowed in
1803the rest of a name.
f9f59935 1804
969fe9b5 1805Setting this variable makes it buffer-local in the current buffer.
f9f59935
RS
1806@end defvar
1807
1808 Another way to customize Imenu for a major mode is to set the
1809variables @code{imenu-prev-index-position-function} and
969fe9b5 1810@code{imenu-extract-index-name-function}:
f9f59935
RS
1811
1812@defvar imenu-prev-index-position-function
05aea714 1813If this variable is non-@code{nil}, its value should be a function that
a40d4712
PR
1814finds the next ``definition'' to put in the buffer index, scanning
1815backward in the buffer from point. It should return @code{nil} if it
5fe3b9bc 1816doesn't find another ``definition'' before point. Otherwise it should
a40d4712
PR
1817leave point at the place it finds a ``definition,'' and return any
1818non-@code{nil} value.
f9f59935 1819
969fe9b5 1820Setting this variable makes it buffer-local in the current buffer.
f9f59935
RS
1821@end defvar
1822
1823@defvar imenu-extract-index-name-function
1824If this variable is non-@code{nil}, its value should be a function to
969fe9b5
RS
1825return the name for a definition, assuming point is in that definition
1826as the @code{imenu-prev-index-position-function} function would leave
1827it.
f9f59935 1828
969fe9b5 1829Setting this variable makes it buffer-local in the current buffer.
f9f59935
RS
1830@end defvar
1831
969fe9b5 1832 The last way to customize Imenu for a major mode is to set the
a40d4712 1833variable @code{imenu-create-index-function}:
969fe9b5 1834
f9f59935 1835@defvar imenu-create-index-function
10ee4e90
LK
1836This variable specifies the function to use for creating a buffer
1837index. The function should take no arguments, and return an index
1838alist for the current buffer. It is called within
1839@code{save-excursion}, so where it leaves point makes no difference.
f9f59935 1840
10ee4e90
LK
1841The index alist can have three types of elements. Simple elements
1842look like this:
f9f59935 1843
10ee4e90
LK
1844@example
1845(@var{index-name} . @var{index-position})
1846@end example
f9f59935 1847
10ee4e90
LK
1848Selecting a simple element has the effect of moving to position
1849@var{index-position} in the buffer. Special elements look like this:
f9f59935 1850
10ee4e90
LK
1851@example
1852(@var{index-name} @var{index-position} @var{function} @var{arguments}@dots{})
1853@end example
f9f59935 1854
10ee4e90 1855Selecting a special element performs:
f9f59935
RS
1856
1857@example
c22c5da6 1858(funcall @var{function}
10ee4e90 1859 @var{index-name} @var{index-position} @var{arguments}@dots{})
f9f59935
RS
1860@end example
1861
10ee4e90
LK
1862A nested sub-alist element looks like this:
1863
1864@example
f008b925 1865(@var{menu-title} @var{sub-alist})
10ee4e90
LK
1866@end example
1867
f008b925 1868It creates the submenu @var{menu-title} specified by @var{sub-alist}.
10ee4e90 1869
f008b925
LK
1870The default value of @code{imenu-create-index-function} is
1871@code{imenu-default-create-index-function}. This function uses
1872@code{imenu-prev-index-position-function} and
10ee4e90
LK
1873@code{imenu-extract-index-name-function} to produce the index alist.
1874However, if either of these two variables is @code{nil}, the default
1875function uses @code{imenu-generic-expression} instead.
1876
1877Setting this variable makes it buffer-local in the current buffer.
f9f59935
RS
1878@end defvar
1879
1880@node Font Lock Mode
1881@section Font Lock Mode
1882@cindex Font Lock Mode
1883
1884 @dfn{Font Lock mode} is a feature that automatically attaches
1885@code{face} properties to certain parts of the buffer based on their
1886syntactic role. How it parses the buffer depends on the major mode;
a40d4712 1887most major modes define syntactic criteria for which faces to use in
969fe9b5 1888which contexts. This section explains how to customize Font Lock for a
a40d4712 1889particular major mode.
f9f59935
RS
1890
1891 Font Lock mode finds text to highlight in two ways: through syntactic
1892parsing based on the syntax table, and through searching (usually for
1893regular expressions). Syntactic fontification happens first; it finds
1894comments and string constants, and highlights them using
1895@code{font-lock-comment-face} and @code{font-lock-string-face}
a40d4712 1896(@pxref{Faces for Font Lock}). Search-based fontification follows.
f9f59935
RS
1897
1898@menu
1899* Font Lock Basics::
1900* Search-based Fontification::
1901* Other Font Lock Variables::
1902* Levels of Font Lock::
651f7556 1903* Precalculated Fontification::
f9f59935 1904* Faces for Font Lock::
969fe9b5 1905* Syntactic Font Lock::
f9f59935
RS
1906@end menu
1907
1908@node Font Lock Basics
1909@subsection Font Lock Basics
1910
1911 There are several variables that control how Font Lock mode highlights
1912text. But major modes should not set any of these variables directly.
86494bd5 1913Instead, they should set @code{font-lock-defaults} as a buffer-local
969fe9b5
RS
1914variable. The value assigned to this variable is used, if and when Font
1915Lock mode is enabled, to set all the other variables.
f9f59935
RS
1916
1917@defvar font-lock-defaults
1918This variable is set by major modes, as a buffer-local variable, to
1919specify how to fontify text in that mode. The value should look like
1920this:
1921
1922@example
1923(@var{keywords} @var{keywords-only} @var{case-fold}
1924 @var{syntax-alist} @var{syntax-begin} @var{other-vars}@dots{})
1925@end example
1926
1927The first element, @var{keywords}, indirectly specifies the value of
1928@code{font-lock-keywords}. It can be a symbol, a variable whose value
a40d4712 1929is the list to use for @code{font-lock-keywords}. It can also be a list of
f9f59935
RS
1930several such symbols, one for each possible level of fontification. The
1931first symbol specifies how to do level 1 fontification, the second
1932symbol how to do level 2, and so on.
1933
1934The second element, @var{keywords-only}, specifies the value of the
a9f0a989 1935variable @code{font-lock-keywords-only}. If this is non-@code{nil},
969fe9b5 1936syntactic fontification (of strings and comments) is not performed.
f9f59935
RS
1937
1938The third element, @var{case-fold}, specifies the value of
1939@code{font-lock-case-fold-search}. If it is non-@code{nil}, Font Lock
969fe9b5
RS
1940mode ignores case when searching as directed by
1941@code{font-lock-keywords}.
f9f59935
RS
1942
1943If the fourth element, @var{syntax-alist}, is non-@code{nil}, it should be
1944a list of cons cells of the form @code{(@var{char-or-string}
1945. @var{string})}. These are used to set up a syntax table for
1946fontification (@pxref{Syntax Table Functions}). The resulting syntax
1947table is stored in @code{font-lock-syntax-table}.
1948
1949The fifth element, @var{syntax-begin}, specifies the value of
969fe9b5 1950@code{font-lock-beginning-of-syntax-function} (see below).
f9f59935 1951
a40d4712
PR
1952All the remaining elements (if any) are collectively called
1953@var{other-vars}. Each of these elements should have the form
1954@code{(@var{variable} . @var{value})}---which means, make @var{variable}
1955buffer-local and then set it to @var{value}. You can use these
1956@var{other-vars} to set other variables that affect fontification,
1957aside from those you can control with the first five elements.
f9f59935
RS
1958@end defvar
1959
1960@node Search-based Fontification
1961@subsection Search-based Fontification
1962
1963 The most important variable for customizing Font Lock mode is
1964@code{font-lock-keywords}. It specifies the search criteria for
1965search-based fontification.
1966
1967@defvar font-lock-keywords
1968This variable's value is a list of the keywords to highlight. Be
969fe9b5
RS
1969careful when composing regular expressions for this list; a poorly
1970written pattern can dramatically slow things down!
f9f59935
RS
1971@end defvar
1972
1973 Each element of @code{font-lock-keywords} specifies how to find
969fe9b5
RS
1974certain cases of text, and how to highlight those cases. Font Lock mode
1975processes the elements of @code{font-lock-keywords} one by one, and for
1976each element, it finds and handles all matches. Ordinarily, once
1977part of the text has been fontified already, this cannot be overridden
1978by a subsequent match in the same text; but you can specify different
1979behavior using the @var{override} element of a @var{highlighter}.
1980
1981 Each element of @code{font-lock-keywords} should have one of these
1982forms:
f9f59935
RS
1983
1984@table @code
1985@item @var{regexp}
1986Highlight all matches for @var{regexp} using
1987@code{font-lock-keyword-face}. For example,
1988
1989@example
1990;; @r{Highlight discrete occurrences of @samp{foo}}
1991;; @r{using @code{font-lock-keyword-face}.}
1992"\\<foo\\>"
1993@end example
1994
969fe9b5
RS
1995The function @code{regexp-opt} (@pxref{Syntax of Regexps}) is useful for
1996calculating optimal regular expressions to match a number of different
1997keywords.
f9f59935
RS
1998
1999@item @var{function}
2000Find text by calling @var{function}, and highlight the matches
2001it finds using @code{font-lock-keyword-face}.
2002
2003When @var{function} is called, it receives one argument, the limit of
022cb162 2004the search; it should begin searching at point, and not search beyond the
07f7b41c
RS
2005limit. It should return non-@code{nil} if it succeeds, and set the
2006match data to describe the match that was found. Returning @code{nil}
2007indicates failure of the search.
2008
2009Fontification will call @var{function} repeatedly with the same limit,
2010and with point where the previous invocation left it, until
2011@var{function} fails. On failure, @var{function} need not reset point
2012in any particular way.
f9f59935
RS
2013
2014@item (@var{matcher} . @var{match})
86494bd5 2015In this kind of element, @var{matcher} is either a regular
f9f59935
RS
2016expression or a function, as described above. The @sc{cdr},
2017@var{match}, specifies which subexpression of @var{matcher} should be
969fe9b5 2018highlighted (instead of the entire text that @var{matcher} matched).
f9f59935
RS
2019
2020@example
8241495d 2021;; @r{Highlight the @samp{bar} in each occurrence of @samp{fubar},}
f9f59935
RS
2022;; @r{using @code{font-lock-keyword-face}.}
2023("fu\\(bar\\)" . 1)
2024@end example
2025
969fe9b5 2026If you use @code{regexp-opt} to produce the regular expression
f9f59935
RS
2027@var{matcher}, then you can use @code{regexp-opt-depth} (@pxref{Syntax
2028of Regexps}) to calculate the value for @var{match}.
2029
3ab66863
RS
2030@item (@var{matcher} . @var{facespec})
2031In this kind of element, @var{facespec} is an object which specifies
2032the face variable to use for highlighting. In the simplest case, it
2033is a Lisp variable (a symbol), whose value should be a face name.
f9f59935
RS
2034
2035@example
2036;; @r{Highlight occurrences of @samp{fubar},}
2037;; @r{using the face which is the value of @code{fubar-face}.}
2038("fubar" . fubar-face)
2039@end example
2040
3ab66863 2041However, @var{facespec} can also be a list of the form
fdba9ef4
RS
2042
2043@example
2044(face @var{face} @var{prop1} @var{val1} @var{prop2} @var{val2}@dots{})
2045@end example
2046
2047to specify various text properties to put on the text that matches.
2048If you do this, be sure to add the other text property names that you
2049set in this way to the value of @code{font-lock-extra-managed-props}
2050so that the properties will also be cleared out when they are no longer
2051appropriate.
2052
f9f59935
RS
2053@item (@var{matcher} . @var{highlighter})
2054In this kind of element, @var{highlighter} is a list
2055which specifies how to highlight matches found by @var{matcher}.
2056It has the form
2057
2058@example
3ab66863 2059(@var{subexp} @var{facespec} @var{override} @var{laxmatch})
f9f59935
RS
2060@end example
2061
2062The @sc{car}, @var{subexp}, is an integer specifying which subexpression
969fe9b5 2063of the match to fontify (0 means the entire matching text). The second
3ab66863 2064subelement, @var{facespec}, specifies the face, as described above.
f9f59935
RS
2065
2066The last two values in @var{highlighter}, @var{override} and
bda7c6dd
RS
2067@var{laxmatch}, are flags. If @var{override} is @code{t}, this
2068element can override existing fontification made by previous elements
2069of @code{font-lock-keywords}. If it is @code{keep}, then each
2070character is fontified if it has not been fontified already by some
3ab66863
RS
2071other element. If it is @code{prepend}, the face specified by
2072@var{facespec} is added to the beginning of the @code{font-lock-face}
2073property. If it is @code{append}, the face is added to the end of the
bda7c6dd 2074@code{font-lock-face} property.
f9f59935
RS
2075
2076If @var{laxmatch} is non-@code{nil}, it means there should be no error
2077if there is no subexpression numbered @var{subexp} in @var{matcher}.
99b62845
GM
2078Obviously, fontification of the subexpression numbered @var{subexp} will
2079not occur. However, fontification of other subexpressions (and other
2080regexps) will continue. If @var{laxmatch} is @code{nil}, and the
2081specified subexpression is missing, then an error is signalled which
2082terminates search-based fontification.
f9f59935
RS
2083
2084Here are some examples of elements of this kind, and what they do:
2085
2086@smallexample
2087;; @r{Highlight occurrences of either @samp{foo} or @samp{bar},}
2088;; @r{using @code{foo-bar-face}, even if they have already been highlighted.}
2089;; @r{@code{foo-bar-face} should be a variable whose value is a face.}
2090("foo\\|bar" 0 foo-bar-face t)
2091
8241495d 2092;; @r{Highlight the first subexpression within each occurrence}
f9f59935
RS
2093;; @r{that the function @code{fubar-match} finds,}
2094;; @r{using the face which is the value of @code{fubar-face}.}
2095(fubar-match 1 fubar-face)
2096@end smallexample
2097
2098@item (@var{matcher} @var{highlighters}@dots{})
2099This sort of element specifies several @var{highlighter} lists for a
2100single @var{matcher}. In order for this to be useful, each
2101@var{highlighter} should have a different value of @var{subexp}; that is,
2102each one should apply to a different subexpression of @var{matcher}.
2103
2104@ignore
2105@item (@var{matcher} . @var{anchored})
2106In this kind of element, @var{anchored} acts much like a
2107@var{highlighter}, but it is more complex and can specify multiple
2108successive searches.
2109
2110For highlighting single items, typically only @var{highlighter} is
2111required. However, if an item or (typically) items are to be
2112highlighted following the instance of another item (the anchor) then
2113@var{anchored} may be required.
2114
2115It has this format:
2116
2117@example
2118(@var{submatcher} @var{pre-match-form} @var{post-match-form} @var{highlighters}@dots{})
2119@end example
2120
2121@c I can't parse this text -- rms
2122where @var{submatcher} is much like @var{matcher}, with one
2123exception---see below. @var{pre-match-form} and @var{post-match-form}
2124are evaluated before the first, and after the last, instance
2125@var{anchored}'s @var{submatcher} is used. Therefore they can be used
a9f0a989 2126to initialize before, and cleanup after, @var{submatcher} is used.
f9f59935
RS
2127Typically, @var{pre-match-form} is used to move to some position
2128relative to the original @var{submatcher}, before starting with
2129@var{anchored}'s @var{submatcher}. @var{post-match-form} might be used
2130to move, before resuming with @var{anchored}'s parent's @var{matcher}.
2131
2132For example, an element of the form highlights (if not already highlighted):
2133
2134@example
2135("\\<anchor\\>" (0 anchor-face) ("\\<item\\>" nil nil (0 item-face)))
2136@end example
2137
2138Discrete occurrences of @samp{anchor} in the value of
2139@code{anchor-face}, and subsequent discrete occurrences of @samp{item}
2140(on the same line) in the value of @code{item-face}. (Here
2141@var{pre-match-form} and @var{post-match-form} are @code{nil}.
2142Therefore @samp{item} is initially searched for starting from the end of
2143the match of @samp{anchor}, and searching for subsequent instance of
2144@samp{anchor} resumes from where searching for @samp{item} concluded.)
2145
2146The above-mentioned exception is as follows. The limit of the
2147@var{submatcher} search defaults to the end of the line after
2148@var{pre-match-form} is evaluated. However, if @var{pre-match-form}
2149returns a position greater than the position after @var{pre-match-form}
2150is evaluated, that position is used as the limit of the search. It is
2151generally a bad idea to return a position greater than the end of the
2152line; in other words, the @var{submatcher} search should not span lines.
2153
2154@item (@var{matcher} @var{highlighters-or-anchoreds} ...)
2155@end ignore
2156
2157@item (eval . @var{form})
969fe9b5 2158Here @var{form} is an expression to be evaluated the first time
f9f59935 2159this value of @code{font-lock-keywords} is used in a buffer.
969fe9b5 2160Its value should have one of the forms described in this table.
f9f59935
RS
2161@end table
2162
2163@strong{Warning:} Do not design an element of @code{font-lock-keywords}
2164to match text which spans lines; this does not work reliably. While
2165@code{font-lock-fontify-buffer} handles multi-line patterns correctly,
2166updating when you edit the buffer does not, since it considers text one
8ba2808b
SM
2167line at a time. If you have patterns that typically only span one
2168line but can occasionally span two or three, such as
2169@samp{<title>...</title>}, you can ask font-lock to be more careful by
2170setting @code{font-lock-multiline} to @code{t}. But it still will not
2171work in all cases.
f9f59935 2172
f9f59935
RS
2173@node Other Font Lock Variables
2174@subsection Other Font Lock Variables
2175
2176 This section describes additional variables that a major mode
2177can set by means of @code{font-lock-defaults}.
2178
2179@defvar font-lock-keywords-only
2180Non-@code{nil} means Font Lock should not fontify comments or strings
969fe9b5
RS
2181syntactically; it should only fontify based on
2182@code{font-lock-keywords}.
f9f59935
RS
2183@end defvar
2184
2185@ignore
a9f0a989 2186Other variables include those for buffer-specialized fontification functions,
f9f59935
RS
2187`font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function',
2188`font-lock-fontify-region-function', `font-lock-unfontify-region-function',
2189`font-lock-inhibit-thing-lock' and `font-lock-maximum-size'.
2190@end ignore
2191
2192@defvar font-lock-keywords-case-fold-search
969fe9b5
RS
2193Non-@code{nil} means that regular expression matching for the sake of
2194@code{font-lock-keywords} should be case-insensitive.
f9f59935
RS
2195@end defvar
2196
969fe9b5
RS
2197@defvar font-lock-syntax-table
2198This variable specifies the syntax table to use for fontification of
2199comments and strings.
2200@end defvar
f9f59935 2201
969fe9b5
RS
2202@defvar font-lock-beginning-of-syntax-function
2203If this variable is non-@code{nil}, it should be a function to move
2204point back to a position that is syntactically at ``top level'' and
2205outside of strings or comments. Font Lock uses this when necessary
2206to get the right results for syntactic fontification.
f9f59935
RS
2207
2208This function is called with no arguments. It should leave point at the
2209beginning of any enclosing syntactic block. Typical values are
2210@code{beginning-of-line} (i.e., the start of the line is known to be
2211outside a syntactic block), or @code{beginning-of-defun} for programming
2212modes or @code{backward-paragraph} for textual modes (i.e., the
2213mode-dependent function is known to move outside a syntactic block).
2214
2215If the value is @code{nil}, the beginning of the buffer is used as a
969fe9b5
RS
2216position outside of a syntactic block. This cannot be wrong, but it can
2217be slow.
f9f59935
RS
2218@end defvar
2219
2220@defvar font-lock-mark-block-function
969fe9b5
RS
2221If this variable is non-@code{nil}, it should be a function that is
2222called with no arguments, to choose an enclosing range of text for
2223refontification for the command @kbd{M-g M-g}
2224(@code{font-lock-fontify-block}).
2225
2226The function should report its choice by placing the region around it.
2227A good choice is a range of text large enough to give proper results,
2228but not too large so that refontification becomes slow. Typical values
2229are @code{mark-defun} for programming modes or @code{mark-paragraph} for
2230textual modes.
f9f59935
RS
2231@end defvar
2232
fdba9ef4 2233@defvar font-lock-extra-managed-props
bda7c6dd
RS
2234Additional properties (other than @code{font-lock-face}) that are
2235being managed by Font Lock mode. Font Lock mode normally manages only
2236the @code{font-lock-face} property; if you want it to manage others as
3ab66863 2237well, you must specify them in a @var{facespec} in
bda7c6dd 2238@code{font-lock-keywords} as well as adding them to this list.
fdba9ef4
RS
2239@end defvar
2240
8ba2808b
SM
2241@defvar font-lock-syntactic-face-function
2242A function to determine which face to use for a given syntactic
2243element (a string or a comment). The function is called with one
2244argument, the parse state at point returned by
2245@code{parse-partial-sexp}, and should return a face. The default
2246value returns @code{font-lock-comment-face} for comments and
2247@code{font-lock-string-face} for strings.
2248
2249This can be used to highlighting different kinds of strings or
2250comments differently. It is also sometimes abused together with
2251@code{font-lock-syntactic-keywords} to highlight elements that span
2252multiple lines, but this is too obscure to document in this manual.
2253@end defvar
2254
f9f59935
RS
2255@node Levels of Font Lock
2256@subsection Levels of Font Lock
2257
2258 Many major modes offer three different levels of fontification. You
2259can define multiple levels by using a list of symbols for @var{keywords}
2260in @code{font-lock-defaults}. Each symbol specifies one level of
2261fontification; it is up to the user to choose one of these levels. The
2262chosen level's symbol value is used to initialize
2263@code{font-lock-keywords}.
2264
969fe9b5
RS
2265 Here are the conventions for how to define the levels of
2266fontification:
2267
f9f59935
RS
2268@itemize @bullet
2269@item
2270Level 1: highlight function declarations, file directives (such as include or
2271import directives), strings and comments. The idea is speed, so only
2272the most important and top-level components are fontified.
2273
2274@item
969fe9b5
RS
2275Level 2: in addition to level 1, highlight all language keywords,
2276including type names that act like keywords, as well as named constant
2277values. The idea is that all keywords (either syntactic or semantic)
2278should be fontified appropriately.
f9f59935
RS
2279
2280@item
969fe9b5
RS
2281Level 3: in addition to level 2, highlight the symbols being defined in
2282function and variable declarations, and all builtin function names,
2283wherever they appear.
f9f59935
RS
2284@end itemize
2285
651f7556
CW
2286@node Precalculated Fontification
2287@subsection Precalculated Fontification
2288
2289In addition to using @code{font-lock-defaults} for search-based
2290fontification, you may use the special character property
2291@code{font-lock-face} (@pxref{Special Properties}). This property
2292acts just like the explicit @code{face} property, but its activation
2293is toggled when the user calls @kbd{M-x font-lock-mode}. Using
06862374 2294@code{font-lock-face} is especially convenient for special modes
651f7556
CW
2295which construct their text programmatically, such as
2296@code{list-buffers} and @code{occur}.
2297
0ab0c481
CW
2298If your mode does not use any of the other machinery of Font Lock
2299(i.e. it only uses the @code{font-lock-face} property), you can tell
2300Emacs not to load all of font-lock.el (unless it's already loaded), by
6fe50867
RS
2301setting the variable @code{font-lock-core-only} to non-@code{nil} as
2302part of the @code{font-lock-defaults} settings. Here is the canonical
2303way to do this:
0ab0c481
CW
2304
2305@example
2306(set (make-local-variable 'font-lock-defaults)
2307 '(nil t nil nil nil (font-lock-core-only . t)))
2308@end example
2309
f9f59935
RS
2310@node Faces for Font Lock
2311@subsection Faces for Font Lock
2312
2313 You can make Font Lock mode use any face, but several faces are
2314defined specifically for Font Lock mode. Each of these symbols is both
2315a face name, and a variable whose default value is the symbol itself.
2316Thus, the default value of @code{font-lock-comment-face} is
2317@code{font-lock-comment-face}. This means you can write
2318@code{font-lock-comment-face} in a context such as
2319@code{font-lock-keywords} where a face-name-valued expression is used.
2320
2321@table @code
2322@item font-lock-comment-face
2323@vindex font-lock-comment-face
f9f59935
RS
2324Used (typically) for comments.
2325
2326@item font-lock-string-face
2327@vindex font-lock-string-face
f9f59935
RS
2328Used (typically) for string constants.
2329
2330@item font-lock-keyword-face
2331@vindex font-lock-keyword-face
f9f59935
RS
2332Used (typically) for keywords---names that have special syntactic
2333significance, like @code{for} and @code{if} in C.
2334
2335@item font-lock-builtin-face
2336@vindex font-lock-builtin-face
f9f59935
RS
2337Used (typically) for built-in function names.
2338
2339@item font-lock-function-name-face
2340@vindex font-lock-function-name-face
f9f59935 2341Used (typically) for the name of a function being defined or declared,
177c0ea7 2342in a function definition or declaration.
f9f59935
RS
2343
2344@item font-lock-variable-name-face
2345@vindex font-lock-variable-name-face
f9f59935
RS
2346Used (typically) for the name of a variable being defined or declared,
2347in a variable definition or declaration.
2348
2349@item font-lock-type-face
2350@vindex font-lock-type-face
f9f59935
RS
2351Used (typically) for names of user-defined data types,
2352where they are defined and where they are used.
2353
2354@item font-lock-constant-face
2355@vindex font-lock-constant-face
f9f59935
RS
2356Used (typically) for constant names.
2357
c22c5da6
LK
2358@item font-lock-preprocessor-face
2359@vindex font-lock-preprocessor-face
fdba9ef4
RS
2360Used (typically) for preprocessor commands.
2361
f9f59935
RS
2362@item font-lock-warning-face
2363@vindex font-lock-warning-face
f9f59935
RS
2364Used (typically) for constructs that are peculiar, or that greatly
2365change the meaning of other text. For example, this is used for
2366@samp{;;;###autoload} cookies in Emacs Lisp, and for @code{#error}
2367directives in C.
2368@end table
2369
969fe9b5
RS
2370@node Syntactic Font Lock
2371@subsection Syntactic Font Lock
2372
2373 Font Lock mode can be used to update @code{syntax-table} properties
2374automatically. This is useful in languages for which a single syntax
2375table by itself is not sufficient.
2376
2377@defvar font-lock-syntactic-keywords
f8cecb20
DL
2378This variable enables and controls syntactic Font Lock. It is
2379normally set via @code{font-lock-defaults}. Its value should be a
2380list of elements of this form:
969fe9b5
RS
2381
2382@example
2383(@var{matcher} @var{subexp} @var{syntax} @var{override} @var{laxmatch})
2384@end example
2385
2386The parts of this element have the same meanings as in the corresponding
2387sort of element of @code{font-lock-keywords},
2388
2389@example
2390(@var{matcher} @var{subexp} @var{facename} @var{override} @var{laxmatch})
2391@end example
2392
2393However, instead of specifying the value @var{facename} to use for the
f8cecb20
DL
2394@code{face} property, it specifies the value @var{syntax} to use for
2395the @code{syntax-table} property. Here, @var{syntax} can be a string
2396(as taken by @code{modify-syntax-entry}), a syntax table, a cons cell
2397(as returned by @code{string-to-syntax}), or an expression whose value
2398is one of those two types. @var{override} cannot be @code{prepend} or
2399@code{append}.
2400
2401For example, an element of the form:
2402
2403@example
2404("\\$\\(#\\)" 1 ".")
2405@end example
2406
2407highlights syntactically a hash character when following a dollar
2408character, with a SYNTAX of @code{"."} (meaning punctuation syntax).
2409Assuming that the buffer syntax table specifies hash characters to
2410have comment start syntax, the element will only highlight hash
2411characters that do not follow dollar characters as comments
2412syntactically.
2413
2414An element of the form:
2415
2416@example
2417 ("\\('\\).\\('\\)"
2418 (1 "\"")
2419 (2 "\""))
2420@end example
2421
2422highlights syntactically both single quotes which surround a single
2423character, with a SYNTAX of @code{"\""} (meaning string quote syntax).
2424Assuming that the buffer syntax table does not specify single quotes
2425to have quote syntax, the element will only highlight single quotes of
2426the form @samp{'@var{c}'} as strings syntactically. Other forms, such
2427as @samp{foo'bar} or @samp{'fubar'}, will not be highlighted as
2428strings.
2429
969fe9b5
RS
2430@end defvar
2431
f730cc62
LH
2432@node Desktop Save Mode
2433@section Desktop Save Mode
2434@cindex desktop save mode
2435
2436@dfn{Desktop Save Mode} is a feature to save the state of Emacs from
2437one session to another. The user-level commands for using Desktop
2438Save Mode are described in the GNU Emacs Manual (@pxref{Saving Emacs
2439Sessions,,, emacs, the GNU Emacs Manual}). Modes whose buffers visit
2440a file, don't have to do anything to use this feature.
2441
2442For buffers not visiting a file to have their state saved, the major
2443mode must bind the buffer local variable @code{desktop-save-buffer} to
2444a non-nil value.
2445
2446@defvar desktop-save-buffer
2447If this buffer-local variable is non-@code{nil}, the buffer will have
2448its state saved in the desktop file at desktop save. If the value is
2449a function, it is called at desktop save with argument
2450@var{desktop-dirname}, and its value is saved in the desktop file along
2451with the state of the buffer for which it was called. When file names
2452are returned as part of the auxiliary information, they should be
2453formatted using the call
2454
2455@example
2456(desktop-file-name @var{file-name} @var{desktop-dirname})
2457@end example
2458
2459@end defvar
2460
2461For buffers not visiting a file to be restored, the major mode must
2462define a function to do the job, and that function must be listed in
2463the alist @code{desktop-buffer-mode-handlers}.
2464
2465@defvar desktop-buffer-mode-handlers
2466Alist with elements
2467
2468@example
2469(@var{major-mode} . @var{restore-buffer-function})
2470@end example
2471
2472The function @var{restore-buffer-function} will be called with
2473argument list
2474
2475@example
2476(@var{buffer-file-name} @var{buffer-name} @var{desktop-buffer-misc})
2477@end example
2478
2479and it should return the restored buffer.
2480Here @var{desktop-buffer-misc} is the value returned by the function
2481optionally bound to @code{desktop-save-buffer}.
2482
2483@end defvar
2484
a44af9f2
RS
2485@node Hooks
2486@section Hooks
2487@cindex hooks
2488
2489 A @dfn{hook} is a variable where you can store a function or functions
2490to be called on a particular occasion by an existing program. Emacs
2491provides hooks for the sake of customization. Most often, hooks are set
a40d4712 2492up in the init file (@pxref{Init File}), but Lisp programs can set them also.
a44af9f2
RS
2493@xref{Standard Hooks}, for a list of standard hook variables.
2494
f9f59935 2495@cindex normal hook
a44af9f2 2496 Most of the hooks in Emacs are @dfn{normal hooks}. These variables
dd73b091
RS
2497contain lists of functions to be called with no arguments. When the
2498hook name ends in @samp{-hook}, that tells you it is normal. We try to
2499make all hooks normal, as much as possible, so that you can use them in
2500a uniform way.
2501
2502 Every major mode function is supposed to run a normal hook called the
2503@dfn{mode hook} as the last step of initialization. This makes it easy
2504for a user to customize the behavior of the mode, by overriding the
969fe9b5
RS
2505buffer-local variable assignments already made by the mode. But hooks
2506are used in other contexts too. For example, the hook
2507@code{suspend-hook} runs just before Emacs suspends itself
2508(@pxref{Suspending Emacs}).
a44af9f2
RS
2509
2510 The recommended way to add a hook function to a normal hook is by
2511calling @code{add-hook} (see below). The hook functions may be any of
da3178e2
RS
2512the valid kinds of functions that @code{funcall} accepts (@pxref{What
2513Is a Function}). Most normal hook variables are initially void;
2514@code{add-hook} knows how to deal with this. You can add hooks either
2515globally or buffer-locally with @code{add-hook}.
c22c5da6 2516
f9f59935 2517@cindex abnormal hook
dd73b091 2518 If the hook variable's name does not end with @samp{-hook}, that
a40d4712 2519indicates it is probably an @dfn{abnormal hook}. Then you should look at its
dd73b091
RS
2520documentation to see how to use the hook properly.
2521
2522 If the variable's name ends in @samp{-functions} or @samp{-hooks},
2523then the value is a list of functions, but it is abnormal in that either
2524these functions are called with arguments or their values are used in
2525some way. You can use @code{add-hook} to add a function to the list,
2526but you must take care in writing the function. (A few of these
89cda0c5
SM
2527variables, notably those ending in @samp{-hooks}, are actually
2528normal hooks which were named before we established the convention of
2529using @samp{-hook} for them.)
dd73b091
RS
2530
2531 If the variable's name ends in @samp{-function}, then its value
2532is just a single function, not a list of functions.
a44af9f2 2533
969fe9b5
RS
2534 Here's an example that uses a mode hook to turn on Auto Fill mode when
2535in Lisp Interaction mode:
a44af9f2
RS
2536
2537@example
2538(add-hook 'lisp-interaction-mode-hook 'turn-on-auto-fill)
a44af9f2
RS
2539@end example
2540
2541 At the appropriate time, Emacs uses the @code{run-hooks} function to
bfe721d1
KH
2542run particular hooks. This function calls the hook functions that have
2543been added with @code{add-hook}.
a44af9f2 2544
a40d4712 2545@defun run-hooks &rest hookvars
c22c5da6
LK
2546This function takes one or more normal hook variable names as
2547arguments, and runs each hook in turn. Each argument should be a
da3178e2
RS
2548symbol that is a normal hook variable. These arguments are processed
2549in the order specified.
a44af9f2
RS
2550
2551If a hook variable has a non-@code{nil} value, that value may be a
da3178e2
RS
2552function or a list of functions. (The former option is considered
2553obsolete.) If the value is a function (either a lambda expression or
2554a symbol with a function definition), it is called. If it is a list
2555that isn't a function, its elements are called, consecutively. All
2556the hook functions are called with no arguments.
a44af9f2 2557
bfe721d1 2558For example, here's how @code{emacs-lisp-mode} runs its mode hook:
a44af9f2
RS
2559
2560@example
2561(run-hooks 'emacs-lisp-mode-hook)
2562@end example
2563@end defun
2564
fdba9ef4
RS
2565@defun run-mode-hooks &rest hookvars
2566Like @code{run-hooks}, but is affected by the @code{delay-mode-hooks}
2567macro.
2568@end defun
2569
2570@defmac delay-mode-hooks body...
2571This macro executes the @var{body} forms but defers all calls to
2572@code{run-mode-hooks} within them until the end of @var{body}.
2573This macro enables a derived mode to arrange not to run
2574its parent modes' mode hooks until the end.
2575@end defmac
2576
a9f0a989 2577@defun run-hook-with-args hook &rest args
da3178e2
RS
2578This function is the way to run an abnormal hook and always call all
2579of the hook functions. It calls each of the hook functions one by
2580one, passing each of them the arguments @var{args}.
a9f0a989
RS
2581@end defun
2582
2583@defun run-hook-with-args-until-failure hook &rest args
c22c5da6
LK
2584This function is the way to run an abnormal hook until one of the hook
2585functions fails. It calls each of the hook functions, passing each of
2586them the arguments @var{args}, until some hook function returns
2587@code{nil}. It then stops and returns @code{nil}. If none of the
2588hook functions return @code{nil}, it returns a non-@code{nil} value.
a9f0a989
RS
2589@end defun
2590
2591@defun run-hook-with-args-until-success hook &rest args
c22c5da6
LK
2592This function is the way to run an abnormal hook until a hook function
2593succeeds. It calls each of the hook functions, passing each of them
2594the arguments @var{args}, until some hook function returns
2595non-@code{nil}. Then it stops, and returns whatever was returned by
2596the last hook function that was called. If all hook functions return
2597@code{nil}, it returns @code{nil} as well.
a9f0a989
RS
2598@end defun
2599
22697dac 2600@defun add-hook hook function &optional append local
a44af9f2 2601This function is the handy way to add function @var{function} to hook
da3178e2
RS
2602variable @var{hook}. You can use it for abnormal hooks as well as for
2603normal hooks. @var{function} can be any Lisp function that can accept
2604the proper number of arguments for @var{hook}. For example,
a44af9f2
RS
2605
2606@example
2607(add-hook 'text-mode-hook 'my-text-hook-function)
2608@end example
2609
2610@noindent
2611adds @code{my-text-hook-function} to the hook called @code{text-mode-hook}.
2612
da3178e2
RS
2613If @var{function} is already present in @var{hook} (comparing using
2614@code{equal}), then @code{add-hook} does not add it a second time.
de9f0bd9 2615
a44af9f2
RS
2616It is best to design your hook functions so that the order in which they
2617are executed does not matter. Any dependence on the order is ``asking
c22c5da6 2618for trouble''. However, the order is predictable: normally,
a44af9f2 2619@var{function} goes at the front of the hook list, so it will be
969fe9b5
RS
2620executed first (barring another @code{add-hook} call). If the optional
2621argument @var{append} is non-@code{nil}, the new hook function goes at
2622the end of the hook list and will be executed last.
22697dac 2623
c22c5da6
LK
2624If @var{local} is non-@code{nil}, that says to add @var{function} to
2625the buffer-local hook list instead of to the global hook list. If
2626needed, this makes the hook buffer-local and adds @code{t} to the
2627buffer-local value. The latter acts as a flag to run the hook
2628functions in the default value as well as in the local value.
a44af9f2
RS
2629@end defun
2630
22697dac 2631@defun remove-hook hook function &optional local
c22c5da6 2632This function removes @var{function} from the hook variable
da3178e2
RS
2633@var{hook}. It compares @var{function} with elements of @var{hook}
2634using @code{equal}, so it works for both symbols and lambda
2635expressions.
c44d2ced 2636
22697dac 2637If @var{local} is non-@code{nil}, that says to remove @var{function}
969fe9b5 2638from the buffer-local hook list instead of from the global hook list.
22697dac 2639@end defun
ab5796a9
MB
2640
2641@ignore
2642 arch-tag: 4c7bff41-36e6-4da6-9e7f-9b9289e27c8e
2643@end ignore