added a check for nlist.h
[emacs.git] / src / keymap.c
blob018e1ea1eb9744fb89bd85d18700dcad4f015365
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 "charset.h"
29 #include "keyboard.h"
30 #include "termhooks.h"
31 #include "blockinput.h"
32 #include "puresize.h"
33 #include "intervals.h"
34 #include "keymap.h"
36 /* The number of elements in keymap vectors. */
37 #define DENSE_TABLE_SIZE (0200)
39 /* Actually allocate storage for these variables */
41 Lisp_Object current_global_map; /* Current global keymap */
43 Lisp_Object global_map; /* default global key bindings */
45 Lisp_Object meta_map; /* The keymap used for globally bound
46 ESC-prefixed default commands */
48 Lisp_Object control_x_map; /* The keymap used for globally bound
49 C-x-prefixed default commands */
51 /* was MinibufLocalMap */
52 Lisp_Object Vminibuffer_local_map;
53 /* The keymap used by the minibuf for local
54 bindings when spaces are allowed in the
55 minibuf */
57 /* was MinibufLocalNSMap */
58 Lisp_Object Vminibuffer_local_ns_map;
59 /* The keymap used by the minibuf for local
60 bindings when spaces are not encouraged
61 in the minibuf */
63 /* keymap used for minibuffers when doing completion */
64 /* was MinibufLocalCompletionMap */
65 Lisp_Object Vminibuffer_local_completion_map;
67 /* keymap used for minibuffers when doing completion and require a match */
68 /* was MinibufLocalMustMatchMap */
69 Lisp_Object Vminibuffer_local_must_match_map;
71 /* Alist of minor mode variables and keymaps. */
72 Lisp_Object Vminor_mode_map_alist;
74 /* Alist of major-mode-specific overrides for
75 minor mode variables and keymaps. */
76 Lisp_Object Vminor_mode_overriding_map_alist;
78 /* Keymap mapping ASCII function key sequences onto their preferred forms.
79 Initialized by the terminal-specific lisp files. See DEFVAR for more
80 documentation. */
81 Lisp_Object Vfunction_key_map;
83 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
84 Lisp_Object Vkey_translation_map;
86 /* A list of all commands given new bindings since a certain time
87 when nil was stored here.
88 This is used to speed up recomputation of menu key equivalents
89 when Emacs starts up. t means don't record anything here. */
90 Lisp_Object Vdefine_key_rebound_commands;
92 Lisp_Object Qkeymapp, Qkeymap, Qnon_ascii, Qmenu_item;
94 /* A char with the CHAR_META bit set in a vector or the 0200 bit set
95 in a string key sequence is equivalent to prefixing with this
96 character. */
97 extern Lisp_Object meta_prefix_char;
99 extern Lisp_Object Voverriding_local_map;
101 /* Hash table used to cache a reverse-map to speed up calls to where-is. */
102 static Lisp_Object where_is_cache;
103 /* Which keymaps are reverse-stored in the cache. */
104 static Lisp_Object where_is_cache_keymaps;
106 static Lisp_Object store_in_keymap P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
107 static void fix_submap_inheritance P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
109 static Lisp_Object define_as_prefix P_ ((Lisp_Object, Lisp_Object));
110 static void describe_command P_ ((Lisp_Object));
111 static void describe_translation P_ ((Lisp_Object));
112 static void describe_map P_ ((Lisp_Object, Lisp_Object,
113 void (*) P_ ((Lisp_Object)),
114 int, Lisp_Object, Lisp_Object*, int));
116 /* Keymap object support - constructors and predicates. */
118 DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 1, 0,
119 "Construct and return a new keymap, of the form (keymap CHARTABLE . ALIST).\n\
120 CHARTABLE is a char-table that holds the bindings for the ASCII\n\
121 characters. ALIST is an assoc-list which holds bindings for function keys,\n\
122 mouse events, and any other things that appear in the input stream.\n\
123 All entries in it are initially nil, meaning \"command undefined\".\n\n\
124 The optional arg STRING supplies a menu name for the keymap\n\
125 in case you use it as a menu with `x-popup-menu'.")
126 (string)
127 Lisp_Object string;
129 Lisp_Object tail;
130 if (!NILP (string))
131 tail = Fcons (string, Qnil);
132 else
133 tail = Qnil;
134 return Fcons (Qkeymap,
135 Fcons (Fmake_char_table (Qkeymap, Qnil), tail));
138 DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 1, 0,
139 "Construct and return a new sparse keymap.\n\
140 Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),\n\
141 which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),\n\
142 which binds the function key or mouse event SYMBOL to DEFINITION.\n\
143 Initially the alist is nil.\n\n\
144 The optional arg STRING supplies a menu name for the keymap\n\
145 in case you use it as a menu with `x-popup-menu'.")
146 (string)
147 Lisp_Object string;
149 if (!NILP (string))
150 return Fcons (Qkeymap, Fcons (string, Qnil));
151 return Fcons (Qkeymap, Qnil);
154 /* This function is used for installing the standard key bindings
155 at initialization time.
157 For example:
159 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); */
161 void
162 initial_define_key (keymap, key, defname)
163 Lisp_Object keymap;
164 int key;
165 char *defname;
167 store_in_keymap (keymap, make_number (key), intern (defname));
170 void
171 initial_define_lispy_key (keymap, keyname, defname)
172 Lisp_Object keymap;
173 char *keyname;
174 char *defname;
176 store_in_keymap (keymap, intern (keyname), intern (defname));
179 DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0,
180 "Return t if OBJECT is a keymap.\n\
182 A keymap is a list (keymap . ALIST),\n\
183 or a symbol whose function definition is itself a keymap.\n\
184 ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);\n\
185 a vector of densely packed bindings for small character codes\n\
186 is also allowed as an element.")
187 (object)
188 Lisp_Object object;
190 return (KEYMAPP (object) ? Qt : Qnil);
193 DEFUN ("keymap-prompt", Fkeymap_prompt, Skeymap_prompt, 1, 1, 0,
194 "Return the prompt-string of a keymap MAP.\n\
195 If non-nil, the prompt is shown in the echo-area\n\
196 when reading a key-sequence to be looked-up in this keymap.")
197 (map)
198 Lisp_Object map;
200 while (CONSP (map))
202 register Lisp_Object tem;
203 tem = Fcar (map);
204 if (STRINGP (tem))
205 return tem;
206 map = Fcdr (map);
208 return Qnil;
211 /* Check that OBJECT is a keymap (after dereferencing through any
212 symbols). If it is, return it.
214 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
215 is an autoload form, do the autoload and try again.
216 If AUTOLOAD is nonzero, callers must assume GC is possible.
218 If the map needs to be autoloaded, but AUTOLOAD is zero (and ERROR
219 is zero as well), return Qt.
221 ERROR controls how we respond if OBJECT isn't a keymap.
222 If ERROR is non-zero, signal an error; otherwise, just return Qnil.
224 Note that most of the time, we don't want to pursue autoloads.
225 Functions like Faccessible_keymaps which scan entire keymap trees
226 shouldn't load every autoloaded keymap. I'm not sure about this,
227 but it seems to me that only read_key_sequence, Flookup_key, and
228 Fdefine_key should cause keymaps to be autoloaded.
230 This function can GC when AUTOLOAD is non-zero, because it calls
231 do_autoload which can GC. */
233 Lisp_Object
234 get_keymap (object, error, autoload)
235 Lisp_Object object;
236 int error, autoload;
238 Lisp_Object tem;
240 autoload_retry:
241 if (NILP (object))
242 goto end;
243 if (CONSP (object) && EQ (XCAR (object), Qkeymap))
244 return object;
246 tem = indirect_function (object);
247 if (CONSP (tem))
249 if (EQ (XCAR (tem), Qkeymap))
250 return tem;
252 /* Should we do an autoload? Autoload forms for keymaps have
253 Qkeymap as their fifth element. */
254 if ((autoload || !error) && EQ (XCAR (tem), Qautoload))
256 Lisp_Object tail;
258 tail = Fnth (make_number (4), tem);
259 if (EQ (tail, Qkeymap))
261 if (autoload)
263 struct gcpro gcpro1, gcpro2;
265 GCPRO2 (tem, object);
266 do_autoload (tem, object);
267 UNGCPRO;
269 goto autoload_retry;
271 else
272 return Qt;
277 end:
278 if (error)
279 wrong_type_argument (Qkeymapp, object);
280 return Qnil;
283 /* Return the parent map of the keymap MAP, or nil if it has none.
284 We assume that MAP is a valid keymap. */
286 DEFUN ("keymap-parent", Fkeymap_parent, Skeymap_parent, 1, 1, 0,
287 "Return the parent keymap of KEYMAP.")
288 (keymap)
289 Lisp_Object keymap;
291 Lisp_Object list;
293 keymap = get_keymap (keymap, 1, 1);
295 /* Skip past the initial element `keymap'. */
296 list = XCDR (keymap);
297 for (; CONSP (list); list = XCDR (list))
299 /* See if there is another `keymap'. */
300 if (KEYMAPP (list))
301 return list;
304 return get_keymap (list, 0, 1);
308 /* Check whether MAP is one of MAPS parents. */
310 keymap_memberp (map, maps)
311 Lisp_Object map, maps;
313 if (NILP (map)) return 0;
314 while (KEYMAPP (maps) && !EQ (map, maps))
315 maps = Fkeymap_parent (maps);
316 return (EQ (map, maps));
319 /* Set the parent keymap of MAP to PARENT. */
321 DEFUN ("set-keymap-parent", Fset_keymap_parent, Sset_keymap_parent, 2, 2, 0,
322 "Modify KEYMAP to set its parent map to PARENT.\n\
323 PARENT should be nil or another keymap.")
324 (keymap, parent)
325 Lisp_Object keymap, parent;
327 Lisp_Object list, prev;
328 struct gcpro gcpro1;
329 int i;
331 /* Force a keymap flush for the next call to where-is.
332 Since this can be called from within where-is, we don't set where_is_cache
333 directly but only where_is_cache_keymaps, since where_is_cache shouldn't
334 be changed during where-is, while where_is_cache_keymaps is only used at
335 the very beginning of where-is and can thus be changed here without any
336 adverse effect.
337 This is a very minor correctness (rather than safety) issue. */
338 where_is_cache_keymaps = Qt;
340 keymap = get_keymap (keymap, 1, 1);
341 GCPRO1 (keymap);
343 if (!NILP (parent))
345 parent = get_keymap (parent, 1, 1);
347 /* Check for cycles. */
348 if (keymap_memberp (keymap, parent))
349 error ("Cyclic keymap inheritance");
352 /* Skip past the initial element `keymap'. */
353 prev = keymap;
354 while (1)
356 list = XCDR (prev);
357 /* If there is a parent keymap here, replace it.
358 If we came to the end, add the parent in PREV. */
359 if (!CONSP (list) || KEYMAPP (list))
361 /* If we already have the right parent, return now
362 so that we avoid the loops below. */
363 if (EQ (XCDR (prev), parent))
364 RETURN_UNGCPRO (parent);
366 XSETCDR (prev, parent);
367 break;
369 prev = list;
372 /* Scan through for submaps, and set their parents too. */
374 for (list = XCDR (keymap); CONSP (list); list = XCDR (list))
376 /* Stop the scan when we come to the parent. */
377 if (EQ (XCAR (list), Qkeymap))
378 break;
380 /* If this element holds a prefix map, deal with it. */
381 if (CONSP (XCAR (list))
382 && CONSP (XCDR (XCAR (list))))
383 fix_submap_inheritance (keymap, XCAR (XCAR (list)),
384 XCDR (XCAR (list)));
386 if (VECTORP (XCAR (list)))
387 for (i = 0; i < XVECTOR (XCAR (list))->size; i++)
388 if (CONSP (XVECTOR (XCAR (list))->contents[i]))
389 fix_submap_inheritance (keymap, make_number (i),
390 XVECTOR (XCAR (list))->contents[i]);
392 if (CHAR_TABLE_P (XCAR (list)))
394 Lisp_Object indices[3];
396 map_char_table (fix_submap_inheritance, Qnil, XCAR (list),
397 keymap, 0, indices);
401 RETURN_UNGCPRO (parent);
404 /* EVENT is defined in MAP as a prefix, and SUBMAP is its definition.
405 if EVENT is also a prefix in MAP's parent,
406 make sure that SUBMAP inherits that definition as its own parent. */
408 static void
409 fix_submap_inheritance (map, event, submap)
410 Lisp_Object map, event, submap;
412 Lisp_Object map_parent, parent_entry;
414 /* SUBMAP is a cons that we found as a key binding.
415 Discard the other things found in a menu key binding. */
417 submap = get_keymap (get_keyelt (submap, 0), 0, 0);
419 /* If it isn't a keymap now, there's no work to do. */
420 if (!CONSP (submap))
421 return;
423 map_parent = Fkeymap_parent (map);
424 if (!NILP (map_parent))
425 parent_entry =
426 get_keymap (access_keymap (map_parent, event, 0, 0, 0), 0, 0);
427 else
428 parent_entry = Qnil;
430 /* If MAP's parent has something other than a keymap,
431 our own submap shadows it completely. */
432 if (!CONSP (parent_entry))
433 return;
435 if (! EQ (parent_entry, submap))
437 Lisp_Object submap_parent;
438 submap_parent = submap;
439 while (1)
441 Lisp_Object tem;
443 tem = Fkeymap_parent (submap_parent);
445 if (KEYMAPP (tem))
447 if (keymap_memberp (tem, parent_entry))
448 /* Fset_keymap_parent could create a cycle. */
449 return;
450 submap_parent = tem;
452 else
453 break;
455 Fset_keymap_parent (submap_parent, parent_entry);
459 /* Look up IDX in MAP. IDX may be any sort of event.
460 Note that this does only one level of lookup; IDX must be a single
461 event, not a sequence.
463 If T_OK is non-zero, bindings for Qt are treated as default
464 bindings; any key left unmentioned by other tables and bindings is
465 given the binding of Qt.
467 If T_OK is zero, bindings for Qt are not treated specially.
469 If NOINHERIT, don't accept a subkeymap found in an inherited keymap. */
471 Lisp_Object
472 access_keymap (map, idx, t_ok, noinherit, autoload)
473 Lisp_Object map;
474 Lisp_Object idx;
475 int t_ok;
476 int noinherit;
477 int autoload;
479 Lisp_Object val = Qunbound;
481 /* If idx is a list (some sort of mouse click, perhaps?),
482 the index we want to use is the car of the list, which
483 ought to be a symbol. */
484 idx = EVENT_HEAD (idx);
486 /* If idx is a symbol, it might have modifiers, which need to
487 be put in the canonical order. */
488 if (SYMBOLP (idx))
489 idx = reorder_modifiers (idx);
490 else if (INTEGERP (idx))
491 /* Clobber the high bits that can be present on a machine
492 with more than 24 bits of integer. */
493 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
495 /* Handle the special meta -> esc mapping. */
496 if (INTEGERP (idx) && XUINT (idx) & meta_modifier)
498 /* See if there is a meta-map. If there's none, there is
499 no binding for IDX, unless a default binding exists in MAP. */
500 Lisp_Object meta_map =
501 get_keymap (access_keymap (map, meta_prefix_char,
502 t_ok, noinherit, autoload),
503 0, autoload);
504 if (CONSP (meta_map))
506 map = meta_map;
507 idx = make_number (XUINT (idx) & ~meta_modifier);
509 else if (t_ok)
510 /* Set IDX to t, so that we only find a default binding. */
511 idx = Qt;
512 else
513 /* We know there is no binding. */
514 return Qnil;
518 Lisp_Object tail;
519 Lisp_Object t_binding = Qnil;
521 /* If `t_ok' is 2, both `t' and generic-char bindings are accepted.
522 If it is 1, only generic-char bindings are accepted.
523 Otherwise, neither are. */
524 t_ok = t_ok ? 2 : 0;
526 for (tail = XCDR (map);
527 (CONSP (tail)
528 || (tail = get_keymap (tail, 0, autoload), CONSP (tail)));
529 tail = XCDR (tail))
531 Lisp_Object binding;
533 binding = XCAR (tail);
534 if (SYMBOLP (binding))
536 /* If NOINHERIT, stop finding prefix definitions
537 after we pass a second occurrence of the `keymap' symbol. */
538 if (noinherit && EQ (binding, Qkeymap))
539 return Qnil;
541 else if (CONSP (binding))
543 Lisp_Object key = XCAR (binding);
544 int c1, c2;
546 if (EQ (key, idx))
547 val = XCDR (binding);
548 else if (t_ok
549 && INTEGERP (idx)
550 && (XINT (idx) & CHAR_MODIFIER_MASK) == 0
551 && INTEGERP (key)
552 && (XINT (key) & CHAR_MODIFIER_MASK) == 0
553 && !SINGLE_BYTE_CHAR_P (XINT (idx))
554 && !SINGLE_BYTE_CHAR_P (XINT (key))
555 && CHAR_VALID_P (XINT (key), 1)
556 && !CHAR_VALID_P (XINT (key), 0)
557 && (CHAR_CHARSET (XINT (key))
558 == CHAR_CHARSET (XINT (idx))))
560 /* KEY is the generic character of the charset of IDX.
561 Use KEY's binding if there isn't a binding for IDX
562 itself. */
563 t_binding = XCDR (binding);
564 t_ok = 0;
566 else if (t_ok > 1 && EQ (key, Qt))
568 t_binding = XCDR (binding);
569 t_ok = 1;
572 else if (VECTORP (binding))
574 if (NATNUMP (idx) && XFASTINT (idx) < ASIZE (binding))
575 val = AREF (binding, XFASTINT (idx));
577 else if (CHAR_TABLE_P (binding))
579 /* Character codes with modifiers
580 are not included in a char-table.
581 All character codes without modifiers are included. */
582 if (NATNUMP (idx)
583 && (XFASTINT (idx) & CHAR_MODIFIER_MASK) == 0)
584 val = Faref (binding, idx);
587 /* If we found a binding, clean it up and return it. */
588 if (!EQ (val, Qunbound))
590 val = get_keyelt (val, autoload);
591 if (KEYMAPP (val))
592 fix_submap_inheritance (map, idx, val);
593 return val;
595 QUIT;
598 return get_keyelt (t_binding, autoload);
602 /* Given OBJECT which was found in a slot in a keymap,
603 trace indirect definitions to get the actual definition of that slot.
604 An indirect definition is a list of the form
605 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
606 and INDEX is the object to look up in KEYMAP to yield the definition.
608 Also if OBJECT has a menu string as the first element,
609 remove that. Also remove a menu help string as second element.
611 If AUTOLOAD is nonzero, load autoloadable keymaps
612 that are referred to with indirection. */
614 Lisp_Object
615 get_keyelt (object, autoload)
616 register Lisp_Object object;
617 int autoload;
619 while (1)
621 if (!(CONSP (object)))
622 /* This is really the value. */
623 return object;
625 /* If the keymap contents looks like (keymap ...) or (lambda ...)
626 then use itself. */
627 else if (EQ (XCAR (object), Qkeymap) || EQ (XCAR (object), Qlambda))
628 return object;
630 /* If the keymap contents looks like (menu-item name . DEFN)
631 or (menu-item name DEFN ...) then use DEFN.
632 This is a new format menu item. */
633 else if (EQ (XCAR (object), Qmenu_item))
635 if (CONSP (XCDR (object)))
637 Lisp_Object tem;
639 object = XCDR (XCDR (object));
640 tem = object;
641 if (CONSP (object))
642 object = XCAR (object);
644 /* If there's a `:filter FILTER', apply FILTER to the
645 menu-item's definition to get the real definition to
646 use. */
647 for (; CONSP (tem) && CONSP (XCDR (tem)); tem = XCDR (tem))
648 if (EQ (XCAR (tem), QCfilter) && autoload)
650 Lisp_Object filter;
651 filter = XCAR (XCDR (tem));
652 filter = list2 (filter, list2 (Qquote, object));
653 object = menu_item_eval_property (filter);
654 break;
657 else
658 /* Invalid keymap */
659 return object;
662 /* If the keymap contents looks like (STRING . DEFN), use DEFN.
663 Keymap alist elements like (CHAR MENUSTRING . DEFN)
664 will be used by HierarKey menus. */
665 else if (STRINGP (XCAR (object)))
667 object = XCDR (object);
668 /* Also remove a menu help string, if any,
669 following the menu item name. */
670 if (CONSP (object) && STRINGP (XCAR (object)))
671 object = XCDR (object);
672 /* Also remove the sublist that caches key equivalences, if any. */
673 if (CONSP (object) && CONSP (XCAR (object)))
675 Lisp_Object carcar;
676 carcar = XCAR (XCAR (object));
677 if (NILP (carcar) || VECTORP (carcar))
678 object = XCDR (object);
682 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
683 else
685 Lisp_Object map;
686 map = get_keymap (Fcar_safe (object), 0, autoload);
687 return (!CONSP (map) ? object /* Invalid keymap */
688 : access_keymap (map, Fcdr (object), 0, 0, autoload));
693 static Lisp_Object
694 store_in_keymap (keymap, idx, def)
695 Lisp_Object keymap;
696 register Lisp_Object idx;
697 register Lisp_Object def;
699 /* Flush any reverse-map cache. */
700 where_is_cache = Qnil;
701 where_is_cache_keymaps = Qt;
703 /* If we are preparing to dump, and DEF is a menu element
704 with a menu item indicator, copy it to ensure it is not pure. */
705 if (CONSP (def) && PURE_P (def)
706 && (EQ (XCAR (def), Qmenu_item) || STRINGP (XCAR (def))))
707 def = Fcons (XCAR (def), XCDR (def));
709 if (!CONSP (keymap) || !EQ (XCAR (keymap), Qkeymap))
710 error ("attempt to define a key in a non-keymap");
712 /* If idx is a list (some sort of mouse click, perhaps?),
713 the index we want to use is the car of the list, which
714 ought to be a symbol. */
715 idx = EVENT_HEAD (idx);
717 /* If idx is a symbol, it might have modifiers, which need to
718 be put in the canonical order. */
719 if (SYMBOLP (idx))
720 idx = reorder_modifiers (idx);
721 else if (INTEGERP (idx))
722 /* Clobber the high bits that can be present on a machine
723 with more than 24 bits of integer. */
724 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
726 /* Scan the keymap for a binding of idx. */
728 Lisp_Object tail;
730 /* The cons after which we should insert new bindings. If the
731 keymap has a table element, we record its position here, so new
732 bindings will go after it; this way, the table will stay
733 towards the front of the alist and character lookups in dense
734 keymaps will remain fast. Otherwise, this just points at the
735 front of the keymap. */
736 Lisp_Object insertion_point;
738 insertion_point = keymap;
739 for (tail = XCDR (keymap); CONSP (tail); tail = XCDR (tail))
741 Lisp_Object elt;
743 elt = XCAR (tail);
744 if (VECTORP (elt))
746 if (NATNUMP (idx) && XFASTINT (idx) < ASIZE (elt))
748 ASET (elt, XFASTINT (idx), def);
749 return def;
751 insertion_point = tail;
753 else if (CHAR_TABLE_P (elt))
755 /* Character codes with modifiers
756 are not included in a char-table.
757 All character codes without modifiers are included. */
758 if (NATNUMP (idx)
759 && ! (XFASTINT (idx)
760 & (CHAR_ALT | CHAR_SUPER | CHAR_HYPER
761 | CHAR_SHIFT | CHAR_CTL | CHAR_META)))
763 Faset (elt, idx, def);
764 return def;
766 insertion_point = tail;
768 else if (CONSP (elt))
770 if (EQ (idx, XCAR (elt)))
772 XSETCDR (elt, def);
773 return def;
776 else if (EQ (elt, Qkeymap))
777 /* If we find a 'keymap' symbol in the spine of KEYMAP,
778 then we must have found the start of a second keymap
779 being used as the tail of KEYMAP, and a binding for IDX
780 should be inserted before it. */
781 goto keymap_end;
783 QUIT;
786 keymap_end:
787 /* We have scanned the entire keymap, and not found a binding for
788 IDX. Let's add one. */
789 XSETCDR (insertion_point,
790 Fcons (Fcons (idx, def), XCDR (insertion_point)));
793 return def;
796 EXFUN (Fcopy_keymap, 1);
798 void
799 copy_keymap_1 (chartable, idx, elt)
800 Lisp_Object chartable, idx, elt;
802 if (CONSP (elt) && EQ (XCAR (elt), Qkeymap))
803 Faset (chartable, idx, Fcopy_keymap (elt));
806 DEFUN ("copy-keymap", Fcopy_keymap, Scopy_keymap, 1, 1, 0,
807 "Return a copy of the keymap KEYMAP.\n\
808 The copy starts out with the same definitions of KEYMAP,\n\
809 but changing either the copy or KEYMAP does not affect the other.\n\
810 Any key definitions that are subkeymaps are recursively copied.\n\
811 However, a key definition which is a symbol whose definition is a keymap\n\
812 is not copied.")
813 (keymap)
814 Lisp_Object keymap;
816 /* FIXME: This doesn't properly copy menu-items in vectors. */
817 /* FIXME: This also copies the parent keymap. */
819 register Lisp_Object copy, tail;
821 copy = Fcopy_alist (get_keymap (keymap, 1, 0));
823 for (tail = copy; CONSP (tail); tail = XCDR (tail))
825 Lisp_Object elt;
827 elt = XCAR (tail);
828 if (CHAR_TABLE_P (elt))
830 Lisp_Object indices[3];
832 elt = Fcopy_sequence (elt);
833 XSETCAR (tail, elt);
835 map_char_table (copy_keymap_1, Qnil, elt, elt, 0, indices);
837 else if (VECTORP (elt))
839 int i;
841 elt = Fcopy_sequence (elt);
842 XSETCAR (tail, elt);
844 for (i = 0; i < ASIZE (elt); i++)
845 if (CONSP (AREF (elt, i)) && EQ (XCAR (AREF (elt, i)), Qkeymap))
846 ASET (elt, i, Fcopy_keymap (AREF (elt, i)));
848 else if (CONSP (elt) && CONSP (XCDR (elt)))
850 Lisp_Object tem;
851 tem = XCDR (elt);
853 /* Is this a new format menu item. */
854 if (EQ (XCAR (tem),Qmenu_item))
856 /* Copy cell with menu-item marker. */
857 XSETCDR (elt,
858 Fcons (XCAR (tem), XCDR (tem)));
859 elt = XCDR (elt);
860 tem = XCDR (elt);
861 if (CONSP (tem))
863 /* Copy cell with menu-item name. */
864 XSETCDR (elt,
865 Fcons (XCAR (tem), XCDR (tem)));
866 elt = XCDR (elt);
867 tem = XCDR (elt);
869 if (CONSP (tem))
871 /* Copy cell with binding and if the binding is a keymap,
872 copy that. */
873 XSETCDR (elt,
874 Fcons (XCAR (tem), XCDR (tem)));
875 elt = XCDR (elt);
876 tem = XCAR (elt);
877 if (CONSP (tem) && EQ (XCAR (tem), Qkeymap))
878 XSETCAR (elt, Fcopy_keymap (tem));
879 tem = XCDR (elt);
880 if (CONSP (tem) && CONSP (XCAR (tem)))
881 /* Delete cache for key equivalences. */
882 XSETCDR (elt, XCDR (tem));
885 else
887 /* It may be an old fomat menu item.
888 Skip the optional menu string.
890 if (STRINGP (XCAR (tem)))
892 /* Copy the cell, since copy-alist didn't go this deep. */
893 XSETCDR (elt,
894 Fcons (XCAR (tem), XCDR (tem)));
895 elt = XCDR (elt);
896 tem = XCDR (elt);
897 /* Also skip the optional menu help string. */
898 if (CONSP (tem) && STRINGP (XCAR (tem)))
900 XSETCDR (elt,
901 Fcons (XCAR (tem), XCDR (tem)));
902 elt = XCDR (elt);
903 tem = XCDR (elt);
905 /* There may also be a list that caches key equivalences.
906 Just delete it for the new keymap. */
907 if (CONSP (tem)
908 && CONSP (XCAR (tem))
909 && (NILP (XCAR (XCAR (tem)))
910 || VECTORP (XCAR (XCAR (tem)))))
911 XSETCDR (elt, XCDR (tem));
913 if (CONSP (elt)
914 && CONSP (XCDR (elt))
915 && EQ (XCAR (XCDR (elt)), Qkeymap))
916 XSETCDR (elt, Fcopy_keymap (XCDR (elt)));
922 return copy;
925 /* Simple Keymap mutators and accessors. */
927 /* GC is possible in this function if it autoloads a keymap. */
929 DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0,
930 "Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.\n\
931 KEYMAP is a keymap. KEY is a string or a vector of symbols and characters\n\
932 meaning a sequence of keystrokes and events.\n\
933 Non-ASCII characters with codes above 127 (such as ISO Latin-1)\n\
934 can be included if you use a vector.\n\
935 DEF is anything that can be a key's definition:\n\
936 nil (means key is undefined in this keymap),\n\
937 a command (a Lisp function suitable for interactive calling)\n\
938 a string (treated as a keyboard macro),\n\
939 a keymap (to define a prefix key),\n\
940 a symbol. When the key is looked up, the symbol will stand for its\n\
941 function definition, which should at that time be one of the above,\n\
942 or another symbol whose function definition is used, etc.\n\
943 a cons (STRING . DEFN), meaning that DEFN is the definition\n\
944 (DEFN should be a valid definition in its own right),\n\
945 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.\n\
947 If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at\n\
948 the front of KEYMAP.")
949 (keymap, key, def)
950 Lisp_Object keymap;
951 Lisp_Object key;
952 Lisp_Object def;
954 register int idx;
955 register Lisp_Object c;
956 register Lisp_Object cmd;
957 int metized = 0;
958 int meta_bit;
959 int length;
960 struct gcpro gcpro1, gcpro2, gcpro3;
962 keymap = get_keymap (keymap, 1, 1);
964 if (!VECTORP (key) && !STRINGP (key))
965 key = wrong_type_argument (Qarrayp, key);
967 length = XFASTINT (Flength (key));
968 if (length == 0)
969 return Qnil;
971 if (SYMBOLP (def) && !EQ (Vdefine_key_rebound_commands, Qt))
972 Vdefine_key_rebound_commands = Fcons (def, Vdefine_key_rebound_commands);
974 GCPRO3 (keymap, key, def);
976 if (VECTORP (key))
977 meta_bit = meta_modifier;
978 else
979 meta_bit = 0x80;
981 idx = 0;
982 while (1)
984 c = Faref (key, make_number (idx));
986 if (CONSP (c) && lucid_event_type_list_p (c))
987 c = Fevent_convert_list (c);
989 if (INTEGERP (c)
990 && (XINT (c) & meta_bit)
991 && !metized)
993 c = meta_prefix_char;
994 metized = 1;
996 else
998 if (INTEGERP (c))
999 XSETINT (c, XINT (c) & ~meta_bit);
1001 metized = 0;
1002 idx++;
1005 if (!INTEGERP (c) && !SYMBOLP (c) && !CONSP (c))
1006 error ("Key sequence contains invalid events");
1008 if (idx == length)
1009 RETURN_UNGCPRO (store_in_keymap (keymap, c, def));
1011 cmd = access_keymap (keymap, c, 0, 1, 1);
1013 /* If this key is undefined, make it a prefix. */
1014 if (NILP (cmd))
1015 cmd = define_as_prefix (keymap, c);
1017 keymap = get_keymap (cmd, 0, 1);
1018 if (!CONSP (keymap))
1019 /* We must use Fkey_description rather than just passing key to
1020 error; key might be a vector, not a string. */
1021 error ("Key sequence %s uses invalid prefix characters",
1022 XSTRING (Fkey_description (key))->data);
1026 /* Value is number if KEY is too long; NIL if valid but has no definition. */
1027 /* GC is possible in this function if it autoloads a keymap. */
1029 DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 3, 0,
1030 "In keymap KEYMAP, look up key sequence KEY. Return the definition.\n\
1031 nil means undefined. See doc of `define-key' for kinds of definitions.\n\
1033 A number as value means KEY is \"too long\";\n\
1034 that is, characters or symbols in it except for the last one\n\
1035 fail to be a valid sequence of prefix characters in KEYMAP.\n\
1036 The number is how many characters at the front of KEY\n\
1037 it takes to reach a non-prefix command.\n\
1039 Normally, `lookup-key' ignores bindings for t, which act as default\n\
1040 bindings, used when nothing else in the keymap applies; this makes it\n\
1041 usable as a general function for probing keymaps. However, if the\n\
1042 third optional argument ACCEPT-DEFAULT is non-nil, `lookup-key' will\n\
1043 recognize the default bindings, just as `read-key-sequence' does.")
1044 (keymap, key, accept_default)
1045 register Lisp_Object keymap;
1046 Lisp_Object key;
1047 Lisp_Object accept_default;
1049 register int idx;
1050 register Lisp_Object cmd;
1051 register Lisp_Object c;
1052 int length;
1053 int t_ok = !NILP (accept_default);
1054 struct gcpro gcpro1;
1056 keymap = get_keymap (keymap, 1, 1);
1058 if (!VECTORP (key) && !STRINGP (key))
1059 key = wrong_type_argument (Qarrayp, key);
1061 length = XFASTINT (Flength (key));
1062 if (length == 0)
1063 return keymap;
1065 GCPRO1 (key);
1067 idx = 0;
1068 while (1)
1070 c = Faref (key, make_number (idx++));
1072 if (CONSP (c) && lucid_event_type_list_p (c))
1073 c = Fevent_convert_list (c);
1075 /* Turn the 8th bit of string chars into a meta modifier. */
1076 if (XINT (c) & 0x80 && STRINGP (key))
1077 XSETINT (c, (XINT (c) | meta_modifier) & ~0x80);
1079 cmd = access_keymap (keymap, c, t_ok, 0, 1);
1080 if (idx == length)
1081 RETURN_UNGCPRO (cmd);
1083 keymap = get_keymap (cmd, 0, 1);
1084 if (!CONSP (keymap))
1085 RETURN_UNGCPRO (make_number (idx));
1087 QUIT;
1091 /* Make KEYMAP define event C as a keymap (i.e., as a prefix).
1092 Assume that currently it does not define C at all.
1093 Return the keymap. */
1095 static Lisp_Object
1096 define_as_prefix (keymap, c)
1097 Lisp_Object keymap, c;
1099 Lisp_Object cmd;
1101 cmd = Fmake_sparse_keymap (Qnil);
1102 /* If this key is defined as a prefix in an inherited keymap,
1103 make it a prefix in this map, and make its definition
1104 inherit the other prefix definition. */
1105 cmd = nconc2 (cmd, access_keymap (keymap, c, 0, 0, 0));
1106 store_in_keymap (keymap, c, cmd);
1108 return cmd;
1111 /* Append a key to the end of a key sequence. We always make a vector. */
1113 Lisp_Object
1114 append_key (key_sequence, key)
1115 Lisp_Object key_sequence, key;
1117 Lisp_Object args[2];
1119 args[0] = key_sequence;
1121 args[1] = Fcons (key, Qnil);
1122 return Fvconcat (2, args);
1126 /* Global, local, and minor mode keymap stuff. */
1128 /* We can't put these variables inside current_minor_maps, since under
1129 some systems, static gets macro-defined to be the empty string.
1130 Ickypoo. */
1131 static Lisp_Object *cmm_modes, *cmm_maps;
1132 static int cmm_size;
1134 /* Error handler used in current_minor_maps. */
1135 static Lisp_Object
1136 current_minor_maps_error ()
1138 return Qnil;
1141 /* Store a pointer to an array of the keymaps of the currently active
1142 minor modes in *buf, and return the number of maps it contains.
1144 This function always returns a pointer to the same buffer, and may
1145 free or reallocate it, so if you want to keep it for a long time or
1146 hand it out to lisp code, copy it. This procedure will be called
1147 for every key sequence read, so the nice lispy approach (return a
1148 new assoclist, list, what have you) for each invocation would
1149 result in a lot of consing over time.
1151 If we used xrealloc/xmalloc and ran out of memory, they would throw
1152 back to the command loop, which would try to read a key sequence,
1153 which would call this function again, resulting in an infinite
1154 loop. Instead, we'll use realloc/malloc and silently truncate the
1155 list, let the key sequence be read, and hope some other piece of
1156 code signals the error. */
1158 current_minor_maps (modeptr, mapptr)
1159 Lisp_Object **modeptr, **mapptr;
1161 int i = 0;
1162 int list_number = 0;
1163 Lisp_Object alist, assoc, var, val;
1164 Lisp_Object lists[2];
1166 lists[0] = Vminor_mode_overriding_map_alist;
1167 lists[1] = Vminor_mode_map_alist;
1169 for (list_number = 0; list_number < 2; list_number++)
1170 for (alist = lists[list_number];
1171 CONSP (alist);
1172 alist = XCDR (alist))
1173 if ((assoc = XCAR (alist), CONSP (assoc))
1174 && (var = XCAR (assoc), SYMBOLP (var))
1175 && (val = find_symbol_value (var), !EQ (val, Qunbound))
1176 && !NILP (val))
1178 Lisp_Object temp;
1180 /* If a variable has an entry in Vminor_mode_overriding_map_alist,
1181 and also an entry in Vminor_mode_map_alist,
1182 ignore the latter. */
1183 if (list_number == 1)
1185 val = assq_no_quit (var, lists[0]);
1186 if (!NILP (val))
1187 continue;
1190 if (i >= cmm_size)
1192 Lisp_Object *newmodes, *newmaps;
1194 /* Use malloc/realloc here. See the comment above
1195 this function. */
1196 if (cmm_maps)
1198 BLOCK_INPUT;
1199 cmm_size *= 2;
1200 newmodes
1201 = (Lisp_Object *) realloc (cmm_modes,
1202 cmm_size * sizeof *newmodes);
1203 newmaps
1204 = (Lisp_Object *) realloc (cmm_maps,
1205 cmm_size * sizeof *newmaps);
1206 UNBLOCK_INPUT;
1208 else
1210 BLOCK_INPUT;
1211 cmm_size = 30;
1212 newmodes
1213 = (Lisp_Object *) malloc (cmm_size * sizeof *newmodes);
1214 newmaps
1215 = (Lisp_Object *) malloc (cmm_size * sizeof *newmaps);
1216 UNBLOCK_INPUT;
1219 if (newmodes)
1220 cmm_modes = newmodes;
1221 if (newmaps)
1222 cmm_maps = newmaps;
1224 if (newmodes == NULL || newmaps == NULL)
1225 break;
1228 /* Get the keymap definition--or nil if it is not defined. */
1229 temp = internal_condition_case_1 (Findirect_function,
1230 XCDR (assoc),
1231 Qerror, current_minor_maps_error);
1232 if (!NILP (temp))
1234 cmm_modes[i] = var;
1235 cmm_maps [i] = temp;
1236 i++;
1240 if (modeptr) *modeptr = cmm_modes;
1241 if (mapptr) *mapptr = cmm_maps;
1242 return i;
1245 DEFUN ("current-active-maps", Fcurrent_active_maps, Scurrent_active_maps,
1246 0, 1, 0,
1247 "Return a list of the currently active keymaps.\n\
1248 OLP if non-nil indicates that we should obey `overriding-local-map' and\n\
1249 `overriding-terminal-local-map'.")
1250 (olp)
1251 Lisp_Object olp;
1253 Lisp_Object keymaps = Fcons (current_global_map, Qnil);
1255 if (!NILP (olp))
1257 if (!NILP (Voverriding_local_map))
1258 keymaps = Fcons (Voverriding_local_map, keymaps);
1259 if (!NILP (current_kboard->Voverriding_terminal_local_map))
1260 keymaps = Fcons (current_kboard->Voverriding_terminal_local_map, keymaps);
1262 if (NILP (XCDR (keymaps)))
1264 Lisp_Object local;
1265 Lisp_Object *maps;
1266 int nmaps, i;
1268 local = get_local_map (PT, current_buffer, Qlocal_map);
1269 if (!NILP (local))
1270 keymaps = Fcons (local, keymaps);
1272 local = get_local_map (PT, current_buffer, Qkeymap);
1273 if (!NILP (local))
1274 keymaps = Fcons (local, keymaps);
1276 nmaps = current_minor_maps (0, &maps);
1278 for (i = --nmaps; i >= 0; i--)
1279 if (!NILP (maps[i]))
1280 keymaps = Fcons (maps[i], keymaps);
1283 return keymaps;
1286 /* GC is possible in this function if it autoloads a keymap. */
1288 DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 2, 0,
1289 "Return the binding for command KEY in current keymaps.\n\
1290 KEY is a string or vector, a sequence of keystrokes.\n\
1291 The binding is probably a symbol with a function definition.\n\
1293 Normally, `key-binding' ignores bindings for t, which act as default\n\
1294 bindings, used when nothing else in the keymap applies; this makes it\n\
1295 usable as a general function for probing keymaps. However, if the\n\
1296 optional second argument ACCEPT-DEFAULT is non-nil, `key-binding' does\n\
1297 recognize the default bindings, just as `read-key-sequence' does.")
1298 (key, accept_default)
1299 Lisp_Object key, accept_default;
1301 Lisp_Object *maps, value;
1302 int nmaps, i;
1303 struct gcpro gcpro1;
1305 GCPRO1 (key);
1307 if (!NILP (current_kboard->Voverriding_terminal_local_map))
1309 value = Flookup_key (current_kboard->Voverriding_terminal_local_map,
1310 key, accept_default);
1311 if (! NILP (value) && !INTEGERP (value))
1312 RETURN_UNGCPRO (value);
1314 else if (!NILP (Voverriding_local_map))
1316 value = Flookup_key (Voverriding_local_map, key, accept_default);
1317 if (! NILP (value) && !INTEGERP (value))
1318 RETURN_UNGCPRO (value);
1320 else
1322 Lisp_Object local;
1324 nmaps = current_minor_maps (0, &maps);
1325 /* Note that all these maps are GCPRO'd
1326 in the places where we found them. */
1328 for (i = 0; i < nmaps; i++)
1329 if (! NILP (maps[i]))
1331 value = Flookup_key (maps[i], key, accept_default);
1332 if (! NILP (value) && !INTEGERP (value))
1333 RETURN_UNGCPRO (value);
1336 local = get_local_map (PT, current_buffer, Qkeymap);
1337 if (! NILP (local))
1339 value = Flookup_key (local, key, accept_default);
1340 if (! NILP (value) && !INTEGERP (value))
1341 RETURN_UNGCPRO (value);
1344 local = get_local_map (PT, current_buffer, Qlocal_map);
1346 if (! NILP (local))
1348 value = Flookup_key (local, key, accept_default);
1349 if (! NILP (value) && !INTEGERP (value))
1350 RETURN_UNGCPRO (value);
1354 value = Flookup_key (current_global_map, key, accept_default);
1355 UNGCPRO;
1356 if (! NILP (value) && !INTEGERP (value))
1357 return value;
1359 return Qnil;
1362 /* GC is possible in this function if it autoloads a keymap. */
1364 DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 2, 0,
1365 "Return the binding for command KEYS in current local keymap only.\n\
1366 KEYS is a string, a sequence of keystrokes.\n\
1367 The binding is probably a symbol with a function definition.\n\
1369 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1370 bindings; see the description of `lookup-key' for more details about this.")
1371 (keys, accept_default)
1372 Lisp_Object keys, accept_default;
1374 register Lisp_Object map;
1375 map = current_buffer->keymap;
1376 if (NILP (map))
1377 return Qnil;
1378 return Flookup_key (map, keys, accept_default);
1381 /* GC is possible in this function if it autoloads a keymap. */
1383 DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 2, 0,
1384 "Return the binding for command KEYS in current global keymap only.\n\
1385 KEYS is a string, a sequence of keystrokes.\n\
1386 The binding is probably a symbol with a function definition.\n\
1387 This function's return values are the same as those of lookup-key\n\
1388 \(which see).\n\
1390 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1391 bindings; see the description of `lookup-key' for more details about this.")
1392 (keys, accept_default)
1393 Lisp_Object keys, accept_default;
1395 return Flookup_key (current_global_map, keys, accept_default);
1398 /* GC is possible in this function if it autoloads a keymap. */
1400 DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0,
1401 "Find the visible minor mode bindings of KEY.\n\
1402 Return an alist of pairs (MODENAME . BINDING), where MODENAME is the\n\
1403 the symbol which names the minor mode binding KEY, and BINDING is\n\
1404 KEY's definition in that mode. In particular, if KEY has no\n\
1405 minor-mode bindings, return nil. If the first binding is a\n\
1406 non-prefix, all subsequent bindings will be omitted, since they would\n\
1407 be ignored. Similarly, the list doesn't include non-prefix bindings\n\
1408 that come after prefix bindings.\n\
1410 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1411 bindings; see the description of `lookup-key' for more details about this.")
1412 (key, accept_default)
1413 Lisp_Object key, accept_default;
1415 Lisp_Object *modes, *maps;
1416 int nmaps;
1417 Lisp_Object binding;
1418 int i, j;
1419 struct gcpro gcpro1, gcpro2;
1421 nmaps = current_minor_maps (&modes, &maps);
1422 /* Note that all these maps are GCPRO'd
1423 in the places where we found them. */
1425 binding = Qnil;
1426 GCPRO2 (key, binding);
1428 for (i = j = 0; i < nmaps; i++)
1429 if (!NILP (maps[i])
1430 && !NILP (binding = Flookup_key (maps[i], key, accept_default))
1431 && !INTEGERP (binding))
1433 if (KEYMAPP (binding))
1434 maps[j++] = Fcons (modes[i], binding);
1435 else if (j == 0)
1436 RETURN_UNGCPRO (Fcons (Fcons (modes[i], binding), Qnil));
1439 UNGCPRO;
1440 return Flist (j, maps);
1443 DEFUN ("define-prefix-command", Fdefine_prefix_command, Sdefine_prefix_command, 1, 3, 0,
1444 "Define COMMAND as a prefix command. COMMAND should be a symbol.\n\
1445 A new sparse keymap is stored as COMMAND's function definition and its value.\n\
1446 If a second optional argument MAPVAR is given, the map is stored as\n\
1447 its value instead of as COMMAND's value; but COMMAND is still defined\n\
1448 as a function.\n\
1449 The third optional argument NAME, if given, supplies a menu name\n\
1450 string for the map. This is required to use the keymap as a menu.")
1451 (command, mapvar, name)
1452 Lisp_Object command, mapvar, name;
1454 Lisp_Object map;
1455 map = Fmake_sparse_keymap (name);
1456 Ffset (command, map);
1457 if (!NILP (mapvar))
1458 Fset (mapvar, map);
1459 else
1460 Fset (command, map);
1461 return command;
1464 DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0,
1465 "Select KEYMAP as the global keymap.")
1466 (keymap)
1467 Lisp_Object keymap;
1469 keymap = get_keymap (keymap, 1, 1);
1470 current_global_map = keymap;
1472 return Qnil;
1475 DEFUN ("use-local-map", Fuse_local_map, Suse_local_map, 1, 1, 0,
1476 "Select KEYMAP as the local keymap.\n\
1477 If KEYMAP is nil, that means no local keymap.")
1478 (keymap)
1479 Lisp_Object keymap;
1481 if (!NILP (keymap))
1482 keymap = get_keymap (keymap, 1, 1);
1484 current_buffer->keymap = keymap;
1486 return Qnil;
1489 DEFUN ("current-local-map", Fcurrent_local_map, Scurrent_local_map, 0, 0, 0,
1490 "Return current buffer's local keymap, or nil if it has none.")
1493 return current_buffer->keymap;
1496 DEFUN ("current-global-map", Fcurrent_global_map, Scurrent_global_map, 0, 0, 0,
1497 "Return the current global keymap.")
1500 return current_global_map;
1503 DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps, Scurrent_minor_mode_maps, 0, 0, 0,
1504 "Return a list of keymaps for the minor modes of the current buffer.")
1507 Lisp_Object *maps;
1508 int nmaps = current_minor_maps (0, &maps);
1510 return Flist (nmaps, maps);
1513 /* Help functions for describing and documenting keymaps. */
1516 static void
1517 accessible_keymaps_1 (key, cmd, maps, tail, thisseq, is_metized)
1518 Lisp_Object maps, tail, thisseq, key, cmd;
1519 int is_metized; /* If 1, `key' is assumed to be INTEGERP. */
1521 Lisp_Object tem;
1523 cmd = get_keyelt (cmd, 0);
1524 if (NILP (cmd))
1525 return;
1527 tem = get_keymap (cmd, 0, 0);
1528 if (CONSP (tem))
1530 cmd = tem;
1531 /* Ignore keymaps that are already added to maps. */
1532 tem = Frassq (cmd, maps);
1533 if (NILP (tem))
1535 /* If the last key in thisseq is meta-prefix-char,
1536 turn it into a meta-ized keystroke. We know
1537 that the event we're about to append is an
1538 ascii keystroke since we're processing a
1539 keymap table. */
1540 if (is_metized)
1542 int meta_bit = meta_modifier;
1543 Lisp_Object last = make_number (XINT (Flength (thisseq)) - 1);
1544 tem = Fcopy_sequence (thisseq);
1546 Faset (tem, last, make_number (XINT (key) | meta_bit));
1548 /* This new sequence is the same length as
1549 thisseq, so stick it in the list right
1550 after this one. */
1551 XSETCDR (tail,
1552 Fcons (Fcons (tem, cmd), XCDR (tail)));
1554 else
1556 tem = append_key (thisseq, key);
1557 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
1563 static void
1564 accessible_keymaps_char_table (args, index, cmd)
1565 Lisp_Object args, index, cmd;
1567 accessible_keymaps_1 (index, cmd,
1568 XCAR (XCAR (args)),
1569 XCAR (XCDR (args)),
1570 XCDR (XCDR (args)),
1571 XINT (XCDR (XCAR (args))));
1574 /* This function cannot GC. */
1576 DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps,
1577 1, 2, 0,
1578 "Find all keymaps accessible via prefix characters from KEYMAP.\n\
1579 Returns a list of elements of the form (KEYS . MAP), where the sequence\n\
1580 KEYS starting from KEYMAP gets you to MAP. These elements are ordered\n\
1581 so that the KEYS increase in length. The first element is ([] . KEYMAP).\n\
1582 An optional argument PREFIX, if non-nil, should be a key sequence;\n\
1583 then the value includes only maps for prefixes that start with PREFIX.")
1584 (keymap, prefix)
1585 Lisp_Object keymap, prefix;
1587 Lisp_Object maps, good_maps, tail;
1588 int prefixlen = 0;
1590 /* no need for gcpro because we don't autoload any keymaps. */
1592 if (!NILP (prefix))
1593 prefixlen = XINT (Flength (prefix));
1595 if (!NILP (prefix))
1597 /* If a prefix was specified, start with the keymap (if any) for
1598 that prefix, so we don't waste time considering other prefixes. */
1599 Lisp_Object tem;
1600 tem = Flookup_key (keymap, prefix, Qt);
1601 /* Flookup_key may give us nil, or a number,
1602 if the prefix is not defined in this particular map.
1603 It might even give us a list that isn't a keymap. */
1604 tem = get_keymap (tem, 0, 0);
1605 if (CONSP (tem))
1607 /* Convert PREFIX to a vector now, so that later on
1608 we don't have to deal with the possibility of a string. */
1609 if (STRINGP (prefix))
1611 int i, i_byte, c;
1612 Lisp_Object copy;
1614 copy = Fmake_vector (make_number (XSTRING (prefix)->size), Qnil);
1615 for (i = 0, i_byte = 0; i < XSTRING (prefix)->size;)
1617 int i_before = i;
1619 FETCH_STRING_CHAR_ADVANCE (c, prefix, i, i_byte);
1620 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
1621 c ^= 0200 | meta_modifier;
1622 ASET (copy, i_before, make_number (c));
1624 prefix = copy;
1626 maps = Fcons (Fcons (prefix, tem), Qnil);
1628 else
1629 return Qnil;
1631 else
1632 maps = Fcons (Fcons (Fmake_vector (make_number (0), Qnil),
1633 get_keymap (keymap, 1, 0)),
1634 Qnil);
1636 /* For each map in the list maps,
1637 look at any other maps it points to,
1638 and stick them at the end if they are not already in the list.
1640 This is a breadth-first traversal, where tail is the queue of
1641 nodes, and maps accumulates a list of all nodes visited. */
1643 for (tail = maps; CONSP (tail); tail = XCDR (tail))
1645 register Lisp_Object thisseq, thismap;
1646 Lisp_Object last;
1647 /* Does the current sequence end in the meta-prefix-char? */
1648 int is_metized;
1650 thisseq = Fcar (Fcar (tail));
1651 thismap = Fcdr (Fcar (tail));
1652 last = make_number (XINT (Flength (thisseq)) - 1);
1653 is_metized = (XINT (last) >= 0
1654 /* Don't metize the last char of PREFIX. */
1655 && XINT (last) >= prefixlen
1656 && EQ (Faref (thisseq, last), meta_prefix_char));
1658 for (; CONSP (thismap); thismap = XCDR (thismap))
1660 Lisp_Object elt;
1662 elt = XCAR (thismap);
1664 QUIT;
1666 if (CHAR_TABLE_P (elt))
1668 Lisp_Object indices[3];
1670 map_char_table (accessible_keymaps_char_table, Qnil,
1671 elt, Fcons (Fcons (maps, make_number (is_metized)),
1672 Fcons (tail, thisseq)),
1673 0, indices);
1675 else if (VECTORP (elt))
1677 register int i;
1679 /* Vector keymap. Scan all the elements. */
1680 for (i = 0; i < ASIZE (elt); i++)
1681 accessible_keymaps_1 (make_number (i), AREF (elt, i),
1682 maps, tail, thisseq, is_metized);
1685 else if (CONSP (elt))
1686 accessible_keymaps_1 (XCAR (elt), XCDR (elt),
1687 maps, tail, thisseq,
1688 is_metized && INTEGERP (XCAR (elt)));
1693 if (NILP (prefix))
1694 return maps;
1696 /* Now find just the maps whose access prefixes start with PREFIX. */
1698 good_maps = Qnil;
1699 for (; CONSP (maps); maps = XCDR (maps))
1701 Lisp_Object elt, thisseq;
1702 elt = XCAR (maps);
1703 thisseq = XCAR (elt);
1704 /* The access prefix must be at least as long as PREFIX,
1705 and the first elements must match those of PREFIX. */
1706 if (XINT (Flength (thisseq)) >= prefixlen)
1708 int i;
1709 for (i = 0; i < prefixlen; i++)
1711 Lisp_Object i1;
1712 XSETFASTINT (i1, i);
1713 if (!EQ (Faref (thisseq, i1), Faref (prefix, i1)))
1714 break;
1716 if (i == prefixlen)
1717 good_maps = Fcons (elt, good_maps);
1721 return Fnreverse (good_maps);
1724 Lisp_Object Qsingle_key_description, Qkey_description;
1726 /* This function cannot GC. */
1728 DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0,
1729 "Return a pretty description of key-sequence KEYS.\n\
1730 Control characters turn into \"C-foo\" sequences, meta into \"M-foo\"\n\
1731 spaces are put between sequence elements, etc.")
1732 (keys)
1733 Lisp_Object keys;
1735 int len = 0;
1736 int i, i_byte;
1737 Lisp_Object sep;
1738 Lisp_Object *args = NULL;
1740 if (STRINGP (keys))
1742 Lisp_Object vector;
1743 vector = Fmake_vector (Flength (keys), Qnil);
1744 for (i = 0, i_byte = 0; i < XSTRING (keys)->size; )
1746 int c;
1747 int i_before = i;
1749 FETCH_STRING_CHAR_ADVANCE (c, keys, i, i_byte);
1750 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
1751 c ^= 0200 | meta_modifier;
1752 XSETFASTINT (AREF (vector, i_before), c);
1754 keys = vector;
1757 if (VECTORP (keys))
1759 /* In effect, this computes
1760 (mapconcat 'single-key-description keys " ")
1761 but we shouldn't use mapconcat because it can do GC. */
1763 len = XVECTOR (keys)->size;
1764 sep = build_string (" ");
1765 /* This has one extra element at the end that we don't pass to Fconcat. */
1766 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
1768 for (i = 0; i < len; i++)
1770 args[i * 2] = Fsingle_key_description (AREF (keys, i), Qnil);
1771 args[i * 2 + 1] = sep;
1774 else if (CONSP (keys))
1776 /* In effect, this computes
1777 (mapconcat 'single-key-description keys " ")
1778 but we shouldn't use mapconcat because it can do GC. */
1780 len = XFASTINT (Flength (keys));
1781 sep = build_string (" ");
1782 /* This has one extra element at the end that we don't pass to Fconcat. */
1783 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
1785 for (i = 0; i < len; i++)
1787 args[i * 2] = Fsingle_key_description (XCAR (keys), Qnil);
1788 args[i * 2 + 1] = sep;
1789 keys = XCDR (keys);
1792 else
1793 keys = wrong_type_argument (Qarrayp, keys);
1795 if (len == 0)
1796 return empty_string;
1797 return Fconcat (len * 2 - 1, args);
1800 char *
1801 push_key_description (c, p, force_multibyte)
1802 register unsigned int c;
1803 register char *p;
1804 int force_multibyte;
1806 unsigned c2;
1808 /* Clear all the meaningless bits above the meta bit. */
1809 c &= meta_modifier | ~ - meta_modifier;
1810 c2 = c & ~(alt_modifier | ctrl_modifier | hyper_modifier
1811 | meta_modifier | shift_modifier | super_modifier);
1813 if (c & alt_modifier)
1815 *p++ = 'A';
1816 *p++ = '-';
1817 c -= alt_modifier;
1819 if ((c & ctrl_modifier) != 0
1820 || (c2 < ' ' && c2 != 27 && c2 != '\t' && c2 != Ctl ('M')))
1822 *p++ = 'C';
1823 *p++ = '-';
1824 c &= ~ctrl_modifier;
1826 if (c & hyper_modifier)
1828 *p++ = 'H';
1829 *p++ = '-';
1830 c -= hyper_modifier;
1832 if (c & meta_modifier)
1834 *p++ = 'M';
1835 *p++ = '-';
1836 c -= meta_modifier;
1838 if (c & shift_modifier)
1840 *p++ = 'S';
1841 *p++ = '-';
1842 c -= shift_modifier;
1844 if (c & super_modifier)
1846 *p++ = 's';
1847 *p++ = '-';
1848 c -= super_modifier;
1850 if (c < 040)
1852 if (c == 033)
1854 *p++ = 'E';
1855 *p++ = 'S';
1856 *p++ = 'C';
1858 else if (c == '\t')
1860 *p++ = 'T';
1861 *p++ = 'A';
1862 *p++ = 'B';
1864 else if (c == Ctl ('M'))
1866 *p++ = 'R';
1867 *p++ = 'E';
1868 *p++ = 'T';
1870 else
1872 /* `C-' already added above. */
1873 if (c > 0 && c <= Ctl ('Z'))
1874 *p++ = c + 0140;
1875 else
1876 *p++ = c + 0100;
1879 else if (c == 0177)
1881 *p++ = 'D';
1882 *p++ = 'E';
1883 *p++ = 'L';
1885 else if (c == ' ')
1887 *p++ = 'S';
1888 *p++ = 'P';
1889 *p++ = 'C';
1891 else if (c < 128
1892 || (NILP (current_buffer->enable_multibyte_characters)
1893 && SINGLE_BYTE_CHAR_P (c)
1894 && !force_multibyte))
1896 *p++ = c;
1898 else
1900 int valid_p = SINGLE_BYTE_CHAR_P (c) || char_valid_p (c, 0);
1902 if (force_multibyte && valid_p)
1904 if (SINGLE_BYTE_CHAR_P (c))
1905 c = unibyte_char_to_multibyte (c);
1906 p += CHAR_STRING (c, p);
1908 else if (NILP (current_buffer->enable_multibyte_characters)
1909 || valid_p)
1911 int bit_offset;
1912 *p++ = '\\';
1913 /* The biggest character code uses 19 bits. */
1914 for (bit_offset = 18; bit_offset >= 0; bit_offset -= 3)
1916 if (c >= (1 << bit_offset))
1917 *p++ = ((c & (7 << bit_offset)) >> bit_offset) + '0';
1920 else
1921 p += CHAR_STRING (c, p);
1924 return p;
1927 /* This function cannot GC. */
1929 DEFUN ("single-key-description", Fsingle_key_description,
1930 Ssingle_key_description, 1, 2, 0,
1931 "Return a pretty description of command character KEY.\n\
1932 Control characters turn into C-whatever, etc.\n\
1933 Optional argument NO-ANGLES non-nil means don't put angle brackets\n\
1934 around function keys and event symbols.")
1935 (key, no_angles)
1936 Lisp_Object key, no_angles;
1938 if (CONSP (key) && lucid_event_type_list_p (key))
1939 key = Fevent_convert_list (key);
1941 key = EVENT_HEAD (key);
1943 if (INTEGERP (key)) /* Normal character */
1945 unsigned int charset, c1, c2;
1946 int without_bits = XINT (key) & ~((-1) << CHARACTERBITS);
1948 if (SINGLE_BYTE_CHAR_P (without_bits))
1949 charset = 0;
1950 else
1951 SPLIT_CHAR (without_bits, charset, c1, c2);
1953 if (charset
1954 && CHARSET_DEFINED_P (charset)
1955 && ((c1 >= 0 && c1 < 32)
1956 || (c2 >= 0 && c2 < 32)))
1958 /* Handle a generic character. */
1959 Lisp_Object name;
1960 name = CHARSET_TABLE_INFO (charset, CHARSET_LONG_NAME_IDX);
1961 CHECK_STRING (name);
1962 return concat2 (build_string ("Character set "), name);
1964 else
1966 char tem[KEY_DESCRIPTION_SIZE], *end;
1967 int nbytes, nchars;
1968 Lisp_Object string;
1970 end = push_key_description (XUINT (key), tem, 1);
1971 nbytes = end - tem;
1972 nchars = multibyte_chars_in_text (tem, nbytes);
1973 if (nchars == nbytes)
1975 *end = '\0';
1976 string = build_string (tem);
1978 else
1979 string = make_multibyte_string (tem, nchars, nbytes);
1980 return string;
1983 else if (SYMBOLP (key)) /* Function key or event-symbol */
1985 if (NILP (no_angles))
1987 char *buffer
1988 = (char *) alloca (STRING_BYTES (XSYMBOL (key)->name) + 5);
1989 sprintf (buffer, "<%s>", XSYMBOL (key)->name->data);
1990 return build_string (buffer);
1992 else
1993 return Fsymbol_name (key);
1995 else if (STRINGP (key)) /* Buffer names in the menubar. */
1996 return Fcopy_sequence (key);
1997 else
1998 error ("KEY must be an integer, cons, symbol, or string");
1999 return Qnil;
2002 char *
2003 push_text_char_description (c, p)
2004 register unsigned int c;
2005 register char *p;
2007 if (c >= 0200)
2009 *p++ = 'M';
2010 *p++ = '-';
2011 c -= 0200;
2013 if (c < 040)
2015 *p++ = '^';
2016 *p++ = c + 64; /* 'A' - 1 */
2018 else if (c == 0177)
2020 *p++ = '^';
2021 *p++ = '?';
2023 else
2024 *p++ = c;
2025 return p;
2028 /* This function cannot GC. */
2030 DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0,
2031 "Return a pretty description of file-character CHARACTER.\n\
2032 Control characters turn into \"^char\", etc.")
2033 (character)
2034 Lisp_Object character;
2036 /* Currently MAX_MULTIBYTE_LENGTH is 4 (< 6). */
2037 unsigned char str[6];
2038 int c;
2040 CHECK_NUMBER (character);
2042 c = XINT (character);
2043 if (!SINGLE_BYTE_CHAR_P (c))
2045 int len = CHAR_STRING (c, str);
2047 return make_multibyte_string (str, 1, len);
2050 *push_text_char_description (c & 0377, str) = 0;
2052 return build_string (str);
2055 /* Return non-zero if SEQ contains only ASCII characters, perhaps with
2056 a meta bit. */
2057 static int
2058 ascii_sequence_p (seq)
2059 Lisp_Object seq;
2061 int i;
2062 int len = XINT (Flength (seq));
2064 for (i = 0; i < len; i++)
2066 Lisp_Object ii, elt;
2068 XSETFASTINT (ii, i);
2069 elt = Faref (seq, ii);
2071 if (!INTEGERP (elt)
2072 || (XUINT (elt) & ~CHAR_META) >= 0x80)
2073 return 0;
2076 return 1;
2080 /* where-is - finding a command in a set of keymaps. */
2082 static Lisp_Object where_is_internal_1 ();
2083 static void where_is_internal_2 ();
2085 /* Like Flookup_key, but uses a list of keymaps SHADOW instead of a single map.
2086 Returns the first non-nil binding found in any of those maps. */
2088 static Lisp_Object
2089 shadow_lookup (shadow, key, flag)
2090 Lisp_Object shadow, key, flag;
2092 Lisp_Object tail, value;
2094 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2096 value = Flookup_key (XCAR (tail), key, flag);
2097 if (!NILP (value) && !NATNUMP (value))
2098 return value;
2100 return Qnil;
2103 /* This function can GC if Flookup_key autoloads any keymaps. */
2105 static Lisp_Object
2106 where_is_internal (definition, keymaps, firstonly, noindirect)
2107 Lisp_Object definition, keymaps;
2108 Lisp_Object firstonly, noindirect;
2110 Lisp_Object maps = Qnil;
2111 Lisp_Object found, sequences;
2112 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
2113 /* 1 means ignore all menu bindings entirely. */
2114 int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii);
2116 found = keymaps;
2117 while (CONSP (found))
2119 maps =
2120 nconc2 (maps,
2121 Faccessible_keymaps (get_keymap (XCAR (found), 1, 0), Qnil));
2122 found = XCDR (found);
2125 GCPRO5 (definition, keymaps, maps, found, sequences);
2126 found = Qnil;
2127 sequences = Qnil;
2129 for (; !NILP (maps); maps = Fcdr (maps))
2131 /* Key sequence to reach map, and the map that it reaches */
2132 register Lisp_Object this, map;
2134 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
2135 [M-CHAR] sequences, check if last character of the sequence
2136 is the meta-prefix char. */
2137 Lisp_Object last;
2138 int last_is_meta;
2140 this = Fcar (Fcar (maps));
2141 map = Fcdr (Fcar (maps));
2142 last = make_number (XINT (Flength (this)) - 1);
2143 last_is_meta = (XINT (last) >= 0
2144 && EQ (Faref (this, last), meta_prefix_char));
2146 /* if (nomenus && !ascii_sequence_p (this)) */
2147 if (nomenus && XINT (last) >= 0
2148 && !INTEGERP (Faref (this, make_number (0))))
2149 /* If no menu entries should be returned, skip over the
2150 keymaps bound to `menu-bar' and `tool-bar' and other
2151 non-ascii prefixes like `C-down-mouse-2'. */
2152 continue;
2154 QUIT;
2156 while (CONSP (map))
2158 /* Because the code we want to run on each binding is rather
2159 large, we don't want to have two separate loop bodies for
2160 sparse keymap bindings and tables; we want to iterate one
2161 loop body over both keymap and vector bindings.
2163 For this reason, if Fcar (map) is a vector, we don't
2164 advance map to the next element until i indicates that we
2165 have finished off the vector. */
2166 Lisp_Object elt, key, binding;
2167 elt = XCAR (map);
2168 map = XCDR (map);
2170 sequences = Qnil;
2172 QUIT;
2174 /* Set key and binding to the current key and binding, and
2175 advance map and i to the next binding. */
2176 if (VECTORP (elt))
2178 Lisp_Object sequence;
2179 int i;
2180 /* In a vector, look at each element. */
2181 for (i = 0; i < XVECTOR (elt)->size; i++)
2183 binding = AREF (elt, i);
2184 XSETFASTINT (key, i);
2185 sequence = where_is_internal_1 (binding, key, definition,
2186 noindirect, this,
2187 last, nomenus, last_is_meta);
2188 if (!NILP (sequence))
2189 sequences = Fcons (sequence, sequences);
2192 else if (CHAR_TABLE_P (elt))
2194 Lisp_Object indices[3];
2195 Lisp_Object args;
2197 args = Fcons (Fcons (Fcons (definition, noindirect),
2198 Qnil), /* Result accumulator. */
2199 Fcons (Fcons (this, last),
2200 Fcons (make_number (nomenus),
2201 make_number (last_is_meta))));
2202 map_char_table (where_is_internal_2, Qnil, elt, args,
2203 0, indices);
2204 sequences = XCDR (XCAR (args));
2206 else if (CONSP (elt))
2208 Lisp_Object sequence;
2210 key = XCAR (elt);
2211 binding = XCDR (elt);
2213 sequence = where_is_internal_1 (binding, key, definition,
2214 noindirect, this,
2215 last, nomenus, last_is_meta);
2216 if (!NILP (sequence))
2217 sequences = Fcons (sequence, sequences);
2221 for (; !NILP (sequences); sequences = XCDR (sequences))
2223 Lisp_Object sequence;
2225 sequence = XCAR (sequences);
2227 /* Verify that this key binding is not shadowed by another
2228 binding for the same key, before we say it exists.
2230 Mechanism: look for local definition of this key and if
2231 it is defined and does not match what we found then
2232 ignore this key.
2234 Either nil or number as value from Flookup_key
2235 means undefined. */
2236 if (!EQ (shadow_lookup (keymaps, sequence, Qnil), definition))
2237 continue;
2239 /* It is a true unshadowed match. Record it, unless it's already
2240 been seen (as could happen when inheriting keymaps). */
2241 if (NILP (Fmember (sequence, found)))
2242 found = Fcons (sequence, found);
2244 /* If firstonly is Qnon_ascii, then we can return the first
2245 binding we find. If firstonly is not Qnon_ascii but not
2246 nil, then we should return the first ascii-only binding
2247 we find. */
2248 if (EQ (firstonly, Qnon_ascii))
2249 RETURN_UNGCPRO (sequence);
2250 else if (!NILP (firstonly) && ascii_sequence_p (sequence))
2251 RETURN_UNGCPRO (sequence);
2256 UNGCPRO;
2258 found = Fnreverse (found);
2260 /* firstonly may have been t, but we may have gone all the way through
2261 the keymaps without finding an all-ASCII key sequence. So just
2262 return the best we could find. */
2263 if (!NILP (firstonly))
2264 return Fcar (found);
2266 return found;
2269 DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 4, 0,
2270 "Return list of keys that invoke DEFINITION.\n\
2271 If KEYMAP is non-nil, search only KEYMAP and the global keymap.\n\
2272 If KEYMAP is nil, search all the currently active keymaps.\n\
2273 If KEYMAP is a list of keymaps, search only those keymaps.\n\
2275 If optional 3rd arg FIRSTONLY is non-nil, return the first key sequence found,\n\
2276 rather than a list of all possible key sequences.\n\
2277 If FIRSTONLY is the symbol `non-ascii', return the first binding found,\n\
2278 no matter what it is.\n\
2279 If FIRSTONLY has another non-nil value, prefer sequences of ASCII characters,\n\
2280 and entirely reject menu bindings.\n\
2282 If optional 4th arg NOINDIRECT is non-nil, don't follow indirections\n\
2283 to other keymaps or slots. This makes it possible to search for an\n\
2284 indirect definition itself.")
2285 (definition, keymap, firstonly, noindirect)
2286 Lisp_Object definition, keymap;
2287 Lisp_Object firstonly, noindirect;
2289 Lisp_Object sequences, keymaps;
2290 /* 1 means ignore all menu bindings entirely. */
2291 int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii);
2292 Lisp_Object result;
2294 /* Find the relevant keymaps. */
2295 if (CONSP (keymap) && KEYMAPP (XCAR (keymap)))
2296 keymaps = keymap;
2297 else if (!NILP (keymap))
2298 keymaps = Fcons (keymap, Fcons (current_global_map, Qnil));
2299 else
2300 keymaps = Fcurrent_active_maps (Qnil);
2302 /* Only use caching for the menubar (i.e. called with (def nil t nil).
2303 We don't really need to check `keymap'. */
2304 if (nomenus && NILP (noindirect) && NILP (keymap))
2306 Lisp_Object *defns;
2307 int i, j, n;
2308 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2310 /* Check heuristic-consistency of the cache. */
2311 if (NILP (Fequal (keymaps, where_is_cache_keymaps)))
2312 where_is_cache = Qnil;
2314 if (NILP (where_is_cache))
2316 /* We need to create the cache. */
2317 Lisp_Object args[2];
2318 where_is_cache = Fmake_hash_table (0, args);
2319 where_is_cache_keymaps = Qt;
2321 /* Fill in the cache. */
2322 GCPRO4 (definition, keymaps, firstonly, noindirect);
2323 where_is_internal (definition, keymaps, firstonly, noindirect);
2324 UNGCPRO;
2326 where_is_cache_keymaps = keymaps;
2329 /* We want to process definitions from the last to the first.
2330 Instead of consing, copy definitions to a vector and step
2331 over that vector. */
2332 sequences = Fgethash (definition, where_is_cache, Qnil);
2333 n = XINT (Flength (sequences));
2334 defns = (Lisp_Object *) alloca (n * sizeof *defns);
2335 for (i = 0; CONSP (sequences); sequences = XCDR (sequences))
2336 defns[i++] = XCAR (sequences);
2338 /* Verify that the key bindings are not shadowed. Note that
2339 the following can GC. */
2340 GCPRO2 (definition, keymaps);
2341 result = Qnil;
2342 j = -1;
2343 for (i = n - 1; i >= 0; --i)
2344 if (EQ (shadow_lookup (keymaps, defns[i], Qnil), definition))
2346 if (ascii_sequence_p (defns[i]))
2347 break;
2348 else if (j < 0)
2349 j = i;
2352 result = i >= 0 ? defns[i] : (j >= 0 ? defns[j] : Qnil);
2353 UNGCPRO;
2355 else
2357 /* Kill the cache so that where_is_internal_1 doesn't think
2358 we're filling it up. */
2359 where_is_cache = Qnil;
2360 result = where_is_internal (definition, keymaps, firstonly, noindirect);
2363 return result;
2366 /* This is the function that Fwhere_is_internal calls using map_char_table.
2367 ARGS has the form
2368 (((DEFINITION . NOINDIRECT) . (KEYMAP . RESULT))
2370 ((THIS . LAST) . (NOMENUS . LAST_IS_META)))
2371 Since map_char_table doesn't really use the return value from this function,
2372 we the result append to RESULT, the slot in ARGS.
2374 This function can GC because it calls where_is_internal_1 which can
2375 GC. */
2377 static void
2378 where_is_internal_2 (args, key, binding)
2379 Lisp_Object args, key, binding;
2381 Lisp_Object definition, noindirect, this, last;
2382 Lisp_Object result, sequence;
2383 int nomenus, last_is_meta;
2384 struct gcpro gcpro1, gcpro2, gcpro3;
2386 GCPRO3 (args, key, binding);
2387 result = XCDR (XCAR (args));
2388 definition = XCAR (XCAR (XCAR (args)));
2389 noindirect = XCDR (XCAR (XCAR (args)));
2390 this = XCAR (XCAR (XCDR (args)));
2391 last = XCDR (XCAR (XCDR (args)));
2392 nomenus = XFASTINT (XCAR (XCDR (XCDR (args))));
2393 last_is_meta = XFASTINT (XCDR (XCDR (XCDR (args))));
2395 sequence = where_is_internal_1 (binding, key, definition, noindirect,
2396 this, last, nomenus, last_is_meta);
2398 if (!NILP (sequence))
2399 XSETCDR (XCAR (args), Fcons (sequence, result));
2401 UNGCPRO;
2405 /* This function cannot GC. */
2407 static Lisp_Object
2408 where_is_internal_1 (binding, key, definition, noindirect, this, last,
2409 nomenus, last_is_meta)
2410 Lisp_Object binding, key, definition, noindirect, this, last;
2411 int nomenus, last_is_meta;
2413 Lisp_Object sequence;
2415 /* Search through indirections unless that's not wanted. */
2416 if (NILP (noindirect))
2417 binding = get_keyelt (binding, 0);
2419 /* End this iteration if this element does not match
2420 the target. */
2422 if (!(!NILP (where_is_cache) /* everything "matches" during cache-fill. */
2423 || EQ (binding, definition)
2424 || (CONSP (definition) && !NILP (Fequal (binding, definition)))))
2425 /* Doesn't match. */
2426 return Qnil;
2428 /* We have found a match. Construct the key sequence where we found it. */
2429 if (INTEGERP (key) && last_is_meta)
2431 sequence = Fcopy_sequence (this);
2432 Faset (sequence, last, make_number (XINT (key) | meta_modifier));
2434 else
2435 sequence = append_key (this, key);
2437 if (!NILP (where_is_cache))
2439 Lisp_Object sequences = Fgethash (binding, where_is_cache, Qnil);
2440 Fputhash (binding, Fcons (sequence, sequences), where_is_cache);
2441 return Qnil;
2443 else
2444 return sequence;
2447 /* describe-bindings - summarizing all the bindings in a set of keymaps. */
2449 DEFUN ("describe-buffer-bindings", Fdescribe_buffer_bindings, Sdescribe_buffer_bindings, 1, 3, 0,
2450 "Insert the list of all defined keys and their definitions.\n\
2451 The list is inserted in the current buffer, while the bindings are\n\
2452 looked up in BUFFER.\n\
2453 The optional argument PREFIX, if non-nil, should be a key sequence;\n\
2454 then we display only bindings that start with that prefix.\n\
2455 The optional argument MENUS, if non-nil, says to mention menu bindings.\n\
2456 \(Ordinarily these are omitted from the output.)")
2457 (buffer, prefix, menus)
2458 Lisp_Object buffer, prefix, menus;
2460 Lisp_Object outbuf, shadow;
2461 int nomenu = NILP (menus);
2462 register Lisp_Object start1;
2463 struct gcpro gcpro1;
2465 char *alternate_heading
2466 = "\
2467 Keyboard translations:\n\n\
2468 You type Translation\n\
2469 -------- -----------\n";
2471 shadow = Qnil;
2472 GCPRO1 (shadow);
2474 outbuf = Fcurrent_buffer ();
2476 /* Report on alternates for keys. */
2477 if (STRINGP (Vkeyboard_translate_table) && !NILP (prefix))
2479 int c;
2480 unsigned char *translate = XSTRING (Vkeyboard_translate_table)->data;
2481 int translate_len = XSTRING (Vkeyboard_translate_table)->size;
2483 for (c = 0; c < translate_len; c++)
2484 if (translate[c] != c)
2486 char buf[KEY_DESCRIPTION_SIZE];
2487 char *bufend;
2489 if (alternate_heading)
2491 insert_string (alternate_heading);
2492 alternate_heading = 0;
2495 bufend = push_key_description (translate[c], buf, 1);
2496 insert (buf, bufend - buf);
2497 Findent_to (make_number (16), make_number (1));
2498 bufend = push_key_description (c, buf, 1);
2499 insert (buf, bufend - buf);
2501 insert ("\n", 1);
2504 insert ("\n", 1);
2507 if (!NILP (Vkey_translation_map))
2508 describe_map_tree (Vkey_translation_map, 0, Qnil, prefix,
2509 "Key translations", nomenu, 1, 0);
2512 /* Print the (major mode) local map. */
2513 start1 = Qnil;
2514 if (!NILP (current_kboard->Voverriding_terminal_local_map))
2515 start1 = current_kboard->Voverriding_terminal_local_map;
2516 else if (!NILP (Voverriding_local_map))
2517 start1 = Voverriding_local_map;
2519 if (!NILP (start1))
2521 describe_map_tree (start1, 1, shadow, prefix,
2522 "\f\nOverriding Bindings", nomenu, 0, 0);
2523 shadow = Fcons (start1, shadow);
2525 else
2527 /* Print the minor mode and major mode keymaps. */
2528 int i, nmaps;
2529 Lisp_Object *modes, *maps;
2531 /* Temporarily switch to `buffer', so that we can get that buffer's
2532 minor modes correctly. */
2533 Fset_buffer (buffer);
2535 nmaps = current_minor_maps (&modes, &maps);
2536 Fset_buffer (outbuf);
2538 /* Print the minor mode maps. */
2539 for (i = 0; i < nmaps; i++)
2541 /* The title for a minor mode keymap
2542 is constructed at run time.
2543 We let describe_map_tree do the actual insertion
2544 because it takes care of other features when doing so. */
2545 char *title, *p;
2547 if (!SYMBOLP (modes[i]))
2548 abort();
2550 p = title = (char *) alloca (42 + XSYMBOL (modes[i])->name->size);
2551 *p++ = '\f';
2552 *p++ = '\n';
2553 *p++ = '`';
2554 bcopy (XSYMBOL (modes[i])->name->data, p,
2555 XSYMBOL (modes[i])->name->size);
2556 p += XSYMBOL (modes[i])->name->size;
2557 *p++ = '\'';
2558 bcopy (" Minor Mode Bindings", p, sizeof (" Minor Mode Bindings") - 1);
2559 p += sizeof (" Minor Mode Bindings") - 1;
2560 *p = 0;
2562 describe_map_tree (maps[i], 1, shadow, prefix, title, nomenu, 0, 0);
2563 shadow = Fcons (maps[i], shadow);
2566 start1 = get_local_map (BUF_PT (XBUFFER (buffer)),
2567 XBUFFER (buffer), Qkeymap);
2568 if (!NILP (start1))
2570 describe_map_tree (start1, 1, shadow, prefix,
2571 "\f\nChar Property Bindings", nomenu, 0, 0);
2572 shadow = Fcons (start1, shadow);
2575 start1 = get_local_map (BUF_PT (XBUFFER (buffer)),
2576 XBUFFER (buffer), Qlocal_map);
2577 if (!NILP (start1))
2579 if (EQ (start1, XBUFFER (buffer)->keymap))
2580 describe_map_tree (start1, 1, shadow, prefix,
2581 "\f\nMajor Mode Bindings", nomenu, 0, 0);
2582 else
2583 describe_map_tree (start1, 1, shadow, prefix,
2584 "\f\nChar Property Bindings", nomenu, 0, 0);
2586 shadow = Fcons (start1, shadow);
2590 describe_map_tree (current_global_map, 1, shadow, prefix,
2591 "\f\nGlobal Bindings", nomenu, 0, 1);
2593 /* Print the function-key-map translations under this prefix. */
2594 if (!NILP (Vfunction_key_map))
2595 describe_map_tree (Vfunction_key_map, 0, Qnil, prefix,
2596 "\f\nFunction key map translations", nomenu, 1, 0);
2598 UNGCPRO;
2599 return Qnil;
2602 /* Insert a description of the key bindings in STARTMAP,
2603 followed by those of all maps reachable through STARTMAP.
2604 If PARTIAL is nonzero, omit certain "uninteresting" commands
2605 (such as `undefined').
2606 If SHADOW is non-nil, it is a list of maps;
2607 don't mention keys which would be shadowed by any of them.
2608 PREFIX, if non-nil, says mention only keys that start with PREFIX.
2609 TITLE, if not 0, is a string to insert at the beginning.
2610 TITLE should not end with a colon or a newline; we supply that.
2611 If NOMENU is not 0, then omit menu-bar commands.
2613 If TRANSL is nonzero, the definitions are actually key translations
2614 so print strings and vectors differently.
2616 If ALWAYS_TITLE is nonzero, print the title even if there are no maps
2617 to look through. */
2619 void
2620 describe_map_tree (startmap, partial, shadow, prefix, title, nomenu, transl,
2621 always_title)
2622 Lisp_Object startmap, shadow, prefix;
2623 int partial;
2624 char *title;
2625 int nomenu;
2626 int transl;
2627 int always_title;
2629 Lisp_Object maps, orig_maps, seen, sub_shadows;
2630 struct gcpro gcpro1, gcpro2, gcpro3;
2631 int something = 0;
2632 char *key_heading
2633 = "\
2634 key binding\n\
2635 --- -------\n";
2637 orig_maps = maps = Faccessible_keymaps (startmap, prefix);
2638 seen = Qnil;
2639 sub_shadows = Qnil;
2640 GCPRO3 (maps, seen, sub_shadows);
2642 if (nomenu)
2644 Lisp_Object list;
2646 /* Delete from MAPS each element that is for the menu bar. */
2647 for (list = maps; !NILP (list); list = XCDR (list))
2649 Lisp_Object elt, prefix, tem;
2651 elt = Fcar (list);
2652 prefix = Fcar (elt);
2653 if (XVECTOR (prefix)->size >= 1)
2655 tem = Faref (prefix, make_number (0));
2656 if (EQ (tem, Qmenu_bar))
2657 maps = Fdelq (elt, maps);
2662 if (!NILP (maps) || always_title)
2664 if (title)
2666 insert_string (title);
2667 if (!NILP (prefix))
2669 insert_string (" Starting With ");
2670 insert1 (Fkey_description (prefix));
2672 insert_string (":\n");
2674 insert_string (key_heading);
2675 something = 1;
2678 for (; !NILP (maps); maps = Fcdr (maps))
2680 register Lisp_Object elt, prefix, tail;
2682 elt = Fcar (maps);
2683 prefix = Fcar (elt);
2685 sub_shadows = Qnil;
2687 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2689 Lisp_Object shmap;
2691 shmap = XCAR (tail);
2693 /* If the sequence by which we reach this keymap is zero-length,
2694 then the shadow map for this keymap is just SHADOW. */
2695 if ((STRINGP (prefix) && XSTRING (prefix)->size == 0)
2696 || (VECTORP (prefix) && XVECTOR (prefix)->size == 0))
2698 /* If the sequence by which we reach this keymap actually has
2699 some elements, then the sequence's definition in SHADOW is
2700 what we should use. */
2701 else
2703 shmap = Flookup_key (shmap, Fcar (elt), Qt);
2704 if (INTEGERP (shmap))
2705 shmap = Qnil;
2708 /* If shmap is not nil and not a keymap,
2709 it completely shadows this map, so don't
2710 describe this map at all. */
2711 if (!NILP (shmap) && !KEYMAPP (shmap))
2712 goto skip;
2714 if (!NILP (shmap))
2715 sub_shadows = Fcons (shmap, sub_shadows);
2718 /* Maps we have already listed in this loop shadow this map. */
2719 for (tail = orig_maps; !EQ (tail, maps); tail = XCDR (tail))
2721 Lisp_Object tem;
2722 tem = Fequal (Fcar (XCAR (tail)), prefix);
2723 if (!NILP (tem))
2724 sub_shadows = Fcons (XCDR (XCAR (tail)), sub_shadows);
2727 describe_map (Fcdr (elt), prefix,
2728 transl ? describe_translation : describe_command,
2729 partial, sub_shadows, &seen, nomenu);
2731 skip: ;
2734 if (something)
2735 insert_string ("\n");
2737 UNGCPRO;
2740 static int previous_description_column;
2742 static void
2743 describe_command (definition)
2744 Lisp_Object definition;
2746 register Lisp_Object tem1;
2747 int column = current_column ();
2748 int description_column;
2750 /* If column 16 is no good, go to col 32;
2751 but don't push beyond that--go to next line instead. */
2752 if (column > 30)
2754 insert_char ('\n');
2755 description_column = 32;
2757 else if (column > 14 || (column > 10 && previous_description_column == 32))
2758 description_column = 32;
2759 else
2760 description_column = 16;
2762 Findent_to (make_number (description_column), make_number (1));
2763 previous_description_column = description_column;
2765 if (SYMBOLP (definition))
2767 XSETSTRING (tem1, XSYMBOL (definition)->name);
2768 insert1 (tem1);
2769 insert_string ("\n");
2771 else if (STRINGP (definition) || VECTORP (definition))
2772 insert_string ("Keyboard Macro\n");
2773 else if (KEYMAPP (definition))
2774 insert_string ("Prefix Command\n");
2775 else
2776 insert_string ("??\n");
2779 static void
2780 describe_translation (definition)
2781 Lisp_Object definition;
2783 register Lisp_Object tem1;
2785 Findent_to (make_number (16), make_number (1));
2787 if (SYMBOLP (definition))
2789 XSETSTRING (tem1, XSYMBOL (definition)->name);
2790 insert1 (tem1);
2791 insert_string ("\n");
2793 else if (STRINGP (definition) || VECTORP (definition))
2795 insert1 (Fkey_description (definition));
2796 insert_string ("\n");
2798 else if (KEYMAPP (definition))
2799 insert_string ("Prefix Command\n");
2800 else
2801 insert_string ("??\n");
2804 /* Describe the contents of map MAP, assuming that this map itself is
2805 reached by the sequence of prefix keys KEYS (a string or vector).
2806 PARTIAL, SHADOW, NOMENU are as in `describe_map_tree' above. */
2808 static void
2809 describe_map (map, keys, elt_describer, partial, shadow, seen, nomenu)
2810 register Lisp_Object map;
2811 Lisp_Object keys;
2812 void (*elt_describer) P_ ((Lisp_Object));
2813 int partial;
2814 Lisp_Object shadow;
2815 Lisp_Object *seen;
2816 int nomenu;
2818 Lisp_Object elt_prefix;
2819 Lisp_Object tail, definition, event;
2820 Lisp_Object tem;
2821 Lisp_Object suppress;
2822 Lisp_Object kludge;
2823 int first = 1;
2824 struct gcpro gcpro1, gcpro2, gcpro3;
2826 suppress = Qnil;
2828 if (!NILP (keys) && XFASTINT (Flength (keys)) > 0)
2830 /* Call Fkey_description first, to avoid GC bug for the other string. */
2831 tem = Fkey_description (keys);
2832 elt_prefix = concat2 (tem, build_string (" "));
2834 else
2835 elt_prefix = Qnil;
2837 if (partial)
2838 suppress = intern ("suppress-keymap");
2840 /* This vector gets used to present single keys to Flookup_key. Since
2841 that is done once per keymap element, we don't want to cons up a
2842 fresh vector every time. */
2843 kludge = Fmake_vector (make_number (1), Qnil);
2844 definition = Qnil;
2846 GCPRO3 (elt_prefix, definition, kludge);
2848 for (tail = map; CONSP (tail); tail = XCDR (tail))
2850 QUIT;
2852 if (VECTORP (XCAR (tail))
2853 || CHAR_TABLE_P (XCAR (tail)))
2854 describe_vector (XCAR (tail),
2855 elt_prefix, elt_describer, partial, shadow, map,
2856 (int *)0, 0);
2857 else if (CONSP (XCAR (tail)))
2859 event = XCAR (XCAR (tail));
2861 /* Ignore bindings whose "keys" are not really valid events.
2862 (We get these in the frames and buffers menu.) */
2863 if (!(SYMBOLP (event) || INTEGERP (event)))
2864 continue;
2866 if (nomenu && EQ (event, Qmenu_bar))
2867 continue;
2869 definition = get_keyelt (XCDR (XCAR (tail)), 0);
2871 /* Don't show undefined commands or suppressed commands. */
2872 if (NILP (definition)) continue;
2873 if (SYMBOLP (definition) && partial)
2875 tem = Fget (definition, suppress);
2876 if (!NILP (tem))
2877 continue;
2880 /* Don't show a command that isn't really visible
2881 because a local definition of the same key shadows it. */
2883 ASET (kludge, 0, event);
2884 if (!NILP (shadow))
2886 tem = shadow_lookup (shadow, kludge, Qt);
2887 if (!NILP (tem)) continue;
2890 tem = Flookup_key (map, kludge, Qt);
2891 if (!EQ (tem, definition)) continue;
2893 if (first)
2895 previous_description_column = 0;
2896 insert ("\n", 1);
2897 first = 0;
2900 if (!NILP (elt_prefix))
2901 insert1 (elt_prefix);
2903 /* THIS gets the string to describe the character EVENT. */
2904 insert1 (Fsingle_key_description (event, Qnil));
2906 /* Print a description of the definition of this character.
2907 elt_describer will take care of spacing out far enough
2908 for alignment purposes. */
2909 (*elt_describer) (definition);
2911 else if (EQ (XCAR (tail), Qkeymap))
2913 /* The same keymap might be in the structure twice, if we're
2914 using an inherited keymap. So skip anything we've already
2915 encountered. */
2916 tem = Fassq (tail, *seen);
2917 if (CONSP (tem) && !NILP (Fequal (XCAR (tem), keys)))
2918 break;
2919 *seen = Fcons (Fcons (tail, keys), *seen);
2923 UNGCPRO;
2926 static void
2927 describe_vector_princ (elt)
2928 Lisp_Object elt;
2930 Findent_to (make_number (16), make_number (1));
2931 Fprinc (elt, Qnil);
2932 Fterpri (Qnil);
2935 DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 1, 0,
2936 "Insert a description of contents of VECTOR.\n\
2937 This is text showing the elements of vector matched against indices.")
2938 (vector)
2939 Lisp_Object vector;
2941 int count = specpdl_ptr - specpdl;
2943 specbind (Qstandard_output, Fcurrent_buffer ());
2944 CHECK_VECTOR_OR_CHAR_TABLE (vector);
2945 describe_vector (vector, Qnil, describe_vector_princ, 0,
2946 Qnil, Qnil, (int *)0, 0);
2948 return unbind_to (count, Qnil);
2951 /* Insert in the current buffer a description of the contents of VECTOR.
2952 We call ELT_DESCRIBER to insert the description of one value found
2953 in VECTOR.
2955 ELT_PREFIX describes what "comes before" the keys or indices defined
2956 by this vector. This is a human-readable string whose size
2957 is not necessarily related to the situation.
2959 If the vector is in a keymap, ELT_PREFIX is a prefix key which
2960 leads to this keymap.
2962 If the vector is a chartable, ELT_PREFIX is the vector
2963 of bytes that lead to the character set or portion of a character
2964 set described by this chartable.
2966 If PARTIAL is nonzero, it means do not mention suppressed commands
2967 (that assumes the vector is in a keymap).
2969 SHADOW is a list of keymaps that shadow this map.
2970 If it is non-nil, then we look up the key in those maps
2971 and we don't mention it now if it is defined by any of them.
2973 ENTIRE_MAP is the keymap in which this vector appears.
2974 If the definition in effect in the whole map does not match
2975 the one in this vector, we ignore this one.
2977 When describing a sub-char-table, INDICES is a list of
2978 indices at higher levels in this char-table,
2979 and CHAR_TABLE_DEPTH says how many levels down we have gone. */
2981 void
2982 describe_vector (vector, elt_prefix, elt_describer,
2983 partial, shadow, entire_map,
2984 indices, char_table_depth)
2985 register Lisp_Object vector;
2986 Lisp_Object elt_prefix;
2987 void (*elt_describer) P_ ((Lisp_Object));
2988 int partial;
2989 Lisp_Object shadow;
2990 Lisp_Object entire_map;
2991 int *indices;
2992 int char_table_depth;
2994 Lisp_Object definition;
2995 Lisp_Object tem2;
2996 register int i;
2997 Lisp_Object suppress;
2998 Lisp_Object kludge;
2999 int first = 1;
3000 struct gcpro gcpro1, gcpro2, gcpro3;
3001 /* Range of elements to be handled. */
3002 int from, to;
3003 /* A flag to tell if a leaf in this level of char-table is not a
3004 generic character (i.e. a complete multibyte character). */
3005 int complete_char;
3006 int character;
3007 int starting_i;
3009 suppress = Qnil;
3011 if (indices == 0)
3012 indices = (int *) alloca (3 * sizeof (int));
3014 definition = Qnil;
3016 /* This vector gets used to present single keys to Flookup_key. Since
3017 that is done once per vector element, we don't want to cons up a
3018 fresh vector every time. */
3019 kludge = Fmake_vector (make_number (1), Qnil);
3020 GCPRO3 (elt_prefix, definition, kludge);
3022 if (partial)
3023 suppress = intern ("suppress-keymap");
3025 if (CHAR_TABLE_P (vector))
3027 if (char_table_depth == 0)
3029 /* VECTOR is a top level char-table. */
3030 complete_char = 1;
3031 from = 0;
3032 to = CHAR_TABLE_ORDINARY_SLOTS;
3034 else
3036 /* VECTOR is a sub char-table. */
3037 if (char_table_depth >= 3)
3038 /* A char-table is never that deep. */
3039 error ("Too deep char table");
3041 complete_char
3042 = (CHARSET_VALID_P (indices[0])
3043 && ((CHARSET_DIMENSION (indices[0]) == 1
3044 && char_table_depth == 1)
3045 || char_table_depth == 2));
3047 /* Meaningful elements are from 32th to 127th. */
3048 from = 32;
3049 to = SUB_CHAR_TABLE_ORDINARY_SLOTS;
3052 else
3054 /* This does the right thing for ordinary vectors. */
3056 complete_char = 1;
3057 from = 0;
3058 to = XVECTOR (vector)->size;
3061 for (i = from; i < to; i++)
3063 QUIT;
3065 if (CHAR_TABLE_P (vector))
3067 if (char_table_depth == 0 && i >= CHAR_TABLE_SINGLE_BYTE_SLOTS)
3068 complete_char = 0;
3070 if (i >= CHAR_TABLE_SINGLE_BYTE_SLOTS
3071 && !CHARSET_DEFINED_P (i - 128))
3072 continue;
3074 definition
3075 = get_keyelt (XCHAR_TABLE (vector)->contents[i], 0);
3077 else
3078 definition = get_keyelt (AREF (vector, i), 0);
3080 if (NILP (definition)) continue;
3082 /* Don't mention suppressed commands. */
3083 if (SYMBOLP (definition) && partial)
3085 Lisp_Object tem;
3087 tem = Fget (definition, suppress);
3089 if (!NILP (tem)) continue;
3092 /* Set CHARACTER to the character this entry describes, if any.
3093 Also update *INDICES. */
3094 if (CHAR_TABLE_P (vector))
3096 indices[char_table_depth] = i;
3098 if (char_table_depth == 0)
3100 character = i;
3101 indices[0] = i - 128;
3103 else if (complete_char)
3105 character = MAKE_CHAR (indices[0], indices[1], indices[2]);
3107 else
3108 character = 0;
3110 else
3111 character = i;
3113 /* If this binding is shadowed by some other map, ignore it. */
3114 if (!NILP (shadow) && complete_char)
3116 Lisp_Object tem;
3118 ASET (kludge, 0, make_number (character));
3119 tem = shadow_lookup (shadow, kludge, Qt);
3121 if (!NILP (tem)) continue;
3124 /* Ignore this definition if it is shadowed by an earlier
3125 one in the same keymap. */
3126 if (!NILP (entire_map) && complete_char)
3128 Lisp_Object tem;
3130 ASET (kludge, 0, make_number (character));
3131 tem = Flookup_key (entire_map, kludge, Qt);
3133 if (!EQ (tem, definition))
3134 continue;
3137 if (first)
3139 if (char_table_depth == 0)
3140 insert ("\n", 1);
3141 first = 0;
3144 /* For a sub char-table, show the depth by indentation.
3145 CHAR_TABLE_DEPTH can be greater than 0 only for a char-table. */
3146 if (char_table_depth > 0)
3147 insert (" ", char_table_depth * 2); /* depth is 1 or 2. */
3149 /* Output the prefix that applies to every entry in this map. */
3150 if (!NILP (elt_prefix))
3151 insert1 (elt_prefix);
3153 /* Insert or describe the character this slot is for,
3154 or a description of what it is for. */
3155 if (SUB_CHAR_TABLE_P (vector))
3157 if (complete_char)
3158 insert_char (character);
3159 else
3161 /* We need an octal representation for this block of
3162 characters. */
3163 char work[16];
3164 sprintf (work, "(row %d)", i);
3165 insert (work, strlen (work));
3168 else if (CHAR_TABLE_P (vector))
3170 if (complete_char)
3171 insert1 (Fsingle_key_description (make_number (character), Qnil));
3172 else
3174 /* Print the information for this character set. */
3175 insert_string ("<");
3176 tem2 = CHARSET_TABLE_INFO (i - 128, CHARSET_SHORT_NAME_IDX);
3177 if (STRINGP (tem2))
3178 insert_from_string (tem2, 0, 0, XSTRING (tem2)->size,
3179 STRING_BYTES (XSTRING (tem2)), 0);
3180 else
3181 insert ("?", 1);
3182 insert (">", 1);
3185 else
3187 insert1 (Fsingle_key_description (make_number (character), Qnil));
3190 /* If we find a sub char-table within a char-table,
3191 scan it recursively; it defines the details for
3192 a character set or a portion of a character set. */
3193 if (CHAR_TABLE_P (vector) && SUB_CHAR_TABLE_P (definition))
3195 insert ("\n", 1);
3196 describe_vector (definition, elt_prefix, elt_describer,
3197 partial, shadow, entire_map,
3198 indices, char_table_depth + 1);
3199 continue;
3202 starting_i = i;
3204 /* Find all consecutive characters or rows that have the same
3205 definition. But, for elements of a top level char table, if
3206 they are for charsets, we had better describe one by one even
3207 if they have the same definition. */
3208 if (CHAR_TABLE_P (vector))
3210 int limit = to;
3212 if (char_table_depth == 0)
3213 limit = CHAR_TABLE_SINGLE_BYTE_SLOTS;
3215 while (i + 1 < limit
3216 && (tem2 = get_keyelt (XCHAR_TABLE (vector)->contents[i + 1], 0),
3217 !NILP (tem2))
3218 && !NILP (Fequal (tem2, definition)))
3219 i++;
3221 else
3222 while (i + 1 < to
3223 && (tem2 = get_keyelt (AREF (vector, i + 1), 0),
3224 !NILP (tem2))
3225 && !NILP (Fequal (tem2, definition)))
3226 i++;
3229 /* If we have a range of more than one character,
3230 print where the range reaches to. */
3232 if (i != starting_i)
3234 insert (" .. ", 4);
3236 if (!NILP (elt_prefix))
3237 insert1 (elt_prefix);
3239 if (CHAR_TABLE_P (vector))
3241 if (char_table_depth == 0)
3243 insert1 (Fsingle_key_description (make_number (i), Qnil));
3245 else if (complete_char)
3247 indices[char_table_depth] = i;
3248 character = MAKE_CHAR (indices[0], indices[1], indices[2]);
3249 insert_char (character);
3251 else
3253 /* We need an octal representation for this block of
3254 characters. */
3255 char work[16];
3256 sprintf (work, "(row %d)", i);
3257 insert (work, strlen (work));
3260 else
3262 insert1 (Fsingle_key_description (make_number (i), Qnil));
3266 /* Print a description of the definition of this character.
3267 elt_describer will take care of spacing out far enough
3268 for alignment purposes. */
3269 (*elt_describer) (definition);
3272 /* For (sub) char-table, print `defalt' slot at last. */
3273 if (CHAR_TABLE_P (vector) && !NILP (XCHAR_TABLE (vector)->defalt))
3275 insert (" ", char_table_depth * 2);
3276 insert_string ("<<default>>");
3277 (*elt_describer) (XCHAR_TABLE (vector)->defalt);
3280 UNGCPRO;
3283 /* Apropos - finding all symbols whose names match a regexp. */
3284 Lisp_Object apropos_predicate;
3285 Lisp_Object apropos_accumulate;
3287 static void
3288 apropos_accum (symbol, string)
3289 Lisp_Object symbol, string;
3291 register Lisp_Object tem;
3293 tem = Fstring_match (string, Fsymbol_name (symbol), Qnil);
3294 if (!NILP (tem) && !NILP (apropos_predicate))
3295 tem = call1 (apropos_predicate, symbol);
3296 if (!NILP (tem))
3297 apropos_accumulate = Fcons (symbol, apropos_accumulate);
3300 DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0,
3301 "Show all symbols whose names contain match for REGEXP.\n\
3302 If optional 2nd arg PREDICATE is non-nil, (funcall PREDICATE SYMBOL) is done\n\
3303 for each symbol and a symbol is mentioned only if that returns non-nil.\n\
3304 Return list of symbols found.")
3305 (regexp, predicate)
3306 Lisp_Object regexp, predicate;
3308 struct gcpro gcpro1, gcpro2;
3309 CHECK_STRING (regexp);
3310 apropos_predicate = predicate;
3311 GCPRO2 (apropos_predicate, apropos_accumulate);
3312 apropos_accumulate = Qnil;
3313 map_obarray (Vobarray, apropos_accum, regexp);
3314 apropos_accumulate = Fsort (apropos_accumulate, Qstring_lessp);
3315 UNGCPRO;
3316 return apropos_accumulate;
3319 void
3320 syms_of_keymap ()
3322 Qkeymap = intern ("keymap");
3323 staticpro (&Qkeymap);
3325 /* Now we are ready to set up this property, so we can
3326 create char tables. */
3327 Fput (Qkeymap, Qchar_table_extra_slots, make_number (0));
3329 /* Initialize the keymaps standardly used.
3330 Each one is the value of a Lisp variable, and is also
3331 pointed to by a C variable */
3333 global_map = Fmake_keymap (Qnil);
3334 Fset (intern ("global-map"), global_map);
3336 current_global_map = global_map;
3337 staticpro (&global_map);
3338 staticpro (&current_global_map);
3340 meta_map = Fmake_keymap (Qnil);
3341 Fset (intern ("esc-map"), meta_map);
3342 Ffset (intern ("ESC-prefix"), meta_map);
3344 control_x_map = Fmake_keymap (Qnil);
3345 Fset (intern ("ctl-x-map"), control_x_map);
3346 Ffset (intern ("Control-X-prefix"), control_x_map);
3348 DEFVAR_LISP ("define-key-rebound-commands", &Vdefine_key_rebound_commands,
3349 "List of commands given new key bindings recently.\n\
3350 This is used for internal purposes during Emacs startup;\n\
3351 don't alter it yourself.");
3352 Vdefine_key_rebound_commands = Qt;
3354 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map,
3355 "Default keymap to use when reading from the minibuffer.");
3356 Vminibuffer_local_map = Fmake_sparse_keymap (Qnil);
3358 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map,
3359 "Local keymap for the minibuffer when spaces are not allowed.");
3360 Vminibuffer_local_ns_map = Fmake_sparse_keymap (Qnil);
3361 Fset_keymap_parent (Vminibuffer_local_ns_map, Vminibuffer_local_map);
3363 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map,
3364 "Local keymap for minibuffer input with completion.");
3365 Vminibuffer_local_completion_map = Fmake_sparse_keymap (Qnil);
3366 Fset_keymap_parent (Vminibuffer_local_completion_map, Vminibuffer_local_map);
3368 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map,
3369 "Local keymap for minibuffer input with completion, for exact match.");
3370 Vminibuffer_local_must_match_map = Fmake_sparse_keymap (Qnil);
3371 Fset_keymap_parent (Vminibuffer_local_must_match_map,
3372 Vminibuffer_local_completion_map);
3374 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist,
3375 "Alist of keymaps to use for minor modes.\n\
3376 Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read\n\
3377 key sequences and look up bindings iff VARIABLE's value is non-nil.\n\
3378 If two active keymaps bind the same key, the keymap appearing earlier\n\
3379 in the list takes precedence.");
3380 Vminor_mode_map_alist = Qnil;
3382 DEFVAR_LISP ("minor-mode-overriding-map-alist", &Vminor_mode_overriding_map_alist,
3383 "Alist of keymaps to use for minor modes, in current major mode.\n\
3384 This variable is a alist just like `minor-mode-map-alist', and it is\n\
3385 used the same way (and before `minor-mode-map-alist'); however,\n\
3386 it is provided for major modes to bind locally.");
3387 Vminor_mode_overriding_map_alist = Qnil;
3389 DEFVAR_LISP ("function-key-map", &Vfunction_key_map,
3390 "Keymap mapping ASCII function key sequences onto their preferred forms.\n\
3391 This allows Emacs to recognize function keys sent from ASCII\n\
3392 terminals at any point in a key sequence.\n\
3394 The `read-key-sequence' function replaces any subsequence bound by\n\
3395 `function-key-map' with its binding. More precisely, when the active\n\
3396 keymaps have no binding for the current key sequence but\n\
3397 `function-key-map' binds a suffix of the sequence to a vector or string,\n\
3398 `read-key-sequence' replaces the matching suffix with its binding, and\n\
3399 continues with the new sequence.\n\
3401 The events that come from bindings in `function-key-map' are not\n\
3402 themselves looked up in `function-key-map'.\n\
3404 For example, suppose `function-key-map' binds `ESC O P' to [f1].\n\
3405 Typing `ESC O P' to `read-key-sequence' would return [f1]. Typing\n\
3406 `C-x ESC O P' would return [?\\C-x f1]. If [f1] were a prefix\n\
3407 key, typing `ESC O P x' would return [f1 x].");
3408 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
3410 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
3411 "Keymap of key translations that can override keymaps.\n\
3412 This keymap works like `function-key-map', but comes after that,\n\
3413 and applies even for keys that have ordinary bindings.");
3414 Vkey_translation_map = Qnil;
3416 Qsingle_key_description = intern ("single-key-description");
3417 staticpro (&Qsingle_key_description);
3419 Qkey_description = intern ("key-description");
3420 staticpro (&Qkey_description);
3422 Qkeymapp = intern ("keymapp");
3423 staticpro (&Qkeymapp);
3425 Qnon_ascii = intern ("non-ascii");
3426 staticpro (&Qnon_ascii);
3428 Qmenu_item = intern ("menu-item");
3429 staticpro (&Qmenu_item);
3431 where_is_cache_keymaps = Qt;
3432 where_is_cache = Qnil;
3433 staticpro (&where_is_cache);
3434 staticpro (&where_is_cache_keymaps);
3436 defsubr (&Skeymapp);
3437 defsubr (&Skeymap_parent);
3438 defsubr (&Skeymap_prompt);
3439 defsubr (&Sset_keymap_parent);
3440 defsubr (&Smake_keymap);
3441 defsubr (&Smake_sparse_keymap);
3442 defsubr (&Scopy_keymap);
3443 defsubr (&Skey_binding);
3444 defsubr (&Slocal_key_binding);
3445 defsubr (&Sglobal_key_binding);
3446 defsubr (&Sminor_mode_key_binding);
3447 defsubr (&Sdefine_key);
3448 defsubr (&Slookup_key);
3449 defsubr (&Sdefine_prefix_command);
3450 defsubr (&Suse_global_map);
3451 defsubr (&Suse_local_map);
3452 defsubr (&Scurrent_local_map);
3453 defsubr (&Scurrent_global_map);
3454 defsubr (&Scurrent_minor_mode_maps);
3455 defsubr (&Scurrent_active_maps);
3456 defsubr (&Saccessible_keymaps);
3457 defsubr (&Skey_description);
3458 defsubr (&Sdescribe_vector);
3459 defsubr (&Ssingle_key_description);
3460 defsubr (&Stext_char_description);
3461 defsubr (&Swhere_is_internal);
3462 defsubr (&Sdescribe_buffer_bindings);
3463 defsubr (&Sapropos_internal);
3466 void
3467 keys_of_keymap ()
3469 initial_define_key (global_map, 033, "ESC-prefix");
3470 initial_define_key (global_map, Ctl('X'), "Control-X-prefix");