(Top): Update subnode menu.
[bpt/emacs.git] / lispref / keymaps.texi
CommitLineData
73804d4b
RS
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
651f374c 3@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1998, 1999, 2000, 2002, 2003,
ceb4c4d3 4@c 2004, 2005, 2006 Free Software Foundation, Inc.
73804d4b
RS
5@c See the file elisp.texi for copying conditions.
6@setfilename ../info/keymaps
7@node Keymaps, Modes, Command Loop, Top
8@chapter Keymaps
9@cindex keymap
10
11 The bindings between input events and commands are recorded in data
12structures called @dfn{keymaps}. Each binding in a keymap associates
8a36c244 13(or @dfn{binds}) an individual event type, either to another keymap or to
f9f59935
RS
14a command. When an event type is bound to a keymap, that keymap is used
15to look up the next input event; this continues until a command is
16found. The whole process is called @dfn{key lookup}.
73804d4b
RS
17
18@menu
b3540319
RS
19* Key Sequences:: Key sequences as Lisp objects.
20* Keymap Basics:: Basic concepts of keymaps.
73804d4b
RS
21* Format of Keymaps:: What a keymap looks like as a Lisp object.
22* Creating Keymaps:: Functions to create and copy keymaps.
23* Inheritance and Keymaps:: How one keymap can inherit the bindings
24 of another keymap.
25* Prefix Keys:: Defining a key with a keymap as its definition.
59e58738
RS
26* Active Keymaps:: How Emacs searches the active keymaps
27 for a key binding.
28* Searching Keymaps:: A pseudo-Lisp summary of searching active maps.
29* Controlling Active Maps:: Each buffer has a local keymap
73804d4b
RS
30 to override the standard (global) bindings.
31 A minor mode can also override them.
59e58738 32* Key Lookup:: Finding a key's binding in one keymap.
73804d4b
RS
33* Functions for Key Lookup:: How to request key lookup.
34* Changing Key Bindings:: Redefining a key in a keymap.
229644e7 35* Remapping Commands:: Bindings that translate one command to another.
73804d4b
RS
36* Key Binding Commands:: Interactive interfaces for redefining keys.
37* Scanning Keymaps:: Looking through all keymaps, for printing help.
f9f59935 38* Menu Keymaps:: Defining a menu as a keymap.
73804d4b
RS
39@end menu
40
08244b81
CY
41@node Key Sequences
42@section Key Sequences
73804d4b
RS
43@cindex key
44@cindex keystroke
b3540319
RS
45@cindex key sequence
46
47 A @dfn{key sequence}, or @dfn{key} for short, is a sequence of one
917b8227
CY
48or more input events that form a unit. Input events include
49characters, function keys, and mouse actions (@pxref{Input Events}).
50The Emacs Lisp representation for a key sequence is a string or
51vector. Unless otherwise stated, any Emacs Lisp function that accepts
52a key sequence as an argument can handle both representations.
b3540319
RS
53
54 In the string representation, alphanumeric characters ordinarily
55stand for themselves; for example, @code{"a"} represents @kbd{a} and
56and @code{"2"} represents @kbd{2}. Control character events are
57prefixed by the substring @code{"\C-"}, and meta characters by
58@code{"\M-"}; for example, @code{"\C-x"} represents the key @kbd{C-x}.
59In addition, the @key{TAB}, @key{RET}, @key{ESC}, and @key{DEL} events
60are represented by @code{"\t"}, @code{"\r"}, @code{"\e"}, and
61@code{"\d"} respectively. The string representation of a complete key
62sequence is the concatenation of the string representations of the
63constituent events; thus, @code{"\C-xl"} represents the key sequence
64@kbd{C-x l}.
65
66 Key sequences containing function keys, mouse button events, or
67non-ASCII characters such as @kbd{C-=} or @kbd{H-a} cannot be
68represented as strings; they have to be represented as vectors.
69
70 In the vector representation, each element of the vector represents
71an input event, in its Lisp form. @xref{Input Events}. For example,
72the vector @code{[?\C-x ?l]} represents the key sequence @kbd{C-x l}.
73
74 For examples of key sequences written in string and vector
75representations, @ref{Init Rebinding,,, emacs, The GNU Emacs Manual}.
76
77@defmac kbd keyseq-text
78This macro converts the text @var{keyseq-text} (a string constant)
79into a key sequence (a string or vector constant). The contents of
80@var{keyseq-text} should describe the key sequence using almost the same
81syntax used in this manual. More precisely, it uses the same syntax
82that Edit Macro mode uses for editing keyboard macros (@pxref{Edit
83Keyboard Macro,,, emacs, The GNU Emacs Manual}); you must surround
84function key names with @samp{<@dots{}>}.
85
86@example
87(kbd "C-x") @result{} "\C-x"
88(kbd "C-x C-f") @result{} "\C-x\C-f"
89(kbd "C-x 4 C-f") @result{} "\C-x4\C-f"
90(kbd "X") @result{} "X"
91(kbd "RET") @result{} "\^M"
92(kbd "C-c SPC") @result{} "\C-c@ "
93(kbd "<f1> SPC") @result{} [f1 32]
94(kbd "C-M-<down>") @result{} [C-M-down]
95@end example
96@end defmac
97
98@node Keymap Basics
99@section Keymap Basics
73804d4b
RS
100@cindex key binding
101@cindex binding of a key
102@cindex complete key
103@cindex undefined key
104
b3540319
RS
105 A keymap is a Lisp data structure that specifies @dfn{key bindings}
106for various key sequences.
73804d4b 107
b3540319
RS
108 A single keymap directly specifies definitions for individual
109events. When a key sequence consists of a single event, its binding
110in a keymap is the keymap's definition for that event. The binding of
111a longer key sequence is found by an iterative process: first find the
112definition of the first event (which must itself be a keymap); then
113find the second event's definition in that keymap, and so on until all
114the events in the key sequence have been processed.
73804d4b
RS
115
116 If the binding of a key sequence is a keymap, we call the key sequence
117a @dfn{prefix key}. Otherwise, we call it a @dfn{complete key} (because
87b2d5ff 118no more events can be added to it). If the binding is @code{nil},
73804d4b
RS
119we call the key @dfn{undefined}. Examples of prefix keys are @kbd{C-c},
120@kbd{C-x}, and @kbd{C-x 4}. Examples of defined complete keys are
121@kbd{X}, @key{RET}, and @kbd{C-x 4 C-f}. Examples of undefined complete
122keys are @kbd{C-x C-g}, and @kbd{C-c 3}. @xref{Prefix Keys}, for more
123details.
124
125 The rule for finding the binding of a key sequence assumes that the
126intermediate bindings (found for the events before the last) are all
127keymaps; if this is not so, the sequence of events does not form a
f9f59935
RS
128unit---it is not really one key sequence. In other words, removing one
129or more events from the end of any valid key sequence must always yield
130a prefix key. For example, @kbd{C-f C-n} is not a key sequence;
131@kbd{C-f} is not a prefix key, so a longer sequence starting with
132@kbd{C-f} cannot be a key sequence.
133
134 The set of possible multi-event key sequences depends on the bindings
135for prefix keys; therefore, it can be different for different keymaps,
136and can change when bindings are changed. However, a one-event sequence
137is always a key sequence, because it does not depend on any prefix keys
138for its well-formedness.
73804d4b 139
b3540319
RS
140 At any time, several primary keymaps are @dfn{active}---that is, in
141use for finding key bindings. These are the @dfn{global map}, which is
142shared by all buffers; the @dfn{local keymap}, which is usually
143associated with a specific major mode; and zero or more @dfn{minor mode
144keymaps}, which belong to currently enabled minor modes. (Not all minor
145modes have keymaps.) The local keymap bindings shadow (i.e., take
146precedence over) the corresponding global bindings. The minor mode
147keymaps shadow both local and global keymaps. @xref{Active Keymaps},
148for details.
402fe423 149
73804d4b
RS
150@node Format of Keymaps
151@section Format of Keymaps
152@cindex format of keymaps
153@cindex keymap format
154@cindex full keymap
155@cindex sparse keymap
156
08244b81 157 Each keymap is a list whose @sc{car} is the symbol @code{keymap}. The
73804d4b 158remaining elements of the list define the key bindings of the keymap.
aa2ac20c
RS
159A symbol whose function definition is a keymap is also a keymap. Use
160the function @code{keymapp} (see below) to test whether an object is a
161keymap.
73804d4b 162
f9f59935
RS
163 Several kinds of elements may appear in a keymap, after the symbol
164@code{keymap} that begins it:
87b2d5ff 165
f9f59935
RS
166@table @code
167@item (@var{type} .@: @var{binding})
168This specifies one binding, for events of type @var{type}. Each
169ordinary binding applies to events of a particular @dfn{event type},
170which is always a character or a symbol. @xref{Classifying Events}.
73804d4b 171
f9f59935 172@item (t .@: @var{binding})
73804d4b 173@cindex default key binding
f9f59935
RS
174This specifies a @dfn{default key binding}; any event not bound by other
175elements of the keymap is given @var{binding} as its binding. Default
176bindings allow a keymap to bind all possible event types without having
177to enumerate all of them. A keymap that has a default binding
db8af011
LT
178completely masks any lower-precedence keymap, except for events
179explicitly bound to @code{nil} (see below).
f9f59935 180
229644e7
RS
181@item @var{char-table}
182If an element of a keymap is a char-table, it counts as holding
183bindings for all character events with no modifier bits
184(@pxref{modifier bits}): element @var{n} is the binding for the
185character with code @var{n}. This is a compact way to record lots of
186bindings. A keymap with such a char-table is called a @dfn{full
187keymap}. Other keymaps are called @dfn{sparse keymaps}.
188
f9f59935 189@item @var{string}
73804d4b
RS
190@cindex keymap prompt string
191@cindex overall prompt string
192@cindex prompt string of keymap
f9f59935 193Aside from bindings, a keymap can also have a string as an element.
73804d4b 194This is called the @dfn{overall prompt string} and makes it possible to
e465fdc2 195use the keymap as a menu. @xref{Defining Menus}.
f9f59935 196@end table
73804d4b 197
d64b177a
SM
198When the binding is @code{nil}, it doesn't constitute a definition
199but it does take precedence over a default binding or a binding in the
200parent keymap. On the other hand, a binding of @code{nil} does
201@emph{not} override lower-precedence keymaps; thus, if the local map
202gives a binding of @code{nil}, Emacs uses the binding from the
203global map.
204
73804d4b 205@cindex meta characters lookup
f9f59935 206 Keymaps do not directly record bindings for the meta characters.
5f1f5955
GM
207Instead, meta characters are regarded for purposes of key lookup as
208sequences of two characters, the first of which is @key{ESC} (or
209whatever is currently the value of @code{meta-prefix-char}). Thus, the
210key @kbd{M-a} is internally represented as @kbd{@key{ESC} a}, and its
211global binding is found at the slot for @kbd{a} in @code{esc-map}
212(@pxref{Prefix Keys}).
213
214 This conversion applies only to characters, not to function keys or
215other input events; thus, @kbd{M-@key{end}} has nothing to do with
216@kbd{@key{ESC} @key{end}}.
73804d4b
RS
217
218 Here as an example is the local keymap for Lisp mode, a sparse
219keymap. It defines bindings for @key{DEL} and @key{TAB}, plus @kbd{C-c
220C-l}, @kbd{M-C-q}, and @kbd{M-C-x}.
221
222@example
223@group
224lisp-mode-map
177c0ea7 225@result{}
73804d4b
RS
226@end group
227@group
177c0ea7 228(keymap
8a36c244
RS
229 (3 keymap
230 ;; @kbd{C-c C-z}
231 (26 . run-lisp))
73804d4b
RS
232@end group
233@group
8a36c244
RS
234 (27 keymap
235 ;; @r{@kbd{M-C-x}, treated as @kbd{@key{ESC} C-x}}
236 (24 . lisp-send-defun)
237 keymap
238 ;; @r{@kbd{M-C-q}, treated as @kbd{@key{ESC} C-q}}
239 (17 . indent-sexp)))
73804d4b
RS
240@end group
241@group
8a36c244
RS
242 ;; @r{This part is inherited from @code{lisp-mode-shared-map}.}
243 keymap
244 ;; @key{DEL}
245 (127 . backward-delete-char-untabify)
73804d4b
RS
246@end group
247@group
177c0ea7 248 (27 keymap
73804d4b 249 ;; @r{@kbd{M-C-q}, treated as @kbd{@key{ESC} C-q}}
8a36c244
RS
250 (17 . indent-sexp))
251 (9 . lisp-indent-line))
73804d4b
RS
252@end group
253@end example
254
255@defun keymapp object
256This function returns @code{t} if @var{object} is a keymap, @code{nil}
87b2d5ff 257otherwise. More precisely, this function tests for a list whose
aa2ac20c
RS
258@sc{car} is @code{keymap}, or for a symbol whose function definition
259satisfies @code{keymapp}.
73804d4b
RS
260
261@example
262@group
263(keymapp '(keymap))
264 @result{} t
265@end group
266@group
aa2ac20c
RS
267(fset 'foo '(keymap))
268(keymapp 'foo)
269 @result{} t
270@end group
271@group
73804d4b
RS
272(keymapp (current-global-map))
273 @result{} t
274@end group
275@end example
276@end defun
277
278@node Creating Keymaps
279@section Creating Keymaps
280@cindex creating keymaps
281
282 Here we describe the functions for creating keymaps.
283
171920a6
RS
284@defun make-sparse-keymap &optional prompt
285This function creates and returns a new sparse keymap with no entries.
286(A sparse keymap is the kind of keymap you usually want.) The new
287keymap does not contain a char-table, unlike @code{make-keymap}, and
288does not bind any events.
73804d4b
RS
289
290@example
291@group
171920a6
RS
292(make-sparse-keymap)
293 @result{} (keymap)
73804d4b
RS
294@end group
295@end example
296
297If you specify @var{prompt}, that becomes the overall prompt string for
b08d86c6 298the keymap. The prompt string should be provided for menu keymaps
e465fdc2 299(@pxref{Defining Menus}).
73804d4b
RS
300@end defun
301
171920a6
RS
302@defun make-keymap &optional prompt
303This function creates and returns a new full keymap. That keymap
304contains a char-table (@pxref{Char-Tables}) with slots for all
305characters without modifiers. The new keymap initially binds all
306these characters to @code{nil}, and does not bind any other kind of
307event. The argument @var{prompt} specifies a
308prompt string, as in @code{make-sparse-keymap}.
73804d4b
RS
309
310@example
311@group
171920a6
RS
312(make-keymap)
313 @result{} (keymap #^[t nil nil nil @dots{} nil nil keymap])
73804d4b
RS
314@end group
315@end example
171920a6
RS
316
317A full keymap is more efficient than a sparse keymap when it holds
318lots of bindings; for just a few, the sparse keymap is better.
73804d4b
RS
319@end defun
320
321@defun copy-keymap keymap
87b2d5ff 322This function returns a copy of @var{keymap}. Any keymaps that
73804d4b
RS
323appear directly as bindings in @var{keymap} are also copied recursively,
324and so on to any number of levels. However, recursive copying does not
325take place when the definition of a character is a symbol whose function
326definition is a keymap; the same symbol appears in the new copy.
327@c Emacs 19 feature
328
329@example
330@group
331(setq map (copy-keymap (current-local-map)))
332@result{} (keymap
333@end group
334@group
335 ;; @r{(This implements meta characters.)}
177c0ea7 336 (27 keymap
73804d4b
RS
337 (83 . center-paragraph)
338 (115 . center-line))
339 (9 . tab-to-tab-stop))
340@end group
341
342@group
343(eq map (current-local-map))
344 @result{} nil
345@end group
346@group
347(equal map (current-local-map))
348 @result{} t
349@end group
350@end example
351@end defun
352
353@node Inheritance and Keymaps
354@section Inheritance and Keymaps
355@cindex keymap inheritance
356@cindex inheriting a keymap's bindings
357
0521d6f5
RS
358 A keymap can inherit the bindings of another keymap, which we call the
359@dfn{parent keymap}. Such a keymap looks like this:
73804d4b
RS
360
361@example
0521d6f5 362(keymap @var{bindings}@dots{} . @var{parent-keymap})
73804d4b
RS
363@end example
364
365@noindent
366The effect is that this keymap inherits all the bindings of
0521d6f5 367@var{parent-keymap}, whatever they may be at the time a key is looked up,
73804d4b
RS
368but can add to them or override them with @var{bindings}.
369
0521d6f5 370If you change the bindings in @var{parent-keymap} using @code{define-key}
73804d4b
RS
371or other key-binding functions, these changes are visible in the
372inheriting keymap unless shadowed by @var{bindings}. The converse is
373not true: if you use @code{define-key} to change the inheriting keymap,
0521d6f5
RS
374that affects @var{bindings}, but has no effect on @var{parent-keymap}.
375
376The proper way to construct a keymap with a parent is to use
377@code{set-keymap-parent}; if you have code that directly constructs a
378keymap with a parent, please convert the program to use
379@code{set-keymap-parent} instead.
380
381@defun keymap-parent keymap
382This returns the parent keymap of @var{keymap}. If @var{keymap}
383has no parent, @code{keymap-parent} returns @code{nil}.
384@end defun
385
386@defun set-keymap-parent keymap parent
387This sets the parent keymap of @var{keymap} to @var{parent}, and returns
388@var{parent}. If @var{parent} is @code{nil}, this function gives
389@var{keymap} no parent at all.
390
391If @var{keymap} has submaps (bindings for prefix keys), they too receive
392new parent keymaps that reflect what @var{parent} specifies for those
393prefix keys.
394@end defun
73804d4b 395
6a0f8bed 396 Here is an example showing how to make a keymap that inherits
73804d4b
RS
397from @code{text-mode-map}:
398
399@example
0521d6f5
RS
400(let ((map (make-sparse-keymap)))
401 (set-keymap-parent map text-mode-map)
402 map)
73804d4b
RS
403@end example
404
6a0f8bed
RS
405 A non-sparse keymap can have a parent too, but this is not very
406useful. A non-sparse keymap always specifies something as the binding
407for every numeric character code without modifier bits, even if it is
408@code{nil}, so these character's bindings are never inherited from
409the parent keymap.
410
73804d4b
RS
411@node Prefix Keys
412@section Prefix Keys
413@cindex prefix key
414
f9f59935 415 A @dfn{prefix key} is a key sequence whose binding is a keymap. The
969fe9b5 416keymap defines what to do with key sequences that extend the prefix key.
f9f59935
RS
417For example, @kbd{C-x} is a prefix key, and it uses a keymap that is
418also stored in the variable @code{ctl-x-map}. This keymap defines
419bindings for key sequences starting with @kbd{C-x}.
420
1911e6e5
RS
421 Some of the standard Emacs prefix keys use keymaps that are
422also found in Lisp variables:
73804d4b
RS
423
424@itemize @bullet
425@item
426@vindex esc-map
427@findex ESC-prefix
f9f59935
RS
428@code{esc-map} is the global keymap for the @key{ESC} prefix key. Thus,
429the global definitions of all meta characters are actually found here.
430This map is also the function definition of @code{ESC-prefix}.
73804d4b
RS
431
432@item
433@cindex @kbd{C-h}
a9f0a989 434@code{help-map} is the global keymap for the @kbd{C-h} prefix key.
73804d4b
RS
435
436@item
437@cindex @kbd{C-c}
438@vindex mode-specific-map
f9f59935
RS
439@code{mode-specific-map} is the global keymap for the prefix key
440@kbd{C-c}. This map is actually global, not mode-specific, but its name
441provides useful information about @kbd{C-c} in the output of @kbd{C-h b}
442(@code{display-bindings}), since the main use of this prefix key is for
443mode-specific bindings.
73804d4b
RS
444
445@item
446@cindex @kbd{C-x}
447@vindex ctl-x-map
448@findex Control-X-prefix
a9f0a989
RS
449@code{ctl-x-map} is the global keymap used for the @kbd{C-x} prefix key.
450This map is found via the function cell of the symbol
f9f59935 451@code{Control-X-prefix}.
73804d4b 452
1911e6e5
RS
453@item
454@cindex @kbd{C-x @key{RET}}
455@vindex mule-keymap
456@code{mule-keymap} is the global keymap used for the @kbd{C-x @key{RET}}
457prefix key.
458
73804d4b
RS
459@item
460@cindex @kbd{C-x 4}
461@vindex ctl-x-4-map
f9f59935
RS
462@code{ctl-x-4-map} is the global keymap used for the @kbd{C-x 4} prefix
463key.
73804d4b
RS
464
465@c Emacs 19 feature
466@item
467@cindex @kbd{C-x 5}
468@vindex ctl-x-5-map
f9f59935
RS
469@code{ctl-x-5-map} is the global keymap used for the @kbd{C-x 5} prefix
470key.
73804d4b
RS
471
472@c Emacs 19 feature
473@item
1911e6e5
RS
474@cindex @kbd{C-x 6}
475@vindex 2C-mode-map
476@code{2C-mode-map} is the global keymap used for the @kbd{C-x 6} prefix
477key.
478
479@item
480@cindex @kbd{C-x v}
481@vindex vc-prefix-map
482@code{vc-prefix-map} is the global keymap used for the @kbd{C-x v} prefix
483key.
484
485@item
f141c9bb 486@cindex @kbd{M-o}
1911e6e5 487@vindex facemenu-keymap
f141c9bb 488@code{facemenu-keymap} is the global keymap used for the @kbd{M-o}
1911e6e5
RS
489prefix key.
490
491@c Emacs 19 feature
492@item
c5568a11
LT
493The other Emacs prefix keys are @kbd{M-g}, @kbd{C-x @@}, @kbd{C-x a i},
494@kbd{C-x @key{ESC}} and @kbd{@key{ESC} @key{ESC}}. They use keymaps
495that have no special names.
73804d4b
RS
496@end itemize
497
f9f59935
RS
498 The keymap binding of a prefix key is used for looking up the event
499that follows the prefix key. (It may instead be a symbol whose function
500definition is a keymap. The effect is the same, but the symbol serves
501as a name for the prefix key.) Thus, the binding of @kbd{C-x} is the
a9f0a989 502symbol @code{Control-X-prefix}, whose function cell holds the keymap
f9f59935 503for @kbd{C-x} commands. (The same keymap is also the value of
73804d4b
RS
504@code{ctl-x-map}.)
505
87b2d5ff
RS
506 Prefix key definitions can appear in any active keymap. The
507definitions of @kbd{C-c}, @kbd{C-x}, @kbd{C-h} and @key{ESC} as prefix
508keys appear in the global map, so these prefix keys are always
73804d4b
RS
509available. Major and minor modes can redefine a key as a prefix by
510putting a prefix key definition for it in the local map or the minor
511mode's map. @xref{Active Keymaps}.
512
513 If a key is defined as a prefix in more than one active map, then its
514various definitions are in effect merged: the commands defined in the
515minor mode keymaps come first, followed by those in the local map's
516prefix definition, and then by those from the global map.
517
518 In the following example, we make @kbd{C-p} a prefix key in the local
519keymap, in such a way that @kbd{C-p} is identical to @kbd{C-x}. Then
520the binding for @kbd{C-p C-f} is the function @code{find-file}, just
521like @kbd{C-x C-f}. The key sequence @kbd{C-p 6} is not found in any
522active keymap.
523
524@example
525@group
526(use-local-map (make-sparse-keymap))
527 @result{} nil
528@end group
529@group
530(local-set-key "\C-p" ctl-x-map)
531 @result{} nil
532@end group
533@group
534(key-binding "\C-p\C-f")
535 @result{} find-file
536@end group
537
538@group
539(key-binding "\C-p6")
540 @result{} nil
541@end group
542@end example
543
b6954afd 544@defun define-prefix-command symbol &optional mapvar prompt
73804d4b 545@cindex prefix command
db8af011 546@anchor{Definition of define-prefix-command}
f9f59935 547This function prepares @var{symbol} for use as a prefix key's binding:
62f20204 548it creates a sparse keymap and stores it as @var{symbol}'s function
f9f59935 549definition. Subsequently binding a key sequence to @var{symbol} will
b6954afd 550make that key sequence into a prefix key. The return value is @code{symbol}.
f9f59935
RS
551
552This function also sets @var{symbol} as a variable, with the keymap as
b6954afd
RS
553its value. But if @var{mapvar} is non-@code{nil}, it sets @var{mapvar}
554as a variable instead.
f9f59935 555
b6954afd 556If @var{prompt} is non-@code{nil}, that becomes the overall prompt
b08d86c6 557string for the keymap. The prompt string should be given for menu keymaps
e465fdc2 558(@pxref{Defining Menus}).
73804d4b
RS
559@end defun
560
87b2d5ff
RS
561@node Active Keymaps
562@section Active Keymaps
563@cindex active keymap
564@cindex global keymap
565@cindex local keymap
73804d4b 566
d38edfc3 567 Emacs normally contains many keymaps; at any given time, just a few
59e58738 568of them are @dfn{active}, meaning that they participate in the
d38edfc3
RS
569interpretation of user input. All the active keymaps are used
570together to determine what command to execute when a key is entered.
571Emacs searches these keymaps one by one, in a standard order, until it
59e58738 572finds a binding in one of the keymaps.
d38edfc3
RS
573
574 Normally the active keymaps are the @code{keymap} property keymap,
575the keymaps of any enabled minor modes, the current buffer's local
576keymap, and the global keymap, in that order. Therefore, Emacs
59e58738
RS
577searches for each input key sequence in all these keymaps. Here is a
578pseudo-Lisp description of how this process works:
579
580@lisp
581(or (if overriding-terminal-local-map
582 (@var{find-in} overriding-terminal-local-map)
583 (if overriding-local-map
584 (@var{find-in} overriding-local-map)
585 (or (@var{find-in} (get-text-property (point) 'keymap))
586 (@var{find-in-any} emulation-mode-map-alists)
587 (@var{find-in-any} minor-mode-overriding-map-alist)
588 (@var{find-in-any} minor-mode-map-alist)
e98a14ff 589 (if (get-text-property (point) 'local-map)
59e58738
RS
590 (@var{find-in} (get-text-property (point) 'local-map))
591 (@var{find-in} (current-local-map))))))
592 (@var{find-in} (current-global-map)))
593@end lisp
594
595@noindent
596Here, the pseudo-function @var{find-in} means to look up the key
597sequence in a single map, and @var{find-in-any} means to search the
598appropriate keymaps from an alist. (Searching a single keymap for a
599binding is called @dfn{key lookup}; see @ref{Key Lookup}.)
73804d4b 600
87b2d5ff
RS
601 The @dfn{global keymap} holds the bindings of keys that are defined
602regardless of the current buffer, such as @kbd{C-f}. The variable
603@code{global-map} holds this keymap, which is always active.
73804d4b 604
d38edfc3
RS
605 Each buffer may have another keymap, its @dfn{local keymap}, which
606may contain new or overriding definitions for keys. The current
607buffer's local keymap is always active except when
608@code{overriding-local-map} overrides it. The @code{local-map} text
609or overlay property can specify an alternative local keymap for certain
610parts of the buffer; see @ref{Special Properties}.
73804d4b 611
a9f0a989 612 Each minor mode can have a keymap; if it does, the keymap is active
d38edfc3
RS
613when the minor mode is enabled. Modes for emulation can specify
614additional active keymaps through the variable
615@code{emulation-mode-map-alists}.
616
8ed9e36a 617 The highest precedence normal keymap comes from the @code{keymap}
d38edfc3
RS
618text or overlay property. If that is non-@code{nil}, it is the first
619keymap to be processed, in normal circumstances.
620
7fdc81ab 621 However, there are also special ways for programs to substitute
8a36c244 622other keymaps for some of those. The variable
d38edfc3
RS
623@code{overriding-local-map}, if non-@code{nil}, specifies a keymap
624that replaces all the usual active keymaps except the global keymap.
625Another way to do this is with @code{overriding-terminal-local-map};
626it operates on a per-terminal basis. These variables are documented
627below.
73804d4b 628
87b2d5ff
RS
629@cindex major mode keymap
630 Since every buffer that uses the same major mode normally uses the
631same local keymap, you can think of the keymap as local to the mode. A
632change to the local keymap of a buffer (using @code{local-set-key}, for
633example) is seen also in the other buffers that share that keymap.
73804d4b 634
969fe9b5 635 The local keymaps that are used for Lisp mode and some other major
d38edfc3 636modes exist even if they have not yet been used. These local keymaps are
969fe9b5
RS
637the values of variables such as @code{lisp-mode-map}. For most major
638modes, which are less frequently used, the local keymap is constructed
639only when the mode is used for the first time in a session.
73804d4b 640
87b2d5ff
RS
641 The minibuffer has local keymaps, too; they contain various completion
642and exit commands. @xref{Intro to Minibuffers}.
73804d4b 643
a9f0a989
RS
644 Emacs has other keymaps that are used in a different way---translating
645events within @code{read-key-sequence}. @xref{Translating Input}.
646
87b2d5ff 647 @xref{Standard Keymaps}, for a list of standard keymaps.
73804d4b 648
59e58738
RS
649@defun current-active-maps &optional olp
650This returns the list of active keymaps that would be used by the
651command loop in the current circumstances to look up a key sequence.
652Normally it ignores @code{overriding-local-map} and
653@code{overriding-terminal-local-map}, but if @var{olp} is
654non-@code{nil} then it pays attention to them.
655@end defun
656
657@defun key-binding key &optional accept-defaults no-remap
658This function returns the binding for @var{key} according to the
659current active keymaps. The result is @code{nil} if @var{key} is
660undefined in the keymaps.
661
662@c Emacs 19 feature
663The argument @var{accept-defaults} controls checking for default
664bindings, as in @code{lookup-key} (above).
665
666When commands are remapped (@pxref{Remapping Commands}),
667@code{key-binding} normally processes command remappings so as to
668returns the remapped command that will actually be executed. However,
669if @var{no-remap} is non-@code{nil}, @code{key-binding} ignores
670remappings and returns the binding directly specified for @var{key}.
671
672An error is signaled if @var{key} is not a string or a vector.
673
674@example
675@group
676(key-binding "\C-x\C-f")
677 @result{} find-file
678@end group
679@end example
680@end defun
681
682@node Searching Keymaps
683@section Searching the Active Keymaps
684
685 After translation of the input events (@pxref{Translating Input})
686Emacs looks for them in the active keymaps. Here is a pseudo-Lisp
687description of the order in which the active keymaps are searched:
688
689@lisp
690(or (if overriding-terminal-local-map
691 (@var{find-in} overriding-terminal-local-map)
692 (if overriding-local-map
693 (@var{find-in} overriding-local-map)
694 (or (@var{find-in} (get-text-property (point) 'keymap))
695 (@var{find-in-any} emulation-mode-map-alists)
696 (@var{find-in-any} minor-mode-overriding-map-alist)
697 (@var{find-in-any} minor-mode-map-alist)
e98a14ff
EZ
698 (if (get-text-property (point) 'local-map)
699 (@var{find-in} (get-text-property (point) 'local-map))
700 (@var{find-in} (current-local-map))))))
59e58738
RS
701 (@var{find-in} (current-global-map)))
702@end lisp
703
704@noindent
705The @var{find-in} and @var{find-in-any} are pseudo functions that
e98a14ff 706search in one keymap and in an alist of keymaps, respectively.
59e58738
RS
707
708@enumerate
709@item
710The function finally found may be remapped
711(@pxref{Remapping Commands}).
712
713@item
714Characters that are bound to @code{self-insert-command} are translated
715according to @code{translation-table-for-input} before insertion.
716
717@item
718@code{current-active-maps} returns a list of the
719currently active keymaps at point.
720
721@item
722When a match is found (@pxref{Key Lookup}), if the binding in the
723keymap is a function, the search is over. However if the keymap entry
724is a symbol with a value or a string, Emacs replaces the input key
725sequences with the variable's value or the string, and restarts the
726search of the active keymaps.
727@end enumerate
728
729@node Controlling Active Maps
730@section Controlling the Active Keymaps
731
87b2d5ff
RS
732@defvar global-map
733This variable contains the default global keymap that maps Emacs
59e58738
RS
734keyboard input to commands. The global keymap is normally this
735keymap. The default global keymap is a full keymap that binds
87b2d5ff 736@code{self-insert-command} to all of the printing characters.
73804d4b 737
d38edfc3 738It is normal practice to change the bindings in the global keymap, but you
87b2d5ff
RS
739should not assign this variable any value other than the keymap it starts
740out with.
741@end defvar
73804d4b 742
87b2d5ff
RS
743@defun current-global-map
744This function returns the current global keymap. This is the
745same as the value of @code{global-map} unless you change one or the
746other.
73804d4b 747
73804d4b 748@example
87b2d5ff
RS
749@group
750(current-global-map)
177c0ea7 751@result{} (keymap [set-mark-command beginning-of-line @dots{}
87b2d5ff
RS
752 delete-backward-char])
753@end group
73804d4b 754@end example
87b2d5ff 755@end defun
73804d4b 756
87b2d5ff
RS
757@defun current-local-map
758This function returns the current buffer's local keymap, or @code{nil}
759if it has none. In the following example, the keymap for the
760@samp{*scratch*} buffer (using Lisp Interaction mode) is a sparse keymap
ad800164 761in which the entry for @key{ESC}, @acronym{ASCII} code 27, is another sparse
87b2d5ff 762keymap.
73804d4b 763
87b2d5ff
RS
764@example
765@group
766(current-local-map)
177c0ea7
JB
767@result{} (keymap
768 (10 . eval-print-last-sexp)
769 (9 . lisp-indent-line)
770 (127 . backward-delete-char-untabify)
87b2d5ff
RS
771@end group
772@group
177c0ea7
JB
773 (27 keymap
774 (24 . eval-defun)
87b2d5ff
RS
775 (17 . indent-sexp)))
776@end group
777@end example
778@end defun
73804d4b 779
87b2d5ff
RS
780@defun current-minor-mode-maps
781This function returns a list of the keymaps of currently enabled minor modes.
782@end defun
73804d4b 783
87b2d5ff
RS
784@defun use-global-map keymap
785This function makes @var{keymap} the new current global keymap. It
786returns @code{nil}.
73804d4b 787
87b2d5ff
RS
788It is very unusual to change the global keymap.
789@end defun
73804d4b 790
87b2d5ff
RS
791@defun use-local-map keymap
792This function makes @var{keymap} the new local keymap of the current
793buffer. If @var{keymap} is @code{nil}, then the buffer has no local
794keymap. @code{use-local-map} returns @code{nil}. Most major mode
795commands use this function.
796@end defun
73804d4b 797
87b2d5ff
RS
798@c Emacs 19 feature
799@defvar minor-mode-map-alist
ca1b0914 800@anchor{Definition of minor-mode-map-alist}
87b2d5ff
RS
801This variable is an alist describing keymaps that may or may not be
802active according to the values of certain variables. Its elements look
803like this:
73804d4b 804
87b2d5ff
RS
805@example
806(@var{variable} . @var{keymap})
807@end example
73804d4b 808
87b2d5ff
RS
809The keymap @var{keymap} is active whenever @var{variable} has a
810non-@code{nil} value. Typically @var{variable} is the variable that
811enables or disables a minor mode. @xref{Keymaps and Minor Modes}.
73804d4b 812
87b2d5ff
RS
813Note that elements of @code{minor-mode-map-alist} do not have the same
814structure as elements of @code{minor-mode-alist}. The map must be the
a40d4712
PR
815@sc{cdr} of the element; a list with the map as the second element will
816not do. The @sc{cdr} can be either a keymap (a list) or a symbol whose
817function definition is a keymap.
73804d4b 818
8a36c244
RS
819When more than one minor mode keymap is active, the earlier one in
820@code{minor-mode-map-alist} takes priority. But you should design
87b2d5ff
RS
821minor modes so that they don't interfere with each other. If you do
822this properly, the order will not matter.
73804d4b 823
f9f59935
RS
824See @ref{Keymaps and Minor Modes}, for more information about minor
825modes. See also @code{minor-mode-key-binding} (@pxref{Functions for Key
826Lookup}).
827@end defvar
828
f9f59935
RS
829@defvar minor-mode-overriding-map-alist
830This variable allows major modes to override the key bindings for
831particular minor modes. The elements of this alist look like the
832elements of @code{minor-mode-map-alist}: @code{(@var{variable}
a9f0a989
RS
833. @var{keymap})}.
834
1911e6e5 835If a variable appears as an element of
a9f0a989
RS
836@code{minor-mode-overriding-map-alist}, the map specified by that
837element totally replaces any map specified for the same variable in
838@code{minor-mode-map-alist}.
f9f59935 839
969fe9b5
RS
840@code{minor-mode-overriding-map-alist} is automatically buffer-local in
841all buffers.
87b2d5ff 842@end defvar
73804d4b 843
87b2d5ff
RS
844@defvar overriding-local-map
845If non-@code{nil}, this variable holds a keymap to use instead of the
d38edfc3
RS
846buffer's local keymap, any text property or overlay keymaps, and any
847minor mode keymaps. This keymap, if specified, overrides all other
848maps that would have been active, except for the current global map.
73804d4b
RS
849@end defvar
850
5fe8e44d
RS
851@defvar overriding-terminal-local-map
852If non-@code{nil}, this variable holds a keymap to use instead of
db8af011
LT
853@code{overriding-local-map}, the buffer's local keymap, text property
854or overlay keymaps, and all the minor mode keymaps.
5fe8e44d
RS
855
856This variable is always local to the current terminal and cannot be
857buffer-local. @xref{Multiple Displays}. It is used to implement
858incremental search mode.
859@end defvar
860
4b4b65a6
RS
861@defvar overriding-local-map-menu-flag
862If this variable is non-@code{nil}, the value of
863@code{overriding-local-map} or @code{overriding-terminal-local-map} can
864affect the display of the menu bar. The default value is @code{nil}, so
865those map variables have no effect on the menu bar.
866
867Note that these two map variables do affect the execution of key
868sequences entered using the menu bar, even if they do not affect the
869menu bar display. So if a menu bar key sequence comes in, you should
870clear the variables before looking up and executing that key sequence.
871Modes that use the variables would typically do this anyway; normally
872they respond to events that they do not handle by ``unreading'' them and
873exiting.
874@end defvar
875
f9f59935
RS
876@defvar special-event-map
877This variable holds a keymap for special events. If an event type has a
878binding in this keymap, then it is special, and the binding for the
879event is run directly by @code{read-event}. @xref{Special Events}.
880@end defvar
881
229644e7
RS
882@defvar emulation-mode-map-alists
883This variable holds a list of keymap alists to use for emulations
884modes. It is intended for modes or packages using multiple minor-mode
885keymaps. Each element is a keymap alist which has the same format and
886meaning as @code{minor-mode-map-alist}, or a symbol with a variable
887binding which is such an alist. The ``active'' keymaps in each alist
888are used before @code{minor-mode-map-alist} and
889@code{minor-mode-overriding-map-alist}.
890@end defvar
891
87b2d5ff
RS
892@node Key Lookup
893@section Key Lookup
894@cindex key lookup
895@cindex keymap entry
73804d4b 896
87b2d5ff 897 @dfn{Key lookup} is the process of finding the binding of a key
59e58738
RS
898sequence from a given keymap. The execution or use of the binding is
899not part of key lookup.
73804d4b 900
f9f59935
RS
901 Key lookup uses just the event type of each event in the key sequence;
902the rest of the event is ignored. In fact, a key sequence used for key
8a36c244
RS
903lookup may designate a mouse event with just its types (a symbol)
904instead of the entire event (a list). @xref{Input Events}. Such
59e58738 905a ``key sequence'' is insufficient for @code{command-execute} to run,
f9f59935 906but it is sufficient for looking up or rebinding a key.
73804d4b 907
87b2d5ff
RS
908 When the key sequence consists of multiple events, key lookup
909processes the events sequentially: the binding of the first event is
910found, and must be a keymap; then the second event's binding is found in
911that keymap, and so on until all the events in the key sequence are used
912up. (The binding thus found for the last event may or may not be a
913keymap.) Thus, the process of key lookup is defined in terms of a
914simpler process for looking up a single event in a keymap. How that is
915done depends on the type of object associated with the event in that
916keymap.
73804d4b 917
87b2d5ff
RS
918 Let's use the term @dfn{keymap entry} to describe the value found by
919looking up an event type in a keymap. (This doesn't include the item
969fe9b5 920string and other extra elements in menu key bindings, because
87b2d5ff
RS
921@code{lookup-key} and other key lookup functions don't include them in
922the returned value.) While any Lisp object may be stored in a keymap as
969fe9b5 923a keymap entry, not all make sense for key lookup. Here is a table of
87b2d5ff 924the meaningful kinds of keymap entries:
73804d4b 925
87b2d5ff
RS
926@table @asis
927@item @code{nil}
928@cindex @code{nil} in keymap
929@code{nil} means that the events used so far in the lookup form an
930undefined key. When a keymap fails to mention an event type at all, and
931has no default binding, that is equivalent to a binding of @code{nil}
932for that event type.
73804d4b 933
87b2d5ff
RS
934@item @var{command}
935@cindex command in keymap
936The events used so far in the lookup form a complete key,
937and @var{command} is its binding. @xref{What Is a Function}.
73804d4b 938
bfe721d1 939@item @var{array}
87b2d5ff 940@cindex string in keymap
bfe721d1
KH
941The array (either a string or a vector) is a keyboard macro. The events
942used so far in the lookup form a complete key, and the array is its
943binding. See @ref{Keyboard Macros}, for more information.
73804d4b 944
969fe9b5
RS
945@item @var{keymap}
946@cindex keymap in keymap
947The events used so far in the lookup form a prefix key. The next
948event of the key sequence is looked up in @var{keymap}.
949
87b2d5ff
RS
950@item @var{list}
951@cindex list in keymap
952The meaning of a list depends on the types of the elements of the list.
73804d4b 953
87b2d5ff
RS
954@itemize @bullet
955@item
956If the @sc{car} of @var{list} is the symbol @code{keymap}, then the list
957is a keymap, and is treated as a keymap (see above).
73804d4b 958
87b2d5ff
RS
959@item
960@cindex @code{lambda} in keymap
961If the @sc{car} of @var{list} is @code{lambda}, then the list is a
91055930
RS
962lambda expression. This is presumed to be a function, and is treated
963as such (see above). In order to execute properly as a key binding,
964this function must be a command---it must have an @code{interactive}
965specification. @xref{Defining Commands}.
73804d4b 966
87b2d5ff
RS
967@item
968If the @sc{car} of @var{list} is a keymap and the @sc{cdr} is an event
969type, then this is an @dfn{indirect entry}:
73804d4b
RS
970
971@example
87b2d5ff 972(@var{othermap} . @var{othertype})
73804d4b
RS
973@end example
974
87b2d5ff
RS
975When key lookup encounters an indirect entry, it looks up instead the
976binding of @var{othertype} in @var{othermap} and uses that.
73804d4b 977
87b2d5ff
RS
978This feature permits you to define one key as an alias for another key.
979For example, an entry whose @sc{car} is the keymap called @code{esc-map}
bfe721d1 980and whose @sc{cdr} is 32 (the code for @key{SPC}) means, ``Use the global
87b2d5ff
RS
981binding of @kbd{Meta-@key{SPC}}, whatever that may be.''
982@end itemize
73804d4b 983
87b2d5ff
RS
984@item @var{symbol}
985@cindex symbol in keymap
986The function definition of @var{symbol} is used in place of
987@var{symbol}. If that too is a symbol, then this process is repeated,
988any number of times. Ultimately this should lead to an object that is
f9f59935 989a keymap, a command, or a keyboard macro. A list is allowed if it is a
87b2d5ff
RS
990keymap or a command, but indirect entries are not understood when found
991via symbols.
73804d4b 992
87b2d5ff
RS
993Note that keymaps and keyboard macros (strings and vectors) are not
994valid functions, so a symbol with a keymap, string, or vector as its
995function definition is invalid as a function. It is, however, valid as
996a key binding. If the definition is a keyboard macro, then the symbol
997is also valid as an argument to @code{command-execute}
998(@pxref{Interactive Call}).
73804d4b 999
87b2d5ff
RS
1000@cindex @code{undefined} in keymap
1001The symbol @code{undefined} is worth special mention: it means to treat
1002the key as undefined. Strictly speaking, the key is defined, and its
1003binding is the command @code{undefined}; but that command does the same
1004thing that is done automatically for an undefined key: it rings the bell
1005(by calling @code{ding}) but does not signal an error.
73804d4b 1006
87b2d5ff
RS
1007@cindex preventing prefix key
1008@code{undefined} is used in local keymaps to override a global key
1009binding and make the key ``undefined'' locally. A local binding of
1010@code{nil} would fail to do this because it would not override the
1011global binding.
1012
1013@item @var{anything else}
1014If any other type of object is found, the events used so far in the
1015lookup form a complete key, and the object is its binding, but the
1016binding is not executable as a command.
1017@end table
1018
1019 In short, a keymap entry may be a keymap, a command, a keyboard macro,
1020a symbol that leads to one of them, or an indirection or @code{nil}.
1021Here is an example of a sparse keymap with two characters bound to
1022commands and one bound to another keymap. This map is the normal value
1023of @code{emacs-lisp-mode-map}. Note that 9 is the code for @key{TAB},
1024127 for @key{DEL}, 27 for @key{ESC}, 17 for @kbd{C-q} and 24 for
1025@kbd{C-x}.
73804d4b
RS
1026
1027@example
87b2d5ff
RS
1028@group
1029(keymap (9 . lisp-indent-line)
1030 (127 . backward-delete-char-untabify)
1031 (27 keymap (17 . indent-sexp) (24 . eval-defun)))
1032@end group
73804d4b
RS
1033@end example
1034
87b2d5ff
RS
1035@node Functions for Key Lookup
1036@section Functions for Key Lookup
73804d4b 1037
87b2d5ff 1038 Here are the functions and variables pertaining to key lookup.
73804d4b 1039
87b2d5ff 1040@defun lookup-key keymap key &optional accept-defaults
969fe9b5
RS
1041This function returns the definition of @var{key} in @var{keymap}. All
1042the other functions described in this chapter that look up keys use
1043@code{lookup-key}. Here are examples:
73804d4b 1044
87b2d5ff
RS
1045@example
1046@group
1047(lookup-key (current-global-map) "\C-x\C-f")
1048 @result{} find-file
1049@end group
1050@group
402fe423
RS
1051(lookup-key (current-global-map) (kbd "C-x C-f"))
1052 @result{} find-file
1053@end group
1054@group
87b2d5ff
RS
1055(lookup-key (current-global-map) "\C-x\C-f12345")
1056 @result{} 2
1057@end group
1058@end example
73804d4b 1059
969fe9b5
RS
1060If the string or vector @var{key} is not a valid key sequence according
1061to the prefix keys specified in @var{keymap}, it must be ``too long''
1062and have extra events at the end that do not fit into a single key
1063sequence. Then the value is a number, the number of events at the front
1064of @var{key} that compose a complete key.
1065
1066@c Emacs 19 feature
1067If @var{accept-defaults} is non-@code{nil}, then @code{lookup-key}
1068considers default bindings as well as bindings for the specific events
1069in @var{key}. Otherwise, @code{lookup-key} reports only bindings for
1070the specific sequence @var{key}, ignoring default bindings except when
1071you explicitly ask about them. (To do this, supply @code{t} as an
1072element of @var{key}; see @ref{Format of Keymaps}.)
1073
5f1f5955
GM
1074If @var{key} contains a meta character (not a function key), that
1075character is implicitly replaced by a two-character sequence: the value
1076of @code{meta-prefix-char}, followed by the corresponding non-meta
87b2d5ff
RS
1077character. Thus, the first example below is handled by conversion into
1078the second example.
73804d4b
RS
1079
1080@example
1081@group
87b2d5ff
RS
1082(lookup-key (current-global-map) "\M-f")
1083 @result{} forward-word
1084@end group
1085@group
1086(lookup-key (current-global-map) "\ef")
1087 @result{} forward-word
73804d4b
RS
1088@end group
1089@end example
87b2d5ff
RS
1090
1091Unlike @code{read-key-sequence}, this function does not modify the
1092specified events in ways that discard information (@pxref{Key Sequence
1093Input}). In particular, it does not convert letters to lower case and
1094it does not change drag events to clicks.
73804d4b
RS
1095@end defun
1096
87b2d5ff
RS
1097@deffn Command undefined
1098Used in keymaps to undefine keys. It calls @code{ding}, but does
1099not cause an error.
1100@end deffn
1101
87b2d5ff
RS
1102@defun local-key-binding key &optional accept-defaults
1103This function returns the binding for @var{key} in the current
1104local keymap, or @code{nil} if it is undefined there.
73804d4b 1105
87b2d5ff
RS
1106@c Emacs 19 feature
1107The argument @var{accept-defaults} controls checking for default bindings,
1108as in @code{lookup-key} (above).
73804d4b
RS
1109@end defun
1110
87b2d5ff
RS
1111@defun global-key-binding key &optional accept-defaults
1112This function returns the binding for command @var{key} in the
1113current global keymap, or @code{nil} if it is undefined there.
73804d4b
RS
1114
1115@c Emacs 19 feature
87b2d5ff
RS
1116The argument @var{accept-defaults} controls checking for default bindings,
1117as in @code{lookup-key} (above).
1118@end defun
73804d4b 1119
87b2d5ff
RS
1120@c Emacs 19 feature
1121@defun minor-mode-key-binding key &optional accept-defaults
1122This function returns a list of all the active minor mode bindings of
1123@var{key}. More precisely, it returns an alist of pairs
1124@code{(@var{modename} . @var{binding})}, where @var{modename} is the
1125variable that enables the minor mode, and @var{binding} is @var{key}'s
1126binding in that mode. If @var{key} has no minor-mode bindings, the
1127value is @code{nil}.
73804d4b 1128
f9f59935
RS
1129If the first binding found is not a prefix definition (a keymap or a
1130symbol defined as a keymap), all subsequent bindings from other minor
1131modes are omitted, since they would be completely shadowed. Similarly,
1132the list omits non-prefix bindings that follow prefix bindings.
73804d4b 1133
87b2d5ff
RS
1134The argument @var{accept-defaults} controls checking for default
1135bindings, as in @code{lookup-key} (above).
1136@end defun
73804d4b 1137
87b2d5ff
RS
1138@defvar meta-prefix-char
1139@cindex @key{ESC}
59e58738 1140This variable is the meta-prefix character code. It is used for
87b2d5ff 1141translating a meta character to a two-character sequence so it can be
59e58738
RS
1142looked up in a keymap. For useful results, the value should be a
1143prefix event (@pxref{Prefix Keys}). The default value is 27, which is
1144the @acronym{ASCII} code for @key{ESC}.
73804d4b 1145
5f1f5955
GM
1146As long as the value of @code{meta-prefix-char} remains 27, key lookup
1147translates @kbd{M-b} into @kbd{@key{ESC} b}, which is normally defined
1148as the @code{backward-word} command. However, if you were to set
87b2d5ff
RS
1149@code{meta-prefix-char} to 24, the code for @kbd{C-x}, then Emacs will
1150translate @kbd{M-b} into @kbd{C-x b}, whose standard binding is the
5f1f5955
GM
1151@code{switch-to-buffer} command. (Don't actually do this!) Here is an
1152illustration of what would happen:
73804d4b 1153
87b2d5ff
RS
1154@smallexample
1155@group
1156meta-prefix-char ; @r{The default value.}
1157 @result{} 27
1158@end group
1159@group
1160(key-binding "\M-b")
1161 @result{} backward-word
1162@end group
1163@group
1164?\C-x ; @r{The print representation}
1165 @result{} 24 ; @r{of a character.}
1166@end group
1167@group
1168(setq meta-prefix-char 24)
177c0ea7 1169 @result{} 24
87b2d5ff
RS
1170@end group
1171@group
1172(key-binding "\M-b")
1173 @result{} switch-to-buffer ; @r{Now, typing @kbd{M-b} is}
1174 ; @r{like typing @kbd{C-x b}.}
73804d4b 1175
87b2d5ff
RS
1176(setq meta-prefix-char 27) ; @r{Avoid confusion!}
1177 @result{} 27 ; @r{Restore the default value!}
1178@end group
1179@end smallexample
5f1f5955
GM
1180
1181This translation of one event into two happens only for characters, not
1182for other kinds of input events. Thus, @kbd{M-@key{F1}}, a function
1183key, is not converted into @kbd{@key{ESC} @key{F1}}.
73804d4b
RS
1184@end defvar
1185
87b2d5ff
RS
1186@node Changing Key Bindings
1187@section Changing Key Bindings
1188@cindex changing key bindings
1189@cindex rebinding
73804d4b 1190
87b2d5ff
RS
1191 The way to rebind a key is to change its entry in a keymap. If you
1192change a binding in the global keymap, the change is effective in all
1193buffers (though it has no direct effect in buffers that shadow the
1194global binding with a local one). If you change the current buffer's
1195local map, that usually affects all buffers using the same major mode.
1196The @code{global-set-key} and @code{local-set-key} functions are
1197convenient interfaces for these operations (@pxref{Key Binding
1198Commands}). You can also use @code{define-key}, a more general
1199function; then you must specify explicitly the map to change.
73804d4b 1200
fc0804c4
RS
1201 When choosing the key sequences for Lisp programs to rebind, please
1202follow the Emacs conventions for use of various keys (@pxref{Key
1203Binding Conventions}).
1204
87b2d5ff
RS
1205@cindex meta character key constants
1206@cindex control character key constants
1207 In writing the key sequence to rebind, it is good to use the special
1208escape sequences for control and meta characters (@pxref{String Type}).
1209The syntax @samp{\C-} means that the following character is a control
1210character and @samp{\M-} means that the following character is a meta
1211character. Thus, the string @code{"\M-x"} is read as containing a
1212single @kbd{M-x}, @code{"\C-f"} is read as containing a single
1213@kbd{C-f}, and @code{"\M-\C-x"} and @code{"\C-\M-x"} are both read as
1214containing a single @kbd{C-M-x}. You can also use this escape syntax in
1215vectors, as well as others that aren't allowed in strings; one example
1216is @samp{[?\C-\H-x home]}. @xref{Character Type}.
73804d4b 1217
22697dac
KH
1218 The key definition and lookup functions accept an alternate syntax for
1219event types in a key sequence that is a vector: you can use a list
1220containing modifier names plus one base event (a character or function
1221key name). For example, @code{(control ?a)} is equivalent to
1222@code{?\C-a} and @code{(hyper control left)} is equivalent to
969fe9b5
RS
1223@code{C-H-left}. One advantage of such lists is that the precise
1224numeric codes for the modifier bits don't appear in compiled files.
bfe721d1 1225
fc0804c4
RS
1226 The functions below signal an error if @var{keymap} is not a keymap,
1227or if @var{key} is not a string or vector representing a key sequence.
1228You can use event types (symbols) as shorthand for events that are
1229lists. The @code{kbd} macro (@pxref{Key Sequences}) is a convenient
1230way to specify the key sequence.
73804d4b 1231
87b2d5ff
RS
1232@defun define-key keymap key binding
1233This function sets the binding for @var{key} in @var{keymap}. (If
1234@var{key} is more than one event long, the change is actually made
1235in another keymap reached from @var{keymap}.) The argument
1236@var{binding} can be any Lisp object, but only certain types are
1237meaningful. (For a list of meaningful types, see @ref{Key Lookup}.)
1238The value returned by @code{define-key} is @var{binding}.
73804d4b 1239
48bf63e3
RS
1240If @var{key} is @code{[t]}, this sets the default binding in
1241@var{keymap}. When an event has no binding of its own, the Emacs
1242command loop uses the keymap's default binding, if there is one.
1243
87b2d5ff
RS
1244@cindex invalid prefix key error
1245@cindex key sequence error
969fe9b5
RS
1246Every prefix of @var{key} must be a prefix key (i.e., bound to a keymap)
1247or undefined; otherwise an error is signaled. If some prefix of
1248@var{key} is undefined, then @code{define-key} defines it as a prefix
1249key so that the rest of @var{key} can be defined as specified.
f9f59935
RS
1250
1251If there was previously no binding for @var{key} in @var{keymap}, the
1252new binding is added at the beginning of @var{keymap}. The order of
48bf63e3
RS
1253bindings in a keymap makes no difference for keyboard input, but it
1254does matter for menu keymaps (@pxref{Menu Keymaps}).
87b2d5ff 1255@end defun
73804d4b 1256
87b2d5ff
RS
1257 Here is an example that creates a sparse keymap and makes a number of
1258bindings in it:
73804d4b 1259
87b2d5ff 1260@smallexample
73804d4b 1261@group
87b2d5ff
RS
1262(setq map (make-sparse-keymap))
1263 @result{} (keymap)
73804d4b 1264@end group
73804d4b 1265@group
87b2d5ff
RS
1266(define-key map "\C-f" 'forward-char)
1267 @result{} forward-char
73804d4b
RS
1268@end group
1269@group
87b2d5ff
RS
1270map
1271 @result{} (keymap (6 . forward-char))
73804d4b 1272@end group
73804d4b 1273
73804d4b 1274@group
87b2d5ff 1275;; @r{Build sparse submap for @kbd{C-x} and bind @kbd{f} in that.}
402fe423 1276(define-key map (kbd "C-x f") 'forward-word)
73804d4b
RS
1277 @result{} forward-word
1278@end group
1279@group
87b2d5ff 1280map
177c0ea7 1281@result{} (keymap
87b2d5ff
RS
1282 (24 keymap ; @kbd{C-x}
1283 (102 . forward-word)) ; @kbd{f}
1284 (6 . forward-char)) ; @kbd{C-f}
73804d4b 1285@end group
73804d4b 1286
87b2d5ff
RS
1287@group
1288;; @r{Bind @kbd{C-p} to the @code{ctl-x-map}.}
402fe423 1289(define-key map (kbd "C-p") ctl-x-map)
87b2d5ff 1290;; @code{ctl-x-map}
177c0ea7 1291@result{} [nil @dots{} find-file @dots{} backward-kill-sentence]
87b2d5ff 1292@end group
73804d4b 1293
73804d4b 1294@group
87b2d5ff 1295;; @r{Bind @kbd{C-f} to @code{foo} in the @code{ctl-x-map}.}
402fe423 1296(define-key map (kbd "C-p C-f") 'foo)
87b2d5ff 1297@result{} 'foo
73804d4b 1298@end group
87b2d5ff
RS
1299@group
1300map
1301@result{} (keymap ; @r{Note @code{foo} in @code{ctl-x-map}.}
1302 (16 keymap [nil @dots{} foo @dots{} backward-kill-sentence])
177c0ea7 1303 (24 keymap
87b2d5ff
RS
1304 (102 . forward-word))
1305 (6 . forward-char))
1306@end group
1307@end smallexample
73804d4b 1308
87b2d5ff
RS
1309@noindent
1310Note that storing a new binding for @kbd{C-p C-f} actually works by
1311changing an entry in @code{ctl-x-map}, and this has the effect of
1312changing the bindings of both @kbd{C-p C-f} and @kbd{C-x C-f} in the
1313default global map.
73804d4b 1314
229644e7 1315 The function @code{substitute-key-definition} scans a keymap for
db8af011 1316keys that have a certain binding and rebinds them with a different
91055930
RS
1317binding. Another feature which is cleaner and can often produce the
1318same results to remap one command into another (@pxref{Remapping
1319Commands}).
229644e7 1320
87b2d5ff
RS
1321@defun substitute-key-definition olddef newdef keymap &optional oldmap
1322@cindex replace bindings
1323This function replaces @var{olddef} with @var{newdef} for any keys in
1324@var{keymap} that were bound to @var{olddef}. In other words,
1325@var{olddef} is replaced with @var{newdef} wherever it appears. The
1326function returns @code{nil}.
73804d4b 1327
87b2d5ff
RS
1328For example, this redefines @kbd{C-x C-f}, if you do it in an Emacs with
1329standard bindings:
73804d4b 1330
87b2d5ff
RS
1331@smallexample
1332@group
177c0ea7 1333(substitute-key-definition
87b2d5ff
RS
1334 'find-file 'find-file-read-only (current-global-map))
1335@end group
1336@end smallexample
73804d4b
RS
1337
1338@c Emacs 19 feature
a0a1df48
GM
1339If @var{oldmap} is non-@code{nil}, that changes the behavior of
1340@code{substitute-key-definition}: the bindings in @var{oldmap} determine
1341which keys to rebind. The rebindings still happen in @var{keymap}, not
1342in @var{oldmap}. Thus, you can change one map under the control of the
87b2d5ff 1343bindings in another. For example,
73804d4b 1344
87b2d5ff
RS
1345@smallexample
1346(substitute-key-definition
1347 'delete-backward-char 'my-funny-delete
1348 my-map global-map)
1349@end smallexample
73804d4b 1350
87b2d5ff
RS
1351@noindent
1352puts the special deletion command in @code{my-map} for whichever keys
1353are globally bound to the standard deletion command.
73804d4b 1354
87b2d5ff 1355Here is an example showing a keymap before and after substitution:
73804d4b
RS
1356
1357@smallexample
1358@group
177c0ea7
JB
1359(setq map '(keymap
1360 (?1 . olddef-1)
1361 (?2 . olddef-2)
73804d4b
RS
1362 (?3 . olddef-1)))
1363@result{} (keymap (49 . olddef-1) (50 . olddef-2) (51 . olddef-1))
1364@end group
1365
1366@group
1367(substitute-key-definition 'olddef-1 'newdef map)
1368@result{} nil
1369@end group
1370@group
1371map
1372@result{} (keymap (49 . newdef) (50 . olddef-2) (51 . newdef))
1373@end group
1374@end smallexample
1375@end defun
1376
1377@defun suppress-keymap keymap &optional nodigits
1378@cindex @code{self-insert-command} override
1379This function changes the contents of the full keymap @var{keymap} by
cd7e5dd6
LT
1380remapping @code{self-insert-command} to the command @code{undefined}
1381(@pxref{Remapping Commands}). This has the effect of undefining all
1382printing characters, thus making ordinary insertion of text impossible.
1383@code{suppress-keymap} returns @code{nil}.
73804d4b
RS
1384
1385If @var{nodigits} is @code{nil}, then @code{suppress-keymap} defines
1386digits to run @code{digit-argument}, and @kbd{-} to run
1387@code{negative-argument}. Otherwise it makes them undefined like the
1388rest of the printing characters.
1389
177c0ea7
JB
1390@cindex yank suppression
1391@cindex @code{quoted-insert} suppression
73804d4b
RS
1392The @code{suppress-keymap} function does not make it impossible to
1393modify a buffer, as it does not suppress commands such as @code{yank}
1394and @code{quoted-insert}. To prevent any modification of a buffer, make
1395it read-only (@pxref{Read Only Buffers}).
1396
1397Since this function modifies @var{keymap}, you would normally use it
1398on a newly created keymap. Operating on an existing keymap
1399that is used for some other purpose is likely to cause trouble; for
1400example, suppressing @code{global-map} would make it impossible to use
1401most of Emacs.
1402
1403Most often, @code{suppress-keymap} is used to initialize local
1404keymaps of modes such as Rmail and Dired where insertion of text is not
1405desirable and the buffer is read-only. Here is an example taken from
1406the file @file{emacs/lisp/dired.el}, showing how the local keymap for
1407Dired mode is set up:
1408
1409@smallexample
1410@group
1911e6e5
RS
1411(setq dired-mode-map (make-keymap))
1412(suppress-keymap dired-mode-map)
1413(define-key dired-mode-map "r" 'dired-rename-file)
1414(define-key dired-mode-map "\C-d" 'dired-flag-file-deleted)
1415(define-key dired-mode-map "d" 'dired-flag-file-deleted)
1416(define-key dired-mode-map "v" 'dired-view-file)
1417(define-key dired-mode-map "e" 'dired-find-file)
1418(define-key dired-mode-map "f" 'dired-find-file)
1419@dots{}
73804d4b
RS
1420@end group
1421@end smallexample
1422@end defun
1423
229644e7
RS
1424@node Remapping Commands
1425@section Remapping Commands
1426@cindex remapping commands
1427
1428 A special kind of key binding, using a special ``key sequence''
1429which includes a command name, has the effect of @dfn{remapping} that
1430command into another. Here's how it works. You make a key binding
b0110b4a 1431for a key sequence that starts with the dummy event @code{remap},
229644e7
RS
1432followed by the command name you want to remap. Specify the remapped
1433definition as the definition in this binding. The remapped definition
1434is usually a command name, but it can be any valid definition for
1435a key binding.
1436
1437 Here's an example. Suppose that My mode uses special commands
1438@code{my-kill-line} and @code{my-kill-word}, which should be invoked
1439instead of @code{kill-line} and @code{kill-word}. It can establish
1440this by making these two command-remapping bindings in its keymap:
1441
342fd6cd 1442@smallexample
229644e7
RS
1443(define-key my-mode-map [remap kill-line] 'my-kill-line)
1444(define-key my-mode-map [remap kill-word] 'my-kill-word)
342fd6cd 1445@end smallexample
229644e7
RS
1446
1447Whenever @code{my-mode-map} is an active keymap, if the user types
1448@kbd{C-k}, Emacs will find the standard global binding of
1449@code{kill-line} (assuming nobody has changed it). But
db8af011 1450@code{my-mode-map} remaps @code{kill-line} to @code{my-kill-line},
229644e7
RS
1451so instead of running @code{kill-line}, Emacs runs
1452@code{my-kill-line}.
1453
1454Remapping only works through a single level. In other words,
1455
342fd6cd 1456@smallexample
229644e7
RS
1457(define-key my-mode-map [remap kill-line] 'my-kill-line)
1458(define-key my-mode-map [remap my-kill-line] 'my-other-kill-line)
342fd6cd 1459@end smallexample
229644e7
RS
1460
1461@noindent
1462does not have the effect of remapping @code{kill-line} into
db8af011 1463@code{my-other-kill-line}. If an ordinary key binding specifies
229644e7
RS
1464@code{kill-line}, this keymap will remap it to @code{my-kill-line};
1465if an ordinary binding specifies @code{my-kill-line}, this keymap will
1466remap it to @code{my-other-kill-line}.
1467
1468@defun command-remapping command
db8af011
LT
1469This function returns the remapping for @var{command} (a symbol),
1470given the current active keymaps. If @var{command} is not remapped
1471(which is the usual situation), or not a symbol, the function returns
1472@code{nil}.
229644e7
RS
1473@end defun
1474
73804d4b
RS
1475@node Key Binding Commands
1476@section Commands for Binding Keys
1477
1478 This section describes some convenient interactive interfaces for
1479changing key bindings. They work by calling @code{define-key}.
1480
a40d4712
PR
1481 People often use @code{global-set-key} in their init files
1482(@pxref{Init File}) for simple customization. For example,
87b2d5ff
RS
1483
1484@smallexample
402fe423 1485(global-set-key (kbd "C-x C-\\") 'next-line)
87b2d5ff
RS
1486@end smallexample
1487
1488@noindent
1489or
1490
1491@smallexample
1492(global-set-key [?\C-x ?\C-\\] 'next-line)
1493@end smallexample
1494
bfe721d1
KH
1495@noindent
1496or
1497
1498@smallexample
1499(global-set-key [(control ?x) (control ?\\)] 'next-line)
1500@end smallexample
1501
87b2d5ff
RS
1502@noindent
1503redefines @kbd{C-x C-\} to move down a line.
1504
1505@smallexample
1506(global-set-key [M-mouse-1] 'mouse-set-point)
1507@end smallexample
1508
1509@noindent
8a36c244 1510redefines the first (leftmost) mouse button, entered with the Meta key, to
87b2d5ff
RS
1511set point where you click.
1512
ad800164
EZ
1513@cindex non-@acronym{ASCII} text in keybindings
1514 Be careful when using non-@acronym{ASCII} text characters in Lisp
8241495d
RS
1515specifications of keys to bind. If these are read as multibyte text, as
1516they usually will be in a Lisp file (@pxref{Loading Non-ASCII}), you
1517must type the keys as multibyte too. For instance, if you use this:
1518
1519@smallexample
1520(global-set-key "@"o" 'my-function) ; bind o-umlaut
1521@end smallexample
1522
1523@noindent
1524or
1525
1526@smallexample
1527(global-set-key ?@"o 'my-function) ; bind o-umlaut
1528@end smallexample
1529
1530@noindent
1531and your language environment is multibyte Latin-1, these commands
1532actually bind the multibyte character with code 2294, not the unibyte
1533Latin-1 character with code 246 (@kbd{M-v}). In order to use this
1534binding, you need to enter the multibyte Latin-1 character as keyboard
1535input. One way to do this is by using an appropriate input method
db8af011 1536(@pxref{Input Methods, , Input Methods, emacs, The GNU Emacs Manual}).
8241495d
RS
1537
1538 If you want to use a unibyte character in the key binding, you can
1539construct the key sequence string using @code{multibyte-char-to-unibyte}
1540or @code{string-make-unibyte} (@pxref{Converting Representations}).
1541
b68f60d7 1542@deffn Command global-set-key key binding
87b2d5ff 1543This function sets the binding of @var{key} in the current global map
b68f60d7 1544to @var{binding}.
73804d4b
RS
1545
1546@smallexample
1547@group
b68f60d7 1548(global-set-key @var{key} @var{binding})
73804d4b 1549@equiv{}
b68f60d7 1550(define-key (current-global-map) @var{key} @var{binding})
73804d4b
RS
1551@end group
1552@end smallexample
1553@end deffn
1554
1555@deffn Command global-unset-key key
1556@cindex unbinding keys
87b2d5ff 1557This function removes the binding of @var{key} from the current
73804d4b
RS
1558global map.
1559
87b2d5ff
RS
1560One use of this function is in preparation for defining a longer key
1561that uses @var{key} as a prefix---which would not be allowed if
1562@var{key} has a non-prefix binding. For example:
1563
1564@smallexample
1565@group
1566(global-unset-key "\C-l")
1567 @result{} nil
1568@end group
1569@group
1570(global-set-key "\C-l\C-l" 'redraw-display)
1571 @result{} nil
1572@end group
1573@end smallexample
1574
1575This function is implemented simply using @code{define-key}:
1576
1577@smallexample
1578@group
1579(global-unset-key @var{key})
1580@equiv{}
1581(define-key (current-global-map) @var{key} nil)
1582@end group
1583@end smallexample
1584@end deffn
1585
b68f60d7 1586@deffn Command local-set-key key binding
87b2d5ff 1587This function sets the binding of @var{key} in the current local
b68f60d7 1588keymap to @var{binding}.
87b2d5ff
RS
1589
1590@smallexample
1591@group
b68f60d7 1592(local-set-key @var{key} @var{binding})
87b2d5ff 1593@equiv{}
b68f60d7 1594(define-key (current-local-map) @var{key} @var{binding})
87b2d5ff
RS
1595@end group
1596@end smallexample
1597@end deffn
1598
1599@deffn Command local-unset-key key
1600This function removes the binding of @var{key} from the current
1601local map.
1602
1603@smallexample
1604@group
1605(local-unset-key @var{key})
1606@equiv{}
1607(define-key (current-local-map) @var{key} nil)
1608@end group
1609@end smallexample
1610@end deffn
1611
1612@node Scanning Keymaps
1613@section Scanning Keymaps
1614
1615 This section describes functions used to scan all the current keymaps
1616for the sake of printing help information.
1617
1618@defun accessible-keymaps keymap &optional prefix
f9f59935
RS
1619This function returns a list of all the keymaps that can be reached (via
1620zero or more prefix keys) from @var{keymap}. The value is an
1621association list with elements of the form @code{(@var{key} .@:
1622@var{map})}, where @var{key} is a prefix key whose definition in
1623@var{keymap} is @var{map}.
87b2d5ff
RS
1624
1625The elements of the alist are ordered so that the @var{key} increases
db8af011 1626in length. The first element is always @code{([] .@: @var{keymap})},
87b2d5ff
RS
1627because the specified keymap is accessible from itself with a prefix of
1628no events.
1629
1630If @var{prefix} is given, it should be a prefix key sequence; then
1631@code{accessible-keymaps} includes only the submaps whose prefixes start
1632with @var{prefix}. These elements look just as they do in the value of
1633@code{(accessible-keymaps)}; the only difference is that some elements
1634are omitted.
1635
1636In the example below, the returned alist indicates that the key
1637@key{ESC}, which is displayed as @samp{^[}, is a prefix key whose
1638definition is the sparse keymap @code{(keymap (83 .@: center-paragraph)
1639(115 .@: foo))}.
1640
1641@smallexample
1642@group
1643(accessible-keymaps (current-local-map))
db8af011 1644@result{}(([] keymap
87b2d5ff
RS
1645 (27 keymap ; @r{Note this keymap for @key{ESC} is repeated below.}
1646 (83 . center-paragraph)
1647 (115 . center-line))
1648 (9 . tab-to-tab-stop))
1649@end group
1650
1651@group
177c0ea7
JB
1652 ("^[" keymap
1653 (83 . center-paragraph)
87b2d5ff
RS
1654 (115 . foo)))
1655@end group
1656@end smallexample
1657
1658In the following example, @kbd{C-h} is a prefix key that uses a sparse
1659keymap starting with @code{(keymap (118 . describe-variable)@dots{})}.
1660Another prefix, @kbd{C-x 4}, uses a keymap which is also the value of
1661the variable @code{ctl-x-4-map}. The event @code{mode-line} is one of
1662several dummy events used as prefixes for mouse actions in special parts
1663of a window.
1664
1665@smallexample
1666@group
1667(accessible-keymaps (current-global-map))
db8af011 1668@result{} (([] keymap [set-mark-command beginning-of-line @dots{}
87b2d5ff
RS
1669 delete-backward-char])
1670@end group
1671@group
1672 ("^H" keymap (118 . describe-variable) @dots{}
1673 (8 . help-for-help))
1674@end group
1675@group
1676 ("^X" keymap [x-flush-mouse-queue @dots{}
1677 backward-kill-sentence])
1678@end group
1679@group
1680 ("^[" keymap [mark-sexp backward-sexp @dots{}
1681 backward-kill-word])
1682@end group
1683 ("^X4" keymap (15 . display-buffer) @dots{})
1684@group
1685 ([mode-line] keymap
1686 (S-mouse-2 . mouse-split-window-horizontally) @dots{}))
1687@end group
1688@end smallexample
1689
1690@noindent
969fe9b5 1691These are not all the keymaps you would see in actuality.
87b2d5ff
RS
1692@end defun
1693
0f201864
RS
1694@defun map-keymap function keymap
1695The function @code{map-keymap} calls @var{function} once
1696for each binding in @var{keymap}. It passes two arguments,
1697the event type and the value of the binding. If @var{keymap}
1698has a parent, the parent's bindings are included as well.
db8af011
LT
1699This works recursively: if the parent has itself a parent, then the
1700grandparent's bindings are also included and so on.
0f201864
RS
1701
1702This function is the cleanest way to examine all the bindings
1703in a keymap.
1704@end defun
1705
229644e7 1706@defun where-is-internal command &optional keymap firstonly noindirect no-remap
f9f59935
RS
1707This function is a subroutine used by the @code{where-is} command
1708(@pxref{Help, , Help, emacs,The GNU Emacs Manual}). It returns a list
db8af011 1709of all key sequences (of any length) that are bound to @var{command} in a
f9f59935 1710set of keymaps.
87b2d5ff
RS
1711
1712The argument @var{command} can be any object; it is compared with all
1713keymap entries using @code{eq}.
1714
1715If @var{keymap} is @code{nil}, then the maps used are the current active
1716keymaps, disregarding @code{overriding-local-map} (that is, pretending
db8af011 1717its value is @code{nil}). If @var{keymap} is a keymap, then the
87d6dc14
EZ
1718maps searched are @var{keymap} and the global keymap. If @var{keymap}
1719is a list of keymaps, only those keymaps are searched.
87b2d5ff
RS
1720
1721Usually it's best to use @code{overriding-local-map} as the expression
1722for @var{keymap}. Then @code{where-is-internal} searches precisely the
1723keymaps that are active. To search only the global map, pass
1724@code{(keymap)} (an empty keymap) as @var{keymap}.
1725
1726If @var{firstonly} is @code{non-ascii}, then the value is a single
db8af011 1727vector representing the first key sequence found, rather than a list of
87b2d5ff
RS
1728all possible key sequences. If @var{firstonly} is @code{t}, then the
1729value is the first key sequence, except that key sequences consisting
ad800164 1730entirely of @acronym{ASCII} characters (or meta variants of @acronym{ASCII}
db8af011
LT
1731characters) are preferred to all other key sequences and that the
1732return value can never be a menu binding.
87b2d5ff
RS
1733
1734If @var{noindirect} is non-@code{nil}, @code{where-is-internal} doesn't
1735follow indirect keymap bindings. This makes it possible to search for
1736an indirect definition itself.
1737
229644e7
RS
1738When command remapping is in effect (@pxref{Remapping Commands}),
1739@code{where-is-internal} figures out when a command will be run due to
1740remapping and reports keys accordingly. It also returns @code{nil} if
1741@var{command} won't really be run because it has been remapped to some
1742other command. However, if @var{no-remap} is non-@code{nil}.
1743@code{where-is-internal} ignores remappings.
1744
87b2d5ff
RS
1745@smallexample
1746@group
1747(where-is-internal 'describe-function)
1748 @result{} ("\^hf" "\^hd")
1749@end group
1750@end smallexample
1751@end defun
1752
db8af011 1753@deffn Command describe-bindings &optional prefix buffer-or-name
969fe9b5
RS
1754This function creates a listing of all current key bindings, and
1755displays it in a buffer named @samp{*Help*}. The text is grouped by
1756modes---minor modes first, then the major mode, then global bindings.
87b2d5ff
RS
1757
1758If @var{prefix} is non-@code{nil}, it should be a prefix key; then the
1759listing includes only keys that start with @var{prefix}.
1760
1761The listing describes meta characters as @key{ESC} followed by the
1762corresponding non-meta character.
1763
ad800164 1764When several characters with consecutive @acronym{ASCII} codes have the
87b2d5ff
RS
1765same definition, they are shown together, as
1766@samp{@var{firstchar}..@var{lastchar}}. In this instance, you need to
ad800164 1767know the @acronym{ASCII} codes to understand which characters this means.
87b2d5ff 1768For example, in the default global map, the characters @samp{@key{SPC}
ad800164
EZ
1769..@: ~} are described by a single line. @key{SPC} is @acronym{ASCII} 32,
1770@kbd{~} is @acronym{ASCII} 126, and the characters between them include all
87b2d5ff
RS
1771the normal printing characters, (e.g., letters, digits, punctuation,
1772etc.@:); all these characters are bound to @code{self-insert-command}.
db8af011
LT
1773
1774If @var{buffer-or-name} is non-@code{nil}, it should be a buffer or a
1775buffer name. Then @code{describe-bindings} lists that buffer's bindings,
1776instead of the current buffer's.
87b2d5ff
RS
1777@end deffn
1778
1779@node Menu Keymaps
1780@section Menu Keymaps
1781@cindex menu keymaps
1782
1783@c Emacs 19 feature
1784A keymap can define a menu as well as bindings for keyboard keys and
1785mouse button. Menus are usually actuated with the mouse, but they can
1786work with the keyboard also.
1787
1788@menu
1789* Defining Menus:: How to make a keymap that defines a menu.
1790* Mouse Menus:: How users actuate the menu with the mouse.
1791* Keyboard Menus:: How they actuate it with the keyboard.
1792* Menu Example:: Making a simple menu.
1793* Menu Bar:: How to customize the menu bar.
8241495d 1794* Tool Bar:: A tool bar is a row of images.
87b2d5ff
RS
1795* Modifying Menus:: How to add new items to a menu.
1796@end menu
1797
1798@node Defining Menus
1799@subsection Defining Menus
1800@cindex defining menus
1801@cindex menu prompt string
1802@cindex prompt string (of menu)
1803
1804A keymap is suitable for menu use if it has an @dfn{overall prompt
1805string}, which is a string that appears as an element of the keymap.
1806(@xref{Format of Keymaps}.) The string should describe the purpose of
e465fdc2 1807the menu's commands. Emacs displays the overall prompt string as the
b08d86c6
DL
1808menu title in some cases, depending on the toolkit (if any) used for
1809displaying menus.@footnote{It is required for menus which do not use a
1810toolkit, e.g.@: under MS-DOS.} Keyboard menus also display the overall
1811prompt string.
e465fdc2
GM
1812
1813The easiest way to construct a keymap with a prompt string is to specify
b08d86c6 1814the string as an argument when you call @code{make-keymap},
db8af011
LT
1815@code{make-sparse-keymap} (@pxref{Creating Keymaps}), or
1816@code{define-prefix-command} (@pxref{Definition of define-prefix-command}).
1817
87b2d5ff 1818
0f201864
RS
1819@defun keymap-prompt keymap
1820This function returns the overall prompt string of @var{keymap},
1821or @code{nil} if it has none.
1822@end defun
1823
aae60c21
RS
1824The order of items in the menu is the same as the order of bindings in
1825the keymap. Since @code{define-key} puts new bindings at the front, you
1826should define the menu items starting at the bottom of the menu and
1827moving to the top, if you care about the order. When you add an item to
1828an existing menu, you can specify its position in the menu using
1829@code{define-key-after} (@pxref{Modifying Menus}).
1830
969fe9b5 1831@menu
a9f0a989
RS
1832* Simple Menu Items:: A simple kind of menu key binding,
1833 limited in capabilities.
a9f0a989
RS
1834* Extended Menu Items:: More powerful menu item definitions
1835 let you specify keywords to enable
1836 various features.
8241495d
RS
1837* Menu Separators:: Drawing a horizontal line through a menu.
1838* Alias Menu Items:: Using command aliases in menu items.
969fe9b5
RS
1839@end menu
1840
1841@node Simple Menu Items
1842@subsubsection Simple Menu Items
1843
1844 The simpler and older way to define a menu keymap binding
1845looks like this:
87b2d5ff
RS
1846
1847@example
969fe9b5 1848(@var{item-string} . @var{real-binding})
87b2d5ff
RS
1849@end example
1850
a9f0a989 1851@noindent
969fe9b5
RS
1852The @sc{car}, @var{item-string}, is the string to be displayed in the
1853menu. It should be short---preferably one to three words. It should
79dc1dfc 1854describe the action of the command it corresponds to. Note that it is
ad800164 1855not generally possible to display non-@acronym{ASCII} text in menus. It will
79dc1dfc 1856work for keyboard menus and will work to a large extent when Emacs is
8a36c244 1857built with the Gtk+ toolkit.@footnote{In this case, the text is first
79dc1dfc
DL
1858encoded using the @code{utf-8} coding system and then rendered by the
1859toolkit as it sees fit.}
87b2d5ff 1860
87b2d5ff
RS
1861You can also supply a second string, called the help string, as follows:
1862
1863@example
b08d86c6 1864(@var{item-string} @var{help} . @var{real-binding})
87b2d5ff
RS
1865@end example
1866
b08d86c6
DL
1867@var{help} specifies a ``help-echo'' string to display while the mouse
1868is on that item in the same way as @code{help-echo} text properties
1869(@pxref{Help display}).
87b2d5ff 1870
969fe9b5 1871As far as @code{define-key} is concerned, @var{item-string} and
0521d6f5
RS
1872@var{help-string} are part of the event's binding. However,
1873@code{lookup-key} returns just @var{real-binding}, and only
1874@var{real-binding} is used for executing the key.
1875
969fe9b5
RS
1876If @var{real-binding} is @code{nil}, then @var{item-string} appears in
1877the menu but cannot be selected.
87b2d5ff
RS
1878
1879If @var{real-binding} is a symbol and has a non-@code{nil}
1880@code{menu-enable} property, that property is an expression that
1881controls whether the menu item is enabled. Every time the keymap is
1882used to display a menu, Emacs evaluates the expression, and it enables
1883the menu item only if the expression's value is non-@code{nil}. When a
1884menu item is disabled, it is displayed in a ``fuzzy'' fashion, and
969fe9b5 1885cannot be selected.
87b2d5ff 1886
bfe721d1
KH
1887The menu bar does not recalculate which items are enabled every time you
1888look at a menu. This is because the X toolkit requires the whole tree
1889of menus in advance. To force recalculation of the menu bar, call
1890@code{force-mode-line-update} (@pxref{Mode Line Format}).
1891
0521d6f5
RS
1892You've probably noticed that menu items show the equivalent keyboard key
1893sequence (if any) to invoke the same command. To save time on
1894recalculation, menu display caches this information in a sublist in the
1895binding, like this:
1896
1897@c This line is not too long--rms.
1898@example
969fe9b5 1899(@var{item-string} @r{[}@var{help-string}@r{]} (@var{key-binding-data}) . @var{real-binding})
0521d6f5
RS
1900@end example
1901
969fe9b5 1902@noindent
0521d6f5 1903Don't put these sublists in the menu item yourself; menu display
969fe9b5
RS
1904calculates them automatically. Don't mention keyboard equivalents in
1905the item strings themselves, since that is redundant.
0521d6f5 1906
969fe9b5
RS
1907@node Extended Menu Items
1908@subsubsection Extended Menu Items
a9f0a989 1909@kindex menu-item
969fe9b5
RS
1910
1911 An extended-format menu item is a more flexible and also cleaner
1912alternative to the simple format. It consists of a list that starts
1913with the symbol @code{menu-item}. To define a non-selectable string,
1914the item looks like this:
1915
1916@example
1917(menu-item @var{item-name})
1918@end example
1919
1920@noindent
8241495d
RS
1921A string starting with two or more dashes specifies a separator line;
1922see @ref{Menu Separators}.
969fe9b5
RS
1923
1924 To define a real menu item which can be selected, the extended format
1925item looks like this:
1926
1927@example
1928(menu-item @var{item-name} @var{real-binding}
1929 . @var{item-property-list})
1930@end example
1931
1932@noindent
1933Here, @var{item-name} is an expression which evaluates to the menu item
1934string. Thus, the string need not be a constant. The third element,
1935@var{real-binding}, is the command to execute. The tail of the list,
1936@var{item-property-list}, has the form of a property list which contains
1937other information. Here is a table of the properties that are supported:
1938
1939@table @code
8241495d 1940@item :enable @var{form}
969fe9b5 1941The result of evaluating @var{form} determines whether the item is
8241495d
RS
1942enabled (non-@code{nil} means yes). If the item is not enabled,
1943you can't really click on it.
969fe9b5 1944
8241495d 1945@item :visible @var{form}
969fe9b5
RS
1946The result of evaluating @var{form} determines whether the item should
1947actually appear in the menu (non-@code{nil} means yes). If the item
1948does not appear, then the menu is displayed as if this item were
1949not defined at all.
1950
1951@item :help @var{help}
b08d86c6
DL
1952The value of this property, @var{help}, specifies a ``help-echo'' string
1953to display while the mouse is on that item. This is displayed in the
1954same way as @code{help-echo} text properties (@pxref{Help display}).
1955Note that this must be a constant string, unlike the @code{help-echo}
1956property for text and overlays.
969fe9b5
RS
1957
1958@item :button (@var{type} . @var{selected})
1959This property provides a way to define radio buttons and toggle buttons.
a40d4712 1960The @sc{car}, @var{type}, says which: it should be @code{:toggle} or
969fe9b5
RS
1961@code{:radio}. The @sc{cdr}, @var{selected}, should be a form; the
1962result of evaluating it says whether this button is currently selected.
1963
a9f0a989
RS
1964A @dfn{toggle} is a menu item which is labeled as either ``on'' or ``off''
1965according to the value of @var{selected}. The command itself should
1966toggle @var{selected}, setting it to @code{t} if it is @code{nil},
1967and to @code{nil} if it is @code{t}. Here is how the menu item
1968to toggle the @code{debug-on-error} flag is defined:
1969
1970@example
1971(menu-item "Debug on Error" toggle-debug-on-error
1972 :button (:toggle
1973 . (and (boundp 'debug-on-error)
08f0f5e9 1974 debug-on-error)))
a9f0a989
RS
1975@end example
1976
1977@noindent
1978This works because @code{toggle-debug-on-error} is defined as a command
1979which toggles the variable @code{debug-on-error}.
1980
1981@dfn{Radio buttons} are a group of menu items, in which at any time one
1982and only one is ``selected.'' There should be a variable whose value
1983says which one is selected at any time. The @var{selected} form for
1984each radio button in the group should check whether the variable has the
1985right value for selecting that button. Clicking on the button should
1986set the variable so that the button you clicked on becomes selected.
1987
1988@item :key-sequence @var{key-sequence}
1989This property specifies which key sequence is likely to be bound to the
1990same command invoked by this menu item. If you specify the right key
1991sequence, that makes preparing the menu for display run much faster.
1992
1993If you specify the wrong key sequence, it has no effect; before Emacs
1994displays @var{key-sequence} in the menu, it verifies that
1995@var{key-sequence} is really equivalent to this menu item.
1996
1997@item :key-sequence nil
1998This property indicates that there is normally no key binding which is
1999equivalent to this menu item. Using this property saves time in
2000preparing the menu for display, because Emacs does not need to search
2001the keymaps for a keyboard equivalent for this menu item.
2002
2003However, if the user has rebound this item's definition to a key
2004sequence, Emacs ignores the @code{:keys} property and finds the keyboard
2005equivalent anyway.
2006
2007@item :keys @var{string}
2008This property specifies that @var{string} is the string to display
2009as the keyboard equivalent for this menu item. You can use
2010the @samp{\\[...]} documentation construct in @var{string}.
2011
969fe9b5
RS
2012@item :filter @var{filter-fn}
2013This property provides a way to compute the menu item dynamically.
2014The property value @var{filter-fn} should be a function of one argument;
2015when it is called, its argument will be @var{real-binding}. The
2016function should return the binding to use instead.
bf58181a
RS
2017
2018Emacs can call this function at any time that it does redisplay or
2019operates on menu data structures, so you should write it so it can
2020safely be called at any time.
969fe9b5
RS
2021@end table
2022
8241495d
RS
2023@node Menu Separators
2024@subsubsection Menu Separators
2025@cindex menu separators
2026
2027 A menu separator is a kind of menu item that doesn't display any
4810d170 2028text---instead, it divides the menu into subparts with a horizontal line.
8241495d
RS
2029A separator looks like this in the menu keymap:
2030
2031@example
2032(menu-item @var{separator-type})
2033@end example
2034
2035@noindent
2036where @var{separator-type} is a string starting with two or more dashes.
2037
2038 In the simplest case, @var{separator-type} consists of only dashes.
2039That specifies the default kind of separator. (For compatibility,
2040@code{""} and @code{-} also count as separators.)
2041
35c14f98
RS
2042 Certain other values of @var{separator-type} specify a different
2043style of separator. Here is a table of them:
8241495d
RS
2044
2045@table @code
2046@item "--no-line"
2047@itemx "--space"
2048An extra vertical space, with no actual line.
2049
2050@item "--single-line"
2051A single line in the menu's foreground color.
2052
2053@item "--double-line"
2054A double line in the menu's foreground color.
2055
2056@item "--single-dashed-line"
2057A single dashed line in the menu's foreground color.
2058
2059@item "--double-dashed-line"
2060A double dashed line in the menu's foreground color.
2061
2062@item "--shadow-etched-in"
2063A single line with a 3D sunken appearance. This is the default,
2064used separators consisting of dashes only.
2065
2066@item "--shadow-etched-out"
2067A single line with a 3D raised appearance.
2068
2069@item "--shadow-etched-in-dash"
2070A single dashed line with a 3D sunken appearance.
2071
2072@item "--shadow-etched-out-dash"
2073A single dashed line with a 3D raised appearance.
2074
2075@item "--shadow-double-etched-in"
2076Two lines with a 3D sunken appearance.
2077
2078@item "--shadow-double-etched-out"
2079Two lines with a 3D raised appearance.
2080
2081@item "--shadow-double-etched-in-dash"
2082Two dashed lines with a 3D sunken appearance.
2083
2084@item "--shadow-double-etched-out-dash"
2085Two dashed lines with a 3D raised appearance.
2086@end table
2087
2088 You can also give these names in another style, adding a colon after
2089the double-dash and replacing each single dash with capitalization of
2090the following word. Thus, @code{"--:singleLine"}, is equivalent to
2091@code{"--single-line"}.
2092
2093 Some systems and display toolkits don't really handle all of these
2094separator types. If you use a type that isn't supported, the menu
2095displays a similar kind of separator that is supported.
2096
a9f0a989
RS
2097@node Alias Menu Items
2098@subsubsection Alias Menu Items
2099
2100 Sometimes it is useful to make menu items that use the ``same''
2101command but with different enable conditions. The best way to do this
2102in Emacs now is with extended menu items; before that feature existed,
2103it could be done by defining alias commands and using them in menu
2104items. Here's an example that makes two aliases for
2105@code{toggle-read-only} and gives them different enable conditions:
2106
2107@example
2108(defalias 'make-read-only 'toggle-read-only)
2109(put 'make-read-only 'menu-enable '(not buffer-read-only))
2110(defalias 'make-writable 'toggle-read-only)
2111(put 'make-writable 'menu-enable 'buffer-read-only)
2112@end example
2113
2114When using aliases in menus, often it is useful to display the
2115equivalent key bindings for the ``real'' command name, not the aliases
2116(which typically don't have any key bindings except for the menu
2117itself). To request this, give the alias symbol a non-@code{nil}
2118@code{menu-alias} property. Thus,
2119
2120@example
2121(put 'make-read-only 'menu-alias t)
2122(put 'make-writable 'menu-alias t)
2123@end example
2124
2125@noindent
2126causes menu items for @code{make-read-only} and @code{make-writable} to
2127show the keyboard bindings for @code{toggle-read-only}.
2128
87b2d5ff
RS
2129@node Mouse Menus
2130@subsection Menus and the Mouse
2131
969fe9b5
RS
2132 The usual way to make a menu keymap produce a menu is to make it the
2133definition of a prefix key. (A Lisp program can explicitly pop up a
2134menu and receive the user's choice---see @ref{Pop-Up Menus}.)
87b2d5ff 2135
969fe9b5 2136 If the prefix key ends with a mouse event, Emacs handles the menu keymap
87b2d5ff
RS
2137by popping up a visible menu, so that the user can select a choice with
2138the mouse. When the user clicks on a menu item, the event generated is
2139whatever character or symbol has the binding that brought about that
2140menu item. (A menu item may generate a series of events if the menu has
2141multiple levels or comes from the menu bar.)
2142
969fe9b5 2143 It's often best to use a button-down event to trigger the menu. Then
87b2d5ff
RS
2144the user can select a menu item by releasing the button.
2145
969fe9b5 2146 A single keymap can appear as multiple menu panes, if you explicitly
87b2d5ff
RS
2147arrange for this. The way to do this is to make a keymap for each pane,
2148then create a binding for each of those maps in the main keymap of the
2149menu. Give each of these bindings an item string that starts with
2150@samp{@@}. The rest of the item string becomes the name of the pane.
2151See the file @file{lisp/mouse.el} for an example of this. Any ordinary
2152bindings with @samp{@@}-less item strings are grouped into one pane,
2153which appears along with the other panes explicitly created for the
2154submaps.
2155
969fe9b5 2156 X toolkit menus don't have panes; instead, they can have submenus.
87b2d5ff
RS
2157Every nested keymap becomes a submenu, whether the item string starts
2158with @samp{@@} or not. In a toolkit version of Emacs, the only thing
2159special about @samp{@@} at the beginning of an item string is that the
2160@samp{@@} doesn't appear in the menu item.
2161
8a36c244
RS
2162 Multiple keymaps that define the same menu prefix key produce
2163separate panes or separate submenus.
87b2d5ff
RS
2164
2165@node Keyboard Menus
2166@subsection Menus and the Keyboard
2167
2168When a prefix key ending with a keyboard event (a character or function
2169key) has a definition that is a menu keymap, the user can use the
2170keyboard to choose a menu item.
2171
e465fdc2
GM
2172Emacs displays the menu's overall prompt string followed by the
2173alternatives (the item strings of the bindings) in the echo area. If
2174the bindings don't all fit at once, the user can type @key{SPC} to see
2175the next line of alternatives. Successive uses of @key{SPC} eventually
2176get to the end of the menu and then cycle around to the beginning. (The
2177variable @code{menu-prompt-more-char} specifies which character is used
2178for this; @key{SPC} is the default.)
87b2d5ff
RS
2179
2180When the user has found the desired alternative from the menu, he or she
2181should type the corresponding character---the one whose binding is that
2182alternative.
2183
bfe721d1 2184@ignore
87b2d5ff
RS
2185In a menu intended for keyboard use, each menu item must clearly
2186indicate what character to type. The best convention to use is to make
bfe721d1
KH
2187the character the first letter of the item string---that is something
2188users will understand without being told. We plan to change this; by
2189the time you read this manual, keyboard menus may explicitly name the
2190key for each alternative.
2191@end ignore
87b2d5ff
RS
2192
2193This way of using menus in an Emacs-like editor was inspired by the
2194Hierarkey system.
73804d4b 2195
87b2d5ff
RS
2196@defvar menu-prompt-more-char
2197This variable specifies the character to use to ask to see
2198the next line of a menu. Its initial value is 32, the code
2199for @key{SPC}.
2200@end defvar
73804d4b 2201
87b2d5ff
RS
2202@node Menu Example
2203@subsection Menu Example
f9f59935 2204@cindex menu definition example
73804d4b 2205
f9f59935 2206 Here is a complete example of defining a menu keymap. It is the
8a36c244
RS
2207definition of the @samp{Replace} submenu in the @samp{Edit} menu in
2208the menu bar, and it uses the extended menu item format
2209(@pxref{Extended Menu Items}). First we create the keymap, and give
2210it a name:
73804d4b 2211
8a36c244
RS
2212@smallexample
2213(defvar menu-bar-replace-menu (make-sparse-keymap "Replace"))
2214@end smallexample
73804d4b 2215
969fe9b5
RS
2216@noindent
2217Next we define the menu items:
73804d4b 2218
8a36c244
RS
2219@smallexample
2220(define-key menu-bar-replace-menu [tags-repl-continue]
2221 '(menu-item "Continue Replace" tags-loop-continue
2222 :help "Continue last tags replace operation"))
2223(define-key menu-bar-replace-menu [tags-repl]
2224 '(menu-item "Replace in tagged files" tags-query-replace
2225 :help "Interactively replace a regexp in all tagged files"))
2226(define-key menu-bar-replace-menu [separator-replace-tags]
2227 '(menu-item "--"))
2228;; @r{@dots{}}
2229@end smallexample
f9f59935
RS
2230
2231@noindent
2232Note the symbols which the bindings are ``made for''; these appear
2233inside square brackets, in the key sequence being defined. In some
2234cases, this symbol is the same as the command name; sometimes it is
827b7ee7 2235different. These symbols are treated as ``function keys,'' but they are
f9f59935
RS
2236not real function keys on the keyboard. They do not affect the
2237functioning of the menu itself, but they are ``echoed'' in the echo area
2238when the user selects from the menu, and they appear in the output of
2239@code{where-is} and @code{apropos}.
2240
db8af011
LT
2241 The menu in this example is intended for use with the mouse. If a
2242menu is intended for use with the keyboard, that is, if it is bound to
2243a key sequence ending with a keyboard event, then the menu items
2244should be bound to characters or ``real'' function keys, that can be
2245typed with the keyboard.
2246
f9f59935
RS
2247 The binding whose definition is @code{("--")} is a separator line.
2248Like a real menu item, the separator has a key symbol, in this case
8a36c244
RS
2249@code{separator-replace-tags}. If one menu has two separators, they
2250must have two different key symbols.
f9f59935
RS
2251
2252 Here is how we make this menu appear as an item in the parent menu:
2253
2254@example
8a36c244
RS
2255(define-key menu-bar-edit-menu [replace]
2256 (list 'menu-item "Replace" menu-bar-replace-menu))
f9f59935
RS
2257@end example
2258
2259@noindent
2260Note that this incorporates the submenu keymap, which is the value of
8a36c244
RS
2261the variable @code{menu-bar-replace-menu}, rather than the symbol
2262@code{menu-bar-replace-menu} itself. Using that symbol in the parent
2263menu item would be meaningless because @code{menu-bar-replace-menu} is
2264not a command.
f9f59935 2265
8a36c244 2266 If you wanted to attach the same replace menu to a mouse click, you
969fe9b5 2267can do it this way:
f9f59935
RS
2268
2269@example
a9f0a989 2270(define-key global-map [C-S-down-mouse-1]
8a36c244 2271 menu-bar-replace-menu)
f9f59935 2272@end example
73804d4b 2273
87b2d5ff
RS
2274@node Menu Bar
2275@subsection The Menu Bar
2276@cindex menu bar
73804d4b 2277
87b2d5ff
RS
2278 Most window systems allow each frame to have a @dfn{menu bar}---a
2279permanently displayed menu stretching horizontally across the top of the
2280frame. The items of the menu bar are the subcommands of the fake
8a36c244 2281``function key'' @code{menu-bar}, as defined in the active keymaps.
73804d4b 2282
87b2d5ff
RS
2283 To add an item to the menu bar, invent a fake ``function key'' of your
2284own (let's call it @var{key}), and make a binding for the key sequence
2285@code{[menu-bar @var{key}]}. Most often, the binding is a menu keymap,
2286so that pressing a button on the menu bar item leads to another menu.
73804d4b 2287
87b2d5ff
RS
2288 When more than one active keymap defines the same fake function key
2289for the menu bar, the item appears just once. If the user clicks on
969fe9b5 2290that menu bar item, it brings up a single, combined menu containing
87b2d5ff 2291all the subcommands of that item---the global subcommands, the local
969fe9b5 2292subcommands, and the minor mode subcommands.
73804d4b 2293
22697dac
KH
2294 The variable @code{overriding-local-map} is normally ignored when
2295determining the menu bar contents. That is, the menu bar is computed
2296from the keymaps that would be active if @code{overriding-local-map}
2297were @code{nil}. @xref{Active Keymaps}.
2298
87b2d5ff
RS
2299 In order for a frame to display a menu bar, its @code{menu-bar-lines}
2300parameter must be greater than zero. Emacs uses just one line for the
2301menu bar itself; if you specify more than one line, the other lines
2302serve to separate the menu bar from the windows in the frame. We
fdb48508 2303recommend 1 or 2 as the value of @code{menu-bar-lines}. @xref{Layout
bfe721d1 2304Parameters}.
73804d4b 2305
87b2d5ff 2306 Here's an example of setting up a menu bar item:
73804d4b 2307
87b2d5ff 2308@example
73804d4b 2309@group
87b2d5ff
RS
2310(modify-frame-parameters (selected-frame)
2311 '((menu-bar-lines . 2)))
73804d4b 2312@end group
73804d4b 2313
73804d4b 2314@group
87b2d5ff
RS
2315;; @r{Make a menu keymap (with a prompt string)}
2316;; @r{and make it the menu bar item's definition.}
2317(define-key global-map [menu-bar words]
2318 (cons "Words" (make-sparse-keymap "Words")))
73804d4b 2319@end group
87b2d5ff 2320
73804d4b 2321@group
969fe9b5 2322;; @r{Define specific subcommands in this menu.}
87b2d5ff
RS
2323(define-key global-map
2324 [menu-bar words forward]
2325 '("Forward word" . forward-word))
73804d4b 2326@end group
73804d4b 2327@group
87b2d5ff
RS
2328(define-key global-map
2329 [menu-bar words backward]
2330 '("Backward word" . backward-word))
73804d4b 2331@end group
87b2d5ff 2332@end example
73804d4b 2333
87b2d5ff
RS
2334 A local keymap can cancel a menu bar item made by the global keymap by
2335rebinding the same fake function key with @code{undefined} as the
2336binding. For example, this is how Dired suppresses the @samp{Edit} menu
2337bar item:
73804d4b 2338
87b2d5ff
RS
2339@example
2340(define-key dired-mode-map [menu-bar edit] 'undefined)
2341@end example
73804d4b 2342
87b2d5ff
RS
2343@noindent
2344@code{edit} is the fake function key used by the global map for the
2345@samp{Edit} menu bar item. The main reason to suppress a global
2346menu bar item is to regain space for mode-specific items.
73804d4b 2347
87b2d5ff
RS
2348@defvar menu-bar-final-items
2349Normally the menu bar shows global items followed by items defined by the
2350local maps.
73804d4b 2351
87b2d5ff
RS
2352This variable holds a list of fake function keys for items to display at
2353the end of the menu bar rather than in normal sequence. The default
969fe9b5 2354value is @code{(help-menu)}; thus, the @samp{Help} menu item normally appears
87b2d5ff
RS
2355at the end of the menu bar, following local menu items.
2356@end defvar
73804d4b 2357
bd98ada9 2358@defvar menu-bar-update-hook
35c14f98
RS
2359This normal hook is run by redisplay to update the menu bar contents,
2360before redisplaying the menu bar. You can use it to update submenus
2361whose contents should vary. Since this hook is run frequently, we
2362advise you to ensure that the functions it calls do not take much time
2363in the usual case.
bd98ada9
RS
2364@end defvar
2365
8241495d
RS
2366@node Tool Bar
2367@subsection Tool bars
2368@cindex tool bar
2369
2370 A @dfn{tool bar} is a row of icons at the top of a frame, that execute
2371commands when you click on them---in effect, a kind of graphical menu
35c14f98 2372bar.
8241495d
RS
2373
2374 The frame parameter @code{tool-bar-lines} (X resource @samp{toolBar})
05aea714 2375controls how many lines' worth of height to reserve for the tool bar. A
8241495d
RS
2376zero value suppresses the tool bar. If the value is nonzero, and
2377@code{auto-resize-tool-bars} is non-@code{nil}, the tool bar expands and
2378contracts automatically as needed to hold the specified contents.
2379
2380 The tool bar contents are controlled by a menu keymap attached to a
2381fake ``function key'' called @code{tool-bar} (much like the way the menu
2382bar is controlled). So you define a tool bar item using
2383@code{define-key}, like this:
2384
2385@example
2386(define-key global-map [tool-bar @var{key}] @var{item})
2387@end example
2388
2389@noindent
2390where @var{key} is a fake ``function key'' to distinguish this item from
2391other items, and @var{item} is a menu item key binding (@pxref{Extended
2392Menu Items}), which says how to display this item and how it behaves.
2393
2394 The usual menu keymap item properties, @code{:visible},
2395@code{:enable}, @code{:button}, and @code{:filter}, are useful in
2396tool bar bindings and have their normal meanings. The @var{real-binding}
2397in the item must be a command, not a keymap; in other words, it does not
2398work to define a tool bar icon as a prefix key.
2399
b08d86c6
DL
2400 The @code{:help} property specifies a ``help-echo'' string to display
2401while the mouse is on that item. This is displayed in the same way as
2402@code{help-echo} text properties (@pxref{Help display}).
8241495d
RS
2403
2404 In addition, you should use the @code{:image} property;
2405this is how you specify the image to display in the tool bar:
2406
2407@table @code
2408@item :image @var{image}
2409@var{images} is either a single image specification or a vector of four
2410image specifications. If you use a vector of four,
2411one of them is used, depending on circumstances:
2412
2413@table @asis
2414@item item 0
05aea714 2415Used when the item is enabled and selected.
8241495d
RS
2416@item item 1
2417Used when the item is enabled and deselected.
2418@item item 2
2419Used when the item is disabled and selected.
2420@item item 3
2421Used when the item is disabled and deselected.
2422@end table
2423@end table
2424
a4776185
GM
2425If @var{image} is a single image specification, Emacs draws the tool bar
2426button in disabled state by applying an edge-detection algorithm to the
2427image.
2428
9e445e29
DL
2429The default tool bar is defined so that items specific to editing do not
2430appear for major modes whose command symbol has a @code{mode-class}
2431property of @code{special} (@pxref{Major Mode Conventions}). Major
2432modes may add items to the global bar by binding @code{[tool-bar
2433@var{foo}]} in their local map. It makes sense for some major modes to
2434replace the default tool bar items completely, since not many can be
2435accommodated conveniently, and the default bindings make this easy by
2436using an indirection through @code{tool-bar-map}.
2437
2438@defvar tool-bar-map
9e445e29
DL
2439By default, the global map binds @code{[tool-bar]} as follows:
2440@example
2441(global-set-key [tool-bar]
2442 '(menu-item "tool bar" ignore
2443 :filter (lambda (ignore) tool-bar-map)))
2444@end example
2445@noindent
2446Thus the tool bar map is derived dynamically from the value of variable
2447@code{tool-bar-map} and you should normally adjust the default (global)
2448tool bar by changing that map. Major modes may replace the global bar
2449completely by making @code{tool-bar-map} buffer-local and set to a
2450keymap containing only the desired items. Info mode provides an
2451example.
2452@end defvar
2453
2454There are two convenience functions for defining tool bar items, as
2455follows.
2456
2457@defun tool-bar-add-item icon def key &rest props
9e445e29
DL
2458This function adds an item to the tool bar by modifying
2459@code{tool-bar-map}. The image to use is defined by @var{icon}, which
6d682d42 2460is the base name of an XPM, XBM or PBM image file to be located by
9e445e29
DL
2461@code{find-image}. Given a value @samp{"exit"}, say, @file{exit.xpm},
2462@file{exit.pbm} and @file{exit.xbm} would be searched for in that order
2463on a color display. On a monochrome display, the search order is
2464@samp{.pbm}, @samp{.xbm} and @samp{.xpm}. The binding to use is the
2465command @var{def}, and @var{key} is the fake function key symbol in the
2466prefix keymap. The remaining arguments @var{props} are additional
2467property list elements to add to the menu item specification.
2468
f3544d11 2469To define items in some local map, bind @code{tool-bar-map} with
9e445e29
DL
2470@code{let} around calls of this function:
2471@example
177c0ea7 2472(defvar foo-tool-bar-map
9e445e29
DL
2473 (let ((tool-bar-map (make-sparse-keymap)))
2474 (tool-bar-add-item @dots{})
2475 @dots{}
2476 tool-bar-map))
2477@end example
2478@end defun
2479
2480@defun tool-bar-add-item-from-menu command icon &optional map &rest props
229644e7 2481This function is a convenience for defining tool bar items which are
9e445e29
DL
2482consistent with existing menu bar bindings. The binding of
2483@var{command} is looked up in the menu bar in @var{map} (default
2484@code{global-map}) and modified to add an image specification for
229644e7 2485@var{icon}, which is found in the same way as by
9e445e29 2486@code{tool-bar-add-item}. The resulting binding is then placed in
229644e7
RS
2487@code{tool-bar-map}, so use this function only for global tool bar
2488items.
2489
2490@var{map} must contain an appropriate keymap bound to
2491@code{[menu-bar]}. The remaining arguments @var{props} are additional
2492property list elements to add to the menu item specification.
2493@end defun
2494
2495@defun tool-bar-local-item-from-menu command icon in-map &optional from-map &rest props
2496This function is used for making non-global tool bar items. Use it
2497like @code{tool-bar-add-item-from-menu} except that @var{in-map}
2498specifies the local map to make the definition in. The argument
db8af011 2499@var{from-map} is like the @var{map} argument of
229644e7 2500@code{tool-bar-add-item-from-menu}.
9e445e29
DL
2501@end defun
2502
8241495d
RS
2503@defvar auto-resize-tool-bar
2504If this variable is non-@code{nil}, the tool bar automatically resizes to
2505show all defined tool bar items---but not larger than a quarter of the
2506frame's height.
2507@end defvar
2508
6d682d42 2509@defvar auto-raise-tool-bar-buttons
8241495d
RS
2510If this variable is non-@code{nil}, tool bar items display
2511in raised form when the mouse moves over them.
2512@end defvar
2513
6d682d42 2514@defvar tool-bar-button-margin
8241495d 2515This variable specifies an extra margin to add around tool bar items.
6d682d42 2516The value is an integer, a number of pixels. The default is 4.
8241495d
RS
2517@end defvar
2518
6d682d42 2519@defvar tool-bar-button-relief
8241495d 2520This variable specifies the shadow width for tool bar items.
6d682d42 2521The value is an integer, a number of pixels. The default is 1.
7e50c033
KS
2522@end defvar
2523
7e50c033
KS
2524@defvar tool-bar-border
2525This variable specifies the height of the border drawn below the tool
2526bar area. An integer value specifies height as a number of pixels.
2527If the value is one of @code{internal-border-width} (the default) or
2528@code{border-width}, the tool bar border height corresponds to the
2529corresponding frame parameter.
8241495d
RS
2530@end defvar
2531
2532 You can define a special meaning for clicking on a tool bar item with
2533the shift, control, meta, etc., modifiers. You do this by setting up
2534additional items that relate to the original item through the fake
2535function keys. Specifically, the additional items should use the
2536modified versions of the same fake function key used to name the
2537original item.
2538
2539 Thus, if the original item was defined this way,
2540
2541@example
2542(define-key global-map [tool-bar shell]
2543 '(menu-item "Shell" shell
2544 :image (image :type xpm :file "shell.xpm")))
2545@end example
2546
2547@noindent
2548then here is how you can define clicking on the same tool bar image with
2549the shift modifier:
2550
2551@example
2552(define-key global-map [tool-bar S-shell] 'some-command)
2553@end example
2554
2555@xref{Function Keys}, for more information about how to add modifiers to
2556function keys.
2557
87b2d5ff
RS
2558@node Modifying Menus
2559@subsection Modifying Menus
73804d4b 2560
87b2d5ff
RS
2561 When you insert a new item in an existing menu, you probably want to
2562put it in a particular place among the menu's existing items. If you
2563use @code{define-key} to add the item, it normally goes at the front of
f9f59935 2564the menu. To put it elsewhere in the menu, use @code{define-key-after}:
73804d4b 2565
e5a00c9c 2566@defun define-key-after map key binding &optional after
87b2d5ff
RS
2567Define a binding in @var{map} for @var{key}, with value @var{binding},
2568just like @code{define-key}, but position the binding in @var{map} after
f9f59935
RS
2569the binding for the event @var{after}. The argument @var{key} should be
2570of length one---a vector or string with just one element. But
969fe9b5
RS
2571@var{after} should be a single event type---a symbol or a character, not
2572a sequence. The new binding goes after the binding for @var{after}. If
32f44537
DL
2573@var{after} is @code{t} or is omitted, then the new binding goes last, at
2574the end of the keymap. However, new bindings are added before any
2575inherited keymap.
b2955417 2576
969fe9b5 2577Here is an example:
73804d4b 2578
87b2d5ff
RS
2579@example
2580(define-key-after my-menu [drink]
32f44537 2581 '("Drink" . drink-command) 'eat)
87b2d5ff 2582@end example
73804d4b 2583
87b2d5ff 2584@noindent
969fe9b5
RS
2585makes a binding for the fake function key @key{DRINK} and puts it
2586right after the binding for @key{EAT}.
f9f59935 2587
87b2d5ff
RS
2588Here is how to insert an item called @samp{Work} in the @samp{Signals}
2589menu of Shell mode, after the item @code{break}:
73804d4b 2590
87b2d5ff
RS
2591@example
2592(define-key-after
2593 (lookup-key shell-mode-map [menu-bar signals])
2594 [work] '("Work" . work-command) 'break)
2595@end example
87b2d5ff 2596@end defun
ab5796a9
MB
2597
2598@ignore
2599 arch-tag: cfb87287-9364-4e46-9e93-6c2f7f6ae794
2600@end ignore