-
Notifications
You must be signed in to change notification settings - Fork 6
/
vi.c
2262 lines (2047 loc) · 46.9 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
/* $OpenBSD: vi.c,v 1.60 2021/03/12 02:10:25 millert Exp $ */
/*
* vi command editing
* written by John Rochester (initially for nsh)
* bludgeoned to fit pdksh by Larry Bouzane, Jeff Sparkes & Eric Gisin
*
*/
#include "config.h"
#ifdef VI
#include <sys/stat.h> /* completion */
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ttydefaults.h>
#ifndef SMALL
# include <term.h>
# include <curses.h>
#endif
#include "sh.h"
#include "edit.h"
#undef CTRL
#define CTRL(x) ((x) & 0x1F) /* ASCII */
struct edstate {
char *cbuf; /* main buffer to build the command line */
int cbufsize; /* number of bytes allocated for cbuf */
int linelen; /* current number of bytes in cbuf */
int winleft; /* first byte# in cbuf to be displayed */
int cursor; /* byte# in cbuf having the cursor */
};
static int vi_hook(int);
static void vi_reset(char *, size_t);
static int nextstate(int);
static int vi_insert(int);
static int vi_cmd(int, const char *);
static int domove(int, const char *, int);
static int redo_insert(int);
static void yank_range(int, int);
static int bracktype(int);
static void save_cbuf(void);
static void restore_cbuf(void);
static void edit_reset(char *, size_t);
static int putbuf(const char *, int, int);
static void del_range(int, int);
static int findch(int, int, int, int);
static int forwword(int);
static int backword(int);
static int endword(int);
static int Forwword(int);
static int Backword(int);
static int Endword(int);
static int grabhist(int, int);
static int grabsearch(int, int, int, char *);
static void do_clear_screen(void);
static void redraw_line(int, int);
static void refresh_line(int);
static int outofwin(void);
static void rewindow(void);
static int newcol(int, int);
static void display(char *, char *, int);
static void ed_mov_opt(int, char *);
static int expand_word(int);
static int complete_word(int, int);
static int print_expansions(struct edstate *);
static int char_len(int);
static void x_vi_zotc(int);
static void vi_pprompt(int);
static void vi_error(void);
static void vi_macro_reset(void);
static int x_vi_putbuf(const char *, size_t);
static int isu8cont(unsigned char);
#define C_ 0x1 /* a valid command that isn't a M_, E_, U_ */
#define M_ 0x2 /* movement command (h, l, etc.) */
#define E_ 0x4 /* extended command (c, d, y) */
#define X_ 0x8 /* long command (@, f, F, t, T, etc.) */
#define U_ 0x10 /* an UN-undoable command (that isn't a M_) */
#define B_ 0x20 /* bad command (^@) */
#define Z_ 0x40 /* repeat count defaults to 0 (not 1) */
#define S_ 0x80 /* search (/, ?) */
#define is_bad(c) (classify[(c)&0x7f]&B_)
#define is_cmd(c) (classify[(c)&0x7f]&(M_|E_|C_|U_))
#define is_move(c) (classify[(c)&0x7f]&M_)
#define is_extend(c) (classify[(c)&0x7f]&E_)
#define is_long(c) (classify[(c)&0x7f]&X_)
#define is_undoable(c) (!(classify[(c)&0x7f]&U_))
#define is_srch(c) (classify[(c)&0x7f]&S_)
#define is_zerocount(c) (classify[(c)&0x7f]&Z_)
const unsigned char classify[128] = {
/* 0 1 2 3 4 5 6 7 */
/* 0 ^@ ^A ^B ^C ^D ^E ^F ^G */
B_, 0, 0, 0, 0, C_|U_, C_|Z_, 0,
/* 01 ^H ^I ^J ^K ^L ^M ^N ^O */
M_, C_|Z_, 0, 0, C_|U_, 0, C_, 0,
/* 02 ^P ^Q ^R ^S ^T ^U ^V ^W */
C_, 0, C_|U_, 0, 0, 0, C_, 0,
/* 03 ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
C_, 0, 0, C_|Z_, 0, 0, 0, 0,
/* 04 <space> ! " # $ % & ' */
M_, 0, 0, C_, M_, M_, 0, 0,
/* 05 ( ) * + , - . / */
0, 0, C_, C_, M_, C_, 0, C_|S_,
/* 06 0 1 2 3 4 5 6 7 */
M_, 0, 0, 0, 0, 0, 0, 0,
/* 07 8 9 : ; < = > ? */
0, 0, 0, M_, 0, C_, 0, C_|S_,
/* 010 @ A B C D E F G */
C_|X_, C_, M_, C_, C_, M_, M_|X_, C_|U_|Z_,
/* 011 H I J K L M N O */
0, C_, 0, 0, 0, 0, C_|U_, 0,
/* 012 P Q R S T U V W */
C_, 0, C_, C_, M_|X_, C_, 0, M_,
/* 013 X Y Z [ \ ] ^ _ */
C_, C_|U_, 0, 0, C_|Z_, 0, M_, C_|Z_,
/* 014 ` a b c d e f g */
0, C_, M_, E_, E_, M_, M_|X_, C_|Z_,
/* 015 h i j k l m n o */
M_, C_, C_|U_, C_|U_, M_, 0, C_|U_, 0,
/* 016 p q r s t u v w */
C_, 0, X_, C_, M_|X_, C_|U_, C_|U_|Z_,M_,
/* 017 x y z { | } ~ ^? */
C_, E_|U_, 0, 0, M_|Z_, 0, C_, 0
};
#define MAXVICMD 3
#define SRCHLEN 40
#define INSERT 1
#define REPLACE 2
#define VNORMAL 0 /* command, insert or replace mode */
#define VARG1 1 /* digit prefix (first, eg, 5l) */
#define VEXTCMD 2 /* cmd + movement (eg, cl) */
#define VARG2 3 /* digit prefix (second, eg, 2c3l) */
#define VXCH 4 /* f, F, t, T, @ */
#define VFAIL 5 /* bad command */
#define VCMD 6 /* single char command (eg, X) */
#define VREDO 7 /* . */
#define VLIT 8 /* ^V */
#define VSEARCH 9 /* /, ? */
static char undocbuf[LINE];
static struct edstate *save_edstate(struct edstate *old);
static void restore_edstate(struct edstate *old, struct edstate *new);
static void free_edstate(struct edstate *old);
static struct edstate ebuf;
static struct edstate undobuf = { undocbuf, LINE, 0, 0, 0 };
static struct edstate *es; /* current editor state */
static struct edstate *undo;
static char ibuf[LINE]; /* input buffer */
static int first_insert; /* set when starting in insert mode */
static int saved_inslen; /* saved inslen for first insert */
static int inslen; /* length of input buffer */
static int srchlen; /* number of bytes in search pattern */
static char ybuf[LINE]; /* yank buffer */
static int yanklen; /* length of yank buffer */
static int fsavecmd = ' '; /* last find command */
static int fsavech; /* character to find */
static char lastcmd[MAXVICMD]; /* last non-move command */
static int lastac; /* argcnt for lastcmd */
static int lastsearch = ' '; /* last search command */
static char srchpat[SRCHLEN]; /* last search pattern */
static int insert; /* mode: INSERT, REPLACE, or 0 */
static int hnum; /* position in history */
static int ohnum; /* history line copied (after mod) */
static int hlast; /* 1 past last position in history */
static int modified; /* buffer has been "modified" */
static int state;
/* Information for keeping track of macros that are being expanded.
* The format of buf is the alias contents followed by a null byte followed
* by the name (letter) of the alias. The end of the buffer is marked by
* a double null. The name of the alias is stored so recursive macros can
* be detected.
*/
struct macro_state {
unsigned char *p; /* current position in buf */
unsigned char *buf; /* pointer to macro(s) being expanded */
int len; /* how much data in buffer */
};
static struct macro_state macro;
enum expand_mode { NONE, EXPAND, COMPLETE, PRINT };
static enum expand_mode expanded = NONE;/* last input was expanded */
int
x_vi(char *buf, size_t len)
{
int c;
vi_reset(buf, len > LINE ? LINE : len);
vi_pprompt(1);
x_flush();
while (1) {
if (macro.p) {
c = (unsigned char)*macro.p++;
/* end of current macro? */
if (!c) {
/* more macros left to finish? */
if (*macro.p++)
continue;
/* must be the end of all the macros */
vi_macro_reset();
c = x_getc();
}
} else
c = x_getc();
if (c == -1)
break;
if (state != VLIT) {
if (c == edchars.intr || c == edchars.quit) {
/* pretend we got an interrupt */
x_vi_zotc(c);
x_flush();
trapsig(c == edchars.intr ? SIGINT : SIGQUIT);
x_mode(false);
unwind(LSHELL);
} else if (c == edchars.eof) {
if (es->linelen == 0) {
x_vi_zotc(edchars.eof);
c = -1;
break;
}
continue;
}
}
if (vi_hook(c))
break;
x_flush();
}
x_putc('\r'); x_putc('\n'); x_flush();
if (c == -1 || len <= (size_t)es->linelen)
return -1;
if (es->cbuf != buf)
memmove(buf, es->cbuf, es->linelen);
buf[es->linelen++] = '\n';
return es->linelen;
}
static int
vi_hook(int ch)
{
static char curcmd[MAXVICMD], locpat[SRCHLEN];
static int cmdlen, argc1, argc2;
switch (state) {
case VNORMAL:
if (insert != 0) {
if (ch == CTRL('v')) {
state = VLIT;
ch = '^';
}
switch (vi_insert(ch)) {
case -1:
vi_error();
state = VNORMAL;
break;
case 0:
if (state == VLIT) {
es->cursor--;
refresh_line(0);
} else
refresh_line(insert != 0);
break;
case 1:
return 1;
}
} else {
if (ch == '\r' || ch == '\n')
return 1;
cmdlen = 0;
argc1 = 0;
if (ch >= '1' && ch <= '9') {
argc1 = ch - '0';
state = VARG1;
} else {
curcmd[cmdlen++] = ch;
state = nextstate(ch);
if (state == VSEARCH) {
save_cbuf();
es->cursor = 0;
es->linelen = 0;
if (ch == '/') {
if (putbuf("/", 1, 0) != 0)
return -1;
} else if (putbuf("?", 1, 0) != 0)
return -1;
refresh_line(0);
}
}
}
break;
case VLIT:
if (is_bad(ch)) {
del_range(es->cursor, es->cursor + 1);
vi_error();
} else
es->cbuf[es->cursor++] = ch;
refresh_line(1);
state = VNORMAL;
break;
case VARG1:
if (isdigit(ch))
argc1 = argc1 * 10 + ch - '0';
else {
curcmd[cmdlen++] = ch;
state = nextstate(ch);
}
break;
case VEXTCMD:
argc2 = 0;
if (ch >= '1' && ch <= '9') {
argc2 = ch - '0';
state = VARG2;
return 0;
} else {
curcmd[cmdlen++] = ch;
if (ch == curcmd[0])
state = VCMD;
else if (is_move(ch))
state = nextstate(ch);
else
state = VFAIL;
}
break;
case VARG2:
if (isdigit(ch))
argc2 = argc2 * 10 + ch - '0';
else {
if (argc1 == 0)
argc1 = argc2;
else
argc1 *= argc2;
curcmd[cmdlen++] = ch;
if (ch == curcmd[0])
state = VCMD;
else if (is_move(ch))
state = nextstate(ch);
else
state = VFAIL;
}
break;
case VXCH:
if (ch == CTRL('['))
state = VNORMAL;
else {
curcmd[cmdlen++] = ch;
state = VCMD;
}
break;
case VSEARCH:
if (ch == '\r' || ch == '\n' /*|| ch == CTRL('[')*/ ) {
restore_cbuf();
/* Repeat last search? */
if (srchlen == 0) {
if (!srchpat[0]) {
vi_error();
state = VNORMAL;
refresh_line(0);
return 0;
}
} else {
locpat[srchlen] = '\0';
(void) strlcpy(srchpat, locpat, sizeof srchpat);
}
state = VCMD;
} else if (ch == edchars.erase || ch == CTRL('h')) {
if (srchlen != 0) {
do {
srchlen--;
es->linelen -= char_len(
(unsigned char)locpat[srchlen]);
} while (srchlen > 0 &&
isu8cont(locpat[srchlen]));
es->cursor = es->linelen;
refresh_line(0);
return 0;
}
restore_cbuf();
state = VNORMAL;
refresh_line(0);
} else if (ch == edchars.kill) {
srchlen = 0;
es->linelen = 1;
es->cursor = 1;
refresh_line(0);
return 0;
} else if (ch == edchars.werase) {
struct edstate new_es, *save_es;
int i;
int n = srchlen;
new_es.cursor = n;
new_es.cbuf = locpat;
save_es = es;
es = &new_es;
n = backword(1);
es = save_es;
for (i = srchlen; --i >= n; )
es->linelen -= char_len((unsigned char)locpat[i]);
srchlen = n;
es->cursor = es->linelen;
refresh_line(0);
return 0;
} else {
if (srchlen == SRCHLEN - 1)
vi_error();
else {
locpat[srchlen++] = ch;
if ((ch & 0x80) && Flag(FVISHOW8)) {
if (es->linelen + 2 > es->cbufsize)
vi_error();
es->cbuf[es->linelen++] = 'M';
es->cbuf[es->linelen++] = '-';
ch &= 0x7f;
}
if (ch < ' ' || ch == 0x7f) {
if (es->linelen + 2 > es->cbufsize)
vi_error();
es->cbuf[es->linelen++] = '^';
es->cbuf[es->linelen++] = ch ^ '@';
} else {
if (es->linelen >= es->cbufsize)
vi_error();
es->cbuf[es->linelen++] = ch;
}
es->cursor = es->linelen;
refresh_line(0);
}
return 0;
}
break;
}
switch (state) {
case VCMD:
state = VNORMAL;
switch (vi_cmd(argc1, curcmd)) {
case -1:
vi_error();
refresh_line(0);
break;
case 0:
if (insert != 0)
inslen = 0;
refresh_line(insert != 0);
break;
case 1:
refresh_line(0);
return 1;
case 2:
/* back from a 'v' command - don't redraw the screen */
return 1;
}
break;
case VREDO:
state = VNORMAL;
if (argc1 != 0)
lastac = argc1;
switch (vi_cmd(lastac, lastcmd)) {
case -1:
vi_error();
refresh_line(0);
break;
case 0:
if (insert != 0) {
if (lastcmd[0] == 's' || lastcmd[0] == 'c' ||
lastcmd[0] == 'C') {
if (redo_insert(1) != 0)
vi_error();
} else {
if (redo_insert(lastac) != 0)
vi_error();
}
}
refresh_line(0);
break;
case 1:
refresh_line(0);
return 1;
case 2:
/* back from a 'v' command - can't happen */
break;
}
break;
case VFAIL:
state = VNORMAL;
vi_error();
break;
}
return 0;
}
static void
vi_reset(char *buf, size_t len)
{
state = VNORMAL;
ohnum = hnum = hlast = histnum(-1) + 1;
insert = INSERT;
saved_inslen = inslen;
first_insert = 1;
inslen = 0;
modified = 1;
vi_macro_reset();
edit_reset(buf, len);
}
static int
nextstate(int ch)
{
if (is_extend(ch))
return VEXTCMD;
else if (is_srch(ch))
return VSEARCH;
else if (is_long(ch))
return VXCH;
else if (ch == '.')
return VREDO;
else if (is_cmd(ch))
return VCMD;
else
return VFAIL;
}
static int
vi_insert(int ch)
{
int tcursor;
if (ch == edchars.erase || ch == CTRL('h')) {
if (insert == REPLACE) {
if (es->cursor == undo->cursor) {
vi_error();
return 0;
}
} else {
if (es->cursor == 0) {
/* x_putc(BEL); no annoying bell here */
return 0;
}
}
tcursor = es->cursor - 1;
while(tcursor > 0 && isu8cont(es->cbuf[tcursor]))
tcursor--;
if (insert == INSERT)
memmove(es->cbuf + tcursor, es->cbuf + es->cursor,
es->linelen - es->cursor);
if (insert == REPLACE && es->cursor < undo->linelen)
memcpy(es->cbuf + tcursor, undo->cbuf + tcursor,
es->cursor - tcursor);
else
es->linelen -= es->cursor - tcursor;
if (inslen < es->cursor - tcursor)
inslen = 0;
else
inslen -= es->cursor - tcursor;
es->cursor = tcursor;
expanded = NONE;
return 0;
}
if (ch == edchars.kill) {
if (es->cursor != 0) {
inslen = 0;
memmove(es->cbuf, &es->cbuf[es->cursor],
es->linelen - es->cursor);
es->linelen -= es->cursor;
es->cursor = 0;
}
expanded = NONE;
return 0;
}
if (ch == edchars.werase) {
if (es->cursor != 0) {
tcursor = backword(1);
memmove(&es->cbuf[tcursor], &es->cbuf[es->cursor],
es->linelen - es->cursor);
es->linelen -= es->cursor - tcursor;
if (inslen < es->cursor - tcursor)
inslen = 0;
else
inslen -= es->cursor - tcursor;
es->cursor = tcursor;
}
expanded = NONE;
return 0;
}
/* If any chars are entered before escape, trash the saved insert
* buffer (if user inserts & deletes char, ibuf gets trashed and
* we don't want to use it)
*/
if (first_insert && ch != CTRL('['))
saved_inslen = 0;
switch (ch) {
case '\0':
return -1;
case '\r':
case '\n':
return 1;
case CTRL('['):
expanded = NONE;
if (first_insert) {
first_insert = 0;
if (inslen == 0) {
inslen = saved_inslen;
return redo_insert(0);
}
lastcmd[0] = 'a';
lastac = 1;
}
if (lastcmd[0] == 's' || lastcmd[0] == 'c' ||
lastcmd[0] == 'C')
return redo_insert(0);
else
return redo_insert(lastac - 1);
/* { Begin nonstandard vi commands */
case CTRL('x'):
expand_word(0);
break;
case CTRL('f'):
complete_word(0, 0);
break;
case CTRL('e'):
print_expansions(es);
break;
case CTRL('l'):
do_clear_screen();
break;
case CTRL('r'):
redraw_line(1, 0);
break;
case CTRL('i'):
if (Flag(FVITABCOMPLETE)) {
complete_word(0, 0);
break;
}
/* FALLTHROUGH */
/* End nonstandard vi commands } */
default:
if (es->linelen >= es->cbufsize - 1)
return -1;
ibuf[inslen++] = ch;
if (insert == INSERT) {
memmove(&es->cbuf[es->cursor+1], &es->cbuf[es->cursor],
es->linelen - es->cursor);
es->linelen++;
}
es->cbuf[es->cursor++] = ch;
if (insert == REPLACE && es->cursor > es->linelen)
es->linelen++;
expanded = NONE;
}
return 0;
}
static int
vi_cmd(int argcnt, const char *cmd)
{
int ncursor;
int cur, c1, c2, c3 = 0;
int any;
struct edstate *t;
if (argcnt == 0 && !is_zerocount(*cmd))
argcnt = 1;
if (is_move(*cmd)) {
if ((cur = domove(argcnt, cmd, 0)) >= 0) {
if (cur == es->linelen && cur != 0)
while (isu8cont(es->cbuf[--cur]))
continue;
es->cursor = cur;
} else
return -1;
} else {
/* Don't save state in middle of macro.. */
if (is_undoable(*cmd) && !macro.p) {
undo->winleft = es->winleft;
memmove(undo->cbuf, es->cbuf, es->linelen);
undo->linelen = es->linelen;
undo->cursor = es->cursor;
lastac = argcnt;
memmove(lastcmd, cmd, MAXVICMD);
}
switch (*cmd) {
case CTRL('l'):
do_clear_screen();
break;
case CTRL('r'):
redraw_line(1, 0);
break;
case '@':
{
static char alias[] = "_\0";
struct tbl *ap;
int olen, nlen;
char *p, *nbuf;
/* lookup letter in alias list... */
alias[1] = cmd[1];
ap = ktsearch(&aliases, alias, hash(alias));
if (!cmd[1] || !ap || !(ap->flag & ISSET))
return -1;
/* check if this is a recursive call... */
if ((p = (char *) macro.p))
while ((p = strchr(p, '\0')) && p[1])
if (*++p == cmd[1])
return -1;
/* insert alias into macro buffer */
nlen = strlen(ap->val.s) + 1;
olen = !macro.p ? 2 :
macro.len - (macro.p - macro.buf);
nbuf = alloc(nlen + 1 + olen, APERM);
memcpy(nbuf, ap->val.s, nlen);
nbuf[nlen++] = cmd[1];
if (macro.p) {
memcpy(nbuf + nlen, macro.p, olen);
afree(macro.buf, APERM);
nlen += olen;
} else {
nbuf[nlen++] = '\0';
nbuf[nlen++] = '\0';
}
macro.p = macro.buf = (unsigned char *) nbuf;
macro.len = nlen;
}
break;
case 'a':
modified = 1; hnum = hlast;
if (es->linelen != 0)
while (isu8cont(es->cbuf[++es->cursor]))
continue;
insert = INSERT;
break;
case 'A':
modified = 1; hnum = hlast;
del_range(0, 0);
es->cursor = es->linelen;
insert = INSERT;
break;
case 'S':
es->cursor = domove(1, "^", 1);
del_range(es->cursor, es->linelen);
modified = 1; hnum = hlast;
insert = INSERT;
break;
case 'Y':
cmd = "y$";
/* ahhhhhh... */
case 'c':
case 'd':
case 'y':
if (*cmd == cmd[1]) {
c1 = *cmd == 'c' ? domove(1, "^", 1) : 0;
c2 = es->linelen;
} else if (!is_move(cmd[1]))
return -1;
else {
if ((ncursor = domove(argcnt, &cmd[1], 1)) < 0)
return -1;
if (*cmd == 'c' &&
(cmd[1]=='w' || cmd[1]=='W') &&
!isspace((unsigned char)es->cbuf[es->cursor])) {
while (isspace(
(unsigned char)es->cbuf[--ncursor]))
;
ncursor++;
}
if (ncursor > es->cursor) {
c1 = es->cursor;
c2 = ncursor;
} else {
c1 = ncursor;
c2 = es->cursor;
if (cmd[1] == '%')
c2++;
}
}
if (*cmd != 'c' && c1 != c2)
yank_range(c1, c2);
if (*cmd != 'y') {
del_range(c1, c2);
es->cursor = c1;
}
if (*cmd == 'c') {
modified = 1; hnum = hlast;
insert = INSERT;
}
break;
case 'p':
modified = 1; hnum = hlast;
if (es->linelen != 0)
es->cursor++;
while (putbuf(ybuf, yanklen, 0) == 0 && --argcnt > 0)
;
if (es->cursor != 0)
es->cursor--;
if (argcnt != 0)
return -1;
break;
case 'P':
modified = 1; hnum = hlast;
any = 0;
while (putbuf(ybuf, yanklen, 0) == 0 && --argcnt > 0)
any = 1;
if (any && es->cursor != 0)
es->cursor--;
if (argcnt != 0)
return -1;
break;
case 'C':
modified = 1; hnum = hlast;
del_range(es->cursor, es->linelen);
insert = INSERT;
break;
case 'D':
yank_range(es->cursor, es->linelen);
del_range(es->cursor, es->linelen);
if (es->cursor != 0)
es->cursor--;
break;
case 'g':
if (!argcnt)
argcnt = hlast;
/* FALLTHROUGH */
case 'G':
if (!argcnt)
argcnt = 1;
else
argcnt = hlast - (source->line - argcnt);
if (grabhist(modified, argcnt - 1) < 0)
return -1;
else {
modified = 0;
hnum = argcnt - 1;
}
break;
case 'i':
modified = 1; hnum = hlast;
insert = INSERT;
break;
case 'I':
modified = 1; hnum = hlast;
es->cursor = domove(1, "^", 1);
insert = INSERT;
break;
case 'j':
case '+':
case CTRL('n'):
if (grabhist(modified, hnum + argcnt) < 0)
return -1;
else {
modified = 0;
hnum += argcnt;
}
break;
case 'k':
case '-':
case CTRL('p'):
if (grabhist(modified, hnum - argcnt) < 0)
return -1;
else {
modified = 0;
hnum -= argcnt;
}
break;
case 'r':
if (es->linelen == 0)
return -1;
modified = 1; hnum = hlast;
if (cmd[1] == 0)
vi_error();
else {
c1 = 0;
for (cur = es->cursor;
cur < es->linelen; cur++) {
if (!isu8cont(es->cbuf[cur]))
c1++;
if (c1 > argcnt)
break;
}
if (argcnt > c1)
return -1;
del_range(es->cursor, cur);
while (argcnt-- > 0)
putbuf(&cmd[1], 1, 0);
while (es->cursor > 0)
if (!isu8cont(es->cbuf[--es->cursor]))
break;
es->cbuf[es->linelen] = '\0';
}
break;
case 'R':
modified = 1; hnum = hlast;
insert = REPLACE;
break;
case 's':
if (es->linelen == 0)
return -1;
modified = 1; hnum = hlast;
for (cur = es->cursor; cur < es->linelen; cur++)
if (!isu8cont(es->cbuf[cur]))
if (argcnt-- == 0)
break;
del_range(es->cursor, cur);
insert = INSERT;
break;
case 'v':
if (es->linelen == 0 && argcnt == 0)
return -1;
if (!argcnt) {
if (modified) {
es->cbuf[es->linelen] = '\0';
source->line++;
histsave(source->line, es->cbuf, 1);
} else
argcnt = source->line + 1
- (hlast - hnum);
}
shf_snprintf(es->cbuf, es->cbufsize,
argcnt ? "%s %d" : "%s",
"fc -e ${VISUAL:-${EDITOR:-vi}} --",
argcnt);
es->linelen = strlen(es->cbuf);
return 2;
case 'x':
if (es->linelen == 0)
return -1;
modified = 1; hnum = hlast;
for (cur = es->cursor; cur < es->linelen; cur++)
if (!isu8cont(es->cbuf[cur]))
if (argcnt-- == 0)
break;
yank_range(es->cursor, cur);
del_range(es->cursor, cur);
break;
case 'X':
if (es->cursor == 0)
return -1;