1 /* Manipulation of keymaps
2 Copyright (C) 1985, 1986, 1987, 1988, 1992 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
29 #define min(a, b) ((a) < (b) ? (a) : (b))
31 /* The number of elements in keymap vectors. */
32 #define DENSE_TABLE_SIZE (0200)
34 /* Actually allocate storage for these variables */
36 Lisp_Object current_global_map
; /* Current global keymap */
38 Lisp_Object global_map
; /* default global key bindings */
40 Lisp_Object meta_map
; /* The keymap used for globally bound
41 ESC-prefixed default commands */
43 Lisp_Object control_x_map
; /* The keymap used for globally bound
44 C-x-prefixed default commands */
46 /* was MinibufLocalMap */
47 Lisp_Object Vminibuffer_local_map
;
48 /* The keymap used by the minibuf for local
49 bindings when spaces are allowed in the
52 /* was MinibufLocalNSMap */
53 Lisp_Object Vminibuffer_local_ns_map
;
54 /* The keymap used by the minibuf for local
55 bindings when spaces are not encouraged
58 /* keymap used for minibuffers when doing completion */
59 /* was MinibufLocalCompletionMap */
60 Lisp_Object Vminibuffer_local_completion_map
;
62 /* keymap used for minibuffers when doing completion and require a match */
63 /* was MinibufLocalMustMatchMap */
64 Lisp_Object Vminibuffer_local_must_match_map
;
66 /* Alist of minor mode variables and keymaps. */
67 Lisp_Object Vminor_mode_map_alist
;
69 /* Keymap mapping ASCII function key sequences onto their preferred forms.
70 Initialized by the terminal-specific lisp files. See DEFVAR for more
72 Lisp_Object Vfunction_key_map
;
74 Lisp_Object Qkeymapp
, Qkeymap
;
76 /* A char over 0200 in a key sequence
77 is equivalent to prefixing with this character. */
79 extern Lisp_Object meta_prefix_char
;
81 void describe_map_tree ();
82 static Lisp_Object
describe_buffer_bindings ();
83 static void describe_command ();
84 static void describe_map ();
85 static void describe_map_2 ();
87 /* Keymap object support - constructors and predicates. */
89 DEFUN ("make-keymap", Fmake_keymap
, Smake_keymap
, 0, 1, 0,
90 "Construct and return a new keymap, of the form (keymap VECTOR . ALIST).\n\
91 VECTOR is a 128-element vector which holds the bindings for the ASCII\n\
92 characters. ALIST is an assoc-list which holds bindings for function keys,\n\
93 mouse events, and any other things that appear in the input stream.\n\
94 All entries in it are initially nil, meaning \"command undefined\".\n\n\
95 The optional arg STRING supplies a menu name for the keymap\n\
96 in case you use it as a menu with `x-popup-menu'.")
102 tail
= Fcons (string
, Qnil
);
105 return Fcons (Qkeymap
,
106 Fcons (Fmake_vector (make_number (DENSE_TABLE_SIZE
), Qnil
),
110 DEFUN ("make-sparse-keymap", Fmake_sparse_keymap
, Smake_sparse_keymap
, 0, 1, 0,
111 "Construct and return a new sparse-keymap list.\n\
112 Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),\n\
113 which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),\n\
114 which binds the function key or mouse event SYMBOL to DEFINITION.\n\
115 Initially the alist is nil.\n\n\
116 The optional arg STRING supplies a menu name for the keymap\n\
117 in case you use it as a menu with `x-popup-menu'.")
122 return Fcons (Qkeymap
, Fcons (string
, Qnil
));
123 return Fcons (Qkeymap
, Qnil
);
126 /* This function is used for installing the standard key bindings
127 at initialization time.
131 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); */
134 initial_define_key (keymap
, key
, defname
)
139 store_in_keymap (keymap
, make_number (key
), intern (defname
));
143 initial_define_lispy_key (keymap
, keyname
, defname
)
148 store_in_keymap (keymap
, intern (keyname
), intern (defname
));
151 /* Define character fromchar in map frommap as an alias for character
152 tochar in map tomap. Subsequent redefinitions of the latter WILL
153 affect the former. */
157 synkey (frommap
, fromchar
, tomap
, tochar
)
158 struct Lisp_Vector
*frommap
, *tomap
;
159 int fromchar
, tochar
;
162 XSET (v
, Lisp_Vector
, tomap
);
163 XFASTINT (c
) = tochar
;
164 frommap
->contents
[fromchar
] = Fcons (v
, c
);
168 DEFUN ("keymapp", Fkeymapp
, Skeymapp
, 1, 1, 0,
169 "Return t if ARG is a keymap.\n\
171 A keymap is list (keymap . ALIST), a list (keymap VECTOR . ALIST),\n\
172 or a symbol whose function definition is a keymap is itself a keymap.\n\
173 ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);\n\
174 VECTOR is a 128-element vector of bindings for ASCII characters.")
178 return (NILP (get_keymap_1 (object
, 0, 0)) ? Qnil
: Qt
);
181 /* Check that OBJECT is a keymap (after dereferencing through any
182 symbols). If it is, return it.
184 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
185 is an autoload form, do the autoload and try again.
187 ERROR controls how we respond if OBJECT isn't a keymap.
188 If ERROR is non-zero, signal an error; otherwise, just return Qnil.
190 Note that most of the time, we don't want to pursue autoloads.
191 Functions like Faccessible_keymaps which scan entire keymap trees
192 shouldn't load every autoloaded keymap. I'm not sure about this,
193 but it seems to me that only read_key_sequence, Flookup_key, and
194 Fdefine_key should cause keymaps to be autoloaded. */
197 get_keymap_1 (object
, error
, autoload
)
204 tem
= indirect_function (object
);
205 if (CONSP (tem
) && EQ (XCONS (tem
)->car
, Qkeymap
))
208 /* Should we do an autoload? */
210 && XTYPE (object
) == Lisp_Symbol
212 && EQ (XCONS (tem
)->car
, Qautoload
))
214 struct gcpro gcpro1
, gcpro2
;
217 do_autoload (tem
, object
);
224 wrong_type_argument (Qkeymapp
, object
);
230 /* Follow any symbol chaining, and return the keymap denoted by OBJECT.
231 If OBJECT doesn't denote a keymap at all, signal an error. */
236 return get_keymap_1 (object
, 0, 0);
240 /* Look up IDX in MAP. IDX may be any sort of event.
241 Note that this does only one level of lookup; IDX must be a single
242 event, not a sequence.
244 If T_OK is non-zero, bindings for Qt are treated as default
245 bindings; any key left unmentioned by other tables and bindings is
246 given the binding of Qt.
248 If T_OK is zero, bindings for Qt are not treated specially. */
251 access_keymap (map
, idx
, t_ok
)
256 /* If idx is a list (some sort of mouse click, perhaps?),
257 the index we want to use is the car of the list, which
258 ought to be a symbol. */
259 idx
= EVENT_HEAD (idx
);
261 if (XTYPE (idx
) == Lisp_Int
262 && (XINT (idx
) < 0 || XINT (idx
) >= DENSE_TABLE_SIZE
))
263 error ("only ASCII characters may be looked up in keymaps");
265 /* If idx is a symbol, it might have modifiers, which need to
266 be put in the canonical order. */
267 else if (XTYPE (idx
) == Lisp_Symbol
)
268 idx
= reorder_modifiers (idx
);
272 Lisp_Object t_binding
= Qnil
;
274 for (tail
= map
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
276 Lisp_Object binding
= XCONS (tail
)->car
;
278 switch (XTYPE (binding
))
281 if (EQ (XCONS (binding
)->car
, idx
))
282 return XCONS (binding
)->cdr
;
283 if (t_ok
&& EQ (XCONS (binding
)->car
, Qt
))
284 t_binding
= XCONS (binding
)->cdr
;
288 if (XVECTOR (binding
)->size
== DENSE_TABLE_SIZE
289 && XTYPE (idx
) == Lisp_Int
)
290 return XVECTOR (binding
)->contents
[XINT (idx
)];
301 /* Given OBJECT which was found in a slot in a keymap,
302 trace indirect definitions to get the actual definition of that slot.
303 An indirect definition is a list of the form
304 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
305 and INDEX is the object to look up in KEYMAP to yield the definition.
307 Also if OBJECT has a menu string as the first element,
308 remove that. Also remove a menu help string as second element. */
312 register Lisp_Object object
;
316 register Lisp_Object map
, tem
;
318 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
319 map
= get_keymap_1 (Fcar_safe (object
), 0, 0);
320 tem
= Fkeymapp (map
);
322 object
= access_keymap (map
, Fcdr (object
), 0);
324 /* If the keymap contents looks like (STRING . DEFN),
326 Keymap alist elements like (CHAR MENUSTRING . DEFN)
327 will be used by HierarKey menus. */
328 else if (XTYPE (object
) == Lisp_Cons
329 && XTYPE (XCONS (object
)->car
) == Lisp_String
)
331 object
= XCONS (object
)->cdr
;
332 /* Also remove a menu help string, if any,
333 following the menu item name. */
334 if (XTYPE (object
) == Lisp_Cons
335 && XTYPE (XCONS (object
)->car
) == Lisp_String
)
336 object
= XCONS (object
)->cdr
;
340 /* Anything else is really the value. */
346 store_in_keymap (keymap
, idx
, def
)
348 register Lisp_Object idx
;
349 register Lisp_Object def
;
351 if (XTYPE (keymap
) != Lisp_Cons
352 || ! EQ (XCONS (keymap
)->car
, Qkeymap
))
353 error ("attempt to define a key in a non-keymap");
355 /* If idx is a list (some sort of mouse click, perhaps?),
356 the index we want to use is the car of the list, which
357 ought to be a symbol. */
358 idx
= EVENT_HEAD (idx
);
360 if (XTYPE (idx
) == Lisp_Int
361 && (XINT (idx
) < 0 || XINT (idx
) >= DENSE_TABLE_SIZE
))
362 error ("only ASCII characters may be used as keymap indices");
364 /* If idx is a symbol, it might have modifiers, which need to
365 be put in the canonical order. */
366 else if (XTYPE (idx
) == Lisp_Symbol
)
367 idx
= reorder_modifiers (idx
);
370 /* Scan the keymap for a binding of idx. */
374 /* The cons after which we should insert new bindings. If the
375 keymap has a table element, we record its position here, so new
376 bindings will go after it; this way, the table will stay
377 towards the front of the alist and character lookups in dense
378 keymaps will remain fast. Otherwise, this just points at the
379 front of the keymap. */
380 Lisp_Object insertion_point
= keymap
;
382 for (tail
= XCONS (keymap
)->cdr
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
384 Lisp_Object elt
= XCONS (tail
)->car
;
389 if (XVECTOR (elt
)->size
!= DENSE_TABLE_SIZE
)
391 if (XTYPE (idx
) == Lisp_Int
)
393 XVECTOR (elt
)->contents
[XFASTINT (idx
)] = def
;
396 insertion_point
= tail
;
400 if (EQ (idx
, XCONS (elt
)->car
))
402 XCONS (elt
)->cdr
= def
;
408 /* If we find a 'keymap' symbol in the spine of KEYMAP,
409 then we must have found the start of a second keymap
410 being used as the tail of KEYMAP, and a binding for IDX
411 should be inserted before it. */
412 if (EQ (elt
, Qkeymap
))
421 /* We have scanned the entire keymap, and not found a binding for
422 IDX. Let's add one. */
423 XCONS (insertion_point
)->cdr
=
424 Fcons (Fcons (idx
, def
), XCONS (insertion_point
)->cdr
);
431 DEFUN ("copy-keymap", Fcopy_keymap
, Scopy_keymap
, 1, 1, 0,
432 "Return a copy of the keymap KEYMAP.\n\
433 The copy starts out with the same definitions of KEYMAP,\n\
434 but changing either the copy or KEYMAP does not affect the other.\n\
435 Any key definitions that are subkeymaps are recursively copied.\n\
436 However, a key definition which is a symbol whose definition is a keymap\n\
441 register Lisp_Object copy
, tail
;
443 copy
= Fcopy_alist (get_keymap (keymap
));
445 for (tail
= copy
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
447 Lisp_Object elt
= XCONS (tail
)->car
;
449 if (XTYPE (elt
) == Lisp_Vector
450 && XVECTOR (elt
)->size
== DENSE_TABLE_SIZE
)
454 elt
= Fcopy_sequence (elt
);
455 XCONS (tail
)->car
= elt
;
457 for (i
= 0; i
< DENSE_TABLE_SIZE
; i
++)
458 if (XTYPE (XVECTOR (elt
)->contents
[i
]) != Lisp_Symbol
459 && Fkeymapp (XVECTOR (elt
)->contents
[i
]))
460 XVECTOR (elt
)->contents
[i
] =
461 Fcopy_keymap (XVECTOR (elt
)->contents
[i
]);
464 && XTYPE (XCONS (elt
)->cdr
) != Lisp_Symbol
465 && ! NILP (Fkeymapp (XCONS (elt
)->cdr
)))
466 XCONS (elt
)->cdr
= Fcopy_keymap (XCONS (elt
)->cdr
);
472 /* Simple Keymap mutators and accessors. */
474 DEFUN ("define-key", Fdefine_key
, Sdefine_key
, 3, 3, 0,
475 "Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.\n\
476 KEYMAP is a keymap. KEY is a string or a vector of symbols and characters\n\
477 meaning a sequence of keystrokes and events.\n\
478 DEF is anything that can be a key's definition:\n\
479 nil (means key is undefined in this keymap),\n\
480 a command (a Lisp function suitable for interactive calling)\n\
481 a string (treated as a keyboard macro),\n\
482 a keymap (to define a prefix key),\n\
483 a symbol. When the key is looked up, the symbol will stand for its\n\
484 function definition, which should at that time be one of the above,\n\
485 or another symbol whose function definition is used, etc.\n\
486 a cons (STRING . DEFN), meaning that DEFN is the definition\n\
487 (DEFN should be a valid definition in its own right),\n\
488 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.\n\
490 If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at\n\
491 the front of KEYMAP.")
498 register Lisp_Object c
;
499 register Lisp_Object tem
;
500 register Lisp_Object cmd
;
503 struct gcpro gcpro1
, gcpro2
, gcpro3
;
505 keymap
= get_keymap (keymap
);
507 if (XTYPE (key
) != Lisp_Vector
508 && XTYPE (key
) != Lisp_String
)
509 key
= wrong_type_argument (Qarrayp
, key
);
511 length
= XFASTINT (Flength (key
));
515 GCPRO3 (keymap
, key
, def
);
520 c
= Faref (key
, make_number (idx
));
522 if (XTYPE (c
) == Lisp_Int
526 c
= meta_prefix_char
;
531 if (XTYPE (c
) == Lisp_Int
)
532 XSETINT (c
, XINT (c
) & 0177);
539 RETURN_UNGCPRO (store_in_keymap (keymap
, c
, def
));
541 cmd
= get_keyelt (access_keymap (keymap
, c
, 0));
545 cmd
= Fmake_sparse_keymap (Qnil
);
546 store_in_keymap (keymap
, c
, cmd
);
549 keymap
= get_keymap_1 (cmd
, 0, 1);
551 error ("Key sequence %s uses invalid prefix characters",
552 XSTRING (key
)->data
);
556 /* Value is number if KEY is too long; NIL if valid but has no definition. */
558 DEFUN ("lookup-key", Flookup_key
, Slookup_key
, 2, 2, 0,
559 "In keymap KEYMAP, look up key sequence KEY. Return the definition.\n\
560 nil means undefined. See doc of `define-key' for kinds of definitions.\n\
561 A number as value means KEY is \"too long\";\n\
562 that is, characters or symbols in it except for the last one\n\
563 fail to be a valid sequence of prefix characters in KEYMAP.\n\
564 The number is how many characters at the front of KEY\n\
565 it takes to reach a non-prefix command.")
567 register Lisp_Object keymap
;
571 register Lisp_Object tem
;
572 register Lisp_Object cmd
;
573 register Lisp_Object c
;
577 keymap
= get_keymap (keymap
);
579 if (XTYPE (key
) != Lisp_Vector
580 && XTYPE (key
) != Lisp_String
)
581 key
= wrong_type_argument (Qarrayp
, key
);
583 length
= XFASTINT (Flength (key
));
590 c
= Faref (key
, make_number (idx
));
592 if (XTYPE (c
) == Lisp_Int
596 c
= meta_prefix_char
;
601 if (XTYPE (c
) == Lisp_Int
)
602 XSETINT (c
, XINT (c
) & 0177);
608 cmd
= get_keyelt (access_keymap (keymap
, c
, 0));
612 keymap
= get_keymap_1 (cmd
, 0, 0);
614 return make_number (idx
);
620 /* Append a key to the end of a key sequence. If key_sequence is a
621 string and key is a character, the result will be another string;
622 otherwise, it will be a vector. */
624 append_key (key_sequence
, key
)
625 Lisp_Object key_sequence
, key
;
629 args
[0] = key_sequence
;
631 if (XTYPE (key_sequence
) == Lisp_String
632 && XTYPE (key
) == Lisp_Int
)
634 args
[1] = Fchar_to_string (key
);
635 return Fconcat (2, args
);
639 args
[1] = Fcons (key
, Qnil
);
640 return Fvconcat (2, args
);
645 /* Global, local, and minor mode keymap stuff. */
647 /* We can't put these variables inside current_minor_maps, since under
648 some systems, static gets macro-defined to be the empty string.
650 static Lisp_Object
*cmm_modes
, *cmm_maps
;
653 /* Store a pointer to an array of the keymaps of the currently active
654 minor modes in *buf, and return the number of maps it contains.
656 This function always returns a pointer to the same buffer, and may
657 free or reallocate it, so if you want to keep it for a long time or
658 hand it out to lisp code, copy it. This procedure will be called
659 for every key sequence read, so the nice lispy approach (return a
660 new assoclist, list, what have you) for each invocation would
661 result in a lot of consing over time.
663 If we used xrealloc/xmalloc and ran out of memory, they would throw
664 back to the command loop, which would try to read a key sequence,
665 which would call this function again, resulting in an infinite
666 loop. Instead, we'll use realloc/malloc and silently truncate the
667 list, let the key sequence be read, and hope some other piece of
668 code signals the error. */
670 current_minor_maps (modeptr
, mapptr
)
671 Lisp_Object
**modeptr
, **mapptr
;
674 Lisp_Object alist
, assoc
, var
, val
;
676 for (alist
= Vminor_mode_map_alist
;
678 alist
= XCONS (alist
)->cdr
)
679 if (CONSP (assoc
= XCONS (alist
)->car
)
680 && XTYPE (var
= XCONS (assoc
)->car
) == Lisp_Symbol
681 && ! EQ ((val
= find_symbol_value (var
)), Qunbound
)
686 Lisp_Object
*newmodes
, *newmaps
;
690 newmodes
= (Lisp_Object
*) realloc (cmm_modes
, cmm_size
*= 2);
691 newmaps
= (Lisp_Object
*) realloc (cmm_maps
, cmm_size
);
695 newmodes
= (Lisp_Object
*) malloc (cmm_size
= 30);
696 newmaps
= (Lisp_Object
*) malloc (cmm_size
);
699 if (newmaps
&& newmodes
)
701 cmm_modes
= newmodes
;
708 cmm_maps
[i
] = XCONS (assoc
)->cdr
;
712 if (modeptr
) *modeptr
= cmm_modes
;
713 if (mapptr
) *mapptr
= cmm_maps
;
717 DEFUN ("key-binding", Fkey_binding
, Skey_binding
, 1, 1, 0,
718 "Return the binding for command KEY in current keymaps.\n\
719 KEY is a string, a sequence of keystrokes.\n\
720 The binding is probably a symbol with a function definition.")
724 Lisp_Object
*maps
, value
;
727 nmaps
= current_minor_maps (0, &maps
);
728 for (i
= 0; i
< nmaps
; i
++)
729 if (! NILP (maps
[i
]))
731 value
= Flookup_key (maps
[i
], key
);
732 if (! NILP (value
) && XTYPE (value
) != Lisp_Int
)
736 if (! NILP (current_buffer
->keymap
))
738 value
= Flookup_key (current_buffer
->keymap
, key
);
739 if (! NILP (value
) && XTYPE (value
) != Lisp_Int
)
743 value
= Flookup_key (current_global_map
, key
);
744 if (! NILP (value
) && XTYPE (value
) != Lisp_Int
)
750 DEFUN ("local-key-binding", Flocal_key_binding
, Slocal_key_binding
, 1, 1, 0,
751 "Return the binding for command KEYS in current local keymap only.\n\
752 KEYS is a string, a sequence of keystrokes.\n\
753 The binding is probably a symbol with a function definition.")
757 register Lisp_Object map
;
758 map
= current_buffer
->keymap
;
761 return Flookup_key (map
, keys
);
764 DEFUN ("global-key-binding", Fglobal_key_binding
, Sglobal_key_binding
, 1, 1, 0,
765 "Return the binding for command KEYS in current global keymap only.\n\
766 KEYS is a string, a sequence of keystrokes.\n\
767 The binding is probably a symbol with a function definition.\n\
768 This function's return values are the same as those of lookup-key\n\
773 return Flookup_key (current_global_map
, keys
);
776 DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding
, Sminor_mode_key_binding
, 1, 1, 0,
777 "Find the visible minor mode bindings of KEY.\n\
778 Return an alist of pairs (MODENAME . BINDING), where MODENAME is the\n\
779 the symbol which names the minor mode binding KEY, and BINDING is\n\
780 KEY's definition in that mode. In particular, if KEY has no\n\
781 minor-mode bindings, return nil. If the first binding is a\n\
782 non-prefix, all subsequent bindings will be omitted, since they would\n\
783 be ignored. Similarly, the list doesn't include non-prefix bindings\n\
784 that come after prefix bindings.")
787 Lisp_Object
*modes
, *maps
;
792 nmaps
= current_minor_maps (&modes
, &maps
);
794 for (i
= j
= 0; i
< nmaps
; i
++)
796 && ! NILP (binding
= Flookup_key (maps
[i
], key
))
797 && XTYPE (binding
) != Lisp_Int
)
799 if (! NILP (get_keymap (binding
)))
800 maps
[j
++] = Fcons (modes
[i
], binding
);
802 return Fcons (Fcons (modes
[i
], binding
), Qnil
);
805 return Flist (j
, maps
);
808 DEFUN ("global-set-key", Fglobal_set_key
, Sglobal_set_key
, 2, 2,
809 "kSet key globally: \nCSet key %s to command: ",
810 "Give KEY a global binding as COMMAND.\n\
811 COMMAND is a symbol naming an interactively-callable function.\n\
812 KEY is a string representing a sequence of keystrokes.\n\
813 Note that if KEY has a local binding in the current buffer\n\
814 that local binding will continue to shadow any global binding.")
816 Lisp_Object keys
, function
;
818 if (XTYPE (keys
) != Lisp_Vector
819 && XTYPE (keys
) != Lisp_String
)
820 keys
= wrong_type_argument (Qarrayp
, keys
);
822 Fdefine_key (current_global_map
, keys
, function
);
826 DEFUN ("local-set-key", Flocal_set_key
, Slocal_set_key
, 2, 2,
827 "kSet key locally: \nCSet key %s locally to command: ",
828 "Give KEY a local binding as COMMAND.\n\
829 COMMAND is a symbol naming an interactively-callable function.\n\
830 KEY is a string representing a sequence of keystrokes.\n\
831 The binding goes in the current buffer's local map,\n\
832 which is shared with other buffers in the same major mode.")
834 Lisp_Object keys
, function
;
836 register Lisp_Object map
;
837 map
= current_buffer
->keymap
;
840 map
= Fmake_sparse_keymap (Qnil
);
841 current_buffer
->keymap
= map
;
844 if (XTYPE (keys
) != Lisp_Vector
845 && XTYPE (keys
) != Lisp_String
)
846 keys
= wrong_type_argument (Qarrayp
, keys
);
848 Fdefine_key (map
, keys
, function
);
852 DEFUN ("global-unset-key", Fglobal_unset_key
, Sglobal_unset_key
,
853 1, 1, "kUnset key globally: ",
854 "Remove global binding of KEY.\n\
855 KEY is a string representing a sequence of keystrokes.")
859 return Fglobal_set_key (keys
, Qnil
);
862 DEFUN ("local-unset-key", Flocal_unset_key
, Slocal_unset_key
, 1, 1,
863 "kUnset key locally: ",
864 "Remove local binding of KEY.\n\
865 KEY is a string representing a sequence of keystrokes.")
869 if (!NILP (current_buffer
->keymap
))
870 Flocal_set_key (keys
, Qnil
);
874 DEFUN ("define-prefix-command", Fdefine_prefix_command
, Sdefine_prefix_command
, 1, 2, 0,
875 "Define COMMAND as a prefix command.\n\
876 A new sparse keymap is stored as COMMAND's function definition and its value.\n\
877 If a second optional argument MAPVAR is given, the map is stored as\n\
878 its value instead of as COMMAND's value; but COMMAND is still defined\n\
881 Lisp_Object name
, mapvar
;
884 map
= Fmake_sparse_keymap (Qnil
);
893 DEFUN ("use-global-map", Fuse_global_map
, Suse_global_map
, 1, 1, 0,
894 "Select KEYMAP as the global keymap.")
898 keymap
= get_keymap (keymap
);
899 current_global_map
= keymap
;
903 DEFUN ("use-local-map", Fuse_local_map
, Suse_local_map
, 1, 1, 0,
904 "Select KEYMAP as the local keymap.\n\
905 If KEYMAP is nil, that means no local keymap.")
910 keymap
= get_keymap (keymap
);
912 current_buffer
->keymap
= keymap
;
917 DEFUN ("current-local-map", Fcurrent_local_map
, Scurrent_local_map
, 0, 0, 0,
918 "Return current buffer's local keymap, or nil if it has none.")
921 return current_buffer
->keymap
;
924 DEFUN ("current-global-map", Fcurrent_global_map
, Scurrent_global_map
, 0, 0, 0,
925 "Return the current global keymap.")
928 return current_global_map
;
931 DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps
, Scurrent_minor_mode_maps
, 0, 0, 0,
932 "Return a list of keymaps for the minor modes of the current buffer.")
936 int nmaps
= current_minor_maps (0, &maps
);
938 return Flist (nmaps
, maps
);
941 /* Help functions for describing and documenting keymaps. */
943 DEFUN ("accessible-keymaps", Faccessible_keymaps
, Saccessible_keymaps
,
945 "Find all keymaps accessible via prefix characters from KEYMAP.\n\
946 Returns a list of elements of the form (KEYS . MAP), where the sequence\n\
947 KEYS starting from KEYMAP gets you to MAP. These elements are ordered\n\
948 so that the KEYS increase in length. The first element is (\"\" . KEYMAP).")
950 Lisp_Object startmap
;
952 Lisp_Object maps
, tail
;
954 maps
= Fcons (Fcons (build_string (""), get_keymap (startmap
)), Qnil
);
956 /* For each map in the list maps,
957 look at any other maps it points to,
958 and stick them at the end if they are not already in the list.
960 This is a breadth-first traversal, where tail is the queue of
961 nodes, and maps accumulates a list of all nodes visited. */
963 for (tail
= maps
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
965 register Lisp_Object thisseq
= Fcar (Fcar (tail
));
966 register Lisp_Object thismap
= Fcdr (Fcar (tail
));
967 Lisp_Object last
= make_number (XINT (Flength (thisseq
)) - 1);
969 /* Does the current sequence end in the meta-prefix-char? */
970 int is_metized
= (XINT (last
) >= 0
971 && EQ (Faref (thisseq
, last
), meta_prefix_char
));
973 for (; CONSP (thismap
); thismap
= XCONS (thismap
)->cdr
)
975 Lisp_Object elt
= XCONS (thismap
)->car
;
979 if (XTYPE (elt
) == Lisp_Vector
)
983 /* Vector keymap. Scan all the elements. */
984 for (i
= 0; i
< DENSE_TABLE_SIZE
; i
++)
986 register Lisp_Object tem
;
987 register Lisp_Object cmd
;
989 cmd
= get_keyelt (XVECTOR (elt
)->contents
[i
]);
990 if (NILP (cmd
)) continue;
991 tem
= Fkeymapp (cmd
);
994 cmd
= get_keymap (cmd
);
995 /* Ignore keymaps that are already added to maps. */
996 tem
= Frassq (cmd
, maps
);
999 /* If the last key in thisseq is meta-prefix-char,
1000 turn it into a meta-ized keystroke. We know
1001 that the event we're about to append is an
1002 ascii keystroke since we're processing a
1006 tem
= Fcopy_sequence (thisseq
);
1007 Faset (tem
, last
, make_number (i
| 0200));
1009 /* This new sequence is the same length as
1010 thisseq, so stick it in the list right
1013 Fcons (Fcons (tem
, cmd
), XCONS (tail
)->cdr
);
1017 tem
= append_key (thisseq
, make_number (i
));
1018 nconc2 (tail
, Fcons (Fcons (tem
, cmd
), Qnil
));
1024 else if (CONSP (elt
))
1026 register Lisp_Object cmd
= get_keyelt (XCONS (elt
)->cdr
);
1027 register Lisp_Object tem
;
1029 /* Ignore definitions that aren't keymaps themselves. */
1030 tem
= Fkeymapp (cmd
);
1033 /* Ignore keymaps that have been seen already. */
1034 cmd
= get_keymap (cmd
);
1035 tem
= Frassq (cmd
, maps
);
1038 /* let elt be the event defined by this map entry. */
1039 elt
= XCONS (elt
)->car
;
1041 /* If the last key in thisseq is meta-prefix-char, and
1042 this entry is a binding for an ascii keystroke,
1043 turn it into a meta-ized keystroke. */
1044 if (is_metized
&& XTYPE (elt
) == Lisp_Int
)
1046 tem
= Fcopy_sequence (thisseq
);
1047 Faset (tem
, last
, make_number (XINT (elt
) | 0200));
1049 /* This new sequence is the same length as
1050 thisseq, so stick it in the list right
1053 Fcons (Fcons (tem
, cmd
), XCONS (tail
)->cdr
);
1057 Fcons (Fcons (append_key (thisseq
, elt
), cmd
),
1068 Lisp_Object Qsingle_key_description
, Qkey_description
;
1070 DEFUN ("key-description", Fkey_description
, Skey_description
, 1, 1, 0,
1071 "Return a pretty description of key-sequence KEYS.\n\
1072 Control characters turn into \"C-foo\" sequences, meta into \"M-foo\"\n\
1073 spaces are put between sequence elements, etc.")
1077 return Fmapconcat (Qsingle_key_description
, keys
, build_string (" "));
1081 push_key_description (c
, p
)
1082 register unsigned int c
;
1099 else if (c
== Ctl('I'))
1105 else if (c
== Ctl('J'))
1111 else if (c
== Ctl('M'))
1121 if (c
> 0 && c
<= Ctl ('Z'))
1145 DEFUN ("single-key-description", Fsingle_key_description
, Ssingle_key_description
, 1, 1, 0,
1146 "Return a pretty description of command character KEY.\n\
1147 Control characters turn into C-whatever, etc.")
1151 register unsigned char c
;
1154 key
= EVENT_HEAD (key
);
1156 switch (XTYPE (key
))
1158 case Lisp_Int
: /* Normal character */
1159 c
= XINT (key
) & 0377;
1160 *push_key_description (c
, tem
) = 0;
1161 return build_string (tem
);
1163 case Lisp_Symbol
: /* Function key or event-symbol */
1164 return Fsymbol_name (key
);
1167 error ("KEY must be an integer, cons, or symbol.");
1172 push_text_char_description (c
, p
)
1173 register unsigned int c
;
1185 *p
++ = c
+ 64; /* 'A' - 1 */
1197 DEFUN ("text-char-description", Ftext_char_description
, Stext_char_description
, 1, 1, 0,
1198 "Return a pretty description of file-character CHAR.\n\
1199 Control characters turn into \"^char\", etc.")
1205 CHECK_NUMBER (chr
, 0);
1207 *push_text_char_description (XINT (chr
) & 0377, tem
) = 0;
1209 return build_string (tem
);
1212 /* where-is - finding a command in a set of keymaps. */
1214 DEFUN ("where-is-internal", Fwhere_is_internal
, Swhere_is_internal
, 1, 5, 0,
1215 "Return list of keys that invoke DEFINITION in KEYMAP or KEYMAP1.\n\
1216 If KEYMAP is nil, search only KEYMAP1.\n\
1217 If KEYMAP1 is nil, use the current global map.\n\
1219 If optional 4th arg FIRSTONLY is non-nil,\n\
1220 return a string representing the first key sequence found,\n\
1221 rather than a list of all possible key sequences.\n\
1223 If optional 5th arg NOINDIRECT is non-nil, don't follow indirections\n\
1224 to other keymaps or slots. This makes it possible to search for an\n\
1225 indirect definition itself.")
1226 (definition
, local_keymap
, global_keymap
, firstonly
, noindirect
)
1227 Lisp_Object definition
, local_keymap
, global_keymap
;
1228 Lisp_Object firstonly
, noindirect
;
1230 register Lisp_Object maps
;
1233 if (NILP (global_keymap
))
1234 global_keymap
= current_global_map
;
1236 if (!NILP (local_keymap
))
1237 maps
= nconc2 (Faccessible_keymaps (get_keymap (local_keymap
)),
1238 Faccessible_keymaps (get_keymap (global_keymap
)));
1240 maps
= Faccessible_keymaps (get_keymap (global_keymap
));
1244 for (; !NILP (maps
); maps
= Fcdr (maps
))
1246 /* Key sequence to reach map */
1247 register Lisp_Object
this = Fcar (Fcar (maps
));
1249 /* The map that it reaches */
1250 register Lisp_Object map
= Fcdr (Fcar (maps
));
1252 /* If Fcar (map) is a VECTOR, the current element within that vector. */
1255 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
1256 [M-CHAR] sequences, check if last character of the sequence
1257 is the meta-prefix char. */
1258 Lisp_Object last
= make_number (XINT (Flength (this)) - 1);
1259 int last_is_meta
= (XINT (last
) >= 0
1260 && EQ (Faref (this, last
), meta_prefix_char
));
1266 /* Because the code we want to run on each binding is rather
1267 large, we don't want to have two separate loop bodies for
1268 sparse keymap bindings and tables; we want to iterate one
1269 loop body over both keymap and vector bindings.
1271 For this reason, if Fcar (map) is a vector, we don't
1272 advance map to the next element until i indicates that we
1273 have finished off the vector. */
1275 Lisp_Object elt
= XCONS (map
)->car
;
1276 Lisp_Object key
, binding
, sequence
;
1280 /* Set key and binding to the current key and binding, and
1281 advance map and i to the next binding. */
1282 if (XTYPE (elt
) == Lisp_Vector
)
1284 /* In a vector, look at each element. */
1285 binding
= XVECTOR (elt
)->contents
[i
];
1289 /* If we've just finished scanning a vector, advance map
1290 to the next element, and reset i in anticipation of the
1291 next vector we may find. */
1292 if (i
>= DENSE_TABLE_SIZE
)
1294 map
= XCONS (map
)->cdr
;
1298 else if (CONSP (elt
))
1300 key
= Fcar (Fcar (map
));
1301 binding
= Fcdr (Fcar (map
));
1303 map
= XCONS (map
)->cdr
;
1306 /* We want to ignore keymap elements that are neither
1307 vectors nor conses. */
1309 map
= XCONS (map
)->cdr
;
1313 /* Search through indirections unless that's not wanted. */
1314 if (NILP (noindirect
))
1315 binding
= get_keyelt (binding
);
1317 /* End this iteration if this element does not match
1320 if (XTYPE (definition
) == Lisp_Cons
)
1323 tem
= Fequal (binding
, definition
);
1328 if (!EQ (binding
, definition
))
1331 /* We have found a match.
1332 Construct the key sequence where we found it. */
1333 if (XTYPE (key
) == Lisp_Int
&& last_is_meta
)
1335 sequence
= Fcopy_sequence (this);
1336 Faset (sequence
, last
, make_number (XINT (key
) | 0200));
1339 sequence
= append_key (this, key
);
1341 /* Verify that this key binding is not shadowed by another
1342 binding for the same key, before we say it exists.
1344 Mechanism: look for local definition of this key and if
1345 it is defined and does not match what we found then
1348 Either nil or number as value from Flookup_key
1350 if (!NILP (local_keymap
))
1352 binding
= Flookup_key (local_keymap
, sequence
);
1353 if (!NILP (binding
) && XTYPE (binding
) != Lisp_Int
)
1355 if (XTYPE (definition
) == Lisp_Cons
)
1358 tem
= Fequal (binding
, definition
);
1363 if (!EQ (binding
, definition
))
1368 /* It is a true unshadowed match. Record it. */
1370 if (!NILP (firstonly
))
1372 found
= Fcons (sequence
, found
);
1375 return Fnreverse (found
);
1378 /* Return a string listing the keys and buttons that run DEFINITION. */
1381 where_is_string (definition
)
1382 Lisp_Object definition
;
1384 register Lisp_Object keys
, keys1
;
1386 keys
= Fwhere_is_internal (definition
,
1387 current_buffer
->keymap
, Qnil
, Qnil
, Qnil
);
1388 keys1
= Fmapconcat (Qkey_description
, keys
, build_string (", "));
1393 DEFUN ("where-is", Fwhere_is
, Swhere_is
, 1, 1, "CWhere is command: ",
1394 "Print message listing key sequences that invoke specified command.\n\
1395 Argument is a command definition, usually a symbol with a function definition.")
1397 Lisp_Object definition
;
1399 register Lisp_Object string
;
1401 CHECK_SYMBOL (definition
, 0);
1402 string
= where_is_string (definition
);
1404 if (XSTRING (string
)->size
)
1405 message ("%s is on %s", XSYMBOL (definition
)->name
->data
,
1406 XSTRING (string
)->data
);
1408 message ("%s is not on any key", XSYMBOL (definition
)->name
->data
);
1412 /* describe-bindings - summarizing all the bindings in a set of keymaps. */
1414 DEFUN ("describe-bindings", Fdescribe_bindings
, Sdescribe_bindings
, 0, 0, "",
1415 "Show a list of all defined keys, and their definitions.\n\
1416 The list is put in a buffer, which is displayed.")
1419 register Lisp_Object thisbuf
;
1420 XSET (thisbuf
, Lisp_Buffer
, current_buffer
);
1421 internal_with_output_to_temp_buffer ("*Help*",
1422 describe_buffer_bindings
,
1428 describe_buffer_bindings (descbuf
)
1429 Lisp_Object descbuf
;
1431 register Lisp_Object start1
, start2
;
1437 char *alternate_heading
1439 Alternate Characters (use anywhere the nominal character is listed):\n\
1440 nominal alternate\n\
1441 ------- ---------\n";
1443 Fset_buffer (Vstandard_output
);
1445 /* Report on alternates for keys. */
1446 if (XTYPE (Vkeyboard_translate_table
) == Lisp_String
)
1449 unsigned char *translate
= XSTRING (Vkeyboard_translate_table
)->data
;
1450 int translate_len
= XSTRING (Vkeyboard_translate_table
)->size
;
1452 for (c
= 0; c
< translate_len
; c
++)
1453 if (translate
[c
] != c
)
1458 if (alternate_heading
)
1460 insert_string (alternate_heading
);
1461 alternate_heading
= 0;
1464 bufend
= push_key_description (translate
[c
], buf
);
1465 insert (buf
, bufend
- buf
);
1466 Findent_to (make_number (16), make_number (1));
1467 bufend
= push_key_description (c
, buf
);
1468 insert (buf
, bufend
- buf
);
1478 Lisp_Object
*modes
, *maps
;
1480 /* Temporarily switch to descbuf, so that we can get that buffer's
1481 minor modes correctly. */
1482 Fset_buffer (descbuf
);
1483 nmaps
= current_minor_maps (&modes
, &maps
);
1484 Fset_buffer (Vstandard_output
);
1486 for (i
= 0; i
< nmaps
; i
++)
1488 if (XTYPE (modes
[i
]) == Lisp_Symbol
)
1491 insert_string (XSYMBOL (modes
[i
])->name
->data
);
1495 insert_string ("Strangely Named");
1496 insert_string (" Minor Mode Bindings:\n");
1497 insert_string (key_heading
);
1498 describe_map_tree (maps
[i
], 0, Qnil
);
1503 start1
= XBUFFER (descbuf
)->keymap
;
1506 insert_string ("Local Bindings:\n");
1507 insert_string (key_heading
);
1508 describe_map_tree (start1
, 0, Qnil
);
1509 insert_string ("\n");
1512 insert_string ("Global Bindings:\n");
1514 insert_string (key_heading
);
1516 describe_map_tree (current_global_map
, 0, XBUFFER (descbuf
)->keymap
);
1518 Fset_buffer (descbuf
);
1522 /* Insert a desription of the key bindings in STARTMAP,
1523 followed by those of all maps reachable through STARTMAP.
1524 If PARTIAL is nonzero, omit certain "uninteresting" commands
1525 (such as `undefined').
1526 If SHADOW is non-nil, it is another map;
1527 don't mention keys which would be shadowed by it. */
1530 describe_map_tree (startmap
, partial
, shadow
)
1531 Lisp_Object startmap
, shadow
;
1534 register Lisp_Object elt
, sh
;
1536 struct gcpro gcpro1
;
1538 maps
= Faccessible_keymaps (startmap
);
1541 for (; !NILP (maps
); maps
= Fcdr (maps
))
1546 /* If there is no shadow keymap given, don't shadow. */
1550 /* If the sequence by which we reach this keymap is zero-length,
1551 then the shadow map for this keymap is just SHADOW. */
1552 else if ((XTYPE (sh
) == Lisp_String
1553 && XSTRING (sh
)->size
== 0)
1554 || (XTYPE (sh
) == Lisp_Vector
1555 && XVECTOR (sh
)->size
== 0))
1558 /* If the sequence by which we reach this keymap actually has
1559 some elements, then the sequence's definition in SHADOW is
1560 what we should use. */
1563 sh
= Flookup_key (shadow
, Fcar (elt
));
1564 if (XTYPE (sh
) == Lisp_Int
)
1568 /* If sh is null (meaning that the current map is not shadowed),
1569 or a keymap (meaning that bindings from the current map might
1570 show through), describe the map. Otherwise, sh is a command
1571 that completely shadows the current map, and we shouldn't
1573 if (NILP (sh
) || !NILP (Fkeymapp (sh
)))
1574 describe_map (Fcdr (elt
), Fcar (elt
), partial
, sh
);
1581 describe_command (definition
)
1582 Lisp_Object definition
;
1584 register Lisp_Object tem1
;
1586 Findent_to (make_number (16), make_number (1));
1588 if (XTYPE (definition
) == Lisp_Symbol
)
1590 XSET (tem1
, Lisp_String
, XSYMBOL (definition
)->name
);
1592 insert_string ("\n");
1596 tem1
= Fkeymapp (definition
);
1598 insert_string ("Prefix Command\n");
1600 insert_string ("??\n");
1604 /* Describe the contents of map MAP, assuming that this map itself is
1605 reached by the sequence of prefix keys KEYS (a string or vector).
1606 PARTIAL, SHADOW is as in `describe_map_tree' above. */
1609 describe_map (map
, keys
, partial
, shadow
)
1610 Lisp_Object map
, keys
;
1614 register Lisp_Object keysdesc
;
1616 if (!NILP (keys
) && XFASTINT (Flength (keys
)) > 0)
1617 keysdesc
= concat2 (Fkey_description (keys
),
1618 build_string (" "));
1622 describe_map_2 (map
, keysdesc
, describe_command
, partial
, shadow
);
1625 /* Insert a description of KEYMAP into the current buffer. */
1628 describe_map_2 (keymap
, elt_prefix
, elt_describer
, partial
, shadow
)
1629 register Lisp_Object keymap
;
1630 Lisp_Object elt_prefix
;
1631 int (*elt_describer
) ();
1636 Lisp_Object tem1
, tem2
= Qnil
;
1637 Lisp_Object suppress
;
1640 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1643 suppress
= intern ("suppress-keymap");
1645 /* This vector gets used to present single keys to Flookup_key. Since
1646 that is done once per keymap element, we don't want to cons up a
1647 fresh vector every time. */
1648 kludge
= Fmake_vector (make_number (1), Qnil
);
1650 GCPRO3 (elt_prefix
, tem2
, kludge
);
1652 for (; CONSP (keymap
); keymap
= Fcdr (keymap
))
1656 if (XTYPE (XCONS (keymap
)->car
) == Lisp_Vector
)
1657 describe_vector (XCONS (keymap
)->car
,
1658 elt_prefix
, elt_describer
, partial
, shadow
);
1661 tem1
= Fcar_safe (Fcar (keymap
));
1662 tem2
= get_keyelt (Fcdr_safe (Fcar (keymap
)));
1664 /* Don't show undefined commands or suppressed commands. */
1665 if (NILP (tem2
)) continue;
1666 if (XTYPE (tem2
) == Lisp_Symbol
&& partial
)
1668 this = Fget (tem2
, suppress
);
1673 /* Don't show a command that isn't really visible
1674 because a local definition of the same key shadows it. */
1680 XVECTOR (kludge
)->contents
[0] = tem1
;
1681 tem
= Flookup_key (shadow
, kludge
);
1682 if (!NILP (tem
)) continue;
1691 if (!NILP (elt_prefix
))
1692 insert1 (elt_prefix
);
1694 /* THIS gets the string to describe the character TEM1. */
1695 this = Fsingle_key_description (tem1
);
1698 /* Print a description of the definition of this character.
1699 elt_describer will take care of spacing out far enough
1700 for alignment purposes. */
1701 (*elt_describer
) (tem2
);
1709 describe_vector_princ (elt
)
1715 DEFUN ("describe-vector", Fdescribe_vector
, Sdescribe_vector
, 1, 1, 0,
1716 "Print on `standard-output' a description of contents of VECTOR.\n\
1717 This is text showing the elements of vector matched against indices.")
1721 CHECK_VECTOR (vector
, 0);
1722 describe_vector (vector
, Qnil
, describe_vector_princ
, 0, Qnil
, Qnil
);
1725 describe_vector (vector
, elt_prefix
, elt_describer
, partial
, shadow
)
1726 register Lisp_Object vector
;
1727 Lisp_Object elt_prefix
;
1728 int (*elt_describer
) ();
1734 Lisp_Object tem1
, tem2
;
1736 Lisp_Object suppress
;
1739 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1743 /* This vector gets used to present single keys to Flookup_key. Since
1744 that is done once per vector element, we don't want to cons up a
1745 fresh vector every time. */
1746 kludge
= Fmake_vector (make_number (1), Qnil
);
1747 GCPRO3 (elt_prefix
, tem1
, kludge
);
1750 suppress
= intern ("suppress-keymap");
1752 for (i
= 0; i
< DENSE_TABLE_SIZE
; i
++)
1755 tem1
= get_keyelt (XVECTOR (vector
)->contents
[i
]);
1757 if (NILP (tem1
)) continue;
1759 /* Don't mention suppressed commands. */
1760 if (XTYPE (tem1
) == Lisp_Symbol
&& partial
)
1762 this = Fget (tem1
, suppress
);
1767 /* If this command in this map is shadowed by some other map,
1773 XVECTOR (kludge
)->contents
[0] = make_number (i
);
1774 tem
= Flookup_key (shadow
, kludge
);
1776 if (!NILP (tem
)) continue;
1785 /* Output the prefix that applies to every entry in this map. */
1786 if (!NILP (elt_prefix
))
1787 insert1 (elt_prefix
);
1789 /* Get the string to describe the character I, and print it. */
1790 XFASTINT (dummy
) = i
;
1792 /* THIS gets the string to describe the character DUMMY. */
1793 this = Fsingle_key_description (dummy
);
1796 /* Find all consecutive characters that have the same definition. */
1797 while (i
+ 1 < DENSE_TABLE_SIZE
1798 && (tem2
= get_keyelt (XVECTOR (vector
)->contents
[i
+1]),
1802 /* If we have a range of more than one character,
1803 print where the range reaches to. */
1805 if (i
!= XINT (dummy
))
1808 if (!NILP (elt_prefix
))
1809 insert1 (elt_prefix
);
1811 XFASTINT (dummy
) = i
;
1812 insert1 (Fsingle_key_description (dummy
));
1815 /* Print a description of the definition of this character.
1816 elt_describer will take care of spacing out far enough
1817 for alignment purposes. */
1818 (*elt_describer
) (tem1
);
1824 /* Apropos - finding all symbols whose names match a regexp. */
1825 Lisp_Object apropos_predicate
;
1826 Lisp_Object apropos_accumulate
;
1829 apropos_accum (symbol
, string
)
1830 Lisp_Object symbol
, string
;
1832 register Lisp_Object tem
;
1834 tem
= Fstring_match (string
, Fsymbol_name (symbol
), Qnil
);
1835 if (!NILP (tem
) && !NILP (apropos_predicate
))
1836 tem
= call1 (apropos_predicate
, symbol
);
1838 apropos_accumulate
= Fcons (symbol
, apropos_accumulate
);
1841 DEFUN ("apropos-internal", Fapropos_internal
, Sapropos_internal
, 1, 2, 0,
1842 "Show all symbols whose names contain match for REGEXP.\n\
1843 If optional 2nd arg PRED is non-nil, (funcall PRED SYM) is done\n\
1844 for each symbol and a symbol is mentioned only if that returns non-nil.\n\
1845 Return list of symbols found.")
1847 Lisp_Object string
, pred
;
1849 struct gcpro gcpro1
, gcpro2
;
1850 CHECK_STRING (string
, 0);
1851 apropos_predicate
= pred
;
1852 GCPRO2 (apropos_predicate
, apropos_accumulate
);
1853 apropos_accumulate
= Qnil
;
1854 map_obarray (Vobarray
, apropos_accum
, string
);
1855 apropos_accumulate
= Fsort (apropos_accumulate
, Qstring_lessp
);
1857 return apropos_accumulate
;
1864 Qkeymap
= intern ("keymap");
1865 staticpro (&Qkeymap
);
1867 /* Initialize the keymaps standardly used.
1868 Each one is the value of a Lisp variable, and is also
1869 pointed to by a C variable */
1871 global_map
= Fmake_keymap (Qnil
);
1872 Fset (intern ("global-map"), global_map
);
1874 meta_map
= Fmake_keymap (Qnil
);
1875 Fset (intern ("esc-map"), meta_map
);
1876 Ffset (intern ("ESC-prefix"), meta_map
);
1878 control_x_map
= Fmake_keymap (Qnil
);
1879 Fset (intern ("ctl-x-map"), control_x_map
);
1880 Ffset (intern ("Control-X-prefix"), control_x_map
);
1882 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map
,
1883 "Default keymap to use when reading from the minibuffer.");
1884 Vminibuffer_local_map
= Fmake_sparse_keymap (Qnil
);
1886 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map
,
1887 "Local keymap for the minibuffer when spaces are not allowed.");
1888 Vminibuffer_local_ns_map
= Fmake_sparse_keymap (Qnil
);
1890 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map
,
1891 "Local keymap for minibuffer input with completion.");
1892 Vminibuffer_local_completion_map
= Fmake_sparse_keymap (Qnil
);
1894 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map
,
1895 "Local keymap for minibuffer input with completion, for exact match.");
1896 Vminibuffer_local_must_match_map
= Fmake_sparse_keymap (Qnil
);
1898 current_global_map
= global_map
;
1900 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist
,
1901 "Alist of keymaps to use for minor modes.\n\
1902 Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read\n\
1903 key sequences and look up bindings iff VARIABLE's value is non-nil.\n\
1904 If two active keymaps bind the same key, the keymap appearing earlier\n\
1905 in the list takes precedence.");
1906 Vminor_mode_map_alist
= Qnil
;
1908 DEFVAR_LISP ("function-key-map", &Vfunction_key_map
,
1909 "Keymap mapping ASCII function key sequences onto their preferred forms.\n\
1910 This allows Emacs to recognize function keys sent from ASCII\n\
1911 terminals at any point in a key sequence.\n\
1913 The read-key-sequence function replaces subsequences bound by\n\
1914 function-key-map with their bindings. When the current local and global\n\
1915 keymaps have no binding for the current key sequence but\n\
1916 function-key-map binds a suffix of the sequence to a vector,\n\
1917 read-key-sequence replaces the matching suffix with its binding, and\n\
1918 continues with the new sequence.\n\
1920 For example, suppose function-key-map binds `ESC O P' to [pf1].\n\
1921 Typing `ESC O P' to read-key-sequence would return [pf1]. Typing\n\
1922 `C-x ESC O P' would return [?\C-x pf1]. If [pf1] were a prefix\n\
1923 key, typing `ESC O P x' would return [pf1 x].");
1924 Vfunction_key_map
= Fmake_sparse_keymap (Qnil
);
1926 Qsingle_key_description
= intern ("single-key-description");
1927 staticpro (&Qsingle_key_description
);
1929 Qkey_description
= intern ("key-description");
1930 staticpro (&Qkey_description
);
1932 Qkeymapp
= intern ("keymapp");
1933 staticpro (&Qkeymapp
);
1935 defsubr (&Skeymapp
);
1936 defsubr (&Smake_keymap
);
1937 defsubr (&Smake_sparse_keymap
);
1938 defsubr (&Scopy_keymap
);
1939 defsubr (&Skey_binding
);
1940 defsubr (&Slocal_key_binding
);
1941 defsubr (&Sglobal_key_binding
);
1942 defsubr (&Sminor_mode_key_binding
);
1943 defsubr (&Sglobal_set_key
);
1944 defsubr (&Slocal_set_key
);
1945 defsubr (&Sdefine_key
);
1946 defsubr (&Slookup_key
);
1947 defsubr (&Sglobal_unset_key
);
1948 defsubr (&Slocal_unset_key
);
1949 defsubr (&Sdefine_prefix_command
);
1950 defsubr (&Suse_global_map
);
1951 defsubr (&Suse_local_map
);
1952 defsubr (&Scurrent_local_map
);
1953 defsubr (&Scurrent_global_map
);
1954 defsubr (&Scurrent_minor_mode_maps
);
1955 defsubr (&Saccessible_keymaps
);
1956 defsubr (&Skey_description
);
1957 defsubr (&Sdescribe_vector
);
1958 defsubr (&Ssingle_key_description
);
1959 defsubr (&Stext_char_description
);
1960 defsubr (&Swhere_is_internal
);
1961 defsubr (&Swhere_is
);
1962 defsubr (&Sdescribe_bindings
);
1963 defsubr (&Sapropos_internal
);
1970 initial_define_key (global_map
, 033, "ESC-prefix");
1971 initial_define_key (global_map
, Ctl('X'), "Control-X-prefix");