(x_term_init): Turn off polling around XtOpenDisplay.
[bpt/emacs.git] / src / keymap.c
... / ...
CommitLineData
1/* Manipulation of keymaps
2 Copyright (C) 1985, 86,87,88,93,94,95,98,99, 2000, 2001
3 Free Software Foundation, Inc.
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, 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
41Lisp_Object current_global_map; /* Current global keymap */
42
43Lisp_Object global_map; /* default global key bindings */
44
45Lisp_Object meta_map; /* The keymap used for globally bound
46 ESC-prefixed default commands */
47
48Lisp_Object control_x_map; /* The keymap used for globally bound
49 C-x-prefixed default commands */
50
51/* was MinibufLocalMap */
52Lisp_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 */
58Lisp_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 */
65Lisp_Object Vminibuffer_local_completion_map;
66
67/* keymap used for minibuffers when doing completion and require a match */
68/* was MinibufLocalMustMatchMap */
69Lisp_Object Vminibuffer_local_must_match_map;
70
71/* Alist of minor mode variables and keymaps. */
72Lisp_Object Vminor_mode_map_alist;
73
74/* Alist of major-mode-specific overrides for
75 minor mode variables and keymaps. */
76Lisp_Object Vminor_mode_overriding_map_alist;
77
78/* List of emulation mode keymap alists. */
79Lisp_Object Vemulation_mode_map_alists;
80
81/* Keymap mapping ASCII function key sequences onto their preferred forms.
82 Initialized by the terminal-specific lisp files. See DEFVAR for more
83 documentation. */
84Lisp_Object Vfunction_key_map;
85
86/* Keymap mapping ASCII function key sequences onto their preferred forms. */
87Lisp_Object Vkey_translation_map;
88
89/* A list of all commands given new bindings since a certain time
90 when nil was stored here.
91 This is used to speed up recomputation of menu key equivalents
92 when Emacs starts up. t means don't record anything here. */
93Lisp_Object Vdefine_key_rebound_commands;
94
95Lisp_Object Qkeymapp, Qkeymap, Qnon_ascii, Qmenu_item, Qremap;
96
97/* Alist of elements like (DEL . "\d"). */
98static Lisp_Object exclude_keys;
99
100/* Pre-allocated 2-element vector for Fremap_command to use. */
101static Lisp_Object remap_command_vector;
102
103/* A char with the CHAR_META bit set in a vector or the 0200 bit set
104 in a string key sequence is equivalent to prefixing with this
105 character. */
106extern Lisp_Object meta_prefix_char;
107
108extern Lisp_Object Voverriding_local_map;
109
110/* Hash table used to cache a reverse-map to speed up calls to where-is. */
111static Lisp_Object where_is_cache;
112/* Which keymaps are reverse-stored in the cache. */
113static Lisp_Object where_is_cache_keymaps;
114
115static Lisp_Object store_in_keymap P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
116static void fix_submap_inheritance P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
117
118static Lisp_Object define_as_prefix P_ ((Lisp_Object, Lisp_Object));
119static void describe_command P_ ((Lisp_Object, Lisp_Object));
120static void describe_translation P_ ((Lisp_Object, Lisp_Object));
121static void describe_map P_ ((Lisp_Object, Lisp_Object,
122 void (*) P_ ((Lisp_Object, Lisp_Object)),
123 int, Lisp_Object, Lisp_Object*, int));
124static void silly_event_symbol_error P_ ((Lisp_Object));
125\f
126/* Keymap object support - constructors and predicates. */
127
128DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 1, 0,
129 doc: /* Construct and return a new keymap, of the form (keymap CHARTABLE . ALIST).
130CHARTABLE is a char-table that holds the bindings for the ASCII
131characters. ALIST is an assoc-list which holds bindings for function keys,
132mouse events, and any other things that appear in the input stream.
133All entries in it are initially nil, meaning "command undefined".
134
135The optional arg STRING supplies a menu name for the keymap
136in case you use it as a menu with `x-popup-menu'. */)
137 (string)
138 Lisp_Object string;
139{
140 Lisp_Object tail;
141 if (!NILP (string))
142 tail = Fcons (string, Qnil);
143 else
144 tail = Qnil;
145 return Fcons (Qkeymap,
146 Fcons (Fmake_char_table (Qkeymap, Qnil), tail));
147}
148
149DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 1, 0,
150 doc: /* Construct and return a new sparse keymap.
151Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),
152which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),
153which binds the function key or mouse event SYMBOL to DEFINITION.
154Initially the alist is nil.
155
156The optional arg STRING supplies a menu name for the keymap
157in case you use it as a menu with `x-popup-menu'. */)
158 (string)
159 Lisp_Object string;
160{
161 if (!NILP (string))
162 return Fcons (Qkeymap, Fcons (string, Qnil));
163 return Fcons (Qkeymap, Qnil);
164}
165
166/* This function is used for installing the standard key bindings
167 at initialization time.
168
169 For example:
170
171 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); */
172
173void
174initial_define_key (keymap, key, defname)
175 Lisp_Object keymap;
176 int key;
177 char *defname;
178{
179 store_in_keymap (keymap, make_number (key), intern (defname));
180}
181
182void
183initial_define_lispy_key (keymap, keyname, defname)
184 Lisp_Object keymap;
185 char *keyname;
186 char *defname;
187{
188 store_in_keymap (keymap, intern (keyname), intern (defname));
189}
190
191DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0,
192 doc: /* Return t if OBJECT is a keymap.
193
194A keymap is a list (keymap . ALIST),
195or a symbol whose function definition is itself a keymap.
196ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);
197a vector of densely packed bindings for small character codes
198is also allowed as an element. */)
199 (object)
200 Lisp_Object object;
201{
202 return (KEYMAPP (object) ? Qt : Qnil);
203}
204
205DEFUN ("keymap-prompt", Fkeymap_prompt, Skeymap_prompt, 1, 1, 0,
206 doc: /* Return the prompt-string of a keymap MAP.
207If non-nil, the prompt is shown in the echo-area
208when reading a key-sequence to be looked-up in this keymap. */)
209 (map)
210 Lisp_Object map;
211{
212 while (CONSP (map))
213 {
214 register Lisp_Object tem;
215 tem = Fcar (map);
216 if (STRINGP (tem))
217 return tem;
218 map = Fcdr (map);
219 }
220 return Qnil;
221}
222
223/* Check that OBJECT is a keymap (after dereferencing through any
224 symbols). If it is, return it.
225
226 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
227 is an autoload form, do the autoload and try again.
228 If AUTOLOAD is nonzero, callers must assume GC is possible.
229
230 If the map needs to be autoloaded, but AUTOLOAD is zero (and ERROR
231 is zero as well), return Qt.
232
233 ERROR controls how we respond if OBJECT isn't a keymap.
234 If ERROR is non-zero, signal an error; otherwise, just return Qnil.
235
236 Note that most of the time, we don't want to pursue autoloads.
237 Functions like Faccessible_keymaps which scan entire keymap trees
238 shouldn't load every autoloaded keymap. I'm not sure about this,
239 but it seems to me that only read_key_sequence, Flookup_key, and
240 Fdefine_key should cause keymaps to be autoloaded.
241
242 This function can GC when AUTOLOAD is non-zero, because it calls
243 do_autoload which can GC. */
244
245Lisp_Object
246get_keymap (object, error, autoload)
247 Lisp_Object object;
248 int error, autoload;
249{
250 Lisp_Object tem;
251
252 autoload_retry:
253 if (NILP (object))
254 goto end;
255 if (CONSP (object) && EQ (XCAR (object), Qkeymap))
256 return object;
257
258 tem = indirect_function (object);
259 if (CONSP (tem))
260 {
261 if (EQ (XCAR (tem), Qkeymap))
262 return tem;
263
264 /* Should we do an autoload? Autoload forms for keymaps have
265 Qkeymap as their fifth element. */
266 if ((autoload || !error) && EQ (XCAR (tem), Qautoload))
267 {
268 Lisp_Object tail;
269
270 tail = Fnth (make_number (4), tem);
271 if (EQ (tail, Qkeymap))
272 {
273 if (autoload)
274 {
275 struct gcpro gcpro1, gcpro2;
276
277 GCPRO2 (tem, object);
278 do_autoload (tem, object);
279 UNGCPRO;
280
281 goto autoload_retry;
282 }
283 else
284 return Qt;
285 }
286 }
287 }
288
289 end:
290 if (error)
291 wrong_type_argument (Qkeymapp, object);
292 return Qnil;
293}
294\f
295/* Return the parent map of KEYMAP, or nil if it has none.
296 We assume that KEYMAP is a valid keymap. */
297
298Lisp_Object
299keymap_parent (keymap, autoload)
300 Lisp_Object keymap;
301 int autoload;
302{
303 Lisp_Object list;
304
305 keymap = get_keymap (keymap, 1, autoload);
306
307 /* Skip past the initial element `keymap'. */
308 list = XCDR (keymap);
309 for (; CONSP (list); list = XCDR (list))
310 {
311 /* See if there is another `keymap'. */
312 if (KEYMAPP (list))
313 return list;
314 }
315
316 return get_keymap (list, 0, autoload);
317}
318
319DEFUN ("keymap-parent", Fkeymap_parent, Skeymap_parent, 1, 1, 0,
320 doc: /* Return the parent keymap of KEYMAP. */)
321 (keymap)
322 Lisp_Object keymap;
323{
324 return keymap_parent (keymap, 1);
325}
326
327/* Check whether MAP is one of MAPS parents. */
328int
329keymap_memberp (map, maps)
330 Lisp_Object map, maps;
331{
332 if (NILP (map)) return 0;
333 while (KEYMAPP (maps) && !EQ (map, maps))
334 maps = keymap_parent (maps, 0);
335 return (EQ (map, maps));
336}
337
338/* Set the parent keymap of MAP to PARENT. */
339
340DEFUN ("set-keymap-parent", Fset_keymap_parent, Sset_keymap_parent, 2, 2, 0,
341 doc: /* Modify KEYMAP to set its parent map to PARENT.
342PARENT should be nil or another keymap. */)
343 (keymap, parent)
344 Lisp_Object keymap, parent;
345{
346 Lisp_Object list, prev;
347 struct gcpro gcpro1, gcpro2;
348 int i;
349
350 /* Force a keymap flush for the next call to where-is.
351 Since this can be called from within where-is, we don't set where_is_cache
352 directly but only where_is_cache_keymaps, since where_is_cache shouldn't
353 be changed during where-is, while where_is_cache_keymaps is only used at
354 the very beginning of where-is and can thus be changed here without any
355 adverse effect.
356 This is a very minor correctness (rather than safety) issue. */
357 where_is_cache_keymaps = Qt;
358
359 GCPRO2 (keymap, parent);
360 keymap = get_keymap (keymap, 1, 1);
361
362 if (!NILP (parent))
363 {
364 parent = get_keymap (parent, 1, 1);
365
366 /* Check for cycles. */
367 if (keymap_memberp (keymap, parent))
368 error ("Cyclic keymap inheritance");
369 }
370
371 /* Skip past the initial element `keymap'. */
372 prev = keymap;
373 while (1)
374 {
375 list = XCDR (prev);
376 /* If there is a parent keymap here, replace it.
377 If we came to the end, add the parent in PREV. */
378 if (!CONSP (list) || KEYMAPP (list))
379 {
380 /* If we already have the right parent, return now
381 so that we avoid the loops below. */
382 if (EQ (XCDR (prev), parent))
383 RETURN_UNGCPRO (parent);
384
385 XSETCDR (prev, parent);
386 break;
387 }
388 prev = list;
389 }
390
391 /* Scan through for submaps, and set their parents too. */
392
393 for (list = XCDR (keymap); CONSP (list); list = XCDR (list))
394 {
395 /* Stop the scan when we come to the parent. */
396 if (EQ (XCAR (list), Qkeymap))
397 break;
398
399 /* If this element holds a prefix map, deal with it. */
400 if (CONSP (XCAR (list))
401 && CONSP (XCDR (XCAR (list))))
402 fix_submap_inheritance (keymap, XCAR (XCAR (list)),
403 XCDR (XCAR (list)));
404
405 if (VECTORP (XCAR (list)))
406 for (i = 0; i < XVECTOR (XCAR (list))->size; i++)
407 if (CONSP (XVECTOR (XCAR (list))->contents[i]))
408 fix_submap_inheritance (keymap, make_number (i),
409 XVECTOR (XCAR (list))->contents[i]);
410
411 if (CHAR_TABLE_P (XCAR (list)))
412 {
413 Lisp_Object indices[3];
414
415 map_char_table (fix_submap_inheritance, Qnil, XCAR (list),
416 keymap, 0, indices);
417 }
418 }
419
420 RETURN_UNGCPRO (parent);
421}
422
423/* EVENT is defined in MAP as a prefix, and SUBMAP is its definition.
424 if EVENT is also a prefix in MAP's parent,
425 make sure that SUBMAP inherits that definition as its own parent. */
426
427static void
428fix_submap_inheritance (map, event, submap)
429 Lisp_Object map, event, submap;
430{
431 Lisp_Object map_parent, parent_entry;
432
433 /* SUBMAP is a cons that we found as a key binding.
434 Discard the other things found in a menu key binding. */
435
436 submap = get_keymap (get_keyelt (submap, 0), 0, 0);
437
438 /* If it isn't a keymap now, there's no work to do. */
439 if (!CONSP (submap))
440 return;
441
442 map_parent = keymap_parent (map, 0);
443 if (!NILP (map_parent))
444 parent_entry =
445 get_keymap (access_keymap (map_parent, event, 0, 0, 0), 0, 0);
446 else
447 parent_entry = Qnil;
448
449 /* If MAP's parent has something other than a keymap,
450 our own submap shadows it completely. */
451 if (!CONSP (parent_entry))
452 return;
453
454 if (! EQ (parent_entry, submap))
455 {
456 Lisp_Object submap_parent;
457 submap_parent = submap;
458 while (1)
459 {
460 Lisp_Object tem;
461
462 tem = keymap_parent (submap_parent, 0);
463
464 if (KEYMAPP (tem))
465 {
466 if (keymap_memberp (tem, parent_entry))
467 /* Fset_keymap_parent could create a cycle. */
468 return;
469 submap_parent = tem;
470 }
471 else
472 break;
473 }
474 Fset_keymap_parent (submap_parent, parent_entry);
475 }
476}
477\f
478/* Look up IDX in MAP. IDX may be any sort of event.
479 Note that this does only one level of lookup; IDX must be a single
480 event, not a sequence.
481
482 If T_OK is non-zero, bindings for Qt are treated as default
483 bindings; any key left unmentioned by other tables and bindings is
484 given the binding of Qt.
485
486 If T_OK is zero, bindings for Qt are not treated specially.
487
488 If NOINHERIT, don't accept a subkeymap found in an inherited keymap. */
489
490Lisp_Object
491access_keymap (map, idx, t_ok, noinherit, autoload)
492 Lisp_Object map;
493 Lisp_Object idx;
494 int t_ok;
495 int noinherit;
496 int autoload;
497{
498 Lisp_Object val;
499
500 /* Qunbound in VAL means we have found no binding yet. */
501 val = Qunbound;
502
503 /* If idx is a list (some sort of mouse click, perhaps?),
504 the index we want to use is the car of the list, which
505 ought to be a symbol. */
506 idx = EVENT_HEAD (idx);
507
508 /* If idx is a symbol, it might have modifiers, which need to
509 be put in the canonical order. */
510 if (SYMBOLP (idx))
511 idx = reorder_modifiers (idx);
512 else if (INTEGERP (idx))
513 /* Clobber the high bits that can be present on a machine
514 with more than 24 bits of integer. */
515 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
516
517 /* Handle the special meta -> esc mapping. */
518 if (INTEGERP (idx) && XUINT (idx) & meta_modifier)
519 {
520 /* See if there is a meta-map. If there's none, there is
521 no binding for IDX, unless a default binding exists in MAP. */
522 struct gcpro gcpro1;
523 Lisp_Object meta_map;
524 GCPRO1 (map);
525 meta_map = get_keymap (access_keymap (map, meta_prefix_char,
526 t_ok, noinherit, autoload),
527 0, autoload);
528 UNGCPRO;
529 if (CONSP (meta_map))
530 {
531 map = meta_map;
532 idx = make_number (XUINT (idx) & ~meta_modifier);
533 }
534 else if (t_ok)
535 /* Set IDX to t, so that we only find a default binding. */
536 idx = Qt;
537 else
538 /* We know there is no binding. */
539 return Qnil;
540 }
541
542 /* t_binding is where we put a default binding that applies,
543 to use in case we do not find a binding specifically
544 for this key sequence. */
545 {
546 Lisp_Object tail;
547 Lisp_Object t_binding = Qnil;
548 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
549
550 GCPRO4 (map, tail, idx, t_binding);
551
552 /* If `t_ok' is 2, both `t' and generic-char bindings are accepted.
553 If it is 1, only generic-char bindings are accepted.
554 Otherwise, neither are. */
555 t_ok = t_ok ? 2 : 0;
556
557 for (tail = XCDR (map);
558 (CONSP (tail)
559 || (tail = get_keymap (tail, 0, autoload), CONSP (tail)));
560 tail = XCDR (tail))
561 {
562 Lisp_Object binding;
563
564 binding = XCAR (tail);
565 if (SYMBOLP (binding))
566 {
567 /* If NOINHERIT, stop finding prefix definitions
568 after we pass a second occurrence of the `keymap' symbol. */
569 if (noinherit && EQ (binding, Qkeymap))
570 RETURN_UNGCPRO (Qnil);
571 }
572 else if (CONSP (binding))
573 {
574 Lisp_Object key = XCAR (binding);
575
576 if (EQ (key, idx))
577 val = XCDR (binding);
578 else if (t_ok
579 && INTEGERP (idx)
580 && (XINT (idx) & CHAR_MODIFIER_MASK) == 0
581 && INTEGERP (key)
582 && (XINT (key) & CHAR_MODIFIER_MASK) == 0
583 && !SINGLE_BYTE_CHAR_P (XINT (idx))
584 && !SINGLE_BYTE_CHAR_P (XINT (key))
585 && CHAR_VALID_P (XINT (key), 1)
586 && !CHAR_VALID_P (XINT (key), 0)
587 && (CHAR_CHARSET (XINT (key))
588 == CHAR_CHARSET (XINT (idx))))
589 {
590 /* KEY is the generic character of the charset of IDX.
591 Use KEY's binding if there isn't a binding for IDX
592 itself. */
593 t_binding = XCDR (binding);
594 t_ok = 0;
595 }
596 else if (t_ok > 1 && EQ (key, Qt))
597 {
598 t_binding = XCDR (binding);
599 t_ok = 1;
600 }
601 }
602 else if (VECTORP (binding))
603 {
604 if (NATNUMP (idx) && XFASTINT (idx) < ASIZE (binding))
605 val = AREF (binding, XFASTINT (idx));
606 }
607 else if (CHAR_TABLE_P (binding))
608 {
609 /* Character codes with modifiers
610 are not included in a char-table.
611 All character codes without modifiers are included. */
612 if (NATNUMP (idx) && (XFASTINT (idx) & CHAR_MODIFIER_MASK) == 0)
613 {
614 val = Faref (binding, idx);
615 /* `nil' has a special meaning for char-tables, so
616 we use something else to record an explicitly
617 unbound entry. */
618 if (NILP (val))
619 val = Qunbound;
620 }
621 }
622
623 /* If we found a binding, clean it up and return it. */
624 if (!EQ (val, Qunbound))
625 {
626 if (EQ (val, Qt))
627 /* A Qt binding is just like an explicit nil binding
628 (i.e. it shadows any parent binding but not bindings in
629 keymaps of lower precedence). */
630 val = Qnil;
631 val = get_keyelt (val, autoload);
632 if (KEYMAPP (val))
633 fix_submap_inheritance (map, idx, val);
634 RETURN_UNGCPRO (val);
635 }
636 QUIT;
637 }
638 UNGCPRO;
639 return get_keyelt (t_binding, autoload);
640 }
641}
642
643/* Given OBJECT which was found in a slot in a keymap,
644 trace indirect definitions to get the actual definition of that slot.
645 An indirect definition is a list of the form
646 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
647 and INDEX is the object to look up in KEYMAP to yield the definition.
648
649 Also if OBJECT has a menu string as the first element,
650 remove that. Also remove a menu help string as second element.
651
652 If AUTOLOAD is nonzero, load autoloadable keymaps
653 that are referred to with indirection. */
654
655Lisp_Object
656get_keyelt (object, autoload)
657 Lisp_Object object;
658 int autoload;
659{
660 while (1)
661 {
662 if (!(CONSP (object)))
663 /* This is really the value. */
664 return object;
665
666 /* If the keymap contents looks like (keymap ...) or (lambda ...)
667 then use itself. */
668 else if (EQ (XCAR (object), Qkeymap) || EQ (XCAR (object), Qlambda))
669 return object;
670
671 /* If the keymap contents looks like (menu-item name . DEFN)
672 or (menu-item name DEFN ...) then use DEFN.
673 This is a new format menu item. */
674 else if (EQ (XCAR (object), Qmenu_item))
675 {
676 if (CONSP (XCDR (object)))
677 {
678 Lisp_Object tem;
679
680 object = XCDR (XCDR (object));
681 tem = object;
682 if (CONSP (object))
683 object = XCAR (object);
684
685 /* If there's a `:filter FILTER', apply FILTER to the
686 menu-item's definition to get the real definition to
687 use. */
688 for (; CONSP (tem) && CONSP (XCDR (tem)); tem = XCDR (tem))
689 if (EQ (XCAR (tem), QCfilter) && autoload)
690 {
691 Lisp_Object filter;
692 filter = XCAR (XCDR (tem));
693 filter = list2 (filter, list2 (Qquote, object));
694 object = menu_item_eval_property (filter);
695 break;
696 }
697 }
698 else
699 /* Invalid keymap. */
700 return object;
701 }
702
703 /* If the keymap contents looks like (STRING . DEFN), use DEFN.
704 Keymap alist elements like (CHAR MENUSTRING . DEFN)
705 will be used by HierarKey menus. */
706 else if (STRINGP (XCAR (object)))
707 {
708 object = XCDR (object);
709 /* Also remove a menu help string, if any,
710 following the menu item name. */
711 if (CONSP (object) && STRINGP (XCAR (object)))
712 object = XCDR (object);
713 /* Also remove the sublist that caches key equivalences, if any. */
714 if (CONSP (object) && CONSP (XCAR (object)))
715 {
716 Lisp_Object carcar;
717 carcar = XCAR (XCAR (object));
718 if (NILP (carcar) || VECTORP (carcar))
719 object = XCDR (object);
720 }
721 }
722
723 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
724 else
725 {
726 struct gcpro gcpro1;
727 Lisp_Object map;
728 GCPRO1 (object);
729 map = get_keymap (Fcar_safe (object), 0, autoload);
730 UNGCPRO;
731 return (!CONSP (map) ? object /* Invalid keymap */
732 : access_keymap (map, Fcdr (object), 0, 0, autoload));
733 }
734 }
735}
736
737static Lisp_Object
738store_in_keymap (keymap, idx, def)
739 Lisp_Object keymap;
740 register Lisp_Object idx;
741 register Lisp_Object def;
742{
743 /* Flush any reverse-map cache. */
744 where_is_cache = Qnil;
745 where_is_cache_keymaps = Qt;
746
747 /* If we are preparing to dump, and DEF is a menu element
748 with a menu item indicator, copy it to ensure it is not pure. */
749 if (CONSP (def) && PURE_P (def)
750 && (EQ (XCAR (def), Qmenu_item) || STRINGP (XCAR (def))))
751 def = Fcons (XCAR (def), XCDR (def));
752
753 if (!CONSP (keymap) || !EQ (XCAR (keymap), Qkeymap))
754 error ("attempt to define a key in a non-keymap");
755
756 /* If idx is a list (some sort of mouse click, perhaps?),
757 the index we want to use is the car of the list, which
758 ought to be a symbol. */
759 idx = EVENT_HEAD (idx);
760
761 /* If idx is a symbol, it might have modifiers, which need to
762 be put in the canonical order. */
763 if (SYMBOLP (idx))
764 idx = reorder_modifiers (idx);
765 else if (INTEGERP (idx))
766 /* Clobber the high bits that can be present on a machine
767 with more than 24 bits of integer. */
768 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
769
770 /* Scan the keymap for a binding of idx. */
771 {
772 Lisp_Object tail;
773
774 /* The cons after which we should insert new bindings. If the
775 keymap has a table element, we record its position here, so new
776 bindings will go after it; this way, the table will stay
777 towards the front of the alist and character lookups in dense
778 keymaps will remain fast. Otherwise, this just points at the
779 front of the keymap. */
780 Lisp_Object insertion_point;
781
782 insertion_point = keymap;
783 for (tail = XCDR (keymap); CONSP (tail); tail = XCDR (tail))
784 {
785 Lisp_Object elt;
786
787 elt = XCAR (tail);
788 if (VECTORP (elt))
789 {
790 if (NATNUMP (idx) && XFASTINT (idx) < ASIZE (elt))
791 {
792 ASET (elt, XFASTINT (idx), def);
793 return def;
794 }
795 insertion_point = tail;
796 }
797 else if (CHAR_TABLE_P (elt))
798 {
799 /* Character codes with modifiers
800 are not included in a char-table.
801 All character codes without modifiers are included. */
802 if (NATNUMP (idx) && !(XFASTINT (idx) & CHAR_MODIFIER_MASK))
803 {
804 Faset (elt, idx,
805 /* `nil' has a special meaning for char-tables, so
806 we use something else to record an explicitly
807 unbound entry. */
808 NILP (def) ? Qt : def);
809 return def;
810 }
811 insertion_point = tail;
812 }
813 else if (CONSP (elt))
814 {
815 if (EQ (idx, XCAR (elt)))
816 {
817 XSETCDR (elt, def);
818 return def;
819 }
820 }
821 else if (EQ (elt, Qkeymap))
822 /* If we find a 'keymap' symbol in the spine of KEYMAP,
823 then we must have found the start of a second keymap
824 being used as the tail of KEYMAP, and a binding for IDX
825 should be inserted before it. */
826 goto keymap_end;
827
828 QUIT;
829 }
830
831 keymap_end:
832 /* We have scanned the entire keymap, and not found a binding for
833 IDX. Let's add one. */
834 XSETCDR (insertion_point,
835 Fcons (Fcons (idx, def), XCDR (insertion_point)));
836 }
837
838 return def;
839}
840
841EXFUN (Fcopy_keymap, 1);
842
843Lisp_Object
844copy_keymap_item (elt)
845 Lisp_Object elt;
846{
847 Lisp_Object res, tem;
848
849 if (!CONSP (elt))
850 return elt;
851
852 res = tem = elt;
853
854 /* Is this a new format menu item. */
855 if (EQ (XCAR (tem), Qmenu_item))
856 {
857 /* Copy cell with menu-item marker. */
858 res = elt = Fcons (XCAR (tem), XCDR (tem));
859 tem = XCDR (elt);
860 if (CONSP (tem))
861 {
862 /* Copy cell with menu-item name. */
863 XSETCDR (elt, Fcons (XCAR (tem), XCDR (tem)));
864 elt = XCDR (elt);
865 tem = XCDR (elt);
866 }
867 if (CONSP (tem))
868 {
869 /* Copy cell with binding and if the binding is a keymap,
870 copy that. */
871 XSETCDR (elt, Fcons (XCAR (tem), XCDR (tem)));
872 elt = XCDR (elt);
873 tem = XCAR (elt);
874 if (CONSP (tem) && EQ (XCAR (tem), Qkeymap))
875 XSETCAR (elt, Fcopy_keymap (tem));
876 tem = XCDR (elt);
877 if (CONSP (tem) && CONSP (XCAR (tem)))
878 /* Delete cache for key equivalences. */
879 XSETCDR (elt, XCDR (tem));
880 }
881 }
882 else
883 {
884 /* It may be an old fomat menu item.
885 Skip the optional menu string. */
886 if (STRINGP (XCAR (tem)))
887 {
888 /* Copy the cell, since copy-alist didn't go this deep. */
889 res = elt = Fcons (XCAR (tem), XCDR (tem));
890 tem = XCDR (elt);
891 /* Also skip the optional menu help string. */
892 if (CONSP (tem) && STRINGP (XCAR (tem)))
893 {
894 XSETCDR (elt, Fcons (XCAR (tem), XCDR (tem)));
895 elt = XCDR (elt);
896 tem = XCDR (elt);
897 }
898 /* There may also be a list that caches key equivalences.
899 Just delete it for the new keymap. */
900 if (CONSP (tem)
901 && CONSP (XCAR (tem))
902 && (NILP (XCAR (XCAR (tem)))
903 || VECTORP (XCAR (XCAR (tem)))))
904 {
905 XSETCDR (elt, XCDR (tem));
906 tem = XCDR (tem);
907 }
908 if (CONSP (tem) && EQ (XCAR (tem), Qkeymap))
909 XSETCDR (elt, Fcopy_keymap (tem));
910 }
911 else if (EQ (XCAR (tem), Qkeymap))
912 res = Fcopy_keymap (elt);
913 }
914 return res;
915}
916
917void
918copy_keymap_1 (chartable, idx, elt)
919 Lisp_Object chartable, idx, elt;
920{
921 Faset (chartable, idx, copy_keymap_item (elt));
922}
923
924DEFUN ("copy-keymap", Fcopy_keymap, Scopy_keymap, 1, 1, 0,
925 doc: /* Return a copy of the keymap KEYMAP.
926The copy starts out with the same definitions of KEYMAP,
927but changing either the copy or KEYMAP does not affect the other.
928Any key definitions that are subkeymaps are recursively copied.
929However, a key definition which is a symbol whose definition is a keymap
930is not copied. */)
931 (keymap)
932 Lisp_Object keymap;
933{
934 register Lisp_Object copy, tail;
935 keymap = get_keymap (keymap, 1, 0);
936 copy = tail = Fcons (Qkeymap, Qnil);
937 keymap = XCDR (keymap); /* Skip the `keymap' symbol. */
938
939 while (CONSP (keymap) && !EQ (XCAR (keymap), Qkeymap))
940 {
941 Lisp_Object elt = XCAR (keymap);
942 if (CHAR_TABLE_P (elt))
943 {
944 Lisp_Object indices[3];
945 elt = Fcopy_sequence (elt);
946 map_char_table (copy_keymap_1, Qnil, elt, elt, 0, indices);
947 }
948 else if (VECTORP (elt))
949 {
950 int i;
951 elt = Fcopy_sequence (elt);
952 for (i = 0; i < ASIZE (elt); i++)
953 ASET (elt, i, copy_keymap_item (AREF (elt, i)));
954 }
955 else if (CONSP (elt))
956 elt = Fcons (XCAR (elt), copy_keymap_item (XCDR (elt)));
957 XSETCDR (tail, Fcons (elt, Qnil));
958 tail = XCDR (tail);
959 keymap = XCDR (keymap);
960 }
961 XSETCDR (tail, keymap);
962 return copy;
963}
964\f
965/* Simple Keymap mutators and accessors. */
966
967/* GC is possible in this function if it autoloads a keymap. */
968
969DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0,
970 doc: /* Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.
971KEYMAP is a keymap.
972
973KEY is a string or a vector of symbols and characters meaning a
974sequence of keystrokes and events. Non-ASCII characters with codes
975above 127 (such as ISO Latin-1) can be included if you use a vector.
976
977DEF is anything that can be a key's definition:
978 nil (means key is undefined in this keymap),
979 a command (a Lisp function suitable for interactive calling)
980 a string (treated as a keyboard macro),
981 a keymap (to define a prefix key),
982 a symbol. When the key is looked up, the symbol will stand for its
983 function definition, which should at that time be one of the above,
984 or another symbol whose function definition is used, etc.
985 a cons (STRING . DEFN), meaning that DEFN is the definition
986 (DEFN should be a valid definition in its own right),
987 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.
988
989If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at
990the front of KEYMAP. */)
991 (keymap, key, def)
992 Lisp_Object keymap;
993 Lisp_Object key;
994 Lisp_Object def;
995{
996 register int idx;
997 register Lisp_Object c;
998 register Lisp_Object cmd;
999 int metized = 0;
1000 int meta_bit;
1001 int length;
1002 struct gcpro gcpro1, gcpro2, gcpro3;
1003
1004 GCPRO3 (keymap, key, def);
1005 keymap = get_keymap (keymap, 1, 1);
1006
1007 if (!VECTORP (key) && !STRINGP (key))
1008 key = wrong_type_argument (Qarrayp, key);
1009
1010 length = XFASTINT (Flength (key));
1011 if (length == 0)
1012 RETURN_UNGCPRO (Qnil);
1013
1014 if (SYMBOLP (def) && !EQ (Vdefine_key_rebound_commands, Qt))
1015 Vdefine_key_rebound_commands = Fcons (def, Vdefine_key_rebound_commands);
1016
1017 meta_bit = VECTORP (key) ? meta_modifier : 0x80;
1018
1019 idx = 0;
1020 while (1)
1021 {
1022 c = Faref (key, make_number (idx));
1023
1024 if (CONSP (c) && lucid_event_type_list_p (c))
1025 c = Fevent_convert_list (c);
1026
1027 if (SYMBOLP (c))
1028 silly_event_symbol_error (c);
1029
1030 if (INTEGERP (c)
1031 && (XINT (c) & meta_bit)
1032 && !metized)
1033 {
1034 c = meta_prefix_char;
1035 metized = 1;
1036 }
1037 else
1038 {
1039 if (INTEGERP (c))
1040 XSETINT (c, XINT (c) & ~meta_bit);
1041
1042 metized = 0;
1043 idx++;
1044 }
1045
1046 if (!INTEGERP (c) && !SYMBOLP (c) && !CONSP (c))
1047 error ("Key sequence contains invalid event");
1048
1049 if (idx == length)
1050 RETURN_UNGCPRO (store_in_keymap (keymap, c, def));
1051
1052 cmd = access_keymap (keymap, c, 0, 1, 1);
1053
1054 /* If this key is undefined, make it a prefix. */
1055 if (NILP (cmd))
1056 cmd = define_as_prefix (keymap, c);
1057
1058 keymap = get_keymap (cmd, 0, 1);
1059 if (!CONSP (keymap))
1060 /* We must use Fkey_description rather than just passing key to
1061 error; key might be a vector, not a string. */
1062 error ("Key sequence %s uses invalid prefix characters",
1063 XSTRING (Fkey_description (key))->data);
1064 }
1065}
1066
1067/* This function may GC (it calls Fkey_binding). */
1068
1069DEFUN ("remap-command", Fremap_command, Sremap_command, 1, 1, 0,
1070 doc: /* Return the remapping for command COMMAND in current keymaps.
1071Returns nil if COMMAND is not remapped. */)
1072 (command)
1073 Lisp_Object command;
1074{
1075 ASET (remap_command_vector, 1, command);
1076 return Fkey_binding (remap_command_vector, Qnil, Qt);
1077}
1078
1079/* Value is number if KEY is too long; nil if valid but has no definition. */
1080/* GC is possible in this function if it autoloads a keymap. */
1081
1082DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 3, 0,
1083 doc: /* In keymap KEYMAP, look up key sequence KEY. Return the definition.
1084nil means undefined. See doc of `define-key' for kinds of definitions.
1085
1086A number as value means KEY is "too long";
1087that is, characters or symbols in it except for the last one
1088fail to be a valid sequence of prefix characters in KEYMAP.
1089The number is how many characters at the front of KEY
1090it takes to reach a non-prefix command.
1091
1092Normally, `lookup-key' ignores bindings for t, which act as default
1093bindings, used when nothing else in the keymap applies; this makes it
1094usable as a general function for probing keymaps. However, if the
1095third optional argument ACCEPT-DEFAULT is non-nil, `lookup-key' will
1096recognize the default bindings, just as `read-key-sequence' does. */)
1097 (keymap, key, accept_default)
1098 Lisp_Object keymap;
1099 Lisp_Object key;
1100 Lisp_Object accept_default;
1101{
1102 register int idx;
1103 register Lisp_Object cmd;
1104 register Lisp_Object c;
1105 int length;
1106 int t_ok = !NILP (accept_default);
1107 struct gcpro gcpro1, gcpro2;
1108
1109 GCPRO2 (keymap, key);
1110 keymap = get_keymap (keymap, 1, 1);
1111
1112 if (!VECTORP (key) && !STRINGP (key))
1113 key = wrong_type_argument (Qarrayp, key);
1114
1115 length = XFASTINT (Flength (key));
1116 if (length == 0)
1117 RETURN_UNGCPRO (keymap);
1118
1119 idx = 0;
1120 while (1)
1121 {
1122 c = Faref (key, make_number (idx++));
1123
1124 if (CONSP (c) && lucid_event_type_list_p (c))
1125 c = Fevent_convert_list (c);
1126
1127 /* Turn the 8th bit of string chars into a meta modifier. */
1128 if (XINT (c) & 0x80 && STRINGP (key))
1129 XSETINT (c, (XINT (c) | meta_modifier) & ~0x80);
1130
1131 /* Allow string since binding for `menu-bar-select-buffer'
1132 includes the buffer name in the key sequence. */
1133 if (!INTEGERP (c) && !SYMBOLP (c) && !CONSP (c) && !STRINGP (c))
1134 error ("Key sequence contains invalid event");
1135
1136 cmd = access_keymap (keymap, c, t_ok, 0, 1);
1137 if (idx == length)
1138 RETURN_UNGCPRO (cmd);
1139
1140 keymap = get_keymap (cmd, 0, 1);
1141 if (!CONSP (keymap))
1142 RETURN_UNGCPRO (make_number (idx));
1143
1144 QUIT;
1145 }
1146}
1147
1148/* Make KEYMAP define event C as a keymap (i.e., as a prefix).
1149 Assume that currently it does not define C at all.
1150 Return the keymap. */
1151
1152static Lisp_Object
1153define_as_prefix (keymap, c)
1154 Lisp_Object keymap, c;
1155{
1156 Lisp_Object cmd;
1157
1158 cmd = Fmake_sparse_keymap (Qnil);
1159 /* If this key is defined as a prefix in an inherited keymap,
1160 make it a prefix in this map, and make its definition
1161 inherit the other prefix definition. */
1162 cmd = nconc2 (cmd, access_keymap (keymap, c, 0, 0, 0));
1163 store_in_keymap (keymap, c, cmd);
1164
1165 return cmd;
1166}
1167
1168/* Append a key to the end of a key sequence. We always make a vector. */
1169
1170Lisp_Object
1171append_key (key_sequence, key)
1172 Lisp_Object key_sequence, key;
1173{
1174 Lisp_Object args[2];
1175
1176 args[0] = key_sequence;
1177
1178 args[1] = Fcons (key, Qnil);
1179 return Fvconcat (2, args);
1180}
1181
1182/* Given a event type C which is a symbol,
1183 signal an error if is a mistake such as RET or M-RET or C-DEL, etc. */
1184
1185static void
1186silly_event_symbol_error (c)
1187 Lisp_Object c;
1188{
1189 Lisp_Object parsed, base, name, assoc;
1190 int modifiers;
1191
1192 parsed = parse_modifiers (c);
1193 modifiers = (int) XUINT (XCAR (XCDR (parsed)));
1194 base = XCAR (parsed);
1195 name = Fsymbol_name (base);
1196 /* This alist includes elements such as ("RET" . "\\r"). */
1197 assoc = Fassoc (name, exclude_keys);
1198
1199 if (! NILP (assoc))
1200 {
1201 char new_mods[sizeof ("\\A-\\C-\\H-\\M-\\S-\\s-")];
1202 char *p = new_mods;
1203 Lisp_Object keystring;
1204 if (modifiers & alt_modifier)
1205 { *p++ = '\\'; *p++ = 'A'; *p++ = '-'; }
1206 if (modifiers & ctrl_modifier)
1207 { *p++ = '\\'; *p++ = 'C'; *p++ = '-'; }
1208 if (modifiers & hyper_modifier)
1209 { *p++ = '\\'; *p++ = 'H'; *p++ = '-'; }
1210 if (modifiers & meta_modifier)
1211 { *p++ = '\\'; *p++ = 'M'; *p++ = '-'; }
1212 if (modifiers & shift_modifier)
1213 { *p++ = '\\'; *p++ = 'S'; *p++ = '-'; }
1214 if (modifiers & super_modifier)
1215 { *p++ = '\\'; *p++ = 's'; *p++ = '-'; }
1216 *p = 0;
1217
1218 c = reorder_modifiers (c);
1219 keystring = concat2 (build_string (new_mods), XCDR (assoc));
1220
1221 error ((modifiers & ~meta_modifier
1222 ? "To bind the key %s, use [?%s], not [%s]"
1223 : "To bind the key %s, use \"%s\", not [%s]"),
1224 XSTRING (SYMBOL_NAME (c))->data, XSTRING (keystring)->data,
1225 XSTRING (SYMBOL_NAME (c))->data);
1226 }
1227}
1228\f
1229/* Global, local, and minor mode keymap stuff. */
1230
1231/* We can't put these variables inside current_minor_maps, since under
1232 some systems, static gets macro-defined to be the empty string.
1233 Ickypoo. */
1234static Lisp_Object *cmm_modes = NULL, *cmm_maps = NULL;
1235static int cmm_size = 0;
1236
1237/* Error handler used in current_minor_maps. */
1238static Lisp_Object
1239current_minor_maps_error ()
1240{
1241 return Qnil;
1242}
1243
1244/* Store a pointer to an array of the keymaps of the currently active
1245 minor modes in *buf, and return the number of maps it contains.
1246
1247 This function always returns a pointer to the same buffer, and may
1248 free or reallocate it, so if you want to keep it for a long time or
1249 hand it out to lisp code, copy it. This procedure will be called
1250 for every key sequence read, so the nice lispy approach (return a
1251 new assoclist, list, what have you) for each invocation would
1252 result in a lot of consing over time.
1253
1254 If we used xrealloc/xmalloc and ran out of memory, they would throw
1255 back to the command loop, which would try to read a key sequence,
1256 which would call this function again, resulting in an infinite
1257 loop. Instead, we'll use realloc/malloc and silently truncate the
1258 list, let the key sequence be read, and hope some other piece of
1259 code signals the error. */
1260int
1261current_minor_maps (modeptr, mapptr)
1262 Lisp_Object **modeptr, **mapptr;
1263{
1264 int i = 0;
1265 int list_number = 0;
1266 Lisp_Object alist, assoc, var, val;
1267 Lisp_Object emulation_alists;
1268 Lisp_Object lists[2];
1269
1270 emulation_alists = Vemulation_mode_map_alists;
1271 lists[0] = Vminor_mode_overriding_map_alist;
1272 lists[1] = Vminor_mode_map_alist;
1273
1274 for (list_number = 0; list_number < 2; list_number++)
1275 {
1276 if (CONSP (emulation_alists))
1277 {
1278 alist = XCAR (emulation_alists);
1279 emulation_alists = XCDR (emulation_alists);
1280 if (SYMBOLP (alist))
1281 alist = find_symbol_value (alist);
1282 list_number = -1;
1283 }
1284 else
1285 alist = lists[list_number];
1286
1287 for ( ; CONSP (alist); alist = XCDR (alist))
1288 if ((assoc = XCAR (alist), CONSP (assoc))
1289 && (var = XCAR (assoc), SYMBOLP (var))
1290 && (val = find_symbol_value (var), !EQ (val, Qunbound))
1291 && !NILP (val))
1292 {
1293 Lisp_Object temp;
1294
1295 /* If a variable has an entry in Vminor_mode_overriding_map_alist,
1296 and also an entry in Vminor_mode_map_alist,
1297 ignore the latter. */
1298 if (list_number == 1)
1299 {
1300 val = assq_no_quit (var, lists[0]);
1301 if (!NILP (val))
1302 continue;
1303 }
1304
1305 if (i >= cmm_size)
1306 {
1307 int newsize, allocsize;
1308 Lisp_Object *newmodes, *newmaps;
1309
1310 newsize = cmm_size == 0 ? 30 : cmm_size * 2;
1311 allocsize = newsize * sizeof *newmodes;
1312
1313 /* Use malloc here. See the comment above this function.
1314 Avoid realloc here; it causes spurious traps on GNU/Linux [KFS] */
1315 BLOCK_INPUT;
1316 newmodes = (Lisp_Object *) malloc (allocsize);
1317 if (newmodes)
1318 {
1319 if (cmm_modes)
1320 {
1321 bcopy (cmm_modes, newmodes, cmm_size * sizeof cmm_modes[0]);
1322 free (cmm_modes);
1323 }
1324 cmm_modes = newmodes;
1325 }
1326
1327 newmaps = (Lisp_Object *) malloc (allocsize);
1328 if (newmaps)
1329 {
1330 if (cmm_maps)
1331 {
1332 bcopy (cmm_maps, newmaps, cmm_size * sizeof cmm_maps[0]);
1333 free (cmm_maps);
1334 }
1335 cmm_maps = newmaps;
1336 }
1337 UNBLOCK_INPUT;
1338
1339 if (newmodes == NULL || newmaps == NULL)
1340 break;
1341 cmm_size = newsize;
1342 }
1343
1344 /* Get the keymap definition--or nil if it is not defined. */
1345 temp = internal_condition_case_1 (Findirect_function,
1346 XCDR (assoc),
1347 Qerror, current_minor_maps_error);
1348 if (!NILP (temp))
1349 {
1350 cmm_modes[i] = var;
1351 cmm_maps [i] = temp;
1352 i++;
1353 }
1354 }
1355 }
1356
1357 if (modeptr) *modeptr = cmm_modes;
1358 if (mapptr) *mapptr = cmm_maps;
1359 return i;
1360}
1361
1362DEFUN ("current-active-maps", Fcurrent_active_maps, Scurrent_active_maps,
1363 0, 1, 0,
1364 doc: /* Return a list of the currently active keymaps.
1365OLP 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
1405DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 3, 0,
1406 doc: /* Return the binding for command KEY in current keymaps.
1407KEY is a string or vector, a sequence of keystrokes.
1408The binding is probably a symbol with a function definition.
1409
1410Normally, `key-binding' ignores bindings for t, which act as default
1411bindings, used when nothing else in the keymap applies; this makes it
1412usable as a general function for probing keymaps. However, if the
1413optional second argument ACCEPT-DEFAULT is non-nil, `key-binding' does
1414recognize the default bindings, just as `read-key-sequence' does.
1415
1416Like the normal command loop, `key-binding' will remap the command
1417resulting from looking up KEY by looking up the command in the
1418currrent keymaps. However, if the optional third argument NO-REMAP
1419is 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
1497DEFUN ("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.
1499KEYS is a string, a sequence of keystrokes.
1500The binding is probably a symbol with a function definition.
1501
1502If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1503bindings; 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
1516DEFUN ("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.
1518KEYS is a string, a sequence of keystrokes.
1519The binding is probably a symbol with a function definition.
1520This function's return values are the same as those of lookup-key
1521\(which see).
1522
1523If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1524bindings; 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
1533DEFUN ("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.
1535Return an alist of pairs (MODENAME . BINDING), where MODENAME is the
1536the symbol which names the minor mode binding KEY, and BINDING is
1537KEY's definition in that mode. In particular, if KEY has no
1538minor-mode bindings, return nil. If the first binding is a
1539non-prefix, all subsequent bindings will be omitted, since they would
1540be ignored. Similarly, the list doesn't include non-prefix bindings
1541that come after prefix bindings.
1542
1543If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1544bindings; 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
1576DEFUN ("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.
1578A new sparse keymap is stored as COMMAND's function definition and its value.
1579If a second optional argument MAPVAR is given, the map is stored as
1580its value instead of as COMMAND's value; but COMMAND is still defined
1581as a function.
1582The third optional argument NAME, if given, supplies a menu name
1583string 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
1597DEFUN ("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
1608DEFUN ("use-local-map", Fuse_local_map, Suse_local_map, 1, 1, 0,
1609 doc: /* Select KEYMAP as the local keymap.
1610If 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
1622DEFUN ("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
1629DEFUN ("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
1636DEFUN ("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
1649static void
1650accessible_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
1696static void
1697accessible_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
1709DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps,
1710 1, 2, 0,
1711 doc: /* Find all keymaps accessible via prefix characters from KEYMAP.
1712Returns a list of elements of the form (KEYS . MAP), where the sequence
1713KEYS starting from KEYMAP gets you to MAP. These elements are ordered
1714so that the KEYS increase in length. The first element is ([] . KEYMAP).
1715An optional argument PREFIX, if non-nil, should be a key sequence;
1716then 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
1857Lisp_Object Qsingle_key_description, Qkey_description;
1858
1859/* This function cannot GC. */
1860
1861DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0,
1862 doc: /* Return a pretty description of key-sequence KEYS.
1863Control characters turn into "C-foo" sequences, meta into "M-foo"
1864spaces 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
1933char *
1934push_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
2062DEFUN ("single-key-description", Fsingle_key_description,
2063 Ssingle_key_description, 1, 2, 0,
2064 doc: /* Return a pretty description of command character KEY.
2065Control characters turn into C-whatever, etc.
2066Optional argument NO-ANGLES non-nil means don't put angle brackets
2067around 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 (XSTRING (SYMBOL_NAME (key))) + 5);
2122 sprintf (buffer, "<%s>", XSTRING (SYMBOL_NAME (key))->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
2135char *
2136push_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
2163DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0,
2164 doc: /* Return a pretty description of file-character CHARACTER.
2165Control 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. */
2190static int
2191ascii_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
2215static Lisp_Object where_is_internal ();
2216static Lisp_Object where_is_internal_1 ();
2217static 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
2222static Lisp_Object
2223shadow_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
2239static Lisp_Object
2240where_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
2445DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 5, 0,
2446 doc: /* Return list of keys that invoke DEFINITION.
2447If KEYMAP is non-nil, search only KEYMAP and the global keymap.
2448If KEYMAP is nil, search all the currently active keymaps.
2449If KEYMAP is a list of keymaps, search only those keymaps.
2450
2451If optional 3rd arg FIRSTONLY is non-nil, return the first key sequence found,
2452rather than a list of all possible key sequences.
2453If FIRSTONLY is the symbol `non-ascii', return the first binding found,
2454no matter what it is.
2455If FIRSTONLY has another non-nil value, prefer sequences of ASCII characters,
2456and entirely reject menu bindings.
2457
2458If optional 4th arg NOINDIRECT is non-nil, don't follow indirections
2459to other keymaps or slots. This makes it possible to search for an
2460indirect definition itself.
2461
2462If optional 5th arg NO-REMAP is non-nil, don't search for key sequences
2463that invoke a command which is remapped to DEFINITION, but include the
2464remapped 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
2557static void
2558where_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
2587static Lisp_Object
2588where_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
2629DEFUN ("describe-buffer-bindings", Fdescribe_buffer_bindings, Sdescribe_buffer_bindings, 1, 3, 0,
2630 doc: /* Insert the list of all defined keys and their definitions.
2631The list is inserted in the current buffer, while the bindings are
2632looked up in BUFFER.
2633The optional argument PREFIX, if non-nil, should be a key sequence;
2634then we display only bindings that start with that prefix.
2635The 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 = "\
2647Keyboard translations:\n\n\
2648You 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 + XSTRING (SYMBOL_NAME (modes[i]))->size);
2740 *p++ = '\f';
2741 *p++ = '\n';
2742 *p++ = '`';
2743 bcopy (XSTRING (SYMBOL_NAME (modes[i]))->data, p,
2744 XSTRING (SYMBOL_NAME (modes[i]))->size);
2745 p += XSTRING (SYMBOL_NAME (modes[i]))->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
2800void
2801describe_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 = "\
2815key 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
2921static int previous_description_column;
2922
2923static void
2924describe_command (definition, args)
2925 Lisp_Object definition, args;
2926{
2927 register Lisp_Object tem1;
2928 int column = (int) current_column (); /* iftc */
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 tem1 = SYMBOL_NAME (definition);
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
2960static void
2961describe_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 tem1 = SYMBOL_NAME (definition);
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
2989static void
2990describe_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
3107static void
3108describe_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
3116DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 2, 0,
3117 doc: /* Insert a description of contents of VECTOR.
3118This 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
3165void
3166describe_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. */
3468Lisp_Object apropos_predicate;
3469Lisp_Object apropos_accumulate;
3470
3471static void
3472apropos_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
3484DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0,
3485 doc: /* Show all symbols whose names contain match for REGEXP.
3486If optional 2nd arg PREDICATE is non-nil, (funcall PREDICATE SYMBOL) is done
3487for each symbol and a symbol is mentioned only if that returns non-nil.
3488Return 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
3503void
3504syms_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.
3543This is used for internal purposes during Emacs startup;
3544don'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.
3569Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read
3570key sequences and look up bindings iff VARIABLE's value is non-nil.
3571If two active keymaps bind the same key, the keymap appearing earlier
3572in 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.
3577This variable is an alist just like `minor-mode-map-alist', and it is
3578used the same way (and before `minor-mode-map-alist'); however,
3579it is provided for major modes to bind locally. */);
3580 Vminor_mode_overriding_map_alist = Qnil;
3581
3582 DEFVAR_LISP ("emulation-mode-map-alists", &Vemulation_mode_map_alists,
3583 doc: /* List of keymap alists to use for emulations modes.
3584It is intended for modes or packages using multiple minor-mode keymaps.
3585Each element is a keymap alist just like `minor-mode-map-alist', or a
3586symbol with a variable binding which is a keymap alist, and it is used
3587the same way. The "active" keymaps in each alist are used before
3588`minor-mode-map-alist' and `minor-mode-overriding-map-alist'. */);
3589 Vemulation_mode_map_alists = Qnil;
3590
3591
3592 DEFVAR_LISP ("function-key-map", &Vfunction_key_map,
3593 doc: /* Keymap mapping ASCII function key sequences onto their preferred forms.
3594This allows Emacs to recognize function keys sent from ASCII
3595terminals at any point in a key sequence.
3596
3597The `read-key-sequence' function replaces any subsequence bound by
3598`function-key-map' with its binding. More precisely, when the active
3599keymaps have no binding for the current key sequence but
3600`function-key-map' binds a suffix of the sequence to a vector or string,
3601`read-key-sequence' replaces the matching suffix with its binding, and
3602continues with the new sequence.
3603
3604The events that come from bindings in `function-key-map' are not
3605themselves looked up in `function-key-map'.
3606
3607For example, suppose `function-key-map' binds `ESC O P' to [f1].
3608Typing `ESC O P' to `read-key-sequence' would return [f1]. Typing
3609`C-x ESC O P' would return [?\\C-x f1]. If [f1] were a prefix
3610key, typing `ESC O P x' would return [f1 x]. */);
3611 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
3612
3613 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
3614 doc: /* Keymap of key translations that can override keymaps.
3615This keymap works like `function-key-map', but comes after that,
3616and applies even for keys that have ordinary bindings. */);
3617 Vkey_translation_map = Qnil;
3618
3619 Qsingle_key_description = intern ("single-key-description");
3620 staticpro (&Qsingle_key_description);
3621
3622 Qkey_description = intern ("key-description");
3623 staticpro (&Qkey_description);
3624
3625 Qkeymapp = intern ("keymapp");
3626 staticpro (&Qkeymapp);
3627
3628 Qnon_ascii = intern ("non-ascii");
3629 staticpro (&Qnon_ascii);
3630
3631 Qmenu_item = intern ("menu-item");
3632 staticpro (&Qmenu_item);
3633
3634 Qremap = intern ("remap");
3635 staticpro (&Qremap);
3636
3637 remap_command_vector = Fmake_vector (make_number (2), Qremap);
3638 staticpro (&remap_command_vector);
3639
3640 where_is_cache_keymaps = Qt;
3641 where_is_cache = Qnil;
3642 staticpro (&where_is_cache);
3643 staticpro (&where_is_cache_keymaps);
3644
3645 defsubr (&Skeymapp);
3646 defsubr (&Skeymap_parent);
3647 defsubr (&Skeymap_prompt);
3648 defsubr (&Sset_keymap_parent);
3649 defsubr (&Smake_keymap);
3650 defsubr (&Smake_sparse_keymap);
3651 defsubr (&Scopy_keymap);
3652 defsubr (&Sremap_command);
3653 defsubr (&Skey_binding);
3654 defsubr (&Slocal_key_binding);
3655 defsubr (&Sglobal_key_binding);
3656 defsubr (&Sminor_mode_key_binding);
3657 defsubr (&Sdefine_key);
3658 defsubr (&Slookup_key);
3659 defsubr (&Sdefine_prefix_command);
3660 defsubr (&Suse_global_map);
3661 defsubr (&Suse_local_map);
3662 defsubr (&Scurrent_local_map);
3663 defsubr (&Scurrent_global_map);
3664 defsubr (&Scurrent_minor_mode_maps);
3665 defsubr (&Scurrent_active_maps);
3666 defsubr (&Saccessible_keymaps);
3667 defsubr (&Skey_description);
3668 defsubr (&Sdescribe_vector);
3669 defsubr (&Ssingle_key_description);
3670 defsubr (&Stext_char_description);
3671 defsubr (&Swhere_is_internal);
3672 defsubr (&Sdescribe_buffer_bindings);
3673 defsubr (&Sapropos_internal);
3674}
3675
3676void
3677keys_of_keymap ()
3678{
3679 initial_define_key (global_map, 033, "ESC-prefix");
3680 initial_define_key (global_map, Ctl('X'), "Control-X-prefix");
3681}