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