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