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