/
ex.c
868 lines (816 loc) · 16 KB
/
ex.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
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "vi.h"
int xrow, xoff, xtop; /* current row, column, and top row */
int xleft; /* the first visible column */
int xquit; /* exit if set */
int xvis; /* visual mode */
int xai = 1; /* autoindent option */
int xic = 1; /* ignorecase option */
int xaw; /* autowrite option */
int xhl = 1; /* syntax highlight option */
int xled = 1; /* use the line editor */
int xdir = +1; /* current direction context */
int xshape = 1; /* perform letter shaping */
int xorder = 1; /* change the order of characters */
char xfindkwd[EXLEN]; /* the last searched keyword */
int xfinddir = +1; /* the last search direction */
static char *xkmap = "en"; /* the current keymap */
static char xkmap2[8] = "fa"; /* the alternate keymap */
static struct buf {
char ft[32];
char *path;
struct lbuf *lb;
int row, off;
} bufs[8];
static int bufs_find(char *path)
{
int i;
for (i = 0; i < LEN(bufs); i++)
if (bufs[i].lb && !strcmp(bufs[i].path, path))
return i;
return -1;
}
static void bufs_free(int idx)
{
if (bufs[idx].lb) {
free(bufs[idx].path);
lbuf_free(bufs[idx].lb);
memset(&bufs[idx], 0, sizeof(bufs[idx]));
}
}
static int bufs_open(char *path)
{
int i;
for (i = 0; i < LEN(bufs) - 1; i++)
if (!bufs[i].lb)
break;
bufs_free(i);
bufs[i].path = uc_dup(path);
bufs[i].lb = lbuf_make();
bufs[i].row = 0;
bufs[i].off = 0;
strcpy(bufs[i].ft, syn_filetype(path));
return i;
}
static void bufs_swap(int i, int j)
{
struct buf tmp;
if (i == j)
return;
memcpy(&tmp, &bufs[i], sizeof(tmp));
memcpy(&bufs[i], &bufs[j], sizeof(tmp));
memcpy(&bufs[j], &tmp, sizeof(tmp));
}
static void bufs_switch(int idx)
{
if (idx > 1)
bufs_swap(0, 1);
bufs_swap(0, idx);
xrow = bufs[0].row;
xoff = bufs[0].off;
}
char *ex_path(void)
{
return bufs[0].path;
}
struct lbuf *ex_lbuf(void)
{
return bufs[0].lb;
}
char *ex_filetype(void)
{
return xhl ? bufs[0].ft : "";
}
char **ex_kmap(void)
{
return &xkmap;
}
char *ex_kmapalt(void)
{
return xkmap2;
}
/* read ex command location */
static char *ex_loc(char *s, char *loc)
{
while (*s == ':' || isspace((unsigned char) *s))
s++;
while (*s && !isalpha((unsigned char) *s) && *s != '=' && *s != '!') {
if (*s == '\'')
*loc++ = *s++;
if (*s == '/' || *s == '?') {
int d = *s;
*loc++ = *s++;
while (*s && *s != d) {
if (*s == '\\' && s[1])
*loc++ = *s++;
*loc++ = *s++;
}
}
if (*s)
*loc++ = *s++;
}
*loc = '\0';
return s;
}
/* read ex command name */
static char *ex_cmd(char *s, char *cmd)
{
char *cmd0 = cmd;
s = ex_loc(s, cmd);
while (isspace((unsigned char) *s))
s++;
while (isalpha((unsigned char) *s))
if ((*cmd++ = *s++) == 'k' && cmd == cmd0 + 1)
break;
if (*s == '!' || *s == '=')
*cmd++ = *s++;
*cmd = '\0';
return s;
}
/* read ex command argument */
static char *ex_arg(char *s, char *arg)
{
s = ex_cmd(s, arg);
while (isspace((unsigned char) *s))
s++;
while (*s && !isspace((unsigned char) *s))
*arg++ = *s++;
*arg = '\0';
return s;
}
static char *ex_argeol(char *ec)
{
char arg[EXLEN];
char *s = ex_cmd(ec, arg);
while (isspace((unsigned char) *s))
s++;
return s;
}
static char *ex_line(char *s, char *ln)
{
while (*s && *s != '|' && *s != '\n')
*ln++ = *s++;
*ln = '\0';
return *s ? s + 1 : s;
}
static int ex_search(char *pat)
{
struct sbuf *kw;
int dir = *pat == '/' ? 1 : -1;
char *b = pat;
char *e = b;
char *pats[1];
struct rset *re;
int row;
kw = sbuf_make();
while (*++e) {
if (*e == *pat)
break;
sbuf_chr(kw, (unsigned char) *e);
if (*e == '\\' && e[1])
e++;
}
if (sbuf_len(kw))
snprintf(xfindkwd, sizeof(xfindkwd), "%s", sbuf_buf(kw));
sbuf_free(kw);
if (!xfindkwd[0])
return xrow;
xfinddir = dir;
pats[0] = xfindkwd;
re = rset_make(1, pats, xic ? RE_ICASE : 0);
if (!re)
return 1;
row = xrow + dir;
while (row >= 0 && row < lbuf_len(xb)) {
if (rset_find(re, lbuf_get(xb, row), 0, NULL, 0) >= 0)
break;
row += dir;
}
rset_free(re);
return row < 0 || row >= lbuf_len(xb) ? xrow : row;
}
static int ex_lineno(char *num)
{
int n = xrow;
if (!num[0] || num[0] == '.')
n = xrow;
if (isdigit(num[0]))
n = atoi(num) - 1;
if (num[0] == '$')
n = lbuf_len(xb) - 1;
if (num[0] == '-')
n = xrow - (num[1] ? ex_lineno(num + 1) : 1);
if (num[0] == '+')
n = xrow + (num[1] ? ex_lineno(num + 1) : 1);
if (num[0] == '\'')
lbuf_jump(xb, num[1], &n, NULL);
if (num[0] == '/' && num[1])
n = ex_search(num);
if (num[0] == '?' && num[1])
n = ex_search(num);
return n;
}
/* parse ex command location */
static int ex_region(char *loc, int *beg, int *end)
{
int naddr = 0;
if (!strcmp("%", loc)) {
*beg = 0;
*end = MAX(0, lbuf_len(xb));
return 0;
}
if (!*loc) {
*beg = xrow;
*end = xrow == lbuf_len(xb) ? xrow : xrow + 1;
return 0;
}
while (*loc) {
int end0 = *end;
*end = ex_lineno(loc) + 1;
*beg = naddr++ ? end0 - 1 : *end - 1;
if (!naddr++)
*beg = *end - 1;
while (*loc && *loc != ';' && *loc != ',')
loc++;
if (!*loc)
break;
if (*loc == ';')
xrow = *end - 1;
loc++;
}
if (*beg < 0 || *beg >= lbuf_len(xb))
return 1;
if (*end < *beg || *end > lbuf_len(xb))
return 1;
return 0;
}
static int ec_write(char *ec);
static int ex_modifiedbuffer(char *msg)
{
if (!lbuf_modified(xb))
return 0;
if (xaw && ex_path()[0])
return ec_write("w");
if (msg)
ex_show(msg);
return 1;
}
static int ec_quit(char *ec)
{
char cmd[EXLEN];
ex_cmd(ec, cmd);
if (!strchr(cmd, '!'))
if (ex_modifiedbuffer("buffer modified\n"))
return 1;
xquit = 1;
return 0;
}
static int ex_expand(char *d, char *s)
{
while (*s) {
int c = (unsigned char) *s++;
if (c == '%') {
if (!bufs[0].path || !bufs[0].path[0]) {
ex_show("\"%\" is unset\n");
return 1;
}
strcpy(d, bufs[0].path);
d = strchr(d, '\0');
continue;
}
if (c == '#') {
if (!bufs[1].path || !bufs[1].path[0]) {
ex_show("\"#\" is unset\n");
return 1;
}
strcpy(d, bufs[1].path);
d = strchr(d, '\0');
continue;
}
if (c == '\\' && (*s == '%' || *s == '#'))
c = *s++;
*d++ = c;
}
*d = '\0';
return 0;
}
static int ec_edit(char *ec)
{
char msg[128];
char arg[EXLEN], cmd[EXLEN];
char path[EXLEN];
int fd;
ex_cmd(ec, cmd);
ex_arg(ec, arg);
if (!strchr(cmd, '!'))
if (xb && ex_modifiedbuffer("buffer modified\n"))
return 1;
if (ex_expand(path, arg))
return 1;
bufs[0].row = xrow;
bufs[0].off = xoff;
if (arg[0] && bufs_find(path) >= 0) {
bufs_switch(bufs_find(path));
return 0;
}
if (path[0] || !bufs[0].path)
bufs_switch(bufs_open(path));
fd = open(ex_path(), O_RDONLY);
if (fd >= 0) {
lbuf_rd(xb, fd, 0, lbuf_len(xb));
close(fd);
snprintf(msg, sizeof(msg), "\"%s\" %d lines [r]\n",
ex_path(), lbuf_len(xb));
ex_show(msg);
}
xrow = xvis ? 0 : MAX(0, MIN(xrow, lbuf_len(xb) - 1));
lbuf_saved(xb, path[0] != '\0');
return 0;
}
static int ec_read(char *ec)
{
char arg[EXLEN], loc[EXLEN];
char msg[128];
int beg, end;
char *path;
char *obuf;
int n = lbuf_len(xb);
ex_arg(ec, arg);
ex_loc(ec, loc);
path = arg[0] ? arg : ex_path();
if (ex_region(loc, &beg, &end))
return 1;
if (arg[0] == '!') {
int pos = MIN(xrow + 1, lbuf_len(xb));
if (ex_expand(arg, ex_argeol(ec)))
return 1;
obuf = cmd_pipe(arg + 1, NULL, 0, 1);
if (obuf)
lbuf_edit(xb, obuf, pos, pos);
free(obuf);
} else {
int fd = open(path, O_RDONLY);
int pos = lbuf_len(xb) ? end : 0;
if (fd < 0) {
ex_show("read failed\n");
return 1;
}
lbuf_rd(xb, fd, pos, pos);
close(fd);
}
xrow = end + lbuf_len(xb) - n - 1;
snprintf(msg, sizeof(msg), "\"%s\" %d lines [r]\n",
path, lbuf_len(xb) - n);
ex_show(msg);
return 0;
}
static int ec_write(char *ec)
{
char cmd[EXLEN], arg[EXLEN], loc[EXLEN];
char msg[128];
char *path;
char *ibuf;
int beg, end;
ex_cmd(ec, cmd);
ex_arg(ec, arg);
ex_loc(ec, loc);
path = arg[0] ? arg : ex_path();
if (cmd[0] == 'x' && !lbuf_modified(xb))
return ec_quit(cmd);
if (ex_region(loc, &beg, &end))
return 1;
if (!loc[0]) {
beg = 0;
end = lbuf_len(xb);
}
if (arg[0] == '!') {
if (ex_expand(arg, ex_argeol(ec)))
return 1;
ibuf = lbuf_cp(xb, beg, end);
ex_print(NULL);
cmd_pipe(arg + 1, ibuf, 1, 0);
free(ibuf);
} else {
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fd < 0) {
ex_show("write failed\n");
return 1;
}
lbuf_wr(xb, fd, beg, end);
close(fd);
}
snprintf(msg, sizeof(msg), "\"%s\" %d lines [w]\n",
path, end - beg);
ex_show(msg);
if (!ex_path()[0]) {
free(bufs[0].path);
bufs[0].path = uc_dup(path);
}
if (!strcmp(ex_path(), path))
lbuf_saved(xb, 0);
if (cmd[0] == 'x' || (cmd[0] == 'w' && cmd[1] == 'q'))
ec_quit(cmd);
return 0;
}
static int ec_insert(char *ec)
{
char arg[EXLEN], cmd[EXLEN], loc[EXLEN];
struct sbuf *sb;
char *s;
int beg, end;
int n;
ex_arg(ec, arg);
ex_cmd(ec, cmd);
ex_loc(ec, loc);
if (ex_region(loc, &beg, &end) && (beg != 0 || end != 0))
return 1;
sb = sbuf_make();
while ((s = ex_read(""))) {
if (!strcmp(".", s)) {
free(s);
break;
}
sbuf_str(sb, s);
sbuf_chr(sb, '\n');
free(s);
}
if (cmd[0] == 'a')
if (beg + 1 <= lbuf_len(xb))
beg++;
if (cmd[0] != 'c')
end = beg;
n = lbuf_len(xb);
lbuf_edit(xb, sbuf_buf(sb), beg, end);
xrow = MIN(lbuf_len(xb) - 1, end + lbuf_len(xb) - n - 1);
sbuf_free(sb);
return 0;
}
static int ec_print(char *ec)
{
char cmd[EXLEN], loc[EXLEN];
int beg, end;
int i;
ex_cmd(ec, cmd);
ex_loc(ec, loc);
if (!cmd[0] && !loc[0]) {
if (xrow >= lbuf_len(xb) - 1)
return 1;
xrow = xrow + 1;
}
if (ex_region(loc, &beg, &end))
return 1;
for (i = beg; i < end; i++)
ex_print(lbuf_get(xb, i));
xrow = end;
xoff = 0;
return 0;
}
static int ec_null(char *ec)
{
char loc[EXLEN];
int beg, end;
if (!xvis)
return ec_print(ec);
ex_loc(ec, loc);
if (ex_region(loc, &beg, &end))
return 1;
xrow = MAX(beg, end - 1);
xoff = 0;
return 0;
}
static void ex_yank(int reg, int beg, int end)
{
char *buf = lbuf_cp(xb, beg, end);
reg_put(reg, buf, 1);
free(buf);
}
static int ec_delete(char *ec)
{
char loc[EXLEN];
char arg[EXLEN];
int beg, end;
ex_loc(ec, loc);
ex_arg(ec, arg);
if (ex_region(loc, &beg, &end) || !lbuf_len(xb))
return 1;
ex_yank(arg[0], beg, end);
lbuf_edit(xb, NULL, beg, end);
xrow = beg;
return 0;
}
static int ec_yank(char *ec)
{
char loc[EXLEN];
char arg[EXLEN];
int beg, end;
ex_loc(ec, loc);
ex_arg(ec, arg);
if (ex_region(loc, &beg, &end) || !lbuf_len(xb))
return 1;
ex_yank(arg[0], beg, end);
return 0;
}
static int ec_put(char *ec)
{
char loc[EXLEN];
char arg[EXLEN];
int beg, end;
int lnmode;
char *buf;
int n = lbuf_len(xb);
ex_loc(ec, loc);
ex_arg(ec, arg);
buf = reg_get(arg[0], &lnmode);
if (!buf || ex_region(loc, &beg, &end))
return 1;
lbuf_edit(xb, buf, end, end);
xrow = MIN(lbuf_len(xb) - 1, end + lbuf_len(xb) - n - 1);
return 0;
}
static int ec_lnum(char *ec)
{
char loc[EXLEN];
char msg[128];
int beg, end;
ex_loc(ec, loc);
if (ex_region(loc, &beg, &end))
return 1;
sprintf(msg, "%d\n", end);
ex_print(msg);
return 0;
}
static int ec_undo(char *ec)
{
return lbuf_undo(xb);
}
static int ec_redo(char *ec)
{
return lbuf_redo(xb);
}
static int ec_mark(char *ec)
{
char loc[EXLEN], arg[EXLEN];
int beg, end;
ex_arg(ec, arg);
ex_loc(ec, loc);
if (ex_region(loc, &beg, &end))
return 1;
lbuf_mark(xb, arg[0], end - 1, 0);
return 0;
}
static char *readuntil(char **src, int delim)
{
struct sbuf *sbuf = sbuf_make();
char *s = *src;
/* reading the pattern */
while (*s && *s != delim) {
if (s[0] == '\\' && s[1])
sbuf_chr(sbuf, (unsigned char) *s++);
sbuf_chr(sbuf, (unsigned char) *s++);
}
if (*s) /* skipping the delimiter */
s++;
*src = s;
return sbuf_done(sbuf);
}
static int ec_substitute(char *ec)
{
char loc[EXLEN];
struct rset *re;
int offs[32];
int beg, end;
char *pats[1];
char *pat, *rep;
char *s;
int delim;
int i;
ex_loc(ec, loc);
if (ex_region(loc, &beg, &end))
return 1;
s = ex_argeol(ec);
delim = (unsigned char) *s++;
pat = readuntil(&s, delim);
rep = readuntil(&s, delim);
if (pat[0])
snprintf(xfindkwd, sizeof(xfindkwd), "%s", pat);
free(pat);
pats[0] = xfindkwd;
re = rset_make(1, pats, xic ? RE_ICASE : 0);
if (!re) {
free(rep);
return 1;
}
for (i = beg; i < end; i++) {
char *ln = lbuf_get(xb, i);
struct sbuf *r = sbuf_make();
while (rset_find(re, ln, LEN(offs) / 2, offs, 0) >= 0) {
sbuf_mem(r, ln, offs[0]);
sbuf_str(r, rep);
ln += offs[1];
if (!strchr(s, 'g'))
break;
}
sbuf_str(r, ln);
lbuf_edit(xb, sbuf_buf(r), i, i + 1);
sbuf_free(r);
}
rset_free(re);
free(rep);
return 0;
}
static int ec_exec(char *ec)
{
char arg[EXLEN];
ex_modifiedbuffer(NULL);
if (ex_expand(arg, ex_argeol(ec)))
return 1;
ex_print(NULL);
if (cmd_exec(arg))
return 1;
return 0;
}
static int ec_make(char *ec)
{
char arg[EXLEN];
char make[EXLEN];
ex_modifiedbuffer(NULL);
if (ex_expand(arg, ex_argeol(ec)))
return 1;
sprintf(make, "make %s", arg);
ex_print(NULL);
if (cmd_exec(make))
return 1;
return 0;
}
static int ec_ft(char *ec)
{
char arg[EXLEN];
ex_arg(ec, arg);
if (arg[0])
snprintf(bufs[0].ft, sizeof(bufs[0].ft), arg);
else
ex_print(ex_filetype());
return 0;
}
static int ec_cmap(char *ec)
{
char cmd[EXLEN];
char arg[EXLEN];
ex_cmd(ec, cmd);
ex_arg(ec, arg);
if (arg[0])
snprintf(xkmap2, sizeof(xkmap2), arg);
else
ex_print(xkmap);
if (arg[0] && !strchr(cmd, '!'))
xkmap = xkmap2;
return 0;
}
static struct option {
char *abbr;
char *name;
int *var;
} options[] = {
{"ai", "autoindent", &xai},
{"aw", "autowrite", &xaw},
{"ic", "ignorecase", &xic},
{"td", "textdirection", &xdir},
{"shape", "shape", &xshape},
{"order", "xorder", &xorder},
{"hl", "highlight", &xhl},
};
static char *cutword(char *s, char *d)
{
while (isspace(*s))
s++;
while (*s && !isspace(*s))
*d++ = *s++;
while (isspace(*s))
s++;
*d = '\0';
return s;
}
static int ec_set(char *ec)
{
char arg[EXLEN];
char tok[EXLEN];
char opt[EXLEN];
char *s = arg;
int val = 0;
int i;
ex_arg(ec, arg);
if (*s) {
s = cutword(s, tok);
if (tok[0] == 'n' && tok[1] == 'o') {
strcpy(opt, tok + 2);
val = 0;
} else {
char *r = strchr(tok, '=');
if (r) {
*r = '\0';
strcpy(opt, tok);
val = atoi(r + 1);
} else {
strcpy(opt, tok);
val = 1;
}
}
for (i = 0; i < LEN(options); i++) {
struct option *o = &options[i];
if (!strcmp(o->abbr, opt) || !strcmp(o->name, opt)) {
*o->var = val;
return 0;
}
}
ex_show("unknown option");
return 1;
}
return 0;
}
static struct excmd {
char *abbr;
char *name;
int (*ec)(char *s);
} excmds[] = {
{"p", "print", ec_print},
{"a", "append", ec_insert},
{"i", "insert", ec_insert},
{"d", "delete", ec_delete},
{"c", "change", ec_insert},
{"e", "edit", ec_edit},
{"e!", "edit!", ec_edit},
{"=", "=", ec_lnum},
{"k", "mark", ec_mark},
{"pu", "put", ec_put},
{"q", "quit", ec_quit},
{"q!", "quit!", ec_quit},
{"r", "read", ec_read},
{"w", "write", ec_write},
{"wq", "wq", ec_write},
{"u", "undo", ec_undo},
{"r", "redo", ec_redo},
{"se", "set", ec_set},
{"s", "substitute", ec_substitute},
{"x", "xit", ec_write},
{"x!", "xit!", ec_write},
{"ya", "yank", ec_yank},
{"!", "!", ec_exec},
{"make", "make", ec_make},
{"ft", "filetype", ec_ft},
{"cm", "cmap", ec_cmap},
{"cm!", "cmap!", ec_cmap},
{"", "", ec_null},
};
/* execute a single ex command */
void ex_command(char *ln)
{
char ec[EXLEN];
char cmd[EXLEN];
int i;
while (*ln) {
ln = ex_line(ln, ec);
ex_cmd(ec, cmd);
for (i = 0; i < LEN(excmds); i++) {
if (!strcmp(excmds[i].abbr, cmd) ||
!strcmp(excmds[i].name, cmd)) {
excmds[i].ec(ec);
break;
}
}
if (!xvis && !cmd[0])
ec_print(ec);
}
lbuf_modified(xb);
}
/* ex main loop */
void ex(void)
{
while (!xquit) {
char *ln = ex_read(":");
if (ln)
ex_command(ln);
free(ln);
}
}
int ex_init(char **files)
{
char cmd[EXLEN];
snprintf(cmd, sizeof(cmd), "e %s", files[0] ? files[0] : "");
if (ec_edit(cmd))
return 1;
if (getenv("EXINIT"))
ex_command(getenv("EXINIT"));
return 0;
}
void ex_done(void)
{
int i;
for (i = 0; i < LEN(bufs); i++)
bufs_free(i);
}