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