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