Merge from emacs-24; up to 2012-04-20T05:47:55Z!eliz@gnu.org
[bpt/emacs.git] / doc / emacs / custom.texi
1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985-1987, 1993-1995, 1997, 2000-2012
3 @c Free Software Foundation, Inc.
4 @c See file emacs.texi for copying conditions.
5 @node Customization
6 @chapter Customization
7 @cindex customization
8
9 This chapter describes some simple methods to customize the behavior
10 of Emacs.
11
12 Apart from the methods described here, see @ref{X Resources} for
13 information about using X resources to customize Emacs, and see
14 @ref{Keyboard Macros} for information about recording and replaying
15 keyboard macros. Making more far-reaching and open-ended changes
16 involves writing Emacs Lisp code; see
17 @iftex
18 @cite{The Emacs Lisp Reference Manual}.
19 @end iftex
20 @ifnottex
21 @ref{Top, Emacs Lisp, Emacs Lisp, elisp, The Emacs Lisp
22 Reference Manual}.
23 @end ifnottex
24
25 @menu
26 * Easy Customization:: Convenient way to browse and change settings.
27 * Variables:: Many Emacs commands examine Emacs variables
28 to decide what to do; by setting variables,
29 you can control their functioning.
30 * Key Bindings:: The keymaps say what command each key runs.
31 By changing them, you can "redefine keys".
32 * Init File:: How to write common customizations in the
33 initialization file.
34 @end menu
35
36 @node Easy Customization
37 @section Easy Customization Interface
38
39 @cindex settings
40 @cindex user option
41 @cindex customizable variable
42 Emacs has many @dfn{settings} which you can change. Most settings
43 are @dfn{customizable variables} (@pxref{Variables}), which are also
44 called @dfn{user options}. There is a huge number of customizable
45 variables, controlling numerous aspects of Emacs behavior; the
46 variables documented in this manual are listed in @ref{Variable
47 Index}. A separate class of settings are the @dfn{faces}, which
48 determine the fonts, colors, and other attributes of text
49 (@pxref{Faces}).
50
51 @findex customize
52 @cindex customization buffer
53 To browse and alter settings (both variables and faces), type
54 @kbd{M-x customize}. This creates a @dfn{customization buffer}, which
55 lets you navigate through a logically organized list of settings, edit
56 and set their values, and save them permanently.
57
58 @menu
59 * Customization Groups:: How settings are classified.
60 * Browsing Custom:: Browsing and searching for settings.
61 * Changing a Variable:: How to edit an option's value and set the option.
62 * Saving Customizations:: Saving customizations for future Emacs sessions.
63 * Face Customization:: How to edit the attributes of a face.
64 * Specific Customization:: Customizing specific settings or groups.
65 * Custom Themes:: Collections of customization settings.
66 * Creating Custom Themes:: How to create a new custom theme.
67 @end menu
68
69 @node Customization Groups
70 @subsection Customization Groups
71 @cindex customization groups
72
73 Customization settings are organized into @dfn{customization
74 groups}. These groups are collected into bigger groups, all the way
75 up to a master group called @code{Emacs}.
76
77 @kbd{M-x customize} creates a customization buffer that shows the
78 top-level @code{Emacs} group. It looks like this, in part:
79
80 @c we want the buffer example to all be on one page, but unfortunately
81 @c that's quite a bit of text, so force all space to the bottom.
82 @c @page
83 @smallexample
84 @group
85 To apply changes, use the Save or Set buttons.
86 For details, see [Saving Customizations] in the [Emacs manual].
87
88 ________________________________________ [ Search ]
89
90 Operate on all settings in this buffer:
91 [ Set for current session ] [ Save for future sessions ]
92 [ Undo edits ] [ Reset to saved ] [ Erase customizations ] [ Exit ]
93
94
95 Emacs group: Customization of the One True Editor.
96 [State]: visible group members are all at standard values.
97 See also [Manual].
98
99 [Editing] : Basic text editing facilities.
100
101 [Convenience] : Convenience features for faster editing.
102
103 @var{more second-level groups}
104 @end group
105 @end smallexample
106
107 @noindent
108 The main part of this buffer shows the @samp{Emacs} customization
109 group, which contains several other groups (@samp{Editing},
110 @samp{Convenience}, etc.). The contents of those groups are not
111 listed here, only one line of documentation each.
112
113 The @dfn{state} of the group indicates whether setting in that group
114 has been edited, set or saved. @xref{Changing a Variable}.
115
116 @cindex editable fields (customization buffer)
117 @cindex buttons (customization buffer)
118 @cindex links (customization buffer)
119 Most of the customization buffer is read-only, but it includes some
120 @dfn{editable fields} that you can edit. For example, at the top of
121 the customization buffer is an editable field for searching for
122 settings (@pxref{Browsing Custom}). There are also @dfn{buttons} and
123 @dfn{links}, which you can activate by either clicking with the mouse,
124 or moving point there and typing @key{RET}. For example, the group
125 names like @samp{[Editing]} are links; activating one of these links
126 brings up the customization buffer for that group.
127
128 @kindex TAB @r{(customization buffer)}
129 @kindex S-TAB @r{(customization buffer)}
130 @findex widget-forward
131 @findex widget-backward
132 In the customizable buffer, you can type @key{TAB}
133 (@code{widget-forward}) to move forward to the next button or editable
134 field. @kbd{S-@key{TAB}} (@code{widget-backward}) moves back to the
135 previous button or editable field.
136
137 @node Browsing Custom
138 @subsection Browsing and Searching for Settings
139 @findex customize-browse
140
141 From the top-level customization buffer created by @kbd{M-x
142 customize}, you can follow the links to the subgroups of the
143 @samp{Emacs} customization group. These subgroups may contain
144 settings for you to customize; they may also contain further subgroups,
145 dealing with yet more specialized subsystems of Emacs. As you
146 navigate the hierarchy of customization groups, you should find some
147 settings that you want to customize.
148
149 If you are interested in customizing a particular setting or
150 customization group, you can go straight there with the commands
151 @kbd{M-x customize-option}, @kbd{M-x customize-face}, or @kbd{M-x
152 customize-group}. @xref{Specific Customization}.
153
154 @vindex custom-search-field
155 If you don't know exactly what groups or settings you want to
156 customize, you can search for them using the editable search field at
157 the top of each customization buffer. Here, you can type in a search
158 term---either one or more words separated by spaces, or a regular
159 expression (@pxref{Regexps}). Then type @key{RET} in the field, or
160 activate the @samp{Search} button next to it, to switch to a
161 customization buffer containing groups and settings that match those
162 terms. Note, however, that this feature only finds groups and
163 settings that are loaded in the current Emacs session.
164
165 If you don't want customization buffers to show the search field,
166 change the variable @code{custom-search-field} to @code{nil}.
167
168 The command @kbd{M-x customize-apropos} is similar to using the
169 search field, except that it reads the search term(s) using the
170 minibuffer. @xref{Specific Customization}.
171
172 @kbd{M-x customize-browse} is another way to browse the available
173 settings. This command creates a special customization buffer which
174 shows only the names of groups and settings, in a structured layout.
175 You can show the contents of a group, in the same buffer, by invoking
176 the @samp{[+]} button next to the group name. When the group contents
177 are shown, the button changes to @samp{[-]}; invoking that hides the
178 group contents again. Each group or setting in this buffer has a link
179 which says @samp{[Group]}, @samp{[Option]} or @samp{[Face]}. Invoking
180 this link creates an ordinary customization buffer showing just that
181 group, option, or face; this is the way to change settings that you
182 find with @kbd{M-x customize-browse}.
183
184 @node Changing a Variable
185 @subsection Changing a Variable
186
187 Here is an example of what a variable, or user option, looks like in
188 the customization buffer:
189
190 @smallexample
191 [Hide] Kill Ring Max: 60
192 [State]: STANDARD.
193 Maximum length of kill ring before oldest elements are thrown away.
194 @end smallexample
195
196 The first line shows that the variable is named
197 @code{kill-ring-max}, formatted as @samp{Kill Ring Max} for easier
198 viewing. Its value is @samp{60}. The button labeled @samp{[Hide]},
199 if activated, hides the variable's value and state; this is useful to
200 avoid cluttering up the customization buffer with very long values
201 (for this reason, variables that have very long values may start out
202 hidden). If you use the @samp{[Hide]} button, it changes to
203 @samp{[Show Value]}, which you can activate to reveal the value and
204 state. On a graphical display, the @samp{[Hide]} and @samp{[Show
205 Value]} buttons are replaced with graphical triangles pointing
206 downwards and rightwards respectively.
207
208 The line after the variable name indicates the @dfn{customization
209 state} of the variable: in this example, @samp{STANDARD} means you
210 have not changed the variable, so its value is the default one. The
211 @samp{[State]} button gives a menu of operations for customizing the
212 variable.
213
214 Below the customization state is the documentation for the variable.
215 This is the same documentation that would be shown by the @kbd{C-h v}
216 command (@pxref{Examining}). If the documentation is more than one
217 line long, only one line may be shown. If so, that line ends with a
218 @samp{[More]} button; activate this to see the full documentation.
219
220 @cindex user options, changing
221 @cindex customizing variables
222 @cindex variables, changing
223 To enter a new value for @samp{Kill Ring Max}, just move point to
224 the value and edit it. For example, type @kbd{M-d} to delete the
225 @samp{60} and type in another number. As you begin to alter the text,
226 the @samp{[State]} line will change:
227
228 @smallexample
229 [State]: EDITED, shown value does not take effect until you
230 set or save it.
231 @end smallexample
232
233 @noindent
234 Editing the value does not make it take effect right away. To do
235 that, you must @dfn{set} the variable by activating the @samp{[State]}
236 button and choosing @samp{Set for Current Session}. Then the
237 variable's state becomes:
238
239 @smallexample
240 [State]: SET for current session only.
241 @end smallexample
242
243 @noindent
244 You don't have to worry about specifying a value that is not valid;
245 the @samp{Set for Current Session} operation checks for validity and
246 will not install an unacceptable value.
247
248 @kindex M-TAB @r{(customization buffer)}
249 @kindex C-M-i @r{(customization buffer)}
250 @findex widget-complete
251 While editing certain kinds of values, such as file names, directory
252 names, and Emacs command names, you can perform completion with
253 @kbd{C-M-i} (@code{widget-complete}), or the equivalent keys
254 @kbd{M-@key{TAB}} or @kbd{@key{ESC} @key{TAB}}. This behaves much
255 like minibuffer completion (@pxref{Completion}).
256
257 Typing @key{RET} on an editable value field moves point forward to
258 the next field or button, like @key{TAB}. You can thus type @key{RET}
259 when you are finished editing a field, to move on to the next button
260 or field. To insert a newline within an editable field, use @kbd{C-o}
261 or @kbd{C-q C-j}.
262
263 For some variables, there is only a fixed set of legitimate values,
264 and you are not allowed to edit the value directly. Instead, a
265 @samp{[Value Menu]} button appears before the value; activating this
266 button presents a choice of values. For a boolean ``on or off''
267 value, the button says @samp{[Toggle]}, and flips the value. After
268 using the @samp{[Value Menu]} or @samp{[Toggle]} button, you must
269 again set the variable to make the chosen value take effect.
270
271 Some variables have values with complex structure. For example, the
272 value of @code{minibuffer-frame-alist} is an association list. Here
273 is how it appears in the customization buffer:
274
275 @smallexample
276 [Hide] Minibuffer Frame Alist:
277 [INS] [DEL] Parameter: width
278 Value: 80
279 [INS] [DEL] Parameter: height
280 Value: 2
281 [INS]
282 [ State ]: STANDARD.
283 Alist of parameters for the initial minibuffer frame. [Hide]
284 @r{[@dots{}more lines of documentation@dots{}]}
285 @end smallexample
286
287 @noindent
288 In this case, each association in the list consists of two items, one
289 labeled @samp{Parameter} and one labeled @samp{Value}; both are
290 editable fields. You can delete an association from the list with the
291 @samp{[DEL]} button next to it. To add an association, use the
292 @samp{[INS]} button at the position where you want to insert it; the
293 very last @samp{[INS]} button inserts at the end of the list.
294
295 @cindex saving a setting
296 @cindex settings, how to save
297 When you set a variable, the new value takes effect only in the
298 current Emacs session. To @dfn{save} the value for future sessions,
299 use the @samp{[State]} button and select the @samp{Save for Future
300 Sessions} operation. @xref{Saving Customizations}.
301
302 You can also restore the variable to its standard value by using the
303 @samp{[State]} button and selecting the @samp{Erase Customization}
304 operation. There are actually four reset operations:
305
306 @table @samp
307 @item Undo Edits
308 If you have modified but not yet set the variable, this restores the
309 text in the customization buffer to match the actual value.
310
311 @item Reset to Saved
312 This restores the value of the variable to the last saved value,
313 and updates the text accordingly.
314
315 @item Erase Customization
316 This sets the variable to its standard value. Any saved value that
317 you have is also eliminated.
318
319 @item Set to Backup Value
320 This sets the variable to a previous value that was set in the
321 customization buffer in this session. If you customize a variable
322 and then reset it, which discards the customized value,
323 you can get the discarded value back again with this operation.
324 @end table
325
326 @cindex comments on customized settings
327 Sometimes it is useful to record a comment about a specific
328 customization. Use the @samp{Add Comment} item from the
329 @samp{[State]} menu to create a field for entering the comment.
330
331 Near the top of the customization buffer are two lines of buttons:
332
333 @smallexample
334 [Set for Current Session] [Save for Future Sessions]
335 [Undo Edits] [Reset to Saved] [Erase Customization] [Exit]
336 @end smallexample
337
338 @noindent
339 Each of the first five buttons performs the stated operation---set,
340 save, reset, etc.---on all the settings in the buffer that could
341 meaningfully be affected. They do not operate on settings that are
342 hidden, nor on subgroups that are hidden or not visible in the buffer.
343
344 @kindex C-c C-c @r{(customization buffer)}
345 @kindex C-x C-c @r{(customization buffer)}
346 @findex Custom-set
347 @findex Custom-save
348 The command @kbd{C-c C-c} (@code{Custom-set}) is equivalent using to
349 the @samp{[Set for Current Session]} button. The command @kbd{C-x
350 C-s} (@code{Custom-save}) is like using the @samp{[Save for Future
351 Sessions]} button.
352
353 @vindex custom-buffer-done-kill
354 The @samp{[Exit]} button switches out of the customization buffer,
355 and buries the buffer at the bottom of the buffer list. To make it
356 kill the customization buffer instead, change the variable
357 @code{custom-buffer-done-kill} to @code{t}.
358
359 @node Saving Customizations
360 @subsection Saving Customizations
361
362 In the customization buffer, you can @dfn{save} a customization
363 setting by choosing the @samp{Save for Future Sessions} choice from
364 its @samp{[State]} button. The @kbd{C-x C-s} (@code{Custom-save})
365 command, or the @samp{[Save for Future Sessions]} button at the top of
366 the customization buffer, saves all applicable settings in the buffer.
367
368 Saving works by writing code to a file, usually your initialization
369 file (@pxref{Init File}). Future Emacs sessions automatically read
370 this file at startup, which sets up the customizations again.
371
372 @vindex custom-file
373 You can choose to save customizations somewhere other than your
374 initialization file. To make this work, you must add a couple of
375 lines of code to your initialization file, to set the variable
376 @code{custom-file} to the name of the desired file, and to load that
377 file. For example:
378
379 @example
380 (setq custom-file "~/.emacs-custom.el")
381 (load custom-file)
382 @end example
383
384 You can even specify different customization files for different
385 Emacs versions, like this:
386
387 @example
388 (cond ((< emacs-major-version 22)
389 ;; @r{Emacs 21 customization.}
390 (setq custom-file "~/.custom-21.el"))
391 ((and (= emacs-major-version 22)
392 (< emacs-minor-version 3))
393 ;; @r{Emacs 22 customization, before version 22.3.}
394 (setq custom-file "~/.custom-22.el"))
395 (t
396 ;; @r{Emacs version 22.3 or later.}
397 (setq custom-file "~/.emacs-custom.el")))
398
399 (load custom-file)
400 @end example
401
402 If Emacs was invoked with the @option{-q} or @option{--no-init-file}
403 options (@pxref{Initial Options}), it will not let you save your
404 customizations in your initialization file. This is because saving
405 customizations from such a session would wipe out all the other
406 customizations you might have on your initialization file.
407
408 @node Face Customization
409 @subsection Customizing Faces
410 @cindex customizing faces
411 @cindex faces, customizing
412 @cindex fonts and faces
413
414 You can customize faces (@pxref{Faces}), which determine how Emacs
415 displays different types of text. Customization groups can contain
416 both variables and faces.
417
418 For example, in programming language modes, source code comments are
419 shown with @code{font-lock-comment-face} (@pxref{Font Lock}). In a
420 customization buffer, that face appears like this:
421
422 @smallexample
423 [Hide] Font Lock Comment Face:[sample]
424 [State] : STANDARD.
425 Font Lock mode face used to highlight comments.
426 [ ] Font Family: --
427 [ ] Font Foundry: --
428 [ ] Width: --
429 [ ] Height: --
430 [ ] Weight: --
431 [ ] Slant: --
432 [ ] Underline: --
433 [ ] Overline: --
434 [ ] Strike-through: --
435 [ ] Box around text: --
436 [ ] Inverse-video: --
437 [X] Foreground: Firebrick [Choose] (sample)
438 [ ] Background: --
439 [ ] Stipple: --
440 [ ] Inherit: --
441 [Hide Unused Attributes]
442 @end smallexample
443
444 @noindent
445 The first three lines show the name, @samp{[State]} button, and
446 documentation for the face. Below that is a list of @dfn{face
447 attributes}. In front of each attribute is a checkbox. A filled
448 checkbox, @samp{[X]}, means that the face specifies a value for this
449 attribute; an empty checkbox, @samp{[ ]}, means that the face does not
450 specify any special value for the attribute. You can activate a
451 checkbox to specify or unspecify its attribute.
452
453 Most faces only specify a few attributes (in the above example,
454 @code{font-lock-comment-face} only specifies the foreground color).
455 Emacs has a special face, @code{default}, whose attributes are all
456 specified; it determines the attributes left unspecified by other
457 faces.
458
459 The @samp{Hide Unused Attributes} button, at the end of the
460 attribute list, hides the unspecified attributes of the face. When
461 attributes are being hidden, the button changes to @samp{[Show All
462 Attributes]}, which reveals the entire attribute list. The
463 customization buffer may start out with unspecified attributes hidden,
464 to avoid cluttering the interface.
465
466 When an attribute is specified, you can change its value in the
467 usual ways.
468
469 Foreground and background colors can be specified using either color
470 names or RGB triplets (@pxref{Colors}). You can also use the
471 @samp{[Choose]} button to switch to a list of color names; select a
472 color with @key{RET} in that buffer to put the color name in the value
473 field.
474
475 Setting, saving and resetting a face work like the same operations for
476 variables (@pxref{Changing a Variable}).
477
478 A face can specify different appearances for different types of
479 displays. For example, a face can make text red on a color display,
480 but use a bold font on a monochrome display. To specify multiple
481 appearances for a face, select @samp{For All Kinds of Displays} in the
482 menu you get from invoking @samp{[State]}.
483
484 @node Specific Customization
485 @subsection Customizing Specific Items
486
487 @table @kbd
488 @item M-x customize-option @key{RET} @var{option} @key{RET}
489 @itemx M-x customize-variable @key{RET} @var{option} @key{RET}
490 Set up a customization buffer for just one user option, @var{option}.
491 @item M-x customize-face @key{RET} @var{face} @key{RET}
492 Set up a customization buffer for just one face, @var{face}.
493 @item M-x customize-group @key{RET} @var{group} @key{RET}
494 Set up a customization buffer for just one group, @var{group}.
495 @item M-x customize-apropos @key{RET} @var{regexp} @key{RET}
496 Set up a customization buffer for all the settings and groups that
497 match @var{regexp}.
498 @item M-x customize-changed @key{RET} @var{version} @key{RET}
499 Set up a customization buffer with all the settings and groups
500 whose meaning has changed since Emacs version @var{version}.
501 @item M-x customize-saved
502 Set up a customization buffer containing all settings that you
503 have saved with customization buffers.
504 @item M-x customize-unsaved
505 Set up a customization buffer containing all settings that you have
506 set but not saved.
507 @end table
508
509 @findex customize-option
510 If you want to customize a particular user option, type @kbd{M-x
511 customize-option}. This reads the variable name, and sets up the
512 customization buffer with just that one user option. When entering
513 the variable name into the minibuffer, completion is available, but
514 only for the names of variables that have been loaded into Emacs.
515
516 @findex customize-face
517 @findex customize-group
518 Likewise, you can customize a specific face using @kbd{M-x
519 customize-face}. You can set up a customization buffer for a specific
520 customization group using @kbd{M-x customize-group}.
521
522 @findex customize-apropos
523 @kbd{M-x customize-apropos} prompts for a search term---either one
524 or more words separated by spaces, or a regular expression---and sets
525 up a customization buffer for all @emph{loaded} settings and groups
526 with matching names. This is like using the search field at the top
527 of the customization buffer (@pxref{Customization Groups}).
528
529 @findex customize-changed
530 When you upgrade to a new Emacs version, you might want to consider
531 customizing new settings, and settings whose meanings or default
532 values have changed. To do this, use @kbd{M-x customize-changed} and
533 specify a previous Emacs version number using the minibuffer. It
534 creates a customization buffer which shows all the settings and groups
535 whose definitions have been changed since the specified version,
536 loading them if necessary.
537
538 @findex customize-saved
539 @findex customize-unsaved
540 If you change settings and then decide the change was a mistake, you
541 can use two commands to revisit your changes. Use @kbd{M-x
542 customize-saved} to customize settings that you have saved. Use
543 @kbd{M-x customize-unsaved} to customize settings that you have set
544 but not saved.
545
546 @node Custom Themes
547 @subsection Custom Themes
548 @cindex custom themes
549
550 @dfn{Custom themes} are collections of settings that can be enabled
551 or disabled as a unit. You can use Custom themes to switch easily
552 between various collections of settings, and to transfer such
553 collections from one computer to another.
554
555 A Custom theme is stored an Emacs Lisp source file. If the name of
556 the Custom theme is @var{name}, the theme file is named
557 @file{@var{name}-theme.el}. @xref{Creating Custom Themes}, for the
558 format of a theme file and how to make one.
559
560 @findex customize-themes
561 @vindex custom-theme-directory
562 @cindex color scheme
563 Type @kbd{M-x customize-themes} to switch to a buffer named
564 @file{*Custom Themes*}, which lists the Custom themes that Emacs knows
565 about. By default, Emacs looks for theme files in two locations: the
566 directory specified by the variable @code{custom-theme-directory}
567 (which defaults to @file{~/.emacs.d/}), and a directory named
568 @file{etc/themes} in your Emacs installation (see the variable
569 @code{data-directory}). The latter contains several Custom themes
570 which are distributed with Emacs, which customize Emacs's faces to fit
571 various color schemes. (Note, however, that Custom themes need not be
572 restricted to this purpose; they can be used to customize variables
573 too).
574
575 @vindex custom-theme-load-path
576 If you want Emacs to look for Custom themes in some other directory,
577 add the directory name to the list variable
578 @code{custom-theme-load-path}. Its default value is
579 @code{(custom-theme-directory t)}; here, the symbol
580 @code{custom-theme-directory} has the special meaning of the value of
581 the variable @code{custom-theme-directory}, while @code{t} stands for
582 the built-in theme directory @file{etc/themes}. The themes listed in
583 the @file{*Custom Themes*} buffer are those found in the directories
584 specified by @code{custom-theme-load-path}.
585
586 @kindex C-x C-s @r{(Custom Themes buffer)}
587 In the @file{*Custom Themes*} buffer, you can activate the checkbox
588 next to a Custom theme to enable or disable the theme for the current
589 Emacs session. When a Custom theme is enabled, all of its settings
590 (variables and faces) take effect in the Emacs session. To apply the
591 choice of theme(s) to future Emacs sessions, type @kbd{C-x C-s}
592 (@code{custom-theme-save}) or use the @samp{[Save Theme Settings]}
593 button.
594
595 @vindex custom-safe-themes
596 When you first enable a Custom theme, Emacs displays the contents of
597 the theme file and asks if you really want to load it. Because
598 loading a Custom theme can execute arbitrary Lisp code, you should
599 only say yes if you know that the theme is safe; in that case, Emacs
600 offers to remember in the future that the theme is safe (this is done
601 by saving the theme file's SHA-256 hash to the variable
602 @code{custom-safe-themes}; if you want to treat all themes as safe,
603 change its value to @code{t}). Themes that come with Emacs (in the
604 @file{etc/themes} directory) are exempt from this check, and are
605 always considered safe.
606
607 @vindex custom-enabled-themes
608 Setting or saving Custom themes actually works by customizing the
609 variable @code{custom-enabled-themes}. The value of this variable is
610 a list of Custom theme names (as Lisp symbols, e.g.@: @code{tango}).
611 Instead of using the @file{*Custom Themes*} buffer to set
612 @code{custom-enabled-themes}, you can customize the variable using the
613 usual customization interface, e.g.@: with @kbd{M-x customize-option}.
614 Note that Custom themes are not allowed to set
615 @code{custom-enabled-themes} themselves.
616
617 Any customizations that you make through the customization buffer
618 take precedence over theme settings. This lets you easily override
619 individual theme settings that you disagree with. If settings from
620 two different themes overlap, the theme occurring earlier in
621 @code{custom-enabled-themes} takes precedence. In the customization
622 buffer, if a setting has been changed from its default by a Custom
623 theme, its @samp{State} display shows @samp{THEMED} instead of
624 @samp{STANDARD}.
625
626 @findex load-theme
627 @findex enable-theme
628 @findex disable-theme
629 You can enable a specific Custom theme in the current Emacs session
630 by typing @kbd{M-x load-theme}. This prompts for a theme name, loads
631 the theme from the theme file, and enables it. If a theme file
632 has been loaded before, you can enable the theme without loading its
633 file by typing @kbd{M-x enable-theme}. To disable a Custom theme,
634 type @kbd{M-x disable-theme}.
635
636 @findex describe-theme
637 To see a description of a Custom theme, type @kbd{?} on its line in
638 the @file{*Custom Themes*} buffer; or type @kbd{M-x describe-theme}
639 anywhere in Emacs and enter the theme name.
640
641 @node Creating Custom Themes
642 @subsection Creating Custom Themes
643 @cindex custom themes, creating
644
645 @findex customize-create-theme
646 You can define a Custom theme using an interface similar to the
647 customization buffer, by typing @kbd{M-x customize-create-theme}.
648 This switches to a buffer named @file{*Custom Theme*}. It also offers
649 to insert some common Emacs faces into the theme (a convenience, since
650 Custom themes are often used to customize faces). If you answer no,
651 the theme will initially contain no settings.
652
653 Near the top of the @file{*Custom Theme*} buffer are editable fields
654 where you can enter the theme's name and description. The name can be
655 anything except @samp{user}. The description is the one that will be
656 shown when you invoke @kbd{M-x describe-theme} for the theme. Its
657 first line should be a brief one-sentence summary; in the buffer made
658 by @kbd{M-x customize-themes}, this sentence is displayed next to the
659 theme name.
660
661 To add a new setting to the theme, use the @samp{[Insert Additional
662 Face]} or @samp{[Insert Additional Variable]} buttons. Each button
663 reads a face or variable name using the minibuffer, with completion,
664 and inserts a customization entry for the face or variable. You can
665 edit the variable values or face attributes in the same way as in a
666 normal customization buffer. To remove a face or variable from the
667 theme, uncheck the checkbox next to its name.
668
669 @vindex custom-theme-directory
670 After specifying the Custom theme's faces and variables, type
671 @kbd{C-x C-s} (@code{custom-theme-write}) or use the buffer's
672 @samp{[Save Theme]} button. This saves the theme file, named
673 @file{@var{name}-theme.el} where @var{name} is the theme name, in the
674 directory named by @code{custom-theme-directory}.
675
676 From the @file{*Custom Theme*} buffer, you can view and edit an
677 existing Custom theme by activating the @samp{[Visit Theme]} button
678 and specifying the theme name. You can also add the settings of
679 another theme into the buffer, using the @samp{[Merge Theme]} button.
680 You can import your non-theme settings into a Custom theme by using
681 the @samp{[Merge Theme]} button and specifying the special theme named
682 @samp{user}.
683
684 A theme file is simply an Emacs Lisp source file, and loading the
685 Custom theme works by loading the Lisp file. Therefore, you can edit
686 a theme file directly instead of using the @file{*Custom Theme*}
687 buffer. @xref{Custom Themes,,, elisp, The Emacs Lisp Reference
688 Manual}, for details.
689
690 @node Variables
691 @section Variables
692 @cindex variable
693
694 A @dfn{variable} is a Lisp symbol which has a value. The symbol's
695 name is also called the @dfn{variable name}. A variable name can
696 contain any characters that can appear in a file, but most variable
697 names consist of ordinary words separated by hyphens.
698
699 The name of the variable serves as a compact description of its
700 role. Most variables also have a @dfn{documentation string}, which
701 describes what the variable's purpose is, what kind of value it should
702 have, and how the value will be used. You can view this documentation
703 using the help command @kbd{C-h v} (@code{describe-variable}).
704 @xref{Examining}.
705
706 Emacs uses many Lisp variables for internal record keeping, but the
707 most interesting variables for a non-programmer user are those meant
708 for users to change---these are called @dfn{customizable variables} or
709 @dfn{user options} (@pxref{Easy Customization}). In the following
710 sections, we will describe other aspects of Emacs variables, such as
711 how to set them outside Customize.
712
713 Emacs Lisp allows any variable (with a few exceptions) to have any
714 kind of value. However, many variables are meaningful only if
715 assigned values of a certain type. For example, only numbers are
716 meaningful values for @code{kill-ring-max}, which specifies the
717 maximum length of the kill ring (@pxref{Earlier Kills}); if you give
718 @code{kill-ring-max} a string value, commands such as @kbd{C-y}
719 (@code{yank}) will signal an error. On the other hand, some variables
720 don't care about type; for instance, if a variable has one effect for
721 @code{nil} values and another effect for ``non-@code{nil}'' values,
722 then any value that is not the symbol @code{nil} induces the second
723 effect, regardless of its type (by convention, we usually use the
724 value @code{t}---a symbol which stands for ``true''---to specify a
725 non-@code{nil} value). If you set a variable using the customization
726 buffer, you need not worry about giving it an invalid type: the
727 customization buffer usually only allows you to enter meaningful
728 values. When in doubt, use @kbd{C-h v} (@code{describe-variable}) to
729 check the variable's documentation string to see kind of value it
730 expects (@pxref{Examining}).
731
732 @menu
733 * Examining:: Examining or setting one variable's value.
734 * Hooks:: Hook variables let you specify programs for parts
735 of Emacs to run on particular occasions.
736 * Locals:: Per-buffer values of variables.
737 * File Variables:: How files can specify variable values.
738 * Directory Variables:: How variable values can be specified by directory.
739 @end menu
740
741 @node Examining
742 @subsection Examining and Setting Variables
743 @cindex setting variables
744
745 @table @kbd
746 @item C-h v @var{var} @key{RET}
747 Display the value and documentation of variable @var{var}
748 (@code{describe-variable}).
749 @item M-x set-variable @key{RET} @var{var} @key{RET} @var{value} @key{RET}
750 Change the value of variable @var{var} to @var{value}.
751 @end table
752
753 To examine the value of a variable, use @kbd{C-h v}
754 (@code{describe-variable}). This reads a variable name using the
755 minibuffer, with completion, and displays both the value and the
756 documentation of the variable. For example,
757
758 @example
759 C-h v fill-column @key{RET}
760 @end example
761
762 @noindent
763 displays something like this:
764
765 @example
766 fill-column is a variable defined in `C source code'.
767 fill-column's value is 70
768 Local in buffer custom.texi; global value is 70
769 Automatically becomes buffer-local when set in any fashion.
770
771 Automatically becomes buffer-local when set in any fashion.
772 This variable is safe as a file local variable if its value
773 satisfies the predicate `integerp'.
774
775 Documentation:
776 *Column beyond which automatic line-wrapping should happen.
777 Interactively, you can set the buffer local value using C-x f.
778
779 You can customize this variable.
780 @end example
781
782 @noindent
783 The line that says ``You can customize the variable'' indicates that
784 this variable is a user option. @kbd{C-h v} is not restricted to user
785 options; it allows non-customizable variables too.
786
787 @findex set-variable
788 The most convenient way to set a specific customizable variable is
789 with @kbd{M-x set-variable}. This reads the variable name with the
790 minibuffer (with completion), and then reads a Lisp expression for the
791 new value using the minibuffer a second time (you can insert the old
792 value into the minibuffer for editing via @kbd{M-n}). For example,
793
794 @example
795 M-x set-variable @key{RET} fill-column @key{RET} 75 @key{RET}
796 @end example
797
798 @noindent
799 sets @code{fill-column} to 75.
800
801 @kbd{M-x set-variable} is limited to customizable variables, but you
802 can set any variable with a Lisp expression like this:
803
804 @example
805 (setq fill-column 75)
806 @end example
807
808 @noindent
809 To execute such an expression, type @kbd{M-:} (@code{eval-expression})
810 and enter the expression in the minibuffer (@pxref{Lisp Eval}).
811 Alternatively, go to the @file{*scratch*} buffer, type in the
812 expression, and then type @kbd{C-j} (@pxref{Lisp Interaction}).
813
814 Setting variables, like all means of customizing Emacs except where
815 otherwise stated, affects only the current Emacs session. The only
816 way to alter the variable in future sessions is to put something in
817 your initialization file (@pxref{Init File}).
818
819 @node Hooks
820 @subsection Hooks
821 @cindex hook
822 @cindex running a hook
823
824 @dfn{Hooks} are an important mechanism for customizing Emacs. A
825 hook is a Lisp variable which holds a list of functions, to be called
826 on some well-defined occasion. (This is called @dfn{running the
827 hook}.) The individual functions in the list are called the @dfn{hook
828 functions} of the hook. For example, the hook @code{kill-emacs-hook}
829 runs just before exiting Emacs (@pxref{Exiting}).
830
831 @cindex normal hook
832 Most hooks are @dfn{normal hooks}. This means that when Emacs runs
833 the hook, it calls each hook function in turn, with no arguments. We
834 have made an effort to keep most hooks normal, so that you can use
835 them in a uniform way. Every variable whose name ends in @samp{-hook}
836 is a normal hook.
837
838 @cindex abnormal hook
839 A few hooks are @dfn{abnormal hooks}. Their names end in
840 @samp{-hooks} or @samp{-functions}, instead of @samp{-hook}. What
841 makes these hooks abnormal is the way its functions are
842 called---perhaps they are given arguments, or perhaps the values they
843 return are used in some way. For example,
844 @code{find-file-not-found-functions} is abnormal because as soon as
845 one hook function returns a non-@code{nil} value, the rest are not
846 called at all (@pxref{Visiting}). The documentation of each abnormal
847 hook variable explains how its functions are used.
848
849 @findex add-hook
850 You can set a hook variable with @code{setq} like any other Lisp
851 variable, but the recommended way to add a function to a hook (either
852 normal or abnormal) is to use @code{add-hook}, as shown by the
853 following examples. @xref{Hooks,,, elisp, The Emacs Lisp Reference
854 Manual}, for details.
855
856 Most major modes run one or more @dfn{mode hooks} as the last step
857 of initialization. Mode hooks are a convenient way to customize the
858 behavior of individual modes; they are always normal. For example,
859 here's how to set up a hook to turn on Auto Fill mode in Text mode and
860 other modes based on Text mode:
861
862 @example
863 (add-hook 'text-mode-hook 'auto-fill-mode)
864 @end example
865
866 @noindent
867 This works by calling @code{auto-fill-mode}, which enables the minor
868 mode when no argument is supplied (@pxref{Minor Modes}). Next,
869 suppose you don't want Auto Fill mode turned on in La@TeX{} mode,
870 which is one of the modes based on Text mode. You can do this with
871 the following additional line:
872
873 @example
874 (add-hook 'latex-mode-hook (lambda () (auto-fill-mode -1)))
875 @end example
876
877 @noindent
878 Here we have used the special macro @code{lambda} to construct an
879 anonymous function (@pxref{Lambda Expressions,,, elisp, The Emacs Lisp
880 Reference Manual}), which calls @code{auto-fill-mode} with an argument
881 of @code{-1} to disable the minor mode. Because La@TeX{} mode runs
882 @code{latex-mode-hook} after running @code{text-mode-hook}, the result
883 leaves Auto Fill mode disabled.
884
885 Here is a more complex example, showing how to use a hook to
886 customize the indentation of C code:
887
888 @example
889 @group
890 (setq my-c-style
891 '((c-comment-only-line-offset . 4)
892 @end group
893 @group
894 (c-cleanup-list . (scope-operator
895 empty-defun-braces
896 defun-close-semi))))
897 @end group
898
899 @group
900 (add-hook 'c-mode-common-hook
901 (lambda () (c-add-style "my-style" my-c-style t)))
902 @end group
903 @end example
904
905 @cindex Prog mode
906 @cindex program editing
907 Major mode hooks also apply to other major modes @dfn{derived} from
908 the original mode (@pxref{Derived Modes,,, elisp, The Emacs Lisp
909 Reference Manual}). For instance, HTML mode is derived from Text mode
910 (@pxref{HTML Mode}); when HTML mode is enabled, it runs
911 @code{text-mode-hook} before running @code{html-mode-hook}. This
912 provides a convenient way to use a single hook to affect several
913 related modes. In particular, if you want to apply a hook function to
914 any programming language mode, add it to @code{prog-mode-hook}; Prog
915 mode is a major mode that does little else than to let other major
916 modes inherit from it, exactly for this purpose.
917
918 It is best to design your hook functions so that the order in which
919 they are executed does not matter. Any dependence on the order is
920 asking for trouble. However, the order is predictable: the hook
921 functions are executed in the order they appear in the hook.
922
923 @findex remove-hook
924 If you play with adding various different versions of a hook
925 function by calling @code{add-hook} over and over, remember that all
926 the versions you added will remain in the hook variable together. You
927 can clear out individual functions by calling @code{remove-hook}, or
928 do @code{(setq @var{hook-variable} nil)} to remove everything.
929
930 @cindex buffer-local hooks
931 If the hook variable is buffer-local, the buffer-local variable will
932 be used instead of the global variable. However, if the buffer-local
933 variable contains the element @code{t}, the global hook variable will
934 be run as well.
935
936 @node Locals
937 @subsection Local Variables
938
939 @table @kbd
940 @item M-x make-local-variable @key{RET} @var{var} @key{RET}
941 Make variable @var{var} have a local value in the current buffer.
942 @item M-x kill-local-variable @key{RET} @var{var} @key{RET}
943 Make variable @var{var} use its global value in the current buffer.
944 @item M-x make-variable-buffer-local @key{RET} @var{var} @key{RET}
945 Mark variable @var{var} so that setting it will make it local to the
946 buffer that is current at that time.
947 @end table
948
949 @cindex local variables
950 Almost any variable can be made @dfn{local} to a specific Emacs
951 buffer. This means that its value in that buffer is independent of its
952 value in other buffers. A few variables are always local in every
953 buffer. Every other Emacs variable has a @dfn{global} value which is in
954 effect in all buffers that have not made the variable local.
955
956 @findex make-local-variable
957 @kbd{M-x make-local-variable} reads the name of a variable and makes
958 it local to the current buffer. Changing its value subsequently in
959 this buffer will not affect others, and changes in its global value
960 will not affect this buffer.
961
962 @findex make-variable-buffer-local
963 @cindex per-buffer variables
964 @kbd{M-x make-variable-buffer-local} marks a variable so it will
965 become local automatically whenever it is set. More precisely, once a
966 variable has been marked in this way, the usual ways of setting the
967 variable automatically do @code{make-local-variable} first. We call
968 such variables @dfn{per-buffer} variables. Many variables in Emacs
969 are normally per-buffer; the variable's document string tells you when
970 this is so. A per-buffer variable's global value is normally never
971 effective in any buffer, but it still has a meaning: it is the initial
972 value of the variable for each new buffer.
973
974 Major modes (@pxref{Major Modes}) always make variables local to the
975 buffer before setting the variables. This is why changing major modes
976 in one buffer has no effect on other buffers. Minor modes also work
977 by setting variables---normally, each minor mode has one controlling
978 variable which is non-@code{nil} when the mode is enabled
979 (@pxref{Minor Modes}). For many minor modes, the controlling variable
980 is per buffer, and thus always buffer-local. Otherwise, you can make
981 it local in a specific buffer like any other variable.
982
983 A few variables cannot be local to a buffer because they are always
984 local to each display instead (@pxref{Multiple Displays}). If you try to
985 make one of these variables buffer-local, you'll get an error message.
986
987 @findex kill-local-variable
988 @kbd{M-x kill-local-variable} makes a specified variable cease to be
989 local to the current buffer. The global value of the variable
990 henceforth is in effect in this buffer. Setting the major mode kills
991 all the local variables of the buffer except for a few variables
992 specially marked as @dfn{permanent locals}.
993
994 @findex setq-default
995 To set the global value of a variable, regardless of whether the
996 variable has a local value in the current buffer, you can use the Lisp
997 construct @code{setq-default}. This construct is used just like
998 @code{setq}, but it sets variables' global values instead of their local
999 values (if any). When the current buffer does have a local value, the
1000 new global value may not be visible until you switch to another buffer.
1001 Here is an example:
1002
1003 @example
1004 (setq-default fill-column 75)
1005 @end example
1006
1007 @noindent
1008 @code{setq-default} is the only way to set the global value of a variable
1009 that has been marked with @code{make-variable-buffer-local}.
1010
1011 @findex default-value
1012 Lisp programs can use @code{default-value} to look at a variable's
1013 default value. This function takes a symbol as argument and returns its
1014 default value. The argument is evaluated; usually you must quote it
1015 explicitly. For example, here's how to obtain the default value of
1016 @code{fill-column}:
1017
1018 @example
1019 (default-value 'fill-column)
1020 @end example
1021
1022 @node File Variables
1023 @subsection Local Variables in Files
1024 @cindex local variables in files
1025 @cindex file local variables
1026
1027 A file can specify local variable values to use when editing the
1028 file with Emacs. Visiting the file checks for local variable
1029 specifications; it automatically makes these variables local to the
1030 buffer, and sets them to the values specified in the file.
1031
1032 @menu
1033 * Specifying File Variables:: Specifying file local variables.
1034 * Safe File Variables:: Making sure file local variables are safe.
1035 @end menu
1036
1037 @node Specifying File Variables
1038 @subsubsection Specifying File Variables
1039
1040 There are two ways to specify file local variable values: in the first
1041 line, or with a local variables list. Here's how to specify them in the
1042 first line:
1043
1044 @example
1045 -*- mode: @var{modename}; @var{var}: @var{value}; @dots{} -*-
1046 @end example
1047
1048 @noindent
1049 You can specify any number of variable/value pairs in this way, each
1050 pair with a colon and semicolon. The special variable/value pair
1051 @code{mode: @var{modename};}, if present, specifies a major mode. The
1052 @var{value}s are used literally, and not evaluated.
1053
1054 @findex add-file-local-variable-prop-line
1055 @findex delete-file-local-variable-prop-line
1056 @findex copy-dir-locals-to-file-locals-prop-line
1057 You can use the command @kbd{M-x add-file-local-variable-prop-line}
1058 instead of adding entries by hand. It prompts for
1059 a variable and value, and adds them to the first line in the
1060 appropriate way. @kbd{M-x delete-file-local-variable-prop-line}
1061 prompts for a variable, and deletes its entry from the line. @kbd{M-x
1062 copy-dir-locals-to-file-locals-prop-line} copies directory-local
1063 variables to the first line (@pxref{Directory Variables}).
1064
1065 Here is an example first line that specifies Lisp mode and sets two
1066 variables with numeric values:
1067
1068 @smallexample
1069 ;; -*- mode: Lisp; fill-column: 75; comment-column: 50; -*-
1070 @end smallexample
1071
1072 @noindent
1073 Aside from @code{mode}, other keywords that have special meanings as
1074 file variables are @code{coding}, @code{unibyte}, and @code{eval}.
1075 These are described below.
1076
1077 @cindex shell scripts, and local file variables
1078 @cindex man pages, and local file variables
1079 In shell scripts, the first line is used to identify the script
1080 interpreter, so you cannot put any local variables there. To
1081 accommodate this, Emacs looks for local variable specifications in the
1082 @emph{second} line if the first line specifies an interpreter. The
1083 same is true for man pages which start with the magic string
1084 @samp{'\"} to specify a list of troff preprocessors (not all do,
1085 however).
1086
1087 Apart from using a @samp{-*-} line, you can define file local
1088 variables using a @dfn{local variables list} near the end of the file.
1089 The start of the local variables list should be no more than 3000
1090 characters from the end of the file, and must be on the last page if
1091 the file is divided into pages.
1092
1093 If a file has both a local variables list and a @samp{-*-} line,
1094 Emacs processes @emph{everything} in the @samp{-*-} line first, and
1095 @emph{everything} in the local variables list afterward. The exception
1096 to this is a major mode specification. Emacs applies this first,
1097 wherever it appears, since most major modes kill all local variables as
1098 part of their initialization.
1099
1100 A local variables list starts with a line containing the string
1101 @samp{Local Variables:}, and ends with a line containing the string
1102 @samp{End:}. In between come the variable names and values, one set
1103 per line, like this:
1104
1105 @example
1106 /* Local Variables: */
1107 /* mode: c */
1108 /* comment-column: 0 */
1109 /* End: */
1110 @end example
1111
1112 @noindent
1113 In this example, each line starts with the prefix @samp{/*} and ends
1114 with the suffix @samp{*/}. Emacs recognizes the prefix and suffix by
1115 finding them surrounding the magic string @samp{Local Variables:}, on
1116 the first line of the list; it then automatically discards them from
1117 the other lines of the list. The usual reason for using a prefix
1118 and/or suffix is to embed the local variables list in a comment, so it
1119 won't confuse other programs that the file is intended for. The
1120 example above is for the C programming language, where comments start
1121 with @samp{/*} and end with @samp{*/}.
1122
1123 @findex add-file-local-variable
1124 @findex delete-file-local-variable
1125 @findex copy-dir-locals-to-file-locals
1126 Instead of typing in the local variables list directly, you can use
1127 the command @kbd{M-x add-file-local-variable}. This prompts for a
1128 variable and value, and adds them to the list, adding the @samp{Local
1129 Variables:} string and start and end markers as necessary. The
1130 command @kbd{M-x delete-file-local-variable} deletes a variable from
1131 the list. @kbd{M-x copy-dir-locals-to-file-locals} copies
1132 directory-local variables to the list (@pxref{Directory Variables}).
1133
1134 As with the @samp{-*-} line, the variables in a local variables list
1135 are used literally, and are not evaluated first. If you want to split
1136 a long string value across multiple lines of the file, you can use
1137 backslash-newline, which is ignored in Lisp string constants; you
1138 should put the prefix and suffix on each line, even lines that start
1139 or end within the string, as they will be stripped off when processing
1140 the list. Here is an example:
1141
1142 @example
1143 # Local Variables:
1144 # compile-command: "cc foo.c -Dfoo=bar -Dhack=whatever \
1145 # -Dmumble=blaah"
1146 # End:
1147 @end example
1148
1149 Some ``variable names'' have special meanings in a local variables
1150 list:
1151
1152 @itemize
1153 @item
1154 @code{mode} enables the specified major mode.
1155
1156 @item
1157 @code{eval} evaluates the specified Lisp expression (the value
1158 returned by that expression is ignored).
1159
1160 @item
1161 @code{coding} specifies the coding system for character code
1162 conversion of this file. @xref{Coding Systems}.
1163
1164 @item
1165 @code{unibyte} says to load or compile a file of Emacs Lisp in unibyte
1166 mode, if the value is @code{t}. @xref{Disabling Multibyte}.
1167 @end itemize
1168
1169 @noindent
1170 These four keywords are not really variables; setting them in any
1171 other context has no special meaning.
1172
1173 Do not use the @code{mode} keyword for minor modes. To enable or
1174 disable a minor mode in a local variables list, use the @code{eval}
1175 keyword with a Lisp expression that runs the mode command
1176 (@pxref{Minor Modes}). For example, the following local variables
1177 list enables Eldoc mode (@pxref{Lisp Doc}) by calling
1178 @code{eldoc-mode} with no argument (calling it with an argument of 1
1179 would do the same), and disables Font Lock mode (@pxref{Font Lock}) by
1180 calling @code{font-lock-mode} with an argument of -1.
1181
1182 @example
1183 ;; Local Variables:
1184 ;; eval: (eldoc-mode)
1185 ;; eval: (font-lock-mode -1)
1186 ;; End:
1187 @end example
1188
1189 @noindent
1190 Note, however, that it is often a mistake to specify minor modes this
1191 way. Minor modes represent individual user preferences, and it may be
1192 inappropriate to impose your preferences on another user who might
1193 edit the file. If you wish to automatically enable or disable a minor
1194 mode in a situation-dependent way, it is often better to do it in a
1195 major mode hook (@pxref{Hooks}).
1196
1197 Use the command @kbd{M-x normal-mode} to reset the local variables
1198 and major mode of a buffer according to the file name and contents,
1199 including the local variables list if any. @xref{Choosing Modes}.
1200
1201 @node Safe File Variables
1202 @subsubsection Safety of File Variables
1203
1204 File-local variables can be dangerous; when you visit someone else's
1205 file, there's no telling what its local variables list could do to
1206 your Emacs. Improper values of the @code{eval} ``variable'', and
1207 other variables such as @code{load-path}, could execute Lisp code you
1208 didn't intend to run.
1209
1210 Therefore, whenever Emacs encounters file local variable values that
1211 are not known to be safe, it displays the file's entire local
1212 variables list, and asks you for confirmation before setting them.
1213 You can type @kbd{y} or @key{SPC} to put the local variables list into
1214 effect, or @kbd{n} to ignore it. When Emacs is run in batch mode
1215 (@pxref{Initial Options}), it can't really ask you, so it assumes the
1216 answer @kbd{n}.
1217
1218 Emacs normally recognizes certain variable/value pairs as safe.
1219 For instance, it is safe to give @code{comment-column} or
1220 @code{fill-column} any integer value. If a file specifies only
1221 known-safe variable/value pairs, Emacs does not ask for confirmation
1222 before setting them. Otherwise, you can tell Emacs to record all the
1223 variable/value pairs in this file as safe, by typing @kbd{!} at the
1224 confirmation prompt. When Emacs encounters these variable/value pairs
1225 subsequently, in the same file or others, it will assume they are
1226 safe.
1227
1228 @vindex safe-local-variable-values
1229 @cindex risky variable
1230 Some variables, such as @code{load-path}, are considered
1231 particularly @dfn{risky}: there is seldom any reason to specify them
1232 as local variables, and changing them can be dangerous. If a file
1233 contains only risky local variables, Emacs neither offers nor accepts
1234 @kbd{!} as input at the confirmation prompt. If some of the local
1235 variables in a file are risky, and some are only potentially unsafe, you
1236 can enter @kbd{!} at the prompt. It applies all the variables, but only
1237 marks the non-risky ones as safe for the future. If you really want to
1238 record safe values for risky variables, do it directly by customizing
1239 @samp{safe-local-variable-values} (@pxref{Easy Customization}).
1240
1241 @vindex enable-local-variables
1242 The variable @code{enable-local-variables} allows you to change the
1243 way Emacs processes local variables. Its default value is @code{t},
1244 which specifies the behavior described above. If it is @code{nil},
1245 Emacs simply ignores all file local variables. @code{:safe} means use
1246 only the safe values and ignore the rest. Any other value says to
1247 query you about each file that has local variables, without trying to
1248 determine whether the values are known to be safe.
1249
1250 @vindex enable-local-eval
1251 @vindex safe-local-eval-forms
1252 The variable @code{enable-local-eval} controls whether Emacs
1253 processes @code{eval} variables. The three possibilities for the
1254 variable's value are @code{t}, @code{nil}, and anything else, just as
1255 for @code{enable-local-variables}. The default is @code{maybe}, which
1256 is neither @code{t} nor @code{nil}, so normally Emacs does ask for
1257 confirmation about processing @code{eval} variables.
1258
1259 As an exception, Emacs never asks for confirmation to evaluate any
1260 @code{eval} form if that form occurs within the variable
1261 @code{safe-local-eval-forms}.
1262
1263 @node Directory Variables
1264 @subsection Per-Directory Local Variables
1265 @cindex local variables, for all files in a directory
1266 @cindex directory-local variables
1267 @cindex per-directory local variables
1268
1269 Sometimes, you may wish to define the same set of local variables to
1270 all the files in a certain directory and its subdirectories, such as
1271 the directory tree of a large software project. This can be
1272 accomplished with @dfn{directory-local variables}.
1273
1274 @cindex @file{.dir-locals.el} file
1275 The usual way to define directory-local variables is to put a file
1276 named @file{.dir-locals.el}@footnote{ On MS-DOS, the name of this file
1277 should be @file{_dir-locals.el}, due to limitations of the DOS
1278 filesystems. If the filesystem is limited to 8+3 file names, the name
1279 of the file will be truncated by the OS to @file{_dir-loc.el}. } in a
1280 directory. Whenever Emacs visits any file in that directory or any of
1281 its subdirectories, it will apply the directory-local variables
1282 specified in @file{.dir-locals.el}, as though they had been defined as
1283 file-local variables for that file (@pxref{File Variables}). Emacs
1284 searches for @file{.dir-locals.el} starting in the directory of the
1285 visited file, and moving up the directory tree. To avoid slowdown,
1286 this search is skipped for remote files.
1287
1288 The @file{.dir-locals.el} file should hold a specially-constructed
1289 list, which maps major mode names (symbols) to alists
1290 (@pxref{Association Lists,,, elisp, The Emacs Lisp Reference Manual}).
1291 Each alist entry consists of a variable name and the directory-local
1292 value to assign to that variable, when the specified major mode is
1293 enabled. Instead of a mode name, you can specify @samp{nil}, which
1294 means that the alist applies to any mode; or you can specify a
1295 subdirectory name (a string), in which case the alist applies to all
1296 files in that subdirectory.
1297
1298 Here's an example of a @file{.dir-locals.el} file:
1299
1300 @example
1301 ((nil . ((indent-tabs-mode . t)
1302 (fill-column . 80)))
1303 (c-mode . ((c-file-style . "BSD")))
1304 (subdirs . nil)))
1305 ("src/imported"
1306 . ((nil . ((change-log-default-name
1307 . "ChangeLog.local"))))))
1308 @end example
1309
1310 @noindent
1311 This sets @samp{indent-tabs-mode} and @code{fill-column} for any file
1312 in the directory tree, and the indentation style for any C source
1313 file. The special @code{subdirs} element is not a variable, but a
1314 special keyword which indicates that the C mode settings are only to
1315 be applied in the current directory, not in any subdirectories.
1316 Finally, it specifies a different @file{ChangeLog} file name for any
1317 file in the @file{src/imported} subdirectory.
1318
1319 @findex add-dir-local-variable
1320 @findex delete-dir-local-variable
1321 @findex copy-file-locals-to-dir-locals
1322 Instead of editing the @file{.dir-locals.el} file by hand, you can
1323 use the command @kbd{M-x add-dir-local-variable}. This prompts for a
1324 mode or subdirectory name, and for variable and value, and adds the
1325 entry defining the directory-local variable. @kbd{M-x
1326 delete-dir-local-variable} deletes an entry. @kbd{M-x
1327 copy-file-locals-to-dir-locals} copies the file-local variables in the
1328 current file into @file{.dir-locals.el}.
1329
1330 @findex dir-locals-set-class-variables
1331 @findex dir-locals-set-directory-class
1332 Another method of specifying directory-local variables is to define
1333 a group of variables/value pairs in a @dfn{directory class}, using the
1334 @code{dir-locals-set-class-variables} function; then, tell Emacs which
1335 directories correspond to the class by using the
1336 @code{dir-locals-set-directory-class} function. These function calls
1337 normally go in your initialization file (@pxref{Init File}). This
1338 method is useful when you can't put @file{.dir-locals.el} in a
1339 directory for some reason. For example, you could apply settings to
1340 an unwritable directory this way:
1341
1342 @example
1343 (dir-locals-set-class-variables 'unwritable-directory
1344 '((nil . ((some-useful-setting . value)))))
1345
1346 (dir-locals-set-directory-class
1347 "/usr/include/" 'unwritable-directory)
1348 @end example
1349
1350 If a variable has both a directory-local and file-local value
1351 specified, the file-local value takes effect. Unsafe directory-local
1352 variables are handled in the same way as unsafe file-local variables
1353 (@pxref{Safe File Variables}).
1354
1355 Directory-local variables also take effect in certain buffers that
1356 do not visit a file directly but perform work within a directory, such
1357 as Dired buffers (@pxref{Dired}).
1358
1359 @node Key Bindings
1360 @section Customizing Key Bindings
1361 @cindex key bindings
1362
1363 This section describes @dfn{key bindings}, which map keys to
1364 commands, and @dfn{keymaps}, which record key bindings. It also
1365 explains how to customize key bindings, which is done by editing your
1366 init file (@pxref{Init Rebinding}).
1367
1368 @menu
1369 * Keymaps:: Generalities. The global keymap.
1370 * Prefix Keymaps:: Keymaps for prefix keys.
1371 * Local Keymaps:: Major and minor modes have their own keymaps.
1372 * Minibuffer Maps:: The minibuffer uses its own local keymaps.
1373 * Rebinding:: How to redefine one key's meaning conveniently.
1374 * Init Rebinding:: Rebinding keys with your initialization file.
1375 * Modifier Keys:: Using modifier keys in key bindings.
1376 * Function Keys:: Rebinding terminal function keys.
1377 * Named ASCII Chars:: Distinguishing @key{TAB} from @kbd{C-i}, and so on.
1378 * Mouse Buttons:: Rebinding mouse buttons in Emacs.
1379 * Disabling:: Disabling a command means confirmation is required
1380 before it can be executed. This is done to protect
1381 beginners from surprises.
1382 @end menu
1383
1384 @node Keymaps
1385 @subsection Keymaps
1386 @cindex keymap
1387
1388 As described in @ref{Commands}, each Emacs command is a Lisp
1389 function whose definition provides for interactive use. Like every
1390 Lisp function, a command has a function name, which usually consists
1391 of lower-case letters and hyphens.
1392
1393 A @dfn{key sequence} (@dfn{key}, for short) is a sequence of
1394 @dfn{input events} that have a meaning as a unit. Input events
1395 include characters, function keys and mouse buttons---all the inputs
1396 that you can send to the computer. A key sequence gets its meaning
1397 from its @dfn{binding}, which says what command it runs.
1398
1399 The bindings between key sequences and command functions are
1400 recorded in data structures called @dfn{keymaps}. Emacs has many of
1401 these, each used on particular occasions.
1402
1403 @cindex global keymap
1404 The @dfn{global} keymap is the most important keymap because it is
1405 always in effect. The global keymap defines keys for Fundamental mode
1406 (@pxref{Major Modes}); most of these definitions are common to most or
1407 all major modes. Each major or minor mode can have its own keymap
1408 which overrides the global definitions of some keys.
1409
1410 For example, a self-inserting character such as @kbd{g} is
1411 self-inserting because the global keymap binds it to the command
1412 @code{self-insert-command}. The standard Emacs editing characters
1413 such as @kbd{C-a} also get their standard meanings from the global
1414 keymap. Commands to rebind keys, such as @kbd{M-x global-set-key},
1415 work by storing the new binding in the proper place in the global map
1416 (@pxref{Rebinding}).
1417
1418 @cindex function key
1419 Most modern keyboards have function keys as well as character keys.
1420 Function keys send input events just as character keys do, and keymaps
1421 can have bindings for them. Key sequences can mix function keys and
1422 characters. For example, if your keyboard has a @key{Home} function
1423 key, Emacs can recognize key sequences like @kbd{C-x @key{Home}}. You
1424 can even mix mouse events with keyboard events, such as
1425 @kbd{S-down-mouse-1}.
1426
1427 On text terminals, typing a function key actually sends the computer
1428 a sequence of characters; the precise details of the sequence depends
1429 on the function key and on the terminal type. (Often the sequence
1430 starts with @kbd{@key{ESC} [}.) If Emacs understands your terminal
1431 type properly, it automatically handles such sequences as single input
1432 events.
1433
1434 @node Prefix Keymaps
1435 @subsection Prefix Keymaps
1436
1437 Internally, Emacs records only single events in each keymap.
1438 Interpreting a key sequence of multiple events involves a chain of
1439 keymaps: the first keymap gives a definition for the first event,
1440 which is another keymap, which is used to look up the second event in
1441 the sequence, and so on. Thus, a prefix key such as @kbd{C-x} or
1442 @key{ESC} has its own keymap, which holds the definition for the event
1443 that immediately follows that prefix.
1444
1445 The definition of a prefix key is usually the keymap to use for
1446 looking up the following event. The definition can also be a Lisp
1447 symbol whose function definition is the following keymap; the effect is
1448 the same, but it provides a command name for the prefix key that can be
1449 used as a description of what the prefix key is for. Thus, the binding
1450 of @kbd{C-x} is the symbol @code{Control-X-prefix}, whose function
1451 definition is the keymap for @kbd{C-x} commands. The definitions of
1452 @kbd{C-c}, @kbd{C-x}, @kbd{C-h} and @key{ESC} as prefix keys appear in
1453 the global map, so these prefix keys are always available.
1454
1455 Aside from ordinary prefix keys, there is a fictitious ``prefix key''
1456 which represents the menu bar; see @ref{Menu Bar,,,elisp, The Emacs Lisp
1457 Reference Manual}, for special information about menu bar key bindings.
1458 Mouse button events that invoke pop-up menus are also prefix keys; see
1459 @ref{Menu Keymaps,,,elisp, The Emacs Lisp Reference Manual}, for more
1460 details.
1461
1462 Some prefix keymaps are stored in variables with names:
1463
1464 @itemize @bullet
1465 @item
1466 @vindex ctl-x-map
1467 @code{ctl-x-map} is the variable name for the map used for characters that
1468 follow @kbd{C-x}.
1469 @item
1470 @vindex help-map
1471 @code{help-map} is for characters that follow @kbd{C-h}.
1472 @item
1473 @vindex esc-map
1474 @code{esc-map} is for characters that follow @key{ESC}. Thus, all Meta
1475 characters are actually defined by this map.
1476 @item
1477 @vindex ctl-x-4-map
1478 @code{ctl-x-4-map} is for characters that follow @kbd{C-x 4}.
1479 @item
1480 @vindex mode-specific-map
1481 @code{mode-specific-map} is for characters that follow @kbd{C-c}.
1482 @end itemize
1483
1484 @node Local Keymaps
1485 @subsection Local Keymaps
1486
1487 @cindex local keymap
1488 @cindex minor mode keymap
1489 So far, we have explained the ins and outs of the global map. Major
1490 modes customize Emacs by providing their own key bindings in
1491 @dfn{local keymaps}. For example, C mode overrides @key{TAB} to make
1492 it indent the current line for C code. Minor modes can also have
1493 local keymaps; whenever a minor mode is in effect, the definitions in
1494 its keymap override both the major mode's local keymap and the global
1495 keymap. In addition, portions of text in the buffer can specify their
1496 own keymaps, which override all other keymaps.
1497
1498 A local keymap can redefine a key as a prefix key by defining it as
1499 a prefix keymap. If the key is also defined globally as a prefix, its
1500 local and global definitions (both keymaps) effectively combine: both
1501 definitions are used to look up the event that follows the prefix key.
1502 For example, if a local keymap defines @kbd{C-c} as a prefix keymap,
1503 and that keymap defines @kbd{C-z} as a command, this provides a local
1504 meaning for @kbd{C-c C-z}. This does not affect other sequences that
1505 start with @kbd{C-c}; if those sequences don't have their own local
1506 bindings, their global bindings remain in effect.
1507
1508 Another way to think of this is that Emacs handles a multi-event key
1509 sequence by looking in several keymaps, one by one, for a binding of the
1510 whole key sequence. First it checks the minor mode keymaps for minor
1511 modes that are enabled, then it checks the major mode's keymap, and then
1512 it checks the global keymap. This is not precisely how key lookup
1513 works, but it's good enough for understanding the results in ordinary
1514 circumstances.
1515
1516 @node Minibuffer Maps
1517 @subsection Minibuffer Keymaps
1518
1519 @cindex minibuffer keymaps
1520 @vindex minibuffer-local-map
1521 @vindex minibuffer-local-ns-map
1522 @vindex minibuffer-local-completion-map
1523 @vindex minibuffer-local-must-match-map
1524 @vindex minibuffer-local-filename-completion-map
1525 @vindex minibuffer-local-filename-must-match-map
1526 The minibuffer has its own set of local keymaps; they contain various
1527 completion and exit commands.
1528
1529 @itemize @bullet
1530 @item
1531 @code{minibuffer-local-map} is used for ordinary input (no completion).
1532 @item
1533 @code{minibuffer-local-ns-map} is similar, except that @key{SPC} exits
1534 just like @key{RET}.
1535 @item
1536 @code{minibuffer-local-completion-map} is for permissive completion.
1537 @item
1538 @code{minibuffer-local-must-match-map} is for strict completion and
1539 for cautious completion.
1540 @item
1541 @code{minibuffer-local-filename-completion-map} and
1542 @code{minibuffer-local-filename-must-match-map} are like the two
1543 previous ones, but they are specifically for file name completion.
1544 They do not bind @key{SPC}.
1545 @end itemize
1546
1547 @node Rebinding
1548 @subsection Changing Key Bindings Interactively
1549 @cindex key rebinding, this session
1550 @cindex redefining keys, this session
1551 @cindex binding keys
1552
1553 The way to redefine an Emacs key is to change its entry in a keymap.
1554 You can change the global keymap, in which case the change is
1555 effective in all major modes (except those that have their own
1556 overriding local bindings for the same key). Or you can change a
1557 local keymap, which affects all buffers using the same major mode.
1558
1559 In this section, we describe how to rebind keys for the present
1560 Emacs session. @xref{Init Rebinding}, for a description of how to
1561 make key rebindings affect future Emacs sessions.
1562
1563 @findex global-set-key
1564 @findex local-set-key
1565 @findex global-unset-key
1566 @findex local-unset-key
1567 @table @kbd
1568 @item M-x global-set-key @key{RET} @var{key} @var{cmd} @key{RET}
1569 Define @var{key} globally to run @var{cmd}.
1570 @item M-x local-set-key @key{RET} @var{key} @var{cmd} @key{RET}
1571 Define @var{key} locally (in the major mode now in effect) to run
1572 @var{cmd}.
1573 @item M-x global-unset-key @key{RET} @var{key}
1574 Make @var{key} undefined in the global map.
1575 @item M-x local-unset-key @key{RET} @var{key}
1576 Make @var{key} undefined locally (in the major mode now in effect).
1577 @end table
1578
1579 For example, the following binds @kbd{C-z} to the @code{shell}
1580 command (@pxref{Interactive Shell}), replacing the normal global
1581 definition of @kbd{C-z}:
1582
1583 @example
1584 M-x global-set-key @key{RET} C-z shell @key{RET}
1585 @end example
1586
1587 @noindent
1588 The @code{global-set-key} command reads the command name after the
1589 key. After you press the key, a message like this appears so that you
1590 can confirm that you are binding the key you want:
1591
1592 @example
1593 Set key C-z to command:
1594 @end example
1595
1596 You can redefine function keys and mouse events in the same way; just
1597 type the function key or click the mouse when it's time to specify the
1598 key to rebind.
1599
1600 You can rebind a key that contains more than one event in the same
1601 way. Emacs keeps reading the key to rebind until it is a complete key
1602 (that is, not a prefix key). Thus, if you type @kbd{C-f} for
1603 @var{key}, that's the end; it enters the minibuffer immediately to
1604 read @var{cmd}. But if you type @kbd{C-x}, since that's a prefix, it
1605 reads another character; if that is @kbd{4}, another prefix character,
1606 it reads one more character, and so on. For example,
1607
1608 @example
1609 M-x global-set-key @key{RET} C-x 4 $ spell-other-window @key{RET}
1610 @end example
1611
1612 @noindent
1613 redefines @kbd{C-x 4 $} to run the (fictitious) command
1614 @code{spell-other-window}.
1615
1616 You can remove the global definition of a key with
1617 @code{global-unset-key}. This makes the key @dfn{undefined}; if you
1618 type it, Emacs will just beep. Similarly, @code{local-unset-key} makes
1619 a key undefined in the current major mode keymap, which makes the global
1620 definition (or lack of one) come back into effect in that major mode.
1621
1622 If you have redefined (or undefined) a key and you subsequently wish
1623 to retract the change, undefining the key will not do the job---you need
1624 to redefine the key with its standard definition. To find the name of
1625 the standard definition of a key, go to a Fundamental mode buffer in a
1626 fresh Emacs and use @kbd{C-h c}. The documentation of keys in this
1627 manual also lists their command names.
1628
1629 If you want to prevent yourself from invoking a command by mistake, it
1630 is better to disable the command than to undefine the key. A disabled
1631 command is less work to invoke when you really want to.
1632 @xref{Disabling}.
1633
1634 @node Init Rebinding
1635 @subsection Rebinding Keys in Your Init File
1636 @cindex rebinding major mode keys
1637 @c This node is referenced in the tutorial. When renaming or deleting
1638 @c it, the tutorial needs to be adjusted. (TUTORIAL.de)
1639
1640 If you have a set of key bindings that you like to use all the time,
1641 you can specify them in your initialization file by writing Lisp code.
1642 @xref{Init File}, for a description of the initialization file.
1643
1644 @findex kbd
1645 There are several ways to write a key binding using Lisp. The
1646 simplest is to use the @code{kbd} macro, which converts a textual
1647 representation of a key sequence---similar to how we have written key
1648 sequences in this manual---into a form that can be passed as an
1649 argument to @code{global-set-key}. For example, here's how to bind
1650 @kbd{C-z} to the @code{shell} command (@pxref{Interactive Shell}):
1651
1652 @example
1653 (global-set-key (kbd "C-z") 'shell)
1654 @end example
1655
1656 @noindent
1657 The single-quote before the command name, @code{shell}, marks it as a
1658 constant symbol rather than a variable. If you omit the quote, Emacs
1659 would try to evaluate @code{shell} as a variable. This probably
1660 causes an error; it certainly isn't what you want.
1661
1662 Here are some additional examples, including binding function keys
1663 and mouse events:
1664
1665 @example
1666 (global-set-key (kbd "C-c y") 'clipboard-yank)
1667 (global-set-key (kbd "C-M-q") 'query-replace)
1668 (global-set-key (kbd "<f5>") 'flyspell-mode)
1669 (global-set-key (kbd "C-<f5>") 'linum-mode)
1670 (global-set-key (kbd "C-<right>") 'forward-sentence)
1671 (global-set-key (kbd "<mouse-2>") 'mouse-save-then-kill)
1672 @end example
1673
1674 Instead of using the @code{kbd} macro, you can use a Lisp string or
1675 vector to specify the key sequence. Using a string is simpler, but
1676 only works for @acronym{ASCII} characters and Meta-modified
1677 @acronym{ASCII} characters. For example, here's how to bind @kbd{C-x
1678 M-l} to @code{make-symbolic-link} (@pxref{Misc File Ops}):
1679
1680 @example
1681 (global-set-key "\C-x\M-l" 'make-symbolic-link)
1682 @end example
1683
1684 To put @key{TAB}, @key{RET}, @key{ESC}, or @key{DEL} in the string,
1685 use the Emacs Lisp escape sequences @samp{\t}, @samp{\r}, @samp{\e},
1686 and @samp{\d} respectively. Here is an example which binds @kbd{C-x
1687 @key{TAB}} to @code{indent-rigidly} (@pxref{Indentation}):
1688
1689 @example
1690 (global-set-key "\C-x\t" 'indent-rigidly)
1691 @end example
1692
1693 When the key sequence includes function keys or mouse button events,
1694 or non-@acronym{ASCII} characters such as @code{C-=} or @code{H-a},
1695 you can use a vector to specify the key sequence. Each element in the
1696 vector stands for an input event; the elements are separated by spaces
1697 and surrounded by a pair of square brackets. If a vector element is a
1698 character, write it as a Lisp character constant: @samp{?} followed by
1699 the character as it would appear in a string. Function keys are
1700 represented by symbols (@pxref{Function Keys}); simply write the
1701 symbol's name, with no other delimiters or punctuation. Here are some
1702 examples:
1703
1704 @example
1705 (global-set-key [?\C-=] 'make-symbolic-link)
1706 (global-set-key [?\M-\C-=] 'make-symbolic-link)
1707 (global-set-key [?\H-a] 'make-symbolic-link)
1708 (global-set-key [f7] 'make-symbolic-link)
1709 (global-set-key [C-mouse-1] 'make-symbolic-link)
1710 @end example
1711
1712 @noindent
1713 You can use a vector for the simple cases too:
1714
1715 @example
1716 (global-set-key [?\C-z ?\M-l] 'make-symbolic-link)
1717 @end example
1718
1719 Language and coding systems may cause problems with key bindings for
1720 non-@acronym{ASCII} characters. @xref{Init Non-ASCII}.
1721
1722 As described in @ref{Local Keymaps}, major modes and minor modes can
1723 define local keymaps. These keymaps are constructed when the mode is
1724 used for the first time in a session. If you wish to change one of
1725 these keymaps, you must use the @dfn{mode hook} (@pxref{Hooks}).
1726
1727 @findex define-key
1728 For example, Texinfo mode runs the hook @code{texinfo-mode-hook}.
1729 Here's how you can use the hook to add local bindings for @kbd{C-c n}
1730 and @kbd{C-c p} in Texinfo mode:
1731
1732 @example
1733 (add-hook 'texinfo-mode-hook
1734 '(lambda ()
1735 (define-key texinfo-mode-map "\C-cp"
1736 'backward-paragraph)
1737 (define-key texinfo-mode-map "\C-cn"
1738 'forward-paragraph)))
1739 @end example
1740
1741 @node Modifier Keys
1742 @subsection Modifier Keys
1743 @cindex modifier keys
1744
1745 The default key bindings in Emacs are set up so that modified
1746 alphabetical characters are case-insensitive. In other words,
1747 @kbd{C-A} does the same thing as @kbd{C-a}, and @kbd{M-A} does the
1748 same thing as @kbd{M-a}. This concerns only alphabetical characters,
1749 and does not apply to ``shifted'' versions of other keys; for
1750 instance, @kbd{C-@@} is not the same as @kbd{C-2}.
1751
1752 A @key{Control}-modified alphabetical character is always considered
1753 case-insensitive: Emacs always treats @kbd{C-A} as @kbd{C-a},
1754 @kbd{C-B} as @kbd{C-b}, and so forth. The reason for this is
1755 historical.
1756
1757 For all other modifiers, you can make the modified alphabetical
1758 characters case-sensitive when you customize Emacs. For instance, you
1759 could make @kbd{M-a} and @kbd{M-A} run different commands.
1760
1761 Although only the @key{Control} and @key{Meta} modifier keys are
1762 commonly used, Emacs supports three other modifier keys. These are
1763 called @key{Super}, @key{Hyper} and @key{Alt}. Few terminals provide
1764 ways to use these modifiers; the key labeled @key{Alt} on most
1765 keyboards usually issues the @key{Meta} modifier, not @key{Alt}. The
1766 standard key bindings in Emacs do not include any characters with
1767 these modifiers. However, you can customize Emacs to assign meanings
1768 to them. The modifier bits are labeled as @samp{s-}, @samp{H-} and
1769 @samp{A-} respectively.
1770
1771 Even if your keyboard lacks these additional modifier keys, you can
1772 enter it using @kbd{C-x @@}: @kbd{C-x @@ h} adds the ``hyper'' flag to
1773 the next character, @kbd{C-x @@ s} adds the ``super'' flag, and
1774 @kbd{C-x @@ a} adds the ``alt'' flag. For instance, @kbd{C-x @@ h
1775 C-a} is a way to enter @kbd{Hyper-Control-a}. (Unfortunately, there
1776 is no way to add two modifiers by using @kbd{C-x @@} twice for the
1777 same character, because the first one goes to work on the @kbd{C-x}.)
1778
1779 @node Function Keys
1780 @subsection Rebinding Function Keys
1781
1782 Key sequences can contain function keys as well as ordinary
1783 characters. Just as Lisp characters (actually integers) represent
1784 keyboard characters, Lisp symbols represent function keys. If the
1785 function key has a word as its label, then that word is also the name of
1786 the corresponding Lisp symbol. Here are the conventional Lisp names for
1787 common function keys:
1788
1789 @table @asis
1790 @item @code{left}, @code{up}, @code{right}, @code{down}
1791 Cursor arrow keys.
1792
1793 @item @code{begin}, @code{end}, @code{home}, @code{next}, @code{prior}
1794 Other cursor repositioning keys.
1795
1796 @item @code{select}, @code{print}, @code{execute}, @code{backtab}
1797 @itemx @code{insert}, @code{undo}, @code{redo}, @code{clearline}
1798 @itemx @code{insertline}, @code{deleteline}, @code{insertchar}, @code{deletechar}
1799 Miscellaneous function keys.
1800
1801 @item @code{f1}, @code{f2}, @dots{} @code{f35}
1802 Numbered function keys (across the top of the keyboard).
1803
1804 @item @code{kp-add}, @code{kp-subtract}, @code{kp-multiply}, @code{kp-divide}
1805 @itemx @code{kp-backtab}, @code{kp-space}, @code{kp-tab}, @code{kp-enter}
1806 @itemx @code{kp-separator}, @code{kp-decimal}, @code{kp-equal}
1807 Keypad keys (to the right of the regular keyboard), with names or punctuation.
1808
1809 @item @code{kp-0}, @code{kp-1}, @dots{} @code{kp-9}
1810 Keypad keys with digits.
1811
1812 @item @code{kp-f1}, @code{kp-f2}, @code{kp-f3}, @code{kp-f4}
1813 Keypad PF keys.
1814 @end table
1815
1816 These names are conventional, but some systems (especially when using
1817 X) may use different names. To make certain what symbol is used for a
1818 given function key on your terminal, type @kbd{C-h c} followed by that
1819 key.
1820
1821 @xref{Init Rebinding}, for examples of binding function keys.
1822
1823 @cindex keypad
1824 Many keyboards have a ``numeric keypad'' on the right hand side.
1825 The numeric keys in the keypad double up as cursor motion keys,
1826 toggled by a key labeled @samp{Num Lock}. By default, Emacs
1827 translates these keys to the corresponding keys in the main keyboard.
1828 For example, when @samp{Num Lock} is on, the key labeled @samp{8} on
1829 the numeric keypad produces @code{kp-8}, which is translated to
1830 @kbd{8}; when @samp{Num Lock} is off, the same key produces
1831 @code{kp-up}, which is translated to @key{UP}. If you rebind a key
1832 such as @kbd{8} or @key{UP}, it affects the equivalent keypad key too.
1833 However, if you rebind a @samp{kp-} key directly, that won't affect
1834 its non-keypad equivalent. Note that the modified keys are not
1835 translated: for instance, if you hold down the @key{META} key while
1836 pressing the @samp{8} key on the numeric keypad, that generates
1837 @kbd{M-@key{kp-8}}.
1838
1839 Emacs provides a convenient method for binding the numeric keypad
1840 keys, using the variables @code{keypad-setup},
1841 @code{keypad-numlock-setup}, @code{keypad-shifted-setup}, and
1842 @code{keypad-numlock-shifted-setup}. These can be found in the
1843 @samp{keyboard} customization group (@pxref{Easy Customization}). You
1844 can rebind the keys to perform other tasks, such as issuing numeric
1845 prefix arguments.
1846
1847 @node Named ASCII Chars
1848 @subsection Named @acronym{ASCII} Control Characters
1849
1850 @key{TAB}, @key{RET}, @key{BS}, @key{LFD}, @key{ESC} and @key{DEL}
1851 started out as names for certain @acronym{ASCII} control characters,
1852 used so often that they have special keys of their own. For instance,
1853 @key{TAB} was another name for @kbd{C-i}. Later, users found it
1854 convenient to distinguish in Emacs between these keys and the ``same''
1855 control characters typed with the @key{CTRL} key. Therefore, on most
1856 modern terminals, they are no longer the same: @key{TAB} is different
1857 from @kbd{C-i}.
1858
1859 Emacs can distinguish these two kinds of input if the keyboard does.
1860 It treats the ``special'' keys as function keys named @code{tab},
1861 @code{return}, @code{backspace}, @code{linefeed}, @code{escape}, and
1862 @code{delete}. These function keys translate automatically into the
1863 corresponding @acronym{ASCII} characters @emph{if} they have no
1864 bindings of their own. As a result, neither users nor Lisp programs
1865 need to pay attention to the distinction unless they care to.
1866
1867 If you do not want to distinguish between (for example) @key{TAB} and
1868 @kbd{C-i}, make just one binding, for the @acronym{ASCII} character @key{TAB}
1869 (octal code 011). If you do want to distinguish, make one binding for
1870 this @acronym{ASCII} character, and another for the ``function key'' @code{tab}.
1871
1872 With an ordinary @acronym{ASCII} terminal, there is no way to distinguish
1873 between @key{TAB} and @kbd{C-i} (and likewise for other such pairs),
1874 because the terminal sends the same character in both cases.
1875
1876 @node Mouse Buttons
1877 @subsection Rebinding Mouse Buttons
1878 @cindex mouse button events
1879 @cindex rebinding mouse buttons
1880 @cindex click events
1881 @cindex drag events
1882 @cindex down events
1883 @cindex button down events
1884
1885 Emacs uses Lisp symbols to designate mouse buttons, too. The ordinary
1886 mouse events in Emacs are @dfn{click} events; these happen when you
1887 press a button and release it without moving the mouse. You can also
1888 get @dfn{drag} events, when you move the mouse while holding the button
1889 down. Drag events happen when you finally let go of the button.
1890
1891 The symbols for basic click events are @code{mouse-1} for the leftmost
1892 button, @code{mouse-2} for the next, and so on. Here is how you can
1893 redefine the second mouse button to split the current window:
1894
1895 @example
1896 (global-set-key [mouse-2] 'split-window-below)
1897 @end example
1898
1899 The symbols for drag events are similar, but have the prefix
1900 @samp{drag-} before the word @samp{mouse}. For example, dragging the
1901 first button generates a @code{drag-mouse-1} event.
1902
1903 You can also define bindings for events that occur when a mouse button
1904 is pressed down. These events start with @samp{down-} instead of
1905 @samp{drag-}. Such events are generated only if they have key bindings.
1906 When you get a button-down event, a corresponding click or drag event
1907 will always follow.
1908
1909 @cindex double clicks
1910 @cindex triple clicks
1911 If you wish, you can distinguish single, double, and triple clicks. A
1912 double click means clicking a mouse button twice in approximately the
1913 same place. The first click generates an ordinary click event. The
1914 second click, if it comes soon enough, generates a double-click event
1915 instead. The event type for a double-click event starts with
1916 @samp{double-}: for example, @code{double-mouse-3}.
1917
1918 This means that you can give a special meaning to the second click at
1919 the same place, but it must act on the assumption that the ordinary
1920 single click definition has run when the first click was received.
1921
1922 This constrains what you can do with double clicks, but user interface
1923 designers say that this constraint ought to be followed in any case. A
1924 double click should do something similar to the single click, only
1925 ``more so''. The command for the double-click event should perform the
1926 extra work for the double click.
1927
1928 If a double-click event has no binding, it changes to the
1929 corresponding single-click event. Thus, if you don't define a
1930 particular double click specially, it executes the single-click command
1931 twice.
1932
1933 Emacs also supports triple-click events whose names start with
1934 @samp{triple-}. Emacs does not distinguish quadruple clicks as event
1935 types; clicks beyond the third generate additional triple-click events.
1936 However, the full number of clicks is recorded in the event list, so
1937 if you know Emacs Lisp you can distinguish if you really want to
1938 (@pxref{Click Events,,, elisp, The Emacs Lisp Reference Manual}).
1939 We don't recommend distinct meanings for more than three clicks, but
1940 sometimes it is useful for subsequent clicks to cycle through the same
1941 set of three meanings, so that four clicks are equivalent to one
1942 click, five are equivalent to two, and six are equivalent to three.
1943
1944 Emacs also records multiple presses in drag and button-down events.
1945 For example, when you press a button twice, then move the mouse while
1946 holding the button, Emacs gets a @samp{double-drag-} event. And at the
1947 moment when you press it down for the second time, Emacs gets a
1948 @samp{double-down-} event (which is ignored, like all button-down
1949 events, if it has no binding).
1950
1951 @vindex double-click-time
1952 The variable @code{double-click-time} specifies how much time can
1953 elapse between clicks and still allow them to be grouped as a multiple
1954 click. Its value is in units of milliseconds. If the value is
1955 @code{nil}, double clicks are not detected at all. If the value is
1956 @code{t}, then there is no time limit. The default is 500.
1957
1958 @vindex double-click-fuzz
1959 The variable @code{double-click-fuzz} specifies how much the mouse
1960 can move between clicks and still allow them to be grouped as a multiple
1961 click. Its value is in units of pixels on windowed displays and in
1962 units of 1/8 of a character cell on text-mode terminals; the default is
1963 3.
1964
1965 The symbols for mouse events also indicate the status of the modifier
1966 keys, with the usual prefixes @samp{C-}, @samp{M-}, @samp{H-},
1967 @samp{s-}, @samp{A-} and @samp{S-}. These always precede @samp{double-}
1968 or @samp{triple-}, which always precede @samp{drag-} or @samp{down-}.
1969
1970 A frame includes areas that don't show text from the buffer, such as
1971 the mode line and the scroll bar. You can tell whether a mouse button
1972 comes from a special area of the screen by means of dummy ``prefix
1973 keys''. For example, if you click the mouse in the mode line, you get
1974 the prefix key @code{mode-line} before the ordinary mouse-button symbol.
1975 Thus, here is how to define the command for clicking the first button in
1976 a mode line to run @code{scroll-up-command}:
1977
1978 @example
1979 (global-set-key [mode-line mouse-1] 'scroll-up-command)
1980 @end example
1981
1982 Here is the complete list of these dummy prefix keys and their
1983 meanings:
1984
1985 @table @code
1986 @item mode-line
1987 The mouse was in the mode line of a window.
1988 @item vertical-line
1989 The mouse was in the vertical line separating side-by-side windows. (If
1990 you use scroll bars, they appear in place of these vertical lines.)
1991 @item vertical-scroll-bar
1992 The mouse was in a vertical scroll bar. (This is the only kind of
1993 scroll bar Emacs currently supports.)
1994 @item menu-bar
1995 The mouse was in the menu bar.
1996 @item header-line
1997 The mouse was in a header line.
1998 @ignore
1999 @item horizontal-scroll-bar
2000 The mouse was in a horizontal scroll bar. Horizontal scroll bars do
2001 horizontal scrolling, and people don't use them often.
2002 @end ignore
2003 @end table
2004
2005 You can put more than one mouse button in a key sequence, but it isn't
2006 usual to do so.
2007
2008 @node Disabling
2009 @subsection Disabling Commands
2010 @cindex disabled command
2011
2012 Disabling a command means that invoking it interactively asks for
2013 confirmation from the user. The purpose of disabling a command is to
2014 prevent users from executing it by accident; we do this for commands
2015 that might be confusing to the uninitiated.
2016
2017 Attempting to invoke a disabled command interactively in Emacs
2018 displays a window containing the command's name, its documentation,
2019 and some instructions on what to do immediately; then Emacs asks for
2020 input saying whether to execute the command as requested, enable it
2021 and execute it, or cancel. If you decide to enable the command, you
2022 must then answer another question---whether to do this permanently, or
2023 just for the current session. (Enabling permanently works by
2024 automatically editing your initialization file.) You can also type
2025 @kbd{!} to enable @emph{all} commands, for the current session only.
2026
2027 The direct mechanism for disabling a command is to put a
2028 non-@code{nil} @code{disabled} property on the Lisp symbol for the
2029 command. Here is the Lisp program to do this:
2030
2031 @example
2032 (put 'delete-region 'disabled t)
2033 @end example
2034
2035 If the value of the @code{disabled} property is a string, that string
2036 is included in the message displayed when the command is used:
2037
2038 @example
2039 (put 'delete-region 'disabled
2040 "It's better to use `kill-region' instead.\n")
2041 @end example
2042
2043 @findex disable-command
2044 @findex enable-command
2045 You can make a command disabled either by editing the initialization
2046 file directly, or with the command @kbd{M-x disable-command}, which
2047 edits the initialization file for you. Likewise, @kbd{M-x
2048 enable-command} edits the initialization file to enable a command
2049 permanently. @xref{Init File}.
2050
2051 If Emacs was invoked with the @option{-q} or @option{--no-init-file}
2052 options (@pxref{Initial Options}), it will not edit your
2053 initialization file. Doing so could lose information because Emacs
2054 has not read your initialization file.
2055
2056 Whether a command is disabled is independent of what key is used to
2057 invoke it; disabling also applies if the command is invoked using
2058 @kbd{M-x}. However, disabling a command has no effect on calling it
2059 as a function from Lisp programs.
2060
2061 @node Init File
2062 @section The Emacs Initialization File
2063 @cindex init file
2064 @cindex .emacs file
2065 @cindex ~/.emacs file
2066 @cindex Emacs initialization file
2067 @cindex key rebinding, permanent
2068 @cindex rebinding keys, permanently
2069 @cindex startup (init file)
2070
2071 When Emacs is started, it normally tries to load a Lisp program from
2072 an @dfn{initialization file}, or @dfn{init file} for short. This
2073 file, if it exists, specifies how to initialize Emacs for you. Emacs
2074 looks for your init file using the filenames @file{~/.emacs},
2075 @file{~/.emacs.el}, or @file{~/.emacs.d/init.el}; you can choose to
2076 use any one of these three names (@pxref{Find Init}). Here, @file{~/}
2077 stands for your home directory.
2078
2079 You can use the command line switch @samp{-q} to prevent loading
2080 your init file, and @samp{-u} (or @samp{--user}) to specify a
2081 different user's init file (@pxref{Initial Options}).
2082
2083 @cindex @file{default.el}, the default init file
2084 There can also be a @dfn{default init file}, which is the library
2085 named @file{default.el}, found via the standard search path for
2086 libraries. The Emacs distribution contains no such library; your site
2087 may create one for local customizations. If this library exists, it is
2088 loaded whenever you start Emacs (except when you specify @samp{-q}).
2089 But your init file, if any, is loaded first; if it sets
2090 @code{inhibit-default-init} non-@code{nil}, then @file{default} is not
2091 loaded.
2092
2093 @cindex site init file
2094 @cindex @file{site-start.el}, the site startup file
2095 Your site may also have a @dfn{site startup file}; this is named
2096 @file{site-start.el}, if it exists. Like @file{default.el}, Emacs
2097 finds this file via the standard search path for Lisp libraries.
2098 Emacs loads this library before it loads your init file. To inhibit
2099 loading of this library, use the option @samp{--no-site-file}.
2100 @xref{Initial Options}. We recommend against using
2101 @file{site-start.el} for changes that some users may not like. It is
2102 better to put them in @file{default.el}, so that users can more easily
2103 override them.
2104
2105 You can place @file{default.el} and @file{site-start.el} in any of
2106 the directories which Emacs searches for Lisp libraries. The variable
2107 @code{load-path} (@pxref{Lisp Libraries}) specifies these directories.
2108 Many sites put these files in the @file{site-lisp} subdirectory of the
2109 Emacs installation directory, typically
2110 @file{/usr/local/share/emacs/site-lisp}.
2111
2112 Byte-compiling your init file is not recommended (@pxref{Byte
2113 Compilation,, Byte Compilation, elisp, the Emacs Lisp Reference
2114 Manual}). It generally does not speed up startup very much, and often
2115 leads to problems when you forget to recompile the file. A better
2116 solution is to use the Emacs server to reduce the number of times you
2117 have to start Emacs (@pxref{Emacs Server}). If your init file defines
2118 many functions, consider moving them to a separate (byte-compiled)
2119 file that you load in your init file.
2120
2121 If you are going to write actual Emacs Lisp programs that go beyond
2122 minor customization, you should read the @cite{Emacs Lisp Reference Manual}.
2123 @ifnottex
2124 @xref{Top, Emacs Lisp, Emacs Lisp, elisp, the Emacs Lisp Reference
2125 Manual}.
2126 @end ifnottex
2127
2128 @menu
2129 * Init Syntax:: Syntax of constants in Emacs Lisp.
2130 * Init Examples:: How to do some things with an init file.
2131 * Terminal Init:: Each terminal type can have an init file.
2132 * Find Init:: How Emacs finds the init file.
2133 * Init Non-ASCII:: Using non-@acronym{ASCII} characters in an init file.
2134 @end menu
2135
2136 @node Init Syntax
2137 @subsection Init File Syntax
2138
2139 The init file contains one or more Lisp expressions. Each of these
2140 consists of a function name followed by arguments, all surrounded by
2141 parentheses. For example, @code{(setq fill-column 60)} calls the
2142 function @code{setq} to set the variable @code{fill-column}
2143 (@pxref{Filling}) to 60.
2144
2145 You can set any Lisp variable with @code{setq}, but with certain
2146 variables @code{setq} won't do what you probably want in the
2147 @file{.emacs} file. Some variables automatically become buffer-local
2148 when set with @code{setq}; what you want in @file{.emacs} is to set
2149 the default value, using @code{setq-default}. Some customizable minor
2150 mode variables do special things to enable the mode when you set them
2151 with Customize, but ordinary @code{setq} won't do that; to enable the
2152 mode in your @file{.emacs} file, call the minor mode command. The
2153 following section has examples of both of these methods.
2154
2155 The second argument to @code{setq} is an expression for the new
2156 value of the variable. This can be a constant, a variable, or a
2157 function call expression. In @file{.emacs}, constants are used most
2158 of the time. They can be:
2159
2160 @table @asis
2161 @item Numbers:
2162 Numbers are written in decimal, with an optional initial minus sign.
2163
2164 @item Strings:
2165 @cindex Lisp string syntax
2166 @cindex string syntax
2167 Lisp string syntax is the same as C string syntax with a few extra
2168 features. Use a double-quote character to begin and end a string constant.
2169
2170 In a string, you can include newlines and special characters literally.
2171 But often it is cleaner to use backslash sequences for them: @samp{\n}
2172 for newline, @samp{\b} for backspace, @samp{\r} for carriage return,
2173 @samp{\t} for tab, @samp{\f} for formfeed (control-L), @samp{\e} for
2174 escape, @samp{\\} for a backslash, @samp{\"} for a double-quote, or
2175 @samp{\@var{ooo}} for the character whose octal code is @var{ooo}.
2176 Backslash and double-quote are the only characters for which backslash
2177 sequences are mandatory.
2178
2179 @samp{\C-} can be used as a prefix for a control character, as in
2180 @samp{\C-s} for @acronym{ASCII} control-S, and @samp{\M-} can be used as a prefix for
2181 a Meta character, as in @samp{\M-a} for @kbd{Meta-A} or @samp{\M-\C-a} for
2182 @kbd{Control-Meta-A}.@refill
2183
2184 @xref{Init Non-ASCII}, for information about including
2185 non-@acronym{ASCII} in your init file.
2186
2187 @item Characters:
2188 @cindex Lisp character syntax
2189 @cindex character syntax
2190 Lisp character constant syntax consists of a @samp{?} followed by
2191 either a character or an escape sequence starting with @samp{\}.
2192 Examples: @code{?x}, @code{?\n}, @code{?\"}, @code{?\)}. Note that
2193 strings and characters are not interchangeable in Lisp; some contexts
2194 require one and some contexts require the other.
2195
2196 @xref{Init Non-ASCII}, for information about binding commands to
2197 keys which send non-@acronym{ASCII} characters.
2198
2199 @item True:
2200 @code{t} stands for `true'.
2201
2202 @item False:
2203 @code{nil} stands for `false'.
2204
2205 @item Other Lisp objects:
2206 @cindex Lisp object syntax
2207 Write a single-quote (@code{'}) followed by the Lisp object you want.
2208 @end table
2209
2210 @node Init Examples
2211 @subsection Init File Examples
2212
2213 Here are some examples of doing certain commonly desired things with
2214 Lisp expressions:
2215
2216 @itemize @bullet
2217 @item
2218 Add a directory to the variable @code{load-path}. You can then put
2219 Lisp libraries that are not included with Emacs in this directory, and
2220 load them with @kbd{M-x load-library}. @xref{Lisp Libraries}.
2221
2222 @example
2223 (add-to-list 'load-path "/path/to/lisp/libraries")
2224 @end example
2225
2226 @item
2227 Make @key{TAB} in C mode just insert a tab if point is in the middle of a
2228 line.
2229
2230 @example
2231 (setq c-tab-always-indent nil)
2232 @end example
2233
2234 Here we have a variable whose value is normally @code{t} for `true'
2235 and the alternative is @code{nil} for `false'.
2236
2237 @item
2238 Make searches case sensitive by default (in all buffers that do not
2239 override this).
2240
2241 @example
2242 (setq-default case-fold-search nil)
2243 @end example
2244
2245 This sets the default value, which is effective in all buffers that do
2246 not have local values for the variable (@pxref{Locals}). Setting
2247 @code{case-fold-search} with @code{setq} affects only the current
2248 buffer's local value, which is probably not what you want to do in an
2249 init file.
2250
2251 @item
2252 @vindex user-mail-address
2253 Specify your own email address, if Emacs can't figure it out correctly.
2254
2255 @example
2256 (setq user-mail-address "cheney@@torture.gov")
2257 @end example
2258
2259 Various Emacs packages, such as Message mode, consult
2260 @code{user-mail-address} when they need to know your email address.
2261 @xref{Mail Headers}.
2262
2263 @item
2264 Make Text mode the default mode for new buffers.
2265
2266 @example
2267 (setq-default major-mode 'text-mode)
2268 @end example
2269
2270 Note that @code{text-mode} is used because it is the command for
2271 entering Text mode. The single-quote before it makes the symbol a
2272 constant; otherwise, @code{text-mode} would be treated as a variable
2273 name.
2274
2275 @need 1500
2276 @item
2277 Set up defaults for the Latin-1 character set
2278 which supports most of the languages of Western Europe.
2279
2280 @example
2281 (set-language-environment "Latin-1")
2282 @end example
2283
2284 @need 1500
2285 @item
2286 Turn off Line Number mode, a global minor mode.
2287
2288 @example
2289 (line-number-mode 0)
2290 @end example
2291
2292 @need 1500
2293 @item
2294 Turn on Auto Fill mode automatically in Text mode and related modes
2295 (@pxref{Hooks}).
2296
2297 @example
2298 (add-hook 'text-mode-hook 'auto-fill-mode)
2299 @end example
2300
2301 @item
2302 Load the installed Lisp library named @file{foo} (actually a file
2303 @file{foo.elc} or @file{foo.el} in a standard Emacs directory).
2304
2305 @example
2306 (load "foo")
2307 @end example
2308
2309 When the argument to @code{load} is a relative file name, not starting
2310 with @samp{/} or @samp{~}, @code{load} searches the directories in
2311 @code{load-path} (@pxref{Lisp Libraries}).
2312
2313 @item
2314 Load the compiled Lisp file @file{foo.elc} from your home directory.
2315
2316 @example
2317 (load "~/foo.elc")
2318 @end example
2319
2320 Here a full file name is used, so no searching is done.
2321
2322 @item
2323 @cindex loading Lisp libraries automatically
2324 @cindex autoload Lisp libraries
2325 Tell Emacs to find the definition for the function @code{myfunction}
2326 by loading a Lisp library named @file{mypackage} (i.e.@: a file
2327 @file{mypackage.elc} or @file{mypackage.el}):
2328
2329 @example
2330 (autoload 'myfunction "mypackage" "Do what I say." t)
2331 @end example
2332
2333 @noindent
2334 Here the string @code{"Do what I say."} is the function's
2335 documentation string. You specify it in the @code{autoload}
2336 definition so it will be available for help commands even when the
2337 package is not loaded. The last argument, @code{t}, indicates that
2338 this function is interactive; that is, it can be invoked interactively
2339 by typing @kbd{M-x myfunction @key{RET}} or by binding it to a key.
2340 If the function is not interactive, omit the @code{t} or use
2341 @code{nil}.
2342
2343 @item
2344 Rebind the key @kbd{C-x l} to run the function @code{make-symbolic-link}
2345 (@pxref{Init Rebinding}).
2346
2347 @example
2348 (global-set-key "\C-xl" 'make-symbolic-link)
2349 @end example
2350
2351 or
2352
2353 @example
2354 (define-key global-map "\C-xl" 'make-symbolic-link)
2355 @end example
2356
2357 Note once again the single-quote used to refer to the symbol
2358 @code{make-symbolic-link} instead of its value as a variable.
2359
2360 @item
2361 Do the same thing for Lisp mode only.
2362
2363 @example
2364 (define-key lisp-mode-map "\C-xl" 'make-symbolic-link)
2365 @end example
2366
2367 @item
2368 Redefine all keys which now run @code{next-line} in Fundamental mode
2369 so that they run @code{forward-line} instead.
2370
2371 @findex substitute-key-definition
2372 @example
2373 (substitute-key-definition 'next-line 'forward-line
2374 global-map)
2375 @end example
2376
2377 @item
2378 Make @kbd{C-x C-v} undefined.
2379
2380 @example
2381 (global-unset-key "\C-x\C-v")
2382 @end example
2383
2384 One reason to undefine a key is so that you can make it a prefix.
2385 Simply defining @kbd{C-x C-v @var{anything}} will make @kbd{C-x C-v} a
2386 prefix, but @kbd{C-x C-v} must first be freed of its usual non-prefix
2387 definition.
2388
2389 @item
2390 Make @samp{$} have the syntax of punctuation in Text mode.
2391 Note the use of a character constant for @samp{$}.
2392
2393 @example
2394 (modify-syntax-entry ?\$ "." text-mode-syntax-table)
2395 @end example
2396
2397 @item
2398 Enable the use of the command @code{narrow-to-region} without confirmation.
2399
2400 @example
2401 (put 'narrow-to-region 'disabled nil)
2402 @end example
2403
2404 @item
2405 Adjusting the configuration to various platforms and Emacs versions.
2406
2407 Users typically want Emacs to behave the same on all systems, so the
2408 same init file is right for all platforms. However, sometimes it
2409 happens that a function you use for customizing Emacs is not available
2410 on some platforms or in older Emacs versions. To deal with that
2411 situation, put the customization inside a conditional that tests whether
2412 the function or facility is available, like this:
2413
2414 @example
2415 (if (fboundp 'blink-cursor-mode)
2416 (blink-cursor-mode 0))
2417
2418 (if (boundp 'coding-category-utf-8)
2419 (set-coding-priority '(coding-category-utf-8)))
2420 @end example
2421
2422 @noindent
2423 You can also simply disregard the errors that occur if the
2424 function is not defined.
2425
2426 @example
2427 (condition case ()
2428 (set-face-background 'region "grey75")
2429 (error nil))
2430 @end example
2431
2432 A @code{setq} on a variable which does not exist is generally
2433 harmless, so those do not need a conditional.
2434 @end itemize
2435
2436 @node Terminal Init
2437 @subsection Terminal-specific Initialization
2438
2439 Each terminal type can have a Lisp library to be loaded into Emacs when
2440 it is run on that type of terminal. For a terminal type named
2441 @var{termtype}, the library is called @file{term/@var{termtype}} and it is
2442 found by searching the directories @code{load-path} as usual and trying the
2443 suffixes @samp{.elc} and @samp{.el}. Normally it appears in the
2444 subdirectory @file{term} of the directory where most Emacs libraries are
2445 kept.@refill
2446
2447 The usual purpose of the terminal-specific library is to map the
2448 escape sequences used by the terminal's function keys onto more
2449 meaningful names, using @code{input-decode-map} (or
2450 @code{function-key-map} before it). See the file
2451 @file{term/lk201.el} for an example of how this is done. Many function
2452 keys are mapped automatically according to the information in the
2453 Termcap data base; the terminal-specific library needs to map only the
2454 function keys that Termcap does not specify.
2455
2456 When the terminal type contains a hyphen, only the part of the name
2457 before the first hyphen is significant in choosing the library name.
2458 Thus, terminal types @samp{aaa-48} and @samp{aaa-30-rv} both use
2459 the library @file{term/aaa}. The code in the library can use
2460 @code{(getenv "TERM")} to find the full terminal type name.@refill
2461
2462 @vindex term-file-prefix
2463 The library's name is constructed by concatenating the value of the
2464 variable @code{term-file-prefix} and the terminal type. Your @file{.emacs}
2465 file can prevent the loading of the terminal-specific library by setting
2466 @code{term-file-prefix} to @code{nil}.
2467
2468 @vindex term-setup-hook
2469 Emacs runs the hook @code{term-setup-hook} at the end of
2470 initialization, after both your @file{.emacs} file and any
2471 terminal-specific library have been read in. Add hook functions to this
2472 hook if you wish to override part of any of the terminal-specific
2473 libraries and to define initializations for terminals that do not have a
2474 library. @xref{Hooks}.
2475
2476 @node Find Init
2477 @subsection How Emacs Finds Your Init File
2478
2479 Normally Emacs uses the environment variable @env{HOME}
2480 (@pxref{General Variables, HOME}) to find @file{.emacs}; that's what
2481 @samp{~} means in a file name. If @file{.emacs} is not found inside
2482 @file{~/} (nor @file{.emacs.el}), Emacs looks for
2483 @file{~/.emacs.d/init.el} (which, like @file{~/.emacs.el}, can be
2484 byte-compiled).
2485
2486 However, if you run Emacs from a shell started by @code{su}, Emacs
2487 tries to find your own @file{.emacs}, not that of the user you are
2488 currently pretending to be. The idea is that you should get your own
2489 editor customizations even if you are running as the super user.
2490
2491 More precisely, Emacs first determines which user's init file to use.
2492 It gets your user name from the environment variables @env{LOGNAME} and
2493 @env{USER}; if neither of those exists, it uses effective user-ID.
2494 If that user name matches the real user-ID, then Emacs uses @env{HOME};
2495 otherwise, it looks up the home directory corresponding to that user
2496 name in the system's data base of users.
2497 @c LocalWords: backtab
2498
2499 @node Init Non-ASCII
2500 @subsection Non-@acronym{ASCII} Characters in Init Files
2501 @cindex international characters in @file{.emacs}
2502 @cindex non-@acronym{ASCII} characters in @file{.emacs}
2503 @cindex non-@acronym{ASCII} keys, binding
2504 @cindex rebinding non-@acronym{ASCII} keys
2505
2506 Language and coding systems may cause problems if your init file
2507 contains non-@acronym{ASCII} characters, such as accented letters, in
2508 strings or key bindings.
2509
2510 If you want to use non-@acronym{ASCII} characters in your init file,
2511 you should put a @w{@samp{-*-coding: @var{coding-system}-*-}} tag on
2512 the first line of the init file, and specify a coding system that
2513 supports the character(s) in question. @xref{Recognize Coding}. This
2514 is because the defaults for decoding non-@acronym{ASCII} text might
2515 not yet be set up by the time Emacs reads those parts of your init
2516 file which use such strings, possibly leading Emacs to decode those
2517 strings incorrectly. You should then avoid adding Emacs Lisp code
2518 that modifies the coding system in other ways, such as calls to
2519 @code{set-language-environment}.
2520
2521 To bind non-@acronym{ASCII} keys, you must use a vector (@pxref{Init
2522 Rebinding}). The string syntax cannot be used, since the
2523 non-@acronym{ASCII} characters will be interpreted as meta keys. For
2524 instance:
2525
2526 @example
2527 (global-set-key [?@var{char}] 'some-function)
2528 @end example
2529
2530 @noindent
2531 Type @kbd{C-q}, followed by the key you want to bind, to insert @var{char}.
2532
2533 @strong{Warning:} if you change the keyboard encoding, or change
2534 between multibyte and unibyte mode, or anything that would alter which
2535 code @kbd{C-q} would insert for that character, this key binding may
2536 stop working. It is therefore advisable to use one and only one
2537 coding system, for your init file as well as the files you edit. For
2538 example, don't mix the @samp{latin-1} and @samp{latin-9} coding
2539 systems.