The Great Whitespace Cleanup
[sxemacs] / info / sxemacs / major.texi
1 @node Major Modes, Indentation, Mule, Top
2 @chapter Major Modes
3 @cindex major modes
4 @kindex TAB
5 @kindex DEL
6 @kindex LFD
7
8   Emacs has many different @dfn{major modes}, each of which customizes
9 Emacs for editing text of a particular sort.  The major modes are mutually
10 exclusive;  at any time, each buffer has one major mode.  The mode line
11 normally contains the name of the current major mode in parentheses.
12 @xref{Mode Line}.
13
14   The least specialized major mode is called @dfn{Fundamental mode}.  This
15 mode has no mode-specific redefinitions or variable settings.  Each
16 Emacs command behaves in its most general manner, and each option is in its
17 default state.  For editing any specific type of text, such as Lisp code or
18 English text, you should switch to the appropriate major mode, such as Lisp
19 mode or Text mode.
20
21   Selecting a major mode changes the meanings of a few keys to become
22 more specifically adapted to the language being edited.  @key{TAB},
23 @key{DEL}, and @key{LFD} are changed frequently.  In addition, commands
24 which handle comments use the mode to determine how to delimit comments.
25 Many major modes redefine the syntactical properties of characters
26 appearing in the buffer.  @xref{Syntax}.
27
28   The major modes fall into three major groups.  Programming modes
29 (@pxref{Programs}) are for specific programming languages.  Text modes
30 (like Nroff mode, @TeX{} mode, Outline mode, XML mode, etc.@:) are for
31 editing human readable text.  The remaining major modes are not intended
32 for direct use in editing user files; they are used in buffers created
33 by Emacs for specific purposes. Examples of such modes include Dired
34 mode which is used for buffers made by Dired (@pxref{Dired}), Mail mode
35 for buffers made by @kbd{C-x m} (@pxref{Sending Mail}), and Shell mode
36 for buffers used for communicating with an inferior shell process
37 (@pxref{Interactive Shell}).
38
39   Most programming language major modes specify that only blank lines
40 separate paragraphs.  This is so that the paragraph commands remain useful.
41 @xref{Paragraphs}.  They also cause Auto Fill mode to use the definition of
42 @key{TAB} to indent the new lines it creates.  This is because most lines
43 in a program are usually indented.  @xref{Indentation}.
44
45 @menu
46 * Choosing Modes::     How major modes are specified or chosen.
47 * Mode Hooks::         Customizing a major mode
48 @end menu
49
50 @node Choosing Modes, Mode Hooks, Major Modes, Major Modes
51 @section Choosing Major Modes
52
53   You can select a major mode explicitly for the current buffer, but
54 most of the time Emacs determines which mode to use based on the file
55 name or some text in the file.
56
57   Use a @kbd{M-x} command to explicitly select a new major mode.  Add
58 @code{-mode} to the name of a major mode to get the name of a command to
59 select that mode.  For example, to enter Lisp mode, execute @kbd{M-x
60 lisp-mode}.
61
62 @vindex auto-mode-alist
63   When you visit a file, Emacs usually chooses the right major mode
64 based on the file's name.  For example, files whose names end in
65 @code{.c} are edited in C mode.  The variable @code{auto-mode-alist}
66 controls the correspondence between file names and major mode.  Its value
67 is a list in which each element has the form:
68
69 @example
70 (@var{regexp} . @var{mode-function})
71 @end example
72
73 @noindent
74 For example, one element normally found in the list has the form
75 @code{(@t{"\\.c$"} . c-mode)}. It is responsible for selecting C mode
76 for files whose names end in @file{.c}.  (Note that @samp{\\} is needed in
77 Lisp syntax to include a @samp{\} in the string, which is needed to
78 suppress the special meaning of @samp{.} in regexps.)  The only practical
79 way to change this variable is with Lisp code.
80
81   You can specify which major mode should be used for editing a certain
82 file by a special sort of text in the first non-blank line of the file.
83 The mode name should appear in this line both preceded and followed by
84 @samp{-*-}.  Other text may appear on the line as well.  For example,
85
86 @example
87 ;-*-Lisp-*-
88 @end example
89
90 @noindent
91 tells Emacs to use Lisp mode.  Note how the semicolon is used to make Lisp
92 treat this line as a comment.  Such an explicit specification overrides any
93 default mode based on the file name.
94
95   Another format of mode specification is:
96
97 @example
98 -*-Mode: @var{modename};-*-
99 @end example
100
101 @noindent
102 which allows other things besides the major mode name to be specified.
103 However, Emacs does not look for anything except the mode name.
104
105 The major mode can also be specified in a local variables list.
106 @xref{File Variables}.
107
108 @vindex default-major-mode
109   When you visit a file that does not specify a major mode to use, or
110 when you create a new buffer with @kbd{C-x b}, Emacs uses the major mode
111 specified by the variable @code{default-major-mode}.  Normally this
112 value is the symbol @code{fundamental-mode}, which specifies Fundamental
113 mode.  If @code{default-major-mode} is @code{nil}, the major mode is
114 taken from the previously selected buffer.
115
116 @node Mode Hooks,  , Choosing Modes, Major Modes
117 @section Mode Hook Variables
118
119 @cindex Hook variables
120 @cindex mode hook
121 @findex add-hook
122 @findex remove-hook
123 @vindex lisp-mode-hook
124 @vindex emacs-lisp-mode-hook
125 @vindex lisp-interaction-mode-hook
126 @vindex scheme-mode-hook
127
128   The last step taken by a major mode, by convention, is to invoke a
129 list of user supplied functions that are stored in a ``hook'' variable.
130 This allows a user to further customize the major mode, and is
131 particularly convenient for setting up buffer local variables
132 (@pxref{Locals}).
133
134   The name of the hook variable is created by appending the string
135 @code{-hook} to the name of the major mode.  For example, the hook
136 variable used by @code{text-mode} would be named @code{text-mode-hook}.
137 By convention the mode hook function receives no arguments. If a hook
138 variable does not exist, or it has the value @code{nil}, the major mode
139 simply ignores it.
140
141   The recommended way to add functions to a hook variable is with the
142 @code{add-hook} function.  For example, to automatically turn on the
143 Auto Fill mode when Text mode is invoked the following code can be used in
144 the initialization file (@pxref{Init File})
145
146 @example
147 (add-hook 'text-mode-hook 'turn-on-auto-fill)
148 @end example
149
150 The @code{add-hook} function will check that the function is not already
151 listed in the hook variable before adding it. It will also create a hook
152 variable with the value @code{nil} if one does not exist before adding
153 the function. @code{add-hook} adds functions to the front of the hook
154 variable list. This means that the last hook added is run first by the
155 major mode. It is considered very poor style to write hook functions
156 that depend on the order that hooks are executed.
157
158 Hooks can be removed from hook variables with @code{remove-hook}.