-
Notifications
You must be signed in to change notification settings - Fork 0
/
er.c
1617 lines (1509 loc) · 29 KB
/
er.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
#ifdef __linux__
# define _XOPEN_SOURCE 600
#endif
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <locale.h>
#include <math.h>
#include <regex.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <termios.h>
#include <unistd.h>
#include <wchar.h>
#define CSI(ch) ("\x1b[" ch)
#define LEN(X) (sizeof(X) / sizeof(X[0]))
#ifndef CTRL
# define CTRL(X) ((X) & 0x1F)
#endif
/* misc constants */
enum
{
Gaplen = 256, /* number of bytes in a full gap */
Vbufmax = 4096 /* number of bytes in screen buffer */
};
/* error handling status */
enum
{
Ok,
Panic, /* Unrecoverable errors */
Reset /* Recoverable errors */
};
/* keyboard keys */
enum
{
Kesc = 0x1b,
Kbs = 0x7f, /* assume backspace sends DEL */
Kdel = 0xE000,
Kleft,
Kright,
Kup,
Kdown,
Khome,
Kend,
Kpgup,
Kpgdown,
Kins
};
/* editing mode */
enum
{
Command,
Input,
Select
};
/* undo stack type */
enum
{
Udelete,
Uinsert,
Uend /* sentinel for sequence of changes */
};
typedef struct Change Change;
typedef struct Array Array;
typedef struct Buffer Buffer;
/* textual change */
struct Change
{
short type; /* undo stack type */
size_t i; /* byte offset of change */
char c; /* value of change */
};
/* dynamic array */
struct Array
{
void *data; /* contents */
size_t size; /* element size */
size_t len; /* length */
size_t cap; /* capacity */
};
/* editing buffer */
struct Buffer
{
char *c; /* contents */
char path[PATH_MAX]; /* filename */
Array changes; /* undo stack */
short dirty; /* modified flag */
size_t *lead, addr1, addr2; /* selection offsets */
size_t cap, gap, start; /* gap book-keeping */
size_t vstart, vline; /* display book-keeping */
};
Buffer bufs[32], *buf;
Array ybuf, bbuf, dbuf;
char ch[5], vbuf[Vbufmax];
size_t vbuflen, current, nbuf;
struct winsize dim;
jmp_buf env;
const char invalid[] = "�";
short mode, refresh, quit, usetabs, tabspace, autoindent;
sigset_t oset;
volatile sig_atomic_t status;
struct termios term;
/* convert logical byte offset to internal byte offset */
size_t
bufaddr(size_t in)
{
return (in >= buf->start) ? in + buf->gap : in;
}
/* throw error */
void
err(int n)
{
status = n;
siglongjmp(env, 1);
}
/* number of decimal digits */
int
digits(long v)
{
return (v == 0) ? 1 : floor(log10((double)v)) + 1;
}
/* get current terminal dimensions */
int
dims(void)
{
if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &dim) == -1 || dim.ws_col == 0)
return -1;
return 0;
}
/* read a byte from STDIN (see terminit() for timeout) */
int
readbyte(void)
{
int n;
unsigned char c;
if((n = read(STDIN_FILENO, &c, 1)) == -1){
if(errno == EINTR)
n = 0;
else
err(Panic);
}
if(n == 0)
return -1;
return c;
}
/* read a byte stream until a complete multibyte character is found */
int
decode(char first, int (*m1)(size_t*), int (*m2)(void), size_t *i, wchar_t *wc)
{
int n, r;
mbstate_t ps;
for(n = 1, ch[0] = first; n < 5; n++){
memset(&ps, 0, sizeof(ps));
switch (mbrtowc(wc, ch, n, &ps)){
case (size_t)-2:
if((r = (m1 != NULL) ? m1(i) : m2()) == -1)
return -1;
ch[n] = r;
break;
case (size_t)-1:
return -1;
default:
return first;
}
}
return -1;
}
/* read a character from STDIN */
int
parsechar(char c)
{
if(decode(c, NULL, readbyte, NULL, NULL) == -1)
err(Reset);
return c;
}
/* get user input from keyboard */
int
key(void)
{
int k, l, m, n;
size_t i;
static const struct
{
int a;
int b;
int c;
int out;
} vt[] = {
/* VT100 */
{ 'O', 'A', -1, Kup },
{ 'O', 'B', -1, Kdown },
{ 'O', 'C', -1, Kright },
{ 'O', 'D', -1, Kleft },
/* VT220 */
{ '[', 'A', -1, Kup },
{ '[', 'B', -1, Kdown },
{ '[', 'C', -1, Kright },
{ '[', 'D', -1, Kleft },
{ '[', '1', '~', Khome },
{ '[', '2', '~', Kins },
{ '[', '3', '~', Kdel },
{ '[', '4', '~', Kend },
{ '[', '5', '~', Kpgup },
{ '[', '6', '~', Kpgdown },
/* xterm */
{ 'O', 'H', -1, Khome },
{ 'O', 'F', -1, Kend },
/* rxvt */
{ '[', '7', '~', Khome },
{ '[', '8', '~', Kend },
/* SCO ANSI */
{ '[', 'H', -1, Khome },
{ '[', 'F', -1, Kend }
};
memset(ch, 0, 5);
if((k = readbyte()) == -1)
return -1;
if(k == Kesc){
if((l = readbyte()) != -1 && (m = readbyte()) != -1){
for(i = 0; i < LEN(vt); i++){
if(l == vt[i].a && m == vt[i].b && vt[i].c == -1)
return vt[i].out;
}
if((n = readbyte()) != -1){
for(i = 0; i < LEN(vt); i++){
if(l == vt[i].a && m == vt[i].b && n == vt[i].c)
return vt[i].out;
}
}
}
return Kesc;
}
if(k == '\b' || k == Kbs)
return Kbs;
if (k <= 0x1f)
return k;
return parsechar(k);
}
/* length of current buffer */
size_t
len(void)
{
return buf->cap - buf->gap;
}
/* next byte in the current buffer */
int
nextbuf(size_t *i)
{
return (*i == len()) ? -1 : buf->c[bufaddr((*i)++)];
}
/* next character in the current buffer */
int
next(size_t *i)
{
wchar_t wc;
memset(ch, 0, 5);
if(decode(buf->c[bufaddr((*i)++)], nextbuf, NULL, i, &wc) == -1){
memcpy(ch, invalid, sizeof(invalid));
return 1;
}
if(wc == '\t')
return 8;
if(wc == '\n')
return 1;
return wcwidth(wc);
}
/* end of line in current buffer */
int
eol(size_t *i)
{
int r;
r = 0;
if(*i < len()){
do{
next(i);
r++;
}while(*i < len() && buf->c[bufaddr(*i)] != '\n');
}
return r;
}
/* previous character in current buffer */
int
prev(size_t *i)
{
size_t m, n;
int r;
n = r = 0;
if(*i > 0){
do{
n++;
m = *i - n;
r = next(&m);
}while(m > 0 && n < 4 && strncmp(ch, invalid, 5) == 0);
*i = (*i < n) ? 0 : *i - n;
}
return r;
}
/* start of line in current buffer */
int
sol(size_t *i)
{
int r;
r = 0;
if (*i > 0) {
do{
prev(i);
r++;
}while(*i > 0 && buf->c[bufaddr(*i)] != '\n');
}
return r;
}
/* next line in current buffer */
void
nextline(size_t *i)
{
size_t m, n;
m = *i;
n = sol(&m);
if(m > 0)
n--;
if(buf->c[bufaddr(*i)] != '\n')
eol(i);
if(*i < len() - 1){
next(i);
while(n && buf->c[bufaddr(*i)] != '\n' && *i < len() - 1){
next(i);
n--;
}
}
}
/* previous line in current buffer */
void
prevline(size_t *i)
{
size_t n;
if(*i > 0){
n = sol(i) - 1;
if (*i == 0)
return;
sol(i);
if(buf->c[bufaddr(*i)] == '\n')
next(i);
while(n && buf->c[bufaddr(*i)] != '\n' && *i < len() - 1){
next(i);
n--;
}
}
}
/* check if displayed subset of current buffer needs to be updated */
int
checkline(int dir)
{
int n, r;
size_t i;
n = 1;
r = 0;
if(dir){
for(i = buf->vstart; i < buf->addr2; i++){
if (buf->c[bufaddr(i)] == '\n')
n++;
}
r = n;
n -= dim.ws_row - 1;
while(n-- > 0){
nextline(&buf->vstart);
buf->vline++;
}
}else{
while(buf->addr1 < buf->vstart){
prevline(&buf->vstart);
buf->vline--;
}
}
return r;
}
/* reposition current buffer's gap ready for insertion/deletion */
void
move(size_t i)
{
size_t j, dst, src, num;
j = bufaddr(i);
if(j != buf->start + buf->gap){
dst = (j < buf->start) ? j + buf->gap : buf->start;
src = (j < buf->start) ? j : buf->start + buf->gap;
num = (j < buf->start) ? buf->start - j : j - (buf->gap + buf->start);
memmove(buf->c + dst, buf->c + src, num);
buf->start = (j < buf->start) ? j : j - buf->gap;
}
}
/* expand allocated memory for dynamic array */
void
resize(Array *a)
{
void *new;
new = realloc(a->data, a->size * 2 * a->cap);
if(new == NULL)
err(Panic);
a->data = new;
a->cap *= 2;
}
/* append new item to the end of dynamic array */
#define APPEND(A, T, E) do{ \
if((A)->len == (A)->cap) \
resize(A); \
((T *)(A)->data)[(A)->len++] = (E); \
}while(0)
/* append new textual change to the current buffer's undo stack */
void
record(short t, size_t i, char c) {
Change x = { t, i, c };
APPEND(&buf->changes, Change, x);
}
/* reallocate memory for current buffer's gap */
void
grow(void)
{
char *new;
new = realloc(buf->c, (buf->cap + Gaplen));
if(new == NULL)
err(Panic);
buf->c = new;
buf->start = buf->cap;
buf->gap = Gaplen;
buf->cap += Gaplen;
}
/* insert byte into current buffer, optionally recording on undo stack */
void
insert(size_t i, char c, int r)
{
if(buf->gap == 0)
grow(); /* please mind the gap */
move(i);
buf->c[buf->start++] = c;
buf->gap--;
buf->dirty = 1;
if(r)
record(Uinsert, i, 0);
}
/* delete byte from current buffer, optionally recording on undo stack */
void
delete(size_t i, int r)
{
char c;
move(i);
c = buf->c[buf->start + buf->gap++];
buf->dirty = 1;
if(r)
record(Udelete, i, c);
}
/* (de/in)dent selected lines in current buffer */
void
indent(size_t *a, size_t *b, int fwd)
{
int k;
size_t i;
i = *a;
if(i > 0){
sol(&i);
if(buf->c[bufaddr(i)] == '\n' && i < len() - 1)
next(&i);
}
while(i <= *b && i < len() - 1){
k = tabspace;
while(k-- > 0){
if(fwd){
if(i <= *a)
(*a)++;
insert(i, usetabs ? '\t' : ' ', 1);
(*b)++;
}else{
if(len() > 0 && i < len() &&
buf->c[bufaddr(i)] == (usetabs ? '\t' : ' ')){
delete(i, 1);
if(i < *a)
(*a)--;
(*b)--;
}
}
}
if(buf->c[bufaddr(i)] != '\n')
eol(&i);
if(buf->c[bufaddr(i)] == '\n' && i < len() - 1)
next(&i);
}
record(Uend, 0, 0);
}
/* insert newline and automatically match previous indentation */
void
newline(size_t *a, int O)
{
size_t b;
b = *a;
if(O && *a == 0){
insert(0, '\n', 1);
return;
}
insert((*a)++, '\n', 1);
if(autoindent){
sol(&b);
if(buf->c[bufaddr(b)] == '\n' && b < len())
next(&b);
while(buf->c[bufaddr(b++)] == (usetabs ? '\t' : ' ') && b < len())
insert((*a)++, usetabs ? '\t' : ' ', 1);
}
}
/* attempt to open file and read contents into buffer */
int
fileinit(Buffer *b)
{
struct stat st;
size_t n;
ssize_t k;
char *p;
int fd;
fd = -1;
n = 0;
if((fd = open(b->path, O_RDWR | O_CREAT, 0666)) > 0 && fstat(fd, &st) != -1)
n = st.st_size;
b->c = calloc(n + Gaplen, 1);
if(b->c != NULL){
b->cap = n + Gaplen;
b->start = n;
b->gap = Gaplen;
if(fd > 0){
p = b->c;
while(n){
k = read(fd, p, n);
if(k == -1){
free(b->c);
close(fd);
return -1;
}
p += k;
n -= k;
};
close(fd);
}
return 0;
}
if(fd > 0)
close(fd);
return -1;
}
/* signal handler */
void
sig(int n)
{
switch(n){
case SIGINT:
case SIGWINCH:
err(Reset);
break;
case SIGTERM:
case SIGQUIT:
err(Panic);
}
}
/* block all incoming signals */
void
sigpend(void)
{
sigset_t mask;
if(sigfillset(&mask) != -1 && sigprocmask(SIG_SETMASK, &mask, &oset) != -1)
return;
perror("sigpend");
exit(1);
}
/* install signal handler and unblock signals */
void
siginit(void)
{
size_t i;
struct sigaction sa;
static const int siglist[] = { SIGINT, SIGWINCH, SIGTERM, SIGQUIT };
sa.sa_handler = sig;
sa.sa_flags = 0;
if(sigfillset(&sa.sa_mask) != -1){
for(i = 0; i < LEN(siglist); i++){
if(sigaction(siglist[i], &sa, NULL) == -1)
goto Error;
}
if(sigprocmask(SIG_SETMASK, &oset, NULL) != -1)
return;
}
Error:
perror("siginit");
exit(1);
}
/* set terminal into raw mode */
void
terminit(void)
{
struct termios new;
if(tcgetattr(STDIN_FILENO, &new) != -1){
term = new;
new.c_iflag &= ~(BRKINT | ISTRIP | INLCR | INPCK | IXON);
new.c_oflag &= ~OPOST;
new.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
new.c_cflag |= CS8;
new.c_cc[VMIN] = 0;
new.c_cc[VTIME] = 1; /* ds */
if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &new) != -1)
return;
}
perror("terminit");
exit(1);
}
/* allocate memory for dynamic array */
int
arrinit(Array *a, size_t size)
{
a->data = calloc(Gaplen, size);
if(a->data == NULL)
return -1;
a->size = size;
a->len = 0;
a->cap = Gaplen;
return 0;
}
/* free memory for dynamic array */
void
arrfree(Array *a)
{
free(a->data);
}
/* initialise buffer for given file */
int
bufinit(int i, const char *path)
{
bufs[i].addr1 = bufs[i].addr2 = bufs[i].vstart = bufs[i].vline = 0;
bufs[i].lead = &bufs[i].addr2;
bufs[i].dirty = 0;
strncpy(bufs[i].path, path, PATH_MAX);
if(arrinit(&bufs[i].changes, sizeof(Change)) != -1){
if(fileinit(&bufs[i]) != -1)
return 0;
arrfree(&bufs[i].changes);
}
return -1;
}
/* free memory for buffer */
void
buffree(Buffer *b)
{
arrfree(&b->changes);
free(b->c);
}
/* initialise all components of the editor */
void
init(int n, char **paths)
{
int i;
sigpend();
setlocale(LC_ALL, "");
for(i = 0; i < n; i++){
if((bufinit(i, paths[i + 1])) == -1)
goto Error;
}
if(arrinit(&ybuf, 1) == -1)
goto Error;
if(arrinit(&bbuf, 1) == -1)
goto Error;
if(arrinit(&dbuf, 1) == -1)
goto Error;
terminit();
siginit();
return;
Error:
perror("init");
exit(1);
}
/* return the terminal to original state */
void
termreset(void)
{
(void)!write(STDOUT_FILENO, CSI("9999;1H\r\n"), 11);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
}
/* deinitialise all components of the editor */
void
end(void)
{
size_t i;
termreset();
for(i = 0; i < nbuf; i++)
buffree(&buf[i]);
arrfree(&ybuf);
arrfree(&bbuf);
arrfree(&dbuf);
}
/* revert last sequence of changes, popping from top of undo stack */
void
undo(size_t *a, size_t *b)
{
Change m;
if(buf->changes.len > 0){
buf->changes.len--;
while(buf->changes.len > 0){
m = ((Change *)buf->changes.data)[--buf->changes.len];
switch(m.type){
case Uinsert:
delete(m.i, 0);
*a = *b = m.i;
break;
case Udelete:
insert(m.i, m.c, 0);
*a = *b = m.i;
break;
case Uend:
buf->changes.len++;
return;
}
}
} else /* no more changes */
buf->dirty = 0;
}
/* write contents of byte string to file */
ssize_t
writeall(int f, const char *s, size_t n)
{
int w;
ssize_t r;
r = n;
while(n){
w = write(f, s, n);
if(w == -1){
if(errno == EINTR)
w = 0;
else
return -1;
}
s += w;
n -= w;
}
return r;
}
/* write entire contents of current buffer to file */
ssize_t
writef(int f)
{
ssize_t n, r;
n = writeall(f, buf->c, buf->start);
if(n == -1)
return -1;
r = writeall(f, buf->c + buf->start + buf->gap, len() - buf->start);
if(r == -1)
return -1;
return r + n;
}
/* emergency backup in case of panic */
void
save(void)
{
int fd;
size_t i;
fd = creat("er.out", 0666);
if(fd > 0){
for(i = 0; i < nbuf; i++){
buf = &bufs[i];
writef(fd);
}
close(fd);
}
}
/* flush contents of screen buffer to STDOUT */
void
vflush(void)
{
if(writeall(STDOUT_FILENO, vbuf, vbuflen) == -1)
err(Panic);
vbuflen = 0;
}
/* append to screen buffer */
void
vpush(int n, ...)
{
int i;
char *s;
va_list args;
va_start(args, n);
for(i = 0; i < n; i++){
s = va_arg(args, char*);
while(*s){
if(vbuflen == Vbufmax)
vflush();
vbuf[vbuflen++] = *s++;
}
}
va_end(args);
}
/* position cursor */
void
cursor(unsigned int x, unsigned int y)
{
char tmp[32];
if(snprintf(tmp, sizeof(tmp), CSI("%u;%uH"), y + 1 , x + 1) < 0)
err(Panic);
vpush(1, tmp);
}
/* draw message to status bar */
void
bar(const char *fmt, ...){
int n;
va_list args;
while(bbuf.cap < (size_t)dim.ws_col + 1)
resize(&bbuf);
va_start(args, fmt);
n = vsnprintf(bbuf.data, dim.ws_col + 1, fmt, args);
va_end(args);
cursor(0, dim.ws_row - 1);
vpush(3, CSI("0m"), CSI("36m"), bbuf.data);
if(n > dim.ws_col){
cursor(dim.ws_col - 2, dim.ws_row - 1);
vpush(2, CSI("35m>"), CSI("0m"));
}
vpush(2, CSI("K"), CSI("0m"));
vflush();
}
/* start dialogue prompt in status bar */
int
dialogue(const char *prompt)
{
char *s;
int k;
bar("%s%s", prompt, dbuf.data);
while((k = key()) != '\n'){
if(k == -1)
continue;
else if(k == Kesc){
bar("");
return -1;
}else if(k == Kbs && dbuf.len > 0)
while(dbuf.len)
((char *)dbuf.data)[--dbuf.len] = 0;
/* TODO: single character backspace (handling multibyte properly) */
else{
s = ch;
while (*s)
APPEND(&dbuf, char, *s++);
}
bar("%s%s", prompt, dbuf.data);
}
return 0;
}
/* search for regular expression in current buffer */
void
search(size_t *a, size_t *b, int replace, int all)
{
regex_t reg;
regmatch_t m[1];
char err[128], *s;
int r;
size_t n, k;
if(dialogue(replace ?
(all? "Replace all: " : "Replace: ") : "Search: ") == -1)
return;
r = regcomp(®, dbuf.data, REG_EXTENDED | REG_NEWLINE);
k = 0;
if(r == 0){
if(replace && dialogue("with: ") == -1){
regfree(®);
return;
}
do{
move(len()); /* please mind the gap */
r = regexec(®, buf->c + *b, 1, m, 0);
if(r == 0 && *b + m[0].rm_so < len()){
*a = *b + m[0].rm_so;
*b += m[0].rm_eo - 1;
if(replace){
n = 1 + *b - *a;
while(n-- > 0)
delete(*a, 1);
*b = *a;
s = dbuf.data;
while(*s)
insert((*b)++, *s++, 1);
*a = *b;
k++;
record(Uend, 0, 0);
}
}else
break;
}while(all && r == 0);
}
bar("");
if(r != 0){
if(k > 0)
bar("Replaced %ld matches", k);
else{
regerror(r, ®, err, sizeof(err));
bar(err);
}
}
regfree(®);
return;
}
/* display current buffer to terminal */
void
display(void)
{
int i, j, l, i2, j2, n, h, jp, width;