Mercurial > emacs
comparison lispref/keymaps.texi @ 6558:fa8ff07eaafc
Initial revision
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Mon, 28 Mar 1994 20:21:44 +0000 |
parents | |
children | 2ddb8063f154 |
comparison
equal
deleted
inserted
replaced
6557:74758cf67338 | 6558:fa8ff07eaafc |
---|---|
1 @c -*-texinfo-*- | |
2 @c This is part of the GNU Emacs Lisp Reference Manual. | |
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. | |
4 @c See the file elisp.texi for copying conditions. | |
5 @setfilename ../info/keymaps | |
6 @node Keymaps, Modes, Command Loop, Top | |
7 @chapter Keymaps | |
8 @cindex keymap | |
9 | |
10 The bindings between input events and commands are recorded in data | |
11 structures called @dfn{keymaps}. Each binding in a keymap associates | |
12 (or @dfn{binds}) an individual event type either with another keymap or | |
13 with a command. When an event is bound to a keymap, that keymap is | |
14 used to look up the next input event; this continues until a command | |
15 is found. The whole process is called @dfn{key lookup}. | |
16 | |
17 @menu | |
18 * Keymap Terminology:: Definitions of terms pertaining to keymaps. | |
19 * Format of Keymaps:: What a keymap looks like as a Lisp object. | |
20 * Creating Keymaps:: Functions to create and copy keymaps. | |
21 * Inheritance and Keymaps:: How one keymap can inherit the bindings | |
22 of another keymap. | |
23 * Prefix Keys:: Defining a key with a keymap as its definition. | |
24 * Menu Keymaps:: A keymap can define a menu. | |
25 * Active Keymaps:: Each buffer has a local keymap | |
26 to override the standard (global) bindings. | |
27 A minor mode can also override them. | |
28 * Key Lookup:: How extracting elements from keymaps works. | |
29 * Functions for Key Lookup:: How to request key lookup. | |
30 * Changing Key Bindings:: Redefining a key in a keymap. | |
31 * Key Binding Commands:: Interactive interfaces for redefining keys. | |
32 * Scanning Keymaps:: Looking through all keymaps, for printing help. | |
33 @end menu | |
34 | |
35 @node Keymap Terminology | |
36 @section Keymap Terminology | |
37 @cindex key | |
38 @cindex keystroke | |
39 @cindex key binding | |
40 @cindex binding of a key | |
41 @cindex complete key | |
42 @cindex undefined key | |
43 | |
44 A @dfn{keymap} is a table mapping event types to definitions (which | |
45 can be any Lisp objects, though only certain types are meaningful for | |
46 execution by the command loop). Given an event (or an event type) and a | |
47 keymap, Emacs can get the event's definition. Events include ordinary | |
48 @sc{ASCII} characters, function keys, and mouse actions (@pxref{Input | |
49 Events}). | |
50 | |
51 A sequence of input events that form a unit is called a | |
52 @dfn{key sequence}, or @dfn{key} for short. A sequence of one event | |
53 is always a key sequence, and so are some multi-event sequences. | |
54 | |
55 A keymap determines a binding or definition for any key sequence. If | |
56 the key sequence is a single event, its binding is the definition of the | |
57 event in the keymap. The binding of a key sequence of more than one | |
58 event is found by an iterative process: the binding of the first event | |
59 is found, and must be a keymap; then the second event's binding is found | |
60 in that keymap, and so on until all the events in the key sequence are | |
61 used up. | |
62 | |
63 If the binding of a key sequence is a keymap, we call the key sequence | |
64 a @dfn{prefix key}. Otherwise, we call it a @dfn{complete key} (because | |
65 no more characters can be added to it). If the binding is @code{nil}, | |
66 we call the key @dfn{undefined}. Examples of prefix keys are @kbd{C-c}, | |
67 @kbd{C-x}, and @kbd{C-x 4}. Examples of defined complete keys are | |
68 @kbd{X}, @key{RET}, and @kbd{C-x 4 C-f}. Examples of undefined complete | |
69 keys are @kbd{C-x C-g}, and @kbd{C-c 3}. @xref{Prefix Keys}, for more | |
70 details. | |
71 | |
72 The rule for finding the binding of a key sequence assumes that the | |
73 intermediate bindings (found for the events before the last) are all | |
74 keymaps; if this is not so, the sequence of events does not form a | |
75 unit---it is not really a key sequence. In other words, removing one or | |
76 more events from the end of any valid key must always yield a prefix | |
77 key. For example, @kbd{C-f C-f} is not a key; @kbd{C-f} is not a prefix | |
78 key, so a longer sequence starting with @kbd{C-f} cannot be a key. | |
79 | |
80 Note that the set of possible multi-event key sequences depends on the | |
81 bindings for prefix keys; therefore, it can be different for different | |
82 keymaps, and can change when bindings are changed. However, a one-event | |
83 sequence is always a key sequence, because it does not depend on any | |
84 prefix keys for its well-formedness. | |
85 | |
86 At any time, several primary keymaps are @dfn{active}---that is, in | |
87 use for finding key bindings. These are the @dfn{global map}, which is | |
88 shared by all buffers; the @dfn{local keymap}, which is usually | |
89 associated with a specific major mode; and zero or more @dfn{minor mode | |
90 keymaps} which belong to currently enabled minor modes. (Not all minor | |
91 modes have keymaps.) The local keymap bindings shadow (i.e., take | |
92 precedence over) the corresponding global bindings. The minor mode | |
93 keymaps shadow both local and global keymaps. @xref{Active Keymaps}, | |
94 for details. | |
95 | |
96 @node Format of Keymaps | |
97 @section Format of Keymaps | |
98 @cindex format of keymaps | |
99 @cindex keymap format | |
100 @cindex full keymap | |
101 @cindex sparse keymap | |
102 | |
103 A keymap is a list whose @sc{car} is the symbol @code{keymap}. The | |
104 remaining elements of the list define the key bindings of the keymap. | |
105 Use the function @code{keymapp} (see below) to test whether an object is | |
106 a keymap. | |
107 | |
108 An ordinary element is a cons cell of the form @code{(@var{type} .@: | |
109 @var{binding})}. This specifies one binding which applies to events of | |
110 type @var{type}. Each ordinary binding applies to events of a | |
111 particular @dfn{event type}, which is always a character or a symbol. | |
112 @xref{Classifying Events}. | |
113 | |
114 @cindex default key binding | |
115 @c Emacs 19 feature | |
116 A cons cell whose @sc{car} is @code{t} is a @dfn{default key binding}; | |
117 any event not bound by other elements of the keymap is given | |
118 @var{binding} as its binding. Default bindings allow a keymap to bind | |
119 all possible event types without having to enumerate all of them. A | |
120 keymap that has a default binding completely masks any lower-precedence | |
121 keymap. | |
122 | |
123 If an element of a keymap is a vector, the vector counts as bindings | |
124 for all the @sc{ASCII} characters; vector element @var{n} is the binding | |
125 for the character with code @var{n}. This is a more compact way to | |
126 record lots of bindings. A keymap with such a vector is called a | |
127 @dfn{full keymap}. Other keymaps are called @dfn{sparse keymaps}. | |
128 | |
129 When a keymap contains a vector, it always defines a binding for every | |
130 @sc{ASCII} character even if the vector element is @code{nil}. Such a | |
131 binding of @code{nil} overrides any default binding in the keymap. | |
132 However, default bindings are still meaningful for events that are not | |
133 @sc{ASCII} characters. A binding of @code{nil} does @emph{not} | |
134 override lower-precedence keymaps; thus, if the local map gives a | |
135 binding of @code{nil}, Emacs uses the binding from the global map. | |
136 | |
137 @cindex keymap prompt string | |
138 @cindex overall prompt string | |
139 @cindex prompt string of keymap | |
140 Aside from bindings, a keymap can also have a string as an element. | |
141 This is called the @dfn{overall prompt string} and makes it possible to | |
142 use the keymap as a menu. @xref{Menu Keymaps}. | |
143 | |
144 @cindex meta characters lookup | |
145 Keymaps do not directly record bindings for the meta characters, whose | |
146 codes are from 128 to 255. Instead, meta characters are regarded for | |
147 purposes of key lookup as sequences of two characters, the first of | |
148 which is @key{ESC} (or whatever is currently the value of | |
149 @code{meta-prefix-char}). Thus, the key @kbd{M-a} is really represented | |
150 as @kbd{@key{ESC} a}, and its global binding is found at the slot for | |
151 @kbd{a} in @code{esc-map} (@pxref{Prefix Keys}). | |
152 | |
153 Here as an example is the local keymap for Lisp mode, a sparse | |
154 keymap. It defines bindings for @key{DEL} and @key{TAB}, plus @kbd{C-c | |
155 C-l}, @kbd{M-C-q}, and @kbd{M-C-x}. | |
156 | |
157 @example | |
158 @group | |
159 lisp-mode-map | |
160 @result{} | |
161 @end group | |
162 @group | |
163 (keymap | |
164 ;; @key{TAB} | |
165 (9 . lisp-indent-line) | |
166 @end group | |
167 @group | |
168 ;; @key{DEL} | |
169 (127 . backward-delete-char-untabify) | |
170 @end group | |
171 @group | |
172 (3 keymap | |
173 ;; @kbd{C-c C-l} | |
174 (12 . run-lisp)) | |
175 @end group | |
176 @group | |
177 (27 keymap | |
178 ;; @r{@kbd{M-C-q}, treated as @kbd{@key{ESC} C-q}} | |
179 (17 . indent-sexp) | |
180 ;; @r{@kbd{M-C-x}, treated as @kbd{@key{ESC} C-x}} | |
181 (24 . lisp-send-defun))) | |
182 @end group | |
183 @end example | |
184 | |
185 @defun keymapp object | |
186 This function returns @code{t} if @var{object} is a keymap, @code{nil} | |
187 otherwise. Practically speaking, this function tests for a list whose | |
188 @sc{car} is @code{keymap}. | |
189 | |
190 @example | |
191 @group | |
192 (keymapp '(keymap)) | |
193 @result{} t | |
194 @end group | |
195 @group | |
196 (keymapp (current-global-map)) | |
197 @result{} t | |
198 @end group | |
199 @end example | |
200 @end defun | |
201 | |
202 @node Creating Keymaps | |
203 @section Creating Keymaps | |
204 @cindex creating keymaps | |
205 | |
206 Here we describe the functions for creating keymaps. | |
207 | |
208 @defun make-keymap &optional prompt | |
209 This function creates and returns a new full keymap (i.e., one which | |
210 contains a vector of length 128 for defining all the @sc{ASCII} | |
211 characters). The new keymap initially binds all @sc{ASCII} characters | |
212 to @code{nil}, and does not bind any other kind of event. | |
213 | |
214 @example | |
215 @group | |
216 (make-keymap) | |
217 @result{} (keymap [nil nil nil @dots{} nil nil]) | |
218 @end group | |
219 @end example | |
220 | |
221 If you specify @var{prompt}, that becomes the overall prompt string for | |
222 the keymap. The prompt string is useful for menu keymaps (@pxref{Menu | |
223 Keymaps}). | |
224 @end defun | |
225 | |
226 @defun make-sparse-keymap &optional prompt | |
227 This function creates and returns a new sparse keymap with no entries. | |
228 The new keymap does not bind any events. The argument @var{prompt} | |
229 specifies a prompt string, as in @code{make-keymap}. | |
230 | |
231 @example | |
232 @group | |
233 (make-sparse-keymap) | |
234 @result{} (keymap) | |
235 @end group | |
236 @end example | |
237 @end defun | |
238 | |
239 @defun copy-keymap keymap | |
240 This function returns a copy of @var{keymap}. Any keymaps which | |
241 appear directly as bindings in @var{keymap} are also copied recursively, | |
242 and so on to any number of levels. However, recursive copying does not | |
243 take place when the definition of a character is a symbol whose function | |
244 definition is a keymap; the same symbol appears in the new copy. | |
245 @c Emacs 19 feature | |
246 | |
247 @example | |
248 @group | |
249 (setq map (copy-keymap (current-local-map))) | |
250 @result{} (keymap | |
251 @end group | |
252 @group | |
253 ;; @r{(This implements meta characters.)} | |
254 (27 keymap | |
255 (83 . center-paragraph) | |
256 (115 . center-line)) | |
257 (9 . tab-to-tab-stop)) | |
258 @end group | |
259 | |
260 @group | |
261 (eq map (current-local-map)) | |
262 @result{} nil | |
263 @end group | |
264 @group | |
265 (equal map (current-local-map)) | |
266 @result{} t | |
267 @end group | |
268 @end example | |
269 @end defun | |
270 | |
271 @node Inheritance and Keymaps | |
272 @section Inheritance and Keymaps | |
273 @cindex keymap inheritance | |
274 @cindex inheriting a keymap's bindings | |
275 | |
276 A keymap can inherit the bindings of another keymap. Do do this, make | |
277 a keymap whose ``tail'' is another existing keymap to inherit from. | |
278 Such a keymap looks like this: | |
279 | |
280 @example | |
281 (keymap @var{bindings}@dots{} . @var{other-keymap}) | |
282 @end example | |
283 | |
284 @noindent | |
285 The effect is that this keymap inherits all the bindings of | |
286 @var{other-keymap}, whatever they may be at the time a key is looked up, | |
287 but can add to them or override them with @var{bindings}. | |
288 | |
289 If you change the bindings in @var{other-keymap} using @code{define-key} | |
290 or other key-binding functions, these changes are visible in the | |
291 inheriting keymap unless shadowed by @var{bindings}. The converse is | |
292 not true: if you use @code{define-key} to change the inheriting keymap, | |
293 that affects @var{bindings}, but has no effect on @var{other-keymap}. | |
294 | |
295 Here is an example showing how to make a keymap that inherits | |
296 from @code{text-mode-map}: | |
297 | |
298 @example | |
299 (setq my-mode-map (cons 'keymap text-mode-map)) | |
300 @end example | |
301 | |
302 @node Prefix Keys | |
303 @section Prefix Keys | |
304 @cindex prefix key | |
305 | |
306 A @dfn{prefix key} has an associated keymap which defines what to do | |
307 with key sequences that start with the prefix key. For example, | |
308 @kbd{C-x} is a prefix key, and it uses a keymap which is also stored in | |
309 the variable @code{ctl-x-map}. Here is a list of the standard prefix | |
310 keys of Emacs and their keymaps: | |
311 | |
312 @itemize @bullet | |
313 @item | |
314 @vindex esc-map | |
315 @findex ESC-prefix | |
316 @code{esc-map} is used for events that follow @key{ESC}. Thus, the | |
317 global definitions of all meta characters are actually found here. This | |
318 map is also the function definition of @code{ESC-prefix}. | |
319 | |
320 @item | |
321 @cindex @kbd{C-h} | |
322 @code{help-map} is used for events that follow @kbd{C-h}. | |
323 | |
324 @item | |
325 @cindex @kbd{C-c} | |
326 @vindex mode-specific-map | |
327 @code{mode-specific-map} is for events that follow @kbd{C-c}. This | |
328 map is not actually mode specific; its name was chosen to be informative | |
329 for the user in @kbd{C-h b} (@code{display-bindings}), where it | |
330 describes the main use of the @kbd{C-c} prefix key. | |
331 | |
332 @item | |
333 @cindex @kbd{C-x} | |
334 @vindex ctl-x-map | |
335 @findex Control-X-prefix | |
336 @code{ctl-x-map} is the variable name for the map used for events | |
337 that follow @kbd{C-x}. This map is also the function definition of | |
338 @code{Control-X-prefix}. | |
339 | |
340 @item | |
341 @cindex @kbd{C-x 4} | |
342 @vindex ctl-x-4-map | |
343 @code{ctl-x-4-map} is used for events that follow @kbd{C-x 4}. | |
344 | |
345 @c Emacs 19 feature | |
346 @item | |
347 @cindex @kbd{C-x 5} | |
348 @vindex ctl-x-5-map | |
349 @code{ctl-x-5-map} used is for events that follow @kbd{C-x 5}. | |
350 | |
351 @c Emacs 19 feature | |
352 @item | |
353 @cindex @kbd{C-x n} | |
354 @cindex @kbd{C-x r} | |
355 @cindex @kbd{C-x a} | |
356 The prefix keys @kbd{C-x n}, @kbd{C-x r} and @kbd{C-x a} use keymaps | |
357 that have no special name. | |
358 @end itemize | |
359 | |
360 The binding of a prefix key is the keymap to use for looking up the | |
361 events that follow the prefix key. (It may instead be a symbol whose | |
362 function definition is a keymap. The effect is the same, but the symbol | |
363 serves as a name for the prefix key.) Thus, the binding of @kbd{C-x} is | |
364 the symbol @code{Control-X-prefix}, whose function definition is the | |
365 keymap for @kbd{C-x} commands. (The same keymap is also the value of | |
366 @code{ctl-x-map}.) | |
367 | |
368 Prefix key definitions of this sort can appear in any active keymap. | |
369 The definitions of @kbd{C-c}, @kbd{C-x}, @kbd{C-h} and @key{ESC} as | |
370 prefix keys appear in the global map, so these prefix keys are always | |
371 available. Major and minor modes can redefine a key as a prefix by | |
372 putting a prefix key definition for it in the local map or the minor | |
373 mode's map. @xref{Active Keymaps}. | |
374 | |
375 If a key is defined as a prefix in more than one active map, then its | |
376 various definitions are in effect merged: the commands defined in the | |
377 minor mode keymaps come first, followed by those in the local map's | |
378 prefix definition, and then by those from the global map. | |
379 | |
380 In the following example, we make @kbd{C-p} a prefix key in the local | |
381 keymap, in such a way that @kbd{C-p} is identical to @kbd{C-x}. Then | |
382 the binding for @kbd{C-p C-f} is the function @code{find-file}, just | |
383 like @kbd{C-x C-f}. The key sequence @kbd{C-p 6} is not found in any | |
384 active keymap. | |
385 | |
386 @example | |
387 @group | |
388 (use-local-map (make-sparse-keymap)) | |
389 @result{} nil | |
390 @end group | |
391 @group | |
392 (local-set-key "\C-p" ctl-x-map) | |
393 @result{} nil | |
394 @end group | |
395 @group | |
396 (key-binding "\C-p\C-f") | |
397 @result{} find-file | |
398 @end group | |
399 | |
400 @group | |
401 (key-binding "\C-p6") | |
402 @result{} nil | |
403 @end group | |
404 @end example | |
405 | |
406 @defun define-prefix-command symbol | |
407 @cindex prefix command | |
408 This function defines @var{symbol} as a prefix command: it creates a | |
409 full keymap and stores it as @var{symbol}'s function definition. | |
410 Storing the symbol as the binding of a key makes the key a prefix key | |
411 which has a name. It also sets @var{symbol} as a variable, to have the | |
412 keymap as its value. The function returns @var{symbol}. | |
413 | |
414 In Emacs version 18, only the function definition of @var{symbol} was | |
415 set, not the value as a variable. | |
416 @end defun | |
417 | |
418 @node Menu Keymaps | |
419 @section Menu Keymaps | |
420 @cindex menu keymaps | |
421 | |
422 @c Emacs 19 feature | |
423 A keymap can define a menu as well as bindings for keyboard keys and | |
424 mouse button. Menus are usually actuated with the mouse, but they can | |
425 work with the keyboard also. | |
426 | |
427 @menu | |
428 * Defining Menus:: How to make a keymap that defines a menu. | |
429 * Mouse Menus:: How users actuate the menu with the mouse. | |
430 * Keyboard Menus:: How they actuate it with the keyboard. | |
431 * Menu Example:: Making a simple menu. | |
432 * Menu Bar:: How to customize the menu bar. | |
433 * Modifying Menus:: How to add new items to a menu. | |
434 @end menu | |
435 | |
436 @node Defining Menus | |
437 @subsection Defining Menus | |
438 @cindex defining menus | |
439 @cindex menu prompt string | |
440 @cindex prompt string (of menu) | |
441 | |
442 A keymap is suitable for menu use if it has an @dfn{overall prompt | |
443 string}, which is a string that appears as an element of the keymap. | |
444 (@xref{Format of Keymaps}.) The string should describe the purpose of | |
445 the menu. The easiest way to construct a keymap with a prompt string is | |
446 to specify the string as an argument when you call @code{make-keymap} or | |
447 @code{make-sparse-keymap} (@pxref{Creating Keymaps}). | |
448 | |
449 The individual bindings in the menu keymap should have item | |
450 strings; these strings become the items displayed in the menu. A | |
451 binding with a item string looks like this: | |
452 | |
453 @example | |
454 (@var{string} . @var{real-binding}) | |
455 @end example | |
456 | |
457 The item string for a binding should be short---one or two words. It | |
458 should describe the action of the command it corresponds to. | |
459 | |
460 As far as @code{define-key} is concerned, @var{string} is part of the | |
461 event's binding. However, @code{lookup-key} returns just | |
462 @var{real-binding}, and only @var{real-binding} is used for executing | |
463 the key. | |
464 | |
465 You can also supply a second string, called the help string, as follows: | |
466 | |
467 @example | |
468 (@var{string} @var{help-string} . @var{real-binding}) | |
469 @end example | |
470 | |
471 Currently Emacs does not actually use @var{help-string}; it knows only | |
472 how to ignore @var{help-string} in order to extract @var{real-binding}. | |
473 In the future we hope to make @var{help-string} serve as extended | |
474 documentation for the menu item, available on request. | |
475 | |
476 If @var{real-binding} is @code{nil}, then @var{string} appears in the | |
477 menu but cannot be selected. | |
478 | |
479 If @var{real-binding} is a symbol, and has a non-@code{nil} | |
480 @code{menu-enable} property, that property is an expression which | |
481 controls whether the menu item is enabled. Every time the keymap is | |
482 used to display a menu, Emacs evaluates the expression, and it enables | |
483 the menu item only if the expression's value is non-@code{nil}. When a | |
484 menu item is disabled, it is displayed in a ``fuzzy'' fashion, and | |
485 cannot be selected with the mouse. | |
486 | |
487 The order of items in the menu is the same as the order of bindings in | |
488 the keymap. Since @code{define-key} puts new bindings at the front, you | |
489 should define the menu items starting at the bottom of the menu and | |
490 moving to the top, if you care about the order. When you add an item to | |
491 an existing menu, you can specify its position in the menu using | |
492 @code{define-key-after} (@pxref{Modifying Menus}). | |
493 | |
494 You've probably noticed that menu items show the equivalent keyboard key | |
495 sequence (if any) to invoke the same command. To save time on | |
496 recalculation, menu display caches this information in a sublist in the | |
497 binding, like this: | |
498 | |
499 @c This line is not too long--rms. | |
500 @example | |
501 (@var{string} @r{[}@var{help-string}@r{]} (@var{key-binding-data}) . @var{real-binding}) | |
502 @end example | |
503 | |
504 Don't put these sublists in the menu item yourself; menu display | |
505 calculates them automatically. Don't add keyboard equivalents to the | |
506 item string yourself, for that is redundant. | |
507 | |
508 @node Mouse Menus | |
509 @subsection Menus and the Mouse | |
510 | |
511 The way to make a menu keymap produce a menu is to make it the | |
512 definition of a prefix key. | |
513 | |
514 If the prefix key ends with a mouse event, Emacs handles the menu keymap | |
515 by popping up a visible menu, so that the user can select a choice with | |
516 the mouse. When the user clicks on a menu item, the event generated is | |
517 whatever character or symbol has the binding which brought about that | |
518 menu item. (A menu item may generate a series of events if the menu has | |
519 multiple levels or comes from the menu bar.) | |
520 | |
521 It's often best to use a button-down event to trigger the menu. Then | |
522 the user can select a menu item by releasing the button. | |
523 | |
524 A single keymap can appear as multiple menu panes, if you explicitly | |
525 arrange for this. The way to do this is to make a keymap for each pane, | |
526 then create a binding for each of those maps in the main keymap of the | |
527 menu. Give each of these bindings a item string that starts with | |
528 @samp{@@}. The rest of the item string becomes the name of the pane. | |
529 See the file @file{lisp/mouse.el} for an example of this. Any ordinary | |
530 bindings with @samp{@@}-less item strings are grouped into one pane, | |
531 which appears along with the other panes explicitly created for the | |
532 submaps. | |
533 | |
534 X toolkit menus don't have panes; instead, they can have submenus. | |
535 Every nested keymap becomes a submenu, whether the item string starts | |
536 with @samp{@@} or not. In a toolkit version of Emacs, The only thing | |
537 special about @samp{@@} at the beginning of an item string is that the | |
538 @samp{@@} doesn't appear in the menu item. | |
539 | |
540 You can also get multiple panes from separate keymaps. The full | |
541 definition of a prefix key always comes from merging the definitions | |
542 supplied by the various active keymaps (minor mode, local, and | |
543 global). When more than one of these keymaps is a menu, each of them | |
544 makes a separate pane or panes. @xref{Active Keymaps}. | |
545 | |
546 In toolkit versions of Emacs, menus don't have panes, so submenus are | |
547 used to represent the separate keymaps. Each keymap's contribution | |
548 becomes one submenu. | |
549 | |
550 A Lisp program can explicitly pop up a menu and receive the user's | |
551 choice. You can use keymaps for this also. @xref{Pop-Up Menus}. | |
552 | |
553 @node Keyboard Menus | |
554 @subsection Menus and the Keyboard | |
555 | |
556 When a prefix key ending with a keyboard event (a character or function | |
557 key) has a definition that is a menu keymap, the user can use the | |
558 keyboard to choose a menu item. | |
559 | |
560 Emacs displays the menu alternatives (the item strings of the | |
561 bindings) in the echo area. If they don't all fit at once, the user can | |
562 type @key{SPC} to see the next line of alternatives. Successive uses of | |
563 @key{SPC} eventually get to the end of the menu and then cycle around to | |
564 the beginning. | |
565 | |
566 When the user has found the desired alternative from the menu, he or she | |
567 should type the corresponding character---the one whose binding is that | |
568 alternative. | |
569 | |
570 In a menu intended for keyboard use, each menu item must clearly | |
571 indicate what character to type. The best convention to use is to make | |
572 the character the first letter of the item string. That is something | |
573 users will understand without being told. | |
574 | |
575 This way of using menus in an Emacs-like editor was inspired by the | |
576 Hierarkey system. | |
577 | |
578 @defvar menu-prompt-more-char | |
579 This variable specifies the character to use to ask to see | |
580 the next line of a menu. Its initial value is 32, the code | |
581 for @key{SPC}. | |
582 @end defvar | |
583 | |
584 @node Menu Example | |
585 @subsection Menu Example | |
586 | |
587 Here is a simple example of how to set up a menu for mouse use. | |
588 | |
589 @example | |
590 (defvar my-menu-map | |
591 (make-sparse-keymap "Key Commands <==> Functions")) | |
592 (fset 'help-for-keys my-menu-map) | |
593 | |
594 (define-key my-menu-map [bindings] | |
595 '("List all keystroke commands" . describe-bindings)) | |
596 (define-key my-menu-map [key] | |
597 '("Describe key briefly" . describe-key-briefly)) | |
598 (define-key my-menu-map [key-verbose] | |
599 '("Describe key verbose" . describe-key)) | |
600 (define-key my-menu-map [function] | |
601 '("Describe Lisp function" . describe-function)) | |
602 (define-key my-menu-map [where-is] | |
603 '("Where is this command" . where-is)) | |
604 | |
605 (define-key global-map [C-S-down-mouse-1] 'help-for-keys) | |
606 @end example | |
607 | |
608 The symbols used in the key sequences bound in the menu are fictitious | |
609 ``function keys''; they don't appear on the keyboard, but that doesn't | |
610 stop you from using them in the menu. Their names were chosen to be | |
611 mnemonic, because they show up in the output of @code{where-is} and | |
612 @code{apropos} to identify the corresponding menu items. | |
613 | |
614 However, if you want the menu to be usable from the keyboard as well, | |
615 you must bind real @sc{ASCII} characters as well as fictitious function | |
616 keys. | |
617 | |
618 @node Menu Bar | |
619 @subsection The Menu Bar | |
620 @cindex menu bar | |
621 | |
622 Most window systems allow each frame to have a @dfn{menu bar}---a | |
623 permanently displayed menu stretching horizontally across the top of the | |
624 frame. The items of the menu bar are the subcommands of the fake | |
625 ``function key'' @code{menu-bar}, as defined by all the active keymaps. | |
626 | |
627 To add an item to the menu bar, invent a fake ``function key'' of your | |
628 own (let's call it @var{key}), and make a binding for the key sequence | |
629 @code{[menu-bar @var{key}]}. Most often, the binding is a menu keymap, | |
630 so that pressing a button on the menu bar item leads to another menu. | |
631 | |
632 When more than one active keymap defines the same fake function key | |
633 for the menu bar, the item appears just once. If the user clicks on | |
634 that menu bar item, it brings up a single, combined submenu containing | |
635 all the subcommands of that item---the global subcommands, the local | |
636 subcommands, and the minor mode subcommands, all together. | |
637 | |
638 In order for a frame to display a menu bar, its @code{menu-bar-lines} | |
639 parameter must be greater than zero. Emacs uses just one line for the | |
640 menu bar itself; if you specify more than one line, the other lines | |
641 serve to separate the menu bar from the windows in the frame. We | |
642 recommend you try 1 or 2 as the value of @code{menu-bar-lines}. @xref{X | |
643 Frame Parameters}. | |
644 | |
645 Here's an example of setting up a menu bar item: | |
646 | |
647 @smallexample | |
648 (modify-frame-parameters (selected-frame) '((menu-bar-lines . 2))) | |
649 | |
650 ;; @r{Make a menu keymap (with a prompt string)} | |
651 ;; @r{and make it the menu bar item's definition.} | |
652 (define-key global-map [menu-bar words] | |
653 (cons "Words" (make-sparse-keymap "Words"))) | |
654 | |
655 @group | |
656 ;; @r{Define specific subcommands in the item's menu.} | |
657 (define-key global-map | |
658 [menu-bar words forward] | |
659 '("Forward word" . forward-word)) | |
660 @end group | |
661 @group | |
662 (define-key global-map | |
663 [menu-bar words backward] | |
664 '("Backward word" . backward-word)) | |
665 @end group | |
666 @end smallexample | |
667 | |
668 A local keymap can cancel a menu bar item made by the global keymap by | |
669 rebinding the same fake function key with @code{undefined} as the | |
670 binding. For example, this is how Dired suppresses the @samp{Edit} menu | |
671 bar item: | |
672 | |
673 @example | |
674 (define-key dired-mode-map [menu-bar edit] 'undefined) | |
675 @end example | |
676 | |
677 @noindent | |
678 @code{edit} is the fake function key used by the global map for the | |
679 @samp{Edit} menu bar item. The main reason to suppress a global | |
680 menu bar item is to regain space for mode-specific items. | |
681 | |
682 @defvar menu-bar-final-items | |
683 Normally the menu bar shows global items followed by items defined by the | |
684 local maps. | |
685 | |
686 This variable holds a list of fake function keys for items to display at | |
687 the end of the menu bar rather than in normal sequence. The default | |
688 value is @code{(help)}; thus, the @samp{Help} menu item normally appears | |
689 at the end of the menu bar, following local menu items. | |
690 @end defvar | |
691 | |
692 @node Modifying Menus | |
693 @subsection Modifying Menus | |
694 | |
695 When you insert a new item in an existing menu, you probably want to | |
696 put it in a particular place among the menu's existing items. If you | |
697 use @code{define-key} to add the item, it normally goes at the front of | |
698 the menu. To put it elsewhere, use @code{define-key-after}: | |
699 | |
700 @defun define-key-after map key binding after | |
701 Define a binding in @var{map} for @var{key}, with value @var{binding}, | |
702 just like @code{define-key}, but position the binding in @var{map} after | |
703 the binding for the key @var{after}. For example, | |
704 | |
705 @example | |
706 (define-key-after my-menu [drink] | |
707 '("Drink" . drink-command) [eat]) | |
708 @end example | |
709 | |
710 @noindent | |
711 makes a binding for the fake function key @key{drink} and puts it | |
712 right after the binding for @key{eat}. | |
713 @end defun | |
714 | |
715 @node Active Keymaps | |
716 @section Active Keymaps | |
717 @cindex active keymap | |
718 @cindex global keymap | |
719 @cindex local keymap | |
720 | |
721 Emacs normally contains many keymaps; at any given time, just a few of | |
722 them are @dfn{active} in that they participate in the interpretation | |
723 of user input. These are the global keymap, the current buffer's | |
724 local keymap, and the keymaps of any enabled minor modes. | |
725 | |
726 The @dfn{global keymap} holds the bindings of keys that are defined | |
727 regardless of the current buffer, such as @kbd{C-f}. The variable | |
728 @code{global-map} holds this keymap, which is always active. | |
729 | |
730 Each buffer may have another keymap, its @dfn{local keymap}, which may | |
731 contain new or overriding definitions for keys. The current buffer's | |
732 local keymap is always active except when @code{overriding-local-map} | |
733 overrides it. Text properties can specify an alternative local map for | |
734 certain parts of the buffer; see @ref{Special Properties}. | |
735 | |
736 Each minor mode may have a keymap; if it does, the keymap is active | |
737 when the minor mode is enabled. | |
738 | |
739 The variable @code{overriding-local-map}, if non-@code{nil}, specifies | |
740 another local keymap that overrides the buffer's local map and all the | |
741 minor mode keymaps. | |
742 | |
743 All the active keymaps are used together to determine what command to | |
744 execute when a key is entered. Emacs searches these maps one by one, in | |
745 order of decreasing precedence, until it finds a binding in one of the maps. | |
746 | |
747 Normally, Emacs @emph{first} searches for the key in the minor mode | |
748 maps (one map at a time); if they do not supply a binding for the key, | |
749 Emacs searches the local map; if that too has no binding, Emacs then | |
750 searches the global map. However, if @code{overriding-local-map} is | |
751 non-@code{nil}, Emacs searches that map first, followed by the global | |
752 map. | |
753 | |
754 The procedure for searching a single keymap is called | |
755 @dfn{key lookup}; see @ref{Key Lookup}. | |
756 | |
757 @cindex major mode keymap | |
758 Since every buffer that uses the same major mode normally uses the | |
759 very same local keymap, it may appear as if the keymap is local to the | |
760 mode. A change to the local keymap of a buffer (using | |
761 @code{local-set-key}, for example) will be seen also in the other | |
762 buffers that share that keymap. | |
763 | |
764 The local keymaps that are used for Lisp mode, C mode, and several | |
765 other major modes exist even if they have not yet been used. These | |
766 local maps are the values of the variables @code{lisp-mode-map}, | |
767 @code{c-mode-map}, and so on. For most other modes, which are less | |
768 frequently used, the local keymap is constructed only when the mode is | |
769 used for the first time in a session. | |
770 | |
771 The minibuffer has local keymaps, too; they contain various completion | |
772 and exit commands. @xref{Intro to Minibuffers}. | |
773 | |
774 @xref{Standard Keymaps}, for a list of standard keymaps. | |
775 | |
776 @defvar global-map | |
777 This variable contains the default global keymap that maps Emacs | |
778 keyboard input to commands. The global keymap is normally this keymap. | |
779 The default global keymap is a full keymap that binds | |
780 @code{self-insert-command} to all of the printing characters. | |
781 | |
782 It is normal practice to change the bindings in the global map, but you | |
783 should not assign this variable any value other than the keymap it starts | |
784 out with. | |
785 @end defvar | |
786 | |
787 @defun current-global-map | |
788 This function returns the current global keymap. This is the | |
789 same as the value of @code{global-map} unless you change one or the | |
790 other. | |
791 | |
792 @example | |
793 @group | |
794 (current-global-map) | |
795 @result{} (keymap [set-mark-command beginning-of-line @dots{} | |
796 delete-backward-char]) | |
797 @end group | |
798 @end example | |
799 @end defun | |
800 | |
801 @defun current-local-map | |
802 This function returns the current buffer's local keymap, or @code{nil} | |
803 if it has none. In the following example, the keymap for the | |
804 @samp{*scratch*} buffer (using Lisp Interaction mode) is a sparse keymap | |
805 in which the entry for @key{ESC}, @sc{ASCII} code 27, is another sparse | |
806 keymap. | |
807 | |
808 @example | |
809 @group | |
810 (current-local-map) | |
811 @result{} (keymap | |
812 (10 . eval-print-last-sexp) | |
813 (9 . lisp-indent-line) | |
814 (127 . backward-delete-char-untabify) | |
815 @end group | |
816 @group | |
817 (27 keymap | |
818 (24 . eval-defun) | |
819 (17 . indent-sexp))) | |
820 @end group | |
821 @end example | |
822 @end defun | |
823 | |
824 @defun current-minor-mode-maps | |
825 This function returns a list of the keymaps of currently enabled minor modes. | |
826 @end defun | |
827 | |
828 @defun use-global-map keymap | |
829 This function makes @var{keymap} the new current global keymap. It | |
830 returns @code{nil}. | |
831 | |
832 It is very unusual to change the global keymap. | |
833 @end defun | |
834 | |
835 @defun use-local-map keymap | |
836 This function makes @var{keymap} the new local keymap of the current | |
837 buffer. If @var{keymap} is @code{nil}, then the buffer has no local | |
838 keymap. @code{use-local-map} returns @code{nil}. Most major mode | |
839 commands use this function. | |
840 @end defun | |
841 | |
842 @c Emacs 19 feature | |
843 @defvar minor-mode-map-alist | |
844 This variable is an alist describing keymaps that may or may not be | |
845 active according to the values of certain variables. Its elements look | |
846 like this: | |
847 | |
848 @example | |
849 (@var{variable} . @var{keymap}) | |
850 @end example | |
851 | |
852 The keymap @var{keymap} is active whenever @var{variable} has a | |
853 non-@code{nil} value. Typically @var{variable} is the variable which | |
854 enables or disables a minor mode. @xref{Keymaps and Minor Modes}. | |
855 | |
856 Note that elements of @code{minor-mode-map-alist} do not have the same | |
857 structure as elements of @code{minor-mode-alist}. The map must be the | |
858 @sc{cdr} of the element; a list with the map as the second element will | |
859 not do. | |
860 | |
861 What's more, the keymap itself must appear in the @sc{cdr}. It does not | |
862 work to store a variable in the @sc{cdr} and make the map the value of | |
863 that variable. | |
864 | |
865 When more than one minor mode keymap is active, their order of priority | |
866 is the order of @code{minor-mode-map-alist}. But you should design | |
867 minor modes so that they don't interfere with each other. If you do | |
868 this properly, the order will not matter. | |
869 | |
870 See also @code{minor-mode-key-binding} in @ref{Functions for Key | |
871 Lookup}. See @ref{Keymaps and Minor Modes}, for more information about | |
872 minor modes. | |
873 @end defvar | |
874 | |
875 @defvar overriding-local-map | |
876 If non-@code{nil}, a keymap to use instead of the buffer's local keymap | |
877 and instead of all the minor mode keymaps. This keymap, if any, | |
878 overrides all other maps that would have been active, except for the | |
879 current global map. | |
880 @end defvar | |
881 | |
882 @node Key Lookup | |
883 @section Key Lookup | |
884 @cindex key lookup | |
885 @cindex keymap entry | |
886 | |
887 @dfn{Key lookup} is the process of finding the binding of a key | |
888 sequence from a given keymap. Actual execution of the binding is not | |
889 part of key lookup. | |
890 | |
891 Key lookup uses just the event types of each event in the key | |
892 sequence; the rest of the event is ignored. In fact, a key sequence | |
893 used for key lookup may designate mouse events with just their types | |
894 (symbols) instead of with entire mouse events (lists). @xref{Input | |
895 Events}. Such a pseudo-key-sequence is insufficient for | |
896 @code{command-execute}, but it is sufficient for looking up or rebinding | |
897 a key. | |
898 | |
899 When the key sequence consists of multiple events, key lookup | |
900 processes the events sequentially: the binding of the first event is | |
901 found, and must be a keymap; then the second event's binding is found in | |
902 that keymap, and so on until all the events in the key sequence are used | |
903 up. (The binding thus found for the last event may or may not be a | |
904 keymap.) Thus, the process of key lookup is defined in terms of a | |
905 simpler process for looking up a single event in a keymap. How that is | |
906 done depends on the type of object associated with the event in that | |
907 keymap. | |
908 | |
909 Let's use the term @dfn{keymap entry} to describe the value found by | |
910 looking up an event type in a keymap. (This doesn't include the item | |
911 string and other extra elements in menu key bindings because | |
912 @code{lookup-key} and other key lookup functions don't include them in | |
913 the returned value.) While any Lisp object may be stored in a keymap as | |
914 a keymap entry, not all make sense for key lookup. Here is a list of | |
915 the meaningful kinds of keymap entries: | |
916 | |
917 @table @asis | |
918 @item @code{nil} | |
919 @cindex @code{nil} in keymap | |
920 @code{nil} means that the events used so far in the lookup form an | |
921 undefined key. When a keymap fails to mention an event type at all, and | |
922 has no default binding, that is equivalent to a binding of @code{nil} | |
923 for that event type. | |
924 | |
925 @item @var{keymap} | |
926 @cindex keymap in keymap | |
927 The events used so far in the lookup form a prefix key. The next | |
928 event of the key sequence is looked up in @var{keymap}. | |
929 | |
930 @item @var{command} | |
931 @cindex command in keymap | |
932 The events used so far in the lookup form a complete key, | |
933 and @var{command} is its binding. | |
934 | |
935 @item @var{string} | |
936 @itemx @var{vector} | |
937 @cindex string in keymap | |
938 The events used so far in the lookup form a complete key, whose | |
939 binding is a keyboard macro. See @ref{Keyboard Macros}, for more | |
940 information. | |
941 | |
942 @item @var{list} | |
943 @cindex list in keymap | |
944 The meaning of a list depends on the types of the elements of the list. | |
945 | |
946 @itemize @bullet | |
947 @item | |
948 If the @sc{car} of @var{list} is the symbol @code{keymap}, then the list | |
949 is a keymap, and is treated as a keymap (see above). | |
950 | |
951 @item | |
952 @cindex @code{lambda} in keymap | |
953 If the @sc{car} of @var{list} is @code{lambda}, then the list is a | |
954 lambda expression. This is presumed to be a command, and is treated as | |
955 such (see above). | |
956 | |
957 @item | |
958 If the @sc{car} of @var{list} is a keymap and the @sc{cdr} is an event | |
959 type, then this is an @dfn{indirect entry}: | |
960 | |
961 @example | |
962 (@var{othermap} . @var{othertype}) | |
963 @end example | |
964 | |
965 When key lookup encounters an indirect entry, it looks up instead the | |
966 binding of @var{othertype} in @var{othermap} and uses that. | |
967 | |
968 This feature permits you to define one key as an alias for another key. | |
969 For example, an entry whose @sc{car} is the keymap called @code{esc-map} | |
970 and whose @sc{cdr} is 32 (the code for space) means, ``Use the global | |
971 binding of @kbd{Meta-@key{SPC}}, whatever that may be.'' | |
972 @end itemize | |
973 | |
974 @item @var{symbol} | |
975 @cindex symbol in keymap | |
976 The function definition of @var{symbol} is used in place of | |
977 @var{symbol}. If that too is a symbol, then this process is repeated, | |
978 any number of times. Ultimately this should lead to an object which is | |
979 a keymap, a command or a keyboard macro. A list is allowed if it is a | |
980 keymap or a command, but indirect entries are not understood when found | |
981 via symbols. | |
982 | |
983 Note that keymaps and keyboard macros (strings and vectors) are not | |
984 valid functions, so a symbol with a keymap, string or vector as its | |
985 function definition is also invalid as a function. It is, however, | |
986 valid as a key binding. If the definition is a keyboard macro, then the | |
987 symbol is also valid as an argument to @code{command-execute} | |
988 (@pxref{Interactive Call}). | |
989 | |
990 @cindex @code{undefined} in keymap | |
991 The symbol @code{undefined} is worth special mention: it means to treat | |
992 the key as undefined. Strictly speaking, the key is defined, and its | |
993 binding is the command @code{undefined}; but that command does the same | |
994 thing that is done automatically for an undefined key: it rings the bell | |
995 (by calling @code{ding}) but does not signal an error. | |
996 | |
997 @cindex preventing prefix key | |
998 @code{undefined} is used in local keymaps to override a global key | |
999 binding and make the key ``undefined'' locally. A local binding of | |
1000 @code{nil} would fail to do this because it would not override the | |
1001 global binding. | |
1002 | |
1003 @item @var{anything else} | |
1004 If any other type of object is found, the events used so far in the | |
1005 lookup form a complete key, and the object is its binding, but the | |
1006 binding is not executable as a command. | |
1007 @end table | |
1008 | |
1009 In short, a keymap entry may be a keymap, a command, a keyboard macro, | |
1010 a symbol which leads to one of them, or an indirection or @code{nil}. | |
1011 Here is an example of a sparse keymap with two characters bound to | |
1012 commands and one bound to another keymap. This map is the normal value | |
1013 of @code{emacs-lisp-mode-map}. Note that 9 is the code for @key{TAB}, | |
1014 127 for @key{DEL}, 27 for @key{ESC}, 17 for @kbd{C-q} and 24 for | |
1015 @kbd{C-x}. | |
1016 | |
1017 @example | |
1018 @group | |
1019 (keymap (9 . lisp-indent-line) | |
1020 (127 . backward-delete-char-untabify) | |
1021 (27 keymap (17 . indent-sexp) (24 . eval-defun))) | |
1022 @end group | |
1023 @end example | |
1024 | |
1025 @node Functions for Key Lookup | |
1026 @section Functions for Key Lookup | |
1027 | |
1028 Here are the functions and variables pertaining to key lookup. | |
1029 | |
1030 @defun lookup-key keymap key &optional accept-defaults | |
1031 This function returns the definition of @var{key} in @var{keymap}. If | |
1032 the string or vector @var{key} is not a valid key sequence according to | |
1033 the prefix keys specified in @var{keymap} (which means it is ``too | |
1034 long'' and has extra events at the end), then the value is a number, the | |
1035 number of events at the front of @var{key} that compose a complete key. | |
1036 | |
1037 @c Emacs 19 feature | |
1038 If @var{accept-defaults} is non-@code{nil}, then @code{lookup-key} | |
1039 considers default bindings as well as bindings for the specific events | |
1040 in @var{key}. Otherwise, @code{lookup-key} reports only bindings for | |
1041 the specific sequence @var{key}, ignoring default bindings except when | |
1042 an element of @var{key} is @code{t}. | |
1043 | |
1044 All the other functions described in this chapter that look up keys use | |
1045 @code{lookup-key}. | |
1046 | |
1047 @example | |
1048 @group | |
1049 (lookup-key (current-global-map) "\C-x\C-f") | |
1050 @result{} find-file | |
1051 @end group | |
1052 @group | |
1053 (lookup-key (current-global-map) "\C-x\C-f12345") | |
1054 @result{} 2 | |
1055 @end group | |
1056 @end example | |
1057 | |
1058 If @var{key} contains a meta character, that character is implicitly | |
1059 replaced by a two-character sequence: the value of | |
1060 @code{meta-prefix-char}, followed by the corresponding non-meta | |
1061 character. Thus, the first example below is handled by conversion into | |
1062 the second example. | |
1063 | |
1064 @example | |
1065 @group | |
1066 (lookup-key (current-global-map) "\M-f") | |
1067 @result{} forward-word | |
1068 @end group | |
1069 @group | |
1070 (lookup-key (current-global-map) "\ef") | |
1071 @result{} forward-word | |
1072 @end group | |
1073 @end example | |
1074 | |
1075 Unlike @code{read-key-sequence}, this function does not modify the | |
1076 specified events in ways that discard information (@pxref{Key Sequence | |
1077 Input}). In particular, it does not convert letters to lower case and | |
1078 it does not change drag events to clicks. | |
1079 @end defun | |
1080 | |
1081 @deffn Command undefined | |
1082 Used in keymaps to undefine keys. It calls @code{ding}, but does | |
1083 not cause an error. | |
1084 @end deffn | |
1085 | |
1086 @defun key-binding key &optional accept-defaults | |
1087 This function returns the binding for @var{key} in the current | |
1088 keymaps, trying all the active keymaps. The result is @code{nil} if | |
1089 @var{key} is undefined in the keymaps. | |
1090 | |
1091 @c Emacs 19 feature | |
1092 The argument @var{accept-defaults} controls checking for default | |
1093 bindings, as in @code{lookup-key}. | |
1094 | |
1095 An error is signaled if @var{key} is not a string or a vector. | |
1096 | |
1097 @example | |
1098 @group | |
1099 (key-binding "\C-x\C-f") | |
1100 @result{} find-file | |
1101 @end group | |
1102 @end example | |
1103 @end defun | |
1104 | |
1105 @defun local-key-binding key &optional accept-defaults | |
1106 This function returns the binding for @var{key} in the current | |
1107 local keymap, or @code{nil} if it is undefined there. | |
1108 | |
1109 @c Emacs 19 feature | |
1110 The argument @var{accept-defaults} controls checking for default bindings, | |
1111 as in @code{lookup-key} (above). | |
1112 @end defun | |
1113 | |
1114 @defun global-key-binding key &optional accept-defaults | |
1115 This function returns the binding for command @var{key} in the | |
1116 current global keymap, or @code{nil} if it is undefined there. | |
1117 | |
1118 @c Emacs 19 feature | |
1119 The argument @var{accept-defaults} controls checking for default bindings, | |
1120 as in @code{lookup-key} (above). | |
1121 @end defun | |
1122 | |
1123 @c Emacs 19 feature | |
1124 @defun minor-mode-key-binding key &optional accept-defaults | |
1125 This function returns a list of all the active minor mode bindings of | |
1126 @var{key}. More precisely, it returns an alist of pairs | |
1127 @code{(@var{modename} . @var{binding})}, where @var{modename} is the the | |
1128 variable which enables the minor mode, and @var{binding} is @var{key}'s | |
1129 binding in that mode. If @var{key} has no minor-mode bindings, the | |
1130 value is @code{nil}. | |
1131 | |
1132 If the first binding is not a prefix command, all subsequent bindings | |
1133 from other minor modes are omitted, since they would be completely | |
1134 shadowed. Similarly, the list omits non-prefix bindings that follow | |
1135 prefix bindings. | |
1136 | |
1137 The argument @var{accept-defaults} controls checking for default | |
1138 bindings, as in @code{lookup-key} (above). | |
1139 @end defun | |
1140 | |
1141 @defvar meta-prefix-char | |
1142 @cindex @key{ESC} | |
1143 This variable is the meta-prefix character code. It is used when | |
1144 translating a meta character to a two-character sequence so it can be | |
1145 looked up in a keymap. For useful results, the value should be a prefix | |
1146 event (@pxref{Prefix Keys}). The default value is 27, which is the | |
1147 @sc{ASCII} code for @key{ESC}. | |
1148 | |
1149 As long as the value of @code{meta-prefix-char} remains 27, key | |
1150 lookup translates @kbd{M-b} into @kbd{@key{ESC} b}, which is normally | |
1151 defined as the @code{backward-word} command. However, if you set | |
1152 @code{meta-prefix-char} to 24, the code for @kbd{C-x}, then Emacs will | |
1153 translate @kbd{M-b} into @kbd{C-x b}, whose standard binding is the | |
1154 @code{switch-to-buffer} command. | |
1155 | |
1156 @smallexample | |
1157 @group | |
1158 meta-prefix-char ; @r{The default value.} | |
1159 @result{} 27 | |
1160 @end group | |
1161 @group | |
1162 (key-binding "\M-b") | |
1163 @result{} backward-word | |
1164 @end group | |
1165 @group | |
1166 ?\C-x ; @r{The print representation} | |
1167 @result{} 24 ; @r{of a character.} | |
1168 @end group | |
1169 @group | |
1170 (setq meta-prefix-char 24) | |
1171 @result{} 24 | |
1172 @end group | |
1173 @group | |
1174 (key-binding "\M-b") | |
1175 @result{} switch-to-buffer ; @r{Now, typing @kbd{M-b} is} | |
1176 ; @r{like typing @kbd{C-x b}.} | |
1177 | |
1178 (setq meta-prefix-char 27) ; @r{Avoid confusion!} | |
1179 @result{} 27 ; @r{Restore the default value!} | |
1180 @end group | |
1181 @end smallexample | |
1182 @end defvar | |
1183 | |
1184 @node Changing Key Bindings | |
1185 @section Changing Key Bindings | |
1186 @cindex changing key bindings | |
1187 @cindex rebinding | |
1188 | |
1189 The way to rebind a key is to change its entry in a keymap. If you | |
1190 change the global keymap, the change is effective in all buffers (except | |
1191 those that override the global binding with a local one). If you change | |
1192 the current buffer's local map, that usually affects all buffers using | |
1193 the same major mode. The @code{global-set-key} and @code{local-set-key} | |
1194 functions are convenient interfaces for these operations. Or you can | |
1195 use @code{define-key} and specify explicitly which map to change. | |
1196 | |
1197 People often use @code{global-set-key} in their @file{.emacs} file for | |
1198 simple customization. For example, | |
1199 | |
1200 @smallexample | |
1201 (global-set-key "\C-x\C-\\" 'next-line) | |
1202 @end smallexample | |
1203 | |
1204 @noindent | |
1205 or | |
1206 | |
1207 @smallexample | |
1208 (global-set-key [?\C-x ?\C-\\] 'next-line) | |
1209 @end smallexample | |
1210 | |
1211 @noindent | |
1212 redefines @kbd{C-x C-\} to move down a line. | |
1213 | |
1214 @smallexample | |
1215 (global-set-key [M-mouse-1] 'mouse-set-point) | |
1216 @end smallexample | |
1217 | |
1218 @noindent | |
1219 redefines the first (leftmost) mouse button, typed with the Meta key, to | |
1220 set point where you click. | |
1221 | |
1222 @cindex meta character key constants | |
1223 @cindex control character key constants | |
1224 In writing the key sequence to rebind, it is useful to use the special | |
1225 escape sequences for control and meta characters (@pxref{String Type}). | |
1226 The syntax @samp{\C-} means that the following character is a control | |
1227 character and @samp{\M-} means that the following character is a meta | |
1228 character. Thus, the string @code{"\M-x"} is read as containing a | |
1229 single @kbd{M-x}, @code{"\C-f"} is read as containing a single | |
1230 @kbd{C-f}, and @code{"\M-\C-x"} and @code{"\C-\M-x"} are both read as | |
1231 containing a single @kbd{C-M-x}. | |
1232 | |
1233 For the functions below, an error is signaled if @var{keymap} is not a | |
1234 keymap or if @var{key} is not a string or vector representing a key | |
1235 sequence. You can use event types (symbols) as shorthand for events | |
1236 that are lists. | |
1237 | |
1238 @defun define-key keymap key binding | |
1239 This function sets the binding for @var{key} in @var{keymap}. (If | |
1240 @var{key} is more than one event long, the change is actually made | |
1241 in another keymap reached from @var{keymap}.) The argument | |
1242 @var{binding} can be any Lisp object, but only certain types are | |
1243 meaningful. (For a list of meaningful types, see @ref{Key Lookup}.) | |
1244 The value returned by @code{define-key} is @var{binding}. | |
1245 | |
1246 @cindex invalid prefix key error | |
1247 @cindex key sequence error | |
1248 Every prefix of @var{key} must be a prefix key (i.e., bound to a | |
1249 keymap) or undefined; otherwise an error is signaled. | |
1250 | |
1251 If some prefix of @var{key} is undefined, then @code{define-key} defines | |
1252 it as a prefix key so that the rest of @var{key} may be defined as | |
1253 specified. | |
1254 @end defun | |
1255 | |
1256 This example creates a sparse keymap and makes a number of bindings: | |
1257 | |
1258 @smallexample | |
1259 @group | |
1260 (setq map (make-sparse-keymap)) | |
1261 @result{} (keymap) | |
1262 @end group | |
1263 @group | |
1264 (define-key map "\C-f" 'forward-char) | |
1265 @result{} forward-char | |
1266 @end group | |
1267 @group | |
1268 map | |
1269 @result{} (keymap (6 . forward-char)) | |
1270 @end group | |
1271 | |
1272 @group | |
1273 ;; @r{Build sparse submap for @kbd{C-x} and bind @kbd{f} in that.} | |
1274 (define-key map "\C-xf" 'forward-word) | |
1275 @result{} forward-word | |
1276 @end group | |
1277 @group | |
1278 map | |
1279 @result{} (keymap | |
1280 (24 keymap ; @kbd{C-x} | |
1281 (102 . forward-word)) ; @kbd{f} | |
1282 (6 . forward-char)) ; @kbd{C-f} | |
1283 @end group | |
1284 | |
1285 @group | |
1286 ;; @r{Bind @kbd{C-p} to the @code{ctl-x-map}.} | |
1287 (define-key map "\C-p" ctl-x-map) | |
1288 ;; @code{ctl-x-map} | |
1289 @result{} [nil @dots{} find-file @dots{} backward-kill-sentence] | |
1290 @end group | |
1291 | |
1292 @group | |
1293 ;; @r{Bind @kbd{C-f} to @code{foo} in the @code{ctl-x-map}.} | |
1294 (define-key map "\C-p\C-f" 'foo) | |
1295 @result{} 'foo | |
1296 @end group | |
1297 @group | |
1298 map | |
1299 @result{} (keymap ; @r{Note @code{foo} in @code{ctl-x-map}.} | |
1300 (16 keymap [nil @dots{} foo @dots{} backward-kill-sentence]) | |
1301 (24 keymap | |
1302 (102 . forward-word)) | |
1303 (6 . forward-char)) | |
1304 @end group | |
1305 @end smallexample | |
1306 | |
1307 @noindent | |
1308 Note that storing a new binding for @kbd{C-p C-f} actually works by | |
1309 changing an entry in @code{ctl-x-map}, and this has the effect of | |
1310 changing the bindings of both @kbd{C-p C-f} and @kbd{C-x C-f} in the | |
1311 default global map. | |
1312 | |
1313 @defun substitute-key-definition olddef newdef keymap &optional oldmap | |
1314 @cindex replace bindings | |
1315 This function replaces @var{olddef} with @var{newdef} for any keys in | |
1316 @var{keymap} that were bound to @var{olddef}. In other words, | |
1317 @var{olddef} is replaced with @var{newdef} wherever it appears. The | |
1318 function returns @code{nil}. | |
1319 | |
1320 For example, this redefines @kbd{C-x C-f}, if you do it in an Emacs with | |
1321 standard bindings: | |
1322 | |
1323 @smallexample | |
1324 @group | |
1325 (substitute-key-definition | |
1326 'find-file 'find-file-read-only (current-global-map)) | |
1327 @end group | |
1328 @end smallexample | |
1329 | |
1330 @c Emacs 19 feature | |
1331 If @var{oldmap} is non-@code{nil}, then its bindings determine which | |
1332 keys to rebind. The rebindings still happen in @var{newmap}, not in | |
1333 @var{oldmap}. Thus, you can change one map under the control of the | |
1334 bindings in another. For example, | |
1335 | |
1336 @smallexample | |
1337 (substitute-key-definition | |
1338 'delete-backward-char 'my-funny-delete | |
1339 my-map global-map) | |
1340 @end smallexample | |
1341 | |
1342 @noindent | |
1343 puts the special deletion command in @code{my-map} for whichever keys | |
1344 are globally bound to the standard deletion command. | |
1345 | |
1346 @ignore | |
1347 @c Emacs 18 only | |
1348 Prefix keymaps that appear within @var{keymap} are not checked | |
1349 recursively for keys bound to @var{olddef}; they are not changed at all. | |
1350 Perhaps it would be better to check nested keymaps recursively. | |
1351 @end ignore | |
1352 | |
1353 Here is an example showing a keymap before and after substitution: | |
1354 | |
1355 @smallexample | |
1356 @group | |
1357 (setq map '(keymap | |
1358 (?1 . olddef-1) | |
1359 (?2 . olddef-2) | |
1360 (?3 . olddef-1))) | |
1361 @result{} (keymap (49 . olddef-1) (50 . olddef-2) (51 . olddef-1)) | |
1362 @end group | |
1363 | |
1364 @group | |
1365 (substitute-key-definition 'olddef-1 'newdef map) | |
1366 @result{} nil | |
1367 @end group | |
1368 @group | |
1369 map | |
1370 @result{} (keymap (49 . newdef) (50 . olddef-2) (51 . newdef)) | |
1371 @end group | |
1372 @end smallexample | |
1373 @end defun | |
1374 | |
1375 @defun suppress-keymap keymap &optional nodigits | |
1376 @cindex @code{self-insert-command} override | |
1377 This function changes the contents of the full keymap @var{keymap} by | |
1378 making all the printing characters undefined. More precisely, it binds | |
1379 them to the command @code{undefined}. This makes ordinary insertion of | |
1380 text impossible. @code{suppress-keymap} returns @code{nil}. | |
1381 | |
1382 If @var{nodigits} is @code{nil}, then @code{suppress-keymap} defines | |
1383 digits to run @code{digit-argument}, and @kbd{-} to run | |
1384 @code{negative-argument}. Otherwise it makes them undefined like the | |
1385 rest of the printing characters. | |
1386 | |
1387 @cindex yank suppression | |
1388 @cindex @code{quoted-insert} suppression | |
1389 The @code{suppress-keymap} function does not make it impossible to | |
1390 modify a buffer, as it does not suppress commands such as @code{yank} | |
1391 and @code{quoted-insert}. To prevent any modification of a buffer, make | |
1392 it read-only (@pxref{Read Only Buffers}). | |
1393 | |
1394 Since this function modifies @var{keymap}, you would normally use it | |
1395 on a newly created keymap. Operating on an existing keymap | |
1396 that is used for some other purpose is likely to cause trouble; for | |
1397 example, suppressing @code{global-map} would make it impossible to use | |
1398 most of Emacs. | |
1399 | |
1400 Most often, @code{suppress-keymap} is used to initialize local | |
1401 keymaps of modes such as Rmail and Dired where insertion of text is not | |
1402 desirable and the buffer is read-only. Here is an example taken from | |
1403 the file @file{emacs/lisp/dired.el}, showing how the local keymap for | |
1404 Dired mode is set up: | |
1405 | |
1406 @smallexample | |
1407 @group | |
1408 @dots{} | |
1409 (setq dired-mode-map (make-keymap)) | |
1410 (suppress-keymap dired-mode-map) | |
1411 (define-key dired-mode-map "r" 'dired-rename-file) | |
1412 (define-key dired-mode-map "\C-d" 'dired-flag-file-deleted) | |
1413 (define-key dired-mode-map "d" 'dired-flag-file-deleted) | |
1414 (define-key dired-mode-map "v" 'dired-view-file) | |
1415 (define-key dired-mode-map "e" 'dired-find-file) | |
1416 (define-key dired-mode-map "f" 'dired-find-file) | |
1417 @dots{} | |
1418 @end group | |
1419 @end smallexample | |
1420 @end defun | |
1421 | |
1422 @node Key Binding Commands | |
1423 @section Commands for Binding Keys | |
1424 | |
1425 This section describes some convenient interactive interfaces for | |
1426 changing key bindings. They work by calling @code{define-key}. | |
1427 | |
1428 @deffn Command global-set-key key definition | |
1429 This function sets the binding of @var{key} in the current global map | |
1430 to @var{definition}. | |
1431 | |
1432 @smallexample | |
1433 @group | |
1434 (global-set-key @var{key} @var{definition}) | |
1435 @equiv{} | |
1436 (define-key (current-global-map) @var{key} @var{definition}) | |
1437 @end group | |
1438 @end smallexample | |
1439 @end deffn | |
1440 | |
1441 @deffn Command global-unset-key key | |
1442 @cindex unbinding keys | |
1443 This function removes the binding of @var{key} from the current | |
1444 global map. | |
1445 | |
1446 One use of this function is in preparation for defining a longer key | |
1447 which uses it implicitly as a prefix---which would not be allowed if | |
1448 @var{key} has a non-prefix binding. For example: | |
1449 | |
1450 @smallexample | |
1451 @group | |
1452 (global-unset-key "\C-l") | |
1453 @result{} nil | |
1454 @end group | |
1455 @group | |
1456 (global-set-key "\C-l\C-l" 'redraw-display) | |
1457 @result{} nil | |
1458 @end group | |
1459 @end smallexample | |
1460 | |
1461 This function is implemented simply using @code{define-key}: | |
1462 | |
1463 @smallexample | |
1464 @group | |
1465 (global-unset-key @var{key}) | |
1466 @equiv{} | |
1467 (define-key (current-global-map) @var{key} nil) | |
1468 @end group | |
1469 @end smallexample | |
1470 @end deffn | |
1471 | |
1472 @deffn Command local-set-key key definition | |
1473 This function sets the binding of @var{key} in the current local | |
1474 keymap to @var{definition}. | |
1475 | |
1476 @smallexample | |
1477 @group | |
1478 (local-set-key @var{key} @var{definition}) | |
1479 @equiv{} | |
1480 (define-key (current-local-map) @var{key} @var{definition}) | |
1481 @end group | |
1482 @end smallexample | |
1483 @end deffn | |
1484 | |
1485 @deffn Command local-unset-key key | |
1486 This function removes the binding of @var{key} from the current | |
1487 local map. | |
1488 | |
1489 @smallexample | |
1490 @group | |
1491 (local-unset-key @var{key}) | |
1492 @equiv{} | |
1493 (define-key (current-local-map) @var{key} nil) | |
1494 @end group | |
1495 @end smallexample | |
1496 @end deffn | |
1497 | |
1498 @node Scanning Keymaps | |
1499 @section Scanning Keymaps | |
1500 | |
1501 This section describes functions used to scan all the current keymaps | |
1502 for the sake of printing help information. | |
1503 | |
1504 @defun accessible-keymaps keymap &optional prefix | |
1505 This function returns a list of all the keymaps that can be accessed | |
1506 (via prefix keys) from @var{keymap}. The value is an association list | |
1507 with elements of the form @code{(@var{key} .@: @var{map})}, where | |
1508 @var{key} is a prefix key whose definition in @var{keymap} is | |
1509 @var{map}. | |
1510 | |
1511 The elements of the alist are ordered so that the @var{key} increases | |
1512 in length. The first element is always @code{("" .@: @var{keymap})}, | |
1513 because the specified keymap is accessible from itself with a prefix of | |
1514 no events. | |
1515 | |
1516 If @var{prefix} is given, it should be a prefix key sequence; then | |
1517 @code{accessible-keymaps} includes only the submaps whose prefixes start | |
1518 with @var{prefix}. These elements look just as they do in the value of | |
1519 @code{(accessible-keymaps)}; the only difference is that some elements | |
1520 are omitted. | |
1521 | |
1522 In the example below, the returned alist indicates that the key | |
1523 @key{ESC}, which is displayed as @samp{^[}, is a prefix key whose | |
1524 definition is the sparse keymap @code{(keymap (83 .@: center-paragraph) | |
1525 (115 .@: foo))}. | |
1526 | |
1527 @smallexample | |
1528 @group | |
1529 (accessible-keymaps (current-local-map)) | |
1530 @result{}(("" keymap | |
1531 (27 keymap ; @r{Note this keymap for @key{ESC} is repeated below.} | |
1532 (83 . center-paragraph) | |
1533 (115 . center-line)) | |
1534 (9 . tab-to-tab-stop)) | |
1535 @end group | |
1536 | |
1537 @group | |
1538 ("^[" keymap | |
1539 (83 . center-paragraph) | |
1540 (115 . foo))) | |
1541 @end group | |
1542 @end smallexample | |
1543 | |
1544 In the following example, @kbd{C-h} is a prefix key that uses a sparse | |
1545 keymap starting with @code{(keymap (118 . describe-variable)@dots{})}. | |
1546 Another prefix, @kbd{C-x 4}, uses a keymap which happens to be | |
1547 @code{ctl-x-4-map}. The event @code{mode-line} is one of several dummy | |
1548 events used as prefixes for mouse actions in special parts of a window. | |
1549 | |
1550 @smallexample | |
1551 @group | |
1552 (accessible-keymaps (current-global-map)) | |
1553 @result{} (("" keymap [set-mark-command beginning-of-line @dots{} | |
1554 delete-backward-char]) | |
1555 @end group | |
1556 @group | |
1557 ("^H" keymap (118 . describe-variable) @dots{} | |
1558 (8 . help-for-help)) | |
1559 @end group | |
1560 @group | |
1561 ("^X" keymap [x-flush-mouse-queue @dots{} | |
1562 backward-kill-sentence]) | |
1563 @end group | |
1564 @group | |
1565 ("^[" keymap [mark-sexp backward-sexp @dots{} | |
1566 backward-kill-word]) | |
1567 @end group | |
1568 ("^X4" keymap (15 . display-buffer) @dots{}) | |
1569 @group | |
1570 ([mode-line] keymap | |
1571 (S-mouse-2 . mouse-split-window-horizontally) @dots{})) | |
1572 @end group | |
1573 @end smallexample | |
1574 | |
1575 @noindent | |
1576 These are not all the keymaps you would see in an actual case. | |
1577 @end defun | |
1578 | |
1579 @defun where-is-internal command &optional keymap firstonly noindirect | |
1580 This function returns a list of key sequences (of any length) that are | |
1581 bound to @var{command} in a set of keymaps. | |
1582 | |
1583 The argument @var{command} can be any object; it is compared with all | |
1584 keymap entries using @code{eq}. | |
1585 | |
1586 If @var{keymap} is @code{nil}, then the maps used are the current active | |
1587 keymaps, disregarding @code{overriding-local-map} (that is, pretending | |
1588 its value is @code{nil}). If @var{keymap} is non-@code{nil}, then the | |
1589 maps searched are @var{keymap} and the global keymap. | |
1590 | |
1591 Usually it's best to use @code{overriding-local-map} as the expression | |
1592 for @var{keymap}. Then @code{where-is-internal} searches precisely the | |
1593 keymaps that are active. To search only the global map, pass | |
1594 @code{(keymap)} (an empty keymap) as @var{keymap}. | |
1595 | |
1596 If @var{firstonly} is @code{non-ascii}, then the value is a single | |
1597 string representing the first key sequence found, rather than a list of | |
1598 all possible key sequences. If @var{firstonly} is @code{t}, then the | |
1599 value is the first key sequence, except that key sequences consisting | |
1600 entirely of @sc{ASCII} characters (or meta variants of @sc{ASCII} | |
1601 characters) are preferred to all other key sequences. | |
1602 | |
1603 If @var{noindirect} is non-@code{nil}, @code{where-is-internal} doesn't | |
1604 follow indirections to other keymaps or slots. This makes it possible | |
1605 to search for an indirect definition itself. | |
1606 | |
1607 This function is used by @code{where-is} (@pxref{Help, , Help, emacs, | |
1608 The GNU Emacs Manual}). | |
1609 | |
1610 @smallexample | |
1611 @group | |
1612 (where-is-internal 'describe-function) | |
1613 @result{} ("\^hf" "\^hd") | |
1614 @end group | |
1615 @end smallexample | |
1616 @end defun | |
1617 | |
1618 @deffn Command describe-bindings prefix | |
1619 This function creates a listing of all defined keys, and their | |
1620 definitions. It writes the listing in a buffer named @samp{*Help*} and | |
1621 displays it in a window. | |
1622 | |
1623 The listing describes meta characters as @key{ESC} followed by the | |
1624 corresponding non-meta character. | |
1625 | |
1626 When several characters with consecutive @sc{ASCII} codes have the | |
1627 same definition, they are shown together, as | |
1628 @samp{@var{firstchar}..@var{lastchar}}. In this instance, you need to | |
1629 know the @sc{ASCII} codes to understand which characters this means. | |
1630 For example, in the default global map, the characters @samp{@key{SPC} | |
1631 ..@: ~} are described by a single line. @key{SPC} is @sc{ASCII} 32, | |
1632 @kbd{~} is @sc{ASCII} 126, and the characters between them include all | |
1633 the normal printing characters, (e.g., letters, digits, punctuation, | |
1634 etc.@:); all these characters are bound to @code{self-insert-command}. | |
1635 | |
1636 If @var{prefix} is non-@code{nil}, it should be a prefix key; then the | |
1637 listing includes only keys that start with @var{prefix}. | |
1638 @end deffn |