| 1 |
/************************************************************************** |
| 2 |
* This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is * |
| 3 |
* provided by Jonathan and Jovehacks without charge and without * |
| 4 |
* warranty. You may copy, modify, and/or distribute JOVE, provided that * |
| 5 |
* this notice is included in all the source files and documentation. * |
| 6 |
**************************************************************************/ |
| 7 |
|
| 8 |
#include "jove.h" |
| 9 |
#include "list.h" |
| 10 |
#include "fp.h" |
| 11 |
#include "jctype.h" |
| 12 |
#include "chars.h" |
| 13 |
#include "disp.h" |
| 14 |
#include "re.h" |
| 15 |
#include "ask.h" |
| 16 |
#include "commands.h" |
| 17 |
#include "macros.h" |
| 18 |
#include "extend.h" |
| 19 |
#include "fmt.h" |
| 20 |
#include "screen.h" /* for Placur */ |
| 21 |
#include "vars.h" |
| 22 |
|
| 23 |
#ifdef IPROCS |
| 24 |
# include "iproc.h" /* ... for definition of ProcNewline() */ |
| 25 |
#endif |
| 26 |
|
| 27 |
#ifdef PCNONASCII |
| 28 |
private const char *const altseq[] = { |
| 29 |
/*000*/NULL, "Alt Esc", NULL, "Ctl-@", NULL, NULL, NULL, "Ctl-^", |
| 30 |
/*010*/NULL, NULL, NULL, NULL, "Ctl-_", "Enter", "Alt-Bksp", "Left", |
| 31 |
/*020*/"Alt-Q", "Alt-W", "Alt-E", "Alt-R", "Alt-T", "Alt-Y", "Alt-U", "Alt-I", |
| 32 |
/*030*/"Alt-O", "Alt-P", "Alt-[", "Alt-]", "Alt-Enter", NULL, "Alt-A", "Alt-S", |
| 33 |
/*040*/"Alt-D", "Alt-F", "Alt-G", "Alt-H", "Alt-J", "Alt-K", "Alt-L", "Alt-;", |
| 34 |
/*050*/"Alt-'", "Alt-~", NULL, "Alt-\\", "Alt-Z", "Alt-X", "Alt-C", "Alt-V", |
| 35 |
/*060*/"Alt-B", "Alt-N", "Alt-M", "Alt-,", "Alt-.", "Alt-/", NULL, "Alt KP-", |
| 36 |
/*070*/NULL, NULL, NULL, "F1", "F2", "F3", "F4", "F5", |
| 37 |
/*100*/"F6", "F7", "F8", "F9", "F10", NULL, NULL, "Home", |
| 38 |
/*110*/"Up", "PgUp", "Alt KP-", "Left", "Shift KP5", "Right", "Alt KP+", "End", |
| 39 |
/*120*/"Down", "PgDn", "Ins", "Del", "Shift F1", "Shift F2", "Shift F3", "Shift F4", |
| 40 |
/*130*/"Shift F5", "Shift F6", "Shift F7", "Shift F8", "Shift F9", "Shift F10", "Ctl F1", "Ctl F2", |
| 41 |
/*140*/"Ctl F3", "Ctl F4", "Ctl F5", "Ctl F6", "Ctl F7", "Ctl F8", "Ctl F9", "Ctl F10", |
| 42 |
/*150*/"Alt F1", "Alt F2", "Alt F3", "Alt F4", "Alt F5", "Alt F6", "Alt F7", "Alt F8", |
| 43 |
/*160*/"Alt F9", "Alt F10", "Ctl PrtSc", "Ctl Left", "Ctl Right", "Ctl End", "Ctl PgDn", "Ctl Home", |
| 44 |
/*170*/"Alt 1", "Alt 2", "Alt 3", "Alt 4", "Alt 5", "Alt 6", "Alt 7", "Alt 8", |
| 45 |
/*200*/"Alt 9", "Alt 0", "Alt Minus", "Alt Equals", "Ctl PgUp", "F11", "F12", "Shift F11", |
| 46 |
/*210*/"Shift F12", "Ctl F11", "Ctl F12", "Alt F11", "Alt F12", "Ctl Up", "Ctl KP-", "Ctl KP5", |
| 47 |
/*220*/"Ctl KP+", "Ctl Down", "Ctl Ins", "Ctl Del", "Ctl Tab", "Ctl KP/", "Ctl KP*", "Alt Home", |
| 48 |
/*230*/"Alt Up", "Alt PgUp", NULL, "Alt Left", NULL, "Alt Right", NULL, "Alt End", |
| 49 |
/*240*/"Alt Down", "Alt PgDn", "Alt Ins", "Alt Del", "Alt KP/", "Alt Tab", "Alt KP Enter", NULL, |
| 50 |
/*250*/NULL, NULL, NULL, "Shift Home", "Shift Up", "Shift PgUp", "Shift Alt KP-", "Shift Left", |
| 51 |
/*260*/"Shift KP5", "Shift Right", "Shift Alt KP+", "Shift End", "Shift Down", "Shift PgDn", "Shift Ins", "Shift Del" |
| 52 |
}; |
| 53 |
#endif /* PCNONASCII */ |
| 54 |
|
| 55 |
int this_cmd, last_cmd; |
| 56 |
|
| 57 |
/* |
| 58 |
* A data_obj can be a command, a macro, a variable or a keymap. So a keymap |
| 59 |
* can be bound to a key just like commands, macros and variables can. Users |
| 60 |
* don't bind keymaps to keys, though, they just bind the other things to |
| 61 |
* keys, and this package automatically creates keymaps as they are needed to |
| 62 |
* accomodate the specified key sequences. |
| 63 |
* |
| 64 |
* NOTE: Since users don't bind keymaps to keys, there is no reason for keymaps |
| 65 |
* to have names. Right now they do, but perhaps that can be fixed later. At |
| 66 |
* least the names need not be made visible. |
| 67 |
*/ |
| 68 |
|
| 69 |
/* |
| 70 |
* A keymap is either a sparse keymap or a full keymap. The main keymap as |
| 71 |
* well as escape and control X maps are full maps, because they are almost |
| 72 |
* full. All other keymaps that the system creates on behalf of users in |
| 73 |
* order to accomodate an arbitrary key binding will be sparse maps, on the |
| 74 |
* theory that they will be pretty small. Sparse keymaps are sorted. |
| 75 |
*/ |
| 76 |
|
| 77 |
#define KEYMAP_QUANTUM 8 /* sparse keymap allocation batch size */ |
| 78 |
#define SPARSE_LIMIT (NCHARS/3 - KEYMAP_QUANTUM) /* threshold for conversion */ |
| 79 |
|
| 80 |
struct keybinding { |
| 81 |
ZXchar key; |
| 82 |
data_obj *value; |
| 83 |
}; |
| 84 |
|
| 85 |
struct keymap { |
| 86 |
int Type; /* keymap type (sparse or full) */ |
| 87 |
char *Name; /* keymap name */ |
| 88 |
struct keymap *next_map; |
| 89 |
jbool unfreeable; |
| 90 |
jbool mark; /* used for cycle avoidance */ |
| 91 |
union { |
| 92 |
struct { |
| 93 |
data_obj **map; /* keys array indexed by key */ |
| 94 |
} full; |
| 95 |
struct { |
| 96 |
struct keybinding *bindings; |
| 97 |
short nused; /* number of keybindings used */ |
| 98 |
short nalloc; /* number of keybindings allocated */ |
| 99 |
} sparse; |
| 100 |
} u; |
| 101 |
}; |
| 102 |
|
| 103 |
#define IsKeymap(/* data_obj* */ o) ((o) != NULL && \ |
| 104 |
((o)->Type == FULL_KEYMAP || \ |
| 105 |
(o)->Type == SPARSE_KEYMAP)) |
| 106 |
|
| 107 |
private struct keymap *keymaps = NULL; /* list of all keymaps */ |
| 108 |
|
| 109 |
private struct keymap *mainmap; |
| 110 |
|
| 111 |
#ifdef IPROCS |
| 112 |
private struct keymap *procsmap; |
| 113 |
#endif |
| 114 |
|
| 115 |
/* |
| 116 |
* Make a new keymap. Type is either FULL_KEYMAP or SPARSE_KEYMAP. If it's |
| 117 |
* full, the keys is either NULL or a pointer to a valid keymap of length |
| 118 |
* NCHARS. |
| 119 |
*/ |
| 120 |
|
| 121 |
private void |
| 122 |
km_init(struct keymap *km, int kind, data_obj **keys) |
| 123 |
{ |
| 124 |
byte_zero(&km->u, sizeof km->u); /* zero integral fields */ |
| 125 |
if ((km->Type = kind) == FULL_KEYMAP) { |
| 126 |
if (keys == NULL) { |
| 127 |
data_obj **p; |
| 128 |
|
| 129 |
keys = (data_obj **) emalloc(NCHARS * sizeof(data_obj *)); |
| 130 |
p = &keys[NCHARS]; |
| 131 |
do *--p = NULL; while (p != keys); |
| 132 |
} else { |
| 133 |
km->unfreeable = YES; |
| 134 |
} |
| 135 |
km->u.full.map = keys; |
| 136 |
} else { |
| 137 |
km->u.sparse.bindings = NULL; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
private struct keymap * |
| 142 |
km_new(int kind, data_obj **keys, char *name) |
| 143 |
{ |
| 144 |
struct keymap *km = (struct keymap *) emalloc(sizeof(struct keymap)); |
| 145 |
|
| 146 |
km->Name = name; |
| 147 |
km->next_map = keymaps; |
| 148 |
keymaps = km; |
| 149 |
km->unfreeable = NO; /* provisional */ |
| 150 |
km_init(km, kind, keys); |
| 151 |
return km; |
| 152 |
} |
| 153 |
|
| 154 |
/* km_getkey: look up key in keymap |
| 155 |
* |
| 156 |
* If the keymap is sparse, km_getkey sets km_sparsepos to the index |
| 157 |
* of the entry found (if any), or the index of the entry which would |
| 158 |
* be its successor (if not found). |
| 159 |
*/ |
| 160 |
|
| 161 |
private int km_sparsepos; |
| 162 |
|
| 163 |
private data_obj * |
| 164 |
km_getkey(struct keymap *km, ZXchar key) |
| 165 |
{ |
| 166 |
if (km->Type == FULL_KEYMAP) { |
| 167 |
return (km->u.full.map[key]); |
| 168 |
} else { |
| 169 |
struct keybinding *b = km->u.sparse.bindings; |
| 170 |
int lwb = 0; /* closed lower bound */ |
| 171 |
int upb = km->u.sparse.nused; /* open upper bound */ |
| 172 |
|
| 173 |
for (;;) { |
| 174 |
int mid = (lwb + upb) >> 1; /* fast average */ |
| 175 |
|
| 176 |
if (mid == lwb) |
| 177 |
break; |
| 178 |
|
| 179 |
if (b[mid].key <= key) |
| 180 |
lwb = mid; |
| 181 |
else |
| 182 |
upb = mid; |
| 183 |
} |
| 184 |
if (lwb != upb) { |
| 185 |
if (b[lwb].key == key) { |
| 186 |
/* found */ |
| 187 |
km_sparsepos = lwb; |
| 188 |
return b[lwb].value; |
| 189 |
} |
| 190 |
if (b[lwb].key > key) |
| 191 |
upb = lwb; /* make upb tight */ |
| 192 |
} |
| 193 |
/* not found; insertion would be before upb */ |
| 194 |
km_sparsepos = upb; |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
private ZXchar |
| 200 |
km_nextkey(struct keymap *km, ZXchar key) |
| 201 |
{ |
| 202 |
if (km->Type == FULL_KEYMAP) { |
| 203 |
while (key != NCHARS && km->u.full.map[key] == NULL) |
| 204 |
key++; |
| 205 |
} else { |
| 206 |
(void) km_getkey(km, key); |
| 207 |
key = km_sparsepos == km->u.sparse.nused |
| 208 |
? NCHARS : km->u.sparse.bindings[km_sparsepos].key; |
| 209 |
} |
| 210 |
return key; |
| 211 |
} |
| 212 |
|
| 213 |
/* Free a dataobj reference formerly bound in or as a keymap. |
| 214 |
* At the present time, only values that represent keymaps |
| 215 |
* can be usefully freed, and only those that are not marked as |
| 216 |
* unfreeable. unfreeable ones have other references (mainmap |
| 217 |
* or procsmap), or they include a non-heap (static) allocation. |
| 218 |
*/ |
| 219 |
|
| 220 |
void |
| 221 |
DelObjRef(data_obj *value) |
| 222 |
{ |
| 223 |
if (IsKeymap(value)) { |
| 224 |
struct keymap *km = (struct keymap *)value; |
| 225 |
|
| 226 |
if (!km->unfreeable) { |
| 227 |
ZXchar k; |
| 228 |
|
| 229 |
for (k = 0; (k = km_nextkey(km, k)) != NCHARS; k++) |
| 230 |
DelObjRef(km_getkey(km, k)); |
| 231 |
|
| 232 |
switch (km->Type) { |
| 233 |
case FULL_KEYMAP: |
| 234 |
free(km->u.full.map); |
| 235 |
break; |
| 236 |
case SPARSE_KEYMAP: |
| 237 |
free(km->u.sparse.bindings); |
| 238 |
break; |
| 239 |
} |
| 240 |
{ |
| 241 |
/* remove km from list of all keymaps */ |
| 242 |
struct keymap **p; |
| 243 |
|
| 244 |
for (p = &keymaps; *p != km; p = &(*p)->next_map) |
| 245 |
; |
| 246 |
*p = (*p)->next_map; |
| 247 |
} |
| 248 |
free(km); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
private void |
| 254 |
km_setkey(struct keymap *km, ZXchar key, data_obj *d) |
| 255 |
{ |
| 256 |
if (km->Type == FULL_KEYMAP) { |
| 257 |
DelObjRef(km->u.full.map[key]); |
| 258 |
km->u.full.map[key] = d; |
| 259 |
} else { |
| 260 |
struct keybinding *b = km->u.sparse.bindings; |
| 261 |
int nused = km->u.sparse.nused; |
| 262 |
|
| 263 |
if (km_getkey(km, key) != NULL) { |
| 264 |
/* overwriting a binding */ |
| 265 |
int i = km_sparsepos; |
| 266 |
|
| 267 |
DelObjRef(b[i].value); /* note: overwrites km_sparsepos */ |
| 268 |
if (d == NULL) { |
| 269 |
/* overwriting with NULL is actually deleting an entry */ |
| 270 |
while (++i < nused) |
| 271 |
b[i-1] = b[i]; |
| 272 |
km->u.sparse.nused -= 1; |
| 273 |
} else { |
| 274 |
b[i].value = d; |
| 275 |
} |
| 276 |
} else if (d != NULL) { |
| 277 |
/* inserting a new binding. */ |
| 278 |
if (nused == km->u.sparse.nalloc) { |
| 279 |
/* grow the keymap */ |
| 280 |
if (nused >= SPARSE_LIMIT) { |
| 281 |
/* convert to full keymap (in place!) */ |
| 282 |
km_init(km, FULL_KEYMAP, (data_obj **) NULL); |
| 283 |
while (nused-- != 0) |
| 284 |
km->u.full.map[b[nused].key] = b[nused].value; |
| 285 |
free(b); |
| 286 |
km->u.full.map[key] = d; |
| 287 |
return; |
| 288 |
} else { |
| 289 |
km->u.sparse.bindings = b = (struct keybinding *) erealloc( |
| 290 |
km->u.sparse.bindings, |
| 291 |
(km->u.sparse.nalloc += KEYMAP_QUANTUM) |
| 292 |
* sizeof(struct keybinding)); |
| 293 |
} |
| 294 |
} |
| 295 |
km->u.sparse.nused += 1; |
| 296 |
for (; nused > km_sparsepos; nused--) |
| 297 |
b[nused] = b[nused-1]; |
| 298 |
b[km_sparsepos].key = key; |
| 299 |
b[km_sparsepos].value = d; |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
private void |
| 305 |
UnmarkMaps(void) |
| 306 |
{ |
| 307 |
struct keymap *km; |
| 308 |
|
| 309 |
for (km = keymaps; km != NULL; km = km->next_map) |
| 310 |
km->mark = NO; |
| 311 |
} |
| 312 |
|
| 313 |
/* get the currently active keymaps into km_buf */ |
| 314 |
|
| 315 |
#define MAX_KEYMAPS 3 /* bound on number of keymaps found by get_keymaps */ |
| 316 |
|
| 317 |
private int |
| 318 |
get_keymaps(struct keymap **km_buf) |
| 319 |
{ |
| 320 |
int nmaps = 0; |
| 321 |
|
| 322 |
/* add maps to array in order of decreasing priority (and specificity) */ |
| 323 |
|
| 324 |
if (curbuf->b_map != NULL) |
| 325 |
km_buf[nmaps++] = curbuf->b_map; |
| 326 |
|
| 327 |
#ifdef IPROCS |
| 328 |
if (curbuf->b_process != NULL) |
| 329 |
km_buf[nmaps++] = procsmap; |
| 330 |
#endif |
| 331 |
|
| 332 |
km_buf[nmaps++] = mainmap; |
| 333 |
|
| 334 |
return nmaps; |
| 335 |
} |
| 336 |
|
| 337 |
jbool |
| 338 |
IsPrefixChar(ZXchar c) |
| 339 |
{ |
| 340 |
return IsKeymap(km_getkey(mainmap, c)); |
| 341 |
} |
| 342 |
|
| 343 |
private struct keymap * |
| 344 |
GetKeymap(struct keymap *m, ZXchar key) |
| 345 |
{ |
| 346 |
data_obj *val = km_getkey(m, key); |
| 347 |
|
| 348 |
return IsKeymap(val)? (struct keymap *) val : (struct keymap *) NULL; |
| 349 |
} |
| 350 |
|
| 351 |
private const data_obj * |
| 352 |
findmap(const char *fmt) |
| 353 |
{ |
| 354 |
struct keymap *km; |
| 355 |
const char *strings[128]; |
| 356 |
int i; |
| 357 |
|
| 358 |
for (km = keymaps, i = 0; km != NULL; km = km->next_map) |
| 359 |
if (i < (int) elemsof(strings) - 1 && km->Name != NULL) |
| 360 |
strings[i++] = km->Name; |
| 361 |
strings[i] = NULL; |
| 362 |
|
| 363 |
i = complete(strings, (char *)NULL, fmt, ALLOW_OLD | ALLOW_INDEX); |
| 364 |
|
| 365 |
for (km = keymaps; ; km = km->next_map) { |
| 366 |
if (km->Name != NULL && i-- == 0) { |
| 367 |
km->unfreeable = YES; |
| 368 |
return (data_obj *) km; |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
private void |
| 374 |
BindSequence(struct keymap *m, char *keys, int k_len, data_obj *obj) |
| 375 |
{ |
| 376 |
int i; |
| 377 |
|
| 378 |
if (k_len == 0) { |
| 379 |
complain("can't bind empty key sequence"); |
| 380 |
/* NOTREACHED */ |
| 381 |
} |
| 382 |
for (i = 0; i < k_len - 1; i++) { |
| 383 |
struct keymap *submap = GetKeymap(m, ZXC(keys[i])); |
| 384 |
|
| 385 |
if (submap == NULL) { |
| 386 |
submap = km_new(SPARSE_KEYMAP, (data_obj **)NULL, (char *)NULL); |
| 387 |
km_setkey(m, ZXC(keys[i]), (data_obj *) submap); |
| 388 |
} |
| 389 |
m = submap; |
| 390 |
} |
| 391 |
km_setkey(m, ZXC(keys[i]), obj); |
| 392 |
#ifdef MAC |
| 393 |
/* info for About Jove ... */ |
| 394 |
if (obj != NULL && obj_type(obj) == COMMAND) { |
| 395 |
struct cmd *cmd = (struct cmd *) obj; |
| 396 |
char map = 0; |
| 397 |
|
| 398 |
if (m->Type == FULL_KEYMAP) { |
| 399 |
if (m->u.full.map == MainKeys) |
| 400 |
map = F_MAINMAP; |
| 401 |
else if (m->u.full.map == EscKeys) |
| 402 |
map = F_PREF1MAP; |
| 403 |
else if (m->u.full.map == CtlxKeys) |
| 404 |
map = F_PREF2MAP; |
| 405 |
} |
| 406 |
if (map != 0) { |
| 407 |
cmd->c_map = map; |
| 408 |
cmd->c_key = ZXC(keys[i]); /* see about_j() in mac.c */ |
| 409 |
} |
| 410 |
} |
| 411 |
#endif |
| 412 |
} |
| 413 |
|
| 414 |
private void |
| 415 |
DoBind(const data_obj *(*findproc)(const char *), |
| 416 |
struct keymap *map) |
| 417 |
{ |
| 418 |
const data_obj *d = (*findproc) (ProcFmt); |
| 419 |
char keys[64]; |
| 420 |
int i; |
| 421 |
struct keymap *m; |
| 422 |
|
| 423 |
s_mess(": %f %s ", d->Name); |
| 424 |
if (obj_type(d) == COMMAND && ((struct cmd *)d)->c_proc == Unbound) |
| 425 |
d = NULL; |
| 426 |
i = 0; |
| 427 |
m = map; |
| 428 |
for (;;) { |
| 429 |
ZXchar c = addgetc(); |
| 430 |
|
| 431 |
if (c == EOF) |
| 432 |
break; |
| 433 |
|
| 434 |
if (i == sizeof(keys) - 1) { |
| 435 |
complain("key sequence too long"); |
| 436 |
/* NOTREACHED */ |
| 437 |
} |
| 438 |
keys[i++] = c; |
| 439 |
if (!InJoverc) { |
| 440 |
if (is_an_arg()) { |
| 441 |
/* Disgusting hack to allow more interactive power. |
| 442 |
* This ought to be replaced by a cleaner mechanism. |
| 443 |
*/ |
| 444 |
if (c == '\r' || c == '\n') { |
| 445 |
i -= 1; |
| 446 |
break; |
| 447 |
} |
| 448 |
if (c == '\\') |
| 449 |
keys[i-1] = addgetc(); |
| 450 |
} else { |
| 451 |
if ((m = GetKeymap(m, c)) == NULL) |
| 452 |
break; |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
BindSequence(map, keys, i, (data_obj *)d); /* lose const on d */ |
| 457 |
} |
| 458 |
|
| 459 |
private void |
| 460 |
DoLBind(const data_obj *(*findproc)(const char *)) |
| 461 |
{ |
| 462 |
if (curbuf->b_map == NULL) |
| 463 |
curbuf->b_map = km_new(SPARSE_KEYMAP, (data_obj **)NULL, (char *)NULL); |
| 464 |
DoBind(findproc, curbuf->b_map); |
| 465 |
} |
| 466 |
|
| 467 |
/* bind a command to a key in the buffer's local keymap. */ |
| 468 |
void |
| 469 |
LBindAKey(void) |
| 470 |
{ |
| 471 |
DoLBind(findcom); |
| 472 |
} |
| 473 |
|
| 474 |
/* bind a macro to a key in the buffers local keymap. */ |
| 475 |
void |
| 476 |
LBindMac(void) |
| 477 |
{ |
| 478 |
DoLBind(findmac); |
| 479 |
} |
| 480 |
|
| 481 |
void |
| 482 |
LBindMap(void) |
| 483 |
{ |
| 484 |
DoLBind(findmap); |
| 485 |
} |
| 486 |
|
| 487 |
void |
| 488 |
BindAKey(void) |
| 489 |
{ |
| 490 |
DoBind(findcom, mainmap); |
| 491 |
} |
| 492 |
|
| 493 |
void |
| 494 |
BindMac(void) |
| 495 |
{ |
| 496 |
DoBind(findmac, mainmap); |
| 497 |
} |
| 498 |
|
| 499 |
void |
| 500 |
BindMap(void) |
| 501 |
{ |
| 502 |
DoBind(findmap, mainmap); |
| 503 |
} |
| 504 |
|
| 505 |
#ifdef IPROCS |
| 506 |
|
| 507 |
void |
| 508 |
PBindAKey(void) |
| 509 |
{ |
| 510 |
DoBind(findcom, procsmap); |
| 511 |
} |
| 512 |
|
| 513 |
void |
| 514 |
PBindMac(void) |
| 515 |
{ |
| 516 |
DoBind(findmac, procsmap); |
| 517 |
} |
| 518 |
|
| 519 |
void |
| 520 |
PBindMap(void) |
| 521 |
{ |
| 522 |
DoBind(findmap, procsmap); |
| 523 |
} |
| 524 |
|
| 525 |
#endif |
| 526 |
|
| 527 |
void |
| 528 |
Unbound(void) |
| 529 |
{ |
| 530 |
complain("%f"); |
| 531 |
/* NOTREACHED */ |
| 532 |
} |
| 533 |
|
| 534 |
void |
| 535 |
KeyDesc(void) |
| 536 |
{ |
| 537 |
struct keymap *maps[MAX_KEYMAPS]; |
| 538 |
int nmaps; |
| 539 |
|
| 540 |
nmaps = get_keymaps(maps); |
| 541 |
s_mess(ProcFmt); |
| 542 |
while (YES) { |
| 543 |
int i; |
| 544 |
jbool still_hope = NO; |
| 545 |
ZXchar key = addgetc(); |
| 546 |
|
| 547 |
for (i = 0; i < nmaps; i++) { |
| 548 |
if (maps[i] != NULL) { |
| 549 |
data_obj *cp = km_getkey(maps[i], key); |
| 550 |
|
| 551 |
if (cp != NULL) { |
| 552 |
if (!IsKeymap(cp)) { |
| 553 |
add_mess("is bound to %s.", cp->Name); |
| 554 |
stickymsg = YES; |
| 555 |
return; |
| 556 |
} |
| 557 |
still_hope = YES; |
| 558 |
} |
| 559 |
maps[i] = (struct keymap *) cp; |
| 560 |
} |
| 561 |
} |
| 562 |
if (!still_hope) |
| 563 |
break; |
| 564 |
} |
| 565 |
add_mess("is unbound."); |
| 566 |
} |
| 567 |
|
| 568 |
private void |
| 569 |
DescMap(struct keymap *map, char *pref) |
| 570 |
{ |
| 571 |
if (map != NULL && !map->mark) { |
| 572 |
ZXchar c1, c2; |
| 573 |
|
| 574 |
map->mark = YES; |
| 575 |
for (c1 = km_nextkey(map, 0); c1 < NCHARS; c1 = km_nextkey(map, c2 + 1)) { |
| 576 |
data_obj *c1obj = km_getkey(map, c1); |
| 577 |
char keydescbuf[40]; |
| 578 |
|
| 579 |
c2 = c1; |
| 580 |
do {} while (++c2 < NCHARS && c1obj == km_getkey(map, c2)); |
| 581 |
c2 -= 1; |
| 582 |
swritef(keydescbuf, sizeof(keydescbuf), |
| 583 |
c1 == c2 ? "%s %p" : c1 + 1 == c2 ? "%s {%p,%p}" : "%s [%p-%p]", |
| 584 |
pref, c1, c2); |
| 585 |
if (IsKeymap(c1obj)) { |
| 586 |
#ifdef PCNONASCII |
| 587 |
/* horrible kludge to handle PC non-ASCII keys */ |
| 588 |
if (c1 == PCNONASCII) { |
| 589 |
ZXchar pc; |
| 590 |
struct keymap *pcm = (struct keymap *)c1obj; |
| 591 |
|
| 592 |
c2 = c1; /* don't handle range */ |
| 593 |
for (pc = km_nextkey(pcm, 0); pc < NCHARS; pc = km_nextkey(pcm, pc + 1)) { |
| 594 |
data_obj *pcobj = km_getkey(pcm, pc); |
| 595 |
const char *as = pc < elemsof(altseq)? altseq[pc] : NULL; |
| 596 |
char pckeydescbuf[40]; |
| 597 |
|
| 598 |
if (as == NULL) |
| 599 |
swritef(pckeydescbuf, sizeof(pckeydescbuf), |
| 600 |
"%s %p %p", pref, PCNONASCII, pc); |
| 601 |
else |
| 602 |
swritef(pckeydescbuf, sizeof(pckeydescbuf), |
| 603 |
"%s %s", pref, as); |
| 604 |
if (IsKeymap(pcobj)) |
| 605 |
DescMap((struct keymap *)pcobj, pckeydescbuf); |
| 606 |
else |
| 607 |
Typeout("%-18s %s", pckeydescbuf, pcobj->Name); |
| 608 |
} |
| 609 |
continue; |
| 610 |
} |
| 611 |
#endif /* PCNONASCII */ |
| 612 |
DescMap((struct keymap *)c1obj, keydescbuf); |
| 613 |
} else { |
| 614 |
Typeout("%-18s %s", keydescbuf, c1obj->Name); |
| 615 |
} |
| 616 |
} |
| 617 |
map->mark = NO; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
void |
| 622 |
DescBindings(void) |
| 623 |
{ |
| 624 |
TOstart("Key Bindings"); |
| 625 |
UnmarkMaps(); |
| 626 |
DescMap(mainmap, NullStr); |
| 627 |
DescMap(curbuf->b_map, "Local:"); |
| 628 |
#ifdef IPROCS |
| 629 |
DescMap(procsmap, "Proc:"); |
| 630 |
#endif |
| 631 |
TOstop(); |
| 632 |
} |
| 633 |
|
| 634 |
private char * |
| 635 |
fb_aux(const data_obj *cp, |
| 636 |
struct keymap *map, |
| 637 |
char *prefix, char *buf, |
| 638 |
size_t room) |
| 639 |
{ |
| 640 |
char *bufp = buf; |
| 641 |
|
| 642 |
if (map != NULL && !map->mark) { |
| 643 |
ZXchar c1, c2; |
| 644 |
|
| 645 |
map->mark = YES; |
| 646 |
for (c1 = km_nextkey(map, 0); c1 < NCHARS; c1 = km_nextkey(map, c2 + 1)) { |
| 647 |
data_obj *c1obj = km_getkey(map, c1); |
| 648 |
|
| 649 |
c2 = c1; |
| 650 |
if (c1obj == cp) { |
| 651 |
do {} while (++c2 < NCHARS && c1obj == km_getkey(map, c2)); |
| 652 |
c2 -= 1; |
| 653 |
swritef(bufp, room - (bufp-buf), |
| 654 |
c1==c2? "%s%p, " : c1+1==c2? "%s{%p,%p}, " : "%s[%p-%p], ", |
| 655 |
prefix, c1, c2); |
| 656 |
bufp += strlen(bufp); |
| 657 |
} |
| 658 |
if (IsKeymap(c1obj)) { |
| 659 |
char prefbuf[20]; |
| 660 |
|
| 661 |
#ifdef PCNONASCII |
| 662 |
/* horrible kludge to handle PC non-ASCII keys */ |
| 663 |
if (c1 == PCNONASCII) { |
| 664 |
struct keymap *pcm = (struct keymap *)c1obj; |
| 665 |
ZXchar pc; |
| 666 |
|
| 667 |
c2 = c1; /* don't handle range */ |
| 668 |
for (pc = km_nextkey(pcm, 0); pc < NCHARS; pc = km_nextkey(pcm, pc + 1)) { |
| 669 |
const char *as = pc < elemsof(altseq)? altseq[pc] : NULL; |
| 670 |
data_obj *pcobj = km_getkey(pcm, pc); |
| 671 |
|
| 672 |
if (pcobj == cp) { |
| 673 |
if (as == NULL) |
| 674 |
swritef(bufp, room - (bufp-buf), |
| 675 |
"%s%p %p, ", prefix, PCNONASCII, pc); |
| 676 |
else |
| 677 |
swritef(bufp, room - (bufp-buf), |
| 678 |
"%s%s, ", prefix, as); |
| 679 |
bufp += strlen(bufp); |
| 680 |
} |
| 681 |
if (IsKeymap(pcobj)) { |
| 682 |
if (as == NULL) |
| 683 |
swritef(prefbuf, sizeof(prefbuf), |
| 684 |
"%s%p %p ", prefix, PCNONASCII, pc); |
| 685 |
else |
| 686 |
swritef(prefbuf, sizeof(prefbuf), |
| 687 |
"%s%s ", prefix, as); |
| 688 |
bufp = fb_aux(cp, (struct keymap *) pcobj, |
| 689 |
prefbuf, bufp, room-(bufp-buf)); |
| 690 |
} |
| 691 |
} |
| 692 |
continue; |
| 693 |
} |
| 694 |
#endif /* PCNONASCII */ |
| 695 |
swritef(prefbuf, sizeof(prefbuf), "%s%p ", prefix, c1); |
| 696 |
bufp = fb_aux(cp, (struct keymap *) c1obj, prefbuf, bufp, |
| 697 |
room-(bufp-buf)); |
| 698 |
} |
| 699 |
} |
| 700 |
map->mark = NO; |
| 701 |
} |
| 702 |
return bufp; |
| 703 |
} |
| 704 |
|
| 705 |
private void |
| 706 |
find_binds(const data_obj *dp, char *buf, size_t size) |
| 707 |
{ |
| 708 |
char *endp; |
| 709 |
|
| 710 |
UnmarkMaps(); |
| 711 |
buf[0] = '\0'; |
| 712 |
endp = fb_aux(dp, mainmap, NullStr, buf, size); |
| 713 |
endp = fb_aux(dp, curbuf->b_map, "Local:", endp, size - (endp - buf)); |
| 714 |
#ifdef IPROCS |
| 715 |
endp = fb_aux(dp, procsmap, "Proc:", endp, size - (endp - buf)); |
| 716 |
#endif |
| 717 |
if ((endp > buf+2) && (strcmp(endp-2, ", ") == 0)) |
| 718 |
endp[-2] = '\0'; |
| 719 |
} |
| 720 |
|
| 721 |
private void |
| 722 |
ShowDoc(char *doc_type, const data_obj *dp, jbool show_bindings) |
| 723 |
{ |
| 724 |
char pattern[100]; |
| 725 |
char CmdDb[FILESIZE]; |
| 726 |
File *fp; |
| 727 |
|
| 728 |
PathCat(CmdDb, sizeof(CmdDb), ShareDir, "cmds.txt"); |
| 729 |
fp = open_file(CmdDb, iobuff, F_READ, YES); |
| 730 |
Placur(ILI, 0); |
| 731 |
flushscreen(); |
| 732 |
swritef(pattern, sizeof(pattern), "^:entry \"%s\" \"%s\"$", |
| 733 |
dp->Name, doc_type); |
| 734 |
TOstart("Help"); |
| 735 |
for (;;) { |
| 736 |
if (f_gets(fp, genbuf, (size_t) LBSIZE)) { |
| 737 |
Typeout("There is no documentation for \"%s\".", dp->Name); |
| 738 |
break; |
| 739 |
} |
| 740 |
if (genbuf[0] == ':' && LookingAt(pattern, genbuf, 0)) { |
| 741 |
/* found it ... let's print it */ |
| 742 |
static const char entrystr[] = ":entry"; |
| 743 |
|
| 744 |
if (show_bindings) { |
| 745 |
char binding[128]; |
| 746 |
|
| 747 |
find_binds(dp, binding, sizeof(binding)); |
| 748 |
if (blnkp(binding)) { |
| 749 |
Typeout("To invoke %s, type \"ESC X %s<cr>\".", |
| 750 |
dp->Name, dp->Name); |
| 751 |
} else { |
| 752 |
Typeout("Type \"%s\" to invoke %s.", |
| 753 |
binding, dp->Name); |
| 754 |
} |
| 755 |
} else |
| 756 |
Typeout("%s", dp->Name); |
| 757 |
Typeout(NullStr); |
| 758 |
while (!f_gets(fp, genbuf, (size_t) LBSIZE) |
| 759 |
&& strncmp(genbuf, entrystr, sizeof(entrystr) - 1) != 0) |
| 760 |
Typeout("%s", genbuf); |
| 761 |
break; |
| 762 |
} |
| 763 |
} |
| 764 |
f_close(fp); |
| 765 |
TOstop(); |
| 766 |
} |
| 767 |
|
| 768 |
void |
| 769 |
DescCom(void) |
| 770 |
{ |
| 771 |
ShowDoc("Command", findcom(ProcFmt), YES); |
| 772 |
} |
| 773 |
|
| 774 |
void |
| 775 |
DescVar(void) |
| 776 |
{ |
| 777 |
ShowDoc("Variable", findvar(ProcFmt), NO); |
| 778 |
} |
| 779 |
|
| 780 |
void |
| 781 |
Apropos(void) |
| 782 |
{ |
| 783 |
const struct cmd |
| 784 |
*cp; |
| 785 |
struct macro |
| 786 |
*m; |
| 787 |
const struct variable |
| 788 |
*v; |
| 789 |
const char |
| 790 |
*ans; |
| 791 |
jbool |
| 792 |
anyfs = NO, |
| 793 |
anyvs = NO, |
| 794 |
anyms = NO; |
| 795 |
char buf[MAXCOLS]; |
| 796 |
|
| 797 |
ans = ask((char *) NULL, ": %f (keyword) "); |
| 798 |
if (strcmp(ans, "?") == 0) |
| 799 |
ans = NullStr; /* matches everything */ |
| 800 |
TOstart("Help"); |
| 801 |
for (cp = commands; cp->Name != NULL && !TOabort; cp++) |
| 802 |
if (sindex(ans, cp->Name)) { |
| 803 |
if (!anyfs) { |
| 804 |
anyfs = YES; |
| 805 |
Typeout("Commands"); |
| 806 |
Typeout("--------"); |
| 807 |
} |
| 808 |
find_binds((data_obj *) cp, buf, sizeof(buf)); |
| 809 |
if (buf[0] != '\0') |
| 810 |
Typeout(": %-35s (%s)", cp->Name, buf); |
| 811 |
else |
| 812 |
Typeout(": %s", cp->Name); |
| 813 |
} |
| 814 |
for (v = variables; v->Name != NULL && !TOabort; v++) |
| 815 |
if (sindex(ans, v->Name)) { |
| 816 |
if (!anyvs) { |
| 817 |
anyvs = YES; |
| 818 |
if (anyfs) |
| 819 |
Typeout(NullStr); |
| 820 |
Typeout("Variables"); |
| 821 |
Typeout("---------"); |
| 822 |
} |
| 823 |
vpr_aux(v, buf, sizeof(buf)); |
| 824 |
Typeout(": set %-26s %s", v->Name, buf); |
| 825 |
} |
| 826 |
for (m = macros; m != NULL && !TOabort; m = m->m_nextm) |
| 827 |
if (sindex(ans, m->Name)) { |
| 828 |
if (!anyms) { |
| 829 |
anyms = YES; |
| 830 |
if (anyfs || anyvs) |
| 831 |
Typeout(NullStr); |
| 832 |
Typeout("Macros"); |
| 833 |
Typeout("------"); |
| 834 |
} |
| 835 |
find_binds((data_obj *) m, buf, sizeof(buf)); |
| 836 |
if (buf[0]) |
| 837 |
Typeout(": %-35s (%s)", m->Name, buf); |
| 838 |
else |
| 839 |
Typeout(": %s", m->Name); |
| 840 |
} |
| 841 |
TOstop(); |
| 842 |
} |
| 843 |
|
| 844 |
void |
| 845 |
InitKeymaps(void) |
| 846 |
{ |
| 847 |
struct keymap *km; |
| 848 |
|
| 849 |
mainmap = km_new(FULL_KEYMAP, MainKeys, "global-map"); |
| 850 |
|
| 851 |
/* setup ESC map */ |
| 852 |
km = km_new(FULL_KEYMAP, EscKeys, "ESC-map"); |
| 853 |
km_setkey(mainmap, ESC, (data_obj *) km); |
| 854 |
|
| 855 |
/* setup Ctlx map */ |
| 856 |
km = km_new(FULL_KEYMAP, CtlxKeys, "CtlX-map"); |
| 857 |
km_setkey(mainmap, CTL('X'), (data_obj *) km); |
| 858 |
|
| 859 |
#ifdef PCNONASCII |
| 860 |
km = km_new(FULL_KEYMAP, NonASCIIKeys, "non-ASCII-map"); |
| 861 |
km_setkey(mainmap, PCNONASCII, (data_obj *) km); |
| 862 |
#endif |
| 863 |
|
| 864 |
#ifdef IPROCS |
| 865 |
procsmap = km_new(SPARSE_KEYMAP, (data_obj **) NULL, "process-map"); |
| 866 |
procsmap->unfreeable = YES; |
| 867 |
BindSequence(procsmap, "\r", 1, (data_obj *) FindCmd(ProcNewline)); |
| 868 |
#endif |
| 869 |
} |
| 870 |
|
| 871 |
/* |
| 872 |
* Dispatch a character. If the specified character maps to a sub keymap, |
| 873 |
* this routine reads more characters until (1) a command is found, or (2) an |
| 874 |
* unbound key error occurs. Callers of this routine can be assured that it |
| 875 |
* won't return before completing a key sequence. |
| 876 |
*/ |
| 877 |
void |
| 878 |
dispatch(ZXchar c) |
| 879 |
{ |
| 880 |
struct keymap *maps[MAX_KEYMAPS]; |
| 881 |
int nmaps; |
| 882 |
|
| 883 |
if (InMacDefine) |
| 884 |
note_dispatch(); |
| 885 |
this_cmd = OTHER_CMD; |
| 886 |
nmaps = get_keymaps(maps); |
| 887 |
|
| 888 |
while (YES) { |
| 889 |
int i; |
| 890 |
jbool still_hope = NO; |
| 891 |
|
| 892 |
for (i = 0; i < nmaps; i++) { |
| 893 |
if (maps[i] != NULL) { |
| 894 |
data_obj *cp = km_getkey(maps[i], c); |
| 895 |
|
| 896 |
if (cp != NULL) { |
| 897 |
if (!IsKeymap(cp)) { |
| 898 |
ExecCmd(cp); |
| 899 |
return; |
| 900 |
} |
| 901 |
still_hope = YES; |
| 902 |
} |
| 903 |
maps[i] = (struct keymap *) cp; |
| 904 |
} |
| 905 |
} |
| 906 |
if (!still_hope) { |
| 907 |
char strokes[128]; |
| 908 |
|
| 909 |
pp_key_strokes(strokes, sizeof(strokes)); |
| 910 |
s_mess("[%sunbound]", strokes); |
| 911 |
rbell(); |
| 912 |
clr_arg_value(); |
| 913 |
stickymsg = NO; |
| 914 |
break; |
| 915 |
} |
| 916 |
c = waitchar(); |
| 917 |
if (c == AbortChar) { |
| 918 |
message("[Aborted]"); |
| 919 |
rbell(); |
| 920 |
break; |
| 921 |
} |
| 922 |
} |
| 923 |
} |