-
Notifications
You must be signed in to change notification settings - Fork 20
/
vi.c
2089 lines (1920 loc) · 45.8 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
/* SC A Spreadsheet Calculator
*
* One line vi emulation
* $Revision: 7.16 $
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#if defined(REGCOMP)
#include <regex.h>
#endif
#include "compat.h"
#include "sc.h"
#if defined(RE_COMP)
extern char *re_comp(char *s);
extern char *re_exec(char *s);
#endif
#if defined(REGCMP)
char *regcmp();
char *regex();
#endif
#define istext(a) (isalnum(a) || ((a) == '_'))
static void append_line(void);
static void back_hist(void);
static int back_line(int arg);
static int back_word(int arg, int big_word);
static void back_space(void);
static void change_case(int arg);
static void col_0(void);
static void cr_line(void);
static void del_in_line(int arg, int back_null);
static void del_to_end(void);
static void doabbrev(void);
static void dogoto(void);
static void dotab(void);
static void dotcmd(void);
static int find_char(int arg, int n);
static void for_hist(void);
static int for_line(int arg, int stop_null);
static int for_word(int arg, int end_word, int big_word, int stop_null);
static int istart;
static void last_col(void);
static void match_paren(void);
static void readhistfile(FILE *fp);
static void rep_char(void);
static void replace_in_line(int c);
static void replace_mode(void);
static void restore_it(void);
static void savedot(int c);
static void save_hist(void);
static void search_again(bool reverse);
static void search_hist(void);
static void search_mode(char sind);
static void stop_edit(void);
static int to_char(int arg, int n);
static void u_save(int c);
static void yank_cmd(int delete, int change);
static void yank_chars(register int first, register int last, int delete);
static int get_motion(int);
int vigetch(void);
#ifdef NCURSES_MOUSE_VERSION
void mouse_set_pos(int);
#endif
extern int framerows; /* Rows in current frame */
extern char mode_ind; /* Mode indicator */
extern char search_ind; /* Search indicator */
extern int lcols; /* Spreadsheet Column the cursor was in last */
static char *completethis = NULL;
static int search_dir; /* Search direction: forward = 0; back = 1 */
char histfile[PATHLEN] = "~/.sc_history";
/* values for mode below */
#define INSERT_MODE 0 /* Insert mode */
#define EDIT_MODE 1 /* Edit mode */
#define REP_MODE 2 /* Replace mode */
#define SEARCH_MODE 3 /* Get arguments for '/' command */
#define NAVIGATE_MODE 4 /* Navigate the spreadsheet while editing
a line */
#define DOTLEN 200
static int mode = INSERT_MODE;
static struct hist {
unsigned int len;
char *histline;
} history[HISTLEN + 1];
static int histp = 0;
static int lasthist = 0;
static int endhist = -1;
static int histsessionstart = 0;
static int histsessionnew = 0;
#ifdef REGCOMP
static regex_t preg;
static regex_t *last_search = NULL;
static int errcode;
#else
#ifndef RE_COMP
static char *last_search = NULL;
#endif
#endif
static char *undo_line = NULL;
static unsigned undolen = 0;
static int undo_lim;
static char dotb[DOTLEN];
static int doti = 0;
static int do_dot = 0;
static int nosavedot = 1;
static int dotarg = 1;
static char putbuf[FBUFLEN];
static int findfunc = '\0';
static int findchar = 1;
static int finddir = 0;
void
write_line(int c)
{
struct frange *fr;
struct crange *cr;
CLEAR_LINE;
if (c != ctl('i')) completethis = NULL;
if (mode == EDIT_MODE) {
nosavedot = 0;
switch (c) {
case KEY_BACKSPACE:
case (ctl('h')): linelim = back_line(arg); break;
case (ctl('i')): dotab(); break;
case (ctl('m')): if (search_ind != ' ')
search_hist();
else
cr_line();
break;
case 'v':
case (ctl('v')): toggle_navigate_mode(); break;
case ESC: stop_edit(); break;
case '+': for_hist(); break;
case '-': back_hist(); break;
case KEY_END:
case (ctl('e')):
case '$': last_col(); break;
case '.': dotcmd(); break;
case '!': doshell(); break;
case ';': if (findfunc)
ungetch(findchar);
else
break;
findchar = 0;
if (findfunc == 'f')
linelim = find_char(arg, finddir);
else
linelim = to_char(arg, finddir);
break;
case ',': if (findfunc)
ungetch(findchar);
else
break;
findchar = 0;
if (findfunc == 'f')
linelim = find_char(arg, -(finddir));
else
linelim = to_char(arg, -(finddir));
break;
case '~': u_save(c); change_case(arg); break;
case '%': match_paren(); break;
#ifdef KEY_FIND
case KEY_FIND:
#endif
case '?':
case '/': search_mode(c); break;
case KEY_HOME:
case (ctl('a')):
case '0': col_0(); break;
case 'A': u_save(c); last_col(); append_line(); break;
case 'B': linelim = back_word(arg, 1); break;
case 'C': u_save(c); del_to_end(); append_line(); break;
case 'D': u_save(c); del_to_end(); break;
case 'E': linelim = for_word(arg, 1, 1, 0); break;
case 'F': linelim = find_char(arg, -1); break;
case 'G': if (histp > 0) histp = lasthist; for_hist(); break;
case 'I': u_save(c); col_0(); insert_mode(); break;
case 'N': search_again(true); break;
case 'P': u_save(c);
ins_string(putbuf);
linelim = back_line(1); break;
case 'R': u_save(c); replace_mode(); break;
case 'T': linelim = to_char(arg, -1); break;
case 'W': linelim = for_word(arg, 0, 1, 0); break;
case 'X': u_save(c); back_space(); break;
case 'Y': yank_chars(linelim, strlen(line), 0); break;
case 'a': u_save(c); append_line(); break;
case 'b': linelim = back_word(arg, 0); break;
case 'c': u_save(c); yank_cmd(1, 1); insert_mode(); break;
case 'd': u_save(c); yank_cmd(1, 0); break;
case 'e': linelim = for_word(arg, 1, 0, 0); break;
case 'f': linelim = find_char(arg, 1); break;
case KEY_LEFT:
case (ctl('b')):
case 'h': linelim = back_line(arg); break;
case KEY_IC:
case 'i': u_save(c); insert_mode(); break;
case KEY_DOWN:
case 'j': for_hist(); break;
case KEY_UP:
case 'k': back_hist(); break;
case KEY_RIGHT:
case (ctl('f')):
case ' ':
case 'l': linelim = for_line(arg, 0); break;
case 'n': search_again(false); break;
case 'p': u_save(c);
linelim = for_line(1, 1);
ins_string(putbuf);
linelim = back_line(1); break;
case 'q': stop_edit(); break;
case 'r': u_save(c); rep_char(); break;
case 's': u_save(c); del_in_line(arg, 0); insert_mode(); break;
case 't': linelim = to_char(arg, 1); break;
case 'u': restore_it(); break;
case 'w': linelim = for_word(arg, 0, 0, 0); break;
case KEY_DC:
case 'x': u_save(c); del_in_line(arg, 1); break;
case 'y': yank_cmd(0, 0); break;
#ifdef NCURSES_MOUSE_VERSION
case KEY_MOUSE:
if (getmouse(&mevent) != OK || mevent.y)
break;
if (mevent.bstate & BUTTON1_CLICKED)
mouse_set_pos(0);
else if (mevent.bstate & BUTTON1_PRESSED)
mouse_set_pos(1);
else if (mevent.bstate & BUTTON1_RELEASED)
mouse_set_pos(2);
break;
#endif
default:
break;
}
} else if (mode == INSERT_MODE) {
if (c == (ctl('m')))
savedot(ESC);
else
savedot(c);
switch (c) {
case KEY_BACKSPACE:
case (ctl('h')): back_space(); break;
case (ctl('i')): dotab(); break;
case (ctl('m')): cr_line(); break;
case (ctl('v')): toggle_navigate_mode(); break;
case KEY_LEFT:
case (ctl('b')): if (numeric_field) {
if (linelim > 0 &&
(size_t)linelim == strlen(line) &&
(line[linelim - 1] == '+' ||
line[linelim - 1] == '-')) {
toggle_navigate_mode();
backcol(1);
} else {
c = craction;
craction = 0;
cr_line();
craction = c;
backcol(1);
}
} else {
linelim = back_line(arg);
istart = linelim;
} break;
case KEY_RIGHT:
case (ctl('f')): if (numeric_field) {
if (linelim > 0 &&
(size_t)linelim == strlen(line) &&
(line[linelim - 1] == '+' ||
line[linelim - 1] == '-')) {
toggle_navigate_mode();
forwcol(1);
} else {
c = craction;
craction = 0;
cr_line();
craction = c;
forwcol(1);
}
} else {
linelim = for_line(arg, 1);
istart = linelim;
} break;
case KEY_DOWN:
case (ctl('n')): if (numeric_field) {
if (linelim > 0 &&
(size_t)linelim == strlen(line) &&
(line[linelim - 1] == '+' ||
line[linelim - 1] == '-')) {
toggle_navigate_mode();
forwrow(1);
} else {
c = craction;
craction = 0;
cr_line();
craction = c;
forwrow(1);
}
} else {
for_hist();
istart = linelim;
} break;
case KEY_UP:
case (ctl('p')): if (numeric_field) {
if (linelim > 0 &&
(size_t)linelim == strlen(line) &&
(line[linelim - 1] == '+' ||
line[linelim - 1] == '-')) {
toggle_navigate_mode();
backrow(1);
} else {
c = craction;
craction = 0;
cr_line();
craction = c;
backrow(1);
}
} else {
back_hist();
istart = linelim;
} break;
case KEY_HOME:
case (ctl('a')): col_0(); break;
case KEY_END:
case (ctl('e')): last_col(); break;
case ESC: ins_in_line(0);
edit_mode(); break;
/* '\035' is ^], which expands abbreviations without inserting another
* character in the line
*/
case '\035': if (linelim > 0) doabbrev(); break;
#ifdef NCURSES_MOUSE_VERSION
case KEY_MOUSE:
if (getmouse(&mevent) != OK || mevent.y)
break;
if (mevent.bstate & BUTTON1_CLICKED)
mouse_set_pos(0);
else if (mevent.bstate & BUTTON1_PRESSED)
mouse_set_pos(1);
else if (mevent.bstate & BUTTON1_RELEASED)
mouse_set_pos(2);
break;
#endif
default: ins_in_line(c); break;
}
} else if (mode == SEARCH_MODE) {
switch (c) {
case KEY_BACKSPACE:
case (ctl('h')): back_space(); break;
case (ctl('m')): search_hist(); break;
case ESC: ins_in_line(0);
edit_mode(); break;
/* '\035' is ^], which expands abbreviations without inserting another
* character in the line
*/
case '\035': if (linelim > 0) doabbrev(); break;
default: ins_in_line(c); break;
}
} else if (mode == REP_MODE) {
savedot(c);
switch (c) {
case KEY_BACKSPACE:
case (ctl('h')): if (linelim >= 0 &&
(size_t)linelim > strlen(undo_line))
back_space();
else {
linelim = back_line(1);
*(line+linelim) = *(undo_line+linelim);
} break;
case (ctl('m')): cr_line(); break;
case ESC: edit_mode(); break;
default: replace_in_line(c); break;
}
} else if (mode == NAVIGATE_MODE) {
switch (c) {
case '.':
case ':':
case (ctl('i')): if (!showrange) {
toggle_navigate_mode();
startshow();
} else if ((size_t)linelim == strlen(line) &&
(line[linelim - 1] == '+' ||
line[linelim - 1] == '-' ||
(line[linelim - 1] == ' ' &&
line[linelim - 2] == '='))) {
ins_string("@sum(");
showdr();
ins_in_line(')');
} else {
showdr();
ins_in_line(' ');
} break;
case ' ': if (showrange) {
showdr();
ins_in_line(' ');
toggle_navigate_mode();
} else {
forwcol(arg);
} break;
case '+':
case '-': if (!showrange) {
ins_string(v_name(currow, curcol));
ins_in_line(c);
} else if ((size_t)linelim == strlen(line) &&
(line[linelim - 1] == '+' ||
line[linelim - 1] == '-' ||
(line[linelim - 1] == ' ' &&
line[linelim - 2] == '='))) {
ins_string("@sum(");
showdr();
ins_in_line(')');
ins_in_line(c);
toggle_navigate_mode();
} else {
showdr();
ins_in_line(')');
ins_in_line(c);
} break;
case (ctl('m')): if (!showrange) {
ins_string(v_name(currow, curcol));
toggle_navigate_mode();
} else {
toggle_navigate_mode();
cr_line();
} break;
case 'v': { /* insert variable value */
struct ent *p = *ATBL(tbl, currow, curcol);
char temp[100];
if (p && p->flags & IS_VALID) {
snprintf(temp, sizeof temp, "%.*f",
precision[curcol], p->v);
ins_string(temp);
toggle_navigate_mode();
}
} break;
case 'c': if ((cr = find_crange(currow, curcol))) {
ins_string(r_name(cr->r_left->row,
cr->r_left->col,
cr->r_right->row,
cr->r_right->col));
toggle_navigate_mode();
ins_in_line(' ');
showrange = 0;
} break;
case 'f': if ((fr = find_frange(currow, curcol))) {
ins_string(r_name(fr->or_left->row,
fr->or_left->col,
fr->or_right->row,
fr->or_right->col));
toggle_navigate_mode();
ins_in_line(' ');
showrange = 0;
} break;
case 'r': if ((fr = find_frange(currow, curcol))) {
ins_string(r_name(fr->ir_left->row,
fr->ir_left->col,
fr->ir_right->row,
fr->ir_right->col));
toggle_navigate_mode();
ins_in_line(' ');
showrange = 0;
} break;
case KEY_LEFT:
case 'h': backcol(arg); break;
case KEY_RIGHT:
case 'l': forwcol(arg); break;
case KEY_DOWN:
case (ctl('n')):
case 'j': forwrow(arg); break;
case KEY_UP:
case (ctl('p')):
case 'k': backrow(arg); break;
case 'q':
case ctl('g'):
case (ctl('v')):
case ESC: toggle_navigate_mode();
showrange = 0; break;
case 'H': backcol(curcol - stcol + 2);
break;
case KEY_NPAGE: /* next page */
case (ctl('f')):
case 'J':
{
int ps;
ps = pagesize ? pagesize :
(LINES - RESROW - framerows)/2;
forwrow(arg * ps);
strow = strow + (arg * ps);
FullUpdate++;
}
break;
case KEY_PPAGE: /* previous page */
case (ctl('b')):
case 'K':
{
int ps;
ps = pagesize ? pagesize :
(LINES - RESROW - framerows)/2;
backrow(arg * ps);
strow = strow - (arg * ps);
if (strow < 0) strow = 0;
FullUpdate++;
}
break;
case 'L': forwcol(lcols - (curcol - stcol) + 1); break;
case (ctl('a')):
case KEY_HOME: gohome(); break;
case '0': leftlimit(); break;
case '$': rightlimit(); break;
case '^': gototop(); break;
case '#': gotobottom(); break;
case 'o': if (showrange) {
int r = currow;
int cc = curcol;
currow = showsr;
showsr = r;
curcol = showsc;
showsc = cc;
rowsinrange = 1;
colsinrange = fwidth[curcol];
} break;
case 'm': markcell(); break;
case '`': case '\'': dotick(c); break;
case '*': if (nmgetch() == '*') gotonote(); break;
case 'g': dogoto(); break;
case 'n': go_last(); break;
case 'w': {
register struct ent *p;
while (--arg>=0) {
do {
if (curcol < maxcols - 1)
curcol++;
else {
if (currow < maxrows - 1) {
while(++currow < maxrows - 1 &&
row_hidden[currow])
;
curcol = 0;
} else {
error("At end of table");
break;
}
}
} while(col_hidden[curcol] ||
!VALID_CELL(p, currow, curcol));
}
rowsinrange = 1;
colsinrange = fwidth[curcol];
break;
}
case 'b': {
register struct ent *p;
while (--arg>=0) {
do {
if (curcol)
curcol--;
else {
if (currow) {
while(--currow && row_hidden[currow])
;
curcol = maxcols - 1;
} else {
error("At start of table");
break;
}
}
} while (col_hidden[curcol] ||
!VALID_CELL(p, currow, curcol));
}
rowsinrange = 1;
colsinrange = fwidth[curcol];
break;
}
case 'C': if (braille)
braillealt ^= 1;
break;
}
}
}
void
edit_mode(void) {
mode_ind = 'e';
mode = EDIT_MODE;
if (linelim < 0) /* -1 says stop editing, ...so we still aren't */
return;
numeric_field = 0;
linelim = back_line(1);
}
void
insert_mode(void)
{
mode_ind = 'i';
mode = INSERT_MODE;
istart = linelim;
}
static void
search_mode(char sind)
{
if (search_ind == ' ') {
/*
* This back and forth movement through the history is just a quick
* way to force the current command to be saved in history[0].histline,
* allocating space for it if necessary. The command will be copied
* back into line by search_hist() before the search is done. - CRM
*/
back_hist();
for_hist();
line[0] = '\0';
linelim = 0;
mode_ind = 'i';
search_ind = sind;
search_dir = sind == '/' ? 1 : 0;
mode = SEARCH_MODE;
istart = linelim;
}
}
static void
replace_mode(void) {
mode_ind = 'R';
mode = REP_MODE;
}
void
toggle_navigate_mode(void)
{
static char prev_mode = NAVIGATE_MODE;
int limtmp;
switch (prev_mode) {
case INSERT_MODE:
if (mode == NAVIGATE_MODE) {
prev_mode = NAVIGATE_MODE;
insert_mode();
break;
} else
return;
case EDIT_MODE:
if (mode == NAVIGATE_MODE) {
prev_mode = NAVIGATE_MODE;
limtmp = linelim;
edit_mode();
linelim = limtmp;
break;
} else
return;
case NAVIGATE_MODE:
prev_mode = mode;
mode_ind = 'v';
mode = NAVIGATE_MODE;
break;
default:
prev_mode = NAVIGATE_MODE;
break;
}
}
void
dotab(void) {
static struct range *firstmatch;
static struct range *lastmatch;
static struct range *nextmatch;
int len;
if ((linelim > 0 && isalnum((int)line[linelim-1])) || line[linelim-1] == '_' ||
(completethis && line[linelim-1] == ' ')) {
if (!completethis) {
for (completethis = line + linelim - 1; isalnum((int)*completethis) ||
*completethis == '_'; completethis--) /* */;
completethis++;
len = line + linelim - completethis;
if (!find_range(completethis, -len, NULL, NULL, &lastmatch)) {
firstmatch = lastmatch;
while (firstmatch->r_next &&
!strncmp(completethis, firstmatch->r_next->r_name, len))
firstmatch = firstmatch->r_next;
nextmatch = firstmatch;
} else
nextmatch = NULL;
}
if (nextmatch) {
len = line + linelim - completethis;
memmove(completethis, line + linelim, strlen(line) - linelim + 1);
linelim -= len;
ins_string(nextmatch->r_name);
if (*(completethis - 1) == ' ' && *(line + linelim) != ' ')
ins_in_line(' ');
if (nextmatch == lastmatch)
nextmatch = firstmatch;
else
nextmatch = nextmatch->r_prev;
}
} else
startshow();
}
/* show the current range (see ^I), we are moving around to define a range */
void
startshow(void)
{
showrange = 1;
showsr = currow;
showsc = curcol;
toggle_navigate_mode();
}
/* insert the range we defined by moving around the screen, see startshow() */
void
showdr(void)
{
int minsr, minsc, maxsr, maxsc;
char r[12];
struct frange *fr = find_frange(currow, curcol);
if (showrange == SHOWROWS) {
minsr = showsr < currow ? showsr : currow;
minsc = fr ? fr->or_left->col : 0;
maxsr = showsr > currow ? showsr : currow;
maxsc = fr ? fr->or_right->col : maxcols;
toggle_navigate_mode();
snprintf(r, sizeof r, "%d:%d", minsr, maxsr);
ins_string(r);
} else if (showrange == SHOWCOLS) {
minsr = 0;
minsc = showsc < curcol ? showsc : curcol;
maxsr = maxrows;
maxsc = showsc > curcol ? showsc : curcol;
strlcpy(r, coltoa(minsc), sizeof r);
strlcat(r, ":", sizeof r);
strlcat(r, coltoa(maxsc), sizeof r);
toggle_navigate_mode();
ins_string(r);
} else {
minsr = showsr < currow ? showsr : currow;
minsc = showsc < curcol ? showsc : curcol;
maxsr = showsr > currow ? showsr : currow;
maxsc = showsc > curcol ? showsc : curcol;
toggle_navigate_mode();
ins_string(r_name(minsr, minsc, maxsr, maxsc));
}
showrange = 0;
}
/* dot command functions. Saves info so we can redo on a '.' command */
static void
savedot(int c)
{
if (do_dot || nosavedot || (c == '\n'))
return;
if (doti == 0) dotarg = arg;
if (doti < DOTLEN-1) {
if (c > 255) {
if (doti < DOTLEN-2) {
dotb[doti++] = c/256;
c %= 256;
} else
return;
}
dotb[doti++] = c;
dotb[doti] = '\0';
}
}
static void
dotcmd(void) {
static int dotcalled = 0;
int c;
if (dotcalled) /* stop recursive calling of dotcmd() */
return;
do_dot = 1;
doti = 0;
if (arg == 1)
arg = dotarg;
else
dotarg = arg;
while (dotb[doti] != '\0') {
if ((c = dotb[doti++]) < 4)
c = c * 256 + dotb[doti++];
dotcalled = 1;
write_line(c);
}
do_dot = 0;
doti = 0;
dotcalled = 0;
}
int
vigetch(void) {
int c;
if (do_dot) {
if (dotb[doti] != '\0') {
return (dotb[doti++]);
} else {
do_dot = 0;
doti = 0;
return (nmgetch());
}
}
update(1);
c = nmgetch();
return (c);
}
/* saves the current line for possible use by an undo cmd */
static void
u_save(int c)
{
if (strlen(line)+1 > undolen) {
undolen = strlen(line)+40;
undo_line = scxrealloc(undo_line, undolen);
}
strlcpy(undo_line, line, undolen);
undo_lim = linelim;
/* reset dot command if not processing it. */
if (!do_dot) {
doti = 0;
savedot(c);
}
}
/* Restores the current line saved by u_save() */
static void
restore_it(void) {
static char *tempc = NULL;
static unsigned templen = 0;
int tempi;
if ((undo_line == NULL) || (*undo_line == '\0'))
return;
if (strlen(line)+1 > templen) {
templen = strlen(line) + 40;
tempc = scxrealloc(tempc, templen);
}
strlcpy(tempc, line, templen);
tempi = linelim;
strlcpy(line, undo_line, sizeof line);
linelim = undo_lim;
strlcpy(undo_line, tempc, undolen);
undo_lim = tempi;
}
/* This command stops the editing process. */
static void
stop_edit(void) {
if (search_ind != ' ') {
search_ind = ' ';
strlcpy(line, history[0].histline, sizeof line);
write_line('G');
} else {
showrange = 0;
numeric_field = 0;
linelim = -1;
(void) move(1, 0);
(void) clrtoeol();
}
}
/*
* Motion commands. Forward motion commands take an argument
* which, when set, cause the forward motion to continue onto
* the null at the end of the line instead of stopping at the
* the last character of the line.
*/
static int
for_line(int a, int stop_null)
{
ssize_t cpos = linelim;
if (linelim < 0)
return (linelim);
else if (a >= 0 && (size_t)(linelim + a) <= strlen(line))
cpos += a;
else
cpos = strlen(line);
if (cpos > 0 && (size_t)cpos == strlen(line) && !stop_null)
return (cpos - 1);
else
return (cpos);
}
/* If end_word is non-zero, go to next end-of-word. Otherwise, go to next
* beginning-of-word.
*/
static int
for_word(int a, int end_word, int big_word, int stop_null)
{
register int c;
ssize_t cpos;
cpos = linelim;
while (cpos >= 0 && (size_t)cpos < strlen(line) && a--) {
if (end_word)
cpos++;
if (line[cpos] == ' ') {
while (line[cpos] == ' ')
cpos++;
if (cpos > 0 && line[cpos] == '\0')
--cpos;
if (!end_word)
continue;
}
if (big_word)
while ((c = line[cpos]) && c != ' ')
cpos++;
else if (istext((int)line[cpos])) {
while ((c = line[cpos]) && istext(c))
cpos++;
} else {
while ((c = line[cpos]) && !istext(c) && c != ' ')
cpos++;
}
if (end_word)
cpos--;
else while (line[cpos] == ' ')
cpos++;
if (cpos > 0 && line[cpos] == '\0' && !stop_null)
--cpos;
}
return (cpos);
}
static int
back_line(int a)
{
if (linelim > a)
return (linelim - a);
else
return (0);
}
static int
back_word(int a, int big_word)
{
register int c;
register int cpos;
cpos = linelim;
while (cpos > 0 && a--) {
if (line[cpos] == ' ') {
/* Skip white space */
while (cpos > 0 && line[cpos] == ' ')
--cpos;
} else if (cpos > 0 && (line[cpos-1] == ' '
|| ( istext((int)line[cpos]) && !istext((int)line[cpos-1]))
|| (!istext((int)line[cpos]) && istext((int)line[cpos-1])))) {
/* Started on the first char of a word - back up to prev. word */
--cpos;
while (cpos > 0 && line[cpos] == ' ')
--cpos;
}