(Ffile_name_directory, Ffile_name_nondirectory, Ffile_name_as_directory,
[bpt/emacs.git] / src / keymap.c
CommitLineData
2c6f1a39 1/* Manipulation of keymaps
f8c25f1b 2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95 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
18160b98 21#include <config.h>
2c6f1a39
JB
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"
9ac0d9e0 29#include "blockinput.h"
d964248c 30#include "puresize.h"
2c6f1a39
JB
31
32#define min(a, b) ((a) < (b) ? (a) : (b))
33
f5b79c1c 34/* The number of elements in keymap vectors. */
2c6f1a39
JB
35#define DENSE_TABLE_SIZE (0200)
36
37/* Actually allocate storage for these variables */
38
39Lisp_Object current_global_map; /* Current global keymap */
40
41Lisp_Object global_map; /* default global key bindings */
42
43Lisp_Object meta_map; /* The keymap used for globally bound
44 ESC-prefixed default commands */
45
46Lisp_Object control_x_map; /* The keymap used for globally bound
47 C-x-prefixed default commands */
48
49/* was MinibufLocalMap */
50Lisp_Object Vminibuffer_local_map;
51 /* The keymap used by the minibuf for local
52 bindings when spaces are allowed in the
53 minibuf */
54
55/* was MinibufLocalNSMap */
56Lisp_Object Vminibuffer_local_ns_map;
57 /* The keymap used by the minibuf for local
58 bindings when spaces are not encouraged
59 in the minibuf */
60
61/* keymap used for minibuffers when doing completion */
62/* was MinibufLocalCompletionMap */
63Lisp_Object Vminibuffer_local_completion_map;
64
65/* keymap used for minibuffers when doing completion and require a match */
66/* was MinibufLocalMustMatchMap */
67Lisp_Object Vminibuffer_local_must_match_map;
68
cc0a8174
JB
69/* Alist of minor mode variables and keymaps. */
70Lisp_Object Vminor_mode_map_alist;
71
6bbbd9b0
JB
72/* Keymap mapping ASCII function key sequences onto their preferred forms.
73 Initialized by the terminal-specific lisp files. See DEFVAR for more
74 documentation. */
75Lisp_Object Vfunction_key_map;
76
d7bf9bf5
RS
77/* Keymap mapping ASCII function key sequences onto their preferred forms. */
78Lisp_Object Vkey_translation_map;
79
107fd03d
RS
80/* A list of all commands given new bindings since a certain time
81 when nil was stored here.
82 This is used to speed up recomputation of menu key equivalents
83 when Emacs starts up. t means don't record anything here. */
84Lisp_Object Vdefine_key_rebound_commands;
85
2fc66973 86Lisp_Object Qkeymapp, Qkeymap, Qnon_ascii;
2c6f1a39 87
3d248688
JB
88/* A char with the CHAR_META bit set in a vector or the 0200 bit set
89 in a string key sequence is equivalent to prefixing with this
90 character. */
2c6f1a39
JB
91extern Lisp_Object meta_prefix_char;
92
7d92e329
RS
93extern Lisp_Object Voverriding_local_map;
94
c07aec97 95static Lisp_Object define_as_prefix ();
2c6f1a39 96static Lisp_Object describe_buffer_bindings ();
d7bf9bf5 97static void describe_command (), describe_translation ();
2c6f1a39 98static void describe_map ();
2c6f1a39 99\f
cc0a8174
JB
100/* Keymap object support - constructors and predicates. */
101
ce6e5d0b 102DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 1, 0,
2c6f1a39 103 "Construct and return a new keymap, of the form (keymap VECTOR . ALIST).\n\
926a64aa 104VECTOR is a vector which holds the bindings for the ASCII\n\
2c6f1a39
JB
105characters. ALIST is an assoc-list which holds bindings for function keys,\n\
106mouse events, and any other things that appear in the input stream.\n\
ce6e5d0b
RS
107All entries in it are initially nil, meaning \"command undefined\".\n\n\
108The optional arg STRING supplies a menu name for the keymap\n\
109in case you use it as a menu with `x-popup-menu'.")
110 (string)
111 Lisp_Object string;
2c6f1a39 112{
ce6e5d0b
RS
113 Lisp_Object tail;
114 if (!NILP (string))
115 tail = Fcons (string, Qnil);
116 else
117 tail = Qnil;
2c6f1a39
JB
118 return Fcons (Qkeymap,
119 Fcons (Fmake_vector (make_number (DENSE_TABLE_SIZE), Qnil),
ce6e5d0b 120 tail));
2c6f1a39
JB
121}
122
ce6e5d0b 123DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 1, 0,
2c6f1a39
JB
124 "Construct and return a new sparse-keymap list.\n\
125Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),\n\
126which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),\n\
127which binds the function key or mouse event SYMBOL to DEFINITION.\n\
ce6e5d0b
RS
128Initially the alist is nil.\n\n\
129The optional arg STRING supplies a menu name for the keymap\n\
130in case you use it as a menu with `x-popup-menu'.")
131 (string)
132 Lisp_Object string;
2c6f1a39 133{
ce6e5d0b
RS
134 if (!NILP (string))
135 return Fcons (Qkeymap, Fcons (string, Qnil));
2c6f1a39
JB
136 return Fcons (Qkeymap, Qnil);
137}
138
139/* This function is used for installing the standard key bindings
140 at initialization time.
141
142 For example:
143
e25c4e44 144 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); */
2c6f1a39
JB
145
146void
147initial_define_key (keymap, key, defname)
148 Lisp_Object keymap;
149 int key;
150 char *defname;
151{
152 store_in_keymap (keymap, make_number (key), intern (defname));
153}
154
e25c4e44
JB
155void
156initial_define_lispy_key (keymap, keyname, defname)
157 Lisp_Object keymap;
158 char *keyname;
159 char *defname;
160{
161 store_in_keymap (keymap, intern (keyname), intern (defname));
162}
163
2c6f1a39
JB
164/* Define character fromchar in map frommap as an alias for character
165 tochar in map tomap. Subsequent redefinitions of the latter WILL
166 affect the former. */
167
168#if 0
169void
170synkey (frommap, fromchar, tomap, tochar)
171 struct Lisp_Vector *frommap, *tomap;
172 int fromchar, tochar;
173{
174 Lisp_Object v, c;
bff4ec1f 175 XSETVECTOR (v, tomap);
6e344130 176 XSETFASTINT (c, tochar);
2c6f1a39
JB
177 frommap->contents[fromchar] = Fcons (v, c);
178}
179#endif /* 0 */
180
181DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0,
182 "Return t if ARG is a keymap.\n\
1d8d96fa 183\n\
926a64aa 184A keymap is a list (keymap . ALIST),\n\
90f80bcf 185or a symbol whose function definition is itself a keymap.\n\
1d8d96fa 186ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);\n\
926a64aa
RS
187a vector of densely packed bindings for small character codes\n\
188is also allowed as an element.")
2c6f1a39
JB
189 (object)
190 Lisp_Object object;
191{
d09b2024 192 return (NILP (get_keymap_1 (object, 0, 0)) ? Qnil : Qt);
2c6f1a39
JB
193}
194
195/* Check that OBJECT is a keymap (after dereferencing through any
d09b2024
JB
196 symbols). If it is, return it.
197
198 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
199 is an autoload form, do the autoload and try again.
21a0d7a0 200 If AUTOLOAD is nonzero, callers must assume GC is possible.
d09b2024
JB
201
202 ERROR controls how we respond if OBJECT isn't a keymap.
203 If ERROR is non-zero, signal an error; otherwise, just return Qnil.
204
205 Note that most of the time, we don't want to pursue autoloads.
206 Functions like Faccessible_keymaps which scan entire keymap trees
207 shouldn't load every autoloaded keymap. I'm not sure about this,
208 but it seems to me that only read_key_sequence, Flookup_key, and
209 Fdefine_key should cause keymaps to be autoloaded. */
210
2c6f1a39 211Lisp_Object
d09b2024 212get_keymap_1 (object, error, autoload)
2c6f1a39 213 Lisp_Object object;
d09b2024 214 int error, autoload;
2c6f1a39 215{
d09b2024 216 Lisp_Object tem;
2c6f1a39 217
d09b2024 218 autoload_retry:
502ddf23 219 tem = indirect_function (object);
2c6f1a39
JB
220 if (CONSP (tem) && EQ (XCONS (tem)->car, Qkeymap))
221 return tem;
f5b79c1c 222
8e4dfd54
JB
223 /* Should we do an autoload? Autoload forms for keymaps have
224 Qkeymap as their fifth element. */
d09b2024 225 if (autoload
47684cd9 226 && SYMBOLP (object)
d09b2024
JB
227 && CONSP (tem)
228 && EQ (XCONS (tem)->car, Qautoload))
229 {
8e4dfd54 230 Lisp_Object tail;
d09b2024 231
8e4dfd54
JB
232 tail = Fnth (make_number (4), tem);
233 if (EQ (tail, Qkeymap))
234 {
235 struct gcpro gcpro1, gcpro2;
d09b2024 236
81fa9e2f
RS
237 GCPRO2 (tem, object);
238 do_autoload (tem, object);
8e4dfd54
JB
239 UNGCPRO;
240
241 goto autoload_retry;
242 }
d09b2024
JB
243 }
244
2c6f1a39
JB
245 if (error)
246 wrong_type_argument (Qkeymapp, object);
cc0a8174
JB
247 else
248 return Qnil;
2c6f1a39
JB
249}
250
d09b2024
JB
251
252/* Follow any symbol chaining, and return the keymap denoted by OBJECT.
253 If OBJECT doesn't denote a keymap at all, signal an error. */
2c6f1a39
JB
254Lisp_Object
255get_keymap (object)
256 Lisp_Object object;
257{
224a16e8 258 return get_keymap_1 (object, 1, 0);
2c6f1a39
JB
259}
260
261
2c6f1a39 262/* Look up IDX in MAP. IDX may be any sort of event.
f5b79c1c 263 Note that this does only one level of lookup; IDX must be a single
e25c4e44
JB
264 event, not a sequence.
265
266 If T_OK is non-zero, bindings for Qt are treated as default
267 bindings; any key left unmentioned by other tables and bindings is
268 given the binding of Qt.
269
c07aec97
RS
270 If T_OK is zero, bindings for Qt are not treated specially.
271
272 If NOINHERIT, don't accept a subkeymap found in an inherited keymap. */
2c6f1a39
JB
273
274Lisp_Object
c07aec97 275access_keymap (map, idx, t_ok, noinherit)
2c6f1a39
JB
276 Lisp_Object map;
277 Lisp_Object idx;
e25c4e44 278 int t_ok;
c07aec97 279 int noinherit;
2c6f1a39 280{
c07aec97
RS
281 int noprefix = 0;
282 Lisp_Object val;
283
2c6f1a39
JB
284 /* If idx is a list (some sort of mouse click, perhaps?),
285 the index we want to use is the car of the list, which
286 ought to be a symbol. */
cebd887d 287 idx = EVENT_HEAD (idx);
2c6f1a39 288
f5b79c1c
JB
289 /* If idx is a symbol, it might have modifiers, which need to
290 be put in the canonical order. */
47684cd9 291 if (SYMBOLP (idx))
f5b79c1c 292 idx = reorder_modifiers (idx);
2732bdbb
RS
293 else if (INTEGERP (idx))
294 /* Clobber the high bits that can be present on a machine
295 with more than 24 bits of integer. */
6e344130 296 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
2c6f1a39 297
f5b79c1c
JB
298 {
299 Lisp_Object tail;
e9b6dfb0 300 Lisp_Object t_binding;
2c6f1a39 301
e9b6dfb0 302 t_binding = Qnil;
f5b79c1c 303 for (tail = map; CONSP (tail); tail = XCONS (tail)->cdr)
2c6f1a39 304 {
e9b6dfb0 305 Lisp_Object binding;
f5b79c1c 306
e9b6dfb0 307 binding = XCONS (tail)->car;
783a2838 308 if (SYMBOLP (binding))
f5b79c1c 309 {
c07aec97
RS
310 /* If NOINHERIT, stop finding prefix definitions
311 after we pass a second occurrence of the `keymap' symbol. */
312 if (noinherit && EQ (binding, Qkeymap) && ! EQ (tail, map))
313 noprefix = 1;
783a2838
KH
314 }
315 else if (CONSP (binding))
316 {
f5b79c1c 317 if (EQ (XCONS (binding)->car, idx))
c07aec97
RS
318 {
319 val = XCONS (binding)->cdr;
320 if (noprefix && CONSP (val) && EQ (XCONS (val)->car, Qkeymap))
321 return Qnil;
322 return val;
323 }
e25c4e44
JB
324 if (t_ok && EQ (XCONS (binding)->car, Qt))
325 t_binding = XCONS (binding)->cdr;
783a2838
KH
326 }
327 else if (VECTORP (binding))
328 {
be3bfff1 329 if (NATNUMP (idx) && XFASTINT (idx) < XVECTOR (binding)->size)
c07aec97 330 {
783a2838 331 val = XVECTOR (binding)->contents[XFASTINT (idx)];
c07aec97
RS
332 if (noprefix && CONSP (val) && EQ (XCONS (val)->car, Qkeymap))
333 return Qnil;
334 return val;
335 }
f5b79c1c 336 }
20218e2f
JB
337
338 QUIT;
2c6f1a39 339 }
fde3a52f 340
e25c4e44
JB
341 return t_binding;
342 }
2c6f1a39
JB
343}
344
345/* Given OBJECT which was found in a slot in a keymap,
346 trace indirect definitions to get the actual definition of that slot.
347 An indirect definition is a list of the form
348 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
349 and INDEX is the object to look up in KEYMAP to yield the definition.
350
351 Also if OBJECT has a menu string as the first element,
224a16e8
RS
352 remove that. Also remove a menu help string as second element.
353
354 If AUTOLOAD is nonzero, load autoloadable keymaps
355 that are referred to with indirection. */
2c6f1a39
JB
356
357Lisp_Object
224a16e8 358get_keyelt (object, autoload)
2c6f1a39 359 register Lisp_Object object;
224a16e8 360 int autoload;
2c6f1a39
JB
361{
362 while (1)
363 {
364 register Lisp_Object map, tem;
365
fde3a52f 366 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
224a16e8 367 map = get_keymap_1 (Fcar_safe (object), 0, autoload);
2c6f1a39 368 tem = Fkeymapp (map);
265a9e55 369 if (!NILP (tem))
c07aec97 370 object = access_keymap (map, Fcdr (object), 0, 0);
2c6f1a39
JB
371
372 /* If the keymap contents looks like (STRING . DEFN),
373 use DEFN.
374 Keymap alist elements like (CHAR MENUSTRING . DEFN)
375 will be used by HierarKey menus. */
47684cd9
RS
376 else if (CONSP (object)
377 && STRINGP (XCONS (object)->car))
1a8c3f10
RS
378 {
379 object = XCONS (object)->cdr;
380 /* Also remove a menu help string, if any,
381 following the menu item name. */
416349ec 382 if (CONSP (object) && STRINGP (XCONS (object)->car))
1a8c3f10 383 object = XCONS (object)->cdr;
c6ec9f6e
RS
384 /* Also remove the sublist that caches key equivalences, if any. */
385 if (CONSP (object)
386 && CONSP (XCONS (object)->car))
ffab2bd6 387 {
c6ec9f6e
RS
388 Lisp_Object carcar;
389 carcar = XCONS (XCONS (object)->car)->car;
390 if (NILP (carcar) || VECTORP (carcar))
ffab2bd6
RS
391 object = XCONS (object)->cdr;
392 }
1a8c3f10 393 }
2c6f1a39
JB
394
395 else
396 /* Anything else is really the value. */
397 return object;
398 }
399}
400
401Lisp_Object
402store_in_keymap (keymap, idx, def)
403 Lisp_Object keymap;
404 register Lisp_Object idx;
405 register Lisp_Object def;
406{
dce4372a
RS
407 /* If we are preparing to dump, and DEF is a menu element
408 with a menu item string, copy it to ensure it is not pure. */
d964248c 409 if (CONSP (def) && PURE_P (def) && STRINGP (XCONS (def)->car))
32ce36ad
RS
410 def = Fcons (XCONS (def)->car, XCONS (def)->cdr);
411
416349ec 412 if (!CONSP (keymap) || ! EQ (XCONS (keymap)->car, Qkeymap))
f5b79c1c
JB
413 error ("attempt to define a key in a non-keymap");
414
2c6f1a39
JB
415 /* If idx is a list (some sort of mouse click, perhaps?),
416 the index we want to use is the car of the list, which
417 ought to be a symbol. */
cebd887d 418 idx = EVENT_HEAD (idx);
2c6f1a39 419
f5b79c1c
JB
420 /* If idx is a symbol, it might have modifiers, which need to
421 be put in the canonical order. */
416349ec 422 if (SYMBOLP (idx))
f5b79c1c 423 idx = reorder_modifiers (idx);
2732bdbb
RS
424 else if (INTEGERP (idx))
425 /* Clobber the high bits that can be present on a machine
426 with more than 24 bits of integer. */
6e344130 427 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
f5b79c1c
JB
428
429 /* Scan the keymap for a binding of idx. */
2c6f1a39 430 {
f5b79c1c 431 Lisp_Object tail;
2c6f1a39 432
f5b79c1c
JB
433 /* The cons after which we should insert new bindings. If the
434 keymap has a table element, we record its position here, so new
435 bindings will go after it; this way, the table will stay
436 towards the front of the alist and character lookups in dense
437 keymaps will remain fast. Otherwise, this just points at the
438 front of the keymap. */
e9b6dfb0 439 Lisp_Object insertion_point;
2c6f1a39 440
e9b6dfb0 441 insertion_point = keymap;
f5b79c1c 442 for (tail = XCONS (keymap)->cdr; CONSP (tail); tail = XCONS (tail)->cdr)
2c6f1a39 443 {
e9b6dfb0 444 Lisp_Object elt;
f5b79c1c 445
e9b6dfb0 446 elt = XCONS (tail)->car;
783a2838 447 if (VECTORP (elt))
f5b79c1c 448 {
be3bfff1 449 if (NATNUMP (idx) && XFASTINT (idx) < XVECTOR (elt)->size)
f5b79c1c
JB
450 {
451 XVECTOR (elt)->contents[XFASTINT (idx)] = def;
452 return def;
453 }
454 insertion_point = tail;
783a2838
KH
455 }
456 else if (CONSP (elt))
457 {
f5b79c1c
JB
458 if (EQ (idx, XCONS (elt)->car))
459 {
460 XCONS (elt)->cdr = def;
461 return def;
462 }
783a2838
KH
463 }
464 else if (SYMBOLP (elt))
465 {
f5b79c1c
JB
466 /* If we find a 'keymap' symbol in the spine of KEYMAP,
467 then we must have found the start of a second keymap
468 being used as the tail of KEYMAP, and a binding for IDX
469 should be inserted before it. */
470 if (EQ (elt, Qkeymap))
471 goto keymap_end;
f5b79c1c 472 }
0188441d
JB
473
474 QUIT;
2c6f1a39 475 }
2c6f1a39 476
f5b79c1c
JB
477 keymap_end:
478 /* We have scanned the entire keymap, and not found a binding for
479 IDX. Let's add one. */
32ce36ad
RS
480 XCONS (insertion_point)->cdr
481 = Fcons (Fcons (idx, def), XCONS (insertion_point)->cdr);
f5b79c1c
JB
482 }
483
2c6f1a39
JB
484 return def;
485}
486
f5b79c1c 487
2c6f1a39
JB
488DEFUN ("copy-keymap", Fcopy_keymap, Scopy_keymap, 1, 1, 0,
489 "Return a copy of the keymap KEYMAP.\n\
490The copy starts out with the same definitions of KEYMAP,\n\
491but changing either the copy or KEYMAP does not affect the other.\n\
1d8d96fa
JB
492Any key definitions that are subkeymaps are recursively copied.\n\
493However, a key definition which is a symbol whose definition is a keymap\n\
494is not copied.")
2c6f1a39
JB
495 (keymap)
496 Lisp_Object keymap;
497{
498 register Lisp_Object copy, tail;
499
500 copy = Fcopy_alist (get_keymap (keymap));
2c6f1a39 501
f5b79c1c 502 for (tail = copy; CONSP (tail); tail = XCONS (tail)->cdr)
2c6f1a39 503 {
e9b6dfb0 504 Lisp_Object elt;
2c6f1a39 505
e9b6dfb0 506 elt = XCONS (tail)->car;
416349ec 507 if (VECTORP (elt))
2c6f1a39 508 {
f5b79c1c 509 int i;
2c6f1a39 510
f5b79c1c
JB
511 elt = Fcopy_sequence (elt);
512 XCONS (tail)->car = elt;
2c6f1a39 513
926a64aa 514 for (i = 0; i < XVECTOR (elt)->size; i++)
416349ec 515 if (!SYMBOLP (XVECTOR (elt)->contents[i])
98006242 516 && ! NILP (Fkeymapp (XVECTOR (elt)->contents[i])))
f5b79c1c
JB
517 XVECTOR (elt)->contents[i] =
518 Fcopy_keymap (XVECTOR (elt)->contents[i]);
2c6f1a39 519 }
d65a13c5
KH
520 else if (CONSP (elt))
521 {
522 /* Skip the optional menu string. */
523 if (CONSP (XCONS (elt)->cdr)
524 && STRINGP (XCONS (XCONS (elt)->cdr)->car))
525 {
526 Lisp_Object tem;
527
528 /* Copy the cell, since copy-alist didn't go this deep. */
529 XCONS (elt)->cdr = Fcons (XCONS (XCONS (elt)->cdr)->car,
530 XCONS (XCONS (elt)->cdr)->cdr);
531 elt = XCONS (elt)->cdr;
532
533 /* Also skip the optional menu help string. */
534 if (CONSP (XCONS (elt)->cdr)
535 && STRINGP (XCONS (XCONS (elt)->cdr)->car))
536 {
537 XCONS (elt)->cdr = Fcons (XCONS (XCONS (elt)->cdr)->car,
538 XCONS (XCONS (elt)->cdr)->cdr);
539 elt = XCONS (elt)->cdr;
540 }
541 /* There may also be a list that caches key equivalences.
542 Just delete it for the new keymap. */
543 if (CONSP (XCONS (elt)->cdr)
544 && CONSP (XCONS (XCONS (elt)->cdr)->car)
545 && (NILP (tem = XCONS (XCONS (XCONS (elt)->cdr)->car)->car)
546 || VECTORP (tem)))
547 XCONS (elt)->cdr = XCONS (XCONS (elt)->cdr)->cdr;
548 }
549 if (CONSP (elt)
550 && ! SYMBOLP (XCONS (elt)->cdr)
551 && ! NILP (Fkeymapp (XCONS (elt)->cdr)))
552 XCONS (elt)->cdr = Fcopy_keymap (XCONS (elt)->cdr);
553 }
2c6f1a39
JB
554 }
555
556 return copy;
557}
558\f
cc0a8174
JB
559/* Simple Keymap mutators and accessors. */
560
21a0d7a0
RS
561/* GC is possible in this function if it autoloads a keymap. */
562
2c6f1a39
JB
563DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0,
564 "Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.\n\
565KEYMAP is a keymap. KEY is a string or a vector of symbols and characters\n\
566meaning a sequence of keystrokes and events.\n\
c818754b
RS
567Non-ASCII characters with codes above 127 (such as ISO Latin-1)\n\
568can be included if you use a vector.\n\
2c6f1a39
JB
569DEF is anything that can be a key's definition:\n\
570 nil (means key is undefined in this keymap),\n\
571 a command (a Lisp function suitable for interactive calling)\n\
572 a string (treated as a keyboard macro),\n\
573 a keymap (to define a prefix key),\n\
574 a symbol. When the key is looked up, the symbol will stand for its\n\
575 function definition, which should at that time be one of the above,\n\
576 or another symbol whose function definition is used, etc.\n\
577 a cons (STRING . DEFN), meaning that DEFN is the definition\n\
578 (DEFN should be a valid definition in its own right),\n\
6e8290aa
JB
579 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.\n\
580\n\
581If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at\n\
582the front of KEYMAP.")
2c6f1a39 583 (keymap, key, def)
d09b2024 584 Lisp_Object keymap;
2c6f1a39
JB
585 Lisp_Object key;
586 Lisp_Object def;
587{
588 register int idx;
589 register Lisp_Object c;
590 register Lisp_Object tem;
591 register Lisp_Object cmd;
592 int metized = 0;
6ba6e250 593 int meta_bit;
2c6f1a39 594 int length;
d09b2024 595 struct gcpro gcpro1, gcpro2, gcpro3;
2c6f1a39 596
224a16e8 597 keymap = get_keymap_1 (keymap, 1, 1);
2c6f1a39 598
416349ec 599 if (!VECTORP (key) && !STRINGP (key))
2c6f1a39
JB
600 key = wrong_type_argument (Qarrayp, key);
601
d09b2024 602 length = XFASTINT (Flength (key));
2c6f1a39
JB
603 if (length == 0)
604 return Qnil;
605
107fd03d
RS
606 if (SYMBOLP (def) && !EQ (Vdefine_key_rebound_commands, Qt))
607 Vdefine_key_rebound_commands = Fcons (def, Vdefine_key_rebound_commands);
608
d09b2024
JB
609 GCPRO3 (keymap, key, def);
610
416349ec 611 if (VECTORP (key))
6ba6e250
RS
612 meta_bit = meta_modifier;
613 else
614 meta_bit = 0x80;
615
2c6f1a39
JB
616 idx = 0;
617 while (1)
618 {
619 c = Faref (key, make_number (idx));
620
f09bc924 621 if (CONSP (c) && lucid_event_type_list_p (c))
41015a19 622 c = Fevent_convert_list (c);
f09bc924 623
416349ec 624 if (INTEGERP (c)
6ba6e250 625 && (XINT (c) & meta_bit)
2c6f1a39
JB
626 && !metized)
627 {
628 c = meta_prefix_char;
629 metized = 1;
630 }
631 else
632 {
416349ec 633 if (INTEGERP (c))
0b8fc2d4 634 XSETINT (c, XINT (c) & ~meta_bit);
2c6f1a39
JB
635
636 metized = 0;
637 idx++;
638 }
639
5907b863 640 if (! INTEGERP (c) && ! SYMBOLP (c) && ! CONSP (c))
4b04c52e 641 error ("Key sequence contains invalid events");
5907b863 642
2c6f1a39 643 if (idx == length)
d09b2024 644 RETURN_UNGCPRO (store_in_keymap (keymap, c, def));
2c6f1a39 645
224a16e8 646 cmd = get_keyelt (access_keymap (keymap, c, 0, 1), 1);
2c6f1a39 647
c07aec97 648 /* If this key is undefined, make it a prefix. */
265a9e55 649 if (NILP (cmd))
c07aec97 650 cmd = define_as_prefix (keymap, c);
2c6f1a39 651
d09b2024
JB
652 keymap = get_keymap_1 (cmd, 0, 1);
653 if (NILP (keymap))
e9b6dfb0
KH
654 /* We must use Fkey_description rather than just passing key to
655 error; key might be a vector, not a string. */
656 error ("Key sequence %s uses invalid prefix characters",
657 XSTRING (Fkey_description (key))->data);
2c6f1a39
JB
658 }
659}
660
661/* Value is number if KEY is too long; NIL if valid but has no definition. */
21a0d7a0 662/* GC is possible in this function if it autoloads a keymap. */
2c6f1a39 663
7c140252 664DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 3, 0,
2c6f1a39
JB
665 "In keymap KEYMAP, look up key sequence KEY. Return the definition.\n\
666nil means undefined. See doc of `define-key' for kinds of definitions.\n\
7c140252 667\n\
2c6f1a39
JB
668A number as value means KEY is \"too long\";\n\
669that is, characters or symbols in it except for the last one\n\
670fail to be a valid sequence of prefix characters in KEYMAP.\n\
671The number is how many characters at the front of KEY\n\
7c140252
JB
672it takes to reach a non-prefix command.\n\
673\n\
674Normally, `lookup-key' ignores bindings for t, which act as default\n\
675bindings, used when nothing else in the keymap applies; this makes it\n\
b31a4218 676usable as a general function for probing keymaps. However, if the\n\
7c140252
JB
677third optional argument ACCEPT-DEFAULT is non-nil, `lookup-key' will\n\
678recognize the default bindings, just as `read-key-sequence' does.")
679 (keymap, key, accept_default)
2c6f1a39
JB
680 register Lisp_Object keymap;
681 Lisp_Object key;
7c140252 682 Lisp_Object accept_default;
2c6f1a39
JB
683{
684 register int idx;
685 register Lisp_Object tem;
686 register Lisp_Object cmd;
687 register Lisp_Object c;
688 int metized = 0;
689 int length;
7c140252 690 int t_ok = ! NILP (accept_default);
6ba6e250 691 int meta_bit;
21a0d7a0 692 struct gcpro gcpro1;
2c6f1a39 693
224a16e8 694 keymap = get_keymap_1 (keymap, 1, 1);
2c6f1a39 695
416349ec 696 if (!VECTORP (key) && !STRINGP (key))
2c6f1a39
JB
697 key = wrong_type_argument (Qarrayp, key);
698
d09b2024 699 length = XFASTINT (Flength (key));
2c6f1a39
JB
700 if (length == 0)
701 return keymap;
702
416349ec 703 if (VECTORP (key))
6ba6e250
RS
704 meta_bit = meta_modifier;
705 else
706 meta_bit = 0x80;
707
21a0d7a0
RS
708 GCPRO1 (key);
709
2c6f1a39
JB
710 idx = 0;
711 while (1)
712 {
713 c = Faref (key, make_number (idx));
714
f09bc924 715 if (CONSP (c) && lucid_event_type_list_p (c))
41015a19 716 c = Fevent_convert_list (c);
f09bc924 717
416349ec 718 if (INTEGERP (c)
6ba6e250 719 && (XINT (c) & meta_bit)
2c6f1a39
JB
720 && !metized)
721 {
722 c = meta_prefix_char;
723 metized = 1;
724 }
725 else
726 {
416349ec 727 if (INTEGERP (c))
6ba6e250 728 XSETINT (c, XINT (c) & ~meta_bit);
2c6f1a39
JB
729
730 metized = 0;
731 idx++;
732 }
733
224a16e8 734 cmd = get_keyelt (access_keymap (keymap, c, t_ok, 0), 1);
2c6f1a39 735 if (idx == length)
21a0d7a0 736 RETURN_UNGCPRO (cmd);
2c6f1a39 737
224a16e8 738 keymap = get_keymap_1 (cmd, 0, 1);
d09b2024 739 if (NILP (keymap))
21a0d7a0 740 RETURN_UNGCPRO (make_number (idx));
2c6f1a39 741
2c6f1a39
JB
742 QUIT;
743 }
744}
745
c07aec97
RS
746/* Make KEYMAP define event C as a keymap (i.e., as a prefix).
747 Assume that currently it does not define C at all.
748 Return the keymap. */
749
750static Lisp_Object
751define_as_prefix (keymap, c)
752 Lisp_Object keymap, c;
753{
754 Lisp_Object inherit, cmd;
755
756 cmd = Fmake_sparse_keymap (Qnil);
757 /* If this key is defined as a prefix in an inherited keymap,
758 make it a prefix in this map, and make its definition
759 inherit the other prefix definition. */
760 inherit = access_keymap (keymap, c, 0, 0);
761 if (NILP (inherit))
762 {
763 /* If there's an inherited keymap
764 and it doesn't define this key,
765 make it define this key. */
766 Lisp_Object tail;
767
768 for (tail = Fcdr (keymap); CONSP (tail); tail = XCONS (tail)->cdr)
769 if (EQ (XCONS (tail)->car, Qkeymap))
770 break;
771
772 if (!NILP (tail))
773 inherit = define_as_prefix (tail, c);
774 }
775
776 cmd = nconc2 (cmd, inherit);
777 store_in_keymap (keymap, c, cmd);
778
779 return cmd;
780}
781
0b8fc2d4
RS
782/* Append a key to the end of a key sequence. We always make a vector. */
783
2c6f1a39
JB
784Lisp_Object
785append_key (key_sequence, key)
786 Lisp_Object key_sequence, key;
787{
788 Lisp_Object args[2];
789
790 args[0] = key_sequence;
791
0b8fc2d4
RS
792 args[1] = Fcons (key, Qnil);
793 return Fvconcat (2, args);
2c6f1a39
JB
794}
795
796\f
cc0a8174
JB
797/* Global, local, and minor mode keymap stuff. */
798
265a9e55 799/* We can't put these variables inside current_minor_maps, since under
6bbbd9b0
JB
800 some systems, static gets macro-defined to be the empty string.
801 Ickypoo. */
265a9e55
JB
802static Lisp_Object *cmm_modes, *cmm_maps;
803static int cmm_size;
804
fbb90829
KH
805/* Error handler used in current_minor_maps. */
806static Lisp_Object
807current_minor_maps_error ()
808{
809 return Qnil;
810}
811
cc0a8174
JB
812/* Store a pointer to an array of the keymaps of the currently active
813 minor modes in *buf, and return the number of maps it contains.
814
815 This function always returns a pointer to the same buffer, and may
816 free or reallocate it, so if you want to keep it for a long time or
817 hand it out to lisp code, copy it. This procedure will be called
818 for every key sequence read, so the nice lispy approach (return a
819 new assoclist, list, what have you) for each invocation would
820 result in a lot of consing over time.
821
822 If we used xrealloc/xmalloc and ran out of memory, they would throw
823 back to the command loop, which would try to read a key sequence,
824 which would call this function again, resulting in an infinite
825 loop. Instead, we'll use realloc/malloc and silently truncate the
826 list, let the key sequence be read, and hope some other piece of
827 code signals the error. */
828int
829current_minor_maps (modeptr, mapptr)
830 Lisp_Object **modeptr, **mapptr;
831{
cc0a8174 832 int i = 0;
6bbbd9b0 833 Lisp_Object alist, assoc, var, val;
cc0a8174
JB
834
835 for (alist = Vminor_mode_map_alist;
836 CONSP (alist);
837 alist = XCONS (alist)->cdr)
9f56ecfc
KH
838 if ((assoc = XCONS (alist)->car, CONSP (assoc))
839 && (var = XCONS (assoc)->car, SYMBOLP (var))
840 && (val = find_symbol_value (var), ! EQ (val, Qunbound))
6bbbd9b0 841 && ! NILP (val))
cc0a8174 842 {
fbb90829
KH
843 Lisp_Object temp;
844
265a9e55 845 if (i >= cmm_size)
cc0a8174
JB
846 {
847 Lisp_Object *newmodes, *newmaps;
848
265a9e55 849 if (cmm_maps)
cc0a8174 850 {
9ac0d9e0 851 BLOCK_INPUT;
e58077c8 852 cmm_size *= 2;
64116ad5
RS
853 newmodes
854 = (Lisp_Object *) realloc (cmm_modes,
855 cmm_size * sizeof (Lisp_Object));
856 newmaps
857 = (Lisp_Object *) realloc (cmm_maps,
858 cmm_size * sizeof (Lisp_Object));
9ac0d9e0 859 UNBLOCK_INPUT;
cc0a8174
JB
860 }
861 else
862 {
9ac0d9e0 863 BLOCK_INPUT;
e58077c8 864 cmm_size = 30;
64116ad5
RS
865 newmodes
866 = (Lisp_Object *) malloc (cmm_size * sizeof (Lisp_Object));
867 newmaps
868 = (Lisp_Object *) malloc (cmm_size * sizeof (Lisp_Object));
9ac0d9e0 869 UNBLOCK_INPUT;
cc0a8174
JB
870 }
871
872 if (newmaps && newmodes)
873 {
265a9e55
JB
874 cmm_modes = newmodes;
875 cmm_maps = newmaps;
cc0a8174
JB
876 }
877 else
878 break;
879 }
fbb90829
KH
880
881 /* Get the keymap definition--or nil if it is not defined. */
882 temp = internal_condition_case_1 (Findirect_function,
883 XCONS (assoc)->cdr,
884 Qerror, current_minor_maps_error);
885 if (!NILP (temp))
886 {
887 cmm_modes[i] = var;
888 cmm_maps [i] = temp;
889 i++;
890 }
cc0a8174
JB
891 }
892
265a9e55
JB
893 if (modeptr) *modeptr = cmm_modes;
894 if (mapptr) *mapptr = cmm_maps;
cc0a8174
JB
895 return i;
896}
897
21a0d7a0
RS
898/* GC is possible in this function if it autoloads a keymap. */
899
7c140252 900DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 2, 0,
2c6f1a39 901 "Return the binding for command KEY in current keymaps.\n\
7c140252
JB
902KEY is a string or vector, a sequence of keystrokes.\n\
903The binding is probably a symbol with a function definition.\n\
904\n\
905Normally, `key-binding' ignores bindings for t, which act as default\n\
906bindings, used when nothing else in the keymap applies; this makes it\n\
d831234b
RS
907usable as a general function for probing keymaps. However, if the\n\
908optional second argument ACCEPT-DEFAULT is non-nil, `key-binding' does\n\
7c140252
JB
909recognize the default bindings, just as `read-key-sequence' does.")
910 (key, accept_default)
c2a2858a 911 Lisp_Object key, accept_default;
2c6f1a39 912{
cc0a8174
JB
913 Lisp_Object *maps, value;
914 int nmaps, i;
21a0d7a0
RS
915 struct gcpro gcpro1;
916
917 GCPRO1 (key);
cc0a8174 918
e784236d
KH
919 if (!NILP (current_kboard->Voverriding_terminal_local_map))
920 {
921 value = Flookup_key (current_kboard->Voverriding_terminal_local_map,
922 key, accept_default);
923 if (! NILP (value) && !INTEGERP (value))
924 RETURN_UNGCPRO (value);
925 }
926 else if (!NILP (Voverriding_local_map))
2c6f1a39 927 {
7d92e329 928 value = Flookup_key (Voverriding_local_map, key, accept_default);
416349ec 929 if (! NILP (value) && !INTEGERP (value))
21a0d7a0 930 RETURN_UNGCPRO (value);
2c6f1a39 931 }
7d92e329
RS
932 else
933 {
d964248c
KH
934 Lisp_Object local;
935
7d92e329 936 nmaps = current_minor_maps (0, &maps);
21a0d7a0
RS
937 /* Note that all these maps are GCPRO'd
938 in the places where we found them. */
939
7d92e329
RS
940 for (i = 0; i < nmaps; i++)
941 if (! NILP (maps[i]))
942 {
943 value = Flookup_key (maps[i], key, accept_default);
416349ec 944 if (! NILP (value) && !INTEGERP (value))
21a0d7a0 945 RETURN_UNGCPRO (value);
7d92e329
RS
946 }
947
d964248c
KH
948 local = get_local_map (PT, current_buffer);
949
950 if (! NILP (local))
7d92e329 951 {
d964248c 952 value = Flookup_key (local, key, accept_default);
416349ec 953 if (! NILP (value) && !INTEGERP (value))
21a0d7a0 954 RETURN_UNGCPRO (value);
7d92e329
RS
955 }
956 }
cc0a8174 957
7c140252 958 value = Flookup_key (current_global_map, key, accept_default);
21a0d7a0 959 UNGCPRO;
416349ec 960 if (! NILP (value) && !INTEGERP (value))
cc0a8174
JB
961 return value;
962
963 return Qnil;
2c6f1a39
JB
964}
965
21a0d7a0
RS
966/* GC is possible in this function if it autoloads a keymap. */
967
7c140252 968DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 2, 0,
2c6f1a39
JB
969 "Return the binding for command KEYS in current local keymap only.\n\
970KEYS is a string, a sequence of keystrokes.\n\
7c140252
JB
971The binding is probably a symbol with a function definition.\n\
972\n\
973If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
974bindings; see the description of `lookup-key' for more details about this.")
975 (keys, accept_default)
976 Lisp_Object keys, accept_default;
2c6f1a39
JB
977{
978 register Lisp_Object map;
979 map = current_buffer->keymap;
265a9e55 980 if (NILP (map))
2c6f1a39 981 return Qnil;
7c140252 982 return Flookup_key (map, keys, accept_default);
2c6f1a39
JB
983}
984
21a0d7a0
RS
985/* GC is possible in this function if it autoloads a keymap. */
986
7c140252 987DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 2, 0,
2c6f1a39
JB
988 "Return the binding for command KEYS in current global keymap only.\n\
989KEYS is a string, a sequence of keystrokes.\n\
6bbbd9b0
JB
990The binding is probably a symbol with a function definition.\n\
991This function's return values are the same as those of lookup-key\n\
21a0d7a0 992\(which see).\n\
7c140252
JB
993\n\
994If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
995bindings; see the description of `lookup-key' for more details about this.")
996 (keys, accept_default)
997 Lisp_Object keys, accept_default;
2c6f1a39 998{
7c140252 999 return Flookup_key (current_global_map, keys, accept_default);
2c6f1a39
JB
1000}
1001
21a0d7a0
RS
1002/* GC is possible in this function if it autoloads a keymap. */
1003
7c140252 1004DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0,
cc0a8174
JB
1005 "Find the visible minor mode bindings of KEY.\n\
1006Return an alist of pairs (MODENAME . BINDING), where MODENAME is the\n\
1007the symbol which names the minor mode binding KEY, and BINDING is\n\
1008KEY's definition in that mode. In particular, if KEY has no\n\
1009minor-mode bindings, return nil. If the first binding is a\n\
1010non-prefix, all subsequent bindings will be omitted, since they would\n\
1011be ignored. Similarly, the list doesn't include non-prefix bindings\n\
7c140252
JB
1012that come after prefix bindings.\n\
1013\n\
1014If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1015bindings; see the description of `lookup-key' for more details about this.")
1016 (key, accept_default)
1017 Lisp_Object key, accept_default;
cc0a8174
JB
1018{
1019 Lisp_Object *modes, *maps;
1020 int nmaps;
1021 Lisp_Object binding;
1022 int i, j;
21a0d7a0 1023 struct gcpro gcpro1, gcpro2;
cc0a8174
JB
1024
1025 nmaps = current_minor_maps (&modes, &maps);
21a0d7a0
RS
1026 /* Note that all these maps are GCPRO'd
1027 in the places where we found them. */
1028
1029 binding = Qnil;
1030 GCPRO2 (key, binding);
cc0a8174
JB
1031
1032 for (i = j = 0; i < nmaps; i++)
265a9e55 1033 if (! NILP (maps[i])
7c140252 1034 && ! NILP (binding = Flookup_key (maps[i], key, accept_default))
416349ec 1035 && !INTEGERP (binding))
cc0a8174 1036 {
d09b2024 1037 if (! NILP (get_keymap (binding)))
cc0a8174
JB
1038 maps[j++] = Fcons (modes[i], binding);
1039 else if (j == 0)
21a0d7a0 1040 RETURN_UNGCPRO (Fcons (Fcons (modes[i], binding), Qnil));
cc0a8174
JB
1041 }
1042
21a0d7a0 1043 UNGCPRO;
cc0a8174
JB
1044 return Flist (j, maps);
1045}
1046
2c6f1a39 1047DEFUN ("define-prefix-command", Fdefine_prefix_command, Sdefine_prefix_command, 1, 2, 0,
cd8520b9 1048 "Define COMMAND as a prefix command. COMMAND should be a symbol.\n\
2c6f1a39 1049A new sparse keymap is stored as COMMAND's function definition and its value.\n\
1d8d96fa
JB
1050If a second optional argument MAPVAR is given, the map is stored as\n\
1051its value instead of as COMMAND's value; but COMMAND is still defined\n\
1052as a function.")
2c6f1a39
JB
1053 (name, mapvar)
1054 Lisp_Object name, mapvar;
1055{
1056 Lisp_Object map;
ce6e5d0b 1057 map = Fmake_sparse_keymap (Qnil);
2c6f1a39 1058 Ffset (name, map);
265a9e55 1059 if (!NILP (mapvar))
2c6f1a39
JB
1060 Fset (mapvar, map);
1061 else
1062 Fset (name, map);
1063 return name;
1064}
1065
1066DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0,
1067 "Select KEYMAP as the global keymap.")
1068 (keymap)
1069 Lisp_Object keymap;
1070{
1071 keymap = get_keymap (keymap);
1072 current_global_map = keymap;
6f27e7a2
RS
1073 record_asynch_buffer_change ();
1074
2c6f1a39
JB
1075 return Qnil;
1076}
1077
1078DEFUN ("use-local-map", Fuse_local_map, Suse_local_map, 1, 1, 0,
1079 "Select KEYMAP as the local keymap.\n\
1080If KEYMAP is nil, that means no local keymap.")
1081 (keymap)
1082 Lisp_Object keymap;
1083{
265a9e55 1084 if (!NILP (keymap))
2c6f1a39
JB
1085 keymap = get_keymap (keymap);
1086
1087 current_buffer->keymap = keymap;
6f27e7a2 1088 record_asynch_buffer_change ();
2c6f1a39
JB
1089
1090 return Qnil;
1091}
1092
1093DEFUN ("current-local-map", Fcurrent_local_map, Scurrent_local_map, 0, 0, 0,
1094 "Return current buffer's local keymap, or nil if it has none.")
1095 ()
1096{
1097 return current_buffer->keymap;
1098}
1099
1100DEFUN ("current-global-map", Fcurrent_global_map, Scurrent_global_map, 0, 0, 0,
1101 "Return the current global keymap.")
1102 ()
1103{
1104 return current_global_map;
1105}
cc0a8174
JB
1106
1107DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps, Scurrent_minor_mode_maps, 0, 0, 0,
1108 "Return a list of keymaps for the minor modes of the current buffer.")
1109 ()
1110{
1111 Lisp_Object *maps;
1112 int nmaps = current_minor_maps (0, &maps);
1113
1114 return Flist (nmaps, maps);
1115}
2c6f1a39 1116\f
cc0a8174
JB
1117/* Help functions for describing and documenting keymaps. */
1118
21a0d7a0
RS
1119/* This function cannot GC. */
1120
2c6f1a39 1121DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps,
53c8f9fa 1122 1, 2, 0,
2c6f1a39
JB
1123 "Find all keymaps accessible via prefix characters from KEYMAP.\n\
1124Returns a list of elements of the form (KEYS . MAP), where the sequence\n\
1125KEYS starting from KEYMAP gets you to MAP. These elements are ordered\n\
f66ef185
RS
1126so that the KEYS increase in length. The first element is (\"\" . KEYMAP).\n\
1127An optional argument PREFIX, if non-nil, should be a key sequence;\n\
1128then the value includes only maps for prefixes that start with PREFIX.")
53c8f9fa
RS
1129 (startmap, prefix)
1130 Lisp_Object startmap, prefix;
2c6f1a39 1131{
53c8f9fa
RS
1132 Lisp_Object maps, good_maps, tail;
1133 int prefixlen = 0;
1134
21a0d7a0
RS
1135 /* no need for gcpro because we don't autoload any keymaps. */
1136
53c8f9fa
RS
1137 if (!NILP (prefix))
1138 prefixlen = XINT (Flength (prefix));
2c6f1a39 1139
44a4a59b
RS
1140 if (!NILP (prefix))
1141 {
1142 /* If a prefix was specified, start with the keymap (if any) for
1143 that prefix, so we don't waste time considering other prefixes. */
1144 Lisp_Object tem;
1145 tem = Flookup_key (startmap, prefix, Qt);
1ae2097f
RS
1146 /* Flookup_key may give us nil, or a number,
1147 if the prefix is not defined in this particular map.
1148 It might even give us a list that isn't a keymap. */
1149 tem = get_keymap_1 (tem, 0, 0);
44a4a59b 1150 if (!NILP (tem))
1ae2097f 1151 maps = Fcons (Fcons (prefix, tem), Qnil);
44a4a59b
RS
1152 else
1153 return Qnil;
1154 }
1155 else
1156 maps = Fcons (Fcons (Fmake_vector (make_number (0), Qnil),
1157 get_keymap (startmap)),
1158 Qnil);
2c6f1a39
JB
1159
1160 /* For each map in the list maps,
1161 look at any other maps it points to,
1162 and stick them at the end if they are not already in the list.
1163
1164 This is a breadth-first traversal, where tail is the queue of
1165 nodes, and maps accumulates a list of all nodes visited. */
1166
f5b79c1c 1167 for (tail = maps; CONSP (tail); tail = XCONS (tail)->cdr)
2c6f1a39 1168 {
e9b6dfb0
KH
1169 register Lisp_Object thisseq, thismap;
1170 Lisp_Object last;
2c6f1a39 1171 /* Does the current sequence end in the meta-prefix-char? */
e9b6dfb0
KH
1172 int is_metized;
1173
1174 thisseq = Fcar (Fcar (tail));
1175 thismap = Fcdr (Fcar (tail));
1176 last = make_number (XINT (Flength (thisseq)) - 1);
1177 is_metized = (XINT (last) >= 0
1178 && EQ (Faref (thisseq, last), meta_prefix_char));
2c6f1a39 1179
f5b79c1c 1180 for (; CONSP (thismap); thismap = XCONS (thismap)->cdr)
2c6f1a39 1181 {
e9b6dfb0
KH
1182 Lisp_Object elt;
1183
1184 elt = XCONS (thismap)->car;
2c6f1a39 1185
f5b79c1c
JB
1186 QUIT;
1187
416349ec 1188 if (VECTORP (elt))
2c6f1a39
JB
1189 {
1190 register int i;
1191
1192 /* Vector keymap. Scan all the elements. */
db6f9d95 1193 for (i = 0; i < XVECTOR (elt)->size; i++)
2c6f1a39
JB
1194 {
1195 register Lisp_Object tem;
1196 register Lisp_Object cmd;
1197
224a16e8 1198 cmd = get_keyelt (XVECTOR (elt)->contents[i], 0);
265a9e55 1199 if (NILP (cmd)) continue;
2c6f1a39 1200 tem = Fkeymapp (cmd);
265a9e55 1201 if (!NILP (tem))
2c6f1a39
JB
1202 {
1203 cmd = get_keymap (cmd);
1204 /* Ignore keymaps that are already added to maps. */
1205 tem = Frassq (cmd, maps);
265a9e55 1206 if (NILP (tem))
2c6f1a39
JB
1207 {
1208 /* If the last key in thisseq is meta-prefix-char,
1209 turn it into a meta-ized keystroke. We know
1210 that the event we're about to append is an
f5b79c1c
JB
1211 ascii keystroke since we're processing a
1212 keymap table. */
2c6f1a39
JB
1213 if (is_metized)
1214 {
0b8fc2d4 1215 int meta_bit = meta_modifier;
2c6f1a39 1216 tem = Fcopy_sequence (thisseq);
0b8fc2d4
RS
1217
1218 Faset (tem, last, make_number (i | meta_bit));
2c6f1a39
JB
1219
1220 /* This new sequence is the same length as
1221 thisseq, so stick it in the list right
1222 after this one. */
0b8fc2d4
RS
1223 XCONS (tail)->cdr
1224 = Fcons (Fcons (tem, cmd), XCONS (tail)->cdr);
2c6f1a39
JB
1225 }
1226 else
1227 {
1228 tem = append_key (thisseq, make_number (i));
1229 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
1230 }
1231 }
1232 }
1233 }
f5b79c1c
JB
1234 }
1235 else if (CONSP (elt))
2c6f1a39 1236 {
e9b6dfb0 1237 register Lisp_Object cmd, tem, filter;
2c6f1a39 1238
224a16e8 1239 cmd = get_keyelt (XCONS (elt)->cdr, 0);
2c6f1a39
JB
1240 /* Ignore definitions that aren't keymaps themselves. */
1241 tem = Fkeymapp (cmd);
265a9e55 1242 if (!NILP (tem))
2c6f1a39
JB
1243 {
1244 /* Ignore keymaps that have been seen already. */
1245 cmd = get_keymap (cmd);
1246 tem = Frassq (cmd, maps);
265a9e55 1247 if (NILP (tem))
2c6f1a39 1248 {
53c8f9fa 1249 /* Let elt be the event defined by this map entry. */
2c6f1a39
JB
1250 elt = XCONS (elt)->car;
1251
1252 /* If the last key in thisseq is meta-prefix-char, and
1253 this entry is a binding for an ascii keystroke,
1254 turn it into a meta-ized keystroke. */
416349ec 1255 if (is_metized && INTEGERP (elt))
2c6f1a39
JB
1256 {
1257 tem = Fcopy_sequence (thisseq);
0b8fc2d4
RS
1258 Faset (tem, last,
1259 make_number (XINT (elt) | meta_modifier));
2c6f1a39
JB
1260
1261 /* This new sequence is the same length as
1262 thisseq, so stick it in the list right
1263 after this one. */
53c8f9fa
RS
1264 XCONS (tail)->cdr
1265 = Fcons (Fcons (tem, cmd), XCONS (tail)->cdr);
2c6f1a39
JB
1266 }
1267 else
1268 nconc2 (tail,
1269 Fcons (Fcons (append_key (thisseq, elt), cmd),
1270 Qnil));
1271 }
1272 }
1273 }
2c6f1a39 1274 }
2c6f1a39
JB
1275 }
1276
53c8f9fa
RS
1277 if (NILP (prefix))
1278 return maps;
1279
1280 /* Now find just the maps whose access prefixes start with PREFIX. */
1281
1282 good_maps = Qnil;
1283 for (; CONSP (maps); maps = XCONS (maps)->cdr)
1284 {
1285 Lisp_Object elt, thisseq;
1286 elt = XCONS (maps)->car;
1287 thisseq = XCONS (elt)->car;
1288 /* The access prefix must be at least as long as PREFIX,
1289 and the first elements must match those of PREFIX. */
1290 if (XINT (Flength (thisseq)) >= prefixlen)
1291 {
1292 int i;
1293 for (i = 0; i < prefixlen; i++)
1294 {
1295 Lisp_Object i1;
6e344130 1296 XSETFASTINT (i1, i);
53c8f9fa
RS
1297 if (!EQ (Faref (thisseq, i1), Faref (prefix, i1)))
1298 break;
1299 }
1300 if (i == prefixlen)
1301 good_maps = Fcons (elt, good_maps);
1302 }
1303 }
1304
1305 return Fnreverse (good_maps);
2c6f1a39
JB
1306}
1307
1308Lisp_Object Qsingle_key_description, Qkey_description;
1309
21a0d7a0
RS
1310/* This function cannot GC. */
1311
2c6f1a39
JB
1312DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0,
1313 "Return a pretty description of key-sequence KEYS.\n\
1314Control characters turn into \"C-foo\" sequences, meta into \"M-foo\"\n\
1315spaces are put between sequence elements, etc.")
1316 (keys)
1317 Lisp_Object keys;
1318{
4c7d5f13
RS
1319 int len;
1320 int i;
1321 Lisp_Object sep;
1322 Lisp_Object *args;
1323
47684cd9 1324 if (STRINGP (keys))
6ba6e250
RS
1325 {
1326 Lisp_Object vector;
6ba6e250
RS
1327 vector = Fmake_vector (Flength (keys), Qnil);
1328 for (i = 0; i < XSTRING (keys)->size; i++)
1329 {
1330 if (XSTRING (keys)->data[i] & 0x80)
6e344130
KH
1331 XSETFASTINT (XVECTOR (vector)->contents[i],
1332 meta_modifier | (XSTRING (keys)->data[i] & ~0x80));
6ba6e250 1333 else
6e344130
KH
1334 XSETFASTINT (XVECTOR (vector)->contents[i],
1335 XSTRING (keys)->data[i]);
6ba6e250
RS
1336 }
1337 keys = vector;
1338 }
e283121b 1339 else if (!VECTORP (keys))
47684cd9 1340 keys = wrong_type_argument (Qarrayp, keys);
4c7d5f13
RS
1341
1342 /* In effect, this computes
1343 (mapconcat 'single-key-description keys " ")
1344 but we shouldn't use mapconcat because it can do GC. */
1345
1346 len = XVECTOR (keys)->size;
1347 sep = build_string (" ");
1348 /* This has one extra element at the end that we don't pass to Fconcat. */
1349 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
1350
1351 for (i = 0; i < len; i++)
1352 {
1353 args[i * 2] = Fsingle_key_description (XVECTOR (keys)->contents[i]);
1354 args[i * 2 + 1] = sep;
1355 }
1356
1357 return Fconcat (len * 2 - 1, args);
2c6f1a39
JB
1358}
1359
1360char *
1361push_key_description (c, p)
1362 register unsigned int c;
1363 register char *p;
1364{
71ac885b
RS
1365 /* Clear all the meaningless bits above the meta bit. */
1366 c &= meta_modifier | ~ - meta_modifier;
1367
6ba6e250
RS
1368 if (c & alt_modifier)
1369 {
1370 *p++ = 'A';
1371 *p++ = '-';
1372 c -= alt_modifier;
1373 }
1374 if (c & ctrl_modifier)
1375 {
1376 *p++ = 'C';
1377 *p++ = '-';
1378 c -= ctrl_modifier;
1379 }
1380 if (c & hyper_modifier)
1381 {
1382 *p++ = 'H';
1383 *p++ = '-';
1384 c -= hyper_modifier;
1385 }
1386 if (c & meta_modifier)
2c6f1a39
JB
1387 {
1388 *p++ = 'M';
1389 *p++ = '-';
6ba6e250
RS
1390 c -= meta_modifier;
1391 }
1392 if (c & shift_modifier)
1393 {
1394 *p++ = 'S';
1395 *p++ = '-';
1396 c -= shift_modifier;
1397 }
1398 if (c & super_modifier)
1399 {
1400 *p++ = 's';
1401 *p++ = '-';
1402 c -= super_modifier;
2c6f1a39
JB
1403 }
1404 if (c < 040)
1405 {
1406 if (c == 033)
1407 {
1408 *p++ = 'E';
1409 *p++ = 'S';
1410 *p++ = 'C';
1411 }
6ba6e250 1412 else if (c == '\t')
2c6f1a39
JB
1413 {
1414 *p++ = 'T';
1415 *p++ = 'A';
1416 *p++ = 'B';
1417 }
1418 else if (c == Ctl('J'))
1419 {
1420 *p++ = 'L';
1421 *p++ = 'F';
1422 *p++ = 'D';
1423 }
1424 else if (c == Ctl('M'))
1425 {
1426 *p++ = 'R';
1427 *p++ = 'E';
1428 *p++ = 'T';
1429 }
1430 else
1431 {
1432 *p++ = 'C';
1433 *p++ = '-';
1434 if (c > 0 && c <= Ctl ('Z'))
1435 *p++ = c + 0140;
1436 else
1437 *p++ = c + 0100;
1438 }
1439 }
1440 else if (c == 0177)
1441 {
1442 *p++ = 'D';
1443 *p++ = 'E';
1444 *p++ = 'L';
1445 }
1446 else if (c == ' ')
1447 {
1448 *p++ = 'S';
1449 *p++ = 'P';
1450 *p++ = 'C';
1451 }
6ba6e250 1452 else if (c < 256)
2c6f1a39 1453 *p++ = c;
6ba6e250
RS
1454 else
1455 {
1456 *p++ = '\\';
1457 *p++ = (7 & (c >> 15)) + '0';
1458 *p++ = (7 & (c >> 12)) + '0';
1459 *p++ = (7 & (c >> 9)) + '0';
1460 *p++ = (7 & (c >> 6)) + '0';
1461 *p++ = (7 & (c >> 3)) + '0';
1462 *p++ = (7 & (c >> 0)) + '0';
1463 }
2c6f1a39
JB
1464
1465 return p;
1466}
1467
21a0d7a0
RS
1468/* This function cannot GC. */
1469
2c6f1a39
JB
1470DEFUN ("single-key-description", Fsingle_key_description, Ssingle_key_description, 1, 1, 0,
1471 "Return a pretty description of command character KEY.\n\
1472Control characters turn into C-whatever, etc.")
1473 (key)
1474 Lisp_Object key;
1475{
6ba6e250 1476 char tem[20];
2c6f1a39 1477
cebd887d 1478 key = EVENT_HEAD (key);
6bbbd9b0 1479
e958fd9a 1480 if (INTEGERP (key)) /* Normal character */
2c6f1a39 1481 {
6ba6e250 1482 *push_key_description (XUINT (key), tem) = 0;
2c6f1a39 1483 return build_string (tem);
2c6f1a39 1484 }
e958fd9a
KH
1485 else if (SYMBOLP (key)) /* Function key or event-symbol */
1486 return Fsymbol_name (key);
1487 else if (STRINGP (key)) /* Buffer names in the menubar. */
1488 return Fcopy_sequence (key);
1489 else
1490 error ("KEY must be an integer, cons, symbol, or string");
2c6f1a39
JB
1491}
1492
1493char *
1494push_text_char_description (c, p)
1495 register unsigned int c;
1496 register char *p;
1497{
1498 if (c >= 0200)
1499 {
1500 *p++ = 'M';
1501 *p++ = '-';
1502 c -= 0200;
1503 }
1504 if (c < 040)
1505 {
1506 *p++ = '^';
1507 *p++ = c + 64; /* 'A' - 1 */
1508 }
1509 else if (c == 0177)
1510 {
1511 *p++ = '^';
1512 *p++ = '?';
1513 }
1514 else
1515 *p++ = c;
1516 return p;
1517}
1518
21a0d7a0
RS
1519/* This function cannot GC. */
1520
2c6f1a39
JB
1521DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0,
1522 "Return a pretty description of file-character CHAR.\n\
1523Control characters turn into \"^char\", etc.")
1524 (chr)
1525 Lisp_Object chr;
1526{
1527 char tem[6];
1528
1529 CHECK_NUMBER (chr, 0);
1530
1531 *push_text_char_description (XINT (chr) & 0377, tem) = 0;
1532
1533 return build_string (tem);
1534}
2fc66973
JB
1535
1536/* Return non-zero if SEQ contains only ASCII characters, perhaps with
1537 a meta bit. */
1538static int
1539ascii_sequence_p (seq)
1540 Lisp_Object seq;
1541{
6e344130 1542 int i;
2fc66973 1543 int len = XINT (Flength (seq));
ffab2bd6 1544
6e344130 1545 for (i = 0; i < len; i++)
2fc66973 1546 {
6e344130 1547 Lisp_Object ii, elt;
ffab2bd6 1548
6e344130
KH
1549 XSETFASTINT (ii, i);
1550 elt = Faref (seq, ii);
2fc66973 1551
416349ec 1552 if (!INTEGERP (elt)
2fc66973
JB
1553 || (XUINT (elt) & ~CHAR_META) >= 0x80)
1554 return 0;
1555 }
1556
1557 return 1;
1558}
1559
2c6f1a39 1560\f
cc0a8174
JB
1561/* where-is - finding a command in a set of keymaps. */
1562
21a0d7a0
RS
1563/* This function can GC if Flookup_key autoloads any keymaps. */
1564
f0148b5e
RS
1565DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 4, 0,
1566 "Return list of keys that invoke DEFINITION.\n\
1567If KEYMAP is non-nil, search only KEYMAP and the global keymap.\n\
1568If KEYMAP is nil, search all the currently active keymaps.\n\
2c6f1a39 1569\n\
f0148b5e 1570If optional 3rd arg FIRSTONLY is non-nil, return the first key sequence found,\n\
b8d584f6 1571rather than a list of all possible key sequences.\n\
0bc395d4
RS
1572If FIRSTONLY is the symbol `non-ascii', return the first binding found,\n\
1573no matter what it is.\n\
d7ec5fa2 1574If FIRSTONLY has another non-nil value, prefer sequences of ASCII characters,\n\
0bc395d4 1575and entirely reject menu bindings.\n\
2c6f1a39 1576\n\
f0148b5e 1577If optional 4th arg NOINDIRECT is non-nil, don't follow indirections\n\
2c6f1a39
JB
1578to other keymaps or slots. This makes it possible to search for an\n\
1579indirect definition itself.")
f0148b5e
RS
1580 (definition, keymap, firstonly, noindirect)
1581 Lisp_Object definition, keymap;
2c6f1a39
JB
1582 Lisp_Object firstonly, noindirect;
1583{
21a0d7a0
RS
1584 Lisp_Object maps;
1585 Lisp_Object found, sequence;
f0148b5e 1586 int keymap_specified = !NILP (keymap);
21a0d7a0 1587 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
0bc395d4
RS
1588 /* 1 means ignore all menu bindings entirely. */
1589 int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii);
2c6f1a39 1590
f0148b5e
RS
1591 if (! keymap_specified)
1592 {
1593#ifdef USE_TEXT_PROPERTIES
1594 keymap = get_local_map (PT, current_buffer);
1595#else
1596 keymap = current_buffer->keymap;
1597#endif
1598 }
2c6f1a39 1599
f0148b5e
RS
1600 if (!NILP (keymap))
1601 maps = nconc2 (Faccessible_keymaps (get_keymap (keymap), Qnil),
1602 Faccessible_keymaps (get_keymap (current_global_map),
1603 Qnil));
2c6f1a39 1604 else
f0148b5e
RS
1605 maps = Faccessible_keymaps (get_keymap (current_global_map), Qnil);
1606
1607 /* Put the minor mode keymaps on the front. */
1608 if (! keymap_specified)
1609 {
1610 Lisp_Object minors;
1611 minors = Fnreverse (Fcurrent_minor_mode_maps ());
1612 while (!NILP (minors))
1613 {
1614 maps = nconc2 (Faccessible_keymaps (get_keymap (XCONS (minors)->car),
1615 Qnil),
1616 maps);
1617 minors = XCONS (minors)->cdr;
1618 }
1619 }
2c6f1a39 1620
21a0d7a0 1621 GCPRO5 (definition, keymap, maps, found, sequence);
2c6f1a39 1622 found = Qnil;
21a0d7a0 1623 sequence = Qnil;
2c6f1a39 1624
265a9e55 1625 for (; !NILP (maps); maps = Fcdr (maps))
2c6f1a39 1626 {
e9b6dfb0
KH
1627 /* Key sequence to reach map, and the map that it reaches */
1628 register Lisp_Object this, map;
f5b79c1c
JB
1629
1630 /* If Fcar (map) is a VECTOR, the current element within that vector. */
1631 int i = 0;
2c6f1a39
JB
1632
1633 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
1634 [M-CHAR] sequences, check if last character of the sequence
1635 is the meta-prefix char. */
e9b6dfb0
KH
1636 Lisp_Object last;
1637 int last_is_meta;
1638
1639 this = Fcar (Fcar (maps));
1640 map = Fcdr (Fcar (maps));
1641 last = make_number (XINT (Flength (this)) - 1);
1642 last_is_meta = (XINT (last) >= 0
1643 && EQ (Faref (this, last), meta_prefix_char));
2c6f1a39 1644
fde3a52f
JB
1645 QUIT;
1646
f5b79c1c 1647 while (CONSP (map))
2c6f1a39 1648 {
f5b79c1c
JB
1649 /* Because the code we want to run on each binding is rather
1650 large, we don't want to have two separate loop bodies for
1651 sparse keymap bindings and tables; we want to iterate one
1652 loop body over both keymap and vector bindings.
1653
1654 For this reason, if Fcar (map) is a vector, we don't
1655 advance map to the next element until i indicates that we
1656 have finished off the vector. */
2c6f1a39 1657
21a0d7a0 1658 Lisp_Object elt, key, binding;
e9b6dfb0 1659 elt = XCONS (map)->car;
f5b79c1c 1660
fde3a52f
JB
1661 QUIT;
1662
f5b79c1c
JB
1663 /* Set key and binding to the current key and binding, and
1664 advance map and i to the next binding. */
416349ec 1665 if (VECTORP (elt))
2c6f1a39
JB
1666 {
1667 /* In a vector, look at each element. */
f5b79c1c 1668 binding = XVECTOR (elt)->contents[i];
6e344130 1669 XSETFASTINT (key, i);
2c6f1a39
JB
1670 i++;
1671
f5b79c1c
JB
1672 /* If we've just finished scanning a vector, advance map
1673 to the next element, and reset i in anticipation of the
1674 next vector we may find. */
db6f9d95 1675 if (i >= XVECTOR (elt)->size)
2c6f1a39 1676 {
f5b79c1c
JB
1677 map = XCONS (map)->cdr;
1678 i = 0;
2c6f1a39 1679 }
f5b79c1c
JB
1680 }
1681 else if (CONSP (elt))
1682 {
2c6f1a39 1683 key = Fcar (Fcar (map));
f5b79c1c
JB
1684 binding = Fcdr (Fcar (map));
1685
1686 map = XCONS (map)->cdr;
2c6f1a39
JB
1687 }
1688 else
f5b79c1c
JB
1689 /* We want to ignore keymap elements that are neither
1690 vectors nor conses. */
fde3a52f
JB
1691 {
1692 map = XCONS (map)->cdr;
1693 continue;
1694 }
2c6f1a39
JB
1695
1696 /* Search through indirections unless that's not wanted. */
265a9e55 1697 if (NILP (noindirect))
0bc395d4
RS
1698 {
1699 if (nomenus)
1700 {
1701 while (1)
1702 {
1703 Lisp_Object map, tem;
1704 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
1705 map = get_keymap_1 (Fcar_safe (definition), 0, 0);
1706 tem = Fkeymapp (map);
1707 if (!NILP (tem))
1708 definition = access_keymap (map, Fcdr (definition), 0, 0);
1709 else
1710 break;
1711 }
1712 /* If the contents are (STRING ...), reject. */
1713 if (CONSP (definition)
1714 && STRINGP (XCONS (definition)->car))
1715 continue;
1716 }
1717 else
1718 binding = get_keyelt (binding, 0);
1719 }
2c6f1a39
JB
1720
1721 /* End this iteration if this element does not match
1722 the target. */
1723
416349ec 1724 if (CONSP (definition))
2c6f1a39
JB
1725 {
1726 Lisp_Object tem;
1727 tem = Fequal (binding, definition);
265a9e55 1728 if (NILP (tem))
2c6f1a39
JB
1729 continue;
1730 }
1731 else
1732 if (!EQ (binding, definition))
1733 continue;
1734
1735 /* We have found a match.
1736 Construct the key sequence where we found it. */
416349ec 1737 if (INTEGERP (key) && last_is_meta)
2c6f1a39
JB
1738 {
1739 sequence = Fcopy_sequence (this);
0b8fc2d4 1740 Faset (sequence, last, make_number (XINT (key) | meta_modifier));
2c6f1a39
JB
1741 }
1742 else
1743 sequence = append_key (this, key);
1744
1745 /* Verify that this key binding is not shadowed by another
1746 binding for the same key, before we say it exists.
1747
1748 Mechanism: look for local definition of this key and if
1749 it is defined and does not match what we found then
1750 ignore this key.
1751
1752 Either nil or number as value from Flookup_key
1753 means undefined. */
f0148b5e 1754 if (keymap_specified)
2c6f1a39 1755 {
f0148b5e 1756 binding = Flookup_key (keymap, sequence, Qnil);
416349ec 1757 if (!NILP (binding) && !INTEGERP (binding))
2c6f1a39 1758 {
416349ec 1759 if (CONSP (definition))
2c6f1a39
JB
1760 {
1761 Lisp_Object tem;
1762 tem = Fequal (binding, definition);
265a9e55 1763 if (NILP (tem))
2c6f1a39
JB
1764 continue;
1765 }
1766 else
1767 if (!EQ (binding, definition))
1768 continue;
1769 }
1770 }
f0148b5e
RS
1771 else
1772 {
1773 binding = Fkey_binding (sequence, Qnil);
1774 if (!EQ (binding, definition))
1775 continue;
1776 }
2c6f1a39 1777
9fc722de
KH
1778 /* It is a true unshadowed match. Record it, unless it's already
1779 been seen (as could happen when inheriting keymaps). */
1780 if (NILP (Fmember (sequence, found)))
1781 found = Fcons (sequence, found);
2c6f1a39 1782
2fc66973
JB
1783 /* If firstonly is Qnon_ascii, then we can return the first
1784 binding we find. If firstonly is not Qnon_ascii but not
1785 nil, then we should return the first ascii-only binding
1786 we find. */
1787 if (EQ (firstonly, Qnon_ascii))
21a0d7a0 1788 RETURN_UNGCPRO (sequence);
2fc66973 1789 else if (! NILP (firstonly) && ascii_sequence_p (sequence))
21a0d7a0 1790 RETURN_UNGCPRO (sequence);
2c6f1a39
JB
1791 }
1792 }
2fc66973 1793
21a0d7a0
RS
1794 UNGCPRO;
1795
2fc66973
JB
1796 found = Fnreverse (found);
1797
1798 /* firstonly may have been t, but we may have gone all the way through
1799 the keymaps without finding an all-ASCII key sequence. So just
1800 return the best we could find. */
1801 if (! NILP (firstonly))
1802 return Fcar (found);
1803
1804 return found;
2c6f1a39 1805}
2c6f1a39 1806\f
cc0a8174
JB
1807/* describe-bindings - summarizing all the bindings in a set of keymaps. */
1808
53c8f9fa 1809DEFUN ("describe-bindings", Fdescribe_bindings, Sdescribe_bindings, 0, 1, "",
2c6f1a39 1810 "Show a list of all defined keys, and their definitions.\n\
53c8f9fa
RS
1811The list is put in a buffer, which is displayed.\n\
1812An optional argument PREFIX, if non-nil, should be a key sequence;\n\
1813then we display only bindings that start with that prefix.")
1814 (prefix)
1815 Lisp_Object prefix;
2c6f1a39
JB
1816{
1817 register Lisp_Object thisbuf;
bff4ec1f 1818 XSETBUFFER (thisbuf, current_buffer);
2c6f1a39
JB
1819 internal_with_output_to_temp_buffer ("*Help*",
1820 describe_buffer_bindings,
53c8f9fa 1821 Fcons (thisbuf, prefix));
2c6f1a39
JB
1822 return Qnil;
1823}
1824
53c8f9fa
RS
1825/* ARG is (BUFFER . PREFIX). */
1826
2c6f1a39 1827static Lisp_Object
53c8f9fa
RS
1828describe_buffer_bindings (arg)
1829 Lisp_Object arg;
2c6f1a39 1830{
53c8f9fa 1831 Lisp_Object descbuf, prefix, shadow;
d7ab90a9
KH
1832 register Lisp_Object start1;
1833 struct gcpro gcpro1;
2c6f1a39 1834
4726a9f1
JB
1835 char *alternate_heading
1836 = "\
1837Alternate Characters (use anywhere the nominal character is listed):\n\
1838nominal alternate\n\
1839------- ---------\n";
2c6f1a39 1840
53c8f9fa
RS
1841 descbuf = XCONS (arg)->car;
1842 prefix = XCONS (arg)->cdr;
a588e041 1843 shadow = Qnil;
d7ab90a9 1844 GCPRO1 (shadow);
53c8f9fa 1845
2c6f1a39
JB
1846 Fset_buffer (Vstandard_output);
1847
4726a9f1 1848 /* Report on alternates for keys. */
d7bf9bf5 1849 if (STRINGP (Vkeyboard_translate_table) && !NILP (prefix))
4726a9f1
JB
1850 {
1851 int c;
1852 unsigned char *translate = XSTRING (Vkeyboard_translate_table)->data;
1853 int translate_len = XSTRING (Vkeyboard_translate_table)->size;
1854
1855 for (c = 0; c < translate_len; c++)
1856 if (translate[c] != c)
1857 {
1858 char buf[20];
1859 char *bufend;
1860
1861 if (alternate_heading)
1862 {
1863 insert_string (alternate_heading);
1864 alternate_heading = 0;
1865 }
1866
1867 bufend = push_key_description (translate[c], buf);
1868 insert (buf, bufend - buf);
1869 Findent_to (make_number (16), make_number (1));
1870 bufend = push_key_description (c, buf);
1871 insert (buf, bufend - buf);
1872
1873 insert ("\n", 1);
1874 }
1875
1876 insert ("\n", 1);
1877 }
1878
d7bf9bf5
RS
1879 if (!NILP (Vkey_translation_map))
1880 describe_map_tree (Vkey_translation_map, 0, Qnil, prefix,
c2b714de 1881 "Key translations", 0, 1, 0);
d7bf9bf5 1882
cc0a8174
JB
1883 {
1884 int i, nmaps;
1885 Lisp_Object *modes, *maps;
1886
4726a9f1
JB
1887 /* Temporarily switch to descbuf, so that we can get that buffer's
1888 minor modes correctly. */
1889 Fset_buffer (descbuf);
d7bf9bf5 1890
e784236d
KH
1891 if (!NILP (current_kboard->Voverriding_terminal_local_map)
1892 || !NILP (Voverriding_local_map))
7d92e329
RS
1893 nmaps = 0;
1894 else
1895 nmaps = current_minor_maps (&modes, &maps);
4726a9f1
JB
1896 Fset_buffer (Vstandard_output);
1897
53c8f9fa 1898 /* Print the minor mode maps. */
cc0a8174
JB
1899 for (i = 0; i < nmaps; i++)
1900 {
c9b7c53a 1901 /* The title for a minor mode keymap
07f15dfd
RS
1902 is constructed at run time.
1903 We let describe_map_tree do the actual insertion
1904 because it takes care of other features when doing so. */
c9b7c53a 1905 char *title, *p;
07f15dfd 1906
416349ec 1907 if (!SYMBOLP (modes[i]))
d7ab90a9
KH
1908 abort();
1909
1910 p = title = (char *) alloca (40 + XSYMBOL (modes[i])->name->size);
1911 *p++ = '`';
1912 bcopy (XSYMBOL (modes[i])->name->data, p,
1913 XSYMBOL (modes[i])->name->size);
1914 p += XSYMBOL (modes[i])->name->size;
1915 *p++ = '\'';
c9b7c53a
KH
1916 bcopy (" Minor Mode Bindings", p, sizeof (" Minor Mode Bindings") - 1);
1917 p += sizeof (" Minor Mode Bindings") - 1;
07f15dfd
RS
1918 *p = 0;
1919
c2b714de 1920 describe_map_tree (maps[i], 0, shadow, prefix, title, 0, 0, 0);
53c8f9fa 1921 shadow = Fcons (maps[i], shadow);
cc0a8174
JB
1922 }
1923 }
1924
53c8f9fa 1925 /* Print the (major mode) local map. */
e784236d
KH
1926 if (!NILP (current_kboard->Voverriding_terminal_local_map))
1927 start1 = current_kboard->Voverriding_terminal_local_map;
1928 else if (!NILP (Voverriding_local_map))
7d92e329
RS
1929 start1 = Voverriding_local_map;
1930 else
1931 start1 = XBUFFER (descbuf)->keymap;
1932
265a9e55 1933 if (!NILP (start1))
2c6f1a39 1934 {
53c8f9fa 1935 describe_map_tree (start1, 0, shadow, prefix,
c2b714de 1936 "Major Mode Bindings", 0, 0, 0);
53c8f9fa 1937 shadow = Fcons (start1, shadow);
2c6f1a39
JB
1938 }
1939
53c8f9fa 1940 describe_map_tree (current_global_map, 0, shadow, prefix,
c2b714de 1941 "Global Bindings", 0, 0, 1);
d7bf9bf5
RS
1942
1943 /* Print the function-key-map translations under this prefix. */
1944 if (!NILP (Vfunction_key_map))
1945 describe_map_tree (Vfunction_key_map, 0, Qnil, prefix,
c2b714de 1946 "Function key map translations", 0, 1, 0);
2c6f1a39 1947
04befa07 1948 call0 (intern ("help-mode"));
2c6f1a39 1949 Fset_buffer (descbuf);
d7ab90a9 1950 UNGCPRO;
2c6f1a39
JB
1951 return Qnil;
1952}
1953
b31a4218 1954/* Insert a description of the key bindings in STARTMAP,
2c6f1a39
JB
1955 followed by those of all maps reachable through STARTMAP.
1956 If PARTIAL is nonzero, omit certain "uninteresting" commands
1957 (such as `undefined').
53c8f9fa
RS
1958 If SHADOW is non-nil, it is a list of maps;
1959 don't mention keys which would be shadowed by any of them.
1960 PREFIX, if non-nil, says mention only keys that start with PREFIX.
07f15dfd 1961 TITLE, if not 0, is a string to insert at the beginning.
af1d6f09 1962 TITLE should not end with a colon or a newline; we supply that.
d7bf9bf5
RS
1963 If NOMENU is not 0, then omit menu-bar commands.
1964
1965 If TRANSL is nonzero, the definitions are actually key translations
c2b714de
RS
1966 so print strings and vectors differently.
1967
1968 If ALWAYS_TITLE is nonzero, print the title even if there are no maps
1969 to look through. */
2c6f1a39
JB
1970
1971void
c2b714de
RS
1972describe_map_tree (startmap, partial, shadow, prefix, title, nomenu, transl,
1973 always_title)
53c8f9fa 1974 Lisp_Object startmap, shadow, prefix;
2c6f1a39 1975 int partial;
53c8f9fa 1976 char *title;
af1d6f09 1977 int nomenu;
d7bf9bf5 1978 int transl;
c2b714de 1979 int always_title;
2c6f1a39 1980{
e3dfcd4e
KH
1981 Lisp_Object maps, seen, sub_shadows;
1982 struct gcpro gcpro1, gcpro2, gcpro3;
07f15dfd 1983 int something = 0;
53c8f9fa
RS
1984 char *key_heading
1985 = "\
1986key binding\n\
1987--- -------\n";
2c6f1a39 1988
53c8f9fa 1989 maps = Faccessible_keymaps (startmap, prefix);
925083d1 1990 seen = Qnil;
e3dfcd4e
KH
1991 sub_shadows = Qnil;
1992 GCPRO3 (maps, seen, sub_shadows);
2c6f1a39 1993
af1d6f09
RS
1994 if (nomenu)
1995 {
1996 Lisp_Object list;
1997
1998 /* Delete from MAPS each element that is for the menu bar. */
1999 for (list = maps; !NILP (list); list = XCONS (list)->cdr)
2000 {
2001 Lisp_Object elt, prefix, tem;
2002
2003 elt = Fcar (list);
2004 prefix = Fcar (elt);
2005 if (XVECTOR (prefix)->size >= 1)
2006 {
2007 tem = Faref (prefix, make_number (0));
2008 if (EQ (tem, Qmenu_bar))
2009 maps = Fdelq (elt, maps);
2010 }
2011 }
2012 }
2013
c2b714de 2014 if (!NILP (maps) || always_title)
53c8f9fa
RS
2015 {
2016 if (title)
07f15dfd
RS
2017 {
2018 insert_string (title);
2019 if (!NILP (prefix))
2020 {
2021 insert_string (" Starting With ");
2022 insert1 (Fkey_description (prefix));
2023 }
2024 insert_string (":\n");
2025 }
53c8f9fa 2026 insert_string (key_heading);
07f15dfd 2027 something = 1;
53c8f9fa
RS
2028 }
2029
265a9e55 2030 for (; !NILP (maps); maps = Fcdr (maps))
2c6f1a39 2031 {
e3dfcd4e 2032 register Lisp_Object elt, prefix, tail;
53c8f9fa 2033
2c6f1a39 2034 elt = Fcar (maps);
53c8f9fa
RS
2035 prefix = Fcar (elt);
2036
2037 sub_shadows = Qnil;
2038
2039 for (tail = shadow; CONSP (tail); tail = XCONS (tail)->cdr)
2c6f1a39 2040 {
53c8f9fa
RS
2041 Lisp_Object shmap;
2042
2043 shmap = XCONS (tail)->car;
2044
2045 /* If the sequence by which we reach this keymap is zero-length,
2046 then the shadow map for this keymap is just SHADOW. */
416349ec
KH
2047 if ((STRINGP (prefix) && XSTRING (prefix)->size == 0)
2048 || (VECTORP (prefix) && XVECTOR (prefix)->size == 0))
53c8f9fa
RS
2049 ;
2050 /* If the sequence by which we reach this keymap actually has
2051 some elements, then the sequence's definition in SHADOW is
2052 what we should use. */
2053 else
2054 {
98234407 2055 shmap = Flookup_key (shmap, Fcar (elt), Qt);
416349ec 2056 if (INTEGERP (shmap))
53c8f9fa
RS
2057 shmap = Qnil;
2058 }
2059
2060 /* If shmap is not nil and not a keymap,
2061 it completely shadows this map, so don't
2062 describe this map at all. */
2063 if (!NILP (shmap) && NILP (Fkeymapp (shmap)))
2064 goto skip;
2065
2066 if (!NILP (shmap))
2067 sub_shadows = Fcons (shmap, sub_shadows);
2c6f1a39
JB
2068 }
2069
d7bf9bf5
RS
2070 describe_map (Fcdr (elt), Fcar (elt),
2071 transl ? describe_translation : describe_command,
925083d1 2072 partial, sub_shadows, &seen);
53c8f9fa
RS
2073
2074 skip: ;
2c6f1a39
JB
2075 }
2076
07f15dfd
RS
2077 if (something)
2078 insert_string ("\n");
2079
2c6f1a39
JB
2080 UNGCPRO;
2081}
2082
2083static void
2084describe_command (definition)
2085 Lisp_Object definition;
2086{
2087 register Lisp_Object tem1;
2088
2089 Findent_to (make_number (16), make_number (1));
2090
416349ec 2091 if (SYMBOLP (definition))
2c6f1a39 2092 {
bff4ec1f 2093 XSETSTRING (tem1, XSYMBOL (definition)->name);
2c6f1a39
JB
2094 insert1 (tem1);
2095 insert_string ("\n");
2096 }
d7bf9bf5 2097 else if (STRINGP (definition) || VECTORP (definition))
24065b9c 2098 insert_string ("Keyboard Macro\n");
2c6f1a39
JB
2099 else
2100 {
2101 tem1 = Fkeymapp (definition);
265a9e55 2102 if (!NILP (tem1))
2c6f1a39
JB
2103 insert_string ("Prefix Command\n");
2104 else
2105 insert_string ("??\n");
2106 }
2107}
2108
d7bf9bf5
RS
2109static void
2110describe_translation (definition)
2111 Lisp_Object definition;
2112{
2113 register Lisp_Object tem1;
2114
2115 Findent_to (make_number (16), make_number (1));
2116
2117 if (SYMBOLP (definition))
2118 {
2119 XSETSTRING (tem1, XSYMBOL (definition)->name);
2120 insert1 (tem1);
2121 insert_string ("\n");
2122 }
2123 else if (STRINGP (definition) || VECTORP (definition))
b902ac28
RS
2124 {
2125 insert1 (Fkey_description (definition));
2126 insert_string ("\n");
2127 }
d7bf9bf5
RS
2128 else
2129 {
2130 tem1 = Fkeymapp (definition);
2131 if (!NILP (tem1))
2132 insert_string ("Prefix Command\n");
2133 else
2134 insert_string ("??\n");
2135 }
2136}
2137
53c8f9fa
RS
2138/* Like Flookup_key, but uses a list of keymaps SHADOW instead of a single map.
2139 Returns the first non-nil binding found in any of those maps. */
2140
2141static Lisp_Object
2142shadow_lookup (shadow, key, flag)
2143 Lisp_Object shadow, key, flag;
2144{
2145 Lisp_Object tail, value;
2146
2147 for (tail = shadow; CONSP (tail); tail = XCONS (tail)->cdr)
2148 {
2149 value = Flookup_key (XCONS (tail)->car, key, flag);
2150 if (!NILP (value))
2151 return value;
2152 }
2153 return Qnil;
2154}
2155
c3c0ee93
KH
2156/* Describe the contents of map MAP, assuming that this map itself is
2157 reached by the sequence of prefix keys KEYS (a string or vector).
925083d1 2158 PARTIAL, SHADOW are as in `describe_map_tree' above. */
2c6f1a39
JB
2159
2160static void
925083d1 2161describe_map (map, keys, elt_describer, partial, shadow, seen)
c3c0ee93
KH
2162 register Lisp_Object map;
2163 Lisp_Object keys;
2c6f1a39
JB
2164 int (*elt_describer) ();
2165 int partial;
2166 Lisp_Object shadow;
925083d1 2167 Lisp_Object *seen;
2c6f1a39 2168{
c3c0ee93 2169 Lisp_Object elt_prefix;
53c8f9fa 2170 Lisp_Object tail, definition, event;
99a225a9 2171 Lisp_Object tem;
2c6f1a39
JB
2172 Lisp_Object suppress;
2173 Lisp_Object kludge;
2174 int first = 1;
2175 struct gcpro gcpro1, gcpro2, gcpro3;
2176
c3c0ee93
KH
2177 if (!NILP (keys) && XFASTINT (Flength (keys)) > 0)
2178 {
c3c0ee93
KH
2179 /* Call Fkey_description first, to avoid GC bug for the other string. */
2180 tem = Fkey_description (keys);
2181 elt_prefix = concat2 (tem, build_string (" "));
2182 }
2183 else
2184 elt_prefix = Qnil;
2185
2c6f1a39
JB
2186 if (partial)
2187 suppress = intern ("suppress-keymap");
2188
2189 /* This vector gets used to present single keys to Flookup_key. Since
f5b79c1c 2190 that is done once per keymap element, we don't want to cons up a
2c6f1a39
JB
2191 fresh vector every time. */
2192 kludge = Fmake_vector (make_number (1), Qnil);
99a225a9 2193 definition = Qnil;
2c6f1a39 2194
99a225a9 2195 GCPRO3 (elt_prefix, definition, kludge);
2c6f1a39 2196
925083d1 2197 for (tail = map; CONSP (tail); tail = XCONS (tail)->cdr)
2c6f1a39
JB
2198 {
2199 QUIT;
2c6f1a39 2200
416349ec 2201 if (VECTORP (XCONS (tail)->car))
53c8f9fa 2202 describe_vector (XCONS (tail)->car,
32bfcae1 2203 elt_prefix, elt_describer, partial, shadow, map);
925083d1 2204 else if (CONSP (XCONS (tail)->car))
2c6f1a39 2205 {
925083d1 2206 event = XCONS (XCONS (tail)->car)->car;
2c3b35b0
RS
2207
2208 /* Ignore bindings whose "keys" are not really valid events.
2209 (We get these in the frames and buffers menu.) */
2210 if (! (SYMBOLP (event) || INTEGERP (event)))
c96dcc01 2211 continue;
2c3b35b0 2212
925083d1 2213 definition = get_keyelt (XCONS (XCONS (tail)->car)->cdr, 0);
2c6f1a39 2214
f5b79c1c 2215 /* Don't show undefined commands or suppressed commands. */
99a225a9 2216 if (NILP (definition)) continue;
416349ec 2217 if (SYMBOLP (definition) && partial)
f5b79c1c 2218 {
99a225a9
RS
2219 tem = Fget (definition, suppress);
2220 if (!NILP (tem))
f5b79c1c
JB
2221 continue;
2222 }
2c6f1a39 2223
f5b79c1c
JB
2224 /* Don't show a command that isn't really visible
2225 because a local definition of the same key shadows it. */
2c6f1a39 2226
99a225a9 2227 XVECTOR (kludge)->contents[0] = event;
f5b79c1c
JB
2228 if (!NILP (shadow))
2229 {
53c8f9fa 2230 tem = shadow_lookup (shadow, kludge, Qt);
f5b79c1c
JB
2231 if (!NILP (tem)) continue;
2232 }
2233
c3c0ee93 2234 tem = Flookup_key (map, kludge, Qt);
99a225a9
RS
2235 if (! EQ (tem, definition)) continue;
2236
f5b79c1c
JB
2237 if (first)
2238 {
2239 insert ("\n", 1);
2240 first = 0;
2241 }
2c6f1a39 2242
f5b79c1c
JB
2243 if (!NILP (elt_prefix))
2244 insert1 (elt_prefix);
2c6f1a39 2245
99a225a9
RS
2246 /* THIS gets the string to describe the character EVENT. */
2247 insert1 (Fsingle_key_description (event));
2c6f1a39 2248
f5b79c1c
JB
2249 /* Print a description of the definition of this character.
2250 elt_describer will take care of spacing out far enough
2251 for alignment purposes. */
99a225a9 2252 (*elt_describer) (definition);
f5b79c1c 2253 }
925083d1
KH
2254 else if (EQ (XCONS (tail)->car, Qkeymap))
2255 {
2256 /* The same keymap might be in the structure twice, if we're
2257 using an inherited keymap. So skip anything we've already
2258 encountered. */
2259 tem = Fassq (tail, *seen);
b5b90d18 2260 if (CONSP (tem) && !NILP (Fequal (XCONS (tem)->car, keys)))
925083d1
KH
2261 break;
2262 *seen = Fcons (Fcons (tail, keys), *seen);
2263 }
2c6f1a39
JB
2264 }
2265
2266 UNGCPRO;
2267}
2268
2269static int
2270describe_vector_princ (elt)
2271 Lisp_Object elt;
2272{
81fa9e2f 2273 Findent_to (make_number (16), make_number (1));
2c6f1a39 2274 Fprinc (elt, Qnil);
ad4ec84a 2275 Fterpri (Qnil);
2c6f1a39
JB
2276}
2277
2278DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 1, 0,
ad4ec84a 2279 "Insert a description of contents of VECTOR.\n\
2c6f1a39
JB
2280This is text showing the elements of vector matched against indices.")
2281 (vector)
2282 Lisp_Object vector;
2283{
ad4ec84a
RS
2284 int count = specpdl_ptr - specpdl;
2285
2286 specbind (Qstandard_output, Fcurrent_buffer ());
352e5dea 2287 CHECK_VECTOR_OR_CHAR_TABLE (vector, 0);
32bfcae1 2288 describe_vector (vector, Qnil, describe_vector_princ, 0, Qnil, Qnil);
ad4ec84a
RS
2289
2290 return unbind_to (count, Qnil);
2c6f1a39
JB
2291}
2292
352e5dea
RS
2293/* Insert in the current buffer a description of the contents of VECTOR.
2294 We call ELT_DESCRIBER to insert the description of one value found
2295 in VECTOR.
2296
2297 ELT_PREFIX describes what "comes before" the keys or indices defined
2298 by this vector.
2299
2300 If the vector is in a keymap, ELT_PREFIX is a prefix key which
2301 leads to this keymap.
2302
2303 If the vector is a chartable, ELT_PREFIX is the vector
2304 of bytes that lead to the character set or portion of a character
2305 set described by this chartable.
2306
2307 If PARTIAL is nonzero, it means do not mention suppressed commands
2308 (that assumes the vector is in a keymap).
2309
2310 SHADOW is a list of keymaps that shadow this map.
2311 If it is non-nil, then we look up the key in those maps
2312 and we don't mention it now if it is defined by any of them.
2313
2314 ENTIRE_MAP is the keymap in which this vector appears.
2315 If the definition in effect in the whole map does not match
2316 the one in this vector, we ignore this one. */
2317
32bfcae1
KH
2318describe_vector (vector, elt_prefix, elt_describer,
2319 partial, shadow, entire_map)
2c6f1a39
JB
2320 register Lisp_Object vector;
2321 Lisp_Object elt_prefix;
2322 int (*elt_describer) ();
2323 int partial;
2324 Lisp_Object shadow;
32bfcae1 2325 Lisp_Object entire_map;
2c6f1a39
JB
2326{
2327 Lisp_Object this;
2328 Lisp_Object dummy;
32bfcae1
KH
2329 Lisp_Object definition;
2330 Lisp_Object tem2;
2c6f1a39
JB
2331 register int i;
2332 Lisp_Object suppress;
2333 Lisp_Object kludge;
352e5dea 2334 Lisp_Object chartable_kludge;
2c6f1a39 2335 int first = 1;
b5585f5c 2336 int size;
352e5dea 2337 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2c6f1a39 2338
32bfcae1 2339 definition = Qnil;
352e5dea 2340 chartable_kludge = Qnil;
2c6f1a39
JB
2341
2342 /* This vector gets used to present single keys to Flookup_key. Since
2343 that is done once per vector element, we don't want to cons up a
2344 fresh vector every time. */
2345 kludge = Fmake_vector (make_number (1), Qnil);
352e5dea 2346 GCPRO4 (elt_prefix, definition, kludge, chartable_kludge);
2c6f1a39
JB
2347
2348 if (partial)
2349 suppress = intern ("suppress-keymap");
2350
b5585f5c
RS
2351 /* This does the right thing for char-tables as well as ordinary vectors. */
2352 size = XFASTINT (Flength (vector));
2353
2354 for (i = 0; i < size; i++)
2c6f1a39
JB
2355 {
2356 QUIT;
32bfcae1 2357 definition = get_keyelt (XVECTOR (vector)->contents[i], 0);
2c6f1a39 2358
32bfcae1 2359 if (NILP (definition)) continue;
2c6f1a39
JB
2360
2361 /* Don't mention suppressed commands. */
32bfcae1 2362 if (SYMBOLP (definition) && partial)
2c6f1a39 2363 {
32bfcae1 2364 this = Fget (definition, suppress);
265a9e55 2365 if (!NILP (this))
2c6f1a39
JB
2366 continue;
2367 }
2368
32bfcae1 2369 /* If this binding is shadowed by some other map, ignore it. */
265a9e55 2370 if (!NILP (shadow))
2c6f1a39
JB
2371 {
2372 Lisp_Object tem;
2373
2374 XVECTOR (kludge)->contents[0] = make_number (i);
53c8f9fa 2375 tem = shadow_lookup (shadow, kludge, Qt);
2c6f1a39 2376
265a9e55 2377 if (!NILP (tem)) continue;
2c6f1a39
JB
2378 }
2379
32bfcae1
KH
2380 /* Ignore this definition if it is shadowed by an earlier
2381 one in the same keymap. */
2382 if (!NILP (entire_map))
2383 {
2384 Lisp_Object tem;
2385
2386 XVECTOR (kludge)->contents[0] = make_number (i);
2387 tem = Flookup_key (entire_map, kludge, Qt);
2388
2389 if (! EQ (tem, definition))
2390 continue;
2391 }
2392
352e5dea
RS
2393 /* If we find a char-table within a char-table,
2394 scan it recursively; it defines the details for
2395 a character set or a portion of a character set. */
2396 if (CHAR_TABLE_P (vector) && CHAR_TABLE_P (definition))
2397 {
2398 int outer_level
2399 = !NILP (elt_prefix) ? XVECTOR (elt_prefix)->size : 0;
2400 if (NILP (chartable_kludge))
2401 {
2402 chartable_kludge
2403 = Fmake_vector (make_number (outer_level + 1), Qnil);
2404 if (outer_level != 0)
2405 bcopy (XVECTOR (elt_prefix)->contents,
2406 XVECTOR (chartable_kludge)->contents,
2407 outer_level * sizeof (Lisp_Object));
2408 }
2409 XVECTOR (chartable_kludge)->contents[outer_level]
2410 = make_number (i);
2411 describe_vector (definition, chartable_kludge, elt_describer,
2412 partial, shadow, entire_map);
2413 continue;
2414 }
2415
2c6f1a39
JB
2416 if (first)
2417 {
2418 insert ("\n", 1);
2419 first = 0;
2420 }
2421
352e5dea
RS
2422 if (CHAR_TABLE_P (vector))
2423 {
2424 if (!NILP (elt_prefix))
2425 {
2426 /* Must combine elt_prefix with i to produce a character
2427 code, then insert that character's description. */
2428 }
2429 else
2430 {
2431 /* Get the string to describe the character I, and print it. */
2432 XSETFASTINT (dummy, i);
2c6f1a39 2433
352e5dea
RS
2434 /* THIS gets the string to describe the character DUMMY. */
2435 this = Fsingle_key_description (dummy);
2436 insert1 (this);
2437 }
2438 }
2439 else
2440 {
2441 /* Output the prefix that applies to every entry in this map. */
2442 if (!NILP (elt_prefix))
2443 insert1 (elt_prefix);
2c6f1a39 2444
352e5dea
RS
2445 /* Get the string to describe the character I, and print it. */
2446 XSETFASTINT (dummy, i);
2447
2448 /* THIS gets the string to describe the character DUMMY. */
2449 this = Fsingle_key_description (dummy);
2450 insert1 (this);
2451 }
2c6f1a39
JB
2452
2453 /* Find all consecutive characters that have the same definition. */
db6f9d95 2454 while (i + 1 < XVECTOR (vector)->size
224a16e8 2455 && (tem2 = get_keyelt (XVECTOR (vector)->contents[i+1], 0),
32bfcae1 2456 EQ (tem2, definition)))
2c6f1a39
JB
2457 i++;
2458
2459 /* If we have a range of more than one character,
2460 print where the range reaches to. */
2461
2462 if (i != XINT (dummy))
2463 {
2464 insert (" .. ", 4);
352e5dea
RS
2465 if (CHAR_TABLE_P (vector))
2466 {
2467 if (!NILP (elt_prefix))
2468 {
2469 /* Must combine elt_prefix with i to produce a character
2470 code, then insert that character's description. */
2471 }
2472 else
2473 {
2474 XSETFASTINT (dummy, i);
2c6f1a39 2475
352e5dea
RS
2476 this = Fsingle_key_description (dummy);
2477 insert1 (this);
2478 }
2479 }
2480 else
2481 {
2482 if (!NILP (elt_prefix))
2483 insert1 (elt_prefix);
2484
2485 XSETFASTINT (dummy, i);
2486 insert1 (Fsingle_key_description (dummy));
2487 }
2c6f1a39
JB
2488 }
2489
2490 /* Print a description of the definition of this character.
2491 elt_describer will take care of spacing out far enough
2492 for alignment purposes. */
32bfcae1 2493 (*elt_describer) (definition);
2c6f1a39
JB
2494 }
2495
2496 UNGCPRO;
2497}
2498\f
cc0a8174 2499/* Apropos - finding all symbols whose names match a regexp. */
2c6f1a39
JB
2500Lisp_Object apropos_predicate;
2501Lisp_Object apropos_accumulate;
2502
2503static void
2504apropos_accum (symbol, string)
2505 Lisp_Object symbol, string;
2506{
2507 register Lisp_Object tem;
2508
2509 tem = Fstring_match (string, Fsymbol_name (symbol), Qnil);
265a9e55 2510 if (!NILP (tem) && !NILP (apropos_predicate))
2c6f1a39 2511 tem = call1 (apropos_predicate, symbol);
265a9e55 2512 if (!NILP (tem))
2c6f1a39
JB
2513 apropos_accumulate = Fcons (symbol, apropos_accumulate);
2514}
2515
2516DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0,
2517 "Show all symbols whose names contain match for REGEXP.\n\
2518If optional 2nd arg PRED is non-nil, (funcall PRED SYM) is done\n\
2519for each symbol and a symbol is mentioned only if that returns non-nil.\n\
2520Return list of symbols found.")
2521 (string, pred)
2522 Lisp_Object string, pred;
2523{
2524 struct gcpro gcpro1, gcpro2;
2525 CHECK_STRING (string, 0);
2526 apropos_predicate = pred;
2527 GCPRO2 (apropos_predicate, apropos_accumulate);
2528 apropos_accumulate = Qnil;
2529 map_obarray (Vobarray, apropos_accum, string);
2530 apropos_accumulate = Fsort (apropos_accumulate, Qstring_lessp);
2531 UNGCPRO;
2532 return apropos_accumulate;
2533}
2534\f
2535syms_of_keymap ()
2536{
2537 Lisp_Object tem;
2538
2539 Qkeymap = intern ("keymap");
2540 staticpro (&Qkeymap);
2541
2542/* Initialize the keymaps standardly used.
2543 Each one is the value of a Lisp variable, and is also
2544 pointed to by a C variable */
2545
19eaeb86 2546 global_map = Fcons (Qkeymap,
1447c534 2547 Fcons (Fmake_vector (make_number (0400), Qnil), Qnil));
2c6f1a39
JB
2548 Fset (intern ("global-map"), global_map);
2549
ce6e5d0b 2550 meta_map = Fmake_keymap (Qnil);
2c6f1a39
JB
2551 Fset (intern ("esc-map"), meta_map);
2552 Ffset (intern ("ESC-prefix"), meta_map);
2553
ce6e5d0b 2554 control_x_map = Fmake_keymap (Qnil);
2c6f1a39
JB
2555 Fset (intern ("ctl-x-map"), control_x_map);
2556 Ffset (intern ("Control-X-prefix"), control_x_map);
2557
107fd03d
RS
2558 DEFVAR_LISP ("define-key-rebound-commands", &Vdefine_key_rebound_commands,
2559 "List of commands given new key bindings recently.\n\
2560This is used for internal purposes during Emacs startup;\n\
2561don't alter it yourself.");
2562 Vdefine_key_rebound_commands = Qt;
2563
2c6f1a39
JB
2564 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map,
2565 "Default keymap to use when reading from the minibuffer.");
ce6e5d0b 2566 Vminibuffer_local_map = Fmake_sparse_keymap (Qnil);
2c6f1a39
JB
2567
2568 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map,
2569 "Local keymap for the minibuffer when spaces are not allowed.");
ce6e5d0b 2570 Vminibuffer_local_ns_map = Fmake_sparse_keymap (Qnil);
2c6f1a39
JB
2571
2572 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map,
2573 "Local keymap for minibuffer input with completion.");
ce6e5d0b 2574 Vminibuffer_local_completion_map = Fmake_sparse_keymap (Qnil);
2c6f1a39
JB
2575
2576 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map,
2577 "Local keymap for minibuffer input with completion, for exact match.");
ce6e5d0b 2578 Vminibuffer_local_must_match_map = Fmake_sparse_keymap (Qnil);
2c6f1a39
JB
2579
2580 current_global_map = global_map;
2581
cc0a8174
JB
2582 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist,
2583 "Alist of keymaps to use for minor modes.\n\
2584Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read\n\
2585key sequences and look up bindings iff VARIABLE's value is non-nil.\n\
2586If two active keymaps bind the same key, the keymap appearing earlier\n\
2587in the list takes precedence.");
2588 Vminor_mode_map_alist = Qnil;
2589
6bbbd9b0
JB
2590 DEFVAR_LISP ("function-key-map", &Vfunction_key_map,
2591 "Keymap mapping ASCII function key sequences onto their preferred forms.\n\
2592This allows Emacs to recognize function keys sent from ASCII\n\
2593terminals at any point in a key sequence.\n\
2594\n\
1981e886
RS
2595The `read-key-sequence' function replaces any subsequence bound by\n\
2596`function-key-map' with its binding. More precisely, when the active\n\
6bbbd9b0 2597keymaps have no binding for the current key sequence but\n\
1981e886
RS
2598`function-key-map' binds a suffix of the sequence to a vector or string,\n\
2599`read-key-sequence' replaces the matching suffix with its binding, and\n\
6bbbd9b0
JB
2600continues with the new sequence.\n\
2601\n\
1981e886
RS
2602The events that come from bindings in `function-key-map' are not\n\
2603themselves looked up in `function-key-map'.\n\
2604\n\
2605For example, suppose `function-key-map' binds `ESC O P' to [f1].\n\
2606Typing `ESC O P' to `read-key-sequence' would return [f1]. Typing\n\
718ca51e
JB
2607`C-x ESC O P' would return [?\\C-x f1]. If [f1] were a prefix\n\
2608key, typing `ESC O P x' would return [f1 x].");
ce6e5d0b 2609 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
6bbbd9b0 2610
d7bf9bf5
RS
2611 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
2612 "Keymap of key translations that can override keymaps.\n\
2613This keymap works like `function-key-map', but comes after that,\n\
2614and applies even for keys that have ordinary bindings.");
2615 Vkey_translation_map = Qnil;
2616
2c6f1a39
JB
2617 Qsingle_key_description = intern ("single-key-description");
2618 staticpro (&Qsingle_key_description);
2619
2620 Qkey_description = intern ("key-description");
2621 staticpro (&Qkey_description);
2622
2623 Qkeymapp = intern ("keymapp");
2624 staticpro (&Qkeymapp);
2625
2fc66973
JB
2626 Qnon_ascii = intern ("non-ascii");
2627 staticpro (&Qnon_ascii);
2628
2c6f1a39
JB
2629 defsubr (&Skeymapp);
2630 defsubr (&Smake_keymap);
2631 defsubr (&Smake_sparse_keymap);
2632 defsubr (&Scopy_keymap);
2633 defsubr (&Skey_binding);
2634 defsubr (&Slocal_key_binding);
2635 defsubr (&Sglobal_key_binding);
cc0a8174 2636 defsubr (&Sminor_mode_key_binding);
2c6f1a39
JB
2637 defsubr (&Sdefine_key);
2638 defsubr (&Slookup_key);
2c6f1a39
JB
2639 defsubr (&Sdefine_prefix_command);
2640 defsubr (&Suse_global_map);
2641 defsubr (&Suse_local_map);
2642 defsubr (&Scurrent_local_map);
2643 defsubr (&Scurrent_global_map);
cc0a8174 2644 defsubr (&Scurrent_minor_mode_maps);
2c6f1a39
JB
2645 defsubr (&Saccessible_keymaps);
2646 defsubr (&Skey_description);
2647 defsubr (&Sdescribe_vector);
2648 defsubr (&Ssingle_key_description);
2649 defsubr (&Stext_char_description);
2650 defsubr (&Swhere_is_internal);
2c6f1a39
JB
2651 defsubr (&Sdescribe_bindings);
2652 defsubr (&Sapropos_internal);
2653}
2654
2655keys_of_keymap ()
2656{
2657 Lisp_Object tem;
2658
2659 initial_define_key (global_map, 033, "ESC-prefix");
2660 initial_define_key (global_map, Ctl('X'), "Control-X-prefix");
2661}