Fix typos.
[bpt/emacs.git] / doc / emacs / modes.texi
CommitLineData
8875da1e
CY
1@c This is part of the Emacs manual.
2@c Copyright (C) 1985-1987, 1993-1995, 1997, 2000-2011
3@c Free Software Foundation, Inc.
4@c See file emacs.texi for copying conditions.
5@node Modes, Indentation, International, Top
6@chapter Editing Modes
7
8 Emacs contains many @dfn{editing modes}, each of which alters its
9basic behavior in useful ways. These are divided into @dfn{major
10modes} and @dfn{minor modes}.
11
12 Major modes provide specialized facilities for working on a
13particular file type, such as a C source file (@pxref{Programs}), or a
14particular type of non-file buffer, such as a shell buffer
15(@pxref{Shell}). Major modes are mutually exclusive; each buffer has
16one and only one major mode at any time.
17
18 Minor modes are optional features which you can turn on or off, not
19necessarily specific to a type of file or buffer. For example, Auto
20Fill mode is a minor mode in which @key{SPC} breaks lines between
21words as you type (@pxref{Auto Fill}). Minor modes are independent of
22one another, and of the selected major mode.
23
24@menu
25* Major Modes:: Text mode vs. Lisp mode vs. C mode...
26* Minor Modes:: Each minor mode is a feature you can turn on
27 independently of any others.
28* Choosing Modes:: How modes are chosen when visiting files.
29@end menu
30
31@node Major Modes
32@section Major Modes
33@cindex major modes
34@cindex mode, major
35@kindex TAB @r{(and major modes)}
36@kindex DEL @r{(and major modes)}
37@kindex C-j @r{(and major modes)}
38
39 Every buffer possesses a major mode, which determines the editing
40behavior of Emacs while that buffer is current. The mode line
41normally shows the name of the current major mode, in parentheses.
42@xref{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
51 The least specialized major mode is called @dfn{Fundamental mode}.
52This mode has no mode-specific redefinitions or variable settings, so
53that each Emacs command behaves in its most general manner, and each
54user option variable is in its default state.
55
56 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
58mode, such as Lisp mode or Text mode. Such major modes change the
59meanings of some keys to become more specifically adapted to the
60language being edited. The ones that are commonly changed are
61@key{TAB}, @key{DEL}, and @kbd{C-j}. The prefix key @kbd{C-c}
62normally contains mode-specific commands. In addition, the commands
63which handle comments use the mode to determine how comments are to be
64delimited. Many major modes redefine the syntactical properties of
65characters appearing in the buffer.
66
67 The major modes fall into three major groups. The first group
68contains modes for normal text, either plain or with mark-up. It
69includes Text mode, HTML mode, SGML mode, @TeX{} mode and Outline
70mode. The second group contains modes for specific programming
71languages. These include Lisp mode (which has several variants), C
72mode, Fortran mode, and others. The remaining major modes are not
73intended for use on users' files; they are used in buffers created for
74specific purposes by Emacs, such as Dired mode for buffers made by
75Dired (@pxref{Dired}), Message mode for buffers made by @kbd{C-x m}
76(@pxref{Sending Mail}), and Shell mode for buffers used for
77communicating with an inferior shell process (@pxref{Interactive
78Shell}).
79
80 Most programming-language major modes specify that only blank lines
81separate paragraphs. This is to make the paragraph commands useful.
82(@xref{Paragraphs}.) They also cause Auto Fill mode to use the
83definition of @key{TAB} to indent the new lines it creates. This is
84because most lines in a program are usually indented
85(@pxref{Indentation}).
86
87@node Minor Modes
88@section Minor Modes
89@cindex minor modes
90@cindex mode, minor
91
92 A minor mode is an optional editing modes that alters the behavior
93of Emacs in some well-defined way. Unlike major modes, any number of
94minor 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
96enable the mode in certain buffers and not others. Other minor modes
97are @dfn{global}: while enabled, they affect everything you do in the
98Emacs session, in all buffers. Some global minor modes are enabled by
99default.
100
101 Most minor modes say in the mode line when they are enabled, just
102after the major mode indicator. For example, @samp{Fill} in the mode
103line means that Auto Fill mode is enabled. @xref{Mode Line}.
104
105 Each minor mode is associated with a command, called the @dfn{mode
106command}, which turns it on or off. The name of this command consists
107of the name of the minor mode, followed by @samp{-mode}; for instance,
108the mode command for Auto Fill mode is @code{auto-fill-mode}. Calling
109the minor mode command with no prefix argument @dfn{toggles} the mode,
110turning it on if it was off, and off if it was on. A positive
111argument always turns the mode on, and a zero or negative argument
112always turns it off. Mode commands are usually invoked with
113@kbd{M-x}, but you can bind keys to them if you wish (@pxref{Key
114Bindings}).
115
116 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
118enabled, and @code{nil} if it is disabled. In some minor modes---but
119not all---the value of the variable alone determines whether the mode
120is active: the mode command works simply by setting the variable, and
121changing the value of the variable has the same effect as calling the
122mode command. Because not all minor modes work this way, we recommend
123that you avoid changing the mode variables directly; use the mode
124commands instead.
125
126 The following is a list of some buffer-local minor modes:
127
128@itemize @bullet
129@item
130Abbrev mode automatically expands text based on pre-defined
131abbreviation definitions. @xref{Abbrevs}.
132
133@item
134Auto Fill mode inserts newlines as you type to prevent lines from
135becoming too long. @xref{Filling}.
136
137@item
138Auto Save mode saves the buffer contents periodically to reduce the
139amount of work you can lose in case of a crash. @xref{Auto Save}.
140
141@item
142Enriched mode enables editing and saving of formatted text.
143@xref{Formatted Text}.
144
145@item
146Flyspell mode automatically highlights misspelled words.
147@xref{Spelling}.
148
149@item
150Font-Lock mode automatically highlights certain textual units found in
151programs. It is enabled globally by default, but you can disable it
152in individual buffers. @xref{Faces}.
153
154@findex linum-mode
155@cindex Linum mode
156@item
157Linum mode displays each line's line number in the window's left
158margin. Its mode command is @code{linum-mode}.
159
160@item
161Outline minor mode provides similar facilities to the major mode
162called Outline mode. @xref{Outline Mode}.
163
164@cindex Overwrite mode
165@cindex mode, Overwrite
166@findex overwrite-mode
167@kindex INSERT
168@item
169Overwrite mode causes ordinary printing characters to replace existing
170text instead of shoving it to the right. For example, if point is in
171front of the @samp{B} in @samp{FOOBAR}, then in Overwrite mode typing
172a @kbd{G} changes it to @samp{FOOGAR}, instead of producing
173@samp{FOOGBAR} as usual. In Overwrite mode, the command @kbd{C-q}
174inserts the next character whatever it may be, even if it is a
175digit---this gives you a way to insert a character instead of
176replacing an existing character. The mode command,
177@code{overwrite-mode}, is bound to the @key{Insert} key.
178
179@findex binary-overwrite-mode
180@item
181Binary Overwrite mode is a variant of Overwrite mode for editing
182binary files; it treats newlines and tabs like other characters, so
183that they overwrite other characters and can be overwritten by them.
184In Binary Overwrite mode, digits after @kbd{C-q} specify an octal
185character code, as usual.
186
187@item
188Visual Line mode performs ``word wrapping'', causing long lines to be
189wrapped at word boundaries. @xref{Visual Line Mode}.
190@end itemize
191
192 Here are some useful global minor modes. Since Line Number mode and
193Transient Mark mode can be enabled or disabled just by setting the
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
198@itemize @bullet
199@item
200Column Number mode enables display of the current column number in the
201mode line. @xref{Mode Line}.
202
203@item
204Delete Selection mode causes text insertion to first delete the text
205in the region, if the region is active. @xref{Using Region}.
206
207@item
208Icomplete mode displays an indication of available completions when
209you are in the minibuffer and completion is active. @xref{Completion
210Options}.
211
212@item
213Line Number mode enables display of the current line number in the
214mode line. It is enabled by default. @xref{Mode Line}.
215
216@item
217Menu Bar mode gives each frame a menu bar. It is enabled by default.
218@xref{Menu Bars}.
219
220@item
221Scroll Bar mode gives each window a scroll bar. It is enabled by
222default, but the scroll bar is only displayed on graphical terminals.
223@xref{Scroll Bars}.
224
225@item
226Tool Bar mode gives each frame a tool bar. It is enabled by default,
227but the tool bar is only displayed on graphical terminals. @xref{Tool
228Bars}.
229
230@item
231Transient Mark mode highlights the region, and makes many Emacs
232commands operate on the region when the mark is active. It is enabled
233by default. @xref{Mark}.
234@end itemize
235
236@node Choosing Modes
237@section Choosing File Modes
238
239@cindex choosing a major mode
240@cindex choosing a minor mode
241@vindex auto-mode-alist
242 When you visit a file, Emacs chooses a major mode automatically.
243Normally, it makes the choice based on the file name---for example,
244files whose names end in @samp{.c} are normally edited in C mode---but
245sometimes it chooses the major mode based on special text in the file.
246This special text can also be used to enable buffer-local minor modes.
247
248 Here is the exact procedure:
249
250 First, Emacs checks whether the file contains file-local mode
251variables. @xref{File Variables}. If there is a file-local variable
252that specifies a major mode, then Emacs uses that major mode, ignoring
253all other criteria. There are several methods to specify a major mode
254using a file-local variable; the simplest is to put the mode name in
255the first nonblank line, preceded and followed by @samp{-*-}. Other
256text may appear on the line as well. For example,
257
258@example
259; -*-Lisp-*-
260@end example
261
262@noindent
263tells Emacs to use Lisp mode. Note how the semicolon is used to make
264Lisp treat this line as a comment. Alternatively, you could write
265
266@example
267; -*- mode: Lisp;-*-
268@end example
269
270@noindent
271The latter format allows you to specify local variables as well, like
272this:
273
274@example
275; -*- mode: Lisp; tab-width: 4; -*-
276@end example
277
278 If a file variable specifies a buffer-local minor mode, Emacs
279enables that minor mode in the buffer.
280
281@vindex interpreter-mode-alist
282 Second, if there is no file variable specifying a major mode, Emacs
283checks whether the file's contents begin with @samp{#!}. If so, that
284indicates that the file can serve as an executable shell command,
285which works by running an interpreter named on the file's first line
286(the rest of the file is used as input to the interpreter).
287Therefore, Emacs tries to use the interpreter name to choose a mode.
288For instance, a file that begins with @samp{#!/usr/bin/perl} is opened
289in Perl mode. The variable @code{interpreter-mode-alist} specifies
290the correspondence between interpreter program names and major modes.
291
292 When the first line starts with @samp{#!}, you usually cannot use
293the @samp{-*-} feature on the first line, because the system would get
294confused when running the interpreter. So Emacs looks for @samp{-*-}
295on the second line in such files as well as on the first line. The
296same is true for man pages which start with the magic string
297@samp{'\"} to specify a list of troff preprocessors.
298
299@vindex magic-mode-alist
300 Third, Emacs tries to determine the major mode by looking at the
301text at the start of the buffer, based on the variable
302@code{magic-mode-alist}. By default, this variable is @code{nil} (an
303empty list), so Emacs skips this step; however, you can customize it
304in your init file (@pxref{Init File}). The value should be a list of
305elements of the form
306
307@example
308(@var{regexp} . @var{mode-function})
309@end example
310
311@noindent
312where @var{regexp} is a regular expression (@pxref{Regexps}), and
313@var{mode-function} is a Lisp function that toggles a major mode. If
314the text at the beginning of the file matches @var{regexp}, Emacs
315chooses the major mode specified by @var{mode-function}.
316
317Alternatively, an element of @code{magic-mode-alist} may have the form
318
319@example
320(@var{match-function} . @var{mode-function})
321@end example
322
323@noindent
324where @var{match-function} is a Lisp function that is called at the
325beginning of the buffer; if the function returns non-@code{nil}, Emacs
326set the major mode wit @var{mode-function}.
327
328 Fourth---if Emacs still hasn't found a suitable major mode---it
329looks at the file's name. The correspondence between file names and
330major modes is controlled by the variable @code{auto-mode-alist}. Its
331value is a list in which each element has this form,
332
333@example
334(@var{regexp} . @var{mode-function})
335@end example
336
337@noindent
338or this form,
339
340@example
341(@var{regexp} @var{mode-function} @var{flag})
342@end example
343
344@noindent
345For example, one element normally found in the list has the form
346@code{(@t{"\\.c\\'"} . c-mode)}, and it is responsible for selecting C
347mode for files whose names end in @file{.c}. (Note that @samp{\\} is
348needed in Lisp syntax to include a @samp{\} in the string, which must
349be used to suppress the special meaning of @samp{.} in regexps.) If
350the element has the form @code{(@var{regexp} @var{mode-function}
351@var{flag})} and @var{flag} is non-@code{nil}, then after calling
352@var{mode-function}, Emacs discards the suffix that matched
353@var{regexp} and searches the list again for another match.
354
355@vindex auto-mode-case-fold
3fd50d5c
CY
356 On GNU/Linux and other systems with case-sensitive file names, Emacs
357performs a case-sensitive search through @code{auto-mode-alist}; if
358this search fails, it performs a second case-insensitive search
359through the alist. To suppress the second search, change the variable
360@code{auto-mode-case-fold} to @code{nil}. On systems with
361case-insensitive file names, such as Microsoft Windows, Emacs performs
362a single case-insensitive search through @code{auto-mode-alist}.
8875da1e
CY
363
364@vindex magic-fallback-mode-alist
365 Finally, if Emacs @emph{still} hasn't found a major mode to use, it
366compares the text at the start of the buffer to the variable
367@code{magic-fallback-mode-alist}. This variable works like
368@code{magic-mode-alist}, described above, except that is consulted
369only after @code{auto-mode-alist}. By default,
370@code{magic-fallback-mode-alist} contains forms that check for image
7877f373 371files, HTML/XML/SGML files, and PostScript files.
8875da1e
CY
372
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
397 If you have changed the major mode of a buffer, you can return to
398the major mode Emacs would have chosen automatically, by typing
399@kbd{M-x normal-mode}. This is the same function that
400@code{find-file} calls to choose the major mode. It also processes
401the file's @samp{-*-} line or local variables list (if any).
402@xref{File Variables}.
403
404@vindex change-major-mode-with-file-name
405 The commands @kbd{C-x C-w} and @code{set-visited-file-name} change to
406a new major mode if the new file name implies a mode (@pxref{Saving}).
407(@kbd{C-x C-s} does this too, if the buffer wasn't visiting a file.)
408However, this does not happen if the buffer contents specify a major
409mode, and certain ``special'' major modes do not allow the mode to
410change. You can turn off this mode-changing feature by setting
411@code{change-major-mode-with-file-name} to @code{nil}.