aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChong Yidong2008-10-21 03:21:29 +0000
committerChong Yidong2008-10-21 03:21:29 +0000
commit8be68090963c9306b016df9fbefca1c634ec8da9 (patch)
treef648a1cbf77badb95a363c90334b2f5750ae73d1
parent69051ab62bfe0043e5d1965f31c080d3efee42ad (diff)
downloademacs-8be68090963c9306b016df9fbefca1c634ec8da9.tar.gz
emacs-8be68090963c9306b016df9fbefca1c634ec8da9.zip
(Choosing Modes): Make mode selection sequence more obvious by
describing the steps in order of priority. Note that magic-mode-alist is nil by default. Document magic-fallback-mode-alist.
-rw-r--r--doc/emacs/major.texi163
1 files changed, 90 insertions, 73 deletions
diff --git a/doc/emacs/major.texi b/doc/emacs/major.texi
index 0387aa5f7c2..1774adfc1c3 100644
--- a/doc/emacs/major.texi
+++ b/doc/emacs/major.texi
@@ -71,121 +71,138 @@ the command to select that mode. Thus, you can enter Lisp mode by
71executing @kbd{M-x lisp-mode}. 71executing @kbd{M-x lisp-mode}.
72 72
73@vindex auto-mode-alist 73@vindex auto-mode-alist
74 When you visit a file, Emacs usually chooses the right major mode based 74 When you visit a file, Emacs usually chooses the right major mode
75on the file's name. For example, files whose names end in @samp{.c} are 75automatically. Normally, the choice is made based on the file
76edited in C mode. The correspondence between file names and major modes is 76name---for example, files whose names end in @samp{.c} are normally
77controlled by the variable @code{auto-mode-alist}. Its value is a list in 77edited in C mode---but sometimes the major mode is selected using the
78which each element has this form, 78contents of the file. Here is the exact procedure:
79
80 First, Emacs checks whether the file contains a file-local variable
81that specifies the major mode. If so, it uses that major mode,
82ignoring all other criteria. @xref{File Variables}. There are
83several methods to specify a major mode using a file-local variable;
84the simplest is to put the mode name in the first nonblank line,
85preceded and followed by @samp{-*-}. Other text may appear on the
86line as well. For example,
79 87
80@example 88@example
81(@var{regexp} . @var{mode-function}) 89; -*-Lisp-*-
82@end example 90@end example
83 91
84@noindent 92@noindent
85or this form, 93tells Emacs to use Lisp mode. Note how the semicolon is used to make
94Lisp treat this line as a comment. Alternatively, you could write
86 95
87@example 96@example
88(@var{regexp} @var{mode-function} @var{flag}) 97; -*- mode: Lisp;-*-
89@end example 98@end example
90 99
91@noindent 100@noindent
92For example, one element normally found in the list has the form 101The latter format allows you to specify local variables as well, like
93@code{(@t{"\\.c\\'"} . c-mode)}, and it is responsible for selecting C 102this:
94mode for files whose names end in @file{.c}. (Note that @samp{\\} is 103
95needed in Lisp syntax to include a @samp{\} in the string, which must 104@example
96be used to suppress the special meaning of @samp{.} in regexps.) If 105; -*- mode: Lisp; tab-width: 4; -*-
97the element has the form @code{(@var{regexp} @var{mode-function} 106@end example
98@var{flag})} and @var{flag} is non-@code{nil}, then after calling 107
99@var{mode-function}, Emacs discards the suffix that matched 108@vindex interpreter-mode-alist
100@var{regexp} and searches the list again for another match. 109 Secondly, Emacs checks whether the file's contents begin with
110@samp{#!}. If so, that indicates that the file can serve as an
111executable shell command, which works by running an interpreter named
112on the file's first line (the rest of the file is used as input to the
113interpreter). Therefore, Emacs tries to use the interpreter name to
114choose a mode. For instance, a file that begins with
115@samp{#!/usr/bin/perl} is opened in Perl mode. The variable
116@code{interpreter-mode-alist} specifies the correspondence between
117interpreter program names and major modes.
118
119 When the first line starts with @samp{#!}, you usually cannot use
120the @samp{-*-} feature on the first line, because the system would get
121confused when running the interpreter. So Emacs looks for @samp{-*-}
122on the second line in such files as well as on the first line. The
123same is true for man pages which start with the magic string
124@samp{'\"} to specify a list of troff preprocessors.
101 125
102@vindex magic-mode-alist 126@vindex magic-mode-alist
103 Sometimes the major mode is determined from the way the file's text 127 Thirdly, Emacs tries to determine the major mode by looking at the
104begins. The variable @code{magic-mode-alist} controls this. Its value 128text at the start of the buffer, based on the variable
105is a list of elements of these forms: 129@code{magic-mode-alist}. By default, this variable is @code{nil} (an
130empty list), so Emacs skips this step; however, you can customize it
131in your init file (@pxref{Init File}). The value should be a list of
132elements of the form
106 133
107@example 134@example
108(@var{regexp} . @var{mode-function}) 135(@var{regexp} . @var{mode-function})
109(@var{match-function} . @var{mode-function})
110@end example 136@end example
111 137
112@noindent 138@noindent
113The first form looks like an element of @code{auto-mode-alist}, but it 139where @var{regexp} is a regular expression (@pxref{Regexps}), and
114doesn't work the same: this @var{regexp} is matched against the text 140@var{mode-function} is a Lisp function that toggles a major mode. If
115at the start of the buffer, not against the file name. Likewise, the 141the text at the beginning of the file matches @var{regexp}, Emacs
116second form calls @var{match-function} at the beginning of the buffer, 142chooses the major mode specified by @var{mode-function}.
117and if the function returns non-@code{nil}, the @var{mode-function} is 143
118called. @code{magic-mode-alist} takes priority over 144Alternatively, an element of @code{magic-mode-alist} may have the form
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 145
126@example 146@example
127;-*-Lisp-*- 147(@var{match-function} . @var{mode-function})
128@end example 148@end example
129 149
130@noindent 150@noindent
131tells Emacs to use Lisp mode. Such an explicit specification overrides 151where @var{match-function} is a Lisp function that is called at the
132any defaults based on the file name. Note how the semicolon is used 152beginning of the buffer; if the function returns non-@code{nil}, Emacs
133to make Lisp treat this line as a comment. 153set the major mode wit @var{mode-function}.
134 154
135 Another format of mode specification is 155 Fourthly---if Emacs still hasn't found a suitable major mode---it
156looks at the file's name. The correspondence between file names and
157major modes is controlled by the variable @code{auto-mode-alist}. Its
158value is a list in which each element has this form,
136 159
137@example 160@example
138-*- mode: @var{modename};-*- 161(@var{regexp} . @var{mode-function})
139@end example 162@end example
140 163
141@noindent 164@noindent
142which allows you to specify local variables as well, like this: 165or this form,
143 166
144@example 167@example
145-*- mode: @var{modename}; @var{var}: @var{value}; @dots{} -*- 168(@var{regexp} @var{mode-function} @var{flag})
146@end example 169@end example
147 170
148@noindent 171@noindent
149@xref{File Variables}, for more information about this. 172For example, one element normally found in the list has the form
173@code{(@t{"\\.c\\'"} . c-mode)}, and it is responsible for selecting C
174mode for files whose names end in @file{.c}. (Note that @samp{\\} is
175needed in Lisp syntax to include a @samp{\} in the string, which must
176be used to suppress the special meaning of @samp{.} in regexps.) If
177the element has the form @code{(@var{regexp} @var{mode-function}
178@var{flag})} and @var{flag} is non-@code{nil}, then after calling
179@var{mode-function}, Emacs discards the suffix that matched
180@var{regexp} and searches the list again for another match.
150 181
151@vindex auto-mode-case-fold 182@vindex auto-mode-case-fold
152 On systems with case-insensitive file names, only a single 183 On systems with case-insensitive file names, such as Microsoft
153case-insensitive search through the @code{auto-mode-alist} is made. 184Windows, Emacs performs a single case-insensitive search through
154On other systems, Emacs normally performs a single case-sensitive 185@code{auto-mode-alist}. On other systems, Emacs normally performs a
155search through the alist, but if you set this variable to a 186single case-sensitive search through the alist. However, if you
156non-@code{nil} value, Emacs will perform a second case-insensitive 187change the variable @code{auto-mode-case-fold} to @code{t}, Emacs
157search if the first search fails. 188performs a second case-insensitive search if the first search fails.
158 189
159@vindex interpreter-mode-alist 190@vindex magic-fallback-mode-alist
160 When a file's contents begin with @samp{#!}, it can serve as an 191 Finally, if Emacs @emph{still} hasn't found a major mode to use, it
161executable shell command, which works by running an interpreter named on 192compares the text at the start of the buffer to the variable
162the file's first line. The rest of the file is used as input to the 193@code{magic-fallback-mode-alist}. This variable works like
163interpreter. 194@code{magic-mode-alist}, described above, except that is consulted
164 195only after @code{auto-mode-alist}. By default,
165 When you visit such a file in Emacs, if the file's name does not 196@code{magic-fallback-mode-alist} contains forms that check for image
166specify a major mode, Emacs uses the interpreter name on the first line 197files, HTML/XML/SGML files, and Postscript files.
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
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
181 198
182@vindex default-major-mode 199@vindex default-major-mode
183 When you visit a file that does not specify a major mode to use, or 200 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 201when you create a new buffer with @kbd{C-x b}, the variable
185@code{default-major-mode} specifies which major mode to use. Normally 202@code{default-major-mode} specifies which major mode to use. Normally
186its value is the symbol @code{fundamental-mode}, which specifies 203its value is the symbol @code{fundamental-mode}, which specifies
187Fundamental mode. If @code{default-major-mode} is @code{nil}, the major 204Fundamental mode. If @code{default-major-mode} is @code{nil}, the
188mode is taken from the previously current buffer. 205major mode is taken from the previously current buffer.
189 206
190@findex normal-mode 207@findex normal-mode
191 If you change the major mode of a buffer, you can go back to the major 208 If you change the major mode of a buffer, you can go back to the major