aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChong Yidong2011-11-28 14:24:48 +0800
committerChong Yidong2011-11-28 14:24:48 +0800
commitdc95a8b0de5b4c823f838a66d6cdc74ea5be2ccb (patch)
treedd3d04bce069218964359b0fc718b79e36cd7057
parent1305621bc212d922c5ef8ee89d73ce7aae496b71 (diff)
downloademacs-dc95a8b0de5b4c823f838a66d6cdc74ea5be2ccb.tar.gz
emacs-dc95a8b0de5b4c823f838a66d6cdc74ea5be2ccb.zip
More updates to Modes chapter of Emacs manual.
* doc/emacs/modes.texi (Major Modes): Move major-mode variable doc here from Choosing Modes. Document describe-mode. Document prog-mode-hook and text-mode-hook. Add example of using hooks. (Minor Modes): Document behavior of mode command calls from Lisp. Note that setting the mode variable using Customize will DTRT. (Choosing Modes): Add example of setting a minor mode using a local variable.
-rw-r--r--admin/FOR-RELEASE5
-rw-r--r--doc/emacs/ChangeLog10
-rw-r--r--doc/emacs/modes.texi255
3 files changed, 160 insertions, 110 deletions
diff --git a/admin/FOR-RELEASE b/admin/FOR-RELEASE
index 01d488c97e3..9a6d7b68751 100644
--- a/admin/FOR-RELEASE
+++ b/admin/FOR-RELEASE
@@ -170,7 +170,7 @@ entering.texi cyd
170files.texi cyd 170files.texi cyd
171fixit.texi 171fixit.texi
172fortran-xtra.texi 172fortran-xtra.texi
173frames.texi 173frames.texi cyd
174glossary.texi 174glossary.texi
175help.texi cyd 175help.texi cyd
176indent.texi 176indent.texi
@@ -182,6 +182,7 @@ major.texi
182mark.texi cyd 182mark.texi cyd
183mini.texi 183mini.texi
184misc.texi 184misc.texi
185modes.texi cyd
185msdog.texi 186msdog.texi
186msdog-xtra.texi 187msdog-xtra.texi
187mule.texi 188mule.texi
@@ -197,7 +198,7 @@ text.texi
197trouble.texi 198trouble.texi
198vc-xtra.texi 199vc-xtra.texi
199vc1-xtra.texi 200vc1-xtra.texi
200windows.texi 201windows.texi cyd
201xresources.texi 202xresources.texi
202 203
203** Check the Lisp manual. 204** Check the Lisp manual.
diff --git a/doc/emacs/ChangeLog b/doc/emacs/ChangeLog
index 4f68215b7ff..23c022af600 100644
--- a/doc/emacs/ChangeLog
+++ b/doc/emacs/ChangeLog
@@ -1,3 +1,13 @@
12011-11-28 Chong Yidong <cyd@gnu.org>
2
3 * modes.texi (Major Modes): Move major-mode variable doc here from
4 Choosing Modes. Document describe-mode. Document prog-mode-hook
5 and text-mode-hook. Add example of using hooks.
6 (Minor Modes): Document behavior of mode command calls from Lisp.
7 Note that setting the mode variable using Customize will DTRT.
8 (Choosing Modes): Add example of setting a minor mode using a
9 local variable.
10
12011-11-27 Chong Yidong <cyd@gnu.org> 112011-11-27 Chong Yidong <cyd@gnu.org>
2 12
3 * frames.texi (Creating Frames): Move frame parameter example to 13 * frames.texi (Creating Frames): Move frame parameter example to
diff --git a/doc/emacs/modes.texi b/doc/emacs/modes.texi
index 5a786be62cf..60636ce040f 100644
--- a/doc/emacs/modes.texi
+++ b/doc/emacs/modes.texi
@@ -3,11 +3,11 @@
3@c Free Software Foundation, Inc. 3@c Free Software Foundation, Inc.
4@c See file emacs.texi for copying conditions. 4@c See file emacs.texi for copying conditions.
5@node Modes, Indentation, International, Top 5@node Modes, Indentation, International, Top
6@chapter Editing Modes 6@chapter Major and Minor Modes
7 7
8 Emacs contains many @dfn{editing modes}, each of which alters its 8 Emacs contains many @dfn{editing modes} that alter its basic
9basic behavior in useful ways. These are divided into @dfn{major 9behavior in useful ways. These are divided into @dfn{major modes} and
10modes} and @dfn{minor modes}. 10@dfn{minor modes}.
11 11
12 Major modes provide specialized facilities for working on a 12 Major modes provide specialized facilities for working on a
13particular file type, such as a C source file (@pxref{Programs}), or a 13particular file type, such as a C source file (@pxref{Programs}), or a
@@ -38,15 +38,8 @@ one another, and of the selected major mode.
38 38
39 Every buffer possesses a major mode, which determines the editing 39 Every buffer possesses a major mode, which determines the editing
40behavior of Emacs while that buffer is current. The mode line 40behavior of Emacs while that buffer is current. The mode line
41normally shows the name of the current major mode, in parentheses. 41normally shows the name of the current major mode, in parentheses
42@xref{Mode Line}. 42(@pxref{Mode Line}).
43
44 Usually, the major mode is automatically set by Emacs, when you
45first visit a file or create a buffer. @xref{Choosing Modes}. You
46can explicitly select a new major mode by using an @kbd{M-x} command.
47Take the name of the mode and add @code{-mode} to get the name of the
48command to select that mode. Thus, you can enter Lisp mode with
49@kbd{M-x lisp-mode}.
50 43
51 The least specialized major mode is called @dfn{Fundamental mode}. 44 The least specialized major mode is called @dfn{Fundamental mode}.
52This mode has no mode-specific redefinitions or variable settings, so 45This mode has no mode-specific redefinitions or variable settings, so
@@ -55,73 +48,140 @@ user option variable is in its default state.
55 48
56 For editing text of a specific type that Emacs knows about, such as 49 For editing text of a specific type that Emacs knows about, such as
57Lisp code or English text, you typically use a more specialized major 50Lisp code or English text, you typically use a more specialized major
58mode, such as Lisp mode or Text mode. Such major modes change the 51mode, such as Lisp mode or Text mode. Most major modes fall into
59meanings of some keys to become more specifically adapted to the 52three major groups. The first group contains modes for normal text,
60language being edited. The ones that are commonly changed are 53either plain or with mark-up. It includes Text mode, HTML mode, SGML
61@key{TAB}, @key{DEL}, and @kbd{C-j}. The prefix key @kbd{C-c} 54mode, @TeX{} mode and Outline mode. The second group contains modes
62normally contains mode-specific commands. In addition, the commands 55for specific programming languages. These include Lisp mode (which
63which handle comments use the mode to determine how comments are to be 56has several variants), C mode, Fortran mode, and others. The third
64delimited. Many major modes redefine the syntactical properties of 57group consists of major modes that are not associated directly with
65characters appearing in the buffer. 58files; they are used in buffers created for specific purposes by
66 59Emacs, such as Dired mode for buffers made by Dired (@pxref{Dired}),
67 The major modes fall into three major groups. The first group 60Message mode for buffers made by @kbd{C-x m} (@pxref{Sending Mail}),
68contains modes for normal text, either plain or with mark-up. It 61and Shell mode for buffers used to communicate with an inferior shell
69includes Text mode, HTML mode, SGML mode, @TeX{} mode and Outline 62process (@pxref{Interactive Shell}).
70mode. The second group contains modes for specific programming 63
71languages. These include Lisp mode (which has several variants), C 64 Usually, the major mode is automatically set by Emacs, when you
72mode, Fortran mode, and others. The remaining major modes are not 65first visit a file or create a buffer (@pxref{Choosing Modes}). You
73intended for use on users' files; they are used in buffers created for 66can explicitly select a new major mode by using an @kbd{M-x} command.
74specific purposes by Emacs, such as Dired mode for buffers made by 67Take the name of the mode and add @code{-mode} to get the name of the
75Dired (@pxref{Dired}), Message mode for buffers made by @kbd{C-x m} 68command to select that mode. Thus, you can enter Lisp mode with
76(@pxref{Sending Mail}), and Shell mode for buffers used for 69@kbd{M-x lisp-mode}.
77communicating with an inferior shell process (@pxref{Interactive 70
78Shell}). 71@vindex major-mode
79 72 The value of the buffer-local variable @code{major-mode} is a symbol
80 Most programming-language major modes specify that only blank lines 73with the same name as the major mode command (e.g. @code{lisp-mode}).
81separate paragraphs. This is to make the paragraph commands useful. 74This variable is set automatically; you should not change it yourself.
82(@xref{Paragraphs}.) They also cause Auto Fill mode to use the 75
83definition of @key{TAB} to indent the new lines it creates. This is 76 The default value of @code{major-mode} determines the major mode to
84because most lines in a program are usually indented 77use for files that do not specify a major mode, and for new buffers
85(@pxref{Indentation}). 78created with @kbd{C-x b}. Normally, this default value is the symbol
79@code{fundamental-mode}, which specifies Fundamental mode. You can
80change this default value via the Customization interface (@pxref{Easy
81Customization}), or by adding a line like this to your init file
82(@pxref{Init File}):
83
84@smallexample
85(setq-default major-mode 'text-mode)
86@end smallexample
87
88@noindent
89If the default value of @code{major-mode} is @code{nil}, the major
90mode is taken from the previously current buffer.
91
92 Specialized major modes often change the meanings of certain keys to
93do something more suitable for the mode. For instance, programming
94language modes bind @key{TAB} to indent the current line according to
95the rules of the language (@pxref{Indentation}). The keys that are
96commonly changed are @key{TAB}, @key{DEL}, and @kbd{C-j}. Many modes
97also define special commands of their own, usually bound in the prefix
98key @kbd{C-c}. Major modes can also alter user options and variables;
99for instance, programming language modes typicaly set a buffer-local
100value for the variable @code{comment-start}, which determines how
101source code comments are delimited (@pxref{Comments}).
102
103@findex describe-mode
104@kindex C-h m
105 To view the documentation for the current major mode, including a
106list of its key bindings, type @code{C-h m} (@code{describe-mode}).
107
108@cindex mode hook
109@vindex text-mode-hook
110@vindex prog-mode-hook
111 Every major mode, apart from Fundamental mode, defines a @dfn{mode
112hook}, a customizable list of Lisp functions to run each time the mode
113is enabled in a buffer. @xref{Hooks}, for more information about
114hooks. Each mode hook is named after its major mode, e.g. Fortran
115mode has @code{fortran-mode-hook}. Furthermore, all text-based major
116modes run @code{text-mode-hook}, and all programming language modes
117run @code{prog-mode-hook}, prior to running their own mode hooks.
118
119 Mode hooks are commonly used to enable minor modes (@pxref{Minor
120Modes}). For example, you can put the following lines in your init
121file to enable Flyspell minor mode in all text-based major modes
122(@pxref{Spelling}), and Eldoc minor mode in Emacs Lisp mode
123(@pxref{Lisp Doc}):
124
125@example
126(add-hook 'text-mode-hook 'flyspell-mode)
127(add-hook 'emacs-lisp-mode-hook 'eldoc-mode)
128@end example
86 129
87@node Minor Modes 130@node Minor Modes
88@section Minor Modes 131@section Minor Modes
89@cindex minor modes 132@cindex minor modes
90@cindex mode, minor 133@cindex mode, minor
91 134
92 A minor mode is an optional editing modes that alters the behavior 135 A minor mode is an optional editing mode that alters the behavior of
93of Emacs in some well-defined way. Unlike major modes, any number of 136Emacs in some well-defined way. Unlike major modes, any number of
94minor modes can be in effect at any time. Some minor modes are 137minor modes can be in effect at any time. Some minor modes are
95@dfn{buffer-local}: they apply only to the current buffer, so you can 138@dfn{buffer-local}, and can be turned on (enabled) in certain buffers
96enable the mode in certain buffers and not others. Other minor modes 139and off (disabled) in others. Other minor modes are @dfn{global}:
97are @dfn{global}: while enabled, they affect everything you do in the 140while enabled, they affect everything you do in the Emacs session, in
98Emacs session, in all buffers. Some global minor modes are enabled by 141all buffers. Most minor modes are disabled by default, but a few are
99default. 142enabled by default.
100 143
101 Most minor modes say in the mode line when they are enabled, just 144 Most buffer-local minor modes say in the mode line when they are
102after the major mode indicator. For example, @samp{Fill} in the mode 145enabled, just after the major mode indicator. For example,
103line means that Auto Fill mode is enabled. @xref{Mode Line}. 146@samp{Fill} in the mode line means that Auto Fill mode is enabled.
104 147@xref{Mode Line}.
105 Each minor mode is associated with a command, called the @dfn{mode 148
106command}, which turns it on or off. The name of this command consists 149@cindex mode commands for minor modes
107of the name of the minor mode, followed by @samp{-mode}; for instance, 150 Like major modes, each minor mode is associated with a @dfn{mode
108the mode command for Auto Fill mode is @code{auto-fill-mode}. Calling 151command}, whose name consists of the mode name followed by
109the minor mode command with no prefix argument @dfn{toggles} the mode, 152@samp{-mode}. For instance, the mode command for Auto Fill mode is
110turning it on if it was off, and off if it was on. A positive 153@code{auto-fill-mode}. But unlike a major mode command, which simply
111argument always turns the mode on, and a zero or negative argument 154enables the mode, the mode command for a minor mode can either enable
112always turns it off. Mode commands are usually invoked with 155or disable it:
113@kbd{M-x}, but you can bind keys to them if you wish (@pxref{Key 156
114Bindings}). 157@itemize
158@item
159If you invoke the mode command directly with no prefix argument
160(either via @kbd{M-x}, or by binding it to a key and typing that key;
161@pxref{Key Bindings}), that @dfn{toggles} the minor mode. The minor
162mode is turned on if it was off, and turned off if it was on.
163
164@item
165If you invoke the mode command with a prefix argument, the minor mode
166is unconditionally turned off if that argument is zero or negative;
167otherwise, it is unconditionally turned on.
168
169@item
170If the mode command is called via Lisp, the minor mode is
171unconditionally turned on if the argument is omitted or @code{nil}.
172This makes it easy to turn on a minor mode from a major mode's mode
173hook (@pxref{Major Modes}). A non-@code{nil} argument is handled like
174an interactive prefix argument, as described above.
175@end itemize
115 176
116 Most minor modes also have a @dfn{mode variable}, with the same name 177 Most minor modes also have a @dfn{mode variable}, with the same name
117as the mode command. Its value is non-@code{nil} if the mode is 178as the mode command. Its value is non-@code{nil} if the mode is
118enabled, and @code{nil} if it is disabled. In some minor modes---but 179enabled, and @code{nil} if it is disabled. In general, you should not
119not all---the value of the variable alone determines whether the mode 180try to enable or disable the mode by changing the value of the mode
120is active: the mode command works simply by setting the variable, and 181variable directly in Lisp; you should run the mode command instead.
121changing the value of the variable has the same effect as calling the 182However, setting the mode variable through the Customize interface
122mode command. Because not all minor modes work this way, we recommend 183(@pxref{Easy Customization}) will always properly enable or disable
123that you avoid changing the mode variables directly; use the mode 184the mode, since Customize automatically runs the mode command for you.
124commands instead.
125 185
126 The following is a list of some buffer-local minor modes: 186 The following is a list of some buffer-local minor modes:
127 187
@@ -189,11 +249,8 @@ Visual Line mode performs ``word wrapping'', causing long lines to be
189wrapped at word boundaries. @xref{Visual Line Mode}. 249wrapped at word boundaries. @xref{Visual Line Mode}.
190@end itemize 250@end itemize
191 251
192 Here are some useful global minor modes. Since Line Number mode and 252@noindent
193Transient Mark mode can be enabled or disabled just by setting the 253And here are some useful global minor modes:
194value of the minor mode variable, you @emph{can} set them differently
195for particular buffers, by explicitly making the corresponding
196variable local in those buffers. @xref{Locals}.
197 254
198@itemize @bullet 255@itemize @bullet
199@item 256@item
@@ -261,22 +318,27 @@ text may appear on the line as well. For example,
261 318
262@noindent 319@noindent
263tells Emacs to use Lisp mode. Note how the semicolon is used to make 320tells Emacs to use Lisp mode. Note how the semicolon is used to make
264Lisp treat this line as a comment. Alternatively, you could write 321Lisp treat this line as a comment. You could equivalently write
265 322
266@example 323@example
267; -*- mode: Lisp;-*- 324; -*- mode: Lisp;-*-
268@end example 325@end example
269 326
270@noindent 327@noindent
271The latter format allows you to specify local variables as well, like 328You can also use file-local variables to specify buffer-local minor
272this: 329modes, by using @code{eval} specifications. For example, this first
330nonblank line puts the buffer in Lisp mode and enables Auto-Fill mode:
273 331
274@example 332@example
275; -*- mode: Lisp; tab-width: 4; -*- 333; -*- mode: Lisp; eval: (auto-fill-mode 1); -*-
276@end example 334@end example
277 335
278 If a file variable specifies a buffer-local minor mode, Emacs 336@noindent
279enables that minor mode in the buffer. 337Note, however, that it is usually inappropriate to enable minor modes
338this way, since most minor modes represent individual user
339preferences. If you personally want to use a minor mode for a
340particular file type, it is better to enable the minor mode via a
341major mode hook (@pxref{Major Modes}).
280 342
281@vindex interpreter-mode-alist 343@vindex interpreter-mode-alist
282 Second, if there is no file variable specifying a major mode, Emacs 344 Second, if there is no file variable specifying a major mode, Emacs
@@ -310,9 +372,9 @@ elements of the form
310 372
311@noindent 373@noindent
312where @var{regexp} is a regular expression (@pxref{Regexps}), and 374where @var{regexp} is a regular expression (@pxref{Regexps}), and
313@var{mode-function} is a Lisp function that toggles a major mode. If 375@var{mode-function} is a major mode command. If the text at the
314the text at the beginning of the file matches @var{regexp}, Emacs 376beginning of the file matches @var{regexp}, Emacs chooses the major
315chooses the major mode specified by @var{mode-function}. 377mode specified by @var{mode-function}.
316 378
317Alternatively, an element of @code{magic-mode-alist} may have the form 379Alternatively, an element of @code{magic-mode-alist} may have the form
318 380
@@ -323,7 +385,7 @@ Alternatively, an element of @code{magic-mode-alist} may have the form
323@noindent 385@noindent
324where @var{match-function} is a Lisp function that is called at the 386where @var{match-function} is a Lisp function that is called at the
325beginning of the buffer; if the function returns non-@code{nil}, Emacs 387beginning of the buffer; if the function returns non-@code{nil}, Emacs
326set the major mode wit @var{mode-function}. 388set the major mode with @var{mode-function}.
327 389
328 Fourth---if Emacs still hasn't found a suitable major mode---it 390 Fourth---if Emacs still hasn't found a suitable major mode---it
329looks at the file's name. The correspondence between file names and 391looks at the file's name. The correspondence between file names and
@@ -370,29 +432,6 @@ only after @code{auto-mode-alist}. By default,
370@code{magic-fallback-mode-alist} contains forms that check for image 432@code{magic-fallback-mode-alist} contains forms that check for image
371files, HTML/XML/SGML files, and PostScript files. 433files, HTML/XML/SGML files, and PostScript files.
372 434
373@vindex major-mode
374 Once a major mode is chosen, Emacs sets the value of the variable
375@code{major-mode} to the symbol for that major mode (e.g.,
376@code{text-mode} for Text mode). This is a per-buffer variable
377(@pxref{Locals}); its buffer-local value is set automatically, and you
378should not change it yourself.
379
380 The default value of @code{major-mode} determines the major mode to
381use for files that do not specify a major mode, and for new buffers
382created with @kbd{C-x b}. Normally, this default value is the symbol
383@code{fundamental-mode}, which specifies Fundamental mode. You can
384change it via the Customization interface (@pxref{Easy
385Customization}), or by adding a line like this to your init file
386(@pxref{Init File}):
387
388@smallexample
389(setq-default major-mode 'text-mode)
390@end smallexample
391
392@noindent
393If the default value of @code{major-mode} is @code{nil}, the major
394mode is taken from the previously current buffer.
395
396@findex normal-mode 435@findex normal-mode
397 If you have changed the major mode of a buffer, you can return to 436 If you have changed the major mode of a buffer, you can return to
398the major mode Emacs would have chosen automatically, by typing 437the major mode Emacs would have chosen automatically, by typing