(current_minor_maps): `override' only shadows its
[bpt/emacs.git] / src / keymap.c
1 /* Manipulation of keymaps
2 Copyright (C) 1985, 86,87,88,93,94,95,98,99, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include <config.h>
24 #include <stdio.h>
25 #include "lisp.h"
26 #include "commands.h"
27 #include "buffer.h"
28 #include "charset.h"
29 #include "keyboard.h"
30 #include "termhooks.h"
31 #include "blockinput.h"
32 #include "puresize.h"
33 #include "intervals.h"
34 #include "keymap.h"
35
36 /* The number of elements in keymap vectors. */
37 #define DENSE_TABLE_SIZE (0200)
38
39 /* Actually allocate storage for these variables */
40
41 Lisp_Object current_global_map; /* Current global keymap */
42
43 Lisp_Object global_map; /* default global key bindings */
44
45 Lisp_Object meta_map; /* The keymap used for globally bound
46 ESC-prefixed default commands */
47
48 Lisp_Object control_x_map; /* The keymap used for globally bound
49 C-x-prefixed default commands */
50
51 /* was MinibufLocalMap */
52 Lisp_Object Vminibuffer_local_map;
53 /* The keymap used by the minibuf for local
54 bindings when spaces are allowed in the
55 minibuf */
56
57 /* was MinibufLocalNSMap */
58 Lisp_Object Vminibuffer_local_ns_map;
59 /* The keymap used by the minibuf for local
60 bindings when spaces are not encouraged
61 in the minibuf */
62
63 /* keymap used for minibuffers when doing completion */
64 /* was MinibufLocalCompletionMap */
65 Lisp_Object Vminibuffer_local_completion_map;
66
67 /* keymap used for minibuffers when doing completion and require a match */
68 /* was MinibufLocalMustMatchMap */
69 Lisp_Object Vminibuffer_local_must_match_map;
70
71 /* Alist of minor mode variables and keymaps. */
72 Lisp_Object Vminor_mode_map_alist;
73
74 /* Alist of major-mode-specific overrides for
75 minor mode variables and keymaps. */
76 Lisp_Object Vminor_mode_overriding_map_alist;
77
78 /* Keymap mapping ASCII function key sequences onto their preferred forms.
79 Initialized by the terminal-specific lisp files. See DEFVAR for more
80 documentation. */
81 Lisp_Object Vfunction_key_map;
82
83 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
84 Lisp_Object Vkey_translation_map;
85
86 /* A list of all commands given new bindings since a certain time
87 when nil was stored here.
88 This is used to speed up recomputation of menu key equivalents
89 when Emacs starts up. t means don't record anything here. */
90 Lisp_Object Vdefine_key_rebound_commands;
91
92 Lisp_Object Qkeymapp, Qkeymap, Qnon_ascii, Qmenu_item;
93
94 /* A char with the CHAR_META bit set in a vector or the 0200 bit set
95 in a string key sequence is equivalent to prefixing with this
96 character. */
97 extern Lisp_Object meta_prefix_char;
98
99 extern Lisp_Object Voverriding_local_map;
100
101 /* Hash table used to cache a reverse-map to speed up calls to where-is. */
102 static Lisp_Object where_is_cache;
103 /* Which keymaps are reverse-stored in the cache. */
104 static Lisp_Object where_is_cache_keymaps;
105
106 static Lisp_Object store_in_keymap P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
107 static void fix_submap_inheritance P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
108
109 static Lisp_Object define_as_prefix P_ ((Lisp_Object, Lisp_Object));
110 static void describe_command P_ ((Lisp_Object));
111 static void describe_translation P_ ((Lisp_Object));
112 static void describe_map P_ ((Lisp_Object, Lisp_Object,
113 void (*) P_ ((Lisp_Object)),
114 int, Lisp_Object, Lisp_Object*, int));
115 \f
116 /* Keymap object support - constructors and predicates. */
117
118 DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 1, 0,
119 "Construct and return a new keymap, of the form (keymap CHARTABLE . ALIST).\n\
120 CHARTABLE is a char-table that holds the bindings for the ASCII\n\
121 characters. ALIST is an assoc-list which holds bindings for function keys,\n\
122 mouse events, and any other things that appear in the input stream.\n\
123 All entries in it are initially nil, meaning \"command undefined\".\n\n\
124 The optional arg STRING supplies a menu name for the keymap\n\
125 in case you use it as a menu with `x-popup-menu'.")
126 (string)
127 Lisp_Object string;
128 {
129 Lisp_Object tail;
130 if (!NILP (string))
131 tail = Fcons (string, Qnil);
132 else
133 tail = Qnil;
134 return Fcons (Qkeymap,
135 Fcons (Fmake_char_table (Qkeymap, Qnil), tail));
136 }
137
138 DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 1, 0,
139 "Construct and return a new sparse keymap.\n\
140 Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),\n\
141 which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),\n\
142 which binds the function key or mouse event SYMBOL to DEFINITION.\n\
143 Initially the alist is nil.\n\n\
144 The optional arg STRING supplies a menu name for the keymap\n\
145 in case you use it as a menu with `x-popup-menu'.")
146 (string)
147 Lisp_Object string;
148 {
149 if (!NILP (string))
150 return Fcons (Qkeymap, Fcons (string, Qnil));
151 return Fcons (Qkeymap, Qnil);
152 }
153
154 /* This function is used for installing the standard key bindings
155 at initialization time.
156
157 For example:
158
159 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); */
160
161 void
162 initial_define_key (keymap, key, defname)
163 Lisp_Object keymap;
164 int key;
165 char *defname;
166 {
167 store_in_keymap (keymap, make_number (key), intern (defname));
168 }
169
170 void
171 initial_define_lispy_key (keymap, keyname, defname)
172 Lisp_Object keymap;
173 char *keyname;
174 char *defname;
175 {
176 store_in_keymap (keymap, intern (keyname), intern (defname));
177 }
178
179 DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0,
180 "Return t if OBJECT is a keymap.\n\
181 \n\
182 A keymap is a list (keymap . ALIST),\n\
183 or a symbol whose function definition is itself a keymap.\n\
184 ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);\n\
185 a vector of densely packed bindings for small character codes\n\
186 is also allowed as an element.")
187 (object)
188 Lisp_Object object;
189 {
190 return (KEYMAPP (object) ? Qt : Qnil);
191 }
192
193 DEFUN ("keymap-prompt", Fkeymap_prompt, Skeymap_prompt, 1, 1, 0,
194 "Return the prompt-string of a keymap MAP.\n\
195 If non-nil, the prompt is shown in the echo-area\n\
196 when reading a key-sequence to be looked-up in this keymap.")
197 (map)
198 Lisp_Object map;
199 {
200 while (CONSP (map))
201 {
202 register Lisp_Object tem;
203 tem = Fcar (map);
204 if (STRINGP (tem))
205 return tem;
206 map = Fcdr (map);
207 }
208 return Qnil;
209 }
210
211 /* Check that OBJECT is a keymap (after dereferencing through any
212 symbols). If it is, return it.
213
214 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
215 is an autoload form, do the autoload and try again.
216 If AUTOLOAD is nonzero, callers must assume GC is possible.
217
218 If the map needs to be autoloaded, but AUTOLOAD is zero (and ERROR
219 is zero as well), return Qt.
220
221 ERROR controls how we respond if OBJECT isn't a keymap.
222 If ERROR is non-zero, signal an error; otherwise, just return Qnil.
223
224 Note that most of the time, we don't want to pursue autoloads.
225 Functions like Faccessible_keymaps which scan entire keymap trees
226 shouldn't load every autoloaded keymap. I'm not sure about this,
227 but it seems to me that only read_key_sequence, Flookup_key, and
228 Fdefine_key should cause keymaps to be autoloaded.
229
230 This function can GC when AUTOLOAD is non-zero, because it calls
231 do_autoload which can GC. */
232
233 Lisp_Object
234 get_keymap (object, error, autoload)
235 Lisp_Object object;
236 int error, autoload;
237 {
238 Lisp_Object tem;
239
240 autoload_retry:
241 if (NILP (object))
242 goto end;
243 if (CONSP (object) && EQ (XCAR (object), Qkeymap))
244 return object;
245
246 tem = indirect_function (object);
247 if (CONSP (tem))
248 {
249 if (EQ (XCAR (tem), Qkeymap))
250 return tem;
251
252 /* Should we do an autoload? Autoload forms for keymaps have
253 Qkeymap as their fifth element. */
254 if ((autoload || !error) && EQ (XCAR (tem), Qautoload))
255 {
256 Lisp_Object tail;
257
258 tail = Fnth (make_number (4), tem);
259 if (EQ (tail, Qkeymap))
260 {
261 if (autoload)
262 {
263 struct gcpro gcpro1, gcpro2;
264
265 GCPRO2 (tem, object);
266 do_autoload (tem, object);
267 UNGCPRO;
268
269 goto autoload_retry;
270 }
271 else
272 return Qt;
273 }
274 }
275 }
276
277 end:
278 if (error)
279 wrong_type_argument (Qkeymapp, object);
280 return Qnil;
281 }
282 \f
283 /* Return the parent map of the keymap MAP, or nil if it has none.
284 We assume that MAP is a valid keymap. */
285
286 DEFUN ("keymap-parent", Fkeymap_parent, Skeymap_parent, 1, 1, 0,
287 "Return the parent keymap of KEYMAP.")
288 (keymap)
289 Lisp_Object keymap;
290 {
291 Lisp_Object list;
292
293 keymap = get_keymap (keymap, 1, 1);
294
295 /* Skip past the initial element `keymap'. */
296 list = XCDR (keymap);
297 for (; CONSP (list); list = XCDR (list))
298 {
299 /* See if there is another `keymap'. */
300 if (KEYMAPP (list))
301 return list;
302 }
303
304 return get_keymap (list, 0, 1);
305 }
306
307
308 /* Check whether MAP is one of MAPS parents. */
309 int
310 keymap_memberp (map, maps)
311 Lisp_Object map, maps;
312 {
313 if (NILP (map)) return 0;
314 while (KEYMAPP (maps) && !EQ (map, maps))
315 maps = Fkeymap_parent (maps);
316 return (EQ (map, maps));
317 }
318
319 /* Set the parent keymap of MAP to PARENT. */
320
321 DEFUN ("set-keymap-parent", Fset_keymap_parent, Sset_keymap_parent, 2, 2, 0,
322 "Modify KEYMAP to set its parent map to PARENT.\n\
323 PARENT should be nil or another keymap.")
324 (keymap, parent)
325 Lisp_Object keymap, parent;
326 {
327 Lisp_Object list, prev;
328 struct gcpro gcpro1;
329 int i;
330
331 /* Force a keymap flush for the next call to where-is.
332 Since this can be called from within where-is, we don't set where_is_cache
333 directly but only where_is_cache_keymaps, since where_is_cache shouldn't
334 be changed during where-is, while where_is_cache_keymaps is only used at
335 the very beginning of where-is and can thus be changed here without any
336 adverse effect.
337 This is a very minor correctness (rather than safety) issue. */
338 where_is_cache_keymaps = Qt;
339
340 keymap = get_keymap (keymap, 1, 1);
341 GCPRO1 (keymap);
342
343 if (!NILP (parent))
344 {
345 parent = get_keymap (parent, 1, 1);
346
347 /* Check for cycles. */
348 if (keymap_memberp (keymap, parent))
349 error ("Cyclic keymap inheritance");
350 }
351
352 /* Skip past the initial element `keymap'. */
353 prev = keymap;
354 while (1)
355 {
356 list = XCDR (prev);
357 /* If there is a parent keymap here, replace it.
358 If we came to the end, add the parent in PREV. */
359 if (!CONSP (list) || KEYMAPP (list))
360 {
361 /* If we already have the right parent, return now
362 so that we avoid the loops below. */
363 if (EQ (XCDR (prev), parent))
364 RETURN_UNGCPRO (parent);
365
366 XSETCDR (prev, parent);
367 break;
368 }
369 prev = list;
370 }
371
372 /* Scan through for submaps, and set their parents too. */
373
374 for (list = XCDR (keymap); CONSP (list); list = XCDR (list))
375 {
376 /* Stop the scan when we come to the parent. */
377 if (EQ (XCAR (list), Qkeymap))
378 break;
379
380 /* If this element holds a prefix map, deal with it. */
381 if (CONSP (XCAR (list))
382 && CONSP (XCDR (XCAR (list))))
383 fix_submap_inheritance (keymap, XCAR (XCAR (list)),
384 XCDR (XCAR (list)));
385
386 if (VECTORP (XCAR (list)))
387 for (i = 0; i < XVECTOR (XCAR (list))->size; i++)
388 if (CONSP (XVECTOR (XCAR (list))->contents[i]))
389 fix_submap_inheritance (keymap, make_number (i),
390 XVECTOR (XCAR (list))->contents[i]);
391
392 if (CHAR_TABLE_P (XCAR (list)))
393 {
394 Lisp_Object indices[3];
395
396 map_char_table (fix_submap_inheritance, Qnil, XCAR (list),
397 keymap, 0, indices);
398 }
399 }
400
401 RETURN_UNGCPRO (parent);
402 }
403
404 /* EVENT is defined in MAP as a prefix, and SUBMAP is its definition.
405 if EVENT is also a prefix in MAP's parent,
406 make sure that SUBMAP inherits that definition as its own parent. */
407
408 static void
409 fix_submap_inheritance (map, event, submap)
410 Lisp_Object map, event, submap;
411 {
412 Lisp_Object map_parent, parent_entry;
413
414 /* SUBMAP is a cons that we found as a key binding.
415 Discard the other things found in a menu key binding. */
416
417 submap = get_keymap (get_keyelt (submap, 0), 0, 0);
418
419 /* If it isn't a keymap now, there's no work to do. */
420 if (!CONSP (submap))
421 return;
422
423 map_parent = Fkeymap_parent (map);
424 if (!NILP (map_parent))
425 parent_entry =
426 get_keymap (access_keymap (map_parent, event, 0, 0, 0), 0, 0);
427 else
428 parent_entry = Qnil;
429
430 /* If MAP's parent has something other than a keymap,
431 our own submap shadows it completely. */
432 if (!CONSP (parent_entry))
433 return;
434
435 if (! EQ (parent_entry, submap))
436 {
437 Lisp_Object submap_parent;
438 submap_parent = submap;
439 while (1)
440 {
441 Lisp_Object tem;
442
443 tem = Fkeymap_parent (submap_parent);
444
445 if (KEYMAPP (tem))
446 {
447 if (keymap_memberp (tem, parent_entry))
448 /* Fset_keymap_parent could create a cycle. */
449 return;
450 submap_parent = tem;
451 }
452 else
453 break;
454 }
455 Fset_keymap_parent (submap_parent, parent_entry);
456 }
457 }
458 \f
459 /* Look up IDX in MAP. IDX may be any sort of event.
460 Note that this does only one level of lookup; IDX must be a single
461 event, not a sequence.
462
463 If T_OK is non-zero, bindings for Qt are treated as default
464 bindings; any key left unmentioned by other tables and bindings is
465 given the binding of Qt.
466
467 If T_OK is zero, bindings for Qt are not treated specially.
468
469 If NOINHERIT, don't accept a subkeymap found in an inherited keymap. */
470
471 Lisp_Object
472 access_keymap (map, idx, t_ok, noinherit, autoload)
473 Lisp_Object map;
474 Lisp_Object idx;
475 int t_ok;
476 int noinherit;
477 int autoload;
478 {
479 Lisp_Object val = Qunbound;
480
481 /* If idx is a list (some sort of mouse click, perhaps?),
482 the index we want to use is the car of the list, which
483 ought to be a symbol. */
484 idx = EVENT_HEAD (idx);
485
486 /* If idx is a symbol, it might have modifiers, which need to
487 be put in the canonical order. */
488 if (SYMBOLP (idx))
489 idx = reorder_modifiers (idx);
490 else if (INTEGERP (idx))
491 /* Clobber the high bits that can be present on a machine
492 with more than 24 bits of integer. */
493 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
494
495 /* Handle the special meta -> esc mapping. */
496 if (INTEGERP (idx) && XUINT (idx) & meta_modifier)
497 {
498 /* See if there is a meta-map. If there's none, there is
499 no binding for IDX, unless a default binding exists in MAP. */
500 Lisp_Object meta_map =
501 get_keymap (access_keymap (map, meta_prefix_char,
502 t_ok, noinherit, autoload),
503 0, autoload);
504 if (CONSP (meta_map))
505 {
506 map = meta_map;
507 idx = make_number (XUINT (idx) & ~meta_modifier);
508 }
509 else if (t_ok)
510 /* Set IDX to t, so that we only find a default binding. */
511 idx = Qt;
512 else
513 /* We know there is no binding. */
514 return Qnil;
515 }
516
517 {
518 Lisp_Object tail;
519 Lisp_Object t_binding = Qnil;
520
521 /* If `t_ok' is 2, both `t' and generic-char bindings are accepted.
522 If it is 1, only generic-char bindings are accepted.
523 Otherwise, neither are. */
524 t_ok = t_ok ? 2 : 0;
525
526 for (tail = XCDR (map);
527 (CONSP (tail)
528 || (tail = get_keymap (tail, 0, autoload), CONSP (tail)));
529 tail = XCDR (tail))
530 {
531 Lisp_Object binding;
532
533 binding = XCAR (tail);
534 if (SYMBOLP (binding))
535 {
536 /* If NOINHERIT, stop finding prefix definitions
537 after we pass a second occurrence of the `keymap' symbol. */
538 if (noinherit && EQ (binding, Qkeymap))
539 return Qnil;
540 }
541 else if (CONSP (binding))
542 {
543 Lisp_Object key = XCAR (binding);
544 int c1, c2, charset;
545
546 if (EQ (key, idx))
547 val = XCDR (binding);
548 else if (t_ok
549 && INTEGERP (idx)
550 && (XINT (idx) & CHAR_MODIFIER_MASK) == 0
551 && INTEGERP (key)
552 && (XINT (key) & CHAR_MODIFIER_MASK) == 0
553 && !SINGLE_BYTE_CHAR_P (XINT (idx))
554 && !SINGLE_BYTE_CHAR_P (XINT (key))
555 && CHAR_VALID_P (XINT (key), 1)
556 && !CHAR_VALID_P (XINT (key), 0)
557 && (CHAR_CHARSET (XINT (key))
558 == CHAR_CHARSET (XINT (idx))))
559 {
560 /* KEY is the generic character of the charset of IDX.
561 Use KEY's binding if there isn't a binding for IDX
562 itself. */
563 t_binding = XCDR (binding);
564 t_ok = 0;
565 }
566 else if (t_ok > 1 && EQ (key, Qt))
567 {
568 t_binding = XCDR (binding);
569 t_ok = 1;
570 }
571 }
572 else if (VECTORP (binding))
573 {
574 if (NATNUMP (idx) && XFASTINT (idx) < ASIZE (binding))
575 val = AREF (binding, XFASTINT (idx));
576 }
577 else if (CHAR_TABLE_P (binding))
578 {
579 /* Character codes with modifiers
580 are not included in a char-table.
581 All character codes without modifiers are included. */
582 if (NATNUMP (idx)
583 && (XFASTINT (idx) & CHAR_MODIFIER_MASK) == 0)
584 val = Faref (binding, idx);
585 }
586
587 /* If we found a binding, clean it up and return it. */
588 if (!EQ (val, Qunbound))
589 {
590 val = get_keyelt (val, autoload);
591 if (KEYMAPP (val))
592 fix_submap_inheritance (map, idx, val);
593 return val;
594 }
595 QUIT;
596 }
597
598 return get_keyelt (t_binding, autoload);
599 }
600 }
601
602 /* Given OBJECT which was found in a slot in a keymap,
603 trace indirect definitions to get the actual definition of that slot.
604 An indirect definition is a list of the form
605 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
606 and INDEX is the object to look up in KEYMAP to yield the definition.
607
608 Also if OBJECT has a menu string as the first element,
609 remove that. Also remove a menu help string as second element.
610
611 If AUTOLOAD is nonzero, load autoloadable keymaps
612 that are referred to with indirection. */
613
614 Lisp_Object
615 get_keyelt (object, autoload)
616 register Lisp_Object object;
617 int autoload;
618 {
619 while (1)
620 {
621 if (!(CONSP (object)))
622 /* This is really the value. */
623 return object;
624
625 /* If the keymap contents looks like (keymap ...) or (lambda ...)
626 then use itself. */
627 else if (EQ (XCAR (object), Qkeymap) || EQ (XCAR (object), Qlambda))
628 return object;
629
630 /* If the keymap contents looks like (menu-item name . DEFN)
631 or (menu-item name DEFN ...) then use DEFN.
632 This is a new format menu item. */
633 else if (EQ (XCAR (object), Qmenu_item))
634 {
635 if (CONSP (XCDR (object)))
636 {
637 Lisp_Object tem;
638
639 object = XCDR (XCDR (object));
640 tem = object;
641 if (CONSP (object))
642 object = XCAR (object);
643
644 /* If there's a `:filter FILTER', apply FILTER to the
645 menu-item's definition to get the real definition to
646 use. */
647 for (; CONSP (tem) && CONSP (XCDR (tem)); tem = XCDR (tem))
648 if (EQ (XCAR (tem), QCfilter) && autoload)
649 {
650 Lisp_Object filter;
651 filter = XCAR (XCDR (tem));
652 filter = list2 (filter, list2 (Qquote, object));
653 object = menu_item_eval_property (filter);
654 break;
655 }
656 }
657 else
658 /* Invalid keymap */
659 return object;
660 }
661
662 /* If the keymap contents looks like (STRING . DEFN), use DEFN.
663 Keymap alist elements like (CHAR MENUSTRING . DEFN)
664 will be used by HierarKey menus. */
665 else if (STRINGP (XCAR (object)))
666 {
667 object = XCDR (object);
668 /* Also remove a menu help string, if any,
669 following the menu item name. */
670 if (CONSP (object) && STRINGP (XCAR (object)))
671 object = XCDR (object);
672 /* Also remove the sublist that caches key equivalences, if any. */
673 if (CONSP (object) && CONSP (XCAR (object)))
674 {
675 Lisp_Object carcar;
676 carcar = XCAR (XCAR (object));
677 if (NILP (carcar) || VECTORP (carcar))
678 object = XCDR (object);
679 }
680 }
681
682 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
683 else
684 {
685 Lisp_Object map;
686 map = get_keymap (Fcar_safe (object), 0, autoload);
687 return (!CONSP (map) ? object /* Invalid keymap */
688 : access_keymap (map, Fcdr (object), 0, 0, autoload));
689 }
690 }
691 }
692
693 static Lisp_Object
694 store_in_keymap (keymap, idx, def)
695 Lisp_Object keymap;
696 register Lisp_Object idx;
697 register Lisp_Object def;
698 {
699 /* Flush any reverse-map cache. */
700 where_is_cache = Qnil;
701 where_is_cache_keymaps = Qt;
702
703 /* If we are preparing to dump, and DEF is a menu element
704 with a menu item indicator, copy it to ensure it is not pure. */
705 if (CONSP (def) && PURE_P (def)
706 && (EQ (XCAR (def), Qmenu_item) || STRINGP (XCAR (def))))
707 def = Fcons (XCAR (def), XCDR (def));
708
709 if (!CONSP (keymap) || !EQ (XCAR (keymap), Qkeymap))
710 error ("attempt to define a key in a non-keymap");
711
712 /* If idx is a list (some sort of mouse click, perhaps?),
713 the index we want to use is the car of the list, which
714 ought to be a symbol. */
715 idx = EVENT_HEAD (idx);
716
717 /* If idx is a symbol, it might have modifiers, which need to
718 be put in the canonical order. */
719 if (SYMBOLP (idx))
720 idx = reorder_modifiers (idx);
721 else if (INTEGERP (idx))
722 /* Clobber the high bits that can be present on a machine
723 with more than 24 bits of integer. */
724 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
725
726 /* Scan the keymap for a binding of idx. */
727 {
728 Lisp_Object tail;
729
730 /* The cons after which we should insert new bindings. If the
731 keymap has a table element, we record its position here, so new
732 bindings will go after it; this way, the table will stay
733 towards the front of the alist and character lookups in dense
734 keymaps will remain fast. Otherwise, this just points at the
735 front of the keymap. */
736 Lisp_Object insertion_point;
737
738 insertion_point = keymap;
739 for (tail = XCDR (keymap); CONSP (tail); tail = XCDR (tail))
740 {
741 Lisp_Object elt;
742
743 elt = XCAR (tail);
744 if (VECTORP (elt))
745 {
746 if (NATNUMP (idx) && XFASTINT (idx) < ASIZE (elt))
747 {
748 ASET (elt, XFASTINT (idx), def);
749 return def;
750 }
751 insertion_point = tail;
752 }
753 else if (CHAR_TABLE_P (elt))
754 {
755 /* Character codes with modifiers
756 are not included in a char-table.
757 All character codes without modifiers are included. */
758 if (NATNUMP (idx)
759 && ! (XFASTINT (idx)
760 & (CHAR_ALT | CHAR_SUPER | CHAR_HYPER
761 | CHAR_SHIFT | CHAR_CTL | CHAR_META)))
762 {
763 Faset (elt, idx, def);
764 return def;
765 }
766 insertion_point = tail;
767 }
768 else if (CONSP (elt))
769 {
770 if (EQ (idx, XCAR (elt)))
771 {
772 XSETCDR (elt, def);
773 return def;
774 }
775 }
776 else if (EQ (elt, Qkeymap))
777 /* If we find a 'keymap' symbol in the spine of KEYMAP,
778 then we must have found the start of a second keymap
779 being used as the tail of KEYMAP, and a binding for IDX
780 should be inserted before it. */
781 goto keymap_end;
782
783 QUIT;
784 }
785
786 keymap_end:
787 /* We have scanned the entire keymap, and not found a binding for
788 IDX. Let's add one. */
789 XSETCDR (insertion_point,
790 Fcons (Fcons (idx, def), XCDR (insertion_point)));
791 }
792
793 return def;
794 }
795
796 EXFUN (Fcopy_keymap, 1);
797
798 void
799 copy_keymap_1 (chartable, idx, elt)
800 Lisp_Object chartable, idx, elt;
801 {
802 if (CONSP (elt) && EQ (XCAR (elt), Qkeymap))
803 Faset (chartable, idx, Fcopy_keymap (elt));
804 }
805
806 DEFUN ("copy-keymap", Fcopy_keymap, Scopy_keymap, 1, 1, 0,
807 "Return a copy of the keymap KEYMAP.\n\
808 The copy starts out with the same definitions of KEYMAP,\n\
809 but changing either the copy or KEYMAP does not affect the other.\n\
810 Any key definitions that are subkeymaps are recursively copied.\n\
811 However, a key definition which is a symbol whose definition is a keymap\n\
812 is not copied.")
813 (keymap)
814 Lisp_Object keymap;
815 {
816 /* FIXME: This doesn't properly copy menu-items in vectors. */
817 /* FIXME: This also copies the parent keymap. */
818
819 register Lisp_Object copy, tail;
820
821 copy = Fcopy_alist (get_keymap (keymap, 1, 0));
822
823 for (tail = copy; CONSP (tail); tail = XCDR (tail))
824 {
825 Lisp_Object elt;
826
827 elt = XCAR (tail);
828 if (CHAR_TABLE_P (elt))
829 {
830 Lisp_Object indices[3];
831
832 elt = Fcopy_sequence (elt);
833 XSETCAR (tail, elt);
834
835 map_char_table (copy_keymap_1, Qnil, elt, elt, 0, indices);
836 }
837 else if (VECTORP (elt))
838 {
839 int i;
840
841 elt = Fcopy_sequence (elt);
842 XSETCAR (tail, elt);
843
844 for (i = 0; i < ASIZE (elt); i++)
845 if (CONSP (AREF (elt, i)) && EQ (XCAR (AREF (elt, i)), Qkeymap))
846 ASET (elt, i, Fcopy_keymap (AREF (elt, i)));
847 }
848 else if (CONSP (elt) && CONSP (XCDR (elt)))
849 {
850 Lisp_Object tem;
851 tem = XCDR (elt);
852
853 /* Is this a new format menu item. */
854 if (EQ (XCAR (tem),Qmenu_item))
855 {
856 /* Copy cell with menu-item marker. */
857 XSETCDR (elt,
858 Fcons (XCAR (tem), XCDR (tem)));
859 elt = XCDR (elt);
860 tem = XCDR (elt);
861 if (CONSP (tem))
862 {
863 /* Copy cell with menu-item name. */
864 XSETCDR (elt,
865 Fcons (XCAR (tem), XCDR (tem)));
866 elt = XCDR (elt);
867 tem = XCDR (elt);
868 };
869 if (CONSP (tem))
870 {
871 /* Copy cell with binding and if the binding is a keymap,
872 copy that. */
873 XSETCDR (elt,
874 Fcons (XCAR (tem), XCDR (tem)));
875 elt = XCDR (elt);
876 tem = XCAR (elt);
877 if (CONSP (tem) && EQ (XCAR (tem), Qkeymap))
878 XSETCAR (elt, Fcopy_keymap (tem));
879 tem = XCDR (elt);
880 if (CONSP (tem) && CONSP (XCAR (tem)))
881 /* Delete cache for key equivalences. */
882 XSETCDR (elt, XCDR (tem));
883 }
884 }
885 else
886 {
887 /* It may be an old fomat menu item.
888 Skip the optional menu string.
889 */
890 if (STRINGP (XCAR (tem)))
891 {
892 /* Copy the cell, since copy-alist didn't go this deep. */
893 XSETCDR (elt,
894 Fcons (XCAR (tem), XCDR (tem)));
895 elt = XCDR (elt);
896 tem = XCDR (elt);
897 /* Also skip the optional menu help string. */
898 if (CONSP (tem) && STRINGP (XCAR (tem)))
899 {
900 XSETCDR (elt,
901 Fcons (XCAR (tem), XCDR (tem)));
902 elt = XCDR (elt);
903 tem = XCDR (elt);
904 }
905 /* There may also be a list that caches key equivalences.
906 Just delete it for the new keymap. */
907 if (CONSP (tem)
908 && CONSP (XCAR (tem))
909 && (NILP (XCAR (XCAR (tem)))
910 || VECTORP (XCAR (XCAR (tem)))))
911 XSETCDR (elt, XCDR (tem));
912 }
913 if (CONSP (elt)
914 && CONSP (XCDR (elt))
915 && EQ (XCAR (XCDR (elt)), Qkeymap))
916 XSETCDR (elt, Fcopy_keymap (XCDR (elt)));
917 }
918
919 }
920 }
921
922 return copy;
923 }
924 \f
925 /* Simple Keymap mutators and accessors. */
926
927 /* GC is possible in this function if it autoloads a keymap. */
928
929 DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0,
930 "Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.\n\
931 KEYMAP is a keymap. KEY is a string or a vector of symbols and characters\n\
932 meaning a sequence of keystrokes and events.\n\
933 Non-ASCII characters with codes above 127 (such as ISO Latin-1)\n\
934 can be included if you use a vector.\n\
935 DEF is anything that can be a key's definition:\n\
936 nil (means key is undefined in this keymap),\n\
937 a command (a Lisp function suitable for interactive calling)\n\
938 a string (treated as a keyboard macro),\n\
939 a keymap (to define a prefix key),\n\
940 a symbol. When the key is looked up, the symbol will stand for its\n\
941 function definition, which should at that time be one of the above,\n\
942 or another symbol whose function definition is used, etc.\n\
943 a cons (STRING . DEFN), meaning that DEFN is the definition\n\
944 (DEFN should be a valid definition in its own right),\n\
945 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.\n\
946 \n\
947 If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at\n\
948 the front of KEYMAP.")
949 (keymap, key, def)
950 Lisp_Object keymap;
951 Lisp_Object key;
952 Lisp_Object def;
953 {
954 register int idx;
955 register Lisp_Object c;
956 register Lisp_Object cmd;
957 int metized = 0;
958 int meta_bit;
959 int length;
960 struct gcpro gcpro1, gcpro2, gcpro3;
961
962 keymap = get_keymap (keymap, 1, 1);
963
964 if (!VECTORP (key) && !STRINGP (key))
965 key = wrong_type_argument (Qarrayp, key);
966
967 length = XFASTINT (Flength (key));
968 if (length == 0)
969 return Qnil;
970
971 if (SYMBOLP (def) && !EQ (Vdefine_key_rebound_commands, Qt))
972 Vdefine_key_rebound_commands = Fcons (def, Vdefine_key_rebound_commands);
973
974 GCPRO3 (keymap, key, def);
975
976 if (VECTORP (key))
977 meta_bit = meta_modifier;
978 else
979 meta_bit = 0x80;
980
981 idx = 0;
982 while (1)
983 {
984 c = Faref (key, make_number (idx));
985
986 if (CONSP (c) && lucid_event_type_list_p (c))
987 c = Fevent_convert_list (c);
988
989 if (INTEGERP (c)
990 && (XINT (c) & meta_bit)
991 && !metized)
992 {
993 c = meta_prefix_char;
994 metized = 1;
995 }
996 else
997 {
998 if (INTEGERP (c))
999 XSETINT (c, XINT (c) & ~meta_bit);
1000
1001 metized = 0;
1002 idx++;
1003 }
1004
1005 if (!INTEGERP (c) && !SYMBOLP (c) && !CONSP (c))
1006 error ("Key sequence contains invalid events");
1007
1008 if (idx == length)
1009 RETURN_UNGCPRO (store_in_keymap (keymap, c, def));
1010
1011 cmd = access_keymap (keymap, c, 0, 1, 1);
1012
1013 /* If this key is undefined, make it a prefix. */
1014 if (NILP (cmd))
1015 cmd = define_as_prefix (keymap, c);
1016
1017 keymap = get_keymap (cmd, 0, 1);
1018 if (!CONSP (keymap))
1019 /* We must use Fkey_description rather than just passing key to
1020 error; key might be a vector, not a string. */
1021 error ("Key sequence %s uses invalid prefix characters",
1022 XSTRING (Fkey_description (key))->data);
1023 }
1024 }
1025
1026 /* Value is number if KEY is too long; NIL if valid but has no definition. */
1027 /* GC is possible in this function if it autoloads a keymap. */
1028
1029 DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 3, 0,
1030 "In keymap KEYMAP, look up key sequence KEY. Return the definition.\n\
1031 nil means undefined. See doc of `define-key' for kinds of definitions.\n\
1032 \n\
1033 A number as value means KEY is \"too long\";\n\
1034 that is, characters or symbols in it except for the last one\n\
1035 fail to be a valid sequence of prefix characters in KEYMAP.\n\
1036 The number is how many characters at the front of KEY\n\
1037 it takes to reach a non-prefix command.\n\
1038 \n\
1039 Normally, `lookup-key' ignores bindings for t, which act as default\n\
1040 bindings, used when nothing else in the keymap applies; this makes it\n\
1041 usable as a general function for probing keymaps. However, if the\n\
1042 third optional argument ACCEPT-DEFAULT is non-nil, `lookup-key' will\n\
1043 recognize the default bindings, just as `read-key-sequence' does.")
1044 (keymap, key, accept_default)
1045 register Lisp_Object keymap;
1046 Lisp_Object key;
1047 Lisp_Object accept_default;
1048 {
1049 register int idx;
1050 register Lisp_Object cmd;
1051 register Lisp_Object c;
1052 int length;
1053 int t_ok = !NILP (accept_default);
1054 struct gcpro gcpro1;
1055
1056 keymap = get_keymap (keymap, 1, 1);
1057
1058 if (!VECTORP (key) && !STRINGP (key))
1059 key = wrong_type_argument (Qarrayp, key);
1060
1061 length = XFASTINT (Flength (key));
1062 if (length == 0)
1063 return keymap;
1064
1065 GCPRO1 (key);
1066
1067 idx = 0;
1068 while (1)
1069 {
1070 c = Faref (key, make_number (idx++));
1071
1072 if (CONSP (c) && lucid_event_type_list_p (c))
1073 c = Fevent_convert_list (c);
1074
1075 /* Turn the 8th bit of string chars into a meta modifier. */
1076 if (XINT (c) & 0x80 && STRINGP (key))
1077 XSETINT (c, (XINT (c) | meta_modifier) & ~0x80);
1078
1079 cmd = access_keymap (keymap, c, t_ok, 0, 1);
1080 if (idx == length)
1081 RETURN_UNGCPRO (cmd);
1082
1083 keymap = get_keymap (cmd, 0, 1);
1084 if (!CONSP (keymap))
1085 RETURN_UNGCPRO (make_number (idx));
1086
1087 QUIT;
1088 }
1089 }
1090
1091 /* Make KEYMAP define event C as a keymap (i.e., as a prefix).
1092 Assume that currently it does not define C at all.
1093 Return the keymap. */
1094
1095 static Lisp_Object
1096 define_as_prefix (keymap, c)
1097 Lisp_Object keymap, c;
1098 {
1099 Lisp_Object cmd;
1100
1101 cmd = Fmake_sparse_keymap (Qnil);
1102 /* If this key is defined as a prefix in an inherited keymap,
1103 make it a prefix in this map, and make its definition
1104 inherit the other prefix definition. */
1105 cmd = nconc2 (cmd, access_keymap (keymap, c, 0, 0, 0));
1106 store_in_keymap (keymap, c, cmd);
1107
1108 return cmd;
1109 }
1110
1111 /* Append a key to the end of a key sequence. We always make a vector. */
1112
1113 Lisp_Object
1114 append_key (key_sequence, key)
1115 Lisp_Object key_sequence, key;
1116 {
1117 Lisp_Object args[2];
1118
1119 args[0] = key_sequence;
1120
1121 args[1] = Fcons (key, Qnil);
1122 return Fvconcat (2, args);
1123 }
1124
1125 \f
1126 /* Global, local, and minor mode keymap stuff. */
1127
1128 /* We can't put these variables inside current_minor_maps, since under
1129 some systems, static gets macro-defined to be the empty string.
1130 Ickypoo. */
1131 static Lisp_Object *cmm_modes, *cmm_maps;
1132 static int cmm_size;
1133
1134 /* Error handler used in current_minor_maps. */
1135 static Lisp_Object
1136 current_minor_maps_error ()
1137 {
1138 return Qnil;
1139 }
1140
1141 /* Store a pointer to an array of the keymaps of the currently active
1142 minor modes in *buf, and return the number of maps it contains.
1143
1144 This function always returns a pointer to the same buffer, and may
1145 free or reallocate it, so if you want to keep it for a long time or
1146 hand it out to lisp code, copy it. This procedure will be called
1147 for every key sequence read, so the nice lispy approach (return a
1148 new assoclist, list, what have you) for each invocation would
1149 result in a lot of consing over time.
1150
1151 If we used xrealloc/xmalloc and ran out of memory, they would throw
1152 back to the command loop, which would try to read a key sequence,
1153 which would call this function again, resulting in an infinite
1154 loop. Instead, we'll use realloc/malloc and silently truncate the
1155 list, let the key sequence be read, and hope some other piece of
1156 code signals the error. */
1157 int
1158 current_minor_maps (modeptr, mapptr)
1159 Lisp_Object **modeptr, **mapptr;
1160 {
1161 int i = 0;
1162 int list_number = 0;
1163 Lisp_Object alist, assoc, var, val;
1164 Lisp_Object lists[2];
1165
1166 lists[0] = Vminor_mode_overriding_map_alist;
1167 lists[1] = Vminor_mode_map_alist;
1168
1169 for (list_number = 0; list_number < 2; list_number++)
1170 for (alist = lists[list_number];
1171 CONSP (alist);
1172 alist = XCDR (alist))
1173 if ((assoc = XCAR (alist), CONSP (assoc))
1174 && (var = XCAR (assoc), SYMBOLP (var))
1175 && (val = find_symbol_value (var), !EQ (val, Qunbound))
1176 && !NILP (val))
1177 {
1178 Lisp_Object temp;
1179
1180 /* If a variable has an entry in Vminor_mode_overriding_map_alist,
1181 and also an entry in Vminor_mode_map_alist,
1182 ignore the latter. */
1183 if (list_number == 1)
1184 {
1185 val = assq_no_quit (var, lists[0]);
1186 if (!NILP (val))
1187 continue;
1188 }
1189
1190 if (i >= cmm_size)
1191 {
1192 Lisp_Object *newmodes, *newmaps;
1193
1194 /* Use malloc/realloc here. See the comment above
1195 this function. */
1196 if (cmm_maps)
1197 {
1198 BLOCK_INPUT;
1199 cmm_size *= 2;
1200 newmodes
1201 = (Lisp_Object *) realloc (cmm_modes,
1202 cmm_size * sizeof *newmodes);
1203 newmaps
1204 = (Lisp_Object *) realloc (cmm_maps,
1205 cmm_size * sizeof *newmaps);
1206 UNBLOCK_INPUT;
1207 }
1208 else
1209 {
1210 BLOCK_INPUT;
1211 cmm_size = 30;
1212 newmodes
1213 = (Lisp_Object *) malloc (cmm_size * sizeof *newmodes);
1214 newmaps
1215 = (Lisp_Object *) malloc (cmm_size * sizeof *newmaps);
1216 UNBLOCK_INPUT;
1217 }
1218
1219 if (newmodes)
1220 cmm_modes = newmodes;
1221 if (newmaps)
1222 cmm_maps = newmaps;
1223
1224 if (newmodes == NULL || newmaps == NULL)
1225 break;
1226 }
1227
1228 /* Get the keymap definition--or nil if it is not defined. */
1229 temp = internal_condition_case_1 (Findirect_function,
1230 XCDR (assoc),
1231 Qerror, current_minor_maps_error);
1232 if (!NILP (temp))
1233 {
1234 cmm_modes[i] = var;
1235 cmm_maps [i] = temp;
1236 i++;
1237 }
1238 }
1239
1240 if (modeptr) *modeptr = cmm_modes;
1241 if (mapptr) *mapptr = cmm_maps;
1242 return i;
1243 }
1244
1245 DEFUN ("current-active-maps", Fcurrent_active_maps, Scurrent_active_maps,
1246 0, 1, 0,
1247 "Return a list of the currently active keymaps.\n\
1248 OLP if non-nil indicates that we should obey `overriding-local-map' and\n\
1249 `overriding-terminal-local-map'.")
1250 (olp)
1251 Lisp_Object olp;
1252 {
1253 Lisp_Object keymaps = Fcons (current_global_map, Qnil);
1254
1255 if (!NILP (olp))
1256 {
1257 if (!NILP (Voverriding_local_map))
1258 keymaps = Fcons (Voverriding_local_map, keymaps);
1259 if (!NILP (current_kboard->Voverriding_terminal_local_map))
1260 keymaps = Fcons (current_kboard->Voverriding_terminal_local_map, keymaps);
1261 }
1262 if (NILP (XCDR (keymaps)))
1263 {
1264 Lisp_Object local;
1265 Lisp_Object *maps;
1266 int nmaps, i;
1267
1268 local = get_local_map (PT, current_buffer, Qlocal_map);
1269 if (!NILP (local))
1270 keymaps = Fcons (local, keymaps);
1271
1272 local = get_local_map (PT, current_buffer, Qkeymap);
1273 if (!NILP (local))
1274 keymaps = Fcons (local, keymaps);
1275
1276 nmaps = current_minor_maps (0, &maps);
1277
1278 for (i = --nmaps; i >= 0; i--)
1279 if (!NILP (maps[i]))
1280 keymaps = Fcons (maps[i], keymaps);
1281 }
1282
1283 return keymaps;
1284 }
1285
1286 /* GC is possible in this function if it autoloads a keymap. */
1287
1288 DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 2, 0,
1289 "Return the binding for command KEY in current keymaps.\n\
1290 KEY is a string or vector, a sequence of keystrokes.\n\
1291 The binding is probably a symbol with a function definition.\n\
1292 \n\
1293 Normally, `key-binding' ignores bindings for t, which act as default\n\
1294 bindings, used when nothing else in the keymap applies; this makes it\n\
1295 usable as a general function for probing keymaps. However, if the\n\
1296 optional second argument ACCEPT-DEFAULT is non-nil, `key-binding' does\n\
1297 recognize the default bindings, just as `read-key-sequence' does.")
1298 (key, accept_default)
1299 Lisp_Object key, accept_default;
1300 {
1301 Lisp_Object *maps, value;
1302 int nmaps, i;
1303 struct gcpro gcpro1;
1304
1305 GCPRO1 (key);
1306
1307 if (!NILP (current_kboard->Voverriding_terminal_local_map))
1308 {
1309 value = Flookup_key (current_kboard->Voverriding_terminal_local_map,
1310 key, accept_default);
1311 if (! NILP (value) && !INTEGERP (value))
1312 RETURN_UNGCPRO (value);
1313 }
1314 else if (!NILP (Voverriding_local_map))
1315 {
1316 value = Flookup_key (Voverriding_local_map, key, accept_default);
1317 if (! NILP (value) && !INTEGERP (value))
1318 RETURN_UNGCPRO (value);
1319 }
1320 else
1321 {
1322 Lisp_Object local;
1323
1324 nmaps = current_minor_maps (0, &maps);
1325 /* Note that all these maps are GCPRO'd
1326 in the places where we found them. */
1327
1328 for (i = 0; i < nmaps; i++)
1329 if (! NILP (maps[i]))
1330 {
1331 value = Flookup_key (maps[i], key, accept_default);
1332 if (! NILP (value) && !INTEGERP (value))
1333 RETURN_UNGCPRO (value);
1334 }
1335
1336 local = get_local_map (PT, current_buffer, Qkeymap);
1337 if (! NILP (local))
1338 {
1339 value = Flookup_key (local, key, accept_default);
1340 if (! NILP (value) && !INTEGERP (value))
1341 RETURN_UNGCPRO (value);
1342 }
1343
1344 local = get_local_map (PT, current_buffer, Qlocal_map);
1345
1346 if (! NILP (local))
1347 {
1348 value = Flookup_key (local, key, accept_default);
1349 if (! NILP (value) && !INTEGERP (value))
1350 RETURN_UNGCPRO (value);
1351 }
1352 }
1353
1354 value = Flookup_key (current_global_map, key, accept_default);
1355 UNGCPRO;
1356 if (! NILP (value) && !INTEGERP (value))
1357 return value;
1358
1359 return Qnil;
1360 }
1361
1362 /* GC is possible in this function if it autoloads a keymap. */
1363
1364 DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 2, 0,
1365 "Return the binding for command KEYS in current local keymap only.\n\
1366 KEYS is a string, a sequence of keystrokes.\n\
1367 The binding is probably a symbol with a function definition.\n\
1368 \n\
1369 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1370 bindings; see the description of `lookup-key' for more details about this.")
1371 (keys, accept_default)
1372 Lisp_Object keys, accept_default;
1373 {
1374 register Lisp_Object map;
1375 map = current_buffer->keymap;
1376 if (NILP (map))
1377 return Qnil;
1378 return Flookup_key (map, keys, accept_default);
1379 }
1380
1381 /* GC is possible in this function if it autoloads a keymap. */
1382
1383 DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 2, 0,
1384 "Return the binding for command KEYS in current global keymap only.\n\
1385 KEYS is a string, a sequence of keystrokes.\n\
1386 The binding is probably a symbol with a function definition.\n\
1387 This function's return values are the same as those of lookup-key\n\
1388 \(which see).\n\
1389 \n\
1390 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1391 bindings; see the description of `lookup-key' for more details about this.")
1392 (keys, accept_default)
1393 Lisp_Object keys, accept_default;
1394 {
1395 return Flookup_key (current_global_map, keys, accept_default);
1396 }
1397
1398 /* GC is possible in this function if it autoloads a keymap. */
1399
1400 DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0,
1401 "Find the visible minor mode bindings of KEY.\n\
1402 Return an alist of pairs (MODENAME . BINDING), where MODENAME is the\n\
1403 the symbol which names the minor mode binding KEY, and BINDING is\n\
1404 KEY's definition in that mode. In particular, if KEY has no\n\
1405 minor-mode bindings, return nil. If the first binding is a\n\
1406 non-prefix, all subsequent bindings will be omitted, since they would\n\
1407 be ignored. Similarly, the list doesn't include non-prefix bindings\n\
1408 that come after prefix bindings.\n\
1409 \n\
1410 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1411 bindings; see the description of `lookup-key' for more details about this.")
1412 (key, accept_default)
1413 Lisp_Object key, accept_default;
1414 {
1415 Lisp_Object *modes, *maps;
1416 int nmaps;
1417 Lisp_Object binding;
1418 int i, j;
1419 struct gcpro gcpro1, gcpro2;
1420
1421 nmaps = current_minor_maps (&modes, &maps);
1422 /* Note that all these maps are GCPRO'd
1423 in the places where we found them. */
1424
1425 binding = Qnil;
1426 GCPRO2 (key, binding);
1427
1428 for (i = j = 0; i < nmaps; i++)
1429 if (!NILP (maps[i])
1430 && !NILP (binding = Flookup_key (maps[i], key, accept_default))
1431 && !INTEGERP (binding))
1432 {
1433 if (KEYMAPP (binding))
1434 maps[j++] = Fcons (modes[i], binding);
1435 else if (j == 0)
1436 RETURN_UNGCPRO (Fcons (Fcons (modes[i], binding), Qnil));
1437 }
1438
1439 UNGCPRO;
1440 return Flist (j, maps);
1441 }
1442
1443 DEFUN ("define-prefix-command", Fdefine_prefix_command, Sdefine_prefix_command, 1, 3, 0,
1444 "Define COMMAND as a prefix command. COMMAND should be a symbol.\n\
1445 A new sparse keymap is stored as COMMAND's function definition and its value.\n\
1446 If a second optional argument MAPVAR is given, the map is stored as\n\
1447 its value instead of as COMMAND's value; but COMMAND is still defined\n\
1448 as a function.\n\
1449 The third optional argument NAME, if given, supplies a menu name\n\
1450 string for the map. This is required to use the keymap as a menu.")
1451 (command, mapvar, name)
1452 Lisp_Object command, mapvar, name;
1453 {
1454 Lisp_Object map;
1455 map = Fmake_sparse_keymap (name);
1456 Ffset (command, map);
1457 if (!NILP (mapvar))
1458 Fset (mapvar, map);
1459 else
1460 Fset (command, map);
1461 return command;
1462 }
1463
1464 DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0,
1465 "Select KEYMAP as the global keymap.")
1466 (keymap)
1467 Lisp_Object keymap;
1468 {
1469 keymap = get_keymap (keymap, 1, 1);
1470 current_global_map = keymap;
1471
1472 return Qnil;
1473 }
1474
1475 DEFUN ("use-local-map", Fuse_local_map, Suse_local_map, 1, 1, 0,
1476 "Select KEYMAP as the local keymap.\n\
1477 If KEYMAP is nil, that means no local keymap.")
1478 (keymap)
1479 Lisp_Object keymap;
1480 {
1481 if (!NILP (keymap))
1482 keymap = get_keymap (keymap, 1, 1);
1483
1484 current_buffer->keymap = keymap;
1485
1486 return Qnil;
1487 }
1488
1489 DEFUN ("current-local-map", Fcurrent_local_map, Scurrent_local_map, 0, 0, 0,
1490 "Return current buffer's local keymap, or nil if it has none.")
1491 ()
1492 {
1493 return current_buffer->keymap;
1494 }
1495
1496 DEFUN ("current-global-map", Fcurrent_global_map, Scurrent_global_map, 0, 0, 0,
1497 "Return the current global keymap.")
1498 ()
1499 {
1500 return current_global_map;
1501 }
1502
1503 DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps, Scurrent_minor_mode_maps, 0, 0, 0,
1504 "Return a list of keymaps for the minor modes of the current buffer.")
1505 ()
1506 {
1507 Lisp_Object *maps;
1508 int nmaps = current_minor_maps (0, &maps);
1509
1510 return Flist (nmaps, maps);
1511 }
1512 \f
1513 /* Help functions for describing and documenting keymaps. */
1514
1515
1516 static void
1517 accessible_keymaps_1 (key, cmd, maps, tail, thisseq, is_metized)
1518 Lisp_Object maps, tail, thisseq, key, cmd;
1519 int is_metized; /* If 1, `key' is assumed to be INTEGERP. */
1520 {
1521 Lisp_Object tem;
1522
1523 cmd = get_keyelt (cmd, 0);
1524 if (NILP (cmd))
1525 return;
1526
1527 tem = get_keymap (cmd, 0, 0);
1528 if (CONSP (tem))
1529 {
1530 cmd = tem;
1531 /* Ignore keymaps that are already added to maps. */
1532 tem = Frassq (cmd, maps);
1533 if (NILP (tem))
1534 {
1535 /* If the last key in thisseq is meta-prefix-char,
1536 turn it into a meta-ized keystroke. We know
1537 that the event we're about to append is an
1538 ascii keystroke since we're processing a
1539 keymap table. */
1540 if (is_metized)
1541 {
1542 int meta_bit = meta_modifier;
1543 Lisp_Object last = make_number (XINT (Flength (thisseq)) - 1);
1544 tem = Fcopy_sequence (thisseq);
1545
1546 Faset (tem, last, make_number (XINT (key) | meta_bit));
1547
1548 /* This new sequence is the same length as
1549 thisseq, so stick it in the list right
1550 after this one. */
1551 XSETCDR (tail,
1552 Fcons (Fcons (tem, cmd), XCDR (tail)));
1553 }
1554 else
1555 {
1556 tem = append_key (thisseq, key);
1557 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
1558 }
1559 }
1560 }
1561 }
1562
1563 static void
1564 accessible_keymaps_char_table (args, index, cmd)
1565 Lisp_Object args, index, cmd;
1566 {
1567 accessible_keymaps_1 (index, cmd,
1568 XCAR (XCAR (args)),
1569 XCAR (XCDR (args)),
1570 XCDR (XCDR (args)),
1571 XINT (XCDR (XCAR (args))));
1572 }
1573
1574 /* This function cannot GC. */
1575
1576 DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps,
1577 1, 2, 0,
1578 "Find all keymaps accessible via prefix characters from KEYMAP.\n\
1579 Returns a list of elements of the form (KEYS . MAP), where the sequence\n\
1580 KEYS starting from KEYMAP gets you to MAP. These elements are ordered\n\
1581 so that the KEYS increase in length. The first element is ([] . KEYMAP).\n\
1582 An optional argument PREFIX, if non-nil, should be a key sequence;\n\
1583 then the value includes only maps for prefixes that start with PREFIX.")
1584 (keymap, prefix)
1585 Lisp_Object keymap, prefix;
1586 {
1587 Lisp_Object maps, good_maps, tail;
1588 int prefixlen = 0;
1589
1590 /* no need for gcpro because we don't autoload any keymaps. */
1591
1592 if (!NILP (prefix))
1593 prefixlen = XINT (Flength (prefix));
1594
1595 if (!NILP (prefix))
1596 {
1597 /* If a prefix was specified, start with the keymap (if any) for
1598 that prefix, so we don't waste time considering other prefixes. */
1599 Lisp_Object tem;
1600 tem = Flookup_key (keymap, prefix, Qt);
1601 /* Flookup_key may give us nil, or a number,
1602 if the prefix is not defined in this particular map.
1603 It might even give us a list that isn't a keymap. */
1604 tem = get_keymap (tem, 0, 0);
1605 if (CONSP (tem))
1606 {
1607 /* Convert PREFIX to a vector now, so that later on
1608 we don't have to deal with the possibility of a string. */
1609 if (STRINGP (prefix))
1610 {
1611 int i, i_byte, c;
1612 Lisp_Object copy;
1613
1614 copy = Fmake_vector (make_number (XSTRING (prefix)->size), Qnil);
1615 for (i = 0, i_byte = 0; i < XSTRING (prefix)->size;)
1616 {
1617 int i_before = i;
1618
1619 FETCH_STRING_CHAR_ADVANCE (c, prefix, i, i_byte);
1620 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
1621 c ^= 0200 | meta_modifier;
1622 ASET (copy, i_before, make_number (c));
1623 }
1624 prefix = copy;
1625 }
1626 maps = Fcons (Fcons (prefix, tem), Qnil);
1627 }
1628 else
1629 return Qnil;
1630 }
1631 else
1632 maps = Fcons (Fcons (Fmake_vector (make_number (0), Qnil),
1633 get_keymap (keymap, 1, 0)),
1634 Qnil);
1635
1636 /* For each map in the list maps,
1637 look at any other maps it points to,
1638 and stick them at the end if they are not already in the list.
1639
1640 This is a breadth-first traversal, where tail is the queue of
1641 nodes, and maps accumulates a list of all nodes visited. */
1642
1643 for (tail = maps; CONSP (tail); tail = XCDR (tail))
1644 {
1645 register Lisp_Object thisseq, thismap;
1646 Lisp_Object last;
1647 /* Does the current sequence end in the meta-prefix-char? */
1648 int is_metized;
1649
1650 thisseq = Fcar (Fcar (tail));
1651 thismap = Fcdr (Fcar (tail));
1652 last = make_number (XINT (Flength (thisseq)) - 1);
1653 is_metized = (XINT (last) >= 0
1654 /* Don't metize the last char of PREFIX. */
1655 && XINT (last) >= prefixlen
1656 && EQ (Faref (thisseq, last), meta_prefix_char));
1657
1658 for (; CONSP (thismap); thismap = XCDR (thismap))
1659 {
1660 Lisp_Object elt;
1661
1662 elt = XCAR (thismap);
1663
1664 QUIT;
1665
1666 if (CHAR_TABLE_P (elt))
1667 {
1668 Lisp_Object indices[3];
1669
1670 map_char_table (accessible_keymaps_char_table, Qnil,
1671 elt, Fcons (Fcons (maps, make_number (is_metized)),
1672 Fcons (tail, thisseq)),
1673 0, indices);
1674 }
1675 else if (VECTORP (elt))
1676 {
1677 register int i;
1678
1679 /* Vector keymap. Scan all the elements. */
1680 for (i = 0; i < ASIZE (elt); i++)
1681 accessible_keymaps_1 (make_number (i), AREF (elt, i),
1682 maps, tail, thisseq, is_metized);
1683
1684 }
1685 else if (CONSP (elt))
1686 accessible_keymaps_1 (XCAR (elt), XCDR (elt),
1687 maps, tail, thisseq,
1688 is_metized && INTEGERP (XCAR (elt)));
1689
1690 }
1691 }
1692
1693 if (NILP (prefix))
1694 return maps;
1695
1696 /* Now find just the maps whose access prefixes start with PREFIX. */
1697
1698 good_maps = Qnil;
1699 for (; CONSP (maps); maps = XCDR (maps))
1700 {
1701 Lisp_Object elt, thisseq;
1702 elt = XCAR (maps);
1703 thisseq = XCAR (elt);
1704 /* The access prefix must be at least as long as PREFIX,
1705 and the first elements must match those of PREFIX. */
1706 if (XINT (Flength (thisseq)) >= prefixlen)
1707 {
1708 int i;
1709 for (i = 0; i < prefixlen; i++)
1710 {
1711 Lisp_Object i1;
1712 XSETFASTINT (i1, i);
1713 if (!EQ (Faref (thisseq, i1), Faref (prefix, i1)))
1714 break;
1715 }
1716 if (i == prefixlen)
1717 good_maps = Fcons (elt, good_maps);
1718 }
1719 }
1720
1721 return Fnreverse (good_maps);
1722 }
1723 \f
1724 Lisp_Object Qsingle_key_description, Qkey_description;
1725
1726 /* This function cannot GC. */
1727
1728 DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0,
1729 "Return a pretty description of key-sequence KEYS.\n\
1730 Control characters turn into \"C-foo\" sequences, meta into \"M-foo\"\n\
1731 spaces are put between sequence elements, etc.")
1732 (keys)
1733 Lisp_Object keys;
1734 {
1735 int len = 0;
1736 int i, i_byte;
1737 Lisp_Object sep;
1738 Lisp_Object *args = NULL;
1739
1740 if (STRINGP (keys))
1741 {
1742 Lisp_Object vector;
1743 vector = Fmake_vector (Flength (keys), Qnil);
1744 for (i = 0, i_byte = 0; i < XSTRING (keys)->size; )
1745 {
1746 int c;
1747 int i_before = i;
1748
1749 FETCH_STRING_CHAR_ADVANCE (c, keys, i, i_byte);
1750 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
1751 c ^= 0200 | meta_modifier;
1752 XSETFASTINT (AREF (vector, i_before), c);
1753 }
1754 keys = vector;
1755 }
1756
1757 if (VECTORP (keys))
1758 {
1759 /* In effect, this computes
1760 (mapconcat 'single-key-description keys " ")
1761 but we shouldn't use mapconcat because it can do GC. */
1762
1763 len = XVECTOR (keys)->size;
1764 sep = build_string (" ");
1765 /* This has one extra element at the end that we don't pass to Fconcat. */
1766 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
1767
1768 for (i = 0; i < len; i++)
1769 {
1770 args[i * 2] = Fsingle_key_description (AREF (keys, i), Qnil);
1771 args[i * 2 + 1] = sep;
1772 }
1773 }
1774 else if (CONSP (keys))
1775 {
1776 /* In effect, this computes
1777 (mapconcat 'single-key-description keys " ")
1778 but we shouldn't use mapconcat because it can do GC. */
1779
1780 len = XFASTINT (Flength (keys));
1781 sep = build_string (" ");
1782 /* This has one extra element at the end that we don't pass to Fconcat. */
1783 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
1784
1785 for (i = 0; i < len; i++)
1786 {
1787 args[i * 2] = Fsingle_key_description (XCAR (keys), Qnil);
1788 args[i * 2 + 1] = sep;
1789 keys = XCDR (keys);
1790 }
1791 }
1792 else
1793 keys = wrong_type_argument (Qarrayp, keys);
1794
1795 if (len == 0)
1796 return empty_string;
1797 return Fconcat (len * 2 - 1, args);
1798 }
1799
1800 char *
1801 push_key_description (c, p, force_multibyte)
1802 register unsigned int c;
1803 register char *p;
1804 int force_multibyte;
1805 {
1806 unsigned c2;
1807
1808 /* Clear all the meaningless bits above the meta bit. */
1809 c &= meta_modifier | ~ - meta_modifier;
1810 c2 = c & ~(alt_modifier | ctrl_modifier | hyper_modifier
1811 | meta_modifier | shift_modifier | super_modifier);
1812
1813 if (c & alt_modifier)
1814 {
1815 *p++ = 'A';
1816 *p++ = '-';
1817 c -= alt_modifier;
1818 }
1819 if ((c & ctrl_modifier) != 0
1820 || (c2 < ' ' && c2 != 27 && c2 != '\t' && c2 != Ctl ('M')))
1821 {
1822 *p++ = 'C';
1823 *p++ = '-';
1824 c &= ~ctrl_modifier;
1825 }
1826 if (c & hyper_modifier)
1827 {
1828 *p++ = 'H';
1829 *p++ = '-';
1830 c -= hyper_modifier;
1831 }
1832 if (c & meta_modifier)
1833 {
1834 *p++ = 'M';
1835 *p++ = '-';
1836 c -= meta_modifier;
1837 }
1838 if (c & shift_modifier)
1839 {
1840 *p++ = 'S';
1841 *p++ = '-';
1842 c -= shift_modifier;
1843 }
1844 if (c & super_modifier)
1845 {
1846 *p++ = 's';
1847 *p++ = '-';
1848 c -= super_modifier;
1849 }
1850 if (c < 040)
1851 {
1852 if (c == 033)
1853 {
1854 *p++ = 'E';
1855 *p++ = 'S';
1856 *p++ = 'C';
1857 }
1858 else if (c == '\t')
1859 {
1860 *p++ = 'T';
1861 *p++ = 'A';
1862 *p++ = 'B';
1863 }
1864 else if (c == Ctl ('M'))
1865 {
1866 *p++ = 'R';
1867 *p++ = 'E';
1868 *p++ = 'T';
1869 }
1870 else
1871 {
1872 /* `C-' already added above. */
1873 if (c > 0 && c <= Ctl ('Z'))
1874 *p++ = c + 0140;
1875 else
1876 *p++ = c + 0100;
1877 }
1878 }
1879 else if (c == 0177)
1880 {
1881 *p++ = 'D';
1882 *p++ = 'E';
1883 *p++ = 'L';
1884 }
1885 else if (c == ' ')
1886 {
1887 *p++ = 'S';
1888 *p++ = 'P';
1889 *p++ = 'C';
1890 }
1891 else if (c < 128
1892 || (NILP (current_buffer->enable_multibyte_characters)
1893 && SINGLE_BYTE_CHAR_P (c)
1894 && !force_multibyte))
1895 {
1896 *p++ = c;
1897 }
1898 else
1899 {
1900 int valid_p = SINGLE_BYTE_CHAR_P (c) || char_valid_p (c, 0);
1901
1902 if (force_multibyte && valid_p)
1903 {
1904 if (SINGLE_BYTE_CHAR_P (c))
1905 c = unibyte_char_to_multibyte (c);
1906 p += CHAR_STRING (c, p);
1907 }
1908 else if (NILP (current_buffer->enable_multibyte_characters)
1909 || valid_p)
1910 {
1911 int bit_offset;
1912 *p++ = '\\';
1913 /* The biggest character code uses 19 bits. */
1914 for (bit_offset = 18; bit_offset >= 0; bit_offset -= 3)
1915 {
1916 if (c >= (1 << bit_offset))
1917 *p++ = ((c & (7 << bit_offset)) >> bit_offset) + '0';
1918 }
1919 }
1920 else
1921 p += CHAR_STRING (c, p);
1922 }
1923
1924 return p;
1925 }
1926
1927 /* This function cannot GC. */
1928
1929 DEFUN ("single-key-description", Fsingle_key_description,
1930 Ssingle_key_description, 1, 2, 0,
1931 "Return a pretty description of command character KEY.\n\
1932 Control characters turn into C-whatever, etc.\n\
1933 Optional argument NO-ANGLES non-nil means don't put angle brackets\n\
1934 around function keys and event symbols.")
1935 (key, no_angles)
1936 Lisp_Object key, no_angles;
1937 {
1938 if (CONSP (key) && lucid_event_type_list_p (key))
1939 key = Fevent_convert_list (key);
1940
1941 key = EVENT_HEAD (key);
1942
1943 if (INTEGERP (key)) /* Normal character */
1944 {
1945 unsigned int charset, c1, c2;
1946 int without_bits = XINT (key) & ~((-1) << CHARACTERBITS);
1947
1948 if (SINGLE_BYTE_CHAR_P (without_bits))
1949 charset = 0;
1950 else
1951 SPLIT_CHAR (without_bits, charset, c1, c2);
1952
1953 if (charset
1954 && CHARSET_DEFINED_P (charset)
1955 && ((c1 >= 0 && c1 < 32)
1956 || (c2 >= 0 && c2 < 32)))
1957 {
1958 /* Handle a generic character. */
1959 Lisp_Object name;
1960 name = CHARSET_TABLE_INFO (charset, CHARSET_LONG_NAME_IDX);
1961 CHECK_STRING (name, 0);
1962 return concat2 (build_string ("Character set "), name);
1963 }
1964 else
1965 {
1966 char tem[KEY_DESCRIPTION_SIZE], *end;
1967 int nbytes, nchars;
1968 Lisp_Object string;
1969
1970 end = push_key_description (XUINT (key), tem, 1);
1971 nbytes = end - tem;
1972 nchars = multibyte_chars_in_text (tem, nbytes);
1973 if (nchars == nbytes)
1974 {
1975 *end = '\0';
1976 string = build_string (tem);
1977 }
1978 else
1979 string = make_multibyte_string (tem, nchars, nbytes);
1980 return string;
1981 }
1982 }
1983 else if (SYMBOLP (key)) /* Function key or event-symbol */
1984 {
1985 if (NILP (no_angles))
1986 {
1987 char *buffer
1988 = (char *) alloca (STRING_BYTES (XSYMBOL (key)->name) + 5);
1989 sprintf (buffer, "<%s>", XSYMBOL (key)->name->data);
1990 return build_string (buffer);
1991 }
1992 else
1993 return Fsymbol_name (key);
1994 }
1995 else if (STRINGP (key)) /* Buffer names in the menubar. */
1996 return Fcopy_sequence (key);
1997 else
1998 error ("KEY must be an integer, cons, symbol, or string");
1999 return Qnil;
2000 }
2001
2002 char *
2003 push_text_char_description (c, p)
2004 register unsigned int c;
2005 register char *p;
2006 {
2007 if (c >= 0200)
2008 {
2009 *p++ = 'M';
2010 *p++ = '-';
2011 c -= 0200;
2012 }
2013 if (c < 040)
2014 {
2015 *p++ = '^';
2016 *p++ = c + 64; /* 'A' - 1 */
2017 }
2018 else if (c == 0177)
2019 {
2020 *p++ = '^';
2021 *p++ = '?';
2022 }
2023 else
2024 *p++ = c;
2025 return p;
2026 }
2027
2028 /* This function cannot GC. */
2029
2030 DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0,
2031 "Return a pretty description of file-character CHARACTER.\n\
2032 Control characters turn into \"^char\", etc.")
2033 (character)
2034 Lisp_Object character;
2035 {
2036 /* Currently MAX_MULTIBYTE_LENGTH is 4 (< 6). */
2037 unsigned char str[6];
2038 int c;
2039
2040 CHECK_NUMBER (character, 0);
2041
2042 c = XINT (character);
2043 if (!SINGLE_BYTE_CHAR_P (c))
2044 {
2045 int len = CHAR_STRING (c, str);
2046
2047 return make_multibyte_string (str, 1, len);
2048 }
2049
2050 *push_text_char_description (c & 0377, str) = 0;
2051
2052 return build_string (str);
2053 }
2054
2055 /* Return non-zero if SEQ contains only ASCII characters, perhaps with
2056 a meta bit. */
2057 static int
2058 ascii_sequence_p (seq)
2059 Lisp_Object seq;
2060 {
2061 int i;
2062 int len = XINT (Flength (seq));
2063
2064 for (i = 0; i < len; i++)
2065 {
2066 Lisp_Object ii, elt;
2067
2068 XSETFASTINT (ii, i);
2069 elt = Faref (seq, ii);
2070
2071 if (!INTEGERP (elt)
2072 || (XUINT (elt) & ~CHAR_META) >= 0x80)
2073 return 0;
2074 }
2075
2076 return 1;
2077 }
2078
2079 \f
2080 /* where-is - finding a command in a set of keymaps. */
2081
2082 static Lisp_Object where_is_internal_1 ();
2083 static void where_is_internal_2 ();
2084
2085 /* Like Flookup_key, but uses a list of keymaps SHADOW instead of a single map.
2086 Returns the first non-nil binding found in any of those maps. */
2087
2088 static Lisp_Object
2089 shadow_lookup (shadow, key, flag)
2090 Lisp_Object shadow, key, flag;
2091 {
2092 Lisp_Object tail, value;
2093
2094 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2095 {
2096 value = Flookup_key (XCAR (tail), key, flag);
2097 if (!NILP (value) && !NATNUMP (value))
2098 return value;
2099 }
2100 return Qnil;
2101 }
2102
2103 /* This function can GC if Flookup_key autoloads any keymaps. */
2104
2105 static Lisp_Object
2106 where_is_internal (definition, keymaps, firstonly, noindirect)
2107 Lisp_Object definition, keymaps;
2108 Lisp_Object firstonly, noindirect;
2109 {
2110 Lisp_Object maps = Qnil;
2111 Lisp_Object found, sequences;
2112 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
2113 /* 1 means ignore all menu bindings entirely. */
2114 int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii);
2115
2116 found = keymaps;
2117 while (CONSP (found))
2118 {
2119 maps =
2120 nconc2 (maps,
2121 Faccessible_keymaps (get_keymap (XCAR (found), 1, 0), Qnil));
2122 found = XCDR (found);
2123 }
2124
2125 GCPRO5 (definition, keymaps, maps, found, sequences);
2126 found = Qnil;
2127 sequences = Qnil;
2128
2129 for (; !NILP (maps); maps = Fcdr (maps))
2130 {
2131 /* Key sequence to reach map, and the map that it reaches */
2132 register Lisp_Object this, map;
2133
2134 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
2135 [M-CHAR] sequences, check if last character of the sequence
2136 is the meta-prefix char. */
2137 Lisp_Object last;
2138 int last_is_meta;
2139
2140 this = Fcar (Fcar (maps));
2141 map = Fcdr (Fcar (maps));
2142 last = make_number (XINT (Flength (this)) - 1);
2143 last_is_meta = (XINT (last) >= 0
2144 && EQ (Faref (this, last), meta_prefix_char));
2145
2146 /* if (nomenus && !ascii_sequence_p (this)) */
2147 if (nomenus && XINT (last) >= 0
2148 && !INTEGERP (Faref (this, make_number (0))))
2149 /* If no menu entries should be returned, skip over the
2150 keymaps bound to `menu-bar' and `tool-bar' and other
2151 non-ascii prefixes like `C-down-mouse-2'. */
2152 continue;
2153
2154 QUIT;
2155
2156 while (CONSP (map))
2157 {
2158 /* Because the code we want to run on each binding is rather
2159 large, we don't want to have two separate loop bodies for
2160 sparse keymap bindings and tables; we want to iterate one
2161 loop body over both keymap and vector bindings.
2162
2163 For this reason, if Fcar (map) is a vector, we don't
2164 advance map to the next element until i indicates that we
2165 have finished off the vector. */
2166 Lisp_Object elt, key, binding;
2167 elt = XCAR (map);
2168 map = XCDR (map);
2169
2170 sequences = Qnil;
2171
2172 QUIT;
2173
2174 /* Set key and binding to the current key and binding, and
2175 advance map and i to the next binding. */
2176 if (VECTORP (elt))
2177 {
2178 Lisp_Object sequence;
2179 int i;
2180 /* In a vector, look at each element. */
2181 for (i = 0; i < XVECTOR (elt)->size; i++)
2182 {
2183 binding = AREF (elt, i);
2184 XSETFASTINT (key, i);
2185 sequence = where_is_internal_1 (binding, key, definition,
2186 noindirect, this,
2187 last, nomenus, last_is_meta);
2188 if (!NILP (sequence))
2189 sequences = Fcons (sequence, sequences);
2190 }
2191 }
2192 else if (CHAR_TABLE_P (elt))
2193 {
2194 Lisp_Object indices[3];
2195 Lisp_Object args;
2196
2197 args = Fcons (Fcons (Fcons (definition, noindirect),
2198 Qnil), /* Result accumulator. */
2199 Fcons (Fcons (this, last),
2200 Fcons (make_number (nomenus),
2201 make_number (last_is_meta))));
2202 map_char_table (where_is_internal_2, Qnil, elt, args,
2203 0, indices);
2204 sequences = XCDR (XCAR (args));
2205 }
2206 else if (CONSP (elt))
2207 {
2208 Lisp_Object sequence;
2209
2210 key = XCAR (elt);
2211 binding = XCDR (elt);
2212
2213 sequence = where_is_internal_1 (binding, key, definition,
2214 noindirect, this,
2215 last, nomenus, last_is_meta);
2216 if (!NILP (sequence))
2217 sequences = Fcons (sequence, sequences);
2218 }
2219
2220
2221 for (; !NILP (sequences); sequences = XCDR (sequences))
2222 {
2223 Lisp_Object sequence;
2224
2225 sequence = XCAR (sequences);
2226
2227 /* Verify that this key binding is not shadowed by another
2228 binding for the same key, before we say it exists.
2229
2230 Mechanism: look for local definition of this key and if
2231 it is defined and does not match what we found then
2232 ignore this key.
2233
2234 Either nil or number as value from Flookup_key
2235 means undefined. */
2236 if (!EQ (shadow_lookup (keymaps, sequence, Qnil), definition))
2237 continue;
2238
2239 /* It is a true unshadowed match. Record it, unless it's already
2240 been seen (as could happen when inheriting keymaps). */
2241 if (NILP (Fmember (sequence, found)))
2242 found = Fcons (sequence, found);
2243
2244 /* If firstonly is Qnon_ascii, then we can return the first
2245 binding we find. If firstonly is not Qnon_ascii but not
2246 nil, then we should return the first ascii-only binding
2247 we find. */
2248 if (EQ (firstonly, Qnon_ascii))
2249 RETURN_UNGCPRO (sequence);
2250 else if (!NILP (firstonly) && ascii_sequence_p (sequence))
2251 RETURN_UNGCPRO (sequence);
2252 }
2253 }
2254 }
2255
2256 UNGCPRO;
2257
2258 found = Fnreverse (found);
2259
2260 /* firstonly may have been t, but we may have gone all the way through
2261 the keymaps without finding an all-ASCII key sequence. So just
2262 return the best we could find. */
2263 if (!NILP (firstonly))
2264 return Fcar (found);
2265
2266 return found;
2267 }
2268
2269 DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 4, 0,
2270 "Return list of keys that invoke DEFINITION.\n\
2271 If KEYMAP is non-nil, search only KEYMAP and the global keymap.\n\
2272 If KEYMAP is nil, search all the currently active keymaps.\n\
2273 If KEYMAP is a list of keymaps, search only those keymaps.\n\
2274 \n\
2275 If optional 3rd arg FIRSTONLY is non-nil, return the first key sequence found,\n\
2276 rather than a list of all possible key sequences.\n\
2277 If FIRSTONLY is the symbol `non-ascii', return the first binding found,\n\
2278 no matter what it is.\n\
2279 If FIRSTONLY has another non-nil value, prefer sequences of ASCII characters,\n\
2280 and entirely reject menu bindings.\n\
2281 \n\
2282 If optional 4th arg NOINDIRECT is non-nil, don't follow indirections\n\
2283 to other keymaps or slots. This makes it possible to search for an\n\
2284 indirect definition itself.")
2285 (definition, keymap, firstonly, noindirect)
2286 Lisp_Object definition, keymap;
2287 Lisp_Object firstonly, noindirect;
2288 {
2289 Lisp_Object sequences, keymaps;
2290 /* 1 means ignore all menu bindings entirely. */
2291 int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii);
2292 Lisp_Object result;
2293
2294 /* Find the relevant keymaps. */
2295 if (CONSP (keymap) && KEYMAPP (XCAR (keymap)))
2296 keymaps = keymap;
2297 else if (!NILP (keymap))
2298 keymaps = Fcons (keymap, Fcons (current_global_map, Qnil));
2299 else
2300 keymaps = Fcurrent_active_maps (Qnil);
2301
2302 /* Only use caching for the menubar (i.e. called with (def nil t nil).
2303 We don't really need to check `keymap'. */
2304 if (nomenus && NILP (noindirect) && NILP (keymap))
2305 {
2306 Lisp_Object *defns;
2307 int i, j, n;
2308 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2309
2310 /* Check heuristic-consistency of the cache. */
2311 if (NILP (Fequal (keymaps, where_is_cache_keymaps)))
2312 where_is_cache = Qnil;
2313
2314 if (NILP (where_is_cache))
2315 {
2316 /* We need to create the cache. */
2317 Lisp_Object args[2];
2318 where_is_cache = Fmake_hash_table (0, args);
2319 where_is_cache_keymaps = Qt;
2320
2321 /* Fill in the cache. */
2322 GCPRO4 (definition, keymaps, firstonly, noindirect);
2323 where_is_internal (definition, keymaps, firstonly, noindirect);
2324 UNGCPRO;
2325
2326 where_is_cache_keymaps = keymaps;
2327 }
2328
2329 /* We want to process definitions from the last to the first.
2330 Instead of consing, copy definitions to a vector and step
2331 over that vector. */
2332 sequences = Fgethash (definition, where_is_cache, Qnil);
2333 n = XINT (Flength (sequences));
2334 defns = (Lisp_Object *) alloca (n * sizeof *defns);
2335 for (i = 0; CONSP (sequences); sequences = XCDR (sequences))
2336 defns[i++] = XCAR (sequences);
2337
2338 /* Verify that the key bindings are not shadowed. Note that
2339 the following can GC. */
2340 GCPRO2 (definition, keymaps);
2341 result = Qnil;
2342 j = -1;
2343 for (i = n - 1; i >= 0; --i)
2344 if (EQ (shadow_lookup (keymaps, defns[i], Qnil), definition))
2345 {
2346 if (ascii_sequence_p (defns[i]))
2347 break;
2348 else if (j < 0)
2349 j = i;
2350 }
2351
2352 result = i >= 0 ? defns[i] : (j >= 0 ? defns[j] : Qnil);
2353 UNGCPRO;
2354 }
2355 else
2356 {
2357 /* Kill the cache so that where_is_internal_1 doesn't think
2358 we're filling it up. */
2359 where_is_cache = Qnil;
2360 result = where_is_internal (definition, keymaps, firstonly, noindirect);
2361 }
2362
2363 return result;
2364 }
2365
2366 /* This is the function that Fwhere_is_internal calls using map_char_table.
2367 ARGS has the form
2368 (((DEFINITION . NOINDIRECT) . (KEYMAP . RESULT))
2369 .
2370 ((THIS . LAST) . (NOMENUS . LAST_IS_META)))
2371 Since map_char_table doesn't really use the return value from this function,
2372 we the result append to RESULT, the slot in ARGS.
2373
2374 This function can GC because it calls where_is_internal_1 which can
2375 GC. */
2376
2377 static void
2378 where_is_internal_2 (args, key, binding)
2379 Lisp_Object args, key, binding;
2380 {
2381 Lisp_Object definition, noindirect, this, last;
2382 Lisp_Object result, sequence;
2383 int nomenus, last_is_meta;
2384 struct gcpro gcpro1, gcpro2, gcpro3;
2385
2386 GCPRO3 (args, key, binding);
2387 result = XCDR (XCAR (args));
2388 definition = XCAR (XCAR (XCAR (args)));
2389 noindirect = XCDR (XCAR (XCAR (args)));
2390 this = XCAR (XCAR (XCDR (args)));
2391 last = XCDR (XCAR (XCDR (args)));
2392 nomenus = XFASTINT (XCAR (XCDR (XCDR (args))));
2393 last_is_meta = XFASTINT (XCDR (XCDR (XCDR (args))));
2394
2395 sequence = where_is_internal_1 (binding, key, definition, noindirect,
2396 this, last, nomenus, last_is_meta);
2397
2398 if (!NILP (sequence))
2399 XSETCDR (XCAR (args), Fcons (sequence, result));
2400
2401 UNGCPRO;
2402 }
2403
2404
2405 /* This function cannot GC. */
2406
2407 static Lisp_Object
2408 where_is_internal_1 (binding, key, definition, noindirect, this, last,
2409 nomenus, last_is_meta)
2410 Lisp_Object binding, key, definition, noindirect, this, last;
2411 int nomenus, last_is_meta;
2412 {
2413 Lisp_Object sequence;
2414
2415 /* Search through indirections unless that's not wanted. */
2416 if (NILP (noindirect))
2417 binding = get_keyelt (binding, 0);
2418
2419 /* End this iteration if this element does not match
2420 the target. */
2421
2422 if (!(!NILP (where_is_cache) /* everything "matches" during cache-fill. */
2423 || EQ (binding, definition)
2424 || (CONSP (definition) && !NILP (Fequal (binding, definition)))))
2425 /* Doesn't match. */
2426 return Qnil;
2427
2428 /* We have found a match. Construct the key sequence where we found it. */
2429 if (INTEGERP (key) && last_is_meta)
2430 {
2431 sequence = Fcopy_sequence (this);
2432 Faset (sequence, last, make_number (XINT (key) | meta_modifier));
2433 }
2434 else
2435 sequence = append_key (this, key);
2436
2437 if (!NILP (where_is_cache))
2438 {
2439 Lisp_Object sequences = Fgethash (binding, where_is_cache, Qnil);
2440 Fputhash (binding, Fcons (sequence, sequences), where_is_cache);
2441 return Qnil;
2442 }
2443 else
2444 return sequence;
2445 }
2446 \f
2447 /* describe-bindings - summarizing all the bindings in a set of keymaps. */
2448
2449 DEFUN ("describe-buffer-bindings", Fdescribe_buffer_bindings, Sdescribe_buffer_bindings, 1, 3, 0,
2450 "Insert the list of all defined keys and their definitions.\n\
2451 The list is inserted in the current buffer, while the bindings are\n\
2452 looked up in BUFFER.\n\
2453 The optional argument PREFIX, if non-nil, should be a key sequence;\n\
2454 then we display only bindings that start with that prefix.\n\
2455 The optional argument MENUS, if non-nil, says to mention menu bindings.\n\
2456 \(Ordinarily these are omitted from the output.)")
2457 (buffer, prefix, menus)
2458 Lisp_Object buffer, prefix, menus;
2459 {
2460 Lisp_Object outbuf, shadow;
2461 int nomenu = NILP (menus);
2462 register Lisp_Object start1;
2463 struct gcpro gcpro1;
2464
2465 char *alternate_heading
2466 = "\
2467 Keyboard translations:\n\n\
2468 You type Translation\n\
2469 -------- -----------\n";
2470
2471 shadow = Qnil;
2472 GCPRO1 (shadow);
2473
2474 outbuf = Fcurrent_buffer();
2475
2476 /* Report on alternates for keys. */
2477 if (STRINGP (Vkeyboard_translate_table) && !NILP (prefix))
2478 {
2479 int c;
2480 unsigned char *translate = XSTRING (Vkeyboard_translate_table)->data;
2481 int translate_len = XSTRING (Vkeyboard_translate_table)->size;
2482
2483 for (c = 0; c < translate_len; c++)
2484 if (translate[c] != c)
2485 {
2486 char buf[KEY_DESCRIPTION_SIZE];
2487 char *bufend;
2488
2489 if (alternate_heading)
2490 {
2491 insert_string (alternate_heading);
2492 alternate_heading = 0;
2493 }
2494
2495 bufend = push_key_description (translate[c], buf, 1);
2496 insert (buf, bufend - buf);
2497 Findent_to (make_number (16), make_number (1));
2498 bufend = push_key_description (c, buf, 1);
2499 insert (buf, bufend - buf);
2500
2501 insert ("\n", 1);
2502 }
2503
2504 insert ("\n", 1);
2505 }
2506
2507 if (!NILP (Vkey_translation_map))
2508 describe_map_tree (Vkey_translation_map, 0, Qnil, prefix,
2509 "Key translations", nomenu, 1, 0);
2510
2511 {
2512 int i, nmaps;
2513 Lisp_Object *modes, *maps;
2514
2515 /* Temporarily switch to `buffer', so that we can get that buffer's
2516 minor modes correctly. */
2517 Fset_buffer (buffer);
2518
2519 if (!NILP (current_kboard->Voverriding_terminal_local_map)
2520 || !NILP (Voverriding_local_map))
2521 nmaps = 0;
2522 else
2523 nmaps = current_minor_maps (&modes, &maps);
2524 Fset_buffer (outbuf);
2525
2526 /* Print the minor mode maps. */
2527 for (i = 0; i < nmaps; i++)
2528 {
2529 /* The title for a minor mode keymap
2530 is constructed at run time.
2531 We let describe_map_tree do the actual insertion
2532 because it takes care of other features when doing so. */
2533 char *title, *p;
2534
2535 if (!SYMBOLP (modes[i]))
2536 abort();
2537
2538 p = title = (char *) alloca (42 + XSYMBOL (modes[i])->name->size);
2539 *p++ = '\f';
2540 *p++ = '\n';
2541 *p++ = '`';
2542 bcopy (XSYMBOL (modes[i])->name->data, p,
2543 XSYMBOL (modes[i])->name->size);
2544 p += XSYMBOL (modes[i])->name->size;
2545 *p++ = '\'';
2546 bcopy (" Minor Mode Bindings", p, sizeof (" Minor Mode Bindings") - 1);
2547 p += sizeof (" Minor Mode Bindings") - 1;
2548 *p = 0;
2549
2550 describe_map_tree (maps[i], 1, shadow, prefix, title, nomenu, 0, 0);
2551 shadow = Fcons (maps[i], shadow);
2552 }
2553 }
2554
2555 /* Print the (major mode) local map. */
2556 if (!NILP (current_kboard->Voverriding_terminal_local_map))
2557 start1 = current_kboard->Voverriding_terminal_local_map;
2558 else if (!NILP (Voverriding_local_map))
2559 start1 = Voverriding_local_map;
2560 else
2561 start1 = XBUFFER (buffer)->keymap;
2562
2563 if (!NILP (start1))
2564 {
2565 describe_map_tree (start1, 1, shadow, prefix,
2566 "\f\nMajor Mode Bindings", nomenu, 0, 0);
2567 shadow = Fcons (start1, shadow);
2568 }
2569
2570 describe_map_tree (current_global_map, 1, shadow, prefix,
2571 "\f\nGlobal Bindings", nomenu, 0, 1);
2572
2573 /* Print the function-key-map translations under this prefix. */
2574 if (!NILP (Vfunction_key_map))
2575 describe_map_tree (Vfunction_key_map, 0, Qnil, prefix,
2576 "\f\nFunction key map translations", nomenu, 1, 0);
2577
2578 UNGCPRO;
2579 return Qnil;
2580 }
2581
2582 /* Insert a description of the key bindings in STARTMAP,
2583 followed by those of all maps reachable through STARTMAP.
2584 If PARTIAL is nonzero, omit certain "uninteresting" commands
2585 (such as `undefined').
2586 If SHADOW is non-nil, it is a list of maps;
2587 don't mention keys which would be shadowed by any of them.
2588 PREFIX, if non-nil, says mention only keys that start with PREFIX.
2589 TITLE, if not 0, is a string to insert at the beginning.
2590 TITLE should not end with a colon or a newline; we supply that.
2591 If NOMENU is not 0, then omit menu-bar commands.
2592
2593 If TRANSL is nonzero, the definitions are actually key translations
2594 so print strings and vectors differently.
2595
2596 If ALWAYS_TITLE is nonzero, print the title even if there are no maps
2597 to look through. */
2598
2599 void
2600 describe_map_tree (startmap, partial, shadow, prefix, title, nomenu, transl,
2601 always_title)
2602 Lisp_Object startmap, shadow, prefix;
2603 int partial;
2604 char *title;
2605 int nomenu;
2606 int transl;
2607 int always_title;
2608 {
2609 Lisp_Object maps, orig_maps, seen, sub_shadows;
2610 struct gcpro gcpro1, gcpro2, gcpro3;
2611 int something = 0;
2612 char *key_heading
2613 = "\
2614 key binding\n\
2615 --- -------\n";
2616
2617 orig_maps = maps = Faccessible_keymaps (startmap, prefix);
2618 seen = Qnil;
2619 sub_shadows = Qnil;
2620 GCPRO3 (maps, seen, sub_shadows);
2621
2622 if (nomenu)
2623 {
2624 Lisp_Object list;
2625
2626 /* Delete from MAPS each element that is for the menu bar. */
2627 for (list = maps; !NILP (list); list = XCDR (list))
2628 {
2629 Lisp_Object elt, prefix, tem;
2630
2631 elt = Fcar (list);
2632 prefix = Fcar (elt);
2633 if (XVECTOR (prefix)->size >= 1)
2634 {
2635 tem = Faref (prefix, make_number (0));
2636 if (EQ (tem, Qmenu_bar))
2637 maps = Fdelq (elt, maps);
2638 }
2639 }
2640 }
2641
2642 if (!NILP (maps) || always_title)
2643 {
2644 if (title)
2645 {
2646 insert_string (title);
2647 if (!NILP (prefix))
2648 {
2649 insert_string (" Starting With ");
2650 insert1 (Fkey_description (prefix));
2651 }
2652 insert_string (":\n");
2653 }
2654 insert_string (key_heading);
2655 something = 1;
2656 }
2657
2658 for (; !NILP (maps); maps = Fcdr (maps))
2659 {
2660 register Lisp_Object elt, prefix, tail;
2661
2662 elt = Fcar (maps);
2663 prefix = Fcar (elt);
2664
2665 sub_shadows = Qnil;
2666
2667 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2668 {
2669 Lisp_Object shmap;
2670
2671 shmap = XCAR (tail);
2672
2673 /* If the sequence by which we reach this keymap is zero-length,
2674 then the shadow map for this keymap is just SHADOW. */
2675 if ((STRINGP (prefix) && XSTRING (prefix)->size == 0)
2676 || (VECTORP (prefix) && XVECTOR (prefix)->size == 0))
2677 ;
2678 /* If the sequence by which we reach this keymap actually has
2679 some elements, then the sequence's definition in SHADOW is
2680 what we should use. */
2681 else
2682 {
2683 shmap = Flookup_key (shmap, Fcar (elt), Qt);
2684 if (INTEGERP (shmap))
2685 shmap = Qnil;
2686 }
2687
2688 /* If shmap is not nil and not a keymap,
2689 it completely shadows this map, so don't
2690 describe this map at all. */
2691 if (!NILP (shmap) && !KEYMAPP (shmap))
2692 goto skip;
2693
2694 if (!NILP (shmap))
2695 sub_shadows = Fcons (shmap, sub_shadows);
2696 }
2697
2698 /* Maps we have already listed in this loop shadow this map. */
2699 for (tail = orig_maps; !EQ (tail, maps); tail = XCDR (tail))
2700 {
2701 Lisp_Object tem;
2702 tem = Fequal (Fcar (XCAR (tail)), prefix);
2703 if (!NILP (tem))
2704 sub_shadows = Fcons (XCDR (XCAR (tail)), sub_shadows);
2705 }
2706
2707 describe_map (Fcdr (elt), prefix,
2708 transl ? describe_translation : describe_command,
2709 partial, sub_shadows, &seen, nomenu);
2710
2711 skip: ;
2712 }
2713
2714 if (something)
2715 insert_string ("\n");
2716
2717 UNGCPRO;
2718 }
2719
2720 static int previous_description_column;
2721
2722 static void
2723 describe_command (definition)
2724 Lisp_Object definition;
2725 {
2726 register Lisp_Object tem1;
2727 int column = current_column ();
2728 int description_column;
2729
2730 /* If column 16 is no good, go to col 32;
2731 but don't push beyond that--go to next line instead. */
2732 if (column > 30)
2733 {
2734 insert_char ('\n');
2735 description_column = 32;
2736 }
2737 else if (column > 14 || (column > 10 && previous_description_column == 32))
2738 description_column = 32;
2739 else
2740 description_column = 16;
2741
2742 Findent_to (make_number (description_column), make_number (1));
2743 previous_description_column = description_column;
2744
2745 if (SYMBOLP (definition))
2746 {
2747 XSETSTRING (tem1, XSYMBOL (definition)->name);
2748 insert1 (tem1);
2749 insert_string ("\n");
2750 }
2751 else if (STRINGP (definition) || VECTORP (definition))
2752 insert_string ("Keyboard Macro\n");
2753 else if (KEYMAPP (definition))
2754 insert_string ("Prefix Command\n");
2755 else
2756 insert_string ("??\n");
2757 }
2758
2759 static void
2760 describe_translation (definition)
2761 Lisp_Object definition;
2762 {
2763 register Lisp_Object tem1;
2764
2765 Findent_to (make_number (16), make_number (1));
2766
2767 if (SYMBOLP (definition))
2768 {
2769 XSETSTRING (tem1, XSYMBOL (definition)->name);
2770 insert1 (tem1);
2771 insert_string ("\n");
2772 }
2773 else if (STRINGP (definition) || VECTORP (definition))
2774 {
2775 insert1 (Fkey_description (definition));
2776 insert_string ("\n");
2777 }
2778 else if (KEYMAPP (definition))
2779 insert_string ("Prefix Command\n");
2780 else
2781 insert_string ("??\n");
2782 }
2783
2784 /* Describe the contents of map MAP, assuming that this map itself is
2785 reached by the sequence of prefix keys KEYS (a string or vector).
2786 PARTIAL, SHADOW, NOMENU are as in `describe_map_tree' above. */
2787
2788 static void
2789 describe_map (map, keys, elt_describer, partial, shadow, seen, nomenu)
2790 register Lisp_Object map;
2791 Lisp_Object keys;
2792 void (*elt_describer) P_ ((Lisp_Object));
2793 int partial;
2794 Lisp_Object shadow;
2795 Lisp_Object *seen;
2796 int nomenu;
2797 {
2798 Lisp_Object elt_prefix;
2799 Lisp_Object tail, definition, event;
2800 Lisp_Object tem;
2801 Lisp_Object suppress;
2802 Lisp_Object kludge;
2803 int first = 1;
2804 struct gcpro gcpro1, gcpro2, gcpro3;
2805
2806 suppress = Qnil;
2807
2808 if (!NILP (keys) && XFASTINT (Flength (keys)) > 0)
2809 {
2810 /* Call Fkey_description first, to avoid GC bug for the other string. */
2811 tem = Fkey_description (keys);
2812 elt_prefix = concat2 (tem, build_string (" "));
2813 }
2814 else
2815 elt_prefix = Qnil;
2816
2817 if (partial)
2818 suppress = intern ("suppress-keymap");
2819
2820 /* This vector gets used to present single keys to Flookup_key. Since
2821 that is done once per keymap element, we don't want to cons up a
2822 fresh vector every time. */
2823 kludge = Fmake_vector (make_number (1), Qnil);
2824 definition = Qnil;
2825
2826 GCPRO3 (elt_prefix, definition, kludge);
2827
2828 for (tail = map; CONSP (tail); tail = XCDR (tail))
2829 {
2830 QUIT;
2831
2832 if (VECTORP (XCAR (tail))
2833 || CHAR_TABLE_P (XCAR (tail)))
2834 describe_vector (XCAR (tail),
2835 elt_prefix, elt_describer, partial, shadow, map,
2836 (int *)0, 0);
2837 else if (CONSP (XCAR (tail)))
2838 {
2839 event = XCAR (XCAR (tail));
2840
2841 /* Ignore bindings whose "keys" are not really valid events.
2842 (We get these in the frames and buffers menu.) */
2843 if (!(SYMBOLP (event) || INTEGERP (event)))
2844 continue;
2845
2846 if (nomenu && EQ (event, Qmenu_bar))
2847 continue;
2848
2849 definition = get_keyelt (XCDR (XCAR (tail)), 0);
2850
2851 /* Don't show undefined commands or suppressed commands. */
2852 if (NILP (definition)) continue;
2853 if (SYMBOLP (definition) && partial)
2854 {
2855 tem = Fget (definition, suppress);
2856 if (!NILP (tem))
2857 continue;
2858 }
2859
2860 /* Don't show a command that isn't really visible
2861 because a local definition of the same key shadows it. */
2862
2863 ASET (kludge, 0, event);
2864 if (!NILP (shadow))
2865 {
2866 tem = shadow_lookup (shadow, kludge, Qt);
2867 if (!NILP (tem)) continue;
2868 }
2869
2870 tem = Flookup_key (map, kludge, Qt);
2871 if (!EQ (tem, definition)) continue;
2872
2873 if (first)
2874 {
2875 previous_description_column = 0;
2876 insert ("\n", 1);
2877 first = 0;
2878 }
2879
2880 if (!NILP (elt_prefix))
2881 insert1 (elt_prefix);
2882
2883 /* THIS gets the string to describe the character EVENT. */
2884 insert1 (Fsingle_key_description (event, Qnil));
2885
2886 /* Print a description of the definition of this character.
2887 elt_describer will take care of spacing out far enough
2888 for alignment purposes. */
2889 (*elt_describer) (definition);
2890 }
2891 else if (EQ (XCAR (tail), Qkeymap))
2892 {
2893 /* The same keymap might be in the structure twice, if we're
2894 using an inherited keymap. So skip anything we've already
2895 encountered. */
2896 tem = Fassq (tail, *seen);
2897 if (CONSP (tem) && !NILP (Fequal (XCAR (tem), keys)))
2898 break;
2899 *seen = Fcons (Fcons (tail, keys), *seen);
2900 }
2901 }
2902
2903 UNGCPRO;
2904 }
2905
2906 static void
2907 describe_vector_princ (elt)
2908 Lisp_Object elt;
2909 {
2910 Findent_to (make_number (16), make_number (1));
2911 Fprinc (elt, Qnil);
2912 Fterpri (Qnil);
2913 }
2914
2915 DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 1, 0,
2916 "Insert a description of contents of VECTOR.\n\
2917 This is text showing the elements of vector matched against indices.")
2918 (vector)
2919 Lisp_Object vector;
2920 {
2921 int count = specpdl_ptr - specpdl;
2922
2923 specbind (Qstandard_output, Fcurrent_buffer ());
2924 CHECK_VECTOR_OR_CHAR_TABLE (vector, 0);
2925 describe_vector (vector, Qnil, describe_vector_princ, 0,
2926 Qnil, Qnil, (int *)0, 0);
2927
2928 return unbind_to (count, Qnil);
2929 }
2930
2931 /* Insert in the current buffer a description of the contents of VECTOR.
2932 We call ELT_DESCRIBER to insert the description of one value found
2933 in VECTOR.
2934
2935 ELT_PREFIX describes what "comes before" the keys or indices defined
2936 by this vector. This is a human-readable string whose size
2937 is not necessarily related to the situation.
2938
2939 If the vector is in a keymap, ELT_PREFIX is a prefix key which
2940 leads to this keymap.
2941
2942 If the vector is a chartable, ELT_PREFIX is the vector
2943 of bytes that lead to the character set or portion of a character
2944 set described by this chartable.
2945
2946 If PARTIAL is nonzero, it means do not mention suppressed commands
2947 (that assumes the vector is in a keymap).
2948
2949 SHADOW is a list of keymaps that shadow this map.
2950 If it is non-nil, then we look up the key in those maps
2951 and we don't mention it now if it is defined by any of them.
2952
2953 ENTIRE_MAP is the keymap in which this vector appears.
2954 If the definition in effect in the whole map does not match
2955 the one in this vector, we ignore this one.
2956
2957 When describing a sub-char-table, INDICES is a list of
2958 indices at higher levels in this char-table,
2959 and CHAR_TABLE_DEPTH says how many levels down we have gone. */
2960
2961 void
2962 describe_vector (vector, elt_prefix, elt_describer,
2963 partial, shadow, entire_map,
2964 indices, char_table_depth)
2965 register Lisp_Object vector;
2966 Lisp_Object elt_prefix;
2967 void (*elt_describer) P_ ((Lisp_Object));
2968 int partial;
2969 Lisp_Object shadow;
2970 Lisp_Object entire_map;
2971 int *indices;
2972 int char_table_depth;
2973 {
2974 Lisp_Object definition;
2975 Lisp_Object tem2;
2976 register int i;
2977 Lisp_Object suppress;
2978 Lisp_Object kludge;
2979 int first = 1;
2980 struct gcpro gcpro1, gcpro2, gcpro3;
2981 /* Range of elements to be handled. */
2982 int from, to;
2983 /* A flag to tell if a leaf in this level of char-table is not a
2984 generic character (i.e. a complete multibyte character). */
2985 int complete_char;
2986 int character;
2987 int starting_i;
2988
2989 suppress = Qnil;
2990
2991 if (indices == 0)
2992 indices = (int *) alloca (3 * sizeof (int));
2993
2994 definition = Qnil;
2995
2996 /* This vector gets used to present single keys to Flookup_key. Since
2997 that is done once per vector element, we don't want to cons up a
2998 fresh vector every time. */
2999 kludge = Fmake_vector (make_number (1), Qnil);
3000 GCPRO3 (elt_prefix, definition, kludge);
3001
3002 if (partial)
3003 suppress = intern ("suppress-keymap");
3004
3005 if (CHAR_TABLE_P (vector))
3006 {
3007 if (char_table_depth == 0)
3008 {
3009 /* VECTOR is a top level char-table. */
3010 complete_char = 1;
3011 from = 0;
3012 to = CHAR_TABLE_ORDINARY_SLOTS;
3013 }
3014 else
3015 {
3016 /* VECTOR is a sub char-table. */
3017 if (char_table_depth >= 3)
3018 /* A char-table is never that deep. */
3019 error ("Too deep char table");
3020
3021 complete_char
3022 = (CHARSET_VALID_P (indices[0])
3023 && ((CHARSET_DIMENSION (indices[0]) == 1
3024 && char_table_depth == 1)
3025 || char_table_depth == 2));
3026
3027 /* Meaningful elements are from 32th to 127th. */
3028 from = 32;
3029 to = SUB_CHAR_TABLE_ORDINARY_SLOTS;
3030 }
3031 }
3032 else
3033 {
3034 /* This does the right thing for ordinary vectors. */
3035
3036 complete_char = 1;
3037 from = 0;
3038 to = XVECTOR (vector)->size;
3039 }
3040
3041 for (i = from; i < to; i++)
3042 {
3043 QUIT;
3044
3045 if (CHAR_TABLE_P (vector))
3046 {
3047 if (char_table_depth == 0 && i >= CHAR_TABLE_SINGLE_BYTE_SLOTS)
3048 complete_char = 0;
3049
3050 if (i >= CHAR_TABLE_SINGLE_BYTE_SLOTS
3051 && !CHARSET_DEFINED_P (i - 128))
3052 continue;
3053
3054 definition
3055 = get_keyelt (XCHAR_TABLE (vector)->contents[i], 0);
3056 }
3057 else
3058 definition = get_keyelt (AREF (vector, i), 0);
3059
3060 if (NILP (definition)) continue;
3061
3062 /* Don't mention suppressed commands. */
3063 if (SYMBOLP (definition) && partial)
3064 {
3065 Lisp_Object tem;
3066
3067 tem = Fget (definition, suppress);
3068
3069 if (!NILP (tem)) continue;
3070 }
3071
3072 /* Set CHARACTER to the character this entry describes, if any.
3073 Also update *INDICES. */
3074 if (CHAR_TABLE_P (vector))
3075 {
3076 indices[char_table_depth] = i;
3077
3078 if (char_table_depth == 0)
3079 {
3080 character = i;
3081 indices[0] = i - 128;
3082 }
3083 else if (complete_char)
3084 {
3085 character = MAKE_CHAR (indices[0], indices[1], indices[2]);
3086 }
3087 else
3088 character = 0;
3089 }
3090 else
3091 character = i;
3092
3093 /* If this binding is shadowed by some other map, ignore it. */
3094 if (!NILP (shadow) && complete_char)
3095 {
3096 Lisp_Object tem;
3097
3098 ASET (kludge, 0, make_number (character));
3099 tem = shadow_lookup (shadow, kludge, Qt);
3100
3101 if (!NILP (tem)) continue;
3102 }
3103
3104 /* Ignore this definition if it is shadowed by an earlier
3105 one in the same keymap. */
3106 if (!NILP (entire_map) && complete_char)
3107 {
3108 Lisp_Object tem;
3109
3110 ASET (kludge, 0, make_number (character));
3111 tem = Flookup_key (entire_map, kludge, Qt);
3112
3113 if (!EQ (tem, definition))
3114 continue;
3115 }
3116
3117 if (first)
3118 {
3119 if (char_table_depth == 0)
3120 insert ("\n", 1);
3121 first = 0;
3122 }
3123
3124 /* For a sub char-table, show the depth by indentation.
3125 CHAR_TABLE_DEPTH can be greater than 0 only for a char-table. */
3126 if (char_table_depth > 0)
3127 insert (" ", char_table_depth * 2); /* depth is 1 or 2. */
3128
3129 /* Output the prefix that applies to every entry in this map. */
3130 if (!NILP (elt_prefix))
3131 insert1 (elt_prefix);
3132
3133 /* Insert or describe the character this slot is for,
3134 or a description of what it is for. */
3135 if (SUB_CHAR_TABLE_P (vector))
3136 {
3137 if (complete_char)
3138 insert_char (character);
3139 else
3140 {
3141 /* We need an octal representation for this block of
3142 characters. */
3143 char work[16];
3144 sprintf (work, "(row %d)", i);
3145 insert (work, strlen (work));
3146 }
3147 }
3148 else if (CHAR_TABLE_P (vector))
3149 {
3150 if (complete_char)
3151 insert1 (Fsingle_key_description (make_number (character), Qnil));
3152 else
3153 {
3154 /* Print the information for this character set. */
3155 insert_string ("<");
3156 tem2 = CHARSET_TABLE_INFO (i - 128, CHARSET_SHORT_NAME_IDX);
3157 if (STRINGP (tem2))
3158 insert_from_string (tem2, 0, 0, XSTRING (tem2)->size,
3159 STRING_BYTES (XSTRING (tem2)), 0);
3160 else
3161 insert ("?", 1);
3162 insert (">", 1);
3163 }
3164 }
3165 else
3166 {
3167 insert1 (Fsingle_key_description (make_number (character), Qnil));
3168 }
3169
3170 /* If we find a sub char-table within a char-table,
3171 scan it recursively; it defines the details for
3172 a character set or a portion of a character set. */
3173 if (CHAR_TABLE_P (vector) && SUB_CHAR_TABLE_P (definition))
3174 {
3175 insert ("\n", 1);
3176 describe_vector (definition, elt_prefix, elt_describer,
3177 partial, shadow, entire_map,
3178 indices, char_table_depth + 1);
3179 continue;
3180 }
3181
3182 starting_i = i;
3183
3184 /* Find all consecutive characters or rows that have the same
3185 definition. But, for elements of a top level char table, if
3186 they are for charsets, we had better describe one by one even
3187 if they have the same definition. */
3188 if (CHAR_TABLE_P (vector))
3189 {
3190 int limit = to;
3191
3192 if (char_table_depth == 0)
3193 limit = CHAR_TABLE_SINGLE_BYTE_SLOTS;
3194
3195 while (i + 1 < limit
3196 && (tem2 = get_keyelt (XCHAR_TABLE (vector)->contents[i + 1], 0),
3197 !NILP (tem2))
3198 && !NILP (Fequal (tem2, definition)))
3199 i++;
3200 }
3201 else
3202 while (i + 1 < to
3203 && (tem2 = get_keyelt (AREF (vector, i + 1), 0),
3204 !NILP (tem2))
3205 && !NILP (Fequal (tem2, definition)))
3206 i++;
3207
3208
3209 /* If we have a range of more than one character,
3210 print where the range reaches to. */
3211
3212 if (i != starting_i)
3213 {
3214 insert (" .. ", 4);
3215
3216 if (!NILP (elt_prefix))
3217 insert1 (elt_prefix);
3218
3219 if (CHAR_TABLE_P (vector))
3220 {
3221 if (char_table_depth == 0)
3222 {
3223 insert1 (Fsingle_key_description (make_number (i), Qnil));
3224 }
3225 else if (complete_char)
3226 {
3227 indices[char_table_depth] = i;
3228 character = MAKE_CHAR (indices[0], indices[1], indices[2]);
3229 insert_char (character);
3230 }
3231 else
3232 {
3233 /* We need an octal representation for this block of
3234 characters. */
3235 char work[16];
3236 sprintf (work, "(row %d)", i);
3237 insert (work, strlen (work));
3238 }
3239 }
3240 else
3241 {
3242 insert1 (Fsingle_key_description (make_number (i), Qnil));
3243 }
3244 }
3245
3246 /* Print a description of the definition of this character.
3247 elt_describer will take care of spacing out far enough
3248 for alignment purposes. */
3249 (*elt_describer) (definition);
3250 }
3251
3252 /* For (sub) char-table, print `defalt' slot at last. */
3253 if (CHAR_TABLE_P (vector) && !NILP (XCHAR_TABLE (vector)->defalt))
3254 {
3255 insert (" ", char_table_depth * 2);
3256 insert_string ("<<default>>");
3257 (*elt_describer) (XCHAR_TABLE (vector)->defalt);
3258 }
3259
3260 UNGCPRO;
3261 }
3262 \f
3263 /* Apropos - finding all symbols whose names match a regexp. */
3264 Lisp_Object apropos_predicate;
3265 Lisp_Object apropos_accumulate;
3266
3267 static void
3268 apropos_accum (symbol, string)
3269 Lisp_Object symbol, string;
3270 {
3271 register Lisp_Object tem;
3272
3273 tem = Fstring_match (string, Fsymbol_name (symbol), Qnil);
3274 if (!NILP (tem) && !NILP (apropos_predicate))
3275 tem = call1 (apropos_predicate, symbol);
3276 if (!NILP (tem))
3277 apropos_accumulate = Fcons (symbol, apropos_accumulate);
3278 }
3279
3280 DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0,
3281 "Show all symbols whose names contain match for REGEXP.\n\
3282 If optional 2nd arg PREDICATE is non-nil, (funcall PREDICATE SYMBOL) is done\n\
3283 for each symbol and a symbol is mentioned only if that returns non-nil.\n\
3284 Return list of symbols found.")
3285 (regexp, predicate)
3286 Lisp_Object regexp, predicate;
3287 {
3288 struct gcpro gcpro1, gcpro2;
3289 CHECK_STRING (regexp, 0);
3290 apropos_predicate = predicate;
3291 GCPRO2 (apropos_predicate, apropos_accumulate);
3292 apropos_accumulate = Qnil;
3293 map_obarray (Vobarray, apropos_accum, regexp);
3294 apropos_accumulate = Fsort (apropos_accumulate, Qstring_lessp);
3295 UNGCPRO;
3296 return apropos_accumulate;
3297 }
3298 \f
3299 void
3300 syms_of_keymap ()
3301 {
3302 Qkeymap = intern ("keymap");
3303 staticpro (&Qkeymap);
3304
3305 /* Now we are ready to set up this property, so we can
3306 create char tables. */
3307 Fput (Qkeymap, Qchar_table_extra_slots, make_number (0));
3308
3309 /* Initialize the keymaps standardly used.
3310 Each one is the value of a Lisp variable, and is also
3311 pointed to by a C variable */
3312
3313 global_map = Fmake_keymap (Qnil);
3314 Fset (intern ("global-map"), global_map);
3315
3316 current_global_map = global_map;
3317 staticpro (&global_map);
3318 staticpro (&current_global_map);
3319
3320 meta_map = Fmake_keymap (Qnil);
3321 Fset (intern ("esc-map"), meta_map);
3322 Ffset (intern ("ESC-prefix"), meta_map);
3323
3324 control_x_map = Fmake_keymap (Qnil);
3325 Fset (intern ("ctl-x-map"), control_x_map);
3326 Ffset (intern ("Control-X-prefix"), control_x_map);
3327
3328 DEFVAR_LISP ("define-key-rebound-commands", &Vdefine_key_rebound_commands,
3329 "List of commands given new key bindings recently.\n\
3330 This is used for internal purposes during Emacs startup;\n\
3331 don't alter it yourself.");
3332 Vdefine_key_rebound_commands = Qt;
3333
3334 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map,
3335 "Default keymap to use when reading from the minibuffer.");
3336 Vminibuffer_local_map = Fmake_sparse_keymap (Qnil);
3337
3338 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map,
3339 "Local keymap for the minibuffer when spaces are not allowed.");
3340 Vminibuffer_local_ns_map = Fmake_sparse_keymap (Qnil);
3341 Fset_keymap_parent (Vminibuffer_local_ns_map, Vminibuffer_local_map);
3342
3343 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map,
3344 "Local keymap for minibuffer input with completion.");
3345 Vminibuffer_local_completion_map = Fmake_sparse_keymap (Qnil);
3346 Fset_keymap_parent (Vminibuffer_local_completion_map, Vminibuffer_local_map);
3347
3348 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map,
3349 "Local keymap for minibuffer input with completion, for exact match.");
3350 Vminibuffer_local_must_match_map = Fmake_sparse_keymap (Qnil);
3351 Fset_keymap_parent (Vminibuffer_local_must_match_map,
3352 Vminibuffer_local_completion_map);
3353
3354 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist,
3355 "Alist of keymaps to use for minor modes.\n\
3356 Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read\n\
3357 key sequences and look up bindings iff VARIABLE's value is non-nil.\n\
3358 If two active keymaps bind the same key, the keymap appearing earlier\n\
3359 in the list takes precedence.");
3360 Vminor_mode_map_alist = Qnil;
3361
3362 DEFVAR_LISP ("minor-mode-overriding-map-alist", &Vminor_mode_overriding_map_alist,
3363 "Alist of keymaps to use for minor modes, in current major mode.\n\
3364 This variable is a alist just like `minor-mode-map-alist', and it is\n\
3365 used the same way (and before `minor-mode-map-alist'); however,\n\
3366 it is provided for major modes to bind locally.");
3367 Vminor_mode_overriding_map_alist = Qnil;
3368
3369 DEFVAR_LISP ("function-key-map", &Vfunction_key_map,
3370 "Keymap mapping ASCII function key sequences onto their preferred forms.\n\
3371 This allows Emacs to recognize function keys sent from ASCII\n\
3372 terminals at any point in a key sequence.\n\
3373 \n\
3374 The `read-key-sequence' function replaces any subsequence bound by\n\
3375 `function-key-map' with its binding. More precisely, when the active\n\
3376 keymaps have no binding for the current key sequence but\n\
3377 `function-key-map' binds a suffix of the sequence to a vector or string,\n\
3378 `read-key-sequence' replaces the matching suffix with its binding, and\n\
3379 continues with the new sequence.\n\
3380 \n\
3381 The events that come from bindings in `function-key-map' are not\n\
3382 themselves looked up in `function-key-map'.\n\
3383 \n\
3384 For example, suppose `function-key-map' binds `ESC O P' to [f1].\n\
3385 Typing `ESC O P' to `read-key-sequence' would return [f1]. Typing\n\
3386 `C-x ESC O P' would return [?\\C-x f1]. If [f1] were a prefix\n\
3387 key, typing `ESC O P x' would return [f1 x].");
3388 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
3389
3390 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
3391 "Keymap of key translations that can override keymaps.\n\
3392 This keymap works like `function-key-map', but comes after that,\n\
3393 and applies even for keys that have ordinary bindings.");
3394 Vkey_translation_map = Qnil;
3395
3396 Qsingle_key_description = intern ("single-key-description");
3397 staticpro (&Qsingle_key_description);
3398
3399 Qkey_description = intern ("key-description");
3400 staticpro (&Qkey_description);
3401
3402 Qkeymapp = intern ("keymapp");
3403 staticpro (&Qkeymapp);
3404
3405 Qnon_ascii = intern ("non-ascii");
3406 staticpro (&Qnon_ascii);
3407
3408 Qmenu_item = intern ("menu-item");
3409 staticpro (&Qmenu_item);
3410
3411 where_is_cache_keymaps = Qt;
3412 where_is_cache = Qnil;
3413 staticpro (&where_is_cache);
3414 staticpro (&where_is_cache_keymaps);
3415
3416 defsubr (&Skeymapp);
3417 defsubr (&Skeymap_parent);
3418 defsubr (&Skeymap_prompt);
3419 defsubr (&Sset_keymap_parent);
3420 defsubr (&Smake_keymap);
3421 defsubr (&Smake_sparse_keymap);
3422 defsubr (&Scopy_keymap);
3423 defsubr (&Skey_binding);
3424 defsubr (&Slocal_key_binding);
3425 defsubr (&Sglobal_key_binding);
3426 defsubr (&Sminor_mode_key_binding);
3427 defsubr (&Sdefine_key);
3428 defsubr (&Slookup_key);
3429 defsubr (&Sdefine_prefix_command);
3430 defsubr (&Suse_global_map);
3431 defsubr (&Suse_local_map);
3432 defsubr (&Scurrent_local_map);
3433 defsubr (&Scurrent_global_map);
3434 defsubr (&Scurrent_minor_mode_maps);
3435 defsubr (&Scurrent_active_maps);
3436 defsubr (&Saccessible_keymaps);
3437 defsubr (&Skey_description);
3438 defsubr (&Sdescribe_vector);
3439 defsubr (&Ssingle_key_description);
3440 defsubr (&Stext_char_description);
3441 defsubr (&Swhere_is_internal);
3442 defsubr (&Sdescribe_buffer_bindings);
3443 defsubr (&Sapropos_internal);
3444 }
3445
3446 void
3447 keys_of_keymap ()
3448 {
3449 initial_define_key (global_map, 033, "ESC-prefix");
3450 initial_define_key (global_map, Ctl('X'), "Control-X-prefix");
3451 }