File: vi.c

package info (click to toggle)
sc 7.16-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 1,300 kB
  • ctags: 1,691
  • sloc: ansic: 28,398; yacc: 1,315; makefile: 369; lisp: 99; sed: 4
file content (2064 lines) | stat: -rw-r--r-- 44,990 bytes parent folder | download | duplicates (4)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
/*	SC	A Spreadsheet Calculator
 *
 *	One line vi emulation
 *	$Revision: 7.16 $
 */

#include <sys/types.h>
#ifdef BSD42
#include <strings.h>
#else
#ifndef SYSIII
#include <string.h>
#endif
#endif

#include <signal.h>
#include <curses.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include "sc.h"

#if defined(REGCOMP)
#include <regex.h>
#endif
#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

void doshell();
void gohome();
void leftlimit();
void rightlimit();
void gototop();
void gotobottom();

#define istext(a) (isalnum(a) || ((a) == '_'))

/*#define bool	int*/
#define true	1
#define false	0

static void append_line();
static void back_hist();
static int  back_line(int arg);
static int  back_word(int arg, int big_word);
static void back_space();
static void change_case(int arg);
static void col_0();
static void cr_line();
static void del_in_line(int arg, int back_null);
static void del_to_end();
static void doabbrev();
static void dogoto();
static void dotab();
static void dotcmd();
static int  find_char(int arg, int n);
static void for_hist();
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();
static void match_paren();
static void readhistfile(FILE *fp);
static void rep_char();
static void replace_in_line(int c);
static void replace_mode();
static void restore_it();
static void savedot(int c);
static void save_hist();
static void search_again(bool reverse);
static void search_hist();
static void search_mode(char sind);
static void stop_edit();
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);

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 */
char	*completethis = NULL;
int	search_dir;		/* Search direction:  forward = 0; back = 1 */

/* 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;
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 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;

    error("");
    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;
	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 == 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 == 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 == 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 == 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;
	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 > 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 (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 (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) {
					(void) sprintf(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 c = curcol;

				    currow = showsr;
				    showsr = r;
				    curcol = showsc;
				    showsc = c;
				    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()
{
    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()
{
    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()
{
    mode_ind = 'R';
    mode = REP_MODE;
}

void
toggle_navigate_mode()
{
    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()
{
    static struct range *firstmatch;
    static struct range *lastmatch;
    static struct range *nextmatch;
    int len;

    if ((linelim > 0 && isalnum(line[linelim-1])) || 
    	line[linelim-1] == '_' ||
	(completethis && line[linelim-1] == ' ')) {

	if (!completethis) {
	    for (completethis = line + linelim - 1; isalnum(*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()
{
    showrange = 1;
    showsr = currow;
    showsc = curcol;
    toggle_navigate_mode();
}

/* insert the range we defined by moving around the screen, see startshow() */

void
showdr()
{
    int			minsr, minsc, maxsr, maxsc;
    /*char		*p;*/
    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();
	sprintf(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;

	strcpy(r, coltoa(minsc));
	strcat(r, ":");
	strcat(r, coltoa(maxsc));

	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 int dotcalled = 0;

static void
dotcmd()
{
    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()
{
    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)
{
    static unsigned	undolen = 0;

    if (strlen(line)+1 > undolen) {
    	undolen = strlen(line)+40;

	undo_line = scxrealloc(undo_line, undolen);
    }
    (void) strcpy(undo_line, line);

    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()
{
    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);
    }

    strcpy(tempc, line);
    tempi = linelim;
    (void) strcpy(line, undo_line);
    linelim = undo_lim;
    strcpy(undo_line, tempc);
    undo_lim = tempi;
}

/* This command stops the editing process. */
static void
stop_edit()
{
    if (search_ind != ' ') {
	search_ind = ' ';
	(void) strcpy(line, history[0].histline);
	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 arg, int stop_null)
{
    int cpos = linelim;

    if (linelim < 0)
	return (linelim);
    else if (linelim + arg <= strlen(line))
	cpos += arg;
    else
	cpos = strlen(line);

    if (cpos == strlen(line) && cpos > 0 && !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 arg, int end_word, int big_word, int stop_null)
{
    register int c;
    register int cpos;

    cpos = linelim;

    while (cpos < strlen(line) && arg--) {
	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(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 arg)
{
    if (linelim > arg)
        return (linelim - arg);
    else
	return (0);
}

static int
back_word(int arg, int big_word)
{
    register int c;
    register int cpos;

    cpos = linelim;

    while (cpos > 0 && arg--) {
	if (line[cpos] == ' ') {
	    /* Skip white space */
	    while (cpos > 0 && line[cpos] == ' ')
		--cpos;
	} else if (cpos > 0 && (line[cpos-1] == ' ' 
			|| ( istext(line[cpos]) && !istext(line[cpos-1]))
			|| (!istext(line[cpos]) &&  istext(line[cpos-1])))) {
	    /* Started on the first char of a word - back up to prev. word */
	    --cpos;
	    while (cpos > 0 && line[cpos] == ' ')
		--cpos;
	}

	/* Skip across the word - goes 1 too far */
	if (big_word)
	    while (cpos > 0 && (c = line[cpos]) && c != ' ')
		--cpos;
	else if (istext(line[cpos])) {
	    while (cpos > 0 && (c = line[cpos]) && istext(c)) 
		--cpos;
	} else {
	    while (cpos > 0 && (c = line[cpos]) && !istext(c) && c != ' ')
		--cpos;
	}

	/* We are done - fix up the one too far */
	if (cpos > 0 && line[cpos] && line[cpos+1]) 
	    cpos++;
    }

    return(cpos);
}

/* Text manipulation commands */

/* If back_null is set, back up if the deletion leaves you at the null
 * line terminator.  Otherwise, don't.
 */
static void
del_in_line(int arg, int back_null)
{
    register int len, i;

    if (linelim >= 0) {
	len = strlen(line);
	if (arg > len - linelim)
	    arg = len - linelim;
	if (linelim == len && linelim > 0)
	    linelim--;
	strncpy(putbuf, line + linelim, arg);
	putbuf[arg] = '\0';
	for (i = linelim; i < len; i++)
	    line[i] = line[i+arg];
    }
    if (back_null && linelim > 0 && line[linelim] == '\0')
	--linelim;
}

void
ins_in_line(int c)
{
    int i, len;
    static int inabbr;

    if (c < 256) {
	if (linelim < 0 && c > 0) {
	    *line = '\0';
	    linelim = 0;
	}
	if (!inabbr && linelim > 0 && !(isalpha(c) || isdigit(c) || c == '_')) {
	    inabbr++;
	    doabbrev();
	    inabbr--;
	}
	if (c > 0) {
	    len = strlen(line);
	    for (i = len; i >= linelim; --i)
		line[i+1] = line[i];
	    line[linelim++] = c;
	    line[len+1] = '\0';
	}
    }
}

void
ins_string(char *s)
{
    while (*s)
	ins_in_line(*s++);
}

void
doabbrev()
{
    int len, pos;
    struct abbrev *a;
    struct abbrev *prev;
    
    if (!(isalpha(line[linelim-1]) || isdigit(line[linelim-1]) ||
	    line[linelim-1] == '_') || !(mode == INSERT_MODE ||
	    mode == SEARCH_MODE) || istart >= linelim)
	return;

    pos = linelim - 2;
    if (isalpha(line[pos]) || isdigit(line[pos]) || line[pos] == '_') {
	for (; pos >= istart; pos--)
	    if (!(isalpha(line[pos]) || isdigit(line[pos]) || line[pos] == '_'))
		break;
    } else if (line[pos] != ' ')
	for (; pos >= istart; pos--)
	    if (isalpha(line[pos]) || isdigit(line[pos]) || line[pos] == '_' ||
		    line[pos] == ' ')
		break;
    pos++;

    if (istart && pos == istart) {
	if (isalpha(line[pos]) || isdigit(line[pos]) || line[pos] == '_') {
	    if (isalpha(line[--pos]) || isdigit(line[pos]) || line[pos] == '_')
		return;
	} else {
	    if (!(isalpha(line[--pos]) || isdigit(line[pos]) ||
		    line[pos] == '_' || line[pos] == ' '))
		return;
	}
	pos++;
    }

    len = linelim - pos;
    if (len && (a = find_abbr(line + pos, len, &prev))) {
	if (len > 1 || pos == 0 || line[pos-1] == ' ') {
	    linelim = pos;
	    del_in_line(len, 0);
	    ins_string(a->exp);
	}
    }
}

static void
append_line()
{
    register int i;

    i = linelim;
    if (i >= 0 && line[i])
	linelim++;
    insert_mode();
}

static void
change_case(arg)
{
    if (linelim < 0) {
    	linelim = 0;
	*line = '\0';
    }
    while (arg--) {
	if (islower(line[linelim]))
	    line[linelim] = toupper(line[linelim]);
	else if (isupper(line[linelim]))
	    line[linelim] = tolower(line[linelim]);
	linelim = for_line(1, 0);
    }
}

static void
rep_char()
{
    int c;

    if (linelim < 0) {
    	linelim = 0;
	*line = '\0';
    }
    c = vigetch();
    savedot(c);
    if (c < 256 && c!= ESC && c != ctl('g')) {
	if (line[linelim] == '\0')
	    line[linelim+1] = '\0';
	line[linelim] = c;
    }
}

static void
replace_in_line(int c)
{
    register int len;

    if (c < 256) {
	if (linelim < 0) {
	    linelim = 0;
	    *line = '\0';
	}
	len = strlen(line);
	line[linelim++] = c;
	if (linelim > len)
	    line[linelim] = '\0';
    }
}

static void
back_space()
{
    if (linelim == 0)
	return;

    if (line[linelim] == '\0') {
	linelim = back_line(1);
	del_in_line(1, 1);
	linelim = strlen(line);
    } else {
	linelim = back_line(1);
	del_in_line(1, 1);
    }
    if (linelim < istart)
	istart = linelim;
}

/* Setting change to 1 makes `w' act like `e' so that `cw' will act like
 * `ce', just as in vi.  Setting change to 0 causes `w' to act as usual.
 */

int
get_motion(int change)
{
    int c;
    int arg2 = 0;

    c = vigetch();
    if (c == '0') {
	savedot(c);
	return (0);
    }
    while (c >= '0' && c <= '9') {
	arg2 = 10 * arg2 + c - '0';
	c = vigetch();
    }
    if (!arg2)
	arg2++;
    arg *= arg2;
    if (!nosavedot) {
	savedot(c);
	dotarg = arg;
    }
    switch (c) {
	case '$':	return (strlen(line));
	case 'b':	return (back_word(arg, 0));
	case 'B':	return (back_word(arg, 1));
	case 'c':	return (change ? -1 : linelim);
	case 'd':	return (!change ? -1 : linelim);
	case 'e':	return (for_word(arg, 1, 0, 1) + 1);
	case 'E':	return (for_word(arg, 1, 1, 1) + 1);
	case 'f':	return ((c = find_char(arg, 1)) == linelim ? c : c + 1);
	case 'F':	return (find_char(arg, -1));
	case 'h':	return (back_line(arg));
	case 'l':	return (for_line(arg, 1));
	case 't':	return ((c = to_char(arg, 1)) == linelim ? c : c + 1);
	case 'T':	return (to_char(arg, -1));
	case 'w':	return (for_word(arg, change, 0, 1) + change);
	case 'W':	return (for_word(arg, change, 1, 1) + change);
	default:	return (linelim);
    }
}

static void
yank_cmd(int delete, int change)
{
    int cpos;

    if ((cpos = get_motion(change)) == -1) {
	cpos++;
	linelim = strlen(line);
    }
    yank_chars(cpos, linelim, delete);
}

static void
yank_chars(register int first, register int last, int delete)
{
    int temp;

    if (first == last)
	return;

    if (last < first) {
	temp = last; last = first; first = temp;
    }

    linelim = first;
    strncpy(putbuf, line + first, last - first);
    putbuf[last - first] = '\0';
    if (delete)
	memmove(line + first, line + last, strlen(line + last) + 1);
}

static void
del_to_end()
{
    if (linelim < 0)
	return;
    strcpy(putbuf, line + linelim);
    line[linelim] = '\0';
    linelim = back_line(1);
}

static void
cr_line()
{
    struct frange *fr;

    ins_in_line(0);
    insert_mode();
    numeric_field = 0;
    if (linelim == -1) {	/* '\n' alone will put you into insert mode */
    	*line = '\0';		/* unless numeric and craction are both	set */
	linelim = 0;
	if (numeric && craction)
	    cellassign = 1;
	else
	    return;
    }
    save_hist();
    nosavedot = 1;
    linelim = 0;
    (void) yyparse();
    showrange = 0;
    linelim = -1;
    if (cellassign) {
	cellassign = 0;
	switch (craction) {
	    case CRROWS:
		if ((rowlimit >= 0) && (currow >= rowlimit)) {
		    forwcol(1);
		    currow = 0;
		} else {
		    if ((fr = find_frange(currow, curcol))) {
			forwrow(1);
			if (currow > fr->ir_right->row) {
			    backrow(1);
			    if (autowrap) {
				forwcol(1);
				currow = fr->ir_left->row;
				if (row_hidden[currow])
				    forwrow(1);
				if (curcol > fr->ir_right->col) {
				    backcol(1);
				    if (autoinsert)
					insertcol(1, 1);
				    else {
					currow = fr->ir_right->row;
					if (row_hidden[currow])
					    backrow(1);
				    }
				}
			    } else if (autoinsert)
				insertrow(1, 1);
			}
		    } else
			forwrow(1);
		}
		break;
	    case CRCOLS:
		if ((collimit >= 0) && (curcol >= collimit)) {
		    forwrow(1);
		    curcol = 0;
		} else {
		    if ((fr = find_frange(currow, curcol))) {
			forwcol(1);
			if (curcol > fr->ir_right->col) {
			    backcol(1);
			    if (autowrap) {
				forwrow(1);
				curcol = fr->ir_left->col;
				if (col_hidden[curcol])
				    forwcol(1);
				if (currow > fr->ir_right->row) {
				    backrow(1);
				    if (autoinsert)
					insertrow(1, 1);
				    else {
					curcol = fr->ir_right->col;
					if (col_hidden[curcol])
					    backcol(1);
				    }
				}
			    } else if (autoinsert)
				insertcol(1, 1);
			}
		    } else
			forwcol(1);
		}
		break;
	    default:
		break;
	}
    }
}

void
doshell()
{
    /*
    *  "! command"  executes command
    *  "!"	forks a shell
    *  "!!" repeats last command
    */
#if VMS || MSDOS
    error("Not implemented on VMS or MS-DOS");
#else /* VMS */
    char *shl;
    int pid, temp;
    char cmd[MAXCMD];
    static char lastcmd[MAXCMD];

    if (!(shl = getenv("SHELL")))
	shl = "/bin/sh";

    deraw(1);
    (void) fputs("! ", stdout);
    (void) fflush(stdout);
    (void) fgets(cmd, MAXCMD, stdin);
    cmd[strlen(cmd) - 1] = '\0';	/* clobber \n */
    if (strcmp(cmd,"!") == 0)		/* repeat? */
	(void) strcpy(cmd, lastcmd);
    else
	(void) strcpy(lastcmd, cmd);

    if (modflg) {
	(void) puts("[No write since last change]");
	(void) fflush(stdout);
    }

    if (!(pid = fork())) {
	(void) signal(SIGINT, SIG_DFL);  /* reset */
	if (strlen(cmd))
	    (void) execl(shl, shl, "-c", cmd, NULL);
	else
	    (void) execl(shl, shl, NULL);
	exit (-127);
    }

    while (pid != wait(&temp));

    (void) printf("Press any key to continue ");
    fflush(stdout);
    cbreak();
    (void)getch();
    goraw();
    clear();
#endif /* VMS */
}

/* History functions */

static void
save_hist()
{
    if (!lasthist || strcmp(history[lasthist].histline, line)) {
	if (lasthist < 0)
	    lasthist = 1;
	else
	    lasthist = lasthist % HISTLEN + 1;

	if (lasthist > endhist)
	    endhist = lasthist;

	if (history[lasthist].len < strlen(line) + 1) {
	    history[lasthist].len = strlen(line) + 40;
	    history[lasthist].histline = scxrealloc(history[lasthist].histline,
		    history[lasthist].len);
	}
	(void) strcpy(history[lasthist].histline, line);
	histsessionnew++;
    }
    if (history[0].histline) {
	scxfree(history[0].histline);
	history[0].histline = NULL;
	history[0].len = 0;
    }
    histp = 0;
}

static void
for_hist()
{
    if (histp == 0) {
	last_col();
    	return;
    }

    if (histp == lasthist)
	histp = 0;
    else
	histp = histp % endhist + 1;

    if (lasthist >= 0) {
	(void) strcpy(line, history[histp].histline);
	last_col();
    }
    if (histp)
	error("History line %d", endhist - lasthist + histp);
    else
	error("");
}

static void
back_hist()
{
    if (histp == 0) {
	if (history[0].len < strlen(line) + 1) {
	    history[0].len = strlen(line) + 40;
	    history[0].histline = scxrealloc(history[0].histline,
		    history[0].len);
	}
	(void) strcpy(history[0].histline, line);

	if (lasthist >= 0)
	    histp = lasthist;
    } else if (histp == 1) {
    	if (endhist != lasthist)
	    histp = endhist;
    } else if (histp != ((lasthist + 1) % (endhist + 1)))
	histp--;

    if (lasthist >= 0) {
    	(void) strcpy(line, history[histp].histline);
	last_col();
    }
    if (histp)
	error("History line %d", endhist - lasthist + histp);
    else
	error("");
}

static void
search_hist()
{
#ifdef RECOMP
    char	*tmp;
#endif
#if !defined(REGCOMP) && !defined(RE_COMP) && !defined(REGCMP)
    static	unsigned lastsrchlen = 0;
#endif

    if (linelim < 1) {
	linelim = 0;
	edit_mode();
	return;
    }

#ifdef REGCOMP
    if (last_search)
	regfree(last_search);
    else
	last_search = &preg;
    if ((errcode = regcomp(last_search, line, REG_EXTENDED))) {
	char *tmp = scxmalloc((size_t)160);
	regerror(errcode, last_search, tmp, sizeof(tmp));
	error(tmp);
	scxfree(tmp);
	return;
    }
#else
#ifdef RE_COMP
    if ((tmp = re_comp(line)) != NULL) {
	error(tmp);
	return;
    }
#else
#ifdef REGCMP
    free(last_search);
    if ((last_search = regcmp(line, NULL)) == NULL) {
	error("Invalid search string");
	return;
    }
#else
    if (strlen(line) + 1 > lastsrchlen) {
    	lastsrchlen = strlen(line) + 40;
	last_search = scxrealloc(last_search, lastsrchlen);
    }
    (void) strcpy(last_search, line);
#endif
#endif
#endif
    (void) strcpy(line, history[0].histline);
    search_again(false);
    if (mode != EDIT_MODE) edit_mode();
    search_ind = ' ';
}

static void
search_again(bool reverse)
{
    int prev_match;
    int found_it=0;
#if !defined(REGCOMP) && !defined(RE_COMP) && !defined(REGCMP)
    char *look_here;
    int do_next;
#endif

#ifdef REGCOMP
    if ((last_search == NULL))
	return;
#else
#ifndef RE_COMP
    if ((last_search == NULL) || (*last_search == '\0'))
	return;
#endif
#endif
    prev_match = histp > 0 ? histp : 0;
    error("");

    do {
	if (lasthist > 0) {
	    if (!(search_dir ^ reverse) && histp != lasthist)
		if (histp <= 0) {
		    histp = ((lasthist + 1) % endhist);
		    (void) strcpy(line, history[histp].histline);
		} else
		    for_hist();
	    else if ((search_dir ^ reverse) && histp != ((lasthist + 1) % endhist))
		back_hist();
	    else {
		histp = 0;
		(void) strcpy(line, history[0].histline);
		last_col();
	    }
	} else
	    break;
	if (histp == prev_match) {
	    if (histp <= 0) {
		error("No matches found");
		break;
	    }
	}
	if (histp <= 0) {
	    if (search_dir ^ reverse)
		back_hist();
	    else
		histp = ((lasthist + 1) % endhist);
		(void) strcpy(line, history[histp].histline);
	}
	found_it = 0;
#ifdef REGCOMP
	if (regexec(last_search, line, 0, NULL, 0) == 0)
	    found_it++;
#else
#ifdef RE_COMP
	if (re_exec(line) != 0)
	    found_it++;
#else
#ifdef REGCMP
	if (regex(last_search, line) != NULL)
	    found_it++;
#else
	look_here = line;
	do_next = 0;
	while ((look_here = (char *)strchr(look_here, *last_search)) != NULL &&
		!found_it && !do_next) {

	    if (strncmp(look_here, last_search, strlen(last_search)) == 0)
		found_it++;
	    else if (look_here < line + strlen(line) - 1)
	        look_here++;
	    else
		do_next++;
	}
#endif
#endif
#endif
	if (histp == prev_match)
	    break;
    } while (!found_it);
    if (found_it)
	error("History line %d", endhist - lasthist + histp);
    else
	error("No matches found");
    edit_mode();
    linelim = strlen(line) - 1;
}

#if !defined(MSDOS) && defined HISTORY_FILE
void
write_hist()
{
    int i;
    FILE *fp, *tmpfp = NULL;
    char histfile[PATHLEN];

    if (histsessionnew < HISTLEN) {
	/* write the new history for this session to a tmp file */
	tmpfp = tmpfile();
	for (i = 1; i <= histsessionnew; i++) {
	    histsessionstart = histsessionstart % endhist + 1;
	    if (history[histsessionstart].len > 40)
		fprintf(tmpfp, "%s\n", history[histsessionstart].histline);
	}
	fseek(tmpfp, 0, SEEK_SET);

	/* re-read the main history, then read back in the saved session hist*/
	histp = 0;
	lasthist = 0;
	endhist = -1;
	read_hist();
	readhistfile(tmpfp);
    }

    /* now write to whole lot out to the proper save file */
    strcpy(histfile, HISTORY_FILE);
    if (findhome(histfile) && (fp = fopen(histfile, "w")) != NULL) {
	for (i = 1; i <= endhist; i++) {
	    lasthist = lasthist % endhist + 1;
	    if (history[lasthist].len > 40)
		fprintf(fp, "%s\n", history[lasthist].histline);
	}
	fclose(fp);
    }
}

static void
readhistfile(FILE *fp)
{
    while (fgets(line, FBUFLEN, fp)) {
	line[strlen(line)-1] = '\0'; /* chop the \n */
	save_hist();
    }
    fclose(fp);
}

void
read_hist()
{
    FILE *fp;
    char histfile[PATHLEN];

    strcpy(histfile, HISTORY_FILE);
    if (findhome(histfile) && (fp = fopen(histfile, "r")) != NULL)
	readhistfile(fp);
    histsessionstart = lasthist;
    histsessionnew = 0;
}
#endif

static void
col_0()
{
    linelim = 0;
}

static void
last_col()
{
    linelim = strlen(line);
    if (linelim > 0 && mode_ind == 'e')
	--linelim;
}

static int
find_char(int arg, int n)
{
    register int i;

    if (findchar)
	finddir = n;
    findchar = vigetch();
    switch (dotb[doti - 1]) {
	case 'f': case 'F': case 't': case 'T':
	    savedot(findchar);
	default:
	    break;
    }
    i = linelim;
    while (arg--) {
	i += n;
	while (i >= 0 && line[i] && line[i] != findchar)
	    i += n;
	if (i < 0 || !line[i]) {
	    i = linelim;
	    break;
	}
    }
    findfunc = 'f';
    return (i);
}

static int
to_char(int arg, int n)
{
    register int i;
    int tmp = linelim;

    if (linelim + n >= 0 && linelim + n < strlen(line))
	linelim += n;
    i = find_char(arg, n);
    if (i != linelim)
	i -= n;
    linelim = tmp;
    findfunc = 't';

    return (i);
}

static void
match_paren()
{
    /*register int i;*/
    int nest = 1;
    int tmp = linelim;

    if (line[linelim] == '(') {
	while (nest && ++linelim >= 0 && line[linelim]) {
	    if (line[linelim] == '(')
		nest++;
	    else if (line[linelim] == ')')
		nest--;
	}
	if (line[linelim] != ')')
	    linelim = tmp;
    }
    else if (line[linelim] == ')') {
	while (nest && --linelim >= 0 && line[linelim]) {
	    if (line[linelim] == ')')
		nest++;
	    else if (line[linelim] == '(')
		nest--;
	}
	if (line[linelim] != '(')
	    linelim = tmp;
    }
}

/* If save is 0, remember the current position.  Otherwise, if the current
 * cell has changed since the last remember(0), save the remembered location
 * for the `, ', and c comands.
 */
void
remember(int save)
{
    static int remrow, remcol, remstrow, remstcol;

    if (save && (currow != remrow || curcol != remcol ||
	    strow != remstrow || stcol != remstcol)) {
	savedrow[0] = remrow;
	savedcol[0] = remcol;
	savedstrow[0] = remstrow;
	savedstcol[0] = remstcol;
    } else {
	remrow = currow;
	remcol = curcol;
	remstrow = strow;
	remstcol = stcol;
    }
}

void
gohome()
{
    struct frange *fr;

    remember(0);
    if ((fr = find_frange(currow, curcol))) {
	if (currow >= fr->ir_left->row &&
		currow <= fr->ir_right->row &&
		curcol >= fr->ir_left->col &&
		curcol <= fr->ir_right->col &&
		(currow > fr->ir_left->row ||
		curcol > fr->ir_left->col)) {
	    currow = fr->ir_left->row;
	    curcol = fr->ir_left->col;
	} else if (currow > fr->or_left->row ||
		curcol > fr->or_left->col) {
	    currow = fr->or_left->row;
	    curcol = fr->or_left->col;
	} else {
	    currow = 0;
	    curcol = 0;
	}
    } else {
	currow = 0;
	curcol = 0;
    }
    rowsinrange = 1;
    colsinrange = fwidth[curcol];
    remember(1);
    FullUpdate++;
}

void
leftlimit()
{
    struct frange *fr;

    remember(0);
    if ((fr = find_frange(currow, curcol))) {
	if (currow >= fr->ir_left->row &&
		currow <= fr->ir_right->row &&
		curcol > fr->ir_left->col &&
		curcol <= fr->ir_right->col)
	    curcol = fr->ir_left->col;
	else if (curcol > fr->or_left->col &&
		curcol <= fr->or_right->col)
	    curcol = fr->or_left->col;
	else
	    curcol = 0;
    } else
	curcol = 0;
    rowsinrange = 1;
    colsinrange = fwidth[curcol];
    remember(1);
}

void
rightlimit()
{
    register struct ent *p;
    struct frange *fr;

    remember(0);
    if ((fr = find_frange(currow, curcol))) {
	if (currow >= fr->ir_left->row &&
		currow <= fr->ir_right->row &&
		curcol >= fr->ir_left->col &&
		curcol < fr->ir_right->col)
	    curcol = fr->ir_right->col;
	else if (curcol >= fr->or_left->col &&
		curcol < fr->or_right->col)
	    curcol = fr->or_right->col;
	else {
	    curcol = maxcols - 1;
	    while (!VALID_CELL(p, currow, curcol) &&
		    curcol > fr->or_right->col)
		curcol--;
	    if ((fr = find_frange(currow, curcol)))
		curcol = fr->or_right->col;
	}
    } else {
	curcol = maxcols - 1;
	while (!VALID_CELL(p, currow, curcol) && curcol > 0)
	    curcol--;
	if ((fr = find_frange(currow, curcol)))
	    curcol = fr->or_right->col;
    }
    rowsinrange = 1;
    colsinrange = fwidth[curcol];
    remember(1);
}

void
gototop()
{
    struct frange *fr;

    remember(0);
    if ((fr = find_frange(currow, curcol))) {
	if (curcol >= fr->ir_left->col &&
		curcol <= fr->ir_right->col &&
		currow > fr->ir_left->row &&
		currow <= fr->ir_right->row)
	    currow = fr->ir_left->row;
	else if (currow > fr->or_left->row &&
		currow <= fr->or_right->row)
	    currow = fr->or_left->row;
	else
	    currow = 0;
    } else
	currow = 0;
    rowsinrange = 1;
    colsinrange = fwidth[curcol];
    remember(1);
}

void
gotobottom()
{
    register struct ent *p;
    struct frange *fr;

    remember(0);
    if ((fr = find_frange(currow, curcol))) {
	if (curcol >= fr->ir_left->col &&
		curcol <= fr->ir_right->col &&
		currow >= fr->ir_left->row &&
		currow < fr->ir_right->row)
	    currow = fr->ir_right->row;
	else if (currow >= fr->or_left->row &&
		currow < fr->or_right->row)
	    currow = fr->or_right->row;
	else {
	    currow = maxrows - 1;
	    while (!VALID_CELL(p, currow, curcol) &&
		    currow > fr->or_right->row)
		currow--;
	    if ((fr = find_frange(currow, curcol)))
		currow = fr->or_right->row;
	}
    } else {
	currow = maxrows - 1;
	while (!VALID_CELL(p, currow, curcol) && currow > 0)
	    currow--;
	if ((fr = find_frange(currow, curcol)))
	    currow = fr->or_right->row;
    }
    rowsinrange = 1;
    colsinrange = fwidth[curcol];
    remember(1);
}

static void
dogoto()
{
    static char		*tempc = NULL;
    static unsigned	templen = 0;
    int			tempi;

    if (strlen(line) + 1 > templen) {
    	templen = strlen(line) + 40;
	tempc = scxrealloc(tempc, templen);
    }

    strcpy(tempc, line);
    tempi = linelim;

    /* Can't switch back to navigate mode if insert_mode() is used here
     * instead of toggle_navigate_mode(), which is what we want when doing
     * a goto from within navigate mode.
     */
    insert_mode();
    /* Tempted as I was, I had to resist making this "Where would you like
     * to go today?" - CRM :)
     */
    query("goto where?", NULL);
    if (linelim >= 0) {
	memmove(line + 5, line, strlen(line) + 1);
	strncpy(line, "goto ", 5);
	linelim = 0;
	yyparse();
    }

    strcpy(line, tempc);
    linelim = tempi;
    /* Now we need to change back to navigate mode ourselves so that
     * toggle_navigate_mode() will work properly again.
     */
    mode_ind = 'v';
    mode = NAVIGATE_MODE;
    if (!showrange)
	toggle_navigate_mode();
}

void
query(char *s, char *data)
{
    int c;

    insert_mode();
    if (data != NULL) {
	strcpy(line, data);
	linelim = strlen(line);
    } else {
    	*line = '\0';
	linelim = 0;
    }
    if (s != NULL) error(s);

    while (linelim >= 0) {
	update(0);
	switch (c = nmgetch()) {
	    case ctl('m'):
		error("");
		return;
	    case ctl('g'):
		line[0] = '\0';
		linelim = -1;
		error("");
		update(0);
		return;
	    case ctl('l'):
		FullUpdate++;
		clearok(stdscr,1);
		update(1);
		break;
	    default:
		write_line(c);
	}
    }
}