*** empty log message ***
[emacs.git] / src / keymap.c
blob410d38e54f40e63832946747e34015b6e52e2b96
1 /* Manipulation of keymaps
2 Copyright (C) 1985, 86,87,88,93,94,95,98,99 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include <stdio.h>
24 #undef NULL
25 #include "lisp.h"
26 #include "commands.h"
27 #include "buffer.h"
28 #include "charset.h"
29 #include "keyboard.h"
30 #include "termhooks.h"
31 #include "blockinput.h"
32 #include "puresize.h"
33 #include "intervals.h"
35 #define min(a, b) ((a) < (b) ? (a) : (b))
36 #define KEYMAPP(m) (!NILP (Fkeymapp (m)))
38 /* The number of elements in keymap vectors. */
39 #define DENSE_TABLE_SIZE (0200)
41 /* Actually allocate storage for these variables */
43 Lisp_Object current_global_map; /* Current global keymap */
45 Lisp_Object global_map; /* default global key bindings */
47 Lisp_Object meta_map; /* The keymap used for globally bound
48 ESC-prefixed default commands */
50 Lisp_Object control_x_map; /* The keymap used for globally bound
51 C-x-prefixed default commands */
53 /* was MinibufLocalMap */
54 Lisp_Object Vminibuffer_local_map;
55 /* The keymap used by the minibuf for local
56 bindings when spaces are allowed in the
57 minibuf */
59 /* was MinibufLocalNSMap */
60 Lisp_Object Vminibuffer_local_ns_map;
61 /* The keymap used by the minibuf for local
62 bindings when spaces are not encouraged
63 in the minibuf */
65 /* keymap used for minibuffers when doing completion */
66 /* was MinibufLocalCompletionMap */
67 Lisp_Object Vminibuffer_local_completion_map;
69 /* keymap used for minibuffers when doing completion and require a match */
70 /* was MinibufLocalMustMatchMap */
71 Lisp_Object Vminibuffer_local_must_match_map;
73 /* Alist of minor mode variables and keymaps. */
74 Lisp_Object Vminor_mode_map_alist;
76 /* Alist of major-mode-specific overrides for
77 minor mode variables and keymaps. */
78 Lisp_Object Vminor_mode_overriding_map_alist;
80 /* Keymap mapping ASCII function key sequences onto their preferred forms.
81 Initialized by the terminal-specific lisp files. See DEFVAR for more
82 documentation. */
83 Lisp_Object Vfunction_key_map;
85 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
86 Lisp_Object Vkey_translation_map;
88 /* A list of all commands given new bindings since a certain time
89 when nil was stored here.
90 This is used to speed up recomputation of menu key equivalents
91 when Emacs starts up. t means don't record anything here. */
92 Lisp_Object Vdefine_key_rebound_commands;
94 Lisp_Object Qkeymapp, Qkeymap, Qnon_ascii, Qmenu_item;
96 /* A char with the CHAR_META bit set in a vector or the 0200 bit set
97 in a string key sequence is equivalent to prefixing with this
98 character. */
99 extern Lisp_Object meta_prefix_char;
101 extern Lisp_Object Voverriding_local_map;
103 static Lisp_Object store_in_keymap P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
104 static void fix_submap_inheritance P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
106 static Lisp_Object define_as_prefix P_ ((Lisp_Object, Lisp_Object));
107 static Lisp_Object describe_buffer_bindings P_ ((Lisp_Object));
108 static void describe_command P_ ((Lisp_Object));
109 static void describe_translation P_ ((Lisp_Object));
110 static void describe_map P_ ((Lisp_Object, Lisp_Object,
111 void (*) P_ ((Lisp_Object)),
112 int, Lisp_Object, Lisp_Object*, int));
114 /* Keymap object support - constructors and predicates. */
116 DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 1, 0,
117 "Construct and return a new keymap, of the form (keymap CHARTABLE . ALIST).\n\
118 CHARTABLE is a char-table that holds the bindings for the ASCII\n\
119 characters. ALIST is an assoc-list which holds bindings for function keys,\n\
120 mouse events, and any other things that appear in the input stream.\n\
121 All entries in it are initially nil, meaning \"command undefined\".\n\n\
122 The optional arg STRING supplies a menu name for the keymap\n\
123 in case you use it as a menu with `x-popup-menu'.")
124 (string)
125 Lisp_Object string;
127 Lisp_Object tail;
128 if (!NILP (string))
129 tail = Fcons (string, Qnil);
130 else
131 tail = Qnil;
132 return Fcons (Qkeymap,
133 Fcons (Fmake_char_table (Qkeymap, Qnil), tail));
136 DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 1, 0,
137 "Construct and return a new sparse-keymap list.\n\
138 Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),\n\
139 which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),\n\
140 which binds the function key or mouse event SYMBOL to DEFINITION.\n\
141 Initially the alist is nil.\n\n\
142 The optional arg STRING supplies a menu name for the keymap\n\
143 in case you use it as a menu with `x-popup-menu'.")
144 (string)
145 Lisp_Object string;
147 if (!NILP (string))
148 return Fcons (Qkeymap, Fcons (string, Qnil));
149 return Fcons (Qkeymap, Qnil);
152 /* This function is used for installing the standard key bindings
153 at initialization time.
155 For example:
157 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); */
159 void
160 initial_define_key (keymap, key, defname)
161 Lisp_Object keymap;
162 int key;
163 char *defname;
165 store_in_keymap (keymap, make_number (key), intern (defname));
168 void
169 initial_define_lispy_key (keymap, keyname, defname)
170 Lisp_Object keymap;
171 char *keyname;
172 char *defname;
174 store_in_keymap (keymap, intern (keyname), intern (defname));
177 /* Define character fromchar in map frommap as an alias for character
178 tochar in map tomap. Subsequent redefinitions of the latter WILL
179 affect the former. */
181 #if 0
182 void
183 synkey (frommap, fromchar, tomap, tochar)
184 struct Lisp_Vector *frommap, *tomap;
185 int fromchar, tochar;
187 Lisp_Object v, c;
188 XSETVECTOR (v, tomap);
189 XSETFASTINT (c, tochar);
190 frommap->contents[fromchar] = Fcons (v, c);
192 #endif /* 0 */
194 DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0,
195 "Return t if OBJECT is a keymap.\n\
197 A keymap is a list (keymap . ALIST),\n\
198 or a symbol whose function definition is itself a keymap.\n\
199 ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);\n\
200 a vector of densely packed bindings for small character codes\n\
201 is also allowed as an element.")
202 (object)
203 Lisp_Object object;
205 /* FIXME: Maybe this should return t for autoloaded keymaps? -sm */
206 return (NILP (get_keymap_1 (object, 0, 0)) ? Qnil : Qt);
209 /* Check that OBJECT is a keymap (after dereferencing through any
210 symbols). If it is, return it.
212 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
213 is an autoload form, do the autoload and try again.
214 If AUTOLOAD is nonzero, callers must assume GC is possible.
216 ERROR controls how we respond if OBJECT isn't a keymap.
217 If ERROR is non-zero, signal an error; otherwise, just return Qnil.
219 Note that most of the time, we don't want to pursue autoloads.
220 Functions like Faccessible_keymaps which scan entire keymap trees
221 shouldn't load every autoloaded keymap. I'm not sure about this,
222 but it seems to me that only read_key_sequence, Flookup_key, and
223 Fdefine_key should cause keymaps to be autoloaded.
225 This function can GC when AUTOLOAD is non-zero, because it calls
226 do_autoload which can GC. */
228 Lisp_Object
229 get_keymap_1 (object, error, autoload)
230 Lisp_Object object;
231 int error, autoload;
233 Lisp_Object tem;
235 autoload_retry:
236 if (NILP (object))
237 goto end;
238 if (CONSP (object) && EQ (XCAR (object), Qkeymap))
239 return object;
240 else
242 tem = indirect_function (object);
243 if (CONSP (tem) && EQ (XCAR (tem), Qkeymap))
244 return tem;
247 /* Should we do an autoload? Autoload forms for keymaps have
248 Qkeymap as their fifth element. */
249 if (autoload
250 && SYMBOLP (object)
251 && CONSP (tem)
252 && EQ (XCAR (tem), Qautoload))
254 Lisp_Object tail;
256 tail = Fnth (make_number (4), tem);
257 if (EQ (tail, Qkeymap))
259 struct gcpro gcpro1, gcpro2;
261 GCPRO2 (tem, object);
262 do_autoload (tem, object);
263 UNGCPRO;
265 goto autoload_retry;
269 end:
270 if (error)
271 wrong_type_argument (Qkeymapp, object);
272 else
273 return Qnil;
277 /* Follow any symbol chaining, and return the keymap denoted by OBJECT.
278 If OBJECT doesn't denote a keymap at all, signal an error. */
279 Lisp_Object
280 get_keymap (object)
281 Lisp_Object object;
283 return get_keymap_1 (object, 1, 0);
286 /* Return the parent map of the keymap MAP, or nil if it has none.
287 We assume that MAP is a valid keymap. */
289 DEFUN ("keymap-parent", Fkeymap_parent, Skeymap_parent, 1, 1, 0,
290 "Return the parent keymap of KEYMAP.")
291 (keymap)
292 Lisp_Object keymap;
294 Lisp_Object list;
296 keymap = get_keymap_1 (keymap, 1, 1);
298 /* Skip past the initial element `keymap'. */
299 list = XCDR (keymap);
300 for (; CONSP (list); list = XCDR (list))
302 /* See if there is another `keymap'. */
303 if (KEYMAPP (list))
304 return list;
307 return Qnil;
310 /* Set the parent keymap of MAP to PARENT. */
312 DEFUN ("set-keymap-parent", Fset_keymap_parent, Sset_keymap_parent, 2, 2, 0,
313 "Modify KEYMAP to set its parent map to PARENT.\n\
314 PARENT should be nil or another keymap.")
315 (keymap, parent)
316 Lisp_Object keymap, parent;
318 Lisp_Object list, prev;
319 struct gcpro gcpro1;
320 int i;
322 keymap = get_keymap_1 (keymap, 1, 1);
323 GCPRO1 (keymap);
325 if (!NILP (parent))
326 parent = get_keymap_1 (parent, 1, 1);
328 /* Skip past the initial element `keymap'. */
329 prev = keymap;
330 while (1)
332 list = XCDR (prev);
333 /* If there is a parent keymap here, replace it.
334 If we came to the end, add the parent in PREV. */
335 if (! CONSP (list) || KEYMAPP (list))
337 /* If we already have the right parent, return now
338 so that we avoid the loops below. */
339 if (EQ (XCDR (prev), parent))
340 RETURN_UNGCPRO (parent);
342 XCDR (prev) = parent;
343 break;
345 prev = list;
348 /* Scan through for submaps, and set their parents too. */
350 for (list = XCDR (keymap); CONSP (list); list = XCDR (list))
352 /* Stop the scan when we come to the parent. */
353 if (EQ (XCAR (list), Qkeymap))
354 break;
356 /* If this element holds a prefix map, deal with it. */
357 if (CONSP (XCAR (list))
358 && CONSP (XCDR (XCAR (list))))
359 fix_submap_inheritance (keymap, XCAR (XCAR (list)),
360 XCDR (XCAR (list)));
362 if (VECTORP (XCAR (list)))
363 for (i = 0; i < XVECTOR (XCAR (list))->size; i++)
364 if (CONSP (XVECTOR (XCAR (list))->contents[i]))
365 fix_submap_inheritance (keymap, make_number (i),
366 XVECTOR (XCAR (list))->contents[i]);
368 if (CHAR_TABLE_P (XCAR (list)))
370 Lisp_Object indices[3];
372 map_char_table (fix_submap_inheritance, Qnil, XCAR (list),
373 keymap, 0, indices);
377 RETURN_UNGCPRO (parent);
380 /* EVENT is defined in MAP as a prefix, and SUBMAP is its definition.
381 if EVENT is also a prefix in MAP's parent,
382 make sure that SUBMAP inherits that definition as its own parent. */
384 static void
385 fix_submap_inheritance (map, event, submap)
386 Lisp_Object map, event, submap;
388 Lisp_Object map_parent, parent_entry;
390 /* SUBMAP is a cons that we found as a key binding.
391 Discard the other things found in a menu key binding. */
393 if (CONSP (submap))
395 /* May be an old format menu item */
396 if (STRINGP (XCAR (submap)))
398 submap = XCDR (submap);
399 /* Also remove a menu help string, if any,
400 following the menu item name. */
401 if (CONSP (submap) && STRINGP (XCAR (submap)))
402 submap = XCDR (submap);
403 /* Also remove the sublist that caches key equivalences, if any. */
404 if (CONSP (submap)
405 && CONSP (XCAR (submap)))
407 Lisp_Object carcar;
408 carcar = XCAR (XCAR (submap));
409 if (NILP (carcar) || VECTORP (carcar))
410 submap = XCDR (submap);
414 /* Or a new format menu item */
415 else if (EQ (XCAR (submap), Qmenu_item)
416 && CONSP (XCDR (submap)))
418 submap = XCDR (XCDR (submap));
419 if (CONSP (submap))
420 submap = XCAR (submap);
424 /* If it isn't a keymap now, there's no work to do. */
425 if (! CONSP (submap)
426 || ! EQ (XCAR (submap), Qkeymap))
427 return;
429 map_parent = Fkeymap_parent (map);
430 if (! NILP (map_parent))
431 parent_entry = access_keymap (map_parent, event, 0, 0);
432 else
433 parent_entry = Qnil;
435 /* If MAP's parent has something other than a keymap,
436 our own submap shadows it completely, so use nil as SUBMAP's parent. */
437 if (! (CONSP (parent_entry) && EQ (XCAR (parent_entry), Qkeymap)))
438 parent_entry = Qnil;
440 if (! EQ (parent_entry, submap))
442 Lisp_Object submap_parent;
443 submap_parent = submap;
444 while (1)
446 Lisp_Object tem;
447 tem = Fkeymap_parent (submap_parent);
448 if (EQ (tem, parent_entry))
449 return;
450 if (CONSP (tem)
451 && EQ (XCAR (tem), Qkeymap))
452 submap_parent = tem;
453 else
454 break;
456 Fset_keymap_parent (submap_parent, parent_entry);
460 /* Look up IDX in MAP. IDX may be any sort of event.
461 Note that this does only one level of lookup; IDX must be a single
462 event, not a sequence.
464 If T_OK is non-zero, bindings for Qt are treated as default
465 bindings; any key left unmentioned by other tables and bindings is
466 given the binding of Qt.
468 If T_OK is zero, bindings for Qt are not treated specially.
470 If NOINHERIT, don't accept a subkeymap found in an inherited keymap. */
472 Lisp_Object
473 access_keymap (map, idx, t_ok, noinherit)
474 Lisp_Object map;
475 Lisp_Object idx;
476 int t_ok;
477 int noinherit;
479 int noprefix = 0;
480 Lisp_Object val;
482 /* If idx is a list (some sort of mouse click, perhaps?),
483 the index we want to use is the car of the list, which
484 ought to be a symbol. */
485 idx = EVENT_HEAD (idx);
487 /* If idx is a symbol, it might have modifiers, which need to
488 be put in the canonical order. */
489 if (SYMBOLP (idx))
490 idx = reorder_modifiers (idx);
491 else if (INTEGERP (idx))
492 /* Clobber the high bits that can be present on a machine
493 with more than 24 bits of integer. */
494 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
497 Lisp_Object tail;
498 Lisp_Object t_binding;
500 t_binding = Qnil;
501 for (tail = map; CONSP (tail); tail = XCDR (tail))
503 Lisp_Object binding;
505 binding = XCAR (tail);
506 if (SYMBOLP (binding))
508 /* If NOINHERIT, stop finding prefix definitions
509 after we pass a second occurrence of the `keymap' symbol. */
510 if (noinherit && EQ (binding, Qkeymap) && ! EQ (tail, map))
511 noprefix = 1;
513 else if (CONSP (binding))
515 if (EQ (XCAR (binding), idx))
517 val = XCDR (binding);
518 if (noprefix && CONSP (val) && EQ (XCAR (val), Qkeymap))
519 return Qnil;
520 if (CONSP (val))
521 fix_submap_inheritance (map, idx, val);
522 return val;
524 if (t_ok && EQ (XCAR (binding), Qt))
525 t_binding = XCDR (binding);
527 else if (VECTORP (binding))
529 if (NATNUMP (idx) && XFASTINT (idx) < XVECTOR (binding)->size)
531 val = XVECTOR (binding)->contents[XFASTINT (idx)];
532 if (noprefix && CONSP (val) && EQ (XCAR (val), Qkeymap))
533 return Qnil;
534 if (CONSP (val))
535 fix_submap_inheritance (map, idx, val);
536 return val;
539 else if (CHAR_TABLE_P (binding))
541 /* Character codes with modifiers
542 are not included in a char-table.
543 All character codes without modifiers are included. */
544 if (NATNUMP (idx)
545 && ! (XFASTINT (idx)
546 & (CHAR_ALT | CHAR_SUPER | CHAR_HYPER
547 | CHAR_SHIFT | CHAR_CTL | CHAR_META)))
549 val = Faref (binding, idx);
550 if (noprefix && CONSP (val) && EQ (XCAR (val), Qkeymap))
551 return Qnil;
552 if (CONSP (val))
553 fix_submap_inheritance (map, idx, val);
554 return val;
558 QUIT;
561 return t_binding;
565 /* Given OBJECT which was found in a slot in a keymap,
566 trace indirect definitions to get the actual definition of that slot.
567 An indirect definition is a list of the form
568 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
569 and INDEX is the object to look up in KEYMAP to yield the definition.
571 Also if OBJECT has a menu string as the first element,
572 remove that. Also remove a menu help string as second element.
574 If AUTOLOAD is nonzero, load autoloadable keymaps
575 that are referred to with indirection. */
577 Lisp_Object
578 get_keyelt (object, autoload)
579 register Lisp_Object object;
580 int autoload;
582 while (1)
584 if (!(CONSP (object)))
585 /* This is really the value. */
586 return object;
588 /* If the keymap contents looks like (keymap ...) or (lambda ...)
589 then use itself. */
590 else if (EQ (XCAR (object), Qkeymap) || EQ (XCAR (object), Qlambda))
591 return object;
593 /* If the keymap contents looks like (menu-item name . DEFN)
594 or (menu-item name DEFN ...) then use DEFN.
595 This is a new format menu item. */
596 else if (EQ (XCAR (object), Qmenu_item))
598 if (CONSP (XCDR (object)))
600 Lisp_Object tem;
602 object = XCDR (XCDR (object));
603 tem = object;
604 if (CONSP (object))
605 object = XCAR (object);
607 /* If there's a `:filter FILTER', apply FILTER to the
608 menu-item's definition to get the real definition to
609 use. Temporarily inhibit GC while evaluating FILTER,
610 because not functions calling get_keyelt are prepared
611 for a GC. */
612 for (; CONSP (tem) && CONSP (XCDR (tem)); tem = XCDR (tem))
613 if (EQ (XCAR (tem), QCfilter))
615 int count = inhibit_garbage_collection ();
616 Lisp_Object filter;
617 filter = XCAR (XCDR (tem));
618 filter = list2 (filter, list2 (Qquote, object));
619 object = menu_item_eval_property (filter);
620 unbind_to (count, Qnil);
621 break;
624 else
625 /* Invalid keymap */
626 return object;
629 /* If the keymap contents looks like (STRING . DEFN), use DEFN.
630 Keymap alist elements like (CHAR MENUSTRING . DEFN)
631 will be used by HierarKey menus. */
632 else if (STRINGP (XCAR (object)))
634 object = XCDR (object);
635 /* Also remove a menu help string, if any,
636 following the menu item name. */
637 if (CONSP (object) && STRINGP (XCAR (object)))
638 object = XCDR (object);
639 /* Also remove the sublist that caches key equivalences, if any. */
640 if (CONSP (object) && CONSP (XCAR (object)))
642 Lisp_Object carcar;
643 carcar = XCAR (XCAR (object));
644 if (NILP (carcar) || VECTORP (carcar))
645 object = XCDR (object);
649 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
650 else
652 Lisp_Object map;
654 map = get_keymap_1 (Fcar_safe (object), 0, autoload);
655 if (NILP (map))
656 /* Invalid keymap */
657 return object;
658 else
660 Lisp_Object key;
661 key = Fcdr (object);
662 if (INTEGERP (key) && (XUINT (key) & meta_modifier))
664 object = access_keymap (map, meta_prefix_char, 0, 0);
665 map = get_keymap_1 (object, 0, autoload);
666 object = access_keymap (map, make_number (XINT (key)
667 & ~meta_modifier),
668 0, 0);
670 else
671 object = access_keymap (map, key, 0, 0);
677 static Lisp_Object
678 store_in_keymap (keymap, idx, def)
679 Lisp_Object keymap;
680 register Lisp_Object idx;
681 register Lisp_Object def;
683 /* If we are preparing to dump, and DEF is a menu element
684 with a menu item indicator, copy it to ensure it is not pure. */
685 if (CONSP (def) && PURE_P (def)
686 && (EQ (XCAR (def), Qmenu_item) || STRINGP (XCAR (def))))
687 def = Fcons (XCAR (def), XCDR (def));
689 if (!CONSP (keymap) || ! EQ (XCAR (keymap), Qkeymap))
690 error ("attempt to define a key in a non-keymap");
692 /* If idx is a list (some sort of mouse click, perhaps?),
693 the index we want to use is the car of the list, which
694 ought to be a symbol. */
695 idx = EVENT_HEAD (idx);
697 /* If idx is a symbol, it might have modifiers, which need to
698 be put in the canonical order. */
699 if (SYMBOLP (idx))
700 idx = reorder_modifiers (idx);
701 else if (INTEGERP (idx))
702 /* Clobber the high bits that can be present on a machine
703 with more than 24 bits of integer. */
704 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
706 /* Scan the keymap for a binding of idx. */
708 Lisp_Object tail;
710 /* The cons after which we should insert new bindings. If the
711 keymap has a table element, we record its position here, so new
712 bindings will go after it; this way, the table will stay
713 towards the front of the alist and character lookups in dense
714 keymaps will remain fast. Otherwise, this just points at the
715 front of the keymap. */
716 Lisp_Object insertion_point;
718 insertion_point = keymap;
719 for (tail = XCDR (keymap); CONSP (tail); tail = XCDR (tail))
721 Lisp_Object elt;
723 elt = XCAR (tail);
724 if (VECTORP (elt))
726 if (NATNUMP (idx) && XFASTINT (idx) < XVECTOR (elt)->size)
728 XVECTOR (elt)->contents[XFASTINT (idx)] = def;
729 return def;
731 insertion_point = tail;
733 else if (CHAR_TABLE_P (elt))
735 /* Character codes with modifiers
736 are not included in a char-table.
737 All character codes without modifiers are included. */
738 if (NATNUMP (idx)
739 && ! (XFASTINT (idx)
740 & (CHAR_ALT | CHAR_SUPER | CHAR_HYPER
741 | CHAR_SHIFT | CHAR_CTL | CHAR_META)))
743 Faset (elt, idx, def);
744 return def;
746 insertion_point = tail;
748 else if (CONSP (elt))
750 if (EQ (idx, XCAR (elt)))
752 XCDR (elt) = def;
753 return def;
756 else if (SYMBOLP (elt))
758 /* If we find a 'keymap' symbol in the spine of KEYMAP,
759 then we must have found the start of a second keymap
760 being used as the tail of KEYMAP, and a binding for IDX
761 should be inserted before it. */
762 if (EQ (elt, Qkeymap))
763 goto keymap_end;
766 QUIT;
769 keymap_end:
770 /* We have scanned the entire keymap, and not found a binding for
771 IDX. Let's add one. */
772 XCDR (insertion_point)
773 = Fcons (Fcons (idx, def), XCDR (insertion_point));
776 return def;
779 void
780 copy_keymap_1 (chartable, idx, elt)
781 Lisp_Object chartable, idx, elt;
783 if (!SYMBOLP (elt) && ! NILP (Fkeymapp (elt)))
784 Faset (chartable, idx, Fcopy_keymap (elt));
787 DEFUN ("copy-keymap", Fcopy_keymap, Scopy_keymap, 1, 1, 0,
788 "Return a copy of the keymap KEYMAP.\n\
789 The copy starts out with the same definitions of KEYMAP,\n\
790 but changing either the copy or KEYMAP does not affect the other.\n\
791 Any key definitions that are subkeymaps are recursively copied.\n\
792 However, a key definition which is a symbol whose definition is a keymap\n\
793 is not copied.")
794 (keymap)
795 Lisp_Object keymap;
797 register Lisp_Object copy, tail;
799 copy = Fcopy_alist (get_keymap (keymap));
801 for (tail = copy; CONSP (tail); tail = XCDR (tail))
803 Lisp_Object elt;
805 elt = XCAR (tail);
806 if (CHAR_TABLE_P (elt))
808 Lisp_Object indices[3];
810 elt = Fcopy_sequence (elt);
811 XCAR (tail) = elt;
813 map_char_table (copy_keymap_1, Qnil, elt, elt, 0, indices);
815 else if (VECTORP (elt))
817 int i;
819 elt = Fcopy_sequence (elt);
820 XCAR (tail) = elt;
822 for (i = 0; i < XVECTOR (elt)->size; i++)
823 if (!SYMBOLP (XVECTOR (elt)->contents[i])
824 && ! NILP (Fkeymapp (XVECTOR (elt)->contents[i])))
825 XVECTOR (elt)->contents[i]
826 = Fcopy_keymap (XVECTOR (elt)->contents[i]);
828 else if (CONSP (elt) && CONSP (XCDR (elt)))
830 Lisp_Object tem;
831 tem = XCDR (elt);
833 /* Is this a new format menu item. */
834 if (EQ (XCAR (tem),Qmenu_item))
836 /* Copy cell with menu-item marker. */
837 XCDR (elt)
838 = Fcons (XCAR (tem), XCDR (tem));
839 elt = XCDR (elt);
840 tem = XCDR (elt);
841 if (CONSP (tem))
843 /* Copy cell with menu-item name. */
844 XCDR (elt)
845 = Fcons (XCAR (tem), XCDR (tem));
846 elt = XCDR (elt);
847 tem = XCDR (elt);
849 if (CONSP (tem))
851 /* Copy cell with binding and if the binding is a keymap,
852 copy that. */
853 XCDR (elt)
854 = Fcons (XCAR (tem), XCDR (tem));
855 elt = XCDR (elt);
856 tem = XCAR (elt);
857 if (!(SYMBOLP (tem) || NILP (Fkeymapp (tem))))
858 XCAR (elt) = Fcopy_keymap (tem);
859 tem = XCDR (elt);
860 if (CONSP (tem) && CONSP (XCAR (tem)))
861 /* Delete cache for key equivalences. */
862 XCDR (elt) = XCDR (tem);
865 else
867 /* It may be an old fomat menu item.
868 Skip the optional menu string.
870 if (STRINGP (XCAR (tem)))
872 /* Copy the cell, since copy-alist didn't go this deep. */
873 XCDR (elt)
874 = Fcons (XCAR (tem), XCDR (tem));
875 elt = XCDR (elt);
876 tem = XCDR (elt);
877 /* Also skip the optional menu help string. */
878 if (CONSP (tem) && STRINGP (XCAR (tem)))
880 XCDR (elt)
881 = Fcons (XCAR (tem), XCDR (tem));
882 elt = XCDR (elt);
883 tem = XCDR (elt);
885 /* There may also be a list that caches key equivalences.
886 Just delete it for the new keymap. */
887 if (CONSP (tem)
888 && CONSP (XCAR (tem))
889 && (NILP (XCAR (XCAR (tem)))
890 || VECTORP (XCAR (XCAR (tem)))))
891 XCDR (elt) = XCDR (tem);
893 if (CONSP (elt)
894 && ! SYMBOLP (XCDR (elt))
895 && ! NILP (Fkeymapp (XCDR (elt))))
896 XCDR (elt) = Fcopy_keymap (XCDR (elt));
902 return copy;
905 /* Simple Keymap mutators and accessors. */
907 /* GC is possible in this function if it autoloads a keymap. */
909 DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0,
910 "Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.\n\
911 KEYMAP is a keymap. KEY is a string or a vector of symbols and characters\n\
912 meaning a sequence of keystrokes and events.\n\
913 Non-ASCII characters with codes above 127 (such as ISO Latin-1)\n\
914 can be included if you use a vector.\n\
915 DEF is anything that can be a key's definition:\n\
916 nil (means key is undefined in this keymap),\n\
917 a command (a Lisp function suitable for interactive calling)\n\
918 a string (treated as a keyboard macro),\n\
919 a keymap (to define a prefix key),\n\
920 a symbol. When the key is looked up, the symbol will stand for its\n\
921 function definition, which should at that time be one of the above,\n\
922 or another symbol whose function definition is used, etc.\n\
923 a cons (STRING . DEFN), meaning that DEFN is the definition\n\
924 (DEFN should be a valid definition in its own right),\n\
925 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.\n\
927 If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at\n\
928 the front of KEYMAP.")
929 (keymap, key, def)
930 Lisp_Object keymap;
931 Lisp_Object key;
932 Lisp_Object def;
934 register int idx;
935 register Lisp_Object c;
936 register Lisp_Object cmd;
937 int metized = 0;
938 int meta_bit;
939 int length;
940 struct gcpro gcpro1, gcpro2, gcpro3;
942 keymap = get_keymap_1 (keymap, 1, 1);
944 if (!VECTORP (key) && !STRINGP (key))
945 key = wrong_type_argument (Qarrayp, key);
947 length = XFASTINT (Flength (key));
948 if (length == 0)
949 return Qnil;
951 if (SYMBOLP (def) && !EQ (Vdefine_key_rebound_commands, Qt))
952 Vdefine_key_rebound_commands = Fcons (def, Vdefine_key_rebound_commands);
954 GCPRO3 (keymap, key, def);
956 if (VECTORP (key))
957 meta_bit = meta_modifier;
958 else
959 meta_bit = 0x80;
961 idx = 0;
962 while (1)
964 c = Faref (key, make_number (idx));
966 if (CONSP (c) && lucid_event_type_list_p (c))
967 c = Fevent_convert_list (c);
969 if (INTEGERP (c)
970 && (XINT (c) & meta_bit)
971 && !metized)
973 c = meta_prefix_char;
974 metized = 1;
976 else
978 if (INTEGERP (c))
979 XSETINT (c, XINT (c) & ~meta_bit);
981 metized = 0;
982 idx++;
985 if (! INTEGERP (c) && ! SYMBOLP (c) && ! CONSP (c))
986 error ("Key sequence contains invalid events");
988 if (idx == length)
989 RETURN_UNGCPRO (store_in_keymap (keymap, c, def));
991 cmd = get_keyelt (access_keymap (keymap, c, 0, 1), 1);
993 /* If this key is undefined, make it a prefix. */
994 if (NILP (cmd))
995 cmd = define_as_prefix (keymap, c);
997 keymap = get_keymap_1 (cmd, 0, 1);
998 if (NILP (keymap))
999 /* We must use Fkey_description rather than just passing key to
1000 error; key might be a vector, not a string. */
1001 error ("Key sequence %s uses invalid prefix characters",
1002 XSTRING (Fkey_description (key))->data);
1006 /* Value is number if KEY is too long; NIL if valid but has no definition. */
1007 /* GC is possible in this function if it autoloads a keymap. */
1009 DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 3, 0,
1010 "In keymap KEYMAP, look up key sequence KEY. Return the definition.\n\
1011 nil means undefined. See doc of `define-key' for kinds of definitions.\n\
1013 A number as value means KEY is \"too long\";\n\
1014 that is, characters or symbols in it except for the last one\n\
1015 fail to be a valid sequence of prefix characters in KEYMAP.\n\
1016 The number is how many characters at the front of KEY\n\
1017 it takes to reach a non-prefix command.\n\
1019 Normally, `lookup-key' ignores bindings for t, which act as default\n\
1020 bindings, used when nothing else in the keymap applies; this makes it\n\
1021 usable as a general function for probing keymaps. However, if the\n\
1022 third optional argument ACCEPT-DEFAULT is non-nil, `lookup-key' will\n\
1023 recognize the default bindings, just as `read-key-sequence' does.")
1024 (keymap, key, accept_default)
1025 register Lisp_Object keymap;
1026 Lisp_Object key;
1027 Lisp_Object accept_default;
1029 register int idx;
1030 register Lisp_Object cmd;
1031 register Lisp_Object c;
1032 int metized = 0;
1033 int length;
1034 int t_ok = ! NILP (accept_default);
1035 int meta_bit;
1036 struct gcpro gcpro1;
1038 keymap = get_keymap_1 (keymap, 1, 1);
1040 if (!VECTORP (key) && !STRINGP (key))
1041 key = wrong_type_argument (Qarrayp, key);
1043 length = XFASTINT (Flength (key));
1044 if (length == 0)
1045 return keymap;
1047 if (VECTORP (key))
1048 meta_bit = meta_modifier;
1049 else
1050 meta_bit = 0x80;
1052 GCPRO1 (key);
1054 idx = 0;
1055 while (1)
1057 c = Faref (key, make_number (idx));
1059 if (CONSP (c) && lucid_event_type_list_p (c))
1060 c = Fevent_convert_list (c);
1062 if (INTEGERP (c)
1063 && (XINT (c) & meta_bit)
1064 && !metized)
1066 c = meta_prefix_char;
1067 metized = 1;
1069 else
1071 if (INTEGERP (c))
1072 XSETINT (c, XINT (c) & ~meta_bit);
1074 metized = 0;
1075 idx++;
1078 cmd = get_keyelt (access_keymap (keymap, c, t_ok, 0), 1);
1079 if (idx == length)
1080 RETURN_UNGCPRO (cmd);
1082 keymap = get_keymap_1 (cmd, 0, 1);
1083 if (NILP (keymap))
1084 RETURN_UNGCPRO (make_number (idx));
1086 QUIT;
1090 /* Make KEYMAP define event C as a keymap (i.e., as a prefix).
1091 Assume that currently it does not define C at all.
1092 Return the keymap. */
1094 static Lisp_Object
1095 define_as_prefix (keymap, c)
1096 Lisp_Object keymap, c;
1098 Lisp_Object inherit, cmd;
1100 cmd = Fmake_sparse_keymap (Qnil);
1101 /* If this key is defined as a prefix in an inherited keymap,
1102 make it a prefix in this map, and make its definition
1103 inherit the other prefix definition. */
1104 inherit = access_keymap (keymap, c, 0, 0);
1105 #if 0
1106 /* This code is needed to do the right thing in the following case:
1107 keymap A inherits from B,
1108 you define KEY as a prefix in A,
1109 then later you define KEY as a prefix in B.
1110 We want the old prefix definition in A to inherit from that in B.
1111 It is hard to do that retroactively, so this code
1112 creates the prefix in B right away.
1114 But it turns out that this code causes problems immediately
1115 when the prefix in A is defined: it causes B to define KEY
1116 as a prefix with no subcommands.
1118 So I took out this code. */
1119 if (NILP (inherit))
1121 /* If there's an inherited keymap
1122 and it doesn't define this key,
1123 make it define this key. */
1124 Lisp_Object tail;
1126 for (tail = Fcdr (keymap); CONSP (tail); tail = XCDR (tail))
1127 if (EQ (XCAR (tail), Qkeymap))
1128 break;
1130 if (!NILP (tail))
1131 inherit = define_as_prefix (tail, c);
1133 #endif
1135 cmd = nconc2 (cmd, inherit);
1136 store_in_keymap (keymap, c, cmd);
1138 return cmd;
1141 /* Append a key to the end of a key sequence. We always make a vector. */
1143 Lisp_Object
1144 append_key (key_sequence, key)
1145 Lisp_Object key_sequence, key;
1147 Lisp_Object args[2];
1149 args[0] = key_sequence;
1151 args[1] = Fcons (key, Qnil);
1152 return Fvconcat (2, args);
1156 /* Global, local, and minor mode keymap stuff. */
1158 /* We can't put these variables inside current_minor_maps, since under
1159 some systems, static gets macro-defined to be the empty string.
1160 Ickypoo. */
1161 static Lisp_Object *cmm_modes, *cmm_maps;
1162 static int cmm_size;
1164 /* Error handler used in current_minor_maps. */
1165 static Lisp_Object
1166 current_minor_maps_error ()
1168 return Qnil;
1171 /* Store a pointer to an array of the keymaps of the currently active
1172 minor modes in *buf, and return the number of maps it contains.
1174 This function always returns a pointer to the same buffer, and may
1175 free or reallocate it, so if you want to keep it for a long time or
1176 hand it out to lisp code, copy it. This procedure will be called
1177 for every key sequence read, so the nice lispy approach (return a
1178 new assoclist, list, what have you) for each invocation would
1179 result in a lot of consing over time.
1181 If we used xrealloc/xmalloc and ran out of memory, they would throw
1182 back to the command loop, which would try to read a key sequence,
1183 which would call this function again, resulting in an infinite
1184 loop. Instead, we'll use realloc/malloc and silently truncate the
1185 list, let the key sequence be read, and hope some other piece of
1186 code signals the error. */
1188 current_minor_maps (modeptr, mapptr)
1189 Lisp_Object **modeptr, **mapptr;
1191 int i = 0;
1192 int list_number = 0;
1193 Lisp_Object alist, assoc, var, val;
1194 Lisp_Object lists[2];
1196 lists[0] = Vminor_mode_overriding_map_alist;
1197 lists[1] = Vminor_mode_map_alist;
1199 for (list_number = 0; list_number < 2; list_number++)
1200 for (alist = lists[list_number];
1201 CONSP (alist);
1202 alist = XCDR (alist))
1203 if ((assoc = XCAR (alist), CONSP (assoc))
1204 && (var = XCAR (assoc), SYMBOLP (var))
1205 && (val = find_symbol_value (var), ! EQ (val, Qunbound))
1206 && ! NILP (val))
1208 Lisp_Object temp;
1210 /* If a variable has an entry in Vminor_mode_overriding_map_alist,
1211 and also an entry in Vminor_mode_map_alist,
1212 ignore the latter. */
1213 if (list_number == 1)
1215 val = assq_no_quit (var, lists[0]);
1216 if (!NILP (val))
1217 break;
1220 if (i >= cmm_size)
1222 Lisp_Object *newmodes, *newmaps;
1224 if (cmm_maps)
1226 BLOCK_INPUT;
1227 cmm_size *= 2;
1228 newmodes
1229 = (Lisp_Object *) realloc (cmm_modes,
1230 cmm_size * sizeof (Lisp_Object));
1231 newmaps
1232 = (Lisp_Object *) realloc (cmm_maps,
1233 cmm_size * sizeof (Lisp_Object));
1234 UNBLOCK_INPUT;
1236 else
1238 BLOCK_INPUT;
1239 cmm_size = 30;
1240 newmodes
1241 = (Lisp_Object *) xmalloc (cmm_size * sizeof (Lisp_Object));
1242 newmaps
1243 = (Lisp_Object *) xmalloc (cmm_size * sizeof (Lisp_Object));
1244 UNBLOCK_INPUT;
1247 if (newmaps && newmodes)
1249 cmm_modes = newmodes;
1250 cmm_maps = newmaps;
1252 else
1253 break;
1256 /* Get the keymap definition--or nil if it is not defined. */
1257 temp = internal_condition_case_1 (Findirect_function,
1258 XCDR (assoc),
1259 Qerror, current_minor_maps_error);
1260 if (!NILP (temp))
1262 cmm_modes[i] = var;
1263 cmm_maps [i] = temp;
1264 i++;
1268 if (modeptr) *modeptr = cmm_modes;
1269 if (mapptr) *mapptr = cmm_maps;
1270 return i;
1273 /* GC is possible in this function if it autoloads a keymap. */
1275 DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 2, 0,
1276 "Return the binding for command KEY in current keymaps.\n\
1277 KEY is a string or vector, a sequence of keystrokes.\n\
1278 The binding is probably a symbol with a function definition.\n\
1280 Normally, `key-binding' ignores bindings for t, which act as default\n\
1281 bindings, used when nothing else in the keymap applies; this makes it\n\
1282 usable as a general function for probing keymaps. However, if the\n\
1283 optional second argument ACCEPT-DEFAULT is non-nil, `key-binding' does\n\
1284 recognize the default bindings, just as `read-key-sequence' does.")
1285 (key, accept_default)
1286 Lisp_Object key, accept_default;
1288 Lisp_Object *maps, value;
1289 int nmaps, i;
1290 struct gcpro gcpro1;
1292 GCPRO1 (key);
1294 if (!NILP (current_kboard->Voverriding_terminal_local_map))
1296 value = Flookup_key (current_kboard->Voverriding_terminal_local_map,
1297 key, accept_default);
1298 if (! NILP (value) && !INTEGERP (value))
1299 RETURN_UNGCPRO (value);
1301 else if (!NILP (Voverriding_local_map))
1303 value = Flookup_key (Voverriding_local_map, key, accept_default);
1304 if (! NILP (value) && !INTEGERP (value))
1305 RETURN_UNGCPRO (value);
1307 else
1309 Lisp_Object local;
1311 nmaps = current_minor_maps (0, &maps);
1312 /* Note that all these maps are GCPRO'd
1313 in the places where we found them. */
1315 for (i = 0; i < nmaps; i++)
1316 if (! NILP (maps[i]))
1318 value = Flookup_key (maps[i], key, accept_default);
1319 if (! NILP (value) && !INTEGERP (value))
1320 RETURN_UNGCPRO (value);
1323 local = get_local_map (PT, current_buffer, keymap);
1324 if (! NILP (local))
1326 value = Flookup_key (local, key, accept_default);
1327 if (! NILP (value) && !INTEGERP (value))
1328 RETURN_UNGCPRO (value);
1331 local = get_local_map (PT, current_buffer, local_map);
1333 if (! NILP (local))
1335 value = Flookup_key (local, key, accept_default);
1336 if (! NILP (value) && !INTEGERP (value))
1337 RETURN_UNGCPRO (value);
1341 value = Flookup_key (current_global_map, key, accept_default);
1342 UNGCPRO;
1343 if (! NILP (value) && !INTEGERP (value))
1344 return value;
1346 return Qnil;
1349 /* GC is possible in this function if it autoloads a keymap. */
1351 DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 2, 0,
1352 "Return the binding for command KEYS in current local keymap only.\n\
1353 KEYS is a string, a sequence of keystrokes.\n\
1354 The binding is probably a symbol with a function definition.\n\
1356 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1357 bindings; see the description of `lookup-key' for more details about this.")
1358 (keys, accept_default)
1359 Lisp_Object keys, accept_default;
1361 register Lisp_Object map;
1362 map = current_buffer->keymap;
1363 if (NILP (map))
1364 return Qnil;
1365 return Flookup_key (map, keys, accept_default);
1368 /* GC is possible in this function if it autoloads a keymap. */
1370 DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 2, 0,
1371 "Return the binding for command KEYS in current global keymap only.\n\
1372 KEYS is a string, a sequence of keystrokes.\n\
1373 The binding is probably a symbol with a function definition.\n\
1374 This function's return values are the same as those of lookup-key\n\
1375 \(which see).\n\
1377 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1378 bindings; see the description of `lookup-key' for more details about this.")
1379 (keys, accept_default)
1380 Lisp_Object keys, accept_default;
1382 return Flookup_key (current_global_map, keys, accept_default);
1385 /* GC is possible in this function if it autoloads a keymap. */
1387 DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0,
1388 "Find the visible minor mode bindings of KEY.\n\
1389 Return an alist of pairs (MODENAME . BINDING), where MODENAME is the\n\
1390 the symbol which names the minor mode binding KEY, and BINDING is\n\
1391 KEY's definition in that mode. In particular, if KEY has no\n\
1392 minor-mode bindings, return nil. If the first binding is a\n\
1393 non-prefix, all subsequent bindings will be omitted, since they would\n\
1394 be ignored. Similarly, the list doesn't include non-prefix bindings\n\
1395 that come after prefix bindings.\n\
1397 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1398 bindings; see the description of `lookup-key' for more details about this.")
1399 (key, accept_default)
1400 Lisp_Object key, accept_default;
1402 Lisp_Object *modes, *maps;
1403 int nmaps;
1404 Lisp_Object binding;
1405 int i, j;
1406 struct gcpro gcpro1, gcpro2;
1408 nmaps = current_minor_maps (&modes, &maps);
1409 /* Note that all these maps are GCPRO'd
1410 in the places where we found them. */
1412 binding = Qnil;
1413 GCPRO2 (key, binding);
1415 for (i = j = 0; i < nmaps; i++)
1416 if (! NILP (maps[i])
1417 && ! NILP (binding = Flookup_key (maps[i], key, accept_default))
1418 && !INTEGERP (binding))
1420 if (! NILP (get_keymap (binding)))
1421 maps[j++] = Fcons (modes[i], binding);
1422 else if (j == 0)
1423 RETURN_UNGCPRO (Fcons (Fcons (modes[i], binding), Qnil));
1426 UNGCPRO;
1427 return Flist (j, maps);
1430 DEFUN ("define-prefix-command", Fdefine_prefix_command, Sdefine_prefix_command, 1, 3, 0,
1431 "Define COMMAND as a prefix command. COMMAND should be a symbol.\n\
1432 A new sparse keymap is stored as COMMAND's function definition and its value.\n\
1433 If a second optional argument MAPVAR is given, the map is stored as\n\
1434 its value instead of as COMMAND's value; but COMMAND is still defined\n\
1435 as a function.\n\
1436 The third optional argument NAME, if given, supplies a menu name\n\
1437 string for the map. This is required to use the keymap as a menu.")
1438 (command, mapvar, name)
1439 Lisp_Object command, mapvar, name;
1441 Lisp_Object map;
1442 map = Fmake_sparse_keymap (name);
1443 Ffset (command, map);
1444 if (!NILP (mapvar))
1445 Fset (mapvar, map);
1446 else
1447 Fset (command, map);
1448 return command;
1451 DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0,
1452 "Select KEYMAP as the global keymap.")
1453 (keymap)
1454 Lisp_Object keymap;
1456 keymap = get_keymap (keymap);
1457 current_global_map = keymap;
1459 return Qnil;
1462 DEFUN ("use-local-map", Fuse_local_map, Suse_local_map, 1, 1, 0,
1463 "Select KEYMAP as the local keymap.\n\
1464 If KEYMAP is nil, that means no local keymap.")
1465 (keymap)
1466 Lisp_Object keymap;
1468 if (!NILP (keymap))
1469 keymap = get_keymap (keymap);
1471 current_buffer->keymap = keymap;
1473 return Qnil;
1476 DEFUN ("current-local-map", Fcurrent_local_map, Scurrent_local_map, 0, 0, 0,
1477 "Return current buffer's local keymap, or nil if it has none.")
1480 return current_buffer->keymap;
1483 DEFUN ("current-global-map", Fcurrent_global_map, Scurrent_global_map, 0, 0, 0,
1484 "Return the current global keymap.")
1487 return current_global_map;
1490 DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps, Scurrent_minor_mode_maps, 0, 0, 0,
1491 "Return a list of keymaps for the minor modes of the current buffer.")
1494 Lisp_Object *maps;
1495 int nmaps = current_minor_maps (0, &maps);
1497 return Flist (nmaps, maps);
1500 /* Help functions for describing and documenting keymaps. */
1502 static void accessible_keymaps_char_table ();
1504 /* This function cannot GC. */
1506 DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps,
1507 1, 2, 0,
1508 "Find all keymaps accessible via prefix characters from KEYMAP.\n\
1509 Returns a list of elements of the form (KEYS . MAP), where the sequence\n\
1510 KEYS starting from KEYMAP gets you to MAP. These elements are ordered\n\
1511 so that the KEYS increase in length. The first element is ([] . KEYMAP).\n\
1512 An optional argument PREFIX, if non-nil, should be a key sequence;\n\
1513 then the value includes only maps for prefixes that start with PREFIX.")
1514 (keymap, prefix)
1515 Lisp_Object keymap, prefix;
1517 Lisp_Object maps, good_maps, tail;
1518 int prefixlen = 0;
1520 /* no need for gcpro because we don't autoload any keymaps. */
1522 if (!NILP (prefix))
1523 prefixlen = XINT (Flength (prefix));
1525 if (!NILP (prefix))
1527 /* If a prefix was specified, start with the keymap (if any) for
1528 that prefix, so we don't waste time considering other prefixes. */
1529 Lisp_Object tem;
1530 tem = Flookup_key (keymap, prefix, Qt);
1531 /* Flookup_key may give us nil, or a number,
1532 if the prefix is not defined in this particular map.
1533 It might even give us a list that isn't a keymap. */
1534 tem = get_keymap_1 (tem, 0, 0);
1535 if (!NILP (tem))
1537 /* Convert PREFIX to a vector now, so that later on
1538 we don't have to deal with the possibility of a string. */
1539 if (STRINGP (prefix))
1541 int i, i_byte, c;
1542 Lisp_Object copy;
1544 copy = Fmake_vector (make_number (XSTRING (prefix)->size), Qnil);
1545 for (i = 0, i_byte = 0; i < XSTRING (prefix)->size;)
1547 int i_before = i;
1549 FETCH_STRING_CHAR_ADVANCE (c, prefix, i, i_byte);
1550 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
1551 c ^= 0200 | meta_modifier;
1552 XVECTOR (copy)->contents[i_before] = make_number (c);
1554 prefix = copy;
1556 maps = Fcons (Fcons (prefix, tem), Qnil);
1558 else
1559 return Qnil;
1561 else
1562 maps = Fcons (Fcons (Fmake_vector (make_number (0), Qnil),
1563 get_keymap (keymap)),
1564 Qnil);
1566 /* For each map in the list maps,
1567 look at any other maps it points to,
1568 and stick them at the end if they are not already in the list.
1570 This is a breadth-first traversal, where tail is the queue of
1571 nodes, and maps accumulates a list of all nodes visited. */
1573 for (tail = maps; CONSP (tail); tail = XCDR (tail))
1575 register Lisp_Object thisseq, thismap;
1576 Lisp_Object last;
1577 /* Does the current sequence end in the meta-prefix-char? */
1578 int is_metized;
1580 thisseq = Fcar (Fcar (tail));
1581 thismap = Fcdr (Fcar (tail));
1582 last = make_number (XINT (Flength (thisseq)) - 1);
1583 is_metized = (XINT (last) >= 0
1584 /* Don't metize the last char of PREFIX. */
1585 && XINT (last) >= prefixlen
1586 && EQ (Faref (thisseq, last), meta_prefix_char));
1588 for (; CONSP (thismap); thismap = XCDR (thismap))
1590 Lisp_Object elt;
1592 elt = XCAR (thismap);
1594 QUIT;
1596 if (CHAR_TABLE_P (elt))
1598 Lisp_Object indices[3];
1600 map_char_table (accessible_keymaps_char_table, Qnil,
1601 elt, Fcons (maps, Fcons (tail, thisseq)),
1602 0, indices);
1604 else if (VECTORP (elt))
1606 register int i;
1608 /* Vector keymap. Scan all the elements. */
1609 for (i = 0; i < XVECTOR (elt)->size; i++)
1611 register Lisp_Object tem;
1612 register Lisp_Object cmd;
1614 cmd = get_keyelt (XVECTOR (elt)->contents[i], 0);
1615 if (NILP (cmd)) continue;
1616 tem = Fkeymapp (cmd);
1617 if (!NILP (tem))
1619 cmd = get_keymap (cmd);
1620 /* Ignore keymaps that are already added to maps. */
1621 tem = Frassq (cmd, maps);
1622 if (NILP (tem))
1624 /* If the last key in thisseq is meta-prefix-char,
1625 turn it into a meta-ized keystroke. We know
1626 that the event we're about to append is an
1627 ascii keystroke since we're processing a
1628 keymap table. */
1629 if (is_metized)
1631 int meta_bit = meta_modifier;
1632 tem = Fcopy_sequence (thisseq);
1634 Faset (tem, last, make_number (i | meta_bit));
1636 /* This new sequence is the same length as
1637 thisseq, so stick it in the list right
1638 after this one. */
1639 XCDR (tail)
1640 = Fcons (Fcons (tem, cmd), XCDR (tail));
1642 else
1644 tem = append_key (thisseq, make_number (i));
1645 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
1651 else if (CONSP (elt))
1653 register Lisp_Object cmd, tem;
1655 cmd = get_keyelt (XCDR (elt), 0);
1656 /* Ignore definitions that aren't keymaps themselves. */
1657 tem = Fkeymapp (cmd);
1658 if (!NILP (tem))
1660 /* Ignore keymaps that have been seen already. */
1661 cmd = get_keymap (cmd);
1662 tem = Frassq (cmd, maps);
1663 if (NILP (tem))
1665 /* Let elt be the event defined by this map entry. */
1666 elt = XCAR (elt);
1668 /* If the last key in thisseq is meta-prefix-char, and
1669 this entry is a binding for an ascii keystroke,
1670 turn it into a meta-ized keystroke. */
1671 if (is_metized && INTEGERP (elt))
1673 Lisp_Object element;
1675 element = thisseq;
1676 tem = Fvconcat (1, &element);
1677 XSETFASTINT (XVECTOR (tem)->contents[XINT (last)],
1678 XINT (elt) | meta_modifier);
1680 /* This new sequence is the same length as
1681 thisseq, so stick it in the list right
1682 after this one. */
1683 XCDR (tail)
1684 = Fcons (Fcons (tem, cmd), XCDR (tail));
1686 else
1687 nconc2 (tail,
1688 Fcons (Fcons (append_key (thisseq, elt), cmd),
1689 Qnil));
1696 if (NILP (prefix))
1697 return maps;
1699 /* Now find just the maps whose access prefixes start with PREFIX. */
1701 good_maps = Qnil;
1702 for (; CONSP (maps); maps = XCDR (maps))
1704 Lisp_Object elt, thisseq;
1705 elt = XCAR (maps);
1706 thisseq = XCAR (elt);
1707 /* The access prefix must be at least as long as PREFIX,
1708 and the first elements must match those of PREFIX. */
1709 if (XINT (Flength (thisseq)) >= prefixlen)
1711 int i;
1712 for (i = 0; i < prefixlen; i++)
1714 Lisp_Object i1;
1715 XSETFASTINT (i1, i);
1716 if (!EQ (Faref (thisseq, i1), Faref (prefix, i1)))
1717 break;
1719 if (i == prefixlen)
1720 good_maps = Fcons (elt, good_maps);
1724 return Fnreverse (good_maps);
1727 static void
1728 accessible_keymaps_char_table (args, index, cmd)
1729 Lisp_Object args, index, cmd;
1731 Lisp_Object tem;
1732 Lisp_Object maps, tail, thisseq;
1734 if (NILP (cmd))
1735 return;
1737 maps = XCAR (args);
1738 tail = XCAR (XCDR (args));
1739 thisseq = XCDR (XCDR (args));
1741 tem = Fkeymapp (cmd);
1742 if (!NILP (tem))
1744 cmd = get_keymap (cmd);
1745 /* Ignore keymaps that are already added to maps. */
1746 tem = Frassq (cmd, maps);
1747 if (NILP (tem))
1749 tem = append_key (thisseq, index);
1750 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
1755 Lisp_Object Qsingle_key_description, Qkey_description;
1757 /* This function cannot GC. */
1759 DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0,
1760 "Return a pretty description of key-sequence KEYS.\n\
1761 Control characters turn into \"C-foo\" sequences, meta into \"M-foo\"\n\
1762 spaces are put between sequence elements, etc.")
1763 (keys)
1764 Lisp_Object keys;
1766 int len;
1767 int i, i_byte;
1768 Lisp_Object sep;
1769 Lisp_Object *args;
1771 if (STRINGP (keys))
1773 Lisp_Object vector;
1774 vector = Fmake_vector (Flength (keys), Qnil);
1775 for (i = 0, i_byte = 0; i < XSTRING (keys)->size; )
1777 int c;
1778 int i_before = i;
1780 FETCH_STRING_CHAR_ADVANCE (c, keys, i, i_byte);
1781 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
1782 c ^= 0200 | meta_modifier;
1783 XSETFASTINT (XVECTOR (vector)->contents[i_before], c);
1785 keys = vector;
1788 if (VECTORP (keys))
1790 /* In effect, this computes
1791 (mapconcat 'single-key-description keys " ")
1792 but we shouldn't use mapconcat because it can do GC. */
1794 len = XVECTOR (keys)->size;
1795 sep = build_string (" ");
1796 /* This has one extra element at the end that we don't pass to Fconcat. */
1797 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
1799 for (i = 0; i < len; i++)
1801 args[i * 2] = Fsingle_key_description (XVECTOR (keys)->contents[i],
1802 Qnil);
1803 args[i * 2 + 1] = sep;
1806 else if (CONSP (keys))
1808 /* In effect, this computes
1809 (mapconcat 'single-key-description keys " ")
1810 but we shouldn't use mapconcat because it can do GC. */
1812 len = XFASTINT (Flength (keys));
1813 sep = build_string (" ");
1814 /* This has one extra element at the end that we don't pass to Fconcat. */
1815 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
1817 for (i = 0; i < len; i++)
1819 args[i * 2] = Fsingle_key_description (XCAR (keys), Qnil);
1820 args[i * 2 + 1] = sep;
1821 keys = XCDR (keys);
1824 else
1825 keys = wrong_type_argument (Qarrayp, keys);
1827 return Fconcat (len * 2 - 1, args);
1830 char *
1831 push_key_description (c, p)
1832 register unsigned int c;
1833 register char *p;
1835 unsigned c2;
1837 /* Clear all the meaningless bits above the meta bit. */
1838 c &= meta_modifier | ~ - meta_modifier;
1839 c2 = c & ~(alt_modifier | ctrl_modifier | hyper_modifier
1840 | meta_modifier | shift_modifier | super_modifier);
1842 if (c & alt_modifier)
1844 *p++ = 'A';
1845 *p++ = '-';
1846 c -= alt_modifier;
1848 if ((c & ctrl_modifier) != 0
1849 || (c2 < ' ' && c2 != 27 && c2 != '\t' && c2 != Ctl ('M')))
1851 *p++ = 'C';
1852 *p++ = '-';
1853 c &= ~ctrl_modifier;
1855 if (c & hyper_modifier)
1857 *p++ = 'H';
1858 *p++ = '-';
1859 c -= hyper_modifier;
1861 if (c & meta_modifier)
1863 *p++ = 'M';
1864 *p++ = '-';
1865 c -= meta_modifier;
1867 if (c & shift_modifier)
1869 *p++ = 'S';
1870 *p++ = '-';
1871 c -= shift_modifier;
1873 if (c & super_modifier)
1875 *p++ = 's';
1876 *p++ = '-';
1877 c -= super_modifier;
1879 if (c < 040)
1881 if (c == 033)
1883 *p++ = 'E';
1884 *p++ = 'S';
1885 *p++ = 'C';
1887 else if (c == '\t')
1889 *p++ = 'T';
1890 *p++ = 'A';
1891 *p++ = 'B';
1893 else if (c == Ctl ('M'))
1895 *p++ = 'R';
1896 *p++ = 'E';
1897 *p++ = 'T';
1899 else
1901 /* `C-' already added above. */
1902 if (c > 0 && c <= Ctl ('Z'))
1903 *p++ = c + 0140;
1904 else
1905 *p++ = c + 0100;
1908 else if (c == 0177)
1910 *p++ = 'D';
1911 *p++ = 'E';
1912 *p++ = 'L';
1914 else if (c == ' ')
1916 *p++ = 'S';
1917 *p++ = 'P';
1918 *p++ = 'C';
1920 else if (c < 128
1921 || (NILP (current_buffer->enable_multibyte_characters)
1922 && SINGLE_BYTE_CHAR_P (c)))
1923 *p++ = c;
1924 else
1926 if (! NILP (current_buffer->enable_multibyte_characters))
1927 c = unibyte_char_to_multibyte (c);
1929 if (NILP (current_buffer->enable_multibyte_characters)
1930 || SINGLE_BYTE_CHAR_P (c)
1931 || ! char_valid_p (c, 0))
1933 int bit_offset;
1934 *p++ = '\\';
1935 /* The biggest character code uses 19 bits. */
1936 for (bit_offset = 18; bit_offset >= 0; bit_offset -= 3)
1938 if (c >= (1 << bit_offset))
1939 *p++ = ((c & (7 << bit_offset)) >> bit_offset) + '0';
1942 else
1944 p += CHAR_STRING (c, p);
1948 return p;
1951 /* This function cannot GC. */
1953 DEFUN ("single-key-description", Fsingle_key_description,
1954 Ssingle_key_description, 1, 2, 0,
1955 "Return a pretty description of command character KEY.\n\
1956 Control characters turn into C-whatever, etc.\n\
1957 Optional argument NO-ANGLES non-nil means don't put angle brackets\n\
1958 around function keys and event symbols.")
1959 (key, no_angles)
1960 Lisp_Object key, no_angles;
1962 if (CONSP (key) && lucid_event_type_list_p (key))
1963 key = Fevent_convert_list (key);
1965 key = EVENT_HEAD (key);
1967 if (INTEGERP (key)) /* Normal character */
1969 unsigned int charset, c1, c2;
1970 int without_bits = XINT (key) & ~((-1) << CHARACTERBITS);
1972 if (SINGLE_BYTE_CHAR_P (without_bits))
1973 charset = 0;
1974 else
1975 SPLIT_CHAR (without_bits, charset, c1, c2);
1977 if (charset
1978 && CHARSET_DEFINED_P (charset)
1979 && ((c1 >= 0 && c1 < 32)
1980 || (c2 >= 0 && c2 < 32)))
1982 /* Handle a generic character. */
1983 Lisp_Object name;
1984 name = CHARSET_TABLE_INFO (charset, CHARSET_LONG_NAME_IDX);
1985 CHECK_STRING (name, 0);
1986 return concat2 (build_string ("Character set "), name);
1988 else
1990 char tem[KEY_DESCRIPTION_SIZE];
1992 *push_key_description (XUINT (key), tem) = 0;
1993 return build_string (tem);
1996 else if (SYMBOLP (key)) /* Function key or event-symbol */
1998 if (NILP (no_angles))
2000 char *buffer
2001 = (char *) alloca (STRING_BYTES (XSYMBOL (key)->name) + 5);
2002 sprintf (buffer, "<%s>", XSYMBOL (key)->name->data);
2003 return build_string (buffer);
2005 else
2006 return Fsymbol_name (key);
2008 else if (STRINGP (key)) /* Buffer names in the menubar. */
2009 return Fcopy_sequence (key);
2010 else
2011 error ("KEY must be an integer, cons, symbol, or string");
2014 char *
2015 push_text_char_description (c, p)
2016 register unsigned int c;
2017 register char *p;
2019 if (c >= 0200)
2021 *p++ = 'M';
2022 *p++ = '-';
2023 c -= 0200;
2025 if (c < 040)
2027 *p++ = '^';
2028 *p++ = c + 64; /* 'A' - 1 */
2030 else if (c == 0177)
2032 *p++ = '^';
2033 *p++ = '?';
2035 else
2036 *p++ = c;
2037 return p;
2040 /* This function cannot GC. */
2042 DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0,
2043 "Return a pretty description of file-character CHARACTER.\n\
2044 Control characters turn into \"^char\", etc.")
2045 (character)
2046 Lisp_Object character;
2048 /* Currently MAX_MULTIBYTE_LENGTH is 4 (< 6). */
2049 unsigned char str[6];
2050 int c;
2052 CHECK_NUMBER (character, 0);
2054 c = XINT (character);
2055 if (!SINGLE_BYTE_CHAR_P (c))
2057 int len = CHAR_STRING (c, str);
2059 return make_multibyte_string (str, 1, len);
2062 *push_text_char_description (c & 0377, str) = 0;
2064 return build_string (str);
2067 /* Return non-zero if SEQ contains only ASCII characters, perhaps with
2068 a meta bit. */
2069 static int
2070 ascii_sequence_p (seq)
2071 Lisp_Object seq;
2073 int i;
2074 int len = XINT (Flength (seq));
2076 for (i = 0; i < len; i++)
2078 Lisp_Object ii, elt;
2080 XSETFASTINT (ii, i);
2081 elt = Faref (seq, ii);
2083 if (!INTEGERP (elt)
2084 || (XUINT (elt) & ~CHAR_META) >= 0x80)
2085 return 0;
2088 return 1;
2092 /* where-is - finding a command in a set of keymaps. */
2094 static Lisp_Object where_is_internal_1 ();
2095 static void where_is_internal_2 ();
2097 /* This function can GC if Flookup_key autoloads any keymaps. */
2099 DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 4, 0,
2100 "Return list of keys that invoke DEFINITION.\n\
2101 If KEYMAP is non-nil, search only KEYMAP and the global keymap.\n\
2102 If KEYMAP is nil, search all the currently active keymaps.\n\
2104 If optional 3rd arg FIRSTONLY is non-nil, return the first key sequence found,\n\
2105 rather than a list of all possible key sequences.\n\
2106 If FIRSTONLY is the symbol `non-ascii', return the first binding found,\n\
2107 no matter what it is.\n\
2108 If FIRSTONLY has another non-nil value, prefer sequences of ASCII characters,\n\
2109 and entirely reject menu bindings.\n\
2111 If optional 4th arg NOINDIRECT is non-nil, don't follow indirections\n\
2112 to other keymaps or slots. This makes it possible to search for an\n\
2113 indirect definition itself.")
2114 (definition, xkeymap, firstonly, noindirect)
2115 Lisp_Object definition, xkeymap;
2116 Lisp_Object firstonly, noindirect;
2118 Lisp_Object maps;
2119 Lisp_Object found, sequences;
2120 Lisp_Object keymap1;
2121 int keymap_specified = !NILP (xkeymap);
2122 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
2123 /* 1 means ignore all menu bindings entirely. */
2124 int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii);
2126 /* Find keymaps accessible from `keymap' or the current
2127 context. But don't muck with the value of `keymap',
2128 because `where_is_internal_1' uses it to check for
2129 shadowed bindings. */
2130 keymap1 = xkeymap;
2131 if (! keymap_specified)
2132 keymap1 = get_local_map (PT, current_buffer, keymap);
2134 if (!NILP (keymap1))
2135 maps = nconc2 (Faccessible_keymaps (get_keymap (keymap1), Qnil),
2136 Faccessible_keymaps (get_keymap (current_global_map),
2137 Qnil));
2138 else
2140 keymap1 = xkeymap;
2141 if (! keymap_specified)
2142 keymap1 = get_local_map (PT, current_buffer, local_map);
2144 if (!NILP (keymap1))
2145 maps = nconc2 (Faccessible_keymaps (get_keymap (keymap1), Qnil),
2146 Faccessible_keymaps (get_keymap (current_global_map),
2147 Qnil));
2148 else
2149 maps = Faccessible_keymaps (get_keymap (current_global_map), Qnil);
2152 /* Put the minor mode keymaps on the front. */
2153 if (! keymap_specified)
2155 Lisp_Object minors;
2156 minors = Fnreverse (Fcurrent_minor_mode_maps ());
2157 while (!NILP (minors))
2159 maps = nconc2 (Faccessible_keymaps (get_keymap (XCAR (minors)),
2160 Qnil),
2161 maps);
2162 minors = XCDR (minors);
2166 GCPRO5 (definition, xkeymap, maps, found, sequences);
2167 found = Qnil;
2168 sequences = Qnil;
2170 for (; !NILP (maps); maps = Fcdr (maps))
2172 /* Key sequence to reach map, and the map that it reaches */
2173 register Lisp_Object this, map;
2175 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
2176 [M-CHAR] sequences, check if last character of the sequence
2177 is the meta-prefix char. */
2178 Lisp_Object last;
2179 int last_is_meta;
2181 this = Fcar (Fcar (maps));
2182 map = Fcdr (Fcar (maps));
2183 last = make_number (XINT (Flength (this)) - 1);
2184 last_is_meta = (XINT (last) >= 0
2185 && EQ (Faref (this, last), meta_prefix_char));
2187 QUIT;
2189 while (CONSP (map))
2191 /* Because the code we want to run on each binding is rather
2192 large, we don't want to have two separate loop bodies for
2193 sparse keymap bindings and tables; we want to iterate one
2194 loop body over both keymap and vector bindings.
2196 For this reason, if Fcar (map) is a vector, we don't
2197 advance map to the next element until i indicates that we
2198 have finished off the vector. */
2199 Lisp_Object elt, key, binding;
2200 elt = XCAR (map);
2201 map = XCDR (map);
2203 sequences = Qnil;
2205 QUIT;
2207 /* Set key and binding to the current key and binding, and
2208 advance map and i to the next binding. */
2209 if (VECTORP (elt))
2211 Lisp_Object sequence;
2212 int i;
2213 /* In a vector, look at each element. */
2214 for (i = 0; i < XVECTOR (elt)->size; i++)
2216 binding = XVECTOR (elt)->contents[i];
2217 XSETFASTINT (key, i);
2218 sequence = where_is_internal_1 (binding, key, definition,
2219 noindirect, xkeymap, this,
2220 last, nomenus, last_is_meta);
2221 if (!NILP (sequence))
2222 sequences = Fcons (sequence, sequences);
2225 else if (CHAR_TABLE_P (elt))
2227 Lisp_Object indices[3];
2228 Lisp_Object args;
2230 args = Fcons (Fcons (Fcons (definition, noindirect),
2231 Fcons (xkeymap, Qnil)),
2232 Fcons (Fcons (this, last),
2233 Fcons (make_number (nomenus),
2234 make_number (last_is_meta))));
2235 map_char_table (where_is_internal_2, Qnil, elt, args,
2236 0, indices);
2237 sequences = XCDR (XCDR (XCAR (args)));
2239 else if (CONSP (elt))
2241 Lisp_Object sequence;
2243 key = XCAR (elt);
2244 binding = XCDR (elt);
2246 sequence = where_is_internal_1 (binding, key, definition,
2247 noindirect, xkeymap, this,
2248 last, nomenus, last_is_meta);
2249 if (!NILP (sequence))
2250 sequences = Fcons (sequence, sequences);
2254 for (; ! NILP (sequences); sequences = XCDR (sequences))
2256 Lisp_Object sequence;
2258 sequence = XCAR (sequences);
2260 /* It is a true unshadowed match. Record it, unless it's already
2261 been seen (as could happen when inheriting keymaps). */
2262 if (NILP (Fmember (sequence, found)))
2263 found = Fcons (sequence, found);
2265 /* If firstonly is Qnon_ascii, then we can return the first
2266 binding we find. If firstonly is not Qnon_ascii but not
2267 nil, then we should return the first ascii-only binding
2268 we find. */
2269 if (EQ (firstonly, Qnon_ascii))
2270 RETURN_UNGCPRO (sequence);
2271 else if (! NILP (firstonly) && ascii_sequence_p (sequence))
2272 RETURN_UNGCPRO (sequence);
2277 UNGCPRO;
2279 found = Fnreverse (found);
2281 /* firstonly may have been t, but we may have gone all the way through
2282 the keymaps without finding an all-ASCII key sequence. So just
2283 return the best we could find. */
2284 if (! NILP (firstonly))
2285 return Fcar (found);
2287 return found;
2290 /* This is the function that Fwhere_is_internal calls using map_char_table.
2291 ARGS has the form
2292 (((DEFINITION . NOINDIRECT) . (KEYMAP . RESULT))
2294 ((THIS . LAST) . (NOMENUS . LAST_IS_META)))
2295 Since map_char_table doesn't really use the return value from this function,
2296 we the result append to RESULT, the slot in ARGS.
2298 This function can GC because it calls where_is_internal_1 which can
2299 GC. */
2301 static void
2302 where_is_internal_2 (args, key, binding)
2303 Lisp_Object args, key, binding;
2305 Lisp_Object definition, noindirect, keymap, this, last;
2306 Lisp_Object result, sequence;
2307 int nomenus, last_is_meta;
2308 struct gcpro gcpro1, gcpro2, gcpro3;
2310 GCPRO3 (args, key, binding);
2311 result = XCDR (XCDR (XCAR (args)));
2312 definition = XCAR (XCAR (XCAR (args)));
2313 noindirect = XCDR (XCAR (XCAR (args)));
2314 keymap = XCAR (XCDR (XCAR (args)));
2315 this = XCAR (XCAR (XCDR (args)));
2316 last = XCDR (XCAR (XCDR (args)));
2317 nomenus = XFASTINT (XCAR (XCDR (XCDR (args))));
2318 last_is_meta = XFASTINT (XCDR (XCDR (XCDR (args))));
2320 sequence = where_is_internal_1 (binding, key, definition, noindirect, keymap,
2321 this, last, nomenus, last_is_meta);
2323 if (!NILP (sequence))
2324 XCDR (XCDR (XCAR (args))) = Fcons (sequence, result);
2326 UNGCPRO;
2330 /* This function can GC.because Flookup_key calls get_keymap_1 with
2331 non-zero argument AUTOLOAD. */
2333 static Lisp_Object
2334 where_is_internal_1 (binding, key, definition, noindirect, keymap, this, last,
2335 nomenus, last_is_meta)
2336 Lisp_Object binding, key, definition, noindirect, keymap, this, last;
2337 int nomenus, last_is_meta;
2339 Lisp_Object sequence;
2340 int keymap_specified = !NILP (keymap);
2341 struct gcpro gcpro1, gcpro2;
2343 /* Search through indirections unless that's not wanted. */
2344 if (NILP (noindirect))
2346 if (nomenus)
2348 while (1)
2350 Lisp_Object map, tem;
2351 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
2352 map = get_keymap_1 (Fcar_safe (definition), 0, 0);
2353 tem = Fkeymapp (map);
2354 if (!NILP (tem))
2355 definition = access_keymap (map, Fcdr (definition), 0, 0);
2356 else
2357 break;
2359 /* If the contents are (menu-item ...) or (STRING ...), reject. */
2360 if (CONSP (definition)
2361 && (EQ (XCAR (definition),Qmenu_item)
2362 || STRINGP (XCAR (definition))))
2363 return Qnil;
2365 else
2366 binding = get_keyelt (binding, 0);
2369 /* End this iteration if this element does not match
2370 the target. */
2372 if (CONSP (definition))
2374 Lisp_Object tem;
2375 tem = Fequal (binding, definition);
2376 if (NILP (tem))
2377 return Qnil;
2379 else
2380 if (!EQ (binding, definition))
2381 return Qnil;
2383 /* We have found a match.
2384 Construct the key sequence where we found it. */
2385 if (INTEGERP (key) && last_is_meta)
2387 sequence = Fcopy_sequence (this);
2388 Faset (sequence, last, make_number (XINT (key) | meta_modifier));
2390 else
2391 sequence = append_key (this, key);
2393 /* Verify that this key binding is not shadowed by another
2394 binding for the same key, before we say it exists.
2396 Mechanism: look for local definition of this key and if
2397 it is defined and does not match what we found then
2398 ignore this key.
2400 Either nil or number as value from Flookup_key
2401 means undefined. */
2402 GCPRO2 (sequence, binding);
2403 if (keymap_specified)
2405 binding = Flookup_key (keymap, sequence, Qnil);
2406 if (!NILP (binding) && !INTEGERP (binding))
2408 if (CONSP (definition))
2410 Lisp_Object tem;
2411 tem = Fequal (binding, definition);
2412 if (NILP (tem))
2413 RETURN_UNGCPRO (Qnil);
2415 else
2416 if (!EQ (binding, definition))
2417 RETURN_UNGCPRO (Qnil);
2420 else
2422 binding = Fkey_binding (sequence, Qnil);
2423 if (!EQ (binding, definition))
2424 RETURN_UNGCPRO (Qnil);
2427 RETURN_UNGCPRO (sequence);
2430 /* describe-bindings - summarizing all the bindings in a set of keymaps. */
2432 DEFUN ("describe-bindings-internal", Fdescribe_bindings_internal, Sdescribe_bindings_internal, 0, 2, "",
2433 "Show a list of all defined keys, and their definitions.\n\
2434 We put that list in a buffer, and display the buffer.\n\
2436 The optional argument MENUS, if non-nil, says to mention menu bindings.\n\
2437 \(Ordinarily these are omitted from the output.)\n\
2438 The optional argument PREFIX, if non-nil, should be a key sequence;\n\
2439 then we display only bindings that start with that prefix.")
2440 (menus, prefix)
2441 Lisp_Object menus, prefix;
2443 register Lisp_Object thisbuf;
2444 XSETBUFFER (thisbuf, current_buffer);
2445 internal_with_output_to_temp_buffer ("*Help*",
2446 describe_buffer_bindings,
2447 list3 (thisbuf, prefix, menus));
2448 return Qnil;
2451 /* ARG is (BUFFER PREFIX MENU-FLAG). */
2453 static Lisp_Object
2454 describe_buffer_bindings (arg)
2455 Lisp_Object arg;
2457 Lisp_Object descbuf, prefix, shadow;
2458 int nomenu;
2459 register Lisp_Object start1;
2460 struct gcpro gcpro1;
2462 char *alternate_heading
2463 = "\
2464 Keyboard translations:\n\n\
2465 You type Translation\n\
2466 -------- -----------\n";
2468 descbuf = XCAR (arg);
2469 arg = XCDR (arg);
2470 prefix = XCAR (arg);
2471 arg = XCDR (arg);
2472 nomenu = NILP (XCAR (arg));
2474 shadow = Qnil;
2475 GCPRO1 (shadow);
2477 Fset_buffer (Vstandard_output);
2479 /* Report on alternates for keys. */
2480 if (STRINGP (Vkeyboard_translate_table) && !NILP (prefix))
2482 int c;
2483 unsigned char *translate = XSTRING (Vkeyboard_translate_table)->data;
2484 int translate_len = XSTRING (Vkeyboard_translate_table)->size;
2486 for (c = 0; c < translate_len; c++)
2487 if (translate[c] != c)
2489 char buf[KEY_DESCRIPTION_SIZE];
2490 char *bufend;
2492 if (alternate_heading)
2494 insert_string (alternate_heading);
2495 alternate_heading = 0;
2498 bufend = push_key_description (translate[c], buf);
2499 insert (buf, bufend - buf);
2500 Findent_to (make_number (16), make_number (1));
2501 bufend = push_key_description (c, buf);
2502 insert (buf, bufend - buf);
2504 insert ("\n", 1);
2507 insert ("\n", 1);
2510 if (!NILP (Vkey_translation_map))
2511 describe_map_tree (Vkey_translation_map, 0, Qnil, prefix,
2512 "Key translations", nomenu, 1, 0);
2515 int i, nmaps;
2516 Lisp_Object *modes, *maps;
2518 /* Temporarily switch to descbuf, so that we can get that buffer's
2519 minor modes correctly. */
2520 Fset_buffer (descbuf);
2522 if (!NILP (current_kboard->Voverriding_terminal_local_map)
2523 || !NILP (Voverriding_local_map))
2524 nmaps = 0;
2525 else
2526 nmaps = current_minor_maps (&modes, &maps);
2527 Fset_buffer (Vstandard_output);
2529 /* Print the minor mode maps. */
2530 for (i = 0; i < nmaps; i++)
2532 /* The title for a minor mode keymap
2533 is constructed at run time.
2534 We let describe_map_tree do the actual insertion
2535 because it takes care of other features when doing so. */
2536 char *title, *p;
2538 if (!SYMBOLP (modes[i]))
2539 abort();
2541 p = title = (char *) alloca (42 + XSYMBOL (modes[i])->name->size);
2542 *p++ = '\f';
2543 *p++ = '\n';
2544 *p++ = '`';
2545 bcopy (XSYMBOL (modes[i])->name->data, p,
2546 XSYMBOL (modes[i])->name->size);
2547 p += XSYMBOL (modes[i])->name->size;
2548 *p++ = '\'';
2549 bcopy (" Minor Mode Bindings", p, sizeof (" Minor Mode Bindings") - 1);
2550 p += sizeof (" Minor Mode Bindings") - 1;
2551 *p = 0;
2553 describe_map_tree (maps[i], 1, shadow, prefix, title, nomenu, 0, 0);
2554 shadow = Fcons (maps[i], shadow);
2558 /* Print the (major mode) local map. */
2559 if (!NILP (current_kboard->Voverriding_terminal_local_map))
2560 start1 = current_kboard->Voverriding_terminal_local_map;
2561 else if (!NILP (Voverriding_local_map))
2562 start1 = Voverriding_local_map;
2563 else
2564 start1 = XBUFFER (descbuf)->keymap;
2566 if (!NILP (start1))
2568 describe_map_tree (start1, 1, shadow, prefix,
2569 "\f\nMajor Mode Bindings", nomenu, 0, 0);
2570 shadow = Fcons (start1, shadow);
2573 describe_map_tree (current_global_map, 1, shadow, prefix,
2574 "\f\nGlobal Bindings", nomenu, 0, 1);
2576 /* Print the function-key-map translations under this prefix. */
2577 if (!NILP (Vfunction_key_map))
2578 describe_map_tree (Vfunction_key_map, 0, Qnil, prefix,
2579 "\f\nFunction key map translations", nomenu, 1, 0);
2581 call0 (intern ("help-mode"));
2582 Fset_buffer (descbuf);
2583 UNGCPRO;
2584 return Qnil;
2587 /* Insert a description of the key bindings in STARTMAP,
2588 followed by those of all maps reachable through STARTMAP.
2589 If PARTIAL is nonzero, omit certain "uninteresting" commands
2590 (such as `undefined').
2591 If SHADOW is non-nil, it is a list of maps;
2592 don't mention keys which would be shadowed by any of them.
2593 PREFIX, if non-nil, says mention only keys that start with PREFIX.
2594 TITLE, if not 0, is a string to insert at the beginning.
2595 TITLE should not end with a colon or a newline; we supply that.
2596 If NOMENU is not 0, then omit menu-bar commands.
2598 If TRANSL is nonzero, the definitions are actually key translations
2599 so print strings and vectors differently.
2601 If ALWAYS_TITLE is nonzero, print the title even if there are no maps
2602 to look through. */
2604 void
2605 describe_map_tree (startmap, partial, shadow, prefix, title, nomenu, transl,
2606 always_title)
2607 Lisp_Object startmap, shadow, prefix;
2608 int partial;
2609 char *title;
2610 int nomenu;
2611 int transl;
2612 int always_title;
2614 Lisp_Object maps, orig_maps, seen, sub_shadows;
2615 struct gcpro gcpro1, gcpro2, gcpro3;
2616 int something = 0;
2617 char *key_heading
2618 = "\
2619 key binding\n\
2620 --- -------\n";
2622 orig_maps = maps = Faccessible_keymaps (startmap, prefix);
2623 seen = Qnil;
2624 sub_shadows = Qnil;
2625 GCPRO3 (maps, seen, sub_shadows);
2627 if (nomenu)
2629 Lisp_Object list;
2631 /* Delete from MAPS each element that is for the menu bar. */
2632 for (list = maps; !NILP (list); list = XCDR (list))
2634 Lisp_Object elt, prefix, tem;
2636 elt = Fcar (list);
2637 prefix = Fcar (elt);
2638 if (XVECTOR (prefix)->size >= 1)
2640 tem = Faref (prefix, make_number (0));
2641 if (EQ (tem, Qmenu_bar))
2642 maps = Fdelq (elt, maps);
2647 if (!NILP (maps) || always_title)
2649 if (title)
2651 insert_string (title);
2652 if (!NILP (prefix))
2654 insert_string (" Starting With ");
2655 insert1 (Fkey_description (prefix));
2657 insert_string (":\n");
2659 insert_string (key_heading);
2660 something = 1;
2663 for (; !NILP (maps); maps = Fcdr (maps))
2665 register Lisp_Object elt, prefix, tail;
2667 elt = Fcar (maps);
2668 prefix = Fcar (elt);
2670 sub_shadows = Qnil;
2672 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2674 Lisp_Object shmap;
2676 shmap = XCAR (tail);
2678 /* If the sequence by which we reach this keymap is zero-length,
2679 then the shadow map for this keymap is just SHADOW. */
2680 if ((STRINGP (prefix) && XSTRING (prefix)->size == 0)
2681 || (VECTORP (prefix) && XVECTOR (prefix)->size == 0))
2683 /* If the sequence by which we reach this keymap actually has
2684 some elements, then the sequence's definition in SHADOW is
2685 what we should use. */
2686 else
2688 shmap = Flookup_key (shmap, Fcar (elt), Qt);
2689 if (INTEGERP (shmap))
2690 shmap = Qnil;
2693 /* If shmap is not nil and not a keymap,
2694 it completely shadows this map, so don't
2695 describe this map at all. */
2696 if (!NILP (shmap) && NILP (Fkeymapp (shmap)))
2697 goto skip;
2699 if (!NILP (shmap))
2700 sub_shadows = Fcons (shmap, sub_shadows);
2703 /* Maps we have already listed in this loop shadow this map. */
2704 for (tail = orig_maps; ! EQ (tail, maps); tail = XCDR (tail))
2706 Lisp_Object tem;
2707 tem = Fequal (Fcar (XCAR (tail)), prefix);
2708 if (! NILP (tem))
2709 sub_shadows = Fcons (XCDR (XCAR (tail)), sub_shadows);
2712 describe_map (Fcdr (elt), prefix,
2713 transl ? describe_translation : describe_command,
2714 partial, sub_shadows, &seen, nomenu);
2716 skip: ;
2719 if (something)
2720 insert_string ("\n");
2722 UNGCPRO;
2725 static int previous_description_column;
2727 static void
2728 describe_command (definition)
2729 Lisp_Object definition;
2731 register Lisp_Object tem1;
2732 int column = current_column ();
2733 int description_column;
2735 /* If column 16 is no good, go to col 32;
2736 but don't push beyond that--go to next line instead. */
2737 if (column > 30)
2739 insert_char ('\n');
2740 description_column = 32;
2742 else if (column > 14 || (column > 10 && previous_description_column == 32))
2743 description_column = 32;
2744 else
2745 description_column = 16;
2747 Findent_to (make_number (description_column), make_number (1));
2748 previous_description_column = description_column;
2750 if (SYMBOLP (definition))
2752 XSETSTRING (tem1, XSYMBOL (definition)->name);
2753 insert1 (tem1);
2754 insert_string ("\n");
2756 else if (STRINGP (definition) || VECTORP (definition))
2757 insert_string ("Keyboard Macro\n");
2758 else
2760 tem1 = Fkeymapp (definition);
2761 if (!NILP (tem1))
2762 insert_string ("Prefix Command\n");
2763 else
2764 insert_string ("??\n");
2768 static void
2769 describe_translation (definition)
2770 Lisp_Object definition;
2772 register Lisp_Object tem1;
2774 Findent_to (make_number (16), make_number (1));
2776 if (SYMBOLP (definition))
2778 XSETSTRING (tem1, XSYMBOL (definition)->name);
2779 insert1 (tem1);
2780 insert_string ("\n");
2782 else if (STRINGP (definition) || VECTORP (definition))
2784 insert1 (Fkey_description (definition));
2785 insert_string ("\n");
2787 else
2789 tem1 = Fkeymapp (definition);
2790 if (!NILP (tem1))
2791 insert_string ("Prefix Command\n");
2792 else
2793 insert_string ("??\n");
2797 /* Like Flookup_key, but uses a list of keymaps SHADOW instead of a single map.
2798 Returns the first non-nil binding found in any of those maps. */
2800 static Lisp_Object
2801 shadow_lookup (shadow, key, flag)
2802 Lisp_Object shadow, key, flag;
2804 Lisp_Object tail, value;
2806 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2808 value = Flookup_key (XCAR (tail), key, flag);
2809 if (!NILP (value))
2810 return value;
2812 return Qnil;
2815 /* Describe the contents of map MAP, assuming that this map itself is
2816 reached by the sequence of prefix keys KEYS (a string or vector).
2817 PARTIAL, SHADOW, NOMENU are as in `describe_map_tree' above. */
2819 static void
2820 describe_map (map, keys, elt_describer, partial, shadow, seen, nomenu)
2821 register Lisp_Object map;
2822 Lisp_Object keys;
2823 void (*elt_describer) P_ ((Lisp_Object));
2824 int partial;
2825 Lisp_Object shadow;
2826 Lisp_Object *seen;
2827 int nomenu;
2829 Lisp_Object elt_prefix;
2830 Lisp_Object tail, definition, event;
2831 Lisp_Object tem;
2832 Lisp_Object suppress;
2833 Lisp_Object kludge;
2834 int first = 1;
2835 struct gcpro gcpro1, gcpro2, gcpro3;
2837 if (!NILP (keys) && XFASTINT (Flength (keys)) > 0)
2839 /* Call Fkey_description first, to avoid GC bug for the other string. */
2840 tem = Fkey_description (keys);
2841 elt_prefix = concat2 (tem, build_string (" "));
2843 else
2844 elt_prefix = Qnil;
2846 if (partial)
2847 suppress = intern ("suppress-keymap");
2849 /* This vector gets used to present single keys to Flookup_key. Since
2850 that is done once per keymap element, we don't want to cons up a
2851 fresh vector every time. */
2852 kludge = Fmake_vector (make_number (1), Qnil);
2853 definition = Qnil;
2855 GCPRO3 (elt_prefix, definition, kludge);
2857 for (tail = map; CONSP (tail); tail = XCDR (tail))
2859 QUIT;
2861 if (VECTORP (XCAR (tail))
2862 || CHAR_TABLE_P (XCAR (tail)))
2863 describe_vector (XCAR (tail),
2864 elt_prefix, elt_describer, partial, shadow, map,
2865 (int *)0, 0);
2866 else if (CONSP (XCAR (tail)))
2868 event = XCAR (XCAR (tail));
2870 /* Ignore bindings whose "keys" are not really valid events.
2871 (We get these in the frames and buffers menu.) */
2872 if (! (SYMBOLP (event) || INTEGERP (event)))
2873 continue;
2875 if (nomenu && EQ (event, Qmenu_bar))
2876 continue;
2878 definition = get_keyelt (XCDR (XCAR (tail)), 0);
2880 /* Don't show undefined commands or suppressed commands. */
2881 if (NILP (definition)) continue;
2882 if (SYMBOLP (definition) && partial)
2884 tem = Fget (definition, suppress);
2885 if (!NILP (tem))
2886 continue;
2889 /* Don't show a command that isn't really visible
2890 because a local definition of the same key shadows it. */
2892 XVECTOR (kludge)->contents[0] = event;
2893 if (!NILP (shadow))
2895 tem = shadow_lookup (shadow, kludge, Qt);
2896 if (!NILP (tem)) continue;
2899 tem = Flookup_key (map, kludge, Qt);
2900 if (! EQ (tem, definition)) continue;
2902 if (first)
2904 previous_description_column = 0;
2905 insert ("\n", 1);
2906 first = 0;
2909 if (!NILP (elt_prefix))
2910 insert1 (elt_prefix);
2912 /* THIS gets the string to describe the character EVENT. */
2913 insert1 (Fsingle_key_description (event, Qnil));
2915 /* Print a description of the definition of this character.
2916 elt_describer will take care of spacing out far enough
2917 for alignment purposes. */
2918 (*elt_describer) (definition);
2920 else if (EQ (XCAR (tail), Qkeymap))
2922 /* The same keymap might be in the structure twice, if we're
2923 using an inherited keymap. So skip anything we've already
2924 encountered. */
2925 tem = Fassq (tail, *seen);
2926 if (CONSP (tem) && !NILP (Fequal (XCAR (tem), keys)))
2927 break;
2928 *seen = Fcons (Fcons (tail, keys), *seen);
2932 UNGCPRO;
2935 static void
2936 describe_vector_princ (elt)
2937 Lisp_Object elt;
2939 Findent_to (make_number (16), make_number (1));
2940 Fprinc (elt, Qnil);
2941 Fterpri (Qnil);
2944 DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 1, 0,
2945 "Insert a description of contents of VECTOR.\n\
2946 This is text showing the elements of vector matched against indices.")
2947 (vector)
2948 Lisp_Object vector;
2950 int count = specpdl_ptr - specpdl;
2952 specbind (Qstandard_output, Fcurrent_buffer ());
2953 CHECK_VECTOR_OR_CHAR_TABLE (vector, 0);
2954 describe_vector (vector, Qnil, describe_vector_princ, 0,
2955 Qnil, Qnil, (int *)0, 0);
2957 return unbind_to (count, Qnil);
2960 /* Insert in the current buffer a description of the contents of VECTOR.
2961 We call ELT_DESCRIBER to insert the description of one value found
2962 in VECTOR.
2964 ELT_PREFIX describes what "comes before" the keys or indices defined
2965 by this vector. This is a human-readable string whose size
2966 is not necessarily related to the situation.
2968 If the vector is in a keymap, ELT_PREFIX is a prefix key which
2969 leads to this keymap.
2971 If the vector is a chartable, ELT_PREFIX is the vector
2972 of bytes that lead to the character set or portion of a character
2973 set described by this chartable.
2975 If PARTIAL is nonzero, it means do not mention suppressed commands
2976 (that assumes the vector is in a keymap).
2978 SHADOW is a list of keymaps that shadow this map.
2979 If it is non-nil, then we look up the key in those maps
2980 and we don't mention it now if it is defined by any of them.
2982 ENTIRE_MAP is the keymap in which this vector appears.
2983 If the definition in effect in the whole map does not match
2984 the one in this vector, we ignore this one.
2986 When describing a sub-char-table, INDICES is a list of
2987 indices at higher levels in this char-table,
2988 and CHAR_TABLE_DEPTH says how many levels down we have gone. */
2990 void
2991 describe_vector (vector, elt_prefix, elt_describer,
2992 partial, shadow, entire_map,
2993 indices, char_table_depth)
2994 register Lisp_Object vector;
2995 Lisp_Object elt_prefix;
2996 void (*elt_describer) P_ ((Lisp_Object));
2997 int partial;
2998 Lisp_Object shadow;
2999 Lisp_Object entire_map;
3000 int *indices;
3001 int char_table_depth;
3003 Lisp_Object definition;
3004 Lisp_Object tem2;
3005 register int i;
3006 Lisp_Object suppress;
3007 Lisp_Object kludge;
3008 int first = 1;
3009 struct gcpro gcpro1, gcpro2, gcpro3;
3010 /* Range of elements to be handled. */
3011 int from, to;
3012 /* A flag to tell if a leaf in this level of char-table is not a
3013 generic character (i.e. a complete multibyte character). */
3014 int complete_char;
3015 int character;
3016 int starting_i;
3018 if (indices == 0)
3019 indices = (int *) alloca (3 * sizeof (int));
3021 definition = Qnil;
3023 /* This vector gets used to present single keys to Flookup_key. Since
3024 that is done once per vector element, we don't want to cons up a
3025 fresh vector every time. */
3026 kludge = Fmake_vector (make_number (1), Qnil);
3027 GCPRO3 (elt_prefix, definition, kludge);
3029 if (partial)
3030 suppress = intern ("suppress-keymap");
3032 if (CHAR_TABLE_P (vector))
3034 if (char_table_depth == 0)
3036 /* VECTOR is a top level char-table. */
3037 complete_char = 1;
3038 from = 0;
3039 to = CHAR_TABLE_ORDINARY_SLOTS;
3041 else
3043 /* VECTOR is a sub char-table. */
3044 if (char_table_depth >= 3)
3045 /* A char-table is never that deep. */
3046 error ("Too deep char table");
3048 complete_char
3049 = (CHARSET_VALID_P (indices[0])
3050 && ((CHARSET_DIMENSION (indices[0]) == 1
3051 && char_table_depth == 1)
3052 || char_table_depth == 2));
3054 /* Meaningful elements are from 32th to 127th. */
3055 from = 32;
3056 to = SUB_CHAR_TABLE_ORDINARY_SLOTS;
3059 else
3061 /* This does the right thing for ordinary vectors. */
3063 complete_char = 1;
3064 from = 0;
3065 to = XVECTOR (vector)->size;
3068 for (i = from; i < to; i++)
3070 QUIT;
3072 if (CHAR_TABLE_P (vector))
3074 if (char_table_depth == 0 && i >= CHAR_TABLE_SINGLE_BYTE_SLOTS)
3075 complete_char = 0;
3077 if (i >= CHAR_TABLE_SINGLE_BYTE_SLOTS
3078 && !CHARSET_DEFINED_P (i - 128))
3079 continue;
3081 definition
3082 = get_keyelt (XCHAR_TABLE (vector)->contents[i], 0);
3084 else
3085 definition = get_keyelt (XVECTOR (vector)->contents[i], 0);
3087 if (NILP (definition)) continue;
3089 /* Don't mention suppressed commands. */
3090 if (SYMBOLP (definition) && partial)
3092 Lisp_Object tem;
3094 tem = Fget (definition, suppress);
3096 if (!NILP (tem)) continue;
3099 /* Set CHARACTER to the character this entry describes, if any.
3100 Also update *INDICES. */
3101 if (CHAR_TABLE_P (vector))
3103 indices[char_table_depth] = i;
3105 if (char_table_depth == 0)
3107 character = i;
3108 indices[0] = i - 128;
3110 else if (complete_char)
3112 character = MAKE_CHAR (indices[0], indices[1], indices[2]);
3114 else
3115 character = 0;
3117 else
3118 character = i;
3120 /* If this binding is shadowed by some other map, ignore it. */
3121 if (!NILP (shadow) && complete_char)
3123 Lisp_Object tem;
3125 XVECTOR (kludge)->contents[0] = make_number (character);
3126 tem = shadow_lookup (shadow, kludge, Qt);
3128 if (!NILP (tem)) continue;
3131 /* Ignore this definition if it is shadowed by an earlier
3132 one in the same keymap. */
3133 if (!NILP (entire_map) && complete_char)
3135 Lisp_Object tem;
3137 XVECTOR (kludge)->contents[0] = make_number (character);
3138 tem = Flookup_key (entire_map, kludge, Qt);
3140 if (! EQ (tem, definition))
3141 continue;
3144 if (first)
3146 if (char_table_depth == 0)
3147 insert ("\n", 1);
3148 first = 0;
3151 /* For a sub char-table, show the depth by indentation.
3152 CHAR_TABLE_DEPTH can be greater than 0 only for a char-table. */
3153 if (char_table_depth > 0)
3154 insert (" ", char_table_depth * 2); /* depth is 1 or 2. */
3156 /* Output the prefix that applies to every entry in this map. */
3157 if (!NILP (elt_prefix))
3158 insert1 (elt_prefix);
3160 /* Insert or describe the character this slot is for,
3161 or a description of what it is for. */
3162 if (SUB_CHAR_TABLE_P (vector))
3164 if (complete_char)
3165 insert_char (character);
3166 else
3168 /* We need an octal representation for this block of
3169 characters. */
3170 char work[16];
3171 sprintf (work, "(row %d)", i);
3172 insert (work, strlen (work));
3175 else if (CHAR_TABLE_P (vector))
3177 if (complete_char)
3178 insert1 (Fsingle_key_description (make_number (character), Qnil));
3179 else
3181 /* Print the information for this character set. */
3182 insert_string ("<");
3183 tem2 = CHARSET_TABLE_INFO (i - 128, CHARSET_SHORT_NAME_IDX);
3184 if (STRINGP (tem2))
3185 insert_from_string (tem2, 0, 0, XSTRING (tem2)->size,
3186 STRING_BYTES (XSTRING (tem2)), 0);
3187 else
3188 insert ("?", 1);
3189 insert (">", 1);
3192 else
3194 insert1 (Fsingle_key_description (make_number (character), Qnil));
3197 /* If we find a sub char-table within a char-table,
3198 scan it recursively; it defines the details for
3199 a character set or a portion of a character set. */
3200 if (CHAR_TABLE_P (vector) && SUB_CHAR_TABLE_P (definition))
3202 insert ("\n", 1);
3203 describe_vector (definition, elt_prefix, elt_describer,
3204 partial, shadow, entire_map,
3205 indices, char_table_depth + 1);
3206 continue;
3209 starting_i = i;
3211 /* Find all consecutive characters or rows that have the same
3212 definition. But, for elements of a top level char table, if
3213 they are for charsets, we had better describe one by one even
3214 if they have the same definition. */
3215 if (CHAR_TABLE_P (vector))
3217 int limit = to;
3219 if (char_table_depth == 0)
3220 limit = CHAR_TABLE_SINGLE_BYTE_SLOTS;
3222 while (i + 1 < limit
3223 && (tem2 = get_keyelt (XCHAR_TABLE (vector)->contents[i + 1], 0),
3224 !NILP (tem2))
3225 && !NILP (Fequal (tem2, definition)))
3226 i++;
3228 else
3229 while (i + 1 < to
3230 && (tem2 = get_keyelt (XVECTOR (vector)->contents[i + 1], 0),
3231 !NILP (tem2))
3232 && !NILP (Fequal (tem2, definition)))
3233 i++;
3236 /* If we have a range of more than one character,
3237 print where the range reaches to. */
3239 if (i != starting_i)
3241 insert (" .. ", 4);
3243 if (!NILP (elt_prefix))
3244 insert1 (elt_prefix);
3246 if (CHAR_TABLE_P (vector))
3248 if (char_table_depth == 0)
3250 insert1 (Fsingle_key_description (make_number (i), Qnil));
3252 else if (complete_char)
3254 indices[char_table_depth] = i;
3255 character = MAKE_CHAR (indices[0], indices[1], indices[2]);
3256 insert_char (character);
3258 else
3260 /* We need an octal representation for this block of
3261 characters. */
3262 char work[16];
3263 sprintf (work, "(row %d)", i);
3264 insert (work, strlen (work));
3267 else
3269 insert1 (Fsingle_key_description (make_number (i), Qnil));
3273 /* Print a description of the definition of this character.
3274 elt_describer will take care of spacing out far enough
3275 for alignment purposes. */
3276 (*elt_describer) (definition);
3279 /* For (sub) char-table, print `defalt' slot at last. */
3280 if (CHAR_TABLE_P (vector) && !NILP (XCHAR_TABLE (vector)->defalt))
3282 insert (" ", char_table_depth * 2);
3283 insert_string ("<<default>>");
3284 (*elt_describer) (XCHAR_TABLE (vector)->defalt);
3287 UNGCPRO;
3290 /* Apropos - finding all symbols whose names match a regexp. */
3291 Lisp_Object apropos_predicate;
3292 Lisp_Object apropos_accumulate;
3294 static void
3295 apropos_accum (symbol, string)
3296 Lisp_Object symbol, string;
3298 register Lisp_Object tem;
3300 tem = Fstring_match (string, Fsymbol_name (symbol), Qnil);
3301 if (!NILP (tem) && !NILP (apropos_predicate))
3302 tem = call1 (apropos_predicate, symbol);
3303 if (!NILP (tem))
3304 apropos_accumulate = Fcons (symbol, apropos_accumulate);
3307 DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0,
3308 "Show all symbols whose names contain match for REGEXP.\n\
3309 If optional 2nd arg PREDICATE is non-nil, (funcall PREDICATE SYMBOL) is done\n\
3310 for each symbol and a symbol is mentioned only if that returns non-nil.\n\
3311 Return list of symbols found.")
3312 (regexp, predicate)
3313 Lisp_Object regexp, predicate;
3315 struct gcpro gcpro1, gcpro2;
3316 CHECK_STRING (regexp, 0);
3317 apropos_predicate = predicate;
3318 GCPRO2 (apropos_predicate, apropos_accumulate);
3319 apropos_accumulate = Qnil;
3320 map_obarray (Vobarray, apropos_accum, regexp);
3321 apropos_accumulate = Fsort (apropos_accumulate, Qstring_lessp);
3322 UNGCPRO;
3323 return apropos_accumulate;
3326 void
3327 syms_of_keymap ()
3329 Qkeymap = intern ("keymap");
3330 staticpro (&Qkeymap);
3332 /* Now we are ready to set up this property, so we can
3333 create char tables. */
3334 Fput (Qkeymap, Qchar_table_extra_slots, make_number (0));
3336 /* Initialize the keymaps standardly used.
3337 Each one is the value of a Lisp variable, and is also
3338 pointed to by a C variable */
3340 global_map = Fmake_keymap (Qnil);
3341 Fset (intern ("global-map"), global_map);
3343 current_global_map = global_map;
3344 staticpro (&global_map);
3345 staticpro (&current_global_map);
3347 meta_map = Fmake_keymap (Qnil);
3348 Fset (intern ("esc-map"), meta_map);
3349 Ffset (intern ("ESC-prefix"), meta_map);
3351 control_x_map = Fmake_keymap (Qnil);
3352 Fset (intern ("ctl-x-map"), control_x_map);
3353 Ffset (intern ("Control-X-prefix"), control_x_map);
3355 DEFVAR_LISP ("define-key-rebound-commands", &Vdefine_key_rebound_commands,
3356 "List of commands given new key bindings recently.\n\
3357 This is used for internal purposes during Emacs startup;\n\
3358 don't alter it yourself.");
3359 Vdefine_key_rebound_commands = Qt;
3361 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map,
3362 "Default keymap to use when reading from the minibuffer.");
3363 Vminibuffer_local_map = Fmake_sparse_keymap (Qnil);
3365 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map,
3366 "Local keymap for the minibuffer when spaces are not allowed.");
3367 Vminibuffer_local_ns_map = Fmake_sparse_keymap (Qnil);
3369 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map,
3370 "Local keymap for minibuffer input with completion.");
3371 Vminibuffer_local_completion_map = Fmake_sparse_keymap (Qnil);
3373 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map,
3374 "Local keymap for minibuffer input with completion, for exact match.");
3375 Vminibuffer_local_must_match_map = Fmake_sparse_keymap (Qnil);
3377 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist,
3378 "Alist of keymaps to use for minor modes.\n\
3379 Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read\n\
3380 key sequences and look up bindings iff VARIABLE's value is non-nil.\n\
3381 If two active keymaps bind the same key, the keymap appearing earlier\n\
3382 in the list takes precedence.");
3383 Vminor_mode_map_alist = Qnil;
3385 DEFVAR_LISP ("minor-mode-overriding-map-alist", &Vminor_mode_overriding_map_alist,
3386 "Alist of keymaps to use for minor modes, in current major mode.\n\
3387 This variable is a alist just like `minor-mode-map-alist', and it is\n\
3388 used the same way (and before `minor-mode-map-alist'); however,\n\
3389 it is provided for major modes to bind locally.");
3390 Vminor_mode_overriding_map_alist = Qnil;
3392 DEFVAR_LISP ("function-key-map", &Vfunction_key_map,
3393 "Keymap mapping ASCII function key sequences onto their preferred forms.\n\
3394 This allows Emacs to recognize function keys sent from ASCII\n\
3395 terminals at any point in a key sequence.\n\
3397 The `read-key-sequence' function replaces any subsequence bound by\n\
3398 `function-key-map' with its binding. More precisely, when the active\n\
3399 keymaps have no binding for the current key sequence but\n\
3400 `function-key-map' binds a suffix of the sequence to a vector or string,\n\
3401 `read-key-sequence' replaces the matching suffix with its binding, and\n\
3402 continues with the new sequence.\n\
3404 The events that come from bindings in `function-key-map' are not\n\
3405 themselves looked up in `function-key-map'.\n\
3407 For example, suppose `function-key-map' binds `ESC O P' to [f1].\n\
3408 Typing `ESC O P' to `read-key-sequence' would return [f1]. Typing\n\
3409 `C-x ESC O P' would return [?\\C-x f1]. If [f1] were a prefix\n\
3410 key, typing `ESC O P x' would return [f1 x].");
3411 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
3413 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
3414 "Keymap of key translations that can override keymaps.\n\
3415 This keymap works like `function-key-map', but comes after that,\n\
3416 and applies even for keys that have ordinary bindings.");
3417 Vkey_translation_map = Qnil;
3419 Qsingle_key_description = intern ("single-key-description");
3420 staticpro (&Qsingle_key_description);
3422 Qkey_description = intern ("key-description");
3423 staticpro (&Qkey_description);
3425 Qkeymapp = intern ("keymapp");
3426 staticpro (&Qkeymapp);
3428 Qnon_ascii = intern ("non-ascii");
3429 staticpro (&Qnon_ascii);
3431 Qmenu_item = intern ("menu-item");
3432 staticpro (&Qmenu_item);
3434 defsubr (&Skeymapp);
3435 defsubr (&Skeymap_parent);
3436 defsubr (&Sset_keymap_parent);
3437 defsubr (&Smake_keymap);
3438 defsubr (&Smake_sparse_keymap);
3439 defsubr (&Scopy_keymap);
3440 defsubr (&Skey_binding);
3441 defsubr (&Slocal_key_binding);
3442 defsubr (&Sglobal_key_binding);
3443 defsubr (&Sminor_mode_key_binding);
3444 defsubr (&Sdefine_key);
3445 defsubr (&Slookup_key);
3446 defsubr (&Sdefine_prefix_command);
3447 defsubr (&Suse_global_map);
3448 defsubr (&Suse_local_map);
3449 defsubr (&Scurrent_local_map);
3450 defsubr (&Scurrent_global_map);
3451 defsubr (&Scurrent_minor_mode_maps);
3452 defsubr (&Saccessible_keymaps);
3453 defsubr (&Skey_description);
3454 defsubr (&Sdescribe_vector);
3455 defsubr (&Ssingle_key_description);
3456 defsubr (&Stext_char_description);
3457 defsubr (&Swhere_is_internal);
3458 defsubr (&Sdescribe_bindings_internal);
3459 defsubr (&Sapropos_internal);
3462 void
3463 keys_of_keymap ()
3465 initial_define_key (global_map, 033, "ESC-prefix");
3466 initial_define_key (global_map, Ctl('X'), "Control-X-prefix");