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
|
/* 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.
*/
/*
* Code to handle tags and the tag stack
*/
#include "vim.h"
/*
* Structure to hold pointers to various items in a tag line.
*/
typedef struct tag_pointers
{
// filled in by parse_tag_line():
char_u *tagname; // start of tag name (skip "file:")
char_u *tagname_end; // char after tag name
char_u *fname; // first char of file name
char_u *fname_end; // char after file name
char_u *command; // first char of command
// filled in by parse_match():
char_u *command_end; // first char after command
char_u *tag_fname; // file name of the tags file. This is used
// when 'tr' is set.
#ifdef FEAT_EMACS_TAGS
int is_etag; // TRUE for emacs tag
#endif
char_u *tagkind; // "kind:" value
char_u *tagkind_end; // end of tagkind
char_u *user_data; // user_data string
char_u *user_data_end; // end of user_data
linenr_T tagline; // "line:" value
} tagptrs_T;
/*
* Return values used when reading lines from a tags file.
*/
typedef enum
{
TAGS_READ_SUCCESS = 1,
TAGS_READ_EOF,
TAGS_READ_IGNORE,
} tags_read_status_T;
/*
* States used during a tags search
*/
typedef enum
{
TS_START, // at start of file
TS_LINEAR, // linear searching forward, till EOF
TS_BINARY, // binary searching
TS_SKIP_BACK, // skipping backwards
TS_STEP_FORWARD // stepping forwards
} tagsearch_state_T; // Current search state
/*
* Binary search file offsets in a tags file
*/
typedef struct
{
off_T low_offset; // offset for first char of first line that
// could match
off_T high_offset; // offset of char after last line that could
// match
off_T curr_offset; // Current file offset in search range
off_T curr_offset_used; // curr_offset used when skipping back
off_T match_offset; // Where the binary search found a tag
int low_char; // first char at low_offset
int high_char; // first char at high_offset
} tagsearch_info_T;
/*
* Return values used when matching tags against a pattern.
*/
typedef enum
{
TAG_MATCH_SUCCESS = 1,
TAG_MATCH_FAIL,
TAG_MATCH_STOP,
TAG_MATCH_NEXT
} tagmatch_status_T;
/*
* Arguments used for matching tags read from a tags file against a pattern.
*/
typedef struct
{
int matchoff; // tag match offset
int match_re; // TRUE if the tag matches a regexp
int match_no_ic; // TRUE if the tag matches with case
int has_re; // regular expression used
int sortic; // tags file sorted ignoring case (foldcase)
int sort_error; // tags file not sorted
} findtags_match_args_T;
/*
* The matching tags are first stored in one of the hash tables. In
* which one depends on the priority of the match.
* ht_match[] is used to find duplicates, ga_match[] to keep them in sequence.
* At the end, all the matches from ga_match[] are concatenated, to make a list
* sorted on priority.
*/
#define MT_ST_CUR 0 // static match in current file
#define MT_GL_CUR 1 // global match in current file
#define MT_GL_OTH 2 // global match in other file
#define MT_ST_OTH 3 // static match in other file
#define MT_IC_OFF 4 // add for icase match
#define MT_RE_OFF 8 // add for regexp match
#define MT_MASK 7 // mask for printing priority
#define MT_COUNT 16
static char *mt_names[MT_COUNT/2] =
{"FSC", "F C", "F ", "FS ", " SC", " C", " ", " S "};
#define NOTAGFILE 99 // return value for jumpto_tag
static char_u *nofile_fname = NULL; // fname for NOTAGFILE error
static void taglen_advance(int l);
static int jumpto_tag(char_u *lbuf, int forceit, int keep_help);
#ifdef FEAT_EMACS_TAGS
static int parse_tag_line(char_u *lbuf, int is_etag, tagptrs_T *tagp);
#else
static int parse_tag_line(char_u *lbuf, tagptrs_T *tagp);
#endif
static int test_for_static(tagptrs_T *);
static int parse_match(char_u *lbuf, tagptrs_T *tagp);
static char_u *tag_full_fname(tagptrs_T *tagp);
static char_u *expand_tag_fname(char_u *fname, char_u *tag_fname, int expand);
#ifdef FEAT_EMACS_TAGS
static int test_for_current(int, char_u *, char_u *, char_u *, char_u *);
#else
static int test_for_current(char_u *, char_u *, char_u *, char_u *);
#endif
static int find_extra(char_u **pp);
static void print_tag_list(int new_tag, int use_tagstack, int num_matches, char_u **matches);
#if defined(FEAT_QUICKFIX) && defined(FEAT_EVAL)
static int add_llist_tags(char_u *tag, int num_matches, char_u **matches);
#endif
static void tagstack_clear_entry(taggy_T *item);
static char_u *tagmatchname = NULL; // name of last used tag
#if defined(FEAT_QUICKFIX)
/*
* Tag for preview window is remembered separately, to avoid messing up the
* normal tagstack.
*/
static taggy_T ptag_entry = {NULL, {{0, 0, 0}, 0}, 0, 0, NULL};
#endif
#ifdef FEAT_EVAL
static int tfu_in_use = FALSE; // disallow recursive call of tagfunc
static callback_T tfu_cb; // 'tagfunc' callback function
#endif
// Used instead of NUL to separate tag fields in the growarrays.
#define TAG_SEP 0x02
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Reads the 'tagfunc' option value and convert that to a callback value.
* Invoked when the 'tagfunc' option is set. The option value can be a name of
* a function (string), or function(<name>) or funcref(<name>) or a lambda.
*/
int
set_tagfunc_option(void)
{
#ifdef FEAT_EVAL
free_callback(&tfu_cb);
free_callback(&curbuf->b_tfu_cb);
if (*curbuf->b_p_tfu == NUL)
return OK;
if (option_set_callback_func(curbuf->b_p_tfu, &tfu_cb) == FAIL)
return FAIL;
copy_callback(&curbuf->b_tfu_cb, &tfu_cb);
#endif
return OK;
}
#endif
# if defined(EXITFREE) || defined(PROTO)
void
free_tagfunc_option(void)
{
# ifdef FEAT_EVAL
free_callback(&tfu_cb);
# endif
}
# endif
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Mark the global 'tagfunc' callback with 'copyID' so that it is not garbage
* collected.
*/
int
set_ref_in_tagfunc(int copyID UNUSED)
{
int abort = FALSE;
abort = set_ref_in_callback(&tfu_cb, copyID);
return abort;
}
/*
* Copy the global 'tagfunc' callback function to the buffer-local 'tagfunc'
* callback for 'buf'.
*/
void
set_buflocal_tfu_callback(buf_T *buf UNUSED)
{
free_callback(&buf->b_tfu_cb);
if (tfu_cb.cb_name != NULL && *tfu_cb.cb_name != NUL)
copy_callback(&buf->b_tfu_cb, &tfu_cb);
}
#endif
/*
* Jump to tag; handling of tag commands and tag stack
*
* *tag != NUL: ":tag {tag}", jump to new tag, add to tag stack
*
* type == DT_TAG: ":tag [tag]", jump to newer position or same tag again
* type == DT_HELP: like DT_TAG, but don't use regexp.
* type == DT_POP: ":pop" or CTRL-T, jump to old position
* type == DT_NEXT: jump to next match of same tag
* type == DT_PREV: jump to previous match of same tag
* type == DT_FIRST: jump to first match of same tag
* type == DT_LAST: jump to last match of same tag
* type == DT_SELECT: ":tselect [tag]", select tag from a list of all matches
* type == DT_JUMP: ":tjump [tag]", jump to tag or select tag from a list
* type == DT_CSCOPE: use cscope to find the tag
* type == DT_LTAG: use location list for displaying tag matches
* type == DT_FREE: free cached matches
*
* for cscope, returns TRUE if we jumped to tag or aborted, FALSE otherwise
*/
int
do_tag(
char_u *tag, // tag (pattern) to jump to
int type,
int count,
int forceit, // :ta with !
int verbose) // print "tag not found" message
{
taggy_T *tagstack = curwin->w_tagstack;
int tagstackidx = curwin->w_tagstackidx;
int tagstacklen = curwin->w_tagstacklen;
int cur_match = 0;
int cur_fnum = curbuf->b_fnum;
int oldtagstackidx = tagstackidx;
int prevtagstackidx = tagstackidx;
int prev_num_matches;
int new_tag = FALSE;
int i;
int ic;
int no_regexp = FALSE;
int error_cur_match = 0;
int save_pos = FALSE;
fmark_T saved_fmark;
#ifdef FEAT_CSCOPE
int jumped_to_tag = FALSE;
#endif
int new_num_matches;
char_u **new_matches;
int use_tagstack;
int skip_msg = FALSE;
char_u *buf_ffname = curbuf->b_ffname; // name to use for
// priority computation
int use_tfu = 1;
// remember the matches for the last used tag
static int num_matches = 0;
static int max_num_matches = 0; // limit used for match search
static char_u **matches = NULL;
static int flags;
#ifdef FEAT_EVAL
if (tfu_in_use)
{
emsg(_(e_cannot_modify_tag_stack_within_tagfunc));
return FALSE;
}
#endif
#ifdef EXITFREE
if (type == DT_FREE)
{
// remove the list of matches
FreeWild(num_matches, matches);
# ifdef FEAT_CSCOPE
cs_free_tags();
# endif
num_matches = 0;
return FALSE;
}
#endif
if (type == DT_HELP)
{
type = DT_TAG;
no_regexp = TRUE;
use_tfu = 0;
}
prev_num_matches = num_matches;
free_string_option(nofile_fname);
nofile_fname = NULL;
CLEAR_POS(&saved_fmark.mark); // shutup gcc 4.0
saved_fmark.fnum = 0;
/*
* Don't add a tag to the tagstack if 'tagstack' has been reset.
*/
if ((!p_tgst && *tag != NUL))
{
use_tagstack = FALSE;
new_tag = TRUE;
#if defined(FEAT_QUICKFIX)
if (g_do_tagpreview != 0)
{
tagstack_clear_entry(&ptag_entry);
if ((ptag_entry.tagname = vim_strsave(tag)) == NULL)
goto end_do_tag;
}
#endif
}
else
{
#if defined(FEAT_QUICKFIX)
if (g_do_tagpreview != 0)
use_tagstack = FALSE;
else
#endif
use_tagstack = TRUE;
// new pattern, add to the tag stack
if (*tag != NUL
&& (type == DT_TAG || type == DT_SELECT || type == DT_JUMP
#ifdef FEAT_QUICKFIX
|| type == DT_LTAG
#endif
#ifdef FEAT_CSCOPE
|| type == DT_CSCOPE
#endif
))
{
#if defined(FEAT_QUICKFIX)
if (g_do_tagpreview != 0)
{
if (ptag_entry.tagname != NULL
&& STRCMP(ptag_entry.tagname, tag) == 0)
{
// Jumping to same tag: keep the current match, so that
// the CursorHold autocommand example works.
cur_match = ptag_entry.cur_match;
cur_fnum = ptag_entry.cur_fnum;
}
else
{
tagstack_clear_entry(&ptag_entry);
if ((ptag_entry.tagname = vim_strsave(tag)) == NULL)
goto end_do_tag;
}
}
else
#endif
{
/*
* If the last used entry is not at the top, delete all tag
* stack entries above it.
*/
while (tagstackidx < tagstacklen)
tagstack_clear_entry(&tagstack[--tagstacklen]);
// if the tagstack is full: remove oldest entry
if (++tagstacklen > TAGSTACKSIZE)
{
tagstacklen = TAGSTACKSIZE;
tagstack_clear_entry(&tagstack[0]);
for (i = 1; i < tagstacklen; ++i)
tagstack[i - 1] = tagstack[i];
--tagstackidx;
}
/*
* put the tag name in the tag stack
*/
if ((tagstack[tagstackidx].tagname = vim_strsave(tag)) == NULL)
{
curwin->w_tagstacklen = tagstacklen - 1;
goto end_do_tag;
}
curwin->w_tagstacklen = tagstacklen;
save_pos = TRUE; // save the cursor position below
}
new_tag = TRUE;
}
else
{
if (
#if defined(FEAT_QUICKFIX)
g_do_tagpreview != 0 ? ptag_entry.tagname == NULL :
#endif
tagstacklen == 0)
{
// empty stack
emsg(_(e_tag_stack_empty));
goto end_do_tag;
}
if (type == DT_POP) // go to older position
{
#ifdef FEAT_FOLDING
int old_KeyTyped = KeyTyped;
#endif
if ((tagstackidx -= count) < 0)
{
emsg(_(e_at_bottom_of_tag_stack));
if (tagstackidx + count == 0)
{
// We did [num]^T from the bottom of the stack
tagstackidx = 0;
goto end_do_tag;
}
// We weren't at the bottom of the stack, so jump all the
// way to the bottom now.
tagstackidx = 0;
}
else if (tagstackidx >= tagstacklen) // count == 0?
{
emsg(_(e_at_top_of_tag_stack));
goto end_do_tag;
}
// Make a copy of the fmark, autocommands may invalidate the
// tagstack before it's used.
saved_fmark = tagstack[tagstackidx].fmark;
if (saved_fmark.fnum != curbuf->b_fnum)
{
/*
* Jump to other file. If this fails (e.g. because the
* file was changed) keep original position in tag stack.
*/
if (buflist_getfile(saved_fmark.fnum, saved_fmark.mark.lnum,
GETF_SETMARK, forceit) == FAIL)
{
tagstackidx = oldtagstackidx; // back to old posn
goto end_do_tag;
}
// An BufReadPost autocommand may jump to the '" mark, but
// we don't what that here.
curwin->w_cursor.lnum = saved_fmark.mark.lnum;
}
else
{
setpcmark();
curwin->w_cursor.lnum = saved_fmark.mark.lnum;
}
curwin->w_cursor.col = saved_fmark.mark.col;
curwin->w_set_curswant = TRUE;
check_cursor();
#ifdef FEAT_FOLDING
if ((fdo_flags & FDO_TAG) && old_KeyTyped)
foldOpenCursor();
#endif
// remove the old list of matches
FreeWild(num_matches, matches);
#ifdef FEAT_CSCOPE
cs_free_tags();
#endif
num_matches = 0;
tag_freematch();
goto end_do_tag;
}
if (type == DT_TAG
#if defined(FEAT_QUICKFIX)
|| type == DT_LTAG
#endif
)
{
#if defined(FEAT_QUICKFIX)
if (g_do_tagpreview != 0)
{
cur_match = ptag_entry.cur_match;
cur_fnum = ptag_entry.cur_fnum;
}
else
#endif
{
// ":tag" (no argument): go to newer pattern
save_pos = TRUE; // save the cursor position below
if ((tagstackidx += count - 1) >= tagstacklen)
{
/*
* Beyond the last one, just give an error message and
* go to the last one. Don't store the cursor
* position.
*/
tagstackidx = tagstacklen - 1;
emsg(_(e_at_top_of_tag_stack));
save_pos = FALSE;
}
else if (tagstackidx < 0) // must have been count == 0
{
emsg(_(e_at_bottom_of_tag_stack));
tagstackidx = 0;
goto end_do_tag;
}
cur_match = tagstack[tagstackidx].cur_match;
cur_fnum = tagstack[tagstackidx].cur_fnum;
}
new_tag = TRUE;
}
else // go to other matching tag
{
// Save index for when selection is cancelled.
prevtagstackidx = tagstackidx;
#if defined(FEAT_QUICKFIX)
if (g_do_tagpreview != 0)
{
cur_match = ptag_entry.cur_match;
cur_fnum = ptag_entry.cur_fnum;
}
else
#endif
{
if (--tagstackidx < 0)
tagstackidx = 0;
cur_match = tagstack[tagstackidx].cur_match;
cur_fnum = tagstack[tagstackidx].cur_fnum;
}
switch (type)
{
case DT_FIRST: cur_match = count - 1; break;
case DT_SELECT:
case DT_JUMP:
#ifdef FEAT_CSCOPE
case DT_CSCOPE:
#endif
case DT_LAST: cur_match = MAXCOL - 1; break;
case DT_NEXT: cur_match += count; break;
case DT_PREV: cur_match -= count; break;
}
if (cur_match >= MAXCOL)
cur_match = MAXCOL - 1;
else if (cur_match < 0)
{
emsg(_(e_cannot_go_before_first_matching_tag));
skip_msg = TRUE;
cur_match = 0;
cur_fnum = curbuf->b_fnum;
}
}
}
#if defined(FEAT_QUICKFIX)
if (g_do_tagpreview != 0)
{
if (type != DT_SELECT && type != DT_JUMP)
{
ptag_entry.cur_match = cur_match;
ptag_entry.cur_fnum = cur_fnum;
}
}
else
#endif
{
/*
* For ":tag [arg]" or ":tselect" remember position before the jump.
*/
saved_fmark = tagstack[tagstackidx].fmark;
if (save_pos)
{
tagstack[tagstackidx].fmark.mark = curwin->w_cursor;
tagstack[tagstackidx].fmark.fnum = curbuf->b_fnum;
}
// Curwin will change in the call to jumpto_tag() if ":stag" was
// used or an autocommand jumps to another window; store value of
// tagstackidx now.
curwin->w_tagstackidx = tagstackidx;
if (type != DT_SELECT && type != DT_JUMP)
{
curwin->w_tagstack[tagstackidx].cur_match = cur_match;
curwin->w_tagstack[tagstackidx].cur_fnum = cur_fnum;
}
}
}
// When not using the current buffer get the name of buffer "cur_fnum".
// Makes sure that the tag order doesn't change when using a remembered
// position for "cur_match".
if (cur_fnum != curbuf->b_fnum)
{
buf_T *buf = buflist_findnr(cur_fnum);
if (buf != NULL)
buf_ffname = buf->b_ffname;
}
/*
* Repeat searching for tags, when a file has not been found.
*/
for (;;)
{
int other_name;
char_u *name;
/*
* When desired match not found yet, try to find it (and others).
*/
if (use_tagstack)
name = tagstack[tagstackidx].tagname;
#if defined(FEAT_QUICKFIX)
else if (g_do_tagpreview != 0)
name = ptag_entry.tagname;
#endif
else
name = tag;
other_name = (tagmatchname == NULL || STRCMP(tagmatchname, name) != 0);
if (new_tag
|| (cur_match >= num_matches && max_num_matches != MAXCOL)
|| other_name)
{
if (other_name)
{
vim_free(tagmatchname);
tagmatchname = vim_strsave(name);
}
if (type == DT_SELECT || type == DT_JUMP
#if defined(FEAT_QUICKFIX)
|| type == DT_LTAG
#endif
)
cur_match = MAXCOL - 1;
if (type == DT_TAG)
max_num_matches = MAXCOL;
else
max_num_matches = cur_match + 1;
// when the argument starts with '/', use it as a regexp
if (!no_regexp && *name == '/')
{
flags = TAG_REGEXP;
++name;
}
else
flags = TAG_NOIC;
#ifdef FEAT_CSCOPE
if (type == DT_CSCOPE)
flags = TAG_CSCOPE;
#endif
if (verbose)
flags |= TAG_VERBOSE;
if (!use_tfu)
flags |= TAG_NO_TAGFUNC;
if (find_tags(name, &new_num_matches, &new_matches, flags,
max_num_matches, buf_ffname) == OK
&& new_num_matches < max_num_matches)
max_num_matches = MAXCOL; // If less than max_num_matches
// found: all matches found.
// If there already were some matches for the same name, move them
// to the start. Avoids that the order changes when using
// ":tnext" and jumping to another file.
if (!new_tag && !other_name)
{
int j, k;
int idx = 0;
tagptrs_T tagp, tagp2;
// Find the position of each old match in the new list. Need
// to use parse_match() to find the tag line.
for (j = 0; j < num_matches; ++j)
{
parse_match(matches[j], &tagp);
for (i = idx; i < new_num_matches; ++i)
{
parse_match(new_matches[i], &tagp2);
if (STRCMP(tagp.tagname, tagp2.tagname) == 0)
{
char_u *p = new_matches[i];
for (k = i; k > idx; --k)
new_matches[k] = new_matches[k - 1];
new_matches[idx++] = p;
break;
}
}
}
}
FreeWild(num_matches, matches);
num_matches = new_num_matches;
matches = new_matches;
}
if (num_matches <= 0)
{
if (verbose)
semsg(_(e_tag_not_found_str), name);
#if defined(FEAT_QUICKFIX)
g_do_tagpreview = 0;
#endif
}
else
{
int ask_for_selection = FALSE;
#ifdef FEAT_CSCOPE
if (type == DT_CSCOPE && num_matches > 1)
{
cs_print_tags();
ask_for_selection = TRUE;
}
else
#endif
if (type == DT_TAG && *tag != NUL)
// If a count is supplied to the ":tag <name>" command, then
// jump to count'th matching tag.
cur_match = count > 0 ? count - 1 : 0;
else if (type == DT_SELECT || (type == DT_JUMP && num_matches > 1))
{
print_tag_list(new_tag, use_tagstack, num_matches, matches);
ask_for_selection = TRUE;
}
#if defined(FEAT_QUICKFIX) && defined(FEAT_EVAL)
else if (type == DT_LTAG)
{
if (add_llist_tags(tag, num_matches, matches) == FAIL)
goto end_do_tag;
cur_match = 0; // Jump to the first tag
}
#endif
if (ask_for_selection == TRUE)
{
/*
* Ask to select a tag from the list.
*/
i = prompt_for_number(NULL);
if (i <= 0 || i > num_matches || got_int)
{
// no valid choice: don't change anything
if (use_tagstack)
{
tagstack[tagstackidx].fmark = saved_fmark;
tagstackidx = prevtagstackidx;
}
#ifdef FEAT_CSCOPE
cs_free_tags();
jumped_to_tag = TRUE;
#endif
break;
}
cur_match = i - 1;
}
if (cur_match >= num_matches)
{
// Avoid giving this error when a file wasn't found and we're
// looking for a match in another file, which wasn't found.
// There will be an emsg("file doesn't exist") below then.
if ((type == DT_NEXT || type == DT_FIRST)
&& nofile_fname == NULL)
{
if (num_matches == 1)
emsg(_(e_there_is_only_one_matching_tag));
else
emsg(_(e_cannot_go_beyond_last_matching_tag));
skip_msg = TRUE;
}
cur_match = num_matches - 1;
}
if (use_tagstack)
{
tagptrs_T tagp;
tagstack[tagstackidx].cur_match = cur_match;
tagstack[tagstackidx].cur_fnum = cur_fnum;
// store user-provided data originating from tagfunc
if (use_tfu && parse_match(matches[cur_match], &tagp) == OK
&& tagp.user_data)
{
VIM_CLEAR(tagstack[tagstackidx].user_data);
tagstack[tagstackidx].user_data = vim_strnsave(
tagp.user_data, tagp.user_data_end - tagp.user_data);
}
++tagstackidx;
}
#if defined(FEAT_QUICKFIX)
else if (g_do_tagpreview != 0)
{
ptag_entry.cur_match = cur_match;
ptag_entry.cur_fnum = cur_fnum;
}
#endif
/*
* Only when going to try the next match, report that the previous
* file didn't exist. Otherwise an emsg() is given below.
*/
if (nofile_fname != NULL && error_cur_match != cur_match)
smsg(_("File \"%s\" does not exist"), nofile_fname);
ic = (matches[cur_match][0] & MT_IC_OFF);
if (type != DT_TAG && type != DT_SELECT && type != DT_JUMP
#ifdef FEAT_CSCOPE
&& type != DT_CSCOPE
#endif
&& (num_matches > 1 || ic)
&& !skip_msg)
{
// Give an indication of the number of matching tags
sprintf((char *)IObuff, _("tag %d of %d%s"),
cur_match + 1,
num_matches,
max_num_matches != MAXCOL ? _(" or more") : "");
if (ic)
STRCAT(IObuff, _(" Using tag with different case!"));
if ((num_matches > prev_num_matches || new_tag)
&& num_matches > 1)
{
if (ic)
msg_attr((char *)IObuff, HL_ATTR(HLF_W));
else
msg((char *)IObuff);
msg_scroll = TRUE; // don't overwrite this message
}
else
give_warning(IObuff, ic);
if (ic && !msg_scrolled && msg_silent == 0)
{
out_flush();
ui_delay(1007L, TRUE);
}
}
#if defined(FEAT_EVAL)
// Let the SwapExists event know what tag we are jumping to.
vim_snprintf((char *)IObuff, IOSIZE, ":ta %s\r", name);
set_vim_var_string(VV_SWAPCOMMAND, IObuff, -1);
#endif
/*
* Jump to the desired match.
*/
i = jumpto_tag(matches[cur_match], forceit, type != DT_CSCOPE);
#if defined(FEAT_EVAL)
set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
#endif
if (i == NOTAGFILE)
{
// File not found: try again with another matching tag
if ((type == DT_PREV && cur_match > 0)
|| ((type == DT_TAG || type == DT_NEXT
|| type == DT_FIRST)
&& (max_num_matches != MAXCOL
|| cur_match < num_matches - 1)))
{
error_cur_match = cur_match;
if (use_tagstack)
--tagstackidx;
if (type == DT_PREV)
--cur_match;
else
{
type = DT_NEXT;
++cur_match;
}
continue;
}
semsg(_(e_file_str_does_not_exist), nofile_fname);
}
else
{
// We may have jumped to another window, check that
// tagstackidx is still valid.
if (use_tagstack && tagstackidx > curwin->w_tagstacklen)
tagstackidx = curwin->w_tagstackidx;
#ifdef FEAT_CSCOPE
jumped_to_tag = TRUE;
#endif
}
}
break;
}
end_do_tag:
// Only store the new index when using the tagstack and it's valid.
if (use_tagstack && tagstackidx <= curwin->w_tagstacklen)
curwin->w_tagstackidx = tagstackidx;
postponed_split = 0; // don't split next time
# ifdef FEAT_QUICKFIX
g_do_tagpreview = 0; // don't do tag preview next time
# endif
#ifdef FEAT_CSCOPE
return jumped_to_tag;
#else
return FALSE;
#endif
}
/*
* List all the matching tags.
*/
static void
print_tag_list(
int new_tag,
int use_tagstack,
int num_matches,
char_u **matches)
{
taggy_T *tagstack = curwin->w_tagstack;
int tagstackidx = curwin->w_tagstackidx;
int i;
char_u *p;
char_u *command_end;
tagptrs_T tagp;
int taglen;
int attr;
/*
* Assume that the first match indicates how long the tags can
* be, and align the file names to that.
*/
parse_match(matches[0], &tagp);
taglen = (int)(tagp.tagname_end - tagp.tagname + 2);
if (taglen < 18)
taglen = 18;
if (taglen > Columns - 25)
taglen = MAXCOL;
if (msg_col == 0)
msg_didout = FALSE; // overwrite previous message
msg_start();
msg_puts_attr(_(" # pri kind tag"), HL_ATTR(HLF_T));
msg_clr_eos();
taglen_advance(taglen);
msg_puts_attr(_("file\n"), HL_ATTR(HLF_T));
for (i = 0; i < num_matches && !got_int; ++i)
{
parse_match(matches[i], &tagp);
if (!new_tag && (
#if defined(FEAT_QUICKFIX)
(g_do_tagpreview != 0
&& i == ptag_entry.cur_match) ||
#endif
(use_tagstack
&& i == tagstack[tagstackidx].cur_match)))
*IObuff = '>';
else
*IObuff = ' ';
vim_snprintf((char *)IObuff + 1, IOSIZE - 1,
"%2d %s ", i + 1,
mt_names[matches[i][0] & MT_MASK]);
msg_puts((char *)IObuff);
if (tagp.tagkind != NULL)
msg_outtrans_len(tagp.tagkind,
(int)(tagp.tagkind_end - tagp.tagkind));
msg_advance(13);
msg_outtrans_len_attr(tagp.tagname,
(int)(tagp.tagname_end - tagp.tagname),
HL_ATTR(HLF_T));
msg_putchar(' ');
taglen_advance(taglen);
// Find out the actual file name. If it is long, truncate
// it and put "..." in the middle
p = tag_full_fname(&tagp);
if (p != NULL)
{
msg_outtrans_long_attr(p, HL_ATTR(HLF_D));
vim_free(p);
}
if (msg_col > 0)
msg_putchar('\n');
if (got_int)
break;
msg_advance(15);
// print any extra fields
command_end = tagp.command_end;
if (command_end != NULL)
{
p = command_end + 3;
while (*p && *p != '\r' && *p != '\n')
{
while (*p == TAB)
++p;
// skip "file:" without a value (static tag)
if (STRNCMP(p, "file:", 5) == 0
&& vim_isspace(p[5]))
{
p += 5;
continue;
}
// skip "kind:<kind>" and "<kind>"
if (p == tagp.tagkind
|| (p + 5 == tagp.tagkind
&& STRNCMP(p, "kind:", 5) == 0))
{
p = tagp.tagkind_end;
continue;
}
// print all other extra fields
attr = HL_ATTR(HLF_CM);
while (*p && *p != '\r' && *p != '\n')
{
if (msg_col + ptr2cells(p) >= Columns)
{
msg_putchar('\n');
if (got_int)
break;
msg_advance(15);
}
p = msg_outtrans_one(p, attr);
if (*p == TAB)
{
msg_puts_attr(" ", attr);
break;
}
if (*p == ':')
attr = 0;
}
}
if (msg_col > 15)
{
msg_putchar('\n');
if (got_int)
break;
msg_advance(15);
}
}
else
{
for (p = tagp.command;
*p && *p != '\r' && *p != '\n'; ++p)
;
command_end = p;
}
// Put the info (in several lines) at column 15.
// Don't display "/^" and "?^".
p = tagp.command;
if (*p == '/' || *p == '?')
{
++p;
if (*p == '^')
++p;
}
// Remove leading whitespace from pattern
while (p != command_end && vim_isspace(*p))
++p;
while (p != command_end)
{
if (msg_col + (*p == TAB ? 1 : ptr2cells(p)) > Columns)
msg_putchar('\n');
if (got_int)
break;
msg_advance(15);
// skip backslash used for escaping a command char or
// a backslash
if (*p == '\\' && (*(p + 1) == *tagp.command
|| *(p + 1) == '\\'))
++p;
if (*p == TAB)
{
msg_putchar(' ');
++p;
}
else
p = msg_outtrans_one(p, 0);
// don't display the "$/;\"" and "$?;\""
if (p == command_end - 2 && *p == '$'
&& *(p + 1) == *tagp.command)
break;
// don't display matching '/' or '?'
if (p == command_end - 1 && *p == *tagp.command
&& (*p == '/' || *p == '?'))
break;
}
if (msg_col)
msg_putchar('\n');
ui_breakcheck();
}
if (got_int)
got_int = FALSE; // only stop the listing
}
#if defined(FEAT_QUICKFIX) && defined(FEAT_EVAL)
/*
* Add the matching tags to the location list for the current
* window.
*/
static int
add_llist_tags(
char_u *tag,
int num_matches,
char_u **matches)
{
list_T *list;
char_u tag_name[128 + 1];
char_u *fname;
char_u *cmd;
int i;
char_u *p;
tagptrs_T tagp;
fname = alloc(MAXPATHL + 1);
cmd = alloc(CMDBUFFSIZE + 1);
list = list_alloc();
if (list == NULL || fname == NULL || cmd == NULL)
{
vim_free(cmd);
vim_free(fname);
if (list != NULL)
list_free(list);
return FAIL;
}
for (i = 0; i < num_matches; ++i)
{
int len, cmd_len;
long lnum;
dict_T *dict;
parse_match(matches[i], &tagp);
// Save the tag name
len = (int)(tagp.tagname_end - tagp.tagname);
if (len > 128)
len = 128;
vim_strncpy(tag_name, tagp.tagname, len);
tag_name[len] = NUL;
// Save the tag file name
p = tag_full_fname(&tagp);
if (p == NULL)
continue;
vim_strncpy(fname, p, MAXPATHL);
vim_free(p);
// Get the line number or the search pattern used to locate
// the tag.
lnum = 0;
if (isdigit(*tagp.command))
// Line number is used to locate the tag
lnum = atol((char *)tagp.command);
else
{
char_u *cmd_start, *cmd_end;
// Search pattern is used to locate the tag
// Locate the end of the command
cmd_start = tagp.command;
cmd_end = tagp.command_end;
if (cmd_end == NULL)
{
for (p = tagp.command;
*p && *p != '\r' && *p != '\n'; ++p)
;
cmd_end = p;
}
// Now, cmd_end points to the character after the
// command. Adjust it to point to the last
// character of the command.
cmd_end--;
// Skip the '/' and '?' characters at the
// beginning and end of the search pattern.
if (*cmd_start == '/' || *cmd_start == '?')
cmd_start++;
if (*cmd_end == '/' || *cmd_end == '?')
cmd_end--;
len = 0;
cmd[0] = NUL;
// If "^" is present in the tag search pattern, then
// copy it first.
if (*cmd_start == '^')
{
STRCPY(cmd, "^");
cmd_start++;
len++;
}
// Precede the tag pattern with \V to make it very
// nomagic.
STRCAT(cmd, "\\V");
len += 2;
cmd_len = (int)(cmd_end - cmd_start + 1);
if (cmd_len > (CMDBUFFSIZE - 5))
cmd_len = CMDBUFFSIZE - 5;
STRNCAT(cmd, cmd_start, cmd_len);
len += cmd_len;
if (cmd[len - 1] == '$')
{
// Replace '$' at the end of the search pattern
// with '\$'
cmd[len - 1] = '\\';
cmd[len] = '$';
len++;
}
cmd[len] = NUL;
}
if ((dict = dict_alloc()) == NULL)
continue;
if (list_append_dict(list, dict) == FAIL)
{
vim_free(dict);
continue;
}
dict_add_string(dict, "text", tag_name);
dict_add_string(dict, "filename", fname);
dict_add_number(dict, "lnum", lnum);
if (lnum == 0)
dict_add_string(dict, "pattern", cmd);
}
vim_snprintf((char *)IObuff, IOSIZE, "ltag %s", tag);
set_errorlist(curwin, list, ' ', IObuff, NULL);
list_free(list);
vim_free(fname);
vim_free(cmd);
return OK;
}
#endif
/*
* Free cached tags.
*/
void
tag_freematch(void)
{
VIM_CLEAR(tagmatchname);
}
static void
taglen_advance(int l)
{
if (l == MAXCOL)
{
msg_putchar('\n');
msg_advance(24);
}
else
msg_advance(13 + l);
}
/*
* Print the tag stack
*/
void
do_tags(exarg_T *eap UNUSED)
{
int i;
char_u *name;
taggy_T *tagstack = curwin->w_tagstack;
int tagstackidx = curwin->w_tagstackidx;
int tagstacklen = curwin->w_tagstacklen;
// Highlight title
msg_puts_title(_("\n # TO tag FROM line in file/text"));
for (i = 0; i < tagstacklen; ++i)
{
if (tagstack[i].tagname != NULL)
{
name = fm_getname(&(tagstack[i].fmark), 30);
if (name == NULL) // file name not available
continue;
msg_putchar('\n');
vim_snprintf((char *)IObuff, IOSIZE, "%c%2d %2d %-15s %5ld ",
i == tagstackidx ? '>' : ' ',
i + 1,
tagstack[i].cur_match + 1,
tagstack[i].tagname,
tagstack[i].fmark.mark.lnum);
msg_outtrans(IObuff);
msg_outtrans_attr(name, tagstack[i].fmark.fnum == curbuf->b_fnum
? HL_ATTR(HLF_D) : 0);
vim_free(name);
}
out_flush(); // show one line at a time
}
if (tagstackidx == tagstacklen) // idx at top of stack
msg_puts("\n>");
}
/*
* Compare two strings, for length "len", ignoring case the ASCII way.
* return 0 for match, < 0 for smaller, > 0 for bigger
* Make sure case is folded to uppercase in comparison (like for 'sort -f')
*/
static int
tag_strnicmp(char_u *s1, char_u *s2, size_t len)
{
int i;
while (len > 0)
{
i = (int)TOUPPER_ASC(*s1) - (int)TOUPPER_ASC(*s2);
if (i != 0)
return i; // this character different
if (*s1 == NUL)
break; // strings match until NUL
++s1;
++s2;
--len;
}
return 0; // strings match
}
/*
* Structure to hold info about the tag pattern being used.
*/
typedef struct
{
char_u *pat; // the pattern
int len; // length of pat[]
char_u *head; // start of pattern head
int headlen; // length of head[]
regmatch_T regmatch; // regexp program, may be NULL
} pat_T;
/*
* Extract info from the tag search pattern "pats->pat".
*/
static void
prepare_pats(pat_T *pats, int has_re)
{
pats->head = pats->pat;
pats->headlen = pats->len;
if (has_re)
{
// When the pattern starts with '^' or "\\<", binary searching can be
// used (much faster).
if (pats->pat[0] == '^')
pats->head = pats->pat + 1;
else if (pats->pat[0] == '\\' && pats->pat[1] == '<')
pats->head = pats->pat + 2;
if (pats->head == pats->pat)
pats->headlen = 0;
else
for (pats->headlen = 0; pats->head[pats->headlen] != NUL;
++pats->headlen)
if (vim_strchr((char_u *)(magic_isset() ? ".[~*\\$" : "\\$"),
pats->head[pats->headlen]) != NULL)
break;
if (p_tl != 0 && pats->headlen > p_tl) // adjust for 'taglength'
pats->headlen = p_tl;
}
if (has_re)
pats->regmatch.regprog = vim_regcomp(pats->pat,
magic_isset() ? RE_MAGIC : 0);
else
pats->regmatch.regprog = NULL;
}
#ifdef FEAT_EVAL
/*
* Call the user-defined function to generate a list of tags used by
* find_tags().
*
* Return OK if at least 1 tag has been successfully found,
* NOTDONE if the function returns v:null, and FAIL otherwise.
*/
static int
find_tagfunc_tags(
char_u *pat, // pattern supplied to the user-defined function
garray_T *ga, // the tags will be placed here
int *match_count, // here the number of tags found will be placed
int flags, // flags from find_tags (TAG_*)
char_u *buf_ffname) // name of buffer for priority
{
pos_T save_pos;
list_T *taglist;
listitem_T *item;
int ntags = 0;
int result = FAIL;
typval_T args[4];
typval_T rettv;
char_u flagString[4];
dict_T *d;
taggy_T *tag = &curwin->w_tagstack[curwin->w_tagstackidx];
if (*curbuf->b_p_tfu == NUL || curbuf->b_tfu_cb.cb_name == NULL
|| *curbuf->b_tfu_cb.cb_name == NUL)
return FAIL;
args[0].v_type = VAR_STRING;
args[0].vval.v_string = pat;
args[1].v_type = VAR_STRING;
args[1].vval.v_string = flagString;
// create 'info' dict argument
if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
return FAIL;
if (tag->user_data != NULL)
dict_add_string(d, "user_data", tag->user_data);
if (buf_ffname != NULL)
dict_add_string(d, "buf_ffname", buf_ffname);
++d->dv_refcount;
args[2].v_type = VAR_DICT;
args[2].vval.v_dict = d;
args[3].v_type = VAR_UNKNOWN;
vim_snprintf((char *)flagString, sizeof(flagString),
"%s%s%s",
g_tag_at_cursor ? "c": "",
flags & TAG_INS_COMP ? "i": "",
flags & TAG_REGEXP ? "r": "");
save_pos = curwin->w_cursor;
result = call_callback(&curbuf->b_tfu_cb, 0, &rettv, 3, args);
curwin->w_cursor = save_pos; // restore the cursor position
--d->dv_refcount;
if (result == FAIL)
return FAIL;
if (rettv.v_type == VAR_SPECIAL && rettv.vval.v_number == VVAL_NULL)
{
clear_tv(&rettv);
return NOTDONE;
}
if (rettv.v_type != VAR_LIST || !rettv.vval.v_list)
{
clear_tv(&rettv);
emsg(_(e_invalid_return_value_from_tagfunc));
return FAIL;
}
taglist = rettv.vval.v_list;
FOR_ALL_LIST_ITEMS(taglist, item)
{
char_u *mfp;
char_u *res_name, *res_fname, *res_cmd, *res_kind;
int len;
dict_iterator_T iter;
char_u *dict_key;
typval_T *tv;
int has_extra = 0;
int name_only = flags & TAG_NAMES;
if (item->li_tv.v_type != VAR_DICT)
{
emsg(_(e_invalid_return_value_from_tagfunc));
break;
}
#ifdef FEAT_EMACS_TAGS
len = 3;
#else
len = 2;
#endif
res_name = NULL;
res_fname = NULL;
res_cmd = NULL;
res_kind = NULL;
dict_iterate_start(&item->li_tv, &iter);
while (NULL != (dict_key = dict_iterate_next(&iter, &tv)))
{
if (tv->v_type != VAR_STRING || tv->vval.v_string == NULL)
continue;
len += (int)STRLEN(tv->vval.v_string) + 1; // Space for "\tVALUE"
if (!STRCMP(dict_key, "name"))
{
res_name = tv->vval.v_string;
continue;
}
if (!STRCMP(dict_key, "filename"))
{
res_fname = tv->vval.v_string;
continue;
}
if (!STRCMP(dict_key, "cmd"))
{
res_cmd = tv->vval.v_string;
continue;
}
has_extra = 1;
if (!STRCMP(dict_key, "kind"))
{
res_kind = tv->vval.v_string;
continue;
}
// Other elements will be stored as "\tKEY:VALUE"
// Allocate space for the key and the colon
len += (int)STRLEN(dict_key) + 1;
}
if (has_extra)
len += 2; // need space for ;"
if (!res_name || !res_fname || !res_cmd)
{
emsg(_(e_invalid_return_value_from_tagfunc));
break;
}
if (name_only)
mfp = vim_strsave(res_name);
else
mfp = alloc(sizeof(char_u) + len + 1);
if (mfp == NULL)
continue;
if (!name_only)
{
char_u *p = mfp;
*p++ = MT_GL_OTH + 1; // mtt
*p++ = TAG_SEP; // no tag file name
#ifdef FEAT_EMACS_TAGS
*p++ = TAG_SEP;
#endif
STRCPY(p, res_name);
p += STRLEN(p);
*p++ = TAB;
STRCPY(p, res_fname);
p += STRLEN(p);
*p++ = TAB;
STRCPY(p, res_cmd);
p += STRLEN(p);
if (has_extra)
{
STRCPY(p, ";\"");
p += STRLEN(p);
if (res_kind)
{
*p++ = TAB;
STRCPY(p, res_kind);
p += STRLEN(p);
}
dict_iterate_start(&item->li_tv, &iter);
while (NULL != (dict_key = dict_iterate_next(&iter, &tv)))
{
if (tv->v_type != VAR_STRING || tv->vval.v_string == NULL)
continue;
if (!STRCMP(dict_key, "name"))
continue;
if (!STRCMP(dict_key, "filename"))
continue;
if (!STRCMP(dict_key, "cmd"))
continue;
if (!STRCMP(dict_key, "kind"))
continue;
*p++ = TAB;
STRCPY(p, dict_key);
p += STRLEN(p);
STRCPY(p, ":");
p += STRLEN(p);
STRCPY(p, tv->vval.v_string);
p += STRLEN(p);
}
}
}
// Add all matches because tagfunc should do filtering.
if (ga_grow(ga, 1) == OK)
{
((char_u **)(ga->ga_data))[ga->ga_len++] = mfp;
++ntags;
result = OK;
}
else
{
vim_free(mfp);
break;
}
}
clear_tv(&rettv);
*match_count = ntags;
return result;
}
#endif
/*
* State information used during a tag search
*/
typedef struct
{
tagsearch_state_T state; // tag search state
char_u *tag_fname; // name of the tag file
FILE *fp; // current tags file pointer
pat_T orgpat; // holds unconverted pattern info
int flags; // flags used for tag search
int tag_file_sorted; // !_TAG_FILE_SORTED value
int get_searchpat; // used for 'showfulltag'
int help_only; // only search for help tags
vimconv_T vimconv;
#ifdef FEAT_MULTI_LANG
char_u help_lang[3]; // lang of current tags file
int help_pri; // help language priority
char_u *help_lang_find; // lang to be found
int is_txt; // flag of file extension
#endif
int did_open; // did open a tag file
int mincount; // MAXCOL: find all matches
// other: minimal number of matches
int linear; // do a linear search
char_u *lbuf; // line buffer
int lbuf_size; // length of lbuf
#ifdef FEAT_EMACS_TAGS
int is_etag; // current file is emacs style
char_u *ebuf; // additional buffer for etag fname
#endif
int match_count; // number of matches found
garray_T ga_match[MT_COUNT]; // stores matches in sequence
hashtab_T ht_match[MT_COUNT]; // stores matches by key
int stop_searching; // stop when match found or error
} findtags_state_T;
/*
* Initialize the state used by find_tags().
* Returns OK on success and FAIL on memory allocation failure.
*/
static int
findtags_state_init(
findtags_state_T *st,
char_u *pat,
int flags,
int mincount)
{
int mtt;
st->tag_fname = alloc(MAXPATHL + 1);
st->fp = NULL;
st->orgpat.pat = pat;
st->orgpat.len = (int)STRLEN(pat);
st->orgpat.regmatch.regprog = NULL;
st->flags = flags;
st->tag_file_sorted = NUL;
st->help_only = (flags & TAG_HELP);
st->get_searchpat = FALSE;
#ifdef FEAT_MULTI_LANG
st->help_lang[0] = NUL;
st->help_pri = 0;
st->help_lang_find = NULL;
st->is_txt = FALSE;
#endif
st->did_open = FALSE;
st->mincount = mincount;
st->lbuf_size = LSIZE;
st->lbuf = alloc(st->lbuf_size);
#ifdef FEAT_EMACS_TAGS
st->ebuf = alloc(LSIZE);
#endif
st->match_count = 0;
st->stop_searching = FALSE;
for (mtt = 0; mtt < MT_COUNT; ++mtt)
{
ga_init2(&st->ga_match[mtt], sizeof(char_u *), 100);
hash_init(&st->ht_match[mtt]);
}
// check for out of memory situation
if (st->tag_fname == NULL
|| st->lbuf == NULL
#ifdef FEAT_EMACS_TAGS
|| st->ebuf == NULL
#endif
)
return FAIL;
return OK;
}
/*
* Free the state used by find_tags()
*/
static void
findtags_state_free(findtags_state_T *st)
{
vim_free(st->tag_fname);
vim_free(st->lbuf);
vim_regfree(st->orgpat.regmatch.regprog);
#ifdef FEAT_EMACS_TAGS
vim_free(st->ebuf);
#endif
}
#ifdef FEAT_MULTI_LANG
/*
* Initialize the language and priority used for searching tags in a Vim help
* file.
* Returns TRUE to process the help file for tags and FALSE to skip the file.
*/
static int
findtags_in_help_init(findtags_state_T *st)
{
int i;
char_u *s;
// Keep 'en' as the language if the file extension is '.txt'
if (st->is_txt)
STRCPY(st->help_lang, "en");
else
{
// Prefer help tags according to 'helplang'. Put the two-letter
// language name in help_lang[].
i = (int)STRLEN(st->tag_fname);
if (i > 3 && st->tag_fname[i - 3] == '-')
vim_strncpy(st->help_lang, st->tag_fname + i - 2, 2);
else
STRCPY(st->help_lang, "en");
}
// When searching for a specific language skip tags files for other
// languages.
if (st->help_lang_find != NULL
&& STRICMP(st->help_lang, st->help_lang_find) != 0)
return FALSE;
// For CTRL-] in a help file prefer a match with the same language.
if ((st->flags & TAG_KEEP_LANG)
&& st->help_lang_find == NULL
&& curbuf->b_fname != NULL
&& (i = (int)STRLEN(curbuf->b_fname)) > 4
&& curbuf->b_fname[i - 1] == 'x'
&& curbuf->b_fname[i - 4] == '.'
&& STRNICMP(curbuf->b_fname + i - 3, st->help_lang, 2) == 0)
st->help_pri = 0;
else
{
// search for the language in 'helplang'
st->help_pri = 1;
for (s = p_hlg; *s != NUL; ++s)
{
if (STRNICMP(s, st->help_lang, 2) == 0)
break;
++st->help_pri;
if ((s = vim_strchr(s, ',')) == NULL)
break;
}
if (s == NULL || *s == NUL)
{
// Language not in 'helplang': use last, prefer English, unless
// found already.
++st->help_pri;
if (STRICMP(st->help_lang, "en") != 0)
++st->help_pri;
}
}
return TRUE;
}
#endif
#ifdef FEAT_EVAL
/*
* Use the function set in 'tagfunc' (if configured and enabled) to get the
* tags.
* Return OK if at least 1 tag has been successfully found, NOTDONE if the
* 'tagfunc' is not used or the 'tagfunc' returns v:null and FAIL otherwise.
*/
static int
findtags_apply_tfu(findtags_state_T *st, char_u *pat, char_u *buf_ffname)
{
int use_tfu = ((st->flags & TAG_NO_TAGFUNC) == 0);
int retval;
if (!use_tfu || tfu_in_use || *curbuf->b_p_tfu == NUL)
return NOTDONE;
tfu_in_use = TRUE;
retval = find_tagfunc_tags(pat, st->ga_match, &st->match_count,
st->flags, buf_ffname);
tfu_in_use = FALSE;
return retval;
}
#endif
#ifdef FEAT_EMACS_TAGS
/*
* Stack for included emacs-tags file.
* It has a fixed size, to truncate cyclic includes. jw
*/
# define INCSTACK_SIZE 42
static struct
{
FILE *fp;
char_u *etag_fname;
} incstack[INCSTACK_SIZE];
static int incstack_idx = 0; // index in incstack
/*
* Free the emacs include tags file stack.
*/
static void
emacs_tags_incstack_free(void)
{
while (incstack_idx)
{
--incstack_idx;
fclose(incstack[incstack_idx].fp);
incstack[incstack_idx].fp = NULL;
VIM_CLEAR(incstack[incstack_idx].etag_fname);
}
}
/*
* Emacs tags line with CTRL-L: New file name on next line.
* The file name is followed by a ','. Remember etag file name in ebuf.
* The FILE pointer to the tags file is stored in 'st->fp'. If another tags
* file is included, then the FILE pointer to the new tags file is stored in
* 'st->fp'. The old file pointer is saved in incstack.
*/
static void
emacs_tags_new_filename(findtags_state_T *st)
{
char_u *p;
char_u *fullpath_ebuf;
if (vim_fgets(st->ebuf, LSIZE, st->fp))
return;
for (p = st->ebuf; *p && *p != ','; p++)
;
*p = NUL;
// check for an included tags file.
// atoi(p+1) is the number of bytes before the next ^L unless it is an
// include statement. Skip the included tags file if it exceeds the
// maximum.
if (STRNCMP(p + 1, "include", 7) != 0 || incstack_idx >= INCSTACK_SIZE)
return;
// Save current "fp" and "tag_fname" in the stack.
incstack[incstack_idx].etag_fname = vim_strsave(st->tag_fname);
if (incstack[incstack_idx].etag_fname == NULL)
return;
incstack[incstack_idx].fp = st->fp;
st->fp = NULL;
// Figure out "tag_fname" and "fp" to use for
// included file.
fullpath_ebuf = expand_tag_fname(st->ebuf, st->tag_fname, FALSE);
if (fullpath_ebuf != NULL)
{
st->fp = mch_fopen((char *)fullpath_ebuf, "r");
if (st->fp != NULL)
{
if (STRLEN(fullpath_ebuf) > LSIZE)
semsg(_(e_tag_file_path_truncated_for_str), st->ebuf);
vim_strncpy(st->tag_fname, fullpath_ebuf, MAXPATHL);
++incstack_idx;
st->is_etag = FALSE; // we can include anything
}
vim_free(fullpath_ebuf);
}
if (st->fp == NULL)
{
// Can't open the included file, skip it and
// restore old value of "fp".
st->fp = incstack[incstack_idx].fp;
vim_free(incstack[incstack_idx].etag_fname);
}
}
/*
* Reached the end of an emacs-style tags file. If this is an included tags
* file, then pop it from the incstack and continue processing the parent tags
* file. Otherwise, processed all the tags.
* Returns TRUE if an included tags file is popped and processing should
* continue with the parent tags file. Returns FALSE to stop processing tags.
*/
static int
emacs_tags_file_eof(findtags_state_T *st)
{
if (!incstack_idx) // reached end of file. stop processing.
return FALSE;
// reached the end of an included tags file. pop it.
--incstack_idx;
fclose(st->fp); // end of this file ...
st->fp = incstack[incstack_idx].fp;
STRCPY(st->tag_fname, incstack[incstack_idx].etag_fname);
vim_free(incstack[incstack_idx].etag_fname);
return TRUE;
}
/*
* Parse a line from an emacs-style tags file.
* Returns OK if the line is parsed successfully, returns FAIL if the line is
* not terminated by a newline.
*/
static int
emacs_tags_parse_line(char_u *lbuf, tagptrs_T *tagp)
{
char_u *p_7f;
char_u *p;
// There are two formats for an emacs tag line:
// 1: struct EnvBase ^?EnvBase^A139,4627
// 2: #define ARPB_WILD_WORLD ^?153,5194
p_7f = vim_strchr(lbuf, 0x7f);
if (p_7f == NULL)
{
etag_fail:
if (vim_strchr(lbuf, '\n') != NULL)
return FAIL;
// Truncated line. Ignore it.
if (p_verbose >= 5)
{
verbose_enter();
msg(_("Ignoring long line in tags file"));
verbose_leave();
}
tagp->command = lbuf;
tagp->tagname = lbuf;
tagp->tagname_end = lbuf;
return OK;
}
// Find ^A. If not found the line number is after the 0x7f
p = vim_strchr(p_7f, Ctrl_A);
if (p == NULL)
p = p_7f + 1;
else
++p;
if (!VIM_ISDIGIT(*p)) // check for start of line number
goto etag_fail;
tagp->command = p;
if (p[-1] == Ctrl_A) // first format: explicit tagname given
{
tagp->tagname = p_7f + 1;
tagp->tagname_end = p - 1;
}
else // second format: isolate tagname
{
// find end of tagname
for (p = p_7f - 1; !vim_iswordc(*p); --p)
if (p == lbuf)
goto etag_fail;
tagp->tagname_end = p + 1;
while (p >= lbuf && vim_iswordc(*p))
--p;
tagp->tagname = p + 1;
}
return OK;
}
#endif
/*
* Read the next line from a tags file.
* Returns TAGS_READ_SUCCESS if a tags line is successfully read and should be
* processed.
* Returns TAGS_READ_EOF if the end of file is reached.
* Returns TAGS_READ_IGNORE if the current line should be ignored (used when
* reached end of a emacs included tags file)
*/
static tags_read_status_T
findtags_get_next_line(findtags_state_T *st, tagsearch_info_T *sinfo_p)
{
int eof;
off_T offset;
// For binary search: compute the next offset to use.
if (st->state == TS_BINARY)
{
offset = sinfo_p->low_offset + ((sinfo_p->high_offset
- sinfo_p->low_offset) / 2);
if (offset == sinfo_p->curr_offset)
return TAGS_READ_EOF; // End the binary search without a match.
else
sinfo_p->curr_offset = offset;
}
// Skipping back (after a match during binary search).
else if (st->state == TS_SKIP_BACK)
{
sinfo_p->curr_offset -= st->lbuf_size * 2;
if (sinfo_p->curr_offset < 0)
{
sinfo_p->curr_offset = 0;
rewind(st->fp);
st->state = TS_STEP_FORWARD;
}
}
// When jumping around in the file, first read a line to find the
// start of the next line.
if (st->state == TS_BINARY || st->state == TS_SKIP_BACK)
{
// Adjust the search file offset to the correct position
sinfo_p->curr_offset_used = sinfo_p->curr_offset;
vim_ignored = vim_fseek(st->fp, sinfo_p->curr_offset, SEEK_SET);
eof = vim_fgets(st->lbuf, st->lbuf_size, st->fp);
if (!eof && sinfo_p->curr_offset != 0)
{
sinfo_p->curr_offset = vim_ftell(st->fp);
if (sinfo_p->curr_offset == sinfo_p->high_offset)
{
// oops, gone a bit too far; try from low offset
vim_ignored = vim_fseek(st->fp, sinfo_p->low_offset, SEEK_SET);
sinfo_p->curr_offset = sinfo_p->low_offset;
}
eof = vim_fgets(st->lbuf, st->lbuf_size, st->fp);
}
// skip empty and blank lines
while (!eof && vim_isblankline(st->lbuf))
{
sinfo_p->curr_offset = vim_ftell(st->fp);
eof = vim_fgets(st->lbuf, st->lbuf_size, st->fp);
}
if (eof)
{
// Hit end of file. Skip backwards.
st->state = TS_SKIP_BACK;
sinfo_p->match_offset = vim_ftell(st->fp);
sinfo_p->curr_offset = sinfo_p->curr_offset_used;
return TAGS_READ_IGNORE;
}
}
// Not jumping around in the file: Read the next line.
else
{
// skip empty and blank lines
do
{
#ifdef FEAT_CSCOPE
if (st->flags & TAG_CSCOPE)
eof = cs_fgets(st->lbuf, st->lbuf_size);
else
#endif
{
sinfo_p->curr_offset = vim_ftell(st->fp);
eof = vim_fgets(st->lbuf, st->lbuf_size, st->fp);
}
} while (!eof && vim_isblankline(st->lbuf));
if (eof)
{
#ifdef FEAT_EMACS_TAGS
if (emacs_tags_file_eof(st) == TRUE)
{
// an included tags file. Continue processing the parent
// tags file.
st->is_etag = TRUE; // (only etags can include)
return TAGS_READ_IGNORE;
}
#endif
return TAGS_READ_EOF;
}
}
return TAGS_READ_SUCCESS;
}
/*
* Parse a tags file header line in 'st->lbuf'.
* Returns TRUE if the current line in st->lbuf is not a tags header line and
* should be parsed as a regular tag line. Returns FALSE if the line is a
* header line and the next header line should be read.
*/
static int
findtags_hdr_parse(findtags_state_T *st)
{
char_u *p;
// Header lines in a tags file start with "!_TAG_"
if (STRNCMP(st->lbuf, "!_TAG_", 6) != 0)
// Non-header item before the header, e.g. "!" itself.
return TRUE;
// Process the header line.
if (STRNCMP(st->lbuf, "!_TAG_FILE_SORTED\t", 18) == 0)
st->tag_file_sorted = st->lbuf[18];
if (STRNCMP(st->lbuf, "!_TAG_FILE_ENCODING\t", 20) == 0)
{
// Prepare to convert every line from the specified encoding to
// 'encoding'.
for (p = st->lbuf + 20; *p > ' ' && *p < 127; ++p)
;
*p = NUL;
convert_setup(&st->vimconv, st->lbuf + 20, p_enc);
}
// Read the next line. Unrecognized flags are ignored.
return FALSE;
}
/*
* Handler to initialize the state when starting to process a new tags file.
* Called in the TS_START state when finding tags from a tags file.
* Returns TRUE if the line read from the tags file should be parsed and
* FALSE if the line should be ignored.
*/
static int
findtags_start_state_handler(
findtags_state_T *st,
int *sortic,
tagsearch_info_T *sinfo_p)
{
#ifdef FEAT_CSCOPE
int use_cscope = (st->flags & TAG_CSCOPE);
#endif
int noic = (st->flags & TAG_NOIC);
off_T filesize;
// The header ends when the line sorts below "!_TAG_". When case is
// folded lower case letters sort before "_".
if (STRNCMP(st->lbuf, "!_TAG_", 6) <= 0
|| (st->lbuf[0] == '!' && ASCII_ISLOWER(st->lbuf[1])))
return findtags_hdr_parse(st);
// Headers ends.
// When there is no tag head, or ignoring case, need to do a
// linear search.
// When no "!_TAG_" is found, default to binary search. If
// the tag file isn't sorted, the second loop will find it.
// When "!_TAG_FILE_SORTED" found: start binary search if
// flag set.
// For cscope, it's always linear.
# ifdef FEAT_CSCOPE
if (st->linear || use_cscope)
# else
if (st->linear)
# endif
st->state = TS_LINEAR;
else if (st->tag_file_sorted == NUL)
st->state = TS_BINARY;
else if (st->tag_file_sorted == '1')
st->state = TS_BINARY;
else if (st->tag_file_sorted == '2')
{
st->state = TS_BINARY;
*sortic = TRUE;
st->orgpat.regmatch.rm_ic = (p_ic || !noic);
}
else
st->state = TS_LINEAR;
if (st->state == TS_BINARY && st->orgpat.regmatch.rm_ic && !*sortic)
{
// Binary search won't work for ignoring case, use linear
// search.
st->linear = TRUE;
st->state = TS_LINEAR;
}
// When starting a binary search, get the size of the file and
// compute the first offset.
if (st->state == TS_BINARY)
{
if (vim_fseek(st->fp, 0L, SEEK_END) != 0)
// can't seek, don't use binary search
st->state = TS_LINEAR;
else
{
// Get the tag file size (don't use mch_fstat(), it's
// not portable). Don't use lseek(), it doesn't work
// properly on MacOS Catalina.
filesize = vim_ftell(st->fp);
vim_ignored = vim_fseek(st->fp, 0L, SEEK_SET);
// Calculate the first read offset in the file. Start
// the search in the middle of the file.
sinfo_p->low_offset = 0;
sinfo_p->low_char = 0;
sinfo_p->high_offset = filesize;
sinfo_p->curr_offset = 0;
sinfo_p->high_char = 0xff;
}
return FALSE;
}
return TRUE;
}
/*
* Parse a tag line read from a tags file.
* Returns OK if a tags line is successfully parsed.
* Returns FAIL if a format error is encountered.
*/
static int
findtags_parse_line(findtags_state_T *st, tagptrs_T *tagpp)
{
int status;
// Figure out where the different strings are in this line.
// For "normal" tags: Do a quick check if the tag matches.
// This speeds up tag searching a lot!
if (st->orgpat.headlen
#ifdef FEAT_EMACS_TAGS
&& !st->is_etag
#endif
)
{
CLEAR_FIELD(*tagpp);
tagpp->tagname = st->lbuf;
tagpp->tagname_end = vim_strchr(st->lbuf, TAB);
if (tagpp->tagname_end == NULL)
{
// Corrupted tag line.
return FAIL;
}
// Can be a matching tag, isolate the file name and command.
tagpp->fname = tagpp->tagname_end + 1;
tagpp->fname_end = vim_strchr(tagpp->fname, TAB);
if (tagpp->fname_end == NULL)
status = FAIL;
else
{
tagpp->command = tagpp->fname_end + 1;
status = OK;
}
}
else
status = parse_tag_line(st->lbuf,
#ifdef FEAT_EMACS_TAGS
st->is_etag,
#endif
tagpp);
if (status == FAIL)
return FAIL;
#ifdef FEAT_EMACS_TAGS
if (st->is_etag)
tagpp->fname = st->ebuf;
#endif
return OK;
}
/*
* Initialize the structure used for tag matching.
*/
static void
findtags_matchargs_init(findtags_match_args_T *margs, int flags)
{
margs->matchoff = 0; // match offset
margs->match_re = FALSE; // match with regexp
margs->match_no_ic = FALSE; // matches with case
margs->has_re = (flags & TAG_REGEXP); // regexp used
margs->sortic = FALSE; // tag file sorted in nocase
margs->sort_error = FALSE; // tags file not sorted
}
/*
* Compares the tag name in 'tagpp->tagname' with a search pattern in
* 'st->orgpat.head'.
* Returns TAG_MATCH_SUCCESS if the tag matches, TAG_MATCH_FAIL if the tag
* doesn't match, TAG_MATCH_NEXT to look for the next matching tag (used in a
* binary search) and TAG_MATCH_STOP if all the tags are processed without a
* match. Uses the values in 'margs' for doing the comparison.
*/
static tagmatch_status_T
findtags_match_tag(
findtags_state_T *st,
tagptrs_T *tagpp,
findtags_match_args_T *margs,
tagsearch_info_T *sinfo_p)
{
int match = FALSE;
int cmplen;
int i;
int tagcmp;
// Skip this line if the length of the tag is different and
// there is no regexp, or the tag is too short.
if (st->orgpat.headlen
#ifdef FEAT_EMACS_TAGS
&& !st->is_etag
#endif
)
{
cmplen = (int)(tagpp->tagname_end - tagpp->tagname);
if (p_tl != 0 && cmplen > p_tl) // adjust for 'taglength'
cmplen = p_tl;
if (margs->has_re && st->orgpat.headlen < cmplen)
cmplen = st->orgpat.headlen;
else if (st->state == TS_LINEAR && st->orgpat.headlen != cmplen)
return TAG_MATCH_FAIL;
if (st->state == TS_BINARY)
{
// Simplistic check for unsorted tags file.
i = (int)tagpp->tagname[0];
if (margs->sortic)
i = (int)TOUPPER_ASC(tagpp->tagname[0]);
if (i < sinfo_p->low_char || i > sinfo_p->high_char)
margs->sort_error = TRUE;
// Compare the current tag with the searched tag.
if (margs->sortic)
tagcmp = tag_strnicmp(tagpp->tagname, st->orgpat.head,
(size_t)cmplen);
else
tagcmp = STRNCMP(tagpp->tagname, st->orgpat.head, cmplen);
// A match with a shorter tag means to search forward.
// A match with a longer tag means to search backward.
if (tagcmp == 0)
{
if (cmplen < st->orgpat.headlen)
tagcmp = -1;
else if (cmplen > st->orgpat.headlen)
tagcmp = 1;
}
if (tagcmp == 0)
{
// We've located the tag, now skip back and search
// forward until the first matching tag is found.
st->state = TS_SKIP_BACK;
sinfo_p->match_offset = sinfo_p->curr_offset;
return TAG_MATCH_NEXT;
}
if (tagcmp < 0)
{
sinfo_p->curr_offset = vim_ftell(st->fp);
if (sinfo_p->curr_offset < sinfo_p->high_offset)
{
sinfo_p->low_offset = sinfo_p->curr_offset;
if (margs->sortic)
sinfo_p->low_char = TOUPPER_ASC(tagpp->tagname[0]);
else
sinfo_p->low_char = tagpp->tagname[0];
return TAG_MATCH_NEXT;
}
}
if (tagcmp > 0 && sinfo_p->curr_offset != sinfo_p->high_offset)
{
sinfo_p->high_offset = sinfo_p->curr_offset;
if (margs->sortic)
sinfo_p->high_char = TOUPPER_ASC(tagpp->tagname[0]);
else
sinfo_p->high_char = tagpp->tagname[0];
return TAG_MATCH_NEXT;
}
// No match yet and are at the end of the binary search.
return TAG_MATCH_STOP;
}
else if (st->state == TS_SKIP_BACK)
{
if (MB_STRNICMP(tagpp->tagname, st->orgpat.head, cmplen) != 0)
st->state = TS_STEP_FORWARD;
else
// Have to skip back more. Restore the curr_offset
// used, otherwise we get stuck at a long line.
sinfo_p->curr_offset = sinfo_p->curr_offset_used;
return TAG_MATCH_NEXT;
}
else if (st->state == TS_STEP_FORWARD)
{
if (MB_STRNICMP(tagpp->tagname, st->orgpat.head, cmplen) != 0)
{
if ((off_T)vim_ftell(st->fp) > sinfo_p->match_offset)
return TAG_MATCH_STOP; // past last match
else
return TAG_MATCH_NEXT; // before first match
}
}
else
// skip this match if it can't match
if (MB_STRNICMP(tagpp->tagname, st->orgpat.head, cmplen) != 0)
return TAG_MATCH_FAIL;
}
// First try matching with the pattern literally (also when it is
// a regexp).
cmplen = (int)(tagpp->tagname_end - tagpp->tagname);
if (p_tl != 0 && cmplen > p_tl) // adjust for 'taglength'
cmplen = p_tl;
// if tag length does not match, don't try comparing
if (st->orgpat.len != cmplen)
match = FALSE;
else
{
if (st->orgpat.regmatch.rm_ic)
{
match =
(MB_STRNICMP(tagpp->tagname, st->orgpat.pat, cmplen) == 0);
if (match)
margs->match_no_ic =
(STRNCMP(tagpp->tagname, st->orgpat.pat, cmplen) == 0);
}
else
match = (STRNCMP(tagpp->tagname, st->orgpat.pat, cmplen) == 0);
}
// Has a regexp: Also find tags matching regexp.
margs->match_re = FALSE;
if (!match && st->orgpat.regmatch.regprog != NULL)
{
int cc;
cc = *tagpp->tagname_end;
*tagpp->tagname_end = NUL;
match = vim_regexec(&st->orgpat.regmatch, tagpp->tagname, (colnr_T)0);
if (match)
{
margs->matchoff = (int)(st->orgpat.regmatch.startp[0] -
tagpp->tagname);
if (st->orgpat.regmatch.rm_ic)
{
st->orgpat.regmatch.rm_ic = FALSE;
margs->match_no_ic = vim_regexec(&st->orgpat.regmatch,
tagpp->tagname, (colnr_T)0);
st->orgpat.regmatch.rm_ic = TRUE;
}
}
*tagpp->tagname_end = cc;
margs->match_re = TRUE;
}
return match ? TAG_MATCH_SUCCESS : TAG_MATCH_FAIL;
}
/*
* Convert the encoding of a line read from a tags file in 'st->lbuf'.
* Converting the pattern from 'enc' to the tags file encoding doesn't work,
* because characters are not recognized. The converted line is saved in
* st->lbuf.
*/
static void
findtags_string_convert(findtags_state_T *st)
{
char_u *conv_line;
int len;
conv_line = string_convert(&st->vimconv, st->lbuf, NULL);
if (conv_line == NULL)
return;
// Copy or swap lbuf and conv_line.
len = (int)STRLEN(conv_line) + 1;
if (len > st->lbuf_size)
{
vim_free(st->lbuf);
st->lbuf = conv_line;
st->lbuf_size = len;
}
else
{
STRCPY(st->lbuf, conv_line);
vim_free(conv_line);
}
}
/*
* Add a matching tag found in a tags file to st->ht_match and st->ga_match.
* Returns OK if successfully added the match and FAIL on memory allocation
* failure.
*/
static int
findtags_add_match(
findtags_state_T *st,
tagptrs_T *tagpp,
findtags_match_args_T *margs,
char_u *buf_ffname,
hash_T *hash)
{
#ifdef FEAT_CSCOPE
int use_cscope = (st->flags & TAG_CSCOPE);
#endif
int name_only = (st->flags & TAG_NAMES);
int mtt;
int len = 0;
int is_current; // file name matches
int is_static; // current tag line is static
char_u *mfp;
char_u *p;
char_u *s;
#ifdef FEAT_CSCOPE
if (use_cscope)
{
// Don't change the ordering, always use the same table.
mtt = MT_GL_OTH;
}
else
#endif
{
// Decide in which array to store this match.
is_current = test_for_current(
#ifdef FEAT_EMACS_TAGS
st->is_etag,
#endif
tagpp->fname, tagpp->fname_end, st->tag_fname, buf_ffname);
#ifdef FEAT_EMACS_TAGS
is_static = FALSE;
if (!st->is_etag) // emacs tags are never static
#endif
is_static = test_for_static(tagpp);
// decide in which of the sixteen tables to store this
// match
if (is_static)
{
if (is_current)
mtt = MT_ST_CUR;
else
mtt = MT_ST_OTH;
}
else
{
if (is_current)
mtt = MT_GL_CUR;
else
mtt = MT_GL_OTH;
}
if (st->orgpat.regmatch.rm_ic && !margs->match_no_ic)
mtt += MT_IC_OFF;
if (margs->match_re)
mtt += MT_RE_OFF;
}
// Add the found match in ht_match[mtt] and ga_match[mtt].
// Store the info we need later, which depends on the kind of
// tags we are dealing with.
if (st->help_only)
{
#ifdef FEAT_MULTI_LANG
# define ML_EXTRA 3
#else
# define ML_EXTRA 0
#endif
// Append the help-heuristic number after the tagname, for
// sorting it later. The heuristic is ignored for
// detecting duplicates.
// The format is {tagname}@{lang}NUL{heuristic}NUL
*tagpp->tagname_end = NUL;
len = (int)(tagpp->tagname_end - tagpp->tagname);
mfp = alloc(sizeof(char_u) + len + 10 + ML_EXTRA + 1);
if (mfp != NULL)
{
int heuristic;
p = mfp;
STRCPY(p, tagpp->tagname);
#ifdef FEAT_MULTI_LANG
p[len] = '@';
STRCPY(p + len + 1, st->help_lang);
#endif
heuristic = help_heuristic(tagpp->tagname,
margs->match_re ? margs->matchoff : 0,
!margs->match_no_ic);
#ifdef FEAT_MULTI_LANG
heuristic += st->help_pri;
#endif
sprintf((char *)p + len + 1 + ML_EXTRA, "%06d",
heuristic);
}
*tagpp->tagname_end = TAB;
}
else if (name_only)
{
if (st->get_searchpat)
{
char_u *temp_end = tagpp->command;
if (*temp_end == '/')
while (*temp_end && *temp_end != '\r'
&& *temp_end != '\n'
&& *temp_end != '$')
temp_end++;
if (tagpp->command + 2 < temp_end)
{
len = (int)(temp_end - tagpp->command - 2);
mfp = alloc(len + 2);
if (mfp != NULL)
vim_strncpy(mfp, tagpp->command + 2, len);
}
else
mfp = NULL;
st->get_searchpat = FALSE;
}
else
{
len = (int)(tagpp->tagname_end - tagpp->tagname);
mfp = alloc(sizeof(char_u) + len + 1);
if (mfp != NULL)
vim_strncpy(mfp, tagpp->tagname, len);
// if wanted, re-read line to get long form too
if (State & INSERT)
st->get_searchpat = p_sft;
}
}
else
{
size_t tag_fname_len = STRLEN(st->tag_fname);
#ifdef FEAT_EMACS_TAGS
size_t ebuf_len = 0;
#endif
// Save the tag in a buffer.
// Use 0x02 to separate fields (Can't use NUL because the
// hash key is terminated by NUL, or Ctrl_A because that is
// part of some Emacs tag files -- see parse_tag_line).
// Emacs tag: <mtt><tag_fname><0x02><ebuf><0x02><lbuf><NUL>
// other tag: <mtt><tag_fname><0x02><0x02><lbuf><NUL>
// without Emacs tags: <mtt><tag_fname><0x02><lbuf><NUL>
// Here <mtt> is the "mtt" value plus 1 to avoid NUL.
len = (int)tag_fname_len + (int)STRLEN(st->lbuf) + 3;
#ifdef FEAT_EMACS_TAGS
if (st->is_etag)
{
ebuf_len = STRLEN(st->ebuf);
len += (int)ebuf_len + 1;
}
else
++len;
#endif
mfp = alloc(sizeof(char_u) + len + 1);
if (mfp != NULL)
{
p = mfp;
p[0] = mtt + 1;
STRCPY(p + 1, st->tag_fname);
#ifdef BACKSLASH_IN_FILENAME
// Ignore differences in slashes, avoid adding
// both path/file and path\file.
slash_adjust(p + 1);
#endif
p[tag_fname_len + 1] = TAG_SEP;
s = p + 1 + tag_fname_len + 1;
#ifdef FEAT_EMACS_TAGS
if (st->is_etag)
{
STRCPY(s, st->ebuf);
s[ebuf_len] = TAG_SEP;
s += ebuf_len + 1;
}
else
*s++ = TAG_SEP;
#endif
STRCPY(s, st->lbuf);
}
}
if (mfp != NULL)
{
hashitem_T *hi;
// Don't add identical matches.
// Add all cscope tags, because they are all listed.
// "mfp" is used as a hash key, there is a NUL byte to end
// the part that matters for comparing, more bytes may
// follow after it. E.g. help tags store the priority
// after the NUL.
#ifdef FEAT_CSCOPE
if (use_cscope)
++*hash;
else
#endif
*hash = hash_hash(mfp);
hi = hash_lookup(&st->ht_match[mtt], mfp, *hash);
if (HASHITEM_EMPTY(hi))
{
if (hash_add_item(&st->ht_match[mtt], hi, mfp, *hash) == FAIL
|| ga_grow(&st->ga_match[mtt], 1) != OK)
{
// Out of memory! Just forget about the rest.
st->stop_searching = TRUE;
return FAIL;
}
((char_u **)(st->ga_match[mtt].ga_data))
[st->ga_match[mtt].ga_len++] = mfp;
st->match_count++;
}
else
// duplicate tag, drop it
vim_free(mfp);
}
return OK;
}
/*
* Read and get all the tags from file st->tag_fname.
* Sets 'st->stop_searching' to TRUE to stop searching for additional tags.
*/
static void
findtags_get_all_tags(
findtags_state_T *st,
findtags_match_args_T *margs,
char_u *buf_ffname)
{
tagptrs_T tagp;
tagsearch_info_T search_info;
int retval;
#ifdef FEAT_CSCOPE
int use_cscope = (st->flags & TAG_CSCOPE);
#endif
hash_T hash = 0;
// This is only to avoid a compiler warning for using search_info
// uninitialised.
CLEAR_FIELD(search_info);
// Read and parse the lines in the file one by one
for (;;)
{
// check for CTRL-C typed, more often when jumping around
if (st->state == TS_BINARY || st->state == TS_SKIP_BACK)
line_breakcheck();
else
fast_breakcheck();
if ((st->flags & TAG_INS_COMP)) // Double brackets for gcc
ins_compl_check_keys(30, FALSE);
if (got_int || ins_compl_interrupted())
{
st->stop_searching = TRUE;
break;
}
// When mincount is TAG_MANY, stop when enough matches have been
// found (for completion).
if (st->mincount == TAG_MANY && st->match_count >= TAG_MANY)
{
st->stop_searching = TRUE;
break;
}
if (st->get_searchpat)
goto line_read_in;
retval = findtags_get_next_line(st, &search_info);
if (retval == TAGS_READ_IGNORE)
continue;
if (retval == TAGS_READ_EOF)
break;
line_read_in:
if (st->vimconv.vc_type != CONV_NONE)
findtags_string_convert(st);
#ifdef FEAT_EMACS_TAGS
// Emacs tags line with CTRL-L: New file name on next line.
// The file name is followed by a ','.
// Remember etag file name in ebuf.
if (*st->lbuf == Ctrl_L
# ifdef FEAT_CSCOPE
&& !use_cscope
# endif
)
{
st->is_etag = TRUE; // in case at the start
st->state = TS_LINEAR;
emacs_tags_new_filename(st);
continue;
}
#endif
// When still at the start of the file, check for Emacs tags file
// format, and for "not sorted" flag.
if (st->state == TS_START)
{
if (findtags_start_state_handler(st, &margs->sortic, &search_info) == FALSE)
continue;
}
// When the line is too long the NUL will not be in the
// last-but-one byte (see vim_fgets()).
// Has been reported for Mozilla JS with extremely long names.
// In that case we need to increase lbuf_size.
if (st->lbuf[st->lbuf_size - 2] != NUL
#ifdef FEAT_CSCOPE
&& !use_cscope
#endif
)
{
st->lbuf_size *= 2;
vim_free(st->lbuf);
st->lbuf = alloc(st->lbuf_size);
if (st->lbuf == NULL)
{
if (st->fp != NULL)
fclose(st->fp);
st->fp = NULL;
st->stop_searching = TRUE;
return;
}
if (st->state == TS_STEP_FORWARD)
// Seek to the same position to read the same line again
vim_ignored = vim_fseek(st->fp, search_info.curr_offset,
SEEK_SET);
// this will try the same thing again, make sure the offset is
// different
search_info.curr_offset = 0;
continue;
}
if (findtags_parse_line(st, &tagp) == FAIL)
{
semsg(_(e_format_error_in_tags_file_str), st->tag_fname);
#ifdef FEAT_CSCOPE
if (!use_cscope)
#endif
semsg(_("Before byte %ld"), (long)vim_ftell(st->fp));
st->stop_searching = TRUE;
return;
}
retval = findtags_match_tag(st, &tagp, margs, &search_info);
if (retval == TAG_MATCH_NEXT)
continue;
if (retval == TAG_MATCH_STOP)
break;
// If a match is found, add it to ht_match[] and ga_match[].
if (retval == TAG_MATCH_SUCCESS)
{
if (findtags_add_match(st, &tagp, margs, buf_ffname, &hash)
== FAIL)
break;
}
} // forever
}
/*
* Search for tags matching 'st->orgpat.pat' in the 'st->tag_fname' tags file.
* Information needed to search for the tags is in the 'st' state structure.
* The matching tags are returned in 'st'. If an error is encountered, then
* 'st->stop_searching' is set to TRUE.
*/
static void
findtags_in_file(findtags_state_T *st, char_u *buf_ffname)
{
findtags_match_args_T margs;
#ifdef FEAT_CSCOPE
int use_cscope = FALSE;
#endif
st->vimconv.vc_type = CONV_NONE;
st->tag_file_sorted = NUL;
st->fp = NULL;
findtags_matchargs_init(&margs, st->flags);
// A file that doesn't exist is silently ignored. Only when not a
// single file is found, an error message is given (further on).
#ifdef FEAT_CSCOPE
use_cscope = (st->flags & TAG_CSCOPE);
if (use_cscope)
st->fp = NULL; // avoid GCC warning
else
#endif
{
#ifdef FEAT_MULTI_LANG
if (curbuf->b_help)
{
if (!findtags_in_help_init(st))
return;
}
#endif
st->fp = mch_fopen((char *)st->tag_fname, "r");
if (st->fp == NULL)
return;
if (p_verbose >= 5)
{
verbose_enter();
smsg(_("Searching tags file %s"), st->tag_fname);
verbose_leave();
}
}
st->did_open = TRUE; // remember that we found at least one file
st->state = TS_START; // we're at the start of the file
#ifdef FEAT_EMACS_TAGS
st->is_etag = FALSE; // default is: not emacs style
#endif
// Read and parse the lines in the file one by one
findtags_get_all_tags(st, &margs, buf_ffname);
if (st->fp != NULL)
{
fclose(st->fp);
st->fp = NULL;
}
#ifdef FEAT_EMACS_TAGS
emacs_tags_incstack_free();
#endif
if (st->vimconv.vc_type != CONV_NONE)
convert_setup(&st->vimconv, NULL, NULL);
if (margs.sort_error)
semsg(_(e_tags_file_not_sorted_str), st->tag_fname);
// Stop searching if sufficient tags have been found.
if (st->match_count >= st->mincount)
st->stop_searching = TRUE;
}
/*
* Copy the tags found by find_tags() to 'matchesp'.
* Returns the number of matches copied.
*/
static int
findtags_copy_matches(findtags_state_T *st, char_u ***matchesp)
{
int name_only = (st->flags & TAG_NAMES);
char_u **matches;
int mtt;
int i;
char_u *mfp;
char_u *p;
if (st->match_count > 0)
matches = ALLOC_MULT(char_u *, st->match_count);
else
matches = NULL;
st->match_count = 0;
for (mtt = 0; mtt < MT_COUNT; ++mtt)
{
for (i = 0; i < st->ga_match[mtt].ga_len; ++i)
{
mfp = ((char_u **)(st->ga_match[mtt].ga_data))[i];
if (matches == NULL)
vim_free(mfp);
else
{
if (!name_only)
{
// Change mtt back to zero-based.
*mfp = *mfp - 1;
// change the TAG_SEP back to NUL
for (p = mfp + 1; *p != NUL; ++p)
if (*p == TAG_SEP)
*p = NUL;
}
matches[st->match_count++] = mfp;
}
}
ga_clear(&st->ga_match[mtt]);
hash_clear(&st->ht_match[mtt]);
}
*matchesp = matches;
return st->match_count;
}
/*
* find_tags() - search for tags in tags files
*
* Return FAIL if search completely failed (*num_matches will be 0, *matchesp
* will be NULL), OK otherwise.
*
* Priority depending on which type of tag is recognized:
* 6. A static or global tag with a full matching tag for the current file.
* 5. A global tag with a full matching tag for another file.
* 4. A static tag with a full matching tag for another file.
* 3. A static or global tag with an ignore-case matching tag for the
* current file.
* 2. A global tag with an ignore-case matching tag for another file.
* 1. A static tag with an ignore-case matching tag for another file.
*
* Tags in an emacs-style tags file are always global.
*
* flags:
* TAG_HELP only search for help tags
* TAG_NAMES only return name of tag
* TAG_REGEXP use "pat" as a regexp
* TAG_NOIC don't always ignore case
* TAG_KEEP_LANG keep language
* TAG_CSCOPE use cscope results for tags
* TAG_NO_TAGFUNC do not call the 'tagfunc' function
*/
int
find_tags(
char_u *pat, // pattern to search for
int *num_matches, // return: number of matches found
char_u ***matchesp, // return: array of matches found
int flags,
int mincount, // MAXCOL: find all matches
// other: minimal number of matches
char_u *buf_ffname) // name of buffer for priority
{
findtags_state_T st;
tagname_T tn; // info for get_tagfname()
int first_file; // trying first tag file
int retval = FAIL; // return value
int round;
int save_emsg_off;
int help_save;
#ifdef FEAT_MULTI_LANG
int i;
char_u *saved_pat = NULL; // copy of pat[]
#endif
int findall = (mincount == MAXCOL || mincount == TAG_MANY);
// find all matching tags
int has_re = (flags & TAG_REGEXP); // regexp used
int noic = (flags & TAG_NOIC);
#ifdef FEAT_CSCOPE
int use_cscope = (flags & TAG_CSCOPE);
#endif
int verbose = (flags & TAG_VERBOSE);
int save_p_ic = p_ic;
/*
* Change the value of 'ignorecase' according to 'tagcase' for the
* duration of this function.
*/
switch (curbuf->b_tc_flags ? curbuf->b_tc_flags : tc_flags)
{
case TC_FOLLOWIC: break;
case TC_IGNORE: p_ic = TRUE; break;
case TC_MATCH: p_ic = FALSE; break;
case TC_FOLLOWSCS: p_ic = ignorecase(pat); break;
case TC_SMART: p_ic = ignorecase_opt(pat, TRUE, TRUE); break;
}
help_save = curbuf->b_help;
if (findtags_state_init(&st, pat, flags, mincount) == FAIL)
goto findtag_end;
#ifdef FEAT_CSCOPE
STRCPY(st.tag_fname, "from cscope"); // for error messages
#endif
/*
* Initialize a few variables
*/
if (st.help_only) // want tags from help file
curbuf->b_help = TRUE; // will be restored later
#ifdef FEAT_CSCOPE
else if (use_cscope)
{
// Make sure we don't mix help and cscope, confuses Coverity.
st.help_only = FALSE;
curbuf->b_help = FALSE;
}
#endif
#ifdef FEAT_MULTI_LANG
if (curbuf->b_help)
{
// When "@ab" is specified use only the "ab" language, otherwise
// search all languages.
if (st.orgpat.len > 3 && pat[st.orgpat.len - 3] == '@'
&& ASCII_ISALPHA(pat[st.orgpat.len - 2])
&& ASCII_ISALPHA(pat[st.orgpat.len - 1]))
{
saved_pat = vim_strnsave(pat, st.orgpat.len - 3);
if (saved_pat != NULL)
{
st.help_lang_find = &pat[st.orgpat.len - 2];
st.orgpat.pat = saved_pat;
st.orgpat.len -= 3;
}
}
}
#endif
if (p_tl != 0 && st.orgpat.len > p_tl) // adjust for 'taglength'
st.orgpat.len = p_tl;
save_emsg_off = emsg_off;
emsg_off = TRUE; // don't want error for invalid RE here
prepare_pats(&st.orgpat, has_re);
emsg_off = save_emsg_off;
if (has_re && st.orgpat.regmatch.regprog == NULL)
goto findtag_end;
#ifdef FEAT_EVAL
retval = findtags_apply_tfu(&st, pat, buf_ffname);
if (retval != NOTDONE)
goto findtag_end;
// re-initialize the default return value
retval = FAIL;
#endif
#ifdef FEAT_MULTI_LANG
// Set a flag if the file extension is .txt
if ((flags & TAG_KEEP_LANG)
&& st.help_lang_find == NULL
&& curbuf->b_fname != NULL
&& (i = (int)STRLEN(curbuf->b_fname)) > 4
&& STRICMP(curbuf->b_fname + i - 4, ".txt") == 0)
st.is_txt = TRUE;
#endif
/*
* When finding a specified number of matches, first try with matching
* case, so binary search can be used, and try ignore-case matches in a
* second loop.
* When finding all matches, 'tagbsearch' is off, or there is no fixed
* string to look for, ignore case right away to avoid going though the
* tags files twice.
* When the tag file is case-fold sorted, it is either one or the other.
* Only ignore case when TAG_NOIC not used or 'ignorecase' set.
*/
st.orgpat.regmatch.rm_ic = ((p_ic || !noic)
&& (findall || st.orgpat.headlen == 0 || !p_tbs));
for (round = 1; round <= 2; ++round)
{
st.linear = (st.orgpat.headlen == 0 || !p_tbs || round == 2);
/*
* Try tag file names from tags option one by one.
*/
for (first_file = TRUE;
#ifdef FEAT_CSCOPE
use_cscope ||
#endif
get_tagfname(&tn, first_file, st.tag_fname) == OK;
first_file = FALSE)
{
findtags_in_file(&st, buf_ffname);
if (st.stop_searching
#ifdef FEAT_CSCOPE
|| use_cscope
#endif
)
{
retval = OK;
break;
}
} // end of for-each-file loop
#ifdef FEAT_CSCOPE
if (!use_cscope)
#endif
tagname_free(&tn);
// stop searching when already did a linear search, or when TAG_NOIC
// used, and 'ignorecase' not set or already did case-ignore search
if (st.stop_searching || st.linear || (!p_ic && noic) ||
st.orgpat.regmatch.rm_ic)
break;
# ifdef FEAT_CSCOPE
if (use_cscope)
break;
# endif
// try another time while ignoring case
st.orgpat.regmatch.rm_ic = TRUE;
}
if (!st.stop_searching)
{
if (!st.did_open && verbose) // never opened any tags file
emsg(_(e_no_tags_file));
retval = OK; // It's OK even when no tag found
}
findtag_end:
findtags_state_free(&st);
/*
* Move the matches from the ga_match[] arrays into one list of
* matches. When retval == FAIL, free the matches.
*/
if (retval == FAIL)
st.match_count = 0;
*num_matches = findtags_copy_matches(&st, matchesp);
curbuf->b_help = help_save;
#ifdef FEAT_MULTI_LANG
vim_free(saved_pat);
#endif
p_ic = save_p_ic;
return retval;
}
static garray_T tag_fnames = GA_EMPTY;
/*
* Callback function for finding all "tags" and "tags-??" files in
* 'runtimepath' doc directories.
*/
static void
found_tagfile_cb(char_u *fname, void *cookie UNUSED)
{
if (ga_grow(&tag_fnames, 1) == OK)
{
char_u *tag_fname = vim_strsave(fname);
#ifdef BACKSLASH_IN_FILENAME
slash_adjust(tag_fname);
#endif
simplify_filename(tag_fname);
((char_u **)(tag_fnames.ga_data))[tag_fnames.ga_len++] = tag_fname;
}
}
#if defined(EXITFREE) || defined(PROTO)
void
free_tag_stuff(void)
{
ga_clear_strings(&tag_fnames);
if (curwin != NULL)
do_tag(NULL, DT_FREE, 0, 0, 0);
tag_freematch();
# if defined(FEAT_QUICKFIX)
tagstack_clear_entry(&ptag_entry);
# endif
}
#endif
/*
* Get the next name of a tag file from the tag file list.
* For help files, use "tags" file only.
*
* Return FAIL if no more tag file names, OK otherwise.
*/
int
get_tagfname(
tagname_T *tnp, // holds status info
int first, // TRUE when first file name is wanted
char_u *buf) // pointer to buffer of MAXPATHL chars
{
char_u *fname = NULL;
char_u *r_ptr;
int i;
if (first)
CLEAR_POINTER(tnp);
if (curbuf->b_help)
{
/*
* For help files it's done in a completely different way:
* Find "doc/tags" and "doc/tags-??" in all directories in
* 'runtimepath'.
*/
if (first)
{
ga_clear_strings(&tag_fnames);
ga_init2(&tag_fnames, sizeof(char_u *), 10);
do_in_runtimepath((char_u *)
#ifdef FEAT_MULTI_LANG
# ifdef VMS
// Functions decc$to_vms() and decc$translate_vms() crash
// on some VMS systems with wildcards "??". Seems ECO
// patches do fix the problem in C RTL, but we can't use
// an #ifdef for that.
"doc/tags doc/tags-*"
# else
"doc/tags doc/tags-??"
# endif
#else
"doc/tags"
#endif
, DIP_ALL, found_tagfile_cb, NULL);
}
if (tnp->tn_hf_idx >= tag_fnames.ga_len)
{
// Not found in 'runtimepath', use 'helpfile', if it exists and
// wasn't used yet, replacing "help.txt" with "tags".
if (tnp->tn_hf_idx > tag_fnames.ga_len || *p_hf == NUL)
return FAIL;
++tnp->tn_hf_idx;
STRCPY(buf, p_hf);
STRCPY(gettail(buf), "tags");
#ifdef BACKSLASH_IN_FILENAME
slash_adjust(buf);
#endif
simplify_filename(buf);
for (i = 0; i < tag_fnames.ga_len; ++i)
if (STRCMP(buf, ((char_u **)(tag_fnames.ga_data))[i]) == 0)
return FAIL; // avoid duplicate file names
}
else
vim_strncpy(buf, ((char_u **)(tag_fnames.ga_data))[
tnp->tn_hf_idx++], MAXPATHL - 1);
return OK;
}
if (first)
{
// Init. We make a copy of 'tags', because autocommands may change
// the value without notifying us.
tnp->tn_tags = vim_strsave((*curbuf->b_p_tags != NUL)
? curbuf->b_p_tags : p_tags);
if (tnp->tn_tags == NULL)
return FAIL;
tnp->tn_np = tnp->tn_tags;
}
/*
* Loop until we have found a file name that can be used.
* There are two states:
* tnp->tn_did_filefind_init == FALSE: setup for next part in 'tags'.
* tnp->tn_did_filefind_init == TRUE: find next file in this part.
*/
for (;;)
{
if (tnp->tn_did_filefind_init)
{
fname = vim_findfile(tnp->tn_search_ctx);
if (fname != NULL)
break;
tnp->tn_did_filefind_init = FALSE;
}
else
{
char_u *filename = NULL;
// Stop when used all parts of 'tags'.
if (*tnp->tn_np == NUL)
{
vim_findfile_cleanup(tnp->tn_search_ctx);
tnp->tn_search_ctx = NULL;
return FAIL;
}
/*
* Copy next file name into buf.
*/
buf[0] = NUL;
(void)copy_option_part(&tnp->tn_np, buf, MAXPATHL - 1, " ,");
#ifdef FEAT_PATH_EXTRA
r_ptr = vim_findfile_stopdir(buf);
#else
r_ptr = NULL;
#endif
// move the filename one char forward and truncate the
// filepath with a NUL
filename = gettail(buf);
STRMOVE(filename + 1, filename);
*filename++ = NUL;
tnp->tn_search_ctx = vim_findfile_init(buf, filename,
r_ptr, 100,
FALSE, // don't free visited list
FINDFILE_FILE, // we search for a file
tnp->tn_search_ctx, TRUE, curbuf->b_ffname);
if (tnp->tn_search_ctx != NULL)
tnp->tn_did_filefind_init = TRUE;
}
}
STRCPY(buf, fname);
vim_free(fname);
return OK;
}
/*
* Free the contents of a tagname_T that was filled by get_tagfname().
*/
void
tagname_free(tagname_T *tnp)
{
vim_free(tnp->tn_tags);
vim_findfile_cleanup(tnp->tn_search_ctx);
tnp->tn_search_ctx = NULL;
ga_clear_strings(&tag_fnames);
}
/*
* Parse one line from the tags file. Find start/end of tag name, start/end of
* file name and start of search pattern.
*
* If is_etag is TRUE, tagp->fname and tagp->fname_end are not set.
*
* Return FAIL if there is a format error in this line, OK otherwise.
*/
static int
parse_tag_line(
char_u *lbuf, // line to be parsed
#ifdef FEAT_EMACS_TAGS
int is_etag,
#endif
tagptrs_T *tagp)
{
char_u *p;
#ifdef FEAT_EMACS_TAGS
if (is_etag)
// emacs-style tag file
return emacs_tags_parse_line(lbuf, tagp);
#endif
// Isolate the tagname, from lbuf up to the first white
tagp->tagname = lbuf;
p = vim_strchr(lbuf, TAB);
if (p == NULL)
return FAIL;
tagp->tagname_end = p;
// Isolate file name, from first to second white space
if (*p != NUL)
++p;
tagp->fname = p;
p = vim_strchr(p, TAB);
if (p == NULL)
return FAIL;
tagp->fname_end = p;
// find start of search command, after second white space
if (*p != NUL)
++p;
if (*p == NUL)
return FAIL;
tagp->command = p;
return OK;
}
/*
* Check if tagname is a static tag
*
* Static tags produced by the older ctags program have the format:
* 'file:tag file /pattern'.
* This is only recognized when both occurrence of 'file' are the same, to
* avoid recognizing "string::string" or ":exit".
*
* Static tags produced by the new ctags program have the format:
* 'tag file /pattern/;"<Tab>file:' "
*
* Return TRUE if it is a static tag and adjust *tagname to the real tag.
* Return FALSE if it is not a static tag.
*/
static int
test_for_static(tagptrs_T *tagp)
{
char_u *p;
/*
* Check for new style static tag ":...<Tab>file:[<Tab>...]"
*/
p = tagp->command;
while ((p = vim_strchr(p, '\t')) != NULL)
{
++p;
if (STRNCMP(p, "file:", 5) == 0)
return TRUE;
}
return FALSE;
}
/*
* Returns the length of a matching tag line.
*/
static size_t
matching_line_len(char_u *lbuf)
{
char_u *p = lbuf + 1;
// does the same thing as parse_match()
p += STRLEN(p) + 1;
#ifdef FEAT_EMACS_TAGS
p += STRLEN(p) + 1;
#endif
return (p - lbuf) + STRLEN(p);
}
/*
* Parse a line from a matching tag. Does not change the line itself.
*
* The line that we get looks like this:
* Emacs tag: <mtt><tag_fname><NUL><ebuf><NUL><lbuf>
* other tag: <mtt><tag_fname><NUL><NUL><lbuf>
* without Emacs tags: <mtt><tag_fname><NUL><lbuf>
*
* Return OK or FAIL.
*/
static int
parse_match(
char_u *lbuf, // input: matching line
tagptrs_T *tagp) // output: pointers into the line
{
int retval;
char_u *p;
char_u *pc, *pt;
tagp->tag_fname = lbuf + 1;
lbuf += STRLEN(tagp->tag_fname) + 2;
#ifdef FEAT_EMACS_TAGS
if (*lbuf)
{
tagp->is_etag = TRUE;
tagp->fname = lbuf;
lbuf += STRLEN(lbuf);
tagp->fname_end = lbuf++;
}
else
{
tagp->is_etag = FALSE;
++lbuf;
}
#endif
// Find search pattern and the file name for non-etags.
retval = parse_tag_line(lbuf,
#ifdef FEAT_EMACS_TAGS
tagp->is_etag,
#endif
tagp);
tagp->tagkind = NULL;
tagp->user_data = NULL;
tagp->tagline = 0;
tagp->command_end = NULL;
if (retval == OK)
{
// Try to find a kind field: "kind:<kind>" or just "<kind>"
p = tagp->command;
if (find_extra(&p) == OK)
{
if (p > tagp->command && p[-1] == '|')
tagp->command_end = p - 1; // drop trailing bar
else
tagp->command_end = p;
p += 2; // skip ";\""
if (*p++ == TAB)
// Accept ASCII alphabetic kind characters and any multi-byte
// character.
while (ASCII_ISALPHA(*p) || mb_ptr2len(p) > 1)
{
if (STRNCMP(p, "kind:", 5) == 0)
tagp->tagkind = p + 5;
else if (STRNCMP(p, "user_data:", 10) == 0)
tagp->user_data = p + 10;
else if (STRNCMP(p, "line:", 5) == 0)
tagp->tagline = atoi((char *)p + 5);
if (tagp->tagkind != NULL && tagp->user_data != NULL)
break;
pc = vim_strchr(p, ':');
pt = vim_strchr(p, '\t');
if (pc == NULL || (pt != NULL && pc > pt))
tagp->tagkind = p;
if (pt == NULL)
break;
p = pt;
MB_PTR_ADV(p);
}
}
if (tagp->tagkind != NULL)
{
for (p = tagp->tagkind;
*p && *p != '\t' && *p != '\r' && *p != '\n'; MB_PTR_ADV(p))
;
tagp->tagkind_end = p;
}
if (tagp->user_data != NULL)
{
for (p = tagp->user_data;
*p && *p != '\t' && *p != '\r' && *p != '\n'; MB_PTR_ADV(p))
;
tagp->user_data_end = p;
}
}
return retval;
}
/*
* Find out the actual file name of a tag. Concatenate the tags file name
* with the matching tag file name.
* Returns an allocated string or NULL (out of memory).
*/
static char_u *
tag_full_fname(tagptrs_T *tagp)
{
char_u *fullname;
int c;
#ifdef FEAT_EMACS_TAGS
if (tagp->is_etag)
c = 0; // to shut up GCC
else
#endif
{
c = *tagp->fname_end;
*tagp->fname_end = NUL;
}
fullname = expand_tag_fname(tagp->fname, tagp->tag_fname, FALSE);
#ifdef FEAT_EMACS_TAGS
if (!tagp->is_etag)
#endif
*tagp->fname_end = c;
return fullname;
}
/*
* Jump to a tag that has been found in one of the tag files
*
* returns OK for success, NOTAGFILE when file not found, FAIL otherwise.
*/
static int
jumpto_tag(
char_u *lbuf_arg, // line from the tags file for this tag
int forceit, // :ta with !
int keep_help) // keep help flag (FALSE for cscope)
{
optmagic_T save_magic_overruled;
int save_p_ws, save_p_scs, save_p_ic;
linenr_T save_lnum;
char_u *str;
char_u *pbuf; // search pattern buffer
char_u *pbuf_end;
char_u *tofree_fname = NULL;
char_u *fname;
tagptrs_T tagp;
int retval = FAIL;
int getfile_result = GETFILE_UNUSED;
int search_options;
#ifdef FEAT_SEARCH_EXTRA
int save_no_hlsearch;
#endif
#if defined(FEAT_QUICKFIX)
win_T *curwin_save = NULL;
#endif
char_u *full_fname = NULL;
#ifdef FEAT_FOLDING
int old_KeyTyped = KeyTyped; // getting the file may reset it
#endif
size_t len;
char_u *lbuf;
// Make a copy of the line, it can become invalid when an autocommand calls
// back here recursively.
len = matching_line_len(lbuf_arg) + 1;
lbuf = alloc(len);
if (lbuf != NULL)
mch_memmove(lbuf, lbuf_arg, len);
pbuf = alloc(LSIZE);
// parse the match line into the tagp structure
if (pbuf == NULL || lbuf == NULL || parse_match(lbuf, &tagp) == FAIL)
{
tagp.fname_end = NULL;
goto erret;
}
// truncate the file name, so it can be used as a string
*tagp.fname_end = NUL;
fname = tagp.fname;
// copy the command to pbuf[], remove trailing CR/NL
str = tagp.command;
for (pbuf_end = pbuf; *str && *str != '\n' && *str != '\r'; )
{
#ifdef FEAT_EMACS_TAGS
if (tagp.is_etag && *str == ',')// stop at ',' after line number
break;
#endif
*pbuf_end++ = *str++;
if (pbuf_end - pbuf + 1 >= LSIZE)
break;
}
*pbuf_end = NUL;
#ifdef FEAT_EMACS_TAGS
if (!tagp.is_etag)
#endif
{
/*
* Remove the "<Tab>fieldname:value" stuff; we don't need it here.
*/
str = pbuf;
if (find_extra(&str) == OK)
{
pbuf_end = str;
*pbuf_end = NUL;
}
}
/*
* Expand file name, when needed (for environment variables).
* If 'tagrelative' option set, may change file name.
*/
fname = expand_tag_fname(fname, tagp.tag_fname, TRUE);
if (fname == NULL)
goto erret;
tofree_fname = fname; // free() it later
/*
* Check if the file with the tag exists before abandoning the current
* file. Also accept a file name for which there is a matching BufReadCmd
* autocommand event (e.g., http://sys/file).
*/
if (mch_getperm(fname) < 0 && !has_autocmd(EVENT_BUFREADCMD, fname, NULL))
{
retval = NOTAGFILE;
vim_free(nofile_fname);
nofile_fname = vim_strsave(fname);
if (nofile_fname == NULL)
nofile_fname = empty_option;
goto erret;
}
++RedrawingDisabled;
#ifdef FEAT_GUI
need_mouse_correct = TRUE;
#endif
#if defined(FEAT_QUICKFIX)
if (g_do_tagpreview != 0)
{
postponed_split = 0; // don't split again below
curwin_save = curwin; // Save current window
/*
* If we are reusing a window, we may change dir when
* entering it (autocommands) so turn the tag filename
* into a fullpath
*/
if (!curwin->w_p_pvw)
{
full_fname = FullName_save(fname, FALSE);
fname = full_fname;
/*
* Make the preview window the current window.
* Open a preview window when needed.
*/
prepare_tagpreview(TRUE, TRUE, FALSE);
}
}
// If it was a CTRL-W CTRL-] command split window now. For ":tab tag"
// open a new tab page.
if (postponed_split && (swb_flags & (SWB_USEOPEN | SWB_USETAB)))
{
buf_T *existing_buf = buflist_findname_exp(fname);
if (existing_buf != NULL)
{
win_T *wp = NULL;
if (swb_flags & SWB_USEOPEN)
wp = buf_jump_open_win(existing_buf);
// If 'switchbuf' contains "usetab": jump to first window in any tab
// page containing "existing_buf" if one exists
if (wp == NULL && (swb_flags & SWB_USETAB))
wp = buf_jump_open_tab(existing_buf);
// We've switched to the buffer, the usual loading of the file must
// be skipped.
if (wp != NULL)
getfile_result = GETFILE_SAME_FILE;
}
}
if (getfile_result == GETFILE_UNUSED
&& (postponed_split || cmdmod.cmod_tab != 0))
{
if (win_split(postponed_split > 0 ? postponed_split : 0,
postponed_split_flags) == FAIL)
{
--RedrawingDisabled;
goto erret;
}
RESET_BINDING(curwin);
}
#endif
if (keep_help)
{
// A :ta from a help file will keep the b_help flag set. For ":ptag"
// we need to use the flag from the window where we came from.
#if defined(FEAT_QUICKFIX)
if (g_do_tagpreview != 0)
keep_help_flag = bt_help(curwin_save->w_buffer);
else
#endif
keep_help_flag = curbuf->b_help;
}
if (getfile_result == GETFILE_UNUSED)
// Careful: getfile() may trigger autocommands and call jumpto_tag()
// recursively.
getfile_result = getfile(0, fname, NULL, TRUE, (linenr_T)0, forceit);
keep_help_flag = FALSE;
if (GETFILE_SUCCESS(getfile_result)) // got to the right file
{
curwin->w_set_curswant = TRUE;
postponed_split = 0;
save_magic_overruled = magic_overruled;
magic_overruled = OPTION_MAGIC_OFF; // always execute with 'nomagic'
#ifdef FEAT_SEARCH_EXTRA
// Save value of no_hlsearch, jumping to a tag is not a real search
save_no_hlsearch = no_hlsearch;
#endif
#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
// getfile() may have cleared options, apply 'previewpopup' again.
if (g_do_tagpreview != 0 && *p_pvp != NUL)
parse_previewpopup(curwin);
#endif
/*
* If 'cpoptions' contains 't', store the search pattern for the "n"
* command. If 'cpoptions' does not contain 't', the search pattern
* is not stored.
*/
if (vim_strchr(p_cpo, CPO_TAGPAT) != NULL)
search_options = 0;
else
search_options = SEARCH_KEEP;
/*
* If the command is a search, try here.
*
* Reset 'smartcase' for the search, since the search pattern was not
* typed by the user.
* Only use do_search() when there is a full search command, without
* anything following.
*/
str = pbuf;
if (pbuf[0] == '/' || pbuf[0] == '?')
str = skip_regexp(pbuf + 1, pbuf[0], FALSE) + 1;
if (str > pbuf_end - 1) // search command with nothing following
{
save_p_ws = p_ws;
save_p_ic = p_ic;
save_p_scs = p_scs;
p_ws = TRUE; // need 'wrapscan' for backward searches
p_ic = FALSE; // don't ignore case now
p_scs = FALSE;
save_lnum = curwin->w_cursor.lnum;
if (tagp.tagline > 0)
// start search before line from "line:" field
curwin->w_cursor.lnum = tagp.tagline - 1;
else
// start search before first line
curwin->w_cursor.lnum = 0;
if (do_search(NULL, pbuf[0], pbuf[0], pbuf + 1, (long)1,
search_options, NULL))
retval = OK;
else
{
int found = 1;
int cc;
/*
* try again, ignore case now
*/
p_ic = TRUE;
if (!do_search(NULL, pbuf[0], pbuf[0], pbuf + 1, (long)1,
search_options, NULL))
{
/*
* Failed to find pattern, take a guess: "^func ("
*/
found = 2;
(void)test_for_static(&tagp);
cc = *tagp.tagname_end;
*tagp.tagname_end = NUL;
sprintf((char *)pbuf, "^%s\\s\\*(", tagp.tagname);
if (!do_search(NULL, '/', '/', pbuf, (long)1,
search_options, NULL))
{
// Guess again: "^char * \<func ("
sprintf((char *)pbuf, "^\\[#a-zA-Z_]\\.\\*\\<%s\\s\\*(",
tagp.tagname);
if (!do_search(NULL, '/', '/', pbuf, (long)1,
search_options, NULL))
found = 0;
}
*tagp.tagname_end = cc;
}
if (found == 0)
{
emsg(_(e_canot_find_tag_pattern));
curwin->w_cursor.lnum = save_lnum;
}
else
{
/*
* Only give a message when really guessed, not when 'ic'
* is set and match found while ignoring case.
*/
if (found == 2 || !save_p_ic)
{
msg(_(e_couldnt_find_tag_just_guessing));
if (!msg_scrolled && msg_silent == 0)
{
out_flush();
ui_delay(1010L, TRUE);
}
}
retval = OK;
}
}
p_ws = save_p_ws;
p_ic = save_p_ic;
p_scs = save_p_scs;
// A search command may have positioned the cursor beyond the end
// of the line. May need to correct that here.
check_cursor();
}
else
{
int save_secure = secure;
// Setup the sandbox for executing the command from the tags file.
secure = 1;
#ifdef HAVE_SANDBOX
++sandbox;
#endif
curwin->w_cursor.lnum = 1; // start command in line 1
do_cmdline_cmd(pbuf);
retval = OK;
// When the command has done something that is not allowed make
// sure the error message can be seen.
if (secure == 2)
wait_return(TRUE);
secure = save_secure;
#ifdef HAVE_SANDBOX
--sandbox;
#endif
}
magic_overruled = save_magic_overruled;
#ifdef FEAT_SEARCH_EXTRA
// restore no_hlsearch when keeping the old search pattern
if (search_options)
set_no_hlsearch(save_no_hlsearch);
#endif
// Return OK if jumped to another file (at least we found the file!).
if (getfile_result == GETFILE_OPEN_OTHER)
retval = OK;
if (retval == OK)
{
/*
* For a help buffer: Put the cursor line at the top of the window,
* the help subject will be below it.
*/
if (curbuf->b_help)
set_topline(curwin, curwin->w_cursor.lnum);
#ifdef FEAT_FOLDING
if ((fdo_flags & FDO_TAG) && old_KeyTyped)
foldOpenCursor();
#endif
}
#if defined(FEAT_QUICKFIX)
if (g_do_tagpreview != 0
&& curwin != curwin_save && win_valid(curwin_save))
{
// Return cursor to where we were
validate_cursor();
redraw_later(VALID);
win_enter(curwin_save, TRUE);
}
#endif
--RedrawingDisabled;
}
else
{
--RedrawingDisabled;
got_int = FALSE; // don't want entering window to fail
if (postponed_split) // close the window
{
win_close(curwin, FALSE);
postponed_split = 0;
}
#if defined(FEAT_QUICKFIX) && defined(FEAT_PROP_POPUP)
else if (WIN_IS_POPUP(curwin))
{
win_T *wp = curwin;
if (win_valid(curwin_save))
win_enter(curwin_save, TRUE);
popup_close(wp->w_id, FALSE);
}
#endif
}
#if defined(FEAT_QUICKFIX) && defined(FEAT_PROP_POPUP)
if (WIN_IS_POPUP(curwin))
// something went wrong, still in popup, but it can't have focus
win_enter(firstwin, TRUE);
#endif
erret:
#if defined(FEAT_QUICKFIX)
g_do_tagpreview = 0; // For next time
#endif
vim_free(lbuf);
vim_free(pbuf);
vim_free(tofree_fname);
vim_free(full_fname);
return retval;
}
/*
* If "expand" is TRUE, expand wildcards in fname.
* If 'tagrelative' option set, change fname (name of file containing tag)
* according to tag_fname (name of tag file containing fname).
* Returns a pointer to allocated memory (or NULL when out of memory).
*/
static char_u *
expand_tag_fname(char_u *fname, char_u *tag_fname, int expand)
{
char_u *p;
char_u *retval;
char_u *expanded_fname = NULL;
expand_T xpc;
/*
* Expand file name (for environment variables) when needed.
*/
if (expand && mch_has_wildcard(fname))
{
ExpandInit(&xpc);
xpc.xp_context = EXPAND_FILES;
expanded_fname = ExpandOne(&xpc, fname, NULL,
WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE);
if (expanded_fname != NULL)
fname = expanded_fname;
}
if ((p_tr || curbuf->b_help)
&& !vim_isAbsName(fname)
&& (p = gettail(tag_fname)) != tag_fname)
{
retval = alloc(MAXPATHL);
if (retval != NULL)
{
STRCPY(retval, tag_fname);
vim_strncpy(retval + (p - tag_fname), fname,
MAXPATHL - (p - tag_fname) - 1);
/*
* Translate names like "src/a/../b/file.c" into "src/b/file.c".
*/
simplify_filename(retval);
}
}
else
retval = vim_strsave(fname);
vim_free(expanded_fname);
return retval;
}
/*
* Check if we have a tag for the buffer with name "buf_ffname".
* This is a bit slow, because of the full path compare in fullpathcmp().
* Return TRUE if tag for file "fname" if tag file "tag_fname" is for current
* file.
*/
static int
test_for_current(
#ifdef FEAT_EMACS_TAGS
int is_etag,
#endif
char_u *fname,
char_u *fname_end,
char_u *tag_fname,
char_u *buf_ffname)
{
int c;
int retval = FALSE;
char_u *fullname;
if (buf_ffname != NULL) // if the buffer has a name
{
#ifdef FEAT_EMACS_TAGS
if (is_etag)
c = 0; // to shut up GCC
else
#endif
{
c = *fname_end;
*fname_end = NUL;
}
fullname = expand_tag_fname(fname, tag_fname, TRUE);
if (fullname != NULL)
{
retval = (fullpathcmp(fullname, buf_ffname, TRUE, TRUE) & FPC_SAME);
vim_free(fullname);
}
#ifdef FEAT_EMACS_TAGS
if (!is_etag)
#endif
*fname_end = c;
}
return retval;
}
/*
* Find the end of the tagaddress.
* Return OK if ";\"" is following, FAIL otherwise.
*/
static int
find_extra(char_u **pp)
{
char_u *str = *pp;
char_u first_char = **pp;
// Repeat for addresses separated with ';'
for (;;)
{
if (VIM_ISDIGIT(*str))
str = skipdigits(str + 1);
else if (*str == '/' || *str == '?')
{
str = skip_regexp(str + 1, *str, FALSE);
if (*str != first_char)
str = NULL;
else
++str;
}
else
{
// not a line number or search string, look for terminator.
str = (char_u *)strstr((char *)str, "|;\"");
if (str != NULL)
{
++str;
break;
}
}
if (str == NULL || *str != ';'
|| !(VIM_ISDIGIT(str[1]) || str[1] == '/' || str[1] == '?'))
break;
++str; // skip ';'
first_char = *str;
}
if (str != NULL && STRNCMP(str, ";\"", 2) == 0)
{
*pp = str;
return OK;
}
return FAIL;
}
/*
* Free a single entry in a tag stack
*/
static void
tagstack_clear_entry(taggy_T *item)
{
VIM_CLEAR(item->tagname);
VIM_CLEAR(item->user_data);
}
int
expand_tags(
int tagnames, // expand tag names
char_u *pat,
int *num_file,
char_u ***file)
{
int i;
int extra_flag;
char_u *name_buf;
size_t name_buf_size = 100;
tagptrs_T t_p;
int ret;
name_buf = alloc(name_buf_size);
if (name_buf == NULL)
return FAIL;
if (tagnames)
extra_flag = TAG_NAMES;
else
extra_flag = 0;
if (pat[0] == '/')
ret = find_tags(pat + 1, num_file, file,
TAG_REGEXP | extra_flag | TAG_VERBOSE | TAG_NO_TAGFUNC,
TAG_MANY, curbuf->b_ffname);
else
ret = find_tags(pat, num_file, file,
TAG_REGEXP | extra_flag | TAG_VERBOSE | TAG_NO_TAGFUNC | TAG_NOIC,
TAG_MANY, curbuf->b_ffname);
if (ret == OK && !tagnames)
{
// Reorganize the tags for display and matching as strings of:
// "<tagname>\0<kind>\0<filename>\0"
for (i = 0; i < *num_file; i++)
{
size_t len;
parse_match((*file)[i], &t_p);
len = t_p.tagname_end - t_p.tagname;
if (len > name_buf_size - 3)
{
char_u *buf;
name_buf_size = len + 3;
buf = vim_realloc(name_buf, name_buf_size);
if (buf == NULL)
{
vim_free(name_buf);
return FAIL;
}
name_buf = buf;
}
mch_memmove(name_buf, t_p.tagname, len);
name_buf[len++] = 0;
name_buf[len++] = (t_p.tagkind != NULL && *t_p.tagkind)
? *t_p.tagkind : 'f';
name_buf[len++] = 0;
mch_memmove((*file)[i] + len, t_p.fname,
t_p.fname_end - t_p.fname);
(*file)[i][len + (t_p.fname_end - t_p.fname)] = 0;
mch_memmove((*file)[i], name_buf, len);
}
}
vim_free(name_buf);
return ret;
}
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Add a tag field to the dictionary "dict".
* Return OK or FAIL.
*/
static int
add_tag_field(
dict_T *dict,
char *field_name,
char_u *start, // start of the value
char_u *end) // after the value; can be NULL
{
char_u *buf;
int len = 0;
int retval;
// check that the field name doesn't exist yet
if (dict_find(dict, (char_u *)field_name, -1) != NULL)
{
if (p_verbose > 0)
{
verbose_enter();
smsg(_("Duplicate field name: %s"), field_name);
verbose_leave();
}
return FAIL;
}
buf = alloc(MAXPATHL);
if (buf == NULL)
return FAIL;
if (start != NULL)
{
if (end == NULL)
{
end = start + STRLEN(start);
while (end > start && (end[-1] == '\r' || end[-1] == '\n'))
--end;
}
len = (int)(end - start);
if (len > MAXPATHL - 1)
len = MAXPATHL - 1;
vim_strncpy(buf, start, len);
}
buf[len] = NUL;
retval = dict_add_string(dict, field_name, buf);
vim_free(buf);
return retval;
}
/*
* Add the tags matching the specified pattern "pat" to the list "list"
* as a dictionary. Use "buf_fname" for priority, unless NULL.
*/
int
get_tags(list_T *list, char_u *pat, char_u *buf_fname)
{
int num_matches, i, ret;
char_u **matches, *p;
char_u *full_fname;
dict_T *dict;
tagptrs_T tp;
long is_static;
ret = find_tags(pat, &num_matches, &matches,
TAG_REGEXP | TAG_NOIC, (int)MAXCOL, buf_fname);
if (ret == OK && num_matches > 0)
{
for (i = 0; i < num_matches; ++i)
{
parse_match(matches[i], &tp);
is_static = test_for_static(&tp);
// Skip pseudo-tag lines.
if (STRNCMP(tp.tagname, "!_TAG_", 6) == 0)
{
vim_free(matches[i]);
continue;
}
if ((dict = dict_alloc()) == NULL)
ret = FAIL;
if (list_append_dict(list, dict) == FAIL)
ret = FAIL;
full_fname = tag_full_fname(&tp);
if (add_tag_field(dict, "name", tp.tagname, tp.tagname_end) == FAIL
|| add_tag_field(dict, "filename", full_fname,
NULL) == FAIL
|| add_tag_field(dict, "cmd", tp.command,
tp.command_end) == FAIL
|| add_tag_field(dict, "kind", tp.tagkind,
tp.tagkind_end) == FAIL
|| dict_add_number(dict, "static", is_static) == FAIL)
ret = FAIL;
vim_free(full_fname);
if (tp.command_end != NULL)
{
for (p = tp.command_end + 3;
*p != NUL && *p != '\n' && *p != '\r'; MB_PTR_ADV(p))
{
if (p == tp.tagkind || (p + 5 == tp.tagkind
&& STRNCMP(p, "kind:", 5) == 0))
// skip "kind:<kind>" and "<kind>"
p = tp.tagkind_end - 1;
else if (STRNCMP(p, "file:", 5) == 0)
// skip "file:" (static tag)
p += 4;
else if (!VIM_ISWHITE(*p))
{
char_u *s, *n;
int len;
// Add extra field as a dict entry. Fields are
// separated by Tabs.
n = p;
while (*p != NUL && *p >= ' ' && *p < 127 && *p != ':')
++p;
len = (int)(p - n);
if (*p == ':' && len > 0)
{
s = ++p;
while (*p != NUL && *p >= ' ')
++p;
n[len] = NUL;
if (add_tag_field(dict, (char *)n, s, p) == FAIL)
ret = FAIL;
n[len] = ':';
}
else
// Skip field without colon.
while (*p != NUL && *p >= ' ')
++p;
if (*p == NUL)
break;
}
}
}
vim_free(matches[i]);
}
vim_free(matches);
}
return ret;
}
/*
* Return information about 'tag' in dict 'retdict'.
*/
static void
get_tag_details(taggy_T *tag, dict_T *retdict)
{
list_T *pos;
fmark_T *fmark;
dict_add_string(retdict, "tagname", tag->tagname);
dict_add_number(retdict, "matchnr", tag->cur_match + 1);
dict_add_number(retdict, "bufnr", tag->cur_fnum);
if (tag->user_data)
dict_add_string(retdict, "user_data", tag->user_data);
if ((pos = list_alloc_id(aid_tagstack_from)) == NULL)
return;
dict_add_list(retdict, "from", pos);
fmark = &tag->fmark;
list_append_number(pos,
(varnumber_T)(fmark->fnum != -1 ? fmark->fnum : 0));
list_append_number(pos, (varnumber_T)fmark->mark.lnum);
list_append_number(pos, (varnumber_T)(fmark->mark.col == MAXCOL ?
MAXCOL : fmark->mark.col + 1));
list_append_number(pos, (varnumber_T)fmark->mark.coladd);
}
/*
* Return the tag stack entries of the specified window 'wp' in dictionary
* 'retdict'.
*/
void
get_tagstack(win_T *wp, dict_T *retdict)
{
list_T *l;
int i;
dict_T *d;
dict_add_number(retdict, "length", wp->w_tagstacklen);
dict_add_number(retdict, "curidx", wp->w_tagstackidx + 1);
l = list_alloc_id(aid_tagstack_items);
if (l == NULL)
return;
dict_add_list(retdict, "items", l);
for (i = 0; i < wp->w_tagstacklen; i++)
{
if ((d = dict_alloc_id(aid_tagstack_details)) == NULL)
return;
list_append_dict(l, d);
get_tag_details(&wp->w_tagstack[i], d);
}
}
/*
* Free all the entries in the tag stack of the specified window
*/
static void
tagstack_clear(win_T *wp)
{
int i;
// Free the current tag stack
for (i = 0; i < wp->w_tagstacklen; ++i)
tagstack_clear_entry(&wp->w_tagstack[i]);
wp->w_tagstacklen = 0;
wp->w_tagstackidx = 0;
}
/*
* Remove the oldest entry from the tag stack and shift the rest of
* the entries to free up the top of the stack.
*/
static void
tagstack_shift(win_T *wp)
{
taggy_T *tagstack = wp->w_tagstack;
int i;
tagstack_clear_entry(&tagstack[0]);
for (i = 1; i < wp->w_tagstacklen; ++i)
tagstack[i - 1] = tagstack[i];
wp->w_tagstacklen--;
}
/*
* Push a new item to the tag stack
*/
static void
tagstack_push_item(
win_T *wp,
char_u *tagname,
int cur_fnum,
int cur_match,
pos_T mark,
int fnum,
char_u *user_data)
{
taggy_T *tagstack = wp->w_tagstack;
int idx = wp->w_tagstacklen; // top of the stack
// if the tagstack is full: remove the oldest entry
if (idx >= TAGSTACKSIZE)
{
tagstack_shift(wp);
idx = TAGSTACKSIZE - 1;
}
wp->w_tagstacklen++;
tagstack[idx].tagname = tagname;
tagstack[idx].cur_fnum = cur_fnum;
tagstack[idx].cur_match = cur_match;
if (tagstack[idx].cur_match < 0)
tagstack[idx].cur_match = 0;
tagstack[idx].fmark.mark = mark;
tagstack[idx].fmark.fnum = fnum;
tagstack[idx].user_data = user_data;
}
/*
* Add a list of items to the tag stack in the specified window
*/
static void
tagstack_push_items(win_T *wp, list_T *l)
{
listitem_T *li;
dictitem_T *di;
dict_T *itemdict;
char_u *tagname;
pos_T mark;
int fnum;
// Add one entry at a time to the tag stack
FOR_ALL_LIST_ITEMS(l, li)
{
if (li->li_tv.v_type != VAR_DICT || li->li_tv.vval.v_dict == NULL)
continue; // Skip non-dict items
itemdict = li->li_tv.vval.v_dict;
// parse 'from' for the cursor position before the tag jump
if ((di = dict_find(itemdict, (char_u *)"from", -1)) == NULL)
continue;
if (list2fpos(&di->di_tv, &mark, &fnum, NULL, FALSE) != OK)
continue;
if ((tagname =
dict_get_string(itemdict, (char_u *)"tagname", TRUE)) == NULL)
continue;
if (mark.col > 0)
mark.col--;
tagstack_push_item(wp, tagname,
(int)dict_get_number(itemdict, (char_u *)"bufnr"),
(int)dict_get_number(itemdict, (char_u *)"matchnr") - 1,
mark, fnum,
dict_get_string(itemdict, (char_u *)"user_data", TRUE));
}
}
/*
* Set the current index in the tag stack. Valid values are between 0
* and the stack length (inclusive).
*/
static void
tagstack_set_curidx(win_T *wp, int curidx)
{
wp->w_tagstackidx = curidx;
if (wp->w_tagstackidx < 0) // sanity check
wp->w_tagstackidx = 0;
if (wp->w_tagstackidx > wp->w_tagstacklen)
wp->w_tagstackidx = wp->w_tagstacklen;
}
/*
* Set the tag stack entries of the specified window.
* 'action' is set to one of:
* 'a' for append
* 'r' for replace
* 't' for truncate
*/
int
set_tagstack(win_T *wp, dict_T *d, int action)
{
dictitem_T *di;
list_T *l = NULL;
#ifdef FEAT_EVAL
// not allowed to alter the tag stack entries from inside tagfunc
if (tfu_in_use)
{
emsg(_(e_cannot_modify_tag_stack_within_tagfunc));
return FAIL;
}
#endif
if ((di = dict_find(d, (char_u *)"items", -1)) != NULL)
{
if (di->di_tv.v_type != VAR_LIST)
{
emsg(_(e_list_required));
return FAIL;
}
l = di->di_tv.vval.v_list;
}
if ((di = dict_find(d, (char_u *)"curidx", -1)) != NULL)
tagstack_set_curidx(wp, (int)tv_get_number(&di->di_tv) - 1);
if (action == 't') // truncate the stack
{
taggy_T *tagstack = wp->w_tagstack;
int tagstackidx = wp->w_tagstackidx;
int tagstacklen = wp->w_tagstacklen;
// delete all the tag stack entries above the current entry
while (tagstackidx < tagstacklen)
tagstack_clear_entry(&tagstack[--tagstacklen]);
wp->w_tagstacklen = tagstacklen;
}
if (l != NULL)
{
if (action == 'r') // replace the stack
tagstack_clear(wp);
tagstack_push_items(wp, l);
// set the current index after the last entry
wp->w_tagstackidx = wp->w_tagstacklen;
}
return OK;
}
#endif
|