Commit | Line | Data |
---|---|---|
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 | |
9 | basic behavior in useful ways. These are divided into @dfn{major | |
10 | modes} and @dfn{minor modes}. | |
11 | ||
12 | Major modes provide specialized facilities for working on a | |
13 | particular file type, such as a C source file (@pxref{Programs}), or a | |
14 | particular type of non-file buffer, such as a shell buffer | |
15 | (@pxref{Shell}). Major modes are mutually exclusive; each buffer has | |
16 | one and only one major mode at any time. | |
17 | ||
18 | Minor modes are optional features which you can turn on or off, not | |
19 | necessarily specific to a type of file or buffer. For example, Auto | |
20 | Fill mode is a minor mode in which @key{SPC} breaks lines between | |
21 | words as you type (@pxref{Auto Fill}). Minor modes are independent of | |
22 | one 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 | |
40 | behavior of Emacs while that buffer is current. The mode line | |
41 | normally 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 | |
45 | first visit a file or create a buffer. @xref{Choosing Modes}. You | |
46 | can explicitly select a new major mode by using an @kbd{M-x} command. | |
47 | Take the name of the mode and add @code{-mode} to get the name of the | |
48 | command 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}. | |
52 | This mode has no mode-specific redefinitions or variable settings, so | |
53 | that each Emacs command behaves in its most general manner, and each | |
54 | user option variable is in its default state. | |
55 | ||
56 | For editing text of a specific type that Emacs knows about, such as | |
57 | Lisp code or English text, you typically use a more specialized major | |
58 | mode, such as Lisp mode or Text mode. Such major modes change the | |
59 | meanings of some keys to become more specifically adapted to the | |
60 | language being edited. The ones that are commonly changed are | |
61 | @key{TAB}, @key{DEL}, and @kbd{C-j}. The prefix key @kbd{C-c} | |
62 | normally contains mode-specific commands. In addition, the commands | |
63 | which handle comments use the mode to determine how comments are to be | |
64 | delimited. Many major modes redefine the syntactical properties of | |
65 | characters appearing in the buffer. | |
66 | ||
67 | The major modes fall into three major groups. The first group | |
68 | contains modes for normal text, either plain or with mark-up. It | |
69 | includes Text mode, HTML mode, SGML mode, @TeX{} mode and Outline | |
70 | mode. The second group contains modes for specific programming | |
71 | languages. These include Lisp mode (which has several variants), C | |
72 | mode, Fortran mode, and others. The remaining major modes are not | |
73 | intended for use on users' files; they are used in buffers created for | |
74 | specific purposes by Emacs, such as Dired mode for buffers made by | |
75 | Dired (@pxref{Dired}), Message mode for buffers made by @kbd{C-x m} | |
76 | (@pxref{Sending Mail}), and Shell mode for buffers used for | |
77 | communicating with an inferior shell process (@pxref{Interactive | |
78 | Shell}). | |
79 | ||
80 | Most programming-language major modes specify that only blank lines | |
81 | separate paragraphs. This is to make the paragraph commands useful. | |
82 | (@xref{Paragraphs}.) They also cause Auto Fill mode to use the | |
83 | definition of @key{TAB} to indent the new lines it creates. This is | |
84 | because 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 | |
93 | of Emacs in some well-defined way. Unlike major modes, any number of | |
94 | minor 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 | |
96 | enable the mode in certain buffers and not others. Other minor modes | |
97 | are @dfn{global}: while enabled, they affect everything you do in the | |
98 | Emacs session, in all buffers. Some global minor modes are enabled by | |
99 | default. | |
100 | ||
101 | Most minor modes say in the mode line when they are enabled, just | |
102 | after the major mode indicator. For example, @samp{Fill} in the mode | |
103 | line means that Auto Fill mode is enabled. @xref{Mode Line}. | |
104 | ||
105 | Each minor mode is associated with a command, called the @dfn{mode | |
106 | command}, which turns it on or off. The name of this command consists | |
107 | of the name of the minor mode, followed by @samp{-mode}; for instance, | |
108 | the mode command for Auto Fill mode is @code{auto-fill-mode}. Calling | |
109 | the minor mode command with no prefix argument @dfn{toggles} the mode, | |
110 | turning it on if it was off, and off if it was on. A positive | |
111 | argument always turns the mode on, and a zero or negative argument | |
112 | always 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 | |
114 | Bindings}). | |
115 | ||
116 | Most minor modes also have a @dfn{mode variable}, with the same name | |
117 | as the mode command. Its value is non-@code{nil} if the mode is | |
118 | enabled, and @code{nil} if it is disabled. In some minor modes---but | |
119 | not all---the value of the variable alone determines whether the mode | |
120 | is active: the mode command works simply by setting the variable, and | |
121 | changing the value of the variable has the same effect as calling the | |
122 | mode command. Because not all minor modes work this way, we recommend | |
123 | that you avoid changing the mode variables directly; use the mode | |
124 | commands instead. | |
125 | ||
126 | The following is a list of some buffer-local minor modes: | |
127 | ||
128 | @itemize @bullet | |
129 | @item | |
130 | Abbrev mode automatically expands text based on pre-defined | |
131 | abbreviation definitions. @xref{Abbrevs}. | |
132 | ||
133 | @item | |
134 | Auto Fill mode inserts newlines as you type to prevent lines from | |
135 | becoming too long. @xref{Filling}. | |
136 | ||
137 | @item | |
138 | Auto Save mode saves the buffer contents periodically to reduce the | |
139 | amount of work you can lose in case of a crash. @xref{Auto Save}. | |
140 | ||
141 | @item | |
142 | Enriched mode enables editing and saving of formatted text. | |
143 | @xref{Formatted Text}. | |
144 | ||
145 | @item | |
146 | Flyspell mode automatically highlights misspelled words. | |
147 | @xref{Spelling}. | |
148 | ||
149 | @item | |
150 | Font-Lock mode automatically highlights certain textual units found in | |
151 | programs. It is enabled globally by default, but you can disable it | |
152 | in individual buffers. @xref{Faces}. | |
153 | ||
154 | @findex linum-mode | |
155 | @cindex Linum mode | |
156 | @item | |
157 | Linum mode displays each line's line number in the window's left | |
158 | margin. Its mode command is @code{linum-mode}. | |
159 | ||
160 | @item | |
161 | Outline minor mode provides similar facilities to the major mode | |
162 | called Outline mode. @xref{Outline Mode}. | |
163 | ||
164 | @cindex Overwrite mode | |
165 | @cindex mode, Overwrite | |
166 | @findex overwrite-mode | |
167 | @kindex INSERT | |
168 | @item | |
169 | Overwrite mode causes ordinary printing characters to replace existing | |
170 | text instead of shoving it to the right. For example, if point is in | |
171 | front of the @samp{B} in @samp{FOOBAR}, then in Overwrite mode typing | |
172 | a @kbd{G} changes it to @samp{FOOGAR}, instead of producing | |
173 | @samp{FOOGBAR} as usual. In Overwrite mode, the command @kbd{C-q} | |
174 | inserts the next character whatever it may be, even if it is a | |
175 | digit---this gives you a way to insert a character instead of | |
176 | replacing 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 | |
181 | Binary Overwrite mode is a variant of Overwrite mode for editing | |
182 | binary files; it treats newlines and tabs like other characters, so | |
183 | that they overwrite other characters and can be overwritten by them. | |
184 | In Binary Overwrite mode, digits after @kbd{C-q} specify an octal | |
185 | character code, as usual. | |
186 | ||
187 | @item | |
188 | Visual Line mode performs ``word wrapping'', causing long lines to be | |
189 | wrapped at word boundaries. @xref{Visual Line Mode}. | |
190 | @end itemize | |
191 | ||
192 | Here are some useful global minor modes. Since Line Number mode and | |
193 | Transient Mark mode can be enabled or disabled just by setting the | |
194 | value of the minor mode variable, you @emph{can} set them differently | |
195 | for particular buffers, by explicitly making the corresponding | |
196 | variable local in those buffers. @xref{Locals}. | |
197 | ||
198 | @itemize @bullet | |
199 | @item | |
200 | Column Number mode enables display of the current column number in the | |
201 | mode line. @xref{Mode Line}. | |
202 | ||
203 | @item | |
204 | Delete Selection mode causes text insertion to first delete the text | |
205 | in the region, if the region is active. @xref{Using Region}. | |
206 | ||
207 | @item | |
208 | Icomplete mode displays an indication of available completions when | |
209 | you are in the minibuffer and completion is active. @xref{Completion | |
210 | Options}. | |
211 | ||
212 | @item | |
213 | Line Number mode enables display of the current line number in the | |
214 | mode line. It is enabled by default. @xref{Mode Line}. | |
215 | ||
216 | @item | |
217 | Menu Bar mode gives each frame a menu bar. It is enabled by default. | |
218 | @xref{Menu Bars}. | |
219 | ||
220 | @item | |
221 | Scroll Bar mode gives each window a scroll bar. It is enabled by | |
222 | default, but the scroll bar is only displayed on graphical terminals. | |
223 | @xref{Scroll Bars}. | |
224 | ||
225 | @item | |
226 | Tool Bar mode gives each frame a tool bar. It is enabled by default, | |
227 | but the tool bar is only displayed on graphical terminals. @xref{Tool | |
228 | Bars}. | |
229 | ||
230 | @item | |
231 | Transient Mark mode highlights the region, and makes many Emacs | |
232 | commands operate on the region when the mark is active. It is enabled | |
233 | by 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. | |
243 | Normally, it makes the choice based on the file name---for example, | |
244 | files whose names end in @samp{.c} are normally edited in C mode---but | |
245 | sometimes it chooses the major mode based on special text in the file. | |
246 | This 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 | |
251 | variables. @xref{File Variables}. If there is a file-local variable | |
252 | that specifies a major mode, then Emacs uses that major mode, ignoring | |
253 | all other criteria. There are several methods to specify a major mode | |
254 | using a file-local variable; the simplest is to put the mode name in | |
255 | the first nonblank line, preceded and followed by @samp{-*-}. Other | |
256 | text may appear on the line as well. For example, | |
257 | ||
258 | @example | |
259 | ; -*-Lisp-*- | |
260 | @end example | |
261 | ||
262 | @noindent | |
263 | tells Emacs to use Lisp mode. Note how the semicolon is used to make | |
264 | Lisp treat this line as a comment. Alternatively, you could write | |
265 | ||
266 | @example | |
267 | ; -*- mode: Lisp;-*- | |
268 | @end example | |
269 | ||
270 | @noindent | |
271 | The latter format allows you to specify local variables as well, like | |
272 | this: | |
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 | |
279 | enables 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 | |
283 | checks whether the file's contents begin with @samp{#!}. If so, that | |
284 | indicates that the file can serve as an executable shell command, | |
285 | which 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). | |
287 | Therefore, Emacs tries to use the interpreter name to choose a mode. | |
288 | For instance, a file that begins with @samp{#!/usr/bin/perl} is opened | |
289 | in Perl mode. The variable @code{interpreter-mode-alist} specifies | |
290 | the correspondence between interpreter program names and major modes. | |
291 | ||
292 | When the first line starts with @samp{#!}, you usually cannot use | |
293 | the @samp{-*-} feature on the first line, because the system would get | |
294 | confused when running the interpreter. So Emacs looks for @samp{-*-} | |
295 | on the second line in such files as well as on the first line. The | |
296 | same 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 | |
301 | text at the start of the buffer, based on the variable | |
302 | @code{magic-mode-alist}. By default, this variable is @code{nil} (an | |
303 | empty list), so Emacs skips this step; however, you can customize it | |
304 | in your init file (@pxref{Init File}). The value should be a list of | |
305 | elements of the form | |
306 | ||
307 | @example | |
308 | (@var{regexp} . @var{mode-function}) | |
309 | @end example | |
310 | ||
311 | @noindent | |
312 | where @var{regexp} is a regular expression (@pxref{Regexps}), and | |
313 | @var{mode-function} is a Lisp function that toggles a major mode. If | |
314 | the text at the beginning of the file matches @var{regexp}, Emacs | |
315 | chooses the major mode specified by @var{mode-function}. | |
316 | ||
317 | Alternatively, 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 | |
324 | where @var{match-function} is a Lisp function that is called at the | |
325 | beginning of the buffer; if the function returns non-@code{nil}, Emacs | |
326 | set the major mode wit @var{mode-function}. | |
327 | ||
328 | Fourth---if Emacs still hasn't found a suitable major mode---it | |
329 | looks at the file's name. The correspondence between file names and | |
330 | major modes is controlled by the variable @code{auto-mode-alist}. Its | |
331 | value 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 | |
338 | or this form, | |
339 | ||
340 | @example | |
341 | (@var{regexp} @var{mode-function} @var{flag}) | |
342 | @end example | |
343 | ||
344 | @noindent | |
345 | For example, one element normally found in the list has the form | |
346 | @code{(@t{"\\.c\\'"} . c-mode)}, and it is responsible for selecting C | |
347 | mode for files whose names end in @file{.c}. (Note that @samp{\\} is | |
348 | needed in Lisp syntax to include a @samp{\} in the string, which must | |
349 | be used to suppress the special meaning of @samp{.} in regexps.) If | |
350 | the 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 |
357 | performs a case-sensitive search through @code{auto-mode-alist}; if | |
358 | this search fails, it performs a second case-insensitive search | |
359 | through the alist. To suppress the second search, change the variable | |
360 | @code{auto-mode-case-fold} to @code{nil}. On systems with | |
361 | case-insensitive file names, such as Microsoft Windows, Emacs performs | |
362 | a 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 | |
366 | compares 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 | |
369 | only after @code{auto-mode-alist}. By default, | |
370 | @code{magic-fallback-mode-alist} contains forms that check for image | |
7877f373 | 371 | files, 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 | |
378 | should not change it yourself. | |
379 | ||
380 | The default value of @code{major-mode} determines the major mode to | |
381 | use for files that do not specify a major mode, and for new buffers | |
382 | created with @kbd{C-x b}. Normally, this default value is the symbol | |
383 | @code{fundamental-mode}, which specifies Fundamental mode. You can | |
384 | change it via the Customization interface (@pxref{Easy | |
385 | Customization}), 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 | |
393 | If the default value of @code{major-mode} is @code{nil}, the major | |
394 | mode 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 | |
398 | the 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 | |
401 | the 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 | |
406 | a 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.) | |
408 | However, this does not happen if the buffer contents specify a major | |
409 | mode, and certain ``special'' major modes do not allow the mode to | |
410 | change. You can turn off this mode-changing feature by setting | |
411 | @code{change-major-mode-with-file-name} to @code{nil}. |