diff options
| author | Chong Yidong | 2008-10-21 03:21:29 +0000 |
|---|---|---|
| committer | Chong Yidong | 2008-10-21 03:21:29 +0000 |
| commit | 8be68090963c9306b016df9fbefca1c634ec8da9 (patch) | |
| tree | f648a1cbf77badb95a363c90334b2f5750ae73d1 | |
| parent | 69051ab62bfe0043e5d1965f31c080d3efee42ad (diff) | |
| download | emacs-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.texi | 163 |
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 | |||
| 71 | executing @kbd{M-x lisp-mode}. | 71 | executing @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 |
| 75 | on the file's name. For example, files whose names end in @samp{.c} are | 75 | automatically. Normally, the choice is made based on the file |
| 76 | edited in C mode. The correspondence between file names and major modes is | 76 | name---for example, files whose names end in @samp{.c} are normally |
| 77 | controlled by the variable @code{auto-mode-alist}. Its value is a list in | 77 | edited in C mode---but sometimes the major mode is selected using the |
| 78 | which each element has this form, | 78 | contents of the file. Here is the exact procedure: |
| 79 | |||
| 80 | First, Emacs checks whether the file contains a file-local variable | ||
| 81 | that specifies the major mode. If so, it uses that major mode, | ||
| 82 | ignoring all other criteria. @xref{File Variables}. There are | ||
| 83 | several methods to specify a major mode using a file-local variable; | ||
| 84 | the simplest is to put the mode name in the first nonblank line, | ||
| 85 | preceded and followed by @samp{-*-}. Other text may appear on the | ||
| 86 | line 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 |
| 85 | or this form, | 93 | tells Emacs to use Lisp mode. Note how the semicolon is used to make |
| 94 | Lisp 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 |
| 92 | For example, one element normally found in the list has the form | 101 | The latter format allows you to specify local variables as well, like |
| 93 | @code{(@t{"\\.c\\'"} . c-mode)}, and it is responsible for selecting C | 102 | this: |
| 94 | mode for files whose names end in @file{.c}. (Note that @samp{\\} is | 103 | |
| 95 | needed in Lisp syntax to include a @samp{\} in the string, which must | 104 | @example |
| 96 | be used to suppress the special meaning of @samp{.} in regexps.) If | 105 | ; -*- mode: Lisp; tab-width: 4; -*- |
| 97 | the 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 | ||
| 111 | executable shell command, which works by running an interpreter named | ||
| 112 | on the file's first line (the rest of the file is used as input to the | ||
| 113 | interpreter). Therefore, Emacs tries to use the interpreter name to | ||
| 114 | choose 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 | ||
| 117 | interpreter program names and major modes. | ||
| 118 | |||
| 119 | When the first line starts with @samp{#!}, you usually cannot use | ||
| 120 | the @samp{-*-} feature on the first line, because the system would get | ||
| 121 | confused when running the interpreter. So Emacs looks for @samp{-*-} | ||
| 122 | on the second line in such files as well as on the first line. The | ||
| 123 | same 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 |
| 104 | begins. The variable @code{magic-mode-alist} controls this. Its value | 128 | text at the start of the buffer, based on the variable |
| 105 | is a list of elements of these forms: | 129 | @code{magic-mode-alist}. By default, this variable is @code{nil} (an |
| 130 | empty list), so Emacs skips this step; however, you can customize it | ||
| 131 | in your init file (@pxref{Init File}). The value should be a list of | ||
| 132 | elements 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 |
| 113 | The first form looks like an element of @code{auto-mode-alist}, but it | 139 | where @var{regexp} is a regular expression (@pxref{Regexps}), and |
| 114 | doesn'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 |
| 115 | at the start of the buffer, not against the file name. Likewise, the | 141 | the text at the beginning of the file matches @var{regexp}, Emacs |
| 116 | second form calls @var{match-function} at the beginning of the buffer, | 142 | chooses the major mode specified by @var{mode-function}. |
| 117 | and if the function returns non-@code{nil}, the @var{mode-function} is | 143 | |
| 118 | called. @code{magic-mode-alist} takes priority over | 144 | Alternatively, 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 | ||
| 122 | special text in the first nonblank line of the file. The | ||
| 123 | mode 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 |
| 131 | tells Emacs to use Lisp mode. Such an explicit specification overrides | 151 | where @var{match-function} is a Lisp function that is called at the |
| 132 | any defaults based on the file name. Note how the semicolon is used | 152 | beginning of the buffer; if the function returns non-@code{nil}, Emacs |
| 133 | to make Lisp treat this line as a comment. | 153 | set 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 |
| 156 | looks at the file's name. The correspondence between file names and | ||
| 157 | major modes is controlled by the variable @code{auto-mode-alist}. Its | ||
| 158 | value 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 |
| 142 | which allows you to specify local variables as well, like this: | 165 | or 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. | 172 | For example, one element normally found in the list has the form |
| 173 | @code{(@t{"\\.c\\'"} . c-mode)}, and it is responsible for selecting C | ||
| 174 | mode for files whose names end in @file{.c}. (Note that @samp{\\} is | ||
| 175 | needed in Lisp syntax to include a @samp{\} in the string, which must | ||
| 176 | be used to suppress the special meaning of @samp{.} in regexps.) If | ||
| 177 | the 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 |
| 153 | case-insensitive search through the @code{auto-mode-alist} is made. | 184 | Windows, Emacs performs a single case-insensitive search through |
| 154 | On other systems, Emacs normally performs a single case-sensitive | 185 | @code{auto-mode-alist}. On other systems, Emacs normally performs a |
| 155 | search through the alist, but if you set this variable to a | 186 | single case-sensitive search through the alist. However, if you |
| 156 | non-@code{nil} value, Emacs will perform a second case-insensitive | 187 | change the variable @code{auto-mode-case-fold} to @code{t}, Emacs |
| 157 | search if the first search fails. | 188 | performs 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 |
| 161 | executable shell command, which works by running an interpreter named on | 192 | compares the text at the start of the buffer to the variable |
| 162 | the 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 |
| 163 | interpreter. | 194 | @code{magic-mode-alist}, described above, except that is consulted |
| 164 | 195 | only 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 |
| 166 | specify a major mode, Emacs uses the interpreter name on the first line | 197 | files, HTML/XML/SGML files, and Postscript files. |
| 167 | to choose a mode. If the first line is the name of a recognized | ||
| 168 | interpreter program, such as @samp{perl} or @samp{tcl}, Emacs uses a | ||
| 169 | mode appropriate for programs for that interpreter. The variable | ||
| 170 | @code{interpreter-mode-alist} specifies the correspondence between | ||
| 171 | interpreter program names and major modes. | ||
| 172 | |||
| 173 | When the first line starts with @samp{#!}, you cannot (on many | ||
| 174 | systems) use the @samp{-*-} feature on the first line, because the | ||
| 175 | system would get confused when running the interpreter. So Emacs looks | ||
| 176 | for @samp{-*-} on the second line in such files as well as on the | ||
| 177 | first line. The same is true for man pages which start with the magic | ||
| 178 | string @samp{'\"} to specify a list of troff preprocessors (not all do, | ||
| 179 | however). | ||
| 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 |
| 184 | when you create a new buffer with @kbd{C-x b}, the variable | 201 | when 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 |
| 186 | its value is the symbol @code{fundamental-mode}, which specifies | 203 | its value is the symbol @code{fundamental-mode}, which specifies |
| 187 | Fundamental mode. If @code{default-major-mode} is @code{nil}, the major | 204 | Fundamental mode. If @code{default-major-mode} is @code{nil}, the |
| 188 | mode is taken from the previously current buffer. | 205 | major 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 |