merge trun
[bpt/emacs.git] / doc / lispref / keymaps.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1994, 1998-2012 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @node Keymaps
6 @chapter Keymaps
7 @cindex keymap
8
9 The command bindings of input events are recorded in data structures
10 called @dfn{keymaps}. Each entry in a keymap associates (or
11 @dfn{binds}) an individual event type, either to another keymap or to
12 a command. When an event type is bound to a keymap, that keymap is
13 used to look up the next input event; this continues until a command
14 is found. The whole process is called @dfn{key lookup}.
15
16 @menu
17 * Key Sequences:: Key sequences as Lisp objects.
18 * Keymap Basics:: Basic concepts of keymaps.
19 * Format of Keymaps:: What a keymap looks like as a Lisp object.
20 * Creating Keymaps:: Functions to create and copy keymaps.
21 * Inheritance and Keymaps:: How one keymap can inherit the bindings
22 of another keymap.
23 * Prefix Keys:: Defining a key with a keymap as its definition.
24 * Active Keymaps:: How Emacs searches the active keymaps
25 for a key binding.
26 * Searching Keymaps:: A pseudo-Lisp summary of searching active maps.
27 * Controlling Active Maps:: Each buffer has a local keymap
28 to override the standard (global) bindings.
29 A minor mode can also override them.
30 * Key Lookup:: Finding a key's binding in one keymap.
31 * Functions for Key Lookup:: How to request key lookup.
32 * Changing Key Bindings:: Redefining a key in a keymap.
33 * Remapping Commands:: A keymap can translate one command to another.
34 * Translation Keymaps:: Keymaps for translating sequences of events.
35 * Key Binding Commands:: Interactive interfaces for redefining keys.
36 * Scanning Keymaps:: Looking through all keymaps, for printing help.
37 * Menu Keymaps:: Defining a menu as a keymap.
38 @end menu
39
40 @node Key Sequences
41 @section Key Sequences
42 @cindex key
43 @cindex keystroke
44 @cindex key sequence
45
46 A @dfn{key sequence}, or @dfn{key} for short, is a sequence of one
47 or more input events that form a unit. Input events include
48 characters, function keys, mouse actions, or system events external to
49 Emacs, such as @code{iconify-frame} (@pxref{Input Events}).
50 The Emacs Lisp representation for a key sequence is a string or
51 vector. Unless otherwise stated, any Emacs Lisp function that accepts
52 a key sequence as an argument can handle both representations.
53
54 In the string representation, alphanumeric characters ordinarily
55 stand for themselves; for example, @code{"a"} represents @kbd{a}
56 and @code{"2"} represents @kbd{2}. Control character events are
57 prefixed by the substring @code{"\C-"}, and meta characters by
58 @code{"\M-"}; for example, @code{"\C-x"} represents the key @kbd{C-x}.
59 In addition, the @key{TAB}, @key{RET}, @key{ESC}, and @key{DEL} events
60 are represented by @code{"\t"}, @code{"\r"}, @code{"\e"}, and
61 @code{"\d"} respectively. The string representation of a complete key
62 sequence is the concatenation of the string representations of the
63 constituent events; thus, @code{"\C-xl"} represents the key sequence
64 @kbd{C-x l}.
65
66 Key sequences containing function keys, mouse button events, system
67 events, or non-@acronym{ASCII} characters such as @kbd{C-=} or
68 @kbd{H-a} cannot be represented as strings; they have to be
69 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 use the same syntax as in the buffer invoked
82 by the @kbd{C-x C-k @key{RET}} (@code{kmacro-edit-macro}) command; in
83 particular, you must surround function key names with
84 @samp{<@dots{}>}. @xref{Edit Keyboard Macro,,, emacs, The GNU Emacs
85 Manual}.
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} .@: @var{binding})
178 This specifies a binding which is also a simple menu item that
179 displays as @var{item-name} in the menu. @xref{Simple Menu Items}.
180
181 @item (@var{type} @var{item-name} @var{help-string} .@: @var{binding})
182 This is a simple menu item with help string @var{help-string}.
183
184 @item (@var{type} menu-item .@: @var{details})
185 This specifies a binding which is also an extended menu item. This
186 allows use of other features. @xref{Extended Menu Items}.
187
188 @item (t .@: @var{binding})
189 @cindex default key binding
190 This specifies a @dfn{default key binding}; any event not bound by other
191 elements of the keymap is given @var{binding} as its binding. Default
192 bindings allow a keymap to bind all possible event types without having
193 to enumerate all of them. A keymap that has a default binding
194 completely masks any lower-precedence keymap, except for events
195 explicitly bound to @code{nil} (see below).
196
197 @item @var{char-table}
198 If an element of a keymap is a char-table, it counts as holding
199 bindings for all character events with no modifier bits
200 (@pxref{modifier bits}): element @var{n} is the binding for the
201 character with code @var{n}. This is a compact way to record lots of
202 bindings. A keymap with such a char-table is called a @dfn{full
203 keymap}. Other keymaps are called @dfn{sparse keymaps}.
204
205 @item @var{string}
206 @cindex keymap prompt string
207 @cindex overall prompt string
208 @cindex prompt string of keymap
209 Aside from elements that specify bindings for keys, a keymap can also
210 have a string as an element. This is called the @dfn{overall prompt
211 string} and makes it possible to use the keymap as a menu.
212 @xref{Defining Menus}.
213
214 @item (keymap @dots{})
215 If an element of a keymap is itself a keymap, it counts as if this inner keymap
216 were inlined in the outer keymap. This is used for multiple-inheritance, such
217 as in @code{make-composed-keymap}.
218 @end table
219
220 When the binding is @code{nil}, it doesn't constitute a definition
221 but it does take precedence over a default binding or a binding in the
222 parent keymap. On the other hand, a binding of @code{nil} does
223 @emph{not} override lower-precedence keymaps; thus, if the local map
224 gives a binding of @code{nil}, Emacs uses the binding from the
225 global map.
226
227 @cindex meta characters lookup
228 Keymaps do not directly record bindings for the meta characters.
229 Instead, meta characters are regarded for purposes of key lookup as
230 sequences of two characters, the first of which is @key{ESC} (or
231 whatever is currently the value of @code{meta-prefix-char}). Thus, the
232 key @kbd{M-a} is internally represented as @kbd{@key{ESC} a}, and its
233 global binding is found at the slot for @kbd{a} in @code{esc-map}
234 (@pxref{Prefix Keys}).
235
236 This conversion applies only to characters, not to function keys or
237 other input events; thus, @kbd{M-@key{end}} has nothing to do with
238 @kbd{@key{ESC} @key{end}}.
239
240 Here as an example is the local keymap for Lisp mode, a sparse
241 keymap. It defines bindings for @key{DEL}, @kbd{C-c C-z},
242 @kbd{C-M-q}, and @kbd{C-M-x} (the actual value also contains a menu
243 binding, which is omitted here for the sake of brevity).
244
245 @example
246 @group
247 lisp-mode-map
248 @result{}
249 @end group
250 @group
251 (keymap
252 (3 keymap
253 ;; @kbd{C-c C-z}
254 (26 . run-lisp))
255 @end group
256 @group
257 (27 keymap
258 ;; @r{@kbd{C-M-x}, treated as @kbd{@key{ESC} C-x}}
259 (24 . lisp-send-defun))
260 @end group
261 @group
262 ;; @r{This part is inherited from @code{lisp-mode-shared-map}.}
263 keymap
264 ;; @key{DEL}
265 (127 . backward-delete-char-untabify)
266 @end group
267 @group
268 (27 keymap
269 ;; @r{@kbd{C-M-q}, treated as @kbd{@key{ESC} C-q}}
270 (17 . indent-sexp)))
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 inheritance, keymap
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 @cindex keymap inheritance from multiple maps
437 Sometimes you want to make a keymap that inherits from more than one
438 map. You can use the function @code{make-composed-keymap} for this.
439
440 @defun make-composed-keymap maps &optional parent
441 This function returns a new keymap composed of the existing keymap(s)
442 @var{maps}, and optionally inheriting from a parent keymap
443 @var{parent}. @var{maps} can be a single keymap or a list of more
444 than one. When looking up a key in the resulting new map, Emacs
445 searches in each of the @var{maps} in turn, and then in @var{parent},
446 stopping at the first match. A @code{nil} binding in any one of
447 @var{maps} overrides any binding in @var{parent}, but it does not
448 override any non-@code{nil} binding in any other of the @var{maps}.
449 @end defun
450
451 @noindent For example, here is how Emacs sets the parent of
452 @code{help-mode-map}, such that it inherits from both
453 @code{button-buffer-map} and @code{special-mode-map}:
454
455 @example
456 (defvar help-mode-map
457 (let ((map (make-sparse-keymap)))
458 (set-keymap-parent map
459 (make-composed-keymap button-buffer-map special-mode-map))
460 ... map) ... )
461 @end example
462
463
464 @node Prefix Keys
465 @section Prefix Keys
466 @cindex prefix key
467
468 A @dfn{prefix key} is a key sequence whose binding is a keymap. The
469 keymap defines what to do with key sequences that extend the prefix key.
470 For example, @kbd{C-x} is a prefix key, and it uses a keymap that is
471 also stored in the variable @code{ctl-x-map}. This keymap defines
472 bindings for key sequences starting with @kbd{C-x}.
473
474 Some of the standard Emacs prefix keys use keymaps that are
475 also found in Lisp variables:
476
477 @itemize @bullet
478 @item
479 @vindex esc-map
480 @findex ESC-prefix
481 @code{esc-map} is the global keymap for the @key{ESC} prefix key. Thus,
482 the global definitions of all meta characters are actually found here.
483 This map is also the function definition of @code{ESC-prefix}.
484
485 @item
486 @cindex @kbd{C-h}
487 @code{help-map} is the global keymap for the @kbd{C-h} prefix key.
488
489 @item
490 @cindex @kbd{C-c}
491 @vindex mode-specific-map
492 @code{mode-specific-map} is the global keymap for the prefix key
493 @kbd{C-c}. This map is actually global, not mode-specific, but its name
494 provides useful information about @kbd{C-c} in the output of @kbd{C-h b}
495 (@code{display-bindings}), since the main use of this prefix key is for
496 mode-specific bindings.
497
498 @item
499 @cindex @kbd{C-x}
500 @vindex ctl-x-map
501 @findex Control-X-prefix
502 @code{ctl-x-map} is the global keymap used for the @kbd{C-x} prefix key.
503 This map is found via the function cell of the symbol
504 @code{Control-X-prefix}.
505
506 @item
507 @cindex @kbd{C-x @key{RET}}
508 @vindex mule-keymap
509 @code{mule-keymap} is the global keymap used for the @kbd{C-x @key{RET}}
510 prefix key.
511
512 @item
513 @cindex @kbd{C-x 4}
514 @vindex ctl-x-4-map
515 @code{ctl-x-4-map} is the global keymap used for the @kbd{C-x 4} prefix
516 key.
517
518 @item
519 @cindex @kbd{C-x 5}
520 @vindex ctl-x-5-map
521 @code{ctl-x-5-map} is the global keymap used for the @kbd{C-x 5} prefix
522 key.
523
524 @item
525 @cindex @kbd{C-x 6}
526 @vindex 2C-mode-map
527 @code{2C-mode-map} is the global keymap used for the @kbd{C-x 6} prefix
528 key.
529
530 @item
531 @cindex @kbd{C-x v}
532 @vindex vc-prefix-map
533 @code{vc-prefix-map} is the global keymap used for the @kbd{C-x v} prefix
534 key.
535
536 @item
537 @cindex @kbd{M-g}
538 @vindex goto-map
539 @code{goto-map} is the global keymap used for the @kbd{M-g} prefix
540 key.
541
542 @item
543 @cindex @kbd{M-s}
544 @vindex search-map
545 @code{search-map} is the global keymap used for the @kbd{M-s} prefix
546 key.
547
548 @item
549 @cindex @kbd{M-o}
550 @vindex facemenu-keymap
551 @code{facemenu-keymap} is the global keymap used for the @kbd{M-o}
552 prefix key.
553
554 @item
555 The other Emacs prefix keys are @kbd{C-x @@}, @kbd{C-x a i}, @kbd{C-x
556 @key{ESC}} and @kbd{@key{ESC} @key{ESC}}. They use keymaps that have
557 no special names.
558 @end itemize
559
560 The keymap binding of a prefix key is used for looking up the event
561 that follows the prefix key. (It may instead be a symbol whose function
562 definition is a keymap. The effect is the same, but the symbol serves
563 as a name for the prefix key.) Thus, the binding of @kbd{C-x} is the
564 symbol @code{Control-X-prefix}, whose function cell holds the keymap
565 for @kbd{C-x} commands. (The same keymap is also the value of
566 @code{ctl-x-map}.)
567
568 Prefix key definitions can appear in any active keymap. The
569 definitions of @kbd{C-c}, @kbd{C-x}, @kbd{C-h} and @key{ESC} as prefix
570 keys appear in the global map, so these prefix keys are always
571 available. Major and minor modes can redefine a key as a prefix by
572 putting a prefix key definition for it in the local map or the minor
573 mode's map. @xref{Active Keymaps}.
574
575 If a key is defined as a prefix in more than one active map, then its
576 various definitions are in effect merged: the commands defined in the
577 minor mode keymaps come first, followed by those in the local map's
578 prefix definition, and then by those from the global map.
579
580 In the following example, we make @kbd{C-p} a prefix key in the local
581 keymap, in such a way that @kbd{C-p} is identical to @kbd{C-x}. Then
582 the binding for @kbd{C-p C-f} is the function @code{find-file}, just
583 like @kbd{C-x C-f}. The key sequence @kbd{C-p 6} is not found in any
584 active keymap.
585
586 @example
587 @group
588 (use-local-map (make-sparse-keymap))
589 @result{} nil
590 @end group
591 @group
592 (local-set-key "\C-p" ctl-x-map)
593 @result{} nil
594 @end group
595 @group
596 (key-binding "\C-p\C-f")
597 @result{} find-file
598 @end group
599
600 @group
601 (key-binding "\C-p6")
602 @result{} nil
603 @end group
604 @end example
605
606 @defun define-prefix-command symbol &optional mapvar prompt
607 @cindex prefix command
608 @anchor{Definition of define-prefix-command}
609 This function prepares @var{symbol} for use as a prefix key's binding:
610 it creates a sparse keymap and stores it as @var{symbol}'s function
611 definition. Subsequently binding a key sequence to @var{symbol} will
612 make that key sequence into a prefix key. The return value is @code{symbol}.
613
614 This function also sets @var{symbol} as a variable, with the keymap as
615 its value. But if @var{mapvar} is non-@code{nil}, it sets @var{mapvar}
616 as a variable instead.
617
618 If @var{prompt} is non-@code{nil}, that becomes the overall prompt
619 string for the keymap. The prompt string should be given for menu keymaps
620 (@pxref{Defining Menus}).
621 @end defun
622
623 @node Active Keymaps
624 @section Active Keymaps
625 @cindex active keymap
626 @cindex global keymap
627 @cindex local keymap
628
629 Emacs normally contains many keymaps; at any given time, just a few
630 of them are @dfn{active}, meaning that they participate in the
631 interpretation of user input. All the active keymaps are used
632 together to determine what command to execute when a key is entered.
633
634 Normally the active keymaps are the @code{keymap} property keymap,
635 the keymaps of any enabled minor modes, the current buffer's local
636 keymap, and the global keymap, in that order. Emacs searches for each
637 input key sequence in all these keymaps. @xref{Searching Keymaps},
638 for more details of this procedure.
639
640 When the key sequence starts with a mouse event (optionally preceded
641 by a symbolic prefix), the active keymaps are determined based on the
642 position in that event. If the event happened on a string embedded
643 with a @code{display}, @code{before-string}, or @code{after-string}
644 property (@pxref{Special Properties}), the non-@code{nil} map
645 properties of the string override those of the buffer (if the
646 underlying buffer text contains map properties in its text properties
647 or overlays, they are ignored).
648
649 The @dfn{global keymap} holds the bindings of keys that are defined
650 regardless of the current buffer, such as @kbd{C-f}. The variable
651 @code{global-map} holds this keymap, which is always active.
652
653 Each buffer may have another keymap, its @dfn{local keymap}, which
654 may contain new or overriding definitions for keys. The current
655 buffer's local keymap is always active except when
656 @code{overriding-local-map} overrides it. The @code{local-map} text
657 or overlay property can specify an alternative local keymap for certain
658 parts of the buffer; see @ref{Special Properties}.
659
660 Each minor mode can have a keymap; if it does, the keymap is active
661 when the minor mode is enabled. Modes for emulation can specify
662 additional active keymaps through the variable
663 @code{emulation-mode-map-alists}.
664
665 The highest precedence normal keymap comes from the @code{keymap}
666 text or overlay property. If that is non-@code{nil}, it is the first
667 keymap to be processed, in normal circumstances.
668
669 However, there are also special ways for programs to substitute
670 other keymaps for some of those. The variable
671 @code{overriding-local-map}, if non-@code{nil}, specifies a keymap
672 that replaces all the usual active keymaps except the global keymap.
673 Another way to do this is with @code{overriding-terminal-local-map};
674 it operates on a per-terminal basis. These variables are documented
675 below.
676
677 @cindex major mode keymap
678 Since every buffer that uses the same major mode normally uses the
679 same local keymap, you can think of the keymap as local to the mode. A
680 change to the local keymap of a buffer (using @code{local-set-key}, for
681 example) is seen also in the other buffers that share that keymap.
682
683 The local keymaps that are used for Lisp mode and some other major
684 modes exist even if they have not yet been used. These local keymaps are
685 the values of variables such as @code{lisp-mode-map}. For most major
686 modes, which are less frequently used, the local keymap is constructed
687 only when the mode is used for the first time in a session.
688
689 The minibuffer has local keymaps, too; they contain various completion
690 and exit commands. @xref{Intro to Minibuffers}.
691
692 Emacs has other keymaps that are used in a different way---translating
693 events within @code{read-key-sequence}. @xref{Translation Keymaps}.
694
695 @xref{Standard Keymaps}, for a list of some standard keymaps.
696
697 @defun current-active-maps &optional olp position
698 This returns the list of active keymaps that would be used by the
699 command loop in the current circumstances to look up a key sequence.
700 Normally it ignores @code{overriding-local-map} and
701 @code{overriding-terminal-local-map}, but if @var{olp} is non-@code{nil}
702 then it pays attention to them. @var{position} can optionally be either
703 an event position as returned by @code{event-start} or a buffer
704 position, and may change the keymaps as described for
705 @code{key-binding}.
706 @end defun
707
708 @defun key-binding key &optional accept-defaults no-remap position
709 This function returns the binding for @var{key} according to the
710 current active keymaps. The result is @code{nil} if @var{key} is
711 undefined in the keymaps.
712
713 The argument @var{accept-defaults} controls checking for default
714 bindings, as in @code{lookup-key} (@pxref{Functions for Key Lookup}).
715
716 When commands are remapped (@pxref{Remapping Commands}),
717 @code{key-binding} normally processes command remappings so as to
718 return the remapped command that will actually be executed. However,
719 if @var{no-remap} is non-@code{nil}, @code{key-binding} ignores
720 remappings and returns the binding directly specified for @var{key}.
721
722 If @var{key} starts with a mouse event (perhaps following a prefix
723 event), the maps to be consulted are determined based on the event's
724 position. Otherwise, they are determined based on the value of point.
725 However, you can override either of them by specifying @var{position}.
726 If @var{position} is non-@code{nil}, it should be either a buffer
727 position or an event position like the value of @code{event-start}.
728 Then the maps consulted are determined based on @var{position}.
729
730 An error is signaled if @var{key} is not a string or a vector.
731
732 @example
733 @group
734 (key-binding "\C-x\C-f")
735 @result{} find-file
736 @end group
737 @end example
738 @end defun
739
740 @node Searching Keymaps
741 @section Searching the Active Keymaps
742 @cindex searching active keymaps for keys
743
744 After translation of event subsequences (@pxref{Translation
745 Keymaps}) Emacs looks for them in the active keymaps. Here is a
746 pseudo-Lisp description of the order and conditions for searching
747 them:
748
749 @lisp
750 (or (cond
751 (overriding-terminal-local-map
752 (@var{find-in} overriding-terminal-local-map))
753 (overriding-local-map
754 (@var{find-in} overriding-local-map))
755 ((or (@var{find-in} (get-char-property (point) 'keymap))
756 (@var{find-in-any} emulation-mode-map-alists)
757 (@var{find-in-any} minor-mode-overriding-map-alist)
758 (@var{find-in-any} minor-mode-map-alist)
759 (if (get-text-property (point) 'local-map)
760 (@var{find-in} (get-char-property (point) 'local-map))
761 (@var{find-in} (current-local-map))))))
762 (@var{find-in} (current-global-map)))
763 @end lisp
764
765 @noindent
766 @var{find-in} and @var{find-in-any} are pseudo functions that search
767 in one keymap and in an alist of keymaps, respectively. (Searching a
768 single keymap for a binding is called @dfn{key lookup}; see @ref{Key
769 Lookup}.) If the key sequence starts with a mouse event, or a
770 symbolic prefix event followed by a mouse event, that event's position
771 is used instead of point and the current buffer. Mouse events on an
772 embedded string use non-@code{nil} text properties from that string
773 instead of the buffer.
774
775 When a match is found (@pxref{Key Lookup}), if the binding in the
776 keymap is a function, the search is over. However if the keymap entry
777 is a symbol with a value or a string, Emacs replaces the input key
778 sequences with the variable's value or the string, and restarts the
779 search of the active keymaps.
780
781 The function finally found might also be remapped. @xref{Remapping
782 Commands}.
783
784 @node Controlling Active Maps
785 @section Controlling the Active Keymaps
786
787 @defvar global-map
788 This variable contains the default global keymap that maps Emacs
789 keyboard input to commands. The global keymap is normally this
790 keymap. The default global keymap is a full keymap that binds
791 @code{self-insert-command} to all of the printing characters.
792
793 It is normal practice to change the bindings in the global keymap, but you
794 should not assign this variable any value other than the keymap it starts
795 out with.
796 @end defvar
797
798 @defun current-global-map
799 This function returns the current global keymap. This is the same as
800 the value of @code{global-map} unless you change one or the other.
801 The return value is a reference, not a copy; if you use
802 @code{define-key} or other functions on it you will alter global
803 bindings.
804
805 @example
806 @group
807 (current-global-map)
808 @result{} (keymap [set-mark-command beginning-of-line @dots{}
809 delete-backward-char])
810 @end group
811 @end example
812 @end defun
813
814 @defun current-local-map
815 This function returns the current buffer's local keymap, or @code{nil}
816 if it has none. In the following example, the keymap for the
817 @file{*scratch*} buffer (using Lisp Interaction mode) is a sparse keymap
818 in which the entry for @key{ESC}, @acronym{ASCII} code 27, is another sparse
819 keymap.
820
821 @example
822 @group
823 (current-local-map)
824 @result{} (keymap
825 (10 . eval-print-last-sexp)
826 (9 . lisp-indent-line)
827 (127 . backward-delete-char-untabify)
828 @end group
829 @group
830 (27 keymap
831 (24 . eval-defun)
832 (17 . indent-sexp)))
833 @end group
834 @end example
835 @end defun
836
837 @code{current-local-map} returns a reference to the local keymap, not
838 a copy of it; if you use @code{define-key} or other functions on it
839 you will alter local bindings.
840
841 @defun current-minor-mode-maps
842 This function returns a list of the keymaps of currently enabled minor modes.
843 @end defun
844
845 @defun use-global-map keymap
846 This function makes @var{keymap} the new current global keymap. It
847 returns @code{nil}.
848
849 It is very unusual to change the global keymap.
850 @end defun
851
852 @defun use-local-map keymap
853 This function makes @var{keymap} the new local keymap of the current
854 buffer. If @var{keymap} is @code{nil}, then the buffer has no local
855 keymap. @code{use-local-map} returns @code{nil}. Most major mode
856 commands use this function.
857 @end defun
858
859 @c Emacs 19 feature
860 @defvar minor-mode-map-alist
861 @anchor{Definition of minor-mode-map-alist}
862 This variable is an alist describing keymaps that may or may not be
863 active according to the values of certain variables. Its elements look
864 like this:
865
866 @example
867 (@var{variable} . @var{keymap})
868 @end example
869
870 The keymap @var{keymap} is active whenever @var{variable} has a
871 non-@code{nil} value. Typically @var{variable} is the variable that
872 enables or disables a minor mode. @xref{Keymaps and Minor Modes}.
873
874 Note that elements of @code{minor-mode-map-alist} do not have the same
875 structure as elements of @code{minor-mode-alist}. The map must be the
876 @sc{cdr} of the element; a list with the map as the second element will
877 not do. The @sc{cdr} can be either a keymap (a list) or a symbol whose
878 function definition is a keymap.
879
880 When more than one minor mode keymap is active, the earlier one in
881 @code{minor-mode-map-alist} takes priority. But you should design
882 minor modes so that they don't interfere with each other. If you do
883 this properly, the order will not matter.
884
885 See @ref{Keymaps and Minor Modes}, for more information about minor
886 modes. See also @code{minor-mode-key-binding} (@pxref{Functions for Key
887 Lookup}).
888 @end defvar
889
890 @defvar minor-mode-overriding-map-alist
891 This variable allows major modes to override the key bindings for
892 particular minor modes. The elements of this alist look like the
893 elements of @code{minor-mode-map-alist}: @code{(@var{variable}
894 . @var{keymap})}.
895
896 If a variable appears as an element of
897 @code{minor-mode-overriding-map-alist}, the map specified by that
898 element totally replaces any map specified for the same variable in
899 @code{minor-mode-map-alist}.
900
901 @code{minor-mode-overriding-map-alist} is automatically buffer-local in
902 all buffers.
903 @end defvar
904
905 @defvar overriding-local-map
906 If non-@code{nil}, this variable holds a keymap to use instead of the
907 buffer's local keymap, any text property or overlay keymaps, and any
908 minor mode keymaps. This keymap, if specified, overrides all other
909 maps that would have been active, except for the current global map.
910 @end defvar
911
912 @defvar overriding-terminal-local-map
913 If non-@code{nil}, this variable holds a keymap to use instead of
914 @code{overriding-local-map}, the buffer's local keymap, text property
915 or overlay keymaps, and all the minor mode keymaps.
916
917 This variable is always local to the current terminal and cannot be
918 buffer-local. @xref{Multiple Terminals}. It is used to implement
919 incremental search mode.
920 @end defvar
921
922 @defvar overriding-local-map-menu-flag
923 If this variable is non-@code{nil}, the value of
924 @code{overriding-local-map} or @code{overriding-terminal-local-map} can
925 affect the display of the menu bar. The default value is @code{nil}, so
926 those map variables have no effect on the menu bar.
927
928 Note that these two map variables do affect the execution of key
929 sequences entered using the menu bar, even if they do not affect the
930 menu bar display. So if a menu bar key sequence comes in, you should
931 clear the variables before looking up and executing that key sequence.
932 Modes that use the variables would typically do this anyway; normally
933 they respond to events that they do not handle by ``unreading'' them and
934 exiting.
935 @end defvar
936
937 @defvar special-event-map
938 This variable holds a keymap for special events. If an event type has a
939 binding in this keymap, then it is special, and the binding for the
940 event is run directly by @code{read-event}. @xref{Special Events}.
941 @end defvar
942
943 @defvar emulation-mode-map-alists
944 This variable holds a list of keymap alists to use for emulations
945 modes. It is intended for modes or packages using multiple minor-mode
946 keymaps. Each element is a keymap alist which has the same format and
947 meaning as @code{minor-mode-map-alist}, or a symbol with a variable
948 binding which is such an alist. The ``active'' keymaps in each alist
949 are used before @code{minor-mode-map-alist} and
950 @code{minor-mode-overriding-map-alist}.
951 @end defvar
952
953 @node Key Lookup
954 @section Key Lookup
955 @cindex key lookup
956 @cindex keymap entry
957
958 @dfn{Key lookup} is the process of finding the binding of a key
959 sequence from a given keymap. The execution or use of the binding is
960 not part of key lookup.
961
962 Key lookup uses just the event type of each event in the key sequence;
963 the rest of the event is ignored. In fact, a key sequence used for key
964 lookup may designate a mouse event with just its types (a symbol)
965 instead of the entire event (a list). @xref{Input Events}. Such
966 a ``key sequence'' is insufficient for @code{command-execute} to run,
967 but it is sufficient for looking up or rebinding a key.
968
969 When the key sequence consists of multiple events, key lookup
970 processes the events sequentially: the binding of the first event is
971 found, and must be a keymap; then the second event's binding is found in
972 that keymap, and so on until all the events in the key sequence are used
973 up. (The binding thus found for the last event may or may not be a
974 keymap.) Thus, the process of key lookup is defined in terms of a
975 simpler process for looking up a single event in a keymap. How that is
976 done depends on the type of object associated with the event in that
977 keymap.
978
979 Let's use the term @dfn{keymap entry} to describe the value found by
980 looking up an event type in a keymap. (This doesn't include the item
981 string and other extra elements in a keymap element for a menu item, because
982 @code{lookup-key} and other key lookup functions don't include them in
983 the returned value.) While any Lisp object may be stored in a keymap
984 as a keymap entry, not all make sense for key lookup. Here is a table
985 of the meaningful types of keymap entries:
986
987 @table @asis
988 @item @code{nil}
989 @cindex @code{nil} in keymap
990 @code{nil} means that the events used so far in the lookup form an
991 undefined key. When a keymap fails to mention an event type at all, and
992 has no default binding, that is equivalent to a binding of @code{nil}
993 for that event type.
994
995 @item @var{command}
996 @cindex command in keymap
997 The events used so far in the lookup form a complete key,
998 and @var{command} is its binding. @xref{What Is a Function}.
999
1000 @item @var{array}
1001 @cindex string in keymap
1002 The array (either a string or a vector) is a keyboard macro. The events
1003 used so far in the lookup form a complete key, and the array is its
1004 binding. See @ref{Keyboard Macros}, for more information.
1005
1006 @item @var{keymap}
1007 @cindex keymap in keymap
1008 The events used so far in the lookup form a prefix key. The next
1009 event of the key sequence is looked up in @var{keymap}.
1010
1011 @item @var{list}
1012 @cindex list in keymap
1013 The meaning of a list depends on what it contains:
1014
1015 @itemize @bullet
1016 @item
1017 If the @sc{car} of @var{list} is the symbol @code{keymap}, then the list
1018 is a keymap, and is treated as a keymap (see above).
1019
1020 @item
1021 @cindex @code{lambda} in keymap
1022 If the @sc{car} of @var{list} is @code{lambda}, then the list is a
1023 lambda expression. This is presumed to be a function, and is treated
1024 as such (see above). In order to execute properly as a key binding,
1025 this function must be a command---it must have an @code{interactive}
1026 specification. @xref{Defining Commands}.
1027
1028 @item
1029 If the @sc{car} of @var{list} is a keymap and the @sc{cdr} is an event
1030 type, then this is an @dfn{indirect entry}:
1031
1032 @example
1033 (@var{othermap} . @var{othertype})
1034 @end example
1035
1036 When key lookup encounters an indirect entry, it looks up instead the
1037 binding of @var{othertype} in @var{othermap} and uses that.
1038
1039 This feature permits you to define one key as an alias for another key.
1040 For example, an entry whose @sc{car} is the keymap called @code{esc-map}
1041 and whose @sc{cdr} is 32 (the code for @key{SPC}) means, ``Use the global
1042 binding of @kbd{Meta-@key{SPC}}, whatever that may be''.
1043 @end itemize
1044
1045 @item @var{symbol}
1046 @cindex symbol in keymap
1047 The function definition of @var{symbol} is used in place of
1048 @var{symbol}. If that too is a symbol, then this process is repeated,
1049 any number of times. Ultimately this should lead to an object that is
1050 a keymap, a command, or a keyboard macro. A list is allowed if it is a
1051 keymap or a command, but indirect entries are not understood when found
1052 via symbols.
1053
1054 Note that keymaps and keyboard macros (strings and vectors) are not
1055 valid functions, so a symbol with a keymap, string, or vector as its
1056 function definition is invalid as a function. It is, however, valid as
1057 a key binding. If the definition is a keyboard macro, then the symbol
1058 is also valid as an argument to @code{command-execute}
1059 (@pxref{Interactive Call}).
1060
1061 @cindex @code{undefined} in keymap
1062 The symbol @code{undefined} is worth special mention: it means to treat
1063 the key as undefined. Strictly speaking, the key is defined, and its
1064 binding is the command @code{undefined}; but that command does the same
1065 thing that is done automatically for an undefined key: it rings the bell
1066 (by calling @code{ding}) but does not signal an error.
1067
1068 @cindex preventing prefix key
1069 @code{undefined} is used in local keymaps to override a global key
1070 binding and make the key ``undefined'' locally. A local binding of
1071 @code{nil} would fail to do this because it would not override the
1072 global binding.
1073
1074 @item @var{anything else}
1075 If any other type of object is found, the events used so far in the
1076 lookup form a complete key, and the object is its binding, but the
1077 binding is not executable as a command.
1078 @end table
1079
1080 In short, a keymap entry may be a keymap, a command, a keyboard
1081 macro, a symbol that leads to one of them, or an indirection or
1082 @code{nil}.
1083
1084 @node Functions for Key Lookup
1085 @section Functions for Key Lookup
1086
1087 Here are the functions and variables pertaining to key lookup.
1088
1089 @defun lookup-key keymap key &optional accept-defaults
1090 This function returns the definition of @var{key} in @var{keymap}. All
1091 the other functions described in this chapter that look up keys use
1092 @code{lookup-key}. Here are examples:
1093
1094 @example
1095 @group
1096 (lookup-key (current-global-map) "\C-x\C-f")
1097 @result{} find-file
1098 @end group
1099 @group
1100 (lookup-key (current-global-map) (kbd "C-x C-f"))
1101 @result{} find-file
1102 @end group
1103 @group
1104 (lookup-key (current-global-map) "\C-x\C-f12345")
1105 @result{} 2
1106 @end group
1107 @end example
1108
1109 If the string or vector @var{key} is not a valid key sequence according
1110 to the prefix keys specified in @var{keymap}, it must be ``too long''
1111 and have extra events at the end that do not fit into a single key
1112 sequence. Then the value is a number, the number of events at the front
1113 of @var{key} that compose a complete key.
1114
1115 @c Emacs 19 feature
1116 If @var{accept-defaults} is non-@code{nil}, then @code{lookup-key}
1117 considers default bindings as well as bindings for the specific events
1118 in @var{key}. Otherwise, @code{lookup-key} reports only bindings for
1119 the specific sequence @var{key}, ignoring default bindings except when
1120 you explicitly ask about them. (To do this, supply @code{t} as an
1121 element of @var{key}; see @ref{Format of Keymaps}.)
1122
1123 If @var{key} contains a meta character (not a function key), that
1124 character is implicitly replaced by a two-character sequence: the value
1125 of @code{meta-prefix-char}, followed by the corresponding non-meta
1126 character. Thus, the first example below is handled by conversion into
1127 the second example.
1128
1129 @example
1130 @group
1131 (lookup-key (current-global-map) "\M-f")
1132 @result{} forward-word
1133 @end group
1134 @group
1135 (lookup-key (current-global-map) "\ef")
1136 @result{} forward-word
1137 @end group
1138 @end example
1139
1140 Unlike @code{read-key-sequence}, this function does not modify the
1141 specified events in ways that discard information (@pxref{Key Sequence
1142 Input}). In particular, it does not convert letters to lower case and
1143 it does not change drag events to clicks.
1144 @end defun
1145
1146 @deffn Command undefined
1147 Used in keymaps to undefine keys. It calls @code{ding}, but does
1148 not cause an error.
1149 @end deffn
1150
1151 @defun local-key-binding key &optional accept-defaults
1152 This function returns the binding for @var{key} in the current
1153 local keymap, or @code{nil} if it is undefined there.
1154
1155 @c Emacs 19 feature
1156 The argument @var{accept-defaults} controls checking for default bindings,
1157 as in @code{lookup-key} (above).
1158 @end defun
1159
1160 @defun global-key-binding key &optional accept-defaults
1161 This function returns the binding for command @var{key} in the
1162 current global keymap, or @code{nil} if it is undefined there.
1163
1164 @c Emacs 19 feature
1165 The argument @var{accept-defaults} controls checking for default bindings,
1166 as in @code{lookup-key} (above).
1167 @end defun
1168
1169 @c Emacs 19 feature
1170 @defun minor-mode-key-binding key &optional accept-defaults
1171 This function returns a list of all the active minor mode bindings of
1172 @var{key}. More precisely, it returns an alist of pairs
1173 @code{(@var{modename} . @var{binding})}, where @var{modename} is the
1174 variable that enables the minor mode, and @var{binding} is @var{key}'s
1175 binding in that mode. If @var{key} has no minor-mode bindings, the
1176 value is @code{nil}.
1177
1178 If the first binding found is not a prefix definition (a keymap or a
1179 symbol defined as a keymap), all subsequent bindings from other minor
1180 modes are omitted, since they would be completely shadowed. Similarly,
1181 the list omits non-prefix bindings that follow prefix bindings.
1182
1183 The argument @var{accept-defaults} controls checking for default
1184 bindings, as in @code{lookup-key} (above).
1185 @end defun
1186
1187 @defopt meta-prefix-char
1188 @cindex @key{ESC}
1189 This variable is the meta-prefix character code. It is used for
1190 translating a meta character to a two-character sequence so it can be
1191 looked up in a keymap. For useful results, the value should be a
1192 prefix event (@pxref{Prefix Keys}). The default value is 27, which is
1193 the @acronym{ASCII} code for @key{ESC}.
1194
1195 As long as the value of @code{meta-prefix-char} remains 27, key lookup
1196 translates @kbd{M-b} into @kbd{@key{ESC} b}, which is normally defined
1197 as the @code{backward-word} command. However, if you were to set
1198 @code{meta-prefix-char} to 24, the code for @kbd{C-x}, then Emacs will
1199 translate @kbd{M-b} into @kbd{C-x b}, whose standard binding is the
1200 @code{switch-to-buffer} command. (Don't actually do this!) Here is an
1201 illustration of what would happen:
1202
1203 @smallexample
1204 @group
1205 meta-prefix-char ; @r{The default value.}
1206 @result{} 27
1207 @end group
1208 @group
1209 (key-binding "\M-b")
1210 @result{} backward-word
1211 @end group
1212 @group
1213 ?\C-x ; @r{The print representation}
1214 @result{} 24 ; @r{of a character.}
1215 @end group
1216 @group
1217 (setq meta-prefix-char 24)
1218 @result{} 24
1219 @end group
1220 @group
1221 (key-binding "\M-b")
1222 @result{} switch-to-buffer ; @r{Now, typing @kbd{M-b} is}
1223 ; @r{like typing @kbd{C-x b}.}
1224
1225 (setq meta-prefix-char 27) ; @r{Avoid confusion!}
1226 @result{} 27 ; @r{Restore the default value!}
1227 @end group
1228 @end smallexample
1229
1230 This translation of one event into two happens only for characters, not
1231 for other kinds of input events. Thus, @kbd{M-@key{F1}}, a function
1232 key, is not converted into @kbd{@key{ESC} @key{F1}}.
1233 @end defopt
1234
1235 @node Changing Key Bindings
1236 @section Changing Key Bindings
1237 @cindex changing key bindings
1238 @cindex rebinding
1239
1240 The way to rebind a key is to change its entry in a keymap. If you
1241 change a binding in the global keymap, the change is effective in all
1242 buffers (though it has no direct effect in buffers that shadow the
1243 global binding with a local one). If you change the current buffer's
1244 local map, that usually affects all buffers using the same major mode.
1245 The @code{global-set-key} and @code{local-set-key} functions are
1246 convenient interfaces for these operations (@pxref{Key Binding
1247 Commands}). You can also use @code{define-key}, a more general
1248 function; then you must explicitly specify the map to change.
1249
1250 When choosing the key sequences for Lisp programs to rebind, please
1251 follow the Emacs conventions for use of various keys (@pxref{Key
1252 Binding Conventions}).
1253
1254 @cindex meta character key constants
1255 @cindex control character key constants
1256 In writing the key sequence to rebind, it is good to use the special
1257 escape sequences for control and meta characters (@pxref{String Type}).
1258 The syntax @samp{\C-} means that the following character is a control
1259 character and @samp{\M-} means that the following character is a meta
1260 character. Thus, the string @code{"\M-x"} is read as containing a
1261 single @kbd{M-x}, @code{"\C-f"} is read as containing a single
1262 @kbd{C-f}, and @code{"\M-\C-x"} and @code{"\C-\M-x"} are both read as
1263 containing a single @kbd{C-M-x}. You can also use this escape syntax in
1264 vectors, as well as others that aren't allowed in strings; one example
1265 is @samp{[?\C-\H-x home]}. @xref{Character Type}.
1266
1267 The key definition and lookup functions accept an alternate syntax for
1268 event types in a key sequence that is a vector: you can use a list
1269 containing modifier names plus one base event (a character or function
1270 key name). For example, @code{(control ?a)} is equivalent to
1271 @code{?\C-a} and @code{(hyper control left)} is equivalent to
1272 @code{C-H-left}. One advantage of such lists is that the precise
1273 numeric codes for the modifier bits don't appear in compiled files.
1274
1275 The functions below signal an error if @var{keymap} is not a keymap,
1276 or if @var{key} is not a string or vector representing a key sequence.
1277 You can use event types (symbols) as shorthand for events that are
1278 lists. The @code{kbd} macro (@pxref{Key Sequences}) is a convenient
1279 way to specify the key sequence.
1280
1281 @defun define-key keymap key binding
1282 This function sets the binding for @var{key} in @var{keymap}. (If
1283 @var{key} is more than one event long, the change is actually made
1284 in another keymap reached from @var{keymap}.) The argument
1285 @var{binding} can be any Lisp object, but only certain types are
1286 meaningful. (For a list of meaningful types, see @ref{Key Lookup}.)
1287 The value returned by @code{define-key} is @var{binding}.
1288
1289 If @var{key} is @code{[t]}, this sets the default binding in
1290 @var{keymap}. When an event has no binding of its own, the Emacs
1291 command loop uses the keymap's default binding, if there is one.
1292
1293 @cindex invalid prefix key error
1294 @cindex key sequence error
1295 Every prefix of @var{key} must be a prefix key (i.e., bound to a keymap)
1296 or undefined; otherwise an error is signaled. If some prefix of
1297 @var{key} is undefined, then @code{define-key} defines it as a prefix
1298 key so that the rest of @var{key} can be defined as specified.
1299
1300 If there was previously no binding for @var{key} in @var{keymap}, the
1301 new binding is added at the beginning of @var{keymap}. The order of
1302 bindings in a keymap makes no difference for keyboard input, but it
1303 does matter for menu keymaps (@pxref{Menu Keymaps}).
1304 @end defun
1305
1306 This example creates a sparse keymap and makes a number of
1307 bindings in it:
1308
1309 @smallexample
1310 @group
1311 (setq map (make-sparse-keymap))
1312 @result{} (keymap)
1313 @end group
1314 @group
1315 (define-key map "\C-f" 'forward-char)
1316 @result{} forward-char
1317 @end group
1318 @group
1319 map
1320 @result{} (keymap (6 . forward-char))
1321 @end group
1322
1323 @group
1324 ;; @r{Build sparse submap for @kbd{C-x} and bind @kbd{f} in that.}
1325 (define-key map (kbd "C-x f") 'forward-word)
1326 @result{} forward-word
1327 @end group
1328 @group
1329 map
1330 @result{} (keymap
1331 (24 keymap ; @kbd{C-x}
1332 (102 . forward-word)) ; @kbd{f}
1333 (6 . forward-char)) ; @kbd{C-f}
1334 @end group
1335
1336 @group
1337 ;; @r{Bind @kbd{C-p} to the @code{ctl-x-map}.}
1338 (define-key map (kbd "C-p") ctl-x-map)
1339 ;; @code{ctl-x-map}
1340 @result{} [nil @dots{} find-file @dots{} backward-kill-sentence]
1341 @end group
1342
1343 @group
1344 ;; @r{Bind @kbd{C-f} to @code{foo} in the @code{ctl-x-map}.}
1345 (define-key map (kbd "C-p C-f") 'foo)
1346 @result{} 'foo
1347 @end group
1348 @group
1349 map
1350 @result{} (keymap ; @r{Note @code{foo} in @code{ctl-x-map}.}
1351 (16 keymap [nil @dots{} foo @dots{} backward-kill-sentence])
1352 (24 keymap
1353 (102 . forward-word))
1354 (6 . forward-char))
1355 @end group
1356 @end smallexample
1357
1358 @noindent
1359 Note that storing a new binding for @kbd{C-p C-f} actually works by
1360 changing an entry in @code{ctl-x-map}, and this has the effect of
1361 changing the bindings of both @kbd{C-p C-f} and @kbd{C-x C-f} in the
1362 default global map.
1363
1364 The function @code{substitute-key-definition} scans a keymap for
1365 keys that have a certain binding and rebinds them with a different
1366 binding. Another feature which is cleaner and can often produce the
1367 same results to remap one command into another (@pxref{Remapping
1368 Commands}).
1369
1370 @defun substitute-key-definition olddef newdef keymap &optional oldmap
1371 @cindex replace bindings
1372 This function replaces @var{olddef} with @var{newdef} for any keys in
1373 @var{keymap} that were bound to @var{olddef}. In other words,
1374 @var{olddef} is replaced with @var{newdef} wherever it appears. The
1375 function returns @code{nil}.
1376
1377 For example, this redefines @kbd{C-x C-f}, if you do it in an Emacs with
1378 standard bindings:
1379
1380 @smallexample
1381 @group
1382 (substitute-key-definition
1383 'find-file 'find-file-read-only (current-global-map))
1384 @end group
1385 @end smallexample
1386
1387 @c Emacs 19 feature
1388 If @var{oldmap} is non-@code{nil}, that changes the behavior of
1389 @code{substitute-key-definition}: the bindings in @var{oldmap} determine
1390 which keys to rebind. The rebindings still happen in @var{keymap}, not
1391 in @var{oldmap}. Thus, you can change one map under the control of the
1392 bindings in another. For example,
1393
1394 @smallexample
1395 (substitute-key-definition
1396 'delete-backward-char 'my-funny-delete
1397 my-map global-map)
1398 @end smallexample
1399
1400 @noindent
1401 puts the special deletion command in @code{my-map} for whichever keys
1402 are globally bound to the standard deletion command.
1403
1404 Here is an example showing a keymap before and after substitution:
1405
1406 @smallexample
1407 @group
1408 (setq map '(keymap
1409 (?1 . olddef-1)
1410 (?2 . olddef-2)
1411 (?3 . olddef-1)))
1412 @result{} (keymap (49 . olddef-1) (50 . olddef-2) (51 . olddef-1))
1413 @end group
1414
1415 @group
1416 (substitute-key-definition 'olddef-1 'newdef map)
1417 @result{} nil
1418 @end group
1419 @group
1420 map
1421 @result{} (keymap (49 . newdef) (50 . olddef-2) (51 . newdef))
1422 @end group
1423 @end smallexample
1424 @end defun
1425
1426 @defun suppress-keymap keymap &optional nodigits
1427 @cindex @code{self-insert-command} override
1428 This function changes the contents of the full keymap @var{keymap} by
1429 remapping @code{self-insert-command} to the command @code{undefined}
1430 (@pxref{Remapping Commands}). This has the effect of undefining all
1431 printing characters, thus making ordinary insertion of text impossible.
1432 @code{suppress-keymap} returns @code{nil}.
1433
1434 If @var{nodigits} is @code{nil}, then @code{suppress-keymap} defines
1435 digits to run @code{digit-argument}, and @kbd{-} to run
1436 @code{negative-argument}. Otherwise it makes them undefined like the
1437 rest of the printing characters.
1438
1439 @cindex yank suppression
1440 @cindex @code{quoted-insert} suppression
1441 The @code{suppress-keymap} function does not make it impossible to
1442 modify a buffer, as it does not suppress commands such as @code{yank}
1443 and @code{quoted-insert}. To prevent any modification of a buffer, make
1444 it read-only (@pxref{Read Only Buffers}).
1445
1446 Since this function modifies @var{keymap}, you would normally use it
1447 on a newly created keymap. Operating on an existing keymap
1448 that is used for some other purpose is likely to cause trouble; for
1449 example, suppressing @code{global-map} would make it impossible to use
1450 most of Emacs.
1451
1452 This function can be used to initialize the local keymap of a major
1453 mode for which insertion of text is not desirable. But usually such a
1454 mode should be derived from @code{special-mode} (@pxref{Basic Major
1455 Modes}); then its keymap will automatically inherit from
1456 @code{special-mode-map}, which is already suppressed. Here is how
1457 @code{special-mode-map} is defined:
1458
1459 @smallexample
1460 @group
1461 (defvar special-mode-map
1462 (let ((map (make-sparse-keymap)))
1463 (suppress-keymap map)
1464 (define-key map "q" 'quit-window)
1465 @dots{}
1466 map))
1467 @end group
1468 @end smallexample
1469 @end defun
1470
1471 @node Remapping Commands
1472 @section Remapping Commands
1473 @cindex remapping commands
1474
1475 A special kind of key binding can be used to @dfn{remap} one command
1476 to another, without having to refer to the key sequence(s) bound to
1477 the original command. To use this feature, make a key binding for a
1478 key sequence that starts with the dummy event @code{remap}, followed
1479 by the command name you want to remap; for the binding, specify the
1480 new definition (usually a command name, but possibly any other valid
1481 definition for a key binding).
1482
1483 For example, suppose My mode provides a special command
1484 @code{my-kill-line}, which should be invoked instead of
1485 @code{kill-line}. To establish this, its mode keymap should contain
1486 the following remapping:
1487
1488 @smallexample
1489 (define-key my-mode-map [remap kill-line] 'my-kill-line)
1490 @end smallexample
1491
1492 @noindent
1493 Then, whenever @code{my-mode-map} is active, if the user types
1494 @kbd{C-k} (the default global key sequence for @code{kill-line}) Emacs
1495 will instead run @code{my-kill-line}.
1496
1497 Note that remapping only takes place through active keymaps; for
1498 example, putting a remapping in a prefix keymap like @code{ctl-x-map}
1499 typically has no effect, as such keymaps are not themselves active.
1500 In addition, remapping only works through a single level; in the
1501 following example,
1502
1503 @smallexample
1504 (define-key my-mode-map [remap kill-line] 'my-kill-line)
1505 (define-key my-mode-map [remap my-kill-line] 'my-other-kill-line)
1506 @end smallexample
1507
1508 @noindent
1509 @code{kill-line} is @emph{not} remapped to @code{my-other-kill-line}.
1510 Instead, if an ordinary key binding specifies @code{kill-line}, it is
1511 remapped to @code{my-kill-line}; if an ordinary binding specifies
1512 @code{my-kill-line}, it is remapped to @code{my-other-kill-line}.
1513
1514 To undo the remapping of a command, remap it to @code{nil}; e.g.
1515
1516 @smallexample
1517 (define-key my-mode-map [remap kill-line] nil)
1518 @end smallexample
1519
1520 @defun command-remapping command &optional position keymaps
1521 This function returns the remapping for @var{command} (a symbol),
1522 given the current active keymaps. If @var{command} is not remapped
1523 (which is the usual situation), or not a symbol, the function returns
1524 @code{nil}. @code{position} can optionally specify a buffer position
1525 or an event position to determine the keymaps to use, as in
1526 @code{key-binding}.
1527
1528 If the optional argument @code{keymaps} is non-@code{nil}, it
1529 specifies a list of keymaps to search in. This argument is ignored if
1530 @code{position} is non-@code{nil}.
1531 @end defun
1532
1533 @node Translation Keymaps
1534 @section Keymaps for Translating Sequences of Events
1535 @cindex keymaps for translating events
1536
1537 This section describes keymaps that are used during reading a key
1538 sequence, to translate certain event sequences into others.
1539 @code{read-key-sequence} checks every subsequence of the key sequence
1540 being read, as it is read, against @code{input-decode-map}, then
1541 @code{local-function-key-map}, and then against @code{key-translation-map}.
1542
1543 @defvar input-decode-map
1544 This variable holds a keymap that describes the character sequences sent
1545 by function keys on an ordinary character terminal. This keymap has the
1546 same structure as other keymaps, but is used differently: it specifies
1547 translations to make while reading key sequences, rather than bindings
1548 for key sequences.
1549
1550 If @code{input-decode-map} ``binds'' a key sequence @var{k} to a vector
1551 @var{v}, then when @var{k} appears as a subsequence @emph{anywhere} in a
1552 key sequence, it is replaced with the events in @var{v}.
1553
1554 For example, VT100 terminals send @kbd{@key{ESC} O P} when the
1555 keypad @key{PF1} key is pressed. Therefore, we want Emacs to translate
1556 that sequence of events into the single event @code{pf1}. We accomplish
1557 this by ``binding'' @kbd{@key{ESC} O P} to @code{[pf1]} in
1558 @code{input-decode-map}, when using a VT100.
1559
1560 Thus, typing @kbd{C-c @key{PF1}} sends the character sequence @kbd{C-c
1561 @key{ESC} O P}; later the function @code{read-key-sequence} translates
1562 this back into @kbd{C-c @key{PF1}}, which it returns as the vector
1563 @code{[?\C-c pf1]}.
1564
1565 The value of @code{input-decode-map} is usually set up automatically
1566 according to the terminal's Terminfo or Termcap entry, but sometimes
1567 those need help from terminal-specific Lisp files. Emacs comes with
1568 terminal-specific files for many common terminals; their main purpose is
1569 to make entries in @code{input-decode-map} beyond those that can be
1570 deduced from Termcap and Terminfo. @xref{Terminal-Specific}.
1571 @end defvar
1572
1573 @defvar local-function-key-map
1574 This variable holds a keymap similar to @code{input-decode-map} except
1575 that it describes key sequences which should be translated to
1576 alternative interpretations that are usually preferred. It applies
1577 after @code{input-decode-map} and before @code{key-translation-map}.
1578
1579 Entries in @code{local-function-key-map} are ignored if they conflict
1580 with bindings made in the minor mode, local, or global keymaps. I.e.
1581 the remapping only applies if the original key sequence would
1582 otherwise not have any binding.
1583
1584 @code{local-function-key-map} inherits from @code{function-key-map},
1585 but the latter should not be used directly.
1586 @end defvar
1587
1588 @defvar key-translation-map
1589 This variable is another keymap used just like @code{input-decode-map}
1590 to translate input events into other events. It differs from
1591 @code{input-decode-map} in that it goes to work after
1592 @code{local-function-key-map} is finished rather than before; it
1593 receives the results of translation by @code{local-function-key-map}.
1594
1595 Just like @code{input-decode-map}, but unlike
1596 @code{local-function-key-map}, this keymap is applied regardless of
1597 whether the input key-sequence has a normal binding. Note however
1598 that actual key bindings can have an effect on
1599 @code{key-translation-map}, even though they are overridden by it.
1600 Indeed, actual key bindings override @code{local-function-key-map} and
1601 thus may alter the key sequence that @code{key-translation-map}
1602 receives. Clearly, it is better to avoid this type of situation.
1603
1604 The intent of @code{key-translation-map} is for users to map one
1605 character set to another, including ordinary characters normally bound
1606 to @code{self-insert-command}.
1607 @end defvar
1608
1609 @cindex key translation function
1610 You can use @code{input-decode-map}, @code{local-function-key-map},
1611 and @code{key-translation-map} for more than simple aliases, by using
1612 a function, instead of a key sequence, as the ``translation'' of a
1613 key. Then this function is called to compute the translation of that
1614 key.
1615
1616 The key translation function receives one argument, which is the prompt
1617 that was specified in @code{read-key-sequence}---or @code{nil} if the
1618 key sequence is being read by the editor command loop. In most cases
1619 you can ignore the prompt value.
1620
1621 If the function reads input itself, it can have the effect of altering
1622 the event that follows. For example, here's how to define @kbd{C-c h}
1623 to turn the character that follows into a Hyper character:
1624
1625 @example
1626 @group
1627 (defun hyperify (prompt)
1628 (let ((e (read-event)))
1629 (vector (if (numberp e)
1630 (logior (lsh 1 24) e)
1631 (if (memq 'hyper (event-modifiers e))
1632 e
1633 (add-event-modifier "H-" e))))))
1634
1635 (defun add-event-modifier (string e)
1636 (let ((symbol (if (symbolp e) e (car e))))
1637 (setq symbol (intern (concat string
1638 (symbol-name symbol))))
1639 @end group
1640 @group
1641 (if (symbolp e)
1642 symbol
1643 (cons symbol (cdr e)))))
1644
1645 (define-key local-function-key-map "\C-ch" 'hyperify)
1646 @end group
1647 @end example
1648
1649 If you have enabled keyboard character set decoding using
1650 @code{set-keyboard-coding-system}, decoding is done after the
1651 translations listed above. @xref{Terminal I/O Encoding}. However, in
1652 future Emacs versions, character set decoding may be done at an
1653 earlier stage.
1654
1655 @node Key Binding Commands
1656 @section Commands for Binding Keys
1657
1658 This section describes some convenient interactive interfaces for
1659 changing key bindings. They work by calling @code{define-key}.
1660
1661 People often use @code{global-set-key} in their init files
1662 (@pxref{Init File}) for simple customization. For example,
1663
1664 @smallexample
1665 (global-set-key (kbd "C-x C-\\") 'next-line)
1666 @end smallexample
1667
1668 @noindent
1669 or
1670
1671 @smallexample
1672 (global-set-key [?\C-x ?\C-\\] 'next-line)
1673 @end smallexample
1674
1675 @noindent
1676 or
1677
1678 @smallexample
1679 (global-set-key [(control ?x) (control ?\\)] 'next-line)
1680 @end smallexample
1681
1682 @noindent
1683 redefines @kbd{C-x C-\} to move down a line.
1684
1685 @smallexample
1686 (global-set-key [M-mouse-1] 'mouse-set-point)
1687 @end smallexample
1688
1689 @noindent
1690 redefines the first (leftmost) mouse button, entered with the Meta key, to
1691 set point where you click.
1692
1693 @cindex non-@acronym{ASCII} text in keybindings
1694 Be careful when using non-@acronym{ASCII} text characters in Lisp
1695 specifications of keys to bind. If these are read as multibyte text, as
1696 they usually will be in a Lisp file (@pxref{Loading Non-ASCII}), you
1697 must type the keys as multibyte too. For instance, if you use this:
1698
1699 @smallexample
1700 (global-set-key "@"o" 'my-function) ; bind o-umlaut
1701 @end smallexample
1702
1703 @noindent
1704 or
1705
1706 @smallexample
1707 (global-set-key ?@"o 'my-function) ; bind o-umlaut
1708 @end smallexample
1709
1710 @noindent
1711 and your language environment is multibyte Latin-1, these commands
1712 actually bind the multibyte character with code 246, not the byte
1713 code 246 (@kbd{M-v}) sent by a Latin-1 terminal. In order to use this
1714 binding, you need to teach Emacs how to decode the keyboard by using an
1715 appropriate input method (@pxref{Input Methods, , Input Methods, emacs, The GNU
1716 Emacs Manual}).
1717
1718 @deffn Command global-set-key key binding
1719 This function sets the binding of @var{key} in the current global map
1720 to @var{binding}.
1721
1722 @smallexample
1723 @group
1724 (global-set-key @var{key} @var{binding})
1725 @equiv{}
1726 (define-key (current-global-map) @var{key} @var{binding})
1727 @end group
1728 @end smallexample
1729 @end deffn
1730
1731 @deffn Command global-unset-key key
1732 @cindex unbinding keys
1733 This function removes the binding of @var{key} from the current
1734 global map.
1735
1736 One use of this function is in preparation for defining a longer key
1737 that uses @var{key} as a prefix---which would not be allowed if
1738 @var{key} has a non-prefix binding. For example:
1739
1740 @smallexample
1741 @group
1742 (global-unset-key "\C-l")
1743 @result{} nil
1744 @end group
1745 @group
1746 (global-set-key "\C-l\C-l" 'redraw-display)
1747 @result{} nil
1748 @end group
1749 @end smallexample
1750
1751 This function is implemented simply using @code{define-key}:
1752
1753 @smallexample
1754 @group
1755 (global-unset-key @var{key})
1756 @equiv{}
1757 (define-key (current-global-map) @var{key} nil)
1758 @end group
1759 @end smallexample
1760 @end deffn
1761
1762 @deffn Command local-set-key key binding
1763 This function sets the binding of @var{key} in the current local
1764 keymap to @var{binding}.
1765
1766 @smallexample
1767 @group
1768 (local-set-key @var{key} @var{binding})
1769 @equiv{}
1770 (define-key (current-local-map) @var{key} @var{binding})
1771 @end group
1772 @end smallexample
1773 @end deffn
1774
1775 @deffn Command local-unset-key key
1776 This function removes the binding of @var{key} from the current
1777 local map.
1778
1779 @smallexample
1780 @group
1781 (local-unset-key @var{key})
1782 @equiv{}
1783 (define-key (current-local-map) @var{key} nil)
1784 @end group
1785 @end smallexample
1786 @end deffn
1787
1788 @node Scanning Keymaps
1789 @section Scanning Keymaps
1790
1791 This section describes functions used to scan all the current keymaps
1792 for the sake of printing help information.
1793
1794 @defun accessible-keymaps keymap &optional prefix
1795 This function returns a list of all the keymaps that can be reached (via
1796 zero or more prefix keys) from @var{keymap}. The value is an
1797 association list with elements of the form @code{(@var{key} .@:
1798 @var{map})}, where @var{key} is a prefix key whose definition in
1799 @var{keymap} is @var{map}.
1800
1801 The elements of the alist are ordered so that the @var{key} increases
1802 in length. The first element is always @code{([] .@: @var{keymap})},
1803 because the specified keymap is accessible from itself with a prefix of
1804 no events.
1805
1806 If @var{prefix} is given, it should be a prefix key sequence; then
1807 @code{accessible-keymaps} includes only the submaps whose prefixes start
1808 with @var{prefix}. These elements look just as they do in the value of
1809 @code{(accessible-keymaps)}; the only difference is that some elements
1810 are omitted.
1811
1812 In the example below, the returned alist indicates that the key
1813 @key{ESC}, which is displayed as @samp{^[}, is a prefix key whose
1814 definition is the sparse keymap @code{(keymap (83 .@: center-paragraph)
1815 (115 .@: foo))}.
1816
1817 @smallexample
1818 @group
1819 (accessible-keymaps (current-local-map))
1820 @result{}(([] keymap
1821 (27 keymap ; @r{Note this keymap for @key{ESC} is repeated below.}
1822 (83 . center-paragraph)
1823 (115 . center-line))
1824 (9 . tab-to-tab-stop))
1825 @end group
1826
1827 @group
1828 ("^[" keymap
1829 (83 . center-paragraph)
1830 (115 . foo)))
1831 @end group
1832 @end smallexample
1833
1834 In the following example, @kbd{C-h} is a prefix key that uses a sparse
1835 keymap starting with @code{(keymap (118 . describe-variable)@dots{})}.
1836 Another prefix, @kbd{C-x 4}, uses a keymap which is also the value of
1837 the variable @code{ctl-x-4-map}. The event @code{mode-line} is one of
1838 several dummy events used as prefixes for mouse actions in special parts
1839 of a window.
1840
1841 @smallexample
1842 @group
1843 (accessible-keymaps (current-global-map))
1844 @result{} (([] keymap [set-mark-command beginning-of-line @dots{}
1845 delete-backward-char])
1846 @end group
1847 @group
1848 ("^H" keymap (118 . describe-variable) @dots{}
1849 (8 . help-for-help))
1850 @end group
1851 @group
1852 ("^X" keymap [x-flush-mouse-queue @dots{}
1853 backward-kill-sentence])
1854 @end group
1855 @group
1856 ("^[" keymap [mark-sexp backward-sexp @dots{}
1857 backward-kill-word])
1858 @end group
1859 ("^X4" keymap (15 . display-buffer) @dots{})
1860 @group
1861 ([mode-line] keymap
1862 (S-mouse-2 . mouse-split-window-horizontally) @dots{}))
1863 @end group
1864 @end smallexample
1865
1866 @noindent
1867 These are not all the keymaps you would see in actuality.
1868 @end defun
1869
1870 @defun map-keymap function keymap
1871 The function @code{map-keymap} calls @var{function} once
1872 for each binding in @var{keymap}. It passes two arguments,
1873 the event type and the value of the binding. If @var{keymap}
1874 has a parent, the parent's bindings are included as well.
1875 This works recursively: if the parent has itself a parent, then the
1876 grandparent's bindings are also included and so on.
1877
1878 This function is the cleanest way to examine all the bindings
1879 in a keymap.
1880 @end defun
1881
1882 @defun where-is-internal command &optional keymap firstonly noindirect no-remap
1883 This function is a subroutine used by the @code{where-is} command
1884 (@pxref{Help, , Help, emacs,The GNU Emacs Manual}). It returns a list
1885 of all key sequences (of any length) that are bound to @var{command} in a
1886 set of keymaps.
1887
1888 The argument @var{command} can be any object; it is compared with all
1889 keymap entries using @code{eq}.
1890
1891 If @var{keymap} is @code{nil}, then the maps used are the current active
1892 keymaps, disregarding @code{overriding-local-map} (that is, pretending
1893 its value is @code{nil}). If @var{keymap} is a keymap, then the
1894 maps searched are @var{keymap} and the global keymap. If @var{keymap}
1895 is a list of keymaps, only those keymaps are searched.
1896
1897 Usually it's best to use @code{overriding-local-map} as the expression
1898 for @var{keymap}. Then @code{where-is-internal} searches precisely
1899 the keymaps that are active. To search only the global map, pass the
1900 value @code{(keymap)} (an empty keymap) as @var{keymap}.
1901
1902 If @var{firstonly} is @code{non-ascii}, then the value is a single
1903 vector representing the first key sequence found, rather than a list of
1904 all possible key sequences. If @var{firstonly} is @code{t}, then the
1905 value is the first key sequence, except that key sequences consisting
1906 entirely of @acronym{ASCII} characters (or meta variants of @acronym{ASCII}
1907 characters) are preferred to all other key sequences and that the
1908 return value can never be a menu binding.
1909
1910 If @var{noindirect} is non-@code{nil}, @code{where-is-internal} doesn't
1911 follow indirect keymap bindings. This makes it possible to search for
1912 an indirect definition itself.
1913
1914 The fifth argument, @var{no-remap}, determines how this function
1915 treats command remappings (@pxref{Remapping Commands}). There are two
1916 cases of interest:
1917
1918 @table @asis
1919 @item If a command @var{other-command} is remapped to @var{command}:
1920 If @var{no-remap} is @code{nil}, find the bindings for
1921 @var{other-command} and treat them as though they are also bindings
1922 for @var{command}. If @var{no-remap} is non-@code{nil}, include the
1923 vector @code{[remap @var{other-command}]} in the list of possible key
1924 sequences, instead of finding those bindings.
1925
1926 @item If @var{command} is remapped to @var{other-command}:
1927 If @var{no-remap} is @code{nil}, return the bindings for
1928 @var{other-command} rather than @var{command}. If @var{no-remap} is
1929 non-@code{nil}, return the bindings for @var{command}, ignoring the
1930 fact that it is remapped.
1931 @end table
1932 @end defun
1933
1934 @deffn Command describe-bindings &optional prefix buffer-or-name
1935 This function creates a listing of all current key bindings, and
1936 displays it in a buffer named @file{*Help*}. The text is grouped by
1937 modes---minor modes first, then the major mode, then global bindings.
1938
1939 If @var{prefix} is non-@code{nil}, it should be a prefix key; then the
1940 listing includes only keys that start with @var{prefix}.
1941
1942 The listing describes meta characters as @key{ESC} followed by the
1943 corresponding non-meta character.
1944
1945 When several characters with consecutive @acronym{ASCII} codes have the
1946 same definition, they are shown together, as
1947 @samp{@var{firstchar}..@var{lastchar}}. In this instance, you need to
1948 know the @acronym{ASCII} codes to understand which characters this means.
1949 For example, in the default global map, the characters @samp{@key{SPC}
1950 ..@: ~} are described by a single line. @key{SPC} is @acronym{ASCII} 32,
1951 @kbd{~} is @acronym{ASCII} 126, and the characters between them include all
1952 the normal printing characters, (e.g., letters, digits, punctuation,
1953 etc.@:); all these characters are bound to @code{self-insert-command}.
1954
1955 If @var{buffer-or-name} is non-@code{nil}, it should be a buffer or a
1956 buffer name. Then @code{describe-bindings} lists that buffer's bindings,
1957 instead of the current buffer's.
1958 @end deffn
1959
1960 @node Menu Keymaps
1961 @section Menu Keymaps
1962 @cindex menu keymaps
1963
1964 A keymap can operate as a menu as well as defining bindings for
1965 keyboard keys and mouse buttons. Menus are usually actuated with the
1966 mouse, but they can function with the keyboard also. If a menu keymap
1967 is active for the next input event, that activates the keyboard menu
1968 feature.
1969
1970 @menu
1971 * Defining Menus:: How to make a keymap that defines a menu.
1972 * Mouse Menus:: How users actuate the menu with the mouse.
1973 * Keyboard Menus:: How users actuate the menu with the keyboard.
1974 * Menu Example:: Making a simple menu.
1975 * Menu Bar:: How to customize the menu bar.
1976 * Tool Bar:: A tool bar is a row of images.
1977 * Modifying Menus:: How to add new items to a menu.
1978 * Easy Menu:: A convenience macro for making menus.
1979 @end menu
1980
1981 @node Defining Menus
1982 @subsection Defining Menus
1983 @cindex defining menus
1984 @cindex menu prompt string
1985 @cindex prompt string (of menu)
1986 @cindex menu item
1987
1988 A keymap acts as a menu if it has an @dfn{overall prompt string},
1989 which is a string that appears as an element of the keymap.
1990 (@xref{Format of Keymaps}.) The string should describe the purpose of
1991 the menu's commands. Emacs displays the overall prompt string as the
1992 menu title in some cases, depending on the toolkit (if any) used for
1993 displaying menus.@footnote{It is required for menus which do not use a
1994 toolkit, e.g.@: under MS-DOS.} Keyboard menus also display the
1995 overall prompt string.
1996
1997 The easiest way to construct a keymap with a prompt string is to
1998 specify the string as an argument when you call @code{make-keymap},
1999 @code{make-sparse-keymap} (@pxref{Creating Keymaps}), or
2000 @code{define-prefix-command} (@pxref{Definition of
2001 define-prefix-command}). If you do not want the keymap to operate as
2002 a menu, don't specify a prompt string for it.
2003
2004 @defun keymap-prompt keymap
2005 This function returns the overall prompt string of @var{keymap},
2006 or @code{nil} if it has none.
2007 @end defun
2008
2009 The menu's items are the bindings in the keymap. Each binding
2010 associates an event type to a definition, but the event types have no
2011 significance for the menu appearance. (Usually we use pseudo-events,
2012 symbols that the keyboard cannot generate, as the event types for menu
2013 item bindings.) The menu is generated entirely from the bindings that
2014 correspond in the keymap to these events.
2015
2016 The order of items in the menu is the same as the order of bindings in
2017 the keymap. Since @code{define-key} puts new bindings at the front, you
2018 should define the menu items starting at the bottom of the menu and
2019 moving to the top, if you care about the order. When you add an item to
2020 an existing menu, you can specify its position in the menu using
2021 @code{define-key-after} (@pxref{Modifying Menus}).
2022
2023 @menu
2024 * Simple Menu Items:: A simple kind of menu key binding.
2025 * Extended Menu Items:: More complex menu item definitions.
2026 * Menu Separators:: Drawing a horizontal line through a menu.
2027 * Alias Menu Items:: Using command aliases in menu items.
2028 @end menu
2029
2030 @node Simple Menu Items
2031 @subsubsection Simple Menu Items
2032
2033 The simpler (and original) way to define a menu item is to bind some
2034 event type (it doesn't matter what event type) to a binding like this:
2035
2036 @example
2037 (@var{item-string} . @var{real-binding})
2038 @end example
2039
2040 @noindent
2041 The @sc{car}, @var{item-string}, is the string to be displayed in the
2042 menu. It should be short---preferably one to three words. It should
2043 describe the action of the command it corresponds to. Note that not
2044 all graphical toolkits can display non-@acronym{ASCII} text in menus
2045 (it will work for keyboard menus and will work to a large extent with
2046 the GTK+ toolkit).
2047
2048 You can also supply a second string, called the help string, as follows:
2049
2050 @example
2051 (@var{item-string} @var{help} . @var{real-binding})
2052 @end example
2053
2054 @noindent
2055 @var{help} specifies a ``help-echo'' string to display while the mouse
2056 is on that item in the same way as @code{help-echo} text properties
2057 (@pxref{Help display}).
2058
2059 As far as @code{define-key} is concerned, @var{item-string} and
2060 @var{help-string} are part of the event's binding. However,
2061 @code{lookup-key} returns just @var{real-binding}, and only
2062 @var{real-binding} is used for executing the key.
2063
2064 If @var{real-binding} is @code{nil}, then @var{item-string} appears in
2065 the menu but cannot be selected.
2066
2067 If @var{real-binding} is a symbol and has a non-@code{nil}
2068 @code{menu-enable} property, that property is an expression that
2069 controls whether the menu item is enabled. Every time the keymap is
2070 used to display a menu, Emacs evaluates the expression, and it enables
2071 the menu item only if the expression's value is non-@code{nil}. When a
2072 menu item is disabled, it is displayed in a ``fuzzy'' fashion, and
2073 cannot be selected.
2074
2075 The menu bar does not recalculate which items are enabled every time you
2076 look at a menu. This is because the X toolkit requires the whole tree
2077 of menus in advance. To force recalculation of the menu bar, call
2078 @code{force-mode-line-update} (@pxref{Mode Line Format}).
2079
2080 @node Extended Menu Items
2081 @subsubsection Extended Menu Items
2082 @kindex menu-item
2083 @cindex extended menu item
2084
2085 An extended-format menu item is a more flexible and also cleaner
2086 alternative to the simple format. You define an event type with a
2087 binding that's a list starting with the symbol @code{menu-item}.
2088 For a non-selectable string, the binding looks like this:
2089
2090 @example
2091 (menu-item @var{item-name})
2092 @end example
2093
2094 @noindent
2095 A string starting with two or more dashes specifies a separator line;
2096 see @ref{Menu Separators}.
2097
2098 To define a real menu item which can be selected, the extended format
2099 binding looks like this:
2100
2101 @example
2102 (menu-item @var{item-name} @var{real-binding}
2103 . @var{item-property-list})
2104 @end example
2105
2106 @noindent
2107 Here, @var{item-name} is an expression which evaluates to the menu item
2108 string. Thus, the string need not be a constant. The third element,
2109 @var{real-binding}, is the command to execute. The tail of the list,
2110 @var{item-property-list}, has the form of a property list which contains
2111 other information.
2112
2113 Here is a table of the properties that are supported:
2114
2115 @table @code
2116 @item :enable @var{form}
2117 The result of evaluating @var{form} determines whether the item is
2118 enabled (non-@code{nil} means yes). If the item is not enabled,
2119 you can't really click on it.
2120
2121 @item :visible @var{form}
2122 The result of evaluating @var{form} determines whether the item should
2123 actually appear in the menu (non-@code{nil} means yes). If the item
2124 does not appear, then the menu is displayed as if this item were
2125 not defined at all.
2126
2127 @item :help @var{help}
2128 The value of this property, @var{help}, specifies a ``help-echo'' string
2129 to display while the mouse is on that item. This is displayed in the
2130 same way as @code{help-echo} text properties (@pxref{Help display}).
2131 Note that this must be a constant string, unlike the @code{help-echo}
2132 property for text and overlays.
2133
2134 @item :button (@var{type} . @var{selected})
2135 This property provides a way to define radio buttons and toggle buttons.
2136 The @sc{car}, @var{type}, says which: it should be @code{:toggle} or
2137 @code{:radio}. The @sc{cdr}, @var{selected}, should be a form; the
2138 result of evaluating it says whether this button is currently selected.
2139
2140 A @dfn{toggle} is a menu item which is labeled as either ``on'' or ``off''
2141 according to the value of @var{selected}. The command itself should
2142 toggle @var{selected}, setting it to @code{t} if it is @code{nil},
2143 and to @code{nil} if it is @code{t}. Here is how the menu item
2144 to toggle the @code{debug-on-error} flag is defined:
2145
2146 @example
2147 (menu-item "Debug on Error" toggle-debug-on-error
2148 :button (:toggle
2149 . (and (boundp 'debug-on-error)
2150 debug-on-error)))
2151 @end example
2152
2153 @noindent
2154 This works because @code{toggle-debug-on-error} is defined as a command
2155 which toggles the variable @code{debug-on-error}.
2156
2157 @dfn{Radio buttons} are a group of menu items, in which at any time one
2158 and only one is ``selected''. There should be a variable whose value
2159 says which one is selected at any time. The @var{selected} form for
2160 each radio button in the group should check whether the variable has the
2161 right value for selecting that button. Clicking on the button should
2162 set the variable so that the button you clicked on becomes selected.
2163
2164 @item :key-sequence @var{key-sequence}
2165 This property specifies which key sequence is likely to be bound to the
2166 same command invoked by this menu item. If you specify the right key
2167 sequence, that makes preparing the menu for display run much faster.
2168
2169 If you specify the wrong key sequence, it has no effect; before Emacs
2170 displays @var{key-sequence} in the menu, it verifies that
2171 @var{key-sequence} is really equivalent to this menu item.
2172
2173 @item :key-sequence nil
2174 This property indicates that there is normally no key binding which is
2175 equivalent to this menu item. Using this property saves time in
2176 preparing the menu for display, because Emacs does not need to search
2177 the keymaps for a keyboard equivalent for this menu item.
2178
2179 However, if the user has rebound this item's definition to a key
2180 sequence, Emacs ignores the @code{:keys} property and finds the keyboard
2181 equivalent anyway.
2182
2183 @item :keys @var{string}
2184 This property specifies that @var{string} is the string to display
2185 as the keyboard equivalent for this menu item. You can use
2186 the @samp{\\[...]} documentation construct in @var{string}.
2187
2188 @item :filter @var{filter-fn}
2189 This property provides a way to compute the menu item dynamically.
2190 The property value @var{filter-fn} should be a function of one argument;
2191 when it is called, its argument will be @var{real-binding}. The
2192 function should return the binding to use instead.
2193
2194 Emacs can call this function at any time that it does redisplay or
2195 operates on menu data structures, so you should write it so it can
2196 safely be called at any time.
2197 @end table
2198
2199 @node Menu Separators
2200 @subsubsection Menu Separators
2201 @cindex menu separators
2202
2203 A menu separator is a kind of menu item that doesn't display any
2204 text---instead, it divides the menu into subparts with a horizontal line.
2205 A separator looks like this in the menu keymap:
2206
2207 @example
2208 (menu-item @var{separator-type})
2209 @end example
2210
2211 @noindent
2212 where @var{separator-type} is a string starting with two or more dashes.
2213
2214 In the simplest case, @var{separator-type} consists of only dashes.
2215 That specifies the default kind of separator. (For compatibility,
2216 @code{""} and @code{-} also count as separators.)
2217
2218 Certain other values of @var{separator-type} specify a different
2219 style of separator. Here is a table of them:
2220
2221 @table @code
2222 @item "--no-line"
2223 @itemx "--space"
2224 An extra vertical space, with no actual line.
2225
2226 @item "--single-line"
2227 A single line in the menu's foreground color.
2228
2229 @item "--double-line"
2230 A double line in the menu's foreground color.
2231
2232 @item "--single-dashed-line"
2233 A single dashed line in the menu's foreground color.
2234
2235 @item "--double-dashed-line"
2236 A double dashed line in the menu's foreground color.
2237
2238 @item "--shadow-etched-in"
2239 A single line with a 3D sunken appearance. This is the default,
2240 used separators consisting of dashes only.
2241
2242 @item "--shadow-etched-out"
2243 A single line with a 3D raised appearance.
2244
2245 @item "--shadow-etched-in-dash"
2246 A single dashed line with a 3D sunken appearance.
2247
2248 @item "--shadow-etched-out-dash"
2249 A single dashed line with a 3D raised appearance.
2250
2251 @item "--shadow-double-etched-in"
2252 Two lines with a 3D sunken appearance.
2253
2254 @item "--shadow-double-etched-out"
2255 Two lines with a 3D raised appearance.
2256
2257 @item "--shadow-double-etched-in-dash"
2258 Two dashed lines with a 3D sunken appearance.
2259
2260 @item "--shadow-double-etched-out-dash"
2261 Two dashed lines with a 3D raised appearance.
2262 @end table
2263
2264 You can also give these names in another style, adding a colon after
2265 the double-dash and replacing each single dash with capitalization of
2266 the following word. Thus, @code{"--:singleLine"}, is equivalent to
2267 @code{"--single-line"}.
2268
2269 You can use a longer form to specify keywords such as @code{:enable}
2270 and @code{:visible} for a menu separator:
2271
2272 @code{(menu-item @var{separator-type} nil . @var{item-property-list})}
2273
2274 For example:
2275
2276 @example
2277 (menu-item "--" nil :visible (boundp 'foo))
2278 @end example
2279
2280 Some systems and display toolkits don't really handle all of these
2281 separator types. If you use a type that isn't supported, the menu
2282 displays a similar kind of separator that is supported.
2283
2284 @node Alias Menu Items
2285 @subsubsection Alias Menu Items
2286
2287 Sometimes it is useful to make menu items that use the ``same''
2288 command but with different enable conditions. The best way to do this
2289 in Emacs now is with extended menu items; before that feature existed,
2290 it could be done by defining alias commands and using them in menu
2291 items. Here's an example that makes two aliases for
2292 @code{read-only-mode} and gives them different enable conditions:
2293
2294 @example
2295 (defalias 'make-read-only 'read-only-mode)
2296 (put 'make-read-only 'menu-enable '(not buffer-read-only))
2297 (defalias 'make-writable 'read-only-mode)
2298 (put 'make-writable 'menu-enable 'buffer-read-only)
2299 @end example
2300
2301 When using aliases in menus, often it is useful to display the
2302 equivalent key bindings for the ``real'' command name, not the aliases
2303 (which typically don't have any key bindings except for the menu
2304 itself). To request this, give the alias symbol a non-@code{nil}
2305 @code{menu-alias} property. Thus,
2306
2307 @example
2308 (put 'make-read-only 'menu-alias t)
2309 (put 'make-writable 'menu-alias t)
2310 @end example
2311
2312 @noindent
2313 causes menu items for @code{make-read-only} and @code{make-writable} to
2314 show the keyboard bindings for @code{read-only-mode}.
2315
2316 @node Mouse Menus
2317 @subsection Menus and the Mouse
2318
2319 The usual way to make a menu keymap produce a menu is to make it the
2320 definition of a prefix key. (A Lisp program can explicitly pop up a
2321 menu and receive the user's choice---see @ref{Pop-Up Menus}.)
2322
2323 If the prefix key ends with a mouse event, Emacs handles the menu keymap
2324 by popping up a visible menu, so that the user can select a choice with
2325 the mouse. When the user clicks on a menu item, the event generated is
2326 whatever character or symbol has the binding that brought about that
2327 menu item. (A menu item may generate a series of events if the menu has
2328 multiple levels or comes from the menu bar.)
2329
2330 It's often best to use a button-down event to trigger the menu. Then
2331 the user can select a menu item by releasing the button.
2332
2333 @cindex submenu
2334 If the menu keymap contains a binding to a nested keymap, the nested
2335 keymap specifies a @dfn{submenu}. There will be a menu item, labeled
2336 by the nested keymap's item string, and clicking on this item
2337 automatically pops up the specified submenu. As a special exception,
2338 if the menu keymap contains a single nested keymap and no other menu
2339 items, the menu shows the contents of the nested keymap directly, not
2340 as a submenu.
2341
2342 However, if Emacs is compiled without X toolkit support, submenus
2343 are not supported. Each nested keymap is shown as a menu item, but
2344 clicking on it does not automatically pop up the submenu. If you wish
2345 to imitate the effect of submenus, you can do that by giving a nested
2346 keymap an item string which starts with @samp{@@}. This causes Emacs
2347 to display the nested keymap using a separate @dfn{menu pane}; the
2348 rest of the item string after the @samp{@@} is the pane label. If
2349 Emacs is compiled without X toolkit support, menu panes are not used;
2350 in that case, a @samp{@@} at the beginning of an item string is
2351 omitted when the menu label is displayed, and has no other effect.
2352
2353 @node Keyboard Menus
2354 @subsection Menus and the Keyboard
2355
2356 When a prefix key ending with a keyboard event (a character or
2357 function key) has a definition that is a menu keymap, the keymap
2358 operates as a keyboard menu; the user specifies the next event by
2359 choosing a menu item with the keyboard.
2360
2361 Emacs displays the keyboard menu with the map's overall prompt
2362 string, followed by the alternatives (the item strings of the map's
2363 bindings), in the echo area. If the bindings don't all fit at once,
2364 the user can type @key{SPC} to see the next line of alternatives.
2365 Successive uses of @key{SPC} eventually get to the end of the menu and
2366 then cycle around to the beginning. (The variable
2367 @code{menu-prompt-more-char} specifies which character is used for
2368 this; @key{SPC} is the default.)
2369
2370 When the user has found the desired alternative from the menu, he or
2371 she should type the corresponding character---the one whose binding is
2372 that alternative.
2373
2374 @defvar menu-prompt-more-char
2375 This variable specifies the character to use to ask to see
2376 the next line of a menu. Its initial value is 32, the code
2377 for @key{SPC}.
2378 @end defvar
2379
2380 @node Menu Example
2381 @subsection Menu Example
2382 @cindex menu definition example
2383
2384 Here is a complete example of defining a menu keymap. It is the
2385 definition of the @samp{Replace} submenu in the @samp{Edit} menu in
2386 the menu bar, and it uses the extended menu item format
2387 (@pxref{Extended Menu Items}). First we create the keymap, and give
2388 it a name:
2389
2390 @smallexample
2391 (defvar menu-bar-replace-menu (make-sparse-keymap "Replace"))
2392 @end smallexample
2393
2394 @noindent
2395 Next we define the menu items:
2396
2397 @smallexample
2398 (define-key menu-bar-replace-menu [tags-repl-continue]
2399 '(menu-item "Continue Replace" tags-loop-continue
2400 :help "Continue last tags replace operation"))
2401 (define-key menu-bar-replace-menu [tags-repl]
2402 '(menu-item "Replace in tagged files" tags-query-replace
2403 :help "Interactively replace a regexp in all tagged files"))
2404 (define-key menu-bar-replace-menu [separator-replace-tags]
2405 '(menu-item "--"))
2406 ;; @r{@dots{}}
2407 @end smallexample
2408
2409 @noindent
2410 Note the symbols which the bindings are ``made for''; these appear
2411 inside square brackets, in the key sequence being defined. In some
2412 cases, this symbol is the same as the command name; sometimes it is
2413 different. These symbols are treated as ``function keys'', but they are
2414 not real function keys on the keyboard. They do not affect the
2415 functioning of the menu itself, but they are ``echoed'' in the echo area
2416 when the user selects from the menu, and they appear in the output of
2417 @code{where-is} and @code{apropos}.
2418
2419 The menu in this example is intended for use with the mouse. If a
2420 menu is intended for use with the keyboard, that is, if it is bound to
2421 a key sequence ending with a keyboard event, then the menu items
2422 should be bound to characters or ``real'' function keys, that can be
2423 typed with the keyboard.
2424
2425 The binding whose definition is @code{("--")} is a separator line.
2426 Like a real menu item, the separator has a key symbol, in this case
2427 @code{separator-replace-tags}. If one menu has two separators, they
2428 must have two different key symbols.
2429
2430 Here is how we make this menu appear as an item in the parent menu:
2431
2432 @example
2433 (define-key menu-bar-edit-menu [replace]
2434 (list 'menu-item "Replace" menu-bar-replace-menu))
2435 @end example
2436
2437 @noindent
2438 Note that this incorporates the submenu keymap, which is the value of
2439 the variable @code{menu-bar-replace-menu}, rather than the symbol
2440 @code{menu-bar-replace-menu} itself. Using that symbol in the parent
2441 menu item would be meaningless because @code{menu-bar-replace-menu} is
2442 not a command.
2443
2444 If you wanted to attach the same replace menu to a mouse click, you
2445 can do it this way:
2446
2447 @example
2448 (define-key global-map [C-S-down-mouse-1]
2449 menu-bar-replace-menu)
2450 @end example
2451
2452 @node Menu Bar
2453 @subsection The Menu Bar
2454 @cindex menu bar
2455
2456 On graphical displays, there is usually a @dfn{menu bar} at the top
2457 of each frame. @xref{Menu Bars,,,emacs, The GNU Emacs Manual}. Menu
2458 bar items are subcommands of the fake ``function key''
2459 @code{menu-bar}, as defined in the active keymaps.
2460
2461 To add an item to the menu bar, invent a fake ``function key'' of your
2462 own (let's call it @var{key}), and make a binding for the key sequence
2463 @code{[menu-bar @var{key}]}. Most often, the binding is a menu keymap,
2464 so that pressing a button on the menu bar item leads to another menu.
2465
2466 When more than one active keymap defines the same ``function key''
2467 for the menu bar, the item appears just once. If the user clicks on
2468 that menu bar item, it brings up a single, combined menu containing
2469 all the subcommands of that item---the global subcommands, the local
2470 subcommands, and the minor mode subcommands.
2471
2472 The variable @code{overriding-local-map} is normally ignored when
2473 determining the menu bar contents. That is, the menu bar is computed
2474 from the keymaps that would be active if @code{overriding-local-map}
2475 were @code{nil}. @xref{Active Keymaps}.
2476
2477 Here's an example of setting up a menu bar item:
2478
2479 @example
2480 @group
2481 ;; @r{Make a menu keymap (with a prompt string)}
2482 ;; @r{and make it the menu bar item's definition.}
2483 (define-key global-map [menu-bar words]
2484 (cons "Words" (make-sparse-keymap "Words")))
2485 @end group
2486
2487 @group
2488 ;; @r{Define specific subcommands in this menu.}
2489 (define-key global-map
2490 [menu-bar words forward]
2491 '("Forward word" . forward-word))
2492 @end group
2493 @group
2494 (define-key global-map
2495 [menu-bar words backward]
2496 '("Backward word" . backward-word))
2497 @end group
2498 @end example
2499
2500 A local keymap can cancel a menu bar item made by the global keymap by
2501 rebinding the same fake function key with @code{undefined} as the
2502 binding. For example, this is how Dired suppresses the @samp{Edit} menu
2503 bar item:
2504
2505 @example
2506 (define-key dired-mode-map [menu-bar edit] 'undefined)
2507 @end example
2508
2509 @noindent
2510 Here, @code{edit} is the fake function key used by the global map for
2511 the @samp{Edit} menu bar item. The main reason to suppress a global
2512 menu bar item is to regain space for mode-specific items.
2513
2514 @defvar menu-bar-final-items
2515 Normally the menu bar shows global items followed by items defined by the
2516 local maps.
2517
2518 This variable holds a list of fake function keys for items to display at
2519 the end of the menu bar rather than in normal sequence. The default
2520 value is @code{(help-menu)}; thus, the @samp{Help} menu item normally appears
2521 at the end of the menu bar, following local menu items.
2522 @end defvar
2523
2524 @defvar menu-bar-update-hook
2525 This normal hook is run by redisplay to update the menu bar contents,
2526 before redisplaying the menu bar. You can use it to update submenus
2527 whose contents should vary. Since this hook is run frequently, we
2528 advise you to ensure that the functions it calls do not take much time
2529 in the usual case.
2530 @end defvar
2531
2532 Next to every menu bar item, Emacs displays a key binding that runs
2533 the same command (if such a key binding exists). This serves as a
2534 convenient hint for users who do not know the key binding. If a
2535 command has multiple bindings, Emacs normally displays the first one
2536 it finds. You can specify one particular key binding by assigning an
2537 @code{:advertised-binding} symbol property to the command. @xref{Keys
2538 in Documentation}.
2539
2540 @node Tool Bar
2541 @subsection Tool bars
2542 @cindex tool bar
2543
2544 A @dfn{tool bar} is a row of clickable icons at the top of a frame,
2545 just below the menu bar. @xref{Tool Bars,,,emacs, The GNU Emacs
2546 Manual}.
2547
2548 On each frame, the frame parameter @code{tool-bar-lines} controls
2549 how many lines' worth of height to reserve for the tool bar. A zero
2550 value suppresses the tool bar. If the value is nonzero, and
2551 @code{auto-resize-tool-bars} is non-@code{nil}, the tool bar expands
2552 and contracts automatically as needed to hold the specified contents.
2553 If the value is @code{grow-only}, the tool bar expands automatically,
2554 but does not contract automatically.
2555
2556 The tool bar contents are controlled by a menu keymap attached to a
2557 fake ``function key'' called @code{tool-bar} (much like the way the menu
2558 bar is controlled). So you define a tool bar item using
2559 @code{define-key}, like this:
2560
2561 @example
2562 (define-key global-map [tool-bar @var{key}] @var{item})
2563 @end example
2564
2565 @noindent
2566 where @var{key} is a fake ``function key'' to distinguish this item from
2567 other items, and @var{item} is a menu item key binding (@pxref{Extended
2568 Menu Items}), which says how to display this item and how it behaves.
2569
2570 The usual menu keymap item properties, @code{:visible},
2571 @code{:enable}, @code{:button}, and @code{:filter}, are useful in
2572 tool bar bindings and have their normal meanings. The @var{real-binding}
2573 in the item must be a command, not a keymap; in other words, it does not
2574 work to define a tool bar icon as a prefix key.
2575
2576 The @code{:help} property specifies a ``help-echo'' string to display
2577 while the mouse is on that item. This is displayed in the same way as
2578 @code{help-echo} text properties (@pxref{Help display}).
2579
2580 In addition, you should use the @code{:image} property;
2581 this is how you specify the image to display in the tool bar:
2582
2583 @table @code
2584 @item :image @var{image}
2585 @var{images} is either a single image specification or a vector of four
2586 image specifications. If you use a vector of four,
2587 one of them is used, depending on circumstances:
2588
2589 @table @asis
2590 @item item 0
2591 Used when the item is enabled and selected.
2592 @item item 1
2593 Used when the item is enabled and deselected.
2594 @item item 2
2595 Used when the item is disabled and selected.
2596 @item item 3
2597 Used when the item is disabled and deselected.
2598 @end table
2599 @end table
2600
2601 If @var{image} is a single image specification, Emacs draws the tool bar
2602 button in disabled state by applying an edge-detection algorithm to the
2603 image.
2604
2605 The @code{:rtl} property specifies an alternative image to use for
2606 right-to-left languages. Only the GTK+ version of Emacs supports this
2607 at present.
2608
2609 Like the menu bar, the tool bar can display separators (@pxref{Menu
2610 Separators}). Tool bar separators are vertical rather than
2611 horizontal, though, and only a single style is supported. They are
2612 represented in the tool bar keymap by @code{(menu-item "--")} entries;
2613 properties like @code{:visible} are not supported for tool bar
2614 separators. Separators are rendered natively in GTK+ and Nextstep
2615 tool bars; in the other cases, they are rendered using an image of a
2616 vertical line.
2617
2618 The default tool bar is defined so that items specific to editing do not
2619 appear for major modes whose command symbol has a @code{mode-class}
2620 property of @code{special} (@pxref{Major Mode Conventions}). Major
2621 modes may add items to the global bar by binding @code{[tool-bar
2622 @var{foo}]} in their local map. It makes sense for some major modes to
2623 replace the default tool bar items completely, since not many can be
2624 accommodated conveniently, and the default bindings make this easy by
2625 using an indirection through @code{tool-bar-map}.
2626
2627 @defvar tool-bar-map
2628 By default, the global map binds @code{[tool-bar]} as follows:
2629
2630 @example
2631 (global-set-key [tool-bar]
2632 `(menu-item ,(purecopy "tool bar") ignore
2633 :filter tool-bar-make-keymap))
2634 @end example
2635
2636 @noindent
2637 The function @code{tool-bar-make-keymap}, in turn, derives the actual
2638 tool bar map dynamically from the value of the variable
2639 @code{tool-bar-map}. Hence, you should normally adjust the default
2640 (global) tool bar by changing that map. Some major modes, such as
2641 Info mode, completely replace the global tool bar by making
2642 @code{tool-bar-map} buffer-local and setting it to a different keymap.
2643 @end defvar
2644
2645 There are two convenience functions for defining tool bar items, as
2646 follows.
2647
2648 @defun tool-bar-add-item icon def key &rest props
2649 This function adds an item to the tool bar by modifying
2650 @code{tool-bar-map}. The image to use is defined by @var{icon}, which
2651 is the base name of an XPM, XBM or PBM image file to be located by
2652 @code{find-image}. Given a value @samp{"exit"}, say, @file{exit.xpm},
2653 @file{exit.pbm} and @file{exit.xbm} would be searched for in that order
2654 on a color display. On a monochrome display, the search order is
2655 @samp{.pbm}, @samp{.xbm} and @samp{.xpm}. The binding to use is the
2656 command @var{def}, and @var{key} is the fake function key symbol in the
2657 prefix keymap. The remaining arguments @var{props} are additional
2658 property list elements to add to the menu item specification.
2659
2660 To define items in some local map, bind @code{tool-bar-map} with
2661 @code{let} around calls of this function:
2662 @example
2663 (defvar foo-tool-bar-map
2664 (let ((tool-bar-map (make-sparse-keymap)))
2665 (tool-bar-add-item @dots{})
2666 @dots{}
2667 tool-bar-map))
2668 @end example
2669 @end defun
2670
2671 @defun tool-bar-add-item-from-menu command icon &optional map &rest props
2672 This function is a convenience for defining tool bar items which are
2673 consistent with existing menu bar bindings. The binding of
2674 @var{command} is looked up in the menu bar in @var{map} (default
2675 @code{global-map}) and modified to add an image specification for
2676 @var{icon}, which is found in the same way as by
2677 @code{tool-bar-add-item}. The resulting binding is then placed in
2678 @code{tool-bar-map}, so use this function only for global tool bar
2679 items.
2680
2681 @var{map} must contain an appropriate keymap bound to
2682 @code{[menu-bar]}. The remaining arguments @var{props} are additional
2683 property list elements to add to the menu item specification.
2684 @end defun
2685
2686 @defun tool-bar-local-item-from-menu command icon in-map &optional from-map &rest props
2687 This function is used for making non-global tool bar items. Use it
2688 like @code{tool-bar-add-item-from-menu} except that @var{in-map}
2689 specifies the local map to make the definition in. The argument
2690 @var{from-map} is like the @var{map} argument of
2691 @code{tool-bar-add-item-from-menu}.
2692 @end defun
2693
2694 @defvar auto-resize-tool-bars
2695 If this variable is non-@code{nil}, the tool bar automatically resizes to
2696 show all defined tool bar items---but not larger than a quarter of the
2697 frame's height.
2698
2699 If the value is @code{grow-only}, the tool bar expands automatically,
2700 but does not contract automatically. To contract the tool bar, the
2701 user has to redraw the frame by entering @kbd{C-l}.
2702
2703 If Emacs is built with GTK or Nextstep, the tool bar can only show one
2704 line, so this variable has no effect.
2705 @end defvar
2706
2707 @defvar auto-raise-tool-bar-buttons
2708 If this variable is non-@code{nil}, tool bar items display
2709 in raised form when the mouse moves over them.
2710 @end defvar
2711
2712 @defvar tool-bar-button-margin
2713 This variable specifies an extra margin to add around tool bar items.
2714 The value is an integer, a number of pixels. The default is 4.
2715 @end defvar
2716
2717 @defvar tool-bar-button-relief
2718 This variable specifies the shadow width for tool bar items.
2719 The value is an integer, a number of pixels. The default is 1.
2720 @end defvar
2721
2722 @defvar tool-bar-border
2723 This variable specifies the height of the border drawn below the tool
2724 bar area. An integer value specifies height as a number of pixels.
2725 If the value is one of @code{internal-border-width} (the default) or
2726 @code{border-width}, the tool bar border height corresponds to the
2727 corresponding frame parameter.
2728 @end defvar
2729
2730 You can define a special meaning for clicking on a tool bar item with
2731 the shift, control, meta, etc., modifiers. You do this by setting up
2732 additional items that relate to the original item through the fake
2733 function keys. Specifically, the additional items should use the
2734 modified versions of the same fake function key used to name the
2735 original item.
2736
2737 Thus, if the original item was defined this way,
2738
2739 @example
2740 (define-key global-map [tool-bar shell]
2741 '(menu-item "Shell" shell
2742 :image (image :type xpm :file "shell.xpm")))
2743 @end example
2744
2745 @noindent
2746 then here is how you can define clicking on the same tool bar image with
2747 the shift modifier:
2748
2749 @example
2750 (define-key global-map [tool-bar S-shell] 'some-command)
2751 @end example
2752
2753 @xref{Function Keys}, for more information about how to add modifiers to
2754 function keys.
2755
2756 @node Modifying Menus
2757 @subsection Modifying Menus
2758
2759 When you insert a new item in an existing menu, you probably want to
2760 put it in a particular place among the menu's existing items. If you
2761 use @code{define-key} to add the item, it normally goes at the front of
2762 the menu. To put it elsewhere in the menu, use @code{define-key-after}:
2763
2764 @defun define-key-after map key binding &optional after
2765 Define a binding in @var{map} for @var{key}, with value @var{binding},
2766 just like @code{define-key}, but position the binding in @var{map} after
2767 the binding for the event @var{after}. The argument @var{key} should be
2768 of length one---a vector or string with just one element. But
2769 @var{after} should be a single event type---a symbol or a character, not
2770 a sequence. The new binding goes after the binding for @var{after}. If
2771 @var{after} is @code{t} or is omitted, then the new binding goes last, at
2772 the end of the keymap. However, new bindings are added before any
2773 inherited keymap.
2774
2775 Here is an example:
2776
2777 @example
2778 (define-key-after my-menu [drink]
2779 '("Drink" . drink-command) 'eat)
2780 @end example
2781
2782 @noindent
2783 makes a binding for the fake function key @key{DRINK} and puts it
2784 right after the binding for @key{EAT}.
2785
2786 Here is how to insert an item called @samp{Work} in the @samp{Signals}
2787 menu of Shell mode, after the item @code{break}:
2788
2789 @example
2790 (define-key-after
2791 (lookup-key shell-mode-map [menu-bar signals])
2792 [work] '("Work" . work-command) 'break)
2793 @end example
2794 @end defun
2795
2796 @node Easy Menu
2797 @subsection Easy Menu
2798
2799 The following macro provides a convenient way to define pop-up menus
2800 and/or menu bar menus.
2801
2802 @defmac easy-menu-define symbol maps doc menu
2803 This macro defines a pop-up menu and/or menu bar submenu, whose
2804 contents are given by @var{menu}.
2805
2806 If @var{symbol} is non-@code{nil}, it should be a symbol; then this
2807 macro defines @var{symbol} as a function for popping up the menu
2808 (@pxref{Pop-Up Menus}), with @var{doc} as its documentation string.
2809 @var{symbol} should not be quoted.
2810
2811 Regardless of the value of @var{symbol}, if @var{maps} is a keymap,
2812 the menu is added to that keymap, as a top-level menu for the menu bar
2813 (@pxref{Menu Bar}). It can also be a list of keymaps, in which case
2814 the menu is added separately to each of those keymaps.
2815
2816 The first element of @var{menu} must be a string, which serves as the
2817 menu label. It may be followed by any number of the following
2818 keyword-argument pairs:
2819
2820 @table @code
2821 @item :filter @var{function}
2822 @var{function} must be a function which, if called with one
2823 argument---the list of the other menu items---returns the actual items
2824 to be displayed in the menu.
2825
2826 @item :visible @var{include}
2827 @var{include} is an expression; if it evaluates to @code{nil}, the
2828 menu is made invisible. @code{:included} is an alias for
2829 @code{:visible}.
2830
2831 @item :active @var{enable}
2832 @var{enable} is an expression; if it evaluates to @code{nil}, the menu
2833 is not selectable. @code{:enable} is an alias for @code{:active}.
2834 @end table
2835
2836 The remaining elements in @var{menu} are menu items.
2837
2838 A menu item can be a vector of three elements, @code{[@var{name}
2839 @var{callback} @var{enable}]}. @var{name} is the menu item name (a
2840 string). @var{callback} is a command to run, or an expression to
2841 evaluate, when the item is chosen. @var{enable} is an expression; if
2842 it evaluates to @code{nil}, the item is disabled for selection.
2843
2844 Alternatively, a menu item may have the form:
2845
2846 @smallexample
2847 [ @var{name} @var{callback} [ @var{keyword} @var{arg} ]... ]
2848 @end smallexample
2849
2850 @noindent
2851 where @var{name} and @var{callback} have the same meanings as above,
2852 and each optional @var{keyword} and @var{arg} pair should be one of
2853 the following:
2854
2855 @table @code
2856 @item :keys @var{keys}
2857 @var{keys} is a keyboard equivalent to the menu item (a string). This
2858 is normally not needed, as keyboard equivalents are computed
2859 automatically. @var{keys} is expanded with
2860 @code{substitute-command-keys} before it is displayed (@pxref{Keys in
2861 Documentation}).
2862
2863 @item :key-sequence @var{keys}
2864 @var{keys} is a hint for speeding up Emacs's first display of the
2865 menu. It should be nil if you know that the menu item has no keyboard
2866 equivalent; otherwise it should be a string or vector specifying a
2867 keyboard equivalent for the menu item.
2868
2869 @item :active @var{enable}
2870 @var{enable} is an expression; if it evaluates to @code{nil}, the item
2871 is make unselectable.. @code{:enable} is an alias for @code{:active}.
2872
2873 @item :visible @var{include}
2874 @var{include} is an expression; if it evaluates to @code{nil}, the
2875 item is made invisible. @code{:included} is an alias for
2876 @code{:visible}.
2877
2878 @item :label @var{form}
2879 @var{form} is an expression that is evaluated to obtain a value which
2880 serves as the menu item's label (the default is @var{name}).
2881
2882 @item :suffix @var{form}
2883 @var{form} is an expression that is dynamically evaluated and whose
2884 value is concatenated with the menu entry's label.
2885
2886 @item :style @var{style}
2887 @var{style} is a symbol describing the type of menu item; it should be
2888 @code{toggle} (a checkbox), or @code{radio} (a radio button), or
2889 anything else (meaning an ordinary menu item).
2890
2891 @item :selected @var{selected}
2892 @var{selected} is an expression; the checkbox or radio button is
2893 selected whenever the expression's value is non-nil.
2894
2895 @item :help @var{help}
2896 @var{help} is a string describing the menu item.
2897 @end table
2898
2899 Alternatively, a menu item can be a string. Then that string appears
2900 in the menu as unselectable text. A string consisting of dashes is
2901 displayed as a separator (@pxref{Menu Separators}).
2902
2903 Alternatively, a menu item can be a list with the same format as
2904 @var{menu}. This is a submenu.
2905 @end defmac
2906
2907 Here is an example of using @code{easy-menu-define} to define a menu
2908 similar to the one defined in the example in @ref{Menu Bar}:
2909
2910 @example
2911 (easy-menu-define words-menu global-map
2912 "Menu for word navigation commands."
2913 '("Words"
2914 ["Forward word" forward-word]
2915 ["Backward word" backward-word]))
2916 @end example