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