-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvi.c
614 lines (517 loc) · 10.3 KB
/
vi.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/* SC A Spreadsheet Calculator
*
* One line vi emulation
* $Revision: 6.8 $
*/
#include <signal.h>
#include <curses.h>
#ifdef BSD42
#include <strings.h>
#else
#ifndef SYSIII
#include <string.h>
#endif
#endif
#if !defined(strchr) && !defined(UPORT)
#define strchr index
#endif
extern char *strchr();
#include <stdio.h>
#include <ctype.h>
#include "sc.h"
#define istext(a) (isalnum(a) || ((a) == '_'))
extern int showrange;
extern char mode_ind; /* Mode indicator */
/* values for mode below */
#define INSERT_MODE 0 /* Insert mode */
#define EDIT_MODE 1 /* Edit mode */
#define REP_MODE 2 /* Replace mode */
#define SEARCH_MODE 3 /* Get arguments for '/' command */
static int mode = INSERT_MODE;
static char *history[HISTLEN];
static int histp = -1;
static char *last_search;
static char *undo_line;
static int undo_lim;
static char dotb[100];
static int doti = 0;
static int do_dot = 0;
void
write_line(c)
int c;
{
if (mode == EDIT_MODE) {
switch(c) {
case (ctl('h')): linelim = back_line(); break;
case (ctl('m')): cr_line(); break;
case ESC: stop_edit(); break;
case '+': for_hist(); break;
case '-': back_hist(); break;
case '$': last_col(); break;
case '.': dotcmd(); break;
case '/': search_mode(); break;
case '0': col_0(); break;
case 'D': u_save(c);del_to_end(); break;
case 'I': u_save(c);col_0();insert_mode();break;
case 'R': replace_mode(); break;
case 'X': u_save(c); back_space(); break;
case 'a': u_save(c); append_line(); break;
case 'b': linelim = back_word(); break;
case 'c': u_save(c); change_cmd(); break;
case 'd': u_save(c); delete_cmd(); break;
case 'f': linelim = find_char(); break;
case 'h': linelim = back_line(); break;
case 'i': u_save(c); insert_mode(); break;
case 'j': for_hist(); break;
case 'k': back_hist(); break;
case 'l': linelim = for_line(0); break;
case 'n': search_again(); break;
case 'q': stop_edit(); break;
case 'r': u_save(c); rep_char(); break;
case 't': linelim = to_char(); break;
case 'u': restore_it(); break;
case 'w': linelim = for_word(0); break;
case 'x': u_save(c); del_in_line(); break;
default: break;
}
} else if (mode == INSERT_MODE) {
savedot(c);
switch(c) {
case (ctl('h')): back_space(); break;
case (ctl('m')): cr_line(); break;
case ESC: edit_mode(); break;
default: ins_in_line(c); break;
}
} else if (mode == SEARCH_MODE) {
switch(c) {
case (ctl('h')): back_space(); break;
case (ctl('m')): search_hist(); break;
case ESC: edit_mode(); break;
default: ins_in_line(c); break;
}
} else if (mode == REP_MODE) {
savedot(c);
switch(c) {
case (ctl('h')): back_space(); break;
case (ctl('m')): cr_line(); break;
case ESC: edit_mode(); break;
default: replace_in_line(c); break;
}
}
}
edit_mode()
{
mode = EDIT_MODE;
mode_ind = 'e';
histp = -1;
if (line[linelim] == '\0')
linelim = back_line();
}
void
insert_mode()
{
mode_ind = 'i';
mode = INSERT_MODE;
}
search_mode()
{
line[0] = '/';
line[1] = 0;
linelim = 1;
histp = -1;
mode_ind = '/';
mode = SEARCH_MODE;
}
replace_mode()
{
mode_ind = 'R';
mode = REP_MODE;
}
/* dot command functions. Saves info so we can redo on a '.' command */
savedot(c)
int c;
{
if (do_dot)
return;
dotb[doti++] = c;
dotb[doti] = 0;
}
dotcmd()
{
int c;
do_dot = 1;
doti = 0;
while(dotb[doti] != 0) {
c = dotb[doti++];
write_line(c);
}
do_dot = 0;
doti = 0;
}
vigetch()
{
int c;
if(do_dot) {
if (dotb[doti] != 0) {
return(dotb[doti++]);
} else {
do_dot = 0;
doti = 0;
return(nmgetch());
}
}
c = nmgetch();
savedot(c);
return(c);
}
/* saves the current line for possible use by an undo cmd */
u_save(c)
int c;
{
if (undo_line) {
xfree(undo_line);
undo_line = 0;
}
undo_line = strcpy(xmalloc((unsigned)(strlen(line)+1)), line);
undo_lim = linelim;
/* reset dot command if not processing it. */
if (!do_dot) {
doti = 0;
savedot(c);
}
}
/* Restores the current line saved by u_save() */
restore_it()
{
register char *tempc;
register int tempi;
if (!undo_line)
return;
tempc = strcpy(xmalloc((unsigned)(strlen(line)+1)), line);
tempi = linelim;
strcpy(line, undo_line);
linelim = undo_lim;
xfree(undo_line);
undo_line = tempc;
undo_lim = tempi;
}
/* This command stops the editing process. */
stop_edit()
{
showrange = 0;
linelim = -1;
(void) move(1, 0);
(void) clrtoeol();
}
/*
* Motion commands. Forward motion commands take an argument
* which, when set, cause the forward motion to continue onto
* the null at the end of the line instead of stopping at the
* the last character of the line.
*/
for_line(stop_null)
int stop_null;
{
if (linelim >= 0 && line[linelim] != 0 &&
(line[linelim+1] != 0 || stop_null))
return(linelim+1);
else
return(linelim);
}
for_word(stop_null)
int stop_null;
{
register int c;
register int cpos;
cpos = linelim;
if (line[cpos] == ' ') {
while (line[cpos] == ' ')
cpos++;
if (cpos > 0 && line[cpos] == 0)
--cpos;
return(cpos);
}
if (istext(line[cpos])) {
while ((c = line[cpos]) && istext(c))
cpos++;
} else {
while ((c = line[cpos]) && !istext(c) && c != ' ')
cpos++;
}
while (line[cpos] == ' ')
cpos++;
if (cpos > 0 && line[cpos] == 0 && !stop_null)
--cpos;
return(cpos);
}
back_line()
{
if (linelim)
return(linelim-1);
else
return(0);
}
back_word()
{
register int c;
register int cpos;
cpos = linelim;
if (line[cpos] == ' ') {
/* Skip white space */
while (cpos > 0 && line[cpos] == ' ')
--cpos;
} else if (cpos > 0 && (line[cpos-1] == ' '
|| istext(line[cpos]) && !istext(line[cpos-1])
|| !istext(line[cpos]) && istext(line[cpos-1]))) {
/* Started on the first char of a word - back up to prev. word */
--cpos;
while (cpos > 0 && line[cpos] == ' ')
--cpos;
}
/* Skip across the word - goes 1 too far */
if (istext(line[cpos])) {
while (cpos > 0 && (c = line[cpos]) && istext(c))
--cpos;
} else {
while (cpos > 0 && (c = line[cpos]) && !istext(c) && c != ' ')
--cpos;
}
/* We are done - fix up the one too far */
if (cpos > 0 && line[cpos] && line[cpos+1])
cpos++;
return(cpos);
}
/* Text manipulation commands */
del_in_line()
{
register int len, i;
if (linelim >= 0) {
len = strlen(line);
if (linelim == len && linelim > 0)
linelim--;
for (i = linelim; i < len; i++)
line[i] = line[i+1];
}
if (linelim > 0 && line[linelim] == 0)
--linelim;
}
ins_in_line(c)
int c;
{
register int i, len;
len = strlen(line);
for (i = len; i >= linelim; --i)
line[i+1] = line[i];
line[linelim++] = c;
line[len+1] = 0;
}
void
ins_string(s)
char *s;
{
while (*s)
ins_in_line(*s++);
}
append_line()
{
register int i;
i = linelim;
if (i >= 0 && line[i])
linelim++;
insert_mode();
}
rep_char()
{
int c;
c = vigetch();
if (line[linelim] != 0) {
line[linelim] = c;
} else {
line[linelim] = c;
line[linelim+1] = 0;
}
}
replace_in_line(c)
{
register int len;
len = strlen(line);
line[linelim++] = c;
if (linelim > len)
line[linelim] = 0;
}
back_space()
{
if (linelim == 0)
return;
if (line[linelim] == 0) {
linelim = back_line();
del_in_line();
linelim = strlen(line);
} else {
linelim = back_line();
del_in_line();
}
}
get_motion()
{
int c;
c = vigetch();
switch (c) {
case 'b': return(back_word());
case 'f': return(find_char()+1);
case 'h': return(back_line());
case 'l': return(for_line(1));
case 't': return(to_char()+1);
case 'w': return(for_word(1));
default: return(linelim);
}
}
delete_cmd()
{
int cpos;
cpos = get_motion();
del_chars(cpos, linelim);
}
change_cmd()
{
delete_cmd();
insert_mode();
}
del_chars(first, last)
register int first, last;
{
int temp;
if (first == last)
return;
if (last < first) {
temp = last; last = first; first = temp;
}
linelim = first;
while(first < last) {
del_in_line();
--last;
}
}
del_to_end()
{
if (linelim < 0)
return;
line[linelim] = 0;
linelim = back_line();
}
cr_line()
{
showrange = 0;
insert_mode();
save_hist();
linelim = 0;
(void) yyparse ();
linelim = -1;
}
/* History functions */
save_hist()
{
register int i;
/* free the oldest one */
if (history[HISTLEN-1]) {
xfree(history[HISTLEN-1]);
history[HISTLEN-1] = 0;
}
/* Move the others back */
for (i = HISTLEN-1; i > 0; --i)
history[i] = history[i-1];
history[0] = xmalloc((unsigned) strlen(line)+1);
strcpy(history[0], line);
}
back_hist()
{
if (histp == -1 || histp < HISTLEN-1 && history[histp + 1])
histp++;
if (history[histp]) {
strcpy(line, history[histp]);
linelim = 0;
} else
line[linelim = 0] = 0;
}
search_hist()
{
if (last_search) {
xfree(last_search);
last_search = 0;
}
if(linelim < 1) {
linelim = 0;
edit_mode();
return;
}
last_search = strcpy(xmalloc((unsigned)(strlen(line+1)+1)), line+1);
search_again();
mode = EDIT_MODE;
}
search_again()
{
int found_it;
int do_next;
int prev_histp;
char *look_here;
prev_histp = histp;
if (!last_search)
return;
do {
back_hist();
if (prev_histp == histp)
break;
prev_histp = histp;
look_here = line;
found_it = do_next = 0;
while ((look_here = strchr(look_here, last_search[0])) &&
!found_it && !do_next) {
if (strncmp(look_here, last_search, strlen(last_search)) == 0)
found_it++;
else if (look_here < line + strlen(line) - 1)
look_here++;
else
do_next++;
}
} while (!found_it);
}
for_hist()
{
if (histp > 0)
histp--;
if (histp >= 0 && history[histp]) {
strcpy(line, history[histp]);
linelim = 0;
} else
line[linelim = 0] = 0;
}
col_0()
{
linelim = 0;
}
last_col()
{
linelim = strlen(line);
if (linelim > 0)
--linelim;
}
find_char()
{
register int c;
register int i;
c = vigetch();
i = linelim;
while(line[i] && line[i] != c)
i++;
if (!line[i])
i = linelim;
return(i);
}
to_char()
{
register int i;
i = find_char();
if (i > 0 && i != linelim)
--i;
return(i);
}