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
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
|
/* $OpenBSD: edit.c,v 1.4 1996/10/14 03:55:10 downsj Exp $ */
/* vi:set ts=4 sw=4:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
*/
/*
* edit.c: functions for insert mode
*/
#include "vim.h"
#include "globals.h"
#include "proto.h"
#include "option.h"
#include "ops.h" /* for op_type */
#ifdef INSERT_EXPAND
/*
* definitions used for CTRL-X submode
*/
#define CTRL_X_WANT_IDENT 0x100
#define CTRL_X_NOT_DEFINED_YET (1)
#define CTRL_X_SCROLL (2)
#define CTRL_X_WHOLE_LINE (3)
#define CTRL_X_FILES (4)
#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
#define CTRL_X_FINISHED (8)
#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
struct Completion
{
char_u *str;
char_u *fname;
int original;
struct Completion *next;
struct Completion *prev;
};
struct Completion *first_match = NULL;
struct Completion *curr_match = NULL;
static int add_completion __ARGS((char_u *str, int len, char_u *, int dir));
static int make_cyclic __ARGS((void));
static void complete_dictionaries __ARGS((char_u *, int));
static void free_completions __ARGS((void));
static int count_completions __ARGS((void));
#endif /* INSERT_EXPAND */
#define BACKSPACE_CHAR 1
#define BACKSPACE_WORD 2
#define BACKSPACE_WORD_NOT_SPACE 3
#define BACKSPACE_LINE 4
static void change_indent __ARGS((int type, int amount, int round));
static void insert_special __ARGS((int, int));
static void start_arrow __ARGS((FPOS *end_insert_pos));
static void stop_arrow __ARGS((void));
static void stop_insert __ARGS((FPOS *end_insert_pos));
static int echeck_abbr __ARGS((int));
static FPOS Insstart; /* This is where the latest insert/append
* mode started. */
static colnr_t Insstart_textlen; /* length of line when insert started */
static colnr_t Insstart_blank_vcol; /* vcol for first inserted blank */
static char_u *last_insert = NULL;
/* the text of the previous insert */
static int last_insert_skip;
/* number of chars in front of previous insert */
static int new_insert_skip;
/* number of chars in front of the current insert */
#ifdef INSERT_EXPAND
static char_u *original_text = NULL;
/* Original text typed before completion */
#endif
#ifdef CINDENT
static int can_cindent; /* may do cindenting on this line */
#endif
/*
* edit() returns TRUE if it returns because of a CTRL-O command
*/
int
edit(initstr, startln, count)
int initstr;
int startln; /* if set, insert at start of line */
long count;
{
int c;
int cc;
char_u *ptr;
linenr_t lnum;
int temp = 0;
int mode;
int lastc = 0;
colnr_t mincol;
static linenr_t o_lnum = 0;
static int o_eol = FALSE;
int need_redraw = FALSE;
int i;
int did_backspace = TRUE; /* previous char was backspace */
#ifdef RIGHTLEFT
int revins; /* reverse insert mode */
int revinschars = 0; /* how much to skip after edit */
int revinslegal = 0; /* was the last char 'legal'? */
int revinsscol = -1; /* start column of revins session */
#endif
#ifdef INSERT_EXPAND
FPOS first_match_pos;
FPOS last_match_pos;
FPOS *complete_pos;
char_u *complete_pat = NULL;
char_u *tmp_ptr;
char_u *mesg = NULL; /* Message about completion */
int started_completion = FALSE;
colnr_t complete_col = 0; /* init for gcc */
int complete_direction;
int done_dir = 0; /* Found all matches in this
* direction */
int num_matches;
char_u **matches;
regexp *prog;
int save_sm = -1; /* init for gcc */
int save_p_scs;
#endif
#ifdef CINDENT
int line_is_white = FALSE; /* line is empty before insert */
#endif
FPOS tpos;
#ifdef USE_MOUSE
/*
* When doing a paste with the middle mouse button, Insstart is set to
* where the paste started.
*/
if (where_paste_started.lnum != 0)
Insstart = where_paste_started;
else
#endif
{
Insstart = curwin->w_cursor;
if (startln)
Insstart.col = 0;
}
Insstart_textlen = linetabsize(ml_get_curline());
Insstart_blank_vcol = MAXCOL;
if (initstr != NUL && !restart_edit)
{
ResetRedobuff();
AppendNumberToRedobuff(count);
AppendCharToRedobuff(initstr);
if (initstr == 'g') /* "gI" command */
AppendCharToRedobuff('I');
}
if (initstr == 'R')
State = REPLACE;
else
State = INSERT;
#ifdef USE_MOUSE
setmouse();
#endif
clear_showcmd();
#ifdef RIGHTLEFT
revins = (State == INSERT && p_ri); /* there is no reverse replace mode */
if (revins)
undisplay_dollar();
#endif
/*
* When CTRL-O . is used to repeat an insert, we get here with
* restart_edit non-zero, but something in the stuff buffer
*/
if (restart_edit && stuff_empty())
{
#ifdef USE_MOUSE
/*
* After a paste we consider text typed to be part of the insert for
* the pasted text. You can backspace over the paste text too.
*/
if (where_paste_started.lnum)
arrow_used = FALSE;
else
#endif
arrow_used = TRUE;
restart_edit = 0;
/*
* If the cursor was after the end-of-line before the CTRL-O
* and it is now at the end-of-line, put it after the end-of-line
* (this is not correct in very rare cases).
* Also do this if curswant is greater than the current virtual column.
* Eg after "^O$" or "^O80|".
*/
if (((o_eol && curwin->w_cursor.lnum == o_lnum) ||
curwin->w_curswant > curwin->w_virtcol) &&
*(ptr = ml_get_curline() + curwin->w_cursor.col)
!= NUL &&
*(ptr + 1) == NUL)
++curwin->w_cursor.col;
}
else
{
arrow_used = FALSE;
o_eol = FALSE;
}
#ifdef USE_MOUSE
where_paste_started.lnum = 0;
#endif
#ifdef CINDENT
can_cindent = TRUE;
#endif
/*
* If 'showmode' is set, show the current (insert/replace/..) mode.
* A warning message for changing a readonly file is given here, before
* actually changing anything. It's put after the mode, if any.
*/
i = 0;
if (p_smd)
i = showmode();
if (!p_im)
change_warning(i + 1);
#ifdef DIGRAPHS
do_digraph(-1); /* clear digraphs */
#endif
/*
* Get the current length of the redo buffer, those characters have to be
* skipped if we want to get to the inserted characters.
*/
ptr = get_inserted();
new_insert_skip = STRLEN(ptr);
vim_free(ptr);
old_indent = 0;
for (;;)
{
#ifdef RIGHTLEFT
if (!revinslegal)
revinsscol = -1; /* reset on illegal motions */
else
revinslegal = 0;
#endif
if (arrow_used) /* don't repeat insert when arrow key used */
count = 0;
/* set curwin->w_curswant for next K_DOWN or K_UP */
if (!arrow_used)
curwin->w_set_curswant = TRUE;
/* Figure out where the cursor is based on curwin->w_cursor. */
mincol = curwin->w_col;
i = curwin->w_row;
cursupdate();
/*
* When emsg() was called msg_scroll will have been set.
*/
msg_scroll = FALSE;
/*
* If we inserted a character at the last position of the last line in
* the window, scroll the window one line up. This avoids an extra
* redraw.
* This is detected when the cursor column is smaller after inserting
* something.
*/
if (curwin->w_p_wrap && !did_backspace &&
(int)curwin->w_col < (int)mincol - curbuf->b_p_ts &&
i == curwin->w_winpos + curwin->w_height - 1 &&
curwin->w_cursor.lnum != curwin->w_topline)
{
++curwin->w_topline;
updateScreen(VALID_TO_CURSCHAR);
cursupdate();
need_redraw = FALSE;
}
did_backspace = FALSE;
/*
* redraw is postponed until after cursupdate() to make 'dollar'
* option work correctly.
*/
if (need_redraw)
{
updateline();
need_redraw = FALSE;
}
showruler(0);
setcursor();
emsg_on_display = FALSE; /* may remove error message now */
c = vgetc();
/*
* Ignore got_int when CTRL-C was typed here.
* Don't ignore it in :global, we really need to break then.
*/
if (c == Ctrl('C') && !global_busy)
got_int = FALSE;
#ifdef RIGHTLEFT
if (p_hkmap && KeyTyped)
c = hkmap(c); /* Hebrew mode mapping */
#endif
#ifdef INSERT_EXPAND
if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
{
/* We have just entered ctrl-x mode and aren't quite sure which
* ctrl-x mode it will be yet. Now we decide -- webb
*/
switch (c)
{
case Ctrl('E'):
case Ctrl('Y'):
ctrl_x_mode = CTRL_X_SCROLL;
if (State == INSERT)
edit_submode = (char_u *)" (insert) Scroll (^E/^Y)";
else
edit_submode = (char_u *)" (replace) Scroll (^E/^Y)";
break;
case Ctrl('L'):
ctrl_x_mode = CTRL_X_WHOLE_LINE;
edit_submode = (char_u *)" Whole line completion (^L/^N/^P)";
break;
case Ctrl('F'):
ctrl_x_mode = CTRL_X_FILES;
edit_submode = (char_u *)" File name completion (^F/^N/^P)";
break;
case Ctrl('K'):
ctrl_x_mode = CTRL_X_DICTIONARY;
edit_submode = (char_u *)" Dictionary completion (^K/^N/^P)";
break;
case Ctrl(']'):
ctrl_x_mode = CTRL_X_TAGS;
edit_submode = (char_u *)" Tag completion (^]/^N/^P)";
break;
case Ctrl('I'):
ctrl_x_mode = CTRL_X_PATH_PATTERNS;
edit_submode = (char_u *)" Path pattern completion (^N/^P)";
break;
case Ctrl('D'):
ctrl_x_mode = CTRL_X_PATH_DEFINES;
edit_submode = (char_u *)" Definition completion (^D/^N/^P)";
break;
default:
ctrl_x_mode = 0;
break;
}
showmode();
}
else if (ctrl_x_mode)
{
/* We we're already in ctrl-x mode, do we stay in it? */
if (!is_ctrl_x_key(c))
{
if (ctrl_x_mode == CTRL_X_SCROLL)
ctrl_x_mode = 0;
else
ctrl_x_mode = CTRL_X_FINISHED;
edit_submode = NULL;
}
showmode();
}
if (started_completion || ctrl_x_mode == CTRL_X_FINISHED)
{
/* Show error message from attempted keyword completion (probably
* 'Pattern not found') until another key is hit, then go back to
* showing what mode we are in.
*/
showmode();
if ((ctrl_x_mode == 0 && c != Ctrl('N') && c != Ctrl('P')) ||
ctrl_x_mode == CTRL_X_FINISHED)
{
/* Get here when we have finished typing a sequence of ^N and
* ^P or other completion characters in CTRL-X mode. Free up
* memory that was used, and make sure we can redo the insert
* -- webb.
*/
if (curr_match != NULL)
{
/*
* If any of the original typed text has been changed,
* eg when ignorecase is set, we must add back-spaces to
* the redo buffer. We add as few as necessary to delete
* just the part of the original text that has changed
* -- webb
*/
ptr = curr_match->str;
tmp_ptr = original_text;
while (*tmp_ptr && *tmp_ptr == *ptr)
{
++tmp_ptr;
++ptr;
}
for (temp = 0; tmp_ptr[temp]; ++temp)
AppendCharToRedobuff(K_BS);
if (*ptr)
AppendToRedobuff(ptr);
}
/* Break line if it's too long */
lnum = curwin->w_cursor.lnum;
insertchar(NUL, FALSE, -1);
if (lnum != curwin->w_cursor.lnum)
updateScreen(CURSUPD);
else
need_redraw = TRUE;
vim_free(complete_pat);
complete_pat = NULL;
vim_free(original_text);
original_text = NULL;
free_completions();
started_completion = FALSE;
ctrl_x_mode = 0;
p_sm = save_sm;
if (edit_submode != NULL)
{
edit_submode = NULL;
showmode();
}
}
}
#endif /* INSERT_EXPAND */
if (c != Ctrl('D')) /* remember to detect ^^D and 0^D */
lastc = c;
#ifdef DIGRAPHS
c = do_digraph(c);
#endif /* DIGRAPHS */
if (c == Ctrl('V') || c == Ctrl('Q'))
{
if (NextScreen != NULL)
screen_outchar('^', curwin->w_winpos + curwin->w_row,
#ifdef RIGHTLEFT
curwin->w_p_rl ? (int)Columns - 1 - curwin->w_col :
#endif
curwin->w_col);
AppendToRedobuff((char_u *)"\026"); /* CTRL-V */
cursupdate();
if (!add_to_showcmd(c, FALSE))
setcursor();
c = get_literal();
clear_showcmd();
insert_special(c, TRUE);
need_redraw = TRUE;
#ifdef RIGHTLEFT
revinschars++;
revinslegal++;
#endif
continue;
}
#ifdef CINDENT
if (curbuf->b_p_cin
# ifdef INSERT_EXPAND
&& !ctrl_x_mode
# endif
)
{
line_is_white = inindent(0);
/*
* A key name preceded by a bang means that this
* key wasn't destined to be inserted. Skip ahead
* to the re-indenting if we find one.
*/
if (in_cinkeys(c, '!', line_is_white))
goto force_cindent;
/*
* A key name preceded by a star means that indenting
* has to be done before inserting the key.
*/
if (can_cindent && in_cinkeys(c, '*', line_is_white))
{
stop_arrow();
/* re-indent the current line */
fixthisline(get_c_indent);
/* draw the changes on the screen later */
need_redraw = TRUE;
}
}
#endif /* CINDENT */
#ifdef RIGHTLEFT
if (curwin->w_p_rl)
switch (c)
{
case K_LEFT: c = K_RIGHT; break;
case K_S_LEFT: c = K_S_RIGHT; break;
case K_RIGHT: c = K_LEFT; break;
case K_S_RIGHT: c = K_S_LEFT; break;
}
#endif
switch (c) /* handle character in insert mode */
{
case K_INS: /* toggle insert/replace mode */
if (State == REPLACE)
State = INSERT;
else
State = REPLACE;
AppendCharToRedobuff(K_INS);
showmode();
break;
#ifdef INSERT_EXPAND
case Ctrl('X'): /* Enter ctrl-x mode */
/* We're not sure which ctrl-x mode it will be yet */
ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
edit_submode = (char_u *)"^X mode (^E/^Y/^L/^]/^F/^I/^K/^D)";
showmode();
break;
#endif /* INSERT_EXPAND */
case Ctrl('O'): /* execute one command */
if (echeck_abbr(Ctrl('O') + ABBR_OFF))
break;
count = 0;
if (State == INSERT)
restart_edit = 'I';
else
restart_edit = 'R';
o_lnum = curwin->w_cursor.lnum;
o_eol = (gchar_cursor() == NUL);
goto doESCkey;
/* Hitting the help key in insert mode is like <ESC> <Help> */
case K_HELP:
case K_F1:
stuffcharReadbuff(K_HELP);
/*FALLTHROUGH*/
case ESC: /* an escape ends input mode */
if (echeck_abbr(ESC + ABBR_OFF))
break;
/*FALLTHROUGH*/
case Ctrl('C'):
doESCkey:
temp = curwin->w_cursor.col;
if (!arrow_used)
{
AppendToRedobuff(ESC_STR);
if (--count > 0) /* repeat what was typed */
{
(void)start_redo_ins();
continue;
}
stop_insert(&curwin->w_cursor);
if (dollar_vcol)
{
dollar_vcol = 0;
/* may have to redraw status line if this was the
* first change, show "[+]" */
if (curwin->w_redr_status == TRUE)
must_redraw = NOT_VALID;
else
need_redraw = TRUE;
}
}
if (need_redraw)
updateline();
/* When an autoindent was removed, curswant stays after the
* indent */
if (!restart_edit && (colnr_t)temp == curwin->w_cursor.col)
curwin->w_set_curswant = TRUE;
/*
* The cursor should end up on the last inserted character.
*/
if (curwin->w_cursor.col != 0 &&
(!restart_edit || gchar_cursor() == NUL)
#ifdef RIGHTLEFT
&& !revins
#endif
)
--curwin->w_cursor.col;
if (State == REPLACE)
replace_flush(); /* free replace stack */
State = NORMAL;
#ifdef USE_MOUSE
setmouse();
#endif
/* inchar() may have deleted the "INSERT" message */
/* for CTRL-O we display -- INSERT COMMAND -- */
if (Recording || restart_edit)
showmode();
else if (p_smd)
MSG("");
old_indent = 0;
/*
* This is the ONLY return from edit().
*/
return (c == Ctrl('O'));
/*
* Insert the previously inserted text.
* For ^@ the trailing ESC will end the insert, unless there
* is an error.
*/
case K_ZERO:
case NUL:
case Ctrl('A'):
if (stuff_inserted(NUL, 1L, (c == Ctrl('A'))) == FAIL &&
c != Ctrl('A'))
goto doESCkey; /* quit insert mode */
break;
/*
* insert the contents of a register
*/
case Ctrl('R'):
if (NextScreen != NULL)
screen_outchar('"', curwin->w_winpos + curwin->w_row,
#ifdef RIGHTLEFT
curwin->w_p_rl ? (int)Columns - 1 - curwin->w_col :
#endif
curwin->w_col);
if (!add_to_showcmd(c, FALSE))
setcursor();
/* don't map the register name. This also prevents the
* mode message to be deleted when ESC is hit */
++no_mapping;
#ifdef HAVE_LANGMAP
cc = vgetc();
LANGMAP_ADJUST(cc, TRUE);
if (insertbuf(cc) == FAIL)
#else
if (insertbuf(vgetc()) == FAIL)
#endif
{
beep_flush();
need_redraw = TRUE; /* remove the '"' */
}
--no_mapping;
clear_showcmd();
break;
#ifdef RIGHTLEFT
case Ctrl('B'): /* toggle reverse insert mode */
p_ri = !p_ri;
revins = (State == INSERT && p_ri);
if (revins)
undisplay_dollar();
showmode();
break;
case Ctrl('_'): /* toggle language: khmap and revins */
/* Move to end of reverse inserted text */
if (revins && revinschars && revinsscol >= 0)
while (gchar_cursor() != NUL && revinschars--)
++curwin->w_cursor.col;
p_ri = !p_ri;
revins = (State == INSERT && p_ri);
if (revins)
{
revinsscol = curwin->w_cursor.col;
revinslegal++;
revinschars = 0;
undisplay_dollar();
}
else
revinsscol = -1;
p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
showmode();
break;
#endif
/*
* If the cursor is on an indent, ^T/^D insert/delete one
* shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
* Always round the indent to 'shiftwith', this is compatible
* with vi. But vi only supports ^T and ^D after an
* autoindent, we support it everywhere.
*/
case Ctrl('D'): /* make indent one shiftwidth smaller */
#ifdef INSERT_EXPAND
if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
goto docomplete;
#endif /* INSERT_EXPAND */
/* FALLTHROUGH */
case Ctrl('T'): /* make indent one shiftwidth greater */
stop_arrow();
AppendCharToRedobuff(c);
/*
* 0^D and ^^D: remove all indent.
*/
if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
{
--curwin->w_cursor.col;
(void)delchar(FALSE); /* delete the '^' or '0' */
if (lastc == '^')
old_indent = get_indent(); /* remember curr. indent */
change_indent(INDENT_SET, 0, TRUE);
}
else
change_indent(c == Ctrl('D') ? INDENT_DEC : INDENT_INC,
0, TRUE);
did_ai = FALSE;
did_si = FALSE;
can_si = FALSE;
can_si_back = FALSE;
#ifdef CINDENT
can_cindent = FALSE; /* no cindenting after ^D or ^T */
#endif
goto redraw;
case K_DEL:
stop_arrow();
if (gchar_cursor() == NUL) /* delete newline */
{
temp = curwin->w_cursor.col;
if (!p_bs || /* only if 'bs' set */
u_save((linenr_t)(curwin->w_cursor.lnum - 1),
(linenr_t)(curwin->w_cursor.lnum + 2)) == FAIL ||
do_join(FALSE, TRUE) == FAIL)
beep_flush();
else
curwin->w_cursor.col = temp;
}
else if (delchar(FALSE) == FAIL)/* delete char under cursor */
beep_flush();
did_ai = FALSE;
did_si = FALSE;
can_si = FALSE;
can_si_back = FALSE;
AppendCharToRedobuff(c);
goto redraw;
case K_BS:
case Ctrl('H'):
mode = BACKSPACE_CHAR;
dodel:
/* can't delete anything in an empty file */
/* can't backup past first character in buffer */
/* can't backup past starting point unless 'backspace' > 1 */
/* can backup to a previous line if 'backspace' == 0 */
if (bufempty() || (
#ifdef RIGHTLEFT
!revins &&
#endif
((curwin->w_cursor.lnum == 1 &&
curwin->w_cursor.col <= 0) ||
(p_bs < 2 && (arrow_used ||
(curwin->w_cursor.lnum == Insstart.lnum &&
curwin->w_cursor.col <= Insstart.col) ||
(curwin->w_cursor.col <= 0 && p_bs == 0))))))
{
beep_flush();
goto redraw;
}
stop_arrow();
#ifdef CINDENT
if (inindent(0))
can_cindent = FALSE;
#endif
#ifdef RIGHTLEFT
if (revins) /* put cursor after last inserted char */
inc_cursor();
#endif
if (curwin->w_cursor.col <= 0) /* delete newline! */
{
lnum = Insstart.lnum;
if (curwin->w_cursor.lnum == Insstart.lnum
#ifdef RIGHTLEFT
|| revins
#endif
)
{
if (u_save((linenr_t)(curwin->w_cursor.lnum - 2),
(linenr_t)(curwin->w_cursor.lnum + 1)) == FAIL)
goto redraw;
--Insstart.lnum;
Insstart.col = 0;
}
/*
* In replace mode:
* cc < 0: NL was inserted, delete it
* cc >= 0: NL was replaced, put original characters back
*/
cc = -1;
if (State == REPLACE)
cc = replace_pop();
/* in replace mode, in the line we started replacing, we
only move the cursor */
if (State != REPLACE || curwin->w_cursor.lnum > lnum)
{
temp = gchar_cursor(); /* remember current char */
--curwin->w_cursor.lnum;
(void)do_join(FALSE, curs_rows() == OK);
if (temp == NUL && gchar_cursor() != NUL)
++curwin->w_cursor.col;
/*
* in REPLACE mode we have to put back the text that
* was replace by the NL. On the replace stack is
* first a NUL-terminated sequence of characters that
* were deleted and then the character that NL
* replaced.
*/
if (State == REPLACE)
{
/*
* Do the next ins_char() in NORMAL state, to
* prevent ins_char() from replacing characters and
* avoiding showmatch().
*/
State = NORMAL;
/*
* restore blanks deleted after cursor
*/
while (cc > 0)
{
temp = curwin->w_cursor.col;
ins_char(cc);
curwin->w_cursor.col = temp;
cc = replace_pop();
}
cc = replace_pop();
if (cc > 0)
{
ins_char(cc);
dec_cursor();
}
State = REPLACE;
}
}
else
dec_cursor();
did_ai = FALSE;
}
else
{
#ifdef RIGHTLEFT
if (revins) /* put cursor on last inserted char */
dec_cursor();
#endif
mincol = 0;
/* keep indent */
if (mode == BACKSPACE_LINE && curbuf->b_p_ai
#ifdef RIGHTLEFT
&& !revins
#endif
)
{
temp = curwin->w_cursor.col;
beginline(TRUE);
if (curwin->w_cursor.col < (colnr_t)temp)
mincol = curwin->w_cursor.col;
curwin->w_cursor.col = temp;
}
/* delete upto starting point, start of line or previous
* word */
do
{
#ifdef RIGHTLEFT
if (!revins) /* put cursor on char to be deleted */
#endif
dec_cursor();
/* start of word? */
if (mode == BACKSPACE_WORD &&
!vim_isspace(gchar_cursor()))
{
mode = BACKSPACE_WORD_NOT_SPACE;
temp = iswordchar(gchar_cursor());
}
/* end of word? */
else if (mode == BACKSPACE_WORD_NOT_SPACE &&
(vim_isspace(cc = gchar_cursor()) ||
iswordchar(cc) != temp))
{
#ifdef RIGHTLEFT
if (!revins)
#endif
inc_cursor();
#ifdef RIGHTLEFT
else if (State == REPLACE)
dec_cursor();
#endif
break;
}
if (State == REPLACE)
{
/*
* cc < 0: replace stack empty, just move cursor
* cc == 0: character was inserted, delete it
* cc > 0: character was replace, put original back
*/
cc = replace_pop();
if (cc > 0)
pchar_cursor(cc);
else if (cc == 0)
(void)delchar(FALSE);
}
else /* State != REPLACE */
{
(void)delchar(FALSE);
#ifdef RIGHTLEFT
if (revinschars)
{
revinschars--;
revinslegal++;
}
if (revins && gchar_cursor() == NUL)
break;
#endif
}
/* Just a single backspace?: */
if (mode == BACKSPACE_CHAR)
break;
} while (
#ifdef RIGHTLEFT
revins ||
#endif
(curwin->w_cursor.col > mincol &&
(curwin->w_cursor.lnum != Insstart.lnum ||
curwin->w_cursor.col != Insstart.col)));
did_backspace = TRUE;
}
did_si = FALSE;
can_si = FALSE;
can_si_back = FALSE;
if (curwin->w_cursor.col <= 1)
did_ai = FALSE;
/*
* It's a little strange to put backspaces into the redo
* buffer, but it makes auto-indent a lot easier to deal
* with.
*/
AppendCharToRedobuff(c);
redraw:
need_redraw = TRUE;
break;
case Ctrl('W'): /* delete word before cursor */
mode = BACKSPACE_WORD;
goto dodel;
case Ctrl('U'): /* delete inserted text in current line */
mode = BACKSPACE_LINE;
goto dodel;
#ifdef USE_MOUSE
case K_LEFTMOUSE:
case K_LEFTDRAG:
case K_LEFTRELEASE:
case K_MIDDLEMOUSE:
case K_MIDDLEDRAG:
case K_MIDDLERELEASE:
case K_RIGHTMOUSE:
case K_RIGHTDRAG:
case K_RIGHTRELEASE:
#ifdef USE_GUI
/* When GUI is active, also move/paste when 'mouse' is empty */
if (!gui.in_use)
#endif
if (!mouse_has(MOUSE_INSERT))
break;
undisplay_dollar();
tpos = curwin->w_cursor;
if (do_mouse(c, BACKWARD, 1L, FALSE))
{
start_arrow(&tpos);
# ifdef CINDENT
can_cindent = TRUE;
# endif
}
break;
case K_IGNORE:
break;
#endif
#ifdef USE_GUI
case K_SCROLLBAR:
undisplay_dollar();
tpos = curwin->w_cursor;
if (gui_do_scroll())
{
start_arrow(&tpos);
# ifdef CINDENT
can_cindent = TRUE;
# endif
}
break;
case K_HORIZ_SCROLLBAR:
undisplay_dollar();
tpos = curwin->w_cursor;
if (gui_do_horiz_scroll())
{
start_arrow(&tpos);
#ifdef CINDENT
can_cindent = TRUE;
#endif
}
break;
#endif
case K_LEFT:
undisplay_dollar();
tpos = curwin->w_cursor;
if (oneleft() == OK)
{
start_arrow(&tpos);
#ifdef RIGHTLEFT
/* If exit reversed string, position is fixed */
if (revinsscol != -1 &&
(int)curwin->w_cursor.col >= revinsscol)
revinslegal++;
revinschars++;
#endif
}
/*
* if 'whichwrap' set for cursor in insert mode may go to
* previous line
*/
else if (vim_strchr(p_ww, '[') != NULL &&
curwin->w_cursor.lnum > 1)
{
start_arrow(&tpos);
--(curwin->w_cursor.lnum);
coladvance(MAXCOL);
curwin->w_set_curswant = TRUE; /* so we stay at the end */
}
else
beep_flush();
break;
case K_HOME:
case K_KHOME:
undisplay_dollar();
tpos = curwin->w_cursor;
if ((mod_mask & MOD_MASK_CTRL))
curwin->w_cursor.lnum = 1;
curwin->w_cursor.col = 0;
curwin->w_curswant = 0;
start_arrow(&tpos);
break;
case K_END:
case K_KEND:
undisplay_dollar();
tpos = curwin->w_cursor;
if ((mod_mask & MOD_MASK_CTRL))
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
coladvance(MAXCOL);
curwin->w_curswant = MAXCOL;
start_arrow(&tpos);
break;
case K_S_LEFT:
undisplay_dollar();
if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
{
start_arrow(&curwin->w_cursor);
(void)bck_word(1L, 0, FALSE);
}
else
beep_flush();
break;
case K_RIGHT:
undisplay_dollar();
if (gchar_cursor() != NUL)
{
start_arrow(&curwin->w_cursor);
curwin->w_set_curswant = TRUE;
++curwin->w_cursor.col;
#ifdef RIGHTLEFT
revinslegal++;
if (revinschars)
revinschars--;
#endif
}
/* if 'whichwrap' set for cursor in insert mode may go
* to next line */
else if (vim_strchr(p_ww, ']') != NULL &&
curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
{
start_arrow(&curwin->w_cursor);
curwin->w_set_curswant = TRUE;
++curwin->w_cursor.lnum;
curwin->w_cursor.col = 0;
}
else
beep_flush();
break;
case K_S_RIGHT:
undisplay_dollar();
if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count ||
gchar_cursor() != NUL)
{
start_arrow(&curwin->w_cursor);
(void)fwd_word(1L, 0, 0);
}
else
beep_flush();
break;
case K_UP:
undisplay_dollar();
tpos = curwin->w_cursor;
if (cursor_up(1L) == OK)
{
start_arrow(&tpos);
#ifdef CINDENT
can_cindent = TRUE;
#endif
}
else
beep_flush();
break;
case K_S_UP:
case K_PAGEUP:
case K_KPAGEUP:
undisplay_dollar();
tpos = curwin->w_cursor;
if (onepage(BACKWARD, 1L) == OK)
{
start_arrow(&tpos);
#ifdef CINDENT
can_cindent = TRUE;
#endif
}
else
beep_flush();
break;
case K_DOWN:
undisplay_dollar();
tpos = curwin->w_cursor;
if (cursor_down(1L) == OK)
{
start_arrow(&tpos);
#ifdef CINDENT
can_cindent = TRUE;
#endif
}
else
beep_flush();
break;
case K_S_DOWN:
case K_PAGEDOWN:
case K_KPAGEDOWN:
undisplay_dollar();
tpos = curwin->w_cursor;
if (onepage(FORWARD, 1L) == OK)
{
start_arrow(&tpos);
#ifdef CINDENT
can_cindent = TRUE;
#endif
}
else
beep_flush();
break;
case TAB: /* TAB or Complete patterns along path */
#ifdef INSERT_EXPAND
if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
goto docomplete;
#endif /* INSERT_EXPAND */
if (Insstart_blank_vcol == MAXCOL &&
curwin->w_cursor.lnum == Insstart.lnum)
Insstart_blank_vcol = curwin->w_virtcol;
if (echeck_abbr(TAB + ABBR_OFF))
break;
i = inindent(0);
#ifdef CINDENT
if (i)
can_cindent = FALSE;
#endif
if (!curbuf->b_p_et && !(p_sta && i))
goto normalchar;
stop_arrow();
did_ai = FALSE;
did_si = FALSE;
can_si = FALSE;
can_si_back = FALSE;
AppendToRedobuff((char_u *)"\t");
if (p_sta && i) /* insert tab in indent */
{
change_indent(INDENT_INC, 0, p_sr);
goto redraw;
}
/*
* p_et is set: expand a tab into spaces
*/
temp = (int)curbuf->b_p_ts;
temp -= curwin->w_virtcol % temp;
/*
* insert the first space with ins_char(); it will delete one
* char in replace mode. Insert the rest with ins_str(); it
* will not delete any chars
*/
ins_char(' ');
while (--temp)
{
ins_str((char_u *)" ");
if (State == REPLACE) /* no char replaced */
replace_push(NUL);
}
goto redraw;
case CR:
case NL:
if (echeck_abbr(c + ABBR_OFF))
break;
stop_arrow();
if (State == REPLACE)
replace_push(NUL);
#ifdef RIGHTLEFT
/* NL in reverse insert will allways start in the end of
* current line. */
if (revins)
while (gchar_cursor() != NUL)
++curwin->w_cursor.col;
#endif
AppendToRedobuff(NL_STR);
if (has_format_option(FO_RET_COMS))
fo_do_comments = TRUE;
i = Opencmd(FORWARD, TRUE, FALSE);
fo_do_comments = FALSE;
#ifdef CINDENT
can_cindent = TRUE;
#endif
if (!i)
goto doESCkey; /* out of memory */
break;
#ifdef DIGRAPHS
case Ctrl('K'):
#ifdef INSERT_EXPAND
if (ctrl_x_mode == CTRL_X_DICTIONARY)
goto docomplete;
#endif
if (NextScreen != NULL)
screen_outchar('?', curwin->w_winpos + curwin->w_row,
#ifdef RIGHTLEFT
curwin->w_p_rl ? (int)Columns - 1 - curwin->w_col :
#endif
curwin->w_col);
if (!add_to_showcmd(c, FALSE))
setcursor();
/* don't map the digraph chars. This also prevents the
* mode message to be deleted when ESC is hit */
++no_mapping;
++allow_keys;
c = vgetc();
--no_mapping;
--allow_keys;
if (IS_SPECIAL(c)) /* special key */
{
clear_showcmd();
insert_special(c, TRUE);
need_redraw = TRUE;
break;
}
if (c != ESC)
{
if (charsize(c) == 1 && NextScreen != NULL)
screen_outchar(c, curwin->w_winpos + curwin->w_row,
#ifdef RIGHTLEFT
curwin->w_p_rl ? (int)Columns - 1 - curwin->w_col :
#endif
curwin->w_col);
if (!add_to_showcmd(c, FALSE))
setcursor();
++no_mapping;
++allow_keys;
cc = vgetc();
--no_mapping;
--allow_keys;
if (cc != ESC)
{
AppendToRedobuff((char_u *)"\026"); /* CTRL-V */
c = getdigraph(c, cc, TRUE);
clear_showcmd();
goto normalchar;
}
}
clear_showcmd();
need_redraw = TRUE;
break;
#else /* DIGRAPHS */
# ifdef INSERT_EXPAND
case Ctrl('K'):
if (ctrl_x_mode != CTRL_X_DICTIONARY)
goto normalchar;
goto docomplete;
# endif /* INSERT_EXPAND */
#endif /* DIGRAPHS */
#ifdef INSERT_EXPAND
case Ctrl(']'): /* Tag name completion after ^X */
if (ctrl_x_mode != CTRL_X_TAGS)
goto normalchar;
goto docomplete;
case Ctrl('F'): /* File name completion after ^X */
if (ctrl_x_mode != CTRL_X_FILES)
goto normalchar;
goto docomplete;
case Ctrl('L'): /* Whole line completion after ^X */
if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
goto normalchar;
/* FALLTHROUGH */
case Ctrl('P'): /* Do previous pattern completion */
case Ctrl('N'): /* Do next pattern completion */
docomplete:
if (c == Ctrl('P') || c == Ctrl('L'))
complete_direction = BACKWARD;
else
complete_direction = FORWARD;
mesg = NULL; /* No message by default */
if (!started_completion)
{
/* First time we hit ^N or ^P (in a row, I mean) */
/* Turn off 'sm' so we don't show matches with ^X^L */
save_sm = p_sm;
p_sm = FALSE;
if (ctrl_x_mode == 0)
{
edit_submode = (char_u *)" Keyword completion (^P/^N)";
showmode();
}
did_ai = FALSE;
did_si = FALSE;
can_si = FALSE;
can_si_back = FALSE;
stop_arrow();
done_dir = 0;
first_match_pos = curwin->w_cursor;
ptr = tmp_ptr = ml_get(first_match_pos.lnum);
complete_col = first_match_pos.col;
temp = (int)complete_col - 1;
/* Work out completion pattern and original text -- webb */
if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
{
if (temp < 0 || !iswordchar(ptr[temp]))
{
/* Match any word of at least two chars */
complete_pat = strsave((char_u *)"\\<\\k\\k");
if (complete_pat == NULL)
break;
tmp_ptr += complete_col;
temp = 0;
}
else
{
while (temp >= 0 && iswordchar(ptr[temp]))
temp--;
tmp_ptr += ++temp;
if ((temp = (int)complete_col - temp) == 1)
{
/* Only match word with at least two
* chars -- webb
*/
sprintf((char *)IObuff, "\\<%c\\k", *tmp_ptr);
complete_pat = strsave(IObuff);
if (complete_pat == NULL)
break;
}
else
{
complete_pat = alloc(temp + 3);
if (complete_pat == NULL)
break;
sprintf((char *)complete_pat, "\\<%.*s", temp,
tmp_ptr);
}
}
}
else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
{
tmp_ptr = skipwhite(ptr);
temp = (int)complete_col - (tmp_ptr - ptr);
complete_pat = strnsave(tmp_ptr, temp);
if (complete_pat == NULL)
break;
}
else if (ctrl_x_mode == CTRL_X_FILES)
{
while (temp >= 0 && isfilechar(ptr[temp]))
temp--;
tmp_ptr += ++temp;
temp = (int)complete_col - temp;
complete_pat = addstar(tmp_ptr, temp);
if (complete_pat == NULL)
break;
}
original_text = strnsave(tmp_ptr, temp);
if (original_text == NULL)
{
vim_free(complete_pat);
complete_pat = NULL;
break;
}
complete_col = tmp_ptr - ptr;
first_match_pos.col -= temp;
/* So that ^N can match word immediately after cursor */
if (ctrl_x_mode == 0)
dec(&first_match_pos);
last_match_pos = first_match_pos;
/* Get list of all completions now, if appropriate */
if (ctrl_x_mode == CTRL_X_PATH_PATTERNS ||
ctrl_x_mode == CTRL_X_PATH_DEFINES)
{
started_completion = TRUE;
find_pattern_in_path(complete_pat,
(int)STRLEN(complete_pat), FALSE, FALSE,
(ctrl_x_mode == CTRL_X_PATH_DEFINES) ? FIND_DEFINE
: FIND_ANY, 1L, ACTION_EXPAND,
(linenr_t)1, (linenr_t)MAXLNUM);
if (make_cyclic() > 1)
{
sprintf((char *)IObuff, "There are %d matches",
count_completions());
mesg = IObuff;
}
}
else if (ctrl_x_mode == CTRL_X_DICTIONARY)
{
started_completion = TRUE;
if (*p_dict == NUL)
mesg = (char_u *)"'dictionary' option is empty";
else
{
complete_dictionaries(complete_pat,
complete_direction);
if (make_cyclic() > 1)
{
sprintf((char *)IObuff,
"There are %d matching words",
count_completions());
mesg = IObuff;
}
}
}
else if (ctrl_x_mode == CTRL_X_TAGS)
{
started_completion = TRUE;
/* set reg_ic according to p_ic, p_scs and pat */
set_reg_ic(complete_pat);
prog = vim_regcomp(complete_pat);
if (prog != NULL &&
find_tags(NULL, prog, &num_matches, &matches,
FALSE, FALSE) == OK && num_matches > 0)
{
for (i = 0; i < num_matches; i++)
if (add_completion(matches[i], -1, NULL,
FORWARD) == RET_ERROR)
break;
FreeWild(num_matches, matches);
vim_free(prog);
if (make_cyclic() > 1)
{
sprintf((char *)IObuff,
"There are %d matching tags",
count_completions());
mesg = IObuff;
}
}
else
{
vim_free(prog);
vim_free(complete_pat);
complete_pat = NULL;
}
}
else if (ctrl_x_mode == CTRL_X_FILES)
{
started_completion = TRUE;
expand_interactively = TRUE;
if (ExpandWildCards(1, &complete_pat, &num_matches,
&matches, FALSE, FALSE) == OK)
{
/*
* May change home directory back to "~".
*/
tilde_replace(complete_pat, num_matches, matches);
for (i = 0; i < num_matches; i++)
if (add_completion(matches[i], -1, NULL,
FORWARD) == RET_ERROR)
break;
FreeWild(num_matches, matches);
if (make_cyclic() > 1)
{
sprintf((char *)IObuff,
"There are %d matching file names",
count_completions());
mesg = IObuff;
}
}
else
{
vim_free(complete_pat);
complete_pat = NULL;
}
expand_interactively = FALSE;
}
}
/*
* In insert mode: Delete the typed part.
* In replace mode: Put the old characters back, if any.
*/
while (curwin->w_cursor.col > complete_col)
{
curwin->w_cursor.col--;
if (State == REPLACE)
{
if ((cc = replace_pop()) > 0)
pchar(curwin->w_cursor, cc);
}
else
delchar(FALSE);
}
complete_pos = NULL;
if (started_completion && curr_match == NULL &&
(p_ws || done_dir == BOTH_DIRECTIONS))
{
edit_submode_extra = e_patnotf;
edit_submode_highl = 'e';
}
else if (curr_match != NULL && complete_direction == FORWARD &&
curr_match->next != NULL)
curr_match = curr_match->next;
else if (curr_match != NULL && complete_direction == BACKWARD &&
curr_match->prev != NULL)
curr_match = curr_match->prev;
else
{
complete_pos = (complete_direction == FORWARD) ?
&last_match_pos : &first_match_pos;
/*
* If 'infercase' is set, don't use 'smartcase' here
*/
save_p_scs = p_scs;
if (curbuf->b_p_inf)
p_scs = FALSE;
for (;;)
{
if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
temp = search_for_exact_line(complete_pos,
complete_direction, complete_pat);
else
temp = searchit(complete_pos, complete_direction,
complete_pat, 1L,
SEARCH_KEEP + SEARCH_NFMSG, RE_LAST);
if (temp == FAIL)
{
if (!p_ws && done_dir != -complete_direction)
{
/*
* With nowrapscan, we haven't finished
* looking in the other direction yet -- webb
*/
temp = OK;
done_dir = complete_direction;
}
else if (!p_ws)
done_dir = BOTH_DIRECTIONS;
break;
}
if (!started_completion)
{
started_completion = TRUE;
first_match_pos = *complete_pos;
last_match_pos = *complete_pos;
}
else if (first_match_pos.lnum == last_match_pos.lnum &&
first_match_pos.col == last_match_pos.col)
{
/* We have found all the matches in this file */
temp = FAIL;
break;
}
ptr = ml_get_pos(complete_pos);
if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
temp = STRLEN(ptr);
else
{
tmp_ptr = ptr;
temp = 0;
while (*tmp_ptr != NUL && iswordchar(*tmp_ptr++))
temp++;
}
if (add_completion_and_infercase(ptr, temp, NULL,
complete_direction) != FAIL)
{
temp = OK;
break;
}
}
p_scs = save_p_scs;
}
if (complete_pos != NULL && temp == FAIL)
{
int tot;
tot = count_completions(); /* Total num matches */
if (curr_match != NULL)
(void)make_cyclic();
if (tot > 1)
{
sprintf((char *)IObuff,
"All %d matches have now been found", tot);
mesg = IObuff;
}
else if (tot == 0)
{
edit_submode_extra = e_patnotf;
edit_submode_highl = 'e';
}
}
/* eat the ESC to avoid leaving insert mode */
if (got_int && !global_busy)
{
(void)vgetc();
got_int = FALSE;
}
/*
* When using match from another file, show the file name.
*/
if (curr_match != NULL)
ptr = curr_match->str;
else /* back to what has been typed */
ptr = original_text;
if (edit_submode_extra == NULL)
{
if (curr_match == NULL || curr_match->original)
{
edit_submode_extra = (char_u *)"Back at original";
edit_submode_highl = 'w';
}
else if (first_match != NULL &&
first_match->next != NULL &&
(first_match->next == first_match ||
first_match->next->original))
{
edit_submode_extra = (char_u *)"(the only match)";
edit_submode_highl = NUL;
}
}
/*
* Use ins_char() to insert the text, it is a bit slower than
* ins_str(), but it takes care of replace mode.
*/
if (ptr != NULL)
while (*ptr)
ins_char(*ptr++);
started_completion = TRUE;
need_redraw = TRUE;
if (mesg != NULL)
{
(void)set_highlight('r');
msg_highlight = TRUE;
msg(mesg);
mch_delay(2000L, FALSE);
}
if (edit_submode_extra != NULL)
{
if (!p_smd)
{
if (edit_submode_highl)
{
set_highlight('r'); /* Highlight mode */
msg_highlight = TRUE;
}
msg(edit_submode_extra);
}
else
showmode();
edit_submode_extra = NULL;
}
/*
* If there is a file name for the match, overwrite any
* previous message, it's more interesting to know where the
* match comes from, except when using the dictionary.
* Truncate the file name to avoid a wait for return.
*/
if (curr_match != NULL && curr_match->fname != NULL &&
(ctrl_x_mode != CTRL_X_DICTIONARY || mesg == NULL))
{
STRCPY(IObuff, "match in file ");
i = (strsize(curr_match->fname) + 16) - sc_col;
if (i <= 0)
i = 0;
else
STRCAT(IObuff, "<");
STRCAT(IObuff, curr_match->fname + i);
msg(IObuff);
redraw_cmdline = FALSE; /* don't overwrite! */
}
break;
#endif /* INSERT_EXPAND */
case Ctrl('Y'): /* copy from previous line */
#ifdef INSERT_EXPAND
if (ctrl_x_mode == CTRL_X_SCROLL)
{
scrolldown_clamp();
updateScreen(VALID);
break;
}
#endif /* INSERT_EXPAND */
lnum = curwin->w_cursor.lnum - 1;
goto copychar;
case Ctrl('E'): /* copy from next line */
#ifdef INSERT_EXPAND
if (ctrl_x_mode == CTRL_X_SCROLL)
{
scrollup_clamp();
updateScreen(VALID);
break;
}
#endif /* INSERT_EXPAND */
lnum = curwin->w_cursor.lnum + 1;
copychar:
if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
{
beep_flush();
break;
}
/* try to advance to the cursor column */
temp = 0;
ptr = ml_get(lnum);
while ((colnr_t)temp < curwin->w_virtcol && *ptr)
temp += lbr_chartabsize(ptr++, (colnr_t)temp);
if ((colnr_t)temp > curwin->w_virtcol)
--ptr;
if ((c = *ptr) == NUL)
{
beep_flush();
break;
}
/*FALLTHROUGH*/
default:
normalchar:
/*
* do some very smart indenting when entering '{' or '}'
*/
if (((did_si || can_si_back) && c == '{') ||
(can_si && c == '}'))
{
FPOS *pos, old_pos;
/* for '}' set indent equal to indent of line
* containing matching '{'
*/
if (c == '}' && (pos = findmatch('{')) != NULL)
{
old_pos = curwin->w_cursor;
/*
* If the matching '{' has a ')' immediately before it
* (ignoring white-space), then line up with the start
* of the line containing the matching '(' if there is
* one. This handles the case where an
* "if (..\n..) {" statement continues over multiple
* lines -- webb
*/
ptr = ml_get(pos->lnum);
i = pos->col;
if (i > 0) /* skip blanks before '{' */
while (--i > 0 && vim_iswhite(ptr[i]))
;
curwin->w_cursor.lnum = pos->lnum;
curwin->w_cursor.col = i;
if (ptr[i] == ')' && (pos = findmatch('(')) != NULL)
curwin->w_cursor = *pos;
i = get_indent();
curwin->w_cursor = old_pos;
set_indent(i, TRUE);
}
else if (curwin->w_cursor.col > 0)
{
/*
* when inserting '{' after "O" reduce indent, but not
* more than indent of previous line
*/
temp = TRUE;
if (c == '{' && can_si_back &&
curwin->w_cursor.lnum > 1)
{
old_pos = curwin->w_cursor;
i = get_indent();
while (curwin->w_cursor.lnum > 1)
{
ptr = skipwhite(
ml_get(--(curwin->w_cursor.lnum)));
/* ignore empty lines and lines starting with
* '#'.
*/
if (*ptr != '#' && *ptr != NUL)
break;
}
if (get_indent() >= i)
temp = FALSE;
curwin->w_cursor = old_pos;
}
if (temp)
shift_line(TRUE, FALSE, 1);
}
}
/* set indent of '#' always to 0 */
if (curwin->w_cursor.col > 0 && can_si && c == '#')
{
/* remember current indent for next line */
old_indent = get_indent();
set_indent(0, TRUE);
}
if (c == ' ')
{
#ifdef CINDENT
if (inindent(0))
can_cindent = FALSE;
#endif
if (Insstart_blank_vcol == MAXCOL &&
curwin->w_cursor.lnum == Insstart.lnum)
Insstart_blank_vcol = curwin->w_virtcol;
}
if (iswordchar(c) || !echeck_abbr(c))
{
insert_special(c, FALSE);
need_redraw = TRUE;
#ifdef RIGHTLEFT
revinslegal++;
revinschars++;
#endif
}
break;
} /* end of switch (c) */
#ifdef CINDENT
if (curbuf->b_p_cin && can_cindent
# ifdef INSERT_EXPAND
&& !ctrl_x_mode
# endif
)
{
force_cindent:
/*
* Indent now if a key was typed that is in 'cinkeys'.
*/
if (in_cinkeys(c, ' ', line_is_white))
{
stop_arrow();
/* re-indent the current line */
fixthisline(get_c_indent);
/* draw the changes on the screen later */
need_redraw = TRUE;
}
}
#endif /* CINDENT */
} /* for (;;) */
}
/*
* Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
* Keep the cursor on the same character.
* type == INDENT_INC increase indent (for CTRL-T or <Tab>)
* type == INDENT_DEC decrease indent (for CTRL-D)
* type == INDENT_SET set indent to "amount"
* if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
*/
static void
change_indent(type, amount, round)
int type;
int amount;
int round;
{
int vcol;
int last_vcol;
int insstart_less; /* reduction for Insstart.col */
int new_cursor_col;
int i;
char_u *ptr;
int save_p_list;
/* for the following tricks we don't want list mode */
save_p_list = curwin->w_p_list;
if (save_p_list)
{
curwin->w_p_list = FALSE;
curs_columns(FALSE); /* recompute w_virtcol */
}
vcol = curwin->w_virtcol;
/* determine offset from first non-blank */
new_cursor_col = curwin->w_cursor.col;
beginline(TRUE);
new_cursor_col -= curwin->w_cursor.col;
insstart_less = curwin->w_cursor.col;
/*
* If the cursor is in the indent, compute how many screen columns the
* cursor is to the left of the first non-blank.
*/
if (new_cursor_col < 0)
vcol = get_indent() - vcol;
/*
* Set the new indent. The cursor will be put on the first non-blank.
*/
if (type == INDENT_SET)
set_indent(amount, TRUE);
else
shift_line(type == INDENT_DEC, round, 1);
insstart_less -= curwin->w_cursor.col;
/*
* Try to put cursor on same character.
* If the cursor is at or after the first non-blank in the line,
* compute the cursor column relative to the column of the first
* non-blank character.
* If we are not in insert mode, leave the cursor on the first non-blank.
* If the cursor is before the first non-blank, position it relative
* to the first non-blank, counted in screen columns.
*/
if (new_cursor_col >= 0)
new_cursor_col += curwin->w_cursor.col;
else if (!(State & INSERT))
new_cursor_col = curwin->w_cursor.col;
else
{
/*
* Compute the screen column where the cursor should be.
*/
vcol = get_indent() - vcol;
curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
/*
* Advance the cursor until we reach the right screen column.
*/
vcol = last_vcol = 0;
new_cursor_col = -1;
ptr = ml_get_curline();
while (vcol <= (int)curwin->w_virtcol)
{
last_vcol = vcol;
++new_cursor_col;
vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_t)vcol);
}
vcol = last_vcol;
/*
* May need to insert spaces to be able to position the cursor on
* the right screen column.
*/
if (vcol != (int)curwin->w_virtcol)
{
curwin->w_cursor.col = new_cursor_col;
i = (int)curwin->w_virtcol - vcol;
ptr = alloc(i + 1);
if (ptr != NULL)
{
new_cursor_col += i;
ptr[i] = NUL;
while (--i >= 0)
ptr[i] = ' ';
ins_str(ptr);
vim_free(ptr);
}
}
/*
* When changing the indent while the cursor is in it, reset
* Insstart_col to 0.
*/
insstart_less = Insstart.col;
}
curwin->w_p_list = save_p_list;
if (new_cursor_col <= 0)
curwin->w_cursor.col = 0;
else
curwin->w_cursor.col = new_cursor_col;
curwin->w_set_curswant = TRUE;
/*
* May have to adjust the start of the insert.
*/
if ((State & INSERT) && curwin->w_cursor.lnum == Insstart.lnum &&
Insstart.col != 0)
{
if ((int)Insstart.col <= insstart_less)
Insstart.col = 0;
else
Insstart.col -= insstart_less;
}
}
#ifdef INSERT_EXPAND
/*
* Is the character 'c' a valid key to keep us in the current ctrl-x mode?
* -- webb
*/
int
is_ctrl_x_key(c)
int c;
{
switch (ctrl_x_mode)
{
case 0: /* Not in any ctrl-x mode */
break;
case CTRL_X_NOT_DEFINED_YET:
if (c == Ctrl('X') || c == Ctrl('Y') || c == Ctrl('E') ||
c == Ctrl('L') || c == Ctrl('F') || c == Ctrl(']') ||
c == Ctrl('I') || c == Ctrl('D') || c == Ctrl('P') ||
c == Ctrl('N'))
return TRUE;
break;
case CTRL_X_SCROLL:
if (c == Ctrl('Y') || c == Ctrl('E'))
return TRUE;
break;
case CTRL_X_WHOLE_LINE:
if (c == Ctrl('L') || c == Ctrl('P') || c == Ctrl('N'))
return TRUE;
break;
case CTRL_X_FILES:
if (c == Ctrl('F') || c == Ctrl('P') || c == Ctrl('N'))
return TRUE;
break;
case CTRL_X_DICTIONARY:
if (c == Ctrl('K') || c == Ctrl('P') || c == Ctrl('N'))
return TRUE;
break;
case CTRL_X_TAGS:
if (c == Ctrl(']') || c == Ctrl('P') || c == Ctrl('N'))
return TRUE;
break;
case CTRL_X_PATH_PATTERNS:
if (c == Ctrl('P') || c == Ctrl('N'))
return TRUE;
break;
case CTRL_X_PATH_DEFINES:
if (c == Ctrl('D') || c == Ctrl('P') || c == Ctrl('N'))
return TRUE;
break;
default:
emsg(e_internal);
break;
}
return FALSE;
}
/*
* This is like add_completion(), but if ic and inf are set, then the
* case of the originally typed text is used, and the case of the completed
* text is infered, ie this tries to work out what case you probably wanted
* the rest of the word to be in -- webb
*/
int
add_completion_and_infercase(str, len, fname, dir)
char_u *str;
int len;
char_u *fname;
int dir;
{
int has_lower = FALSE;
int was_letter = FALSE;
int orig_len;
int idx;
if (p_ic && curbuf->b_p_inf && len < IOSIZE)
{
/* Infer case of completed part -- webb */
orig_len = STRLEN(original_text);
/* Use IObuff, str would change text in buffer! */
STRNCPY(IObuff, str, len);
IObuff[len] = NUL;
/* Rule 1: Were any chars converted to lower? */
for (idx = 0; idx < orig_len; ++idx)
{
if (islower(original_text[idx]))
{
has_lower = TRUE;
if (isupper(IObuff[idx]))
{
/* Rule 1 is satisfied */
for (idx = orig_len; idx < len; ++idx)
IObuff[idx] = TO_LOWER(IObuff[idx]);
break;
}
}
}
/*
* Rule 2: No lower case, 2nd consecutive letter converted to
* upper case.
*/
if (!has_lower)
{
for (idx = 0; idx < orig_len; ++idx)
{
if (was_letter && isupper(original_text[idx]) &&
islower(IObuff[idx]))
{
/* Rule 2 is satisfied */
for (idx = orig_len; idx < len; ++idx)
IObuff[idx] = TO_UPPER(IObuff[idx]);
break;
}
was_letter = isalpha(original_text[idx]);
}
}
/* Copy the original case of the part we typed */
STRNCPY(IObuff, original_text, orig_len);
return add_completion(IObuff, len, fname, dir);
}
return add_completion(str, len, fname, dir);
}
/*
* Add a match to the list of matches.
* If the given string is already in the list of completions, then return
* FAIL, otherwise add it to the list and return OK. If there is an error,
* maybe because alloc returns NULL, then RET_ERROR is returned -- webb.
*/
static int
add_completion(str, len, fname, dir)
char_u *str;
int len;
char_u *fname;
int dir;
{
struct Completion *match;
mch_breakcheck();
if (got_int)
return RET_ERROR;
if (len < 0)
len = STRLEN(str);
/*
* If the same match is already present, don't add it.
*/
if (first_match != NULL)
{
match = first_match;
do
{
if (STRNCMP(match->str, str, (size_t)len) == 0 &&
match->str[len] == NUL)
return FAIL;
match = match->next;
} while (match != NULL && match != first_match);
}
/*
* Allocate a new match structure.
* Copy the values to the new match structure.
*/
match = (struct Completion *)alloc((unsigned)sizeof(struct Completion));
if (match == NULL)
return RET_ERROR;
if ((match->str = strnsave(str, len)) == NULL)
{
vim_free(match);
return RET_ERROR;
}
if (fname != NULL)
match->fname = strsave(fname); /* ignore errors */
else
match->fname = NULL;
match->original = FALSE;
/*
* Link the new match structure in the list of matches.
*/
if (first_match == NULL)
{
first_match = curr_match = match;
curr_match->next = curr_match->prev = NULL;
}
else
{
if (dir == FORWARD)
{
match->next = NULL;
match->prev = curr_match;
curr_match->next = match;
curr_match = match;
}
else /* BACKWARD */
{
match->prev = NULL;
match->next = curr_match;
curr_match->prev = match;
first_match = curr_match = match;
}
}
return OK;
}
/*
* Make the completion list cyclic. Add the original text at the end.
* Return the number of matches (excluding the original).
*/
static int
make_cyclic()
{
struct Completion *match, *orig;
int count = 0;
if (first_match != NULL)
{
/*
* Find the end of the list.
*/
match = first_match;
count = 1;
while (match->next != NULL)
{
match = match->next;
++count;
}
if (original_text != NULL)
{
/*
* Allocate a new structure for the original text.
* Copy the original text to the new structure.
* Link it in the list at the end.
*/
orig = (struct Completion *)alloc((unsigned)sizeof(
struct Completion));
if (orig != NULL)
{
if ((orig->str = strsave(original_text)) == NULL)
vim_free(orig);
else
{
orig->fname = NULL;
orig->original = TRUE;
orig->prev = match;
match->next = orig;
match = orig;
curr_match = orig;
}
}
}
match->next = first_match;
first_match->prev = match;
}
return count;
}
/*
* Add any identifiers that match the given pattern to the list of
* completions.
*/
static void
complete_dictionaries(pat, dir)
char_u *pat;
int dir;
{
struct Completion *save_curr_match = curr_match;
char_u *dict = p_dict;
char_u *ptr;
char_u *buf;
char_u *fname;
int at_start;
FILE *fp;
struct regexp *prog = NULL;
if ((buf = alloc(LSIZE)) == NULL)
return;
if (curr_match != NULL)
{
while (curr_match->next != NULL)
curr_match = curr_match->next;
}
if (*dict != NUL)
{
(void)set_highlight('r');
msg_highlight = TRUE;
MSG("Please wait, searching dictionaries");
set_reg_ic(pat); /* set reg_ic according to p_ic, p_scs and pat */
reg_magic = p_magic;
prog = vim_regcomp(pat);
}
while (*dict != NUL && prog != NULL && !got_int)
{
/* copy one dictionary file name into buf */
(void)copy_option_part(&dict, buf, LSIZE, ",");
fp = fopen((char *)buf, "r"); /* open dictionary file */
if (fp != NULL)
{
fname = strsave(buf); /* keep name of file */
/*
* Read dictionary file line by line.
* Check each line for a match.
*/
while (!got_int && !vim_fgets(buf, LSIZE, fp))
{
ptr = buf;
at_start = TRUE;
while (vim_regexec(prog, ptr, at_start))
{
at_start = FALSE;
ptr = prog->startp[0];
while (iswordchar(*ptr))
++ptr;
if (add_completion_and_infercase(prog->startp[0],
(int)(ptr - prog->startp[0]), fname, FORWARD)
== RET_ERROR)
break;
}
line_breakcheck();
}
fclose(fp);
vim_free(fname);
}
}
vim_free(prog);
if (save_curr_match != NULL)
curr_match = save_curr_match;
else if (dir == BACKWARD)
curr_match = first_match;
vim_free(buf);
}
/*
* Free the list of completions
*/
static void
free_completions()
{
struct Completion *match;
if (first_match == NULL)
return;
curr_match = first_match;
do
{
match = curr_match;
curr_match = curr_match->next;
vim_free(match->str);
vim_free(match->fname);
vim_free(match);
} while (curr_match != NULL && curr_match != first_match);
first_match = curr_match = NULL;
}
/*
* Return the number of items in the Completion list
*/
static int
count_completions()
{
struct Completion *match;
int num = 0;
if (first_match == NULL)
return 0;
match = first_match;
do
{
if (!match->original) /* original string doesn't count */
num++;
match = match->next;
} while (match != NULL && match != first_match);
return num;
}
#endif /* INSERT_EXPAND */
/*
* Next character is interpreted literally.
* A one, two or three digit decimal number is interpreted as its byte value.
* If one or two digits are entered, the next character is given to vungetc().
*/
int
get_literal()
{
int cc;
int nc;
int i;
if (got_int)
return Ctrl('C');
#ifdef USE_GUI
/*
* In GUI there is no point inserting the internal code for a special key.
* It is more useful to insert the string "<KEY>" instead. This would
* probably be useful in a text window too, but it would not be
* vi-compatible (maybe there should be an option for it?) -- webb
*/
if (gui.in_use)
++allow_keys;
#endif
++no_mapping; /* don't map the next key hits */
cc = 0;
for (i = 0; i < 3; ++i)
{
do
nc = vgetc();
while (nc == K_IGNORE || nc == K_SCROLLBAR || nc == K_HORIZ_SCROLLBAR);
if (!(State & CMDLINE))
add_to_showcmd(nc, FALSE);
if (IS_SPECIAL(nc) || !isdigit(nc))
break;
cc = cc * 10 + nc - '0';
if (cc > 255)
cc = 255; /* limit range to 0-255 */
nc = 0;
}
if (i == 0) /* no number entered */
{
if (nc == K_ZERO) /* NUL is stored as NL */
{
cc = '\n';
nc = 0;
}
else
{
cc = nc;
nc = 0;
}
}
if (cc == 0) /* NUL is stored as NL */
cc = '\n';
--no_mapping;
#ifdef USE_GUI
if (gui.in_use)
--allow_keys;
#endif
if (nc)
vungetc(nc);
got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
return cc;
}
/*
* Insert character, taking care of special keys and mod_mask
*/
static void
insert_special(c, allow_modmask)
int c;
int allow_modmask;
{
char_u *p;
int len;
/*
* Special function key, translate into "<Key>". Up to the last '>' is
* inserted with ins_str(), so as not to replace characters in replace
* mode.
* Only use mod_mask for special keys, to avoid things like <S-Space>,
* unless 'allow_modmask' is TRUE.
*/
if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
{
p = get_special_key_name(c, mod_mask);
len = STRLEN(p);
c = p[len - 1];
if (len > 2)
{
p[len - 1] = NUL;
ins_str(p);
AppendToRedobuff(p);
}
}
insertchar(c, FALSE, -1);
}
/*
* Special characters in this context are those that need processing other
* than the simple insertion that can be performed here. This includes ESC
* which terminates the insert, and CR/NL which need special processing to
* open up a new line. This routine tries to optimize insertions performed by
* the "redo", "undo" or "put" commands, so it needs to know when it should
* stop and defer processing to the "normal" mechanism.
*/
#define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL)
void
insertchar(c, force_formatting, second_indent)
unsigned c;
int force_formatting; /* format line regardless of p_fo */
int second_indent; /* indent for second line if >= 0 */
{
int haveto_redraw = FALSE;
int textwidth;
colnr_t leader_len;
int first_line = TRUE;
int fo_ins_blank;
int save_char = NUL;
stop_arrow();
/*
* find out textwidth to be used:
* if 'textwidth' option is set, use it
* else if 'wrapmargin' option is set, use Columns - 'wrapmargin'
* if invalid value, use 0.
* Set default to window width (maximum 79) for "Q" command.
*/
textwidth = curbuf->b_p_tw;
if (textwidth == 0 && curbuf->b_p_wm)
textwidth = Columns - curbuf->b_p_wm;
if (textwidth < 0)
textwidth = 0;
if (force_formatting && textwidth == 0)
{
textwidth = Columns - 1;
if (textwidth > 79)
textwidth = 79;
}
fo_ins_blank = has_format_option(FO_INS_BLANK);
/*
* Try to break the line in two or more pieces when:
* - Always do this if we have been called to do formatting only.
* - Otherwise:
* - Don't do this if inserting a blank
* - Don't do this if an existing character is being replaced.
* - Do this if the cursor is not on the line where insert started
* or - 'formatoptions' doesn't have 'l' or the line was not too long
* before the insert.
* - 'formatoptions' doesn't have 'b' or a blank was inserted at or
* before 'textwidth'
*/
if (force_formatting || (!vim_iswhite(c) &&
!(State == REPLACE && *ml_get_cursor() != NUL) &&
(curwin->w_cursor.lnum != Insstart.lnum ||
((!has_format_option(FO_INS_LONG) ||
Insstart_textlen <= (colnr_t)textwidth) &&
(!fo_ins_blank || Insstart_blank_vcol <= (colnr_t)textwidth)))))
{
/*
* When 'ai' is off we don't want a space under the cursor to be
* deleted. Replace it with an 'x' temporarily.
*/
if (!curbuf->b_p_ai && vim_iswhite(gchar_cursor()))
{
save_char = gchar_cursor();
pchar_cursor('x');
}
while (textwidth && curwin->w_virtcol >= (colnr_t)textwidth)
{
int startcol; /* Cursor column at entry */
int wantcol; /* column at textwidth border */
int foundcol; /* column for start of spaces */
int end_foundcol = 0; /* column for start of word */
colnr_t len;
if (!force_formatting && has_format_option(FO_WRAP_COMS))
fo_do_comments = TRUE;
/* Don't break until after the comment leader */
leader_len = get_leader_len(ml_get_curline(), NULL);
if (!force_formatting && leader_len == 0 &&
!has_format_option(FO_WRAP))
{
textwidth = 0;
break;
}
if ((startcol = curwin->w_cursor.col) == 0)
break;
/* find column of textwidth border */
coladvance((colnr_t)textwidth);
wantcol = curwin->w_cursor.col;
curwin->w_cursor.col = startcol - 1;
foundcol = 0;
/*
* Find position to break at.
* Stop at start of line.
* Stop at first entered white when 'formatoptions' has 'v'
*/
while (curwin->w_cursor.col > 0 &&
((!fo_ins_blank && !has_format_option(FO_INS_VI)) ||
curwin->w_cursor.lnum != Insstart.lnum ||
curwin->w_cursor.col >= Insstart.col))
{
if (vim_iswhite(gchar_cursor()))
{
/* remember position of blank just before text */
end_foundcol = curwin->w_cursor.col;
while (curwin->w_cursor.col > 0 &&
vim_iswhite(gchar_cursor()))
--curwin->w_cursor.col;
if (curwin->w_cursor.col == 0 &&
vim_iswhite(gchar_cursor()))
break; /* only spaces in front of text */
/* Don't break until after the comment leader */
if (curwin->w_cursor.col < leader_len)
break;
foundcol = curwin->w_cursor.col + 1;
if (curwin->w_cursor.col < (colnr_t)wantcol)
break;
}
--curwin->w_cursor.col;
}
if (foundcol == 0) /* no spaces, cannot break line */
{
curwin->w_cursor.col = startcol;
break;
}
/*
* offset between cursor position and line break is used by
* replace stack functions
*/
replace_offset = startcol - end_foundcol - 1;
/*
* adjust startcol for spaces that will be deleted and
* characters that will remain on top line
*/
curwin->w_cursor.col = foundcol;
while (vim_iswhite(gchar_cursor()))
{
++curwin->w_cursor.col;
--startcol;
}
startcol -= foundcol;
if (startcol < 0)
startcol = 0;
/* put cursor after pos. to break line */
curwin->w_cursor.col = foundcol;
Opencmd(FORWARD, FALSE, TRUE);
replace_offset = 0;
if (second_indent >= 0 && first_line)
set_indent(second_indent, TRUE);
first_line = FALSE;
/*
* check if cursor is not past the NUL off the line, cindent may
* have added or removed indent.
*/
curwin->w_cursor.col += startcol;
len = STRLEN(ml_get_curline());
if (curwin->w_cursor.col > len)
curwin->w_cursor.col = len;
curs_columns(FALSE); /* update curwin->w_virtcol */
haveto_redraw = TRUE;
#ifdef CINDENT
can_cindent = TRUE;
#endif
}
if (save_char) /* put back space after cursor */
pchar_cursor(save_char);
if (c == NUL) /* formatting only */
return;
fo_do_comments = FALSE;
if (haveto_redraw)
{
/*
* If the cursor ended up just below the screen we scroll up here
* to avoid a redraw of the whole screen in the most common cases.
*/
if (curwin->w_cursor.lnum == curwin->w_botline &&
!curwin->w_empty_rows)
win_del_lines(curwin, 0, 1, TRUE, TRUE);
updateScreen(CURSUPD);
}
}
if (c == NUL) /* only formatting was wanted */
return;
did_ai = FALSE;
did_si = FALSE;
can_si = FALSE;
can_si_back = FALSE;
/*
* If there's any pending input, grab up to INPUT_BUFLEN at once.
* This speeds up normal text input considerably.
* Don't do this when 'cindent' is set, because we might need to re-indent
* at a ':', or any other character.
*/
#define INPUT_BUFLEN 100
if (!ISSPECIAL(c) && vpeekc() != NUL && State != REPLACE
#ifdef CINDENT
&& !curbuf->b_p_cin
#endif
#ifdef RIGHTLEFT
&& !p_ri
#endif
)
{
char_u p[INPUT_BUFLEN + 1];
int i;
p[0] = c;
i = 1;
while ((c = vpeekc()) != NUL && !ISSPECIAL(c) && i < INPUT_BUFLEN &&
(textwidth == 0 ||
(curwin->w_virtcol += charsize(p[i - 1])) < (colnr_t)textwidth) &&
!(!no_abbr && !iswordchar(c) && iswordchar(p[i - 1])))
{
#ifdef RIGHTLEFT
c = vgetc();
if (p_hkmap && KeyTyped)
c = hkmap(c); /* Hebrew mode mapping */
p[i++] = c;
#else
p[i++] = vgetc();
#endif
}
#ifdef DIGRAPHS
do_digraph(-1); /* clear digraphs */
do_digraph(p[i-1]); /* may be the start of a digraph */
#endif
p[i] = '\0';
ins_str(p);
AppendToRedobuff(p);
}
else
{
ins_char(c);
AppendCharToRedobuff(c);
}
}
/*
* start_arrow() is called when an arrow key is used in insert mode.
* It resembles hitting the <ESC> key.
*/
static void
start_arrow(end_insert_pos)
FPOS *end_insert_pos;
{
if (!arrow_used) /* something has been inserted */
{
AppendToRedobuff(ESC_STR);
arrow_used = TRUE; /* this means we stopped the current insert */
stop_insert(end_insert_pos);
}
}
/*
* stop_arrow() is called before a change is made in insert mode.
* If an arrow key has been used, start a new insertion.
*/
static void
stop_arrow()
{
if (arrow_used)
{
(void)u_save_cursor(); /* errors are ignored! */
Insstart = curwin->w_cursor; /* new insertion starts here */
Insstart_textlen = linetabsize(ml_get_curline());
ResetRedobuff();
AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
arrow_used = FALSE;
}
}
/*
* do a few things to stop inserting
*/
static void
stop_insert(end_insert_pos)
FPOS *end_insert_pos; /* where insert ended */
{
stop_redo_ins();
/*
* save the inserted text for later redo with ^@
*/
vim_free(last_insert);
last_insert = get_inserted();
last_insert_skip = new_insert_skip;
/*
* If we just did an auto-indent, remove the white space from the end of
* the line, and put the cursor back.
*/
if (did_ai && !arrow_used)
{
if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
--curwin->w_cursor.col;
while (vim_iswhite(gchar_cursor()))
delchar(TRUE);
if (gchar_cursor() != NUL)
++curwin->w_cursor.col; /* put cursor back on the NUL */
if (curwin->w_p_list) /* the deletion is only seen in list
* mode */
updateline();
}
did_ai = FALSE;
did_si = FALSE;
can_si = FALSE;
can_si_back = FALSE;
/* set '[ and '] to the inserted text */
curbuf->b_op_start = Insstart;
curbuf->b_op_end = *end_insert_pos;
}
/*
* Set the last inserted text to a single character.
* Used for the replace command.
*/
void
set_last_insert(c)
int c;
{
vim_free(last_insert);
last_insert = alloc(4);
if (last_insert != NULL)
{
last_insert[0] = Ctrl('V');
last_insert[1] = c;
last_insert[2] = ESC;
last_insert[3] = NUL;
/* Use the CTRL-V only when not entering a digit */
last_insert_skip = isdigit(c) ? 1 : 0;
}
}
/*
* move cursor to start of line
* if flag == TRUE move to first non-white
* if flag == MAYBE then move to first non-white if startofline is set,
* otherwise don't move at all.
*/
void
beginline(flag)
int flag;
{
if (flag == MAYBE && !p_sol)
coladvance(curwin->w_curswant);
else
{
curwin->w_cursor.col = 0;
if (flag)
{
register char_u *ptr;
for (ptr = ml_get_curline(); vim_iswhite(*ptr); ++ptr)
++curwin->w_cursor.col;
}
curwin->w_set_curswant = TRUE;
}
}
/*
* oneright oneleft cursor_down cursor_up
*
* Move one char {right,left,down,up}.
* Return OK when successful, FAIL when we hit a line of file boundary.
*/
int
oneright()
{
char_u *ptr;
ptr = ml_get_cursor();
if (*ptr++ == NUL || *ptr == NUL)
return FAIL;
curwin->w_set_curswant = TRUE;
++curwin->w_cursor.col;
return OK;
}
int
oneleft()
{
if (curwin->w_cursor.col == 0)
return FAIL;
curwin->w_set_curswant = TRUE;
--curwin->w_cursor.col;
return OK;
}
int
cursor_up(n)
long n;
{
if (n != 0 && curwin->w_cursor.lnum == 1)
return FAIL;
if (n >= curwin->w_cursor.lnum)
curwin->w_cursor.lnum = 1;
else
curwin->w_cursor.lnum -= n;
/* try to advance to the column we want to be at */
coladvance(curwin->w_curswant);
if (op_type == NOP)
cursupdate(); /* make sure curwin->w_topline is valid */
return OK;
}
int
cursor_down(n)
long n;
{
if (n != 0 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
return FAIL;
curwin->w_cursor.lnum += n;
if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
/* try to advance to the column we want to be at */
coladvance(curwin->w_curswant);
if (op_type == NOP)
cursupdate(); /* make sure curwin->w_topline is valid */
return OK;
}
/*
* screengo() --
*
* move 'dist' lines in direction 'dir', counting lines by *screen*
* lines rather than lines in the file
* 'dist' must be positive.
*
* return OK if able to move cursor, FAIL otherwise.
*/
int
screengo(dir, dist)
int dir;
long dist;
{
int linelen = linetabsize(ml_get_curline());
int retval = OK;
int atend = FALSE;
int n;
op_motion_type = MCHAR;
op_inclusive = FALSE;
/*
* Instead of sticking at the last character of the line in the file we
* try to stick in the last column of the screen
*/
if (curwin->w_curswant == MAXCOL)
{
atend = TRUE;
curwin->w_curswant = ((curwin->w_virtcol +
(curwin->w_p_nu ? 8 : 0)) / Columns + 1) * Columns - 1;
if (curwin->w_p_nu && curwin->w_curswant > 8)
curwin->w_curswant -= 8;
}
else
while (curwin->w_curswant >= (colnr_t)(linelen + Columns))
curwin->w_curswant -= Columns;
while (dist--)
{
if (dir == BACKWARD)
{
/* move back within line */
if ((long)curwin->w_curswant >= Columns)
curwin->w_curswant -= Columns;
else /* to previous line */
{
if (curwin->w_cursor.lnum == 1)
{
retval = FAIL;
break;
}
--curwin->w_cursor.lnum;
linelen = linetabsize(ml_get_curline());
n = ((linelen + (curwin->w_p_nu ? 8 : 0) - 1) / Columns)
* Columns;
if (curwin->w_p_nu &&
(long)curwin->w_curswant >= Columns - 8 && n)
n -= Columns;
curwin->w_curswant += n;
}
}
else /* dir == FORWARD */
{
n = ((linelen + (curwin->w_p_nu ? 8 : 0) - 1) / Columns) * Columns;
if (curwin->w_p_nu && n > 8)
n -= 8;
/* move forward within line */
if (curwin->w_curswant < (colnr_t)n)
curwin->w_curswant += Columns;
else /* to next line */
{
if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
{
retval = FAIL;
break;
}
curwin->w_cursor.lnum++;
linelen = linetabsize(ml_get_curline());
curwin->w_curswant %= Columns;
}
}
}
coladvance(curwin->w_curswant);
if (atend)
curwin->w_curswant = MAXCOL; /* stick in the last column */
if (op_type == NOP)
cursupdate();
return retval;
}
/*
* move screen 'count' pages up or down and update screen
*
* return FAIL for failure, OK otherwise
*/
int
onepage(dir, count)
int dir;
long count;
{
linenr_t lp;
long n;
int off;
if (curbuf->b_ml.ml_line_count == 1) /* nothing to do */
return FAIL;
for ( ; count > 0; --count)
{
/*
* It's an error to move a page up when the first line is already on
* the screen. It's an error to move a page down when the last line
* is on the screen and the topline is 'scrolloff' lines from the
* last line.
*/
if (dir == FORWARD
? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - p_so) &&
curwin->w_botline > curbuf->b_ml.ml_line_count)
: (curwin->w_topline == 1))
{
beep_flush();
return FAIL;
}
if (dir == FORWARD)
{
/* at end of file */
if (curwin->w_botline > curbuf->b_ml.ml_line_count)
curwin->w_topline = curbuf->b_ml.ml_line_count;
else
{
/*
* When there are three or less lines on the screen, move them
* all to above the screen.
*/
if (curwin->w_botline - curwin->w_topline <= 3)
off = 0;
/*
* Make sure at least w_botline gets onto the screen, also
* when 'scrolloff' is non-zero and with very long lines.
*/
else if (plines(curwin->w_botline) +
plines(curwin->w_botline - 1) +
plines(curwin->w_botline - 2) >= curwin->w_height - 2)
off = 0;
else
off = 2;
curwin->w_topline = curwin->w_botline - off;
curwin->w_cursor.lnum = curwin->w_topline;
}
comp_Botline(curwin);
}
else /* dir == BACKWARDS */
{
lp = curwin->w_topline;
/*
* If the first two lines on the screen are not too big, we keep
* them on the screen.
*/
if ((n = plines(lp)) > curwin->w_height / 2)
--lp;
else if (lp < curbuf->b_ml.ml_line_count &&
n + plines(lp + 1) < curwin->w_height / 2)
++lp;
curwin->w_cursor.lnum = lp;
n = 0;
while (n <= curwin->w_height && lp >= 1)
{
n += plines(lp);
--lp;
}
if (n <= curwin->w_height) /* at begin of file */
{
curwin->w_topline = 1;
comp_Botline(curwin);
}
else if (lp >= curwin->w_topline - 2) /* very long lines */
{
--curwin->w_topline;
comp_Botline(curwin);
curwin->w_cursor.lnum = curwin->w_botline - 1;
}
else
{
curwin->w_topline = lp + 2;
comp_Botline(curwin);
}
}
}
cursor_correct();
beginline(MAYBE);
/*
* Avoid the screen jumping up and down when 'scrolloff' is non-zero.
*/
if (dir == FORWARD && curwin->w_cursor.lnum < curwin->w_topline + p_so)
scroll_cursor_top(1, FALSE);
updateScreen(VALID);
return OK;
}
/* #define KEEP_SCREEN_LINE */
void
halfpage(flag, Prenum)
int flag;
linenr_t Prenum;
{
long scrolled = 0;
int i;
int n;
if (Prenum)
curwin->w_p_scroll = (Prenum > curwin->w_height) ?
curwin->w_height : Prenum;
n = (curwin->w_p_scroll <= curwin->w_height) ?
curwin->w_p_scroll : curwin->w_height;
if (flag) /* scroll down */
{
while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
{
i = plines(curwin->w_topline);
n -= i;
if (n < 0 && scrolled)
break;
scrolled += i;
++curwin->w_topline;
comp_Botline(curwin); /* compute curwin->w_botline */
#ifndef KEEP_SCREEN_LINE
if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
++curwin->w_cursor.lnum;
#endif
}
#ifndef KEEP_SCREEN_LINE
/*
* When hit bottom of the file: move cursor down.
*/
if (n > 0)
{
curwin->w_cursor.lnum += n;
if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
}
#else
/* try to put the cursor in the same screen line */
while ((curwin->w_cursor.lnum < curwin->w_topline || scrolled > 0)
&& curwin->w_cursor.lnum < curwin->w_botline - 1)
{
scrolled -= plines(curwin->w_cursor.lnum);
if (scrolled < 0 && curwin->w_cursor.lnum >= curwin->w_topline)
break;
++curwin->w_cursor.lnum;
}
#endif
}
else /* scroll up */
{
while (n > 0 && curwin->w_topline > 1)
{
i = plines(curwin->w_topline - 1);
n -= i;
if (n < 0 && scrolled)
break;
scrolled += i;
--curwin->w_topline;
#ifndef KEEP_SCREEN_LINE
if (curwin->w_cursor.lnum > 1)
--curwin->w_cursor.lnum;
#endif
}
comp_Botline(curwin); /* compute curwin->w_botline */
#ifndef KEEP_SCREEN_LINE
/*
* When hit top of the file: move cursor up.
*/
if (n > 0)
{
if (curwin->w_cursor.lnum > (linenr_t)n)
curwin->w_cursor.lnum -= n;
else
curwin->w_cursor.lnum = 1;
}
#else
/* try to put the cursor in the same screen line */
scrolled += n; /* move cursor when topline is 1 */
while (curwin->w_cursor.lnum > curwin->w_topline &&
(scrolled > 0 || curwin->w_cursor.lnum >= curwin->w_botline))
{
scrolled -= plines(curwin->w_cursor.lnum - 1);
if (scrolled < 0 && curwin->w_cursor.lnum < curwin->w_botline)
break;
--curwin->w_cursor.lnum;
}
#endif
}
cursor_correct();
beginline(MAYBE);
updateScreen(VALID);
}
/*
* Stuff the last inserted text in the read buffer.
* Last_insert actually is a copy of the redo buffer, so we
* first have to remove the command.
*/
int
stuff_inserted(c, count, no_esc)
int c;
long count;
int no_esc;
{
char_u *esc_ptr = NULL;
char_u *ptr;
ptr = get_last_insert();
if (ptr == NULL)
{
EMSG(e_noinstext);
return FAIL;
}
if (c)
stuffcharReadbuff(c);
if (no_esc && (esc_ptr = (char_u *)vim_strrchr(ptr, 27)) != NULL)
*esc_ptr = NUL; /* remove the ESC */
do
stuffReadbuff(ptr);
while (--count > 0);
if (esc_ptr != NULL)
*esc_ptr = 27; /* put the ESC back */
return OK;
}
char_u *
get_last_insert()
{
if (last_insert == NULL)
return NULL;
return last_insert + last_insert_skip;
}
/*
* Check the word in front of the cursor for an abbreviation.
* Called when the non-id character "c" has been entered.
* When an abbreviation is recognized it is removed from the text and
* the replacement string is inserted in typebuf[], followed by "c".
*/
static int
echeck_abbr(c)
int c;
{
if (p_paste || no_abbr) /* no abbreviations or in paste mode */
return FALSE;
return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
}
/*
* replace-stack functions
*
* When replacing characters the replaced character is remembered
* for each new character. This is used to re-insert the old text
* when backspacing.
*
* replace_offset is normally 0, in which case replace_push will add a new
* character at the end of the stack. If replace_offset is not 0, that many
* characters will be left on the stack above the newly inserted character.
*/
char_u *replace_stack = NULL;
long replace_stack_nr = 0; /* next entry in replace stack */
long replace_stack_len = 0; /* max. number of entries */
void
replace_push(c)
int c; /* character that is replaced (NUL is none) */
{
char_u *p;
if (replace_stack_nr < replace_offset) /* nothing to do */
return;
if (replace_stack_len <= replace_stack_nr)
{
replace_stack_len += 50;
p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
if (p == NULL) /* out of memory */
{
replace_stack_len -= 50;
return;
}
if (replace_stack != NULL)
{
vim_memmove(p, replace_stack,
(size_t)(replace_stack_nr * sizeof(char_u)));
vim_free(replace_stack);
}
replace_stack = p;
}
p = replace_stack + replace_stack_nr - replace_offset;
if (replace_offset)
vim_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
*p = c;
++replace_stack_nr;
}
/*
* pop one item from the replace stack
* return -1 if stack empty
* return 0 if no character was replaced
* return replaced character otherwise
*/
int
replace_pop()
{
if (replace_stack_nr == 0)
return -1;
return (int)replace_stack[--replace_stack_nr];
}
/*
* make the replace stack empty
* (called when exiting replace mode)
*/
void
replace_flush()
{
vim_free(replace_stack);
replace_stack = NULL;
replace_stack_len = 0;
replace_stack_nr = 0;
}
#if defined(LISPINDENT) || defined(CINDENT)
/*
* Re-indent the current line, based on the current contents of it and the
* surrounding lines. Fixing the cursor position seems really easy -- I'm very
* confused what all the part that handles Control-T is doing that I'm not.
* "get_the_indent" should be get_c_indent or get_lisp_indent.
*/
void
fixthisline(get_the_indent)
int (*get_the_indent) __ARGS((void));
{
change_indent(INDENT_SET, get_the_indent(), FALSE);
if (linewhite(curwin->w_cursor.lnum))
did_ai = TRUE; /* delete the indent if the line stays empty */
}
#endif /* defined(LISPINDENT) || defined(CINDENT) */
#ifdef CINDENT
/*
* return TRUE if 'cinkeys' contains the key "keytyped",
* when == '*': Only if key is preceded with '*' (indent before insert)
* when == '!': Only if key is prededed with '!' (don't insert)
* when == ' ': Only if key is not preceded with '*'(indent afterwards)
*
* If line_is_empty is TRUE accept keys with '0' before them.
*/
int
in_cinkeys(keytyped, when, line_is_empty)
int keytyped;
int when;
int line_is_empty;
{
char_u *look;
int try_match;
char_u *p;
for (look = curbuf->b_p_cink; *look; )
{
/*
* Find out if we want to try a match with this key, depending on
* 'when' and a '*' or '!' before the key.
*/
switch (when)
{
case '*': try_match = (*look == '*'); break;
case '!': try_match = (*look == '!'); break;
default: try_match = (*look != '*'); break;
}
if (*look == '*' || *look == '!')
++look;
/*
* If there is a '0', only accept a match if the line is empty.
*/
if (*look == '0')
{
if (!line_is_empty)
try_match = FALSE;
++look;
}
/*
* does it look like a control character?
*/
if (*look == '^' && look[1] >= '@' && look[1] <= '_')
{
if (try_match && keytyped == Ctrl(look[1]))
return TRUE;
look += 2;
}
/*
* 'o' means "o" command, open forward.
* 'O' means "O" command, open backward.
*/
else if (*look == 'o')
{
if (try_match && keytyped == KEY_OPEN_FORW)
return TRUE;
++look;
}
else if (*look == 'O')
{
if (try_match && keytyped == KEY_OPEN_BACK)
return TRUE;
++look;
}
/*
* 'e' means to check for "else" at start of line and just before the
* cursor.
*/
else if (*look == 'e')
{
if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
{
p = ml_get_curline();
if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
return TRUE;
}
++look;
}
/*
* ':' only causes an indent if it is at the end of a label or case
* statement.
*/
else if (*look == ':')
{
if (try_match && keytyped == ':')
{
p = ml_get_curline();
if (iscase(p) || islabel(30))
return TRUE;
}
++look;
}
/*
* Is it a key in <>, maybe?
*/
else if (*look == '<')
{
if (try_match)
{
/*
* make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>
* and <!> so that people can re-indent on o, O, e, 0, <, >, *
* and ! keys if they really really want to.
*/
if (vim_strchr((char_u *)"<>!*oOe0", look[1]) != NULL &&
keytyped == look[1])
return TRUE;
if (keytyped == get_special_key_code(look + 1))
return TRUE;
}
while (*look && *look != '>')
look++;
while (*look == '>')
look++;
}
/*
* ok, it's a boring generic character.
*/
else
{
if (try_match && *look == keytyped)
return TRUE;
++look;
}
/*
* Skip over ", ".
*/
look = skip_to_option_part(look);
}
return FALSE;
}
#endif /* CINDENT */
#if defined(RIGHTLEFT) || defined(PROTO)
/*
* Map Hebrew keyboard when in hkmap mode.
*/
int
hkmap(c)
int c;
{
switch(c)
{
case '`': return ';';
case '/': return '.';
case '\'': return ',';
case 'q': return '/';
case 'w': return '\'';
/* Hebrew letters - set offset from 'a' */
case ',': c = '{'; break;
case '.': c = 'v'; break;
case ';': c = 't'; break;
default: {
static char str[] = "zqbcxlsjphmkwonu ydafe rig";
if (c < 'a' || c > 'z')
return c;
c = str[c - 'a'];
break;
}
}
return c - 'a' + p_aleph;
}
#endif
|