Mercurial > hg > xemacs-beta
annotate src/keymap.c @ 5117:3742ea8250b5 ben-lisp-object ben-lisp-object-final-ws-year-2005
Checking in final CVS version of workspace 'ben-lisp-object'
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Sat, 26 Dec 2009 00:20:27 -0600 |
parents | facf3239ba30 |
children | e0db3c197671 |
rev | line source |
---|---|
428 | 1 /* Manipulation of keymaps |
2 Copyright (C) 1985, 1991-1995 Free Software Foundation, Inc. | |
3 Copyright (C) 1995 Board of Trustees, University of Illinois. | |
4 Copyright (C) 1995 Sun Microsystems, Inc. | |
793 | 5 Copyright (C) 2001, 2002 Ben Wing. |
428 | 6 Totally redesigned by jwz in 1991. |
7 | |
8 This file is part of XEmacs. | |
9 | |
10 XEmacs is free software; you can redistribute it and/or modify it | |
11 under the terms of the GNU General Public License as published by the | |
12 Free Software Foundation; either version 2, or (at your option) any | |
13 later version. | |
14 | |
15 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
18 for more details. | |
19 | |
20 You should have received a copy of the GNU General Public License | |
21 along with XEmacs; see the file COPYING. If not, write to | |
22 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 Boston, MA 02111-1307, USA. */ | |
24 | |
25 /* Synched up with: Mule 2.0. Not synched with FSF. Substantially | |
26 different from FSF. */ | |
27 | |
28 | |
29 #include <config.h> | |
30 #include "lisp.h" | |
31 | |
32 #include "buffer.h" | |
33 #include "bytecode.h" | |
872 | 34 #include "console-impl.h" |
428 | 35 #include "elhash.h" |
36 #include "events.h" | |
872 | 37 #include "extents.h" |
428 | 38 #include "frame.h" |
39 #include "insdel.h" | |
40 #include "keymap.h" | |
41 #include "window.h" | |
42 | |
43 | |
44 /* A keymap contains six slots: | |
45 | |
46 parents Ordered list of keymaps to search after | |
47 this one if no match is found. | |
48 Keymaps can thus be arranged in a hierarchy. | |
49 | |
50 table A hash table, hashing keysyms to their bindings. | |
51 It will be one of the following: | |
52 | |
3025 | 53 -- a symbol, e.g. `home' |
428 | 54 -- a character, representing something printable |
55 (not ?\C-c meaning C-c, for instance) | |
56 -- an integer representing a modifier combination | |
57 | |
58 inverse_table A hash table, hashing bindings to the list of keysyms | |
59 in this keymap which are bound to them. This is to make | |
60 the Fwhere_is_internal() function be fast. It needs to be | |
61 fast because we want to be able to call it in realtime to | |
62 update the keyboard-equivalents on the pulldown menus. | |
63 Values of the table are either atoms (keysyms) | |
64 or a dotted list of keysyms. | |
65 | |
66 sub_maps_cache An alist; for each entry in this keymap whose binding is | |
67 a keymap (that is, Fkeymapp()) this alist associates that | |
68 keysym with that binding. This is used to optimize both | |
69 Fwhere_is_internal() and Faccessible_keymaps(). This slot | |
70 gets set to the symbol `t' every time a change is made to | |
71 this keymap, causing it to be recomputed when next needed. | |
72 | |
73 prompt See `set-keymap-prompt'. | |
74 | |
75 default_binding See `set-keymap-default-binding'. | |
76 | |
77 Sequences of keys are stored in the obvious way: if the sequence of keys | |
78 "abc" was bound to some command `foo', the hierarchy would look like | |
79 | |
80 keymap-1: associates "a" with keymap-2 | |
81 keymap-2: associates "b" with keymap-3 | |
82 keymap-3: associates "c" with foo | |
83 | |
84 However, bucky bits ("modifiers" to the X-minded) are represented in the | |
85 keymap hierarchy as well. (This lets us use EQable objects as hash keys.) | |
86 Each combination of modifiers (e.g. control-hyper) gets its own submap | |
87 off of the main map. The hash key for a modifier combination is | |
88 an integer, computed by MAKE_MODIFIER_HASH_KEY(). | |
89 | |
90 If the key `C-a' was bound to some command, the hierarchy would look like | |
91 | |
442 | 92 keymap-1: associates the integer XEMACS_MOD_CONTROL with keymap-2 |
428 | 93 keymap-2: associates "a" with the command |
94 | |
95 Similarly, if the key `C-H-a' was bound to some command, the hierarchy | |
96 would look like | |
97 | |
442 | 98 keymap-1: associates the integer (XEMACS_MOD_CONTROL | XEMACS_MOD_HYPER) |
428 | 99 with keymap-2 |
100 keymap-2: associates "a" with the command | |
101 | |
102 Note that a special exception is made for the meta modifier, in order | |
103 to deal with ESC/meta lossage. Any key combination containing the | |
104 meta modifier is first indexed off of the main map into the meta | |
442 | 105 submap (with hash key XEMACS_MOD_META) and then indexed off of the |
428 | 106 meta submap with the meta modifier removed from the key combination. |
107 For example, when associating a command with C-M-H-a, we'd have | |
108 | |
442 | 109 keymap-1: associates the integer XEMACS_MOD_META with keymap-2 |
110 keymap-2: associates the integer (XEMACS_MOD_CONTROL | XEMACS_MOD_HYPER) | |
428 | 111 with keymap-3 |
112 keymap-3: associates "a" with the command | |
113 | |
114 Note that keymap-2 might have normal bindings in it; these would be | |
115 for key combinations containing only the meta modifier, such as | |
116 M-y or meta-backspace. | |
117 | |
118 If the command that "a" was bound to in keymap-3 was itself a keymap, | |
119 then that would make the key "C-M-H-a" be a prefix character. | |
120 | |
121 Note that this new model of keymaps takes much of the magic away from | |
122 the Escape key: the value of the variable `esc-map' is no longer indexed | |
123 in the `global-map' under the ESC key. It's indexed under the integer | |
442 | 124 XEMACS_MOD_META. This is not user-visible, however; none of the "bucky" |
428 | 125 maps are. |
126 | |
127 There is a hack in Flookup_key() that makes (lookup-key global-map "\^[") | |
128 and (define-key some-random-map "\^[" my-esc-map) work as before, for | |
129 compatibility. | |
130 | |
131 Since keymaps are opaque, the only way to extract information from them | |
132 is with the functions lookup-key, key-binding, local-key-binding, and | |
133 global-key-binding, which work just as before, and the new function | |
440 | 134 map-keymap, which is roughly analogous to maphash. |
428 | 135 |
136 Note that map-keymap perpetuates the illusion that the "bucky" submaps | |
137 don't exist: if you map over a keymap with bucky submaps, it will also | |
138 map over those submaps. It does not, however, map over other random | |
139 submaps of the keymap, just the bucky ones. | |
140 | |
141 One implication of this is that when you map over `global-map', you will | |
142 also map over `esc-map'. It is merely for compatibility that the esc-map | |
143 is accessible at all; I think that's a bad thing, since it blurs the | |
144 distinction between ESC and "meta" even more. "M-x" is no more a two- | |
145 key sequence than "C-x" is. | |
146 | |
147 */ | |
148 | |
440 | 149 struct Lisp_Keymap |
428 | 150 { |
3017 | 151 struct LCRECORD_HEADER header; |
440 | 152 Lisp_Object parents; /* Keymaps to be searched after this one. |
153 An ordered list */ | |
428 | 154 Lisp_Object prompt; /* Qnil or a string to print in the minibuffer |
440 | 155 when reading from this keymap */ |
428 | 156 Lisp_Object table; /* The contents of this keymap */ |
157 Lisp_Object inverse_table; /* The inverse mapping of the above */ | |
158 Lisp_Object default_binding; /* Use this if no other binding is found | |
440 | 159 (this overrides parent maps and the |
160 normal global-map lookup). */ | |
428 | 161 Lisp_Object sub_maps_cache; /* Cache of directly inferior keymaps; |
162 This holds an alist, of the key and the | |
163 maps, or the modifier bit and the map. | |
164 If this is the symbol t, then the cache | |
440 | 165 needs to be recomputed. */ |
428 | 166 Lisp_Object name; /* Just for debugging convenience */ |
440 | 167 }; |
428 | 168 |
169 #define MAKE_MODIFIER_HASH_KEY(modifier) make_int (modifier) | |
170 #define MODIFIER_HASH_KEY_BITS(x) (INTP (x) ? XINT (x) : 0) | |
171 | |
172 | |
173 | |
174 /* Actually allocate storage for these variables */ | |
175 | |
440 | 176 Lisp_Object Vcurrent_global_map; /* Always a keymap */ |
428 | 177 |
771 | 178 static Lisp_Object Vglobal_tty_map, Vglobal_window_system_map; |
179 | |
428 | 180 static Lisp_Object Vmouse_grabbed_buffer; |
181 | |
182 /* Alist of minor mode variables and keymaps. */ | |
183 static Lisp_Object Qminor_mode_map_alist; | |
184 | |
185 static Lisp_Object Voverriding_local_map; | |
186 | |
187 static Lisp_Object Vkey_translation_map; | |
188 | |
189 static Lisp_Object Vvertical_divider_map; | |
190 | |
191 /* This is incremented whenever a change is made to a keymap. This is | |
192 so that things which care (such as the menubar code) can recompute | |
193 privately-cached data when the user has changed keybindings. | |
194 */ | |
458 | 195 Fixnum keymap_tick; |
428 | 196 |
197 /* Prefixing a key with this character is the same as sending a meta bit. */ | |
198 Lisp_Object Vmeta_prefix_char; | |
199 | |
200 Lisp_Object Qkeymapp; | |
201 Lisp_Object Vsingle_space_string; | |
202 Lisp_Object Qsuppress_keymap; | |
203 Lisp_Object Qmodeline_map; | |
204 Lisp_Object Qtoolbar_map; | |
205 | |
206 EXFUN (Fkeymap_fullness, 1); | |
207 EXFUN (Fset_keymap_name, 2); | |
208 EXFUN (Fsingle_key_description, 1); | |
209 | |
210 static void describe_command (Lisp_Object definition, Lisp_Object buffer); | |
211 static void describe_map (Lisp_Object keymap, Lisp_Object elt_prefix, | |
212 void (*elt_describer) (Lisp_Object, Lisp_Object), | |
213 int partial, | |
214 Lisp_Object shadow, | |
215 int mice_only_p, | |
216 Lisp_Object buffer); | |
440 | 217 static Lisp_Object keymap_submaps (Lisp_Object keymap); |
428 | 218 |
219 Lisp_Object Qcontrol, Qctrl, Qmeta, Qsuper, Qhyper, Qalt, Qshift; | |
220 Lisp_Object Qbutton0, Qbutton1, Qbutton2, Qbutton3; | |
221 Lisp_Object Qbutton4, Qbutton5, Qbutton6, Qbutton7; | |
222 Lisp_Object Qbutton0up, Qbutton1up, Qbutton2up, Qbutton3up; | |
223 Lisp_Object Qbutton4up, Qbutton5up, Qbutton6up, Qbutton7up; | |
224 | |
225 Lisp_Object Qmenu_selection; | |
226 /* Emacs compatibility */ | |
458 | 227 Lisp_Object Qdown_mouse_1, Qmouse_1; |
228 Lisp_Object Qdown_mouse_2, Qmouse_2; | |
229 Lisp_Object Qdown_mouse_3, Qmouse_3; | |
230 Lisp_Object Qdown_mouse_4, Qmouse_4; | |
231 Lisp_Object Qdown_mouse_5, Qmouse_5; | |
232 Lisp_Object Qdown_mouse_6, Qmouse_6; | |
233 Lisp_Object Qdown_mouse_7, Qmouse_7; | |
428 | 234 |
235 /* Kludge kludge kludge */ | |
236 Lisp_Object QLFD, QTAB, QRET, QESC, QDEL, QSPC, QBS; | |
237 | |
238 | |
239 /************************************************************************/ | |
240 /* The keymap Lisp object */ | |
241 /************************************************************************/ | |
242 | |
243 static Lisp_Object | |
244 mark_keymap (Lisp_Object obj) | |
245 { | |
246 Lisp_Keymap *keymap = XKEYMAP (obj); | |
247 mark_object (keymap->parents); | |
248 mark_object (keymap->prompt); | |
249 mark_object (keymap->inverse_table); | |
250 mark_object (keymap->sub_maps_cache); | |
251 mark_object (keymap->default_binding); | |
252 mark_object (keymap->name); | |
253 return keymap->table; | |
254 } | |
255 | |
256 static void | |
2286 | 257 print_keymap (Lisp_Object obj, Lisp_Object printcharfun, |
258 int UNUSED (escapeflag)) | |
428 | 259 { |
260 /* This function can GC */ | |
261 Lisp_Keymap *keymap = XKEYMAP (obj); | |
262 if (print_readably) | |
563 | 263 printing_unreadable_object ("#<keymap 0x%x>", keymap->header.uid); |
826 | 264 write_c_string (printcharfun, "#<keymap "); |
428 | 265 if (!NILP (keymap->name)) |
440 | 266 { |
800 | 267 write_fmt_string_lisp (printcharfun, "%S ", 1, keymap->name); |
440 | 268 } |
800 | 269 write_fmt_string (printcharfun, "size %ld 0x%x>", |
270 (long) XINT (Fkeymap_fullness (obj)), keymap->header.uid); | |
428 | 271 } |
272 | |
1204 | 273 static const struct memory_description keymap_description[] = { |
440 | 274 { XD_LISP_OBJECT, offsetof (Lisp_Keymap, parents) }, |
275 { XD_LISP_OBJECT, offsetof (Lisp_Keymap, prompt) }, | |
276 { XD_LISP_OBJECT, offsetof (Lisp_Keymap, table) }, | |
277 { XD_LISP_OBJECT, offsetof (Lisp_Keymap, inverse_table) }, | |
278 { XD_LISP_OBJECT, offsetof (Lisp_Keymap, default_binding) }, | |
279 { XD_LISP_OBJECT, offsetof (Lisp_Keymap, sub_maps_cache) }, | |
280 { XD_LISP_OBJECT, offsetof (Lisp_Keymap, name) }, | |
428 | 281 { XD_END } |
282 }; | |
283 | |
284 /* No need for keymap_equal #### Why not? */ | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
285 DEFINE_LISP_OBJECT ("keymap", keymap, |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
286 mark_keymap, print_keymap, 0, 0, 0, |
934 | 287 keymap_description, |
288 Lisp_Keymap); | |
428 | 289 |
290 /************************************************************************/ | |
291 /* Traversing keymaps and their parents */ | |
292 /************************************************************************/ | |
293 | |
294 static Lisp_Object | |
295 traverse_keymaps (Lisp_Object start_keymap, Lisp_Object start_parents, | |
296 Lisp_Object (*mapper) (Lisp_Object keymap, void *mapper_arg), | |
297 void *mapper_arg) | |
298 { | |
299 /* This function can GC */ | |
300 Lisp_Object keymap; | |
301 Lisp_Object tail = start_parents; | |
302 Lisp_Object malloc_sucks[10]; | |
303 Lisp_Object malloc_bites = Qnil; | |
304 int stack_depth = 0; | |
305 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
306 GCPRO4 (*malloc_sucks, malloc_bites, start_keymap, tail); | |
307 gcpro1.nvars = 0; | |
308 | |
309 start_keymap = get_keymap (start_keymap, 1, 1); | |
310 keymap = start_keymap; | |
311 /* Hack special-case parents at top-level */ | |
440 | 312 tail = !NILP (tail) ? tail : XKEYMAP (keymap)->parents; |
428 | 313 |
314 for (;;) | |
315 { | |
316 Lisp_Object result; | |
317 | |
318 QUIT; | |
440 | 319 result = mapper (keymap, mapper_arg); |
428 | 320 if (!NILP (result)) |
321 { | |
322 while (CONSP (malloc_bites)) | |
323 { | |
853 | 324 Lisp_Object victim = malloc_bites; |
325 malloc_bites = XCDR (victim); | |
428 | 326 free_cons (victim); |
327 } | |
328 UNGCPRO; | |
329 return result; | |
330 } | |
331 if (NILP (tail)) | |
332 { | |
333 if (stack_depth == 0) | |
334 { | |
335 UNGCPRO; | |
336 return Qnil; /* Nothing found */ | |
337 } | |
338 stack_depth--; | |
339 if (CONSP (malloc_bites)) | |
340 { | |
853 | 341 Lisp_Object victim = malloc_bites; |
342 tail = XCAR (victim); | |
343 malloc_bites = XCDR (victim); | |
428 | 344 free_cons (victim); |
345 } | |
346 else | |
347 { | |
348 tail = malloc_sucks[stack_depth]; | |
349 gcpro1.nvars = stack_depth; | |
350 } | |
351 keymap = XCAR (tail); | |
352 tail = XCDR (tail); | |
353 } | |
354 else | |
355 { | |
356 Lisp_Object parents; | |
357 | |
358 keymap = XCAR (tail); | |
359 tail = XCDR (tail); | |
360 parents = XKEYMAP (keymap)->parents; | |
361 if (!CONSP (parents)) | |
362 ; | |
363 else if (NILP (tail)) | |
364 /* Tail-recurse */ | |
365 tail = parents; | |
366 else | |
367 { | |
368 if (CONSP (malloc_bites)) | |
369 malloc_bites = noseeum_cons (tail, malloc_bites); | |
370 else if (stack_depth < countof (malloc_sucks)) | |
371 { | |
372 malloc_sucks[stack_depth++] = tail; | |
373 gcpro1.nvars = stack_depth; | |
374 } | |
375 else | |
376 { | |
377 /* *&@##[*&^$ C. @#[$*&@# Unix. Losers all. */ | |
378 int i; | |
379 for (i = 0, malloc_bites = Qnil; | |
380 i < countof (malloc_sucks); | |
381 i++) | |
382 malloc_bites = noseeum_cons (malloc_sucks[i], | |
383 malloc_bites); | |
384 gcpro1.nvars = 0; | |
385 } | |
386 tail = parents; | |
387 } | |
388 } | |
389 keymap = get_keymap (keymap, 1, 1); | |
390 if (EQ (keymap, start_keymap)) | |
391 { | |
563 | 392 invalid_argument ("Cyclic keymap indirection", start_keymap); |
428 | 393 } |
394 } | |
395 } | |
396 | |
397 | |
398 /************************************************************************/ | |
399 /* Some low-level functions */ | |
400 /************************************************************************/ | |
401 | |
442 | 402 static int |
428 | 403 bucky_sym_to_bucky_bit (Lisp_Object sym) |
404 { | |
442 | 405 if (EQ (sym, Qcontrol)) return XEMACS_MOD_CONTROL; |
406 if (EQ (sym, Qmeta)) return XEMACS_MOD_META; | |
407 if (EQ (sym, Qsuper)) return XEMACS_MOD_SUPER; | |
408 if (EQ (sym, Qhyper)) return XEMACS_MOD_HYPER; | |
409 if (EQ (sym, Qalt)) return XEMACS_MOD_ALT; | |
410 if (EQ (sym, Qsymbol)) return XEMACS_MOD_ALT; /* #### - reverse compat */ | |
411 if (EQ (sym, Qshift)) return XEMACS_MOD_SHIFT; | |
428 | 412 |
413 return 0; | |
414 } | |
415 | |
416 static Lisp_Object | |
442 | 417 control_meta_superify (Lisp_Object frob, int modifiers) |
428 | 418 { |
419 if (modifiers == 0) | |
420 return frob; | |
421 frob = Fcons (frob, Qnil); | |
442 | 422 if (modifiers & XEMACS_MOD_SHIFT) frob = Fcons (Qshift, frob); |
423 if (modifiers & XEMACS_MOD_ALT) frob = Fcons (Qalt, frob); | |
424 if (modifiers & XEMACS_MOD_HYPER) frob = Fcons (Qhyper, frob); | |
425 if (modifiers & XEMACS_MOD_SUPER) frob = Fcons (Qsuper, frob); | |
426 if (modifiers & XEMACS_MOD_CONTROL) frob = Fcons (Qcontrol, frob); | |
427 if (modifiers & XEMACS_MOD_META) frob = Fcons (Qmeta, frob); | |
428 | 428 return frob; |
429 } | |
430 | |
431 static Lisp_Object | |
934 | 432 make_key_description (const Lisp_Key_Data *key, int prettify) |
433 { | |
1204 | 434 Lisp_Object keysym = KEY_DATA_KEYSYM (key); |
934 | 435 int modifiers = KEY_DATA_MODIFIERS (key); |
428 | 436 if (prettify && CHARP (keysym)) |
437 { | |
438 /* This is a little slow, but (control a) is prettier than (control 65). | |
439 It's now ok to do this for digit-chars too, since we've fixed the | |
440 bug where \9 read as the integer 9 instead of as the symbol with | |
441 "9" as its name. | |
442 */ | |
443 /* !!#### I'm not sure how correct this is. */ | |
867 | 444 Ibyte str [1 + MAX_ICHAR_LEN]; |
445 Bytecount count = set_itext_ichar (str, XCHAR (keysym)); | |
428 | 446 str[count] = 0; |
771 | 447 keysym = intern_int (str); |
428 | 448 } |
449 return control_meta_superify (keysym, modifiers); | |
450 } | |
451 | |
452 | |
453 /************************************************************************/ | |
454 /* Low-level keymap-store functions */ | |
455 /************************************************************************/ | |
456 | |
457 static Lisp_Object | |
458 raw_lookup_key (Lisp_Object keymap, | |
934 | 459 const Lisp_Key_Data *raw_keys, int raw_keys_count, |
428 | 460 int keys_so_far, int accept_default); |
461 | |
462 /* Relies on caller to gc-protect args */ | |
463 static Lisp_Object | |
464 keymap_lookup_directly (Lisp_Object keymap, | |
442 | 465 Lisp_Object keysym, int modifiers) |
428 | 466 { |
467 Lisp_Keymap *k; | |
468 | |
442 | 469 modifiers &= ~(XEMACS_MOD_BUTTON1 | XEMACS_MOD_BUTTON2 | XEMACS_MOD_BUTTON3 |
470 | XEMACS_MOD_BUTTON4 | XEMACS_MOD_BUTTON5); | |
471 if ((modifiers & ~(XEMACS_MOD_CONTROL | XEMACS_MOD_META | XEMACS_MOD_SUPER | |
472 | XEMACS_MOD_HYPER | XEMACS_MOD_ALT | XEMACS_MOD_SHIFT)) | |
473 != 0) | |
2500 | 474 ABORT (); |
428 | 475 |
476 k = XKEYMAP (keymap); | |
477 | |
478 /* If the keysym is a one-character symbol, use the char code instead. */ | |
826 | 479 if (SYMBOLP (keysym) && string_char_length (XSYMBOL (keysym)->name) == 1) |
428 | 480 { |
481 Lisp_Object i_fart_on_gcc = | |
867 | 482 make_char (string_ichar (XSYMBOL (keysym)->name, 0)); |
428 | 483 keysym = i_fart_on_gcc; |
484 } | |
485 | |
442 | 486 if (modifiers & XEMACS_MOD_META) /* Utterly hateful ESC lossage */ |
428 | 487 { |
442 | 488 Lisp_Object submap = Fgethash (MAKE_MODIFIER_HASH_KEY (XEMACS_MOD_META), |
428 | 489 k->table, Qnil); |
490 if (NILP (submap)) | |
491 return Qnil; | |
492 k = XKEYMAP (submap); | |
442 | 493 modifiers &= ~XEMACS_MOD_META; |
428 | 494 } |
495 | |
496 if (modifiers != 0) | |
497 { | |
498 Lisp_Object submap = Fgethash (MAKE_MODIFIER_HASH_KEY (modifiers), | |
499 k->table, Qnil); | |
500 if (NILP (submap)) | |
501 return Qnil; | |
502 k = XKEYMAP (submap); | |
503 } | |
504 return Fgethash (keysym, k->table, Qnil); | |
505 } | |
506 | |
507 static void | |
508 keymap_store_inverse_internal (Lisp_Object inverse_table, | |
509 Lisp_Object keysym, | |
510 Lisp_Object value) | |
511 { | |
512 Lisp_Object keys = Fgethash (value, inverse_table, Qunbound); | |
513 | |
514 if (UNBOUNDP (keys)) | |
515 { | |
516 keys = keysym; | |
517 /* Don't cons this unless necessary */ | |
518 /* keys = Fcons (keysym, Qnil); */ | |
519 Fputhash (value, keys, inverse_table); | |
520 } | |
521 else if (!CONSP (keys)) | |
522 { | |
523 /* Now it's necessary to cons */ | |
524 keys = Fcons (keys, keysym); | |
525 Fputhash (value, keys, inverse_table); | |
526 } | |
527 else | |
528 { | |
529 while (CONSP (XCDR (keys))) | |
530 keys = XCDR (keys); | |
531 XCDR (keys) = Fcons (XCDR (keys), keysym); | |
532 /* No need to call puthash because we've destructively | |
533 modified the list tail in place */ | |
534 } | |
535 } | |
536 | |
537 | |
538 static void | |
539 keymap_delete_inverse_internal (Lisp_Object inverse_table, | |
540 Lisp_Object keysym, | |
541 Lisp_Object value) | |
542 { | |
543 Lisp_Object keys = Fgethash (value, inverse_table, Qunbound); | |
544 Lisp_Object new_keys = keys; | |
545 Lisp_Object tail; | |
546 Lisp_Object *prev; | |
547 | |
548 if (UNBOUNDP (keys)) | |
2500 | 549 ABORT (); |
428 | 550 |
551 for (prev = &new_keys, tail = new_keys; | |
552 ; | |
553 prev = &(XCDR (tail)), tail = XCDR (tail)) | |
554 { | |
555 if (EQ (tail, keysym)) | |
556 { | |
557 *prev = Qnil; | |
558 break; | |
559 } | |
560 else if (EQ (keysym, XCAR (tail))) | |
561 { | |
562 *prev = XCDR (tail); | |
563 break; | |
564 } | |
565 } | |
566 | |
567 if (NILP (new_keys)) | |
568 Fremhash (value, inverse_table); | |
569 else if (!EQ (keys, new_keys)) | |
570 /* Removed the first elt */ | |
571 Fputhash (value, new_keys, inverse_table); | |
572 /* else the list's tail has been modified, so we don't need to | |
573 touch the hash table again (the pointer in there is ok). | |
574 */ | |
575 } | |
576 | |
440 | 577 /* Prevent luser from shooting herself in the foot using something like |
578 (define-key ctl-x-4-map "p" global-map) */ | |
579 static void | |
580 check_keymap_definition_loop (Lisp_Object def, Lisp_Keymap *to_keymap) | |
581 { | |
582 def = get_keymap (def, 0, 0); | |
583 | |
584 if (KEYMAPP (def)) | |
585 { | |
586 Lisp_Object maps; | |
587 | |
588 if (XKEYMAP (def) == to_keymap) | |
563 | 589 invalid_argument ("Cyclic keymap definition", def); |
440 | 590 |
591 for (maps = keymap_submaps (def); | |
592 CONSP (maps); | |
593 maps = XCDR (maps)) | |
594 check_keymap_definition_loop (XCDR (XCAR (maps)), to_keymap); | |
595 } | |
596 } | |
428 | 597 |
598 static void | |
599 keymap_store_internal (Lisp_Object keysym, Lisp_Keymap *keymap, | |
440 | 600 Lisp_Object def) |
428 | 601 { |
440 | 602 Lisp_Object prev_def = Fgethash (keysym, keymap->table, Qnil); |
603 | |
604 if (EQ (prev_def, def)) | |
428 | 605 return; |
440 | 606 |
607 check_keymap_definition_loop (def, keymap); | |
608 | |
609 if (!NILP (prev_def)) | |
428 | 610 keymap_delete_inverse_internal (keymap->inverse_table, |
440 | 611 keysym, prev_def); |
612 if (NILP (def)) | |
428 | 613 { |
614 Fremhash (keysym, keymap->table); | |
615 } | |
616 else | |
617 { | |
440 | 618 Fputhash (keysym, def, keymap->table); |
428 | 619 keymap_store_inverse_internal (keymap->inverse_table, |
440 | 620 keysym, def); |
428 | 621 } |
622 keymap_tick++; | |
623 } | |
624 | |
625 | |
626 static Lisp_Object | |
442 | 627 create_bucky_submap (Lisp_Keymap *k, int modifiers, |
428 | 628 Lisp_Object parent_for_debugging_info) |
629 { | |
630 Lisp_Object submap = Fmake_sparse_keymap (Qnil); | |
631 /* User won't see this, but it is nice for debugging Emacs */ | |
632 XKEYMAP (submap)->name | |
633 = control_meta_superify (parent_for_debugging_info, modifiers); | |
634 /* Invalidate cache */ | |
635 k->sub_maps_cache = Qt; | |
636 keymap_store_internal (MAKE_MODIFIER_HASH_KEY (modifiers), k, submap); | |
637 return submap; | |
638 } | |
639 | |
640 | |
641 /* Relies on caller to gc-protect keymap, keysym, value */ | |
642 static void | |
934 | 643 keymap_store (Lisp_Object keymap, const Lisp_Key_Data *key, |
428 | 644 Lisp_Object value) |
645 { | |
934 | 646 Lisp_Object keysym = KEY_DATA_KEYSYM (key); |
647 int modifiers = KEY_DATA_MODIFIERS (key); | |
440 | 648 Lisp_Keymap *k = XKEYMAP (keymap); |
649 | |
442 | 650 modifiers &= ~(XEMACS_MOD_BUTTON1 | XEMACS_MOD_BUTTON2 | XEMACS_MOD_BUTTON3 |
651 | XEMACS_MOD_BUTTON4 | XEMACS_MOD_BUTTON5); | |
652 assert ((modifiers & ~(XEMACS_MOD_CONTROL | XEMACS_MOD_META | |
653 | XEMACS_MOD_SUPER | XEMACS_MOD_HYPER | |
654 | XEMACS_MOD_ALT | XEMACS_MOD_SHIFT)) == 0); | |
428 | 655 |
656 /* If the keysym is a one-character symbol, use the char code instead. */ | |
826 | 657 if (SYMBOLP (keysym) && string_char_length (XSYMBOL (keysym)->name) == 1) |
867 | 658 keysym = make_char (string_ichar (XSYMBOL (keysym)->name, 0)); |
428 | 659 |
442 | 660 if (modifiers & XEMACS_MOD_META) /* Utterly hateful ESC lossage */ |
428 | 661 { |
442 | 662 Lisp_Object submap = Fgethash (MAKE_MODIFIER_HASH_KEY (XEMACS_MOD_META), |
428 | 663 k->table, Qnil); |
664 if (NILP (submap)) | |
442 | 665 submap = create_bucky_submap (k, XEMACS_MOD_META, keymap); |
428 | 666 k = XKEYMAP (submap); |
442 | 667 modifiers &= ~XEMACS_MOD_META; |
428 | 668 } |
669 | |
670 if (modifiers != 0) | |
671 { | |
672 Lisp_Object submap = Fgethash (MAKE_MODIFIER_HASH_KEY (modifiers), | |
673 k->table, Qnil); | |
674 if (NILP (submap)) | |
675 submap = create_bucky_submap (k, modifiers, keymap); | |
676 k = XKEYMAP (submap); | |
677 } | |
678 k->sub_maps_cache = Qt; /* Invalidate cache */ | |
679 keymap_store_internal (keysym, k, value); | |
680 } | |
681 | |
682 | |
683 /************************************************************************/ | |
684 /* Listing the submaps of a keymap */ | |
685 /************************************************************************/ | |
686 | |
687 struct keymap_submaps_closure | |
688 { | |
689 Lisp_Object *result_locative; | |
690 }; | |
691 | |
692 static int | |
2286 | 693 keymap_submaps_mapper_0 (Lisp_Object UNUSED (key), Lisp_Object value, |
694 void *UNUSED (keymap_submaps_closure)) | |
428 | 695 { |
696 /* This function can GC */ | |
697 /* Perform any autoloads, etc */ | |
698 Fkeymapp (value); | |
699 return 0; | |
700 } | |
701 | |
702 static int | |
703 keymap_submaps_mapper (Lisp_Object key, Lisp_Object value, | |
704 void *keymap_submaps_closure) | |
705 { | |
706 /* This function can GC */ | |
707 Lisp_Object *result_locative; | |
708 struct keymap_submaps_closure *cl = | |
709 (struct keymap_submaps_closure *) keymap_submaps_closure; | |
710 result_locative = cl->result_locative; | |
711 | |
712 if (!NILP (Fkeymapp (value))) | |
713 *result_locative = Fcons (Fcons (key, value), *result_locative); | |
714 return 0; | |
715 } | |
716 | |
717 static int map_keymap_sort_predicate (Lisp_Object obj1, Lisp_Object obj2, | |
718 Lisp_Object pred); | |
719 | |
720 static Lisp_Object | |
721 keymap_submaps (Lisp_Object keymap) | |
722 { | |
723 /* This function can GC */ | |
724 Lisp_Keymap *k = XKEYMAP (keymap); | |
725 | |
726 if (EQ (k->sub_maps_cache, Qt)) /* Unknown */ | |
727 { | |
728 Lisp_Object result = Qnil; | |
729 struct gcpro gcpro1, gcpro2; | |
730 struct keymap_submaps_closure keymap_submaps_closure; | |
731 | |
732 GCPRO2 (keymap, result); | |
733 keymap_submaps_closure.result_locative = &result; | |
734 /* Do this first pass to touch (and load) any autoloaded maps */ | |
735 elisp_maphash (keymap_submaps_mapper_0, k->table, | |
736 &keymap_submaps_closure); | |
737 result = Qnil; | |
738 elisp_maphash (keymap_submaps_mapper, k->table, | |
739 &keymap_submaps_closure); | |
740 /* keep it sorted so that the result of accessible-keymaps is ordered */ | |
741 k->sub_maps_cache = list_sort (result, | |
742 Qnil, | |
743 map_keymap_sort_predicate); | |
744 UNGCPRO; | |
745 } | |
746 return k->sub_maps_cache; | |
747 } | |
748 | |
749 | |
750 /************************************************************************/ | |
751 /* Basic operations on keymaps */ | |
752 /************************************************************************/ | |
753 | |
754 static Lisp_Object | |
665 | 755 make_keymap (Elemcount size) |
428 | 756 { |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
757 Lisp_Object obj = ALLOC_LISP_OBJECT (keymap); |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
758 Lisp_Keymap *keymap = XKEYMAP (obj); |
428 | 759 |
760 keymap->parents = Qnil; | |
761 keymap->prompt = Qnil; | |
762 keymap->table = Qnil; | |
763 keymap->inverse_table = Qnil; | |
764 keymap->default_binding = Qnil; | |
765 keymap->sub_maps_cache = Qnil; /* No possible submaps */ | |
766 keymap->name = Qnil; | |
767 | |
768 if (size != 0) /* hack for copy-keymap */ | |
769 { | |
770 keymap->table = | |
771 make_lisp_hash_table (size, HASH_TABLE_NON_WEAK, HASH_TABLE_EQ); | |
772 /* Inverse table is often less dense because of duplicate key-bindings. | |
773 If not, it will grow anyway. */ | |
774 keymap->inverse_table = | |
647 | 775 make_lisp_hash_table (size * 3 / 4, HASH_TABLE_NON_WEAK, |
776 HASH_TABLE_EQ); | |
428 | 777 } |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
778 return obj; |
428 | 779 } |
780 | |
781 DEFUN ("make-keymap", Fmake_keymap, 0, 1, 0, /* | |
782 Construct and return a new keymap object. | |
783 All entries in it are nil, meaning "command undefined". | |
784 | |
785 Optional argument NAME specifies a name to assign to the keymap, | |
786 as in `set-keymap-name'. This name is only a debugging convenience; | |
787 it is not used except when printing the keymap. | |
788 */ | |
789 (name)) | |
790 { | |
791 Lisp_Object keymap = make_keymap (60); | |
792 if (!NILP (name)) | |
793 Fset_keymap_name (keymap, name); | |
794 return keymap; | |
795 } | |
796 | |
797 DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, 0, 1, 0, /* | |
798 Construct and return a new keymap object. | |
799 All entries in it are nil, meaning "command undefined". The only | |
444 | 800 difference between this function and `make-keymap' is that this function |
428 | 801 returns a "smaller" keymap (one that is expected to contain fewer |
444 | 802 entries). As keymaps dynamically resize, this distinction is not great. |
428 | 803 |
804 Optional argument NAME specifies a name to assign to the keymap, | |
805 as in `set-keymap-name'. This name is only a debugging convenience; | |
806 it is not used except when printing the keymap. | |
807 */ | |
808 (name)) | |
809 { | |
810 Lisp_Object keymap = make_keymap (8); | |
811 if (!NILP (name)) | |
812 Fset_keymap_name (keymap, name); | |
813 return keymap; | |
814 } | |
815 | |
816 DEFUN ("keymap-parents", Fkeymap_parents, 1, 1, 0, /* | |
817 Return the `parent' keymaps of KEYMAP, or nil. | |
818 The parents of a keymap are searched for keybindings when a key sequence | |
819 isn't bound in this one. `(current-global-map)' is the default parent | |
820 of all keymaps. | |
821 */ | |
822 (keymap)) | |
823 { | |
824 keymap = get_keymap (keymap, 1, 1); | |
825 return Fcopy_sequence (XKEYMAP (keymap)->parents); | |
826 } | |
827 | |
828 | |
829 | |
830 static Lisp_Object | |
2286 | 831 traverse_keymaps_noop (Lisp_Object UNUSED (keymap), void *UNUSED (arg)) |
428 | 832 { |
833 return Qnil; | |
834 } | |
835 | |
836 DEFUN ("set-keymap-parents", Fset_keymap_parents, 2, 2, 0, /* | |
837 Set the `parent' keymaps of KEYMAP to PARENTS. | |
838 The parents of a keymap are searched for keybindings when a key sequence | |
839 isn't bound in this one. `(current-global-map)' is the default parent | |
840 of all keymaps. | |
841 */ | |
842 (keymap, parents)) | |
843 { | |
844 /* This function can GC */ | |
845 Lisp_Object k; | |
846 struct gcpro gcpro1, gcpro2; | |
847 | |
848 GCPRO2 (keymap, parents); | |
849 keymap = get_keymap (keymap, 1, 1); | |
850 | |
851 if (KEYMAPP (parents)) /* backwards-compatibility */ | |
852 parents = list1 (parents); | |
853 if (!NILP (parents)) | |
854 { | |
855 Lisp_Object tail = parents; | |
856 while (!NILP (tail)) | |
857 { | |
858 QUIT; | |
859 CHECK_CONS (tail); | |
860 k = XCAR (tail); | |
861 /* Require that it be an actual keymap object, rather than a symbol | |
862 with a (crockish) symbol-function which is a keymap */ | |
863 CHECK_KEYMAP (k); /* get_keymap (k, 1, 1); */ | |
864 tail = XCDR (tail); | |
865 } | |
866 } | |
867 | |
868 /* Check for circularities */ | |
869 traverse_keymaps (keymap, parents, traverse_keymaps_noop, 0); | |
870 keymap_tick++; | |
871 XKEYMAP (keymap)->parents = Fcopy_sequence (parents); | |
872 UNGCPRO; | |
873 return parents; | |
874 } | |
875 | |
876 DEFUN ("set-keymap-name", Fset_keymap_name, 2, 2, 0, /* | |
877 Set the `name' of the KEYMAP to NEW-NAME. | |
878 The name is only a debugging convenience; it is not used except | |
879 when printing the keymap. | |
880 */ | |
881 (keymap, new_name)) | |
882 { | |
883 keymap = get_keymap (keymap, 1, 1); | |
884 | |
885 XKEYMAP (keymap)->name = new_name; | |
886 return new_name; | |
887 } | |
888 | |
889 DEFUN ("keymap-name", Fkeymap_name, 1, 1, 0, /* | |
890 Return the `name' of KEYMAP. | |
891 The name is only a debugging convenience; it is not used except | |
892 when printing the keymap. | |
893 */ | |
894 (keymap)) | |
895 { | |
896 keymap = get_keymap (keymap, 1, 1); | |
897 | |
898 return XKEYMAP (keymap)->name; | |
899 } | |
900 | |
901 DEFUN ("set-keymap-prompt", Fset_keymap_prompt, 2, 2, 0, /* | |
902 Set the `prompt' of KEYMAP to string NEW-PROMPT, or `nil' | |
903 if no prompt is desired. The prompt is shown in the echo-area | |
904 when reading a key-sequence to be looked-up in this keymap. | |
905 */ | |
906 (keymap, new_prompt)) | |
907 { | |
908 keymap = get_keymap (keymap, 1, 1); | |
909 | |
910 if (!NILP (new_prompt)) | |
911 CHECK_STRING (new_prompt); | |
912 | |
913 XKEYMAP (keymap)->prompt = new_prompt; | |
914 return new_prompt; | |
915 } | |
916 | |
917 static Lisp_Object | |
2286 | 918 keymap_prompt_mapper (Lisp_Object keymap, void *UNUSED (arg)) |
428 | 919 { |
920 return XKEYMAP (keymap)->prompt; | |
921 } | |
922 | |
923 | |
924 DEFUN ("keymap-prompt", Fkeymap_prompt, 1, 2, 0, /* | |
925 Return the `prompt' of KEYMAP. | |
926 If non-nil, the prompt is shown in the echo-area | |
927 when reading a key-sequence to be looked-up in this keymap. | |
928 */ | |
929 (keymap, use_inherited)) | |
930 { | |
931 /* This function can GC */ | |
932 Lisp_Object prompt; | |
933 | |
934 keymap = get_keymap (keymap, 1, 1); | |
935 prompt = XKEYMAP (keymap)->prompt; | |
936 if (!NILP (prompt) || NILP (use_inherited)) | |
937 return prompt; | |
938 else | |
939 return traverse_keymaps (keymap, Qnil, keymap_prompt_mapper, 0); | |
940 } | |
941 | |
942 DEFUN ("set-keymap-default-binding", Fset_keymap_default_binding, 2, 2, 0, /* | |
943 Sets the default binding of KEYMAP to COMMAND, or `nil' | |
944 if no default is desired. The default-binding is returned when | |
945 no other binding for a key-sequence is found in the keymap. | |
946 If a keymap has a non-nil default-binding, neither the keymap's | |
947 parents nor the current global map are searched for key bindings. | |
948 */ | |
949 (keymap, command)) | |
950 { | |
951 /* This function can GC */ | |
952 keymap = get_keymap (keymap, 1, 1); | |
953 | |
954 XKEYMAP (keymap)->default_binding = command; | |
955 return command; | |
956 } | |
957 | |
958 DEFUN ("keymap-default-binding", Fkeymap_default_binding, 1, 1, 0, /* | |
959 Return the default binding of KEYMAP, or `nil' if it has none. | |
960 The default-binding is returned when no other binding for a key-sequence | |
961 is found in the keymap. | |
962 If a keymap has a non-nil default-binding, neither the keymap's | |
963 parents nor the current global map are searched for key bindings. | |
964 */ | |
965 (keymap)) | |
966 { | |
967 /* This function can GC */ | |
968 keymap = get_keymap (keymap, 1, 1); | |
969 return XKEYMAP (keymap)->default_binding; | |
970 } | |
971 | |
972 DEFUN ("keymapp", Fkeymapp, 1, 1, 0, /* | |
444 | 973 Return t if OBJECT is a keymap object. |
428 | 974 The keymap may be autoloaded first if necessary. |
975 */ | |
976 (object)) | |
977 { | |
978 /* This function can GC */ | |
979 return KEYMAPP (get_keymap (object, 0, 0)) ? Qt : Qnil; | |
980 } | |
981 | |
982 /* Check that OBJECT is a keymap (after dereferencing through any | |
983 symbols). If it is, return it. | |
984 | |
985 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value | |
986 is an autoload form, do the autoload and try again. | |
987 If AUTOLOAD is nonzero, callers must assume GC is possible. | |
988 | |
989 ERRORP controls how we respond if OBJECT isn't a keymap. | |
990 If ERRORP is non-zero, signal an error; otherwise, just return Qnil. | |
991 | |
992 Note that most of the time, we don't want to pursue autoloads. | |
993 Functions like Faccessible_keymaps which scan entire keymap trees | |
994 shouldn't load every autoloaded keymap. I'm not sure about this, | |
995 but it seems to me that only read_key_sequence, Flookup_key, and | |
996 Fdefine_key should cause keymaps to be autoloaded. */ | |
997 | |
998 Lisp_Object | |
999 get_keymap (Lisp_Object object, int errorp, int autoload) | |
1000 { | |
1001 /* This function can GC */ | |
1002 while (1) | |
1003 { | |
1004 Lisp_Object tem = indirect_function (object, 0); | |
1005 | |
1006 if (KEYMAPP (tem)) | |
1007 return tem; | |
1008 /* Should we do an autoload? */ | |
1009 else if (autoload | |
1010 /* (autoload "filename" doc nil keymap) */ | |
1011 && SYMBOLP (object) | |
1012 && CONSP (tem) | |
1013 && EQ (XCAR (tem), Qautoload) | |
1014 && EQ (Fcar (Fcdr (Fcdr (Fcdr (Fcdr (tem))))), Qkeymap)) | |
1015 { | |
970 | 1016 /* do_autoload GCPROs both arguments */ |
428 | 1017 do_autoload (tem, object); |
1018 } | |
1019 else if (errorp) | |
1020 object = wrong_type_argument (Qkeymapp, object); | |
1021 else | |
1022 return Qnil; | |
1023 } | |
1024 } | |
1025 | |
1026 /* Given OBJECT which was found in a slot in a keymap, | |
1027 trace indirect definitions to get the actual definition of that slot. | |
1028 An indirect definition is a list of the form | |
1029 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one | |
1030 and INDEX is an ASCII code, or a cons of (KEYSYM . MODIFIERS). | |
1031 */ | |
1032 static Lisp_Object | |
1033 get_keyelt (Lisp_Object object, int accept_default) | |
1034 { | |
1035 /* This function can GC */ | |
1036 Lisp_Object map; | |
1037 | |
1038 tail_recurse: | |
1039 if (!CONSP (object)) | |
1040 return object; | |
1041 | |
1042 { | |
1043 struct gcpro gcpro1; | |
1044 GCPRO1 (object); | |
1045 map = XCAR (object); | |
1046 map = get_keymap (map, 0, 1); | |
1047 UNGCPRO; | |
1048 } | |
1049 /* If the contents are (KEYMAP . ELEMENT), go indirect. */ | |
1050 if (!NILP (map)) | |
1051 { | |
1052 Lisp_Object idx = Fcdr (object); | |
934 | 1053 Lisp_Key_Data indirection; |
428 | 1054 if (CHARP (idx)) |
1055 { | |
934 | 1056 Lisp_Object event = Fmake_event (Qnil, Qnil); |
1057 struct gcpro gcpro1; | |
1058 GCPRO1 (event); | |
1059 character_to_event (XCHAR (idx), XEVENT (event), | |
1060 XCONSOLE (Vselected_console), 0, 0); | |
1204 | 1061 indirection.keysym = XEVENT_KEY_KEYSYM (event); |
1062 indirection.modifiers = XEVENT_KEY_MODIFIERS (event); | |
1063 UNGCPRO; | |
428 | 1064 } |
1065 else if (CONSP (idx)) | |
1066 { | |
1067 if (!INTP (XCDR (idx))) | |
1068 return Qnil; | |
1069 indirection.keysym = XCAR (idx); | |
442 | 1070 indirection.modifiers = (unsigned char) XINT (XCDR (idx)); |
428 | 1071 } |
1072 else if (SYMBOLP (idx)) | |
1073 { | |
1074 indirection.keysym = idx; | |
934 | 1075 SET_KEY_DATA_MODIFIERS (&indirection, XINT (XCDR (idx))); |
428 | 1076 } |
1077 else | |
1078 { | |
1079 /* Random junk */ | |
1080 return Qnil; | |
1081 } | |
1082 return raw_lookup_key (map, &indirection, 1, 0, accept_default); | |
1083 } | |
1084 else if (STRINGP (XCAR (object))) | |
1085 { | |
1086 /* If the keymap contents looks like (STRING . DEFN), | |
1087 use DEFN. | |
1088 Keymap alist elements like (CHAR MENUSTRING . DEFN) | |
1089 will be used by HierarKey menus. */ | |
1090 object = XCDR (object); | |
1091 goto tail_recurse; | |
1092 } | |
1093 else | |
1094 { | |
1095 /* Anything else is really the value. */ | |
1096 return object; | |
1097 } | |
1098 } | |
1099 | |
1100 static Lisp_Object | |
934 | 1101 keymap_lookup_1 (Lisp_Object keymap, const Lisp_Key_Data *key, |
428 | 1102 int accept_default) |
1103 { | |
1104 /* This function can GC */ | |
934 | 1105 return get_keyelt (keymap_lookup_directly (keymap, |
1106 KEY_DATA_KEYSYM (key), | |
1107 KEY_DATA_MODIFIERS (key)), | |
1108 accept_default); | |
428 | 1109 } |
1110 | |
1111 | |
1112 /************************************************************************/ | |
1113 /* Copying keymaps */ | |
1114 /************************************************************************/ | |
1115 | |
1116 struct copy_keymap_inverse_closure | |
1117 { | |
1118 Lisp_Object inverse_table; | |
1119 }; | |
1120 | |
1121 static int | |
1122 copy_keymap_inverse_mapper (Lisp_Object key, Lisp_Object value, | |
1123 void *copy_keymap_inverse_closure) | |
1124 { | |
1125 struct copy_keymap_inverse_closure *closure = | |
1126 (struct copy_keymap_inverse_closure *) copy_keymap_inverse_closure; | |
1127 | |
1128 /* copy-sequence deals with dotted lists. */ | |
1129 if (CONSP (value)) | |
1130 value = Fcopy_list (value); | |
1131 Fputhash (key, value, closure->inverse_table); | |
1132 | |
1133 return 0; | |
1134 } | |
1135 | |
1136 | |
1137 static Lisp_Object | |
1138 copy_keymap_internal (Lisp_Keymap *keymap) | |
1139 { | |
1140 Lisp_Object nkm = make_keymap (0); | |
1141 Lisp_Keymap *new_keymap = XKEYMAP (nkm); | |
1142 struct copy_keymap_inverse_closure copy_keymap_inverse_closure; | |
1143 copy_keymap_inverse_closure.inverse_table = keymap->inverse_table; | |
1144 | |
1145 new_keymap->parents = Fcopy_sequence (keymap->parents); | |
1146 new_keymap->sub_maps_cache = Qnil; /* No submaps */ | |
1147 new_keymap->table = Fcopy_hash_table (keymap->table); | |
1148 new_keymap->inverse_table = Fcopy_hash_table (keymap->inverse_table); | |
1149 new_keymap->default_binding = keymap->default_binding; | |
1150 /* After copying the inverse map, we need to copy the conses which | |
1151 are its values, lest they be shared by the copy, and mangled. | |
1152 */ | |
1153 elisp_maphash (copy_keymap_inverse_mapper, keymap->inverse_table, | |
1154 ©_keymap_inverse_closure); | |
1155 return nkm; | |
1156 } | |
1157 | |
1158 | |
1159 static Lisp_Object copy_keymap (Lisp_Object keymap); | |
1160 | |
1161 struct copy_keymap_closure | |
1162 { | |
1163 Lisp_Keymap *self; | |
1164 }; | |
1165 | |
1166 static int | |
1167 copy_keymap_mapper (Lisp_Object key, Lisp_Object value, | |
1168 void *copy_keymap_closure) | |
1169 { | |
1170 /* This function can GC */ | |
1171 struct copy_keymap_closure *closure = | |
1172 (struct copy_keymap_closure *) copy_keymap_closure; | |
1173 | |
1174 /* When we encounter a keymap which is indirected through a | |
1175 symbol, we need to copy the sub-map. In v18, the form | |
1176 (lookup-key (copy-keymap global-map) "\C-x") | |
3025 | 1177 returned a new keymap, not the symbol `Control-X-prefix'. |
428 | 1178 */ |
1179 value = get_keymap (value, 0, 1); /* #### autoload GC-safe here? */ | |
1180 if (KEYMAPP (value)) | |
1181 keymap_store_internal (key, closure->self, | |
1182 copy_keymap (value)); | |
1183 return 0; | |
1184 } | |
1185 | |
1186 static Lisp_Object | |
1187 copy_keymap (Lisp_Object keymap) | |
1188 { | |
1189 /* This function can GC */ | |
1190 struct copy_keymap_closure copy_keymap_closure; | |
1191 | |
1192 keymap = copy_keymap_internal (XKEYMAP (keymap)); | |
1193 copy_keymap_closure.self = XKEYMAP (keymap); | |
1194 elisp_maphash (copy_keymap_mapper, | |
1195 XKEYMAP (keymap)->table, | |
1196 ©_keymap_closure); | |
1197 return keymap; | |
1198 } | |
1199 | |
1200 DEFUN ("copy-keymap", Fcopy_keymap, 1, 1, 0, /* | |
1201 Return a copy of the keymap KEYMAP. | |
1202 The copy starts out with the same definitions of KEYMAP, | |
1203 but changing either the copy or KEYMAP does not affect the other. | |
1204 Any key definitions that are subkeymaps are recursively copied. | |
1205 */ | |
1206 (keymap)) | |
1207 { | |
1208 /* This function can GC */ | |
1209 keymap = get_keymap (keymap, 1, 1); | |
1210 return copy_keymap (keymap); | |
1211 } | |
1212 | |
1213 | |
1214 static int | |
1215 keymap_fullness (Lisp_Object keymap) | |
1216 { | |
1217 /* This function can GC */ | |
1218 int fullness; | |
1219 Lisp_Object sub_maps; | |
1220 struct gcpro gcpro1, gcpro2; | |
1221 | |
1222 keymap = get_keymap (keymap, 1, 1); | |
440 | 1223 fullness = XINT (Fhash_table_count (XKEYMAP (keymap)->table)); |
428 | 1224 GCPRO2 (keymap, sub_maps); |
440 | 1225 for (sub_maps = keymap_submaps (keymap); |
1226 !NILP (sub_maps); | |
1227 sub_maps = XCDR (sub_maps)) | |
428 | 1228 { |
1229 if (MODIFIER_HASH_KEY_BITS (XCAR (XCAR (sub_maps))) != 0) | |
1230 { | |
440 | 1231 Lisp_Object bucky_map = XCDR (XCAR (sub_maps)); |
1232 fullness--; /* don't count bucky maps themselves. */ | |
1233 fullness += keymap_fullness (bucky_map); | |
428 | 1234 } |
1235 } | |
1236 UNGCPRO; | |
1237 return fullness; | |
1238 } | |
1239 | |
1240 DEFUN ("keymap-fullness", Fkeymap_fullness, 1, 1, 0, /* | |
1241 Return the number of bindings in the keymap. | |
1242 */ | |
1243 (keymap)) | |
1244 { | |
1245 /* This function can GC */ | |
1246 return make_int (keymap_fullness (get_keymap (keymap, 1, 1))); | |
1247 } | |
1248 | |
1249 | |
1250 /************************************************************************/ | |
1251 /* Defining keys in keymaps */ | |
1252 /************************************************************************/ | |
1253 | |
1254 /* Given a keysym (should be a symbol, int, char), make sure it's valid | |
1255 and perform any necessary canonicalization. */ | |
1256 | |
1257 static void | |
1258 define_key_check_and_coerce_keysym (Lisp_Object spec, | |
1259 Lisp_Object *keysym, | |
442 | 1260 int modifiers) |
428 | 1261 { |
1262 /* Now, check and massage the trailing keysym specifier. */ | |
1263 if (SYMBOLP (*keysym)) | |
1264 { | |
826 | 1265 if (string_char_length (XSYMBOL (*keysym)->name) == 1) |
428 | 1266 { |
1267 Lisp_Object ream_gcc_up_the_ass = | |
867 | 1268 make_char (string_ichar (XSYMBOL (*keysym)->name, 0)); |
428 | 1269 *keysym = ream_gcc_up_the_ass; |
1270 goto fixnum_keysym; | |
1271 } | |
1272 } | |
1273 else if (CHAR_OR_CHAR_INTP (*keysym)) | |
1274 { | |
1275 CHECK_CHAR_COERCE_INT (*keysym); | |
1276 fixnum_keysym: | |
1277 if (XCHAR (*keysym) < ' ' | |
1278 /* || (XCHAR (*keysym) >= 128 && XCHAR (*keysym) < 160) */) | |
1279 /* yuck! Can't make the above restriction; too many compatibility | |
1280 problems ... */ | |
563 | 1281 invalid_argument ("keysym char must be printable", *keysym); |
428 | 1282 /* #### This bites! I want to be able to write (control shift a) */ |
442 | 1283 if (modifiers & XEMACS_MOD_SHIFT) |
563 | 1284 invalid_argument |
428 | 1285 ("The `shift' modifier may not be applied to ASCII keysyms", |
1286 spec); | |
1287 } | |
1288 else | |
1289 { | |
563 | 1290 invalid_argument ("Unknown keysym specifier", *keysym); |
428 | 1291 } |
1292 | |
1293 if (SYMBOLP (*keysym)) | |
1294 { | |
867 | 1295 Ibyte *name = XSTRING_DATA (XSYMBOL (*keysym)->name); |
428 | 1296 |
3025 | 1297 /* GNU Emacs uses symbols with the printed representation of keysyms in |
1298 their names, like `M-x', and we use the syntax '(meta x). So, to | |
1299 avoid confusion, notice the M-x syntax and signal an error - | |
1300 because otherwise it would be interpreted as a regular keysym, and | |
1301 would even show up in the list-buffers output, causing confusion | |
1302 to the naive. | |
428 | 1303 |
1304 We can get away with this because none of the X keysym names contain | |
1305 a hyphen (some contain underscore, however). | |
1306 | |
1307 It might be useful to reject keysyms which are not x-valid-keysym- | |
1308 name-p, but that would interfere with various tricks we do to | |
1309 sanitize the Sun keyboards, and would make it trickier to | |
1310 conditionalize a .emacs file for multiple X servers. | |
1311 */ | |
793 | 1312 if (((int) qxestrlen (name) >= 2 && name[1] == '-') |
428 | 1313 #if 1 |
1314 || | |
1315 /* Ok, this is a bit more dubious - prevent people from doing things | |
1316 like (global-set-key 'RET 'something) because that will have the | |
1317 same problem as above. (Gag!) Maybe we should just silently | |
1318 accept these as aliases for the "real" names? | |
1319 */ | |
793 | 1320 (XSTRING_LENGTH (XSYMBOL (*keysym)->name) <= 3 && |
2367 | 1321 (!qxestrcmp_ascii (name, "LFD") || |
1322 !qxestrcmp_ascii (name, "TAB") || | |
1323 !qxestrcmp_ascii (name, "RET") || | |
1324 !qxestrcmp_ascii (name, "ESC") || | |
1325 !qxestrcmp_ascii (name, "DEL") || | |
1326 !qxestrcmp_ascii (name, "SPC") || | |
1327 !qxestrcmp_ascii (name, "BS"))) | |
428 | 1328 #endif /* unused */ |
1329 ) | |
563 | 1330 invalid_argument |
428 | 1331 ("Invalid (FSF Emacs) key format (see doc of define-key)", |
1332 *keysym); | |
1333 | |
1334 /* #### Ok, this is a bit more dubious - make people not lose if they | |
1335 do things like (global-set-key 'RET 'something) because that would | |
1336 otherwise have the same problem as above. (Gag!) We silently | |
1337 accept these as aliases for the "real" names. | |
1338 */ | |
2367 | 1339 else if (!qxestrncmp_ascii (name, "kp_", 3)) |
793 | 1340 { |
1341 /* Likewise, the obsolete keysym binding of kp_.* should not lose. */ | |
1342 DECLARE_EISTRING (temp); | |
1343 eicpy_raw (temp, name, qxestrlen (name)); | |
1344 eisetch_char (temp, 2, '-'); | |
1345 *keysym = Fintern_soft (eimake_string (temp), Qnil); | |
1346 } | |
1347 else if (EQ (*keysym, QLFD)) | |
428 | 1348 *keysym = QKlinefeed; |
1349 else if (EQ (*keysym, QTAB)) | |
1350 *keysym = QKtab; | |
1351 else if (EQ (*keysym, QRET)) | |
1352 *keysym = QKreturn; | |
1353 else if (EQ (*keysym, QESC)) | |
1354 *keysym = QKescape; | |
1355 else if (EQ (*keysym, QDEL)) | |
1356 *keysym = QKdelete; | |
1357 else if (EQ (*keysym, QSPC)) | |
1358 *keysym = QKspace; | |
1359 else if (EQ (*keysym, QBS)) | |
1360 *keysym = QKbackspace; | |
1361 /* Emacs compatibility */ | |
1362 else if (EQ(*keysym, Qdown_mouse_1)) | |
1363 *keysym = Qbutton1; | |
1364 else if (EQ(*keysym, Qdown_mouse_2)) | |
1365 *keysym = Qbutton2; | |
1366 else if (EQ(*keysym, Qdown_mouse_3)) | |
1367 *keysym = Qbutton3; | |
1368 else if (EQ(*keysym, Qdown_mouse_4)) | |
1369 *keysym = Qbutton4; | |
1370 else if (EQ(*keysym, Qdown_mouse_5)) | |
1371 *keysym = Qbutton5; | |
458 | 1372 else if (EQ(*keysym, Qdown_mouse_6)) |
1373 *keysym = Qbutton6; | |
1374 else if (EQ(*keysym, Qdown_mouse_7)) | |
1375 *keysym = Qbutton7; | |
428 | 1376 else if (EQ(*keysym, Qmouse_1)) |
1377 *keysym = Qbutton1up; | |
1378 else if (EQ(*keysym, Qmouse_2)) | |
1379 *keysym = Qbutton2up; | |
1380 else if (EQ(*keysym, Qmouse_3)) | |
1381 *keysym = Qbutton3up; | |
1382 else if (EQ(*keysym, Qmouse_4)) | |
1383 *keysym = Qbutton4up; | |
1384 else if (EQ(*keysym, Qmouse_5)) | |
1385 *keysym = Qbutton5up; | |
458 | 1386 else if (EQ(*keysym, Qmouse_6)) |
1387 *keysym = Qbutton6up; | |
1388 else if (EQ(*keysym, Qmouse_7)) | |
1389 *keysym = Qbutton7up; | |
428 | 1390 } |
1391 } | |
1392 | |
1393 | |
1394 /* Given any kind of key-specifier, return a keysym and modifier mask. | |
1395 Proper canonicalization is performed: | |
1396 | |
1397 -- integers are converted into the equivalent characters. | |
1398 -- one-character strings are converted into the equivalent characters. | |
1399 */ | |
1400 | |
1401 static void | |
934 | 1402 define_key_parser (Lisp_Object spec, Lisp_Key_Data *returned_value) |
428 | 1403 { |
1404 if (CHAR_OR_CHAR_INTP (spec)) | |
1405 { | |
934 | 1406 Lisp_Object event = Fmake_event (Qnil, Qnil); |
1407 struct gcpro gcpro1; | |
1408 GCPRO1 (event); | |
1409 character_to_event (XCHAR_OR_CHAR_INT (spec), XEVENT (event), | |
1410 XCONSOLE (Vselected_console), 0, 0); | |
1204 | 1411 SET_KEY_DATA_KEYSYM (returned_value, XEVENT_KEY_KEYSYM (event)); |
934 | 1412 SET_KEY_DATA_MODIFIERS (returned_value, |
1204 | 1413 XEVENT_KEY_MODIFIERS (event)); |
1414 UNGCPRO; | |
428 | 1415 } |
1416 else if (EVENTP (spec)) | |
1417 { | |
934 | 1418 switch (XEVENT_TYPE (spec)) |
428 | 1419 { |
1420 case key_press_event: | |
1421 { | |
1204 | 1422 SET_KEY_DATA_KEYSYM (returned_value, XEVENT_KEY_KEYSYM (spec)); |
1423 SET_KEY_DATA_MODIFIERS (returned_value, XEVENT_KEY_MODIFIERS (spec)); | |
428 | 1424 break; |
1425 } | |
1426 case button_press_event: | |
1427 case button_release_event: | |
1428 { | |
934 | 1429 int down = (XEVENT_TYPE (spec) == button_press_event); |
1204 | 1430 switch (XEVENT_BUTTON_BUTTON (spec)) |
934 | 1431 { |
1432 case 1: | |
1433 SET_KEY_DATA_KEYSYM (returned_value, (down ? Qbutton1 : Qbutton1up)); | |
1434 break; | |
1435 case 2: | |
1436 SET_KEY_DATA_KEYSYM (returned_value, (down ? Qbutton2 : Qbutton2up)); | |
1437 break; | |
1438 case 3: | |
1439 SET_KEY_DATA_KEYSYM (returned_value, (down ? Qbutton3 : Qbutton3up)); | |
1440 break; | |
1441 case 4: | |
1442 SET_KEY_DATA_KEYSYM (returned_value, (down ? Qbutton4 : Qbutton4up)); | |
1443 break; | |
1444 case 5: | |
1445 SET_KEY_DATA_KEYSYM (returned_value, (down ? Qbutton5 : Qbutton5up)); | |
1446 break; | |
1447 case 6: | |
1448 SET_KEY_DATA_KEYSYM (returned_value, (down ? Qbutton6 : Qbutton6up)); | |
1449 break; | |
1450 case 7: | |
1451 SET_KEY_DATA_KEYSYM (returned_value, (down ? Qbutton7 : Qbutton7up)); | |
1452 break; | |
1453 default: | |
1454 SET_KEY_DATA_KEYSYM (returned_value, (down ? Qbutton0 : Qbutton0up)); | |
1455 break; | |
1456 } | |
1204 | 1457 SET_KEY_DATA_MODIFIERS (returned_value, XEVENT_BUTTON_MODIFIERS (spec)); |
428 | 1458 break; |
1459 } | |
1460 default: | |
563 | 1461 wtaerror ("unable to bind this type of event", spec); |
428 | 1462 } |
1463 } | |
1464 else if (SYMBOLP (spec)) | |
1465 { | |
1466 /* Be nice, allow = to mean (=) */ | |
1467 if (bucky_sym_to_bucky_bit (spec) != 0) | |
563 | 1468 invalid_argument ("Key is a modifier name", spec); |
428 | 1469 define_key_check_and_coerce_keysym (spec, &spec, 0); |
934 | 1470 SET_KEY_DATA_KEYSYM (returned_value, spec); |
1471 SET_KEY_DATA_MODIFIERS (returned_value, 0); | |
428 | 1472 } |
1473 else if (CONSP (spec)) | |
1474 { | |
442 | 1475 int modifiers = 0; |
428 | 1476 Lisp_Object keysym = Qnil; |
1477 Lisp_Object rest = spec; | |
1478 | |
1479 /* First, parse out the leading modifier symbols. */ | |
1480 while (CONSP (rest)) | |
1481 { | |
442 | 1482 int modifier; |
428 | 1483 |
1484 keysym = XCAR (rest); | |
1485 modifier = bucky_sym_to_bucky_bit (keysym); | |
1486 modifiers |= modifier; | |
1487 if (!NILP (XCDR (rest))) | |
1488 { | |
1489 if (! modifier) | |
563 | 1490 invalid_argument ("Unknown modifier", keysym); |
428 | 1491 } |
1492 else | |
1493 { | |
1494 if (modifier) | |
563 | 1495 sferror ("Nothing but modifiers here", |
428 | 1496 spec); |
1497 } | |
1498 rest = XCDR (rest); | |
1499 QUIT; | |
1500 } | |
1501 if (!NILP (rest)) | |
563 | 1502 signal_error (Qlist_formation_error, |
1503 "List must be nil-terminated", spec); | |
428 | 1504 |
1505 define_key_check_and_coerce_keysym (spec, &keysym, modifiers); | |
934 | 1506 SET_KEY_DATA_KEYSYM(returned_value, keysym); |
1507 SET_KEY_DATA_MODIFIERS (returned_value, modifiers); | |
428 | 1508 } |
1509 else | |
1510 { | |
563 | 1511 invalid_argument ("Unknown key-sequence specifier", |
428 | 1512 spec); |
1513 } | |
1514 } | |
1515 | |
1516 /* Used by character-to-event */ | |
1517 void | |
1518 key_desc_list_to_event (Lisp_Object list, Lisp_Object event, | |
1519 int allow_menu_events) | |
1520 { | |
934 | 1521 Lisp_Key_Data raw_key; |
428 | 1522 |
1523 if (allow_menu_events && | |
1524 CONSP (list) && | |
1525 /* #### where the hell does this come from? */ | |
1526 EQ (XCAR (list), Qmenu_selection)) | |
1527 { | |
1528 Lisp_Object fn, arg; | |
1529 if (! NILP (Fcdr (Fcdr (list)))) | |
563 | 1530 invalid_argument ("Invalid menu event desc", list); |
428 | 1531 arg = Fcar (Fcdr (list)); |
1532 if (SYMBOLP (arg)) | |
1533 fn = Qcall_interactively; | |
1534 else | |
1535 fn = Qeval; | |
934 | 1536 XSET_EVENT_TYPE (event, misc_user_event); |
1204 | 1537 XSET_EVENT_CHANNEL (event, wrap_frame (selected_frame ())); |
1538 XSET_EVENT_MISC_USER_FUNCTION (event, fn); | |
1539 XSET_EVENT_MISC_USER_OBJECT (event, arg); | |
428 | 1540 return; |
1541 } | |
1542 | |
1543 define_key_parser (list, &raw_key); | |
1544 | |
1545 if (EQ (raw_key.keysym, Qbutton0) || EQ (raw_key.keysym, Qbutton0up) || | |
1546 EQ (raw_key.keysym, Qbutton1) || EQ (raw_key.keysym, Qbutton1up) || | |
1547 EQ (raw_key.keysym, Qbutton2) || EQ (raw_key.keysym, Qbutton2up) || | |
1548 EQ (raw_key.keysym, Qbutton3) || EQ (raw_key.keysym, Qbutton3up) || | |
1549 EQ (raw_key.keysym, Qbutton4) || EQ (raw_key.keysym, Qbutton4up) || | |
1550 EQ (raw_key.keysym, Qbutton5) || EQ (raw_key.keysym, Qbutton5up) || | |
1551 EQ (raw_key.keysym, Qbutton6) || EQ (raw_key.keysym, Qbutton6up) || | |
1552 EQ (raw_key.keysym, Qbutton7) || EQ (raw_key.keysym, Qbutton7up)) | |
563 | 1553 invalid_operation ("Mouse-clicks can't appear in saved keyboard macros", |
1554 Qunbound); | |
428 | 1555 |
934 | 1556 XSET_EVENT_CHANNEL (event, Vselected_console); |
1557 XSET_EVENT_TYPE (event, key_press_event); | |
1204 | 1558 XSET_EVENT_KEY_KEYSYM (event, raw_key.keysym); |
1559 XSET_EVENT_KEY_MODIFIERS (event, KEY_DATA_MODIFIERS (&raw_key)); | |
428 | 1560 } |
1561 | |
1562 | |
1563 int | |
1204 | 1564 event_matches_key_specifier_p (Lisp_Object event, Lisp_Object key_specifier) |
428 | 1565 { |
446 | 1566 Lisp_Object event2 = Qnil; |
428 | 1567 int retval; |
1568 struct gcpro gcpro1; | |
1569 | |
1204 | 1570 if (XEVENT_TYPE (event) != key_press_event || NILP (key_specifier) || |
428 | 1571 (INTP (key_specifier) && !CHAR_INTP (key_specifier))) |
1572 return 0; | |
1573 | |
1574 /* if the specifier is an integer such as 27, then it should match | |
3025 | 1575 both of the events `escape' and `control ['. Calling |
1576 Fcharacter_to_event() will only match `escape'. */ | |
428 | 1577 if (CHAR_OR_CHAR_INTP (key_specifier)) |
1578 return (XCHAR_OR_CHAR_INT (key_specifier) | |
2828 | 1579 == event_to_character (event, 0, 0)); |
428 | 1580 |
1581 /* Otherwise, we cannot call event_to_character() because we may | |
1582 be dealing with non-ASCII keystrokes. In any case, if I ask | |
3025 | 1583 for `control [' then I should get exactly that, and not |
1584 `escape'. | |
1585 | |
1586 However, we have to behave differently on TTY's, where `control [' | |
1587 is silently converted into `escape' by the keyboard driver. | |
428 | 1588 In this case, ASCII is the only thing we know about, so we have |
1589 to compare the ASCII values. */ | |
1590 | |
1591 GCPRO1 (event2); | |
1204 | 1592 if (EVENTP (key_specifier)) |
1593 event2 = Fcopy_event (key_specifier, Qnil); | |
1594 else | |
1595 event2 = Fcharacter_to_event (key_specifier, Qnil, Qnil, Qnil); | |
428 | 1596 if (XEVENT (event2)->event_type != key_press_event) |
1597 retval = 0; | |
1204 | 1598 else if (CONSOLE_TTY_P (XCONSOLE (XEVENT_CHANNEL (event)))) |
428 | 1599 { |
1600 int ch1, ch2; | |
1601 | |
2828 | 1602 ch1 = event_to_character (event, 0, 0); |
1603 ch2 = event_to_character (event2, 0, 0); | |
428 | 1604 retval = (ch1 >= 0 && ch2 >= 0 && ch1 == ch2); |
1605 } | |
1204 | 1606 else if (EQ (XEVENT_KEY_KEYSYM (event), XEVENT_KEY_KEYSYM (event2)) && |
1607 XEVENT_KEY_MODIFIERS (event) == XEVENT_KEY_MODIFIERS (event2)) | |
428 | 1608 retval = 1; |
1609 else | |
1610 retval = 0; | |
1611 Fdeallocate_event (event2); | |
1612 UNGCPRO; | |
1613 return retval; | |
1614 } | |
1615 | |
1616 static int | |
934 | 1617 meta_prefix_char_p (const Lisp_Key_Data *key) |
428 | 1618 { |
934 | 1619 Lisp_Object event = Fmake_event (Qnil, Qnil); |
1620 struct gcpro gcpro1; | |
1204 | 1621 int retval; |
1622 | |
934 | 1623 GCPRO1 (event); |
1624 | |
1625 XSET_EVENT_TYPE (event, key_press_event); | |
1626 XSET_EVENT_CHANNEL (event, Vselected_console); | |
1204 | 1627 XSET_EVENT_KEY_KEYSYM (event, KEY_DATA_KEYSYM (key)); |
1628 XSET_EVENT_KEY_MODIFIERS (event, KEY_DATA_MODIFIERS (key)); | |
1629 retval = event_matches_key_specifier_p (event, Vmeta_prefix_char); | |
1630 UNGCPRO; | |
1631 return retval; | |
428 | 1632 } |
1633 | |
1634 DEFUN ("event-matches-key-specifier-p", Fevent_matches_key_specifier_p, 2, 2, 0, /* | |
1635 Return non-nil if EVENT matches KEY-SPECIFIER. | |
1636 This can be useful, e.g., to determine if the user pressed `help-char' or | |
1637 `quit-char'. | |
1204 | 1638 |
1639 KEY-SPECIFIER can be a character, integer, a symbol, a list of modifiers | |
1640 and symbols, or an event. | |
1641 | |
1642 What this actually happens is this: | |
1643 | |
1644 \(1) Return no, if EVENT is not a key press event or if KEY-SPECIFIER is nil | |
1645 or an integer that cannot be converted to a character. | |
1646 | |
1647 \(2) If KEY-SPECIFIER is a character or integer, | |
1648 (event-to-character EVENT nil nil nil) is called, and the characters are | |
1649 compared to get the result. The reason for special-casing this and doing | |
1650 it this way is to ensure that, e.g., a KEY-SPECIFIER of 27 matches both | |
1651 a key-press `escape' and a key-press `control ['. #### Think about META | |
1652 argument to event-to-character. | |
1653 | |
1654 \(3) If KEY-SPECIFIER is an event, fine; else, convert to an event using | |
1655 \(character-to-event KEY-SPECIFIER nil nil nil). If EVENT is not on a TTY, | |
1656 we just compare keysyms and modifiers and return yes if both are equal. | |
1657 For TTY, we do character-level comparison by converting both to a character | |
1658 with (event-to-character ... nil nil nil) and comparing the characters. | |
1659 | |
428 | 1660 */ |
1661 (event, key_specifier)) | |
1662 { | |
1663 CHECK_LIVE_EVENT (event); | |
1204 | 1664 return (event_matches_key_specifier_p (event, key_specifier) ? Qt : Qnil); |
428 | 1665 } |
1204 | 1666 #define MACROLET(k, m) do { \ |
1667 SET_KEY_DATA_KEYSYM (returned_value, k); \ | |
1668 SET_KEY_DATA_MODIFIERS (returned_value, m); \ | |
1669 RETURN_SANS_WARNINGS; \ | |
934 | 1670 } while (0) |
428 | 1671 /* ASCII grunge. |
1672 Given a keysym, return another keysym/modifier pair which could be | |
1673 considered the same key in an ASCII world. Backspace returns ^H, for | |
1674 example. | |
1675 */ | |
1676 static void | |
934 | 1677 define_key_alternate_name (Lisp_Key_Data *key, |
1678 Lisp_Key_Data *returned_value) | |
428 | 1679 { |
934 | 1680 Lisp_Object keysym = KEY_DATA_KEYSYM (key); |
1681 int modifiers = KEY_DATA_MODIFIERS (key); | |
442 | 1682 int modifiers_sans_control = (modifiers & (~XEMACS_MOD_CONTROL)); |
1683 int modifiers_sans_meta = (modifiers & (~XEMACS_MOD_META)); | |
934 | 1684 SET_KEY_DATA_KEYSYM (returned_value, Qnil); /* By default, no "alternate" key */ |
1685 SET_KEY_DATA_MODIFIERS (returned_value, 0); | |
442 | 1686 if (modifiers_sans_meta == XEMACS_MOD_CONTROL) |
428 | 1687 { |
722 | 1688 if (EQ (keysym, QKspace)) |
428 | 1689 MACROLET (make_char ('@'), modifiers); |
1690 else if (!CHARP (keysym)) | |
1691 return; | |
1692 else switch (XCHAR (keysym)) | |
1693 { | |
1694 case '@': /* c-@ => c-space */ | |
1695 MACROLET (QKspace, modifiers); | |
1696 case 'h': /* c-h => backspace */ | |
1697 MACROLET (QKbackspace, modifiers_sans_control); | |
1698 case 'i': /* c-i => tab */ | |
1699 MACROLET (QKtab, modifiers_sans_control); | |
1700 case 'j': /* c-j => linefeed */ | |
1701 MACROLET (QKlinefeed, modifiers_sans_control); | |
1702 case 'm': /* c-m => return */ | |
1703 MACROLET (QKreturn, modifiers_sans_control); | |
1704 case '[': /* c-[ => escape */ | |
1705 MACROLET (QKescape, modifiers_sans_control); | |
1706 default: | |
1707 return; | |
1708 } | |
1709 } | |
1710 else if (modifiers_sans_meta != 0) | |
1711 return; | |
1712 else if (EQ (keysym, QKbackspace)) /* backspace => c-h */ | |
442 | 1713 MACROLET (make_char ('h'), (modifiers | XEMACS_MOD_CONTROL)); |
428 | 1714 else if (EQ (keysym, QKtab)) /* tab => c-i */ |
442 | 1715 MACROLET (make_char ('i'), (modifiers | XEMACS_MOD_CONTROL)); |
428 | 1716 else if (EQ (keysym, QKlinefeed)) /* linefeed => c-j */ |
442 | 1717 MACROLET (make_char ('j'), (modifiers | XEMACS_MOD_CONTROL)); |
428 | 1718 else if (EQ (keysym, QKreturn)) /* return => c-m */ |
442 | 1719 MACROLET (make_char ('m'), (modifiers | XEMACS_MOD_CONTROL)); |
428 | 1720 else if (EQ (keysym, QKescape)) /* escape => c-[ */ |
442 | 1721 MACROLET (make_char ('['), (modifiers | XEMACS_MOD_CONTROL)); |
428 | 1722 else |
1723 return; | |
1724 #undef MACROLET | |
1725 } | |
1726 | |
1727 static void | |
1728 ensure_meta_prefix_char_keymapp (Lisp_Object keys, int indx, | |
1729 Lisp_Object keymap) | |
1730 { | |
1731 /* This function can GC */ | |
1732 Lisp_Object new_keys; | |
1733 int i; | |
1734 Lisp_Object mpc_binding; | |
934 | 1735 Lisp_Key_Data meta_key; |
428 | 1736 if (NILP (Vmeta_prefix_char) || |
1737 (INTP (Vmeta_prefix_char) && !CHAR_INTP (Vmeta_prefix_char))) | |
1738 return; | |
1739 | |
1740 define_key_parser (Vmeta_prefix_char, &meta_key); | |
1741 mpc_binding = keymap_lookup_1 (keymap, &meta_key, 0); | |
1742 if (NILP (mpc_binding) || !NILP (Fkeymapp (mpc_binding))) | |
1743 return; | |
1744 | |
1745 if (indx == 0) | |
1746 new_keys = keys; | |
1747 else if (STRINGP (keys)) | |
1748 new_keys = Fsubstring (keys, Qzero, make_int (indx)); | |
1749 else if (VECTORP (keys)) | |
1750 { | |
1751 new_keys = make_vector (indx, Qnil); | |
1752 for (i = 0; i < indx; i++) | |
1753 XVECTOR_DATA (new_keys) [i] = XVECTOR_DATA (keys) [i]; | |
1754 } | |
1755 else | |
442 | 1756 { |
1757 new_keys = Qnil; | |
2500 | 1758 ABORT (); |
442 | 1759 } |
428 | 1760 |
1761 if (EQ (keys, new_keys)) | |
563 | 1762 signal_ferror_with_frob (Qinvalid_operation, mpc_binding, |
1763 "can't bind %s: %s has a non-keymap binding", | |
1764 (char *) XSTRING_DATA (Fkey_description (keys)), | |
1765 (char *) XSTRING_DATA (Fsingle_key_description | |
1766 (Vmeta_prefix_char))); | |
428 | 1767 else |
563 | 1768 signal_ferror_with_frob (Qinvalid_operation, mpc_binding, |
1769 "can't bind %s: %s %s has a non-keymap binding", | |
1770 (char *) XSTRING_DATA (Fkey_description (keys)), | |
1771 (char *) XSTRING_DATA (Fkey_description | |
1772 (new_keys)), | |
1773 (char *) XSTRING_DATA (Fsingle_key_description | |
1774 (Vmeta_prefix_char))); | |
428 | 1775 } |
1776 | |
1777 DEFUN ("define-key", Fdefine_key, 3, 3, 0, /* | |
1778 Define key sequence KEYS, in KEYMAP, as DEF. | |
1779 KEYMAP is a keymap object. | |
1780 KEYS is the sequence of keystrokes to bind, described below. | |
1781 DEF is anything that can be a key's definition: | |
1782 nil (means key is undefined in this keymap); | |
1783 a command (a Lisp function suitable for interactive calling); | |
1784 a string or key sequence vector (treated as a keyboard macro); | |
1785 a keymap (to define a prefix key); | |
1786 a symbol; when the key is looked up, the symbol will stand for its | |
1787 function definition, that should at that time be one of the above, | |
1788 or another symbol whose function definition is used, and so on. | |
1789 a cons (STRING . DEFN), meaning that DEFN is the definition | |
1790 (DEFN should be a valid definition in its own right); | |
1791 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP. | |
1792 | |
1793 Contrary to popular belief, the world is not ASCII. When running under a | |
771 | 1794 window system, XEmacs can tell the difference between, for example, the |
428 | 1795 keystrokes control-h, control-shift-h, and backspace. You can, in fact, |
1796 bind different commands to each of these. | |
1797 | |
1798 A `key sequence' is a set of keystrokes. A `keystroke' is a keysym and some | |
1799 set of modifiers (such as control and meta). A `keysym' is what is printed | |
1800 on the keys on your keyboard. | |
1801 | |
1802 A keysym may be represented by a symbol, or (if and only if it is equivalent | |
2828 | 1803 to a character with a code in the range 32 - 255) by a character or its |
1804 equivalent code. The `A' key may be represented by the symbol `A', the | |
1805 character `?A', or by the number 65. The `break' key may be represented | |
1806 only by the symbol `break'. | |
428 | 1807 |
1808 A keystroke may be represented by a list: the last element of the list | |
1809 is the key (a symbol, character, or number, as above) and the | |
1810 preceding elements are the symbolic names of modifier keys (control, | |
1811 meta, super, hyper, alt, and shift). Thus, the sequence control-b is | |
1812 represented by the forms `(control b)', `(control ?b)', and `(control | |
1813 98)'. A keystroke may also be represented by an event object, as | |
1814 returned by the `next-command-event' and `read-key-sequence' | |
1815 functions. | |
1816 | |
1817 Note that in this context, the keystroke `control-b' is *not* represented | |
1818 by the number 2 (the ASCII code for ^B) or the character `?\^B'. See below. | |
1819 | |
1820 The `shift' modifier is somewhat of a special case. You should not (and | |
1821 cannot) use `(meta shift a)' to mean `(meta A)', since for characters that | |
1822 have ASCII equivalents, the state of the shift key is implicit in the | |
1823 keysym (a vs. A). You also cannot say `(shift =)' to mean `+', as that | |
1824 sort of thing varies from keyboard to keyboard. The shift modifier is for | |
1825 use only with characters that do not have a second keysym on the same key, | |
1826 such as `backspace' and `tab'. | |
1827 | |
1828 A key sequence is a vector of keystrokes. As a degenerate case, elements | |
1829 of this vector may also be keysyms if they have no modifiers. That is, | |
1830 the `A' keystroke is represented by all of these forms: | |
1831 A ?A 65 (A) (?A) (65) | |
1832 [A] [?A] [65] [(A)] [(?A)] [(65)] | |
1833 | |
1834 the `control-a' keystroke is represented by these forms: | |
1835 (control A) (control ?A) (control 65) | |
1836 [(control A)] [(control ?A)] [(control 65)] | |
1837 the key sequence `control-c control-a' is represented by these forms: | |
1838 [(control c) (control a)] [(control ?c) (control ?a)] | |
1839 [(control 99) (control 65)] etc. | |
1840 | |
1841 Mouse button clicks work just like keypresses: (control button1) means | |
1842 pressing the left mouse button while holding down the control key. | |
1843 \[(control c) (shift button3)] means control-c, hold shift, click right. | |
1844 | |
1845 Commands may be bound to the mouse-button up-stroke rather than the down- | |
1846 stroke as well. `button1' means the down-stroke, and `button1up' means the | |
1847 up-stroke. Different commands may be bound to the up and down strokes, | |
1848 though that is probably not what you want, so be careful. | |
1849 | |
1850 For backward compatibility, a key sequence may also be represented by a | |
1851 string. In this case, it represents the key sequence(s) that would | |
1852 produce that sequence of ASCII characters in a purely ASCII world. For | |
1853 example, a string containing the ASCII backspace character, "\\^H", would | |
1854 represent two key sequences: `(control h)' and `backspace'. Binding a | |
1855 command to this will actually bind both of those key sequences. Likewise | |
1856 for the following pairs: | |
1857 | |
1858 control h backspace | |
1859 control i tab | |
1860 control m return | |
1861 control j linefeed | |
1862 control [ escape | |
1863 control @ control space | |
1864 | |
1865 After binding a command to two key sequences with a form like | |
1866 | |
1867 (define-key global-map "\\^X\\^I" \'command-1) | |
1868 | |
1869 it is possible to redefine only one of those sequences like so: | |
1870 | |
1871 (define-key global-map [(control x) (control i)] \'command-2) | |
1872 (define-key global-map [(control x) tab] \'command-3) | |
1873 | |
1874 Of course, all of this applies only when running under a window system. If | |
1875 you're talking to XEmacs through a TTY connection, you don't get any of | |
1876 these features. | |
771 | 1877 |
1878 To find out programmatically what a key is bound to, use `key-binding' to | |
1879 check all applicable keymaps, or `lookup-key' to check a specific keymap. | |
1880 The documentation for `key-binding' also contains a description of which | |
1881 keymaps are applicable in various situations. `where-is-internal' does | |
1882 the opposite of `key-binding', i.e. searches keymaps for the keys that | |
1883 map to a particular binding. | |
1884 | |
1885 If you are confused about why a particular key sequence is generating a | |
1886 particular binding, and looking through the keymaps doesn't help, setting | |
1887 the variable `debug-emacs-events' may help. If not, try checking | |
1888 what's in `function-key-map' and `key-translation-map'. | |
428 | 1889 */ |
1890 (keymap, keys, def)) | |
1891 { | |
1892 /* This function can GC */ | |
1893 int idx; | |
1894 int metized = 0; | |
1895 int len; | |
1896 int ascii_hack; | |
1897 struct gcpro gcpro1, gcpro2, gcpro3; | |
1898 | |
1899 if (VECTORP (keys)) | |
1900 len = XVECTOR_LENGTH (keys); | |
1901 else if (STRINGP (keys)) | |
826 | 1902 len = string_char_length (keys); |
428 | 1903 else if (CHAR_OR_CHAR_INTP (keys) || SYMBOLP (keys) || CONSP (keys)) |
1904 { | |
1905 if (!CONSP (keys)) keys = list1 (keys); | |
1906 len = 1; | |
1907 keys = make_vector (1, keys); /* this is kinda sleazy. */ | |
1908 } | |
1909 else | |
1910 { | |
1911 keys = wrong_type_argument (Qsequencep, keys); | |
1912 len = XINT (Flength (keys)); | |
1913 } | |
1914 if (len == 0) | |
1915 return Qnil; | |
1916 | |
1917 GCPRO3 (keymap, keys, def); | |
1918 | |
1919 /* ASCII grunge. | |
1920 When the user defines a key which, in a strictly ASCII world, would be | |
1921 produced by two different keys (^J and linefeed, or ^H and backspace, | |
1922 for example) then the binding will be made for both keysyms. | |
1923 | |
1924 This is done if the user binds a command to a string, as in | |
1925 (define-key map "\^H" 'something), but not when using one of the new | |
1926 syntaxes, like (define-key map '(control h) 'something). | |
1927 */ | |
1928 ascii_hack = (STRINGP (keys)); | |
1929 | |
1930 keymap = get_keymap (keymap, 1, 1); | |
1931 | |
1932 idx = 0; | |
1933 while (1) | |
1934 { | |
1935 Lisp_Object c; | |
934 | 1936 Lisp_Key_Data raw_key1; |
1937 Lisp_Key_Data raw_key2; | |
428 | 1938 if (STRINGP (keys)) |
867 | 1939 c = make_char (string_ichar (keys, idx)); |
428 | 1940 else |
1941 c = XVECTOR_DATA (keys) [idx]; | |
1942 | |
1943 define_key_parser (c, &raw_key1); | |
1944 | |
1945 if (!metized && ascii_hack && meta_prefix_char_p (&raw_key1)) | |
1946 { | |
1947 if (idx == (len - 1)) | |
1948 { | |
1949 /* This is a hack to prevent a binding for the meta-prefix-char | |
1950 from being made in a map which already has a non-empty "meta" | |
1951 submap. That is, we can't let both "escape" and "meta" have | |
1952 a binding in the same keymap. This implies that the idiom | |
1953 (define-key my-map "\e" my-escape-map) | |
1954 (define-key my-escape-map "a" 'my-command) | |
1955 no longer works. That's ok. Instead the luser should do | |
1956 (define-key my-map "\ea" 'my-command) | |
1957 or, more correctly | |
1958 (define-key my-map "\M-a" 'my-command) | |
1959 and then perhaps | |
1960 (defvar my-escape-map (lookup-key my-map "\e")) | |
1961 if the luser really wants the map in a variable. | |
1962 */ | |
440 | 1963 Lisp_Object meta_map; |
428 | 1964 struct gcpro ngcpro1; |
1965 | |
1966 NGCPRO1 (c); | |
442 | 1967 meta_map = Fgethash (MAKE_MODIFIER_HASH_KEY (XEMACS_MOD_META), |
440 | 1968 XKEYMAP (keymap)->table, Qnil); |
1969 if (!NILP (meta_map) | |
1970 && keymap_fullness (meta_map) != 0) | |
563 | 1971 invalid_operation_2 |
440 | 1972 ("Map contains meta-bindings, can't bind", |
1973 Fsingle_key_description (Vmeta_prefix_char), keymap); | |
428 | 1974 NUNGCPRO; |
1975 } | |
1976 else | |
1977 { | |
1978 metized = 1; | |
1979 idx++; | |
1980 continue; | |
1981 } | |
1982 } | |
1983 | |
1984 if (ascii_hack) | |
1985 define_key_alternate_name (&raw_key1, &raw_key2); | |
1986 else | |
1987 { | |
1988 raw_key2.keysym = Qnil; | |
1989 raw_key2.modifiers = 0; | |
1990 } | |
1991 | |
1992 if (metized) | |
1993 { | |
442 | 1994 raw_key1.modifiers |= XEMACS_MOD_META; |
1995 raw_key2.modifiers |= XEMACS_MOD_META; | |
428 | 1996 metized = 0; |
1997 } | |
1998 | |
1999 /* This crap is to make sure that someone doesn't bind something like | |
2000 "C-x M-a" while "C-x ESC" has a non-keymap binding. */ | |
442 | 2001 if (raw_key1.modifiers & XEMACS_MOD_META) |
428 | 2002 ensure_meta_prefix_char_keymapp (keys, idx, keymap); |
2003 | |
2004 if (++idx == len) | |
2005 { | |
2006 keymap_store (keymap, &raw_key1, def); | |
2007 if (ascii_hack && !NILP (raw_key2.keysym)) | |
2008 keymap_store (keymap, &raw_key2, def); | |
2009 UNGCPRO; | |
2010 return def; | |
2011 } | |
2012 | |
2013 { | |
2014 Lisp_Object cmd; | |
2015 struct gcpro ngcpro1; | |
2016 NGCPRO1 (c); | |
2017 | |
2018 cmd = keymap_lookup_1 (keymap, &raw_key1, 0); | |
2019 if (NILP (cmd)) | |
2020 { | |
2021 cmd = Fmake_sparse_keymap (Qnil); | |
2022 XKEYMAP (cmd)->name /* for debugging */ | |
2023 = list2 (make_key_description (&raw_key1, 1), keymap); | |
2024 keymap_store (keymap, &raw_key1, cmd); | |
2025 } | |
2026 if (NILP (Fkeymapp (cmd))) | |
563 | 2027 sferror_2 ("Invalid prefix keys in sequence", |
428 | 2028 c, keys); |
2029 | |
2030 if (ascii_hack && !NILP (raw_key2.keysym) && | |
2031 NILP (keymap_lookup_1 (keymap, &raw_key2, 0))) | |
2032 keymap_store (keymap, &raw_key2, cmd); | |
2033 | |
2034 keymap = get_keymap (cmd, 1, 1); | |
2035 NUNGCPRO; | |
2036 } | |
2037 } | |
2038 } | |
2039 | |
2040 | |
2041 /************************************************************************/ | |
2042 /* Looking up keys in keymaps */ | |
2043 /************************************************************************/ | |
2044 | |
2045 /* We need a very fast (i.e., non-consing) version of lookup-key in order | |
2046 to make where-is-internal really fly. */ | |
2047 | |
2048 struct raw_lookup_key_mapper_closure | |
2049 { | |
2050 int remaining; | |
934 | 2051 const Lisp_Key_Data *raw_keys; |
428 | 2052 int raw_keys_count; |
2053 int keys_so_far; | |
2054 int accept_default; | |
2055 }; | |
2056 | |
2057 static Lisp_Object raw_lookup_key_mapper (Lisp_Object k, void *); | |
2058 | |
2059 /* Caller should gc-protect args (keymaps may autoload) */ | |
2060 static Lisp_Object | |
2061 raw_lookup_key (Lisp_Object keymap, | |
934 | 2062 const Lisp_Key_Data *raw_keys, int raw_keys_count, |
428 | 2063 int keys_so_far, int accept_default) |
2064 { | |
2065 /* This function can GC */ | |
2066 struct raw_lookup_key_mapper_closure c; | |
2067 c.remaining = raw_keys_count - 1; | |
2068 c.raw_keys = raw_keys; | |
2069 c.raw_keys_count = raw_keys_count; | |
2070 c.keys_so_far = keys_so_far; | |
2071 c.accept_default = accept_default; | |
2072 | |
2073 return traverse_keymaps (keymap, Qnil, raw_lookup_key_mapper, &c); | |
2074 } | |
2075 | |
2076 static Lisp_Object | |
2077 raw_lookup_key_mapper (Lisp_Object k, void *arg) | |
2078 { | |
2079 /* This function can GC */ | |
2080 struct raw_lookup_key_mapper_closure *c = | |
2081 (struct raw_lookup_key_mapper_closure *) arg; | |
2082 int accept_default = c->accept_default; | |
2083 int remaining = c->remaining; | |
2084 int keys_so_far = c->keys_so_far; | |
934 | 2085 const Lisp_Key_Data *raw_keys = c->raw_keys; |
428 | 2086 Lisp_Object cmd; |
2087 | |
2088 if (! meta_prefix_char_p (&(raw_keys[0]))) | |
2089 { | |
2090 /* Normal case: every case except the meta-hack (see below). */ | |
2091 cmd = keymap_lookup_1 (k, &(raw_keys[0]), accept_default); | |
2092 | |
2093 if (remaining == 0) | |
2094 /* Return whatever we found if we're out of keys */ | |
2095 ; | |
2096 else if (NILP (cmd)) | |
2097 /* Found nothing (though perhaps parent map may have binding) */ | |
2098 ; | |
2099 else if (NILP (Fkeymapp (cmd))) | |
2100 /* Didn't find a keymap, and we have more keys. | |
2101 * Return a fixnum to indicate that keys were too long. | |
2102 */ | |
2103 cmd = make_int (keys_so_far + 1); | |
2104 else | |
2105 cmd = raw_lookup_key (cmd, raw_keys + 1, remaining, | |
2106 keys_so_far + 1, accept_default); | |
2107 } | |
2108 else | |
2109 { | |
2110 /* This is a hack so that looking up a key-sequence whose last | |
2111 * element is the meta-prefix-char will return the keymap that | |
2112 * the "meta" keys are stored in, if there is no binding for | |
2113 * the meta-prefix-char (and if this map has a "meta" submap). | |
2114 * If this map doesn't have a "meta" submap, then the | |
2115 * meta-prefix-char is looked up just like any other key. | |
2116 */ | |
2117 if (remaining == 0) | |
2118 { | |
2119 /* First look for the prefix-char directly */ | |
2120 cmd = keymap_lookup_1 (k, &(raw_keys[0]), accept_default); | |
2121 if (NILP (cmd)) | |
2122 { | |
2123 /* Do kludgy return of the meta-map */ | |
442 | 2124 cmd = Fgethash (MAKE_MODIFIER_HASH_KEY (XEMACS_MOD_META), |
428 | 2125 XKEYMAP (k)->table, Qnil); |
2126 } | |
2127 } | |
2128 else | |
2129 { | |
2130 /* Search for the prefix-char-prefixed sequence directly */ | |
2131 cmd = keymap_lookup_1 (k, &(raw_keys[0]), accept_default); | |
2132 cmd = get_keymap (cmd, 0, 1); | |
2133 if (!NILP (cmd)) | |
2134 cmd = raw_lookup_key (cmd, raw_keys + 1, remaining, | |
2135 keys_so_far + 1, accept_default); | |
442 | 2136 else if ((raw_keys[1].modifiers & XEMACS_MOD_META) == 0) |
428 | 2137 { |
934 | 2138 Lisp_Key_Data metified; |
428 | 2139 metified.keysym = raw_keys[1].keysym; |
442 | 2140 metified.modifiers = raw_keys[1].modifiers | |
2141 (unsigned char) XEMACS_MOD_META; | |
428 | 2142 |
2143 /* Search for meta-next-char sequence directly */ | |
2144 cmd = keymap_lookup_1 (k, &metified, accept_default); | |
2145 if (remaining == 1) | |
2146 ; | |
2147 else | |
2148 { | |
2149 cmd = get_keymap (cmd, 0, 1); | |
2150 if (!NILP (cmd)) | |
2151 cmd = raw_lookup_key (cmd, raw_keys + 2, remaining - 1, | |
2152 keys_so_far + 2, | |
2153 accept_default); | |
2154 } | |
2155 } | |
2156 } | |
2157 } | |
2158 if (accept_default && NILP (cmd)) | |
2159 cmd = XKEYMAP (k)->default_binding; | |
2160 return cmd; | |
2161 } | |
2162 | |
2163 /* Value is number if `keys' is too long; NIL if valid but has no definition.*/ | |
2164 /* Caller should gc-protect arguments */ | |
2165 static Lisp_Object | |
2166 lookup_keys (Lisp_Object keymap, int nkeys, Lisp_Object *keys, | |
2167 int accept_default) | |
2168 { | |
2169 /* This function can GC */ | |
934 | 2170 Lisp_Key_Data kkk[20]; |
2171 Lisp_Key_Data *raw_keys; | |
428 | 2172 int i; |
2173 | |
2174 if (nkeys == 0) | |
2175 return Qnil; | |
2176 | |
438 | 2177 if (nkeys < countof (kkk)) |
428 | 2178 raw_keys = kkk; |
2179 else | |
934 | 2180 raw_keys = alloca_array (Lisp_Key_Data, nkeys); |
428 | 2181 |
2182 for (i = 0; i < nkeys; i++) | |
2183 { | |
2184 define_key_parser (keys[i], &(raw_keys[i])); | |
2185 } | |
2186 return raw_lookup_key (keymap, raw_keys, nkeys, 0, accept_default); | |
2187 } | |
2188 | |
2189 static Lisp_Object | |
2190 lookup_events (Lisp_Object event_head, int nmaps, Lisp_Object keymaps[], | |
2191 int accept_default) | |
2192 { | |
2193 /* This function can GC */ | |
934 | 2194 Lisp_Key_Data kkk[20]; |
428 | 2195 Lisp_Object event; |
2196 | |
2197 int nkeys; | |
934 | 2198 Lisp_Key_Data *raw_keys; |
428 | 2199 Lisp_Object tem = Qnil; |
2200 struct gcpro gcpro1, gcpro2; | |
2201 int iii; | |
2202 | |
2203 CHECK_LIVE_EVENT (event_head); | |
2204 | |
2205 nkeys = event_chain_count (event_head); | |
2206 | |
438 | 2207 if (nkeys < countof (kkk)) |
428 | 2208 raw_keys = kkk; |
2209 else | |
934 | 2210 raw_keys = alloca_array (Lisp_Key_Data, nkeys); |
428 | 2211 |
2212 nkeys = 0; | |
2213 EVENT_CHAIN_LOOP (event, event_head) | |
2214 define_key_parser (event, &(raw_keys[nkeys++])); | |
2215 GCPRO2 (keymaps[0], event_head); | |
2216 gcpro1.nvars = nmaps; | |
2217 /* ####raw_keys[].keysym slots aren't gc-protected. We rely (but shouldn't) | |
2218 * on somebody else somewhere (obarray) having a pointer to all keysyms. */ | |
2219 for (iii = 0; iii < nmaps; iii++) | |
2220 { | |
2221 tem = raw_lookup_key (keymaps[iii], raw_keys, nkeys, 0, | |
2222 accept_default); | |
2223 if (INTP (tem)) | |
2224 { | |
2225 /* Too long in some local map means don't look at global map */ | |
2226 tem = Qnil; | |
2227 break; | |
2228 } | |
2229 else if (!NILP (tem)) | |
2230 break; | |
2231 } | |
2232 UNGCPRO; | |
2233 return tem; | |
2234 } | |
2235 | |
2236 DEFUN ("lookup-key", Flookup_key, 2, 3, 0, /* | |
2237 In keymap KEYMAP, look up key-sequence KEYS. Return the definition. | |
2238 Nil is returned if KEYS is unbound. See documentation of `define-key' | |
2239 for valid key definitions and key-sequence specifications. | |
2240 A number is returned if KEYS is "too long"; that is, the leading | |
2241 characters fail to be a valid sequence of prefix characters in KEYMAP. | |
444 | 2242 The number is how many key strokes at the front of KEYS it takes to |
2243 reach a non-prefix command. | |
428 | 2244 */ |
2245 (keymap, keys, accept_default)) | |
2246 { | |
2247 /* This function can GC */ | |
2248 if (VECTORP (keys)) | |
2249 return lookup_keys (keymap, | |
2250 XVECTOR_LENGTH (keys), | |
2251 XVECTOR_DATA (keys), | |
2252 !NILP (accept_default)); | |
2253 else if (SYMBOLP (keys) || CHAR_OR_CHAR_INTP (keys) || CONSP (keys)) | |
2254 return lookup_keys (keymap, 1, &keys, !NILP (accept_default)); | |
2255 else if (STRINGP (keys)) | |
2256 { | |
826 | 2257 int length = string_char_length (keys); |
428 | 2258 int i; |
934 | 2259 Lisp_Key_Data *raw_keys = alloca_array (Lisp_Key_Data, length); |
428 | 2260 if (length == 0) |
2261 return Qnil; | |
2262 | |
2263 for (i = 0; i < length; i++) | |
2264 { | |
867 | 2265 Ichar n = string_ichar (keys, i); |
428 | 2266 define_key_parser (make_char (n), &(raw_keys[i])); |
2267 } | |
2268 return raw_lookup_key (keymap, raw_keys, length, 0, | |
2269 !NILP (accept_default)); | |
2270 } | |
2271 else | |
2272 { | |
2273 keys = wrong_type_argument (Qsequencep, keys); | |
2274 return Flookup_key (keymap, keys, accept_default); | |
2275 } | |
2276 } | |
2277 | |
2278 /* Given a key sequence, returns a list of keymaps to search for bindings. | |
2279 Does all manner of semi-hairy heuristics, like looking in the current | |
2280 buffer's map before looking in the global map and looking in the local | |
2281 map of the buffer in which the mouse was clicked in event0 is a click. | |
2282 | |
2283 It would be kind of nice if this were in Lisp so that this semi-hairy | |
2284 semi-heuristic command-lookup behavior could be readily understood and | |
2285 customised. However, this needs to be pretty fast, or performance of | |
2286 keyboard macros goes to shit; putting this in lisp slows macros down | |
2287 2-3x. And they're already slower than v18 by 5-6x. | |
2288 */ | |
2289 | |
2290 struct relevant_maps | |
2291 { | |
2292 int nmaps; | |
647 | 2293 int max_maps; |
428 | 2294 Lisp_Object *maps; |
2295 struct gcpro *gcpro; | |
2296 }; | |
2297 | |
2298 static void get_relevant_extent_keymaps (Lisp_Object pos, | |
2299 Lisp_Object buffer_or_string, | |
2300 Lisp_Object glyph, | |
2301 struct relevant_maps *closure); | |
2302 static void get_relevant_minor_maps (Lisp_Object buffer, | |
2303 struct relevant_maps *closure); | |
2304 | |
2305 static void | |
2306 relevant_map_push (Lisp_Object map, struct relevant_maps *closure) | |
2307 { | |
647 | 2308 int nmaps = closure->nmaps; |
428 | 2309 |
2310 if (!KEYMAPP (map)) | |
2311 return; | |
2312 closure->nmaps = nmaps + 1; | |
2313 if (nmaps < closure->max_maps) | |
2314 { | |
2315 closure->maps[nmaps] = map; | |
2316 closure->gcpro->nvars = nmaps; | |
2317 } | |
2318 } | |
2319 | |
2320 static int | |
2321 get_relevant_keymaps (Lisp_Object keys, | |
2322 int max_maps, Lisp_Object maps[]) | |
2323 { | |
2324 /* This function can GC */ | |
2325 Lisp_Object terminal = Qnil; | |
2326 struct gcpro gcpro1; | |
2327 struct relevant_maps closure; | |
2328 struct console *con; | |
2329 | |
2330 GCPRO1 (*maps); | |
2331 gcpro1.nvars = 0; | |
2332 closure.nmaps = 0; | |
2333 closure.max_maps = max_maps; | |
2334 closure.maps = maps; | |
2335 closure.gcpro = &gcpro1; | |
2336 | |
2337 if (EVENTP (keys)) | |
2338 terminal = event_chain_tail (keys); | |
2339 else if (VECTORP (keys)) | |
2340 { | |
2341 int len = XVECTOR_LENGTH (keys); | |
2342 if (len > 0) | |
2343 terminal = XVECTOR_DATA (keys)[len - 1]; | |
2344 } | |
2345 | |
2346 if (EVENTP (terminal)) | |
2347 { | |
2348 CHECK_LIVE_EVENT (terminal); | |
2349 con = event_console_or_selected (terminal); | |
2350 } | |
2351 else | |
2352 con = XCONSOLE (Vselected_console); | |
2353 | |
2354 if (KEYMAPP (con->overriding_terminal_local_map) | |
2355 || KEYMAPP (Voverriding_local_map)) | |
2356 { | |
2357 if (KEYMAPP (con->overriding_terminal_local_map)) | |
2358 relevant_map_push (con->overriding_terminal_local_map, &closure); | |
2359 if (KEYMAPP (Voverriding_local_map)) | |
2360 relevant_map_push (Voverriding_local_map, &closure); | |
2361 } | |
2362 else if (!EVENTP (terminal) | |
2363 || (XEVENT (terminal)->event_type != button_press_event | |
2364 && XEVENT (terminal)->event_type != button_release_event)) | |
2365 { | |
793 | 2366 Lisp_Object tem = wrap_buffer (current_buffer); |
2367 | |
428 | 2368 /* It's not a mouse event; order of keymaps searched is: |
2369 o keymap of any/all extents under the mouse | |
2370 o minor-mode maps | |
2371 o local-map of current-buffer | |
771 | 2372 o global-tty-map or global-window-system-map |
428 | 2373 o global-map |
2374 */ | |
2375 /* The terminal element of the lookup may be nil or a keysym. | |
2376 In those cases we don't want to check for an extent | |
2377 keymap. */ | |
2378 if (EVENTP (terminal)) | |
2379 { | |
2380 get_relevant_extent_keymaps (make_int (BUF_PT (current_buffer)), | |
2381 tem, Qnil, &closure); | |
2382 } | |
2383 get_relevant_minor_maps (tem, &closure); | |
2384 | |
2385 tem = current_buffer->keymap; | |
2386 if (!NILP (tem)) | |
2387 relevant_map_push (tem, &closure); | |
2388 } | |
2389 #ifdef HAVE_WINDOW_SYSTEM | |
2390 else | |
2391 { | |
2392 /* It's a mouse event; order of keymaps searched is: | |
2393 o vertical-divider-map, if event is over a divider | |
2394 o local-map of mouse-grabbed-buffer | |
2395 o keymap of any/all extents under the mouse | |
2396 if the mouse is over a modeline: | |
2397 o modeline-map of buffer corresponding to that modeline | |
2398 o else, local-map of buffer under the mouse | |
2399 o minor-mode maps | |
2400 o local-map of current-buffer | |
771 | 2401 o global-tty-map or global-window-system-map |
428 | 2402 o global-map |
2403 */ | |
2404 Lisp_Object window = Fevent_window (terminal); | |
2405 | |
2406 if (!NILP (Fevent_over_vertical_divider_p (terminal))) | |
2407 { | |
2408 if (KEYMAPP (Vvertical_divider_map)) | |
2409 relevant_map_push (Vvertical_divider_map, &closure); | |
2410 } | |
2411 | |
2412 if (BUFFERP (Vmouse_grabbed_buffer)) | |
2413 { | |
2414 Lisp_Object map = XBUFFER (Vmouse_grabbed_buffer)->keymap; | |
2415 | |
2416 get_relevant_minor_maps (Vmouse_grabbed_buffer, &closure); | |
2417 if (!NILP (map)) | |
2418 relevant_map_push (map, &closure); | |
2419 } | |
2420 | |
2421 if (!NILP (window)) | |
2422 { | |
2423 Lisp_Object buffer = Fwindow_buffer (window); | |
2424 | |
2425 if (!NILP (buffer)) | |
2426 { | |
2427 if (!NILP (Fevent_over_modeline_p (terminal))) | |
2428 { | |
2429 Lisp_Object map = symbol_value_in_buffer (Qmodeline_map, | |
2430 buffer); | |
2431 | |
2432 get_relevant_extent_keymaps | |
2433 (Fevent_modeline_position (terminal), | |
2434 XBUFFER (buffer)->generated_modeline_string, | |
438 | 2435 Fevent_glyph_extent (terminal), &closure); |
428 | 2436 |
2437 if (!UNBOUNDP (map) && !NILP (map)) | |
2438 relevant_map_push (get_keymap (map, 1, 1), &closure); | |
2439 } | |
2440 else | |
2441 { | |
2442 get_relevant_extent_keymaps (Fevent_point (terminal), buffer, | |
2443 Fevent_glyph_extent (terminal), | |
2444 &closure); | |
2445 } | |
2446 | |
2447 if (!EQ (buffer, Vmouse_grabbed_buffer)) /* already pushed */ | |
2448 { | |
2449 Lisp_Object map = XBUFFER (buffer)->keymap; | |
2450 | |
2451 get_relevant_minor_maps (buffer, &closure); | |
2452 if (!NILP(map)) | |
2453 relevant_map_push (map, &closure); | |
2454 } | |
2455 } | |
2456 } | |
2457 else if (!NILP (Fevent_over_toolbar_p (terminal))) | |
2458 { | |
2459 Lisp_Object map = Fsymbol_value (Qtoolbar_map); | |
2460 | |
2461 if (!UNBOUNDP (map) && !NILP (map)) | |
2462 relevant_map_push (map, &closure); | |
2463 } | |
2464 } | |
2465 #endif /* HAVE_WINDOW_SYSTEM */ | |
2466 | |
771 | 2467 if (CONSOLE_TTY_P (con)) |
2468 relevant_map_push (Vglobal_tty_map, &closure); | |
2469 else | |
2470 relevant_map_push (Vglobal_window_system_map, &closure); | |
2471 | |
428 | 2472 { |
2473 int nmaps = closure.nmaps; | |
2474 /* Silently truncate at 100 keymaps to prevent infinite lossage */ | |
2475 if (nmaps >= max_maps && max_maps > 0) | |
2476 maps[max_maps - 1] = Vcurrent_global_map; | |
2477 else | |
2478 maps[nmaps] = Vcurrent_global_map; | |
2479 UNGCPRO; | |
2480 return nmaps + 1; | |
2481 } | |
2482 } | |
2483 | |
2484 /* Returns a set of keymaps extracted from the extents at POS in | |
2485 BUFFER_OR_STRING. The GLYPH arg, if specified, is one more extent | |
2486 to look for a keymap in, and if it has one, its keymap will be the | |
2487 first element in the list returned. This is so we can correctly | |
2488 search the keymaps associated with glyphs which may be physically | |
2489 disjoint from their extents: for example, if a glyph is out in the | |
2490 margin, we should still consult the keymap of that glyph's extent, | |
2491 which may not itself be under the mouse. | |
2492 */ | |
2493 | |
2494 static void | |
2495 get_relevant_extent_keymaps (Lisp_Object pos, Lisp_Object buffer_or_string, | |
2496 Lisp_Object glyph, | |
2497 struct relevant_maps *closure) | |
2498 { | |
2499 /* This function can GC */ | |
2500 /* the glyph keymap, if any, comes first. | |
2501 (Processing it twice is no big deal: noop.) */ | |
2502 if (!NILP (glyph)) | |
2503 { | |
2504 Lisp_Object keymap = Fextent_property (glyph, Qkeymap, Qnil); | |
2505 if (!NILP (keymap)) | |
2506 relevant_map_push (get_keymap (keymap, 1, 1), closure); | |
2507 } | |
2508 | |
2509 /* Next check the extents at the text position, if any */ | |
2510 if (!NILP (pos)) | |
2511 { | |
2512 Lisp_Object extent; | |
2513 for (extent = Fextent_at (pos, buffer_or_string, Qkeymap, Qnil, Qnil); | |
2514 !NILP (extent); | |
2515 extent = Fextent_at (pos, buffer_or_string, Qkeymap, extent, Qnil)) | |
2516 { | |
2517 Lisp_Object keymap = Fextent_property (extent, Qkeymap, Qnil); | |
2518 if (!NILP (keymap)) | |
2519 relevant_map_push (get_keymap (keymap, 1, 1), closure); | |
2520 QUIT; | |
2521 } | |
2522 } | |
2523 } | |
2524 | |
2525 static Lisp_Object | |
2526 minor_mode_keymap_predicate (Lisp_Object assoc, Lisp_Object buffer) | |
2527 { | |
2528 /* This function can GC */ | |
2529 if (CONSP (assoc)) | |
2530 { | |
2531 Lisp_Object sym = XCAR (assoc); | |
2532 if (SYMBOLP (sym)) | |
2533 { | |
2534 Lisp_Object val = symbol_value_in_buffer (sym, buffer); | |
2535 if (!NILP (val) && !UNBOUNDP (val)) | |
2536 { | |
793 | 2537 return get_keymap (XCDR (assoc), 0, 1); |
428 | 2538 } |
2539 } | |
2540 } | |
2541 return Qnil; | |
2542 } | |
2543 | |
2544 static void | |
2545 get_relevant_minor_maps (Lisp_Object buffer, struct relevant_maps *closure) | |
2546 { | |
2547 /* This function can GC */ | |
2548 Lisp_Object alist; | |
2549 | |
2550 /* Will you ever lose badly if you make this circular! */ | |
2551 for (alist = symbol_value_in_buffer (Qminor_mode_map_alist, buffer); | |
2552 CONSP (alist); | |
2553 alist = XCDR (alist)) | |
2554 { | |
2555 Lisp_Object m = minor_mode_keymap_predicate (XCAR (alist), | |
2556 buffer); | |
2557 if (!NILP (m)) relevant_map_push (m, closure); | |
2558 QUIT; | |
2559 } | |
2560 } | |
2561 | |
2562 /* #### Would map-current-keymaps be a better thing?? */ | |
2563 DEFUN ("current-keymaps", Fcurrent_keymaps, 0, 1, 0, /* | |
2564 Return a list of the current keymaps that will be searched for bindings. | |
2565 This lists keymaps such as the current local map and the minor-mode maps, | |
2566 but does not list the parents of those keymaps. | |
2567 EVENT-OR-KEYS controls which keymaps will be listed. | |
2568 If EVENT-OR-KEYS is a mouse event (or a vector whose last element is a | |
2569 mouse event), the keymaps for that mouse event will be listed (see | |
2570 `key-binding'). Otherwise, the keymaps for key presses will be listed. | |
771 | 2571 See `key-binding' for a description of which keymaps are searched in |
2572 various situations. | |
428 | 2573 */ |
2574 (event_or_keys)) | |
2575 { | |
2576 /* This function can GC */ | |
2577 struct gcpro gcpro1; | |
2578 Lisp_Object maps[100]; | |
2579 Lisp_Object *gubbish = maps; | |
2580 int nmaps; | |
2581 | |
2582 GCPRO1 (event_or_keys); | |
2583 nmaps = get_relevant_keymaps (event_or_keys, countof (maps), | |
2584 gubbish); | |
2585 if (nmaps > countof (maps)) | |
2586 { | |
2587 gubbish = alloca_array (Lisp_Object, nmaps); | |
2588 nmaps = get_relevant_keymaps (event_or_keys, nmaps, gubbish); | |
2589 } | |
2590 UNGCPRO; | |
2591 return Flist (nmaps, gubbish); | |
2592 } | |
2593 | |
2594 DEFUN ("key-binding", Fkey_binding, 1, 2, 0, /* | |
2595 Return the binding for command KEYS in current keymaps. | |
2596 KEYS is a string, a vector of events, or a vector of key-description lists | |
2597 as described in the documentation for the `define-key' function. | |
2598 The binding is probably a symbol with a function definition; see | |
2599 the documentation for `lookup-key' for more information. | |
2600 | |
2601 For key-presses, the order of keymaps searched is: | |
2602 - the `keymap' property of any extent(s) at point; | |
2603 - any applicable minor-mode maps; | |
444 | 2604 - the current local map of the current-buffer; |
771 | 2605 - either `global-tty-map' or `global-window-system-map', depending on |
2606 whether the current console is a TTY or non-TTY console; | |
428 | 2607 - the current global map. |
2608 | |
2609 For mouse-clicks, the order of keymaps searched is: | |
2610 - the current-local-map of the `mouse-grabbed-buffer' if any; | |
2611 - vertical-divider-map, if the event happened over a vertical divider | |
2612 - the `keymap' property of any extent(s) at the position of the click | |
2613 (this includes modeline extents); | |
2614 - the modeline-map of the buffer corresponding to the modeline under | |
2615 the mouse (if the click happened over a modeline); | |
444 | 2616 - the value of `toolbar-map' in the current-buffer (if the click |
428 | 2617 happened over a toolbar); |
444 | 2618 - the current local map of the buffer under the mouse (does not |
428 | 2619 apply to toolbar clicks); |
2620 - any applicable minor-mode maps; | |
771 | 2621 - either `global-tty-map' or `global-window-system-map', depending on |
2622 whether the current console is a TTY or non-TTY console; | |
428 | 2623 - the current global map. |
2624 | |
2625 Note that if `overriding-local-map' or `overriding-terminal-local-map' | |
2626 is non-nil, *only* those two maps and the current global map are searched. | |
771 | 2627 |
2628 Note also that key sequences actually received from the keyboard driver | |
2629 may be processed in various ways to generate the key sequence that is | |
2630 actually looked up in the keymaps. In particular: | |
2631 | |
2632 -- Keysyms are individually passed through `keyboard-translate-table' before | |
2633 any other processing. | |
2634 -- After this, key sequences as a whole are passed through | |
2635 `key-translation-map'. | |
2636 -- The resulting key sequence is actually looked up in the keymaps. | |
2637 -- If there's no binding found, the key sequence is passed through | |
2638 `function-key-map' and looked up again. | |
2639 -- If no binding is found and `retry-undefined-key-binding-unshifted' is | |
2640 set (it usually is) and the final keysym is an uppercase character, | |
2641 we lowercase it and start over from the `key-translation-map' stage. | |
2642 -- If no binding is found and we're on MS Windows and have international | |
2643 support, we successively remap the key sequence using the keyboard layouts | |
2644 of various default locales (current language environment, user default, | |
2645 system default, US ASCII) and try again. This makes (e.g.) sequences | |
2646 such as `C-x b' work in a Russian locale, where the alphabetic keys are | |
2647 actually generating Russian characters and not the Roman letters written | |
2648 on the keycaps. (Not yet implemented) | |
2649 -- Finally, if the last keystroke matches `help-char', we automatically | |
2650 generate and display a list of possible key sequences and bindings | |
2651 given the prefix so far generated. | |
428 | 2652 */ |
2653 (keys, accept_default)) | |
2654 { | |
2655 /* This function can GC */ | |
2656 int i; | |
2657 Lisp_Object maps[100]; | |
2658 int nmaps; | |
2659 struct gcpro gcpro1, gcpro2; | |
2660 GCPRO2 (keys, accept_default); /* get_relevant_keymaps may autoload */ | |
2661 | |
2662 nmaps = get_relevant_keymaps (keys, countof (maps), maps); | |
2663 | |
2664 UNGCPRO; | |
2665 | |
2666 if (EVENTP (keys)) /* unadvertised "feature" for the future */ | |
2667 return lookup_events (keys, nmaps, maps, !NILP (accept_default)); | |
2668 | |
2669 for (i = 0; i < nmaps; i++) | |
2670 { | |
2671 Lisp_Object tem = Flookup_key (maps[i], keys, | |
2672 accept_default); | |
2673 if (INTP (tem)) | |
2674 { | |
2675 /* Too long in some local map means don't look at global map */ | |
2676 return Qnil; | |
2677 } | |
2678 else if (!NILP (tem)) | |
2679 return tem; | |
2680 } | |
2681 return Qnil; | |
2682 } | |
2683 | |
2684 static Lisp_Object | |
2685 process_event_binding_result (Lisp_Object result) | |
2686 { | |
2687 if (EQ (result, Qundefined)) | |
3025 | 2688 /* The suppress-keymap function binds keys to `undefined' - special-case |
428 | 2689 that here, so that being bound to that has the same error-behavior as |
2690 not being defined at all. | |
2691 */ | |
2692 result = Qnil; | |
2693 if (!NILP (result)) | |
2694 { | |
2695 Lisp_Object map; | |
2696 /* Snap out possible keymap indirections */ | |
2697 map = get_keymap (result, 0, 1); | |
2698 if (!NILP (map)) | |
2699 result = map; | |
2700 } | |
2701 | |
2702 return result; | |
2703 } | |
2704 | |
2705 /* Attempts to find a command corresponding to the event-sequence | |
2706 whose head is event0 (sequence is threaded though event_next). | |
2707 | |
2708 The return value will be | |
2709 | |
2710 -- nil (there is no binding; this will also be returned | |
2711 whenever the event chain is "too long", i.e. there | |
2712 is a non-nil, non-keymap binding for a prefix of | |
2713 the event chain) | |
2714 -- a keymap (part of a command has been specified) | |
2715 -- a command (anything that satisfies `commandp'; this includes | |
2716 some symbols, lists, subrs, strings, vectors, and | |
2717 compiled-function objects) */ | |
2718 Lisp_Object | |
2719 event_binding (Lisp_Object event0, int accept_default) | |
2720 { | |
2721 /* This function can GC */ | |
2722 Lisp_Object maps[100]; | |
2723 int nmaps; | |
2724 | |
2725 assert (EVENTP (event0)); | |
2726 | |
2727 nmaps = get_relevant_keymaps (event0, countof (maps), maps); | |
2728 if (nmaps > countof (maps)) | |
2729 nmaps = countof (maps); | |
2730 return process_event_binding_result (lookup_events (event0, nmaps, maps, | |
2731 accept_default)); | |
2732 } | |
2733 | |
2734 /* like event_binding, but specify a keymap to search */ | |
2735 | |
2736 Lisp_Object | |
2737 event_binding_in (Lisp_Object event0, Lisp_Object keymap, int accept_default) | |
2738 { | |
2739 /* This function can GC */ | |
2740 if (!KEYMAPP (keymap)) | |
2741 return Qnil; | |
2742 | |
2743 return process_event_binding_result (lookup_events (event0, 1, &keymap, | |
2744 accept_default)); | |
2745 } | |
2746 | |
2747 /* Attempts to find a function key mapping corresponding to the | |
2748 event-sequence whose head is event0 (sequence is threaded through | |
2749 event_next). The return value will be the same as for event_binding(). */ | |
2750 Lisp_Object | |
2751 munging_key_map_event_binding (Lisp_Object event0, | |
2752 enum munge_me_out_the_door munge) | |
2753 { | |
2754 Lisp_Object keymap = (munge == MUNGE_ME_FUNCTION_KEY) ? | |
2755 CONSOLE_FUNCTION_KEY_MAP (event_console_or_selected (event0)) : | |
2756 Vkey_translation_map; | |
2757 | |
2758 if (NILP (keymap)) | |
2759 return Qnil; | |
2760 | |
2761 return process_event_binding_result (lookup_events (event0, 1, &keymap, 1)); | |
2762 } | |
2763 | |
2764 | |
2765 /************************************************************************/ | |
2766 /* Setting/querying the global and local maps */ | |
2767 /************************************************************************/ | |
2768 | |
2769 DEFUN ("use-global-map", Fuse_global_map, 1, 1, 0, /* | |
2770 Select KEYMAP as the global keymap. | |
2771 */ | |
2772 (keymap)) | |
2773 { | |
2774 /* This function can GC */ | |
2775 keymap = get_keymap (keymap, 1, 1); | |
2776 Vcurrent_global_map = keymap; | |
2777 return Qnil; | |
2778 } | |
2779 | |
2780 DEFUN ("use-local-map", Fuse_local_map, 1, 2, 0, /* | |
2781 Select KEYMAP as the local keymap in BUFFER. | |
2782 If KEYMAP is nil, that means no local keymap. | |
2783 If BUFFER is nil, the current buffer is assumed. | |
2784 */ | |
2785 (keymap, buffer)) | |
2786 { | |
2787 /* This function can GC */ | |
2788 struct buffer *b = decode_buffer (buffer, 0); | |
2789 if (!NILP (keymap)) | |
2790 keymap = get_keymap (keymap, 1, 1); | |
2791 | |
2792 b->keymap = keymap; | |
2793 | |
2794 return Qnil; | |
2795 } | |
2796 | |
2797 DEFUN ("current-local-map", Fcurrent_local_map, 0, 1, 0, /* | |
2798 Return BUFFER's local keymap, or nil if it has none. | |
2799 If BUFFER is nil, the current buffer is assumed. | |
2800 */ | |
2801 (buffer)) | |
2802 { | |
2803 struct buffer *b = decode_buffer (buffer, 0); | |
2804 return b->keymap; | |
2805 } | |
2806 | |
2807 DEFUN ("current-global-map", Fcurrent_global_map, 0, 0, 0, /* | |
2808 Return the current global keymap. | |
2809 */ | |
2810 ()) | |
2811 { | |
2812 return Vcurrent_global_map; | |
2813 } | |
2814 | |
2815 | |
2816 /************************************************************************/ | |
2817 /* Mapping over keymap elements */ | |
2818 /************************************************************************/ | |
2819 | |
2820 /* Since keymaps are arranged in a hierarchy, one keymap per bucky bit or | |
2821 prefix key, it's not entirely obvious what map-keymap should do, but | |
2822 what it does is: map over all keys in this map; then recursively map | |
2823 over all submaps of this map that are "bucky" submaps. This means that, | |
2824 when mapping over a keymap, it appears that "x" and "C-x" are in the | |
2825 same map, although "C-x" is really in the "control" submap of this one. | |
2826 However, since we don't recursively descend the submaps that are bound | |
2827 to prefix keys (like C-x, C-h, etc) the caller will have to recurse on | |
2828 those explicitly, if that's what they want. | |
2829 | |
2830 So the end result of this is that the bucky keymaps (the ones indexed | |
2831 under the large integers returned from MAKE_MODIFIER_HASH_KEY()) are | |
2832 invisible from elisp. They're just an implementation detail that code | |
2833 outside of this file doesn't need to know about. | |
2834 */ | |
2835 | |
2836 struct map_keymap_unsorted_closure | |
2837 { | |
934 | 2838 void (*fn) (const Lisp_Key_Data *, Lisp_Object binding, void *arg); |
428 | 2839 void *arg; |
442 | 2840 int modifiers; |
428 | 2841 }; |
2842 | |
2843 /* used by map_keymap() */ | |
2844 static int | |
2845 map_keymap_unsorted_mapper (Lisp_Object keysym, Lisp_Object value, | |
2846 void *map_keymap_unsorted_closure) | |
2847 { | |
2848 /* This function can GC */ | |
2849 struct map_keymap_unsorted_closure *closure = | |
2850 (struct map_keymap_unsorted_closure *) map_keymap_unsorted_closure; | |
442 | 2851 int modifiers = closure->modifiers; |
2852 int mod_bit; | |
428 | 2853 mod_bit = MODIFIER_HASH_KEY_BITS (keysym); |
2854 if (mod_bit != 0) | |
2855 { | |
2856 int omod = modifiers; | |
2857 closure->modifiers = (modifiers | mod_bit); | |
2858 value = get_keymap (value, 1, 0); | |
2859 elisp_maphash (map_keymap_unsorted_mapper, | |
2860 XKEYMAP (value)->table, | |
2861 map_keymap_unsorted_closure); | |
2862 closure->modifiers = omod; | |
2863 } | |
2864 else | |
2865 { | |
934 | 2866 Lisp_Key_Data key; |
428 | 2867 key.keysym = keysym; |
2868 key.modifiers = modifiers; | |
2869 ((*closure->fn) (&key, value, closure->arg)); | |
2870 } | |
2871 return 0; | |
2872 } | |
2873 | |
2874 | |
2875 struct map_keymap_sorted_closure | |
2876 { | |
2877 Lisp_Object *result_locative; | |
2878 }; | |
2879 | |
2880 /* used by map_keymap_sorted() */ | |
2881 static int | |
2882 map_keymap_sorted_mapper (Lisp_Object key, Lisp_Object value, | |
2883 void *map_keymap_sorted_closure) | |
2884 { | |
2885 struct map_keymap_sorted_closure *cl = | |
2886 (struct map_keymap_sorted_closure *) map_keymap_sorted_closure; | |
2887 Lisp_Object *list = cl->result_locative; | |
2888 *list = Fcons (Fcons (key, value), *list); | |
2889 return 0; | |
2890 } | |
2891 | |
2892 | |
2893 /* used by map_keymap_sorted(), describe_map_sort_predicate(), | |
2894 and keymap_submaps(). | |
2895 */ | |
2896 static int | |
2897 map_keymap_sort_predicate (Lisp_Object obj1, Lisp_Object obj2, | |
2286 | 2898 Lisp_Object UNUSED (pred)) |
428 | 2899 { |
2900 /* obj1 and obj2 are conses with keysyms in their cars. Cdrs are ignored. | |
2901 */ | |
442 | 2902 int bit1, bit2; |
428 | 2903 int sym1_p = 0; |
2904 int sym2_p = 0; | |
2828 | 2905 extern Lisp_Object Qcharacter_of_keysym; |
2906 | |
428 | 2907 obj1 = XCAR (obj1); |
2908 obj2 = XCAR (obj2); | |
2909 | |
2910 if (EQ (obj1, obj2)) | |
2911 return -1; | |
2912 bit1 = MODIFIER_HASH_KEY_BITS (obj1); | |
2913 bit2 = MODIFIER_HASH_KEY_BITS (obj2); | |
2914 | |
2828 | 2915 /* If either is a symbol with a Qcharacter_of_keysym property, then sort it by |
428 | 2916 that code instead of alphabetically. |
2917 */ | |
2918 if (! bit1 && SYMBOLP (obj1)) | |
2919 { | |
2828 | 2920 Lisp_Object code = Fget (obj1, Qcharacter_of_keysym, Qnil); |
428 | 2921 if (CHAR_OR_CHAR_INTP (code)) |
2922 { | |
2923 obj1 = code; | |
2924 CHECK_CHAR_COERCE_INT (obj1); | |
2925 sym1_p = 1; | |
2926 } | |
2927 } | |
2928 if (! bit2 && SYMBOLP (obj2)) | |
2929 { | |
2828 | 2930 Lisp_Object code = Fget (obj2, Qcharacter_of_keysym, Qnil); |
428 | 2931 if (CHAR_OR_CHAR_INTP (code)) |
2932 { | |
2933 obj2 = code; | |
2934 CHECK_CHAR_COERCE_INT (obj2); | |
2935 sym2_p = 1; | |
2936 } | |
2937 } | |
2938 | |
2939 /* all symbols (non-ASCIIs) come after characters (ASCIIs) */ | |
2940 if (XTYPE (obj1) != XTYPE (obj2)) | |
2941 return SYMBOLP (obj2) ? 1 : -1; | |
2942 | |
2943 if (! bit1 && CHARP (obj1)) /* they're both ASCII */ | |
2944 { | |
2945 int o1 = XCHAR (obj1); | |
2946 int o2 = XCHAR (obj2); | |
2947 if (o1 == o2 && /* If one started out as a symbol and the */ | |
2948 sym1_p != sym2_p) /* other didn't, the symbol comes last. */ | |
2949 return sym2_p ? 1 : -1; | |
2950 | |
2951 return o1 < o2 ? 1 : -1; /* else just compare them */ | |
2952 } | |
2953 | |
2954 /* else they're both symbols. If they're both buckys, then order them. */ | |
2955 if (bit1 && bit2) | |
2956 return bit1 < bit2 ? 1 : -1; | |
2957 | |
2958 /* if only one is a bucky, then it comes later */ | |
2959 if (bit1 || bit2) | |
2960 return bit2 ? 1 : -1; | |
2961 | |
2962 /* otherwise, string-sort them. */ | |
2963 { | |
867 | 2964 Ibyte *s1 = XSTRING_DATA (XSYMBOL (obj1)->name); |
2965 Ibyte *s2 = XSTRING_DATA (XSYMBOL (obj2)->name); | |
793 | 2966 return 0 > qxestrcmp (s1, s2) ? 1 : -1; |
428 | 2967 } |
2968 } | |
2969 | |
2970 | |
2971 /* used by map_keymap() */ | |
2972 static void | |
2973 map_keymap_sorted (Lisp_Object keymap_table, | |
442 | 2974 int modifiers, |
934 | 2975 void (*function) (const Lisp_Key_Data *key, |
428 | 2976 Lisp_Object binding, |
2977 void *map_keymap_sorted_closure), | |
2978 void *map_keymap_sorted_closure) | |
2979 { | |
2980 /* This function can GC */ | |
2981 struct gcpro gcpro1; | |
2982 Lisp_Object contents = Qnil; | |
2983 | |
2984 if (XINT (Fhash_table_count (keymap_table)) == 0) | |
2985 return; | |
2986 | |
2987 GCPRO1 (contents); | |
2988 | |
2989 { | |
2990 struct map_keymap_sorted_closure c1; | |
2991 c1.result_locative = &contents; | |
2992 elisp_maphash (map_keymap_sorted_mapper, keymap_table, &c1); | |
2993 } | |
2994 contents = list_sort (contents, Qnil, map_keymap_sort_predicate); | |
2995 for (; !NILP (contents); contents = XCDR (contents)) | |
2996 { | |
2997 Lisp_Object keysym = XCAR (XCAR (contents)); | |
2998 Lisp_Object binding = XCDR (XCAR (contents)); | |
442 | 2999 int sub_bits = MODIFIER_HASH_KEY_BITS (keysym); |
428 | 3000 if (sub_bits != 0) |
3001 map_keymap_sorted (XKEYMAP (get_keymap (binding, | |
3002 1, 1))->table, | |
3003 (modifiers | sub_bits), | |
3004 function, | |
3005 map_keymap_sorted_closure); | |
3006 else | |
3007 { | |
934 | 3008 Lisp_Key_Data k; |
428 | 3009 k.keysym = keysym; |
3010 k.modifiers = modifiers; | |
3011 ((*function) (&k, binding, map_keymap_sorted_closure)); | |
3012 } | |
3013 } | |
3014 UNGCPRO; | |
3015 } | |
3016 | |
3017 | |
3018 /* used by Fmap_keymap() */ | |
3019 static void | |
934 | 3020 map_keymap_mapper (const Lisp_Key_Data *key, |
428 | 3021 Lisp_Object binding, |
3022 void *function) | |
3023 { | |
3024 /* This function can GC */ | |
3025 Lisp_Object fn; | |
826 | 3026 fn = VOID_TO_LISP (function); |
428 | 3027 call2 (fn, make_key_description (key, 1), binding); |
3028 } | |
3029 | |
3030 | |
3031 static void | |
3032 map_keymap (Lisp_Object keymap_table, int sort_first, | |
934 | 3033 void (*function) (const Lisp_Key_Data *key, |
428 | 3034 Lisp_Object binding, |
3035 void *fn_arg), | |
3036 void *fn_arg) | |
3037 { | |
3038 /* This function can GC */ | |
3039 if (sort_first) | |
3040 map_keymap_sorted (keymap_table, 0, function, fn_arg); | |
3041 else | |
3042 { | |
3043 struct map_keymap_unsorted_closure map_keymap_unsorted_closure; | |
3044 map_keymap_unsorted_closure.fn = function; | |
3045 map_keymap_unsorted_closure.arg = fn_arg; | |
3046 map_keymap_unsorted_closure.modifiers = 0; | |
3047 elisp_maphash (map_keymap_unsorted_mapper, keymap_table, | |
3048 &map_keymap_unsorted_closure); | |
3049 } | |
3050 } | |
3051 | |
3052 DEFUN ("map-keymap", Fmap_keymap, 2, 3, 0, /* | |
3053 Apply FUNCTION to each element of KEYMAP. | |
3054 FUNCTION will be called with two arguments: a key-description list, and | |
3055 the binding. The order in which the elements of the keymap are passed to | |
3056 the function is unspecified. If the function inserts new elements into | |
3057 the keymap, it may or may not be called with them later. No element of | |
3058 the keymap will ever be passed to the function more than once. | |
3059 | |
3060 The function will not be called on elements of this keymap's parents | |
3061 \(see the function `keymap-parents') or upon keymaps which are contained | |
3062 within this keymap (multi-character definitions). | |
3063 It will be called on "meta" characters since they are not really | |
3064 two-character sequences. | |
3065 | |
3066 If the optional third argument SORT-FIRST is non-nil, then the elements of | |
3067 the keymap will be passed to the mapper function in a canonical order. | |
3068 Otherwise, they will be passed in hash (that is, random) order, which is | |
3069 faster. | |
3070 */ | |
3071 (function, keymap, sort_first)) | |
3072 { | |
3073 /* This function can GC */ | |
489 | 3074 struct gcpro gcpro1, gcpro2; |
428 | 3075 |
3076 /* tolerate obviously transposed args */ | |
3077 if (!NILP (Fkeymapp (function))) | |
3078 { | |
3079 Lisp_Object tmp = function; | |
3080 function = keymap; | |
3081 keymap = tmp; | |
3082 } | |
489 | 3083 GCPRO2 (function, keymap); |
428 | 3084 keymap = get_keymap (keymap, 1, 1); |
489 | 3085 map_keymap (XKEYMAP (keymap)->table, !NILP (sort_first), |
428 | 3086 map_keymap_mapper, LISP_TO_VOID (function)); |
3087 UNGCPRO; | |
3088 return Qnil; | |
3089 } | |
3090 | |
3091 | |
3092 | |
3093 /************************************************************************/ | |
3094 /* Accessible keymaps */ | |
3095 /************************************************************************/ | |
3096 | |
3097 struct accessible_keymaps_closure | |
3098 { | |
3099 Lisp_Object tail; | |
3100 }; | |
3101 | |
3102 | |
3103 static void | |
3104 accessible_keymaps_mapper_1 (Lisp_Object keysym, Lisp_Object contents, | |
442 | 3105 int modifiers, |
428 | 3106 struct accessible_keymaps_closure *closure) |
3107 { | |
3108 /* This function can GC */ | |
442 | 3109 int subbits = MODIFIER_HASH_KEY_BITS (keysym); |
428 | 3110 |
3111 if (subbits != 0) | |
3112 { | |
3113 Lisp_Object submaps; | |
3114 | |
3115 contents = get_keymap (contents, 1, 1); | |
3116 submaps = keymap_submaps (contents); | |
3117 for (; !NILP (submaps); submaps = XCDR (submaps)) | |
3118 { | |
3119 accessible_keymaps_mapper_1 (XCAR (XCAR (submaps)), | |
3120 XCDR (XCAR (submaps)), | |
3121 (subbits | modifiers), | |
3122 closure); | |
3123 } | |
3124 } | |
3125 else | |
3126 { | |
3127 Lisp_Object thisseq = Fcar (Fcar (closure->tail)); | |
3128 Lisp_Object cmd = get_keyelt (contents, 1); | |
3129 Lisp_Object vec; | |
3130 int j; | |
3131 int len; | |
934 | 3132 Lisp_Key_Data key; |
428 | 3133 key.keysym = keysym; |
3134 key.modifiers = modifiers; | |
3135 | |
3136 if (NILP (cmd)) | |
2500 | 3137 ABORT (); |
428 | 3138 cmd = get_keymap (cmd, 0, 1); |
3139 if (!KEYMAPP (cmd)) | |
2500 | 3140 ABORT (); |
428 | 3141 |
3142 vec = make_vector (XVECTOR_LENGTH (thisseq) + 1, Qnil); | |
3143 len = XVECTOR_LENGTH (thisseq); | |
3144 for (j = 0; j < len; j++) | |
3145 XVECTOR_DATA (vec) [j] = XVECTOR_DATA (thisseq) [j]; | |
3146 XVECTOR_DATA (vec) [j] = make_key_description (&key, 1); | |
3147 | |
3148 nconc2 (closure->tail, list1 (Fcons (vec, cmd))); | |
3149 } | |
3150 } | |
3151 | |
3152 | |
3153 static Lisp_Object | |
3154 accessible_keymaps_keymap_mapper (Lisp_Object thismap, void *arg) | |
3155 { | |
3156 /* This function can GC */ | |
3157 struct accessible_keymaps_closure *closure = | |
3158 (struct accessible_keymaps_closure *) arg; | |
3159 Lisp_Object submaps = keymap_submaps (thismap); | |
3160 | |
3161 for (; !NILP (submaps); submaps = XCDR (submaps)) | |
3162 { | |
3163 accessible_keymaps_mapper_1 (XCAR (XCAR (submaps)), | |
3164 XCDR (XCAR (submaps)), | |
3165 0, | |
3166 closure); | |
3167 } | |
3168 return Qnil; | |
3169 } | |
3170 | |
3171 | |
3172 DEFUN ("accessible-keymaps", Faccessible_keymaps, 1, 2, 0, /* | |
3173 Find all keymaps accessible via prefix characters from KEYMAP. | |
3174 Returns a list of elements of the form (KEYS . MAP), where the sequence | |
3175 KEYS starting from KEYMAP gets you to MAP. These elements are ordered | |
3176 so that the KEYS increase in length. The first element is ([] . KEYMAP). | |
3177 An optional argument PREFIX, if non-nil, should be a key sequence; | |
3178 then the value includes only maps for prefixes that start with PREFIX. | |
3179 */ | |
3180 (keymap, prefix)) | |
3181 { | |
3182 /* This function can GC */ | |
3183 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
3184 Lisp_Object accessible_keymaps = Qnil; | |
3185 struct accessible_keymaps_closure c; | |
3186 c.tail = Qnil; | |
3187 GCPRO4 (accessible_keymaps, c.tail, prefix, keymap); | |
3188 | |
440 | 3189 keymap = get_keymap (keymap, 1, 1); |
3190 | |
428 | 3191 retry: |
3192 if (NILP (prefix)) | |
3193 { | |
440 | 3194 prefix = make_vector (0, Qnil); |
428 | 3195 } |
440 | 3196 else if (VECTORP (prefix) || STRINGP (prefix)) |
428 | 3197 { |
3198 int len = XINT (Flength (prefix)); | |
440 | 3199 Lisp_Object def; |
428 | 3200 Lisp_Object p; |
3201 int iii; | |
3202 struct gcpro ngcpro1; | |
3203 | |
440 | 3204 if (len == 0) |
3205 { | |
3206 prefix = Qnil; | |
3207 goto retry; | |
3208 } | |
3209 | |
3210 def = Flookup_key (keymap, prefix, Qnil); | |
428 | 3211 def = get_keymap (def, 0, 1); |
3212 if (!KEYMAPP (def)) | |
3213 goto RETURN; | |
3214 | |
3215 keymap = def; | |
3216 p = make_vector (len, Qnil); | |
3217 NGCPRO1 (p); | |
3218 for (iii = 0; iii < len; iii++) | |
3219 { | |
934 | 3220 Lisp_Key_Data key; |
428 | 3221 define_key_parser (Faref (prefix, make_int (iii)), &key); |
3222 XVECTOR_DATA (p)[iii] = make_key_description (&key, 1); | |
3223 } | |
3224 NUNGCPRO; | |
3225 prefix = p; | |
3226 } | |
440 | 3227 else |
3228 { | |
3229 prefix = wrong_type_argument (Qarrayp, prefix); | |
3230 goto retry; | |
3231 } | |
428 | 3232 |
3233 accessible_keymaps = list1 (Fcons (prefix, keymap)); | |
3234 | |
440 | 3235 /* For each map in the list maps, look at any other maps it points |
3236 to and stick them at the end if they are not already in the list */ | |
428 | 3237 |
3238 for (c.tail = accessible_keymaps; | |
3239 !NILP (c.tail); | |
3240 c.tail = XCDR (c.tail)) | |
3241 { | |
3242 Lisp_Object thismap = Fcdr (Fcar (c.tail)); | |
3243 CHECK_KEYMAP (thismap); | |
3244 traverse_keymaps (thismap, Qnil, | |
3245 accessible_keymaps_keymap_mapper, &c); | |
3246 } | |
3247 RETURN: | |
3248 UNGCPRO; | |
3249 return accessible_keymaps; | |
3250 } | |
3251 | |
3252 | |
3253 | |
3254 /************************************************************************/ | |
3255 /* Pretty descriptions of key sequences */ | |
3256 /************************************************************************/ | |
3257 | |
3258 DEFUN ("key-description", Fkey_description, 1, 1, 0, /* | |
3259 Return a pretty description of key-sequence KEYS. | |
3260 Control characters turn into "C-foo" sequences, meta into "M-foo", | |
3261 spaces are put between sequence elements, etc... | |
3262 */ | |
3263 (keys)) | |
3264 { | |
3265 if (CHAR_OR_CHAR_INTP (keys) || CONSP (keys) || SYMBOLP (keys) | |
3266 || EVENTP (keys)) | |
3267 { | |
3268 return Fsingle_key_description (keys); | |
3269 } | |
3270 else if (VECTORP (keys) || | |
3271 STRINGP (keys)) | |
3272 { | |
3273 Lisp_Object string = Qnil; | |
3274 /* Lisp_Object sep = Qnil; */ | |
3275 int size = XINT (Flength (keys)); | |
3276 int i; | |
3277 | |
3278 for (i = 0; i < size; i++) | |
3279 { | |
3280 Lisp_Object s2 = Fsingle_key_description | |
3281 (STRINGP (keys) | |
867 | 3282 ? make_char (string_ichar (keys, i)) |
428 | 3283 : XVECTOR_DATA (keys)[i]); |
3284 | |
3285 if (i == 0) | |
3286 string = s2; | |
3287 else | |
3288 { | |
3289 /* if (NILP (sep)) Lisp_Object sep = build_string (" ") */; | |
3290 string = concat2 (string, concat2 (Vsingle_space_string, s2)); | |
3291 } | |
3292 } | |
3293 return string; | |
3294 } | |
3295 return Fkey_description (wrong_type_argument (Qsequencep, keys)); | |
3296 } | |
3297 | |
3298 DEFUN ("single-key-description", Fsingle_key_description, 1, 1, 0, /* | |
3299 Return a pretty description of command character KEY. | |
3300 Control characters turn into C-whatever, etc. | |
3301 This differs from `text-char-description' in that it returns a description | |
3302 of a key read from the user rather than a character from a buffer. | |
3303 */ | |
3304 (key)) | |
3305 { | |
3306 if (SYMBOLP (key)) | |
3307 key = Fcons (key, Qnil); /* sleaze sleaze */ | |
3308 | |
3309 if (EVENTP (key) || CHAR_OR_CHAR_INTP (key)) | |
3310 { | |
793 | 3311 DECLARE_EISTRING_MALLOC (buf); |
3312 Lisp_Object str; | |
3313 | |
428 | 3314 if (!EVENTP (key)) |
3315 { | |
934 | 3316 Lisp_Object event = Fmake_event (Qnil, Qnil); |
3317 CHECK_CHAR_COERCE_INT (key); | |
1204 | 3318 character_to_event (XCHAR (key), XEVENT (event), |
934 | 3319 XCONSOLE (Vselected_console), 0, 1); |
3320 format_event_object (buf, event, 1); | |
1204 | 3321 Fdeallocate_event (event); |
934 | 3322 } |
3323 else | |
3324 format_event_object (buf, key, 1); | |
793 | 3325 str = eimake_string (buf); |
3326 eifree (buf); | |
3327 return str; | |
428 | 3328 } |
3329 | |
3330 if (CONSP (key)) | |
3331 { | |
793 | 3332 DECLARE_EISTRING (bufp); |
3333 | |
428 | 3334 Lisp_Object rest; |
3335 LIST_LOOP (rest, key) | |
3336 { | |
3337 Lisp_Object keysym = XCAR (rest); | |
2421 | 3338 if (EQ (keysym, Qcontrol)) eicat_ascii (bufp, "C-"); |
3339 else if (EQ (keysym, Qctrl)) eicat_ascii (bufp, "C-"); | |
3340 else if (EQ (keysym, Qmeta)) eicat_ascii (bufp, "M-"); | |
3341 else if (EQ (keysym, Qsuper)) eicat_ascii (bufp, "S-"); | |
3342 else if (EQ (keysym, Qhyper)) eicat_ascii (bufp, "H-"); | |
3343 else if (EQ (keysym, Qalt)) eicat_ascii (bufp, "A-"); | |
3344 else if (EQ (keysym, Qshift)) eicat_ascii (bufp, "Sh-"); | |
428 | 3345 else if (CHAR_OR_CHAR_INTP (keysym)) |
793 | 3346 eicat_ch (bufp, XCHAR_OR_CHAR_INT (keysym)); |
428 | 3347 else |
3348 { | |
3349 CHECK_SYMBOL (keysym); | |
3350 #if 0 /* This is bogus */ | |
2421 | 3351 if (EQ (keysym, QKlinefeed)) eicat_ascii (bufp, "LFD"); |
3352 else if (EQ (keysym, QKtab)) eicat_ascii (bufp, "TAB"); | |
3353 else if (EQ (keysym, QKreturn)) eicat_ascii (bufp, "RET"); | |
3354 else if (EQ (keysym, QKescape)) eicat_ascii (bufp, "ESC"); | |
3355 else if (EQ (keysym, QKdelete)) eicat_ascii (bufp, "DEL"); | |
3356 else if (EQ (keysym, QKspace)) eicat_ascii (bufp, "SPC"); | |
3357 else if (EQ (keysym, QKbackspace)) eicat_ascii (bufp, "BS"); | |
428 | 3358 else |
3359 #endif | |
793 | 3360 eicat_lstr (bufp, XSYMBOL (keysym)->name); |
428 | 3361 if (!NILP (XCDR (rest))) |
793 | 3362 invalid_argument ("Invalid key description", key); |
428 | 3363 } |
3364 } | |
793 | 3365 return eimake_string (bufp); |
428 | 3366 } |
3367 return Fsingle_key_description | |
3368 (wrong_type_argument (intern ("char-or-event-p"), key)); | |
3369 } | |
3370 | |
3371 DEFUN ("text-char-description", Ftext_char_description, 1, 1, 0, /* | |
3372 Return a pretty description of file-character CHR. | |
3373 Unprintable characters turn into "^char" or \\NNN, depending on the value | |
3374 of the `ctl-arrow' variable. | |
3375 This differs from `single-key-description' in that it returns a description | |
3376 of a character from a buffer rather than a key read from the user. | |
3377 */ | |
3378 (chr)) | |
3379 { | |
867 | 3380 Ibyte buf[200]; |
3381 Ibyte *p; | |
3382 Ichar c; | |
428 | 3383 Lisp_Object ctl_arrow = current_buffer->ctl_arrow; |
3384 int ctl_p = !NILP (ctl_arrow); | |
867 | 3385 Ichar printable_min = (CHAR_OR_CHAR_INTP (ctl_arrow) |
428 | 3386 ? XCHAR_OR_CHAR_INT (ctl_arrow) |
3387 : ((EQ (ctl_arrow, Qt) || NILP (ctl_arrow)) | |
3388 ? 256 : 160)); | |
3389 | |
3390 if (EVENTP (chr)) | |
3391 { | |
2862 | 3392 Lisp_Object ch = Fevent_to_character (chr, Qnil, Qnil, Qnil); |
428 | 3393 if (NILP (ch)) |
3394 return | |
563 | 3395 signal_continuable_error |
3396 (Qinvalid_argument, | |
2828 | 3397 "key has no character equivalent (that we know of)", |
3398 Fcopy_event (chr, Qnil)); | |
428 | 3399 chr = ch; |
3400 } | |
3401 | |
3402 CHECK_CHAR_COERCE_INT (chr); | |
3403 | |
3404 c = XCHAR (chr); | |
3405 p = buf; | |
3406 | |
3407 if (c >= printable_min) | |
3408 { | |
867 | 3409 p += set_itext_ichar (p, c); |
428 | 3410 } |
3411 else if (c < 040 && ctl_p) | |
3412 { | |
3413 *p++ = '^'; | |
3414 *p++ = c + 64; /* 'A' - 1 */ | |
3415 } | |
3416 else if (c == 0177) | |
3417 { | |
3418 *p++ = '^'; | |
3419 *p++ = '?'; | |
3420 } | |
3421 else if (c >= 0200 || c < 040) | |
3422 { | |
3423 *p++ = '\\'; | |
3424 #ifdef MULE | |
3425 /* !!#### This syntax is not readable. It will | |
3426 be interpreted as a 3-digit octal number rather | |
3427 than a 7-digit octal number. */ | |
3428 if (c >= 0400) | |
3429 { | |
3430 *p++ = '0' + ((c & 07000000) >> 18); | |
3431 *p++ = '0' + ((c & 0700000) >> 15); | |
3432 *p++ = '0' + ((c & 070000) >> 12); | |
3433 *p++ = '0' + ((c & 07000) >> 9); | |
3434 } | |
3435 #endif | |
3436 *p++ = '0' + ((c & 0700) >> 6); | |
3437 *p++ = '0' + ((c & 0070) >> 3); | |
3438 *p++ = '0' + ((c & 0007)); | |
3439 } | |
3440 else | |
3441 { | |
867 | 3442 p += set_itext_ichar (p, c); |
428 | 3443 } |
3444 | |
3445 *p = 0; | |
3446 return build_string ((char *) buf); | |
3447 } | |
3448 | |
3449 | |
3450 /************************************************************************/ | |
3451 /* where-is (mapping bindings to keys) */ | |
3452 /************************************************************************/ | |
3453 | |
3454 static Lisp_Object | |
3455 where_is_internal (Lisp_Object definition, Lisp_Object *maps, int nmaps, | |
793 | 3456 Lisp_Object firstonly, Eistring *target_buffer); |
428 | 3457 |
3458 DEFUN ("where-is-internal", Fwhere_is_internal, 1, 5, 0, /* | |
3459 Return list of keys that invoke DEFINITION in KEYMAPS. | |
3460 KEYMAPS can be either a keymap (meaning search in that keymap and the | |
3461 current global keymap) or a list of keymaps (meaning search in exactly | |
3462 those keymaps and no others). If KEYMAPS is nil, search in the currently | |
3463 applicable maps for EVENT-OR-KEYS (this is equivalent to specifying | |
3464 `(current-keymaps EVENT-OR-KEYS)' as the argument to KEYMAPS). | |
3465 | |
3466 If optional 3rd arg FIRSTONLY is non-nil, return a vector representing | |
3467 the first key sequence found, rather than a list of all possible key | |
3468 sequences. | |
3469 | |
3470 If optional 4th arg NOINDIRECT is non-nil, don't follow indirections | |
3471 to other keymaps or slots. This makes it possible to search for an | |
3472 indirect definition itself. | |
3473 */ | |
2286 | 3474 (definition, keymaps, firstonly, UNUSED (noindirect), event_or_keys)) |
428 | 3475 { |
3476 /* This function can GC */ | |
3477 Lisp_Object maps[100]; | |
3478 Lisp_Object *gubbish = maps; | |
3479 int nmaps; | |
3480 | |
3481 /* Get keymaps as an array */ | |
3482 if (NILP (keymaps)) | |
3483 { | |
3484 nmaps = get_relevant_keymaps (event_or_keys, countof (maps), | |
3485 gubbish); | |
3486 if (nmaps > countof (maps)) | |
3487 { | |
3488 gubbish = alloca_array (Lisp_Object, nmaps); | |
3489 nmaps = get_relevant_keymaps (event_or_keys, nmaps, gubbish); | |
3490 } | |
3491 } | |
3492 else if (CONSP (keymaps)) | |
3493 { | |
3494 Lisp_Object rest; | |
3495 int i; | |
3496 | |
3497 nmaps = XINT (Flength (keymaps)); | |
3498 if (nmaps > countof (maps)) | |
3499 { | |
3500 gubbish = alloca_array (Lisp_Object, nmaps); | |
3501 } | |
3502 for (rest = keymaps, i = 0; !NILP (rest); | |
3503 rest = XCDR (keymaps), i++) | |
3504 { | |
3505 gubbish[i] = get_keymap (XCAR (keymaps), 1, 1); | |
3506 } | |
3507 } | |
3508 else | |
3509 { | |
3510 nmaps = 1; | |
3511 gubbish[0] = get_keymap (keymaps, 1, 1); | |
3512 if (!EQ (gubbish[0], Vcurrent_global_map)) | |
3513 { | |
3514 gubbish[1] = Vcurrent_global_map; | |
3515 nmaps++; | |
3516 } | |
3517 } | |
3518 | |
3519 return where_is_internal (definition, gubbish, nmaps, firstonly, 0); | |
3520 } | |
3521 | |
3522 /* This function is like | |
3523 (key-description (where-is-internal definition nil t)) | |
3524 except that it writes its output into a (char *) buffer that you | |
3525 provide; it doesn't cons (or allocate memory) at all, so it's | |
3526 very fast. This is used by menubar.c. | |
3527 */ | |
3528 void | |
793 | 3529 where_is_to_char (Lisp_Object definition, Eistring *buffer) |
428 | 3530 { |
3531 /* This function can GC */ | |
3532 Lisp_Object maps[100]; | |
3533 Lisp_Object *gubbish = maps; | |
3534 int nmaps; | |
3535 | |
3536 /* Get keymaps as an array */ | |
3537 nmaps = get_relevant_keymaps (Qnil, countof (maps), gubbish); | |
3538 if (nmaps > countof (maps)) | |
3539 { | |
3540 gubbish = alloca_array (Lisp_Object, nmaps); | |
3541 nmaps = get_relevant_keymaps (Qnil, nmaps, gubbish); | |
3542 } | |
3543 | |
3544 where_is_internal (definition, maps, nmaps, Qt, buffer); | |
3545 } | |
3546 | |
3547 | |
3548 static Lisp_Object | |
934 | 3549 raw_keys_to_keys (Lisp_Key_Data *keys, int count) |
428 | 3550 { |
3551 Lisp_Object result = make_vector (count, Qnil); | |
3552 while (count--) | |
3553 XVECTOR_DATA (result) [count] = make_key_description (&(keys[count]), 1); | |
3554 return result; | |
3555 } | |
3556 | |
3557 | |
3558 static void | |
934 | 3559 format_raw_keys (Lisp_Key_Data *keys, int count, Eistring *buf) |
428 | 3560 { |
3561 int i; | |
934 | 3562 Lisp_Object event = Fmake_event (Qnil, Qnil); |
3563 XSET_EVENT_TYPE (event, key_press_event); | |
3564 XSET_EVENT_CHANNEL (event, Vselected_console); | |
428 | 3565 for (i = 0; i < count; i++) |
3566 { | |
1204 | 3567 XSET_EVENT_KEY_KEYSYM (event, keys[i].keysym); |
3568 XSET_EVENT_KEY_MODIFIERS (event, KEY_DATA_MODIFIERS (&keys[i])); | |
934 | 3569 format_event_object (buf, event, 1); |
793 | 3570 if (i < count - 1) |
2421 | 3571 eicat_ascii (buf, " "); |
428 | 3572 } |
1204 | 3573 Fdeallocate_event (event); |
428 | 3574 } |
3575 | |
3576 | |
3577 /* definition is the thing to look for. | |
3578 map is a keymap. | |
3579 shadow is an array of shadow_count keymaps; if there is a different | |
3580 binding in any of the keymaps of a key that we are considering | |
3581 returning, then we reconsider. | |
3582 firstonly means give up after finding the first match; | |
3583 keys_so_far and modifiers_so_far describe which map we're looking in; | |
3584 If we're in the "meta" submap of the map that "C-x 4" is bound to, | |
3585 then keys_so_far will be {(control x), \4}, and modifiers_so_far | |
442 | 3586 will be XEMACS_MOD_META. That is, keys_so_far is the chain of keys that we |
428 | 3587 have followed, and modifiers_so_far_so_far is the bits (partial keys) |
3588 beyond that. | |
3589 | |
3590 (keys_so_far is a global buffer and the keys_count arg says how much | |
3591 of it we're currently interested in.) | |
3592 | |
3593 If target_buffer is provided, then we write a key-description into it, | |
3594 to avoid consing a string. This only works with firstonly on. | |
3595 */ | |
3596 | |
3597 struct where_is_closure | |
3598 { | |
3599 Lisp_Object definition; | |
3600 Lisp_Object *shadow; | |
3601 int shadow_count; | |
3602 int firstonly; | |
3603 int keys_count; | |
442 | 3604 int modifiers_so_far; |
793 | 3605 Eistring *target_buffer; |
934 | 3606 Lisp_Key_Data *keys_so_far; |
428 | 3607 int keys_so_far_total_size; |
3608 int keys_so_far_malloced; | |
3609 }; | |
3610 | |
3611 static Lisp_Object where_is_recursive_mapper (Lisp_Object map, void *arg); | |
3612 | |
3613 static Lisp_Object | |
3614 where_is_recursive_mapper (Lisp_Object map, void *arg) | |
3615 { | |
3616 /* This function can GC */ | |
3617 struct where_is_closure *c = (struct where_is_closure *) arg; | |
3618 Lisp_Object definition = c->definition; | |
442 | 3619 const int firstonly = c->firstonly; |
3620 const int keys_count = c->keys_count; | |
3621 const int modifiers_so_far = c->modifiers_so_far; | |
793 | 3622 Eistring *target_buffer = c->target_buffer; |
428 | 3623 Lisp_Object keys = Fgethash (definition, |
3624 XKEYMAP (map)->inverse_table, | |
3625 Qnil); | |
3626 Lisp_Object submaps; | |
3627 Lisp_Object result = Qnil; | |
3628 | |
3629 if (!NILP (keys)) | |
3630 { | |
3631 /* One or more keys in this map match the definition we're looking for. | |
3632 Verify that these bindings aren't shadowed by other bindings | |
3633 in the shadow maps. Either nil or number as value from | |
3634 raw_lookup_key() means undefined. */ | |
934 | 3635 Lisp_Key_Data *so_far = c->keys_so_far; |
428 | 3636 |
3637 for (;;) /* loop over all keys that match */ | |
3638 { | |
3639 Lisp_Object k = CONSP (keys) ? XCAR (keys) : keys; | |
3640 int i; | |
3641 | |
3642 so_far [keys_count].keysym = k; | |
934 | 3643 SET_KEY_DATA_MODIFIERS (&so_far [keys_count], modifiers_so_far); |
428 | 3644 |
3645 /* now loop over all shadow maps */ | |
3646 for (i = 0; i < c->shadow_count; i++) | |
3647 { | |
3648 Lisp_Object shadowed = raw_lookup_key (c->shadow[i], | |
3649 so_far, | |
3650 keys_count + 1, | |
3651 0, 1); | |
3652 | |
3653 if (NILP (shadowed) || CHARP (shadowed) || | |
3654 EQ (shadowed, definition)) | |
3655 continue; /* we passed this test; it's not shadowed here. */ | |
3656 else | |
3657 /* ignore this key binding, since it actually has a | |
3658 different binding in a shadowing map */ | |
3659 goto c_doesnt_have_proper_loop_exit_statements; | |
3660 } | |
3661 | |
3662 /* OK, the key is for real */ | |
3663 if (target_buffer) | |
3664 { | |
2500 | 3665 if (!firstonly) ABORT (); |
428 | 3666 format_raw_keys (so_far, keys_count + 1, target_buffer); |
3667 return make_int (1); | |
3668 } | |
3669 else if (firstonly) | |
3670 return raw_keys_to_keys (so_far, keys_count + 1); | |
3671 else | |
3672 result = Fcons (raw_keys_to_keys (so_far, keys_count + 1), | |
3673 result); | |
3674 | |
3675 c_doesnt_have_proper_loop_exit_statements: | |
3676 /* now on to the next matching key ... */ | |
3677 if (!CONSP (keys)) break; | |
3678 keys = XCDR (keys); | |
3679 } | |
3680 } | |
3681 | |
3682 /* Now search the sub-keymaps of this map. | |
3683 If we're in "firstonly" mode and have already found one, this | |
3684 point is not reached. If we get one from lower down, either | |
3685 return it immediately (in firstonly mode) or tack it onto the | |
3686 end of the ones we've gotten so far. | |
3687 */ | |
3688 for (submaps = keymap_submaps (map); | |
3689 !NILP (submaps); | |
3690 submaps = XCDR (submaps)) | |
3691 { | |
3692 Lisp_Object key = XCAR (XCAR (submaps)); | |
3693 Lisp_Object submap = XCDR (XCAR (submaps)); | |
442 | 3694 int lower_modifiers; |
428 | 3695 int lower_keys_count = keys_count; |
442 | 3696 int bucky; |
428 | 3697 |
3698 submap = get_keymap (submap, 0, 0); | |
3699 | |
3700 if (EQ (submap, map)) | |
3701 /* Arrgh! Some loser has introduced a loop... */ | |
3702 continue; | |
3703 | |
3704 /* If this is not a keymap, then that's probably because someone | |
3705 did an `fset' of a symbol that used to point to a map such that | |
3706 it no longer does. Sigh. Ignore this, and invalidate the cache | |
3707 so that it doesn't happen to us next time too. | |
3708 */ | |
3709 if (NILP (submap)) | |
3710 { | |
3711 XKEYMAP (map)->sub_maps_cache = Qt; | |
3712 continue; | |
3713 } | |
3714 | |
3715 /* If the map is a "bucky" map, then add a bit to the | |
3716 modifiers_so_far list. | |
3717 Otherwise, add a new raw_key onto the end of keys_so_far. | |
3718 */ | |
3719 bucky = MODIFIER_HASH_KEY_BITS (key); | |
3720 if (bucky != 0) | |
3721 lower_modifiers = (modifiers_so_far | bucky); | |
3722 else | |
3723 { | |
934 | 3724 Lisp_Key_Data *so_far = c->keys_so_far; |
428 | 3725 lower_modifiers = 0; |
3726 so_far [lower_keys_count].keysym = key; | |
934 | 3727 SET_KEY_DATA_MODIFIERS (&so_far [lower_keys_count], modifiers_so_far); |
428 | 3728 lower_keys_count++; |
3729 } | |
3730 | |
3731 if (lower_keys_count >= c->keys_so_far_total_size) | |
3732 { | |
3733 int size = lower_keys_count + 50; | |
3734 if (! c->keys_so_far_malloced) | |
3735 { | |
3025 | 3736 Lisp_Key_Data *new_ = xnew_array (Lisp_Key_Data, size); |
3737 memcpy ((void *)new_, (const void *)c->keys_so_far, | |
934 | 3738 c->keys_so_far_total_size * sizeof (Lisp_Key_Data)); |
428 | 3739 } |
3740 else | |
934 | 3741 XREALLOC_ARRAY (c->keys_so_far, Lisp_Key_Data, size); |
428 | 3742 |
3743 c->keys_so_far_total_size = size; | |
3744 c->keys_so_far_malloced = 1; | |
3745 } | |
3746 | |
3747 { | |
3748 Lisp_Object lower; | |
3749 | |
3750 c->keys_count = lower_keys_count; | |
3751 c->modifiers_so_far = lower_modifiers; | |
3752 | |
3753 lower = traverse_keymaps (submap, Qnil, where_is_recursive_mapper, c); | |
3754 | |
3755 c->keys_count = keys_count; | |
3756 c->modifiers_so_far = modifiers_so_far; | |
3757 | |
3758 if (!firstonly) | |
3759 result = nconc2 (lower, result); | |
3760 else if (!NILP (lower)) | |
3761 return lower; | |
3762 } | |
3763 } | |
3764 return result; | |
3765 } | |
3766 | |
3767 | |
3768 static Lisp_Object | |
3769 where_is_internal (Lisp_Object definition, Lisp_Object *maps, int nmaps, | |
793 | 3770 Lisp_Object firstonly, Eistring *target_buffer) |
428 | 3771 { |
3772 /* This function can GC */ | |
3773 Lisp_Object result = Qnil; | |
3774 int i; | |
934 | 3775 Lisp_Key_Data raw[20]; |
428 | 3776 struct where_is_closure c; |
3777 | |
3778 c.definition = definition; | |
3779 c.shadow = maps; | |
3780 c.firstonly = !NILP (firstonly); | |
3781 c.target_buffer = target_buffer; | |
3782 c.keys_so_far = raw; | |
3783 c.keys_so_far_total_size = countof (raw); | |
3784 c.keys_so_far_malloced = 0; | |
3785 | |
3786 /* Loop over each of the maps, accumulating the keys found. | |
3787 For each map searched, all previous maps shadow this one | |
3788 so that bogus keys aren't listed. */ | |
3789 for (i = 0; i < nmaps; i++) | |
3790 { | |
3791 Lisp_Object this_result; | |
3792 c.shadow_count = i; | |
3793 /* Reset the things set in each iteration */ | |
3794 c.keys_count = 0; | |
3795 c.modifiers_so_far = 0; | |
3796 | |
3797 this_result = traverse_keymaps (maps[i], Qnil, where_is_recursive_mapper, | |
3798 &c); | |
3799 if (!NILP (firstonly)) | |
3800 { | |
3801 result = this_result; | |
3802 if (!NILP (result)) | |
3803 break; | |
3804 } | |
3805 else | |
3806 result = nconc2 (this_result, result); | |
3807 } | |
3808 | |
3809 if (NILP (firstonly)) | |
3810 result = Fnreverse (result); | |
3811 | |
3812 if (c.keys_so_far_malloced) | |
1726 | 3813 xfree (c.keys_so_far, Lisp_Key_Data *); |
428 | 3814 return result; |
3815 } | |
3816 | |
3817 | |
3818 /************************************************************************/ | |
3819 /* Describing keymaps */ | |
3820 /************************************************************************/ | |
3821 | |
3822 DEFUN ("describe-bindings-internal", Fdescribe_bindings_internal, 1, 5, 0, /* | |
3823 Insert a list of all defined keys and their definitions in MAP. | |
3824 Optional second argument ALL says whether to include even "uninteresting" | |
3825 definitions (ie symbols with a non-nil `suppress-keymap' property. | |
3826 Third argument SHADOW is a list of keymaps whose bindings shadow those | |
3827 of map; if a binding is present in any shadowing map, it is not printed. | |
3828 Fourth argument PREFIX, if non-nil, should be a key sequence; | |
3829 only bindings which start with that key sequence will be printed. | |
3830 Fifth argument MOUSE-ONLY-P says to only print bindings for mouse clicks. | |
3831 */ | |
3832 (map, all, shadow, prefix, mouse_only_p)) | |
3833 { | |
3834 /* This function can GC */ | |
3835 | |
3836 /* #### At some point, this function should be changed to accept a | |
3837 BUFFER argument. Currently, the BUFFER argument to | |
3838 describe_map_tree is being used only internally. */ | |
3839 describe_map_tree (map, NILP (all), shadow, prefix, | |
3840 !NILP (mouse_only_p), Fcurrent_buffer ()); | |
3841 return Qnil; | |
3842 } | |
3843 | |
3844 | |
3845 /* Insert a description of the key bindings in STARTMAP, | |
3846 followed by those of all maps reachable through STARTMAP. | |
3847 If PARTIAL is nonzero, omit certain "uninteresting" commands | |
3848 (such as `undefined'). | |
3849 If SHADOW is non-nil, it is a list of other maps; | |
3850 don't mention keys which would be shadowed by any of them | |
3851 If PREFIX is non-nil, only list bindings which start with those keys. | |
3852 */ | |
3853 | |
3854 void | |
3855 describe_map_tree (Lisp_Object startmap, int partial, Lisp_Object shadow, | |
3856 Lisp_Object prefix, int mice_only_p, Lisp_Object buffer) | |
3857 { | |
3858 /* This function can GC */ | |
3859 Lisp_Object maps = Qnil; | |
3860 struct gcpro gcpro1, gcpro2; /* get_keymap may autoload */ | |
3861 GCPRO2 (maps, shadow); | |
3862 | |
3863 maps = Faccessible_keymaps (startmap, prefix); | |
3864 | |
3865 for (; !NILP (maps); maps = Fcdr (maps)) | |
3866 { | |
3867 Lisp_Object sub_shadow = Qnil; | |
3868 Lisp_Object elt = Fcar (maps); | |
3869 Lisp_Object tail; | |
3870 int no_prefix = (VECTORP (Fcar (elt)) | |
3871 && XINT (Flength (Fcar (elt))) == 0); | |
3872 struct gcpro ngcpro1, ngcpro2, ngcpro3; | |
3873 NGCPRO3 (sub_shadow, elt, tail); | |
3874 | |
3875 for (tail = shadow; CONSP (tail); tail = XCDR (tail)) | |
3876 { | |
3877 Lisp_Object shmap = XCAR (tail); | |
3878 | |
3879 /* If the sequence by which we reach this keymap is zero-length, | |
3880 then the shadow maps for this keymap are just SHADOW. */ | |
3881 if (no_prefix) | |
3882 ; | |
3883 /* If the sequence by which we reach this keymap actually has | |
3884 some elements, then the sequence's definition in SHADOW is | |
3885 what we should use. */ | |
3886 else | |
3887 { | |
3888 shmap = Flookup_key (shmap, Fcar (elt), Qt); | |
3889 if (CHARP (shmap)) | |
3890 shmap = Qnil; | |
3891 } | |
3892 | |
3893 if (!NILP (shmap)) | |
3894 { | |
3895 Lisp_Object shm = get_keymap (shmap, 0, 1); | |
3896 /* If shmap is not nil and not a keymap, it completely | |
3897 shadows this map, so don't describe this map at all. */ | |
3898 if (!KEYMAPP (shm)) | |
3899 goto SKIP; | |
3900 sub_shadow = Fcons (shm, sub_shadow); | |
3901 } | |
3902 } | |
3903 | |
3904 { | |
3905 /* Describe the contents of map MAP, assuming that this map | |
3906 itself is reached by the sequence of prefix keys KEYS (a vector). | |
3907 PARTIAL and SHADOW are as in `describe_map_tree'. */ | |
3908 Lisp_Object keysdesc | |
3909 = ((!no_prefix) | |
3910 ? concat2 (Fkey_description (Fcar (elt)), Vsingle_space_string) | |
3911 : Qnil); | |
3912 describe_map (Fcdr (elt), keysdesc, | |
3913 describe_command, | |
3914 partial, | |
3915 sub_shadow, | |
3916 mice_only_p, | |
3917 buffer); | |
3918 } | |
3919 SKIP: | |
3920 NUNGCPRO; | |
3921 } | |
3922 UNGCPRO; | |
3923 } | |
3924 | |
3925 | |
3926 static void | |
3927 describe_command (Lisp_Object definition, Lisp_Object buffer) | |
3928 { | |
3929 /* This function can GC */ | |
3930 int keymapp = !NILP (Fkeymapp (definition)); | |
3931 struct gcpro gcpro1; | |
3932 GCPRO1 (definition); | |
3933 | |
3934 Findent_to (make_int (16), make_int (3), buffer); | |
3935 if (keymapp) | |
3936 buffer_insert_c_string (XBUFFER (buffer), "<< "); | |
3937 | |
3938 if (SYMBOLP (definition)) | |
3939 { | |
3940 buffer_insert1 (XBUFFER (buffer), Fsymbol_name (definition)); | |
3941 } | |
3942 else if (STRINGP (definition) || VECTORP (definition)) | |
3943 { | |
3944 buffer_insert_c_string (XBUFFER (buffer), "Kbd Macro: "); | |
3945 buffer_insert1 (XBUFFER (buffer), Fkey_description (definition)); | |
3946 } | |
3947 else if (COMPILED_FUNCTIONP (definition)) | |
3948 buffer_insert_c_string (XBUFFER (buffer), "Anonymous Compiled Function"); | |
3949 else if (CONSP (definition) && EQ (XCAR (definition), Qlambda)) | |
3950 buffer_insert_c_string (XBUFFER (buffer), "Anonymous Lambda"); | |
3951 else if (KEYMAPP (definition)) | |
3952 { | |
3953 Lisp_Object name = XKEYMAP (definition)->name; | |
3954 if (STRINGP (name) || (SYMBOLP (name) && !NILP (name))) | |
3955 { | |
3956 buffer_insert_c_string (XBUFFER (buffer), "Prefix command "); | |
3957 if (SYMBOLP (name) | |
3958 && EQ (find_symbol_value (name), definition)) | |
3959 buffer_insert1 (XBUFFER (buffer), Fsymbol_name (name)); | |
3960 else | |
3961 { | |
3962 buffer_insert1 (XBUFFER (buffer), Fprin1_to_string (name, Qnil)); | |
3963 } | |
3964 } | |
3965 else | |
3966 buffer_insert_c_string (XBUFFER (buffer), "Prefix Command"); | |
3967 } | |
3968 else | |
3969 buffer_insert_c_string (XBUFFER (buffer), "??"); | |
3970 | |
3971 if (keymapp) | |
3972 buffer_insert_c_string (XBUFFER (buffer), " >>"); | |
3973 buffer_insert_c_string (XBUFFER (buffer), "\n"); | |
3974 UNGCPRO; | |
3975 } | |
3976 | |
3977 struct describe_map_closure | |
3978 { | |
3979 Lisp_Object *list; /* pointer to the list to update */ | |
3980 Lisp_Object partial; /* whether to ignore suppressed commands */ | |
3981 Lisp_Object shadow; /* list of maps shadowing this one */ | |
3982 Lisp_Object self; /* this map */ | |
3983 Lisp_Object self_root; /* this map, or some map that has this map as | |
3984 a parent. this is the base of the tree */ | |
3985 int mice_only_p; /* whether we are to display only button bindings */ | |
3986 }; | |
3987 | |
3988 struct describe_map_shadow_closure | |
3989 { | |
934 | 3990 const Lisp_Key_Data *raw_key; |
428 | 3991 Lisp_Object self; |
3992 }; | |
3993 | |
3994 static Lisp_Object | |
3995 describe_map_mapper_shadow_search (Lisp_Object map, void *arg) | |
3996 { | |
3997 struct describe_map_shadow_closure *c = | |
3998 (struct describe_map_shadow_closure *) arg; | |
3999 | |
4000 if (EQ (map, c->self)) | |
4001 return Qzero; /* Not shadowed; terminate search */ | |
4002 | |
934 | 4003 return !NILP (keymap_lookup_directly (map, |
4004 KEY_DATA_KEYSYM (c->raw_key), | |
4005 KEY_DATA_MODIFIERS (c->raw_key))) | |
428 | 4006 ? Qt : Qnil; |
4007 } | |
4008 | |
4009 | |
4010 static Lisp_Object | |
4011 keymap_lookup_inherited_mapper (Lisp_Object km, void *arg) | |
4012 { | |
934 | 4013 Lisp_Key_Data *k = (Lisp_Key_Data *) arg; |
4014 return keymap_lookup_directly (km, KEY_DATA_KEYSYM (k), KEY_DATA_MODIFIERS (k)); | |
428 | 4015 } |
4016 | |
4017 | |
4018 static void | |
934 | 4019 describe_map_mapper (const Lisp_Key_Data *key, |
428 | 4020 Lisp_Object binding, |
4021 void *describe_map_closure) | |
4022 { | |
4023 /* This function can GC */ | |
4024 struct describe_map_closure *closure = | |
4025 (struct describe_map_closure *) describe_map_closure; | |
934 | 4026 Lisp_Object keysym = KEY_DATA_KEYSYM (key); |
4027 int modifiers = KEY_DATA_MODIFIERS (key); | |
428 | 4028 |
4029 /* Don't mention suppressed commands. */ | |
4030 if (SYMBOLP (binding) | |
4031 && !NILP (closure->partial) | |
4032 && !NILP (Fget (binding, closure->partial, Qnil))) | |
4033 return; | |
4034 | |
4035 /* If we're only supposed to display mouse bindings and this isn't one, | |
4036 then bug out. */ | |
4037 if (closure->mice_only_p && | |
4038 (! (EQ (keysym, Qbutton0) || | |
4039 EQ (keysym, Qbutton1) || | |
4040 EQ (keysym, Qbutton2) || | |
4041 EQ (keysym, Qbutton3) || | |
4042 EQ (keysym, Qbutton4) || | |
4043 EQ (keysym, Qbutton5) || | |
4044 EQ (keysym, Qbutton6) || | |
4045 EQ (keysym, Qbutton7) || | |
4046 EQ (keysym, Qbutton0up) || | |
4047 EQ (keysym, Qbutton1up) || | |
4048 EQ (keysym, Qbutton2up) || | |
4049 EQ (keysym, Qbutton3up) || | |
4050 EQ (keysym, Qbutton4up) || | |
4051 EQ (keysym, Qbutton5up) || | |
4052 EQ (keysym, Qbutton6up) || | |
4053 EQ (keysym, Qbutton7up)))) | |
4054 return; | |
4055 | |
4056 /* If this command in this map is shadowed by some other map, ignore it. */ | |
4057 { | |
4058 Lisp_Object tail; | |
4059 | |
4060 for (tail = closure->shadow; CONSP (tail); tail = XCDR (tail)) | |
4061 { | |
4062 QUIT; | |
4063 if (!NILP (traverse_keymaps (XCAR (tail), Qnil, | |
4064 keymap_lookup_inherited_mapper, | |
4065 /* Cast to discard `const' */ | |
4066 (void *)key))) | |
4067 return; | |
4068 } | |
4069 } | |
4070 | |
4071 /* If this key is in some map of which this map is a parent, then ignore | |
4072 it (in that case, it has been shadowed). | |
4073 */ | |
4074 { | |
4075 Lisp_Object sh; | |
4076 struct describe_map_shadow_closure c; | |
4077 c.raw_key = key; | |
4078 c.self = closure->self; | |
4079 | |
4080 sh = traverse_keymaps (closure->self_root, Qnil, | |
4081 describe_map_mapper_shadow_search, &c); | |
4082 if (!NILP (sh) && !ZEROP (sh)) | |
4083 return; | |
4084 } | |
4085 | |
4086 /* Otherwise add it to the list to be sorted. */ | |
4087 *(closure->list) = Fcons (Fcons (Fcons (keysym, make_int (modifiers)), | |
4088 binding), | |
4089 *(closure->list)); | |
4090 } | |
4091 | |
4092 | |
4093 static int | |
4094 describe_map_sort_predicate (Lisp_Object obj1, Lisp_Object obj2, | |
4095 Lisp_Object pred) | |
4096 { | |
4097 /* obj1 and obj2 are conses of the form | |
4098 ( ( <keysym> . <modifiers> ) . <binding> ) | |
4099 keysym and modifiers are used, binding is ignored. | |
4100 */ | |
442 | 4101 int bit1, bit2; |
428 | 4102 obj1 = XCAR (obj1); |
4103 obj2 = XCAR (obj2); | |
4104 bit1 = XINT (XCDR (obj1)); | |
4105 bit2 = XINT (XCDR (obj2)); | |
4106 if (bit1 != bit2) | |
4107 return bit1 < bit2 ? 1 : -1; | |
4108 else | |
4109 return map_keymap_sort_predicate (obj1, obj2, pred); | |
4110 } | |
4111 | |
4112 /* Elide 2 or more consecutive numeric keysyms bound to the same thing, | |
4113 or 2 or more symbolic keysyms that are bound to the same thing and | |
4114 have consecutive character-set-properties. | |
4115 */ | |
4116 static int | |
4117 elide_next_two_p (Lisp_Object list) | |
4118 { | |
4119 Lisp_Object s1, s2; | |
2828 | 4120 extern Lisp_Object Qcharacter_of_keysym; |
428 | 4121 |
4122 if (NILP (XCDR (list))) | |
4123 return 0; | |
4124 | |
4125 /* next two bindings differ */ | |
4126 if (!EQ (XCDR (XCAR (list)), | |
4127 XCDR (XCAR (XCDR (list))))) | |
4128 return 0; | |
4129 | |
4130 /* next two modifier-sets differ */ | |
4131 if (!EQ (XCDR (XCAR (XCAR (list))), | |
4132 XCDR (XCAR (XCAR (XCDR (list)))))) | |
4133 return 0; | |
4134 | |
4135 s1 = XCAR (XCAR (XCAR (list))); | |
4136 s2 = XCAR (XCAR (XCAR (XCDR (list)))); | |
4137 | |
4138 if (SYMBOLP (s1)) | |
4139 { | |
2828 | 4140 Lisp_Object code = Fget (s1, Qcharacter_of_keysym, Qnil); |
428 | 4141 if (CHAR_OR_CHAR_INTP (code)) |
4142 { | |
4143 s1 = code; | |
4144 CHECK_CHAR_COERCE_INT (s1); | |
4145 } | |
4146 else return 0; | |
4147 } | |
4148 if (SYMBOLP (s2)) | |
4149 { | |
2828 | 4150 Lisp_Object code = Fget (s2, Qcharacter_of_keysym, Qnil); |
428 | 4151 if (CHAR_OR_CHAR_INTP (code)) |
4152 { | |
4153 s2 = code; | |
4154 CHECK_CHAR_COERCE_INT (s2); | |
4155 } | |
4156 else return 0; | |
4157 } | |
4158 | |
4159 return (XCHAR (s1) == XCHAR (s2) || | |
4160 XCHAR (s1) + 1 == XCHAR (s2)); | |
4161 } | |
4162 | |
4163 | |
4164 static Lisp_Object | |
4165 describe_map_parent_mapper (Lisp_Object keymap, void *arg) | |
4166 { | |
4167 /* This function can GC */ | |
4168 struct describe_map_closure *describe_map_closure = | |
4169 (struct describe_map_closure *) arg; | |
4170 describe_map_closure->self = keymap; | |
4171 map_keymap (XKEYMAP (keymap)->table, | |
4172 0, /* don't sort: we'll do it later */ | |
4173 describe_map_mapper, describe_map_closure); | |
4174 return Qnil; | |
4175 } | |
4176 | |
4177 | |
4178 /* Describe the contents of map MAP, assuming that this map itself is | |
4179 reached by the sequence of prefix keys KEYS (a string or vector). | |
4180 PARTIAL, SHADOW, NOMENU are as in `describe_map_tree' above. */ | |
4181 | |
4182 static void | |
4183 describe_map (Lisp_Object keymap, Lisp_Object elt_prefix, | |
4184 void (*elt_describer) (Lisp_Object, Lisp_Object), | |
4185 int partial, | |
4186 Lisp_Object shadow, | |
4187 int mice_only_p, | |
4188 Lisp_Object buffer) | |
4189 { | |
4190 /* This function can GC */ | |
4191 struct describe_map_closure describe_map_closure; | |
4192 Lisp_Object list = Qnil; | |
4193 struct buffer *buf = XBUFFER (buffer); | |
867 | 4194 Ichar printable_min = (CHAR_OR_CHAR_INTP (buf->ctl_arrow) |
428 | 4195 ? XCHAR_OR_CHAR_INT (buf->ctl_arrow) |
4196 : ((EQ (buf->ctl_arrow, Qt) | |
4197 || EQ (buf->ctl_arrow, Qnil)) | |
4198 ? 256 : 160)); | |
4199 int elided = 0; | |
4200 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
2828 | 4201 extern Lisp_Object Qcharacter_of_keysym; |
428 | 4202 |
4203 keymap = get_keymap (keymap, 1, 1); | |
4204 describe_map_closure.partial = (partial ? Qsuppress_keymap : Qnil); | |
4205 describe_map_closure.shadow = shadow; | |
4206 describe_map_closure.list = &list; | |
4207 describe_map_closure.self_root = keymap; | |
4208 describe_map_closure.mice_only_p = mice_only_p; | |
4209 | |
4210 GCPRO4 (keymap, elt_prefix, shadow, list); | |
4211 | |
4212 traverse_keymaps (keymap, Qnil, | |
4213 describe_map_parent_mapper, &describe_map_closure); | |
4214 | |
4215 if (!NILP (list)) | |
4216 { | |
4217 list = list_sort (list, Qnil, describe_map_sort_predicate); | |
4218 buffer_insert_c_string (buf, "\n"); | |
4219 while (!NILP (list)) | |
4220 { | |
4221 Lisp_Object elt = XCAR (XCAR (list)); | |
4222 Lisp_Object keysym = XCAR (elt); | |
442 | 4223 int modifiers = XINT (XCDR (elt)); |
428 | 4224 |
4225 if (!NILP (elt_prefix)) | |
4226 buffer_insert_lisp_string (buf, elt_prefix); | |
4227 | |
442 | 4228 if (modifiers & XEMACS_MOD_META) |
4229 buffer_insert_c_string (buf, "M-"); | |
4230 if (modifiers & XEMACS_MOD_CONTROL) | |
4231 buffer_insert_c_string (buf, "C-"); | |
4232 if (modifiers & XEMACS_MOD_SUPER) | |
4233 buffer_insert_c_string (buf, "S-"); | |
4234 if (modifiers & XEMACS_MOD_HYPER) | |
4235 buffer_insert_c_string (buf, "H-"); | |
4236 if (modifiers & XEMACS_MOD_ALT) | |
4237 buffer_insert_c_string (buf, "Alt-"); | |
4238 if (modifiers & XEMACS_MOD_SHIFT) | |
4239 buffer_insert_c_string (buf, "Sh-"); | |
428 | 4240 if (SYMBOLP (keysym)) |
4241 { | |
2828 | 4242 Lisp_Object code = Fget (keysym, Qcharacter_of_keysym, Qnil); |
867 | 4243 Ichar c = (CHAR_OR_CHAR_INTP (code) |
4244 ? XCHAR_OR_CHAR_INT (code) : (Ichar) -1); | |
428 | 4245 /* Calling Fsingle_key_description() would cons more */ |
4246 #if 0 /* This is bogus */ | |
4247 if (EQ (keysym, QKlinefeed)) | |
4248 buffer_insert_c_string (buf, "LFD"); | |
4249 else if (EQ (keysym, QKtab)) | |
4250 buffer_insert_c_string (buf, "TAB"); | |
4251 else if (EQ (keysym, QKreturn)) | |
4252 buffer_insert_c_string (buf, "RET"); | |
4253 else if (EQ (keysym, QKescape)) | |
4254 buffer_insert_c_string (buf, "ESC"); | |
4255 else if (EQ (keysym, QKdelete)) | |
4256 buffer_insert_c_string (buf, "DEL"); | |
4257 else if (EQ (keysym, QKspace)) | |
4258 buffer_insert_c_string (buf, "SPC"); | |
4259 else if (EQ (keysym, QKbackspace)) | |
4260 buffer_insert_c_string (buf, "BS"); | |
4261 else | |
4262 #endif | |
4263 if (c >= printable_min) | |
4264 buffer_insert_emacs_char (buf, c); | |
4265 else buffer_insert1 (buf, Fsymbol_name (keysym)); | |
4266 } | |
4267 else if (CHARP (keysym)) | |
4268 buffer_insert_emacs_char (buf, XCHAR (keysym)); | |
4269 else | |
4270 buffer_insert_c_string (buf, "---bad keysym---"); | |
4271 | |
4272 if (elided) | |
4273 elided = 0; | |
4274 else | |
4275 { | |
4276 int k = 0; | |
4277 | |
4278 while (elide_next_two_p (list)) | |
4279 { | |
4280 k++; | |
4281 list = XCDR (list); | |
4282 } | |
4283 if (k != 0) | |
4284 { | |
4285 if (k == 1) | |
4286 buffer_insert_c_string (buf, ", "); | |
4287 else | |
4288 buffer_insert_c_string (buf, " .. "); | |
4289 elided = 1; | |
4290 continue; | |
4291 } | |
4292 } | |
4293 | |
4294 /* Print a description of the definition of this character. */ | |
4295 (*elt_describer) (XCDR (XCAR (list)), buffer); | |
4296 list = XCDR (list); | |
4297 } | |
4298 } | |
4299 UNGCPRO; | |
4300 } | |
4301 | |
4302 | |
4303 void | |
4304 syms_of_keymap (void) | |
4305 { | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
4306 INIT_LISP_OBJECT (keymap); |
442 | 4307 |
502 | 4308 DEFSYMBOL (Qminor_mode_map_alist); |
4309 | |
4310 DEFSYMBOL (Qkeymapp); | |
4311 | |
4312 DEFSYMBOL (Qsuppress_keymap); | |
4313 | |
4314 DEFSYMBOL (Qmodeline_map); | |
4315 DEFSYMBOL (Qtoolbar_map); | |
428 | 4316 |
4317 DEFSUBR (Fkeymap_parents); | |
4318 DEFSUBR (Fset_keymap_parents); | |
4319 DEFSUBR (Fkeymap_name); | |
4320 DEFSUBR (Fset_keymap_name); | |
4321 DEFSUBR (Fkeymap_prompt); | |
4322 DEFSUBR (Fset_keymap_prompt); | |
4323 DEFSUBR (Fkeymap_default_binding); | |
4324 DEFSUBR (Fset_keymap_default_binding); | |
4325 | |
4326 DEFSUBR (Fkeymapp); | |
4327 DEFSUBR (Fmake_keymap); | |
4328 DEFSUBR (Fmake_sparse_keymap); | |
4329 | |
4330 DEFSUBR (Fcopy_keymap); | |
4331 DEFSUBR (Fkeymap_fullness); | |
4332 DEFSUBR (Fmap_keymap); | |
4333 DEFSUBR (Fevent_matches_key_specifier_p); | |
4334 DEFSUBR (Fdefine_key); | |
4335 DEFSUBR (Flookup_key); | |
4336 DEFSUBR (Fkey_binding); | |
4337 DEFSUBR (Fuse_global_map); | |
4338 DEFSUBR (Fuse_local_map); | |
4339 DEFSUBR (Fcurrent_local_map); | |
4340 DEFSUBR (Fcurrent_global_map); | |
4341 DEFSUBR (Fcurrent_keymaps); | |
4342 DEFSUBR (Faccessible_keymaps); | |
4343 DEFSUBR (Fkey_description); | |
4344 DEFSUBR (Fsingle_key_description); | |
4345 DEFSUBR (Fwhere_is_internal); | |
4346 DEFSUBR (Fdescribe_bindings_internal); | |
4347 | |
4348 DEFSUBR (Ftext_char_description); | |
4349 | |
502 | 4350 DEFSYMBOL (Qcontrol); |
4351 DEFSYMBOL (Qctrl); | |
4352 DEFSYMBOL (Qmeta); | |
4353 DEFSYMBOL (Qsuper); | |
4354 DEFSYMBOL (Qhyper); | |
4355 DEFSYMBOL (Qalt); | |
4356 DEFSYMBOL (Qshift); | |
4357 DEFSYMBOL (Qbutton0); | |
4358 DEFSYMBOL (Qbutton1); | |
4359 DEFSYMBOL (Qbutton2); | |
4360 DEFSYMBOL (Qbutton3); | |
4361 DEFSYMBOL (Qbutton4); | |
4362 DEFSYMBOL (Qbutton5); | |
4363 DEFSYMBOL (Qbutton6); | |
4364 DEFSYMBOL (Qbutton7); | |
4365 DEFSYMBOL (Qbutton0up); | |
4366 DEFSYMBOL (Qbutton1up); | |
4367 DEFSYMBOL (Qbutton2up); | |
4368 DEFSYMBOL (Qbutton3up); | |
4369 DEFSYMBOL (Qbutton4up); | |
4370 DEFSYMBOL (Qbutton5up); | |
4371 DEFSYMBOL (Qbutton6up); | |
4372 DEFSYMBOL (Qbutton7up); | |
4373 DEFSYMBOL (Qmouse_1); | |
4374 DEFSYMBOL (Qmouse_2); | |
4375 DEFSYMBOL (Qmouse_3); | |
4376 DEFSYMBOL (Qmouse_4); | |
4377 DEFSYMBOL (Qmouse_5); | |
4378 DEFSYMBOL (Qmouse_6); | |
4379 DEFSYMBOL (Qmouse_7); | |
4380 DEFSYMBOL (Qdown_mouse_1); | |
4381 DEFSYMBOL (Qdown_mouse_2); | |
4382 DEFSYMBOL (Qdown_mouse_3); | |
4383 DEFSYMBOL (Qdown_mouse_4); | |
4384 DEFSYMBOL (Qdown_mouse_5); | |
4385 DEFSYMBOL (Qdown_mouse_6); | |
4386 DEFSYMBOL (Qdown_mouse_7); | |
4387 DEFSYMBOL (Qmenu_selection); | |
4388 DEFSYMBOL (QLFD); | |
4389 DEFSYMBOL (QTAB); | |
4390 DEFSYMBOL (QRET); | |
4391 DEFSYMBOL (QESC); | |
4392 DEFSYMBOL (QDEL); | |
4393 DEFSYMBOL (QSPC); | |
4394 DEFSYMBOL (QBS); | |
428 | 4395 } |
4396 | |
4397 void | |
4398 vars_of_keymap (void) | |
4399 { | |
4400 DEFVAR_LISP ("meta-prefix-char", &Vmeta_prefix_char /* | |
4401 Meta-prefix character. | |
4402 This character followed by some character `foo' turns into `Meta-foo'. | |
4403 This can be any form recognized as a single key specifier. | |
4404 To disable the meta-prefix-char, set it to a negative number. | |
4405 */ ); | |
4406 Vmeta_prefix_char = make_char (033); | |
4407 | |
4408 DEFVAR_LISP ("mouse-grabbed-buffer", &Vmouse_grabbed_buffer /* | |
4409 A buffer which should be consulted first for all mouse activity. | |
4410 When a mouse-click is processed, it will first be looked up in the | |
4411 local-map of this buffer, and then through the normal mechanism if there | |
4412 is no binding for that click. This buffer's value of `mode-motion-hook' | |
4413 will be consulted instead of the `mode-motion-hook' of the buffer of the | |
4414 window under the mouse. You should *bind* this, not set it. | |
4415 */ ); | |
4416 Vmouse_grabbed_buffer = Qnil; | |
4417 | |
4418 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map /* | |
4419 Keymap that overrides all other local keymaps. | |
4420 If this variable is non-nil, it is used as a keymap instead of the | |
4421 buffer's local map, and the minor mode keymaps and extent-local keymaps. | |
4422 You should *bind* this, not set it. | |
4423 */ ); | |
4424 Voverriding_local_map = Qnil; | |
4425 | |
4426 Fset (Qminor_mode_map_alist, Qnil); | |
4427 | |
4428 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map /* | |
4429 Keymap of key translations that can override keymaps. | |
2027 | 4430 |
4431 This keymap works like `function-key-map', but is searched before it, | |
428 | 4432 and applies even for keys that have ordinary bindings. |
2027 | 4433 |
4434 The `read-key-sequence' function replaces any subsequence bound by | |
4435 `key-translation-map' with its binding. More precisely, when the active | |
4436 keymaps have no binding for the current key sequence but | |
4437 `key-translation-map' binds a suffix of the sequence to a vector or string, | |
4438 `read-key-sequence' replaces the matching suffix with its binding, and | |
4439 continues with the new sequence. See `key-binding' for details. | |
4440 | |
4441 The events that come from bindings in `key-translation-map' are not | |
4442 themselves looked up in `key-translation-map'. | |
4443 | |
4444 #### FIXME: stolen from `function-key-map'; need better example. | |
4445 #### I guess you could implement a Dvorak keyboard with this? | |
4446 For example, suppose `key-translation-map' binds `ESC O P' to [f1]. | |
4447 Typing `ESC O P' to `read-key-sequence' would return | |
4448 \[#<keypress-event f1>]. Typing `C-x ESC O P' would return | |
4449 \[#<keypress-event control-X> #<keypress-event f1>]. If [f1] | |
4450 were a prefix key, typing `ESC O P x' would return | |
4451 \[#<keypress-event f1> #<keypress-event x>]. | |
428 | 4452 */ ); |
4453 Vkey_translation_map = Qnil; | |
4454 | |
771 | 4455 DEFVAR_LISP ("global-tty-map", &Vglobal_tty_map /* |
4456 Global keymap that applies only to TTY's. | |
4457 Key bindings are looked up in this map just before looking in the global map, | |
4458 but only when the current console is a TTY console. See also | |
4459 `global-window-system-map'. | |
4460 */ ); | |
4461 Vglobal_tty_map = Qnil; | |
4462 | |
4463 DEFVAR_LISP ("global-window-system-map", &Vglobal_window_system_map /* | |
4464 Global keymap that applies only to window systems. | |
4465 Key bindings are looked up in this map just before looking in the global map, | |
4466 but only when the current console is not a TTY console. See also | |
4467 `global-tty-map'. | |
4468 */ ); | |
4469 Vglobal_window_system_map = Qnil; | |
4470 | |
428 | 4471 DEFVAR_LISP ("vertical-divider-map", &Vvertical_divider_map /* |
4472 Keymap which handles mouse clicks over vertical dividers. | |
4473 */ ); | |
4474 Vvertical_divider_map = Qnil; | |
4475 | |
4476 DEFVAR_INT ("keymap-tick", &keymap_tick /* | |
4477 Incremented for each change to any keymap. | |
4478 */ ); | |
4479 keymap_tick = 0; | |
4480 | |
4481 staticpro (&Vcurrent_global_map); | |
4482 | |
867 | 4483 Vsingle_space_string = make_string ((const Ibyte *) " ", 1); |
428 | 4484 staticpro (&Vsingle_space_string); |
4485 } | |
4486 | |
4487 void | |
4488 complex_vars_of_keymap (void) | |
4489 { | |
4490 /* This function can GC */ | |
4491 Lisp_Object ESC_prefix = intern ("ESC-prefix"); | |
4492 Lisp_Object meta_disgustitute; | |
4493 | |
4494 Vcurrent_global_map = Fmake_keymap (Qnil); | |
771 | 4495 Vglobal_tty_map = Fmake_keymap (intern ("global-tty-map")); |
4496 Vglobal_window_system_map = | |
4497 Fmake_keymap (intern ("global-window-system-map")); | |
428 | 4498 |
4499 meta_disgustitute = Fmake_keymap (Qnil); | |
4500 Ffset (ESC_prefix, meta_disgustitute); | |
4501 /* no need to protect meta_disgustitute, though */ | |
442 | 4502 keymap_store_internal (MAKE_MODIFIER_HASH_KEY (XEMACS_MOD_META), |
428 | 4503 XKEYMAP (Vcurrent_global_map), |
4504 meta_disgustitute); | |
4505 XKEYMAP (Vcurrent_global_map)->sub_maps_cache = Qt; | |
4506 | |
4507 Vkey_translation_map = Fmake_sparse_keymap (intern ("key-translation-map")); | |
4508 } |