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