1 /* Manipulation of keymaps
2 Copyright (C) 1985, 86,87,88,93,94,95,98,99, 2000, 01, 02, 03
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)
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. */
28 #include "character.h"
31 #include "termhooks.h"
32 #include "blockinput.h"
34 #include "intervals.h"
37 /* The number of elements in keymap vectors. */
38 #define DENSE_TABLE_SIZE (0200)
40 /* Actually allocate storage for these variables */
42 Lisp_Object current_global_map
; /* Current global keymap */
44 Lisp_Object global_map
; /* default global key bindings */
46 Lisp_Object meta_map
; /* The keymap used for globally bound
47 ESC-prefixed default commands */
49 Lisp_Object control_x_map
; /* The keymap used for globally bound
50 C-x-prefixed default commands */
52 /* was MinibufLocalMap */
53 Lisp_Object Vminibuffer_local_map
;
54 /* The keymap used by the minibuf for local
55 bindings when spaces are allowed in the
58 /* was MinibufLocalNSMap */
59 Lisp_Object Vminibuffer_local_ns_map
;
60 /* The keymap used by the minibuf for local
61 bindings when spaces are not encouraged
64 /* keymap used for minibuffers when doing completion */
65 /* was MinibufLocalCompletionMap */
66 Lisp_Object Vminibuffer_local_completion_map
;
68 /* keymap used for minibuffers when doing completion and require a match */
69 /* was MinibufLocalMustMatchMap */
70 Lisp_Object Vminibuffer_local_must_match_map
;
72 /* Alist of minor mode variables and keymaps. */
73 Lisp_Object Vminor_mode_map_alist
;
75 /* Alist of major-mode-specific overrides for
76 minor mode variables and keymaps. */
77 Lisp_Object Vminor_mode_overriding_map_alist
;
79 /* Keymap mapping ASCII function key sequences onto their preferred forms.
80 Initialized by the terminal-specific lisp files. See DEFVAR for more
82 Lisp_Object Vfunction_key_map
;
84 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
85 Lisp_Object Vkey_translation_map
;
87 /* A list of all commands given new bindings since a certain time
88 when nil was stored here.
89 This is used to speed up recomputation of menu key equivalents
90 when Emacs starts up. t means don't record anything here. */
91 Lisp_Object Vdefine_key_rebound_commands
;
93 Lisp_Object Qkeymapp
, Qkeymap
, Qnon_ascii
, Qmenu_item
, Qremap
;
95 /* Alist of elements like (DEL . "\d"). */
96 static Lisp_Object exclude_keys
;
98 /* Pre-allocated 2-element vector for Fremap_command to use. */
99 static Lisp_Object remap_command_vector
;
101 /* A char with the CHAR_META bit set in a vector or the 0200 bit set
102 in a string key sequence is equivalent to prefixing with this
104 extern Lisp_Object meta_prefix_char
;
106 extern Lisp_Object Voverriding_local_map
;
108 /* Hash table used to cache a reverse-map to speed up calls to where-is. */
109 static Lisp_Object where_is_cache
;
110 /* Which keymaps are reverse-stored in the cache. */
111 static Lisp_Object where_is_cache_keymaps
;
113 static Lisp_Object store_in_keymap
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
));
114 static void fix_submap_inheritance
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
));
116 static Lisp_Object define_as_prefix
P_ ((Lisp_Object
, Lisp_Object
));
117 static void describe_command
P_ ((Lisp_Object
, Lisp_Object
));
118 static void describe_translation
P_ ((Lisp_Object
, Lisp_Object
));
119 static void describe_map
P_ ((Lisp_Object
, Lisp_Object
,
120 void (*) P_ ((Lisp_Object
, Lisp_Object
)),
121 int, Lisp_Object
, Lisp_Object
*, int));
122 static void silly_event_symbol_error
P_ ((Lisp_Object
));
124 /* Keymap object support - constructors and predicates. */
126 DEFUN ("make-keymap", Fmake_keymap
, Smake_keymap
, 0, 1, 0,
127 doc
: /* Construct and return a new keymap, of the form (keymap CHARTABLE . ALIST).
128 CHARTABLE is a char-table that holds the bindings for the ASCII
129 characters. ALIST is an assoc-list which holds bindings for function keys,
130 mouse events, and any other things that appear in the input stream.
131 All entries in it are initially nil, meaning "command undefined".
133 The optional arg STRING supplies a menu name for the keymap
134 in case you use it as a menu with `x-popup-menu'. */)
140 tail
= Fcons (string
, Qnil
);
143 return Fcons (Qkeymap
,
144 Fcons (Fmake_char_table (Qkeymap
, Qnil
), tail
));
147 DEFUN ("make-sparse-keymap", Fmake_sparse_keymap
, Smake_sparse_keymap
, 0, 1, 0,
148 doc
: /* Construct and return a new sparse keymap.
149 Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),
150 which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),
151 which binds the function key or mouse event SYMBOL to DEFINITION.
152 Initially the alist is nil.
154 The optional arg STRING supplies a menu name for the keymap
155 in case you use it as a menu with `x-popup-menu'. */)
160 return Fcons (Qkeymap
, Fcons (string
, Qnil
));
161 return Fcons (Qkeymap
, Qnil
);
164 /* This function is used for installing the standard key bindings
165 at initialization time.
169 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); */
172 initial_define_key (keymap
, key
, defname
)
177 store_in_keymap (keymap
, make_number (key
), intern (defname
));
181 initial_define_lispy_key (keymap
, keyname
, defname
)
186 store_in_keymap (keymap
, intern (keyname
), intern (defname
));
189 DEFUN ("keymapp", Fkeymapp
, Skeymapp
, 1, 1, 0,
190 doc
: /* Return t if OBJECT is a keymap.
192 A keymap is a list (keymap . ALIST),
193 or a symbol whose function definition is itself a keymap.
194 ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);
195 a vector of densely packed bindings for small character codes
196 is also allowed as an element. */)
200 return (KEYMAPP (object
) ? Qt
: Qnil
);
203 DEFUN ("keymap-prompt", Fkeymap_prompt
, Skeymap_prompt
, 1, 1, 0,
204 doc
: /* Return the prompt-string of a keymap MAP.
205 If non-nil, the prompt is shown in the echo-area
206 when reading a key-sequence to be looked-up in this keymap. */)
212 register Lisp_Object tem
;
221 /* Check that OBJECT is a keymap (after dereferencing through any
222 symbols). If it is, return it.
224 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
225 is an autoload form, do the autoload and try again.
226 If AUTOLOAD is nonzero, callers must assume GC is possible.
228 If the map needs to be autoloaded, but AUTOLOAD is zero (and ERROR
229 is zero as well), return Qt.
231 ERROR controls how we respond if OBJECT isn't a keymap.
232 If ERROR is non-zero, signal an error; otherwise, just return Qnil.
234 Note that most of the time, we don't want to pursue autoloads.
235 Functions like Faccessible_keymaps which scan entire keymap trees
236 shouldn't load every autoloaded keymap. I'm not sure about this,
237 but it seems to me that only read_key_sequence, Flookup_key, and
238 Fdefine_key should cause keymaps to be autoloaded.
240 This function can GC when AUTOLOAD is non-zero, because it calls
241 do_autoload which can GC. */
244 get_keymap (object
, error
, autoload
)
253 if (CONSP (object
) && EQ (XCAR (object
), Qkeymap
))
256 tem
= indirect_function (object
);
259 if (EQ (XCAR (tem
), Qkeymap
))
262 /* Should we do an autoload? Autoload forms for keymaps have
263 Qkeymap as their fifth element. */
264 if ((autoload
|| !error
) && EQ (XCAR (tem
), Qautoload
))
268 tail
= Fnth (make_number (4), tem
);
269 if (EQ (tail
, Qkeymap
))
273 struct gcpro gcpro1
, gcpro2
;
275 GCPRO2 (tem
, object
);
276 do_autoload (tem
, object
);
289 wrong_type_argument (Qkeymapp
, object
);
293 /* Return the parent map of the keymap MAP, or nil if it has none.
294 We assume that MAP is a valid keymap. */
296 DEFUN ("keymap-parent", Fkeymap_parent
, Skeymap_parent
, 1, 1, 0,
297 doc
: /* Return the parent keymap of KEYMAP. */)
303 keymap
= get_keymap (keymap
, 1, 1);
305 /* Skip past the initial element `keymap'. */
306 list
= XCDR (keymap
);
307 for (; CONSP (list
); list
= XCDR (list
))
309 /* See if there is another `keymap'. */
314 return get_keymap (list
, 0, 1);
318 /* Check whether MAP is one of MAPS parents. */
320 keymap_memberp (map
, maps
)
321 Lisp_Object map
, maps
;
323 if (NILP (map
)) return 0;
324 while (KEYMAPP (maps
) && !EQ (map
, maps
))
325 maps
= Fkeymap_parent (maps
);
326 return (EQ (map
, maps
));
329 /* Set the parent keymap of MAP to PARENT. */
331 DEFUN ("set-keymap-parent", Fset_keymap_parent
, Sset_keymap_parent
, 2, 2, 0,
332 doc
: /* Modify KEYMAP to set its parent map to PARENT.
333 PARENT should be nil or another keymap. */)
335 Lisp_Object keymap
, parent
;
337 Lisp_Object list
, prev
;
341 /* Force a keymap flush for the next call to where-is.
342 Since this can be called from within where-is, we don't set where_is_cache
343 directly but only where_is_cache_keymaps, since where_is_cache shouldn't
344 be changed during where-is, while where_is_cache_keymaps is only used at
345 the very beginning of where-is and can thus be changed here without any
347 This is a very minor correctness (rather than safety) issue. */
348 where_is_cache_keymaps
= Qt
;
350 keymap
= get_keymap (keymap
, 1, 1);
355 parent
= get_keymap (parent
, 1, 1);
357 /* Check for cycles. */
358 if (keymap_memberp (keymap
, parent
))
359 error ("Cyclic keymap inheritance");
362 /* Skip past the initial element `keymap'. */
367 /* If there is a parent keymap here, replace it.
368 If we came to the end, add the parent in PREV. */
369 if (!CONSP (list
) || KEYMAPP (list
))
371 /* If we already have the right parent, return now
372 so that we avoid the loops below. */
373 if (EQ (XCDR (prev
), parent
))
374 RETURN_UNGCPRO (parent
);
376 XSETCDR (prev
, parent
);
382 /* Scan through for submaps, and set their parents too. */
384 for (list
= XCDR (keymap
); CONSP (list
); list
= XCDR (list
))
386 /* Stop the scan when we come to the parent. */
387 if (EQ (XCAR (list
), Qkeymap
))
390 /* If this element holds a prefix map, deal with it. */
391 if (CONSP (XCAR (list
))
392 && CONSP (XCDR (XCAR (list
))))
393 fix_submap_inheritance (keymap
, XCAR (XCAR (list
)),
396 if (VECTORP (XCAR (list
)))
397 for (i
= 0; i
< XVECTOR (XCAR (list
))->size
; i
++)
398 if (CONSP (XVECTOR (XCAR (list
))->contents
[i
]))
399 fix_submap_inheritance (keymap
, make_number (i
),
400 XVECTOR (XCAR (list
))->contents
[i
]);
402 if (CHAR_TABLE_P (XCAR (list
)))
404 Lisp_Object indices
[3];
406 map_char_table (fix_submap_inheritance
, Qnil
, XCAR (list
),
411 RETURN_UNGCPRO (parent
);
414 /* EVENT is defined in MAP as a prefix, and SUBMAP is its definition.
415 if EVENT is also a prefix in MAP's parent,
416 make sure that SUBMAP inherits that definition as its own parent. */
419 fix_submap_inheritance (map
, event
, submap
)
420 Lisp_Object map
, event
, submap
;
422 Lisp_Object map_parent
, parent_entry
;
424 /* SUBMAP is a cons that we found as a key binding.
425 Discard the other things found in a menu key binding. */
427 submap
= get_keymap (get_keyelt (submap
, 0), 0, 0);
429 /* If it isn't a keymap now, there's no work to do. */
433 map_parent
= Fkeymap_parent (map
);
434 if (!NILP (map_parent
))
436 get_keymap (access_keymap (map_parent
, event
, 0, 0, 0), 0, 0);
440 /* If MAP's parent has something other than a keymap,
441 our own submap shadows it completely. */
442 if (!CONSP (parent_entry
))
445 if (! EQ (parent_entry
, submap
))
447 Lisp_Object submap_parent
;
448 submap_parent
= submap
;
453 tem
= Fkeymap_parent (submap_parent
);
457 if (keymap_memberp (tem
, parent_entry
))
458 /* Fset_keymap_parent could create a cycle. */
465 Fset_keymap_parent (submap_parent
, parent_entry
);
469 /* Look up IDX in MAP. IDX may be any sort of event.
470 Note that this does only one level of lookup; IDX must be a single
471 event, not a sequence.
473 If T_OK is non-zero, bindings for Qt are treated as default
474 bindings; any key left unmentioned by other tables and bindings is
475 given the binding of Qt.
477 If T_OK is zero, bindings for Qt are not treated specially.
479 If NOINHERIT, don't accept a subkeymap found in an inherited keymap. */
482 access_keymap (map
, idx
, t_ok
, noinherit
, autoload
)
491 /* Qunbound in VAL means we have found no binding yet. */
494 /* If idx is a list (some sort of mouse click, perhaps?),
495 the index we want to use is the car of the list, which
496 ought to be a symbol. */
497 idx
= EVENT_HEAD (idx
);
499 /* If idx is a symbol, it might have modifiers, which need to
500 be put in the canonical order. */
502 idx
= reorder_modifiers (idx
);
503 else if (INTEGERP (idx
))
504 /* Clobber the high bits that can be present on a machine
505 with more than 24 bits of integer. */
506 XSETFASTINT (idx
, XINT (idx
) & (CHAR_META
| (CHAR_META
- 1)));
508 /* Handle the special meta -> esc mapping. */
509 if (INTEGERP (idx
) && XUINT (idx
) & meta_modifier
)
511 /* See if there is a meta-map. If there's none, there is
512 no binding for IDX, unless a default binding exists in MAP. */
513 Lisp_Object meta_map
=
514 get_keymap (access_keymap (map
, meta_prefix_char
,
515 t_ok
, noinherit
, autoload
),
517 if (CONSP (meta_map
))
520 idx
= make_number (XUINT (idx
) & ~meta_modifier
);
523 /* Set IDX to t, so that we only find a default binding. */
526 /* We know there is no binding. */
533 /* t_binding is where we put a default binding that applies,
534 to use in case we do not find a binding specifically
535 for this key sequence. */
537 Lisp_Object t_binding
;
540 /* If `t_ok' is 2, both `t' is accepted. */
543 for (tail
= XCDR (map
);
545 || (tail
= get_keymap (tail
, 0, autoload
), CONSP (tail
)));
550 binding
= XCAR (tail
);
551 if (SYMBOLP (binding
))
553 /* If NOINHERIT, stop finding prefix definitions
554 after we pass a second occurrence of the `keymap' symbol. */
555 if (noinherit
&& EQ (binding
, Qkeymap
))
558 else if (CONSP (binding
))
560 Lisp_Object key
= XCAR (binding
);
563 val
= XCDR (binding
);
564 else if (t_ok
> 1 && EQ (key
, Qt
))
566 t_binding
= XCDR (binding
);
570 else if (VECTORP (binding
))
572 if (NATNUMP (idx
) && XFASTINT (idx
) < ASIZE (binding
))
573 val
= AREF (binding
, XFASTINT (idx
));
575 else if (CHAR_TABLE_P (binding
))
577 /* Character codes with modifiers
578 are not included in a char-table.
579 All character codes without modifiers are included. */
580 if (NATNUMP (idx
) && (XFASTINT (idx
) & CHAR_MODIFIER_MASK
) == 0)
582 val
= Faref (binding
, idx
);
583 /* `nil' has a special meaning for char-tables, so
584 we use something else to record an explicitly
591 /* If we found a binding, clean it up and return it. */
592 if (!EQ (val
, Qunbound
))
595 /* A Qt binding is just like an explicit nil binding
596 (i.e. it shadows any parent binding but not bindings in
597 keymaps of lower precedence). */
599 val
= get_keyelt (val
, autoload
);
601 fix_submap_inheritance (map
, idx
, val
);
607 return get_keyelt (t_binding
, autoload
);
611 /* Given OBJECT which was found in a slot in a keymap,
612 trace indirect definitions to get the actual definition of that slot.
613 An indirect definition is a list of the form
614 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
615 and INDEX is the object to look up in KEYMAP to yield the definition.
617 Also if OBJECT has a menu string as the first element,
618 remove that. Also remove a menu help string as second element.
620 If AUTOLOAD is nonzero, load autoloadable keymaps
621 that are referred to with indirection. */
624 get_keyelt (object
, autoload
)
625 register Lisp_Object object
;
630 if (!(CONSP (object
)))
631 /* This is really the value. */
634 /* If the keymap contents looks like (keymap ...) or (lambda ...)
636 else if (EQ (XCAR (object
), Qkeymap
) || EQ (XCAR (object
), Qlambda
))
639 /* If the keymap contents looks like (menu-item name . DEFN)
640 or (menu-item name DEFN ...) then use DEFN.
641 This is a new format menu item. */
642 else if (EQ (XCAR (object
), Qmenu_item
))
644 if (CONSP (XCDR (object
)))
648 object
= XCDR (XCDR (object
));
651 object
= XCAR (object
);
653 /* If there's a `:filter FILTER', apply FILTER to the
654 menu-item's definition to get the real definition to
656 for (; CONSP (tem
) && CONSP (XCDR (tem
)); tem
= XCDR (tem
))
657 if (EQ (XCAR (tem
), QCfilter
) && autoload
)
660 filter
= XCAR (XCDR (tem
));
661 filter
= list2 (filter
, list2 (Qquote
, object
));
662 object
= menu_item_eval_property (filter
);
671 /* If the keymap contents looks like (STRING . DEFN), use DEFN.
672 Keymap alist elements like (CHAR MENUSTRING . DEFN)
673 will be used by HierarKey menus. */
674 else if (STRINGP (XCAR (object
)))
676 object
= XCDR (object
);
677 /* Also remove a menu help string, if any,
678 following the menu item name. */
679 if (CONSP (object
) && STRINGP (XCAR (object
)))
680 object
= XCDR (object
);
681 /* Also remove the sublist that caches key equivalences, if any. */
682 if (CONSP (object
) && CONSP (XCAR (object
)))
685 carcar
= XCAR (XCAR (object
));
686 if (NILP (carcar
) || VECTORP (carcar
))
687 object
= XCDR (object
);
691 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
695 map
= get_keymap (Fcar_safe (object
), 0, autoload
);
696 return (!CONSP (map
) ? object
/* Invalid keymap */
697 : access_keymap (map
, Fcdr (object
), 0, 0, autoload
));
703 store_in_keymap (keymap
, idx
, def
)
705 register Lisp_Object idx
;
706 register Lisp_Object def
;
708 /* Flush any reverse-map cache. */
709 where_is_cache
= Qnil
;
710 where_is_cache_keymaps
= Qt
;
712 /* If we are preparing to dump, and DEF is a menu element
713 with a menu item indicator, copy it to ensure it is not pure. */
714 if (CONSP (def
) && PURE_P (def
)
715 && (EQ (XCAR (def
), Qmenu_item
) || STRINGP (XCAR (def
))))
716 def
= Fcons (XCAR (def
), XCDR (def
));
718 if (!CONSP (keymap
) || !EQ (XCAR (keymap
), Qkeymap
))
719 error ("attempt to define a key in a non-keymap");
721 /* If idx is a list (some sort of mouse click, perhaps?),
722 the index we want to use is the car of the list, which
723 ought to be a symbol. */
724 idx
= EVENT_HEAD (idx
);
726 /* If idx is a symbol, it might have modifiers, which need to
727 be put in the canonical order. */
729 idx
= reorder_modifiers (idx
);
730 else if (INTEGERP (idx
))
731 /* Clobber the high bits that can be present on a machine
732 with more than 24 bits of integer. */
733 XSETFASTINT (idx
, XINT (idx
) & (CHAR_META
| (CHAR_META
- 1)));
735 /* Scan the keymap for a binding of idx. */
739 /* The cons after which we should insert new bindings. If the
740 keymap has a table element, we record its position here, so new
741 bindings will go after it; this way, the table will stay
742 towards the front of the alist and character lookups in dense
743 keymaps will remain fast. Otherwise, this just points at the
744 front of the keymap. */
745 Lisp_Object insertion_point
;
747 insertion_point
= keymap
;
748 for (tail
= XCDR (keymap
); CONSP (tail
); tail
= XCDR (tail
))
755 if (NATNUMP (idx
) && XFASTINT (idx
) < ASIZE (elt
))
757 ASET (elt
, XFASTINT (idx
), def
);
760 insertion_point
= tail
;
762 else if (CHAR_TABLE_P (elt
))
764 /* Character codes with modifiers
765 are not included in a char-table.
766 All character codes without modifiers are included. */
767 if (NATNUMP (idx
) && !(XFASTINT (idx
) & CHAR_MODIFIER_MASK
))
770 /* `nil' has a special meaning for char-tables, so
771 we use something else to record an explicitly
773 NILP (def
) ? Qt
: def
);
776 else if (CONSP (idx
) && CHARACTERP (XCAR (idx
)))
778 Fset_char_table_range (elt
, idx
, NILP (def
) ? Qt
: def
);
781 insertion_point
= tail
;
783 else if (CONSP (elt
))
785 if (EQ (idx
, XCAR (elt
)))
791 else if (EQ (elt
, Qkeymap
))
792 /* If we find a 'keymap' symbol in the spine of KEYMAP,
793 then we must have found the start of a second keymap
794 being used as the tail of KEYMAP, and a binding for IDX
795 should be inserted before it. */
802 /* We have scanned the entire keymap, and not found a binding for
803 IDX. Let's add one. */
804 XSETCDR (insertion_point
,
805 Fcons (Fcons (idx
, def
), XCDR (insertion_point
)));
811 EXFUN (Fcopy_keymap
, 1);
814 copy_keymap_1 (chartable
, idx
, elt
)
815 Lisp_Object chartable
, idx
, elt
;
817 if (CONSP (elt
) && EQ (XCAR (elt
), Qkeymap
))
818 Faset (chartable
, idx
, Fcopy_keymap (elt
));
821 DEFUN ("copy-keymap", Fcopy_keymap
, Scopy_keymap
, 1, 1, 0,
822 doc
: /* Return a copy of the keymap KEYMAP.
823 The copy starts out with the same definitions of KEYMAP,
824 but changing either the copy or KEYMAP does not affect the other.
825 Any key definitions that are subkeymaps are recursively copied.
826 However, a key definition which is a symbol whose definition is a keymap
831 /* FIXME: This doesn't properly copy menu-items in vectors. */
832 /* FIXME: This also copies the parent keymap. */
834 register Lisp_Object copy
, tail
;
836 copy
= Fcopy_alist (get_keymap (keymap
, 1, 0));
838 for (tail
= copy
; CONSP (tail
); tail
= XCDR (tail
))
843 if (CHAR_TABLE_P (elt
))
845 Lisp_Object indices
[3];
847 elt
= Fcopy_sequence (elt
);
850 map_char_table (copy_keymap_1
, Qnil
, elt
, elt
, 0, indices
);
852 else if (VECTORP (elt
))
856 elt
= Fcopy_sequence (elt
);
859 for (i
= 0; i
< ASIZE (elt
); i
++)
860 if (CONSP (AREF (elt
, i
)) && EQ (XCAR (AREF (elt
, i
)), Qkeymap
))
861 ASET (elt
, i
, Fcopy_keymap (AREF (elt
, i
)));
863 else if (CONSP (elt
) && CONSP (XCDR (elt
)))
868 /* Is this a new format menu item. */
869 if (EQ (XCAR (tem
),Qmenu_item
))
871 /* Copy cell with menu-item marker. */
873 Fcons (XCAR (tem
), XCDR (tem
)));
878 /* Copy cell with menu-item name. */
880 Fcons (XCAR (tem
), XCDR (tem
)));
886 /* Copy cell with binding and if the binding is a keymap,
889 Fcons (XCAR (tem
), XCDR (tem
)));
892 if (CONSP (tem
) && EQ (XCAR (tem
), Qkeymap
))
893 XSETCAR (elt
, Fcopy_keymap (tem
));
895 if (CONSP (tem
) && CONSP (XCAR (tem
)))
896 /* Delete cache for key equivalences. */
897 XSETCDR (elt
, XCDR (tem
));
902 /* It may be an old fomat menu item.
903 Skip the optional menu string.
905 if (STRINGP (XCAR (tem
)))
907 /* Copy the cell, since copy-alist didn't go this deep. */
909 Fcons (XCAR (tem
), XCDR (tem
)));
912 /* Also skip the optional menu help string. */
913 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
916 Fcons (XCAR (tem
), XCDR (tem
)));
920 /* There may also be a list that caches key equivalences.
921 Just delete it for the new keymap. */
923 && CONSP (XCAR (tem
))
924 && (NILP (XCAR (XCAR (tem
)))
925 || VECTORP (XCAR (XCAR (tem
)))))
926 XSETCDR (elt
, XCDR (tem
));
929 && CONSP (XCDR (elt
))
930 && EQ (XCAR (XCDR (elt
)), Qkeymap
))
931 XSETCDR (elt
, Fcopy_keymap (XCDR (elt
)));
940 /* Simple Keymap mutators and accessors. */
942 /* GC is possible in this function if it autoloads a keymap. */
944 DEFUN ("define-key", Fdefine_key
, Sdefine_key
, 3, 3, 0,
945 doc
: /* Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.
948 KEY is a string or a vector of symbols and characters meaning a
949 sequence of keystrokes and events. Non-ASCII characters with codes
950 above 127 (such as ISO Latin-1) can be included if you use a vector.
952 DEF is anything that can be a key's definition:
953 nil (means key is undefined in this keymap),
954 a command (a Lisp function suitable for interactive calling)
955 a string (treated as a keyboard macro),
956 a keymap (to define a prefix key),
957 a symbol. When the key is looked up, the symbol will stand for its
958 function definition, which should at that time be one of the above,
959 or another symbol whose function definition is used, etc.
960 a cons (STRING . DEFN), meaning that DEFN is the definition
961 (DEFN should be a valid definition in its own right),
962 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.
964 If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at
965 the front of KEYMAP. */)
972 register Lisp_Object c
;
973 register Lisp_Object cmd
;
977 struct gcpro gcpro1
, gcpro2
, gcpro3
;
979 keymap
= get_keymap (keymap
, 1, 1);
981 if (!VECTORP (key
) && !STRINGP (key
))
982 key
= wrong_type_argument (Qarrayp
, key
);
984 length
= XFASTINT (Flength (key
));
988 /* Check for valid [remap COMMAND] bindings. */
989 if (VECTORP (key
) && EQ (AREF (key
, 0), Qremap
)
990 && (length
!= 2 || !SYMBOLP (AREF (key
, 1))))
991 wrong_type_argument (Qvectorp
, key
);
993 if (SYMBOLP (def
) && !EQ (Vdefine_key_rebound_commands
, Qt
))
994 Vdefine_key_rebound_commands
= Fcons (def
, Vdefine_key_rebound_commands
);
996 GCPRO3 (keymap
, key
, def
);
999 meta_bit
= meta_modifier
;
1006 c
= Faref (key
, make_number (idx
));
1010 /* C may be a cons (FROM . TO) specifying a range of
1012 if (CHARACTERP (XCAR (c
)))
1013 CHECK_CHARACTER (XCDR (c
));
1014 else if (lucid_event_type_list_p (c
))
1015 c
= Fevent_convert_list (c
);
1019 silly_event_symbol_error (c
);
1022 && (XINT (c
) & meta_bit
)
1025 c
= meta_prefix_char
;
1031 XSETINT (c
, XINT (c
) & ~meta_bit
);
1037 if (!INTEGERP (c
) && !SYMBOLP (c
)
1039 /* If C is a range, it must be a leaf. */
1040 || (INTEGERP (XCAR (c
)) && idx
!= length
)))
1041 error ("Key sequence contains invalid event");
1044 RETURN_UNGCPRO (store_in_keymap (keymap
, c
, def
));
1046 cmd
= access_keymap (keymap
, c
, 0, 1, 1);
1048 /* If this key is undefined, make it a prefix. */
1050 cmd
= define_as_prefix (keymap
, c
);
1052 keymap
= get_keymap (cmd
, 0, 1);
1053 if (!CONSP (keymap
))
1054 /* We must use Fkey_description rather than just passing key to
1055 error; key might be a vector, not a string. */
1056 error ("Key sequence %s uses invalid prefix characters",
1057 XSTRING (Fkey_description (key
))->data
);
1061 /* This function may GC (it calls Fkey_binding). */
1063 DEFUN ("remap-command", Fremap_command
, Sremap_command
, 1, 1, 0,
1064 doc
: /* Return the remapping for command COMMAND in current keymaps.
1065 Returns nil if COMMAND is not remapped. */)
1067 Lisp_Object command
;
1069 /* This will GCPRO the command argument. */
1070 ASET (remap_command_vector
, 1, command
);
1071 return Fkey_binding (remap_command_vector
, Qnil
, Qt
);
1074 /* Value is number if KEY is too long; nil if valid but has no definition. */
1075 /* GC is possible in this function if it autoloads a keymap. */
1077 DEFUN ("lookup-key", Flookup_key
, Slookup_key
, 2, 3, 0,
1078 doc
: /* In keymap KEYMAP, look up key sequence KEY. Return the definition.
1079 nil means undefined. See doc of `define-key' for kinds of definitions.
1081 A number as value means KEY is "too long";
1082 that is, characters or symbols in it except for the last one
1083 fail to be a valid sequence of prefix characters in KEYMAP.
1084 The number is how many characters at the front of KEY
1085 it takes to reach a non-prefix command.
1087 Normally, `lookup-key' ignores bindings for t, which act as default
1088 bindings, used when nothing else in the keymap applies; this makes it
1089 usable as a general function for probing keymaps. However, if the
1090 third optional argument ACCEPT-DEFAULT is non-nil, `lookup-key' will
1091 recognize the default bindings, just as `read-key-sequence' does. */)
1092 (keymap
, key
, accept_default
)
1093 register Lisp_Object keymap
;
1095 Lisp_Object accept_default
;
1098 register Lisp_Object cmd
;
1099 register Lisp_Object c
;
1101 int t_ok
= !NILP (accept_default
);
1102 struct gcpro gcpro1
;
1104 keymap
= get_keymap (keymap
, 1, 1);
1106 /* Perform command remapping initiated by Fremap_command directly.
1107 This is strictly not necessary, but it is faster and it returns
1108 nil instead of 1 if KEYMAP doesn't contain command remappings. */
1109 if (EQ (key
, remap_command_vector
))
1111 /* KEY has format [remap COMMAND].
1112 Lookup `remap' in KEYMAP; result is nil or a keymap containing
1113 command remappings. Then lookup COMMAND in that keymap. */
1114 if ((keymap
= access_keymap (keymap
, Qremap
, t_ok
, 0, 1), !NILP (keymap
))
1115 && (keymap
= get_keymap (keymap
, 0, 1), CONSP (keymap
)))
1116 return access_keymap (keymap
, AREF (key
, 1), t_ok
, 0, 1);
1120 if (!VECTORP (key
) && !STRINGP (key
))
1121 key
= wrong_type_argument (Qarrayp
, key
);
1123 length
= XFASTINT (Flength (key
));
1132 c
= Faref (key
, make_number (idx
++));
1134 if (CONSP (c
) && lucid_event_type_list_p (c
))
1135 c
= Fevent_convert_list (c
);
1137 /* Turn the 8th bit of string chars into a meta modifier. */
1138 if (XINT (c
) & 0x80 && STRINGP (key
))
1139 XSETINT (c
, (XINT (c
) | meta_modifier
) & ~0x80);
1141 /* Allow string since binding for `menu-bar-select-buffer'
1142 includes the buffer name in the key sequence. */
1143 if (!INTEGERP (c
) && !SYMBOLP (c
) && !CONSP (c
) && !STRINGP (c
))
1144 error ("Key sequence contains invalid event");
1146 cmd
= access_keymap (keymap
, c
, t_ok
, 0, 1);
1148 RETURN_UNGCPRO (cmd
);
1150 keymap
= get_keymap (cmd
, 0, 1);
1151 if (!CONSP (keymap
))
1152 RETURN_UNGCPRO (make_number (idx
));
1158 /* Make KEYMAP define event C as a keymap (i.e., as a prefix).
1159 Assume that currently it does not define C at all.
1160 Return the keymap. */
1163 define_as_prefix (keymap
, c
)
1164 Lisp_Object keymap
, c
;
1168 cmd
= Fmake_sparse_keymap (Qnil
);
1169 /* If this key is defined as a prefix in an inherited keymap,
1170 make it a prefix in this map, and make its definition
1171 inherit the other prefix definition. */
1172 cmd
= nconc2 (cmd
, access_keymap (keymap
, c
, 0, 0, 0));
1173 store_in_keymap (keymap
, c
, cmd
);
1178 /* Append a key to the end of a key sequence. We always make a vector. */
1181 append_key (key_sequence
, key
)
1182 Lisp_Object key_sequence
, key
;
1184 Lisp_Object args
[2];
1186 args
[0] = key_sequence
;
1188 args
[1] = Fcons (key
, Qnil
);
1189 return Fvconcat (2, args
);
1192 /* Given a event type C which is a symbol,
1193 signal an error if is a mistake such as RET or M-RET or C-DEL, etc. */
1196 silly_event_symbol_error (c
)
1199 Lisp_Object parsed
, base
, name
, assoc
;
1202 parsed
= parse_modifiers (c
);
1203 modifiers
= (int) XUINT (XCAR (XCDR (parsed
)));
1204 base
= XCAR (parsed
);
1205 name
= Fsymbol_name (base
);
1206 /* This alist includes elements such as ("RET" . "\\r"). */
1207 assoc
= Fassoc (name
, exclude_keys
);
1211 char new_mods
[sizeof ("\\A-\\C-\\H-\\M-\\S-\\s-")];
1213 Lisp_Object keystring
;
1214 if (modifiers
& alt_modifier
)
1215 { *p
++ = '\\'; *p
++ = 'A'; *p
++ = '-'; }
1216 if (modifiers
& ctrl_modifier
)
1217 { *p
++ = '\\'; *p
++ = 'C'; *p
++ = '-'; }
1218 if (modifiers
& hyper_modifier
)
1219 { *p
++ = '\\'; *p
++ = 'H'; *p
++ = '-'; }
1220 if (modifiers
& meta_modifier
)
1221 { *p
++ = '\\'; *p
++ = 'M'; *p
++ = '-'; }
1222 if (modifiers
& shift_modifier
)
1223 { *p
++ = '\\'; *p
++ = 'S'; *p
++ = '-'; }
1224 if (modifiers
& super_modifier
)
1225 { *p
++ = '\\'; *p
++ = 's'; *p
++ = '-'; }
1228 c
= reorder_modifiers (c
);
1229 keystring
= concat2 (build_string (new_mods
), XCDR (assoc
));
1231 error ((modifiers
& ~meta_modifier
1232 ? "To bind the key %s, use [?%s], not [%s]"
1233 : "To bind the key %s, use \"%s\", not [%s]"),
1234 XSYMBOL (c
)->name
->data
, XSTRING (keystring
)->data
,
1235 XSYMBOL (c
)->name
->data
);
1239 /* Global, local, and minor mode keymap stuff. */
1241 /* We can't put these variables inside current_minor_maps, since under
1242 some systems, static gets macro-defined to be the empty string.
1244 static Lisp_Object
*cmm_modes
, *cmm_maps
;
1245 static int cmm_size
;
1247 /* Error handler used in current_minor_maps. */
1249 current_minor_maps_error ()
1254 /* Store a pointer to an array of the keymaps of the currently active
1255 minor modes in *buf, and return the number of maps it contains.
1257 This function always returns a pointer to the same buffer, and may
1258 free or reallocate it, so if you want to keep it for a long time or
1259 hand it out to lisp code, copy it. This procedure will be called
1260 for every key sequence read, so the nice lispy approach (return a
1261 new assoclist, list, what have you) for each invocation would
1262 result in a lot of consing over time.
1264 If we used xrealloc/xmalloc and ran out of memory, they would throw
1265 back to the command loop, which would try to read a key sequence,
1266 which would call this function again, resulting in an infinite
1267 loop. Instead, we'll use realloc/malloc and silently truncate the
1268 list, let the key sequence be read, and hope some other piece of
1269 code signals the error. */
1271 current_minor_maps (modeptr
, mapptr
)
1272 Lisp_Object
**modeptr
, **mapptr
;
1275 int list_number
= 0;
1276 Lisp_Object alist
, assoc
, var
, val
;
1277 Lisp_Object lists
[2];
1279 lists
[0] = Vminor_mode_overriding_map_alist
;
1280 lists
[1] = Vminor_mode_map_alist
;
1282 for (list_number
= 0; list_number
< 2; list_number
++)
1283 for (alist
= lists
[list_number
];
1285 alist
= XCDR (alist
))
1286 if ((assoc
= XCAR (alist
), CONSP (assoc
))
1287 && (var
= XCAR (assoc
), SYMBOLP (var
))
1288 && (val
= find_symbol_value (var
), !EQ (val
, Qunbound
))
1293 /* If a variable has an entry in Vminor_mode_overriding_map_alist,
1294 and also an entry in Vminor_mode_map_alist,
1295 ignore the latter. */
1296 if (list_number
== 1)
1298 val
= assq_no_quit (var
, lists
[0]);
1305 Lisp_Object
*newmodes
, *newmaps
;
1307 /* Use malloc/realloc here. See the comment above
1314 = (Lisp_Object
*) realloc (cmm_modes
,
1315 cmm_size
* sizeof *newmodes
);
1317 = (Lisp_Object
*) realloc (cmm_maps
,
1318 cmm_size
* sizeof *newmaps
);
1326 = (Lisp_Object
*) malloc (cmm_size
* sizeof *newmodes
);
1328 = (Lisp_Object
*) malloc (cmm_size
* sizeof *newmaps
);
1333 cmm_modes
= newmodes
;
1337 if (newmodes
== NULL
|| newmaps
== NULL
)
1341 /* Get the keymap definition--or nil if it is not defined. */
1342 temp
= internal_condition_case_1 (Findirect_function
,
1344 Qerror
, current_minor_maps_error
);
1348 cmm_maps
[i
] = temp
;
1353 if (modeptr
) *modeptr
= cmm_modes
;
1354 if (mapptr
) *mapptr
= cmm_maps
;
1358 DEFUN ("current-active-maps", Fcurrent_active_maps
, Scurrent_active_maps
,
1360 doc
: /* Return a list of the currently active keymaps.
1361 OLP if non-nil indicates that we should obey `overriding-local-map' and
1362 `overriding-terminal-local-map'. */)
1366 Lisp_Object keymaps
= Fcons (current_global_map
, Qnil
);
1370 if (!NILP (Voverriding_local_map
))
1371 keymaps
= Fcons (Voverriding_local_map
, keymaps
);
1372 if (!NILP (current_kboard
->Voverriding_terminal_local_map
))
1373 keymaps
= Fcons (current_kboard
->Voverriding_terminal_local_map
, keymaps
);
1375 if (NILP (XCDR (keymaps
)))
1381 local
= get_local_map (PT
, current_buffer
, Qlocal_map
);
1383 keymaps
= Fcons (local
, keymaps
);
1385 nmaps
= current_minor_maps (0, &maps
);
1387 for (i
= --nmaps
; i
>= 0; i
--)
1388 if (!NILP (maps
[i
]))
1389 keymaps
= Fcons (maps
[i
], keymaps
);
1391 local
= get_local_map (PT
, current_buffer
, Qkeymap
);
1393 keymaps
= Fcons (local
, keymaps
);
1399 /* GC is possible in this function if it autoloads a keymap. */
1401 DEFUN ("key-binding", Fkey_binding
, Skey_binding
, 1, 3, 0,
1402 doc
: /* Return the binding for command KEY in current keymaps.
1403 KEY is a string or vector, a sequence of keystrokes.
1404 The binding is probably a symbol with a function definition.
1406 Normally, `key-binding' ignores bindings for t, which act as default
1407 bindings, used when nothing else in the keymap applies; this makes it
1408 usable as a general function for probing keymaps. However, if the
1409 optional second argument ACCEPT-DEFAULT is non-nil, `key-binding' does
1410 recognize the default bindings, just as `read-key-sequence' does.
1412 Like the normal command loop, `key-binding' will remap the command
1413 resulting from looking up KEY by looking up the command in the
1414 currrent keymaps. However, if the optional third argument NO-REMAP
1415 is non-nil, `key-binding' returns the unmapped command. */)
1416 (key
, accept_default
, no_remap
)
1417 Lisp_Object key
, accept_default
, no_remap
;
1419 Lisp_Object
*maps
, value
;
1421 struct gcpro gcpro1
;
1425 if (!NILP (current_kboard
->Voverriding_terminal_local_map
))
1427 value
= Flookup_key (current_kboard
->Voverriding_terminal_local_map
,
1428 key
, accept_default
);
1429 if (! NILP (value
) && !INTEGERP (value
))
1432 else if (!NILP (Voverriding_local_map
))
1434 value
= Flookup_key (Voverriding_local_map
, key
, accept_default
);
1435 if (! NILP (value
) && !INTEGERP (value
))
1442 local
= get_local_map (PT
, current_buffer
, Qkeymap
);
1445 value
= Flookup_key (local
, key
, accept_default
);
1446 if (! NILP (value
) && !INTEGERP (value
))
1450 nmaps
= current_minor_maps (0, &maps
);
1451 /* Note that all these maps are GCPRO'd
1452 in the places where we found them. */
1454 for (i
= 0; i
< nmaps
; i
++)
1455 if (! NILP (maps
[i
]))
1457 value
= Flookup_key (maps
[i
], key
, accept_default
);
1458 if (! NILP (value
) && !INTEGERP (value
))
1462 local
= get_local_map (PT
, current_buffer
, Qlocal_map
);
1465 value
= Flookup_key (local
, key
, accept_default
);
1466 if (! NILP (value
) && !INTEGERP (value
))
1471 value
= Flookup_key (current_global_map
, key
, accept_default
);
1475 if (NILP (value
) || INTEGERP (value
))
1478 /* If the result of the ordinary keymap lookup is an interactive
1479 command, look for a key binding (ie. remapping) for that command. */
1481 if (NILP (no_remap
) && SYMBOLP (value
))
1484 if (value1
= Fremap_command (value
), !NILP (value1
))
1491 /* GC is possible in this function if it autoloads a keymap. */
1493 DEFUN ("local-key-binding", Flocal_key_binding
, Slocal_key_binding
, 1, 2, 0,
1494 doc
: /* Return the binding for command KEYS in current local keymap only.
1495 KEYS is a string, a sequence of keystrokes.
1496 The binding is probably a symbol with a function definition.
1498 If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1499 bindings; see the description of `lookup-key' for more details about this. */)
1500 (keys
, accept_default
)
1501 Lisp_Object keys
, accept_default
;
1503 register Lisp_Object map
;
1504 map
= current_buffer
->keymap
;
1507 return Flookup_key (map
, keys
, accept_default
);
1510 /* GC is possible in this function if it autoloads a keymap. */
1512 DEFUN ("global-key-binding", Fglobal_key_binding
, Sglobal_key_binding
, 1, 2, 0,
1513 doc
: /* Return the binding for command KEYS in current global keymap only.
1514 KEYS is a string, a sequence of keystrokes.
1515 The binding is probably a symbol with a function definition.
1516 This function's return values are the same as those of lookup-key
1519 If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1520 bindings; see the description of `lookup-key' for more details about this. */)
1521 (keys
, accept_default
)
1522 Lisp_Object keys
, accept_default
;
1524 return Flookup_key (current_global_map
, keys
, accept_default
);
1527 /* GC is possible in this function if it autoloads a keymap. */
1529 DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding
, Sminor_mode_key_binding
, 1, 2, 0,
1530 doc
: /* Find the visible minor mode bindings of KEY.
1531 Return an alist of pairs (MODENAME . BINDING), where MODENAME is the
1532 the symbol which names the minor mode binding KEY, and BINDING is
1533 KEY's definition in that mode. In particular, if KEY has no
1534 minor-mode bindings, return nil. If the first binding is a
1535 non-prefix, all subsequent bindings will be omitted, since they would
1536 be ignored. Similarly, the list doesn't include non-prefix bindings
1537 that come after prefix bindings.
1539 If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1540 bindings; see the description of `lookup-key' for more details about this. */)
1541 (key
, accept_default
)
1542 Lisp_Object key
, accept_default
;
1544 Lisp_Object
*modes
, *maps
;
1546 Lisp_Object binding
;
1548 struct gcpro gcpro1
, gcpro2
;
1550 nmaps
= current_minor_maps (&modes
, &maps
);
1551 /* Note that all these maps are GCPRO'd
1552 in the places where we found them. */
1555 GCPRO2 (key
, binding
);
1557 for (i
= j
= 0; i
< nmaps
; i
++)
1559 && !NILP (binding
= Flookup_key (maps
[i
], key
, accept_default
))
1560 && !INTEGERP (binding
))
1562 if (KEYMAPP (binding
))
1563 maps
[j
++] = Fcons (modes
[i
], binding
);
1565 RETURN_UNGCPRO (Fcons (Fcons (modes
[i
], binding
), Qnil
));
1569 return Flist (j
, maps
);
1572 DEFUN ("define-prefix-command", Fdefine_prefix_command
, Sdefine_prefix_command
, 1, 3, 0,
1573 doc
: /* Define COMMAND as a prefix command. COMMAND should be a symbol.
1574 A new sparse keymap is stored as COMMAND's function definition and its value.
1575 If a second optional argument MAPVAR is given, the map is stored as
1576 its value instead of as COMMAND's value; but COMMAND is still defined
1578 The third optional argument NAME, if given, supplies a menu name
1579 string for the map. This is required to use the keymap as a menu. */)
1580 (command
, mapvar
, name
)
1581 Lisp_Object command
, mapvar
, name
;
1584 map
= Fmake_sparse_keymap (name
);
1585 Ffset (command
, map
);
1589 Fset (command
, map
);
1593 DEFUN ("use-global-map", Fuse_global_map
, Suse_global_map
, 1, 1, 0,
1594 doc
: /* Select KEYMAP as the global keymap. */)
1598 keymap
= get_keymap (keymap
, 1, 1);
1599 current_global_map
= keymap
;
1604 DEFUN ("use-local-map", Fuse_local_map
, Suse_local_map
, 1, 1, 0,
1605 doc
: /* Select KEYMAP as the local keymap.
1606 If KEYMAP is nil, that means no local keymap. */)
1611 keymap
= get_keymap (keymap
, 1, 1);
1613 current_buffer
->keymap
= keymap
;
1618 DEFUN ("current-local-map", Fcurrent_local_map
, Scurrent_local_map
, 0, 0, 0,
1619 doc
: /* Return current buffer's local keymap, or nil if it has none. */)
1622 return current_buffer
->keymap
;
1625 DEFUN ("current-global-map", Fcurrent_global_map
, Scurrent_global_map
, 0, 0, 0,
1626 doc
: /* Return the current global keymap. */)
1629 return current_global_map
;
1632 DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps
, Scurrent_minor_mode_maps
, 0, 0, 0,
1633 doc
: /* Return a list of keymaps for the minor modes of the current buffer. */)
1637 int nmaps
= current_minor_maps (0, &maps
);
1639 return Flist (nmaps
, maps
);
1642 /* Help functions for describing and documenting keymaps. */
1646 accessible_keymaps_1 (key
, cmd
, maps
, tail
, thisseq
, is_metized
)
1647 Lisp_Object maps
, tail
, thisseq
, key
, cmd
;
1648 int is_metized
; /* If 1, `key' is assumed to be INTEGERP. */
1652 cmd
= get_keyelt (cmd
, 0);
1656 tem
= get_keymap (cmd
, 0, 0);
1660 /* Ignore keymaps that are already added to maps. */
1661 tem
= Frassq (cmd
, maps
);
1664 /* If the last key in thisseq is meta-prefix-char,
1665 turn it into a meta-ized keystroke. We know
1666 that the event we're about to append is an
1667 ascii keystroke since we're processing a
1671 int meta_bit
= meta_modifier
;
1672 Lisp_Object last
= make_number (XINT (Flength (thisseq
)) - 1);
1673 tem
= Fcopy_sequence (thisseq
);
1675 Faset (tem
, last
, make_number (XINT (key
) | meta_bit
));
1677 /* This new sequence is the same length as
1678 thisseq, so stick it in the list right
1681 Fcons (Fcons (tem
, cmd
), XCDR (tail
)));
1685 tem
= append_key (thisseq
, key
);
1686 nconc2 (tail
, Fcons (Fcons (tem
, cmd
), Qnil
));
1693 accessible_keymaps_char_table (args
, index
, cmd
)
1694 Lisp_Object args
, index
, cmd
;
1696 accessible_keymaps_1 (index
, cmd
,
1700 XINT (XCDR (XCAR (args
))));
1703 /* This function cannot GC. */
1705 DEFUN ("accessible-keymaps", Faccessible_keymaps
, Saccessible_keymaps
,
1707 doc
: /* Find all keymaps accessible via prefix characters from KEYMAP.
1708 Returns a list of elements of the form (KEYS . MAP), where the sequence
1709 KEYS starting from KEYMAP gets you to MAP. These elements are ordered
1710 so that the KEYS increase in length. The first element is ([] . KEYMAP).
1711 An optional argument PREFIX, if non-nil, should be a key sequence;
1712 then the value includes only maps for prefixes that start with PREFIX. */)
1714 Lisp_Object keymap
, prefix
;
1716 Lisp_Object maps
, good_maps
, tail
;
1719 /* no need for gcpro because we don't autoload any keymaps. */
1722 prefixlen
= XINT (Flength (prefix
));
1726 /* If a prefix was specified, start with the keymap (if any) for
1727 that prefix, so we don't waste time considering other prefixes. */
1729 tem
= Flookup_key (keymap
, prefix
, Qt
);
1730 /* Flookup_key may give us nil, or a number,
1731 if the prefix is not defined in this particular map.
1732 It might even give us a list that isn't a keymap. */
1733 tem
= get_keymap (tem
, 0, 0);
1736 /* Convert PREFIX to a vector now, so that later on
1737 we don't have to deal with the possibility of a string. */
1738 if (STRINGP (prefix
))
1743 copy
= Fmake_vector (make_number (XSTRING (prefix
)->size
), Qnil
);
1744 for (i
= 0, i_byte
= 0; i
< XSTRING (prefix
)->size
;)
1748 FETCH_STRING_CHAR_ADVANCE (c
, prefix
, i
, i_byte
);
1749 if (SINGLE_BYTE_CHAR_P (c
) && (c
& 0200))
1750 c
^= 0200 | meta_modifier
;
1751 ASET (copy
, i_before
, make_number (c
));
1755 maps
= Fcons (Fcons (prefix
, tem
), Qnil
);
1761 maps
= Fcons (Fcons (Fmake_vector (make_number (0), Qnil
),
1762 get_keymap (keymap
, 1, 0)),
1765 /* For each map in the list maps,
1766 look at any other maps it points to,
1767 and stick them at the end if they are not already in the list.
1769 This is a breadth-first traversal, where tail is the queue of
1770 nodes, and maps accumulates a list of all nodes visited. */
1772 for (tail
= maps
; CONSP (tail
); tail
= XCDR (tail
))
1774 register Lisp_Object thisseq
, thismap
;
1776 /* Does the current sequence end in the meta-prefix-char? */
1779 thisseq
= Fcar (Fcar (tail
));
1780 thismap
= Fcdr (Fcar (tail
));
1781 last
= make_number (XINT (Flength (thisseq
)) - 1);
1782 is_metized
= (XINT (last
) >= 0
1783 /* Don't metize the last char of PREFIX. */
1784 && XINT (last
) >= prefixlen
1785 && EQ (Faref (thisseq
, last
), meta_prefix_char
));
1787 for (; CONSP (thismap
); thismap
= XCDR (thismap
))
1791 elt
= XCAR (thismap
);
1795 if (CHAR_TABLE_P (elt
))
1797 Lisp_Object indices
[3];
1799 map_char_table (accessible_keymaps_char_table
, Qnil
,
1800 elt
, Fcons (Fcons (maps
, make_number (is_metized
)),
1801 Fcons (tail
, thisseq
)),
1804 else if (VECTORP (elt
))
1808 /* Vector keymap. Scan all the elements. */
1809 for (i
= 0; i
< ASIZE (elt
); i
++)
1810 accessible_keymaps_1 (make_number (i
), AREF (elt
, i
),
1811 maps
, tail
, thisseq
, is_metized
);
1814 else if (CONSP (elt
))
1815 accessible_keymaps_1 (XCAR (elt
), XCDR (elt
),
1816 maps
, tail
, thisseq
,
1817 is_metized
&& INTEGERP (XCAR (elt
)));
1825 /* Now find just the maps whose access prefixes start with PREFIX. */
1828 for (; CONSP (maps
); maps
= XCDR (maps
))
1830 Lisp_Object elt
, thisseq
;
1832 thisseq
= XCAR (elt
);
1833 /* The access prefix must be at least as long as PREFIX,
1834 and the first elements must match those of PREFIX. */
1835 if (XINT (Flength (thisseq
)) >= prefixlen
)
1838 for (i
= 0; i
< prefixlen
; i
++)
1841 XSETFASTINT (i1
, i
);
1842 if (!EQ (Faref (thisseq
, i1
), Faref (prefix
, i1
)))
1846 good_maps
= Fcons (elt
, good_maps
);
1850 return Fnreverse (good_maps
);
1853 Lisp_Object Qsingle_key_description
, Qkey_description
;
1855 /* This function cannot GC. */
1857 DEFUN ("key-description", Fkey_description
, Skey_description
, 1, 1, 0,
1858 doc
: /* Return a pretty description of key-sequence KEYS.
1859 Control characters turn into "C-foo" sequences, meta into "M-foo"
1860 spaces are put between sequence elements, etc. */)
1867 Lisp_Object
*args
= NULL
;
1872 vector
= Fmake_vector (Flength (keys
), Qnil
);
1873 for (i
= 0, i_byte
= 0; i
< XSTRING (keys
)->size
; )
1878 FETCH_STRING_CHAR_ADVANCE (c
, keys
, i
, i_byte
);
1879 if (SINGLE_BYTE_CHAR_P (c
) && (c
& 0200))
1880 c
^= 0200 | meta_modifier
;
1881 XSETFASTINT (AREF (vector
, i_before
), c
);
1888 /* In effect, this computes
1889 (mapconcat 'single-key-description keys " ")
1890 but we shouldn't use mapconcat because it can do GC. */
1892 len
= XVECTOR (keys
)->size
;
1893 sep
= build_string (" ");
1894 /* This has one extra element at the end that we don't pass to Fconcat. */
1895 args
= (Lisp_Object
*) alloca (len
* 2 * sizeof (Lisp_Object
));
1897 for (i
= 0; i
< len
; i
++)
1899 args
[i
* 2] = Fsingle_key_description (AREF (keys
, i
), Qnil
);
1900 args
[i
* 2 + 1] = sep
;
1903 else if (CONSP (keys
))
1905 /* In effect, this computes
1906 (mapconcat 'single-key-description keys " ")
1907 but we shouldn't use mapconcat because it can do GC. */
1909 len
= XFASTINT (Flength (keys
));
1910 sep
= build_string (" ");
1911 /* This has one extra element at the end that we don't pass to Fconcat. */
1912 args
= (Lisp_Object
*) alloca (len
* 2 * sizeof (Lisp_Object
));
1914 for (i
= 0; i
< len
; i
++)
1916 args
[i
* 2] = Fsingle_key_description (XCAR (keys
), Qnil
);
1917 args
[i
* 2 + 1] = sep
;
1922 keys
= wrong_type_argument (Qarrayp
, keys
);
1925 return empty_string
;
1926 return Fconcat (len
* 2 - 1, args
);
1930 push_key_description (c
, p
, force_multibyte
)
1931 register unsigned int c
;
1933 int force_multibyte
;
1937 /* Clear all the meaningless bits above the meta bit. */
1938 c
&= meta_modifier
| ~ - meta_modifier
;
1939 c2
= c
& ~(alt_modifier
| ctrl_modifier
| hyper_modifier
1940 | meta_modifier
| shift_modifier
| super_modifier
);
1942 if (c
& alt_modifier
)
1948 if ((c
& ctrl_modifier
) != 0
1949 || (c2
< ' ' && c2
!= 27 && c2
!= '\t' && c2
!= Ctl ('M')))
1953 c
&= ~ctrl_modifier
;
1955 if (c
& hyper_modifier
)
1959 c
-= hyper_modifier
;
1961 if (c
& meta_modifier
)
1967 if (c
& shift_modifier
)
1971 c
-= shift_modifier
;
1973 if (c
& super_modifier
)
1977 c
-= super_modifier
;
1993 else if (c
== Ctl ('M'))
2001 /* `C-' already added above. */
2002 if (c
> 0 && c
<= Ctl ('Z'))
2021 || (NILP (current_buffer
->enable_multibyte_characters
)
2022 && SINGLE_BYTE_CHAR_P (c
)
2023 && !force_multibyte
))
2027 else if (CHARACTERP (make_number (c
)))
2029 if (NILP (current_buffer
->enable_multibyte_characters
)
2030 && ! force_multibyte
)
2031 *p
++ = multibyte_char_to_unibyte (c
, Qnil
);
2033 p
+= CHAR_STRING (c
, (unsigned char *) p
);
2039 /* The biggest character code uses 22 bits. */
2040 for (bit_offset
= 21; bit_offset
>= 0; bit_offset
-= 3)
2042 if (c
>= (1 << bit_offset
))
2043 *p
++ = ((c
& (7 << bit_offset
)) >> bit_offset
) + '0';
2050 /* This function cannot GC. */
2052 DEFUN ("single-key-description", Fsingle_key_description
,
2053 Ssingle_key_description
, 1, 2, 0,
2054 doc
: /* Return a pretty description of command character KEY.
2055 Control characters turn into C-whatever, etc.
2056 Optional argument NO-ANGLES non-nil means don't put angle brackets
2057 around function keys and event symbols. */)
2059 Lisp_Object key
, no_angles
;
2061 if (CONSP (key
) && lucid_event_type_list_p (key
))
2062 key
= Fevent_convert_list (key
);
2064 key
= EVENT_HEAD (key
);
2066 if (INTEGERP (key
)) /* Normal character */
2068 char tem
[KEY_DESCRIPTION_SIZE
];
2070 *push_key_description (XUINT (key
), tem
, 1) = 0;
2071 return build_string (tem
);
2073 else if (SYMBOLP (key
)) /* Function key or event-symbol */
2075 if (NILP (no_angles
))
2078 = (char *) alloca (STRING_BYTES (XSYMBOL (key
)->name
) + 5);
2079 sprintf (buffer
, "<%s>", XSYMBOL (key
)->name
->data
);
2080 return build_string (buffer
);
2083 return Fsymbol_name (key
);
2085 else if (STRINGP (key
)) /* Buffer names in the menubar. */
2086 return Fcopy_sequence (key
);
2088 error ("KEY must be an integer, cons, symbol, or string");
2093 push_text_char_description (c
, p
)
2094 register unsigned int c
;
2106 *p
++ = c
+ 64; /* 'A' - 1 */
2118 /* This function cannot GC. */
2120 DEFUN ("text-char-description", Ftext_char_description
, Stext_char_description
, 1, 1, 0,
2121 doc
: /* Return a pretty description of file-character CHARACTER.
2122 Control characters turn into "^char", etc. */)
2124 Lisp_Object character
;
2126 /* Currently MAX_MULTIBYTE_LENGTH is 4 (< 6). */
2127 unsigned char str
[6];
2130 CHECK_NUMBER (character
);
2132 c
= XINT (character
);
2133 if (!ASCII_CHAR_P (c
))
2135 int len
= CHAR_STRING (c
, str
);
2137 return make_multibyte_string (str
, 1, len
);
2140 *push_text_char_description (c
& 0377, str
) = 0;
2142 return build_string (str
);
2145 /* Return non-zero if SEQ contains only ASCII characters, perhaps with
2148 ascii_sequence_p (seq
)
2152 int len
= XINT (Flength (seq
));
2154 for (i
= 0; i
< len
; i
++)
2156 Lisp_Object ii
, elt
;
2158 XSETFASTINT (ii
, i
);
2159 elt
= Faref (seq
, ii
);
2162 || (XUINT (elt
) & ~CHAR_META
) >= 0x80)
2170 /* where-is - finding a command in a set of keymaps. */
2172 static Lisp_Object
where_is_internal ();
2173 static Lisp_Object
where_is_internal_1 ();
2174 static void where_is_internal_2 ();
2176 /* Like Flookup_key, but uses a list of keymaps SHADOW instead of a single map.
2177 Returns the first non-nil binding found in any of those maps. */
2180 shadow_lookup (shadow
, key
, flag
)
2181 Lisp_Object shadow
, key
, flag
;
2183 Lisp_Object tail
, value
;
2185 for (tail
= shadow
; CONSP (tail
); tail
= XCDR (tail
))
2187 value
= Flookup_key (XCAR (tail
), key
, flag
);
2188 if (!NILP (value
) && !NATNUMP (value
))
2194 /* This function can GC if Flookup_key autoloads any keymaps. */
2197 where_is_internal (definition
, keymaps
, firstonly
, noindirect
, no_remap
)
2198 Lisp_Object definition
, keymaps
;
2199 Lisp_Object firstonly
, noindirect
, no_remap
;
2201 Lisp_Object maps
= Qnil
;
2202 Lisp_Object found
, sequences
;
2203 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
2204 /* 1 means ignore all menu bindings entirely. */
2205 int nomenus
= !NILP (firstonly
) && !EQ (firstonly
, Qnon_ascii
);
2207 /* If this command is remapped, then it has no key bindings
2209 if (NILP (no_remap
) && SYMBOLP (definition
))
2212 if (tem
= Fremap_command (definition
), !NILP (tem
))
2217 while (CONSP (found
))
2221 Faccessible_keymaps (get_keymap (XCAR (found
), 1, 0), Qnil
));
2222 found
= XCDR (found
);
2225 GCPRO5 (definition
, keymaps
, maps
, found
, sequences
);
2229 for (; !NILP (maps
); maps
= Fcdr (maps
))
2231 /* Key sequence to reach map, and the map that it reaches */
2232 register Lisp_Object
this, map
;
2234 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
2235 [M-CHAR] sequences, check if last character of the sequence
2236 is the meta-prefix char. */
2240 this = Fcar (Fcar (maps
));
2241 map
= Fcdr (Fcar (maps
));
2242 last
= make_number (XINT (Flength (this)) - 1);
2243 last_is_meta
= (XINT (last
) >= 0
2244 && EQ (Faref (this, last
), meta_prefix_char
));
2246 /* if (nomenus && !ascii_sequence_p (this)) */
2247 if (nomenus
&& XINT (last
) >= 0
2248 && !INTEGERP (Faref (this, make_number (0))))
2249 /* If no menu entries should be returned, skip over the
2250 keymaps bound to `menu-bar' and `tool-bar' and other
2251 non-ascii prefixes like `C-down-mouse-2'. */
2258 /* Because the code we want to run on each binding is rather
2259 large, we don't want to have two separate loop bodies for
2260 sparse keymap bindings and tables; we want to iterate one
2261 loop body over both keymap and vector bindings.
2263 For this reason, if Fcar (map) is a vector, we don't
2264 advance map to the next element until i indicates that we
2265 have finished off the vector. */
2266 Lisp_Object elt
, key
, binding
;
2274 /* Set key and binding to the current key and binding, and
2275 advance map and i to the next binding. */
2278 Lisp_Object sequence
;
2280 /* In a vector, look at each element. */
2281 for (i
= 0; i
< XVECTOR (elt
)->size
; i
++)
2283 binding
= AREF (elt
, i
);
2284 XSETFASTINT (key
, i
);
2285 sequence
= where_is_internal_1 (binding
, key
, definition
,
2287 last
, nomenus
, last_is_meta
);
2288 if (!NILP (sequence
))
2289 sequences
= Fcons (sequence
, sequences
);
2292 else if (CHAR_TABLE_P (elt
))
2294 Lisp_Object indices
[3];
2297 args
= Fcons (Fcons (Fcons (definition
, noindirect
),
2298 Qnil
), /* Result accumulator. */
2299 Fcons (Fcons (this, last
),
2300 Fcons (make_number (nomenus
),
2301 make_number (last_is_meta
))));
2302 map_char_table (where_is_internal_2
, Qnil
, elt
, args
,
2304 sequences
= XCDR (XCAR (args
));
2306 else if (CONSP (elt
))
2308 Lisp_Object sequence
;
2311 binding
= XCDR (elt
);
2313 sequence
= where_is_internal_1 (binding
, key
, definition
,
2315 last
, nomenus
, last_is_meta
);
2316 if (!NILP (sequence
))
2317 sequences
= Fcons (sequence
, sequences
);
2321 while (!NILP (sequences
))
2323 Lisp_Object sequence
, remapped
, function
;
2325 sequence
= XCAR (sequences
);
2326 sequences
= XCDR (sequences
);
2328 /* If the current sequence is a command remapping with
2329 format [remap COMMAND], find the key sequences
2330 which run COMMAND, and use those sequences instead. */
2333 && VECTORP (sequence
) && XVECTOR (sequence
)->size
== 2
2334 && EQ (AREF (sequence
, 0), Qremap
)
2335 && (function
= AREF (sequence
, 1), SYMBOLP (function
)))
2337 Lisp_Object remapped1
;
2339 remapped1
= where_is_internal (function
, keymaps
, firstonly
, noindirect
, Qt
);
2340 if (CONSP (remapped1
))
2342 /* Verify that this key binding actually maps to the
2343 remapped command (see below). */
2344 if (!EQ (shadow_lookup (keymaps
, XCAR (remapped1
), Qnil
), function
))
2346 sequence
= XCAR (remapped1
);
2347 remapped
= XCDR (remapped1
);
2348 goto record_sequence
;
2352 /* Verify that this key binding is not shadowed by another
2353 binding for the same key, before we say it exists.
2355 Mechanism: look for local definition of this key and if
2356 it is defined and does not match what we found then
2359 Either nil or number as value from Flookup_key
2361 if (!EQ (shadow_lookup (keymaps
, sequence
, Qnil
), definition
))
2365 /* It is a true unshadowed match. Record it, unless it's already
2366 been seen (as could happen when inheriting keymaps). */
2367 if (NILP (Fmember (sequence
, found
)))
2368 found
= Fcons (sequence
, found
);
2370 /* If firstonly is Qnon_ascii, then we can return the first
2371 binding we find. If firstonly is not Qnon_ascii but not
2372 nil, then we should return the first ascii-only binding
2374 if (EQ (firstonly
, Qnon_ascii
))
2375 RETURN_UNGCPRO (sequence
);
2376 else if (!NILP (firstonly
) && ascii_sequence_p (sequence
))
2377 RETURN_UNGCPRO (sequence
);
2379 if (CONSP (remapped
))
2381 sequence
= XCAR (remapped
);
2382 remapped
= XCDR (remapped
);
2383 goto record_sequence
;
2391 found
= Fnreverse (found
);
2393 /* firstonly may have been t, but we may have gone all the way through
2394 the keymaps without finding an all-ASCII key sequence. So just
2395 return the best we could find. */
2396 if (!NILP (firstonly
))
2397 return Fcar (found
);
2402 DEFUN ("where-is-internal", Fwhere_is_internal
, Swhere_is_internal
, 1, 5, 0,
2403 doc
: /* Return list of keys that invoke DEFINITION.
2404 If KEYMAP is non-nil, search only KEYMAP and the global keymap.
2405 If KEYMAP is nil, search all the currently active keymaps.
2406 If KEYMAP is a list of keymaps, search only those keymaps.
2408 If optional 3rd arg FIRSTONLY is non-nil, return the first key sequence found,
2409 rather than a list of all possible key sequences.
2410 If FIRSTONLY is the symbol `non-ascii', return the first binding found,
2411 no matter what it is.
2412 If FIRSTONLY has another non-nil value, prefer sequences of ASCII characters,
2413 and entirely reject menu bindings.
2415 If optional 4th arg NOINDIRECT is non-nil, don't follow indirections
2416 to other keymaps or slots. This makes it possible to search for an
2417 indirect definition itself.
2419 If optional 5th arg NO-REMAP is non-nil, don't search for key sequences
2420 that invoke a command which is remapped to DEFINITION, but include the
2421 remapped command in the returned list. */)
2422 (definition
, keymap
, firstonly
, noindirect
, no_remap
)
2423 Lisp_Object definition
, keymap
;
2424 Lisp_Object firstonly
, noindirect
, no_remap
;
2426 Lisp_Object sequences
, keymaps
;
2427 /* 1 means ignore all menu bindings entirely. */
2428 int nomenus
= !NILP (firstonly
) && !EQ (firstonly
, Qnon_ascii
);
2431 /* Find the relevant keymaps. */
2432 if (CONSP (keymap
) && KEYMAPP (XCAR (keymap
)))
2434 else if (!NILP (keymap
))
2435 keymaps
= Fcons (keymap
, Fcons (current_global_map
, Qnil
));
2437 keymaps
= Fcurrent_active_maps (Qnil
);
2439 /* Only use caching for the menubar (i.e. called with (def nil t nil).
2440 We don't really need to check `keymap'. */
2441 if (nomenus
&& NILP (noindirect
) && NILP (keymap
))
2445 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
2447 /* Check heuristic-consistency of the cache. */
2448 if (NILP (Fequal (keymaps
, where_is_cache_keymaps
)))
2449 where_is_cache
= Qnil
;
2451 if (NILP (where_is_cache
))
2453 /* We need to create the cache. */
2454 Lisp_Object args
[2];
2455 where_is_cache
= Fmake_hash_table (0, args
);
2456 where_is_cache_keymaps
= Qt
;
2458 /* Fill in the cache. */
2459 GCPRO5 (definition
, keymaps
, firstonly
, noindirect
, no_remap
);
2460 where_is_internal (definition
, keymaps
, firstonly
, noindirect
, no_remap
);
2463 where_is_cache_keymaps
= keymaps
;
2466 /* We want to process definitions from the last to the first.
2467 Instead of consing, copy definitions to a vector and step
2468 over that vector. */
2469 sequences
= Fgethash (definition
, where_is_cache
, Qnil
);
2470 n
= XINT (Flength (sequences
));
2471 defns
= (Lisp_Object
*) alloca (n
* sizeof *defns
);
2472 for (i
= 0; CONSP (sequences
); sequences
= XCDR (sequences
))
2473 defns
[i
++] = XCAR (sequences
);
2475 /* Verify that the key bindings are not shadowed. Note that
2476 the following can GC. */
2477 GCPRO2 (definition
, keymaps
);
2480 for (i
= n
- 1; i
>= 0; --i
)
2481 if (EQ (shadow_lookup (keymaps
, defns
[i
], Qnil
), definition
))
2483 if (ascii_sequence_p (defns
[i
]))
2489 result
= i
>= 0 ? defns
[i
] : (j
>= 0 ? defns
[j
] : Qnil
);
2494 /* Kill the cache so that where_is_internal_1 doesn't think
2495 we're filling it up. */
2496 where_is_cache
= Qnil
;
2497 result
= where_is_internal (definition
, keymaps
, firstonly
, noindirect
, no_remap
);
2503 /* This is the function that Fwhere_is_internal calls using map_char_table.
2505 (((DEFINITION . NOINDIRECT) . (KEYMAP . RESULT))
2507 ((THIS . LAST) . (NOMENUS . LAST_IS_META)))
2508 Since map_char_table doesn't really use the return value from this function,
2509 we the result append to RESULT, the slot in ARGS.
2511 This function can GC because it calls where_is_internal_1 which can
2515 where_is_internal_2 (args
, key
, binding
)
2516 Lisp_Object args
, key
, binding
;
2518 Lisp_Object definition
, noindirect
, this, last
;
2519 Lisp_Object result
, sequence
;
2520 int nomenus
, last_is_meta
;
2521 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2523 GCPRO3 (args
, key
, binding
);
2524 result
= XCDR (XCAR (args
));
2525 definition
= XCAR (XCAR (XCAR (args
)));
2526 noindirect
= XCDR (XCAR (XCAR (args
)));
2527 this = XCAR (XCAR (XCDR (args
)));
2528 last
= XCDR (XCAR (XCDR (args
)));
2529 nomenus
= XFASTINT (XCAR (XCDR (XCDR (args
))));
2530 last_is_meta
= XFASTINT (XCDR (XCDR (XCDR (args
))));
2532 sequence
= where_is_internal_1 (binding
, key
, definition
, noindirect
,
2533 this, last
, nomenus
, last_is_meta
);
2535 if (!NILP (sequence
))
2536 XSETCDR (XCAR (args
), Fcons (sequence
, result
));
2542 /* This function cannot GC. */
2545 where_is_internal_1 (binding
, key
, definition
, noindirect
, this, last
,
2546 nomenus
, last_is_meta
)
2547 Lisp_Object binding
, key
, definition
, noindirect
, this, last
;
2548 int nomenus
, last_is_meta
;
2550 Lisp_Object sequence
;
2552 /* Search through indirections unless that's not wanted. */
2553 if (NILP (noindirect
))
2554 binding
= get_keyelt (binding
, 0);
2556 /* End this iteration if this element does not match
2559 if (!(!NILP (where_is_cache
) /* everything "matches" during cache-fill. */
2560 || EQ (binding
, definition
)
2561 || (CONSP (definition
) && !NILP (Fequal (binding
, definition
)))))
2562 /* Doesn't match. */
2565 /* We have found a match. Construct the key sequence where we found it. */
2566 if (INTEGERP (key
) && last_is_meta
)
2568 sequence
= Fcopy_sequence (this);
2569 Faset (sequence
, last
, make_number (XINT (key
) | meta_modifier
));
2572 sequence
= append_key (this, key
);
2574 if (!NILP (where_is_cache
))
2576 Lisp_Object sequences
= Fgethash (binding
, where_is_cache
, Qnil
);
2577 Fputhash (binding
, Fcons (sequence
, sequences
), where_is_cache
);
2584 /* describe-bindings - summarizing all the bindings in a set of keymaps. */
2586 DEFUN ("describe-buffer-bindings", Fdescribe_buffer_bindings
, Sdescribe_buffer_bindings
, 1, 3, 0,
2587 doc
: /* Insert the list of all defined keys and their definitions.
2588 The list is inserted in the current buffer, while the bindings are
2589 looked up in BUFFER.
2590 The optional argument PREFIX, if non-nil, should be a key sequence;
2591 then we display only bindings that start with that prefix.
2592 The optional argument MENUS, if non-nil, says to mention menu bindings.
2593 \(Ordinarily these are omitted from the output.) */)
2594 (buffer
, prefix
, menus
)
2595 Lisp_Object buffer
, prefix
, menus
;
2597 Lisp_Object outbuf
, shadow
;
2598 int nomenu
= NILP (menus
);
2599 register Lisp_Object start1
;
2600 struct gcpro gcpro1
;
2602 char *alternate_heading
2604 Keyboard translations:\n\n\
2605 You type Translation\n\
2606 -------- -----------\n";
2611 outbuf
= Fcurrent_buffer ();
2613 /* Report on alternates for keys. */
2614 if (STRINGP (Vkeyboard_translate_table
) && !NILP (prefix
))
2617 unsigned char *translate
= XSTRING (Vkeyboard_translate_table
)->data
;
2618 int translate_len
= XSTRING (Vkeyboard_translate_table
)->size
;
2620 for (c
= 0; c
< translate_len
; c
++)
2621 if (translate
[c
] != c
)
2623 char buf
[KEY_DESCRIPTION_SIZE
];
2626 if (alternate_heading
)
2628 insert_string (alternate_heading
);
2629 alternate_heading
= 0;
2632 bufend
= push_key_description (translate
[c
], buf
, 1);
2633 insert (buf
, bufend
- buf
);
2634 Findent_to (make_number (16), make_number (1));
2635 bufend
= push_key_description (c
, buf
, 1);
2636 insert (buf
, bufend
- buf
);
2644 if (!NILP (Vkey_translation_map
))
2645 describe_map_tree (Vkey_translation_map
, 0, Qnil
, prefix
,
2646 "Key translations", nomenu
, 1, 0);
2649 /* Print the (major mode) local map. */
2651 if (!NILP (current_kboard
->Voverriding_terminal_local_map
))
2652 start1
= current_kboard
->Voverriding_terminal_local_map
;
2653 else if (!NILP (Voverriding_local_map
))
2654 start1
= Voverriding_local_map
;
2658 describe_map_tree (start1
, 1, shadow
, prefix
,
2659 "\f\nOverriding Bindings", nomenu
, 0, 0);
2660 shadow
= Fcons (start1
, shadow
);
2664 /* Print the minor mode and major mode keymaps. */
2666 Lisp_Object
*modes
, *maps
;
2668 /* Temporarily switch to `buffer', so that we can get that buffer's
2669 minor modes correctly. */
2670 Fset_buffer (buffer
);
2672 nmaps
= current_minor_maps (&modes
, &maps
);
2673 Fset_buffer (outbuf
);
2675 start1
= get_local_map (BUF_PT (XBUFFER (buffer
)),
2676 XBUFFER (buffer
), Qkeymap
);
2679 describe_map_tree (start1
, 1, shadow
, prefix
,
2680 "\f\n`keymap' Property Bindings", nomenu
, 0, 0);
2681 shadow
= Fcons (start1
, shadow
);
2684 /* Print the minor mode maps. */
2685 for (i
= 0; i
< nmaps
; i
++)
2687 /* The title for a minor mode keymap
2688 is constructed at run time.
2689 We let describe_map_tree do the actual insertion
2690 because it takes care of other features when doing so. */
2693 if (!SYMBOLP (modes
[i
]))
2696 p
= title
= (char *) alloca (42 + XSYMBOL (modes
[i
])->name
->size
);
2700 bcopy (XSYMBOL (modes
[i
])->name
->data
, p
,
2701 XSYMBOL (modes
[i
])->name
->size
);
2702 p
+= XSYMBOL (modes
[i
])->name
->size
;
2704 bcopy (" Minor Mode Bindings", p
, sizeof (" Minor Mode Bindings") - 1);
2705 p
+= sizeof (" Minor Mode Bindings") - 1;
2708 describe_map_tree (maps
[i
], 1, shadow
, prefix
, title
, nomenu
, 0, 0);
2709 shadow
= Fcons (maps
[i
], shadow
);
2712 start1
= get_local_map (BUF_PT (XBUFFER (buffer
)),
2713 XBUFFER (buffer
), Qlocal_map
);
2716 if (EQ (start1
, XBUFFER (buffer
)->keymap
))
2717 describe_map_tree (start1
, 1, shadow
, prefix
,
2718 "\f\nMajor Mode Bindings", nomenu
, 0, 0);
2720 describe_map_tree (start1
, 1, shadow
, prefix
,
2721 "\f\n`local-map' Property Bindings",
2724 shadow
= Fcons (start1
, shadow
);
2728 describe_map_tree (current_global_map
, 1, shadow
, prefix
,
2729 "\f\nGlobal Bindings", nomenu
, 0, 1);
2731 /* Print the function-key-map translations under this prefix. */
2732 if (!NILP (Vfunction_key_map
))
2733 describe_map_tree (Vfunction_key_map
, 0, Qnil
, prefix
,
2734 "\f\nFunction key map translations", nomenu
, 1, 0);
2740 /* Insert a description of the key bindings in STARTMAP,
2741 followed by those of all maps reachable through STARTMAP.
2742 If PARTIAL is nonzero, omit certain "uninteresting" commands
2743 (such as `undefined').
2744 If SHADOW is non-nil, it is a list of maps;
2745 don't mention keys which would be shadowed by any of them.
2746 PREFIX, if non-nil, says mention only keys that start with PREFIX.
2747 TITLE, if not 0, is a string to insert at the beginning.
2748 TITLE should not end with a colon or a newline; we supply that.
2749 If NOMENU is not 0, then omit menu-bar commands.
2751 If TRANSL is nonzero, the definitions are actually key translations
2752 so print strings and vectors differently.
2754 If ALWAYS_TITLE is nonzero, print the title even if there are no maps
2758 describe_map_tree (startmap
, partial
, shadow
, prefix
, title
, nomenu
, transl
,
2760 Lisp_Object startmap
, shadow
, prefix
;
2767 Lisp_Object maps
, orig_maps
, seen
, sub_shadows
;
2768 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2775 orig_maps
= maps
= Faccessible_keymaps (startmap
, prefix
);
2778 GCPRO3 (maps
, seen
, sub_shadows
);
2784 /* Delete from MAPS each element that is for the menu bar. */
2785 for (list
= maps
; !NILP (list
); list
= XCDR (list
))
2787 Lisp_Object elt
, prefix
, tem
;
2790 prefix
= Fcar (elt
);
2791 if (XVECTOR (prefix
)->size
>= 1)
2793 tem
= Faref (prefix
, make_number (0));
2794 if (EQ (tem
, Qmenu_bar
))
2795 maps
= Fdelq (elt
, maps
);
2800 if (!NILP (maps
) || always_title
)
2804 insert_string (title
);
2807 insert_string (" Starting With ");
2808 insert1 (Fkey_description (prefix
));
2810 insert_string (":\n");
2812 insert_string (key_heading
);
2816 for (; !NILP (maps
); maps
= Fcdr (maps
))
2818 register Lisp_Object elt
, prefix
, tail
;
2821 prefix
= Fcar (elt
);
2825 for (tail
= shadow
; CONSP (tail
); tail
= XCDR (tail
))
2829 shmap
= XCAR (tail
);
2831 /* If the sequence by which we reach this keymap is zero-length,
2832 then the shadow map for this keymap is just SHADOW. */
2833 if ((STRINGP (prefix
) && XSTRING (prefix
)->size
== 0)
2834 || (VECTORP (prefix
) && XVECTOR (prefix
)->size
== 0))
2836 /* If the sequence by which we reach this keymap actually has
2837 some elements, then the sequence's definition in SHADOW is
2838 what we should use. */
2841 shmap
= Flookup_key (shmap
, Fcar (elt
), Qt
);
2842 if (INTEGERP (shmap
))
2846 /* If shmap is not nil and not a keymap,
2847 it completely shadows this map, so don't
2848 describe this map at all. */
2849 if (!NILP (shmap
) && !KEYMAPP (shmap
))
2853 sub_shadows
= Fcons (shmap
, sub_shadows
);
2856 /* Maps we have already listed in this loop shadow this map. */
2857 for (tail
= orig_maps
; !EQ (tail
, maps
); tail
= XCDR (tail
))
2860 tem
= Fequal (Fcar (XCAR (tail
)), prefix
);
2862 sub_shadows
= Fcons (XCDR (XCAR (tail
)), sub_shadows
);
2865 describe_map (Fcdr (elt
), prefix
,
2866 transl
? describe_translation
: describe_command
,
2867 partial
, sub_shadows
, &seen
, nomenu
);
2873 insert_string ("\n");
2878 static int previous_description_column
;
2881 describe_command (definition
, args
)
2882 Lisp_Object definition
, args
;
2884 register Lisp_Object tem1
;
2885 int column
= current_column ();
2886 int description_column
;
2888 /* If column 16 is no good, go to col 32;
2889 but don't push beyond that--go to next line instead. */
2893 description_column
= 32;
2895 else if (column
> 14 || (column
> 10 && previous_description_column
== 32))
2896 description_column
= 32;
2898 description_column
= 16;
2900 Findent_to (make_number (description_column
), make_number (1));
2901 previous_description_column
= description_column
;
2903 if (SYMBOLP (definition
))
2905 XSETSTRING (tem1
, XSYMBOL (definition
)->name
);
2907 insert_string ("\n");
2909 else if (STRINGP (definition
) || VECTORP (definition
))
2910 insert_string ("Keyboard Macro\n");
2911 else if (KEYMAPP (definition
))
2912 insert_string ("Prefix Command\n");
2914 insert_string ("??\n");
2918 describe_translation (definition
, args
)
2919 Lisp_Object definition
, args
;
2921 register Lisp_Object tem1
;
2923 Findent_to (make_number (16), make_number (1));
2925 if (SYMBOLP (definition
))
2927 XSETSTRING (tem1
, XSYMBOL (definition
)->name
);
2929 insert_string ("\n");
2931 else if (STRINGP (definition
) || VECTORP (definition
))
2933 insert1 (Fkey_description (definition
));
2934 insert_string ("\n");
2936 else if (KEYMAPP (definition
))
2937 insert_string ("Prefix Command\n");
2939 insert_string ("??\n");
2942 /* Describe the contents of map MAP, assuming that this map itself is
2943 reached by the sequence of prefix keys KEYS (a string or vector).
2944 PARTIAL, SHADOW, NOMENU are as in `describe_map_tree' above. */
2947 describe_map (map
, keys
, elt_describer
, partial
, shadow
, seen
, nomenu
)
2948 register Lisp_Object map
;
2950 void (*elt_describer
) P_ ((Lisp_Object
, Lisp_Object
));
2956 Lisp_Object elt_prefix
;
2957 Lisp_Object tail
, definition
, event
;
2959 Lisp_Object suppress
;
2962 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2966 if (!NILP (keys
) && XFASTINT (Flength (keys
)) > 0)
2968 /* Call Fkey_description first, to avoid GC bug for the other string. */
2969 tem
= Fkey_description (keys
);
2970 elt_prefix
= concat2 (tem
, build_string (" "));
2976 suppress
= intern ("suppress-keymap");
2978 /* This vector gets used to present single keys to Flookup_key. Since
2979 that is done once per keymap element, we don't want to cons up a
2980 fresh vector every time. */
2981 kludge
= Fmake_vector (make_number (1), Qnil
);
2984 GCPRO3 (elt_prefix
, definition
, kludge
);
2986 for (tail
= map
; CONSP (tail
); tail
= XCDR (tail
))
2990 if (VECTORP (XCAR (tail
))
2991 || CHAR_TABLE_P (XCAR (tail
)))
2992 describe_vector (XCAR (tail
),
2993 elt_prefix
, Qnil
, elt_describer
, partial
, shadow
, map
,
2995 else if (CONSP (XCAR (tail
)))
2997 event
= XCAR (XCAR (tail
));
2999 /* Ignore bindings whose "keys" are not really valid events.
3000 (We get these in the frames and buffers menu.) */
3001 if (!(SYMBOLP (event
) || INTEGERP (event
)))
3004 if (nomenu
&& EQ (event
, Qmenu_bar
))
3007 definition
= get_keyelt (XCDR (XCAR (tail
)), 0);
3009 /* Don't show undefined commands or suppressed commands. */
3010 if (NILP (definition
)) continue;
3011 if (SYMBOLP (definition
) && partial
)
3013 tem
= Fget (definition
, suppress
);
3018 /* Don't show a command that isn't really visible
3019 because a local definition of the same key shadows it. */
3021 ASET (kludge
, 0, event
);
3024 tem
= shadow_lookup (shadow
, kludge
, Qt
);
3025 if (!NILP (tem
)) continue;
3028 tem
= Flookup_key (map
, kludge
, Qt
);
3029 if (!EQ (tem
, definition
)) continue;
3033 previous_description_column
= 0;
3038 if (!NILP (elt_prefix
))
3039 insert1 (elt_prefix
);
3041 /* THIS gets the string to describe the character EVENT. */
3042 insert1 (Fsingle_key_description (event
, Qnil
));
3044 /* Print a description of the definition of this character.
3045 elt_describer will take care of spacing out far enough
3046 for alignment purposes. */
3047 (*elt_describer
) (definition
, Qnil
);
3049 else if (EQ (XCAR (tail
), Qkeymap
))
3051 /* The same keymap might be in the structure twice, if we're
3052 using an inherited keymap. So skip anything we've already
3054 tem
= Fassq (tail
, *seen
);
3055 if (CONSP (tem
) && !NILP (Fequal (XCAR (tem
), keys
)))
3057 *seen
= Fcons (Fcons (tail
, keys
), *seen
);
3065 describe_vector_princ (elt
, fun
)
3066 Lisp_Object elt
, fun
;
3068 Findent_to (make_number (16), make_number (1));
3073 DEFUN ("describe-vector", Fdescribe_vector
, Sdescribe_vector
, 1, 2, 0,
3074 doc
: /* Insert a description of contents of VECTOR.
3075 This is text showing the elements of vector matched against indices. */)
3077 Lisp_Object vector
, describer
;
3079 int count
= specpdl_ptr
- specpdl
;
3080 if (NILP (describer
))
3081 describer
= intern ("princ");
3082 specbind (Qstandard_output
, Fcurrent_buffer ());
3083 CHECK_VECTOR_OR_CHAR_TABLE (vector
);
3084 describe_vector (vector
, Qnil
, describer
, describe_vector_princ
, 0,
3085 Qnil
, Qnil
, (int *)0, 0);
3087 return unbind_to (count
, Qnil
);
3090 /* Insert in the current buffer a description of the contents of VECTOR.
3091 We call ELT_DESCRIBER to insert the description of one value found
3094 ELT_PREFIX describes what "comes before" the keys or indices defined
3095 by this vector. This is a human-readable string whose size
3096 is not necessarily related to the situation.
3098 If the vector is in a keymap, ELT_PREFIX is a prefix key which
3099 leads to this keymap.
3101 If the vector is a chartable, ELT_PREFIX is the vector
3102 of bytes that lead to the character set or portion of a character
3103 set described by this chartable.
3105 If PARTIAL is nonzero, it means do not mention suppressed commands
3106 (that assumes the vector is in a keymap).
3108 SHADOW is a list of keymaps that shadow this map.
3109 If it is non-nil, then we look up the key in those maps
3110 and we don't mention it now if it is defined by any of them.
3112 ENTIRE_MAP is the keymap in which this vector appears.
3113 If the definition in effect in the whole map does not match
3114 the one in this vector, we ignore this one.
3116 ARGS is simply passed as the second argument to ELT_DESCRIBER.
3118 INDICES and CHAR_TABLE_DEPTH are ignored. They will be removed in
3122 describe_vector (vector
, elt_prefix
, args
, elt_describer
,
3123 partial
, shadow
, entire_map
,
3124 indices
, char_table_depth
)
3125 register Lisp_Object vector
;
3126 Lisp_Object elt_prefix
, args
;
3127 void (*elt_describer
) P_ ((Lisp_Object
, Lisp_Object
));
3130 Lisp_Object entire_map
;
3132 int char_table_depth
;
3134 Lisp_Object definition
;
3137 Lisp_Object suppress
;
3139 struct gcpro gcpro1
, gcpro2
, gcpro3
;
3140 /* Range of elements to be handled. */
3142 Lisp_Object character
;
3150 /* This vector gets used to present single keys to Flookup_key. Since
3151 that is done once per vector element, we don't want to cons up a
3152 fresh vector every time. */
3153 kludge
= Fmake_vector (make_number (1), Qnil
);
3154 GCPRO3 (elt_prefix
, definition
, kludge
);
3157 suppress
= intern ("suppress-keymap");
3160 to
= CHAR_TABLE_P (vector
) ? MAX_CHAR
+ 1 : XVECTOR (vector
)->size
;
3162 for (i
= from
; i
< to
; i
++)
3164 int range_beg
, range_end
;
3171 if (CHAR_TABLE_P (vector
))
3172 val
= char_table_ref_and_range (vector
, i
, &range_beg
, &i
);
3174 val
= AREF (vector
, i
);
3175 definition
= get_keyelt (val
, 0);
3177 if (NILP (definition
)) continue;
3179 /* Don't mention suppressed commands. */
3180 if (SYMBOLP (definition
) && partial
)
3184 tem
= Fget (definition
, suppress
);
3186 if (!NILP (tem
)) continue;
3189 character
= make_number (starting_i
);
3191 /* If this binding is shadowed by some other map, ignore it. */
3196 ASET (kludge
, 0, character
);
3197 tem
= shadow_lookup (shadow
, kludge
, Qt
);
3199 if (!NILP (tem
)) continue;
3202 /* Ignore this definition if it is shadowed by an earlier
3203 one in the same keymap. */
3204 if (!NILP (entire_map
))
3208 ASET (kludge
, 0, character
);
3209 tem
= Flookup_key (entire_map
, kludge
, Qt
);
3211 if (!EQ (tem
, definition
))
3221 /* Output the prefix that applies to every entry in this map. */
3222 if (!NILP (elt_prefix
))
3223 insert1 (elt_prefix
);
3225 insert1 (Fsingle_key_description (character
, Qnil
));
3227 /* Find all consecutive characters or rows that have the same
3228 definition. But, for elements of a top level char table, if
3229 they are for charsets, we had better describe one by one even
3230 if they have the same definition. */
3231 if (CHAR_TABLE_P (vector
))
3233 && (val
= char_table_ref_and_range (vector
, i
+ 1,
3234 &range_beg
, &range_end
),
3235 tem2
= get_keyelt (val
, 0),
3237 && !NILP (Fequal (tem2
, definition
)))
3241 && (tem2
= get_keyelt (AREF (vector
, i
+ 1), 0),
3243 && !NILP (Fequal (tem2
, definition
)))
3246 /* If we have a range of more than one character,
3247 print where the range reaches to. */
3249 if (i
!= starting_i
)
3253 if (!NILP (elt_prefix
))
3254 insert1 (elt_prefix
);
3255 insert1 (Fsingle_key_description (make_number (i
), Qnil
));
3258 /* Print a description of the definition of this character.
3259 elt_describer will take care of spacing out far enough
3260 for alignment purposes. */
3261 (*elt_describer
) (definition
, args
);
3268 /* Apropos - finding all symbols whose names match a regexp. */
3269 static Lisp_Object apropos_predicate
;
3270 static Lisp_Object apropos_accumulate
;
3273 apropos_accum (symbol
, string
)
3274 Lisp_Object symbol
, string
;
3276 register Lisp_Object tem
;
3278 tem
= Fstring_match (string
, Fsymbol_name (symbol
), Qnil
);
3279 if (!NILP (tem
) && !NILP (apropos_predicate
))
3280 tem
= call1 (apropos_predicate
, symbol
);
3282 apropos_accumulate
= Fcons (symbol
, apropos_accumulate
);
3285 DEFUN ("apropos-internal", Fapropos_internal
, Sapropos_internal
, 1, 2, 0,
3286 doc
: /* Find all symbols whose names contain match for REGEXP.
3287 If optional 2nd arg PREDICATE is non-nil, (funcall PREDICATE SYMBOL) is done
3288 for each symbol and a symbol is mentioned only if that returns non-nil.
3289 Return list of symbols found. */)
3291 Lisp_Object regexp
, predicate
;
3293 struct gcpro gcpro1
;
3295 CHECK_STRING (regexp
);
3296 apropos_predicate
= predicate
; /* predicate protected by the call */
3297 apropos_accumulate
= Qnil
; /* staticpro'd */
3298 map_obarray (Vobarray
, apropos_accum
, regexp
);
3299 result
= Fsort (apropos_accumulate
, Qstring_lessp
);
3300 apropos_accumulate
= Qnil
; /* Allow the result to be GCed. */
3301 apropos_predicate
= Qnil
;
3308 Qkeymap
= intern ("keymap");
3309 staticpro (&Qkeymap
);
3311 /* Now we are ready to set up this property, so we can
3312 create char tables. */
3313 Fput (Qkeymap
, Qchar_table_extra_slots
, make_number (0));
3315 /* Initialize the keymaps standardly used.
3316 Each one is the value of a Lisp variable, and is also
3317 pointed to by a C variable */
3319 global_map
= Fmake_keymap (Qnil
);
3320 Fset (intern ("global-map"), global_map
);
3322 current_global_map
= global_map
;
3323 staticpro (&global_map
);
3324 staticpro (¤t_global_map
);
3326 meta_map
= Fmake_keymap (Qnil
);
3327 Fset (intern ("esc-map"), meta_map
);
3328 Ffset (intern ("ESC-prefix"), meta_map
);
3330 control_x_map
= Fmake_keymap (Qnil
);
3331 Fset (intern ("ctl-x-map"), control_x_map
);
3332 Ffset (intern ("Control-X-prefix"), control_x_map
);
3335 = Fcons (Fcons (build_string ("DEL"), build_string ("\\d")),
3336 Fcons (Fcons (build_string ("TAB"), build_string ("\\t")),
3337 Fcons (Fcons (build_string ("RET"), build_string ("\\r")),
3338 Fcons (Fcons (build_string ("ESC"), build_string ("\\e")),
3339 Fcons (Fcons (build_string ("SPC"), build_string (" ")),
3341 staticpro (&exclude_keys
);
3343 DEFVAR_LISP ("define-key-rebound-commands", &Vdefine_key_rebound_commands
,
3344 doc
: /* List of commands given new key bindings recently.
3345 This is used for internal purposes during Emacs startup;
3346 don't alter it yourself. */);
3347 Vdefine_key_rebound_commands
= Qt
;
3349 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map
,
3350 doc
: /* Default keymap to use when reading from the minibuffer. */);
3351 Vminibuffer_local_map
= Fmake_sparse_keymap (Qnil
);
3353 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map
,
3354 doc
: /* Local keymap for the minibuffer when spaces are not allowed. */);
3355 Vminibuffer_local_ns_map
= Fmake_sparse_keymap (Qnil
);
3356 Fset_keymap_parent (Vminibuffer_local_ns_map
, Vminibuffer_local_map
);
3358 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map
,
3359 doc
: /* Local keymap for minibuffer input with completion. */);
3360 Vminibuffer_local_completion_map
= Fmake_sparse_keymap (Qnil
);
3361 Fset_keymap_parent (Vminibuffer_local_completion_map
, Vminibuffer_local_map
);
3363 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map
,
3364 doc
: /* Local keymap for minibuffer input with completion, for exact match. */);
3365 Vminibuffer_local_must_match_map
= Fmake_sparse_keymap (Qnil
);
3366 Fset_keymap_parent (Vminibuffer_local_must_match_map
,
3367 Vminibuffer_local_completion_map
);
3369 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist
,
3370 doc
: /* Alist of keymaps to use for minor modes.
3371 Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read
3372 key sequences and look up bindings iff VARIABLE's value is non-nil.
3373 If two active keymaps bind the same key, the keymap appearing earlier
3374 in the list takes precedence. */);
3375 Vminor_mode_map_alist
= Qnil
;
3377 DEFVAR_LISP ("minor-mode-overriding-map-alist", &Vminor_mode_overriding_map_alist
,
3378 doc
: /* Alist of keymaps to use for minor modes, in current major mode.
3379 This variable is a alist just like `minor-mode-map-alist', and it is
3380 used the same way (and before `minor-mode-map-alist'); however,
3381 it is provided for major modes to bind locally. */);
3382 Vminor_mode_overriding_map_alist
= Qnil
;
3384 DEFVAR_LISP ("function-key-map", &Vfunction_key_map
,
3385 doc
: /* Keymap mapping ASCII function key sequences onto their preferred forms.
3386 This allows Emacs to recognize function keys sent from ASCII
3387 terminals at any point in a key sequence.
3389 The `read-key-sequence' function replaces any subsequence bound by
3390 `function-key-map' with its binding. More precisely, when the active
3391 keymaps have no binding for the current key sequence but
3392 `function-key-map' binds a suffix of the sequence to a vector or string,
3393 `read-key-sequence' replaces the matching suffix with its binding, and
3394 continues with the new sequence.
3396 The events that come from bindings in `function-key-map' are not
3397 themselves looked up in `function-key-map'.
3399 For example, suppose `function-key-map' binds `ESC O P' to [f1].
3400 Typing `ESC O P' to `read-key-sequence' would return [f1]. Typing
3401 `C-x ESC O P' would return [?\\C-x f1]. If [f1] were a prefix
3402 key, typing `ESC O P x' would return [f1 x]. */);
3403 Vfunction_key_map
= Fmake_sparse_keymap (Qnil
);
3405 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map
,
3406 doc
: /* Keymap of key translations that can override keymaps.
3407 This keymap works like `function-key-map', but comes after that,
3408 and applies even for keys that have ordinary bindings. */);
3409 Vkey_translation_map
= Qnil
;
3411 Qsingle_key_description
= intern ("single-key-description");
3412 staticpro (&Qsingle_key_description
);
3414 Qkey_description
= intern ("key-description");
3415 staticpro (&Qkey_description
);
3417 Qkeymapp
= intern ("keymapp");
3418 staticpro (&Qkeymapp
);
3420 Qnon_ascii
= intern ("non-ascii");
3421 staticpro (&Qnon_ascii
);
3423 Qmenu_item
= intern ("menu-item");
3424 staticpro (&Qmenu_item
);
3426 Qremap
= intern ("remap");
3427 staticpro (&Qremap
);
3429 remap_command_vector
= Fmake_vector (make_number (2), Qremap
);
3430 staticpro (&remap_command_vector
);
3432 where_is_cache_keymaps
= Qt
;
3433 where_is_cache
= Qnil
;
3434 staticpro (&where_is_cache
);
3435 staticpro (&where_is_cache_keymaps
);
3436 apropos_accumulate
= Qnil
;
3437 staticpro (&apropos_accumulate
);
3439 defsubr (&Skeymapp
);
3440 defsubr (&Skeymap_parent
);
3441 defsubr (&Skeymap_prompt
);
3442 defsubr (&Sset_keymap_parent
);
3443 defsubr (&Smake_keymap
);
3444 defsubr (&Smake_sparse_keymap
);
3445 defsubr (&Scopy_keymap
);
3446 defsubr (&Sremap_command
);
3447 defsubr (&Skey_binding
);
3448 defsubr (&Slocal_key_binding
);
3449 defsubr (&Sglobal_key_binding
);
3450 defsubr (&Sminor_mode_key_binding
);
3451 defsubr (&Sdefine_key
);
3452 defsubr (&Slookup_key
);
3453 defsubr (&Sdefine_prefix_command
);
3454 defsubr (&Suse_global_map
);
3455 defsubr (&Suse_local_map
);
3456 defsubr (&Scurrent_local_map
);
3457 defsubr (&Scurrent_global_map
);
3458 defsubr (&Scurrent_minor_mode_maps
);
3459 defsubr (&Scurrent_active_maps
);
3460 defsubr (&Saccessible_keymaps
);
3461 defsubr (&Skey_description
);
3462 defsubr (&Sdescribe_vector
);
3463 defsubr (&Ssingle_key_description
);
3464 defsubr (&Stext_char_description
);
3465 defsubr (&Swhere_is_internal
);
3466 defsubr (&Sdescribe_buffer_bindings
);
3467 defsubr (&Sapropos_internal
);
3473 initial_define_key (global_map
, 033, "ESC-prefix");
3474 initial_define_key (global_map
, Ctl('X'), "Control-X-prefix");