* cmds.c (overwrite_binary_mode): Deleted; this implements the
[bpt/emacs.git] / src / keymap.c
CommitLineData
2c6f1a39 1/* Manipulation of keymaps
502ddf23 2 Copyright (C) 1985, 1986, 1987, 1988, 1992 Free Software Foundation, Inc.
2c6f1a39
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
502ddf23 8the Free Software Foundation; either version 2, or (at your option)
2c6f1a39
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include "config.h"
22#include <stdio.h>
23#undef NULL
24#include "lisp.h"
25#include "commands.h"
26#include "buffer.h"
6bbbd9b0 27#include "keyboard.h"
6ba6e250 28#include "termhooks.h"
2c6f1a39
JB
29
30#define min(a, b) ((a) < (b) ? (a) : (b))
31
f5b79c1c 32/* The number of elements in keymap vectors. */
2c6f1a39
JB
33#define DENSE_TABLE_SIZE (0200)
34
35/* Actually allocate storage for these variables */
36
37Lisp_Object current_global_map; /* Current global keymap */
38
39Lisp_Object global_map; /* default global key bindings */
40
41Lisp_Object meta_map; /* The keymap used for globally bound
42 ESC-prefixed default commands */
43
44Lisp_Object control_x_map; /* The keymap used for globally bound
45 C-x-prefixed default commands */
46
47/* was MinibufLocalMap */
48Lisp_Object Vminibuffer_local_map;
49 /* The keymap used by the minibuf for local
50 bindings when spaces are allowed in the
51 minibuf */
52
53/* was MinibufLocalNSMap */
54Lisp_Object Vminibuffer_local_ns_map;
55 /* The keymap used by the minibuf for local
56 bindings when spaces are not encouraged
57 in the minibuf */
58
59/* keymap used for minibuffers when doing completion */
60/* was MinibufLocalCompletionMap */
61Lisp_Object Vminibuffer_local_completion_map;
62
63/* keymap used for minibuffers when doing completion and require a match */
64/* was MinibufLocalMustMatchMap */
65Lisp_Object Vminibuffer_local_must_match_map;
66
cc0a8174
JB
67/* Alist of minor mode variables and keymaps. */
68Lisp_Object Vminor_mode_map_alist;
69
6bbbd9b0
JB
70/* Keymap mapping ASCII function key sequences onto their preferred forms.
71 Initialized by the terminal-specific lisp files. See DEFVAR for more
72 documentation. */
73Lisp_Object Vfunction_key_map;
74
2c6f1a39
JB
75Lisp_Object Qkeymapp, Qkeymap;
76
77/* A char over 0200 in a key sequence
78 is equivalent to prefixing with this character. */
79
80extern Lisp_Object meta_prefix_char;
81
82void describe_map_tree ();
83static Lisp_Object describe_buffer_bindings ();
84static void describe_command ();
85static void describe_map ();
f5b79c1c 86static void describe_map_2 ();
2c6f1a39 87\f
cc0a8174
JB
88/* Keymap object support - constructors and predicates. */
89
ce6e5d0b 90DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 1, 0,
2c6f1a39
JB
91 "Construct and return a new keymap, of the form (keymap VECTOR . ALIST).\n\
92VECTOR is a 128-element vector which holds the bindings for the ASCII\n\
93characters. ALIST is an assoc-list which holds bindings for function keys,\n\
94mouse events, and any other things that appear in the input stream.\n\
ce6e5d0b
RS
95All entries in it are initially nil, meaning \"command undefined\".\n\n\
96The optional arg STRING supplies a menu name for the keymap\n\
97in case you use it as a menu with `x-popup-menu'.")
98 (string)
99 Lisp_Object string;
2c6f1a39 100{
ce6e5d0b
RS
101 Lisp_Object tail;
102 if (!NILP (string))
103 tail = Fcons (string, Qnil);
104 else
105 tail = Qnil;
2c6f1a39
JB
106 return Fcons (Qkeymap,
107 Fcons (Fmake_vector (make_number (DENSE_TABLE_SIZE), Qnil),
ce6e5d0b 108 tail));
2c6f1a39
JB
109}
110
ce6e5d0b 111DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 1, 0,
2c6f1a39
JB
112 "Construct and return a new sparse-keymap list.\n\
113Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),\n\
114which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),\n\
115which binds the function key or mouse event SYMBOL to DEFINITION.\n\
ce6e5d0b
RS
116Initially the alist is nil.\n\n\
117The optional arg STRING supplies a menu name for the keymap\n\
118in case you use it as a menu with `x-popup-menu'.")
119 (string)
120 Lisp_Object string;
2c6f1a39 121{
ce6e5d0b
RS
122 if (!NILP (string))
123 return Fcons (Qkeymap, Fcons (string, Qnil));
2c6f1a39
JB
124 return Fcons (Qkeymap, Qnil);
125}
126
127/* This function is used for installing the standard key bindings
128 at initialization time.
129
130 For example:
131
e25c4e44 132 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); */
2c6f1a39
JB
133
134void
135initial_define_key (keymap, key, defname)
136 Lisp_Object keymap;
137 int key;
138 char *defname;
139{
140 store_in_keymap (keymap, make_number (key), intern (defname));
141}
142
e25c4e44
JB
143void
144initial_define_lispy_key (keymap, keyname, defname)
145 Lisp_Object keymap;
146 char *keyname;
147 char *defname;
148{
149 store_in_keymap (keymap, intern (keyname), intern (defname));
150}
151
2c6f1a39
JB
152/* Define character fromchar in map frommap as an alias for character
153 tochar in map tomap. Subsequent redefinitions of the latter WILL
154 affect the former. */
155
156#if 0
157void
158synkey (frommap, fromchar, tomap, tochar)
159 struct Lisp_Vector *frommap, *tomap;
160 int fromchar, tochar;
161{
162 Lisp_Object v, c;
163 XSET (v, Lisp_Vector, tomap);
164 XFASTINT (c) = tochar;
165 frommap->contents[fromchar] = Fcons (v, c);
166}
167#endif /* 0 */
168
169DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0,
170 "Return t if ARG is a keymap.\n\
1d8d96fa
JB
171\n\
172A keymap is list (keymap . ALIST), a list (keymap VECTOR . ALIST),\n\
173or a symbol whose function definition is a keymap is itself a keymap.\n\
174ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);\n\
175VECTOR is a 128-element vector of bindings for ASCII characters.")
2c6f1a39
JB
176 (object)
177 Lisp_Object object;
178{
d09b2024 179 return (NILP (get_keymap_1 (object, 0, 0)) ? Qnil : Qt);
2c6f1a39
JB
180}
181
182/* Check that OBJECT is a keymap (after dereferencing through any
d09b2024
JB
183 symbols). If it is, return it.
184
185 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
186 is an autoload form, do the autoload and try again.
187
188 ERROR controls how we respond if OBJECT isn't a keymap.
189 If ERROR is non-zero, signal an error; otherwise, just return Qnil.
190
191 Note that most of the time, we don't want to pursue autoloads.
192 Functions like Faccessible_keymaps which scan entire keymap trees
193 shouldn't load every autoloaded keymap. I'm not sure about this,
194 but it seems to me that only read_key_sequence, Flookup_key, and
195 Fdefine_key should cause keymaps to be autoloaded. */
196
2c6f1a39 197Lisp_Object
d09b2024 198get_keymap_1 (object, error, autoload)
2c6f1a39 199 Lisp_Object object;
d09b2024 200 int error, autoload;
2c6f1a39 201{
d09b2024 202 Lisp_Object tem;
2c6f1a39 203
d09b2024 204 autoload_retry:
502ddf23 205 tem = indirect_function (object);
2c6f1a39
JB
206 if (CONSP (tem) && EQ (XCONS (tem)->car, Qkeymap))
207 return tem;
f5b79c1c 208
8e4dfd54
JB
209 /* Should we do an autoload? Autoload forms for keymaps have
210 Qkeymap as their fifth element. */
d09b2024
JB
211 if (autoload
212 && XTYPE (object) == Lisp_Symbol
213 && CONSP (tem)
214 && EQ (XCONS (tem)->car, Qautoload))
215 {
8e4dfd54 216 Lisp_Object tail;
d09b2024 217
8e4dfd54
JB
218 tail = Fnth (make_number (4), tem);
219 if (EQ (tail, Qkeymap))
220 {
221 struct gcpro gcpro1, gcpro2;
d09b2024 222
8e4dfd54
JB
223 GCPRO2 (tem, object)
224 do_autoload (tem, object);
225 UNGCPRO;
226
227 goto autoload_retry;
228 }
d09b2024
JB
229 }
230
2c6f1a39
JB
231 if (error)
232 wrong_type_argument (Qkeymapp, object);
cc0a8174
JB
233 else
234 return Qnil;
2c6f1a39
JB
235}
236
d09b2024
JB
237
238/* Follow any symbol chaining, and return the keymap denoted by OBJECT.
239 If OBJECT doesn't denote a keymap at all, signal an error. */
2c6f1a39
JB
240Lisp_Object
241get_keymap (object)
242 Lisp_Object object;
243{
d09b2024 244 return get_keymap_1 (object, 0, 0);
2c6f1a39
JB
245}
246
247
2c6f1a39 248/* Look up IDX in MAP. IDX may be any sort of event.
f5b79c1c 249 Note that this does only one level of lookup; IDX must be a single
e25c4e44
JB
250 event, not a sequence.
251
252 If T_OK is non-zero, bindings for Qt are treated as default
253 bindings; any key left unmentioned by other tables and bindings is
254 given the binding of Qt.
255
256 If T_OK is zero, bindings for Qt are not treated specially. */
2c6f1a39
JB
257
258Lisp_Object
e25c4e44 259access_keymap (map, idx, t_ok)
2c6f1a39
JB
260 Lisp_Object map;
261 Lisp_Object idx;
e25c4e44 262 int t_ok;
2c6f1a39
JB
263{
264 /* If idx is a list (some sort of mouse click, perhaps?),
265 the index we want to use is the car of the list, which
266 ought to be a symbol. */
cebd887d 267 idx = EVENT_HEAD (idx);
2c6f1a39 268
f5b79c1c
JB
269 /* If idx is a symbol, it might have modifiers, which need to
270 be put in the canonical order. */
0b8fc2d4 271 if (XTYPE (idx) == Lisp_Symbol)
f5b79c1c 272 idx = reorder_modifiers (idx);
2c6f1a39 273
f5b79c1c
JB
274 {
275 Lisp_Object tail;
e25c4e44 276 Lisp_Object t_binding = Qnil;
2c6f1a39 277
f5b79c1c 278 for (tail = map; CONSP (tail); tail = XCONS (tail)->cdr)
2c6f1a39 279 {
f5b79c1c
JB
280 Lisp_Object binding = XCONS (tail)->car;
281
282 switch (XTYPE (binding))
283 {
284 case Lisp_Cons:
285 if (EQ (XCONS (binding)->car, idx))
286 return XCONS (binding)->cdr;
e25c4e44
JB
287 if (t_ok && EQ (XCONS (binding)->car, Qt))
288 t_binding = XCONS (binding)->cdr;
f5b79c1c
JB
289 break;
290
291 case Lisp_Vector:
292 if (XVECTOR (binding)->size == DENSE_TABLE_SIZE
0b8fc2d4
RS
293 && XTYPE (idx) == Lisp_Int
294 && XINT (idx) >= 0
295 && XINT (idx) < DENSE_TABLE_SIZE)
f5b79c1c
JB
296 return XVECTOR (binding)->contents[XINT (idx)];
297 break;
298 }
20218e2f
JB
299
300 QUIT;
2c6f1a39 301 }
fde3a52f 302
e25c4e44
JB
303 return t_binding;
304 }
2c6f1a39
JB
305}
306
307/* Given OBJECT which was found in a slot in a keymap,
308 trace indirect definitions to get the actual definition of that slot.
309 An indirect definition is a list of the form
310 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
311 and INDEX is the object to look up in KEYMAP to yield the definition.
312
313 Also if OBJECT has a menu string as the first element,
1a8c3f10 314 remove that. Also remove a menu help string as second element. */
2c6f1a39
JB
315
316Lisp_Object
317get_keyelt (object)
318 register Lisp_Object object;
319{
320 while (1)
321 {
322 register Lisp_Object map, tem;
323
fde3a52f 324 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
d09b2024 325 map = get_keymap_1 (Fcar_safe (object), 0, 0);
2c6f1a39 326 tem = Fkeymapp (map);
265a9e55 327 if (!NILP (tem))
e25c4e44 328 object = access_keymap (map, Fcdr (object), 0);
2c6f1a39
JB
329
330 /* If the keymap contents looks like (STRING . DEFN),
331 use DEFN.
332 Keymap alist elements like (CHAR MENUSTRING . DEFN)
333 will be used by HierarKey menus. */
334 else if (XTYPE (object) == Lisp_Cons
335 && XTYPE (XCONS (object)->car) == Lisp_String)
1a8c3f10
RS
336 {
337 object = XCONS (object)->cdr;
338 /* Also remove a menu help string, if any,
339 following the menu item name. */
340 if (XTYPE (object) == Lisp_Cons
341 && XTYPE (XCONS (object)->car) == Lisp_String)
342 object = XCONS (object)->cdr;
343 }
2c6f1a39
JB
344
345 else
346 /* Anything else is really the value. */
347 return object;
348 }
349}
350
351Lisp_Object
352store_in_keymap (keymap, idx, def)
353 Lisp_Object keymap;
354 register Lisp_Object idx;
355 register Lisp_Object def;
356{
f5b79c1c
JB
357 if (XTYPE (keymap) != Lisp_Cons
358 || ! EQ (XCONS (keymap)->car, Qkeymap))
359 error ("attempt to define a key in a non-keymap");
360
2c6f1a39
JB
361 /* If idx is a list (some sort of mouse click, perhaps?),
362 the index we want to use is the car of the list, which
363 ought to be a symbol. */
cebd887d 364 idx = EVENT_HEAD (idx);
2c6f1a39 365
f5b79c1c
JB
366 /* If idx is a symbol, it might have modifiers, which need to
367 be put in the canonical order. */
0b8fc2d4 368 if (XTYPE (idx) == Lisp_Symbol)
f5b79c1c
JB
369 idx = reorder_modifiers (idx);
370
371
372 /* Scan the keymap for a binding of idx. */
2c6f1a39 373 {
f5b79c1c 374 Lisp_Object tail;
2c6f1a39 375
f5b79c1c
JB
376 /* The cons after which we should insert new bindings. If the
377 keymap has a table element, we record its position here, so new
378 bindings will go after it; this way, the table will stay
379 towards the front of the alist and character lookups in dense
380 keymaps will remain fast. Otherwise, this just points at the
381 front of the keymap. */
382 Lisp_Object insertion_point = keymap;
2c6f1a39 383
f5b79c1c 384 for (tail = XCONS (keymap)->cdr; CONSP (tail); tail = XCONS (tail)->cdr)
2c6f1a39 385 {
f5b79c1c
JB
386 Lisp_Object elt = XCONS (tail)->car;
387
388 switch (XTYPE (elt))
389 {
390 case Lisp_Vector:
0188441d
JB
391 if (XVECTOR (elt)->size != DENSE_TABLE_SIZE)
392 break;
0b8fc2d4
RS
393 if (XTYPE (idx) == Lisp_Int
394 && XINT (idx) >= 0 && XINT (idx) < DENSE_TABLE_SIZE)
f5b79c1c
JB
395 {
396 XVECTOR (elt)->contents[XFASTINT (idx)] = def;
397 return def;
398 }
399 insertion_point = tail;
400 break;
401
402 case Lisp_Cons:
403 if (EQ (idx, XCONS (elt)->car))
404 {
405 XCONS (elt)->cdr = def;
406 return def;
407 }
408 break;
409
410 case Lisp_Symbol:
411 /* If we find a 'keymap' symbol in the spine of KEYMAP,
412 then we must have found the start of a second keymap
413 being used as the tail of KEYMAP, and a binding for IDX
414 should be inserted before it. */
415 if (EQ (elt, Qkeymap))
416 goto keymap_end;
417 break;
418 }
0188441d
JB
419
420 QUIT;
2c6f1a39 421 }
2c6f1a39 422
f5b79c1c
JB
423 keymap_end:
424 /* We have scanned the entire keymap, and not found a binding for
425 IDX. Let's add one. */
426 XCONS (insertion_point)->cdr =
427 Fcons (Fcons (idx, def), XCONS (insertion_point)->cdr);
428 }
429
2c6f1a39
JB
430 return def;
431}
432
f5b79c1c 433
2c6f1a39
JB
434DEFUN ("copy-keymap", Fcopy_keymap, Scopy_keymap, 1, 1, 0,
435 "Return a copy of the keymap KEYMAP.\n\
436The copy starts out with the same definitions of KEYMAP,\n\
437but changing either the copy or KEYMAP does not affect the other.\n\
1d8d96fa
JB
438Any key definitions that are subkeymaps are recursively copied.\n\
439However, a key definition which is a symbol whose definition is a keymap\n\
440is not copied.")
2c6f1a39
JB
441 (keymap)
442 Lisp_Object keymap;
443{
444 register Lisp_Object copy, tail;
445
446 copy = Fcopy_alist (get_keymap (keymap));
2c6f1a39 447
f5b79c1c 448 for (tail = copy; CONSP (tail); tail = XCONS (tail)->cdr)
2c6f1a39 449 {
f5b79c1c 450 Lisp_Object elt = XCONS (tail)->car;
2c6f1a39 451
f5b79c1c
JB
452 if (XTYPE (elt) == Lisp_Vector
453 && XVECTOR (elt)->size == DENSE_TABLE_SIZE)
2c6f1a39 454 {
f5b79c1c 455 int i;
2c6f1a39 456
f5b79c1c
JB
457 elt = Fcopy_sequence (elt);
458 XCONS (tail)->car = elt;
2c6f1a39
JB
459
460 for (i = 0; i < DENSE_TABLE_SIZE; i++)
f5b79c1c
JB
461 if (XTYPE (XVECTOR (elt)->contents[i]) != Lisp_Symbol
462 && Fkeymapp (XVECTOR (elt)->contents[i]))
463 XVECTOR (elt)->contents[i] =
464 Fcopy_keymap (XVECTOR (elt)->contents[i]);
2c6f1a39 465 }
f5b79c1c
JB
466 else if (CONSP (elt)
467 && XTYPE (XCONS (elt)->cdr) != Lisp_Symbol
468 && ! NILP (Fkeymapp (XCONS (elt)->cdr)))
2c6f1a39 469 XCONS (elt)->cdr = Fcopy_keymap (XCONS (elt)->cdr);
2c6f1a39
JB
470 }
471
472 return copy;
473}
474\f
cc0a8174
JB
475/* Simple Keymap mutators and accessors. */
476
2c6f1a39
JB
477DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0,
478 "Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.\n\
479KEYMAP is a keymap. KEY is a string or a vector of symbols and characters\n\
480meaning a sequence of keystrokes and events.\n\
481DEF is anything that can be a key's definition:\n\
482 nil (means key is undefined in this keymap),\n\
483 a command (a Lisp function suitable for interactive calling)\n\
484 a string (treated as a keyboard macro),\n\
485 a keymap (to define a prefix key),\n\
486 a symbol. When the key is looked up, the symbol will stand for its\n\
487 function definition, which should at that time be one of the above,\n\
488 or another symbol whose function definition is used, etc.\n\
489 a cons (STRING . DEFN), meaning that DEFN is the definition\n\
490 (DEFN should be a valid definition in its own right),\n\
6e8290aa
JB
491 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.\n\
492\n\
493If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at\n\
494the front of KEYMAP.")
2c6f1a39 495 (keymap, key, def)
d09b2024 496 Lisp_Object keymap;
2c6f1a39
JB
497 Lisp_Object key;
498 Lisp_Object def;
499{
500 register int idx;
501 register Lisp_Object c;
502 register Lisp_Object tem;
503 register Lisp_Object cmd;
504 int metized = 0;
6ba6e250 505 int meta_bit;
2c6f1a39 506 int length;
d09b2024 507 struct gcpro gcpro1, gcpro2, gcpro3;
2c6f1a39
JB
508
509 keymap = get_keymap (keymap);
510
511 if (XTYPE (key) != Lisp_Vector
512 && XTYPE (key) != Lisp_String)
513 key = wrong_type_argument (Qarrayp, key);
514
d09b2024 515 length = XFASTINT (Flength (key));
2c6f1a39
JB
516 if (length == 0)
517 return Qnil;
518
d09b2024
JB
519 GCPRO3 (keymap, key, def);
520
6ba6e250
RS
521 if (XTYPE (key) == Lisp_Vector)
522 meta_bit = meta_modifier;
523 else
524 meta_bit = 0x80;
525
2c6f1a39
JB
526 idx = 0;
527 while (1)
528 {
529 c = Faref (key, make_number (idx));
530
531 if (XTYPE (c) == Lisp_Int
6ba6e250 532 && (XINT (c) & meta_bit)
2c6f1a39
JB
533 && !metized)
534 {
535 c = meta_prefix_char;
536 metized = 1;
537 }
538 else
539 {
540 if (XTYPE (c) == Lisp_Int)
0b8fc2d4 541 XSETINT (c, XINT (c) & ~meta_bit);
2c6f1a39
JB
542
543 metized = 0;
544 idx++;
545 }
546
547 if (idx == length)
d09b2024 548 RETURN_UNGCPRO (store_in_keymap (keymap, c, def));
2c6f1a39 549
e25c4e44 550 cmd = get_keyelt (access_keymap (keymap, c, 0));
2c6f1a39 551
265a9e55 552 if (NILP (cmd))
2c6f1a39 553 {
ce6e5d0b 554 cmd = Fmake_sparse_keymap (Qnil);
2c6f1a39
JB
555 store_in_keymap (keymap, c, cmd);
556 }
557
d09b2024
JB
558 keymap = get_keymap_1 (cmd, 0, 1);
559 if (NILP (keymap))
dbc4e1c1
JB
560 {
561 /* We must use Fkey_description rather than just passing key to
562 error; key might be a vector, not a string. */
563 Lisp_Object description = Fkey_description (key);
564
565 error ("Key sequence %s uses invalid prefix characters",
566 XSTRING (description)->data);
567 }
2c6f1a39
JB
568 }
569}
570
571/* Value is number if KEY is too long; NIL if valid but has no definition. */
572
7c140252 573DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 3, 0,
2c6f1a39
JB
574 "In keymap KEYMAP, look up key sequence KEY. Return the definition.\n\
575nil means undefined. See doc of `define-key' for kinds of definitions.\n\
7c140252 576\n\
2c6f1a39
JB
577A number as value means KEY is \"too long\";\n\
578that is, characters or symbols in it except for the last one\n\
579fail to be a valid sequence of prefix characters in KEYMAP.\n\
580The number is how many characters at the front of KEY\n\
7c140252
JB
581it takes to reach a non-prefix command.\n\
582\n\
583Normally, `lookup-key' ignores bindings for t, which act as default\n\
584bindings, used when nothing else in the keymap applies; this makes it\n\
585useable as a general function for probing keymaps. However, if the\n\
586third optional argument ACCEPT-DEFAULT is non-nil, `lookup-key' will\n\
587recognize the default bindings, just as `read-key-sequence' does.")
588 (keymap, key, accept_default)
2c6f1a39
JB
589 register Lisp_Object keymap;
590 Lisp_Object key;
7c140252 591 Lisp_Object accept_default;
2c6f1a39
JB
592{
593 register int idx;
594 register Lisp_Object tem;
595 register Lisp_Object cmd;
596 register Lisp_Object c;
597 int metized = 0;
598 int length;
7c140252 599 int t_ok = ! NILP (accept_default);
6ba6e250 600 int meta_bit;
2c6f1a39
JB
601
602 keymap = get_keymap (keymap);
603
604 if (XTYPE (key) != Lisp_Vector
605 && XTYPE (key) != Lisp_String)
606 key = wrong_type_argument (Qarrayp, key);
607
d09b2024 608 length = XFASTINT (Flength (key));
2c6f1a39
JB
609 if (length == 0)
610 return keymap;
611
6ba6e250
RS
612 if (XTYPE (key) == Lisp_Vector)
613 meta_bit = meta_modifier;
614 else
615 meta_bit = 0x80;
616
2c6f1a39
JB
617 idx = 0;
618 while (1)
619 {
620 c = Faref (key, make_number (idx));
621
622 if (XTYPE (c) == Lisp_Int
6ba6e250 623 && (XINT (c) & meta_bit)
2c6f1a39
JB
624 && !metized)
625 {
626 c = meta_prefix_char;
627 metized = 1;
628 }
629 else
630 {
631 if (XTYPE (c) == Lisp_Int)
6ba6e250 632 XSETINT (c, XINT (c) & ~meta_bit);
2c6f1a39
JB
633
634 metized = 0;
635 idx++;
636 }
637
7c140252 638 cmd = get_keyelt (access_keymap (keymap, c, t_ok));
2c6f1a39
JB
639 if (idx == length)
640 return cmd;
641
d09b2024
JB
642 keymap = get_keymap_1 (cmd, 0, 0);
643 if (NILP (keymap))
2c6f1a39
JB
644 return make_number (idx);
645
2c6f1a39
JB
646 QUIT;
647 }
648}
649
0b8fc2d4
RS
650/* Append a key to the end of a key sequence. We always make a vector. */
651
2c6f1a39
JB
652Lisp_Object
653append_key (key_sequence, key)
654 Lisp_Object key_sequence, key;
655{
656 Lisp_Object args[2];
657
658 args[0] = key_sequence;
659
0b8fc2d4
RS
660 args[1] = Fcons (key, Qnil);
661 return Fvconcat (2, args);
2c6f1a39
JB
662}
663
664\f
cc0a8174
JB
665/* Global, local, and minor mode keymap stuff. */
666
265a9e55 667/* We can't put these variables inside current_minor_maps, since under
6bbbd9b0
JB
668 some systems, static gets macro-defined to be the empty string.
669 Ickypoo. */
265a9e55
JB
670static Lisp_Object *cmm_modes, *cmm_maps;
671static int cmm_size;
672
cc0a8174
JB
673/* Store a pointer to an array of the keymaps of the currently active
674 minor modes in *buf, and return the number of maps it contains.
675
676 This function always returns a pointer to the same buffer, and may
677 free or reallocate it, so if you want to keep it for a long time or
678 hand it out to lisp code, copy it. This procedure will be called
679 for every key sequence read, so the nice lispy approach (return a
680 new assoclist, list, what have you) for each invocation would
681 result in a lot of consing over time.
682
683 If we used xrealloc/xmalloc and ran out of memory, they would throw
684 back to the command loop, which would try to read a key sequence,
685 which would call this function again, resulting in an infinite
686 loop. Instead, we'll use realloc/malloc and silently truncate the
687 list, let the key sequence be read, and hope some other piece of
688 code signals the error. */
689int
690current_minor_maps (modeptr, mapptr)
691 Lisp_Object **modeptr, **mapptr;
692{
cc0a8174 693 int i = 0;
6bbbd9b0 694 Lisp_Object alist, assoc, var, val;
cc0a8174
JB
695
696 for (alist = Vminor_mode_map_alist;
697 CONSP (alist);
698 alist = XCONS (alist)->cdr)
699 if (CONSP (assoc = XCONS (alist)->car)
700 && XTYPE (var = XCONS (assoc)->car) == Lisp_Symbol
6bbbd9b0
JB
701 && ! EQ ((val = find_symbol_value (var)), Qunbound)
702 && ! NILP (val))
cc0a8174 703 {
265a9e55 704 if (i >= cmm_size)
cc0a8174
JB
705 {
706 Lisp_Object *newmodes, *newmaps;
707
265a9e55 708 if (cmm_maps)
cc0a8174 709 {
265a9e55
JB
710 newmodes = (Lisp_Object *) realloc (cmm_modes, cmm_size *= 2);
711 newmaps = (Lisp_Object *) realloc (cmm_maps, cmm_size);
cc0a8174
JB
712 }
713 else
714 {
265a9e55
JB
715 newmodes = (Lisp_Object *) malloc (cmm_size = 30);
716 newmaps = (Lisp_Object *) malloc (cmm_size);
cc0a8174
JB
717 }
718
719 if (newmaps && newmodes)
720 {
265a9e55
JB
721 cmm_modes = newmodes;
722 cmm_maps = newmaps;
cc0a8174
JB
723 }
724 else
725 break;
726 }
265a9e55
JB
727 cmm_modes[i] = var;
728 cmm_maps [i] = XCONS (assoc)->cdr;
cc0a8174
JB
729 i++;
730 }
731
265a9e55
JB
732 if (modeptr) *modeptr = cmm_modes;
733 if (mapptr) *mapptr = cmm_maps;
cc0a8174
JB
734 return i;
735}
736
7c140252 737DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 2, 0,
2c6f1a39 738 "Return the binding for command KEY in current keymaps.\n\
7c140252
JB
739KEY is a string or vector, a sequence of keystrokes.\n\
740The binding is probably a symbol with a function definition.\n\
741\n\
742Normally, `key-binding' ignores bindings for t, which act as default\n\
743bindings, used when nothing else in the keymap applies; this makes it\n\
744useable as a general function for probing keymaps. However, if the\n\
745third optional argument ACCEPT-DEFAULT is non-nil, `key-binding' will\n\
746recognize the default bindings, just as `read-key-sequence' does.")
747 (key, accept_default)
2c6f1a39
JB
748 Lisp_Object key;
749{
cc0a8174
JB
750 Lisp_Object *maps, value;
751 int nmaps, i;
752
753 nmaps = current_minor_maps (0, &maps);
754 for (i = 0; i < nmaps; i++)
265a9e55 755 if (! NILP (maps[i]))
cc0a8174 756 {
7c140252 757 value = Flookup_key (maps[i], key, accept_default);
265a9e55 758 if (! NILP (value) && XTYPE (value) != Lisp_Int)
cc0a8174
JB
759 return value;
760 }
761
265a9e55 762 if (! NILP (current_buffer->keymap))
2c6f1a39 763 {
7c140252 764 value = Flookup_key (current_buffer->keymap, key, accept_default);
265a9e55 765 if (! NILP (value) && XTYPE (value) != Lisp_Int)
2c6f1a39
JB
766 return value;
767 }
cc0a8174 768
7c140252 769 value = Flookup_key (current_global_map, key, accept_default);
265a9e55 770 if (! NILP (value) && XTYPE (value) != Lisp_Int)
cc0a8174
JB
771 return value;
772
773 return Qnil;
2c6f1a39
JB
774}
775
7c140252 776DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 2, 0,
2c6f1a39
JB
777 "Return the binding for command KEYS in current local keymap only.\n\
778KEYS is a string, a sequence of keystrokes.\n\
7c140252
JB
779The binding is probably a symbol with a function definition.\n\
780\n\
781If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
782bindings; see the description of `lookup-key' for more details about this.")
783 (keys, accept_default)
784 Lisp_Object keys, accept_default;
2c6f1a39
JB
785{
786 register Lisp_Object map;
787 map = current_buffer->keymap;
265a9e55 788 if (NILP (map))
2c6f1a39 789 return Qnil;
7c140252 790 return Flookup_key (map, keys, accept_default);
2c6f1a39
JB
791}
792
7c140252 793DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 2, 0,
2c6f1a39
JB
794 "Return the binding for command KEYS in current global keymap only.\n\
795KEYS is a string, a sequence of keystrokes.\n\
6bbbd9b0
JB
796The binding is probably a symbol with a function definition.\n\
797This function's return values are the same as those of lookup-key\n\
7c140252
JB
798(which see).\n\
799\n\
800If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
801bindings; see the description of `lookup-key' for more details about this.")
802 (keys, accept_default)
803 Lisp_Object keys, accept_default;
2c6f1a39 804{
7c140252 805 return Flookup_key (current_global_map, keys, accept_default);
2c6f1a39
JB
806}
807
7c140252 808DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0,
cc0a8174
JB
809 "Find the visible minor mode bindings of KEY.\n\
810Return an alist of pairs (MODENAME . BINDING), where MODENAME is the\n\
811the symbol which names the minor mode binding KEY, and BINDING is\n\
812KEY's definition in that mode. In particular, if KEY has no\n\
813minor-mode bindings, return nil. If the first binding is a\n\
814non-prefix, all subsequent bindings will be omitted, since they would\n\
815be ignored. Similarly, the list doesn't include non-prefix bindings\n\
7c140252
JB
816that come after prefix bindings.\n\
817\n\
818If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
819bindings; see the description of `lookup-key' for more details about this.")
820 (key, accept_default)
821 Lisp_Object key, accept_default;
cc0a8174
JB
822{
823 Lisp_Object *modes, *maps;
824 int nmaps;
825 Lisp_Object binding;
826 int i, j;
827
828 nmaps = current_minor_maps (&modes, &maps);
829
830 for (i = j = 0; i < nmaps; i++)
265a9e55 831 if (! NILP (maps[i])
7c140252 832 && ! NILP (binding = Flookup_key (maps[i], key, accept_default))
cc0a8174
JB
833 && XTYPE (binding) != Lisp_Int)
834 {
d09b2024 835 if (! NILP (get_keymap (binding)))
cc0a8174
JB
836 maps[j++] = Fcons (modes[i], binding);
837 else if (j == 0)
838 return Fcons (Fcons (modes[i], binding), Qnil);
839 }
840
841 return Flist (j, maps);
842}
843
2c6f1a39
JB
844DEFUN ("global-set-key", Fglobal_set_key, Sglobal_set_key, 2, 2,
845 "kSet key globally: \nCSet key %s to command: ",
846 "Give KEY a global binding as COMMAND.\n\
847COMMAND is a symbol naming an interactively-callable function.\n\
848KEY is a string representing a sequence of keystrokes.\n\
849Note that if KEY has a local binding in the current buffer\n\
850that local binding will continue to shadow any global binding.")
851 (keys, function)
852 Lisp_Object keys, function;
853{
854 if (XTYPE (keys) != Lisp_Vector
855 && XTYPE (keys) != Lisp_String)
856 keys = wrong_type_argument (Qarrayp, keys);
857
858 Fdefine_key (current_global_map, keys, function);
859 return Qnil;
860}
861
862DEFUN ("local-set-key", Flocal_set_key, Slocal_set_key, 2, 2,
863 "kSet key locally: \nCSet key %s locally to command: ",
864 "Give KEY a local binding as COMMAND.\n\
865COMMAND is a symbol naming an interactively-callable function.\n\
866KEY is a string representing a sequence of keystrokes.\n\
867The binding goes in the current buffer's local map,\n\
868which is shared with other buffers in the same major mode.")
869 (keys, function)
870 Lisp_Object keys, function;
871{
872 register Lisp_Object map;
873 map = current_buffer->keymap;
265a9e55 874 if (NILP (map))
2c6f1a39 875 {
ce6e5d0b 876 map = Fmake_sparse_keymap (Qnil);
2c6f1a39
JB
877 current_buffer->keymap = map;
878 }
879
880 if (XTYPE (keys) != Lisp_Vector
881 && XTYPE (keys) != Lisp_String)
882 keys = wrong_type_argument (Qarrayp, keys);
883
884 Fdefine_key (map, keys, function);
885 return Qnil;
886}
887
888DEFUN ("global-unset-key", Fglobal_unset_key, Sglobal_unset_key,
889 1, 1, "kUnset key globally: ",
890 "Remove global binding of KEY.\n\
891KEY is a string representing a sequence of keystrokes.")
892 (keys)
893 Lisp_Object keys;
894{
895 return Fglobal_set_key (keys, Qnil);
896}
897
898DEFUN ("local-unset-key", Flocal_unset_key, Slocal_unset_key, 1, 1,
899 "kUnset key locally: ",
900 "Remove local binding of KEY.\n\
901KEY is a string representing a sequence of keystrokes.")
902 (keys)
903 Lisp_Object keys;
904{
265a9e55 905 if (!NILP (current_buffer->keymap))
2c6f1a39
JB
906 Flocal_set_key (keys, Qnil);
907 return Qnil;
908}
909
910DEFUN ("define-prefix-command", Fdefine_prefix_command, Sdefine_prefix_command, 1, 2, 0,
911 "Define COMMAND as a prefix command.\n\
912A new sparse keymap is stored as COMMAND's function definition and its value.\n\
1d8d96fa
JB
913If a second optional argument MAPVAR is given, the map is stored as\n\
914its value instead of as COMMAND's value; but COMMAND is still defined\n\
915as a function.")
2c6f1a39
JB
916 (name, mapvar)
917 Lisp_Object name, mapvar;
918{
919 Lisp_Object map;
ce6e5d0b 920 map = Fmake_sparse_keymap (Qnil);
2c6f1a39 921 Ffset (name, map);
265a9e55 922 if (!NILP (mapvar))
2c6f1a39
JB
923 Fset (mapvar, map);
924 else
925 Fset (name, map);
926 return name;
927}
928
929DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0,
930 "Select KEYMAP as the global keymap.")
931 (keymap)
932 Lisp_Object keymap;
933{
934 keymap = get_keymap (keymap);
935 current_global_map = keymap;
936 return Qnil;
937}
938
939DEFUN ("use-local-map", Fuse_local_map, Suse_local_map, 1, 1, 0,
940 "Select KEYMAP as the local keymap.\n\
941If KEYMAP is nil, that means no local keymap.")
942 (keymap)
943 Lisp_Object keymap;
944{
265a9e55 945 if (!NILP (keymap))
2c6f1a39
JB
946 keymap = get_keymap (keymap);
947
948 current_buffer->keymap = keymap;
949
950 return Qnil;
951}
952
953DEFUN ("current-local-map", Fcurrent_local_map, Scurrent_local_map, 0, 0, 0,
954 "Return current buffer's local keymap, or nil if it has none.")
955 ()
956{
957 return current_buffer->keymap;
958}
959
960DEFUN ("current-global-map", Fcurrent_global_map, Scurrent_global_map, 0, 0, 0,
961 "Return the current global keymap.")
962 ()
963{
964 return current_global_map;
965}
cc0a8174
JB
966
967DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps, Scurrent_minor_mode_maps, 0, 0, 0,
968 "Return a list of keymaps for the minor modes of the current buffer.")
969 ()
970{
971 Lisp_Object *maps;
972 int nmaps = current_minor_maps (0, &maps);
973
974 return Flist (nmaps, maps);
975}
2c6f1a39 976\f
cc0a8174
JB
977/* Help functions for describing and documenting keymaps. */
978
2c6f1a39
JB
979DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps,
980 1, 1, 0,
981 "Find all keymaps accessible via prefix characters from KEYMAP.\n\
982Returns a list of elements of the form (KEYS . MAP), where the sequence\n\
983KEYS starting from KEYMAP gets you to MAP. These elements are ordered\n\
984so that the KEYS increase in length. The first element is (\"\" . KEYMAP).")
985 (startmap)
986 Lisp_Object startmap;
987{
988 Lisp_Object maps, tail;
989
0b8fc2d4
RS
990 maps = Fcons (Fcons (Fmake_vector (make_number (0), Qnil),
991 get_keymap (startmap)),
992 Qnil);
2c6f1a39
JB
993
994 /* For each map in the list maps,
995 look at any other maps it points to,
996 and stick them at the end if they are not already in the list.
997
998 This is a breadth-first traversal, where tail is the queue of
999 nodes, and maps accumulates a list of all nodes visited. */
1000
f5b79c1c 1001 for (tail = maps; CONSP (tail); tail = XCONS (tail)->cdr)
2c6f1a39
JB
1002 {
1003 register Lisp_Object thisseq = Fcar (Fcar (tail));
1004 register Lisp_Object thismap = Fcdr (Fcar (tail));
1005 Lisp_Object last = make_number (XINT (Flength (thisseq)) - 1);
1006
1007 /* Does the current sequence end in the meta-prefix-char? */
1008 int is_metized = (XINT (last) >= 0
1009 && EQ (Faref (thisseq, last), meta_prefix_char));
1010
f5b79c1c 1011 for (; CONSP (thismap); thismap = XCONS (thismap)->cdr)
2c6f1a39 1012 {
f5b79c1c 1013 Lisp_Object elt = XCONS (thismap)->car;
2c6f1a39 1014
f5b79c1c
JB
1015 QUIT;
1016
1017 if (XTYPE (elt) == Lisp_Vector)
2c6f1a39
JB
1018 {
1019 register int i;
1020
1021 /* Vector keymap. Scan all the elements. */
1022 for (i = 0; i < DENSE_TABLE_SIZE; i++)
1023 {
1024 register Lisp_Object tem;
1025 register Lisp_Object cmd;
1026
f5b79c1c 1027 cmd = get_keyelt (XVECTOR (elt)->contents[i]);
265a9e55 1028 if (NILP (cmd)) continue;
2c6f1a39 1029 tem = Fkeymapp (cmd);
265a9e55 1030 if (!NILP (tem))
2c6f1a39
JB
1031 {
1032 cmd = get_keymap (cmd);
1033 /* Ignore keymaps that are already added to maps. */
1034 tem = Frassq (cmd, maps);
265a9e55 1035 if (NILP (tem))
2c6f1a39
JB
1036 {
1037 /* If the last key in thisseq is meta-prefix-char,
1038 turn it into a meta-ized keystroke. We know
1039 that the event we're about to append is an
f5b79c1c
JB
1040 ascii keystroke since we're processing a
1041 keymap table. */
2c6f1a39
JB
1042 if (is_metized)
1043 {
0b8fc2d4 1044 int meta_bit = meta_modifier;
2c6f1a39 1045 tem = Fcopy_sequence (thisseq);
0b8fc2d4
RS
1046
1047 Faset (tem, last, make_number (i | meta_bit));
2c6f1a39
JB
1048
1049 /* This new sequence is the same length as
1050 thisseq, so stick it in the list right
1051 after this one. */
0b8fc2d4
RS
1052 XCONS (tail)->cdr
1053 = Fcons (Fcons (tem, cmd), XCONS (tail)->cdr);
2c6f1a39
JB
1054 }
1055 else
1056 {
1057 tem = append_key (thisseq, make_number (i));
1058 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
1059 }
1060 }
1061 }
1062 }
f5b79c1c
JB
1063 }
1064 else if (CONSP (elt))
2c6f1a39
JB
1065 {
1066 register Lisp_Object cmd = get_keyelt (XCONS (elt)->cdr);
1067 register Lisp_Object tem;
1068
1069 /* Ignore definitions that aren't keymaps themselves. */
1070 tem = Fkeymapp (cmd);
265a9e55 1071 if (!NILP (tem))
2c6f1a39
JB
1072 {
1073 /* Ignore keymaps that have been seen already. */
1074 cmd = get_keymap (cmd);
1075 tem = Frassq (cmd, maps);
265a9e55 1076 if (NILP (tem))
2c6f1a39
JB
1077 {
1078 /* let elt be the event defined by this map entry. */
1079 elt = XCONS (elt)->car;
1080
1081 /* If the last key in thisseq is meta-prefix-char, and
1082 this entry is a binding for an ascii keystroke,
1083 turn it into a meta-ized keystroke. */
1084 if (is_metized && XTYPE (elt) == Lisp_Int)
1085 {
1086 tem = Fcopy_sequence (thisseq);
0b8fc2d4
RS
1087 Faset (tem, last,
1088 make_number (XINT (elt) | meta_modifier));
2c6f1a39
JB
1089
1090 /* This new sequence is the same length as
1091 thisseq, so stick it in the list right
1092 after this one. */
1093 XCONS (tail)->cdr =
1094 Fcons (Fcons (tem, cmd), XCONS (tail)->cdr);
1095 }
1096 else
1097 nconc2 (tail,
1098 Fcons (Fcons (append_key (thisseq, elt), cmd),
1099 Qnil));
1100 }
1101 }
1102 }
2c6f1a39 1103 }
2c6f1a39
JB
1104 }
1105
1106 return maps;
1107}
1108
1109Lisp_Object Qsingle_key_description, Qkey_description;
1110
1111DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0,
1112 "Return a pretty description of key-sequence KEYS.\n\
1113Control characters turn into \"C-foo\" sequences, meta into \"M-foo\"\n\
1114spaces are put between sequence elements, etc.")
1115 (keys)
1116 Lisp_Object keys;
1117{
6ba6e250
RS
1118 if (XTYPE (keys) == Lisp_String)
1119 {
1120 Lisp_Object vector;
1121 int i;
1122 vector = Fmake_vector (Flength (keys), Qnil);
1123 for (i = 0; i < XSTRING (keys)->size; i++)
1124 {
1125 if (XSTRING (keys)->data[i] & 0x80)
1126 XFASTINT (XVECTOR (vector)->contents[i])
1127 = meta_modifier | (XSTRING (keys)->data[i] & ~0x80);
1128 else
1129 XFASTINT (XVECTOR (vector)->contents[i])
1130 = XSTRING (keys)->data[i];
1131 }
1132 keys = vector;
1133 }
2c6f1a39
JB
1134 return Fmapconcat (Qsingle_key_description, keys, build_string (" "));
1135}
1136
1137char *
1138push_key_description (c, p)
1139 register unsigned int c;
1140 register char *p;
1141{
6ba6e250
RS
1142 if (c & alt_modifier)
1143 {
1144 *p++ = 'A';
1145 *p++ = '-';
1146 c -= alt_modifier;
1147 }
1148 if (c & ctrl_modifier)
1149 {
1150 *p++ = 'C';
1151 *p++ = '-';
1152 c -= ctrl_modifier;
1153 }
1154 if (c & hyper_modifier)
1155 {
1156 *p++ = 'H';
1157 *p++ = '-';
1158 c -= hyper_modifier;
1159 }
1160 if (c & meta_modifier)
2c6f1a39
JB
1161 {
1162 *p++ = 'M';
1163 *p++ = '-';
6ba6e250
RS
1164 c -= meta_modifier;
1165 }
1166 if (c & shift_modifier)
1167 {
1168 *p++ = 'S';
1169 *p++ = '-';
1170 c -= shift_modifier;
1171 }
1172 if (c & super_modifier)
1173 {
1174 *p++ = 's';
1175 *p++ = '-';
1176 c -= super_modifier;
2c6f1a39
JB
1177 }
1178 if (c < 040)
1179 {
1180 if (c == 033)
1181 {
1182 *p++ = 'E';
1183 *p++ = 'S';
1184 *p++ = 'C';
1185 }
6ba6e250 1186 else if (c == '\t')
2c6f1a39
JB
1187 {
1188 *p++ = 'T';
1189 *p++ = 'A';
1190 *p++ = 'B';
1191 }
1192 else if (c == Ctl('J'))
1193 {
1194 *p++ = 'L';
1195 *p++ = 'F';
1196 *p++ = 'D';
1197 }
1198 else if (c == Ctl('M'))
1199 {
1200 *p++ = 'R';
1201 *p++ = 'E';
1202 *p++ = 'T';
1203 }
1204 else
1205 {
1206 *p++ = 'C';
1207 *p++ = '-';
1208 if (c > 0 && c <= Ctl ('Z'))
1209 *p++ = c + 0140;
1210 else
1211 *p++ = c + 0100;
1212 }
1213 }
1214 else if (c == 0177)
1215 {
1216 *p++ = 'D';
1217 *p++ = 'E';
1218 *p++ = 'L';
1219 }
1220 else if (c == ' ')
1221 {
1222 *p++ = 'S';
1223 *p++ = 'P';
1224 *p++ = 'C';
1225 }
6ba6e250 1226 else if (c < 256)
2c6f1a39 1227 *p++ = c;
6ba6e250
RS
1228 else
1229 {
1230 *p++ = '\\';
1231 *p++ = (7 & (c >> 15)) + '0';
1232 *p++ = (7 & (c >> 12)) + '0';
1233 *p++ = (7 & (c >> 9)) + '0';
1234 *p++ = (7 & (c >> 6)) + '0';
1235 *p++ = (7 & (c >> 3)) + '0';
1236 *p++ = (7 & (c >> 0)) + '0';
1237 }
2c6f1a39
JB
1238
1239 return p;
1240}
1241
1242DEFUN ("single-key-description", Fsingle_key_description, Ssingle_key_description, 1, 1, 0,
1243 "Return a pretty description of command character KEY.\n\
1244Control characters turn into C-whatever, etc.")
1245 (key)
1246 Lisp_Object key;
1247{
6ba6e250 1248 char tem[20];
2c6f1a39 1249
cebd887d 1250 key = EVENT_HEAD (key);
6bbbd9b0 1251
2c6f1a39
JB
1252 switch (XTYPE (key))
1253 {
1254 case Lisp_Int: /* Normal character */
6ba6e250 1255 *push_key_description (XUINT (key), tem) = 0;
2c6f1a39
JB
1256 return build_string (tem);
1257
1258 case Lisp_Symbol: /* Function key or event-symbol */
1259 return Fsymbol_name (key);
1260
2c6f1a39
JB
1261 default:
1262 error ("KEY must be an integer, cons, or symbol.");
1263 }
1264}
1265
1266char *
1267push_text_char_description (c, p)
1268 register unsigned int c;
1269 register char *p;
1270{
1271 if (c >= 0200)
1272 {
1273 *p++ = 'M';
1274 *p++ = '-';
1275 c -= 0200;
1276 }
1277 if (c < 040)
1278 {
1279 *p++ = '^';
1280 *p++ = c + 64; /* 'A' - 1 */
1281 }
1282 else if (c == 0177)
1283 {
1284 *p++ = '^';
1285 *p++ = '?';
1286 }
1287 else
1288 *p++ = c;
1289 return p;
1290}
1291
1292DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0,
1293 "Return a pretty description of file-character CHAR.\n\
1294Control characters turn into \"^char\", etc.")
1295 (chr)
1296 Lisp_Object chr;
1297{
1298 char tem[6];
1299
1300 CHECK_NUMBER (chr, 0);
1301
1302 *push_text_char_description (XINT (chr) & 0377, tem) = 0;
1303
1304 return build_string (tem);
1305}
1306\f
cc0a8174
JB
1307/* where-is - finding a command in a set of keymaps. */
1308
2c6f1a39
JB
1309DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 5, 0,
1310 "Return list of keys that invoke DEFINITION in KEYMAP or KEYMAP1.\n\
1311If KEYMAP is nil, search only KEYMAP1.\n\
1312If KEYMAP1 is nil, use the current global map.\n\
1313\n\
1314If optional 4th arg FIRSTONLY is non-nil,\n\
1315return a string representing the first key sequence found,\n\
1316rather than a list of all possible key sequences.\n\
1317\n\
1318If optional 5th arg NOINDIRECT is non-nil, don't follow indirections\n\
1319to other keymaps or slots. This makes it possible to search for an\n\
1320indirect definition itself.")
1321 (definition, local_keymap, global_keymap, firstonly, noindirect)
1322 Lisp_Object definition, local_keymap, global_keymap;
1323 Lisp_Object firstonly, noindirect;
1324{
1325 register Lisp_Object maps;
1326 Lisp_Object found;
1327
265a9e55 1328 if (NILP (global_keymap))
2c6f1a39
JB
1329 global_keymap = current_global_map;
1330
265a9e55 1331 if (!NILP (local_keymap))
2c6f1a39
JB
1332 maps = nconc2 (Faccessible_keymaps (get_keymap (local_keymap)),
1333 Faccessible_keymaps (get_keymap (global_keymap)));
1334 else
1335 maps = Faccessible_keymaps (get_keymap (global_keymap));
1336
1337 found = Qnil;
1338
265a9e55 1339 for (; !NILP (maps); maps = Fcdr (maps))
2c6f1a39 1340 {
f5b79c1c
JB
1341 /* Key sequence to reach map */
1342 register Lisp_Object this = Fcar (Fcar (maps));
1343
1344 /* The map that it reaches */
1345 register Lisp_Object map = Fcdr (Fcar (maps));
1346
1347 /* If Fcar (map) is a VECTOR, the current element within that vector. */
1348 int i = 0;
2c6f1a39
JB
1349
1350 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
1351 [M-CHAR] sequences, check if last character of the sequence
1352 is the meta-prefix char. */
1353 Lisp_Object last = make_number (XINT (Flength (this)) - 1);
1354 int last_is_meta = (XINT (last) >= 0
1355 && EQ (Faref (this, last), meta_prefix_char));
2c6f1a39 1356
fde3a52f
JB
1357 QUIT;
1358
f5b79c1c 1359 while (CONSP (map))
2c6f1a39 1360 {
f5b79c1c
JB
1361 /* Because the code we want to run on each binding is rather
1362 large, we don't want to have two separate loop bodies for
1363 sparse keymap bindings and tables; we want to iterate one
1364 loop body over both keymap and vector bindings.
1365
1366 For this reason, if Fcar (map) is a vector, we don't
1367 advance map to the next element until i indicates that we
1368 have finished off the vector. */
2c6f1a39 1369
f5b79c1c
JB
1370 Lisp_Object elt = XCONS (map)->car;
1371 Lisp_Object key, binding, sequence;
1372
fde3a52f
JB
1373 QUIT;
1374
f5b79c1c
JB
1375 /* Set key and binding to the current key and binding, and
1376 advance map and i to the next binding. */
1377 if (XTYPE (elt) == Lisp_Vector)
2c6f1a39
JB
1378 {
1379 /* In a vector, look at each element. */
f5b79c1c 1380 binding = XVECTOR (elt)->contents[i];
2c6f1a39
JB
1381 XFASTINT (key) = i;
1382 i++;
1383
f5b79c1c
JB
1384 /* If we've just finished scanning a vector, advance map
1385 to the next element, and reset i in anticipation of the
1386 next vector we may find. */
2c6f1a39 1387 if (i >= DENSE_TABLE_SIZE)
2c6f1a39 1388 {
f5b79c1c
JB
1389 map = XCONS (map)->cdr;
1390 i = 0;
2c6f1a39 1391 }
f5b79c1c
JB
1392 }
1393 else if (CONSP (elt))
1394 {
2c6f1a39 1395 key = Fcar (Fcar (map));
f5b79c1c
JB
1396 binding = Fcdr (Fcar (map));
1397
1398 map = XCONS (map)->cdr;
2c6f1a39
JB
1399 }
1400 else
f5b79c1c
JB
1401 /* We want to ignore keymap elements that are neither
1402 vectors nor conses. */
fde3a52f
JB
1403 {
1404 map = XCONS (map)->cdr;
1405 continue;
1406 }
2c6f1a39
JB
1407
1408 /* Search through indirections unless that's not wanted. */
265a9e55 1409 if (NILP (noindirect))
2c6f1a39
JB
1410 binding = get_keyelt (binding);
1411
1412 /* End this iteration if this element does not match
1413 the target. */
1414
1415 if (XTYPE (definition) == Lisp_Cons)
1416 {
1417 Lisp_Object tem;
1418 tem = Fequal (binding, definition);
265a9e55 1419 if (NILP (tem))
2c6f1a39
JB
1420 continue;
1421 }
1422 else
1423 if (!EQ (binding, definition))
1424 continue;
1425
1426 /* We have found a match.
1427 Construct the key sequence where we found it. */
1428 if (XTYPE (key) == Lisp_Int && last_is_meta)
1429 {
1430 sequence = Fcopy_sequence (this);
0b8fc2d4 1431 Faset (sequence, last, make_number (XINT (key) | meta_modifier));
2c6f1a39
JB
1432 }
1433 else
1434 sequence = append_key (this, key);
1435
1436 /* Verify that this key binding is not shadowed by another
1437 binding for the same key, before we say it exists.
1438
1439 Mechanism: look for local definition of this key and if
1440 it is defined and does not match what we found then
1441 ignore this key.
1442
1443 Either nil or number as value from Flookup_key
1444 means undefined. */
265a9e55 1445 if (!NILP (local_keymap))
2c6f1a39 1446 {
7c140252 1447 binding = Flookup_key (local_keymap, sequence, Qnil);
265a9e55 1448 if (!NILP (binding) && XTYPE (binding) != Lisp_Int)
2c6f1a39
JB
1449 {
1450 if (XTYPE (definition) == Lisp_Cons)
1451 {
1452 Lisp_Object tem;
1453 tem = Fequal (binding, definition);
265a9e55 1454 if (NILP (tem))
2c6f1a39
JB
1455 continue;
1456 }
1457 else
1458 if (!EQ (binding, definition))
1459 continue;
1460 }
1461 }
1462
1463 /* It is a true unshadowed match. Record it. */
1464
265a9e55 1465 if (!NILP (firstonly))
2c6f1a39
JB
1466 return sequence;
1467 found = Fcons (sequence, found);
1468 }
1469 }
1470 return Fnreverse (found);
1471}
1472
1473/* Return a string listing the keys and buttons that run DEFINITION. */
1474
1475static Lisp_Object
1476where_is_string (definition)
1477 Lisp_Object definition;
1478{
1479 register Lisp_Object keys, keys1;
1480
1481 keys = Fwhere_is_internal (definition,
1482 current_buffer->keymap, Qnil, Qnil, Qnil);
1483 keys1 = Fmapconcat (Qkey_description, keys, build_string (", "));
1484
1485 return keys1;
1486}
1487
1488DEFUN ("where-is", Fwhere_is, Swhere_is, 1, 1, "CWhere is command: ",
1489 "Print message listing key sequences that invoke specified command.\n\
1490Argument is a command definition, usually a symbol with a function definition.")
1491 (definition)
1492 Lisp_Object definition;
1493{
1494 register Lisp_Object string;
1495
1496 CHECK_SYMBOL (definition, 0);
1497 string = where_is_string (definition);
1498
1499 if (XSTRING (string)->size)
1500 message ("%s is on %s", XSYMBOL (definition)->name->data,
1501 XSTRING (string)->data);
1502 else
1503 message ("%s is not on any key", XSYMBOL (definition)->name->data);
1504 return Qnil;
1505}
1506\f
cc0a8174
JB
1507/* describe-bindings - summarizing all the bindings in a set of keymaps. */
1508
2c6f1a39
JB
1509DEFUN ("describe-bindings", Fdescribe_bindings, Sdescribe_bindings, 0, 0, "",
1510 "Show a list of all defined keys, and their definitions.\n\
1511The list is put in a buffer, which is displayed.")
1512 ()
1513{
1514 register Lisp_Object thisbuf;
1515 XSET (thisbuf, Lisp_Buffer, current_buffer);
1516 internal_with_output_to_temp_buffer ("*Help*",
1517 describe_buffer_bindings,
1518 thisbuf);
1519 return Qnil;
1520}
1521
1522static Lisp_Object
1523describe_buffer_bindings (descbuf)
1524 Lisp_Object descbuf;
1525{
1526 register Lisp_Object start1, start2;
1527
4726a9f1
JB
1528 char *key_heading
1529 = "\
1530key binding\n\
1531--- -------\n";
1532 char *alternate_heading
1533 = "\
1534Alternate Characters (use anywhere the nominal character is listed):\n\
1535nominal alternate\n\
1536------- ---------\n";
2c6f1a39
JB
1537
1538 Fset_buffer (Vstandard_output);
1539
4726a9f1
JB
1540 /* Report on alternates for keys. */
1541 if (XTYPE (Vkeyboard_translate_table) == Lisp_String)
1542 {
1543 int c;
1544 unsigned char *translate = XSTRING (Vkeyboard_translate_table)->data;
1545 int translate_len = XSTRING (Vkeyboard_translate_table)->size;
1546
1547 for (c = 0; c < translate_len; c++)
1548 if (translate[c] != c)
1549 {
1550 char buf[20];
1551 char *bufend;
1552
1553 if (alternate_heading)
1554 {
1555 insert_string (alternate_heading);
1556 alternate_heading = 0;
1557 }
1558
1559 bufend = push_key_description (translate[c], buf);
1560 insert (buf, bufend - buf);
1561 Findent_to (make_number (16), make_number (1));
1562 bufend = push_key_description (c, buf);
1563 insert (buf, bufend - buf);
1564
1565 insert ("\n", 1);
1566 }
1567
1568 insert ("\n", 1);
1569 }
1570
cc0a8174
JB
1571 {
1572 int i, nmaps;
1573 Lisp_Object *modes, *maps;
1574
4726a9f1
JB
1575 /* Temporarily switch to descbuf, so that we can get that buffer's
1576 minor modes correctly. */
1577 Fset_buffer (descbuf);
cc0a8174 1578 nmaps = current_minor_maps (&modes, &maps);
4726a9f1
JB
1579 Fset_buffer (Vstandard_output);
1580
cc0a8174
JB
1581 for (i = 0; i < nmaps; i++)
1582 {
1583 if (XTYPE (modes[i]) == Lisp_Symbol)
1584 {
1585 insert_char ('`');
1586 insert_string (XSYMBOL (modes[i])->name->data);
1587 insert_char ('\'');
1588 }
1589 else
1590 insert_string ("Strangely Named");
1591 insert_string (" Minor Mode Bindings:\n");
4726a9f1 1592 insert_string (key_heading);
cc0a8174
JB
1593 describe_map_tree (maps[i], 0, Qnil);
1594 insert_char ('\n');
1595 }
1596 }
1597
2c6f1a39 1598 start1 = XBUFFER (descbuf)->keymap;
265a9e55 1599 if (!NILP (start1))
2c6f1a39
JB
1600 {
1601 insert_string ("Local Bindings:\n");
4726a9f1 1602 insert_string (key_heading);
cc0a8174 1603 describe_map_tree (start1, 0, Qnil);
2c6f1a39
JB
1604 insert_string ("\n");
1605 }
1606
1607 insert_string ("Global Bindings:\n");
4726a9f1
JB
1608 if (NILP (start1))
1609 insert_string (key_heading);
2c6f1a39 1610
cc0a8174 1611 describe_map_tree (current_global_map, 0, XBUFFER (descbuf)->keymap);
2c6f1a39
JB
1612
1613 Fset_buffer (descbuf);
1614 return Qnil;
1615}
1616
1617/* Insert a desription of the key bindings in STARTMAP,
1618 followed by those of all maps reachable through STARTMAP.
1619 If PARTIAL is nonzero, omit certain "uninteresting" commands
1620 (such as `undefined').
1621 If SHADOW is non-nil, it is another map;
1622 don't mention keys which would be shadowed by it. */
1623
1624void
1625describe_map_tree (startmap, partial, shadow)
1626 Lisp_Object startmap, shadow;
1627 int partial;
1628{
1629 register Lisp_Object elt, sh;
1630 Lisp_Object maps;
1631 struct gcpro gcpro1;
1632
1633 maps = Faccessible_keymaps (startmap);
1634 GCPRO1 (maps);
1635
265a9e55 1636 for (; !NILP (maps); maps = Fcdr (maps))
2c6f1a39
JB
1637 {
1638 elt = Fcar (maps);
1639 sh = Fcar (elt);
1640
1641 /* If there is no shadow keymap given, don't shadow. */
265a9e55 1642 if (NILP (shadow))
2c6f1a39
JB
1643 sh = Qnil;
1644
1645 /* If the sequence by which we reach this keymap is zero-length,
1646 then the shadow map for this keymap is just SHADOW. */
1647 else if ((XTYPE (sh) == Lisp_String
1648 && XSTRING (sh)->size == 0)
1649 || (XTYPE (sh) == Lisp_Vector
1650 && XVECTOR (sh)->size == 0))
1651 sh = shadow;
1652
1653 /* If the sequence by which we reach this keymap actually has
1654 some elements, then the sequence's definition in SHADOW is
1655 what we should use. */
1656 else
1657 {
7c140252 1658 sh = Flookup_key (shadow, Fcar (elt), Qt);
2c6f1a39
JB
1659 if (XTYPE (sh) == Lisp_Int)
1660 sh = Qnil;
1661 }
1662
1663 /* If sh is null (meaning that the current map is not shadowed),
1664 or a keymap (meaning that bindings from the current map might
1665 show through), describe the map. Otherwise, sh is a command
1666 that completely shadows the current map, and we shouldn't
1667 bother. */
265a9e55 1668 if (NILP (sh) || !NILP (Fkeymapp (sh)))
2c6f1a39
JB
1669 describe_map (Fcdr (elt), Fcar (elt), partial, sh);
1670 }
1671
1672 UNGCPRO;
1673}
1674
1675static void
1676describe_command (definition)
1677 Lisp_Object definition;
1678{
1679 register Lisp_Object tem1;
1680
1681 Findent_to (make_number (16), make_number (1));
1682
1683 if (XTYPE (definition) == Lisp_Symbol)
1684 {
1685 XSET (tem1, Lisp_String, XSYMBOL (definition)->name);
1686 insert1 (tem1);
1687 insert_string ("\n");
1688 }
1689 else
1690 {
1691 tem1 = Fkeymapp (definition);
265a9e55 1692 if (!NILP (tem1))
2c6f1a39
JB
1693 insert_string ("Prefix Command\n");
1694 else
1695 insert_string ("??\n");
1696 }
1697}
1698
1699/* Describe the contents of map MAP, assuming that this map itself is
1700 reached by the sequence of prefix keys KEYS (a string or vector).
1701 PARTIAL, SHADOW is as in `describe_map_tree' above. */
1702
1703static void
1704describe_map (map, keys, partial, shadow)
1705 Lisp_Object map, keys;
1706 int partial;
1707 Lisp_Object shadow;
1708{
1709 register Lisp_Object keysdesc;
1710
d09b2024 1711 if (!NILP (keys) && XFASTINT (Flength (keys)) > 0)
5cba3869
RS
1712 {
1713 Lisp_Object tem;
1714 /* Call Fkey_description first, to avoid GC bug for the other string. */
1715 tem = Fkey_description (keys);
1716 keysdesc = concat2 (tem, build_string (" "));
1717 }
2c6f1a39
JB
1718 else
1719 keysdesc = Qnil;
1720
f5b79c1c 1721 describe_map_2 (map, keysdesc, describe_command, partial, shadow);
2c6f1a39
JB
1722}
1723
f5b79c1c 1724/* Insert a description of KEYMAP into the current buffer. */
2c6f1a39
JB
1725
1726static void
f5b79c1c
JB
1727describe_map_2 (keymap, elt_prefix, elt_describer, partial, shadow)
1728 register Lisp_Object keymap;
2c6f1a39
JB
1729 Lisp_Object elt_prefix;
1730 int (*elt_describer) ();
1731 int partial;
1732 Lisp_Object shadow;
1733{
1734 Lisp_Object this;
1735 Lisp_Object tem1, tem2 = Qnil;
1736 Lisp_Object suppress;
1737 Lisp_Object kludge;
1738 int first = 1;
1739 struct gcpro gcpro1, gcpro2, gcpro3;
1740
1741 if (partial)
1742 suppress = intern ("suppress-keymap");
1743
1744 /* This vector gets used to present single keys to Flookup_key. Since
f5b79c1c 1745 that is done once per keymap element, we don't want to cons up a
2c6f1a39
JB
1746 fresh vector every time. */
1747 kludge = Fmake_vector (make_number (1), Qnil);
1748
1749 GCPRO3 (elt_prefix, tem2, kludge);
1750
f5b79c1c 1751 for (; CONSP (keymap); keymap = Fcdr (keymap))
2c6f1a39
JB
1752 {
1753 QUIT;
2c6f1a39 1754
f5b79c1c
JB
1755 if (XTYPE (XCONS (keymap)->car) == Lisp_Vector)
1756 describe_vector (XCONS (keymap)->car,
1757 elt_prefix, elt_describer, partial, shadow);
1758 else
2c6f1a39 1759 {
f5b79c1c
JB
1760 tem1 = Fcar_safe (Fcar (keymap));
1761 tem2 = get_keyelt (Fcdr_safe (Fcar (keymap)));
2c6f1a39 1762
f5b79c1c
JB
1763 /* Don't show undefined commands or suppressed commands. */
1764 if (NILP (tem2)) continue;
1765 if (XTYPE (tem2) == Lisp_Symbol && partial)
1766 {
1767 this = Fget (tem2, suppress);
1768 if (!NILP (this))
1769 continue;
1770 }
2c6f1a39 1771
f5b79c1c
JB
1772 /* Don't show a command that isn't really visible
1773 because a local definition of the same key shadows it. */
2c6f1a39 1774
f5b79c1c
JB
1775 if (!NILP (shadow))
1776 {
1777 Lisp_Object tem;
2c6f1a39 1778
f5b79c1c 1779 XVECTOR (kludge)->contents[0] = tem1;
7c140252 1780 tem = Flookup_key (shadow, kludge, Qt);
f5b79c1c
JB
1781 if (!NILP (tem)) continue;
1782 }
1783
1784 if (first)
1785 {
1786 insert ("\n", 1);
1787 first = 0;
1788 }
2c6f1a39 1789
f5b79c1c
JB
1790 if (!NILP (elt_prefix))
1791 insert1 (elt_prefix);
2c6f1a39 1792
f5b79c1c
JB
1793 /* THIS gets the string to describe the character TEM1. */
1794 this = Fsingle_key_description (tem1);
1795 insert1 (this);
2c6f1a39 1796
f5b79c1c
JB
1797 /* Print a description of the definition of this character.
1798 elt_describer will take care of spacing out far enough
1799 for alignment purposes. */
1800 (*elt_describer) (tem2);
1801 }
2c6f1a39
JB
1802 }
1803
1804 UNGCPRO;
1805}
1806
1807static int
1808describe_vector_princ (elt)
1809 Lisp_Object elt;
1810{
1811 Fprinc (elt, Qnil);
1812}
1813
1814DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 1, 0,
1815 "Print on `standard-output' a description of contents of VECTOR.\n\
1816This is text showing the elements of vector matched against indices.")
1817 (vector)
1818 Lisp_Object vector;
1819{
1820 CHECK_VECTOR (vector, 0);
92cc37e8 1821 describe_vector (vector, Qnil, describe_vector_princ, 0, Qnil);
2c6f1a39
JB
1822}
1823
1824describe_vector (vector, elt_prefix, elt_describer, partial, shadow)
1825 register Lisp_Object vector;
1826 Lisp_Object elt_prefix;
1827 int (*elt_describer) ();
1828 int partial;
1829 Lisp_Object shadow;
1830{
1831 Lisp_Object this;
1832 Lisp_Object dummy;
1833 Lisp_Object tem1, tem2;
1834 register int i;
1835 Lisp_Object suppress;
1836 Lisp_Object kludge;
1837 int first = 1;
1838 struct gcpro gcpro1, gcpro2, gcpro3;
1839
1840 tem1 = Qnil;
1841
1842 /* This vector gets used to present single keys to Flookup_key. Since
1843 that is done once per vector element, we don't want to cons up a
1844 fresh vector every time. */
1845 kludge = Fmake_vector (make_number (1), Qnil);
1846 GCPRO3 (elt_prefix, tem1, kludge);
1847
1848 if (partial)
1849 suppress = intern ("suppress-keymap");
1850
1851 for (i = 0; i < DENSE_TABLE_SIZE; i++)
1852 {
1853 QUIT;
1854 tem1 = get_keyelt (XVECTOR (vector)->contents[i]);
1855
265a9e55 1856 if (NILP (tem1)) continue;
2c6f1a39
JB
1857
1858 /* Don't mention suppressed commands. */
1859 if (XTYPE (tem1) == Lisp_Symbol && partial)
1860 {
1861 this = Fget (tem1, suppress);
265a9e55 1862 if (!NILP (this))
2c6f1a39
JB
1863 continue;
1864 }
1865
1866 /* If this command in this map is shadowed by some other map,
1867 ignore it. */
265a9e55 1868 if (!NILP (shadow))
2c6f1a39
JB
1869 {
1870 Lisp_Object tem;
1871
1872 XVECTOR (kludge)->contents[0] = make_number (i);
7c140252 1873 tem = Flookup_key (shadow, kludge, Qt);
2c6f1a39 1874
265a9e55 1875 if (!NILP (tem)) continue;
2c6f1a39
JB
1876 }
1877
1878 if (first)
1879 {
1880 insert ("\n", 1);
1881 first = 0;
1882 }
1883
1884 /* Output the prefix that applies to every entry in this map. */
265a9e55 1885 if (!NILP (elt_prefix))
2c6f1a39
JB
1886 insert1 (elt_prefix);
1887
1888 /* Get the string to describe the character I, and print it. */
1889 XFASTINT (dummy) = i;
1890
1891 /* THIS gets the string to describe the character DUMMY. */
1892 this = Fsingle_key_description (dummy);
1893 insert1 (this);
1894
1895 /* Find all consecutive characters that have the same definition. */
1896 while (i + 1 < DENSE_TABLE_SIZE
1897 && (tem2 = get_keyelt (XVECTOR (vector)->contents[i+1]),
1898 EQ (tem2, tem1)))
1899 i++;
1900
1901 /* If we have a range of more than one character,
1902 print where the range reaches to. */
1903
1904 if (i != XINT (dummy))
1905 {
1906 insert (" .. ", 4);
265a9e55 1907 if (!NILP (elt_prefix))
2c6f1a39
JB
1908 insert1 (elt_prefix);
1909
1910 XFASTINT (dummy) = i;
1911 insert1 (Fsingle_key_description (dummy));
1912 }
1913
1914 /* Print a description of the definition of this character.
1915 elt_describer will take care of spacing out far enough
1916 for alignment purposes. */
1917 (*elt_describer) (tem1);
1918 }
1919
1920 UNGCPRO;
1921}
1922\f
cc0a8174 1923/* Apropos - finding all symbols whose names match a regexp. */
2c6f1a39
JB
1924Lisp_Object apropos_predicate;
1925Lisp_Object apropos_accumulate;
1926
1927static void
1928apropos_accum (symbol, string)
1929 Lisp_Object symbol, string;
1930{
1931 register Lisp_Object tem;
1932
1933 tem = Fstring_match (string, Fsymbol_name (symbol), Qnil);
265a9e55 1934 if (!NILP (tem) && !NILP (apropos_predicate))
2c6f1a39 1935 tem = call1 (apropos_predicate, symbol);
265a9e55 1936 if (!NILP (tem))
2c6f1a39
JB
1937 apropos_accumulate = Fcons (symbol, apropos_accumulate);
1938}
1939
1940DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0,
1941 "Show all symbols whose names contain match for REGEXP.\n\
1942If optional 2nd arg PRED is non-nil, (funcall PRED SYM) is done\n\
1943for each symbol and a symbol is mentioned only if that returns non-nil.\n\
1944Return list of symbols found.")
1945 (string, pred)
1946 Lisp_Object string, pred;
1947{
1948 struct gcpro gcpro1, gcpro2;
1949 CHECK_STRING (string, 0);
1950 apropos_predicate = pred;
1951 GCPRO2 (apropos_predicate, apropos_accumulate);
1952 apropos_accumulate = Qnil;
1953 map_obarray (Vobarray, apropos_accum, string);
1954 apropos_accumulate = Fsort (apropos_accumulate, Qstring_lessp);
1955 UNGCPRO;
1956 return apropos_accumulate;
1957}
1958\f
1959syms_of_keymap ()
1960{
1961 Lisp_Object tem;
1962
1963 Qkeymap = intern ("keymap");
1964 staticpro (&Qkeymap);
1965
1966/* Initialize the keymaps standardly used.
1967 Each one is the value of a Lisp variable, and is also
1968 pointed to by a C variable */
1969
ce6e5d0b 1970 global_map = Fmake_keymap (Qnil);
2c6f1a39
JB
1971 Fset (intern ("global-map"), global_map);
1972
ce6e5d0b 1973 meta_map = Fmake_keymap (Qnil);
2c6f1a39
JB
1974 Fset (intern ("esc-map"), meta_map);
1975 Ffset (intern ("ESC-prefix"), meta_map);
1976
ce6e5d0b 1977 control_x_map = Fmake_keymap (Qnil);
2c6f1a39
JB
1978 Fset (intern ("ctl-x-map"), control_x_map);
1979 Ffset (intern ("Control-X-prefix"), control_x_map);
1980
1981 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map,
1982 "Default keymap to use when reading from the minibuffer.");
ce6e5d0b 1983 Vminibuffer_local_map = Fmake_sparse_keymap (Qnil);
2c6f1a39
JB
1984
1985 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map,
1986 "Local keymap for the minibuffer when spaces are not allowed.");
ce6e5d0b 1987 Vminibuffer_local_ns_map = Fmake_sparse_keymap (Qnil);
2c6f1a39
JB
1988
1989 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map,
1990 "Local keymap for minibuffer input with completion.");
ce6e5d0b 1991 Vminibuffer_local_completion_map = Fmake_sparse_keymap (Qnil);
2c6f1a39
JB
1992
1993 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map,
1994 "Local keymap for minibuffer input with completion, for exact match.");
ce6e5d0b 1995 Vminibuffer_local_must_match_map = Fmake_sparse_keymap (Qnil);
2c6f1a39
JB
1996
1997 current_global_map = global_map;
1998
cc0a8174
JB
1999 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist,
2000 "Alist of keymaps to use for minor modes.\n\
2001Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read\n\
2002key sequences and look up bindings iff VARIABLE's value is non-nil.\n\
2003If two active keymaps bind the same key, the keymap appearing earlier\n\
2004in the list takes precedence.");
2005 Vminor_mode_map_alist = Qnil;
2006
6bbbd9b0
JB
2007 DEFVAR_LISP ("function-key-map", &Vfunction_key_map,
2008 "Keymap mapping ASCII function key sequences onto their preferred forms.\n\
2009This allows Emacs to recognize function keys sent from ASCII\n\
2010terminals at any point in a key sequence.\n\
2011\n\
2012The read-key-sequence function replaces subsequences bound by\n\
2013function-key-map with their bindings. When the current local and global\n\
2014keymaps have no binding for the current key sequence but\n\
2015function-key-map binds a suffix of the sequence to a vector,\n\
2016read-key-sequence replaces the matching suffix with its binding, and\n\
2017continues with the new sequence.\n\
2018\n\
2019For example, suppose function-key-map binds `ESC O P' to [pf1].\n\
2020Typing `ESC O P' to read-key-sequence would return [pf1]. Typing\n\
2021`C-x ESC O P' would return [?\C-x pf1]. If [pf1] were a prefix\n\
2022key, typing `ESC O P x' would return [pf1 x].");
ce6e5d0b 2023 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
6bbbd9b0 2024
2c6f1a39
JB
2025 Qsingle_key_description = intern ("single-key-description");
2026 staticpro (&Qsingle_key_description);
2027
2028 Qkey_description = intern ("key-description");
2029 staticpro (&Qkey_description);
2030
2031 Qkeymapp = intern ("keymapp");
2032 staticpro (&Qkeymapp);
2033
2034 defsubr (&Skeymapp);
2035 defsubr (&Smake_keymap);
2036 defsubr (&Smake_sparse_keymap);
2037 defsubr (&Scopy_keymap);
2038 defsubr (&Skey_binding);
2039 defsubr (&Slocal_key_binding);
2040 defsubr (&Sglobal_key_binding);
cc0a8174 2041 defsubr (&Sminor_mode_key_binding);
2c6f1a39
JB
2042 defsubr (&Sglobal_set_key);
2043 defsubr (&Slocal_set_key);
2044 defsubr (&Sdefine_key);
2045 defsubr (&Slookup_key);
2046 defsubr (&Sglobal_unset_key);
2047 defsubr (&Slocal_unset_key);
2048 defsubr (&Sdefine_prefix_command);
2049 defsubr (&Suse_global_map);
2050 defsubr (&Suse_local_map);
2051 defsubr (&Scurrent_local_map);
2052 defsubr (&Scurrent_global_map);
cc0a8174 2053 defsubr (&Scurrent_minor_mode_maps);
2c6f1a39
JB
2054 defsubr (&Saccessible_keymaps);
2055 defsubr (&Skey_description);
2056 defsubr (&Sdescribe_vector);
2057 defsubr (&Ssingle_key_description);
2058 defsubr (&Stext_char_description);
2059 defsubr (&Swhere_is_internal);
2060 defsubr (&Swhere_is);
2061 defsubr (&Sdescribe_bindings);
2062 defsubr (&Sapropos_internal);
2063}
2064
2065keys_of_keymap ()
2066{
2067 Lisp_Object tem;
2068
2069 initial_define_key (global_map, 033, "ESC-prefix");
2070 initial_define_key (global_map, Ctl('X'), "Control-X-prefix");
2071}