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