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