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