-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathex.c
1368 lines (1284 loc) · 28.8 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
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
#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"
#define REG(s) ((s)[0] != '\\' ? (unsigned char) (s)[0] : 0x80 | (unsigned char) (s)[1])
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 xwa; /* writeany option */
int xhl = 1; /* syntax highlight option */
int xhll; /* highlight current line */
int xled = 1; /* use the line editor */
int xtd = 0; /* current text direction */
int xshape = 1; /* perform letter shaping */
int xorder = 1; /* change the order of characters */
int xkmap = 0; /* the current keymap */
int xkmap_alt = 1; /* the alternate keymap */
int xlim = 256; /* do not process lines longer than this */
int xru = 1; /* show line number */
int xhist = 0; /* number of history lines */
static char xkwd[EXLEN]; /* the last searched keyword */
static char xrep[EXLEN]; /* the last replacement */
static int xkwddir; /* the last search direction */
static int xgdep; /* global command recursion depth */
static char **next; /* argument list */
static int next_pos; /* position in argument list */
static struct buf {
char ft[32]; /* file type */
char *path; /* file path */
struct lbuf *lb;
int row, off, top, left;
short id; /* buffer number */
short td; /* text direction */
long mtime; /* modification time */
} bufs[16];
static int bufs_cnt = 0; /* number of allocated buffers */
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_find(char *path)
{
int i;
path = path[0] == '/' && path[1] == '\0' ? "" : path;
for (i = 0; i < LEN(bufs); i++)
if (bufs[i].path && !strcmp(bufs[i].path, path))
return i;
return -1;
}
static int bufs_findroom(void)
{
int i;
for (i = 0; i < LEN(bufs) - 1; i++)
if (!bufs[i].lb)
break;
return i;
}
static void bufs_init(int idx, char *path)
{
bufs_free(idx);
bufs[idx].id = ++bufs_cnt;
bufs[idx].path = uc_dup(path);
bufs[idx].lb = lbuf_make();
bufs[idx].row = 0;
bufs[idx].off = 0;
bufs[idx].top = 0;
bufs[idx].left = 0;
bufs[idx].td = +1;
bufs[idx].mtime = -1;
strcpy(bufs[idx].ft, syn_filetype(path));
}
static int bufs_open(char *path)
{
int idx = bufs_findroom();
path = path[0] == '/' && path[1] == '\0' ? "" : path;
bufs_init(idx, path);
return idx;
}
static void bufs_save(void)
{
bufs[0].row = xrow;
bufs[0].off = xoff;
bufs[0].top = xtop;
bufs[0].left = xleft;
bufs[0].td = xtd;
}
static void bufs_load(void)
{
xrow = bufs[0].row;
xoff = bufs[0].off;
xtop = bufs[0].top;
xleft = bufs[0].left;
xtd = bufs[0].td;
reg_put('%', bufs[0].path ? bufs[0].path : "", 0);
}
static void bufs_shift(void)
{
bufs_free(0);
memmove(&bufs[0], &bufs[1], sizeof(bufs) - sizeof(bufs[0]));
memset(&bufs[LEN(bufs) - 1], 0, sizeof(bufs[0]));
bufs_load();
}
static void bufs_switch(int idx)
{
struct buf tmp;
bufs_save();
memcpy(&tmp, &bufs[idx], sizeof(tmp));
memmove(&bufs[1], &bufs[0], sizeof(tmp) * idx);
memcpy(&bufs[0], &tmp, sizeof(tmp));
bufs_load();
}
static void bufs_number(void)
{
int n = 0;
int i;
for (i = 0; i < LEN(bufs); i++)
if (bufs[i].lb != NULL)
n++;
bufs_cnt = n;
for (i = 0; i < LEN(bufs); i++)
if (bufs[i].lb != NULL)
bufs[i].id = n--;
}
static long mtime(char *path)
{
struct stat st;
if (!stat(path, &st))
return st.st_mtime;
return -1;
}
char *ex_path(void)
{
return bufs[0].path;
}
struct lbuf *ex_lbuf(void)
{
return bufs[0].lb;
}
char *ex_filetype(void)
{
return bufs[0].ft;
}
/* replace % and # with current and alternate path names; returns a static buffer */
static char *ex_pathexpand(char *src, int spaceallowed)
{
static char buf[1024];
char *dst = buf;
char *end = dst + sizeof(buf);
while (dst + 1 < end && *src && *src != '\n' &&
(spaceallowed || (*src != ' ' && *src != '\t'))) {
if (*src == '%' || *src == '#') {
int idx = *src == '#';
if (!bufs[idx].path) {
ex_show("pathname \"%\" or \"#\" is not set");
return NULL;
}
dst += snprintf(dst, end - dst, "%s",
bufs[idx].path[0] ? bufs[idx].path : "/");
src++;
} else if (dst == buf && *src == '=') {
char *cur = bufs[0].path;
char *dir = cur != NULL ? strrchr(cur, '/') : NULL;
if (cur != NULL && dir != NULL) {
int len = MIN(dir - cur, end - dst - 2);
memcpy(dst, cur, len);
dst += len;
*dst++ = '/';
}
src++;
} else {
if (*src == '\\' && src[1])
src++;
*dst++ = *src++;
}
}
if (dst + 1 >= end)
dst = end - 1;
*dst = '\0';
return buf;
}
/* read :e +cmd arguments */
static char *ex_plus(char *src, char *dst)
{
while (*src == ' ')
src++;
*dst = '\0';
if (*src != '+')
return src;
while (*src && *src != ' ') {
if (src[0] == '\\' && src[1])
src++;
*dst++ = *src++;
}
*dst = '\0';
while (*src == ' ' || *src == '\t')
src++;
return src;
}
/* read register name */
static char *ex_reg(char *src, int *reg)
{
while (*src == ' ')
src++;
*reg = REG(src);
while (*src && *src != ' ' && *src != '\t')
src++;
return src;
}
/* the previous search keyword */
int ex_kwd(char **kwd, int *dir)
{
if (kwd)
*kwd = xkwd;
if (dir)
*dir = xkwddir;
return xkwddir == 0;
}
/* set the previous search keyword */
void ex_kwdset(char *kwd, int dir)
{
if (kwd)
snprintf(xkwd, sizeof(xkwd), "%s", kwd);
xkwddir = dir;
}
static int ex_search(char **pat)
{
char *pat_re;
struct rstr *re;
int dir, row;
int delim = **pat;
char *kw = re_read(pat);
if (kw != NULL && *kw)
ex_kwdset(kw, delim == '/' ? 1 : -1);
free(kw);
if (ex_kwd(&pat_re, &dir))
return -1;
re = rstr_make(pat_re, xic ? RE_ICASE : 0);
if (!re)
return -1;
row = xrow + dir;
while (row >= 0 && row < lbuf_len(xb)) {
if (rstr_find(re, lbuf_get(xb, row), 0, NULL, 0) >= 0)
break;
row += dir;
}
rstr_free(re);
return row >= 0 && row < lbuf_len(xb) ? row : -1;
}
static int ex_lineno(char **num)
{
int n = xrow;
switch ((unsigned char) **num) {
case '.':
++*num;
break;
case '$':
n = lbuf_len(xb) - 1;
++*num;
break;
case '\'':
if (lbuf_jump(xb, (unsigned char) *++(*num), &n, NULL))
return -1;
++*num;
break;
case '/':
case '?':
n = ex_search(num);
break;
default:
if (isdigit((unsigned char) **num)) {
n = atoi(*num) - 1;
while (isdigit((unsigned char) **num))
++*num;
}
}
while (**num == '-' || **num == '+') {
n += atoi((*num)++);
while (isdigit((unsigned char) **num))
(*num)++;
}
return n;
}
/* parse ex command addresses */
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 && *end == 0)
*beg = 0;
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 *loc, char *cmd, char *arg, char *txt);
static int ex_modifiedbuffer(char *msg)
{
if (!lbuf_modified(xb))
return 0;
if (xaw && ex_path()[0])
return ec_write("", "w", "", NULL);
if (msg)
ex_show(msg);
return 1;
}
static int ec_buffer(char *loc, char *cmd, char *arg, char *txt)
{
char *aliases = "%#^";
char ln[128];
int i;
if (!arg[0]) {
/* print buffer list */
for (i = 0; i < LEN(bufs) && bufs[i].lb; i++) {
char c = i < strlen(aliases) ? aliases[i] : ' ';
char m = lbuf_modified(bufs[i].lb) ? '*' : ' ';
snprintf(ln, LEN(ln), "%2i %c %s %c",
(int) bufs[i].id, c, bufs[i].path, m);
ex_print(ln);
}
} else if (arg[0] == '!') {
/* delete buffer */
bufs_shift();
if (bufs[0].lb == NULL)
bufs_init(0, "");
} else if (arg[0] == '~') {
/* reassign buffer ids */
bufs_number();
} else {
int id = arg[0] ? atoi(arg) : 0;
int idx = -1;
/* switch to the given buffer */
if (isdigit((unsigned char) arg[0])) { /* buffer id given */
for (idx = 0; idx < LEN(bufs); idx++)
if (bufs[idx].lb && id == bufs[idx].id)
break;
} else if (arg[0] == '-') { /* previous buffer */
for (i = 0; i < LEN(bufs); i++)
if (bufs[i].lb && bufs[i].id < bufs[0].id)
if (idx < 0 || bufs[i].id > bufs[idx].id)
idx = i;
} else if (arg[0] == '+') { /* next buffer */
for (i = 0; i < LEN(bufs); i++)
if (bufs[i].lb && bufs[i].id > bufs[0].id)
if (idx < 0 || bufs[i].id < bufs[idx].id)
idx = i;
} else { /* buffer alias given */
char *r = strchr(aliases, (unsigned char) arg[0]);
idx = r ? r - aliases : -1;
}
if (idx >= 0 && idx < LEN(bufs) && bufs[idx].lb) {
if (xb && !xwa && strchr(cmd, '!') == NULL)
if (ex_modifiedbuffer("buffer modified"))
return 1;
bufs_switch(idx);
} else {
ex_show("no such buffer");
return 1;
}
}
return 0;
}
int ex_list(char **ls, int size)
{
int i;
for (i = 0; i < LEN(bufs) && bufs[i].lb && i < size; i++)
ls[i] = bufs[i].path;
return i;
}
static int ec_quit(char *loc, char *cmd, char *arg, char *txt)
{
if (!strchr(cmd, '!'))
if (ex_modifiedbuffer("buffer modified"))
return 1;
xquit = 1;
return 0;
}
static int ec_edit(char *loc, char *cmd, char *arg, char *txt)
{
char pls[EXLEN];
char msg[128];
char *path;
int fd;
if (!strchr(cmd, '!'))
if (xb && !xwa && ex_modifiedbuffer("buffer modified"))
return 1;
arg = ex_plus(arg, pls);
if (!(path = ex_pathexpand(arg, 0)))
return 1;
/* ew: switch buffer without changing # */
if (path[0] && cmd[0] == 'e' && cmd[1] == 'w' && bufs_find(path) > 1)
bufs_switch(1);
/* check if the buffer is already available */
if (path[0] && bufs_find(path) >= 0) {
bufs_switch(bufs_find(path));
if (pls[0] == '+')
return ex_command(pls + 1);
return 0;
}
if (path[0] || !bufs[0].path)
bufs_switch(bufs_open(path));
fd = open(ex_path(), O_RDONLY);
if (fd >= 0) {
int rd = lbuf_rd(xb, fd, 0, lbuf_len(xb));
close(fd);
snprintf(msg, sizeof(msg), "\"%s\" [=%d] [r]",
ex_path(), lbuf_len(xb));
if (rd)
ex_show("read failed");
else
ex_show(msg);
}
lbuf_saved(xb, path[0] != '\0');
bufs[0].mtime = mtime(ex_path());
xrow = MAX(0, MIN(xrow, lbuf_len(xb) - 1));
xoff = 0;
xtop = MAX(0, MIN(xtop, lbuf_len(xb) - 1));
if (pls[0] == '+')
return ex_command(pls + 1);
return 0;
}
static int ex_next(char *cmd, int dis)
{
char arg[EXLEN];
int old = next_pos;
int idx = next != NULL && next[old] != NULL ? next_pos + dis : -1;
char *path = idx >= 0 && next[idx] != NULL ? next[idx] : NULL;
char *s = arg;
char *r = path != NULL ? path : "";
if (dis && path == NULL) {
ex_show("no more files");
return 1;
}
while (*r && s + 2 < arg + sizeof(arg)) {
if (*r == ' ' || *r == '%' || *r == '#' || *r == '=')
*s++ = '\\';
*s++ = *r++;
}
*s = '\0';
if (ec_edit("", cmd, arg, NULL))
return 1;
next_pos = idx;
return 0;
}
static int ec_next(char *loc, char *cmd, char *arg, char *txt)
{
return ex_next(cmd, +1);
}
static int ec_prev(char *loc, char *cmd, char *arg, char *txt)
{
return ex_next(cmd, -1);
}
static int ec_read(char *loc, char *cmd, char *arg, char *txt)
{
char msg[128];
int beg, end, pos;
char *path;
char *obuf;
int n = lbuf_len(xb);
path = arg[0] ? ex_pathexpand(arg, 1) : ex_path();
if (ex_region(loc, &beg, &end) || path == NULL)
return 1;
pos = lbuf_len(xb) ? end : 0;
if (path[0] == '!') {
if (!path[1])
return 1;
obuf = cmd_pipe(path + 1, NULL, 1);
if (obuf)
lbuf_edit(xb, obuf, pos, pos);
free(obuf);
} else {
int fd = open(path, O_RDONLY);
if (fd < 0) {
ex_show("read failed");
return 1;
}
if (lbuf_rd(xb, fd, pos, pos)) {
ex_show("read failed");
close(fd);
return 1;
}
close(fd);
}
xrow = end + lbuf_len(xb) - n - 1;
snprintf(msg, sizeof(msg), "\"%s\" [=%d] [r]",
path, lbuf_len(xb) - n);
ex_show(msg);
return 0;
}
static int ec_write(char *loc, char *cmd, char *arg, char *txt)
{
char msg[128];
char *path;
char *ibuf;
int beg, end;
path = arg[0] ? ex_pathexpand(arg, 1) : ex_path();
if (cmd[0] == 'x' && !lbuf_modified(xb))
return ec_quit("", cmd, "", NULL);
if (ex_region(loc, &beg, &end) || path == NULL)
return 1;
if (!loc[0]) {
beg = 0;
end = lbuf_len(xb);
}
if (path[0] == '!') {
if (!path[1])
return 1;
ibuf = lbuf_cp(xb, beg, end);
ex_print(NULL);
cmd_pipe(path + 1, ibuf, 0);
free(ibuf);
} else {
char *err = NULL;
int fd = -1;
if (!strchr(cmd, '!') && !strcmp(ex_path(), path) &&
mtime(ex_path()) > bufs[0].mtime) {
err = "write failed: file changed";
} else if (!strchr(cmd, '!') && arg[0] && mtime(arg) >= 0) {
err = "write failed: file exists";
} else if ((fd = open(path, O_WRONLY | O_CREAT, conf_mode())) < 0) {
err = "write failed: cannot create file";
} else if (lbuf_wr(xb, fd, beg, end) != 0 || close(fd) != 0) {
err = "write failed";
close(fd);
}
if (err != NULL) {
ex_show(err);
return 1;
}
}
snprintf(msg, sizeof(msg), "\"%s\" [=%d] [w]", path, end - beg);
ex_show(msg);
if (!ex_path()[0]) {
free(bufs[0].path);
bufs[0].path = uc_dup(path);
reg_put('%', path, 0);
}
if (!strcmp(ex_path(), path))
lbuf_saved(xb, 0);
if (!strcmp(ex_path(), path))
bufs[0].mtime = mtime(path);
if (cmd[0] == 'x' || (cmd[0] == 'w' && cmd[1] == 'q'))
ec_quit("", cmd, "", NULL);
return 0;
}
static int ec_insert(char *loc, char *cmd, char *arg, char *txt)
{
int beg, end;
int n;
if (ex_region(loc, &beg, &end) && (beg != 0 || end != 0))
return 1;
if (cmd[0] == 'a')
if (beg + 1 <= lbuf_len(xb))
beg++;
if (cmd[0] != 'c')
end = beg;
n = lbuf_len(xb);
lbuf_edit(xb, txt, beg, end);
xrow = MIN(lbuf_len(xb) - 1, end + lbuf_len(xb) - n - 1);
return 0;
}
static int ec_print(char *loc, char *cmd, char *arg, char *txt)
{
int beg, end;
int i;
if (!cmd[0] && !loc[0])
if (xrow >= lbuf_len(xb))
return 1;
if (ex_region(loc, &beg, &end))
return 1;
for (i = beg; i < end; i++)
ex_print(lbuf_get(xb, i));
xrow = MAX(beg, end - 1);
xoff = 0;
return 0;
}
static int ec_null(char *loc, char *cmd, char *arg, char *txt)
{
int beg, end;
if (!xvis) {
xrow = xrow + 1 < lbuf_len(xb) ? xrow + 1 : xrow;
return ec_print(loc, cmd, arg, txt);
}
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_rs(char *loc, char *cmd, char *arg, char *txt)
{
reg_put(REG(arg), txt, 1);
return 0;
}
static int ec_delete(char *loc, char *cmd, char *arg, char *txt)
{
int beg, end;
if (ex_region(loc, &beg, &end) || !lbuf_len(xb))
return 1;
ex_yank(REG(arg), beg, end);
lbuf_edit(xb, NULL, beg, end);
xrow = beg;
return 0;
}
static int ec_yank(char *loc, char *cmd, char *arg, char *txt)
{
int beg, end;
if (ex_region(loc, &beg, &end) || !lbuf_len(xb))
return 1;
ex_yank(REG(arg), beg, end);
return 0;
}
static int ec_put(char *loc, char *cmd, char *arg, char *txt)
{
int beg, end;
int lnmode;
char *buf;
int n = lbuf_len(xb);
buf = reg_get(REG(arg), &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 *loc, char *cmd, char *arg, char *txt)
{
char msg[128];
int beg, end;
if (ex_region(loc, &beg, &end))
return 1;
sprintf(msg, "%d\n", end);
ex_print(msg);
return 0;
}
static int ec_undo(char *loc, char *cmd, char *arg, char *txt)
{
return lbuf_undo(xb);
}
static int ec_redo(char *loc, char *cmd, char *arg, char *txt)
{
return lbuf_redo(xb);
}
static int ec_mark(char *loc, char *cmd, char *arg, char *txt)
{
int beg, end;
if (ex_region(loc, &beg, &end))
return 1;
lbuf_mark(xb, (unsigned char) arg[0], end - 1, 0);
return 0;
}
static void replace(struct sbuf *dst, char *rep, char *ln, int *offs)
{
while (rep[0]) {
if (rep[0] == '\\' && rep[1]) {
if (rep[1] >= '0' && rep[1] <= '9') {
int grp = (rep[1] - '0') * 2;
int len = offs[grp + 1] - offs[grp];
sbuf_mem(dst, ln + offs[grp], len);
} else {
sbuf_chr(dst, (unsigned char) rep[1]);
}
rep++;
} else {
sbuf_chr(dst, (unsigned char) rep[0]);
}
rep++;
}
}
static int ec_substitute(char *loc, char *cmd, char *arg, char *txt)
{
struct rstr *re;
int offs[32];
int beg, end;
char *pat = NULL, *rep = NULL;
char *s = arg;
int i;
if (ex_region(loc, &beg, &end))
return 1;
pat = re_read(&s);
if (pat && pat[0])
ex_kwdset(pat, +1);
if (pat && *s) {
s--;
rep = re_read(&s);
}
if (pat || rep)
snprintf(xrep, sizeof(xrep), "%s", rep ? rep : "");
free(pat);
free(rep);
if (ex_kwd(&pat, NULL))
return 1;
re = rstr_make(pat, xic ? RE_ICASE : 0);
if (!re)
return 1;
for (i = beg; i < end; i++) {
char *ln = lbuf_get(xb, i);
struct sbuf *r = NULL;
while (rstr_find(re, ln, LEN(offs) / 2, offs, 0) >= 0) {
if (!r)
r = sbuf_make();
sbuf_mem(r, ln, offs[0]);
replace(r, xrep, ln, offs);
ln += offs[1];
if (offs[1] <= 0) /* zero-length match */
sbuf_chr(r, (unsigned char) *ln++);
if (!*ln || *ln == '\n' || !strchr(s, 'g'))
break;
}
if (r) {
sbuf_str(r, ln);
lbuf_edit(xb, sbuf_buf(r), i, i + 1);
sbuf_free(r);
}
}
rstr_free(re);
return 0;
}
static int ec_exec(char *loc, char *cmd, char *arg, char *txt)
{
int beg, end;
char *text;
char *rep;
char *ecmd;
if (!xwa)
ex_modifiedbuffer(NULL);
if (!(ecmd = ex_pathexpand(arg, 1)))
return 1;
if (!loc[0]) {
ex_print(NULL);
return cmd_exec(ecmd);
}
if (ex_region(loc, &beg, &end))
return 1;
text = lbuf_cp(xb, beg, end);
rep = cmd_pipe(ecmd, text, 1);
if (rep)
lbuf_edit(xb, rep, beg, end);
free(text);
free(rep);
return 0;
}
static int ec_rx(char *loc, char *cmd, char *arg, char *txt)
{
char *rep, *ecmd;
int reg = 0;
if (!xwa)
ex_modifiedbuffer(NULL);
arg = ex_reg(arg, ®);
if (reg <= 0)
return 1;
if (!(ecmd = ex_pathexpand(arg, 1)))
return 1;
rep = cmd_pipe(ecmd, reg_get(reg, NULL), 1);
reg_put(reg, rep != NULL ? rep : "", 1);
free(rep);
return 0;
}
static int ec_make(char *loc, char *cmd, char *arg, char *txt)
{
char make[EXLEN];
char *target;
if (!xwa)
ex_modifiedbuffer(NULL);
if (!(target = ex_pathexpand(arg, 0)))
return 1;
sprintf(make, "make %s", target);
ex_print(NULL);
if (cmd_exec(make))
return 1;
return 0;
}
static int ec_ft(char *loc, char *cmd, char *arg, char *txt)
{
if (arg[0])
snprintf(bufs[0].ft, sizeof(bufs[0].ft), "%s", arg);
else
ex_print(ex_filetype());
return 0;
}
static int ec_cmap(char *loc, char *cmd, char *arg, char *txt)
{
if (arg[0])
xkmap_alt = conf_kmapfind(arg);
else
ex_print(conf_kmap(xkmap)[0]);
if (arg[0] && !strchr(cmd, '!'))
xkmap = xkmap_alt;
return 0;
}
static int ex_exec(char *ln);
static int ec_glob(char *loc, char *cmd, char *arg, char *txt)
{
struct rstr *re;
int offs[32];
int beg, end, not;
char *pat;
char *s = arg;
int i;
if (!loc[0] && !xgdep)
strcpy(loc, "%");
if (ex_region(loc, &beg, &end))
return 1;
not = strchr(cmd, '!') || cmd[0] == 'v';
pat = re_read(&s);
if (pat && pat[0])
ex_kwdset(pat, +1);
free(pat);
if (ex_kwd(&pat, NULL))
return 1;
if (!(re = rstr_make(pat, xic ? RE_ICASE : 0)))
return 1;
xgdep++;
for (i = beg + 1; i < end; i++)
lbuf_globset(xb, i, xgdep);
i = beg;
while (i < lbuf_len(xb)) {
char *ln = lbuf_get(xb, i);
if ((rstr_find(re, ln, LEN(offs) / 2, offs, 0) < 0) == not) {
xrow = i;
if (ex_exec(s))
break;
i = MIN(i, xrow);
}
while (i < lbuf_len(xb) && !lbuf_globget(xb, i, xgdep))
i++;
}
for (i = 0; i < lbuf_len(xb); i++)
lbuf_globget(xb, i, xgdep);
xgdep--;
rstr_free(re);
return 0;
}
#define TAGCNT 32
#define TAGLEN 128
static int tag_row[TAGCNT];
static int tag_off[TAGCNT];
static int tag_pos[TAGCNT];
static char tag_path[TAGCNT][TAGLEN];
static char tag_name[TAGCNT][TAGLEN];
static int tag_cnt = 0;
static void ex_tagput(char *name)
{
if (tag_cnt < TAGCNT) {
tag_row[tag_cnt] = xrow;
tag_off[tag_cnt] = xoff;
snprintf(tag_path[tag_cnt], TAGLEN, "%s", ex_path());
snprintf(tag_name[tag_cnt], TAGLEN, "%s", name);
tag_cnt++;
}
}
/* go to definition (dir=+1 next, dir=-1 prev, dir=0 first) */
static int tag_goto(char *cw, int dir)
{
char path[120], cmd[120];
char *s, *ln;
int pos = dir == 0 || tag_cnt == 0 ? 0 : tag_pos[tag_cnt - 1];
if (tag_find(cw, &pos, dir, path, sizeof(path), cmd, sizeof(cmd))) {
ex_show("not found");
return 1;
}
if (dir == 0)
ex_tagput(cw);
tag_pos[tag_cnt - 1] = pos;
if (strcmp(path, ex_path()) != 0) {
if (access(path, R_OK) != 0) {
ex_show("cannot open");
return 1;
}
if (ec_edit("", "e", path, NULL) != 0)
return 1;
}
xrow = 0;
xoff = 0;
ex_command(cmd);
ln = lbuf_get(xb, xrow);
if (ln && (s = strstr(ln, cw)) != NULL)
xoff = s - ln;
return 0;
}
static int ec_tag(char *loc, char *cmd, char *arg, char *txt)
{
return tag_goto(arg, 0);
}
static int ec_tfree(char *loc, char *cmd, char *arg, char *txt)
{
tag_done();
return 0;
}
static int ec_pop(char *loc, char *cmd, char *arg, char *txt)
{
if (tag_cnt > 0) {
tag_cnt--;
if (ex_path() == NULL || strcmp(tag_path[tag_cnt], ex_path()) != 0)