(rename): New function.
[bpt/emacs.git] / lispref / modes.texi
CommitLineData
a44af9f2
RS
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4@c See the file elisp.texi for copying conditions.
5@setfilename ../info/modes
6@node Modes, Documentation, Keymaps, Top
7@chapter Major and Minor Modes
8@cindex mode
9
10 A @dfn{mode} is a set of definitions that customize Emacs and can be
11turned on and off while you edit. There are two varieties of modes:
12@dfn{major modes}, which are mutually exclusive and used for editing
13particular kinds of text, and @dfn{minor modes}, which provide features
14that users can enable individually.
15
16 This chapter describes how to write both major and minor modes, how to
17indicate them in the mode line, and how they run hooks supplied by the
18user. For related topics such as keymaps and syntax tables, see
19@ref{Keymaps}, and @ref{Syntax Tables}.
20
21@menu
22* Major Modes:: Defining major modes.
23* Minor Modes:: Defining minor modes.
24* Mode Line Format:: Customizing the text that appears in the mode line.
25* Hooks:: How to use hooks; how to write code that provides hooks.
26@end menu
27
28@node Major Modes
29@section Major Modes
30@cindex major mode
31@cindex Fundamental mode
32
33 Major modes specialize Emacs for editing particular kinds of text.
34Each buffer has only one major mode at a time.
35
36 The least specialized major mode is called @dfn{Fundamental mode}.
37This mode has no mode-specific definitions or variable settings, so each
38Emacs command behaves in its default manner, and each option is in its
39default state. All other major modes redefine various keys and options.
40For example, Lisp Interaction mode provides special key bindings for
41@key{LFD} (@code{eval-print-last-sexp}), @key{TAB}
42(@code{lisp-indent-line}), and other keys.
43
44 When you need to write several editing commands to help you perform a
45specialized editing task, creating a new major mode is usually a good
46idea. In practice, writing a major mode is easy (in contrast to
47writing a minor mode, which is often difficult).
48
49 If the new mode is similar to an old one, it is often unwise to modify
50the old one to serve two purposes, since it may become harder to use and
51maintain. Instead, copy and rename an existing major mode definition
52and alter the copy---or define a @dfn{derived mode} (@pxref{Derived
53Modes}). For example, Rmail Edit mode, which is in
54@file{emacs/lisp/rmailedit.el}, is a major mode that is very similar to
55Text mode except that it provides three additional commands. Its
56definition is distinct from that of Text mode, but was derived from it.
57
58 Rmail Edit mode is an example of a case where one piece of text is put
59temporarily into a different major mode so it can be edited in a
60different way (with ordinary Emacs commands rather than Rmail). In such
61cases, the temporary major mode usually has a command to switch back to
62the buffer's usual mode (Rmail mode, in this case). You might be
63tempted to present the temporary redefinitions inside a recursive edit
64and restore the usual ones when the user exits; but this is a bad idea
65because it constrains the user's options when it is done in more than
66one buffer: recursive edits must be exited most-recently-entered first.
67Using alternative major modes avoids this limitation. @xref{Recursive
68Editing}.
69
70 The standard GNU Emacs Lisp library directory contains the code for
71several major modes, in files including @file{text-mode.el},
72@file{texinfo.el}, @file{lisp-mode.el}, @file{c-mode.el}, and
73@file{rmail.el}. You can look at these libraries to see how modes are
74written. Text mode is perhaps the simplest major mode aside from
75Fundamental mode. Rmail mode is a complicated and specialized mode.
76
77@menu
78* Major Mode Conventions:: Coding conventions for keymaps, etc.
79* Example Major Modes:: Text mode and Lisp modes.
80* Auto Major Mode:: How Emacs chooses the major mode automatically.
81* Mode Help:: Finding out how to use a mode.
82* Derived Modes:: Defining a new major mode based on another major
83 mode.
84@end menu
85
86@node Major Mode Conventions
87@subsection Major Mode Conventions
88
89 The code for existing major modes follows various coding conventions,
90including conventions for local keymap and syntax table initialization,
91global names, and hooks. Please follow these conventions when you
92define a new major mode:
93
94@itemize @bullet
95@item
96Define a command whose name ends in @samp{-mode}, with no arguments,
97that switches to the new mode in the current buffer. This command
98should set up the keymap, syntax table, and local variables in an
99existing buffer without changing the buffer's text.
100
101@item
de9f0bd9 102Write a documentation string for this command that describes the
a44af9f2
RS
103special commands available in this mode. @kbd{C-h m}
104(@code{describe-mode}) in your mode will display this string.
105
106The documentation string may include the special documentation
107substrings, @samp{\[@var{command}]}, @samp{\@{@var{keymap}@}}, and
108@samp{\<@var{keymap}>}, that enable the documentation to adapt
109automatically to the user's own key bindings. @xref{Keys in
110Documentation}.
111
112@item
113The major mode command should start by calling
114@code{kill-all-local-variables}. This is what gets rid of the local
115variables of the major mode previously in effect.
116
117@item
118The major mode command should set the variable @code{major-mode} to the
119major mode command symbol. This is how @code{describe-mode} discovers
120which documentation to print.
121
122@item
123The major mode command should set the variable @code{mode-name} to the
124``pretty'' name of the mode, as a string. This appears in the mode
125line.
126
127@item
128@cindex functions in modes
129Since all global names are in the same name space, all the global
130variables, constants, and functions that are part of the mode should
131have names that start with the major mode name (or with an abbreviation
132of it if the name is long). @xref{Style Tips}.
133
134@item
135@cindex keymaps in modes
136The major mode should usually have its own keymap, which is used as the
137local keymap in all buffers in that mode. The major mode function
138should call @code{use-local-map} to install this local map.
139@xref{Active Keymaps}, for more information.
140
141This keymap should be kept in a global variable named
142@code{@var{modename}-mode-map}. Normally the library that defines the
de9f0bd9 143mode sets this variable.
a44af9f2 144
23ce41fc
RS
145@xref{Tips for Defining}, for advice about how to write the code to set
146up the mode's keymap variable.
147
a44af9f2
RS
148@item
149@cindex syntax tables in modes
150The mode may have its own syntax table or may share one with other
151related modes. If it has its own syntax table, it should store this in
de9f0bd9 152a variable named @code{@var{modename}-mode-syntax-table}. @xref{Syntax
a44af9f2
RS
153Tables}.
154
155@item
156@cindex abbrev tables in modes
157The mode may have its own abbrev table or may share one with other
158related modes. If it has its own abbrev table, it should store this in
159a variable named @code{@var{modename}-mode-abbrev-table}. @xref{Abbrev
160Tables}.
161
de9f0bd9
RS
162@item
163Use @code{defvar} to set mode-related variables, so that they are not
164reinitialized if they already have a value. (Such reinitialization
165could discard customizations made by the user.)
166
a44af9f2
RS
167@item
168@cindex buffer-local variables in modes
169To make a buffer-local binding for an Emacs customization variable, use
170@code{make-local-variable} in the major mode command, not
171@code{make-variable-buffer-local}. The latter function would make the
172variable local to every buffer in which it is subsequently set, which
173would affect buffers that do not use this mode. It is undesirable for a
174mode to have such global effects. @xref{Buffer-Local Variables}.
175
176It's ok to use @code{make-variable-buffer-local}, if you wish, for a
177variable used only within a single Lisp package.
178
179@item
180@cindex mode hook
181@cindex major mode hook
182Each major mode should have a @dfn{mode hook} named
183@code{@var{modename}-mode-hook}. The major mode command should run that
184hook, with @code{run-hooks}, as the very last thing it
185does. @xref{Hooks}.
186
187@item
188The major mode command may also run the hooks of some more basic modes.
189For example, @code{indented-text-mode} runs @code{text-mode-hook} as
190well as @code{indented-text-mode-hook}. It may run these other hooks
191immediately before the mode's own hook (that is, after everything else),
192or it may run them earlier.
193
194@item
195If something special should be done if the user switches a buffer from
196this mode to any other major mode, the mode can set a local value for
197@code{change-major-mode-hook}.
198
199@item
200If this mode is appropriate only for specially-prepared text, then the
201major mode command symbol should have a property named @code{mode-class}
202with value @code{special}, put on as follows:
203
204@cindex @code{mode-class} property
205@cindex @code{special}
206@example
207(put 'funny-mode 'mode-class 'special)
208@end example
209
210@noindent
211This tells Emacs that new buffers created while the current buffer has
212Funny mode should not inherit Funny mode. Modes such as Dired, Rmail,
213and Buffer List use this feature.
214
215@item
216If you want to make the new mode the default for files with certain
217recognizable names, add an element to @code{auto-mode-alist} to select
218the mode for those file names. If you define the mode command to
219autoload, you should add this element in the same file that calls
220@code{autoload}. Otherwise, it is sufficient to add the element in the
221file that contains the mode definition. @xref{Auto Major Mode}.
222
223@item
224@cindex @file{.emacs} customization
225In the documentation, you should provide a sample @code{autoload} form
226and an example of how to add to @code{auto-mode-alist}, that users can
227include in their @file{.emacs} files.
228
229@item
230@cindex mode loading
de9f0bd9 231The top-level forms in the file defining the mode should be written so
a44af9f2
RS
232that they may be evaluated more than once without adverse consequences.
233Even if you never load the file more than once, someone else will.
234@end itemize
235
236@defvar change-major-mode-hook
237This normal hook is run by @code{kill-all-local-variables} before it
238does anything else. This gives major modes a way to arrange for
239something special to be done if the user switches to a different major
240mode. For best results, make this variable buffer-local, so that it
241will disappear after doing its job and will not interfere with the
de9f0bd9 242subsequent major mode. @xref{Hooks}.
a44af9f2
RS
243@end defvar
244
245@node Example Major Modes
246@subsection Major Mode Examples
247
248 Text mode is perhaps the simplest mode besides Fundamental mode.
249Here are excerpts from @file{text-mode.el} that illustrate many of
250the conventions listed above:
251
252@smallexample
253@group
254;; @r{Create mode-specific tables.}
255(defvar text-mode-syntax-table nil
256 "Syntax table used while in text mode.")
257@end group
258
259@group
260(if text-mode-syntax-table
261 () ; @r{Do not change the table if it is already set up.}
262 (setq text-mode-syntax-table (make-syntax-table))
263 (modify-syntax-entry ?\" ". " text-mode-syntax-table)
264 (modify-syntax-entry ?\\ ". " text-mode-syntax-table)
265 (modify-syntax-entry ?' "w " text-mode-syntax-table))
266@end group
267
268@group
269(defvar text-mode-abbrev-table nil
270 "Abbrev table used while in text mode.")
271(define-abbrev-table 'text-mode-abbrev-table ())
272@end group
273
274@group
275(defvar text-mode-map nil) ; @r{Create a mode-specific keymap.}
276
277(if text-mode-map
278 () ; @r{Do not change the keymap if it is already set up.}
279 (setq text-mode-map (make-sparse-keymap))
280 (define-key text-mode-map "\t" 'tab-to-tab-stop)
281 (define-key text-mode-map "\es" 'center-line)
282 (define-key text-mode-map "\eS" 'center-paragraph))
283@end group
284@end smallexample
285
286 Here is the complete major mode function definition for Text mode:
287
288@smallexample
289@group
290(defun text-mode ()
291 "Major mode for editing text intended for humans to read.
292 Special commands: \\@{text-mode-map@}
293@end group
294@group
295Turning on text-mode runs the hook `text-mode-hook'."
296 (interactive)
297 (kill-all-local-variables)
298@end group
299@group
300 (use-local-map text-mode-map) ; @r{This provides the local keymap.}
301 (setq mode-name "Text") ; @r{This name goes into the mode line.}
302 (setq major-mode 'text-mode) ; @r{This is how @code{describe-mode}}
303 ; @r{finds the doc string to print.}
304 (setq local-abbrev-table text-mode-abbrev-table)
305 (set-syntax-table text-mode-syntax-table)
306 (run-hooks 'text-mode-hook)) ; @r{Finally, this permits the user to}
307 ; @r{customize the mode with a hook.}
308@end group
309@end smallexample
310
311@cindex @file{lisp-mode.el}
312 The three Lisp modes (Lisp mode, Emacs Lisp mode, and Lisp
313Interaction mode) have more features than Text mode and the code is
314correspondingly more complicated. Here are excerpts from
315@file{lisp-mode.el} that illustrate how these modes are written.
316
317@cindex syntax table example
318@smallexample
319@group
320;; @r{Create mode-specific table variables.}
321(defvar lisp-mode-syntax-table nil "")
322(defvar emacs-lisp-mode-syntax-table nil "")
323(defvar lisp-mode-abbrev-table nil "")
324@end group
325
326@group
327(if (not emacs-lisp-mode-syntax-table) ; @r{Do not change the table}
328 ; @r{if it is already set.}
329 (let ((i 0))
330 (setq emacs-lisp-mode-syntax-table (make-syntax-table))
331@end group
332
333@group
334 ;; @r{Set syntax of chars up to 0 to class of chars that are}
335 ;; @r{part of symbol names but not words.}
336 ;; @r{(The number 0 is @code{48} in the @sc{ASCII} character set.)}
337 (while (< i ?0)
338 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
339 (setq i (1+ i)))
340 @dots{}
341@end group
342@group
343 ;; @r{Set the syntax for other characters.}
344 (modify-syntax-entry ? " " emacs-lisp-mode-syntax-table)
345 (modify-syntax-entry ?\t " " emacs-lisp-mode-syntax-table)
346 @dots{}
347@end group
348@group
349 (modify-syntax-entry ?\( "() " emacs-lisp-mode-syntax-table)
350 (modify-syntax-entry ?\) ")( " emacs-lisp-mode-syntax-table)
351 @dots{}))
352;; @r{Create an abbrev table for lisp-mode.}
353(define-abbrev-table 'lisp-mode-abbrev-table ())
354@end group
355@end smallexample
356
357 Much code is shared among the three Lisp modes. The following
358function sets various variables; it is called by each of the major Lisp
359mode functions:
360
361@smallexample
362@group
363(defun lisp-mode-variables (lisp-syntax)
364 ;; @r{The @code{lisp-syntax} argument is @code{nil} in Emacs Lisp mode,}
365 ;; @r{and @code{t} in the other two Lisp modes.}
366 (cond (lisp-syntax
367 (if (not lisp-mode-syntax-table)
368 ;; @r{The Emacs Lisp mode syntax table always exists, but}
369 ;; @r{the Lisp Mode syntax table is created the first time a}
370 ;; @r{mode that needs it is called. This is to save space.}
371@end group
372@group
373 (progn (setq lisp-mode-syntax-table
374 (copy-syntax-table emacs-lisp-mode-syntax-table))
375 ;; @r{Change some entries for Lisp mode.}
376 (modify-syntax-entry ?\| "\" "
377 lisp-mode-syntax-table)
378 (modify-syntax-entry ?\[ "_ "
379 lisp-mode-syntax-table)
380 (modify-syntax-entry ?\] "_ "
381 lisp-mode-syntax-table)))
382@end group
383@group
384 (set-syntax-table lisp-mode-syntax-table)))
385 (setq local-abbrev-table lisp-mode-abbrev-table)
386 @dots{})
387@end group
388@end smallexample
389
390 Functions such as @code{forward-paragraph} use the value of the
391@code{paragraph-start} variable. Since Lisp code is different from
392ordinary text, the @code{paragraph-start} variable needs to be set
393specially to handle Lisp. Also, comments are indented in a special
394fashion in Lisp and the Lisp modes need their own mode-specific
395@code{comment-indent-function}. The code to set these variables is the
396rest of @code{lisp-mode-variables}.
397
398@smallexample
399@group
400 (make-local-variable 'paragraph-start)
bfe721d1
KH
401 ;; @r{Having @samp{^} is not clean, but @code{page-delimiter}}
402 ;; @r{has them too, and removing those is a pain.}
a44af9f2
RS
403 (setq paragraph-start (concat "^$\\|" page-delimiter))
404 @dots{}
405@end group
406@group
407 (make-local-variable 'comment-indent-function)
408 (setq comment-indent-function 'lisp-comment-indent))
409@end group
410@end smallexample
411
412 Each of the different Lisp modes has a slightly different keymap. For
413example, Lisp mode binds @kbd{C-c C-l} to @code{run-lisp}, but the other
414Lisp modes do not. However, all Lisp modes have some commands in
415common. The following function adds these common commands to a given
416keymap.
417
418@smallexample
419@group
420(defun lisp-mode-commands (map)
421 (define-key map "\e\C-q" 'indent-sexp)
422 (define-key map "\177" 'backward-delete-char-untabify)
423 (define-key map "\t" 'lisp-indent-line))
424@end group
425@end smallexample
426
427 Here is an example of using @code{lisp-mode-commands} to initialize a
428keymap, as part of the code for Emacs Lisp mode. First we declare a
429variable with @code{defvar} to hold the mode-specific keymap. When this
430@code{defvar} executes, it sets the variable to @code{nil} if it was
431void. Then we set up the keymap if the variable is @code{nil}.
432
433 This code avoids changing the keymap or the variable if it is already
de9f0bd9 434set up. This lets the user customize the keymap.
a44af9f2
RS
435
436@smallexample
437@group
438(defvar emacs-lisp-mode-map () "")
439(if emacs-lisp-mode-map
440 ()
441 (setq emacs-lisp-mode-map (make-sparse-keymap))
442 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
443 (lisp-mode-commands emacs-lisp-mode-map))
444@end group
445@end smallexample
446
447 Finally, here is the complete major mode function definition for
448Emacs Lisp mode.
449
450@smallexample
451@group
452(defun emacs-lisp-mode ()
453 "Major mode for editing Lisp code to run in Emacs.
454Commands:
455Delete converts tabs to spaces as it moves back.
456Blank lines separate paragraphs. Semicolons start comments.
457\\@{emacs-lisp-mode-map@}
458@end group
459@group
460Entry to this mode runs the hook `emacs-lisp-mode-hook'."
461 (interactive)
462 (kill-all-local-variables)
463 (use-local-map emacs-lisp-mode-map) ; @r{This provides the local keymap.}
464 (set-syntax-table emacs-lisp-mode-syntax-table)
465@end group
466@group
467 (setq major-mode 'emacs-lisp-mode) ; @r{This is how @code{describe-mode}}
468 ; @r{finds out what to describe.}
469 (setq mode-name "Emacs-Lisp") ; @r{This goes into the mode line.}
de9f0bd9 470 (lisp-mode-variables nil) ; @r{This defines various variables.}
a44af9f2
RS
471 (run-hooks 'emacs-lisp-mode-hook)) ; @r{This permits the user to use a}
472 ; @r{hook to customize the mode.}
473@end group
474@end smallexample
475
476@node Auto Major Mode
477@subsection How Emacs Chooses a Major Mode
478
479 Based on information in the file name or in the file itself, Emacs
480automatically selects a major mode for the new buffer when a file is
481visited.
482
483@deffn Command fundamental-mode
484 Fundamental mode is a major mode that is not specialized for anything
485in particular. Other major modes are defined in effect by comparison
486with this one---their definitions say what to change, starting from
487Fundamental mode. The @code{fundamental-mode} function does @emph{not}
488run any hooks; you're not supposed to customize it. (If you want Emacs
489to behave differently in Fundamental mode, change the @emph{global}
490state of Emacs.)
491@end deffn
492
493@deffn Command normal-mode &optional find-file
bfe721d1 494This function establishes the proper major mode and local variable
a44af9f2
RS
495bindings for the current buffer. First it calls @code{set-auto-mode},
496then it runs @code{hack-local-variables} to parse, and bind or
497evaluate as appropriate, any local variables.
498
bfe721d1 499If the @var{find-file} argument to @code{normal-mode} is
a44af9f2
RS
500non-@code{nil}, @code{normal-mode} assumes that the @code{find-file}
501function is calling it. In this case, it may process a local variables
bfe721d1
KH
502list at the end of the file and in the @samp{-*-} line. The variable
503@code{enable-local-variables} controls whether to do so.
a44af9f2 504
bfe721d1 505If you run @code{normal-mode} interactively, the argument
a44af9f2
RS
506@var{find-file} is normally @code{nil}. In this case,
507@code{normal-mode} unconditionally processes any local variables list.
508@xref{File variables, , Local Variables in Files, emacs, The GNU Emacs
509Manual}, for the syntax of the local variables section of a file.
510
511@cindex file mode specification error
bfe721d1 512@code{normal-mode} uses @code{condition-case} around the call to the
a44af9f2
RS
513major mode function, so errors are caught and reported as a @samp{File
514mode specification error}, followed by the original error message.
515@end deffn
516
517@defopt enable-local-variables
518This variable controls processing of local variables lists in files
519being visited. A value of @code{t} means process the local variables
520lists unconditionally; @code{nil} means ignore them; anything else means
521ask the user what to do for each file. The default value is @code{t}.
522@end defopt
523
bfe721d1
KH
524@defvar ignored-local-variables
525This variable holds a list of variables that should not be
526set by a local variables list. Any value specified
527for one of these variables is ignored.
528@end defvar
529
530In addition to this list, any variable whose name has a non-@code{nil}
531@code{risky-local-variable} property is also ignored.
532
a44af9f2
RS
533@defopt enable-local-eval
534This variable controls processing of @samp{Eval:} in local variables
535lists in files being visited. A value of @code{t} means process them
536unconditionally; @code{nil} means ignore them; anything else means ask
537the user what to do for each file. The default value is @code{maybe}.
538@end defopt
539
540@defun set-auto-mode
541@cindex visited file mode
542 This function selects the major mode that is appropriate for the
543current buffer. It may base its decision on the value of the @w{@samp{-*-}}
76352dc1
RS
544line, on the visited file name (using @code{auto-mode-alist}), on the
545@w{@samp{#!}} line (using @code{interpreter-mode-alist}), or on the
de9f0bd9 546value of a local variable. However, this function does not look for
a44af9f2
RS
547the @samp{mode:} local variable near the end of a file; the
548@code{hack-local-variables} function does that. @xref{Choosing Modes, ,
549How Major Modes are Chosen, emacs, The GNU Emacs Manual}.
550@end defun
551
552@defopt default-major-mode
553 This variable holds the default major mode for new buffers. The
554standard value is @code{fundamental-mode}.
555
556 If the value of @code{default-major-mode} is @code{nil}, Emacs uses
557the (previously) current buffer's major mode for the major mode of a new
558buffer. However, if the major mode symbol has a @code{mode-class}
559property with value @code{special}, then it is not used for new buffers;
560Fundamental mode is used instead. The modes that have this property are
561those such as Dired and Rmail that are useful only with text that has
562been specially prepared.
563@end defopt
564
22697dac
KH
565@defun set-buffer-major-mode buffer
566This function sets the major mode of @var{buffer} to the value of
567@code{default-major-mode}. If that variable is @code{nil}, it uses
568the current buffer's major mode (if that is suitable).
569
570The low-level primitives for creating buffers do not use this function,
bfe721d1
KH
571but medium-level commands such as @code{switch-to-buffer} and
572@code{find-file-noselect} use it whenever they create buffers.
22697dac
KH
573@end defun
574
a44af9f2
RS
575@defvar initial-major-mode
576@cindex @samp{*scratch*}
577The value of this variable determines the major mode of the initial
578@samp{*scratch*} buffer. The value should be a symbol that is a major
579mode command name. The default value is @code{lisp-interaction-mode}.
580@end defvar
581
582@defvar auto-mode-alist
583This variable contains an association list of file name patterns
584(regular expressions; @pxref{Regular Expressions}) and corresponding
585major mode functions. Usually, the file name patterns test for
586suffixes, such as @samp{.el} and @samp{.c}, but this need not be the
587case. An ordinary element of the alist looks like @code{(@var{regexp} .
588@var{mode-function})}.
589
590For example,
591
592@smallexample
593@group
594(("^/tmp/fol/" . text-mode)
24675e99
RS
595 ("\\.texinfo\\'" . texinfo-mode)
596 ("\\.texi\\'" . texinfo-mode)
a44af9f2
RS
597@end group
598@group
24675e99
RS
599 ("\\.el\\'" . emacs-lisp-mode)
600 ("\\.c\\'" . c-mode)
601 ("\\.h\\'" . c-mode)
a44af9f2
RS
602 @dots{})
603@end group
604@end smallexample
605
606When you visit a file whose expanded file name (@pxref{File Name
607Expansion}) matches a @var{regexp}, @code{set-auto-mode} calls the
608corresponding @var{mode-function}. This feature enables Emacs to select
609the proper major mode for most files.
610
611If an element of @code{auto-mode-alist} has the form @code{(@var{regexp}
612@var{function} t)}, then after calling @var{function}, Emacs searches
613@code{auto-mode-alist} again for a match against the portion of the file
614name that did not match before.
615
616This match-again feature is useful for uncompression packages: an entry
617of the form @code{("\\.gz\\'" . @var{function})} can uncompress the file
618and then put the uncompressed file in the proper mode according to the
619name sans @samp{.gz}.
620
621Here is an example of how to prepend several pattern pairs to
622@code{auto-mode-alist}. (You might use this sort of expression in your
623@file{.emacs} file.)
624
625@smallexample
626@group
627(setq auto-mode-alist
628 (append
de9f0bd9 629 ;; @r{File name starts with a dot.}
24675e99 630 '(("/\\.[^/]*\\'" . fundamental-mode)
de9f0bd9 631 ;; @r{File name has no dot.}
24675e99 632 ("[^\\./]*\\'" . fundamental-mode)
de9f0bd9 633 ;; @r{File name ends in @samp{.C}.}
24675e99 634 ("\\.C\\'" . c++-mode))
a44af9f2
RS
635 auto-mode-alist))
636@end group
637@end smallexample
638@end defvar
639
640@defvar interpreter-mode-alist
641This variable specifes major modes to use for scripts that specify a
76352dc1 642command interpreter in an @samp{#!} line. Its value is a list of
a44af9f2
RS
643elements of the form @code{(@var{interpreter} . @var{mode})}; for
644example, @code{("perl" . perl-mode)} is one element present by default.
645The element says to use mode @var{mode} if the file specifies
646@var{interpreter}.
647
de9f0bd9
RS
648This variable is applicable only when the @code{auto-mode-alist} does
649not indicate which major mode to use.
a44af9f2
RS
650@end defvar
651
652@defun hack-local-variables &optional force
653 This function parses, and binds or evaluates as appropriate, any local
654variables for the current buffer.
655
656 The handling of @code{enable-local-variables} documented for
657@code{normal-mode} actually takes place here. The argument @var{force}
de9f0bd9
RS
658usually comes from the argument @var{find-file} given to
659@code{normal-mode}.
a44af9f2
RS
660@end defun
661
662@node Mode Help
663@subsection Getting Help about a Major Mode
664@cindex mode help
665@cindex help for major mode
666@cindex documentation for major mode
667
668 The @code{describe-mode} function is used to provide information
669about major modes. It is normally called with @kbd{C-h m}. The
670@code{describe-mode} function uses the value of @code{major-mode},
671which is why every major mode function needs to set the
672@code{major-mode} variable.
673
674@deffn Command describe-mode
675This function displays the documentation of the current major mode.
676
677The @code{describe-mode} function calls the @code{documentation}
678function using the value of @code{major-mode} as an argument. Thus, it
679displays the documentation string of the major mode function.
680(@xref{Accessing Documentation}.)
681@end deffn
682
683@defvar major-mode
684This variable holds the symbol for the current buffer's major mode.
de9f0bd9 685This symbol should have a function definition that is the command to
a44af9f2 686switch to that major mode. The @code{describe-mode} function uses the
de9f0bd9 687documentation string of the function as the documentation of the major
a44af9f2
RS
688mode.
689@end defvar
690
691@node Derived Modes
692@subsection Defining Derived Modes
693
694 It's often useful to define a new major mode in terms of an existing
695one. An easy way to do this is to use @code{define-derived-mode}.
696
de9f0bd9 697@defmac define-derived-mode variant parent name docstring body@dots{}
a44af9f2 698This construct defines @var{variant} as a major mode command, using
de9f0bd9 699@var{name} as the string form of the mode name.
a44af9f2 700
de9f0bd9
RS
701The new command @var{variant} is defined to call the function
702@var{parent}, then override certain aspects of that parent mode:
a44af9f2
RS
703
704@itemize @bullet
705@item
706The new mode has its own keymap, named @code{@var{variant}-map}.
707@code{define-derived-mode} initializes this map to inherit from
708@code{@var{parent}-map}, if it is not already set.
709
710@item
de9f0bd9 711The new mode has its own syntax table, kept in the variable
a44af9f2
RS
712@code{@var{variant}-syntax-table}.
713@code{define-derived-mode} initializes this variable by copying
714@code{@var{parent}-syntax-table}, if it is not already set.
715
716@item
de9f0bd9 717The new mode has its own abbrev table, kept in the variable
a44af9f2
RS
718@code{@var{variant}-abbrev-table}.
719@code{define-derived-mode} initializes this variable by copying
720@code{@var{parent}-abbrev-table}, if it is not already set.
721
722@item
723The new mode has its own mode hook, @code{@var{variant}-hook},
724which it runs in standard fashion as the very last thing that it does.
725(The new mode also runs the mode hook of @var{parent} as part
726of calling @var{parent}.)
727@end itemize
728
729In addition, you can specify how to override other aspects of
de9f0bd9 730@var{parent} with @var{body}. The command @var{variant}
a44af9f2
RS
731evaluates the forms in @var{body} after setting up all its usual
732overrides, just before running @code{@var{variant}-hook}.
733
734The argument @var{docstring} specifies the documentation string for the
735new mode. If you omit @var{docstring}, @code{define-derived-mode}
736generates a documentation string.
737
738Here is a hypothetical example:
739
740@example
741(define-derived-mode hypertext-mode
742 text-mode "Hypertext"
743 "Major mode for hypertext.
744\\@{hypertext-mode-map@}"
745 (setq case-fold-search nil))
746
747(define-key hypertext-mode-map
748 [down-mouse-3] 'do-hyper-link)
749@end example
750@end defmac
751
752@node Minor Modes
753@section Minor Modes
754@cindex minor mode
755
756 A @dfn{minor mode} provides features that users may enable or disable
757independently of the choice of major mode. Minor modes can be enabled
758individually or in combination. Minor modes would be better named
759``Generally available, optional feature modes'' except that such a name is
760unwieldy.
761
762 A minor mode is not usually a modification of single major mode. For
763example, Auto Fill mode may be used in any major mode that permits text
764insertion. To be general, a minor mode must be effectively independent
765of the things major modes do.
766
767 A minor mode is often much more difficult to implement than a major
768mode. One reason is that you should be able to activate and deactivate
de9f0bd9
RS
769minor modes in any order. A minor mode should be able to have its
770desired effect regardless of the major mode and regardless of the other
771minor modes in effect.
a44af9f2
RS
772
773 Often the biggest problem in implementing a minor mode is finding a
774way to insert the necessary hook into the rest of Emacs. Minor mode
bfe721d1 775keymaps make this easier than it used to be.
a44af9f2
RS
776
777@menu
778* Minor Mode Conventions:: Tips for writing a minor mode.
779* Keymaps and Minor Modes:: How a minor mode can have its own keymap.
780@end menu
781
782@node Minor Mode Conventions
783@subsection Conventions for Writing Minor Modes
784@cindex minor mode conventions
785@cindex conventions for writing minor modes
786
787 There are conventions for writing minor modes just as there are for
788major modes. Several of the major mode conventions apply to minor
789modes as well: those regarding the name of the mode initialization
790function, the names of global symbols, and the use of keymaps and
791other tables.
792
793 In addition, there are several conventions that are specific to
794minor modes.
795
796@itemize @bullet
797@item
798@cindex mode variable
799Make a variable whose name ends in @samp{-mode} to represent the minor
800mode. Its value should enable or disable the mode (@code{nil} to
801disable; anything else to enable.) We call this the @dfn{mode
802variable}.
803
804This variable is used in conjunction with the @code{minor-mode-alist} to
805display the minor mode name in the mode line. It can also enable
806or disable a minor mode keymap. Individual commands or hooks can also
807check the variable's value.
808
809If you want the minor mode to be enabled separately in each buffer,
810make the variable buffer-local.
811
812@item
813Define a command whose name is the same as the mode variable.
814Its job is to enable and disable the mode by setting the variable.
815
816The command should accept one optional argument. If the argument is
817@code{nil}, it should toggle the mode (turn it on if it is off, and off
818if it is on). Otherwise, it should turn the mode on if the argument is
819a positive integer, a symbol other than @code{nil} or @code{-}, or a
820list whose @sc{car} is such an integer or symbol; it should turn the
821mode off otherwise.
822
bfe721d1
KH
823Here is an example taken from the definition of @code{transient-mark-mode}.
824It shows the use of @code{transient-mark-mode} as a variable that enables or
de9f0bd9
RS
825disables the mode's behavior, and also shows the proper way to toggle,
826enable or disable the minor mode based on the raw prefix argument value.
a44af9f2
RS
827
828@smallexample
829@group
bfe721d1
KH
830(setq transient-mark-mode
831 (if (null arg) (not transient-mark-mode)
a44af9f2
RS
832 (> (prefix-numeric-value arg) 0)))
833@end group
834@end smallexample
835
836@item
837Add an element to @code{minor-mode-alist} for each minor mode
838(@pxref{Mode Line Variables}). This element should be a list of the
839following form:
840
841@smallexample
842(@var{mode-variable} @var{string})
843@end smallexample
844
de9f0bd9 845Here @var{mode-variable} is the variable that controls enabling of the
a44af9f2
RS
846minor mode, and @var{string} is a short string, starting with a space,
847to represent the mode in the mode line. These strings must be short so
848that there is room for several of them at once.
849
850When you add an element to @code{minor-mode-alist}, use @code{assq} to
851check for an existing element, to avoid duplication. For example:
852
853@smallexample
854@group
855(or (assq 'leif-mode minor-mode-alist)
856 (setq minor-mode-alist
857 (cons '(leif-mode " Leif") minor-mode-alist)))
858@end group
859@end smallexample
860@end itemize
861
862@node Keymaps and Minor Modes
863@subsection Keymaps and Minor Modes
864
bfe721d1
KH
865 Each minor mode can have its own keymap, which is active when the mode
866is enabled. To set up a keymap for a minor mode, add an element to the
867alist @code{minor-mode-map-alist}. @xref{Active Keymaps}.
a44af9f2
RS
868
869@cindex @code{self-insert-command}, minor modes
870One use of minor mode keymaps is to modify the behavior of certain
871self-inserting characters so that they do something else as well as
872self-insert. In general, this is the only way to do that, since the
873facilities for customizing @code{self-insert-command} are limited to
874special cases (designed for abbrevs and Auto Fill mode). (Do not try
875substituting your own definition of @code{self-insert-command} for the
876standard one. The editor command loop handles this function specially.)
877
a44af9f2
RS
878@node Mode Line Format
879@section Mode Line Format
880@cindex mode line
881
de9f0bd9 882 Each Emacs window (aside from minibuffer windows) includes a mode line,
a44af9f2 883which displays status information about the buffer displayed in the
de9f0bd9 884window. The mode line contains information about the buffer, such as its
a44af9f2 885name, associated file, depth of recursive editing, and the major and
de9f0bd9 886minor modes.
a44af9f2
RS
887
888 This section describes how the contents of the mode line are
889controlled. It is in the chapter on modes because much of the
890information displayed in the mode line relates to the enabled major and
891minor modes.
892
893 @code{mode-line-format} is a buffer-local variable that holds a
894template used to display the mode line of the current buffer. All
bfe721d1
KH
895windows for the same buffer use the same @code{mode-line-format} and
896their mode lines appear the same (except for scrolling percentages and
a44af9f2
RS
897line numbers).
898
899 The mode line of a window is normally updated whenever a different
900buffer is shown in the window, or when the buffer's modified-status
901changes from @code{nil} to @code{t} or vice-versa. If you modify any of
de9f0bd9
RS
902the variables referenced by @code{mode-line-format} (@pxref{Mode Line
903Variables}), you may want to force an update of the mode line so as to
904display the new information.
a44af9f2
RS
905
906@c Emacs 19 feature
907@defun force-mode-line-update
908Force redisplay of the current buffer's mode line.
909@end defun
910
911 The mode line is usually displayed in inverse video; see
912@code{mode-line-inverse-video} in @ref{Inverse Video}.
913
914@menu
915* Mode Line Data:: The data structure that controls the mode line.
916* Mode Line Variables:: Variables used in that data structure.
917* %-Constructs:: Putting information into a mode line.
918@end menu
919
920@node Mode Line Data
921@subsection The Data Structure of the Mode Line
922@cindex mode line construct
923
924 The mode line contents are controlled by a data structure of lists,
de9f0bd9 925strings, symbols, and numbers kept in the buffer-local variable
a44af9f2
RS
926@code{mode-line-format}. The data structure is called a @dfn{mode line
927construct}, and it is built in recursive fashion out of simpler mode line
22697dac 928constructs. The same data structure is used for constructing
bfe721d1 929frame titles (@pxref{Frame Titles}).
a44af9f2
RS
930
931@defvar mode-line-format
932The value of this variable is a mode line construct with overall
933responsibility for the mode line format. The value of this variable
934controls which other variables are used to form the mode line text, and
935where they appear.
936@end defvar
937
938 A mode line construct may be as simple as a fixed string of text, but
939it usually specifies how to use other variables to construct the text.
940Many of these variables are themselves defined to have mode line
941constructs as their values.
942
943 The default value of @code{mode-line-format} incorporates the values
944of variables such as @code{mode-name} and @code{minor-mode-alist}.
945Because of this, very few modes need to alter @code{mode-line-format}.
946For most purposes, it is sufficient to alter the variables referenced by
947@code{mode-line-format}.
948
de9f0bd9
RS
949 A mode line construct may be a list, a symbol, or a string. If the
950value is a list, each element may be a list, a symbol, or a string.
a44af9f2
RS
951
952@table @code
953@cindex percent symbol in mode line
954@item @var{string}
955A string as a mode line construct is displayed verbatim in the mode line
bfe721d1 956except for @dfn{@code{%}-constructs}. Decimal digits after the @samp{%}
a44af9f2
RS
957specify the field width for space filling on the right (i.e., the data
958is left justified). @xref{%-Constructs}.
959
960@item @var{symbol}
961A symbol as a mode line construct stands for its value. The value of
de9f0bd9
RS
962@var{symbol} is used as a mode line construct, in place of @var{symbol}.
963However, the symbols @code{t} and @code{nil} are ignored; so is any
964symbol whose value is void.
a44af9f2
RS
965
966There is one exception: if the value of @var{symbol} is a string, it is
de9f0bd9 967displayed verbatim: the @code{%}-constructs are not recognized.
a44af9f2
RS
968
969@item (@var{string} @var{rest}@dots{}) @r{or} (@var{list} @var{rest}@dots{})
de9f0bd9
RS
970A list whose first element is a string or list means to process all the
971elements recursively and concatenate the results. This is the most
972common form of mode line construct.
a44af9f2
RS
973
974@item (@var{symbol} @var{then} @var{else})
975A list whose first element is a symbol is a conditional. Its meaning
976depends on the value of @var{symbol}. If the value is non-@code{nil},
de9f0bd9
RS
977the second element, @var{then}, is processed recursively as a mode line
978element. But if the value of @var{symbol} is @code{nil}, the third
979element, @var{else}, is processed recursively. You may omit @var{else};
980then the mode line element displays nothing if the value of @var{symbol}
981is @code{nil}.
a44af9f2
RS
982
983@item (@var{width} @var{rest}@dots{})
984A list whose first element is an integer specifies truncation or
985padding of the results of @var{rest}. The remaining elements
986@var{rest} are processed recursively as mode line constructs and
987concatenated together. Then the result is space filled (if
988@var{width} is positive) or truncated (to @minus{}@var{width} columns,
989if @var{width} is negative) on the right.
990
991For example, the usual way to show what percentage of a buffer is above
de9f0bd9 992the top of the window is to use a list like this: @code{(-3 "%p")}.
a44af9f2
RS
993@end table
994
995 If you do alter @code{mode-line-format} itself, the new value should
de9f0bd9
RS
996use the same variables that appear in the default value (@pxref{Mode
997Line Variables}), rather than duplicating their contents or displaying
998the information in another fashion. This way, customizations made by
bfe721d1
KH
999the user or by Lisp programs (such as @code{display-time} and major
1000modes) via changes to those variables remain effective.
a44af9f2
RS
1001
1002@cindex Shell mode @code{mode-line-format}
1003 Here is an example of a @code{mode-line-format} that might be
de9f0bd9 1004useful for @code{shell-mode}, since it contains the hostname and default
a44af9f2
RS
1005directory.
1006
1007@example
1008@group
1009(setq mode-line-format
1010 (list ""
1011 'mode-line-modified
1012 "%b--"
1013@end group
1014 (getenv "HOST") ; @r{One element is not constant.}
1015 ":"
1016 'default-directory
1017 " "
1018 'global-mode-string
de9f0bd9
RS
1019 " %[("
1020 'mode-name
1021 'mode-line-process
a44af9f2
RS
1022 'minor-mode-alist
1023 "%n"
a44af9f2
RS
1024 ")%]----"
1025@group
bfe721d1 1026 '(line-number-mode "L%l--")
a44af9f2
RS
1027 '(-3 . "%p")
1028 "-%-"))
1029@end group
1030@end example
1031
1032@node Mode Line Variables
1033@subsection Variables Used in the Mode Line
1034
1035 This section describes variables incorporated by the
1036standard value of @code{mode-line-format} into the text of the mode
1037line. There is nothing inherently special about these variables; any
1038other variables could have the same effects on the mode line if
1039@code{mode-line-format} were changed to use them.
1040
1041@defvar mode-line-modified
de9f0bd9 1042This variable holds the value of the mode-line construct that displays
a44af9f2
RS
1043whether the current buffer is modified.
1044
bfe721d1
KH
1045The default value of @code{mode-line-modified} is @code{("--%1*%1+-")}.
1046This means that the mode line displays @samp{--**-} if the buffer is
1047modified, @samp{-----} if the buffer is not modified, @samp{--%%-} if
1048the buffer is read only, and @samp{--%*--} if the buffer is read only
1049and modified.
a44af9f2
RS
1050
1051Changing this variable does not force an update of the mode line.
1052@end defvar
1053
1054@defvar mode-line-buffer-identification
de9f0bd9 1055This variable identifies the buffer being displayed in the window. Its
bfe721d1
KH
1056default value is @code{("%F: %17b")}, which means that it usually
1057displays @samp{Emacs:} followed by seventeen characters of the buffer
1058name. (In a terminal frame, it displays the frame name instead of
1059@samp{Emacs}; this has the effect of showing the frame number.) You may
1060want to change this in modes such as Rmail that do not behave like a
de9f0bd9 1061``normal'' Emacs.
a44af9f2
RS
1062@end defvar
1063
1064@defvar global-mode-string
1065This variable holds a mode line spec that appears in the mode line by
1066default, just after the buffer name. The command @code{display-time}
1067sets @code{global-mode-string} to refer to the variable
1068@code{display-time-string}, which holds a string containing the time and
1069load information.
1070
1071The @samp{%M} construct substitutes the value of
1072@code{global-mode-string}, but this is obsolete, since the variable is
1073included directly in the mode line.
1074@end defvar
1075
1076@defvar mode-name
de9f0bd9 1077This buffer-local variable holds the ``pretty'' name of the current
a44af9f2
RS
1078buffer's major mode. Each major mode should set this variable so that the
1079mode name will appear in the mode line.
1080@end defvar
1081
1082@defvar minor-mode-alist
de9f0bd9 1083This variable holds an association list whose elements specify how the
a44af9f2
RS
1084mode line should indicate that a minor mode is active. Each element of
1085the @code{minor-mode-alist} should be a two-element list:
1086
1087@example
1088(@var{minor-mode-variable} @var{mode-line-string})
1089@end example
1090
1091More generally, @var{mode-line-string} can be any mode line spec. It
1092appears in the mode line when the value of @var{minor-mode-variable} is
1093non-@code{nil}, and not otherwise. These strings should begin with
1094spaces so that they don't run together. Conventionally, the
1095@var{minor-mode-variable} for a specific mode is set to a non-@code{nil}
1096value when that minor mode is activated.
1097
1098The default value of @code{minor-mode-alist} is:
1099
1100@example
1101@group
1102minor-mode-alist
bfe721d1
KH
1103@result{} ((vc-mode vc-mode)
1104 (abbrev-mode " Abbrev")
1105 (overwrite-mode overwrite-mode)
a44af9f2 1106 (auto-fill-function " Fill")
bfe721d1
KH
1107 (defining-kbd-macro " Def")
1108 (isearch-mode isearch-mode))
a44af9f2
RS
1109@end group
1110@end example
1111
bfe721d1 1112@code{minor-mode-alist} is not buffer-local. The variables mentioned
a44af9f2
RS
1113in the alist should be buffer-local if the minor mode can be enabled
1114separately in each buffer.
1115@end defvar
1116
1117@defvar mode-line-process
1118This buffer-local variable contains the mode line information on process
1119status in modes used for communicating with subprocesses. It is
1120displayed immediately following the major mode name, with no intervening
1121space. For example, its value in the @samp{*shell*} buffer is
1122@code{(":@: %s")}, which allows the shell to display its status along
1123with the major mode as: @samp{(Shell:@: run)}. Normally this variable
1124is @code{nil}.
1125@end defvar
1126
1127@defvar default-mode-line-format
de9f0bd9 1128This variable holds the default @code{mode-line-format} for buffers
a44af9f2
RS
1129that do not override it. This is the same as @code{(default-value
1130'mode-line-format)}.
1131
de9f0bd9 1132The default value of @code{default-mode-line-format} is:
a44af9f2
RS
1133
1134@example
1135@group
1136(""
1137 mode-line-modified
1138 mode-line-buffer-identification
1139 " "
1140 global-mode-string
1141 " %[("
1142 mode-name
1143@end group
1144@group
bfe721d1 1145 mode-line-process
a44af9f2
RS
1146 minor-mode-alist
1147 "%n"
a44af9f2 1148 ")%]----"
bfe721d1 1149 (line-number-mode "L%l--")
a44af9f2
RS
1150 (-3 . "%p")
1151 "-%-")
1152@end group
1153@end example
1154@end defvar
1155
de9f0bd9
RS
1156@defvar vc-mode
1157The variable @code{vc-mode}, local in each buffer, records whether the
1158buffer's visited file is maintained with version control, and, if so,
1159which kind. Its value is @code{nil} for no version control, or a string
1160that appears in the mode line.
1161@end defvar
1162
a44af9f2
RS
1163@node %-Constructs
1164@subsection @code{%}-Constructs in the Mode Line
1165
1166 The following table lists the recognized @code{%}-constructs and what
de9f0bd9
RS
1167they mean. In any construct except @samp{%%}, you can add a decimal
1168integer after the @samp{%} to specify how many characters to display.
a44af9f2
RS
1169
1170@table @code
1171@item %b
1172The current buffer name, obtained with the @code{buffer-name} function.
1173@xref{Buffer Names}.
1174
1175@item %f
1176The visited file name, obtained with the @code{buffer-file-name}
1177function. @xref{Buffer File Name}.
1178
22697dac
KH
1179@item %F
1180The name of the selected frame.
1181
1182@item %c
1183The current column number of point.
1184
1185@item %l
1186The current line number of point.
1187
a44af9f2
RS
1188@item %*
1189@samp{%} if the buffer is read only (see @code{buffer-read-only}); @*
1190@samp{*} if the buffer is modified (see @code{buffer-modified-p}); @*
1191@samp{-} otherwise. @xref{Buffer Modification}.
1192
1193@item %+
22697dac
KH
1194@samp{*} if the buffer is modified (see @code{buffer-modified-p}); @*
1195@samp{%} if the buffer is read only (see @code{buffer-read-only}); @*
1196@samp{-} otherwise. This differs from @samp{%*} only for a modified
1197read-only buffer. @xref{Buffer Modification}.
1198
1199@item %&
de9f0bd9 1200@samp{*} if the buffer is modified, and @samp{-} otherwise.
a44af9f2
RS
1201
1202@item %s
1203The status of the subprocess belonging to the current buffer, obtained with
1204@code{process-status}. @xref{Process Information}.
1205
22697dac
KH
1206@item %t
1207Whether the visited file is a text file or a binary file. (This is a
1208meaningful distinction only on certain operating systems.)
1209
a44af9f2 1210@item %p
22697dac 1211The percentage of the buffer text above the @strong{top} of window, or
a44af9f2
RS
1212@samp{Top}, @samp{Bottom} or @samp{All}.
1213
1214@item %P
1215The percentage of the buffer text that is above the @strong{bottom} of
1216the window (which includes the text visible in the window, as well as
1217the text above the top), plus @samp{Top} if the top of the buffer is
1218visible on screen; or @samp{Bottom} or @samp{All}.
1219
1220@item %n
1221@samp{Narrow} when narrowing is in effect; nothing otherwise (see
1222@code{narrow-to-region} in @ref{Narrowing}).
1223
1224@item %[
1225An indication of the depth of recursive editing levels (not counting
1226minibuffer levels): one @samp{[} for each editing level.
1227@xref{Recursive Editing}.
1228
1229@item %]
1230One @samp{]} for each recursive editing level (not counting minibuffer
1231levels).
1232
1233@item %%
1234The character @samp{%}---this is how to include a literal @samp{%} in a
1235string in which @code{%}-constructs are allowed.
1236
1237@item %-
1238Dashes sufficient to fill the remainder of the mode line.
1239@end table
1240
1241The following two @code{%}-constructs are still supported, but they are
1242obsolete, since you can get the same results with the variables
1243@code{mode-name} and @code{global-mode-string}.
1244
1245@table @code
1246@item %m
1247The value of @code{mode-name}.
1248
1249@item %M
1250The value of @code{global-mode-string}. Currently, only
1251@code{display-time} modifies the value of @code{global-mode-string}.
1252@end table
1253
1254@node Hooks
1255@section Hooks
1256@cindex hooks
1257
1258 A @dfn{hook} is a variable where you can store a function or functions
1259to be called on a particular occasion by an existing program. Emacs
1260provides hooks for the sake of customization. Most often, hooks are set
1261up in the @file{.emacs} file, but Lisp programs can set them also.
1262@xref{Standard Hooks}, for a list of standard hook variables.
1263
1264 Most of the hooks in Emacs are @dfn{normal hooks}. These variables
1265contain lists of functions to be called with no arguments. The reason
1266most hooks are normal hooks is so that you can use them in a uniform
1267way. You can always tell when a hook is a normal hook, because its
1268name ends in @samp{-hook}.
1269
1270 The recommended way to add a hook function to a normal hook is by
1271calling @code{add-hook} (see below). The hook functions may be any of
1272the valid kinds of functions that @code{funcall} accepts (@pxref{What Is
1273a Function}). Most normal hook variables are initially void;
1274@code{add-hook} knows how to deal with this.
1275
1276 As for abnormal hooks, those whose names end in @samp{-function} have
de9f0bd9
RS
1277a value that is a single function. Those whose names end in
1278@samp{-hooks} have a value that is a list of functions. Any hook that
a44af9f2
RS
1279is abnormal is abnormal because a normal hook won't do the job; either
1280the functions are called with arguments, or their values are meaningful.
1281The name shows you that the hook is abnormal and that you should look at
1282its documentation string to see how to use it properly.
1283
bfe721d1
KH
1284 Major mode functions are supposed to run a hook called the @dfn{mode
1285hook} as the last step of initialization. This makes it easy for a user
1286to customize the behavior of the mode, by overriding the local variable
1287assignments already made by the mode. But hooks are used in other
1288contexts too. For example, the hook @code{suspend-hook} runs just
1289before Emacs suspends itself (@pxref{Suspending Emacs}).
a44af9f2 1290
bfe721d1
KH
1291 Here's an expression that uses a mode hook to turn on Auto Fill mode
1292when in Lisp Interaction mode:
a44af9f2
RS
1293
1294@example
1295(add-hook 'lisp-interaction-mode-hook 'turn-on-auto-fill)
1296@end example
1297
1298 The next example shows how to use a hook to customize the way Emacs
1299formats C code. (People often have strong personal preferences for one
1300format or another.) Here the hook function is an anonymous lambda
1301expression.
1302
1303@cindex lambda expression in hook
1304@example
1305@group
1306(add-hook 'c-mode-hook
1307 (function (lambda ()
1308 (setq c-indent-level 4
1309 c-argdecl-indent 0
1310 c-label-offset -4
1311@end group
1312@group
1313 c-continued-statement-indent 0
1314 c-brace-offset 0
1315 comment-column 40))))
1316
1317(setq c++-mode-hook c-mode-hook)
1318@end group
a44af9f2
RS
1319@end example
1320
1321 At the appropriate time, Emacs uses the @code{run-hooks} function to
bfe721d1
KH
1322run particular hooks. This function calls the hook functions that have
1323been added with @code{add-hook}.
a44af9f2
RS
1324
1325@defun run-hooks &rest hookvar
1326This function takes one or more hook variable names as arguments, and
1327runs each hook in turn. Each @var{hookvar} argument should be a symbol
1328that is a hook variable. These arguments are processed in the order
1329specified.
1330
1331If a hook variable has a non-@code{nil} value, that value may be a
1332function or a list of functions. If the value is a function (either a
1333lambda expression or a symbol with a function definition), it is
1334called. If it is a list, the elements are called, in order.
1335The hook functions are called with no arguments.
1336
bfe721d1 1337For example, here's how @code{emacs-lisp-mode} runs its mode hook:
a44af9f2
RS
1338
1339@example
1340(run-hooks 'emacs-lisp-mode-hook)
1341@end example
1342@end defun
1343
22697dac 1344@defun add-hook hook function &optional append local
a44af9f2 1345This function is the handy way to add function @var{function} to hook
de9f0bd9
RS
1346variable @var{hook}. The argument @var{function} may be any valid Lisp
1347function with the proper number of arguments. For example,
a44af9f2
RS
1348
1349@example
1350(add-hook 'text-mode-hook 'my-text-hook-function)
1351@end example
1352
1353@noindent
1354adds @code{my-text-hook-function} to the hook called @code{text-mode-hook}.
1355
de9f0bd9
RS
1356You can use @code{add-hook} for abnormal hooks as well as for normal
1357hooks.
1358
a44af9f2
RS
1359It is best to design your hook functions so that the order in which they
1360are executed does not matter. Any dependence on the order is ``asking
1361for trouble.'' However, the order is predictable: normally,
1362@var{function} goes at the front of the hook list, so it will be
1363executed first (barring another @code{add-hook} call).
1364
1365If the optional argument @var{append} is non-@code{nil}, the new hook
1366function goes at the end of the hook list and will be executed last.
22697dac
KH
1367
1368If @var{local} is non-@code{nil}, that says to make the new hook
1369function local to the current buffer. Before you can do this, you must
1370make the hook itself buffer-local by calling @code{make-local-hook}
1371(@strong{not} @code{make-local-variable}). If the hook itself is not
1372buffer-local, then the value of @var{local} makes no difference---the
1373hook function is always global.
a44af9f2
RS
1374@end defun
1375
22697dac 1376@defun remove-hook hook function &optional local
a44af9f2 1377This function removes @var{function} from the hook variable @var{hook}.
c44d2ced 1378
22697dac
KH
1379If @var{local} is non-@code{nil}, that says to remove @var{function}
1380from the local hook list instead of from the global hook list. If the
1381hook itself is not buffer-local, then the value of @var{local} makes no
1382difference.
1383@end defun
c44d2ced 1384
22697dac
KH
1385@defun make-local-hook hook
1386This function makes the hook variable @code{hook} local to the current
1387buffer. When a hook variable is local, it can have local and global
1388hook functions, and @code{run-hooks} runs all of them.
c44d2ced 1389
fc0cb073
RS
1390This function works by making @code{t} an element of the buffer-local
1391value. That serves as a flag to use the hook functions in the default
1392value of the hook variable as well as those in the local value. Since
1393@code{run-hooks} understands this flag, @code{make-local-hook} works
1394with all normal hooks. It works for only some non-normal hooks---those
1395whose callers have been updated to understand this meaning of @code{t}.
1396
22697dac
KH
1397Do not use @code{make-local-variable} directly for hook variables; it is
1398not sufficient.
1399@end defun