-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMAP.C
478 lines (426 loc) · 10.2 KB
/
MAP.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* Copyright (c) 1990,1991,1992 Chris and John Downey */
#ifndef lint
static char *sccsid = "@(#)map.c 2.1 (Chris & John Downey) 7/29/92";
#endif
/***
* program name:
xvi
* function:
PD version of UNIX "vi" editor, with extensions.
* module name:
map.c
* module function:
Keyboard input/pushback routines, "map" command.
Note that we provide key mapping through a different interface,
so that cursor key mappings etc do not show up to the user.
This works by having a two-stage process; first keymapping is
done, and then the result is fed through the normal mapping
process. The intent of the keymapping stage is to convert
machine-local keys into a standard form.
* bug:
If a map fails, we just pass all characters which had already
been accepted, plus the character which caused the mismatch,
straight through. This is not quite correct because we might
have got a good match starting at the very next character, i.e.
if we have mapped
foo to bar
and get input "ffoo", then the seconf 'f' will cause the map to
fail and both characters will go through, and so the whole thing
will pass through unmapped.
The only way around this problem is to introduce another flexbuf
on the input side of the keymap stage, to give us somewhere to
put all characters after the first, when a map fails. I.e. in the
case above, we would pass the first 'f' through to the "mp_dest"
flexbuf, and stuff any characters after that into "mp_src".
* history:
STEVIE - ST Editor for VI Enthusiasts, Version 3.10
Originally by Tim Thompson (twitch!tjt)
Extensive modifications by Tony Andrews (onecom!wldrdg!tony)
Heavily modified by Chris & John Downey
***/
#include "xvi.h"
/*
* This is the fundamental structure type which is used
* to hold mappings from one string to another.
*/
typedef struct map {
struct map *m_next;
char *m_lhs; /* lhs of map */
char *m_rhs; /* rhs of map */
unsigned int m_same; /* no characters same as next map */
} Map;
/*
* This structure holds a current position while scanning a map list.
* It is also effectively used to form a chain of mapping structures,
* interconnected with flexbufs.
*/
typedef struct mpos {
Map *mp_map;
int mp_index;
Flexbuf *mp_src;
Flexbuf *mp_dest;
} Mpos;
/*
* Two mapping structures exist; one for "map", and one for "map!".
*/
static Map *cmd_map = NULL;
static Map *ins_map = NULL;
static Map *key_map = NULL;
/*
* This is the position in cmd_map/ins_map when we are waiting for
* the next character. When not waiting, mp_map is NULL (as at start).
* Also stores flexbuf pointers for input and output at each stage.
*/
static Flexbuf getcbuff;
static Flexbuf inputbuff;
static Mpos npos = { NULL, 0, &inputbuff, &getcbuff };
static Mpos kpos = { NULL, 0, NULL, &inputbuff };
/*
* This is used for "display" mode; it records the current map which
* is being displayed. It is used by show_map().
*/
static Map *curmap;
static char *show_map P((void));
static void mapthrough P((void));
static bool_t process_map P((int, Mpos *));
static void calc_same P((Map *));
static void map_failed P((Mpos *));
static void insert_map P((Map **, char *, char *, bool_t));
static void delete_map P((Map **, char *));
/*VARARGS1*/
/*PRINTFLIKE*/
void
stuff
#ifdef __STDC__
(char *format, ...)
#else /* not __STDC__ */
(format, va_alist)
char *format;
va_dcl
#endif
{
va_list argp;
VA_START(argp, format);
(void) vformat(npos.mp_dest, format, argp);
va_end(argp);
}
int
map_getc()
{
return(flexempty(npos.mp_dest) ? EOF : flexpopch(npos.mp_dest));
}
void
map_char(c)
register int c;
{
/*
* Send the input character through the keymap list.
*/
if (kpos.mp_map == NULL) {
kpos.mp_map = key_map;
kpos.mp_index = 0;
}
if (process_map(c, &kpos) == FALSE) {
/*
* Send resulting output through the normal map list.
*/
kpos.mp_map = NULL;
mapthrough();
}
}
/*
* Process any characters in the input buffer through the
* cmd_map/ins_map lists into the output buffer, whence
* characters go into the editor itself.
*/
static void
mapthrough()
{
while (!flexempty(npos.mp_src)) {
if (npos.mp_map == NULL) {
if (State == NORMAL) {
npos.mp_map = cmd_map;
} else if (State == INSERT || State == REPLACE) {
npos.mp_map = ins_map;
}
npos.mp_index = 0;
}
if (process_map(flexpopch(npos.mp_src), &npos) == FALSE) {
npos.mp_map = NULL;
}
}
}
/*
* Process the given character through the maplist pointed to
* by the given position. Returns TRUE if we should continue,
* or FALSE if this attempt at mapping has terminated (either
* due to success or definite failure).
*/
static bool_t
process_map(c, pos)
register int c;
register Mpos *pos;
{
register Map *tmp;
register int ind;
ind = pos->mp_index;
for (tmp = pos->mp_map; tmp != NULL; tmp = tmp->m_next) {
if (tmp->m_lhs[ind] == c) {
if (tmp->m_lhs[ind + 1] == '\0') {
/*
* Found complete match. Insert the result into
* the appropriate output buffer, according to
* whether "remap" is set or not.
*/
(void) lformat((Pb(P_remap) && pos->mp_src != NULL) ?
pos->mp_src : pos->mp_dest, "%s", tmp->m_rhs);
return(FALSE);
} else {
/*
* Found incomplete match,
* keep going.
*/
pos->mp_map = tmp;
pos->mp_index++;
}
return(TRUE);
}
/*
* Can't move on to next map entry unless the m_same
* field is sufficient that the match so far would
* have worked.
*/
if (tmp->m_same < ind) {
break;
}
}
map_failed(pos);
/*
* Don't forget to re-stuff the character we have just received.
*/
(void) flexaddch(pos->mp_dest, c);
return(FALSE);
}
void
map_timeout()
{
if (kpos.mp_map != NULL) {
map_failed(&kpos);
mapthrough();
} else {
map_failed(&npos);
}
}
bool_t
map_waiting()
{
return(kpos.mp_map != NULL || npos.mp_map != NULL);
}
/*
* This routine is called when a timeout has occurred.
*/
static void
map_failed(pos)
Mpos *pos;
{
register char *cp;
register Flexbuf *fbp;
register int i;
if (pos->mp_map != NULL) {
fbp = pos->mp_dest;
for (i = 0, cp = pos->mp_map->m_lhs; i < pos->mp_index; i++) {
(void) flexaddch(fbp, cp[i]);
}
pos->mp_map = NULL;
}
}
/*
* Insert the key map lhs as mapping into rhs.
*/
void
xvi_keymap(lhs, rhs)
char *lhs;
char *rhs;
{
insert_map(&key_map, lhs, rhs, FALSE);
}
/*
* Insert the entry "lhs" as mapping into "rhs".
*/
void
xvi_map(argc, argv, exclam, inter)
int argc;
char *argv[];
bool_t exclam;
bool_t inter;
{
switch (argc) {
case 2: /* valid input */
if (argv[0][0] == '\0') {
if (inter) {
show_message(curwin, "Usage: map lhs rhs");
}
return;
}
insert_map(exclam ? &ins_map : &cmd_map, argv[0], argv[1], inter);
break;
case 0:
curmap = exclam ? ins_map : cmd_map;
disp_init(curwin, show_map, (int) curwin->w_ncols, FALSE);
break;
default:
if (inter) {
show_message(curwin, "Wrong number of arguments to map");
}
}
}
static void
insert_map(maplist, left, right, interactive)
Map **maplist;
char *left;
char *right;
bool_t interactive;
{
char *lhs; /* saved lhs of map */
char *rhs; /* saved rhs of map */
Map *mptr; /* new map element */
Map **p; /* used for loop to find position */
int rel;
lhs = strsave(left);
if (lhs == NULL || (rhs = strsave(right)) == NULL) {
if (interactive) {
show_message(curwin, "no memory for that map");
}
return;
}
mptr = (Map *) alloc(sizeof(Map));
if (mptr == NULL) {
free(lhs);
free(rhs);
return;
}
mptr->m_lhs = lhs;
mptr->m_rhs = rhs;
p = maplist;
if ((*p) == NULL || strcmp((*p)->m_lhs, lhs) > 0) {
/*
* Either there are no maps yet, or the one
* we want to enter should go at the start.
*/
mptr->m_next = *p;
*p = mptr;
calc_same(mptr);
} else if (strcmp((*p)->m_lhs, lhs) == 0) {
/*
* We need to replace the rhs of the first map.
*/
free(lhs);
free((char *) mptr);
free((*p)->m_rhs);
(*p)->m_rhs = rhs;
calc_same(*p);
} else {
for ( ; (*p) != NULL; p = &((*p)->m_next)) {
/*
* Set "rel" to +ve if the next element is greater
* than the current one, -ve if it is less, or 0
* if they are the same (if the lhs is the same).
*/
rel = ((*p)->m_next == NULL) ? 1 :
strcmp((*p)->m_next->m_lhs, lhs);
if (rel >= 0) {
if (rel > 0) {
/*
* The right place to insert
* the new map.
*/
mptr->m_next = (*p)->m_next;
(*p)->m_next = mptr;
calc_same(*p);
calc_same(mptr);
} else /* rel == 0 */ {
/*
* The lhs of the new map is identical
* to that of an existing map.
* Replace the old rhs with the new.
*/
free(lhs);
free((char *) mptr);
mptr = (*p)->m_next;
free(mptr->m_rhs);
mptr->m_rhs = rhs;
calc_same(mptr);
}
return;
}
}
}
}
void
xvi_unmap(argc, argv, exclam, inter)
int argc;
char *argv[];
bool_t exclam;
bool_t inter;
{
int count;
if (argc < 1) {
if (inter) {
show_message(curwin, "But what do you want me to unmap?");
}
return;
}
for (count = 0; count < argc; count++) {
delete_map(exclam ? &ins_map : &cmd_map, argv[count]);
}
}
static void
delete_map(maplist, lhs)
Map **maplist;
char *lhs;
{
Map *p;
p = *maplist;
if (p != NULL && strcmp(p->m_lhs, lhs) == 0) {
*maplist = p->m_next;
} else {
for (; p != NULL; p = p->m_next) {
if (p->m_next != NULL && strcmp(lhs, p->m_next->m_lhs) == 0) {
Map *tmp;
tmp = p->m_next;
p->m_next = p->m_next->m_next;
free(tmp->m_lhs);
free(tmp->m_rhs);
free((char *) tmp);
calc_same(p);
}
}
}
}
static void
calc_same(mptr)
Map *mptr;
{
register char *a, *b;
mptr->m_same = 0;
if (mptr->m_next != NULL) {
for (a = mptr->m_lhs, b = mptr->m_next->m_lhs; *a == *b; a++, b++) {
mptr->m_same++;
}
}
}
static char *
show_map()
{
static Flexbuf buf;
/*
* Have we reached the end?
*/
if (curmap == NULL) {
return(NULL);
}
flexclear(&buf);
(void) lformat(&buf, "%-18.18s %-s", curmap->m_lhs, curmap->m_rhs);
curmap = curmap->m_next;
return flexgetstr(&buf);
}