Check for pty.h. Improve the libungif test.
[emacs.git] / src / keymap.c
blobc9c6390fc2d100a94a8dc4b495c09f2c43f35ea4
1 /* Manipulation of keymaps
2 Copyright (C) 1985, 86,87,88,93,94,95,98,99, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include <config.h>
24 #include <stdio.h>
25 #include "lisp.h"
26 #include "commands.h"
27 #include "buffer.h"
28 #include "character.h"
29 #include "charset.h"
30 #include "keyboard.h"
31 #include "termhooks.h"
32 #include "blockinput.h"
33 #include "puresize.h"
34 #include "intervals.h"
35 #include "keymap.h"
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, Qremap;
95 /* Alist of elements like (DEL . "\d"). */
96 static Lisp_Object exclude_keys;
98 /* Pre-allocated 2-element vector for Fremap_command to use. */
99 static Lisp_Object remap_command_vector;
101 /* A char with the CHAR_META bit set in a vector or the 0200 bit set
102 in a string key sequence is equivalent to prefixing with this
103 character. */
104 extern Lisp_Object meta_prefix_char;
106 extern Lisp_Object Voverriding_local_map;
108 /* Hash table used to cache a reverse-map to speed up calls to where-is. */
109 static Lisp_Object where_is_cache;
110 /* Which keymaps are reverse-stored in the cache. */
111 static Lisp_Object where_is_cache_keymaps;
113 static Lisp_Object store_in_keymap P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
114 static void fix_submap_inheritance P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
116 static Lisp_Object define_as_prefix P_ ((Lisp_Object, Lisp_Object));
117 static void describe_command P_ ((Lisp_Object, Lisp_Object));
118 static void describe_translation P_ ((Lisp_Object, Lisp_Object));
119 static void describe_map P_ ((Lisp_Object, Lisp_Object,
120 void (*) P_ ((Lisp_Object, Lisp_Object)),
121 int, Lisp_Object, Lisp_Object*, int));
122 static void silly_event_symbol_error P_ ((Lisp_Object));
124 /* Keymap object support - constructors and predicates. */
126 DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 1, 0,
127 doc: /* Construct and return a new keymap, of the form (keymap CHARTABLE . ALIST).
128 CHARTABLE is a char-table that holds the bindings for the ASCII
129 characters. ALIST is an assoc-list which holds bindings for function keys,
130 mouse events, and any other things that appear in the input stream.
131 All entries in it are initially nil, meaning "command undefined".
133 The optional arg STRING supplies a menu name for the keymap
134 in case you use it as a menu with `x-popup-menu'. */)
135 (string)
136 Lisp_Object string;
138 Lisp_Object tail;
139 if (!NILP (string))
140 tail = Fcons (string, Qnil);
141 else
142 tail = Qnil;
143 return Fcons (Qkeymap,
144 Fcons (Fmake_char_table (Qkeymap, Qnil), tail));
147 DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 1, 0,
148 doc: /* Construct and return a new sparse keymap.
149 Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),
150 which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),
151 which binds the function key or mouse event SYMBOL to DEFINITION.
152 Initially the alist is nil.
154 The optional arg STRING supplies a menu name for the keymap
155 in case you use it as a menu with `x-popup-menu'. */)
156 (string)
157 Lisp_Object string;
159 if (!NILP (string))
160 return Fcons (Qkeymap, Fcons (string, Qnil));
161 return Fcons (Qkeymap, Qnil);
164 /* This function is used for installing the standard key bindings
165 at initialization time.
167 For example:
169 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); */
171 void
172 initial_define_key (keymap, key, defname)
173 Lisp_Object keymap;
174 int key;
175 char *defname;
177 store_in_keymap (keymap, make_number (key), intern (defname));
180 void
181 initial_define_lispy_key (keymap, keyname, defname)
182 Lisp_Object keymap;
183 char *keyname;
184 char *defname;
186 store_in_keymap (keymap, intern (keyname), intern (defname));
189 DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0,
190 doc: /* Return t if OBJECT is a keymap.
192 A keymap is a list (keymap . ALIST),
193 or a symbol whose function definition is itself a keymap.
194 ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);
195 a vector of densely packed bindings for small character codes
196 is also allowed as an element. */)
197 (object)
198 Lisp_Object object;
200 return (KEYMAPP (object) ? Qt : Qnil);
203 DEFUN ("keymap-prompt", Fkeymap_prompt, Skeymap_prompt, 1, 1, 0,
204 doc: /* Return the prompt-string of a keymap MAP.
205 If non-nil, the prompt is shown in the echo-area
206 when reading a key-sequence to be looked-up in this keymap. */)
207 (map)
208 Lisp_Object map;
210 while (CONSP (map))
212 register Lisp_Object tem;
213 tem = Fcar (map);
214 if (STRINGP (tem))
215 return tem;
216 map = Fcdr (map);
218 return Qnil;
221 /* Check that OBJECT is a keymap (after dereferencing through any
222 symbols). If it is, return it.
224 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
225 is an autoload form, do the autoload and try again.
226 If AUTOLOAD is nonzero, callers must assume GC is possible.
228 If the map needs to be autoloaded, but AUTOLOAD is zero (and ERROR
229 is zero as well), return Qt.
231 ERROR controls how we respond if OBJECT isn't a keymap.
232 If ERROR is non-zero, signal an error; otherwise, just return Qnil.
234 Note that most of the time, we don't want to pursue autoloads.
235 Functions like Faccessible_keymaps which scan entire keymap trees
236 shouldn't load every autoloaded keymap. I'm not sure about this,
237 but it seems to me that only read_key_sequence, Flookup_key, and
238 Fdefine_key should cause keymaps to be autoloaded.
240 This function can GC when AUTOLOAD is non-zero, because it calls
241 do_autoload which can GC. */
243 Lisp_Object
244 get_keymap (object, error, autoload)
245 Lisp_Object object;
246 int error, autoload;
248 Lisp_Object tem;
250 autoload_retry:
251 if (NILP (object))
252 goto end;
253 if (CONSP (object) && EQ (XCAR (object), Qkeymap))
254 return object;
256 tem = indirect_function (object);
257 if (CONSP (tem))
259 if (EQ (XCAR (tem), Qkeymap))
260 return tem;
262 /* Should we do an autoload? Autoload forms for keymaps have
263 Qkeymap as their fifth element. */
264 if ((autoload || !error) && EQ (XCAR (tem), Qautoload))
266 Lisp_Object tail;
268 tail = Fnth (make_number (4), tem);
269 if (EQ (tail, Qkeymap))
271 if (autoload)
273 struct gcpro gcpro1, gcpro2;
275 GCPRO2 (tem, object);
276 do_autoload (tem, object);
277 UNGCPRO;
279 goto autoload_retry;
281 else
282 return Qt;
287 end:
288 if (error)
289 wrong_type_argument (Qkeymapp, object);
290 return Qnil;
293 /* Return the parent map of the keymap MAP, or nil if it has none.
294 We assume that MAP is a valid keymap. */
296 DEFUN ("keymap-parent", Fkeymap_parent, Skeymap_parent, 1, 1, 0,
297 doc: /* Return the parent keymap of KEYMAP. */)
298 (keymap)
299 Lisp_Object keymap;
301 Lisp_Object list;
303 keymap = get_keymap (keymap, 1, 1);
305 /* Skip past the initial element `keymap'. */
306 list = XCDR (keymap);
307 for (; CONSP (list); list = XCDR (list))
309 /* See if there is another `keymap'. */
310 if (KEYMAPP (list))
311 return list;
314 return get_keymap (list, 0, 1);
318 /* Check whether MAP is one of MAPS parents. */
320 keymap_memberp (map, maps)
321 Lisp_Object map, maps;
323 if (NILP (map)) return 0;
324 while (KEYMAPP (maps) && !EQ (map, maps))
325 maps = Fkeymap_parent (maps);
326 return (EQ (map, maps));
329 /* Set the parent keymap of MAP to PARENT. */
331 DEFUN ("set-keymap-parent", Fset_keymap_parent, Sset_keymap_parent, 2, 2, 0,
332 doc: /* Modify KEYMAP to set its parent map to PARENT.
333 PARENT should be nil or another keymap. */)
334 (keymap, parent)
335 Lisp_Object keymap, parent;
337 Lisp_Object list, prev;
338 struct gcpro gcpro1;
339 int i;
341 /* Force a keymap flush for the next call to where-is.
342 Since this can be called from within where-is, we don't set where_is_cache
343 directly but only where_is_cache_keymaps, since where_is_cache shouldn't
344 be changed during where-is, while where_is_cache_keymaps is only used at
345 the very beginning of where-is and can thus be changed here without any
346 adverse effect.
347 This is a very minor correctness (rather than safety) issue. */
348 where_is_cache_keymaps = Qt;
350 keymap = get_keymap (keymap, 1, 1);
351 GCPRO1 (keymap);
353 if (!NILP (parent))
355 parent = get_keymap (parent, 1, 1);
357 /* Check for cycles. */
358 if (keymap_memberp (keymap, parent))
359 error ("Cyclic keymap inheritance");
362 /* Skip past the initial element `keymap'. */
363 prev = keymap;
364 while (1)
366 list = XCDR (prev);
367 /* If there is a parent keymap here, replace it.
368 If we came to the end, add the parent in PREV. */
369 if (!CONSP (list) || KEYMAPP (list))
371 /* If we already have the right parent, return now
372 so that we avoid the loops below. */
373 if (EQ (XCDR (prev), parent))
374 RETURN_UNGCPRO (parent);
376 XSETCDR (prev, parent);
377 break;
379 prev = list;
382 /* Scan through for submaps, and set their parents too. */
384 for (list = XCDR (keymap); CONSP (list); list = XCDR (list))
386 /* Stop the scan when we come to the parent. */
387 if (EQ (XCAR (list), Qkeymap))
388 break;
390 /* If this element holds a prefix map, deal with it. */
391 if (CONSP (XCAR (list))
392 && CONSP (XCDR (XCAR (list))))
393 fix_submap_inheritance (keymap, XCAR (XCAR (list)),
394 XCDR (XCAR (list)));
396 if (VECTORP (XCAR (list)))
397 for (i = 0; i < XVECTOR (XCAR (list))->size; i++)
398 if (CONSP (XVECTOR (XCAR (list))->contents[i]))
399 fix_submap_inheritance (keymap, make_number (i),
400 XVECTOR (XCAR (list))->contents[i]);
402 if (CHAR_TABLE_P (XCAR (list)))
404 Lisp_Object indices[3];
406 map_char_table (fix_submap_inheritance, Qnil, XCAR (list),
407 keymap, 0, indices);
411 RETURN_UNGCPRO (parent);
414 /* EVENT is defined in MAP as a prefix, and SUBMAP is its definition.
415 if EVENT is also a prefix in MAP's parent,
416 make sure that SUBMAP inherits that definition as its own parent. */
418 static void
419 fix_submap_inheritance (map, event, submap)
420 Lisp_Object map, event, submap;
422 Lisp_Object map_parent, parent_entry;
424 /* SUBMAP is a cons that we found as a key binding.
425 Discard the other things found in a menu key binding. */
427 submap = get_keymap (get_keyelt (submap, 0), 0, 0);
429 /* If it isn't a keymap now, there's no work to do. */
430 if (!CONSP (submap))
431 return;
433 map_parent = Fkeymap_parent (map);
434 if (!NILP (map_parent))
435 parent_entry =
436 get_keymap (access_keymap (map_parent, event, 0, 0, 0), 0, 0);
437 else
438 parent_entry = Qnil;
440 /* If MAP's parent has something other than a keymap,
441 our own submap shadows it completely. */
442 if (!CONSP (parent_entry))
443 return;
445 if (! EQ (parent_entry, submap))
447 Lisp_Object submap_parent;
448 submap_parent = submap;
449 while (1)
451 Lisp_Object tem;
453 tem = Fkeymap_parent (submap_parent);
455 if (KEYMAPP (tem))
457 if (keymap_memberp (tem, parent_entry))
458 /* Fset_keymap_parent could create a cycle. */
459 return;
460 submap_parent = tem;
462 else
463 break;
465 Fset_keymap_parent (submap_parent, parent_entry);
469 /* Look up IDX in MAP. IDX may be any sort of event.
470 Note that this does only one level of lookup; IDX must be a single
471 event, not a sequence.
473 If T_OK is non-zero, bindings for Qt are treated as default
474 bindings; any key left unmentioned by other tables and bindings is
475 given the binding of Qt.
477 If T_OK is zero, bindings for Qt are not treated specially.
479 If NOINHERIT, don't accept a subkeymap found in an inherited keymap. */
481 Lisp_Object
482 access_keymap (map, idx, t_ok, noinherit, autoload)
483 Lisp_Object map;
484 Lisp_Object idx;
485 int t_ok;
486 int noinherit;
487 int autoload;
489 Lisp_Object val;
491 /* Qunbound in VAL means we have found no binding yet. */
492 val = Qunbound;
494 /* If idx is a list (some sort of mouse click, perhaps?),
495 the index we want to use is the car of the list, which
496 ought to be a symbol. */
497 idx = EVENT_HEAD (idx);
499 /* If idx is a symbol, it might have modifiers, which need to
500 be put in the canonical order. */
501 if (SYMBOLP (idx))
502 idx = reorder_modifiers (idx);
503 else if (INTEGERP (idx))
504 /* Clobber the high bits that can be present on a machine
505 with more than 24 bits of integer. */
506 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
508 /* Handle the special meta -> esc mapping. */
509 if (INTEGERP (idx) && XUINT (idx) & meta_modifier)
511 /* See if there is a meta-map. If there's none, there is
512 no binding for IDX, unless a default binding exists in MAP. */
513 Lisp_Object meta_map =
514 get_keymap (access_keymap (map, meta_prefix_char,
515 t_ok, noinherit, autoload),
516 0, autoload);
517 if (CONSP (meta_map))
519 map = meta_map;
520 idx = make_number (XUINT (idx) & ~meta_modifier);
522 else if (t_ok)
523 /* Set IDX to t, so that we only find a default binding. */
524 idx = Qt;
525 else
526 /* We know there is no binding. */
527 return Qnil;
531 Lisp_Object tail;
533 /* t_binding is where we put a default binding that applies,
534 to use in case we do not find a binding specifically
535 for this key sequence. */
537 Lisp_Object t_binding;
538 t_binding = Qnil;
540 /* If `t_ok' is 2, both `t' and generic-char bindings are accepted.
541 If it is 1, only generic-char bindings are accepted.
542 Otherwise, neither are. */
543 t_ok = t_ok ? 2 : 0;
545 for (tail = XCDR (map);
546 (CONSP (tail)
547 || (tail = get_keymap (tail, 0, autoload), CONSP (tail)));
548 tail = XCDR (tail))
550 Lisp_Object binding;
552 binding = XCAR (tail);
553 if (SYMBOLP (binding))
555 /* If NOINHERIT, stop finding prefix definitions
556 after we pass a second occurrence of the `keymap' symbol. */
557 if (noinherit && EQ (binding, Qkeymap))
558 return Qnil;
560 else if (CONSP (binding))
562 Lisp_Object key = XCAR (binding);
564 if (EQ (key, idx))
565 val = XCDR (binding);
566 else if (t_ok
567 && INTEGERP (idx)
568 && (XINT (idx) & CHAR_MODIFIER_MASK) == 0
569 && INTEGERP (key)
570 && (XINT (key) & CHAR_MODIFIER_MASK) == 0
571 && !SINGLE_BYTE_CHAR_P (XINT (idx))
572 && !SINGLE_BYTE_CHAR_P (XINT (key))
573 && CHAR_VALID_P (XINT (key), 1)
574 && !CHAR_VALID_P (XINT (key), 0)
575 && (CHAR_CHARSET (XINT (key))
576 == CHAR_CHARSET (XINT (idx))))
578 /* KEY is the generic character of the charset of IDX.
579 Use KEY's binding if there isn't a binding for IDX
580 itself. */
581 t_binding = XCDR (binding);
582 t_ok = 0;
584 else if (t_ok > 1 && EQ (key, Qt))
586 t_binding = XCDR (binding);
587 t_ok = 1;
590 else if (VECTORP (binding))
592 if (NATNUMP (idx) && XFASTINT (idx) < ASIZE (binding))
593 val = AREF (binding, XFASTINT (idx));
595 else if (CHAR_TABLE_P (binding))
597 /* Character codes with modifiers
598 are not included in a char-table.
599 All character codes without modifiers are included. */
600 if (NATNUMP (idx) && (XFASTINT (idx) & CHAR_MODIFIER_MASK) == 0)
602 val = Faref (binding, idx);
603 /* `nil' has a special meaning for char-tables, so
604 we use something else to record an explicitly
605 unbound entry. */
606 if (NILP (val))
607 val = Qunbound;
611 /* If we found a binding, clean it up and return it. */
612 if (!EQ (val, Qunbound))
614 if (EQ (val, Qt))
615 /* A Qt binding is just like an explicit nil binding
616 (i.e. it shadows any parent binding but not bindings in
617 keymaps of lower precedence). */
618 val = Qnil;
619 val = get_keyelt (val, autoload);
620 if (KEYMAPP (val))
621 fix_submap_inheritance (map, idx, val);
622 return val;
624 QUIT;
627 return get_keyelt (t_binding, autoload);
631 /* Given OBJECT which was found in a slot in a keymap,
632 trace indirect definitions to get the actual definition of that slot.
633 An indirect definition is a list of the form
634 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
635 and INDEX is the object to look up in KEYMAP to yield the definition.
637 Also if OBJECT has a menu string as the first element,
638 remove that. Also remove a menu help string as second element.
640 If AUTOLOAD is nonzero, load autoloadable keymaps
641 that are referred to with indirection. */
643 Lisp_Object
644 get_keyelt (object, autoload)
645 register Lisp_Object object;
646 int autoload;
648 while (1)
650 if (!(CONSP (object)))
651 /* This is really the value. */
652 return object;
654 /* If the keymap contents looks like (keymap ...) or (lambda ...)
655 then use itself. */
656 else if (EQ (XCAR (object), Qkeymap) || EQ (XCAR (object), Qlambda))
657 return object;
659 /* If the keymap contents looks like (menu-item name . DEFN)
660 or (menu-item name DEFN ...) then use DEFN.
661 This is a new format menu item. */
662 else if (EQ (XCAR (object), Qmenu_item))
664 if (CONSP (XCDR (object)))
666 Lisp_Object tem;
668 object = XCDR (XCDR (object));
669 tem = object;
670 if (CONSP (object))
671 object = XCAR (object);
673 /* If there's a `:filter FILTER', apply FILTER to the
674 menu-item's definition to get the real definition to
675 use. */
676 for (; CONSP (tem) && CONSP (XCDR (tem)); tem = XCDR (tem))
677 if (EQ (XCAR (tem), QCfilter) && autoload)
679 Lisp_Object filter;
680 filter = XCAR (XCDR (tem));
681 filter = list2 (filter, list2 (Qquote, object));
682 object = menu_item_eval_property (filter);
683 break;
686 else
687 /* Invalid keymap */
688 return object;
691 /* If the keymap contents looks like (STRING . DEFN), use DEFN.
692 Keymap alist elements like (CHAR MENUSTRING . DEFN)
693 will be used by HierarKey menus. */
694 else if (STRINGP (XCAR (object)))
696 object = XCDR (object);
697 /* Also remove a menu help string, if any,
698 following the menu item name. */
699 if (CONSP (object) && STRINGP (XCAR (object)))
700 object = XCDR (object);
701 /* Also remove the sublist that caches key equivalences, if any. */
702 if (CONSP (object) && CONSP (XCAR (object)))
704 Lisp_Object carcar;
705 carcar = XCAR (XCAR (object));
706 if (NILP (carcar) || VECTORP (carcar))
707 object = XCDR (object);
711 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
712 else
714 Lisp_Object map;
715 map = get_keymap (Fcar_safe (object), 0, autoload);
716 return (!CONSP (map) ? object /* Invalid keymap */
717 : access_keymap (map, Fcdr (object), 0, 0, autoload));
722 static Lisp_Object
723 store_in_keymap (keymap, idx, def)
724 Lisp_Object keymap;
725 register Lisp_Object idx;
726 register Lisp_Object def;
728 /* Flush any reverse-map cache. */
729 where_is_cache = Qnil;
730 where_is_cache_keymaps = Qt;
732 /* If we are preparing to dump, and DEF is a menu element
733 with a menu item indicator, copy it to ensure it is not pure. */
734 if (CONSP (def) && PURE_P (def)
735 && (EQ (XCAR (def), Qmenu_item) || STRINGP (XCAR (def))))
736 def = Fcons (XCAR (def), XCDR (def));
738 if (!CONSP (keymap) || !EQ (XCAR (keymap), Qkeymap))
739 error ("attempt to define a key in a non-keymap");
741 /* If idx is a list (some sort of mouse click, perhaps?),
742 the index we want to use is the car of the list, which
743 ought to be a symbol. */
744 idx = EVENT_HEAD (idx);
746 /* If idx is a symbol, it might have modifiers, which need to
747 be put in the canonical order. */
748 if (SYMBOLP (idx))
749 idx = reorder_modifiers (idx);
750 else if (INTEGERP (idx))
751 /* Clobber the high bits that can be present on a machine
752 with more than 24 bits of integer. */
753 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
755 /* Scan the keymap for a binding of idx. */
757 Lisp_Object tail;
759 /* The cons after which we should insert new bindings. If the
760 keymap has a table element, we record its position here, so new
761 bindings will go after it; this way, the table will stay
762 towards the front of the alist and character lookups in dense
763 keymaps will remain fast. Otherwise, this just points at the
764 front of the keymap. */
765 Lisp_Object insertion_point;
767 insertion_point = keymap;
768 for (tail = XCDR (keymap); CONSP (tail); tail = XCDR (tail))
770 Lisp_Object elt;
772 elt = XCAR (tail);
773 if (VECTORP (elt))
775 if (NATNUMP (idx) && XFASTINT (idx) < ASIZE (elt))
777 ASET (elt, XFASTINT (idx), def);
778 return def;
780 insertion_point = tail;
782 else if (CHAR_TABLE_P (elt))
784 /* Character codes with modifiers
785 are not included in a char-table.
786 All character codes without modifiers are included. */
787 if (NATNUMP (idx) && !(XFASTINT (idx) & CHAR_MODIFIER_MASK))
789 Faset (elt, idx,
790 /* `nil' has a special meaning for char-tables, so
791 we use something else to record an explicitly
792 unbound entry. */
793 NILP (def) ? Qt : def);
794 return def;
796 else if (CONSP (idx) && CHARACTERP (XCAR (idx)))
798 Fset_char_table_range (elt, idx, NILP (def) ? Qt : def);
799 return def;
801 insertion_point = tail;
803 else if (CONSP (elt))
805 if (EQ (idx, XCAR (elt)))
807 XSETCDR (elt, def);
808 return def;
811 else if (EQ (elt, Qkeymap))
812 /* If we find a 'keymap' symbol in the spine of KEYMAP,
813 then we must have found the start of a second keymap
814 being used as the tail of KEYMAP, and a binding for IDX
815 should be inserted before it. */
816 goto keymap_end;
818 QUIT;
821 keymap_end:
822 /* We have scanned the entire keymap, and not found a binding for
823 IDX. Let's add one. */
824 XSETCDR (insertion_point,
825 Fcons (Fcons (idx, def), XCDR (insertion_point)));
828 return def;
831 EXFUN (Fcopy_keymap, 1);
833 void
834 copy_keymap_1 (chartable, idx, elt)
835 Lisp_Object chartable, idx, elt;
837 if (CONSP (elt) && EQ (XCAR (elt), Qkeymap))
838 Faset (chartable, idx, Fcopy_keymap (elt));
841 DEFUN ("copy-keymap", Fcopy_keymap, Scopy_keymap, 1, 1, 0,
842 doc: /* Return a copy of the keymap KEYMAP.
843 The copy starts out with the same definitions of KEYMAP,
844 but changing either the copy or KEYMAP does not affect the other.
845 Any key definitions that are subkeymaps are recursively copied.
846 However, a key definition which is a symbol whose definition is a keymap
847 is not copied. */)
848 (keymap)
849 Lisp_Object keymap;
851 /* FIXME: This doesn't properly copy menu-items in vectors. */
852 /* FIXME: This also copies the parent keymap. */
854 register Lisp_Object copy, tail;
856 copy = Fcopy_alist (get_keymap (keymap, 1, 0));
858 for (tail = copy; CONSP (tail); tail = XCDR (tail))
860 Lisp_Object elt;
862 elt = XCAR (tail);
863 if (CHAR_TABLE_P (elt))
865 Lisp_Object indices[3];
867 elt = Fcopy_sequence (elt);
868 XSETCAR (tail, elt);
870 map_char_table (copy_keymap_1, Qnil, elt, elt, 0, indices);
872 else if (VECTORP (elt))
874 int i;
876 elt = Fcopy_sequence (elt);
877 XSETCAR (tail, elt);
879 for (i = 0; i < ASIZE (elt); i++)
880 if (CONSP (AREF (elt, i)) && EQ (XCAR (AREF (elt, i)), Qkeymap))
881 ASET (elt, i, Fcopy_keymap (AREF (elt, i)));
883 else if (CONSP (elt) && CONSP (XCDR (elt)))
885 Lisp_Object tem;
886 tem = XCDR (elt);
888 /* Is this a new format menu item. */
889 if (EQ (XCAR (tem),Qmenu_item))
891 /* Copy cell with menu-item marker. */
892 XSETCDR (elt,
893 Fcons (XCAR (tem), XCDR (tem)));
894 elt = XCDR (elt);
895 tem = XCDR (elt);
896 if (CONSP (tem))
898 /* Copy cell with menu-item name. */
899 XSETCDR (elt,
900 Fcons (XCAR (tem), XCDR (tem)));
901 elt = XCDR (elt);
902 tem = XCDR (elt);
904 if (CONSP (tem))
906 /* Copy cell with binding and if the binding is a keymap,
907 copy that. */
908 XSETCDR (elt,
909 Fcons (XCAR (tem), XCDR (tem)));
910 elt = XCDR (elt);
911 tem = XCAR (elt);
912 if (CONSP (tem) && EQ (XCAR (tem), Qkeymap))
913 XSETCAR (elt, Fcopy_keymap (tem));
914 tem = XCDR (elt);
915 if (CONSP (tem) && CONSP (XCAR (tem)))
916 /* Delete cache for key equivalences. */
917 XSETCDR (elt, XCDR (tem));
920 else
922 /* It may be an old fomat menu item.
923 Skip the optional menu string.
925 if (STRINGP (XCAR (tem)))
927 /* Copy the cell, since copy-alist didn't go this deep. */
928 XSETCDR (elt,
929 Fcons (XCAR (tem), XCDR (tem)));
930 elt = XCDR (elt);
931 tem = XCDR (elt);
932 /* Also skip the optional menu help string. */
933 if (CONSP (tem) && STRINGP (XCAR (tem)))
935 XSETCDR (elt,
936 Fcons (XCAR (tem), XCDR (tem)));
937 elt = XCDR (elt);
938 tem = XCDR (elt);
940 /* There may also be a list that caches key equivalences.
941 Just delete it for the new keymap. */
942 if (CONSP (tem)
943 && CONSP (XCAR (tem))
944 && (NILP (XCAR (XCAR (tem)))
945 || VECTORP (XCAR (XCAR (tem)))))
946 XSETCDR (elt, XCDR (tem));
948 if (CONSP (elt)
949 && CONSP (XCDR (elt))
950 && EQ (XCAR (XCDR (elt)), Qkeymap))
951 XSETCDR (elt, Fcopy_keymap (XCDR (elt)));
957 return copy;
960 /* Simple Keymap mutators and accessors. */
962 /* GC is possible in this function if it autoloads a keymap. */
964 DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0,
965 doc: /* Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.
966 KEYMAP is a keymap.
968 KEY is a string or a vector of symbols and characters meaning a
969 sequence of keystrokes and events. Non-ASCII characters with codes
970 above 127 (such as ISO Latin-1) can be included if you use a vector.
972 DEF is anything that can be a key's definition:
973 nil (means key is undefined in this keymap),
974 a command (a Lisp function suitable for interactive calling)
975 a string (treated as a keyboard macro),
976 a keymap (to define a prefix key),
977 a symbol. When the key is looked up, the symbol will stand for its
978 function definition, which should at that time be one of the above,
979 or another symbol whose function definition is used, etc.
980 a cons (STRING . DEFN), meaning that DEFN is the definition
981 (DEFN should be a valid definition in its own right),
982 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.
984 If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at
985 the front of KEYMAP. */)
986 (keymap, key, def)
987 Lisp_Object keymap;
988 Lisp_Object key;
989 Lisp_Object def;
991 register int idx;
992 register Lisp_Object c;
993 register Lisp_Object cmd;
994 int metized = 0;
995 int meta_bit;
996 int length;
997 struct gcpro gcpro1, gcpro2, gcpro3;
999 keymap = get_keymap (keymap, 1, 1);
1001 if (!VECTORP (key) && !STRINGP (key))
1002 key = wrong_type_argument (Qarrayp, key);
1004 length = XFASTINT (Flength (key));
1005 if (length == 0)
1006 return Qnil;
1008 /* Check for valid [remap COMMAND] bindings. */
1009 if (VECTORP (key) && EQ (AREF (key, 0), Qremap)
1010 && (length != 2 || !SYMBOLP (AREF (key, 1))))
1011 wrong_type_argument (Qvectorp, key);
1013 if (SYMBOLP (def) && !EQ (Vdefine_key_rebound_commands, Qt))
1014 Vdefine_key_rebound_commands = Fcons (def, Vdefine_key_rebound_commands);
1016 GCPRO3 (keymap, key, def);
1018 if (VECTORP (key))
1019 meta_bit = meta_modifier;
1020 else
1021 meta_bit = 0x80;
1023 idx = 0;
1024 while (1)
1026 c = Faref (key, make_number (idx));
1028 if (CONSP (c))
1030 /* C may be a cons (FROM . TO) specifying a range of
1031 characters. */
1032 if (CHARACTERP (XCAR (c)))
1033 CHECK_CHARACTER (XCDR (c));
1034 else if (lucid_event_type_list_p (c))
1035 c = Fevent_convert_list (c);
1038 if (SYMBOLP (c))
1039 silly_event_symbol_error (c);
1041 if (INTEGERP (c)
1042 && (XINT (c) & meta_bit)
1043 && !metized)
1045 c = meta_prefix_char;
1046 metized = 1;
1048 else
1050 if (INTEGERP (c))
1051 XSETINT (c, XINT (c) & ~meta_bit);
1053 metized = 0;
1054 idx++;
1057 if (!INTEGERP (c) && !SYMBOLP (c)
1058 && (!CONSP (c)
1059 /* If C is a range, it must be a leaf. */
1060 || (INTEGERP (XCAR (c)) && idx != length)))
1061 error ("Key sequence contains invalid event");
1063 if (idx == length)
1064 RETURN_UNGCPRO (store_in_keymap (keymap, c, def));
1066 cmd = access_keymap (keymap, c, 0, 1, 1);
1068 /* If this key is undefined, make it a prefix. */
1069 if (NILP (cmd))
1070 cmd = define_as_prefix (keymap, c);
1072 keymap = get_keymap (cmd, 0, 1);
1073 if (!CONSP (keymap))
1074 /* We must use Fkey_description rather than just passing key to
1075 error; key might be a vector, not a string. */
1076 error ("Key sequence %s uses invalid prefix characters",
1077 XSTRING (Fkey_description (key))->data);
1081 /* This function may GC (it calls Fkey_binding). */
1083 DEFUN ("remap-command", Fremap_command, Sremap_command, 1, 1, 0,
1084 doc: /* Return the remapping for command COMMAND in current keymaps.
1085 Returns nil if COMMAND is not remapped. */)
1086 (command)
1087 Lisp_Object command;
1089 /* This will GCPRO the command argument. */
1090 ASET (remap_command_vector, 1, command);
1091 return Fkey_binding (remap_command_vector, Qnil, Qt);
1094 /* Value is number if KEY is too long; nil if valid but has no definition. */
1095 /* GC is possible in this function if it autoloads a keymap. */
1097 DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 3, 0,
1098 doc: /* In keymap KEYMAP, look up key sequence KEY. Return the definition.
1099 nil means undefined. See doc of `define-key' for kinds of definitions.
1101 A number as value means KEY is "too long";
1102 that is, characters or symbols in it except for the last one
1103 fail to be a valid sequence of prefix characters in KEYMAP.
1104 The number is how many characters at the front of KEY
1105 it takes to reach a non-prefix command.
1107 Normally, `lookup-key' ignores bindings for t, which act as default
1108 bindings, used when nothing else in the keymap applies; this makes it
1109 usable as a general function for probing keymaps. However, if the
1110 third optional argument ACCEPT-DEFAULT is non-nil, `lookup-key' will
1111 recognize the default bindings, just as `read-key-sequence' does. */)
1112 (keymap, key, accept_default)
1113 register Lisp_Object keymap;
1114 Lisp_Object key;
1115 Lisp_Object accept_default;
1117 register int idx;
1118 register Lisp_Object cmd;
1119 register Lisp_Object c;
1120 int length;
1121 int t_ok = !NILP (accept_default);
1122 struct gcpro gcpro1;
1124 keymap = get_keymap (keymap, 1, 1);
1126 /* Perform command remapping initiated by Fremap_command directly.
1127 This is strictly not necessary, but it is faster and it returns
1128 nil instead of 1 if KEYMAP doesn't contain command remappings. */
1129 if (EQ (key, remap_command_vector))
1131 /* KEY has format [remap COMMAND].
1132 Lookup `remap' in KEYMAP; result is nil or a keymap containing
1133 command remappings. Then lookup COMMAND in that keymap. */
1134 if ((keymap = access_keymap (keymap, Qremap, t_ok, 0, 1), !NILP (keymap))
1135 && (keymap = get_keymap (keymap, 0, 1), CONSP (keymap)))
1136 return access_keymap (keymap, AREF (key, 1), t_ok, 0, 1);
1137 return Qnil;
1140 if (!VECTORP (key) && !STRINGP (key))
1141 key = wrong_type_argument (Qarrayp, key);
1143 length = XFASTINT (Flength (key));
1144 if (length == 0)
1145 return keymap;
1147 GCPRO1 (key);
1149 idx = 0;
1150 while (1)
1152 c = Faref (key, make_number (idx++));
1154 if (CONSP (c) && lucid_event_type_list_p (c))
1155 c = Fevent_convert_list (c);
1157 /* Turn the 8th bit of string chars into a meta modifier. */
1158 if (XINT (c) & 0x80 && STRINGP (key))
1159 XSETINT (c, (XINT (c) | meta_modifier) & ~0x80);
1161 /* Allow string since binding for `menu-bar-select-buffer'
1162 includes the buffer name in the key sequence. */
1163 if (!INTEGERP (c) && !SYMBOLP (c) && !CONSP (c) && !STRINGP (c))
1164 error ("Key sequence contains invalid event");
1166 cmd = access_keymap (keymap, c, t_ok, 0, 1);
1167 if (idx == length)
1168 RETURN_UNGCPRO (cmd);
1170 keymap = get_keymap (cmd, 0, 1);
1171 if (!CONSP (keymap))
1172 RETURN_UNGCPRO (make_number (idx));
1174 QUIT;
1178 /* Make KEYMAP define event C as a keymap (i.e., as a prefix).
1179 Assume that currently it does not define C at all.
1180 Return the keymap. */
1182 static Lisp_Object
1183 define_as_prefix (keymap, c)
1184 Lisp_Object keymap, c;
1186 Lisp_Object cmd;
1188 cmd = Fmake_sparse_keymap (Qnil);
1189 /* If this key is defined as a prefix in an inherited keymap,
1190 make it a prefix in this map, and make its definition
1191 inherit the other prefix definition. */
1192 cmd = nconc2 (cmd, access_keymap (keymap, c, 0, 0, 0));
1193 store_in_keymap (keymap, c, cmd);
1195 return cmd;
1198 /* Append a key to the end of a key sequence. We always make a vector. */
1200 Lisp_Object
1201 append_key (key_sequence, key)
1202 Lisp_Object key_sequence, key;
1204 Lisp_Object args[2];
1206 args[0] = key_sequence;
1208 args[1] = Fcons (key, Qnil);
1209 return Fvconcat (2, args);
1212 /* Given a event type C which is a symbol,
1213 signal an error if is a mistake such as RET or M-RET or C-DEL, etc. */
1215 static void
1216 silly_event_symbol_error (c)
1217 Lisp_Object c;
1219 Lisp_Object parsed, base, name, assoc;
1220 int modifiers;
1222 parsed = parse_modifiers (c);
1223 modifiers = (int) XUINT (XCAR (XCDR (parsed)));
1224 base = XCAR (parsed);
1225 name = Fsymbol_name (base);
1226 /* This alist includes elements such as ("RET" . "\\r"). */
1227 assoc = Fassoc (name, exclude_keys);
1229 if (! NILP (assoc))
1231 char new_mods[sizeof ("\\A-\\C-\\H-\\M-\\S-\\s-")];
1232 char *p = new_mods;
1233 Lisp_Object keystring;
1234 if (modifiers & alt_modifier)
1235 { *p++ = '\\'; *p++ = 'A'; *p++ = '-'; }
1236 if (modifiers & ctrl_modifier)
1237 { *p++ = '\\'; *p++ = 'C'; *p++ = '-'; }
1238 if (modifiers & hyper_modifier)
1239 { *p++ = '\\'; *p++ = 'H'; *p++ = '-'; }
1240 if (modifiers & meta_modifier)
1241 { *p++ = '\\'; *p++ = 'M'; *p++ = '-'; }
1242 if (modifiers & shift_modifier)
1243 { *p++ = '\\'; *p++ = 'S'; *p++ = '-'; }
1244 if (modifiers & super_modifier)
1245 { *p++ = '\\'; *p++ = 's'; *p++ = '-'; }
1246 *p = 0;
1248 c = reorder_modifiers (c);
1249 keystring = concat2 (build_string (new_mods), XCDR (assoc));
1251 error ((modifiers & ~meta_modifier
1252 ? "To bind the key %s, use [?%s], not [%s]"
1253 : "To bind the key %s, use \"%s\", not [%s]"),
1254 XSYMBOL (c)->name->data, XSTRING (keystring)->data,
1255 XSYMBOL (c)->name->data);
1259 /* Global, local, and minor mode keymap stuff. */
1261 /* We can't put these variables inside current_minor_maps, since under
1262 some systems, static gets macro-defined to be the empty string.
1263 Ickypoo. */
1264 static Lisp_Object *cmm_modes, *cmm_maps;
1265 static int cmm_size;
1267 /* Error handler used in current_minor_maps. */
1268 static Lisp_Object
1269 current_minor_maps_error ()
1271 return Qnil;
1274 /* Store a pointer to an array of the keymaps of the currently active
1275 minor modes in *buf, and return the number of maps it contains.
1277 This function always returns a pointer to the same buffer, and may
1278 free or reallocate it, so if you want to keep it for a long time or
1279 hand it out to lisp code, copy it. This procedure will be called
1280 for every key sequence read, so the nice lispy approach (return a
1281 new assoclist, list, what have you) for each invocation would
1282 result in a lot of consing over time.
1284 If we used xrealloc/xmalloc and ran out of memory, they would throw
1285 back to the command loop, which would try to read a key sequence,
1286 which would call this function again, resulting in an infinite
1287 loop. Instead, we'll use realloc/malloc and silently truncate the
1288 list, let the key sequence be read, and hope some other piece of
1289 code signals the error. */
1291 current_minor_maps (modeptr, mapptr)
1292 Lisp_Object **modeptr, **mapptr;
1294 int i = 0;
1295 int list_number = 0;
1296 Lisp_Object alist, assoc, var, val;
1297 Lisp_Object lists[2];
1299 lists[0] = Vminor_mode_overriding_map_alist;
1300 lists[1] = Vminor_mode_map_alist;
1302 for (list_number = 0; list_number < 2; list_number++)
1303 for (alist = lists[list_number];
1304 CONSP (alist);
1305 alist = XCDR (alist))
1306 if ((assoc = XCAR (alist), CONSP (assoc))
1307 && (var = XCAR (assoc), SYMBOLP (var))
1308 && (val = find_symbol_value (var), !EQ (val, Qunbound))
1309 && !NILP (val))
1311 Lisp_Object temp;
1313 /* If a variable has an entry in Vminor_mode_overriding_map_alist,
1314 and also an entry in Vminor_mode_map_alist,
1315 ignore the latter. */
1316 if (list_number == 1)
1318 val = assq_no_quit (var, lists[0]);
1319 if (!NILP (val))
1320 continue;
1323 if (i >= cmm_size)
1325 Lisp_Object *newmodes, *newmaps;
1327 /* Use malloc/realloc here. See the comment above
1328 this function. */
1329 if (cmm_maps)
1331 BLOCK_INPUT;
1332 cmm_size *= 2;
1333 newmodes
1334 = (Lisp_Object *) realloc (cmm_modes,
1335 cmm_size * sizeof *newmodes);
1336 newmaps
1337 = (Lisp_Object *) realloc (cmm_maps,
1338 cmm_size * sizeof *newmaps);
1339 UNBLOCK_INPUT;
1341 else
1343 BLOCK_INPUT;
1344 cmm_size = 30;
1345 newmodes
1346 = (Lisp_Object *) malloc (cmm_size * sizeof *newmodes);
1347 newmaps
1348 = (Lisp_Object *) malloc (cmm_size * sizeof *newmaps);
1349 UNBLOCK_INPUT;
1352 if (newmodes)
1353 cmm_modes = newmodes;
1354 if (newmaps)
1355 cmm_maps = newmaps;
1357 if (newmodes == NULL || newmaps == NULL)
1358 break;
1361 /* Get the keymap definition--or nil if it is not defined. */
1362 temp = internal_condition_case_1 (Findirect_function,
1363 XCDR (assoc),
1364 Qerror, current_minor_maps_error);
1365 if (!NILP (temp))
1367 cmm_modes[i] = var;
1368 cmm_maps [i] = temp;
1369 i++;
1373 if (modeptr) *modeptr = cmm_modes;
1374 if (mapptr) *mapptr = cmm_maps;
1375 return i;
1378 DEFUN ("current-active-maps", Fcurrent_active_maps, Scurrent_active_maps,
1379 0, 1, 0,
1380 doc: /* Return a list of the currently active keymaps.
1381 OLP if non-nil indicates that we should obey `overriding-local-map' and
1382 `overriding-terminal-local-map'. */)
1383 (olp)
1384 Lisp_Object olp;
1386 Lisp_Object keymaps = Fcons (current_global_map, Qnil);
1388 if (!NILP (olp))
1390 if (!NILP (Voverriding_local_map))
1391 keymaps = Fcons (Voverriding_local_map, keymaps);
1392 if (!NILP (current_kboard->Voverriding_terminal_local_map))
1393 keymaps = Fcons (current_kboard->Voverriding_terminal_local_map, keymaps);
1395 if (NILP (XCDR (keymaps)))
1397 Lisp_Object local;
1398 Lisp_Object *maps;
1399 int nmaps, i;
1401 local = get_local_map (PT, current_buffer, Qlocal_map);
1402 if (!NILP (local))
1403 keymaps = Fcons (local, keymaps);
1405 nmaps = current_minor_maps (0, &maps);
1407 for (i = --nmaps; i >= 0; i--)
1408 if (!NILP (maps[i]))
1409 keymaps = Fcons (maps[i], keymaps);
1411 local = get_local_map (PT, current_buffer, Qkeymap);
1412 if (!NILP (local))
1413 keymaps = Fcons (local, keymaps);
1416 return keymaps;
1419 /* GC is possible in this function if it autoloads a keymap. */
1421 DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 3, 0,
1422 doc: /* Return the binding for command KEY in current keymaps.
1423 KEY is a string or vector, a sequence of keystrokes.
1424 The binding is probably a symbol with a function definition.
1426 Normally, `key-binding' ignores bindings for t, which act as default
1427 bindings, used when nothing else in the keymap applies; this makes it
1428 usable as a general function for probing keymaps. However, if the
1429 optional second argument ACCEPT-DEFAULT is non-nil, `key-binding' does
1430 recognize the default bindings, just as `read-key-sequence' does.
1432 Like the normal command loop, `key-binding' will remap the command
1433 resulting from looking up KEY by looking up the command in the
1434 currrent keymaps. However, if the optional third argument NO-REMAP
1435 is non-nil, `key-binding' returns the unmapped command. */)
1436 (key, accept_default, no_remap)
1437 Lisp_Object key, accept_default, no_remap;
1439 Lisp_Object *maps, value;
1440 int nmaps, i;
1441 struct gcpro gcpro1;
1443 GCPRO1 (key);
1445 if (!NILP (current_kboard->Voverriding_terminal_local_map))
1447 value = Flookup_key (current_kboard->Voverriding_terminal_local_map,
1448 key, accept_default);
1449 if (! NILP (value) && !INTEGERP (value))
1450 goto done;
1452 else if (!NILP (Voverriding_local_map))
1454 value = Flookup_key (Voverriding_local_map, key, accept_default);
1455 if (! NILP (value) && !INTEGERP (value))
1456 goto done;
1458 else
1460 Lisp_Object local;
1462 local = get_local_map (PT, current_buffer, Qkeymap);
1463 if (! NILP (local))
1465 value = Flookup_key (local, key, accept_default);
1466 if (! NILP (value) && !INTEGERP (value))
1467 goto done;
1470 nmaps = current_minor_maps (0, &maps);
1471 /* Note that all these maps are GCPRO'd
1472 in the places where we found them. */
1474 for (i = 0; i < nmaps; i++)
1475 if (! NILP (maps[i]))
1477 value = Flookup_key (maps[i], key, accept_default);
1478 if (! NILP (value) && !INTEGERP (value))
1479 goto done;
1482 local = get_local_map (PT, current_buffer, Qlocal_map);
1483 if (! NILP (local))
1485 value = Flookup_key (local, key, accept_default);
1486 if (! NILP (value) && !INTEGERP (value))
1487 goto done;
1491 value = Flookup_key (current_global_map, key, accept_default);
1493 done:
1494 UNGCPRO;
1495 if (NILP (value) || INTEGERP (value))
1496 return Qnil;
1498 /* If the result of the ordinary keymap lookup is an interactive
1499 command, look for a key binding (ie. remapping) for that command. */
1501 if (NILP (no_remap) && SYMBOLP (value))
1503 Lisp_Object value1;
1504 if (value1 = Fremap_command (value), !NILP (value1))
1505 value = value1;
1508 return value;
1511 /* GC is possible in this function if it autoloads a keymap. */
1513 DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 2, 0,
1514 doc: /* Return the binding for command KEYS in current local keymap only.
1515 KEYS is a string, a sequence of keystrokes.
1516 The binding is probably a symbol with a function definition.
1518 If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1519 bindings; see the description of `lookup-key' for more details about this. */)
1520 (keys, accept_default)
1521 Lisp_Object keys, accept_default;
1523 register Lisp_Object map;
1524 map = current_buffer->keymap;
1525 if (NILP (map))
1526 return Qnil;
1527 return Flookup_key (map, keys, accept_default);
1530 /* GC is possible in this function if it autoloads a keymap. */
1532 DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 2, 0,
1533 doc: /* Return the binding for command KEYS in current global keymap only.
1534 KEYS is a string, a sequence of keystrokes.
1535 The binding is probably a symbol with a function definition.
1536 This function's return values are the same as those of lookup-key
1537 \(which see).
1539 If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1540 bindings; see the description of `lookup-key' for more details about this. */)
1541 (keys, accept_default)
1542 Lisp_Object keys, accept_default;
1544 return Flookup_key (current_global_map, keys, accept_default);
1547 /* GC is possible in this function if it autoloads a keymap. */
1549 DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0,
1550 doc: /* Find the visible minor mode bindings of KEY.
1551 Return an alist of pairs (MODENAME . BINDING), where MODENAME is the
1552 the symbol which names the minor mode binding KEY, and BINDING is
1553 KEY's definition in that mode. In particular, if KEY has no
1554 minor-mode bindings, return nil. If the first binding is a
1555 non-prefix, all subsequent bindings will be omitted, since they would
1556 be ignored. Similarly, the list doesn't include non-prefix bindings
1557 that come after prefix bindings.
1559 If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1560 bindings; see the description of `lookup-key' for more details about this. */)
1561 (key, accept_default)
1562 Lisp_Object key, accept_default;
1564 Lisp_Object *modes, *maps;
1565 int nmaps;
1566 Lisp_Object binding;
1567 int i, j;
1568 struct gcpro gcpro1, gcpro2;
1570 nmaps = current_minor_maps (&modes, &maps);
1571 /* Note that all these maps are GCPRO'd
1572 in the places where we found them. */
1574 binding = Qnil;
1575 GCPRO2 (key, binding);
1577 for (i = j = 0; i < nmaps; i++)
1578 if (!NILP (maps[i])
1579 && !NILP (binding = Flookup_key (maps[i], key, accept_default))
1580 && !INTEGERP (binding))
1582 if (KEYMAPP (binding))
1583 maps[j++] = Fcons (modes[i], binding);
1584 else if (j == 0)
1585 RETURN_UNGCPRO (Fcons (Fcons (modes[i], binding), Qnil));
1588 UNGCPRO;
1589 return Flist (j, maps);
1592 DEFUN ("define-prefix-command", Fdefine_prefix_command, Sdefine_prefix_command, 1, 3, 0,
1593 doc: /* Define COMMAND as a prefix command. COMMAND should be a symbol.
1594 A new sparse keymap is stored as COMMAND's function definition and its value.
1595 If a second optional argument MAPVAR is given, the map is stored as
1596 its value instead of as COMMAND's value; but COMMAND is still defined
1597 as a function.
1598 The third optional argument NAME, if given, supplies a menu name
1599 string for the map. This is required to use the keymap as a menu. */)
1600 (command, mapvar, name)
1601 Lisp_Object command, mapvar, name;
1603 Lisp_Object map;
1604 map = Fmake_sparse_keymap (name);
1605 Ffset (command, map);
1606 if (!NILP (mapvar))
1607 Fset (mapvar, map);
1608 else
1609 Fset (command, map);
1610 return command;
1613 DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0,
1614 doc: /* Select KEYMAP as the global keymap. */)
1615 (keymap)
1616 Lisp_Object keymap;
1618 keymap = get_keymap (keymap, 1, 1);
1619 current_global_map = keymap;
1621 return Qnil;
1624 DEFUN ("use-local-map", Fuse_local_map, Suse_local_map, 1, 1, 0,
1625 doc: /* Select KEYMAP as the local keymap.
1626 If KEYMAP is nil, that means no local keymap. */)
1627 (keymap)
1628 Lisp_Object keymap;
1630 if (!NILP (keymap))
1631 keymap = get_keymap (keymap, 1, 1);
1633 current_buffer->keymap = keymap;
1635 return Qnil;
1638 DEFUN ("current-local-map", Fcurrent_local_map, Scurrent_local_map, 0, 0, 0,
1639 doc: /* Return current buffer's local keymap, or nil if it has none. */)
1642 return current_buffer->keymap;
1645 DEFUN ("current-global-map", Fcurrent_global_map, Scurrent_global_map, 0, 0, 0,
1646 doc: /* Return the current global keymap. */)
1649 return current_global_map;
1652 DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps, Scurrent_minor_mode_maps, 0, 0, 0,
1653 doc: /* Return a list of keymaps for the minor modes of the current buffer. */)
1656 Lisp_Object *maps;
1657 int nmaps = current_minor_maps (0, &maps);
1659 return Flist (nmaps, maps);
1662 /* Help functions for describing and documenting keymaps. */
1665 static void
1666 accessible_keymaps_1 (key, cmd, maps, tail, thisseq, is_metized)
1667 Lisp_Object maps, tail, thisseq, key, cmd;
1668 int is_metized; /* If 1, `key' is assumed to be INTEGERP. */
1670 Lisp_Object tem;
1672 cmd = get_keyelt (cmd, 0);
1673 if (NILP (cmd))
1674 return;
1676 tem = get_keymap (cmd, 0, 0);
1677 if (CONSP (tem))
1679 cmd = tem;
1680 /* Ignore keymaps that are already added to maps. */
1681 tem = Frassq (cmd, maps);
1682 if (NILP (tem))
1684 /* If the last key in thisseq is meta-prefix-char,
1685 turn it into a meta-ized keystroke. We know
1686 that the event we're about to append is an
1687 ascii keystroke since we're processing a
1688 keymap table. */
1689 if (is_metized)
1691 int meta_bit = meta_modifier;
1692 Lisp_Object last = make_number (XINT (Flength (thisseq)) - 1);
1693 tem = Fcopy_sequence (thisseq);
1695 Faset (tem, last, make_number (XINT (key) | meta_bit));
1697 /* This new sequence is the same length as
1698 thisseq, so stick it in the list right
1699 after this one. */
1700 XSETCDR (tail,
1701 Fcons (Fcons (tem, cmd), XCDR (tail)));
1703 else
1705 tem = append_key (thisseq, key);
1706 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
1712 static void
1713 accessible_keymaps_char_table (args, index, cmd)
1714 Lisp_Object args, index, cmd;
1716 accessible_keymaps_1 (index, cmd,
1717 XCAR (XCAR (args)),
1718 XCAR (XCDR (args)),
1719 XCDR (XCDR (args)),
1720 XINT (XCDR (XCAR (args))));
1723 /* This function cannot GC. */
1725 DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps,
1726 1, 2, 0,
1727 doc: /* Find all keymaps accessible via prefix characters from KEYMAP.
1728 Returns a list of elements of the form (KEYS . MAP), where the sequence
1729 KEYS starting from KEYMAP gets you to MAP. These elements are ordered
1730 so that the KEYS increase in length. The first element is ([] . KEYMAP).
1731 An optional argument PREFIX, if non-nil, should be a key sequence;
1732 then the value includes only maps for prefixes that start with PREFIX. */)
1733 (keymap, prefix)
1734 Lisp_Object keymap, prefix;
1736 Lisp_Object maps, good_maps, tail;
1737 int prefixlen = 0;
1739 /* no need for gcpro because we don't autoload any keymaps. */
1741 if (!NILP (prefix))
1742 prefixlen = XINT (Flength (prefix));
1744 if (!NILP (prefix))
1746 /* If a prefix was specified, start with the keymap (if any) for
1747 that prefix, so we don't waste time considering other prefixes. */
1748 Lisp_Object tem;
1749 tem = Flookup_key (keymap, prefix, Qt);
1750 /* Flookup_key may give us nil, or a number,
1751 if the prefix is not defined in this particular map.
1752 It might even give us a list that isn't a keymap. */
1753 tem = get_keymap (tem, 0, 0);
1754 if (CONSP (tem))
1756 /* Convert PREFIX to a vector now, so that later on
1757 we don't have to deal with the possibility of a string. */
1758 if (STRINGP (prefix))
1760 int i, i_byte, c;
1761 Lisp_Object copy;
1763 copy = Fmake_vector (make_number (XSTRING (prefix)->size), Qnil);
1764 for (i = 0, i_byte = 0; i < XSTRING (prefix)->size;)
1766 int i_before = i;
1768 FETCH_STRING_CHAR_ADVANCE (c, prefix, i, i_byte);
1769 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
1770 c ^= 0200 | meta_modifier;
1771 ASET (copy, i_before, make_number (c));
1773 prefix = copy;
1775 maps = Fcons (Fcons (prefix, tem), Qnil);
1777 else
1778 return Qnil;
1780 else
1781 maps = Fcons (Fcons (Fmake_vector (make_number (0), Qnil),
1782 get_keymap (keymap, 1, 0)),
1783 Qnil);
1785 /* For each map in the list maps,
1786 look at any other maps it points to,
1787 and stick them at the end if they are not already in the list.
1789 This is a breadth-first traversal, where tail is the queue of
1790 nodes, and maps accumulates a list of all nodes visited. */
1792 for (tail = maps; CONSP (tail); tail = XCDR (tail))
1794 register Lisp_Object thisseq, thismap;
1795 Lisp_Object last;
1796 /* Does the current sequence end in the meta-prefix-char? */
1797 int is_metized;
1799 thisseq = Fcar (Fcar (tail));
1800 thismap = Fcdr (Fcar (tail));
1801 last = make_number (XINT (Flength (thisseq)) - 1);
1802 is_metized = (XINT (last) >= 0
1803 /* Don't metize the last char of PREFIX. */
1804 && XINT (last) >= prefixlen
1805 && EQ (Faref (thisseq, last), meta_prefix_char));
1807 for (; CONSP (thismap); thismap = XCDR (thismap))
1809 Lisp_Object elt;
1811 elt = XCAR (thismap);
1813 QUIT;
1815 if (CHAR_TABLE_P (elt))
1817 Lisp_Object indices[3];
1819 map_char_table (accessible_keymaps_char_table, Qnil,
1820 elt, Fcons (Fcons (maps, make_number (is_metized)),
1821 Fcons (tail, thisseq)),
1822 0, indices);
1824 else if (VECTORP (elt))
1826 register int i;
1828 /* Vector keymap. Scan all the elements. */
1829 for (i = 0; i < ASIZE (elt); i++)
1830 accessible_keymaps_1 (make_number (i), AREF (elt, i),
1831 maps, tail, thisseq, is_metized);
1834 else if (CONSP (elt))
1835 accessible_keymaps_1 (XCAR (elt), XCDR (elt),
1836 maps, tail, thisseq,
1837 is_metized && INTEGERP (XCAR (elt)));
1842 if (NILP (prefix))
1843 return maps;
1845 /* Now find just the maps whose access prefixes start with PREFIX. */
1847 good_maps = Qnil;
1848 for (; CONSP (maps); maps = XCDR (maps))
1850 Lisp_Object elt, thisseq;
1851 elt = XCAR (maps);
1852 thisseq = XCAR (elt);
1853 /* The access prefix must be at least as long as PREFIX,
1854 and the first elements must match those of PREFIX. */
1855 if (XINT (Flength (thisseq)) >= prefixlen)
1857 int i;
1858 for (i = 0; i < prefixlen; i++)
1860 Lisp_Object i1;
1861 XSETFASTINT (i1, i);
1862 if (!EQ (Faref (thisseq, i1), Faref (prefix, i1)))
1863 break;
1865 if (i == prefixlen)
1866 good_maps = Fcons (elt, good_maps);
1870 return Fnreverse (good_maps);
1873 Lisp_Object Qsingle_key_description, Qkey_description;
1875 /* This function cannot GC. */
1877 DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0,
1878 doc: /* Return a pretty description of key-sequence KEYS.
1879 Control characters turn into "C-foo" sequences, meta into "M-foo"
1880 spaces are put between sequence elements, etc. */)
1881 (keys)
1882 Lisp_Object keys;
1884 int len = 0;
1885 int i, i_byte;
1886 Lisp_Object sep;
1887 Lisp_Object *args = NULL;
1889 if (STRINGP (keys))
1891 Lisp_Object vector;
1892 vector = Fmake_vector (Flength (keys), Qnil);
1893 for (i = 0, i_byte = 0; i < XSTRING (keys)->size; )
1895 int c;
1896 int i_before = i;
1898 FETCH_STRING_CHAR_ADVANCE (c, keys, i, i_byte);
1899 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
1900 c ^= 0200 | meta_modifier;
1901 XSETFASTINT (AREF (vector, i_before), c);
1903 keys = vector;
1906 if (VECTORP (keys))
1908 /* In effect, this computes
1909 (mapconcat 'single-key-description keys " ")
1910 but we shouldn't use mapconcat because it can do GC. */
1912 len = XVECTOR (keys)->size;
1913 sep = build_string (" ");
1914 /* This has one extra element at the end that we don't pass to Fconcat. */
1915 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
1917 for (i = 0; i < len; i++)
1919 args[i * 2] = Fsingle_key_description (AREF (keys, i), Qnil);
1920 args[i * 2 + 1] = sep;
1923 else if (CONSP (keys))
1925 /* In effect, this computes
1926 (mapconcat 'single-key-description keys " ")
1927 but we shouldn't use mapconcat because it can do GC. */
1929 len = XFASTINT (Flength (keys));
1930 sep = build_string (" ");
1931 /* This has one extra element at the end that we don't pass to Fconcat. */
1932 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
1934 for (i = 0; i < len; i++)
1936 args[i * 2] = Fsingle_key_description (XCAR (keys), Qnil);
1937 args[i * 2 + 1] = sep;
1938 keys = XCDR (keys);
1941 else
1942 keys = wrong_type_argument (Qarrayp, keys);
1944 if (len == 0)
1945 return empty_string;
1946 return Fconcat (len * 2 - 1, args);
1949 char *
1950 push_key_description (c, p, force_multibyte)
1951 register unsigned int c;
1952 register char *p;
1953 int force_multibyte;
1955 unsigned c2;
1957 /* Clear all the meaningless bits above the meta bit. */
1958 c &= meta_modifier | ~ - meta_modifier;
1959 c2 = c & ~(alt_modifier | ctrl_modifier | hyper_modifier
1960 | meta_modifier | shift_modifier | super_modifier);
1962 if (c & alt_modifier)
1964 *p++ = 'A';
1965 *p++ = '-';
1966 c -= alt_modifier;
1968 if ((c & ctrl_modifier) != 0
1969 || (c2 < ' ' && c2 != 27 && c2 != '\t' && c2 != Ctl ('M')))
1971 *p++ = 'C';
1972 *p++ = '-';
1973 c &= ~ctrl_modifier;
1975 if (c & hyper_modifier)
1977 *p++ = 'H';
1978 *p++ = '-';
1979 c -= hyper_modifier;
1981 if (c & meta_modifier)
1983 *p++ = 'M';
1984 *p++ = '-';
1985 c -= meta_modifier;
1987 if (c & shift_modifier)
1989 *p++ = 'S';
1990 *p++ = '-';
1991 c -= shift_modifier;
1993 if (c & super_modifier)
1995 *p++ = 's';
1996 *p++ = '-';
1997 c -= super_modifier;
1999 if (c < 040)
2001 if (c == 033)
2003 *p++ = 'E';
2004 *p++ = 'S';
2005 *p++ = 'C';
2007 else if (c == '\t')
2009 *p++ = 'T';
2010 *p++ = 'A';
2011 *p++ = 'B';
2013 else if (c == Ctl ('M'))
2015 *p++ = 'R';
2016 *p++ = 'E';
2017 *p++ = 'T';
2019 else
2021 /* `C-' already added above. */
2022 if (c > 0 && c <= Ctl ('Z'))
2023 *p++ = c + 0140;
2024 else
2025 *p++ = c + 0100;
2028 else if (c == 0177)
2030 *p++ = 'D';
2031 *p++ = 'E';
2032 *p++ = 'L';
2034 else if (c == ' ')
2036 *p++ = 'S';
2037 *p++ = 'P';
2038 *p++ = 'C';
2040 else if (c < 128
2041 || (NILP (current_buffer->enable_multibyte_characters)
2042 && SINGLE_BYTE_CHAR_P (c)
2043 && !force_multibyte))
2045 *p++ = c;
2047 else if (CHAR_VALID_P (c, 0))
2049 if (NILP (current_buffer->enable_multibyte_characters))
2050 *p++ = multibyte_char_to_unibyte (c, Qnil);
2051 else
2052 p += CHAR_STRING (c, (unsigned char *) p);
2054 else
2056 int bit_offset;
2057 *p++ = '\\';
2058 /* The biggest character code uses 22 bits. */
2059 for (bit_offset = 21; bit_offset >= 0; bit_offset -= 3)
2061 if (c >= (1 << bit_offset))
2062 *p++ = ((c & (7 << bit_offset)) >> bit_offset) + '0';
2066 return p;
2069 /* This function cannot GC. */
2071 DEFUN ("single-key-description", Fsingle_key_description,
2072 Ssingle_key_description, 1, 2, 0,
2073 doc: /* Return a pretty description of command character KEY.
2074 Control characters turn into C-whatever, etc.
2075 Optional argument NO-ANGLES non-nil means don't put angle brackets
2076 around function keys and event symbols. */)
2077 (key, no_angles)
2078 Lisp_Object key, no_angles;
2080 if (CONSP (key) && lucid_event_type_list_p (key))
2081 key = Fevent_convert_list (key);
2083 key = EVENT_HEAD (key);
2085 if (INTEGERP (key)) /* Normal character */
2087 char tem[KEY_DESCRIPTION_SIZE];
2089 *push_key_description (XUINT (key), tem, 1) = 0;
2090 return build_string (tem);
2092 else if (SYMBOLP (key)) /* Function key or event-symbol */
2094 if (NILP (no_angles))
2096 char *buffer
2097 = (char *) alloca (STRING_BYTES (XSYMBOL (key)->name) + 5);
2098 sprintf (buffer, "<%s>", XSYMBOL (key)->name->data);
2099 return build_string (buffer);
2101 else
2102 return Fsymbol_name (key);
2104 else if (STRINGP (key)) /* Buffer names in the menubar. */
2105 return Fcopy_sequence (key);
2106 else
2107 error ("KEY must be an integer, cons, symbol, or string");
2108 return Qnil;
2111 char *
2112 push_text_char_description (c, p)
2113 register unsigned int c;
2114 register char *p;
2116 if (c >= 0200)
2118 *p++ = 'M';
2119 *p++ = '-';
2120 c -= 0200;
2122 if (c < 040)
2124 *p++ = '^';
2125 *p++ = c + 64; /* 'A' - 1 */
2127 else if (c == 0177)
2129 *p++ = '^';
2130 *p++ = '?';
2132 else
2133 *p++ = c;
2134 return p;
2137 /* This function cannot GC. */
2139 DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0,
2140 doc: /* Return a pretty description of file-character CHARACTER.
2141 Control characters turn into "^char", etc. */)
2142 (character)
2143 Lisp_Object character;
2145 /* Currently MAX_MULTIBYTE_LENGTH is 4 (< 6). */
2146 unsigned char str[6];
2147 int c;
2149 CHECK_NUMBER (character);
2151 c = XINT (character);
2152 if (!SINGLE_BYTE_CHAR_P (c))
2154 int len = CHAR_STRING (c, str);
2156 return make_multibyte_string (str, 1, len);
2159 *push_text_char_description (c & 0377, str) = 0;
2161 return build_string (str);
2164 /* Return non-zero if SEQ contains only ASCII characters, perhaps with
2165 a meta bit. */
2166 static int
2167 ascii_sequence_p (seq)
2168 Lisp_Object seq;
2170 int i;
2171 int len = XINT (Flength (seq));
2173 for (i = 0; i < len; i++)
2175 Lisp_Object ii, elt;
2177 XSETFASTINT (ii, i);
2178 elt = Faref (seq, ii);
2180 if (!INTEGERP (elt)
2181 || (XUINT (elt) & ~CHAR_META) >= 0x80)
2182 return 0;
2185 return 1;
2189 /* where-is - finding a command in a set of keymaps. */
2191 static Lisp_Object where_is_internal ();
2192 static Lisp_Object where_is_internal_1 ();
2193 static void where_is_internal_2 ();
2195 /* Like Flookup_key, but uses a list of keymaps SHADOW instead of a single map.
2196 Returns the first non-nil binding found in any of those maps. */
2198 static Lisp_Object
2199 shadow_lookup (shadow, key, flag)
2200 Lisp_Object shadow, key, flag;
2202 Lisp_Object tail, value;
2204 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2206 value = Flookup_key (XCAR (tail), key, flag);
2207 if (!NILP (value) && !NATNUMP (value))
2208 return value;
2210 return Qnil;
2213 /* This function can GC if Flookup_key autoloads any keymaps. */
2215 static Lisp_Object
2216 where_is_internal (definition, keymaps, firstonly, noindirect, no_remap)
2217 Lisp_Object definition, keymaps;
2218 Lisp_Object firstonly, noindirect, no_remap;
2220 Lisp_Object maps = Qnil;
2221 Lisp_Object found, sequences;
2222 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
2223 /* 1 means ignore all menu bindings entirely. */
2224 int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii);
2226 /* If this command is remapped, then it has no key bindings
2227 of its own. */
2228 if (NILP (no_remap) && SYMBOLP (definition))
2230 Lisp_Object tem;
2231 if (tem = Fremap_command (definition), !NILP (tem))
2232 return Qnil;
2235 found = keymaps;
2236 while (CONSP (found))
2238 maps =
2239 nconc2 (maps,
2240 Faccessible_keymaps (get_keymap (XCAR (found), 1, 0), Qnil));
2241 found = XCDR (found);
2244 GCPRO5 (definition, keymaps, maps, found, sequences);
2245 found = Qnil;
2246 sequences = Qnil;
2248 for (; !NILP (maps); maps = Fcdr (maps))
2250 /* Key sequence to reach map, and the map that it reaches */
2251 register Lisp_Object this, map;
2253 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
2254 [M-CHAR] sequences, check if last character of the sequence
2255 is the meta-prefix char. */
2256 Lisp_Object last;
2257 int last_is_meta;
2259 this = Fcar (Fcar (maps));
2260 map = Fcdr (Fcar (maps));
2261 last = make_number (XINT (Flength (this)) - 1);
2262 last_is_meta = (XINT (last) >= 0
2263 && EQ (Faref (this, last), meta_prefix_char));
2265 /* if (nomenus && !ascii_sequence_p (this)) */
2266 if (nomenus && XINT (last) >= 0
2267 && !INTEGERP (Faref (this, make_number (0))))
2268 /* If no menu entries should be returned, skip over the
2269 keymaps bound to `menu-bar' and `tool-bar' and other
2270 non-ascii prefixes like `C-down-mouse-2'. */
2271 continue;
2273 QUIT;
2275 while (CONSP (map))
2277 /* Because the code we want to run on each binding is rather
2278 large, we don't want to have two separate loop bodies for
2279 sparse keymap bindings and tables; we want to iterate one
2280 loop body over both keymap and vector bindings.
2282 For this reason, if Fcar (map) is a vector, we don't
2283 advance map to the next element until i indicates that we
2284 have finished off the vector. */
2285 Lisp_Object elt, key, binding;
2286 elt = XCAR (map);
2287 map = XCDR (map);
2289 sequences = Qnil;
2291 QUIT;
2293 /* Set key and binding to the current key and binding, and
2294 advance map and i to the next binding. */
2295 if (VECTORP (elt))
2297 Lisp_Object sequence;
2298 int i;
2299 /* In a vector, look at each element. */
2300 for (i = 0; i < XVECTOR (elt)->size; i++)
2302 binding = AREF (elt, i);
2303 XSETFASTINT (key, i);
2304 sequence = where_is_internal_1 (binding, key, definition,
2305 noindirect, this,
2306 last, nomenus, last_is_meta);
2307 if (!NILP (sequence))
2308 sequences = Fcons (sequence, sequences);
2311 else if (CHAR_TABLE_P (elt))
2313 Lisp_Object indices[3];
2314 Lisp_Object args;
2316 args = Fcons (Fcons (Fcons (definition, noindirect),
2317 Qnil), /* Result accumulator. */
2318 Fcons (Fcons (this, last),
2319 Fcons (make_number (nomenus),
2320 make_number (last_is_meta))));
2321 map_char_table (where_is_internal_2, Qnil, elt, args,
2322 0, indices);
2323 sequences = XCDR (XCAR (args));
2325 else if (CONSP (elt))
2327 Lisp_Object sequence;
2329 key = XCAR (elt);
2330 binding = XCDR (elt);
2332 sequence = where_is_internal_1 (binding, key, definition,
2333 noindirect, this,
2334 last, nomenus, last_is_meta);
2335 if (!NILP (sequence))
2336 sequences = Fcons (sequence, sequences);
2340 while (!NILP (sequences))
2342 Lisp_Object sequence, remapped, function;
2344 sequence = XCAR (sequences);
2345 sequences = XCDR (sequences);
2347 /* If the current sequence is a command remapping with
2348 format [remap COMMAND], find the key sequences
2349 which run COMMAND, and use those sequences instead. */
2350 remapped = Qnil;
2351 if (NILP (no_remap)
2352 && VECTORP (sequence) && XVECTOR (sequence)->size == 2
2353 && EQ (AREF (sequence, 0), Qremap)
2354 && (function = AREF (sequence, 1), SYMBOLP (function)))
2356 Lisp_Object remapped1;
2358 remapped1 = where_is_internal (function, keymaps, firstonly, noindirect, Qt);
2359 if (CONSP (remapped1))
2361 /* Verify that this key binding actually maps to the
2362 remapped command (see below). */
2363 if (!EQ (shadow_lookup (keymaps, XCAR (remapped1), Qnil), function))
2364 continue;
2365 sequence = XCAR (remapped1);
2366 remapped = XCDR (remapped1);
2367 goto record_sequence;
2371 /* Verify that this key binding is not shadowed by another
2372 binding for the same key, before we say it exists.
2374 Mechanism: look for local definition of this key and if
2375 it is defined and does not match what we found then
2376 ignore this key.
2378 Either nil or number as value from Flookup_key
2379 means undefined. */
2380 if (!EQ (shadow_lookup (keymaps, sequence, Qnil), definition))
2381 continue;
2383 record_sequence:
2384 /* It is a true unshadowed match. Record it, unless it's already
2385 been seen (as could happen when inheriting keymaps). */
2386 if (NILP (Fmember (sequence, found)))
2387 found = Fcons (sequence, found);
2389 /* If firstonly is Qnon_ascii, then we can return the first
2390 binding we find. If firstonly is not Qnon_ascii but not
2391 nil, then we should return the first ascii-only binding
2392 we find. */
2393 if (EQ (firstonly, Qnon_ascii))
2394 RETURN_UNGCPRO (sequence);
2395 else if (!NILP (firstonly) && ascii_sequence_p (sequence))
2396 RETURN_UNGCPRO (sequence);
2398 if (CONSP (remapped))
2400 sequence = XCAR (remapped);
2401 remapped = XCDR (remapped);
2402 goto record_sequence;
2408 UNGCPRO;
2410 found = Fnreverse (found);
2412 /* firstonly may have been t, but we may have gone all the way through
2413 the keymaps without finding an all-ASCII key sequence. So just
2414 return the best we could find. */
2415 if (!NILP (firstonly))
2416 return Fcar (found);
2418 return found;
2421 DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 5, 0,
2422 doc: /* Return list of keys that invoke DEFINITION.
2423 If KEYMAP is non-nil, search only KEYMAP and the global keymap.
2424 If KEYMAP is nil, search all the currently active keymaps.
2425 If KEYMAP is a list of keymaps, search only those keymaps.
2427 If optional 3rd arg FIRSTONLY is non-nil, return the first key sequence found,
2428 rather than a list of all possible key sequences.
2429 If FIRSTONLY is the symbol `non-ascii', return the first binding found,
2430 no matter what it is.
2431 If FIRSTONLY has another non-nil value, prefer sequences of ASCII characters,
2432 and entirely reject menu bindings.
2434 If optional 4th arg NOINDIRECT is non-nil, don't follow indirections
2435 to other keymaps or slots. This makes it possible to search for an
2436 indirect definition itself.
2438 If optional 5th arg NO-REMAP is non-nil, don't search for key sequences
2439 that invoke a command which is remapped to DEFINITION, but include the
2440 remapped command in the returned list. */)
2441 (definition, keymap, firstonly, noindirect, no_remap)
2442 Lisp_Object definition, keymap;
2443 Lisp_Object firstonly, noindirect, no_remap;
2445 Lisp_Object sequences, keymaps;
2446 /* 1 means ignore all menu bindings entirely. */
2447 int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii);
2448 Lisp_Object result;
2450 /* Find the relevant keymaps. */
2451 if (CONSP (keymap) && KEYMAPP (XCAR (keymap)))
2452 keymaps = keymap;
2453 else if (!NILP (keymap))
2454 keymaps = Fcons (keymap, Fcons (current_global_map, Qnil));
2455 else
2456 keymaps = Fcurrent_active_maps (Qnil);
2458 /* Only use caching for the menubar (i.e. called with (def nil t nil).
2459 We don't really need to check `keymap'. */
2460 if (nomenus && NILP (noindirect) && NILP (keymap))
2462 Lisp_Object *defns;
2463 int i, j, n;
2464 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
2466 /* Check heuristic-consistency of the cache. */
2467 if (NILP (Fequal (keymaps, where_is_cache_keymaps)))
2468 where_is_cache = Qnil;
2470 if (NILP (where_is_cache))
2472 /* We need to create the cache. */
2473 Lisp_Object args[2];
2474 where_is_cache = Fmake_hash_table (0, args);
2475 where_is_cache_keymaps = Qt;
2477 /* Fill in the cache. */
2478 GCPRO5 (definition, keymaps, firstonly, noindirect, no_remap);
2479 where_is_internal (definition, keymaps, firstonly, noindirect, no_remap);
2480 UNGCPRO;
2482 where_is_cache_keymaps = keymaps;
2485 /* We want to process definitions from the last to the first.
2486 Instead of consing, copy definitions to a vector and step
2487 over that vector. */
2488 sequences = Fgethash (definition, where_is_cache, Qnil);
2489 n = XINT (Flength (sequences));
2490 defns = (Lisp_Object *) alloca (n * sizeof *defns);
2491 for (i = 0; CONSP (sequences); sequences = XCDR (sequences))
2492 defns[i++] = XCAR (sequences);
2494 /* Verify that the key bindings are not shadowed. Note that
2495 the following can GC. */
2496 GCPRO2 (definition, keymaps);
2497 result = Qnil;
2498 j = -1;
2499 for (i = n - 1; i >= 0; --i)
2500 if (EQ (shadow_lookup (keymaps, defns[i], Qnil), definition))
2502 if (ascii_sequence_p (defns[i]))
2503 break;
2504 else if (j < 0)
2505 j = i;
2508 result = i >= 0 ? defns[i] : (j >= 0 ? defns[j] : Qnil);
2509 UNGCPRO;
2511 else
2513 /* Kill the cache so that where_is_internal_1 doesn't think
2514 we're filling it up. */
2515 where_is_cache = Qnil;
2516 result = where_is_internal (definition, keymaps, firstonly, noindirect, no_remap);
2519 return result;
2522 /* This is the function that Fwhere_is_internal calls using map_char_table.
2523 ARGS has the form
2524 (((DEFINITION . NOINDIRECT) . (KEYMAP . RESULT))
2526 ((THIS . LAST) . (NOMENUS . LAST_IS_META)))
2527 Since map_char_table doesn't really use the return value from this function,
2528 we the result append to RESULT, the slot in ARGS.
2530 This function can GC because it calls where_is_internal_1 which can
2531 GC. */
2533 static void
2534 where_is_internal_2 (args, key, binding)
2535 Lisp_Object args, key, binding;
2537 Lisp_Object definition, noindirect, this, last;
2538 Lisp_Object result, sequence;
2539 int nomenus, last_is_meta;
2540 struct gcpro gcpro1, gcpro2, gcpro3;
2542 GCPRO3 (args, key, binding);
2543 result = XCDR (XCAR (args));
2544 definition = XCAR (XCAR (XCAR (args)));
2545 noindirect = XCDR (XCAR (XCAR (args)));
2546 this = XCAR (XCAR (XCDR (args)));
2547 last = XCDR (XCAR (XCDR (args)));
2548 nomenus = XFASTINT (XCAR (XCDR (XCDR (args))));
2549 last_is_meta = XFASTINT (XCDR (XCDR (XCDR (args))));
2551 sequence = where_is_internal_1 (binding, key, definition, noindirect,
2552 this, last, nomenus, last_is_meta);
2554 if (!NILP (sequence))
2555 XSETCDR (XCAR (args), Fcons (sequence, result));
2557 UNGCPRO;
2561 /* This function cannot GC. */
2563 static Lisp_Object
2564 where_is_internal_1 (binding, key, definition, noindirect, this, last,
2565 nomenus, last_is_meta)
2566 Lisp_Object binding, key, definition, noindirect, this, last;
2567 int nomenus, last_is_meta;
2569 Lisp_Object sequence;
2571 /* Search through indirections unless that's not wanted. */
2572 if (NILP (noindirect))
2573 binding = get_keyelt (binding, 0);
2575 /* End this iteration if this element does not match
2576 the target. */
2578 if (!(!NILP (where_is_cache) /* everything "matches" during cache-fill. */
2579 || EQ (binding, definition)
2580 || (CONSP (definition) && !NILP (Fequal (binding, definition)))))
2581 /* Doesn't match. */
2582 return Qnil;
2584 /* We have found a match. Construct the key sequence where we found it. */
2585 if (INTEGERP (key) && last_is_meta)
2587 sequence = Fcopy_sequence (this);
2588 Faset (sequence, last, make_number (XINT (key) | meta_modifier));
2590 else
2591 sequence = append_key (this, key);
2593 if (!NILP (where_is_cache))
2595 Lisp_Object sequences = Fgethash (binding, where_is_cache, Qnil);
2596 Fputhash (binding, Fcons (sequence, sequences), where_is_cache);
2597 return Qnil;
2599 else
2600 return sequence;
2603 /* describe-bindings - summarizing all the bindings in a set of keymaps. */
2605 DEFUN ("describe-buffer-bindings", Fdescribe_buffer_bindings, Sdescribe_buffer_bindings, 1, 3, 0,
2606 doc: /* Insert the list of all defined keys and their definitions.
2607 The list is inserted in the current buffer, while the bindings are
2608 looked up in BUFFER.
2609 The optional argument PREFIX, if non-nil, should be a key sequence;
2610 then we display only bindings that start with that prefix.
2611 The optional argument MENUS, if non-nil, says to mention menu bindings.
2612 \(Ordinarily these are omitted from the output.) */)
2613 (buffer, prefix, menus)
2614 Lisp_Object buffer, prefix, menus;
2616 Lisp_Object outbuf, shadow;
2617 int nomenu = NILP (menus);
2618 register Lisp_Object start1;
2619 struct gcpro gcpro1;
2621 char *alternate_heading
2622 = "\
2623 Keyboard translations:\n\n\
2624 You type Translation\n\
2625 -------- -----------\n";
2627 shadow = Qnil;
2628 GCPRO1 (shadow);
2630 outbuf = Fcurrent_buffer ();
2632 /* Report on alternates for keys. */
2633 if (STRINGP (Vkeyboard_translate_table) && !NILP (prefix))
2635 int c;
2636 unsigned char *translate = XSTRING (Vkeyboard_translate_table)->data;
2637 int translate_len = XSTRING (Vkeyboard_translate_table)->size;
2639 for (c = 0; c < translate_len; c++)
2640 if (translate[c] != c)
2642 char buf[KEY_DESCRIPTION_SIZE];
2643 char *bufend;
2645 if (alternate_heading)
2647 insert_string (alternate_heading);
2648 alternate_heading = 0;
2651 bufend = push_key_description (translate[c], buf, 1);
2652 insert (buf, bufend - buf);
2653 Findent_to (make_number (16), make_number (1));
2654 bufend = push_key_description (c, buf, 1);
2655 insert (buf, bufend - buf);
2657 insert ("\n", 1);
2660 insert ("\n", 1);
2663 if (!NILP (Vkey_translation_map))
2664 describe_map_tree (Vkey_translation_map, 0, Qnil, prefix,
2665 "Key translations", nomenu, 1, 0);
2668 /* Print the (major mode) local map. */
2669 start1 = Qnil;
2670 if (!NILP (current_kboard->Voverriding_terminal_local_map))
2671 start1 = current_kboard->Voverriding_terminal_local_map;
2672 else if (!NILP (Voverriding_local_map))
2673 start1 = Voverriding_local_map;
2675 if (!NILP (start1))
2677 describe_map_tree (start1, 1, shadow, prefix,
2678 "\f\nOverriding Bindings", nomenu, 0, 0);
2679 shadow = Fcons (start1, shadow);
2681 else
2683 /* Print the minor mode and major mode keymaps. */
2684 int i, nmaps;
2685 Lisp_Object *modes, *maps;
2687 /* Temporarily switch to `buffer', so that we can get that buffer's
2688 minor modes correctly. */
2689 Fset_buffer (buffer);
2691 nmaps = current_minor_maps (&modes, &maps);
2692 Fset_buffer (outbuf);
2694 start1 = get_local_map (BUF_PT (XBUFFER (buffer)),
2695 XBUFFER (buffer), Qkeymap);
2696 if (!NILP (start1))
2698 describe_map_tree (start1, 1, shadow, prefix,
2699 "\f\n`keymap' Property Bindings", nomenu, 0, 0);
2700 shadow = Fcons (start1, shadow);
2703 /* Print the minor mode maps. */
2704 for (i = 0; i < nmaps; i++)
2706 /* The title for a minor mode keymap
2707 is constructed at run time.
2708 We let describe_map_tree do the actual insertion
2709 because it takes care of other features when doing so. */
2710 char *title, *p;
2712 if (!SYMBOLP (modes[i]))
2713 abort();
2715 p = title = (char *) alloca (42 + XSYMBOL (modes[i])->name->size);
2716 *p++ = '\f';
2717 *p++ = '\n';
2718 *p++ = '`';
2719 bcopy (XSYMBOL (modes[i])->name->data, p,
2720 XSYMBOL (modes[i])->name->size);
2721 p += XSYMBOL (modes[i])->name->size;
2722 *p++ = '\'';
2723 bcopy (" Minor Mode Bindings", p, sizeof (" Minor Mode Bindings") - 1);
2724 p += sizeof (" Minor Mode Bindings") - 1;
2725 *p = 0;
2727 describe_map_tree (maps[i], 1, shadow, prefix, title, nomenu, 0, 0);
2728 shadow = Fcons (maps[i], shadow);
2731 start1 = get_local_map (BUF_PT (XBUFFER (buffer)),
2732 XBUFFER (buffer), Qlocal_map);
2733 if (!NILP (start1))
2735 if (EQ (start1, XBUFFER (buffer)->keymap))
2736 describe_map_tree (start1, 1, shadow, prefix,
2737 "\f\nMajor Mode Bindings", nomenu, 0, 0);
2738 else
2739 describe_map_tree (start1, 1, shadow, prefix,
2740 "\f\n`local-map' Property Bindings",
2741 nomenu, 0, 0);
2743 shadow = Fcons (start1, shadow);
2747 describe_map_tree (current_global_map, 1, shadow, prefix,
2748 "\f\nGlobal Bindings", nomenu, 0, 1);
2750 /* Print the function-key-map translations under this prefix. */
2751 if (!NILP (Vfunction_key_map))
2752 describe_map_tree (Vfunction_key_map, 0, Qnil, prefix,
2753 "\f\nFunction key map translations", nomenu, 1, 0);
2755 UNGCPRO;
2756 return Qnil;
2759 /* Insert a description of the key bindings in STARTMAP,
2760 followed by those of all maps reachable through STARTMAP.
2761 If PARTIAL is nonzero, omit certain "uninteresting" commands
2762 (such as `undefined').
2763 If SHADOW is non-nil, it is a list of maps;
2764 don't mention keys which would be shadowed by any of them.
2765 PREFIX, if non-nil, says mention only keys that start with PREFIX.
2766 TITLE, if not 0, is a string to insert at the beginning.
2767 TITLE should not end with a colon or a newline; we supply that.
2768 If NOMENU is not 0, then omit menu-bar commands.
2770 If TRANSL is nonzero, the definitions are actually key translations
2771 so print strings and vectors differently.
2773 If ALWAYS_TITLE is nonzero, print the title even if there are no maps
2774 to look through. */
2776 void
2777 describe_map_tree (startmap, partial, shadow, prefix, title, nomenu, transl,
2778 always_title)
2779 Lisp_Object startmap, shadow, prefix;
2780 int partial;
2781 char *title;
2782 int nomenu;
2783 int transl;
2784 int always_title;
2786 Lisp_Object maps, orig_maps, seen, sub_shadows;
2787 struct gcpro gcpro1, gcpro2, gcpro3;
2788 int something = 0;
2789 char *key_heading
2790 = "\
2791 key binding\n\
2792 --- -------\n";
2794 orig_maps = maps = Faccessible_keymaps (startmap, prefix);
2795 seen = Qnil;
2796 sub_shadows = Qnil;
2797 GCPRO3 (maps, seen, sub_shadows);
2799 if (nomenu)
2801 Lisp_Object list;
2803 /* Delete from MAPS each element that is for the menu bar. */
2804 for (list = maps; !NILP (list); list = XCDR (list))
2806 Lisp_Object elt, prefix, tem;
2808 elt = Fcar (list);
2809 prefix = Fcar (elt);
2810 if (XVECTOR (prefix)->size >= 1)
2812 tem = Faref (prefix, make_number (0));
2813 if (EQ (tem, Qmenu_bar))
2814 maps = Fdelq (elt, maps);
2819 if (!NILP (maps) || always_title)
2821 if (title)
2823 insert_string (title);
2824 if (!NILP (prefix))
2826 insert_string (" Starting With ");
2827 insert1 (Fkey_description (prefix));
2829 insert_string (":\n");
2831 insert_string (key_heading);
2832 something = 1;
2835 for (; !NILP (maps); maps = Fcdr (maps))
2837 register Lisp_Object elt, prefix, tail;
2839 elt = Fcar (maps);
2840 prefix = Fcar (elt);
2842 sub_shadows = Qnil;
2844 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2846 Lisp_Object shmap;
2848 shmap = XCAR (tail);
2850 /* If the sequence by which we reach this keymap is zero-length,
2851 then the shadow map for this keymap is just SHADOW. */
2852 if ((STRINGP (prefix) && XSTRING (prefix)->size == 0)
2853 || (VECTORP (prefix) && XVECTOR (prefix)->size == 0))
2855 /* If the sequence by which we reach this keymap actually has
2856 some elements, then the sequence's definition in SHADOW is
2857 what we should use. */
2858 else
2860 shmap = Flookup_key (shmap, Fcar (elt), Qt);
2861 if (INTEGERP (shmap))
2862 shmap = Qnil;
2865 /* If shmap is not nil and not a keymap,
2866 it completely shadows this map, so don't
2867 describe this map at all. */
2868 if (!NILP (shmap) && !KEYMAPP (shmap))
2869 goto skip;
2871 if (!NILP (shmap))
2872 sub_shadows = Fcons (shmap, sub_shadows);
2875 /* Maps we have already listed in this loop shadow this map. */
2876 for (tail = orig_maps; !EQ (tail, maps); tail = XCDR (tail))
2878 Lisp_Object tem;
2879 tem = Fequal (Fcar (XCAR (tail)), prefix);
2880 if (!NILP (tem))
2881 sub_shadows = Fcons (XCDR (XCAR (tail)), sub_shadows);
2884 describe_map (Fcdr (elt), prefix,
2885 transl ? describe_translation : describe_command,
2886 partial, sub_shadows, &seen, nomenu);
2888 skip: ;
2891 if (something)
2892 insert_string ("\n");
2894 UNGCPRO;
2897 static int previous_description_column;
2899 static void
2900 describe_command (definition, args)
2901 Lisp_Object definition, args;
2903 register Lisp_Object tem1;
2904 int column = current_column ();
2905 int description_column;
2907 /* If column 16 is no good, go to col 32;
2908 but don't push beyond that--go to next line instead. */
2909 if (column > 30)
2911 insert_char ('\n');
2912 description_column = 32;
2914 else if (column > 14 || (column > 10 && previous_description_column == 32))
2915 description_column = 32;
2916 else
2917 description_column = 16;
2919 Findent_to (make_number (description_column), make_number (1));
2920 previous_description_column = description_column;
2922 if (SYMBOLP (definition))
2924 XSETSTRING (tem1, XSYMBOL (definition)->name);
2925 insert1 (tem1);
2926 insert_string ("\n");
2928 else if (STRINGP (definition) || VECTORP (definition))
2929 insert_string ("Keyboard Macro\n");
2930 else if (KEYMAPP (definition))
2931 insert_string ("Prefix Command\n");
2932 else
2933 insert_string ("??\n");
2936 static void
2937 describe_translation (definition, args)
2938 Lisp_Object definition, args;
2940 register Lisp_Object tem1;
2942 Findent_to (make_number (16), make_number (1));
2944 if (SYMBOLP (definition))
2946 XSETSTRING (tem1, XSYMBOL (definition)->name);
2947 insert1 (tem1);
2948 insert_string ("\n");
2950 else if (STRINGP (definition) || VECTORP (definition))
2952 insert1 (Fkey_description (definition));
2953 insert_string ("\n");
2955 else if (KEYMAPP (definition))
2956 insert_string ("Prefix Command\n");
2957 else
2958 insert_string ("??\n");
2961 /* Describe the contents of map MAP, assuming that this map itself is
2962 reached by the sequence of prefix keys KEYS (a string or vector).
2963 PARTIAL, SHADOW, NOMENU are as in `describe_map_tree' above. */
2965 static void
2966 describe_map (map, keys, elt_describer, partial, shadow, seen, nomenu)
2967 register Lisp_Object map;
2968 Lisp_Object keys;
2969 void (*elt_describer) P_ ((Lisp_Object, Lisp_Object));
2970 int partial;
2971 Lisp_Object shadow;
2972 Lisp_Object *seen;
2973 int nomenu;
2975 Lisp_Object elt_prefix;
2976 Lisp_Object tail, definition, event;
2977 Lisp_Object tem;
2978 Lisp_Object suppress;
2979 Lisp_Object kludge;
2980 int first = 1;
2981 struct gcpro gcpro1, gcpro2, gcpro3;
2983 suppress = Qnil;
2985 if (!NILP (keys) && XFASTINT (Flength (keys)) > 0)
2987 /* Call Fkey_description first, to avoid GC bug for the other string. */
2988 tem = Fkey_description (keys);
2989 elt_prefix = concat2 (tem, build_string (" "));
2991 else
2992 elt_prefix = Qnil;
2994 if (partial)
2995 suppress = intern ("suppress-keymap");
2997 /* This vector gets used to present single keys to Flookup_key. Since
2998 that is done once per keymap element, we don't want to cons up a
2999 fresh vector every time. */
3000 kludge = Fmake_vector (make_number (1), Qnil);
3001 definition = Qnil;
3003 GCPRO3 (elt_prefix, definition, kludge);
3005 for (tail = map; CONSP (tail); tail = XCDR (tail))
3007 QUIT;
3009 if (VECTORP (XCAR (tail))
3010 || CHAR_TABLE_P (XCAR (tail)))
3011 describe_vector (XCAR (tail),
3012 elt_prefix, Qnil, elt_describer, partial, shadow, map,
3013 (int *)0, 0);
3014 else if (CONSP (XCAR (tail)))
3016 event = XCAR (XCAR (tail));
3018 /* Ignore bindings whose "keys" are not really valid events.
3019 (We get these in the frames and buffers menu.) */
3020 if (!(SYMBOLP (event) || INTEGERP (event)))
3021 continue;
3023 if (nomenu && EQ (event, Qmenu_bar))
3024 continue;
3026 definition = get_keyelt (XCDR (XCAR (tail)), 0);
3028 /* Don't show undefined commands or suppressed commands. */
3029 if (NILP (definition)) continue;
3030 if (SYMBOLP (definition) && partial)
3032 tem = Fget (definition, suppress);
3033 if (!NILP (tem))
3034 continue;
3037 /* Don't show a command that isn't really visible
3038 because a local definition of the same key shadows it. */
3040 ASET (kludge, 0, event);
3041 if (!NILP (shadow))
3043 tem = shadow_lookup (shadow, kludge, Qt);
3044 if (!NILP (tem)) continue;
3047 tem = Flookup_key (map, kludge, Qt);
3048 if (!EQ (tem, definition)) continue;
3050 if (first)
3052 previous_description_column = 0;
3053 insert ("\n", 1);
3054 first = 0;
3057 if (!NILP (elt_prefix))
3058 insert1 (elt_prefix);
3060 /* THIS gets the string to describe the character EVENT. */
3061 insert1 (Fsingle_key_description (event, Qnil));
3063 /* Print a description of the definition of this character.
3064 elt_describer will take care of spacing out far enough
3065 for alignment purposes. */
3066 (*elt_describer) (definition, Qnil);
3068 else if (EQ (XCAR (tail), Qkeymap))
3070 /* The same keymap might be in the structure twice, if we're
3071 using an inherited keymap. So skip anything we've already
3072 encountered. */
3073 tem = Fassq (tail, *seen);
3074 if (CONSP (tem) && !NILP (Fequal (XCAR (tem), keys)))
3075 break;
3076 *seen = Fcons (Fcons (tail, keys), *seen);
3080 UNGCPRO;
3083 static void
3084 describe_vector_princ (elt, fun)
3085 Lisp_Object elt, fun;
3087 Findent_to (make_number (16), make_number (1));
3088 call1 (fun, elt);
3089 Fterpri (Qnil);
3092 DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 2, 0,
3093 doc: /* Insert a description of contents of VECTOR.
3094 This is text showing the elements of vector matched against indices. */)
3095 (vector, describer)
3096 Lisp_Object vector, describer;
3098 int count = specpdl_ptr - specpdl;
3099 if (NILP (describer))
3100 describer = intern ("princ");
3101 specbind (Qstandard_output, Fcurrent_buffer ());
3102 CHECK_VECTOR_OR_CHAR_TABLE (vector);
3103 describe_vector (vector, Qnil, describer, describe_vector_princ, 0,
3104 Qnil, Qnil, (int *)0, 0);
3106 return unbind_to (count, Qnil);
3109 /* Insert in the current buffer a description of the contents of VECTOR.
3110 We call ELT_DESCRIBER to insert the description of one value found
3111 in VECTOR.
3113 ELT_PREFIX describes what "comes before" the keys or indices defined
3114 by this vector. This is a human-readable string whose size
3115 is not necessarily related to the situation.
3117 If the vector is in a keymap, ELT_PREFIX is a prefix key which
3118 leads to this keymap.
3120 If the vector is a chartable, ELT_PREFIX is the vector
3121 of bytes that lead to the character set or portion of a character
3122 set described by this chartable.
3124 If PARTIAL is nonzero, it means do not mention suppressed commands
3125 (that assumes the vector is in a keymap).
3127 SHADOW is a list of keymaps that shadow this map.
3128 If it is non-nil, then we look up the key in those maps
3129 and we don't mention it now if it is defined by any of them.
3131 ENTIRE_MAP is the keymap in which this vector appears.
3132 If the definition in effect in the whole map does not match
3133 the one in this vector, we ignore this one.
3135 ARGS is simply passed as the second argument to ELT_DESCRIBER.
3137 INDICES and CHAR_TABLE_DEPTH are ignored. They will be removed in
3138 the near future. */
3140 void
3141 describe_vector (vector, elt_prefix, args, elt_describer,
3142 partial, shadow, entire_map,
3143 indices, char_table_depth)
3144 register Lisp_Object vector;
3145 Lisp_Object elt_prefix, args;
3146 void (*elt_describer) P_ ((Lisp_Object, Lisp_Object));
3147 int partial;
3148 Lisp_Object shadow;
3149 Lisp_Object entire_map;
3150 int *indices;
3151 int char_table_depth;
3153 Lisp_Object definition;
3154 Lisp_Object tem2;
3155 register int i;
3156 Lisp_Object suppress;
3157 Lisp_Object kludge;
3158 struct gcpro gcpro1, gcpro2, gcpro3;
3159 /* Range of elements to be handled. */
3160 int from, to;
3161 Lisp_Object character;
3162 int starting_i;
3164 if (CHAR_TABLE_P (vector))
3166 describe_char_table (vector, elt_prefix, args, elt_describer,
3167 partial, shadow, entire_map);
3168 return;
3171 suppress = Qnil;
3173 definition = Qnil;
3175 /* This vector gets used to present single keys to Flookup_key. Since
3176 that is done once per vector element, we don't want to cons up a
3177 fresh vector every time. */
3178 kludge = Fmake_vector (make_number (1), Qnil);
3179 GCPRO3 (elt_prefix, definition, kludge);
3181 if (partial)
3182 suppress = intern ("suppress-keymap");
3184 from = 0;
3185 to = XVECTOR (vector)->size;
3187 for (i = from; i < to; i++)
3189 QUIT;
3191 definition = get_keyelt (AREF (vector, i), 0);
3193 if (NILP (definition)) continue;
3195 /* Don't mention suppressed commands. */
3196 if (SYMBOLP (definition) && partial)
3198 Lisp_Object tem;
3200 tem = Fget (definition, suppress);
3202 if (!NILP (tem)) continue;
3205 character = make_number (i);
3207 /* If this binding is shadowed by some other map, ignore it. */
3208 if (!NILP (shadow))
3210 Lisp_Object tem;
3212 ASET (kludge, 0, character);
3213 tem = shadow_lookup (shadow, kludge, Qt);
3215 if (!NILP (tem)) continue;
3218 /* Ignore this definition if it is shadowed by an earlier
3219 one in the same keymap. */
3220 if (!NILP (entire_map))
3222 Lisp_Object tem;
3224 ASET (kludge, 0, make_number (character));
3225 tem = Flookup_key (entire_map, kludge, Qt);
3227 if (!EQ (tem, definition))
3228 continue;
3231 /* Output the prefix that applies to every entry in this map. */
3232 if (!NILP (elt_prefix))
3233 insert1 (elt_prefix);
3235 insert1 (Fsingle_key_description (make_number (character), Qnil));
3237 starting_i = i;
3239 /* Find all consecutive characters or rows that have the same
3240 definition. But, for elements of a top level char table, if
3241 they are for charsets, we had better describe one by one even
3242 if they have the same definition. */
3243 while (i + 1 < to
3244 && (tem2 = get_keyelt (AREF (vector, i + 1), 0),
3245 !NILP (tem2))
3246 && !NILP (Fequal (tem2, definition)))
3247 i++;
3249 /* If we have a range of more than one character,
3250 print where the range reaches to. */
3252 if (i != starting_i)
3254 insert (" .. ", 4);
3256 if (!NILP (elt_prefix))
3257 insert1 (elt_prefix);
3258 insert1 (Fsingle_key_description (make_number (i), Qnil));
3261 /* Print a description of the definition of this character.
3262 elt_describer will take care of spacing out far enough
3263 for alignment purposes. */
3264 (*elt_describer) (definition, args);
3267 UNGCPRO;
3270 /* Insert in the current buffer a description of the contents of
3271 char-table TABLE. We call ELT_DESCRIBER to insert the description
3272 of one value found in TABLE.
3274 ELT_PREFIX describes what "comes before" the keys or indices defined
3275 by this vector. This is a human-readable string whose size
3276 is not necessarily related to the situation.
3278 If PARTIAL is nonzero, it means do not mention suppressed commands
3279 (that assumes the vector is in a keymap).
3281 SHADOW is a list of keymaps that shadow this map.
3282 If it is non-nil, then we look up the key in those maps
3283 and we don't mention it now if it is defined by any of them.
3285 ENTIRE_MAP is the keymap in which this vector appears.
3286 If the definition in effect in the whole map does not match
3287 the one in this vector, we ignore this one.
3289 ARGS is simply passed as the second argument to ELT_DESCRIBER. */
3291 void
3292 describe_char_table (table, elt_prefix, args, elt_describer,
3293 partial, shadow, entire_map)
3294 register Lisp_Object table;
3295 Lisp_Object args;
3296 Lisp_Object elt_prefix;
3297 void (*elt_describer) P_ ((Lisp_Object, Lisp_Object));
3298 int partial;
3299 Lisp_Object shadow;
3300 Lisp_Object entire_map;
3302 Lisp_Object definition;
3303 Lisp_Object tem2;
3304 register int i;
3305 Lisp_Object suppress;
3306 Lisp_Object kludge;
3307 struct gcpro gcpro1, gcpro2, gcpro3;
3308 /* Range of elements to be handled. */
3309 int from, to;
3310 int c;
3311 int starting_i;
3313 suppress = Qnil;
3315 definition = Qnil;
3317 /* This vector gets used to present single keys to Flookup_key. Since
3318 that is done once per vector element, we don't want to cons up a
3319 fresh vector every time. */
3320 kludge = Fmake_vector (make_number (1), Qnil);
3321 GCPRO3 (elt_prefix, definition, kludge);
3323 if (partial)
3324 suppress = intern ("suppress-keymap");
3326 from = 0;
3327 to = MAX_CHAR + 1;
3329 while (from < to)
3331 int range_beg, range_end;
3332 Lisp_Object val;
3334 QUIT;
3336 val = char_table_ref_and_range (table, from, &range_beg, &range_end);
3337 from = range_end + 1;
3338 definition = get_keyelt (val, 0);
3340 if (NILP (definition)) continue;
3342 /* Don't mention suppressed commands. */
3343 if (SYMBOLP (definition) && partial)
3345 Lisp_Object tem;
3347 tem = Fget (definition, suppress);
3349 if (!NILP (tem)) continue;
3352 /* Output the prefix that applies to every entry in this map. */
3353 if (!NILP (elt_prefix))
3354 insert1 (elt_prefix);
3356 starting_i = range_beg;
3357 insert_char (starting_i);
3359 /* Find all consecutive characters that have the same
3360 definition. */
3361 while (from < to
3362 && (val = char_table_ref_and_range (table, from,
3363 &range_beg, &range_end),
3364 tem2 = get_keyelt (val, 0),
3365 !NILP (tem2))
3366 && !NILP (Fequal (tem2, definition)))
3367 from = range_end + 1;
3369 /* If we have a range of more than one character,
3370 print where the range reaches to. */
3371 if (starting_i + 1 < from)
3373 insert (" .. ", 4);
3375 if (!NILP (elt_prefix))
3376 insert1 (elt_prefix);
3378 insert_char (from - 1);
3381 /* Print a description of the definition of this character.
3382 elt_describer will take care of spacing out far enough
3383 for alignment purposes. */
3384 (*elt_describer) (definition, args);
3387 UNGCPRO;
3391 /* Apropos - finding all symbols whose names match a regexp. */
3392 Lisp_Object apropos_predicate;
3393 Lisp_Object apropos_accumulate;
3395 static void
3396 apropos_accum (symbol, string)
3397 Lisp_Object symbol, string;
3399 register Lisp_Object tem;
3401 tem = Fstring_match (string, Fsymbol_name (symbol), Qnil);
3402 if (!NILP (tem) && !NILP (apropos_predicate))
3403 tem = call1 (apropos_predicate, symbol);
3404 if (!NILP (tem))
3405 apropos_accumulate = Fcons (symbol, apropos_accumulate);
3408 DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0,
3409 doc: /* Show all symbols whose names contain match for REGEXP.
3410 If optional 2nd arg PREDICATE is non-nil, (funcall PREDICATE SYMBOL) is done
3411 for each symbol and a symbol is mentioned only if that returns non-nil.
3412 Return list of symbols found. */)
3413 (regexp, predicate)
3414 Lisp_Object regexp, predicate;
3416 struct gcpro gcpro1, gcpro2;
3417 CHECK_STRING (regexp);
3418 apropos_predicate = predicate;
3419 GCPRO2 (apropos_predicate, apropos_accumulate);
3420 apropos_accumulate = Qnil;
3421 map_obarray (Vobarray, apropos_accum, regexp);
3422 apropos_accumulate = Fsort (apropos_accumulate, Qstring_lessp);
3423 UNGCPRO;
3424 return apropos_accumulate;
3427 void
3428 syms_of_keymap ()
3430 Qkeymap = intern ("keymap");
3431 staticpro (&Qkeymap);
3433 /* Now we are ready to set up this property, so we can
3434 create char tables. */
3435 Fput (Qkeymap, Qchar_table_extra_slots, make_number (0));
3437 /* Initialize the keymaps standardly used.
3438 Each one is the value of a Lisp variable, and is also
3439 pointed to by a C variable */
3441 global_map = Fmake_keymap (Qnil);
3442 Fset (intern ("global-map"), global_map);
3444 current_global_map = global_map;
3445 staticpro (&global_map);
3446 staticpro (&current_global_map);
3448 meta_map = Fmake_keymap (Qnil);
3449 Fset (intern ("esc-map"), meta_map);
3450 Ffset (intern ("ESC-prefix"), meta_map);
3452 control_x_map = Fmake_keymap (Qnil);
3453 Fset (intern ("ctl-x-map"), control_x_map);
3454 Ffset (intern ("Control-X-prefix"), control_x_map);
3456 exclude_keys
3457 = Fcons (Fcons (build_string ("DEL"), build_string ("\\d")),
3458 Fcons (Fcons (build_string ("TAB"), build_string ("\\t")),
3459 Fcons (Fcons (build_string ("RET"), build_string ("\\r")),
3460 Fcons (Fcons (build_string ("ESC"), build_string ("\\e")),
3461 Fcons (Fcons (build_string ("SPC"), build_string (" ")),
3462 Qnil)))));
3463 staticpro (&exclude_keys);
3465 DEFVAR_LISP ("define-key-rebound-commands", &Vdefine_key_rebound_commands,
3466 doc: /* List of commands given new key bindings recently.
3467 This is used for internal purposes during Emacs startup;
3468 don't alter it yourself. */);
3469 Vdefine_key_rebound_commands = Qt;
3471 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map,
3472 doc: /* Default keymap to use when reading from the minibuffer. */);
3473 Vminibuffer_local_map = Fmake_sparse_keymap (Qnil);
3475 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map,
3476 doc: /* Local keymap for the minibuffer when spaces are not allowed. */);
3477 Vminibuffer_local_ns_map = Fmake_sparse_keymap (Qnil);
3478 Fset_keymap_parent (Vminibuffer_local_ns_map, Vminibuffer_local_map);
3480 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map,
3481 doc: /* Local keymap for minibuffer input with completion. */);
3482 Vminibuffer_local_completion_map = Fmake_sparse_keymap (Qnil);
3483 Fset_keymap_parent (Vminibuffer_local_completion_map, Vminibuffer_local_map);
3485 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map,
3486 doc: /* Local keymap for minibuffer input with completion, for exact match. */);
3487 Vminibuffer_local_must_match_map = Fmake_sparse_keymap (Qnil);
3488 Fset_keymap_parent (Vminibuffer_local_must_match_map,
3489 Vminibuffer_local_completion_map);
3491 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist,
3492 doc: /* Alist of keymaps to use for minor modes.
3493 Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read
3494 key sequences and look up bindings iff VARIABLE's value is non-nil.
3495 If two active keymaps bind the same key, the keymap appearing earlier
3496 in the list takes precedence. */);
3497 Vminor_mode_map_alist = Qnil;
3499 DEFVAR_LISP ("minor-mode-overriding-map-alist", &Vminor_mode_overriding_map_alist,
3500 doc: /* Alist of keymaps to use for minor modes, in current major mode.
3501 This variable is a alist just like `minor-mode-map-alist', and it is
3502 used the same way (and before `minor-mode-map-alist'); however,
3503 it is provided for major modes to bind locally. */);
3504 Vminor_mode_overriding_map_alist = Qnil;
3506 DEFVAR_LISP ("function-key-map", &Vfunction_key_map,
3507 doc: /* Keymap mapping ASCII function key sequences onto their preferred forms.
3508 This allows Emacs to recognize function keys sent from ASCII
3509 terminals at any point in a key sequence.
3511 The `read-key-sequence' function replaces any subsequence bound by
3512 `function-key-map' with its binding. More precisely, when the active
3513 keymaps have no binding for the current key sequence but
3514 `function-key-map' binds a suffix of the sequence to a vector or string,
3515 `read-key-sequence' replaces the matching suffix with its binding, and
3516 continues with the new sequence.
3518 The events that come from bindings in `function-key-map' are not
3519 themselves looked up in `function-key-map'.
3521 For example, suppose `function-key-map' binds `ESC O P' to [f1].
3522 Typing `ESC O P' to `read-key-sequence' would return [f1]. Typing
3523 `C-x ESC O P' would return [?\\C-x f1]. If [f1] were a prefix
3524 key, typing `ESC O P x' would return [f1 x]. */);
3525 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
3527 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
3528 doc: /* Keymap of key translations that can override keymaps.
3529 This keymap works like `function-key-map', but comes after that,
3530 and applies even for keys that have ordinary bindings. */);
3531 Vkey_translation_map = Qnil;
3533 Qsingle_key_description = intern ("single-key-description");
3534 staticpro (&Qsingle_key_description);
3536 Qkey_description = intern ("key-description");
3537 staticpro (&Qkey_description);
3539 Qkeymapp = intern ("keymapp");
3540 staticpro (&Qkeymapp);
3542 Qnon_ascii = intern ("non-ascii");
3543 staticpro (&Qnon_ascii);
3545 Qmenu_item = intern ("menu-item");
3546 staticpro (&Qmenu_item);
3548 Qremap = intern ("remap");
3549 staticpro (&Qremap);
3551 remap_command_vector = Fmake_vector (make_number (2), Qremap);
3552 staticpro (&remap_command_vector);
3554 where_is_cache_keymaps = Qt;
3555 where_is_cache = Qnil;
3556 staticpro (&where_is_cache);
3557 staticpro (&where_is_cache_keymaps);
3559 defsubr (&Skeymapp);
3560 defsubr (&Skeymap_parent);
3561 defsubr (&Skeymap_prompt);
3562 defsubr (&Sset_keymap_parent);
3563 defsubr (&Smake_keymap);
3564 defsubr (&Smake_sparse_keymap);
3565 defsubr (&Scopy_keymap);
3566 defsubr (&Sremap_command);
3567 defsubr (&Skey_binding);
3568 defsubr (&Slocal_key_binding);
3569 defsubr (&Sglobal_key_binding);
3570 defsubr (&Sminor_mode_key_binding);
3571 defsubr (&Sdefine_key);
3572 defsubr (&Slookup_key);
3573 defsubr (&Sdefine_prefix_command);
3574 defsubr (&Suse_global_map);
3575 defsubr (&Suse_local_map);
3576 defsubr (&Scurrent_local_map);
3577 defsubr (&Scurrent_global_map);
3578 defsubr (&Scurrent_minor_mode_maps);
3579 defsubr (&Scurrent_active_maps);
3580 defsubr (&Saccessible_keymaps);
3581 defsubr (&Skey_description);
3582 defsubr (&Sdescribe_vector);
3583 defsubr (&Ssingle_key_description);
3584 defsubr (&Stext_char_description);
3585 defsubr (&Swhere_is_internal);
3586 defsubr (&Sdescribe_buffer_bindings);
3587 defsubr (&Sapropos_internal);
3590 void
3591 keys_of_keymap ()
3593 initial_define_key (global_map, 033, "ESC-prefix");
3594 initial_define_key (global_map, Ctl('X'), "Control-X-prefix");