File: estruct.h

package info (click to toggle)
vile 9.6m-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,196 kB
  • ctags: 8,459
  • sloc: ansic: 90,876; lex: 9,514; sh: 3,223; cpp: 3,137; perl: 2,928; makefile: 785; awk: 276
file content (3096 lines) | stat: -rw-r--r-- 89,509 bytes parent folder | download
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
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
/*	ESTRUCT:	Structure and preprocessor defines for
			vile.  Reshaped from the original, which
			was for microemacs.

			vile is by Paul Fox, Tom Dickey, Kevin Buettner,
			Rick Sladkey and many others (see the CHANGES*
			files for details).

			MicroEmacs was written by Dave G. Conroy
			modified by Steve Wilhite, George Jones,
			greatly modified by Daniel Lawrence.
*/

/*
 * $Header: /usr/build/vile/vile/RCS/estruct.h,v 1.662 2008/05/06 22:38:48 tom Exp $
 */

#ifndef _estruct_h
#define _estruct_h 1

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/*
 * This is used in the configure script:
 */
#ifndef CHECK_PROTOTYPES
#define CHECK_PROTOTYPES 0
#endif

/* ====================================================================== */

#ifndef os_chosen

/* All variants of UNIX should now be handled by the configure script */

#ifdef VMS		/* predefined by VAX/VMS compiler */
# define scrn_chosen
# define DISP_VMSVT  1
#endif

#ifdef __TURBOC__	/* predefined in Turbo C, both DOS and Windows */
# ifdef __FLAT__	/* predefined in Borland C for WIN32 */
#  define SYS_WINNT  1
# else
#  define SYS_MSDOS  1
# endif
# define CC_TURBO  1
#endif

#ifdef _WIN32		/* predefined in Visual C/C++ 4.0 */
# define SYS_WINNT 1
#endif

#ifdef __WATCOMC__
#define SYS_MSDOS  1
#define CC_WATCOM 1
#endif

#ifdef __IBMC__
# if __IBMC__ >= 200	/* IBM C Set ++ version 2.x */
#  define CC_CSETPP 1
# endif
#endif

#ifdef __OS2__
/* assume compiler already chosen */
#define SYS_OS2    1
#endif

#ifdef __GO32__		/* DJ's GCC version 1.09 */
#define SYS_MSDOS  1
#define CC_DJGPP   1
#endif

#ifdef __LCC__
#define SYS_WINNT	1
#define CC_LCC_WIN32	1
#endif

#ifdef _MSC_VER
#define SYS_WINNT	1
#define CC_MSVC		1
#endif

#endif /* os_chosen */

#ifdef sun
# define SYS_SUNOS 1		/* FIXME: need to tweak lint ifdefs */
#endif

#if defined(__CYGWIN__) || defined(__CYGWIN32__)
# define SYS_CYGWIN 1
#endif

#ifdef __EMX__
# define SYS_OS2_EMX 1		/* makefile.emx, or configure-script */
#endif

#if defined(VMS) || defined(__VMS) /* predefined by DEC's VMS compilers */
# define SYS_VMS    1
#endif

/* ====================================================================== */

/* Some code uses these as values in expressions, so we always want them
 * defined, just in case we run into a substandard preprocessor.
 */
#ifndef CC_CSETPP
#define CC_CSETPP		0	/* IBM C Set ++ 2.x */
#endif

#ifndef CC_DJGPP
#define CC_DJGPP		0	/* DJ's GCC version 1.09 */
#endif

#ifndef CC_LCC_WIN32
#define CC_LCC_WIN32		0	/* Lcc-win32 */
#endif

#ifndef CC_MSC
#define CC_MSC			0	/* Microsoft C versions 3 & 4 & 5 & 6 */
#endif

#ifndef CC_MSVC
#define CC_MSVC			0	/* Microsoft Visual C++ 7 & 8 & 9 */
#endif

#ifndef CC_TURBO
#define CC_TURBO		0	/* Turbo C or Borland C++ */
#endif

#ifndef CC_WATCOM
#define CC_WATCOM		0	/* WATCOM C/386 version 9.0 or above */
#endif

#ifndef SYS_CYGWIN
#define SYS_CYGWIN		0	/* Unix'ed Win32		*/
#endif

#ifndef SYS_MSDOS
#define SYS_MSDOS		0	/* MS-DOS			*/
#endif

#ifndef SYS_OS2
#define SYS_OS2			0	/* son of DOS			*/
#endif

#ifndef SYS_OS2_EMX
#define SYS_OS2_EMX		0	/* Unix'ed OS2			*/
#endif

#ifndef SYS_SUNOS
#define SYS_SUNOS		0	/* SunOS 4.x			*/
#endif

#ifndef SYS_UNIX
#define SYS_UNIX		0	/* Unix & friends		*/
#endif

#ifndef SYS_VMS
#define SYS_VMS			0	/* OpenVMS			*/
#endif

#ifndef SYS_WINNT
#define SYS_WINNT		0	/* Windows/NT			*/
#endif

/* As of version 3.51 of vile, CC_NEWDOSCC should be correct for Turbo,
 * Watcom, and the DJ gcc (GO32) compilers.  I'm betting that it's also
 * probably correct for MSC (Microsoft C) and ZTC (Zortech), but I'm not
 * sure of those.  (It implies a lot of ANSI and POSIX behavior.)
 */
#if CC_TURBO || CC_WATCOM || CC_MSC || CC_DJGPP || SYS_WINNT || CC_CSETPP || CC_MSVC || CC_LCC_WIN32
# define CC_NEWDOSCC 1
#endif

#ifndef CC_NEWDOSCC
#define CC_NEWDOSCC		0
#endif

/* ====================================================================== */

#ifndef HAVE_CONFIG_H		/* we did not run the configure script */

#if CC_NEWDOSCC
# define HAVE_GETCWD		1
# define HAVE_LIMITS_H		1
#endif

#if CC_CSETPP
# define HAVE_UTIME		1
# define HAVE_STRERROR		1
# define HAVE_SYS_UTIME_H	1
# define CPP_SUBS_BEFORE_QUOTE	1
#endif

#if CC_DJGPP
# define HAVE_UNISTD_H		1
#endif

#if SYS_WINNT && !CC_TURBO
# define HAVE_SYS_UTIME_H	1
# define HAVE_UTIME		1
#endif

#if SYS_WINNT
# define HAVE_PUTENV		1
#endif

/*
 * Porting constraints: supply the normally assumed values that we get from
 * the "configure" script, for systems on which we cannot run "configure"
 * (e.g., VMS, OS2, MSDOS).
 */
#ifndef HAVE_ACCESS
# define HAVE_ACCESS    1	/* if your system has the access() system call */
#endif

#ifndef HAVE_MKDIR
# define HAVE_MKDIR	1	/* if your system has the mkdir() system call */
#endif

#ifndef HAVE_SETJMP_H
# define HAVE_SETJMP_H  1	/* if your system has <setjmp.h> */
#endif

#ifndef HAVE_SIGNAL_H
# define HAVE_SIGNAL_H  1	/* if your system has <signal.h> */
#endif

#ifndef HAVE_STDLIB_H
# define HAVE_STDLIB_H  1	/* if your system has <stdlib.h> */
#endif

#if !(defined(HAVE_STRCHR) || defined(HAVE_STRRCHR))
# define HAVE_STRCHR    1
# define HAVE_STRRCHR   1
#endif

#ifndef HAVE_STRFTIME
# define HAVE_STRFTIME	1	/* if your system has the strftime() function */
#endif

#ifndef HAVE_STRTOUL
# define HAVE_STRTOUL	1	/* if your system has the strtoul() function */
#endif

#ifndef HAVE_STRING_H
# define HAVE_STRING_H  1	/* if your system has <string.h> */
#endif

#if SYS_VMS
# define HAVE_GETCWD 1
# if defined(__DECC) && !defined(__alpha)
#  undef HAVE_ACCESS		/* 'access()' is reported to not work */
# endif
# if !defined(__DECC)
#  define CC_CANNOT_INIT_UNIONS 1
# endif
# define SIGT void
# define SIGRET
# if DISP_X11 && defined(NEED_X_INCLUDES)
#  define XTSTRINGDEFINES
# endif
#endif

#endif /* HAVE_CONFIG_H */

#if defined(WIN32) || CC_TURBO
#include "w32vile.h"
#endif

#include <vl_stdio.h>

#if SYS_VMS && (! defined(__DECC_VER))
# include <types.h>
# include <stat.h>
#else
# include <sys/types.h>
# include <sys/stat.h>
#endif

#ifdef HAVE_LIBC_H
#include <libc.h>
#endif
#ifdef HAVE_FCNTL_H
#ifndef O_RDONLY	/* prevent multiple inclusion on lame systems */
#include <fcntl.h>	/* 'open()' */
#endif
#endif

#if defined(HAVE_SYS_TIME_H) && (defined(SELECT_WITH_TIME) || !(defined(HAVE_SELECT_H) || defined(HAVE_SYS_SELECT_H)))
#include <sys/time.h>
#ifdef TIME_WITH_SYS_TIME
# include <time.h>
#endif
#else
#include <time.h>
#endif

#ifdef RESOURCE_WITH_WAIT
#include <sys/resource.h>
#endif

#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>	/* 'wait()' */
#endif

#ifndef SIGT
/* choose between void and int signal handler return type.
  "typedefs?  we don't need no steenking typedefs..." */
# if CC_NEWDOSCC
#  define SIGT void
#  define SIGRET
# else
#  define SIGT int
#  define SIGRET return 0
# endif
#endif /* SIGT */

#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif

#ifndef ACTUAL_SIG_ARGS
# define ACTUAL_SIG_ARGS signo	/* some systems have more arguments... */
#endif

#if defined(__GNUC__)
# undef  SIG_DFL
# undef  SIG_IGN
# define SIG_DFL	(SIGT (*)(int ACTUAL_SIG_ARGS))0
# define SIG_IGN	(SIGT (*)(int ACTUAL_SIG_ARGS))1
#endif

#ifdef HAVE_SETJMP_H
#include	<setjmp.h>
#endif

/* argument for 'exit()' or '_exit()' */
#if SYS_VMS
#include	<stsdef.h>
#define GOODEXIT	(STS$M_INHIB_MSG | STS$K_SUCCESS)
#define BADEXIT		(STS$M_INHIB_MSG | STS$K_ERROR)
#else
#if defined(EXIT_SUCCESS) && defined(EXIT_FAILURE)
#define GOODEXIT	EXIT_SUCCESS
#define BADEXIT		EXIT_FAILURE
#else
#define GOODEXIT	0
#define BADEXIT		1
#endif
#endif

/* has the select() or poll() call, only used for short sleeps in fmatch() */
#ifdef HAVE_SELECT
#undef HAVE_POLL
#endif

/* ====================================================================== */
#ifndef scrn_chosen
/*	Terminal Output definitions		*/
/* choose ONLY one of the following */
#define DISP_TERMCAP	SYS_UNIX	/* Use TERMCAP			*/
#define DISP_ANSI	0		/* ansi escape sequences	*/
#define	DISP_VMSVT	SYS_VMS		/* various VMS terminal entries	*/
#define	DISP_BORLAND	0		/* Borland console I/O routines */
#define	DISP_X11	0		/* X Window System */
#define DISP_NTCONS	0		/* Windows/NT console		*/
#define DISP_NTWIN	0		/* Windows/NT screen/windows	*/
#define DISP_VIO	SYS_OS2		/* OS/2 VIO (text mode)		*/
#endif

/* Some code uses these as values in expressions, so we always want them
 * defined, just in case we run into a substandard preprocessor.
 */
#ifndef DISP_ANSI
#define DISP_ANSI	0
#endif

#ifndef DISP_BORLAND
#define DISP_BORLAND	0
#endif

#ifndef DISP_CURSES
#define DISP_CURSES	0
#endif

#ifndef DISP_NTCONS
#define DISP_NTCONS	0
#endif

#ifndef DISP_NTWIN
#define DISP_NTWIN	0
#endif

#ifndef DISP_TERMCAP
#define DISP_TERMCAP	0
#endif

#ifndef DISP_VIO
#define DISP_VIO	0
#endif

#ifndef DISP_VMSVT
#define DISP_VMSVT	0
#endif

#ifndef DISP_X11
#define DISP_X11	0
#endif

#ifndef USE_TERMINFO
#define USE_TERMINFO	0
#endif

#ifndef XTOOLKIT
#define XTOOLKIT	0
#endif

/* ====================================================================== */
/*	Configuration options... pick and choose as you wish */

/*	Code size options	*/
#ifndef FEWNAMES
#define	FEWNAMES 0	/* strip some names - will no longer be bindable */
#endif

#ifndef SMALLER
#define	SMALLER	0	/* strip some fluff -- not a lot smaller, but some */
#endif

#ifndef OPT_DEBUG
#define OPT_DEBUG 0	/* normally set by configure-script */
#endif

#ifndef OPT_FILTER
#define OPT_FILTER 0	/* normally set by configure-script */
#endif

#ifndef OPT_ICONV_FUNCS
#define OPT_ICONV_FUNCS 0 /* normally set by configure-script */
#endif

#ifndef OPT_LOCALE
#define OPT_LOCALE 0	/* normally set by configure-script */
#endif

#ifndef OPT_PERL
#define OPT_PERL 0	/* normally set by configure-script */
#endif

#ifndef OPT_PLUGIN
#define OPT_PLUGIN 0	/* normally set by configure-script */
#endif

#ifndef OPT_SHELL
#define OPT_SHELL 1	/* we'll disable this only as an exercise */
#endif

#ifndef OPT_TCL
#define OPT_TCL 0	/* normally set by configure-script */
#endif

/*
 * Widgets for xvile
 */
#ifndef ATHENA_WIDGETS
#define ATHENA_WIDGETS 0
#endif

#ifndef MOTIF_WIDGETS
#define MOTIF_WIDGETS 0
#endif

#ifndef NO_WIDGETS
#define NO_WIDGETS 0
#endif

#ifndef OL_WIDGETS
#define OL_WIDGETS 0
#endif

/*
 * Constants for ifdef'ing out chunks of code
 */
#define VILE_MAYBE 0
#define VILE_NEVER 0
#define VILE_NEEDED 0
#define VILE_SOMEDAY 0

/* various terminal stuff */
#define IBM_VIDEO	(SYS_MSDOS || SYS_OS2 || SYS_WINNT)
#define CRLF_LINES	(SYS_MSDOS || SYS_OS2 || SYS_WINNT)

/* various color stuff */
#define	OPT_COLOR (DISP_ANSI || IBM_VIDEO || DISP_TERMCAP || DISP_CURSES || DISP_X11)
#define	OPT_16_COLOR (IBM_VIDEO || DISP_TERMCAP || DISP_CURSES || DISP_X11)

#define OPT_DUMBTERM (DISP_TERMCAP || DISP_CURSES || DISP_VMSVT)

/* Feature turnon/turnoff */
#define OPT_CACHE_VCOL  !SMALLER /* cache mk_to_vcol() starting point          */
#define	OPT_DOSFILES	1	/* turn on code for DOS mode (lines that
				   end in crlf).  use DOSFILES, for instance,
				   if you edit DOS-created files under UNIX   */
#define	OPT_REVSTA	1	/* Status line appears in reverse video       */
#define	OPT_CFENCE	1	/* do fence matching in CMODE		      */
#define OPT_LCKFILES	0	/* create lock files (file.lck style)	      */
#define OPT_TAGS	1	/* tags support				      */
#define	OPT_PROCEDURES	1	/* macro language procedures		      */
#define	OPT_PATHLOOKUP	1	/* search $PATH for startup and help files    */
#define	OPT_SCROLLCODE	1	/* code in display.c for scrolling the screen.
				   Only useful if your display can scroll
				   regions, or at least insert/delete lines.
				   ANSI, TERMCAP/CURSES and VMSVT can do this */
#define OPT_CVMVAS	1	/* arguments to forward/back page and half page
				   are in pages	instead of rows (in vi,
				   they're rows, and the argument is "sticky",
				   i.e. it's remembered */
#define OPT_PRETTIER_SCROLL 0	/* can improve the appearance of a scrolling
				   screen, but it will in general be slower */
#define OPT_STUTTER_SEC_CMD 0	/* must the next/prev section commands (i.e.
				   ']]' and '[[' be stuttered?  they must be
				   stuttered in real vi, I prefer them not
				   to be */
#define OPT_VILE_ALLOC	1	/* use vile's malloc-macros */
#define OPT_VILE_CTYPE	1	/* use vile's character-testing macros */
#define OPT_VILE_REGEX	1	/* use vile's regular expressions */
#define OPT_W32PIPES    SYS_WINNT /* Win32 pipes */
#define WINMARK		0	/* experimental */

/*
 * use an insertion cursor if possible
 *
 * NOTE:  OPT_ICURSOR is _only_ supported by borland.c for a PC build and
 * for either win32 flavor.
 */
#if SYS_WINNT
# define OPT_ICURSOR    1
#else
# define OPT_ICURSOR    0
#endif

#if SYS_WINNT
# define MAX_RECENT_FILES 20
# define MAX_RECENT_FLDRS 20
#endif

/*
 * The $findpath statevar and find-cfg mode features require:
 *
 * - access to an OS that can handle potentially long command lines,
 *   in some cases transmitted via a pipe.
 *
 * - access to the unix find, xargs, and egrep commands.
 *
 * These restrictions make a port to DOS or VMS problematic.
 */
#if (SYS_WINNT || SYS_UNIX) && defined(OPT_SHELL)
# define OPT_FINDPATH   OPT_SHELL&&!SMALLER
#else
# define OPT_FINDPATH   0
#endif

#ifndef OPT_EXEC_MACROS		/* total numbered macros (see mktbls.c) */
#if SMALLER
#define OPT_EXEC_MACROS 10
#else
#define OPT_EXEC_MACROS 40
#endif
#endif

/* allow shift/ctrl/alt mods */
#define OPT_KEY_MODIFY	(SYS_WINNT | DISP_X11 | DISP_TERMCAP | DISP_CURSES)

/* the "working..." message -- we must have the alarm() syscall, and
   system calls must be restartable after an interrupt by default or be
   made restartable with sigaction() */
#if !SMALLER && defined(HAVE_ALARM) && defined(HAVE_RESTARTABLE_PIPEREAD)
#define OPT_WORKING 1
#else
#define OPT_WORKING 0
#endif

#define OPT_SCROLLBARS (XTOOLKIT | DISP_NTWIN)	/* scrollbars */

#ifndef OPT_VMS_PATH
#define OPT_VMS_PATH    (SYS_VMS)  /* vax/vms path parsing (testing/porting)*/
#endif

#if OPT_VMS_PATH
#undef SYSTEM_NAME
#define SYSTEM_NAME	"vms"	/* fakevms pretends to scripts it's vms */
#endif

/* systems with MSDOS-like filename syntax */
#define OPT_MSDOS_PATH  (SYS_MSDOS || SYS_OS2 || SYS_WINNT || SYS_OS2_EMX)
#define OPT_CASELESS	(SYS_MSDOS || SYS_OS2 || SYS_WINNT || SYS_OS2_EMX || SYS_CYGWIN || SYS_VMS)
#define OPT_UNC_PATH	(SYS_WINNT)

/* individual features that are (normally) controlled by SMALLER */
#define OPT_BNAME_CMPL  !SMALLER		/* name-completion for buffers */
#define OPT_B_LIMITS    !SMALLER		/* left-margin */
#define OPT_CURTOKENS   !SMALLER		/* cursor-tokens mode */
#define OPT_ENUM_MODES  !SMALLER		/* fixed-string modes */
#define OPT_EVAL        !SMALLER		/* expression-evaluation */
#define OPT_FILEBACK    (!SMALLER && !SYS_VMS)	/* file backup style */
#define OPT_FINDERR     !SMALLER		/* finderr support. */
#define OPT_FLASH       !SMALLER		/* visible-bell */
#define OPT_FORMAT      !SMALLER		/* region formatting support. */
#define OPT_HILITEMATCH !SMALLER		/* highlight all matches of a search */
#define OPT_HISTORY     !SMALLER		/* command-history */
#define OPT_HOOKS	!SMALLER		/* read/write hooks, etc. */
#define OPT_ISO_8859    !SMALLER		/* ISO 8859 characters */
#define OPT_ISRCH       !SMALLER		/* Incremental searches */
#define OPT_LINEWRAP    !SMALLER		/* line-wrap mode */
#define OPT_MAJORMODE   !SMALLER		/* majormode support */
#define OPT_MACRO_ARGS	(!SMALLER && OPT_EVAL)	/* macro argument parsing */
#define OPT_MLFORMAT    !SMALLER		/* modeline-format */
#define OPT_MODELINE    !SMALLER		/* vi-style modeline-support */
#define OPT_MULTIBYTE   (!SMALLER && OPT_LOCALE) /* experimental multibyte support */
#define OPT_NAMEBST     !SMALLER		/* name's stored in a bst */
#define OPT_ONLINEHELP  !SMALLER		/* short per-command help */
#define OPT_POPUPCHOICE !SMALLER		/* popup-choices mode */
#define OPT_POPUP_MSGS  !SMALLER		/* popup-msgs mode */
#define OPT_POSFORMAT   !SMALLER		/* position-format */
#define OPT_REBIND      !SMALLER		/* permit rebinding of keys at run-time	*/
#define OPT_SHOW_WHICH	!SMALLER		/* which-source, etc. */
#define OPT_TAGS_CMPL   (!SMALLER && OPT_TAGS)	/* name-completion for tags */
#define OPT_TERMCHRS    !SMALLER		/* set/show-terminal */
#define OPT_UPBUFF      !SMALLER		/* animated buffer-update */
#define OPT_WIDE_CTYPES !SMALLER		/* extra char-types tests */
#define OPT_WORDCOUNT   !SMALLER		/* "count-words" command" */

/* "show" commands for the optional features */
#define OPT_SHOW_COLORS	(!SMALLER && OPT_COLOR)	/* "show-colors" */
#define OPT_SHOW_CTYPE	!SMALLER		/* "show-printable" */
#define OPT_SHOW_EVAL   (!SMALLER && OPT_EVAL)	/* "show-variables" */
#define OPT_SHOW_MAPS   !SMALLER		/* display mapping for ":map" */
#define OPT_SHOW_MARKS  !SMALLER		/* "show-marks" */
#define OPT_SHOW_REGS   !SMALLER		/* "show-registers" */
#define OPT_SHOW_TAGS   (!SMALLER && OPT_TAGS)	/* ":tags" displays tag-stack */

/* selections and attributed regions */
#define OPT_VIDEO_ATTRS (!SMALLER || XTOOLKIT)
#define OPT_SELECTIONS  OPT_VIDEO_ATTRS
#define OPT_HYPERTEXT	OPT_VIDEO_ATTRS
#define OPT_LINE_ATTRS	OPT_VIDEO_ATTRS

/* OPT_PSCREEN permits a direct interface to the pscreen data structure
 * in display.c. This allows us to avoid storing the screen data on the
 * screen interface side.
 */
#define OPT_PSCREEN  (XTOOLKIT)

#if	(DISP_TERMCAP || DISP_CURSES) && !SMALLER
/* the setting "xterm-mouse" is always available, i.e.  the modetbl entry
 * is not conditional.  but all of the code that supports xterm-mouse _is_
 * ifdefed.  this makes it easier for users to be able to put "set
 * xterm-mouse" in their .vilerc which is shared between vile and xvile.
 */
#define	OPT_XTERM	2	/* mouse-clicking support */
#else
#define	OPT_XTERM	0	/* vile doesn't recognize xterm mouse */
#endif

	/* implement window title */
#define OPT_TITLE	(SYS_WINNT | DISP_X11 | OPT_XTERM)

	/* combine select/yank (for mouse support) */
#define OPT_SEL_YANK    ((DISP_X11 && XTOOLKIT) || SYS_WINNT || SYS_OS2)

	/* any mouse capability */
#define OPT_MOUSE       (OPT_SEL_YANK || OPT_XTERM)

	/* menus */
#define	OPT_MENUS	(!SMALLER && DISP_X11 && (MOTIF_WIDGETS||ATHENA_WIDGETS))
#define OPT_MENUS_COLORED 0	/* (MOTIF_WIDGETS && OPT_MENUS) */

	/* icons */
#define OPT_X11_ICON	DISP_X11 /* use compiled-in X icon */

/*
 * If selections will be used to communicate between vile and other
 * applications, OWN_SELECTION must be defined to call the procedure
 * for establishing ownership of the selection.
 */
#if OPT_SELECTIONS && XTOOLKIT /* or others? */
#define OWN_SELECTION() own_selection()
#else
#define OWN_SELECTION()	/*EMPTY*/
#endif

/*
 * Unix vi-style file encryption is available only on some platforms
 */
#ifdef HAVE_CRYPT
#define OPT_ENCRYPT     !SMALLER
#else
#define OPT_ENCRYPT     0
#endif

/*
 * Symbols that turn on tables related to OPT_ENUM_MODES in nefsms.h
 */
#define OPT_COLOR_SCHEMES          (OPT_ENUM_MODES && !SMALLER && OPT_COLOR)

#define OPT_BACKUP_CHOICES	   (OPT_ENUM_MODES && OPT_FILEBACK)
#define OPT_BOOL_CHOICES	   !SMALLER
#define OPT_BYTEORDER_MARK_CHOICES OPT_MULTIBYTE
#define OPT_CHARCLASS_CHOICES	   OPT_SHOW_CTYPE
#define OPT_COLOR_CHOICES	   (OPT_ENUM_MODES && OPT_COLOR)
#define OPT_CURTOKENS_CHOICES      OPT_CURTOKENS
#define OPT_DIRECTIVE_CHOICES      !SMALLER
#define OPT_FILE_ENCODING_CHOICES  OPT_MULTIBYTE
#define OPT_FORBUFFERS_CHOICES     !SMALLER
#define OPT_HILITE_CHOICES	   (OPT_ENUM_MODES && OPT_HILITEMATCH)
#define OPT_LOOKUP_CHOICES         !SMALLER
#define OPT_MMQUALIFIERS_CHOICES   OPT_MAJORMODE
#define OPT_PARAMTYPES_CHOICES     OPT_MACRO_ARGS
#define OPT_PATH_CHOICES           !SMALLER
#define OPT_POPUP_CHOICES	   (OPT_ENUM_MODES && OPT_POPUPCHOICE)
#define OPT_READERPOLICY_CHOICES   !SMALLER
#define OPT_RECORDATTRS_CHOICES    (OPT_ENUM_MODES && SYS_VMS)
#define OPT_RECORDFORMAT_CHOICES   (OPT_ENUM_MODES && SYS_VMS)
#define OPT_RECORDSEP_CHOICES      !SMALLER
#define OPT_SHOWFORMAT_CHOICES     !SMALLER
#define OPT_VIDEOATTRS_CHOICES     (OPT_ENUM_MODES && OPT_COLOR_SCHEMES)
#define OPT_VTFLASHSEQ_CHOICES     (OPT_ENUM_MODES && VTFLASH_HOST && OPT_FLASH)

/*
 * Special characters used in globbing
 */
#define	GLOB_MULTI	'*'
#define	GLOB_SINGLE	'?'
#define	GLOB_ELLIPSIS	"..."
#define	GLOB_RANGE	"[]"
#define	GLOB_NEGATE	"^!"

/*
 * Configuration options for globbing
 */
#define	OPT_GLOB_ENVIRON	!SMALLER
#define	OPT_GLOB_ELLIPSIS	SYS_VMS || SYS_UNIX || SYS_OS2 || SYS_WINNT || (SYS_MSDOS && !SMALLER)
#define	OPT_GLOB_PIPE		SYS_UNIX && OPT_SHELL
#define	OPT_GLOB_RANGE		SYS_UNIX || SYS_OS2 || SYS_WINNT || (SYS_MSDOS && !SMALLER)

/*	Debugging options	*/
#define	OPT_HEAPSIZE		0  /* track heap usage */
#define OPT_DEBUGMACROS		0  /* let $debug control macro tracing */

#ifndef OPT_ELAPSED
#define OPT_ELAPSED		0  /* turn on timing of traced calls */
#endif

#ifndef OPT_TRACE
#define OPT_TRACE		0  /* turn on debug/trace (link with trace.o) */
#endif

#ifndef CAN_TRACE
#define CAN_TRACE		OPT_TRACE  /* (link with trace.o) */
#endif

#ifndef CAN_VMS_PATH
#define CAN_VMS_PATH		1  /* (link with fakevms.o) */
#endif

/* That's the end of the user selections -- the rest is static definition */
/* (i.e. you shouldn't need to touch anything below here */
/* ====================================================================== */

#include <errno.h>

#if SYS_VMS
#include <perror.h>		/* defines 'sys_errlist[]' */
#endif

#if SYS_UNIX
# ifdef DECL_ERRNO
extern	int	errno;		/* some systems don't define this in <errno.h> */
# endif
# ifdef DECL_SYS_NERR
extern	int	sys_nerr;
# endif
# ifdef DECL_SYS_ERRLIST
extern	char *	sys_errlist[];
# endif
#endif
#define	set_errno(code)	errno = code

	/* bit-mask definitions */
#define	lBIT(n)	((ULONG)(1L<<(n)))
#define	iBIT(n) ((UINT)(1 <<(n)))

#if !(defined(HAVE_STRCHR) && defined(HAVE_STRRCHR))
#define USE_INDEX 1
#endif

#ifdef USE_INDEX
#define strchr index
#define strrchr rindex
#ifdef MISSING_EXTERN_RINDEX
extern char *index (const char *s, int c);
extern char *rindex (const char *s, int c);
#endif
#endif /* USE_INDEX */

#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
# include <string.h>
  /* An ANSI string.h and pre-ANSI memory.h might conflict.  */
# if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
#  include <memory.h>
# endif /* not STDC_HEADERS and HAVE_MEMORY_H */
#else /* not STDC_HEADERS and not HAVE_STRING_H */
# ifdef HAVE_STRINGS_H
#  include <strings.h>
  /* memory.h and strings.h conflict on some systems */
  /* FIXME: should probably define memcpy and company in terms of bcopy,
     et al here */
# endif
#endif /* not STDC_HEADERS and not HAVE_STRING_H */

#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif

#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif

/*	System dependent library redefinitions, structures and includes	*/

#if CC_CSETPP
#elif CC_LCC_WIN32
#include <direct.h>		/* chdir */
#define cwait(i,j,k) _cwait(i,j,k)
#define utime(i,j)   _utime(i,j)
#define SP_NOTREPORTED 0x4000	/* wingdi.h */
#define WHEEL_DELTA 120		/* winuser.h */
#elif CC_NEWDOSCC && ! CC_CSETPP
#include <dos.h>
# define HAVE_STRERROR		1
#endif

#if  CC_WATCOM
#include      <string.h>
#endif

#if CC_MSC
#include <memory.h>
#endif


/* on MS-DOS we have to open files in binary mode to see the ^Z characters. */

#if SYS_MSDOS || SYS_OS2 || SYS_WINNT || SYS_CYGWIN || SYS_OS2_EMX
#define FOPEN_READ	"rb"
#define FOPEN_WRITE	"wb"
#define FOPEN_APPEND	"ab"
#define FOPEN_UPDATE	"w+b"
#else
#define FOPEN_READ	"r"
#define FOPEN_WRITE	"w"
#define FOPEN_APPEND	"a"
#define FOPEN_UPDATE	"w+"
#endif

#if SYS_VMS
#undef  FOPEN_READ
#define FOPEN_READ "r", "shr=get,upd"
#define RECORD_ATTRS(ftn,cr,prn,blk) \
			(ftn<<FAB$V_FTN) | \
			(cr <<FAB$V_CR)  | \
			(prn<<FAB$V_PRN) | \
			(blk<<FAB$V_BLK)
#endif

#if OPT_MSDOS_PATH && !SYS_OS2_EMX	/* DOS path / to \ conversions */
# define is_slashc(c) (c == '\\' || c == '/')
# define SL_TO_BSL(s)	sl_to_bsl(s)
#else
# define SL_TO_BSL(s)	(s)
# define bsl_to_sl_inplace(s)
#endif


#ifndef is_slashc
# define is_slashc(c) (c == '/')
#endif

#define SLASHC '/'	/* UNIX, MSDOS-compatibility, VMS-shell */


#if SYS_VMS
#define	unlink(a)	delete(a)
#define tempnam		vile_tempnam
#endif

#if SYS_OS2 && CC_WATCOM
#define unlink(a)	remove(a)
#endif

/*
 * Definitions for hardwired VT-compatible flash esc sequences.  These
 * defns must precede inclusion of nemode.h
 */
#define VTFLASH_HOST    (SYS_UNIX||SYS_VMS)
#if OPT_FLASH
#define VTFLASH_OFF     0
#define VTFLASH_REVERSE 1
#define VTFLASH_NORMAL  2
#define VTFLASH_REVSEQ  "\033[?5h"
#define VTFLASH_NORMSEQ "\033[?5l"
#endif


	/* intermediate config-controls for filec.c (needed in nemode.h) */
#if !SMALLER
#define COMPLETE_FILES  (SYS_UNIX || SYS_MSDOS || SYS_VMS || SYS_OS2 || SYS_WINNT)
#define	COMPLETE_DIRS   (SYS_UNIX || SYS_MSDOS || SYS_VMS || SYS_OS2 || SYS_WINNT)
#else
#define COMPLETE_FILES  0
#define COMPLETE_DIRS   0
#endif

#if DISP_NTWIN
extern int MainProgram(int argc, char *argv[]);
#else
#define MainProgram main
#endif

	/* semaphore may be needed to prevent interrupt of display-code */
#if defined(SIGWINCH) || OPT_WORKING
# if OPT_TRACE > 2
extern void beginDisplay(void);
extern void endofDisplay(void);
# else
# define beginDisplay() ++im_displaying
# define endofDisplay() if (im_displaying > 0) --im_displaying; else assert(im_displaying > 0)
# endif
#else
# define beginDisplay() /* nothing */
# define endofDisplay() /* nothing */
#endif

#if OPT_WORKING
#define ShowWorking() (!global_b_val(MDTERSE) && global_g_val(GMDWORKING))
#else
#define ShowWorking() (!global_b_val(MDTERSE))
#endif

/* how to signal our process group: pass the 0 to 'getpgrp()' if we can,
 * since it's safer --- the machines where we can't are probably POSIX
 * machines with ANSI C.
 */
#if !defined(GETPGRP_VOID) || defined(MISSING_EXTERN_GETPGRP)
# define GETPGRPCALL getpgrp(0)
#else
# define GETPGRPCALL getpgrp()
#endif

#ifdef HAVE_KILLPG
# define signal_pg(sig) killpg( GETPGRPCALL, sig)
#else
# define signal_pg(sig)   kill(-GETPGRPCALL, sig)
#endif

/* only the raw pc screen driver has a memory-mapped "frame buffer" */
#if SYS_OS2
/*
 * The OS/2 toolkit defines identical typedefs for UCHAR, etc.;
 * we use those definitions to avoid trouble when using OS/2 include
 * files.
 */
# include <os2def.h>
#else
# if !defined(WIN32)
#  define UCHAR  unsigned char
#  define UINT   unsigned int
#  define USHORT unsigned short
#  define ULONG  unsigned long
# endif
#endif

/*	internal constants	*/

#if SYS_MSDOS
#define	BITS_PER_INT	16
#endif

#ifndef	BITS_PER_INT
#define	BITS_PER_INT	32
#endif

#ifdef  MAXPATHLEN			/* usually in <sys/param.h>	*/
#define NFILEN	MAXPATHLEN		/* # of bytes, file name	*/
#else
#if SYS_MSDOS && CC_TURBO
#define NFILEN	128			/* # of bytes, file name	*/
#else
#define NFILEN	256			/* # of bytes, file name	*/
#endif
#endif

#define NBUFN	21			/* # of bytes, buffername, incl. null*/
#define NLINE	256			/* # of bytes, input line	*/
#define	NSTRING	144			/* # of bytes, string buffers	*/
#define NPAT	144			/* # of bytes, pattern		*/
#define NKEYLEN	20			/* # of bytes, crypt key	*/
#define VL_HUGE	(1<<(BITS_PER_INT-2))	/* Huge number			*/
#define	NLOCKS	100			/* max # of file locks active	*/
#if OPT_16_COLOR
#define	NCOLORS	16			/* number of supported colors	*/
#else
#define	NCOLORS	8			/* number of supported colors	*/
#endif
#define	KBLOCK	256			/* sizeof kill buffer chunks	*/

#if OPT_SELECTIONS
#define NKREGS	39			/* When selections are enabled, we
					 * allocate an extra kill buffer for
					 * the current selection and another
					 * for the clipboard.
					 */
#define CLIP_KREG  (NKREGS-1)
#define SEL_KREG   (NKREGS-2)
#define KEYST_KREG (NKREGS-3)
#else
#define	NKREGS	37			/* number of kill buffers	*/
#define KEYST_KREG (NKREGS-1)
#endif

#if SMALLER
#define NMARKS  26
#else
#define NMARKS  36
#endif

/* special characters assigned to kill-registers */
#define CLIP_KCHR  ';'
#define SEL_KCHR   '.'
#define KEYST_KCHR '<'

#define	NBLOCK	16			/* line block chunk size	*/
#define MINWLNS	3			/* min # lines, window/screen	*/
#define MAXROWS	200			/* max # lines per screen	*/
#define MAXCOLS	200			/* max # cols per screen	*/

#define C_BLACK 0
#define C_WHITE (ncolors-1)

#define MinCBits   8			/* bits in N_chars		*/

#if OPT_MULTIBYTE
#define MaxCBits   16			/* allow UTF-16 internal	*/
#else
#define MaxCBits   MinCBits		/* allow 8bit internal		*/
#endif

#define N_chars    (1 << MinCBits)	/* must be a power-of-2		*/
#define HIGHBIT    (1 << 7)		/* the meta bit			*/

#define FoldTo8bits(value) vl_mb_is_8bit(value)

#define COLS_CTRL  2			/* columns for "^X"		*/
#define COLS_8BIT  4			/* columns for "\xXX"		*/
#define COLS_UTF8  6			/* columns for "\uXXXX"		*/

#define CTLA       iBIT(MaxCBits+0)	/* ^A flag, or'ed in		*/
#define CTLX       iBIT(MaxCBits+1)	/* ^X flag, or'ed in		*/
#define SPEC       iBIT(MaxCBits+2)	/* special key (function keys)	*/
#define NOREMAP    iBIT(MaxCBits+3)	/* unremappable */
#define YESREMAP   iBIT(MaxCBits+4)	/* override noremap */
#define REMAPFLAGS (NOREMAP|YESREMAP)

#if OPT_KEY_MODIFY
#define mod_KEY    iBIT(MaxCBits+5)	/* special Win32 keys		*/
#define mod_SHIFT  iBIT(MaxCBits+6)	/* shift was held down		*/
#define mod_CTRL   iBIT(MaxCBits+7)	/* control was held down	*/
#define mod_ALT    iBIT(MaxCBits+8)	/* alt was held down		*/
#define mod_NOMOD  (~(mod_KEY|mod_SHIFT|mod_CTRL|mod_ALT))
#endif

#define kcod2key(c)	((c) & (UINT)(iBIT(MaxCBits)-1)) /* strip off the above prefixes */
#define	isSpecial(c)	(((UINT)(c) & (UINT)~(iBIT(MaxCBits)-1)) != 0)

#define	char2int(c)	((int)kcod2key(c))	/* mask off sign-extension, etc. */

#define	PLURAL(n)	((n) != 1 ? "s" : "")
#define NONNULL(s)	((s) != 0 ? (s) : "")
#define isEmpty(s)	((s) == 0 || *(s) == EOS)

#define EOS        '\0'
#define BQUOTE     '`'
#define SQUOTE     '\''
#define DQUOTE     '"'
#define BACKSLASH  '\\'
#define CH_TILDE   '~'

#define isEscaped(s)	((s)[-1] == BACKSLASH)
#define isTab(c)	((c) == '\t')

/* protect against losing namespaces */
#undef	FALSE
#undef	TRUE
#undef	ABORT
#undef	SORTOFTRUE

#define FALSE	0			/* False, no, bad, etc.		*/
#define TRUE	1			/* True, yes, good, etc.	*/
#define ABORT	2			/* Death, ESC, abort, etc.	*/
#define	SORTOFTRUE	3		/* really!	*/

/* keystroke replay states */
#define	PLAY	'P'
#define	RECORD	'R'

#define MAP_QUOTED	TRUE
#define MAP_UNQUOTED	FALSE

#define DOMAP	TRUE
#define NODOMAP	FALSE

/* values for regionshape */
typedef enum {
	rgn_EXACT,
	rgn_FULLLINE,
	rgn_RECTANGLE
} REGIONSHAPE;

#define ENUM_ILLEGAL   (-3)
#define ENUM_FCOLOR    (-2)
#define ENUM_UNKNOWN   (-1)
#define END_CHOICES    { (char *)0, ENUM_ILLEGAL }

typedef struct {
	const char * choice_name;
	int    choice_code;
} FSM_CHOICES;

#include <blist.h>

typedef struct {
	const FSM_CHOICES *choices;
	BLIST blist;
} FSM_BLIST;

typedef struct {
	const char * mode_name;
	FSM_BLIST * lists;
} FSM_TABLE;

typedef enum {
	bom_NONE = 0
	, bom_UTF8
	, bom_UTF16LE
	, bom_UTF16BE
	, bom_UTF32LE
	, bom_UTF32BE
} BOM_CODES;

typedef enum {
	CT_BOTH = 0
	, CT_CCLASS
	, CT_REGEX
} CURTOKENS;

/*	Directive definitions	*/

typedef	enum {
	D_UNKNOWN = ENUM_UNKNOWN,
	D_ENDM = 0
#if ! SMALLER
	, D_IF
	, D_ELSEIF
	, D_ELSE
	, D_ENDIF
	, D_GOTO
	, D_RETURN
	, D_WHILE
	, D_ENDWHILE
	, D_BREAK
	, D_FORCE
	, D_HIDDEN
	, D_QUIET
	, D_LOCAL
	, D_WITH
	, D_ELSEWITH
	, D_ENDWITH
	, D_TRACE
#endif
} DIRECTIVE;

typedef enum {
	FB_MIXED = 0
	, FB_GLOB
	, FB_REGEX
} FOR_BUFFERS;

typedef enum {
	bak_OFF = 0
	, bak_BAK
	, bak_TILDE
	, bak_TILDE_N0
	, bak_TILDE_N
} BAK_CHOICES;

typedef enum {
	enc_LOCALE = ENUM_UNKNOWN - 1
	, enc_AUTO = ENUM_UNKNOWN
	, enc_POSIX = 0
	, enc_8BIT
#if OPT_MULTIBYTE
	, enc_UTF8
	, enc_UTF16
	, enc_UTF32
#define enc_DEFAULT enc_LOCALE
#else
#define enc_DEFAULT enc_POSIX
#endif
} ENC_CHOICES;

#define b_is_enc_AUTO(bp)    ((ENC_CHOICES) b_val(bp, VAL_FILE_ENCODING) == enc_AUTO)
#define b_is_enc_LOCALE(bp)  ((ENC_CHOICES) b_val(bp, VAL_FILE_ENCODING) == enc_LOCALE)

/*
 * True if the buffer contents are in UTF-8 (or -16, -32).
 */
#define b_is_utfXX(bp)       ((b_val(bp, VAL_FILE_ENCODING) >= enc_UTF8) \
			   || (b_val(bp, VAL_FILE_ENCODING) == enc_LOCALE \
			    && vl_encoding >= enc_UTF8))

/*
 * Resolve file-encoding and the special symbols "auto" and "locale" to
 * map to one of the "real" file-encoding values.
 */
#define buf_encoding(bp)     ((b_val(bp, VAL_FILE_ENCODING) == enc_LOCALE) \
			      ? vl_encoding \
			      : ((b_val(bp, VAL_FILE_ENCODING) == enc_AUTO) \
			          ? enc_8BIT \
			          : b_val(bp, VAL_FILE_ENCODING)))

typedef enum {
	MMQ_ANY = 0
	, MMQ_ALL
} MMQ_CHOICES;

typedef enum {
	UNI_MODE = 0
	, BUF_MODE
	, WIN_MODE
#if OPT_MAJORMODE
	, MAJ_MODE
	, SUB_MODE
#endif
	, END_MODE
} MODECLASS;

typedef enum {
	PATH_UNKNOWN = ENUM_UNKNOWN
	, PATH_END
	, PATH_FULL
	, PATH_HEAD
	, PATH_ROOT
	, PATH_SHORT
	, PATH_TAIL
} PATH_CHOICES;

typedef enum {
	PT_UNKNOWN = ENUM_UNKNOWN
	, PT_BOOL
	, PT_BUFFER
	, PT_DIR
	, PT_ENUM
	, PT_FILE
	, PT_INT
	, PT_MODE
	, PT_STR
	, PT_VAR
#if OPT_MAJORMODE
	, PT_MAJORMODE
#endif
} PARAM_TYPES;

typedef struct {
	PARAM_TYPES pi_type;
	char *pi_text;		/* prompt, if customized */
#if OPT_ENUM_MODES
	FSM_BLIST *pi_choice;	/* if pi_type==PT_ENUM, points to table */
#endif
} PARAM_INFO;

typedef enum {
	RS_AUTO = 0
	, RS_LF			/* unix */
	, RS_CR			/* mac */
	, RS_CRLF		/* dos */
} RECORD_SEP;

typedef enum {
	RP_QUICK
	, RP_SLOW
	, RP_BOTH
} READERPOLICY_CHOICES;

#if CRLF_LINES
#define RS_DEFAULT RS_CRLF
#else
#define RS_DEFAULT RS_LF
#endif

typedef enum {
	SF_NEVER = 0
	, SF_ALWAYS
	, SF_DIFFERS
	, SF_FOREIGN
	, SF_LOCAL
} SHOW_FORMAT;

/* cfg_locate options */
#define FL_EXECABLE  iBIT(0)	/* maps to X_OK */
#define FL_WRITEABLE iBIT(1)	/* maps to W_OK */
#define FL_READABLE  iBIT(2)	/* maps to R_OK */
#define FL_CDIR      iBIT(3)	/* look in current directory */
#define FL_HOME      iBIT(4)	/* look in environment $HOME */
#define FL_EXECDIR   iBIT(5)	/* look in execution directory */
#define FL_STARTPATH iBIT(6)	/* look in environment $VILE_STARTUP_PATH */
#define FL_PATH      iBIT(7)	/* look in environment $PATH */
#define FL_LIBDIR    iBIT(8)	/* look in environment $VILE_LIBDIR_PATH */

#define FL_ANYWHERE  (FL_CDIR|FL_HOME|FL_EXECDIR|FL_STARTPATH|FL_PATH|FL_LIBDIR)

/* These are the chief ways we use the cfg_locate options: */

#if SYS_MSDOS || SYS_OS2 || SYS_WINNT
#define LOCATE_SOURCE (FL_READABLE | FL_ANYWHERE)
#else
#define LOCATE_SOURCE (FL_READABLE | FL_CDIR | FL_HOME | FL_STARTPATH)
#endif

#define LOCATE_EXEC   (FL_PATH | FL_LIBDIR | FL_EXECABLE)

/* definitions for name-completion */
#define	NAMEC		name_cmpl /* char for forcing name-completion */
#define	TESTC		test_cmpl /* char for testing name-completion */

/* kbd_string options */
#define KBD_EXPAND	iBIT(0)	/* do we want to expand %, #, : */
#define KBD_QUOTES	iBIT(1)	/* do we add and delete '\' chars for the caller */
#define KBD_LOWERC	iBIT(2)	/* do we force input to lowercase */
#define KBD_UPPERC	iBIT(3)	/* do we force input to uppercase */
#define KBD_NOEVAL	iBIT(4)	/* disable 'tokval()' (e.g., from buffer) */
#define KBD_MAYBEC	iBIT(5)	/* may be completed -- or not */
#define KBD_NULLOK	iBIT(6)	/* may be empty -- or not */
#define KBD_EXPCMD	iBIT(7)	/* expand %, #, : only in shell-command */
#define KBD_SHPIPE	iBIT(8)	/* expand, assuming shell-command */
#define KBD_NOMAP	iBIT(9) /* don't permit mapping via kbd_key() */
#define KBD_EXPPAT	iBIT(10) /* expand ~ to last replacement */
#define KBD_0CHAR	iBIT(11) /* string can have embedded nulls */
#define KBD_STATED	iBIT(12) /* erasing buffer returns for prev-state */
#define KBD_CASELESS	iBIT(13) /* name-completion ignores case */
#define KBD_MAYBEC2	iBIT(14) /* completion optional if user-variable */
#define KBD_REGLUE	iBIT(15) /* glue tokens across calls */

typedef UINT KBD_OPTIONS;	/* big enough to hold bits defined above */

/* default option for 'mlreply' (used in modes.c also) */
#if !(SYS_MSDOS || SYS_OS2 || SYS_WINNT)
#define	KBD_NORMAL	KBD_EXPAND|KBD_QUOTES
#else
#define	KBD_NORMAL	KBD_EXPAND
#endif

/* This was an enum, but did not compile with TurboC */
#define WATCHREAD   iBIT(0)
#define WATCHWRITE  iBIT(1)
#define WATCHEXCEPT iBIT(2)
typedef UINT WATCHTYPE;

/* reserve space for ram-usage option */
#if OPT_HEAPSIZE
#define	LastMsgCol	(term.cols - 10)
#else
#define	LastMsgCol	(term.cols - 1)
#endif

/*
 * directions for the scan routines.
 */
#define	FORWARD	TRUE
#define REVERSE	FALSE

typedef enum {
	/* nonfatal codes */
    FIOBAD = -1			/* File I/O, truncation		*/
    ,FIOSUC			/* File I/O, success.		*/
    ,FIOEOF			/* File I/O, end of file.	*/
	/* error codes */
    ,FIONUL			/* File I/O, empty file		*/
    ,FIOFNF			/* File I/O, file not found.	*/
    ,FIOERR			/* File I/O, error.		*/
    ,FIOMEM			/* File I/O, out of memory	*/
    ,FIOABRT			/* File I/O, aborted		*/
} FIO_TYPES;

/* three flavors of insert mode	*/
/* it's FALSE, or one of:	*/
#define INSMODE_INS 1
#define INSMODE_OVR 2
#define INSMODE_RPL 3

/* kill register control -- values for kbflag */
#define KNEEDCLEAN   iBIT(0)		/* Kill register needs cleaning */
#define KYANK        iBIT(1)		/* Kill register resulted from yank */
#define KLINES       iBIT(2)		/* Kill register contains full lines */
#define KRECT        iBIT(3)		/* Kill register contains rectangle */
#define KAPPEND      iBIT(4)		/* Kill register should be appended */

/* operator types.  Needed mainly because word movement changes depending on
	whether operator is "delete" or not.  Aargh.  */
#define OPDEL 1
#define OPOTHER 2

/* popup-choices values */
#define POPUP_CHOICES_OFF      0
#define POPUP_CHOICES_IMMED    1
#define POPUP_CHOICES_DELAYED  2

/* define these so C-fence matching doesn't get confused when we're editing
	the cfence code itself */
#define L_CURLY '{'
#define R_CURLY '}'
#define L_PAREN '('
#define R_PAREN ')'
#define L_BLOCK '['
#define R_BLOCK ']'
#define L_ANGLE '<'
#define R_ANGLE '>'

/* these are the characters that are used in the expand-chars mode */
#define EXPC_THIS  '%'
#define EXPC_THAT  '#'
#define EXPC_SHELL '!'
#define EXPC_RPAT  '~'
#if OPT_VMS_PATH || OPT_MSDOS_PATH	/* ':' gets in the way of drives */
#define EXPC_TOKEN '&'
#else
#define EXPC_TOKEN ':'
#endif

/* separator used when scanning PATH environment variable */
#if SYS_VMS
#define	PATHCHR	','
#define PATHSEP '/'
#endif

#if OPT_MSDOS_PATH
#define	PATHCHR	';'
#define PATHSEP '\\'
#endif

#ifndef PATHCHR				/* e.g., UNIX */
#define	PATHCHR	':'
#define PATHSEP '/'
#endif


/* token types								*/
/* these must be kept in sync with the eval_func[] table in eval.c	*/
#define TOK_NULL        0   /* null string          */
#define TOK_QUERY       1   /* query response       */
#define TOK_BUFLINE     2   /* line from buffer     */
#define TOK_TEMPVAR     3   /* temp variables       */
#define TOK_STATEVAR    4   /* state variables      */
#define TOK_FUNCTION    5   /* functions            */
#define TOK_DIRECTIVE   6   /* macro lang directive */
#define TOK_LABEL       7   /* goto target          */
#define TOK_QUOTSTR     8   /* quoted string        */
#define TOK_LITSTR      9   /* unquoted string      */
#define	MAXTOKTYPE	9

#if OPT_VILE_ALLOC
#include <vl_alloc.h>
#endif

#if OPT_VILE_CTYPE
#include <vl_ctype.h>
#endif

#if OPT_VILE_REGEX
#define regcomp  vl_regcomp
#define regexec  vl_regexec
#include <vl_regex.h>
#endif

			/* see screen_to_bname() */
#if OPT_WIDE_CTYPES
#define	SCREEN_STRING (vl_pathn | vl_wild | vl_scrtch | vl_shpipe)
#else
#define	SCREEN_STRING (vl_pathn | vl_wild)
#endif

#define KEY_Space	' '
#define KEY_Tab		'\t'

#if OPT_KEY_MODIFY
#define isBackTab(c)	((c) == KEY_BackTab || (((c) & mod_SHIFT) != 0 && CharOf(c) == KEY_Tab))
#else
#define isBackTab(c)	((c) == KEY_BackTab)
#endif

#define	NonNull(s)	((s == 0) ? "" : s)

#define ESC		tocntrl('[')
#define BEL		tocntrl('G')	/* ascii bell character		*/
#define CONTROL_A	tocntrl('A')	/* for cntl_a attribute sequences */

#if !(SYS_MSDOS && CC_DJGPP)
/* some systems need a routine to check for interrupts.  most don't, and
 * the routine overhead can be expensive in some places
 */
# define interrupted() (am_interrupted != 0)
#endif

#define ABORTED(c) ((c) == esc_c || (c) == intrc || interrupted())

/*
 * Data for read/write/etc, hooks
 */
typedef struct {
	char proc[NBUFN];
	int latch;
} HOOK;

/*
 * Definitions for 'tbuff.c' (temporary/dynamic char-buffers)
 */
typedef	struct	vl_tbuff	{
	char *	tb_data;	/* the buffer-data */
	size_t	tb_size;	/* allocated size */
	size_t	tb_used;	/* total used in */
	size_t	tb_last;	/* last put/get index */
	int	tb_endc;
	int	tb_errs;	/* true if we copied error_val here */
	} TBUFF;

#define isTB_ERRS(p) ((p) != 0 && (p)->tb_errs)

/*
 * Definitions for 'itbuff.c' (temporary/dynamic int-buffers)
 */
typedef	struct	vl_itbuff	{
	int *	itb_data;	/* the buffer-data */
	size_t	itb_size;	/* allocated size */
	size_t	itb_used;	/* total used in */
	size_t	itb_last;	/* last put/get index */
	int	itb_endc;
	} ITBUFF;

/*
 * Primitive types
 */
typedef	int		L_NUM;		/* line-number */
typedef	int		C_NUM;		/* column-number */
typedef	struct {
    USHORT flgs;
    USHORT cook;
} L_FLAG;		/* LINE-flags */

typedef	ULONG		CMDFLAGS;	/* CMDFUNC flags */
typedef	ULONG		B_COUNT;	/* byte-count */

/*
 * Normally we build with ANSI C compilers, but allow for the possibility of
 * building with C++.  There is an incompatibility between C/C++ in the
 * treatment of const:  C++ will not export const data unless it is declared
 * extern -- for statements where the data is given for initialization.  While
 * ANSI C permits (but does not require) putting the extern there as well, some
 * compilers that we support via unproto do not allow this syntax.
 */
#ifdef __cplusplus
#define EXTERN_CONST extern const
#else
#define EXTERN_CONST const
#endif

/*
 * The Intel compiler's warnings regarding externs which have not been
 * previously declared applies to data items as well as functions.  Use these
 * macros to hide the extra declaration.
 */
#if defined(__INTEL_COMPILER)
# define DECL_EXTERN_CONST(name) extern const name; EXTERN_CONST name
# define DECL_EXTERN(name)       extern       name;              name
#else
# define DECL_EXTERN_CONST(name) EXTERN_CONST name
# define DECL_EXTERN(name)                    name
#endif

/*
 * Control structures
 */
#define	for_ever for(;;)

	/* avoid "constant-in-conditional-context */
#ifdef lint
#define one_time while(malloc(1)==0)
#else
#define one_time while(0)
#endif

/*
 * All text is kept in circularly linked lists of "LINE" structures. These
 * begin at the header line. This line is pointed to by the "BUFFER".
 * Each line contains:
 *  number of bytes in the line (the "used" size),
 *  the size of the text array,
 *  the text.
 * The end of line is not stored as a byte; it's implied. Future
 * additions may include update hints, and a list of marks into the line.
 *
 * Lines are additionally sometimes stacked in undo lists.
 */
typedef struct	LINE {
	struct LINE *l_fp;		/* Link to the next line	*/
	struct LINE *l_bp;		/* Link to the previous line	*/
	union {
		size_t	l_sze;		/* Allocated size		*/
		C_NUM	l_fo;		/* forward undo dot offs (undo only) */
	} l_s_fo;
	union {
		L_NUM	l_nmbr;		/* line-# iff b_numlines > 0	*/
		C_NUM	l_bo;		/* backward undo dot offs (undo only) */
	} l_n_bo;
	int	l_used;			/* Used size (may be negative)	*/
	union {
	    char *l_txt;		/* The data for this line	*/
	    struct LINE *l_nxt;		/* if an undo stack separator,	*/
	} lt;				/*  a pointer to the next one	*/
	union
	{
	    struct LINE	*l_stklnk;	/* Link for undo stack		*/
	    L_FLAG	l_flg;		/* flags for undo ops		*/
	} l;
#if OPT_LINE_ATTRS
	UCHAR  *l_attrs;		/* indexes into the line_attr_tbl
					   hash table */
#endif
}	LINE;

#define l_size		l_s_fo.l_sze
#define l_forw_offs	l_s_fo.l_fo
#define l_number	l_n_bo.l_nmbr
#define l_back_offs	l_n_bo.l_bo
#define l_text		lt.l_txt
#define l_nextsep	lt.l_nxt

#define l_undo_cookie	l_flg.cook
#define l_flag		l_flg.flgs

/* LINE.l_flag values */
#define LCOPIED  lBIT(0)	/* original line is already on an undo stack */
#define LGMARK   lBIT(1)	/* line matched a global scan */
#define LTRIMMED lBIT(2)	/* line doesn't have newline to display */

/* macros to ease the use of lines */
#define	for_each_line(lp,bp) for (lp = lforw(buf_head(bp)); \
					lp != buf_head(bp); \
					lp = lforw(lp))

#define l_nxtundo		l.l_stklnk

	/*
	 * Special values used in LINE.l_used
	 */
#define LINENOTREAL	((int)(-1)) /* for undo, marks an inserted line */
#define LINEUNDOPATCH	((int)(-2)) /* provides stack patching value for undo */
#define STACKSEP	((int)(-4)) /* delimit set of changes on undo stack */
#define PURESTACKSEP	((int)(-3)) /* as above, but buffer unmodified before */
					/* this change */

#define set_lforw(a,b)	lforw(a) = (b)
#define set_lback(a,b)	lback(a) = (b)
#define lforw(lp)	(lp)->l_fp
#define lback(lp)	(lp)->l_bp

	/*
	 * Macros for referencing fields in the LINE struct.
	 */
#define lgetc(lp, n)		char2int(lvalue(lp)[(n)])
#define lputc(lp, n, c)		(lvalue(lp)[(n)]=(char)(c))
#define lvalue(lp)		((lp)->l_text)
#define llength(lp)		((lp)->l_used)
#define line_length(lp)		((B_COUNT)(llength(lp))+len_rs) /* counting recordsep */

#define liscopied(lp)		((lp)->l.l_undo_cookie == current_undo_cookie)
#define lsetcopied(lp)		((lp)->l.l_undo_cookie = current_undo_cookie)
#define lsetnotcopied(lp)	((lp)->l.l_undo_cookie = 0)

#define lismarked(lp)		((lp)->l.l_flag & LGMARK)
#define lsetmarked(lp)		((lp)->l.l_flag |= LGMARK)
#define lsetnotmarked(lp)	((lp)->l.l_flag &= ~LGMARK)
#define lflipmark(lp)		((lp)->l.l_flag ^= LGMARK)

#define listrimmed(lp)		((lp)->l.l_flag & LTRIMMED)
#define lsettrimmed(lp)		((lp)->l.l_flag |= LTRIMMED)
#define lsetnottrimmed(lp)	((lp)->l.l_flag &= ~LTRIMMED)
#define lsetclear(lp)		((lp)->l.l_flag = (lp)->l.l_undo_cookie = 0)

#define lisreal(lp)		(llength(lp) >= 0)
#define lisnotreal(lp)		(llength(lp) == LINENOTREAL)
#define lislinepatch(lp)	(llength(lp) == LINEUNDOPATCH)
#define lispatch(lp)		(lislinepatch(lp))
#define lisstacksep(lp)		(llength(lp) == STACKSEP || \
					llength(lp) == PURESTACKSEP)
#define lispurestacksep(lp)	(llength(lp) == PURESTACKSEP)

/* marks are a line and an offset into that line */
typedef struct MARK {
	LINE *l;
	C_NUM o;
} MARK;

/* some macros that take marks as arguments */
#define is_at_end_of_line(m)	(m.o == llength(m.l))
#define is_empty_line(m)	(llength(m.l) == 0)
#define sameline(m1,m2)		(m1.l == m2.l)
#define samepoint(m1,m2)	(sameline(m1,m2) && (m1.o == m2.o))
#define char_at(m)		(lgetc(m.l,m.o))
#define put_char_at(m,c)	(lputc(m.l,m.o,c))
#define is_header_line(m,bp)	(m.l == buf_head(bp))
#define is_last_line(m,bp)	(lforw(m.l) == buf_head(bp))
#define is_first_line(m,bp)	(lback(m.l) == buf_head(bp))

/*
 * The starting position of a region, and the size of the region in
 * characters, is kept in a region structure.  Used by the region commands.
 */
typedef struct	{
	MARK	r_orig;			/* Origin LINE address.		*/
	MARK	r_end;			/* Ending LINE address.		*/
	C_NUM	r_leftcol;		/* Leftmost column.		*/
	C_NUM	r_rightcol;		/* Rightmost column.		*/
	B_COUNT r_size;			/* Length in characters.	*/
#if OPT_SELECTIONS
	USHORT	r_attr_id;		/* id of corresponding display  */
#endif
}	REGION;

#if OPT_COLOR || OPT_HILITEMATCH
typedef USHORT VIDEO_ATTR;		/* assume short is at least 16 bits */
#else
typedef UCHAR VIDEO_ATTR;
#endif

#define VACURS	iBIT(0)			/* cursor -- this is intentionally
					 * the same as VADIRTY. It should
					 * not be used anywhere other than
					 * in specific places in the low
					 * level drivers (e.g, x11.c).
					 */
#define VAMLFOC	iBIT(1)			/* modeline w/ focus		*/
#define VAML	iBIT(2)			/* standard mode line (no focus)*/
#define VASEL	iBIT(3)			/* selection			*/
#define	VAREV	iBIT(4)			/* reverse video		*/
#define	VAUL	iBIT(5)			/* underline			*/
#define	VAITAL	iBIT(6)			/* italics			*/
#define	VABOLD	iBIT(7)			/* bold				*/
#define VAOWNER ((VIDEO_ATTR)0x0700)	/* owner mask			*/
#define VASPCOL ((VIDEO_ATTR)0x0800)	/* specific color		*/
#define VACOLOR ((VIDEO_ATTR)0xf000)	/* color mask			*/
#define VACOL_0 (VASPCOL|0x0000)	/* color palette index 0	*/
#define VACOL_1 (VASPCOL|0x1000)	/* etc.				*/
#define VACOL_2 (VASPCOL|0x2000)
#define VACOL_3 (VASPCOL|0x3000)
#define VACOL_4 (VASPCOL|0x4000)
#define VACOL_5 (VASPCOL|0x5000)
#define VACOL_6 (VASPCOL|0x6000)
#define VACOL_7 (VASPCOL|0x7000)
#define VACOL_8 (VASPCOL|0x8000)
#define VACOL_9 (VASPCOL|0x9000)
#define VACOL_A (VASPCOL|0xA000)
#define VACOL_B (VASPCOL|0xB000)
#define VACOL_C (VASPCOL|0xC000)
#define VACOL_D (VASPCOL|0xD000)
#define VACOL_E (VASPCOL|0xE000)
#define VACOL_F (VASPCOL|0xF000)

#define VCOLORNUM(attr) ((int) ((attr) & VACOLOR) >> 12)
#define VCOLORATTR(num) ((VIDEO_ATTR) ((UINT)(num) << 12))

/* who owns an attributed region -- so we can delete them independently */
#define VOWNER(attr)	((attr) & VAOWNER)
#define VOWN_MATCHES	0x0100
#define VOWN_OPERS	0x0200
#define VOWN_SELECT	0x0300
#define VOWN_CTLA	0x0400

/* The VATTRIB macro masks out those bits which should not be considered
 * for comparison purposes
 */

#if OPT_PSCREEN
#define VADIRTY	iBIT(0)			/* cell needs to be written out */
#define VATTRIB(attr) ((VIDEO_ATTR) ((attr) & (VIDEO_ATTR) ~(VAOWNER|VADIRTY)))
#else
#define VADIRTY ((VIDEO_ATTR)0)		/* nop for all others */
#define VATTRIB(attr) ((VIDEO_ATTR) ((attr) & (VIDEO_ATTR) ~(VAOWNER)))
#endif

/* grow (or initially allocate) a vector of newsize types, pointed to by
 * ptr.  this is used primarily for resizing the screen
 * the studious will note this is a lot like realloc.   but realloc
 * doesn't guarantee to preserve contents if it fails, and this also
 * zeroes the new space.
 */
#define GROW(ptr, type, oldsize, newsize) \
{ \
	int tmpold = oldsize; \
	type *tmpp; \
	tmpp = typeallocn(type, newsize); \
	if (tmpp == NULL) \
		return FALSE; \
 \
	if (ptr) { \
		(void) memcpy((char *)tmpp, (char *)ptr, tmpold * sizeof(type)); \
		free((char *)ptr); \
	} else { \
		tmpold = 0; \
	} \
	ptr = tmpp; \
	(void) memset ((char *)(ptr+tmpold), 0, (newsize - tmpold) * sizeof(type)); \
}

/*
 * An attributed region is attached to a buffer and indicates how the
 * region should be displayed; eg. inverse video, underlined, etc.
 */

typedef struct vl_aregion {
	struct vl_aregion	*ar_next;
	REGION		ar_region;
	VIDEO_ATTR	ar_vattr;
	REGIONSHAPE	ar_shape;
#if OPT_HYPERTEXT
	char *		ar_hypercmd;
#endif
}	AREGION;

/* Hash table entry for line attribute indices. */
typedef struct vl_line_attr_entry {
	VIDEO_ATTR	vattr;
	char		in_use;
}	LINE_ATTR_ENTRY;

typedef	struct {
	char *pat;
	regexp *reg;
} REGEXVAL;

/* this is to ensure values can be of any type we wish.
   more can be added if needed.  */
union V {
	int i;
	char *p;
	REGEXVAL *r;
};

struct VAL {
	union V v;
	union V *vp;
};

typedef	struct	{
	const struct VALNAMES *names;
	struct VAL      *local;
	struct VAL      *global;
} VALARGS;

struct	BUFFER;

#define CHGD_ARGS struct BUFFER *bp, VALARGS *args, int glob_vals, int testing

typedef	int	(*ChgdFunc) ( CHGD_ARGS );

/* settable values have their names stored here, along with a synonym, and
	what type they are */
struct VALNAMES {
		const char *name;
		const char *shortname;
		char  type;
		ChgdFunc side_effect;
};
/* the values of VALNAMES->type */
#define VALTYPE_INT    0
#define VALTYPE_STRING 1
#define VALTYPE_BOOL   2
#define VALTYPE_REGEX  3
#define VALTYPE_ENUM   4
#define VALTYPE_MAJOR  5

	/*
	 * Values are either local or global. We distinguish the two cases
	 * by whether the value-pointer points into the VAL-struct or not.
	 */
#define is_local_val(lv,which)          (lv[which].vp == &(lv[which].v))
#define make_local_val(lv,which)        (lv[which].vp = &(lv[which].v))
#define make_global_val(lv,gv,which)    (lv[which].vp = &(gv[which].v))

/* these are masks for the WINDOW.w_flag hint */
#define WFFORCE iBIT(0)			/* Window needs forced reframe	*/
#define WFMOVE	iBIT(1)			/* Movement from line to line	*/
#define WFEDIT	iBIT(2)			/* Editing within a line	*/
#define WFHARD	iBIT(3)			/* Better do a full display	*/
#define WFMODE	iBIT(4)			/* Update mode line.		*/
#define WFCOLR	iBIT(5)			/* Needs a color change		*/
#define WFKILLS	iBIT(6)			/* something was deleted	*/
#define WFINS	iBIT(7)			/* something was inserted	*/
#define WFSTAT	iBIT(8)			/* Update mode line (info only).*/
#define WFSBAR	iBIT(9)			/* Update scroll bar(s)		*/

/* define indices for GLOBAL, BUFFER, WINDOW modes */
#ifdef realdef
#include "chgdfunc.h"
#endif

#if	CHECK_PROTOTYPES
	typedef long W_VALUES;
	typedef long M_VALUES;
	typedef long Q_VALUES;
	typedef long B_VALUES;
#else
#	include "nemode.h"
#endif

/* macros for setting GLOBAL modes */

#define global_g_val(which) global_g_values.gv[which].v.i
#define set_global_g_val(which,val) global_g_val(which) = val
#define global_g_val_ptr(which) global_g_values.gv[which].v.p
#define set_global_g_val_ptr(which,val) global_g_val_ptr(which) = val
#define global_g_val_rexp(which) global_g_values.gv[which].v.r
#define set_global_g_val_rexp(which,val) global_g_val_rexp(which) = val

/* these are window properties affecting window appearance _only_ */
typedef struct	{
	MARK	w_dt;		/* Line containing "."	       */
		/* i don't think "mark" needs to be here -- I think it
			could safely live only in the buffer -pgf */
#if WINMARK
	MARK	w_mk;		/* Line containing "mark"      */
#endif
	MARK	w_ld;		/* Line containing "lastdotmark"*/
	MARK	w_tld;		/* Line which may become "lastdotmark"*/
	MARK	w_ln;		/* Top line in the window (offset used in linewrap) */
#if OPT_MOUSE
	int	insmode;
#endif
	W_VALUES w_vals;
#if OPT_CACHE_VCOL
	MARK	w_left_dot;	/* nominal location of left-side of screen */
	int	w_left_col;	/* ...corresponding effective column */
	int	w_left_adj;	/* ...and margin of error */
	int	w_list_opt;	/* ...and list-mode setting */
	int	w_num_cols;	/* ...and screen-width */
#endif
} W_TRAITS;

#define global_w_val(which)             global_w_values.wv[which].v.i
#define global_w_val_ptr(which)         global_w_values.wv[which].v.p

#define set_global_w_val(which,val)     global_w_val(which) = val
#define set_global_w_val_ptr(which,val) global_w_val_ptr(which) = val

#define w_val(wp,val)                   (wp->w_values.wv[val].vp->i)
#define w_val_ptr(wp,val)               (wp->w_values.wv[val].vp->p)

#define set_w_val(wp,which,val)         w_val(wp,which) = val
#define set_w_val_ptr(wp,which,val)     w_val_ptr(wp,which) = val

#define make_local_w_val(wp,which)  \
	make_local_val(wp->w_values.wv, which)
#define make_global_w_val(wp,which)  \
	make_global_val(wp->w_values.wv, global_wvalues.wv, which)

#define is_local_w_val(wp,which)  \
	is_local_val(wp->w_values.wv,which)

#if OPT_COLOR
#define gfcolor global_g_val(GVAL_FCOLOR)
#define gbcolor global_g_val(GVAL_BCOLOR)
#define gccolor global_g_val(GVAL_CCOLOR)
#else
#define gfcolor C_WHITE
#define gbcolor C_BLACK
#endif

#if OPT_MAJORMODE
/*
 * A majormode is a special set of buffer mode values, together with other
 * values (such as filename suffixes) which are used to determine when a
 * majormode should be attached to a buffer.  We allocate the structure in two
 * levels (MAJORMODE vs MAJORMODE_LIST) to avoid having to adjust pointers
 * within the VAL arrays (M_VALUES and B_VALUES) when we add or remove new
 * majormodes.
 */
typedef struct MINORMODE {
	struct MINORMODE *sm_next;
	char             *sm_name;	/* the name of this group */
	B_VALUES          sm_vals;	/* mode-values for the group */
} MINORMODE;

typedef struct {
	char *shortname;	/* name without "mode" suffix */
	char *longname;		/* name with "mode" suffix */
	M_VALUES mm;		/* majormode-specific flags, e.g., "preamble" */
	Q_VALUES mq;		/* qualifier values such as "group" */
	MINORMODE *sm;
} MAJORMODE;

#define is_c_mode(bp) (bp->majr != 0 && !strcmp(bp->majr->shortname, "c"))
#define fix_cmode(bp,value)	/* nothing */
#define for_each_modegroup(bp,n,m,vals) \
	for (vals = get_submode_vals(bp, n = m); vals != 0; vals = get_submode_valx(bp, m, &n))
#else
#define is_c_mode(bp) (b_val(bp,MDCMOD))
#define fix_cmode(bp,value)	make_local_b_val(bp, MDCMOD), \
				set_b_val(bp, MDCMOD, value)
#define for_each_modegroup(bp,n,m,vals) vals = bp->b_values.bv;
#endif

#if SYS_UNIX && !SYS_OS2_EMX
#define CAN_CHECK_INO 1
typedef struct {
	dev_t dev;
	ino_t ino;
	int   valid;		/* are dev and ino good? */
} FUID;
#else
typedef int FUID;
#endif

/*
 * Text is kept in buffers.  A buffer header, described below, exists
 * for every buffer in the system.  The buffers are kept in a big
 * list, so that commands that search for a buffer by name can find
 * the buffer header.  There is a safe store for the dot and mark in
 * the header, but this is only valid if the buffer is not being
 * displayed (that is, if "b_nwnd" is 0).  The text for the buffer is
 * kept in a circularly linked list of lines, with a pointer to the
 * header line in "b_line" Buffers may be "Inactive" which means the
 * files associated with them have not been read in yet.  These get
 * read in at "use buffer" time.
 */

typedef	int	(*UpBuffFunc) ( struct BUFFER * );

typedef struct	BUFFER {
	MARK	b_line;		/* Link to the header LINE (offset unused) */
	struct	BUFFER *b_bufp;		/* Link to next BUFFER		*/
	MARK	*b_nmmarks;		/* named marks a-z		*/
#if OPT_SELECTIONS
	AREGION	*b_attribs;		/* attributed regions		*/
#endif
#if OPT_MAJORMODE
	MAJORMODE *majr;		/* majormode, if any */
#endif
	B_VALUES b_values;		/* buffer traits we inherit from */
					/*  global values		*/
	W_TRAITS b_wtraits;		/* saved window traits, while we're */
					/*  not displayed		*/
	B_COUNT	b_bytecount;		/* # of chars			*/
	L_NUM	b_linecount;		/* no. lines in buffer		*/
	L_NUM	b_lines_on_disk;	/* no. lines as of last read/write */
	LINE	*b_udstks[2];		/* undo stack pointers		*/
	MARK	b_uddot[2];		/* Link to "." before undoable op */
	short	b_udstkindx;		/* which of above to use	*/
	LINE	*b_udtail;		/* tail of undo backstack	*/
	LINE	*b_udlastsep;		/* last stack separator pushed	*/
	int	b_udcount;		/* how many undo's we can do	*/
	LINE	*b_LINEs;		/* block-malloced LINE structs	*/
	LINE	*b_LINEs_end;		/* end of	"	"	*/
	LINE	*b_freeLINEs;		/* list of free "	"	*/
	UCHAR	*b_ltext;		/* block-malloced text		*/
	UCHAR	*b_ltext_end;		/* end of block-malloced text	*/
	LINE	*b_ulinep;		/* pointer at 'Undo' line	*/
	int	b_active;		/* window activated flag	*/
	int	b_refcount;		/* counts levels of source'ing	*/
	UINT	b_nwnd;			/* Count of windows on buffer	*/
	UINT	b_flag;			/* Flags			*/
	short	b_inuse;		/* nonzero if executing macro	*/
	short	b_acount;		/* auto-save count		*/
	const char *b_recordsep_str;	/* string for recordsep		*/
	int	b_recordsep_len;	/* ...its length		*/
	char	*b_fname;		/* File name			*/
	int	b_fnlen;		/* length of filename		*/
	char	b_bname[NBUFN];		/* Buffer name			*/
#if OPT_CURTOKENS
	struct VAL buf_fname_expr;	/* $buf-fname-expr		*/
#endif
#if OPT_ENCRYPT
	char	b_cryptkey[NKEYLEN];	/* encryption key		*/
#endif
#if SYS_UNIX
	FUID	b_fileuid;		/* file unique id (dev/ino on unix) */
	FUID	b_fileuid_at_warn;	/* file unique id when user warned */
#endif
#ifdef	MDCHK_MODTIME
	time_t	b_modtime;		/* file's last-modification time */
	time_t	b_modtime_at_warn;	/* file's modtime when user warned */
#endif
#if	OPT_NAMEBST
	TBUFF	*b_procname;		/* full procedure name		*/
#endif
#if	OPT_UPBUFF
	UpBuffFunc b_upbuff;		/* call to recompute		*/
	UpBuffFunc b_rmbuff;		/* call on removal		*/
#endif
#if	OPT_B_LIMITS
	int	b_lim_left;		/* extra left-margin (cf:show-reg) */
#endif
	struct	BUFFER *b_relink;	/* Link to next BUFFER (sorting) */
	int	b_created;
	int	b_last_used;
#if OPT_HILITEMATCH
	USHORT	b_highlight;
#endif
#if OPT_MULTIBYTE
	BOM_CODES implied_BOM;		/* fix decode/encode if BOM missing */
	UINT	*decode_utf_buf;	/* workspace for decode_charset() */
	UINT	decode_utf_len;
	char	*encode_utf_buf;	/* workspace for encode_charset() */
	UINT	encode_utf_len;
#endif
#if OPT_PERL || OPT_TCL || OPT_PLUGIN
	void *	b_api_private;		/* pointer to private perl, tcl, etc.
					   data */
#endif
#if COMPLETE_FILES || COMPLETE_DIRS
	char ** b_index_list;		/* array to index into this buffer */
	size_t	b_index_size;
	int	b_index_counter;	/* iterator for the array */
#endif
}	BUFFER;

/*
 * Special symbols for scratch-buffer names.
 */
#define	SCRTCH_LEFT  "["
#define	SCRTCH_RIGHT "]"
#define	SHPIPE_LEFT  "!"

/* warning:  code in file.c and fileio.c knows how long the shell, pipe, and
	append prefixes are (e.g. fn += 2 when appending) */
#define	isShellOrPipe(s)  ((s)[0] == SHPIPE_LEFT[0])
#define	isInternalName(s) (isShellOrPipe(s) || is_internalname(s))
#define	isAppendToName(s) ((s)[0] == '>' && (s)[1] == '>')

/* shift-commands can be repeated when typed on :-command */
#define isRepeatable(c)   ((c) == '<' || (c) == '>')

/*
 * Macros for manipulating buffer-struct members.
 */
#define	for_each_buffer(bp) for (bp = bheadp; bp; bp = bp->b_bufp)

#define gbl_b_val(which)                 global_b_values.bv[which]

#define global_b_val(which)              gbl_b_val(which).v.i
#define global_b_val_ptr(which)          gbl_b_val(which).v.p
#define global_b_val_rexp(which)         gbl_b_val(which).v.r

#define set_global_b_val(which,val)      global_b_val(which) = val
#define set_global_b_val_ptr(which,val)  global_b_val_ptr(which) = val
#define set_global_b_val_rexp(which,val) global_b_val_rexp(which) = val

#define any_b_val(bp,which)              (bp)->b_values.bv[which]

#define b_val(bp,which)                  (any_b_val(bp,which).vp->i)
#define b_val_ptr(bp,which)              (any_b_val(bp,which).vp->p)
#define b_val_rexp(bp,which)             (any_b_val(bp,which).vp->r)

#define set_b_val(bp,which,val)          b_val(bp,which) = val
#define set_b_val_ptr(bp,which,val)      b_val_ptr(bp,which) = val
#define set_b_val_rexp(bp,which,val)     b_val_rexp(bp,which) = val

#define window_b_val(wp,val) \
	((wp != 0 && wp->w_bufp != 0) \
		? b_val(wp->w_bufp,val) \
		: global_b_val(val))

#define make_local_b_val(bp,which)  \
		make_local_val(bp->b_values.bv, which)
#define make_global_b_val(bp,which)  \
		make_global_val(bp->b_values.bv, global_b_values.bv, which)

#define is_local_b_val(bp,which)  \
	is_local_val(bp->b_values.bv,which)

#define is_empty_buf(bp) (lforw(buf_head(bp)) == buf_head(bp))

#define b_dot     b_wtraits.w_dt
#if WINMARK
#define b_mark    b_wtraits.w_mk
#endif
#define b_lastdot b_wtraits.w_ld
#define b_tentative_lastdot b_wtraits.w_tld
#define b_wline   b_wtraits.w_ln

#if OPT_CASELESS
#define eql_bname(bp,name) !stricmp(bp->b_bname, name)
#else
#define eql_bname(bp,name) !strcmp(bp->b_bname, name)
#endif

#define COPY_B_VAL(dst,src,val) \
	if (is_local_b_val(src, val)) { \
	    make_local_b_val(dst, val); \
	    set_b_val(dst, val, b_val(src, val)); \
	}

/* values for b_flag */
#define BFINVS     iBIT(0)	/* Internal invisible buffer	*/
#define BFCHG      iBIT(1)	/* Changed since last write	*/
#define BFDIRS     iBIT(2)	/* set for directory-buffers */
#define BFSCRTCH   iBIT(3)	/* scratch -- gone on last close */
#define BFARGS     iBIT(4)	/* set for ":args" buffers */
#define BFEXEC     iBIT(5)	/* set for ":source" buffers */
#define BFIMPLY    iBIT(6)	/* set for implied-# buffers */
#define BFSIZES    iBIT(7)	/* set if byte/line counts current */
#define BFUPBUFF   iBIT(8)	/* set if buffer should be updated */
#define BFRCHG     iBIT(9)	/* Changed since last reset of this flag*/
#define BFISREAD   iBIT(10)	/* set if we are reading data into buffer */
#define BFREGD     iBIT(11)	/* set if file path written to registry
				 * (winvile feature)
				 */

/* macros for manipulating b_flag */
#define b_is_set(bp,flags)        (((bp)->b_flag & (flags)) != 0)
#define b_is_argument(bp)         b_is_set(bp, BFARGS)
#define b_is_changed(bp)          b_is_set(bp, BFCHG)
#define b_is_counted(bp)          b_is_set(bp, BFSIZES)
#define b_is_directory(bp)        b_is_set(bp, BFDIRS)
#define b_is_implied(bp)          b_is_set(bp, BFIMPLY)
#define b_is_invisible(bp)        b_is_set(bp, BFINVS)
#define b_is_obsolete(bp)         b_is_set(bp, BFUPBUFF)
#define b_is_reading(bp)          b_is_set(bp, BFISREAD)
#define b_is_recentlychanged(bp)  b_is_set(bp, BFRCHG)
#define b_is_scratch(bp)          b_is_set(bp, BFSCRTCH)
#define b_is_temporary(bp)        b_is_set(bp, BFINVS|BFSCRTCH)
/*
 * don't write file paths to registry if already written _or_ if dealing
 * with a non-file and/or transient buffer.
 */
#define b_is_registered(bp)        b_is_set(bp, \
                                       BFINVS|BFSCRTCH|BFREGD|BFEXEC|BFDIRS)

#define b_set_flags(bp,flags)     (bp)->b_flag |= (flags)
#define b_set_changed(bp)         b_set_flags(bp, BFCHG)
#define b_set_counted(bp)         b_set_flags(bp, BFSIZES)
#define b_set_invisible(bp)       b_set_flags(bp, BFINVS)
#define b_set_obsolete(bp)        b_set_flags(bp, BFUPBUFF)
#define b_set_reading(bp)         b_set_flags(bp, BFISREAD)
#define b_set_recentlychanged(bp) b_set_flags(bp, BFRCHG)
#define b_set_scratch(bp)         b_set_flags(bp, BFSCRTCH)
#define b_set_registered(bp)      b_set_flags(bp, BFREGD)

#define b_clr_flags(bp,flags)     (bp)->b_flag &= ~(flags)
#define b_clr_changed(bp)         b_clr_flags(bp, BFCHG)
#define b_clr_counted(bp)         b_clr_flags(bp, BFSIZES)
#define b_clr_invisible(bp)       b_clr_flags(bp, BFINVS)
#define b_clr_obsolete(bp)        b_clr_flags(bp, BFUPBUFF)
#define b_clr_reading(bp)         b_clr_flags(bp, BFISREAD)
#define b_clr_recentlychanged(bp) b_clr_flags(bp, BFRCHG)
#define b_clr_scratch(bp)         b_clr_flags(bp, BFSCRTCH)
#define b_clr_registered(bp)      b_clr_flags(bp, BFREGD)

#if OPT_HILITEMATCH
#define b_match_attrs_dirty(bp)	(bp)->b_highlight |= HILITE_DIRTY
#else
#define b_match_attrs_dirty(bp)
#endif

#if OPT_B_LIMITS
#define b_left_margin(bp)       bp->b_lim_left
#define b_set_left_margin(bp,n) b_left_margin(bp) = n
#else
#define b_left_margin(bp)       0
#define b_set_left_margin(bp,n)
#endif

#if OPT_HILITEMATCH
#define HILITE_ON	1
#define HILITE_DIRTY	2
#endif

#define len_record_sep(bp)	((bp)->b_recordsep_len)
#define get_record_sep(bp)	((bp)->b_recordsep_str)

/* macro for iterating over the marks associated with the current buffer */

#if OPT_PERL || OPT_TCL || OPT_PLUGIN
extern MARK *api_mark_iterator(BUFFER *bp, int *iter);
#define api_do_mark_iterate_helper(mp, statement)	\
	{						\
	    int dmi_iter = 0;				\
	    while ((mp = api_mark_iterator(curbp, &dmi_iter)) != NULL) { \
		statement				\
	    }						\
	}
#else
#define api_do_mark_iterate_helper(mp, statement)
#endif

#if OPT_VIDEO_ATTRS
#define do_mark_iterate(mp, statement)			\
    do {						\
	struct MARK *mp;				\
	int	     idx;				\
	AREGION     *dmi_ap = curbp->b_attribs;		\
	if (curbp->b_nmmarks != NULL)			\
	    for (idx=0; idx < NMARKS; idx++) {		\
		mp = &(curbp->b_nmmarks[idx]);		\
		statement				\
	    }						\
	if (dmi_ap != NULL) {				\
	    while (dmi_ap != NULL) {			\
		mp = &dmi_ap->ar_region.r_orig;		\
		statement				\
		mp = &dmi_ap->ar_region.r_end;		\
		statement				\
		dmi_ap = dmi_ap->ar_next;		\
	    }						\
	    sel_reassert_ownership(curbp);		\
	}						\
	api_do_mark_iterate_helper(mp, statement)	\
    } one_time
#else /* OPT_VIDEO_ATTRS */
#define do_mark_iterate(mp, statement)			\
    do {						\
	struct MARK *mp;				\
	if (curbp->b_nmmarks != NULL) {			\
	    int idx;					\
	    for (idx=0; idx < NMARKS; idx++) {		\
		mp = &(curbp->b_nmmarks[idx]);		\
		statement				\
	    }						\
	}						\
	api_do_mark_iterate_helper(mp, statement)	\
    } one_time
#endif /* OPT_VIDEO_ATTRS */

/*
 * There is a window structure allocated for every active display window. The
 * windows are kept in a big list, in top to bottom screen order, with the
 * listhead at "wheadp". Each window contains its own values of dot and mark.
 * The flag field contains some bits that are set by commands to guide
 * redisplay; although this is a bit of a compromise in terms of decoupling,
 * the full blown redisplay is just too expensive to run for every input
 * character.
 */

#define WINDOW	vile_WINDOW		/* avoid conflict with curses.h */

typedef struct	WINDOW {
	W_TRAITS w_traits;		/* features of the window we should */
					/* remember between displays	*/
	struct	WINDOW *w_wndp;		/* Next window			*/
	BUFFER	*w_bufp;		/* Buffer displayed in window	*/
	int	w_toprow;		/* Origin 0 top row of window	*/
	int	w_ntrows;		/* # of rows of text in window	*/
	int	w_force;		/* If non-zero, forcing row.	*/
	USHORT	w_flag;			/* Flags.			*/
	ULONG	w_split_hist;		/* how to recombine deleted windows */
	int	w_tabstop;		/* vtset's latest tabstop value */
#ifdef WMDRULER
	int	w_ruler_line;
	int	w_ruler_col;
#endif
#if OPT_PERL || OPT_TCL
	ULONG	w_id;			/* Unique window id */
#endif
}	WINDOW;

#define is_visible_window(wp) ((wp)->w_toprow >= 0)
#define is_fake_window(wp) (!(is_visible_window(wp)))

#define	for_each_window(wp) for (wp = wheadp; wp; wp = wp->w_wndp)
#define for_each_visible_window(wp) \
		for_each_window(wp) if (is_visible_window(wp))

#define w_dot     w_traits.w_dt
#if WINMARK
#define w_mark    w_traits.w_mk
#endif
#define w_lastdot w_traits.w_ld
#define w_tentative_lastdot w_traits.w_tld
#define w_line    w_traits.w_ln
#define w_values  w_traits.w_vals

#define mode_row(wp)	((wp)->w_toprow + (wp)->w_ntrows)
#define	buf_head(bp)	((bp)->b_line.l)
#define	win_head(wp)	buf_head((wp)->w_bufp)

#define DOT curwp->w_dot
#if OPT_MOUSE
#define insertmode (curwp->w_traits.insmode)
#endif /* OPT_MOUSE */
#if WINMARK
#define MK curwp->w_mark
#else
#define MK Mark
#endif

	/* we use left-margin for protecting the prefix-area of [Registers]
	 * from cut/paste selection.
	 */
#define w_left_margin(wp) b_left_margin(wp->w_bufp)

	/* tputs uses a 3rd parameter (a function pointer).  We're stuck with
	 * making ttputc and term.putch the same type.
	 */
#ifdef OUTC_RETURN
#define OUTC_DCL int
#define OUTC_RET return
#else
#define OUTC_DCL void
#define OUTC_RET (void)
#endif

#ifndef OUTC_ARGS
#define OUTC_ARGS int c
#endif

/*
 * The editor communicates with the display using a high level interface. A
 * "TERM" structure holds useful variables, and indirect pointers to routines
 * that do useful operations. The low level get and put routines are here too.
 * This lets a terminal, in addition to having non standard commands, have
 * funny get and put character code too. The calls might get changed to
 * "termp->t_field" style in the future, to make it possible to run more than
 * one terminal type.
 */
typedef struct	{
	int	maxrows;		/* max rows count		*/
	int	rows;			/* current row count		*/
	int	maxcols;		/* max column count		*/
	int	cols;			/* current column count		*/
	ENC_CHOICES encoding;		/* tell what the display can do	*/
	void	(*open) (void);		/* Open terminal at the start.	*/
	void	(*close) (void);	/* Close terminal at end.	*/
	void	(*kopen) (void);	/* keyboard open		*/
	void	(*kclose) (void);	/* keyboard close		*/
	void	(*clean) (int f);	/* cleanup before shell-out	*/
	void	(*unclean) (void);	/* cleanup after shell-out	*/
	void	(*openup) (void);	/* open new line for prompt	*/
	int	(*getch) (void);	/* Get character from keyboard. */
	OUTC_DCL(*putch) (int c);	/* Put character to display.	*/
	int	(*typahead) (void);	/* character ready?		*/
	void	(*flush) (void);	/* Flush output buffers.	*/
	void	(*curmove) (int row, int col); /* Move the cursor, origin 0. */
	void	(*eeol) (void);		/* Erase to end of line.	*/
	void	(*eeop) (void);		/* Erase to end of page.	*/
	void	(*beep) (void);		/* Beep.			*/
	void	(*rev) (UINT f);	/* set reverse video state	*/
	int	(*setdescrip) (const char *f); /* reset display description */
	void	(*setfore) (int f);	/* set foreground color		*/
	void	(*setback) (int b);	/* set background color		*/
	void	(*setpal) (const char *p); /* set color palette	*/
	void	(*setccol) (int c);	/* set cursor color		*/
	void	(*scroll) (int from, int to, int n); /* scroll region	*/
	void	(*pflush) (void);	/* really flush			*/
	void	(*icursor) (int c);	/* set cursor shape for insertion */
	void	(*set_title) (const char *t); /* set window title	*/
	int	(*watchfd)(int, WATCHTYPE, long *);
					/* Watch a file descriptor for
					   input; execute associated
					   command when input is present*/
	void	(*unwatchfd)(int, long);
					/* Don't watch file descriptor	*/
	void	(*cursorvis)(int flag);	/* hide/show cursor		*/
	/* mouse interface, e.g., for xterm and clones */
	void	(*mopen) (void);	/* mouse open			*/
	void	(*mclose) (void);	/* mouse close			*/
	void	(*mevent) (void);	/* mouse event			*/
}	TERM;

#define term_is_utfXX()         ((int) term.encoding >= enc_UTF8)

#if DISP_CURSES && defined(HAVE_ADDNWSTR)
#define WIDE_CURSES 1
#else
#define WIDE_CURSES 0
#endif

#if OPT_MULTIBYTE && (DISP_TERMCAP || WIDE_CURSES || DISP_X11 || (DISP_NTWIN && defined(UNICODE)))
typedef USHORT VIDEO_TEXT;
typedef USHORT VIDEO_CHAR;
#else
typedef UCHAR VIDEO_TEXT;
typedef char  VIDEO_CHAR;
#endif

#define VIDEO_MIN 4

typedef struct  VIDEO {
	UINT	v_flag;			/* Flags */
#if	OPT_COLOR
	int	v_fcolor;		/* current forground color */
	int	v_bcolor;		/* current background color */
	int	v_rfcolor;		/* requested forground color */
	int	v_rbcolor;		/* requested background color */
#endif
#if	OPT_VIDEO_ATTRS
	VIDEO_ATTR *v_attrs;		/* screen data attributes */
#endif
	/* allocate 4 bytes here, and malloc 4 bytes less than we need,
		to keep malloc from rounding up. */
	VIDEO_TEXT v_text[VIDEO_MIN];	/* Screen data. */
}	VIDEO;

#define VideoText(vp) (vp)->v_text
#define VideoAttr(vp) (vp)->v_attrs

#if OPT_COLOR
#define CurFcolor(vp) (vp)->v_fcolor
#define CurBcolor(vp) (vp)->v_bcolor
#define ReqFcolor(vp) (vp)->v_rfcolor
#define ReqBcolor(vp) (vp)->v_rbcolor
#else
#define CurFcolor(vp) gfcolor
#define CurBcolor(vp) gbcolor
#define ReqFcolor(vp) gfcolor
#define ReqBcolor(vp) gbcolor
#endif

#define VFCHG	iBIT(0)			/* Changed flag			*/
#define	VFEXT	iBIT(1)			/* extended (beyond column 80)	*/
#define	VFREV	iBIT(2)			/* reverse video status		*/
#define	VFREQ	iBIT(3)			/* reverse video request	*/
#define	VFCOL	iBIT(4)			/* color change requested	*/

/*
 * Menus are implemented by passing a string to a given callback function:
 */
typedef void (*ActionFunc) (char *);

/* Commands are represented as CMDFUNC structures, which contain a
 *	pointer to the actual function, and flags which help to classify it.
 *	(things like is it a MOTION, can it be UNDOne)
 *
 *	These structures are generated automatically from the cmdtbl file,
 *	and can be found in the file nefunc.h
 */
#define CMD_ARGS int f, int n

typedef	int	(*CmdFunc) (int f, int n);

typedef	struct {
#ifdef CC_CANNOT_INIT_UNIONS
	void	*c_union;
#define CMD_U_FUNC(p) (CmdFunc)((p)->c_union)
#define CMD_U_BUFF(p) (BUFFER*)((p)->c_union)
#define CMD_U_PERL(p) (void  *)((p)->c_union)
#define INIT_UNION(n) n
#else	/* C can init unions */
	union {
		CmdFunc c_func;
		BUFFER *c_buff;
#if OPT_PERL
		void *c_perl;	/* Perl 5 'AV' type */
#endif
	} cu;
	/* using the union gives us some type-checking and eliminates casts */
#define CMD_U_FUNC(p) ((p)->cu.c_func)
#define CMD_U_BUFF(p) ((p)->cu.c_buff)
#define CMD_U_PERL(p) ((p)->cu.c_perl)
#define INIT_UNION(n) {n}
#endif /* CC_CANNOT_INIT_UNIONS */
	CMDFLAGS c_flags;	/* what sort of command is it? */
#if OPT_MACRO_ARGS
	PARAM_INFO *c_args;	/* if nonnull, lists types of parameters */
#endif
#if OPT_TRACE
	const char *c_name;
#endif
#if OPT_ONLINEHELP
	const char *c_help;	/* short help message for the command */
#endif
}	CMDFUNC;

/*
 * Other useful argument templates
 */
#define EOL_ARGS  const char * buffer, size_t cpos, int c, int eolchar
#define DONE_ARGS KBD_OPTIONS flags, int c, char *buf, size_t *pos
#define LIST_ARGS int flag, void *ptr
#define REGN_ARGS void *flagp, int l, int r

#define PASS_DONE_ARGS flags, c, buf, pos

typedef	int	(*OpsFunc) (void);

/* when referencing a command by name (e.g ":e file") it is looked up in
 *	the nametbl, which is an array of NTAB structures, containing the
 *	name, and a pointer to the CMDFUNC structure.  There can be several
 *	entries pointing at a single CMDFUNC, since a command might have
 *	several synonymous names.
 *
 *	The nametbl array is generated automatically from the cmdtbl file,
 *	and can be found in the file nename.h
 */
typedef struct {
	const char *n_name;
	const CMDFUNC *n_cmd;
}	NTAB;

/*
 * a binary search tree of the above structure.  we use this so that we can
 * add in procedures as they are created.
 */
typedef struct {
	const char *bi_key;		/* the name of the command	*/
	const CMDFUNC *n_cmd;		/* command details		*/
	UCHAR n_flags;			/* flags (below)		*/
#define	NBST_READONLY	1		/* for builtin functions	*/
#define	NBST_DONE	2		/* temporary flag used by
					   bind.c:makebindlist()	*/
}	NBST_DATA;


/* when a command is referenced by bound key (like h,j,k,l, or "dd"), it
 *	is looked up one of two ways: single character 8-bit ascii commands (by
 *	far the majority) are simply indexed into an array of CMDFUNC pointers.
 *	Other commands (those with ^A, ^X, or SPEC prefixes) are searched for
 *	in a binding table, made up of KBIND structures.  This structure
 *	contains the command code, and again, a pointer to the CMDFUNC
 *	structure for the command
 *
 *	The asciitbl array, and the kbindtbl array are generated automatically
 *	from the cmdtbl file, and can be found in the file nebind.h
 */

#if OPT_REBIND
#define KBIND_LINK(code) ,code
#else
#define KBIND_LINK(code) /*nothing*/
#endif

typedef struct  k_bind {
	int	k_code;			/* Key code			*/
	const CMDFUNC *k_cmd;
#if OPT_REBIND
	struct  k_bind *k_link;
#endif
}	KBIND;

typedef struct {
	const char *bufname;
	const CMDFUNC **kb_normal;
	KBIND *kb_special;
#if OPT_REBIND
	KBIND *kb_extra;
#endif
} BINDINGS;

#define DefaultKeyBinding(c) kcod2fnc(&dft_bindings, c)
#define InsertKeyBinding(c)  kcod2fnc(&ins_bindings, c)
#define CommandKeyBinding(c) kcod2fnc(&cmd_bindings, c)
#define SelectKeyBinding(c)  kcod2fnc(&sel_bindings, c)

/* These are the flags which can appear in the CMDFUNC structure, describing a
 * command.
 */
#define NONE    0L
#define cmdBIT(n) lBIT(n)	/* ...to simplify typing */
/* bits 0-12 */
#define UNDO    cmdBIT(0)	/* command is undo-able, so clean up undo lists */
#define REDO    cmdBIT(1)	/* command is redo-able, record it for dotcmd */
#define MOTION  cmdBIT(2)	/* command causes motion, okay after operator cmds */
#define FL      cmdBIT(3)	/* if command causes motion, opers act on full lines */
#define ABSM    cmdBIT(4)	/* command causes absolute (i.e. non-relative) motion */
#define GOAL    cmdBIT(5)	/* column goal should be retained */
#define GLOBOK  cmdBIT(6)	/* permitted after global command */
#define OPER    cmdBIT(7)	/* function is an operator, affects a region */
#define LISTED  cmdBIT(8)	/* internal use only -- used in describing
				 * bindings to only describe each once */
#define NOMOVE  cmdBIT(9)	/* dot doesn't move (although address may be used) */
#define VIEWOK  cmdBIT(10)	/* command is okay in view mode, even though it
				 * _may_ be undoable (macros and maps) */
#define VL_RECT cmdBIT(11)	/* motion causes rectangular operation */
#define MINIBUF cmdBIT(12)	/* may use in minibuffer edit */

/* These flags are 'ex' argument descriptors, adapted from elvis.  Not all are
 * used or honored or implemented.
 */
#define argBIT(n) cmdBIT(n+13)	/* ...to simplify adding bits */
/* bits 13-27 */
#define FROM    argBIT(0)	/* allow a linespec */
#define TO      argBIT(1)	/* allow a second linespec */
#define BANG    argBIT(2)	/* allow a ! after the command name */
#define EXTRA   argBIT(3)	/* allow extra args after command name */
#define XFILE   argBIT(4)	/* expand wildcards in extra part */
#define NOSPC   argBIT(5)	/* no spaces allowed in the extra part */
#define DFLALL  argBIT(6)	/* default file range is 1,$ */
#define DFLNONE argBIT(7)	/* no default file range */
#define NODFL   argBIT(8)	/* do not default to the current file name */
#define EXRCOK  argBIT(9)	/* can be in a .exrc file */
#define VI_NL   argBIT(10)	/* if !exmode, then write a newline first */
#define PLUS    argBIT(11)	/* allow a line number, as in ":e +32 foo" */
#define ZERO    argBIT(12)	/* allow 0 to be given as a line number */
#define OPTREG  argBIT(13)	/* allow optional register-name */
#define USEREG  argBIT(14)	/* expect register-name */
#define FILES   (XFILE | EXTRA)	/* multiple extra files allowed */
#define WORD1   (EXTRA | NOSPC)	/* one extra word allowed */
#define FILE1   (FILES | NOSPC)	/* 1 file allowed, defaults to current file */
#define NAMEDF  (FILE1 | NODFL)	/* 1 file allowed, defaults to "" */
#define NAMEDFS (FILES | NODFL)	/* multiple files allowed, default is "" */
#define RANGE   (FROM  | TO)	/* range of linespecs allowed */

/* these flags determine the type of cu.* */
#define typBIT(n) cmdBIT(n+28)	/* ...to simplify adding bits */
/* bits 27-28 */
#define CMD_FUNC 0L		/* this is the default (CmdFunc) */
#define CMD_PROC typBIT(0)	/* named procedure (BUFFER *) */
#define CMD_PERL typBIT(1)	/* perl subroutine (AV *) */
#define CMD_TYPE (CMD_PROC | CMD_PERL) /* type mask */

#define SPECIAL_BANG_ARG -42	/* arg passed as 'n' to functions which
					were invoked by their "xxx!" name */

/* definitions for fileio.c's ffXXX() functions */
typedef enum {
	file_is_closed
	,file_is_unbuffered
	,file_is_external
	,file_is_pipe
	,file_is_internal
	,file_is_new          /* winvile-specific functionality */
} FFType;

/* definitions for 'mlreply_file()' and other filename-completion */
#define	FILEC_WRITE    1
#define	FILEC_WRITE2   2
#define	FILEC_UNKNOWN  3
#define	FILEC_READ     4
#define	FILEC_REREAD   5

#define	FILEC_PROMPT   8	/* always prompt (never from screen) */
#define	FILEC_EXPAND   16	/* allow glob-expansion to multiple files */

#ifndef P_tmpdir		/* not all systems define this */
#if SYS_MSDOS || SYS_OS2 || SYS_WINNT
#define P_tmpdir ""
#endif
#if SYS_UNIX
#define P_tmpdir "/usr/tmp"
#endif
#if SYS_VMS
#define P_tmpdir "sys$scratch:"
#endif
#endif	/* P_tmpdir */

#undef TMPDIR

#if OPT_EVAL
#define TMPDIR get_directory()
#else
#define TMPDIR P_tmpdir		/* defined in <stdio.h> */
#endif	/* OPT_EVAL */

/*	The editor holds deleted text chunks in the KILL registers. The
	kill registers are logically a stream of ascii characters, however
	due to unpredictable size, are implemented as a linked
	list of chunks. (The d_ prefix is for "deleted" text, as k_
	was taken up by the keycode structure.)
*/

typedef	struct KILL {
	struct KILL *d_next;	/* link to next chunk, NULL if last */
	UCHAR d_chunk[KBLOCK];	/* deleted text */
} KILL;

typedef struct KILLREG {
	struct KILL *kbufp;	/* current kill register chunk pointer */
	struct KILL *kbufh;	/* kill register header pointer	*/
	unsigned kused;		/* # of bytes used in kill last chunk	*/
	C_NUM kbwidth;		/* width of chunk, if rectangle */
	USHORT kbflag;		/* flags describing kill register	*/
} KILLREG;

#define	KbSize(i,p)	((p->d_next != 0) ? KBLOCK : kbs[i].kused)

#ifndef NULL
# define NULL 0
#endif

/*
 * General purpose includes
 */

#include <stdarg.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#if SYS_VMS
#include <unixio.h>
#include <unixlib.h>
#include <file.h>	/* aka <sys/file.h> */
#include <rms.h>	/* required to compile nefsms.h */
#define stricmp(a,b)	strcasecmp(a,b)
#define strnicmp(a,b,n)	strncasecmp(a,b,n)
#elif OPT_VMS_PATH
#define strnicmp(a,b,n)	strncasecmp(a,b,n)
#endif

#ifdef HAVE_STDDEF_H
#include <stddef.h>
#endif

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#else
extern void exit (int code);
extern void _exit (int code);
#endif	/* HAVE_STDLIB_H */

/* array/table size */
#define	TABLESIZE(v)	(sizeof(v)/sizeof(v[0]))

#define PERCENT(num,den) ((den) ? (int)((100.0 * (num))/(den)) : 100)

/* Quiet compiler warnings on places where we're being blamed incorrectly,
 * e.g., for casting away const, or for alignment problems.  It's generally
 * legal in c89 to cast a pointer to long w/o loss of precision.
 *
 * FIXME: c99 may require a wider type.
 */
#ifdef __GNUC__
#define TYPECAST(type,ptr) (type*)((long)(ptr))
#else
#define TYPECAST(type,ptr) (type*)(ptr)
#endif

#if defined(VILE_ERROR_ABORT)
extern void ExitProgram(int code);
#endif

#if SYS_WINNT && defined(VILE_OLE) && DISP_NTWIN
#define ExitProgram(code)   oleauto_exit(code)
#else
#if !defined(VILE_ERROR_ABORT)
#define	ExitProgram(code)	exit(code)
#endif
#endif

/*
 * We cannot define these in config.h, since they require parameters to be
 * passed (that's non-portable).
 */
#ifdef __cplusplus
#undef GCC_PRINTF
#undef GCC_NORETURN
#undef GCC_UNUSED
#endif

#ifndef GCC_PRINTFLIKE
#ifdef GCC_PRINTF
#define GCC_PRINTFLIKE(fmt,var) __attribute__((format(printf,fmt,var)))
#else
#define GCC_PRINTFLIKE(fmt,var) /*nothing*/
#endif
#endif /* GCC_PRINTFLIKE */

#ifndef	GCC_NORETURN
#define	GCC_NORETURN /* nothing */
#endif

#ifndef	GCC_UNUSED
#define	GCC_UNUSED /* nothing */
#endif

#if 1				/* requires a patch to gcc */
#define VILE_PRINTF(fmt,var) GCC_PRINTFLIKE(fmt,var)
#else
#define VILE_PRINTF(fmt,var) /*nothing*/
#endif

#ifdef HAVE_SELECT
# ifdef HAVE_SELECT_H
# include <select.h>
# endif
# ifdef USE_SYS_SELECT_H
# include <sys/select.h>
# endif
#endif

#ifdef HAVE_UTIME_H
# include <utime.h>
#endif

#ifdef HAVE_SYS_UTIME_H
# include <sys/utime.h>
#endif

/*
 * If we have va_copy(), use it.
 */
#if defined(va_copy) || defined(HAVE_VA_COPY)
#define begin_va_copy(dst,src)	va_copy(dst, src)
#define end_va_copy(dst)	va_end(dst)
#else
#define begin_va_copy(dst,src) (dst) = (src)
#define end_va_copy(dst)	/* nothing */
#endif

/*
 * We will make the default unix globbing code use 'echo' rather than our
 * internal globber if we do not configure the 'glob' string-mode.
 */
#if SYS_MSDOS || SYS_OS2 || SYS_WINNT
# define UNIX_GLOBBING OPT_GLOB_ENVIRON
#endif

#if SYS_UNIX && defined(GVAL_GLOB) && !OPT_VMS_PATH
# define UNIX_GLOBBING 1
#endif

#ifndef UNIX_GLOBBING
# define UNIX_GLOBBING 0
#endif

/*
 * Debugging/memory-leak testing
 */

#ifndef	DOALLOC		/* record info for 'show_alloc()' */
#define	DOALLOC		0
#endif
#ifndef	USE_DBMALLOC	/* test malloc/free/strcpy/memcpy, etc. */
#define	USE_DBMALLOC	0
#endif
#ifndef	USE_DMALLOC	/* test malloc/free/strcpy/memcpy, etc. */
#define	USE_DMALLOC	0
#endif
#ifndef	USE_MPATROL	/* test malloc/free/strcpy/memcpy, etc. */
#define	USE_MPATROL	0
#endif
#ifndef	NO_LEAKS	/* free permanent memory, analyze leaks */
#define	NO_LEAKS	0
#endif
#ifndef TEST_DOS_PIPES
#define TEST_DOS_PIPES	0
#endif

/* heap size tracking */
#if	OPT_HEAPSIZE
#undef	realloc
#define	realloc	track_realloc
#undef	calloc
#define	calloc(n,m)	track_malloc((n)*(m))
#undef	malloc
#define	malloc	track_malloc
#undef	free
#define	free	track_free
#endif


#undef TRACE

#if USE_DBMALLOC || USE_DMALLOC
#  undef strchr
#  undef strrchr
#  undef memcpy
#  undef memccpy
#  undef malloc
#  undef realloc
#  undef free
#  define strmalloc strdup
#  if USE_DBMALLOC
#    include <dbmalloc.h>		/* renamed from dbmalloc's convention */
#    define show_alloc() malloc_dump(fileno(stderr))
#  endif
#  if USE_DMALLOC
#    include <dmalloc.h>
#    define show_alloc() dmalloc_log_unfreed()
#  endif
#  if USE_MPATROL
#    include <mpatrol.h>
#    define show_alloc() void __mp_summary()
#  endif
#  if CAN_TRACE && OPT_TRACE
#    include "trace.h"
#  endif
#else
#  if CAN_TRACE && (NO_LEAKS || DOALLOC || OPT_TRACE)
#    include "trace.h"
#  elif !defined(show_alloc) && NO_LEAKS
#    include "trace.h"
#  endif
#endif	/* USE_DBMALLOC */

#ifndef init_alloc
#define init_alloc(s,n) /* nothing */
#endif

#if OPT_ELAPSED && OPT_TRACE
extern void show_elapsed(void);
#else
#define show_elapsed() /* nothing */
#endif

/* extra checking if we're tracing */
#if !OPT_TRACE
#undef  NDEBUG
#define NDEBUG			/* turn off assert's */
#define valid_buffer(bp)        ((bp) != NULL)
#define valid_window(wp)        ((wp) != NULL)
#define valid_line_bp(lp,bp)    ((lp) != NULL)
#define valid_line_wp(lp,wp)    ((lp) != NULL)
#endif

/* this must be after NDEBUG is defined */
#include <assert.h>

/* Normally defined in "trace.h" */
#ifndef TRACE
#define TRACE(p) /* nothing */
#define returnCode(c)   return(c)
#define returnPtr(c)    return(c)
#define returnString(c) return(c)
#define returnVoid()    return
#endif

#if OPT_TRACE > 1
#define TRACE2(params)   TRACE(params)
#define return2Code(c)   returnCode(c)
#define return2Ptr(c)    returnPtr(c)
#define return2String(c) returnString(c)
#define return2Void()    returnVoid()
#else
#define TRACE2(params) /*nothing*/
#define return2Code(c)   return(c)
#define return2Ptr(c)    return(c)
#define return2String(c) return(c)
#define return2Void()    return
#endif

#if OPT_EVAL || OPT_DEBUGMACROS
#define TPRINTF(p) { TRACE(p); if (tracemacros) tprintf p; }
#else
#define TPRINTF(p) /* nothing */
#endif

#if DISP_X11 && defined(NEED_X_INCLUDES)
#include	<X11/Intrinsic.h>
#include	<X11/StringDefs.h>
#endif

/*
 * workarounds for defective versions of gcc, e.g., defining a random set of
 * names as "built-in".
 */
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
#if (__GNUC__ == 3 && __GNUC_MINOR__ == 3)
#define exp vl_exp	/* gcc 3.3 */
#endif
#endif /* gcc workarounds */

/*
 * Local prototypes (must follow NO_LEAKS definition)
 */

#if	!CHECK_PROTOTYPES
#include "neproto.h"
#include "proto.h"
#endif

/*
 * the list of generic function key bindings
 */
#if	!CHECK_PROTOTYPES
#include "nefkeys.h"
#endif

/* for debugging VMS pathnames on UNIX... */
#if CAN_VMS_PATH && (SYS_UNIX && OPT_VMS_PATH)
#include "fakevms.h"
#endif

#endif /* _estruct_h */