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
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
|
/* vi:set ts=8 sts=4 sw=4 noet:
*
* 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.
* See README.txt for an overview of the Vim source code.
*/
/*
* misc2.c: Various functions.
*/
#include "vim.h"
static char_u *username = NULL; // cached result of mch_get_user_name()
static int coladvance2(pos_T *pos, int addspaces, int finetune, colnr_T wcol);
/*
* Return TRUE if in the current mode we need to use virtual.
*/
int
virtual_active(void)
{
// While an operator is being executed we return "virtual_op", because
// VIsual_active has already been reset, thus we can't check for "block"
// being used.
if (virtual_op != MAYBE)
return virtual_op;
return (ve_flags == VE_ALL
|| ((ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V)
|| ((ve_flags & VE_INSERT) && (State & INSERT)));
}
/*
* Get the screen position of the cursor.
*/
int
getviscol(void)
{
colnr_T x;
getvvcol(curwin, &curwin->w_cursor, &x, NULL, NULL);
return (int)x;
}
/*
* Go to column "wcol", and add/insert white space as necessary to get the
* cursor in that column.
* The caller must have saved the cursor line for undo!
*/
int
coladvance_force(colnr_T wcol)
{
int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol);
if (wcol == MAXCOL)
curwin->w_valid &= ~VALID_VIRTCOL;
else
{
// Virtcol is valid
curwin->w_valid |= VALID_VIRTCOL;
curwin->w_virtcol = wcol;
}
return rc;
}
/*
* Get the screen position of character col with a coladd in the cursor line.
*/
int
getviscol2(colnr_T col, colnr_T coladd UNUSED)
{
colnr_T x;
pos_T pos;
pos.lnum = curwin->w_cursor.lnum;
pos.col = col;
pos.coladd = coladd;
getvvcol(curwin, &pos, &x, NULL, NULL);
return (int)x;
}
/*
* Try to advance the Cursor to the specified screen column.
* If virtual editing: fine tune the cursor position.
* Note that all virtual positions off the end of a line should share
* a curwin->w_cursor.col value (n.b. this is equal to STRLEN(line)),
* beginning at coladd 0.
*
* return OK if desired column is reached, FAIL if not
*/
int
coladvance(colnr_T wcol)
{
int rc = getvpos(&curwin->w_cursor, wcol);
if (wcol == MAXCOL || rc == FAIL)
curwin->w_valid &= ~VALID_VIRTCOL;
else if (*ml_get_cursor() != TAB)
{
// Virtcol is valid when not on a TAB
curwin->w_valid |= VALID_VIRTCOL;
curwin->w_virtcol = wcol;
}
return rc;
}
/*
* Return in "pos" the position of the cursor advanced to screen column "wcol".
* return OK if desired column is reached, FAIL if not
*/
int
getvpos(pos_T *pos, colnr_T wcol)
{
return coladvance2(pos, FALSE, virtual_active(), wcol);
}
static int
coladvance2(
pos_T *pos,
int addspaces, // change the text to achieve our goal?
int finetune, // change char offset for the exact column
colnr_T wcol_arg) // column to move to (can be negative)
{
colnr_T wcol = wcol_arg;
int idx;
char_u *ptr;
char_u *line;
colnr_T col = 0;
int csize = 0;
int one_more;
#ifdef FEAT_LINEBREAK
int head = 0;
#endif
one_more = (State & INSERT)
|| restart_edit != NUL
|| (VIsual_active && *p_sel != 'o')
|| ((ve_flags & VE_ONEMORE) && wcol < MAXCOL);
line = ml_get_buf(curbuf, pos->lnum, FALSE);
if (wcol >= MAXCOL)
{
idx = (int)STRLEN(line) - 1 + one_more;
col = wcol;
if ((addspaces || finetune) && !VIsual_active)
{
curwin->w_curswant = linetabsize(line) + one_more;
if (curwin->w_curswant > 0)
--curwin->w_curswant;
}
}
else
{
int width = curwin->w_width - win_col_off(curwin);
if (finetune
&& curwin->w_p_wrap
&& curwin->w_width != 0
&& wcol >= (colnr_T)width)
{
csize = linetabsize(line);
if (csize > 0)
csize--;
if (wcol / width > (colnr_T)csize / width
&& ((State & INSERT) == 0 || (int)wcol > csize + 1))
{
// In case of line wrapping don't move the cursor beyond the
// right screen edge. In Insert mode allow going just beyond
// the last character (like what happens when typing and
// reaching the right window edge).
wcol = (csize / width + 1) * width - 1;
}
}
ptr = line;
while (col <= wcol && *ptr != NUL)
{
// Count a tab for what it's worth (if list mode not on)
#ifdef FEAT_LINEBREAK
csize = win_lbr_chartabsize(curwin, line, ptr, col, &head);
MB_PTR_ADV(ptr);
#else
csize = lbr_chartabsize_adv(line, &ptr, col);
#endif
col += csize;
}
idx = (int)(ptr - line);
/*
* Handle all the special cases. The virtual_active() check
* is needed to ensure that a virtual position off the end of
* a line has the correct indexing. The one_more comparison
* replaces an explicit add of one_more later on.
*/
if (col > wcol || (!virtual_active() && one_more == 0))
{
idx -= 1;
# ifdef FEAT_LINEBREAK
// Don't count the chars from 'showbreak'.
csize -= head;
# endif
col -= csize;
}
if (virtual_active()
&& addspaces
&& wcol >= 0
&& ((col != wcol && col != wcol + 1) || csize > 1))
{
// 'virtualedit' is set: The difference between wcol and col is
// filled with spaces.
if (line[idx] == NUL)
{
// Append spaces
int correct = wcol - col;
char_u *newline = alloc(idx + correct + 1);
int t;
if (newline == NULL)
return FAIL;
for (t = 0; t < idx; ++t)
newline[t] = line[t];
for (t = 0; t < correct; ++t)
newline[t + idx] = ' ';
newline[idx + correct] = NUL;
ml_replace(pos->lnum, newline, FALSE);
changed_bytes(pos->lnum, (colnr_T)idx);
idx += correct;
col = wcol;
}
else
{
// Break a tab
int linelen = (int)STRLEN(line);
int correct = wcol - col - csize + 1; // negative!!
char_u *newline;
int t, s = 0;
int v;
if (-correct > csize)
return FAIL;
newline = alloc(linelen + csize);
if (newline == NULL)
return FAIL;
for (t = 0; t < linelen; t++)
{
if (t != idx)
newline[s++] = line[t];
else
for (v = 0; v < csize; v++)
newline[s++] = ' ';
}
newline[linelen + csize - 1] = NUL;
ml_replace(pos->lnum, newline, FALSE);
changed_bytes(pos->lnum, idx);
idx += (csize - 1 + correct);
col += correct;
}
}
}
if (idx < 0)
pos->col = 0;
else
pos->col = idx;
pos->coladd = 0;
if (finetune)
{
if (wcol == MAXCOL)
{
// The width of the last character is used to set coladd.
if (!one_more)
{
colnr_T scol, ecol;
getvcol(curwin, pos, &scol, NULL, &ecol);
pos->coladd = ecol - scol;
}
}
else
{
int b = (int)wcol - (int)col;
// The difference between wcol and col is used to set coladd.
if (b > 0 && b < (MAXCOL - 2 * curwin->w_width))
pos->coladd = b;
col += b;
}
}
// prevent from moving onto a trail byte
if (has_mbyte)
mb_adjustpos(curbuf, pos);
if (wcol < 0 || col < wcol)
return FAIL;
return OK;
}
/*
* Increment the cursor position. See inc() for return values.
*/
int
inc_cursor(void)
{
return inc(&curwin->w_cursor);
}
/*
* Increment the line pointer "lp" crossing line boundaries as necessary.
* Return 1 when going to the next line.
* Return 2 when moving forward onto a NUL at the end of the line).
* Return -1 when at the end of file.
* Return 0 otherwise.
*/
int
inc(pos_T *lp)
{
char_u *p;
// when searching position may be set to end of a line
if (lp->col != MAXCOL)
{
p = ml_get_pos(lp);
if (*p != NUL) // still within line, move to next char (may be NUL)
{
if (has_mbyte)
{
int l = (*mb_ptr2len)(p);
lp->col += l;
return ((p[l] != NUL) ? 0 : 2);
}
lp->col++;
lp->coladd = 0;
return ((p[1] != NUL) ? 0 : 2);
}
}
if (lp->lnum != curbuf->b_ml.ml_line_count) // there is a next line
{
lp->col = 0;
lp->lnum++;
lp->coladd = 0;
return 1;
}
return -1;
}
/*
* incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
*/
int
incl(pos_T *lp)
{
int r;
if ((r = inc(lp)) >= 1 && lp->col)
r = inc(lp);
return r;
}
/*
* dec(p)
*
* Decrement the line pointer 'p' crossing line boundaries as necessary.
* Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
*/
int
dec_cursor(void)
{
return dec(&curwin->w_cursor);
}
int
dec(pos_T *lp)
{
char_u *p;
lp->coladd = 0;
if (lp->col == MAXCOL)
{
// past end of line
p = ml_get(lp->lnum);
lp->col = (colnr_T)STRLEN(p);
if (has_mbyte)
lp->col -= (*mb_head_off)(p, p + lp->col);
return 0;
}
if (lp->col > 0)
{
// still within line
lp->col--;
if (has_mbyte)
{
p = ml_get(lp->lnum);
lp->col -= (*mb_head_off)(p, p + lp->col);
}
return 0;
}
if (lp->lnum > 1)
{
// there is a prior line
lp->lnum--;
p = ml_get(lp->lnum);
lp->col = (colnr_T)STRLEN(p);
if (has_mbyte)
lp->col -= (*mb_head_off)(p, p + lp->col);
return 1;
}
// at start of file
return -1;
}
/*
* decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
*/
int
decl(pos_T *lp)
{
int r;
if ((r = dec(lp)) == 1 && lp->col)
r = dec(lp);
return r;
}
/*
* Get the line number relative to the current cursor position, i.e. the
* difference between line number and cursor position. Only look for lines that
* can be visible, folded lines don't count.
*/
linenr_T
get_cursor_rel_lnum(
win_T *wp,
linenr_T lnum) // line number to get the result for
{
linenr_T cursor = wp->w_cursor.lnum;
linenr_T retval = 0;
#ifdef FEAT_FOLDING
if (hasAnyFolding(wp))
{
if (lnum > cursor)
{
while (lnum > cursor)
{
(void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
// if lnum and cursor are in the same fold,
// now lnum <= cursor
if (lnum > cursor)
retval++;
lnum--;
}
}
else if (lnum < cursor)
{
while (lnum < cursor)
{
(void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
// if lnum and cursor are in the same fold,
// now lnum >= cursor
if (lnum < cursor)
retval--;
lnum++;
}
}
// else if (lnum == cursor)
// retval = 0;
}
else
#endif
retval = lnum - cursor;
return retval;
}
/*
* Make sure "pos.lnum" and "pos.col" are valid in "buf".
* This allows for the col to be on the NUL byte.
*/
void
check_pos(buf_T *buf, pos_T *pos)
{
char_u *line;
colnr_T len;
if (pos->lnum > buf->b_ml.ml_line_count)
pos->lnum = buf->b_ml.ml_line_count;
if (pos->col > 0)
{
line = ml_get_buf(buf, pos->lnum, FALSE);
len = (colnr_T)STRLEN(line);
if (pos->col > len)
pos->col = len;
}
}
/*
* Make sure curwin->w_cursor.lnum is valid.
*/
void
check_cursor_lnum(void)
{
if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
{
#ifdef FEAT_FOLDING
// If there is a closed fold at the end of the file, put the cursor in
// its first line. Otherwise in the last line.
if (!hasFolding(curbuf->b_ml.ml_line_count,
&curwin->w_cursor.lnum, NULL))
#endif
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
}
if (curwin->w_cursor.lnum <= 0)
curwin->w_cursor.lnum = 1;
}
/*
* Make sure curwin->w_cursor.col is valid.
*/
void
check_cursor_col(void)
{
check_cursor_col_win(curwin);
}
/*
* Make sure win->w_cursor.col is valid.
*/
void
check_cursor_col_win(win_T *win)
{
colnr_T len;
colnr_T oldcol = win->w_cursor.col;
colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
if (len == 0)
win->w_cursor.col = 0;
else if (win->w_cursor.col >= len)
{
// Allow cursor past end-of-line when:
// - in Insert mode or restarting Insert mode
// - in Visual mode and 'selection' isn't "old"
// - 'virtualedit' is set
if ((State & INSERT) || restart_edit
|| (VIsual_active && *p_sel != 'o')
|| (ve_flags & VE_ONEMORE)
|| virtual_active())
win->w_cursor.col = len;
else
{
win->w_cursor.col = len - 1;
// Move the cursor to the head byte.
if (has_mbyte)
mb_adjustpos(win->w_buffer, &win->w_cursor);
}
}
else if (win->w_cursor.col < 0)
win->w_cursor.col = 0;
// If virtual editing is on, we can leave the cursor on the old position,
// only we must set it to virtual. But don't do it when at the end of the
// line.
if (oldcol == MAXCOL)
win->w_cursor.coladd = 0;
else if (ve_flags == VE_ALL)
{
if (oldcoladd > win->w_cursor.col)
{
win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
// Make sure that coladd is not more than the char width.
// Not for the last character, coladd is then used when the cursor
// is actually after the last character.
if (win->w_cursor.col + 1 < len && win->w_cursor.coladd > 0)
{
int cs, ce;
getvcol(win, &win->w_cursor, &cs, NULL, &ce);
if (win->w_cursor.coladd > ce - cs)
win->w_cursor.coladd = ce - cs;
}
}
else
// avoid weird number when there is a miscalculation or overflow
win->w_cursor.coladd = 0;
}
}
/*
* make sure curwin->w_cursor in on a valid character
*/
void
check_cursor(void)
{
check_cursor_lnum();
check_cursor_col();
}
#if defined(FEAT_TEXTOBJ) || defined(PROTO)
/*
* Make sure curwin->w_cursor is not on the NUL at the end of the line.
* Allow it when in Visual mode and 'selection' is not "old".
*/
void
adjust_cursor_col(void)
{
if (curwin->w_cursor.col > 0
&& (!VIsual_active || *p_sel == 'o')
&& gchar_cursor() == NUL)
--curwin->w_cursor.col;
}
#endif
/*
* When curwin->w_leftcol has changed, adjust the cursor position.
* Return TRUE if the cursor was moved.
*/
int
leftcol_changed(void)
{
long lastcol;
colnr_T s, e;
int retval = FALSE;
long siso = get_sidescrolloff_value();
changed_cline_bef_curs();
lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1;
validate_virtcol();
/*
* If the cursor is right or left of the screen, move it to last or first
* character.
*/
if (curwin->w_virtcol > (colnr_T)(lastcol - siso))
{
retval = TRUE;
coladvance((colnr_T)(lastcol - siso));
}
else if (curwin->w_virtcol < curwin->w_leftcol + siso)
{
retval = TRUE;
(void)coladvance((colnr_T)(curwin->w_leftcol + siso));
}
/*
* If the start of the character under the cursor is not on the screen,
* advance the cursor one more char. If this fails (last char of the
* line) adjust the scrolling.
*/
getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
if (e > (colnr_T)lastcol)
{
retval = TRUE;
coladvance(s - 1);
}
else if (s < curwin->w_leftcol)
{
retval = TRUE;
if (coladvance(e + 1) == FAIL) // there isn't another character
{
curwin->w_leftcol = s; // adjust w_leftcol instead
changed_cline_bef_curs();
}
}
if (retval)
curwin->w_set_curswant = TRUE;
redraw_later(NOT_VALID);
return retval;
}
/**********************************************************************
* Various routines dealing with allocation and deallocation of memory.
*/
#if defined(MEM_PROFILE) || defined(PROTO)
# define MEM_SIZES 8200
static long_u mem_allocs[MEM_SIZES];
static long_u mem_frees[MEM_SIZES];
static long_u mem_allocated;
static long_u mem_freed;
static long_u mem_peak;
static long_u num_alloc;
static long_u num_freed;
static void
mem_pre_alloc_s(size_t *sizep)
{
*sizep += sizeof(size_t);
}
static void
mem_pre_alloc_l(size_t *sizep)
{
*sizep += sizeof(size_t);
}
static void
mem_post_alloc(
void **pp,
size_t size)
{
if (*pp == NULL)
return;
size -= sizeof(size_t);
*(long_u *)*pp = size;
if (size <= MEM_SIZES-1)
mem_allocs[size-1]++;
else
mem_allocs[MEM_SIZES-1]++;
mem_allocated += size;
if (mem_allocated - mem_freed > mem_peak)
mem_peak = mem_allocated - mem_freed;
num_alloc++;
*pp = (void *)((char *)*pp + sizeof(size_t));
}
static void
mem_pre_free(void **pp)
{
long_u size;
*pp = (void *)((char *)*pp - sizeof(size_t));
size = *(size_t *)*pp;
if (size <= MEM_SIZES-1)
mem_frees[size-1]++;
else
mem_frees[MEM_SIZES-1]++;
mem_freed += size;
num_freed++;
}
/*
* called on exit via atexit()
*/
void
vim_mem_profile_dump(void)
{
int i, j;
printf("\r\n");
j = 0;
for (i = 0; i < MEM_SIZES - 1; i++)
{
if (mem_allocs[i] || mem_frees[i])
{
if (mem_frees[i] > mem_allocs[i])
printf("\r\n%s", _("ERROR: "));
printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
j++;
if (j > 3)
{
j = 0;
printf("\r\n");
}
}
}
i = MEM_SIZES - 1;
if (mem_allocs[i])
{
printf("\r\n");
if (mem_frees[i] > mem_allocs[i])
puts(_("ERROR: "));
printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
}
printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
num_alloc, num_freed);
}
#endif // MEM_PROFILE
#ifdef FEAT_EVAL
int
alloc_does_fail(size_t size)
{
if (alloc_fail_countdown == 0)
{
if (--alloc_fail_repeat <= 0)
alloc_fail_id = 0;
do_outofmem_msg(size);
return TRUE;
}
--alloc_fail_countdown;
return FALSE;
}
#endif
/*
* Some memory is reserved for error messages and for being able to
* call mf_release_all(), which needs some memory for mf_trans_add().
*/
#define KEEP_ROOM (2 * 8192L)
#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
/*
* The normal way to allocate memory. This handles an out-of-memory situation
* as well as possible, still returns NULL when we're completely out.
*/
void *
alloc(size_t size)
{
return lalloc(size, TRUE);
}
/*
* alloc() with an ID for alloc_fail().
*/
void *
alloc_id(size_t size, alloc_id_T id UNUSED)
{
#ifdef FEAT_EVAL
if (alloc_fail_id == id && alloc_does_fail(size))
return NULL;
#endif
return lalloc(size, TRUE);
}
/*
* Allocate memory and set all bytes to zero.
*/
void *
alloc_clear(size_t size)
{
void *p;
p = lalloc(size, TRUE);
if (p != NULL)
(void)vim_memset(p, 0, size);
return p;
}
/*
* Same as alloc_clear() but with allocation id for testing
*/
void *
alloc_clear_id(size_t size, alloc_id_T id UNUSED)
{
#ifdef FEAT_EVAL
if (alloc_fail_id == id && alloc_does_fail(size))
return NULL;
#endif
return alloc_clear(size);
}
/*
* Allocate memory like lalloc() and set all bytes to zero.
*/
void *
lalloc_clear(size_t size, int message)
{
void *p;
p = lalloc(size, message);
if (p != NULL)
(void)vim_memset(p, 0, size);
return p;
}
/*
* Low level memory allocation function.
* This is used often, KEEP IT FAST!
*/
void *
lalloc(size_t size, int message)
{
void *p; // pointer to new storage space
static int releasing = FALSE; // don't do mf_release_all() recursive
int try_again;
#if defined(HAVE_AVAIL_MEM)
static size_t allocated = 0; // allocated since last avail check
#endif
// Safety check for allocating zero bytes
if (size == 0)
{
// Don't hide this message
emsg_silent = 0;
iemsg(_("E341: Internal error: lalloc(0, )"));
return NULL;
}
#ifdef MEM_PROFILE
mem_pre_alloc_l(&size);
#endif
/*
* Loop when out of memory: Try to release some memfile blocks and
* if some blocks are released call malloc again.
*/
for (;;)
{
/*
* Handle three kind of systems:
* 1. No check for available memory: Just return.
* 2. Slow check for available memory: call mch_avail_mem() after
* allocating KEEP_ROOM amount of memory.
* 3. Strict check for available memory: call mch_avail_mem()
*/
if ((p = malloc(size)) != NULL)
{
#ifndef HAVE_AVAIL_MEM
// 1. No check for available memory: Just return.
goto theend;
#else
// 2. Slow check for available memory: call mch_avail_mem() after
// allocating (KEEP_ROOM / 2) amount of memory.
allocated += size;
if (allocated < KEEP_ROOM / 2)
goto theend;
allocated = 0;
// 3. check for available memory: call mch_avail_mem()
if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
{
free(p); // System is low... no go!
p = NULL;
}
else
goto theend;
#endif
}
/*
* Remember that mf_release_all() is being called to avoid an endless
* loop, because mf_release_all() may call alloc() recursively.
*/
if (releasing)
break;
releasing = TRUE;
clear_sb_text(TRUE); // free any scrollback text
try_again = mf_release_all(); // release as many blocks as possible
releasing = FALSE;
if (!try_again)
break;
}
if (message && p == NULL)
do_outofmem_msg(size);
theend:
#ifdef MEM_PROFILE
mem_post_alloc(&p, size);
#endif
return p;
}
/*
* lalloc() with an ID for alloc_fail().
*/
#if defined(FEAT_SIGNS) || defined(PROTO)
void *
lalloc_id(size_t size, int message, alloc_id_T id UNUSED)
{
#ifdef FEAT_EVAL
if (alloc_fail_id == id && alloc_does_fail(size))
return NULL;
#endif
return (lalloc(size, message));
}
#endif
#if defined(MEM_PROFILE) || defined(PROTO)
/*
* realloc() with memory profiling.
*/
void *
mem_realloc(void *ptr, size_t size)
{
void *p;
mem_pre_free(&ptr);
mem_pre_alloc_s(&size);
p = realloc(ptr, size);
mem_post_alloc(&p, size);
return p;
}
#endif
/*
* Avoid repeating the error message many times (they take 1 second each).
* Did_outofmem_msg is reset when a character is read.
*/
void
do_outofmem_msg(size_t size)
{
if (!did_outofmem_msg)
{
// Don't hide this message
emsg_silent = 0;
// Must come first to avoid coming back here when printing the error
// message fails, e.g. when setting v:errmsg.
did_outofmem_msg = TRUE;
semsg(_("E342: Out of memory! (allocating %lu bytes)"), (long_u)size);
if (starting == NO_SCREEN)
// Not even finished with initializations and already out of
// memory? Then nothing is going to work, exit.
mch_exit(123);
}
}
#if defined(EXITFREE) || defined(PROTO)
/*
* Free everything that we allocated.
* Can be used to detect memory leaks, e.g., with ccmalloc.
* NOTE: This is tricky! Things are freed that functions depend on. Don't be
* surprised if Vim crashes...
* Some things can't be freed, esp. things local to a library function.
*/
void
free_all_mem(void)
{
buf_T *buf, *nextbuf;
// When we cause a crash here it is caught and Vim tries to exit cleanly.
// Don't try freeing everything again.
if (entered_free_all_mem)
return;
entered_free_all_mem = TRUE;
// Don't want to trigger autocommands from here on.
block_autocmds();
// Close all tabs and windows. Reset 'equalalways' to avoid redraws.
p_ea = FALSE;
if (first_tabpage != NULL && first_tabpage->tp_next != NULL)
do_cmdline_cmd((char_u *)"tabonly!");
if (!ONE_WINDOW)
do_cmdline_cmd((char_u *)"only!");
# if defined(FEAT_SPELL)
// Free all spell info.
spell_free_all();
# endif
# if defined(FEAT_BEVAL_TERM)
ui_remove_balloon();
# endif
# ifdef FEAT_PROP_POPUP
if (curwin != NULL)
close_all_popups(TRUE);
# endif
// Clear user commands (before deleting buffers).
ex_comclear(NULL);
// When exiting from mainerr_arg_missing curbuf has not been initialized,
// and not much else.
if (curbuf != NULL)
{
# ifdef FEAT_MENU
// Clear menus.
do_cmdline_cmd((char_u *)"aunmenu *");
# ifdef FEAT_MULTI_LANG
do_cmdline_cmd((char_u *)"menutranslate clear");
# endif
# endif
// Clear mappings, abbreviations, breakpoints.
do_cmdline_cmd((char_u *)"lmapclear");
do_cmdline_cmd((char_u *)"xmapclear");
do_cmdline_cmd((char_u *)"mapclear");
do_cmdline_cmd((char_u *)"mapclear!");
do_cmdline_cmd((char_u *)"abclear");
# if defined(FEAT_EVAL)
do_cmdline_cmd((char_u *)"breakdel *");
# endif
# if defined(FEAT_PROFILE)
do_cmdline_cmd((char_u *)"profdel *");
# endif
# if defined(FEAT_KEYMAP)
do_cmdline_cmd((char_u *)"set keymap=");
# endif
}
# ifdef FEAT_TITLE
free_titles();
# endif
# if defined(FEAT_SEARCHPATH)
free_findfile();
# endif
// Obviously named calls.
free_all_autocmds();
clear_termcodes();
free_all_marks();
alist_clear(&global_alist);
free_homedir();
free_users();
free_search_patterns();
free_old_sub();
free_last_insert();
free_insexpand_stuff();
free_prev_shellcmd();
free_regexp_stuff();
free_tag_stuff();
free_cd_dir();
# ifdef FEAT_SIGNS
free_signs();
# endif
# ifdef FEAT_EVAL
set_expr_line(NULL);
# endif
# ifdef FEAT_DIFF
if (curtab != NULL)
diff_clear(curtab);
# endif
clear_sb_text(TRUE); // free any scrollback text
// Free some global vars.
vim_free(username);
# ifdef FEAT_CLIPBOARD
vim_regfree(clip_exclude_prog);
# endif
vim_free(last_cmdline);
vim_free(new_last_cmdline);
set_keep_msg(NULL, 0);
// Clear cmdline history.
p_hi = 0;
init_history();
# ifdef FEAT_PROP_POPUP
clear_global_prop_types();
# endif
# ifdef FEAT_QUICKFIX
{
win_T *win;
tabpage_T *tab;
qf_free_all(NULL);
// Free all location lists
FOR_ALL_TAB_WINDOWS(tab, win)
qf_free_all(win);
}
# endif
// Close all script inputs.
close_all_scripts();
if (curwin != NULL)
// Destroy all windows. Must come before freeing buffers.
win_free_all();
// Free all option values. Must come after closing windows.
free_all_options();
// Free all buffers. Reset 'autochdir' to avoid accessing things that
// were freed already.
# ifdef FEAT_AUTOCHDIR
p_acd = FALSE;
# endif
for (buf = firstbuf; buf != NULL; )
{
bufref_T bufref;
set_bufref(&bufref, buf);
nextbuf = buf->b_next;
close_buffer(NULL, buf, DOBUF_WIPE, FALSE, FALSE);
if (bufref_valid(&bufref))
buf = nextbuf; // didn't work, try next one
else
buf = firstbuf;
}
# ifdef FEAT_ARABIC
free_arshape_buf();
# endif
// Clear registers.
clear_registers();
ResetRedobuff();
ResetRedobuff();
# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
vim_free(serverDelayedStartName);
# endif
// highlight info
free_highlight();
reset_last_sourcing();
if (first_tabpage != NULL)
{
free_tabpage(first_tabpage);
first_tabpage = NULL;
}
# ifdef UNIX
// Machine-specific free.
mch_free_mem();
# endif
// message history
for (;;)
if (delete_first_msg() == FAIL)
break;
# ifdef FEAT_JOB_CHANNEL
channel_free_all();
# endif
# ifdef FEAT_TIMERS
timer_free_all();
# endif
# ifdef FEAT_EVAL
// must be after channel_free_all() with unrefs partials
eval_clear();
# endif
# ifdef FEAT_JOB_CHANNEL
// must be after eval_clear() with unrefs jobs
job_free_all();
# endif
free_termoptions();
// screenlines (can't display anything now!)
free_screenlines();
# if defined(FEAT_SOUND)
sound_free();
# endif
# if defined(USE_XSMP)
xsmp_close();
# endif
# ifdef FEAT_GUI_GTK
gui_mch_free_all();
# endif
clear_hl_tables();
vim_free(IObuff);
vim_free(NameBuff);
# ifdef FEAT_QUICKFIX
check_quickfix_busy();
# endif
}
#endif
/*
* Copy "string" into newly allocated memory.
*/
char_u *
vim_strsave(char_u *string)
{
char_u *p;
size_t len;
len = STRLEN(string) + 1;
p = alloc(len);
if (p != NULL)
mch_memmove(p, string, len);
return p;
}
/*
* Copy up to "len" bytes of "string" into newly allocated memory and
* terminate with a NUL.
* The allocated memory always has size "len + 1", also when "string" is
* shorter.
*/
char_u *
vim_strnsave(char_u *string, int len)
{
char_u *p;
p = alloc(len + 1);
if (p != NULL)
{
STRNCPY(p, string, len);
p[len] = NUL;
}
return p;
}
/*
* Copy "p[len]" into allocated memory, ignoring NUL characters.
* Returns NULL when out of memory.
*/
char_u *
vim_memsave(char_u *p, size_t len)
{
char_u *ret = alloc(len);
if (ret != NULL)
mch_memmove(ret, p, len);
return ret;
}
/*
* Same as vim_strsave(), but any characters found in esc_chars are preceded
* by a backslash.
*/
char_u *
vim_strsave_escaped(char_u *string, char_u *esc_chars)
{
return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE);
}
/*
* Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape
* characters where rem_backslash() would remove the backslash.
* Escape the characters with "cc".
*/
char_u *
vim_strsave_escaped_ext(
char_u *string,
char_u *esc_chars,
int cc,
int bsl)
{
char_u *p;
char_u *p2;
char_u *escaped_string;
unsigned length;
int l;
/*
* First count the number of backslashes required.
* Then allocate the memory and insert them.
*/
length = 1; // count the trailing NUL
for (p = string; *p; p++)
{
if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
{
length += l; // count a multibyte char
p += l - 1;
continue;
}
if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
++length; // count a backslash
++length; // count an ordinary char
}
escaped_string = alloc(length);
if (escaped_string != NULL)
{
p2 = escaped_string;
for (p = string; *p; p++)
{
if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
{
mch_memmove(p2, p, (size_t)l);
p2 += l;
p += l - 1; // skip multibyte char
continue;
}
if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
*p2++ = cc;
*p2++ = *p;
}
*p2 = NUL;
}
return escaped_string;
}
/*
* Return TRUE when 'shell' has "csh" in the tail.
*/
int
csh_like_shell(void)
{
return (strstr((char *)gettail(p_sh), "csh") != NULL);
}
/*
* Escape "string" for use as a shell argument with system().
* This uses single quotes, except when we know we need to use double quotes
* (MS-DOS and MS-Windows without 'shellslash' set).
* Escape a newline, depending on the 'shell' option.
* When "do_special" is TRUE also replace "!", "%", "#" and things starting
* with "<" like "<cfile>".
* When "do_newline" is FALSE do not escape newline unless it is csh shell.
* Returns the result in allocated memory, NULL if we have run out.
*/
char_u *
vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
{
unsigned length;
char_u *p;
char_u *d;
char_u *escaped_string;
int l;
int csh_like;
// Only csh and similar shells expand '!' within single quotes. For sh and
// the like we must not put a backslash before it, it will be taken
// literally. If do_special is set the '!' will be escaped twice.
// Csh also needs to have "\n" escaped twice when do_special is set.
csh_like = csh_like_shell();
// First count the number of extra bytes required.
length = (unsigned)STRLEN(string) + 3; // two quotes and a trailing NUL
for (p = string; *p != NUL; MB_PTR_ADV(p))
{
# ifdef MSWIN
if (!p_ssl)
{
if (*p == '"')
++length; // " -> ""
}
else
# endif
if (*p == '\'')
length += 3; // ' => '\''
if ((*p == '\n' && (csh_like || do_newline))
|| (*p == '!' && (csh_like || do_special)))
{
++length; // insert backslash
if (csh_like && do_special)
++length; // insert backslash
}
if (do_special && find_cmdline_var(p, &l) >= 0)
{
++length; // insert backslash
p += l - 1;
}
}
// Allocate memory for the result and fill it.
escaped_string = alloc(length);
if (escaped_string != NULL)
{
d = escaped_string;
// add opening quote
# ifdef MSWIN
if (!p_ssl)
*d++ = '"';
else
# endif
*d++ = '\'';
for (p = string; *p != NUL; )
{
# ifdef MSWIN
if (!p_ssl)
{
if (*p == '"')
{
*d++ = '"';
*d++ = '"';
++p;
continue;
}
}
else
# endif
if (*p == '\'')
{
*d++ = '\'';
*d++ = '\\';
*d++ = '\'';
*d++ = '\'';
++p;
continue;
}
if ((*p == '\n' && (csh_like || do_newline))
|| (*p == '!' && (csh_like || do_special)))
{
*d++ = '\\';
if (csh_like && do_special)
*d++ = '\\';
*d++ = *p++;
continue;
}
if (do_special && find_cmdline_var(p, &l) >= 0)
{
*d++ = '\\'; // insert backslash
while (--l >= 0) // copy the var
*d++ = *p++;
continue;
}
MB_COPY_CHAR(p, d);
}
// add terminating quote and finish with a NUL
# ifdef MSWIN
if (!p_ssl)
*d++ = '"';
else
# endif
*d++ = '\'';
*d = NUL;
}
return escaped_string;
}
/*
* Like vim_strsave(), but make all characters uppercase.
* This uses ASCII lower-to-upper case translation, language independent.
*/
char_u *
vim_strsave_up(char_u *string)
{
char_u *p1;
p1 = vim_strsave(string);
vim_strup(p1);
return p1;
}
/*
* Like vim_strnsave(), but make all characters uppercase.
* This uses ASCII lower-to-upper case translation, language independent.
*/
char_u *
vim_strnsave_up(char_u *string, int len)
{
char_u *p1;
p1 = vim_strnsave(string, len);
vim_strup(p1);
return p1;
}
/*
* ASCII lower-to-upper case translation, language independent.
*/
void
vim_strup(
char_u *p)
{
char_u *p2;
int c;
if (p != NULL)
{
p2 = p;
while ((c = *p2) != NUL)
#ifdef EBCDIC
*p2++ = isalpha(c) ? toupper(c) : c;
#else
*p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20);
#endif
}
}
#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
/*
* Make string "s" all upper-case and return it in allocated memory.
* Handles multi-byte characters as well as possible.
* Returns NULL when out of memory.
*/
char_u *
strup_save(char_u *orig)
{
char_u *p;
char_u *res;
res = p = vim_strsave(orig);
if (res != NULL)
while (*p != NUL)
{
int l;
if (enc_utf8)
{
int c, uc;
int newl;
char_u *s;
c = utf_ptr2char(p);
l = utf_ptr2len(p);
if (c == 0)
{
// overlong sequence, use only the first byte
c = *p;
l = 1;
}
uc = utf_toupper(c);
// Reallocate string when byte count changes. This is rare,
// thus it's OK to do another malloc()/free().
newl = utf_char2len(uc);
if (newl != l)
{
s = alloc(STRLEN(res) + 1 + newl - l);
if (s == NULL)
{
vim_free(res);
return NULL;
}
mch_memmove(s, res, p - res);
STRCPY(s + (p - res) + newl, p + l);
p = s + (p - res);
vim_free(res);
res = s;
}
utf_char2bytes(uc, p);
p += newl;
}
else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
p += l; // skip multi-byte character
else
{
*p = TOUPPER_LOC(*p); // note that toupper() can be a macro
p++;
}
}
return res;
}
/*
* Make string "s" all lower-case and return it in allocated memory.
* Handles multi-byte characters as well as possible.
* Returns NULL when out of memory.
*/
char_u *
strlow_save(char_u *orig)
{
char_u *p;
char_u *res;
res = p = vim_strsave(orig);
if (res != NULL)
while (*p != NUL)
{
int l;
if (enc_utf8)
{
int c, lc;
int newl;
char_u *s;
c = utf_ptr2char(p);
l = utf_ptr2len(p);
if (c == 0)
{
// overlong sequence, use only the first byte
c = *p;
l = 1;
}
lc = utf_tolower(c);
// Reallocate string when byte count changes. This is rare,
// thus it's OK to do another malloc()/free().
newl = utf_char2len(lc);
if (newl != l)
{
s = alloc(STRLEN(res) + 1 + newl - l);
if (s == NULL)
{
vim_free(res);
return NULL;
}
mch_memmove(s, res, p - res);
STRCPY(s + (p - res) + newl, p + l);
p = s + (p - res);
vim_free(res);
res = s;
}
utf_char2bytes(lc, p);
p += newl;
}
else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
p += l; // skip multi-byte character
else
{
*p = TOLOWER_LOC(*p); // note that tolower() can be a macro
p++;
}
}
return res;
}
#endif
/*
* delete spaces at the end of a string
*/
void
del_trailing_spaces(char_u *ptr)
{
char_u *q;
q = ptr + STRLEN(ptr);
while (--q > ptr && VIM_ISWHITE(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V)
*q = NUL;
}
/*
* Like strncpy(), but always terminate the result with one NUL.
* "to" must be "len + 1" long!
*/
void
vim_strncpy(char_u *to, char_u *from, size_t len)
{
STRNCPY(to, from, len);
to[len] = NUL;
}
/*
* Like strcat(), but make sure the result fits in "tosize" bytes and is
* always NUL terminated. "from" and "to" may overlap.
*/
void
vim_strcat(char_u *to, char_u *from, size_t tosize)
{
size_t tolen = STRLEN(to);
size_t fromlen = STRLEN(from);
if (tolen + fromlen + 1 > tosize)
{
mch_memmove(to + tolen, from, tosize - tolen - 1);
to[tosize - 1] = NUL;
}
else
mch_memmove(to + tolen, from, fromlen + 1);
}
/*
* Isolate one part of a string option where parts are separated with
* "sep_chars".
* The part is copied into "buf[maxlen]".
* "*option" is advanced to the next part.
* The length is returned.
*/
int
copy_option_part(
char_u **option,
char_u *buf,
int maxlen,
char *sep_chars)
{
int len = 0;
char_u *p = *option;
// skip '.' at start of option part, for 'suffixes'
if (*p == '.')
buf[len++] = *p++;
while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
{
/*
* Skip backslash before a separator character and space.
*/
if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
++p;
if (len < maxlen - 1)
buf[len++] = *p;
++p;
}
buf[len] = NUL;
if (*p != NUL && *p != ',') // skip non-standard separator
++p;
p = skip_to_option_part(p); // p points to next file name
*option = p;
return len;
}
/*
* Replacement for free() that ignores NULL pointers.
* Also skip free() when exiting for sure, this helps when we caught a deadly
* signal that was caused by a crash in free().
* If you want to set NULL after calling this function, you should use
* VIM_CLEAR() instead.
*/
void
vim_free(void *x)
{
if (x != NULL && !really_exiting)
{
#ifdef MEM_PROFILE
mem_pre_free(&x);
#endif
free(x);
}
}
#ifndef HAVE_MEMSET
void *
vim_memset(void *ptr, int c, size_t size)
{
char *p = ptr;
while (size-- > 0)
*p++ = c;
return ptr;
}
#endif
#if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO)
/*
* Compare two strings, ignoring case, using current locale.
* Doesn't work for multi-byte characters.
* return 0 for match, < 0 for smaller, > 0 for bigger
*/
int
vim_stricmp(char *s1, char *s2)
{
int i;
for (;;)
{
i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
if (i != 0)
return i; // this character different
if (*s1 == NUL)
break; // strings match until NUL
++s1;
++s2;
}
return 0; // strings match
}
#endif
#if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO)
/*
* Compare two strings, for length "len", ignoring case, using current locale.
* Doesn't work for multi-byte characters.
* return 0 for match, < 0 for smaller, > 0 for bigger
*/
int
vim_strnicmp(char *s1, char *s2, size_t len)
{
int i;
while (len > 0)
{
i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
if (i != 0)
return i; // this character different
if (*s1 == NUL)
break; // strings match until NUL
++s1;
++s2;
--len;
}
return 0; // strings match
}
#endif
/*
* Version of strchr() and strrchr() that handle unsigned char strings
* with characters from 128 to 255 correctly. It also doesn't return a
* pointer to the NUL at the end of the string.
*/
char_u *
vim_strchr(char_u *string, int c)
{
char_u *p;
int b;
p = string;
if (enc_utf8 && c >= 0x80)
{
while (*p != NUL)
{
int l = utfc_ptr2len(p);
// Avoid matching an illegal byte here.
if (utf_ptr2char(p) == c && l > 1)
return p;
p += l;
}
return NULL;
}
if (enc_dbcs != 0 && c > 255)
{
int n2 = c & 0xff;
c = ((unsigned)c >> 8) & 0xff;
while ((b = *p) != NUL)
{
if (b == c && p[1] == n2)
return p;
p += (*mb_ptr2len)(p);
}
return NULL;
}
if (has_mbyte)
{
while ((b = *p) != NUL)
{
if (b == c)
return p;
p += (*mb_ptr2len)(p);
}
return NULL;
}
while ((b = *p) != NUL)
{
if (b == c)
return p;
++p;
}
return NULL;
}
/*
* Version of strchr() that only works for bytes and handles unsigned char
* strings with characters above 128 correctly. It also doesn't return a
* pointer to the NUL at the end of the string.
*/
char_u *
vim_strbyte(char_u *string, int c)
{
char_u *p = string;
while (*p != NUL)
{
if (*p == c)
return p;
++p;
}
return NULL;
}
/*
* Search for last occurrence of "c" in "string".
* Return NULL if not found.
* Does not handle multi-byte char for "c"!
*/
char_u *
vim_strrchr(char_u *string, int c)
{
char_u *retval = NULL;
char_u *p = string;
while (*p)
{
if (*p == c)
retval = p;
MB_PTR_ADV(p);
}
return retval;
}
/*
* Vim's version of strpbrk(), in case it's missing.
* Don't generate a prototype for this, causes problems when it's not used.
*/
#ifndef PROTO
# ifndef HAVE_STRPBRK
# ifdef vim_strpbrk
# undef vim_strpbrk
# endif
char_u *
vim_strpbrk(char_u *s, char_u *charset)
{
while (*s)
{
if (vim_strchr(charset, *s) != NULL)
return s;
MB_PTR_ADV(s);
}
return NULL;
}
# endif
#endif
/*
* Vim has its own isspace() function, because on some machines isspace()
* can't handle characters above 128.
*/
int
vim_isspace(int x)
{
return ((x >= 9 && x <= 13) || x == ' ');
}
/************************************************************************
* Functions for handling growing arrays.
*/
/*
* Clear an allocated growing array.
*/
void
ga_clear(garray_T *gap)
{
vim_free(gap->ga_data);
ga_init(gap);
}
/*
* Clear a growing array that contains a list of strings.
*/
void
ga_clear_strings(garray_T *gap)
{
int i;
for (i = 0; i < gap->ga_len; ++i)
vim_free(((char_u **)(gap->ga_data))[i]);
ga_clear(gap);
}
/*
* Initialize a growing array. Don't forget to set ga_itemsize and
* ga_growsize! Or use ga_init2().
*/
void
ga_init(garray_T *gap)
{
gap->ga_data = NULL;
gap->ga_maxlen = 0;
gap->ga_len = 0;
}
void
ga_init2(garray_T *gap, int itemsize, int growsize)
{
ga_init(gap);
gap->ga_itemsize = itemsize;
gap->ga_growsize = growsize;
}
/*
* Make room in growing array "gap" for at least "n" items.
* Return FAIL for failure, OK otherwise.
*/
int
ga_grow(garray_T *gap, int n)
{
if (gap->ga_maxlen - gap->ga_len < n)
return ga_grow_inner(gap, n);
return OK;
}
int
ga_grow_inner(garray_T *gap, int n)
{
size_t old_len;
size_t new_len;
char_u *pp;
if (n < gap->ga_growsize)
n = gap->ga_growsize;
// A linear growth is very inefficient when the array grows big. This
// is a compromise between allocating memory that won't be used and too
// many copy operations. A factor of 1.5 seems reasonable.
if (n < gap->ga_len / 2)
n = gap->ga_len / 2;
new_len = gap->ga_itemsize * (gap->ga_len + n);
pp = vim_realloc(gap->ga_data, new_len);
if (pp == NULL)
return FAIL;
old_len = gap->ga_itemsize * gap->ga_maxlen;
vim_memset(pp + old_len, 0, new_len - old_len);
gap->ga_maxlen = gap->ga_len + n;
gap->ga_data = pp;
return OK;
}
#if defined(FEAT_EVAL) || defined(FEAT_SEARCHPATH) || defined(PROTO)
/*
* For a growing array that contains a list of strings: concatenate all the
* strings with a separating "sep".
* Returns NULL when out of memory.
*/
char_u *
ga_concat_strings(garray_T *gap, char *sep)
{
int i;
int len = 0;
int sep_len = (int)STRLEN(sep);
char_u *s;
char_u *p;
for (i = 0; i < gap->ga_len; ++i)
len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
s = alloc(len + 1);
if (s != NULL)
{
*s = NUL;
p = s;
for (i = 0; i < gap->ga_len; ++i)
{
if (p != s)
{
STRCPY(p, sep);
p += sep_len;
}
STRCPY(p, ((char_u **)(gap->ga_data))[i]);
p += STRLEN(p);
}
}
return s;
}
#endif
#if defined(FEAT_VIMINFO) || defined(FEAT_EVAL) || defined(PROTO)
/*
* Make a copy of string "p" and add it to "gap".
* When out of memory nothing changes.
*/
void
ga_add_string(garray_T *gap, char_u *p)
{
char_u *cp = vim_strsave(p);
if (cp != NULL)
{
if (ga_grow(gap, 1) == OK)
((char_u **)(gap->ga_data))[gap->ga_len++] = cp;
else
vim_free(cp);
}
}
#endif
/*
* Concatenate a string to a growarray which contains bytes.
* When "s" is NULL does not do anything.
* Note: Does NOT copy the NUL at the end!
*/
void
ga_concat(garray_T *gap, char_u *s)
{
int len;
if (s == NULL || *s == NUL)
return;
len = (int)STRLEN(s);
if (ga_grow(gap, len) == OK)
{
mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
gap->ga_len += len;
}
}
/*
* Append one byte to a growarray which contains bytes.
*/
void
ga_append(garray_T *gap, int c)
{
if (ga_grow(gap, 1) == OK)
{
*((char *)gap->ga_data + gap->ga_len) = c;
++gap->ga_len;
}
}
#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(MSWIN) \
|| defined(PROTO)
/*
* Append the text in "gap" below the cursor line and clear "gap".
*/
void
append_ga_line(garray_T *gap)
{
// Remove trailing CR.
if (gap->ga_len > 0
&& !curbuf->b_p_bin
&& ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
--gap->ga_len;
ga_append(gap, NUL);
ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
gap->ga_len = 0;
}
#endif
/************************************************************************
* functions that use lookup tables for various things, generally to do with
* special key codes.
*/
/*
* Some useful tables.
*/
static struct modmasktable
{
short mod_mask; // Bit-mask for particular key modifier
short mod_flag; // Bit(s) for particular key modifier
char_u name; // Single letter name of modifier
} mod_mask_table[] =
{
{MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
{MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
{MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
{MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
{MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
{MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
{MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
#ifdef MACOS_X
{MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
#endif
// 'A' must be the last one
{MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
{0, 0, NUL}
// NOTE: when adding an entry, update MAX_KEY_NAME_LEN!
};
/*
* Shifted key terminal codes and their unshifted equivalent.
* Don't add mouse codes here, they are handled separately!
*/
#define MOD_KEYS_ENTRY_SIZE 5
static char_u modifier_keys_table[] =
{
// mod mask with modifier without modifier
MOD_MASK_SHIFT, '&', '9', '@', '1', // begin
MOD_MASK_SHIFT, '&', '0', '@', '2', // cancel
MOD_MASK_SHIFT, '*', '1', '@', '4', // command
MOD_MASK_SHIFT, '*', '2', '@', '5', // copy
MOD_MASK_SHIFT, '*', '3', '@', '6', // create
MOD_MASK_SHIFT, '*', '4', 'k', 'D', // delete char
MOD_MASK_SHIFT, '*', '5', 'k', 'L', // delete line
MOD_MASK_SHIFT, '*', '7', '@', '7', // end
MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', // end
MOD_MASK_SHIFT, '*', '9', '@', '9', // exit
MOD_MASK_SHIFT, '*', '0', '@', '0', // find
MOD_MASK_SHIFT, '#', '1', '%', '1', // help
MOD_MASK_SHIFT, '#', '2', 'k', 'h', // home
MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', // home
MOD_MASK_SHIFT, '#', '3', 'k', 'I', // insert
MOD_MASK_SHIFT, '#', '4', 'k', 'l', // left arrow
MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', // left arrow
MOD_MASK_SHIFT, '%', 'a', '%', '3', // message
MOD_MASK_SHIFT, '%', 'b', '%', '4', // move
MOD_MASK_SHIFT, '%', 'c', '%', '5', // next
MOD_MASK_SHIFT, '%', 'd', '%', '7', // options
MOD_MASK_SHIFT, '%', 'e', '%', '8', // previous
MOD_MASK_SHIFT, '%', 'f', '%', '9', // print
MOD_MASK_SHIFT, '%', 'g', '%', '0', // redo
MOD_MASK_SHIFT, '%', 'h', '&', '3', // replace
MOD_MASK_SHIFT, '%', 'i', 'k', 'r', // right arr.
MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', // right arr.
MOD_MASK_SHIFT, '%', 'j', '&', '5', // resume
MOD_MASK_SHIFT, '!', '1', '&', '6', // save
MOD_MASK_SHIFT, '!', '2', '&', '7', // suspend
MOD_MASK_SHIFT, '!', '3', '&', '8', // undo
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', // up arrow
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', // down arrow
// vt100 F1
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', // F1
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', // F10
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
// TAB pseudo code
MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
NUL
};
static struct key_name_entry
{
int key; // Special key code or ascii value
char_u *name; // Name of key
} key_names_table[] =
{
{' ', (char_u *)"Space"},
{TAB, (char_u *)"Tab"},
{K_TAB, (char_u *)"Tab"},
{NL, (char_u *)"NL"},
{NL, (char_u *)"NewLine"}, // Alternative name
{NL, (char_u *)"LineFeed"}, // Alternative name
{NL, (char_u *)"LF"}, // Alternative name
{CAR, (char_u *)"CR"},
{CAR, (char_u *)"Return"}, // Alternative name
{CAR, (char_u *)"Enter"}, // Alternative name
{K_BS, (char_u *)"BS"},
{K_BS, (char_u *)"BackSpace"}, // Alternative name
{ESC, (char_u *)"Esc"},
{CSI, (char_u *)"CSI"},
{K_CSI, (char_u *)"xCSI"},
{'|', (char_u *)"Bar"},
{'\\', (char_u *)"Bslash"},
{K_DEL, (char_u *)"Del"},
{K_DEL, (char_u *)"Delete"}, // Alternative name
{K_KDEL, (char_u *)"kDel"},
{K_UP, (char_u *)"Up"},
{K_DOWN, (char_u *)"Down"},
{K_LEFT, (char_u *)"Left"},
{K_RIGHT, (char_u *)"Right"},
{K_XUP, (char_u *)"xUp"},
{K_XDOWN, (char_u *)"xDown"},
{K_XLEFT, (char_u *)"xLeft"},
{K_XRIGHT, (char_u *)"xRight"},
{K_PS, (char_u *)"PasteStart"},
{K_PE, (char_u *)"PasteEnd"},
{K_F1, (char_u *)"F1"},
{K_F2, (char_u *)"F2"},
{K_F3, (char_u *)"F3"},
{K_F4, (char_u *)"F4"},
{K_F5, (char_u *)"F5"},
{K_F6, (char_u *)"F6"},
{K_F7, (char_u *)"F7"},
{K_F8, (char_u *)"F8"},
{K_F9, (char_u *)"F9"},
{K_F10, (char_u *)"F10"},
{K_F11, (char_u *)"F11"},
{K_F12, (char_u *)"F12"},
{K_F13, (char_u *)"F13"},
{K_F14, (char_u *)"F14"},
{K_F15, (char_u *)"F15"},
{K_F16, (char_u *)"F16"},
{K_F17, (char_u *)"F17"},
{K_F18, (char_u *)"F18"},
{K_F19, (char_u *)"F19"},
{K_F20, (char_u *)"F20"},
{K_F21, (char_u *)"F21"},
{K_F22, (char_u *)"F22"},
{K_F23, (char_u *)"F23"},
{K_F24, (char_u *)"F24"},
{K_F25, (char_u *)"F25"},
{K_F26, (char_u *)"F26"},
{K_F27, (char_u *)"F27"},
{K_F28, (char_u *)"F28"},
{K_F29, (char_u *)"F29"},
{K_F30, (char_u *)"F30"},
{K_F31, (char_u *)"F31"},
{K_F32, (char_u *)"F32"},
{K_F33, (char_u *)"F33"},
{K_F34, (char_u *)"F34"},
{K_F35, (char_u *)"F35"},
{K_F36, (char_u *)"F36"},
{K_F37, (char_u *)"F37"},
{K_XF1, (char_u *)"xF1"},
{K_XF2, (char_u *)"xF2"},
{K_XF3, (char_u *)"xF3"},
{K_XF4, (char_u *)"xF4"},
{K_HELP, (char_u *)"Help"},
{K_UNDO, (char_u *)"Undo"},
{K_INS, (char_u *)"Insert"},
{K_INS, (char_u *)"Ins"}, // Alternative name
{K_KINS, (char_u *)"kInsert"},
{K_HOME, (char_u *)"Home"},
{K_KHOME, (char_u *)"kHome"},
{K_XHOME, (char_u *)"xHome"},
{K_ZHOME, (char_u *)"zHome"},
{K_END, (char_u *)"End"},
{K_KEND, (char_u *)"kEnd"},
{K_XEND, (char_u *)"xEnd"},
{K_ZEND, (char_u *)"zEnd"},
{K_PAGEUP, (char_u *)"PageUp"},
{K_PAGEDOWN, (char_u *)"PageDown"},
{K_KPAGEUP, (char_u *)"kPageUp"},
{K_KPAGEDOWN, (char_u *)"kPageDown"},
{K_KPLUS, (char_u *)"kPlus"},
{K_KMINUS, (char_u *)"kMinus"},
{K_KDIVIDE, (char_u *)"kDivide"},
{K_KMULTIPLY, (char_u *)"kMultiply"},
{K_KENTER, (char_u *)"kEnter"},
{K_KPOINT, (char_u *)"kPoint"},
{K_K0, (char_u *)"k0"},
{K_K1, (char_u *)"k1"},
{K_K2, (char_u *)"k2"},
{K_K3, (char_u *)"k3"},
{K_K4, (char_u *)"k4"},
{K_K5, (char_u *)"k5"},
{K_K6, (char_u *)"k6"},
{K_K7, (char_u *)"k7"},
{K_K8, (char_u *)"k8"},
{K_K9, (char_u *)"k9"},
{'<', (char_u *)"lt"},
{K_MOUSE, (char_u *)"Mouse"},
#ifdef FEAT_MOUSE_NET
{K_NETTERM_MOUSE, (char_u *)"NetMouse"},
#endif
#ifdef FEAT_MOUSE_DEC
{K_DEC_MOUSE, (char_u *)"DecMouse"},
#endif
#ifdef FEAT_MOUSE_JSB
{K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
#endif
#ifdef FEAT_MOUSE_PTERM
{K_PTERM_MOUSE, (char_u *)"PtermMouse"},
#endif
#ifdef FEAT_MOUSE_URXVT
{K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
#endif
{K_SGR_MOUSE, (char_u *)"SgrMouse"},
{K_SGR_MOUSERELEASE, (char_u *)"SgrMouseRelelase"},
{K_LEFTMOUSE, (char_u *)"LeftMouse"},
{K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
{K_LEFTDRAG, (char_u *)"LeftDrag"},
{K_LEFTRELEASE, (char_u *)"LeftRelease"},
{K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
{K_MOUSEMOVE, (char_u *)"MouseMove"},
{K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
{K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
{K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
{K_RIGHTMOUSE, (char_u *)"RightMouse"},
{K_RIGHTDRAG, (char_u *)"RightDrag"},
{K_RIGHTRELEASE, (char_u *)"RightRelease"},
{K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
{K_MOUSEUP, (char_u *)"ScrollWheelDown"},
{K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
{K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
{K_MOUSEDOWN, (char_u *)"MouseDown"}, // OBSOLETE: Use
{K_MOUSEUP, (char_u *)"MouseUp"}, // ScrollWheelXXX instead
{K_X1MOUSE, (char_u *)"X1Mouse"},
{K_X1DRAG, (char_u *)"X1Drag"},
{K_X1RELEASE, (char_u *)"X1Release"},
{K_X2MOUSE, (char_u *)"X2Mouse"},
{K_X2DRAG, (char_u *)"X2Drag"},
{K_X2RELEASE, (char_u *)"X2Release"},
{K_DROP, (char_u *)"Drop"},
{K_ZERO, (char_u *)"Nul"},
#ifdef FEAT_EVAL
{K_SNR, (char_u *)"SNR"},
#endif
{K_PLUG, (char_u *)"Plug"},
{K_CURSORHOLD, (char_u *)"CursorHold"},
{K_IGNORE, (char_u *)"Ignore"},
{0, NULL}
// NOTE: When adding a long name update MAX_KEY_NAME_LEN.
};
#define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry))
/*
* Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
* modifier name ('S' for Shift, 'C' for Ctrl etc).
*/
static int
name_to_mod_mask(int c)
{
int i;
c = TOUPPER_ASC(c);
for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
if (c == mod_mask_table[i].name)
return mod_mask_table[i].mod_flag;
return 0;
}
/*
* Check if if there is a special key code for "key" that includes the
* modifiers specified.
*/
int
simplify_key(int key, int *modifiers)
{
int i;
int key0;
int key1;
if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
{
// TAB is a special case
if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
{
*modifiers &= ~MOD_MASK_SHIFT;
return K_S_TAB;
}
key0 = KEY2TERMCAP0(key);
key1 = KEY2TERMCAP1(key);
for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
if (key0 == modifier_keys_table[i + 3]
&& key1 == modifier_keys_table[i + 4]
&& (*modifiers & modifier_keys_table[i]))
{
*modifiers &= ~modifier_keys_table[i];
return TERMCAP2KEY(modifier_keys_table[i + 1],
modifier_keys_table[i + 2]);
}
}
return key;
}
/*
* Change <xHome> to <Home>, <xUp> to <Up>, etc.
*/
int
handle_x_keys(int key)
{
switch (key)
{
case K_XUP: return K_UP;
case K_XDOWN: return K_DOWN;
case K_XLEFT: return K_LEFT;
case K_XRIGHT: return K_RIGHT;
case K_XHOME: return K_HOME;
case K_ZHOME: return K_HOME;
case K_XEND: return K_END;
case K_ZEND: return K_END;
case K_XF1: return K_F1;
case K_XF2: return K_F2;
case K_XF3: return K_F3;
case K_XF4: return K_F4;
case K_S_XF1: return K_S_F1;
case K_S_XF2: return K_S_F2;
case K_S_XF3: return K_S_F3;
case K_S_XF4: return K_S_F4;
}
return key;
}
/*
* Return a string which contains the name of the given key when the given
* modifiers are down.
*/
char_u *
get_special_key_name(int c, int modifiers)
{
static char_u string[MAX_KEY_NAME_LEN + 1];
int i, idx;
int table_idx;
char_u *s;
string[0] = '<';
idx = 1;
// Key that stands for a normal character.
if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
c = KEY2TERMCAP1(c);
/*
* Translate shifted special keys into unshifted keys and set modifier.
* Same for CTRL and ALT modifiers.
*/
if (IS_SPECIAL(c))
{
for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
&& (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
{
modifiers |= modifier_keys_table[i];
c = TERMCAP2KEY(modifier_keys_table[i + 3],
modifier_keys_table[i + 4]);
break;
}
}
// try to find the key in the special key table
table_idx = find_special_key_in_table(c);
/*
* When not a known special key, and not a printable character, try to
* extract modifiers.
*/
if (c > 0 && (*mb_char2len)(c) == 1)
{
if (table_idx < 0
&& (!vim_isprintc(c) || (c & 0x7f) == ' ')
&& (c & 0x80))
{
c &= 0x7f;
modifiers |= MOD_MASK_ALT;
// try again, to find the un-alted key in the special key table
table_idx = find_special_key_in_table(c);
}
if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
{
#ifdef EBCDIC
c = CtrlChar(c);
#else
c += '@';
#endif
modifiers |= MOD_MASK_CTRL;
}
}
// translate the modifier into a string
for (i = 0; mod_mask_table[i].name != 'A'; i++)
if ((modifiers & mod_mask_table[i].mod_mask)
== mod_mask_table[i].mod_flag)
{
string[idx++] = mod_mask_table[i].name;
string[idx++] = (char_u)'-';
}
if (table_idx < 0) // unknown special key, may output t_xx
{
if (IS_SPECIAL(c))
{
string[idx++] = 't';
string[idx++] = '_';
string[idx++] = KEY2TERMCAP0(c);
string[idx++] = KEY2TERMCAP1(c);
}
// Not a special key, only modifiers, output directly
else
{
if (has_mbyte && (*mb_char2len)(c) > 1)
idx += (*mb_char2bytes)(c, string + idx);
else if (vim_isprintc(c))
string[idx++] = c;
else
{
s = transchar(c);
while (*s)
string[idx++] = *s++;
}
}
}
else // use name of special key
{
size_t len = STRLEN(key_names_table[table_idx].name);
if (len + idx + 2 <= MAX_KEY_NAME_LEN)
{
STRCPY(string + idx, key_names_table[table_idx].name);
idx += (int)len;
}
}
string[idx++] = '>';
string[idx] = NUL;
return string;
}
/*
* Try translating a <> name at (*srcp)[] to dst[].
* Return the number of characters added to dst[], zero for no match.
* If there is a match, srcp is advanced to after the <> name.
* dst[] must be big enough to hold the result (up to six characters)!
*/
int
trans_special(
char_u **srcp,
char_u *dst,
int flags, // FSK_ values
int *did_simplify) // FSK_SIMPLIFY and found <C-H> or <A-x>
{
int modifiers = 0;
int key;
key = find_special_key(srcp, &modifiers, flags, did_simplify);
if (key == 0)
return 0;
return special_to_buf(key, modifiers, flags & FSK_KEYCODE, dst);
}
/*
* Put the character sequence for "key" with "modifiers" into "dst" and return
* the resulting length.
* When "keycode" is TRUE prefer key code, e.g. K_DEL instead of DEL.
* The sequence is not NUL terminated.
* This is how characters in a string are encoded.
*/
int
special_to_buf(int key, int modifiers, int keycode, char_u *dst)
{
int dlen = 0;
// Put the appropriate modifier in a string
if (modifiers != 0)
{
dst[dlen++] = K_SPECIAL;
dst[dlen++] = KS_MODIFIER;
dst[dlen++] = modifiers;
}
if (IS_SPECIAL(key))
{
dst[dlen++] = K_SPECIAL;
dst[dlen++] = KEY2TERMCAP0(key);
dst[dlen++] = KEY2TERMCAP1(key);
}
else if (has_mbyte && !keycode)
dlen += (*mb_char2bytes)(key, dst + dlen);
else if (keycode)
dlen = (int)(add_char2buf(key, dst + dlen) - dst);
else
dst[dlen++] = key;
return dlen;
}
/*
* Try translating a <> name at (*srcp)[], return the key and modifiers.
* srcp is advanced to after the <> name.
* returns 0 if there is no match.
*/
int
find_special_key(
char_u **srcp,
int *modp,
int flags, // FSK_ values
int *did_simplify) // found <C-H> or <A-x>
{
char_u *last_dash;
char_u *end_of_name;
char_u *src;
char_u *bp;
int in_string = flags & FSK_IN_STRING;
int modifiers;
int bit;
int key;
uvarnumber_T n;
int l;
src = *srcp;
if (src[0] != '<')
return 0;
if (src[1] == '*') // <*xxx>: do not simplify
++src;
// Find end of modifier list
last_dash = src;
for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++)
{
if (*bp == '-')
{
last_dash = bp;
if (bp[1] != NUL)
{
if (has_mbyte)
l = mb_ptr2len(bp + 1);
else
l = 1;
// Anything accepted, like <C-?>.
// <C-"> or <M-"> are not special in strings as " is
// the string delimiter. With a backslash it works: <M-\">
if (!(in_string && bp[1] == '"') && bp[l + 1] == '>')
bp += l;
else if (in_string && bp[1] == '\\' && bp[2] == '"'
&& bp[3] == '>')
bp += 2;
}
}
if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
bp += 3; // skip t_xx, xx may be '-' or '>'
else if (STRNICMP(bp, "char-", 5) == 0)
{
vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0, TRUE);
if (l == 0)
{
emsg(_(e_invarg));
return 0;
}
bp += l + 5;
break;
}
}
if (*bp == '>') // found matching '>'
{
end_of_name = bp + 1;
// Which modifiers are given?
modifiers = 0x0;
for (bp = src + 1; bp < last_dash; bp++)
{
if (*bp != '-')
{
bit = name_to_mod_mask(*bp);
if (bit == 0x0)
break; // Illegal modifier name
modifiers |= bit;
}
}
/*
* Legal modifier name.
*/
if (bp >= last_dash)
{
if (STRNICMP(last_dash + 1, "char-", 5) == 0
&& VIM_ISDIGIT(last_dash[6]))
{
// <Char-123> or <Char-033> or <Char-0x33>
vim_str2nr(last_dash + 6, NULL, &l, STR2NR_ALL, NULL,
&n, 0, TRUE);
if (l == 0)
{
emsg(_(e_invarg));
return 0;
}
key = (int)n;
}
else
{
int off = 1;
// Modifier with single letter, or special key name.
if (in_string && last_dash[1] == '\\' && last_dash[2] == '"')
off = 2;
if (has_mbyte)
l = mb_ptr2len(last_dash + off);
else
l = 1;
if (modifiers != 0 && last_dash[l + off] == '>')
key = PTR2CHAR(last_dash + off);
else
{
key = get_special_key_code(last_dash + off);
if (!(flags & FSK_KEEP_X_KEY))
key = handle_x_keys(key);
}
}
/*
* get_special_key_code() may return NUL for invalid
* special key name.
*/
if (key != NUL)
{
/*
* Only use a modifier when there is no special key code that
* includes the modifier.
*/
key = simplify_key(key, &modifiers);
if (!(flags & FSK_KEYCODE))
{
// don't want keycode, use single byte code
if (key == K_BS)
key = BS;
else if (key == K_DEL || key == K_KDEL)
key = DEL;
}
// Normal Key with modifier: Try to make a single byte code.
if (!IS_SPECIAL(key))
key = extract_modifiers(key, &modifiers,
flags & FSK_SIMPLIFY, did_simplify);
*modp = modifiers;
*srcp = end_of_name;
return key;
}
}
}
return 0;
}
/*
* Try to include modifiers in the key.
* Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
* When "simplify" is FALSE don't do Ctrl and Alt.
* When "simplify" is TRUE and Ctrl or Alt is removed from modifiers set
* "did_simplify" when it's not NULL.
*/
int
extract_modifiers(int key, int *modp, int simplify, int *did_simplify)
{
int modifiers = *modp;
#ifdef MACOS_X
// Command-key really special, no fancynest
if (!(modifiers & MOD_MASK_CMD))
#endif
if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
{
key = TOUPPER_ASC(key);
// With <C-S-a> and <A-S-a> we keep the shift modifier.
// With <S-a> and <S-A> we don't keep the shift modifier.
if (simplify || modifiers == MOD_MASK_SHIFT)
modifiers &= ~MOD_MASK_SHIFT;
}
// <C-H> and <C-h> mean the same thing, always use "H"
if ((modifiers & MOD_MASK_CTRL) && ASCII_ISALPHA(key))
key = TOUPPER_ASC(key);
if (simplify && (modifiers & MOD_MASK_CTRL)
#ifdef EBCDIC
// TODO: EBCDIC Better use:
// && (Ctrl_chr(key) || key == '?')
// ???
&& strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
!= NULL
#else
&& ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
#endif
)
{
key = Ctrl_chr(key);
modifiers &= ~MOD_MASK_CTRL;
// <C-@> is <Nul>
if (key == 0)
key = K_ZERO;
if (did_simplify != NULL)
*did_simplify = TRUE;
}
#ifdef MACOS_X
// Command-key really special, no fancynest
if (!(modifiers & MOD_MASK_CMD))
#endif
if (simplify && (modifiers & MOD_MASK_ALT) && key < 0x80
&& !enc_dbcs) // avoid creating a lead byte
{
key |= 0x80;
modifiers &= ~MOD_MASK_ALT; // remove the META modifier
if (did_simplify != NULL)
*did_simplify = TRUE;
}
*modp = modifiers;
return key;
}
/*
* Try to find key "c" in the special key table.
* Return the index when found, -1 when not found.
*/
int
find_special_key_in_table(int c)
{
int i;
for (i = 0; key_names_table[i].name != NULL; i++)
if (c == key_names_table[i].key)
break;
if (key_names_table[i].name == NULL)
i = -1;
return i;
}
/*
* Find the special key with the given name (the given string does not have to
* end with NUL, the name is assumed to end before the first non-idchar).
* If the name starts with "t_" the next two characters are interpreted as a
* termcap name.
* Return the key code, or 0 if not found.
*/
int
get_special_key_code(char_u *name)
{
char_u *table_name;
char_u string[3];
int i, j;
/*
* If it's <t_xx> we get the code for xx from the termcap
*/
if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
{
string[0] = name[2];
string[1] = name[3];
string[2] = NUL;
if (add_termcap_entry(string, FALSE) == OK)
return TERMCAP2KEY(name[2], name[3]);
}
else
for (i = 0; key_names_table[i].name != NULL; i++)
{
table_name = key_names_table[i].name;
for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++)
if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
break;
if (!vim_isIDc(name[j]) && table_name[j] == NUL)
return key_names_table[i].key;
}
return 0;
}
char_u *
get_key_name(int i)
{
if (i >= (int)KEY_NAMES_TABLE_LEN)
return NULL;
return key_names_table[i].name;
}
/*
* Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
*/
int
get_fileformat(buf_T *buf)
{
int c = *buf->b_p_ff;
if (buf->b_p_bin || c == 'u')
return EOL_UNIX;
if (c == 'm')
return EOL_MAC;
return EOL_DOS;
}
/*
* Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
* argument.
*/
int
get_fileformat_force(
buf_T *buf,
exarg_T *eap) // can be NULL!
{
int c;
if (eap != NULL && eap->force_ff != 0)
c = eap->force_ff;
else
{
if ((eap != NULL && eap->force_bin != 0)
? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
return EOL_UNIX;
c = *buf->b_p_ff;
}
if (c == 'u')
return EOL_UNIX;
if (c == 'm')
return EOL_MAC;
return EOL_DOS;
}
/*
* Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
* Sets both 'textmode' and 'fileformat'.
* Note: Does _not_ set global value of 'textmode'!
*/
void
set_fileformat(
int t,
int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
{
char *p = NULL;
switch (t)
{
case EOL_DOS:
p = FF_DOS;
curbuf->b_p_tx = TRUE;
break;
case EOL_UNIX:
p = FF_UNIX;
curbuf->b_p_tx = FALSE;
break;
case EOL_MAC:
p = FF_MAC;
curbuf->b_p_tx = FALSE;
break;
}
if (p != NULL)
set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
OPT_FREE | opt_flags, 0);
// This may cause the buffer to become (un)modified.
check_status(curbuf);
redraw_tabline = TRUE;
#ifdef FEAT_TITLE
need_maketitle = TRUE; // set window title later
#endif
}
/*
* Return the default fileformat from 'fileformats'.
*/
int
default_fileformat(void)
{
switch (*p_ffs)
{
case 'm': return EOL_MAC;
case 'd': return EOL_DOS;
}
return EOL_UNIX;
}
/*
* Call shell. Calls mch_call_shell, with 'shellxquote' added.
*/
int
call_shell(char_u *cmd, int opt)
{
char_u *ncmd;
int retval;
#ifdef FEAT_PROFILE
proftime_T wait_time;
#endif
if (p_verbose > 3)
{
verbose_enter();
smsg(_("Calling shell to execute: \"%s\""), cmd == NULL ? p_sh : cmd);
out_char('\n');
cursor_on();
verbose_leave();
}
#ifdef FEAT_PROFILE
if (do_profiling == PROF_YES)
prof_child_enter(&wait_time);
#endif
if (*p_sh == NUL)
{
emsg(_(e_shellempty));
retval = -1;
}
else
{
#ifdef FEAT_GUI_MSWIN
// Don't hide the pointer while executing a shell command.
gui_mch_mousehide(FALSE);
#endif
#ifdef FEAT_GUI
++hold_gui_events;
#endif
// The external command may update a tags file, clear cached tags.
tag_freematch();
if (cmd == NULL || *p_sxq == NUL)
retval = mch_call_shell(cmd, opt);
else
{
char_u *ecmd = cmd;
if (*p_sxe != NUL && *p_sxq == '(')
{
ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
if (ecmd == NULL)
ecmd = cmd;
}
ncmd = alloc(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1);
if (ncmd != NULL)
{
STRCPY(ncmd, p_sxq);
STRCAT(ncmd, ecmd);
// When 'shellxquote' is ( append ).
// When 'shellxquote' is "( append )".
STRCAT(ncmd, *p_sxq == '(' ? (char_u *)")"
: *p_sxq == '"' && *(p_sxq+1) == '(' ? (char_u *)")\""
: p_sxq);
retval = mch_call_shell(ncmd, opt);
vim_free(ncmd);
}
else
retval = -1;
if (ecmd != cmd)
vim_free(ecmd);
}
#ifdef FEAT_GUI
--hold_gui_events;
#endif
/*
* Check the window size, in case it changed while executing the
* external command.
*/
shell_resized_check();
}
#ifdef FEAT_EVAL
set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
# ifdef FEAT_PROFILE
if (do_profiling == PROF_YES)
prof_child_exit(&wait_time);
# endif
#endif
return retval;
}
/*
* VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
* NORMAL State with a condition. This function returns the real State.
*/
int
get_real_state(void)
{
if (State & NORMAL)
{
if (VIsual_active)
{
if (VIsual_select)
return SELECTMODE;
return VISUAL;
}
else if (finish_op)
return OP_PENDING;
}
return State;
}
/*
* Return TRUE if "p" points to just after a path separator.
* Takes care of multi-byte characters.
* "b" must point to the start of the file name
*/
int
after_pathsep(char_u *b, char_u *p)
{
return p > b && vim_ispathsep(p[-1])
&& (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
}
/*
* Return TRUE if file names "f1" and "f2" are in the same directory.
* "f1" may be a short name, "f2" must be a full path.
*/
int
same_directory(char_u *f1, char_u *f2)
{
char_u ffname[MAXPATHL];
char_u *t1;
char_u *t2;
// safety check
if (f1 == NULL || f2 == NULL)
return FALSE;
(void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
t1 = gettail_sep(ffname);
t2 = gettail_sep(f2);
return (t1 - ffname == t2 - f2
&& pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
}
#if defined(FEAT_SESSION) || defined(FEAT_AUTOCHDIR) \
|| defined(MSWIN) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK) \
|| defined(FEAT_NETBEANS_INTG) \
|| defined(PROTO)
/*
* Change to a file's directory.
* Caller must call shorten_fnames()!
* Return OK or FAIL.
*/
int
vim_chdirfile(char_u *fname, char *trigger_autocmd)
{
char_u old_dir[MAXPATHL];
char_u new_dir[MAXPATHL];
int res;
if (mch_dirname(old_dir, MAXPATHL) != OK)
*old_dir = NUL;
vim_strncpy(new_dir, fname, MAXPATHL - 1);
*gettail_sep(new_dir) = NUL;
if (pathcmp((char *)old_dir, (char *)new_dir, -1) == 0)
// nothing to do
res = OK;
else
{
res = mch_chdir((char *)new_dir) == 0 ? OK : FAIL;
if (res == OK && trigger_autocmd != NULL)
apply_autocmds(EVENT_DIRCHANGED, (char_u *)trigger_autocmd,
new_dir, FALSE, curbuf);
}
return res;
}
#endif
#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
/*
* Check if "name" ends in a slash and is not a directory.
* Used for systems where stat() ignores a trailing slash on a file name.
* The Vim code assumes a trailing slash is only ignored for a directory.
*/
static int
illegal_slash(const char *name)
{
if (name[0] == NUL)
return FALSE; // no file name is not illegal
if (name[strlen(name) - 1] != '/')
return FALSE; // no trailing slash
if (mch_isdir((char_u *)name))
return FALSE; // trailing slash for a directory
return TRUE;
}
/*
* Special implementation of mch_stat() for Solaris.
*/
int
vim_stat(const char *name, stat_T *stp)
{
// On Solaris stat() accepts "file/" as if it was "file". Return -1 if
// the name ends in "/" and it's not a directory.
return illegal_slash(name) ? -1 : stat(name, stp);
}
#endif
#if defined(CURSOR_SHAPE) || defined(PROTO)
/*
* Handling of cursor and mouse pointer shapes in various modes.
*/
cursorentry_T shape_table[SHAPE_IDX_COUNT] =
{
// The values will be filled in from the 'guicursor' and 'mouseshape'
// defaults when Vim starts.
// Adjust the SHAPE_IDX_ defines when making changes!
{0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
{0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
{0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
{0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
{0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
{0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
{0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
{0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
};
#ifdef FEAT_MOUSESHAPE
/*
* Table with names for mouse shapes. Keep in sync with all the tables for
* mch_set_mouse_shape()!.
*/
static char * mshape_names[] =
{
"arrow", // default, must be the first one
"blank", // hidden
"beam",
"updown",
"udsizing",
"leftright",
"lrsizing",
"busy",
"no",
"crosshair",
"hand1",
"hand2",
"pencil",
"question",
"rightup-arrow",
"up-arrow",
NULL
};
#endif
/*
* Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
* ("what" is SHAPE_MOUSE).
* Returns error message for an illegal option, NULL otherwise.
*/
char *
parse_shape_opt(int what)
{
char_u *modep;
char_u *colonp;
char_u *commap;
char_u *slashp;
char_u *p, *endp;
int idx = 0; // init for GCC
int all_idx;
int len;
int i;
long n;
int found_ve = FALSE; // found "ve" flag
int round;
/*
* First round: check for errors; second round: do it for real.
*/
for (round = 1; round <= 2; ++round)
{
/*
* Repeat for all comma separated parts.
*/
#ifdef FEAT_MOUSESHAPE
if (what == SHAPE_MOUSE)
modep = p_mouseshape;
else
#endif
modep = p_guicursor;
while (*modep != NUL)
{
colonp = vim_strchr(modep, ':');
commap = vim_strchr(modep, ',');
if (colonp == NULL || (commap != NULL && commap < colonp))
return N_("E545: Missing colon");
if (colonp == modep)
return N_("E546: Illegal mode");
/*
* Repeat for all mode's before the colon.
* For the 'a' mode, we loop to handle all the modes.
*/
all_idx = -1;
while (modep < colonp || all_idx >= 0)
{
if (all_idx < 0)
{
// Find the mode.
if (modep[1] == '-' || modep[1] == ':')
len = 1;
else
len = 2;
if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
all_idx = SHAPE_IDX_COUNT - 1;
else
{
for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
if (STRNICMP(modep, shape_table[idx].name, len)
== 0)
break;
if (idx == SHAPE_IDX_COUNT
|| (shape_table[idx].used_for & what) == 0)
return N_("E546: Illegal mode");
if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
found_ve = TRUE;
}
modep += len + 1;
}
if (all_idx >= 0)
idx = all_idx--;
else if (round == 2)
{
#ifdef FEAT_MOUSESHAPE
if (what == SHAPE_MOUSE)
{
// Set the default, for the missing parts
shape_table[idx].mshape = 0;
}
else
#endif
{
// Set the defaults, for the missing parts
shape_table[idx].shape = SHAPE_BLOCK;
shape_table[idx].blinkwait = 700L;
shape_table[idx].blinkon = 400L;
shape_table[idx].blinkoff = 250L;
}
}
// Parse the part after the colon
for (p = colonp + 1; *p && *p != ','; )
{
#ifdef FEAT_MOUSESHAPE
if (what == SHAPE_MOUSE)
{
for (i = 0; ; ++i)
{
if (mshape_names[i] == NULL)
{
if (!VIM_ISDIGIT(*p))
return N_("E547: Illegal mouseshape");
if (round == 2)
shape_table[idx].mshape =
getdigits(&p) + MSHAPE_NUMBERED;
else
(void)getdigits(&p);
break;
}
len = (int)STRLEN(mshape_names[i]);
if (STRNICMP(p, mshape_names[i], len) == 0)
{
if (round == 2)
shape_table[idx].mshape = i;
p += len;
break;
}
}
}
else // if (what == SHAPE_MOUSE)
#endif
{
/*
* First handle the ones with a number argument.
*/
i = *p;
len = 0;
if (STRNICMP(p, "ver", 3) == 0)
len = 3;
else if (STRNICMP(p, "hor", 3) == 0)
len = 3;
else if (STRNICMP(p, "blinkwait", 9) == 0)
len = 9;
else if (STRNICMP(p, "blinkon", 7) == 0)
len = 7;
else if (STRNICMP(p, "blinkoff", 8) == 0)
len = 8;
if (len != 0)
{
p += len;
if (!VIM_ISDIGIT(*p))
return N_("E548: digit expected");
n = getdigits(&p);
if (len == 3) // "ver" or "hor"
{
if (n == 0)
return N_("E549: Illegal percentage");
if (round == 2)
{
if (TOLOWER_ASC(i) == 'v')
shape_table[idx].shape = SHAPE_VER;
else
shape_table[idx].shape = SHAPE_HOR;
shape_table[idx].percentage = n;
}
}
else if (round == 2)
{
if (len == 9)
shape_table[idx].blinkwait = n;
else if (len == 7)
shape_table[idx].blinkon = n;
else
shape_table[idx].blinkoff = n;
}
}
else if (STRNICMP(p, "block", 5) == 0)
{
if (round == 2)
shape_table[idx].shape = SHAPE_BLOCK;
p += 5;
}
else // must be a highlight group name then
{
endp = vim_strchr(p, '-');
if (commap == NULL) // last part
{
if (endp == NULL)
endp = p + STRLEN(p); // find end of part
}
else if (endp > commap || endp == NULL)
endp = commap;
slashp = vim_strchr(p, '/');
if (slashp != NULL && slashp < endp)
{
// "group/langmap_group"
i = syn_check_group(p, (int)(slashp - p));
p = slashp + 1;
}
if (round == 2)
{
shape_table[idx].id = syn_check_group(p,
(int)(endp - p));
shape_table[idx].id_lm = shape_table[idx].id;
if (slashp != NULL && slashp < endp)
shape_table[idx].id = i;
}
p = endp;
}
} // if (what != SHAPE_MOUSE)
if (*p == '-')
++p;
}
}
modep = p;
if (*modep == ',')
++modep;
}
}
// If the 's' flag is not given, use the 'v' cursor for 's'
if (!found_ve)
{
#ifdef FEAT_MOUSESHAPE
if (what == SHAPE_MOUSE)
{
shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
}
else
#endif
{
shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
shape_table[SHAPE_IDX_VE].percentage =
shape_table[SHAPE_IDX_V].percentage;
shape_table[SHAPE_IDX_VE].blinkwait =
shape_table[SHAPE_IDX_V].blinkwait;
shape_table[SHAPE_IDX_VE].blinkon =
shape_table[SHAPE_IDX_V].blinkon;
shape_table[SHAPE_IDX_VE].blinkoff =
shape_table[SHAPE_IDX_V].blinkoff;
shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
}
}
return NULL;
}
# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
|| defined(FEAT_MOUSESHAPE) || defined(PROTO)
/*
* Return the index into shape_table[] for the current mode.
* When "mouse" is TRUE, consider indexes valid for the mouse pointer.
*/
int
get_shape_idx(int mouse)
{
#ifdef FEAT_MOUSESHAPE
if (mouse && (State == HITRETURN || State == ASKMORE))
{
# ifdef FEAT_GUI
int x, y;
gui_mch_getmouse(&x, &y);
if (Y_2_ROW(y) == Rows - 1)
return SHAPE_IDX_MOREL;
# endif
return SHAPE_IDX_MORE;
}
if (mouse && drag_status_line)
return SHAPE_IDX_SDRAG;
if (mouse && drag_sep_line)
return SHAPE_IDX_VDRAG;
#endif
if (!mouse && State == SHOWMATCH)
return SHAPE_IDX_SM;
if (State & VREPLACE_FLAG)
return SHAPE_IDX_R;
if (State & REPLACE_FLAG)
return SHAPE_IDX_R;
if (State & INSERT)
return SHAPE_IDX_I;
if (State & CMDLINE)
{
if (cmdline_at_end())
return SHAPE_IDX_C;
if (cmdline_overstrike())
return SHAPE_IDX_CR;
return SHAPE_IDX_CI;
}
if (finish_op)
return SHAPE_IDX_O;
if (VIsual_active)
{
if (*p_sel == 'e')
return SHAPE_IDX_VE;
else
return SHAPE_IDX_V;
}
return SHAPE_IDX_N;
}
#endif
# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
static int old_mouse_shape = 0;
/*
* Set the mouse shape:
* If "shape" is -1, use shape depending on the current mode,
* depending on the current state.
* If "shape" is -2, only update the shape when it's CLINE or STATUS (used
* when the mouse moves off the status or command line).
*/
void
update_mouseshape(int shape_idx)
{
int new_mouse_shape;
// Only works in GUI mode.
if (!gui.in_use || gui.starting)
return;
// Postpone the updating when more is to come. Speeds up executing of
// mappings.
if (shape_idx == -1 && char_avail())
{
postponed_mouseshape = TRUE;
return;
}
// When ignoring the mouse don't change shape on the statusline.
if (*p_mouse == NUL
&& (shape_idx == SHAPE_IDX_CLINE
|| shape_idx == SHAPE_IDX_STATUS
|| shape_idx == SHAPE_IDX_VSEP))
shape_idx = -2;
if (shape_idx == -2
&& old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
&& old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
&& old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
return;
if (shape_idx < 0)
new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
else
new_mouse_shape = shape_table[shape_idx].mshape;
if (new_mouse_shape != old_mouse_shape)
{
mch_set_mouse_shape(new_mouse_shape);
old_mouse_shape = new_mouse_shape;
}
postponed_mouseshape = FALSE;
}
# endif
#endif // CURSOR_SHAPE
/*
* Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
* 'cdpath' for relative directory names, otherwise just mch_chdir().
*/
int
vim_chdir(char_u *new_dir)
{
#ifndef FEAT_SEARCHPATH
return mch_chdir((char *)new_dir);
#else
char_u *dir_name;
int r;
dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
FNAME_MESS, curbuf->b_ffname);
if (dir_name == NULL)
return -1;
r = mch_chdir((char *)dir_name);
vim_free(dir_name);
return r;
#endif
}
/*
* Get user name from machine-specific function.
* Returns the user name in "buf[len]".
* Some systems are quite slow in obtaining the user name (Windows NT), thus
* cache the result.
* Returns OK or FAIL.
*/
int
get_user_name(char_u *buf, int len)
{
if (username == NULL)
{
if (mch_get_user_name(buf, len) == FAIL)
return FAIL;
username = vim_strsave(buf);
}
else
vim_strncpy(buf, username, len - 1);
return OK;
}
#ifndef HAVE_QSORT
/*
* Our own qsort(), for systems that don't have it.
* It's simple and slow. From the K&R C book.
*/
void
qsort(
void *base,
size_t elm_count,
size_t elm_size,
int (*cmp)(const void *, const void *))
{
char_u *buf;
char_u *p1;
char_u *p2;
int i, j;
int gap;
buf = alloc(elm_size);
if (buf == NULL)
return;
for (gap = elm_count / 2; gap > 0; gap /= 2)
for (i = gap; i < elm_count; ++i)
for (j = i - gap; j >= 0; j -= gap)
{
// Compare the elements.
p1 = (char_u *)base + j * elm_size;
p2 = (char_u *)base + (j + gap) * elm_size;
if ((*cmp)((void *)p1, (void *)p2) <= 0)
break;
// Exchange the elements.
mch_memmove(buf, p1, elm_size);
mch_memmove(p1, p2, elm_size);
mch_memmove(p2, buf, elm_size);
}
vim_free(buf);
}
#endif
/*
* Sort an array of strings.
*/
static int sort_compare(const void *s1, const void *s2);
static int
sort_compare(const void *s1, const void *s2)
{
return STRCMP(*(char **)s1, *(char **)s2);
}
void
sort_strings(
char_u **files,
int count)
{
qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
}
/*
* The putenv() implementation below comes from the "screen" program.
* Included with permission from Juergen Weigert.
* See pty.c for the copyright notice.
*/
/*
* putenv -- put value into environment
*
* Usage: i = putenv (string)
* int i;
* char *string;
*
* where string is of the form <name>=<value>.
* Putenv returns 0 normally, -1 on error (not enough core for malloc).
*
* Putenv may need to add a new name into the environment, or to
* associate a value longer than the current value with a particular
* name. So, to make life simpler, putenv() copies your entire
* environment into the heap (i.e. malloc()) from the stack
* (i.e. where it resides when your process is initiated) the first
* time you call it.
*
* (history removed, not very interesting. See the "screen" sources.)
*/
#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
#define EXTRASIZE 5 // increment to add to env. size
static int envsize = -1; // current size of environment
extern char **environ; // the global which is your env.
static int findenv(char *name); // look for a name in the env.
static int newenv(void); // copy env. from stack to heap
static int moreenv(void); // incr. size of env.
int
putenv(const char *string)
{
int i;
char *p;
if (envsize < 0)
{ // first time putenv called
if (newenv() < 0) // copy env. to heap
return -1;
}
i = findenv((char *)string); // look for name in environment
if (i < 0)
{ // name must be added
for (i = 0; environ[i]; i++);
if (i >= (envsize - 1))
{ // need new slot
if (moreenv() < 0)
return -1;
}
p = alloc(strlen(string) + 1);
if (p == NULL) // not enough core
return -1;
environ[i + 1] = 0; // new end of env.
}
else
{ // name already in env.
p = vim_realloc(environ[i], strlen(string) + 1);
if (p == NULL)
return -1;
}
sprintf(p, "%s", string); // copy into env.
environ[i] = p;
return 0;
}
static int
findenv(char *name)
{
char *namechar, *envchar;
int i, found;
found = 0;
for (i = 0; environ[i] && !found; i++)
{
envchar = environ[i];
namechar = name;
while (*namechar && *namechar != '=' && (*namechar == *envchar))
{
namechar++;
envchar++;
}
found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
}
return found ? i - 1 : -1;
}
static int
newenv(void)
{
char **env, *elem;
int i, esize;
for (i = 0; environ[i]; i++)
;
esize = i + EXTRASIZE + 1;
env = ALLOC_MULT(char *, esize);
if (env == NULL)
return -1;
for (i = 0; environ[i]; i++)
{
elem = alloc(strlen(environ[i]) + 1);
if (elem == NULL)
return -1;
env[i] = elem;
strcpy(elem, environ[i]);
}
env[i] = 0;
environ = env;
envsize = esize;
return 0;
}
static int
moreenv(void)
{
int esize;
char **env;
esize = envsize + EXTRASIZE;
env = vim_realloc((char *)environ, esize * sizeof (*env));
if (env == 0)
return -1;
environ = env;
envsize = esize;
return 0;
}
# ifdef USE_VIMPTY_GETENV
/*
* Used for mch_getenv() for Mac.
*/
char_u *
vimpty_getenv(const char_u *string)
{
int i;
char_u *p;
if (envsize < 0)
return NULL;
i = findenv((char *)string);
if (i < 0)
return NULL;
p = vim_strchr((char_u *)environ[i], '=');
return (p + 1);
}
# endif
#endif // !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
/*
* Return 0 for not writable, 1 for writable file, 2 for a dir which we have
* rights to write into.
*/
int
filewritable(char_u *fname)
{
int retval = 0;
#if defined(UNIX) || defined(VMS)
int perm = 0;
#endif
#if defined(UNIX) || defined(VMS)
perm = mch_getperm(fname);
#endif
if (
# ifdef MSWIN
mch_writable(fname) &&
# else
# if defined(UNIX) || defined(VMS)
(perm & 0222) &&
# endif
# endif
mch_access((char *)fname, W_OK) == 0
)
{
++retval;
if (mch_isdir(fname))
++retval;
}
return retval;
}
#endif
#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
/*
* Read 2 bytes from "fd" and turn them into an int, MSB first.
* Returns -1 when encountering EOF.
*/
int
get2c(FILE *fd)
{
int c, n;
n = getc(fd);
if (n == EOF) return -1;
c = getc(fd);
if (c == EOF) return -1;
return (n << 8) + c;
}
/*
* Read 3 bytes from "fd" and turn them into an int, MSB first.
* Returns -1 when encountering EOF.
*/
int
get3c(FILE *fd)
{
int c, n;
n = getc(fd);
if (n == EOF) return -1;
c = getc(fd);
if (c == EOF) return -1;
n = (n << 8) + c;
c = getc(fd);
if (c == EOF) return -1;
return (n << 8) + c;
}
/*
* Read 4 bytes from "fd" and turn them into an int, MSB first.
* Returns -1 when encountering EOF.
*/
int
get4c(FILE *fd)
{
int c;
// Use unsigned rather than int otherwise result is undefined
// when left-shift sets the MSB.
unsigned n;
c = getc(fd);
if (c == EOF) return -1;
n = (unsigned)c;
c = getc(fd);
if (c == EOF) return -1;
n = (n << 8) + (unsigned)c;
c = getc(fd);
if (c == EOF) return -1;
n = (n << 8) + (unsigned)c;
c = getc(fd);
if (c == EOF) return -1;
n = (n << 8) + (unsigned)c;
return (int)n;
}
/*
* Read a string of length "cnt" from "fd" into allocated memory.
* Returns NULL when out of memory or unable to read that many bytes.
*/
char_u *
read_string(FILE *fd, int cnt)
{
char_u *str;
int i;
int c;
// allocate memory
str = alloc(cnt + 1);
if (str != NULL)
{
// Read the string. Quit when running into the EOF.
for (i = 0; i < cnt; ++i)
{
c = getc(fd);
if (c == EOF)
{
vim_free(str);
return NULL;
}
str[i] = c;
}
str[i] = NUL;
}
return str;
}
/*
* Write a number to file "fd", MSB first, in "len" bytes.
*/
int
put_bytes(FILE *fd, long_u nr, int len)
{
int i;
for (i = len - 1; i >= 0; --i)
if (putc((int)(nr >> (i * 8)), fd) == EOF)
return FAIL;
return OK;
}
#endif
#if defined(FEAT_QUICKFIX) || defined(FEAT_SPELL) || defined(PROTO)
/*
* Return TRUE if string "s" contains a non-ASCII character (128 or higher).
* When "s" is NULL FALSE is returned.
*/
int
has_non_ascii(char_u *s)
{
char_u *p;
if (s != NULL)
for (p = s; *p != NUL; ++p)
if (*p >= 128)
return TRUE;
return FALSE;
}
#endif
#ifndef PROTO // proto is defined in vim.h
# ifdef ELAPSED_TIMEVAL
/*
* Return time in msec since "start_tv".
*/
long
elapsed(struct timeval *start_tv)
{
struct timeval now_tv;
gettimeofday(&now_tv, NULL);
return (now_tv.tv_sec - start_tv->tv_sec) * 1000L
+ (now_tv.tv_usec - start_tv->tv_usec) / 1000L;
}
# endif
# ifdef ELAPSED_TICKCOUNT
/*
* Return time in msec since "start_tick".
*/
long
elapsed(DWORD start_tick)
{
DWORD now = GetTickCount();
return (long)now - (long)start_tick;
}
# endif
#endif
#if defined(FEAT_JOB_CHANNEL) \
|| (defined(UNIX) && (!defined(USE_SYSTEM) \
|| (defined(FEAT_GUI) && defined(FEAT_TERMINAL)))) \
|| defined(PROTO)
/*
* Parse "cmd" and put the white-separated parts in "argv".
* "argv" is an allocated array with "argc" entries and room for 4 more.
* Returns FAIL when out of memory.
*/
int
mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
{
int i;
char_u *p, *d;
int inquote;
/*
* Do this loop twice:
* 1: find number of arguments
* 2: separate them and build argv[]
*/
for (i = 1; i <= 2; ++i)
{
p = skipwhite(cmd);
inquote = FALSE;
*argc = 0;
while (*p != NUL)
{
if (i == 2)
(*argv)[*argc] = (char *)p;
++*argc;
d = p;
while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
{
if (p[0] == '"')
// quotes surrounding an argument and are dropped
inquote = !inquote;
else
{
if (rem_backslash(p))
{
// First pass: skip over "\ " and "\"".
// Second pass: Remove the backslash.
++p;
}
if (i == 2)
*d++ = *p;
}
++p;
}
if (*p == NUL)
{
if (i == 2)
*d++ = NUL;
break;
}
if (i == 2)
*d++ = NUL;
p = skipwhite(p + 1);
}
if (*argv == NULL)
{
if (use_shcf)
{
// Account for possible multiple args in p_shcf.
p = p_shcf;
for (;;)
{
p = skiptowhite(p);
if (*p == NUL)
break;
++*argc;
p = skipwhite(p);
}
}
*argv = ALLOC_MULT(char *, *argc + 4);
if (*argv == NULL) // out of memory
return FAIL;
}
}
return OK;
}
# if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
/*
* Build "argv[argc]" from the string "cmd".
* "argv[argc]" is set to NULL;
* Return FAIL when out of memory.
*/
int
build_argv_from_string(char_u *cmd, char ***argv, int *argc)
{
char_u *cmd_copy;
int i;
// Make a copy, parsing will modify "cmd".
cmd_copy = vim_strsave(cmd);
if (cmd_copy == NULL
|| mch_parse_cmd(cmd_copy, FALSE, argv, argc) == FAIL)
{
vim_free(cmd_copy);
return FAIL;
}
for (i = 0; i < *argc; i++)
(*argv)[i] = (char *)vim_strsave((char_u *)(*argv)[i]);
(*argv)[*argc] = NULL;
vim_free(cmd_copy);
return OK;
}
/*
* Build "argv[argc]" from the list "l".
* "argv[argc]" is set to NULL;
* Return FAIL when out of memory.
*/
int
build_argv_from_list(list_T *l, char ***argv, int *argc)
{
listitem_T *li;
char_u *s;
// Pass argv[] to mch_call_shell().
*argv = ALLOC_MULT(char *, l->lv_len + 1);
if (*argv == NULL)
return FAIL;
*argc = 0;
FOR_ALL_LIST_ITEMS(l, li)
{
s = tv_get_string_chk(&li->li_tv);
if (s == NULL)
{
int i;
for (i = 0; i < *argc; ++i)
VIM_CLEAR((*argv)[i]);
return FAIL;
}
(*argv)[*argc] = (char *)vim_strsave(s);
*argc += 1;
}
(*argv)[*argc] = NULL;
return OK;
}
# endif
#endif
/*
* Change the behavior of vterm.
* 0: As usual.
* 1: Windows 10 version 1809
* The bug causes unstable handling of ambiguous width character.
* 2: Windows 10 version 1903 & 1909
* Use the wrong result because each result is different.
* 3: Windows 10 insider preview (current latest logic)
*/
int
get_special_pty_type(void)
{
#ifdef MSWIN
return get_conpty_type();
#else
return 0;
#endif
}
|