| 1 | @c This is part of the Emacs manual. |
| 2 | @c Copyright (C) 1985, 86, 87, 93, 94, 95, 1997 Free Software Foundation, Inc. |
| 3 | @c See file emacs.texi for copying conditions. |
| 4 | @node Major Modes, Indentation, International, Top |
| 5 | @chapter Major Modes |
| 6 | @cindex major modes |
| 7 | @cindex mode, major |
| 8 | @kindex TAB @r{(and major modes)} |
| 9 | @kindex DEL @r{(and major modes)} |
| 10 | @kindex C-j @r{(and major modes)} |
| 11 | |
| 12 | Emacs provides many alternative @dfn{major modes}, each of which |
| 13 | customizes Emacs for editing text of a particular sort. The major modes |
| 14 | are mutually exclusive, and each buffer has one major mode at any time. |
| 15 | The mode line normally shows the name of the current major mode, in |
| 16 | parentheses (@pxref{Mode Line}). |
| 17 | |
| 18 | The least specialized major mode is called @dfn{Fundamental mode}. |
| 19 | This mode has no mode-specific redefinitions or variable settings, so |
| 20 | that each Emacs command behaves in its most general manner, and each |
| 21 | option is in its default state. For editing text of a specific type |
| 22 | that Emacs knows about, such as Lisp code or English text, you should |
| 23 | switch to the appropriate major mode, such as Lisp mode or Text mode. |
| 24 | |
| 25 | Selecting a major mode changes the meanings of a few keys to become |
| 26 | more specifically adapted to the language being edited. The ones that |
| 27 | are changed frequently are @key{TAB}, @key{DEL}, and @kbd{C-j}. The |
| 28 | prefix key @kbd{C-c} normally contains mode-specific commands. In |
| 29 | addition, the commands which handle comments use the mode to determine |
| 30 | how comments are to be delimited. Many major modes redefine the |
| 31 | syntactical properties of characters appearing in the buffer. |
| 32 | @xref{Syntax}. |
| 33 | |
| 34 | The major modes fall into three major groups. Lisp mode (which has |
| 35 | several variants), C mode, Fortran mode and others are for specific |
| 36 | programming languages. Text mode, Nroff mode, @TeX{} mode and Outline |
| 37 | mode are for editing English text. The remaining major modes are not |
| 38 | intended for use on users' files; they are used in buffers created for |
| 39 | specific purposes by Emacs, such as Dired mode for buffers made by Dired |
| 40 | (@pxref{Dired}), Mail mode for buffers made by @kbd{C-x m} |
| 41 | (@pxref{Sending Mail}), and Shell mode for buffers used for |
| 42 | communicating with an inferior shell process (@pxref{Interactive |
| 43 | Shell}). |
| 44 | |
| 45 | Most programming-language major modes specify that only blank lines |
| 46 | separate paragraphs. This is to make the paragraph commands useful. |
| 47 | (@xref{Paragraphs}.) They also cause Auto Fill mode to use the |
| 48 | definition of @key{TAB} to indent the new lines it creates. This is |
| 49 | because most lines in a program are usually indented. |
| 50 | (@xref{Indentation}.) |
| 51 | |
| 52 | @menu |
| 53 | * Choosing Modes:: How major modes are specified or chosen. |
| 54 | @end menu |
| 55 | |
| 56 | @node Choosing Modes,,Major Modes,Major Modes |
| 57 | @section How Major Modes are Chosen |
| 58 | |
| 59 | @cindex choosing a major mode |
| 60 | You can select a major mode explicitly for the current buffer, but |
| 61 | most of the time Emacs determines which mode to use based on the file |
| 62 | name or on special text in the file. |
| 63 | |
| 64 | Explicit selection of a new major mode is done with a @kbd{M-x} command. |
| 65 | From the name of a major mode, add @code{-mode} to get the name of a |
| 66 | command to select that mode. Thus, you can enter Lisp mode by executing |
| 67 | @kbd{M-x lisp-mode}. |
| 68 | |
| 69 | @vindex auto-mode-alist |
| 70 | When you visit a file, Emacs usually chooses the right major mode based |
| 71 | on the file's name. For example, files whose names end in @samp{.c} are |
| 72 | edited in C mode. The correspondence between file names and major modes is |
| 73 | controlled by the variable @code{auto-mode-alist}. Its value is a list in |
| 74 | which each element has this form, |
| 75 | |
| 76 | @example |
| 77 | (@var{regexp} . @var{mode-function}) |
| 78 | @end example |
| 79 | |
| 80 | @noindent |
| 81 | or this form, |
| 82 | |
| 83 | @example |
| 84 | (@var{regexp} @var{mode-function} @var{flag}) |
| 85 | @end example |
| 86 | |
| 87 | @noindent |
| 88 | For example, one element normally found in the list has the form |
| 89 | @code{(@t{"\\.c\\'"} . c-mode)}, and it is responsible for selecting C |
| 90 | mode for files whose names end in @file{.c}. (Note that @samp{\\} is |
| 91 | needed in Lisp syntax to include a @samp{\} in the string, which is |
| 92 | needed to suppress the special meaning of @samp{.} in regexps.) If the |
| 93 | element has the form @code{(@var{regexp} @var{mode-function} |
| 94 | @var{flag})} and @var{flag} is non-@code{nil}, then after calling |
| 95 | @var{function}, the suffix that matched @var{regexp} is discarded and |
| 96 | the list is searched again for another match. |
| 97 | |
| 98 | You can specify which major mode should be used for editing a certain |
| 99 | file by a special sort of text in the first nonblank line of the file. The |
| 100 | mode name should appear in this line both preceded and followed by |
| 101 | @samp{-*-}. Other text may appear on the line as well. For example, |
| 102 | |
| 103 | @example |
| 104 | ;-*-Lisp-*- |
| 105 | @end example |
| 106 | |
| 107 | @noindent |
| 108 | tells Emacs to use Lisp mode. Such an explicit specification overrides |
| 109 | any defaulting based on the file name. Note how the semicolon is used |
| 110 | to make Lisp treat this line as a comment. |
| 111 | |
| 112 | Another format of mode specification is |
| 113 | |
| 114 | @example |
| 115 | -*- mode: @var{modename};-*- |
| 116 | @end example |
| 117 | |
| 118 | @noindent |
| 119 | which allows you to specify local variables as well, like this: |
| 120 | |
| 121 | @example |
| 122 | -*- mode: @var{modename}; @var{var}: @var{value}; @dots{} -*- |
| 123 | @end example |
| 124 | |
| 125 | @noindent |
| 126 | @xref{File Variables}, for more information about this. |
| 127 | |
| 128 | @vindex interpreter-mode-alist |
| 129 | When a file's contents begin with @samp{#!}, it can serve as an |
| 130 | executable shell command, which works by running an interpreter named on |
| 131 | the file's first line. The rest of the file is used as input to the |
| 132 | interpreter. |
| 133 | |
| 134 | When you visit such a file in Emacs, if the file's name does not |
| 135 | specify a major mode, Emacs uses the interpreter name on the first line |
| 136 | to choose a mode. If the first line is the name of a recognized |
| 137 | interpreter program, such as @samp{perl} or @samp{tcl}, Emacs uses a |
| 138 | mode appropriate for programs for that interpreter. The variable |
| 139 | @code{interpreter-mode-alist} specifies the correspondence between |
| 140 | interpreter program names and major modes. |
| 141 | |
| 142 | When the first line starts with @samp{#!}, you cannot (on many |
| 143 | systems) use the @samp{-*-} feature on the first line, because the |
| 144 | system would get confused when running the interpreter. So Emacs looks |
| 145 | for @samp{-*-} on the second line in such files as well as on the |
| 146 | first line. |
| 147 | |
| 148 | @vindex default-major-mode |
| 149 | When you visit a file that does not specify a major mode to use, or |
| 150 | when you create a new buffer with @kbd{C-x b}, the variable |
| 151 | @code{default-major-mode} specifies which major mode to use. Normally |
| 152 | its value is the symbol @code{fundamental-mode}, which specifies |
| 153 | Fundamental mode. If @code{default-major-mode} is @code{nil}, the major |
| 154 | mode is taken from the previously selected buffer. |
| 155 | |
| 156 | @findex normal-mode |
| 157 | If you change the major mode of a buffer, you can go back to the major |
| 158 | mode Emacs would choose automatically: use the command @kbd{M-x |
| 159 | normal-mode} to do this. This is the same function that |
| 160 | @code{find-file} calls to choose the major mode. It also processes |
| 161 | the file's local variables list if any. |
| 162 | |
| 163 | @vindex change-major-mode-with-file-name |
| 164 | The commands @kbd{C-x C-w} and @code{set-visited-file-name} change to |
| 165 | a new major mode if the new file name implies a mode (@pxref{Saving}). |
| 166 | However, this does not happen if the buffer contents specify a major |
| 167 | mode, and certain ``special'' major modes do not allow the mode to |
| 168 | change. You can turn off this mode-changing feature by setting |
| 169 | @code{change-major-mode-with-file-name} to @code{nil}. |