Mercurial > emacs
annotate lispref/modes.texi @ 18429:8326843eefd9
(widget-edit-hook): New variable.
(widget-field-action, widget-choice-action): Run that hook
instead of displaying an echo area message.
(widget-toggle-action): Likewise.
(group-visibility, widget-group-visibility-create):
Moved to cus-edit.el and renamed.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 24 Jun 1997 03:17:34 +0000 |
parents | 8ed8412c1ce7 |
children | 66d807bdc5b4 |
rev | line source |
---|---|
6451 | 1 @c -*-texinfo-*- |
2 @c This is part of the GNU Emacs Lisp Reference Manual. | |
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. | |
4 @c See the file elisp.texi for copying conditions. | |
5 @setfilename ../info/modes | |
6 @node Modes, Documentation, Keymaps, Top | |
7 @chapter Major and Minor Modes | |
8 @cindex mode | |
9 | |
10 A @dfn{mode} is a set of definitions that customize Emacs and can be | |
11 turned on and off while you edit. There are two varieties of modes: | |
12 @dfn{major modes}, which are mutually exclusive and used for editing | |
13 particular kinds of text, and @dfn{minor modes}, which provide features | |
14 that users can enable individually. | |
15 | |
16 This chapter describes how to write both major and minor modes, how to | |
17 indicate them in the mode line, and how they run hooks supplied by the | |
18 user. For related topics such as keymaps and syntax tables, see | |
19 @ref{Keymaps}, and @ref{Syntax Tables}. | |
20 | |
21 @menu | |
22 * Major Modes:: Defining major modes. | |
23 * Minor Modes:: Defining minor modes. | |
24 * Mode Line Format:: Customizing the text that appears in the mode line. | |
25 * Hooks:: How to use hooks; how to write code that provides hooks. | |
26 @end menu | |
27 | |
28 @node Major Modes | |
29 @section Major Modes | |
30 @cindex major mode | |
31 @cindex Fundamental mode | |
32 | |
33 Major modes specialize Emacs for editing particular kinds of text. | |
34 Each buffer has only one major mode at a time. | |
35 | |
36 The least specialized major mode is called @dfn{Fundamental mode}. | |
37 This mode has no mode-specific definitions or variable settings, so each | |
38 Emacs command behaves in its default manner, and each option is in its | |
39 default state. All other major modes redefine various keys and options. | |
40 For example, Lisp Interaction mode provides special key bindings for | |
41 @key{LFD} (@code{eval-print-last-sexp}), @key{TAB} | |
42 (@code{lisp-indent-line}), and other keys. | |
43 | |
44 When you need to write several editing commands to help you perform a | |
45 specialized editing task, creating a new major mode is usually a good | |
46 idea. In practice, writing a major mode is easy (in contrast to | |
47 writing a minor mode, which is often difficult). | |
48 | |
49 If the new mode is similar to an old one, it is often unwise to modify | |
50 the old one to serve two purposes, since it may become harder to use and | |
51 maintain. Instead, copy and rename an existing major mode definition | |
52 and alter the copy---or define a @dfn{derived mode} (@pxref{Derived | |
53 Modes}). For example, Rmail Edit mode, which is in | |
54 @file{emacs/lisp/rmailedit.el}, is a major mode that is very similar to | |
55 Text mode except that it provides three additional commands. Its | |
56 definition is distinct from that of Text mode, but was derived from it. | |
57 | |
58 Rmail Edit mode is an example of a case where one piece of text is put | |
59 temporarily into a different major mode so it can be edited in a | |
60 different way (with ordinary Emacs commands rather than Rmail). In such | |
61 cases, the temporary major mode usually has a command to switch back to | |
62 the buffer's usual mode (Rmail mode, in this case). You might be | |
63 tempted to present the temporary redefinitions inside a recursive edit | |
64 and restore the usual ones when the user exits; but this is a bad idea | |
65 because it constrains the user's options when it is done in more than | |
66 one buffer: recursive edits must be exited most-recently-entered first. | |
67 Using alternative major modes avoids this limitation. @xref{Recursive | |
68 Editing}. | |
69 | |
70 The standard GNU Emacs Lisp library directory contains the code for | |
71 several major modes, in files including @file{text-mode.el}, | |
72 @file{texinfo.el}, @file{lisp-mode.el}, @file{c-mode.el}, and | |
73 @file{rmail.el}. You can look at these libraries to see how modes are | |
74 written. Text mode is perhaps the simplest major mode aside from | |
75 Fundamental mode. Rmail mode is a complicated and specialized mode. | |
76 | |
77 @menu | |
78 * Major Mode Conventions:: Coding conventions for keymaps, etc. | |
79 * Example Major Modes:: Text mode and Lisp modes. | |
80 * Auto Major Mode:: How Emacs chooses the major mode automatically. | |
81 * Mode Help:: Finding out how to use a mode. | |
82 * Derived Modes:: Defining a new major mode based on another major | |
83 mode. | |
84 @end menu | |
85 | |
86 @node Major Mode Conventions | |
87 @subsection Major Mode Conventions | |
88 | |
89 The code for existing major modes follows various coding conventions, | |
90 including conventions for local keymap and syntax table initialization, | |
91 global names, and hooks. Please follow these conventions when you | |
92 define a new major mode: | |
93 | |
94 @itemize @bullet | |
95 @item | |
96 Define a command whose name ends in @samp{-mode}, with no arguments, | |
97 that switches to the new mode in the current buffer. This command | |
98 should set up the keymap, syntax table, and local variables in an | |
99 existing buffer without changing the buffer's text. | |
100 | |
101 @item | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
102 Write a documentation string for this command that describes the |
6451 | 103 special commands available in this mode. @kbd{C-h m} |
104 (@code{describe-mode}) in your mode will display this string. | |
105 | |
106 The documentation string may include the special documentation | |
107 substrings, @samp{\[@var{command}]}, @samp{\@{@var{keymap}@}}, and | |
108 @samp{\<@var{keymap}>}, that enable the documentation to adapt | |
109 automatically to the user's own key bindings. @xref{Keys in | |
110 Documentation}. | |
111 | |
112 @item | |
113 The major mode command should start by calling | |
114 @code{kill-all-local-variables}. This is what gets rid of the local | |
115 variables of the major mode previously in effect. | |
116 | |
117 @item | |
118 The major mode command should set the variable @code{major-mode} to the | |
119 major mode command symbol. This is how @code{describe-mode} discovers | |
120 which documentation to print. | |
121 | |
122 @item | |
123 The major mode command should set the variable @code{mode-name} to the | |
124 ``pretty'' name of the mode, as a string. This appears in the mode | |
125 line. | |
126 | |
127 @item | |
128 @cindex functions in modes | |
129 Since all global names are in the same name space, all the global | |
130 variables, constants, and functions that are part of the mode should | |
131 have names that start with the major mode name (or with an abbreviation | |
17278
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
132 of it if the name is long). @xref{Coding Conventions}. |
6451 | 133 |
134 @item | |
135 @cindex keymaps in modes | |
136 The major mode should usually have its own keymap, which is used as the | |
137 local keymap in all buffers in that mode. The major mode function | |
138 should call @code{use-local-map} to install this local map. | |
139 @xref{Active Keymaps}, for more information. | |
140 | |
141 This keymap should be kept in a global variable named | |
142 @code{@var{modename}-mode-map}. Normally the library that defines the | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
143 mode sets this variable. |
6451 | 144 |
12803
6dcd5ad16790
Make Major Mode Conventions xref to Tips for Defining.
Richard M. Stallman <rms@gnu.org>
parents:
12234
diff
changeset
|
145 @xref{Tips for Defining}, for advice about how to write the code to set |
6dcd5ad16790
Make Major Mode Conventions xref to Tips for Defining.
Richard M. Stallman <rms@gnu.org>
parents:
12234
diff
changeset
|
146 up the mode's keymap variable. |
6dcd5ad16790
Make Major Mode Conventions xref to Tips for Defining.
Richard M. Stallman <rms@gnu.org>
parents:
12234
diff
changeset
|
147 |
6451 | 148 @item |
17278
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
149 The key sequences bound in a major mode keymap should usually start with |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
150 @kbd{C-c}, followed by a control-character, a digit, or @kbd{@{}, |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
151 @kbd{@}}, @kbd{<}, @kbd{>}, @kbd{:} or @kbd{;}. The other punctuation |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
152 characters are reserved for minor modes, and ordinary letters are |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
153 reserved for users. |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
154 |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
155 It is reasonable for a major mode to rebind a key sequence with a |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
156 standard meaning, if it implements a command that does ``the same job'' |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
157 in a way that fits the major mode better. For example, a major mode for |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
158 editing a programming language might redefine @kbd{C-M-a} to ``move to |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
159 the beginning of a function'' in a way that works better for that |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
160 language. |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
161 |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
162 Major modes such as Dired or Rmail that do not allow self-insertion of |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
163 text can reasonably redefine letters and other printing characters as |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
164 editing commands. Dired and Rmail both do this. |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
165 |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
166 @item |
6451 | 167 @cindex syntax tables in modes |
168 The mode may have its own syntax table or may share one with other | |
169 related modes. If it has its own syntax table, it should store this in | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
170 a variable named @code{@var{modename}-mode-syntax-table}. @xref{Syntax |
6451 | 171 Tables}. |
172 | |
173 @item | |
16432
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
174 If the mode handles a language that has a syntax for comments, it should |
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
175 set the variables that define the comment syntax. @xref{Options for |
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
176 Comments,, Options Controlling Comments, emacs, The GNU Emacs Manual}. |
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
177 |
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
178 @item |
6451 | 179 @cindex abbrev tables in modes |
180 The mode may have its own abbrev table or may share one with other | |
181 related modes. If it has its own abbrev table, it should store this in | |
182 a variable named @code{@var{modename}-mode-abbrev-table}. @xref{Abbrev | |
183 Tables}. | |
184 | |
185 @item | |
16432
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
186 @vindex font-lock-defaults |
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
187 The mode should specify how to do highlighting for Font Lock mode, by |
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
188 setting up a buffer-local value for the variable |
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
189 @code{font-lock-defaults}. |
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
190 |
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
191 @item |
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
192 @vindex imenu-generic-expression |
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
193 @vindex imenu-create-index-function |
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
194 The mode should specify how Imenu should find the definitions or |
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
195 sections of a buffer, by setting up a buffer-local value for the |
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
196 variable @code{imenu-generic-expression} or |
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
197 @code{imenu-create-index-function}. |
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
198 |
6f58503776e5
Explain what major modes can do for imenu and font-lock.
Richard M. Stallman <rms@gnu.org>
parents:
16056
diff
changeset
|
199 @item |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
200 Use @code{defvar} to set mode-related variables, so that they are not |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
201 reinitialized if they already have a value. (Such reinitialization |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
202 could discard customizations made by the user.) |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
203 |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
204 @item |
6451 | 205 @cindex buffer-local variables in modes |
206 To make a buffer-local binding for an Emacs customization variable, use | |
207 @code{make-local-variable} in the major mode command, not | |
208 @code{make-variable-buffer-local}. The latter function would make the | |
209 variable local to every buffer in which it is subsequently set, which | |
210 would affect buffers that do not use this mode. It is undesirable for a | |
211 mode to have such global effects. @xref{Buffer-Local Variables}. | |
212 | |
213 It's ok to use @code{make-variable-buffer-local}, if you wish, for a | |
214 variable used only within a single Lisp package. | |
215 | |
216 @item | |
217 @cindex mode hook | |
218 @cindex major mode hook | |
219 Each major mode should have a @dfn{mode hook} named | |
220 @code{@var{modename}-mode-hook}. The major mode command should run that | |
221 hook, with @code{run-hooks}, as the very last thing it | |
17278
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
222 does. @xref{Hooks}. |
6451 | 223 |
224 @item | |
225 The major mode command may also run the hooks of some more basic modes. | |
226 For example, @code{indented-text-mode} runs @code{text-mode-hook} as | |
227 well as @code{indented-text-mode-hook}. It may run these other hooks | |
228 immediately before the mode's own hook (that is, after everything else), | |
229 or it may run them earlier. | |
230 | |
231 @item | |
232 If something special should be done if the user switches a buffer from | |
233 this mode to any other major mode, the mode can set a local value for | |
234 @code{change-major-mode-hook}. | |
235 | |
236 @item | |
237 If this mode is appropriate only for specially-prepared text, then the | |
238 major mode command symbol should have a property named @code{mode-class} | |
239 with value @code{special}, put on as follows: | |
240 | |
241 @cindex @code{mode-class} property | |
242 @cindex @code{special} | |
243 @example | |
244 (put 'funny-mode 'mode-class 'special) | |
245 @end example | |
246 | |
247 @noindent | |
248 This tells Emacs that new buffers created while the current buffer has | |
249 Funny mode should not inherit Funny mode. Modes such as Dired, Rmail, | |
250 and Buffer List use this feature. | |
251 | |
252 @item | |
253 If you want to make the new mode the default for files with certain | |
254 recognizable names, add an element to @code{auto-mode-alist} to select | |
255 the mode for those file names. If you define the mode command to | |
256 autoload, you should add this element in the same file that calls | |
257 @code{autoload}. Otherwise, it is sufficient to add the element in the | |
258 file that contains the mode definition. @xref{Auto Major Mode}. | |
259 | |
260 @item | |
261 @cindex @file{.emacs} customization | |
262 In the documentation, you should provide a sample @code{autoload} form | |
263 and an example of how to add to @code{auto-mode-alist}, that users can | |
264 include in their @file{.emacs} files. | |
265 | |
266 @item | |
267 @cindex mode loading | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
268 The top-level forms in the file defining the mode should be written so |
6451 | 269 that they may be evaluated more than once without adverse consequences. |
270 Even if you never load the file more than once, someone else will. | |
271 @end itemize | |
272 | |
273 @defvar change-major-mode-hook | |
274 This normal hook is run by @code{kill-all-local-variables} before it | |
275 does anything else. This gives major modes a way to arrange for | |
276 something special to be done if the user switches to a different major | |
277 mode. For best results, make this variable buffer-local, so that it | |
278 will disappear after doing its job and will not interfere with the | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
279 subsequent major mode. @xref{Hooks}. |
6451 | 280 @end defvar |
281 | |
282 @node Example Major Modes | |
283 @subsection Major Mode Examples | |
284 | |
285 Text mode is perhaps the simplest mode besides Fundamental mode. | |
286 Here are excerpts from @file{text-mode.el} that illustrate many of | |
287 the conventions listed above: | |
288 | |
289 @smallexample | |
290 @group | |
291 ;; @r{Create mode-specific tables.} | |
292 (defvar text-mode-syntax-table nil | |
293 "Syntax table used while in text mode.") | |
294 @end group | |
295 | |
296 @group | |
297 (if text-mode-syntax-table | |
298 () ; @r{Do not change the table if it is already set up.} | |
299 (setq text-mode-syntax-table (make-syntax-table)) | |
300 (modify-syntax-entry ?\" ". " text-mode-syntax-table) | |
301 (modify-syntax-entry ?\\ ". " text-mode-syntax-table) | |
302 (modify-syntax-entry ?' "w " text-mode-syntax-table)) | |
303 @end group | |
304 | |
305 @group | |
306 (defvar text-mode-abbrev-table nil | |
307 "Abbrev table used while in text mode.") | |
308 (define-abbrev-table 'text-mode-abbrev-table ()) | |
309 @end group | |
310 | |
311 @group | |
312 (defvar text-mode-map nil) ; @r{Create a mode-specific keymap.} | |
313 | |
314 (if text-mode-map | |
315 () ; @r{Do not change the keymap if it is already set up.} | |
316 (setq text-mode-map (make-sparse-keymap)) | |
317 (define-key text-mode-map "\t" 'tab-to-tab-stop) | |
318 (define-key text-mode-map "\es" 'center-line) | |
319 (define-key text-mode-map "\eS" 'center-paragraph)) | |
320 @end group | |
321 @end smallexample | |
322 | |
323 Here is the complete major mode function definition for Text mode: | |
324 | |
325 @smallexample | |
326 @group | |
327 (defun text-mode () | |
328 "Major mode for editing text intended for humans to read. | |
329 Special commands: \\@{text-mode-map@} | |
330 @end group | |
331 @group | |
332 Turning on text-mode runs the hook `text-mode-hook'." | |
333 (interactive) | |
334 (kill-all-local-variables) | |
335 @end group | |
336 @group | |
337 (use-local-map text-mode-map) ; @r{This provides the local keymap.} | |
338 (setq mode-name "Text") ; @r{This name goes into the mode line.} | |
339 (setq major-mode 'text-mode) ; @r{This is how @code{describe-mode}} | |
340 ; @r{finds the doc string to print.} | |
341 (setq local-abbrev-table text-mode-abbrev-table) | |
342 (set-syntax-table text-mode-syntax-table) | |
343 (run-hooks 'text-mode-hook)) ; @r{Finally, this permits the user to} | |
344 ; @r{customize the mode with a hook.} | |
345 @end group | |
346 @end smallexample | |
347 | |
348 @cindex @file{lisp-mode.el} | |
349 The three Lisp modes (Lisp mode, Emacs Lisp mode, and Lisp | |
350 Interaction mode) have more features than Text mode and the code is | |
351 correspondingly more complicated. Here are excerpts from | |
352 @file{lisp-mode.el} that illustrate how these modes are written. | |
353 | |
354 @cindex syntax table example | |
355 @smallexample | |
356 @group | |
357 ;; @r{Create mode-specific table variables.} | |
358 (defvar lisp-mode-syntax-table nil "") | |
359 (defvar emacs-lisp-mode-syntax-table nil "") | |
360 (defvar lisp-mode-abbrev-table nil "") | |
361 @end group | |
362 | |
363 @group | |
364 (if (not emacs-lisp-mode-syntax-table) ; @r{Do not change the table} | |
365 ; @r{if it is already set.} | |
366 (let ((i 0)) | |
367 (setq emacs-lisp-mode-syntax-table (make-syntax-table)) | |
368 @end group | |
369 | |
370 @group | |
371 ;; @r{Set syntax of chars up to 0 to class of chars that are} | |
372 ;; @r{part of symbol names but not words.} | |
373 ;; @r{(The number 0 is @code{48} in the @sc{ASCII} character set.)} | |
374 (while (< i ?0) | |
375 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
376 (setq i (1+ i))) | |
377 @dots{} | |
378 @end group | |
379 @group | |
380 ;; @r{Set the syntax for other characters.} | |
381 (modify-syntax-entry ? " " emacs-lisp-mode-syntax-table) | |
382 (modify-syntax-entry ?\t " " emacs-lisp-mode-syntax-table) | |
383 @dots{} | |
384 @end group | |
385 @group | |
386 (modify-syntax-entry ?\( "() " emacs-lisp-mode-syntax-table) | |
387 (modify-syntax-entry ?\) ")( " emacs-lisp-mode-syntax-table) | |
388 @dots{})) | |
389 ;; @r{Create an abbrev table for lisp-mode.} | |
390 (define-abbrev-table 'lisp-mode-abbrev-table ()) | |
391 @end group | |
392 @end smallexample | |
393 | |
394 Much code is shared among the three Lisp modes. The following | |
395 function sets various variables; it is called by each of the major Lisp | |
396 mode functions: | |
397 | |
398 @smallexample | |
399 @group | |
400 (defun lisp-mode-variables (lisp-syntax) | |
401 ;; @r{The @code{lisp-syntax} argument is @code{nil} in Emacs Lisp mode,} | |
402 ;; @r{and @code{t} in the other two Lisp modes.} | |
403 (cond (lisp-syntax | |
404 (if (not lisp-mode-syntax-table) | |
405 ;; @r{The Emacs Lisp mode syntax table always exists, but} | |
406 ;; @r{the Lisp Mode syntax table is created the first time a} | |
407 ;; @r{mode that needs it is called. This is to save space.} | |
408 @end group | |
409 @group | |
410 (progn (setq lisp-mode-syntax-table | |
411 (copy-syntax-table emacs-lisp-mode-syntax-table)) | |
412 ;; @r{Change some entries for Lisp mode.} | |
413 (modify-syntax-entry ?\| "\" " | |
414 lisp-mode-syntax-table) | |
415 (modify-syntax-entry ?\[ "_ " | |
416 lisp-mode-syntax-table) | |
417 (modify-syntax-entry ?\] "_ " | |
418 lisp-mode-syntax-table))) | |
419 @end group | |
420 @group | |
421 (set-syntax-table lisp-mode-syntax-table))) | |
422 (setq local-abbrev-table lisp-mode-abbrev-table) | |
423 @dots{}) | |
424 @end group | |
425 @end smallexample | |
426 | |
427 Functions such as @code{forward-paragraph} use the value of the | |
428 @code{paragraph-start} variable. Since Lisp code is different from | |
429 ordinary text, the @code{paragraph-start} variable needs to be set | |
430 specially to handle Lisp. Also, comments are indented in a special | |
431 fashion in Lisp and the Lisp modes need their own mode-specific | |
432 @code{comment-indent-function}. The code to set these variables is the | |
433 rest of @code{lisp-mode-variables}. | |
434 | |
435 @smallexample | |
436 @group | |
437 (make-local-variable 'paragraph-start) | |
12098 | 438 ;; @r{Having @samp{^} is not clean, but @code{page-delimiter}} |
439 ;; @r{has them too, and removing those is a pain.} | |
6451 | 440 (setq paragraph-start (concat "^$\\|" page-delimiter)) |
441 @dots{} | |
442 @end group | |
443 @group | |
444 (make-local-variable 'comment-indent-function) | |
445 (setq comment-indent-function 'lisp-comment-indent)) | |
446 @end group | |
447 @end smallexample | |
448 | |
449 Each of the different Lisp modes has a slightly different keymap. For | |
450 example, Lisp mode binds @kbd{C-c C-l} to @code{run-lisp}, but the other | |
451 Lisp modes do not. However, all Lisp modes have some commands in | |
452 common. The following function adds these common commands to a given | |
453 keymap. | |
454 | |
455 @smallexample | |
456 @group | |
457 (defun lisp-mode-commands (map) | |
458 (define-key map "\e\C-q" 'indent-sexp) | |
459 (define-key map "\177" 'backward-delete-char-untabify) | |
460 (define-key map "\t" 'lisp-indent-line)) | |
461 @end group | |
462 @end smallexample | |
463 | |
464 Here is an example of using @code{lisp-mode-commands} to initialize a | |
465 keymap, as part of the code for Emacs Lisp mode. First we declare a | |
466 variable with @code{defvar} to hold the mode-specific keymap. When this | |
467 @code{defvar} executes, it sets the variable to @code{nil} if it was | |
468 void. Then we set up the keymap if the variable is @code{nil}. | |
469 | |
470 This code avoids changing the keymap or the variable if it is already | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
471 set up. This lets the user customize the keymap. |
6451 | 472 |
473 @smallexample | |
474 @group | |
475 (defvar emacs-lisp-mode-map () "") | |
476 (if emacs-lisp-mode-map | |
477 () | |
478 (setq emacs-lisp-mode-map (make-sparse-keymap)) | |
479 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun) | |
480 (lisp-mode-commands emacs-lisp-mode-map)) | |
481 @end group | |
482 @end smallexample | |
483 | |
484 Finally, here is the complete major mode function definition for | |
485 Emacs Lisp mode. | |
486 | |
487 @smallexample | |
488 @group | |
489 (defun emacs-lisp-mode () | |
490 "Major mode for editing Lisp code to run in Emacs. | |
491 Commands: | |
492 Delete converts tabs to spaces as it moves back. | |
493 Blank lines separate paragraphs. Semicolons start comments. | |
494 \\@{emacs-lisp-mode-map@} | |
495 @end group | |
496 @group | |
497 Entry to this mode runs the hook `emacs-lisp-mode-hook'." | |
498 (interactive) | |
499 (kill-all-local-variables) | |
500 (use-local-map emacs-lisp-mode-map) ; @r{This provides the local keymap.} | |
501 (set-syntax-table emacs-lisp-mode-syntax-table) | |
502 @end group | |
503 @group | |
504 (setq major-mode 'emacs-lisp-mode) ; @r{This is how @code{describe-mode}} | |
505 ; @r{finds out what to describe.} | |
506 (setq mode-name "Emacs-Lisp") ; @r{This goes into the mode line.} | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
507 (lisp-mode-variables nil) ; @r{This defines various variables.} |
6451 | 508 (run-hooks 'emacs-lisp-mode-hook)) ; @r{This permits the user to use a} |
509 ; @r{hook to customize the mode.} | |
510 @end group | |
511 @end smallexample | |
512 | |
513 @node Auto Major Mode | |
514 @subsection How Emacs Chooses a Major Mode | |
515 | |
516 Based on information in the file name or in the file itself, Emacs | |
517 automatically selects a major mode for the new buffer when a file is | |
518 visited. | |
519 | |
520 @deffn Command fundamental-mode | |
521 Fundamental mode is a major mode that is not specialized for anything | |
522 in particular. Other major modes are defined in effect by comparison | |
523 with this one---their definitions say what to change, starting from | |
524 Fundamental mode. The @code{fundamental-mode} function does @emph{not} | |
525 run any hooks; you're not supposed to customize it. (If you want Emacs | |
526 to behave differently in Fundamental mode, change the @emph{global} | |
527 state of Emacs.) | |
528 @end deffn | |
529 | |
530 @deffn Command normal-mode &optional find-file | |
12098 | 531 This function establishes the proper major mode and local variable |
6451 | 532 bindings for the current buffer. First it calls @code{set-auto-mode}, |
533 then it runs @code{hack-local-variables} to parse, and bind or | |
534 evaluate as appropriate, any local variables. | |
535 | |
12098 | 536 If the @var{find-file} argument to @code{normal-mode} is |
6451 | 537 non-@code{nil}, @code{normal-mode} assumes that the @code{find-file} |
538 function is calling it. In this case, it may process a local variables | |
12098 | 539 list at the end of the file and in the @samp{-*-} line. The variable |
540 @code{enable-local-variables} controls whether to do so. | |
6451 | 541 |
12098 | 542 If you run @code{normal-mode} interactively, the argument |
6451 | 543 @var{find-file} is normally @code{nil}. In this case, |
544 @code{normal-mode} unconditionally processes any local variables list. | |
545 @xref{File variables, , Local Variables in Files, emacs, The GNU Emacs | |
546 Manual}, for the syntax of the local variables section of a file. | |
547 | |
548 @cindex file mode specification error | |
12098 | 549 @code{normal-mode} uses @code{condition-case} around the call to the |
6451 | 550 major mode function, so errors are caught and reported as a @samp{File |
551 mode specification error}, followed by the original error message. | |
552 @end deffn | |
553 | |
554 @defopt enable-local-variables | |
555 This variable controls processing of local variables lists in files | |
556 being visited. A value of @code{t} means process the local variables | |
557 lists unconditionally; @code{nil} means ignore them; anything else means | |
558 ask the user what to do for each file. The default value is @code{t}. | |
559 @end defopt | |
560 | |
12098 | 561 @defvar ignored-local-variables |
562 This variable holds a list of variables that should not be | |
563 set by a local variables list. Any value specified | |
564 for one of these variables is ignored. | |
565 @end defvar | |
566 | |
567 In addition to this list, any variable whose name has a non-@code{nil} | |
568 @code{risky-local-variable} property is also ignored. | |
569 | |
6451 | 570 @defopt enable-local-eval |
571 This variable controls processing of @samp{Eval:} in local variables | |
572 lists in files being visited. A value of @code{t} means process them | |
573 unconditionally; @code{nil} means ignore them; anything else means ask | |
574 the user what to do for each file. The default value is @code{maybe}. | |
575 @end defopt | |
576 | |
577 @defun set-auto-mode | |
578 @cindex visited file mode | |
579 This function selects the major mode that is appropriate for the | |
580 current buffer. It may base its decision on the value of the @w{@samp{-*-}} | |
12888 | 581 line, on the visited file name (using @code{auto-mode-alist}), on the |
582 @w{@samp{#!}} line (using @code{interpreter-mode-alist}), or on the | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
583 value of a local variable. However, this function does not look for |
6451 | 584 the @samp{mode:} local variable near the end of a file; the |
585 @code{hack-local-variables} function does that. @xref{Choosing Modes, , | |
586 How Major Modes are Chosen, emacs, The GNU Emacs Manual}. | |
587 @end defun | |
588 | |
589 @defopt default-major-mode | |
590 This variable holds the default major mode for new buffers. The | |
591 standard value is @code{fundamental-mode}. | |
592 | |
593 If the value of @code{default-major-mode} is @code{nil}, Emacs uses | |
594 the (previously) current buffer's major mode for the major mode of a new | |
595 buffer. However, if the major mode symbol has a @code{mode-class} | |
596 property with value @code{special}, then it is not used for new buffers; | |
597 Fundamental mode is used instead. The modes that have this property are | |
598 those such as Dired and Rmail that are useful only with text that has | |
599 been specially prepared. | |
600 @end defopt | |
601 | |
12067 | 602 @defun set-buffer-major-mode buffer |
603 This function sets the major mode of @var{buffer} to the value of | |
604 @code{default-major-mode}. If that variable is @code{nil}, it uses | |
605 the current buffer's major mode (if that is suitable). | |
606 | |
607 The low-level primitives for creating buffers do not use this function, | |
12098 | 608 but medium-level commands such as @code{switch-to-buffer} and |
609 @code{find-file-noselect} use it whenever they create buffers. | |
12067 | 610 @end defun |
611 | |
6451 | 612 @defvar initial-major-mode |
613 @cindex @samp{*scratch*} | |
614 The value of this variable determines the major mode of the initial | |
615 @samp{*scratch*} buffer. The value should be a symbol that is a major | |
616 mode command name. The default value is @code{lisp-interaction-mode}. | |
617 @end defvar | |
618 | |
619 @defvar auto-mode-alist | |
620 This variable contains an association list of file name patterns | |
621 (regular expressions; @pxref{Regular Expressions}) and corresponding | |
622 major mode functions. Usually, the file name patterns test for | |
623 suffixes, such as @samp{.el} and @samp{.c}, but this need not be the | |
624 case. An ordinary element of the alist looks like @code{(@var{regexp} . | |
625 @var{mode-function})}. | |
626 | |
627 For example, | |
628 | |
629 @smallexample | |
630 @group | |
631 (("^/tmp/fol/" . text-mode) | |
8505
d3f7cadf8c95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8016
diff
changeset
|
632 ("\\.texinfo\\'" . texinfo-mode) |
d3f7cadf8c95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8016
diff
changeset
|
633 ("\\.texi\\'" . texinfo-mode) |
6451 | 634 @end group |
635 @group | |
8505
d3f7cadf8c95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8016
diff
changeset
|
636 ("\\.el\\'" . emacs-lisp-mode) |
d3f7cadf8c95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8016
diff
changeset
|
637 ("\\.c\\'" . c-mode) |
d3f7cadf8c95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8016
diff
changeset
|
638 ("\\.h\\'" . c-mode) |
6451 | 639 @dots{}) |
640 @end group | |
641 @end smallexample | |
642 | |
643 When you visit a file whose expanded file name (@pxref{File Name | |
644 Expansion}) matches a @var{regexp}, @code{set-auto-mode} calls the | |
645 corresponding @var{mode-function}. This feature enables Emacs to select | |
646 the proper major mode for most files. | |
647 | |
648 If an element of @code{auto-mode-alist} has the form @code{(@var{regexp} | |
649 @var{function} t)}, then after calling @var{function}, Emacs searches | |
650 @code{auto-mode-alist} again for a match against the portion of the file | |
651 name that did not match before. | |
652 | |
653 This match-again feature is useful for uncompression packages: an entry | |
654 of the form @code{("\\.gz\\'" . @var{function})} can uncompress the file | |
655 and then put the uncompressed file in the proper mode according to the | |
656 name sans @samp{.gz}. | |
657 | |
658 Here is an example of how to prepend several pattern pairs to | |
659 @code{auto-mode-alist}. (You might use this sort of expression in your | |
660 @file{.emacs} file.) | |
661 | |
662 @smallexample | |
663 @group | |
664 (setq auto-mode-alist | |
665 (append | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
666 ;; @r{File name starts with a dot.} |
8505
d3f7cadf8c95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8016
diff
changeset
|
667 '(("/\\.[^/]*\\'" . fundamental-mode) |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
668 ;; @r{File name has no dot.} |
8505
d3f7cadf8c95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8016
diff
changeset
|
669 ("[^\\./]*\\'" . fundamental-mode) |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
670 ;; @r{File name ends in @samp{.C}.} |
8505
d3f7cadf8c95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8016
diff
changeset
|
671 ("\\.C\\'" . c++-mode)) |
6451 | 672 auto-mode-alist)) |
673 @end group | |
674 @end smallexample | |
675 @end defvar | |
676 | |
677 @defvar interpreter-mode-alist | |
678 This variable specifes major modes to use for scripts that specify a | |
12888 | 679 command interpreter in an @samp{#!} line. Its value is a list of |
6451 | 680 elements of the form @code{(@var{interpreter} . @var{mode})}; for |
681 example, @code{("perl" . perl-mode)} is one element present by default. | |
682 The element says to use mode @var{mode} if the file specifies | |
683 @var{interpreter}. | |
684 | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
685 This variable is applicable only when the @code{auto-mode-alist} does |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
686 not indicate which major mode to use. |
6451 | 687 @end defvar |
688 | |
689 @defun hack-local-variables &optional force | |
690 This function parses, and binds or evaluates as appropriate, any local | |
691 variables for the current buffer. | |
692 | |
693 The handling of @code{enable-local-variables} documented for | |
694 @code{normal-mode} actually takes place here. The argument @var{force} | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
695 usually comes from the argument @var{find-file} given to |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
696 @code{normal-mode}. |
6451 | 697 @end defun |
698 | |
699 @node Mode Help | |
700 @subsection Getting Help about a Major Mode | |
701 @cindex mode help | |
702 @cindex help for major mode | |
703 @cindex documentation for major mode | |
704 | |
705 The @code{describe-mode} function is used to provide information | |
706 about major modes. It is normally called with @kbd{C-h m}. The | |
707 @code{describe-mode} function uses the value of @code{major-mode}, | |
708 which is why every major mode function needs to set the | |
709 @code{major-mode} variable. | |
710 | |
711 @deffn Command describe-mode | |
712 This function displays the documentation of the current major mode. | |
713 | |
714 The @code{describe-mode} function calls the @code{documentation} | |
715 function using the value of @code{major-mode} as an argument. Thus, it | |
716 displays the documentation string of the major mode function. | |
717 (@xref{Accessing Documentation}.) | |
718 @end deffn | |
719 | |
720 @defvar major-mode | |
721 This variable holds the symbol for the current buffer's major mode. | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
722 This symbol should have a function definition that is the command to |
6451 | 723 switch to that major mode. The @code{describe-mode} function uses the |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
724 documentation string of the function as the documentation of the major |
6451 | 725 mode. |
726 @end defvar | |
727 | |
728 @node Derived Modes | |
729 @subsection Defining Derived Modes | |
730 | |
731 It's often useful to define a new major mode in terms of an existing | |
732 one. An easy way to do this is to use @code{define-derived-mode}. | |
733 | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
734 @defmac define-derived-mode variant parent name docstring body@dots{} |
6451 | 735 This construct defines @var{variant} as a major mode command, using |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
736 @var{name} as the string form of the mode name. |
6451 | 737 |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
738 The new command @var{variant} is defined to call the function |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
739 @var{parent}, then override certain aspects of that parent mode: |
6451 | 740 |
741 @itemize @bullet | |
742 @item | |
743 The new mode has its own keymap, named @code{@var{variant}-map}. | |
744 @code{define-derived-mode} initializes this map to inherit from | |
745 @code{@var{parent}-map}, if it is not already set. | |
746 | |
747 @item | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
748 The new mode has its own syntax table, kept in the variable |
6451 | 749 @code{@var{variant}-syntax-table}. |
750 @code{define-derived-mode} initializes this variable by copying | |
751 @code{@var{parent}-syntax-table}, if it is not already set. | |
752 | |
753 @item | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
754 The new mode has its own abbrev table, kept in the variable |
6451 | 755 @code{@var{variant}-abbrev-table}. |
756 @code{define-derived-mode} initializes this variable by copying | |
757 @code{@var{parent}-abbrev-table}, if it is not already set. | |
758 | |
759 @item | |
760 The new mode has its own mode hook, @code{@var{variant}-hook}, | |
761 which it runs in standard fashion as the very last thing that it does. | |
762 (The new mode also runs the mode hook of @var{parent} as part | |
763 of calling @var{parent}.) | |
764 @end itemize | |
765 | |
766 In addition, you can specify how to override other aspects of | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
767 @var{parent} with @var{body}. The command @var{variant} |
6451 | 768 evaluates the forms in @var{body} after setting up all its usual |
769 overrides, just before running @code{@var{variant}-hook}. | |
770 | |
771 The argument @var{docstring} specifies the documentation string for the | |
772 new mode. If you omit @var{docstring}, @code{define-derived-mode} | |
773 generates a documentation string. | |
774 | |
775 Here is a hypothetical example: | |
776 | |
777 @example | |
778 (define-derived-mode hypertext-mode | |
779 text-mode "Hypertext" | |
780 "Major mode for hypertext. | |
781 \\@{hypertext-mode-map@}" | |
782 (setq case-fold-search nil)) | |
783 | |
784 (define-key hypertext-mode-map | |
785 [down-mouse-3] 'do-hyper-link) | |
786 @end example | |
787 @end defmac | |
788 | |
789 @node Minor Modes | |
790 @section Minor Modes | |
791 @cindex minor mode | |
792 | |
793 A @dfn{minor mode} provides features that users may enable or disable | |
794 independently of the choice of major mode. Minor modes can be enabled | |
795 individually or in combination. Minor modes would be better named | |
796 ``Generally available, optional feature modes'' except that such a name is | |
797 unwieldy. | |
798 | |
799 A minor mode is not usually a modification of single major mode. For | |
800 example, Auto Fill mode may be used in any major mode that permits text | |
801 insertion. To be general, a minor mode must be effectively independent | |
802 of the things major modes do. | |
803 | |
804 A minor mode is often much more difficult to implement than a major | |
805 mode. One reason is that you should be able to activate and deactivate | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
806 minor modes in any order. A minor mode should be able to have its |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
807 desired effect regardless of the major mode and regardless of the other |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
808 minor modes in effect. |
6451 | 809 |
810 Often the biggest problem in implementing a minor mode is finding a | |
811 way to insert the necessary hook into the rest of Emacs. Minor mode | |
12098 | 812 keymaps make this easier than it used to be. |
6451 | 813 |
814 @menu | |
815 * Minor Mode Conventions:: Tips for writing a minor mode. | |
816 * Keymaps and Minor Modes:: How a minor mode can have its own keymap. | |
817 @end menu | |
818 | |
819 @node Minor Mode Conventions | |
820 @subsection Conventions for Writing Minor Modes | |
821 @cindex minor mode conventions | |
822 @cindex conventions for writing minor modes | |
823 | |
824 There are conventions for writing minor modes just as there are for | |
825 major modes. Several of the major mode conventions apply to minor | |
826 modes as well: those regarding the name of the mode initialization | |
827 function, the names of global symbols, and the use of keymaps and | |
828 other tables. | |
829 | |
830 In addition, there are several conventions that are specific to | |
831 minor modes. | |
832 | |
833 @itemize @bullet | |
834 @item | |
835 @cindex mode variable | |
836 Make a variable whose name ends in @samp{-mode} to represent the minor | |
837 mode. Its value should enable or disable the mode (@code{nil} to | |
838 disable; anything else to enable.) We call this the @dfn{mode | |
839 variable}. | |
840 | |
841 This variable is used in conjunction with the @code{minor-mode-alist} to | |
842 display the minor mode name in the mode line. It can also enable | |
843 or disable a minor mode keymap. Individual commands or hooks can also | |
844 check the variable's value. | |
845 | |
846 If you want the minor mode to be enabled separately in each buffer, | |
847 make the variable buffer-local. | |
848 | |
849 @item | |
850 Define a command whose name is the same as the mode variable. | |
851 Its job is to enable and disable the mode by setting the variable. | |
852 | |
853 The command should accept one optional argument. If the argument is | |
854 @code{nil}, it should toggle the mode (turn it on if it is off, and off | |
855 if it is on). Otherwise, it should turn the mode on if the argument is | |
856 a positive integer, a symbol other than @code{nil} or @code{-}, or a | |
857 list whose @sc{car} is such an integer or symbol; it should turn the | |
858 mode off otherwise. | |
859 | |
12098 | 860 Here is an example taken from the definition of @code{transient-mark-mode}. |
861 It shows the use of @code{transient-mark-mode} as a variable that enables or | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
862 disables the mode's behavior, and also shows the proper way to toggle, |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
863 enable or disable the minor mode based on the raw prefix argument value. |
6451 | 864 |
865 @smallexample | |
866 @group | |
12098 | 867 (setq transient-mark-mode |
868 (if (null arg) (not transient-mark-mode) | |
6451 | 869 (> (prefix-numeric-value arg) 0))) |
870 @end group | |
871 @end smallexample | |
872 | |
873 @item | |
874 Add an element to @code{minor-mode-alist} for each minor mode | |
875 (@pxref{Mode Line Variables}). This element should be a list of the | |
876 following form: | |
877 | |
878 @smallexample | |
879 (@var{mode-variable} @var{string}) | |
880 @end smallexample | |
881 | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
882 Here @var{mode-variable} is the variable that controls enabling of the |
6451 | 883 minor mode, and @var{string} is a short string, starting with a space, |
884 to represent the mode in the mode line. These strings must be short so | |
885 that there is room for several of them at once. | |
886 | |
887 When you add an element to @code{minor-mode-alist}, use @code{assq} to | |
888 check for an existing element, to avoid duplication. For example: | |
889 | |
890 @smallexample | |
891 @group | |
892 (or (assq 'leif-mode minor-mode-alist) | |
893 (setq minor-mode-alist | |
894 (cons '(leif-mode " Leif") minor-mode-alist))) | |
895 @end group | |
896 @end smallexample | |
897 @end itemize | |
898 | |
899 @node Keymaps and Minor Modes | |
900 @subsection Keymaps and Minor Modes | |
901 | |
12098 | 902 Each minor mode can have its own keymap, which is active when the mode |
903 is enabled. To set up a keymap for a minor mode, add an element to the | |
904 alist @code{minor-mode-map-alist}. @xref{Active Keymaps}. | |
6451 | 905 |
906 @cindex @code{self-insert-command}, minor modes | |
907 One use of minor mode keymaps is to modify the behavior of certain | |
908 self-inserting characters so that they do something else as well as | |
909 self-insert. In general, this is the only way to do that, since the | |
910 facilities for customizing @code{self-insert-command} are limited to | |
911 special cases (designed for abbrevs and Auto Fill mode). (Do not try | |
912 substituting your own definition of @code{self-insert-command} for the | |
913 standard one. The editor command loop handles this function specially.) | |
914 | |
17278
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
915 The key sequences bound in a minor mode should consist of @kbd{C-c} |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
916 followed by a punctuation character @emph{other than} @kbd{@{}, |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
917 @kbd{@}}, @kbd{<}, @kbd{>}, @kbd{:} or @kbd{;}. (Those few punctuation |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
918 characters are reserved for major modes.) |
8ed8412c1ce7
Document key binding conventions for major modes and minor modes.
Richard M. Stallman <rms@gnu.org>
parents:
16432
diff
changeset
|
919 |
6451 | 920 @node Mode Line Format |
921 @section Mode Line Format | |
922 @cindex mode line | |
923 | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
924 Each Emacs window (aside from minibuffer windows) includes a mode line, |
6451 | 925 which displays status information about the buffer displayed in the |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
926 window. The mode line contains information about the buffer, such as its |
6451 | 927 name, associated file, depth of recursive editing, and the major and |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
928 minor modes. |
6451 | 929 |
930 This section describes how the contents of the mode line are | |
931 controlled. It is in the chapter on modes because much of the | |
932 information displayed in the mode line relates to the enabled major and | |
933 minor modes. | |
934 | |
935 @code{mode-line-format} is a buffer-local variable that holds a | |
936 template used to display the mode line of the current buffer. All | |
12098 | 937 windows for the same buffer use the same @code{mode-line-format} and |
938 their mode lines appear the same (except for scrolling percentages and | |
6451 | 939 line numbers). |
940 | |
941 The mode line of a window is normally updated whenever a different | |
942 buffer is shown in the window, or when the buffer's modified-status | |
943 changes from @code{nil} to @code{t} or vice-versa. If you modify any of | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
944 the variables referenced by @code{mode-line-format} (@pxref{Mode Line |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
945 Variables}), you may want to force an update of the mode line so as to |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
946 display the new information. |
6451 | 947 |
948 @c Emacs 19 feature | |
949 @defun force-mode-line-update | |
950 Force redisplay of the current buffer's mode line. | |
951 @end defun | |
952 | |
953 The mode line is usually displayed in inverse video; see | |
954 @code{mode-line-inverse-video} in @ref{Inverse Video}. | |
955 | |
956 @menu | |
957 * Mode Line Data:: The data structure that controls the mode line. | |
958 * Mode Line Variables:: Variables used in that data structure. | |
959 * %-Constructs:: Putting information into a mode line. | |
960 @end menu | |
961 | |
962 @node Mode Line Data | |
963 @subsection The Data Structure of the Mode Line | |
964 @cindex mode line construct | |
965 | |
966 The mode line contents are controlled by a data structure of lists, | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
967 strings, symbols, and numbers kept in the buffer-local variable |
6451 | 968 @code{mode-line-format}. The data structure is called a @dfn{mode line |
969 construct}, and it is built in recursive fashion out of simpler mode line | |
12067 | 970 constructs. The same data structure is used for constructing |
12098 | 971 frame titles (@pxref{Frame Titles}). |
6451 | 972 |
973 @defvar mode-line-format | |
974 The value of this variable is a mode line construct with overall | |
975 responsibility for the mode line format. The value of this variable | |
976 controls which other variables are used to form the mode line text, and | |
977 where they appear. | |
978 @end defvar | |
979 | |
980 A mode line construct may be as simple as a fixed string of text, but | |
981 it usually specifies how to use other variables to construct the text. | |
982 Many of these variables are themselves defined to have mode line | |
983 constructs as their values. | |
984 | |
985 The default value of @code{mode-line-format} incorporates the values | |
986 of variables such as @code{mode-name} and @code{minor-mode-alist}. | |
987 Because of this, very few modes need to alter @code{mode-line-format}. | |
988 For most purposes, it is sufficient to alter the variables referenced by | |
989 @code{mode-line-format}. | |
990 | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
991 A mode line construct may be a list, a symbol, or a string. If the |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
992 value is a list, each element may be a list, a symbol, or a string. |
6451 | 993 |
994 @table @code | |
995 @cindex percent symbol in mode line | |
996 @item @var{string} | |
997 A string as a mode line construct is displayed verbatim in the mode line | |
12098 | 998 except for @dfn{@code{%}-constructs}. Decimal digits after the @samp{%} |
6451 | 999 specify the field width for space filling on the right (i.e., the data |
1000 is left justified). @xref{%-Constructs}. | |
1001 | |
1002 @item @var{symbol} | |
1003 A symbol as a mode line construct stands for its value. The value of | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1004 @var{symbol} is used as a mode line construct, in place of @var{symbol}. |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1005 However, the symbols @code{t} and @code{nil} are ignored; so is any |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1006 symbol whose value is void. |
6451 | 1007 |
1008 There is one exception: if the value of @var{symbol} is a string, it is | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1009 displayed verbatim: the @code{%}-constructs are not recognized. |
6451 | 1010 |
1011 @item (@var{string} @var{rest}@dots{}) @r{or} (@var{list} @var{rest}@dots{}) | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1012 A list whose first element is a string or list means to process all the |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1013 elements recursively and concatenate the results. This is the most |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1014 common form of mode line construct. |
6451 | 1015 |
1016 @item (@var{symbol} @var{then} @var{else}) | |
1017 A list whose first element is a symbol is a conditional. Its meaning | |
1018 depends on the value of @var{symbol}. If the value is non-@code{nil}, | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1019 the second element, @var{then}, is processed recursively as a mode line |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1020 element. But if the value of @var{symbol} is @code{nil}, the third |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1021 element, @var{else}, is processed recursively. You may omit @var{else}; |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1022 then the mode line element displays nothing if the value of @var{symbol} |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1023 is @code{nil}. |
6451 | 1024 |
1025 @item (@var{width} @var{rest}@dots{}) | |
1026 A list whose first element is an integer specifies truncation or | |
1027 padding of the results of @var{rest}. The remaining elements | |
1028 @var{rest} are processed recursively as mode line constructs and | |
1029 concatenated together. Then the result is space filled (if | |
1030 @var{width} is positive) or truncated (to @minus{}@var{width} columns, | |
1031 if @var{width} is negative) on the right. | |
1032 | |
1033 For example, the usual way to show what percentage of a buffer is above | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1034 the top of the window is to use a list like this: @code{(-3 "%p")}. |
6451 | 1035 @end table |
1036 | |
1037 If you do alter @code{mode-line-format} itself, the new value should | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1038 use the same variables that appear in the default value (@pxref{Mode |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1039 Line Variables}), rather than duplicating their contents or displaying |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1040 the information in another fashion. This way, customizations made by |
12098 | 1041 the user or by Lisp programs (such as @code{display-time} and major |
1042 modes) via changes to those variables remain effective. | |
6451 | 1043 |
1044 @cindex Shell mode @code{mode-line-format} | |
1045 Here is an example of a @code{mode-line-format} that might be | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1046 useful for @code{shell-mode}, since it contains the hostname and default |
6451 | 1047 directory. |
1048 | |
1049 @example | |
1050 @group | |
1051 (setq mode-line-format | |
1052 (list "" | |
1053 'mode-line-modified | |
1054 "%b--" | |
1055 @end group | |
1056 (getenv "HOST") ; @r{One element is not constant.} | |
1057 ":" | |
1058 'default-directory | |
1059 " " | |
1060 'global-mode-string | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1061 " %[(" |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1062 'mode-name |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1063 'mode-line-process |
6451 | 1064 'minor-mode-alist |
1065 "%n" | |
1066 ")%]----" | |
1067 @group | |
12098 | 1068 '(line-number-mode "L%l--") |
6451 | 1069 '(-3 . "%p") |
1070 "-%-")) | |
1071 @end group | |
1072 @end example | |
1073 | |
1074 @node Mode Line Variables | |
1075 @subsection Variables Used in the Mode Line | |
1076 | |
1077 This section describes variables incorporated by the | |
1078 standard value of @code{mode-line-format} into the text of the mode | |
1079 line. There is nothing inherently special about these variables; any | |
1080 other variables could have the same effects on the mode line if | |
1081 @code{mode-line-format} were changed to use them. | |
1082 | |
1083 @defvar mode-line-modified | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1084 This variable holds the value of the mode-line construct that displays |
6451 | 1085 whether the current buffer is modified. |
1086 | |
12098 | 1087 The default value of @code{mode-line-modified} is @code{("--%1*%1+-")}. |
1088 This means that the mode line displays @samp{--**-} if the buffer is | |
1089 modified, @samp{-----} if the buffer is not modified, @samp{--%%-} if | |
1090 the buffer is read only, and @samp{--%*--} if the buffer is read only | |
1091 and modified. | |
6451 | 1092 |
1093 Changing this variable does not force an update of the mode line. | |
1094 @end defvar | |
1095 | |
1096 @defvar mode-line-buffer-identification | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1097 This variable identifies the buffer being displayed in the window. Its |
12098 | 1098 default value is @code{("%F: %17b")}, which means that it usually |
1099 displays @samp{Emacs:} followed by seventeen characters of the buffer | |
1100 name. (In a terminal frame, it displays the frame name instead of | |
1101 @samp{Emacs}; this has the effect of showing the frame number.) You may | |
1102 want to change this in modes such as Rmail that do not behave like a | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1103 ``normal'' Emacs. |
6451 | 1104 @end defvar |
1105 | |
1106 @defvar global-mode-string | |
1107 This variable holds a mode line spec that appears in the mode line by | |
1108 default, just after the buffer name. The command @code{display-time} | |
1109 sets @code{global-mode-string} to refer to the variable | |
1110 @code{display-time-string}, which holds a string containing the time and | |
1111 load information. | |
1112 | |
1113 The @samp{%M} construct substitutes the value of | |
1114 @code{global-mode-string}, but this is obsolete, since the variable is | |
1115 included directly in the mode line. | |
1116 @end defvar | |
1117 | |
1118 @defvar mode-name | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1119 This buffer-local variable holds the ``pretty'' name of the current |
6451 | 1120 buffer's major mode. Each major mode should set this variable so that the |
1121 mode name will appear in the mode line. | |
1122 @end defvar | |
1123 | |
1124 @defvar minor-mode-alist | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1125 This variable holds an association list whose elements specify how the |
6451 | 1126 mode line should indicate that a minor mode is active. Each element of |
1127 the @code{minor-mode-alist} should be a two-element list: | |
1128 | |
1129 @example | |
1130 (@var{minor-mode-variable} @var{mode-line-string}) | |
1131 @end example | |
1132 | |
1133 More generally, @var{mode-line-string} can be any mode line spec. It | |
1134 appears in the mode line when the value of @var{minor-mode-variable} is | |
1135 non-@code{nil}, and not otherwise. These strings should begin with | |
1136 spaces so that they don't run together. Conventionally, the | |
1137 @var{minor-mode-variable} for a specific mode is set to a non-@code{nil} | |
1138 value when that minor mode is activated. | |
1139 | |
1140 The default value of @code{minor-mode-alist} is: | |
1141 | |
1142 @example | |
1143 @group | |
1144 minor-mode-alist | |
12098 | 1145 @result{} ((vc-mode vc-mode) |
1146 (abbrev-mode " Abbrev") | |
1147 (overwrite-mode overwrite-mode) | |
6451 | 1148 (auto-fill-function " Fill") |
12098 | 1149 (defining-kbd-macro " Def") |
1150 (isearch-mode isearch-mode)) | |
6451 | 1151 @end group |
1152 @end example | |
1153 | |
12098 | 1154 @code{minor-mode-alist} is not buffer-local. The variables mentioned |
6451 | 1155 in the alist should be buffer-local if the minor mode can be enabled |
1156 separately in each buffer. | |
1157 @end defvar | |
1158 | |
1159 @defvar mode-line-process | |
1160 This buffer-local variable contains the mode line information on process | |
1161 status in modes used for communicating with subprocesses. It is | |
1162 displayed immediately following the major mode name, with no intervening | |
1163 space. For example, its value in the @samp{*shell*} buffer is | |
1164 @code{(":@: %s")}, which allows the shell to display its status along | |
1165 with the major mode as: @samp{(Shell:@: run)}. Normally this variable | |
1166 is @code{nil}. | |
1167 @end defvar | |
1168 | |
1169 @defvar default-mode-line-format | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1170 This variable holds the default @code{mode-line-format} for buffers |
6451 | 1171 that do not override it. This is the same as @code{(default-value |
1172 'mode-line-format)}. | |
1173 | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1174 The default value of @code{default-mode-line-format} is: |
6451 | 1175 |
1176 @example | |
1177 @group | |
1178 ("" | |
1179 mode-line-modified | |
1180 mode-line-buffer-identification | |
1181 " " | |
1182 global-mode-string | |
1183 " %[(" | |
1184 mode-name | |
1185 @end group | |
1186 @group | |
12098 | 1187 mode-line-process |
6451 | 1188 minor-mode-alist |
1189 "%n" | |
1190 ")%]----" | |
12098 | 1191 (line-number-mode "L%l--") |
6451 | 1192 (-3 . "%p") |
1193 "-%-") | |
1194 @end group | |
1195 @end example | |
1196 @end defvar | |
1197 | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1198 @defvar vc-mode |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1199 The variable @code{vc-mode}, local in each buffer, records whether the |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1200 buffer's visited file is maintained with version control, and, if so, |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1201 which kind. Its value is @code{nil} for no version control, or a string |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1202 that appears in the mode line. |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1203 @end defvar |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1204 |
6451 | 1205 @node %-Constructs |
1206 @subsection @code{%}-Constructs in the Mode Line | |
1207 | |
1208 The following table lists the recognized @code{%}-constructs and what | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1209 they mean. In any construct except @samp{%%}, you can add a decimal |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1210 integer after the @samp{%} to specify how many characters to display. |
6451 | 1211 |
1212 @table @code | |
1213 @item %b | |
1214 The current buffer name, obtained with the @code{buffer-name} function. | |
1215 @xref{Buffer Names}. | |
1216 | |
1217 @item %f | |
1218 The visited file name, obtained with the @code{buffer-file-name} | |
1219 function. @xref{Buffer File Name}. | |
1220 | |
12067 | 1221 @item %F |
1222 The name of the selected frame. | |
1223 | |
1224 @item %c | |
1225 The current column number of point. | |
1226 | |
1227 @item %l | |
1228 The current line number of point. | |
1229 | |
6451 | 1230 @item %* |
1231 @samp{%} if the buffer is read only (see @code{buffer-read-only}); @* | |
1232 @samp{*} if the buffer is modified (see @code{buffer-modified-p}); @* | |
1233 @samp{-} otherwise. @xref{Buffer Modification}. | |
1234 | |
1235 @item %+ | |
12067 | 1236 @samp{*} if the buffer is modified (see @code{buffer-modified-p}); @* |
1237 @samp{%} if the buffer is read only (see @code{buffer-read-only}); @* | |
1238 @samp{-} otherwise. This differs from @samp{%*} only for a modified | |
1239 read-only buffer. @xref{Buffer Modification}. | |
1240 | |
1241 @item %& | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1242 @samp{*} if the buffer is modified, and @samp{-} otherwise. |
6451 | 1243 |
1244 @item %s | |
1245 The status of the subprocess belonging to the current buffer, obtained with | |
1246 @code{process-status}. @xref{Process Information}. | |
1247 | |
12067 | 1248 @item %t |
1249 Whether the visited file is a text file or a binary file. (This is a | |
1250 meaningful distinction only on certain operating systems.) | |
1251 | |
6451 | 1252 @item %p |
12067 | 1253 The percentage of the buffer text above the @strong{top} of window, or |
6451 | 1254 @samp{Top}, @samp{Bottom} or @samp{All}. |
1255 | |
1256 @item %P | |
1257 The percentage of the buffer text that is above the @strong{bottom} of | |
1258 the window (which includes the text visible in the window, as well as | |
1259 the text above the top), plus @samp{Top} if the top of the buffer is | |
1260 visible on screen; or @samp{Bottom} or @samp{All}. | |
1261 | |
1262 @item %n | |
1263 @samp{Narrow} when narrowing is in effect; nothing otherwise (see | |
1264 @code{narrow-to-region} in @ref{Narrowing}). | |
1265 | |
1266 @item %[ | |
1267 An indication of the depth of recursive editing levels (not counting | |
1268 minibuffer levels): one @samp{[} for each editing level. | |
1269 @xref{Recursive Editing}. | |
1270 | |
1271 @item %] | |
1272 One @samp{]} for each recursive editing level (not counting minibuffer | |
1273 levels). | |
1274 | |
1275 @item %% | |
1276 The character @samp{%}---this is how to include a literal @samp{%} in a | |
1277 string in which @code{%}-constructs are allowed. | |
1278 | |
1279 @item %- | |
1280 Dashes sufficient to fill the remainder of the mode line. | |
1281 @end table | |
1282 | |
1283 The following two @code{%}-constructs are still supported, but they are | |
1284 obsolete, since you can get the same results with the variables | |
1285 @code{mode-name} and @code{global-mode-string}. | |
1286 | |
1287 @table @code | |
1288 @item %m | |
1289 The value of @code{mode-name}. | |
1290 | |
1291 @item %M | |
1292 The value of @code{global-mode-string}. Currently, only | |
1293 @code{display-time} modifies the value of @code{global-mode-string}. | |
1294 @end table | |
1295 | |
1296 @node Hooks | |
1297 @section Hooks | |
1298 @cindex hooks | |
1299 | |
1300 A @dfn{hook} is a variable where you can store a function or functions | |
1301 to be called on a particular occasion by an existing program. Emacs | |
1302 provides hooks for the sake of customization. Most often, hooks are set | |
1303 up in the @file{.emacs} file, but Lisp programs can set them also. | |
1304 @xref{Standard Hooks}, for a list of standard hook variables. | |
1305 | |
1306 Most of the hooks in Emacs are @dfn{normal hooks}. These variables | |
16056
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1307 contain lists of functions to be called with no arguments. When the |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1308 hook name ends in @samp{-hook}, that tells you it is normal. We try to |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1309 make all hooks normal, as much as possible, so that you can use them in |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1310 a uniform way. |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1311 |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1312 Every major mode function is supposed to run a normal hook called the |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1313 @dfn{mode hook} as the last step of initialization. This makes it easy |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1314 for a user to customize the behavior of the mode, by overriding the |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1315 local variable assignments already made by the mode. But hooks are used |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1316 in other contexts too. For example, the hook @code{suspend-hook} runs |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1317 just before Emacs suspends itself (@pxref{Suspending Emacs}). |
6451 | 1318 |
1319 The recommended way to add a hook function to a normal hook is by | |
1320 calling @code{add-hook} (see below). The hook functions may be any of | |
1321 the valid kinds of functions that @code{funcall} accepts (@pxref{What Is | |
1322 a Function}). Most normal hook variables are initially void; | |
1323 @code{add-hook} knows how to deal with this. | |
1324 | |
16056
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1325 If the hook variable's name does not end with @samp{-hook}, that |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1326 indicates it is probably an abnormal hook; you should look at its |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1327 documentation to see how to use the hook properly. |
6451 | 1328 |
16056
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1329 If the variable's name ends in @samp{-functions} or @samp{-hooks}, |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1330 then the value is a list of functions, but it is abnormal in that either |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1331 these functions are called with arguments or their values are used in |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1332 some way. You can use @code{add-hook} to add a function to the list, |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1333 but you must take care in writing the function. (A few of these |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1334 variables are actually normal hooks which were named before we |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1335 established the convention of using @samp{-hook} for them.) |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1336 |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1337 If the variable's name ends in @samp{-function}, then its value |
bfe1d6597f08
Explain better about abnormal hooks.
Richard M. Stallman <rms@gnu.org>
parents:
12888
diff
changeset
|
1338 is just a single function, not a list of functions. |
6451 | 1339 |
12098 | 1340 Here's an expression that uses a mode hook to turn on Auto Fill mode |
1341 when in Lisp Interaction mode: | |
6451 | 1342 |
1343 @example | |
1344 (add-hook 'lisp-interaction-mode-hook 'turn-on-auto-fill) | |
1345 @end example | |
1346 | |
1347 The next example shows how to use a hook to customize the way Emacs | |
1348 formats C code. (People often have strong personal preferences for one | |
1349 format or another.) Here the hook function is an anonymous lambda | |
1350 expression. | |
1351 | |
1352 @cindex lambda expression in hook | |
1353 @example | |
1354 @group | |
1355 (add-hook 'c-mode-hook | |
1356 (function (lambda () | |
1357 (setq c-indent-level 4 | |
1358 c-argdecl-indent 0 | |
1359 c-label-offset -4 | |
1360 @end group | |
1361 @group | |
1362 c-continued-statement-indent 0 | |
1363 c-brace-offset 0 | |
1364 comment-column 40)))) | |
1365 | |
1366 (setq c++-mode-hook c-mode-hook) | |
1367 @end group | |
1368 @end example | |
1369 | |
1370 At the appropriate time, Emacs uses the @code{run-hooks} function to | |
12098 | 1371 run particular hooks. This function calls the hook functions that have |
1372 been added with @code{add-hook}. | |
6451 | 1373 |
1374 @defun run-hooks &rest hookvar | |
1375 This function takes one or more hook variable names as arguments, and | |
1376 runs each hook in turn. Each @var{hookvar} argument should be a symbol | |
1377 that is a hook variable. These arguments are processed in the order | |
1378 specified. | |
1379 | |
1380 If a hook variable has a non-@code{nil} value, that value may be a | |
1381 function or a list of functions. If the value is a function (either a | |
1382 lambda expression or a symbol with a function definition), it is | |
1383 called. If it is a list, the elements are called, in order. | |
1384 The hook functions are called with no arguments. | |
1385 | |
12098 | 1386 For example, here's how @code{emacs-lisp-mode} runs its mode hook: |
6451 | 1387 |
1388 @example | |
1389 (run-hooks 'emacs-lisp-mode-hook) | |
1390 @end example | |
1391 @end defun | |
1392 | |
12067 | 1393 @defun add-hook hook function &optional append local |
6451 | 1394 This function is the handy way to add function @var{function} to hook |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1395 variable @var{hook}. The argument @var{function} may be any valid Lisp |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1396 function with the proper number of arguments. For example, |
6451 | 1397 |
1398 @example | |
1399 (add-hook 'text-mode-hook 'my-text-hook-function) | |
1400 @end example | |
1401 | |
1402 @noindent | |
1403 adds @code{my-text-hook-function} to the hook called @code{text-mode-hook}. | |
1404 | |
7253
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1405 You can use @code{add-hook} for abnormal hooks as well as for normal |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1406 hooks. |
6ba87aed7836
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
6451
diff
changeset
|
1407 |
6451 | 1408 It is best to design your hook functions so that the order in which they |
1409 are executed does not matter. Any dependence on the order is ``asking | |
1410 for trouble.'' However, the order is predictable: normally, | |
1411 @var{function} goes at the front of the hook list, so it will be | |
1412 executed first (barring another @code{add-hook} call). | |
1413 | |
1414 If the optional argument @var{append} is non-@code{nil}, the new hook | |
1415 function goes at the end of the hook list and will be executed last. | |
1416 | |
12067 | 1417 If @var{local} is non-@code{nil}, that says to make the new hook |
1418 function local to the current buffer. Before you can do this, you must | |
1419 make the hook itself buffer-local by calling @code{make-local-hook} | |
1420 (@strong{not} @code{make-local-variable}). If the hook itself is not | |
1421 buffer-local, then the value of @var{local} makes no difference---the | |
1422 hook function is always global. | |
6451 | 1423 @end defun |
8929
d7dc9a5b8c70
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8505
diff
changeset
|
1424 |
12067 | 1425 @defun remove-hook hook function &optional local |
1426 This function removes @var{function} from the hook variable @var{hook}. | |
1427 | |
1428 If @var{local} is non-@code{nil}, that says to remove @var{function} | |
1429 from the local hook list instead of from the global hook list. If the | |
1430 hook itself is not buffer-local, then the value of @var{local} makes no | |
1431 difference. | |
1432 @end defun | |
8929
d7dc9a5b8c70
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8505
diff
changeset
|
1433 |
12067 | 1434 @defun make-local-hook hook |
1435 This function makes the hook variable @code{hook} local to the current | |
1436 buffer. When a hook variable is local, it can have local and global | |
1437 hook functions, and @code{run-hooks} runs all of them. | |
8929
d7dc9a5b8c70
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8505
diff
changeset
|
1438 |
12234
2de6f5e4858d
Explain how make-local-hook works.
Richard M. Stallman <rms@gnu.org>
parents:
12098
diff
changeset
|
1439 This function works by making @code{t} an element of the buffer-local |
2de6f5e4858d
Explain how make-local-hook works.
Richard M. Stallman <rms@gnu.org>
parents:
12098
diff
changeset
|
1440 value. That serves as a flag to use the hook functions in the default |
2de6f5e4858d
Explain how make-local-hook works.
Richard M. Stallman <rms@gnu.org>
parents:
12098
diff
changeset
|
1441 value of the hook variable as well as those in the local value. Since |
2de6f5e4858d
Explain how make-local-hook works.
Richard M. Stallman <rms@gnu.org>
parents:
12098
diff
changeset
|
1442 @code{run-hooks} understands this flag, @code{make-local-hook} works |
2de6f5e4858d
Explain how make-local-hook works.
Richard M. Stallman <rms@gnu.org>
parents:
12098
diff
changeset
|
1443 with all normal hooks. It works for only some non-normal hooks---those |
2de6f5e4858d
Explain how make-local-hook works.
Richard M. Stallman <rms@gnu.org>
parents:
12098
diff
changeset
|
1444 whose callers have been updated to understand this meaning of @code{t}. |
2de6f5e4858d
Explain how make-local-hook works.
Richard M. Stallman <rms@gnu.org>
parents:
12098
diff
changeset
|
1445 |
12067 | 1446 Do not use @code{make-local-variable} directly for hook variables; it is |
1447 not sufficient. | |
1448 @end defun |