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
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
|
/* 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.
*/
/*
* indent.c: Indentation related functions
*/
#include "vim.h"
#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
static int cin_iscase(char_u *s, int strict);
static int cin_isscopedecl(char_u *s);
/*
* Return TRUE if the string "line" starts with a word from 'cinwords'.
*/
int
cin_is_cinword(char_u *line)
{
char_u *cinw;
char_u *cinw_buf;
int cinw_len;
int retval = FALSE;
int len;
cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
cinw_buf = alloc(cinw_len);
if (cinw_buf != NULL)
{
line = skipwhite(line);
for (cinw = curbuf->b_p_cinw; *cinw; )
{
len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
if (STRNCMP(line, cinw_buf, len) == 0
&& (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
{
retval = TRUE;
break;
}
}
vim_free(cinw_buf);
}
return retval;
}
#endif
#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
static char_u *skip_string(char_u *p);
static pos_T *find_start_rawstring(int ind_maxcomment);
/*
* Find the start of a comment, not knowing if we are in a comment right now.
* Search starts at w_cursor.lnum and goes backwards.
* Return NULL when not inside a comment.
*/
static pos_T *
ind_find_start_comment(void) // XXX
{
return find_start_comment(curbuf->b_ind_maxcomment);
}
pos_T *
find_start_comment(int ind_maxcomment) // XXX
{
pos_T *pos;
char_u *line;
char_u *p;
int cur_maxcomment = ind_maxcomment;
for (;;)
{
pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
if (pos == NULL)
break;
// Check if the comment start we found is inside a string.
// If it is then restrict the search to below this line and try again.
line = ml_get(pos->lnum);
for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
p = skip_string(p);
if ((colnr_T)(p - line) <= pos->col)
break;
cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
if (cur_maxcomment <= 0)
{
pos = NULL;
break;
}
}
return pos;
}
/*
* Find the start of a comment or raw string, not knowing if we are in a
* comment or raw string right now.
* Search starts at w_cursor.lnum and goes backwards.
* If is_raw is given and returns start of raw_string, sets it to true.
* Return NULL when not inside a comment or raw string.
* "CORS" -> Comment Or Raw String
*/
static pos_T *
ind_find_start_CORS(linenr_T *is_raw) // XXX
{
static pos_T comment_pos_copy;
pos_T *comment_pos;
pos_T *rs_pos;
comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
if (comment_pos != NULL)
{
// Need to make a copy of the static pos in findmatchlimit(),
// calling find_start_rawstring() may change it.
comment_pos_copy = *comment_pos;
comment_pos = &comment_pos_copy;
}
rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
// If comment_pos is before rs_pos the raw string is inside the comment.
// If rs_pos is before comment_pos the comment is inside the raw string.
if (comment_pos == NULL || (rs_pos != NULL
&& LT_POS(*rs_pos, *comment_pos)))
{
if (is_raw != NULL && rs_pos != NULL)
*is_raw = rs_pos->lnum;
return rs_pos;
}
return comment_pos;
}
/*
* Find the start of a raw string, not knowing if we are in one right now.
* Search starts at w_cursor.lnum and goes backwards.
* Return NULL when not inside a raw string.
*/
static pos_T *
find_start_rawstring(int ind_maxcomment) // XXX
{
pos_T *pos;
char_u *line;
char_u *p;
int cur_maxcomment = ind_maxcomment;
for (;;)
{
pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
if (pos == NULL)
break;
// Check if the raw string start we found is inside a string.
// If it is then restrict the search to below this line and try again.
line = ml_get(pos->lnum);
for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
p = skip_string(p);
if ((colnr_T)(p - line) <= pos->col)
break;
cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
if (cur_maxcomment <= 0)
{
pos = NULL;
break;
}
}
return pos;
}
/*
* Skip to the end of a "string" and a 'c' character.
* If there is no string or character, return argument unmodified.
*/
static char_u *
skip_string(char_u *p)
{
int i;
// We loop, because strings may be concatenated: "date""time".
for ( ; ; ++p)
{
if (p[0] == '\'') // 'c' or '\n' or '\000'
{
if (!p[1]) // ' at end of line
break;
i = 2;
if (p[1] == '\\') // '\n' or '\000'
{
++i;
while (vim_isdigit(p[i - 1])) // '\000'
++i;
}
if (p[i] == '\'') // check for trailing '
{
p += i;
continue;
}
}
else if (p[0] == '"') // start of string
{
for (++p; p[0]; ++p)
{
if (p[0] == '\\' && p[1] != NUL)
++p;
else if (p[0] == '"') // end of string
break;
}
if (p[0] == '"')
continue; // continue for another string
}
else if (p[0] == 'R' && p[1] == '"')
{
// Raw string: R"[delim](...)[delim]"
char_u *delim = p + 2;
char_u *paren = vim_strchr(delim, '(');
if (paren != NULL)
{
size_t delim_len = paren - delim;
for (p += 3; *p; ++p)
if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
&& p[delim_len + 1] == '"')
{
p += delim_len + 1;
break;
}
if (p[0] == '"')
continue; // continue for another string
}
}
break; // no string found
}
if (!*p)
--p; // backup from NUL
return p;
}
#endif // FEAT_CINDENT || FEAT_SYN_HL
#if defined(FEAT_CINDENT) || defined(PROTO)
/*
* Return TRUE if C-indenting is on.
*/
int
cindent_on(void)
{
return (!p_paste && (curbuf->b_p_cin
# ifdef FEAT_EVAL
|| *curbuf->b_p_inde != NUL
# endif
));
}
// Find result cache for cpp_baseclass
typedef struct {
int found;
lpos_T lpos;
} cpp_baseclass_cache_T;
/*
* Functions for C-indenting.
* Most of this originally comes from Eric Fischer.
*/
/*
* Below "XXX" means that this function may unlock the current line.
*/
static int cin_isdefault(char_u *);
static int cin_ispreproc(char_u *);
static int cin_iscomment(char_u *);
static int cin_islinecomment(char_u *);
static int cin_isterminated(char_u *, int, int);
static int cin_iselse(char_u *);
static int cin_ends_in(char_u *, char_u *, char_u *);
static int cin_starts_with(char_u *s, char *word);
static pos_T *find_match_paren(int);
static pos_T *find_match_char(int c, int ind_maxparen);
static int find_last_paren(char_u *l, int start, int end);
static int find_match(int lookfor, linenr_T ourscope);
/*
* Skip over white space and C comments within the line.
* Also skip over Perl/shell comments if desired.
*/
static char_u *
cin_skipcomment(char_u *s)
{
while (*s)
{
char_u *prev_s = s;
s = skipwhite(s);
// Perl/shell # comment comment continues until eol. Require a space
// before # to avoid recognizing $#array.
if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
{
s += STRLEN(s);
break;
}
if (*s != '/')
break;
++s;
if (*s == '/') // slash-slash comment continues till eol
{
s += STRLEN(s);
break;
}
if (*s != '*')
break;
for (++s; *s; ++s) // skip slash-star comment
if (s[0] == '*' && s[1] == '/')
{
s += 2;
break;
}
}
return s;
}
/*
* Return TRUE if there is no code at *s. White space and comments are
* not considered code.
*/
static int
cin_nocode(char_u *s)
{
return *cin_skipcomment(s) == NUL;
}
/*
* Check previous lines for a "//" line comment, skipping over blank lines.
*/
static pos_T *
find_line_comment(void) // XXX
{
static pos_T pos;
char_u *line;
char_u *p;
pos = curwin->w_cursor;
while (--pos.lnum > 0)
{
line = ml_get(pos.lnum);
p = skipwhite(line);
if (cin_islinecomment(p))
{
pos.col = (int)(p - line);
return &pos;
}
if (*p != NUL)
break;
}
return NULL;
}
/*
* Return TRUE if "text" starts with "key:".
*/
static int
cin_has_js_key(char_u *text)
{
char_u *s = skipwhite(text);
int quote = -1;
if (*s == '\'' || *s == '"')
{
// can be 'key': or "key":
quote = *s;
++s;
}
if (!vim_isIDc(*s)) // need at least one ID character
return FALSE;
while (vim_isIDc(*s))
++s;
if (*s == quote)
++s;
s = cin_skipcomment(s);
// "::" is not a label, it's C++
return (*s == ':' && s[1] != ':');
}
/*
* Check if string matches "label:"; move to character after ':' if true.
* "*s" must point to the start of the label, if there is one.
*/
static int
cin_islabel_skip(char_u **s)
{
if (!vim_isIDc(**s)) // need at least one ID character
return FALSE;
while (vim_isIDc(**s))
(*s)++;
*s = cin_skipcomment(*s);
// "::" is not a label, it's C++
return (**s == ':' && *++*s != ':');
}
/*
* Recognize a label: "label:".
* Note: curwin->w_cursor must be where we are looking for the label.
*/
static int
cin_islabel(void) // XXX
{
char_u *s;
s = cin_skipcomment(ml_get_curline());
// Exclude "default" from labels, since it should be indented
// like a switch label. Same for C++ scope declarations.
if (cin_isdefault(s))
return FALSE;
if (cin_isscopedecl(s))
return FALSE;
if (cin_islabel_skip(&s))
{
// Only accept a label if the previous line is terminated or is a case
// label.
pos_T cursor_save;
pos_T *trypos;
char_u *line;
cursor_save = curwin->w_cursor;
while (curwin->w_cursor.lnum > 1)
{
--curwin->w_cursor.lnum;
// If we're in a comment or raw string now, skip to the start of
// it.
curwin->w_cursor.col = 0;
if ((trypos = ind_find_start_CORS(NULL)) != NULL) // XXX
curwin->w_cursor = *trypos;
line = ml_get_curline();
if (cin_ispreproc(line)) // ignore #defines, #if, etc.
continue;
if (*(line = cin_skipcomment(line)) == NUL)
continue;
curwin->w_cursor = cursor_save;
if (cin_isterminated(line, TRUE, FALSE)
|| cin_isscopedecl(line)
|| cin_iscase(line, TRUE)
|| (cin_islabel_skip(&line) && cin_nocode(line)))
return TRUE;
return FALSE;
}
curwin->w_cursor = cursor_save;
return TRUE; // label at start of file???
}
return FALSE;
}
/*
* Recognize structure initialization and enumerations:
* "[typedef] [static|public|protected|private] enum"
* "[typedef] [static|public|protected|private] = {"
*/
static int
cin_isinit(void)
{
char_u *s;
static char *skip[] = {"static", "public", "protected", "private"};
s = cin_skipcomment(ml_get_curline());
if (cin_starts_with(s, "typedef"))
s = cin_skipcomment(s + 7);
for (;;)
{
int i, l;
for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
{
l = (int)strlen(skip[i]);
if (cin_starts_with(s, skip[i]))
{
s = cin_skipcomment(s + l);
l = 0;
break;
}
}
if (l != 0)
break;
}
if (cin_starts_with(s, "enum"))
return TRUE;
if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
return TRUE;
return FALSE;
}
/*
* Recognize a switch label: "case .*:" or "default:".
*/
static int
cin_iscase(
char_u *s,
int strict) // Allow relaxed check of case statement for JS
{
s = cin_skipcomment(s);
if (cin_starts_with(s, "case"))
{
for (s += 4; *s; ++s)
{
s = cin_skipcomment(s);
if (*s == ':')
{
if (s[1] == ':') // skip over "::" for C++
++s;
else
return TRUE;
}
if (*s == '\'' && s[1] && s[2] == '\'')
s += 2; // skip over ':'
else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
return FALSE; // stop at comment
else if (*s == '"')
{
// JS etc.
if (strict)
return FALSE; // stop at string
else
return TRUE;
}
}
return FALSE;
}
if (cin_isdefault(s))
return TRUE;
return FALSE;
}
/*
* Recognize a "default" switch label.
*/
static int
cin_isdefault(char_u *s)
{
return (STRNCMP(s, "default", 7) == 0
&& *(s = cin_skipcomment(s + 7)) == ':'
&& s[1] != ':');
}
/*
* Recognize a "public/private/protected" scope declaration label.
*/
static int
cin_isscopedecl(char_u *s)
{
int i;
s = cin_skipcomment(s);
if (STRNCMP(s, "public", 6) == 0)
i = 6;
else if (STRNCMP(s, "protected", 9) == 0)
i = 9;
else if (STRNCMP(s, "private", 7) == 0)
i = 7;
else
return FALSE;
return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
}
// Maximum number of lines to search back for a "namespace" line.
#define FIND_NAMESPACE_LIM 20
/*
* Recognize a "namespace" scope declaration.
*/
static int
cin_is_cpp_namespace(char_u *s)
{
char_u *p;
int has_name = FALSE;
int has_name_start = FALSE;
s = cin_skipcomment(s);
if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
{
p = cin_skipcomment(skipwhite(s + 9));
while (*p != NUL)
{
if (VIM_ISWHITE(*p))
{
has_name = TRUE; // found end of a name
p = cin_skipcomment(skipwhite(p));
}
else if (*p == '{')
{
break;
}
else if (vim_iswordc(*p))
{
has_name_start = TRUE;
if (has_name)
return FALSE; // word character after skipping past name
++p;
}
else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
{
if (!has_name_start || has_name)
return FALSE;
// C++ 17 nested namespace
p += 3;
}
else
{
return FALSE;
}
}
return TRUE;
}
return FALSE;
}
/*
* Recognize a `extern "C"` or `extern "C++"` linkage specifications.
*/
static int
cin_is_cpp_extern_c(char_u *s)
{
char_u *p;
int has_string_literal = FALSE;
s = cin_skipcomment(s);
if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
{
p = cin_skipcomment(skipwhite(s + 6));
while (*p != NUL)
{
if (VIM_ISWHITE(*p))
{
p = cin_skipcomment(skipwhite(p));
}
else if (*p == '{')
{
break;
}
else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
{
if (has_string_literal)
return FALSE;
has_string_literal = TRUE;
p += 3;
}
else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
&& p[4] == '"')
{
if (has_string_literal)
return FALSE;
has_string_literal = TRUE;
p += 5;
}
else
{
return FALSE;
}
}
return has_string_literal ? TRUE : FALSE;
}
return FALSE;
}
/*
* Return a pointer to the first non-empty non-comment character after a ':'.
* Return NULL if not found.
* case 234: a = b;
* ^
*/
static char_u *
after_label(char_u *l)
{
for ( ; *l; ++l)
{
if (*l == ':')
{
if (l[1] == ':') // skip over "::" for C++
++l;
else if (!cin_iscase(l + 1, FALSE))
break;
}
else if (*l == '\'' && l[1] && l[2] == '\'')
l += 2; // skip over 'x'
}
if (*l == NUL)
return NULL;
l = cin_skipcomment(l + 1);
if (*l == NUL)
return NULL;
return l;
}
/*
* Get indent of line "lnum", skipping a label.
* Return 0 if there is nothing after the label.
*/
static int
get_indent_nolabel (linenr_T lnum) // XXX
{
char_u *l;
pos_T fp;
colnr_T col;
char_u *p;
l = ml_get(lnum);
p = after_label(l);
if (p == NULL)
return 0;
fp.col = (colnr_T)(p - l);
fp.lnum = lnum;
getvcol(curwin, &fp, &col, NULL, NULL);
return (int)col;
}
/*
* Find indent for line "lnum", ignoring any case or jump label.
* Also return a pointer to the text (after the label) in "pp".
* label: if (asdf && asdfasdf)
* ^
*/
static int
skip_label(linenr_T lnum, char_u **pp)
{
char_u *l;
int amount;
pos_T cursor_save;
cursor_save = curwin->w_cursor;
curwin->w_cursor.lnum = lnum;
l = ml_get_curline();
// XXX
if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
{
amount = get_indent_nolabel(lnum);
l = after_label(ml_get_curline());
if (l == NULL) // just in case
l = ml_get_curline();
}
else
{
amount = get_indent();
l = ml_get_curline();
}
*pp = l;
curwin->w_cursor = cursor_save;
return amount;
}
/*
* Return the indent of the first variable name after a type in a declaration.
* int a, indent of "a"
* static struct foo b, indent of "b"
* enum bla c, indent of "c"
* Returns zero when it doesn't look like a declaration.
*/
static int
cin_first_id_amount(void)
{
char_u *line, *p, *s;
int len;
pos_T fp;
colnr_T col;
line = ml_get_curline();
p = skipwhite(line);
len = (int)(skiptowhite(p) - p);
if (len == 6 && STRNCMP(p, "static", 6) == 0)
{
p = skipwhite(p + 6);
len = (int)(skiptowhite(p) - p);
}
if (len == 6 && STRNCMP(p, "struct", 6) == 0)
p = skipwhite(p + 6);
else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
p = skipwhite(p + 4);
else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
|| (len == 6 && STRNCMP(p, "signed", 6) == 0))
{
s = skipwhite(p + len);
if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
|| (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
|| (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
|| (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
p = s;
}
for (len = 0; vim_isIDc(p[len]); ++len)
;
if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
return 0;
p = skipwhite(p + len);
fp.lnum = curwin->w_cursor.lnum;
fp.col = (colnr_T)(p - line);
getvcol(curwin, &fp, &col, NULL, NULL);
return (int)col;
}
/*
* Return the indent of the first non-blank after an equal sign.
* char *foo = "here";
* Return zero if no (useful) equal sign found.
* Return -1 if the line above "lnum" ends in a backslash.
* foo = "asdf\
* asdf\
* here";
*/
static int
cin_get_equal_amount(linenr_T lnum)
{
char_u *line;
char_u *s;
colnr_T col;
pos_T fp;
if (lnum > 1)
{
line = ml_get(lnum - 1);
if (*line != NUL && line[STRLEN(line) - 1] == '\\')
return -1;
}
line = s = ml_get(lnum);
while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
{
if (cin_iscomment(s)) // ignore comments
s = cin_skipcomment(s);
else
++s;
}
if (*s != '=')
return 0;
s = skipwhite(s + 1);
if (cin_nocode(s))
return 0;
if (*s == '"') // nice alignment for continued strings
++s;
fp.lnum = lnum;
fp.col = (colnr_T)(s - line);
getvcol(curwin, &fp, &col, NULL, NULL);
return (int)col;
}
/*
* Recognize a preprocessor statement: Any line that starts with '#'.
*/
static int
cin_ispreproc(char_u *s)
{
if (*skipwhite(s) == '#')
return TRUE;
return FALSE;
}
/*
* Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
* continuation line of a preprocessor statement. Decrease "*lnump" to the
* start and return the line in "*pp".
* Put the amount of indent in "*amount".
*/
static int
cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
{
char_u *line = *pp;
linenr_T lnum = *lnump;
int retval = FALSE;
int candidate_amount = *amount;
if (*line != NUL && line[STRLEN(line) - 1] == '\\')
candidate_amount = get_indent_lnum(lnum);
for (;;)
{
if (cin_ispreproc(line))
{
retval = TRUE;
*lnump = lnum;
break;
}
if (lnum == 1)
break;
line = ml_get(--lnum);
if (*line == NUL || line[STRLEN(line) - 1] != '\\')
break;
}
if (lnum != *lnump)
*pp = ml_get(*lnump);
if (retval)
*amount = candidate_amount;
return retval;
}
/*
* Recognize the start of a C or C++ comment.
*/
static int
cin_iscomment(char_u *p)
{
return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
}
/*
* Recognize the start of a "//" comment.
*/
static int
cin_islinecomment(char_u *p)
{
return (p[0] == '/' && p[1] == '/');
}
/*
* Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
* '}'.
* Don't consider "} else" a terminated line.
* If a line begins with an "else", only consider it terminated if no unmatched
* opening braces follow (handle "else { foo();" correctly).
* Return the character terminating the line (ending char's have precedence if
* both apply in order to determine initializations).
*/
static int
cin_isterminated(
char_u *s,
int incl_open, // include '{' at the end as terminator
int incl_comma) // recognize a trailing comma
{
char_u found_start = 0;
unsigned n_open = 0;
int is_else = FALSE;
s = cin_skipcomment(s);
if (*s == '{' || (*s == '}' && !cin_iselse(s)))
found_start = *s;
if (!found_start)
is_else = cin_iselse(s);
while (*s)
{
// skip over comments, "" strings and 'c'haracters
s = skip_string(cin_skipcomment(s));
if (*s == '}' && n_open > 0)
--n_open;
if ((!is_else || n_open == 0)
&& (*s == ';' || *s == '}' || (incl_comma && *s == ','))
&& cin_nocode(s + 1))
return *s;
else if (*s == '{')
{
if (incl_open && cin_nocode(s + 1))
return *s;
else
++n_open;
}
if (*s)
s++;
}
return found_start;
}
/*
* Recognize the basic picture of a function declaration -- it needs to
* have an open paren somewhere and a close paren at the end of the line and
* no semicolons anywhere.
* When a line ends in a comma we continue looking in the next line.
* "sp" points to a string with the line. When looking at other lines it must
* be restored to the line. When it's NULL fetch lines here.
* "first_lnum" is where we start looking.
* "min_lnum" is the line before which we will not be looking.
*/
static int
cin_isfuncdecl(
char_u **sp,
linenr_T first_lnum,
linenr_T min_lnum)
{
char_u *s;
linenr_T lnum = first_lnum;
linenr_T save_lnum = curwin->w_cursor.lnum;
int retval = FALSE;
pos_T *trypos;
int just_started = TRUE;
if (sp == NULL)
s = ml_get(lnum);
else
s = *sp;
curwin->w_cursor.lnum = lnum;
if (find_last_paren(s, '(', ')')
&& (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
{
lnum = trypos->lnum;
if (lnum < min_lnum)
{
curwin->w_cursor.lnum = save_lnum;
return FALSE;
}
s = ml_get(lnum);
}
curwin->w_cursor.lnum = save_lnum;
// Ignore line starting with #.
if (cin_ispreproc(s))
return FALSE;
while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
{
if (cin_iscomment(s)) // ignore comments
s = cin_skipcomment(s);
else if (*s == ':')
{
if (*(s + 1) == ':')
s += 2;
else
// To avoid a mistake in the following situation:
// A::A(int a, int b)
// : a(0) // <--not a function decl
// , b(0)
// {...
return FALSE;
}
else
++s;
}
if (*s != '(')
return FALSE; // ';', ' or " before any () or no '('
while (*s && *s != ';' && *s != '\'' && *s != '"')
{
if (*s == ')' && cin_nocode(s + 1))
{
/*
* ')' at the end: may have found a match
* Check for he previous line not to end in a backslash:
* #if defined(x) && \
* defined(y)
*/
lnum = first_lnum - 1;
s = ml_get(lnum);
if (*s == NUL || s[STRLEN(s) - 1] != '\\')
retval = TRUE;
goto done;
}
if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
{
int comma = (*s == ',');
// ',' at the end: continue looking in the next line.
// At the end: check for ',' in the next line, for this style:
// func(arg1
// , arg2)
for (;;)
{
if (lnum >= curbuf->b_ml.ml_line_count)
break;
s = ml_get(++lnum);
if (!cin_ispreproc(s))
break;
}
if (lnum >= curbuf->b_ml.ml_line_count)
break;
// Require a comma at end of the line or a comma or ')' at the
// start of next line.
s = skipwhite(s);
if (!just_started && (!comma && *s != ',' && *s != ')'))
break;
just_started = FALSE;
}
else if (cin_iscomment(s)) // ignore comments
s = cin_skipcomment(s);
else
{
++s;
just_started = FALSE;
}
}
done:
if (lnum != first_lnum && sp != NULL)
*sp = ml_get(first_lnum);
return retval;
}
static int
cin_isif(char_u *p)
{
return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
}
static int
cin_iselse(
char_u *p)
{
if (*p == '}') // accept "} else"
p = cin_skipcomment(p + 1);
return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
}
static int
cin_isdo(char_u *p)
{
return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
}
/*
* Check if this is a "while" that should have a matching "do".
* We only accept a "while (condition) ;", with only white space between the
* ')' and ';'. The condition may be spread over several lines.
*/
static int
cin_iswhileofdo (char_u *p, linenr_T lnum) // XXX
{
pos_T cursor_save;
pos_T *trypos;
int retval = FALSE;
p = cin_skipcomment(p);
if (*p == '}') // accept "} while (cond);"
p = cin_skipcomment(p + 1);
if (cin_starts_with(p, "while"))
{
cursor_save = curwin->w_cursor;
curwin->w_cursor.lnum = lnum;
curwin->w_cursor.col = 0;
p = ml_get_curline();
while (*p && *p != 'w') // skip any '}', until the 'w' of the "while"
{
++p;
++curwin->w_cursor.col;
}
if ((trypos = findmatchlimit(NULL, 0, 0,
curbuf->b_ind_maxparen)) != NULL
&& *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
retval = TRUE;
curwin->w_cursor = cursor_save;
}
return retval;
}
/*
* Check whether in "p" there is an "if", "for" or "while" before "*poffset".
* Return 0 if there is none.
* Otherwise return !0 and update "*poffset" to point to the place where the
* string was found.
*/
static int
cin_is_if_for_while_before_offset(char_u *line, int *poffset)
{
int offset = *poffset;
if (offset-- < 2)
return 0;
while (offset > 2 && VIM_ISWHITE(line[offset]))
--offset;
offset -= 1;
if (!STRNCMP(line + offset, "if", 2))
goto probablyFound;
if (offset >= 1)
{
offset -= 1;
if (!STRNCMP(line + offset, "for", 3))
goto probablyFound;
if (offset >= 2)
{
offset -= 2;
if (!STRNCMP(line + offset, "while", 5))
goto probablyFound;
}
}
return 0;
probablyFound:
if (!offset || !vim_isIDc(line[offset - 1]))
{
*poffset = offset;
return 1;
}
return 0;
}
/*
* Return TRUE if we are at the end of a do-while.
* do
* nothing;
* while (foo
* && bar); <-- here
* Adjust the cursor to the line with "while".
*/
static int
cin_iswhileofdo_end(int terminated)
{
char_u *line;
char_u *p;
char_u *s;
pos_T *trypos;
int i;
if (terminated != ';') // there must be a ';' at the end
return FALSE;
p = line = ml_get_curline();
while (*p != NUL)
{
p = cin_skipcomment(p);
if (*p == ')')
{
s = skipwhite(p + 1);
if (*s == ';' && cin_nocode(s + 1))
{
// Found ");" at end of the line, now check there is "while"
// before the matching '('. XXX
i = (int)(p - line);
curwin->w_cursor.col = i;
trypos = find_match_paren(curbuf->b_ind_maxparen);
if (trypos != NULL)
{
s = cin_skipcomment(ml_get(trypos->lnum));
if (*s == '}') // accept "} while (cond);"
s = cin_skipcomment(s + 1);
if (cin_starts_with(s, "while"))
{
curwin->w_cursor.lnum = trypos->lnum;
return TRUE;
}
}
// Searching may have made "line" invalid, get it again.
line = ml_get_curline();
p = line + i;
}
}
if (*p != NUL)
++p;
}
return FALSE;
}
static int
cin_isbreak(char_u *p)
{
return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
}
/*
* Find the position of a C++ base-class declaration or
* constructor-initialization. eg:
*
* class MyClass :
* baseClass <-- here
* class MyClass : public baseClass,
* anotherBaseClass <-- here (should probably lineup ??)
* MyClass::MyClass(...) :
* baseClass(...) <-- here (constructor-initialization)
*
* This is a lot of guessing. Watch out for "cond ? func() : foo".
*/
static int
cin_is_cpp_baseclass(
cpp_baseclass_cache_T *cached) // input and output
{
lpos_T *pos = &cached->lpos; // find position
char_u *s;
int class_or_struct, lookfor_ctor_init, cpp_base_class;
linenr_T lnum = curwin->w_cursor.lnum;
char_u *line = ml_get_curline();
if (pos->lnum <= lnum)
return cached->found; // Use the cached result
pos->col = 0;
s = skipwhite(line);
if (*s == '#') // skip #define FOO x ? (x) : x
return FALSE;
s = cin_skipcomment(s);
if (*s == NUL)
return FALSE;
cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
// Search for a line starting with '#', empty, ending in ';' or containing
// '{' or '}' and start below it. This handles the following situations:
// a = cond ?
// func() :
// asdf;
// func::foo()
// : something
// {}
// Foo::Foo (int one, int two)
// : something(4),
// somethingelse(3)
// {}
while (lnum > 1)
{
line = ml_get(lnum - 1);
s = skipwhite(line);
if (*s == '#' || *s == NUL)
break;
while (*s != NUL)
{
s = cin_skipcomment(s);
if (*s == '{' || *s == '}'
|| (*s == ';' && cin_nocode(s + 1)))
break;
if (*s != NUL)
++s;
}
if (*s != NUL)
break;
--lnum;
}
pos->lnum = lnum;
line = ml_get(lnum);
s = line;
for (;;)
{
if (*s == NUL)
{
if (lnum == curwin->w_cursor.lnum)
break;
// Continue in the cursor line.
line = ml_get(++lnum);
s = line;
}
if (s == line)
{
// don't recognize "case (foo):" as a baseclass
if (cin_iscase(s, FALSE))
break;
s = cin_skipcomment(line);
if (*s == NUL)
continue;
}
if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
s = skip_string(s) + 1;
else if (s[0] == ':')
{
if (s[1] == ':')
{
// skip double colon. It can't be a constructor
// initialization any more
lookfor_ctor_init = FALSE;
s = cin_skipcomment(s + 2);
}
else if (lookfor_ctor_init || class_or_struct)
{
// we have something found, that looks like the start of
// cpp-base-class-declaration or constructor-initialization
cpp_base_class = TRUE;
lookfor_ctor_init = class_or_struct = FALSE;
pos->col = 0;
s = cin_skipcomment(s + 1);
}
else
s = cin_skipcomment(s + 1);
}
else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
|| (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
{
class_or_struct = TRUE;
lookfor_ctor_init = FALSE;
if (*s == 'c')
s = cin_skipcomment(s + 5);
else
s = cin_skipcomment(s + 6);
}
else
{
if (s[0] == '{' || s[0] == '}' || s[0] == ';')
{
cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
}
else if (s[0] == ')')
{
// Constructor-initialization is assumed if we come across
// something like "):"
class_or_struct = FALSE;
lookfor_ctor_init = TRUE;
}
else if (s[0] == '?')
{
// Avoid seeing '() :' after '?' as constructor init.
return FALSE;
}
else if (!vim_isIDc(s[0]))
{
// if it is not an identifier, we are wrong
class_or_struct = FALSE;
lookfor_ctor_init = FALSE;
}
else if (pos->col == 0)
{
// it can't be a constructor-initialization any more
lookfor_ctor_init = FALSE;
// the first statement starts here: lineup with this one...
if (cpp_base_class)
pos->col = (colnr_T)(s - line);
}
// When the line ends in a comma don't align with it.
if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
pos->col = 0;
s = cin_skipcomment(s + 1);
}
}
cached->found = cpp_base_class;
if (cpp_base_class)
pos->lnum = lnum;
return cpp_base_class;
}
static int
get_baseclass_amount(int col)
{
int amount;
colnr_T vcol;
pos_T *trypos;
if (col == 0)
{
amount = get_indent();
if (find_last_paren(ml_get_curline(), '(', ')')
&& (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
amount = get_indent_lnum(trypos->lnum); // XXX
if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
amount += curbuf->b_ind_cpp_baseclass;
}
else
{
curwin->w_cursor.col = col;
getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
amount = (int)vcol;
}
if (amount < curbuf->b_ind_cpp_baseclass)
amount = curbuf->b_ind_cpp_baseclass;
return amount;
}
/*
* Return TRUE if string "s" ends with the string "find", possibly followed by
* white space and comments. Skip strings and comments.
* Ignore "ignore" after "find" if it's not NULL.
*/
static int
cin_ends_in(char_u *s, char_u *find, char_u *ignore)
{
char_u *p = s;
char_u *r;
int len = (int)STRLEN(find);
while (*p != NUL)
{
p = cin_skipcomment(p);
if (STRNCMP(p, find, len) == 0)
{
r = skipwhite(p + len);
if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
r = skipwhite(r + STRLEN(ignore));
if (cin_nocode(r))
return TRUE;
}
if (*p != NUL)
++p;
}
return FALSE;
}
/*
* Return TRUE when "s" starts with "word" and then a non-ID character.
*/
static int
cin_starts_with(char_u *s, char *word)
{
int l = (int)STRLEN(word);
return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
}
/*
* Skip strings, chars and comments until at or past "trypos".
* Return the column found.
*/
static int
cin_skip2pos(pos_T *trypos)
{
char_u *line;
char_u *p;
char_u *new_p;
p = line = ml_get(trypos->lnum);
while (*p && (colnr_T)(p - line) < trypos->col)
{
if (cin_iscomment(p))
p = cin_skipcomment(p);
else
{
new_p = skip_string(p);
if (new_p == p)
++p;
else
p = new_p;
}
}
return (int)(p - line);
}
/*
* Find the '{' at the start of the block we are in.
* Return NULL if no match found.
* Ignore a '{' that is in a comment, makes indenting the next three lines
* work.
*/
/* foo() */
/* { */
/* } */
static pos_T *
find_start_brace(void) // XXX
{
pos_T cursor_save;
pos_T *trypos;
pos_T *pos;
static pos_T pos_copy;
cursor_save = curwin->w_cursor;
while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
{
pos_copy = *trypos; // copy pos_T, next findmatch will change it
trypos = &pos_copy;
curwin->w_cursor = *trypos;
pos = NULL;
// ignore the { if it's in a // or / * * / comment
if ((colnr_T)cin_skip2pos(trypos) == trypos->col
&& (pos = ind_find_start_CORS(NULL)) == NULL) // XXX
break;
if (pos != NULL)
curwin->w_cursor.lnum = pos->lnum;
}
curwin->w_cursor = cursor_save;
return trypos;
}
/*
* Find the matching '(', ignoring it if it is in a comment.
* Return NULL if no match found.
*/
static pos_T *
find_match_paren(int ind_maxparen) // XXX
{
return find_match_char('(', ind_maxparen);
}
static pos_T *
find_match_char(int c, int ind_maxparen) // XXX
{
pos_T cursor_save;
pos_T *trypos;
static pos_T pos_copy;
int ind_maxp_wk;
cursor_save = curwin->w_cursor;
ind_maxp_wk = ind_maxparen;
retry:
if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
{
// check if the ( is in a // comment
if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
{
ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
if (ind_maxp_wk > 0)
{
curwin->w_cursor = *trypos;
curwin->w_cursor.col = 0; // XXX
goto retry;
}
trypos = NULL;
}
else
{
pos_T *trypos_wk;
pos_copy = *trypos; // copy trypos, findmatch will change it
trypos = &pos_copy;
curwin->w_cursor = *trypos;
if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) // XXX
{
ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
- trypos_wk->lnum);
if (ind_maxp_wk > 0)
{
curwin->w_cursor = *trypos_wk;
goto retry;
}
trypos = NULL;
}
}
}
curwin->w_cursor = cursor_save;
return trypos;
}
/*
* Find the matching '(', ignoring it if it is in a comment or before an
* unmatched {.
* Return NULL if no match found.
*/
static pos_T *
find_match_paren_after_brace (int ind_maxparen) // XXX
{
pos_T *trypos = find_match_paren(ind_maxparen);
if (trypos != NULL)
{
pos_T *tryposBrace = find_start_brace();
// If both an unmatched '(' and '{' is found. Ignore the '('
// position if the '{' is further down.
if (tryposBrace != NULL
&& (trypos->lnum != tryposBrace->lnum
? trypos->lnum < tryposBrace->lnum
: trypos->col < tryposBrace->col))
trypos = NULL;
}
return trypos;
}
/*
* Return ind_maxparen corrected for the difference in line number between the
* cursor position and "startpos". This makes sure that searching for a
* matching paren above the cursor line doesn't find a match because of
* looking a few lines further.
*/
static int
corr_ind_maxparen(pos_T *startpos)
{
long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
if (n > 0 && n < curbuf->b_ind_maxparen / 2)
return curbuf->b_ind_maxparen - (int)n;
return curbuf->b_ind_maxparen;
}
/*
* Set w_cursor.col to the column number of the last unmatched ')' or '{' in
* line "l". "l" must point to the start of the line.
*/
static int
find_last_paren(char_u *l, int start, int end)
{
int i;
int retval = FALSE;
int open_count = 0;
curwin->w_cursor.col = 0; // default is start of line
for (i = 0; l[i] != NUL; i++)
{
i = (int)(cin_skipcomment(l + i) - l); // ignore parens in comments
i = (int)(skip_string(l + i) - l); // ignore parens in quotes
if (l[i] == start)
++open_count;
else if (l[i] == end)
{
if (open_count > 0)
--open_count;
else
{
curwin->w_cursor.col = i;
retval = TRUE;
}
}
}
return retval;
}
/*
* Parse 'cinoptions' and set the values in "curbuf".
* Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
*/
void
parse_cino(buf_T *buf)
{
char_u *p;
char_u *l;
char_u *digits;
int n;
int divider;
int fraction = 0;
int sw = (int)get_sw_value(buf);
// Set the default values.
// Spaces from a block's opening brace the prevailing indent for that
// block should be.
buf->b_ind_level = sw;
// Spaces from the edge of the line an open brace that's at the end of a
// line is imagined to be.
buf->b_ind_open_imag = 0;
// Spaces from the prevailing indent for a line that is not preceded by
// an opening brace.
buf->b_ind_no_brace = 0;
// Column where the first { of a function should be located }.
buf->b_ind_first_open = 0;
// Spaces from the prevailing indent a leftmost open brace should be
// located.
buf->b_ind_open_extra = 0;
// Spaces from the matching open brace (real location for one at the left
// edge; imaginary location from one that ends a line) the matching close
// brace should be located.
buf->b_ind_close_extra = 0;
// Spaces from the edge of the line an open brace sitting in the leftmost
// column is imagined to be.
buf->b_ind_open_left_imag = 0;
// Spaces jump labels should be shifted to the left if N is non-negative,
// otherwise the jump label will be put to column 1.
buf->b_ind_jump_label = -1;
// Spaces from the switch() indent a "case xx" label should be located.
buf->b_ind_case = sw;
// Spaces from the "case xx:" code after a switch() should be located.
buf->b_ind_case_code = sw;
// Lineup break at end of case in switch() with case label.
buf->b_ind_case_break = 0;
// Spaces from the class declaration indent a scope declaration label
// should be located.
buf->b_ind_scopedecl = sw;
// Spaces from the scope declaration label code should be located.
buf->b_ind_scopedecl_code = sw;
// Amount K&R-style parameters should be indented.
buf->b_ind_param = sw;
// Amount a function type spec should be indented.
buf->b_ind_func_type = sw;
// Amount a cpp base class declaration or constructor initialization
// should be indented.
buf->b_ind_cpp_baseclass = sw;
// additional spaces beyond the prevailing indent a continuation line
// should be located.
buf->b_ind_continuation = sw;
// Spaces from the indent of the line with an unclosed parentheses.
buf->b_ind_unclosed = sw * 2;
// Spaces from the indent of the line with an unclosed parentheses, which
// itself is also unclosed.
buf->b_ind_unclosed2 = sw;
// Suppress ignoring spaces from the indent of a line starting with an
// unclosed parentheses.
buf->b_ind_unclosed_noignore = 0;
// If the opening paren is the last nonwhite character on the line, and
// b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
// context (for very long lines).
buf->b_ind_unclosed_wrapped = 0;
// Suppress ignoring white space when lining up with the character after
// an unclosed parentheses.
buf->b_ind_unclosed_whiteok = 0;
// Indent a closing parentheses under the line start of the matching
// opening parentheses.
buf->b_ind_matching_paren = 0;
// Indent a closing parentheses under the previous line.
buf->b_ind_paren_prev = 0;
// Extra indent for comments.
buf->b_ind_comment = 0;
// Spaces from the comment opener when there is nothing after it.
buf->b_ind_in_comment = 3;
// Boolean: if non-zero, use b_ind_in_comment even if there is something
// after the comment opener.
buf->b_ind_in_comment2 = 0;
// Max lines to search for an open paren.
buf->b_ind_maxparen = 20;
// Max lines to search for an open comment.
buf->b_ind_maxcomment = 70;
// Handle braces for java code.
buf->b_ind_java = 0;
// Not to confuse JS object properties with labels.
buf->b_ind_js = 0;
// Handle blocked cases correctly.
buf->b_ind_keep_case_label = 0;
// Handle C++ namespace.
buf->b_ind_cpp_namespace = 0;
// Handle continuation lines containing conditions of if(), for() and
// while().
buf->b_ind_if_for_while = 0;
// indentation for # comments
buf->b_ind_hash_comment = 0;
// Handle C++ extern "C" or "C++"
buf->b_ind_cpp_extern_c = 0;
for (p = buf->b_p_cino; *p; )
{
l = p++;
if (*p == '-')
++p;
digits = p; // remember where the digits start
n = getdigits(&p);
divider = 0;
if (*p == '.') // ".5s" means a fraction
{
fraction = atol((char *)++p);
while (VIM_ISDIGIT(*p))
{
++p;
if (divider)
divider *= 10;
else
divider = 10;
}
}
if (*p == 's') // "2s" means two times 'shiftwidth'
{
if (p == digits)
n = sw; // just "s" is one 'shiftwidth'
else
{
n *= sw;
if (divider)
n += (sw * fraction + divider / 2) / divider;
}
++p;
}
if (l[1] == '-')
n = -n;
// When adding an entry here, also update the default 'cinoptions' in
// doc/indent.txt, and add explanation for it!
switch (*l)
{
case '>': buf->b_ind_level = n; break;
case 'e': buf->b_ind_open_imag = n; break;
case 'n': buf->b_ind_no_brace = n; break;
case 'f': buf->b_ind_first_open = n; break;
case '{': buf->b_ind_open_extra = n; break;
case '}': buf->b_ind_close_extra = n; break;
case '^': buf->b_ind_open_left_imag = n; break;
case 'L': buf->b_ind_jump_label = n; break;
case ':': buf->b_ind_case = n; break;
case '=': buf->b_ind_case_code = n; break;
case 'b': buf->b_ind_case_break = n; break;
case 'p': buf->b_ind_param = n; break;
case 't': buf->b_ind_func_type = n; break;
case '/': buf->b_ind_comment = n; break;
case 'c': buf->b_ind_in_comment = n; break;
case 'C': buf->b_ind_in_comment2 = n; break;
case 'i': buf->b_ind_cpp_baseclass = n; break;
case '+': buf->b_ind_continuation = n; break;
case '(': buf->b_ind_unclosed = n; break;
case 'u': buf->b_ind_unclosed2 = n; break;
case 'U': buf->b_ind_unclosed_noignore = n; break;
case 'W': buf->b_ind_unclosed_wrapped = n; break;
case 'w': buf->b_ind_unclosed_whiteok = n; break;
case 'm': buf->b_ind_matching_paren = n; break;
case 'M': buf->b_ind_paren_prev = n; break;
case ')': buf->b_ind_maxparen = n; break;
case '*': buf->b_ind_maxcomment = n; break;
case 'g': buf->b_ind_scopedecl = n; break;
case 'h': buf->b_ind_scopedecl_code = n; break;
case 'j': buf->b_ind_java = n; break;
case 'J': buf->b_ind_js = n; break;
case 'l': buf->b_ind_keep_case_label = n; break;
case '#': buf->b_ind_hash_comment = n; break;
case 'N': buf->b_ind_cpp_namespace = n; break;
case 'k': buf->b_ind_if_for_while = n; break;
case 'E': buf->b_ind_cpp_extern_c = n; break;
}
if (*p == ',')
++p;
}
}
/*
* Return the desired indent for C code.
* Return -1 if the indent should be left alone (inside a raw string).
*/
int
get_c_indent(void)
{
pos_T cur_curpos;
int amount;
int scope_amount;
int cur_amount = MAXCOL;
colnr_T col;
char_u *theline;
char_u *linecopy;
pos_T *trypos;
pos_T *comment_pos;
pos_T *tryposBrace = NULL;
pos_T tryposCopy;
pos_T our_paren_pos;
char_u *start;
int start_brace;
#define BRACE_IN_COL0 1 // '{' is in column 0
#define BRACE_AT_START 2 // '{' is at start of line
#define BRACE_AT_END 3 // '{' is at end of line
linenr_T ourscope;
char_u *l;
char_u *look;
char_u terminated;
int lookfor;
#define LOOKFOR_INITIAL 0
#define LOOKFOR_IF 1
#define LOOKFOR_DO 2
#define LOOKFOR_CASE 3
#define LOOKFOR_ANY 4
#define LOOKFOR_TERM 5
#define LOOKFOR_UNTERM 6
#define LOOKFOR_SCOPEDECL 7
#define LOOKFOR_NOBREAK 8
#define LOOKFOR_CPP_BASECLASS 9
#define LOOKFOR_ENUM_OR_INIT 10
#define LOOKFOR_JS_KEY 11
#define LOOKFOR_COMMA 12
int whilelevel;
linenr_T lnum;
int n;
int iscase;
int lookfor_break;
int lookfor_cpp_namespace = FALSE;
int cont_amount = 0; // amount for continuation line
int original_line_islabel;
int added_to_amount = 0;
int js_cur_has_key = 0;
linenr_T raw_string_start = 0;
cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
// make a copy, value is changed below
int ind_continuation = curbuf->b_ind_continuation;
// remember where the cursor was when we started
cur_curpos = curwin->w_cursor;
// if we are at line 1 zero indent is fine, right?
if (cur_curpos.lnum == 1)
return 0;
// Get a copy of the current contents of the line.
// This is required, because only the most recent line obtained with
// ml_get is valid!
linecopy = vim_strsave(ml_get(cur_curpos.lnum));
if (linecopy == NULL)
return 0;
// In insert mode and the cursor is on a ')' truncate the line at the
// cursor position. We don't want to line up with the matching '(' when
// inserting new stuff.
// For unknown reasons the cursor might be past the end of the line, thus
// check for that.
if ((State & INSERT)
&& curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
&& linecopy[curwin->w_cursor.col] == ')')
linecopy[curwin->w_cursor.col] = NUL;
theline = skipwhite(linecopy);
// move the cursor to the start of the line
curwin->w_cursor.col = 0;
original_line_islabel = cin_islabel(); // XXX
// If we are inside a raw string don't change the indent.
// Ignore a raw string inside a comment.
comment_pos = ind_find_start_comment();
if (comment_pos != NULL)
{
// findmatchlimit() static pos is overwritten, make a copy
tryposCopy = *comment_pos;
comment_pos = &tryposCopy;
}
trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
if (trypos != NULL && (comment_pos == NULL
|| LT_POS(*trypos, *comment_pos)))
{
amount = -1;
goto laterend;
}
// #defines and so on always go at the left when included in 'cinkeys'.
if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
{
amount = curbuf->b_ind_hash_comment;
goto theend;
}
// Is it a non-case label? Then that goes at the left margin too unless:
// - JS flag is set.
// - 'L' item has a positive value.
if (original_line_islabel && !curbuf->b_ind_js
&& curbuf->b_ind_jump_label < 0)
{
amount = 0;
goto theend;
}
// If we're inside a "//" comment and there is a "//" comment in a
// previous line, lineup with that one.
if (cin_islinecomment(theline)
&& (trypos = find_line_comment()) != NULL) // XXX
{
// find how indented the line beginning the comment is
getvcol(curwin, trypos, &col, NULL, NULL);
amount = col;
goto theend;
}
// If we're inside a comment and not looking at the start of the
// comment, try using the 'comments' option.
if (!cin_iscomment(theline) && comment_pos != NULL) // XXX
{
int lead_start_len = 2;
int lead_middle_len = 1;
char_u lead_start[COM_MAX_LEN]; // start-comment string
char_u lead_middle[COM_MAX_LEN]; // middle-comment string
char_u lead_end[COM_MAX_LEN]; // end-comment string
char_u *p;
int start_align = 0;
int start_off = 0;
int done = FALSE;
// find how indented the line beginning the comment is
getvcol(curwin, comment_pos, &col, NULL, NULL);
amount = col;
*lead_start = NUL;
*lead_middle = NUL;
p = curbuf->b_p_com;
while (*p != NUL)
{
int align = 0;
int off = 0;
int what = 0;
while (*p != NUL && *p != ':')
{
if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
what = *p++;
else if (*p == COM_LEFT || *p == COM_RIGHT)
align = *p++;
else if (VIM_ISDIGIT(*p) || *p == '-')
off = getdigits(&p);
else
++p;
}
if (*p == ':')
++p;
(void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
if (what == COM_START)
{
STRCPY(lead_start, lead_end);
lead_start_len = (int)STRLEN(lead_start);
start_off = off;
start_align = align;
}
else if (what == COM_MIDDLE)
{
STRCPY(lead_middle, lead_end);
lead_middle_len = (int)STRLEN(lead_middle);
}
else if (what == COM_END)
{
// If our line starts with the middle comment string, line it
// up with the comment opener per the 'comments' option.
if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
&& STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
{
done = TRUE;
if (curwin->w_cursor.lnum > 1)
{
// If the start comment string matches in the previous
// line, use the indent of that line plus offset. If
// the middle comment string matches in the previous
// line, use the indent of that line. XXX
look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
if (STRNCMP(look, lead_start, lead_start_len) == 0)
amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
else if (STRNCMP(look, lead_middle,
lead_middle_len) == 0)
{
amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
break;
}
// If the start comment string doesn't match with the
// start of the comment, skip this entry. XXX
else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
lead_start, lead_start_len) != 0)
continue;
}
if (start_off != 0)
amount += start_off;
else if (start_align == COM_RIGHT)
amount += vim_strsize(lead_start)
- vim_strsize(lead_middle);
break;
}
// If our line starts with the end comment string, line it up
// with the middle comment
if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
&& STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
{
amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
// XXX
if (off != 0)
amount += off;
else if (align == COM_RIGHT)
amount += vim_strsize(lead_start)
- vim_strsize(lead_middle);
done = TRUE;
break;
}
}
}
// If our line starts with an asterisk, line up with the
// asterisk in the comment opener; otherwise, line up
// with the first character of the comment text.
if (done)
;
else if (theline[0] == '*')
amount += 1;
else
{
// If we are more than one line away from the comment opener, take
// the indent of the previous non-empty line. If 'cino' has "CO"
// and we are just below the comment opener and there are any
// white characters after it line up with the text after it;
// otherwise, add the amount specified by "c" in 'cino'
amount = -1;
for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
{
if (linewhite(lnum)) // skip blank lines
continue;
amount = get_indent_lnum(lnum); // XXX
break;
}
if (amount == -1) // use the comment opener
{
if (!curbuf->b_ind_in_comment2)
{
start = ml_get(comment_pos->lnum);
look = start + comment_pos->col + 2; // skip / and *
if (*look != NUL) // if something after it
comment_pos->col = (colnr_T)(skipwhite(look) - start);
}
getvcol(curwin, comment_pos, &col, NULL, NULL);
amount = col;
if (curbuf->b_ind_in_comment2 || *look == NUL)
amount += curbuf->b_ind_in_comment;
}
}
goto theend;
}
// Are we looking at a ']' that has a match?
if (*skipwhite(theline) == ']'
&& (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
{
// align with the line containing the '['.
amount = get_indent_lnum(trypos->lnum);
goto theend;
}
// Are we inside parentheses or braces? XXX
if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
&& curbuf->b_ind_java == 0)
|| (tryposBrace = find_start_brace()) != NULL
|| trypos != NULL)
{
if (trypos != NULL && tryposBrace != NULL)
{
// Both an unmatched '(' and '{' is found. Use the one which is
// closer to the current cursor position, set the other to NULL.
if (trypos->lnum != tryposBrace->lnum
? trypos->lnum < tryposBrace->lnum
: trypos->col < tryposBrace->col)
trypos = NULL;
else
tryposBrace = NULL;
}
if (trypos != NULL)
{
// If the matching paren is more than one line away, use the indent of
// a previous non-empty line that matches the same paren.
if (theline[0] == ')' && curbuf->b_ind_paren_prev)
{
// Line up with the start of the matching paren line.
amount = get_indent_lnum(curwin->w_cursor.lnum - 1); // XXX
}
else
{
amount = -1;
our_paren_pos = *trypos;
for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
{
l = skipwhite(ml_get(lnum));
if (cin_nocode(l)) // skip comment lines
continue;
if (cin_ispreproc_cont(&l, &lnum, &amount))
continue; // ignore #define, #if, etc.
curwin->w_cursor.lnum = lnum;
// Skip a comment or raw string. XXX
if ((trypos = ind_find_start_CORS(NULL)) != NULL)
{
lnum = trypos->lnum + 1;
continue;
}
// XXX
if ((trypos = find_match_paren(
corr_ind_maxparen(&cur_curpos))) != NULL
&& trypos->lnum == our_paren_pos.lnum
&& trypos->col == our_paren_pos.col)
{
amount = get_indent_lnum(lnum); // XXX
if (theline[0] == ')')
{
if (our_paren_pos.lnum != lnum
&& cur_amount > amount)
cur_amount = amount;
amount = -1;
}
break;
}
}
}
// Line up with line where the matching paren is. XXX
// If the line starts with a '(' or the indent for unclosed
// parentheses is zero, line up with the unclosed parentheses.
if (amount == -1)
{
int ignore_paren_col = 0;
int is_if_for_while = 0;
if (curbuf->b_ind_if_for_while)
{
// Look for the outermost opening parenthesis on this line
// and check whether it belongs to an "if", "for" or "while".
pos_T cursor_save = curwin->w_cursor;
pos_T outermost;
char_u *line;
trypos = &our_paren_pos;
do {
outermost = *trypos;
curwin->w_cursor.lnum = outermost.lnum;
curwin->w_cursor.col = outermost.col;
trypos = find_match_paren(curbuf->b_ind_maxparen);
} while (trypos && trypos->lnum == outermost.lnum);
curwin->w_cursor = cursor_save;
line = ml_get(outermost.lnum);
is_if_for_while =
cin_is_if_for_while_before_offset(line, &outermost.col);
}
amount = skip_label(our_paren_pos.lnum, &look);
look = skipwhite(look);
if (*look == '(')
{
linenr_T save_lnum = curwin->w_cursor.lnum;
char_u *line;
int look_col;
// Ignore a '(' in front of the line that has a match before
// our matching '('.
curwin->w_cursor.lnum = our_paren_pos.lnum;
line = ml_get_curline();
look_col = (int)(look - line);
curwin->w_cursor.col = look_col + 1;
if ((trypos = findmatchlimit(NULL, ')', 0,
curbuf->b_ind_maxparen))
!= NULL
&& trypos->lnum == our_paren_pos.lnum
&& trypos->col < our_paren_pos.col)
ignore_paren_col = trypos->col + 1;
curwin->w_cursor.lnum = save_lnum;
look = ml_get(our_paren_pos.lnum) + look_col;
}
if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
&& is_if_for_while == 0)
|| (!curbuf->b_ind_unclosed_noignore && *look == '('
&& ignore_paren_col == 0))
{
// If we're looking at a close paren, line up right there;
// otherwise, line up with the next (non-white) character.
// When b_ind_unclosed_wrapped is set and the matching paren is
// the last nonwhite character of the line, use either the
// indent of the current line or the indentation of the next
// outer paren and add b_ind_unclosed_wrapped (for very long
// lines).
if (theline[0] != ')')
{
cur_amount = MAXCOL;
l = ml_get(our_paren_pos.lnum);
if (curbuf->b_ind_unclosed_wrapped
&& cin_ends_in(l, (char_u *)"(", NULL))
{
// look for opening unmatched paren, indent one level
// for each additional level
n = 1;
for (col = 0; col < our_paren_pos.col; ++col)
{
switch (l[col])
{
case '(':
case '{': ++n;
break;
case ')':
case '}': if (n > 1)
--n;
break;
}
}
our_paren_pos.col = 0;
amount += n * curbuf->b_ind_unclosed_wrapped;
}
else if (curbuf->b_ind_unclosed_whiteok)
our_paren_pos.col++;
else
{
col = our_paren_pos.col + 1;
while (VIM_ISWHITE(l[col]))
col++;
if (l[col] != NUL) // In case of trailing space
our_paren_pos.col = col;
else
our_paren_pos.col++;
}
}
// Find how indented the paren is, or the character after it
// if we did the above "if".
if (our_paren_pos.col > 0)
{
getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
if (cur_amount > (int)col)
cur_amount = col;
}
}
if (theline[0] == ')' && curbuf->b_ind_matching_paren)
{
// Line up with the start of the matching paren line.
}
else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
|| (!curbuf->b_ind_unclosed_noignore
&& *look == '(' && ignore_paren_col == 0))
{
if (cur_amount != MAXCOL)
amount = cur_amount;
}
else
{
// Add b_ind_unclosed2 for each '(' before our matching one,
// but ignore (void) before the line (ignore_paren_col).
col = our_paren_pos.col;
while ((int)our_paren_pos.col > ignore_paren_col)
{
--our_paren_pos.col;
switch (*ml_get_pos(&our_paren_pos))
{
case '(': amount += curbuf->b_ind_unclosed2;
col = our_paren_pos.col;
break;
case ')': amount -= curbuf->b_ind_unclosed2;
col = MAXCOL;
break;
}
}
// Use b_ind_unclosed once, when the first '(' is not inside
// braces
if (col == MAXCOL)
amount += curbuf->b_ind_unclosed;
else
{
curwin->w_cursor.lnum = our_paren_pos.lnum;
curwin->w_cursor.col = col;
if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
!= NULL)
amount += curbuf->b_ind_unclosed2;
else
{
if (is_if_for_while)
amount += curbuf->b_ind_if_for_while;
else
amount += curbuf->b_ind_unclosed;
}
}
// For a line starting with ')' use the minimum of the two
// positions, to avoid giving it more indent than the previous
// lines:
// func_long_name( if (x
// arg && yy
// ) ^ not here ) ^ not here
if (cur_amount < amount)
amount = cur_amount;
}
}
// add extra indent for a comment
if (cin_iscomment(theline))
amount += curbuf->b_ind_comment;
}
else
{
// We are inside braces, there is a { before this line at the position
// stored in tryposBrace.
// Make a copy of tryposBrace, it may point to pos_copy inside
// find_start_brace(), which may be changed somewhere.
tryposCopy = *tryposBrace;
tryposBrace = &tryposCopy;
trypos = tryposBrace;
ourscope = trypos->lnum;
start = ml_get(ourscope);
// Now figure out how indented the line is in general.
// If the brace was at the start of the line, we use that;
// otherwise, check out the indentation of the line as
// a whole and then add the "imaginary indent" to that.
look = skipwhite(start);
if (*look == '{')
{
getvcol(curwin, trypos, &col, NULL, NULL);
amount = col;
if (*start == '{')
start_brace = BRACE_IN_COL0;
else
start_brace = BRACE_AT_START;
}
else
{
// That opening brace might have been on a continuation
// line. if so, find the start of the line.
curwin->w_cursor.lnum = ourscope;
// Position the cursor over the rightmost paren, so that
// matching it will take us back to the start of the line.
lnum = ourscope;
if (find_last_paren(start, '(', ')')
&& (trypos = find_match_paren(curbuf->b_ind_maxparen))
!= NULL)
lnum = trypos->lnum;
// It could have been something like
// case 1: if (asdf &&
// ldfd) {
// }
if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
&& cin_iscase(skipwhite(ml_get_curline()), FALSE))
amount = get_indent();
else if (curbuf->b_ind_js)
amount = get_indent_lnum(lnum);
else
amount = skip_label(lnum, &l);
start_brace = BRACE_AT_END;
}
// For Javascript check if the line starts with "key:".
if (curbuf->b_ind_js)
js_cur_has_key = cin_has_js_key(theline);
// If we're looking at a closing brace, that's where
// we want to be. otherwise, add the amount of room
// that an indent is supposed to be.
if (theline[0] == '}')
{
// they may want closing braces to line up with something
// other than the open brace. indulge them, if so.
amount += curbuf->b_ind_close_extra;
}
else
{
// If we're looking at an "else", try to find an "if"
// to match it with.
// If we're looking at a "while", try to find a "do"
// to match it with.
lookfor = LOOKFOR_INITIAL;
if (cin_iselse(theline))
lookfor = LOOKFOR_IF;
else if (cin_iswhileofdo(theline, cur_curpos.lnum)) // XXX
lookfor = LOOKFOR_DO;
if (lookfor != LOOKFOR_INITIAL)
{
curwin->w_cursor.lnum = cur_curpos.lnum;
if (find_match(lookfor, ourscope) == OK)
{
amount = get_indent(); // XXX
goto theend;
}
}
// We get here if we are not on an "while-of-do" or "else" (or
// failed to find a matching "if").
// Search backwards for something to line up with.
// First set amount for when we don't find anything.
// if the '{' is _really_ at the left margin, use the imaginary
// location of a left-margin brace. Otherwise, correct the
// location for b_ind_open_extra.
if (start_brace == BRACE_IN_COL0) // '{' is in column 0
{
amount = curbuf->b_ind_open_left_imag;
lookfor_cpp_namespace = TRUE;
}
else if (start_brace == BRACE_AT_START &&
lookfor_cpp_namespace) // '{' is at start
{
lookfor_cpp_namespace = TRUE;
}
else
{
if (start_brace == BRACE_AT_END) // '{' is at end of line
{
amount += curbuf->b_ind_open_imag;
l = skipwhite(ml_get_curline());
if (cin_is_cpp_namespace(l))
amount += curbuf->b_ind_cpp_namespace;
else if (cin_is_cpp_extern_c(l))
amount += curbuf->b_ind_cpp_extern_c;
}
else
{
// Compensate for adding b_ind_open_extra later.
amount -= curbuf->b_ind_open_extra;
if (amount < 0)
amount = 0;
}
}
lookfor_break = FALSE;
if (cin_iscase(theline, FALSE)) // it's a switch() label
{
lookfor = LOOKFOR_CASE; // find a previous switch() label
amount += curbuf->b_ind_case;
}
else if (cin_isscopedecl(theline)) // private:, ...
{
lookfor = LOOKFOR_SCOPEDECL; // class decl is this block
amount += curbuf->b_ind_scopedecl;
}
else
{
if (curbuf->b_ind_case_break && cin_isbreak(theline))
// break; ...
lookfor_break = TRUE;
lookfor = LOOKFOR_INITIAL;
// b_ind_level from start of block
amount += curbuf->b_ind_level;
}
scope_amount = amount;
whilelevel = 0;
// Search backwards. If we find something we recognize, line up
// with that.
//
// If we're looking at an open brace, indent
// the usual amount relative to the conditional
// that opens the block.
curwin->w_cursor = cur_curpos;
for (;;)
{
curwin->w_cursor.lnum--;
curwin->w_cursor.col = 0;
// If we went all the way back to the start of our scope, line
// up with it.
if (curwin->w_cursor.lnum <= ourscope)
{
// We reached end of scope:
// If looking for a enum or structure initialization
// go further back:
// If it is an initializer (enum xxx or xxx =), then
// don't add ind_continuation, otherwise it is a variable
// declaration:
// int x,
// here; <-- add ind_continuation
if (lookfor == LOOKFOR_ENUM_OR_INIT)
{
if (curwin->w_cursor.lnum == 0
|| curwin->w_cursor.lnum
< ourscope - curbuf->b_ind_maxparen)
{
// nothing found (abuse curbuf->b_ind_maxparen as
// limit) assume terminated line (i.e. a variable
// initialization)
if (cont_amount > 0)
amount = cont_amount;
else if (!curbuf->b_ind_js)
amount += ind_continuation;
break;
}
l = ml_get_curline();
// If we're in a comment or raw string now, skip to
// the start of it.
trypos = ind_find_start_CORS(NULL);
if (trypos != NULL)
{
curwin->w_cursor.lnum = trypos->lnum + 1;
curwin->w_cursor.col = 0;
continue;
}
// Skip preprocessor directives and blank lines.
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
&amount))
continue;
if (cin_nocode(l))
continue;
terminated = cin_isterminated(l, FALSE, TRUE);
// If we are at top level and the line looks like a
// function declaration, we are done
// (it's a variable declaration).
if (start_brace != BRACE_IN_COL0
|| !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
{
// if the line is terminated with another ','
// it is a continued variable initialization.
// don't add extra indent.
// TODO: does not work, if a function
// declaration is split over multiple lines:
// cin_isfuncdecl returns FALSE then.
if (terminated == ',')
break;
// if it es a enum declaration or an assignment,
// we are done.
if (terminated != ';' && cin_isinit())
break;
// nothing useful found
if (terminated == 0 || terminated == '{')
continue;
}
if (terminated != ';')
{
// Skip parens and braces. Position the cursor
// over the rightmost paren, so that matching it
// will take us back to the start of the line.
// XXX
trypos = NULL;
if (find_last_paren(l, '(', ')'))
trypos = find_match_paren(
curbuf->b_ind_maxparen);
if (trypos == NULL && find_last_paren(l, '{', '}'))
trypos = find_start_brace();
if (trypos != NULL)
{
curwin->w_cursor.lnum = trypos->lnum + 1;
curwin->w_cursor.col = 0;
continue;
}
}
// it's a variable declaration, add indentation
// like in
// int a,
// b;
if (cont_amount > 0)
amount = cont_amount;
else
amount += ind_continuation;
}
else if (lookfor == LOOKFOR_UNTERM)
{
if (cont_amount > 0)
amount = cont_amount;
else
amount += ind_continuation;
}
else
{
if (lookfor != LOOKFOR_TERM
&& lookfor != LOOKFOR_CPP_BASECLASS
&& lookfor != LOOKFOR_COMMA)
{
amount = scope_amount;
if (theline[0] == '{')
{
amount += curbuf->b_ind_open_extra;
added_to_amount = curbuf->b_ind_open_extra;
}
}
if (lookfor_cpp_namespace)
{
// Looking for C++ namespace, need to look further
// back.
if (curwin->w_cursor.lnum == ourscope)
continue;
if (curwin->w_cursor.lnum == 0
|| curwin->w_cursor.lnum
< ourscope - FIND_NAMESPACE_LIM)
break;
l = ml_get_curline();
// If we're in a comment or raw string now, skip
// to the start of it.
trypos = ind_find_start_CORS(NULL);
if (trypos != NULL)
{
curwin->w_cursor.lnum = trypos->lnum + 1;
curwin->w_cursor.col = 0;
continue;
}
// Skip preprocessor directives and blank lines.
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
&amount))
continue;
// Finally the actual check for "namespace".
if (cin_is_cpp_namespace(l))
{
amount += curbuf->b_ind_cpp_namespace
- added_to_amount;
break;
}
else if (cin_is_cpp_extern_c(l))
{
amount += curbuf->b_ind_cpp_extern_c
- added_to_amount;
break;
}
if (cin_nocode(l))
continue;
}
}
break;
}
// If we're in a comment or raw string now, skip to the start
// of it. XXX
if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
{
curwin->w_cursor.lnum = trypos->lnum + 1;
curwin->w_cursor.col = 0;
continue;
}
l = ml_get_curline();
// If this is a switch() label, may line up relative to that.
// If this is a C++ scope declaration, do the same.
iscase = cin_iscase(l, FALSE);
if (iscase || cin_isscopedecl(l))
{
// we are only looking for cpp base class
// declaration/initialization any longer
if (lookfor == LOOKFOR_CPP_BASECLASS)
break;
// When looking for a "do" we are not interested in
// labels.
if (whilelevel > 0)
continue;
// case xx:
// c = 99 + <- this indent plus continuation
//-> here;
if (lookfor == LOOKFOR_UNTERM
|| lookfor == LOOKFOR_ENUM_OR_INIT)
{
if (cont_amount > 0)
amount = cont_amount;
else
amount += ind_continuation;
break;
}
// case xx: <- line up with this case
// x = 333;
// case yy:
if ( (iscase && lookfor == LOOKFOR_CASE)
|| (iscase && lookfor_break)
|| (!iscase && lookfor == LOOKFOR_SCOPEDECL))
{
// Check that this case label is not for another
// switch() XXX
if ((trypos = find_start_brace()) == NULL
|| trypos->lnum == ourscope)
{
amount = get_indent(); // XXX
break;
}
continue;
}
n = get_indent_nolabel(curwin->w_cursor.lnum); // XXX
// case xx: if (cond) <- line up with this if
// y = y + 1;
// -> s = 99;
//
// case xx:
// if (cond) <- line up with this line
// y = y + 1;
// -> s = 99;
if (lookfor == LOOKFOR_TERM)
{
if (n)
amount = n;
if (!lookfor_break)
break;
}
// case xx: x = x + 1; <- line up with this x
// -> y = y + 1;
//
// case xx: if (cond) <- line up with this if
// -> y = y + 1;
if (n)
{
amount = n;
l = after_label(ml_get_curline());
if (l != NULL && cin_is_cinword(l))
{
if (theline[0] == '{')
amount += curbuf->b_ind_open_extra;
else
amount += curbuf->b_ind_level
+ curbuf->b_ind_no_brace;
}
break;
}
// Try to get the indent of a statement before the switch
// label. If nothing is found, line up relative to the
// switch label.
// break; <- may line up with this line
// case xx:
// -> y = 1;
scope_amount = get_indent() + (iscase // XXX
? curbuf->b_ind_case_code
: curbuf->b_ind_scopedecl_code);
lookfor = curbuf->b_ind_case_break
? LOOKFOR_NOBREAK : LOOKFOR_ANY;
continue;
}
// Looking for a switch() label or C++ scope declaration,
// ignore other lines, skip {}-blocks.
if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
{
if (find_last_paren(l, '{', '}')
&& (trypos = find_start_brace()) != NULL)
{
curwin->w_cursor.lnum = trypos->lnum + 1;
curwin->w_cursor.col = 0;
}
continue;
}
// Ignore jump labels with nothing after them.
if (!curbuf->b_ind_js && cin_islabel())
{
l = after_label(ml_get_curline());
if (l == NULL || cin_nocode(l))
continue;
}
// Ignore #defines, #if, etc.
// Ignore comment and empty lines.
// (need to get the line again, cin_islabel() may have
// unlocked it)
l = ml_get_curline();
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
|| cin_nocode(l))
continue;
// Are we at the start of a cpp base class declaration or
// constructor initialization? XXX
n = FALSE;
if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
{
n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
l = ml_get_curline();
}
if (n)
{
if (lookfor == LOOKFOR_UNTERM)
{
if (cont_amount > 0)
amount = cont_amount;
else
amount += ind_continuation;
}
else if (theline[0] == '{')
{
// Need to find start of the declaration.
lookfor = LOOKFOR_UNTERM;
ind_continuation = 0;
continue;
}
else
// XXX
amount = get_baseclass_amount(
cache_cpp_baseclass.lpos.col);
break;
}
else if (lookfor == LOOKFOR_CPP_BASECLASS)
{
// only look, whether there is a cpp base class
// declaration or initialization before the opening brace.
if (cin_isterminated(l, TRUE, FALSE))
break;
else
continue;
}
// What happens next depends on the line being terminated.
// If terminated with a ',' only consider it terminating if
// there is another unterminated statement behind, eg:
// 123,
// sizeof
// here
// Otherwise check whether it is a enumeration or structure
// initialisation (not indented) or a variable declaration
// (indented).
terminated = cin_isterminated(l, FALSE, TRUE);
if (js_cur_has_key)
{
js_cur_has_key = 0; // only check the first line
if (curbuf->b_ind_js && terminated == ',')
{
// For Javascript we might be inside an object:
// key: something, <- align with this
// key: something
// or:
// key: something + <- align with this
// something,
// key: something
lookfor = LOOKFOR_JS_KEY;
}
}
if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
{
amount = get_indent();
break;
}
if (lookfor == LOOKFOR_COMMA)
{
if (tryposBrace != NULL && tryposBrace->lnum
>= curwin->w_cursor.lnum)
break;
if (terminated == ',')
// line below current line is the one that starts a
// (possibly broken) line ending in a comma.
break;
else
{
amount = get_indent();
if (curwin->w_cursor.lnum - 1 == ourscope)
// line above is start of the scope, thus current
// line is the one that stars a (possibly broken)
// line ending in a comma.
break;
}
}
if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
&& terminated == ','))
{
if (lookfor != LOOKFOR_ENUM_OR_INIT &&
(*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
amount += ind_continuation;
// if we're in the middle of a paren thing,
// go back to the line that starts it so
// we can get the right prevailing indent
// if ( foo &&
// bar )
// Position the cursor over the rightmost paren, so that
// matching it will take us back to the start of the line.
// Ignore a match before the start of the block.
(void)find_last_paren(l, '(', ')');
trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
|| (trypos->lnum == tryposBrace->lnum
&& trypos->col < tryposBrace->col)))
trypos = NULL;
// If we are looking for ',', we also look for matching
// braces.
if (trypos == NULL && terminated == ','
&& find_last_paren(l, '{', '}'))
trypos = find_start_brace();
if (trypos != NULL)
{
// Check if we are on a case label now. This is
// handled above.
// case xx: if ( asdf &&
// asdf)
curwin->w_cursor = *trypos;
l = ml_get_curline();
if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
{
++curwin->w_cursor.lnum;
curwin->w_cursor.col = 0;
continue;
}
}
/*
* Skip over continuation lines to find the one to get the
* indent from
* char *usethis = "bla\
* bla",
* here;
*/
if (terminated == ',')
{
while (curwin->w_cursor.lnum > 1)
{
l = ml_get(curwin->w_cursor.lnum - 1);
if (*l == NUL || l[STRLEN(l) - 1] != '\\')
break;
--curwin->w_cursor.lnum;
curwin->w_cursor.col = 0;
}
}
// Get indent and pointer to text for current line,
// ignoring any jump label. XXX
if (curbuf->b_ind_js)
cur_amount = get_indent();
else
cur_amount = skip_label(curwin->w_cursor.lnum, &l);
// If this is just above the line we are indenting, and it
// starts with a '{', line it up with this line.
// while (not)
// -> {
// }
if (terminated != ',' && lookfor != LOOKFOR_TERM
&& theline[0] == '{')
{
amount = cur_amount;
// Only add b_ind_open_extra when the current line
// doesn't start with a '{', which must have a match
// in the same line (scope is the same). Probably:
// { 1, 2 },
// -> { 3, 4 }
if (*skipwhite(l) != '{')
amount += curbuf->b_ind_open_extra;
if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
{
// have to look back, whether it is a cpp base
// class declaration or initialization
lookfor = LOOKFOR_CPP_BASECLASS;
continue;
}
break;
}
// Check if we are after an "if", "while", etc.
// Also allow " } else".
if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
{
// Found an unterminated line after an if (), line up
// with the last one.
// if (cond)
// 100 +
// -> here;
if (lookfor == LOOKFOR_UNTERM
|| lookfor == LOOKFOR_ENUM_OR_INIT)
{
if (cont_amount > 0)
amount = cont_amount;
else
amount += ind_continuation;
break;
}
// If this is just above the line we are indenting, we
// are finished.
// while (not)
// -> here;
// Otherwise this indent can be used when the line
// before this is terminated.
// yyy;
// if (stat)
// while (not)
// xxx;
// -> here;
amount = cur_amount;
if (theline[0] == '{')
amount += curbuf->b_ind_open_extra;
if (lookfor != LOOKFOR_TERM)
{
amount += curbuf->b_ind_level
+ curbuf->b_ind_no_brace;
break;
}
// Special trick: when expecting the while () after a
// do, line up with the while()
// do
// x = 1;
// -> here
l = skipwhite(ml_get_curline());
if (cin_isdo(l))
{
if (whilelevel == 0)
break;
--whilelevel;
}
// When searching for a terminated line, don't use the
// one between the "if" and the matching "else".
// Need to use the scope of this "else". XXX
// If whilelevel != 0 continue looking for a "do {".
if (cin_iselse(l) && whilelevel == 0)
{
// If we're looking at "} else", let's make sure we
// find the opening brace of the enclosing scope,
// not the one from "if () {".
if (*l == '}')
curwin->w_cursor.col =
(colnr_T)(l - ml_get_curline()) + 1;
if ((trypos = find_start_brace()) == NULL
|| find_match(LOOKFOR_IF, trypos->lnum)
== FAIL)
break;
}
}
// If we're below an unterminated line that is not an
// "if" or something, we may line up with this line or
// add something for a continuation line, depending on
// the line before this one.
else
{
// Found two unterminated lines on a row, line up with
// the last one.
// c = 99 +
// 100 +
// -> here;
if (lookfor == LOOKFOR_UNTERM)
{
// When line ends in a comma add extra indent
if (terminated == ',')
amount += ind_continuation;
break;
}
if (lookfor == LOOKFOR_ENUM_OR_INIT)
{
// Found two lines ending in ',', lineup with the
// lowest one, but check for cpp base class
// declaration/initialization, if it is an
// opening brace or we are looking just for
// enumerations/initializations.
if (terminated == ',')
{
if (curbuf->b_ind_cpp_baseclass == 0)
break;
lookfor = LOOKFOR_CPP_BASECLASS;
continue;
}
// Ignore unterminated lines in between, but
// reduce indent.
if (amount > cur_amount)
amount = cur_amount;
}
else
{
// Found first unterminated line on a row, may
// line up with this line, remember its indent
// 100 +
// -> here;
l = ml_get_curline();
amount = cur_amount;
n = (int)STRLEN(l);
if (terminated == ',' && (*skipwhite(l) == ']'
|| (n >=2 && l[n - 2] == ']')))
break;
// If previous line ends in ',', check whether we
// are in an initialization or enum
// struct xxx =
// {
// sizeof a,
// 124 };
// or a normal possible continuation line.
// but only, of no other statement has been found
// yet.
if (lookfor == LOOKFOR_INITIAL && terminated == ',')
{
if (curbuf->b_ind_js)
{
// Search for a line ending in a comma
// and line up with the line below it
// (could be the current line).
// some = [
// 1, <- line up here
// 2,
// some = [
// 3 + <- line up here
// 4 *
// 5,
// 6,
if (cin_iscomment(skipwhite(l)))
break;
lookfor = LOOKFOR_COMMA;
trypos = find_match_char('[',
curbuf->b_ind_maxparen);
if (trypos != NULL)
{
if (trypos->lnum
== curwin->w_cursor.lnum - 1)
{
// Current line is first inside
// [], line up with it.
break;
}
ourscope = trypos->lnum;
}
}
else
{
lookfor = LOOKFOR_ENUM_OR_INIT;
cont_amount = cin_first_id_amount();
}
}
else
{
if (lookfor == LOOKFOR_INITIAL
&& *l != NUL
&& l[STRLEN(l) - 1] == '\\')
// XXX
cont_amount = cin_get_equal_amount(
curwin->w_cursor.lnum);
if (lookfor != LOOKFOR_TERM
&& lookfor != LOOKFOR_JS_KEY
&& lookfor != LOOKFOR_COMMA
&& raw_string_start != curwin->w_cursor.lnum)
lookfor = LOOKFOR_UNTERM;
}
}
}
}
// Check if we are after a while (cond);
// If so: Ignore until the matching "do".
else if (cin_iswhileofdo_end(terminated)) // XXX
{
// Found an unterminated line after a while ();, line up
// with the last one.
// while (cond);
// 100 + <- line up with this one
// -> here;
if (lookfor == LOOKFOR_UNTERM
|| lookfor == LOOKFOR_ENUM_OR_INIT)
{
if (cont_amount > 0)
amount = cont_amount;
else
amount += ind_continuation;
break;
}
if (whilelevel == 0)
{
lookfor = LOOKFOR_TERM;
amount = get_indent(); // XXX
if (theline[0] == '{')
amount += curbuf->b_ind_open_extra;
}
++whilelevel;
}
// We are after a "normal" statement.
// If we had another statement we can stop now and use the
// indent of that other statement.
// Otherwise the indent of the current statement may be used,
// search backwards for the next "normal" statement.
else
{
// Skip single break line, if before a switch label. It
// may be lined up with the case label.
if (lookfor == LOOKFOR_NOBREAK
&& cin_isbreak(skipwhite(ml_get_curline())))
{
lookfor = LOOKFOR_ANY;
continue;
}
// Handle "do {" line.
if (whilelevel > 0)
{
l = cin_skipcomment(ml_get_curline());
if (cin_isdo(l))
{
amount = get_indent(); // XXX
--whilelevel;
continue;
}
}
// Found a terminated line above an unterminated line. Add
// the amount for a continuation line.
// x = 1;
// y = foo +
// -> here;
// or
// int x = 1;
// int foo,
// -> here;
if (lookfor == LOOKFOR_UNTERM
|| lookfor == LOOKFOR_ENUM_OR_INIT)
{
if (cont_amount > 0)
amount = cont_amount;
else
amount += ind_continuation;
break;
}
// Found a terminated line above a terminated line or "if"
// etc. line. Use the amount of the line below us.
// x = 1; x = 1;
// if (asdf) y = 2;
// while (asdf) ->here;
// here;
// ->foo;
if (lookfor == LOOKFOR_TERM)
{
if (!lookfor_break && whilelevel == 0)
break;
}
// First line above the one we're indenting is terminated.
// To know what needs to be done look further backward for
// a terminated line.
else
{
// position the cursor over the rightmost paren, so
// that matching it will take us back to the start of
// the line. Helps for:
// func(asdr,
// asdfasdf);
// here;
term_again:
l = ml_get_curline();
if (find_last_paren(l, '(', ')')
&& (trypos = find_match_paren(
curbuf->b_ind_maxparen)) != NULL)
{
// Check if we are on a case label now. This is
// handled above.
// case xx: if ( asdf &&
// asdf)
curwin->w_cursor = *trypos;
l = ml_get_curline();
if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
{
++curwin->w_cursor.lnum;
curwin->w_cursor.col = 0;
continue;
}
}
// When aligning with the case statement, don't align
// with a statement after it.
// case 1: { <-- don't use this { position
// stat;
// }
// case 2:
// stat;
// }
iscase = (curbuf->b_ind_keep_case_label
&& cin_iscase(l, FALSE));
// Get indent and pointer to text for current line,
// ignoring any jump label.
amount = skip_label(curwin->w_cursor.lnum, &l);
if (theline[0] == '{')
amount += curbuf->b_ind_open_extra;
// See remark above: "Only add b_ind_open_extra.."
l = skipwhite(l);
if (*l == '{')
amount -= curbuf->b_ind_open_extra;
lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
// When a terminated line starts with "else" skip to
// the matching "if":
// else 3;
// indent this;
// Need to use the scope of this "else". XXX
// If whilelevel != 0 continue looking for a "do {".
if (lookfor == LOOKFOR_TERM
&& *l != '}'
&& cin_iselse(l)
&& whilelevel == 0)
{
if ((trypos = find_start_brace()) == NULL
|| find_match(LOOKFOR_IF, trypos->lnum)
== FAIL)
break;
continue;
}
// If we're at the end of a block, skip to the start of
// that block.
l = ml_get_curline();
if (find_last_paren(l, '{', '}') // XXX
&& (trypos = find_start_brace()) != NULL)
{
curwin->w_cursor = *trypos;
// if not "else {" check for terminated again
// but skip block for "} else {"
l = cin_skipcomment(ml_get_curline());
if (*l == '}' || !cin_iselse(l))
goto term_again;
++curwin->w_cursor.lnum;
curwin->w_cursor.col = 0;
}
}
}
}
}
}
// add extra indent for a comment
if (cin_iscomment(theline))
amount += curbuf->b_ind_comment;
// subtract extra left-shift for jump labels
if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
amount -= curbuf->b_ind_jump_label;
goto theend;
}
// ok -- we're not inside any sort of structure at all!
//
// This means we're at the top level, and everything should
// basically just match where the previous line is, except
// for the lines immediately following a function declaration,
// which are K&R-style parameters and need to be indented.
//
// if our line starts with an open brace, forget about any
// prevailing indent and make sure it looks like the start
// of a function
if (theline[0] == '{')
{
amount = curbuf->b_ind_first_open;
goto theend;
}
// If the NEXT line is a function declaration, the current
// line needs to be indented as a function type spec.
// Don't do this if the current line looks like a comment or if the
// current line is terminated, ie. ends in ';', or if the current line
// contains { or }: "void f() {\n if (1)"
if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
&& !cin_nocode(theline)
&& vim_strchr(theline, '{') == NULL
&& vim_strchr(theline, '}') == NULL
&& !cin_ends_in(theline, (char_u *)":", NULL)
&& !cin_ends_in(theline, (char_u *)",", NULL)
&& cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
cur_curpos.lnum + 1)
&& !cin_isterminated(theline, FALSE, TRUE))
{
amount = curbuf->b_ind_func_type;
goto theend;
}
// search backwards until we find something we recognize
amount = 0;
curwin->w_cursor = cur_curpos;
while (curwin->w_cursor.lnum > 1)
{
curwin->w_cursor.lnum--;
curwin->w_cursor.col = 0;
l = ml_get_curline();
// If we're in a comment or raw string now, skip to the start
// of it. XXX
if ((trypos = ind_find_start_CORS(NULL)) != NULL)
{
curwin->w_cursor.lnum = trypos->lnum + 1;
curwin->w_cursor.col = 0;
continue;
}
// Are we at the start of a cpp base class declaration or
// constructor initialization? XXX
n = FALSE;
if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
{
n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
l = ml_get_curline();
}
if (n)
{
// XXX
amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
break;
}
// Skip preprocessor directives and blank lines.
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
continue;
if (cin_nocode(l))
continue;
// If the previous line ends in ',', use one level of
// indentation:
// int foo,
// bar;
// do this before checking for '}' in case of eg.
// enum foobar
// {
// ...
// } foo,
// bar;
n = 0;
if (cin_ends_in(l, (char_u *)",", NULL)
|| (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
{
// take us back to opening paren
if (find_last_paren(l, '(', ')')
&& (trypos = find_match_paren(
curbuf->b_ind_maxparen)) != NULL)
curwin->w_cursor = *trypos;
/*
* For a line ending in ',' that is a continuation line go
* back to the first line with a backslash:
* char *foo = "bla\
* bla",
* here;
*/
while (n == 0 && curwin->w_cursor.lnum > 1)
{
l = ml_get(curwin->w_cursor.lnum - 1);
if (*l == NUL || l[STRLEN(l) - 1] != '\\')
break;
--curwin->w_cursor.lnum;
curwin->w_cursor.col = 0;
}
amount = get_indent(); // XXX
if (amount == 0)
amount = cin_first_id_amount();
if (amount == 0)
amount = ind_continuation;
break;
}
// If the line looks like a function declaration, and we're
// not in a comment, put it the left margin.
if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) // XXX
break;
l = ml_get_curline();
// Finding the closing '}' of a previous function. Put
// current line at the left margin. For when 'cino' has "fs".
if (*skipwhite(l) == '}')
break;
// (matching {)
// If the previous line ends on '};' (maybe followed by
// comments) align at column 0. For example:
// char *string_array[] = { "foo",
// / * x * / "b};ar" }; / * foobar * /
if (cin_ends_in(l, (char_u *)"};", NULL))
break;
// If the previous line ends on '[' we are probably in an
// array constant:
// something = [
// 234, <- extra indent
if (cin_ends_in(l, (char_u *)"[", NULL))
{
amount = get_indent() + ind_continuation;
break;
}
// Find a line only has a semicolon that belongs to a previous
// line ending in '}', e.g. before an #endif. Don't increase
// indent then.
if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
{
pos_T curpos_save = curwin->w_cursor;
while (curwin->w_cursor.lnum > 1)
{
look = ml_get(--curwin->w_cursor.lnum);
if (!(cin_nocode(look) || cin_ispreproc_cont(
&look, &curwin->w_cursor.lnum, &amount)))
break;
}
if (curwin->w_cursor.lnum > 0
&& cin_ends_in(look, (char_u *)"}", NULL))
break;
curwin->w_cursor = curpos_save;
}
// If the PREVIOUS line is a function declaration, the current
// line (and the ones that follow) needs to be indented as
// parameters.
if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
{
amount = curbuf->b_ind_param;
break;
}
// If the previous line ends in ';' and the line before the
// previous line ends in ',' or '\', ident to column zero:
// int foo,
// bar;
// indent_to_0 here;
if (cin_ends_in(l, (char_u *)";", NULL))
{
l = ml_get(curwin->w_cursor.lnum - 1);
if (cin_ends_in(l, (char_u *)",", NULL)
|| (*l != NUL && l[STRLEN(l) - 1] == '\\'))
break;
l = ml_get_curline();
}
// Doesn't look like anything interesting -- so just
// use the indent of this line.
//
// Position the cursor over the rightmost paren, so that
// matching it will take us back to the start of the line.
find_last_paren(l, '(', ')');
if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
curwin->w_cursor = *trypos;
amount = get_indent(); // XXX
break;
}
// add extra indent for a comment
if (cin_iscomment(theline))
amount += curbuf->b_ind_comment;
/*
* add extra indent if the previous line ended in a backslash:
* "asdfasdf\
* here";
* char *foo = "asdf\
* here";
*/
if (cur_curpos.lnum > 1)
{
l = ml_get(cur_curpos.lnum - 1);
if (*l != NUL && l[STRLEN(l) - 1] == '\\')
{
cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
if (cur_amount > 0)
amount = cur_amount;
else if (cur_amount == 0)
amount += ind_continuation;
}
}
theend:
if (amount < 0)
amount = 0;
laterend:
// put the cursor back where it belongs
curwin->w_cursor = cur_curpos;
vim_free(linecopy);
return amount;
}
static int
find_match(int lookfor, linenr_T ourscope)
{
char_u *look;
pos_T *theirscope;
char_u *mightbeif;
int elselevel;
int whilelevel;
if (lookfor == LOOKFOR_IF)
{
elselevel = 1;
whilelevel = 0;
}
else
{
elselevel = 0;
whilelevel = 1;
}
curwin->w_cursor.col = 0;
while (curwin->w_cursor.lnum > ourscope + 1)
{
curwin->w_cursor.lnum--;
curwin->w_cursor.col = 0;
look = cin_skipcomment(ml_get_curline());
if (cin_iselse(look)
|| cin_isif(look)
|| cin_isdo(look) // XXX
|| cin_iswhileofdo(look, curwin->w_cursor.lnum))
{
// if we've gone outside the braces entirely,
// we must be out of scope...
theirscope = find_start_brace(); // XXX
if (theirscope == NULL)
break;
// and if the brace enclosing this is further
// back than the one enclosing the else, we're
// out of luck too.
if (theirscope->lnum < ourscope)
break;
// and if they're enclosed in a *deeper* brace,
// then we can ignore it because it's in a
// different scope...
if (theirscope->lnum > ourscope)
continue;
// if it was an "else" (that's not an "else if")
// then we need to go back to another if, so
// increment elselevel
look = cin_skipcomment(ml_get_curline());
if (cin_iselse(look))
{
mightbeif = cin_skipcomment(look + 4);
if (!cin_isif(mightbeif))
++elselevel;
continue;
}
// if it was a "while" then we need to go back to
// another "do", so increment whilelevel. XXX
if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
{
++whilelevel;
continue;
}
// If it's an "if" decrement elselevel
look = cin_skipcomment(ml_get_curline());
if (cin_isif(look))
{
elselevel--;
// When looking for an "if" ignore "while"s that
// get in the way.
if (elselevel == 0 && lookfor == LOOKFOR_IF)
whilelevel = 0;
}
// If it's a "do" decrement whilelevel
if (cin_isdo(look))
whilelevel--;
// if we've used up all the elses, then
// this must be the if that we want!
// match the indent level of that if.
if (elselevel <= 0 && whilelevel <= 0)
return OK;
}
}
return FAIL;
}
# if defined(FEAT_EVAL) || defined(PROTO)
/*
* Get indent level from 'indentexpr'.
*/
int
get_expr_indent(void)
{
int indent = -1;
char_u *inde_copy;
pos_T save_pos;
colnr_T save_curswant;
int save_set_curswant;
int save_State;
int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
OPT_LOCAL);
// Save and restore cursor position and curswant, in case it was changed
// via :normal commands
save_pos = curwin->w_cursor;
save_curswant = curwin->w_curswant;
save_set_curswant = curwin->w_set_curswant;
set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
if (use_sandbox)
++sandbox;
++textlock;
// Need to make a copy, the 'indentexpr' option could be changed while
// evaluating it.
inde_copy = vim_strsave(curbuf->b_p_inde);
if (inde_copy != NULL)
{
indent = (int)eval_to_number(inde_copy);
vim_free(inde_copy);
}
if (use_sandbox)
--sandbox;
--textlock;
// Restore the cursor position so that 'indentexpr' doesn't need to.
// Pretend to be in Insert mode, allow cursor past end of line for "o"
// command.
save_State = State;
State = INSERT;
curwin->w_cursor = save_pos;
curwin->w_curswant = save_curswant;
curwin->w_set_curswant = save_set_curswant;
check_cursor();
State = save_State;
// If there is an error, just keep the current indent.
if (indent < 0)
indent = get_indent();
return indent;
}
# endif
/*
* return TRUE if 'cinkeys' contains the key "keytyped",
* when == '*': Only if key is preceded with '*' (indent before insert)
* when == '!': Only if key is preceded with '!' (don't insert)
* when == ' ': Only if key is not preceded with '*'(indent afterwards)
*
* "keytyped" can have a few special values:
* KEY_OPEN_FORW
* KEY_OPEN_BACK
* KEY_COMPLETE just finished completion.
*
* If line_is_empty is TRUE accept keys with '0' before them.
*/
int
in_cinkeys(
int keytyped,
int when,
int line_is_empty)
{
char_u *look;
int try_match;
int try_match_word;
char_u *p;
char_u *line;
int icase;
int i;
if (keytyped == NUL)
// Can happen with CTRL-Y and CTRL-E on a short line.
return FALSE;
#ifdef FEAT_EVAL
if (*curbuf->b_p_inde != NUL)
look = curbuf->b_p_indk; // 'indentexpr' set: use 'indentkeys'
else
#endif
look = curbuf->b_p_cink; // 'indentexpr' empty: use 'cinkeys'
while (*look)
{
// Find out if we want to try a match with this key, depending on
// 'when' and a '*' or '!' before the key.
switch (when)
{
case '*': try_match = (*look == '*'); break;
case '!': try_match = (*look == '!'); break;
default: try_match = (*look != '*'); break;
}
if (*look == '*' || *look == '!')
++look;
// If there is a '0', only accept a match if the line is empty.
// But may still match when typing last char of a word.
if (*look == '0')
{
try_match_word = try_match;
if (!line_is_empty)
try_match = FALSE;
++look;
}
else
try_match_word = FALSE;
// does it look like a control character?
if (*look == '^'
#ifdef EBCDIC
&& (Ctrl_chr(look[1]) != 0)
#else
&& look[1] >= '?' && look[1] <= '_'
#endif
)
{
if (try_match && keytyped == Ctrl_chr(look[1]))
return TRUE;
look += 2;
}
// 'o' means "o" command, open forward.
// 'O' means "O" command, open backward.
else if (*look == 'o')
{
if (try_match && keytyped == KEY_OPEN_FORW)
return TRUE;
++look;
}
else if (*look == 'O')
{
if (try_match && keytyped == KEY_OPEN_BACK)
return TRUE;
++look;
}
// 'e' means to check for "else" at start of line and just before the
// cursor.
else if (*look == 'e')
{
if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
{
p = ml_get_curline();
if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
return TRUE;
}
++look;
}
// ':' only causes an indent if it is at the end of a label or case
// statement, or when it was before typing the ':' (to fix
// class::method for C++).
else if (*look == ':')
{
if (try_match && keytyped == ':')
{
p = ml_get_curline();
if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
return TRUE;
// Need to get the line again after cin_islabel().
p = ml_get_curline();
if (curwin->w_cursor.col > 2
&& p[curwin->w_cursor.col - 1] == ':'
&& p[curwin->w_cursor.col - 2] == ':')
{
p[curwin->w_cursor.col - 1] = ' ';
i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
|| cin_islabel());
p = ml_get_curline();
p[curwin->w_cursor.col - 1] = ':';
if (i)
return TRUE;
}
}
++look;
}
// Is it a key in <>, maybe?
else if (*look == '<')
{
if (try_match)
{
// make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
// <:> and <!> so that people can re-indent on o, O, e, 0, <,
// >, *, : and ! keys if they really really want to.
if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
&& keytyped == look[1])
return TRUE;
if (keytyped == get_special_key_code(look + 1))
return TRUE;
}
while (*look && *look != '>')
look++;
while (*look == '>')
look++;
}
// Is it a word: "=word"?
else if (*look == '=' && look[1] != ',' && look[1] != NUL)
{
++look;
if (*look == '~')
{
icase = TRUE;
++look;
}
else
icase = FALSE;
p = vim_strchr(look, ',');
if (p == NULL)
p = look + STRLEN(look);
if ((try_match || try_match_word)
&& curwin->w_cursor.col >= (colnr_T)(p - look))
{
int match = FALSE;
if (keytyped == KEY_COMPLETE)
{
char_u *s;
// Just completed a word, check if it starts with "look".
// search back for the start of a word.
line = ml_get_curline();
if (has_mbyte)
{
char_u *n;
for (s = line + curwin->w_cursor.col; s > line; s = n)
{
n = mb_prevptr(line, s);
if (!vim_iswordp(n))
break;
}
}
else
for (s = line + curwin->w_cursor.col; s > line; --s)
if (!vim_iswordc(s[-1]))
break;
if (s + (p - look) <= line + curwin->w_cursor.col
&& (icase
? MB_STRNICMP(s, look, p - look)
: STRNCMP(s, look, p - look)) == 0)
match = TRUE;
}
else
// TODO: multi-byte
if (keytyped == (int)p[-1] || (icase && keytyped < 256
&& TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
{
line = ml_get_cursor();
if ((curwin->w_cursor.col == (colnr_T)(p - look)
|| !vim_iswordc(line[-(p - look) - 1]))
&& (icase
? MB_STRNICMP(line - (p - look), look, p - look)
: STRNCMP(line - (p - look), look, p - look))
== 0)
match = TRUE;
}
if (match && try_match_word && !try_match)
{
// "0=word": Check if there are only blanks before the
// word.
if (getwhitecols_curline() !=
(int)(curwin->w_cursor.col - (p - look)))
match = FALSE;
}
if (match)
return TRUE;
}
look = p;
}
// ok, it's a boring generic character.
else
{
if (try_match && *look == keytyped)
return TRUE;
if (*look != NUL)
++look;
}
// Skip over ", ".
look = skip_to_option_part(look);
}
return FALSE;
}
#endif // FEAT_CINDENT
#if defined(FEAT_LISP) || defined(PROTO)
static int
lisp_match(char_u *p)
{
char_u buf[LSIZE];
int len;
char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
while (*word != NUL)
{
(void)copy_option_part(&word, buf, LSIZE, ",");
len = (int)STRLEN(buf);
if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
return TRUE;
}
return FALSE;
}
/*
* When 'p' is present in 'cpoptions, a Vi compatible method is used.
* The incompatible newer method is quite a bit better at indenting
* code in lisp-like languages than the traditional one; it's still
* mostly heuristics however -- Dirk van Deun, dirk@rave.org
*
* TODO:
* Findmatch() should be adapted for lisp, also to make showmatch
* work correctly: now (v5.3) it seems all C/C++ oriented:
* - it does not recognize the #\( and #\) notations as character literals
* - it doesn't know about comments starting with a semicolon
* - it incorrectly interprets '(' as a character literal
* All this messes up get_lisp_indent in some rare cases.
* Update from Sergey Khorev:
* I tried to fix the first two issues.
*/
int
get_lisp_indent(void)
{
pos_T *pos, realpos, paren;
int amount;
char_u *that;
colnr_T col;
colnr_T firsttry;
int parencount, quotecount;
int vi_lisp;
// Set vi_lisp to use the vi-compatible method
vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
realpos = curwin->w_cursor;
curwin->w_cursor.col = 0;
if ((pos = findmatch(NULL, '(')) == NULL)
pos = findmatch(NULL, '[');
else
{
paren = *pos;
pos = findmatch(NULL, '[');
if (pos == NULL || LT_POSP(pos, &paren))
pos = &paren;
}
if (pos != NULL)
{
// Extra trick: Take the indent of the first previous non-white
// line that is at the same () level.
amount = -1;
parencount = 0;
while (--curwin->w_cursor.lnum >= pos->lnum)
{
if (linewhite(curwin->w_cursor.lnum))
continue;
for (that = ml_get_curline(); *that != NUL; ++that)
{
if (*that == ';')
{
while (*(that + 1) != NUL)
++that;
continue;
}
if (*that == '\\')
{
if (*(that + 1) != NUL)
++that;
continue;
}
if (*that == '"' && *(that + 1) != NUL)
{
while (*++that && *that != '"')
{
// skipping escaped characters in the string
if (*that == '\\')
{
if (*++that == NUL)
break;
if (that[1] == NUL)
{
++that;
break;
}
}
}
}
if (*that == '(' || *that == '[')
++parencount;
else if (*that == ')' || *that == ']')
--parencount;
}
if (parencount == 0)
{
amount = get_indent();
break;
}
}
if (amount == -1)
{
curwin->w_cursor.lnum = pos->lnum;
curwin->w_cursor.col = pos->col;
col = pos->col;
that = ml_get_curline();
if (vi_lisp && get_indent() == 0)
amount = 2;
else
{
char_u *line = that;
amount = 0;
while (*that && col)
{
amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
col--;
}
// Some keywords require "body" indenting rules (the
// non-standard-lisp ones are Scheme special forms):
//
// (let ((a 1)) instead (let ((a 1))
// (...)) of (...))
if (!vi_lisp && (*that == '(' || *that == '[')
&& lisp_match(that + 1))
amount += 2;
else
{
that++;
amount++;
firsttry = amount;
while (VIM_ISWHITE(*that))
{
amount += lbr_chartabsize(line, that, (colnr_T)amount);
++that;
}
if (*that && *that != ';') // not a comment line
{
// test *that != '(' to accommodate first let/do
// argument if it is more than one line
if (!vi_lisp && *that != '(' && *that != '[')
firsttry++;
parencount = 0;
quotecount = 0;
if (vi_lisp
|| (*that != '"'
&& *that != '\''
&& *that != '#'
&& (*that < '0' || *that > '9')))
{
while (*that
&& (!VIM_ISWHITE(*that)
|| quotecount
|| parencount)
&& (!((*that == '(' || *that == '[')
&& !quotecount
&& !parencount
&& vi_lisp)))
{
if (*that == '"')
quotecount = !quotecount;
if ((*that == '(' || *that == '[')
&& !quotecount)
++parencount;
if ((*that == ')' || *that == ']')
&& !quotecount)
--parencount;
if (*that == '\\' && *(that+1) != NUL)
amount += lbr_chartabsize_adv(
line, &that, (colnr_T)amount);
amount += lbr_chartabsize_adv(
line, &that, (colnr_T)amount);
}
}
while (VIM_ISWHITE(*that))
{
amount += lbr_chartabsize(
line, that, (colnr_T)amount);
that++;
}
if (!*that || *that == ';')
amount = firsttry;
}
}
}
}
}
else
amount = 0; // no matching '(' or '[' found, use zero indent
curwin->w_cursor = realpos;
return amount;
}
#endif // FEAT_LISP
#if defined(FEAT_CINDENT) || defined(PROTO)
/*
* Do C or expression indenting on the current line.
*/
void
do_c_expr_indent(void)
{
# ifdef FEAT_EVAL
if (*curbuf->b_p_inde != NUL)
fixthisline(get_expr_indent);
else
# endif
fixthisline(get_c_indent);
}
#endif
#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
/*
* Re-indent the current line, based on the current contents of it and the
* surrounding lines. Fixing the cursor position seems really easy -- I'm very
* confused what all the part that handles Control-T is doing that I'm not.
* "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
*/
void
fixthisline(int (*get_the_indent)(void))
{
int amount = get_the_indent();
if (amount >= 0)
{
change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
if (linewhite(curwin->w_cursor.lnum))
did_ai = TRUE; // delete the indent if the line stays empty
}
}
void
fix_indent(void)
{
if (p_paste)
return;
# ifdef FEAT_LISP
if (curbuf->b_p_lisp && curbuf->b_p_ai)
fixthisline(get_lisp_indent);
# endif
# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
else
# endif
# ifdef FEAT_CINDENT
if (cindent_on())
do_c_expr_indent();
# endif
}
#endif
#if defined(FEAT_VARTABS) || defined(PROTO)
/*
* Set the integer values corresponding to the string setting of 'vartabstop'.
* "array" will be set, caller must free it if needed.
*/
int
tabstop_set(char_u *var, int **array)
{
int valcount = 1;
int t;
char_u *cp;
if (var[0] == NUL || (var[0] == '0' && var[1] == NUL))
{
*array = NULL;
return TRUE;
}
for (cp = var; *cp != NUL; ++cp)
{
if (cp == var || cp[-1] == ',')
{
char_u *end;
if (strtol((char *)cp, (char **)&end, 10) <= 0)
{
if (cp != end)
emsg(_(e_positive));
else
emsg(_(e_invarg));
return FALSE;
}
}
if (VIM_ISDIGIT(*cp))
continue;
if (cp[0] == ',' && cp > var && cp[-1] != ',' && cp[1] != NUL)
{
++valcount;
continue;
}
emsg(_(e_invarg));
return FALSE;
}
*array = ALLOC_MULT(int, valcount + 1);
if (*array == NULL)
return FALSE;
(*array)[0] = valcount;
t = 1;
for (cp = var; *cp != NUL;)
{
(*array)[t++] = atoi((char *)cp);
while (*cp != NUL && *cp != ',')
++cp;
if (*cp != NUL)
++cp;
}
return TRUE;
}
/*
* Calculate the number of screen spaces a tab will occupy.
* If "vts" is set then the tab widths are taken from that array,
* otherwise the value of ts is used.
*/
int
tabstop_padding(colnr_T col, int ts_arg, int *vts)
{
int ts = ts_arg == 0 ? 8 : ts_arg;
int tabcount;
colnr_T tabcol = 0;
int t;
int padding = 0;
if (vts == NULL || vts[0] == 0)
return ts - (col % ts);
tabcount = vts[0];
for (t = 1; t <= tabcount; ++t)
{
tabcol += vts[t];
if (tabcol > col)
{
padding = (int)(tabcol - col);
break;
}
}
if (t > tabcount)
padding = vts[tabcount] - (int)((col - tabcol) % vts[tabcount]);
return padding;
}
/*
* Find the size of the tab that covers a particular column.
*/
int
tabstop_at(colnr_T col, int ts, int *vts)
{
int tabcount;
colnr_T tabcol = 0;
int t;
int tab_size = 0;
if (vts == 0 || vts[0] == 0)
return ts;
tabcount = vts[0];
for (t = 1; t <= tabcount; ++t)
{
tabcol += vts[t];
if (tabcol > col)
{
tab_size = vts[t];
break;
}
}
if (t > tabcount)
tab_size = vts[tabcount];
return tab_size;
}
/*
* Find the column on which a tab starts.
*/
colnr_T
tabstop_start(colnr_T col, int ts, int *vts)
{
int tabcount;
colnr_T tabcol = 0;
int t;
int excess;
if (vts == NULL || vts[0] == 0)
return (col / ts) * ts;
tabcount = vts[0];
for (t = 1; t <= tabcount; ++t)
{
tabcol += vts[t];
if (tabcol > col)
return tabcol - vts[t];
}
excess = tabcol % vts[tabcount];
return excess + ((col - excess) / vts[tabcount]) * vts[tabcount];
}
/*
* Find the number of tabs and spaces necessary to get from one column
* to another.
*/
void
tabstop_fromto(
colnr_T start_col,
colnr_T end_col,
int ts_arg,
int *vts,
int *ntabs,
int *nspcs)
{
int spaces = end_col - start_col;
colnr_T tabcol = 0;
int padding = 0;
int tabcount;
int t;
int ts = ts_arg == 0 ? curbuf->b_p_ts : ts_arg;
if (vts == NULL || vts[0] == 0)
{
int tabs = 0;
int initspc = 0;
initspc = ts - (start_col % ts);
if (spaces >= initspc)
{
spaces -= initspc;
tabs++;
}
tabs += spaces / ts;
spaces -= (spaces / ts) * ts;
*ntabs = tabs;
*nspcs = spaces;
return;
}
// Find the padding needed to reach the next tabstop.
tabcount = vts[0];
for (t = 1; t <= tabcount; ++t)
{
tabcol += vts[t];
if (tabcol > start_col)
{
padding = (int)(tabcol - start_col);
break;
}
}
if (t > tabcount)
padding = vts[tabcount] - (int)((start_col - tabcol) % vts[tabcount]);
// If the space needed is less than the padding no tabs can be used.
if (spaces < padding)
{
*ntabs = 0;
*nspcs = spaces;
return;
}
*ntabs = 1;
spaces -= padding;
// At least one tab has been used. See if any more will fit.
while (spaces != 0 && ++t <= tabcount)
{
padding = vts[t];
if (spaces < padding)
{
*nspcs = spaces;
return;
}
++*ntabs;
spaces -= padding;
}
*ntabs += spaces / vts[tabcount];
*nspcs = spaces % vts[tabcount];
}
/*
* See if two tabstop arrays contain the same values.
*/
int
tabstop_eq(int *ts1, int *ts2)
{
int t;
if ((ts1 == 0 && ts2) || (ts1 && ts2 == 0))
return FALSE;
if (ts1 == ts2)
return TRUE;
if (ts1[0] != ts2[0])
return FALSE;
for (t = 1; t <= ts1[0]; ++t)
if (ts1[t] != ts2[t])
return FALSE;
return TRUE;
}
#if defined(FEAT_BEVAL) || defined(PROTO)
/*
* Copy a tabstop array, allocating space for the new array.
*/
int *
tabstop_copy(int *oldts)
{
int *newts;
int t;
if (oldts == NULL)
return NULL;
newts = ALLOC_MULT(int, oldts[0] + 1);
if (newts != NULL)
for (t = 0; t <= oldts[0]; ++t)
newts[t] = oldts[t];
return newts;
}
#endif
/*
* Return a count of the number of tabstops.
*/
int
tabstop_count(int *ts)
{
return ts != NULL ? ts[0] : 0;
}
/*
* Return the first tabstop, or 8 if there are no tabstops defined.
*/
int
tabstop_first(int *ts)
{
return ts != NULL ? ts[1] : 8;
}
#endif
/*
* Return the effective shiftwidth value for current buffer, using the
* 'tabstop' value when 'shiftwidth' is zero.
*/
long
get_sw_value(buf_T *buf)
{
return get_sw_value_col(buf, 0);
}
/*
* Idem, using "pos".
*/
static long
get_sw_value_pos(buf_T *buf, pos_T *pos)
{
pos_T save_cursor = curwin->w_cursor;
long sw_value;
curwin->w_cursor = *pos;
sw_value = get_sw_value_col(buf, get_nolist_virtcol());
curwin->w_cursor = save_cursor;
return sw_value;
}
/*
* Idem, using the first non-black in the current line.
*/
long
get_sw_value_indent(buf_T *buf)
{
pos_T pos = curwin->w_cursor;
pos.col = getwhitecols_curline();
return get_sw_value_pos(buf, &pos);
}
/*
* Idem, using virtual column "col".
*/
long
get_sw_value_col(buf_T *buf, colnr_T col UNUSED)
{
return buf->b_p_sw ? buf->b_p_sw :
#ifdef FEAT_VARTABS
tabstop_at(col, buf->b_p_ts, buf->b_p_vts_array);
#else
buf->b_p_ts;
#endif
}
/*
* Return the effective softtabstop value for the current buffer, using the
* 'shiftwidth' value when 'softtabstop' is negative.
*/
long
get_sts_value(void)
{
return curbuf->b_p_sts < 0 ? get_sw_value(curbuf) : curbuf->b_p_sts;
}
|