aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuc Teirlinck2005-05-14 16:16:44 +0000
committerLuc Teirlinck2005-05-14 16:16:44 +0000
commit302691abbceed17e0bfbcf38b3ce1c87917c6f61 (patch)
tree697592594c7ebc13b50e9005a313d44842828541
parent7177e2a315ac8610e397037c7eaddf78630c4279 (diff)
downloademacs-302691abbceed17e0bfbcf38b3ce1c87917c6f61.tar.gz
emacs-302691abbceed17e0bfbcf38b3ce1c87917c6f61.zip
(Modes): Update Menu.
(Hooks): Move to beginning of chapter. (Major Modes): Update Menu. (Major Mode Basics): New node, split off from `Major Modes'. (Major Mode Conventions): Correct xref. Explain how to handle auto-mode-alist if the major mode command has an autoload cookie. (Auto Major Mode): Major update. Add magic-mode-alist. (Derived Modes): Major update. (Mode Line Format): Update Menu. (Mode Line Basics): New node, split off from `Mode Line Format'.
-rw-r--r--lispref/ChangeLog19
-rw-r--r--lispref/modes.texi526
2 files changed, 316 insertions, 229 deletions
diff --git a/lispref/ChangeLog b/lispref/ChangeLog
index 94a67a6e037..6b0d147c586 100644
--- a/lispref/ChangeLog
+++ b/lispref/ChangeLog
@@ -1,3 +1,22 @@
12005-05-14 Luc Teirlinck <teirllm@auburn.edu>
2
3 * modes.texi (Modes): Update Menu.
4 (Hooks): Move to beginning of chapter.
5 Most minor modes run mode hooks too.
6 `add-hook' can handle void hooks or hooks whose value is a single
7 function.
8 (Major Modes): Update Menu.
9 (Major Mode Basics): New node, split off from `Major Modes'.
10 (Major Mode Conventions): Correct xref. Explain how to handle
11 auto-mode-alist if the major mode command has an autoload cookie.
12 (Auto Major Mode): Major update. Add magic-mode-alist.
13 (Derived Modes): Major update.
14 (Mode Line Format): Update Menu.
15 (Mode Line Basics): New node, split off from `Mode Line Format'.
16
17 * loading.texi (Autoload): Mention `autoload cookie' as synonym
18 for `magic autoload comment'. Add index entries and anchor.
19
12005-05-14 Richard M. Stallman <rms@gnu.org> 202005-05-14 Richard M. Stallman <rms@gnu.org>
2 21
3 * tips.texi (Coding Conventions): Explain how important it is 22 * tips.texi (Coding Conventions): Explain how important it is
diff --git a/lispref/modes.texi b/lispref/modes.texi
index 47d9ded1346..6766f357adf 100644
--- a/lispref/modes.texi
+++ b/lispref/modes.texi
@@ -20,6 +20,7 @@ user. For related topics such as keymaps and syntax tables, see
20@ref{Keymaps}, and @ref{Syntax Tables}. 20@ref{Keymaps}, and @ref{Syntax Tables}.
21 21
22@menu 22@menu
23* Hooks:: How to use hooks; how to write code that provides hooks.
23* Major Modes:: Defining major modes. 24* Major Modes:: Defining major modes.
24* Minor Modes:: Defining minor modes. 25* Minor Modes:: Defining minor modes.
25* Mode Line Format:: Customizing the text that appears in the mode line. 26* Mode Line Format:: Customizing the text that appears in the mode line.
@@ -28,13 +29,153 @@ user. For related topics such as keymaps and syntax tables, see
28* Font Lock Mode:: How modes can highlight text according to syntax. 29* Font Lock Mode:: How modes can highlight text according to syntax.
29* Desktop Save Mode:: How modes can have buffer state saved between 30* Desktop Save Mode:: How modes can have buffer state saved between
30 Emacs sessions. 31 Emacs sessions.
31* Hooks:: How to use hooks; how to write code that provides hooks.
32@end menu 32@end menu
33 33
34@node Hooks
35@section Hooks
36@cindex hooks
37
38 A @dfn{hook} is a variable where you can store a function or functions
39to be called on a particular occasion by an existing program. Emacs
40provides hooks for the sake of customization. Most often, hooks are set
41up in the init file (@pxref{Init File}), but Lisp programs can set them also.
42@xref{Standard Hooks}, for a list of standard hook variables.
43
44@cindex normal hook
45 Most of the hooks in Emacs are @dfn{normal hooks}. These variables
46contain lists of functions to be called with no arguments. When the
47hook name ends in @samp{-hook}, that tells you it is normal. We try to
48make all hooks normal, as much as possible, so that you can use them in
49a uniform way.
50
51 Every major mode function is supposed to run a normal hook called the
52@dfn{mode hook} as the last step of initialization. This makes it easy
53for a user to customize the behavior of the mode, by overriding the
54buffer-local variable assignments already made by the mode. Most
55minor modes also run a mode hook at their end. But hooks are used in
56other contexts too. For example, the hook @code{suspend-hook} runs
57just before Emacs suspends itself (@pxref{Suspending Emacs}).
58
59 The recommended way to add a hook function to a normal hook is by
60calling @code{add-hook} (see below). The hook functions may be any of
61the valid kinds of functions that @code{funcall} accepts (@pxref{What
62Is a Function}). Most normal hook variables are initially void;
63@code{add-hook} knows how to deal with this. You can add hooks either
64globally or buffer-locally with @code{add-hook}.
65
66@cindex abnormal hook
67 If the hook variable's name does not end with @samp{-hook}, that
68indicates it is probably an @dfn{abnormal hook}. Then you should look at its
69documentation to see how to use the hook properly.
70
71 If the variable's name ends in @samp{-functions} or @samp{-hooks},
72then the value is a list of functions, but it is abnormal in that either
73these functions are called with arguments or their values are used in
74some way. You can use @code{add-hook} to add a function to the list,
75but you must take care in writing the function. (A few of these
76variables, notably those ending in @samp{-hooks}, are actually
77normal hooks which were named before we established the convention of
78using @samp{-hook} for them.)
79
80 If the variable's name ends in @samp{-function}, then its value
81is just a single function, not a list of functions.
82
83 Here's an example that uses a mode hook to turn on Auto Fill mode when
84in Lisp Interaction mode:
85
86@example
87(add-hook 'lisp-interaction-mode-hook 'turn-on-auto-fill)
88@end example
89
90 At the appropriate time, Emacs uses the @code{run-hooks} function to
91run particular hooks. This function calls the hook functions that have
92been added with @code{add-hook}.
93
94@defun run-hooks &rest hookvars
95This function takes one or more normal hook variable names as
96arguments, and runs each hook in turn. Each argument should be a
97symbol that is a normal hook variable. These arguments are processed
98in the order specified.
99
100If a hook variable has a non-@code{nil} value, that value may be a
101function or a list of functions. (The former option is considered
102obsolete.) If the value is a function (either a lambda expression or
103a symbol with a function definition), it is called. If it is a list
104that isn't a function, its elements are called, consecutively. All
105the hook functions are called with no arguments.
106@end defun
107
108@defun run-hook-with-args hook &rest args
109This function is the way to run an abnormal hook and always call all
110of the hook functions. It calls each of the hook functions one by
111one, passing each of them the arguments @var{args}.
112@end defun
113
114@defun run-hook-with-args-until-failure hook &rest args
115This function is the way to run an abnormal hook until one of the hook
116functions fails. It calls each of the hook functions, passing each of
117them the arguments @var{args}, until some hook function returns
118@code{nil}. It then stops and returns @code{nil}. If none of the
119hook functions return @code{nil}, it returns a non-@code{nil} value.
120@end defun
121
122@defun run-hook-with-args-until-success hook &rest args
123This function is the way to run an abnormal hook until a hook function
124succeeds. It calls each of the hook functions, passing each of them
125the arguments @var{args}, until some hook function returns
126non-@code{nil}. Then it stops, and returns whatever was returned by
127the last hook function that was called. If all hook functions return
128@code{nil}, it returns @code{nil} as well.
129@end defun
130
131@defun add-hook hook function &optional append local
132This function is the handy way to add function @var{function} to hook
133variable @var{hook}. You can use it for abnormal hooks as well as for
134normal hooks. @var{function} can be any Lisp function that can accept
135the proper number of arguments for @var{hook}. For example,
136
137@example
138(add-hook 'text-mode-hook 'my-text-hook-function)
139@end example
140
141@noindent
142adds @code{my-text-hook-function} to the hook called @code{text-mode-hook}.
143
144If @var{function} is already present in @var{hook} (comparing using
145@code{equal}), then @code{add-hook} does not add it a second time.
146
147It is best to design your hook functions so that the order in which they
148are executed does not matter. Any dependence on the order is ``asking
149for trouble''. However, the order is predictable: normally,
150@var{function} goes at the front of the hook list, so it will be
151executed first (barring another @code{add-hook} call). If the optional
152argument @var{append} is non-@code{nil}, the new hook function goes at
153the end of the hook list and will be executed last.
154
155@code{add-hook} can handle the cases where @var{hook} is void or its
156value is a single function; it sets or changes the value to a list of
157functions.
158
159If @var{local} is non-@code{nil}, that says to add @var{function} to
160the buffer-local hook list instead of to the global hook list. If
161needed, this makes the hook buffer-local and adds @code{t} to the
162buffer-local value. The latter acts as a flag to run the hook
163functions in the default value as well as in the local value.
164@end defun
165
166@defun remove-hook hook function &optional local
167This function removes @var{function} from the hook variable
168@var{hook}. It compares @var{function} with elements of @var{hook}
169using @code{equal}, so it works for both symbols and lambda
170expressions.
171
172If @var{local} is non-@code{nil}, that says to remove @var{function}
173from the buffer-local hook list instead of from the global hook list.
174@end defun
175
34@node Major Modes 176@node Major Modes
35@section Major Modes 177@section Major Modes
36@cindex major mode 178@cindex major mode
37@cindex Fundamental mode
38 179
39 Major modes specialize Emacs for editing particular kinds of text. 180 Major modes specialize Emacs for editing particular kinds of text.
40Each buffer has only one major mode at a time. For each major mode 181Each buffer has only one major mode at a time. For each major mode
@@ -44,6 +185,23 @@ buffer-local variable bindings and other data associated with the
44buffer, such as a local keymap. The effect lasts until you switch 185buffer, such as a local keymap. The effect lasts until you switch
45to another major mode in the same buffer. 186to another major mode in the same buffer.
46 187
188@menu
189* Major Mode Basics::
190* Major Mode Conventions:: Coding conventions for keymaps, etc.
191* Example Major Modes:: Text mode and Lisp modes.
192* Auto Major Mode:: How Emacs chooses the major mode automatically.
193* Mode Help:: Finding out how to use a mode.
194* Derived Modes:: Defining a new major mode based on another major
195 mode.
196* Generic Modes:: Defining a simple major mode that supports
197 comment syntax and Font Lock mode.
198* Mode Hooks:: Hooks run at the end of major mode functions.
199@end menu
200
201@node Major Mode Basics
202@subsection Major Mode Basics
203@cindex Fundamental mode
204
47 The least specialized major mode is called @dfn{Fundamental mode}. 205 The least specialized major mode is called @dfn{Fundamental mode}.
48This mode has no mode-specific definitions or variable settings, so each 206This mode has no mode-specific definitions or variable settings, so each
49Emacs command behaves in its default manner, and each option is in its 207Emacs command behaves in its default manner, and each option is in its
@@ -95,18 +253,6 @@ for several major modes, in files such as @file{text-mode.el},
95are written. Text mode is perhaps the simplest major mode aside from 253are written. Text mode is perhaps the simplest major mode aside from
96Fundamental mode. Rmail mode is a complicated and specialized mode. 254Fundamental mode. Rmail mode is a complicated and specialized mode.
97 255
98@menu
99* Major Mode Conventions:: Coding conventions for keymaps, etc.
100* Example Major Modes:: Text mode and Lisp modes.
101* Auto Major Mode:: How Emacs chooses the major mode automatically.
102* Mode Help:: Finding out how to use a mode.
103* Derived Modes:: Defining a new major mode based on another major
104 mode.
105* Generic Modes:: Defining a simple major mode that supports
106 comment syntax and Font Lock mode.
107* Mode Hooks:: Hooks run at the end of major mode functions.
108@end menu
109
110@node Major Mode Conventions 256@node Major Mode Conventions
111@subsection Major Mode Conventions 257@subsection Major Mode Conventions
112 258
@@ -240,7 +386,7 @@ related modes. If it has its own abbrev table, it should store this
240in a variable named @code{@var{modename}-mode-abbrev-table}. If the 386in a variable named @code{@var{modename}-mode-abbrev-table}. If the
241major mode command defines any abbrevs itself, it should pass @code{t} 387major mode command defines any abbrevs itself, it should pass @code{t}
242for the @var{system-flag} argument to @code{define-abbrev}. 388for the @var{system-flag} argument to @code{define-abbrev}.
243@xref{Abbrev Tables}. 389@xref{Defining Abbrevs}.
244 390
245@item 391@item
246The mode should specify how to do highlighting for Font Lock mode, by 392The mode should specify how to do highlighting for Font Lock mode, by
@@ -326,8 +472,11 @@ If you want to make the new mode the default for files with certain
326recognizable names, add an element to @code{auto-mode-alist} to select 472recognizable names, add an element to @code{auto-mode-alist} to select
327the mode for those file names. If you define the mode command to 473the mode for those file names. If you define the mode command to
328autoload, you should add this element in the same file that calls 474autoload, you should add this element in the same file that calls
329@code{autoload}. Otherwise, it is sufficient to add the element in the 475@code{autoload}. If you use an autoload cookie for the mode command,
330file that contains the mode definition. @xref{Auto Major Mode}. 476you can also use an autoload cookie for the form that adds the element
477(@pxref{autoload cookie}). If you do not autoload the mode command,
478it is sufficient to add the element in the file that contains the mode
479definition. @xref{Auto Major Mode}.
331 480
332@item 481@item
333In the comments that document the file, you should provide a sample 482In the comments that document the file, you should provide a sample
@@ -635,21 +784,28 @@ state of Emacs.)
635 784
636@deffn Command normal-mode &optional find-file 785@deffn Command normal-mode &optional find-file
637This function establishes the proper major mode and buffer-local variable 786This function establishes the proper major mode and buffer-local variable
638bindings for the current buffer. First it calls @code{set-auto-mode}, 787bindings for the current buffer. First it calls @code{set-auto-mode}
639then it runs @code{hack-local-variables} to parse, and bind or 788(see below), then it runs @code{hack-local-variables} to parse, and
640evaluate as appropriate, the file's local variables. 789bind or evaluate as appropriate, the file's local variables
790(@pxref{File Local Variables}).
641 791
642If the @var{find-file} argument to @code{normal-mode} is non-@code{nil}, 792If the @var{find-file} argument to @code{normal-mode} is non-@code{nil},
643@code{normal-mode} assumes that the @code{find-file} function is calling 793@code{normal-mode} assumes that the @code{find-file} function is calling
644it. In this case, it may process a local variables list at the end of 794it. In this case, it may process local variables in the @samp{-*-}
645the file and in the @samp{-*-} line. The variable 795line or at the end of the file. The variable
646@code{enable-local-variables} controls whether to do so. @xref{File 796@code{enable-local-variables} controls whether to do so. @xref{File
647variables, , Local Variables in Files, emacs, The GNU Emacs Manual}, for 797Variables, , Local Variables in Files, emacs, The GNU Emacs Manual},
648the syntax of the local variables section of a file. 798for the syntax of the local variables section of a file.
649 799
650If you run @code{normal-mode} interactively, the argument 800If you run @code{normal-mode} interactively, the argument
651@var{find-file} is normally @code{nil}. In this case, 801@var{find-file} is normally @code{nil}. In this case,
652@code{normal-mode} unconditionally processes any local variables list. 802@code{normal-mode} unconditionally processes any file local variables.
803
804If @code{normal-mode} processes the local variables list and this list
805specifies a major mode, that mode overrides any mode chosen by
806@code{set-auto-mode}. If neither @code{set-auto-mode} nor
807@code{hack-local-variables} specify a major mode, the buffer stays in
808the major mode determined by @code{default-major-mode} (see below).
653 809
654@cindex file mode specification error 810@cindex file mode specification error
655@code{normal-mode} uses @code{condition-case} around the call to the 811@code{normal-mode} uses @code{condition-case} around the call to the
@@ -657,16 +813,25 @@ major mode function, so errors are caught and reported as a @samp{File
657mode specification error}, followed by the original error message. 813mode specification error}, followed by the original error message.
658@end deffn 814@end deffn
659 815
660@defun set-auto-mode 816@defun set-auto-mode &optional keep-mode-if-same
661@cindex visited file mode 817@cindex visited file mode
662 This function selects the major mode that is appropriate for the 818 This function selects the major mode that is appropriate for the
663current buffer. It may base its decision on the value of the @w{@samp{-*-}} 819current buffer. It bases its decision (in order of precedence) on
664line, on the visited file name (using @code{auto-mode-alist}), on the 820the @w{@samp{-*-}} line, on the @w{@samp{#!}} line (using
665@w{@samp{#!}} line (using @code{interpreter-mode-alist}), or on the 821@code{interpreter-mode-alist}), on the text at the beginning of the
666file's local variables list. However, this function does not look for 822buffer (using @code{magic-mode-alist}), and finally on the visited
667the @samp{mode:} local variable near the end of a file; the 823file name (using @code{auto-mode-alist}). @xref{Choosing Modes, , How
668@code{hack-local-variables} function does that. @xref{Choosing Modes, , 824Major Modes are Chosen, emacs, The GNU Emacs Manual}. However, this
669How Major Modes are Chosen, emacs, The GNU Emacs Manual}. 825function does not look for the @samp{mode:} local variable near the
826end of a file; the @code{hack-local-variables} function does that.
827If @code{enable-local-variables} is @code{nil}, @code{set-auto-mode}
828does not check the @w{@samp{-*-}} line for a mode tag either.
829
830If @var{keep-mode-if-same} is non-@code{nil}, this function does not
831call the mode command if the buffer is already in the proper major
832mode. For instance, @code{set-visited-file-name} sets this to
833@code{t} to avoid killing buffer local variables that the user may
834have set.
670@end defun 835@end defun
671 836
672@defopt default-major-mode 837@defopt default-major-mode
@@ -674,8 +839,8 @@ This variable holds the default major mode for new buffers. The
674standard value is @code{fundamental-mode}. 839standard value is @code{fundamental-mode}.
675 840
676If the value of @code{default-major-mode} is @code{nil}, Emacs uses 841If the value of @code{default-major-mode} is @code{nil}, Emacs uses
677the (previously) current buffer's major mode for the major mode of a new 842the (previously) current buffer's major mode as the default major mode
678buffer. However, if that major mode symbol has a @code{mode-class} 843of a new buffer. However, if that major mode symbol has a @code{mode-class}
679property with value @code{special}, then it is not used for new buffers; 844property with value @code{special}, then it is not used for new buffers;
680Fundamental mode is used instead. The modes that have this property are 845Fundamental mode is used instead. The modes that have this property are
681those such as Dired and Rmail that are useful only with text that has 846those such as Dired and Rmail that are useful only with text that has
@@ -684,28 +849,50 @@ been specially prepared.
684 849
685@defun set-buffer-major-mode buffer 850@defun set-buffer-major-mode buffer
686This function sets the major mode of @var{buffer} to the value of 851This function sets the major mode of @var{buffer} to the value of
687@code{default-major-mode}. If that variable is @code{nil}, it uses 852@code{default-major-mode}; if that variable is @code{nil}, it uses the
688the current buffer's major mode (if that is suitable). 853current buffer's major mode (if that is suitable). As an exception,
854if @var{buffer}'s name is @samp{*scratch*}, it sets the mode to
855@code{initial-major-mode}.
689 856
690The low-level primitives for creating buffers do not use this function, 857The low-level primitives for creating buffers do not use this function,
691but medium-level commands such as @code{switch-to-buffer} and 858but medium-level commands such as @code{switch-to-buffer} and
692@code{find-file-noselect} use it whenever they create buffers. 859@code{find-file-noselect} use it whenever they create buffers.
693@end defun 860@end defun
694 861
695@defvar initial-major-mode 862@defopt initial-major-mode
696@cindex @samp{*scratch*} 863@cindex @samp{*scratch*}
697The value of this variable determines the major mode of the initial 864The value of this variable determines the major mode of the initial
698@samp{*scratch*} buffer. The value should be a symbol that is a major 865@samp{*scratch*} buffer. The value should be a symbol that is a major
699mode command. The default value is @code{lisp-interaction-mode}. 866mode command. The default value is @code{lisp-interaction-mode}.
867@end defopt
868
869@defvar interpreter-mode-alist
870This variable specifies major modes to use for scripts that specify a
871command interpreter in a @samp{#!} line. Its value is an alist with
872elements of the form @code{(@var{interpreter} . @var{mode})}; for
873example, @code{("perl" . perl-mode)} is one element present by
874default. The element says to use mode @var{mode} if the file
875specifies an interpreter which matches @var{interpreter}. The value
876of @var{interpreter} is actually a regular expression. @xref{Regular
877Expressions}.
878@end defvar
879
880@defvar magic-mode-alist
881This variable's value is an alist with elements of the form
882@code{(@var{regexp} . @var{function})}, where @var{regexp} is a
883regular expression and @var{function} is a function or @code{nil}.
884After visiting a file, @code{set-auto-mode} calls @var{function} if
885the text at the beginning of the buffer matches @var{regexp} and
886@var{function} is non-@code{nil}; if @var{function} is @code{nil},
887@code{auto-mode-alist} gets to decide the mode.
700@end defvar 888@end defvar
701 889
702@defvar auto-mode-alist 890@defvar auto-mode-alist
703This variable contains an association list of file name patterns 891This variable contains an association list of file name patterns
704(regular expressions; @pxref{Regular Expressions}) and corresponding 892(regular expressions) and corresponding major mode commands. Usually,
705major mode commands. Usually, the file name patterns test for suffixes, 893the file name patterns test for suffixes, such as @samp{.el} and
706such as @samp{.el} and @samp{.c}, but this need not be the case. An 894@samp{.c}, but this need not be the case. An ordinary element of the
707ordinary element of the alist looks like @code{(@var{regexp} . 895alist looks like @code{(@var{regexp} . @var{mode-function})}.
708@var{mode-function})}.
709 896
710For example, 897For example,
711 898
@@ -724,9 +911,11 @@ For example,
724@end smallexample 911@end smallexample
725 912
726When you visit a file whose expanded file name (@pxref{File Name 913When you visit a file whose expanded file name (@pxref{File Name
727Expansion}) matches a @var{regexp}, @code{set-auto-mode} calls the 914Expansion}), with version numbers and backup suffixes removed using
728corresponding @var{mode-function}. This feature enables Emacs to select 915@code{file-name-sans-versions} (@pxref{File Name Components}), matches
729the proper major mode for most files. 916a @var{regexp}, @code{set-auto-mode} calls the corresponding
917@var{mode-function}. This feature enables Emacs to select the proper
918major mode for most files.
730 919
731If an element of @code{auto-mode-alist} has the form @code{(@var{regexp} 920If an element of @code{auto-mode-alist} has the form @code{(@var{regexp}
732@var{function} t)}, then after calling @var{function}, Emacs searches 921@var{function} t)}, then after calling @var{function}, Emacs searches
@@ -755,19 +944,6 @@ init file.)
755@end smallexample 944@end smallexample
756@end defvar 945@end defvar
757 946
758@defvar interpreter-mode-alist
759This variable specifies major modes to use for scripts that specify a
760command interpreter in a @samp{#!} line. Its value is a list of
761elements of the form @code{(@var{interpreter} . @var{mode})}; for
762example, @code{("perl" . perl-mode)} is one element present by default.
763The element says to use mode @var{mode} if the file specifies
764an interpreter which matches @var{interpreter}. The value of
765@var{interpreter} is actually a regular expression.
766
767This variable is applicable only when the @code{auto-mode-alist} does
768not indicate which major mode to use.
769@end defvar
770
771@node Mode Help 947@node Mode Help
772@subsection Getting Help about a Major Mode 948@subsection Getting Help about a Major Mode
773@cindex mode help 949@cindex mode help
@@ -804,36 +980,38 @@ mode.
804 It's often useful to define a new major mode in terms of an existing 980 It's often useful to define a new major mode in terms of an existing
805one. An easy way to do this is to use @code{define-derived-mode}. 981one. An easy way to do this is to use @code{define-derived-mode}.
806 982
807@defmac define-derived-mode variant parent name docstring body@dots{} 983@defmac define-derived-mode variant parent name docstring keyword-args@dots{} body@dots{}
808This construct defines @var{variant} as a major mode command, using 984This construct defines @var{variant} as a major mode command, using
809@var{name} as the string form of the mode name. 985@var{name} as the string form of the mode name. @var{variant} and
986@var{parent} should be unquoted symbols.
810 987
811The new command @var{variant} is defined to call the function 988The new command @var{variant} is defined to call the function
812@var{parent}, then override certain aspects of that parent mode: 989@var{parent}, then override certain aspects of that parent mode:
813 990
814@itemize @bullet 991@itemize @bullet
815@item 992@item
816The new mode has its own keymap, named @code{@var{variant}-map}. 993The new mode has its own sparse keymap, named
817@code{define-derived-mode} initializes this map to inherit from 994@code{@var{variant}-map}. @code{define-derived-mode}
818@code{@var{parent}-map}, if it is not already set. 995makes the parent mode's keymap the parent of the new map, unless
996@code{@var{variant}-map} is already set and already has a parent.
819 997
820@item 998@item
821The new mode has its own syntax table, kept in the variable 999The new mode has its own syntax table, kept in the variable
822@code{@var{variant}-syntax-table}. 1000@code{@var{variant}-syntax-table}, unless you override this using the
823@code{define-derived-mode} initializes this variable by copying 1001@code{:syntax-table} keyword (see below). @code{define-derived-mode}
824@code{@var{parent}-syntax-table}, if it is not already set. 1002makes the parent mode's syntax-table the parent of
1003@code{@var{variant}-syntax-table}, unless the latter is already set
1004and already has a parent different from @code{standard-syntax-table}.
825 1005
826@item 1006@item
827The new mode has its own abbrev table, kept in the variable 1007The new mode has its own abbrev table, kept in the variable
828@code{@var{variant}-abbrev-table}. 1008@code{@var{variant}-abbrev-table}, unless you override this using the
829@code{define-derived-mode} initializes this variable by copying 1009@code{:abbrev-table} keyword (see below).
830@code{@var{parent}-abbrev-table}, if it is not already set.
831 1010
832@item 1011@item
833The new mode has its own mode hook, @code{@var{variant}-hook}, 1012The new mode has its own mode hook, @code{@var{variant}-hook}. It
834which it runs in standard fashion as the very last thing that it does. 1013runs this hook, after running the hooks of its ancestor modes, with
835(The new mode also runs the mode hook of @var{parent} as part 1014@code{run-mode-hooks}.
836of calling @var{parent}.)
837@end itemize 1015@end itemize
838 1016
839In addition, you can specify how to override other aspects of 1017In addition, you can specify how to override other aspects of
@@ -841,9 +1019,38 @@ In addition, you can specify how to override other aspects of
841evaluates the forms in @var{body} after setting up all its usual 1019evaluates the forms in @var{body} after setting up all its usual
842overrides, just before running @code{@var{variant}-hook}. 1020overrides, just before running @code{@var{variant}-hook}.
843 1021
844The argument @var{docstring} specifies the documentation string for the 1022You can also specify @code{nil} for @var{parent}. This gives the new
845new mode. If you omit @var{docstring}, @code{define-derived-mode} 1023mode no parent. Then @code{define-derived-mode} behaves as described
846generates a documentation string. 1024above, but, of course, omits all actions connected with @var{parent}.
1025
1026The argument @var{docstring} specifies the documentation string for
1027the new mode. @code{define-derived-mode} adds some general
1028information about the mode's hook, followed by the mode's keymap, at
1029the end of this docstring. If you omit @var{docstring},
1030@code{define-derived-mode} generates a documentation string.
1031
1032The @var{keyword-args} are pairs of keywords and values. The values
1033are evaluated. The following keywords are currently supported:
1034
1035@table @code
1036@item :group
1037If this is specified, it is the customization group for this mode.
1038
1039@item :syntax-table
1040You can use this to explicitly specify a syntax table for the new
1041mode. If you specify a @code{nil} value, the new mode uses the same
1042syntax table as @var{parent}, or @code{standard-syntax-table} if
1043@var{parent} is @code{nil}. (Note that this does @emph{not} follow
1044the convention used for non-keyword arguments that a @code{nil} value
1045is equivalent with not specifying the argument.)
1046
1047@item :abbrev-table
1048You can use this to explicitly specify an abbrev table for the new
1049mode. If you specify a @code{nil} value, the new mode uses the same
1050abbrev-table as @var{parent}, or @code{fundamental-mode-abbrev-table}
1051if @var{parent} is @code{nil}. (Again,a @code{nil} value is
1052@emph{not} equivalent to not specifying this keyword.)
1053@end table
847 1054
848Here is a hypothetical example: 1055Here is a hypothetical example:
849 1056
@@ -1295,6 +1502,19 @@ and header line. We include it in this chapter because much of the
1295information displayed in the mode line relates to the enabled major and 1502information displayed in the mode line relates to the enabled major and
1296minor modes. 1503minor modes.
1297 1504
1505@menu
1506* Mode Line Basics::
1507* Mode Line Data:: The data structure that controls the mode line.
1508* Mode Line Variables:: Variables used in that data structure.
1509* %-Constructs:: Putting information into a mode line.
1510* Properties in Mode:: Using text properties in the mode line.
1511* Header Lines:: Like a mode line, but at the top.
1512* Emulating Mode Line:: Formatting text as the mode line would.
1513@end menu
1514
1515@node Mode Line Basics
1516@subsection Mode Line Basics
1517
1298 @code{mode-line-format} is a buffer-local variable that holds a 1518 @code{mode-line-format} is a buffer-local variable that holds a
1299template used to display the mode line of the current buffer. All 1519template used to display the mode line of the current buffer. All
1300windows for the same buffer use the same @code{mode-line-format}, so 1520windows for the same buffer use the same @code{mode-line-format}, so
@@ -1336,15 +1556,6 @@ that is two lines tall cannot display both a mode line and a header
1336line at once; if the variables call for both, only the mode line 1556line at once; if the variables call for both, only the mode line
1337actually appears. 1557actually appears.
1338 1558
1339@menu
1340* Mode Line Data:: The data structure that controls the mode line.
1341* Mode Line Variables:: Variables used in that data structure.
1342* %-Constructs:: Putting information into a mode line.
1343* Properties in Mode:: Using text properties in the mode line.
1344* Header Lines:: Like a mode line, but at the top.
1345* Emulating Mode Line:: Formatting text as the mode line would.
1346@end menu
1347
1348@node Mode Line Data 1559@node Mode Line Data
1349@subsection The Data Structure of the Mode Line 1560@subsection The Data Structure of the Mode Line
1350@cindex mode-line construct 1561@cindex mode-line construct
@@ -2813,151 +3024,8 @@ argument list
2813and it should return the restored buffer. 3024and it should return the restored buffer.
2814Here @var{desktop-buffer-misc} is the value returned by the function 3025Here @var{desktop-buffer-misc} is the value returned by the function
2815optionally bound to @code{desktop-save-buffer}. 3026optionally bound to @code{desktop-save-buffer}.
2816
2817@end defvar 3027@end defvar
2818 3028
2819@node Hooks
2820@section Hooks
2821@cindex hooks
2822
2823 A @dfn{hook} is a variable where you can store a function or functions
2824to be called on a particular occasion by an existing program. Emacs
2825provides hooks for the sake of customization. Most often, hooks are set
2826up in the init file (@pxref{Init File}), but Lisp programs can set them also.
2827@xref{Standard Hooks}, for a list of standard hook variables.
2828
2829@cindex normal hook
2830 Most of the hooks in Emacs are @dfn{normal hooks}. These variables
2831contain lists of functions to be called with no arguments. When the
2832hook name ends in @samp{-hook}, that tells you it is normal. We try to
2833make all hooks normal, as much as possible, so that you can use them in
2834a uniform way.
2835
2836 Every major mode function is supposed to run a normal hook called the
2837@dfn{mode hook} as the last step of initialization. This makes it easy
2838for a user to customize the behavior of the mode, by overriding the
2839buffer-local variable assignments already made by the mode. Most
2840minor modes also run a mode hook at their end. But hooks are used in
2841other contexts too. For example, the hook @code{suspend-hook} runs
2842just before Emacs suspends itself (@pxref{Suspending Emacs}).
2843
2844 The recommended way to add a hook function to a normal hook is by
2845calling @code{add-hook} (see below). The hook functions may be any of
2846the valid kinds of functions that @code{funcall} accepts (@pxref{What
2847Is a Function}). Most normal hook variables are initially void;
2848@code{add-hook} knows how to deal with this. You can add hooks either
2849globally or buffer-locally with @code{add-hook}.
2850
2851@cindex abnormal hook
2852 If the hook variable's name does not end with @samp{-hook}, that
2853indicates it is probably an @dfn{abnormal hook}. Then you should look at its
2854documentation to see how to use the hook properly.
2855
2856 If the variable's name ends in @samp{-functions} or @samp{-hooks},
2857then the value is a list of functions, but it is abnormal in that either
2858these functions are called with arguments or their values are used in
2859some way. You can use @code{add-hook} to add a function to the list,
2860but you must take care in writing the function. (A few of these
2861variables, notably those ending in @samp{-hooks}, are actually
2862normal hooks which were named before we established the convention of
2863using @samp{-hook} for them.)
2864
2865 If the variable's name ends in @samp{-function}, then its value
2866is just a single function, not a list of functions.
2867
2868 Here's an example that uses a mode hook to turn on Auto Fill mode when
2869in Lisp Interaction mode:
2870
2871@example
2872(add-hook 'lisp-interaction-mode-hook 'turn-on-auto-fill)
2873@end example
2874
2875 At the appropriate time, Emacs uses the @code{run-hooks} function to
2876run particular hooks. This function calls the hook functions that have
2877been added with @code{add-hook}.
2878
2879@defun run-hooks &rest hookvars
2880This function takes one or more normal hook variable names as
2881arguments, and runs each hook in turn. Each argument should be a
2882symbol that is a normal hook variable. These arguments are processed
2883in the order specified.
2884
2885If a hook variable has a non-@code{nil} value, that value may be a
2886function or a list of functions. (The former option is considered
2887obsolete.) If the value is a function (either a lambda expression or
2888a symbol with a function definition), it is called. If it is a list
2889that isn't a function, its elements are called, consecutively. All
2890the hook functions are called with no arguments.
2891@end defun
2892
2893@defun run-hook-with-args hook &rest args
2894This function is the way to run an abnormal hook and always call all
2895of the hook functions. It calls each of the hook functions one by
2896one, passing each of them the arguments @var{args}.
2897@end defun
2898
2899@defun run-hook-with-args-until-failure hook &rest args
2900This function is the way to run an abnormal hook until one of the hook
2901functions fails. It calls each of the hook functions, passing each of
2902them the arguments @var{args}, until some hook function returns
2903@code{nil}. It then stops and returns @code{nil}. If none of the
2904hook functions return @code{nil}, it returns a non-@code{nil} value.
2905@end defun
2906
2907@defun run-hook-with-args-until-success hook &rest args
2908This function is the way to run an abnormal hook until a hook function
2909succeeds. It calls each of the hook functions, passing each of them
2910the arguments @var{args}, until some hook function returns
2911non-@code{nil}. Then it stops, and returns whatever was returned by
2912the last hook function that was called. If all hook functions return
2913@code{nil}, it returns @code{nil} as well.
2914@end defun
2915
2916@defun add-hook hook function &optional append local
2917This function is the handy way to add function @var{function} to hook
2918variable @var{hook}. You can use it for abnormal hooks as well as for
2919normal hooks. @var{function} can be any Lisp function that can accept
2920the proper number of arguments for @var{hook}. For example,
2921
2922@example
2923(add-hook 'text-mode-hook 'my-text-hook-function)
2924@end example
2925
2926@noindent
2927adds @code{my-text-hook-function} to the hook called @code{text-mode-hook}.
2928
2929If @var{function} is already present in @var{hook} (comparing using
2930@code{equal}), then @code{add-hook} does not add it a second time.
2931
2932It is best to design your hook functions so that the order in which they
2933are executed does not matter. Any dependence on the order is ``asking
2934for trouble''. However, the order is predictable: normally,
2935@var{function} goes at the front of the hook list, so it will be
2936executed first (barring another @code{add-hook} call). If the optional
2937argument @var{append} is non-@code{nil}, the new hook function goes at
2938the end of the hook list and will be executed last.
2939
2940@code{add-hook} can handle the cases where @var{hook} is void or its
2941value is a single function; it sets or changes the value to a list of
2942functions.
2943
2944If @var{local} is non-@code{nil}, that says to add @var{function} to
2945the buffer-local hook list instead of to the global hook list. If
2946needed, this makes the hook buffer-local and adds @code{t} to the
2947buffer-local value. The latter acts as a flag to run the hook
2948functions in the default value as well as in the local value.
2949@end defun
2950
2951@defun remove-hook hook function &optional local
2952This function removes @var{function} from the hook variable
2953@var{hook}. It compares @var{function} with elements of @var{hook}
2954using @code{equal}, so it works for both symbols and lambda
2955expressions.
2956
2957If @var{local} is non-@code{nil}, that says to remove @var{function}
2958from the buffer-local hook list instead of from the global hook list.
2959@end defun
2960
2961@ignore 3029@ignore
2962 arch-tag: 4c7bff41-36e6-4da6-9e7f-9b9289e27c8e 3030 arch-tag: 4c7bff41-36e6-4da6-9e7f-9b9289e27c8e
2963@end ignore 3031@end ignore