Commit | Line | Data |
---|---|---|
2c6f1a39 | 1 | /* Manipulation of keymaps |
d4ae91d2 | 2 | Copyright (C) 1985, 86,87,88,93,94,95,98,99, 2000, 2001 |
11adc310 | 3 | Free Software Foundation, Inc. |
2c6f1a39 JB |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
7 | GNU Emacs is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
502ddf23 | 9 | the Free Software Foundation; either version 2, or (at your option) |
2c6f1a39 JB |
10 | any later version. |
11 | ||
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GNU Emacs; see the file COPYING. If not, write to | |
3b7ad313 EN |
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. */ | |
2c6f1a39 JB |
21 | |
22 | ||
18160b98 | 23 | #include <config.h> |
2c6f1a39 | 24 | #include <stdio.h> |
2c6f1a39 JB |
25 | #include "lisp.h" |
26 | #include "commands.h" | |
27 | #include "buffer.h" | |
a98f1d1d | 28 | #include "charset.h" |
6bbbd9b0 | 29 | #include "keyboard.h" |
6ba6e250 | 30 | #include "termhooks.h" |
9ac0d9e0 | 31 | #include "blockinput.h" |
d964248c | 32 | #include "puresize.h" |
93d2aa1c | 33 | #include "intervals.h" |
8feddab4 | 34 | #include "keymap.h" |
2c6f1a39 | 35 | |
f5b79c1c | 36 | /* The number of elements in keymap vectors. */ |
2c6f1a39 JB |
37 | #define DENSE_TABLE_SIZE (0200) |
38 | ||
39 | /* Actually allocate storage for these variables */ | |
40 | ||
41 | Lisp_Object current_global_map; /* Current global keymap */ | |
42 | ||
43 | Lisp_Object global_map; /* default global key bindings */ | |
44 | ||
45 | Lisp_Object meta_map; /* The keymap used for globally bound | |
46 | ESC-prefixed default commands */ | |
47 | ||
48 | Lisp_Object control_x_map; /* The keymap used for globally bound | |
49 | C-x-prefixed default commands */ | |
50 | ||
51 | /* was MinibufLocalMap */ | |
52 | Lisp_Object Vminibuffer_local_map; | |
53 | /* The keymap used by the minibuf for local | |
54 | bindings when spaces are allowed in the | |
55 | minibuf */ | |
56 | ||
57 | /* was MinibufLocalNSMap */ | |
d55627cc | 58 | Lisp_Object Vminibuffer_local_ns_map; |
2c6f1a39 JB |
59 | /* The keymap used by the minibuf for local |
60 | bindings when spaces are not encouraged | |
61 | in the minibuf */ | |
62 | ||
63 | /* keymap used for minibuffers when doing completion */ | |
64 | /* was MinibufLocalCompletionMap */ | |
65 | Lisp_Object Vminibuffer_local_completion_map; | |
66 | ||
67 | /* keymap used for minibuffers when doing completion and require a match */ | |
68 | /* was MinibufLocalMustMatchMap */ | |
69 | Lisp_Object Vminibuffer_local_must_match_map; | |
70 | ||
cc0a8174 JB |
71 | /* Alist of minor mode variables and keymaps. */ |
72 | Lisp_Object Vminor_mode_map_alist; | |
73 | ||
dd9cda06 RS |
74 | /* Alist of major-mode-specific overrides for |
75 | minor mode variables and keymaps. */ | |
76 | Lisp_Object Vminor_mode_overriding_map_alist; | |
77 | ||
99cbcaca KS |
78 | /* List of emulation mode keymap alists. */ |
79 | Lisp_Object Vemulation_mode_map_alists; | |
80 | ||
6bbbd9b0 JB |
81 | /* Keymap mapping ASCII function key sequences onto their preferred forms. |
82 | Initialized by the terminal-specific lisp files. See DEFVAR for more | |
83 | documentation. */ | |
84 | Lisp_Object Vfunction_key_map; | |
85 | ||
d7bf9bf5 RS |
86 | /* Keymap mapping ASCII function key sequences onto their preferred forms. */ |
87 | Lisp_Object Vkey_translation_map; | |
88 | ||
107fd03d RS |
89 | /* A list of all commands given new bindings since a certain time |
90 | when nil was stored here. | |
91 | This is used to speed up recomputation of menu key equivalents | |
92 | when Emacs starts up. t means don't record anything here. */ | |
93 | Lisp_Object Vdefine_key_rebound_commands; | |
94 | ||
a1df473f | 95 | Lisp_Object Qkeymapp, Qkeymap, Qnon_ascii, Qmenu_item, Qremap; |
2c6f1a39 | 96 | |
ade19cac RS |
97 | /* Alist of elements like (DEL . "\d"). */ |
98 | static Lisp_Object exclude_keys; | |
99 | ||
a1df473f KS |
100 | /* Pre-allocated 2-element vector for Fremap_command to use. */ |
101 | static Lisp_Object remap_command_vector; | |
102 | ||
3d248688 JB |
103 | /* A char with the CHAR_META bit set in a vector or the 0200 bit set |
104 | in a string key sequence is equivalent to prefixing with this | |
105 | character. */ | |
2c6f1a39 JB |
106 | extern Lisp_Object meta_prefix_char; |
107 | ||
7d92e329 RS |
108 | extern Lisp_Object Voverriding_local_map; |
109 | ||
1e7d1ab0 SM |
110 | /* Hash table used to cache a reverse-map to speed up calls to where-is. */ |
111 | static Lisp_Object where_is_cache; | |
112 | /* Which keymaps are reverse-stored in the cache. */ | |
113 | static Lisp_Object where_is_cache_keymaps; | |
114 | ||
57495396 SM |
115 | static Lisp_Object store_in_keymap P_ ((Lisp_Object, Lisp_Object, Lisp_Object)); |
116 | static void fix_submap_inheritance P_ ((Lisp_Object, Lisp_Object, Lisp_Object)); | |
117 | ||
118 | static Lisp_Object define_as_prefix P_ ((Lisp_Object, Lisp_Object)); | |
d55627cc SM |
119 | static void describe_command P_ ((Lisp_Object, Lisp_Object)); |
120 | static void describe_translation P_ ((Lisp_Object, Lisp_Object)); | |
57495396 | 121 | static void describe_map P_ ((Lisp_Object, Lisp_Object, |
d55627cc | 122 | void (*) P_ ((Lisp_Object, Lisp_Object)), |
57495396 | 123 | int, Lisp_Object, Lisp_Object*, int)); |
15fff01d | 124 | static void silly_event_symbol_error P_ ((Lisp_Object)); |
2c6f1a39 | 125 | \f |
cc0a8174 JB |
126 | /* Keymap object support - constructors and predicates. */ |
127 | ||
ce6e5d0b | 128 | DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 1, 0, |
335c5470 PJ |
129 | doc: /* Construct and return a new keymap, of the form (keymap CHARTABLE . ALIST). |
130 | CHARTABLE is a char-table that holds the bindings for the ASCII | |
131 | characters. ALIST is an assoc-list which holds bindings for function keys, | |
132 | mouse events, and any other things that appear in the input stream. | |
133 | All entries in it are initially nil, meaning "command undefined". | |
134 | ||
135 | The optional arg STRING supplies a menu name for the keymap | |
136 | in case you use it as a menu with `x-popup-menu'. */) | |
137 | (string) | |
ce6e5d0b | 138 | Lisp_Object string; |
2c6f1a39 | 139 | { |
ce6e5d0b RS |
140 | Lisp_Object tail; |
141 | if (!NILP (string)) | |
142 | tail = Fcons (string, Qnil); | |
143 | else | |
144 | tail = Qnil; | |
2c6f1a39 | 145 | return Fcons (Qkeymap, |
0403641f | 146 | Fcons (Fmake_char_table (Qkeymap, Qnil), tail)); |
2c6f1a39 JB |
147 | } |
148 | ||
ce6e5d0b | 149 | DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 1, 0, |
335c5470 PJ |
150 | doc: /* Construct and return a new sparse keymap. |
151 | Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION), | |
152 | which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION), | |
153 | which binds the function key or mouse event SYMBOL to DEFINITION. | |
154 | Initially the alist is nil. | |
155 | ||
156 | The optional arg STRING supplies a menu name for the keymap | |
157 | in case you use it as a menu with `x-popup-menu'. */) | |
158 | (string) | |
ce6e5d0b | 159 | Lisp_Object string; |
2c6f1a39 | 160 | { |
ce6e5d0b RS |
161 | if (!NILP (string)) |
162 | return Fcons (Qkeymap, Fcons (string, Qnil)); | |
2c6f1a39 JB |
163 | return Fcons (Qkeymap, Qnil); |
164 | } | |
165 | ||
166 | /* This function is used for installing the standard key bindings | |
167 | at initialization time. | |
168 | ||
169 | For example: | |
170 | ||
e25c4e44 | 171 | initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); */ |
2c6f1a39 JB |
172 | |
173 | void | |
174 | initial_define_key (keymap, key, defname) | |
175 | Lisp_Object keymap; | |
176 | int key; | |
177 | char *defname; | |
178 | { | |
179 | store_in_keymap (keymap, make_number (key), intern (defname)); | |
180 | } | |
181 | ||
e25c4e44 JB |
182 | void |
183 | initial_define_lispy_key (keymap, keyname, defname) | |
184 | Lisp_Object keymap; | |
185 | char *keyname; | |
186 | char *defname; | |
187 | { | |
188 | store_in_keymap (keymap, intern (keyname), intern (defname)); | |
189 | } | |
190 | ||
2c6f1a39 | 191 | DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0, |
335c5470 PJ |
192 | doc: /* Return t if OBJECT is a keymap. |
193 | ||
194 | A keymap is a list (keymap . ALIST), | |
195 | or a symbol whose function definition is itself a keymap. | |
196 | ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN); | |
197 | a vector of densely packed bindings for small character codes | |
198 | is also allowed as an element. */) | |
199 | (object) | |
2c6f1a39 JB |
200 | Lisp_Object object; |
201 | { | |
02067692 | 202 | return (KEYMAPP (object) ? Qt : Qnil); |
2c6f1a39 JB |
203 | } |
204 | ||
54cbc3d4 | 205 | DEFUN ("keymap-prompt", Fkeymap_prompt, Skeymap_prompt, 1, 1, 0, |
335c5470 PJ |
206 | doc: /* Return the prompt-string of a keymap MAP. |
207 | If non-nil, the prompt is shown in the echo-area | |
208 | when reading a key-sequence to be looked-up in this keymap. */) | |
209 | (map) | |
54cbc3d4 SM |
210 | Lisp_Object map; |
211 | { | |
212 | while (CONSP (map)) | |
213 | { | |
214 | register Lisp_Object tem; | |
215 | tem = Fcar (map); | |
216 | if (STRINGP (tem)) | |
217 | return tem; | |
218 | map = Fcdr (map); | |
219 | } | |
220 | return Qnil; | |
221 | } | |
222 | ||
2c6f1a39 | 223 | /* Check that OBJECT is a keymap (after dereferencing through any |
d09b2024 JB |
224 | symbols). If it is, return it. |
225 | ||
226 | If AUTOLOAD is non-zero and OBJECT is a symbol whose function value | |
227 | is an autoload form, do the autoload and try again. | |
21a0d7a0 | 228 | If AUTOLOAD is nonzero, callers must assume GC is possible. |
d09b2024 | 229 | |
02067692 SM |
230 | If the map needs to be autoloaded, but AUTOLOAD is zero (and ERROR |
231 | is zero as well), return Qt. | |
232 | ||
d09b2024 JB |
233 | ERROR controls how we respond if OBJECT isn't a keymap. |
234 | If ERROR is non-zero, signal an error; otherwise, just return Qnil. | |
235 | ||
236 | Note that most of the time, we don't want to pursue autoloads. | |
237 | Functions like Faccessible_keymaps which scan entire keymap trees | |
238 | shouldn't load every autoloaded keymap. I'm not sure about this, | |
239 | but it seems to me that only read_key_sequence, Flookup_key, and | |
df75b1a3 GM |
240 | Fdefine_key should cause keymaps to be autoloaded. |
241 | ||
242 | This function can GC when AUTOLOAD is non-zero, because it calls | |
243 | do_autoload which can GC. */ | |
d09b2024 | 244 | |
2c6f1a39 | 245 | Lisp_Object |
02067692 | 246 | get_keymap (object, error, autoload) |
2c6f1a39 | 247 | Lisp_Object object; |
d09b2024 | 248 | int error, autoload; |
2c6f1a39 | 249 | { |
d09b2024 | 250 | Lisp_Object tem; |
2c6f1a39 | 251 | |
d09b2024 | 252 | autoload_retry: |
b1314e15 KH |
253 | if (NILP (object)) |
254 | goto end; | |
255 | if (CONSP (object) && EQ (XCAR (object), Qkeymap)) | |
256 | return object; | |
f5b79c1c | 257 | |
02067692 SM |
258 | tem = indirect_function (object); |
259 | if (CONSP (tem)) | |
d09b2024 | 260 | { |
02067692 SM |
261 | if (EQ (XCAR (tem), Qkeymap)) |
262 | return tem; | |
d09b2024 | 263 | |
02067692 SM |
264 | /* Should we do an autoload? Autoload forms for keymaps have |
265 | Qkeymap as their fifth element. */ | |
266 | if ((autoload || !error) && EQ (XCAR (tem), Qautoload)) | |
8e4dfd54 | 267 | { |
02067692 | 268 | Lisp_Object tail; |
d09b2024 | 269 | |
02067692 SM |
270 | tail = Fnth (make_number (4), tem); |
271 | if (EQ (tail, Qkeymap)) | |
272 | { | |
273 | if (autoload) | |
274 | { | |
275 | struct gcpro gcpro1, gcpro2; | |
276 | ||
277 | GCPRO2 (tem, object); | |
278 | do_autoload (tem, object); | |
279 | UNGCPRO; | |
280 | ||
281 | goto autoload_retry; | |
282 | } | |
283 | else | |
284 | return Qt; | |
285 | } | |
8e4dfd54 | 286 | } |
d09b2024 JB |
287 | } |
288 | ||
b1314e15 | 289 | end: |
2c6f1a39 JB |
290 | if (error) |
291 | wrong_type_argument (Qkeymapp, object); | |
6bbd7a29 | 292 | return Qnil; |
2c6f1a39 | 293 | } |
7d58ed99 RS |
294 | \f |
295 | /* Return the parent map of the keymap MAP, or nil if it has none. | |
296 | We assume that MAP is a valid keymap. */ | |
297 | ||
298 | DEFUN ("keymap-parent", Fkeymap_parent, Skeymap_parent, 1, 1, 0, | |
335c5470 PJ |
299 | doc: /* Return the parent keymap of KEYMAP. */) |
300 | (keymap) | |
7d58ed99 RS |
301 | Lisp_Object keymap; |
302 | { | |
303 | Lisp_Object list; | |
304 | ||
02067692 | 305 | keymap = get_keymap (keymap, 1, 1); |
7d58ed99 RS |
306 | |
307 | /* Skip past the initial element `keymap'. */ | |
03699b14 KR |
308 | list = XCDR (keymap); |
309 | for (; CONSP (list); list = XCDR (list)) | |
7d58ed99 RS |
310 | { |
311 | /* See if there is another `keymap'. */ | |
57495396 | 312 | if (KEYMAPP (list)) |
7d58ed99 RS |
313 | return list; |
314 | } | |
315 | ||
02067692 | 316 | return get_keymap (list, 0, 1); |
7d58ed99 RS |
317 | } |
318 | ||
3015eec0 | 319 | |
b1904cd9 SM |
320 | /* Check whether MAP is one of MAPS parents. */ |
321 | int | |
322 | keymap_memberp (map, maps) | |
323 | Lisp_Object map, maps; | |
324 | { | |
7e05cdaf | 325 | if (NILP (map)) return 0; |
b1904cd9 SM |
326 | while (KEYMAPP (maps) && !EQ (map, maps)) |
327 | maps = Fkeymap_parent (maps); | |
328 | return (EQ (map, maps)); | |
329 | } | |
330 | ||
7d58ed99 RS |
331 | /* Set the parent keymap of MAP to PARENT. */ |
332 | ||
333 | DEFUN ("set-keymap-parent", Fset_keymap_parent, Sset_keymap_parent, 2, 2, 0, | |
335c5470 PJ |
334 | doc: /* Modify KEYMAP to set its parent map to PARENT. |
335 | PARENT should be nil or another keymap. */) | |
336 | (keymap, parent) | |
7d58ed99 RS |
337 | Lisp_Object keymap, parent; |
338 | { | |
339 | Lisp_Object list, prev; | |
df75b1a3 | 340 | struct gcpro gcpro1; |
7d58ed99 | 341 | int i; |
2c6f1a39 | 342 | |
1e7d1ab0 SM |
343 | /* Force a keymap flush for the next call to where-is. |
344 | Since this can be called from within where-is, we don't set where_is_cache | |
345 | directly but only where_is_cache_keymaps, since where_is_cache shouldn't | |
346 | be changed during where-is, while where_is_cache_keymaps is only used at | |
347 | the very beginning of where-is and can thus be changed here without any | |
348 | adverse effect. | |
349 | This is a very minor correctness (rather than safety) issue. */ | |
350 | where_is_cache_keymaps = Qt; | |
351 | ||
02067692 | 352 | keymap = get_keymap (keymap, 1, 1); |
df75b1a3 GM |
353 | GCPRO1 (keymap); |
354 | ||
7d58ed99 | 355 | if (!NILP (parent)) |
3015eec0 | 356 | { |
02067692 | 357 | parent = get_keymap (parent, 1, 1); |
3015eec0 GM |
358 | |
359 | /* Check for cycles. */ | |
b1904cd9 | 360 | if (keymap_memberp (keymap, parent)) |
3015eec0 GM |
361 | error ("Cyclic keymap inheritance"); |
362 | } | |
2c6f1a39 | 363 | |
7d58ed99 RS |
364 | /* Skip past the initial element `keymap'. */ |
365 | prev = keymap; | |
366 | while (1) | |
367 | { | |
03699b14 | 368 | list = XCDR (prev); |
7d58ed99 RS |
369 | /* If there is a parent keymap here, replace it. |
370 | If we came to the end, add the parent in PREV. */ | |
54cbc3d4 | 371 | if (!CONSP (list) || KEYMAPP (list)) |
7d58ed99 | 372 | { |
2a5af1cf RS |
373 | /* If we already have the right parent, return now |
374 | so that we avoid the loops below. */ | |
03699b14 | 375 | if (EQ (XCDR (prev), parent)) |
df75b1a3 | 376 | RETURN_UNGCPRO (parent); |
2a5af1cf | 377 | |
f3fbd155 | 378 | XSETCDR (prev, parent); |
7d58ed99 RS |
379 | break; |
380 | } | |
381 | prev = list; | |
382 | } | |
383 | ||
384 | /* Scan through for submaps, and set their parents too. */ | |
385 | ||
03699b14 | 386 | for (list = XCDR (keymap); CONSP (list); list = XCDR (list)) |
7d58ed99 RS |
387 | { |
388 | /* Stop the scan when we come to the parent. */ | |
03699b14 | 389 | if (EQ (XCAR (list), Qkeymap)) |
7d58ed99 RS |
390 | break; |
391 | ||
392 | /* If this element holds a prefix map, deal with it. */ | |
03699b14 KR |
393 | if (CONSP (XCAR (list)) |
394 | && CONSP (XCDR (XCAR (list)))) | |
395 | fix_submap_inheritance (keymap, XCAR (XCAR (list)), | |
396 | XCDR (XCAR (list))); | |
397 | ||
398 | if (VECTORP (XCAR (list))) | |
399 | for (i = 0; i < XVECTOR (XCAR (list))->size; i++) | |
400 | if (CONSP (XVECTOR (XCAR (list))->contents[i])) | |
7d58ed99 | 401 | fix_submap_inheritance (keymap, make_number (i), |
03699b14 | 402 | XVECTOR (XCAR (list))->contents[i]); |
0403641f | 403 | |
03699b14 | 404 | if (CHAR_TABLE_P (XCAR (list))) |
0403641f | 405 | { |
23cf1efa | 406 | Lisp_Object indices[3]; |
0403641f | 407 | |
03699b14 | 408 | map_char_table (fix_submap_inheritance, Qnil, XCAR (list), |
0403641f RS |
409 | keymap, 0, indices); |
410 | } | |
7d58ed99 RS |
411 | } |
412 | ||
df75b1a3 | 413 | RETURN_UNGCPRO (parent); |
7d58ed99 RS |
414 | } |
415 | ||
416 | /* EVENT is defined in MAP as a prefix, and SUBMAP is its definition. | |
417 | if EVENT is also a prefix in MAP's parent, | |
418 | make sure that SUBMAP inherits that definition as its own parent. */ | |
419 | ||
57495396 | 420 | static void |
7d58ed99 RS |
421 | fix_submap_inheritance (map, event, submap) |
422 | Lisp_Object map, event, submap; | |
423 | { | |
424 | Lisp_Object map_parent, parent_entry; | |
425 | ||
426 | /* SUBMAP is a cons that we found as a key binding. | |
427 | Discard the other things found in a menu key binding. */ | |
428 | ||
02067692 | 429 | submap = get_keymap (get_keyelt (submap, 0), 0, 0); |
7d58ed99 RS |
430 | |
431 | /* If it isn't a keymap now, there's no work to do. */ | |
02067692 | 432 | if (!CONSP (submap)) |
7d58ed99 RS |
433 | return; |
434 | ||
435 | map_parent = Fkeymap_parent (map); | |
02067692 | 436 | if (!NILP (map_parent)) |
320c9428 | 437 | parent_entry = |
02067692 | 438 | get_keymap (access_keymap (map_parent, event, 0, 0, 0), 0, 0); |
7d58ed99 RS |
439 | else |
440 | parent_entry = Qnil; | |
441 | ||
3393c3f5 | 442 | /* If MAP's parent has something other than a keymap, |
9d0ffdd9 | 443 | our own submap shadows it completely. */ |
02067692 | 444 | if (!CONSP (parent_entry)) |
9d0ffdd9 | 445 | return; |
3393c3f5 | 446 | |
7d58ed99 | 447 | if (! EQ (parent_entry, submap)) |
61684f41 RS |
448 | { |
449 | Lisp_Object submap_parent; | |
450 | submap_parent = submap; | |
451 | while (1) | |
452 | { | |
453 | Lisp_Object tem; | |
9d0ffdd9 | 454 | |
61684f41 | 455 | tem = Fkeymap_parent (submap_parent); |
9d0ffdd9 SM |
456 | |
457 | if (KEYMAPP (tem)) | |
458 | { | |
459 | if (keymap_memberp (tem, parent_entry)) | |
460 | /* Fset_keymap_parent could create a cycle. */ | |
461 | return; | |
462 | submap_parent = tem; | |
463 | } | |
61684f41 RS |
464 | else |
465 | break; | |
466 | } | |
467 | Fset_keymap_parent (submap_parent, parent_entry); | |
468 | } | |
7d58ed99 RS |
469 | } |
470 | \f | |
2c6f1a39 | 471 | /* Look up IDX in MAP. IDX may be any sort of event. |
f5b79c1c | 472 | Note that this does only one level of lookup; IDX must be a single |
e25c4e44 JB |
473 | event, not a sequence. |
474 | ||
475 | If T_OK is non-zero, bindings for Qt are treated as default | |
476 | bindings; any key left unmentioned by other tables and bindings is | |
477 | given the binding of Qt. | |
478 | ||
c07aec97 RS |
479 | If T_OK is zero, bindings for Qt are not treated specially. |
480 | ||
481 | If NOINHERIT, don't accept a subkeymap found in an inherited keymap. */ | |
2c6f1a39 JB |
482 | |
483 | Lisp_Object | |
db785038 | 484 | access_keymap (map, idx, t_ok, noinherit, autoload) |
2c6f1a39 JB |
485 | Lisp_Object map; |
486 | Lisp_Object idx; | |
e25c4e44 | 487 | int t_ok; |
c07aec97 | 488 | int noinherit; |
db785038 | 489 | int autoload; |
2c6f1a39 | 490 | { |
efb91645 RS |
491 | Lisp_Object val; |
492 | ||
493 | /* Qunbound in VAL means we have found no binding yet. */ | |
494 | val = Qunbound; | |
c07aec97 | 495 | |
2c6f1a39 JB |
496 | /* If idx is a list (some sort of mouse click, perhaps?), |
497 | the index we want to use is the car of the list, which | |
498 | ought to be a symbol. */ | |
cebd887d | 499 | idx = EVENT_HEAD (idx); |
2c6f1a39 | 500 | |
f5b79c1c JB |
501 | /* If idx is a symbol, it might have modifiers, which need to |
502 | be put in the canonical order. */ | |
47684cd9 | 503 | if (SYMBOLP (idx)) |
f5b79c1c | 504 | idx = reorder_modifiers (idx); |
2732bdbb RS |
505 | else if (INTEGERP (idx)) |
506 | /* Clobber the high bits that can be present on a machine | |
507 | with more than 24 bits of integer. */ | |
6e344130 | 508 | XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1))); |
2c6f1a39 | 509 | |
db785038 SM |
510 | /* Handle the special meta -> esc mapping. */ |
511 | if (INTEGERP (idx) && XUINT (idx) & meta_modifier) | |
512 | { | |
7396a36c GM |
513 | /* See if there is a meta-map. If there's none, there is |
514 | no binding for IDX, unless a default binding exists in MAP. */ | |
9d0ffdd9 | 515 | Lisp_Object meta_map = |
02067692 SM |
516 | get_keymap (access_keymap (map, meta_prefix_char, |
517 | t_ok, noinherit, autoload), | |
518 | 0, autoload); | |
519 | if (CONSP (meta_map)) | |
7396a36c | 520 | { |
9d0ffdd9 | 521 | map = meta_map; |
7396a36c GM |
522 | idx = make_number (XUINT (idx) & ~meta_modifier); |
523 | } | |
524 | else if (t_ok) | |
525 | /* Set IDX to t, so that we only find a default binding. */ | |
526 | idx = Qt; | |
527 | else | |
528 | /* We know there is no binding. */ | |
529 | return Qnil; | |
db785038 SM |
530 | } |
531 | ||
f5b79c1c JB |
532 | { |
533 | Lisp_Object tail; | |
efb91645 RS |
534 | |
535 | /* t_binding is where we put a default binding that applies, | |
536 | to use in case we do not find a binding specifically | |
830ddd77 | 537 | for this key sequence. */ |
efb91645 RS |
538 | |
539 | Lisp_Object t_binding; | |
540 | t_binding = Qnil; | |
845e4cf4 SM |
541 | |
542 | /* If `t_ok' is 2, both `t' and generic-char bindings are accepted. | |
543 | If it is 1, only generic-char bindings are accepted. | |
544 | Otherwise, neither are. */ | |
545 | t_ok = t_ok ? 2 : 0; | |
2c6f1a39 | 546 | |
db785038 | 547 | for (tail = XCDR (map); |
7396a36c | 548 | (CONSP (tail) |
02067692 | 549 | || (tail = get_keymap (tail, 0, autoload), CONSP (tail))); |
db785038 | 550 | tail = XCDR (tail)) |
2c6f1a39 | 551 | { |
e9b6dfb0 | 552 | Lisp_Object binding; |
f5b79c1c | 553 | |
03699b14 | 554 | binding = XCAR (tail); |
783a2838 | 555 | if (SYMBOLP (binding)) |
f5b79c1c | 556 | { |
c07aec97 RS |
557 | /* If NOINHERIT, stop finding prefix definitions |
558 | after we pass a second occurrence of the `keymap' symbol. */ | |
db785038 | 559 | if (noinherit && EQ (binding, Qkeymap)) |
845e4cf4 | 560 | return Qnil; |
783a2838 KH |
561 | } |
562 | else if (CONSP (binding)) | |
563 | { | |
859ea4b8 | 564 | Lisp_Object key = XCAR (binding); |
859ea4b8 GM |
565 | |
566 | if (EQ (key, idx)) | |
845e4cf4 SM |
567 | val = XCDR (binding); |
568 | else if (t_ok | |
569 | && INTEGERP (idx) | |
62b366ff | 570 | && (XINT (idx) & CHAR_MODIFIER_MASK) == 0 |
859ea4b8 | 571 | && INTEGERP (key) |
62b366ff | 572 | && (XINT (key) & CHAR_MODIFIER_MASK) == 0 |
859ea4b8 GM |
573 | && !SINGLE_BYTE_CHAR_P (XINT (idx)) |
574 | && !SINGLE_BYTE_CHAR_P (XINT (key)) | |
575 | && CHAR_VALID_P (XINT (key), 1) | |
576 | && !CHAR_VALID_P (XINT (key), 0) | |
577 | && (CHAR_CHARSET (XINT (key)) | |
578 | == CHAR_CHARSET (XINT (idx)))) | |
579 | { | |
580 | /* KEY is the generic character of the charset of IDX. | |
581 | Use KEY's binding if there isn't a binding for IDX | |
582 | itself. */ | |
845e4cf4 SM |
583 | t_binding = XCDR (binding); |
584 | t_ok = 0; | |
585 | } | |
586 | else if (t_ok > 1 && EQ (key, Qt)) | |
587 | { | |
588 | t_binding = XCDR (binding); | |
589 | t_ok = 1; | |
859ea4b8 | 590 | } |
783a2838 KH |
591 | } |
592 | else if (VECTORP (binding)) | |
593 | { | |
845e4cf4 SM |
594 | if (NATNUMP (idx) && XFASTINT (idx) < ASIZE (binding)) |
595 | val = AREF (binding, XFASTINT (idx)); | |
f5b79c1c | 596 | } |
0403641f RS |
597 | else if (CHAR_TABLE_P (binding)) |
598 | { | |
6418ea16 RS |
599 | /* Character codes with modifiers |
600 | are not included in a char-table. | |
601 | All character codes without modifiers are included. */ | |
4dc3eb25 SM |
602 | if (NATNUMP (idx) && (XFASTINT (idx) & CHAR_MODIFIER_MASK) == 0) |
603 | { | |
604 | val = Faref (binding, idx); | |
605 | /* `nil' has a special meaning for char-tables, so | |
606 | we use something else to record an explicitly | |
607 | unbound entry. */ | |
608 | if (NILP (val)) | |
609 | val = Qunbound; | |
610 | } | |
0403641f | 611 | } |
20218e2f | 612 | |
845e4cf4 SM |
613 | /* If we found a binding, clean it up and return it. */ |
614 | if (!EQ (val, Qunbound)) | |
615 | { | |
4dc3eb25 SM |
616 | if (EQ (val, Qt)) |
617 | /* A Qt binding is just like an explicit nil binding | |
618 | (i.e. it shadows any parent binding but not bindings in | |
619 | keymaps of lower precedence). */ | |
620 | val = Qnil; | |
845e4cf4 SM |
621 | val = get_keyelt (val, autoload); |
622 | if (KEYMAPP (val)) | |
623 | fix_submap_inheritance (map, idx, val); | |
624 | return val; | |
625 | } | |
20218e2f | 626 | QUIT; |
2c6f1a39 | 627 | } |
fde3a52f | 628 | |
db785038 | 629 | return get_keyelt (t_binding, autoload); |
e25c4e44 | 630 | } |
2c6f1a39 JB |
631 | } |
632 | ||
633 | /* Given OBJECT which was found in a slot in a keymap, | |
634 | trace indirect definitions to get the actual definition of that slot. | |
635 | An indirect definition is a list of the form | |
636 | (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one | |
637 | and INDEX is the object to look up in KEYMAP to yield the definition. | |
638 | ||
639 | Also if OBJECT has a menu string as the first element, | |
224a16e8 RS |
640 | remove that. Also remove a menu help string as second element. |
641 | ||
642 | If AUTOLOAD is nonzero, load autoloadable keymaps | |
643 | that are referred to with indirection. */ | |
2c6f1a39 JB |
644 | |
645 | Lisp_Object | |
224a16e8 | 646 | get_keyelt (object, autoload) |
2c6f1a39 | 647 | register Lisp_Object object; |
224a16e8 | 648 | int autoload; |
2c6f1a39 JB |
649 | { |
650 | while (1) | |
651 | { | |
b1314e15 KH |
652 | if (!(CONSP (object))) |
653 | /* This is really the value. */ | |
654 | return object; | |
2c6f1a39 | 655 | |
b1314e15 KH |
656 | /* If the keymap contents looks like (keymap ...) or (lambda ...) |
657 | then use itself. */ | |
658 | else if (EQ (XCAR (object), Qkeymap) || EQ (XCAR (object), Qlambda)) | |
659 | return object; | |
660 | ||
661 | /* If the keymap contents looks like (menu-item name . DEFN) | |
662 | or (menu-item name DEFN ...) then use DEFN. | |
3fc720e4 | 663 | This is a new format menu item. */ |
b1314e15 | 664 | else if (EQ (XCAR (object), Qmenu_item)) |
0403641f | 665 | { |
b1314e15 | 666 | if (CONSP (XCDR (object))) |
0403641f | 667 | { |
3fc720e4 GM |
668 | Lisp_Object tem; |
669 | ||
b1314e15 | 670 | object = XCDR (XCDR (object)); |
3fc720e4 | 671 | tem = object; |
b1314e15 KH |
672 | if (CONSP (object)) |
673 | object = XCAR (object); | |
3fc720e4 GM |
674 | |
675 | /* If there's a `:filter FILTER', apply FILTER to the | |
676 | menu-item's definition to get the real definition to | |
d5b3eb1b | 677 | use. */ |
3fc720e4 | 678 | for (; CONSP (tem) && CONSP (XCDR (tem)); tem = XCDR (tem)) |
d5b3eb1b | 679 | if (EQ (XCAR (tem), QCfilter) && autoload) |
3fc720e4 GM |
680 | { |
681 | Lisp_Object filter; | |
682 | filter = XCAR (XCDR (tem)); | |
683 | filter = list2 (filter, list2 (Qquote, object)); | |
684 | object = menu_item_eval_property (filter); | |
685 | break; | |
686 | } | |
0403641f RS |
687 | } |
688 | else | |
b1314e15 KH |
689 | /* Invalid keymap */ |
690 | return object; | |
0403641f RS |
691 | } |
692 | ||
b1314e15 | 693 | /* If the keymap contents looks like (STRING . DEFN), use DEFN. |
2c6f1a39 JB |
694 | Keymap alist elements like (CHAR MENUSTRING . DEFN) |
695 | will be used by HierarKey menus. */ | |
b1314e15 | 696 | else if (STRINGP (XCAR (object))) |
1a8c3f10 | 697 | { |
b1314e15 | 698 | object = XCDR (object); |
1a8c3f10 RS |
699 | /* Also remove a menu help string, if any, |
700 | following the menu item name. */ | |
b1314e15 KH |
701 | if (CONSP (object) && STRINGP (XCAR (object))) |
702 | object = XCDR (object); | |
c6ec9f6e | 703 | /* Also remove the sublist that caches key equivalences, if any. */ |
b1314e15 | 704 | if (CONSP (object) && CONSP (XCAR (object))) |
ffab2bd6 | 705 | { |
c6ec9f6e | 706 | Lisp_Object carcar; |
b1314e15 | 707 | carcar = XCAR (XCAR (object)); |
c6ec9f6e | 708 | if (NILP (carcar) || VECTORP (carcar)) |
b1314e15 | 709 | object = XCDR (object); |
ffab2bd6 | 710 | } |
1a8c3f10 | 711 | } |
2c6f1a39 | 712 | |
b1314e15 KH |
713 | /* If the contents are (KEYMAP . ELEMENT), go indirect. */ |
714 | else | |
a3fc8840 | 715 | { |
df75b1a3 | 716 | Lisp_Object map; |
02067692 SM |
717 | map = get_keymap (Fcar_safe (object), 0, autoload); |
718 | return (!CONSP (map) ? object /* Invalid keymap */ | |
db785038 | 719 | : access_keymap (map, Fcdr (object), 0, 0, autoload)); |
a3fc8840 | 720 | } |
2c6f1a39 JB |
721 | } |
722 | } | |
723 | ||
2d929694 | 724 | static Lisp_Object |
2c6f1a39 JB |
725 | store_in_keymap (keymap, idx, def) |
726 | Lisp_Object keymap; | |
727 | register Lisp_Object idx; | |
728 | register Lisp_Object def; | |
729 | { | |
1e7d1ab0 SM |
730 | /* Flush any reverse-map cache. */ |
731 | where_is_cache = Qnil; | |
732 | where_is_cache_keymaps = Qt; | |
733 | ||
dce4372a | 734 | /* If we are preparing to dump, and DEF is a menu element |
a3fc8840 RS |
735 | with a menu item indicator, copy it to ensure it is not pure. */ |
736 | if (CONSP (def) && PURE_P (def) | |
03699b14 KR |
737 | && (EQ (XCAR (def), Qmenu_item) || STRINGP (XCAR (def)))) |
738 | def = Fcons (XCAR (def), XCDR (def)); | |
32ce36ad | 739 | |
54cbc3d4 | 740 | if (!CONSP (keymap) || !EQ (XCAR (keymap), Qkeymap)) |
f5b79c1c JB |
741 | error ("attempt to define a key in a non-keymap"); |
742 | ||
2c6f1a39 JB |
743 | /* If idx is a list (some sort of mouse click, perhaps?), |
744 | the index we want to use is the car of the list, which | |
745 | ought to be a symbol. */ | |
cebd887d | 746 | idx = EVENT_HEAD (idx); |
2c6f1a39 | 747 | |
f5b79c1c JB |
748 | /* If idx is a symbol, it might have modifiers, which need to |
749 | be put in the canonical order. */ | |
416349ec | 750 | if (SYMBOLP (idx)) |
f5b79c1c | 751 | idx = reorder_modifiers (idx); |
2732bdbb RS |
752 | else if (INTEGERP (idx)) |
753 | /* Clobber the high bits that can be present on a machine | |
754 | with more than 24 bits of integer. */ | |
6e344130 | 755 | XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1))); |
f5b79c1c JB |
756 | |
757 | /* Scan the keymap for a binding of idx. */ | |
2c6f1a39 | 758 | { |
f5b79c1c | 759 | Lisp_Object tail; |
2c6f1a39 | 760 | |
f5b79c1c JB |
761 | /* The cons after which we should insert new bindings. If the |
762 | keymap has a table element, we record its position here, so new | |
763 | bindings will go after it; this way, the table will stay | |
764 | towards the front of the alist and character lookups in dense | |
765 | keymaps will remain fast. Otherwise, this just points at the | |
766 | front of the keymap. */ | |
e9b6dfb0 | 767 | Lisp_Object insertion_point; |
2c6f1a39 | 768 | |
e9b6dfb0 | 769 | insertion_point = keymap; |
03699b14 | 770 | for (tail = XCDR (keymap); CONSP (tail); tail = XCDR (tail)) |
2c6f1a39 | 771 | { |
e9b6dfb0 | 772 | Lisp_Object elt; |
f5b79c1c | 773 | |
03699b14 | 774 | elt = XCAR (tail); |
783a2838 | 775 | if (VECTORP (elt)) |
f5b79c1c | 776 | { |
49801145 | 777 | if (NATNUMP (idx) && XFASTINT (idx) < ASIZE (elt)) |
f5b79c1c | 778 | { |
49801145 | 779 | ASET (elt, XFASTINT (idx), def); |
f5b79c1c JB |
780 | return def; |
781 | } | |
782 | insertion_point = tail; | |
783a2838 | 783 | } |
0403641f RS |
784 | else if (CHAR_TABLE_P (elt)) |
785 | { | |
6418ea16 RS |
786 | /* Character codes with modifiers |
787 | are not included in a char-table. | |
788 | All character codes without modifiers are included. */ | |
4dc3eb25 | 789 | if (NATNUMP (idx) && !(XFASTINT (idx) & CHAR_MODIFIER_MASK)) |
0403641f | 790 | { |
4dc3eb25 SM |
791 | Faset (elt, idx, |
792 | /* `nil' has a special meaning for char-tables, so | |
793 | we use something else to record an explicitly | |
794 | unbound entry. */ | |
795 | NILP (def) ? Qt : def); | |
0403641f RS |
796 | return def; |
797 | } | |
798 | insertion_point = tail; | |
799 | } | |
783a2838 KH |
800 | else if (CONSP (elt)) |
801 | { | |
03699b14 | 802 | if (EQ (idx, XCAR (elt))) |
f5b79c1c | 803 | { |
f3fbd155 | 804 | XSETCDR (elt, def); |
f5b79c1c JB |
805 | return def; |
806 | } | |
783a2838 | 807 | } |
49801145 SM |
808 | else if (EQ (elt, Qkeymap)) |
809 | /* If we find a 'keymap' symbol in the spine of KEYMAP, | |
810 | then we must have found the start of a second keymap | |
811 | being used as the tail of KEYMAP, and a binding for IDX | |
812 | should be inserted before it. */ | |
813 | goto keymap_end; | |
0188441d JB |
814 | |
815 | QUIT; | |
2c6f1a39 | 816 | } |
2c6f1a39 | 817 | |
f5b79c1c JB |
818 | keymap_end: |
819 | /* We have scanned the entire keymap, and not found a binding for | |
820 | IDX. Let's add one. */ | |
f3fbd155 KR |
821 | XSETCDR (insertion_point, |
822 | Fcons (Fcons (idx, def), XCDR (insertion_point))); | |
f5b79c1c | 823 | } |
b1904cd9 | 824 | |
2c6f1a39 JB |
825 | return def; |
826 | } | |
827 | ||
2b6748c0 SM |
828 | EXFUN (Fcopy_keymap, 1); |
829 | ||
69248761 | 830 | void |
0403641f RS |
831 | copy_keymap_1 (chartable, idx, elt) |
832 | Lisp_Object chartable, idx, elt; | |
833 | { | |
02067692 | 834 | if (CONSP (elt) && EQ (XCAR (elt), Qkeymap)) |
bee3fc83 | 835 | Faset (chartable, idx, Fcopy_keymap (elt)); |
0403641f | 836 | } |
f5b79c1c | 837 | |
2c6f1a39 | 838 | DEFUN ("copy-keymap", Fcopy_keymap, Scopy_keymap, 1, 1, 0, |
335c5470 PJ |
839 | doc: /* Return a copy of the keymap KEYMAP. |
840 | The copy starts out with the same definitions of KEYMAP, | |
841 | but changing either the copy or KEYMAP does not affect the other. | |
842 | Any key definitions that are subkeymaps are recursively copied. | |
843 | However, a key definition which is a symbol whose definition is a keymap | |
844 | is not copied. */) | |
845 | (keymap) | |
2c6f1a39 JB |
846 | Lisp_Object keymap; |
847 | { | |
54cbc3d4 SM |
848 | /* FIXME: This doesn't properly copy menu-items in vectors. */ |
849 | /* FIXME: This also copies the parent keymap. */ | |
850 | ||
2c6f1a39 JB |
851 | register Lisp_Object copy, tail; |
852 | ||
02067692 | 853 | copy = Fcopy_alist (get_keymap (keymap, 1, 0)); |
2c6f1a39 | 854 | |
03699b14 | 855 | for (tail = copy; CONSP (tail); tail = XCDR (tail)) |
2c6f1a39 | 856 | { |
e9b6dfb0 | 857 | Lisp_Object elt; |
2c6f1a39 | 858 | |
03699b14 | 859 | elt = XCAR (tail); |
0403641f RS |
860 | if (CHAR_TABLE_P (elt)) |
861 | { | |
23cf1efa | 862 | Lisp_Object indices[3]; |
0403641f RS |
863 | |
864 | elt = Fcopy_sequence (elt); | |
f3fbd155 | 865 | XSETCAR (tail, elt); |
7cc06296 | 866 | |
0403641f RS |
867 | map_char_table (copy_keymap_1, Qnil, elt, elt, 0, indices); |
868 | } | |
869 | else if (VECTORP (elt)) | |
2c6f1a39 | 870 | { |
f5b79c1c | 871 | int i; |
2c6f1a39 | 872 | |
f5b79c1c | 873 | elt = Fcopy_sequence (elt); |
f3fbd155 | 874 | XSETCAR (tail, elt); |
2c6f1a39 | 875 | |
49801145 | 876 | for (i = 0; i < ASIZE (elt); i++) |
02067692 | 877 | if (CONSP (AREF (elt, i)) && EQ (XCAR (AREF (elt, i)), Qkeymap)) |
49801145 | 878 | ASET (elt, i, Fcopy_keymap (AREF (elt, i))); |
2c6f1a39 | 879 | } |
03699b14 | 880 | else if (CONSP (elt) && CONSP (XCDR (elt))) |
d65a13c5 | 881 | { |
a3fc8840 | 882 | Lisp_Object tem; |
03699b14 | 883 | tem = XCDR (elt); |
d65a13c5 | 884 | |
a3fc8840 | 885 | /* Is this a new format menu item. */ |
03699b14 | 886 | if (EQ (XCAR (tem),Qmenu_item)) |
a3fc8840 RS |
887 | { |
888 | /* Copy cell with menu-item marker. */ | |
f3fbd155 KR |
889 | XSETCDR (elt, |
890 | Fcons (XCAR (tem), XCDR (tem))); | |
03699b14 KR |
891 | elt = XCDR (elt); |
892 | tem = XCDR (elt); | |
a3fc8840 RS |
893 | if (CONSP (tem)) |
894 | { | |
895 | /* Copy cell with menu-item name. */ | |
f3fbd155 KR |
896 | XSETCDR (elt, |
897 | Fcons (XCAR (tem), XCDR (tem))); | |
03699b14 KR |
898 | elt = XCDR (elt); |
899 | tem = XCDR (elt); | |
a3fc8840 RS |
900 | }; |
901 | if (CONSP (tem)) | |
902 | { | |
903 | /* Copy cell with binding and if the binding is a keymap, | |
904 | copy that. */ | |
f3fbd155 KR |
905 | XSETCDR (elt, |
906 | Fcons (XCAR (tem), XCDR (tem))); | |
03699b14 KR |
907 | elt = XCDR (elt); |
908 | tem = XCAR (elt); | |
02067692 | 909 | if (CONSP (tem) && EQ (XCAR (tem), Qkeymap)) |
f3fbd155 | 910 | XSETCAR (elt, Fcopy_keymap (tem)); |
03699b14 KR |
911 | tem = XCDR (elt); |
912 | if (CONSP (tem) && CONSP (XCAR (tem))) | |
a3fc8840 | 913 | /* Delete cache for key equivalences. */ |
f3fbd155 | 914 | XSETCDR (elt, XCDR (tem)); |
a3fc8840 RS |
915 | } |
916 | } | |
917 | else | |
918 | { | |
919 | /* It may be an old fomat menu item. | |
920 | Skip the optional menu string. | |
921 | */ | |
03699b14 | 922 | if (STRINGP (XCAR (tem))) |
d65a13c5 | 923 | { |
a3fc8840 | 924 | /* Copy the cell, since copy-alist didn't go this deep. */ |
f3fbd155 KR |
925 | XSETCDR (elt, |
926 | Fcons (XCAR (tem), XCDR (tem))); | |
03699b14 KR |
927 | elt = XCDR (elt); |
928 | tem = XCDR (elt); | |
a3fc8840 | 929 | /* Also skip the optional menu help string. */ |
03699b14 | 930 | if (CONSP (tem) && STRINGP (XCAR (tem))) |
a3fc8840 | 931 | { |
f3fbd155 KR |
932 | XSETCDR (elt, |
933 | Fcons (XCAR (tem), XCDR (tem))); | |
03699b14 KR |
934 | elt = XCDR (elt); |
935 | tem = XCDR (elt); | |
a3fc8840 RS |
936 | } |
937 | /* There may also be a list that caches key equivalences. | |
938 | Just delete it for the new keymap. */ | |
939 | if (CONSP (tem) | |
03699b14 KR |
940 | && CONSP (XCAR (tem)) |
941 | && (NILP (XCAR (XCAR (tem))) | |
942 | || VECTORP (XCAR (XCAR (tem))))) | |
f3fbd155 | 943 | XSETCDR (elt, XCDR (tem)); |
d65a13c5 | 944 | } |
a3fc8840 | 945 | if (CONSP (elt) |
02067692 SM |
946 | && CONSP (XCDR (elt)) |
947 | && EQ (XCAR (XCDR (elt)), Qkeymap)) | |
f3fbd155 | 948 | XSETCDR (elt, Fcopy_keymap (XCDR (elt))); |
d65a13c5 | 949 | } |
a3fc8840 | 950 | |
d65a13c5 | 951 | } |
2c6f1a39 | 952 | } |
2b6748c0 | 953 | |
2c6f1a39 JB |
954 | return copy; |
955 | } | |
956 | \f | |
cc0a8174 JB |
957 | /* Simple Keymap mutators and accessors. */ |
958 | ||
21a0d7a0 RS |
959 | /* GC is possible in this function if it autoloads a keymap. */ |
960 | ||
2c6f1a39 | 961 | DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0, |
335c5470 | 962 | doc: /* Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF. |
0c412762 KS |
963 | KEYMAP is a keymap. |
964 | ||
965 | KEY is a string or a vector of symbols and characters meaning a | |
966 | sequence of keystrokes and events. Non-ASCII characters with codes | |
967 | above 127 (such as ISO Latin-1) can be included if you use a vector. | |
968 | ||
335c5470 PJ |
969 | DEF is anything that can be a key's definition: |
970 | nil (means key is undefined in this keymap), | |
971 | a command (a Lisp function suitable for interactive calling) | |
972 | a string (treated as a keyboard macro), | |
973 | a keymap (to define a prefix key), | |
974 | a symbol. When the key is looked up, the symbol will stand for its | |
975 | function definition, which should at that time be one of the above, | |
976 | or another symbol whose function definition is used, etc. | |
977 | a cons (STRING . DEFN), meaning that DEFN is the definition | |
978 | (DEFN should be a valid definition in its own right), | |
979 | or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP. | |
980 | ||
981 | If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at | |
a1df473f | 982 | the front of KEYMAP. */) |
335c5470 | 983 | (keymap, key, def) |
d09b2024 | 984 | Lisp_Object keymap; |
2c6f1a39 JB |
985 | Lisp_Object key; |
986 | Lisp_Object def; | |
987 | { | |
988 | register int idx; | |
989 | register Lisp_Object c; | |
2c6f1a39 JB |
990 | register Lisp_Object cmd; |
991 | int metized = 0; | |
6ba6e250 | 992 | int meta_bit; |
2c6f1a39 | 993 | int length; |
d09b2024 | 994 | struct gcpro gcpro1, gcpro2, gcpro3; |
2c6f1a39 | 995 | |
02067692 | 996 | keymap = get_keymap (keymap, 1, 1); |
2c6f1a39 | 997 | |
a1df473f | 998 | if (!VECTORP (key) && !STRINGP (key)) |
0c412762 | 999 | key = wrong_type_argument (Qarrayp, key); |
2c6f1a39 | 1000 | |
d09b2024 | 1001 | length = XFASTINT (Flength (key)); |
2c6f1a39 JB |
1002 | if (length == 0) |
1003 | return Qnil; | |
1004 | ||
a1df473f KS |
1005 | /* Check for valid [remap COMMAND] bindings. */ |
1006 | if (VECTORP (key) && EQ (AREF (key, 0), Qremap) | |
1007 | && (length != 2 || !SYMBOLP (AREF (key, 1)))) | |
1008 | wrong_type_argument (Qvectorp, key); | |
1009 | ||
107fd03d RS |
1010 | if (SYMBOLP (def) && !EQ (Vdefine_key_rebound_commands, Qt)) |
1011 | Vdefine_key_rebound_commands = Fcons (def, Vdefine_key_rebound_commands); | |
1012 | ||
d09b2024 JB |
1013 | GCPRO3 (keymap, key, def); |
1014 | ||
416349ec | 1015 | if (VECTORP (key)) |
6ba6e250 RS |
1016 | meta_bit = meta_modifier; |
1017 | else | |
1018 | meta_bit = 0x80; | |
1019 | ||
2c6f1a39 JB |
1020 | idx = 0; |
1021 | while (1) | |
1022 | { | |
1023 | c = Faref (key, make_number (idx)); | |
1024 | ||
f09bc924 | 1025 | if (CONSP (c) && lucid_event_type_list_p (c)) |
41015a19 | 1026 | c = Fevent_convert_list (c); |
f09bc924 | 1027 | |
15fff01d RS |
1028 | if (SYMBOLP (c)) |
1029 | silly_event_symbol_error (c); | |
2fae9111 | 1030 | |
416349ec | 1031 | if (INTEGERP (c) |
6ba6e250 | 1032 | && (XINT (c) & meta_bit) |
2c6f1a39 JB |
1033 | && !metized) |
1034 | { | |
1035 | c = meta_prefix_char; | |
1036 | metized = 1; | |
1037 | } | |
1038 | else | |
1039 | { | |
416349ec | 1040 | if (INTEGERP (c)) |
0b8fc2d4 | 1041 | XSETINT (c, XINT (c) & ~meta_bit); |
2c6f1a39 JB |
1042 | |
1043 | metized = 0; | |
1044 | idx++; | |
1045 | } | |
1046 | ||
54cbc3d4 | 1047 | if (!INTEGERP (c) && !SYMBOLP (c) && !CONSP (c)) |
2fae9111 | 1048 | error ("Key sequence contains invalid event"); |
5907b863 | 1049 | |
2c6f1a39 | 1050 | if (idx == length) |
d09b2024 | 1051 | RETURN_UNGCPRO (store_in_keymap (keymap, c, def)); |
2c6f1a39 | 1052 | |
db785038 | 1053 | cmd = access_keymap (keymap, c, 0, 1, 1); |
2c6f1a39 | 1054 | |
c07aec97 | 1055 | /* If this key is undefined, make it a prefix. */ |
265a9e55 | 1056 | if (NILP (cmd)) |
c07aec97 | 1057 | cmd = define_as_prefix (keymap, c); |
2c6f1a39 | 1058 | |
02067692 SM |
1059 | keymap = get_keymap (cmd, 0, 1); |
1060 | if (!CONSP (keymap)) | |
e9b6dfb0 KH |
1061 | /* We must use Fkey_description rather than just passing key to |
1062 | error; key might be a vector, not a string. */ | |
1063 | error ("Key sequence %s uses invalid prefix characters", | |
1064 | XSTRING (Fkey_description (key))->data); | |
2c6f1a39 JB |
1065 | } |
1066 | } | |
1067 | ||
a1df473f KS |
1068 | /* This function may GC (it calls Fkey_binding). */ |
1069 | ||
1070 | DEFUN ("remap-command", Fremap_command, Sremap_command, 1, 1, 0, | |
1071 | doc: /* Return the remapping for command COMMAND in current keymaps. | |
1072 | Returns nil if COMMAND is not remapped. */) | |
1073 | (command) | |
1074 | Lisp_Object command; | |
1075 | { | |
1076 | /* This will GCPRO the command argument. */ | |
1077 | ASET (remap_command_vector, 1, command); | |
1078 | return Fkey_binding (remap_command_vector, Qnil, Qt); | |
1079 | } | |
1080 | ||
f0529b5b | 1081 | /* Value is number if KEY is too long; nil if valid but has no definition. */ |
21a0d7a0 | 1082 | /* GC is possible in this function if it autoloads a keymap. */ |
2c6f1a39 | 1083 | |
7c140252 | 1084 | DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 3, 0, |
335c5470 PJ |
1085 | doc: /* In keymap KEYMAP, look up key sequence KEY. Return the definition. |
1086 | nil means undefined. See doc of `define-key' for kinds of definitions. | |
1087 | ||
1088 | A number as value means KEY is "too long"; | |
1089 | that is, characters or symbols in it except for the last one | |
1090 | fail to be a valid sequence of prefix characters in KEYMAP. | |
1091 | The number is how many characters at the front of KEY | |
1092 | it takes to reach a non-prefix command. | |
1093 | ||
1094 | Normally, `lookup-key' ignores bindings for t, which act as default | |
1095 | bindings, used when nothing else in the keymap applies; this makes it | |
1096 | usable as a general function for probing keymaps. However, if the | |
1097 | third optional argument ACCEPT-DEFAULT is non-nil, `lookup-key' will | |
1098 | recognize the default bindings, just as `read-key-sequence' does. */) | |
1099 | (keymap, key, accept_default) | |
2c6f1a39 JB |
1100 | register Lisp_Object keymap; |
1101 | Lisp_Object key; | |
7c140252 | 1102 | Lisp_Object accept_default; |
2c6f1a39 JB |
1103 | { |
1104 | register int idx; | |
2c6f1a39 JB |
1105 | register Lisp_Object cmd; |
1106 | register Lisp_Object c; | |
2c6f1a39 | 1107 | int length; |
54cbc3d4 | 1108 | int t_ok = !NILP (accept_default); |
21a0d7a0 | 1109 | struct gcpro gcpro1; |
2c6f1a39 | 1110 | |
02067692 | 1111 | keymap = get_keymap (keymap, 1, 1); |
2c6f1a39 | 1112 | |
a1df473f KS |
1113 | /* Perform command remapping initiated by Fremap_command directly. |
1114 | This is strictly not necessary, but it is faster and it returns | |
1115 | nil instead of 1 if KEYMAP doesn't contain command remappings. */ | |
1116 | if (EQ (key, remap_command_vector)) | |
1117 | { | |
1118 | /* KEY has format [remap COMMAND]. | |
1119 | Lookup `remap' in KEYMAP; result is nil or a keymap containing | |
1120 | command remappings. Then lookup COMMAND in that keymap. */ | |
1121 | if ((keymap = access_keymap (keymap, Qremap, t_ok, 0, 1), !NILP (keymap)) | |
1122 | && (keymap = get_keymap (keymap, 0, 1), CONSP (keymap))) | |
1123 | return access_keymap (keymap, AREF (key, 1), t_ok, 0, 1); | |
1124 | return Qnil; | |
1125 | } | |
0c412762 | 1126 | |
416349ec | 1127 | if (!VECTORP (key) && !STRINGP (key)) |
2c6f1a39 JB |
1128 | key = wrong_type_argument (Qarrayp, key); |
1129 | ||
d09b2024 | 1130 | length = XFASTINT (Flength (key)); |
2c6f1a39 JB |
1131 | if (length == 0) |
1132 | return keymap; | |
1133 | ||
21a0d7a0 RS |
1134 | GCPRO1 (key); |
1135 | ||
2c6f1a39 JB |
1136 | idx = 0; |
1137 | while (1) | |
1138 | { | |
db785038 | 1139 | c = Faref (key, make_number (idx++)); |
2c6f1a39 | 1140 | |
f09bc924 | 1141 | if (CONSP (c) && lucid_event_type_list_p (c)) |
41015a19 | 1142 | c = Fevent_convert_list (c); |
f09bc924 | 1143 | |
db785038 SM |
1144 | /* Turn the 8th bit of string chars into a meta modifier. */ |
1145 | if (XINT (c) & 0x80 && STRINGP (key)) | |
1146 | XSETINT (c, (XINT (c) | meta_modifier) & ~0x80); | |
2c6f1a39 | 1147 | |
5f245371 KS |
1148 | /* Allow string since binding for `menu-bar-select-buffer' |
1149 | includes the buffer name in the key sequence. */ | |
1150 | if (!INTEGERP (c) && !SYMBOLP (c) && !CONSP (c) && !STRINGP (c)) | |
2fae9111 RS |
1151 | error ("Key sequence contains invalid event"); |
1152 | ||
db785038 | 1153 | cmd = access_keymap (keymap, c, t_ok, 0, 1); |
2c6f1a39 | 1154 | if (idx == length) |
21a0d7a0 | 1155 | RETURN_UNGCPRO (cmd); |
2c6f1a39 | 1156 | |
02067692 SM |
1157 | keymap = get_keymap (cmd, 0, 1); |
1158 | if (!CONSP (keymap)) | |
21a0d7a0 | 1159 | RETURN_UNGCPRO (make_number (idx)); |
2c6f1a39 | 1160 | |
2c6f1a39 JB |
1161 | QUIT; |
1162 | } | |
1163 | } | |
1164 | ||
c07aec97 RS |
1165 | /* Make KEYMAP define event C as a keymap (i.e., as a prefix). |
1166 | Assume that currently it does not define C at all. | |
1167 | Return the keymap. */ | |
1168 | ||
1169 | static Lisp_Object | |
1170 | define_as_prefix (keymap, c) | |
1171 | Lisp_Object keymap, c; | |
1172 | { | |
db785038 | 1173 | Lisp_Object cmd; |
c07aec97 RS |
1174 | |
1175 | cmd = Fmake_sparse_keymap (Qnil); | |
1176 | /* If this key is defined as a prefix in an inherited keymap, | |
1177 | make it a prefix in this map, and make its definition | |
1178 | inherit the other prefix definition. */ | |
db785038 | 1179 | cmd = nconc2 (cmd, access_keymap (keymap, c, 0, 0, 0)); |
c07aec97 RS |
1180 | store_in_keymap (keymap, c, cmd); |
1181 | ||
1182 | return cmd; | |
1183 | } | |
1184 | ||
0b8fc2d4 RS |
1185 | /* Append a key to the end of a key sequence. We always make a vector. */ |
1186 | ||
2c6f1a39 JB |
1187 | Lisp_Object |
1188 | append_key (key_sequence, key) | |
1189 | Lisp_Object key_sequence, key; | |
1190 | { | |
1191 | Lisp_Object args[2]; | |
1192 | ||
1193 | args[0] = key_sequence; | |
1194 | ||
0b8fc2d4 RS |
1195 | args[1] = Fcons (key, Qnil); |
1196 | return Fvconcat (2, args); | |
2c6f1a39 JB |
1197 | } |
1198 | ||
15fff01d RS |
1199 | /* Given a event type C which is a symbol, |
1200 | signal an error if is a mistake such as RET or M-RET or C-DEL, etc. */ | |
1201 | ||
1202 | static void | |
1203 | silly_event_symbol_error (c) | |
1204 | Lisp_Object c; | |
1205 | { | |
1206 | Lisp_Object parsed, base, name, assoc; | |
1207 | int modifiers; | |
1208 | ||
1209 | parsed = parse_modifiers (c); | |
1210 | modifiers = (int) XUINT (XCAR (XCDR (parsed))); | |
1211 | base = XCAR (parsed); | |
1212 | name = Fsymbol_name (base); | |
1213 | /* This alist includes elements such as ("RET" . "\\r"). */ | |
1214 | assoc = Fassoc (name, exclude_keys); | |
1215 | ||
1216 | if (! NILP (assoc)) | |
1217 | { | |
1218 | char new_mods[sizeof ("\\A-\\C-\\H-\\M-\\S-\\s-")]; | |
1219 | char *p = new_mods; | |
1220 | Lisp_Object keystring; | |
1221 | if (modifiers & alt_modifier) | |
1222 | { *p++ = '\\'; *p++ = 'A'; *p++ = '-'; } | |
1223 | if (modifiers & ctrl_modifier) | |
1224 | { *p++ = '\\'; *p++ = 'C'; *p++ = '-'; } | |
1225 | if (modifiers & hyper_modifier) | |
1226 | { *p++ = '\\'; *p++ = 'H'; *p++ = '-'; } | |
1227 | if (modifiers & meta_modifier) | |
1228 | { *p++ = '\\'; *p++ = 'M'; *p++ = '-'; } | |
1229 | if (modifiers & shift_modifier) | |
1230 | { *p++ = '\\'; *p++ = 'S'; *p++ = '-'; } | |
1231 | if (modifiers & super_modifier) | |
1232 | { *p++ = '\\'; *p++ = 's'; *p++ = '-'; } | |
1233 | *p = 0; | |
1234 | ||
1235 | c = reorder_modifiers (c); | |
1236 | keystring = concat2 (build_string (new_mods), XCDR (assoc)); | |
1237 | ||
1238 | error ((modifiers & ~meta_modifier | |
1239 | ? "To bind the key %s, use [?%s], not [%s]" | |
1240 | : "To bind the key %s, use \"%s\", not [%s]"), | |
1241 | XSYMBOL (c)->name->data, XSTRING (keystring)->data, | |
1242 | XSYMBOL (c)->name->data); | |
1243 | } | |
1244 | } | |
2c6f1a39 | 1245 | \f |
cc0a8174 JB |
1246 | /* Global, local, and minor mode keymap stuff. */ |
1247 | ||
265a9e55 | 1248 | /* We can't put these variables inside current_minor_maps, since under |
6bbbd9b0 JB |
1249 | some systems, static gets macro-defined to be the empty string. |
1250 | Ickypoo. */ | |
cd6db61a KS |
1251 | static Lisp_Object *cmm_modes = NULL, *cmm_maps = NULL; |
1252 | static int cmm_size = 0; | |
265a9e55 | 1253 | |
fbb90829 KH |
1254 | /* Error handler used in current_minor_maps. */ |
1255 | static Lisp_Object | |
1256 | current_minor_maps_error () | |
1257 | { | |
1258 | return Qnil; | |
1259 | } | |
1260 | ||
cc0a8174 JB |
1261 | /* Store a pointer to an array of the keymaps of the currently active |
1262 | minor modes in *buf, and return the number of maps it contains. | |
1263 | ||
1264 | This function always returns a pointer to the same buffer, and may | |
1265 | free or reallocate it, so if you want to keep it for a long time or | |
1266 | hand it out to lisp code, copy it. This procedure will be called | |
1267 | for every key sequence read, so the nice lispy approach (return a | |
1268 | new assoclist, list, what have you) for each invocation would | |
1269 | result in a lot of consing over time. | |
1270 | ||
1271 | If we used xrealloc/xmalloc and ran out of memory, they would throw | |
1272 | back to the command loop, which would try to read a key sequence, | |
1273 | which would call this function again, resulting in an infinite | |
1274 | loop. Instead, we'll use realloc/malloc and silently truncate the | |
1275 | list, let the key sequence be read, and hope some other piece of | |
1276 | code signals the error. */ | |
1277 | int | |
1278 | current_minor_maps (modeptr, mapptr) | |
1279 | Lisp_Object **modeptr, **mapptr; | |
1280 | { | |
cc0a8174 | 1281 | int i = 0; |
dd9cda06 | 1282 | int list_number = 0; |
6bbbd9b0 | 1283 | Lisp_Object alist, assoc, var, val; |
99cbcaca | 1284 | Lisp_Object emulation_alists; |
dd9cda06 RS |
1285 | Lisp_Object lists[2]; |
1286 | ||
99cbcaca | 1287 | emulation_alists = Vemulation_mode_map_alists; |
dd9cda06 RS |
1288 | lists[0] = Vminor_mode_overriding_map_alist; |
1289 | lists[1] = Vminor_mode_map_alist; | |
1290 | ||
1291 | for (list_number = 0; list_number < 2; list_number++) | |
99cbcaca KS |
1292 | { |
1293 | if (CONSP (emulation_alists)) | |
dd9cda06 | 1294 | { |
99cbcaca KS |
1295 | alist = XCAR (emulation_alists); |
1296 | emulation_alists = XCDR (emulation_alists); | |
1297 | if (SYMBOLP (alist)) | |
1298 | alist = find_symbol_value (alist); | |
1299 | list_number = -1; | |
1300 | } | |
1301 | else | |
1302 | alist = lists[list_number]; | |
cc0a8174 | 1303 | |
99cbcaca KS |
1304 | for ( ; CONSP (alist); alist = XCDR (alist)) |
1305 | if ((assoc = XCAR (alist), CONSP (assoc)) | |
1306 | && (var = XCAR (assoc), SYMBOLP (var)) | |
1307 | && (val = find_symbol_value (var), !EQ (val, Qunbound)) | |
1308 | && !NILP (val)) | |
1309 | { | |
1310 | Lisp_Object temp; | |
64dd3629 | 1311 | |
99cbcaca KS |
1312 | /* If a variable has an entry in Vminor_mode_overriding_map_alist, |
1313 | and also an entry in Vminor_mode_map_alist, | |
1314 | ignore the latter. */ | |
1315 | if (list_number == 1) | |
1316 | { | |
1317 | val = assq_no_quit (var, lists[0]); | |
1318 | if (!NILP (val)) | |
1319 | continue; | |
1320 | } | |
cc0a8174 | 1321 | |
99cbcaca KS |
1322 | if (i >= cmm_size) |
1323 | { | |
cd6db61a | 1324 | int newsize, allocsize; |
99cbcaca | 1325 | Lisp_Object *newmodes, *newmaps; |
cc0a8174 | 1326 | |
cd6db61a KS |
1327 | newsize = cmm_size == 0 ? 30 : cmm_size * 2; |
1328 | allocsize = newsize * sizeof *newmodes; | |
1329 | ||
1330 | /* Use malloc here. See the comment above this function. | |
1331 | Avoid realloc here; it causes spurious traps on GNU/Linux [KFS] */ | |
1332 | BLOCK_INPUT; | |
1333 | newmodes = (Lisp_Object *) malloc (allocsize); | |
1334 | if (newmodes) | |
99cbcaca | 1335 | { |
cd6db61a KS |
1336 | if (cmm_modes) |
1337 | { | |
1338 | bcopy (cmm_modes, newmodes, cmm_size * sizeof cmm_modes[0]); | |
1339 | free (cmm_modes); | |
1340 | } | |
1341 | cmm_modes = newmodes; | |
99cbcaca KS |
1342 | } |
1343 | ||
cd6db61a | 1344 | newmaps = (Lisp_Object *) malloc (allocsize); |
99cbcaca | 1345 | if (newmaps) |
cd6db61a KS |
1346 | { |
1347 | if (cmm_maps) | |
1348 | { | |
1349 | bcopy (cmm_maps, newmaps, cmm_size * sizeof cmm_maps[0]); | |
1350 | free (cmm_maps); | |
1351 | } | |
1352 | cmm_maps = newmaps; | |
1353 | } | |
1354 | UNBLOCK_INPUT; | |
ee89188f | 1355 | |
99cbcaca KS |
1356 | if (newmodes == NULL || newmaps == NULL) |
1357 | break; | |
cd6db61a | 1358 | cmm_size = newsize; |
99cbcaca | 1359 | } |
fbb90829 | 1360 | |
99cbcaca KS |
1361 | /* Get the keymap definition--or nil if it is not defined. */ |
1362 | temp = internal_condition_case_1 (Findirect_function, | |
1363 | XCDR (assoc), | |
1364 | Qerror, current_minor_maps_error); | |
1365 | if (!NILP (temp)) | |
1366 | { | |
1367 | cmm_modes[i] = var; | |
1368 | cmm_maps [i] = temp; | |
1369 | i++; | |
1370 | } | |
1371 | } | |
1372 | } | |
cc0a8174 | 1373 | |
265a9e55 JB |
1374 | if (modeptr) *modeptr = cmm_modes; |
1375 | if (mapptr) *mapptr = cmm_maps; | |
cc0a8174 JB |
1376 | return i; |
1377 | } | |
1378 | ||
54cbc3d4 | 1379 | DEFUN ("current-active-maps", Fcurrent_active_maps, Scurrent_active_maps, |
335c5470 PJ |
1380 | 0, 1, 0, |
1381 | doc: /* Return a list of the currently active keymaps. | |
1382 | OLP if non-nil indicates that we should obey `overriding-local-map' and | |
1383 | `overriding-terminal-local-map'. */) | |
54cbc3d4 SM |
1384 | (olp) |
1385 | Lisp_Object olp; | |
1386 | { | |
1387 | Lisp_Object keymaps = Fcons (current_global_map, Qnil); | |
1388 | ||
1389 | if (!NILP (olp)) | |
1390 | { | |
1391 | if (!NILP (Voverriding_local_map)) | |
1392 | keymaps = Fcons (Voverriding_local_map, keymaps); | |
1393 | if (!NILP (current_kboard->Voverriding_terminal_local_map)) | |
1394 | keymaps = Fcons (current_kboard->Voverriding_terminal_local_map, keymaps); | |
1395 | } | |
1396 | if (NILP (XCDR (keymaps))) | |
1397 | { | |
1398 | Lisp_Object local; | |
1399 | Lisp_Object *maps; | |
1400 | int nmaps, i; | |
1401 | ||
1402 | local = get_local_map (PT, current_buffer, Qlocal_map); | |
1403 | if (!NILP (local)) | |
1404 | keymaps = Fcons (local, keymaps); | |
1405 | ||
54cbc3d4 SM |
1406 | nmaps = current_minor_maps (0, &maps); |
1407 | ||
1408 | for (i = --nmaps; i >= 0; i--) | |
1409 | if (!NILP (maps[i])) | |
1410 | keymaps = Fcons (maps[i], keymaps); | |
d1d070e3 RS |
1411 | |
1412 | local = get_local_map (PT, current_buffer, Qkeymap); | |
1413 | if (!NILP (local)) | |
1414 | keymaps = Fcons (local, keymaps); | |
54cbc3d4 SM |
1415 | } |
1416 | ||
1417 | return keymaps; | |
1418 | } | |
1419 | ||
21a0d7a0 RS |
1420 | /* GC is possible in this function if it autoloads a keymap. */ |
1421 | ||
0c412762 | 1422 | DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 3, 0, |
335c5470 PJ |
1423 | doc: /* Return the binding for command KEY in current keymaps. |
1424 | KEY is a string or vector, a sequence of keystrokes. | |
1425 | The binding is probably a symbol with a function definition. | |
1426 | ||
1427 | Normally, `key-binding' ignores bindings for t, which act as default | |
1428 | bindings, used when nothing else in the keymap applies; this makes it | |
1429 | usable as a general function for probing keymaps. However, if the | |
1430 | optional second argument ACCEPT-DEFAULT is non-nil, `key-binding' does | |
0c412762 KS |
1431 | recognize the default bindings, just as `read-key-sequence' does. |
1432 | ||
1433 | Like the normal command loop, `key-binding' will remap the command | |
1434 | resulting from looking up KEY by looking up the command in the | |
1435 | currrent keymaps. However, if the optional third argument NO-REMAP | |
1436 | is non-nil, `key-binding' returns the unmapped command. */) | |
1437 | (key, accept_default, no_remap) | |
1438 | Lisp_Object key, accept_default, no_remap; | |
2c6f1a39 | 1439 | { |
cc0a8174 JB |
1440 | Lisp_Object *maps, value; |
1441 | int nmaps, i; | |
21a0d7a0 RS |
1442 | struct gcpro gcpro1; |
1443 | ||
1444 | GCPRO1 (key); | |
cc0a8174 | 1445 | |
e784236d KH |
1446 | if (!NILP (current_kboard->Voverriding_terminal_local_map)) |
1447 | { | |
1448 | value = Flookup_key (current_kboard->Voverriding_terminal_local_map, | |
1449 | key, accept_default); | |
1450 | if (! NILP (value) && !INTEGERP (value)) | |
0c412762 | 1451 | goto done; |
e784236d KH |
1452 | } |
1453 | else if (!NILP (Voverriding_local_map)) | |
2c6f1a39 | 1454 | { |
7d92e329 | 1455 | value = Flookup_key (Voverriding_local_map, key, accept_default); |
416349ec | 1456 | if (! NILP (value) && !INTEGERP (value)) |
0c412762 | 1457 | goto done; |
2c6f1a39 | 1458 | } |
7d92e329 RS |
1459 | else |
1460 | { | |
d964248c KH |
1461 | Lisp_Object local; |
1462 | ||
d1d070e3 RS |
1463 | local = get_local_map (PT, current_buffer, Qkeymap); |
1464 | if (! NILP (local)) | |
1465 | { | |
1466 | value = Flookup_key (local, key, accept_default); | |
1467 | if (! NILP (value) && !INTEGERP (value)) | |
0c412762 | 1468 | goto done; |
d1d070e3 RS |
1469 | } |
1470 | ||
7d92e329 | 1471 | nmaps = current_minor_maps (0, &maps); |
21a0d7a0 RS |
1472 | /* Note that all these maps are GCPRO'd |
1473 | in the places where we found them. */ | |
1474 | ||
7d92e329 RS |
1475 | for (i = 0; i < nmaps; i++) |
1476 | if (! NILP (maps[i])) | |
1477 | { | |
1478 | value = Flookup_key (maps[i], key, accept_default); | |
416349ec | 1479 | if (! NILP (value) && !INTEGERP (value)) |
0c412762 | 1480 | goto done; |
7d92e329 RS |
1481 | } |
1482 | ||
4956d1ef | 1483 | local = get_local_map (PT, current_buffer, Qlocal_map); |
d964248c | 1484 | if (! NILP (local)) |
7d92e329 | 1485 | { |
d964248c | 1486 | value = Flookup_key (local, key, accept_default); |
416349ec | 1487 | if (! NILP (value) && !INTEGERP (value)) |
0c412762 | 1488 | goto done; |
7d92e329 RS |
1489 | } |
1490 | } | |
cc0a8174 | 1491 | |
7c140252 | 1492 | value = Flookup_key (current_global_map, key, accept_default); |
0c412762 KS |
1493 | |
1494 | done: | |
21a0d7a0 | 1495 | UNGCPRO; |
0c412762 KS |
1496 | if (NILP (value) || INTEGERP (value)) |
1497 | return Qnil; | |
1498 | ||
1499 | /* If the result of the ordinary keymap lookup is an interactive | |
1500 | command, look for a key binding (ie. remapping) for that command. */ | |
1501 | ||
a1df473f | 1502 | if (NILP (no_remap) && SYMBOLP (value)) |
0c412762 KS |
1503 | { |
1504 | Lisp_Object value1; | |
a1df473f | 1505 | if (value1 = Fremap_command (value), !NILP (value1)) |
0c412762 KS |
1506 | value = value1; |
1507 | } | |
cc0a8174 | 1508 | |
0c412762 | 1509 | return value; |
2c6f1a39 JB |
1510 | } |
1511 | ||
21a0d7a0 RS |
1512 | /* GC is possible in this function if it autoloads a keymap. */ |
1513 | ||
7c140252 | 1514 | DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 2, 0, |
335c5470 PJ |
1515 | doc: /* Return the binding for command KEYS in current local keymap only. |
1516 | KEYS is a string, a sequence of keystrokes. | |
1517 | The binding is probably a symbol with a function definition. | |
1518 | ||
1519 | If optional argument ACCEPT-DEFAULT is non-nil, recognize default | |
1520 | bindings; see the description of `lookup-key' for more details about this. */) | |
1521 | (keys, accept_default) | |
7c140252 | 1522 | Lisp_Object keys, accept_default; |
2c6f1a39 JB |
1523 | { |
1524 | register Lisp_Object map; | |
1525 | map = current_buffer->keymap; | |
265a9e55 | 1526 | if (NILP (map)) |
2c6f1a39 | 1527 | return Qnil; |
7c140252 | 1528 | return Flookup_key (map, keys, accept_default); |
2c6f1a39 JB |
1529 | } |
1530 | ||
21a0d7a0 RS |
1531 | /* GC is possible in this function if it autoloads a keymap. */ |
1532 | ||
7c140252 | 1533 | DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 2, 0, |
335c5470 PJ |
1534 | doc: /* Return the binding for command KEYS in current global keymap only. |
1535 | KEYS is a string, a sequence of keystrokes. | |
1536 | The binding is probably a symbol with a function definition. | |
1537 | This function's return values are the same as those of lookup-key | |
1538 | \(which see). | |
1539 | ||
1540 | If optional argument ACCEPT-DEFAULT is non-nil, recognize default | |
1541 | bindings; see the description of `lookup-key' for more details about this. */) | |
1542 | (keys, accept_default) | |
7c140252 | 1543 | Lisp_Object keys, accept_default; |
2c6f1a39 | 1544 | { |
7c140252 | 1545 | return Flookup_key (current_global_map, keys, accept_default); |
2c6f1a39 JB |
1546 | } |
1547 | ||
21a0d7a0 RS |
1548 | /* GC is possible in this function if it autoloads a keymap. */ |
1549 | ||
7c140252 | 1550 | DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0, |
335c5470 PJ |
1551 | doc: /* Find the visible minor mode bindings of KEY. |
1552 | Return an alist of pairs (MODENAME . BINDING), where MODENAME is the | |
1553 | the symbol which names the minor mode binding KEY, and BINDING is | |
1554 | KEY's definition in that mode. In particular, if KEY has no | |
1555 | minor-mode bindings, return nil. If the first binding is a | |
1556 | non-prefix, all subsequent bindings will be omitted, since they would | |
1557 | be ignored. Similarly, the list doesn't include non-prefix bindings | |
1558 | that come after prefix bindings. | |
1559 | ||
1560 | If optional argument ACCEPT-DEFAULT is non-nil, recognize default | |
1561 | bindings; see the description of `lookup-key' for more details about this. */) | |
1562 | (key, accept_default) | |
7c140252 | 1563 | Lisp_Object key, accept_default; |
cc0a8174 JB |
1564 | { |
1565 | Lisp_Object *modes, *maps; | |
1566 | int nmaps; | |
1567 | Lisp_Object binding; | |
1568 | int i, j; | |
21a0d7a0 | 1569 | struct gcpro gcpro1, gcpro2; |
cc0a8174 JB |
1570 | |
1571 | nmaps = current_minor_maps (&modes, &maps); | |
21a0d7a0 RS |
1572 | /* Note that all these maps are GCPRO'd |
1573 | in the places where we found them. */ | |
1574 | ||
1575 | binding = Qnil; | |
1576 | GCPRO2 (key, binding); | |
cc0a8174 JB |
1577 | |
1578 | for (i = j = 0; i < nmaps; i++) | |
02067692 SM |
1579 | if (!NILP (maps[i]) |
1580 | && !NILP (binding = Flookup_key (maps[i], key, accept_default)) | |
416349ec | 1581 | && !INTEGERP (binding)) |
cc0a8174 | 1582 | { |
02067692 | 1583 | if (KEYMAPP (binding)) |
cc0a8174 JB |
1584 | maps[j++] = Fcons (modes[i], binding); |
1585 | else if (j == 0) | |
21a0d7a0 | 1586 | RETURN_UNGCPRO (Fcons (Fcons (modes[i], binding), Qnil)); |
cc0a8174 JB |
1587 | } |
1588 | ||
21a0d7a0 | 1589 | UNGCPRO; |
cc0a8174 JB |
1590 | return Flist (j, maps); |
1591 | } | |
1592 | ||
7f8f0e67 | 1593 | DEFUN ("define-prefix-command", Fdefine_prefix_command, Sdefine_prefix_command, 1, 3, 0, |
335c5470 PJ |
1594 | doc: /* Define COMMAND as a prefix command. COMMAND should be a symbol. |
1595 | A new sparse keymap is stored as COMMAND's function definition and its value. | |
1596 | If a second optional argument MAPVAR is given, the map is stored as | |
1597 | its value instead of as COMMAND's value; but COMMAND is still defined | |
1598 | as a function. | |
1599 | The third optional argument NAME, if given, supplies a menu name | |
1600 | string for the map. This is required to use the keymap as a menu. */) | |
1601 | (command, mapvar, name) | |
7f8f0e67 | 1602 | Lisp_Object command, mapvar, name; |
2c6f1a39 JB |
1603 | { |
1604 | Lisp_Object map; | |
7f8f0e67 | 1605 | map = Fmake_sparse_keymap (name); |
88539837 | 1606 | Ffset (command, map); |
265a9e55 | 1607 | if (!NILP (mapvar)) |
2c6f1a39 JB |
1608 | Fset (mapvar, map); |
1609 | else | |
88539837 EN |
1610 | Fset (command, map); |
1611 | return command; | |
2c6f1a39 JB |
1612 | } |
1613 | ||
1614 | DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0, | |
335c5470 PJ |
1615 | doc: /* Select KEYMAP as the global keymap. */) |
1616 | (keymap) | |
2c6f1a39 JB |
1617 | Lisp_Object keymap; |
1618 | { | |
02067692 | 1619 | keymap = get_keymap (keymap, 1, 1); |
2c6f1a39 | 1620 | current_global_map = keymap; |
6f27e7a2 | 1621 | |
2c6f1a39 JB |
1622 | return Qnil; |
1623 | } | |
1624 | ||
1625 | DEFUN ("use-local-map", Fuse_local_map, Suse_local_map, 1, 1, 0, | |
335c5470 PJ |
1626 | doc: /* Select KEYMAP as the local keymap. |
1627 | If KEYMAP is nil, that means no local keymap. */) | |
1628 | (keymap) | |
2c6f1a39 JB |
1629 | Lisp_Object keymap; |
1630 | { | |
265a9e55 | 1631 | if (!NILP (keymap)) |
02067692 | 1632 | keymap = get_keymap (keymap, 1, 1); |
2c6f1a39 JB |
1633 | |
1634 | current_buffer->keymap = keymap; | |
1635 | ||
1636 | return Qnil; | |
1637 | } | |
1638 | ||
1639 | DEFUN ("current-local-map", Fcurrent_local_map, Scurrent_local_map, 0, 0, 0, | |
335c5470 PJ |
1640 | doc: /* Return current buffer's local keymap, or nil if it has none. */) |
1641 | () | |
2c6f1a39 JB |
1642 | { |
1643 | return current_buffer->keymap; | |
1644 | } | |
1645 | ||
1646 | DEFUN ("current-global-map", Fcurrent_global_map, Scurrent_global_map, 0, 0, 0, | |
335c5470 PJ |
1647 | doc: /* Return the current global keymap. */) |
1648 | () | |
2c6f1a39 JB |
1649 | { |
1650 | return current_global_map; | |
1651 | } | |
cc0a8174 JB |
1652 | |
1653 | DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps, Scurrent_minor_mode_maps, 0, 0, 0, | |
335c5470 PJ |
1654 | doc: /* Return a list of keymaps for the minor modes of the current buffer. */) |
1655 | () | |
cc0a8174 JB |
1656 | { |
1657 | Lisp_Object *maps; | |
1658 | int nmaps = current_minor_maps (0, &maps); | |
1659 | ||
1660 | return Flist (nmaps, maps); | |
1661 | } | |
2c6f1a39 | 1662 | \f |
cc0a8174 JB |
1663 | /* Help functions for describing and documenting keymaps. */ |
1664 | ||
54cbc3d4 SM |
1665 | |
1666 | static void | |
1667 | accessible_keymaps_1 (key, cmd, maps, tail, thisseq, is_metized) | |
1668 | Lisp_Object maps, tail, thisseq, key, cmd; | |
1669 | int is_metized; /* If 1, `key' is assumed to be INTEGERP. */ | |
1670 | { | |
1671 | Lisp_Object tem; | |
1672 | ||
1673 | cmd = get_keyelt (cmd, 0); | |
1674 | if (NILP (cmd)) | |
1675 | return; | |
1676 | ||
1677 | tem = get_keymap (cmd, 0, 0); | |
1678 | if (CONSP (tem)) | |
1679 | { | |
1680 | cmd = tem; | |
1681 | /* Ignore keymaps that are already added to maps. */ | |
1682 | tem = Frassq (cmd, maps); | |
1683 | if (NILP (tem)) | |
1684 | { | |
1685 | /* If the last key in thisseq is meta-prefix-char, | |
1686 | turn it into a meta-ized keystroke. We know | |
1687 | that the event we're about to append is an | |
1688 | ascii keystroke since we're processing a | |
1689 | keymap table. */ | |
1690 | if (is_metized) | |
1691 | { | |
1692 | int meta_bit = meta_modifier; | |
1693 | Lisp_Object last = make_number (XINT (Flength (thisseq)) - 1); | |
1694 | tem = Fcopy_sequence (thisseq); | |
1695 | ||
1696 | Faset (tem, last, make_number (XINT (key) | meta_bit)); | |
1697 | ||
1698 | /* This new sequence is the same length as | |
1699 | thisseq, so stick it in the list right | |
1700 | after this one. */ | |
f3fbd155 KR |
1701 | XSETCDR (tail, |
1702 | Fcons (Fcons (tem, cmd), XCDR (tail))); | |
54cbc3d4 SM |
1703 | } |
1704 | else | |
1705 | { | |
1706 | tem = append_key (thisseq, key); | |
1707 | nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil)); | |
1708 | } | |
1709 | } | |
1710 | } | |
1711 | } | |
1712 | ||
1713 | static void | |
1714 | accessible_keymaps_char_table (args, index, cmd) | |
1715 | Lisp_Object args, index, cmd; | |
1716 | { | |
1717 | accessible_keymaps_1 (index, cmd, | |
1718 | XCAR (XCAR (args)), | |
1719 | XCAR (XCDR (args)), | |
1720 | XCDR (XCDR (args)), | |
1721 | XINT (XCDR (XCAR (args)))); | |
1722 | } | |
0403641f | 1723 | |
21a0d7a0 RS |
1724 | /* This function cannot GC. */ |
1725 | ||
2c6f1a39 | 1726 | DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps, |
335c5470 PJ |
1727 | 1, 2, 0, |
1728 | doc: /* Find all keymaps accessible via prefix characters from KEYMAP. | |
1729 | Returns a list of elements of the form (KEYS . MAP), where the sequence | |
1730 | KEYS starting from KEYMAP gets you to MAP. These elements are ordered | |
1731 | so that the KEYS increase in length. The first element is ([] . KEYMAP). | |
1732 | An optional argument PREFIX, if non-nil, should be a key sequence; | |
1733 | then the value includes only maps for prefixes that start with PREFIX. */) | |
1734 | (keymap, prefix) | |
88539837 | 1735 | Lisp_Object keymap, prefix; |
2c6f1a39 | 1736 | { |
53c8f9fa RS |
1737 | Lisp_Object maps, good_maps, tail; |
1738 | int prefixlen = 0; | |
1739 | ||
21a0d7a0 RS |
1740 | /* no need for gcpro because we don't autoload any keymaps. */ |
1741 | ||
53c8f9fa RS |
1742 | if (!NILP (prefix)) |
1743 | prefixlen = XINT (Flength (prefix)); | |
2c6f1a39 | 1744 | |
44a4a59b RS |
1745 | if (!NILP (prefix)) |
1746 | { | |
1747 | /* If a prefix was specified, start with the keymap (if any) for | |
1748 | that prefix, so we don't waste time considering other prefixes. */ | |
1749 | Lisp_Object tem; | |
88539837 | 1750 | tem = Flookup_key (keymap, prefix, Qt); |
1ae2097f RS |
1751 | /* Flookup_key may give us nil, or a number, |
1752 | if the prefix is not defined in this particular map. | |
1753 | It might even give us a list that isn't a keymap. */ | |
02067692 SM |
1754 | tem = get_keymap (tem, 0, 0); |
1755 | if (CONSP (tem)) | |
67fc16a3 RS |
1756 | { |
1757 | /* Convert PREFIX to a vector now, so that later on | |
1758 | we don't have to deal with the possibility of a string. */ | |
1759 | if (STRINGP (prefix)) | |
1760 | { | |
f3ba5409 | 1761 | int i, i_byte, c; |
67fc16a3 RS |
1762 | Lisp_Object copy; |
1763 | ||
1764 | copy = Fmake_vector (make_number (XSTRING (prefix)->size), Qnil); | |
b91f7a6f | 1765 | for (i = 0, i_byte = 0; i < XSTRING (prefix)->size;) |
67fc16a3 | 1766 | { |
f3ba5409 | 1767 | int i_before = i; |
54e03a4a KH |
1768 | |
1769 | FETCH_STRING_CHAR_ADVANCE (c, prefix, i, i_byte); | |
1770 | if (SINGLE_BYTE_CHAR_P (c) && (c & 0200)) | |
1771 | c ^= 0200 | meta_modifier; | |
49801145 | 1772 | ASET (copy, i_before, make_number (c)); |
67fc16a3 RS |
1773 | } |
1774 | prefix = copy; | |
1775 | } | |
1776 | maps = Fcons (Fcons (prefix, tem), Qnil); | |
1777 | } | |
44a4a59b RS |
1778 | else |
1779 | return Qnil; | |
1780 | } | |
1781 | else | |
1782 | maps = Fcons (Fcons (Fmake_vector (make_number (0), Qnil), | |
02067692 | 1783 | get_keymap (keymap, 1, 0)), |
44a4a59b | 1784 | Qnil); |
2c6f1a39 JB |
1785 | |
1786 | /* For each map in the list maps, | |
1787 | look at any other maps it points to, | |
1788 | and stick them at the end if they are not already in the list. | |
1789 | ||
1790 | This is a breadth-first traversal, where tail is the queue of | |
1791 | nodes, and maps accumulates a list of all nodes visited. */ | |
1792 | ||
03699b14 | 1793 | for (tail = maps; CONSP (tail); tail = XCDR (tail)) |
2c6f1a39 | 1794 | { |
e9b6dfb0 KH |
1795 | register Lisp_Object thisseq, thismap; |
1796 | Lisp_Object last; | |
2c6f1a39 | 1797 | /* Does the current sequence end in the meta-prefix-char? */ |
e9b6dfb0 KH |
1798 | int is_metized; |
1799 | ||
1800 | thisseq = Fcar (Fcar (tail)); | |
1801 | thismap = Fcdr (Fcar (tail)); | |
1802 | last = make_number (XINT (Flength (thisseq)) - 1); | |
1803 | is_metized = (XINT (last) >= 0 | |
97ae4b89 RS |
1804 | /* Don't metize the last char of PREFIX. */ |
1805 | && XINT (last) >= prefixlen | |
e9b6dfb0 | 1806 | && EQ (Faref (thisseq, last), meta_prefix_char)); |
2c6f1a39 | 1807 | |
03699b14 | 1808 | for (; CONSP (thismap); thismap = XCDR (thismap)) |
2c6f1a39 | 1809 | { |
e9b6dfb0 KH |
1810 | Lisp_Object elt; |
1811 | ||
03699b14 | 1812 | elt = XCAR (thismap); |
2c6f1a39 | 1813 | |
f5b79c1c JB |
1814 | QUIT; |
1815 | ||
0403641f RS |
1816 | if (CHAR_TABLE_P (elt)) |
1817 | { | |
23cf1efa | 1818 | Lisp_Object indices[3]; |
0403641f RS |
1819 | |
1820 | map_char_table (accessible_keymaps_char_table, Qnil, | |
fc18e547 | 1821 | elt, Fcons (Fcons (maps, make_number (is_metized)), |
f58c6494 | 1822 | Fcons (tail, thisseq)), |
0403641f RS |
1823 | 0, indices); |
1824 | } | |
1825 | else if (VECTORP (elt)) | |
2c6f1a39 JB |
1826 | { |
1827 | register int i; | |
1828 | ||
1829 | /* Vector keymap. Scan all the elements. */ | |
49801145 | 1830 | for (i = 0; i < ASIZE (elt); i++) |
54cbc3d4 SM |
1831 | accessible_keymaps_1 (make_number (i), AREF (elt, i), |
1832 | maps, tail, thisseq, is_metized); | |
1833 | ||
0403641f | 1834 | } |
f5b79c1c | 1835 | else if (CONSP (elt)) |
54cbc3d4 SM |
1836 | accessible_keymaps_1 (XCAR (elt), XCDR (elt), |
1837 | maps, tail, thisseq, | |
1838 | is_metized && INTEGERP (XCAR (elt))); | |
1839 | ||
2c6f1a39 | 1840 | } |
2c6f1a39 JB |
1841 | } |
1842 | ||
53c8f9fa RS |
1843 | if (NILP (prefix)) |
1844 | return maps; | |
1845 | ||
1846 | /* Now find just the maps whose access prefixes start with PREFIX. */ | |
1847 | ||
1848 | good_maps = Qnil; | |
03699b14 | 1849 | for (; CONSP (maps); maps = XCDR (maps)) |
53c8f9fa RS |
1850 | { |
1851 | Lisp_Object elt, thisseq; | |
03699b14 KR |
1852 | elt = XCAR (maps); |
1853 | thisseq = XCAR (elt); | |
53c8f9fa RS |
1854 | /* The access prefix must be at least as long as PREFIX, |
1855 | and the first elements must match those of PREFIX. */ | |
1856 | if (XINT (Flength (thisseq)) >= prefixlen) | |
1857 | { | |
1858 | int i; | |
1859 | for (i = 0; i < prefixlen; i++) | |
1860 | { | |
1861 | Lisp_Object i1; | |
6e344130 | 1862 | XSETFASTINT (i1, i); |
53c8f9fa RS |
1863 | if (!EQ (Faref (thisseq, i1), Faref (prefix, i1))) |
1864 | break; | |
1865 | } | |
1866 | if (i == prefixlen) | |
1867 | good_maps = Fcons (elt, good_maps); | |
1868 | } | |
1869 | } | |
1870 | ||
1871 | return Fnreverse (good_maps); | |
2c6f1a39 | 1872 | } |
0403641f | 1873 | \f |
2c6f1a39 JB |
1874 | Lisp_Object Qsingle_key_description, Qkey_description; |
1875 | ||
21a0d7a0 RS |
1876 | /* This function cannot GC. */ |
1877 | ||
2c6f1a39 | 1878 | DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0, |
335c5470 PJ |
1879 | doc: /* Return a pretty description of key-sequence KEYS. |
1880 | Control characters turn into "C-foo" sequences, meta into "M-foo" | |
1881 | spaces are put between sequence elements, etc. */) | |
1882 | (keys) | |
2c6f1a39 JB |
1883 | Lisp_Object keys; |
1884 | { | |
6bbd7a29 | 1885 | int len = 0; |
f3ba5409 | 1886 | int i, i_byte; |
4c7d5f13 | 1887 | Lisp_Object sep; |
6bbd7a29 | 1888 | Lisp_Object *args = NULL; |
4c7d5f13 | 1889 | |
47684cd9 | 1890 | if (STRINGP (keys)) |
6ba6e250 RS |
1891 | { |
1892 | Lisp_Object vector; | |
6ba6e250 | 1893 | vector = Fmake_vector (Flength (keys), Qnil); |
b91f7a6f | 1894 | for (i = 0, i_byte = 0; i < XSTRING (keys)->size; ) |
6ba6e250 | 1895 | { |
f3ba5409 | 1896 | int c; |
28246d85 | 1897 | int i_before = i; |
f3ba5409 | 1898 | |
54e03a4a KH |
1899 | FETCH_STRING_CHAR_ADVANCE (c, keys, i, i_byte); |
1900 | if (SINGLE_BYTE_CHAR_P (c) && (c & 0200)) | |
1901 | c ^= 0200 | meta_modifier; | |
49801145 | 1902 | XSETFASTINT (AREF (vector, i_before), c); |
6ba6e250 RS |
1903 | } |
1904 | keys = vector; | |
1905 | } | |
4c7d5f13 | 1906 | |
5c9c2c3f RS |
1907 | if (VECTORP (keys)) |
1908 | { | |
1909 | /* In effect, this computes | |
1910 | (mapconcat 'single-key-description keys " ") | |
1911 | but we shouldn't use mapconcat because it can do GC. */ | |
4c7d5f13 | 1912 | |
5c9c2c3f RS |
1913 | len = XVECTOR (keys)->size; |
1914 | sep = build_string (" "); | |
1915 | /* This has one extra element at the end that we don't pass to Fconcat. */ | |
1916 | args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object)); | |
4c7d5f13 | 1917 | |
5c9c2c3f RS |
1918 | for (i = 0; i < len; i++) |
1919 | { | |
49801145 | 1920 | args[i * 2] = Fsingle_key_description (AREF (keys, i), Qnil); |
5c9c2c3f RS |
1921 | args[i * 2 + 1] = sep; |
1922 | } | |
1923 | } | |
1924 | else if (CONSP (keys)) | |
4c7d5f13 | 1925 | { |
5c9c2c3f RS |
1926 | /* In effect, this computes |
1927 | (mapconcat 'single-key-description keys " ") | |
1928 | but we shouldn't use mapconcat because it can do GC. */ | |
1929 | ||
1930 | len = XFASTINT (Flength (keys)); | |
1931 | sep = build_string (" "); | |
1932 | /* This has one extra element at the end that we don't pass to Fconcat. */ | |
1933 | args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object)); | |
1934 | ||
1935 | for (i = 0; i < len; i++) | |
1936 | { | |
c1848a97 | 1937 | args[i * 2] = Fsingle_key_description (XCAR (keys), Qnil); |
5c9c2c3f | 1938 | args[i * 2 + 1] = sep; |
03699b14 | 1939 | keys = XCDR (keys); |
5c9c2c3f | 1940 | } |
4c7d5f13 | 1941 | } |
5c9c2c3f RS |
1942 | else |
1943 | keys = wrong_type_argument (Qarrayp, keys); | |
4c7d5f13 | 1944 | |
6e80fddb | 1945 | if (len == 0) |
2b6748c0 | 1946 | return empty_string; |
4c7d5f13 | 1947 | return Fconcat (len * 2 - 1, args); |
2c6f1a39 JB |
1948 | } |
1949 | ||
1950 | char * | |
f1cb0a25 | 1951 | push_key_description (c, p, force_multibyte) |
2c6f1a39 JB |
1952 | register unsigned int c; |
1953 | register char *p; | |
f1cb0a25 | 1954 | int force_multibyte; |
2c6f1a39 | 1955 | { |
bc89c609 GM |
1956 | unsigned c2; |
1957 | ||
71ac885b RS |
1958 | /* Clear all the meaningless bits above the meta bit. */ |
1959 | c &= meta_modifier | ~ - meta_modifier; | |
bc89c609 GM |
1960 | c2 = c & ~(alt_modifier | ctrl_modifier | hyper_modifier |
1961 | | meta_modifier | shift_modifier | super_modifier); | |
71ac885b | 1962 | |
6ba6e250 RS |
1963 | if (c & alt_modifier) |
1964 | { | |
1965 | *p++ = 'A'; | |
1966 | *p++ = '-'; | |
1967 | c -= alt_modifier; | |
1968 | } | |
bc89c609 GM |
1969 | if ((c & ctrl_modifier) != 0 |
1970 | || (c2 < ' ' && c2 != 27 && c2 != '\t' && c2 != Ctl ('M'))) | |
6ba6e250 RS |
1971 | { |
1972 | *p++ = 'C'; | |
1973 | *p++ = '-'; | |
bc89c609 | 1974 | c &= ~ctrl_modifier; |
6ba6e250 RS |
1975 | } |
1976 | if (c & hyper_modifier) | |
1977 | { | |
1978 | *p++ = 'H'; | |
1979 | *p++ = '-'; | |
1980 | c -= hyper_modifier; | |
1981 | } | |
1982 | if (c & meta_modifier) | |
2c6f1a39 JB |
1983 | { |
1984 | *p++ = 'M'; | |
1985 | *p++ = '-'; | |
6ba6e250 RS |
1986 | c -= meta_modifier; |
1987 | } | |
1988 | if (c & shift_modifier) | |
1989 | { | |
1990 | *p++ = 'S'; | |
1991 | *p++ = '-'; | |
1992 | c -= shift_modifier; | |
1993 | } | |
1994 | if (c & super_modifier) | |
1995 | { | |
1996 | *p++ = 's'; | |
1997 | *p++ = '-'; | |
1998 | c -= super_modifier; | |
2c6f1a39 JB |
1999 | } |
2000 | if (c < 040) | |
2001 | { | |
2002 | if (c == 033) | |
2003 | { | |
2004 | *p++ = 'E'; | |
2005 | *p++ = 'S'; | |
2006 | *p++ = 'C'; | |
2007 | } | |
6ba6e250 | 2008 | else if (c == '\t') |
2c6f1a39 JB |
2009 | { |
2010 | *p++ = 'T'; | |
2011 | *p++ = 'A'; | |
2012 | *p++ = 'B'; | |
2013 | } | |
b8cab006 | 2014 | else if (c == Ctl ('M')) |
2c6f1a39 JB |
2015 | { |
2016 | *p++ = 'R'; | |
2017 | *p++ = 'E'; | |
2018 | *p++ = 'T'; | |
2019 | } | |
2020 | else | |
2021 | { | |
bc89c609 | 2022 | /* `C-' already added above. */ |
2c6f1a39 JB |
2023 | if (c > 0 && c <= Ctl ('Z')) |
2024 | *p++ = c + 0140; | |
2025 | else | |
2026 | *p++ = c + 0100; | |
2027 | } | |
2028 | } | |
2029 | else if (c == 0177) | |
2030 | { | |
2031 | *p++ = 'D'; | |
2032 | *p++ = 'E'; | |
2033 | *p++ = 'L'; | |
2034 | } | |
2035 | else if (c == ' ') | |
9fb71293 | 2036 | { |
2c6f1a39 JB |
2037 | *p++ = 'S'; |
2038 | *p++ = 'P'; | |
2039 | *p++ = 'C'; | |
2040 | } | |
d3c00496 KH |
2041 | else if (c < 128 |
2042 | || (NILP (current_buffer->enable_multibyte_characters) | |
f1cb0a25 GM |
2043 | && SINGLE_BYTE_CHAR_P (c) |
2044 | && !force_multibyte)) | |
2045 | { | |
2046 | *p++ = c; | |
2047 | } | |
6ba6e250 RS |
2048 | else |
2049 | { | |
f1cb0a25 GM |
2050 | int valid_p = SINGLE_BYTE_CHAR_P (c) || char_valid_p (c, 0); |
2051 | ||
2052 | if (force_multibyte && valid_p) | |
2053 | { | |
2054 | if (SINGLE_BYTE_CHAR_P (c)) | |
2055 | c = unibyte_char_to_multibyte (c); | |
2056 | p += CHAR_STRING (c, p); | |
2057 | } | |
2058 | else if (NILP (current_buffer->enable_multibyte_characters) | |
2059 | || valid_p) | |
9fb71293 KH |
2060 | { |
2061 | int bit_offset; | |
2062 | *p++ = '\\'; | |
2063 | /* The biggest character code uses 19 bits. */ | |
2064 | for (bit_offset = 18; bit_offset >= 0; bit_offset -= 3) | |
2065 | { | |
2066 | if (c >= (1 << bit_offset)) | |
2067 | *p++ = ((c & (7 << bit_offset)) >> bit_offset) + '0'; | |
2068 | } | |
2069 | } | |
2070 | else | |
f1cb0a25 | 2071 | p += CHAR_STRING (c, p); |
6ba6e250 | 2072 | } |
2c6f1a39 | 2073 | |
d55627cc | 2074 | return p; |
2c6f1a39 JB |
2075 | } |
2076 | ||
21a0d7a0 RS |
2077 | /* This function cannot GC. */ |
2078 | ||
c1848a97 GM |
2079 | DEFUN ("single-key-description", Fsingle_key_description, |
2080 | Ssingle_key_description, 1, 2, 0, | |
335c5470 PJ |
2081 | doc: /* Return a pretty description of command character KEY. |
2082 | Control characters turn into C-whatever, etc. | |
2083 | Optional argument NO-ANGLES non-nil means don't put angle brackets | |
2084 | around function keys and event symbols. */) | |
2085 | (key, no_angles) | |
c1848a97 | 2086 | Lisp_Object key, no_angles; |
2c6f1a39 | 2087 | { |
5c9c2c3f RS |
2088 | if (CONSP (key) && lucid_event_type_list_p (key)) |
2089 | key = Fevent_convert_list (key); | |
2090 | ||
cebd887d | 2091 | key = EVENT_HEAD (key); |
6bbbd9b0 | 2092 | |
e958fd9a | 2093 | if (INTEGERP (key)) /* Normal character */ |
2c6f1a39 | 2094 | { |
47a18cef | 2095 | unsigned int charset, c1, c2; |
f4977051 | 2096 | int without_bits = XINT (key) & ~((-1) << CHARACTERBITS); |
47a18cef | 2097 | |
f4977051 | 2098 | if (SINGLE_BYTE_CHAR_P (without_bits)) |
47a18cef RS |
2099 | charset = 0; |
2100 | else | |
54e03a4a | 2101 | SPLIT_CHAR (without_bits, charset, c1, c2); |
47a18cef RS |
2102 | |
2103 | if (charset | |
9fb71293 | 2104 | && CHARSET_DEFINED_P (charset) |
47a18cef RS |
2105 | && ((c1 >= 0 && c1 < 32) |
2106 | || (c2 >= 0 && c2 < 32))) | |
2107 | { | |
2108 | /* Handle a generic character. */ | |
2109 | Lisp_Object name; | |
2110 | name = CHARSET_TABLE_INFO (charset, CHARSET_LONG_NAME_IDX); | |
b7826503 | 2111 | CHECK_STRING (name); |
47a18cef RS |
2112 | return concat2 (build_string ("Character set "), name); |
2113 | } | |
2114 | else | |
2115 | { | |
3d9d7a9b GM |
2116 | char tem[KEY_DESCRIPTION_SIZE], *end; |
2117 | int nbytes, nchars; | |
2118 | Lisp_Object string; | |
2119 | ||
2120 | end = push_key_description (XUINT (key), tem, 1); | |
2121 | nbytes = end - tem; | |
2122 | nchars = multibyte_chars_in_text (tem, nbytes); | |
2123 | if (nchars == nbytes) | |
e15e2828 GM |
2124 | { |
2125 | *end = '\0'; | |
2126 | string = build_string (tem); | |
2127 | } | |
3d9d7a9b GM |
2128 | else |
2129 | string = make_multibyte_string (tem, nchars, nbytes); | |
2130 | return string; | |
47a18cef | 2131 | } |
2c6f1a39 | 2132 | } |
e958fd9a | 2133 | else if (SYMBOLP (key)) /* Function key or event-symbol */ |
c7edb960 | 2134 | { |
c1848a97 GM |
2135 | if (NILP (no_angles)) |
2136 | { | |
2137 | char *buffer | |
2138 | = (char *) alloca (STRING_BYTES (XSYMBOL (key)->name) + 5); | |
2139 | sprintf (buffer, "<%s>", XSYMBOL (key)->name->data); | |
2140 | return build_string (buffer); | |
2141 | } | |
2142 | else | |
2143 | return Fsymbol_name (key); | |
c7edb960 | 2144 | } |
e958fd9a KH |
2145 | else if (STRINGP (key)) /* Buffer names in the menubar. */ |
2146 | return Fcopy_sequence (key); | |
2147 | else | |
2148 | error ("KEY must be an integer, cons, symbol, or string"); | |
6bbd7a29 | 2149 | return Qnil; |
2c6f1a39 JB |
2150 | } |
2151 | ||
2152 | char * | |
2153 | push_text_char_description (c, p) | |
2154 | register unsigned int c; | |
2155 | register char *p; | |
2156 | { | |
2157 | if (c >= 0200) | |
2158 | { | |
2159 | *p++ = 'M'; | |
2160 | *p++ = '-'; | |
2161 | c -= 0200; | |
2162 | } | |
2163 | if (c < 040) | |
2164 | { | |
2165 | *p++ = '^'; | |
2166 | *p++ = c + 64; /* 'A' - 1 */ | |
2167 | } | |
2168 | else if (c == 0177) | |
2169 | { | |
2170 | *p++ = '^'; | |
2171 | *p++ = '?'; | |
2172 | } | |
2173 | else | |
2174 | *p++ = c; | |
d55627cc | 2175 | return p; |
2c6f1a39 JB |
2176 | } |
2177 | ||
21a0d7a0 RS |
2178 | /* This function cannot GC. */ |
2179 | ||
2c6f1a39 | 2180 | DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0, |
335c5470 PJ |
2181 | doc: /* Return a pretty description of file-character CHARACTER. |
2182 | Control characters turn into "^char", etc. */) | |
2183 | (character) | |
88539837 | 2184 | Lisp_Object character; |
2c6f1a39 | 2185 | { |
0a16479f KH |
2186 | /* Currently MAX_MULTIBYTE_LENGTH is 4 (< 6). */ |
2187 | unsigned char str[6]; | |
2188 | int c; | |
2c6f1a39 | 2189 | |
b7826503 | 2190 | CHECK_NUMBER (character); |
2c6f1a39 | 2191 | |
0a16479f KH |
2192 | c = XINT (character); |
2193 | if (!SINGLE_BYTE_CHAR_P (c)) | |
a98f1d1d | 2194 | { |
0a16479f | 2195 | int len = CHAR_STRING (c, str); |
a98f1d1d | 2196 | |
f3ba5409 | 2197 | return make_multibyte_string (str, 1, len); |
a98f1d1d KH |
2198 | } |
2199 | ||
0a16479f | 2200 | *push_text_char_description (c & 0377, str) = 0; |
2c6f1a39 | 2201 | |
0a16479f | 2202 | return build_string (str); |
2c6f1a39 | 2203 | } |
2fc66973 JB |
2204 | |
2205 | /* Return non-zero if SEQ contains only ASCII characters, perhaps with | |
2206 | a meta bit. */ | |
2207 | static int | |
2208 | ascii_sequence_p (seq) | |
2209 | Lisp_Object seq; | |
2210 | { | |
6e344130 | 2211 | int i; |
2fc66973 | 2212 | int len = XINT (Flength (seq)); |
ffab2bd6 | 2213 | |
6e344130 | 2214 | for (i = 0; i < len; i++) |
2fc66973 | 2215 | { |
6e344130 | 2216 | Lisp_Object ii, elt; |
ffab2bd6 | 2217 | |
6e344130 KH |
2218 | XSETFASTINT (ii, i); |
2219 | elt = Faref (seq, ii); | |
2fc66973 | 2220 | |
416349ec | 2221 | if (!INTEGERP (elt) |
2fc66973 JB |
2222 | || (XUINT (elt) & ~CHAR_META) >= 0x80) |
2223 | return 0; | |
2224 | } | |
2225 | ||
2226 | return 1; | |
2227 | } | |
2228 | ||
2c6f1a39 | 2229 | \f |
cc0a8174 JB |
2230 | /* where-is - finding a command in a set of keymaps. */ |
2231 | ||
0c412762 | 2232 | static Lisp_Object where_is_internal (); |
0403641f | 2233 | static Lisp_Object where_is_internal_1 (); |
69248761 | 2234 | static void where_is_internal_2 (); |
0403641f | 2235 | |
49801145 SM |
2236 | /* Like Flookup_key, but uses a list of keymaps SHADOW instead of a single map. |
2237 | Returns the first non-nil binding found in any of those maps. */ | |
2238 | ||
2239 | static Lisp_Object | |
2240 | shadow_lookup (shadow, key, flag) | |
2241 | Lisp_Object shadow, key, flag; | |
2242 | { | |
2243 | Lisp_Object tail, value; | |
2244 | ||
2245 | for (tail = shadow; CONSP (tail); tail = XCDR (tail)) | |
2246 | { | |
2247 | value = Flookup_key (XCAR (tail), key, flag); | |
2248 | if (!NILP (value) && !NATNUMP (value)) | |
2249 | return value; | |
2250 | } | |
2251 | return Qnil; | |
2252 | } | |
2253 | ||
2254 | /* This function can GC if Flookup_key autoloads any keymaps. */ | |
2255 | ||
1e7d1ab0 | 2256 | static Lisp_Object |
0c412762 | 2257 | where_is_internal (definition, keymaps, firstonly, noindirect, no_remap) |
1e7d1ab0 | 2258 | Lisp_Object definition, keymaps; |
0c412762 | 2259 | Lisp_Object firstonly, noindirect, no_remap; |
2c6f1a39 | 2260 | { |
49801145 | 2261 | Lisp_Object maps = Qnil; |
0403641f | 2262 | Lisp_Object found, sequences; |
21a0d7a0 | 2263 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
0bc395d4 RS |
2264 | /* 1 means ignore all menu bindings entirely. */ |
2265 | int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii); | |
2c6f1a39 | 2266 | |
0c412762 KS |
2267 | /* If this command is remapped, then it has no key bindings |
2268 | of its own. */ | |
a1df473f KS |
2269 | if (NILP (no_remap) && SYMBOLP (definition)) |
2270 | { | |
2271 | Lisp_Object tem; | |
2272 | if (tem = Fremap_command (definition), !NILP (tem)) | |
2273 | return Qnil; | |
2274 | } | |
0c412762 | 2275 | |
49801145 SM |
2276 | found = keymaps; |
2277 | while (CONSP (found)) | |
93d2aa1c | 2278 | { |
49801145 | 2279 | maps = |
02067692 SM |
2280 | nconc2 (maps, |
2281 | Faccessible_keymaps (get_keymap (XCAR (found), 1, 0), Qnil)); | |
49801145 | 2282 | found = XCDR (found); |
93d2aa1c | 2283 | } |
49801145 SM |
2284 | |
2285 | GCPRO5 (definition, keymaps, maps, found, sequences); | |
2c6f1a39 | 2286 | found = Qnil; |
0403641f | 2287 | sequences = Qnil; |
2c6f1a39 | 2288 | |
265a9e55 | 2289 | for (; !NILP (maps); maps = Fcdr (maps)) |
2c6f1a39 | 2290 | { |
e9b6dfb0 KH |
2291 | /* Key sequence to reach map, and the map that it reaches */ |
2292 | register Lisp_Object this, map; | |
f5b79c1c | 2293 | |
2c6f1a39 JB |
2294 | /* In order to fold [META-PREFIX-CHAR CHAR] sequences into |
2295 | [M-CHAR] sequences, check if last character of the sequence | |
2296 | is the meta-prefix char. */ | |
e9b6dfb0 KH |
2297 | Lisp_Object last; |
2298 | int last_is_meta; | |
2299 | ||
2300 | this = Fcar (Fcar (maps)); | |
2301 | map = Fcdr (Fcar (maps)); | |
2302 | last = make_number (XINT (Flength (this)) - 1); | |
2303 | last_is_meta = (XINT (last) >= 0 | |
2304 | && EQ (Faref (this, last), meta_prefix_char)); | |
2c6f1a39 | 2305 | |
2ba11bbd | 2306 | /* if (nomenus && !ascii_sequence_p (this)) */ |
f58c6494 SM |
2307 | if (nomenus && XINT (last) >= 0 |
2308 | && !INTEGERP (Faref (this, make_number (0)))) | |
88416888 SM |
2309 | /* If no menu entries should be returned, skip over the |
2310 | keymaps bound to `menu-bar' and `tool-bar' and other | |
2ba11bbd | 2311 | non-ascii prefixes like `C-down-mouse-2'. */ |
88416888 SM |
2312 | continue; |
2313 | ||
fde3a52f JB |
2314 | QUIT; |
2315 | ||
f5b79c1c | 2316 | while (CONSP (map)) |
2c6f1a39 | 2317 | { |
f5b79c1c JB |
2318 | /* Because the code we want to run on each binding is rather |
2319 | large, we don't want to have two separate loop bodies for | |
2320 | sparse keymap bindings and tables; we want to iterate one | |
2321 | loop body over both keymap and vector bindings. | |
2322 | ||
2323 | For this reason, if Fcar (map) is a vector, we don't | |
2324 | advance map to the next element until i indicates that we | |
2325 | have finished off the vector. */ | |
21a0d7a0 | 2326 | Lisp_Object elt, key, binding; |
03699b14 KR |
2327 | elt = XCAR (map); |
2328 | map = XCDR (map); | |
0403641f RS |
2329 | |
2330 | sequences = Qnil; | |
f5b79c1c | 2331 | |
fde3a52f JB |
2332 | QUIT; |
2333 | ||
f5b79c1c JB |
2334 | /* Set key and binding to the current key and binding, and |
2335 | advance map and i to the next binding. */ | |
416349ec | 2336 | if (VECTORP (elt)) |
2c6f1a39 | 2337 | { |
0403641f RS |
2338 | Lisp_Object sequence; |
2339 | int i; | |
2c6f1a39 | 2340 | /* In a vector, look at each element. */ |
0403641f | 2341 | for (i = 0; i < XVECTOR (elt)->size; i++) |
2c6f1a39 | 2342 | { |
49801145 | 2343 | binding = AREF (elt, i); |
0403641f RS |
2344 | XSETFASTINT (key, i); |
2345 | sequence = where_is_internal_1 (binding, key, definition, | |
49801145 | 2346 | noindirect, this, |
0403641f RS |
2347 | last, nomenus, last_is_meta); |
2348 | if (!NILP (sequence)) | |
2349 | sequences = Fcons (sequence, sequences); | |
2c6f1a39 | 2350 | } |
f5b79c1c | 2351 | } |
0403641f | 2352 | else if (CHAR_TABLE_P (elt)) |
f5b79c1c | 2353 | { |
23cf1efa | 2354 | Lisp_Object indices[3]; |
0403641f | 2355 | Lisp_Object args; |
23cf1efa | 2356 | |
0403641f | 2357 | args = Fcons (Fcons (Fcons (definition, noindirect), |
49801145 | 2358 | Qnil), /* Result accumulator. */ |
0403641f RS |
2359 | Fcons (Fcons (this, last), |
2360 | Fcons (make_number (nomenus), | |
2361 | make_number (last_is_meta)))); | |
0403641f RS |
2362 | map_char_table (where_is_internal_2, Qnil, elt, args, |
2363 | 0, indices); | |
49801145 | 2364 | sequences = XCDR (XCAR (args)); |
2c6f1a39 | 2365 | } |
0403641f | 2366 | else if (CONSP (elt)) |
fde3a52f | 2367 | { |
0403641f | 2368 | Lisp_Object sequence; |
2c6f1a39 | 2369 | |
03699b14 KR |
2370 | key = XCAR (elt); |
2371 | binding = XCDR (elt); | |
2c6f1a39 | 2372 | |
0403641f | 2373 | sequence = where_is_internal_1 (binding, key, definition, |
49801145 | 2374 | noindirect, this, |
0403641f RS |
2375 | last, nomenus, last_is_meta); |
2376 | if (!NILP (sequence)) | |
2377 | sequences = Fcons (sequence, sequences); | |
2c6f1a39 | 2378 | } |
2c6f1a39 | 2379 | |
2c6f1a39 | 2380 | |
0c412762 | 2381 | while (!NILP (sequences)) |
2c6f1a39 | 2382 | { |
a1df473f | 2383 | Lisp_Object sequence, remapped, function; |
0403641f | 2384 | |
03699b14 | 2385 | sequence = XCAR (sequences); |
0c412762 KS |
2386 | sequences = XCDR (sequences); |
2387 | ||
a1df473f KS |
2388 | /* If the current sequence is a command remapping with |
2389 | format [remap COMMAND], find the key sequences | |
2390 | which run COMMAND, and use those sequences instead. */ | |
0c412762 KS |
2391 | remapped = Qnil; |
2392 | if (NILP (no_remap) | |
a1df473f KS |
2393 | && VECTORP (sequence) && XVECTOR (sequence)->size == 2 |
2394 | && EQ (AREF (sequence, 0), Qremap) | |
2395 | && (function = AREF (sequence, 1), SYMBOLP (function))) | |
0c412762 | 2396 | { |
a1df473f | 2397 | Lisp_Object remapped1; |
0c412762 | 2398 | |
a1df473f KS |
2399 | remapped1 = where_is_internal (function, keymaps, firstonly, noindirect, Qt); |
2400 | if (CONSP (remapped1)) | |
0c412762 | 2401 | { |
a1df473f KS |
2402 | /* Verify that this key binding actually maps to the |
2403 | remapped command (see below). */ | |
2404 | if (!EQ (shadow_lookup (keymaps, XCAR (remapped1), Qnil), function)) | |
2405 | continue; | |
2406 | sequence = XCAR (remapped1); | |
2407 | remapped = XCDR (remapped1); | |
2408 | goto record_sequence; | |
0c412762 KS |
2409 | } |
2410 | } | |
0403641f | 2411 | |
49801145 SM |
2412 | /* Verify that this key binding is not shadowed by another |
2413 | binding for the same key, before we say it exists. | |
2414 | ||
2415 | Mechanism: look for local definition of this key and if | |
2416 | it is defined and does not match what we found then | |
2417 | ignore this key. | |
2418 | ||
2419 | Either nil or number as value from Flookup_key | |
2420 | means undefined. */ | |
1e7d1ab0 | 2421 | if (!EQ (shadow_lookup (keymaps, sequence, Qnil), definition)) |
49801145 SM |
2422 | continue; |
2423 | ||
0c412762 | 2424 | record_sequence: |
0403641f RS |
2425 | /* It is a true unshadowed match. Record it, unless it's already |
2426 | been seen (as could happen when inheriting keymaps). */ | |
2427 | if (NILP (Fmember (sequence, found))) | |
2428 | found = Fcons (sequence, found); | |
2429 | ||
2430 | /* If firstonly is Qnon_ascii, then we can return the first | |
2431 | binding we find. If firstonly is not Qnon_ascii but not | |
2432 | nil, then we should return the first ascii-only binding | |
2433 | we find. */ | |
2434 | if (EQ (firstonly, Qnon_ascii)) | |
2435 | RETURN_UNGCPRO (sequence); | |
54cbc3d4 | 2436 | else if (!NILP (firstonly) && ascii_sequence_p (sequence)) |
0403641f | 2437 | RETURN_UNGCPRO (sequence); |
0c412762 KS |
2438 | |
2439 | if (CONSP (remapped)) | |
2440 | { | |
2441 | sequence = XCAR (remapped); | |
2442 | remapped = XCDR (remapped); | |
2443 | goto record_sequence; | |
2444 | } | |
2c6f1a39 | 2445 | } |
2c6f1a39 JB |
2446 | } |
2447 | } | |
2fc66973 | 2448 | |
21a0d7a0 RS |
2449 | UNGCPRO; |
2450 | ||
2fc66973 JB |
2451 | found = Fnreverse (found); |
2452 | ||
2453 | /* firstonly may have been t, but we may have gone all the way through | |
2454 | the keymaps without finding an all-ASCII key sequence. So just | |
2455 | return the best we could find. */ | |
54cbc3d4 | 2456 | if (!NILP (firstonly)) |
2fc66973 JB |
2457 | return Fcar (found); |
2458 | ||
2459 | return found; | |
2c6f1a39 | 2460 | } |
0403641f | 2461 | |
0c412762 | 2462 | DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 5, 0, |
335c5470 PJ |
2463 | doc: /* Return list of keys that invoke DEFINITION. |
2464 | If KEYMAP is non-nil, search only KEYMAP and the global keymap. | |
2465 | If KEYMAP is nil, search all the currently active keymaps. | |
2466 | If KEYMAP is a list of keymaps, search only those keymaps. | |
2467 | ||
2468 | If optional 3rd arg FIRSTONLY is non-nil, return the first key sequence found, | |
2469 | rather than a list of all possible key sequences. | |
2470 | If FIRSTONLY is the symbol `non-ascii', return the first binding found, | |
2471 | no matter what it is. | |
2472 | If FIRSTONLY has another non-nil value, prefer sequences of ASCII characters, | |
2473 | and entirely reject menu bindings. | |
2474 | ||
2475 | If optional 4th arg NOINDIRECT is non-nil, don't follow indirections | |
2476 | to other keymaps or slots. This makes it possible to search for an | |
0c412762 KS |
2477 | indirect definition itself. |
2478 | ||
2479 | If optional 5th arg NO-REMAP is non-nil, don't search for key sequences | |
2480 | that invoke a command which is remapped to DEFINITION, but include the | |
2481 | remapped command in the returned list. */) | |
2482 | (definition, keymap, firstonly, noindirect, no_remap) | |
4956d1ef | 2483 | Lisp_Object definition, keymap; |
0c412762 | 2484 | Lisp_Object firstonly, noindirect, no_remap; |
1e7d1ab0 SM |
2485 | { |
2486 | Lisp_Object sequences, keymaps; | |
1e7d1ab0 SM |
2487 | /* 1 means ignore all menu bindings entirely. */ |
2488 | int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii); | |
f9aaedb6 | 2489 | Lisp_Object result; |
1e7d1ab0 SM |
2490 | |
2491 | /* Find the relevant keymaps. */ | |
4956d1ef GM |
2492 | if (CONSP (keymap) && KEYMAPP (XCAR (keymap))) |
2493 | keymaps = keymap; | |
54cbc3d4 | 2494 | else if (!NILP (keymap)) |
4956d1ef | 2495 | keymaps = Fcons (keymap, Fcons (current_global_map, Qnil)); |
1e7d1ab0 | 2496 | else |
54cbc3d4 | 2497 | keymaps = Fcurrent_active_maps (Qnil); |
1e7d1ab0 SM |
2498 | |
2499 | /* Only use caching for the menubar (i.e. called with (def nil t nil). | |
4956d1ef GM |
2500 | We don't really need to check `keymap'. */ |
2501 | if (nomenus && NILP (noindirect) && NILP (keymap)) | |
1e7d1ab0 | 2502 | { |
f9aaedb6 | 2503 | Lisp_Object *defns; |
60dc6558 | 2504 | int i, j, n; |
0c412762 | 2505 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
f9aaedb6 | 2506 | |
1e7d1ab0 SM |
2507 | /* Check heuristic-consistency of the cache. */ |
2508 | if (NILP (Fequal (keymaps, where_is_cache_keymaps))) | |
2509 | where_is_cache = Qnil; | |
2510 | ||
2511 | if (NILP (where_is_cache)) | |
2512 | { | |
2513 | /* We need to create the cache. */ | |
2514 | Lisp_Object args[2]; | |
2515 | where_is_cache = Fmake_hash_table (0, args); | |
2516 | where_is_cache_keymaps = Qt; | |
2517 | ||
2518 | /* Fill in the cache. */ | |
0c412762 KS |
2519 | GCPRO5 (definition, keymaps, firstonly, noindirect, no_remap); |
2520 | where_is_internal (definition, keymaps, firstonly, noindirect, no_remap); | |
1e7d1ab0 SM |
2521 | UNGCPRO; |
2522 | ||
2523 | where_is_cache_keymaps = keymaps; | |
2524 | } | |
2525 | ||
f9aaedb6 GM |
2526 | /* We want to process definitions from the last to the first. |
2527 | Instead of consing, copy definitions to a vector and step | |
2528 | over that vector. */ | |
1e7d1ab0 | 2529 | sequences = Fgethash (definition, where_is_cache, Qnil); |
f58c6494 | 2530 | n = XINT (Flength (sequences)); |
f9aaedb6 GM |
2531 | defns = (Lisp_Object *) alloca (n * sizeof *defns); |
2532 | for (i = 0; CONSP (sequences); sequences = XCDR (sequences)) | |
2533 | defns[i++] = XCAR (sequences); | |
2534 | ||
2535 | /* Verify that the key bindings are not shadowed. Note that | |
2536 | the following can GC. */ | |
2537 | GCPRO2 (definition, keymaps); | |
2538 | result = Qnil; | |
60dc6558 | 2539 | j = -1; |
f9aaedb6 | 2540 | for (i = n - 1; i >= 0; --i) |
60dc6558 SM |
2541 | if (EQ (shadow_lookup (keymaps, defns[i], Qnil), definition)) |
2542 | { | |
2543 | if (ascii_sequence_p (defns[i])) | |
2544 | break; | |
2545 | else if (j < 0) | |
2546 | j = i; | |
2547 | } | |
f9aaedb6 | 2548 | |
60dc6558 | 2549 | result = i >= 0 ? defns[i] : (j >= 0 ? defns[j] : Qnil); |
f9aaedb6 | 2550 | UNGCPRO; |
1e7d1ab0 SM |
2551 | } |
2552 | else | |
2553 | { | |
2554 | /* Kill the cache so that where_is_internal_1 doesn't think | |
2555 | we're filling it up. */ | |
2556 | where_is_cache = Qnil; | |
0c412762 | 2557 | result = where_is_internal (definition, keymaps, firstonly, noindirect, no_remap); |
1e7d1ab0 | 2558 | } |
f9aaedb6 GM |
2559 | |
2560 | return result; | |
1e7d1ab0 SM |
2561 | } |
2562 | ||
0403641f RS |
2563 | /* This is the function that Fwhere_is_internal calls using map_char_table. |
2564 | ARGS has the form | |
2565 | (((DEFINITION . NOINDIRECT) . (KEYMAP . RESULT)) | |
2566 | . | |
2567 | ((THIS . LAST) . (NOMENUS . LAST_IS_META))) | |
2568 | Since map_char_table doesn't really use the return value from this function, | |
df75b1a3 GM |
2569 | we the result append to RESULT, the slot in ARGS. |
2570 | ||
2571 | This function can GC because it calls where_is_internal_1 which can | |
2572 | GC. */ | |
0403641f | 2573 | |
69248761 | 2574 | static void |
0403641f RS |
2575 | where_is_internal_2 (args, key, binding) |
2576 | Lisp_Object args, key, binding; | |
2577 | { | |
49801145 | 2578 | Lisp_Object definition, noindirect, this, last; |
0403641f RS |
2579 | Lisp_Object result, sequence; |
2580 | int nomenus, last_is_meta; | |
df75b1a3 | 2581 | struct gcpro gcpro1, gcpro2, gcpro3; |
0403641f | 2582 | |
df75b1a3 | 2583 | GCPRO3 (args, key, binding); |
49801145 | 2584 | result = XCDR (XCAR (args)); |
03699b14 KR |
2585 | definition = XCAR (XCAR (XCAR (args))); |
2586 | noindirect = XCDR (XCAR (XCAR (args))); | |
03699b14 KR |
2587 | this = XCAR (XCAR (XCDR (args))); |
2588 | last = XCDR (XCAR (XCDR (args))); | |
2589 | nomenus = XFASTINT (XCAR (XCDR (XCDR (args)))); | |
2590 | last_is_meta = XFASTINT (XCDR (XCDR (XCDR (args)))); | |
0403641f | 2591 | |
49801145 | 2592 | sequence = where_is_internal_1 (binding, key, definition, noindirect, |
0403641f RS |
2593 | this, last, nomenus, last_is_meta); |
2594 | ||
2595 | if (!NILP (sequence)) | |
f3fbd155 | 2596 | XSETCDR (XCAR (args), Fcons (sequence, result)); |
df75b1a3 GM |
2597 | |
2598 | UNGCPRO; | |
0403641f RS |
2599 | } |
2600 | ||
df75b1a3 | 2601 | |
49801145 | 2602 | /* This function cannot GC. */ |
df75b1a3 | 2603 | |
0403641f | 2604 | static Lisp_Object |
49801145 | 2605 | where_is_internal_1 (binding, key, definition, noindirect, this, last, |
0403641f | 2606 | nomenus, last_is_meta) |
49801145 | 2607 | Lisp_Object binding, key, definition, noindirect, this, last; |
0403641f RS |
2608 | int nomenus, last_is_meta; |
2609 | { | |
2610 | Lisp_Object sequence; | |
0403641f RS |
2611 | |
2612 | /* Search through indirections unless that's not wanted. */ | |
2613 | if (NILP (noindirect)) | |
35810b6f | 2614 | binding = get_keyelt (binding, 0); |
0403641f RS |
2615 | |
2616 | /* End this iteration if this element does not match | |
2617 | the target. */ | |
2618 | ||
1e7d1ab0 SM |
2619 | if (!(!NILP (where_is_cache) /* everything "matches" during cache-fill. */ |
2620 | || EQ (binding, definition) | |
2621 | || (CONSP (definition) && !NILP (Fequal (binding, definition))))) | |
2622 | /* Doesn't match. */ | |
2623 | return Qnil; | |
0403641f | 2624 | |
1e7d1ab0 | 2625 | /* We have found a match. Construct the key sequence where we found it. */ |
0403641f RS |
2626 | if (INTEGERP (key) && last_is_meta) |
2627 | { | |
2628 | sequence = Fcopy_sequence (this); | |
2629 | Faset (sequence, last, make_number (XINT (key) | meta_modifier)); | |
2630 | } | |
2631 | else | |
2632 | sequence = append_key (this, key); | |
2633 | ||
1e7d1ab0 SM |
2634 | if (!NILP (where_is_cache)) |
2635 | { | |
2636 | Lisp_Object sequences = Fgethash (binding, where_is_cache, Qnil); | |
2637 | Fputhash (binding, Fcons (sequence, sequences), where_is_cache); | |
2638 | return Qnil; | |
2639 | } | |
2640 | else | |
2641 | return sequence; | |
0403641f | 2642 | } |
2c6f1a39 | 2643 | \f |
cc0a8174 JB |
2644 | /* describe-bindings - summarizing all the bindings in a set of keymaps. */ |
2645 | ||
54cbc3d4 | 2646 | DEFUN ("describe-buffer-bindings", Fdescribe_buffer_bindings, Sdescribe_buffer_bindings, 1, 3, 0, |
335c5470 PJ |
2647 | doc: /* Insert the list of all defined keys and their definitions. |
2648 | The list is inserted in the current buffer, while the bindings are | |
2649 | looked up in BUFFER. | |
2650 | The optional argument PREFIX, if non-nil, should be a key sequence; | |
2651 | then we display only bindings that start with that prefix. | |
2652 | The optional argument MENUS, if non-nil, says to mention menu bindings. | |
2653 | \(Ordinarily these are omitted from the output.) */) | |
2654 | (buffer, prefix, menus) | |
54cbc3d4 | 2655 | Lisp_Object buffer, prefix, menus; |
2c6f1a39 | 2656 | { |
54cbc3d4 SM |
2657 | Lisp_Object outbuf, shadow; |
2658 | int nomenu = NILP (menus); | |
d7ab90a9 KH |
2659 | register Lisp_Object start1; |
2660 | struct gcpro gcpro1; | |
2c6f1a39 | 2661 | |
4726a9f1 JB |
2662 | char *alternate_heading |
2663 | = "\ | |
6cec169a RS |
2664 | Keyboard translations:\n\n\ |
2665 | You type Translation\n\ | |
2666 | -------- -----------\n"; | |
2c6f1a39 | 2667 | |
a588e041 | 2668 | shadow = Qnil; |
d7ab90a9 | 2669 | GCPRO1 (shadow); |
53c8f9fa | 2670 | |
36ca6189 | 2671 | outbuf = Fcurrent_buffer (); |
2c6f1a39 | 2672 | |
4726a9f1 | 2673 | /* Report on alternates for keys. */ |
d7bf9bf5 | 2674 | if (STRINGP (Vkeyboard_translate_table) && !NILP (prefix)) |
4726a9f1 JB |
2675 | { |
2676 | int c; | |
2677 | unsigned char *translate = XSTRING (Vkeyboard_translate_table)->data; | |
2678 | int translate_len = XSTRING (Vkeyboard_translate_table)->size; | |
2679 | ||
2680 | for (c = 0; c < translate_len; c++) | |
2681 | if (translate[c] != c) | |
2682 | { | |
d2d9586a | 2683 | char buf[KEY_DESCRIPTION_SIZE]; |
4726a9f1 JB |
2684 | char *bufend; |
2685 | ||
2686 | if (alternate_heading) | |
2687 | { | |
2688 | insert_string (alternate_heading); | |
2689 | alternate_heading = 0; | |
2690 | } | |
2691 | ||
f1cb0a25 | 2692 | bufend = push_key_description (translate[c], buf, 1); |
4726a9f1 JB |
2693 | insert (buf, bufend - buf); |
2694 | Findent_to (make_number (16), make_number (1)); | |
f1cb0a25 | 2695 | bufend = push_key_description (c, buf, 1); |
4726a9f1 JB |
2696 | insert (buf, bufend - buf); |
2697 | ||
2698 | insert ("\n", 1); | |
2699 | } | |
2700 | ||
2701 | insert ("\n", 1); | |
2702 | } | |
2703 | ||
d7bf9bf5 RS |
2704 | if (!NILP (Vkey_translation_map)) |
2705 | describe_map_tree (Vkey_translation_map, 0, Qnil, prefix, | |
6cec169a | 2706 | "Key translations", nomenu, 1, 0); |
d7bf9bf5 | 2707 | |
cc0a8174 | 2708 | |
53c8f9fa | 2709 | /* Print the (major mode) local map. */ |
36ca6189 | 2710 | start1 = Qnil; |
e784236d KH |
2711 | if (!NILP (current_kboard->Voverriding_terminal_local_map)) |
2712 | start1 = current_kboard->Voverriding_terminal_local_map; | |
2713 | else if (!NILP (Voverriding_local_map)) | |
7d92e329 | 2714 | start1 = Voverriding_local_map; |
7d92e329 | 2715 | |
265a9e55 | 2716 | if (!NILP (start1)) |
2c6f1a39 | 2717 | { |
91f64ec2 | 2718 | describe_map_tree (start1, 1, shadow, prefix, |
36ca6189 | 2719 | "\f\nOverriding Bindings", nomenu, 0, 0); |
53c8f9fa | 2720 | shadow = Fcons (start1, shadow); |
2c6f1a39 | 2721 | } |
36ca6189 RS |
2722 | else |
2723 | { | |
2724 | /* Print the minor mode and major mode keymaps. */ | |
2725 | int i, nmaps; | |
2726 | Lisp_Object *modes, *maps; | |
2727 | ||
2728 | /* Temporarily switch to `buffer', so that we can get that buffer's | |
2729 | minor modes correctly. */ | |
2730 | Fset_buffer (buffer); | |
2731 | ||
2732 | nmaps = current_minor_maps (&modes, &maps); | |
2733 | Fset_buffer (outbuf); | |
2734 | ||
d1d070e3 RS |
2735 | start1 = get_local_map (BUF_PT (XBUFFER (buffer)), |
2736 | XBUFFER (buffer), Qkeymap); | |
2737 | if (!NILP (start1)) | |
2738 | { | |
2739 | describe_map_tree (start1, 1, shadow, prefix, | |
2740 | "\f\n`keymap' Property Bindings", nomenu, 0, 0); | |
2741 | shadow = Fcons (start1, shadow); | |
2742 | } | |
2743 | ||
36ca6189 RS |
2744 | /* Print the minor mode maps. */ |
2745 | for (i = 0; i < nmaps; i++) | |
2746 | { | |
2747 | /* The title for a minor mode keymap | |
2748 | is constructed at run time. | |
2749 | We let describe_map_tree do the actual insertion | |
2750 | because it takes care of other features when doing so. */ | |
2751 | char *title, *p; | |
2752 | ||
2753 | if (!SYMBOLP (modes[i])) | |
2754 | abort(); | |
2755 | ||
2756 | p = title = (char *) alloca (42 + XSYMBOL (modes[i])->name->size); | |
2757 | *p++ = '\f'; | |
2758 | *p++ = '\n'; | |
2759 | *p++ = '`'; | |
2760 | bcopy (XSYMBOL (modes[i])->name->data, p, | |
2761 | XSYMBOL (modes[i])->name->size); | |
2762 | p += XSYMBOL (modes[i])->name->size; | |
2763 | *p++ = '\''; | |
2764 | bcopy (" Minor Mode Bindings", p, sizeof (" Minor Mode Bindings") - 1); | |
2765 | p += sizeof (" Minor Mode Bindings") - 1; | |
2766 | *p = 0; | |
2767 | ||
2768 | describe_map_tree (maps[i], 1, shadow, prefix, title, nomenu, 0, 0); | |
2769 | shadow = Fcons (maps[i], shadow); | |
2770 | } | |
2771 | ||
36ca6189 RS |
2772 | start1 = get_local_map (BUF_PT (XBUFFER (buffer)), |
2773 | XBUFFER (buffer), Qlocal_map); | |
2774 | if (!NILP (start1)) | |
2775 | { | |
2776 | if (EQ (start1, XBUFFER (buffer)->keymap)) | |
2777 | describe_map_tree (start1, 1, shadow, prefix, | |
2778 | "\f\nMajor Mode Bindings", nomenu, 0, 0); | |
2779 | else | |
2780 | describe_map_tree (start1, 1, shadow, prefix, | |
d1d070e3 RS |
2781 | "\f\n`local-map' Property Bindings", |
2782 | nomenu, 0, 0); | |
36ca6189 RS |
2783 | |
2784 | shadow = Fcons (start1, shadow); | |
2785 | } | |
2786 | } | |
2c6f1a39 | 2787 | |
91f64ec2 | 2788 | describe_map_tree (current_global_map, 1, shadow, prefix, |
97d4edaa | 2789 | "\f\nGlobal Bindings", nomenu, 0, 1); |
d7bf9bf5 RS |
2790 | |
2791 | /* Print the function-key-map translations under this prefix. */ | |
2792 | if (!NILP (Vfunction_key_map)) | |
2793 | describe_map_tree (Vfunction_key_map, 0, Qnil, prefix, | |
97d4edaa | 2794 | "\f\nFunction key map translations", nomenu, 1, 0); |
2c6f1a39 | 2795 | |
d7ab90a9 | 2796 | UNGCPRO; |
2c6f1a39 JB |
2797 | return Qnil; |
2798 | } | |
2799 | ||
b31a4218 | 2800 | /* Insert a description of the key bindings in STARTMAP, |
2c6f1a39 JB |
2801 | followed by those of all maps reachable through STARTMAP. |
2802 | If PARTIAL is nonzero, omit certain "uninteresting" commands | |
2803 | (such as `undefined'). | |
53c8f9fa RS |
2804 | If SHADOW is non-nil, it is a list of maps; |
2805 | don't mention keys which would be shadowed by any of them. | |
2806 | PREFIX, if non-nil, says mention only keys that start with PREFIX. | |
07f15dfd | 2807 | TITLE, if not 0, is a string to insert at the beginning. |
af1d6f09 | 2808 | TITLE should not end with a colon or a newline; we supply that. |
d7bf9bf5 RS |
2809 | If NOMENU is not 0, then omit menu-bar commands. |
2810 | ||
2811 | If TRANSL is nonzero, the definitions are actually key translations | |
c2b714de RS |
2812 | so print strings and vectors differently. |
2813 | ||
2814 | If ALWAYS_TITLE is nonzero, print the title even if there are no maps | |
2815 | to look through. */ | |
2c6f1a39 JB |
2816 | |
2817 | void | |
c2b714de RS |
2818 | describe_map_tree (startmap, partial, shadow, prefix, title, nomenu, transl, |
2819 | always_title) | |
53c8f9fa | 2820 | Lisp_Object startmap, shadow, prefix; |
2c6f1a39 | 2821 | int partial; |
53c8f9fa | 2822 | char *title; |
af1d6f09 | 2823 | int nomenu; |
d7bf9bf5 | 2824 | int transl; |
c2b714de | 2825 | int always_title; |
2c6f1a39 | 2826 | { |
e4b6f8e3 | 2827 | Lisp_Object maps, orig_maps, seen, sub_shadows; |
e3dfcd4e | 2828 | struct gcpro gcpro1, gcpro2, gcpro3; |
07f15dfd | 2829 | int something = 0; |
53c8f9fa RS |
2830 | char *key_heading |
2831 | = "\ | |
2832 | key binding\n\ | |
2833 | --- -------\n"; | |
2c6f1a39 | 2834 | |
e4b6f8e3 | 2835 | orig_maps = maps = Faccessible_keymaps (startmap, prefix); |
925083d1 | 2836 | seen = Qnil; |
e3dfcd4e KH |
2837 | sub_shadows = Qnil; |
2838 | GCPRO3 (maps, seen, sub_shadows); | |
2c6f1a39 | 2839 | |
af1d6f09 RS |
2840 | if (nomenu) |
2841 | { | |
2842 | Lisp_Object list; | |
2843 | ||
2844 | /* Delete from MAPS each element that is for the menu bar. */ | |
03699b14 | 2845 | for (list = maps; !NILP (list); list = XCDR (list)) |
af1d6f09 RS |
2846 | { |
2847 | Lisp_Object elt, prefix, tem; | |
2848 | ||
2849 | elt = Fcar (list); | |
2850 | prefix = Fcar (elt); | |
2851 | if (XVECTOR (prefix)->size >= 1) | |
2852 | { | |
2853 | tem = Faref (prefix, make_number (0)); | |
2854 | if (EQ (tem, Qmenu_bar)) | |
2855 | maps = Fdelq (elt, maps); | |
2856 | } | |
2857 | } | |
2858 | } | |
2859 | ||
c2b714de | 2860 | if (!NILP (maps) || always_title) |
53c8f9fa RS |
2861 | { |
2862 | if (title) | |
07f15dfd RS |
2863 | { |
2864 | insert_string (title); | |
2865 | if (!NILP (prefix)) | |
2866 | { | |
2867 | insert_string (" Starting With "); | |
2868 | insert1 (Fkey_description (prefix)); | |
2869 | } | |
2870 | insert_string (":\n"); | |
2871 | } | |
53c8f9fa | 2872 | insert_string (key_heading); |
07f15dfd | 2873 | something = 1; |
53c8f9fa RS |
2874 | } |
2875 | ||
265a9e55 | 2876 | for (; !NILP (maps); maps = Fcdr (maps)) |
2c6f1a39 | 2877 | { |
e3dfcd4e | 2878 | register Lisp_Object elt, prefix, tail; |
53c8f9fa | 2879 | |
2c6f1a39 | 2880 | elt = Fcar (maps); |
53c8f9fa RS |
2881 | prefix = Fcar (elt); |
2882 | ||
2883 | sub_shadows = Qnil; | |
2884 | ||
03699b14 | 2885 | for (tail = shadow; CONSP (tail); tail = XCDR (tail)) |
2c6f1a39 | 2886 | { |
53c8f9fa RS |
2887 | Lisp_Object shmap; |
2888 | ||
03699b14 | 2889 | shmap = XCAR (tail); |
53c8f9fa RS |
2890 | |
2891 | /* If the sequence by which we reach this keymap is zero-length, | |
2892 | then the shadow map for this keymap is just SHADOW. */ | |
416349ec KH |
2893 | if ((STRINGP (prefix) && XSTRING (prefix)->size == 0) |
2894 | || (VECTORP (prefix) && XVECTOR (prefix)->size == 0)) | |
53c8f9fa RS |
2895 | ; |
2896 | /* If the sequence by which we reach this keymap actually has | |
2897 | some elements, then the sequence's definition in SHADOW is | |
2898 | what we should use. */ | |
2899 | else | |
2900 | { | |
98234407 | 2901 | shmap = Flookup_key (shmap, Fcar (elt), Qt); |
416349ec | 2902 | if (INTEGERP (shmap)) |
53c8f9fa RS |
2903 | shmap = Qnil; |
2904 | } | |
2905 | ||
2906 | /* If shmap is not nil and not a keymap, | |
2907 | it completely shadows this map, so don't | |
2908 | describe this map at all. */ | |
02067692 | 2909 | if (!NILP (shmap) && !KEYMAPP (shmap)) |
53c8f9fa RS |
2910 | goto skip; |
2911 | ||
2912 | if (!NILP (shmap)) | |
2913 | sub_shadows = Fcons (shmap, sub_shadows); | |
2c6f1a39 JB |
2914 | } |
2915 | ||
e4b6f8e3 | 2916 | /* Maps we have already listed in this loop shadow this map. */ |
54cbc3d4 | 2917 | for (tail = orig_maps; !EQ (tail, maps); tail = XCDR (tail)) |
e4b6f8e3 RS |
2918 | { |
2919 | Lisp_Object tem; | |
2920 | tem = Fequal (Fcar (XCAR (tail)), prefix); | |
54cbc3d4 | 2921 | if (!NILP (tem)) |
e4b6f8e3 RS |
2922 | sub_shadows = Fcons (XCDR (XCAR (tail)), sub_shadows); |
2923 | } | |
2924 | ||
2925 | describe_map (Fcdr (elt), prefix, | |
d7bf9bf5 | 2926 | transl ? describe_translation : describe_command, |
279a482a | 2927 | partial, sub_shadows, &seen, nomenu); |
53c8f9fa RS |
2928 | |
2929 | skip: ; | |
2c6f1a39 JB |
2930 | } |
2931 | ||
07f15dfd RS |
2932 | if (something) |
2933 | insert_string ("\n"); | |
2934 | ||
2c6f1a39 JB |
2935 | UNGCPRO; |
2936 | } | |
2937 | ||
c3f27064 KH |
2938 | static int previous_description_column; |
2939 | ||
2c6f1a39 | 2940 | static void |
d55627cc SM |
2941 | describe_command (definition, args) |
2942 | Lisp_Object definition, args; | |
2c6f1a39 JB |
2943 | { |
2944 | register Lisp_Object tem1; | |
c3f27064 KH |
2945 | int column = current_column (); |
2946 | int description_column; | |
2c6f1a39 | 2947 | |
c3f27064 KH |
2948 | /* If column 16 is no good, go to col 32; |
2949 | but don't push beyond that--go to next line instead. */ | |
2950 | if (column > 30) | |
2951 | { | |
2952 | insert_char ('\n'); | |
2953 | description_column = 32; | |
2954 | } | |
2955 | else if (column > 14 || (column > 10 && previous_description_column == 32)) | |
2956 | description_column = 32; | |
2957 | else | |
2958 | description_column = 16; | |
2959 | ||
2960 | Findent_to (make_number (description_column), make_number (1)); | |
2961 | previous_description_column = description_column; | |
2c6f1a39 | 2962 | |
416349ec | 2963 | if (SYMBOLP (definition)) |
2c6f1a39 | 2964 | { |
bff4ec1f | 2965 | XSETSTRING (tem1, XSYMBOL (definition)->name); |
2c6f1a39 | 2966 | insert1 (tem1); |
055234ef | 2967 | insert_string ("\n"); |
2c6f1a39 | 2968 | } |
d7bf9bf5 | 2969 | else if (STRINGP (definition) || VECTORP (definition)) |
24065b9c | 2970 | insert_string ("Keyboard Macro\n"); |
02067692 SM |
2971 | else if (KEYMAPP (definition)) |
2972 | insert_string ("Prefix Command\n"); | |
2c6f1a39 | 2973 | else |
02067692 | 2974 | insert_string ("??\n"); |
2c6f1a39 JB |
2975 | } |
2976 | ||
d7bf9bf5 | 2977 | static void |
d55627cc SM |
2978 | describe_translation (definition, args) |
2979 | Lisp_Object definition, args; | |
d7bf9bf5 RS |
2980 | { |
2981 | register Lisp_Object tem1; | |
2982 | ||
2983 | Findent_to (make_number (16), make_number (1)); | |
2984 | ||
2985 | if (SYMBOLP (definition)) | |
2986 | { | |
2987 | XSETSTRING (tem1, XSYMBOL (definition)->name); | |
2988 | insert1 (tem1); | |
2989 | insert_string ("\n"); | |
2990 | } | |
2991 | else if (STRINGP (definition) || VECTORP (definition)) | |
b902ac28 RS |
2992 | { |
2993 | insert1 (Fkey_description (definition)); | |
2994 | insert_string ("\n"); | |
2995 | } | |
02067692 SM |
2996 | else if (KEYMAPP (definition)) |
2997 | insert_string ("Prefix Command\n"); | |
d7bf9bf5 | 2998 | else |
02067692 | 2999 | insert_string ("??\n"); |
d7bf9bf5 RS |
3000 | } |
3001 | ||
c3c0ee93 KH |
3002 | /* Describe the contents of map MAP, assuming that this map itself is |
3003 | reached by the sequence of prefix keys KEYS (a string or vector). | |
279a482a | 3004 | PARTIAL, SHADOW, NOMENU are as in `describe_map_tree' above. */ |
2c6f1a39 JB |
3005 | |
3006 | static void | |
279a482a | 3007 | describe_map (map, keys, elt_describer, partial, shadow, seen, nomenu) |
c3c0ee93 KH |
3008 | register Lisp_Object map; |
3009 | Lisp_Object keys; | |
d55627cc | 3010 | void (*elt_describer) P_ ((Lisp_Object, Lisp_Object)); |
2c6f1a39 JB |
3011 | int partial; |
3012 | Lisp_Object shadow; | |
925083d1 | 3013 | Lisp_Object *seen; |
279a482a | 3014 | int nomenu; |
2c6f1a39 | 3015 | { |
c3c0ee93 | 3016 | Lisp_Object elt_prefix; |
53c8f9fa | 3017 | Lisp_Object tail, definition, event; |
99a225a9 | 3018 | Lisp_Object tem; |
2c6f1a39 JB |
3019 | Lisp_Object suppress; |
3020 | Lisp_Object kludge; | |
3021 | int first = 1; | |
3022 | struct gcpro gcpro1, gcpro2, gcpro3; | |
3023 | ||
6bbd7a29 GM |
3024 | suppress = Qnil; |
3025 | ||
c3c0ee93 KH |
3026 | if (!NILP (keys) && XFASTINT (Flength (keys)) > 0) |
3027 | { | |
c3c0ee93 KH |
3028 | /* Call Fkey_description first, to avoid GC bug for the other string. */ |
3029 | tem = Fkey_description (keys); | |
3030 | elt_prefix = concat2 (tem, build_string (" ")); | |
3031 | } | |
3032 | else | |
3033 | elt_prefix = Qnil; | |
3034 | ||
2c6f1a39 JB |
3035 | if (partial) |
3036 | suppress = intern ("suppress-keymap"); | |
3037 | ||
3038 | /* This vector gets used to present single keys to Flookup_key. Since | |
f5b79c1c | 3039 | that is done once per keymap element, we don't want to cons up a |
2c6f1a39 JB |
3040 | fresh vector every time. */ |
3041 | kludge = Fmake_vector (make_number (1), Qnil); | |
99a225a9 | 3042 | definition = Qnil; |
2c6f1a39 | 3043 | |
99a225a9 | 3044 | GCPRO3 (elt_prefix, definition, kludge); |
2c6f1a39 | 3045 | |
03699b14 | 3046 | for (tail = map; CONSP (tail); tail = XCDR (tail)) |
2c6f1a39 JB |
3047 | { |
3048 | QUIT; | |
2c6f1a39 | 3049 | |
03699b14 KR |
3050 | if (VECTORP (XCAR (tail)) |
3051 | || CHAR_TABLE_P (XCAR (tail))) | |
3052 | describe_vector (XCAR (tail), | |
d55627cc | 3053 | elt_prefix, Qnil, elt_describer, partial, shadow, map, |
0403641f | 3054 | (int *)0, 0); |
03699b14 | 3055 | else if (CONSP (XCAR (tail))) |
2c6f1a39 | 3056 | { |
03699b14 | 3057 | event = XCAR (XCAR (tail)); |
2c3b35b0 RS |
3058 | |
3059 | /* Ignore bindings whose "keys" are not really valid events. | |
3060 | (We get these in the frames and buffers menu.) */ | |
54cbc3d4 | 3061 | if (!(SYMBOLP (event) || INTEGERP (event))) |
c96dcc01 | 3062 | continue; |
2c3b35b0 | 3063 | |
279a482a KH |
3064 | if (nomenu && EQ (event, Qmenu_bar)) |
3065 | continue; | |
3066 | ||
03699b14 | 3067 | definition = get_keyelt (XCDR (XCAR (tail)), 0); |
2c6f1a39 | 3068 | |
f5b79c1c | 3069 | /* Don't show undefined commands or suppressed commands. */ |
99a225a9 | 3070 | if (NILP (definition)) continue; |
416349ec | 3071 | if (SYMBOLP (definition) && partial) |
f5b79c1c | 3072 | { |
99a225a9 RS |
3073 | tem = Fget (definition, suppress); |
3074 | if (!NILP (tem)) | |
f5b79c1c JB |
3075 | continue; |
3076 | } | |
2c6f1a39 | 3077 | |
f5b79c1c JB |
3078 | /* Don't show a command that isn't really visible |
3079 | because a local definition of the same key shadows it. */ | |
2c6f1a39 | 3080 | |
49801145 | 3081 | ASET (kludge, 0, event); |
f5b79c1c JB |
3082 | if (!NILP (shadow)) |
3083 | { | |
53c8f9fa | 3084 | tem = shadow_lookup (shadow, kludge, Qt); |
f5b79c1c JB |
3085 | if (!NILP (tem)) continue; |
3086 | } | |
3087 | ||
c3c0ee93 | 3088 | tem = Flookup_key (map, kludge, Qt); |
54cbc3d4 | 3089 | if (!EQ (tem, definition)) continue; |
99a225a9 | 3090 | |
f5b79c1c JB |
3091 | if (first) |
3092 | { | |
c3f27064 | 3093 | previous_description_column = 0; |
f5b79c1c JB |
3094 | insert ("\n", 1); |
3095 | first = 0; | |
3096 | } | |
2c6f1a39 | 3097 | |
f5b79c1c JB |
3098 | if (!NILP (elt_prefix)) |
3099 | insert1 (elt_prefix); | |
2c6f1a39 | 3100 | |
99a225a9 | 3101 | /* THIS gets the string to describe the character EVENT. */ |
c1848a97 | 3102 | insert1 (Fsingle_key_description (event, Qnil)); |
2c6f1a39 | 3103 | |
f5b79c1c JB |
3104 | /* Print a description of the definition of this character. |
3105 | elt_describer will take care of spacing out far enough | |
3106 | for alignment purposes. */ | |
d55627cc | 3107 | (*elt_describer) (definition, Qnil); |
f5b79c1c | 3108 | } |
03699b14 | 3109 | else if (EQ (XCAR (tail), Qkeymap)) |
925083d1 KH |
3110 | { |
3111 | /* The same keymap might be in the structure twice, if we're | |
3112 | using an inherited keymap. So skip anything we've already | |
3113 | encountered. */ | |
3114 | tem = Fassq (tail, *seen); | |
03699b14 | 3115 | if (CONSP (tem) && !NILP (Fequal (XCAR (tem), keys))) |
925083d1 KH |
3116 | break; |
3117 | *seen = Fcons (Fcons (tail, keys), *seen); | |
3118 | } | |
2c6f1a39 JB |
3119 | } |
3120 | ||
3121 | UNGCPRO; | |
3122 | } | |
3123 | ||
69248761 | 3124 | static void |
d55627cc SM |
3125 | describe_vector_princ (elt, fun) |
3126 | Lisp_Object elt, fun; | |
2c6f1a39 | 3127 | { |
81fa9e2f | 3128 | Findent_to (make_number (16), make_number (1)); |
d55627cc | 3129 | call1 (fun, elt); |
ad4ec84a | 3130 | Fterpri (Qnil); |
2c6f1a39 JB |
3131 | } |
3132 | ||
d55627cc | 3133 | DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 2, 0, |
335c5470 PJ |
3134 | doc: /* Insert a description of contents of VECTOR. |
3135 | This is text showing the elements of vector matched against indices. */) | |
d55627cc SM |
3136 | (vector, describer) |
3137 | Lisp_Object vector, describer; | |
2c6f1a39 | 3138 | { |
ad4ec84a | 3139 | int count = specpdl_ptr - specpdl; |
d55627cc SM |
3140 | if (NILP (describer)) |
3141 | describer = intern ("princ"); | |
ad4ec84a | 3142 | specbind (Qstandard_output, Fcurrent_buffer ()); |
b7826503 | 3143 | CHECK_VECTOR_OR_CHAR_TABLE (vector); |
d55627cc | 3144 | describe_vector (vector, Qnil, describer, describe_vector_princ, 0, |
0403641f | 3145 | Qnil, Qnil, (int *)0, 0); |
ad4ec84a RS |
3146 | |
3147 | return unbind_to (count, Qnil); | |
2c6f1a39 JB |
3148 | } |
3149 | ||
352e5dea RS |
3150 | /* Insert in the current buffer a description of the contents of VECTOR. |
3151 | We call ELT_DESCRIBER to insert the description of one value found | |
3152 | in VECTOR. | |
3153 | ||
3154 | ELT_PREFIX describes what "comes before" the keys or indices defined | |
0403641f RS |
3155 | by this vector. This is a human-readable string whose size |
3156 | is not necessarily related to the situation. | |
352e5dea RS |
3157 | |
3158 | If the vector is in a keymap, ELT_PREFIX is a prefix key which | |
3159 | leads to this keymap. | |
3160 | ||
3161 | If the vector is a chartable, ELT_PREFIX is the vector | |
3162 | of bytes that lead to the character set or portion of a character | |
3163 | set described by this chartable. | |
3164 | ||
3165 | If PARTIAL is nonzero, it means do not mention suppressed commands | |
3166 | (that assumes the vector is in a keymap). | |
3167 | ||
3168 | SHADOW is a list of keymaps that shadow this map. | |
3169 | If it is non-nil, then we look up the key in those maps | |
3170 | and we don't mention it now if it is defined by any of them. | |
3171 | ||
3172 | ENTIRE_MAP is the keymap in which this vector appears. | |
3173 | If the definition in effect in the whole map does not match | |
0403641f RS |
3174 | the one in this vector, we ignore this one. |
3175 | ||
3176 | When describing a sub-char-table, INDICES is a list of | |
3177 | indices at higher levels in this char-table, | |
d55627cc SM |
3178 | and CHAR_TABLE_DEPTH says how many levels down we have gone. |
3179 | ||
3180 | ARGS is simply passed as the second argument to ELT_DESCRIBER. */ | |
352e5dea | 3181 | |
71a956a6 | 3182 | void |
d55627cc | 3183 | describe_vector (vector, elt_prefix, args, elt_describer, |
0403641f RS |
3184 | partial, shadow, entire_map, |
3185 | indices, char_table_depth) | |
2c6f1a39 | 3186 | register Lisp_Object vector; |
d55627cc SM |
3187 | Lisp_Object elt_prefix, args; |
3188 | void (*elt_describer) P_ ((Lisp_Object, Lisp_Object)); | |
2c6f1a39 JB |
3189 | int partial; |
3190 | Lisp_Object shadow; | |
32bfcae1 | 3191 | Lisp_Object entire_map; |
0403641f RS |
3192 | int *indices; |
3193 | int char_table_depth; | |
2c6f1a39 | 3194 | { |
32bfcae1 KH |
3195 | Lisp_Object definition; |
3196 | Lisp_Object tem2; | |
2c6f1a39 JB |
3197 | register int i; |
3198 | Lisp_Object suppress; | |
3199 | Lisp_Object kludge; | |
3200 | int first = 1; | |
47935df1 | 3201 | struct gcpro gcpro1, gcpro2, gcpro3; |
a98f1d1d KH |
3202 | /* Range of elements to be handled. */ |
3203 | int from, to; | |
a98f1d1d KH |
3204 | /* A flag to tell if a leaf in this level of char-table is not a |
3205 | generic character (i.e. a complete multibyte character). */ | |
3206 | int complete_char; | |
0403641f RS |
3207 | int character; |
3208 | int starting_i; | |
3209 | ||
6bbd7a29 GM |
3210 | suppress = Qnil; |
3211 | ||
0403641f | 3212 | if (indices == 0) |
2e34157c | 3213 | indices = (int *) alloca (3 * sizeof (int)); |
2c6f1a39 | 3214 | |
32bfcae1 | 3215 | definition = Qnil; |
2c6f1a39 JB |
3216 | |
3217 | /* This vector gets used to present single keys to Flookup_key. Since | |
3218 | that is done once per vector element, we don't want to cons up a | |
3219 | fresh vector every time. */ | |
3220 | kludge = Fmake_vector (make_number (1), Qnil); | |
0403641f | 3221 | GCPRO3 (elt_prefix, definition, kludge); |
2c6f1a39 JB |
3222 | |
3223 | if (partial) | |
3224 | suppress = intern ("suppress-keymap"); | |
3225 | ||
a98f1d1d KH |
3226 | if (CHAR_TABLE_P (vector)) |
3227 | { | |
0403641f | 3228 | if (char_table_depth == 0) |
a98f1d1d | 3229 | { |
a1942d88 | 3230 | /* VECTOR is a top level char-table. */ |
0403641f | 3231 | complete_char = 1; |
a98f1d1d KH |
3232 | from = 0; |
3233 | to = CHAR_TABLE_ORDINARY_SLOTS; | |
3234 | } | |
3235 | else | |
3236 | { | |
a1942d88 | 3237 | /* VECTOR is a sub char-table. */ |
0403641f RS |
3238 | if (char_table_depth >= 3) |
3239 | /* A char-table is never that deep. */ | |
a1942d88 | 3240 | error ("Too deep char table"); |
a98f1d1d | 3241 | |
a98f1d1d | 3242 | complete_char |
0403641f RS |
3243 | = (CHARSET_VALID_P (indices[0]) |
3244 | && ((CHARSET_DIMENSION (indices[0]) == 1 | |
3245 | && char_table_depth == 1) | |
3246 | || char_table_depth == 2)); | |
a98f1d1d KH |
3247 | |
3248 | /* Meaningful elements are from 32th to 127th. */ | |
3249 | from = 32; | |
a1942d88 | 3250 | to = SUB_CHAR_TABLE_ORDINARY_SLOTS; |
a98f1d1d | 3251 | } |
a98f1d1d KH |
3252 | } |
3253 | else | |
3254 | { | |
a98f1d1d | 3255 | /* This does the right thing for ordinary vectors. */ |
0403641f RS |
3256 | |
3257 | complete_char = 1; | |
3258 | from = 0; | |
3259 | to = XVECTOR (vector)->size; | |
a98f1d1d | 3260 | } |
b5585f5c | 3261 | |
a98f1d1d | 3262 | for (i = from; i < to; i++) |
2c6f1a39 JB |
3263 | { |
3264 | QUIT; | |
2c6f1a39 | 3265 | |
a1942d88 KH |
3266 | if (CHAR_TABLE_P (vector)) |
3267 | { | |
0403641f RS |
3268 | if (char_table_depth == 0 && i >= CHAR_TABLE_SINGLE_BYTE_SLOTS) |
3269 | complete_char = 0; | |
3270 | ||
a1942d88 KH |
3271 | if (i >= CHAR_TABLE_SINGLE_BYTE_SLOTS |
3272 | && !CHARSET_DEFINED_P (i - 128)) | |
3273 | continue; | |
0403641f RS |
3274 | |
3275 | definition | |
3276 | = get_keyelt (XCHAR_TABLE (vector)->contents[i], 0); | |
a1942d88 KH |
3277 | } |
3278 | else | |
49801145 | 3279 | definition = get_keyelt (AREF (vector, i), 0); |
2c6f1a39 | 3280 | |
d55627cc | 3281 | if (NILP (definition)) continue; |
cc3e6465 | 3282 | |
2c6f1a39 | 3283 | /* Don't mention suppressed commands. */ |
32bfcae1 | 3284 | if (SYMBOLP (definition) && partial) |
2c6f1a39 | 3285 | { |
a98f1d1d KH |
3286 | Lisp_Object tem; |
3287 | ||
3288 | tem = Fget (definition, suppress); | |
3289 | ||
3290 | if (!NILP (tem)) continue; | |
2c6f1a39 JB |
3291 | } |
3292 | ||
0403641f RS |
3293 | /* Set CHARACTER to the character this entry describes, if any. |
3294 | Also update *INDICES. */ | |
3295 | if (CHAR_TABLE_P (vector)) | |
3296 | { | |
3297 | indices[char_table_depth] = i; | |
3298 | ||
3299 | if (char_table_depth == 0) | |
3300 | { | |
3301 | character = i; | |
3302 | indices[0] = i - 128; | |
3303 | } | |
3304 | else if (complete_char) | |
3305 | { | |
54e03a4a | 3306 | character = MAKE_CHAR (indices[0], indices[1], indices[2]); |
0403641f RS |
3307 | } |
3308 | else | |
3309 | character = 0; | |
3310 | } | |
3311 | else | |
3312 | character = i; | |
3313 | ||
32bfcae1 | 3314 | /* If this binding is shadowed by some other map, ignore it. */ |
0403641f | 3315 | if (!NILP (shadow) && complete_char) |
2c6f1a39 JB |
3316 | { |
3317 | Lisp_Object tem; | |
3318 | ||
49801145 | 3319 | ASET (kludge, 0, make_number (character)); |
53c8f9fa | 3320 | tem = shadow_lookup (shadow, kludge, Qt); |
2c6f1a39 | 3321 | |
265a9e55 | 3322 | if (!NILP (tem)) continue; |
2c6f1a39 JB |
3323 | } |
3324 | ||
32bfcae1 KH |
3325 | /* Ignore this definition if it is shadowed by an earlier |
3326 | one in the same keymap. */ | |
0403641f | 3327 | if (!NILP (entire_map) && complete_char) |
32bfcae1 KH |
3328 | { |
3329 | Lisp_Object tem; | |
3330 | ||
49801145 | 3331 | ASET (kludge, 0, make_number (character)); |
32bfcae1 KH |
3332 | tem = Flookup_key (entire_map, kludge, Qt); |
3333 | ||
54cbc3d4 | 3334 | if (!EQ (tem, definition)) |
32bfcae1 KH |
3335 | continue; |
3336 | } | |
3337 | ||
2c6f1a39 JB |
3338 | if (first) |
3339 | { | |
0403641f | 3340 | if (char_table_depth == 0) |
a98f1d1d | 3341 | insert ("\n", 1); |
2c6f1a39 JB |
3342 | first = 0; |
3343 | } | |
3344 | ||
0403641f RS |
3345 | /* For a sub char-table, show the depth by indentation. |
3346 | CHAR_TABLE_DEPTH can be greater than 0 only for a char-table. */ | |
3347 | if (char_table_depth > 0) | |
3348 | insert (" ", char_table_depth * 2); /* depth is 1 or 2. */ | |
a98f1d1d | 3349 | |
0403641f RS |
3350 | /* Output the prefix that applies to every entry in this map. */ |
3351 | if (!NILP (elt_prefix)) | |
3352 | insert1 (elt_prefix); | |
a98f1d1d | 3353 | |
0403641f RS |
3354 | /* Insert or describe the character this slot is for, |
3355 | or a description of what it is for. */ | |
3356 | if (SUB_CHAR_TABLE_P (vector)) | |
a1942d88 | 3357 | { |
0403641f RS |
3358 | if (complete_char) |
3359 | insert_char (character); | |
3360 | else | |
3361 | { | |
3362 | /* We need an octal representation for this block of | |
3363 | characters. */ | |
542d7fd2 RS |
3364 | char work[16]; |
3365 | sprintf (work, "(row %d)", i); | |
3366 | insert (work, strlen (work)); | |
0403641f RS |
3367 | } |
3368 | } | |
3369 | else if (CHAR_TABLE_P (vector)) | |
3370 | { | |
3371 | if (complete_char) | |
c1848a97 | 3372 | insert1 (Fsingle_key_description (make_number (character), Qnil)); |
a1942d88 KH |
3373 | else |
3374 | { | |
3375 | /* Print the information for this character set. */ | |
3376 | insert_string ("<"); | |
3377 | tem2 = CHARSET_TABLE_INFO (i - 128, CHARSET_SHORT_NAME_IDX); | |
3378 | if (STRINGP (tem2)) | |
f3ba5409 | 3379 | insert_from_string (tem2, 0, 0, XSTRING (tem2)->size, |
fc932ac6 | 3380 | STRING_BYTES (XSTRING (tem2)), 0); |
a1942d88 KH |
3381 | else |
3382 | insert ("?", 1); | |
3383 | insert (">", 1); | |
3384 | } | |
3385 | } | |
352e5dea RS |
3386 | else |
3387 | { | |
c1848a97 | 3388 | insert1 (Fsingle_key_description (make_number (character), Qnil)); |
a98f1d1d | 3389 | } |
352e5dea | 3390 | |
a1942d88 | 3391 | /* If we find a sub char-table within a char-table, |
a98f1d1d KH |
3392 | scan it recursively; it defines the details for |
3393 | a character set or a portion of a character set. */ | |
f3ba5409 | 3394 | if (CHAR_TABLE_P (vector) && SUB_CHAR_TABLE_P (definition)) |
a98f1d1d | 3395 | { |
a98f1d1d | 3396 | insert ("\n", 1); |
d55627cc | 3397 | describe_vector (definition, elt_prefix, args, elt_describer, |
0403641f RS |
3398 | partial, shadow, entire_map, |
3399 | indices, char_table_depth + 1); | |
a98f1d1d | 3400 | continue; |
352e5dea | 3401 | } |
2c6f1a39 | 3402 | |
0403641f RS |
3403 | starting_i = i; |
3404 | ||
542d7fd2 | 3405 | /* Find all consecutive characters or rows that have the same |
a1942d88 KH |
3406 | definition. But, for elements of a top level char table, if |
3407 | they are for charsets, we had better describe one by one even | |
3408 | if they have the same definition. */ | |
3409 | if (CHAR_TABLE_P (vector)) | |
3410 | { | |
0403641f RS |
3411 | int limit = to; |
3412 | ||
3413 | if (char_table_depth == 0) | |
3414 | limit = CHAR_TABLE_SINGLE_BYTE_SLOTS; | |
3415 | ||
3416 | while (i + 1 < limit | |
3417 | && (tem2 = get_keyelt (XCHAR_TABLE (vector)->contents[i + 1], 0), | |
3418 | !NILP (tem2)) | |
3419 | && !NILP (Fequal (tem2, definition))) | |
3420 | i++; | |
a1942d88 KH |
3421 | } |
3422 | else | |
0403641f | 3423 | while (i + 1 < to |
49801145 | 3424 | && (tem2 = get_keyelt (AREF (vector, i + 1), 0), |
a1942d88 KH |
3425 | !NILP (tem2)) |
3426 | && !NILP (Fequal (tem2, definition))) | |
3427 | i++; | |
3428 | ||
2c6f1a39 JB |
3429 | |
3430 | /* If we have a range of more than one character, | |
3431 | print where the range reaches to. */ | |
3432 | ||
0403641f | 3433 | if (i != starting_i) |
2c6f1a39 JB |
3434 | { |
3435 | insert (" .. ", 4); | |
0403641f RS |
3436 | |
3437 | if (!NILP (elt_prefix)) | |
3438 | insert1 (elt_prefix); | |
3439 | ||
352e5dea RS |
3440 | if (CHAR_TABLE_P (vector)) |
3441 | { | |
0403641f | 3442 | if (char_table_depth == 0) |
a98f1d1d | 3443 | { |
c1848a97 | 3444 | insert1 (Fsingle_key_description (make_number (i), Qnil)); |
a98f1d1d | 3445 | } |
0403641f | 3446 | else if (complete_char) |
352e5dea | 3447 | { |
0403641f | 3448 | indices[char_table_depth] = i; |
54e03a4a | 3449 | character = MAKE_CHAR (indices[0], indices[1], indices[2]); |
0403641f | 3450 | insert_char (character); |
352e5dea RS |
3451 | } |
3452 | else | |
3453 | { | |
542d7fd2 RS |
3454 | /* We need an octal representation for this block of |
3455 | characters. */ | |
3456 | char work[16]; | |
3457 | sprintf (work, "(row %d)", i); | |
3458 | insert (work, strlen (work)); | |
352e5dea RS |
3459 | } |
3460 | } | |
3461 | else | |
3462 | { | |
c1848a97 | 3463 | insert1 (Fsingle_key_description (make_number (i), Qnil)); |
352e5dea | 3464 | } |
2c6f1a39 JB |
3465 | } |
3466 | ||
3467 | /* Print a description of the definition of this character. | |
3468 | elt_describer will take care of spacing out far enough | |
3469 | for alignment purposes. */ | |
d55627cc | 3470 | (*elt_describer) (definition, args); |
2c6f1a39 JB |
3471 | } |
3472 | ||
a1942d88 | 3473 | /* For (sub) char-table, print `defalt' slot at last. */ |
a98f1d1d KH |
3474 | if (CHAR_TABLE_P (vector) && !NILP (XCHAR_TABLE (vector)->defalt)) |
3475 | { | |
0403641f | 3476 | insert (" ", char_table_depth * 2); |
a98f1d1d | 3477 | insert_string ("<<default>>"); |
d55627cc | 3478 | (*elt_describer) (XCHAR_TABLE (vector)->defalt, args); |
a98f1d1d KH |
3479 | } |
3480 | ||
2c6f1a39 JB |
3481 | UNGCPRO; |
3482 | } | |
3483 | \f | |
cc0a8174 | 3484 | /* Apropos - finding all symbols whose names match a regexp. */ |
2c6f1a39 JB |
3485 | Lisp_Object apropos_predicate; |
3486 | Lisp_Object apropos_accumulate; | |
3487 | ||
3488 | static void | |
3489 | apropos_accum (symbol, string) | |
3490 | Lisp_Object symbol, string; | |
3491 | { | |
3492 | register Lisp_Object tem; | |
3493 | ||
3494 | tem = Fstring_match (string, Fsymbol_name (symbol), Qnil); | |
265a9e55 | 3495 | if (!NILP (tem) && !NILP (apropos_predicate)) |
2c6f1a39 | 3496 | tem = call1 (apropos_predicate, symbol); |
265a9e55 | 3497 | if (!NILP (tem)) |
2c6f1a39 JB |
3498 | apropos_accumulate = Fcons (symbol, apropos_accumulate); |
3499 | } | |
3500 | ||
3501 | DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0, | |
335c5470 PJ |
3502 | doc: /* Show all symbols whose names contain match for REGEXP. |
3503 | If optional 2nd arg PREDICATE is non-nil, (funcall PREDICATE SYMBOL) is done | |
3504 | for each symbol and a symbol is mentioned only if that returns non-nil. | |
3505 | Return list of symbols found. */) | |
3506 | (regexp, predicate) | |
88539837 | 3507 | Lisp_Object regexp, predicate; |
2c6f1a39 JB |
3508 | { |
3509 | struct gcpro gcpro1, gcpro2; | |
b7826503 | 3510 | CHECK_STRING (regexp); |
88539837 | 3511 | apropos_predicate = predicate; |
2c6f1a39 JB |
3512 | GCPRO2 (apropos_predicate, apropos_accumulate); |
3513 | apropos_accumulate = Qnil; | |
88539837 | 3514 | map_obarray (Vobarray, apropos_accum, regexp); |
2c6f1a39 JB |
3515 | apropos_accumulate = Fsort (apropos_accumulate, Qstring_lessp); |
3516 | UNGCPRO; | |
3517 | return apropos_accumulate; | |
3518 | } | |
3519 | \f | |
dfcf069d | 3520 | void |
2c6f1a39 JB |
3521 | syms_of_keymap () |
3522 | { | |
2c6f1a39 JB |
3523 | Qkeymap = intern ("keymap"); |
3524 | staticpro (&Qkeymap); | |
3525 | ||
0403641f RS |
3526 | /* Now we are ready to set up this property, so we can |
3527 | create char tables. */ | |
3528 | Fput (Qkeymap, Qchar_table_extra_slots, make_number (0)); | |
3529 | ||
3530 | /* Initialize the keymaps standardly used. | |
3531 | Each one is the value of a Lisp variable, and is also | |
3532 | pointed to by a C variable */ | |
2c6f1a39 | 3533 | |
0403641f | 3534 | global_map = Fmake_keymap (Qnil); |
2c6f1a39 JB |
3535 | Fset (intern ("global-map"), global_map); |
3536 | ||
44bff953 | 3537 | current_global_map = global_map; |
a3e99933 | 3538 | staticpro (&global_map); |
44bff953 RS |
3539 | staticpro (¤t_global_map); |
3540 | ||
ce6e5d0b | 3541 | meta_map = Fmake_keymap (Qnil); |
2c6f1a39 JB |
3542 | Fset (intern ("esc-map"), meta_map); |
3543 | Ffset (intern ("ESC-prefix"), meta_map); | |
3544 | ||
ce6e5d0b | 3545 | control_x_map = Fmake_keymap (Qnil); |
2c6f1a39 JB |
3546 | Fset (intern ("ctl-x-map"), control_x_map); |
3547 | Ffset (intern ("Control-X-prefix"), control_x_map); | |
3548 | ||
2fae9111 RS |
3549 | exclude_keys |
3550 | = Fcons (Fcons (build_string ("DEL"), build_string ("\\d")), | |
3551 | Fcons (Fcons (build_string ("TAB"), build_string ("\\t")), | |
3552 | Fcons (Fcons (build_string ("RET"), build_string ("\\r")), | |
3553 | Fcons (Fcons (build_string ("ESC"), build_string ("\\e")), | |
3554 | Fcons (Fcons (build_string ("SPC"), build_string (" ")), | |
3555 | Qnil))))); | |
3556 | staticpro (&exclude_keys); | |
3557 | ||
107fd03d | 3558 | DEFVAR_LISP ("define-key-rebound-commands", &Vdefine_key_rebound_commands, |
335c5470 PJ |
3559 | doc: /* List of commands given new key bindings recently. |
3560 | This is used for internal purposes during Emacs startup; | |
3561 | don't alter it yourself. */); | |
107fd03d RS |
3562 | Vdefine_key_rebound_commands = Qt; |
3563 | ||
2c6f1a39 | 3564 | DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map, |
335c5470 | 3565 | doc: /* Default keymap to use when reading from the minibuffer. */); |
ce6e5d0b | 3566 | Vminibuffer_local_map = Fmake_sparse_keymap (Qnil); |
2c6f1a39 JB |
3567 | |
3568 | DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map, | |
335c5470 | 3569 | doc: /* Local keymap for the minibuffer when spaces are not allowed. */); |
ce6e5d0b | 3570 | Vminibuffer_local_ns_map = Fmake_sparse_keymap (Qnil); |
2b6748c0 | 3571 | Fset_keymap_parent (Vminibuffer_local_ns_map, Vminibuffer_local_map); |
2c6f1a39 JB |
3572 | |
3573 | DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map, | |
335c5470 | 3574 | doc: /* Local keymap for minibuffer input with completion. */); |
ce6e5d0b | 3575 | Vminibuffer_local_completion_map = Fmake_sparse_keymap (Qnil); |
2b6748c0 | 3576 | Fset_keymap_parent (Vminibuffer_local_completion_map, Vminibuffer_local_map); |
2c6f1a39 JB |
3577 | |
3578 | DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map, | |
335c5470 | 3579 | doc: /* Local keymap for minibuffer input with completion, for exact match. */); |
ce6e5d0b | 3580 | Vminibuffer_local_must_match_map = Fmake_sparse_keymap (Qnil); |
2b6748c0 SM |
3581 | Fset_keymap_parent (Vminibuffer_local_must_match_map, |
3582 | Vminibuffer_local_completion_map); | |
2c6f1a39 | 3583 | |
cc0a8174 | 3584 | DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist, |
335c5470 PJ |
3585 | doc: /* Alist of keymaps to use for minor modes. |
3586 | Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read | |
3587 | key sequences and look up bindings iff VARIABLE's value is non-nil. | |
3588 | If two active keymaps bind the same key, the keymap appearing earlier | |
3589 | in the list takes precedence. */); | |
cc0a8174 JB |
3590 | Vminor_mode_map_alist = Qnil; |
3591 | ||
dd9cda06 | 3592 | DEFVAR_LISP ("minor-mode-overriding-map-alist", &Vminor_mode_overriding_map_alist, |
335c5470 PJ |
3593 | doc: /* Alist of keymaps to use for minor modes, in current major mode. |
3594 | This variable is a alist just like `minor-mode-map-alist', and it is | |
3595 | used the same way (and before `minor-mode-map-alist'); however, | |
3596 | it is provided for major modes to bind locally. */); | |
dd9cda06 RS |
3597 | Vminor_mode_overriding_map_alist = Qnil; |
3598 | ||
99cbcaca KS |
3599 | DEFVAR_LISP ("emulation-mode-map-alists", &Vemulation_mode_map_alists, |
3600 | doc: /* List of keymap alists to use for emulations modes. | |
3601 | It is intended for modes or packages using multiple minor-mode keymaps. | |
3602 | Each element is a keymap alist just like `minor-mode-map-alist', or a | |
3603 | symbol with a variable binding which is a keymap alist, and it is used | |
3604 | the same way. The "active" keymaps in each alist are used before | |
3605 | `minor-mode-map-alist' and `minor-mode-overriding-map-alist'. */); | |
3606 | Vemulation_mode_map_alists = Qnil; | |
3607 | ||
3608 | ||
6bbbd9b0 | 3609 | DEFVAR_LISP ("function-key-map", &Vfunction_key_map, |
335c5470 PJ |
3610 | doc: /* Keymap mapping ASCII function key sequences onto their preferred forms. |
3611 | This allows Emacs to recognize function keys sent from ASCII | |
3612 | terminals at any point in a key sequence. | |
3613 | ||
3614 | The `read-key-sequence' function replaces any subsequence bound by | |
3615 | `function-key-map' with its binding. More precisely, when the active | |
3616 | keymaps have no binding for the current key sequence but | |
3617 | `function-key-map' binds a suffix of the sequence to a vector or string, | |
3618 | `read-key-sequence' replaces the matching suffix with its binding, and | |
3619 | continues with the new sequence. | |
3620 | ||
3621 | The events that come from bindings in `function-key-map' are not | |
3622 | themselves looked up in `function-key-map'. | |
3623 | ||
3624 | For example, suppose `function-key-map' binds `ESC O P' to [f1]. | |
3625 | Typing `ESC O P' to `read-key-sequence' would return [f1]. Typing | |
3626 | `C-x ESC O P' would return [?\\C-x f1]. If [f1] were a prefix | |
3627 | key, typing `ESC O P x' would return [f1 x]. */); | |
ce6e5d0b | 3628 | Vfunction_key_map = Fmake_sparse_keymap (Qnil); |
6bbbd9b0 | 3629 | |
d7bf9bf5 | 3630 | DEFVAR_LISP ("key-translation-map", &Vkey_translation_map, |
335c5470 PJ |
3631 | doc: /* Keymap of key translations that can override keymaps. |
3632 | This keymap works like `function-key-map', but comes after that, | |
3633 | and applies even for keys that have ordinary bindings. */); | |
d7bf9bf5 RS |
3634 | Vkey_translation_map = Qnil; |
3635 | ||
2c6f1a39 JB |
3636 | Qsingle_key_description = intern ("single-key-description"); |
3637 | staticpro (&Qsingle_key_description); | |
3638 | ||
3639 | Qkey_description = intern ("key-description"); | |
3640 | staticpro (&Qkey_description); | |
3641 | ||
3642 | Qkeymapp = intern ("keymapp"); | |
3643 | staticpro (&Qkeymapp); | |
3644 | ||
2fc66973 JB |
3645 | Qnon_ascii = intern ("non-ascii"); |
3646 | staticpro (&Qnon_ascii); | |
3647 | ||
a3fc8840 RS |
3648 | Qmenu_item = intern ("menu-item"); |
3649 | staticpro (&Qmenu_item); | |
3650 | ||
a1df473f KS |
3651 | Qremap = intern ("remap"); |
3652 | staticpro (&Qremap); | |
3653 | ||
3654 | remap_command_vector = Fmake_vector (make_number (2), Qremap); | |
3655 | staticpro (&remap_command_vector); | |
3656 | ||
1e7d1ab0 SM |
3657 | where_is_cache_keymaps = Qt; |
3658 | where_is_cache = Qnil; | |
3659 | staticpro (&where_is_cache); | |
3660 | staticpro (&where_is_cache_keymaps); | |
3661 | ||
2c6f1a39 | 3662 | defsubr (&Skeymapp); |
7d58ed99 | 3663 | defsubr (&Skeymap_parent); |
54cbc3d4 | 3664 | defsubr (&Skeymap_prompt); |
7d58ed99 | 3665 | defsubr (&Sset_keymap_parent); |
2c6f1a39 JB |
3666 | defsubr (&Smake_keymap); |
3667 | defsubr (&Smake_sparse_keymap); | |
3668 | defsubr (&Scopy_keymap); | |
a1df473f | 3669 | defsubr (&Sremap_command); |
2c6f1a39 JB |
3670 | defsubr (&Skey_binding); |
3671 | defsubr (&Slocal_key_binding); | |
3672 | defsubr (&Sglobal_key_binding); | |
cc0a8174 | 3673 | defsubr (&Sminor_mode_key_binding); |
2c6f1a39 JB |
3674 | defsubr (&Sdefine_key); |
3675 | defsubr (&Slookup_key); | |
2c6f1a39 JB |
3676 | defsubr (&Sdefine_prefix_command); |
3677 | defsubr (&Suse_global_map); | |
3678 | defsubr (&Suse_local_map); | |
3679 | defsubr (&Scurrent_local_map); | |
3680 | defsubr (&Scurrent_global_map); | |
cc0a8174 | 3681 | defsubr (&Scurrent_minor_mode_maps); |
54cbc3d4 | 3682 | defsubr (&Scurrent_active_maps); |
2c6f1a39 JB |
3683 | defsubr (&Saccessible_keymaps); |
3684 | defsubr (&Skey_description); | |
3685 | defsubr (&Sdescribe_vector); | |
3686 | defsubr (&Ssingle_key_description); | |
3687 | defsubr (&Stext_char_description); | |
3688 | defsubr (&Swhere_is_internal); | |
54cbc3d4 | 3689 | defsubr (&Sdescribe_buffer_bindings); |
2c6f1a39 JB |
3690 | defsubr (&Sapropos_internal); |
3691 | } | |
3692 | ||
dfcf069d | 3693 | void |
2c6f1a39 JB |
3694 | keys_of_keymap () |
3695 | { | |
2c6f1a39 JB |
3696 | initial_define_key (global_map, 033, "ESC-prefix"); |
3697 | initial_define_key (global_map, Ctl('X'), "Control-X-prefix"); | |
3698 | } |