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)
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. */
30 #include "termhooks.h"
31 #include "blockinput.h"
33 #include "intervals.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
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
63 /* keymap used for minibuffers when doing completion */
64 /* was MinibufLocalCompletionMap */
65 Lisp_Object Vminibuffer_local_completion_map
;
67 /* keymap used for minibuffers when doing completion and require a match */
68 /* was MinibufLocalMustMatchMap */
69 Lisp_Object Vminibuffer_local_must_match_map
;
71 /* Alist of minor mode variables and keymaps. */
72 Lisp_Object Vminor_mode_map_alist
;
74 /* Alist of major-mode-specific overrides for
75 minor mode variables and keymaps. */
76 Lisp_Object Vminor_mode_overriding_map_alist
;
78 /* Keymap mapping ASCII function key sequences onto their preferred forms.
79 Initialized by the terminal-specific lisp files. See DEFVAR for more
81 Lisp_Object Vfunction_key_map
;
83 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
84 Lisp_Object Vkey_translation_map
;
86 /* A list of all commands given new bindings since a certain time
87 when nil was stored here.
88 This is used to speed up recomputation of menu key equivalents
89 when Emacs starts up. t means don't record anything here. */
90 Lisp_Object Vdefine_key_rebound_commands
;
92 Lisp_Object Qkeymapp
, Qkeymap
, Qnon_ascii
, Qmenu_item
;
94 /* Alist of elements like (DEL . "\d"). */
95 static Lisp_Object exclude_keys
;
97 /* A char with the CHAR_META bit set in a vector or the 0200 bit set
98 in a string key sequence is equivalent to prefixing with this
100 extern Lisp_Object meta_prefix_char
;
102 extern Lisp_Object Voverriding_local_map
;
104 /* Hash table used to cache a reverse-map to speed up calls to where-is. */
105 static Lisp_Object where_is_cache
;
106 /* Which keymaps are reverse-stored in the cache. */
107 static Lisp_Object where_is_cache_keymaps
;
109 static Lisp_Object store_in_keymap
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
));
110 static void fix_submap_inheritance
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
));
112 static Lisp_Object define_as_prefix
P_ ((Lisp_Object
, Lisp_Object
));
113 static void describe_command
P_ ((Lisp_Object
, Lisp_Object
));
114 static void describe_translation
P_ ((Lisp_Object
, Lisp_Object
));
115 static void describe_map
P_ ((Lisp_Object
, Lisp_Object
,
116 void (*) P_ ((Lisp_Object
, Lisp_Object
)),
117 int, Lisp_Object
, Lisp_Object
*, int));
118 static void silly_event_symbol_error
P_ ((Lisp_Object
));
120 /* Keymap object support - constructors and predicates. */
122 DEFUN ("make-keymap", Fmake_keymap
, Smake_keymap
, 0, 1, 0,
123 doc
: /* Construct and return a new keymap, of the form (keymap CHARTABLE . ALIST).
124 CHARTABLE is a char-table that holds the bindings for the ASCII
125 characters. ALIST is an assoc-list which holds bindings for function keys,
126 mouse events, and any other things that appear in the input stream.
127 All entries in it are initially nil, meaning "command undefined".
129 The optional arg STRING supplies a menu name for the keymap
130 in case you use it as a menu with `x-popup-menu'. */)
136 tail
= Fcons (string
, Qnil
);
139 return Fcons (Qkeymap
,
140 Fcons (Fmake_char_table (Qkeymap
, Qnil
), tail
));
143 DEFUN ("make-sparse-keymap", Fmake_sparse_keymap
, Smake_sparse_keymap
, 0, 1, 0,
144 doc
: /* Construct and return a new sparse keymap.
145 Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),
146 which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),
147 which binds the function key or mouse event SYMBOL to DEFINITION.
148 Initially the alist is nil.
150 The optional arg STRING supplies a menu name for the keymap
151 in case you use it as a menu with `x-popup-menu'. */)
156 return Fcons (Qkeymap
, Fcons (string
, Qnil
));
157 return Fcons (Qkeymap
, Qnil
);
160 /* This function is used for installing the standard key bindings
161 at initialization time.
165 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); */
168 initial_define_key (keymap
, key
, defname
)
173 store_in_keymap (keymap
, make_number (key
), intern (defname
));
177 initial_define_lispy_key (keymap
, keyname
, defname
)
182 store_in_keymap (keymap
, intern (keyname
), intern (defname
));
185 DEFUN ("keymapp", Fkeymapp
, Skeymapp
, 1, 1, 0,
186 doc
: /* Return t if OBJECT is a keymap.
188 A keymap is a list (keymap . ALIST),
189 or a symbol whose function definition is itself a keymap.
190 ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);
191 a vector of densely packed bindings for small character codes
192 is also allowed as an element. */)
196 return (KEYMAPP (object
) ? Qt
: Qnil
);
199 DEFUN ("keymap-prompt", Fkeymap_prompt
, Skeymap_prompt
, 1, 1, 0,
200 doc
: /* Return the prompt-string of a keymap MAP.
201 If non-nil, the prompt is shown in the echo-area
202 when reading a key-sequence to be looked-up in this keymap. */)
208 register Lisp_Object tem
;
217 /* Check that OBJECT is a keymap (after dereferencing through any
218 symbols). If it is, return it.
220 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
221 is an autoload form, do the autoload and try again.
222 If AUTOLOAD is nonzero, callers must assume GC is possible.
224 If the map needs to be autoloaded, but AUTOLOAD is zero (and ERROR
225 is zero as well), return Qt.
227 ERROR controls how we respond if OBJECT isn't a keymap.
228 If ERROR is non-zero, signal an error; otherwise, just return Qnil.
230 Note that most of the time, we don't want to pursue autoloads.
231 Functions like Faccessible_keymaps which scan entire keymap trees
232 shouldn't load every autoloaded keymap. I'm not sure about this,
233 but it seems to me that only read_key_sequence, Flookup_key, and
234 Fdefine_key should cause keymaps to be autoloaded.
236 This function can GC when AUTOLOAD is non-zero, because it calls
237 do_autoload which can GC. */
240 get_keymap (object
, error
, autoload
)
249 if (CONSP (object
) && EQ (XCAR (object
), Qkeymap
))
252 tem
= indirect_function (object
);
255 if (EQ (XCAR (tem
), Qkeymap
))
258 /* Should we do an autoload? Autoload forms for keymaps have
259 Qkeymap as their fifth element. */
260 if ((autoload
|| !error
) && EQ (XCAR (tem
), Qautoload
))
264 tail
= Fnth (make_number (4), tem
);
265 if (EQ (tail
, Qkeymap
))
269 struct gcpro gcpro1
, gcpro2
;
271 GCPRO2 (tem
, object
);
272 do_autoload (tem
, object
);
285 wrong_type_argument (Qkeymapp
, object
);
289 /* Return the parent map of the keymap MAP, or nil if it has none.
290 We assume that MAP is a valid keymap. */
292 DEFUN ("keymap-parent", Fkeymap_parent
, Skeymap_parent
, 1, 1, 0,
293 doc
: /* Return the parent keymap of KEYMAP. */)
299 keymap
= get_keymap (keymap
, 1, 1);
301 /* Skip past the initial element `keymap'. */
302 list
= XCDR (keymap
);
303 for (; CONSP (list
); list
= XCDR (list
))
305 /* See if there is another `keymap'. */
310 return get_keymap (list
, 0, 1);
314 /* Check whether MAP is one of MAPS parents. */
316 keymap_memberp (map
, maps
)
317 Lisp_Object map
, maps
;
319 if (NILP (map
)) return 0;
320 while (KEYMAPP (maps
) && !EQ (map
, maps
))
321 maps
= Fkeymap_parent (maps
);
322 return (EQ (map
, maps
));
325 /* Set the parent keymap of MAP to PARENT. */
327 DEFUN ("set-keymap-parent", Fset_keymap_parent
, Sset_keymap_parent
, 2, 2, 0,
328 doc
: /* Modify KEYMAP to set its parent map to PARENT.
329 PARENT should be nil or another keymap. */)
331 Lisp_Object keymap
, parent
;
333 Lisp_Object list
, prev
;
337 /* Force a keymap flush for the next call to where-is.
338 Since this can be called from within where-is, we don't set where_is_cache
339 directly but only where_is_cache_keymaps, since where_is_cache shouldn't
340 be changed during where-is, while where_is_cache_keymaps is only used at
341 the very beginning of where-is and can thus be changed here without any
343 This is a very minor correctness (rather than safety) issue. */
344 where_is_cache_keymaps
= Qt
;
346 keymap
= get_keymap (keymap
, 1, 1);
351 parent
= get_keymap (parent
, 1, 1);
353 /* Check for cycles. */
354 if (keymap_memberp (keymap
, parent
))
355 error ("Cyclic keymap inheritance");
358 /* Skip past the initial element `keymap'. */
363 /* If there is a parent keymap here, replace it.
364 If we came to the end, add the parent in PREV. */
365 if (!CONSP (list
) || KEYMAPP (list
))
367 /* If we already have the right parent, return now
368 so that we avoid the loops below. */
369 if (EQ (XCDR (prev
), parent
))
370 RETURN_UNGCPRO (parent
);
372 XSETCDR (prev
, parent
);
378 /* Scan through for submaps, and set their parents too. */
380 for (list
= XCDR (keymap
); CONSP (list
); list
= XCDR (list
))
382 /* Stop the scan when we come to the parent. */
383 if (EQ (XCAR (list
), Qkeymap
))
386 /* If this element holds a prefix map, deal with it. */
387 if (CONSP (XCAR (list
))
388 && CONSP (XCDR (XCAR (list
))))
389 fix_submap_inheritance (keymap
, XCAR (XCAR (list
)),
392 if (VECTORP (XCAR (list
)))
393 for (i
= 0; i
< XVECTOR (XCAR (list
))->size
; i
++)
394 if (CONSP (XVECTOR (XCAR (list
))->contents
[i
]))
395 fix_submap_inheritance (keymap
, make_number (i
),
396 XVECTOR (XCAR (list
))->contents
[i
]);
398 if (CHAR_TABLE_P (XCAR (list
)))
400 Lisp_Object indices
[3];
402 map_char_table (fix_submap_inheritance
, Qnil
, XCAR (list
),
407 RETURN_UNGCPRO (parent
);
410 /* EVENT is defined in MAP as a prefix, and SUBMAP is its definition.
411 if EVENT is also a prefix in MAP's parent,
412 make sure that SUBMAP inherits that definition as its own parent. */
415 fix_submap_inheritance (map
, event
, submap
)
416 Lisp_Object map
, event
, submap
;
418 Lisp_Object map_parent
, parent_entry
;
420 /* SUBMAP is a cons that we found as a key binding.
421 Discard the other things found in a menu key binding. */
423 submap
= get_keymap (get_keyelt (submap
, 0), 0, 0);
425 /* If it isn't a keymap now, there's no work to do. */
429 map_parent
= Fkeymap_parent (map
);
430 if (!NILP (map_parent
))
432 get_keymap (access_keymap (map_parent
, event
, 0, 0, 0), 0, 0);
436 /* If MAP's parent has something other than a keymap,
437 our own submap shadows it completely. */
438 if (!CONSP (parent_entry
))
441 if (! EQ (parent_entry
, submap
))
443 Lisp_Object submap_parent
;
444 submap_parent
= submap
;
449 tem
= Fkeymap_parent (submap_parent
);
453 if (keymap_memberp (tem
, parent_entry
))
454 /* Fset_keymap_parent could create a cycle. */
461 Fset_keymap_parent (submap_parent
, parent_entry
);
465 /* Look up IDX in MAP. IDX may be any sort of event.
466 Note that this does only one level of lookup; IDX must be a single
467 event, not a sequence.
469 If T_OK is non-zero, bindings for Qt are treated as default
470 bindings; any key left unmentioned by other tables and bindings is
471 given the binding of Qt.
473 If T_OK is zero, bindings for Qt are not treated specially.
475 If NOINHERIT, don't accept a subkeymap found in an inherited keymap. */
478 access_keymap (map
, idx
, t_ok
, noinherit
, autoload
)
487 /* Qunbound in VAL means we have found no binding yet. */
490 /* If idx is a list (some sort of mouse click, perhaps?),
491 the index we want to use is the car of the list, which
492 ought to be a symbol. */
493 idx
= EVENT_HEAD (idx
);
495 /* If idx is a symbol, it might have modifiers, which need to
496 be put in the canonical order. */
498 idx
= reorder_modifiers (idx
);
499 else if (INTEGERP (idx
))
500 /* Clobber the high bits that can be present on a machine
501 with more than 24 bits of integer. */
502 XSETFASTINT (idx
, XINT (idx
) & (CHAR_META
| (CHAR_META
- 1)));
504 /* Handle the special meta -> esc mapping. */
505 if (INTEGERP (idx
) && XUINT (idx
) & meta_modifier
)
507 /* See if there is a meta-map. If there's none, there is
508 no binding for IDX, unless a default binding exists in MAP. */
509 Lisp_Object meta_map
=
510 get_keymap (access_keymap (map
, meta_prefix_char
,
511 t_ok
, noinherit
, autoload
),
513 if (CONSP (meta_map
))
516 idx
= make_number (XUINT (idx
) & ~meta_modifier
);
519 /* Set IDX to t, so that we only find a default binding. */
522 /* We know there is no binding. */
529 /* t_binding is where we put a default binding that applies,
530 to use in case we do not find a binding specifically
531 for this key sequence. */
533 Lisp_Object t_binding
;
536 /* If `t_ok' is 2, both `t' and generic-char bindings are accepted.
537 If it is 1, only generic-char bindings are accepted.
538 Otherwise, neither are. */
541 for (tail
= XCDR (map
);
543 || (tail
= get_keymap (tail
, 0, autoload
), CONSP (tail
)));
548 binding
= XCAR (tail
);
549 if (SYMBOLP (binding
))
551 /* If NOINHERIT, stop finding prefix definitions
552 after we pass a second occurrence of the `keymap' symbol. */
553 if (noinherit
&& EQ (binding
, Qkeymap
))
556 else if (CONSP (binding
))
558 Lisp_Object key
= XCAR (binding
);
561 val
= XCDR (binding
);
564 && (XINT (idx
) & CHAR_MODIFIER_MASK
) == 0
566 && (XINT (key
) & CHAR_MODIFIER_MASK
) == 0
567 && !SINGLE_BYTE_CHAR_P (XINT (idx
))
568 && !SINGLE_BYTE_CHAR_P (XINT (key
))
569 && CHAR_VALID_P (XINT (key
), 1)
570 && !CHAR_VALID_P (XINT (key
), 0)
571 && (CHAR_CHARSET (XINT (key
))
572 == CHAR_CHARSET (XINT (idx
))))
574 /* KEY is the generic character of the charset of IDX.
575 Use KEY's binding if there isn't a binding for IDX
577 t_binding
= XCDR (binding
);
580 else if (t_ok
> 1 && EQ (key
, Qt
))
582 t_binding
= XCDR (binding
);
586 else if (VECTORP (binding
))
588 if (NATNUMP (idx
) && XFASTINT (idx
) < ASIZE (binding
))
589 val
= AREF (binding
, XFASTINT (idx
));
591 else if (CHAR_TABLE_P (binding
))
593 /* Character codes with modifiers
594 are not included in a char-table.
595 All character codes without modifiers are included. */
596 if (NATNUMP (idx
) && (XFASTINT (idx
) & CHAR_MODIFIER_MASK
) == 0)
598 val
= Faref (binding
, idx
);
599 /* `nil' has a special meaning for char-tables, so
600 we use something else to record an explicitly
607 /* If we found a binding, clean it up and return it. */
608 if (!EQ (val
, Qunbound
))
611 /* A Qt binding is just like an explicit nil binding
612 (i.e. it shadows any parent binding but not bindings in
613 keymaps of lower precedence). */
615 val
= get_keyelt (val
, autoload
);
617 fix_submap_inheritance (map
, idx
, val
);
623 return get_keyelt (t_binding
, autoload
);
627 /* Given OBJECT which was found in a slot in a keymap,
628 trace indirect definitions to get the actual definition of that slot.
629 An indirect definition is a list of the form
630 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
631 and INDEX is the object to look up in KEYMAP to yield the definition.
633 Also if OBJECT has a menu string as the first element,
634 remove that. Also remove a menu help string as second element.
636 If AUTOLOAD is nonzero, load autoloadable keymaps
637 that are referred to with indirection. */
640 get_keyelt (object
, autoload
)
641 register Lisp_Object object
;
646 if (!(CONSP (object
)))
647 /* This is really the value. */
650 /* If the keymap contents looks like (keymap ...) or (lambda ...)
652 else if (EQ (XCAR (object
), Qkeymap
) || EQ (XCAR (object
), Qlambda
))
655 /* If the keymap contents looks like (menu-item name . DEFN)
656 or (menu-item name DEFN ...) then use DEFN.
657 This is a new format menu item. */
658 else if (EQ (XCAR (object
), Qmenu_item
))
660 if (CONSP (XCDR (object
)))
664 object
= XCDR (XCDR (object
));
667 object
= XCAR (object
);
669 /* If there's a `:filter FILTER', apply FILTER to the
670 menu-item's definition to get the real definition to
672 for (; CONSP (tem
) && CONSP (XCDR (tem
)); tem
= XCDR (tem
))
673 if (EQ (XCAR (tem
), QCfilter
) && autoload
)
676 filter
= XCAR (XCDR (tem
));
677 filter
= list2 (filter
, list2 (Qquote
, object
));
678 object
= menu_item_eval_property (filter
);
687 /* If the keymap contents looks like (STRING . DEFN), use DEFN.
688 Keymap alist elements like (CHAR MENUSTRING . DEFN)
689 will be used by HierarKey menus. */
690 else if (STRINGP (XCAR (object
)))
692 object
= XCDR (object
);
693 /* Also remove a menu help string, if any,
694 following the menu item name. */
695 if (CONSP (object
) && STRINGP (XCAR (object
)))
696 object
= XCDR (object
);
697 /* Also remove the sublist that caches key equivalences, if any. */
698 if (CONSP (object
) && CONSP (XCAR (object
)))
701 carcar
= XCAR (XCAR (object
));
702 if (NILP (carcar
) || VECTORP (carcar
))
703 object
= XCDR (object
);
707 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
711 map
= get_keymap (Fcar_safe (object
), 0, autoload
);
712 return (!CONSP (map
) ? object
/* Invalid keymap */
713 : access_keymap (map
, Fcdr (object
), 0, 0, autoload
));
719 store_in_keymap (keymap
, idx
, def
)
721 register Lisp_Object idx
;
722 register Lisp_Object def
;
724 /* Flush any reverse-map cache. */
725 where_is_cache
= Qnil
;
726 where_is_cache_keymaps
= Qt
;
728 /* If we are preparing to dump, and DEF is a menu element
729 with a menu item indicator, copy it to ensure it is not pure. */
730 if (CONSP (def
) && PURE_P (def
)
731 && (EQ (XCAR (def
), Qmenu_item
) || STRINGP (XCAR (def
))))
732 def
= Fcons (XCAR (def
), XCDR (def
));
734 if (!CONSP (keymap
) || !EQ (XCAR (keymap
), Qkeymap
))
735 error ("attempt to define a key in a non-keymap");
737 /* If idx is a list (some sort of mouse click, perhaps?),
738 the index we want to use is the car of the list, which
739 ought to be a symbol. */
740 idx
= EVENT_HEAD (idx
);
742 /* If idx is a symbol, it might have modifiers, which need to
743 be put in the canonical order. */
745 idx
= reorder_modifiers (idx
);
746 else if (INTEGERP (idx
))
747 /* Clobber the high bits that can be present on a machine
748 with more than 24 bits of integer. */
749 XSETFASTINT (idx
, XINT (idx
) & (CHAR_META
| (CHAR_META
- 1)));
751 /* Scan the keymap for a binding of idx. */
755 /* The cons after which we should insert new bindings. If the
756 keymap has a table element, we record its position here, so new
757 bindings will go after it; this way, the table will stay
758 towards the front of the alist and character lookups in dense
759 keymaps will remain fast. Otherwise, this just points at the
760 front of the keymap. */
761 Lisp_Object insertion_point
;
763 insertion_point
= keymap
;
764 for (tail
= XCDR (keymap
); CONSP (tail
); tail
= XCDR (tail
))
771 if (NATNUMP (idx
) && XFASTINT (idx
) < ASIZE (elt
))
773 ASET (elt
, XFASTINT (idx
), def
);
776 insertion_point
= tail
;
778 else if (CHAR_TABLE_P (elt
))
780 /* Character codes with modifiers
781 are not included in a char-table.
782 All character codes without modifiers are included. */
783 if (NATNUMP (idx
) && !(XFASTINT (idx
) & CHAR_MODIFIER_MASK
))
786 /* `nil' has a special meaning for char-tables, so
787 we use something else to record an explicitly
789 NILP (def
) ? Qt
: def
);
792 insertion_point
= tail
;
794 else if (CONSP (elt
))
796 if (EQ (idx
, XCAR (elt
)))
802 else if (EQ (elt
, Qkeymap
))
803 /* If we find a 'keymap' symbol in the spine of KEYMAP,
804 then we must have found the start of a second keymap
805 being used as the tail of KEYMAP, and a binding for IDX
806 should be inserted before it. */
813 /* We have scanned the entire keymap, and not found a binding for
814 IDX. Let's add one. */
815 XSETCDR (insertion_point
,
816 Fcons (Fcons (idx
, def
), XCDR (insertion_point
)));
822 EXFUN (Fcopy_keymap
, 1);
825 copy_keymap_1 (chartable
, idx
, elt
)
826 Lisp_Object chartable
, idx
, elt
;
828 if (CONSP (elt
) && EQ (XCAR (elt
), Qkeymap
))
829 Faset (chartable
, idx
, Fcopy_keymap (elt
));
832 DEFUN ("copy-keymap", Fcopy_keymap
, Scopy_keymap
, 1, 1, 0,
833 doc
: /* Return a copy of the keymap KEYMAP.
834 The copy starts out with the same definitions of KEYMAP,
835 but changing either the copy or KEYMAP does not affect the other.
836 Any key definitions that are subkeymaps are recursively copied.
837 However, a key definition which is a symbol whose definition is a keymap
842 /* FIXME: This doesn't properly copy menu-items in vectors. */
843 /* FIXME: This also copies the parent keymap. */
845 register Lisp_Object copy
, tail
;
847 copy
= Fcopy_alist (get_keymap (keymap
, 1, 0));
849 for (tail
= copy
; CONSP (tail
); tail
= XCDR (tail
))
854 if (CHAR_TABLE_P (elt
))
856 Lisp_Object indices
[3];
858 elt
= Fcopy_sequence (elt
);
861 map_char_table (copy_keymap_1
, Qnil
, elt
, elt
, 0, indices
);
863 else if (VECTORP (elt
))
867 elt
= Fcopy_sequence (elt
);
870 for (i
= 0; i
< ASIZE (elt
); i
++)
871 if (CONSP (AREF (elt
, i
)) && EQ (XCAR (AREF (elt
, i
)), Qkeymap
))
872 ASET (elt
, i
, Fcopy_keymap (AREF (elt
, i
)));
874 else if (CONSP (elt
) && CONSP (XCDR (elt
)))
879 /* Is this a new format menu item. */
880 if (EQ (XCAR (tem
),Qmenu_item
))
882 /* Copy cell with menu-item marker. */
884 Fcons (XCAR (tem
), XCDR (tem
)));
889 /* Copy cell with menu-item name. */
891 Fcons (XCAR (tem
), XCDR (tem
)));
897 /* Copy cell with binding and if the binding is a keymap,
900 Fcons (XCAR (tem
), XCDR (tem
)));
903 if (CONSP (tem
) && EQ (XCAR (tem
), Qkeymap
))
904 XSETCAR (elt
, Fcopy_keymap (tem
));
906 if (CONSP (tem
) && CONSP (XCAR (tem
)))
907 /* Delete cache for key equivalences. */
908 XSETCDR (elt
, XCDR (tem
));
913 /* It may be an old fomat menu item.
914 Skip the optional menu string.
916 if (STRINGP (XCAR (tem
)))
918 /* Copy the cell, since copy-alist didn't go this deep. */
920 Fcons (XCAR (tem
), XCDR (tem
)));
923 /* Also skip the optional menu help string. */
924 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
927 Fcons (XCAR (tem
), XCDR (tem
)));
931 /* There may also be a list that caches key equivalences.
932 Just delete it for the new keymap. */
934 && CONSP (XCAR (tem
))
935 && (NILP (XCAR (XCAR (tem
)))
936 || VECTORP (XCAR (XCAR (tem
)))))
937 XSETCDR (elt
, XCDR (tem
));
940 && CONSP (XCDR (elt
))
941 && EQ (XCAR (XCDR (elt
)), Qkeymap
))
942 XSETCDR (elt
, Fcopy_keymap (XCDR (elt
)));
951 /* Simple Keymap mutators and accessors. */
953 /* GC is possible in this function if it autoloads a keymap. */
955 DEFUN ("define-key", Fdefine_key
, Sdefine_key
, 3, 3, 0,
956 doc
: /* Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.
959 KEY is a string or a vector of symbols and characters meaning a
960 sequence of keystrokes and events. Non-ASCII characters with codes
961 above 127 (such as ISO Latin-1) can be included if you use a vector.
963 DEF is anything that can be a key's definition:
964 nil (means key is undefined in this keymap),
965 a command (a Lisp function suitable for interactive calling)
966 a string (treated as a keyboard macro),
967 a keymap (to define a prefix key),
968 a symbol. When the key is looked up, the symbol will stand for its
969 function definition, which should at that time be one of the above,
970 or another symbol whose function definition is used, etc.
971 a cons (STRING . DEFN), meaning that DEFN is the definition
972 (DEFN should be a valid definition in its own right),
973 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.
975 If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at
978 KEY may also be a command name which is remapped to DEF. In this case,
979 DEF must be a symbol or nil (to remove a previous binding of KEY). */)
986 register Lisp_Object c
;
987 register Lisp_Object cmd
;
991 struct gcpro gcpro1
, gcpro2
, gcpro3
;
993 keymap
= get_keymap (keymap
, 1, 1);
997 /* A command may only be remapped to another command. */
999 /* I thought of using is_command_symbol above and below instead
1000 of SYMBOLP, since remapping only works for sych symbols.
1001 However, to make that a requirement would make it impossible
1002 to remap a command before it has been defined, e.g. if a minor
1003 mode were to remap a command of another minor mode which has
1004 not yet been loaded, it would fail. So just use the least
1005 restrictive sanity check here. */
1007 key
= wrong_type_argument (Qsymbolp
, def
);
1009 key
= Fmake_vector (make_number (1), key
);
1011 else if (!VECTORP (key
) && !STRINGP (key
))
1012 key
= wrong_type_argument (Qarrayp
, key
);
1014 length
= XFASTINT (Flength (key
));
1018 if (SYMBOLP (def
) && !EQ (Vdefine_key_rebound_commands
, Qt
))
1019 Vdefine_key_rebound_commands
= Fcons (def
, Vdefine_key_rebound_commands
);
1021 GCPRO3 (keymap
, key
, def
);
1024 meta_bit
= meta_modifier
;
1031 c
= Faref (key
, make_number (idx
));
1033 if (CONSP (c
) && lucid_event_type_list_p (c
))
1034 c
= Fevent_convert_list (c
);
1037 silly_event_symbol_error (c
);
1040 && (XINT (c
) & meta_bit
)
1043 c
= meta_prefix_char
;
1049 XSETINT (c
, XINT (c
) & ~meta_bit
);
1055 if (!INTEGERP (c
) && !SYMBOLP (c
) && !CONSP (c
))
1056 error ("Key sequence contains invalid event");
1059 RETURN_UNGCPRO (store_in_keymap (keymap
, c
, def
));
1061 cmd
= access_keymap (keymap
, c
, 0, 1, 1);
1063 /* If this key is undefined, make it a prefix. */
1065 cmd
= define_as_prefix (keymap
, c
);
1067 keymap
= get_keymap (cmd
, 0, 1);
1068 if (!CONSP (keymap
))
1069 /* We must use Fkey_description rather than just passing key to
1070 error; key might be a vector, not a string. */
1071 error ("Key sequence %s uses invalid prefix characters",
1072 XSTRING (Fkey_description (key
))->data
);
1076 /* Value is number if KEY is too long; nil if valid but has no definition. */
1077 /* GC is possible in this function if it autoloads a keymap. */
1079 DEFUN ("lookup-key", Flookup_key
, Slookup_key
, 2, 3, 0,
1080 doc
: /* In keymap KEYMAP, look up key sequence KEY. Return the definition.
1081 nil means undefined. See doc of `define-key' for kinds of definitions.
1083 A number as value means KEY is "too long";
1084 that is, characters or symbols in it except for the last one
1085 fail to be a valid sequence of prefix characters in KEYMAP.
1086 The number is how many characters at the front of KEY
1087 it takes to reach a non-prefix command.
1089 Normally, `lookup-key' ignores bindings for t, which act as default
1090 bindings, used when nothing else in the keymap applies; this makes it
1091 usable as a general function for probing keymaps. However, if the
1092 third optional argument ACCEPT-DEFAULT is non-nil, `lookup-key' will
1093 recognize the default bindings, just as `read-key-sequence' does. */)
1094 (keymap
, key
, accept_default
)
1095 register Lisp_Object keymap
;
1097 Lisp_Object accept_default
;
1100 register Lisp_Object cmd
;
1101 register Lisp_Object c
;
1103 int t_ok
= !NILP (accept_default
);
1104 struct gcpro gcpro1
;
1106 keymap
= get_keymap (keymap
, 1, 1);
1108 /* Command remapping is simple. */
1110 return access_keymap (keymap
, key
, t_ok
, 0, 1);
1112 if (!VECTORP (key
) && !STRINGP (key
))
1113 key
= wrong_type_argument (Qarrayp
, key
);
1115 length
= XFASTINT (Flength (key
));
1124 c
= Faref (key
, make_number (idx
++));
1126 if (CONSP (c
) && lucid_event_type_list_p (c
))
1127 c
= Fevent_convert_list (c
);
1129 /* Turn the 8th bit of string chars into a meta modifier. */
1130 if (XINT (c
) & 0x80 && STRINGP (key
))
1131 XSETINT (c
, (XINT (c
) | meta_modifier
) & ~0x80);
1133 if (!INTEGERP (c
) && !SYMBOLP (c
) && !CONSP (c
))
1134 error ("Key sequence contains invalid event");
1136 cmd
= access_keymap (keymap
, c
, t_ok
, 0, 1);
1138 RETURN_UNGCPRO (cmd
);
1140 keymap
= get_keymap (cmd
, 0, 1);
1141 if (!CONSP (keymap
))
1142 RETURN_UNGCPRO (make_number (idx
));
1148 /* Make KEYMAP define event C as a keymap (i.e., as a prefix).
1149 Assume that currently it does not define C at all.
1150 Return the keymap. */
1153 define_as_prefix (keymap
, c
)
1154 Lisp_Object keymap
, c
;
1158 cmd
= Fmake_sparse_keymap (Qnil
);
1159 /* If this key is defined as a prefix in an inherited keymap,
1160 make it a prefix in this map, and make its definition
1161 inherit the other prefix definition. */
1162 cmd
= nconc2 (cmd
, access_keymap (keymap
, c
, 0, 0, 0));
1163 store_in_keymap (keymap
, c
, cmd
);
1168 /* Append a key to the end of a key sequence. We always make a vector. */
1171 append_key (key_sequence
, key
)
1172 Lisp_Object key_sequence
, key
;
1174 Lisp_Object args
[2];
1176 args
[0] = key_sequence
;
1178 args
[1] = Fcons (key
, Qnil
);
1179 return Fvconcat (2, args
);
1182 /* Given a event type C which is a symbol,
1183 signal an error if is a mistake such as RET or M-RET or C-DEL, etc. */
1186 silly_event_symbol_error (c
)
1189 Lisp_Object parsed
, base
, name
, assoc
;
1192 parsed
= parse_modifiers (c
);
1193 modifiers
= (int) XUINT (XCAR (XCDR (parsed
)));
1194 base
= XCAR (parsed
);
1195 name
= Fsymbol_name (base
);
1196 /* This alist includes elements such as ("RET" . "\\r"). */
1197 assoc
= Fassoc (name
, exclude_keys
);
1201 char new_mods
[sizeof ("\\A-\\C-\\H-\\M-\\S-\\s-")];
1203 Lisp_Object keystring
;
1204 if (modifiers
& alt_modifier
)
1205 { *p
++ = '\\'; *p
++ = 'A'; *p
++ = '-'; }
1206 if (modifiers
& ctrl_modifier
)
1207 { *p
++ = '\\'; *p
++ = 'C'; *p
++ = '-'; }
1208 if (modifiers
& hyper_modifier
)
1209 { *p
++ = '\\'; *p
++ = 'H'; *p
++ = '-'; }
1210 if (modifiers
& meta_modifier
)
1211 { *p
++ = '\\'; *p
++ = 'M'; *p
++ = '-'; }
1212 if (modifiers
& shift_modifier
)
1213 { *p
++ = '\\'; *p
++ = 'S'; *p
++ = '-'; }
1214 if (modifiers
& super_modifier
)
1215 { *p
++ = '\\'; *p
++ = 's'; *p
++ = '-'; }
1218 c
= reorder_modifiers (c
);
1219 keystring
= concat2 (build_string (new_mods
), XCDR (assoc
));
1221 error ((modifiers
& ~meta_modifier
1222 ? "To bind the key %s, use [?%s], not [%s]"
1223 : "To bind the key %s, use \"%s\", not [%s]"),
1224 XSYMBOL (c
)->name
->data
, XSTRING (keystring
)->data
,
1225 XSYMBOL (c
)->name
->data
);
1229 /* Global, local, and minor mode keymap stuff. */
1231 /* We can't put these variables inside current_minor_maps, since under
1232 some systems, static gets macro-defined to be the empty string.
1234 static Lisp_Object
*cmm_modes
, *cmm_maps
;
1235 static int cmm_size
;
1237 /* Error handler used in current_minor_maps. */
1239 current_minor_maps_error ()
1244 /* Store a pointer to an array of the keymaps of the currently active
1245 minor modes in *buf, and return the number of maps it contains.
1247 This function always returns a pointer to the same buffer, and may
1248 free or reallocate it, so if you want to keep it for a long time or
1249 hand it out to lisp code, copy it. This procedure will be called
1250 for every key sequence read, so the nice lispy approach (return a
1251 new assoclist, list, what have you) for each invocation would
1252 result in a lot of consing over time.
1254 If we used xrealloc/xmalloc and ran out of memory, they would throw
1255 back to the command loop, which would try to read a key sequence,
1256 which would call this function again, resulting in an infinite
1257 loop. Instead, we'll use realloc/malloc and silently truncate the
1258 list, let the key sequence be read, and hope some other piece of
1259 code signals the error. */
1261 current_minor_maps (modeptr
, mapptr
)
1262 Lisp_Object
**modeptr
, **mapptr
;
1265 int list_number
= 0;
1266 Lisp_Object alist
, assoc
, var
, val
;
1267 Lisp_Object lists
[2];
1269 lists
[0] = Vminor_mode_overriding_map_alist
;
1270 lists
[1] = Vminor_mode_map_alist
;
1272 for (list_number
= 0; list_number
< 2; list_number
++)
1273 for (alist
= lists
[list_number
];
1275 alist
= XCDR (alist
))
1276 if ((assoc
= XCAR (alist
), CONSP (assoc
))
1277 && (var
= XCAR (assoc
), SYMBOLP (var
))
1278 && (val
= find_symbol_value (var
), !EQ (val
, Qunbound
))
1283 /* If a variable has an entry in Vminor_mode_overriding_map_alist,
1284 and also an entry in Vminor_mode_map_alist,
1285 ignore the latter. */
1286 if (list_number
== 1)
1288 val
= assq_no_quit (var
, lists
[0]);
1295 Lisp_Object
*newmodes
, *newmaps
;
1297 /* Use malloc/realloc here. See the comment above
1304 = (Lisp_Object
*) realloc (cmm_modes
,
1305 cmm_size
* sizeof *newmodes
);
1307 = (Lisp_Object
*) realloc (cmm_maps
,
1308 cmm_size
* sizeof *newmaps
);
1316 = (Lisp_Object
*) malloc (cmm_size
* sizeof *newmodes
);
1318 = (Lisp_Object
*) malloc (cmm_size
* sizeof *newmaps
);
1323 cmm_modes
= newmodes
;
1327 if (newmodes
== NULL
|| newmaps
== NULL
)
1331 /* Get the keymap definition--or nil if it is not defined. */
1332 temp
= internal_condition_case_1 (Findirect_function
,
1334 Qerror
, current_minor_maps_error
);
1338 cmm_maps
[i
] = temp
;
1343 if (modeptr
) *modeptr
= cmm_modes
;
1344 if (mapptr
) *mapptr
= cmm_maps
;
1348 DEFUN ("current-active-maps", Fcurrent_active_maps
, Scurrent_active_maps
,
1350 doc
: /* Return a list of the currently active keymaps.
1351 OLP if non-nil indicates that we should obey `overriding-local-map' and
1352 `overriding-terminal-local-map'. */)
1356 Lisp_Object keymaps
= Fcons (current_global_map
, Qnil
);
1360 if (!NILP (Voverriding_local_map
))
1361 keymaps
= Fcons (Voverriding_local_map
, keymaps
);
1362 if (!NILP (current_kboard
->Voverriding_terminal_local_map
))
1363 keymaps
= Fcons (current_kboard
->Voverriding_terminal_local_map
, keymaps
);
1365 if (NILP (XCDR (keymaps
)))
1371 local
= get_local_map (PT
, current_buffer
, Qlocal_map
);
1373 keymaps
= Fcons (local
, keymaps
);
1375 nmaps
= current_minor_maps (0, &maps
);
1377 for (i
= --nmaps
; i
>= 0; i
--)
1378 if (!NILP (maps
[i
]))
1379 keymaps
= Fcons (maps
[i
], keymaps
);
1381 local
= get_local_map (PT
, current_buffer
, Qkeymap
);
1383 keymaps
= Fcons (local
, keymaps
);
1389 /* Like Fcommandp, but looks specifically for a command symbol, and
1390 doesn't signal errors. Returns 1 if FUNCTION is a command symbol. */
1392 is_command_symbol (function
)
1393 Lisp_Object function
;
1395 if (!SYMBOLP (function
) || EQ (function
, Qunbound
))
1398 function
= indirect_function (function
);
1399 if (SYMBOLP (function
) && EQ (function
, Qunbound
))
1402 if (SUBRP (function
))
1403 return (XSUBR (function
)->prompt
!= 0);
1405 if (COMPILEDP (function
))
1406 return ((ASIZE (function
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
);
1408 if (CONSP (function
))
1412 funcar
= Fcar (function
);
1413 if (SYMBOLP (funcar
))
1415 if (EQ (funcar
, Qlambda
))
1416 return !NILP (Fassq (Qinteractive
, Fcdr (Fcdr (function
))));
1417 if (EQ (funcar
, Qautoload
))
1418 return !NILP (Fcar (Fcdr (Fcdr (Fcdr (function
)))));
1424 /* GC is possible in this function if it autoloads a keymap. */
1426 DEFUN ("key-binding", Fkey_binding
, Skey_binding
, 1, 3, 0,
1427 doc
: /* Return the binding for command KEY in current keymaps.
1428 KEY is a string or vector, a sequence of keystrokes.
1429 The binding is probably a symbol with a function definition.
1431 Normally, `key-binding' ignores bindings for t, which act as default
1432 bindings, used when nothing else in the keymap applies; this makes it
1433 usable as a general function for probing keymaps. However, if the
1434 optional second argument ACCEPT-DEFAULT is non-nil, `key-binding' does
1435 recognize the default bindings, just as `read-key-sequence' does.
1437 Like the normal command loop, `key-binding' will remap the command
1438 resulting from looking up KEY by looking up the command in the
1439 currrent keymaps. However, if the optional third argument NO-REMAP
1440 is non-nil, `key-binding' returns the unmapped command. */)
1441 (key
, accept_default
, no_remap
)
1442 Lisp_Object key
, accept_default
, no_remap
;
1444 Lisp_Object
*maps
, value
;
1446 struct gcpro gcpro1
;
1450 if (!NILP (current_kboard
->Voverriding_terminal_local_map
))
1452 value
= Flookup_key (current_kboard
->Voverriding_terminal_local_map
,
1453 key
, accept_default
);
1454 if (! NILP (value
) && !INTEGERP (value
))
1457 else if (!NILP (Voverriding_local_map
))
1459 value
= Flookup_key (Voverriding_local_map
, key
, accept_default
);
1460 if (! NILP (value
) && !INTEGERP (value
))
1467 local
= get_local_map (PT
, current_buffer
, Qkeymap
);
1470 value
= Flookup_key (local
, key
, accept_default
);
1471 if (! NILP (value
) && !INTEGERP (value
))
1475 nmaps
= current_minor_maps (0, &maps
);
1476 /* Note that all these maps are GCPRO'd
1477 in the places where we found them. */
1479 for (i
= 0; i
< nmaps
; i
++)
1480 if (! NILP (maps
[i
]))
1482 value
= Flookup_key (maps
[i
], key
, accept_default
);
1483 if (! NILP (value
) && !INTEGERP (value
))
1487 local
= get_local_map (PT
, current_buffer
, Qlocal_map
);
1490 value
= Flookup_key (local
, key
, accept_default
);
1491 if (! NILP (value
) && !INTEGERP (value
))
1496 value
= Flookup_key (current_global_map
, key
, accept_default
);
1500 if (NILP (value
) || INTEGERP (value
))
1503 /* If the result of the ordinary keymap lookup is an interactive
1504 command, look for a key binding (ie. remapping) for that command. */
1506 if (NILP (no_remap
) && is_command_symbol (value
))
1510 value1
= Fkey_binding (value
, accept_default
, Qt
);
1511 if (!NILP (value1
) && is_command_symbol (value1
))
1518 /* GC is possible in this function if it autoloads a keymap. */
1520 DEFUN ("local-key-binding", Flocal_key_binding
, Slocal_key_binding
, 1, 2, 0,
1521 doc
: /* Return the binding for command KEYS in current local keymap only.
1522 KEYS is a string, a sequence of keystrokes.
1523 The binding is probably a symbol with a function definition.
1525 If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1526 bindings; see the description of `lookup-key' for more details about this. */)
1527 (keys
, accept_default
)
1528 Lisp_Object keys
, accept_default
;
1530 register Lisp_Object map
;
1531 map
= current_buffer
->keymap
;
1534 return Flookup_key (map
, keys
, accept_default
);
1537 /* GC is possible in this function if it autoloads a keymap. */
1539 DEFUN ("global-key-binding", Fglobal_key_binding
, Sglobal_key_binding
, 1, 2, 0,
1540 doc
: /* Return the binding for command KEYS in current global keymap only.
1541 KEYS is a string, a sequence of keystrokes.
1542 The binding is probably a symbol with a function definition.
1543 This function's return values are the same as those of lookup-key
1546 If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1547 bindings; see the description of `lookup-key' for more details about this. */)
1548 (keys
, accept_default
)
1549 Lisp_Object keys
, accept_default
;
1551 return Flookup_key (current_global_map
, keys
, accept_default
);
1554 /* GC is possible in this function if it autoloads a keymap. */
1556 DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding
, Sminor_mode_key_binding
, 1, 2, 0,
1557 doc
: /* Find the visible minor mode bindings of KEY.
1558 Return an alist of pairs (MODENAME . BINDING), where MODENAME is the
1559 the symbol which names the minor mode binding KEY, and BINDING is
1560 KEY's definition in that mode. In particular, if KEY has no
1561 minor-mode bindings, return nil. If the first binding is a
1562 non-prefix, all subsequent bindings will be omitted, since they would
1563 be ignored. Similarly, the list doesn't include non-prefix bindings
1564 that come after prefix bindings.
1566 If optional argument ACCEPT-DEFAULT is non-nil, recognize default
1567 bindings; see the description of `lookup-key' for more details about this. */)
1568 (key
, accept_default
)
1569 Lisp_Object key
, accept_default
;
1571 Lisp_Object
*modes
, *maps
;
1573 Lisp_Object binding
;
1575 struct gcpro gcpro1
, gcpro2
;
1577 nmaps
= current_minor_maps (&modes
, &maps
);
1578 /* Note that all these maps are GCPRO'd
1579 in the places where we found them. */
1582 GCPRO2 (key
, binding
);
1584 for (i
= j
= 0; i
< nmaps
; i
++)
1586 && !NILP (binding
= Flookup_key (maps
[i
], key
, accept_default
))
1587 && !INTEGERP (binding
))
1589 if (KEYMAPP (binding
))
1590 maps
[j
++] = Fcons (modes
[i
], binding
);
1592 RETURN_UNGCPRO (Fcons (Fcons (modes
[i
], binding
), Qnil
));
1596 return Flist (j
, maps
);
1599 DEFUN ("define-prefix-command", Fdefine_prefix_command
, Sdefine_prefix_command
, 1, 3, 0,
1600 doc
: /* Define COMMAND as a prefix command. COMMAND should be a symbol.
1601 A new sparse keymap is stored as COMMAND's function definition and its value.
1602 If a second optional argument MAPVAR is given, the map is stored as
1603 its value instead of as COMMAND's value; but COMMAND is still defined
1605 The third optional argument NAME, if given, supplies a menu name
1606 string for the map. This is required to use the keymap as a menu. */)
1607 (command
, mapvar
, name
)
1608 Lisp_Object command
, mapvar
, name
;
1611 map
= Fmake_sparse_keymap (name
);
1612 Ffset (command
, map
);
1616 Fset (command
, map
);
1620 DEFUN ("use-global-map", Fuse_global_map
, Suse_global_map
, 1, 1, 0,
1621 doc
: /* Select KEYMAP as the global keymap. */)
1625 keymap
= get_keymap (keymap
, 1, 1);
1626 current_global_map
= keymap
;
1631 DEFUN ("use-local-map", Fuse_local_map
, Suse_local_map
, 1, 1, 0,
1632 doc
: /* Select KEYMAP as the local keymap.
1633 If KEYMAP is nil, that means no local keymap. */)
1638 keymap
= get_keymap (keymap
, 1, 1);
1640 current_buffer
->keymap
= keymap
;
1645 DEFUN ("current-local-map", Fcurrent_local_map
, Scurrent_local_map
, 0, 0, 0,
1646 doc
: /* Return current buffer's local keymap, or nil if it has none. */)
1649 return current_buffer
->keymap
;
1652 DEFUN ("current-global-map", Fcurrent_global_map
, Scurrent_global_map
, 0, 0, 0,
1653 doc
: /* Return the current global keymap. */)
1656 return current_global_map
;
1659 DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps
, Scurrent_minor_mode_maps
, 0, 0, 0,
1660 doc
: /* Return a list of keymaps for the minor modes of the current buffer. */)
1664 int nmaps
= current_minor_maps (0, &maps
);
1666 return Flist (nmaps
, maps
);
1669 /* Help functions for describing and documenting keymaps. */
1673 accessible_keymaps_1 (key
, cmd
, maps
, tail
, thisseq
, is_metized
)
1674 Lisp_Object maps
, tail
, thisseq
, key
, cmd
;
1675 int is_metized
; /* If 1, `key' is assumed to be INTEGERP. */
1679 cmd
= get_keyelt (cmd
, 0);
1683 tem
= get_keymap (cmd
, 0, 0);
1687 /* Ignore keymaps that are already added to maps. */
1688 tem
= Frassq (cmd
, maps
);
1691 /* If the last key in thisseq is meta-prefix-char,
1692 turn it into a meta-ized keystroke. We know
1693 that the event we're about to append is an
1694 ascii keystroke since we're processing a
1698 int meta_bit
= meta_modifier
;
1699 Lisp_Object last
= make_number (XINT (Flength (thisseq
)) - 1);
1700 tem
= Fcopy_sequence (thisseq
);
1702 Faset (tem
, last
, make_number (XINT (key
) | meta_bit
));
1704 /* This new sequence is the same length as
1705 thisseq, so stick it in the list right
1708 Fcons (Fcons (tem
, cmd
), XCDR (tail
)));
1712 tem
= append_key (thisseq
, key
);
1713 nconc2 (tail
, Fcons (Fcons (tem
, cmd
), Qnil
));
1720 accessible_keymaps_char_table (args
, index
, cmd
)
1721 Lisp_Object args
, index
, cmd
;
1723 accessible_keymaps_1 (index
, cmd
,
1727 XINT (XCDR (XCAR (args
))));
1730 /* This function cannot GC. */
1732 DEFUN ("accessible-keymaps", Faccessible_keymaps
, Saccessible_keymaps
,
1734 doc
: /* Find all keymaps accessible via prefix characters from KEYMAP.
1735 Returns a list of elements of the form (KEYS . MAP), where the sequence
1736 KEYS starting from KEYMAP gets you to MAP. These elements are ordered
1737 so that the KEYS increase in length. The first element is ([] . KEYMAP).
1738 An optional argument PREFIX, if non-nil, should be a key sequence;
1739 then the value includes only maps for prefixes that start with PREFIX. */)
1741 Lisp_Object keymap
, prefix
;
1743 Lisp_Object maps
, good_maps
, tail
;
1746 /* no need for gcpro because we don't autoload any keymaps. */
1749 prefixlen
= XINT (Flength (prefix
));
1753 /* If a prefix was specified, start with the keymap (if any) for
1754 that prefix, so we don't waste time considering other prefixes. */
1756 tem
= Flookup_key (keymap
, prefix
, Qt
);
1757 /* Flookup_key may give us nil, or a number,
1758 if the prefix is not defined in this particular map.
1759 It might even give us a list that isn't a keymap. */
1760 tem
= get_keymap (tem
, 0, 0);
1763 /* Convert PREFIX to a vector now, so that later on
1764 we don't have to deal with the possibility of a string. */
1765 if (STRINGP (prefix
))
1770 copy
= Fmake_vector (make_number (XSTRING (prefix
)->size
), Qnil
);
1771 for (i
= 0, i_byte
= 0; i
< XSTRING (prefix
)->size
;)
1775 FETCH_STRING_CHAR_ADVANCE (c
, prefix
, i
, i_byte
);
1776 if (SINGLE_BYTE_CHAR_P (c
) && (c
& 0200))
1777 c
^= 0200 | meta_modifier
;
1778 ASET (copy
, i_before
, make_number (c
));
1782 maps
= Fcons (Fcons (prefix
, tem
), Qnil
);
1788 maps
= Fcons (Fcons (Fmake_vector (make_number (0), Qnil
),
1789 get_keymap (keymap
, 1, 0)),
1792 /* For each map in the list maps,
1793 look at any other maps it points to,
1794 and stick them at the end if they are not already in the list.
1796 This is a breadth-first traversal, where tail is the queue of
1797 nodes, and maps accumulates a list of all nodes visited. */
1799 for (tail
= maps
; CONSP (tail
); tail
= XCDR (tail
))
1801 register Lisp_Object thisseq
, thismap
;
1803 /* Does the current sequence end in the meta-prefix-char? */
1806 thisseq
= Fcar (Fcar (tail
));
1807 thismap
= Fcdr (Fcar (tail
));
1808 last
= make_number (XINT (Flength (thisseq
)) - 1);
1809 is_metized
= (XINT (last
) >= 0
1810 /* Don't metize the last char of PREFIX. */
1811 && XINT (last
) >= prefixlen
1812 && EQ (Faref (thisseq
, last
), meta_prefix_char
));
1814 for (; CONSP (thismap
); thismap
= XCDR (thismap
))
1818 elt
= XCAR (thismap
);
1822 if (CHAR_TABLE_P (elt
))
1824 Lisp_Object indices
[3];
1826 map_char_table (accessible_keymaps_char_table
, Qnil
,
1827 elt
, Fcons (Fcons (maps
, make_number (is_metized
)),
1828 Fcons (tail
, thisseq
)),
1831 else if (VECTORP (elt
))
1835 /* Vector keymap. Scan all the elements. */
1836 for (i
= 0; i
< ASIZE (elt
); i
++)
1837 accessible_keymaps_1 (make_number (i
), AREF (elt
, i
),
1838 maps
, tail
, thisseq
, is_metized
);
1841 else if (CONSP (elt
))
1842 accessible_keymaps_1 (XCAR (elt
), XCDR (elt
),
1843 maps
, tail
, thisseq
,
1844 is_metized
&& INTEGERP (XCAR (elt
)));
1852 /* Now find just the maps whose access prefixes start with PREFIX. */
1855 for (; CONSP (maps
); maps
= XCDR (maps
))
1857 Lisp_Object elt
, thisseq
;
1859 thisseq
= XCAR (elt
);
1860 /* The access prefix must be at least as long as PREFIX,
1861 and the first elements must match those of PREFIX. */
1862 if (XINT (Flength (thisseq
)) >= prefixlen
)
1865 for (i
= 0; i
< prefixlen
; i
++)
1868 XSETFASTINT (i1
, i
);
1869 if (!EQ (Faref (thisseq
, i1
), Faref (prefix
, i1
)))
1873 good_maps
= Fcons (elt
, good_maps
);
1877 return Fnreverse (good_maps
);
1880 Lisp_Object Qsingle_key_description
, Qkey_description
;
1882 /* This function cannot GC. */
1884 DEFUN ("key-description", Fkey_description
, Skey_description
, 1, 1, 0,
1885 doc
: /* Return a pretty description of key-sequence KEYS.
1886 Control characters turn into "C-foo" sequences, meta into "M-foo"
1887 spaces are put between sequence elements, etc. */)
1894 Lisp_Object
*args
= NULL
;
1899 vector
= Fmake_vector (Flength (keys
), Qnil
);
1900 for (i
= 0, i_byte
= 0; i
< XSTRING (keys
)->size
; )
1905 FETCH_STRING_CHAR_ADVANCE (c
, keys
, i
, i_byte
);
1906 if (SINGLE_BYTE_CHAR_P (c
) && (c
& 0200))
1907 c
^= 0200 | meta_modifier
;
1908 XSETFASTINT (AREF (vector
, i_before
), c
);
1915 /* In effect, this computes
1916 (mapconcat 'single-key-description keys " ")
1917 but we shouldn't use mapconcat because it can do GC. */
1919 len
= XVECTOR (keys
)->size
;
1920 sep
= build_string (" ");
1921 /* This has one extra element at the end that we don't pass to Fconcat. */
1922 args
= (Lisp_Object
*) alloca (len
* 2 * sizeof (Lisp_Object
));
1924 for (i
= 0; i
< len
; i
++)
1926 args
[i
* 2] = Fsingle_key_description (AREF (keys
, i
), Qnil
);
1927 args
[i
* 2 + 1] = sep
;
1930 else if (CONSP (keys
))
1932 /* In effect, this computes
1933 (mapconcat 'single-key-description keys " ")
1934 but we shouldn't use mapconcat because it can do GC. */
1936 len
= XFASTINT (Flength (keys
));
1937 sep
= build_string (" ");
1938 /* This has one extra element at the end that we don't pass to Fconcat. */
1939 args
= (Lisp_Object
*) alloca (len
* 2 * sizeof (Lisp_Object
));
1941 for (i
= 0; i
< len
; i
++)
1943 args
[i
* 2] = Fsingle_key_description (XCAR (keys
), Qnil
);
1944 args
[i
* 2 + 1] = sep
;
1949 keys
= wrong_type_argument (Qarrayp
, keys
);
1952 return empty_string
;
1953 return Fconcat (len
* 2 - 1, args
);
1957 push_key_description (c
, p
, force_multibyte
)
1958 register unsigned int c
;
1960 int force_multibyte
;
1964 /* Clear all the meaningless bits above the meta bit. */
1965 c
&= meta_modifier
| ~ - meta_modifier
;
1966 c2
= c
& ~(alt_modifier
| ctrl_modifier
| hyper_modifier
1967 | meta_modifier
| shift_modifier
| super_modifier
);
1969 if (c
& alt_modifier
)
1975 if ((c
& ctrl_modifier
) != 0
1976 || (c2
< ' ' && c2
!= 27 && c2
!= '\t' && c2
!= Ctl ('M')))
1980 c
&= ~ctrl_modifier
;
1982 if (c
& hyper_modifier
)
1986 c
-= hyper_modifier
;
1988 if (c
& meta_modifier
)
1994 if (c
& shift_modifier
)
1998 c
-= shift_modifier
;
2000 if (c
& super_modifier
)
2004 c
-= super_modifier
;
2020 else if (c
== Ctl ('M'))
2028 /* `C-' already added above. */
2029 if (c
> 0 && c
<= Ctl ('Z'))
2048 || (NILP (current_buffer
->enable_multibyte_characters
)
2049 && SINGLE_BYTE_CHAR_P (c
)
2050 && !force_multibyte
))
2056 int valid_p
= SINGLE_BYTE_CHAR_P (c
) || char_valid_p (c
, 0);
2058 if (force_multibyte
&& valid_p
)
2060 if (SINGLE_BYTE_CHAR_P (c
))
2061 c
= unibyte_char_to_multibyte (c
);
2062 p
+= CHAR_STRING (c
, p
);
2064 else if (NILP (current_buffer
->enable_multibyte_characters
)
2069 /* The biggest character code uses 19 bits. */
2070 for (bit_offset
= 18; bit_offset
>= 0; bit_offset
-= 3)
2072 if (c
>= (1 << bit_offset
))
2073 *p
++ = ((c
& (7 << bit_offset
)) >> bit_offset
) + '0';
2077 p
+= CHAR_STRING (c
, p
);
2083 /* This function cannot GC. */
2085 DEFUN ("single-key-description", Fsingle_key_description
,
2086 Ssingle_key_description
, 1, 2, 0,
2087 doc
: /* Return a pretty description of command character KEY.
2088 Control characters turn into C-whatever, etc.
2089 Optional argument NO-ANGLES non-nil means don't put angle brackets
2090 around function keys and event symbols. */)
2092 Lisp_Object key
, no_angles
;
2094 if (CONSP (key
) && lucid_event_type_list_p (key
))
2095 key
= Fevent_convert_list (key
);
2097 key
= EVENT_HEAD (key
);
2099 if (INTEGERP (key
)) /* Normal character */
2101 unsigned int charset
, c1
, c2
;
2102 int without_bits
= XINT (key
) & ~((-1) << CHARACTERBITS
);
2104 if (SINGLE_BYTE_CHAR_P (without_bits
))
2107 SPLIT_CHAR (without_bits
, charset
, c1
, c2
);
2110 && CHARSET_DEFINED_P (charset
)
2111 && ((c1
>= 0 && c1
< 32)
2112 || (c2
>= 0 && c2
< 32)))
2114 /* Handle a generic character. */
2116 name
= CHARSET_TABLE_INFO (charset
, CHARSET_LONG_NAME_IDX
);
2117 CHECK_STRING (name
);
2118 return concat2 (build_string ("Character set "), name
);
2122 char tem
[KEY_DESCRIPTION_SIZE
], *end
;
2126 end
= push_key_description (XUINT (key
), tem
, 1);
2128 nchars
= multibyte_chars_in_text (tem
, nbytes
);
2129 if (nchars
== nbytes
)
2132 string
= build_string (tem
);
2135 string
= make_multibyte_string (tem
, nchars
, nbytes
);
2139 else if (SYMBOLP (key
)) /* Function key or event-symbol */
2141 if (NILP (no_angles
))
2144 = (char *) alloca (STRING_BYTES (XSYMBOL (key
)->name
) + 5);
2145 sprintf (buffer
, "<%s>", XSYMBOL (key
)->name
->data
);
2146 return build_string (buffer
);
2149 return Fsymbol_name (key
);
2151 else if (STRINGP (key
)) /* Buffer names in the menubar. */
2152 return Fcopy_sequence (key
);
2154 error ("KEY must be an integer, cons, symbol, or string");
2159 push_text_char_description (c
, p
)
2160 register unsigned int c
;
2172 *p
++ = c
+ 64; /* 'A' - 1 */
2184 /* This function cannot GC. */
2186 DEFUN ("text-char-description", Ftext_char_description
, Stext_char_description
, 1, 1, 0,
2187 doc
: /* Return a pretty description of file-character CHARACTER.
2188 Control characters turn into "^char", etc. */)
2190 Lisp_Object character
;
2192 /* Currently MAX_MULTIBYTE_LENGTH is 4 (< 6). */
2193 unsigned char str
[6];
2196 CHECK_NUMBER (character
);
2198 c
= XINT (character
);
2199 if (!SINGLE_BYTE_CHAR_P (c
))
2201 int len
= CHAR_STRING (c
, str
);
2203 return make_multibyte_string (str
, 1, len
);
2206 *push_text_char_description (c
& 0377, str
) = 0;
2208 return build_string (str
);
2211 /* Return non-zero if SEQ contains only ASCII characters, perhaps with
2214 ascii_sequence_p (seq
)
2218 int len
= XINT (Flength (seq
));
2220 for (i
= 0; i
< len
; i
++)
2222 Lisp_Object ii
, elt
;
2224 XSETFASTINT (ii
, i
);
2225 elt
= Faref (seq
, ii
);
2228 || (XUINT (elt
) & ~CHAR_META
) >= 0x80)
2236 /* where-is - finding a command in a set of keymaps. */
2238 static Lisp_Object
where_is_internal ();
2239 static Lisp_Object
where_is_internal_1 ();
2240 static void where_is_internal_2 ();
2242 /* Like Flookup_key, but uses a list of keymaps SHADOW instead of a single map.
2243 Returns the first non-nil binding found in any of those maps. */
2246 shadow_lookup (shadow
, key
, flag
)
2247 Lisp_Object shadow
, key
, flag
;
2249 Lisp_Object tail
, value
;
2251 for (tail
= shadow
; CONSP (tail
); tail
= XCDR (tail
))
2253 value
= Flookup_key (XCAR (tail
), key
, flag
);
2254 if (!NILP (value
) && !NATNUMP (value
))
2260 /* This function can GC if Flookup_key autoloads any keymaps. */
2263 where_is_internal (definition
, keymaps
, firstonly
, noindirect
, no_remap
)
2264 Lisp_Object definition
, keymaps
;
2265 Lisp_Object firstonly
, noindirect
, no_remap
;
2267 Lisp_Object maps
= Qnil
;
2268 Lisp_Object found
, sequences
;
2269 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
2270 /* 1 means ignore all menu bindings entirely. */
2271 int nomenus
= !NILP (firstonly
) && !EQ (firstonly
, Qnon_ascii
);
2273 /* If this command is remapped, then it has no key bindings
2275 if (NILP (no_remap
) && is_command_symbol (definition
)
2276 && !NILP (Fkey_binding (definition
, Qnil
, Qt
)))
2280 while (CONSP (found
))
2284 Faccessible_keymaps (get_keymap (XCAR (found
), 1, 0), Qnil
));
2285 found
= XCDR (found
);
2288 GCPRO5 (definition
, keymaps
, maps
, found
, sequences
);
2292 for (; !NILP (maps
); maps
= Fcdr (maps
))
2294 /* Key sequence to reach map, and the map that it reaches */
2295 register Lisp_Object
this, map
;
2297 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
2298 [M-CHAR] sequences, check if last character of the sequence
2299 is the meta-prefix char. */
2303 this = Fcar (Fcar (maps
));
2304 map
= Fcdr (Fcar (maps
));
2305 last
= make_number (XINT (Flength (this)) - 1);
2306 last_is_meta
= (XINT (last
) >= 0
2307 && EQ (Faref (this, last
), meta_prefix_char
));
2309 /* if (nomenus && !ascii_sequence_p (this)) */
2310 if (nomenus
&& XINT (last
) >= 0
2311 && !INTEGERP (Faref (this, make_number (0))))
2312 /* If no menu entries should be returned, skip over the
2313 keymaps bound to `menu-bar' and `tool-bar' and other
2314 non-ascii prefixes like `C-down-mouse-2'. */
2321 /* Because the code we want to run on each binding is rather
2322 large, we don't want to have two separate loop bodies for
2323 sparse keymap bindings and tables; we want to iterate one
2324 loop body over both keymap and vector bindings.
2326 For this reason, if Fcar (map) is a vector, we don't
2327 advance map to the next element until i indicates that we
2328 have finished off the vector. */
2329 Lisp_Object elt
, key
, binding
;
2337 /* Set key and binding to the current key and binding, and
2338 advance map and i to the next binding. */
2341 Lisp_Object sequence
;
2343 /* In a vector, look at each element. */
2344 for (i
= 0; i
< XVECTOR (elt
)->size
; i
++)
2346 binding
= AREF (elt
, i
);
2347 XSETFASTINT (key
, i
);
2348 sequence
= where_is_internal_1 (binding
, key
, definition
,
2350 last
, nomenus
, last_is_meta
);
2351 if (!NILP (sequence
))
2352 sequences
= Fcons (sequence
, sequences
);
2355 else if (CHAR_TABLE_P (elt
))
2357 Lisp_Object indices
[3];
2360 args
= Fcons (Fcons (Fcons (definition
, noindirect
),
2361 Qnil
), /* Result accumulator. */
2362 Fcons (Fcons (this, last
),
2363 Fcons (make_number (nomenus
),
2364 make_number (last_is_meta
))));
2365 map_char_table (where_is_internal_2
, Qnil
, elt
, args
,
2367 sequences
= XCDR (XCAR (args
));
2369 else if (CONSP (elt
))
2371 Lisp_Object sequence
;
2374 binding
= XCDR (elt
);
2376 sequence
= where_is_internal_1 (binding
, key
, definition
,
2378 last
, nomenus
, last_is_meta
);
2379 if (!NILP (sequence
))
2380 sequences
= Fcons (sequence
, sequences
);
2384 while (!NILP (sequences
))
2386 Lisp_Object sequence
;
2387 Lisp_Object remapped
;
2389 sequence
= XCAR (sequences
);
2390 sequences
= XCDR (sequences
);
2392 /* If the current sequence is of the form [command],
2393 this may be a remapped command, so look for the key
2394 sequences which run that command, and return those
2395 sequences instead. */
2398 && VECTORP (sequence
) && XVECTOR (sequence
)->size
== 1)
2400 Lisp_Object function
;
2402 function
= AREF (sequence
, 0);
2403 if (is_command_symbol (function
))
2405 Lisp_Object remapped1
;
2406 remapped1
= where_is_internal (function
, keymaps
, firstonly
, noindirect
, Qt
);
2407 if (CONSP (remapped1
))
2409 /* Verify that this key binding actually maps to the
2410 remapped command (see below). */
2411 if (!EQ (shadow_lookup (keymaps
, XCAR (remapped1
), Qnil
), function
))
2413 sequence
= XCAR (remapped1
);
2414 remapped
= XCDR (remapped1
);
2415 goto record_sequence
;
2420 /* Verify that this key binding is not shadowed by another
2421 binding for the same key, before we say it exists.
2423 Mechanism: look for local definition of this key and if
2424 it is defined and does not match what we found then
2427 Either nil or number as value from Flookup_key
2429 if (!EQ (shadow_lookup (keymaps
, sequence
, Qnil
), definition
))
2433 /* It is a true unshadowed match. Record it, unless it's already
2434 been seen (as could happen when inheriting keymaps). */
2435 if (NILP (Fmember (sequence
, found
)))
2436 found
= Fcons (sequence
, found
);
2438 /* If firstonly is Qnon_ascii, then we can return the first
2439 binding we find. If firstonly is not Qnon_ascii but not
2440 nil, then we should return the first ascii-only binding
2442 if (EQ (firstonly
, Qnon_ascii
))
2443 RETURN_UNGCPRO (sequence
);
2444 else if (!NILP (firstonly
) && ascii_sequence_p (sequence
))
2445 RETURN_UNGCPRO (sequence
);
2447 if (CONSP (remapped
))
2449 sequence
= XCAR (remapped
);
2450 remapped
= XCDR (remapped
);
2451 goto record_sequence
;
2459 found
= Fnreverse (found
);
2461 /* firstonly may have been t, but we may have gone all the way through
2462 the keymaps without finding an all-ASCII key sequence. So just
2463 return the best we could find. */
2464 if (!NILP (firstonly
))
2465 return Fcar (found
);
2470 DEFUN ("where-is-internal", Fwhere_is_internal
, Swhere_is_internal
, 1, 5, 0,
2471 doc
: /* Return list of keys that invoke DEFINITION.
2472 If KEYMAP is non-nil, search only KEYMAP and the global keymap.
2473 If KEYMAP is nil, search all the currently active keymaps.
2474 If KEYMAP is a list of keymaps, search only those keymaps.
2476 If optional 3rd arg FIRSTONLY is non-nil, return the first key sequence found,
2477 rather than a list of all possible key sequences.
2478 If FIRSTONLY is the symbol `non-ascii', return the first binding found,
2479 no matter what it is.
2480 If FIRSTONLY has another non-nil value, prefer sequences of ASCII characters,
2481 and entirely reject menu bindings.
2483 If optional 4th arg NOINDIRECT is non-nil, don't follow indirections
2484 to other keymaps or slots. This makes it possible to search for an
2485 indirect definition itself.
2487 If optional 5th arg NO-REMAP is non-nil, don't search for key sequences
2488 that invoke a command which is remapped to DEFINITION, but include the
2489 remapped command in the returned list. */)
2490 (definition
, keymap
, firstonly
, noindirect
, no_remap
)
2491 Lisp_Object definition
, keymap
;
2492 Lisp_Object firstonly
, noindirect
, no_remap
;
2494 Lisp_Object sequences
, keymaps
;
2495 /* 1 means ignore all menu bindings entirely. */
2496 int nomenus
= !NILP (firstonly
) && !EQ (firstonly
, Qnon_ascii
);
2499 /* Find the relevant keymaps. */
2500 if (CONSP (keymap
) && KEYMAPP (XCAR (keymap
)))
2502 else if (!NILP (keymap
))
2503 keymaps
= Fcons (keymap
, Fcons (current_global_map
, Qnil
));
2505 keymaps
= Fcurrent_active_maps (Qnil
);
2507 /* Only use caching for the menubar (i.e. called with (def nil t nil).
2508 We don't really need to check `keymap'. */
2509 if (nomenus
&& NILP (noindirect
) && NILP (keymap
))
2513 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
2515 /* Check heuristic-consistency of the cache. */
2516 if (NILP (Fequal (keymaps
, where_is_cache_keymaps
)))
2517 where_is_cache
= Qnil
;
2519 if (NILP (where_is_cache
))
2521 /* We need to create the cache. */
2522 Lisp_Object args
[2];
2523 where_is_cache
= Fmake_hash_table (0, args
);
2524 where_is_cache_keymaps
= Qt
;
2526 /* Fill in the cache. */
2527 GCPRO5 (definition
, keymaps
, firstonly
, noindirect
, no_remap
);
2528 where_is_internal (definition
, keymaps
, firstonly
, noindirect
, no_remap
);
2531 where_is_cache_keymaps
= keymaps
;
2534 /* We want to process definitions from the last to the first.
2535 Instead of consing, copy definitions to a vector and step
2536 over that vector. */
2537 sequences
= Fgethash (definition
, where_is_cache
, Qnil
);
2538 n
= XINT (Flength (sequences
));
2539 defns
= (Lisp_Object
*) alloca (n
* sizeof *defns
);
2540 for (i
= 0; CONSP (sequences
); sequences
= XCDR (sequences
))
2541 defns
[i
++] = XCAR (sequences
);
2543 /* Verify that the key bindings are not shadowed. Note that
2544 the following can GC. */
2545 GCPRO2 (definition
, keymaps
);
2548 for (i
= n
- 1; i
>= 0; --i
)
2549 if (EQ (shadow_lookup (keymaps
, defns
[i
], Qnil
), definition
))
2551 if (ascii_sequence_p (defns
[i
]))
2557 result
= i
>= 0 ? defns
[i
] : (j
>= 0 ? defns
[j
] : Qnil
);
2562 /* Kill the cache so that where_is_internal_1 doesn't think
2563 we're filling it up. */
2564 where_is_cache
= Qnil
;
2565 result
= where_is_internal (definition
, keymaps
, firstonly
, noindirect
, no_remap
);
2571 /* This is the function that Fwhere_is_internal calls using map_char_table.
2573 (((DEFINITION . NOINDIRECT) . (KEYMAP . RESULT))
2575 ((THIS . LAST) . (NOMENUS . LAST_IS_META)))
2576 Since map_char_table doesn't really use the return value from this function,
2577 we the result append to RESULT, the slot in ARGS.
2579 This function can GC because it calls where_is_internal_1 which can
2583 where_is_internal_2 (args
, key
, binding
)
2584 Lisp_Object args
, key
, binding
;
2586 Lisp_Object definition
, noindirect
, this, last
;
2587 Lisp_Object result
, sequence
;
2588 int nomenus
, last_is_meta
;
2589 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2591 GCPRO3 (args
, key
, binding
);
2592 result
= XCDR (XCAR (args
));
2593 definition
= XCAR (XCAR (XCAR (args
)));
2594 noindirect
= XCDR (XCAR (XCAR (args
)));
2595 this = XCAR (XCAR (XCDR (args
)));
2596 last
= XCDR (XCAR (XCDR (args
)));
2597 nomenus
= XFASTINT (XCAR (XCDR (XCDR (args
))));
2598 last_is_meta
= XFASTINT (XCDR (XCDR (XCDR (args
))));
2600 sequence
= where_is_internal_1 (binding
, key
, definition
, noindirect
,
2601 this, last
, nomenus
, last_is_meta
);
2603 if (!NILP (sequence
))
2604 XSETCDR (XCAR (args
), Fcons (sequence
, result
));
2610 /* This function cannot GC. */
2613 where_is_internal_1 (binding
, key
, definition
, noindirect
, this, last
,
2614 nomenus
, last_is_meta
)
2615 Lisp_Object binding
, key
, definition
, noindirect
, this, last
;
2616 int nomenus
, last_is_meta
;
2618 Lisp_Object sequence
;
2620 /* Search through indirections unless that's not wanted. */
2621 if (NILP (noindirect
))
2622 binding
= get_keyelt (binding
, 0);
2624 /* End this iteration if this element does not match
2627 if (!(!NILP (where_is_cache
) /* everything "matches" during cache-fill. */
2628 || EQ (binding
, definition
)
2629 || (CONSP (definition
) && !NILP (Fequal (binding
, definition
)))))
2630 /* Doesn't match. */
2633 /* We have found a match. Construct the key sequence where we found it. */
2634 if (INTEGERP (key
) && last_is_meta
)
2636 sequence
= Fcopy_sequence (this);
2637 Faset (sequence
, last
, make_number (XINT (key
) | meta_modifier
));
2640 sequence
= append_key (this, key
);
2642 if (!NILP (where_is_cache
))
2644 Lisp_Object sequences
= Fgethash (binding
, where_is_cache
, Qnil
);
2645 Fputhash (binding
, Fcons (sequence
, sequences
), where_is_cache
);
2652 /* describe-bindings - summarizing all the bindings in a set of keymaps. */
2654 DEFUN ("describe-buffer-bindings", Fdescribe_buffer_bindings
, Sdescribe_buffer_bindings
, 1, 3, 0,
2655 doc
: /* Insert the list of all defined keys and their definitions.
2656 The list is inserted in the current buffer, while the bindings are
2657 looked up in BUFFER.
2658 The optional argument PREFIX, if non-nil, should be a key sequence;
2659 then we display only bindings that start with that prefix.
2660 The optional argument MENUS, if non-nil, says to mention menu bindings.
2661 \(Ordinarily these are omitted from the output.) */)
2662 (buffer
, prefix
, menus
)
2663 Lisp_Object buffer
, prefix
, menus
;
2665 Lisp_Object outbuf
, shadow
;
2666 int nomenu
= NILP (menus
);
2667 register Lisp_Object start1
;
2668 struct gcpro gcpro1
;
2670 char *alternate_heading
2672 Keyboard translations:\n\n\
2673 You type Translation\n\
2674 -------- -----------\n";
2679 outbuf
= Fcurrent_buffer ();
2681 /* Report on alternates for keys. */
2682 if (STRINGP (Vkeyboard_translate_table
) && !NILP (prefix
))
2685 unsigned char *translate
= XSTRING (Vkeyboard_translate_table
)->data
;
2686 int translate_len
= XSTRING (Vkeyboard_translate_table
)->size
;
2688 for (c
= 0; c
< translate_len
; c
++)
2689 if (translate
[c
] != c
)
2691 char buf
[KEY_DESCRIPTION_SIZE
];
2694 if (alternate_heading
)
2696 insert_string (alternate_heading
);
2697 alternate_heading
= 0;
2700 bufend
= push_key_description (translate
[c
], buf
, 1);
2701 insert (buf
, bufend
- buf
);
2702 Findent_to (make_number (16), make_number (1));
2703 bufend
= push_key_description (c
, buf
, 1);
2704 insert (buf
, bufend
- buf
);
2712 if (!NILP (Vkey_translation_map
))
2713 describe_map_tree (Vkey_translation_map
, 0, Qnil
, prefix
,
2714 "Key translations", nomenu
, 1, 0);
2717 /* Print the (major mode) local map. */
2719 if (!NILP (current_kboard
->Voverriding_terminal_local_map
))
2720 start1
= current_kboard
->Voverriding_terminal_local_map
;
2721 else if (!NILP (Voverriding_local_map
))
2722 start1
= Voverriding_local_map
;
2726 describe_map_tree (start1
, 1, shadow
, prefix
,
2727 "\f\nOverriding Bindings", nomenu
, 0, 0);
2728 shadow
= Fcons (start1
, shadow
);
2732 /* Print the minor mode and major mode keymaps. */
2734 Lisp_Object
*modes
, *maps
;
2736 /* Temporarily switch to `buffer', so that we can get that buffer's
2737 minor modes correctly. */
2738 Fset_buffer (buffer
);
2740 nmaps
= current_minor_maps (&modes
, &maps
);
2741 Fset_buffer (outbuf
);
2743 start1
= get_local_map (BUF_PT (XBUFFER (buffer
)),
2744 XBUFFER (buffer
), Qkeymap
);
2747 describe_map_tree (start1
, 1, shadow
, prefix
,
2748 "\f\n`keymap' Property Bindings", nomenu
, 0, 0);
2749 shadow
= Fcons (start1
, shadow
);
2752 /* Print the minor mode maps. */
2753 for (i
= 0; i
< nmaps
; i
++)
2755 /* The title for a minor mode keymap
2756 is constructed at run time.
2757 We let describe_map_tree do the actual insertion
2758 because it takes care of other features when doing so. */
2761 if (!SYMBOLP (modes
[i
]))
2764 p
= title
= (char *) alloca (42 + XSYMBOL (modes
[i
])->name
->size
);
2768 bcopy (XSYMBOL (modes
[i
])->name
->data
, p
,
2769 XSYMBOL (modes
[i
])->name
->size
);
2770 p
+= XSYMBOL (modes
[i
])->name
->size
;
2772 bcopy (" Minor Mode Bindings", p
, sizeof (" Minor Mode Bindings") - 1);
2773 p
+= sizeof (" Minor Mode Bindings") - 1;
2776 describe_map_tree (maps
[i
], 1, shadow
, prefix
, title
, nomenu
, 0, 0);
2777 shadow
= Fcons (maps
[i
], shadow
);
2780 start1
= get_local_map (BUF_PT (XBUFFER (buffer
)),
2781 XBUFFER (buffer
), Qlocal_map
);
2784 if (EQ (start1
, XBUFFER (buffer
)->keymap
))
2785 describe_map_tree (start1
, 1, shadow
, prefix
,
2786 "\f\nMajor Mode Bindings", nomenu
, 0, 0);
2788 describe_map_tree (start1
, 1, shadow
, prefix
,
2789 "\f\n`local-map' Property Bindings",
2792 shadow
= Fcons (start1
, shadow
);
2796 describe_map_tree (current_global_map
, 1, shadow
, prefix
,
2797 "\f\nGlobal Bindings", nomenu
, 0, 1);
2799 /* Print the function-key-map translations under this prefix. */
2800 if (!NILP (Vfunction_key_map
))
2801 describe_map_tree (Vfunction_key_map
, 0, Qnil
, prefix
,
2802 "\f\nFunction key map translations", nomenu
, 1, 0);
2808 /* Insert a description of the key bindings in STARTMAP,
2809 followed by those of all maps reachable through STARTMAP.
2810 If PARTIAL is nonzero, omit certain "uninteresting" commands
2811 (such as `undefined').
2812 If SHADOW is non-nil, it is a list of maps;
2813 don't mention keys which would be shadowed by any of them.
2814 PREFIX, if non-nil, says mention only keys that start with PREFIX.
2815 TITLE, if not 0, is a string to insert at the beginning.
2816 TITLE should not end with a colon or a newline; we supply that.
2817 If NOMENU is not 0, then omit menu-bar commands.
2819 If TRANSL is nonzero, the definitions are actually key translations
2820 so print strings and vectors differently.
2822 If ALWAYS_TITLE is nonzero, print the title even if there are no maps
2826 describe_map_tree (startmap
, partial
, shadow
, prefix
, title
, nomenu
, transl
,
2828 Lisp_Object startmap
, shadow
, prefix
;
2835 Lisp_Object maps
, orig_maps
, seen
, sub_shadows
;
2836 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2843 orig_maps
= maps
= Faccessible_keymaps (startmap
, prefix
);
2846 GCPRO3 (maps
, seen
, sub_shadows
);
2852 /* Delete from MAPS each element that is for the menu bar. */
2853 for (list
= maps
; !NILP (list
); list
= XCDR (list
))
2855 Lisp_Object elt
, prefix
, tem
;
2858 prefix
= Fcar (elt
);
2859 if (XVECTOR (prefix
)->size
>= 1)
2861 tem
= Faref (prefix
, make_number (0));
2862 if (EQ (tem
, Qmenu_bar
))
2863 maps
= Fdelq (elt
, maps
);
2868 if (!NILP (maps
) || always_title
)
2872 insert_string (title
);
2875 insert_string (" Starting With ");
2876 insert1 (Fkey_description (prefix
));
2878 insert_string (":\n");
2880 insert_string (key_heading
);
2884 for (; !NILP (maps
); maps
= Fcdr (maps
))
2886 register Lisp_Object elt
, prefix
, tail
;
2889 prefix
= Fcar (elt
);
2893 for (tail
= shadow
; CONSP (tail
); tail
= XCDR (tail
))
2897 shmap
= XCAR (tail
);
2899 /* If the sequence by which we reach this keymap is zero-length,
2900 then the shadow map for this keymap is just SHADOW. */
2901 if ((STRINGP (prefix
) && XSTRING (prefix
)->size
== 0)
2902 || (VECTORP (prefix
) && XVECTOR (prefix
)->size
== 0))
2904 /* If the sequence by which we reach this keymap actually has
2905 some elements, then the sequence's definition in SHADOW is
2906 what we should use. */
2909 shmap
= Flookup_key (shmap
, Fcar (elt
), Qt
);
2910 if (INTEGERP (shmap
))
2914 /* If shmap is not nil and not a keymap,
2915 it completely shadows this map, so don't
2916 describe this map at all. */
2917 if (!NILP (shmap
) && !KEYMAPP (shmap
))
2921 sub_shadows
= Fcons (shmap
, sub_shadows
);
2924 /* Maps we have already listed in this loop shadow this map. */
2925 for (tail
= orig_maps
; !EQ (tail
, maps
); tail
= XCDR (tail
))
2928 tem
= Fequal (Fcar (XCAR (tail
)), prefix
);
2930 sub_shadows
= Fcons (XCDR (XCAR (tail
)), sub_shadows
);
2933 describe_map (Fcdr (elt
), prefix
,
2934 transl
? describe_translation
: describe_command
,
2935 partial
, sub_shadows
, &seen
, nomenu
);
2941 insert_string ("\n");
2946 static int previous_description_column
;
2949 describe_command (definition
, args
)
2950 Lisp_Object definition
, args
;
2952 register Lisp_Object tem1
;
2953 int column
= current_column ();
2954 int description_column
;
2956 /* If column 16 is no good, go to col 32;
2957 but don't push beyond that--go to next line instead. */
2961 description_column
= 32;
2963 else if (column
> 14 || (column
> 10 && previous_description_column
== 32))
2964 description_column
= 32;
2966 description_column
= 16;
2968 Findent_to (make_number (description_column
), make_number (1));
2969 previous_description_column
= description_column
;
2971 if (SYMBOLP (definition
))
2973 XSETSTRING (tem1
, XSYMBOL (definition
)->name
);
2975 insert_string ("\n");
2977 else if (STRINGP (definition
) || VECTORP (definition
))
2978 insert_string ("Keyboard Macro\n");
2979 else if (KEYMAPP (definition
))
2980 insert_string ("Prefix Command\n");
2982 insert_string ("??\n");
2986 describe_translation (definition
, args
)
2987 Lisp_Object definition
, args
;
2989 register Lisp_Object tem1
;
2991 Findent_to (make_number (16), make_number (1));
2993 if (SYMBOLP (definition
))
2995 XSETSTRING (tem1
, XSYMBOL (definition
)->name
);
2997 insert_string ("\n");
2999 else if (STRINGP (definition
) || VECTORP (definition
))
3001 insert1 (Fkey_description (definition
));
3002 insert_string ("\n");
3004 else if (KEYMAPP (definition
))
3005 insert_string ("Prefix Command\n");
3007 insert_string ("??\n");
3010 /* Describe the contents of map MAP, assuming that this map itself is
3011 reached by the sequence of prefix keys KEYS (a string or vector).
3012 PARTIAL, SHADOW, NOMENU are as in `describe_map_tree' above. */
3015 describe_map (map
, keys
, elt_describer
, partial
, shadow
, seen
, nomenu
)
3016 register Lisp_Object map
;
3018 void (*elt_describer
) P_ ((Lisp_Object
, Lisp_Object
));
3024 Lisp_Object elt_prefix
;
3025 Lisp_Object tail
, definition
, event
;
3027 Lisp_Object suppress
;
3030 struct gcpro gcpro1
, gcpro2
, gcpro3
;
3034 if (!NILP (keys
) && XFASTINT (Flength (keys
)) > 0)
3036 /* Call Fkey_description first, to avoid GC bug for the other string. */
3037 tem
= Fkey_description (keys
);
3038 elt_prefix
= concat2 (tem
, build_string (" "));
3044 suppress
= intern ("suppress-keymap");
3046 /* This vector gets used to present single keys to Flookup_key. Since
3047 that is done once per keymap element, we don't want to cons up a
3048 fresh vector every time. */
3049 kludge
= Fmake_vector (make_number (1), Qnil
);
3052 GCPRO3 (elt_prefix
, definition
, kludge
);
3054 for (tail
= map
; CONSP (tail
); tail
= XCDR (tail
))
3058 if (VECTORP (XCAR (tail
))
3059 || CHAR_TABLE_P (XCAR (tail
)))
3060 describe_vector (XCAR (tail
),
3061 elt_prefix
, Qnil
, elt_describer
, partial
, shadow
, map
,
3063 else if (CONSP (XCAR (tail
)))
3065 event
= XCAR (XCAR (tail
));
3067 /* Ignore bindings whose "keys" are not really valid events.
3068 (We get these in the frames and buffers menu.) */
3069 if (!(SYMBOLP (event
) || INTEGERP (event
)))
3072 if (nomenu
&& EQ (event
, Qmenu_bar
))
3075 definition
= get_keyelt (XCDR (XCAR (tail
)), 0);
3077 /* Don't show undefined commands or suppressed commands. */
3078 if (NILP (definition
)) continue;
3079 if (SYMBOLP (definition
) && partial
)
3081 tem
= Fget (definition
, suppress
);
3086 /* Don't show a command that isn't really visible
3087 because a local definition of the same key shadows it. */
3089 ASET (kludge
, 0, event
);
3092 tem
= shadow_lookup (shadow
, kludge
, Qt
);
3093 if (!NILP (tem
)) continue;
3096 tem
= Flookup_key (map
, kludge
, Qt
);
3097 if (!EQ (tem
, definition
)) continue;
3101 previous_description_column
= 0;
3106 if (!NILP (elt_prefix
))
3107 insert1 (elt_prefix
);
3109 /* THIS gets the string to describe the character EVENT. */
3110 insert1 (Fsingle_key_description (event
, Qnil
));
3112 /* Print a description of the definition of this character.
3113 elt_describer will take care of spacing out far enough
3114 for alignment purposes. */
3115 (*elt_describer
) (definition
, Qnil
);
3117 else if (EQ (XCAR (tail
), Qkeymap
))
3119 /* The same keymap might be in the structure twice, if we're
3120 using an inherited keymap. So skip anything we've already
3122 tem
= Fassq (tail
, *seen
);
3123 if (CONSP (tem
) && !NILP (Fequal (XCAR (tem
), keys
)))
3125 *seen
= Fcons (Fcons (tail
, keys
), *seen
);
3133 describe_vector_princ (elt
, fun
)
3134 Lisp_Object elt
, fun
;
3136 Findent_to (make_number (16), make_number (1));
3141 DEFUN ("describe-vector", Fdescribe_vector
, Sdescribe_vector
, 1, 2, 0,
3142 doc
: /* Insert a description of contents of VECTOR.
3143 This is text showing the elements of vector matched against indices. */)
3145 Lisp_Object vector
, describer
;
3147 int count
= specpdl_ptr
- specpdl
;
3148 if (NILP (describer
))
3149 describer
= intern ("princ");
3150 specbind (Qstandard_output
, Fcurrent_buffer ());
3151 CHECK_VECTOR_OR_CHAR_TABLE (vector
);
3152 describe_vector (vector
, Qnil
, describer
, describe_vector_princ
, 0,
3153 Qnil
, Qnil
, (int *)0, 0);
3155 return unbind_to (count
, Qnil
);
3158 /* Insert in the current buffer a description of the contents of VECTOR.
3159 We call ELT_DESCRIBER to insert the description of one value found
3162 ELT_PREFIX describes what "comes before" the keys or indices defined
3163 by this vector. This is a human-readable string whose size
3164 is not necessarily related to the situation.
3166 If the vector is in a keymap, ELT_PREFIX is a prefix key which
3167 leads to this keymap.
3169 If the vector is a chartable, ELT_PREFIX is the vector
3170 of bytes that lead to the character set or portion of a character
3171 set described by this chartable.
3173 If PARTIAL is nonzero, it means do not mention suppressed commands
3174 (that assumes the vector is in a keymap).
3176 SHADOW is a list of keymaps that shadow this map.
3177 If it is non-nil, then we look up the key in those maps
3178 and we don't mention it now if it is defined by any of them.
3180 ENTIRE_MAP is the keymap in which this vector appears.
3181 If the definition in effect in the whole map does not match
3182 the one in this vector, we ignore this one.
3184 When describing a sub-char-table, INDICES is a list of
3185 indices at higher levels in this char-table,
3186 and CHAR_TABLE_DEPTH says how many levels down we have gone.
3188 ARGS is simply passed as the second argument to ELT_DESCRIBER. */
3191 describe_vector (vector
, elt_prefix
, args
, elt_describer
,
3192 partial
, shadow
, entire_map
,
3193 indices
, char_table_depth
)
3194 register Lisp_Object vector
;
3195 Lisp_Object elt_prefix
, args
;
3196 void (*elt_describer
) P_ ((Lisp_Object
, Lisp_Object
));
3199 Lisp_Object entire_map
;
3201 int char_table_depth
;
3203 Lisp_Object definition
;
3206 Lisp_Object suppress
;
3209 struct gcpro gcpro1
, gcpro2
, gcpro3
;
3210 /* Range of elements to be handled. */
3212 /* A flag to tell if a leaf in this level of char-table is not a
3213 generic character (i.e. a complete multibyte character). */
3221 indices
= (int *) alloca (3 * sizeof (int));
3225 /* This vector gets used to present single keys to Flookup_key. Since
3226 that is done once per vector element, we don't want to cons up a
3227 fresh vector every time. */
3228 kludge
= Fmake_vector (make_number (1), Qnil
);
3229 GCPRO3 (elt_prefix
, definition
, kludge
);
3232 suppress
= intern ("suppress-keymap");
3234 if (CHAR_TABLE_P (vector
))
3236 if (char_table_depth
== 0)
3238 /* VECTOR is a top level char-table. */
3241 to
= CHAR_TABLE_ORDINARY_SLOTS
;
3245 /* VECTOR is a sub char-table. */
3246 if (char_table_depth
>= 3)
3247 /* A char-table is never that deep. */
3248 error ("Too deep char table");
3251 = (CHARSET_VALID_P (indices
[0])
3252 && ((CHARSET_DIMENSION (indices
[0]) == 1
3253 && char_table_depth
== 1)
3254 || char_table_depth
== 2));
3256 /* Meaningful elements are from 32th to 127th. */
3258 to
= SUB_CHAR_TABLE_ORDINARY_SLOTS
;
3263 /* This does the right thing for ordinary vectors. */
3267 to
= XVECTOR (vector
)->size
;
3270 for (i
= from
; i
< to
; i
++)
3274 if (CHAR_TABLE_P (vector
))
3276 if (char_table_depth
== 0 && i
>= CHAR_TABLE_SINGLE_BYTE_SLOTS
)
3279 if (i
>= CHAR_TABLE_SINGLE_BYTE_SLOTS
3280 && !CHARSET_DEFINED_P (i
- 128))
3284 = get_keyelt (XCHAR_TABLE (vector
)->contents
[i
], 0);
3287 definition
= get_keyelt (AREF (vector
, i
), 0);
3289 if (NILP (definition
)) continue;
3291 /* Don't mention suppressed commands. */
3292 if (SYMBOLP (definition
) && partial
)
3296 tem
= Fget (definition
, suppress
);
3298 if (!NILP (tem
)) continue;
3301 /* Set CHARACTER to the character this entry describes, if any.
3302 Also update *INDICES. */
3303 if (CHAR_TABLE_P (vector
))
3305 indices
[char_table_depth
] = i
;
3307 if (char_table_depth
== 0)
3310 indices
[0] = i
- 128;
3312 else if (complete_char
)
3314 character
= MAKE_CHAR (indices
[0], indices
[1], indices
[2]);
3322 /* If this binding is shadowed by some other map, ignore it. */
3323 if (!NILP (shadow
) && complete_char
)
3327 ASET (kludge
, 0, make_number (character
));
3328 tem
= shadow_lookup (shadow
, kludge
, Qt
);
3330 if (!NILP (tem
)) continue;
3333 /* Ignore this definition if it is shadowed by an earlier
3334 one in the same keymap. */
3335 if (!NILP (entire_map
) && complete_char
)
3339 ASET (kludge
, 0, make_number (character
));
3340 tem
= Flookup_key (entire_map
, kludge
, Qt
);
3342 if (!EQ (tem
, definition
))
3348 if (char_table_depth
== 0)
3353 /* For a sub char-table, show the depth by indentation.
3354 CHAR_TABLE_DEPTH can be greater than 0 only for a char-table. */
3355 if (char_table_depth
> 0)
3356 insert (" ", char_table_depth
* 2); /* depth is 1 or 2. */
3358 /* Output the prefix that applies to every entry in this map. */
3359 if (!NILP (elt_prefix
))
3360 insert1 (elt_prefix
);
3362 /* Insert or describe the character this slot is for,
3363 or a description of what it is for. */
3364 if (SUB_CHAR_TABLE_P (vector
))
3367 insert_char (character
);
3370 /* We need an octal representation for this block of
3373 sprintf (work
, "(row %d)", i
);
3374 insert (work
, strlen (work
));
3377 else if (CHAR_TABLE_P (vector
))
3380 insert1 (Fsingle_key_description (make_number (character
), Qnil
));
3383 /* Print the information for this character set. */
3384 insert_string ("<");
3385 tem2
= CHARSET_TABLE_INFO (i
- 128, CHARSET_SHORT_NAME_IDX
);
3387 insert_from_string (tem2
, 0, 0, XSTRING (tem2
)->size
,
3388 STRING_BYTES (XSTRING (tem2
)), 0);
3396 insert1 (Fsingle_key_description (make_number (character
), Qnil
));
3399 /* If we find a sub char-table within a char-table,
3400 scan it recursively; it defines the details for
3401 a character set or a portion of a character set. */
3402 if (CHAR_TABLE_P (vector
) && SUB_CHAR_TABLE_P (definition
))
3405 describe_vector (definition
, elt_prefix
, args
, elt_describer
,
3406 partial
, shadow
, entire_map
,
3407 indices
, char_table_depth
+ 1);
3413 /* Find all consecutive characters or rows that have the same
3414 definition. But, for elements of a top level char table, if
3415 they are for charsets, we had better describe one by one even
3416 if they have the same definition. */
3417 if (CHAR_TABLE_P (vector
))
3421 if (char_table_depth
== 0)
3422 limit
= CHAR_TABLE_SINGLE_BYTE_SLOTS
;
3424 while (i
+ 1 < limit
3425 && (tem2
= get_keyelt (XCHAR_TABLE (vector
)->contents
[i
+ 1], 0),
3427 && !NILP (Fequal (tem2
, definition
)))
3432 && (tem2
= get_keyelt (AREF (vector
, i
+ 1), 0),
3434 && !NILP (Fequal (tem2
, definition
)))
3438 /* If we have a range of more than one character,
3439 print where the range reaches to. */
3441 if (i
!= starting_i
)
3445 if (!NILP (elt_prefix
))
3446 insert1 (elt_prefix
);
3448 if (CHAR_TABLE_P (vector
))
3450 if (char_table_depth
== 0)
3452 insert1 (Fsingle_key_description (make_number (i
), Qnil
));
3454 else if (complete_char
)
3456 indices
[char_table_depth
] = i
;
3457 character
= MAKE_CHAR (indices
[0], indices
[1], indices
[2]);
3458 insert_char (character
);
3462 /* We need an octal representation for this block of
3465 sprintf (work
, "(row %d)", i
);
3466 insert (work
, strlen (work
));
3471 insert1 (Fsingle_key_description (make_number (i
), Qnil
));
3475 /* Print a description of the definition of this character.
3476 elt_describer will take care of spacing out far enough
3477 for alignment purposes. */
3478 (*elt_describer
) (definition
, args
);
3481 /* For (sub) char-table, print `defalt' slot at last. */
3482 if (CHAR_TABLE_P (vector
) && !NILP (XCHAR_TABLE (vector
)->defalt
))
3484 insert (" ", char_table_depth
* 2);
3485 insert_string ("<<default>>");
3486 (*elt_describer
) (XCHAR_TABLE (vector
)->defalt
, args
);
3492 /* Apropos - finding all symbols whose names match a regexp. */
3493 Lisp_Object apropos_predicate
;
3494 Lisp_Object apropos_accumulate
;
3497 apropos_accum (symbol
, string
)
3498 Lisp_Object symbol
, string
;
3500 register Lisp_Object tem
;
3502 tem
= Fstring_match (string
, Fsymbol_name (symbol
), Qnil
);
3503 if (!NILP (tem
) && !NILP (apropos_predicate
))
3504 tem
= call1 (apropos_predicate
, symbol
);
3506 apropos_accumulate
= Fcons (symbol
, apropos_accumulate
);
3509 DEFUN ("apropos-internal", Fapropos_internal
, Sapropos_internal
, 1, 2, 0,
3510 doc
: /* Show all symbols whose names contain match for REGEXP.
3511 If optional 2nd arg PREDICATE is non-nil, (funcall PREDICATE SYMBOL) is done
3512 for each symbol and a symbol is mentioned only if that returns non-nil.
3513 Return list of symbols found. */)
3515 Lisp_Object regexp
, predicate
;
3517 struct gcpro gcpro1
, gcpro2
;
3518 CHECK_STRING (regexp
);
3519 apropos_predicate
= predicate
;
3520 GCPRO2 (apropos_predicate
, apropos_accumulate
);
3521 apropos_accumulate
= Qnil
;
3522 map_obarray (Vobarray
, apropos_accum
, regexp
);
3523 apropos_accumulate
= Fsort (apropos_accumulate
, Qstring_lessp
);
3525 return apropos_accumulate
;
3531 Qkeymap
= intern ("keymap");
3532 staticpro (&Qkeymap
);
3534 /* Now we are ready to set up this property, so we can
3535 create char tables. */
3536 Fput (Qkeymap
, Qchar_table_extra_slots
, make_number (0));
3538 /* Initialize the keymaps standardly used.
3539 Each one is the value of a Lisp variable, and is also
3540 pointed to by a C variable */
3542 global_map
= Fmake_keymap (Qnil
);
3543 Fset (intern ("global-map"), global_map
);
3545 current_global_map
= global_map
;
3546 staticpro (&global_map
);
3547 staticpro (¤t_global_map
);
3549 meta_map
= Fmake_keymap (Qnil
);
3550 Fset (intern ("esc-map"), meta_map
);
3551 Ffset (intern ("ESC-prefix"), meta_map
);
3553 control_x_map
= Fmake_keymap (Qnil
);
3554 Fset (intern ("ctl-x-map"), control_x_map
);
3555 Ffset (intern ("Control-X-prefix"), control_x_map
);
3558 = Fcons (Fcons (build_string ("DEL"), build_string ("\\d")),
3559 Fcons (Fcons (build_string ("TAB"), build_string ("\\t")),
3560 Fcons (Fcons (build_string ("RET"), build_string ("\\r")),
3561 Fcons (Fcons (build_string ("ESC"), build_string ("\\e")),
3562 Fcons (Fcons (build_string ("SPC"), build_string (" ")),
3564 staticpro (&exclude_keys
);
3566 DEFVAR_LISP ("define-key-rebound-commands", &Vdefine_key_rebound_commands
,
3567 doc
: /* List of commands given new key bindings recently.
3568 This is used for internal purposes during Emacs startup;
3569 don't alter it yourself. */);
3570 Vdefine_key_rebound_commands
= Qt
;
3572 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map
,
3573 doc
: /* Default keymap to use when reading from the minibuffer. */);
3574 Vminibuffer_local_map
= Fmake_sparse_keymap (Qnil
);
3576 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map
,
3577 doc
: /* Local keymap for the minibuffer when spaces are not allowed. */);
3578 Vminibuffer_local_ns_map
= Fmake_sparse_keymap (Qnil
);
3579 Fset_keymap_parent (Vminibuffer_local_ns_map
, Vminibuffer_local_map
);
3581 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map
,
3582 doc
: /* Local keymap for minibuffer input with completion. */);
3583 Vminibuffer_local_completion_map
= Fmake_sparse_keymap (Qnil
);
3584 Fset_keymap_parent (Vminibuffer_local_completion_map
, Vminibuffer_local_map
);
3586 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map
,
3587 doc
: /* Local keymap for minibuffer input with completion, for exact match. */);
3588 Vminibuffer_local_must_match_map
= Fmake_sparse_keymap (Qnil
);
3589 Fset_keymap_parent (Vminibuffer_local_must_match_map
,
3590 Vminibuffer_local_completion_map
);
3592 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist
,
3593 doc
: /* Alist of keymaps to use for minor modes.
3594 Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read
3595 key sequences and look up bindings iff VARIABLE's value is non-nil.
3596 If two active keymaps bind the same key, the keymap appearing earlier
3597 in the list takes precedence. */);
3598 Vminor_mode_map_alist
= Qnil
;
3600 DEFVAR_LISP ("minor-mode-overriding-map-alist", &Vminor_mode_overriding_map_alist
,
3601 doc
: /* Alist of keymaps to use for minor modes, in current major mode.
3602 This variable is a alist just like `minor-mode-map-alist', and it is
3603 used the same way (and before `minor-mode-map-alist'); however,
3604 it is provided for major modes to bind locally. */);
3605 Vminor_mode_overriding_map_alist
= Qnil
;
3607 DEFVAR_LISP ("function-key-map", &Vfunction_key_map
,
3608 doc
: /* Keymap mapping ASCII function key sequences onto their preferred forms.
3609 This allows Emacs to recognize function keys sent from ASCII
3610 terminals at any point in a key sequence.
3612 The `read-key-sequence' function replaces any subsequence bound by
3613 `function-key-map' with its binding. More precisely, when the active
3614 keymaps have no binding for the current key sequence but
3615 `function-key-map' binds a suffix of the sequence to a vector or string,
3616 `read-key-sequence' replaces the matching suffix with its binding, and
3617 continues with the new sequence.
3619 The events that come from bindings in `function-key-map' are not
3620 themselves looked up in `function-key-map'.
3622 For example, suppose `function-key-map' binds `ESC O P' to [f1].
3623 Typing `ESC O P' to `read-key-sequence' would return [f1]. Typing
3624 `C-x ESC O P' would return [?\\C-x f1]. If [f1] were a prefix
3625 key, typing `ESC O P x' would return [f1 x]. */);
3626 Vfunction_key_map
= Fmake_sparse_keymap (Qnil
);
3628 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map
,
3629 doc
: /* Keymap of key translations that can override keymaps.
3630 This keymap works like `function-key-map', but comes after that,
3631 and applies even for keys that have ordinary bindings. */);
3632 Vkey_translation_map
= Qnil
;
3634 Qsingle_key_description
= intern ("single-key-description");
3635 staticpro (&Qsingle_key_description
);
3637 Qkey_description
= intern ("key-description");
3638 staticpro (&Qkey_description
);
3640 Qkeymapp
= intern ("keymapp");
3641 staticpro (&Qkeymapp
);
3643 Qnon_ascii
= intern ("non-ascii");
3644 staticpro (&Qnon_ascii
);
3646 Qmenu_item
= intern ("menu-item");
3647 staticpro (&Qmenu_item
);
3649 where_is_cache_keymaps
= Qt
;
3650 where_is_cache
= Qnil
;
3651 staticpro (&where_is_cache
);
3652 staticpro (&where_is_cache_keymaps
);
3654 defsubr (&Skeymapp
);
3655 defsubr (&Skeymap_parent
);
3656 defsubr (&Skeymap_prompt
);
3657 defsubr (&Sset_keymap_parent
);
3658 defsubr (&Smake_keymap
);
3659 defsubr (&Smake_sparse_keymap
);
3660 defsubr (&Scopy_keymap
);
3661 defsubr (&Skey_binding
);
3662 defsubr (&Slocal_key_binding
);
3663 defsubr (&Sglobal_key_binding
);
3664 defsubr (&Sminor_mode_key_binding
);
3665 defsubr (&Sdefine_key
);
3666 defsubr (&Slookup_key
);
3667 defsubr (&Sdefine_prefix_command
);
3668 defsubr (&Suse_global_map
);
3669 defsubr (&Suse_local_map
);
3670 defsubr (&Scurrent_local_map
);
3671 defsubr (&Scurrent_global_map
);
3672 defsubr (&Scurrent_minor_mode_maps
);
3673 defsubr (&Scurrent_active_maps
);
3674 defsubr (&Saccessible_keymaps
);
3675 defsubr (&Skey_description
);
3676 defsubr (&Sdescribe_vector
);
3677 defsubr (&Ssingle_key_description
);
3678 defsubr (&Stext_char_description
);
3679 defsubr (&Swhere_is_internal
);
3680 defsubr (&Sdescribe_buffer_bindings
);
3681 defsubr (&Sapropos_internal
);
3687 initial_define_key (global_map
, 033, "ESC-prefix");
3688 initial_define_key (global_map
, Ctl('X'), "Control-X-prefix");