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