2000-07-05 Michael Kifer <kifer@cs.sunysb.edu>
[emacs.git] / src / keymap.c
blob414de7b14a3c4ac89e0bcc305e572245db571ff1
1 /* Manipulation of keymaps
2 Copyright (C) 1985, 86,87,88,93,94,95,98,99 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include <stdio.h>
24 #undef NULL
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"
35 #define min(a, b) ((a) < (b) ? (a) : (b))
37 /* The number of elements in keymap vectors. */
38 #define DENSE_TABLE_SIZE (0200)
40 /* Actually allocate storage for these variables */
42 Lisp_Object current_global_map; /* Current global keymap */
44 Lisp_Object global_map; /* default global key bindings */
46 Lisp_Object meta_map; /* The keymap used for globally bound
47 ESC-prefixed default commands */
49 Lisp_Object control_x_map; /* The keymap used for globally bound
50 C-x-prefixed default commands */
52 /* was MinibufLocalMap */
53 Lisp_Object Vminibuffer_local_map;
54 /* The keymap used by the minibuf for local
55 bindings when spaces are allowed in the
56 minibuf */
58 /* was MinibufLocalNSMap */
59 Lisp_Object Vminibuffer_local_ns_map;
60 /* The keymap used by the minibuf for local
61 bindings when spaces are not encouraged
62 in the minibuf */
64 /* keymap used for minibuffers when doing completion */
65 /* was MinibufLocalCompletionMap */
66 Lisp_Object Vminibuffer_local_completion_map;
68 /* keymap used for minibuffers when doing completion and require a match */
69 /* was MinibufLocalMustMatchMap */
70 Lisp_Object Vminibuffer_local_must_match_map;
72 /* Alist of minor mode variables and keymaps. */
73 Lisp_Object Vminor_mode_map_alist;
75 /* Alist of major-mode-specific overrides for
76 minor mode variables and keymaps. */
77 Lisp_Object Vminor_mode_overriding_map_alist;
79 /* Keymap mapping ASCII function key sequences onto their preferred forms.
80 Initialized by the terminal-specific lisp files. See DEFVAR for more
81 documentation. */
82 Lisp_Object Vfunction_key_map;
84 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
85 Lisp_Object Vkey_translation_map;
87 /* A list of all commands given new bindings since a certain time
88 when nil was stored here.
89 This is used to speed up recomputation of menu key equivalents
90 when Emacs starts up. t means don't record anything here. */
91 Lisp_Object Vdefine_key_rebound_commands;
93 Lisp_Object Qkeymapp, Qkeymap, Qnon_ascii, Qmenu_item;
95 /* A char with the CHAR_META bit set in a vector or the 0200 bit set
96 in a string key sequence is equivalent to prefixing with this
97 character. */
98 extern Lisp_Object meta_prefix_char;
100 extern Lisp_Object Voverriding_local_map;
102 static Lisp_Object define_as_prefix ();
103 static Lisp_Object describe_buffer_bindings ();
104 static void describe_command (), describe_translation ();
105 static void describe_map ();
107 /* Keymap object support - constructors and predicates. */
109 DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 1, 0,
110 "Construct and return a new keymap, of the form (keymap CHARTABLE . ALIST).\n\
111 CHARTABLE is a char-table that holds the bindings for the ASCII\n\
112 characters. ALIST is an assoc-list which holds bindings for function keys,\n\
113 mouse events, and any other things that appear in the input stream.\n\
114 All entries in it are initially nil, meaning \"command undefined\".\n\n\
115 The optional arg STRING supplies a menu name for the keymap\n\
116 in case you use it as a menu with `x-popup-menu'.")
117 (string)
118 Lisp_Object string;
120 Lisp_Object tail;
121 if (!NILP (string))
122 tail = Fcons (string, Qnil);
123 else
124 tail = Qnil;
125 return Fcons (Qkeymap,
126 Fcons (Fmake_char_table (Qkeymap, Qnil), tail));
129 DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 1, 0,
130 "Construct and return a new sparse-keymap list.\n\
131 Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),\n\
132 which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),\n\
133 which binds the function key or mouse event SYMBOL to DEFINITION.\n\
134 Initially the alist is nil.\n\n\
135 The optional arg STRING supplies a menu name for the keymap\n\
136 in case you use it as a menu with `x-popup-menu'.")
137 (string)
138 Lisp_Object string;
140 if (!NILP (string))
141 return Fcons (Qkeymap, Fcons (string, Qnil));
142 return Fcons (Qkeymap, Qnil);
145 /* This function is used for installing the standard key bindings
146 at initialization time.
148 For example:
150 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); */
152 void
153 initial_define_key (keymap, key, defname)
154 Lisp_Object keymap;
155 int key;
156 char *defname;
158 store_in_keymap (keymap, make_number (key), intern (defname));
161 void
162 initial_define_lispy_key (keymap, keyname, defname)
163 Lisp_Object keymap;
164 char *keyname;
165 char *defname;
167 store_in_keymap (keymap, intern (keyname), intern (defname));
170 /* Define character fromchar in map frommap as an alias for character
171 tochar in map tomap. Subsequent redefinitions of the latter WILL
172 affect the former. */
174 #if 0
175 void
176 synkey (frommap, fromchar, tomap, tochar)
177 struct Lisp_Vector *frommap, *tomap;
178 int fromchar, tochar;
180 Lisp_Object v, c;
181 XSETVECTOR (v, tomap);
182 XSETFASTINT (c, tochar);
183 frommap->contents[fromchar] = Fcons (v, c);
185 #endif /* 0 */
187 DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0,
188 "Return t if OBJECT is a keymap.\n\
190 A keymap is a list (keymap . ALIST),\n\
191 or a symbol whose function definition is itself a keymap.\n\
192 ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);\n\
193 a vector of densely packed bindings for small character codes\n\
194 is also allowed as an element.")
195 (object)
196 Lisp_Object object;
198 return (NILP (get_keymap_1 (object, 0, 0)) ? Qnil : Qt);
201 /* Check that OBJECT is a keymap (after dereferencing through any
202 symbols). If it is, return it.
204 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
205 is an autoload form, do the autoload and try again.
206 If AUTOLOAD is nonzero, callers must assume GC is possible.
208 ERROR controls how we respond if OBJECT isn't a keymap.
209 If ERROR is non-zero, signal an error; otherwise, just return Qnil.
211 Note that most of the time, we don't want to pursue autoloads.
212 Functions like Faccessible_keymaps which scan entire keymap trees
213 shouldn't load every autoloaded keymap. I'm not sure about this,
214 but it seems to me that only read_key_sequence, Flookup_key, and
215 Fdefine_key should cause keymaps to be autoloaded. */
217 Lisp_Object
218 get_keymap_1 (object, error, autoload)
219 Lisp_Object object;
220 int error, autoload;
222 Lisp_Object tem;
224 autoload_retry:
225 if (NILP (object))
226 goto end;
227 if (CONSP (object) && EQ (XCAR (object), Qkeymap))
228 return object;
229 else
231 tem = indirect_function (object);
232 if (CONSP (tem) && EQ (XCAR (tem), Qkeymap))
233 return tem;
236 /* Should we do an autoload? Autoload forms for keymaps have
237 Qkeymap as their fifth element. */
238 if (autoload
239 && SYMBOLP (object)
240 && CONSP (tem)
241 && EQ (XCAR (tem), Qautoload))
243 Lisp_Object tail;
245 tail = Fnth (make_number (4), tem);
246 if (EQ (tail, Qkeymap))
248 struct gcpro gcpro1, gcpro2;
250 GCPRO2 (tem, object);
251 do_autoload (tem, object);
252 UNGCPRO;
254 goto autoload_retry;
258 end:
259 if (error)
260 wrong_type_argument (Qkeymapp, object);
261 else
262 return Qnil;
266 /* Follow any symbol chaining, and return the keymap denoted by OBJECT.
267 If OBJECT doesn't denote a keymap at all, signal an error. */
268 Lisp_Object
269 get_keymap (object)
270 Lisp_Object object;
272 return get_keymap_1 (object, 1, 0);
275 /* Return the parent map of the keymap MAP, or nil if it has none.
276 We assume that MAP is a valid keymap. */
278 DEFUN ("keymap-parent", Fkeymap_parent, Skeymap_parent, 1, 1, 0,
279 "Return the parent keymap of KEYMAP.")
280 (keymap)
281 Lisp_Object keymap;
283 Lisp_Object list;
285 keymap = get_keymap_1 (keymap, 1, 1);
287 /* Skip past the initial element `keymap'. */
288 list = XCDR (keymap);
289 for (; CONSP (list); list = XCDR (list))
291 /* See if there is another `keymap'. */
292 if (EQ (Qkeymap, XCAR (list)))
293 return list;
296 return Qnil;
299 /* Set the parent keymap of MAP to PARENT. */
301 DEFUN ("set-keymap-parent", Fset_keymap_parent, Sset_keymap_parent, 2, 2, 0,
302 "Modify KEYMAP to set its parent map to PARENT.\n\
303 PARENT should be nil or another keymap.")
304 (keymap, parent)
305 Lisp_Object keymap, parent;
307 Lisp_Object list, prev;
308 int i;
310 keymap = get_keymap_1 (keymap, 1, 1);
311 if (!NILP (parent))
312 parent = get_keymap_1 (parent, 1, 1);
314 /* Skip past the initial element `keymap'. */
315 prev = keymap;
316 while (1)
318 list = XCDR (prev);
319 /* If there is a parent keymap here, replace it.
320 If we came to the end, add the parent in PREV. */
321 if (! CONSP (list) || EQ (Qkeymap, XCAR (list)))
323 /* If we already have the right parent, return now
324 so that we avoid the loops below. */
325 if (EQ (XCDR (prev), parent))
326 return parent;
328 XCDR (prev) = parent;
329 break;
331 prev = list;
334 /* Scan through for submaps, and set their parents too. */
336 for (list = XCDR (keymap); CONSP (list); list = XCDR (list))
338 /* Stop the scan when we come to the parent. */
339 if (EQ (XCAR (list), Qkeymap))
340 break;
342 /* If this element holds a prefix map, deal with it. */
343 if (CONSP (XCAR (list))
344 && CONSP (XCDR (XCAR (list))))
345 fix_submap_inheritance (keymap, XCAR (XCAR (list)),
346 XCDR (XCAR (list)));
348 if (VECTORP (XCAR (list)))
349 for (i = 0; i < XVECTOR (XCAR (list))->size; i++)
350 if (CONSP (XVECTOR (XCAR (list))->contents[i]))
351 fix_submap_inheritance (keymap, make_number (i),
352 XVECTOR (XCAR (list))->contents[i]);
354 if (CHAR_TABLE_P (XCAR (list)))
356 Lisp_Object indices[3];
358 map_char_table (fix_submap_inheritance, Qnil, XCAR (list),
359 keymap, 0, indices);
363 return parent;
366 /* EVENT is defined in MAP as a prefix, and SUBMAP is its definition.
367 if EVENT is also a prefix in MAP's parent,
368 make sure that SUBMAP inherits that definition as its own parent. */
370 void
371 fix_submap_inheritance (map, event, submap)
372 Lisp_Object map, event, submap;
374 Lisp_Object map_parent, parent_entry;
376 /* SUBMAP is a cons that we found as a key binding.
377 Discard the other things found in a menu key binding. */
379 if (CONSP (submap))
381 /* May be an old format menu item */
382 if (STRINGP (XCAR (submap)))
384 submap = XCDR (submap);
385 /* Also remove a menu help string, if any,
386 following the menu item name. */
387 if (CONSP (submap) && STRINGP (XCAR (submap)))
388 submap = XCDR (submap);
389 /* Also remove the sublist that caches key equivalences, if any. */
390 if (CONSP (submap)
391 && CONSP (XCAR (submap)))
393 Lisp_Object carcar;
394 carcar = XCAR (XCAR (submap));
395 if (NILP (carcar) || VECTORP (carcar))
396 submap = XCDR (submap);
400 /* Or a new format menu item */
401 else if (EQ (XCAR (submap), Qmenu_item)
402 && CONSP (XCDR (submap)))
404 submap = XCDR (XCDR (submap));
405 if (CONSP (submap))
406 submap = XCAR (submap);
410 /* If it isn't a keymap now, there's no work to do. */
411 if (! CONSP (submap)
412 || ! EQ (XCAR (submap), Qkeymap))
413 return;
415 map_parent = Fkeymap_parent (map);
416 if (! NILP (map_parent))
417 parent_entry = access_keymap (map_parent, event, 0, 0);
418 else
419 parent_entry = Qnil;
421 /* If MAP's parent has something other than a keymap,
422 our own submap shadows it completely, so use nil as SUBMAP's parent. */
423 if (! (CONSP (parent_entry) && EQ (XCAR (parent_entry), Qkeymap)))
424 parent_entry = Qnil;
426 if (! EQ (parent_entry, submap))
428 Lisp_Object submap_parent;
429 submap_parent = submap;
430 while (1)
432 Lisp_Object tem;
433 tem = Fkeymap_parent (submap_parent);
434 if (EQ (tem, parent_entry))
435 return;
436 if (CONSP (tem)
437 && EQ (XCAR (tem), Qkeymap))
438 submap_parent = tem;
439 else
440 break;
442 Fset_keymap_parent (submap_parent, parent_entry);
446 /* Look up IDX in MAP. IDX may be any sort of event.
447 Note that this does only one level of lookup; IDX must be a single
448 event, not a sequence.
450 If T_OK is non-zero, bindings for Qt are treated as default
451 bindings; any key left unmentioned by other tables and bindings is
452 given the binding of Qt.
454 If T_OK is zero, bindings for Qt are not treated specially.
456 If NOINHERIT, don't accept a subkeymap found in an inherited keymap. */
458 Lisp_Object
459 access_keymap (map, idx, t_ok, noinherit)
460 Lisp_Object map;
461 Lisp_Object idx;
462 int t_ok;
463 int noinherit;
465 int noprefix = 0;
466 Lisp_Object val;
468 /* If idx is a list (some sort of mouse click, perhaps?),
469 the index we want to use is the car of the list, which
470 ought to be a symbol. */
471 idx = EVENT_HEAD (idx);
473 /* If idx is a symbol, it might have modifiers, which need to
474 be put in the canonical order. */
475 if (SYMBOLP (idx))
476 idx = reorder_modifiers (idx);
477 else if (INTEGERP (idx))
478 /* Clobber the high bits that can be present on a machine
479 with more than 24 bits of integer. */
480 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
483 Lisp_Object tail;
484 Lisp_Object t_binding;
486 t_binding = Qnil;
487 for (tail = map; CONSP (tail); tail = XCDR (tail))
489 Lisp_Object binding;
491 binding = XCAR (tail);
492 if (SYMBOLP (binding))
494 /* If NOINHERIT, stop finding prefix definitions
495 after we pass a second occurrence of the `keymap' symbol. */
496 if (noinherit && EQ (binding, Qkeymap) && ! EQ (tail, map))
497 noprefix = 1;
499 else if (CONSP (binding))
501 if (EQ (XCAR (binding), idx))
503 val = XCDR (binding);
504 if (noprefix && CONSP (val) && EQ (XCAR (val), Qkeymap))
505 return Qnil;
506 if (CONSP (val))
507 fix_submap_inheritance (map, idx, val);
508 return val;
510 if (t_ok && EQ (XCAR (binding), Qt))
511 t_binding = XCDR (binding);
513 else if (VECTORP (binding))
515 if (NATNUMP (idx) && XFASTINT (idx) < XVECTOR (binding)->size)
517 val = XVECTOR (binding)->contents[XFASTINT (idx)];
518 if (noprefix && CONSP (val) && EQ (XCAR (val), Qkeymap))
519 return Qnil;
520 if (CONSP (val))
521 fix_submap_inheritance (map, idx, val);
522 return val;
525 else if (CHAR_TABLE_P (binding))
527 /* Character codes with modifiers
528 are not included in a char-table.
529 All character codes without modifiers are included. */
530 if (NATNUMP (idx)
531 && ! (XFASTINT (idx)
532 & (CHAR_ALT | CHAR_SUPER | CHAR_HYPER
533 | CHAR_SHIFT | CHAR_CTL | CHAR_META)))
535 val = Faref (binding, idx);
536 if (noprefix && CONSP (val) && EQ (XCAR (val), Qkeymap))
537 return Qnil;
538 if (CONSP (val))
539 fix_submap_inheritance (map, idx, val);
540 return val;
544 QUIT;
547 return t_binding;
551 /* Given OBJECT which was found in a slot in a keymap,
552 trace indirect definitions to get the actual definition of that slot.
553 An indirect definition is a list of the form
554 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
555 and INDEX is the object to look up in KEYMAP to yield the definition.
557 Also if OBJECT has a menu string as the first element,
558 remove that. Also remove a menu help string as second element.
560 If AUTOLOAD is nonzero, load autoloadable keymaps
561 that are referred to with indirection. */
563 Lisp_Object
564 get_keyelt (object, autoload)
565 register Lisp_Object object;
566 int autoload;
568 while (1)
570 if (!(CONSP (object)))
571 /* This is really the value. */
572 return object;
574 /* If the keymap contents looks like (keymap ...) or (lambda ...)
575 then use itself. */
576 else if (EQ (XCAR (object), Qkeymap) || EQ (XCAR (object), Qlambda))
577 return object;
579 /* If the keymap contents looks like (menu-item name . DEFN)
580 or (menu-item name DEFN ...) then use DEFN.
581 This is a new format menu item. */
582 else if (EQ (XCAR (object), Qmenu_item))
584 if (CONSP (XCDR (object)))
586 Lisp_Object tem;
588 object = XCDR (XCDR (object));
589 tem = object;
590 if (CONSP (object))
591 object = XCAR (object);
593 /* If there's a `:filter FILTER', apply FILTER to the
594 menu-item's definition to get the real definition to
595 use. Temporarily inhibit GC while evaluating FILTER,
596 because not functions calling get_keyelt are prepared
597 for a GC. */
598 for (; CONSP (tem) && CONSP (XCDR (tem)); tem = XCDR (tem))
599 if (EQ (XCAR (tem), QCfilter))
601 int count = inhibit_garbage_collection ();
602 Lisp_Object filter;
603 filter = XCAR (XCDR (tem));
604 filter = list2 (filter, list2 (Qquote, object));
605 object = menu_item_eval_property (filter);
606 unbind_to (count, Qnil);
607 break;
610 else
611 /* Invalid keymap */
612 return object;
615 /* If the keymap contents looks like (STRING . DEFN), use DEFN.
616 Keymap alist elements like (CHAR MENUSTRING . DEFN)
617 will be used by HierarKey menus. */
618 else if (STRINGP (XCAR (object)))
620 object = XCDR (object);
621 /* Also remove a menu help string, if any,
622 following the menu item name. */
623 if (CONSP (object) && STRINGP (XCAR (object)))
624 object = XCDR (object);
625 /* Also remove the sublist that caches key equivalences, if any. */
626 if (CONSP (object) && CONSP (XCAR (object)))
628 Lisp_Object carcar;
629 carcar = XCAR (XCAR (object));
630 if (NILP (carcar) || VECTORP (carcar))
631 object = XCDR (object);
635 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
636 else
638 register Lisp_Object map;
639 map = get_keymap_1 (Fcar_safe (object), 0, autoload);
640 if (NILP (map))
641 /* Invalid keymap */
642 return object;
643 else
645 Lisp_Object key;
646 key = Fcdr (object);
647 if (INTEGERP (key) && (XUINT (key) & meta_modifier))
649 object = access_keymap (map, meta_prefix_char, 0, 0);
650 map = get_keymap_1 (object, 0, autoload);
651 object = access_keymap (map, make_number (XINT (key)
652 & ~meta_modifier),
653 0, 0);
655 else
656 object = access_keymap (map, key, 0, 0);
662 Lisp_Object
663 store_in_keymap (keymap, idx, def)
664 Lisp_Object keymap;
665 register Lisp_Object idx;
666 register Lisp_Object def;
668 /* If we are preparing to dump, and DEF is a menu element
669 with a menu item indicator, copy it to ensure it is not pure. */
670 if (CONSP (def) && PURE_P (def)
671 && (EQ (XCAR (def), Qmenu_item) || STRINGP (XCAR (def))))
672 def = Fcons (XCAR (def), XCDR (def));
674 if (!CONSP (keymap) || ! EQ (XCAR (keymap), Qkeymap))
675 error ("attempt to define a key in a non-keymap");
677 /* If idx is a list (some sort of mouse click, perhaps?),
678 the index we want to use is the car of the list, which
679 ought to be a symbol. */
680 idx = EVENT_HEAD (idx);
682 /* If idx is a symbol, it might have modifiers, which need to
683 be put in the canonical order. */
684 if (SYMBOLP (idx))
685 idx = reorder_modifiers (idx);
686 else if (INTEGERP (idx))
687 /* Clobber the high bits that can be present on a machine
688 with more than 24 bits of integer. */
689 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
691 /* Scan the keymap for a binding of idx. */
693 Lisp_Object tail;
695 /* The cons after which we should insert new bindings. If the
696 keymap has a table element, we record its position here, so new
697 bindings will go after it; this way, the table will stay
698 towards the front of the alist and character lookups in dense
699 keymaps will remain fast. Otherwise, this just points at the
700 front of the keymap. */
701 Lisp_Object insertion_point;
703 insertion_point = keymap;
704 for (tail = XCDR (keymap); CONSP (tail); tail = XCDR (tail))
706 Lisp_Object elt;
708 elt = XCAR (tail);
709 if (VECTORP (elt))
711 if (NATNUMP (idx) && XFASTINT (idx) < XVECTOR (elt)->size)
713 XVECTOR (elt)->contents[XFASTINT (idx)] = def;
714 return def;
716 insertion_point = tail;
718 else if (CHAR_TABLE_P (elt))
720 /* Character codes with modifiers
721 are not included in a char-table.
722 All character codes without modifiers are included. */
723 if (NATNUMP (idx)
724 && ! (XFASTINT (idx)
725 & (CHAR_ALT | CHAR_SUPER | CHAR_HYPER
726 | CHAR_SHIFT | CHAR_CTL | CHAR_META)))
728 Faset (elt, idx, def);
729 return def;
731 insertion_point = tail;
733 else if (CONSP (elt))
735 if (EQ (idx, XCAR (elt)))
737 XCDR (elt) = def;
738 return def;
741 else if (SYMBOLP (elt))
743 /* If we find a 'keymap' symbol in the spine of KEYMAP,
744 then we must have found the start of a second keymap
745 being used as the tail of KEYMAP, and a binding for IDX
746 should be inserted before it. */
747 if (EQ (elt, Qkeymap))
748 goto keymap_end;
751 QUIT;
754 keymap_end:
755 /* We have scanned the entire keymap, and not found a binding for
756 IDX. Let's add one. */
757 XCDR (insertion_point)
758 = Fcons (Fcons (idx, def), XCDR (insertion_point));
761 return def;
764 void
765 copy_keymap_1 (chartable, idx, elt)
766 Lisp_Object chartable, idx, elt;
768 if (!SYMBOLP (elt) && ! NILP (Fkeymapp (elt)))
769 Faset (chartable, idx, Fcopy_keymap (elt));
772 DEFUN ("copy-keymap", Fcopy_keymap, Scopy_keymap, 1, 1, 0,
773 "Return a copy of the keymap KEYMAP.\n\
774 The copy starts out with the same definitions of KEYMAP,\n\
775 but changing either the copy or KEYMAP does not affect the other.\n\
776 Any key definitions that are subkeymaps are recursively copied.\n\
777 However, a key definition which is a symbol whose definition is a keymap\n\
778 is not copied.")
779 (keymap)
780 Lisp_Object keymap;
782 register Lisp_Object copy, tail;
784 copy = Fcopy_alist (get_keymap (keymap));
786 for (tail = copy; CONSP (tail); tail = XCDR (tail))
788 Lisp_Object elt;
790 elt = XCAR (tail);
791 if (CHAR_TABLE_P (elt))
793 Lisp_Object indices[3];
795 elt = Fcopy_sequence (elt);
796 XCAR (tail) = elt;
798 map_char_table (copy_keymap_1, Qnil, elt, elt, 0, indices);
800 else if (VECTORP (elt))
802 int i;
804 elt = Fcopy_sequence (elt);
805 XCAR (tail) = elt;
807 for (i = 0; i < XVECTOR (elt)->size; i++)
808 if (!SYMBOLP (XVECTOR (elt)->contents[i])
809 && ! NILP (Fkeymapp (XVECTOR (elt)->contents[i])))
810 XVECTOR (elt)->contents[i]
811 = Fcopy_keymap (XVECTOR (elt)->contents[i]);
813 else if (CONSP (elt) && CONSP (XCDR (elt)))
815 Lisp_Object tem;
816 tem = XCDR (elt);
818 /* Is this a new format menu item. */
819 if (EQ (XCAR (tem),Qmenu_item))
821 /* Copy cell with menu-item marker. */
822 XCDR (elt)
823 = Fcons (XCAR (tem), XCDR (tem));
824 elt = XCDR (elt);
825 tem = XCDR (elt);
826 if (CONSP (tem))
828 /* Copy cell with menu-item name. */
829 XCDR (elt)
830 = Fcons (XCAR (tem), XCDR (tem));
831 elt = XCDR (elt);
832 tem = XCDR (elt);
834 if (CONSP (tem))
836 /* Copy cell with binding and if the binding is a keymap,
837 copy that. */
838 XCDR (elt)
839 = Fcons (XCAR (tem), XCDR (tem));
840 elt = XCDR (elt);
841 tem = XCAR (elt);
842 if (!(SYMBOLP (tem) || NILP (Fkeymapp (tem))))
843 XCAR (elt) = Fcopy_keymap (tem);
844 tem = XCDR (elt);
845 if (CONSP (tem) && CONSP (XCAR (tem)))
846 /* Delete cache for key equivalences. */
847 XCDR (elt) = XCDR (tem);
850 else
852 /* It may be an old fomat menu item.
853 Skip the optional menu string.
855 if (STRINGP (XCAR (tem)))
857 /* Copy the cell, since copy-alist didn't go this deep. */
858 XCDR (elt)
859 = Fcons (XCAR (tem), XCDR (tem));
860 elt = XCDR (elt);
861 tem = XCDR (elt);
862 /* Also skip the optional menu help string. */
863 if (CONSP (tem) && STRINGP (XCAR (tem)))
865 XCDR (elt)
866 = Fcons (XCAR (tem), XCDR (tem));
867 elt = XCDR (elt);
868 tem = XCDR (elt);
870 /* There may also be a list that caches key equivalences.
871 Just delete it for the new keymap. */
872 if (CONSP (tem)
873 && CONSP (XCAR (tem))
874 && (NILP (XCAR (XCAR (tem)))
875 || VECTORP (XCAR (XCAR (tem)))))
876 XCDR (elt) = XCDR (tem);
878 if (CONSP (elt)
879 && ! SYMBOLP (XCDR (elt))
880 && ! NILP (Fkeymapp (XCDR (elt))))
881 XCDR (elt) = Fcopy_keymap (XCDR (elt));
887 return copy;
890 /* Simple Keymap mutators and accessors. */
892 /* GC is possible in this function if it autoloads a keymap. */
894 DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0,
895 "Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.\n\
896 KEYMAP is a keymap. KEY is a string or a vector of symbols and characters\n\
897 meaning a sequence of keystrokes and events.\n\
898 Non-ASCII characters with codes above 127 (such as ISO Latin-1)\n\
899 can be included if you use a vector.\n\
900 DEF is anything that can be a key's definition:\n\
901 nil (means key is undefined in this keymap),\n\
902 a command (a Lisp function suitable for interactive calling)\n\
903 a string (treated as a keyboard macro),\n\
904 a keymap (to define a prefix key),\n\
905 a symbol. When the key is looked up, the symbol will stand for its\n\
906 function definition, which should at that time be one of the above,\n\
907 or another symbol whose function definition is used, etc.\n\
908 a cons (STRING . DEFN), meaning that DEFN is the definition\n\
909 (DEFN should be a valid definition in its own right),\n\
910 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.\n\
912 If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at\n\
913 the front of KEYMAP.")
914 (keymap, key, def)
915 Lisp_Object keymap;
916 Lisp_Object key;
917 Lisp_Object def;
919 register int idx;
920 register Lisp_Object c;
921 register Lisp_Object cmd;
922 int metized = 0;
923 int meta_bit;
924 int length;
925 struct gcpro gcpro1, gcpro2, gcpro3;
927 keymap = get_keymap_1 (keymap, 1, 1);
929 if (!VECTORP (key) && !STRINGP (key))
930 key = wrong_type_argument (Qarrayp, key);
932 length = XFASTINT (Flength (key));
933 if (length == 0)
934 return Qnil;
936 if (SYMBOLP (def) && !EQ (Vdefine_key_rebound_commands, Qt))
937 Vdefine_key_rebound_commands = Fcons (def, Vdefine_key_rebound_commands);
939 GCPRO3 (keymap, key, def);
941 if (VECTORP (key))
942 meta_bit = meta_modifier;
943 else
944 meta_bit = 0x80;
946 idx = 0;
947 while (1)
949 c = Faref (key, make_number (idx));
951 if (CONSP (c) && lucid_event_type_list_p (c))
952 c = Fevent_convert_list (c);
954 if (INTEGERP (c)
955 && (XINT (c) & meta_bit)
956 && !metized)
958 c = meta_prefix_char;
959 metized = 1;
961 else
963 if (INTEGERP (c))
964 XSETINT (c, XINT (c) & ~meta_bit);
966 metized = 0;
967 idx++;
970 if (! INTEGERP (c) && ! SYMBOLP (c) && ! CONSP (c))
971 error ("Key sequence contains invalid events");
973 if (idx == length)
974 RETURN_UNGCPRO (store_in_keymap (keymap, c, def));
976 cmd = get_keyelt (access_keymap (keymap, c, 0, 1), 1);
978 /* If this key is undefined, make it a prefix. */
979 if (NILP (cmd))
980 cmd = define_as_prefix (keymap, c);
982 keymap = get_keymap_1 (cmd, 0, 1);
983 if (NILP (keymap))
984 /* We must use Fkey_description rather than just passing key to
985 error; key might be a vector, not a string. */
986 error ("Key sequence %s uses invalid prefix characters",
987 XSTRING (Fkey_description (key))->data);
991 /* Value is number if KEY is too long; NIL if valid but has no definition. */
992 /* GC is possible in this function if it autoloads a keymap. */
994 DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 3, 0,
995 "In keymap KEYMAP, look up key sequence KEY. Return the definition.\n\
996 nil means undefined. See doc of `define-key' for kinds of definitions.\n\
998 A number as value means KEY is \"too long\";\n\
999 that is, characters or symbols in it except for the last one\n\
1000 fail to be a valid sequence of prefix characters in KEYMAP.\n\
1001 The number is how many characters at the front of KEY\n\
1002 it takes to reach a non-prefix command.\n\
1004 Normally, `lookup-key' ignores bindings for t, which act as default\n\
1005 bindings, used when nothing else in the keymap applies; this makes it\n\
1006 usable as a general function for probing keymaps. However, if the\n\
1007 third optional argument ACCEPT-DEFAULT is non-nil, `lookup-key' will\n\
1008 recognize the default bindings, just as `read-key-sequence' does.")
1009 (keymap, key, accept_default)
1010 register Lisp_Object keymap;
1011 Lisp_Object key;
1012 Lisp_Object accept_default;
1014 register int idx;
1015 register Lisp_Object cmd;
1016 register Lisp_Object c;
1017 int metized = 0;
1018 int length;
1019 int t_ok = ! NILP (accept_default);
1020 int meta_bit;
1021 struct gcpro gcpro1;
1023 keymap = get_keymap_1 (keymap, 1, 1);
1025 if (!VECTORP (key) && !STRINGP (key))
1026 key = wrong_type_argument (Qarrayp, key);
1028 length = XFASTINT (Flength (key));
1029 if (length == 0)
1030 return keymap;
1032 if (VECTORP (key))
1033 meta_bit = meta_modifier;
1034 else
1035 meta_bit = 0x80;
1037 GCPRO1 (key);
1039 idx = 0;
1040 while (1)
1042 c = Faref (key, make_number (idx));
1044 if (CONSP (c) && lucid_event_type_list_p (c))
1045 c = Fevent_convert_list (c);
1047 if (INTEGERP (c)
1048 && (XINT (c) & meta_bit)
1049 && !metized)
1051 c = meta_prefix_char;
1052 metized = 1;
1054 else
1056 if (INTEGERP (c))
1057 XSETINT (c, XINT (c) & ~meta_bit);
1059 metized = 0;
1060 idx++;
1063 cmd = get_keyelt (access_keymap (keymap, c, t_ok, 0), 1);
1064 if (idx == length)
1065 RETURN_UNGCPRO (cmd);
1067 keymap = get_keymap_1 (cmd, 0, 1);
1068 if (NILP (keymap))
1069 RETURN_UNGCPRO (make_number (idx));
1071 QUIT;
1075 /* Make KEYMAP define event C as a keymap (i.e., as a prefix).
1076 Assume that currently it does not define C at all.
1077 Return the keymap. */
1079 static Lisp_Object
1080 define_as_prefix (keymap, c)
1081 Lisp_Object keymap, c;
1083 Lisp_Object inherit, cmd;
1085 cmd = Fmake_sparse_keymap (Qnil);
1086 /* If this key is defined as a prefix in an inherited keymap,
1087 make it a prefix in this map, and make its definition
1088 inherit the other prefix definition. */
1089 inherit = access_keymap (keymap, c, 0, 0);
1090 #if 0
1091 /* This code is needed to do the right thing in the following case:
1092 keymap A inherits from B,
1093 you define KEY as a prefix in A,
1094 then later you define KEY as a prefix in B.
1095 We want the old prefix definition in A to inherit from that in B.
1096 It is hard to do that retroactively, so this code
1097 creates the prefix in B right away.
1099 But it turns out that this code causes problems immediately
1100 when the prefix in A is defined: it causes B to define KEY
1101 as a prefix with no subcommands.
1103 So I took out this code. */
1104 if (NILP (inherit))
1106 /* If there's an inherited keymap
1107 and it doesn't define this key,
1108 make it define this key. */
1109 Lisp_Object tail;
1111 for (tail = Fcdr (keymap); CONSP (tail); tail = XCDR (tail))
1112 if (EQ (XCAR (tail), Qkeymap))
1113 break;
1115 if (!NILP (tail))
1116 inherit = define_as_prefix (tail, c);
1118 #endif
1120 cmd = nconc2 (cmd, inherit);
1121 store_in_keymap (keymap, c, cmd);
1123 return cmd;
1126 /* Append a key to the end of a key sequence. We always make a vector. */
1128 Lisp_Object
1129 append_key (key_sequence, key)
1130 Lisp_Object key_sequence, key;
1132 Lisp_Object args[2];
1134 args[0] = key_sequence;
1136 args[1] = Fcons (key, Qnil);
1137 return Fvconcat (2, args);
1141 /* Global, local, and minor mode keymap stuff. */
1143 /* We can't put these variables inside current_minor_maps, since under
1144 some systems, static gets macro-defined to be the empty string.
1145 Ickypoo. */
1146 static Lisp_Object *cmm_modes, *cmm_maps;
1147 static int cmm_size;
1149 /* Error handler used in current_minor_maps. */
1150 static Lisp_Object
1151 current_minor_maps_error ()
1153 return Qnil;
1156 /* Store a pointer to an array of the keymaps of the currently active
1157 minor modes in *buf, and return the number of maps it contains.
1159 This function always returns a pointer to the same buffer, and may
1160 free or reallocate it, so if you want to keep it for a long time or
1161 hand it out to lisp code, copy it. This procedure will be called
1162 for every key sequence read, so the nice lispy approach (return a
1163 new assoclist, list, what have you) for each invocation would
1164 result in a lot of consing over time.
1166 If we used xrealloc/xmalloc and ran out of memory, they would throw
1167 back to the command loop, which would try to read a key sequence,
1168 which would call this function again, resulting in an infinite
1169 loop. Instead, we'll use realloc/malloc and silently truncate the
1170 list, let the key sequence be read, and hope some other piece of
1171 code signals the error. */
1173 current_minor_maps (modeptr, mapptr)
1174 Lisp_Object **modeptr, **mapptr;
1176 int i = 0;
1177 int list_number = 0;
1178 Lisp_Object alist, assoc, var, val;
1179 Lisp_Object lists[2];
1181 lists[0] = Vminor_mode_overriding_map_alist;
1182 lists[1] = Vminor_mode_map_alist;
1184 for (list_number = 0; list_number < 2; list_number++)
1185 for (alist = lists[list_number];
1186 CONSP (alist);
1187 alist = XCDR (alist))
1188 if ((assoc = XCAR (alist), CONSP (assoc))
1189 && (var = XCAR (assoc), SYMBOLP (var))
1190 && (val = find_symbol_value (var), ! EQ (val, Qunbound))
1191 && ! NILP (val))
1193 Lisp_Object temp;
1195 /* If a variable has an entry in Vminor_mode_overriding_map_alist,
1196 and also an entry in Vminor_mode_map_alist,
1197 ignore the latter. */
1198 if (list_number == 1)
1200 val = assq_no_quit (var, lists[0]);
1201 if (!NILP (val))
1202 break;
1205 if (i >= cmm_size)
1207 Lisp_Object *newmodes, *newmaps;
1209 if (cmm_maps)
1211 BLOCK_INPUT;
1212 cmm_size *= 2;
1213 newmodes
1214 = (Lisp_Object *) realloc (cmm_modes,
1215 cmm_size * sizeof (Lisp_Object));
1216 newmaps
1217 = (Lisp_Object *) realloc (cmm_maps,
1218 cmm_size * sizeof (Lisp_Object));
1219 UNBLOCK_INPUT;
1221 else
1223 BLOCK_INPUT;
1224 cmm_size = 30;
1225 newmodes
1226 = (Lisp_Object *) malloc (cmm_size * sizeof (Lisp_Object));
1227 newmaps
1228 = (Lisp_Object *) malloc (cmm_size * sizeof (Lisp_Object));
1229 UNBLOCK_INPUT;
1232 if (newmaps && newmodes)
1234 cmm_modes = newmodes;
1235 cmm_maps = newmaps;
1237 else
1238 break;
1241 /* Get the keymap definition--or nil if it is not defined. */
1242 temp = internal_condition_case_1 (Findirect_function,
1243 XCDR (assoc),
1244 Qerror, current_minor_maps_error);
1245 if (!NILP (temp))
1247 cmm_modes[i] = var;
1248 cmm_maps [i] = temp;
1249 i++;
1253 if (modeptr) *modeptr = cmm_modes;
1254 if (mapptr) *mapptr = cmm_maps;
1255 return i;
1258 /* GC is possible in this function if it autoloads a keymap. */
1260 DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 2, 0,
1261 "Return the binding for command KEY in current keymaps.\n\
1262 KEY is a string or vector, a sequence of keystrokes.\n\
1263 The binding is probably a symbol with a function definition.\n\
1265 Normally, `key-binding' ignores bindings for t, which act as default\n\
1266 bindings, used when nothing else in the keymap applies; this makes it\n\
1267 usable as a general function for probing keymaps. However, if the\n\
1268 optional second argument ACCEPT-DEFAULT is non-nil, `key-binding' does\n\
1269 recognize the default bindings, just as `read-key-sequence' does.")
1270 (key, accept_default)
1271 Lisp_Object key, accept_default;
1273 Lisp_Object *maps, value;
1274 int nmaps, i;
1275 struct gcpro gcpro1;
1277 GCPRO1 (key);
1279 if (!NILP (current_kboard->Voverriding_terminal_local_map))
1281 value = Flookup_key (current_kboard->Voverriding_terminal_local_map,
1282 key, accept_default);
1283 if (! NILP (value) && !INTEGERP (value))
1284 RETURN_UNGCPRO (value);
1286 else if (!NILP (Voverriding_local_map))
1288 value = Flookup_key (Voverriding_local_map, key, accept_default);
1289 if (! NILP (value) && !INTEGERP (value))
1290 RETURN_UNGCPRO (value);
1292 else
1294 Lisp_Object local;
1296 nmaps = current_minor_maps (0, &maps);
1297 /* Note that all these maps are GCPRO'd
1298 in the places where we found them. */
1300 for (i = 0; i < nmaps; i++)
1301 if (! NILP (maps[i]))
1303 value = Flookup_key (maps[i], key, accept_default);
1304 if (! NILP (value) && !INTEGERP (value))
1305 RETURN_UNGCPRO (value);
1308 local = get_local_map (PT, current_buffer, keymap);
1309 if (! NILP (local))
1311 value = Flookup_key (local, key, accept_default);
1312 if (! NILP (value) && !INTEGERP (value))
1313 RETURN_UNGCPRO (value);
1316 local = get_local_map (PT, current_buffer, local_map);
1318 if (! NILP (local))
1320 value = Flookup_key (local, key, accept_default);
1321 if (! NILP (value) && !INTEGERP (value))
1322 RETURN_UNGCPRO (value);
1326 value = Flookup_key (current_global_map, key, accept_default);
1327 UNGCPRO;
1328 if (! NILP (value) && !INTEGERP (value))
1329 return value;
1331 return Qnil;
1334 /* GC is possible in this function if it autoloads a keymap. */
1336 DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 2, 0,
1337 "Return the binding for command KEYS in current local keymap only.\n\
1338 KEYS is a string, a sequence of keystrokes.\n\
1339 The binding is probably a symbol with a function definition.\n\
1341 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1342 bindings; see the description of `lookup-key' for more details about this.")
1343 (keys, accept_default)
1344 Lisp_Object keys, accept_default;
1346 register Lisp_Object map;
1347 map = current_buffer->keymap;
1348 if (NILP (map))
1349 return Qnil;
1350 return Flookup_key (map, keys, accept_default);
1353 /* GC is possible in this function if it autoloads a keymap. */
1355 DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 2, 0,
1356 "Return the binding for command KEYS in current global keymap only.\n\
1357 KEYS is a string, a sequence of keystrokes.\n\
1358 The binding is probably a symbol with a function definition.\n\
1359 This function's return values are the same as those of lookup-key\n\
1360 \(which see).\n\
1362 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1363 bindings; see the description of `lookup-key' for more details about this.")
1364 (keys, accept_default)
1365 Lisp_Object keys, accept_default;
1367 return Flookup_key (current_global_map, keys, accept_default);
1370 /* GC is possible in this function if it autoloads a keymap. */
1372 DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0,
1373 "Find the visible minor mode bindings of KEY.\n\
1374 Return an alist of pairs (MODENAME . BINDING), where MODENAME is the\n\
1375 the symbol which names the minor mode binding KEY, and BINDING is\n\
1376 KEY's definition in that mode. In particular, if KEY has no\n\
1377 minor-mode bindings, return nil. If the first binding is a\n\
1378 non-prefix, all subsequent bindings will be omitted, since they would\n\
1379 be ignored. Similarly, the list doesn't include non-prefix bindings\n\
1380 that come after prefix bindings.\n\
1382 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1383 bindings; see the description of `lookup-key' for more details about this.")
1384 (key, accept_default)
1385 Lisp_Object key, accept_default;
1387 Lisp_Object *modes, *maps;
1388 int nmaps;
1389 Lisp_Object binding;
1390 int i, j;
1391 struct gcpro gcpro1, gcpro2;
1393 nmaps = current_minor_maps (&modes, &maps);
1394 /* Note that all these maps are GCPRO'd
1395 in the places where we found them. */
1397 binding = Qnil;
1398 GCPRO2 (key, binding);
1400 for (i = j = 0; i < nmaps; i++)
1401 if (! NILP (maps[i])
1402 && ! NILP (binding = Flookup_key (maps[i], key, accept_default))
1403 && !INTEGERP (binding))
1405 if (! NILP (get_keymap (binding)))
1406 maps[j++] = Fcons (modes[i], binding);
1407 else if (j == 0)
1408 RETURN_UNGCPRO (Fcons (Fcons (modes[i], binding), Qnil));
1411 UNGCPRO;
1412 return Flist (j, maps);
1415 DEFUN ("define-prefix-command", Fdefine_prefix_command, Sdefine_prefix_command, 1, 3, 0,
1416 "Define COMMAND as a prefix command. COMMAND should be a symbol.\n\
1417 A new sparse keymap is stored as COMMAND's function definition and its value.\n\
1418 If a second optional argument MAPVAR is given, the map is stored as\n\
1419 its value instead of as COMMAND's value; but COMMAND is still defined\n\
1420 as a function.\n\
1421 The third optional argument NAME, if given, supplies a menu name\n\
1422 string for the map. This is required to use the keymap as a menu.")
1423 (command, mapvar, name)
1424 Lisp_Object command, mapvar, name;
1426 Lisp_Object map;
1427 map = Fmake_sparse_keymap (name);
1428 Ffset (command, map);
1429 if (!NILP (mapvar))
1430 Fset (mapvar, map);
1431 else
1432 Fset (command, map);
1433 return command;
1436 DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0,
1437 "Select KEYMAP as the global keymap.")
1438 (keymap)
1439 Lisp_Object keymap;
1441 keymap = get_keymap (keymap);
1442 current_global_map = keymap;
1444 return Qnil;
1447 DEFUN ("use-local-map", Fuse_local_map, Suse_local_map, 1, 1, 0,
1448 "Select KEYMAP as the local keymap.\n\
1449 If KEYMAP is nil, that means no local keymap.")
1450 (keymap)
1451 Lisp_Object keymap;
1453 if (!NILP (keymap))
1454 keymap = get_keymap (keymap);
1456 current_buffer->keymap = keymap;
1458 return Qnil;
1461 DEFUN ("current-local-map", Fcurrent_local_map, Scurrent_local_map, 0, 0, 0,
1462 "Return current buffer's local keymap, or nil if it has none.")
1465 return current_buffer->keymap;
1468 DEFUN ("current-global-map", Fcurrent_global_map, Scurrent_global_map, 0, 0, 0,
1469 "Return the current global keymap.")
1472 return current_global_map;
1475 DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps, Scurrent_minor_mode_maps, 0, 0, 0,
1476 "Return a list of keymaps for the minor modes of the current buffer.")
1479 Lisp_Object *maps;
1480 int nmaps = current_minor_maps (0, &maps);
1482 return Flist (nmaps, maps);
1485 /* Help functions for describing and documenting keymaps. */
1487 static void accessible_keymaps_char_table ();
1489 /* This function cannot GC. */
1491 DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps,
1492 1, 2, 0,
1493 "Find all keymaps accessible via prefix characters from KEYMAP.\n\
1494 Returns a list of elements of the form (KEYS . MAP), where the sequence\n\
1495 KEYS starting from KEYMAP gets you to MAP. These elements are ordered\n\
1496 so that the KEYS increase in length. The first element is ([] . KEYMAP).\n\
1497 An optional argument PREFIX, if non-nil, should be a key sequence;\n\
1498 then the value includes only maps for prefixes that start with PREFIX.")
1499 (keymap, prefix)
1500 Lisp_Object keymap, prefix;
1502 Lisp_Object maps, good_maps, tail;
1503 int prefixlen = 0;
1505 /* no need for gcpro because we don't autoload any keymaps. */
1507 if (!NILP (prefix))
1508 prefixlen = XINT (Flength (prefix));
1510 if (!NILP (prefix))
1512 /* If a prefix was specified, start with the keymap (if any) for
1513 that prefix, so we don't waste time considering other prefixes. */
1514 Lisp_Object tem;
1515 tem = Flookup_key (keymap, prefix, Qt);
1516 /* Flookup_key may give us nil, or a number,
1517 if the prefix is not defined in this particular map.
1518 It might even give us a list that isn't a keymap. */
1519 tem = get_keymap_1 (tem, 0, 0);
1520 if (!NILP (tem))
1522 /* Convert PREFIX to a vector now, so that later on
1523 we don't have to deal with the possibility of a string. */
1524 if (STRINGP (prefix))
1526 int i, i_byte, c;
1527 Lisp_Object copy;
1529 copy = Fmake_vector (make_number (XSTRING (prefix)->size), Qnil);
1530 for (i = 0, i_byte = 0; i < XSTRING (prefix)->size;)
1532 int i_before = i;
1534 FETCH_STRING_CHAR_ADVANCE (c, prefix, i, i_byte);
1535 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
1536 c ^= 0200 | meta_modifier;
1537 XVECTOR (copy)->contents[i_before] = make_number (c);
1539 prefix = copy;
1541 maps = Fcons (Fcons (prefix, tem), Qnil);
1543 else
1544 return Qnil;
1546 else
1547 maps = Fcons (Fcons (Fmake_vector (make_number (0), Qnil),
1548 get_keymap (keymap)),
1549 Qnil);
1551 /* For each map in the list maps,
1552 look at any other maps it points to,
1553 and stick them at the end if they are not already in the list.
1555 This is a breadth-first traversal, where tail is the queue of
1556 nodes, and maps accumulates a list of all nodes visited. */
1558 for (tail = maps; CONSP (tail); tail = XCDR (tail))
1560 register Lisp_Object thisseq, thismap;
1561 Lisp_Object last;
1562 /* Does the current sequence end in the meta-prefix-char? */
1563 int is_metized;
1565 thisseq = Fcar (Fcar (tail));
1566 thismap = Fcdr (Fcar (tail));
1567 last = make_number (XINT (Flength (thisseq)) - 1);
1568 is_metized = (XINT (last) >= 0
1569 /* Don't metize the last char of PREFIX. */
1570 && XINT (last) >= prefixlen
1571 && EQ (Faref (thisseq, last), meta_prefix_char));
1573 for (; CONSP (thismap); thismap = XCDR (thismap))
1575 Lisp_Object elt;
1577 elt = XCAR (thismap);
1579 QUIT;
1581 if (CHAR_TABLE_P (elt))
1583 Lisp_Object indices[3];
1585 map_char_table (accessible_keymaps_char_table, Qnil,
1586 elt, Fcons (maps, Fcons (tail, thisseq)),
1587 0, indices);
1589 else if (VECTORP (elt))
1591 register int i;
1593 /* Vector keymap. Scan all the elements. */
1594 for (i = 0; i < XVECTOR (elt)->size; i++)
1596 register Lisp_Object tem;
1597 register Lisp_Object cmd;
1599 cmd = get_keyelt (XVECTOR (elt)->contents[i], 0);
1600 if (NILP (cmd)) continue;
1601 tem = Fkeymapp (cmd);
1602 if (!NILP (tem))
1604 cmd = get_keymap (cmd);
1605 /* Ignore keymaps that are already added to maps. */
1606 tem = Frassq (cmd, maps);
1607 if (NILP (tem))
1609 /* If the last key in thisseq is meta-prefix-char,
1610 turn it into a meta-ized keystroke. We know
1611 that the event we're about to append is an
1612 ascii keystroke since we're processing a
1613 keymap table. */
1614 if (is_metized)
1616 int meta_bit = meta_modifier;
1617 tem = Fcopy_sequence (thisseq);
1619 Faset (tem, last, make_number (i | meta_bit));
1621 /* This new sequence is the same length as
1622 thisseq, so stick it in the list right
1623 after this one. */
1624 XCDR (tail)
1625 = Fcons (Fcons (tem, cmd), XCDR (tail));
1627 else
1629 tem = append_key (thisseq, make_number (i));
1630 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
1636 else if (CONSP (elt))
1638 register Lisp_Object cmd, tem;
1640 cmd = get_keyelt (XCDR (elt), 0);
1641 /* Ignore definitions that aren't keymaps themselves. */
1642 tem = Fkeymapp (cmd);
1643 if (!NILP (tem))
1645 /* Ignore keymaps that have been seen already. */
1646 cmd = get_keymap (cmd);
1647 tem = Frassq (cmd, maps);
1648 if (NILP (tem))
1650 /* Let elt be the event defined by this map entry. */
1651 elt = XCAR (elt);
1653 /* If the last key in thisseq is meta-prefix-char, and
1654 this entry is a binding for an ascii keystroke,
1655 turn it into a meta-ized keystroke. */
1656 if (is_metized && INTEGERP (elt))
1658 Lisp_Object element;
1660 element = thisseq;
1661 tem = Fvconcat (1, &element);
1662 XSETFASTINT (XVECTOR (tem)->contents[XINT (last)],
1663 XINT (elt) | meta_modifier);
1665 /* This new sequence is the same length as
1666 thisseq, so stick it in the list right
1667 after this one. */
1668 XCDR (tail)
1669 = Fcons (Fcons (tem, cmd), XCDR (tail));
1671 else
1672 nconc2 (tail,
1673 Fcons (Fcons (append_key (thisseq, elt), cmd),
1674 Qnil));
1681 if (NILP (prefix))
1682 return maps;
1684 /* Now find just the maps whose access prefixes start with PREFIX. */
1686 good_maps = Qnil;
1687 for (; CONSP (maps); maps = XCDR (maps))
1689 Lisp_Object elt, thisseq;
1690 elt = XCAR (maps);
1691 thisseq = XCAR (elt);
1692 /* The access prefix must be at least as long as PREFIX,
1693 and the first elements must match those of PREFIX. */
1694 if (XINT (Flength (thisseq)) >= prefixlen)
1696 int i;
1697 for (i = 0; i < prefixlen; i++)
1699 Lisp_Object i1;
1700 XSETFASTINT (i1, i);
1701 if (!EQ (Faref (thisseq, i1), Faref (prefix, i1)))
1702 break;
1704 if (i == prefixlen)
1705 good_maps = Fcons (elt, good_maps);
1709 return Fnreverse (good_maps);
1712 static void
1713 accessible_keymaps_char_table (args, index, cmd)
1714 Lisp_Object args, index, cmd;
1716 Lisp_Object tem;
1717 Lisp_Object maps, tail, thisseq;
1719 if (NILP (cmd))
1720 return;
1722 maps = XCAR (args);
1723 tail = XCAR (XCDR (args));
1724 thisseq = XCDR (XCDR (args));
1726 tem = Fkeymapp (cmd);
1727 if (!NILP (tem))
1729 cmd = get_keymap (cmd);
1730 /* Ignore keymaps that are already added to maps. */
1731 tem = Frassq (cmd, maps);
1732 if (NILP (tem))
1734 tem = append_key (thisseq, index);
1735 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
1740 Lisp_Object Qsingle_key_description, Qkey_description;
1742 /* This function cannot GC. */
1744 DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0,
1745 "Return a pretty description of key-sequence KEYS.\n\
1746 Control characters turn into \"C-foo\" sequences, meta into \"M-foo\"\n\
1747 spaces are put between sequence elements, etc.")
1748 (keys)
1749 Lisp_Object keys;
1751 int len;
1752 int i, i_byte;
1753 Lisp_Object sep;
1754 Lisp_Object *args;
1756 if (STRINGP (keys))
1758 Lisp_Object vector;
1759 vector = Fmake_vector (Flength (keys), Qnil);
1760 for (i = 0, i_byte = 0; i < XSTRING (keys)->size; )
1762 int c;
1763 int i_before = i;
1765 FETCH_STRING_CHAR_ADVANCE (c, keys, i, i_byte);
1766 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
1767 c ^= 0200 | meta_modifier;
1768 XSETFASTINT (XVECTOR (vector)->contents[i_before], c);
1770 keys = vector;
1773 if (VECTORP (keys))
1775 /* In effect, this computes
1776 (mapconcat 'single-key-description keys " ")
1777 but we shouldn't use mapconcat because it can do GC. */
1779 len = XVECTOR (keys)->size;
1780 sep = build_string (" ");
1781 /* This has one extra element at the end that we don't pass to Fconcat. */
1782 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
1784 for (i = 0; i < len; i++)
1786 args[i * 2] = Fsingle_key_description (XVECTOR (keys)->contents[i]);
1787 args[i * 2 + 1] = sep;
1790 else if (CONSP (keys))
1792 /* In effect, this computes
1793 (mapconcat 'single-key-description keys " ")
1794 but we shouldn't use mapconcat because it can do GC. */
1796 len = XFASTINT (Flength (keys));
1797 sep = build_string (" ");
1798 /* This has one extra element at the end that we don't pass to Fconcat. */
1799 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
1801 for (i = 0; i < len; i++)
1803 args[i * 2] = Fsingle_key_description (XCAR (keys));
1804 args[i * 2 + 1] = sep;
1805 keys = XCDR (keys);
1808 else
1809 keys = wrong_type_argument (Qarrayp, keys);
1811 return Fconcat (len * 2 - 1, args);
1814 char *
1815 push_key_description (c, p)
1816 register unsigned int c;
1817 register char *p;
1819 /* Clear all the meaningless bits above the meta bit. */
1820 c &= meta_modifier | ~ - meta_modifier;
1822 if (c & alt_modifier)
1824 *p++ = 'A';
1825 *p++ = '-';
1826 c -= alt_modifier;
1828 if (c & ctrl_modifier)
1830 *p++ = 'C';
1831 *p++ = '-';
1832 c -= ctrl_modifier;
1834 if (c & hyper_modifier)
1836 *p++ = 'H';
1837 *p++ = '-';
1838 c -= hyper_modifier;
1840 if (c & meta_modifier)
1842 *p++ = 'M';
1843 *p++ = '-';
1844 c -= meta_modifier;
1846 if (c & shift_modifier)
1848 *p++ = 'S';
1849 *p++ = '-';
1850 c -= shift_modifier;
1852 if (c & super_modifier)
1854 *p++ = 's';
1855 *p++ = '-';
1856 c -= super_modifier;
1858 if (c < 040)
1860 if (c == 033)
1862 *p++ = 'E';
1863 *p++ = 'S';
1864 *p++ = 'C';
1866 else if (c == '\t')
1868 *p++ = 'T';
1869 *p++ = 'A';
1870 *p++ = 'B';
1872 else if (c == Ctl ('M'))
1874 *p++ = 'R';
1875 *p++ = 'E';
1876 *p++ = 'T';
1878 else
1880 *p++ = 'C';
1881 *p++ = '-';
1882 if (c > 0 && c <= Ctl ('Z'))
1883 *p++ = c + 0140;
1884 else
1885 *p++ = c + 0100;
1888 else if (c == 0177)
1890 *p++ = 'D';
1891 *p++ = 'E';
1892 *p++ = 'L';
1894 else if (c == ' ')
1896 *p++ = 'S';
1897 *p++ = 'P';
1898 *p++ = 'C';
1900 else if (c < 128
1901 || (NILP (current_buffer->enable_multibyte_characters)
1902 && SINGLE_BYTE_CHAR_P (c)))
1903 *p++ = c;
1904 else
1906 if (! NILP (current_buffer->enable_multibyte_characters))
1907 c = unibyte_char_to_multibyte (c);
1909 if (NILP (current_buffer->enable_multibyte_characters)
1910 || SINGLE_BYTE_CHAR_P (c)
1911 || ! char_valid_p (c, 0))
1913 int bit_offset;
1914 *p++ = '\\';
1915 /* The biggest character code uses 19 bits. */
1916 for (bit_offset = 18; bit_offset >= 0; bit_offset -= 3)
1918 if (c >= (1 << bit_offset))
1919 *p++ = ((c & (7 << bit_offset)) >> bit_offset) + '0';
1922 else
1924 p += CHAR_STRING (c, p);
1928 return p;
1931 /* This function cannot GC. */
1933 DEFUN ("single-key-description", Fsingle_key_description, Ssingle_key_description, 1, 1, 0,
1934 "Return a pretty description of command character KEY.\n\
1935 Control characters turn into C-whatever, etc.")
1936 (key)
1937 Lisp_Object key;
1939 if (CONSP (key) && lucid_event_type_list_p (key))
1940 key = Fevent_convert_list (key);
1942 key = EVENT_HEAD (key);
1944 if (INTEGERP (key)) /* Normal character */
1946 unsigned int charset, c1, c2;
1947 int without_bits = XINT (key) & ~((-1) << CHARACTERBITS);
1949 if (SINGLE_BYTE_CHAR_P (without_bits))
1950 charset = 0;
1951 else
1952 SPLIT_CHAR (without_bits, charset, c1, c2);
1954 if (charset
1955 && CHARSET_DEFINED_P (charset)
1956 && ((c1 >= 0 && c1 < 32)
1957 || (c2 >= 0 && c2 < 32)))
1959 /* Handle a generic character. */
1960 Lisp_Object name;
1961 name = CHARSET_TABLE_INFO (charset, CHARSET_LONG_NAME_IDX);
1962 CHECK_STRING (name, 0);
1963 return concat2 (build_string ("Character set "), name);
1965 else
1967 char tem[KEY_DESCRIPTION_SIZE];
1969 *push_key_description (XUINT (key), tem) = 0;
1970 return build_string (tem);
1973 else if (SYMBOLP (key)) /* Function key or event-symbol */
1975 char *buffer = (char *) alloca (STRING_BYTES (XSYMBOL (key)->name) + 5);
1976 sprintf (buffer, "<%s>", XSYMBOL (key)->name->data);
1977 return build_string (buffer);
1979 else if (STRINGP (key)) /* Buffer names in the menubar. */
1980 return Fcopy_sequence (key);
1981 else
1982 error ("KEY must be an integer, cons, symbol, or string");
1985 char *
1986 push_text_char_description (c, p)
1987 register unsigned int c;
1988 register char *p;
1990 if (c >= 0200)
1992 *p++ = 'M';
1993 *p++ = '-';
1994 c -= 0200;
1996 if (c < 040)
1998 *p++ = '^';
1999 *p++ = c + 64; /* 'A' - 1 */
2001 else if (c == 0177)
2003 *p++ = '^';
2004 *p++ = '?';
2006 else
2007 *p++ = c;
2008 return p;
2011 /* This function cannot GC. */
2013 DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0,
2014 "Return a pretty description of file-character CHARACTER.\n\
2015 Control characters turn into \"^char\", etc.")
2016 (character)
2017 Lisp_Object character;
2019 /* Currently MAX_MULTIBYTE_LENGTH is 4 (< 6). */
2020 unsigned char str[6];
2021 int c;
2023 CHECK_NUMBER (character, 0);
2025 c = XINT (character);
2026 if (!SINGLE_BYTE_CHAR_P (c))
2028 int len = CHAR_STRING (c, str);
2030 return make_multibyte_string (str, 1, len);
2033 *push_text_char_description (c & 0377, str) = 0;
2035 return build_string (str);
2038 /* Return non-zero if SEQ contains only ASCII characters, perhaps with
2039 a meta bit. */
2040 static int
2041 ascii_sequence_p (seq)
2042 Lisp_Object seq;
2044 int i;
2045 int len = XINT (Flength (seq));
2047 for (i = 0; i < len; i++)
2049 Lisp_Object ii, elt;
2051 XSETFASTINT (ii, i);
2052 elt = Faref (seq, ii);
2054 if (!INTEGERP (elt)
2055 || (XUINT (elt) & ~CHAR_META) >= 0x80)
2056 return 0;
2059 return 1;
2063 /* where-is - finding a command in a set of keymaps. */
2065 static Lisp_Object where_is_internal_1 ();
2066 static void where_is_internal_2 ();
2068 /* This function can GC if Flookup_key autoloads any keymaps. */
2070 DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 4, 0,
2071 "Return list of keys that invoke DEFINITION.\n\
2072 If KEYMAP is non-nil, search only KEYMAP and the global keymap.\n\
2073 If KEYMAP is nil, search all the currently active keymaps.\n\
2075 If optional 3rd arg FIRSTONLY is non-nil, return the first key sequence found,\n\
2076 rather than a list of all possible key sequences.\n\
2077 If FIRSTONLY is the symbol `non-ascii', return the first binding found,\n\
2078 no matter what it is.\n\
2079 If FIRSTONLY has another non-nil value, prefer sequences of ASCII characters,\n\
2080 and entirely reject menu bindings.\n\
2082 If optional 4th arg NOINDIRECT is non-nil, don't follow indirections\n\
2083 to other keymaps or slots. This makes it possible to search for an\n\
2084 indirect definition itself.")
2085 (definition, xkeymap, firstonly, noindirect)
2086 Lisp_Object definition, xkeymap;
2087 Lisp_Object firstonly, noindirect;
2089 Lisp_Object maps;
2090 Lisp_Object found, sequences;
2091 Lisp_Object keymap1;
2092 int keymap_specified = !NILP (xkeymap);
2093 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
2094 /* 1 means ignore all menu bindings entirely. */
2095 int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii);
2097 /* Find keymaps accessible from `keymap' or the current
2098 context. But don't muck with the value of `keymap',
2099 because `where_is_internal_1' uses it to check for
2100 shadowed bindings. */
2101 keymap1 = xkeymap;
2102 if (! keymap_specified)
2103 keymap1 = get_local_map (PT, current_buffer, keymap);
2105 if (!NILP (keymap1))
2106 maps = nconc2 (Faccessible_keymaps (get_keymap (keymap1), Qnil),
2107 Faccessible_keymaps (get_keymap (current_global_map),
2108 Qnil));
2109 else
2111 keymap1 = xkeymap;
2112 if (! keymap_specified)
2113 keymap1 = get_local_map (PT, current_buffer, local_map);
2115 if (!NILP (keymap1))
2116 maps = nconc2 (Faccessible_keymaps (get_keymap (keymap1), Qnil),
2117 Faccessible_keymaps (get_keymap (current_global_map),
2118 Qnil));
2119 else
2120 maps = Faccessible_keymaps (get_keymap (current_global_map), Qnil);
2123 /* Put the minor mode keymaps on the front. */
2124 if (! keymap_specified)
2126 Lisp_Object minors;
2127 minors = Fnreverse (Fcurrent_minor_mode_maps ());
2128 while (!NILP (minors))
2130 maps = nconc2 (Faccessible_keymaps (get_keymap (XCAR (minors)),
2131 Qnil),
2132 maps);
2133 minors = XCDR (minors);
2137 GCPRO5 (definition, xkeymap, maps, found, sequences);
2138 found = Qnil;
2139 sequences = Qnil;
2141 for (; !NILP (maps); maps = Fcdr (maps))
2143 /* Key sequence to reach map, and the map that it reaches */
2144 register Lisp_Object this, map;
2146 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
2147 [M-CHAR] sequences, check if last character of the sequence
2148 is the meta-prefix char. */
2149 Lisp_Object last;
2150 int last_is_meta;
2152 this = Fcar (Fcar (maps));
2153 map = Fcdr (Fcar (maps));
2154 last = make_number (XINT (Flength (this)) - 1);
2155 last_is_meta = (XINT (last) >= 0
2156 && EQ (Faref (this, last), meta_prefix_char));
2158 QUIT;
2160 while (CONSP (map))
2162 /* Because the code we want to run on each binding is rather
2163 large, we don't want to have two separate loop bodies for
2164 sparse keymap bindings and tables; we want to iterate one
2165 loop body over both keymap and vector bindings.
2167 For this reason, if Fcar (map) is a vector, we don't
2168 advance map to the next element until i indicates that we
2169 have finished off the vector. */
2170 Lisp_Object elt, key, binding;
2171 elt = XCAR (map);
2172 map = XCDR (map);
2174 sequences = Qnil;
2176 QUIT;
2178 /* Set key and binding to the current key and binding, and
2179 advance map and i to the next binding. */
2180 if (VECTORP (elt))
2182 Lisp_Object sequence;
2183 int i;
2184 /* In a vector, look at each element. */
2185 for (i = 0; i < XVECTOR (elt)->size; i++)
2187 binding = XVECTOR (elt)->contents[i];
2188 XSETFASTINT (key, i);
2189 sequence = where_is_internal_1 (binding, key, definition,
2190 noindirect, xkeymap, this,
2191 last, nomenus, last_is_meta);
2192 if (!NILP (sequence))
2193 sequences = Fcons (sequence, sequences);
2196 else if (CHAR_TABLE_P (elt))
2198 Lisp_Object indices[3];
2199 Lisp_Object args;
2201 args = Fcons (Fcons (Fcons (definition, noindirect),
2202 Fcons (xkeymap, Qnil)),
2203 Fcons (Fcons (this, last),
2204 Fcons (make_number (nomenus),
2205 make_number (last_is_meta))));
2207 map_char_table (where_is_internal_2, Qnil, elt, args,
2208 0, indices);
2209 sequences = XCDR (XCDR (XCAR (args)));
2211 else if (CONSP (elt))
2213 Lisp_Object sequence;
2215 key = XCAR (elt);
2216 binding = XCDR (elt);
2218 sequence = where_is_internal_1 (binding, key, definition,
2219 noindirect, xkeymap, this,
2220 last, nomenus, last_is_meta);
2221 if (!NILP (sequence))
2222 sequences = Fcons (sequence, sequences);
2226 for (; ! NILP (sequences); sequences = XCDR (sequences))
2228 Lisp_Object sequence;
2230 sequence = XCAR (sequences);
2232 /* It is a true unshadowed match. Record it, unless it's already
2233 been seen (as could happen when inheriting keymaps). */
2234 if (NILP (Fmember (sequence, found)))
2235 found = Fcons (sequence, found);
2237 /* If firstonly is Qnon_ascii, then we can return the first
2238 binding we find. If firstonly is not Qnon_ascii but not
2239 nil, then we should return the first ascii-only binding
2240 we find. */
2241 if (EQ (firstonly, Qnon_ascii))
2242 RETURN_UNGCPRO (sequence);
2243 else if (! NILP (firstonly) && ascii_sequence_p (sequence))
2244 RETURN_UNGCPRO (sequence);
2249 UNGCPRO;
2251 found = Fnreverse (found);
2253 /* firstonly may have been t, but we may have gone all the way through
2254 the keymaps without finding an all-ASCII key sequence. So just
2255 return the best we could find. */
2256 if (! NILP (firstonly))
2257 return Fcar (found);
2259 return found;
2262 /* This is the function that Fwhere_is_internal calls using map_char_table.
2263 ARGS has the form
2264 (((DEFINITION . NOINDIRECT) . (KEYMAP . RESULT))
2266 ((THIS . LAST) . (NOMENUS . LAST_IS_META)))
2267 Since map_char_table doesn't really use the return value from this function,
2268 we the result append to RESULT, the slot in ARGS. */
2270 static void
2271 where_is_internal_2 (args, key, binding)
2272 Lisp_Object args, key, binding;
2274 Lisp_Object definition, noindirect, keymap, this, last;
2275 Lisp_Object result, sequence;
2276 int nomenus, last_is_meta;
2278 result = XCDR (XCDR (XCAR (args)));
2279 definition = XCAR (XCAR (XCAR (args)));
2280 noindirect = XCDR (XCAR (XCAR (args)));
2281 keymap = XCAR (XCDR (XCAR (args)));
2282 this = XCAR (XCAR (XCDR (args)));
2283 last = XCDR (XCAR (XCDR (args)));
2284 nomenus = XFASTINT (XCAR (XCDR (XCDR (args))));
2285 last_is_meta = XFASTINT (XCDR (XCDR (XCDR (args))));
2287 sequence = where_is_internal_1 (binding, key, definition, noindirect, keymap,
2288 this, last, nomenus, last_is_meta);
2290 if (!NILP (sequence))
2291 XCDR (XCDR (XCAR (args)))
2292 = Fcons (sequence, result);
2295 static Lisp_Object
2296 where_is_internal_1 (binding, key, definition, noindirect, keymap, this, last,
2297 nomenus, last_is_meta)
2298 Lisp_Object binding, key, definition, noindirect, keymap, this, last;
2299 int nomenus, last_is_meta;
2301 Lisp_Object sequence;
2302 int keymap_specified = !NILP (keymap);
2304 /* Search through indirections unless that's not wanted. */
2305 if (NILP (noindirect))
2307 if (nomenus)
2309 while (1)
2311 Lisp_Object map, tem;
2312 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
2313 map = get_keymap_1 (Fcar_safe (definition), 0, 0);
2314 tem = Fkeymapp (map);
2315 if (!NILP (tem))
2316 definition = access_keymap (map, Fcdr (definition), 0, 0);
2317 else
2318 break;
2320 /* If the contents are (menu-item ...) or (STRING ...), reject. */
2321 if (CONSP (definition)
2322 && (EQ (XCAR (definition),Qmenu_item)
2323 || STRINGP (XCAR (definition))))
2324 return Qnil;
2326 else
2327 binding = get_keyelt (binding, 0);
2330 /* End this iteration if this element does not match
2331 the target. */
2333 if (CONSP (definition))
2335 Lisp_Object tem;
2336 tem = Fequal (binding, definition);
2337 if (NILP (tem))
2338 return Qnil;
2340 else
2341 if (!EQ (binding, definition))
2342 return Qnil;
2344 /* We have found a match.
2345 Construct the key sequence where we found it. */
2346 if (INTEGERP (key) && last_is_meta)
2348 sequence = Fcopy_sequence (this);
2349 Faset (sequence, last, make_number (XINT (key) | meta_modifier));
2351 else
2352 sequence = append_key (this, key);
2354 /* Verify that this key binding is not shadowed by another
2355 binding for the same key, before we say it exists.
2357 Mechanism: look for local definition of this key and if
2358 it is defined and does not match what we found then
2359 ignore this key.
2361 Either nil or number as value from Flookup_key
2362 means undefined. */
2363 if (keymap_specified)
2365 binding = Flookup_key (keymap, sequence, Qnil);
2366 if (!NILP (binding) && !INTEGERP (binding))
2368 if (CONSP (definition))
2370 Lisp_Object tem;
2371 tem = Fequal (binding, definition);
2372 if (NILP (tem))
2373 return Qnil;
2375 else
2376 if (!EQ (binding, definition))
2377 return Qnil;
2380 else
2382 binding = Fkey_binding (sequence, Qnil);
2383 if (!EQ (binding, definition))
2384 return Qnil;
2387 return sequence;
2390 /* describe-bindings - summarizing all the bindings in a set of keymaps. */
2392 DEFUN ("describe-bindings-internal", Fdescribe_bindings_internal, Sdescribe_bindings_internal, 0, 2, "",
2393 "Show a list of all defined keys, and their definitions.\n\
2394 We put that list in a buffer, and display the buffer.\n\
2396 The optional argument MENUS, if non-nil, says to mention menu bindings.\n\
2397 \(Ordinarily these are omitted from the output.)\n\
2398 The optional argument PREFIX, if non-nil, should be a key sequence;\n\
2399 then we display only bindings that start with that prefix.")
2400 (menus, prefix)
2401 Lisp_Object menus, prefix;
2403 register Lisp_Object thisbuf;
2404 XSETBUFFER (thisbuf, current_buffer);
2405 internal_with_output_to_temp_buffer ("*Help*",
2406 describe_buffer_bindings,
2407 list3 (thisbuf, prefix, menus));
2408 return Qnil;
2411 /* ARG is (BUFFER PREFIX MENU-FLAG). */
2413 static Lisp_Object
2414 describe_buffer_bindings (arg)
2415 Lisp_Object arg;
2417 Lisp_Object descbuf, prefix, shadow;
2418 int nomenu;
2419 register Lisp_Object start1;
2420 struct gcpro gcpro1;
2422 char *alternate_heading
2423 = "\
2424 Keyboard translations:\n\n\
2425 You type Translation\n\
2426 -------- -----------\n";
2428 descbuf = XCAR (arg);
2429 arg = XCDR (arg);
2430 prefix = XCAR (arg);
2431 arg = XCDR (arg);
2432 nomenu = NILP (XCAR (arg));
2434 shadow = Qnil;
2435 GCPRO1 (shadow);
2437 Fset_buffer (Vstandard_output);
2439 /* Report on alternates for keys. */
2440 if (STRINGP (Vkeyboard_translate_table) && !NILP (prefix))
2442 int c;
2443 unsigned char *translate = XSTRING (Vkeyboard_translate_table)->data;
2444 int translate_len = XSTRING (Vkeyboard_translate_table)->size;
2446 for (c = 0; c < translate_len; c++)
2447 if (translate[c] != c)
2449 char buf[KEY_DESCRIPTION_SIZE];
2450 char *bufend;
2452 if (alternate_heading)
2454 insert_string (alternate_heading);
2455 alternate_heading = 0;
2458 bufend = push_key_description (translate[c], buf);
2459 insert (buf, bufend - buf);
2460 Findent_to (make_number (16), make_number (1));
2461 bufend = push_key_description (c, buf);
2462 insert (buf, bufend - buf);
2464 insert ("\n", 1);
2467 insert ("\n", 1);
2470 if (!NILP (Vkey_translation_map))
2471 describe_map_tree (Vkey_translation_map, 0, Qnil, prefix,
2472 "Key translations", nomenu, 1, 0);
2475 int i, nmaps;
2476 Lisp_Object *modes, *maps;
2478 /* Temporarily switch to descbuf, so that we can get that buffer's
2479 minor modes correctly. */
2480 Fset_buffer (descbuf);
2482 if (!NILP (current_kboard->Voverriding_terminal_local_map)
2483 || !NILP (Voverriding_local_map))
2484 nmaps = 0;
2485 else
2486 nmaps = current_minor_maps (&modes, &maps);
2487 Fset_buffer (Vstandard_output);
2489 /* Print the minor mode maps. */
2490 for (i = 0; i < nmaps; i++)
2492 /* The title for a minor mode keymap
2493 is constructed at run time.
2494 We let describe_map_tree do the actual insertion
2495 because it takes care of other features when doing so. */
2496 char *title, *p;
2498 if (!SYMBOLP (modes[i]))
2499 abort();
2501 p = title = (char *) alloca (42 + XSYMBOL (modes[i])->name->size);
2502 *p++ = '\f';
2503 *p++ = '\n';
2504 *p++ = '`';
2505 bcopy (XSYMBOL (modes[i])->name->data, p,
2506 XSYMBOL (modes[i])->name->size);
2507 p += XSYMBOL (modes[i])->name->size;
2508 *p++ = '\'';
2509 bcopy (" Minor Mode Bindings", p, sizeof (" Minor Mode Bindings") - 1);
2510 p += sizeof (" Minor Mode Bindings") - 1;
2511 *p = 0;
2513 describe_map_tree (maps[i], 1, shadow, prefix, title, nomenu, 0, 0);
2514 shadow = Fcons (maps[i], shadow);
2518 /* Print the (major mode) local map. */
2519 if (!NILP (current_kboard->Voverriding_terminal_local_map))
2520 start1 = current_kboard->Voverriding_terminal_local_map;
2521 else if (!NILP (Voverriding_local_map))
2522 start1 = Voverriding_local_map;
2523 else
2524 start1 = XBUFFER (descbuf)->keymap;
2526 if (!NILP (start1))
2528 describe_map_tree (start1, 1, shadow, prefix,
2529 "\f\nMajor Mode Bindings", nomenu, 0, 0);
2530 shadow = Fcons (start1, shadow);
2533 describe_map_tree (current_global_map, 1, shadow, prefix,
2534 "\f\nGlobal Bindings", nomenu, 0, 1);
2536 /* Print the function-key-map translations under this prefix. */
2537 if (!NILP (Vfunction_key_map))
2538 describe_map_tree (Vfunction_key_map, 0, Qnil, prefix,
2539 "\f\nFunction key map translations", nomenu, 1, 0);
2541 call0 (intern ("help-mode"));
2542 Fset_buffer (descbuf);
2543 UNGCPRO;
2544 return Qnil;
2547 /* Insert a description of the key bindings in STARTMAP,
2548 followed by those of all maps reachable through STARTMAP.
2549 If PARTIAL is nonzero, omit certain "uninteresting" commands
2550 (such as `undefined').
2551 If SHADOW is non-nil, it is a list of maps;
2552 don't mention keys which would be shadowed by any of them.
2553 PREFIX, if non-nil, says mention only keys that start with PREFIX.
2554 TITLE, if not 0, is a string to insert at the beginning.
2555 TITLE should not end with a colon or a newline; we supply that.
2556 If NOMENU is not 0, then omit menu-bar commands.
2558 If TRANSL is nonzero, the definitions are actually key translations
2559 so print strings and vectors differently.
2561 If ALWAYS_TITLE is nonzero, print the title even if there are no maps
2562 to look through. */
2564 void
2565 describe_map_tree (startmap, partial, shadow, prefix, title, nomenu, transl,
2566 always_title)
2567 Lisp_Object startmap, shadow, prefix;
2568 int partial;
2569 char *title;
2570 int nomenu;
2571 int transl;
2572 int always_title;
2574 Lisp_Object maps, orig_maps, seen, sub_shadows;
2575 struct gcpro gcpro1, gcpro2, gcpro3;
2576 int something = 0;
2577 char *key_heading
2578 = "\
2579 key binding\n\
2580 --- -------\n";
2582 orig_maps = maps = Faccessible_keymaps (startmap, prefix);
2583 seen = Qnil;
2584 sub_shadows = Qnil;
2585 GCPRO3 (maps, seen, sub_shadows);
2587 if (nomenu)
2589 Lisp_Object list;
2591 /* Delete from MAPS each element that is for the menu bar. */
2592 for (list = maps; !NILP (list); list = XCDR (list))
2594 Lisp_Object elt, prefix, tem;
2596 elt = Fcar (list);
2597 prefix = Fcar (elt);
2598 if (XVECTOR (prefix)->size >= 1)
2600 tem = Faref (prefix, make_number (0));
2601 if (EQ (tem, Qmenu_bar))
2602 maps = Fdelq (elt, maps);
2607 if (!NILP (maps) || always_title)
2609 if (title)
2611 insert_string (title);
2612 if (!NILP (prefix))
2614 insert_string (" Starting With ");
2615 insert1 (Fkey_description (prefix));
2617 insert_string (":\n");
2619 insert_string (key_heading);
2620 something = 1;
2623 for (; !NILP (maps); maps = Fcdr (maps))
2625 register Lisp_Object elt, prefix, tail;
2627 elt = Fcar (maps);
2628 prefix = Fcar (elt);
2630 sub_shadows = Qnil;
2632 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2634 Lisp_Object shmap;
2636 shmap = XCAR (tail);
2638 /* If the sequence by which we reach this keymap is zero-length,
2639 then the shadow map for this keymap is just SHADOW. */
2640 if ((STRINGP (prefix) && XSTRING (prefix)->size == 0)
2641 || (VECTORP (prefix) && XVECTOR (prefix)->size == 0))
2643 /* If the sequence by which we reach this keymap actually has
2644 some elements, then the sequence's definition in SHADOW is
2645 what we should use. */
2646 else
2648 shmap = Flookup_key (shmap, Fcar (elt), Qt);
2649 if (INTEGERP (shmap))
2650 shmap = Qnil;
2653 /* If shmap is not nil and not a keymap,
2654 it completely shadows this map, so don't
2655 describe this map at all. */
2656 if (!NILP (shmap) && NILP (Fkeymapp (shmap)))
2657 goto skip;
2659 if (!NILP (shmap))
2660 sub_shadows = Fcons (shmap, sub_shadows);
2663 /* Maps we have already listed in this loop shadow this map. */
2664 for (tail = orig_maps; ! EQ (tail, maps); tail = XCDR (tail))
2666 Lisp_Object tem;
2667 tem = Fequal (Fcar (XCAR (tail)), prefix);
2668 if (! NILP (tem))
2669 sub_shadows = Fcons (XCDR (XCAR (tail)), sub_shadows);
2672 describe_map (Fcdr (elt), prefix,
2673 transl ? describe_translation : describe_command,
2674 partial, sub_shadows, &seen, nomenu);
2676 skip: ;
2679 if (something)
2680 insert_string ("\n");
2682 UNGCPRO;
2685 static int previous_description_column;
2687 static void
2688 describe_command (definition)
2689 Lisp_Object definition;
2691 register Lisp_Object tem1;
2692 int column = current_column ();
2693 int description_column;
2695 /* If column 16 is no good, go to col 32;
2696 but don't push beyond that--go to next line instead. */
2697 if (column > 30)
2699 insert_char ('\n');
2700 description_column = 32;
2702 else if (column > 14 || (column > 10 && previous_description_column == 32))
2703 description_column = 32;
2704 else
2705 description_column = 16;
2707 Findent_to (make_number (description_column), make_number (1));
2708 previous_description_column = description_column;
2710 if (SYMBOLP (definition))
2712 XSETSTRING (tem1, XSYMBOL (definition)->name);
2713 insert1 (tem1);
2714 insert_string ("\n");
2716 else if (STRINGP (definition) || VECTORP (definition))
2717 insert_string ("Keyboard Macro\n");
2718 else
2720 tem1 = Fkeymapp (definition);
2721 if (!NILP (tem1))
2722 insert_string ("Prefix Command\n");
2723 else
2724 insert_string ("??\n");
2728 static void
2729 describe_translation (definition)
2730 Lisp_Object definition;
2732 register Lisp_Object tem1;
2734 Findent_to (make_number (16), make_number (1));
2736 if (SYMBOLP (definition))
2738 XSETSTRING (tem1, XSYMBOL (definition)->name);
2739 insert1 (tem1);
2740 insert_string ("\n");
2742 else if (STRINGP (definition) || VECTORP (definition))
2744 insert1 (Fkey_description (definition));
2745 insert_string ("\n");
2747 else
2749 tem1 = Fkeymapp (definition);
2750 if (!NILP (tem1))
2751 insert_string ("Prefix Command\n");
2752 else
2753 insert_string ("??\n");
2757 /* Like Flookup_key, but uses a list of keymaps SHADOW instead of a single map.
2758 Returns the first non-nil binding found in any of those maps. */
2760 static Lisp_Object
2761 shadow_lookup (shadow, key, flag)
2762 Lisp_Object shadow, key, flag;
2764 Lisp_Object tail, value;
2766 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2768 value = Flookup_key (XCAR (tail), key, flag);
2769 if (!NILP (value))
2770 return value;
2772 return Qnil;
2775 /* Describe the contents of map MAP, assuming that this map itself is
2776 reached by the sequence of prefix keys KEYS (a string or vector).
2777 PARTIAL, SHADOW, NOMENU are as in `describe_map_tree' above. */
2779 static void
2780 describe_map (map, keys, elt_describer, partial, shadow, seen, nomenu)
2781 register Lisp_Object map;
2782 Lisp_Object keys;
2783 void (*elt_describer) P_ ((Lisp_Object));
2784 int partial;
2785 Lisp_Object shadow;
2786 Lisp_Object *seen;
2787 int nomenu;
2789 Lisp_Object elt_prefix;
2790 Lisp_Object tail, definition, event;
2791 Lisp_Object tem;
2792 Lisp_Object suppress;
2793 Lisp_Object kludge;
2794 int first = 1;
2795 struct gcpro gcpro1, gcpro2, gcpro3;
2797 if (!NILP (keys) && XFASTINT (Flength (keys)) > 0)
2799 /* Call Fkey_description first, to avoid GC bug for the other string. */
2800 tem = Fkey_description (keys);
2801 elt_prefix = concat2 (tem, build_string (" "));
2803 else
2804 elt_prefix = Qnil;
2806 if (partial)
2807 suppress = intern ("suppress-keymap");
2809 /* This vector gets used to present single keys to Flookup_key. Since
2810 that is done once per keymap element, we don't want to cons up a
2811 fresh vector every time. */
2812 kludge = Fmake_vector (make_number (1), Qnil);
2813 definition = Qnil;
2815 GCPRO3 (elt_prefix, definition, kludge);
2817 for (tail = map; CONSP (tail); tail = XCDR (tail))
2819 QUIT;
2821 if (VECTORP (XCAR (tail))
2822 || CHAR_TABLE_P (XCAR (tail)))
2823 describe_vector (XCAR (tail),
2824 elt_prefix, elt_describer, partial, shadow, map,
2825 (int *)0, 0);
2826 else if (CONSP (XCAR (tail)))
2828 event = XCAR (XCAR (tail));
2830 /* Ignore bindings whose "keys" are not really valid events.
2831 (We get these in the frames and buffers menu.) */
2832 if (! (SYMBOLP (event) || INTEGERP (event)))
2833 continue;
2835 if (nomenu && EQ (event, Qmenu_bar))
2836 continue;
2838 definition = get_keyelt (XCDR (XCAR (tail)), 0);
2840 /* Don't show undefined commands or suppressed commands. */
2841 if (NILP (definition)) continue;
2842 if (SYMBOLP (definition) && partial)
2844 tem = Fget (definition, suppress);
2845 if (!NILP (tem))
2846 continue;
2849 /* Don't show a command that isn't really visible
2850 because a local definition of the same key shadows it. */
2852 XVECTOR (kludge)->contents[0] = event;
2853 if (!NILP (shadow))
2855 tem = shadow_lookup (shadow, kludge, Qt);
2856 if (!NILP (tem)) continue;
2859 tem = Flookup_key (map, kludge, Qt);
2860 if (! EQ (tem, definition)) continue;
2862 if (first)
2864 previous_description_column = 0;
2865 insert ("\n", 1);
2866 first = 0;
2869 if (!NILP (elt_prefix))
2870 insert1 (elt_prefix);
2872 /* THIS gets the string to describe the character EVENT. */
2873 insert1 (Fsingle_key_description (event));
2875 /* Print a description of the definition of this character.
2876 elt_describer will take care of spacing out far enough
2877 for alignment purposes. */
2878 (*elt_describer) (definition);
2880 else if (EQ (XCAR (tail), Qkeymap))
2882 /* The same keymap might be in the structure twice, if we're
2883 using an inherited keymap. So skip anything we've already
2884 encountered. */
2885 tem = Fassq (tail, *seen);
2886 if (CONSP (tem) && !NILP (Fequal (XCAR (tem), keys)))
2887 break;
2888 *seen = Fcons (Fcons (tail, keys), *seen);
2892 UNGCPRO;
2895 static void
2896 describe_vector_princ (elt)
2897 Lisp_Object elt;
2899 Findent_to (make_number (16), make_number (1));
2900 Fprinc (elt, Qnil);
2901 Fterpri (Qnil);
2904 DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 1, 0,
2905 "Insert a description of contents of VECTOR.\n\
2906 This is text showing the elements of vector matched against indices.")
2907 (vector)
2908 Lisp_Object vector;
2910 int count = specpdl_ptr - specpdl;
2912 specbind (Qstandard_output, Fcurrent_buffer ());
2913 CHECK_VECTOR_OR_CHAR_TABLE (vector, 0);
2914 describe_vector (vector, Qnil, describe_vector_princ, 0,
2915 Qnil, Qnil, (int *)0, 0);
2917 return unbind_to (count, Qnil);
2920 /* Insert in the current buffer a description of the contents of VECTOR.
2921 We call ELT_DESCRIBER to insert the description of one value found
2922 in VECTOR.
2924 ELT_PREFIX describes what "comes before" the keys or indices defined
2925 by this vector. This is a human-readable string whose size
2926 is not necessarily related to the situation.
2928 If the vector is in a keymap, ELT_PREFIX is a prefix key which
2929 leads to this keymap.
2931 If the vector is a chartable, ELT_PREFIX is the vector
2932 of bytes that lead to the character set or portion of a character
2933 set described by this chartable.
2935 If PARTIAL is nonzero, it means do not mention suppressed commands
2936 (that assumes the vector is in a keymap).
2938 SHADOW is a list of keymaps that shadow this map.
2939 If it is non-nil, then we look up the key in those maps
2940 and we don't mention it now if it is defined by any of them.
2942 ENTIRE_MAP is the keymap in which this vector appears.
2943 If the definition in effect in the whole map does not match
2944 the one in this vector, we ignore this one.
2946 When describing a sub-char-table, INDICES is a list of
2947 indices at higher levels in this char-table,
2948 and CHAR_TABLE_DEPTH says how many levels down we have gone. */
2950 void
2951 describe_vector (vector, elt_prefix, elt_describer,
2952 partial, shadow, entire_map,
2953 indices, char_table_depth)
2954 register Lisp_Object vector;
2955 Lisp_Object elt_prefix;
2956 void (*elt_describer) P_ ((Lisp_Object));
2957 int partial;
2958 Lisp_Object shadow;
2959 Lisp_Object entire_map;
2960 int *indices;
2961 int char_table_depth;
2963 Lisp_Object definition;
2964 Lisp_Object tem2;
2965 register int i;
2966 Lisp_Object suppress;
2967 Lisp_Object kludge;
2968 int first = 1;
2969 struct gcpro gcpro1, gcpro2, gcpro3;
2970 /* Range of elements to be handled. */
2971 int from, to;
2972 /* A flag to tell if a leaf in this level of char-table is not a
2973 generic character (i.e. a complete multibyte character). */
2974 int complete_char;
2975 int character;
2976 int starting_i;
2978 if (indices == 0)
2979 indices = (int *) alloca (3 * sizeof (int));
2981 definition = Qnil;
2983 /* This vector gets used to present single keys to Flookup_key. Since
2984 that is done once per vector element, we don't want to cons up a
2985 fresh vector every time. */
2986 kludge = Fmake_vector (make_number (1), Qnil);
2987 GCPRO3 (elt_prefix, definition, kludge);
2989 if (partial)
2990 suppress = intern ("suppress-keymap");
2992 if (CHAR_TABLE_P (vector))
2994 if (char_table_depth == 0)
2996 /* VECTOR is a top level char-table. */
2997 complete_char = 1;
2998 from = 0;
2999 to = CHAR_TABLE_ORDINARY_SLOTS;
3001 else
3003 /* VECTOR is a sub char-table. */
3004 if (char_table_depth >= 3)
3005 /* A char-table is never that deep. */
3006 error ("Too deep char table");
3008 complete_char
3009 = (CHARSET_VALID_P (indices[0])
3010 && ((CHARSET_DIMENSION (indices[0]) == 1
3011 && char_table_depth == 1)
3012 || char_table_depth == 2));
3014 /* Meaningful elements are from 32th to 127th. */
3015 from = 32;
3016 to = SUB_CHAR_TABLE_ORDINARY_SLOTS;
3019 else
3021 /* This does the right thing for ordinary vectors. */
3023 complete_char = 1;
3024 from = 0;
3025 to = XVECTOR (vector)->size;
3028 for (i = from; i < to; i++)
3030 QUIT;
3032 if (CHAR_TABLE_P (vector))
3034 if (char_table_depth == 0 && i >= CHAR_TABLE_SINGLE_BYTE_SLOTS)
3035 complete_char = 0;
3037 if (i >= CHAR_TABLE_SINGLE_BYTE_SLOTS
3038 && !CHARSET_DEFINED_P (i - 128))
3039 continue;
3041 definition
3042 = get_keyelt (XCHAR_TABLE (vector)->contents[i], 0);
3044 else
3045 definition = get_keyelt (XVECTOR (vector)->contents[i], 0);
3047 if (NILP (definition)) continue;
3049 /* Don't mention suppressed commands. */
3050 if (SYMBOLP (definition) && partial)
3052 Lisp_Object tem;
3054 tem = Fget (definition, suppress);
3056 if (!NILP (tem)) continue;
3059 /* Set CHARACTER to the character this entry describes, if any.
3060 Also update *INDICES. */
3061 if (CHAR_TABLE_P (vector))
3063 indices[char_table_depth] = i;
3065 if (char_table_depth == 0)
3067 character = i;
3068 indices[0] = i - 128;
3070 else if (complete_char)
3072 character = MAKE_CHAR (indices[0], indices[1], indices[2]);
3074 else
3075 character = 0;
3077 else
3078 character = i;
3080 /* If this binding is shadowed by some other map, ignore it. */
3081 if (!NILP (shadow) && complete_char)
3083 Lisp_Object tem;
3085 XVECTOR (kludge)->contents[0] = make_number (character);
3086 tem = shadow_lookup (shadow, kludge, Qt);
3088 if (!NILP (tem)) continue;
3091 /* Ignore this definition if it is shadowed by an earlier
3092 one in the same keymap. */
3093 if (!NILP (entire_map) && complete_char)
3095 Lisp_Object tem;
3097 XVECTOR (kludge)->contents[0] = make_number (character);
3098 tem = Flookup_key (entire_map, kludge, Qt);
3100 if (! EQ (tem, definition))
3101 continue;
3104 if (first)
3106 if (char_table_depth == 0)
3107 insert ("\n", 1);
3108 first = 0;
3111 /* For a sub char-table, show the depth by indentation.
3112 CHAR_TABLE_DEPTH can be greater than 0 only for a char-table. */
3113 if (char_table_depth > 0)
3114 insert (" ", char_table_depth * 2); /* depth is 1 or 2. */
3116 /* Output the prefix that applies to every entry in this map. */
3117 if (!NILP (elt_prefix))
3118 insert1 (elt_prefix);
3120 /* Insert or describe the character this slot is for,
3121 or a description of what it is for. */
3122 if (SUB_CHAR_TABLE_P (vector))
3124 if (complete_char)
3125 insert_char (character);
3126 else
3128 /* We need an octal representation for this block of
3129 characters. */
3130 char work[16];
3131 sprintf (work, "(row %d)", i);
3132 insert (work, strlen (work));
3135 else if (CHAR_TABLE_P (vector))
3137 if (complete_char)
3138 insert1 (Fsingle_key_description (make_number (character)));
3139 else
3141 /* Print the information for this character set. */
3142 insert_string ("<");
3143 tem2 = CHARSET_TABLE_INFO (i - 128, CHARSET_SHORT_NAME_IDX);
3144 if (STRINGP (tem2))
3145 insert_from_string (tem2, 0, 0, XSTRING (tem2)->size,
3146 STRING_BYTES (XSTRING (tem2)), 0);
3147 else
3148 insert ("?", 1);
3149 insert (">", 1);
3152 else
3154 insert1 (Fsingle_key_description (make_number (character)));
3157 /* If we find a sub char-table within a char-table,
3158 scan it recursively; it defines the details for
3159 a character set or a portion of a character set. */
3160 if (CHAR_TABLE_P (vector) && SUB_CHAR_TABLE_P (definition))
3162 insert ("\n", 1);
3163 describe_vector (definition, elt_prefix, elt_describer,
3164 partial, shadow, entire_map,
3165 indices, char_table_depth + 1);
3166 continue;
3169 starting_i = i;
3171 /* Find all consecutive characters or rows that have the same
3172 definition. But, for elements of a top level char table, if
3173 they are for charsets, we had better describe one by one even
3174 if they have the same definition. */
3175 if (CHAR_TABLE_P (vector))
3177 int limit = to;
3179 if (char_table_depth == 0)
3180 limit = CHAR_TABLE_SINGLE_BYTE_SLOTS;
3182 while (i + 1 < limit
3183 && (tem2 = get_keyelt (XCHAR_TABLE (vector)->contents[i + 1], 0),
3184 !NILP (tem2))
3185 && !NILP (Fequal (tem2, definition)))
3186 i++;
3188 else
3189 while (i + 1 < to
3190 && (tem2 = get_keyelt (XVECTOR (vector)->contents[i + 1], 0),
3191 !NILP (tem2))
3192 && !NILP (Fequal (tem2, definition)))
3193 i++;
3196 /* If we have a range of more than one character,
3197 print where the range reaches to. */
3199 if (i != starting_i)
3201 insert (" .. ", 4);
3203 if (!NILP (elt_prefix))
3204 insert1 (elt_prefix);
3206 if (CHAR_TABLE_P (vector))
3208 if (char_table_depth == 0)
3210 insert1 (Fsingle_key_description (make_number (i)));
3212 else if (complete_char)
3214 indices[char_table_depth] = i;
3215 character = MAKE_CHAR (indices[0], indices[1], indices[2]);
3216 insert_char (character);
3218 else
3220 /* We need an octal representation for this block of
3221 characters. */
3222 char work[16];
3223 sprintf (work, "(row %d)", i);
3224 insert (work, strlen (work));
3227 else
3229 insert1 (Fsingle_key_description (make_number (i)));
3233 /* Print a description of the definition of this character.
3234 elt_describer will take care of spacing out far enough
3235 for alignment purposes. */
3236 (*elt_describer) (definition);
3239 /* For (sub) char-table, print `defalt' slot at last. */
3240 if (CHAR_TABLE_P (vector) && !NILP (XCHAR_TABLE (vector)->defalt))
3242 insert (" ", char_table_depth * 2);
3243 insert_string ("<<default>>");
3244 (*elt_describer) (XCHAR_TABLE (vector)->defalt);
3247 UNGCPRO;
3250 /* Apropos - finding all symbols whose names match a regexp. */
3251 Lisp_Object apropos_predicate;
3252 Lisp_Object apropos_accumulate;
3254 static void
3255 apropos_accum (symbol, string)
3256 Lisp_Object symbol, string;
3258 register Lisp_Object tem;
3260 tem = Fstring_match (string, Fsymbol_name (symbol), Qnil);
3261 if (!NILP (tem) && !NILP (apropos_predicate))
3262 tem = call1 (apropos_predicate, symbol);
3263 if (!NILP (tem))
3264 apropos_accumulate = Fcons (symbol, apropos_accumulate);
3267 DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0,
3268 "Show all symbols whose names contain match for REGEXP.\n\
3269 If optional 2nd arg PREDICATE is non-nil, (funcall PREDICATE SYMBOL) is done\n\
3270 for each symbol and a symbol is mentioned only if that returns non-nil.\n\
3271 Return list of symbols found.")
3272 (regexp, predicate)
3273 Lisp_Object regexp, predicate;
3275 struct gcpro gcpro1, gcpro2;
3276 CHECK_STRING (regexp, 0);
3277 apropos_predicate = predicate;
3278 GCPRO2 (apropos_predicate, apropos_accumulate);
3279 apropos_accumulate = Qnil;
3280 map_obarray (Vobarray, apropos_accum, regexp);
3281 apropos_accumulate = Fsort (apropos_accumulate, Qstring_lessp);
3282 UNGCPRO;
3283 return apropos_accumulate;
3286 void
3287 syms_of_keymap ()
3289 Qkeymap = intern ("keymap");
3290 staticpro (&Qkeymap);
3292 /* Now we are ready to set up this property, so we can
3293 create char tables. */
3294 Fput (Qkeymap, Qchar_table_extra_slots, make_number (0));
3296 /* Initialize the keymaps standardly used.
3297 Each one is the value of a Lisp variable, and is also
3298 pointed to by a C variable */
3300 global_map = Fmake_keymap (Qnil);
3301 Fset (intern ("global-map"), global_map);
3303 current_global_map = global_map;
3304 staticpro (&global_map);
3305 staticpro (&current_global_map);
3307 meta_map = Fmake_keymap (Qnil);
3308 Fset (intern ("esc-map"), meta_map);
3309 Ffset (intern ("ESC-prefix"), meta_map);
3311 control_x_map = Fmake_keymap (Qnil);
3312 Fset (intern ("ctl-x-map"), control_x_map);
3313 Ffset (intern ("Control-X-prefix"), control_x_map);
3315 DEFVAR_LISP ("define-key-rebound-commands", &Vdefine_key_rebound_commands,
3316 "List of commands given new key bindings recently.\n\
3317 This is used for internal purposes during Emacs startup;\n\
3318 don't alter it yourself.");
3319 Vdefine_key_rebound_commands = Qt;
3321 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map,
3322 "Default keymap to use when reading from the minibuffer.");
3323 Vminibuffer_local_map = Fmake_sparse_keymap (Qnil);
3325 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map,
3326 "Local keymap for the minibuffer when spaces are not allowed.");
3327 Vminibuffer_local_ns_map = Fmake_sparse_keymap (Qnil);
3329 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map,
3330 "Local keymap for minibuffer input with completion.");
3331 Vminibuffer_local_completion_map = Fmake_sparse_keymap (Qnil);
3333 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map,
3334 "Local keymap for minibuffer input with completion, for exact match.");
3335 Vminibuffer_local_must_match_map = Fmake_sparse_keymap (Qnil);
3337 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist,
3338 "Alist of keymaps to use for minor modes.\n\
3339 Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read\n\
3340 key sequences and look up bindings iff VARIABLE's value is non-nil.\n\
3341 If two active keymaps bind the same key, the keymap appearing earlier\n\
3342 in the list takes precedence.");
3343 Vminor_mode_map_alist = Qnil;
3345 DEFVAR_LISP ("minor-mode-overriding-map-alist", &Vminor_mode_overriding_map_alist,
3346 "Alist of keymaps to use for minor modes, in current major mode.\n\
3347 This variable is a alist just like `minor-mode-map-alist', and it is\n\
3348 used the same way (and before `minor-mode-map-alist'); however,\n\
3349 it is provided for major modes to bind locally.");
3350 Vminor_mode_overriding_map_alist = Qnil;
3352 DEFVAR_LISP ("function-key-map", &Vfunction_key_map,
3353 "Keymap mapping ASCII function key sequences onto their preferred forms.\n\
3354 This allows Emacs to recognize function keys sent from ASCII\n\
3355 terminals at any point in a key sequence.\n\
3357 The `read-key-sequence' function replaces any subsequence bound by\n\
3358 `function-key-map' with its binding. More precisely, when the active\n\
3359 keymaps have no binding for the current key sequence but\n\
3360 `function-key-map' binds a suffix of the sequence to a vector or string,\n\
3361 `read-key-sequence' replaces the matching suffix with its binding, and\n\
3362 continues with the new sequence.\n\
3364 The events that come from bindings in `function-key-map' are not\n\
3365 themselves looked up in `function-key-map'.\n\
3367 For example, suppose `function-key-map' binds `ESC O P' to [f1].\n\
3368 Typing `ESC O P' to `read-key-sequence' would return [f1]. Typing\n\
3369 `C-x ESC O P' would return [?\\C-x f1]. If [f1] were a prefix\n\
3370 key, typing `ESC O P x' would return [f1 x].");
3371 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
3373 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
3374 "Keymap of key translations that can override keymaps.\n\
3375 This keymap works like `function-key-map', but comes after that,\n\
3376 and applies even for keys that have ordinary bindings.");
3377 Vkey_translation_map = Qnil;
3379 Qsingle_key_description = intern ("single-key-description");
3380 staticpro (&Qsingle_key_description);
3382 Qkey_description = intern ("key-description");
3383 staticpro (&Qkey_description);
3385 Qkeymapp = intern ("keymapp");
3386 staticpro (&Qkeymapp);
3388 Qnon_ascii = intern ("non-ascii");
3389 staticpro (&Qnon_ascii);
3391 Qmenu_item = intern ("menu-item");
3392 staticpro (&Qmenu_item);
3394 defsubr (&Skeymapp);
3395 defsubr (&Skeymap_parent);
3396 defsubr (&Sset_keymap_parent);
3397 defsubr (&Smake_keymap);
3398 defsubr (&Smake_sparse_keymap);
3399 defsubr (&Scopy_keymap);
3400 defsubr (&Skey_binding);
3401 defsubr (&Slocal_key_binding);
3402 defsubr (&Sglobal_key_binding);
3403 defsubr (&Sminor_mode_key_binding);
3404 defsubr (&Sdefine_key);
3405 defsubr (&Slookup_key);
3406 defsubr (&Sdefine_prefix_command);
3407 defsubr (&Suse_global_map);
3408 defsubr (&Suse_local_map);
3409 defsubr (&Scurrent_local_map);
3410 defsubr (&Scurrent_global_map);
3411 defsubr (&Scurrent_minor_mode_maps);
3412 defsubr (&Saccessible_keymaps);
3413 defsubr (&Skey_description);
3414 defsubr (&Sdescribe_vector);
3415 defsubr (&Ssingle_key_description);
3416 defsubr (&Stext_char_description);
3417 defsubr (&Swhere_is_internal);
3418 defsubr (&Sdescribe_bindings_internal);
3419 defsubr (&Sapropos_internal);
3422 void
3423 keys_of_keymap ()
3425 initial_define_key (global_map, 033, "ESC-prefix");
3426 initial_define_key (global_map, Ctl('X'), "Control-X-prefix");