-
Notifications
You must be signed in to change notification settings - Fork 6
/
command.c
1238 lines (1094 loc) · 32.7 KB
/
command.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 <errno.h>
#include <libgen.h>
#include <limits.h>
#include <signal.h>
#include <stdbool.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <unistd.h>
#include CURSES_INCLUDE
#include "structs.h"
#include "buffer.h"
#include "command.h"
#include "editor.h"
#include "parser.h"
#include "util.h"
/* UTILITY FUNCTIONS */
static bool
prompt(EDITOR *e, const char *p)
{
werase(e->cmdview.w);
mvwaddstr(e->cmdview.w, 0, 0, p);
wrefresh(e->cmdview.w);
KEYSTROKE k = getkeystroke(e, true);
werase(e->cmdview.w);
return k.c == L'y' || k.c == L'Y';
}
static void
fixblock(VIEW *v)
{
cleartag(v->b, BLOCK);
if (v->bs == NONE || v->be == NONE)
return;
if (v->bs > v->be){
lineno l = v->bs;
v->bs = v->be;
v->be = l;
}
settag(v->b, BLOCK, pos(v->bs, 0), pos(v->be, SIZE_MAX - 1), A_REVERSE);
}
static bool
setfind(EDITOR *e, const wchar_t *s, size_t n)
{
free(e->find);
e->find = NULL;
e->findn = 0;
if (n){
e->find = dupstr(s, n);
if (!e->find)
return error(e, "Out of memory");
e->findn = n;
}
return true;
}
static wint_t
identity(wint_t wc)
{
return wc;
}
static bool
find(EDITOR *e, VIEW *v, POS op, bool r)
{
BUFFER *b = v->b;
bool (*iter)(const BUFFER *, POS *) = r? prev : next;
bool (*atend)(const BUFFER *b, POS) = r? attop : atbot;
wint_t (*xform)(wint_t w) = v->uc? towupper : identity;
if (!e->find || !e->findn)
return error(e, "Empty target");
cleartag(b, HIGHLIGHT);
do{
if (!iter(b, &op))
return error(e, "Search failed");
POS p = op;
size_t i = 0;
while (i < e->findn && xform((wint_t)e->find[i]) == xform(charat(b, p))){
i++;
next(b, &p);
}
if (i == e->findn){
v->p = op;
settag(b, HIGHLIGHT, op, p, A_UNDERLINE | A_BOLD);
if (e->focusview == &e->cmdview)
settag(b, VIRTCURS, op, pos(op.l, op.c + 1), A_REVERSE);
redisplay(&e->docview);
return true;
}
} while (!atend(b, op));
return error(e, "Search failed");
}
static bool
exchange(EDITOR *e, VIEW *v, const ARG *a, bool query)
{
if (a->n1 && !setfind(e, a->s1, a->n1))
return false;
if (!find(e, v, v->p, false))
return false;
if (!query || prompt(e, "Exchange?")){
if (!deletetext(v->b, v->p, a->n1)
|| !inserttext(v->b, v->p, a->s2, a->n2))
return false;
}
v->p.c += a->n2;
return true;
}
static size_t
trimlength(const LINE *l)
{
size_t n = l->n;
while (n && iswspace(l->s[n - 1]))
n--;
return n;
}
static bool
safewrite(int fd, const char *s, size_t n)
{
ssize_t nw = 0;
while ((size_t)nw < n){
ssize_t w = write(fd, s + nw, n - nw);
if (w < 0)
return false;
nw += w;
}
return true;
}
static bool
writelines(int fd, EDITOR *e, const BUFFER *b, lineno ls, lineno le)
{
char *s = NULL;
bool rc = true;
for (lineno l = ls; rc && b->n && l <= le; l++){
if (b->l[l].n){
size_t n = trimlength(&b->l[l]);
if (n){
s = wstos(b->l[l].s, n);
if (!s)
return close(fd), error(e, "Out of memory");
if (!(rc = safewrite(fd, s, strlen(s)))){
int err = errno;
free(s);
close(fd);
return error(e, strerror(err));
}
free(s);
}
}
safewrite(fd, "\n", strlen("\n"));
}
close(fd);
return true;
}
/* COMMAND DEFINITIONS */
enum{
NOFLAGS = 0,
MARK = 1L<<0,
CLEARSBLOCK = 1L<<1,
NEEDSLINES = 1L<<2,
NEEDSBLOCK = 1L<<3,
SETSHILITE = 1L<<4,
NOLOCATOR = 1L<<5
};
#define COMMAND(name, fflags) \
bool \
cmd_ ## name (EDITOR *e, VIEW *v, const ARG *a) \
{ \
long flags = fflags; \
BUFFER *b = v->b; (void)b; \
POS p = v->p; (void)p; \
lineno ol = p.l; \
bool haslines = v->b->n; (void)haslines; \
bool rc = true; \
if ((flags & NEEDSBLOCK) && (v->bs == NONE || v->be == NONE || !haslines)) \
ERROR("No block marked"); \
if (flags & MARK) \
mark(v->b); \
#define END \
goto endfunc; \
endfunc: \
fixblock(&e->docview); \
fixcursor(e); \
if (flags & CLEARSBLOCK){ \
cleartag(v->b, BLOCK); \
v->bs = v->be = NONE; \
} \
if (!(flags & SETSHILITE)) \
cleartag(v->b, HIGHLIGHT); \
if (v->p.l != ol) \
v->ex = false; \
if (!(flags & NOLOCATOR)) \
v->gb = v->p; \
return rc; \
}
#define RETURN(x) do{rc = (x); goto endfunc;}while(false)
#define ERROR(s) RETURN(error(e, s))
#define SUCCEED do{error(e, ""); RETURN(true);}while(false)
#define FAIL RETURN(false)
COMMAND(a, MARK | CLEARSBLOCK) /* insert line after current */
RETURN(insertline(b, p.l + 1) && inserttext(b, pos(p.l + 1, 0), a->s1, a->n1));
END
COMMAND(ai, NOLOCATOR) /* autoindent */
v->ai = true;
END
COMMAND(b, MARK | NOLOCATOR) /* move to bottom of file */
v->p = pos(b->n? b->n - 1 : 0, 0);
END
COMMAND(bb, NOLOCATOR) /* smart mark of block */
if (v->bs == NONE)
v->bs = p.l;
else if (v->be == NONE)
v->be = p.l;
else{
v->be = NONE;
v->bs = p.l;
}
END
COMMAND(be, NOLOCATOR) /* block end at cursor line */
v->be = p.l;
END
COMMAND(bf, MARK | SETSHILITE | NOLOCATOR) /* backwards find */
RETURN((!a->n1 || setfind(e, a->s1, a->n1)) && find(e, v, p, true));
END
COMMAND(bm, NOLOCATOR) /* set bookmark */
if (a->n1 == 0 || a->n1 > BM_MAX)
ERROR("Invalid bookmark");
e->bm[a->n1 - 1] = p;
END
COMMAND(bs, NOLOCATOR) /* block start at cursor line */
v->bs = p.l;
END
COMMAND(bt, NOLOCATOR) /* backwards tab */
if (!cmd_cl(e, v, a))
FAIL;
while (v->p.c > 0 && v->p.c % v->ts){
if (!cmd_cl(e, v,a ))
FAIL;
}
END
COMMAND(ca, NOFLAGS) /* cancel command mode */
cmd_cs(e, v, a);
cmd_el(e, v, a);
e->focusview = &e->docview;
e->err[0] = 0;
END
COMMAND(cb, NEEDSBLOCK | CLEARSBLOCK | NOLOCATOR) /* clear block */
/* called for side effect */
END
COMMAND(cd, MARK) /* cursor down, same column */
if (!haslines || p.l >= b->n - 1)
ERROR("End of file");
v->p.l++;
END
COMMAND(ce, NOFLAGS) /* cursor to end of line */
v->p = pos(p.l, b->n? b->l[p.l].n : 0);
END
COMMAND(cf, NOLOCATOR) /* call a function key */
if (a->n1 >= FUNC_MAX || !e->funcs[a->n1])
ERROR("Invalid function number");
RETURN(runextended(e->funcs[a->n1], wcslen(e->funcs[a->n1]), e));
END
COMMAND(cj, NOFLAGS) /* cursor jump to EOL/BOL */
if (!haslines)
SUCCEED;
LINE *l = &b->l[p.l];
v->p.c = (p.c == l->n)? 0 : l->n;
END
COMMAND(cl, NOFLAGS) /* cursor left */
colno c = v->p.c;
if (v->p.c)
v->p.c--;
while (v->p.c && wcwidth(charat(b, v->p)) == 0)
v->p.c--;
if (c == v->p.c)
ERROR("Beginning of line");
END
COMMAND(cm, NOLOCATOR) /* enter command mode */
e->focusview = &e->cmdview;
werase(e->cmdview.w);
wrefresh(e->cmdview.w);
refresh();
END
COMMAND(cr, NOFLAGS) /* cursor right */
if (!haslines && !insertline(b, 0))
FAIL;
if (p.c >= SIZE_MAX - 1)
ERROR("End of line");
v->p.c++;
while (v->p.c < SIZE_MAX - 1 && wcwidth(charat(b, v->p)) == 0)
v->p.c++;
END
COMMAND(cs, NOFLAGS) /* cursor to start of line */
v->p.c = 0;
END
COMMAND(ct, NOLOCATOR) /* collapse tabs */
v->et = false;
END
COMMAND(cu, MARK) /* cursor up, same column */
if (!haslines || !p.l)
ERROR("Top of file");
v->p.l--;
END
COMMAND(d, MARK | CLEARSBLOCK) /* delete line */
if (!haslines)
SUCCEED;
free(v->dl);
v->dl = dupstr(b->l[p.l].s, b->l[p.l].n);
v->dln = b->l[p.l].n;
if (!v->dl)
ERROR("Out of memory");
RETURN(deleteline(b, p.l));
END
COMMAND(db, MARK | NEEDSBLOCK | CLEARSBLOCK) /* delete block */
size_t n = v->be - v->bs;
for (size_t i = 0; i <= n && b->n; i++)
deleteline(b, v->bs);
END
COMMAND(dc, NOFLAGS | NEEDSLINES) /* delete character at cursor */
if (b->n && p.c < b->l[p.l].n && b->l[p.l].n)
RETURN(deletetext(b, p, 1));
END
COMMAND(df, NOLOCATOR) /* display function definitions */
WINDOW *w = e->docview.w;
WINDOW *c = e->cmdview.w;
int oc = curs_set(0);
wattron(w, A_BOLD);
werase(w);
for (int i = 0; i < FUNC_MAX; i++){ /* XXX handle narrow windows */
mvwprintw(w, i, 0, "Function key %2d: %ls", i + 1,
e->funcs[i]? e->funcs[i] : L"Not set");
}
mvwprintw(w, 12, 0, "Type any character to continue");
wattroff(w, A_BOLD);
wrefresh(w);
werase(c);
mvwprintw(c, 0, 0, "Display Functions");
wrefresh(c);
getkeystroke(e, true);
if (oc != ERR)
curs_set(oc);
END
COMMAND(dl, NOFLAGS) /* delete character to left */
if (!p.c)
ERROR("Beginning of line");
v->p.c--;
if (haslines && v->p.c < b->l[v->p.l].n)
RETURN(deletetext(b, v->p, 1));
END
COMMAND(do, NOLOCATOR) /* do a shell command */
if (!a->n1)
ERROR("Empty command");
char *s = wstos(a->s1, a->n1);
if (!s)
ERROR("Out of memory");
endwin();
system(s);
puts("\nPress any key to continue");
getkeystroke(e, true);
refresh();
free(s);
END
COMMAND(dp, MARK) /* delete previous */
if (!v->p.c)
ERROR("Beginning of line");
while (v->p.c && iswspace(charat(b, pos(v->p.l, v->p.c - 1))) && cmd_dl(e, v, a))
;
if (v->p.c && !iswspace(charat(b, pos(v->p.l, v->p.c - 1)))){
while (v->p.c && !iswspace(charat(b, pos(v->p.l, v->p.c - 1))) && cmd_dl(e, v, a))
;
}
END
COMMAND(dw, MARK) /* delete to end of current word */
bool r = true;
size_t n = 0;
POS start = v->p;
if (ateol(v->b, v->p))
SUCCEED;
if (iswspace(charat(b, v->p))){
while (iswspace(charat(b, v->p)) && !ateol(b, v->p) && r){
v->p.c++;
n++;
}
} else while (!iswspace(charat(b, v->p)) && !ateol(b, v->p) && r){
v->p.c++;
n++;
}
v->p = start;
RETURN(deletetext(b, v->p, n));
END
COMMAND(e, MARK | NOLOCATOR) /* exchange s/t */
RETURN(exchange(e, v, a, false));
END
COMMAND(el, MARK) /* delete to EOL */
RETURN(!haslines || p.c >= b->l[p.l].n || deletetext(b, p, b->l[p.l].n - p.c));
END
COMMAND(ep, MARK | NOLOCATOR) /* go to beginning or end of page */
size_t lines, cols;
if (p.l >= b->n)
SUCCEED;
getmaxyx(v->w, lines, cols);
(void)cols;
lineno n = v->tos.l + lines - 1;
if (v->tos.l + lines - 1 >= b->n)
n = b->n - 1;
LINE *l = &b->l[n];
if (p.l != v->tos.l || p.c > v->tos.c)
v->p = v->tos;
else
v->p = pos(n, l->n);
END
COMMAND(eq, MARK | NOLOCATOR) /* exchange with query */
RETURN(exchange(e, v, a, true));
END
COMMAND(et, NOLOCATOR) /* expand tabs */
v->et = true;
END
COMMAND(ex, NOLOCATOR) /* expand margins */
v->ex = true;
END
COMMAND(f, MARK | SETSHILITE | NOLOCATOR) /* find forward */
RETURN((!a->n1 || setfind(e, a->s1, a->n1)) && find(e, v, p, false));
END
COMMAND(fb, MARK | NEEDSBLOCK | CLEARSBLOCK) /* filter block through command */
/* FIXME - we should do two pipes and a select instead of a temp file */
POS wp = pos(v->bs, 0);
char tfn[] = "/tmp/tineXXXXXX";
int tfd = mkstemp(tfn);
if (tfd < 0)
ERROR("Could not open temporary file");
int tochild[2] = {-1, -1};
if (pipe(tochild) != 0){
close(tfd);
unlink(tfn);
ERROR("Could not open pipe");
}
signal(SIGPIPE, SIG_IGN);
char *s = wstos(a->s1, a->n1);
if (!s){
close(tfd); close(tochild[0]); close(tochild[1]); unlink(tfn);
ERROR("Out of memory");
}
pid_t pid = fork();
if (pid == -1)
ERROR("Could not fork");
else if (pid == 0){
close(tochild[1]);
if (dup2(tochild[0], STDIN_FILENO) == -1) exit(EXIT_FAILURE);
if (dup2(tfd, STDOUT_FILENO) == -1) exit(EXIT_FAILURE);
if (dup2(tfd, STDERR_FILENO) == -1) exit(EXIT_FAILURE);
execl("/bin/sh", "sh", "-c", s, NULL);
} else{
free(s);
close(tochild[0]);
writelines(tochild[1], e, v->b, v->bs, v->be);
while (waitpid(pid, NULL, WNOHANG) == 0){
if (getkeystroke(e, false).o != ERR){
kill(pid, SIGKILL);
break;
}
}
}
close(tfd);
close(tochild[1]);
wchar_t *ws = stows(tfn, sizeof(tfn) - 1);
if (!ws){
free(ws); unlink(tfn);
ERROR("Out of memory");
}
v->p = wp;
ARG a1 = {.t = ARG_STRING, .s1 = ws, .n1 = wcslen(ws)};
bool r = cmd_db(e, v, a) && cmd_if(e, v, &a1);
free(ws); unlink(tfn);
RETURN(r);
END
COMMAND(fc, NOFLAGS) /* flip case */
wchar_t w = charat(b, p);
bool r = true;
w = iswupper(w)? towlower(w) : towupper(w);
if (haslines && p.c < b->l[p.l].n)
r = deletetext(b, p, 1) && inserttext(b, p, &w, 1);
RETURN(r && cmd_cr(e, v, a));
END
COMMAND(gb, MARK | NOLOCATOR) /* go back to previous position */
if (v->gb.l == NONE || v->gb.l >= b->n)
ERROR("No previous position");
v->p = v->gb;
END
COMMAND(gm, MARK | NOLOCATOR) /* go to mark */
if (a->n1 == 0 || a->n1 > BM_MAX)
ERROR("Invalid bookmark");
size_t n = a->n1 - 1;
if (e->bm[n].l == NONE || e->bm[n].l >= b->n)
ERROR("Bookmark not set");
v->p = e->bm[n];
END
COMMAND(i, MARK | CLEARSBLOCK) /* insert line before */
RETURN(insertline(b, p.l) && inserttext(b, pos(p.l, 0), a->s1, a->n1));
END
COMMAND(ib, MARK | NEEDSBLOCK) /* insert block */
if (p.l >= v->bs && p.l <= v->be)
ERROR("Cursor inside block");
size_t n = v->be - v->bs;
lineno bs = p.l < v->bs? v->bs + n + 1 : v->bs;
for (size_t i = 0; i <= n; i++){
if (!insertline(v->b, v->p.l))
FAIL;
}
for (size_t i = 0; i <= n; i++){
const LINE *l = &v->b->l[bs + i];
if (!inserttext(b, pos(p.l + i, 0), l->s, l->n))
FAIL;
}
v->bs = bs;
v->be = bs + n;
END
static bool
cmd_if_cb(const wchar_t *s, size_t n, void *p)
{
VIEW *v = (VIEW *)p;
v->p.c = 0;
bool rc = insertline(v->b, v->p.l) && inserttext(v->b, v->p, s, n);
v->p.l++;
return rc;
}
COMMAND(if, MARK | CLEARSBLOCK) /* insert file */
char *fn = wstos(a->s1, a->n1);
if (!fn)
ERROR("Out of memory");
bool r = readfile(fn, cmd_if_cb, v);
if (!r)
snprintf(e->err, ERR_MAX, "Could not open file: %s", strerror(errno));
v->p = p;
free(fn);
RETURN(r);
END
COMMAND(im, NOLOCATOR) /* ignore (don't show) matching braces */
v->sm = false;
END
static bool
squeezespace(BUFFER *b, POS p)
{
size_t n = 0;
POS start = p;
while (!ateol(b, p) && iswspace(charat(b, p)) && iswspace(charat(b, pos(p.l, p.c + 1)))){
p.c++;
n++;
}
deletetext(b, start, n);
return true;
}
COMMAND(j, MARK | CLEARSBLOCK) /* join this line and next */
if (b->n < 2 || p.l >= b->n - 1)
ERROR("End of file");
LINE *l1 = &b->l[p.l];
LINE *l2 = &b->l[p.l + 1];
POS np = pos(p.l, l1->n);
RETURN(inserttext(b, pos(p.l, l1->n), l2->s, l2->n) && deleteline(b, p.l + 1) && squeezespace(b, np));
END
COMMAND(lc, NOLOCATOR) /* case-sensitive searching */
v->uc = false;
END
COMMAND(m, MARK | NOLOCATOR) /* move to line */
if (a->n1 == NONE || a->n1 == 0 || a->n1 - 1 >= b->n || !b->n)
ERROR("Invalid line");
v->p.l = a->n1 - 1;
END
COMMAND(mc, NOLOCATOR) /* remap control key */
if (a->n1 != 1 || a->n2 != 1 || !a->s1 || !a->s2)
ERROR("Invalid mapping specification");
wchar_t c1 = towupper(a->s1[0]) & 0x1f;
wchar_t c2 = towupper(a->s2[0]) & 0x1f;
if (!iswcntrl(c1) || !iswcntrl(c2) || c1 >= CTRL_MAX || c2 >= CTRL_MAX)
ERROR("Invalid character specification");
e->ctrlmap[c1] = c2;
END
COMMAND(ms, NOLOCATOR) /* show matching braces */
v->sm = true;
END
COMMAND(n, MARK) /* move to beginning of next line */
if (!haslines || p.l >= b->n - 1)
ERROR("End of file");
v->p = pos(p.l + 1, 0);
END
COMMAND(ni, NOLOCATOR) /* regular indent */
v->ai = false;
END
COMMAND(p, MARK) /* move to beginning of previous line */
if (!haslines || !p.l)
ERROR("End of file");
v->p = pos(p.l - 1, 0);
END
COMMAND(pd, NOLOCATOR | MARK) /* page down */
if (b->n < v->ph || b->n - v->tos.l <= v->ph || b->n - p.l <= v->ph)
ERROR("End of file");
v->tos.l += v->ph;
v->p.l += v->ph;
END
COMMAND(ph, NOLOCATOR) /* define page height */
if (a->n1 == 0)
ERROR("Invalid page height");
v->ph = a->n1;
END
COMMAND(pu, NOLOCATOR | MARK) /* page up */
if (v->tos.l < v->ph || v->p.l < v->ph)
ERROR("Top of file");
v->tos.l -= v->ph;
v->p.l -= v->ph;
END
COMMAND(q, NOLOCATOR) /* quit without save */
if (e->docview.b->dirty && !prompt(e, "File has changed. Lose changes?"))
FAIL;
e->running = false;
END
COMMAND(qo, NOFLAGS) /* quote next */
v->q = true;
END
COMMAND(qy, NOFLAGS) /* force quit without save */
e->running = false;
END
COMMAND(rd, CLEARSBLOCK) /* restore deleted */
if (!v->dl)
ERROR("No saved line");
ARG a1 = {.t = ARG_STRING, .s1 = v->dl, .n1 = v->dln};
return cmd_i(e, v, &a1);
END
static bool
iscomment(const wchar_t *s, size_t n)
{
if (!n)
return true;
size_t i = 0;
while (i < n && iswspace(s[i]))
i++;
return s[i] == L'\'' || s[i] == 0;
}
static bool
cmd_rf_cb(const wchar_t *s, size_t n, void *p)
{
if (!n || iscomment(s, n))
return true;
return runextended(s, n, (EDITOR *)p);
}
COMMAND(rf, NOLOCATOR) /* run command file */
char *fn = wstos(a->s1, a->n1);
if (!fn)
ERROR("Out of memory");
bool r = readfile(fn, cmd_rf_cb, e);
free(fn);
RETURN(r);
END
COMMAND(rm, NOLOCATOR) /* reset margins */
v->rm = NONE;
v->lm = 0;
END
static bool
runcommand(EDITOR *e, VIEW *v, const ARG *a, bool stay)
{
if (!v->b->n || v->p.l >= v->b->n)
return error(e, "Commands abandoned");
e->err[0] = 0;
bool rc = true;
if (trimlength(&v->b->l[v->p.l])){
rc = runextended(v->b->l[v->p.l].s, v->b->l[v->p.l].n, e);
e->lc = v->p;
if (v->p.l == v->b->n - 1) /* only add a newline if we're not repeating */
insertline(v->b, v->b->n);
}
v->p = pos(v->b->n - 1, 0);
werase(v->w);
if (!stay)
e->focusview = &e->docview;
return rc;
}
COMMAND(rp, MARK | NOLOCATOR) /* repeat last command */
if (e->lc.l == NONE)
SUCCEED;
e->cmdview.p = e->lc;
RETURN(runcommand(e, &e->cmdview, a, false));
END
COMMAND(rs, MARK | NOLOCATOR) /* run extended command and stay */
RETURN(runcommand(e, v, a, true));
END
COMMAND(ru, MARK | NOLOCATOR) /* run extended command */
RETURN(runcommand(e, v, a, false));
END
COMMAND(s, MARK | CLEARSBLOCK) /* split line */
if (!b->n && !insertline(b, p.l))
ERROR("Out of memory");
size_t lines, cols;
getmaxyx(v->w, lines, cols);
(void)cols;
colno lm = v->lm == NONE? 0 : v->lm;
if (v->ai && b->n && v->p.l){
const LINE *l = &b->l[v->p.l];
for (size_t i = 0; i < l->n; i++){
if (!iswspace(l->s[i])){
lm = i;
break;
}
}
}
size_t ln = b->l[p.l].n;
size_t n = p.c >= ln? 0 : ln - p.c;
if (!insertline(v->b, p.l + 1)
|| !inserttext(v->b, pos(p.l + 1, lm), b->l[p.l].s + p.c, n)
|| !deletetext(v->b, pos(p.l, p.c), n))
ERROR("Out of memory");
v->p = pos(p.l + 1, lm);
if (b->n - v->tos.l >= lines + 1) /* emulate a quirk of ED */
v->tos.l++;
END
COMMAND(sa, NOLOCATOR) /* save text to file */
char *fn = strdup(e->name);
if (a->t == ARG_STRING && a->n1){
free(fn);
fn = wstos(a->s1, a->n1);
}
if (!fn)
ERROR("Out of memory");
int fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0){
free(fn);
ERROR("Could not open file");
}
bool r = writelines(fd, e, b, 0, b->n? b->n - 1 : 0);
if (rc)
e->docview.b->dirty = false;
free(fn);
RETURN(r);
END
COMMAND(sb, NOLOCATOR | MARK) /* show block on screen */
if (v->bs == NONE)
ERROR("No block defined");
v->p.l = v->bs;
END
COMMAND(sd, NOLOCATOR) /* set show matching bracket delay */
if (a->n1 > 2000)
ERROR("Invalid delay value");
v->sd = a->n1;
END
COMMAND(se, CLEARSBLOCK | MARK) /* split line after move to end */
RETURN(cmd_ce(e, v, a) && cmd_s(e, v, a));
END
COMMAND(sf, NOLOCATOR) /* set function key */
if (!a->n1)
ERROR("Invalid function number");
long long n = wcstoll(a->s1, NULL, 10) - 1;
if (n < 0 || n >= FUNC_MAX)
ERROR("Invalid function number");
free(e->funcs[n]);
if ((e->funcs[n] = dupstr(a->s2, a->n2)) == NULL)
ERROR("Out of memory");
END
COMMAND(sh, NOLOCATOR) /* show information */
WINDOW *w = e->docview.w;
WINDOW *c = e->cmdview.w;
char *bs = NULL;
char *be = NULL;
int oc = curs_set(0);
if (v->bs != NONE)
bs = wstos(v->b->l[v->bs].s, v->b->l[v->bs].n);
if (v->be != NONE)
be = wstos(v->b->l[v->be].s, v->b->l[v->be].n);
wattron(w, A_BOLD);
werase(w);
mvwprintw(w, 0, 0, "Editing file %s", basename(e->name));
mvwprintw(w, 1, 0, "In directory %-.61s", dirname(e->name));
mvwprintw(w, 2, 0, "Line count %zu", v->b->n);
mvwprintw(w, 3, 0, "Tab distance %zu", v->ts);
mvwprintw(w, 4, 0, "Page height %zu", v->ph);
mvwprintw(w, 5, 0, "Left margin %zu", v->lm == NONE? 1 : v->lm + 1);
if (v->rm == NONE)
mvwprintw(w, 5, 0, "Right margin Not set");
else
mvwprintw(w, 5, 0, "Right margin %zu", v->rm + 1);
mvwprintw(w, 6, 0, "Block start %-.24s%s", bs? trimleft(bs) : "Not set", bs? "..." : "");
mvwprintw(w, 7, 0, "Block end %-.24s%s", be? trimleft(be) : "Not set", be? "..." : "");
mvwprintw(w, 9, 0, "Type any character to continue");
wattroff(w, A_BOLD);
wrefresh(w);
free(bs);
free(be);
werase(c);
mvwprintw(c, 0, 0,
"tine Copyright (C) 2019-2020 Rob King. See COPYING for details.");
wrefresh(c);
getkeystroke(e, true);
if (oc != ERR)
curs_set(oc);
END
COMMAND(sl, NOLOCATOR) /* set left margin */
if (v->rm != NONE && a->n1 >= v->rm)
ERROR("Left margin would exceed right margin");
if (a->n1 >= SIZE_MAX - 1)
ERROR("Invalid margin");
v->lm = a->n1 > 0? a->n1 - 1 : p.c;
END
static inline bool
onscreen(const VIEW *v, POS p)
{
int y, x;
getmaxyx(v->w, y, x);
return p.l >= v->tos.l
&& p.l < v->tos.l + y
&& p.c >= v->tos.c
&& p.c < v->tos.c + x;
}
COMMAND(hb, MARK | NOLOCATOR)
if (!cmd_ty(e, v, a))
RETURN(false);
if (!v->sm)
SUCCEED;
POS op = v->p;
cmd_cl(e, v, a);
if (cmd_sm(e, v, a)){
if (onscreen(v, v->p)){
redisplay(&e->docview);
napms(v->sd);
}
}
v->p = op;
SUCCEED;
END
COMMAND(sm, MARK | NOLOCATOR) /* show matching brace */
wint_t s = charat(b, p), m = 0;
int c = 1;
bool r = 0;
switch (s){
case L'(': m = L')'; break;
case L')': m = L'('; r = true; break;
case L'{': m = L'}'; break;
case L'}': m = L'{'; r = true; break;
case L'[': m = L']'; break;
case L']': m = L'['; r = true; break;
case L'<': m = L'>'; break;
case L'>': m = L'<'; r = true; break;
default: ERROR("Search failed");
}
bool (*iter)(const BUFFER *, POS *) = r? prev : next;
bool (*atend)(const BUFFER *, POS) = r? attop : atbot;
do{
if (!iter(b, &p))
ERROR("Search failed");
if (charat(b, p) == s)
c++;
else if (charat(b, p) == m)
c--;
if (c == 0){
v->p = p;
SUCCEED;
}
} while (!atend(b, p));
ERROR("Search failed");
END
COMMAND(sr, NOLOCATOR) /* set right margin */
if (v->lm != NONE && a->n1 <= v->lm)
ERROR("Right margin would exceed left margin");
if (a->n1 >= SIZE_MAX - 1)
ERROR("Invalid margin");
v->rm = a->n1 > 0? a->n1 - 1 : p.c;
END
COMMAND(st, NOLOCATOR) /* set tab distance */
if (a->n1 == 0 || a->n1 >= SIZE_MAX - 1)
ERROR("Invalid tab width");
v->ts = a->n1;
END
COMMAND(t, MARK | NOLOCATOR) /* move to top of file */
v->p = pos(0, 0);
END
COMMAND(tb, NOFLAGS) /* move to next tabstop */
if (v->et){
ARG a2 = {.t = ARG_STRING, .n1 = 1, .s1 = L"\t"};
RETURN(cmd_ty(e, v, &a2));
}
if (!cmd_cr(e, v, a))
FAIL;