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