*** empty log message ***
[emacs.git] / src / keymap.c
blob64f849f784536d8f1f3e7b9f0d21b5c7d798db4f
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 /* List of emulation mode keymap alists. */
79 Lisp_Object Vemulation_mode_map_alists;
81 /* Keymap mapping ASCII function key sequences onto their preferred forms.
82 Initialized by the terminal-specific lisp files. See DEFVAR for more
83 documentation. */
84 Lisp_Object Vfunction_key_map;
86 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
87 Lisp_Object Vkey_translation_map;
89 /* A list of all commands given new bindings since a certain time
90 when nil was stored here.
91 This is used to speed up recomputation of menu key equivalents
92 when Emacs starts up. t means don't record anything here. */
93 Lisp_Object Vdefine_key_rebound_commands;
95 Lisp_Object Qkeymapp, Qkeymap, Qnon_ascii, Qmenu_item, Qremap;
97 /* Alist of elements like (DEL . "\d"). */
98 static Lisp_Object exclude_keys;
100 /* Pre-allocated 2-element vector for Fcommand_remapping to use. */
101 static Lisp_Object command_remapping_vector;
103 /* A char with the CHAR_META bit set in a vector or the 0200 bit set
104 in a string key sequence is equivalent to prefixing with this
105 character. */
106 extern Lisp_Object meta_prefix_char;
108 extern Lisp_Object Voverriding_local_map;
110 /* Hash table used to cache a reverse-map to speed up calls to where-is. */
111 static Lisp_Object where_is_cache;
112 /* Which keymaps are reverse-stored in the cache. */
113 static Lisp_Object where_is_cache_keymaps;
115 static Lisp_Object store_in_keymap P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
116 static void fix_submap_inheritance P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
118 static Lisp_Object define_as_prefix P_ ((Lisp_Object, Lisp_Object));
119 static void describe_command P_ ((Lisp_Object, Lisp_Object));
120 static void describe_translation P_ ((Lisp_Object, Lisp_Object));
121 static void describe_map P_ ((Lisp_Object, Lisp_Object,
122 void (*) P_ ((Lisp_Object, Lisp_Object)),
123 int, Lisp_Object, Lisp_Object*, int));
124 static void silly_event_symbol_error P_ ((Lisp_Object));
126 /* Keymap object support - constructors and predicates. */
128 DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 1, 0,
129 doc: /* Construct and return a new keymap, of the form (keymap CHARTABLE . ALIST).
130 CHARTABLE is a char-table that holds the bindings for the ASCII
131 characters. ALIST is an assoc-list which holds bindings for function keys,
132 mouse events, and any other things that appear in the input stream.
133 All entries in it are initially nil, meaning "command undefined".
135 The optional arg STRING supplies a menu name for the keymap
136 in case you use it as a menu with `x-popup-menu'. */)
137 (string)
138 Lisp_Object string;
140 Lisp_Object tail;
141 if (!NILP (string))
142 tail = Fcons (string, Qnil);
143 else
144 tail = Qnil;
145 return Fcons (Qkeymap,
146 Fcons (Fmake_char_table (Qkeymap, Qnil), tail));
149 DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 1, 0,
150 doc: /* Construct and return a new sparse keymap.
151 Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),
152 which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),
153 which binds the function key or mouse event SYMBOL to DEFINITION.
154 Initially the alist is nil.
156 The optional arg STRING supplies a menu name for the keymap
157 in case you use it as a menu with `x-popup-menu'. */)
158 (string)
159 Lisp_Object string;
161 if (!NILP (string))
162 return Fcons (Qkeymap, Fcons (string, Qnil));
163 return Fcons (Qkeymap, Qnil);
166 /* This function is used for installing the standard key bindings
167 at initialization time.
169 For example:
171 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); */
173 void
174 initial_define_key (keymap, key, defname)
175 Lisp_Object keymap;
176 int key;
177 char *defname;
179 store_in_keymap (keymap, make_number (key), intern (defname));
182 void
183 initial_define_lispy_key (keymap, keyname, defname)
184 Lisp_Object keymap;
185 char *keyname;
186 char *defname;
188 store_in_keymap (keymap, intern (keyname), intern (defname));
191 DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0,
192 doc: /* Return t if OBJECT is a keymap.
194 A keymap is a list (keymap . ALIST),
195 or a symbol whose function definition is itself a keymap.
196 ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);
197 a vector of densely packed bindings for small character codes
198 is also allowed as an element. */)
199 (object)
200 Lisp_Object object;
202 return (KEYMAPP (object) ? Qt : Qnil);
205 DEFUN ("keymap-prompt", Fkeymap_prompt, Skeymap_prompt, 1, 1, 0,
206 doc: /* Return the prompt-string of a keymap MAP.
207 If non-nil, the prompt is shown in the echo-area
208 when reading a key-sequence to be looked-up in this keymap. */)
209 (map)
210 Lisp_Object map;
212 while (CONSP (map))
214 register Lisp_Object tem;
215 tem = Fcar (map);
216 if (STRINGP (tem))
217 return tem;
218 map = Fcdr (map);
220 return Qnil;
223 /* Check that OBJECT is a keymap (after dereferencing through any
224 symbols). If it is, return it.
226 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
227 is an autoload form, do the autoload and try again.
228 If AUTOLOAD is nonzero, callers must assume GC is possible.
230 If the map needs to be autoloaded, but AUTOLOAD is zero (and ERROR
231 is zero as well), return Qt.
233 ERROR controls how we respond if OBJECT isn't a keymap.
234 If ERROR is non-zero, signal an error; otherwise, just return Qnil.
236 Note that most of the time, we don't want to pursue autoloads.
237 Functions like Faccessible_keymaps which scan entire keymap trees
238 shouldn't load every autoloaded keymap. I'm not sure about this,
239 but it seems to me that only read_key_sequence, Flookup_key, and
240 Fdefine_key should cause keymaps to be autoloaded.
242 This function can GC when AUTOLOAD is non-zero, because it calls
243 do_autoload which can GC. */
245 Lisp_Object
246 get_keymap (object, error, autoload)
247 Lisp_Object object;
248 int error, autoload;
250 Lisp_Object tem;
252 autoload_retry:
253 if (NILP (object))
254 goto end;
255 if (CONSP (object) && EQ (XCAR (object), Qkeymap))
256 return object;
258 tem = indirect_function (object);
259 if (CONSP (tem))
261 if (EQ (XCAR (tem), Qkeymap))
262 return tem;
264 /* Should we do an autoload? Autoload forms for keymaps have
265 Qkeymap as their fifth element. */
266 if ((autoload || !error) && EQ (XCAR (tem), Qautoload))
268 Lisp_Object tail;
270 tail = Fnth (make_number (4), tem);
271 if (EQ (tail, Qkeymap))
273 if (autoload)
275 struct gcpro gcpro1, gcpro2;
277 GCPRO2 (tem, object);
278 do_autoload (tem, object);
279 UNGCPRO;
281 goto autoload_retry;
283 else
284 return Qt;
289 end:
290 if (error)
291 wrong_type_argument (Qkeymapp, object);
292 return Qnil;
295 /* Return the parent map of KEYMAP, or nil if it has none.
296 We assume that KEYMAP is a valid keymap. */
298 Lisp_Object
299 keymap_parent (keymap, autoload)
300 Lisp_Object keymap;
301 int autoload;
303 Lisp_Object list;
305 keymap = get_keymap (keymap, 1, autoload);
307 /* Skip past the initial element `keymap'. */
308 list = XCDR (keymap);
309 for (; CONSP (list); list = XCDR (list))
311 /* See if there is another `keymap'. */
312 if (KEYMAPP (list))
313 return list;
316 return get_keymap (list, 0, autoload);
319 DEFUN ("keymap-parent", Fkeymap_parent, Skeymap_parent, 1, 1, 0,
320 doc: /* Return the parent keymap of KEYMAP. */)
321 (keymap)
322 Lisp_Object keymap;
324 return keymap_parent (keymap, 1);
327 /* Check whether MAP is one of MAPS parents. */
329 keymap_memberp (map, maps)
330 Lisp_Object map, maps;
332 if (NILP (map)) return 0;
333 while (KEYMAPP (maps) && !EQ (map, maps))
334 maps = keymap_parent (maps, 0);
335 return (EQ (map, maps));
338 /* Set the parent keymap of MAP to PARENT. */
340 DEFUN ("set-keymap-parent", Fset_keymap_parent, Sset_keymap_parent, 2, 2, 0,
341 doc: /* Modify KEYMAP to set its parent map to PARENT.
342 PARENT should be nil or another keymap. */)
343 (keymap, parent)
344 Lisp_Object keymap, parent;
346 Lisp_Object list, prev;
347 struct gcpro gcpro1, gcpro2;
348 int i;
350 /* Force a keymap flush for the next call to where-is.
351 Since this can be called from within where-is, we don't set where_is_cache
352 directly but only where_is_cache_keymaps, since where_is_cache shouldn't
353 be changed during where-is, while where_is_cache_keymaps is only used at
354 the very beginning of where-is and can thus be changed here without any
355 adverse effect.
356 This is a very minor correctness (rather than safety) issue. */
357 where_is_cache_keymaps = Qt;
359 GCPRO2 (keymap, parent);
360 keymap = get_keymap (keymap, 1, 1);
362 if (!NILP (parent))
364 parent = get_keymap (parent, 1, 1);
366 /* Check for cycles. */
367 if (keymap_memberp (keymap, parent))
368 error ("Cyclic keymap inheritance");
371 /* Skip past the initial element `keymap'. */
372 prev = keymap;
373 while (1)
375 list = XCDR (prev);
376 /* If there is a parent keymap here, replace it.
377 If we came to the end, add the parent in PREV. */
378 if (!CONSP (list) || KEYMAPP (list))
380 /* If we already have the right parent, return now
381 so that we avoid the loops below. */
382 if (EQ (XCDR (prev), parent))
383 RETURN_UNGCPRO (parent);
385 XSETCDR (prev, parent);
386 break;
388 prev = list;
391 /* Scan through for submaps, and set their parents too. */
393 for (list = XCDR (keymap); CONSP (list); list = XCDR (list))
395 /* Stop the scan when we come to the parent. */
396 if (EQ (XCAR (list), Qkeymap))
397 break;
399 /* If this element holds a prefix map, deal with it. */
400 if (CONSP (XCAR (list))
401 && CONSP (XCDR (XCAR (list))))
402 fix_submap_inheritance (keymap, XCAR (XCAR (list)),
403 XCDR (XCAR (list)));
405 if (VECTORP (XCAR (list)))
406 for (i = 0; i < XVECTOR (XCAR (list))->size; i++)
407 if (CONSP (XVECTOR (XCAR (list))->contents[i]))
408 fix_submap_inheritance (keymap, make_number (i),
409 XVECTOR (XCAR (list))->contents[i]);
411 if (CHAR_TABLE_P (XCAR (list)))
413 Lisp_Object indices[3];
415 map_char_table (fix_submap_inheritance, Qnil,
416 XCAR (list), XCAR (list),
417 keymap, 0, indices);
421 RETURN_UNGCPRO (parent);
424 /* EVENT is defined in MAP as a prefix, and SUBMAP is its definition.
425 if EVENT is also a prefix in MAP's parent,
426 make sure that SUBMAP inherits that definition as its own parent. */
428 static void
429 fix_submap_inheritance (map, event, submap)
430 Lisp_Object map, event, submap;
432 Lisp_Object map_parent, parent_entry;
434 /* SUBMAP is a cons that we found as a key binding.
435 Discard the other things found in a menu key binding. */
437 submap = get_keymap (get_keyelt (submap, 0), 0, 0);
439 /* If it isn't a keymap now, there's no work to do. */
440 if (!CONSP (submap))
441 return;
443 map_parent = keymap_parent (map, 0);
444 if (!NILP (map_parent))
445 parent_entry =
446 get_keymap (access_keymap (map_parent, event, 0, 0, 0), 0, 0);
447 else
448 parent_entry = Qnil;
450 /* If MAP's parent has something other than a keymap,
451 our own submap shadows it completely. */
452 if (!CONSP (parent_entry))
453 return;
455 if (! EQ (parent_entry, submap))
457 Lisp_Object submap_parent;
458 submap_parent = submap;
459 while (1)
461 Lisp_Object tem;
463 tem = keymap_parent (submap_parent, 0);
465 if (KEYMAPP (tem))
467 if (keymap_memberp (tem, parent_entry))
468 /* Fset_keymap_parent could create a cycle. */
469 return;
470 submap_parent = tem;
472 else
473 break;
475 Fset_keymap_parent (submap_parent, parent_entry);
479 /* Look up IDX in MAP. IDX may be any sort of event.
480 Note that this does only one level of lookup; IDX must be a single
481 event, not a sequence.
483 If T_OK is non-zero, bindings for Qt are treated as default
484 bindings; any key left unmentioned by other tables and bindings is
485 given the binding of Qt.
487 If T_OK is zero, bindings for Qt are not treated specially.
489 If NOINHERIT, don't accept a subkeymap found in an inherited keymap. */
491 Lisp_Object
492 access_keymap (map, idx, t_ok, noinherit, autoload)
493 Lisp_Object map;
494 Lisp_Object idx;
495 int t_ok;
496 int noinherit;
497 int autoload;
499 Lisp_Object val;
501 /* Qunbound in VAL means we have found no binding yet. */
502 val = Qunbound;
504 /* If idx is a list (some sort of mouse click, perhaps?),
505 the index we want to use is the car of the list, which
506 ought to be a symbol. */
507 idx = EVENT_HEAD (idx);
509 /* If idx is a symbol, it might have modifiers, which need to
510 be put in the canonical order. */
511 if (SYMBOLP (idx))
512 idx = reorder_modifiers (idx);
513 else if (INTEGERP (idx))
514 /* Clobber the high bits that can be present on a machine
515 with more than 24 bits of integer. */
516 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
518 /* Handle the special meta -> esc mapping. */
519 if (INTEGERP (idx) && XUINT (idx) & meta_modifier)
521 /* See if there is a meta-map. If there's none, there is
522 no binding for IDX, unless a default binding exists in MAP. */
523 struct gcpro gcpro1;
524 Lisp_Object meta_map;
525 GCPRO1 (map);
526 meta_map = get_keymap (access_keymap (map, meta_prefix_char,
527 t_ok, noinherit, autoload),
528 0, autoload);
529 UNGCPRO;
530 if (CONSP (meta_map))
532 map = meta_map;
533 idx = make_number (XUINT (idx) & ~meta_modifier);
535 else if (t_ok)
536 /* Set IDX to t, so that we only find a default binding. */
537 idx = Qt;
538 else
539 /* We know there is no binding. */
540 return Qnil;
543 /* t_binding is where we put a default binding that applies,
544 to use in case we do not find a binding specifically
545 for this key sequence. */
547 Lisp_Object tail;
548 Lisp_Object t_binding = Qnil;
549 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
551 GCPRO4 (map, tail, idx, t_binding);
553 /* If `t_ok' is 2, both `t' and generic-char bindings are accepted.
554 If it is 1, only generic-char bindings are accepted.
555 Otherwise, neither are. */
556 t_ok = t_ok ? 2 : 0;
558 for (tail = XCDR (map);
559 (CONSP (tail)
560 || (tail = get_keymap (tail, 0, autoload), CONSP (tail)));
561 tail = XCDR (tail))
563 Lisp_Object binding;
565 binding = XCAR (tail);
566 if (SYMBOLP (binding))
568 /* If NOINHERIT, stop finding prefix definitions
569 after we pass a second occurrence of the `keymap' symbol. */
570 if (noinherit && EQ (binding, Qkeymap))
571 RETURN_UNGCPRO (Qnil);
573 else if (CONSP (binding))
575 Lisp_Object key = XCAR (binding);
577 if (EQ (key, idx))
578 val = XCDR (binding);
579 else if (t_ok
580 && INTEGERP (idx)
581 && (XINT (idx) & CHAR_MODIFIER_MASK) == 0
582 && INTEGERP (key)
583 && (XINT (key) & CHAR_MODIFIER_MASK) == 0
584 && !SINGLE_BYTE_CHAR_P (XINT (idx))
585 && !SINGLE_BYTE_CHAR_P (XINT (key))
586 && CHAR_VALID_P (XINT (key), 1)
587 && !CHAR_VALID_P (XINT (key), 0)
588 && (CHAR_CHARSET (XINT (key))
589 == CHAR_CHARSET (XINT (idx))))
591 /* KEY is the generic character of the charset of IDX.
592 Use KEY's binding if there isn't a binding for IDX
593 itself. */
594 t_binding = XCDR (binding);
595 t_ok = 0;
597 else if (t_ok > 1 && EQ (key, Qt))
599 t_binding = XCDR (binding);
600 t_ok = 1;
603 else if (VECTORP (binding))
605 if (NATNUMP (idx) && XFASTINT (idx) < ASIZE (binding))
606 val = AREF (binding, XFASTINT (idx));
608 else if (CHAR_TABLE_P (binding))
610 /* Character codes with modifiers
611 are not included in a char-table.
612 All character codes without modifiers are included. */
613 if (NATNUMP (idx) && (XFASTINT (idx) & CHAR_MODIFIER_MASK) == 0)
615 val = Faref (binding, idx);
616 /* `nil' has a special meaning for char-tables, so
617 we use something else to record an explicitly
618 unbound entry. */
619 if (NILP (val))
620 val = Qunbound;
624 /* If we found a binding, clean it up and return it. */
625 if (!EQ (val, Qunbound))
627 if (EQ (val, Qt))
628 /* A Qt binding is just like an explicit nil binding
629 (i.e. it shadows any parent binding but not bindings in
630 keymaps of lower precedence). */
631 val = Qnil;
632 val = get_keyelt (val, autoload);
633 if (KEYMAPP (val))
634 fix_submap_inheritance (map, idx, val);
635 RETURN_UNGCPRO (val);
637 QUIT;
639 UNGCPRO;
640 return get_keyelt (t_binding, autoload);
644 static void
645 map_keymap_item (fun, args, key, val, data)
646 map_keymap_function_t fun;
647 Lisp_Object args, key, val;
648 void *data;
650 /* We should maybe try to detect bindings shadowed by previous
651 ones and things like that. */
652 if (EQ (val, Qt))
653 val = Qnil;
654 (*fun) (key, val, args, data);
657 static void
658 map_keymap_char_table_item (args, key, val)
659 Lisp_Object args, key, val;
661 if (!NILP (val))
663 map_keymap_function_t fun = XSAVE_VALUE (XCAR (args))->pointer;
664 args = XCDR (args);
665 map_keymap_item (fun, XCDR (args), key, val,
666 XSAVE_VALUE (XCAR (args))->pointer);
670 /* Call FUN for every binding in MAP.
671 FUN is called with 4 arguments: FUN (KEY, BINDING, ARGS, DATA).
672 AUTOLOAD if non-zero means that we can autoload keymaps if necessary. */
673 void
674 map_keymap (map, fun, args, data, autoload)
675 map_keymap_function_t fun;
676 Lisp_Object map, args;
677 void *data;
678 int autoload;
680 struct gcpro gcpro1, gcpro2, gcpro3;
681 Lisp_Object tail;
683 GCPRO3 (map, args, tail);
684 map = get_keymap (map, 1, autoload);
685 for (tail = (CONSP (map) && EQ (Qkeymap, XCAR (map))) ? XCDR (map) : map;
686 CONSP (tail) || (tail = get_keymap (tail, 0, autoload), CONSP (tail));
687 tail = XCDR (tail))
689 Lisp_Object binding = XCAR (tail);
691 if (CONSP (binding))
692 map_keymap_item (fun, args, XCAR (binding), XCDR (binding), data);
693 else if (VECTORP (binding))
695 /* Loop over the char values represented in the vector. */
696 int len = ASIZE (binding);
697 int c;
698 abort();
699 for (c = 0; c < len; c++)
701 Lisp_Object character;
702 XSETFASTINT (character, c);
703 map_keymap_item (fun, args, character, AREF (binding, c), data);
706 else if (CHAR_TABLE_P (binding))
708 Lisp_Object indices[3];
709 map_char_table (map_keymap_char_table_item, Qnil, binding, binding,
710 Fcons (make_save_value (fun, 0),
711 Fcons (make_save_value (data, 0),
712 args)),
713 0, indices);
716 UNGCPRO;
719 static void
720 map_keymap_call (key, val, fun, dummy)
721 Lisp_Object key, val, fun;
722 void *dummy;
724 call2 (fun, key, val);
727 DEFUN ("map-keymap", Fmap_keymap, Smap_keymap, 2, 2, 0,
728 doc: /* Call FUNCTION for every binding in KEYMAP.
729 FUNCTION is called with two arguments: the event and its binding. */)
730 (function, keymap)
731 Lisp_Object function, keymap;
733 if (INTEGERP (function))
734 /* We have to stop integers early since map_keymap gives them special
735 significance. */
736 Fsignal (Qinvalid_function, Fcons (function, Qnil));
737 map_keymap (keymap, map_keymap_call, function, NULL, 1);
738 return Qnil;
741 /* Given OBJECT which was found in a slot in a keymap,
742 trace indirect definitions to get the actual definition of that slot.
743 An indirect definition is a list of the form
744 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
745 and INDEX is the object to look up in KEYMAP to yield the definition.
747 Also if OBJECT has a menu string as the first element,
748 remove that. Also remove a menu help string as second element.
750 If AUTOLOAD is nonzero, load autoloadable keymaps
751 that are referred to with indirection. */
753 Lisp_Object
754 get_keyelt (object, autoload)
755 Lisp_Object object;
756 int autoload;
758 while (1)
760 if (!(CONSP (object)))
761 /* This is really the value. */
762 return object;
764 /* If the keymap contents looks like (keymap ...) or (lambda ...)
765 then use itself. */
766 else if (EQ (XCAR (object), Qkeymap) || EQ (XCAR (object), Qlambda))
767 return object;
769 /* If the keymap contents looks like (menu-item name . DEFN)
770 or (menu-item name DEFN ...) then use DEFN.
771 This is a new format menu item. */
772 else if (EQ (XCAR (object), Qmenu_item))
774 if (CONSP (XCDR (object)))
776 Lisp_Object tem;
778 object = XCDR (XCDR (object));
779 tem = object;
780 if (CONSP (object))
781 object = XCAR (object);
783 /* If there's a `:filter FILTER', apply FILTER to the
784 menu-item's definition to get the real definition to
785 use. */
786 for (; CONSP (tem) && CONSP (XCDR (tem)); tem = XCDR (tem))
787 if (EQ (XCAR (tem), QCfilter) && autoload)
789 Lisp_Object filter;
790 filter = XCAR (XCDR (tem));
791 filter = list2 (filter, list2 (Qquote, object));
792 object = menu_item_eval_property (filter);
793 break;
796 else
797 /* Invalid keymap. */
798 return object;
801 /* If the keymap contents looks like (STRING . DEFN), use DEFN.
802 Keymap alist elements like (CHAR MENUSTRING . DEFN)
803 will be used by HierarKey menus. */
804 else if (STRINGP (XCAR (object)))
806 object = XCDR (object);
807 /* Also remove a menu help string, if any,
808 following the menu item name. */
809 if (CONSP (object) && STRINGP (XCAR (object)))
810 object = XCDR (object);
811 /* Also remove the sublist that caches key equivalences, if any. */
812 if (CONSP (object) && CONSP (XCAR (object)))
814 Lisp_Object carcar;
815 carcar = XCAR (XCAR (object));
816 if (NILP (carcar) || VECTORP (carcar))
817 object = XCDR (object);
821 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
822 else
824 struct gcpro gcpro1;
825 Lisp_Object map;
826 GCPRO1 (object);
827 map = get_keymap (Fcar_safe (object), 0, autoload);
828 UNGCPRO;
829 return (!CONSP (map) ? object /* Invalid keymap */
830 : access_keymap (map, Fcdr (object), 0, 0, autoload));
835 static Lisp_Object
836 store_in_keymap (keymap, idx, def)
837 Lisp_Object keymap;
838 register Lisp_Object idx;
839 register Lisp_Object def;
841 /* Flush any reverse-map cache. */
842 where_is_cache = Qnil;
843 where_is_cache_keymaps = Qt;
845 /* If we are preparing to dump, and DEF is a menu element
846 with a menu item indicator, copy it to ensure it is not pure. */
847 if (CONSP (def) && PURE_P (def)
848 && (EQ (XCAR (def), Qmenu_item) || STRINGP (XCAR (def))))
849 def = Fcons (XCAR (def), XCDR (def));
851 if (!CONSP (keymap) || !EQ (XCAR (keymap), Qkeymap))
852 error ("attempt to define a key in a non-keymap");
854 /* If idx is a list (some sort of mouse click, perhaps?),
855 the index we want to use is the car of the list, which
856 ought to be a symbol. */
857 idx = EVENT_HEAD (idx);
859 /* If idx is a symbol, it might have modifiers, which need to
860 be put in the canonical order. */
861 if (SYMBOLP (idx))
862 idx = reorder_modifiers (idx);
863 else if (INTEGERP (idx))
864 /* Clobber the high bits that can be present on a machine
865 with more than 24 bits of integer. */
866 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
868 /* Scan the keymap for a binding of idx. */
870 Lisp_Object tail;
872 /* The cons after which we should insert new bindings. If the
873 keymap has a table element, we record its position here, so new
874 bindings will go after it; this way, the table will stay
875 towards the front of the alist and character lookups in dense
876 keymaps will remain fast. Otherwise, this just points at the
877 front of the keymap. */
878 Lisp_Object insertion_point;
880 insertion_point = keymap;
881 for (tail = XCDR (keymap); CONSP (tail); tail = XCDR (tail))
883 Lisp_Object elt;
885 elt = XCAR (tail);
886 if (VECTORP (elt))
888 if (NATNUMP (idx) && XFASTINT (idx) < ASIZE (elt))
890 ASET (elt, XFASTINT (idx), def);
891 return def;
893 insertion_point = tail;
895 else if (CHAR_TABLE_P (elt))
897 /* Character codes with modifiers
898 are not included in a char-table.
899 All character codes without modifiers are included. */
900 if (NATNUMP (idx) && !(XFASTINT (idx) & CHAR_MODIFIER_MASK))
902 Faset (elt, idx,
903 /* `nil' has a special meaning for char-tables, so
904 we use something else to record an explicitly
905 unbound entry. */
906 NILP (def) ? Qt : def);
907 return def;
909 insertion_point = tail;
911 else if (CONSP (elt))
913 if (EQ (idx, XCAR (elt)))
915 XSETCDR (elt, def);
916 return def;
919 else if (EQ (elt, Qkeymap))
920 /* If we find a 'keymap' symbol in the spine of KEYMAP,
921 then we must have found the start of a second keymap
922 being used as the tail of KEYMAP, and a binding for IDX
923 should be inserted before it. */
924 goto keymap_end;
926 QUIT;
929 keymap_end:
930 /* We have scanned the entire keymap, and not found a binding for
931 IDX. Let's add one. */
932 XSETCDR (insertion_point,
933 Fcons (Fcons (idx, def), XCDR (insertion_point)));
936 return def;
939 EXFUN (Fcopy_keymap, 1);
941 Lisp_Object
942 copy_keymap_item (elt)
943 Lisp_Object elt;
945 Lisp_Object res, tem;
947 if (!CONSP (elt))
948 return elt;
950 res = tem = elt;
952 /* Is this a new format menu item. */
953 if (EQ (XCAR (tem), Qmenu_item))
955 /* Copy cell with menu-item marker. */
956 res = elt = Fcons (XCAR (tem), XCDR (tem));
957 tem = XCDR (elt);
958 if (CONSP (tem))
960 /* Copy cell with menu-item name. */
961 XSETCDR (elt, Fcons (XCAR (tem), XCDR (tem)));
962 elt = XCDR (elt);
963 tem = XCDR (elt);
965 if (CONSP (tem))
967 /* Copy cell with binding and if the binding is a keymap,
968 copy that. */
969 XSETCDR (elt, Fcons (XCAR (tem), XCDR (tem)));
970 elt = XCDR (elt);
971 tem = XCAR (elt);
972 if (CONSP (tem) && EQ (XCAR (tem), Qkeymap))
973 XSETCAR (elt, Fcopy_keymap (tem));
974 tem = XCDR (elt);
975 if (CONSP (tem) && CONSP (XCAR (tem)))
976 /* Delete cache for key equivalences. */
977 XSETCDR (elt, XCDR (tem));
980 else
982 /* It may be an old fomat menu item.
983 Skip the optional menu string. */
984 if (STRINGP (XCAR (tem)))
986 /* Copy the cell, since copy-alist didn't go this deep. */
987 res = elt = Fcons (XCAR (tem), XCDR (tem));
988 tem = XCDR (elt);
989 /* Also skip the optional menu help string. */
990 if (CONSP (tem) && STRINGP (XCAR (tem)))
992 XSETCDR (elt, Fcons (XCAR (tem), XCDR (tem)));
993 elt = XCDR (elt);
994 tem = XCDR (elt);
996 /* There may also be a list that caches key equivalences.
997 Just delete it for the new keymap. */
998 if (CONSP (tem)
999 && CONSP (XCAR (tem))
1000 && (NILP (XCAR (XCAR (tem)))
1001 || VECTORP (XCAR (XCAR (tem)))))
1003 XSETCDR (elt, XCDR (tem));
1004 tem = XCDR (tem);
1006 if (CONSP (tem) && EQ (XCAR (tem), Qkeymap))
1007 XSETCDR (elt, Fcopy_keymap (tem));
1009 else if (EQ (XCAR (tem), Qkeymap))
1010 res = Fcopy_keymap (elt);
1012 return res;
1015 static void
1016 copy_keymap_1 (chartable, idx, elt)
1017 Lisp_Object chartable, idx, elt;
1019 Faset (chartable, idx, copy_keymap_item (elt));
1022 DEFUN ("copy-keymap", Fcopy_keymap, Scopy_keymap, 1, 1, 0,
1023 doc: /* Return a copy of the keymap KEYMAP.
1024 The copy starts out with the same definitions of KEYMAP,
1025 but changing either the copy or KEYMAP does not affect the other.
1026 Any key definitions that are subkeymaps are recursively copied.
1027 However, a key definition which is a symbol whose definition is a keymap
1028 is not copied. */)
1029 (keymap)
1030 Lisp_Object keymap;
1032 register Lisp_Object copy, tail;
1033 keymap = get_keymap (keymap, 1, 0);
1034 copy = tail = Fcons (Qkeymap, Qnil);
1035 keymap = XCDR (keymap); /* Skip the `keymap' symbol. */
1037 while (CONSP (keymap) && !EQ (XCAR (keymap), Qkeymap))
1039 Lisp_Object elt = XCAR (keymap);
1040 if (CHAR_TABLE_P (elt))
1042 Lisp_Object indices[3];
1043 elt = Fcopy_sequence (elt);
1044 map_char_table (copy_keymap_1, Qnil, elt, elt, elt, 0, indices);
1046 else if (VECTORP (elt))
1048 int i;
1049 elt = Fcopy_sequence (elt);
1050 for (i = 0; i < ASIZE (elt); i++)
1051 ASET (elt, i, copy_keymap_item (AREF (elt, i)));
1053 else if (CONSP (elt))
1054 elt = Fcons (XCAR (elt), copy_keymap_item (XCDR (elt)));
1055 XSETCDR (tail, Fcons (elt, Qnil));
1056 tail = XCDR (tail);
1057 keymap = XCDR (keymap);
1059 XSETCDR (tail, keymap);
1060 return copy;
1063 /* Simple Keymap mutators and accessors. */
1065 /* GC is possible in this function if it autoloads a keymap. */
1067 DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0,
1068 doc: /* In KEYMAP, define key sequence KEY as DEF.
1069 KEYMAP is a keymap.
1071 KEY is a string or a vector of symbols and characters meaning a
1072 sequence of keystrokes and events. Non-ASCII characters with codes
1073 above 127 (such as ISO Latin-1) can be included if you use a vector.
1074 Using [t] for KEY creates a default definition, which applies to any
1075 event type that has no other definition in this keymap.
1077 DEF is anything that can be a key's definition:
1078 nil (means key is undefined in this keymap),
1079 a command (a Lisp function suitable for interactive calling)
1080 a string (treated as a keyboard macro),
1081 a keymap (to define a prefix key),
1082 a symbol. When the key is looked up, the symbol will stand for its
1083 function definition, which should at that time be one of the above,
1084 or another symbol whose function definition is used, etc.
1085 a cons (STRING . DEFN), meaning that DEFN is the definition
1086 (DEFN should be a valid definition in its own right),
1087 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.
1089 If KEYMAP is a sparse keymap with a binding for KEY, the existing
1090 binding is altered. If there is no binding for KEY, the new pair
1091 binding KEY to DEF is added at the front of KEYMAP. */)
1092 (keymap, key, def)
1093 Lisp_Object keymap;
1094 Lisp_Object key;
1095 Lisp_Object def;
1097 register int idx;
1098 register Lisp_Object c;
1099 register Lisp_Object cmd;
1100 int metized = 0;
1101 int meta_bit;
1102 int length;
1103 struct gcpro gcpro1, gcpro2, gcpro3;
1105 GCPRO3 (keymap, key, def);
1106 keymap = get_keymap (keymap, 1, 1);
1108 if (!VECTORP (key) && !STRINGP (key))
1109 key = wrong_type_argument (Qarrayp, key);
1111 length = XFASTINT (Flength (key));
1112 if (length == 0)
1113 RETURN_UNGCPRO (Qnil);
1115 if (SYMBOLP (def) && !EQ (Vdefine_key_rebound_commands, Qt))
1116 Vdefine_key_rebound_commands = Fcons (def, Vdefine_key_rebound_commands);
1118 meta_bit = VECTORP (key) ? meta_modifier : 0x80;
1120 idx = 0;
1121 while (1)
1123 c = Faref (key, make_number (idx));
1125 if (CONSP (c) && lucid_event_type_list_p (c))
1126 c = Fevent_convert_list (c);
1128 if (SYMBOLP (c))
1129 silly_event_symbol_error (c);
1131 if (INTEGERP (c)
1132 && (XINT (c) & meta_bit)
1133 && !metized)
1135 c = meta_prefix_char;
1136 metized = 1;
1138 else
1140 if (INTEGERP (c))
1141 XSETINT (c, XINT (c) & ~meta_bit);
1143 metized = 0;
1144 idx++;
1147 if (!INTEGERP (c) && !SYMBOLP (c) && !CONSP (c))
1148 error ("Key sequence contains invalid event");
1150 if (idx == length)
1151 RETURN_UNGCPRO (store_in_keymap (keymap, c, def));
1153 cmd = access_keymap (keymap, c, 0, 1, 1);
1155 /* If this key is undefined, make it a prefix. */
1156 if (NILP (cmd))
1157 cmd = define_as_prefix (keymap, c);
1159 keymap = get_keymap (cmd, 0, 1);
1160 if (!CONSP (keymap))
1161 /* We must use Fkey_description rather than just passing key to
1162 error; key might be a vector, not a string. */
1163 error ("Key sequence %s uses invalid prefix characters",
1164 SDATA (Fkey_description (key)));
1168 /* This function may GC (it calls Fkey_binding). */
1170 DEFUN ("command-remapping", Fcommand_remapping, Scommand_remapping, 1, 1, 0,
1171 doc: /* Return the remapping for command COMMAND in current keymaps.
1172 Returns nil if COMMAND is not remapped (or not a symbol). */)
1173 (command)
1174 Lisp_Object command;
1176 if (!SYMBOLP (command))
1177 return Qnil;
1179 ASET (command_remapping_vector, 1, command);
1180 return Fkey_binding (command_remapping_vector, Qnil, Qt);
1183 /* Value is number if KEY is too long; nil if valid but has no definition. */
1184 /* GC is possible in this function if it autoloads a keymap. */
1186 DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 3, 0,
1187 doc: /* In keymap KEYMAP, look up key sequence KEY. Return the definition.
1188 nil means undefined. See doc of `define-key' for kinds of definitions.
1190 A number as value means KEY is "too long";
1191 that is, characters or symbols in it except for the last one
1192 fail to be a valid sequence of prefix characters in KEYMAP.
1193 The number is how many characters at the front of KEY
1194 it takes to reach a non-prefix command.
1196 Normally, `lookup-key' ignores bindings for t, which act as default
1197 bindings, used when nothing else in the keymap applies; this makes it
1198 usable as a general function for probing keymaps. However, if the
1199 third optional argument ACCEPT-DEFAULT is non-nil, `lookup-key' will
1200 recognize the default bindings, just as `read-key-sequence' does. */)
1201 (keymap, key, accept_default)
1202 Lisp_Object keymap;
1203 Lisp_Object key;
1204 Lisp_Object accept_default;
1206 register int idx;
1207 register Lisp_Object cmd;
1208 register Lisp_Object c;
1209 int length;
1210 int t_ok = !NILP (accept_default);
1211 struct gcpro gcpro1, gcpro2;
1213 GCPRO2 (keymap, key);
1214 keymap = get_keymap (keymap, 1, 1);
1216 if (!VECTORP (key) && !STRINGP (key))
1217 key = wrong_type_argument (Qarrayp, key);
1219 length = XFASTINT (Flength (key));
1220 if (length == 0)
1221 RETURN_UNGCPRO (keymap);
1223 idx = 0;
1224 while (1)
1226 c = Faref (key, make_number (idx++));
1228 if (CONSP (c) && lucid_event_type_list_p (c))
1229 c = Fevent_convert_list (c);
1231 /* Turn the 8th bit of string chars into a meta modifier. */
1232 if (XINT (c) & 0x80 && STRINGP (key))
1233 XSETINT (c, (XINT (c) | meta_modifier) & ~0x80);
1235 /* Allow string since binding for `menu-bar-select-buffer'
1236 includes the buffer name in the key sequence. */
1237 if (!INTEGERP (c) && !SYMBOLP (c) && !CONSP (c) && !STRINGP (c))
1238 error ("Key sequence contains invalid event");
1240 cmd = access_keymap (keymap, c, t_ok, 0, 1);
1241 if (idx == length)
1242 RETURN_UNGCPRO (cmd);
1244 keymap = get_keymap (cmd, 0, 1);
1245 if (!CONSP (keymap))
1246 RETURN_UNGCPRO (make_number (idx));
1248 QUIT;
1252 /* Make KEYMAP define event C as a keymap (i.e., as a prefix).
1253 Assume that currently it does not define C at all.
1254 Return the keymap. */
1256 static Lisp_Object
1257 define_as_prefix (keymap, c)
1258 Lisp_Object keymap, c;
1260 Lisp_Object cmd;
1262 cmd = Fmake_sparse_keymap (Qnil);
1263 /* If this key is defined as a prefix in an inherited keymap,
1264 make it a prefix in this map, and make its definition
1265 inherit the other prefix definition. */
1266 cmd = nconc2 (cmd, access_keymap (keymap, c, 0, 0, 0));
1267 store_in_keymap (keymap, c, cmd);
1269 return cmd;
1272 /* Append a key to the end of a key sequence. We always make a vector. */
1274 Lisp_Object
1275 append_key (key_sequence, key)
1276 Lisp_Object key_sequence, key;
1278 Lisp_Object args[2];
1280 args[0] = key_sequence;
1282 args[1] = Fcons (key, Qnil);
1283 return Fvconcat (2, args);
1286 /* Given a event type C which is a symbol,
1287 signal an error if is a mistake such as RET or M-RET or C-DEL, etc. */
1289 static void
1290 silly_event_symbol_error (c)
1291 Lisp_Object c;
1293 Lisp_Object parsed, base, name, assoc;
1294 int modifiers;
1296 parsed = parse_modifiers (c);
1297 modifiers = (int) XUINT (XCAR (XCDR (parsed)));
1298 base = XCAR (parsed);
1299 name = Fsymbol_name (base);
1300 /* This alist includes elements such as ("RET" . "\\r"). */
1301 assoc = Fassoc (name, exclude_keys);
1303 if (! NILP (assoc))
1305 char new_mods[sizeof ("\\A-\\C-\\H-\\M-\\S-\\s-")];
1306 char *p = new_mods;
1307 Lisp_Object keystring;
1308 if (modifiers & alt_modifier)
1309 { *p++ = '\\'; *p++ = 'A'; *p++ = '-'; }
1310 if (modifiers & ctrl_modifier)
1311 { *p++ = '\\'; *p++ = 'C'; *p++ = '-'; }
1312 if (modifiers & hyper_modifier)
1313 { *p++ = '\\'; *p++ = 'H'; *p++ = '-'; }
1314 if (modifiers & meta_modifier)
1315 { *p++ = '\\'; *p++ = 'M'; *p++ = '-'; }
1316 if (modifiers & shift_modifier)
1317 { *p++ = '\\'; *p++ = 'S'; *p++ = '-'; }
1318 if (modifiers & super_modifier)
1319 { *p++ = '\\'; *p++ = 's'; *p++ = '-'; }
1320 *p = 0;
1322 c = reorder_modifiers (c);
1323 keystring = concat2 (build_string (new_mods), XCDR (assoc));
1325 error ((modifiers & ~meta_modifier
1326 ? "To bind the key %s, use [?%s], not [%s]"
1327 : "To bind the key %s, use \"%s\", not [%s]"),
1328 SDATA (SYMBOL_NAME (c)), SDATA (keystring),
1329 SDATA (SYMBOL_NAME (c)));
1333 /* Global, local, and minor mode keymap stuff. */
1335 /* We can't put these variables inside current_minor_maps, since under
1336 some systems, static gets macro-defined to be the empty string.
1337 Ickypoo. */
1338 static Lisp_Object *cmm_modes = NULL, *cmm_maps = NULL;
1339 static int cmm_size = 0;
1341 /* Error handler used in current_minor_maps. */
1342 static Lisp_Object
1343 current_minor_maps_error ()
1345 return Qnil;
1348 /* Store a pointer to an array of the keymaps of the currently active
1349 minor modes in *buf, and return the number of maps it contains.
1351 This function always returns a pointer to the same buffer, and may
1352 free or reallocate it, so if you want to keep it for a long time or
1353 hand it out to lisp code, copy it. This procedure will be called
1354 for every key sequence read, so the nice lispy approach (return a
1355 new assoclist, list, what have you) for each invocation would
1356 result in a lot of consing over time.
1358 If we used xrealloc/xmalloc and ran out of memory, they would throw
1359 back to the command loop, which would try to read a key sequence,
1360 which would call this function again, resulting in an infinite
1361 loop. Instead, we'll use realloc/malloc and silently truncate the
1362 list, let the key sequence be read, and hope some other piece of
1363 code signals the error. */
1365 current_minor_maps (modeptr, mapptr)
1366 Lisp_Object **modeptr, **mapptr;
1368 int i = 0;
1369 int list_number = 0;
1370 Lisp_Object alist, assoc, var, val;
1371 Lisp_Object emulation_alists;
1372 Lisp_Object lists[2];
1374 emulation_alists = Vemulation_mode_map_alists;
1375 lists[0] = Vminor_mode_overriding_map_alist;
1376 lists[1] = Vminor_mode_map_alist;
1378 for (list_number = 0; list_number < 2; list_number++)
1380 if (CONSP (emulation_alists))
1382 alist = XCAR (emulation_alists);
1383 emulation_alists = XCDR (emulation_alists);
1384 if (SYMBOLP (alist))
1385 alist = find_symbol_value (alist);
1386 list_number = -1;
1388 else
1389 alist = lists[list_number];
1391 for ( ; CONSP (alist); alist = XCDR (alist))
1392 if ((assoc = XCAR (alist), CONSP (assoc))
1393 && (var = XCAR (assoc), SYMBOLP (var))
1394 && (val = find_symbol_value (var), !EQ (val, Qunbound))
1395 && !NILP (val))
1397 Lisp_Object temp;
1399 /* If a variable has an entry in Vminor_mode_overriding_map_alist,
1400 and also an entry in Vminor_mode_map_alist,
1401 ignore the latter. */
1402 if (list_number == 1)
1404 val = assq_no_quit (var, lists[0]);
1405 if (!NILP (val))
1406 continue;
1409 if (i >= cmm_size)
1411 int newsize, allocsize;
1412 Lisp_Object *newmodes, *newmaps;
1414 newsize = cmm_size == 0 ? 30 : cmm_size * 2;
1415 allocsize = newsize * sizeof *newmodes;
1417 /* Use malloc here. See the comment above this function.
1418 Avoid realloc here; it causes spurious traps on GNU/Linux [KFS] */
1419 BLOCK_INPUT;
1420 newmodes = (Lisp_Object *) malloc (allocsize);
1421 if (newmodes)
1423 if (cmm_modes)
1425 bcopy (cmm_modes, newmodes, cmm_size * sizeof cmm_modes[0]);
1426 free (cmm_modes);
1428 cmm_modes = newmodes;
1431 newmaps = (Lisp_Object *) malloc (allocsize);
1432 if (newmaps)
1434 if (cmm_maps)
1436 bcopy (cmm_maps, newmaps, cmm_size * sizeof cmm_maps[0]);
1437 free (cmm_maps);
1439 cmm_maps = newmaps;
1441 UNBLOCK_INPUT;
1443 if (newmodes == NULL || newmaps == NULL)
1444 break;
1445 cmm_size = newsize;
1448 /* Get the keymap definition--or nil if it is not defined. */
1449 temp = internal_condition_case_1 (Findirect_function,
1450 XCDR (assoc),
1451 Qerror, current_minor_maps_error);
1452 if (!NILP (temp))
1454 cmm_modes[i] = var;
1455 cmm_maps [i] = temp;
1456 i++;
1461 if (modeptr) *modeptr = cmm_modes;
1462 if (mapptr) *mapptr = cmm_maps;
1463 return i;
1466 DEFUN ("current-active-maps", Fcurrent_active_maps, Scurrent_active_maps,
1467 0, 1, 0,
1468 doc: /* Return a list of the currently active keymaps.
1469 OLP if non-nil indicates that we should obey `overriding-local-map' and
1470 `overriding-terminal-local-map'. */)
1471 (olp)
1472 Lisp_Object olp;
1474 Lisp_Object keymaps = Fcons (current_global_map, Qnil);
1476 if (!NILP (olp))
1478 if (!NILP (Voverriding_local_map))
1479 keymaps = Fcons (Voverriding_local_map, keymaps);
1480 if (!NILP (current_kboard->Voverriding_terminal_local_map))
1481 keymaps = Fcons (current_kboard->Voverriding_terminal_local_map, keymaps);
1483 if (NILP (XCDR (keymaps)))
1485 Lisp_Object local;
1486 Lisp_Object *maps;
1487 int nmaps, i;
1489 local = get_local_map (PT, current_buffer, Qlocal_map);
1490 if (!NILP (local))
1491 keymaps = Fcons (local, keymaps);
1493 nmaps = current_minor_maps (0, &maps);
1495 for (i = --nmaps; i >= 0; i--)
1496 if (!NILP (maps[i]))
1497 keymaps = Fcons (maps[i], keymaps);
1499 local = get_local_map (PT, current_buffer, Qkeymap);
1500 if (!NILP (local))
1501 keymaps = Fcons (local, keymaps);
1504 return keymaps;
1507 /* GC is possible in this function if it autoloads a keymap. */
1509 DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 3, 0,
1510 doc: /* Return the binding for command KEY in current keymaps.
1511 KEY is a string or vector, a sequence of keystrokes.
1512 The binding is probably a symbol with a function definition.
1514 Normally, `key-binding' ignores bindings for t, which act as default
1515 bindings, used when nothing else in the keymap applies; this makes it
1516 usable as a general function for probing keymaps. However, if the
1517 optional second argument ACCEPT-DEFAULT is non-nil, `key-binding' does
1518 recognize the default bindings, just as `read-key-sequence' does.
1520 Like the normal command loop, `key-binding' will remap the command
1521 resulting from looking up KEY by looking up the command in the
1522 current keymaps. However, if the optional third argument NO-REMAP
1523 is non-nil, `key-binding' returns the unmapped command. */)
1524 (key, accept_default, no_remap)
1525 Lisp_Object key, accept_default, no_remap;
1527 Lisp_Object *maps, value;
1528 int nmaps, i;
1529 struct gcpro gcpro1;
1531 GCPRO1 (key);
1533 if (!NILP (current_kboard->Voverriding_terminal_local_map))
1535 value = Flookup_key (current_kboard->Voverriding_terminal_local_map,
1536 key, accept_default);
1537 if (! NILP (value) && !INTEGERP (value))
1538 goto done;
1540 else if (!NILP (Voverriding_local_map))
1542 value = Flookup_key (Voverriding_local_map, key, accept_default);
1543 if (! NILP (value) && !INTEGERP (value))
1544 goto done;
1546 else
1548 Lisp_Object local;
1550 local = get_local_map (PT, current_buffer, Qkeymap);
1551 if (! NILP (local))
1553 value = Flookup_key (local, key, accept_default);
1554 if (! NILP (value) && !INTEGERP (value))
1555 goto done;
1558 nmaps = current_minor_maps (0, &maps);
1559 /* Note that all these maps are GCPRO'd
1560 in the places where we found them. */
1562 for (i = 0; i < nmaps; i++)
1563 if (! NILP (maps[i]))
1565 value = Flookup_key (maps[i], key, accept_default);
1566 if (! NILP (value) && !INTEGERP (value))
1567 goto done;
1570 local = get_local_map (PT, current_buffer, Qlocal_map);
1571 if (! NILP (local))
1573 value = Flookup_key (local, key, accept_default);
1574 if (! NILP (value) && !INTEGERP (value))
1575 goto done;
1579 value = Flookup_key (current_global_map, key, accept_default);
1581 done:
1582 UNGCPRO;
1583 if (NILP (value) || INTEGERP (value))
1584 return Qnil;
1586 /* If the result of the ordinary keymap lookup is an interactive
1587 command, look for a key binding (ie. remapping) for that command. */
1589 if (NILP (no_remap) && SYMBOLP (value))
1591 Lisp_Object value1;
1592 if (value1 = Fcommand_remapping (value), !NILP (value1))
1593 value = value1;
1596 return value;
1599 /* GC is possible in this function if it autoloads a keymap. */
1601 DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 2, 0,
1602 doc: /* Return the binding for command KEYS in current local keymap only.
1603 KEYS is a string, a sequence of keystrokes.
1604 The binding is probably a symbol with a function definition.
1606 If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1607 bindings; see the description of `lookup-key' for more details about this. */)
1608 (keys, accept_default)
1609 Lisp_Object keys, accept_default;
1611 register Lisp_Object map;
1612 map = current_buffer->keymap;
1613 if (NILP (map))
1614 return Qnil;
1615 return Flookup_key (map, keys, accept_default);
1618 /* GC is possible in this function if it autoloads a keymap. */
1620 DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 2, 0,
1621 doc: /* Return the binding for command KEYS in current global keymap only.
1622 KEYS is a string, a sequence of keystrokes.
1623 The binding is probably a symbol with a function definition.
1624 This function's return values are the same as those of lookup-key
1625 \(which see).
1627 If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1628 bindings; see the description of `lookup-key' for more details about this. */)
1629 (keys, accept_default)
1630 Lisp_Object keys, accept_default;
1632 return Flookup_key (current_global_map, keys, accept_default);
1635 /* GC is possible in this function if it autoloads a keymap. */
1637 DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0,
1638 doc: /* Find the visible minor mode bindings of KEY.
1639 Return an alist of pairs (MODENAME . BINDING), where MODENAME is the
1640 the symbol which names the minor mode binding KEY, and BINDING is
1641 KEY's definition in that mode. In particular, if KEY has no
1642 minor-mode bindings, return nil. If the first binding is a
1643 non-prefix, all subsequent bindings will be omitted, since they would
1644 be ignored. Similarly, the list doesn't include non-prefix bindings
1645 that come after prefix bindings.
1647 If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1648 bindings; see the description of `lookup-key' for more details about this. */)
1649 (key, accept_default)
1650 Lisp_Object key, accept_default;
1652 Lisp_Object *modes, *maps;
1653 int nmaps;
1654 Lisp_Object binding;
1655 int i, j;
1656 struct gcpro gcpro1, gcpro2;
1658 nmaps = current_minor_maps (&modes, &maps);
1659 /* Note that all these maps are GCPRO'd
1660 in the places where we found them. */
1662 binding = Qnil;
1663 GCPRO2 (key, binding);
1665 for (i = j = 0; i < nmaps; i++)
1666 if (!NILP (maps[i])
1667 && !NILP (binding = Flookup_key (maps[i], key, accept_default))
1668 && !INTEGERP (binding))
1670 if (KEYMAPP (binding))
1671 maps[j++] = Fcons (modes[i], binding);
1672 else if (j == 0)
1673 RETURN_UNGCPRO (Fcons (Fcons (modes[i], binding), Qnil));
1676 UNGCPRO;
1677 return Flist (j, maps);
1680 DEFUN ("define-prefix-command", Fdefine_prefix_command, Sdefine_prefix_command, 1, 3, 0,
1681 doc: /* Define COMMAND as a prefix command. COMMAND should be a symbol.
1682 A new sparse keymap is stored as COMMAND's function definition and its value.
1683 If a second optional argument MAPVAR is given, the map is stored as
1684 its value instead of as COMMAND's value; but COMMAND is still defined
1685 as a function.
1686 The third optional argument NAME, if given, supplies a menu name
1687 string for the map. This is required to use the keymap as a menu. */)
1688 (command, mapvar, name)
1689 Lisp_Object command, mapvar, name;
1691 Lisp_Object map;
1692 map = Fmake_sparse_keymap (name);
1693 Ffset (command, map);
1694 if (!NILP (mapvar))
1695 Fset (mapvar, map);
1696 else
1697 Fset (command, map);
1698 return command;
1701 DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0,
1702 doc: /* Select KEYMAP as the global keymap. */)
1703 (keymap)
1704 Lisp_Object keymap;
1706 keymap = get_keymap (keymap, 1, 1);
1707 current_global_map = keymap;
1709 return Qnil;
1712 DEFUN ("use-local-map", Fuse_local_map, Suse_local_map, 1, 1, 0,
1713 doc: /* Select KEYMAP as the local keymap.
1714 If KEYMAP is nil, that means no local keymap. */)
1715 (keymap)
1716 Lisp_Object keymap;
1718 if (!NILP (keymap))
1719 keymap = get_keymap (keymap, 1, 1);
1721 current_buffer->keymap = keymap;
1723 return Qnil;
1726 DEFUN ("current-local-map", Fcurrent_local_map, Scurrent_local_map, 0, 0, 0,
1727 doc: /* Return current buffer's local keymap, or nil if it has none. */)
1730 return current_buffer->keymap;
1733 DEFUN ("current-global-map", Fcurrent_global_map, Scurrent_global_map, 0, 0, 0,
1734 doc: /* Return the current global keymap. */)
1737 return current_global_map;
1740 DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps, Scurrent_minor_mode_maps, 0, 0, 0,
1741 doc: /* Return a list of keymaps for the minor modes of the current buffer. */)
1744 Lisp_Object *maps;
1745 int nmaps = current_minor_maps (0, &maps);
1747 return Flist (nmaps, maps);
1750 /* Help functions for describing and documenting keymaps. */
1753 static void
1754 accessible_keymaps_1 (key, cmd, maps, tail, thisseq, is_metized)
1755 Lisp_Object maps, tail, thisseq, key, cmd;
1756 int is_metized; /* If 1, `key' is assumed to be INTEGERP. */
1758 Lisp_Object tem;
1760 cmd = get_keymap (get_keyelt (cmd, 0), 0, 0);
1761 if (NILP (cmd))
1762 return;
1764 /* Look for and break cycles. */
1765 while (!NILP (tem = Frassq (cmd, maps)))
1767 Lisp_Object prefix = XCAR (tem);
1768 int lim = XINT (Flength (XCAR (tem)));
1769 if (lim <= XINT (Flength (thisseq)))
1770 { /* This keymap was already seen with a smaller prefix. */
1771 int i = 0;
1772 while (i < lim && EQ (Faref (prefix, make_number (i)),
1773 Faref (thisseq, make_number (i))))
1774 i++;
1775 if (i >= lim)
1776 /* `prefix' is a prefix of `thisseq' => there's a cycle. */
1777 return;
1779 /* This occurrence of `cmd' in `maps' does not correspond to a cycle,
1780 but maybe `cmd' occurs again further down in `maps', so keep
1781 looking. */
1782 maps = XCDR (Fmemq (tem, maps));
1785 /* If the last key in thisseq is meta-prefix-char,
1786 turn it into a meta-ized keystroke. We know
1787 that the event we're about to append is an
1788 ascii keystroke since we're processing a
1789 keymap table. */
1790 if (is_metized)
1792 int meta_bit = meta_modifier;
1793 Lisp_Object last = make_number (XINT (Flength (thisseq)) - 1);
1794 tem = Fcopy_sequence (thisseq);
1796 Faset (tem, last, make_number (XINT (key) | meta_bit));
1798 /* This new sequence is the same length as
1799 thisseq, so stick it in the list right
1800 after this one. */
1801 XSETCDR (tail,
1802 Fcons (Fcons (tem, cmd), XCDR (tail)));
1804 else
1806 tem = append_key (thisseq, key);
1807 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
1811 static void
1812 accessible_keymaps_char_table (args, index, cmd)
1813 Lisp_Object args, index, cmd;
1815 accessible_keymaps_1 (index, cmd,
1816 XCAR (XCAR (args)),
1817 XCAR (XCDR (args)),
1818 XCDR (XCDR (args)),
1819 XINT (XCDR (XCAR (args))));
1822 /* This function cannot GC. */
1824 DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps,
1825 1, 2, 0,
1826 doc: /* Find all keymaps accessible via prefix characters from KEYMAP.
1827 Returns a list of elements of the form (KEYS . MAP), where the sequence
1828 KEYS starting from KEYMAP gets you to MAP. These elements are ordered
1829 so that the KEYS increase in length. The first element is ([] . KEYMAP).
1830 An optional argument PREFIX, if non-nil, should be a key sequence;
1831 then the value includes only maps for prefixes that start with PREFIX. */)
1832 (keymap, prefix)
1833 Lisp_Object keymap, prefix;
1835 Lisp_Object maps, tail;
1836 int prefixlen = 0;
1838 /* no need for gcpro because we don't autoload any keymaps. */
1840 if (!NILP (prefix))
1841 prefixlen = XINT (Flength (prefix));
1843 if (!NILP (prefix))
1845 /* If a prefix was specified, start with the keymap (if any) for
1846 that prefix, so we don't waste time considering other prefixes. */
1847 Lisp_Object tem;
1848 tem = Flookup_key (keymap, prefix, Qt);
1849 /* Flookup_key may give us nil, or a number,
1850 if the prefix is not defined in this particular map.
1851 It might even give us a list that isn't a keymap. */
1852 tem = get_keymap (tem, 0, 0);
1853 if (CONSP (tem))
1855 /* Convert PREFIX to a vector now, so that later on
1856 we don't have to deal with the possibility of a string. */
1857 if (STRINGP (prefix))
1859 int i, i_byte, c;
1860 Lisp_Object copy;
1862 copy = Fmake_vector (make_number (SCHARS (prefix)), Qnil);
1863 for (i = 0, i_byte = 0; i < SCHARS (prefix);)
1865 int i_before = i;
1867 FETCH_STRING_CHAR_ADVANCE (c, prefix, i, i_byte);
1868 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
1869 c ^= 0200 | meta_modifier;
1870 ASET (copy, i_before, make_number (c));
1872 prefix = copy;
1874 maps = Fcons (Fcons (prefix, tem), Qnil);
1876 else
1877 return Qnil;
1879 else
1880 maps = Fcons (Fcons (Fmake_vector (make_number (0), Qnil),
1881 get_keymap (keymap, 1, 0)),
1882 Qnil);
1884 /* For each map in the list maps,
1885 look at any other maps it points to,
1886 and stick them at the end if they are not already in the list.
1888 This is a breadth-first traversal, where tail is the queue of
1889 nodes, and maps accumulates a list of all nodes visited. */
1891 for (tail = maps; CONSP (tail); tail = XCDR (tail))
1893 register Lisp_Object thisseq, thismap;
1894 Lisp_Object last;
1895 /* Does the current sequence end in the meta-prefix-char? */
1896 int is_metized;
1898 thisseq = Fcar (Fcar (tail));
1899 thismap = Fcdr (Fcar (tail));
1900 last = make_number (XINT (Flength (thisseq)) - 1);
1901 is_metized = (XINT (last) >= 0
1902 /* Don't metize the last char of PREFIX. */
1903 && XINT (last) >= prefixlen
1904 && EQ (Faref (thisseq, last), meta_prefix_char));
1906 for (; CONSP (thismap); thismap = XCDR (thismap))
1908 Lisp_Object elt;
1910 elt = XCAR (thismap);
1912 QUIT;
1914 if (CHAR_TABLE_P (elt))
1916 Lisp_Object indices[3];
1918 map_char_table (accessible_keymaps_char_table, Qnil, elt,
1919 elt, Fcons (Fcons (maps, make_number (is_metized)),
1920 Fcons (tail, thisseq)),
1921 0, indices);
1923 else if (VECTORP (elt))
1925 register int i;
1927 /* Vector keymap. Scan all the elements. */
1928 for (i = 0; i < ASIZE (elt); i++)
1929 accessible_keymaps_1 (make_number (i), AREF (elt, i),
1930 maps, tail, thisseq, is_metized);
1933 else if (CONSP (elt))
1934 accessible_keymaps_1 (XCAR (elt), XCDR (elt),
1935 maps, tail, thisseq,
1936 is_metized && INTEGERP (XCAR (elt)));
1941 return maps;
1944 Lisp_Object Qsingle_key_description, Qkey_description;
1946 /* This function cannot GC. */
1948 DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0,
1949 doc: /* Return a pretty description of key-sequence KEYS.
1950 Control characters turn into "C-foo" sequences, meta into "M-foo"
1951 spaces are put between sequence elements, etc. */)
1952 (keys)
1953 Lisp_Object keys;
1955 int len = 0;
1956 int i, i_byte;
1957 Lisp_Object sep;
1958 Lisp_Object *args = NULL;
1960 if (STRINGP (keys))
1962 Lisp_Object vector;
1963 vector = Fmake_vector (Flength (keys), Qnil);
1964 for (i = 0, i_byte = 0; i < SCHARS (keys); )
1966 int c;
1967 int i_before = i;
1969 FETCH_STRING_CHAR_ADVANCE (c, keys, i, i_byte);
1970 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
1971 c ^= 0200 | meta_modifier;
1972 XSETFASTINT (AREF (vector, i_before), c);
1974 keys = vector;
1977 if (VECTORP (keys))
1979 /* In effect, this computes
1980 (mapconcat 'single-key-description keys " ")
1981 but we shouldn't use mapconcat because it can do GC. */
1983 len = XVECTOR (keys)->size;
1984 sep = build_string (" ");
1985 /* This has one extra element at the end that we don't pass to Fconcat. */
1986 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
1988 for (i = 0; i < len; i++)
1990 args[i * 2] = Fsingle_key_description (AREF (keys, i), Qnil);
1991 args[i * 2 + 1] = sep;
1994 else if (CONSP (keys))
1996 /* In effect, this computes
1997 (mapconcat 'single-key-description keys " ")
1998 but we shouldn't use mapconcat because it can do GC. */
2000 len = XFASTINT (Flength (keys));
2001 sep = build_string (" ");
2002 /* This has one extra element at the end that we don't pass to Fconcat. */
2003 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
2005 for (i = 0; i < len; i++)
2007 args[i * 2] = Fsingle_key_description (XCAR (keys), Qnil);
2008 args[i * 2 + 1] = sep;
2009 keys = XCDR (keys);
2012 else
2013 keys = wrong_type_argument (Qarrayp, keys);
2015 if (len == 0)
2016 return empty_string;
2017 return Fconcat (len * 2 - 1, args);
2020 char *
2021 push_key_description (c, p, force_multibyte)
2022 register unsigned int c;
2023 register char *p;
2024 int force_multibyte;
2026 unsigned c2;
2028 /* Clear all the meaningless bits above the meta bit. */
2029 c &= meta_modifier | ~ - meta_modifier;
2030 c2 = c & ~(alt_modifier | ctrl_modifier | hyper_modifier
2031 | meta_modifier | shift_modifier | super_modifier);
2033 if (c & alt_modifier)
2035 *p++ = 'A';
2036 *p++ = '-';
2037 c -= alt_modifier;
2039 if ((c & ctrl_modifier) != 0
2040 || (c2 < ' ' && c2 != 27 && c2 != '\t' && c2 != Ctl ('M')))
2042 *p++ = 'C';
2043 *p++ = '-';
2044 c &= ~ctrl_modifier;
2046 if (c & hyper_modifier)
2048 *p++ = 'H';
2049 *p++ = '-';
2050 c -= hyper_modifier;
2052 if (c & meta_modifier)
2054 *p++ = 'M';
2055 *p++ = '-';
2056 c -= meta_modifier;
2058 if (c & shift_modifier)
2060 *p++ = 'S';
2061 *p++ = '-';
2062 c -= shift_modifier;
2064 if (c & super_modifier)
2066 *p++ = 's';
2067 *p++ = '-';
2068 c -= super_modifier;
2070 if (c < 040)
2072 if (c == 033)
2074 *p++ = 'E';
2075 *p++ = 'S';
2076 *p++ = 'C';
2078 else if (c == '\t')
2080 *p++ = 'T';
2081 *p++ = 'A';
2082 *p++ = 'B';
2084 else if (c == Ctl ('M'))
2086 *p++ = 'R';
2087 *p++ = 'E';
2088 *p++ = 'T';
2090 else
2092 /* `C-' already added above. */
2093 if (c > 0 && c <= Ctl ('Z'))
2094 *p++ = c + 0140;
2095 else
2096 *p++ = c + 0100;
2099 else if (c == 0177)
2101 *p++ = 'D';
2102 *p++ = 'E';
2103 *p++ = 'L';
2105 else if (c == ' ')
2107 *p++ = 'S';
2108 *p++ = 'P';
2109 *p++ = 'C';
2111 else if (c < 128
2112 || (NILP (current_buffer->enable_multibyte_characters)
2113 && SINGLE_BYTE_CHAR_P (c)
2114 && !force_multibyte))
2116 *p++ = c;
2118 else
2120 int valid_p = SINGLE_BYTE_CHAR_P (c) || char_valid_p (c, 0);
2122 if (force_multibyte && valid_p)
2124 if (SINGLE_BYTE_CHAR_P (c))
2125 c = unibyte_char_to_multibyte (c);
2126 p += CHAR_STRING (c, p);
2128 else if (NILP (current_buffer->enable_multibyte_characters)
2129 || valid_p)
2131 int bit_offset;
2132 *p++ = '\\';
2133 /* The biggest character code uses 19 bits. */
2134 for (bit_offset = 18; bit_offset >= 0; bit_offset -= 3)
2136 if (c >= (1 << bit_offset))
2137 *p++ = ((c & (7 << bit_offset)) >> bit_offset) + '0';
2140 else
2141 p += CHAR_STRING (c, p);
2144 return p;
2147 /* This function cannot GC. */
2149 DEFUN ("single-key-description", Fsingle_key_description,
2150 Ssingle_key_description, 1, 2, 0,
2151 doc: /* Return a pretty description of command character KEY.
2152 Control characters turn into C-whatever, etc.
2153 Optional argument NO-ANGLES non-nil means don't put angle brackets
2154 around function keys and event symbols. */)
2155 (key, no_angles)
2156 Lisp_Object key, no_angles;
2158 if (CONSP (key) && lucid_event_type_list_p (key))
2159 key = Fevent_convert_list (key);
2161 key = EVENT_HEAD (key);
2163 if (INTEGERP (key)) /* Normal character */
2165 unsigned int charset, c1, c2;
2166 int without_bits = XINT (key) & ~((-1) << CHARACTERBITS);
2168 if (SINGLE_BYTE_CHAR_P (without_bits))
2169 charset = 0;
2170 else
2171 SPLIT_CHAR (without_bits, charset, c1, c2);
2173 if (charset
2174 && CHARSET_DEFINED_P (charset)
2175 && ((c1 >= 0 && c1 < 32)
2176 || (c2 >= 0 && c2 < 32)))
2178 /* Handle a generic character. */
2179 Lisp_Object name;
2180 name = CHARSET_TABLE_INFO (charset, CHARSET_LONG_NAME_IDX);
2181 CHECK_STRING (name);
2182 return concat2 (build_string ("Character set "), name);
2184 else
2186 char tem[KEY_DESCRIPTION_SIZE], *end;
2187 int nbytes, nchars;
2188 Lisp_Object string;
2190 end = push_key_description (XUINT (key), tem, 1);
2191 nbytes = end - tem;
2192 nchars = multibyte_chars_in_text (tem, nbytes);
2193 if (nchars == nbytes)
2195 *end = '\0';
2196 string = build_string (tem);
2198 else
2199 string = make_multibyte_string (tem, nchars, nbytes);
2200 return string;
2203 else if (SYMBOLP (key)) /* Function key or event-symbol */
2205 if (NILP (no_angles))
2207 char *buffer
2208 = (char *) alloca (SBYTES (SYMBOL_NAME (key)) + 5);
2209 sprintf (buffer, "<%s>", SDATA (SYMBOL_NAME (key)));
2210 return build_string (buffer);
2212 else
2213 return Fsymbol_name (key);
2215 else if (STRINGP (key)) /* Buffer names in the menubar. */
2216 return Fcopy_sequence (key);
2217 else
2218 error ("KEY must be an integer, cons, symbol, or string");
2219 return Qnil;
2222 char *
2223 push_text_char_description (c, p)
2224 register unsigned int c;
2225 register char *p;
2227 if (c >= 0200)
2229 *p++ = 'M';
2230 *p++ = '-';
2231 c -= 0200;
2233 if (c < 040)
2235 *p++ = '^';
2236 *p++ = c + 64; /* 'A' - 1 */
2238 else if (c == 0177)
2240 *p++ = '^';
2241 *p++ = '?';
2243 else
2244 *p++ = c;
2245 return p;
2248 /* This function cannot GC. */
2250 DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0,
2251 doc: /* Return a pretty description of file-character CHARACTER.
2252 Control characters turn into "^char", etc. */)
2253 (character)
2254 Lisp_Object character;
2256 /* Currently MAX_MULTIBYTE_LENGTH is 4 (< 6). */
2257 unsigned char str[6];
2258 int c;
2260 CHECK_NUMBER (character);
2262 c = XINT (character);
2263 if (!SINGLE_BYTE_CHAR_P (c))
2265 int len = CHAR_STRING (c, str);
2267 return make_multibyte_string (str, 1, len);
2270 *push_text_char_description (c & 0377, str) = 0;
2272 return build_string (str);
2275 /* Return non-zero if SEQ contains only ASCII characters, perhaps with
2276 a meta bit. */
2277 static int
2278 ascii_sequence_p (seq)
2279 Lisp_Object seq;
2281 int i;
2282 int len = XINT (Flength (seq));
2284 for (i = 0; i < len; i++)
2286 Lisp_Object ii, elt;
2288 XSETFASTINT (ii, i);
2289 elt = Faref (seq, ii);
2291 if (!INTEGERP (elt)
2292 || (XUINT (elt) & ~CHAR_META) >= 0x80)
2293 return 0;
2296 return 1;
2300 /* where-is - finding a command in a set of keymaps. */
2302 static Lisp_Object where_is_internal ();
2303 static Lisp_Object where_is_internal_1 ();
2304 static void where_is_internal_2 ();
2306 /* Like Flookup_key, but uses a list of keymaps SHADOW instead of a single map.
2307 Returns the first non-nil binding found in any of those maps. */
2309 static Lisp_Object
2310 shadow_lookup (shadow, key, flag)
2311 Lisp_Object shadow, key, flag;
2313 Lisp_Object tail, value;
2315 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2317 value = Flookup_key (XCAR (tail), key, flag);
2318 if (!NILP (value) && !NATNUMP (value))
2319 return value;
2321 return Qnil;
2324 static Lisp_Object Vmenu_events;
2326 /* This function can GC if Flookup_key autoloads any keymaps. */
2328 static Lisp_Object
2329 where_is_internal (definition, keymaps, firstonly, noindirect, no_remap)
2330 Lisp_Object definition, keymaps;
2331 Lisp_Object firstonly, noindirect, no_remap;
2333 Lisp_Object maps = Qnil;
2334 Lisp_Object found, sequences;
2335 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
2336 /* 1 means ignore all menu bindings entirely. */
2337 int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii);
2339 /* If this command is remapped, then it has no key bindings
2340 of its own. */
2341 if (NILP (no_remap) && SYMBOLP (definition))
2343 Lisp_Object tem;
2344 if (tem = Fcommand_remapping (definition), !NILP (tem))
2345 return Qnil;
2348 found = keymaps;
2349 while (CONSP (found))
2351 maps =
2352 nconc2 (maps,
2353 Faccessible_keymaps (get_keymap (XCAR (found), 1, 0), Qnil));
2354 found = XCDR (found);
2357 GCPRO5 (definition, keymaps, maps, found, sequences);
2358 found = Qnil;
2359 sequences = Qnil;
2361 for (; !NILP (maps); maps = Fcdr (maps))
2363 /* Key sequence to reach map, and the map that it reaches */
2364 register Lisp_Object this, map, tem;
2366 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
2367 [M-CHAR] sequences, check if last character of the sequence
2368 is the meta-prefix char. */
2369 Lisp_Object last;
2370 int last_is_meta;
2372 this = Fcar (Fcar (maps));
2373 map = Fcdr (Fcar (maps));
2374 last = make_number (XINT (Flength (this)) - 1);
2375 last_is_meta = (XINT (last) >= 0
2376 && EQ (Faref (this, last), meta_prefix_char));
2378 /* if (nomenus && !ascii_sequence_p (this)) */
2379 if (nomenus && XINT (last) >= 0
2380 && SYMBOLP (tem = Faref (this, make_number (0)))
2381 && !NILP (Fmemq (XCAR (parse_modifiers (tem)), Vmenu_events)))
2382 /* If no menu entries should be returned, skip over the
2383 keymaps bound to `menu-bar' and `tool-bar' and other
2384 non-ascii prefixes like `C-down-mouse-2'. */
2385 continue;
2387 QUIT;
2389 while (CONSP (map))
2391 /* Because the code we want to run on each binding is rather
2392 large, we don't want to have two separate loop bodies for
2393 sparse keymap bindings and tables; we want to iterate one
2394 loop body over both keymap and vector bindings.
2396 For this reason, if Fcar (map) is a vector, we don't
2397 advance map to the next element until i indicates that we
2398 have finished off the vector. */
2399 Lisp_Object elt, key, binding;
2400 elt = XCAR (map);
2401 map = XCDR (map);
2403 sequences = Qnil;
2405 QUIT;
2407 /* Set key and binding to the current key and binding, and
2408 advance map and i to the next binding. */
2409 if (VECTORP (elt))
2411 Lisp_Object sequence;
2412 int i;
2413 /* In a vector, look at each element. */
2414 for (i = 0; i < XVECTOR (elt)->size; i++)
2416 binding = AREF (elt, i);
2417 XSETFASTINT (key, i);
2418 sequence = where_is_internal_1 (binding, key, definition,
2419 noindirect, this,
2420 last, nomenus, last_is_meta);
2421 if (!NILP (sequence))
2422 sequences = Fcons (sequence, sequences);
2425 else if (CHAR_TABLE_P (elt))
2427 Lisp_Object indices[3];
2428 Lisp_Object args;
2430 args = Fcons (Fcons (Fcons (definition, noindirect),
2431 Qnil), /* Result accumulator. */
2432 Fcons (Fcons (this, last),
2433 Fcons (make_number (nomenus),
2434 make_number (last_is_meta))));
2435 map_char_table (where_is_internal_2, Qnil, elt, elt, args,
2436 0, indices);
2437 sequences = XCDR (XCAR (args));
2439 else if (CONSP (elt))
2441 Lisp_Object sequence;
2443 key = XCAR (elt);
2444 binding = XCDR (elt);
2446 sequence = where_is_internal_1 (binding, key, definition,
2447 noindirect, this,
2448 last, nomenus, last_is_meta);
2449 if (!NILP (sequence))
2450 sequences = Fcons (sequence, sequences);
2454 while (!NILP (sequences))
2456 Lisp_Object sequence, remapped, function;
2458 sequence = XCAR (sequences);
2459 sequences = XCDR (sequences);
2461 /* If the current sequence is a command remapping with
2462 format [remap COMMAND], find the key sequences
2463 which run COMMAND, and use those sequences instead. */
2464 remapped = Qnil;
2465 if (NILP (no_remap)
2466 && VECTORP (sequence) && XVECTOR (sequence)->size == 2
2467 && EQ (AREF (sequence, 0), Qremap)
2468 && (function = AREF (sequence, 1), SYMBOLP (function)))
2470 Lisp_Object remapped1;
2472 remapped1 = where_is_internal (function, keymaps, firstonly, noindirect, Qt);
2473 if (CONSP (remapped1))
2475 /* Verify that this key binding actually maps to the
2476 remapped command (see below). */
2477 if (!EQ (shadow_lookup (keymaps, XCAR (remapped1), Qnil), function))
2478 continue;
2479 sequence = XCAR (remapped1);
2480 remapped = XCDR (remapped1);
2481 goto record_sequence;
2485 /* Verify that this key binding is not shadowed by another
2486 binding for the same key, before we say it exists.
2488 Mechanism: look for local definition of this key and if
2489 it is defined and does not match what we found then
2490 ignore this key.
2492 Either nil or number as value from Flookup_key
2493 means undefined. */
2494 if (!EQ (shadow_lookup (keymaps, sequence, Qnil), definition))
2495 continue;
2497 record_sequence:
2498 /* It is a true unshadowed match. Record it, unless it's already
2499 been seen (as could happen when inheriting keymaps). */
2500 if (NILP (Fmember (sequence, found)))
2501 found = Fcons (sequence, found);
2503 /* If firstonly is Qnon_ascii, then we can return the first
2504 binding we find. If firstonly is not Qnon_ascii but not
2505 nil, then we should return the first ascii-only binding
2506 we find. */
2507 if (EQ (firstonly, Qnon_ascii))
2508 RETURN_UNGCPRO (sequence);
2509 else if (!NILP (firstonly) && ascii_sequence_p (sequence))
2510 RETURN_UNGCPRO (sequence);
2512 if (CONSP (remapped))
2514 sequence = XCAR (remapped);
2515 remapped = XCDR (remapped);
2516 goto record_sequence;
2522 UNGCPRO;
2524 found = Fnreverse (found);
2526 /* firstonly may have been t, but we may have gone all the way through
2527 the keymaps without finding an all-ASCII key sequence. So just
2528 return the best we could find. */
2529 if (!NILP (firstonly))
2530 return Fcar (found);
2532 return found;
2535 DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 5, 0,
2536 doc: /* Return list of keys that invoke DEFINITION.
2537 If KEYMAP is non-nil, search only KEYMAP and the global keymap.
2538 If KEYMAP is nil, search all the currently active keymaps.
2539 If KEYMAP is a list of keymaps, search only those keymaps.
2541 If optional 3rd arg FIRSTONLY is non-nil, return the first key sequence found,
2542 rather than a list of all possible key sequences.
2543 If FIRSTONLY is the symbol `non-ascii', return the first binding found,
2544 no matter what it is.
2545 If FIRSTONLY has another non-nil value, prefer sequences of ASCII characters,
2546 and entirely reject menu bindings.
2548 If optional 4th arg NOINDIRECT is non-nil, don't follow indirections
2549 to other keymaps or slots. This makes it possible to search for an
2550 indirect definition itself.
2552 If optional 5th arg NO-REMAP is non-nil, don't search for key sequences
2553 that invoke a command which is remapped to DEFINITION, but include the
2554 remapped command in the returned list. */)
2555 (definition, keymap, firstonly, noindirect, no_remap)
2556 Lisp_Object definition, keymap;
2557 Lisp_Object firstonly, noindirect, no_remap;
2559 Lisp_Object sequences, keymaps;
2560 /* 1 means ignore all menu bindings entirely. */
2561 int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii);
2562 Lisp_Object result;
2564 /* Find the relevant keymaps. */
2565 if (CONSP (keymap) && KEYMAPP (XCAR (keymap)))
2566 keymaps = keymap;
2567 else if (!NILP (keymap))
2568 keymaps = Fcons (keymap, Fcons (current_global_map, Qnil));
2569 else
2570 keymaps = Fcurrent_active_maps (Qnil);
2572 /* Only use caching for the menubar (i.e. called with (def nil t nil).
2573 We don't really need to check `keymap'. */
2574 if (nomenus && NILP (noindirect) && NILP (keymap))
2576 Lisp_Object *defns;
2577 int i, j, n;
2578 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
2580 /* Check heuristic-consistency of the cache. */
2581 if (NILP (Fequal (keymaps, where_is_cache_keymaps)))
2582 where_is_cache = Qnil;
2584 if (NILP (where_is_cache))
2586 /* We need to create the cache. */
2587 Lisp_Object args[2];
2588 where_is_cache = Fmake_hash_table (0, args);
2589 where_is_cache_keymaps = Qt;
2591 /* Fill in the cache. */
2592 GCPRO5 (definition, keymaps, firstonly, noindirect, no_remap);
2593 where_is_internal (definition, keymaps, firstonly, noindirect, no_remap);
2594 UNGCPRO;
2596 where_is_cache_keymaps = keymaps;
2599 /* We want to process definitions from the last to the first.
2600 Instead of consing, copy definitions to a vector and step
2601 over that vector. */
2602 sequences = Fgethash (definition, where_is_cache, Qnil);
2603 n = XINT (Flength (sequences));
2604 defns = (Lisp_Object *) alloca (n * sizeof *defns);
2605 for (i = 0; CONSP (sequences); sequences = XCDR (sequences))
2606 defns[i++] = XCAR (sequences);
2608 /* Verify that the key bindings are not shadowed. Note that
2609 the following can GC. */
2610 GCPRO2 (definition, keymaps);
2611 result = Qnil;
2612 j = -1;
2613 for (i = n - 1; i >= 0; --i)
2614 if (EQ (shadow_lookup (keymaps, defns[i], Qnil), definition))
2616 if (ascii_sequence_p (defns[i]))
2617 break;
2618 else if (j < 0)
2619 j = i;
2622 result = i >= 0 ? defns[i] : (j >= 0 ? defns[j] : Qnil);
2623 UNGCPRO;
2625 else
2627 /* Kill the cache so that where_is_internal_1 doesn't think
2628 we're filling it up. */
2629 where_is_cache = Qnil;
2630 result = where_is_internal (definition, keymaps, firstonly, noindirect, no_remap);
2633 return result;
2636 /* This is the function that Fwhere_is_internal calls using map_char_table.
2637 ARGS has the form
2638 (((DEFINITION . NOINDIRECT) . (KEYMAP . RESULT))
2640 ((THIS . LAST) . (NOMENUS . LAST_IS_META)))
2641 Since map_char_table doesn't really use the return value from this function,
2642 we the result append to RESULT, the slot in ARGS.
2644 This function can GC because it calls where_is_internal_1 which can
2645 GC. */
2647 static void
2648 where_is_internal_2 (args, key, binding)
2649 Lisp_Object args, key, binding;
2651 Lisp_Object definition, noindirect, this, last;
2652 Lisp_Object result, sequence;
2653 int nomenus, last_is_meta;
2654 struct gcpro gcpro1, gcpro2, gcpro3;
2656 GCPRO3 (args, key, binding);
2657 result = XCDR (XCAR (args));
2658 definition = XCAR (XCAR (XCAR (args)));
2659 noindirect = XCDR (XCAR (XCAR (args)));
2660 this = XCAR (XCAR (XCDR (args)));
2661 last = XCDR (XCAR (XCDR (args)));
2662 nomenus = XFASTINT (XCAR (XCDR (XCDR (args))));
2663 last_is_meta = XFASTINT (XCDR (XCDR (XCDR (args))));
2665 sequence = where_is_internal_1 (binding, key, definition, noindirect,
2666 this, last, nomenus, last_is_meta);
2668 if (!NILP (sequence))
2669 XSETCDR (XCAR (args), Fcons (sequence, result));
2671 UNGCPRO;
2675 /* This function cannot GC. */
2677 static Lisp_Object
2678 where_is_internal_1 (binding, key, definition, noindirect, this, last,
2679 nomenus, last_is_meta)
2680 Lisp_Object binding, key, definition, noindirect, this, last;
2681 int nomenus, last_is_meta;
2683 Lisp_Object sequence;
2685 /* Search through indirections unless that's not wanted. */
2686 if (NILP (noindirect))
2687 binding = get_keyelt (binding, 0);
2689 /* End this iteration if this element does not match
2690 the target. */
2692 if (!(!NILP (where_is_cache) /* everything "matches" during cache-fill. */
2693 || EQ (binding, definition)
2694 || (CONSP (definition) && !NILP (Fequal (binding, definition)))))
2695 /* Doesn't match. */
2696 return Qnil;
2698 /* We have found a match. Construct the key sequence where we found it. */
2699 if (INTEGERP (key) && last_is_meta)
2701 sequence = Fcopy_sequence (this);
2702 Faset (sequence, last, make_number (XINT (key) | meta_modifier));
2704 else
2705 sequence = append_key (this, key);
2707 if (!NILP (where_is_cache))
2709 Lisp_Object sequences = Fgethash (binding, where_is_cache, Qnil);
2710 Fputhash (binding, Fcons (sequence, sequences), where_is_cache);
2711 return Qnil;
2713 else
2714 return sequence;
2717 /* describe-bindings - summarizing all the bindings in a set of keymaps. */
2719 DEFUN ("describe-buffer-bindings", Fdescribe_buffer_bindings, Sdescribe_buffer_bindings, 1, 3, 0,
2720 doc: /* Insert the list of all defined keys and their definitions.
2721 The list is inserted in the current buffer, while the bindings are
2722 looked up in BUFFER.
2723 The optional argument PREFIX, if non-nil, should be a key sequence;
2724 then we display only bindings that start with that prefix.
2725 The optional argument MENUS, if non-nil, says to mention menu bindings.
2726 \(Ordinarily these are omitted from the output.) */)
2727 (buffer, prefix, menus)
2728 Lisp_Object buffer, prefix, menus;
2730 Lisp_Object outbuf, shadow;
2731 int nomenu = NILP (menus);
2732 register Lisp_Object start1;
2733 struct gcpro gcpro1;
2735 char *alternate_heading
2736 = "\
2737 Keyboard translations:\n\n\
2738 You type Translation\n\
2739 -------- -----------\n";
2741 shadow = Qnil;
2742 GCPRO1 (shadow);
2744 outbuf = Fcurrent_buffer ();
2746 /* Report on alternates for keys. */
2747 if (STRINGP (Vkeyboard_translate_table) && !NILP (prefix))
2749 int c;
2750 const unsigned char *translate = SDATA (Vkeyboard_translate_table);
2751 int translate_len = SCHARS (Vkeyboard_translate_table);
2753 for (c = 0; c < translate_len; c++)
2754 if (translate[c] != c)
2756 char buf[KEY_DESCRIPTION_SIZE];
2757 char *bufend;
2759 if (alternate_heading)
2761 insert_string (alternate_heading);
2762 alternate_heading = 0;
2765 bufend = push_key_description (translate[c], buf, 1);
2766 insert (buf, bufend - buf);
2767 Findent_to (make_number (16), make_number (1));
2768 bufend = push_key_description (c, buf, 1);
2769 insert (buf, bufend - buf);
2771 insert ("\n", 1);
2774 insert ("\n", 1);
2777 if (!NILP (Vkey_translation_map))
2778 describe_map_tree (Vkey_translation_map, 0, Qnil, prefix,
2779 "Key translations", nomenu, 1, 0);
2782 /* Print the (major mode) local map. */
2783 start1 = Qnil;
2784 if (!NILP (current_kboard->Voverriding_terminal_local_map))
2785 start1 = current_kboard->Voverriding_terminal_local_map;
2786 else if (!NILP (Voverriding_local_map))
2787 start1 = Voverriding_local_map;
2789 if (!NILP (start1))
2791 describe_map_tree (start1, 1, shadow, prefix,
2792 "\f\nOverriding Bindings", nomenu, 0, 0);
2793 shadow = Fcons (start1, shadow);
2795 else
2797 /* Print the minor mode and major mode keymaps. */
2798 int i, nmaps;
2799 Lisp_Object *modes, *maps;
2801 /* Temporarily switch to `buffer', so that we can get that buffer's
2802 minor modes correctly. */
2803 Fset_buffer (buffer);
2805 nmaps = current_minor_maps (&modes, &maps);
2806 Fset_buffer (outbuf);
2808 start1 = get_local_map (BUF_PT (XBUFFER (buffer)),
2809 XBUFFER (buffer), Qkeymap);
2810 if (!NILP (start1))
2812 describe_map_tree (start1, 1, shadow, prefix,
2813 "\f\n`keymap' Property Bindings", nomenu, 0, 0);
2814 shadow = Fcons (start1, shadow);
2817 /* Print the minor mode maps. */
2818 for (i = 0; i < nmaps; i++)
2820 /* The title for a minor mode keymap
2821 is constructed at run time.
2822 We let describe_map_tree do the actual insertion
2823 because it takes care of other features when doing so. */
2824 char *title, *p;
2826 if (!SYMBOLP (modes[i]))
2827 abort();
2829 p = title = (char *) alloca (42 + SCHARS (SYMBOL_NAME (modes[i])));
2830 *p++ = '\f';
2831 *p++ = '\n';
2832 *p++ = '`';
2833 bcopy (SDATA (SYMBOL_NAME (modes[i])), p,
2834 SCHARS (SYMBOL_NAME (modes[i])));
2835 p += SCHARS (SYMBOL_NAME (modes[i]));
2836 *p++ = '\'';
2837 bcopy (" Minor Mode Bindings", p, sizeof (" Minor Mode Bindings") - 1);
2838 p += sizeof (" Minor Mode Bindings") - 1;
2839 *p = 0;
2841 describe_map_tree (maps[i], 1, shadow, prefix, title, nomenu, 0, 0);
2842 shadow = Fcons (maps[i], shadow);
2845 start1 = get_local_map (BUF_PT (XBUFFER (buffer)),
2846 XBUFFER (buffer), Qlocal_map);
2847 if (!NILP (start1))
2849 if (EQ (start1, XBUFFER (buffer)->keymap))
2850 describe_map_tree (start1, 1, shadow, prefix,
2851 "\f\nMajor Mode Bindings", nomenu, 0, 0);
2852 else
2853 describe_map_tree (start1, 1, shadow, prefix,
2854 "\f\n`local-map' Property Bindings",
2855 nomenu, 0, 0);
2857 shadow = Fcons (start1, shadow);
2861 describe_map_tree (current_global_map, 1, shadow, prefix,
2862 "\f\nGlobal Bindings", nomenu, 0, 1);
2864 /* Print the function-key-map translations under this prefix. */
2865 if (!NILP (Vfunction_key_map))
2866 describe_map_tree (Vfunction_key_map, 0, Qnil, prefix,
2867 "\f\nFunction key map translations", nomenu, 1, 0);
2869 UNGCPRO;
2870 return Qnil;
2873 /* Insert a description of the key bindings in STARTMAP,
2874 followed by those of all maps reachable through STARTMAP.
2875 If PARTIAL is nonzero, omit certain "uninteresting" commands
2876 (such as `undefined').
2877 If SHADOW is non-nil, it is a list of maps;
2878 don't mention keys which would be shadowed by any of them.
2879 PREFIX, if non-nil, says mention only keys that start with PREFIX.
2880 TITLE, if not 0, is a string to insert at the beginning.
2881 TITLE should not end with a colon or a newline; we supply that.
2882 If NOMENU is not 0, then omit menu-bar commands.
2884 If TRANSL is nonzero, the definitions are actually key translations
2885 so print strings and vectors differently.
2887 If ALWAYS_TITLE is nonzero, print the title even if there are no maps
2888 to look through. */
2890 void
2891 describe_map_tree (startmap, partial, shadow, prefix, title, nomenu, transl,
2892 always_title)
2893 Lisp_Object startmap, shadow, prefix;
2894 int partial;
2895 char *title;
2896 int nomenu;
2897 int transl;
2898 int always_title;
2900 Lisp_Object maps, orig_maps, seen, sub_shadows;
2901 struct gcpro gcpro1, gcpro2, gcpro3;
2902 int something = 0;
2903 char *key_heading
2904 = "\
2905 key binding\n\
2906 --- -------\n";
2908 orig_maps = maps = Faccessible_keymaps (startmap, prefix);
2909 seen = Qnil;
2910 sub_shadows = Qnil;
2911 GCPRO3 (maps, seen, sub_shadows);
2913 if (nomenu)
2915 Lisp_Object list;
2917 /* Delete from MAPS each element that is for the menu bar. */
2918 for (list = maps; !NILP (list); list = XCDR (list))
2920 Lisp_Object elt, prefix, tem;
2922 elt = Fcar (list);
2923 prefix = Fcar (elt);
2924 if (XVECTOR (prefix)->size >= 1)
2926 tem = Faref (prefix, make_number (0));
2927 if (EQ (tem, Qmenu_bar))
2928 maps = Fdelq (elt, maps);
2933 if (!NILP (maps) || always_title)
2935 if (title)
2937 insert_string (title);
2938 if (!NILP (prefix))
2940 insert_string (" Starting With ");
2941 insert1 (Fkey_description (prefix));
2943 insert_string (":\n");
2945 insert_string (key_heading);
2946 something = 1;
2949 for (; !NILP (maps); maps = Fcdr (maps))
2951 register Lisp_Object elt, prefix, tail;
2953 elt = Fcar (maps);
2954 prefix = Fcar (elt);
2956 sub_shadows = Qnil;
2958 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2960 Lisp_Object shmap;
2962 shmap = XCAR (tail);
2964 /* If the sequence by which we reach this keymap is zero-length,
2965 then the shadow map for this keymap is just SHADOW. */
2966 if ((STRINGP (prefix) && SCHARS (prefix) == 0)
2967 || (VECTORP (prefix) && XVECTOR (prefix)->size == 0))
2969 /* If the sequence by which we reach this keymap actually has
2970 some elements, then the sequence's definition in SHADOW is
2971 what we should use. */
2972 else
2974 shmap = Flookup_key (shmap, Fcar (elt), Qt);
2975 if (INTEGERP (shmap))
2976 shmap = Qnil;
2979 /* If shmap is not nil and not a keymap,
2980 it completely shadows this map, so don't
2981 describe this map at all. */
2982 if (!NILP (shmap) && !KEYMAPP (shmap))
2983 goto skip;
2985 if (!NILP (shmap))
2986 sub_shadows = Fcons (shmap, sub_shadows);
2989 /* Maps we have already listed in this loop shadow this map. */
2990 for (tail = orig_maps; !EQ (tail, maps); tail = XCDR (tail))
2992 Lisp_Object tem;
2993 tem = Fequal (Fcar (XCAR (tail)), prefix);
2994 if (!NILP (tem))
2995 sub_shadows = Fcons (XCDR (XCAR (tail)), sub_shadows);
2998 describe_map (Fcdr (elt), prefix,
2999 transl ? describe_translation : describe_command,
3000 partial, sub_shadows, &seen, nomenu);
3002 skip: ;
3005 if (something)
3006 insert_string ("\n");
3008 UNGCPRO;
3011 static int previous_description_column;
3013 static void
3014 describe_command (definition, args)
3015 Lisp_Object definition, args;
3017 register Lisp_Object tem1;
3018 int column = (int) current_column (); /* iftc */
3019 int description_column;
3021 /* If column 16 is no good, go to col 32;
3022 but don't push beyond that--go to next line instead. */
3023 if (column > 30)
3025 insert_char ('\n');
3026 description_column = 32;
3028 else if (column > 14 || (column > 10 && previous_description_column == 32))
3029 description_column = 32;
3030 else
3031 description_column = 16;
3033 Findent_to (make_number (description_column), make_number (1));
3034 previous_description_column = description_column;
3036 if (SYMBOLP (definition))
3038 tem1 = SYMBOL_NAME (definition);
3039 insert1 (tem1);
3040 insert_string ("\n");
3042 else if (STRINGP (definition) || VECTORP (definition))
3043 insert_string ("Keyboard Macro\n");
3044 else if (KEYMAPP (definition))
3045 insert_string ("Prefix Command\n");
3046 else
3047 insert_string ("??\n");
3050 static void
3051 describe_translation (definition, args)
3052 Lisp_Object definition, args;
3054 register Lisp_Object tem1;
3056 Findent_to (make_number (16), make_number (1));
3058 if (SYMBOLP (definition))
3060 tem1 = SYMBOL_NAME (definition);
3061 insert1 (tem1);
3062 insert_string ("\n");
3064 else if (STRINGP (definition) || VECTORP (definition))
3066 insert1 (Fkey_description (definition));
3067 insert_string ("\n");
3069 else if (KEYMAPP (definition))
3070 insert_string ("Prefix Command\n");
3071 else
3072 insert_string ("??\n");
3075 /* Describe the contents of map MAP, assuming that this map itself is
3076 reached by the sequence of prefix keys KEYS (a string or vector).
3077 PARTIAL, SHADOW, NOMENU are as in `describe_map_tree' above. */
3079 static void
3080 describe_map (map, keys, elt_describer, partial, shadow, seen, nomenu)
3081 register Lisp_Object map;
3082 Lisp_Object keys;
3083 void (*elt_describer) P_ ((Lisp_Object, Lisp_Object));
3084 int partial;
3085 Lisp_Object shadow;
3086 Lisp_Object *seen;
3087 int nomenu;
3089 Lisp_Object elt_prefix;
3090 Lisp_Object tail, definition, event;
3091 Lisp_Object tem;
3092 Lisp_Object suppress;
3093 Lisp_Object kludge;
3094 int first = 1;
3095 struct gcpro gcpro1, gcpro2, gcpro3;
3097 suppress = Qnil;
3099 if (!NILP (keys) && XFASTINT (Flength (keys)) > 0)
3101 /* Call Fkey_description first, to avoid GC bug for the other string. */
3102 tem = Fkey_description (keys);
3103 elt_prefix = concat2 (tem, build_string (" "));
3105 else
3106 elt_prefix = Qnil;
3108 if (partial)
3109 suppress = intern ("suppress-keymap");
3111 /* This vector gets used to present single keys to Flookup_key. Since
3112 that is done once per keymap element, we don't want to cons up a
3113 fresh vector every time. */
3114 kludge = Fmake_vector (make_number (1), Qnil);
3115 definition = Qnil;
3117 GCPRO3 (elt_prefix, definition, kludge);
3119 for (tail = map; CONSP (tail); tail = XCDR (tail))
3121 QUIT;
3123 if (VECTORP (XCAR (tail))
3124 || CHAR_TABLE_P (XCAR (tail)))
3125 describe_vector (XCAR (tail),
3126 elt_prefix, Qnil, elt_describer, partial, shadow, map,
3127 (int *)0, 0);
3128 else if (CONSP (XCAR (tail)))
3130 event = XCAR (XCAR (tail));
3132 /* Ignore bindings whose "keys" are not really valid events.
3133 (We get these in the frames and buffers menu.) */
3134 if (!(SYMBOLP (event) || INTEGERP (event)))
3135 continue;
3137 if (nomenu && EQ (event, Qmenu_bar))
3138 continue;
3140 definition = get_keyelt (XCDR (XCAR (tail)), 0);
3142 /* Don't show undefined commands or suppressed commands. */
3143 if (NILP (definition)) continue;
3144 if (SYMBOLP (definition) && partial)
3146 tem = Fget (definition, suppress);
3147 if (!NILP (tem))
3148 continue;
3151 /* Don't show a command that isn't really visible
3152 because a local definition of the same key shadows it. */
3154 ASET (kludge, 0, event);
3155 if (!NILP (shadow))
3157 tem = shadow_lookup (shadow, kludge, Qt);
3158 if (!NILP (tem)) continue;
3161 tem = Flookup_key (map, kludge, Qt);
3162 if (!EQ (tem, definition)) continue;
3164 if (first)
3166 previous_description_column = 0;
3167 insert ("\n", 1);
3168 first = 0;
3171 if (!NILP (elt_prefix))
3172 insert1 (elt_prefix);
3174 /* THIS gets the string to describe the character EVENT. */
3175 insert1 (Fsingle_key_description (event, Qnil));
3177 /* Print a description of the definition of this character.
3178 elt_describer will take care of spacing out far enough
3179 for alignment purposes. */
3180 (*elt_describer) (definition, Qnil);
3182 else if (EQ (XCAR (tail), Qkeymap))
3184 /* The same keymap might be in the structure twice, if we're
3185 using an inherited keymap. So skip anything we've already
3186 encountered. */
3187 tem = Fassq (tail, *seen);
3188 if (CONSP (tem) && !NILP (Fequal (XCAR (tem), keys)))
3189 break;
3190 *seen = Fcons (Fcons (tail, keys), *seen);
3194 UNGCPRO;
3197 static void
3198 describe_vector_princ (elt, fun)
3199 Lisp_Object elt, fun;
3201 Findent_to (make_number (16), make_number (1));
3202 call1 (fun, elt);
3203 Fterpri (Qnil);
3206 DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 2, 0,
3207 doc: /* Insert a description of contents of VECTOR.
3208 This is text showing the elements of vector matched against indices. */)
3209 (vector, describer)
3210 Lisp_Object vector, describer;
3212 int count = SPECPDL_INDEX ();
3213 if (NILP (describer))
3214 describer = intern ("princ");
3215 specbind (Qstandard_output, Fcurrent_buffer ());
3216 CHECK_VECTOR_OR_CHAR_TABLE (vector);
3217 describe_vector (vector, Qnil, describer, describe_vector_princ, 0,
3218 Qnil, Qnil, (int *)0, 0);
3220 return unbind_to (count, Qnil);
3223 /* Insert in the current buffer a description of the contents of VECTOR.
3224 We call ELT_DESCRIBER to insert the description of one value found
3225 in VECTOR.
3227 ELT_PREFIX describes what "comes before" the keys or indices defined
3228 by this vector. This is a human-readable string whose size
3229 is not necessarily related to the situation.
3231 If the vector is in a keymap, ELT_PREFIX is a prefix key which
3232 leads to this keymap.
3234 If the vector is a chartable, ELT_PREFIX is the vector
3235 of bytes that lead to the character set or portion of a character
3236 set described by this chartable.
3238 If PARTIAL is nonzero, it means do not mention suppressed commands
3239 (that assumes the vector is in a keymap).
3241 SHADOW is a list of keymaps that shadow this map.
3242 If it is non-nil, then we look up the key in those maps
3243 and we don't mention it now if it is defined by any of them.
3245 ENTIRE_MAP is the keymap in which this vector appears.
3246 If the definition in effect in the whole map does not match
3247 the one in this vector, we ignore this one.
3249 When describing a sub-char-table, INDICES is a list of
3250 indices at higher levels in this char-table,
3251 and CHAR_TABLE_DEPTH says how many levels down we have gone.
3253 ARGS is simply passed as the second argument to ELT_DESCRIBER. */
3255 void
3256 describe_vector (vector, elt_prefix, args, elt_describer,
3257 partial, shadow, entire_map,
3258 indices, char_table_depth)
3259 register Lisp_Object vector;
3260 Lisp_Object elt_prefix, args;
3261 void (*elt_describer) P_ ((Lisp_Object, Lisp_Object));
3262 int partial;
3263 Lisp_Object shadow;
3264 Lisp_Object entire_map;
3265 int *indices;
3266 int char_table_depth;
3268 Lisp_Object definition;
3269 Lisp_Object tem2;
3270 register int i;
3271 Lisp_Object suppress;
3272 Lisp_Object kludge;
3273 int first = 1;
3274 struct gcpro gcpro1, gcpro2, gcpro3;
3275 /* Range of elements to be handled. */
3276 int from, to;
3277 /* A flag to tell if a leaf in this level of char-table is not a
3278 generic character (i.e. a complete multibyte character). */
3279 int complete_char;
3280 int character;
3281 int starting_i;
3283 suppress = Qnil;
3285 if (indices == 0)
3286 indices = (int *) alloca (3 * sizeof (int));
3288 definition = Qnil;
3290 /* This vector gets used to present single keys to Flookup_key. Since
3291 that is done once per vector element, we don't want to cons up a
3292 fresh vector every time. */
3293 kludge = Fmake_vector (make_number (1), Qnil);
3294 GCPRO3 (elt_prefix, definition, kludge);
3296 if (partial)
3297 suppress = intern ("suppress-keymap");
3299 if (CHAR_TABLE_P (vector))
3301 if (char_table_depth == 0)
3303 /* VECTOR is a top level char-table. */
3304 complete_char = 1;
3305 from = 0;
3306 to = CHAR_TABLE_ORDINARY_SLOTS;
3308 else
3310 /* VECTOR is a sub char-table. */
3311 if (char_table_depth >= 3)
3312 /* A char-table is never that deep. */
3313 error ("Too deep char table");
3315 complete_char
3316 = (CHARSET_VALID_P (indices[0])
3317 && ((CHARSET_DIMENSION (indices[0]) == 1
3318 && char_table_depth == 1)
3319 || char_table_depth == 2));
3321 /* Meaningful elements are from 32th to 127th. */
3322 from = 32;
3323 to = SUB_CHAR_TABLE_ORDINARY_SLOTS;
3326 else
3328 /* This does the right thing for ordinary vectors. */
3330 complete_char = 1;
3331 from = 0;
3332 to = XVECTOR (vector)->size;
3335 for (i = from; i < to; i++)
3337 QUIT;
3339 if (CHAR_TABLE_P (vector))
3341 if (char_table_depth == 0 && i >= CHAR_TABLE_SINGLE_BYTE_SLOTS)
3342 complete_char = 0;
3344 if (i >= CHAR_TABLE_SINGLE_BYTE_SLOTS
3345 && !CHARSET_DEFINED_P (i - 128))
3346 continue;
3348 definition
3349 = get_keyelt (XCHAR_TABLE (vector)->contents[i], 0);
3351 else
3352 definition = get_keyelt (AREF (vector, i), 0);
3354 if (NILP (definition)) continue;
3356 /* Don't mention suppressed commands. */
3357 if (SYMBOLP (definition) && partial)
3359 Lisp_Object tem;
3361 tem = Fget (definition, suppress);
3363 if (!NILP (tem)) continue;
3366 /* Set CHARACTER to the character this entry describes, if any.
3367 Also update *INDICES. */
3368 if (CHAR_TABLE_P (vector))
3370 indices[char_table_depth] = i;
3372 if (char_table_depth == 0)
3374 character = i;
3375 indices[0] = i - 128;
3377 else if (complete_char)
3379 character = MAKE_CHAR (indices[0], indices[1], indices[2]);
3381 else
3382 character = 0;
3384 else
3385 character = i;
3387 /* If this binding is shadowed by some other map, ignore it. */
3388 if (!NILP (shadow) && complete_char)
3390 Lisp_Object tem;
3392 ASET (kludge, 0, make_number (character));
3393 tem = shadow_lookup (shadow, kludge, Qt);
3395 if (!NILP (tem)) continue;
3398 /* Ignore this definition if it is shadowed by an earlier
3399 one in the same keymap. */
3400 if (!NILP (entire_map) && complete_char)
3402 Lisp_Object tem;
3404 ASET (kludge, 0, make_number (character));
3405 tem = Flookup_key (entire_map, kludge, Qt);
3407 if (!EQ (tem, definition))
3408 continue;
3411 if (first)
3413 if (char_table_depth == 0)
3414 insert ("\n", 1);
3415 first = 0;
3418 /* For a sub char-table, show the depth by indentation.
3419 CHAR_TABLE_DEPTH can be greater than 0 only for a char-table. */
3420 if (char_table_depth > 0)
3421 insert (" ", char_table_depth * 2); /* depth is 1 or 2. */
3423 /* Output the prefix that applies to every entry in this map. */
3424 if (!NILP (elt_prefix))
3425 insert1 (elt_prefix);
3427 /* Insert or describe the character this slot is for,
3428 or a description of what it is for. */
3429 if (SUB_CHAR_TABLE_P (vector))
3431 if (complete_char)
3432 insert_char (character);
3433 else
3435 /* We need an octal representation for this block of
3436 characters. */
3437 char work[16];
3438 sprintf (work, "(row %d)", i);
3439 insert (work, strlen (work));
3442 else if (CHAR_TABLE_P (vector))
3444 if (complete_char)
3445 insert1 (Fsingle_key_description (make_number (character), Qnil));
3446 else
3448 /* Print the information for this character set. */
3449 insert_string ("<");
3450 tem2 = CHARSET_TABLE_INFO (i - 128, CHARSET_SHORT_NAME_IDX);
3451 if (STRINGP (tem2))
3452 insert_from_string (tem2, 0, 0, SCHARS (tem2),
3453 SBYTES (tem2), 0);
3454 else
3455 insert ("?", 1);
3456 insert (">", 1);
3459 else
3461 insert1 (Fsingle_key_description (make_number (character), Qnil));
3464 /* If we find a sub char-table within a char-table,
3465 scan it recursively; it defines the details for
3466 a character set or a portion of a character set. */
3467 if (CHAR_TABLE_P (vector) && SUB_CHAR_TABLE_P (definition))
3469 insert ("\n", 1);
3470 describe_vector (definition, elt_prefix, args, elt_describer,
3471 partial, shadow, entire_map,
3472 indices, char_table_depth + 1);
3473 continue;
3476 starting_i = i;
3478 /* Find all consecutive characters or rows that have the same
3479 definition. But, for elements of a top level char table, if
3480 they are for charsets, we had better describe one by one even
3481 if they have the same definition. */
3482 if (CHAR_TABLE_P (vector))
3484 int limit = to;
3486 if (char_table_depth == 0)
3487 limit = CHAR_TABLE_SINGLE_BYTE_SLOTS;
3489 while (i + 1 < limit
3490 && (tem2 = get_keyelt (XCHAR_TABLE (vector)->contents[i + 1], 0),
3491 !NILP (tem2))
3492 && !NILP (Fequal (tem2, definition)))
3493 i++;
3495 else
3496 while (i + 1 < to
3497 && (tem2 = get_keyelt (AREF (vector, i + 1), 0),
3498 !NILP (tem2))
3499 && !NILP (Fequal (tem2, definition)))
3500 i++;
3503 /* If we have a range of more than one character,
3504 print where the range reaches to. */
3506 if (i != starting_i)
3508 insert (" .. ", 4);
3510 if (!NILP (elt_prefix))
3511 insert1 (elt_prefix);
3513 if (CHAR_TABLE_P (vector))
3515 if (char_table_depth == 0)
3517 insert1 (Fsingle_key_description (make_number (i), Qnil));
3519 else if (complete_char)
3521 indices[char_table_depth] = i;
3522 character = MAKE_CHAR (indices[0], indices[1], indices[2]);
3523 insert_char (character);
3525 else
3527 /* We need an octal representation for this block of
3528 characters. */
3529 char work[16];
3530 sprintf (work, "(row %d)", i);
3531 insert (work, strlen (work));
3534 else
3536 insert1 (Fsingle_key_description (make_number (i), Qnil));
3540 /* Print a description of the definition of this character.
3541 elt_describer will take care of spacing out far enough
3542 for alignment purposes. */
3543 (*elt_describer) (definition, args);
3546 /* For (sub) char-table, print `defalt' slot at last. */
3547 if (CHAR_TABLE_P (vector) && !NILP (XCHAR_TABLE (vector)->defalt))
3549 insert (" ", char_table_depth * 2);
3550 insert_string ("<<default>>");
3551 (*elt_describer) (XCHAR_TABLE (vector)->defalt, args);
3554 UNGCPRO;
3557 /* Apropos - finding all symbols whose names match a regexp. */
3558 static Lisp_Object apropos_predicate;
3559 static Lisp_Object apropos_accumulate;
3561 static void
3562 apropos_accum (symbol, string)
3563 Lisp_Object symbol, string;
3565 register Lisp_Object tem;
3567 tem = Fstring_match (string, Fsymbol_name (symbol), Qnil);
3568 if (!NILP (tem) && !NILP (apropos_predicate))
3569 tem = call1 (apropos_predicate, symbol);
3570 if (!NILP (tem))
3571 apropos_accumulate = Fcons (symbol, apropos_accumulate);
3574 DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0,
3575 doc: /* Show all symbols whose names contain match for REGEXP.
3576 If optional 2nd arg PREDICATE is non-nil, (funcall PREDICATE SYMBOL) is done
3577 for each symbol and a symbol is mentioned only if that returns non-nil.
3578 Return list of symbols found. */)
3579 (regexp, predicate)
3580 Lisp_Object regexp, predicate;
3582 Lisp_Object tem;
3583 CHECK_STRING (regexp);
3584 apropos_predicate = predicate;
3585 apropos_accumulate = Qnil;
3586 map_obarray (Vobarray, apropos_accum, regexp);
3587 tem = Fsort (apropos_accumulate, Qstring_lessp);
3588 apropos_accumulate = Qnil;
3589 apropos_predicate = Qnil;
3590 return tem;
3593 void
3594 syms_of_keymap ()
3596 Qkeymap = intern ("keymap");
3597 staticpro (&Qkeymap);
3598 staticpro (&apropos_predicate);
3599 staticpro (&apropos_accumulate);
3600 apropos_predicate = Qnil;
3601 apropos_accumulate = Qnil;
3603 /* Now we are ready to set up this property, so we can
3604 create char tables. */
3605 Fput (Qkeymap, Qchar_table_extra_slots, make_number (0));
3607 /* Initialize the keymaps standardly used.
3608 Each one is the value of a Lisp variable, and is also
3609 pointed to by a C variable */
3611 global_map = Fmake_keymap (Qnil);
3612 Fset (intern ("global-map"), global_map);
3614 current_global_map = global_map;
3615 staticpro (&global_map);
3616 staticpro (&current_global_map);
3618 meta_map = Fmake_keymap (Qnil);
3619 Fset (intern ("esc-map"), meta_map);
3620 Ffset (intern ("ESC-prefix"), meta_map);
3622 control_x_map = Fmake_keymap (Qnil);
3623 Fset (intern ("ctl-x-map"), control_x_map);
3624 Ffset (intern ("Control-X-prefix"), control_x_map);
3626 exclude_keys
3627 = Fcons (Fcons (build_string ("DEL"), build_string ("\\d")),
3628 Fcons (Fcons (build_string ("TAB"), build_string ("\\t")),
3629 Fcons (Fcons (build_string ("RET"), build_string ("\\r")),
3630 Fcons (Fcons (build_string ("ESC"), build_string ("\\e")),
3631 Fcons (Fcons (build_string ("SPC"), build_string (" ")),
3632 Qnil)))));
3633 staticpro (&exclude_keys);
3635 DEFVAR_LISP ("define-key-rebound-commands", &Vdefine_key_rebound_commands,
3636 doc: /* List of commands given new key bindings recently.
3637 This is used for internal purposes during Emacs startup;
3638 don't alter it yourself. */);
3639 Vdefine_key_rebound_commands = Qt;
3641 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map,
3642 doc: /* Default keymap to use when reading from the minibuffer. */);
3643 Vminibuffer_local_map = Fmake_sparse_keymap (Qnil);
3645 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map,
3646 doc: /* Local keymap for the minibuffer when spaces are not allowed. */);
3647 Vminibuffer_local_ns_map = Fmake_sparse_keymap (Qnil);
3648 Fset_keymap_parent (Vminibuffer_local_ns_map, Vminibuffer_local_map);
3650 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map,
3651 doc: /* Local keymap for minibuffer input with completion. */);
3652 Vminibuffer_local_completion_map = Fmake_sparse_keymap (Qnil);
3653 Fset_keymap_parent (Vminibuffer_local_completion_map, Vminibuffer_local_map);
3655 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map,
3656 doc: /* Local keymap for minibuffer input with completion, for exact match. */);
3657 Vminibuffer_local_must_match_map = Fmake_sparse_keymap (Qnil);
3658 Fset_keymap_parent (Vminibuffer_local_must_match_map,
3659 Vminibuffer_local_completion_map);
3661 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist,
3662 doc: /* Alist of keymaps to use for minor modes.
3663 Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read
3664 key sequences and look up bindings iff VARIABLE's value is non-nil.
3665 If two active keymaps bind the same key, the keymap appearing earlier
3666 in the list takes precedence. */);
3667 Vminor_mode_map_alist = Qnil;
3669 DEFVAR_LISP ("minor-mode-overriding-map-alist", &Vminor_mode_overriding_map_alist,
3670 doc: /* Alist of keymaps to use for minor modes, in current major mode.
3671 This variable is an alist just like `minor-mode-map-alist', and it is
3672 used the same way (and before `minor-mode-map-alist'); however,
3673 it is provided for major modes to bind locally. */);
3674 Vminor_mode_overriding_map_alist = Qnil;
3676 DEFVAR_LISP ("emulation-mode-map-alists", &Vemulation_mode_map_alists,
3677 doc: /* List of keymap alists to use for emulations modes.
3678 It is intended for modes or packages using multiple minor-mode keymaps.
3679 Each element is a keymap alist just like `minor-mode-map-alist', or a
3680 symbol with a variable binding which is a keymap alist, and it is used
3681 the same way. The "active" keymaps in each alist are used before
3682 `minor-mode-map-alist' and `minor-mode-overriding-map-alist'. */);
3683 Vemulation_mode_map_alists = Qnil;
3686 DEFVAR_LISP ("function-key-map", &Vfunction_key_map,
3687 doc: /* Keymap mapping ASCII function key sequences onto their preferred forms.
3688 This allows Emacs to recognize function keys sent from ASCII
3689 terminals at any point in a key sequence.
3691 The `read-key-sequence' function replaces any subsequence bound by
3692 `function-key-map' with its binding. More precisely, when the active
3693 keymaps have no binding for the current key sequence but
3694 `function-key-map' binds a suffix of the sequence to a vector or string,
3695 `read-key-sequence' replaces the matching suffix with its binding, and
3696 continues with the new sequence.
3698 The events that come from bindings in `function-key-map' are not
3699 themselves looked up in `function-key-map'.
3701 For example, suppose `function-key-map' binds `ESC O P' to [f1].
3702 Typing `ESC O P' to `read-key-sequence' would return [f1]. Typing
3703 `C-x ESC O P' would return [?\\C-x f1]. If [f1] were a prefix
3704 key, typing `ESC O P x' would return [f1 x]. */);
3705 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
3707 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
3708 doc: /* Keymap of key translations that can override keymaps.
3709 This keymap works like `function-key-map', but comes after that,
3710 and applies even for keys that have ordinary bindings. */);
3711 Vkey_translation_map = Qnil;
3713 staticpro (&Vmenu_events);
3714 Vmenu_events = Fcons (intern ("menu-bar"),
3715 Fcons (intern ("tool-bar"),
3716 Fcons (intern ("mouse-1"),
3717 Fcons (intern ("mouse-2"),
3718 Fcons (intern ("mouse-3"),
3719 Qnil)))));
3722 Qsingle_key_description = intern ("single-key-description");
3723 staticpro (&Qsingle_key_description);
3725 Qkey_description = intern ("key-description");
3726 staticpro (&Qkey_description);
3728 Qkeymapp = intern ("keymapp");
3729 staticpro (&Qkeymapp);
3731 Qnon_ascii = intern ("non-ascii");
3732 staticpro (&Qnon_ascii);
3734 Qmenu_item = intern ("menu-item");
3735 staticpro (&Qmenu_item);
3737 Qremap = intern ("remap");
3738 staticpro (&Qremap);
3740 command_remapping_vector = Fmake_vector (make_number (2), Qremap);
3741 staticpro (&command_remapping_vector);
3743 where_is_cache_keymaps = Qt;
3744 where_is_cache = Qnil;
3745 staticpro (&where_is_cache);
3746 staticpro (&where_is_cache_keymaps);
3748 defsubr (&Skeymapp);
3749 defsubr (&Skeymap_parent);
3750 defsubr (&Skeymap_prompt);
3751 defsubr (&Sset_keymap_parent);
3752 defsubr (&Smake_keymap);
3753 defsubr (&Smake_sparse_keymap);
3754 defsubr (&Smap_keymap);
3755 defsubr (&Scopy_keymap);
3756 defsubr (&Scommand_remapping);
3757 defsubr (&Skey_binding);
3758 defsubr (&Slocal_key_binding);
3759 defsubr (&Sglobal_key_binding);
3760 defsubr (&Sminor_mode_key_binding);
3761 defsubr (&Sdefine_key);
3762 defsubr (&Slookup_key);
3763 defsubr (&Sdefine_prefix_command);
3764 defsubr (&Suse_global_map);
3765 defsubr (&Suse_local_map);
3766 defsubr (&Scurrent_local_map);
3767 defsubr (&Scurrent_global_map);
3768 defsubr (&Scurrent_minor_mode_maps);
3769 defsubr (&Scurrent_active_maps);
3770 defsubr (&Saccessible_keymaps);
3771 defsubr (&Skey_description);
3772 defsubr (&Sdescribe_vector);
3773 defsubr (&Ssingle_key_description);
3774 defsubr (&Stext_char_description);
3775 defsubr (&Swhere_is_internal);
3776 defsubr (&Sdescribe_buffer_bindings);
3777 defsubr (&Sapropos_internal);
3780 void
3781 keys_of_keymap ()
3783 initial_define_key (global_map, 033, "ESC-prefix");
3784 initial_define_key (global_map, Ctl('X'), "Control-X-prefix");