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