*** empty log message ***
[emacs.git] / src / keymap.c
blob89a273db9725991034a60c9516d88b41e464a018
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)
9 any later version.
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. */
21 #include "config.h"
22 #include <stdio.h>
23 #undef NULL
24 #include "lisp.h"
25 #include "commands.h"
26 #include "buffer.h"
27 #include "keyboard.h"
29 #define min(a, b) ((a) < (b) ? (a) : (b))
31 /* Dense keymaps look like (keymap VECTOR . ALIST), where VECTOR is a
32 128-element vector used to look up bindings for ASCII characters,
33 and ALIST is an assoc list for looking up symbols. */
34 #define DENSE_TABLE_SIZE (0200)
36 /* Actually allocate storage for these variables */
38 Lisp_Object current_global_map; /* Current global keymap */
40 Lisp_Object global_map; /* default global key bindings */
42 Lisp_Object meta_map; /* The keymap used for globally bound
43 ESC-prefixed default commands */
45 Lisp_Object control_x_map; /* The keymap used for globally bound
46 C-x-prefixed default commands */
48 /* was MinibufLocalMap */
49 Lisp_Object Vminibuffer_local_map;
50 /* The keymap used by the minibuf for local
51 bindings when spaces are allowed in the
52 minibuf */
54 /* was MinibufLocalNSMap */
55 Lisp_Object Vminibuffer_local_ns_map;
56 /* The keymap used by the minibuf for local
57 bindings when spaces are not encouraged
58 in the minibuf */
60 /* keymap used for minibuffers when doing completion */
61 /* was MinibufLocalCompletionMap */
62 Lisp_Object Vminibuffer_local_completion_map;
64 /* keymap used for minibuffers when doing completion and require a match */
65 /* was MinibufLocalMustMatchMap */
66 Lisp_Object Vminibuffer_local_must_match_map;
68 /* Alist of minor mode variables and keymaps. */
69 Lisp_Object Vminor_mode_map_alist;
71 /* Keymap mapping ASCII function key sequences onto their preferred forms.
72 Initialized by the terminal-specific lisp files. See DEFVAR for more
73 documentation. */
74 Lisp_Object Vfunction_key_map;
76 Lisp_Object Qkeymapp, Qkeymap;
78 /* A char over 0200 in a key sequence
79 is equivalent to prefixing with this character. */
81 extern Lisp_Object meta_prefix_char;
83 void describe_map_tree ();
84 static Lisp_Object describe_buffer_bindings ();
85 static void describe_command ();
86 static void describe_map ();
87 static void describe_alist ();
89 /* Keymap object support - constructors and predicates. */
91 DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 0, 0,
92 "Construct and return a new keymap, of the form (keymap VECTOR . ALIST).\n\
93 VECTOR is a 128-element vector which holds the bindings for the ASCII\n\
94 characters. ALIST is an assoc-list which holds bindings for function keys,\n\
95 mouse events, and any other things that appear in the input stream.\n\
96 All entries in it are initially nil, meaning \"command undefined\".")
99 return Fcons (Qkeymap,
100 Fcons (Fmake_vector (make_number (DENSE_TABLE_SIZE), Qnil),
101 Qnil));
104 DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 0, 0,
105 "Construct and return a new sparse-keymap list.\n\
106 Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),\n\
107 which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),\n\
108 which binds the function key or mouse event SYMBOL to DEFINITION.\n\
109 Initially the alist is nil.")
112 return Fcons (Qkeymap, Qnil);
115 /* This function is used for installing the standard key bindings
116 at initialization time.
118 For example:
120 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark");
122 I haven't extended these to allow the initializing code to bind
123 function keys and mouse events; since they are called by many files,
124 I'd have to fix lots of callers, and nobody right now would be using
125 the new functionality, so it seems like a waste of time. But there's
126 no technical reason not to. -JimB */
128 void
129 initial_define_key (keymap, key, defname)
130 Lisp_Object keymap;
131 int key;
132 char *defname;
134 store_in_keymap (keymap, make_number (key), intern (defname));
137 /* Define character fromchar in map frommap as an alias for character
138 tochar in map tomap. Subsequent redefinitions of the latter WILL
139 affect the former. */
141 #if 0
142 void
143 synkey (frommap, fromchar, tomap, tochar)
144 struct Lisp_Vector *frommap, *tomap;
145 int fromchar, tochar;
147 Lisp_Object v, c;
148 XSET (v, Lisp_Vector, tomap);
149 XFASTINT (c) = tochar;
150 frommap->contents[fromchar] = Fcons (v, c);
152 #endif /* 0 */
154 DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0,
155 "Return t if ARG is a keymap.\n\
157 A keymap is list (keymap . ALIST), a list (keymap VECTOR . ALIST),\n\
158 or a symbol whose function definition is a keymap is itself a keymap.\n\
159 ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);\n\
160 VECTOR is a 128-element vector of bindings for ASCII characters.")
161 (object)
162 Lisp_Object object;
164 return (NILP (get_keymap_1 (object, 0)) ? Qnil : Qt);
167 /* Check that OBJECT is a keymap (after dereferencing through any
168 symbols). If it is, return it; otherwise, return nil, or signal an
169 error if ERROR != 0. */
170 Lisp_Object
171 get_keymap_1 (object, error)
172 Lisp_Object object;
173 int error;
175 register Lisp_Object tem;
177 tem = indirect_function (object);
178 if (CONSP (tem) && EQ (XCONS (tem)->car, Qkeymap))
179 return tem;
180 if (error)
181 wrong_type_argument (Qkeymapp, object);
182 else
183 return Qnil;
186 Lisp_Object
187 get_keymap (object)
188 Lisp_Object object;
190 return get_keymap_1 (object, 1);
194 /* If KEYMAP is a dense keymap, return the vector from its cadr.
195 Otherwise, return nil. */
197 static Lisp_Object
198 keymap_table (keymap)
199 Lisp_Object keymap;
201 Lisp_Object cadr;
203 if (CONSP (XCONS (keymap)->cdr)
204 && XTYPE (cadr = XCONS (XCONS (keymap)->cdr)->car) == Lisp_Vector
205 && XVECTOR (cadr)->size == DENSE_TABLE_SIZE)
206 return cadr;
207 else
208 return Qnil;
212 /* Look up IDX in MAP. IDX may be any sort of event.
213 Note that this does only one level of lookup; IDX must
214 be a single event, not a sequence. */
216 Lisp_Object
217 access_keymap (map, idx)
218 Lisp_Object map;
219 Lisp_Object idx;
221 /* If idx is a list (some sort of mouse click, perhaps?),
222 the index we want to use is the car of the list, which
223 ought to be a symbol. */
224 if (EVENT_HAS_PARAMETERS (idx))
225 idx = EVENT_HEAD (idx);
227 if (XTYPE (idx) == Lisp_Int
228 && (XINT (idx) < 0 || XINT (idx) >= DENSE_TABLE_SIZE))
229 error ("Command key is not an ASCII character");
232 Lisp_Object table = keymap_table (map);
234 /* A dense keymap indexed by a character? */
235 if (XTYPE (idx) == Lisp_Int
236 && ! NILP (table))
237 return XVECTOR (table)->contents[XFASTINT (idx)];
239 /* This lookup will not involve a vector reference. */
240 else
242 /* If idx is a symbol, it might have modifiers, which need to
243 be put in the canonical order. */
244 if (XTYPE (idx) == Lisp_Symbol)
245 idx = reorder_modifiers (idx);
247 return Fcdr (Fassq (idx, map));
252 /* Given OBJECT which was found in a slot in a keymap,
253 trace indirect definitions to get the actual definition of that slot.
254 An indirect definition is a list of the form
255 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
256 and INDEX is the object to look up in KEYMAP to yield the definition.
258 Also if OBJECT has a menu string as the first element,
259 remove that. */
261 Lisp_Object
262 get_keyelt (object)
263 register Lisp_Object object;
265 while (1)
267 register Lisp_Object map, tem;
269 map = get_keymap_1 (Fcar_safe (object), 0);
270 tem = Fkeymapp (map);
272 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
273 if (!NILP (tem))
274 object = access_keymap (map, Fcdr (object));
276 /* If the keymap contents looks like (STRING . DEFN),
277 use DEFN.
278 Keymap alist elements like (CHAR MENUSTRING . DEFN)
279 will be used by HierarKey menus. */
280 else if (XTYPE (object) == Lisp_Cons
281 && XTYPE (XCONS (object)->car) == Lisp_String)
282 object = XCONS (object)->cdr;
284 else
285 /* Anything else is really the value. */
286 return object;
290 Lisp_Object
291 store_in_keymap (keymap, idx, def)
292 Lisp_Object keymap;
293 register Lisp_Object idx;
294 register Lisp_Object def;
296 /* If idx is a list (some sort of mouse click, perhaps?),
297 the index we want to use is the car of the list, which
298 ought to be a symbol. */
299 if (EVENT_HAS_PARAMETERS (idx))
300 idx = EVENT_HEAD (idx);
302 if (XTYPE (idx) == Lisp_Int
303 && (XINT (idx) < 0 || XINT (idx) >= DENSE_TABLE_SIZE))
304 error ("Command key is a character outside of the ASCII set.");
307 Lisp_Object table = keymap_table (keymap);
309 /* A dense keymap indexed by a character? */
310 if (XTYPE (idx) == Lisp_Int && !NILP (table))
311 XVECTOR (table)->contents[XFASTINT (idx)] = def;
313 /* Must be a sparse keymap, or a dense keymap indexed by a symbol. */
314 else
316 /* Point to the pointer to the start of the assoc-list part
317 of the keymap. */
318 register Lisp_Object *assoc_head
319 = (NILP (table)
320 ? & XCONS (keymap)->cdr
321 : & XCONS (XCONS (keymap)->cdr)->cdr);
322 register Lisp_Object defining_pair;
324 /* If idx is a symbol, it might have modifiers, which need to
325 be put in the canonical order. */
326 if (XTYPE (idx) == Lisp_Symbol)
327 idx = reorder_modifiers (idx);
329 /* Point to the pair where idx is bound, if any. */
330 defining_pair = Fassq (idx, *assoc_head);
332 if (NILP (defining_pair))
333 *assoc_head = Fcons (Fcons (idx, def), *assoc_head);
334 else
335 Fsetcdr (defining_pair, def);
339 return def;
342 DEFUN ("copy-keymap", Fcopy_keymap, Scopy_keymap, 1, 1, 0,
343 "Return a copy of the keymap KEYMAP.\n\
344 The copy starts out with the same definitions of KEYMAP,\n\
345 but changing either the copy or KEYMAP does not affect the other.\n\
346 Any key definitions that are subkeymaps are recursively copied.\n\
347 However, a key definition which is a symbol whose definition is a keymap\n\
348 is not copied.")
349 (keymap)
350 Lisp_Object keymap;
352 register Lisp_Object copy, tail;
354 copy = Fcopy_alist (get_keymap (keymap));
355 tail = XCONS (copy)->cdr;
357 /* If this is a dense keymap, copy the vector. */
358 if (CONSP (tail))
360 register Lisp_Object table = XCONS (tail)->car;
362 if (XTYPE (table) == Lisp_Vector
363 && XVECTOR (table)->size == DENSE_TABLE_SIZE)
365 register int i;
367 table = Fcopy_sequence (table);
369 for (i = 0; i < DENSE_TABLE_SIZE; i++)
370 if (XTYPE (XVECTOR (copy)->contents[i]) != Lisp_Symbol)
371 if (! NILP (Fkeymapp (XVECTOR (table)->contents[i])))
372 XVECTOR (table)->contents[i]
373 = Fcopy_keymap (XVECTOR (table)->contents[i]);
374 XCONS (tail)->car = table;
376 tail = XCONS (tail)->cdr;
380 /* Copy the alist portion of the keymap. */
381 while (CONSP (tail))
383 register Lisp_Object elt;
385 elt = XCONS (tail)->car;
386 if (CONSP (elt)
387 && XTYPE (XCONS (elt)->cdr) != Lisp_Symbol
388 && ! NILP (Fkeymapp (XCONS (elt)->cdr)))
389 XCONS (elt)->cdr = Fcopy_keymap (XCONS (elt)->cdr);
391 tail = XCONS (tail)->cdr;
394 return copy;
397 /* Simple Keymap mutators and accessors. */
399 DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0,
400 "Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.\n\
401 KEYMAP is a keymap. KEY is a string or a vector of symbols and characters\n\
402 meaning a sequence of keystrokes and events.\n\
403 DEF is anything that can be a key's definition:\n\
404 nil (means key is undefined in this keymap),\n\
405 a command (a Lisp function suitable for interactive calling)\n\
406 a string (treated as a keyboard macro),\n\
407 a keymap (to define a prefix key),\n\
408 a symbol. When the key is looked up, the symbol will stand for its\n\
409 function definition, which should at that time be one of the above,\n\
410 or another symbol whose function definition is used, etc.\n\
411 a cons (STRING . DEFN), meaning that DEFN is the definition\n\
412 (DEFN should be a valid definition in its own right),\n\
413 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.\n\
415 If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at\n\
416 the front of KEYMAP.")
417 (keymap, key, def)
418 register Lisp_Object keymap;
419 Lisp_Object key;
420 Lisp_Object def;
422 register int idx;
423 register Lisp_Object c;
424 register Lisp_Object tem;
425 register Lisp_Object cmd;
426 int metized = 0;
427 int length;
429 keymap = get_keymap (keymap);
431 if (XTYPE (key) != Lisp_Vector
432 && XTYPE (key) != Lisp_String)
433 key = wrong_type_argument (Qarrayp, key);
435 length = Flength (key);
436 if (length == 0)
437 return Qnil;
439 idx = 0;
440 while (1)
442 c = Faref (key, make_number (idx));
444 if (XTYPE (c) == Lisp_Int
445 && XINT (c) >= 0200
446 && !metized)
448 c = meta_prefix_char;
449 metized = 1;
451 else
453 if (XTYPE (c) == Lisp_Int)
454 XSETINT (c, XINT (c) & 0177);
456 metized = 0;
457 idx++;
460 if (idx == length)
461 return store_in_keymap (keymap, c, def);
463 cmd = get_keyelt (access_keymap (keymap, c));
465 if (NILP (cmd))
467 cmd = Fmake_sparse_keymap ();
468 store_in_keymap (keymap, c, cmd);
471 tem = Fkeymapp (cmd);
472 if (NILP (tem))
473 error ("Key sequence %s uses invalid prefix characters",
474 XSTRING (key)->data);
476 keymap = get_keymap (cmd);
480 /* Value is number if KEY is too long; NIL if valid but has no definition. */
482 DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 2, 0,
483 "In keymap KEYMAP, look up key sequence KEY. Return the definition.\n\
484 nil means undefined. See doc of `define-key' for kinds of definitions.\n\
485 A number as value means KEY is \"too long\";\n\
486 that is, characters or symbols in it except for the last one\n\
487 fail to be a valid sequence of prefix characters in KEYMAP.\n\
488 The number is how many characters at the front of KEY\n\
489 it takes to reach a non-prefix command.")
490 (keymap, key)
491 register Lisp_Object keymap;
492 Lisp_Object key;
494 register int idx;
495 register Lisp_Object tem;
496 register Lisp_Object cmd;
497 register Lisp_Object c;
498 int metized = 0;
499 int length;
501 keymap = get_keymap (keymap);
503 if (XTYPE (key) != Lisp_Vector
504 && XTYPE (key) != Lisp_String)
505 key = wrong_type_argument (Qarrayp, key);
507 length = Flength (key);
508 if (length == 0)
509 return keymap;
511 idx = 0;
512 while (1)
514 c = Faref (key, make_number (idx));
516 if (XTYPE (c) == Lisp_Int
517 && XINT (c) >= 0200
518 && !metized)
520 c = meta_prefix_char;
521 metized = 1;
523 else
525 if (XTYPE (c) == Lisp_Int)
526 XSETINT (c, XINT (c) & 0177);
528 metized = 0;
529 idx++;
532 cmd = get_keyelt (access_keymap (keymap, c));
533 if (idx == length)
534 return cmd;
536 tem = Fkeymapp (cmd);
537 if (NILP (tem))
538 return make_number (idx);
540 keymap = get_keymap (cmd);
541 QUIT;
545 /* Append a key to the end of a key sequence. If key_sequence is a
546 string and key is a character, the result will be another string;
547 otherwise, it will be a vector. */
548 Lisp_Object
549 append_key (key_sequence, key)
550 Lisp_Object key_sequence, key;
552 Lisp_Object args[2];
554 args[0] = key_sequence;
556 if (XTYPE (key_sequence) == Lisp_String
557 && XTYPE (key) == Lisp_Int)
559 args[1] = Fchar_to_string (key);
560 return Fconcat (2, args);
562 else
564 args[1] = Fcons (key, Qnil);
565 return Fvconcat (2, args);
570 /* Global, local, and minor mode keymap stuff. */
572 /* We can't put these variables inside current_minor_maps, since under
573 some systems, static gets macro-defined to be the empty string.
574 Ickypoo. */
575 static Lisp_Object *cmm_modes, *cmm_maps;
576 static int cmm_size;
578 /* Store a pointer to an array of the keymaps of the currently active
579 minor modes in *buf, and return the number of maps it contains.
581 This function always returns a pointer to the same buffer, and may
582 free or reallocate it, so if you want to keep it for a long time or
583 hand it out to lisp code, copy it. This procedure will be called
584 for every key sequence read, so the nice lispy approach (return a
585 new assoclist, list, what have you) for each invocation would
586 result in a lot of consing over time.
588 If we used xrealloc/xmalloc and ran out of memory, they would throw
589 back to the command loop, which would try to read a key sequence,
590 which would call this function again, resulting in an infinite
591 loop. Instead, we'll use realloc/malloc and silently truncate the
592 list, let the key sequence be read, and hope some other piece of
593 code signals the error. */
595 current_minor_maps (modeptr, mapptr)
596 Lisp_Object **modeptr, **mapptr;
598 int i = 0;
599 Lisp_Object alist, assoc, var, val;
601 for (alist = Vminor_mode_map_alist;
602 CONSP (alist);
603 alist = XCONS (alist)->cdr)
604 if (CONSP (assoc = XCONS (alist)->car)
605 && XTYPE (var = XCONS (assoc)->car) == Lisp_Symbol
606 && ! EQ ((val = find_symbol_value (var)), Qunbound)
607 && ! NILP (val))
609 if (i >= cmm_size)
611 Lisp_Object *newmodes, *newmaps;
613 if (cmm_maps)
615 newmodes = (Lisp_Object *) realloc (cmm_modes, cmm_size *= 2);
616 newmaps = (Lisp_Object *) realloc (cmm_maps, cmm_size);
618 else
620 newmodes = (Lisp_Object *) malloc (cmm_size = 30);
621 newmaps = (Lisp_Object *) malloc (cmm_size);
624 if (newmaps && newmodes)
626 cmm_modes = newmodes;
627 cmm_maps = newmaps;
629 else
630 break;
632 cmm_modes[i] = var;
633 cmm_maps [i] = XCONS (assoc)->cdr;
634 i++;
637 if (modeptr) *modeptr = cmm_modes;
638 if (mapptr) *mapptr = cmm_maps;
639 return i;
642 DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 1, 0,
643 "Return the binding for command KEY in current keymaps.\n\
644 KEY is a string, a sequence of keystrokes.\n\
645 The binding is probably a symbol with a function definition.")
646 (key)
647 Lisp_Object key;
649 Lisp_Object *maps, value;
650 int nmaps, i;
652 nmaps = current_minor_maps (0, &maps);
653 for (i = 0; i < nmaps; i++)
654 if (! NILP (maps[i]))
656 value = Flookup_key (maps[i], key);
657 if (! NILP (value) && XTYPE (value) != Lisp_Int)
658 return value;
661 if (! NILP (current_buffer->keymap))
663 value = Flookup_key (current_buffer->keymap, key);
664 if (! NILP (value) && XTYPE (value) != Lisp_Int)
665 return value;
668 value = Flookup_key (current_global_map, key);
669 if (! NILP (value) && XTYPE (value) != Lisp_Int)
670 return value;
672 return Qnil;
675 DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 1, 0,
676 "Return the binding for command KEYS in current local keymap only.\n\
677 KEYS is a string, a sequence of keystrokes.\n\
678 The binding is probably a symbol with a function definition.")
679 (keys)
680 Lisp_Object keys;
682 register Lisp_Object map;
683 map = current_buffer->keymap;
684 if (NILP (map))
685 return Qnil;
686 return Flookup_key (map, keys);
689 DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 1, 0,
690 "Return the binding for command KEYS in current global keymap only.\n\
691 KEYS is a string, a sequence of keystrokes.\n\
692 The binding is probably a symbol with a function definition.\n\
693 This function's return values are the same as those of lookup-key\n\
694 (which see).")
695 (keys)
696 Lisp_Object keys;
698 return Flookup_key (current_global_map, keys);
701 DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 1, 0,
702 "Find the visible minor mode bindings of KEY.\n\
703 Return an alist of pairs (MODENAME . BINDING), where MODENAME is the\n\
704 the symbol which names the minor mode binding KEY, and BINDING is\n\
705 KEY's definition in that mode. In particular, if KEY has no\n\
706 minor-mode bindings, return nil. If the first binding is a\n\
707 non-prefix, all subsequent bindings will be omitted, since they would\n\
708 be ignored. Similarly, the list doesn't include non-prefix bindings\n\
709 that come after prefix bindings.")
710 (key)
712 Lisp_Object *modes, *maps;
713 int nmaps;
714 Lisp_Object binding;
715 int i, j;
717 nmaps = current_minor_maps (&modes, &maps);
719 for (i = j = 0; i < nmaps; i++)
720 if (! NILP (maps[i])
721 && ! NILP (binding = Flookup_key (maps[i], key))
722 && XTYPE (binding) != Lisp_Int)
724 if (! NILP (get_keymap_1 (binding, 0)))
725 maps[j++] = Fcons (modes[i], binding);
726 else if (j == 0)
727 return Fcons (Fcons (modes[i], binding), Qnil);
730 return Flist (j, maps);
733 DEFUN ("global-set-key", Fglobal_set_key, Sglobal_set_key, 2, 2,
734 "kSet key globally: \nCSet key %s to command: ",
735 "Give KEY a global binding as COMMAND.\n\
736 COMMAND is a symbol naming an interactively-callable function.\n\
737 KEY is a string representing a sequence of keystrokes.\n\
738 Note that if KEY has a local binding in the current buffer\n\
739 that local binding will continue to shadow any global binding.")
740 (keys, function)
741 Lisp_Object keys, function;
743 if (XTYPE (keys) != Lisp_Vector
744 && XTYPE (keys) != Lisp_String)
745 keys = wrong_type_argument (Qarrayp, keys);
747 Fdefine_key (current_global_map, keys, function);
748 return Qnil;
751 DEFUN ("local-set-key", Flocal_set_key, Slocal_set_key, 2, 2,
752 "kSet key locally: \nCSet key %s locally to command: ",
753 "Give KEY a local binding as COMMAND.\n\
754 COMMAND is a symbol naming an interactively-callable function.\n\
755 KEY is a string representing a sequence of keystrokes.\n\
756 The binding goes in the current buffer's local map,\n\
757 which is shared with other buffers in the same major mode.")
758 (keys, function)
759 Lisp_Object keys, function;
761 register Lisp_Object map;
762 map = current_buffer->keymap;
763 if (NILP (map))
765 map = Fmake_sparse_keymap ();
766 current_buffer->keymap = map;
769 if (XTYPE (keys) != Lisp_Vector
770 && XTYPE (keys) != Lisp_String)
771 keys = wrong_type_argument (Qarrayp, keys);
773 Fdefine_key (map, keys, function);
774 return Qnil;
777 DEFUN ("global-unset-key", Fglobal_unset_key, Sglobal_unset_key,
778 1, 1, "kUnset key globally: ",
779 "Remove global binding of KEY.\n\
780 KEY is a string representing a sequence of keystrokes.")
781 (keys)
782 Lisp_Object keys;
784 return Fglobal_set_key (keys, Qnil);
787 DEFUN ("local-unset-key", Flocal_unset_key, Slocal_unset_key, 1, 1,
788 "kUnset key locally: ",
789 "Remove local binding of KEY.\n\
790 KEY is a string representing a sequence of keystrokes.")
791 (keys)
792 Lisp_Object keys;
794 if (!NILP (current_buffer->keymap))
795 Flocal_set_key (keys, Qnil);
796 return Qnil;
799 DEFUN ("define-prefix-command", Fdefine_prefix_command, Sdefine_prefix_command, 1, 2, 0,
800 "Define COMMAND as a prefix command.\n\
801 A new sparse keymap is stored as COMMAND's function definition and its value.\n\
802 If a second optional argument MAPVAR is given, the map is stored as\n\
803 its value instead of as COMMAND's value; but COMMAND is still defined\n\
804 as a function.")
805 (name, mapvar)
806 Lisp_Object name, mapvar;
808 Lisp_Object map;
809 map = Fmake_sparse_keymap ();
810 Ffset (name, map);
811 if (!NILP (mapvar))
812 Fset (mapvar, map);
813 else
814 Fset (name, map);
815 return name;
818 DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0,
819 "Select KEYMAP as the global keymap.")
820 (keymap)
821 Lisp_Object keymap;
823 keymap = get_keymap (keymap);
824 current_global_map = keymap;
825 return Qnil;
828 DEFUN ("use-local-map", Fuse_local_map, Suse_local_map, 1, 1, 0,
829 "Select KEYMAP as the local keymap.\n\
830 If KEYMAP is nil, that means no local keymap.")
831 (keymap)
832 Lisp_Object keymap;
834 if (!NILP (keymap))
835 keymap = get_keymap (keymap);
837 current_buffer->keymap = keymap;
839 return Qnil;
842 DEFUN ("current-local-map", Fcurrent_local_map, Scurrent_local_map, 0, 0, 0,
843 "Return current buffer's local keymap, or nil if it has none.")
846 return current_buffer->keymap;
849 DEFUN ("current-global-map", Fcurrent_global_map, Scurrent_global_map, 0, 0, 0,
850 "Return the current global keymap.")
853 return current_global_map;
856 DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps, Scurrent_minor_mode_maps, 0, 0, 0,
857 "Return a list of keymaps for the minor modes of the current buffer.")
860 Lisp_Object *maps;
861 int nmaps = current_minor_maps (0, &maps);
863 return Flist (nmaps, maps);
866 /* Help functions for describing and documenting keymaps. */
868 DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps,
869 1, 1, 0,
870 "Find all keymaps accessible via prefix characters from KEYMAP.\n\
871 Returns a list of elements of the form (KEYS . MAP), where the sequence\n\
872 KEYS starting from KEYMAP gets you to MAP. These elements are ordered\n\
873 so that the KEYS increase in length. The first element is (\"\" . KEYMAP).")
874 (startmap)
875 Lisp_Object startmap;
877 Lisp_Object maps, tail;
879 maps = Fcons (Fcons (build_string (""), get_keymap (startmap)), Qnil);
880 tail = maps;
882 /* For each map in the list maps,
883 look at any other maps it points to,
884 and stick them at the end if they are not already in the list.
886 This is a breadth-first traversal, where tail is the queue of
887 nodes, and maps accumulates a list of all nodes visited. */
889 while (!NILP (tail))
891 register Lisp_Object thisseq = Fcar (Fcar (tail));
892 register Lisp_Object thismap = Fcdr (Fcar (tail));
893 Lisp_Object last = make_number (XINT (Flength (thisseq)) - 1);
895 /* Does the current sequence end in the meta-prefix-char? */
896 int is_metized = (XINT (last) >= 0
897 && EQ (Faref (thisseq, last), meta_prefix_char));
899 /* Skip the 'keymap element of the list. */
900 thismap = Fcdr (thismap);
902 if (CONSP (thismap))
904 register Lisp_Object table = XCONS (thismap)->car;
906 if (XTYPE (table) == Lisp_Vector)
908 register int i;
910 /* Vector keymap. Scan all the elements. */
911 for (i = 0; i < DENSE_TABLE_SIZE; i++)
913 register Lisp_Object tem;
914 register Lisp_Object cmd;
916 cmd = get_keyelt (XVECTOR (table)->contents[i]);
917 if (NILP (cmd)) continue;
918 tem = Fkeymapp (cmd);
919 if (!NILP (tem))
921 cmd = get_keymap (cmd);
922 /* Ignore keymaps that are already added to maps. */
923 tem = Frassq (cmd, maps);
924 if (NILP (tem))
926 /* If the last key in thisseq is meta-prefix-char,
927 turn it into a meta-ized keystroke. We know
928 that the event we're about to append is an
929 ascii keystroke. */
930 if (is_metized)
932 tem = Fcopy_sequence (thisseq);
933 Faset (tem, last, make_number (i | 0200));
935 /* This new sequence is the same length as
936 thisseq, so stick it in the list right
937 after this one. */
938 XCONS (tail)->cdr =
939 Fcons (Fcons (tem, cmd), XCONS (tail)->cdr);
941 else
943 tem = append_key (thisseq, make_number (i));
944 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
950 /* Once finished with the lookup elements of the dense
951 keymap, go on to scan its assoc list. */
952 thismap = XCONS (thismap)->cdr;
956 /* The rest is an alist. Scan all the alist elements. */
957 while (CONSP (thismap))
959 Lisp_Object elt = XCONS (thismap)->car;
961 /* Ignore elements that are not conses. */
962 if (CONSP (elt))
964 register Lisp_Object cmd = get_keyelt (XCONS (elt)->cdr);
965 register Lisp_Object tem;
967 /* Ignore definitions that aren't keymaps themselves. */
968 tem = Fkeymapp (cmd);
969 if (!NILP (tem))
971 /* Ignore keymaps that have been seen already. */
972 cmd = get_keymap (cmd);
973 tem = Frassq (cmd, maps);
974 if (NILP (tem))
976 /* let elt be the event defined by this map entry. */
977 elt = XCONS (elt)->car;
979 /* If the last key in thisseq is meta-prefix-char, and
980 this entry is a binding for an ascii keystroke,
981 turn it into a meta-ized keystroke. */
982 if (is_metized && XTYPE (elt) == Lisp_Int)
984 tem = Fcopy_sequence (thisseq);
985 Faset (tem, last, make_number (XINT (elt) | 0200));
987 /* This new sequence is the same length as
988 thisseq, so stick it in the list right
989 after this one. */
990 XCONS (tail)->cdr =
991 Fcons (Fcons (tem, cmd), XCONS (tail)->cdr);
993 else
994 nconc2 (tail,
995 Fcons (Fcons (append_key (thisseq, elt), cmd),
996 Qnil));
1001 thismap = XCONS (thismap)->cdr;
1004 tail = Fcdr (tail);
1007 return maps;
1010 Lisp_Object Qsingle_key_description, Qkey_description;
1012 DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0,
1013 "Return a pretty description of key-sequence KEYS.\n\
1014 Control characters turn into \"C-foo\" sequences, meta into \"M-foo\"\n\
1015 spaces are put between sequence elements, etc.")
1016 (keys)
1017 Lisp_Object keys;
1019 return Fmapconcat (Qsingle_key_description, keys, build_string (" "));
1022 char *
1023 push_key_description (c, p)
1024 register unsigned int c;
1025 register char *p;
1027 if (c >= 0200)
1029 *p++ = 'M';
1030 *p++ = '-';
1031 c -= 0200;
1033 if (c < 040)
1035 if (c == 033)
1037 *p++ = 'E';
1038 *p++ = 'S';
1039 *p++ = 'C';
1041 else if (c == Ctl('I'))
1043 *p++ = 'T';
1044 *p++ = 'A';
1045 *p++ = 'B';
1047 else if (c == Ctl('J'))
1049 *p++ = 'L';
1050 *p++ = 'F';
1051 *p++ = 'D';
1053 else if (c == Ctl('M'))
1055 *p++ = 'R';
1056 *p++ = 'E';
1057 *p++ = 'T';
1059 else
1061 *p++ = 'C';
1062 *p++ = '-';
1063 if (c > 0 && c <= Ctl ('Z'))
1064 *p++ = c + 0140;
1065 else
1066 *p++ = c + 0100;
1069 else if (c == 0177)
1071 *p++ = 'D';
1072 *p++ = 'E';
1073 *p++ = 'L';
1075 else if (c == ' ')
1077 *p++ = 'S';
1078 *p++ = 'P';
1079 *p++ = 'C';
1081 else
1082 *p++ = c;
1084 return p;
1087 DEFUN ("single-key-description", Fsingle_key_description, Ssingle_key_description, 1, 1, 0,
1088 "Return a pretty description of command character KEY.\n\
1089 Control characters turn into C-whatever, etc.")
1090 (key)
1091 Lisp_Object key;
1093 register unsigned char c;
1094 char tem[6];
1096 if (EVENT_HAS_PARAMETERS (key))
1097 key = EVENT_HEAD (key);
1099 switch (XTYPE (key))
1101 case Lisp_Int: /* Normal character */
1102 c = XINT (key) & 0377;
1103 *push_key_description (c, tem) = 0;
1104 return build_string (tem);
1106 case Lisp_Symbol: /* Function key or event-symbol */
1107 return Fsymbol_name (key);
1109 default:
1110 error ("KEY must be an integer, cons, or symbol.");
1114 char *
1115 push_text_char_description (c, p)
1116 register unsigned int c;
1117 register char *p;
1119 if (c >= 0200)
1121 *p++ = 'M';
1122 *p++ = '-';
1123 c -= 0200;
1125 if (c < 040)
1127 *p++ = '^';
1128 *p++ = c + 64; /* 'A' - 1 */
1130 else if (c == 0177)
1132 *p++ = '^';
1133 *p++ = '?';
1135 else
1136 *p++ = c;
1137 return p;
1140 DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0,
1141 "Return a pretty description of file-character CHAR.\n\
1142 Control characters turn into \"^char\", etc.")
1143 (chr)
1144 Lisp_Object chr;
1146 char tem[6];
1148 CHECK_NUMBER (chr, 0);
1150 *push_text_char_description (XINT (chr) & 0377, tem) = 0;
1152 return build_string (tem);
1155 /* where-is - finding a command in a set of keymaps. */
1157 DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 5, 0,
1158 "Return list of keys that invoke DEFINITION in KEYMAP or KEYMAP1.\n\
1159 If KEYMAP is nil, search only KEYMAP1.\n\
1160 If KEYMAP1 is nil, use the current global map.\n\
1162 If optional 4th arg FIRSTONLY is non-nil,\n\
1163 return a string representing the first key sequence found,\n\
1164 rather than a list of all possible key sequences.\n\
1166 If optional 5th arg NOINDIRECT is non-nil, don't follow indirections\n\
1167 to other keymaps or slots. This makes it possible to search for an\n\
1168 indirect definition itself.")
1169 (definition, local_keymap, global_keymap, firstonly, noindirect)
1170 Lisp_Object definition, local_keymap, global_keymap;
1171 Lisp_Object firstonly, noindirect;
1173 register Lisp_Object maps;
1174 Lisp_Object found;
1176 if (NILP (global_keymap))
1177 global_keymap = current_global_map;
1179 if (!NILP (local_keymap))
1180 maps = nconc2 (Faccessible_keymaps (get_keymap (local_keymap)),
1181 Faccessible_keymaps (get_keymap (global_keymap)));
1182 else
1183 maps = Faccessible_keymaps (get_keymap (global_keymap));
1185 found = Qnil;
1187 for (; !NILP (maps); maps = Fcdr (maps))
1189 register this = Fcar (Fcar (maps)); /* Key sequence to reach map */
1190 register map = Fcdr (Fcar (maps)); /* The map that it reaches */
1191 register dense_alist;
1192 register int i = 0;
1194 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
1195 [M-CHAR] sequences, check if last character of the sequence
1196 is the meta-prefix char. */
1197 Lisp_Object last = make_number (XINT (Flength (this)) - 1);
1198 int last_is_meta = (XINT (last) >= 0
1199 && EQ (Faref (this, last), meta_prefix_char));
1201 /* Skip the 'keymap element of the list. */
1202 map = Fcdr (map);
1204 /* If the keymap is sparse, map traverses the alist to the end.
1206 If the keymap is dense, we set map to the vector and
1207 dense_alist to the assoc-list portion of the keymap. When we
1208 are finished dealing with the vector portion, we set map to
1209 dense_alist, and handle the rest like a sparse keymap. */
1210 if (XTYPE (XCONS (map)->car) == Lisp_Vector)
1212 dense_alist = XCONS (map)->cdr;
1213 map = XCONS (map)->car;
1216 while (1)
1218 register Lisp_Object key, binding, sequence;
1220 QUIT;
1221 if (XTYPE (map) == Lisp_Vector)
1223 /* In a vector, look at each element. */
1224 binding = XVECTOR (map)->contents[i];
1225 XFASTINT (key) = i;
1226 i++;
1228 /* If we've just finished scanning a vector, switch map to
1229 the assoc-list at the end of the vector. */
1230 if (i >= DENSE_TABLE_SIZE)
1231 map = dense_alist;
1233 else if (CONSP (map))
1235 /* In an alist, ignore elements that aren't conses. */
1236 if (! CONSP (XCONS (map)->car))
1238 /* Ignore other elements. */
1239 map = Fcdr (map);
1240 continue;
1242 binding = Fcdr (Fcar (map));
1243 key = Fcar (Fcar (map));
1244 map = Fcdr (map);
1246 else
1247 break;
1249 /* Search through indirections unless that's not wanted. */
1250 if (NILP (noindirect))
1251 binding = get_keyelt (binding);
1253 /* End this iteration if this element does not match
1254 the target. */
1256 if (XTYPE (definition) == Lisp_Cons)
1258 Lisp_Object tem;
1259 tem = Fequal (binding, definition);
1260 if (NILP (tem))
1261 continue;
1263 else
1264 if (!EQ (binding, definition))
1265 continue;
1267 /* We have found a match.
1268 Construct the key sequence where we found it. */
1269 if (XTYPE (key) == Lisp_Int && last_is_meta)
1271 sequence = Fcopy_sequence (this);
1272 Faset (sequence, last, make_number (XINT (key) | 0200));
1274 else
1275 sequence = append_key (this, key);
1277 /* Verify that this key binding is not shadowed by another
1278 binding for the same key, before we say it exists.
1280 Mechanism: look for local definition of this key and if
1281 it is defined and does not match what we found then
1282 ignore this key.
1284 Either nil or number as value from Flookup_key
1285 means undefined. */
1286 if (!NILP (local_keymap))
1288 binding = Flookup_key (local_keymap, sequence);
1289 if (!NILP (binding) && XTYPE (binding) != Lisp_Int)
1291 if (XTYPE (definition) == Lisp_Cons)
1293 Lisp_Object tem;
1294 tem = Fequal (binding, definition);
1295 if (NILP (tem))
1296 continue;
1298 else
1299 if (!EQ (binding, definition))
1300 continue;
1304 /* It is a true unshadowed match. Record it. */
1306 if (!NILP (firstonly))
1307 return sequence;
1308 found = Fcons (sequence, found);
1311 return Fnreverse (found);
1314 /* Return a string listing the keys and buttons that run DEFINITION. */
1316 static Lisp_Object
1317 where_is_string (definition)
1318 Lisp_Object definition;
1320 register Lisp_Object keys, keys1;
1322 keys = Fwhere_is_internal (definition,
1323 current_buffer->keymap, Qnil, Qnil, Qnil);
1324 keys1 = Fmapconcat (Qkey_description, keys, build_string (", "));
1326 return keys1;
1329 DEFUN ("where-is", Fwhere_is, Swhere_is, 1, 1, "CWhere is command: ",
1330 "Print message listing key sequences that invoke specified command.\n\
1331 Argument is a command definition, usually a symbol with a function definition.")
1332 (definition)
1333 Lisp_Object definition;
1335 register Lisp_Object string;
1337 CHECK_SYMBOL (definition, 0);
1338 string = where_is_string (definition);
1340 if (XSTRING (string)->size)
1341 message ("%s is on %s", XSYMBOL (definition)->name->data,
1342 XSTRING (string)->data);
1343 else
1344 message ("%s is not on any key", XSYMBOL (definition)->name->data);
1345 return Qnil;
1348 /* describe-bindings - summarizing all the bindings in a set of keymaps. */
1350 DEFUN ("describe-bindings", Fdescribe_bindings, Sdescribe_bindings, 0, 0, "",
1351 "Show a list of all defined keys, and their definitions.\n\
1352 The list is put in a buffer, which is displayed.")
1355 register Lisp_Object thisbuf;
1356 XSET (thisbuf, Lisp_Buffer, current_buffer);
1357 internal_with_output_to_temp_buffer ("*Help*",
1358 describe_buffer_bindings,
1359 thisbuf);
1360 return Qnil;
1363 static Lisp_Object
1364 describe_buffer_bindings (descbuf)
1365 Lisp_Object descbuf;
1367 register Lisp_Object start1, start2;
1369 char *heading
1370 = "key binding\n--- -------\n";
1372 Fset_buffer (Vstandard_output);
1375 int i, nmaps;
1376 Lisp_Object *modes, *maps;
1378 nmaps = current_minor_maps (&modes, &maps);
1379 for (i = 0; i < nmaps; i++)
1381 if (XTYPE (modes[i]) == Lisp_Symbol)
1383 insert_char ('`');
1384 insert_string (XSYMBOL (modes[i])->name->data);
1385 insert_char ('\'');
1387 else
1388 insert_string ("Strangely Named");
1389 insert_string (" Minor Mode Bindings:\n");
1390 insert_string (heading);
1391 describe_map_tree (maps[i], 0, Qnil);
1392 insert_char ('\n');
1396 start1 = XBUFFER (descbuf)->keymap;
1397 if (!NILP (start1))
1399 insert_string ("Local Bindings:\n");
1400 insert_string (heading);
1401 describe_map_tree (start1, 0, Qnil);
1402 insert_string ("\n");
1405 insert_string ("Global Bindings:\n");
1406 insert_string (heading);
1408 describe_map_tree (current_global_map, 0, XBUFFER (descbuf)->keymap);
1410 Fset_buffer (descbuf);
1411 return Qnil;
1414 /* Insert a desription of the key bindings in STARTMAP,
1415 followed by those of all maps reachable through STARTMAP.
1416 If PARTIAL is nonzero, omit certain "uninteresting" commands
1417 (such as `undefined').
1418 If SHADOW is non-nil, it is another map;
1419 don't mention keys which would be shadowed by it. */
1421 void
1422 describe_map_tree (startmap, partial, shadow)
1423 Lisp_Object startmap, shadow;
1424 int partial;
1426 register Lisp_Object elt, sh;
1427 Lisp_Object maps;
1428 struct gcpro gcpro1;
1430 maps = Faccessible_keymaps (startmap);
1431 GCPRO1 (maps);
1433 for (; !NILP (maps); maps = Fcdr (maps))
1435 elt = Fcar (maps);
1436 sh = Fcar (elt);
1438 /* If there is no shadow keymap given, don't shadow. */
1439 if (NILP (shadow))
1440 sh = Qnil;
1442 /* If the sequence by which we reach this keymap is zero-length,
1443 then the shadow map for this keymap is just SHADOW. */
1444 else if ((XTYPE (sh) == Lisp_String
1445 && XSTRING (sh)->size == 0)
1446 || (XTYPE (sh) == Lisp_Vector
1447 && XVECTOR (sh)->size == 0))
1448 sh = shadow;
1450 /* If the sequence by which we reach this keymap actually has
1451 some elements, then the sequence's definition in SHADOW is
1452 what we should use. */
1453 else
1455 sh = Flookup_key (shadow, Fcar (elt));
1456 if (XTYPE (sh) == Lisp_Int)
1457 sh = Qnil;
1460 /* If sh is null (meaning that the current map is not shadowed),
1461 or a keymap (meaning that bindings from the current map might
1462 show through), describe the map. Otherwise, sh is a command
1463 that completely shadows the current map, and we shouldn't
1464 bother. */
1465 if (NILP (sh) || !NILP (Fkeymapp (sh)))
1466 describe_map (Fcdr (elt), Fcar (elt), partial, sh);
1469 UNGCPRO;
1472 static void
1473 describe_command (definition)
1474 Lisp_Object definition;
1476 register Lisp_Object tem1;
1478 Findent_to (make_number (16), make_number (1));
1480 if (XTYPE (definition) == Lisp_Symbol)
1482 XSET (tem1, Lisp_String, XSYMBOL (definition)->name);
1483 insert1 (tem1);
1484 insert_string ("\n");
1486 else
1488 tem1 = Fkeymapp (definition);
1489 if (!NILP (tem1))
1490 insert_string ("Prefix Command\n");
1491 else
1492 insert_string ("??\n");
1496 /* Describe the contents of map MAP, assuming that this map itself is
1497 reached by the sequence of prefix keys KEYS (a string or vector).
1498 PARTIAL, SHADOW is as in `describe_map_tree' above. */
1500 static void
1501 describe_map (map, keys, partial, shadow)
1502 Lisp_Object map, keys;
1503 int partial;
1504 Lisp_Object shadow;
1506 register Lisp_Object keysdesc;
1508 if (!NILP (keys) && Flength (keys) > 0)
1509 keysdesc = concat2 (Fkey_description (keys),
1510 build_string (" "));
1511 else
1512 keysdesc = Qnil;
1514 /* Skip the 'keymap element of the list. */
1515 map = Fcdr (map);
1517 /* If this is a dense keymap, take care of the table. */
1518 if (CONSP (map)
1519 && XTYPE (XCONS (map)->car) == Lisp_Vector)
1521 describe_vector (XCONS (map)->car, keysdesc, describe_command,
1522 partial, shadow);
1523 map = XCONS (map)->cdr;
1526 /* Now map is an alist. */
1527 describe_alist (map, keysdesc, describe_command, partial, shadow);
1530 /* Insert a description of ALIST into the current buffer.
1531 Note that ALIST is just a plain association list, not a keymap. */
1533 static void
1534 describe_alist (alist, elt_prefix, elt_describer, partial, shadow)
1535 register Lisp_Object alist;
1536 Lisp_Object elt_prefix;
1537 int (*elt_describer) ();
1538 int partial;
1539 Lisp_Object shadow;
1541 Lisp_Object this;
1542 Lisp_Object tem1, tem2 = Qnil;
1543 Lisp_Object suppress;
1544 Lisp_Object kludge;
1545 int first = 1;
1546 struct gcpro gcpro1, gcpro2, gcpro3;
1548 if (partial)
1549 suppress = intern ("suppress-keymap");
1551 /* This vector gets used to present single keys to Flookup_key. Since
1552 that is done once per alist element, we don't want to cons up a
1553 fresh vector every time. */
1554 kludge = Fmake_vector (make_number (1), Qnil);
1556 GCPRO3 (elt_prefix, tem2, kludge);
1558 for (; CONSP (alist); alist = Fcdr (alist))
1560 QUIT;
1561 tem1 = Fcar_safe (Fcar (alist));
1562 tem2 = get_keyelt (Fcdr_safe (Fcar (alist)));
1564 /* Don't show undefined commands or suppressed commands. */
1565 if (NILP (tem2)) continue;
1566 if (XTYPE (tem2) == Lisp_Symbol && partial)
1568 this = Fget (tem2, suppress);
1569 if (!NILP (this))
1570 continue;
1573 /* Don't show a command that isn't really visible
1574 because a local definition of the same key shadows it. */
1576 if (!NILP (shadow))
1578 Lisp_Object tem;
1580 XVECTOR (kludge)->contents[0] = tem1;
1581 tem = Flookup_key (shadow, kludge);
1582 if (!NILP (tem)) continue;
1585 if (first)
1587 insert ("\n", 1);
1588 first = 0;
1591 if (!NILP (elt_prefix))
1592 insert1 (elt_prefix);
1594 /* THIS gets the string to describe the character TEM1. */
1595 this = Fsingle_key_description (tem1);
1596 insert1 (this);
1598 /* Print a description of the definition of this character.
1599 elt_describer will take care of spacing out far enough
1600 for alignment purposes. */
1601 (*elt_describer) (tem2);
1604 UNGCPRO;
1607 static int
1608 describe_vector_princ (elt)
1609 Lisp_Object elt;
1611 Fprinc (elt, Qnil);
1614 DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 1, 0,
1615 "Print on `standard-output' a description of contents of VECTOR.\n\
1616 This is text showing the elements of vector matched against indices.")
1617 (vector)
1618 Lisp_Object vector;
1620 CHECK_VECTOR (vector, 0);
1621 describe_vector (vector, Qnil, describe_vector_princ, 0, Qnil, Qnil);
1624 describe_vector (vector, elt_prefix, elt_describer, partial, shadow)
1625 register Lisp_Object vector;
1626 Lisp_Object elt_prefix;
1627 int (*elt_describer) ();
1628 int partial;
1629 Lisp_Object shadow;
1631 Lisp_Object this;
1632 Lisp_Object dummy;
1633 Lisp_Object tem1, tem2;
1634 register int i;
1635 Lisp_Object suppress;
1636 Lisp_Object kludge;
1637 int first = 1;
1638 struct gcpro gcpro1, gcpro2, gcpro3;
1640 tem1 = Qnil;
1642 /* This vector gets used to present single keys to Flookup_key. Since
1643 that is done once per vector element, we don't want to cons up a
1644 fresh vector every time. */
1645 kludge = Fmake_vector (make_number (1), Qnil);
1646 GCPRO3 (elt_prefix, tem1, kludge);
1648 if (partial)
1649 suppress = intern ("suppress-keymap");
1651 for (i = 0; i < DENSE_TABLE_SIZE; i++)
1653 QUIT;
1654 tem1 = get_keyelt (XVECTOR (vector)->contents[i]);
1656 if (NILP (tem1)) continue;
1658 /* Don't mention suppressed commands. */
1659 if (XTYPE (tem1) == Lisp_Symbol && partial)
1661 this = Fget (tem1, suppress);
1662 if (!NILP (this))
1663 continue;
1666 /* If this command in this map is shadowed by some other map,
1667 ignore it. */
1668 if (!NILP (shadow))
1670 Lisp_Object tem;
1672 XVECTOR (kludge)->contents[0] = make_number (i);
1673 tem = Flookup_key (shadow, kludge);
1675 if (!NILP (tem)) continue;
1678 if (first)
1680 insert ("\n", 1);
1681 first = 0;
1684 /* Output the prefix that applies to every entry in this map. */
1685 if (!NILP (elt_prefix))
1686 insert1 (elt_prefix);
1688 /* Get the string to describe the character I, and print it. */
1689 XFASTINT (dummy) = i;
1691 /* THIS gets the string to describe the character DUMMY. */
1692 this = Fsingle_key_description (dummy);
1693 insert1 (this);
1695 /* Find all consecutive characters that have the same definition. */
1696 while (i + 1 < DENSE_TABLE_SIZE
1697 && (tem2 = get_keyelt (XVECTOR (vector)->contents[i+1]),
1698 EQ (tem2, tem1)))
1699 i++;
1701 /* If we have a range of more than one character,
1702 print where the range reaches to. */
1704 if (i != XINT (dummy))
1706 insert (" .. ", 4);
1707 if (!NILP (elt_prefix))
1708 insert1 (elt_prefix);
1710 XFASTINT (dummy) = i;
1711 insert1 (Fsingle_key_description (dummy));
1714 /* Print a description of the definition of this character.
1715 elt_describer will take care of spacing out far enough
1716 for alignment purposes. */
1717 (*elt_describer) (tem1);
1720 UNGCPRO;
1723 /* Apropos - finding all symbols whose names match a regexp. */
1724 Lisp_Object apropos_predicate;
1725 Lisp_Object apropos_accumulate;
1727 static void
1728 apropos_accum (symbol, string)
1729 Lisp_Object symbol, string;
1731 register Lisp_Object tem;
1733 tem = Fstring_match (string, Fsymbol_name (symbol), Qnil);
1734 if (!NILP (tem) && !NILP (apropos_predicate))
1735 tem = call1 (apropos_predicate, symbol);
1736 if (!NILP (tem))
1737 apropos_accumulate = Fcons (symbol, apropos_accumulate);
1740 DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0,
1741 "Show all symbols whose names contain match for REGEXP.\n\
1742 If optional 2nd arg PRED is non-nil, (funcall PRED SYM) is done\n\
1743 for each symbol and a symbol is mentioned only if that returns non-nil.\n\
1744 Return list of symbols found.")
1745 (string, pred)
1746 Lisp_Object string, pred;
1748 struct gcpro gcpro1, gcpro2;
1749 CHECK_STRING (string, 0);
1750 apropos_predicate = pred;
1751 GCPRO2 (apropos_predicate, apropos_accumulate);
1752 apropos_accumulate = Qnil;
1753 map_obarray (Vobarray, apropos_accum, string);
1754 apropos_accumulate = Fsort (apropos_accumulate, Qstring_lessp);
1755 UNGCPRO;
1756 return apropos_accumulate;
1759 syms_of_keymap ()
1761 Lisp_Object tem;
1763 Qkeymap = intern ("keymap");
1764 staticpro (&Qkeymap);
1766 /* Initialize the keymaps standardly used.
1767 Each one is the value of a Lisp variable, and is also
1768 pointed to by a C variable */
1770 global_map = Fmake_keymap ();
1771 Fset (intern ("global-map"), global_map);
1773 meta_map = Fmake_keymap ();
1774 Fset (intern ("esc-map"), meta_map);
1775 Ffset (intern ("ESC-prefix"), meta_map);
1777 control_x_map = Fmake_keymap ();
1778 Fset (intern ("ctl-x-map"), control_x_map);
1779 Ffset (intern ("Control-X-prefix"), control_x_map);
1781 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map,
1782 "Default keymap to use when reading from the minibuffer.");
1783 Vminibuffer_local_map = Fmake_sparse_keymap ();
1785 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map,
1786 "Local keymap for the minibuffer when spaces are not allowed.");
1787 Vminibuffer_local_ns_map = Fmake_sparse_keymap ();
1789 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map,
1790 "Local keymap for minibuffer input with completion.");
1791 Vminibuffer_local_completion_map = Fmake_sparse_keymap ();
1793 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map,
1794 "Local keymap for minibuffer input with completion, for exact match.");
1795 Vminibuffer_local_must_match_map = Fmake_sparse_keymap ();
1797 current_global_map = global_map;
1799 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist,
1800 "Alist of keymaps to use for minor modes.\n\
1801 Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read\n\
1802 key sequences and look up bindings iff VARIABLE's value is non-nil.\n\
1803 If two active keymaps bind the same key, the keymap appearing earlier\n\
1804 in the list takes precedence.");
1805 Vminor_mode_map_alist = Qnil;
1807 DEFVAR_LISP ("function-key-map", &Vfunction_key_map,
1808 "Keymap mapping ASCII function key sequences onto their preferred forms.\n\
1809 This allows Emacs to recognize function keys sent from ASCII\n\
1810 terminals at any point in a key sequence.\n\
1812 The read-key-sequence function replaces subsequences bound by\n\
1813 function-key-map with their bindings. When the current local and global\n\
1814 keymaps have no binding for the current key sequence but\n\
1815 function-key-map binds a suffix of the sequence to a vector,\n\
1816 read-key-sequence replaces the matching suffix with its binding, and\n\
1817 continues with the new sequence.\n\
1819 For example, suppose function-key-map binds `ESC O P' to [pf1].\n\
1820 Typing `ESC O P' to read-key-sequence would return [pf1]. Typing\n\
1821 `C-x ESC O P' would return [?\C-x pf1]. If [pf1] were a prefix\n\
1822 key, typing `ESC O P x' would return [pf1 x].");
1823 Vfunction_key_map = Fmake_sparse_keymap ();
1825 Qsingle_key_description = intern ("single-key-description");
1826 staticpro (&Qsingle_key_description);
1828 Qkey_description = intern ("key-description");
1829 staticpro (&Qkey_description);
1831 Qkeymapp = intern ("keymapp");
1832 staticpro (&Qkeymapp);
1834 defsubr (&Skeymapp);
1835 defsubr (&Smake_keymap);
1836 defsubr (&Smake_sparse_keymap);
1837 defsubr (&Scopy_keymap);
1838 defsubr (&Skey_binding);
1839 defsubr (&Slocal_key_binding);
1840 defsubr (&Sglobal_key_binding);
1841 defsubr (&Sminor_mode_key_binding);
1842 defsubr (&Sglobal_set_key);
1843 defsubr (&Slocal_set_key);
1844 defsubr (&Sdefine_key);
1845 defsubr (&Slookup_key);
1846 defsubr (&Sglobal_unset_key);
1847 defsubr (&Slocal_unset_key);
1848 defsubr (&Sdefine_prefix_command);
1849 defsubr (&Suse_global_map);
1850 defsubr (&Suse_local_map);
1851 defsubr (&Scurrent_local_map);
1852 defsubr (&Scurrent_global_map);
1853 defsubr (&Scurrent_minor_mode_maps);
1854 defsubr (&Saccessible_keymaps);
1855 defsubr (&Skey_description);
1856 defsubr (&Sdescribe_vector);
1857 defsubr (&Ssingle_key_description);
1858 defsubr (&Stext_char_description);
1859 defsubr (&Swhere_is_internal);
1860 defsubr (&Swhere_is);
1861 defsubr (&Sdescribe_bindings);
1862 defsubr (&Sapropos_internal);
1865 keys_of_keymap ()
1867 Lisp_Object tem;
1869 initial_define_key (global_map, 033, "ESC-prefix");
1870 initial_define_key (global_map, Ctl('X'), "Control-X-prefix");