-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathvi.c
1977 lines (1845 loc) · 60.8 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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1982-2014 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.eclipse.org/org/documents/epl-v10.html *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* David Korn <dgkorn@gmail.com> *
* *
***********************************************************************/
// Adapted for ksh by David Korn
//
// One line editor for the shell based on the vi editor.
//
// Questions to:
// P.D. Sullivan
// cbosgd!pds
//
#include "config_ast.h" // IWYU pragma: keep
#include <ctype.h>
#include <setjmp.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>
#include "ast.h"
#include "defs.h"
#include "edit.h"
#include "history.h"
#include "sfio.h"
#include "stk.h"
#include "terminal.h"
#define MAXCHAR MAXLINE - 2 // max char per line
#define gencpy(a, b) ed_gencpy(a, b)
#define genncpy(a, b, n) ed_genncpy(a, b, n)
#define genlen(str) ed_genlen(str)
#define digit(c) ((c & ~STRIP) == 0 && isdigit(c))
#define is_print(c) ((c & ~STRIP) || isprint(c))
static_fn int _vi_isalph(int);
static_fn int _vi_isblank(int);
#define vi_isblank(v) _vi_isblank(virtual[v])
#define vi_isalph(v) _vi_isalph(virtual[v])
#define fold(c) ((c) & ~040) // lower and uppercase equivalent
#ifndef iswascii
#define iswascii(c) (((c) & ~0177) == 0)
#endif // !iswascii
typedef struct _vi_ {
int direction;
int lastmacro;
char addnl; // boolean - add newline flag
char last_find; // last find command
char last_cmd; // last command
char repeat_set;
char nonewline;
int findchar; // last find char
wchar_t *lastline;
int first_wind; // first column of window
int last_wind; // last column in window
int lastmotion; // last motion
int long_char; // line bigger than window
int long_line; // line bigger than window
int ocur_phys; // old current physical position
int ocur_virt; // old last virtual position
int ofirst_wind; // old window first col
int o_v_char; // prev virtual[ocur_virt]
int repeat; // repeat count for motion cmds
int lastrepeat; // last repeat count for motion cmds
int u_column; // undo current column
int U_saved; // original virtual saved
wchar_t *U_space; // used for U command
wchar_t *u_space; // used for u command
int bigvi;
Edit_t *ed; // pointer to edit data
} Vi_t;
#define editb (*vp->ed)
#undef putchar
#define putchar(c) ed_putchar(vp->ed, c)
#define cur_virt editb.e_cur // current virtual column
#define cur_phys editb.e_pcur // current phys column cursor is at
#define curhline editb.e_hline // current history line
#define first_virt editb.e_fcol // first allowable column
#define globals editb.e_globals // local global variables
#define histmin editb.e_hismin
#define histmax editb.e_hismax
#define last_phys editb.e_peol // last column in physical
#define last_virt editb.e_eol // last column
#define lsearch editb.e_search // last search string
#define lookahead editb.e_lookahead // characters in buffer
#define previous editb.e_lbuf // lookahead buffer
#define max_col editb.e_llimit // maximum column
#define Prompt editb.e_prompt // pointer to prompt
#define plen editb.e_plen // length of prompt
#define physical editb.e_physbuf // physical image
#define usreof editb.e_eof // user defined eof char
#define usrerase editb.e_erase // user defined erase char
#define usrlnext editb.e_lnext // user defined next literal
#define usrkill editb.e_kill // user defined kill char
#define virtual editb.e_inbuf // pointer to virtual image buffer
#define window editb.e_window // window buffer
#define w_size editb.e_wsize // window size
#define inmacro editb.e_inmacro // true when in macro
#define yankbuf editb.e_killbuf // yank/delete buffer
#define ABORT -2 // user abort
#define APPEND -10 // append chars
#define BAD -1 // failure flag
#define BIGVI -15 // user wants real vi
#define CONTROL -20 // control mode
#define ENTER -25 // enter flag
#define GOOD 0 // success flag
#define INPUT -30 // input mode
#define INSERT -35 // insert mode
#define REPLACE -40 // replace chars
#define SEARCH -45 // search flag
#define TRANSLATE -50 // translate virt to phys only
#define INVALID (-1) // invalid column
static const char paren_chars[] = "([{)]}"; // for % command
static_fn void vi_cursor(Vi_t *, int);
static_fn void vi_del_line(Vi_t *, int);
static_fn int vi_getcount(Vi_t *, int);
static_fn void vi_getline(Vi_t *, int);
static_fn int vi_getrchar(Vi_t *);
static_fn int vi_mvcursor(Vi_t *, int);
static_fn void vi_pr_string(Vi_t *, const char *);
static_fn void vi_refresh(Vi_t *, int);
static_fn void vi_replace(Vi_t *, int, int);
static_fn void vi_restore_v(Vi_t *);
static_fn void vi_save_last(Vi_t *);
static_fn void vi_save_v(Vi_t *);
static_fn int vi_search(Vi_t *, int);
static_fn void vi_sync_cursor(Vi_t *);
static_fn int vi_textmod(Vi_t *, int, int);
//
// This routine implements a one line version of vi and is called by _filbuf.c.
// If reedit is non-zero, initialize edit buffer with reedit chars.
//
int ed_viread(void *context, int fd, char *shbuf, int nchar, int reedit) {
Edit_t *ed = context;
int i; // general variable
Vi_t *vp = ed->e_vi;
char prompt[PRSIZE + 2]; // prompt
wchar_t Physical[2 * MAXLINE]; // physical image
wchar_t Ubuf[MAXLINE]; // used for U command
wchar_t ubuf[MAXLINE]; // used for u command
wchar_t Window[MAXLINE]; // window image
int Globals[9]; // local global variables
if (!vp) {
ed->e_vi = vp = calloc(1, sizeof(Vi_t));
vp->lastline = malloc(MAXLINE * CHARSIZE);
vp->direction = -1;
vp->ed = ed;
}
// Setup prompt.
Prompt = prompt;
ed_setup(vp->ed, fd, reedit);
shbuf[reedit] = 0;
// Set raw mode.
if (tty_raw(STDERR_FILENO, 0) < 0) {
return reedit ? reedit : ed_read(context, fd, shbuf, nchar, 0);
}
i = last_virt - 1;
// Initialize some things.
virtual = (wchar_t *)shbuf;
virtual = (wchar_t *)roundof((ptrdiff_t) virtual, sizeof(wchar_t));
shbuf[i + 1] = 0;
i = ed_internal(shbuf, virtual) - 1;
globals = Globals;
cur_phys = i + 1;
cur_virt = i;
first_virt = 0;
vp->first_wind = 0;
last_virt = i;
last_phys = i;
vp->last_wind = i;
vp->long_line = ' ';
vp->long_char = ' ';
vp->o_v_char = '\0';
vp->ocur_phys = 0;
vp->ocur_virt = MAXCHAR;
vp->ofirst_wind = 0;
physical = Physical;
vp->u_column = INVALID - 1;
vp->U_space = Ubuf;
vp->u_space = ubuf;
window = Window;
window[0] = '\0';
if (!yankbuf) yankbuf = malloc(MAXLINE * CHARSIZE);
if (vp->last_cmd == '\0') {
// First time for this shell.
vp->last_cmd = 'i';
vp->findchar = INVALID;
vp->lastmotion = '\0';
vp->lastrepeat = 1;
vp->repeat = 1;
*yankbuf = 0;
}
// Fiddle around with prompt length.
if (nchar + plen > MAXCHAR) nchar = MAXCHAR - plen;
max_col = nchar - 2;
// Handle usrintr, usrquit, or EOF.
i = sigsetjmp(editb.e_env, 0);
if (i != 0) {
if (vp->ed->e_multiline) {
cur_virt = last_virt;
vi_sync_cursor(vp);
}
virtual[0] = '\0';
tty_cooked(STDERR_FILENO);
if (i == UEOF) return 0; // EOF
if (i == UINTR) return -1; // interrupt
return -1;
}
// Get a line from the terminal.
vp->U_saved = 0;
if (reedit) {
cur_phys = vp->first_wind;
vp->ofirst_wind = INVALID;
vi_refresh(vp, INPUT);
}
vi_getline(vp, APPEND);
if (vp->ed->e_multiline) vi_cursor(vp, last_phys);
// Add a new line if user typed unescaped \n to cause the shell to process the line.
tty_cooked(STDERR_FILENO);
if (ed->e_nlist) {
ed->e_nlist = 0;
stkset(ed->sh->stk, ed->e_stkptr, ed->e_stkoff);
}
if (vp->addnl) {
virtual[++last_virt] = '\n';
ed_crlf(vp->ed);
}
if (++last_virt >= 0) {
if (vp->bigvi) {
vp->bigvi = 0;
shbuf[last_virt - 1] = '\n';
} else {
virtual[last_virt] = 0;
last_virt = ed_external(virtual, shbuf);
}
if (vp->ed->nhlist) ed_histlist(vp->ed, 0);
return last_virt;
}
return -1;
}
//
// This routine will append char after cur_virt in the virtual image.
// if mode =
// APPEND, shift chars right before appending
// REPLACE, replace char if possible
//
static_fn void append(Vi_t *vp, int c, int mode) {
int i, j;
if (last_virt < max_col && last_phys < max_col) {
if (mode == APPEND || (cur_virt == last_virt && last_virt >= 0)) {
j = (cur_virt >= 0 ? cur_virt : 0);
for (i = ++last_virt; i > j; --i) virtual[i] = virtual[i - 1];
}
virtual[++cur_virt] = c;
} else {
ed_ringbell();
}
return;
}
//
// This routine will position cur_virt at the nth previous word.
//
static_fn void backword(Vi_t *vp, int nwords, int cmd) {
int tcur_virt = cur_virt;
while (nwords-- && tcur_virt > first_virt) {
if (tcur_virt > first_virt && !vi_isblank(tcur_virt) && vi_isblank(tcur_virt - 1)) {
--tcur_virt;
} else if (cmd != 'B') {
int last = vi_isalph(tcur_virt - 1);
int cur = vi_isalph(tcur_virt);
if ((!cur && last) || (cur && !last)) --tcur_virt;
}
while (tcur_virt >= first_virt && vi_isblank(tcur_virt)) --tcur_virt;
if (cmd == 'B') {
while (tcur_virt >= first_virt && !vi_isblank(tcur_virt)) --tcur_virt;
} else {
if (vi_isalph(tcur_virt)) {
while (vi_isalph(tcur_virt) && tcur_virt >= first_virt) --tcur_virt;
} else {
while (tcur_virt >= first_virt && !vi_isalph(tcur_virt) && !vi_isblank(tcur_virt)) {
--tcur_virt;
}
}
}
cur_virt = ++tcur_virt;
}
return;
}
//
// This routine implements the vi command subset. The cursor will always be positioned at the char
// of interest.
//
static_fn int cntlmode(Vi_t *vp) {
int c;
int i;
wchar_t tmp_u_space[MAXLINE]; // temporary u_space
wchar_t *real_u_space; // points to real u_space
int tmp_u_column = INVALID; // temporary u_column
int was_inmacro;
if (!vp->U_saved) {
// Save virtual image if never done before.
virtual[last_virt + 1] = '\0';
gencpy(vp->U_space, virtual);
vp->U_saved = 1;
}
vi_save_last(vp);
real_u_space = vp->u_space;
curhline = histmax;
first_virt = 0;
vp->repeat = 1;
if (cur_virt > INVALID) {
// Make sure cursor is at the last char.
vi_sync_cursor(vp);
} else if (last_virt > INVALID) {
cur_virt++;
}
// Read control char until something happens to cause a return to APPEND/REPLACE mode.
while ((c = ed_getchar(vp->ed, -1))) {
vp->repeat_set = 0;
was_inmacro = inmacro;
if (c == '0') {
// Move to leftmost column.
cur_virt = 0;
vi_sync_cursor(vp);
continue;
}
if (digit(c)) {
c = vi_getcount(vp, c);
if (c == '.') vp->lastrepeat = vp->repeat;
}
// See if it's a move cursor command.
if (vi_mvcursor(vp, c)) {
vi_sync_cursor(vp);
vp->repeat = 1;
continue;
}
// See if it's a repeat of the last command.
if (c == '.') {
c = vp->last_cmd;
vp->repeat = vp->lastrepeat;
i = vi_textmod(vp, c, c);
} else {
i = vi_textmod(vp, c, 0);
}
// See if it's a text modification command.
switch (i) {
case BAD: {
break;
}
default: { // input mode
if (!was_inmacro) {
vp->last_cmd = c;
vp->lastrepeat = vp->repeat;
}
vp->repeat = 1;
if (i == GOOD) continue;
return i;
}
}
// Other stuff.
switch (c) {
case cntl('L'): {
// Redraw line. Print the prompt and force a total refresh.
if (vp->nonewline == 0 && !vp->ed->e_nocrnl) putchar('\n');
vp->nonewline = 0;
vi_pr_string(vp, Prompt);
window[0] = '\0';
cur_phys = vp->first_wind;
vp->ofirst_wind = INVALID;
vp->long_line = ' ';
break;
}
case cntl('V'): {
vi_save_v(vp);
vi_del_line(vp, BAD);
const char *p = e_version;
while ((c = *p++)) append(vp, c, APPEND);
vi_refresh(vp, CONTROL);
ed_getchar(vp->ed, -1);
vi_restore_v(vp);
ed_ungetchar(vp->ed, 'a');
break;
}
case '/': // search
case '?':
case 'N':
case 'n': {
vi_save_v(vp);
switch (vi_search(vp, c)) {
case GOOD: { // force a total refresh
window[0] = '\0';
goto newhist;
}
case BAD: { // no match
ed_ringbell();
}
// FALLTHRU
default: {
if (vp->u_column == INVALID) {
vi_del_line(vp, BAD);
} else {
vi_restore_v(vp);
}
break;
}
}
break;
}
case 'j': // get next command
case '+': {
if (vp->ed->hlist) {
if (vp->ed->hoff >= vp->ed->hmax) {
ed_ringbell();
vp->repeat = 1;
continue;
}
vp->ed->hoff++;
ed_histlist(vp->ed, *vp->ed->hlist != 0);
vp->nonewline++;
ed_ungetchar(vp->ed, cntl('L'));
continue;
}
curhline += vp->repeat;
if (curhline > histmax) {
curhline = histmax;
ed_ringbell();
vp->repeat = 1;
continue;
} else if (curhline == histmax && tmp_u_column != INVALID) {
vp->u_space = tmp_u_space;
vp->u_column = tmp_u_column;
vi_restore_v(vp);
vp->u_space = real_u_space;
break;
}
vi_save_v(vp);
cur_virt = INVALID;
goto newhist;
}
case 'k': // get previous command
case '-': {
if (vp->ed->hlist) {
if (vp->ed->hoff == 0) {
ed_ringbell();
vp->repeat = 1;
continue;
}
vp->ed->hoff--;
ed_histlist(vp->ed, *vp->ed->hlist != 0);
vp->nonewline++;
ed_ungetchar(vp->ed, cntl('L'));
continue;
}
if (curhline == histmax) {
vp->u_space = tmp_u_space;
i = vp->u_column;
vi_save_v(vp);
vp->u_space = real_u_space;
tmp_u_column = vp->u_column;
vp->u_column = i;
}
curhline -= vp->repeat;
if (curhline <= histmin) {
curhline += vp->repeat;
ed_ringbell();
vp->repeat = 1;
continue;
}
vi_save_v(vp);
cur_virt = INVALID;
newhist:
if (curhline != histmax || cur_virt == INVALID) {
hist_copy((char *)virtual, MAXLINE, curhline, -1);
} else {
strcpy((char *)virtual, (char *)vp->u_space);
ed_internal((char *)vp->u_space, vp->u_space);
}
ed_internal((char *)virtual, virtual);
if ((last_virt = genlen(virtual) - 1) >= 0 && cur_virt == INVALID) cur_virt = 0;
if (vp->ed->hlist) {
ed_histlist(vp->ed, 0);
if (c == '\n') ed_ungetchar(vp->ed, c);
ed_ungetchar(vp->ed, cntl('L'));
vp->nonewline = 1;
cur_virt = 0;
}
break;
}
case 'u': { // undo the last thing done
vi_restore_v(vp);
break;
}
case 'U': { // undo everything
vi_save_v(vp);
if (virtual[0] == '\0') {
ed_ringbell();
vp->repeat = 1;
continue;
} else {
gencpy(virtual, vp->U_space);
last_virt = genlen(vp->U_space) - 1;
cur_virt = 0;
}
break;
}
case 'v': // run command `hist -e ${VISUAL:-${EDITOR:-vi}}`
case 'G': { // fetch command from hist
if (c == 'G' || vp->repeat_set != 0) {
if (vp->repeat_set == 0) vp->repeat = histmin + 1;
if (vp->repeat <= histmin || vp->repeat > histmax) {
ed_ringbell();
vp->repeat = 1;
continue;
}
curhline = vp->repeat;
vi_save_v(vp);
if (c == 'G') {
cur_virt = INVALID;
goto newhist;
}
}
if (ed_fulledit(vp->ed) == GOOD) return BIGVI;
ed_ringbell();
vp->repeat = 1;
continue;
}
// FALLTHRU
case '#': { // insert(delete) # to (no)comment command
if (cur_virt != INVALID) {
wchar_t *p = &virtual[last_virt + 1];
*p = 0;
// See whether first char is comment char.
c = (virtual[0] == '#');
while (p-- >= virtual) {
if (p < virtual || *p == '\n') {
if (c) { // delete '#'
if (p[1] == '#') {
last_virt--;
gencpy(p + 1, p + 2);
}
} else {
cur_virt = p - virtual;
append(vp, '#', APPEND);
}
}
}
if (c) {
curhline = histmax;
cur_virt = 0;
break;
}
vi_refresh(vp, INPUT);
}
}
// FALLTHRU
case '\n': { // send to shell
if (!vp->ed->hlist) return ENTER;
}
// FALLTHRU
case '\t': { // bring choice to edit
if (vp->ed->hlist) {
if (vp->repeat > vp->ed->nhlist - vp->ed->hoff) {
ed_ringbell();
vp->repeat = 1;
continue;
}
curhline = vp->ed->hlist[vp->repeat + vp->ed->hoff - 1]->index;
goto newhist;
}
ed_ringbell();
vp->repeat = 1;
continue;
}
case ESC: { // don't ring bell if next char is '['
if (!lookahead) {
char x;
if (sfpkrd(editb.e_fd, &x, 1, '\r', 400L, -1) > 0) ed_ungetchar(vp->ed, x);
}
if (lookahead) {
ed_ungetchar(vp->ed, c = ed_getchar(vp->ed, 1));
if (c == '[' || c == 'O') {
vp->repeat = 1;
continue;
}
}
ed_ringbell();
vp->repeat = 1;
continue;
}
default: {
ed_ringbell();
vp->repeat = 1;
continue;
}
}
vi_refresh(vp, CONTROL);
vp->repeat = 1;
}
// NOTREACHED
return 0;
}
//
// This routine will position the virtual cursor at physical column x in the window.
//
static_fn void vi_cursor(Vi_t *vp, int x) {
while (physical[x] == MARKER) x++;
cur_phys = ed_setcursor(vp->ed, physical, cur_phys, x, vp->first_wind);
}
//
// Delete nchars from the virtual space and leave cur_virt positioned at cur_virt - 1.
//
// If mode =
// 'c', do not save the characters deleted.
// 'd', save them in yankbuf and delete.
// 'y', save them in yankbuf but do not delete.
//
static_fn void cdelete(Vi_t *vp, int nchars, int mode) {
int i;
wchar_t *cp;
if (cur_virt < first_virt) {
ed_ringbell();
return;
}
if (nchars <= 0) return;
cp = virtual + cur_virt;
vp->o_v_char = cp[0];
if ((cur_virt-- + nchars) > last_virt) {
// Set nchars to number actually deleted.
nchars = last_virt - cur_virt;
}
// Save characters to be deleted.
if (mode != 'c') {
i = cp[nchars];
cp[nchars] = 0;
gencpy(yankbuf, cp);
cp[nchars] = i;
}
// Now delete these characters.
if (mode != 'y') {
gencpy(cp, cp + nchars);
last_virt -= nchars;
}
}
//
// This routine will delete the line. If mode = GOOD, do a vi_save_v().
//
static_fn void vi_del_line(Vi_t *vp, int mode) {
if (last_virt == INVALID) return;
if (mode == GOOD) vi_save_v(vp);
cur_virt = 0;
first_virt = 0;
cdelete(vp, last_virt + 1, BAD);
vi_refresh(vp, CONTROL);
cur_virt = INVALID;
cur_phys = 0;
vp->findchar = INVALID;
last_phys = INVALID;
last_virt = INVALID;
vp->last_wind = INVALID;
vp->first_wind = 0;
vp->o_v_char = '\0';
vp->ocur_phys = 0;
vp->ocur_virt = MAXCHAR;
vp->ofirst_wind = 0;
window[0] = '\0';
return;
}
//
// Delete thru motion.
//
// If mode =
// 'd', save deleted characters, delete
// 'c', do not save characters, change
// 'y', save characters, yank
//
// Returns 1 if operation successful; else 0.
//
static_fn int delmotion(Vi_t *vp, int motion, int mode) {
int begin, end, delta;
if (cur_virt == INVALID) return 0;
if (mode != 'y') vi_save_v(vp);
begin = cur_virt;
// Fake out the motion routines by appending a blank.
virtual[++last_virt] = ' ';
end = vi_mvcursor(vp, motion);
virtual[last_virt--] = 0;
if (!end) return 0;
end = cur_virt;
if (mode == 'c' && end > begin && strchr("wW", motion)) {
// Called by change operation, user really expects the effect of the eE commands, so back up
// to end of word.
while (end > begin && vi_isblank(end - 1)) --end;
if (end == begin) ++end;
}
delta = end - begin;
if (delta >= 0) {
cur_virt = begin;
if (strchr("eE;,TtFf%", motion)) ++delta;
} else {
delta = -delta + (motion == '%');
}
cdelete(vp, delta, mode);
if (mode == 'y') cur_virt = begin;
return 1;
}
//
// This routine will move cur_virt to the end of the nth word.
//
static_fn void endword(Vi_t *vp, int nwords, int cmd) {
int tcur_virt = cur_virt;
while (nwords--) {
if (tcur_virt <= last_virt && !vi_isblank(tcur_virt)) ++tcur_virt;
while (tcur_virt <= last_virt && vi_isblank(tcur_virt)) ++tcur_virt;
if (cmd == 'E') {
while (tcur_virt <= last_virt && !vi_isblank(tcur_virt)) ++tcur_virt;
} else {
if (vi_isalph(tcur_virt)) {
while (tcur_virt <= last_virt && vi_isalph(tcur_virt)) ++tcur_virt;
} else {
while (tcur_virt <= last_virt && !vi_isalph(tcur_virt) && !vi_isblank(tcur_virt)) {
++tcur_virt;
}
}
}
if (tcur_virt > first_virt) tcur_virt--;
}
cur_virt = tcur_virt;
return;
}
//
// This routine will move cur_virt forward to the next nth word.
//
static_fn void forward(Vi_t *vp, int nwords, int cmd) {
int tcur_virt = cur_virt;
while (nwords--) {
if (cmd == 'W') {
while (tcur_virt < last_virt && !vi_isblank(tcur_virt)) ++tcur_virt;
} else {
if (vi_isalph(tcur_virt)) {
while (tcur_virt < last_virt && vi_isalph(tcur_virt)) ++tcur_virt;
} else {
while (tcur_virt < last_virt && !vi_isalph(tcur_virt) && !vi_isblank(tcur_virt)) {
++tcur_virt;
}
}
}
while (tcur_virt < last_virt && vi_isblank(tcur_virt)) ++tcur_virt;
}
cur_virt = tcur_virt;
return;
}
//
// Set repeat to the user typed number and return the terminating character.
//
static_fn int vi_getcount(Vi_t *vp, int c) {
int i;
// Get any repeat count.
if (c == '0') return c;
vp->repeat_set++;
i = 0;
while (digit(c)) {
i = i * 10 + c - '0';
c = ed_getchar(vp->ed, -1);
}
if (i > 0) vp->repeat *= i;
return c;
}
//
// This routine will fetch a line.
// If mode =
// APPEND, allow escape to cntlmode subroutine appending characters.
// REPLACE, allow escape to cntlmode subroutine replacing characters.
// SEARCH, no escape allowed.
// ESC, enter control mode immediately.
//
// The cursor will always be positioned after the last char printed.
//
// This routine returns when cr, nl, or (eof in column 0) is received (column 0 is the first char
// position).
//
static_fn void vi_getline(Vi_t *vp, int mode) {
int c;
int tmp;
int max_virt = 0, last_save = 0;
wchar_t saveline[MAXLINE];
vp->addnl = 1;
if (mode == ESC) {
// Go directly to control mode.
goto escape;
}
for (;;) {
if ((c = ed_getchar(vp->ed, mode == SEARCH ? 1 : -2)) == usreof) {
c = UEOF;
} else if (c == usrerase) {
c = UERASE;
} else if (c == usrkill) {
c = UKILL;
} else if (c == editb.e_werase) {
c = UWERASE;
} else if (c == usrlnext) {
c = ULNEXT;
} else if (mode == SEARCH && c == editb.e_intr) {
c = UINTR;
}
if (c == ULNEXT) {
// Implement ^V to escape next char.
c = ed_getchar(vp->ed, 2);
append(vp, c, mode);
vi_refresh(vp, INPUT);
continue;
}
if (c != '\t') vp->ed->e_tabcount = 0;
switch (c) {
case ESC: { // enter control mode
if (!sh_isoption(vp->ed->sh, SH_VI)) {
append(vp, c, mode);
break;
}
if (mode == SEARCH) {
ed_ringbell();
continue;
} else {
escape:
if (mode == REPLACE) {
c = max_virt - cur_virt;
if (c > 0 && last_save >= cur_virt) {
genncpy((&virtual[cur_virt]), &saveline[cur_virt], c);
if (last_virt >= last_save) last_virt = last_save - 1;
vi_refresh(vp, INPUT);
}
--cur_virt;
}
tmp = cntlmode(vp);
if (tmp == ENTER || tmp == BIGVI) {
vp->bigvi = (tmp == BIGVI);
return;
}
if (tmp == INSERT) {
mode = APPEND;
continue;
}
mode = tmp;
if (mode == REPLACE) {
c = last_save = last_virt + 1;
if (c >= MAXLINE) c = MAXLINE - 1;
genncpy(saveline, virtual, c);
}
}
break;
}
case UINTR: {
first_virt = 0;
cdelete(vp, cur_virt + 1, BAD);
cur_virt = -1;
return;
}
case UERASE: // user erase char -- treat as backspace
case '\b': { // backspace
if (cur_virt >= 0 && virtual[cur_virt] == '\\') {
cdelete(vp, 1, BAD);
append(vp, usrerase, mode);
} else {
if (mode == SEARCH && cur_virt == 0) {
first_virt = 0;
cdelete(vp, 1, BAD);
return;
}
if (mode == REPLACE || (last_save > 0 && last_virt <= last_save)) {
if (cur_virt <= first_virt) {
ed_ringbell();
} else if (mode == REPLACE) {
--cur_virt;
}
mode = REPLACE;
vi_sync_cursor(vp);
continue;
} else {
cdelete(vp, 1, BAD);
}
}
break;
}
case UWERASE: { // delete back word
if (cur_virt > first_virt && !vi_isblank(cur_virt) && !ispunct(virtual[cur_virt]) &&
vi_isblank(cur_virt - 1)) {
cdelete(vp, 1, BAD);
} else {
tmp = cur_virt;
backword(vp, 1, 'W');
cdelete(vp, tmp - cur_virt + 1, BAD);
}
break;
}
case UKILL: { // user kill line char
if (virtual[cur_virt] == '\\') {
cdelete(vp, 1, BAD);
append(vp, usrkill, mode);
} else {
if (mode == SEARCH) {
cur_virt = 1;
delmotion(vp, '$', BAD);
} else if (first_virt) {
tmp = cur_virt;
cur_virt = first_virt;
cdelete(vp, tmp - cur_virt + 1, BAD);
} else {
vi_del_line(vp, GOOD);
}
}
break;
}
case UEOF: { // eof char
if (cur_virt != INVALID) continue;
vp->addnl = 0;
}
// FALLTHRU
case '\n': { // newline or return