(easy-mmode-define-navigation):
[emacs.git] / src / keymap.c
bloba9b659804cd332ef908d85a5685b3a24db19dddb
1 /* Manipulation of keymaps
2 Copyright (C) 1985, 86,87,88,93,94,95,98,99 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include <stdio.h>
24 #include "lisp.h"
25 #include "commands.h"
26 #include "buffer.h"
27 #include "charset.h"
28 #include "keyboard.h"
29 #include "termhooks.h"
30 #include "blockinput.h"
31 #include "puresize.h"
32 #include "intervals.h"
34 #define min(a, b) ((a) < (b) ? (a) : (b))
35 #define KEYMAPP(m) (!NILP (Fkeymapp (m)))
37 /* The number of elements in keymap vectors. */
38 #define DENSE_TABLE_SIZE (0200)
40 /* Actually allocate storage for these variables */
42 Lisp_Object current_global_map; /* Current global keymap */
44 Lisp_Object global_map; /* default global key bindings */
46 Lisp_Object meta_map; /* The keymap used for globally bound
47 ESC-prefixed default commands */
49 Lisp_Object control_x_map; /* The keymap used for globally bound
50 C-x-prefixed default commands */
52 /* was MinibufLocalMap */
53 Lisp_Object Vminibuffer_local_map;
54 /* The keymap used by the minibuf for local
55 bindings when spaces are allowed in the
56 minibuf */
58 /* was MinibufLocalNSMap */
59 Lisp_Object Vminibuffer_local_ns_map;
60 /* The keymap used by the minibuf for local
61 bindings when spaces are not encouraged
62 in the minibuf */
64 /* keymap used for minibuffers when doing completion */
65 /* was MinibufLocalCompletionMap */
66 Lisp_Object Vminibuffer_local_completion_map;
68 /* keymap used for minibuffers when doing completion and require a match */
69 /* was MinibufLocalMustMatchMap */
70 Lisp_Object Vminibuffer_local_must_match_map;
72 /* Alist of minor mode variables and keymaps. */
73 Lisp_Object Vminor_mode_map_alist;
75 /* Alist of major-mode-specific overrides for
76 minor mode variables and keymaps. */
77 Lisp_Object Vminor_mode_overriding_map_alist;
79 /* Keymap mapping ASCII function key sequences onto their preferred forms.
80 Initialized by the terminal-specific lisp files. See DEFVAR for more
81 documentation. */
82 Lisp_Object Vfunction_key_map;
84 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
85 Lisp_Object Vkey_translation_map;
87 /* A list of all commands given new bindings since a certain time
88 when nil was stored here.
89 This is used to speed up recomputation of menu key equivalents
90 when Emacs starts up. t means don't record anything here. */
91 Lisp_Object Vdefine_key_rebound_commands;
93 Lisp_Object Qkeymapp, Qkeymap, Qnon_ascii, Qmenu_item;
95 /* A char with the CHAR_META bit set in a vector or the 0200 bit set
96 in a string key sequence is equivalent to prefixing with this
97 character. */
98 extern Lisp_Object meta_prefix_char;
100 extern Lisp_Object Voverriding_local_map;
102 static Lisp_Object store_in_keymap P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
103 static void fix_submap_inheritance P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
105 static Lisp_Object define_as_prefix P_ ((Lisp_Object, Lisp_Object));
106 static Lisp_Object describe_buffer_bindings P_ ((Lisp_Object));
107 static void describe_command P_ ((Lisp_Object));
108 static void describe_translation P_ ((Lisp_Object));
109 static void describe_map P_ ((Lisp_Object, Lisp_Object,
110 void (*) P_ ((Lisp_Object)),
111 int, Lisp_Object, Lisp_Object*, int));
113 /* Keymap object support - constructors and predicates. */
115 DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 1, 0,
116 "Construct and return a new keymap, of the form (keymap CHARTABLE . ALIST).\n\
117 CHARTABLE is a char-table that holds the bindings for the ASCII\n\
118 characters. ALIST is an assoc-list which holds bindings for function keys,\n\
119 mouse events, and any other things that appear in the input stream.\n\
120 All entries in it are initially nil, meaning \"command undefined\".\n\n\
121 The optional arg STRING supplies a menu name for the keymap\n\
122 in case you use it as a menu with `x-popup-menu'.")
123 (string)
124 Lisp_Object string;
126 Lisp_Object tail;
127 if (!NILP (string))
128 tail = Fcons (string, Qnil);
129 else
130 tail = Qnil;
131 return Fcons (Qkeymap,
132 Fcons (Fmake_char_table (Qkeymap, Qnil), tail));
135 DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 1, 0,
136 "Construct and return a new sparse-keymap list.\n\
137 Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),\n\
138 which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),\n\
139 which binds the function key or mouse event SYMBOL to DEFINITION.\n\
140 Initially the alist is nil.\n\n\
141 The optional arg STRING supplies a menu name for the keymap\n\
142 in case you use it as a menu with `x-popup-menu'.")
143 (string)
144 Lisp_Object string;
146 if (!NILP (string))
147 return Fcons (Qkeymap, Fcons (string, Qnil));
148 return Fcons (Qkeymap, Qnil);
151 /* This function is used for installing the standard key bindings
152 at initialization time.
154 For example:
156 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); */
158 void
159 initial_define_key (keymap, key, defname)
160 Lisp_Object keymap;
161 int key;
162 char *defname;
164 store_in_keymap (keymap, make_number (key), intern (defname));
167 void
168 initial_define_lispy_key (keymap, keyname, defname)
169 Lisp_Object keymap;
170 char *keyname;
171 char *defname;
173 store_in_keymap (keymap, intern (keyname), intern (defname));
176 /* Define character fromchar in map frommap as an alias for character
177 tochar in map tomap. Subsequent redefinitions of the latter WILL
178 affect the former. */
180 #if 0
181 void
182 synkey (frommap, fromchar, tomap, tochar)
183 struct Lisp_Vector *frommap, *tomap;
184 int fromchar, tochar;
186 Lisp_Object v, c;
187 XSETVECTOR (v, tomap);
188 XSETFASTINT (c, tochar);
189 frommap->contents[fromchar] = Fcons (v, c);
191 #endif /* 0 */
193 DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0,
194 "Return t if OBJECT is a keymap.\n\
196 A keymap is a list (keymap . ALIST),\n\
197 or a symbol whose function definition is itself a keymap.\n\
198 ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);\n\
199 a vector of densely packed bindings for small character codes\n\
200 is also allowed as an element.")
201 (object)
202 Lisp_Object object;
204 /* FIXME: Maybe this should return t for autoloaded keymaps? -sm */
205 return (NILP (get_keymap_1 (object, 0, 0)) ? Qnil : Qt);
208 /* Check that OBJECT is a keymap (after dereferencing through any
209 symbols). If it is, return it.
211 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
212 is an autoload form, do the autoload and try again.
213 If AUTOLOAD is nonzero, callers must assume GC is possible.
215 ERROR controls how we respond if OBJECT isn't a keymap.
216 If ERROR is non-zero, signal an error; otherwise, just return Qnil.
218 Note that most of the time, we don't want to pursue autoloads.
219 Functions like Faccessible_keymaps which scan entire keymap trees
220 shouldn't load every autoloaded keymap. I'm not sure about this,
221 but it seems to me that only read_key_sequence, Flookup_key, and
222 Fdefine_key should cause keymaps to be autoloaded.
224 This function can GC when AUTOLOAD is non-zero, because it calls
225 do_autoload which can GC. */
227 Lisp_Object
228 get_keymap_1 (object, error, autoload)
229 Lisp_Object object;
230 int error, autoload;
232 Lisp_Object tem;
234 autoload_retry:
235 if (NILP (object))
236 goto end;
237 if (CONSP (object) && EQ (XCAR (object), Qkeymap))
238 return object;
239 else
241 tem = indirect_function (object);
242 if (CONSP (tem) && EQ (XCAR (tem), Qkeymap))
243 return tem;
246 /* Should we do an autoload? Autoload forms for keymaps have
247 Qkeymap as their fifth element. */
248 if (autoload
249 && SYMBOLP (object)
250 && CONSP (tem)
251 && EQ (XCAR (tem), Qautoload))
253 Lisp_Object tail;
255 tail = Fnth (make_number (4), tem);
256 if (EQ (tail, Qkeymap))
258 struct gcpro gcpro1, gcpro2;
260 GCPRO2 (tem, object);
261 do_autoload (tem, object);
262 UNGCPRO;
264 goto autoload_retry;
268 end:
269 if (error)
270 wrong_type_argument (Qkeymapp, object);
271 return Qnil;
275 /* Follow any symbol chaining, and return the keymap denoted by OBJECT.
276 If OBJECT doesn't denote a keymap at all, signal an error. */
277 Lisp_Object
278 get_keymap (object)
279 Lisp_Object object;
281 return get_keymap_1 (object, 1, 0);
284 /* Return the parent map of the keymap MAP, or nil if it has none.
285 We assume that MAP is a valid keymap. */
287 DEFUN ("keymap-parent", Fkeymap_parent, Skeymap_parent, 1, 1, 0,
288 "Return the parent keymap of KEYMAP.")
289 (keymap)
290 Lisp_Object keymap;
292 Lisp_Object list;
294 keymap = get_keymap_1 (keymap, 1, 1);
296 /* Skip past the initial element `keymap'. */
297 list = XCDR (keymap);
298 for (; CONSP (list); list = XCDR (list))
300 /* See if there is another `keymap'. */
301 if (KEYMAPP (list))
302 return list;
305 return Qnil;
309 /* Check whether MAP is one of MAPS parents. */
311 keymap_memberp (map, maps)
312 Lisp_Object map, maps;
314 while (KEYMAPP (maps) && !EQ (map, maps))
315 maps = Fkeymap_parent (maps);
316 return (EQ (map, maps));
319 /* Set the parent keymap of MAP to PARENT. */
321 DEFUN ("set-keymap-parent", Fset_keymap_parent, Sset_keymap_parent, 2, 2, 0,
322 "Modify KEYMAP to set its parent map to PARENT.\n\
323 PARENT should be nil or another keymap.")
324 (keymap, parent)
325 Lisp_Object keymap, parent;
327 Lisp_Object list, prev;
328 struct gcpro gcpro1;
329 int i;
331 keymap = get_keymap_1 (keymap, 1, 1);
332 GCPRO1 (keymap);
334 if (!NILP (parent))
336 parent = get_keymap_1 (parent, 1, 1);
338 /* Check for cycles. */
339 if (keymap_memberp (keymap, parent))
340 error ("Cyclic keymap inheritance");
343 /* Skip past the initial element `keymap'. */
344 prev = keymap;
345 while (1)
347 list = XCDR (prev);
348 /* If there is a parent keymap here, replace it.
349 If we came to the end, add the parent in PREV. */
350 if (! CONSP (list) || KEYMAPP (list))
352 /* If we already have the right parent, return now
353 so that we avoid the loops below. */
354 if (EQ (XCDR (prev), parent))
355 RETURN_UNGCPRO (parent);
357 XCDR (prev) = parent;
358 break;
360 prev = list;
363 /* Scan through for submaps, and set their parents too. */
365 for (list = XCDR (keymap); CONSP (list); list = XCDR (list))
367 /* Stop the scan when we come to the parent. */
368 if (EQ (XCAR (list), Qkeymap))
369 break;
371 /* If this element holds a prefix map, deal with it. */
372 if (CONSP (XCAR (list))
373 && CONSP (XCDR (XCAR (list))))
374 fix_submap_inheritance (keymap, XCAR (XCAR (list)),
375 XCDR (XCAR (list)));
377 if (VECTORP (XCAR (list)))
378 for (i = 0; i < XVECTOR (XCAR (list))->size; i++)
379 if (CONSP (XVECTOR (XCAR (list))->contents[i]))
380 fix_submap_inheritance (keymap, make_number (i),
381 XVECTOR (XCAR (list))->contents[i]);
383 if (CHAR_TABLE_P (XCAR (list)))
385 Lisp_Object indices[3];
387 map_char_table (fix_submap_inheritance, Qnil, XCAR (list),
388 keymap, 0, indices);
392 RETURN_UNGCPRO (parent);
395 /* EVENT is defined in MAP as a prefix, and SUBMAP is its definition.
396 if EVENT is also a prefix in MAP's parent,
397 make sure that SUBMAP inherits that definition as its own parent. */
399 static void
400 fix_submap_inheritance (map, event, submap)
401 Lisp_Object map, event, submap;
403 Lisp_Object map_parent, parent_entry;
405 /* SUBMAP is a cons that we found as a key binding.
406 Discard the other things found in a menu key binding. */
408 submap = get_keymap_1 (get_keyelt (submap, 0), 0, 0);
410 /* If it isn't a keymap now, there's no work to do. */
411 if (NILP (submap))
412 return;
414 map_parent = Fkeymap_parent (map);
415 if (! NILP (map_parent))
416 parent_entry = get_keyelt (access_keymap (map_parent, event, 0, 0), 0);
417 else
418 parent_entry = Qnil;
420 /* If MAP's parent has something other than a keymap,
421 our own submap shadows it completely, so use nil as SUBMAP's parent. */
422 if (! KEYMAPP (parent_entry))
423 parent_entry = Qnil;
425 if (! EQ (parent_entry, submap))
427 Lisp_Object submap_parent;
428 submap_parent = submap;
429 while (1)
431 Lisp_Object tem;
432 tem = Fkeymap_parent (submap_parent);
433 if (keymap_memberp (tem, parent_entry))
434 /* Fset_keymap_parent could create a cycle. */
435 return;
436 if (KEYMAPP (tem))
437 submap_parent = tem;
438 else
439 break;
441 Fset_keymap_parent (submap_parent, parent_entry);
445 /* Look up IDX in MAP. IDX may be any sort of event.
446 Note that this does only one level of lookup; IDX must be a single
447 event, not a sequence.
449 If T_OK is non-zero, bindings for Qt are treated as default
450 bindings; any key left unmentioned by other tables and bindings is
451 given the binding of Qt.
453 If T_OK is zero, bindings for Qt are not treated specially.
455 If NOINHERIT, don't accept a subkeymap found in an inherited keymap. */
457 Lisp_Object
458 access_keymap (map, idx, t_ok, noinherit)
459 Lisp_Object map;
460 Lisp_Object idx;
461 int t_ok;
462 int noinherit;
464 int noprefix = 0;
465 Lisp_Object val;
467 /* If idx is a list (some sort of mouse click, perhaps?),
468 the index we want to use is the car of the list, which
469 ought to be a symbol. */
470 idx = EVENT_HEAD (idx);
472 /* If idx is a symbol, it might have modifiers, which need to
473 be put in the canonical order. */
474 if (SYMBOLP (idx))
475 idx = reorder_modifiers (idx);
476 else if (INTEGERP (idx))
477 /* Clobber the high bits that can be present on a machine
478 with more than 24 bits of integer. */
479 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
482 Lisp_Object tail;
483 Lisp_Object t_binding;
485 t_binding = Qnil;
486 for (tail = map; CONSP (tail); tail = XCDR (tail))
488 Lisp_Object binding;
490 binding = XCAR (tail);
491 if (SYMBOLP (binding))
493 /* If NOINHERIT, stop finding prefix definitions
494 after we pass a second occurrence of the `keymap' symbol. */
495 if (noinherit && EQ (binding, Qkeymap) && ! EQ (tail, map))
496 noprefix = 1;
498 else if (CONSP (binding))
500 if (EQ (XCAR (binding), idx))
502 val = XCDR (binding);
503 if (noprefix && KEYMAPP (val))
504 return Qnil;
505 if (CONSP (val))
506 fix_submap_inheritance (map, idx, val);
507 return val;
509 if (t_ok && EQ (XCAR (binding), Qt))
510 t_binding = XCDR (binding);
512 else if (VECTORP (binding))
514 if (NATNUMP (idx) && XFASTINT (idx) < XVECTOR (binding)->size)
516 val = XVECTOR (binding)->contents[XFASTINT (idx)];
517 if (noprefix && KEYMAPP (val))
518 return Qnil;
519 if (CONSP (val))
520 fix_submap_inheritance (map, idx, val);
521 return val;
524 else if (CHAR_TABLE_P (binding))
526 /* Character codes with modifiers
527 are not included in a char-table.
528 All character codes without modifiers are included. */
529 if (NATNUMP (idx)
530 && ! (XFASTINT (idx)
531 & (CHAR_ALT | CHAR_SUPER | CHAR_HYPER
532 | CHAR_SHIFT | CHAR_CTL | CHAR_META)))
534 val = Faref (binding, idx);
535 if (noprefix && KEYMAPP (val))
536 return Qnil;
537 if (CONSP (val))
538 fix_submap_inheritance (map, idx, val);
539 return val;
543 QUIT;
546 return t_binding;
550 /* Given OBJECT which was found in a slot in a keymap,
551 trace indirect definitions to get the actual definition of that slot.
552 An indirect definition is a list of the form
553 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
554 and INDEX is the object to look up in KEYMAP to yield the definition.
556 Also if OBJECT has a menu string as the first element,
557 remove that. Also remove a menu help string as second element.
559 If AUTOLOAD is nonzero, load autoloadable keymaps
560 that are referred to with indirection. */
562 Lisp_Object
563 get_keyelt (object, autoload)
564 register Lisp_Object object;
565 int autoload;
567 while (1)
569 if (!(CONSP (object)))
570 /* This is really the value. */
571 return object;
573 /* If the keymap contents looks like (keymap ...) or (lambda ...)
574 then use itself. */
575 else if (EQ (XCAR (object), Qkeymap) || EQ (XCAR (object), Qlambda))
576 return object;
578 /* If the keymap contents looks like (menu-item name . DEFN)
579 or (menu-item name DEFN ...) then use DEFN.
580 This is a new format menu item. */
581 else if (EQ (XCAR (object), Qmenu_item))
583 if (CONSP (XCDR (object)))
585 Lisp_Object tem;
587 object = XCDR (XCDR (object));
588 tem = object;
589 if (CONSP (object))
590 object = XCAR (object);
592 /* If there's a `:filter FILTER', apply FILTER to the
593 menu-item's definition to get the real definition to
594 use. Temporarily inhibit GC while evaluating FILTER,
595 because not functions calling get_keyelt are prepared
596 for a GC. */
597 for (; CONSP (tem) && CONSP (XCDR (tem)); tem = XCDR (tem))
598 if (EQ (XCAR (tem), QCfilter))
600 int count = inhibit_garbage_collection ();
601 Lisp_Object filter;
602 filter = XCAR (XCDR (tem));
603 filter = list2 (filter, list2 (Qquote, object));
604 object = menu_item_eval_property (filter);
605 unbind_to (count, Qnil);
606 break;
609 else
610 /* Invalid keymap */
611 return object;
614 /* If the keymap contents looks like (STRING . DEFN), use DEFN.
615 Keymap alist elements like (CHAR MENUSTRING . DEFN)
616 will be used by HierarKey menus. */
617 else if (STRINGP (XCAR (object)))
619 object = XCDR (object);
620 /* Also remove a menu help string, if any,
621 following the menu item name. */
622 if (CONSP (object) && STRINGP (XCAR (object)))
623 object = XCDR (object);
624 /* Also remove the sublist that caches key equivalences, if any. */
625 if (CONSP (object) && CONSP (XCAR (object)))
627 Lisp_Object carcar;
628 carcar = XCAR (XCAR (object));
629 if (NILP (carcar) || VECTORP (carcar))
630 object = XCDR (object);
634 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
635 else
637 Lisp_Object map;
639 map = get_keymap_1 (Fcar_safe (object), 0, autoload);
640 if (NILP (map))
641 /* Invalid keymap */
642 return object;
643 else
645 Lisp_Object key;
646 key = Fcdr (object);
647 if (INTEGERP (key) && (XUINT (key) & meta_modifier))
649 object = access_keymap (map, meta_prefix_char, 0, 0);
650 map = get_keymap_1 (object, 0, autoload);
651 object = access_keymap (map, make_number (XINT (key)
652 & ~meta_modifier),
653 0, 0);
655 else
656 object = access_keymap (map, key, 0, 0);
662 static Lisp_Object
663 store_in_keymap (keymap, idx, def)
664 Lisp_Object keymap;
665 register Lisp_Object idx;
666 register Lisp_Object def;
668 /* If we are preparing to dump, and DEF is a menu element
669 with a menu item indicator, copy it to ensure it is not pure. */
670 if (CONSP (def) && PURE_P (def)
671 && (EQ (XCAR (def), Qmenu_item) || STRINGP (XCAR (def))))
672 def = Fcons (XCAR (def), XCDR (def));
674 if (!CONSP (keymap) || ! EQ (XCAR (keymap), Qkeymap))
675 error ("attempt to define a key in a non-keymap");
677 /* If idx is a list (some sort of mouse click, perhaps?),
678 the index we want to use is the car of the list, which
679 ought to be a symbol. */
680 idx = EVENT_HEAD (idx);
682 /* If idx is a symbol, it might have modifiers, which need to
683 be put in the canonical order. */
684 if (SYMBOLP (idx))
685 idx = reorder_modifiers (idx);
686 else if (INTEGERP (idx))
687 /* Clobber the high bits that can be present on a machine
688 with more than 24 bits of integer. */
689 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
691 /* Scan the keymap for a binding of idx. */
693 Lisp_Object tail;
695 /* The cons after which we should insert new bindings. If the
696 keymap has a table element, we record its position here, so new
697 bindings will go after it; this way, the table will stay
698 towards the front of the alist and character lookups in dense
699 keymaps will remain fast. Otherwise, this just points at the
700 front of the keymap. */
701 Lisp_Object insertion_point;
703 insertion_point = keymap;
704 for (tail = XCDR (keymap); CONSP (tail); tail = XCDR (tail))
706 Lisp_Object elt;
708 elt = XCAR (tail);
709 if (VECTORP (elt))
711 if (NATNUMP (idx) && XFASTINT (idx) < XVECTOR (elt)->size)
713 XVECTOR (elt)->contents[XFASTINT (idx)] = def;
714 return def;
716 insertion_point = tail;
718 else if (CHAR_TABLE_P (elt))
720 /* Character codes with modifiers
721 are not included in a char-table.
722 All character codes without modifiers are included. */
723 if (NATNUMP (idx)
724 && ! (XFASTINT (idx)
725 & (CHAR_ALT | CHAR_SUPER | CHAR_HYPER
726 | CHAR_SHIFT | CHAR_CTL | CHAR_META)))
728 Faset (elt, idx, def);
729 return def;
731 insertion_point = tail;
733 else if (CONSP (elt))
735 if (EQ (idx, XCAR (elt)))
737 XCDR (elt) = def;
738 return def;
741 else if (SYMBOLP (elt))
743 /* If we find a 'keymap' symbol in the spine of KEYMAP,
744 then we must have found the start of a second keymap
745 being used as the tail of KEYMAP, and a binding for IDX
746 should be inserted before it. */
747 if (EQ (elt, Qkeymap))
748 goto keymap_end;
751 QUIT;
754 keymap_end:
755 /* We have scanned the entire keymap, and not found a binding for
756 IDX. Let's add one. */
757 XCDR (insertion_point)
758 = Fcons (Fcons (idx, def), XCDR (insertion_point));
761 return def;
764 void
765 copy_keymap_1 (chartable, idx, elt)
766 Lisp_Object chartable, idx, elt;
768 if (!SYMBOLP (elt) && ! NILP (Fkeymapp (elt)))
769 Faset (chartable, idx, Fcopy_keymap (elt));
772 DEFUN ("copy-keymap", Fcopy_keymap, Scopy_keymap, 1, 1, 0,
773 "Return a copy of the keymap KEYMAP.\n\
774 The copy starts out with the same definitions of KEYMAP,\n\
775 but changing either the copy or KEYMAP does not affect the other.\n\
776 Any key definitions that are subkeymaps are recursively copied.\n\
777 However, a key definition which is a symbol whose definition is a keymap\n\
778 is not copied.")
779 (keymap)
780 Lisp_Object keymap;
782 register Lisp_Object copy, tail;
784 copy = Fcopy_alist (get_keymap (keymap));
786 for (tail = copy; CONSP (tail); tail = XCDR (tail))
788 Lisp_Object elt;
790 elt = XCAR (tail);
791 if (CHAR_TABLE_P (elt))
793 Lisp_Object indices[3];
795 elt = Fcopy_sequence (elt);
796 XCAR (tail) = elt;
798 map_char_table (copy_keymap_1, Qnil, elt, elt, 0, indices);
800 else if (VECTORP (elt))
802 int i;
804 elt = Fcopy_sequence (elt);
805 XCAR (tail) = elt;
807 for (i = 0; i < XVECTOR (elt)->size; i++)
808 if (!SYMBOLP (XVECTOR (elt)->contents[i])
809 && ! NILP (Fkeymapp (XVECTOR (elt)->contents[i])))
810 XVECTOR (elt)->contents[i]
811 = Fcopy_keymap (XVECTOR (elt)->contents[i]);
813 else if (CONSP (elt) && CONSP (XCDR (elt)))
815 Lisp_Object tem;
816 tem = XCDR (elt);
818 /* Is this a new format menu item. */
819 if (EQ (XCAR (tem),Qmenu_item))
821 /* Copy cell with menu-item marker. */
822 XCDR (elt)
823 = Fcons (XCAR (tem), XCDR (tem));
824 elt = XCDR (elt);
825 tem = XCDR (elt);
826 if (CONSP (tem))
828 /* Copy cell with menu-item name. */
829 XCDR (elt)
830 = Fcons (XCAR (tem), XCDR (tem));
831 elt = XCDR (elt);
832 tem = XCDR (elt);
834 if (CONSP (tem))
836 /* Copy cell with binding and if the binding is a keymap,
837 copy that. */
838 XCDR (elt)
839 = Fcons (XCAR (tem), XCDR (tem));
840 elt = XCDR (elt);
841 tem = XCAR (elt);
842 if (!(SYMBOLP (tem) || NILP (Fkeymapp (tem))))
843 XCAR (elt) = Fcopy_keymap (tem);
844 tem = XCDR (elt);
845 if (CONSP (tem) && CONSP (XCAR (tem)))
846 /* Delete cache for key equivalences. */
847 XCDR (elt) = XCDR (tem);
850 else
852 /* It may be an old fomat menu item.
853 Skip the optional menu string.
855 if (STRINGP (XCAR (tem)))
857 /* Copy the cell, since copy-alist didn't go this deep. */
858 XCDR (elt)
859 = Fcons (XCAR (tem), XCDR (tem));
860 elt = XCDR (elt);
861 tem = XCDR (elt);
862 /* Also skip the optional menu help string. */
863 if (CONSP (tem) && STRINGP (XCAR (tem)))
865 XCDR (elt)
866 = Fcons (XCAR (tem), XCDR (tem));
867 elt = XCDR (elt);
868 tem = XCDR (elt);
870 /* There may also be a list that caches key equivalences.
871 Just delete it for the new keymap. */
872 if (CONSP (tem)
873 && CONSP (XCAR (tem))
874 && (NILP (XCAR (XCAR (tem)))
875 || VECTORP (XCAR (XCAR (tem)))))
876 XCDR (elt) = XCDR (tem);
878 if (CONSP (elt)
879 && ! SYMBOLP (XCDR (elt))
880 && ! NILP (Fkeymapp (XCDR (elt))))
881 XCDR (elt) = Fcopy_keymap (XCDR (elt));
887 return copy;
890 /* Simple Keymap mutators and accessors. */
892 /* GC is possible in this function if it autoloads a keymap. */
894 DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0,
895 "Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.\n\
896 KEYMAP is a keymap. KEY is a string or a vector of symbols and characters\n\
897 meaning a sequence of keystrokes and events.\n\
898 Non-ASCII characters with codes above 127 (such as ISO Latin-1)\n\
899 can be included if you use a vector.\n\
900 DEF is anything that can be a key's definition:\n\
901 nil (means key is undefined in this keymap),\n\
902 a command (a Lisp function suitable for interactive calling)\n\
903 a string (treated as a keyboard macro),\n\
904 a keymap (to define a prefix key),\n\
905 a symbol. When the key is looked up, the symbol will stand for its\n\
906 function definition, which should at that time be one of the above,\n\
907 or another symbol whose function definition is used, etc.\n\
908 a cons (STRING . DEFN), meaning that DEFN is the definition\n\
909 (DEFN should be a valid definition in its own right),\n\
910 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.\n\
912 If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at\n\
913 the front of KEYMAP.")
914 (keymap, key, def)
915 Lisp_Object keymap;
916 Lisp_Object key;
917 Lisp_Object def;
919 register int idx;
920 register Lisp_Object c;
921 register Lisp_Object cmd;
922 int metized = 0;
923 int meta_bit;
924 int length;
925 struct gcpro gcpro1, gcpro2, gcpro3;
927 keymap = get_keymap_1 (keymap, 1, 1);
929 if (!VECTORP (key) && !STRINGP (key))
930 key = wrong_type_argument (Qarrayp, key);
932 length = XFASTINT (Flength (key));
933 if (length == 0)
934 return Qnil;
936 if (SYMBOLP (def) && !EQ (Vdefine_key_rebound_commands, Qt))
937 Vdefine_key_rebound_commands = Fcons (def, Vdefine_key_rebound_commands);
939 GCPRO3 (keymap, key, def);
941 if (VECTORP (key))
942 meta_bit = meta_modifier;
943 else
944 meta_bit = 0x80;
946 idx = 0;
947 while (1)
949 c = Faref (key, make_number (idx));
951 if (CONSP (c) && lucid_event_type_list_p (c))
952 c = Fevent_convert_list (c);
954 if (INTEGERP (c)
955 && (XINT (c) & meta_bit)
956 && !metized)
958 c = meta_prefix_char;
959 metized = 1;
961 else
963 if (INTEGERP (c))
964 XSETINT (c, XINT (c) & ~meta_bit);
966 metized = 0;
967 idx++;
970 if (! INTEGERP (c) && ! SYMBOLP (c) && ! CONSP (c))
971 error ("Key sequence contains invalid events");
973 if (idx == length)
974 RETURN_UNGCPRO (store_in_keymap (keymap, c, def));
976 cmd = get_keyelt (access_keymap (keymap, c, 0, 1), 1);
978 /* If this key is undefined, make it a prefix. */
979 if (NILP (cmd))
980 cmd = define_as_prefix (keymap, c);
982 keymap = get_keymap_1 (cmd, 0, 1);
983 if (NILP (keymap))
984 /* We must use Fkey_description rather than just passing key to
985 error; key might be a vector, not a string. */
986 error ("Key sequence %s uses invalid prefix characters",
987 XSTRING (Fkey_description (key))->data);
991 /* Value is number if KEY is too long; NIL if valid but has no definition. */
992 /* GC is possible in this function if it autoloads a keymap. */
994 DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 3, 0,
995 "In keymap KEYMAP, look up key sequence KEY. Return the definition.\n\
996 nil means undefined. See doc of `define-key' for kinds of definitions.\n\
998 A number as value means KEY is \"too long\";\n\
999 that is, characters or symbols in it except for the last one\n\
1000 fail to be a valid sequence of prefix characters in KEYMAP.\n\
1001 The number is how many characters at the front of KEY\n\
1002 it takes to reach a non-prefix command.\n\
1004 Normally, `lookup-key' ignores bindings for t, which act as default\n\
1005 bindings, used when nothing else in the keymap applies; this makes it\n\
1006 usable as a general function for probing keymaps. However, if the\n\
1007 third optional argument ACCEPT-DEFAULT is non-nil, `lookup-key' will\n\
1008 recognize the default bindings, just as `read-key-sequence' does.")
1009 (keymap, key, accept_default)
1010 register Lisp_Object keymap;
1011 Lisp_Object key;
1012 Lisp_Object accept_default;
1014 register int idx;
1015 register Lisp_Object cmd;
1016 register Lisp_Object c;
1017 int metized = 0;
1018 int length;
1019 int t_ok = ! NILP (accept_default);
1020 int meta_bit;
1021 struct gcpro gcpro1;
1023 keymap = get_keymap_1 (keymap, 1, 1);
1025 if (!VECTORP (key) && !STRINGP (key))
1026 key = wrong_type_argument (Qarrayp, key);
1028 length = XFASTINT (Flength (key));
1029 if (length == 0)
1030 return keymap;
1032 if (VECTORP (key))
1033 meta_bit = meta_modifier;
1034 else
1035 meta_bit = 0x80;
1037 GCPRO1 (key);
1039 idx = 0;
1040 while (1)
1042 c = Faref (key, make_number (idx));
1044 if (CONSP (c) && lucid_event_type_list_p (c))
1045 c = Fevent_convert_list (c);
1047 if (INTEGERP (c)
1048 && (XINT (c) & meta_bit)
1049 && !metized)
1051 c = meta_prefix_char;
1052 metized = 1;
1054 else
1056 if (INTEGERP (c))
1057 XSETINT (c, XINT (c) & ~meta_bit);
1059 metized = 0;
1060 idx++;
1063 cmd = get_keyelt (access_keymap (keymap, c, t_ok, 0), 1);
1064 if (idx == length)
1065 RETURN_UNGCPRO (cmd);
1067 keymap = get_keymap_1 (cmd, 0, 1);
1068 if (NILP (keymap))
1069 RETURN_UNGCPRO (make_number (idx));
1071 QUIT;
1075 /* Make KEYMAP define event C as a keymap (i.e., as a prefix).
1076 Assume that currently it does not define C at all.
1077 Return the keymap. */
1079 static Lisp_Object
1080 define_as_prefix (keymap, c)
1081 Lisp_Object keymap, c;
1083 Lisp_Object inherit, cmd;
1085 cmd = Fmake_sparse_keymap (Qnil);
1086 /* If this key is defined as a prefix in an inherited keymap,
1087 make it a prefix in this map, and make its definition
1088 inherit the other prefix definition. */
1089 inherit = access_keymap (keymap, c, 0, 0);
1090 #if 0
1091 /* This code is needed to do the right thing in the following case:
1092 keymap A inherits from B,
1093 you define KEY as a prefix in A,
1094 then later you define KEY as a prefix in B.
1095 We want the old prefix definition in A to inherit from that in B.
1096 It is hard to do that retroactively, so this code
1097 creates the prefix in B right away.
1099 But it turns out that this code causes problems immediately
1100 when the prefix in A is defined: it causes B to define KEY
1101 as a prefix with no subcommands.
1103 So I took out this code. */
1104 if (NILP (inherit))
1106 /* If there's an inherited keymap
1107 and it doesn't define this key,
1108 make it define this key. */
1109 Lisp_Object tail;
1111 for (tail = Fcdr (keymap); CONSP (tail); tail = XCDR (tail))
1112 if (EQ (XCAR (tail), Qkeymap))
1113 break;
1115 if (!NILP (tail))
1116 inherit = define_as_prefix (tail, c);
1118 #endif
1120 cmd = nconc2 (cmd, inherit);
1121 store_in_keymap (keymap, c, cmd);
1123 return cmd;
1126 /* Append a key to the end of a key sequence. We always make a vector. */
1128 Lisp_Object
1129 append_key (key_sequence, key)
1130 Lisp_Object key_sequence, key;
1132 Lisp_Object args[2];
1134 args[0] = key_sequence;
1136 args[1] = Fcons (key, Qnil);
1137 return Fvconcat (2, args);
1141 /* Global, local, and minor mode keymap stuff. */
1143 /* We can't put these variables inside current_minor_maps, since under
1144 some systems, static gets macro-defined to be the empty string.
1145 Ickypoo. */
1146 static Lisp_Object *cmm_modes, *cmm_maps;
1147 static int cmm_size;
1149 /* Error handler used in current_minor_maps. */
1150 static Lisp_Object
1151 current_minor_maps_error ()
1153 return Qnil;
1156 /* Store a pointer to an array of the keymaps of the currently active
1157 minor modes in *buf, and return the number of maps it contains.
1159 This function always returns a pointer to the same buffer, and may
1160 free or reallocate it, so if you want to keep it for a long time or
1161 hand it out to lisp code, copy it. This procedure will be called
1162 for every key sequence read, so the nice lispy approach (return a
1163 new assoclist, list, what have you) for each invocation would
1164 result in a lot of consing over time.
1166 If we used xrealloc/xmalloc and ran out of memory, they would throw
1167 back to the command loop, which would try to read a key sequence,
1168 which would call this function again, resulting in an infinite
1169 loop. Instead, we'll use realloc/malloc and silently truncate the
1170 list, let the key sequence be read, and hope some other piece of
1171 code signals the error. */
1173 current_minor_maps (modeptr, mapptr)
1174 Lisp_Object **modeptr, **mapptr;
1176 int i = 0;
1177 int list_number = 0;
1178 Lisp_Object alist, assoc, var, val;
1179 Lisp_Object lists[2];
1181 lists[0] = Vminor_mode_overriding_map_alist;
1182 lists[1] = Vminor_mode_map_alist;
1184 for (list_number = 0; list_number < 2; list_number++)
1185 for (alist = lists[list_number];
1186 CONSP (alist);
1187 alist = XCDR (alist))
1188 if ((assoc = XCAR (alist), CONSP (assoc))
1189 && (var = XCAR (assoc), SYMBOLP (var))
1190 && (val = find_symbol_value (var), ! EQ (val, Qunbound))
1191 && ! NILP (val))
1193 Lisp_Object temp;
1195 /* If a variable has an entry in Vminor_mode_overriding_map_alist,
1196 and also an entry in Vminor_mode_map_alist,
1197 ignore the latter. */
1198 if (list_number == 1)
1200 val = assq_no_quit (var, lists[0]);
1201 if (!NILP (val))
1202 break;
1205 if (i >= cmm_size)
1207 Lisp_Object *newmodes, *newmaps;
1209 if (cmm_maps)
1211 BLOCK_INPUT;
1212 cmm_size *= 2;
1213 newmodes
1214 = (Lisp_Object *) realloc (cmm_modes,
1215 cmm_size * sizeof (Lisp_Object));
1216 newmaps
1217 = (Lisp_Object *) realloc (cmm_maps,
1218 cmm_size * sizeof (Lisp_Object));
1219 UNBLOCK_INPUT;
1221 else
1223 BLOCK_INPUT;
1224 cmm_size = 30;
1225 newmodes
1226 = (Lisp_Object *) xmalloc (cmm_size * sizeof (Lisp_Object));
1227 newmaps
1228 = (Lisp_Object *) xmalloc (cmm_size * sizeof (Lisp_Object));
1229 UNBLOCK_INPUT;
1232 if (newmaps && newmodes)
1234 cmm_modes = newmodes;
1235 cmm_maps = newmaps;
1237 else
1238 break;
1241 /* Get the keymap definition--or nil if it is not defined. */
1242 temp = internal_condition_case_1 (Findirect_function,
1243 XCDR (assoc),
1244 Qerror, current_minor_maps_error);
1245 if (!NILP (temp))
1247 cmm_modes[i] = var;
1248 cmm_maps [i] = temp;
1249 i++;
1253 if (modeptr) *modeptr = cmm_modes;
1254 if (mapptr) *mapptr = cmm_maps;
1255 return i;
1258 /* GC is possible in this function if it autoloads a keymap. */
1260 DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 2, 0,
1261 "Return the binding for command KEY in current keymaps.\n\
1262 KEY is a string or vector, a sequence of keystrokes.\n\
1263 The binding is probably a symbol with a function definition.\n\
1265 Normally, `key-binding' ignores bindings for t, which act as default\n\
1266 bindings, used when nothing else in the keymap applies; this makes it\n\
1267 usable as a general function for probing keymaps. However, if the\n\
1268 optional second argument ACCEPT-DEFAULT is non-nil, `key-binding' does\n\
1269 recognize the default bindings, just as `read-key-sequence' does.")
1270 (key, accept_default)
1271 Lisp_Object key, accept_default;
1273 Lisp_Object *maps, value;
1274 int nmaps, i;
1275 struct gcpro gcpro1;
1277 GCPRO1 (key);
1279 if (!NILP (current_kboard->Voverriding_terminal_local_map))
1281 value = Flookup_key (current_kboard->Voverriding_terminal_local_map,
1282 key, accept_default);
1283 if (! NILP (value) && !INTEGERP (value))
1284 RETURN_UNGCPRO (value);
1286 else if (!NILP (Voverriding_local_map))
1288 value = Flookup_key (Voverriding_local_map, key, accept_default);
1289 if (! NILP (value) && !INTEGERP (value))
1290 RETURN_UNGCPRO (value);
1292 else
1294 Lisp_Object local;
1296 nmaps = current_minor_maps (0, &maps);
1297 /* Note that all these maps are GCPRO'd
1298 in the places where we found them. */
1300 for (i = 0; i < nmaps; i++)
1301 if (! NILP (maps[i]))
1303 value = Flookup_key (maps[i], key, accept_default);
1304 if (! NILP (value) && !INTEGERP (value))
1305 RETURN_UNGCPRO (value);
1308 local = get_local_map (PT, current_buffer, keymap);
1309 if (! NILP (local))
1311 value = Flookup_key (local, key, accept_default);
1312 if (! NILP (value) && !INTEGERP (value))
1313 RETURN_UNGCPRO (value);
1316 local = get_local_map (PT, current_buffer, local_map);
1318 if (! NILP (local))
1320 value = Flookup_key (local, key, accept_default);
1321 if (! NILP (value) && !INTEGERP (value))
1322 RETURN_UNGCPRO (value);
1326 value = Flookup_key (current_global_map, key, accept_default);
1327 UNGCPRO;
1328 if (! NILP (value) && !INTEGERP (value))
1329 return value;
1331 return Qnil;
1334 /* GC is possible in this function if it autoloads a keymap. */
1336 DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 2, 0,
1337 "Return the binding for command KEYS in current local keymap only.\n\
1338 KEYS is a string, a sequence of keystrokes.\n\
1339 The binding is probably a symbol with a function definition.\n\
1341 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1342 bindings; see the description of `lookup-key' for more details about this.")
1343 (keys, accept_default)
1344 Lisp_Object keys, accept_default;
1346 register Lisp_Object map;
1347 map = current_buffer->keymap;
1348 if (NILP (map))
1349 return Qnil;
1350 return Flookup_key (map, keys, accept_default);
1353 /* GC is possible in this function if it autoloads a keymap. */
1355 DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 2, 0,
1356 "Return the binding for command KEYS in current global keymap only.\n\
1357 KEYS is a string, a sequence of keystrokes.\n\
1358 The binding is probably a symbol with a function definition.\n\
1359 This function's return values are the same as those of lookup-key\n\
1360 \(which see).\n\
1362 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1363 bindings; see the description of `lookup-key' for more details about this.")
1364 (keys, accept_default)
1365 Lisp_Object keys, accept_default;
1367 return Flookup_key (current_global_map, keys, accept_default);
1370 /* GC is possible in this function if it autoloads a keymap. */
1372 DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0,
1373 "Find the visible minor mode bindings of KEY.\n\
1374 Return an alist of pairs (MODENAME . BINDING), where MODENAME is the\n\
1375 the symbol which names the minor mode binding KEY, and BINDING is\n\
1376 KEY's definition in that mode. In particular, if KEY has no\n\
1377 minor-mode bindings, return nil. If the first binding is a\n\
1378 non-prefix, all subsequent bindings will be omitted, since they would\n\
1379 be ignored. Similarly, the list doesn't include non-prefix bindings\n\
1380 that come after prefix bindings.\n\
1382 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1383 bindings; see the description of `lookup-key' for more details about this.")
1384 (key, accept_default)
1385 Lisp_Object key, accept_default;
1387 Lisp_Object *modes, *maps;
1388 int nmaps;
1389 Lisp_Object binding;
1390 int i, j;
1391 struct gcpro gcpro1, gcpro2;
1393 nmaps = current_minor_maps (&modes, &maps);
1394 /* Note that all these maps are GCPRO'd
1395 in the places where we found them. */
1397 binding = Qnil;
1398 GCPRO2 (key, binding);
1400 for (i = j = 0; i < nmaps; i++)
1401 if (! NILP (maps[i])
1402 && ! NILP (binding = Flookup_key (maps[i], key, accept_default))
1403 && !INTEGERP (binding))
1405 if (! NILP (get_keymap (binding)))
1406 maps[j++] = Fcons (modes[i], binding);
1407 else if (j == 0)
1408 RETURN_UNGCPRO (Fcons (Fcons (modes[i], binding), Qnil));
1411 UNGCPRO;
1412 return Flist (j, maps);
1415 DEFUN ("define-prefix-command", Fdefine_prefix_command, Sdefine_prefix_command, 1, 3, 0,
1416 "Define COMMAND as a prefix command. COMMAND should be a symbol.\n\
1417 A new sparse keymap is stored as COMMAND's function definition and its value.\n\
1418 If a second optional argument MAPVAR is given, the map is stored as\n\
1419 its value instead of as COMMAND's value; but COMMAND is still defined\n\
1420 as a function.\n\
1421 The third optional argument NAME, if given, supplies a menu name\n\
1422 string for the map. This is required to use the keymap as a menu.")
1423 (command, mapvar, name)
1424 Lisp_Object command, mapvar, name;
1426 Lisp_Object map;
1427 map = Fmake_sparse_keymap (name);
1428 Ffset (command, map);
1429 if (!NILP (mapvar))
1430 Fset (mapvar, map);
1431 else
1432 Fset (command, map);
1433 return command;
1436 DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0,
1437 "Select KEYMAP as the global keymap.")
1438 (keymap)
1439 Lisp_Object keymap;
1441 keymap = get_keymap (keymap);
1442 current_global_map = keymap;
1444 return Qnil;
1447 DEFUN ("use-local-map", Fuse_local_map, Suse_local_map, 1, 1, 0,
1448 "Select KEYMAP as the local keymap.\n\
1449 If KEYMAP is nil, that means no local keymap.")
1450 (keymap)
1451 Lisp_Object keymap;
1453 if (!NILP (keymap))
1454 keymap = get_keymap (keymap);
1456 current_buffer->keymap = keymap;
1458 return Qnil;
1461 DEFUN ("current-local-map", Fcurrent_local_map, Scurrent_local_map, 0, 0, 0,
1462 "Return current buffer's local keymap, or nil if it has none.")
1465 return current_buffer->keymap;
1468 DEFUN ("current-global-map", Fcurrent_global_map, Scurrent_global_map, 0, 0, 0,
1469 "Return the current global keymap.")
1472 return current_global_map;
1475 DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps, Scurrent_minor_mode_maps, 0, 0, 0,
1476 "Return a list of keymaps for the minor modes of the current buffer.")
1479 Lisp_Object *maps;
1480 int nmaps = current_minor_maps (0, &maps);
1482 return Flist (nmaps, maps);
1485 /* Help functions for describing and documenting keymaps. */
1487 static void accessible_keymaps_char_table ();
1489 /* This function cannot GC. */
1491 DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps,
1492 1, 2, 0,
1493 "Find all keymaps accessible via prefix characters from KEYMAP.\n\
1494 Returns a list of elements of the form (KEYS . MAP), where the sequence\n\
1495 KEYS starting from KEYMAP gets you to MAP. These elements are ordered\n\
1496 so that the KEYS increase in length. The first element is ([] . KEYMAP).\n\
1497 An optional argument PREFIX, if non-nil, should be a key sequence;\n\
1498 then the value includes only maps for prefixes that start with PREFIX.")
1499 (keymap, prefix)
1500 Lisp_Object keymap, prefix;
1502 Lisp_Object maps, good_maps, tail;
1503 int prefixlen = 0;
1505 /* no need for gcpro because we don't autoload any keymaps. */
1507 if (!NILP (prefix))
1508 prefixlen = XINT (Flength (prefix));
1510 if (!NILP (prefix))
1512 /* If a prefix was specified, start with the keymap (if any) for
1513 that prefix, so we don't waste time considering other prefixes. */
1514 Lisp_Object tem;
1515 tem = Flookup_key (keymap, prefix, Qt);
1516 /* Flookup_key may give us nil, or a number,
1517 if the prefix is not defined in this particular map.
1518 It might even give us a list that isn't a keymap. */
1519 tem = get_keymap_1 (tem, 0, 0);
1520 if (!NILP (tem))
1522 /* Convert PREFIX to a vector now, so that later on
1523 we don't have to deal with the possibility of a string. */
1524 if (STRINGP (prefix))
1526 int i, i_byte, c;
1527 Lisp_Object copy;
1529 copy = Fmake_vector (make_number (XSTRING (prefix)->size), Qnil);
1530 for (i = 0, i_byte = 0; i < XSTRING (prefix)->size;)
1532 int i_before = i;
1534 FETCH_STRING_CHAR_ADVANCE (c, prefix, i, i_byte);
1535 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
1536 c ^= 0200 | meta_modifier;
1537 XVECTOR (copy)->contents[i_before] = make_number (c);
1539 prefix = copy;
1541 maps = Fcons (Fcons (prefix, tem), Qnil);
1543 else
1544 return Qnil;
1546 else
1547 maps = Fcons (Fcons (Fmake_vector (make_number (0), Qnil),
1548 get_keymap (keymap)),
1549 Qnil);
1551 /* For each map in the list maps,
1552 look at any other maps it points to,
1553 and stick them at the end if they are not already in the list.
1555 This is a breadth-first traversal, where tail is the queue of
1556 nodes, and maps accumulates a list of all nodes visited. */
1558 for (tail = maps; CONSP (tail); tail = XCDR (tail))
1560 register Lisp_Object thisseq, thismap;
1561 Lisp_Object last;
1562 /* Does the current sequence end in the meta-prefix-char? */
1563 int is_metized;
1565 thisseq = Fcar (Fcar (tail));
1566 thismap = Fcdr (Fcar (tail));
1567 last = make_number (XINT (Flength (thisseq)) - 1);
1568 is_metized = (XINT (last) >= 0
1569 /* Don't metize the last char of PREFIX. */
1570 && XINT (last) >= prefixlen
1571 && EQ (Faref (thisseq, last), meta_prefix_char));
1573 for (; CONSP (thismap); thismap = XCDR (thismap))
1575 Lisp_Object elt;
1577 elt = XCAR (thismap);
1579 QUIT;
1581 if (CHAR_TABLE_P (elt))
1583 Lisp_Object indices[3];
1585 map_char_table (accessible_keymaps_char_table, Qnil,
1586 elt, Fcons (maps, Fcons (tail, thisseq)),
1587 0, indices);
1589 else if (VECTORP (elt))
1591 register int i;
1593 /* Vector keymap. Scan all the elements. */
1594 for (i = 0; i < XVECTOR (elt)->size; i++)
1596 register Lisp_Object tem;
1597 register Lisp_Object cmd;
1599 cmd = get_keyelt (XVECTOR (elt)->contents[i], 0);
1600 if (NILP (cmd)) continue;
1601 tem = Fkeymapp (cmd);
1602 if (!NILP (tem))
1604 cmd = get_keymap (cmd);
1605 /* Ignore keymaps that are already added to maps. */
1606 tem = Frassq (cmd, maps);
1607 if (NILP (tem))
1609 /* If the last key in thisseq is meta-prefix-char,
1610 turn it into a meta-ized keystroke. We know
1611 that the event we're about to append is an
1612 ascii keystroke since we're processing a
1613 keymap table. */
1614 if (is_metized)
1616 int meta_bit = meta_modifier;
1617 tem = Fcopy_sequence (thisseq);
1619 Faset (tem, last, make_number (i | meta_bit));
1621 /* This new sequence is the same length as
1622 thisseq, so stick it in the list right
1623 after this one. */
1624 XCDR (tail)
1625 = Fcons (Fcons (tem, cmd), XCDR (tail));
1627 else
1629 tem = append_key (thisseq, make_number (i));
1630 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
1636 else if (CONSP (elt))
1638 register Lisp_Object cmd, tem;
1640 cmd = get_keyelt (XCDR (elt), 0);
1641 /* Ignore definitions that aren't keymaps themselves. */
1642 tem = Fkeymapp (cmd);
1643 if (!NILP (tem))
1645 /* Ignore keymaps that have been seen already. */
1646 cmd = get_keymap (cmd);
1647 tem = Frassq (cmd, maps);
1648 if (NILP (tem))
1650 /* Let elt be the event defined by this map entry. */
1651 elt = XCAR (elt);
1653 /* If the last key in thisseq is meta-prefix-char, and
1654 this entry is a binding for an ascii keystroke,
1655 turn it into a meta-ized keystroke. */
1656 if (is_metized && INTEGERP (elt))
1658 Lisp_Object element;
1660 element = thisseq;
1661 tem = Fvconcat (1, &element);
1662 XSETFASTINT (XVECTOR (tem)->contents[XINT (last)],
1663 XINT (elt) | meta_modifier);
1665 /* This new sequence is the same length as
1666 thisseq, so stick it in the list right
1667 after this one. */
1668 XCDR (tail)
1669 = Fcons (Fcons (tem, cmd), XCDR (tail));
1671 else
1672 nconc2 (tail,
1673 Fcons (Fcons (append_key (thisseq, elt), cmd),
1674 Qnil));
1681 if (NILP (prefix))
1682 return maps;
1684 /* Now find just the maps whose access prefixes start with PREFIX. */
1686 good_maps = Qnil;
1687 for (; CONSP (maps); maps = XCDR (maps))
1689 Lisp_Object elt, thisseq;
1690 elt = XCAR (maps);
1691 thisseq = XCAR (elt);
1692 /* The access prefix must be at least as long as PREFIX,
1693 and the first elements must match those of PREFIX. */
1694 if (XINT (Flength (thisseq)) >= prefixlen)
1696 int i;
1697 for (i = 0; i < prefixlen; i++)
1699 Lisp_Object i1;
1700 XSETFASTINT (i1, i);
1701 if (!EQ (Faref (thisseq, i1), Faref (prefix, i1)))
1702 break;
1704 if (i == prefixlen)
1705 good_maps = Fcons (elt, good_maps);
1709 return Fnreverse (good_maps);
1712 static void
1713 accessible_keymaps_char_table (args, index, cmd)
1714 Lisp_Object args, index, cmd;
1716 Lisp_Object tem;
1717 Lisp_Object maps, tail, thisseq;
1719 if (NILP (cmd))
1720 return;
1722 maps = XCAR (args);
1723 tail = XCAR (XCDR (args));
1724 thisseq = XCDR (XCDR (args));
1726 tem = Fkeymapp (cmd);
1727 if (!NILP (tem))
1729 cmd = get_keymap (cmd);
1730 /* Ignore keymaps that are already added to maps. */
1731 tem = Frassq (cmd, maps);
1732 if (NILP (tem))
1734 tem = append_key (thisseq, index);
1735 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
1740 Lisp_Object Qsingle_key_description, Qkey_description;
1742 /* This function cannot GC. */
1744 DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0,
1745 "Return a pretty description of key-sequence KEYS.\n\
1746 Control characters turn into \"C-foo\" sequences, meta into \"M-foo\"\n\
1747 spaces are put between sequence elements, etc.")
1748 (keys)
1749 Lisp_Object keys;
1751 int len = 0;
1752 int i, i_byte;
1753 Lisp_Object sep;
1754 Lisp_Object *args = NULL;
1756 if (STRINGP (keys))
1758 Lisp_Object vector;
1759 vector = Fmake_vector (Flength (keys), Qnil);
1760 for (i = 0, i_byte = 0; i < XSTRING (keys)->size; )
1762 int c;
1763 int i_before = i;
1765 FETCH_STRING_CHAR_ADVANCE (c, keys, i, i_byte);
1766 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
1767 c ^= 0200 | meta_modifier;
1768 XSETFASTINT (XVECTOR (vector)->contents[i_before], c);
1770 keys = vector;
1773 if (VECTORP (keys))
1775 /* In effect, this computes
1776 (mapconcat 'single-key-description keys " ")
1777 but we shouldn't use mapconcat because it can do GC. */
1779 len = XVECTOR (keys)->size;
1780 sep = build_string (" ");
1781 /* This has one extra element at the end that we don't pass to Fconcat. */
1782 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
1784 for (i = 0; i < len; i++)
1786 args[i * 2] = Fsingle_key_description (XVECTOR (keys)->contents[i],
1787 Qnil);
1788 args[i * 2 + 1] = sep;
1791 else if (CONSP (keys))
1793 /* In effect, this computes
1794 (mapconcat 'single-key-description keys " ")
1795 but we shouldn't use mapconcat because it can do GC. */
1797 len = XFASTINT (Flength (keys));
1798 sep = build_string (" ");
1799 /* This has one extra element at the end that we don't pass to Fconcat. */
1800 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
1802 for (i = 0; i < len; i++)
1804 args[i * 2] = Fsingle_key_description (XCAR (keys), Qnil);
1805 args[i * 2 + 1] = sep;
1806 keys = XCDR (keys);
1809 else
1810 keys = wrong_type_argument (Qarrayp, keys);
1812 return Fconcat (len * 2 - 1, args);
1815 char *
1816 push_key_description (c, p)
1817 register unsigned int c;
1818 register char *p;
1820 unsigned c2;
1822 /* Clear all the meaningless bits above the meta bit. */
1823 c &= meta_modifier | ~ - meta_modifier;
1824 c2 = c & ~(alt_modifier | ctrl_modifier | hyper_modifier
1825 | meta_modifier | shift_modifier | super_modifier);
1827 if (c & alt_modifier)
1829 *p++ = 'A';
1830 *p++ = '-';
1831 c -= alt_modifier;
1833 if ((c & ctrl_modifier) != 0
1834 || (c2 < ' ' && c2 != 27 && c2 != '\t' && c2 != Ctl ('M')))
1836 *p++ = 'C';
1837 *p++ = '-';
1838 c &= ~ctrl_modifier;
1840 if (c & hyper_modifier)
1842 *p++ = 'H';
1843 *p++ = '-';
1844 c -= hyper_modifier;
1846 if (c & meta_modifier)
1848 *p++ = 'M';
1849 *p++ = '-';
1850 c -= meta_modifier;
1852 if (c & shift_modifier)
1854 *p++ = 'S';
1855 *p++ = '-';
1856 c -= shift_modifier;
1858 if (c & super_modifier)
1860 *p++ = 's';
1861 *p++ = '-';
1862 c -= super_modifier;
1864 if (c < 040)
1866 if (c == 033)
1868 *p++ = 'E';
1869 *p++ = 'S';
1870 *p++ = 'C';
1872 else if (c == '\t')
1874 *p++ = 'T';
1875 *p++ = 'A';
1876 *p++ = 'B';
1878 else if (c == Ctl ('M'))
1880 *p++ = 'R';
1881 *p++ = 'E';
1882 *p++ = 'T';
1884 else
1886 /* `C-' already added above. */
1887 if (c > 0 && c <= Ctl ('Z'))
1888 *p++ = c + 0140;
1889 else
1890 *p++ = c + 0100;
1893 else if (c == 0177)
1895 *p++ = 'D';
1896 *p++ = 'E';
1897 *p++ = 'L';
1899 else if (c == ' ')
1901 *p++ = 'S';
1902 *p++ = 'P';
1903 *p++ = 'C';
1905 else if (c < 128
1906 || (NILP (current_buffer->enable_multibyte_characters)
1907 && SINGLE_BYTE_CHAR_P (c)))
1908 *p++ = c;
1909 else
1911 if (! NILP (current_buffer->enable_multibyte_characters))
1912 c = unibyte_char_to_multibyte (c);
1914 if (NILP (current_buffer->enable_multibyte_characters)
1915 || SINGLE_BYTE_CHAR_P (c)
1916 || ! char_valid_p (c, 0))
1918 int bit_offset;
1919 *p++ = '\\';
1920 /* The biggest character code uses 19 bits. */
1921 for (bit_offset = 18; bit_offset >= 0; bit_offset -= 3)
1923 if (c >= (1 << bit_offset))
1924 *p++ = ((c & (7 << bit_offset)) >> bit_offset) + '0';
1927 else
1929 p += CHAR_STRING (c, p);
1933 return p;
1936 /* This function cannot GC. */
1938 DEFUN ("single-key-description", Fsingle_key_description,
1939 Ssingle_key_description, 1, 2, 0,
1940 "Return a pretty description of command character KEY.\n\
1941 Control characters turn into C-whatever, etc.\n\
1942 Optional argument NO-ANGLES non-nil means don't put angle brackets\n\
1943 around function keys and event symbols.")
1944 (key, no_angles)
1945 Lisp_Object key, no_angles;
1947 if (CONSP (key) && lucid_event_type_list_p (key))
1948 key = Fevent_convert_list (key);
1950 key = EVENT_HEAD (key);
1952 if (INTEGERP (key)) /* Normal character */
1954 unsigned int charset, c1, c2;
1955 int without_bits = XINT (key) & ~((-1) << CHARACTERBITS);
1957 if (SINGLE_BYTE_CHAR_P (without_bits))
1958 charset = 0;
1959 else
1960 SPLIT_CHAR (without_bits, charset, c1, c2);
1962 if (charset
1963 && CHARSET_DEFINED_P (charset)
1964 && ((c1 >= 0 && c1 < 32)
1965 || (c2 >= 0 && c2 < 32)))
1967 /* Handle a generic character. */
1968 Lisp_Object name;
1969 name = CHARSET_TABLE_INFO (charset, CHARSET_LONG_NAME_IDX);
1970 CHECK_STRING (name, 0);
1971 return concat2 (build_string ("Character set "), name);
1973 else
1975 char tem[KEY_DESCRIPTION_SIZE];
1977 *push_key_description (XUINT (key), tem) = 0;
1978 return build_string (tem);
1981 else if (SYMBOLP (key)) /* Function key or event-symbol */
1983 if (NILP (no_angles))
1985 char *buffer
1986 = (char *) alloca (STRING_BYTES (XSYMBOL (key)->name) + 5);
1987 sprintf (buffer, "<%s>", XSYMBOL (key)->name->data);
1988 return build_string (buffer);
1990 else
1991 return Fsymbol_name (key);
1993 else if (STRINGP (key)) /* Buffer names in the menubar. */
1994 return Fcopy_sequence (key);
1995 else
1996 error ("KEY must be an integer, cons, symbol, or string");
1997 return Qnil;
2000 char *
2001 push_text_char_description (c, p)
2002 register unsigned int c;
2003 register char *p;
2005 if (c >= 0200)
2007 *p++ = 'M';
2008 *p++ = '-';
2009 c -= 0200;
2011 if (c < 040)
2013 *p++ = '^';
2014 *p++ = c + 64; /* 'A' - 1 */
2016 else if (c == 0177)
2018 *p++ = '^';
2019 *p++ = '?';
2021 else
2022 *p++ = c;
2023 return p;
2026 /* This function cannot GC. */
2028 DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0,
2029 "Return a pretty description of file-character CHARACTER.\n\
2030 Control characters turn into \"^char\", etc.")
2031 (character)
2032 Lisp_Object character;
2034 /* Currently MAX_MULTIBYTE_LENGTH is 4 (< 6). */
2035 unsigned char str[6];
2036 int c;
2038 CHECK_NUMBER (character, 0);
2040 c = XINT (character);
2041 if (!SINGLE_BYTE_CHAR_P (c))
2043 int len = CHAR_STRING (c, str);
2045 return make_multibyte_string (str, 1, len);
2048 *push_text_char_description (c & 0377, str) = 0;
2050 return build_string (str);
2053 /* Return non-zero if SEQ contains only ASCII characters, perhaps with
2054 a meta bit. */
2055 static int
2056 ascii_sequence_p (seq)
2057 Lisp_Object seq;
2059 int i;
2060 int len = XINT (Flength (seq));
2062 for (i = 0; i < len; i++)
2064 Lisp_Object ii, elt;
2066 XSETFASTINT (ii, i);
2067 elt = Faref (seq, ii);
2069 if (!INTEGERP (elt)
2070 || (XUINT (elt) & ~CHAR_META) >= 0x80)
2071 return 0;
2074 return 1;
2078 /* where-is - finding a command in a set of keymaps. */
2080 static Lisp_Object where_is_internal_1 ();
2081 static void where_is_internal_2 ();
2083 /* This function can GC if Flookup_key autoloads any keymaps. */
2085 DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 4, 0,
2086 "Return list of keys that invoke DEFINITION.\n\
2087 If KEYMAP is non-nil, search only KEYMAP and the global keymap.\n\
2088 If KEYMAP is nil, search all the currently active keymaps.\n\
2090 If optional 3rd arg FIRSTONLY is non-nil, return the first key sequence found,\n\
2091 rather than a list of all possible key sequences.\n\
2092 If FIRSTONLY is the symbol `non-ascii', return the first binding found,\n\
2093 no matter what it is.\n\
2094 If FIRSTONLY has another non-nil value, prefer sequences of ASCII characters,\n\
2095 and entirely reject menu bindings.\n\
2097 If optional 4th arg NOINDIRECT is non-nil, don't follow indirections\n\
2098 to other keymaps or slots. This makes it possible to search for an\n\
2099 indirect definition itself.")
2100 (definition, xkeymap, firstonly, noindirect)
2101 Lisp_Object definition, xkeymap;
2102 Lisp_Object firstonly, noindirect;
2104 Lisp_Object maps;
2105 Lisp_Object found, sequences;
2106 Lisp_Object keymap1;
2107 int keymap_specified = !NILP (xkeymap);
2108 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
2109 /* 1 means ignore all menu bindings entirely. */
2110 int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii);
2112 /* Find keymaps accessible from `keymap' or the current
2113 context. But don't muck with the value of `keymap',
2114 because `where_is_internal_1' uses it to check for
2115 shadowed bindings. */
2116 keymap1 = xkeymap;
2117 if (! keymap_specified)
2118 keymap1 = get_local_map (PT, current_buffer, keymap);
2120 if (!NILP (keymap1))
2121 maps = nconc2 (Faccessible_keymaps (get_keymap (keymap1), Qnil),
2122 Faccessible_keymaps (get_keymap (current_global_map),
2123 Qnil));
2124 else
2126 keymap1 = xkeymap;
2127 if (! keymap_specified)
2128 keymap1 = get_local_map (PT, current_buffer, local_map);
2130 if (!NILP (keymap1))
2131 maps = nconc2 (Faccessible_keymaps (get_keymap (keymap1), Qnil),
2132 Faccessible_keymaps (get_keymap (current_global_map),
2133 Qnil));
2134 else
2135 maps = Faccessible_keymaps (get_keymap (current_global_map), Qnil);
2138 /* Put the minor mode keymaps on the front. */
2139 if (! keymap_specified)
2141 Lisp_Object minors;
2142 minors = Fnreverse (Fcurrent_minor_mode_maps ());
2143 while (!NILP (minors))
2145 maps = nconc2 (Faccessible_keymaps (get_keymap (XCAR (minors)),
2146 Qnil),
2147 maps);
2148 minors = XCDR (minors);
2152 GCPRO5 (definition, xkeymap, maps, found, sequences);
2153 found = Qnil;
2154 sequences = Qnil;
2156 for (; !NILP (maps); maps = Fcdr (maps))
2158 /* Key sequence to reach map, and the map that it reaches */
2159 register Lisp_Object this, map;
2161 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
2162 [M-CHAR] sequences, check if last character of the sequence
2163 is the meta-prefix char. */
2164 Lisp_Object last;
2165 int last_is_meta;
2167 this = Fcar (Fcar (maps));
2168 map = Fcdr (Fcar (maps));
2169 last = make_number (XINT (Flength (this)) - 1);
2170 last_is_meta = (XINT (last) >= 0
2171 && EQ (Faref (this, last), meta_prefix_char));
2173 QUIT;
2175 while (CONSP (map))
2177 /* Because the code we want to run on each binding is rather
2178 large, we don't want to have two separate loop bodies for
2179 sparse keymap bindings and tables; we want to iterate one
2180 loop body over both keymap and vector bindings.
2182 For this reason, if Fcar (map) is a vector, we don't
2183 advance map to the next element until i indicates that we
2184 have finished off the vector. */
2185 Lisp_Object elt, key, binding;
2186 elt = XCAR (map);
2187 map = XCDR (map);
2189 sequences = Qnil;
2191 QUIT;
2193 /* Set key and binding to the current key and binding, and
2194 advance map and i to the next binding. */
2195 if (VECTORP (elt))
2197 Lisp_Object sequence;
2198 int i;
2199 /* In a vector, look at each element. */
2200 for (i = 0; i < XVECTOR (elt)->size; i++)
2202 binding = XVECTOR (elt)->contents[i];
2203 XSETFASTINT (key, i);
2204 sequence = where_is_internal_1 (binding, key, definition,
2205 noindirect, xkeymap, this,
2206 last, nomenus, last_is_meta);
2207 if (!NILP (sequence))
2208 sequences = Fcons (sequence, sequences);
2211 else if (CHAR_TABLE_P (elt))
2213 Lisp_Object indices[3];
2214 Lisp_Object args;
2216 args = Fcons (Fcons (Fcons (definition, noindirect),
2217 Fcons (xkeymap, Qnil)),
2218 Fcons (Fcons (this, last),
2219 Fcons (make_number (nomenus),
2220 make_number (last_is_meta))));
2221 map_char_table (where_is_internal_2, Qnil, elt, args,
2222 0, indices);
2223 sequences = XCDR (XCDR (XCAR (args)));
2225 else if (CONSP (elt))
2227 Lisp_Object sequence;
2229 key = XCAR (elt);
2230 binding = XCDR (elt);
2232 sequence = where_is_internal_1 (binding, key, definition,
2233 noindirect, xkeymap, this,
2234 last, nomenus, last_is_meta);
2235 if (!NILP (sequence))
2236 sequences = Fcons (sequence, sequences);
2240 for (; ! NILP (sequences); sequences = XCDR (sequences))
2242 Lisp_Object sequence;
2244 sequence = XCAR (sequences);
2246 /* It is a true unshadowed match. Record it, unless it's already
2247 been seen (as could happen when inheriting keymaps). */
2248 if (NILP (Fmember (sequence, found)))
2249 found = Fcons (sequence, found);
2251 /* If firstonly is Qnon_ascii, then we can return the first
2252 binding we find. If firstonly is not Qnon_ascii but not
2253 nil, then we should return the first ascii-only binding
2254 we find. */
2255 if (EQ (firstonly, Qnon_ascii))
2256 RETURN_UNGCPRO (sequence);
2257 else if (! NILP (firstonly) && ascii_sequence_p (sequence))
2258 RETURN_UNGCPRO (sequence);
2263 UNGCPRO;
2265 found = Fnreverse (found);
2267 /* firstonly may have been t, but we may have gone all the way through
2268 the keymaps without finding an all-ASCII key sequence. So just
2269 return the best we could find. */
2270 if (! NILP (firstonly))
2271 return Fcar (found);
2273 return found;
2276 /* This is the function that Fwhere_is_internal calls using map_char_table.
2277 ARGS has the form
2278 (((DEFINITION . NOINDIRECT) . (KEYMAP . RESULT))
2280 ((THIS . LAST) . (NOMENUS . LAST_IS_META)))
2281 Since map_char_table doesn't really use the return value from this function,
2282 we the result append to RESULT, the slot in ARGS.
2284 This function can GC because it calls where_is_internal_1 which can
2285 GC. */
2287 static void
2288 where_is_internal_2 (args, key, binding)
2289 Lisp_Object args, key, binding;
2291 Lisp_Object definition, noindirect, keymap, this, last;
2292 Lisp_Object result, sequence;
2293 int nomenus, last_is_meta;
2294 struct gcpro gcpro1, gcpro2, gcpro3;
2296 GCPRO3 (args, key, binding);
2297 result = XCDR (XCDR (XCAR (args)));
2298 definition = XCAR (XCAR (XCAR (args)));
2299 noindirect = XCDR (XCAR (XCAR (args)));
2300 keymap = XCAR (XCDR (XCAR (args)));
2301 this = XCAR (XCAR (XCDR (args)));
2302 last = XCDR (XCAR (XCDR (args)));
2303 nomenus = XFASTINT (XCAR (XCDR (XCDR (args))));
2304 last_is_meta = XFASTINT (XCDR (XCDR (XCDR (args))));
2306 sequence = where_is_internal_1 (binding, key, definition, noindirect, keymap,
2307 this, last, nomenus, last_is_meta);
2309 if (!NILP (sequence))
2310 XCDR (XCDR (XCAR (args))) = Fcons (sequence, result);
2312 UNGCPRO;
2316 /* This function can GC.because Flookup_key calls get_keymap_1 with
2317 non-zero argument AUTOLOAD. */
2319 static Lisp_Object
2320 where_is_internal_1 (binding, key, definition, noindirect, keymap, this, last,
2321 nomenus, last_is_meta)
2322 Lisp_Object binding, key, definition, noindirect, keymap, this, last;
2323 int nomenus, last_is_meta;
2325 Lisp_Object sequence;
2326 int keymap_specified = !NILP (keymap);
2327 struct gcpro gcpro1, gcpro2;
2329 /* Search through indirections unless that's not wanted. */
2330 if (NILP (noindirect))
2332 if (nomenus)
2334 while (1)
2336 Lisp_Object map, tem;
2337 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
2338 map = get_keymap_1 (Fcar_safe (definition), 0, 0);
2339 tem = Fkeymapp (map);
2340 if (!NILP (tem))
2341 definition = access_keymap (map, Fcdr (definition), 0, 0);
2342 else
2343 break;
2345 /* If the contents are (menu-item ...) or (STRING ...), reject. */
2346 if (CONSP (definition)
2347 && (EQ (XCAR (definition),Qmenu_item)
2348 || STRINGP (XCAR (definition))))
2349 return Qnil;
2351 else
2352 binding = get_keyelt (binding, 0);
2355 /* End this iteration if this element does not match
2356 the target. */
2358 if (CONSP (definition))
2360 Lisp_Object tem;
2361 tem = Fequal (binding, definition);
2362 if (NILP (tem))
2363 return Qnil;
2365 else
2366 if (!EQ (binding, definition))
2367 return Qnil;
2369 /* We have found a match.
2370 Construct the key sequence where we found it. */
2371 if (INTEGERP (key) && last_is_meta)
2373 sequence = Fcopy_sequence (this);
2374 Faset (sequence, last, make_number (XINT (key) | meta_modifier));
2376 else
2377 sequence = append_key (this, key);
2379 /* Verify that this key binding is not shadowed by another
2380 binding for the same key, before we say it exists.
2382 Mechanism: look for local definition of this key and if
2383 it is defined and does not match what we found then
2384 ignore this key.
2386 Either nil or number as value from Flookup_key
2387 means undefined. */
2388 GCPRO2 (sequence, binding);
2389 if (keymap_specified)
2391 binding = Flookup_key (keymap, sequence, Qnil);
2392 if (!NILP (binding) && !INTEGERP (binding))
2394 if (CONSP (definition))
2396 Lisp_Object tem;
2397 tem = Fequal (binding, definition);
2398 if (NILP (tem))
2399 RETURN_UNGCPRO (Qnil);
2401 else
2402 if (!EQ (binding, definition))
2403 RETURN_UNGCPRO (Qnil);
2406 else
2408 binding = Fkey_binding (sequence, Qnil);
2409 if (!EQ (binding, definition))
2410 RETURN_UNGCPRO (Qnil);
2413 RETURN_UNGCPRO (sequence);
2416 /* describe-bindings - summarizing all the bindings in a set of keymaps. */
2418 DEFUN ("describe-bindings-internal", Fdescribe_bindings_internal, Sdescribe_bindings_internal, 0, 2, "",
2419 "Show a list of all defined keys, and their definitions.\n\
2420 We put that list in a buffer, and display the buffer.\n\
2422 The optional argument MENUS, if non-nil, says to mention menu bindings.\n\
2423 \(Ordinarily these are omitted from the output.)\n\
2424 The optional argument PREFIX, if non-nil, should be a key sequence;\n\
2425 then we display only bindings that start with that prefix.")
2426 (menus, prefix)
2427 Lisp_Object menus, prefix;
2429 register Lisp_Object thisbuf;
2430 XSETBUFFER (thisbuf, current_buffer);
2431 internal_with_output_to_temp_buffer ("*Help*",
2432 describe_buffer_bindings,
2433 list3 (thisbuf, prefix, menus));
2434 return Qnil;
2437 /* ARG is (BUFFER PREFIX MENU-FLAG). */
2439 static Lisp_Object
2440 describe_buffer_bindings (arg)
2441 Lisp_Object arg;
2443 Lisp_Object descbuf, prefix, shadow;
2444 int nomenu;
2445 register Lisp_Object start1;
2446 struct gcpro gcpro1;
2448 char *alternate_heading
2449 = "\
2450 Keyboard translations:\n\n\
2451 You type Translation\n\
2452 -------- -----------\n";
2454 descbuf = XCAR (arg);
2455 arg = XCDR (arg);
2456 prefix = XCAR (arg);
2457 arg = XCDR (arg);
2458 nomenu = NILP (XCAR (arg));
2460 shadow = Qnil;
2461 GCPRO1 (shadow);
2463 Fset_buffer (Vstandard_output);
2465 /* Report on alternates for keys. */
2466 if (STRINGP (Vkeyboard_translate_table) && !NILP (prefix))
2468 int c;
2469 unsigned char *translate = XSTRING (Vkeyboard_translate_table)->data;
2470 int translate_len = XSTRING (Vkeyboard_translate_table)->size;
2472 for (c = 0; c < translate_len; c++)
2473 if (translate[c] != c)
2475 char buf[KEY_DESCRIPTION_SIZE];
2476 char *bufend;
2478 if (alternate_heading)
2480 insert_string (alternate_heading);
2481 alternate_heading = 0;
2484 bufend = push_key_description (translate[c], buf);
2485 insert (buf, bufend - buf);
2486 Findent_to (make_number (16), make_number (1));
2487 bufend = push_key_description (c, buf);
2488 insert (buf, bufend - buf);
2490 insert ("\n", 1);
2493 insert ("\n", 1);
2496 if (!NILP (Vkey_translation_map))
2497 describe_map_tree (Vkey_translation_map, 0, Qnil, prefix,
2498 "Key translations", nomenu, 1, 0);
2501 int i, nmaps;
2502 Lisp_Object *modes, *maps;
2504 /* Temporarily switch to descbuf, so that we can get that buffer's
2505 minor modes correctly. */
2506 Fset_buffer (descbuf);
2508 if (!NILP (current_kboard->Voverriding_terminal_local_map)
2509 || !NILP (Voverriding_local_map))
2510 nmaps = 0;
2511 else
2512 nmaps = current_minor_maps (&modes, &maps);
2513 Fset_buffer (Vstandard_output);
2515 /* Print the minor mode maps. */
2516 for (i = 0; i < nmaps; i++)
2518 /* The title for a minor mode keymap
2519 is constructed at run time.
2520 We let describe_map_tree do the actual insertion
2521 because it takes care of other features when doing so. */
2522 char *title, *p;
2524 if (!SYMBOLP (modes[i]))
2525 abort();
2527 p = title = (char *) alloca (42 + XSYMBOL (modes[i])->name->size);
2528 *p++ = '\f';
2529 *p++ = '\n';
2530 *p++ = '`';
2531 bcopy (XSYMBOL (modes[i])->name->data, p,
2532 XSYMBOL (modes[i])->name->size);
2533 p += XSYMBOL (modes[i])->name->size;
2534 *p++ = '\'';
2535 bcopy (" Minor Mode Bindings", p, sizeof (" Minor Mode Bindings") - 1);
2536 p += sizeof (" Minor Mode Bindings") - 1;
2537 *p = 0;
2539 describe_map_tree (maps[i], 1, shadow, prefix, title, nomenu, 0, 0);
2540 shadow = Fcons (maps[i], shadow);
2544 /* Print the (major mode) local map. */
2545 if (!NILP (current_kboard->Voverriding_terminal_local_map))
2546 start1 = current_kboard->Voverriding_terminal_local_map;
2547 else if (!NILP (Voverriding_local_map))
2548 start1 = Voverriding_local_map;
2549 else
2550 start1 = XBUFFER (descbuf)->keymap;
2552 if (!NILP (start1))
2554 describe_map_tree (start1, 1, shadow, prefix,
2555 "\f\nMajor Mode Bindings", nomenu, 0, 0);
2556 shadow = Fcons (start1, shadow);
2559 describe_map_tree (current_global_map, 1, shadow, prefix,
2560 "\f\nGlobal Bindings", nomenu, 0, 1);
2562 /* Print the function-key-map translations under this prefix. */
2563 if (!NILP (Vfunction_key_map))
2564 describe_map_tree (Vfunction_key_map, 0, Qnil, prefix,
2565 "\f\nFunction key map translations", nomenu, 1, 0);
2567 call0 (intern ("help-mode"));
2568 Fset_buffer (descbuf);
2569 UNGCPRO;
2570 return Qnil;
2573 /* Insert a description of the key bindings in STARTMAP,
2574 followed by those of all maps reachable through STARTMAP.
2575 If PARTIAL is nonzero, omit certain "uninteresting" commands
2576 (such as `undefined').
2577 If SHADOW is non-nil, it is a list of maps;
2578 don't mention keys which would be shadowed by any of them.
2579 PREFIX, if non-nil, says mention only keys that start with PREFIX.
2580 TITLE, if not 0, is a string to insert at the beginning.
2581 TITLE should not end with a colon or a newline; we supply that.
2582 If NOMENU is not 0, then omit menu-bar commands.
2584 If TRANSL is nonzero, the definitions are actually key translations
2585 so print strings and vectors differently.
2587 If ALWAYS_TITLE is nonzero, print the title even if there are no maps
2588 to look through. */
2590 void
2591 describe_map_tree (startmap, partial, shadow, prefix, title, nomenu, transl,
2592 always_title)
2593 Lisp_Object startmap, shadow, prefix;
2594 int partial;
2595 char *title;
2596 int nomenu;
2597 int transl;
2598 int always_title;
2600 Lisp_Object maps, orig_maps, seen, sub_shadows;
2601 struct gcpro gcpro1, gcpro2, gcpro3;
2602 int something = 0;
2603 char *key_heading
2604 = "\
2605 key binding\n\
2606 --- -------\n";
2608 orig_maps = maps = Faccessible_keymaps (startmap, prefix);
2609 seen = Qnil;
2610 sub_shadows = Qnil;
2611 GCPRO3 (maps, seen, sub_shadows);
2613 if (nomenu)
2615 Lisp_Object list;
2617 /* Delete from MAPS each element that is for the menu bar. */
2618 for (list = maps; !NILP (list); list = XCDR (list))
2620 Lisp_Object elt, prefix, tem;
2622 elt = Fcar (list);
2623 prefix = Fcar (elt);
2624 if (XVECTOR (prefix)->size >= 1)
2626 tem = Faref (prefix, make_number (0));
2627 if (EQ (tem, Qmenu_bar))
2628 maps = Fdelq (elt, maps);
2633 if (!NILP (maps) || always_title)
2635 if (title)
2637 insert_string (title);
2638 if (!NILP (prefix))
2640 insert_string (" Starting With ");
2641 insert1 (Fkey_description (prefix));
2643 insert_string (":\n");
2645 insert_string (key_heading);
2646 something = 1;
2649 for (; !NILP (maps); maps = Fcdr (maps))
2651 register Lisp_Object elt, prefix, tail;
2653 elt = Fcar (maps);
2654 prefix = Fcar (elt);
2656 sub_shadows = Qnil;
2658 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2660 Lisp_Object shmap;
2662 shmap = XCAR (tail);
2664 /* If the sequence by which we reach this keymap is zero-length,
2665 then the shadow map for this keymap is just SHADOW. */
2666 if ((STRINGP (prefix) && XSTRING (prefix)->size == 0)
2667 || (VECTORP (prefix) && XVECTOR (prefix)->size == 0))
2669 /* If the sequence by which we reach this keymap actually has
2670 some elements, then the sequence's definition in SHADOW is
2671 what we should use. */
2672 else
2674 shmap = Flookup_key (shmap, Fcar (elt), Qt);
2675 if (INTEGERP (shmap))
2676 shmap = Qnil;
2679 /* If shmap is not nil and not a keymap,
2680 it completely shadows this map, so don't
2681 describe this map at all. */
2682 if (!NILP (shmap) && NILP (Fkeymapp (shmap)))
2683 goto skip;
2685 if (!NILP (shmap))
2686 sub_shadows = Fcons (shmap, sub_shadows);
2689 /* Maps we have already listed in this loop shadow this map. */
2690 for (tail = orig_maps; ! EQ (tail, maps); tail = XCDR (tail))
2692 Lisp_Object tem;
2693 tem = Fequal (Fcar (XCAR (tail)), prefix);
2694 if (! NILP (tem))
2695 sub_shadows = Fcons (XCDR (XCAR (tail)), sub_shadows);
2698 describe_map (Fcdr (elt), prefix,
2699 transl ? describe_translation : describe_command,
2700 partial, sub_shadows, &seen, nomenu);
2702 skip: ;
2705 if (something)
2706 insert_string ("\n");
2708 UNGCPRO;
2711 static int previous_description_column;
2713 static void
2714 describe_command (definition)
2715 Lisp_Object definition;
2717 register Lisp_Object tem1;
2718 int column = current_column ();
2719 int description_column;
2721 /* If column 16 is no good, go to col 32;
2722 but don't push beyond that--go to next line instead. */
2723 if (column > 30)
2725 insert_char ('\n');
2726 description_column = 32;
2728 else if (column > 14 || (column > 10 && previous_description_column == 32))
2729 description_column = 32;
2730 else
2731 description_column = 16;
2733 Findent_to (make_number (description_column), make_number (1));
2734 previous_description_column = description_column;
2736 if (SYMBOLP (definition))
2738 XSETSTRING (tem1, XSYMBOL (definition)->name);
2739 insert1 (tem1);
2740 insert_string ("\n");
2742 else if (STRINGP (definition) || VECTORP (definition))
2743 insert_string ("Keyboard Macro\n");
2744 else
2746 tem1 = Fkeymapp (definition);
2747 if (!NILP (tem1))
2748 insert_string ("Prefix Command\n");
2749 else
2750 insert_string ("??\n");
2754 static void
2755 describe_translation (definition)
2756 Lisp_Object definition;
2758 register Lisp_Object tem1;
2760 Findent_to (make_number (16), make_number (1));
2762 if (SYMBOLP (definition))
2764 XSETSTRING (tem1, XSYMBOL (definition)->name);
2765 insert1 (tem1);
2766 insert_string ("\n");
2768 else if (STRINGP (definition) || VECTORP (definition))
2770 insert1 (Fkey_description (definition));
2771 insert_string ("\n");
2773 else
2775 tem1 = Fkeymapp (definition);
2776 if (!NILP (tem1))
2777 insert_string ("Prefix Command\n");
2778 else
2779 insert_string ("??\n");
2783 /* Like Flookup_key, but uses a list of keymaps SHADOW instead of a single map.
2784 Returns the first non-nil binding found in any of those maps. */
2786 static Lisp_Object
2787 shadow_lookup (shadow, key, flag)
2788 Lisp_Object shadow, key, flag;
2790 Lisp_Object tail, value;
2792 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2794 value = Flookup_key (XCAR (tail), key, flag);
2795 if (!NILP (value))
2796 return value;
2798 return Qnil;
2801 /* Describe the contents of map MAP, assuming that this map itself is
2802 reached by the sequence of prefix keys KEYS (a string or vector).
2803 PARTIAL, SHADOW, NOMENU are as in `describe_map_tree' above. */
2805 static void
2806 describe_map (map, keys, elt_describer, partial, shadow, seen, nomenu)
2807 register Lisp_Object map;
2808 Lisp_Object keys;
2809 void (*elt_describer) P_ ((Lisp_Object));
2810 int partial;
2811 Lisp_Object shadow;
2812 Lisp_Object *seen;
2813 int nomenu;
2815 Lisp_Object elt_prefix;
2816 Lisp_Object tail, definition, event;
2817 Lisp_Object tem;
2818 Lisp_Object suppress;
2819 Lisp_Object kludge;
2820 int first = 1;
2821 struct gcpro gcpro1, gcpro2, gcpro3;
2823 suppress = Qnil;
2825 if (!NILP (keys) && XFASTINT (Flength (keys)) > 0)
2827 /* Call Fkey_description first, to avoid GC bug for the other string. */
2828 tem = Fkey_description (keys);
2829 elt_prefix = concat2 (tem, build_string (" "));
2831 else
2832 elt_prefix = Qnil;
2834 if (partial)
2835 suppress = intern ("suppress-keymap");
2837 /* This vector gets used to present single keys to Flookup_key. Since
2838 that is done once per keymap element, we don't want to cons up a
2839 fresh vector every time. */
2840 kludge = Fmake_vector (make_number (1), Qnil);
2841 definition = Qnil;
2843 GCPRO3 (elt_prefix, definition, kludge);
2845 for (tail = map; CONSP (tail); tail = XCDR (tail))
2847 QUIT;
2849 if (VECTORP (XCAR (tail))
2850 || CHAR_TABLE_P (XCAR (tail)))
2851 describe_vector (XCAR (tail),
2852 elt_prefix, elt_describer, partial, shadow, map,
2853 (int *)0, 0);
2854 else if (CONSP (XCAR (tail)))
2856 event = XCAR (XCAR (tail));
2858 /* Ignore bindings whose "keys" are not really valid events.
2859 (We get these in the frames and buffers menu.) */
2860 if (! (SYMBOLP (event) || INTEGERP (event)))
2861 continue;
2863 if (nomenu && EQ (event, Qmenu_bar))
2864 continue;
2866 definition = get_keyelt (XCDR (XCAR (tail)), 0);
2868 /* Don't show undefined commands or suppressed commands. */
2869 if (NILP (definition)) continue;
2870 if (SYMBOLP (definition) && partial)
2872 tem = Fget (definition, suppress);
2873 if (!NILP (tem))
2874 continue;
2877 /* Don't show a command that isn't really visible
2878 because a local definition of the same key shadows it. */
2880 XVECTOR (kludge)->contents[0] = event;
2881 if (!NILP (shadow))
2883 tem = shadow_lookup (shadow, kludge, Qt);
2884 if (!NILP (tem)) continue;
2887 tem = Flookup_key (map, kludge, Qt);
2888 if (! EQ (tem, definition)) continue;
2890 if (first)
2892 previous_description_column = 0;
2893 insert ("\n", 1);
2894 first = 0;
2897 if (!NILP (elt_prefix))
2898 insert1 (elt_prefix);
2900 /* THIS gets the string to describe the character EVENT. */
2901 insert1 (Fsingle_key_description (event, Qnil));
2903 /* Print a description of the definition of this character.
2904 elt_describer will take care of spacing out far enough
2905 for alignment purposes. */
2906 (*elt_describer) (definition);
2908 else if (EQ (XCAR (tail), Qkeymap))
2910 /* The same keymap might be in the structure twice, if we're
2911 using an inherited keymap. So skip anything we've already
2912 encountered. */
2913 tem = Fassq (tail, *seen);
2914 if (CONSP (tem) && !NILP (Fequal (XCAR (tem), keys)))
2915 break;
2916 *seen = Fcons (Fcons (tail, keys), *seen);
2920 UNGCPRO;
2923 static void
2924 describe_vector_princ (elt)
2925 Lisp_Object elt;
2927 Findent_to (make_number (16), make_number (1));
2928 Fprinc (elt, Qnil);
2929 Fterpri (Qnil);
2932 DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 1, 0,
2933 "Insert a description of contents of VECTOR.\n\
2934 This is text showing the elements of vector matched against indices.")
2935 (vector)
2936 Lisp_Object vector;
2938 int count = specpdl_ptr - specpdl;
2940 specbind (Qstandard_output, Fcurrent_buffer ());
2941 CHECK_VECTOR_OR_CHAR_TABLE (vector, 0);
2942 describe_vector (vector, Qnil, describe_vector_princ, 0,
2943 Qnil, Qnil, (int *)0, 0);
2945 return unbind_to (count, Qnil);
2948 /* Insert in the current buffer a description of the contents of VECTOR.
2949 We call ELT_DESCRIBER to insert the description of one value found
2950 in VECTOR.
2952 ELT_PREFIX describes what "comes before" the keys or indices defined
2953 by this vector. This is a human-readable string whose size
2954 is not necessarily related to the situation.
2956 If the vector is in a keymap, ELT_PREFIX is a prefix key which
2957 leads to this keymap.
2959 If the vector is a chartable, ELT_PREFIX is the vector
2960 of bytes that lead to the character set or portion of a character
2961 set described by this chartable.
2963 If PARTIAL is nonzero, it means do not mention suppressed commands
2964 (that assumes the vector is in a keymap).
2966 SHADOW is a list of keymaps that shadow this map.
2967 If it is non-nil, then we look up the key in those maps
2968 and we don't mention it now if it is defined by any of them.
2970 ENTIRE_MAP is the keymap in which this vector appears.
2971 If the definition in effect in the whole map does not match
2972 the one in this vector, we ignore this one.
2974 When describing a sub-char-table, INDICES is a list of
2975 indices at higher levels in this char-table,
2976 and CHAR_TABLE_DEPTH says how many levels down we have gone. */
2978 void
2979 describe_vector (vector, elt_prefix, elt_describer,
2980 partial, shadow, entire_map,
2981 indices, char_table_depth)
2982 register Lisp_Object vector;
2983 Lisp_Object elt_prefix;
2984 void (*elt_describer) P_ ((Lisp_Object));
2985 int partial;
2986 Lisp_Object shadow;
2987 Lisp_Object entire_map;
2988 int *indices;
2989 int char_table_depth;
2991 Lisp_Object definition;
2992 Lisp_Object tem2;
2993 register int i;
2994 Lisp_Object suppress;
2995 Lisp_Object kludge;
2996 int first = 1;
2997 struct gcpro gcpro1, gcpro2, gcpro3;
2998 /* Range of elements to be handled. */
2999 int from, to;
3000 /* A flag to tell if a leaf in this level of char-table is not a
3001 generic character (i.e. a complete multibyte character). */
3002 int complete_char;
3003 int character;
3004 int starting_i;
3006 suppress = Qnil;
3008 if (indices == 0)
3009 indices = (int *) alloca (3 * sizeof (int));
3011 definition = Qnil;
3013 /* This vector gets used to present single keys to Flookup_key. Since
3014 that is done once per vector element, we don't want to cons up a
3015 fresh vector every time. */
3016 kludge = Fmake_vector (make_number (1), Qnil);
3017 GCPRO3 (elt_prefix, definition, kludge);
3019 if (partial)
3020 suppress = intern ("suppress-keymap");
3022 if (CHAR_TABLE_P (vector))
3024 if (char_table_depth == 0)
3026 /* VECTOR is a top level char-table. */
3027 complete_char = 1;
3028 from = 0;
3029 to = CHAR_TABLE_ORDINARY_SLOTS;
3031 else
3033 /* VECTOR is a sub char-table. */
3034 if (char_table_depth >= 3)
3035 /* A char-table is never that deep. */
3036 error ("Too deep char table");
3038 complete_char
3039 = (CHARSET_VALID_P (indices[0])
3040 && ((CHARSET_DIMENSION (indices[0]) == 1
3041 && char_table_depth == 1)
3042 || char_table_depth == 2));
3044 /* Meaningful elements are from 32th to 127th. */
3045 from = 32;
3046 to = SUB_CHAR_TABLE_ORDINARY_SLOTS;
3049 else
3051 /* This does the right thing for ordinary vectors. */
3053 complete_char = 1;
3054 from = 0;
3055 to = XVECTOR (vector)->size;
3058 for (i = from; i < to; i++)
3060 QUIT;
3062 if (CHAR_TABLE_P (vector))
3064 if (char_table_depth == 0 && i >= CHAR_TABLE_SINGLE_BYTE_SLOTS)
3065 complete_char = 0;
3067 if (i >= CHAR_TABLE_SINGLE_BYTE_SLOTS
3068 && !CHARSET_DEFINED_P (i - 128))
3069 continue;
3071 definition
3072 = get_keyelt (XCHAR_TABLE (vector)->contents[i], 0);
3074 else
3075 definition = get_keyelt (XVECTOR (vector)->contents[i], 0);
3077 if (NILP (definition)) continue;
3079 /* Don't mention suppressed commands. */
3080 if (SYMBOLP (definition) && partial)
3082 Lisp_Object tem;
3084 tem = Fget (definition, suppress);
3086 if (!NILP (tem)) continue;
3089 /* Set CHARACTER to the character this entry describes, if any.
3090 Also update *INDICES. */
3091 if (CHAR_TABLE_P (vector))
3093 indices[char_table_depth] = i;
3095 if (char_table_depth == 0)
3097 character = i;
3098 indices[0] = i - 128;
3100 else if (complete_char)
3102 character = MAKE_CHAR (indices[0], indices[1], indices[2]);
3104 else
3105 character = 0;
3107 else
3108 character = i;
3110 /* If this binding is shadowed by some other map, ignore it. */
3111 if (!NILP (shadow) && complete_char)
3113 Lisp_Object tem;
3115 XVECTOR (kludge)->contents[0] = make_number (character);
3116 tem = shadow_lookup (shadow, kludge, Qt);
3118 if (!NILP (tem)) continue;
3121 /* Ignore this definition if it is shadowed by an earlier
3122 one in the same keymap. */
3123 if (!NILP (entire_map) && complete_char)
3125 Lisp_Object tem;
3127 XVECTOR (kludge)->contents[0] = make_number (character);
3128 tem = Flookup_key (entire_map, kludge, Qt);
3130 if (! EQ (tem, definition))
3131 continue;
3134 if (first)
3136 if (char_table_depth == 0)
3137 insert ("\n", 1);
3138 first = 0;
3141 /* For a sub char-table, show the depth by indentation.
3142 CHAR_TABLE_DEPTH can be greater than 0 only for a char-table. */
3143 if (char_table_depth > 0)
3144 insert (" ", char_table_depth * 2); /* depth is 1 or 2. */
3146 /* Output the prefix that applies to every entry in this map. */
3147 if (!NILP (elt_prefix))
3148 insert1 (elt_prefix);
3150 /* Insert or describe the character this slot is for,
3151 or a description of what it is for. */
3152 if (SUB_CHAR_TABLE_P (vector))
3154 if (complete_char)
3155 insert_char (character);
3156 else
3158 /* We need an octal representation for this block of
3159 characters. */
3160 char work[16];
3161 sprintf (work, "(row %d)", i);
3162 insert (work, strlen (work));
3165 else if (CHAR_TABLE_P (vector))
3167 if (complete_char)
3168 insert1 (Fsingle_key_description (make_number (character), Qnil));
3169 else
3171 /* Print the information for this character set. */
3172 insert_string ("<");
3173 tem2 = CHARSET_TABLE_INFO (i - 128, CHARSET_SHORT_NAME_IDX);
3174 if (STRINGP (tem2))
3175 insert_from_string (tem2, 0, 0, XSTRING (tem2)->size,
3176 STRING_BYTES (XSTRING (tem2)), 0);
3177 else
3178 insert ("?", 1);
3179 insert (">", 1);
3182 else
3184 insert1 (Fsingle_key_description (make_number (character), Qnil));
3187 /* If we find a sub char-table within a char-table,
3188 scan it recursively; it defines the details for
3189 a character set or a portion of a character set. */
3190 if (CHAR_TABLE_P (vector) && SUB_CHAR_TABLE_P (definition))
3192 insert ("\n", 1);
3193 describe_vector (definition, elt_prefix, elt_describer,
3194 partial, shadow, entire_map,
3195 indices, char_table_depth + 1);
3196 continue;
3199 starting_i = i;
3201 /* Find all consecutive characters or rows that have the same
3202 definition. But, for elements of a top level char table, if
3203 they are for charsets, we had better describe one by one even
3204 if they have the same definition. */
3205 if (CHAR_TABLE_P (vector))
3207 int limit = to;
3209 if (char_table_depth == 0)
3210 limit = CHAR_TABLE_SINGLE_BYTE_SLOTS;
3212 while (i + 1 < limit
3213 && (tem2 = get_keyelt (XCHAR_TABLE (vector)->contents[i + 1], 0),
3214 !NILP (tem2))
3215 && !NILP (Fequal (tem2, definition)))
3216 i++;
3218 else
3219 while (i + 1 < to
3220 && (tem2 = get_keyelt (XVECTOR (vector)->contents[i + 1], 0),
3221 !NILP (tem2))
3222 && !NILP (Fequal (tem2, definition)))
3223 i++;
3226 /* If we have a range of more than one character,
3227 print where the range reaches to. */
3229 if (i != starting_i)
3231 insert (" .. ", 4);
3233 if (!NILP (elt_prefix))
3234 insert1 (elt_prefix);
3236 if (CHAR_TABLE_P (vector))
3238 if (char_table_depth == 0)
3240 insert1 (Fsingle_key_description (make_number (i), Qnil));
3242 else if (complete_char)
3244 indices[char_table_depth] = i;
3245 character = MAKE_CHAR (indices[0], indices[1], indices[2]);
3246 insert_char (character);
3248 else
3250 /* We need an octal representation for this block of
3251 characters. */
3252 char work[16];
3253 sprintf (work, "(row %d)", i);
3254 insert (work, strlen (work));
3257 else
3259 insert1 (Fsingle_key_description (make_number (i), Qnil));
3263 /* Print a description of the definition of this character.
3264 elt_describer will take care of spacing out far enough
3265 for alignment purposes. */
3266 (*elt_describer) (definition);
3269 /* For (sub) char-table, print `defalt' slot at last. */
3270 if (CHAR_TABLE_P (vector) && !NILP (XCHAR_TABLE (vector)->defalt))
3272 insert (" ", char_table_depth * 2);
3273 insert_string ("<<default>>");
3274 (*elt_describer) (XCHAR_TABLE (vector)->defalt);
3277 UNGCPRO;
3280 /* Apropos - finding all symbols whose names match a regexp. */
3281 Lisp_Object apropos_predicate;
3282 Lisp_Object apropos_accumulate;
3284 static void
3285 apropos_accum (symbol, string)
3286 Lisp_Object symbol, string;
3288 register Lisp_Object tem;
3290 tem = Fstring_match (string, Fsymbol_name (symbol), Qnil);
3291 if (!NILP (tem) && !NILP (apropos_predicate))
3292 tem = call1 (apropos_predicate, symbol);
3293 if (!NILP (tem))
3294 apropos_accumulate = Fcons (symbol, apropos_accumulate);
3297 DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0,
3298 "Show all symbols whose names contain match for REGEXP.\n\
3299 If optional 2nd arg PREDICATE is non-nil, (funcall PREDICATE SYMBOL) is done\n\
3300 for each symbol and a symbol is mentioned only if that returns non-nil.\n\
3301 Return list of symbols found.")
3302 (regexp, predicate)
3303 Lisp_Object regexp, predicate;
3305 struct gcpro gcpro1, gcpro2;
3306 CHECK_STRING (regexp, 0);
3307 apropos_predicate = predicate;
3308 GCPRO2 (apropos_predicate, apropos_accumulate);
3309 apropos_accumulate = Qnil;
3310 map_obarray (Vobarray, apropos_accum, regexp);
3311 apropos_accumulate = Fsort (apropos_accumulate, Qstring_lessp);
3312 UNGCPRO;
3313 return apropos_accumulate;
3316 void
3317 syms_of_keymap ()
3319 Qkeymap = intern ("keymap");
3320 staticpro (&Qkeymap);
3322 /* Now we are ready to set up this property, so we can
3323 create char tables. */
3324 Fput (Qkeymap, Qchar_table_extra_slots, make_number (0));
3326 /* Initialize the keymaps standardly used.
3327 Each one is the value of a Lisp variable, and is also
3328 pointed to by a C variable */
3330 global_map = Fmake_keymap (Qnil);
3331 Fset (intern ("global-map"), global_map);
3333 current_global_map = global_map;
3334 staticpro (&global_map);
3335 staticpro (&current_global_map);
3337 meta_map = Fmake_keymap (Qnil);
3338 Fset (intern ("esc-map"), meta_map);
3339 Ffset (intern ("ESC-prefix"), meta_map);
3341 control_x_map = Fmake_keymap (Qnil);
3342 Fset (intern ("ctl-x-map"), control_x_map);
3343 Ffset (intern ("Control-X-prefix"), control_x_map);
3345 DEFVAR_LISP ("define-key-rebound-commands", &Vdefine_key_rebound_commands,
3346 "List of commands given new key bindings recently.\n\
3347 This is used for internal purposes during Emacs startup;\n\
3348 don't alter it yourself.");
3349 Vdefine_key_rebound_commands = Qt;
3351 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map,
3352 "Default keymap to use when reading from the minibuffer.");
3353 Vminibuffer_local_map = Fmake_sparse_keymap (Qnil);
3355 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map,
3356 "Local keymap for the minibuffer when spaces are not allowed.");
3357 Vminibuffer_local_ns_map = Fmake_sparse_keymap (Qnil);
3359 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map,
3360 "Local keymap for minibuffer input with completion.");
3361 Vminibuffer_local_completion_map = Fmake_sparse_keymap (Qnil);
3363 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map,
3364 "Local keymap for minibuffer input with completion, for exact match.");
3365 Vminibuffer_local_must_match_map = Fmake_sparse_keymap (Qnil);
3367 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist,
3368 "Alist of keymaps to use for minor modes.\n\
3369 Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read\n\
3370 key sequences and look up bindings iff VARIABLE's value is non-nil.\n\
3371 If two active keymaps bind the same key, the keymap appearing earlier\n\
3372 in the list takes precedence.");
3373 Vminor_mode_map_alist = Qnil;
3375 DEFVAR_LISP ("minor-mode-overriding-map-alist", &Vminor_mode_overriding_map_alist,
3376 "Alist of keymaps to use for minor modes, in current major mode.\n\
3377 This variable is a alist just like `minor-mode-map-alist', and it is\n\
3378 used the same way (and before `minor-mode-map-alist'); however,\n\
3379 it is provided for major modes to bind locally.");
3380 Vminor_mode_overriding_map_alist = Qnil;
3382 DEFVAR_LISP ("function-key-map", &Vfunction_key_map,
3383 "Keymap mapping ASCII function key sequences onto their preferred forms.\n\
3384 This allows Emacs to recognize function keys sent from ASCII\n\
3385 terminals at any point in a key sequence.\n\
3387 The `read-key-sequence' function replaces any subsequence bound by\n\
3388 `function-key-map' with its binding. More precisely, when the active\n\
3389 keymaps have no binding for the current key sequence but\n\
3390 `function-key-map' binds a suffix of the sequence to a vector or string,\n\
3391 `read-key-sequence' replaces the matching suffix with its binding, and\n\
3392 continues with the new sequence.\n\
3394 The events that come from bindings in `function-key-map' are not\n\
3395 themselves looked up in `function-key-map'.\n\
3397 For example, suppose `function-key-map' binds `ESC O P' to [f1].\n\
3398 Typing `ESC O P' to `read-key-sequence' would return [f1]. Typing\n\
3399 `C-x ESC O P' would return [?\\C-x f1]. If [f1] were a prefix\n\
3400 key, typing `ESC O P x' would return [f1 x].");
3401 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
3403 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
3404 "Keymap of key translations that can override keymaps.\n\
3405 This keymap works like `function-key-map', but comes after that,\n\
3406 and applies even for keys that have ordinary bindings.");
3407 Vkey_translation_map = Qnil;
3409 Qsingle_key_description = intern ("single-key-description");
3410 staticpro (&Qsingle_key_description);
3412 Qkey_description = intern ("key-description");
3413 staticpro (&Qkey_description);
3415 Qkeymapp = intern ("keymapp");
3416 staticpro (&Qkeymapp);
3418 Qnon_ascii = intern ("non-ascii");
3419 staticpro (&Qnon_ascii);
3421 Qmenu_item = intern ("menu-item");
3422 staticpro (&Qmenu_item);
3424 defsubr (&Skeymapp);
3425 defsubr (&Skeymap_parent);
3426 defsubr (&Sset_keymap_parent);
3427 defsubr (&Smake_keymap);
3428 defsubr (&Smake_sparse_keymap);
3429 defsubr (&Scopy_keymap);
3430 defsubr (&Skey_binding);
3431 defsubr (&Slocal_key_binding);
3432 defsubr (&Sglobal_key_binding);
3433 defsubr (&Sminor_mode_key_binding);
3434 defsubr (&Sdefine_key);
3435 defsubr (&Slookup_key);
3436 defsubr (&Sdefine_prefix_command);
3437 defsubr (&Suse_global_map);
3438 defsubr (&Suse_local_map);
3439 defsubr (&Scurrent_local_map);
3440 defsubr (&Scurrent_global_map);
3441 defsubr (&Scurrent_minor_mode_maps);
3442 defsubr (&Saccessible_keymaps);
3443 defsubr (&Skey_description);
3444 defsubr (&Sdescribe_vector);
3445 defsubr (&Ssingle_key_description);
3446 defsubr (&Stext_char_description);
3447 defsubr (&Swhere_is_internal);
3448 defsubr (&Sdescribe_bindings_internal);
3449 defsubr (&Sapropos_internal);
3452 void
3453 keys_of_keymap ()
3455 initial_define_key (global_map, 033, "ESC-prefix");
3456 initial_define_key (global_map, Ctl('X'), "Control-X-prefix");