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