root/trunk/lispref/keymaps.texi

Revision 4220, 103.6 kB (checked in by miyoshi, 8 months ago)

Sync up with Emacs22.2.

Line 
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1998, 1999, 2000, 2001,
4 @c   2002, 2003, 2004, 2005, 2006, 2007, 2008  Free Software Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @setfilename ../info/keymaps
7 @node Keymaps, Modes, Command Loop, Top
8 @chapter Keymaps
9 @cindex keymap
10
11   The command bindings of input events are recorded in data structures
12 called @dfn{keymaps}.  Each entry in a keymap associates (or
13 @dfn{binds}) an individual event type, either to another keymap or to
14 a command.  When an event type is bound to a keymap, that keymap is
15 used to look up the next input event; this continues until a command
16 is found.  The whole process is called @dfn{key lookup}.
17
18 @menu
19 * Key Sequences::               Key sequences as Lisp objects.
20 * Keymap Basics::               Basic concepts of keymaps.
21 * Format of Keymaps::           What a keymap looks like as a Lisp object.
22 * Creating Keymaps::            Functions to create and copy keymaps.
23 * Inheritance and Keymaps::     How one keymap can inherit the bindings
24                                    of another keymap.
25 * Prefix Keys::                 Defining a key with a keymap as its definition.
26 * Active Keymaps::              How Emacs searches the active keymaps
27                                    for a key binding.
28 * Searching Keymaps::           A pseudo-Lisp summary of searching active maps.
29 * Controlling Active Maps::     Each buffer has a local keymap
30                                    to override the standard (global) bindings.
31                                    A minor mode can also override them.
32 * Key Lookup::                  Finding a key's binding in one keymap.
33 * Functions for Key Lookup::    How to request key lookup.
34 * Changing Key Bindings::       Redefining a key in a keymap.
35 * Remapping Commands::          A keymap can translate one command to another.
36 * Translation Keymaps::         Keymaps for translating sequences of events.
37 * Key Binding Commands::        Interactive interfaces for redefining keys.
38 * Scanning Keymaps::            Looking through all keymaps, for printing help.
39 * Menu Keymaps::                Defining a menu as a keymap.
40 @end menu
41
42 @node Key Sequences
43 @section Key Sequences
44 @cindex key
45 @cindex keystroke
46 @cindex key sequence
47
48   A @dfn{key sequence}, or @dfn{key} for short, is a sequence of one
49 or more input events that form a unit.  Input events include
50 characters, function keys, and mouse actions (@pxref{Input Events}).
51 The Emacs Lisp representation for a key sequence is a string or
52 vector.  Unless otherwise stated, any Emacs Lisp function that accepts
53 a key sequence as an argument can handle both representations.
54
55   In the string representation, alphanumeric characters ordinarily
56 stand for themselves; for example, @code{"a"} represents @kbd{a}
57 and @code{"2"} represents @kbd{2}.  Control character events are
58 prefixed by the substring @code{"\C-"}, and meta characters by
59 @code{"\M-"}; for example, @code{"\C-x"} represents the key @kbd{C-x}.
60 In addition, the @key{TAB}, @key{RET}, @key{ESC}, and @key{DEL} events
61 are represented by @code{"\t"}, @code{"\r"}, @code{"\e"}, and
62 @code{"\d"} respectively.  The string representation of a complete key
63 sequence is the concatenation of the string representations of the
64 constituent events; thus, @code{"\C-xl"} represents the key sequence
65 @kbd{C-x l}.
66
67   Key sequences containing function keys, mouse button events, or
68 non-ASCII characters such as @kbd{C-=} or @kbd{H-a} cannot be
69 represented as strings; they have to be represented as vectors.
70
71   In the vector representation, each element of the vector represents
72 an input event, in its Lisp form.  @xref{Input Events}.  For example,
73 the vector @code{[?\C-x ?l]} represents the key sequence @kbd{C-x l}.
74
75   For examples of key sequences written in string and vector
76 representations, @ref{Init Rebinding,,, emacs, The GNU Emacs Manual}.
77
78 @defmac kbd keyseq-text
79 This macro converts the text @var{keyseq-text} (a string constant)
80 into a key sequence (a string or vector constant).  The contents of
81 @var{keyseq-text} should describe the key sequence using almost the same
82 syntax used in this manual.  More precisely, it uses the same syntax
83 that Edit Macro mode uses for editing keyboard macros (@pxref{Edit
84 Keyboard Macro,,, emacs, The GNU Emacs Manual}); you must surround
85 function key names with @samp{<@dots{}>}.
86
87 @example
88 (kbd "C-x") @result{} "\C-x"
89 (kbd "C-x C-f") @result{} "\C-x\C-f"
90 (kbd "C-x 4 C-f") @result{} "\C-x4\C-f"
91 (kbd "X") @result{} "X"
92 (kbd "RET") @result{} "\^M"
93 (kbd "C-c SPC") @result{} "\C-c@ "
94 (kbd "<f1> SPC") @result{} [f1 32]
95 (kbd "C-M-<down>") @result{} [C-M-down]
96 @end example
97
98 This macro is not meant for use with arguments that vary---only
99 with string constants.
100 @end defmac
101
102 @node Keymap Basics
103 @section Keymap Basics
104 @cindex key binding
105 @cindex binding of a key
106 @cindex complete key
107 @cindex undefined key
108
109   A keymap is a Lisp data structure that specifies @dfn{key bindings}
110 for various key sequences.
111
112   A single keymap directly specifies definitions for individual
113 events.  When a key sequence consists of a single event, its binding
114 in a keymap is the keymap's definition for that event.  The binding of
115 a longer key sequence is found by an iterative process: first find the
116 definition of the first event (which must itself be a keymap); then
117 find the second event's definition in that keymap, and so on until all
118 the events in the key sequence have been processed.
119
120   If the binding of a key sequence is a keymap, we call the key sequence
121 a @dfn{prefix key}.  Otherwise, we call it a @dfn{complete key} (because
122 no more events can be added to it).  If the binding is @code{nil},
123 we call the key @dfn{undefined}.  Examples of prefix keys are @kbd{C-c},
124 @kbd{C-x}, and @kbd{C-x 4}.  Examples of defined complete keys are
125 @kbd{X}, @key{RET}, and @kbd{C-x 4 C-f}.  Examples of undefined complete
126 keys are @kbd{C-x C-g}, and @kbd{C-c 3}.  @xref{Prefix Keys}, for more
127 details.
128
129   The rule for finding the binding of a key sequence assumes that the
130 intermediate bindings (found for the events before the last) are all
131 keymaps; if this is not so, the sequence of events does not form a
132 unit---it is not really one key sequence.  In other words, removing one
133 or more events from the end of any valid key sequence must always yield
134 a prefix key.  For example, @kbd{C-f C-n} is not a key sequence;
135 @kbd{C-f} is not a prefix key, so a longer sequence starting with
136 @kbd{C-f} cannot be a key sequence.
137
138   The set of possible multi-event key sequences depends on the bindings
139 for prefix keys; therefore, it can be different for different keymaps,
140 and can change when bindings are changed.  However, a one-event sequence
141 is always a key sequence, because it does not depend on any prefix keys
142 for its well-formedness.
143
144   At any time, several primary keymaps are @dfn{active}---that is, in
145 use for finding key bindings.  These are the @dfn{global map}, which is
146 shared by all buffers; the @dfn{local keymap}, which is usually
147 associated with a specific major mode; and zero or more @dfn{minor mode
148 keymaps}, which belong to currently enabled minor modes.  (Not all minor
149 modes have keymaps.)  The local keymap bindings shadow (i.e., take
150 precedence over) the corresponding global bindings.  The minor mode
151 keymaps shadow both local and global keymaps.  @xref{Active Keymaps},
152 for details.
153
154 @node Format of Keymaps
155 @section Format of Keymaps
156 @cindex format of keymaps
157 @cindex keymap format
158 @cindex full keymap
159 @cindex sparse keymap
160
161   Each keymap is a list whose @sc{car} is the symbol @code{keymap}.  The
162 remaining elements of the list define the key bindings of the keymap.
163 A symbol whose function definition is a keymap is also a keymap.  Use
164 the function @code{keymapp} (see below) to test whether an object is a
165 keymap.
166
167   Several kinds of elements may appear in a keymap, after the symbol
168 @code{keymap} that begins it:
169
170 @table @code
171 @item (@var{type} .@: @var{binding})
172 This specifies one binding, for events of type @var{type}.  Each
173 ordinary binding applies to events of a particular @dfn{event type},
174 which is always a character or a symbol.  @xref{Classifying Events}.
175 In this kind of binding, @var{binding} is a command.
176
177 @item (@var{type} @var{item-name} @r{[}@var{cache}@r{]} .@: @var{binding})
178 This specifies a binding which is also a simple menu item that
179 displays as @var{item-name} in the menu.  @var{cache}, if present,
180 caches certain information for display in the menu.  @xref{Simple Menu
181 Items}.
182
183 @item (@var{type} @var{item-name} @var{help-string} @r{[}@var{cache}@r{]} .@: @var{binding})
184 This is a simple menu item with help string @var{help-string}.
185
186 @item (@var{type} menu-item .@: @var{details})
187 This specifies a binding which is also an extended menu item.  This
188 allows use of other features.  @xref{Extended Menu Items}.
189
190 @item (t .@: @var{binding})
191 @cindex default key binding
192 This specifies a @dfn{default key binding}; any event not bound by other
193 elements of the keymap is given @var{binding} as its binding.  Default
194 bindings allow a keymap to bind all possible event types without having
195 to enumerate all of them.  A keymap that has a default binding
196 completely masks any lower-precedence keymap, except for events
197 explicitly bound to @code{nil} (see below).
198
199 @item @var{char-table}
200 If an element of a keymap is a char-table, it counts as holding
201 bindings for all character events with no modifier bits
202 (@pxref{modifier bits}): element @var{n} is the binding for the
203 character with code @var{n}.  This is a compact way to record lots of
204 bindings.  A keymap with such a char-table is called a @dfn{full
205 keymap}.  Other keymaps are called @dfn{sparse keymaps}.
206
207 @item @var{string}
208 @cindex keymap prompt string
209 @cindex overall prompt string
210 @cindex prompt string of keymap
211 Aside from elements that specify bindings for keys, a keymap can also
212 have a string as an element.  This is called the @dfn{overall prompt
213 string} and makes it possible to use the keymap as a menu.
214 @xref{Defining Menus}.
215 @end table
216
217 When the binding is @code{nil}, it doesn't constitute a definition
218 but it does take precedence over a default binding or a binding in the
219 parent keymap.  On the other hand, a binding of @code{nil} does
220 @emph{not} override lower-precedence keymaps; thus, if the local map
221 gives a binding of @code{nil}, Emacs uses the binding from the
222 global map.
223
224 @cindex meta characters lookup
225   Keymaps do not directly record bindings for the meta characters.
226 Instead, meta characters are regarded for purposes of key lookup as
227 sequences of two characters, the first of which is @key{ESC} (or
228 whatever is currently the value of @code{meta-prefix-char}).  Thus, the
229 key @kbd{M-a} is internally represented as @kbd{@key{ESC} a}, and its
230 global binding is found at the slot for @kbd{a} in @code{esc-map}
231 (@pxref{Prefix Keys}).
232
233   This conversion applies only to characters, not to function keys or
234 other input events; thus, @kbd{M-@key{end}} has nothing to do with
235 @kbd{@key{ESC} @key{end}}.
236
237   Here as an example is the local keymap for Lisp mode, a sparse
238 keymap.  It defines bindings for @key{DEL} and @key{TAB}, plus @kbd{C-c
239 C-l}, @kbd{M-C-q}, and @kbd{M-C-x}.
240
241 @example
242 @group
243 lisp-mode-map
244 @result{}
245 @end group
246 @group
247 (keymap
248  (3 keymap
249     ;; @kbd{C-c C-z}
250     (26 . run-lisp))
251 @end group
252 @group
253  (27 keymap
254      ;; @r{@kbd{M-C-x}, treated as @kbd{@key{ESC} C-x}}
255      (24 . lisp-send-defun)
256      keymap
257      ;; @r{@kbd{M-C-q}, treated as @kbd{@key{ESC} C-q}}
258      (17 . indent-sexp))
259 @end group
260 @group
261  ;; @r{This part is inherited from @code{lisp-mode-shared-map}.}
262  keymap
263  ;; @key{DEL}
264  (127 . backward-delete-char-untabify)
265 @end group
266 @group
267  (27 keymap
268      ;; @r{@kbd{M-C-q}, treated as @kbd{@key{ESC} C-q}}
269      (17 . indent-sexp))
270  (9 . lisp-indent-line))
271 @end group
272 @end example
273
274 @defun keymapp object
275 This function returns @code{t} if @var{object} is a keymap, @code{nil}
276 otherwise.  More precisely, this function tests for a list whose
277 @sc{car} is @code{keymap}, or for a symbol whose function definition
278 satisfies @code{keymapp}.
279
280 @example
281 @group
282 (keymapp '(keymap))
283     @result{} t
284 @end group
285 @group
286 (fset 'foo '(keymap))
287 (keymapp 'foo)
288     @result{} t
289 @end group
290 @group
291 (keymapp (current-global-map))
292     @result{} t
293 @end group
294 @end example
295 @end defun
296
297 @node Creating Keymaps
298 @section Creating Keymaps
299 @cindex creating keymaps
300
301   Here we describe the functions for creating keymaps.
302
303 @defun make-sparse-keymap &optional prompt
304 This function creates and returns a new sparse keymap with no entries.
305 (A sparse keymap is the kind of keymap you usually want.)  The new
306 keymap does not contain a char-table, unlike @code{make-keymap}, and
307 does not bind any events.
308
309 @example
310 @group
311 (make-sparse-keymap)
312     @result{} (keymap)
313 @end group
314 @end example
315
316 If you specify @var{prompt}, that becomes the overall prompt string
317 for the keymap.  You should specify this only for menu keymaps
318 (@pxref{Defining Menus}).  A keymap with an overall prompt string will
319 always present a mouse menu or a keyboard menu if it is active for
320 looking up the next input event.  Don't specify an overall prompt string
321 for the main map of a major or minor mode, because that would cause
322 the command loop to present a keyboard menu every time.
323 @end defun
324
325 @defun make-keymap &optional prompt
326 This function creates and returns a new full keymap.  That keymap
327 contains a char-table (@pxref{Char-Tables}) with slots for all
328 characters without modifiers.  The new keymap initially binds all
329 these characters to @code{nil}, and does not bind any other kind of
330 event.  The argument @var{prompt} specifies a
331 prompt string, as in @code{make-sparse-keymap}.
332
333 @example
334 @group
335 (make-keymap)
336     @result{} (keymap #^[t nil nil nil @dots{} nil nil keymap])
337 @end group
338 @end example
339
340 A full keymap is more efficient than a sparse keymap when it holds
341 lots of bindings; for just a few, the sparse keymap is better.
342 @end defun
343
344 @defun copy-keymap keymap
345 This function returns a copy of @var{keymap}.  Any keymaps that
346 appear directly as bindings in @var{keymap} are also copied recursively,
347 and so on to any number of levels.  However, recursive copying does not
348 take place when the definition of a character is a symbol whose function
349 definition is a keymap; the same symbol appears in the new copy.
350 @c Emacs 19 feature
351
352 @example
353 @group
354 (setq map (copy-keymap (current-local-map)))
355 @result{} (keymap
356 @end group
357 @group
358      ;; @r{(This implements meta characters.)}
359      (27 keymap
360          (83 . center-paragraph)
361          (115 . center-line))
362      (9 . tab-to-tab-stop))
363 @end group
364
365 @group
366 (eq map (current-local-map))
367     @result{} nil
368 @end group
369 @group
370 (equal map (current-local-map))
371     @result{} t
372 @end group
373 @end example
374 @end defun
375
376 @node Inheritance and Keymaps
377 @section Inheritance and Keymaps
378 @cindex keymap inheritance
379 @cindex inheriting a keymap's bindings
380
381   A keymap can inherit the bindings of another keymap, which we call the
382 @dfn{parent keymap}.  Such a keymap looks like this:
383
384 @example
385 (keymap @var{elements}@dots{} . @var{parent-keymap})
386 @end example
387
388 @noindent
389 The effect is that this keymap inherits all the bindings of
390 @var{parent-keymap}, whatever they may be at the time a key is looked up,
391 but can add to them or override them with @var{elements}.
392
393 If you change the bindings in @var{parent-keymap} using
394 @code{define-key} or other key-binding functions, these changed
395 bindings are visible in the inheriting keymap, unless shadowed by the
396 bindings made by @var{elements}.  The converse is not true: if you use
397 @code{define-key} to change bindings in the inheriting keymap, these
398 changes are recorded in @var{elements}, but have no effect on
399 @var{parent-keymap}.
400
401 The proper way to construct a keymap with a parent is to use
402 @code{set-keymap-parent}; if you have code that directly constructs a
403 keymap with a parent, please convert the program to use
404 @code{set-keymap-parent} instead.
405
406 @defun keymap-parent keymap
407 This returns the parent keymap of @var{keymap}.  If @var{keymap}
408 has no parent, @code{keymap-parent} returns @code{nil}.
409 @end defun
410
411 @defun set-keymap-parent keymap parent
412 This sets the parent keymap of @var{keymap} to @var{parent}, and returns
413 @var{parent}.  If @var{parent} is @code{nil}, this function gives
414 @var{keymap} no parent at all.
415
416 If @var{keymap} has submaps (bindings for prefix keys), they too receive
417 new parent keymaps that reflect what @var{parent} specifies for those
418 prefix keys.
419 @end defun
420
421    Here is an example showing how to make a keymap that inherits
422 from @code{text-mode-map}:
423
424 @example
425 (let ((map (make-sparse-keymap)))
426   (set-keymap-parent map text-mode-map)
427   map)
428 @end example
429
430   A non-sparse keymap can have a parent too, but this is not very
431 useful.  A non-sparse keymap always specifies something as the binding
432 for every numeric character code without modifier bits, even if it is
433 @code{nil}, so these character's bindings are never inherited from
434 the parent keymap.
435
436 @node Prefix Keys
437 @section Prefix Keys
438 @cindex prefix key
439
440   A @dfn{prefix key} is a key sequence whose binding is a keymap.  The
441 keymap defines what to do with key sequences that extend the prefix key.
442 For example, @kbd{C-x} is a prefix key, and it uses a keymap that is
443 also stored in the variable @code{ctl-x-map}.  This keymap defines
444 bindings for key sequences starting with @kbd{C-x}.
445
446   Some of the standard Emacs prefix keys use keymaps that are
447 also found in Lisp variables:
448
449 @itemize @bullet
450 @item
451 @vindex esc-map
452 @findex ESC-prefix
453 @code{esc-map} is the global keymap for the @key{ESC} prefix key.  Thus,
454 the global definitions of all meta characters are actually found here.
455 This map is also the function definition of @code{ESC-prefix}.
456
457 @item
458 @cindex @kbd{C-h}
459 @code{help-map} is the global keymap for the @kbd{C-h} prefix key.
460
461 @item
462 @cindex @kbd{C-c}
463 @vindex mode-specific-map
464 @code{mode-specific-map} is the global keymap for the prefix key
465 @kbd{C-c}.  This map is actually global, not mode-specific, but its name
466 provides useful information about @kbd{C-c} in the output of @kbd{C-h b}
467 (@code{display-bindings}), since the main use of this prefix key is for
468 mode-specific bindings.
469
470 @item
471 @cindex @kbd{C-x}
472 @vindex ctl-x-map
473 @findex Control-X-prefix
474 @code{ctl-x-map} is the global keymap used for the @kbd{C-x} prefix key.
475 This map is found via the function cell of the symbol
476 @code{Control-X-prefix}.
477
478 @item
479 @cindex @kbd{C-x @key{RET}}
480 @vindex mule-keymap
481 @code{mule-keymap} is the global keymap used for the @kbd{C-x @key{RET}}
482 prefix key.
483
484 @item
485 @cindex @kbd{C-x 4}
486 @vindex ctl-x-4-map
487 @code{ctl-x-4-map} is the global keymap used for the @kbd{C-x 4} prefix
488 key.
489
490 @c Emacs 19 feature
491 @item
492 @cindex @kbd{C-x 5}
493 @vindex ctl-x-5-map
494 @code{ctl-x-5-map} is the global keymap used for the @kbd{C-x 5} prefix
495 key.
496
497 @c Emacs 19 feature
498 @item
499 @cindex @kbd{C-x 6}
500 @vindex 2C-mode-map
501 @code{2C-mode-map} is the global keymap used for the @kbd{C-x 6} prefix
502 key.
503
504 @item
505 @cindex @kbd{C-x v}
506 @vindex vc-prefix-map
507 @code{vc-prefix-map} is the global keymap used for the @kbd{C-x v} prefix
508 key.
509
510 @item
511 @cindex @kbd{M-o}
512 @vindex facemenu-keymap
513 @code{facemenu-keymap} is the global keymap used for the @kbd{M-o}
514 prefix key.
515
516 @c Emacs 19 feature
517 @item
518 The other Emacs prefix keys are @kbd{M-g}, @kbd{C-x @@}, @kbd{C-x a i},
519 @kbd{C-x @key{ESC}} and @kbd{@key{ESC} @key{ESC}}.  They use keymaps
520 that have no special names.
521 @end itemize
522
523   The keymap binding of a prefix key is used for looking up the event
524 that follows the prefix key.  (It may instead be a symbol whose function
525 definition is a keymap.  The effect is the same, but the symbol serves
526 as a name for the prefix key.)  Thus, the binding of @kbd{C-x} is the
527 symbol @code{Control-X-prefix}, whose function cell holds the keymap
528 for @kbd{C-x} commands.  (The same keymap is also the value of
529 @code{ctl-x-map}.)
530
531   Prefix key definitions can appear in any active keymap.  The
532 definitions of @kbd{C-c}, @kbd{C-x}, @kbd{C-h} and @key{ESC} as prefix
533 keys appear in the global map, so these prefix keys are always
534 available.  Major and minor modes can redefine a key as a prefix by
535 putting a prefix key definition for it in the local map or the minor
536 mode's map.  @xref{Active Keymaps}.
537
538   If a key is defined as a prefix in more than one active map, then its
539 various definitions are in effect merged: the commands defined in the
540 minor mode keymaps come first, followed by those in the local map's
541 prefix definition, and then by those from the global map.
542
543   In the following example, we make @kbd{C-p} a prefix key in the local
544 keymap, in such a way that @kbd{C-p} is identical to @kbd{C-x}.  Then
545 the binding for @kbd{C-p C-f} is the function @code{find-file}, just
546 like @kbd{C-x C-f}.  The key sequence @kbd{C-p 6} is not found in any
547 active keymap.
548
549 @example
550 @group
551 (use-local-map (make-sparse-keymap))
552     @result{} nil
553 @end group
554 @group
555 (local-set-key "\C-p" ctl-x-map)
556     @result{} nil
557 @end group
558 @group
559 (key-binding "\C-p\C-f")
560     @result{} find-file
561 @end group
562
563 @group
564 (key-binding "\C-p6")
565     @result{} nil
566 @end group
567 @end example
568
569 @defun define-prefix-command symbol &optional mapvar prompt
570 @cindex prefix command
571 @anchor{Definition of define-prefix-command}
572 This function prepares @var{symbol} for use as a prefix key's binding:
573 it creates a sparse keymap and stores it as @var{symbol}'s function
574 definition.  Subsequently binding a key sequence to @var{symbol} will
575 make that key sequence into a prefix key.  The return value is @code{symbol}.
576
577 This function also sets @var{symbol} as a variable, with the keymap as
578 its value.  But if @var{mapvar} is non-@code{nil}, it sets @var{mapvar}
579 as a variable instead.
580
581 If @var{prompt} is non-@code{nil}, that becomes the overall prompt
582 string for the keymap.  The prompt string should be given for menu keymaps
583 (@pxref{Defining Menus}).
584 @end defun
585
586 @node Active Keymaps
587 @section Active Keymaps
588 @cindex active keymap
589 @cindex global keymap
590 @cindex local keymap
591
592   Emacs normally contains many keymaps; at any given time, just a few
593 of them are @dfn{active}, meaning that they participate in the
594 interpretation of user input.  All the active keymaps are used
595 together to determine what command to execute when a key is entered.
596
597   Normally the active keymaps are the @code{keymap} property keymap,
598 the keymaps of any enabled minor modes, the current buffer's local
599 keymap, and the global keymap, in that order.  Emacs searches for each
600 input key sequence in all these keymaps.  @xref{Searching Keymaps},
601 for more details of this procedure.
602
603   When the key sequence starts with a mouse event (optionally preceded
604 by a symbolic prefix), the active keymaps are determined based on the
605 position in that event.  If the event happened on a string embedded
606 with a @code{display}, @code{before-string}, or @code{after-string}
607 property (@pxref{Special Properties}), the non-@code{nil} map
608 properties of the string override those of the buffer.
609
610   The @dfn{global keymap} holds the bindings of keys that are defined
611 regardless of the current buffer, such as @kbd{C-f}.  The variable
612 @code{global-map} holds this keymap, which is always active.
613
614   Each buffer may have another keymap, its @dfn{local keymap}, which
615 may contain new or overriding definitions for keys.  The current
616 buffer's local keymap is always active except when
617 @code{overriding-local-map} overrides it.  The @code{local-map} text
618 or overlay property can specify an alternative local keymap for certain
619 parts of the buffer; see @ref{Special Properties}.
620
621   Each minor mode can have a keymap; if it does, the keymap is active
622 when the minor mode is enabled.  Modes for emulation can specify
623 additional active keymaps through the variable
624 @code{emulation-mode-map-alists}.
625
626   The highest precedence normal keymap comes from the @code{keymap}
627 text or overlay property.  If that is non-@code{nil}, it is the first
628 keymap to be processed, in normal circumstances.
629
630   However, there are also special ways for programs to substitute
631 other keymaps for some of those.  The variable
632 @code{overriding-local-map}, if non-@code{nil}, specifies a keymap
633 that replaces all the usual active keymaps except the global keymap.
634 Another way to do this is with @code{overriding-terminal-local-map};
635 it operates on a per-terminal basis.  These variables are documented
636 below.
637
638 @cindex major mode keymap
639   Since every buffer that uses the same major mode normally uses the
640 same local keymap, you can think of the keymap as local to the mode.  A
641 change to the local keymap of a buffer (using @code{local-set-key}, for
642 example) is seen also in the other buffers that share that keymap.
643
644   The local keymaps that are used for Lisp mode and some other major
645 modes exist even if they have not yet been used.  These local keymaps are
646 the values of variables such as @code{lisp-mode-map}.  For most major
647 modes, which are less frequently used, the local keymap is constructed
648 only when the mode is used for the first time in a session.
649
650   The minibuffer has local keymaps, too; they contain various completion
651 and exit commands.  @xref{Intro to Minibuffers}.
652
653   Emacs has other keymaps that are used in a different way---translating
654 events within @code{read-key-sequence}.  @xref{Translation Keymaps}.
655
656   @xref{Standard Keymaps}, for a list of standard keymaps.
657
658 @defun current-active-maps &optional olp
659 This returns the list of active keymaps that would be used by the
660 command loop in the current circumstances to look up a key sequence.
661 Normally it ignores @code{overriding-local-map} and
662 @code{overriding-terminal-local-map}, but if @var{olp} is
663 non-@code{nil} then it pays attention to them.
664 @end defun
665
666 @defun key-binding key &optional accept-defaults no-remap position
667 This function returns the binding for @var{key} according to the
668 current active keymaps.  The result is @code{nil} if @var{key} is
669 undefined in the keymaps.
670
671 The argument @var{accept-defaults} controls checking for default
672 bindings, as in @code{lookup-key} (above).
673
674 When commands are remapped (@pxref{Remapping Commands}),
675 @code{key-binding} normally processes command remappings so as to
676 returns the remapped command that will actually be executed.  However,
677 if @var{no-remap} is non-@code{nil}, @code{key-binding} ignores
678 remappings and returns the binding directly specified for @var{key}.
679
680 If @var{key} starts with a mouse event (perhaps following a prefix
681 event), the maps to be consulted are determined based on the event's
682 position.  Otherwise, they are determined based on the value of point.
683 However, you can override either of them by specifying @var{position}.
684 If @var{position} is non-@code{nil}, it should be either a buffer
685 position or an event position like the value of @code{event-start}.
686 Then the maps consulted are determined based on @var{position}.
687
688 An error is signaled if @var{key} is not a string or a vector.
689
690 @example
691 @group
692 (key-binding "\C-x\C-f")
693     @result{} find-file
694 @end group
695 @end example
696 @end defun
697
698 @node Searching Keymaps
699 @section Searching the Active Keymaps
700 @cindex searching active keymaps for keys
701
702   After translation of event subsequences (@pxref{Translation
703 Keymaps}) Emacs looks for them in the active keymaps.  Here is a
704 pseudo-Lisp description of the order and conditions for searching
705 them:
706
707 @lisp
708 (or (if overriding-terminal-local-map
709         (@var{find-in} overriding-terminal-local-map)
710       (if overriding-local-map
711           (@var{find-in} overriding-local-map)
712         (or (@var{find-in} (get-char-property (point) 'keymap))
713             (@var{find-in-any} emulation-mode-map-alists)
714             (@var{find-in-any} minor-mode-overriding-map-alist)
715             (@var{find-in-any} minor-mode-map-alist)
716             (if (get-text-property (point) 'local-map)
717                 (@var{find-in} (get-char-property (point) 'local-map))
718               (@var{find-in} (current-local-map))))))
719     (@var{find-in} (current-global-map)))
720 @end lisp
721
722 @noindent
723 The @var{find-in} and @var{find-in-any} are pseudo functions that
724 search in one keymap and in an alist of keymaps, respectively.
725 (Searching a single keymap for a binding is called @dfn{key lookup};
726 see @ref{Key Lookup}.)  If the key sequence starts with a mouse event,
727 or a symbolic prefix event followed by a mouse event, that event's
728 position is used instead of point and the current buffer.  Mouse
729 events on an embedded string use non-@code{nil} text properties from
730 that string instead of the buffer.
731
732 @enumerate
733 @item
734 The function finally found may be remapped
735 (@pxref{Remapping Commands}).
736
737 @item
738 Characters that are bound to @code{self-insert-command} are translated
739 according to @code{translation-table-for-input} before insertion.
740
741 @item
742 @code{current-active-maps} returns a list of the
743 currently active keymaps at point.
744
745 @item
746 When a match is found (@pxref{Key Lookup}), if the binding in the
747 keymap is a function, the search is over.  However if the keymap entry
748 is a symbol with a value or a string, Emacs replaces the input key
749 sequences with the variable's value or the string, and restarts the
750 search of the active keymaps.
751 @end enumerate
752
753 @node Controlling Active Maps
754 @section Controlling the Active Keymaps
755
756 @defvar global-map
757 This variable contains the default global keymap that maps Emacs
758 keyboard input to commands.  The global keymap is normally this
759 keymap.  The default global keymap is a full keymap that binds
760 @code{self-insert-command} to all of the printing characters.
761
762 It is normal practice to change the bindings in the global keymap, but you
763 should not assign this variable any value other than the keymap it starts
764 out with.
765 @end defvar
766
767 @defun current-global-map
768 This function returns the current global keymap.  This is the
769 same as the value of @code{global-map} unless you change one or the
770 other.
771
772 @example
773 @group
774 (current-global-map)
775 @result{} (keymap [set-mark-command beginning-of-line @dots{}
776             delete-backward-char])
777 @end group
778 @end example
779 @end defun
780
781 @defun current-local-map
782 This function returns the current buffer's local keymap, or @code{nil}
783 if it has none.  In the following example, the keymap for the
784 @samp{*scratch*} buffer (using Lisp Interaction mode) is a sparse keymap
785 in which the entry for @key{ESC}, @acronym{ASCII} code 27, is another sparse
786 keymap.
787
788 @example
789 @group
790 (current-local-map)
791 @result{} (keymap
792     (10 . eval-print-last-sexp)
793     (9 . lisp-indent-line)
794     (127 . backward-delete-char-untabify)
795 @end group
796 @group
797     (27 keymap
798         (24 . eval-defun)
799         (17 . indent-sexp)))
800 @end group
801 @end example
802 @end defun
803
804 @defun current-minor-mode-maps
805 This function returns a list of the keymaps of currently enabled minor modes.
806 @end defun
807
808 @defun use-global-map keymap
809 This function makes @var{keymap} the new current global keymap.  It
810 returns @code{nil}.
811
812 It is very unusual to change the global keymap.
813 @end defun
814
815 @defun use-local-map keymap
816 This function makes @var{keymap} the new local keymap of the current
817 buffer.  If @var{keymap} is @code{nil}, then the buffer has no local
818 keymap.  @code{use-local-map} returns @code{nil}.  Most major mode
819 commands use this function.
820 @end defun
821
822 @c Emacs 19 feature
823 @defvar minor-mode-map-alist
824 @anchor{Definition of minor-mode-map-alist}
825 This variable is an alist describing keymaps that may or may not be
826 active according to the values of certain variables.  Its elements look
827 like this:
828
829 @example
830 (@var{variable} . @var{keymap})
831 @end example
832
833 The keymap @var{keymap} is active whenever @var{variable} has a
834 non-@code{nil} value.  Typically @var{variable} is the variable that
835 enables or disables a minor mode.  @xref{Keymaps and Minor Modes}.
836
837 Note that elements of @code{minor-mode-map-alist} do not have the same
838 structure as elements of @code{minor-mode-alist}.  The map must be the
839 @sc{cdr} of the element; a list with the map as the second element will
840 not do.  The @sc{cdr} can be either a keymap (a list) or a symbol whose
841 function definition is a keymap.
842
843 When more than one minor mode keymap is active, the earlier one in
844 @code{minor-mode-map-alist} takes priority.  But you should design
845 minor modes so that they don't interfere with each other.  If you do
846 this properly, the order will not matter.
847
848 See @ref{Keymaps and Minor Modes}, for more information about minor
849 modes.  See also @code{minor-mode-key-binding} (@pxref{Functions for Key
850 Lookup}).
851 @end defvar
852
853 @defvar minor-mode-overriding-map-alist
854 This variable allows major modes to override the key bindings for
855 particular minor modes.  The elements of this alist look like the
856 elements of @code{minor-mode-map-alist}: @code{(@var{variable}
857 . @var{keymap})}.
858
859 If a variable appears as an element of
860 @code{minor-mode-overriding-map-alist}, the map specified by that
861 element totally replaces any map specified for the same variable in
862 @code{minor-mode-map-alist}.
863
864 @code{minor-mode-overriding-map-alist} is automatically buffer-local in
865 all buffers.
866 @end defvar
867
868 @defvar overriding-local-map
869 If non-@code{nil}, this variable holds a keymap to use instead of the
870 buffer's local keymap, any text property or overlay keymaps, and any
871 minor mode keymaps.  This keymap, if specified, overrides all other
872 maps that would have been active, except for the current global map.
873 @end defvar
874
875 @defvar overriding-terminal-local-map
876 If non-@code{nil}, this variable holds a keymap to use instead of
877 @code{overriding-local-map}, the buffer's local keymap, text property
878 or overlay keymaps, and all the minor mode keymaps.
879
880 This variable is always local to the current terminal and cannot be
881 buffer-local.  @xref{Multiple Displays}.  It is used to implement
882 incremental search mode.
883 @end defvar
884
885 @defvar overriding-local-map-menu-flag
886 If this variable is non-@code{nil}, the value of
887 @code{overriding-local-map} or @code{overriding-terminal-local-map} can
888 affect the display of the menu bar.  The default value is @code{nil}, so
889 those map variables have no effect on the menu bar.
890
891 Note that these two map variables do affect the execution of key
892 sequences entered using the menu bar, even if they do not affect the
893 menu bar display.  So if a menu bar key sequence comes in, you should
894 clear the variables before looking up and executing that key sequence.
895 Modes that use the variables would typically do this anyway; normally
896 they respond to events that they do not handle by ``unreading'' them and
897 exiting.
898 @end defvar
899
900 @defvar special-event-map
901 This variable holds a keymap for special events.  If an event type has a
902 binding in this keymap, then it is special, and the binding for the
903 event is run directly by @code{read-event}.  @xref{Special Events}.
904 @end defvar
905
906 @defvar emulation-mode-map-alists
907 This variable holds a list of keymap alists to use for emulations
908 modes.  It is intended for modes or packages using multiple minor-mode
909 keymaps.  Each element is a keymap alist which has the same format and
910 meaning as @code{minor-mode-map-alist}, or a symbol with a variable
911 binding which is such an alist.  The ``active'' keymaps in each alist
912 are used before @code{minor-mode-map-alist} and
913 @code{minor-mode-overriding-map-alist}.
914 @end defvar
915
916 @node Key Lookup
917 @section Key Lookup
918 @cindex key lookup
919 @cindex keymap entry
920
921   @dfn{Key lookup} is the process of finding the binding of a key
922 sequence from a given keymap.  The execution or use of the binding is
923 not part of key lookup.
924
925   Key lookup uses just the event type of each event in the key sequence;
926 the rest of the event is ignored.  In fact, a key sequence used for key
927 lookup may designate a mouse event with just its types (a symbol)
928 instead of the entire event (a list).  @xref{Input Events}.  Such
929 a ``key sequence'' is insufficient for @code{command-execute} to run,
930 but it is sufficient for looking up or rebinding a key.
931
932   When the key sequence consists of multiple events, key lookup
933 processes the events sequentially: the binding of the first event is
934 found, and must be a keymap; then the second event's binding is found in
935 that keymap, and so on until all the events in the key sequence are used
936 up.  (The binding thus found for the last event may or may not be a
937 keymap.)  Thus, the process of key lookup is defined in terms of a
938 simpler process for looking up a single event in a keymap.  How that is
939 done depends on the type of object associated with the event in that
940 keymap.
941
942   Let's use the term @dfn{keymap entry} to describe the value found by
943 looking up an event type in a keymap.  (This doesn't include the item
944 string and other extra elements in a keymap element for a menu item, because
945 @code{lookup-key} and other key lookup functions don't include them in
946 the returned value.)  While any Lisp object may be stored in a keymap
947 as a keymap entry, not all make sense for key lookup.  Here is a table
948 of the meaningful types of keymap entries:
949
950 @table @asis
951 @item @code{nil}
952 @cindex @code{nil} in keymap
953 @code{nil} means that the events used so far in the lookup form an
954 undefined key.  When a keymap fails to mention an event type at all, and
955 has no default binding, that is equivalent to a binding of @code{nil}
956 for that event type.
957
958 @item @var{command}
959 @cindex command in keymap
960 The events used so far in the lookup form a complete key,
961 and @var{command} is its binding.  @xref{What Is a Function}.
962
963 @item @var{array}
964 @cindex string in keymap
965 The array (either a string or a vector) is a keyboard macro.  The events
966 used so far in the lookup form a complete key, and the array is its
967 binding.  See @ref{Keyboard Macros}, for more information.
968
969 @item @var{keymap}
970 @cindex keymap in keymap
971 The events used so far in the lookup form a prefix key.  The next
972 event of the key sequence is looked up in @var{keymap}.
973
974 @item @var{list}
975 @cindex list in keymap
976 The meaning of a list depends on what it contains:
977
978 @itemize @bullet
979 @item
980 If the @sc{car} of @var{list} is the symbol @code{keymap}, then the list
981 is a keymap, and is treated as a keymap (see above).
982
983 @item
984 @cindex @code{lambda} in keymap
985 If the @sc{car} of @var{list} is @code{lambda}, then the list is a
986 lambda expression.  This is presumed to be a function, and is treated
987 as such (see above).  In order to execute properly as a key binding,
988 this function must be a command---it must have an @code{interactive}
989 specification.  @xref{Defining Commands}.
990
991 @item
992 If the @sc{car} of @var{list} is a keymap and the @sc{cdr} is an event
993 type, then this is an @dfn{indirect entry}:
994
995 @example
996 (@var{othermap} . @var{othertype})
997 @end example
998
999 When key lookup encounters an indirect entry, it looks up instead the
1000 binding of @var{othertype} in @var{othermap} and uses that.
1001
1002 This feature permits you to define one key as an alias for another key.
1003 For example, an entry whose @sc{car} is the keymap called @code{esc-map}
1004 and whose @sc{cdr} is 32 (the code for @key{SPC}) means, ``Use the global
1005 binding of @kbd{Meta-@key{SPC}}, whatever that may be.''
1006 @end itemize
1007
1008 @item @var{symbol}
1009 @cindex symbol in keymap
1010 The function definition of @var{symbol} is used in place of
1011 @var{symbol}.  If that too is a symbol, then this process is repeated,
1012 any number of times.  Ultimately this should lead to an object that is
1013 a keymap, a command, or a keyboard macro.  A list is allowed if it is a
1014 keymap or a command, but indirect entries are not understood when found
1015 via symbols.
1016
1017 Note that keymaps and keyboard macros (strings and vectors) are not
1018 valid functions, so a symbol with a keymap, string, or vector as its
1019 function definition is invalid as a function.  It is, however, valid as
1020 a key binding.  If the definition is a keyboard macro, then the symbol
1021 is also valid as an argument to @code{command-execute}
1022 (@pxref{Interactive Call}).
1023
1024 @cindex @code{undefined} in keymap
1025 The symbol @code{undefined} is worth special mention: it means to treat
1026 the key as undefined.  Strictly speaking, the key is defined, and its
1027 binding is the command @code{undefined}; but that command does the same
1028 thing that is done automatically for an undefined key: it rings the bell
1029 (by calling @code{ding}) but does not signal an error.
1030
1031 @cindex preventing prefix key
1032 @code{undefined} is used in local keymaps to override a global key
1033 binding and make the key ``undefined'' locally.  A local binding of
1034 @code{nil} would fail to do this because it would not override the
1035 global binding.
1036
1037 @item @var{anything else}
1038 If any other type of object is found, the events used so far in the
1039 lookup form a complete key, and the object is its binding, but the
1040 binding is not executable as a command.
1041 @end table
1042
1043   In short, a keymap entry may be a keymap, a command, a keyboard macro,
1044 a symbol that leads to one of them, or an indirection or @code{nil}.
1045 Here is an example of a sparse keymap with two characters bound to
1046 commands and one bound to another keymap.  This map is the normal value
1047 of @code{emacs-lisp-mode-map}.  Note that 9 is the code for @key{TAB},
1048 127 for @key{DEL}, 27 for @key{ESC}, 17 for @kbd{C-q} and 24 for
1049 @kbd{C-x}.
1050
1051 @example
1052 @group
1053 (keymap (9 . lisp-indent-line)
1054         (127 . backward-delete-char-untabify)
1055         (27 keymap (17 . indent-sexp) (24 . eval-defun)))
1056 @end group
1057 @end example
1058
1059 @node Functions for Key Lookup
1060 @section Functions for Key Lookup
1061
1062   Here are the functions and variables pertaining to key lookup.
1063
1064 @defun lookup-key keymap key &optional accept-defaults
1065 This function returns the definition of @var{key} in @var{keymap}.  All
1066 the other functions described in this chapter that look up keys use
1067 @code{lookup-key}.  Here are examples:
1068
1069 @example
1070 @group
1071 (lookup-key (current-global-map) "\C-x\C-f")
1072     @result{} find-file
1073 @end group
1074 @group
1075 (lookup-key (current-global-map) (kbd "C-x C-f"))
1076     @result{} find-file
1077 @end group
1078 @group
1079 (lookup-key (current-global-map) "\C-x\C-f12345")
1080     @result{} 2
1081 @end group
1082 @end example
1083
1084 If the string or vector @var{key} is not a valid key sequence according
1085 to the prefix keys specified in @var{keymap}, it must be ``too long''
1086 and have extra events at the end that do not fit into a single key
1087 sequence.  Then the value is a number, the number of events at the front
1088 of @var{key} that compose a complete key.
1089
1090 @c Emacs 19 feature
1091 If @var{accept-defaults} is non-@code{nil}, then @code{lookup-key}
1092 considers default bindings as well as bindings for the specific events
1093 in @var{key}.  Otherwise, @code{lookup-key} reports only bindings for
1094 the specific sequence @var{key}, ignoring default bindings except when
1095 you explicitly ask about them.  (To do this, supply @code{t} as an
1096 element of @var{key}; see @ref{Format of Keymaps}.)
1097
1098 If @var{key} contains a meta character (not a function key), that
1099 character is implicitly replaced by a two-character sequence: the value
1100 of @code{meta-prefix-char}, followed by the corresponding non-meta
1101 character.  Thus, the first example below is handled by conversion into
1102 the second example.
1103
1104 @example
1105 @group
1106 (lookup-key (current-global-map) "\M-f")
1107     @result{} forward-word
1108 @end group
1109 @group
1110 (lookup-key (current-global-map) "\ef")
1111     @result{} forward-word
1112 @end group
1113 @end example
1114
1115 Unlike @code{read-key-sequence}, this function does not modify the
1116 specified events in ways that discard information (@pxref{Key Sequence
1117 Input}).  In particular, it does not convert letters to lower case and
1118 it does not change drag events to clicks.
1119 @end defun
1120
1121 @deffn Command undefined
1122 Used in keymaps to undefine keys.  It calls @code{ding}, but does
1123 not cause an error.
1124 @end deffn
1125
1126 @defun local-key-binding key &optional accept-defaults
1127 This function returns the binding for @var{key} in the current
1128 local keymap, or @code{nil} if it is undefined there.
1129
1130 @c Emacs 19 feature
1131 The argument @var{accept-defaults} controls checking for default bindings,
1132 as in @code{lookup-key} (above).
1133 @end defun
1134
1135 @defun global-key-binding key &optional accept-defaults
1136 This function returns the binding for command @var{key} in the
1137 current global keymap, or @code{nil} if it is undefined there.
1138
1139 @c Emacs 19 feature
1140 The argument @var{accept-defaults} controls checking for default bindings,
1141 as in @code{lookup-key} (above).
1142 @end defun
1143
1144 @c Emacs 19 feature
1145 @defun minor-mode-key-binding key &optional accept-defaults
1146 This function returns a list of all the active minor mode bindings of
1147 @var{key}.  More precisely, it returns an alist of pairs
1148 @code{(@var{modename} . @var{binding})}, where @var{modename} is the
1149 variable that enables the minor mode, and @var{binding} is @var{key}'s
1150 binding in that mode.  If @var{key} has no minor-mode bindings, the
1151 value is @code{nil}.
1152
1153 If the first binding found is not a prefix definition (a keymap or a
1154 symbol defined as a keymap), all subsequent bindings from other minor
1155 modes are omitted, since they would be completely shadowed.  Similarly,
1156 the list omits non-prefix bindings that follow prefix bindings.
1157
1158 The argument @var{accept-defaults} controls checking for default
1159 bindings, as in @code{lookup-key} (above).
1160 @end defun
1161
1162 @defvar meta-prefix-char
1163 @cindex @key{ESC}
1164 This variable is the meta-prefix character code.  It is used for
1165 translating a meta character to a two-character sequence so it can be
1166 looked up in a keymap.  For useful results, the value should be a
1167 prefix event (@pxref{Prefix Keys}).  The default value is 27, which is
1168 the @acronym{ASCII} code for @key{ESC}.
1169
1170 As long as the value of @code{meta-prefix-char} remains 27, key lookup
1171 translates @kbd{M-b} into @kbd{@key{ESC} b}, which is normally defined
1172 as the @code{backward-word} command.  However, if you were to set
1173 @code{meta-prefix-char} to 24, the code for @kbd{C-x}, then Emacs will
1174 translate @kbd{M-b} into @kbd{C-x b}, whose standard binding is the
1175 @code{switch-to-buffer} command.  (Don't actually do this!)  Here is an
1176 illustration of what would happen:
1177
1178 @smallexample
1179 @group
1180 meta-prefix-char                    ; @r{The default value.}
1181      @result{} 27
1182 @end group
1183 @group
1184 (key-binding "\M-b")
1185      @result{} backward-word
1186 @end group
1187 @group
1188 ?\C-x                               ; @r{The print representation}
1189      @result{} 24                          ;   @r{of a character.}
1190 @end group
1191 @group
1192 (setq meta-prefix-char 24)
1193      @result{} 24
1194 @end group
1195 @group
1196 (key-binding "\M-b")
1197      @result{} switch-to-buffer            ; @r{Now, typing @kbd{M-b} is}
1198                                     ;   @r{like typing @kbd{C-x b}.}
1199
1200 (setq meta-prefix-char 27)          ; @r{Avoid confusion!}
1201      @result{} 27                          ; @r{Restore the default value!}
1202 @end group
1203 @end smallexample
1204
1205 This translation of one event into two happens only for characters, not
1206 for other kinds of input events.  Thus, @kbd{M-@key{F1}}, a function
1207 key, is not converted into @kbd{@key{ESC} @key{F1}}.
1208 @end defvar
1209
1210 @node Changing Key Bindings
1211 @section Changing Key Bindings
1212 @cindex cha