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