* keymap.c (access_keymap): Don't forget to QUIT while scanning
[bpt/emacs.git] / src / keymap.c
1 /* Manipulation of keymaps
2 Copyright (C) 1985, 1986, 1987, 1988, 1992 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21 #include "config.h"
22 #include <stdio.h>
23 #undef NULL
24 #include "lisp.h"
25 #include "commands.h"
26 #include "buffer.h"
27 #include "keyboard.h"
28
29 #define min(a, b) ((a) < (b) ? (a) : (b))
30
31 /* The number of elements in keymap vectors. */
32 #define DENSE_TABLE_SIZE (0200)
33
34 /* Actually allocate storage for these variables */
35
36 Lisp_Object current_global_map; /* Current global keymap */
37
38 Lisp_Object global_map; /* default global key bindings */
39
40 Lisp_Object meta_map; /* The keymap used for globally bound
41 ESC-prefixed default commands */
42
43 Lisp_Object control_x_map; /* The keymap used for globally bound
44 C-x-prefixed default commands */
45
46 /* was MinibufLocalMap */
47 Lisp_Object Vminibuffer_local_map;
48 /* The keymap used by the minibuf for local
49 bindings when spaces are allowed in the
50 minibuf */
51
52 /* was MinibufLocalNSMap */
53 Lisp_Object Vminibuffer_local_ns_map;
54 /* The keymap used by the minibuf for local
55 bindings when spaces are not encouraged
56 in the minibuf */
57
58 /* keymap used for minibuffers when doing completion */
59 /* was MinibufLocalCompletionMap */
60 Lisp_Object Vminibuffer_local_completion_map;
61
62 /* keymap used for minibuffers when doing completion and require a match */
63 /* was MinibufLocalMustMatchMap */
64 Lisp_Object Vminibuffer_local_must_match_map;
65
66 /* Alist of minor mode variables and keymaps. */
67 Lisp_Object Vminor_mode_map_alist;
68
69 /* Keymap mapping ASCII function key sequences onto their preferred forms.
70 Initialized by the terminal-specific lisp files. See DEFVAR for more
71 documentation. */
72 Lisp_Object Vfunction_key_map;
73
74 Lisp_Object Qkeymapp, Qkeymap;
75
76 /* A char over 0200 in a key sequence
77 is equivalent to prefixing with this character. */
78
79 extern Lisp_Object meta_prefix_char;
80
81 void describe_map_tree ();
82 static Lisp_Object describe_buffer_bindings ();
83 static void describe_command ();
84 static void describe_map ();
85 static void describe_map_2 ();
86 \f
87 /* Keymap object support - constructors and predicates. */
88
89 DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 1, 0,
90 "Construct and return a new keymap, of the form (keymap VECTOR . ALIST).\n\
91 VECTOR is a 128-element vector which holds the bindings for the ASCII\n\
92 characters. ALIST is an assoc-list which holds bindings for function keys,\n\
93 mouse events, and any other things that appear in the input stream.\n\
94 All entries in it are initially nil, meaning \"command undefined\".\n\n\
95 The optional arg STRING supplies a menu name for the keymap\n\
96 in case you use it as a menu with `x-popup-menu'.")
97 (string)
98 Lisp_Object string;
99 {
100 Lisp_Object tail;
101 if (!NILP (string))
102 tail = Fcons (string, Qnil);
103 else
104 tail = Qnil;
105 return Fcons (Qkeymap,
106 Fcons (Fmake_vector (make_number (DENSE_TABLE_SIZE), Qnil),
107 tail));
108 }
109
110 DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 1, 0,
111 "Construct and return a new sparse-keymap list.\n\
112 Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),\n\
113 which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),\n\
114 which binds the function key or mouse event SYMBOL to DEFINITION.\n\
115 Initially the alist is nil.\n\n\
116 The optional arg STRING supplies a menu name for the keymap\n\
117 in case you use it as a menu with `x-popup-menu'.")
118 (string)
119 Lisp_Object string;
120 {
121 if (!NILP (string))
122 return Fcons (Qkeymap, Fcons (string, Qnil));
123 return Fcons (Qkeymap, Qnil);
124 }
125
126 /* This function is used for installing the standard key bindings
127 at initialization time.
128
129 For example:
130
131 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark");
132
133 I haven't extended these to allow the initializing code to bind
134 function keys and mouse events; since they are called by many files,
135 I'd have to fix lots of callers, and nobody right now would be using
136 the new functionality, so it seems like a waste of time. But there's
137 no technical reason not to. -JimB */
138
139 void
140 initial_define_key (keymap, key, defname)
141 Lisp_Object keymap;
142 int key;
143 char *defname;
144 {
145 store_in_keymap (keymap, make_number (key), intern (defname));
146 }
147
148 /* Define character fromchar in map frommap as an alias for character
149 tochar in map tomap. Subsequent redefinitions of the latter WILL
150 affect the former. */
151
152 #if 0
153 void
154 synkey (frommap, fromchar, tomap, tochar)
155 struct Lisp_Vector *frommap, *tomap;
156 int fromchar, tochar;
157 {
158 Lisp_Object v, c;
159 XSET (v, Lisp_Vector, tomap);
160 XFASTINT (c) = tochar;
161 frommap->contents[fromchar] = Fcons (v, c);
162 }
163 #endif /* 0 */
164
165 DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0,
166 "Return t if ARG is a keymap.\n\
167 \n\
168 A keymap is list (keymap . ALIST), a list (keymap VECTOR . ALIST),\n\
169 or a symbol whose function definition is a keymap is itself a keymap.\n\
170 ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);\n\
171 VECTOR is a 128-element vector of bindings for ASCII characters.")
172 (object)
173 Lisp_Object object;
174 {
175 return (NILP (get_keymap_1 (object, 0)) ? Qnil : Qt);
176 }
177
178 /* Check that OBJECT is a keymap (after dereferencing through any
179 symbols). If it is, return it; otherwise, return nil, or signal an
180 error if ERROR != 0. */
181 Lisp_Object
182 get_keymap_1 (object, error)
183 Lisp_Object object;
184 int error;
185 {
186 register Lisp_Object tem;
187
188 tem = indirect_function (object);
189 if (CONSP (tem) && EQ (XCONS (tem)->car, Qkeymap))
190 return tem;
191
192 if (error)
193 wrong_type_argument (Qkeymapp, object);
194 else
195 return Qnil;
196 }
197
198 Lisp_Object
199 get_keymap (object)
200 Lisp_Object object;
201 {
202 return get_keymap_1 (object, 1);
203 }
204
205
206 /* Look up IDX in MAP. IDX may be any sort of event.
207 Note that this does only one level of lookup; IDX must be a single
208 event, not a sequence. */
209
210 Lisp_Object
211 access_keymap (map, idx)
212 Lisp_Object map;
213 Lisp_Object idx;
214 {
215 /* If idx is a list (some sort of mouse click, perhaps?),
216 the index we want to use is the car of the list, which
217 ought to be a symbol. */
218 if (EVENT_HAS_PARAMETERS (idx))
219 idx = EVENT_HEAD (idx);
220
221 if (XTYPE (idx) == Lisp_Int
222 && (XINT (idx) < 0 || XINT (idx) >= DENSE_TABLE_SIZE))
223 error ("only ASCII characters may used as keymap indices");
224
225 /* If idx is a symbol, it might have modifiers, which need to
226 be put in the canonical order. */
227 else if (XTYPE (idx) == Lisp_Symbol)
228 idx = reorder_modifiers (idx);
229
230 {
231 Lisp_Object tail;
232
233 for (tail = map; CONSP (tail); tail = XCONS (tail)->cdr)
234 {
235 Lisp_Object binding = XCONS (tail)->car;
236
237 switch (XTYPE (binding))
238 {
239 case Lisp_Cons:
240 if (EQ (XCONS (binding)->car, idx))
241 return XCONS (binding)->cdr;
242 break;
243
244 case Lisp_Vector:
245 if (XVECTOR (binding)->size == DENSE_TABLE_SIZE
246 && XTYPE (idx) == Lisp_Int)
247 return XVECTOR (binding)->contents[XINT (idx)];
248 break;
249 }
250
251 QUIT;
252 }
253 }
254
255 return Qnil;
256 }
257
258 /* Given OBJECT which was found in a slot in a keymap,
259 trace indirect definitions to get the actual definition of that slot.
260 An indirect definition is a list of the form
261 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
262 and INDEX is the object to look up in KEYMAP to yield the definition.
263
264 Also if OBJECT has a menu string as the first element,
265 remove that. Also remove a menu help string as second element. */
266
267 Lisp_Object
268 get_keyelt (object)
269 register Lisp_Object object;
270 {
271 while (1)
272 {
273 register Lisp_Object map, tem;
274
275 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
276 map = get_keymap_1 (Fcar_safe (object), 0);
277 tem = Fkeymapp (map);
278 if (!NILP (tem))
279 object = access_keymap (map, Fcdr (object));
280
281 /* If the keymap contents looks like (STRING . DEFN),
282 use DEFN.
283 Keymap alist elements like (CHAR MENUSTRING . DEFN)
284 will be used by HierarKey menus. */
285 else if (XTYPE (object) == Lisp_Cons
286 && XTYPE (XCONS (object)->car) == Lisp_String)
287 {
288 object = XCONS (object)->cdr;
289 /* Also remove a menu help string, if any,
290 following the menu item name. */
291 if (XTYPE (object) == Lisp_Cons
292 && XTYPE (XCONS (object)->car) == Lisp_String)
293 object = XCONS (object)->cdr;
294 }
295
296 else
297 /* Anything else is really the value. */
298 return object;
299 }
300 }
301
302 Lisp_Object
303 store_in_keymap (keymap, idx, def)
304 Lisp_Object keymap;
305 register Lisp_Object idx;
306 register Lisp_Object def;
307 {
308 if (XTYPE (keymap) != Lisp_Cons
309 || ! EQ (XCONS (keymap)->car, Qkeymap))
310 error ("attempt to define a key in a non-keymap");
311
312 /* If idx is a list (some sort of mouse click, perhaps?),
313 the index we want to use is the car of the list, which
314 ought to be a symbol. */
315 if (EVENT_HAS_PARAMETERS (idx))
316 idx = EVENT_HEAD (idx);
317
318 if (XTYPE (idx) == Lisp_Int
319 && (XINT (idx) < 0 || XINT (idx) >= DENSE_TABLE_SIZE))
320 error ("only ASCII characters may be used as keymap indices");
321
322 /* If idx is a symbol, it might have modifiers, which need to
323 be put in the canonical order. */
324 else if (XTYPE (idx) == Lisp_Symbol)
325 idx = reorder_modifiers (idx);
326
327
328 /* Scan the keymap for a binding of idx. */
329 {
330 Lisp_Object tail;
331
332 /* The cons after which we should insert new bindings. If the
333 keymap has a table element, we record its position here, so new
334 bindings will go after it; this way, the table will stay
335 towards the front of the alist and character lookups in dense
336 keymaps will remain fast. Otherwise, this just points at the
337 front of the keymap. */
338 Lisp_Object insertion_point = keymap;
339
340 for (tail = XCONS (keymap)->cdr; CONSP (tail); tail = XCONS (tail)->cdr)
341 {
342 Lisp_Object elt = XCONS (tail)->car;
343
344 switch (XTYPE (elt))
345 {
346 case Lisp_Vector:
347 if (XTYPE (idx) == Lisp_Int)
348 {
349 XVECTOR (elt)->contents[XFASTINT (idx)] = def;
350 return def;
351 }
352 insertion_point = tail;
353 break;
354
355 case Lisp_Cons:
356 if (EQ (idx, XCONS (elt)->car))
357 {
358 XCONS (elt)->cdr = def;
359 return def;
360 }
361 break;
362
363 case Lisp_Symbol:
364 /* If we find a 'keymap' symbol in the spine of KEYMAP,
365 then we must have found the start of a second keymap
366 being used as the tail of KEYMAP, and a binding for IDX
367 should be inserted before it. */
368 if (EQ (elt, Qkeymap))
369 goto keymap_end;
370 break;
371 }
372 }
373
374 keymap_end:
375 /* We have scanned the entire keymap, and not found a binding for
376 IDX. Let's add one. */
377 XCONS (insertion_point)->cdr =
378 Fcons (Fcons (idx, def), XCONS (insertion_point)->cdr);
379 }
380
381 return def;
382 }
383
384
385 DEFUN ("copy-keymap", Fcopy_keymap, Scopy_keymap, 1, 1, 0,
386 "Return a copy of the keymap KEYMAP.\n\
387 The copy starts out with the same definitions of KEYMAP,\n\
388 but changing either the copy or KEYMAP does not affect the other.\n\
389 Any key definitions that are subkeymaps are recursively copied.\n\
390 However, a key definition which is a symbol whose definition is a keymap\n\
391 is not copied.")
392 (keymap)
393 Lisp_Object keymap;
394 {
395 register Lisp_Object copy, tail;
396
397 copy = Fcopy_alist (get_keymap (keymap));
398
399 for (tail = copy; CONSP (tail); tail = XCONS (tail)->cdr)
400 {
401 Lisp_Object elt = XCONS (tail)->car;
402
403 if (XTYPE (elt) == Lisp_Vector
404 && XVECTOR (elt)->size == DENSE_TABLE_SIZE)
405 {
406 int i;
407
408 elt = Fcopy_sequence (elt);
409 XCONS (tail)->car = elt;
410
411 for (i = 0; i < DENSE_TABLE_SIZE; i++)
412 if (XTYPE (XVECTOR (elt)->contents[i]) != Lisp_Symbol
413 && Fkeymapp (XVECTOR (elt)->contents[i]))
414 XVECTOR (elt)->contents[i] =
415 Fcopy_keymap (XVECTOR (elt)->contents[i]);
416 }
417 else if (CONSP (elt)
418 && XTYPE (XCONS (elt)->cdr) != Lisp_Symbol
419 && ! NILP (Fkeymapp (XCONS (elt)->cdr)))
420 XCONS (elt)->cdr = Fcopy_keymap (XCONS (elt)->cdr);
421 }
422
423 return copy;
424 }
425 \f
426 /* Simple Keymap mutators and accessors. */
427
428 DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0,
429 "Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.\n\
430 KEYMAP is a keymap. KEY is a string or a vector of symbols and characters\n\
431 meaning a sequence of keystrokes and events.\n\
432 DEF is anything that can be a key's definition:\n\
433 nil (means key is undefined in this keymap),\n\
434 a command (a Lisp function suitable for interactive calling)\n\
435 a string (treated as a keyboard macro),\n\
436 a keymap (to define a prefix key),\n\
437 a symbol. When the key is looked up, the symbol will stand for its\n\
438 function definition, which should at that time be one of the above,\n\
439 or another symbol whose function definition is used, etc.\n\
440 a cons (STRING . DEFN), meaning that DEFN is the definition\n\
441 (DEFN should be a valid definition in its own right),\n\
442 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.\n\
443 \n\
444 If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at\n\
445 the front of KEYMAP.")
446 (keymap, key, def)
447 register Lisp_Object keymap;
448 Lisp_Object key;
449 Lisp_Object def;
450 {
451 register int idx;
452 register Lisp_Object c;
453 register Lisp_Object tem;
454 register Lisp_Object cmd;
455 int metized = 0;
456 int length;
457
458 keymap = get_keymap (keymap);
459
460 if (XTYPE (key) != Lisp_Vector
461 && XTYPE (key) != Lisp_String)
462 key = wrong_type_argument (Qarrayp, key);
463
464 length = Flength (key);
465 if (length == 0)
466 return Qnil;
467
468 idx = 0;
469 while (1)
470 {
471 c = Faref (key, make_number (idx));
472
473 if (XTYPE (c) == Lisp_Int
474 && XINT (c) >= 0200
475 && !metized)
476 {
477 c = meta_prefix_char;
478 metized = 1;
479 }
480 else
481 {
482 if (XTYPE (c) == Lisp_Int)
483 XSETINT (c, XINT (c) & 0177);
484
485 metized = 0;
486 idx++;
487 }
488
489 if (idx == length)
490 return store_in_keymap (keymap, c, def);
491
492 cmd = get_keyelt (access_keymap (keymap, c));
493
494 if (NILP (cmd))
495 {
496 cmd = Fmake_sparse_keymap (Qnil);
497 store_in_keymap (keymap, c, cmd);
498 }
499
500 tem = Fkeymapp (cmd);
501 if (NILP (tem))
502 error ("Key sequence %s uses invalid prefix characters",
503 XSTRING (key)->data);
504
505 keymap = get_keymap (cmd);
506 }
507 }
508
509 /* Value is number if KEY is too long; NIL if valid but has no definition. */
510
511 DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 2, 0,
512 "In keymap KEYMAP, look up key sequence KEY. Return the definition.\n\
513 nil means undefined. See doc of `define-key' for kinds of definitions.\n\
514 A number as value means KEY is \"too long\";\n\
515 that is, characters or symbols in it except for the last one\n\
516 fail to be a valid sequence of prefix characters in KEYMAP.\n\
517 The number is how many characters at the front of KEY\n\
518 it takes to reach a non-prefix command.")
519 (keymap, key)
520 register Lisp_Object keymap;
521 Lisp_Object key;
522 {
523 register int idx;
524 register Lisp_Object tem;
525 register Lisp_Object cmd;
526 register Lisp_Object c;
527 int metized = 0;
528 int length;
529
530 keymap = get_keymap (keymap);
531
532 if (XTYPE (key) != Lisp_Vector
533 && XTYPE (key) != Lisp_String)
534 key = wrong_type_argument (Qarrayp, key);
535
536 length = Flength (key);
537 if (length == 0)
538 return keymap;
539
540 idx = 0;
541 while (1)
542 {
543 c = Faref (key, make_number (idx));
544
545 if (XTYPE (c) == Lisp_Int
546 && XINT (c) >= 0200
547 && !metized)
548 {
549 c = meta_prefix_char;
550 metized = 1;
551 }
552 else
553 {
554 if (XTYPE (c) == Lisp_Int)
555 XSETINT (c, XINT (c) & 0177);
556
557 metized = 0;
558 idx++;
559 }
560
561 cmd = get_keyelt (access_keymap (keymap, c));
562 if (idx == length)
563 return cmd;
564
565 tem = Fkeymapp (cmd);
566 if (NILP (tem))
567 return make_number (idx);
568
569 keymap = get_keymap (cmd);
570 QUIT;
571 }
572 }
573
574 /* Append a key to the end of a key sequence. If key_sequence is a
575 string and key is a character, the result will be another string;
576 otherwise, it will be a vector. */
577 Lisp_Object
578 append_key (key_sequence, key)
579 Lisp_Object key_sequence, key;
580 {
581 Lisp_Object args[2];
582
583 args[0] = key_sequence;
584
585 if (XTYPE (key_sequence) == Lisp_String
586 && XTYPE (key) == Lisp_Int)
587 {
588 args[1] = Fchar_to_string (key);
589 return Fconcat (2, args);
590 }
591 else
592 {
593 args[1] = Fcons (key, Qnil);
594 return Fvconcat (2, args);
595 }
596 }
597
598 \f
599 /* Global, local, and minor mode keymap stuff. */
600
601 /* We can't put these variables inside current_minor_maps, since under
602 some systems, static gets macro-defined to be the empty string.
603 Ickypoo. */
604 static Lisp_Object *cmm_modes, *cmm_maps;
605 static int cmm_size;
606
607 /* Store a pointer to an array of the keymaps of the currently active
608 minor modes in *buf, and return the number of maps it contains.
609
610 This function always returns a pointer to the same buffer, and may
611 free or reallocate it, so if you want to keep it for a long time or
612 hand it out to lisp code, copy it. This procedure will be called
613 for every key sequence read, so the nice lispy approach (return a
614 new assoclist, list, what have you) for each invocation would
615 result in a lot of consing over time.
616
617 If we used xrealloc/xmalloc and ran out of memory, they would throw
618 back to the command loop, which would try to read a key sequence,
619 which would call this function again, resulting in an infinite
620 loop. Instead, we'll use realloc/malloc and silently truncate the
621 list, let the key sequence be read, and hope some other piece of
622 code signals the error. */
623 int
624 current_minor_maps (modeptr, mapptr)
625 Lisp_Object **modeptr, **mapptr;
626 {
627 int i = 0;
628 Lisp_Object alist, assoc, var, val;
629
630 for (alist = Vminor_mode_map_alist;
631 CONSP (alist);
632 alist = XCONS (alist)->cdr)
633 if (CONSP (assoc = XCONS (alist)->car)
634 && XTYPE (var = XCONS (assoc)->car) == Lisp_Symbol
635 && ! EQ ((val = find_symbol_value (var)), Qunbound)
636 && ! NILP (val))
637 {
638 if (i >= cmm_size)
639 {
640 Lisp_Object *newmodes, *newmaps;
641
642 if (cmm_maps)
643 {
644 newmodes = (Lisp_Object *) realloc (cmm_modes, cmm_size *= 2);
645 newmaps = (Lisp_Object *) realloc (cmm_maps, cmm_size);
646 }
647 else
648 {
649 newmodes = (Lisp_Object *) malloc (cmm_size = 30);
650 newmaps = (Lisp_Object *) malloc (cmm_size);
651 }
652
653 if (newmaps && newmodes)
654 {
655 cmm_modes = newmodes;
656 cmm_maps = newmaps;
657 }
658 else
659 break;
660 }
661 cmm_modes[i] = var;
662 cmm_maps [i] = XCONS (assoc)->cdr;
663 i++;
664 }
665
666 if (modeptr) *modeptr = cmm_modes;
667 if (mapptr) *mapptr = cmm_maps;
668 return i;
669 }
670
671 DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 1, 0,
672 "Return the binding for command KEY in current keymaps.\n\
673 KEY is a string, a sequence of keystrokes.\n\
674 The binding is probably a symbol with a function definition.")
675 (key)
676 Lisp_Object key;
677 {
678 Lisp_Object *maps, value;
679 int nmaps, i;
680
681 nmaps = current_minor_maps (0, &maps);
682 for (i = 0; i < nmaps; i++)
683 if (! NILP (maps[i]))
684 {
685 value = Flookup_key (maps[i], key);
686 if (! NILP (value) && XTYPE (value) != Lisp_Int)
687 return value;
688 }
689
690 if (! NILP (current_buffer->keymap))
691 {
692 value = Flookup_key (current_buffer->keymap, key);
693 if (! NILP (value) && XTYPE (value) != Lisp_Int)
694 return value;
695 }
696
697 value = Flookup_key (current_global_map, key);
698 if (! NILP (value) && XTYPE (value) != Lisp_Int)
699 return value;
700
701 return Qnil;
702 }
703
704 DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 1, 0,
705 "Return the binding for command KEYS in current local keymap only.\n\
706 KEYS is a string, a sequence of keystrokes.\n\
707 The binding is probably a symbol with a function definition.")
708 (keys)
709 Lisp_Object keys;
710 {
711 register Lisp_Object map;
712 map = current_buffer->keymap;
713 if (NILP (map))
714 return Qnil;
715 return Flookup_key (map, keys);
716 }
717
718 DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 1, 0,
719 "Return the binding for command KEYS in current global keymap only.\n\
720 KEYS is a string, a sequence of keystrokes.\n\
721 The binding is probably a symbol with a function definition.\n\
722 This function's return values are the same as those of lookup-key\n\
723 (which see).")
724 (keys)
725 Lisp_Object keys;
726 {
727 return Flookup_key (current_global_map, keys);
728 }
729
730 DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 1, 0,
731 "Find the visible minor mode bindings of KEY.\n\
732 Return an alist of pairs (MODENAME . BINDING), where MODENAME is the\n\
733 the symbol which names the minor mode binding KEY, and BINDING is\n\
734 KEY's definition in that mode. In particular, if KEY has no\n\
735 minor-mode bindings, return nil. If the first binding is a\n\
736 non-prefix, all subsequent bindings will be omitted, since they would\n\
737 be ignored. Similarly, the list doesn't include non-prefix bindings\n\
738 that come after prefix bindings.")
739 (key)
740 {
741 Lisp_Object *modes, *maps;
742 int nmaps;
743 Lisp_Object binding;
744 int i, j;
745
746 nmaps = current_minor_maps (&modes, &maps);
747
748 for (i = j = 0; i < nmaps; i++)
749 if (! NILP (maps[i])
750 && ! NILP (binding = Flookup_key (maps[i], key))
751 && XTYPE (binding) != Lisp_Int)
752 {
753 if (! NILP (get_keymap_1 (binding, 0)))
754 maps[j++] = Fcons (modes[i], binding);
755 else if (j == 0)
756 return Fcons (Fcons (modes[i], binding), Qnil);
757 }
758
759 return Flist (j, maps);
760 }
761
762 DEFUN ("global-set-key", Fglobal_set_key, Sglobal_set_key, 2, 2,
763 "kSet key globally: \nCSet key %s to command: ",
764 "Give KEY a global binding as COMMAND.\n\
765 COMMAND is a symbol naming an interactively-callable function.\n\
766 KEY is a string representing a sequence of keystrokes.\n\
767 Note that if KEY has a local binding in the current buffer\n\
768 that local binding will continue to shadow any global binding.")
769 (keys, function)
770 Lisp_Object keys, function;
771 {
772 if (XTYPE (keys) != Lisp_Vector
773 && XTYPE (keys) != Lisp_String)
774 keys = wrong_type_argument (Qarrayp, keys);
775
776 Fdefine_key (current_global_map, keys, function);
777 return Qnil;
778 }
779
780 DEFUN ("local-set-key", Flocal_set_key, Slocal_set_key, 2, 2,
781 "kSet key locally: \nCSet key %s locally to command: ",
782 "Give KEY a local binding as COMMAND.\n\
783 COMMAND is a symbol naming an interactively-callable function.\n\
784 KEY is a string representing a sequence of keystrokes.\n\
785 The binding goes in the current buffer's local map,\n\
786 which is shared with other buffers in the same major mode.")
787 (keys, function)
788 Lisp_Object keys, function;
789 {
790 register Lisp_Object map;
791 map = current_buffer->keymap;
792 if (NILP (map))
793 {
794 map = Fmake_sparse_keymap (Qnil);
795 current_buffer->keymap = map;
796 }
797
798 if (XTYPE (keys) != Lisp_Vector
799 && XTYPE (keys) != Lisp_String)
800 keys = wrong_type_argument (Qarrayp, keys);
801
802 Fdefine_key (map, keys, function);
803 return Qnil;
804 }
805
806 DEFUN ("global-unset-key", Fglobal_unset_key, Sglobal_unset_key,
807 1, 1, "kUnset key globally: ",
808 "Remove global binding of KEY.\n\
809 KEY is a string representing a sequence of keystrokes.")
810 (keys)
811 Lisp_Object keys;
812 {
813 return Fglobal_set_key (keys, Qnil);
814 }
815
816 DEFUN ("local-unset-key", Flocal_unset_key, Slocal_unset_key, 1, 1,
817 "kUnset key locally: ",
818 "Remove local binding of KEY.\n\
819 KEY is a string representing a sequence of keystrokes.")
820 (keys)
821 Lisp_Object keys;
822 {
823 if (!NILP (current_buffer->keymap))
824 Flocal_set_key (keys, Qnil);
825 return Qnil;
826 }
827
828 DEFUN ("define-prefix-command", Fdefine_prefix_command, Sdefine_prefix_command, 1, 2, 0,
829 "Define COMMAND as a prefix command.\n\
830 A new sparse keymap is stored as COMMAND's function definition and its value.\n\
831 If a second optional argument MAPVAR is given, the map is stored as\n\
832 its value instead of as COMMAND's value; but COMMAND is still defined\n\
833 as a function.")
834 (name, mapvar)
835 Lisp_Object name, mapvar;
836 {
837 Lisp_Object map;
838 map = Fmake_sparse_keymap (Qnil);
839 Ffset (name, map);
840 if (!NILP (mapvar))
841 Fset (mapvar, map);
842 else
843 Fset (name, map);
844 return name;
845 }
846
847 DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0,
848 "Select KEYMAP as the global keymap.")
849 (keymap)
850 Lisp_Object keymap;
851 {
852 keymap = get_keymap (keymap);
853 current_global_map = keymap;
854 return Qnil;
855 }
856
857 DEFUN ("use-local-map", Fuse_local_map, Suse_local_map, 1, 1, 0,
858 "Select KEYMAP as the local keymap.\n\
859 If KEYMAP is nil, that means no local keymap.")
860 (keymap)
861 Lisp_Object keymap;
862 {
863 if (!NILP (keymap))
864 keymap = get_keymap (keymap);
865
866 current_buffer->keymap = keymap;
867
868 return Qnil;
869 }
870
871 DEFUN ("current-local-map", Fcurrent_local_map, Scurrent_local_map, 0, 0, 0,
872 "Return current buffer's local keymap, or nil if it has none.")
873 ()
874 {
875 return current_buffer->keymap;
876 }
877
878 DEFUN ("current-global-map", Fcurrent_global_map, Scurrent_global_map, 0, 0, 0,
879 "Return the current global keymap.")
880 ()
881 {
882 return current_global_map;
883 }
884
885 DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps, Scurrent_minor_mode_maps, 0, 0, 0,
886 "Return a list of keymaps for the minor modes of the current buffer.")
887 ()
888 {
889 Lisp_Object *maps;
890 int nmaps = current_minor_maps (0, &maps);
891
892 return Flist (nmaps, maps);
893 }
894 \f
895 /* Help functions for describing and documenting keymaps. */
896
897 DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps,
898 1, 1, 0,
899 "Find all keymaps accessible via prefix characters from KEYMAP.\n\
900 Returns a list of elements of the form (KEYS . MAP), where the sequence\n\
901 KEYS starting from KEYMAP gets you to MAP. These elements are ordered\n\
902 so that the KEYS increase in length. The first element is (\"\" . KEYMAP).")
903 (startmap)
904 Lisp_Object startmap;
905 {
906 Lisp_Object maps, tail;
907
908 maps = Fcons (Fcons (build_string (""), get_keymap (startmap)), Qnil);
909
910 /* For each map in the list maps,
911 look at any other maps it points to,
912 and stick them at the end if they are not already in the list.
913
914 This is a breadth-first traversal, where tail is the queue of
915 nodes, and maps accumulates a list of all nodes visited. */
916
917 for (tail = maps; CONSP (tail); tail = XCONS (tail)->cdr)
918 {
919 register Lisp_Object thisseq = Fcar (Fcar (tail));
920 register Lisp_Object thismap = Fcdr (Fcar (tail));
921 Lisp_Object last = make_number (XINT (Flength (thisseq)) - 1);
922
923 /* Does the current sequence end in the meta-prefix-char? */
924 int is_metized = (XINT (last) >= 0
925 && EQ (Faref (thisseq, last), meta_prefix_char));
926
927 for (; CONSP (thismap); thismap = XCONS (thismap)->cdr)
928 {
929 Lisp_Object elt = XCONS (thismap)->car;
930
931 QUIT;
932
933 if (XTYPE (elt) == Lisp_Vector)
934 {
935 register int i;
936
937 /* Vector keymap. Scan all the elements. */
938 for (i = 0; i < DENSE_TABLE_SIZE; i++)
939 {
940 register Lisp_Object tem;
941 register Lisp_Object cmd;
942
943 cmd = get_keyelt (XVECTOR (elt)->contents[i]);
944 if (NILP (cmd)) continue;
945 tem = Fkeymapp (cmd);
946 if (!NILP (tem))
947 {
948 cmd = get_keymap (cmd);
949 /* Ignore keymaps that are already added to maps. */
950 tem = Frassq (cmd, maps);
951 if (NILP (tem))
952 {
953 /* If the last key in thisseq is meta-prefix-char,
954 turn it into a meta-ized keystroke. We know
955 that the event we're about to append is an
956 ascii keystroke since we're processing a
957 keymap table. */
958 if (is_metized)
959 {
960 tem = Fcopy_sequence (thisseq);
961 Faset (tem, last, make_number (i | 0200));
962
963 /* This new sequence is the same length as
964 thisseq, so stick it in the list right
965 after this one. */
966 XCONS (tail)->cdr =
967 Fcons (Fcons (tem, cmd), XCONS (tail)->cdr);
968 }
969 else
970 {
971 tem = append_key (thisseq, make_number (i));
972 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
973 }
974 }
975 }
976 }
977 }
978 else if (CONSP (elt))
979 {
980 register Lisp_Object cmd = get_keyelt (XCONS (elt)->cdr);
981 register Lisp_Object tem;
982
983 /* Ignore definitions that aren't keymaps themselves. */
984 tem = Fkeymapp (cmd);
985 if (!NILP (tem))
986 {
987 /* Ignore keymaps that have been seen already. */
988 cmd = get_keymap (cmd);
989 tem = Frassq (cmd, maps);
990 if (NILP (tem))
991 {
992 /* let elt be the event defined by this map entry. */
993 elt = XCONS (elt)->car;
994
995 /* If the last key in thisseq is meta-prefix-char, and
996 this entry is a binding for an ascii keystroke,
997 turn it into a meta-ized keystroke. */
998 if (is_metized && XTYPE (elt) == Lisp_Int)
999 {
1000 tem = Fcopy_sequence (thisseq);
1001 Faset (tem, last, make_number (XINT (elt) | 0200));
1002
1003 /* This new sequence is the same length as
1004 thisseq, so stick it in the list right
1005 after this one. */
1006 XCONS (tail)->cdr =
1007 Fcons (Fcons (tem, cmd), XCONS (tail)->cdr);
1008 }
1009 else
1010 nconc2 (tail,
1011 Fcons (Fcons (append_key (thisseq, elt), cmd),
1012 Qnil));
1013 }
1014 }
1015 }
1016 }
1017 }
1018
1019 return maps;
1020 }
1021
1022 Lisp_Object Qsingle_key_description, Qkey_description;
1023
1024 DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0,
1025 "Return a pretty description of key-sequence KEYS.\n\
1026 Control characters turn into \"C-foo\" sequences, meta into \"M-foo\"\n\
1027 spaces are put between sequence elements, etc.")
1028 (keys)
1029 Lisp_Object keys;
1030 {
1031 return Fmapconcat (Qsingle_key_description, keys, build_string (" "));
1032 }
1033
1034 char *
1035 push_key_description (c, p)
1036 register unsigned int c;
1037 register char *p;
1038 {
1039 if (c >= 0200)
1040 {
1041 *p++ = 'M';
1042 *p++ = '-';
1043 c -= 0200;
1044 }
1045 if (c < 040)
1046 {
1047 if (c == 033)
1048 {
1049 *p++ = 'E';
1050 *p++ = 'S';
1051 *p++ = 'C';
1052 }
1053 else if (c == Ctl('I'))
1054 {
1055 *p++ = 'T';
1056 *p++ = 'A';
1057 *p++ = 'B';
1058 }
1059 else if (c == Ctl('J'))
1060 {
1061 *p++ = 'L';
1062 *p++ = 'F';
1063 *p++ = 'D';
1064 }
1065 else if (c == Ctl('M'))
1066 {
1067 *p++ = 'R';
1068 *p++ = 'E';
1069 *p++ = 'T';
1070 }
1071 else
1072 {
1073 *p++ = 'C';
1074 *p++ = '-';
1075 if (c > 0 && c <= Ctl ('Z'))
1076 *p++ = c + 0140;
1077 else
1078 *p++ = c + 0100;
1079 }
1080 }
1081 else if (c == 0177)
1082 {
1083 *p++ = 'D';
1084 *p++ = 'E';
1085 *p++ = 'L';
1086 }
1087 else if (c == ' ')
1088 {
1089 *p++ = 'S';
1090 *p++ = 'P';
1091 *p++ = 'C';
1092 }
1093 else
1094 *p++ = c;
1095
1096 return p;
1097 }
1098
1099 DEFUN ("single-key-description", Fsingle_key_description, Ssingle_key_description, 1, 1, 0,
1100 "Return a pretty description of command character KEY.\n\
1101 Control characters turn into C-whatever, etc.")
1102 (key)
1103 Lisp_Object key;
1104 {
1105 register unsigned char c;
1106 char tem[6];
1107
1108 if (EVENT_HAS_PARAMETERS (key))
1109 key = EVENT_HEAD (key);
1110
1111 switch (XTYPE (key))
1112 {
1113 case Lisp_Int: /* Normal character */
1114 c = XINT (key) & 0377;
1115 *push_key_description (c, tem) = 0;
1116 return build_string (tem);
1117
1118 case Lisp_Symbol: /* Function key or event-symbol */
1119 return Fsymbol_name (key);
1120
1121 default:
1122 error ("KEY must be an integer, cons, or symbol.");
1123 }
1124 }
1125
1126 char *
1127 push_text_char_description (c, p)
1128 register unsigned int c;
1129 register char *p;
1130 {
1131 if (c >= 0200)
1132 {
1133 *p++ = 'M';
1134 *p++ = '-';
1135 c -= 0200;
1136 }
1137 if (c < 040)
1138 {
1139 *p++ = '^';
1140 *p++ = c + 64; /* 'A' - 1 */
1141 }
1142 else if (c == 0177)
1143 {
1144 *p++ = '^';
1145 *p++ = '?';
1146 }
1147 else
1148 *p++ = c;
1149 return p;
1150 }
1151
1152 DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0,
1153 "Return a pretty description of file-character CHAR.\n\
1154 Control characters turn into \"^char\", etc.")
1155 (chr)
1156 Lisp_Object chr;
1157 {
1158 char tem[6];
1159
1160 CHECK_NUMBER (chr, 0);
1161
1162 *push_text_char_description (XINT (chr) & 0377, tem) = 0;
1163
1164 return build_string (tem);
1165 }
1166 \f
1167 /* where-is - finding a command in a set of keymaps. */
1168
1169 DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 5, 0,
1170 "Return list of keys that invoke DEFINITION in KEYMAP or KEYMAP1.\n\
1171 If KEYMAP is nil, search only KEYMAP1.\n\
1172 If KEYMAP1 is nil, use the current global map.\n\
1173 \n\
1174 If optional 4th arg FIRSTONLY is non-nil,\n\
1175 return a string representing the first key sequence found,\n\
1176 rather than a list of all possible key sequences.\n\
1177 \n\
1178 If optional 5th arg NOINDIRECT is non-nil, don't follow indirections\n\
1179 to other keymaps or slots. This makes it possible to search for an\n\
1180 indirect definition itself.")
1181 (definition, local_keymap, global_keymap, firstonly, noindirect)
1182 Lisp_Object definition, local_keymap, global_keymap;
1183 Lisp_Object firstonly, noindirect;
1184 {
1185 register Lisp_Object maps;
1186 Lisp_Object found;
1187
1188 if (NILP (global_keymap))
1189 global_keymap = current_global_map;
1190
1191 if (!NILP (local_keymap))
1192 maps = nconc2 (Faccessible_keymaps (get_keymap (local_keymap)),
1193 Faccessible_keymaps (get_keymap (global_keymap)));
1194 else
1195 maps = Faccessible_keymaps (get_keymap (global_keymap));
1196
1197 found = Qnil;
1198
1199 for (; !NILP (maps); maps = Fcdr (maps))
1200 {
1201 /* Key sequence to reach map */
1202 register Lisp_Object this = Fcar (Fcar (maps));
1203
1204 /* The map that it reaches */
1205 register Lisp_Object map = Fcdr (Fcar (maps));
1206
1207 /* If Fcar (map) is a VECTOR, the current element within that vector. */
1208 int i = 0;
1209
1210 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
1211 [M-CHAR] sequences, check if last character of the sequence
1212 is the meta-prefix char. */
1213 Lisp_Object last = make_number (XINT (Flength (this)) - 1);
1214 int last_is_meta = (XINT (last) >= 0
1215 && EQ (Faref (this, last), meta_prefix_char));
1216
1217 QUIT;
1218
1219 while (CONSP (map))
1220 {
1221 /* Because the code we want to run on each binding is rather
1222 large, we don't want to have two separate loop bodies for
1223 sparse keymap bindings and tables; we want to iterate one
1224 loop body over both keymap and vector bindings.
1225
1226 For this reason, if Fcar (map) is a vector, we don't
1227 advance map to the next element until i indicates that we
1228 have finished off the vector. */
1229
1230 Lisp_Object elt = XCONS (map)->car;
1231 Lisp_Object key, binding, sequence;
1232
1233 QUIT;
1234
1235 /* Set key and binding to the current key and binding, and
1236 advance map and i to the next binding. */
1237 if (XTYPE (elt) == Lisp_Vector)
1238 {
1239 /* In a vector, look at each element. */
1240 binding = XVECTOR (elt)->contents[i];
1241 XFASTINT (key) = i;
1242 i++;
1243
1244 /* If we've just finished scanning a vector, advance map
1245 to the next element, and reset i in anticipation of the
1246 next vector we may find. */
1247 if (i >= DENSE_TABLE_SIZE)
1248 {
1249 map = XCONS (map)->cdr;
1250 i = 0;
1251 }
1252 }
1253 else if (CONSP (elt))
1254 {
1255 key = Fcar (Fcar (map));
1256 binding = Fcdr (Fcar (map));
1257
1258 map = XCONS (map)->cdr;
1259 }
1260 else
1261 /* We want to ignore keymap elements that are neither
1262 vectors nor conses. */
1263 {
1264 map = XCONS (map)->cdr;
1265 continue;
1266 }
1267
1268 /* Search through indirections unless that's not wanted. */
1269 if (NILP (noindirect))
1270 binding = get_keyelt (binding);
1271
1272 /* End this iteration if this element does not match
1273 the target. */
1274
1275 if (XTYPE (definition) == Lisp_Cons)
1276 {
1277 Lisp_Object tem;
1278 tem = Fequal (binding, definition);
1279 if (NILP (tem))
1280 continue;
1281 }
1282 else
1283 if (!EQ (binding, definition))
1284 continue;
1285
1286 /* We have found a match.
1287 Construct the key sequence where we found it. */
1288 if (XTYPE (key) == Lisp_Int && last_is_meta)
1289 {
1290 sequence = Fcopy_sequence (this);
1291 Faset (sequence, last, make_number (XINT (key) | 0200));
1292 }
1293 else
1294 sequence = append_key (this, key);
1295
1296 /* Verify that this key binding is not shadowed by another
1297 binding for the same key, before we say it exists.
1298
1299 Mechanism: look for local definition of this key and if
1300 it is defined and does not match what we found then
1301 ignore this key.
1302
1303 Either nil or number as value from Flookup_key
1304 means undefined. */
1305 if (!NILP (local_keymap))
1306 {
1307 binding = Flookup_key (local_keymap, sequence);
1308 if (!NILP (binding) && XTYPE (binding) != Lisp_Int)
1309 {
1310 if (XTYPE (definition) == Lisp_Cons)
1311 {
1312 Lisp_Object tem;
1313 tem = Fequal (binding, definition);
1314 if (NILP (tem))
1315 continue;
1316 }
1317 else
1318 if (!EQ (binding, definition))
1319 continue;
1320 }
1321 }
1322
1323 /* It is a true unshadowed match. Record it. */
1324
1325 if (!NILP (firstonly))
1326 return sequence;
1327 found = Fcons (sequence, found);
1328 }
1329 }
1330 return Fnreverse (found);
1331 }
1332
1333 /* Return a string listing the keys and buttons that run DEFINITION. */
1334
1335 static Lisp_Object
1336 where_is_string (definition)
1337 Lisp_Object definition;
1338 {
1339 register Lisp_Object keys, keys1;
1340
1341 keys = Fwhere_is_internal (definition,
1342 current_buffer->keymap, Qnil, Qnil, Qnil);
1343 keys1 = Fmapconcat (Qkey_description, keys, build_string (", "));
1344
1345 return keys1;
1346 }
1347
1348 DEFUN ("where-is", Fwhere_is, Swhere_is, 1, 1, "CWhere is command: ",
1349 "Print message listing key sequences that invoke specified command.\n\
1350 Argument is a command definition, usually a symbol with a function definition.")
1351 (definition)
1352 Lisp_Object definition;
1353 {
1354 register Lisp_Object string;
1355
1356 CHECK_SYMBOL (definition, 0);
1357 string = where_is_string (definition);
1358
1359 if (XSTRING (string)->size)
1360 message ("%s is on %s", XSYMBOL (definition)->name->data,
1361 XSTRING (string)->data);
1362 else
1363 message ("%s is not on any key", XSYMBOL (definition)->name->data);
1364 return Qnil;
1365 }
1366 \f
1367 /* describe-bindings - summarizing all the bindings in a set of keymaps. */
1368
1369 DEFUN ("describe-bindings", Fdescribe_bindings, Sdescribe_bindings, 0, 0, "",
1370 "Show a list of all defined keys, and their definitions.\n\
1371 The list is put in a buffer, which is displayed.")
1372 ()
1373 {
1374 register Lisp_Object thisbuf;
1375 XSET (thisbuf, Lisp_Buffer, current_buffer);
1376 internal_with_output_to_temp_buffer ("*Help*",
1377 describe_buffer_bindings,
1378 thisbuf);
1379 return Qnil;
1380 }
1381
1382 static Lisp_Object
1383 describe_buffer_bindings (descbuf)
1384 Lisp_Object descbuf;
1385 {
1386 register Lisp_Object start1, start2;
1387
1388 char *key_heading
1389 = "\
1390 key binding\n\
1391 --- -------\n";
1392 char *alternate_heading
1393 = "\
1394 Alternate Characters (use anywhere the nominal character is listed):\n\
1395 nominal alternate\n\
1396 ------- ---------\n";
1397
1398 Fset_buffer (Vstandard_output);
1399
1400 /* Report on alternates for keys. */
1401 if (XTYPE (Vkeyboard_translate_table) == Lisp_String)
1402 {
1403 int c;
1404 unsigned char *translate = XSTRING (Vkeyboard_translate_table)->data;
1405 int translate_len = XSTRING (Vkeyboard_translate_table)->size;
1406
1407 for (c = 0; c < translate_len; c++)
1408 if (translate[c] != c)
1409 {
1410 char buf[20];
1411 char *bufend;
1412
1413 if (alternate_heading)
1414 {
1415 insert_string (alternate_heading);
1416 alternate_heading = 0;
1417 }
1418
1419 bufend = push_key_description (translate[c], buf);
1420 insert (buf, bufend - buf);
1421 Findent_to (make_number (16), make_number (1));
1422 bufend = push_key_description (c, buf);
1423 insert (buf, bufend - buf);
1424
1425 insert ("\n", 1);
1426 }
1427
1428 insert ("\n", 1);
1429 }
1430
1431 {
1432 int i, nmaps;
1433 Lisp_Object *modes, *maps;
1434
1435 /* Temporarily switch to descbuf, so that we can get that buffer's
1436 minor modes correctly. */
1437 Fset_buffer (descbuf);
1438 nmaps = current_minor_maps (&modes, &maps);
1439 Fset_buffer (Vstandard_output);
1440
1441 for (i = 0; i < nmaps; i++)
1442 {
1443 if (XTYPE (modes[i]) == Lisp_Symbol)
1444 {
1445 insert_char ('`');
1446 insert_string (XSYMBOL (modes[i])->name->data);
1447 insert_char ('\'');
1448 }
1449 else
1450 insert_string ("Strangely Named");
1451 insert_string (" Minor Mode Bindings:\n");
1452 insert_string (key_heading);
1453 describe_map_tree (maps[i], 0, Qnil);
1454 insert_char ('\n');
1455 }
1456 }
1457
1458 start1 = XBUFFER (descbuf)->keymap;
1459 if (!NILP (start1))
1460 {
1461 insert_string ("Local Bindings:\n");
1462 insert_string (key_heading);
1463 describe_map_tree (start1, 0, Qnil);
1464 insert_string ("\n");
1465 }
1466
1467 insert_string ("Global Bindings:\n");
1468 if (NILP (start1))
1469 insert_string (key_heading);
1470
1471 describe_map_tree (current_global_map, 0, XBUFFER (descbuf)->keymap);
1472
1473 Fset_buffer (descbuf);
1474 return Qnil;
1475 }
1476
1477 /* Insert a desription of the key bindings in STARTMAP,
1478 followed by those of all maps reachable through STARTMAP.
1479 If PARTIAL is nonzero, omit certain "uninteresting" commands
1480 (such as `undefined').
1481 If SHADOW is non-nil, it is another map;
1482 don't mention keys which would be shadowed by it. */
1483
1484 void
1485 describe_map_tree (startmap, partial, shadow)
1486 Lisp_Object startmap, shadow;
1487 int partial;
1488 {
1489 register Lisp_Object elt, sh;
1490 Lisp_Object maps;
1491 struct gcpro gcpro1;
1492
1493 maps = Faccessible_keymaps (startmap);
1494 GCPRO1 (maps);
1495
1496 for (; !NILP (maps); maps = Fcdr (maps))
1497 {
1498 elt = Fcar (maps);
1499 sh = Fcar (elt);
1500
1501 /* If there is no shadow keymap given, don't shadow. */
1502 if (NILP (shadow))
1503 sh = Qnil;
1504
1505 /* If the sequence by which we reach this keymap is zero-length,
1506 then the shadow map for this keymap is just SHADOW. */
1507 else if ((XTYPE (sh) == Lisp_String
1508 && XSTRING (sh)->size == 0)
1509 || (XTYPE (sh) == Lisp_Vector
1510 && XVECTOR (sh)->size == 0))
1511 sh = shadow;
1512
1513 /* If the sequence by which we reach this keymap actually has
1514 some elements, then the sequence's definition in SHADOW is
1515 what we should use. */
1516 else
1517 {
1518 sh = Flookup_key (shadow, Fcar (elt));
1519 if (XTYPE (sh) == Lisp_Int)
1520 sh = Qnil;
1521 }
1522
1523 /* If sh is null (meaning that the current map is not shadowed),
1524 or a keymap (meaning that bindings from the current map might
1525 show through), describe the map. Otherwise, sh is a command
1526 that completely shadows the current map, and we shouldn't
1527 bother. */
1528 if (NILP (sh) || !NILP (Fkeymapp (sh)))
1529 describe_map (Fcdr (elt), Fcar (elt), partial, sh);
1530 }
1531
1532 UNGCPRO;
1533 }
1534
1535 static void
1536 describe_command (definition)
1537 Lisp_Object definition;
1538 {
1539 register Lisp_Object tem1;
1540
1541 Findent_to (make_number (16), make_number (1));
1542
1543 if (XTYPE (definition) == Lisp_Symbol)
1544 {
1545 XSET (tem1, Lisp_String, XSYMBOL (definition)->name);
1546 insert1 (tem1);
1547 insert_string ("\n");
1548 }
1549 else
1550 {
1551 tem1 = Fkeymapp (definition);
1552 if (!NILP (tem1))
1553 insert_string ("Prefix Command\n");
1554 else
1555 insert_string ("??\n");
1556 }
1557 }
1558
1559 /* Describe the contents of map MAP, assuming that this map itself is
1560 reached by the sequence of prefix keys KEYS (a string or vector).
1561 PARTIAL, SHADOW is as in `describe_map_tree' above. */
1562
1563 static void
1564 describe_map (map, keys, partial, shadow)
1565 Lisp_Object map, keys;
1566 int partial;
1567 Lisp_Object shadow;
1568 {
1569 register Lisp_Object keysdesc;
1570
1571 if (!NILP (keys) && Flength (keys) > 0)
1572 keysdesc = concat2 (Fkey_description (keys),
1573 build_string (" "));
1574 else
1575 keysdesc = Qnil;
1576
1577 describe_map_2 (map, keysdesc, describe_command, partial, shadow);
1578 }
1579
1580 /* Insert a description of KEYMAP into the current buffer. */
1581
1582 static void
1583 describe_map_2 (keymap, elt_prefix, elt_describer, partial, shadow)
1584 register Lisp_Object keymap;
1585 Lisp_Object elt_prefix;
1586 int (*elt_describer) ();
1587 int partial;
1588 Lisp_Object shadow;
1589 {
1590 Lisp_Object this;
1591 Lisp_Object tem1, tem2 = Qnil;
1592 Lisp_Object suppress;
1593 Lisp_Object kludge;
1594 int first = 1;
1595 struct gcpro gcpro1, gcpro2, gcpro3;
1596
1597 if (partial)
1598 suppress = intern ("suppress-keymap");
1599
1600 /* This vector gets used to present single keys to Flookup_key. Since
1601 that is done once per keymap element, we don't want to cons up a
1602 fresh vector every time. */
1603 kludge = Fmake_vector (make_number (1), Qnil);
1604
1605 GCPRO3 (elt_prefix, tem2, kludge);
1606
1607 for (; CONSP (keymap); keymap = Fcdr (keymap))
1608 {
1609 QUIT;
1610
1611 if (XTYPE (XCONS (keymap)->car) == Lisp_Vector)
1612 describe_vector (XCONS (keymap)->car,
1613 elt_prefix, elt_describer, partial, shadow);
1614 else
1615 {
1616 tem1 = Fcar_safe (Fcar (keymap));
1617 tem2 = get_keyelt (Fcdr_safe (Fcar (keymap)));
1618
1619 /* Don't show undefined commands or suppressed commands. */
1620 if (NILP (tem2)) continue;
1621 if (XTYPE (tem2) == Lisp_Symbol && partial)
1622 {
1623 this = Fget (tem2, suppress);
1624 if (!NILP (this))
1625 continue;
1626 }
1627
1628 /* Don't show a command that isn't really visible
1629 because a local definition of the same key shadows it. */
1630
1631 if (!NILP (shadow))
1632 {
1633 Lisp_Object tem;
1634
1635 XVECTOR (kludge)->contents[0] = tem1;
1636 tem = Flookup_key (shadow, kludge);
1637 if (!NILP (tem)) continue;
1638 }
1639
1640 if (first)
1641 {
1642 insert ("\n", 1);
1643 first = 0;
1644 }
1645
1646 if (!NILP (elt_prefix))
1647 insert1 (elt_prefix);
1648
1649 /* THIS gets the string to describe the character TEM1. */
1650 this = Fsingle_key_description (tem1);
1651 insert1 (this);
1652
1653 /* Print a description of the definition of this character.
1654 elt_describer will take care of spacing out far enough
1655 for alignment purposes. */
1656 (*elt_describer) (tem2);
1657 }
1658 }
1659
1660 UNGCPRO;
1661 }
1662
1663 static int
1664 describe_vector_princ (elt)
1665 Lisp_Object elt;
1666 {
1667 Fprinc (elt, Qnil);
1668 }
1669
1670 DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 1, 0,
1671 "Print on `standard-output' a description of contents of VECTOR.\n\
1672 This is text showing the elements of vector matched against indices.")
1673 (vector)
1674 Lisp_Object vector;
1675 {
1676 CHECK_VECTOR (vector, 0);
1677 describe_vector (vector, Qnil, describe_vector_princ, 0, Qnil, Qnil);
1678 }
1679
1680 describe_vector (vector, elt_prefix, elt_describer, partial, shadow)
1681 register Lisp_Object vector;
1682 Lisp_Object elt_prefix;
1683 int (*elt_describer) ();
1684 int partial;
1685 Lisp_Object shadow;
1686 {
1687 Lisp_Object this;
1688 Lisp_Object dummy;
1689 Lisp_Object tem1, tem2;
1690 register int i;
1691 Lisp_Object suppress;
1692 Lisp_Object kludge;
1693 int first = 1;
1694 struct gcpro gcpro1, gcpro2, gcpro3;
1695
1696 tem1 = Qnil;
1697
1698 /* This vector gets used to present single keys to Flookup_key. Since
1699 that is done once per vector element, we don't want to cons up a
1700 fresh vector every time. */
1701 kludge = Fmake_vector (make_number (1), Qnil);
1702 GCPRO3 (elt_prefix, tem1, kludge);
1703
1704 if (partial)
1705 suppress = intern ("suppress-keymap");
1706
1707 for (i = 0; i < DENSE_TABLE_SIZE; i++)
1708 {
1709 QUIT;
1710 tem1 = get_keyelt (XVECTOR (vector)->contents[i]);
1711
1712 if (NILP (tem1)) continue;
1713
1714 /* Don't mention suppressed commands. */
1715 if (XTYPE (tem1) == Lisp_Symbol && partial)
1716 {
1717 this = Fget (tem1, suppress);
1718 if (!NILP (this))
1719 continue;
1720 }
1721
1722 /* If this command in this map is shadowed by some other map,
1723 ignore it. */
1724 if (!NILP (shadow))
1725 {
1726 Lisp_Object tem;
1727
1728 XVECTOR (kludge)->contents[0] = make_number (i);
1729 tem = Flookup_key (shadow, kludge);
1730
1731 if (!NILP (tem)) continue;
1732 }
1733
1734 if (first)
1735 {
1736 insert ("\n", 1);
1737 first = 0;
1738 }
1739
1740 /* Output the prefix that applies to every entry in this map. */
1741 if (!NILP (elt_prefix))
1742 insert1 (elt_prefix);
1743
1744 /* Get the string to describe the character I, and print it. */
1745 XFASTINT (dummy) = i;
1746
1747 /* THIS gets the string to describe the character DUMMY. */
1748 this = Fsingle_key_description (dummy);
1749 insert1 (this);
1750
1751 /* Find all consecutive characters that have the same definition. */
1752 while (i + 1 < DENSE_TABLE_SIZE
1753 && (tem2 = get_keyelt (XVECTOR (vector)->contents[i+1]),
1754 EQ (tem2, tem1)))
1755 i++;
1756
1757 /* If we have a range of more than one character,
1758 print where the range reaches to. */
1759
1760 if (i != XINT (dummy))
1761 {
1762 insert (" .. ", 4);
1763 if (!NILP (elt_prefix))
1764 insert1 (elt_prefix);
1765
1766 XFASTINT (dummy) = i;
1767 insert1 (Fsingle_key_description (dummy));
1768 }
1769
1770 /* Print a description of the definition of this character.
1771 elt_describer will take care of spacing out far enough
1772 for alignment purposes. */
1773 (*elt_describer) (tem1);
1774 }
1775
1776 UNGCPRO;
1777 }
1778 \f
1779 /* Apropos - finding all symbols whose names match a regexp. */
1780 Lisp_Object apropos_predicate;
1781 Lisp_Object apropos_accumulate;
1782
1783 static void
1784 apropos_accum (symbol, string)
1785 Lisp_Object symbol, string;
1786 {
1787 register Lisp_Object tem;
1788
1789 tem = Fstring_match (string, Fsymbol_name (symbol), Qnil);
1790 if (!NILP (tem) && !NILP (apropos_predicate))
1791 tem = call1 (apropos_predicate, symbol);
1792 if (!NILP (tem))
1793 apropos_accumulate = Fcons (symbol, apropos_accumulate);
1794 }
1795
1796 DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0,
1797 "Show all symbols whose names contain match for REGEXP.\n\
1798 If optional 2nd arg PRED is non-nil, (funcall PRED SYM) is done\n\
1799 for each symbol and a symbol is mentioned only if that returns non-nil.\n\
1800 Return list of symbols found.")
1801 (string, pred)
1802 Lisp_Object string, pred;
1803 {
1804 struct gcpro gcpro1, gcpro2;
1805 CHECK_STRING (string, 0);
1806 apropos_predicate = pred;
1807 GCPRO2 (apropos_predicate, apropos_accumulate);
1808 apropos_accumulate = Qnil;
1809 map_obarray (Vobarray, apropos_accum, string);
1810 apropos_accumulate = Fsort (apropos_accumulate, Qstring_lessp);
1811 UNGCPRO;
1812 return apropos_accumulate;
1813 }
1814 \f
1815 syms_of_keymap ()
1816 {
1817 Lisp_Object tem;
1818
1819 Qkeymap = intern ("keymap");
1820 staticpro (&Qkeymap);
1821
1822 /* Initialize the keymaps standardly used.
1823 Each one is the value of a Lisp variable, and is also
1824 pointed to by a C variable */
1825
1826 global_map = Fmake_keymap (Qnil);
1827 Fset (intern ("global-map"), global_map);
1828
1829 meta_map = Fmake_keymap (Qnil);
1830 Fset (intern ("esc-map"), meta_map);
1831 Ffset (intern ("ESC-prefix"), meta_map);
1832
1833 control_x_map = Fmake_keymap (Qnil);
1834 Fset (intern ("ctl-x-map"), control_x_map);
1835 Ffset (intern ("Control-X-prefix"), control_x_map);
1836
1837 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map,
1838 "Default keymap to use when reading from the minibuffer.");
1839 Vminibuffer_local_map = Fmake_sparse_keymap (Qnil);
1840
1841 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map,
1842 "Local keymap for the minibuffer when spaces are not allowed.");
1843 Vminibuffer_local_ns_map = Fmake_sparse_keymap (Qnil);
1844
1845 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map,
1846 "Local keymap for minibuffer input with completion.");
1847 Vminibuffer_local_completion_map = Fmake_sparse_keymap (Qnil);
1848
1849 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map,
1850 "Local keymap for minibuffer input with completion, for exact match.");
1851 Vminibuffer_local_must_match_map = Fmake_sparse_keymap (Qnil);
1852
1853 current_global_map = global_map;
1854
1855 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist,
1856 "Alist of keymaps to use for minor modes.\n\
1857 Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read\n\
1858 key sequences and look up bindings iff VARIABLE's value is non-nil.\n\
1859 If two active keymaps bind the same key, the keymap appearing earlier\n\
1860 in the list takes precedence.");
1861 Vminor_mode_map_alist = Qnil;
1862
1863 DEFVAR_LISP ("function-key-map", &Vfunction_key_map,
1864 "Keymap mapping ASCII function key sequences onto their preferred forms.\n\
1865 This allows Emacs to recognize function keys sent from ASCII\n\
1866 terminals at any point in a key sequence.\n\
1867 \n\
1868 The read-key-sequence function replaces subsequences bound by\n\
1869 function-key-map with their bindings. When the current local and global\n\
1870 keymaps have no binding for the current key sequence but\n\
1871 function-key-map binds a suffix of the sequence to a vector,\n\
1872 read-key-sequence replaces the matching suffix with its binding, and\n\
1873 continues with the new sequence.\n\
1874 \n\
1875 For example, suppose function-key-map binds `ESC O P' to [pf1].\n\
1876 Typing `ESC O P' to read-key-sequence would return [pf1]. Typing\n\
1877 `C-x ESC O P' would return [?\C-x pf1]. If [pf1] were a prefix\n\
1878 key, typing `ESC O P x' would return [pf1 x].");
1879 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
1880
1881 Qsingle_key_description = intern ("single-key-description");
1882 staticpro (&Qsingle_key_description);
1883
1884 Qkey_description = intern ("key-description");
1885 staticpro (&Qkey_description);
1886
1887 Qkeymapp = intern ("keymapp");
1888 staticpro (&Qkeymapp);
1889
1890 defsubr (&Skeymapp);
1891 defsubr (&Smake_keymap);
1892 defsubr (&Smake_sparse_keymap);
1893 defsubr (&Scopy_keymap);
1894 defsubr (&Skey_binding);
1895 defsubr (&Slocal_key_binding);
1896 defsubr (&Sglobal_key_binding);
1897 defsubr (&Sminor_mode_key_binding);
1898 defsubr (&Sglobal_set_key);
1899 defsubr (&Slocal_set_key);
1900 defsubr (&Sdefine_key);
1901 defsubr (&Slookup_key);
1902 defsubr (&Sglobal_unset_key);
1903 defsubr (&Slocal_unset_key);
1904 defsubr (&Sdefine_prefix_command);
1905 defsubr (&Suse_global_map);
1906 defsubr (&Suse_local_map);
1907 defsubr (&Scurrent_local_map);
1908 defsubr (&Scurrent_global_map);
1909 defsubr (&Scurrent_minor_mode_maps);
1910 defsubr (&Saccessible_keymaps);
1911 defsubr (&Skey_description);
1912 defsubr (&Sdescribe_vector);
1913 defsubr (&Ssingle_key_description);
1914 defsubr (&Stext_char_description);
1915 defsubr (&Swhere_is_internal);
1916 defsubr (&Swhere_is);
1917 defsubr (&Sdescribe_bindings);
1918 defsubr (&Sapropos_internal);
1919 }
1920
1921 keys_of_keymap ()
1922 {
1923 Lisp_Object tem;
1924
1925 initial_define_key (global_map, 033, "ESC-prefix");
1926 initial_define_key (global_map, Ctl('X'), "Control-X-prefix");
1927 }