(Fwhere_is_internal): Ignore `menu-bar' and `tool-bar'.
[emacs.git] / src / keymap.c
blob8921f53689b84baeb5a22880471b3dea6499055d
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 #include "lisp.h"
25 #include "commands.h"
26 #include "buffer.h"
27 #include "charset.h"
28 #include "keyboard.h"
29 #include "termhooks.h"
30 #include "blockinput.h"
31 #include "puresize.h"
32 #include "intervals.h"
34 #define min(a, b) ((a) < (b) ? (a) : (b))
35 #define KEYMAPP(m) (!NILP (Fkeymapp (m)))
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 store_in_keymap P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
103 static void fix_submap_inheritance P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
105 static Lisp_Object define_as_prefix P_ ((Lisp_Object, Lisp_Object));
106 static Lisp_Object describe_buffer_bindings P_ ((Lisp_Object));
107 static void describe_command P_ ((Lisp_Object));
108 static void describe_translation P_ ((Lisp_Object));
109 static void describe_map P_ ((Lisp_Object, Lisp_Object,
110 void (*) P_ ((Lisp_Object)),
111 int, Lisp_Object, Lisp_Object*, int));
113 /* Keymap object support - constructors and predicates. */
115 DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 1, 0,
116 "Construct and return a new keymap, of the form (keymap CHARTABLE . ALIST).\n\
117 CHARTABLE is a char-table that holds the bindings for the ASCII\n\
118 characters. ALIST is an assoc-list which holds bindings for function keys,\n\
119 mouse events, and any other things that appear in the input stream.\n\
120 All entries in it are initially nil, meaning \"command undefined\".\n\n\
121 The optional arg STRING supplies a menu name for the keymap\n\
122 in case you use it as a menu with `x-popup-menu'.")
123 (string)
124 Lisp_Object string;
126 Lisp_Object tail;
127 if (!NILP (string))
128 tail = Fcons (string, Qnil);
129 else
130 tail = Qnil;
131 return Fcons (Qkeymap,
132 Fcons (Fmake_char_table (Qkeymap, Qnil), tail));
135 DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 1, 0,
136 "Construct and return a new sparse-keymap list.\n\
137 Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),\n\
138 which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),\n\
139 which binds the function key or mouse event SYMBOL to DEFINITION.\n\
140 Initially the alist is nil.\n\n\
141 The optional arg STRING supplies a menu name for the keymap\n\
142 in case you use it as a menu with `x-popup-menu'.")
143 (string)
144 Lisp_Object string;
146 if (!NILP (string))
147 return Fcons (Qkeymap, Fcons (string, Qnil));
148 return Fcons (Qkeymap, Qnil);
151 /* This function is used for installing the standard key bindings
152 at initialization time.
154 For example:
156 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); */
158 void
159 initial_define_key (keymap, key, defname)
160 Lisp_Object keymap;
161 int key;
162 char *defname;
164 store_in_keymap (keymap, make_number (key), intern (defname));
167 void
168 initial_define_lispy_key (keymap, keyname, defname)
169 Lisp_Object keymap;
170 char *keyname;
171 char *defname;
173 store_in_keymap (keymap, intern (keyname), intern (defname));
176 /* Define character fromchar in map frommap as an alias for character
177 tochar in map tomap. Subsequent redefinitions of the latter WILL
178 affect the former. */
180 #if 0
181 void
182 synkey (frommap, fromchar, tomap, tochar)
183 struct Lisp_Vector *frommap, *tomap;
184 int fromchar, tochar;
186 Lisp_Object v, c;
187 XSETVECTOR (v, tomap);
188 XSETFASTINT (c, tochar);
189 frommap->contents[fromchar] = Fcons (v, c);
191 #endif /* 0 */
193 DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0,
194 "Return t if OBJECT is a keymap.\n\
196 A keymap is a list (keymap . ALIST),\n\
197 or a symbol whose function definition is itself a keymap.\n\
198 ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);\n\
199 a vector of densely packed bindings for small character codes\n\
200 is also allowed as an element.")
201 (object)
202 Lisp_Object object;
204 /* FIXME: Maybe this should return t for autoloaded keymaps? -sm */
205 return (NILP (get_keymap_1 (object, 0, 0)) ? Qnil : Qt);
208 /* Check that OBJECT is a keymap (after dereferencing through any
209 symbols). If it is, return it.
211 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
212 is an autoload form, do the autoload and try again.
213 If AUTOLOAD is nonzero, callers must assume GC is possible.
215 ERROR controls how we respond if OBJECT isn't a keymap.
216 If ERROR is non-zero, signal an error; otherwise, just return Qnil.
218 Note that most of the time, we don't want to pursue autoloads.
219 Functions like Faccessible_keymaps which scan entire keymap trees
220 shouldn't load every autoloaded keymap. I'm not sure about this,
221 but it seems to me that only read_key_sequence, Flookup_key, and
222 Fdefine_key should cause keymaps to be autoloaded.
224 This function can GC when AUTOLOAD is non-zero, because it calls
225 do_autoload which can GC. */
227 Lisp_Object
228 get_keymap_1 (object, error, autoload)
229 Lisp_Object object;
230 int error, autoload;
232 Lisp_Object tem;
234 autoload_retry:
235 if (NILP (object))
236 goto end;
237 if (CONSP (object) && EQ (XCAR (object), Qkeymap))
238 return object;
239 else
241 tem = indirect_function (object);
242 if (CONSP (tem) && EQ (XCAR (tem), Qkeymap))
243 return tem;
246 /* Should we do an autoload? Autoload forms for keymaps have
247 Qkeymap as their fifth element. */
248 if (autoload
249 && SYMBOLP (object)
250 && CONSP (tem)
251 && EQ (XCAR (tem), Qautoload))
253 Lisp_Object tail;
255 tail = Fnth (make_number (4), tem);
256 if (EQ (tail, Qkeymap))
258 struct gcpro gcpro1, gcpro2;
260 GCPRO2 (tem, object);
261 do_autoload (tem, object);
262 UNGCPRO;
264 goto autoload_retry;
268 end:
269 if (error)
270 wrong_type_argument (Qkeymapp, object);
271 return Qnil;
275 /* Follow any symbol chaining, and return the keymap denoted by OBJECT.
276 If OBJECT doesn't denote a keymap at all, signal an error. */
277 Lisp_Object
278 get_keymap (object)
279 Lisp_Object object;
281 return get_keymap_1 (object, 1, 0);
284 /* Return the parent map of the keymap MAP, or nil if it has none.
285 We assume that MAP is a valid keymap. */
287 DEFUN ("keymap-parent", Fkeymap_parent, Skeymap_parent, 1, 1, 0,
288 "Return the parent keymap of KEYMAP.")
289 (keymap)
290 Lisp_Object keymap;
292 Lisp_Object list;
294 keymap = get_keymap_1 (keymap, 1, 1);
296 /* Skip past the initial element `keymap'. */
297 list = XCDR (keymap);
298 for (; CONSP (list); list = XCDR (list))
300 /* See if there is another `keymap'. */
301 if (KEYMAPP (list))
302 return list;
305 return Qnil;
309 /* Check whether MAP is one of MAPS parents. */
311 keymap_memberp (map, maps)
312 Lisp_Object map, maps;
314 while (KEYMAPP (maps) && !EQ (map, maps))
315 maps = Fkeymap_parent (maps);
316 return (EQ (map, maps));
319 /* Set the parent keymap of MAP to PARENT. */
321 DEFUN ("set-keymap-parent", Fset_keymap_parent, Sset_keymap_parent, 2, 2, 0,
322 "Modify KEYMAP to set its parent map to PARENT.\n\
323 PARENT should be nil or another keymap.")
324 (keymap, parent)
325 Lisp_Object keymap, parent;
327 Lisp_Object list, prev;
328 struct gcpro gcpro1;
329 int i;
331 keymap = get_keymap_1 (keymap, 1, 1);
332 GCPRO1 (keymap);
334 if (!NILP (parent))
336 parent = get_keymap_1 (parent, 1, 1);
338 /* Check for cycles. */
339 if (keymap_memberp (keymap, parent))
340 error ("Cyclic keymap inheritance");
343 /* Skip past the initial element `keymap'. */
344 prev = keymap;
345 while (1)
347 list = XCDR (prev);
348 /* If there is a parent keymap here, replace it.
349 If we came to the end, add the parent in PREV. */
350 if (! CONSP (list) || KEYMAPP (list))
352 /* If we already have the right parent, return now
353 so that we avoid the loops below. */
354 if (EQ (XCDR (prev), parent))
355 RETURN_UNGCPRO (parent);
357 XCDR (prev) = parent;
358 break;
360 prev = list;
363 /* Scan through for submaps, and set their parents too. */
365 for (list = XCDR (keymap); CONSP (list); list = XCDR (list))
367 /* Stop the scan when we come to the parent. */
368 if (EQ (XCAR (list), Qkeymap))
369 break;
371 /* If this element holds a prefix map, deal with it. */
372 if (CONSP (XCAR (list))
373 && CONSP (XCDR (XCAR (list))))
374 fix_submap_inheritance (keymap, XCAR (XCAR (list)),
375 XCDR (XCAR (list)));
377 if (VECTORP (XCAR (list)))
378 for (i = 0; i < XVECTOR (XCAR (list))->size; i++)
379 if (CONSP (XVECTOR (XCAR (list))->contents[i]))
380 fix_submap_inheritance (keymap, make_number (i),
381 XVECTOR (XCAR (list))->contents[i]);
383 if (CHAR_TABLE_P (XCAR (list)))
385 Lisp_Object indices[3];
387 map_char_table (fix_submap_inheritance, Qnil, XCAR (list),
388 keymap, 0, indices);
392 RETURN_UNGCPRO (parent);
395 /* EVENT is defined in MAP as a prefix, and SUBMAP is its definition.
396 if EVENT is also a prefix in MAP's parent,
397 make sure that SUBMAP inherits that definition as its own parent. */
399 static void
400 fix_submap_inheritance (map, event, submap)
401 Lisp_Object map, event, submap;
403 Lisp_Object map_parent, parent_entry;
405 /* SUBMAP is a cons that we found as a key binding.
406 Discard the other things found in a menu key binding. */
408 submap = get_keymap_1 (get_keyelt (submap, 0), 0, 0);
410 /* If it isn't a keymap now, there's no work to do. */
411 if (NILP (submap))
412 return;
414 map_parent = Fkeymap_parent (map);
415 if (! NILP (map_parent))
416 parent_entry = get_keyelt (access_keymap (map_parent, event, 0, 0), 0);
417 else
418 parent_entry = Qnil;
420 /* If MAP's parent has something other than a keymap,
421 our own submap shadows it completely, so use nil as SUBMAP's parent. */
422 if (! KEYMAPP (parent_entry))
423 parent_entry = Qnil;
425 if (! EQ (parent_entry, submap))
427 Lisp_Object submap_parent;
428 submap_parent = submap;
429 while (1)
431 Lisp_Object tem;
432 tem = Fkeymap_parent (submap_parent);
433 if (keymap_memberp (tem, parent_entry))
434 /* Fset_keymap_parent could create a cycle. */
435 return;
436 if (KEYMAPP (tem))
437 submap_parent = tem;
438 else
439 break;
441 Fset_keymap_parent (submap_parent, parent_entry);
445 /* Look up IDX in MAP. IDX may be any sort of event.
446 Note that this does only one level of lookup; IDX must be a single
447 event, not a sequence.
449 If T_OK is non-zero, bindings for Qt are treated as default
450 bindings; any key left unmentioned by other tables and bindings is
451 given the binding of Qt.
453 If T_OK is zero, bindings for Qt are not treated specially.
455 If NOINHERIT, don't accept a subkeymap found in an inherited keymap. */
457 Lisp_Object
458 access_keymap (map, idx, t_ok, noinherit)
459 Lisp_Object map;
460 Lisp_Object idx;
461 int t_ok;
462 int noinherit;
464 int noprefix = 0;
465 Lisp_Object val;
467 /* If idx is a list (some sort of mouse click, perhaps?),
468 the index we want to use is the car of the list, which
469 ought to be a symbol. */
470 idx = EVENT_HEAD (idx);
472 /* If idx is a symbol, it might have modifiers, which need to
473 be put in the canonical order. */
474 if (SYMBOLP (idx))
475 idx = reorder_modifiers (idx);
476 else if (INTEGERP (idx))
477 /* Clobber the high bits that can be present on a machine
478 with more than 24 bits of integer. */
479 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
482 Lisp_Object tail;
483 Lisp_Object t_binding;
485 t_binding = Qnil;
486 for (tail = map; CONSP (tail); tail = XCDR (tail))
488 Lisp_Object binding;
490 binding = XCAR (tail);
491 if (SYMBOLP (binding))
493 /* If NOINHERIT, stop finding prefix definitions
494 after we pass a second occurrence of the `keymap' symbol. */
495 if (noinherit && EQ (binding, Qkeymap) && ! EQ (tail, map))
496 noprefix = 1;
498 else if (CONSP (binding))
500 if (EQ (XCAR (binding), idx))
502 val = XCDR (binding);
503 if (noprefix && KEYMAPP (val))
504 return Qnil;
505 if (CONSP (val))
506 fix_submap_inheritance (map, idx, val);
507 return val;
509 if (t_ok && EQ (XCAR (binding), Qt))
510 t_binding = XCDR (binding);
512 else if (VECTORP (binding))
514 if (NATNUMP (idx) && XFASTINT (idx) < XVECTOR (binding)->size)
516 val = XVECTOR (binding)->contents[XFASTINT (idx)];
517 if (noprefix && KEYMAPP (val))
518 return Qnil;
519 if (CONSP (val))
520 fix_submap_inheritance (map, idx, val);
521 return val;
524 else if (CHAR_TABLE_P (binding))
526 /* Character codes with modifiers
527 are not included in a char-table.
528 All character codes without modifiers are included. */
529 if (NATNUMP (idx)
530 && ! (XFASTINT (idx)
531 & (CHAR_ALT | CHAR_SUPER | CHAR_HYPER
532 | CHAR_SHIFT | CHAR_CTL | CHAR_META)))
534 val = Faref (binding, idx);
535 if (noprefix && KEYMAPP (val))
536 return Qnil;
537 if (CONSP (val))
538 fix_submap_inheritance (map, idx, val);
539 return val;
543 QUIT;
546 return t_binding;
550 /* Given OBJECT which was found in a slot in a keymap,
551 trace indirect definitions to get the actual definition of that slot.
552 An indirect definition is a list of the form
553 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
554 and INDEX is the object to look up in KEYMAP to yield the definition.
556 Also if OBJECT has a menu string as the first element,
557 remove that. Also remove a menu help string as second element.
559 If AUTOLOAD is nonzero, load autoloadable keymaps
560 that are referred to with indirection. */
562 Lisp_Object
563 get_keyelt (object, autoload)
564 register Lisp_Object object;
565 int autoload;
567 while (1)
569 if (!(CONSP (object)))
570 /* This is really the value. */
571 return object;
573 /* If the keymap contents looks like (keymap ...) or (lambda ...)
574 then use itself. */
575 else if (EQ (XCAR (object), Qkeymap) || EQ (XCAR (object), Qlambda))
576 return object;
578 /* If the keymap contents looks like (menu-item name . DEFN)
579 or (menu-item name DEFN ...) then use DEFN.
580 This is a new format menu item. */
581 else if (EQ (XCAR (object), Qmenu_item))
583 if (CONSP (XCDR (object)))
585 Lisp_Object tem;
587 object = XCDR (XCDR (object));
588 tem = object;
589 if (CONSP (object))
590 object = XCAR (object);
592 /* If there's a `:filter FILTER', apply FILTER to the
593 menu-item's definition to get the real definition to
594 use. Temporarily inhibit GC while evaluating FILTER,
595 because not functions calling get_keyelt are prepared
596 for a GC. */
597 for (; CONSP (tem) && CONSP (XCDR (tem)); tem = XCDR (tem))
598 if (EQ (XCAR (tem), QCfilter))
600 int count = inhibit_garbage_collection ();
601 Lisp_Object filter;
602 filter = XCAR (XCDR (tem));
603 filter = list2 (filter, list2 (Qquote, object));
604 object = menu_item_eval_property (filter);
605 unbind_to (count, Qnil);
606 break;
609 else
610 /* Invalid keymap */
611 return object;
614 /* If the keymap contents looks like (STRING . DEFN), use DEFN.
615 Keymap alist elements like (CHAR MENUSTRING . DEFN)
616 will be used by HierarKey menus. */
617 else if (STRINGP (XCAR (object)))
619 object = XCDR (object);
620 /* Also remove a menu help string, if any,
621 following the menu item name. */
622 if (CONSP (object) && STRINGP (XCAR (object)))
623 object = XCDR (object);
624 /* Also remove the sublist that caches key equivalences, if any. */
625 if (CONSP (object) && CONSP (XCAR (object)))
627 Lisp_Object carcar;
628 carcar = XCAR (XCAR (object));
629 if (NILP (carcar) || VECTORP (carcar))
630 object = XCDR (object);
634 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
635 else
637 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 static 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 *) xmalloc (cmm_size * sizeof (Lisp_Object));
1227 newmaps
1228 = (Lisp_Object *) xmalloc (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 = 0;
1752 int i, i_byte;
1753 Lisp_Object sep;
1754 Lisp_Object *args = NULL;
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 Qnil);
1788 args[i * 2 + 1] = sep;
1791 else if (CONSP (keys))
1793 /* In effect, this computes
1794 (mapconcat 'single-key-description keys " ")
1795 but we shouldn't use mapconcat because it can do GC. */
1797 len = XFASTINT (Flength (keys));
1798 sep = build_string (" ");
1799 /* This has one extra element at the end that we don't pass to Fconcat. */
1800 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
1802 for (i = 0; i < len; i++)
1804 args[i * 2] = Fsingle_key_description (XCAR (keys), Qnil);
1805 args[i * 2 + 1] = sep;
1806 keys = XCDR (keys);
1809 else
1810 keys = wrong_type_argument (Qarrayp, keys);
1812 return Fconcat (len * 2 - 1, args);
1815 char *
1816 push_key_description (c, p)
1817 register unsigned int c;
1818 register char *p;
1820 unsigned c2;
1822 /* Clear all the meaningless bits above the meta bit. */
1823 c &= meta_modifier | ~ - meta_modifier;
1824 c2 = c & ~(alt_modifier | ctrl_modifier | hyper_modifier
1825 | meta_modifier | shift_modifier | super_modifier);
1827 if (c & alt_modifier)
1829 *p++ = 'A';
1830 *p++ = '-';
1831 c -= alt_modifier;
1833 if ((c & ctrl_modifier) != 0
1834 || (c2 < ' ' && c2 != 27 && c2 != '\t' && c2 != Ctl ('M')))
1836 *p++ = 'C';
1837 *p++ = '-';
1838 c &= ~ctrl_modifier;
1840 if (c & hyper_modifier)
1842 *p++ = 'H';
1843 *p++ = '-';
1844 c -= hyper_modifier;
1846 if (c & meta_modifier)
1848 *p++ = 'M';
1849 *p++ = '-';
1850 c -= meta_modifier;
1852 if (c & shift_modifier)
1854 *p++ = 'S';
1855 *p++ = '-';
1856 c -= shift_modifier;
1858 if (c & super_modifier)
1860 *p++ = 's';
1861 *p++ = '-';
1862 c -= super_modifier;
1864 if (c < 040)
1866 if (c == 033)
1868 *p++ = 'E';
1869 *p++ = 'S';
1870 *p++ = 'C';
1872 else if (c == '\t')
1874 *p++ = 'T';
1875 *p++ = 'A';
1876 *p++ = 'B';
1878 else if (c == Ctl ('M'))
1880 *p++ = 'R';
1881 *p++ = 'E';
1882 *p++ = 'T';
1884 else
1886 /* `C-' already added above. */
1887 if (c > 0 && c <= Ctl ('Z'))
1888 *p++ = c + 0140;
1889 else
1890 *p++ = c + 0100;
1893 else if (c == 0177)
1895 *p++ = 'D';
1896 *p++ = 'E';
1897 *p++ = 'L';
1899 else if (c == ' ')
1901 *p++ = 'S';
1902 *p++ = 'P';
1903 *p++ = 'C';
1905 else if (c < 128
1906 || (NILP (current_buffer->enable_multibyte_characters)
1907 && SINGLE_BYTE_CHAR_P (c)))
1908 *p++ = c;
1909 else
1911 if (! NILP (current_buffer->enable_multibyte_characters))
1912 c = unibyte_char_to_multibyte (c);
1914 if (NILP (current_buffer->enable_multibyte_characters)
1915 || SINGLE_BYTE_CHAR_P (c)
1916 || ! char_valid_p (c, 0))
1918 int bit_offset;
1919 *p++ = '\\';
1920 /* The biggest character code uses 19 bits. */
1921 for (bit_offset = 18; bit_offset >= 0; bit_offset -= 3)
1923 if (c >= (1 << bit_offset))
1924 *p++ = ((c & (7 << bit_offset)) >> bit_offset) + '0';
1927 else
1929 p += CHAR_STRING (c, p);
1933 return p;
1936 /* This function cannot GC. */
1938 DEFUN ("single-key-description", Fsingle_key_description,
1939 Ssingle_key_description, 1, 2, 0,
1940 "Return a pretty description of command character KEY.\n\
1941 Control characters turn into C-whatever, etc.\n\
1942 Optional argument NO-ANGLES non-nil means don't put angle brackets\n\
1943 around function keys and event symbols.")
1944 (key, no_angles)
1945 Lisp_Object key, no_angles;
1947 if (CONSP (key) && lucid_event_type_list_p (key))
1948 key = Fevent_convert_list (key);
1950 key = EVENT_HEAD (key);
1952 if (INTEGERP (key)) /* Normal character */
1954 unsigned int charset, c1, c2;
1955 int without_bits = XINT (key) & ~((-1) << CHARACTERBITS);
1957 if (SINGLE_BYTE_CHAR_P (without_bits))
1958 charset = 0;
1959 else
1960 SPLIT_CHAR (without_bits, charset, c1, c2);
1962 if (charset
1963 && CHARSET_DEFINED_P (charset)
1964 && ((c1 >= 0 && c1 < 32)
1965 || (c2 >= 0 && c2 < 32)))
1967 /* Handle a generic character. */
1968 Lisp_Object name;
1969 name = CHARSET_TABLE_INFO (charset, CHARSET_LONG_NAME_IDX);
1970 CHECK_STRING (name, 0);
1971 return concat2 (build_string ("Character set "), name);
1973 else
1975 char tem[KEY_DESCRIPTION_SIZE];
1977 *push_key_description (XUINT (key), tem) = 0;
1978 return build_string (tem);
1981 else if (SYMBOLP (key)) /* Function key or event-symbol */
1983 if (NILP (no_angles))
1985 char *buffer
1986 = (char *) alloca (STRING_BYTES (XSYMBOL (key)->name) + 5);
1987 sprintf (buffer, "<%s>", XSYMBOL (key)->name->data);
1988 return build_string (buffer);
1990 else
1991 return Fsymbol_name (key);
1993 else if (STRINGP (key)) /* Buffer names in the menubar. */
1994 return Fcopy_sequence (key);
1995 else
1996 error ("KEY must be an integer, cons, symbol, or string");
1997 return Qnil;
2000 char *
2001 push_text_char_description (c, p)
2002 register unsigned int c;
2003 register char *p;
2005 if (c >= 0200)
2007 *p++ = 'M';
2008 *p++ = '-';
2009 c -= 0200;
2011 if (c < 040)
2013 *p++ = '^';
2014 *p++ = c + 64; /* 'A' - 1 */
2016 else if (c == 0177)
2018 *p++ = '^';
2019 *p++ = '?';
2021 else
2022 *p++ = c;
2023 return p;
2026 /* This function cannot GC. */
2028 DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0,
2029 "Return a pretty description of file-character CHARACTER.\n\
2030 Control characters turn into \"^char\", etc.")
2031 (character)
2032 Lisp_Object character;
2034 /* Currently MAX_MULTIBYTE_LENGTH is 4 (< 6). */
2035 unsigned char str[6];
2036 int c;
2038 CHECK_NUMBER (character, 0);
2040 c = XINT (character);
2041 if (!SINGLE_BYTE_CHAR_P (c))
2043 int len = CHAR_STRING (c, str);
2045 return make_multibyte_string (str, 1, len);
2048 *push_text_char_description (c & 0377, str) = 0;
2050 return build_string (str);
2053 /* Return non-zero if SEQ contains only ASCII characters, perhaps with
2054 a meta bit. */
2055 static int
2056 ascii_sequence_p (seq)
2057 Lisp_Object seq;
2059 int i;
2060 int len = XINT (Flength (seq));
2062 for (i = 0; i < len; i++)
2064 Lisp_Object ii, elt;
2066 XSETFASTINT (ii, i);
2067 elt = Faref (seq, ii);
2069 if (!INTEGERP (elt)
2070 || (XUINT (elt) & ~CHAR_META) >= 0x80)
2071 return 0;
2074 return 1;
2078 /* where-is - finding a command in a set of keymaps. */
2080 static Lisp_Object where_is_internal_1 ();
2081 static void where_is_internal_2 ();
2083 /* This function can GC if Flookup_key autoloads any keymaps. */
2085 DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 4, 0,
2086 "Return list of keys that invoke DEFINITION.\n\
2087 If KEYMAP is non-nil, search only KEYMAP and the global keymap.\n\
2088 If KEYMAP is nil, search all the currently active keymaps.\n\
2090 If optional 3rd arg FIRSTONLY is non-nil, return the first key sequence found,\n\
2091 rather than a list of all possible key sequences.\n\
2092 If FIRSTONLY is the symbol `non-ascii', return the first binding found,\n\
2093 no matter what it is.\n\
2094 If FIRSTONLY has another non-nil value, prefer sequences of ASCII characters,\n\
2095 and entirely reject menu bindings.\n\
2097 If optional 4th arg NOINDIRECT is non-nil, don't follow indirections\n\
2098 to other keymaps or slots. This makes it possible to search for an\n\
2099 indirect definition itself.")
2100 (definition, xkeymap, firstonly, noindirect)
2101 Lisp_Object definition, xkeymap;
2102 Lisp_Object firstonly, noindirect;
2104 Lisp_Object maps;
2105 Lisp_Object found, sequences;
2106 Lisp_Object keymap1;
2107 int keymap_specified = !NILP (xkeymap);
2108 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
2109 /* 1 means ignore all menu bindings entirely. */
2110 int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii);
2112 /* Find keymaps accessible from `keymap' or the current
2113 context. But don't muck with the value of `keymap',
2114 because `where_is_internal_1' uses it to check for
2115 shadowed bindings. */
2116 keymap1 = xkeymap;
2117 if (! keymap_specified)
2118 keymap1 = get_local_map (PT, current_buffer, keymap);
2120 if (!NILP (keymap1))
2121 maps = nconc2 (Faccessible_keymaps (get_keymap (keymap1), Qnil),
2122 Faccessible_keymaps (get_keymap (current_global_map),
2123 Qnil));
2124 else
2126 keymap1 = xkeymap;
2127 if (! keymap_specified)
2128 keymap1 = get_local_map (PT, current_buffer, local_map);
2130 if (!NILP (keymap1))
2131 maps = nconc2 (Faccessible_keymaps (get_keymap (keymap1), Qnil),
2132 Faccessible_keymaps (get_keymap (current_global_map),
2133 Qnil));
2134 else
2135 maps = Faccessible_keymaps (get_keymap (current_global_map), Qnil);
2138 /* Put the minor mode keymaps on the front. */
2139 if (! keymap_specified)
2141 Lisp_Object minors;
2142 minors = Fnreverse (Fcurrent_minor_mode_maps ());
2143 while (!NILP (minors))
2145 maps = nconc2 (Faccessible_keymaps (get_keymap (XCAR (minors)),
2146 Qnil),
2147 maps);
2148 minors = XCDR (minors);
2152 GCPRO5 (definition, xkeymap, maps, found, sequences);
2153 found = Qnil;
2154 sequences = Qnil;
2156 for (; !NILP (maps); maps = Fcdr (maps))
2158 /* Key sequence to reach map, and the map that it reaches */
2159 register Lisp_Object this, map;
2161 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
2162 [M-CHAR] sequences, check if last character of the sequence
2163 is the meta-prefix char. */
2164 Lisp_Object last;
2165 int last_is_meta;
2167 this = Fcar (Fcar (maps));
2168 map = Fcdr (Fcar (maps));
2169 last = make_number (XINT (Flength (this)) - 1);
2170 last_is_meta = (XINT (last) >= 0
2171 && EQ (Faref (this, last), meta_prefix_char));
2173 if (nomenus && XINT (last) >= 0)
2174 { /* If no menu entries should be returned, skip over the
2175 keymaps bound to `menu-bar' and `tool-bar'. */
2176 Lisp_Object tem = Faref (this, 0);
2177 if (EQ (tem, Qmenu_bar) || EQ (tem, Qtool_bar))
2178 continue;
2181 QUIT;
2183 while (CONSP (map))
2185 /* Because the code we want to run on each binding is rather
2186 large, we don't want to have two separate loop bodies for
2187 sparse keymap bindings and tables; we want to iterate one
2188 loop body over both keymap and vector bindings.
2190 For this reason, if Fcar (map) is a vector, we don't
2191 advance map to the next element until i indicates that we
2192 have finished off the vector. */
2193 Lisp_Object elt, key, binding;
2194 elt = XCAR (map);
2195 map = XCDR (map);
2197 sequences = Qnil;
2199 QUIT;
2201 /* Set key and binding to the current key and binding, and
2202 advance map and i to the next binding. */
2203 if (VECTORP (elt))
2205 Lisp_Object sequence;
2206 int i;
2207 /* In a vector, look at each element. */
2208 for (i = 0; i < XVECTOR (elt)->size; i++)
2210 binding = XVECTOR (elt)->contents[i];
2211 XSETFASTINT (key, i);
2212 sequence = where_is_internal_1 (binding, key, definition,
2213 noindirect, xkeymap, this,
2214 last, nomenus, last_is_meta);
2215 if (!NILP (sequence))
2216 sequences = Fcons (sequence, sequences);
2219 else if (CHAR_TABLE_P (elt))
2221 Lisp_Object indices[3];
2222 Lisp_Object args;
2224 args = Fcons (Fcons (Fcons (definition, noindirect),
2225 Fcons (xkeymap, Qnil)),
2226 Fcons (Fcons (this, last),
2227 Fcons (make_number (nomenus),
2228 make_number (last_is_meta))));
2229 map_char_table (where_is_internal_2, Qnil, elt, args,
2230 0, indices);
2231 sequences = XCDR (XCDR (XCAR (args)));
2233 else if (CONSP (elt))
2235 Lisp_Object sequence;
2237 key = XCAR (elt);
2238 binding = XCDR (elt);
2240 sequence = where_is_internal_1 (binding, key, definition,
2241 noindirect, xkeymap, this,
2242 last, nomenus, last_is_meta);
2243 if (!NILP (sequence))
2244 sequences = Fcons (sequence, sequences);
2248 for (; ! NILP (sequences); sequences = XCDR (sequences))
2250 Lisp_Object sequence;
2252 sequence = XCAR (sequences);
2254 /* It is a true unshadowed match. Record it, unless it's already
2255 been seen (as could happen when inheriting keymaps). */
2256 if (NILP (Fmember (sequence, found)))
2257 found = Fcons (sequence, found);
2259 /* If firstonly is Qnon_ascii, then we can return the first
2260 binding we find. If firstonly is not Qnon_ascii but not
2261 nil, then we should return the first ascii-only binding
2262 we find. */
2263 if (EQ (firstonly, Qnon_ascii))
2264 RETURN_UNGCPRO (sequence);
2265 else if (! NILP (firstonly) && ascii_sequence_p (sequence))
2266 RETURN_UNGCPRO (sequence);
2271 UNGCPRO;
2273 found = Fnreverse (found);
2275 /* firstonly may have been t, but we may have gone all the way through
2276 the keymaps without finding an all-ASCII key sequence. So just
2277 return the best we could find. */
2278 if (! NILP (firstonly))
2279 return Fcar (found);
2281 return found;
2284 /* This is the function that Fwhere_is_internal calls using map_char_table.
2285 ARGS has the form
2286 (((DEFINITION . NOINDIRECT) . (KEYMAP . RESULT))
2288 ((THIS . LAST) . (NOMENUS . LAST_IS_META)))
2289 Since map_char_table doesn't really use the return value from this function,
2290 we the result append to RESULT, the slot in ARGS.
2292 This function can GC because it calls where_is_internal_1 which can
2293 GC. */
2295 static void
2296 where_is_internal_2 (args, key, binding)
2297 Lisp_Object args, key, binding;
2299 Lisp_Object definition, noindirect, keymap, this, last;
2300 Lisp_Object result, sequence;
2301 int nomenus, last_is_meta;
2302 struct gcpro gcpro1, gcpro2, gcpro3;
2304 GCPRO3 (args, key, binding);
2305 result = XCDR (XCDR (XCAR (args)));
2306 definition = XCAR (XCAR (XCAR (args)));
2307 noindirect = XCDR (XCAR (XCAR (args)));
2308 keymap = XCAR (XCDR (XCAR (args)));
2309 this = XCAR (XCAR (XCDR (args)));
2310 last = XCDR (XCAR (XCDR (args)));
2311 nomenus = XFASTINT (XCAR (XCDR (XCDR (args))));
2312 last_is_meta = XFASTINT (XCDR (XCDR (XCDR (args))));
2314 sequence = where_is_internal_1 (binding, key, definition, noindirect, keymap,
2315 this, last, nomenus, last_is_meta);
2317 if (!NILP (sequence))
2318 XCDR (XCDR (XCAR (args))) = Fcons (sequence, result);
2320 UNGCPRO;
2324 /* This function can GC.because Flookup_key calls get_keymap_1 with
2325 non-zero argument AUTOLOAD. */
2327 static Lisp_Object
2328 where_is_internal_1 (binding, key, definition, noindirect, keymap, this, last,
2329 nomenus, last_is_meta)
2330 Lisp_Object binding, key, definition, noindirect, keymap, this, last;
2331 int nomenus, last_is_meta;
2333 Lisp_Object sequence;
2334 int keymap_specified = !NILP (keymap);
2335 struct gcpro gcpro1, gcpro2;
2337 /* Search through indirections unless that's not wanted. */
2338 if (NILP (noindirect))
2339 binding = get_keyelt (binding, 0);
2341 /* End this iteration if this element does not match
2342 the target. */
2344 if (CONSP (definition))
2346 Lisp_Object tem;
2347 tem = Fequal (binding, definition);
2348 if (NILP (tem))
2349 return Qnil;
2351 else
2352 if (!EQ (binding, definition))
2353 return Qnil;
2355 /* We have found a match.
2356 Construct the key sequence where we found it. */
2357 if (INTEGERP (key) && last_is_meta)
2359 sequence = Fcopy_sequence (this);
2360 Faset (sequence, last, make_number (XINT (key) | meta_modifier));
2362 else
2363 sequence = append_key (this, key);
2365 /* Verify that this key binding is not shadowed by another
2366 binding for the same key, before we say it exists.
2368 Mechanism: look for local definition of this key and if
2369 it is defined and does not match what we found then
2370 ignore this key.
2372 Either nil or number as value from Flookup_key
2373 means undefined. */
2374 GCPRO2 (sequence, binding);
2375 if (keymap_specified)
2377 binding = Flookup_key (keymap, sequence, Qnil);
2378 if (!NILP (binding) && !INTEGERP (binding))
2380 if (CONSP (definition))
2382 Lisp_Object tem;
2383 tem = Fequal (binding, definition);
2384 if (NILP (tem))
2385 RETURN_UNGCPRO (Qnil);
2387 else
2388 if (!EQ (binding, definition))
2389 RETURN_UNGCPRO (Qnil);
2392 else
2394 binding = Fkey_binding (sequence, Qnil);
2395 if (!EQ (binding, definition))
2396 RETURN_UNGCPRO (Qnil);
2399 RETURN_UNGCPRO (sequence);
2402 /* describe-bindings - summarizing all the bindings in a set of keymaps. */
2404 DEFUN ("describe-bindings-internal", Fdescribe_bindings_internal, Sdescribe_bindings_internal, 0, 2, "",
2405 "Show a list of all defined keys, and their definitions.\n\
2406 We put that list in a buffer, and display the buffer.\n\
2408 The optional argument MENUS, if non-nil, says to mention menu bindings.\n\
2409 \(Ordinarily these are omitted from the output.)\n\
2410 The optional argument PREFIX, if non-nil, should be a key sequence;\n\
2411 then we display only bindings that start with that prefix.")
2412 (menus, prefix)
2413 Lisp_Object menus, prefix;
2415 register Lisp_Object thisbuf;
2416 XSETBUFFER (thisbuf, current_buffer);
2417 internal_with_output_to_temp_buffer ("*Help*",
2418 describe_buffer_bindings,
2419 list3 (thisbuf, prefix, menus));
2420 return Qnil;
2423 /* ARG is (BUFFER PREFIX MENU-FLAG). */
2425 static Lisp_Object
2426 describe_buffer_bindings (arg)
2427 Lisp_Object arg;
2429 Lisp_Object descbuf, prefix, shadow;
2430 int nomenu;
2431 register Lisp_Object start1;
2432 struct gcpro gcpro1;
2434 char *alternate_heading
2435 = "\
2436 Keyboard translations:\n\n\
2437 You type Translation\n\
2438 -------- -----------\n";
2440 descbuf = XCAR (arg);
2441 arg = XCDR (arg);
2442 prefix = XCAR (arg);
2443 arg = XCDR (arg);
2444 nomenu = NILP (XCAR (arg));
2446 shadow = Qnil;
2447 GCPRO1 (shadow);
2449 Fset_buffer (Vstandard_output);
2451 /* Report on alternates for keys. */
2452 if (STRINGP (Vkeyboard_translate_table) && !NILP (prefix))
2454 int c;
2455 unsigned char *translate = XSTRING (Vkeyboard_translate_table)->data;
2456 int translate_len = XSTRING (Vkeyboard_translate_table)->size;
2458 for (c = 0; c < translate_len; c++)
2459 if (translate[c] != c)
2461 char buf[KEY_DESCRIPTION_SIZE];
2462 char *bufend;
2464 if (alternate_heading)
2466 insert_string (alternate_heading);
2467 alternate_heading = 0;
2470 bufend = push_key_description (translate[c], buf);
2471 insert (buf, bufend - buf);
2472 Findent_to (make_number (16), make_number (1));
2473 bufend = push_key_description (c, buf);
2474 insert (buf, bufend - buf);
2476 insert ("\n", 1);
2479 insert ("\n", 1);
2482 if (!NILP (Vkey_translation_map))
2483 describe_map_tree (Vkey_translation_map, 0, Qnil, prefix,
2484 "Key translations", nomenu, 1, 0);
2487 int i, nmaps;
2488 Lisp_Object *modes, *maps;
2490 /* Temporarily switch to descbuf, so that we can get that buffer's
2491 minor modes correctly. */
2492 Fset_buffer (descbuf);
2494 if (!NILP (current_kboard->Voverriding_terminal_local_map)
2495 || !NILP (Voverriding_local_map))
2496 nmaps = 0;
2497 else
2498 nmaps = current_minor_maps (&modes, &maps);
2499 Fset_buffer (Vstandard_output);
2501 /* Print the minor mode maps. */
2502 for (i = 0; i < nmaps; i++)
2504 /* The title for a minor mode keymap
2505 is constructed at run time.
2506 We let describe_map_tree do the actual insertion
2507 because it takes care of other features when doing so. */
2508 char *title, *p;
2510 if (!SYMBOLP (modes[i]))
2511 abort();
2513 p = title = (char *) alloca (42 + XSYMBOL (modes[i])->name->size);
2514 *p++ = '\f';
2515 *p++ = '\n';
2516 *p++ = '`';
2517 bcopy (XSYMBOL (modes[i])->name->data, p,
2518 XSYMBOL (modes[i])->name->size);
2519 p += XSYMBOL (modes[i])->name->size;
2520 *p++ = '\'';
2521 bcopy (" Minor Mode Bindings", p, sizeof (" Minor Mode Bindings") - 1);
2522 p += sizeof (" Minor Mode Bindings") - 1;
2523 *p = 0;
2525 describe_map_tree (maps[i], 1, shadow, prefix, title, nomenu, 0, 0);
2526 shadow = Fcons (maps[i], shadow);
2530 /* Print the (major mode) local map. */
2531 if (!NILP (current_kboard->Voverriding_terminal_local_map))
2532 start1 = current_kboard->Voverriding_terminal_local_map;
2533 else if (!NILP (Voverriding_local_map))
2534 start1 = Voverriding_local_map;
2535 else
2536 start1 = XBUFFER (descbuf)->keymap;
2538 if (!NILP (start1))
2540 describe_map_tree (start1, 1, shadow, prefix,
2541 "\f\nMajor Mode Bindings", nomenu, 0, 0);
2542 shadow = Fcons (start1, shadow);
2545 describe_map_tree (current_global_map, 1, shadow, prefix,
2546 "\f\nGlobal Bindings", nomenu, 0, 1);
2548 /* Print the function-key-map translations under this prefix. */
2549 if (!NILP (Vfunction_key_map))
2550 describe_map_tree (Vfunction_key_map, 0, Qnil, prefix,
2551 "\f\nFunction key map translations", nomenu, 1, 0);
2553 call0 (intern ("help-mode"));
2554 Fset_buffer (descbuf);
2555 UNGCPRO;
2556 return Qnil;
2559 /* Insert a description of the key bindings in STARTMAP,
2560 followed by those of all maps reachable through STARTMAP.
2561 If PARTIAL is nonzero, omit certain "uninteresting" commands
2562 (such as `undefined').
2563 If SHADOW is non-nil, it is a list of maps;
2564 don't mention keys which would be shadowed by any of them.
2565 PREFIX, if non-nil, says mention only keys that start with PREFIX.
2566 TITLE, if not 0, is a string to insert at the beginning.
2567 TITLE should not end with a colon or a newline; we supply that.
2568 If NOMENU is not 0, then omit menu-bar commands.
2570 If TRANSL is nonzero, the definitions are actually key translations
2571 so print strings and vectors differently.
2573 If ALWAYS_TITLE is nonzero, print the title even if there are no maps
2574 to look through. */
2576 void
2577 describe_map_tree (startmap, partial, shadow, prefix, title, nomenu, transl,
2578 always_title)
2579 Lisp_Object startmap, shadow, prefix;
2580 int partial;
2581 char *title;
2582 int nomenu;
2583 int transl;
2584 int always_title;
2586 Lisp_Object maps, orig_maps, seen, sub_shadows;
2587 struct gcpro gcpro1, gcpro2, gcpro3;
2588 int something = 0;
2589 char *key_heading
2590 = "\
2591 key binding\n\
2592 --- -------\n";
2594 orig_maps = maps = Faccessible_keymaps (startmap, prefix);
2595 seen = Qnil;
2596 sub_shadows = Qnil;
2597 GCPRO3 (maps, seen, sub_shadows);
2599 if (nomenu)
2601 Lisp_Object list;
2603 /* Delete from MAPS each element that is for the menu bar. */
2604 for (list = maps; !NILP (list); list = XCDR (list))
2606 Lisp_Object elt, prefix, tem;
2608 elt = Fcar (list);
2609 prefix = Fcar (elt);
2610 if (XVECTOR (prefix)->size >= 1)
2612 tem = Faref (prefix, make_number (0));
2613 if (EQ (tem, Qmenu_bar))
2614 maps = Fdelq (elt, maps);
2619 if (!NILP (maps) || always_title)
2621 if (title)
2623 insert_string (title);
2624 if (!NILP (prefix))
2626 insert_string (" Starting With ");
2627 insert1 (Fkey_description (prefix));
2629 insert_string (":\n");
2631 insert_string (key_heading);
2632 something = 1;
2635 for (; !NILP (maps); maps = Fcdr (maps))
2637 register Lisp_Object elt, prefix, tail;
2639 elt = Fcar (maps);
2640 prefix = Fcar (elt);
2642 sub_shadows = Qnil;
2644 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2646 Lisp_Object shmap;
2648 shmap = XCAR (tail);
2650 /* If the sequence by which we reach this keymap is zero-length,
2651 then the shadow map for this keymap is just SHADOW. */
2652 if ((STRINGP (prefix) && XSTRING (prefix)->size == 0)
2653 || (VECTORP (prefix) && XVECTOR (prefix)->size == 0))
2655 /* If the sequence by which we reach this keymap actually has
2656 some elements, then the sequence's definition in SHADOW is
2657 what we should use. */
2658 else
2660 shmap = Flookup_key (shmap, Fcar (elt), Qt);
2661 if (INTEGERP (shmap))
2662 shmap = Qnil;
2665 /* If shmap is not nil and not a keymap,
2666 it completely shadows this map, so don't
2667 describe this map at all. */
2668 if (!NILP (shmap) && NILP (Fkeymapp (shmap)))
2669 goto skip;
2671 if (!NILP (shmap))
2672 sub_shadows = Fcons (shmap, sub_shadows);
2675 /* Maps we have already listed in this loop shadow this map. */
2676 for (tail = orig_maps; ! EQ (tail, maps); tail = XCDR (tail))
2678 Lisp_Object tem;
2679 tem = Fequal (Fcar (XCAR (tail)), prefix);
2680 if (! NILP (tem))
2681 sub_shadows = Fcons (XCDR (XCAR (tail)), sub_shadows);
2684 describe_map (Fcdr (elt), prefix,
2685 transl ? describe_translation : describe_command,
2686 partial, sub_shadows, &seen, nomenu);
2688 skip: ;
2691 if (something)
2692 insert_string ("\n");
2694 UNGCPRO;
2697 static int previous_description_column;
2699 static void
2700 describe_command (definition)
2701 Lisp_Object definition;
2703 register Lisp_Object tem1;
2704 int column = current_column ();
2705 int description_column;
2707 /* If column 16 is no good, go to col 32;
2708 but don't push beyond that--go to next line instead. */
2709 if (column > 30)
2711 insert_char ('\n');
2712 description_column = 32;
2714 else if (column > 14 || (column > 10 && previous_description_column == 32))
2715 description_column = 32;
2716 else
2717 description_column = 16;
2719 Findent_to (make_number (description_column), make_number (1));
2720 previous_description_column = description_column;
2722 if (SYMBOLP (definition))
2724 XSETSTRING (tem1, XSYMBOL (definition)->name);
2725 insert1 (tem1);
2726 insert_string ("\n");
2728 else if (STRINGP (definition) || VECTORP (definition))
2729 insert_string ("Keyboard Macro\n");
2730 else
2732 tem1 = Fkeymapp (definition);
2733 if (!NILP (tem1))
2734 insert_string ("Prefix Command\n");
2735 else
2736 insert_string ("??\n");
2740 static void
2741 describe_translation (definition)
2742 Lisp_Object definition;
2744 register Lisp_Object tem1;
2746 Findent_to (make_number (16), make_number (1));
2748 if (SYMBOLP (definition))
2750 XSETSTRING (tem1, XSYMBOL (definition)->name);
2751 insert1 (tem1);
2752 insert_string ("\n");
2754 else if (STRINGP (definition) || VECTORP (definition))
2756 insert1 (Fkey_description (definition));
2757 insert_string ("\n");
2759 else
2761 tem1 = Fkeymapp (definition);
2762 if (!NILP (tem1))
2763 insert_string ("Prefix Command\n");
2764 else
2765 insert_string ("??\n");
2769 /* Like Flookup_key, but uses a list of keymaps SHADOW instead of a single map.
2770 Returns the first non-nil binding found in any of those maps. */
2772 static Lisp_Object
2773 shadow_lookup (shadow, key, flag)
2774 Lisp_Object shadow, key, flag;
2776 Lisp_Object tail, value;
2778 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2780 value = Flookup_key (XCAR (tail), key, flag);
2781 if (!NILP (value))
2782 return value;
2784 return Qnil;
2787 /* Describe the contents of map MAP, assuming that this map itself is
2788 reached by the sequence of prefix keys KEYS (a string or vector).
2789 PARTIAL, SHADOW, NOMENU are as in `describe_map_tree' above. */
2791 static void
2792 describe_map (map, keys, elt_describer, partial, shadow, seen, nomenu)
2793 register Lisp_Object map;
2794 Lisp_Object keys;
2795 void (*elt_describer) P_ ((Lisp_Object));
2796 int partial;
2797 Lisp_Object shadow;
2798 Lisp_Object *seen;
2799 int nomenu;
2801 Lisp_Object elt_prefix;
2802 Lisp_Object tail, definition, event;
2803 Lisp_Object tem;
2804 Lisp_Object suppress;
2805 Lisp_Object kludge;
2806 int first = 1;
2807 struct gcpro gcpro1, gcpro2, gcpro3;
2809 suppress = Qnil;
2811 if (!NILP (keys) && XFASTINT (Flength (keys)) > 0)
2813 /* Call Fkey_description first, to avoid GC bug for the other string. */
2814 tem = Fkey_description (keys);
2815 elt_prefix = concat2 (tem, build_string (" "));
2817 else
2818 elt_prefix = Qnil;
2820 if (partial)
2821 suppress = intern ("suppress-keymap");
2823 /* This vector gets used to present single keys to Flookup_key. Since
2824 that is done once per keymap element, we don't want to cons up a
2825 fresh vector every time. */
2826 kludge = Fmake_vector (make_number (1), Qnil);
2827 definition = Qnil;
2829 GCPRO3 (elt_prefix, definition, kludge);
2831 for (tail = map; CONSP (tail); tail = XCDR (tail))
2833 QUIT;
2835 if (VECTORP (XCAR (tail))
2836 || CHAR_TABLE_P (XCAR (tail)))
2837 describe_vector (XCAR (tail),
2838 elt_prefix, elt_describer, partial, shadow, map,
2839 (int *)0, 0);
2840 else if (CONSP (XCAR (tail)))
2842 event = XCAR (XCAR (tail));
2844 /* Ignore bindings whose "keys" are not really valid events.
2845 (We get these in the frames and buffers menu.) */
2846 if (! (SYMBOLP (event) || INTEGERP (event)))
2847 continue;
2849 if (nomenu && EQ (event, Qmenu_bar))
2850 continue;
2852 definition = get_keyelt (XCDR (XCAR (tail)), 0);
2854 /* Don't show undefined commands or suppressed commands. */
2855 if (NILP (definition)) continue;
2856 if (SYMBOLP (definition) && partial)
2858 tem = Fget (definition, suppress);
2859 if (!NILP (tem))
2860 continue;
2863 /* Don't show a command that isn't really visible
2864 because a local definition of the same key shadows it. */
2866 XVECTOR (kludge)->contents[0] = event;
2867 if (!NILP (shadow))
2869 tem = shadow_lookup (shadow, kludge, Qt);
2870 if (!NILP (tem)) continue;
2873 tem = Flookup_key (map, kludge, Qt);
2874 if (! EQ (tem, definition)) continue;
2876 if (first)
2878 previous_description_column = 0;
2879 insert ("\n", 1);
2880 first = 0;
2883 if (!NILP (elt_prefix))
2884 insert1 (elt_prefix);
2886 /* THIS gets the string to describe the character EVENT. */
2887 insert1 (Fsingle_key_description (event, Qnil));
2889 /* Print a description of the definition of this character.
2890 elt_describer will take care of spacing out far enough
2891 for alignment purposes. */
2892 (*elt_describer) (definition);
2894 else if (EQ (XCAR (tail), Qkeymap))
2896 /* The same keymap might be in the structure twice, if we're
2897 using an inherited keymap. So skip anything we've already
2898 encountered. */
2899 tem = Fassq (tail, *seen);
2900 if (CONSP (tem) && !NILP (Fequal (XCAR (tem), keys)))
2901 break;
2902 *seen = Fcons (Fcons (tail, keys), *seen);
2906 UNGCPRO;
2909 static void
2910 describe_vector_princ (elt)
2911 Lisp_Object elt;
2913 Findent_to (make_number (16), make_number (1));
2914 Fprinc (elt, Qnil);
2915 Fterpri (Qnil);
2918 DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 1, 0,
2919 "Insert a description of contents of VECTOR.\n\
2920 This is text showing the elements of vector matched against indices.")
2921 (vector)
2922 Lisp_Object vector;
2924 int count = specpdl_ptr - specpdl;
2926 specbind (Qstandard_output, Fcurrent_buffer ());
2927 CHECK_VECTOR_OR_CHAR_TABLE (vector, 0);
2928 describe_vector (vector, Qnil, describe_vector_princ, 0,
2929 Qnil, Qnil, (int *)0, 0);
2931 return unbind_to (count, Qnil);
2934 /* Insert in the current buffer a description of the contents of VECTOR.
2935 We call ELT_DESCRIBER to insert the description of one value found
2936 in VECTOR.
2938 ELT_PREFIX describes what "comes before" the keys or indices defined
2939 by this vector. This is a human-readable string whose size
2940 is not necessarily related to the situation.
2942 If the vector is in a keymap, ELT_PREFIX is a prefix key which
2943 leads to this keymap.
2945 If the vector is a chartable, ELT_PREFIX is the vector
2946 of bytes that lead to the character set or portion of a character
2947 set described by this chartable.
2949 If PARTIAL is nonzero, it means do not mention suppressed commands
2950 (that assumes the vector is in a keymap).
2952 SHADOW is a list of keymaps that shadow this map.
2953 If it is non-nil, then we look up the key in those maps
2954 and we don't mention it now if it is defined by any of them.
2956 ENTIRE_MAP is the keymap in which this vector appears.
2957 If the definition in effect in the whole map does not match
2958 the one in this vector, we ignore this one.
2960 When describing a sub-char-table, INDICES is a list of
2961 indices at higher levels in this char-table,
2962 and CHAR_TABLE_DEPTH says how many levels down we have gone. */
2964 void
2965 describe_vector (vector, elt_prefix, elt_describer,
2966 partial, shadow, entire_map,
2967 indices, char_table_depth)
2968 register Lisp_Object vector;
2969 Lisp_Object elt_prefix;
2970 void (*elt_describer) P_ ((Lisp_Object));
2971 int partial;
2972 Lisp_Object shadow;
2973 Lisp_Object entire_map;
2974 int *indices;
2975 int char_table_depth;
2977 Lisp_Object definition;
2978 Lisp_Object tem2;
2979 register int i;
2980 Lisp_Object suppress;
2981 Lisp_Object kludge;
2982 int first = 1;
2983 struct gcpro gcpro1, gcpro2, gcpro3;
2984 /* Range of elements to be handled. */
2985 int from, to;
2986 /* A flag to tell if a leaf in this level of char-table is not a
2987 generic character (i.e. a complete multibyte character). */
2988 int complete_char;
2989 int character;
2990 int starting_i;
2992 suppress = Qnil;
2994 if (indices == 0)
2995 indices = (int *) alloca (3 * sizeof (int));
2997 definition = Qnil;
2999 /* This vector gets used to present single keys to Flookup_key. Since
3000 that is done once per vector element, we don't want to cons up a
3001 fresh vector every time. */
3002 kludge = Fmake_vector (make_number (1), Qnil);
3003 GCPRO3 (elt_prefix, definition, kludge);
3005 if (partial)
3006 suppress = intern ("suppress-keymap");
3008 if (CHAR_TABLE_P (vector))
3010 if (char_table_depth == 0)
3012 /* VECTOR is a top level char-table. */
3013 complete_char = 1;
3014 from = 0;
3015 to = CHAR_TABLE_ORDINARY_SLOTS;
3017 else
3019 /* VECTOR is a sub char-table. */
3020 if (char_table_depth >= 3)
3021 /* A char-table is never that deep. */
3022 error ("Too deep char table");
3024 complete_char
3025 = (CHARSET_VALID_P (indices[0])
3026 && ((CHARSET_DIMENSION (indices[0]) == 1
3027 && char_table_depth == 1)
3028 || char_table_depth == 2));
3030 /* Meaningful elements are from 32th to 127th. */
3031 from = 32;
3032 to = SUB_CHAR_TABLE_ORDINARY_SLOTS;
3035 else
3037 /* This does the right thing for ordinary vectors. */
3039 complete_char = 1;
3040 from = 0;
3041 to = XVECTOR (vector)->size;
3044 for (i = from; i < to; i++)
3046 QUIT;
3048 if (CHAR_TABLE_P (vector))
3050 if (char_table_depth == 0 && i >= CHAR_TABLE_SINGLE_BYTE_SLOTS)
3051 complete_char = 0;
3053 if (i >= CHAR_TABLE_SINGLE_BYTE_SLOTS
3054 && !CHARSET_DEFINED_P (i - 128))
3055 continue;
3057 definition
3058 = get_keyelt (XCHAR_TABLE (vector)->contents[i], 0);
3060 else
3061 definition = get_keyelt (XVECTOR (vector)->contents[i], 0);
3063 if (NILP (definition)) continue;
3065 /* Don't mention suppressed commands. */
3066 if (SYMBOLP (definition) && partial)
3068 Lisp_Object tem;
3070 tem = Fget (definition, suppress);
3072 if (!NILP (tem)) continue;
3075 /* Set CHARACTER to the character this entry describes, if any.
3076 Also update *INDICES. */
3077 if (CHAR_TABLE_P (vector))
3079 indices[char_table_depth] = i;
3081 if (char_table_depth == 0)
3083 character = i;
3084 indices[0] = i - 128;
3086 else if (complete_char)
3088 character = MAKE_CHAR (indices[0], indices[1], indices[2]);
3090 else
3091 character = 0;
3093 else
3094 character = i;
3096 /* If this binding is shadowed by some other map, ignore it. */
3097 if (!NILP (shadow) && complete_char)
3099 Lisp_Object tem;
3101 XVECTOR (kludge)->contents[0] = make_number (character);
3102 tem = shadow_lookup (shadow, kludge, Qt);
3104 if (!NILP (tem)) continue;
3107 /* Ignore this definition if it is shadowed by an earlier
3108 one in the same keymap. */
3109 if (!NILP (entire_map) && complete_char)
3111 Lisp_Object tem;
3113 XVECTOR (kludge)->contents[0] = make_number (character);
3114 tem = Flookup_key (entire_map, kludge, Qt);
3116 if (! EQ (tem, definition))
3117 continue;
3120 if (first)
3122 if (char_table_depth == 0)
3123 insert ("\n", 1);
3124 first = 0;
3127 /* For a sub char-table, show the depth by indentation.
3128 CHAR_TABLE_DEPTH can be greater than 0 only for a char-table. */
3129 if (char_table_depth > 0)
3130 insert (" ", char_table_depth * 2); /* depth is 1 or 2. */
3132 /* Output the prefix that applies to every entry in this map. */
3133 if (!NILP (elt_prefix))
3134 insert1 (elt_prefix);
3136 /* Insert or describe the character this slot is for,
3137 or a description of what it is for. */
3138 if (SUB_CHAR_TABLE_P (vector))
3140 if (complete_char)
3141 insert_char (character);
3142 else
3144 /* We need an octal representation for this block of
3145 characters. */
3146 char work[16];
3147 sprintf (work, "(row %d)", i);
3148 insert (work, strlen (work));
3151 else if (CHAR_TABLE_P (vector))
3153 if (complete_char)
3154 insert1 (Fsingle_key_description (make_number (character), Qnil));
3155 else
3157 /* Print the information for this character set. */
3158 insert_string ("<");
3159 tem2 = CHARSET_TABLE_INFO (i - 128, CHARSET_SHORT_NAME_IDX);
3160 if (STRINGP (tem2))
3161 insert_from_string (tem2, 0, 0, XSTRING (tem2)->size,
3162 STRING_BYTES (XSTRING (tem2)), 0);
3163 else
3164 insert ("?", 1);
3165 insert (">", 1);
3168 else
3170 insert1 (Fsingle_key_description (make_number (character), Qnil));
3173 /* If we find a sub char-table within a char-table,
3174 scan it recursively; it defines the details for
3175 a character set or a portion of a character set. */
3176 if (CHAR_TABLE_P (vector) && SUB_CHAR_TABLE_P (definition))
3178 insert ("\n", 1);
3179 describe_vector (definition, elt_prefix, elt_describer,
3180 partial, shadow, entire_map,
3181 indices, char_table_depth + 1);
3182 continue;
3185 starting_i = i;
3187 /* Find all consecutive characters or rows that have the same
3188 definition. But, for elements of a top level char table, if
3189 they are for charsets, we had better describe one by one even
3190 if they have the same definition. */
3191 if (CHAR_TABLE_P (vector))
3193 int limit = to;
3195 if (char_table_depth == 0)
3196 limit = CHAR_TABLE_SINGLE_BYTE_SLOTS;
3198 while (i + 1 < limit
3199 && (tem2 = get_keyelt (XCHAR_TABLE (vector)->contents[i + 1], 0),
3200 !NILP (tem2))
3201 && !NILP (Fequal (tem2, definition)))
3202 i++;
3204 else
3205 while (i + 1 < to
3206 && (tem2 = get_keyelt (XVECTOR (vector)->contents[i + 1], 0),
3207 !NILP (tem2))
3208 && !NILP (Fequal (tem2, definition)))
3209 i++;
3212 /* If we have a range of more than one character,
3213 print where the range reaches to. */
3215 if (i != starting_i)
3217 insert (" .. ", 4);
3219 if (!NILP (elt_prefix))
3220 insert1 (elt_prefix);
3222 if (CHAR_TABLE_P (vector))
3224 if (char_table_depth == 0)
3226 insert1 (Fsingle_key_description (make_number (i), Qnil));
3228 else if (complete_char)
3230 indices[char_table_depth] = i;
3231 character = MAKE_CHAR (indices[0], indices[1], indices[2]);
3232 insert_char (character);
3234 else
3236 /* We need an octal representation for this block of
3237 characters. */
3238 char work[16];
3239 sprintf (work, "(row %d)", i);
3240 insert (work, strlen (work));
3243 else
3245 insert1 (Fsingle_key_description (make_number (i), Qnil));
3249 /* Print a description of the definition of this character.
3250 elt_describer will take care of spacing out far enough
3251 for alignment purposes. */
3252 (*elt_describer) (definition);
3255 /* For (sub) char-table, print `defalt' slot at last. */
3256 if (CHAR_TABLE_P (vector) && !NILP (XCHAR_TABLE (vector)->defalt))
3258 insert (" ", char_table_depth * 2);
3259 insert_string ("<<default>>");
3260 (*elt_describer) (XCHAR_TABLE (vector)->defalt);
3263 UNGCPRO;
3266 /* Apropos - finding all symbols whose names match a regexp. */
3267 Lisp_Object apropos_predicate;
3268 Lisp_Object apropos_accumulate;
3270 static void
3271 apropos_accum (symbol, string)
3272 Lisp_Object symbol, string;
3274 register Lisp_Object tem;
3276 tem = Fstring_match (string, Fsymbol_name (symbol), Qnil);
3277 if (!NILP (tem) && !NILP (apropos_predicate))
3278 tem = call1 (apropos_predicate, symbol);
3279 if (!NILP (tem))
3280 apropos_accumulate = Fcons (symbol, apropos_accumulate);
3283 DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0,
3284 "Show all symbols whose names contain match for REGEXP.\n\
3285 If optional 2nd arg PREDICATE is non-nil, (funcall PREDICATE SYMBOL) is done\n\
3286 for each symbol and a symbol is mentioned only if that returns non-nil.\n\
3287 Return list of symbols found.")
3288 (regexp, predicate)
3289 Lisp_Object regexp, predicate;
3291 struct gcpro gcpro1, gcpro2;
3292 CHECK_STRING (regexp, 0);
3293 apropos_predicate = predicate;
3294 GCPRO2 (apropos_predicate, apropos_accumulate);
3295 apropos_accumulate = Qnil;
3296 map_obarray (Vobarray, apropos_accum, regexp);
3297 apropos_accumulate = Fsort (apropos_accumulate, Qstring_lessp);
3298 UNGCPRO;
3299 return apropos_accumulate;
3302 void
3303 syms_of_keymap ()
3305 Qkeymap = intern ("keymap");
3306 staticpro (&Qkeymap);
3308 /* Now we are ready to set up this property, so we can
3309 create char tables. */
3310 Fput (Qkeymap, Qchar_table_extra_slots, make_number (0));
3312 /* Initialize the keymaps standardly used.
3313 Each one is the value of a Lisp variable, and is also
3314 pointed to by a C variable */
3316 global_map = Fmake_keymap (Qnil);
3317 Fset (intern ("global-map"), global_map);
3319 current_global_map = global_map;
3320 staticpro (&global_map);
3321 staticpro (&current_global_map);
3323 meta_map = Fmake_keymap (Qnil);
3324 Fset (intern ("esc-map"), meta_map);
3325 Ffset (intern ("ESC-prefix"), meta_map);
3327 control_x_map = Fmake_keymap (Qnil);
3328 Fset (intern ("ctl-x-map"), control_x_map);
3329 Ffset (intern ("Control-X-prefix"), control_x_map);
3331 DEFVAR_LISP ("define-key-rebound-commands", &Vdefine_key_rebound_commands,
3332 "List of commands given new key bindings recently.\n\
3333 This is used for internal purposes during Emacs startup;\n\
3334 don't alter it yourself.");
3335 Vdefine_key_rebound_commands = Qt;
3337 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map,
3338 "Default keymap to use when reading from the minibuffer.");
3339 Vminibuffer_local_map = Fmake_sparse_keymap (Qnil);
3341 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map,
3342 "Local keymap for the minibuffer when spaces are not allowed.");
3343 Vminibuffer_local_ns_map = Fmake_sparse_keymap (Qnil);
3345 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map,
3346 "Local keymap for minibuffer input with completion.");
3347 Vminibuffer_local_completion_map = Fmake_sparse_keymap (Qnil);
3349 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map,
3350 "Local keymap for minibuffer input with completion, for exact match.");
3351 Vminibuffer_local_must_match_map = Fmake_sparse_keymap (Qnil);
3353 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist,
3354 "Alist of keymaps to use for minor modes.\n\
3355 Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read\n\
3356 key sequences and look up bindings iff VARIABLE's value is non-nil.\n\
3357 If two active keymaps bind the same key, the keymap appearing earlier\n\
3358 in the list takes precedence.");
3359 Vminor_mode_map_alist = Qnil;
3361 DEFVAR_LISP ("minor-mode-overriding-map-alist", &Vminor_mode_overriding_map_alist,
3362 "Alist of keymaps to use for minor modes, in current major mode.\n\
3363 This variable is a alist just like `minor-mode-map-alist', and it is\n\
3364 used the same way (and before `minor-mode-map-alist'); however,\n\
3365 it is provided for major modes to bind locally.");
3366 Vminor_mode_overriding_map_alist = Qnil;
3368 DEFVAR_LISP ("function-key-map", &Vfunction_key_map,
3369 "Keymap mapping ASCII function key sequences onto their preferred forms.\n\
3370 This allows Emacs to recognize function keys sent from ASCII\n\
3371 terminals at any point in a key sequence.\n\
3373 The `read-key-sequence' function replaces any subsequence bound by\n\
3374 `function-key-map' with its binding. More precisely, when the active\n\
3375 keymaps have no binding for the current key sequence but\n\
3376 `function-key-map' binds a suffix of the sequence to a vector or string,\n\
3377 `read-key-sequence' replaces the matching suffix with its binding, and\n\
3378 continues with the new sequence.\n\
3380 The events that come from bindings in `function-key-map' are not\n\
3381 themselves looked up in `function-key-map'.\n\
3383 For example, suppose `function-key-map' binds `ESC O P' to [f1].\n\
3384 Typing `ESC O P' to `read-key-sequence' would return [f1]. Typing\n\
3385 `C-x ESC O P' would return [?\\C-x f1]. If [f1] were a prefix\n\
3386 key, typing `ESC O P x' would return [f1 x].");
3387 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
3389 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
3390 "Keymap of key translations that can override keymaps.\n\
3391 This keymap works like `function-key-map', but comes after that,\n\
3392 and applies even for keys that have ordinary bindings.");
3393 Vkey_translation_map = Qnil;
3395 Qsingle_key_description = intern ("single-key-description");
3396 staticpro (&Qsingle_key_description);
3398 Qkey_description = intern ("key-description");
3399 staticpro (&Qkey_description);
3401 Qkeymapp = intern ("keymapp");
3402 staticpro (&Qkeymapp);
3404 Qnon_ascii = intern ("non-ascii");
3405 staticpro (&Qnon_ascii);
3407 Qmenu_item = intern ("menu-item");
3408 staticpro (&Qmenu_item);
3410 defsubr (&Skeymapp);
3411 defsubr (&Skeymap_parent);
3412 defsubr (&Sset_keymap_parent);
3413 defsubr (&Smake_keymap);
3414 defsubr (&Smake_sparse_keymap);
3415 defsubr (&Scopy_keymap);
3416 defsubr (&Skey_binding);
3417 defsubr (&Slocal_key_binding);
3418 defsubr (&Sglobal_key_binding);
3419 defsubr (&Sminor_mode_key_binding);
3420 defsubr (&Sdefine_key);
3421 defsubr (&Slookup_key);
3422 defsubr (&Sdefine_prefix_command);
3423 defsubr (&Suse_global_map);
3424 defsubr (&Suse_local_map);
3425 defsubr (&Scurrent_local_map);
3426 defsubr (&Scurrent_global_map);
3427 defsubr (&Scurrent_minor_mode_maps);
3428 defsubr (&Saccessible_keymaps);
3429 defsubr (&Skey_description);
3430 defsubr (&Sdescribe_vector);
3431 defsubr (&Ssingle_key_description);
3432 defsubr (&Stext_char_description);
3433 defsubr (&Swhere_is_internal);
3434 defsubr (&Sdescribe_bindings_internal);
3435 defsubr (&Sapropos_internal);
3438 void
3439 keys_of_keymap ()
3441 initial_define_key (global_map, 033, "ESC-prefix");
3442 initial_define_key (global_map, Ctl('X'), "Control-X-prefix");