Merge from emacs--rel--22
[bpt/emacs.git] / doc / emacs / major.texi
CommitLineData
8cf51b2c
GM
1@c This is part of the Emacs manual.
2@c Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1997, 2000, 2001,
3f548a7c 3@c 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
8cf51b2c
GM
4@c See file emacs.texi for copying conditions.
5@node Major Modes, Indentation, International, Top
6@chapter Major Modes
7@cindex major modes
8@cindex mode, major
9@kindex TAB @r{(and major modes)}
10@kindex DEL @r{(and major modes)}
11@kindex C-j @r{(and major modes)}
12
13 Emacs provides many alternative @dfn{major modes}, each of which
14customizes Emacs for editing text of a particular sort. The major modes
15are mutually exclusive, and each buffer has one major mode at any time.
16The mode line normally shows the name of the current major mode, in
17parentheses (@pxref{Mode Line}).
18
19 The least specialized major mode is called @dfn{Fundamental mode}.
20This mode has no mode-specific redefinitions or variable settings, so
21that each Emacs command behaves in its most general manner, and each
22user option variable is in its default state. For editing text of a
23specific type that Emacs knows about, such as Lisp code or English
24text, you should switch to the appropriate major mode, such as Lisp
25mode or Text mode.
26
27 Selecting a major mode changes the meanings of a few keys to become
28more specifically adapted to the language being edited. The ones that
29are changed frequently are @key{TAB}, @key{DEL}, and @kbd{C-j}. The
30prefix key @kbd{C-c} normally contains mode-specific commands. In
31addition, the commands which handle comments use the mode to determine
32how comments are to be delimited. Many major modes redefine the
33syntactical properties of characters appearing in the buffer.
34@xref{Syntax}.
35
36 The major modes fall into three major groups. The first group
37contains modes for normal text, either plain or with mark-up. It
38includes Text mode, HTML mode, SGML mode, @TeX{} mode and Outline
39mode. The second group contains modes for specific programming
40languages. These include Lisp mode (which has several variants), C
41mode, Fortran mode, and others. The remaining major modes are not
42intended for use on users' files; they are used in buffers created for
43specific purposes by Emacs, such as Dired mode for buffers made by
44Dired (@pxref{Dired}), Mail mode for buffers made by @kbd{C-x m}
45(@pxref{Sending Mail}), and Shell mode for buffers used for
46communicating with an inferior shell process (@pxref{Interactive
47Shell}).
48
49 Most programming-language major modes specify that only blank lines
50separate paragraphs. This is to make the paragraph commands useful.
51(@xref{Paragraphs}.) They also cause Auto Fill mode to use the
52definition of @key{TAB} to indent the new lines it creates. This is
53because most lines in a program are usually indented
54(@pxref{Indentation}).
55
56@menu
57* Choosing Modes:: How major modes are specified or chosen.
58@end menu
59
60@node Choosing Modes,,Major Modes,Major Modes
61@section How Major Modes are Chosen
62
63@cindex choosing a major mode
64 You can select a major mode explicitly for the current buffer, but
65most of the time Emacs determines which mode to use based on the file
66name or on special text in the file.
67
68 To explicitly select a new major, you use an @kbd{M-x} command.
69Take the name of a major mode and add @code{-mode} to get the name of
70the command to select that mode. Thus, you can enter Lisp mode by
71executing @kbd{M-x lisp-mode}.
72
73@vindex auto-mode-alist
74 When you visit a file, Emacs usually chooses the right major mode based
75on the file's name. For example, files whose names end in @samp{.c} are
76edited in C mode. The correspondence between file names and major modes is
77controlled by the variable @code{auto-mode-alist}. Its value is a list in
78which each element has this form,
79
80@example
81(@var{regexp} . @var{mode-function})
82@end example
83
84@noindent
85or this form,
86
87@example
88(@var{regexp} @var{mode-function} @var{flag})
89@end example
90
91@noindent
92For example, one element normally found in the list has the form
93@code{(@t{"\\.c\\'"} . c-mode)}, and it is responsible for selecting C
94mode for files whose names end in @file{.c}. (Note that @samp{\\} is
95needed in Lisp syntax to include a @samp{\} in the string, which must
96be used to suppress the special meaning of @samp{.} in regexps.) If
97the element has the form @code{(@var{regexp} @var{mode-function}
98@var{flag})} and @var{flag} is non-@code{nil}, then after calling
99@var{mode-function}, Emacs discards the suffix that matched
100@var{regexp} and searches the list again for another match.
101
102@vindex magic-mode-alist
103 Sometimes the major mode is determined from the way the file's text
104begins. The variable @code{magic-mode-alist} controls this. Its value
105is a list of elements of these forms:
106
107@example
108(@var{regexp} . @var{mode-function})
109(@var{match-function} . @var{mode-function})
110@end example
111
112@noindent
113The first form looks like an element of @code{auto-mode-alist}, but it
114doesn't work the same: this @var{regexp} is matched against the text
115at the start of the buffer, not against the file name. Likewise, the
116second form calls @var{match-function} at the beginning of the buffer,
117and if the function returns non-@code{nil}, the @var{mode-function} is
118called. @code{magic-mode-alist} takes priority over
119@code{auto-mode-alist}.
120
121 You can specify the major mode to use for editing a certain file by
122special text in the first nonblank line of the file. The
123mode name should appear in this line both preceded and followed by
124@samp{-*-}. Other text may appear on the line as well. For example,
125
126@example
127;-*-Lisp-*-
128@end example
129
130@noindent
131tells Emacs to use Lisp mode. Such an explicit specification overrides
132any defaults based on the file name. Note how the semicolon is used
133to make Lisp treat this line as a comment.
134
135 Another format of mode specification is
136
137@example
138-*- mode: @var{modename};-*-
139@end example
140
141@noindent
142which allows you to specify local variables as well, like this:
143
144@example
145-*- mode: @var{modename}; @var{var}: @var{value}; @dots{} -*-
146@end example
147
148@noindent
149@xref{File Variables}, for more information about this.
150
151@vindex auto-mode-case-fold
152 On systems with case-insensitive file names, only a single
153case-insensitive search through the @code{auto-mode-alist} is made.
154On other systems, Emacs normally performs a single case-sensitive
155search through the alist, but if you set this variable to a
156non-@code{nil} value, Emacs will perform a second case-insensitive
157search if the first search fails.
158
159@vindex interpreter-mode-alist
160 When a file's contents begin with @samp{#!}, it can serve as an
161executable shell command, which works by running an interpreter named on
162the file's first line. The rest of the file is used as input to the
163interpreter.
164
165 When you visit such a file in Emacs, if the file's name does not
166specify a major mode, Emacs uses the interpreter name on the first line
167to choose a mode. If the first line is the name of a recognized
168interpreter program, such as @samp{perl} or @samp{tcl}, Emacs uses a
169mode appropriate for programs for that interpreter. The variable
170@code{interpreter-mode-alist} specifies the correspondence between
171interpreter program names and major modes.
172
173 When the first line starts with @samp{#!}, you cannot (on many
174systems) use the @samp{-*-} feature on the first line, because the
175system would get confused when running the interpreter. So Emacs looks
176for @samp{-*-} on the second line in such files as well as on the
1ab397c1
WL
177first line. The same is true for man pages which start with the magic
178string @samp{'\"} to specify a list of troff preprocessors (not all do,
179however).
180
8cf51b2c
GM
181
182@vindex default-major-mode
183 When you visit a file that does not specify a major mode to use, or
184when you create a new buffer with @kbd{C-x b}, the variable
185@code{default-major-mode} specifies which major mode to use. Normally
186its value is the symbol @code{fundamental-mode}, which specifies
187Fundamental mode. If @code{default-major-mode} is @code{nil}, the major
188mode is taken from the previously current buffer.
189
190@findex normal-mode
191 If you change the major mode of a buffer, you can go back to the major
192mode Emacs would choose automatically: use the command @kbd{M-x
193normal-mode} to do this. This is the same function that
194@code{find-file} calls to choose the major mode. It also processes
195the file's @samp{-*-} line or local variables list (if any).
196@xref{File Variables}.
197
198@vindex change-major-mode-with-file-name
199 The commands @kbd{C-x C-w} and @code{set-visited-file-name} change to
200a new major mode if the new file name implies a mode (@pxref{Saving}).
201(@kbd{C-x C-s} does this too, if the buffer wasn't visiting a file.)
202However, this does not happen if the buffer contents specify a major
203mode, and certain ``special'' major modes do not allow the mode to
204change. You can turn off this mode-changing feature by setting
205@code{change-major-mode-with-file-name} to @code{nil}.
206
207@ignore
208 arch-tag: f2558800-cf32-4839-8acb-7d3b4df2a155
209@end ignore