1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
| | ;;; cc-engine.el --- core syntax guessing engine for CC mode -*- coding: utf-8 -*-
;; Copyright (C) 1985, 1987, 1992-2020 Free Software Foundation, Inc.
;; Authors: 2001- Alan Mackenzie
;; 1998- Martin Stjernholm
;; 1992-1999 Barry A. Warsaw
;; 1987 Dave Detlefs
;; 1987 Stewart Clamen
;; 1985 Richard M. Stallman
;; Maintainer: bug-cc-mode@gnu.org
;; Created: 22-Apr-1997 (split from cc-mode.el)
;; Keywords: c languages
;; Package: cc-mode
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; The functions which have docstring documentation can be considered
;; part of an API which other packages can use in CC Mode buffers.
;; Otoh, undocumented functions and functions with the documentation
;; in comments are considered purely internal and can change semantics
;; or even disappear in the future.
;;
;; (This policy applies to CC Mode as a whole, not just this file. It
;; probably also applies to many other Emacs packages, but here it's
;; clearly spelled out.)
;; Hidden buffer changes
;;
;; Various functions in CC Mode use text properties for caching and
;; syntactic markup purposes, and those of them that might modify such
;; properties but still don't modify the buffer in a visible way are
;; said to do "hidden buffer changes". They should be used within
;; `c-save-buffer-state' or a similar function that saves and restores
;; buffer modifiedness, disables buffer change hooks, etc.
;;
;; Interactive functions are assumed to not do hidden buffer changes,
;; except in the specific parts of them that do real changes.
;;
;; Lineup functions are assumed to do hidden buffer changes. They
;; must not do real changes, though.
;;
;; All other functions that do hidden buffer changes have that noted
;; in their doc string or comment.
;;
;; The intention with this system is to avoid wrapping every leaf
;; function that do hidden buffer changes inside
;; `c-save-buffer-state'. It should be used as near the top of the
;; interactive functions as possible.
;;
;; Functions called during font locking are allowed to do hidden
;; buffer changes since the font-lock package run them in a context
;; similar to `c-save-buffer-state' (in fact, that function is heavily
;; inspired by `save-buffer-state' in the font-lock package).
;; Use of text properties
;;
;; CC Mode uses several text properties internally to mark up various
;; positions, e.g. to improve speed and to eliminate glitches in
;; interactive refontification.
;;
;; Note: This doc is for internal use only. Other packages should not
;; assume that these text properties are used as described here.
;;
;; 'category
;; Used for "indirection". With its help, some other property can
;; be cheaply and easily switched on or off everywhere it occurs.
;;
;; 'syntax-table
;; Used to modify the syntax of some characters. It is used to
;; mark the "<" and ">" of angle bracket parens with paren syntax, to
;; "hide" obtrusive characters in preprocessor lines, and to mark C++
;; raw strings to enable their fontification.
;;
;; This property is used on single characters and is therefore
;; always treated as front and rear nonsticky (or start and end open
;; in XEmacs vocabulary). It's therefore installed on
;; `text-property-default-nonsticky' if that variable exists (Emacs
;; >= 21).
;;
;; 'c-fl-syn-tab
;; Saves the value of syntax-table properties which have been
;; temporarily removed from certain buffer positions. The syntax-table
;; properties are restored during c-before-change, c-after-change, and
;; font locking. The purpose of the temporary removal is to enable
;; C-M-* key sequences to operate over bogus pairs of string delimiters
;; which are "adjacent", yet do not delimit a string.
;;
;; 'c-is-sws and 'c-in-sws
;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
;; speed them up. See the comment blurb before `c-put-is-sws'
;; below for further details.
;;
;; 'c-type
;; This property is used on single characters to mark positions with
;; special syntactic relevance of various sorts. Its primary use is
;; to avoid glitches when multiline constructs are refontified
;; interactively (on font lock decoration level 3). It's cleared in
;; a region before it's fontified and is then put on relevant chars
;; in that region as they are encountered during the fontification.
;; The value specifies the kind of position:
;;
;; 'c-decl-arg-start
;; Put on the last char of the token preceding each declaration
;; inside a declaration style arglist (typically in a function
;; prototype).
;;
;; 'c-decl-end
;; Put on the last char of the token preceding a declaration.
;; This is used in cases where declaration boundaries can't be
;; recognized simply by looking for a token like ";" or "}".
;; `c-type-decl-end-used' must be set if this is used (see also
;; `c-find-decl-spots').
;;
;; 'c-<>-arg-sep
;; Put on the commas that separate arguments in angle bracket
;; arglists like C++ template arglists.
;;
;; 'c-decl-id-start and 'c-decl-type-start
;; Put on the last char of the token preceding each declarator
;; in the declarator list of a declaration. They are also used
;; between the identifiers cases like enum declarations.
;; 'c-decl-type-start is used when the declarators are types,
;; 'c-decl-id-start otherwise.
;;
;; 'c-not-decl
;; Put on the brace which introduces a brace list and on the commas
;; which separate the elements within it.
;;
;; 'c-awk-NL-prop
;; Used in AWK mode to mark the various kinds of newlines. See
;; cc-awk.el.
;;; Code:
(eval-when-compile
(let ((load-path
(if (and (boundp 'byte-compile-dest-file)
(stringp byte-compile-dest-file))
(cons (file-name-directory byte-compile-dest-file) load-path)
load-path)))
(load "cc-bytecomp" nil t)))
(cc-require 'cc-defs)
(cc-require-when-compile 'cc-langs)
(cc-require 'cc-vars)
(defvar c-doc-line-join-re)
(defvar c-doc-bright-comment-start-re)
(defvar c-doc-line-join-end-ch)
(cc-bytecomp-defvar c-min-syn-tab-mkr)
(cc-bytecomp-defvar c-max-syn-tab-mkr)
(cc-bytecomp-defun c-clear-syn-tab)
(cc-bytecomp-defun c-clear-string-fences)
(cc-bytecomp-defun c-restore-string-fences)
\f
;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
(defmacro c-declare-lang-variables ()
`(progn
,@(c--mapcan (lambda (init)
`(,(if (elt init 2)
`(defvar ,(car init) nil ,(elt init 2))
`(defvar ,(car init) nil))
(make-variable-buffer-local ',(car init))))
(cdr c-lang-variable-inits))))
(c-declare-lang-variables)
\f
;;; Internal state variables.
;; Internal state of hungry delete key feature
(defvar c-hungry-delete-key nil)
(make-variable-buffer-local 'c-hungry-delete-key)
;; The electric flag (toggled by `c-toggle-electric-state').
;; If t, electric actions (like automatic reindentation, and (if
;; c-auto-newline is also set) auto newlining) will happen when an electric
;; key like `{' is pressed (or an electric keyword like `else').
(defvar c-electric-flag t)
(make-variable-buffer-local 'c-electric-flag)
;; Internal state of auto newline feature.
(defvar c-auto-newline nil)
(make-variable-buffer-local 'c-auto-newline)
;; Included in the mode line to indicate the active submodes.
;; (defvar c-submode-indicators nil)
;; (make-variable-buffer-local 'c-submode-indicators)
(defun c-calculate-state (arg prevstate)
;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
;; arg is nil or zero, toggle the state. If arg is negative, turn
;; the state off, and if arg is positive, turn the state on
(if (or (not arg)
(zerop (setq arg (prefix-numeric-value arg))))
(not prevstate)
(> arg 0)))
\f
;; Basic handling of preprocessor directives.
;; This is a dynamically bound cache used together with
;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
;; works as long as point doesn't cross a macro boundary.
(defvar c-macro-start 'unknown)
(defsubst c-query-and-set-macro-start ()
(if (symbolp c-macro-start)
(setq c-macro-start (save-excursion
(c-save-buffer-state ()
(and (c-beginning-of-macro)
(point)))))
c-macro-start))
(defsubst c-query-macro-start ()
(if (symbolp c-macro-start)
(save-excursion
(c-save-buffer-state ()
(and (c-beginning-of-macro)
(point))))
c-macro-start))
;; One element macro cache to cope with continual movement within very large
;; CPP macros.
(defvar c-macro-cache nil)
(make-variable-buffer-local 'c-macro-cache)
;; Nil or cons of the bounds of the most recent CPP form probed by
;; `c-beginning-of-macro', `c-end-of-macro' or `c-syntactic-end-of-macro'.
;; The cdr will be nil if we know only the start of the CPP form.
(defvar c-macro-cache-start-pos nil)
(make-variable-buffer-local 'c-macro-cache-start-pos)
;; The starting position from where we determined `c-macro-cache'.
(defvar c-macro-cache-syntactic nil)
(make-variable-buffer-local 'c-macro-cache-syntactic)
;; Either nil, or the syntactic end of the macro currently represented by
;; `c-macro-cache'.
(defvar c-macro-cache-no-comment nil)
(make-variable-buffer-local 'c-macro-cache-no-comment)
;; Either nil, or the position of a comment which is open at the end of the
;; macro represented by `c-macro-cache'.
(defun c-invalidate-macro-cache (beg _end)
;; Called from a before-change function. If the change region is before or
;; in the macro characterized by `c-macro-cache' etc., nullify it
;; appropriately. BEG and END are the standard before-change-functions
;; parameters. END isn't used.
(cond
((null c-macro-cache))
((<= beg (car c-macro-cache))
(setq c-macro-cache nil
c-macro-cache-start-pos nil
c-macro-cache-syntactic nil
c-macro-cache-no-comment nil))
((and (cdr c-macro-cache)
(< beg (cdr c-macro-cache)))
(setcdr c-macro-cache nil)
(setq c-macro-cache-start-pos beg
c-macro-cache-syntactic nil
c-macro-cache-no-comment nil))
((and c-macro-cache-start-pos
(< beg c-macro-cache-start-pos))
(setq c-macro-cache-start-pos beg
c-macro-cache-syntactic nil
c-macro-cache-no-comment nil))))
(defun c-macro-is-genuine-p ()
;; Check that the ostensible CPP construct at point is a real one. In
;; particular, if point is on the first line of a narrowed buffer, make sure
;; that the "#" isn't, say, the second character of a "##" operator. Return
;; t when the macro is real, nil otherwise.
(let ((here (point)))
(beginning-of-line)
(prog1
(if (and (eq (point) (point-min))
(/= (point) 1))
(save-restriction
(widen)
(beginning-of-line)
(and (looking-at c-anchored-cpp-prefix)
(eq (match-beginning 1) here)))
t)
(goto-char here))))
(defun c-beginning-of-macro (&optional lim)
"Go to the beginning of a preprocessor directive.
Leave point at the beginning of the directive and return t if in one,
otherwise return nil and leave point unchanged.
Note that this function might do hidden buffer changes. See the
comment at the start of cc-engine.el for more info."
(let ((here (point))
(pause (c-point 'eol)))
(when c-opt-cpp-prefix
(if (and (car c-macro-cache)
(>= (point) (car c-macro-cache))
(or (and (cdr c-macro-cache)
(<= (point) (cdr c-macro-cache)))
(<= (point) c-macro-cache-start-pos)))
(unless (< (car c-macro-cache) (or lim (point-min)))
(progn (goto-char (max (or lim (point-min)) (car c-macro-cache)))
(setq c-macro-cache-start-pos
(max c-macro-cache-start-pos here))
t))
(setq c-macro-cache nil
c-macro-cache-start-pos nil
c-macro-cache-syntactic nil
c-macro-cache-no-comment nil)
(save-restriction
(if lim (narrow-to-region lim (point-max)))
(beginning-of-line)
(when (or (null lim)
(>= here lim))
(save-match-data
;; Note the similarity of the code here to some in
;; `c-backward-sws'.
(while
(progn
(while (eq (char-before (1- (point))) ?\\)
(forward-line -1))
(when (and c-last-c-comment-end-on-line-re
(re-search-forward
c-last-c-comment-end-on-line-re pause t))
(goto-char (match-end 1))
(if (c-backward-single-comment)
(progn
(beginning-of-line)
(setq pause (point)))
(goto-char pause)
nil))))))
(back-to-indentation)
(if (and (<= (point) here)
(save-match-data (looking-at c-opt-cpp-start))
(c-macro-is-genuine-p))
(progn
(setq c-macro-cache (cons (point) nil)
c-macro-cache-start-pos here
c-macro-cache-syntactic nil)
t)
(goto-char here)
nil))))))
(defun c-end-of-macro (&optional lim)
"Go to the end of a preprocessor directive.
More accurately, move the point to the end of the closest following
line that doesn't end with a line continuation backslash - no check is
done that the point is inside a cpp directive to begin with, although
it is assumed that point isn't inside a comment or string.
If LIM is provided, it is a limit position at which point is left
if the end of the macro doesn't occur earlier.
Note that this function might do hidden buffer changes. See the
comment at the start of cc-engine.el for more info."
(save-restriction
(if lim (narrow-to-region (point-min) lim))
(if (and (cdr c-macro-cache)
(<= (point) (cdr c-macro-cache))
(>= (point) (car c-macro-cache)))
(goto-char (cdr c-macro-cache))
(unless (and (car c-macro-cache)
(<= (point) c-macro-cache-start-pos)
(>= (point) (car c-macro-cache)))
(setq c-macro-cache nil
c-macro-cache-start-pos nil
c-macro-cache-syntactic nil
c-macro-cache-no-comment nil))
(save-match-data
(let ((safe-pos (point))) ; a point outside any literal.
;; Move over stuff followed by a multiline block comment lacking
;; escaped newlines each time around this loop.
(while
(progn
(while (progn
(end-of-line)
(when (and (eq (char-before) ?\\)
(not (eobp)))
(forward-char)
t)))
(let ((s (parse-partial-sexp safe-pos (point))))
(when ;; Are we in a block comment?
(and (nth 4 s) (not (nth 7 s)))
(progn
;; Move to after the block comment.
(parse-partial-sexp
(point) (point-max) nil nil s 'syntax-table)
(setq safe-pos (point)))))))
(when (and (car c-macro-cache)
(> (point) (car c-macro-cache)) ; in case we have a
; zero-sized region.
(not lim))
(setcdr c-macro-cache (point))
(setq c-macro-cache-syntactic nil)))))))
(defun c-syntactic-end-of-macro ()
;; Go to the end of a CPP directive, or a "safe" pos just before.
;;
;; This is normally the end of the next non-escaped line. A "safe"
;; position is one not within a string or comment. (The EOL on a line
;; comment is NOT "safe").
;;
;; This function must only be called from the beginning of a CPP construct.
;;
;; Note that this function might do hidden buffer changes. See the comment
;; at the start of cc-engine.el for more info.
(let* ((here (point))
(there (progn (c-end-of-macro) (point)))
s)
(if c-macro-cache-syntactic
(goto-char c-macro-cache-syntactic)
(setq s (parse-partial-sexp here there))
(while (and (or (nth 3 s) ; in a string
(and (nth 4 s) ; in a comment (maybe at end of line comment)
(not (eq (nth 7 s) 'syntax-table)))) ; Not a pseudo comment
(> there here)) ; No infinite loops, please.
(setq there (1- (nth 8 s)))
(setq s (parse-partial-sexp here there)))
(setq c-macro-cache-syntactic (point)))
(point)))
(defun c-no-comment-end-of-macro ()
;; Go to the start of the comment which is open at the end of the current
;; CPP directive, or to the end of that directive. For this purpose, open
;; strings are ignored.
;;
;; This function must only be called from the beginning of a CPP construct.
;;
;; Note that this function might do hidden buffer changes. See the comment
;; at the start of cc-engine.el for more info.
(let* ((here (point))
(there (progn (c-end-of-macro) (point)))
s)
(if c-macro-cache-no-comment
(goto-char c-macro-cache-no-comment)
(setq s (parse-partial-sexp here there))
(while (and (nth 3 s) ; in a string
(> there here)) ; No infinite loops, please.
(setq here (1+ (nth 8 s)))
(setq s (parse-partial-sexp here there)))
(when (and (nth 4 s)
(not (eq (nth 7 s) 'syntax-table))) ; no pseudo comments.
(goto-char (nth 8 s)))
(setq c-macro-cache-no-comment (point)))
(point)))
(defun c-forward-over-cpp-define-id ()
;; Assuming point is at the "#" that introduces a preprocessor
;; directive, it's moved forward to the end of the identifier which is
;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
;; is returned in this case, in all other cases nil is returned and
;; point isn't moved.
;;
;; This function might do hidden buffer changes.
(when (and c-opt-cpp-macro-define-id
(looking-at c-opt-cpp-macro-define-id))
(goto-char (match-end 0))))
(defun c-forward-to-cpp-define-body ()
;; Assuming point is at the "#" that introduces a preprocessor
;; directive, it's moved forward to the start of the definition body
;; if it's a "#define" (or whatever c-opt-cpp-macro-define
;; specifies). Non-nil is returned in this case, in all other cases
;; nil is returned and point isn't moved.
;;
;; This function might do hidden buffer changes.
(when (and c-opt-cpp-macro-define-start
(looking-at c-opt-cpp-macro-define-start)
(not (= (match-end 0) (c-point 'eol))))
(goto-char (match-end 0))))
\f
;;; Basic utility functions.
(defun c-delq-from-dotted-list (elt dlist)
;; If ELT is a member of the (possibly dotted) list DLIST, remove all
;; occurrences of it (except for any in the last cdr of DLIST).
;;
;; Call this as (setq DLIST (c-delq-from-dotted-list ELT DLIST)), as
;; sometimes the original structure is changed, sometimes it's not.
;;
;; This function is needed in Emacs < 24.5, and possibly XEmacs, because
;; `delq' throws an error in these versions when given a dotted list.
(let ((tail dlist) prev)
(while (consp tail)
(if (eq (car tail) elt)
(if prev
(setcdr prev (cdr tail))
(setq dlist (cdr dlist)))
(setq prev tail))
(setq tail (cdr tail)))
dlist))
(defun c-syntactic-content (from to paren-level)
;; Return the given region as a string where all syntactic
;; whitespace is removed or, where necessary, replaced with a single
;; space. If PAREN-LEVEL is given then all parens in the region are
;; collapsed to "()", "[]" etc.
;;
;; This function might do hidden buffer changes.
(save-excursion
(save-restriction
(narrow-to-region from to)
(goto-char from)
(let* ((parts (list nil)) (tail parts) pos in-paren)
(while (re-search-forward c-syntactic-ws-start to t)
(goto-char (setq pos (match-beginning 0)))
(c-forward-syntactic-ws)
(if (= (point) pos)
(forward-char)
(when paren-level
(save-excursion
(setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
pos (point))))
(if (and (> pos from)
(< (point) to)
(looking-at "\\w\\|\\s_")
(save-excursion
(goto-char (1- pos))
(looking-at "\\w\\|\\s_")))
(progn
(setcdr tail (list (buffer-substring-no-properties from pos)
" "))
(setq tail (cddr tail)))
(setcdr tail (list (buffer-substring-no-properties from pos)))
(setq tail (cdr tail)))
(when in-paren
(when (= (car (parse-partial-sexp pos to -1)) -1)
(setcdr tail (list (buffer-substring-no-properties
(1- (point)) (point))))
(setq tail (cdr tail))))
(setq from (point))))
(setcdr tail (list (buffer-substring-no-properties from to)))
(apply 'concat (cdr parts))))))
(defun c-shift-line-indentation (shift-amt)
;; Shift the indentation of the current line with the specified
;; amount (positive inwards). The buffer is modified only if
;; SHIFT-AMT isn't equal to zero.
(let ((pos (- (point-max) (point)))
(c-macro-start c-macro-start)
tmp-char-inserted)
(if (zerop shift-amt)
nil
;; If we're on an empty line inside a macro, we take the point
;; to be at the current indentation and shift it to the
;; appropriate column. This way we don't treat the extra
;; whitespace out to the line continuation as indentation.
(when (and (c-query-and-set-macro-start)
(looking-at "[ \t]*\\\\$")
(save-excursion
(skip-chars-backward " \t")
(bolp)))
(insert ?x)
(backward-char)
(setq tmp-char-inserted t))
(unwind-protect
(let ((col (current-indentation)))
(delete-region (c-point 'bol) (c-point 'boi))
(beginning-of-line)
(indent-to (+ col shift-amt)))
(when tmp-char-inserted
(delete-char 1))))
;; If initial point was within line's indentation and we're not on
;; a line with a line continuation in a macro, position after the
;; indentation. Else stay at same point in text.
(if (and (< (point) (c-point 'boi))
(not tmp-char-inserted))
(back-to-indentation)
(if (> (- (point-max) pos) (point))
(goto-char (- (point-max) pos))))))
(defsubst c-keyword-sym (keyword)
;; Return non-nil if the string KEYWORD is a known keyword. More
;; precisely, the value is the symbol for the keyword in
;; `c-keywords-obarray'.
(intern-soft keyword c-keywords-obarray))
(defsubst c-keyword-member (keyword-sym lang-constant)
;; Return non-nil if the symbol KEYWORD-SYM, as returned by
;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
;; nil then the result is nil.
(get keyword-sym lang-constant))
;; String syntax chars, suitable for skip-syntax-(forward|backward).
(defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
"\"|"
"\""))
;; Regexp matching string limit syntax.
(defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
"\\s\"\\|\\s|"
"\\s\""))
;; Regexp matching WS followed by string limit syntax.
(defconst c-ws*-string-limit-regexp
(concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
;; Holds formatted error strings for the few cases where parse errors
;; are reported.
(defvar c-parsing-error nil)
(make-variable-buffer-local 'c-parsing-error)
(defun c-echo-parsing-error (&optional quiet)
(when (and c-report-syntactic-errors c-parsing-error (not quiet))
(c-benign-error "%s" c-parsing-error))
c-parsing-error)
;; Faces given to comments and string literals. This is used in some
;; situations to speed up recognition; it isn't mandatory that font
;; locking is in use. This variable is extended with the face in
;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
(defvar c-literal-faces
(append '(font-lock-comment-face font-lock-string-face)
(when (facep 'font-lock-comment-delimiter-face)
;; New in Emacs 22.
'(font-lock-comment-delimiter-face))))
(defsubst c-put-c-type-property (pos value)
;; Put a c-type property with the given value at POS.
(c-put-char-property pos 'c-type value))
(defun c-clear-c-type-property (from to value)
;; Remove all occurrences of the c-type property that has the given
;; value in the region between FROM and TO. VALUE is assumed to not
;; be nil.
;;
;; Note: This assumes that c-type is put on single chars only; it's
;; very inefficient if matching properties cover large regions.
(save-excursion
(goto-char from)
(while (progn
(when (eq (get-text-property (point) 'c-type) value)
(c-clear-char-property (point) 'c-type))
(goto-char (c-next-single-property-change (point) 'c-type nil to))
(< (point) to)))))
\f
;; Some debug tools to visualize various special positions. This
;; debug code isn't as portable as the rest of CC Mode.
(cc-bytecomp-defun overlays-in)
(cc-bytecomp-defun overlay-get)
(cc-bytecomp-defun overlay-start)
(cc-bytecomp-defun overlay-end)
(cc-bytecomp-defun delete-overlay)
(cc-bytecomp-defun overlay-put)
(cc-bytecomp-defun make-overlay)
(defun c-debug-add-face (beg end face)
(c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
(while overlays
(setq overlay (car overlays)
overlays (cdr overlays))
(when (eq (overlay-get overlay 'face) face)
(setq beg (min beg (overlay-start overlay))
end (max end (overlay-end overlay)))
(delete-overlay overlay)))
(overlay-put (make-overlay beg end) 'face face)))
(defun c-debug-remove-face (beg end face)
(c-save-buffer-state ((overlays (overlays-in beg end)) overlay
(ol-beg beg) (ol-end end))
(while overlays
(setq overlay (car overlays)
overlays (cdr overlays))
(when (eq (overlay-get overlay 'face) face)
(setq ol-beg (min ol-beg (overlay-start overlay))
ol-end (max ol-end (overlay-end overlay)))
(delete-overlay overlay)))
(when (< ol-beg beg)
(overlay-put (make-overlay ol-beg beg) 'face face))
(when (> ol-end end)
(overlay-put (make-overlay end ol-end) 'face face))))
\f
(defmacro c-looking-at-c++-attribute ()
;; If we're in C++ Mode, and point is at the [[ introducing an attribute,
;; return the position of the end of the attribute, otherwise return nil.
;; The match data are NOT preserved over this macro.
`(and
(c-major-mode-is 'c++-mode)
(looking-at "\\[\\[")
(save-excursion
(and
(c-go-list-forward)
(eq (char-before) ?\])
(eq (char-before (1- (point))) ?\])
(point)))))
\f
;; `c-beginning-of-statement-1' and accompanying stuff.
;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
;; better way should be implemented, but this will at least shut up
;; the byte compiler.
(defvar c-maybe-labelp)
(defvar c-commas-bound-stmts nil)
;; Set to non-nil when `c-beginning-of-statement-1' is to regard a comma as
;; a statement terminator.
;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
;; Macros used internally in c-beginning-of-statement-1 for the
;; automaton actions.
(defmacro c-bos-push-state ()
'(setq stack (cons (cons state saved-pos)
stack)))
(defmacro c-bos-pop-state (&optional do-if-done)
`(if (setq state (car (car stack))
saved-pos (cdr (car stack))
stack (cdr stack))
t
,do-if-done
(setq pre-stmt-found t)
(throw 'loop nil)))
(defmacro c-bos-pop-state-and-retry ()
'(throw 'loop (setq state (car (car stack))
saved-pos (cdr (car stack))
pre-stmt-found (not (cdr stack))
;; Throw nil if stack is empty, else throw non-nil.
stack (cdr stack))))
(defmacro c-bos-save-pos ()
'(setq saved-pos (vector pos tok ptok pptok)))
(defmacro c-bos-restore-pos ()
'(unless (eq (elt saved-pos 0) start)
(setq pos (elt saved-pos 0)
tok (elt saved-pos 1)
ptok (elt saved-pos 2)
pptok (elt saved-pos 3))
(goto-char pos)
(setq sym nil)))
(defmacro c-bos-save-error-info (missing got)
`(setq saved-pos (vector pos ,missing ,got)))
(defmacro c-bos-report-error ()
'(unless noerror
(setq c-parsing-error
(format-message
"No matching `%s' found for `%s' on line %d"
(elt saved-pos 1)
(elt saved-pos 2)
(1+ (count-lines (point-min)
(c-point 'bol (elt saved-pos 0))))))))
(defun c-beginning-of-statement-1 (&optional lim ignore-labels
noerror comma-delim hit-lim)
"Move to the start of the current statement or declaration, or to
the previous one if already at the beginning of one. Only
statements/declarations on the same level are considered, i.e. don't
move into or out of sexps (not even normal expression parentheses).
If point is already at the earliest statement within braces or parens,
this function doesn't move back into any whitespace preceding it; it
returns `same' in this case.
Stop at statement continuation tokens like \"else\", \"catch\",
\"finally\" and the \"while\" in \"do ... while\" if the start point
is within the continuation. If starting at such a token, move to the
corresponding statement start. If at the beginning of a statement,
move to the closest containing statement if there is any. This might
also stop at a continuation clause.
Labels are treated as part of the following statements if
IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
statement start keyword.) Otherwise, each label is treated as a
separate statement.
Macros are ignored (i.e. skipped over) unless point is within one, in
which case the content of the macro is treated as normal code. Aside
from any normal statement starts found in it, stop at the first token
of the content in the macro, i.e. the expression of an \"#if\" or the
start of the definition in a \"#define\". Also stop at start of
macros before leaving them.
Return:
`label' if stopped at a label or \"case...:\" or \"default:\";
`same' if stopped at the beginning of the current statement;
`up' if stepped to a containing statement;
`previous' if stepped to a preceding statement;
`beginning' if stepped from a statement continuation clause to
its start clause;
`macro' if stepped to a macro start; or
nil if HIT-LIM is non-nil, and we hit the limit.
Note that `same' and not `label' is returned if stopped at the same
label without crossing the colon character.
LIM may be given to limit the search. If the search hits the limit,
point will be left at the closest following token, or at the start
position if that is less. If HIT-LIM is non-nil, nil is returned in
this case, otherwise `same'.
NOERROR turns off error logging to `c-parsing-error'.
Normally only `;' and virtual semicolons are considered to delimit
statements, but if COMMA-DELIM is non-nil then `,' is treated
as a delimiter too.
Note that this function might do hidden buffer changes. See the
comment at the start of cc-engine.el for more info."
;; The bulk of this function is a pushdown automaton that looks at statement
;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
;; purpose is to keep track of nested statements, ensuring that such
;; statements are skipped over in their entirety (somewhat akin to what C-M-p
;; does with nested braces/brackets/parentheses).
;;
;; Note: The position of a boundary is the following token.
;;
;; Beginning with the current token (the one following point), move back one
;; sexp at a time (where a sexp is, more or less, either a token or the
;; entire contents of a brace/bracket/paren pair). Each time a statement
;; boundary is crossed or a "while"-like token is found, update the state of
;; the PDA. Stop at the beginning of a statement when the stack (holding
;; nested statement info) is empty and the position has been moved.
;;
;; The following variables constitute the PDA:
;;
;; sym: This is either the "while"-like token (e.g. 'for) we've just
;; scanned back over, 'boundary if we've just gone back over a
;; statement boundary, or nil otherwise.
;; state: takes one of the values (nil else else-boundary while
;; while-boundary catch catch-boundary).
;; nil means "no "while"-like token yet scanned".
;; 'else, for example, means "just gone back over an else".
;; 'else-boundary means "just gone back over a statement boundary
;; immediately after having gone back over an else".
;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
;; of error reporting information.
;; stack: The stack onto which the PDA pushes its state. Each entry
;; consists of a saved value of state and saved-pos. An entry is
;; pushed when we move back over a "continuation" token (e.g. else)
;; and popped when we encounter the corresponding opening token
;; (e.g. if).
;;
;;
;; The following diagram briefly outlines the PDA.
;;
;; Common state:
;; "else": Push state, goto state `else'.
;; "while": Push state, goto state `while'.
;; "catch" or "finally": Push state, goto state `catch'.
;; boundary: Pop state.
;; other: Do nothing special.
;;
;; State `else':
;; boundary: Goto state `else-boundary'.
;; other: Error, pop state, retry token.
;;
;; State `else-boundary':
;; "if": Pop state.
;; boundary: Error, pop state.
;; other: See common state.
;;
;; State `while':
;; boundary: Save position, goto state `while-boundary'.
;; other: Pop state, retry token.
;;
;; State `while-boundary':
;; "do": Pop state.
;; boundary: Restore position if it's not at start, pop state. [*see below]
;; other: See common state.
;;
;; State `catch':
;; boundary: Goto state `catch-boundary'.
;; other: Error, pop state, retry token.
;;
;; State `catch-boundary':
;; "try": Pop state.
;; "catch": Goto state `catch'.
;; boundary: Error, pop state.
;; other: See common state.
;;
;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
;; searching for a "do" which would have opened a do-while. If we didn't
;; find it, we discard the analysis done since the "while", go back to this
;; token in the buffer and restart the scanning there, this time WITHOUT
;; pushing the 'while state onto the stack.
;;
;; In addition to the above there is some special handling of labels
;; and macros.
(let ((case-fold-search nil)
(start (point))
macro-start
(delims (if comma-delim '(?\; ?,) '(?\;)))
(c-commas-bound-stmts (or c-commas-bound-stmts comma-delim))
c-maybe-labelp after-case:-pos saved
;; Current position.
pos
;; Position of last stmt boundary character (e.g. ;).
boundary-pos
;; Non-nil when a construct has been found which delimits the search
;; for a statement start, e.g. an opening brace or a macro start, or a
;; keyword like `if' when the PDA stack is empty.
pre-stmt-found
;; The position of the last sexp or bound that follows the
;; first found colon, i.e. the start of the nonlabel part of
;; the statement. It's `start' if a colon is found just after
;; the start.
after-labels-pos
;; Like `after-labels-pos', but the first such position inside
;; a label, i.e. the start of the last label before the start
;; of the nonlabel part of the statement.
last-label-pos
;; The last position where a label is possible provided the
;; statement started there. It's nil as long as no invalid
;; label content has been found (according to
;; `c-nonlabel-token-key'). It's `start' if no valid label
;; content was found in the label. Note that we might still
;; regard it a label if it starts with `c-label-kwds'.
label-good-pos
;; Putative positions of the components of a bitfield declaration,
;; e.g. "int foo : NUM_FOO_BITS ;"
bitfield-type-pos bitfield-id-pos bitfield-size-pos
;; Symbol just scanned back over (e.g. 'while or 'boundary).
;; See above.
sym
;; Current state in the automaton. See above.
state
;; Current saved positions. See above.
saved-pos
;; Stack of conses (state . saved-pos).
stack
;; Regexp which matches "for", "if", etc.
(cond-key (or c-opt-block-stmt-key
regexp-unmatchable))
;; Return value.
(ret 'same)
;; Positions of the last three sexps or bounds we've stopped at.
tok ptok pptok)
(save-restriction
(setq lim (if lim
(max lim (point-min))
(point-min)))
(widen)
(if (save-excursion
(and (c-beginning-of-macro)
(/= (point) start)))
(setq macro-start (point)))
;; Try to skip back over unary operator characters, to register
;; that we've moved.
(while (progn
(setq pos (point))
(c-backward-syntactic-ws lim)
;; Protect post-++/-- operators just before a virtual semicolon.
(and (not (c-at-vsemi-p))
(/= (skip-chars-backward "-+!*&~@`#") 0))))
;; Skip back over any semicolon here. If it was a bare semicolon, we're
;; done. Later on we ignore the boundaries for statements that don't
;; contain any sexp. The only thing that is affected is that the error
;; checking is a little less strict, and we really don't bother.
(if (and (memq (char-before) delims)
(progn (forward-char -1)
(setq saved (point))
(c-backward-syntactic-ws lim)
(or (memq (char-before) delims)
(memq (char-before) '(?: nil))
(eq (char-syntax (char-before)) ?\()
(c-at-vsemi-p))))
(setq ret 'previous
pos saved)
;; Begin at start and not pos to detect macros if we stand
;; directly after the #.
(goto-char start)
(if (looking-at "\\<\\|\\W")
;; Record this as the first token if not starting inside it.
(setq tok start))
;; The following while loop goes back one sexp (balanced parens,
;; etc. with contents, or symbol or suchlike) each iteration. This
;; movement is accomplished with a call to c-backward-sexp approx 170
;; lines below.
;;
;; The loop is exited only by throwing nil to the (catch 'loop ...):
;; 1. On reaching the start of a macro;
;; 2. On having passed a stmt boundary with the PDA stack empty;
;; 3. Going backwards past the search limit.
;; 4. On reaching the start of an Objective C method def;
;; 5. From macro `c-bos-pop-state'; when the stack is empty;
;; 6. From macro `c-bos-pop-state-and-retry' when the stack is empty.
(while
(catch 'loop ;; Throw nil to break, non-nil to continue.
(cond
;; Are we in a macro, just after the opening #?
((save-excursion
(and macro-start ; Always NIL for AWK.
(progn (skip-chars-backward " \t")
(eq (char-before) ?#))
(progn (setq saved (1- (point)))
(beginning-of-line)
(not (eq (char-before (1- (point))) ?\\)))
(looking-at c-opt-cpp-start)
(progn (skip-chars-forward " \t")
(eq (point) saved))))
(goto-char saved)
(if (and (c-forward-to-cpp-define-body)
(progn (c-forward-syntactic-ws start)
(< (point) start)))
;; Stop at the first token in the content of the macro.
(setq pos (point)
ignore-labels t) ; Avoid the label check on exit.
(setq pos saved
ret 'macro
ignore-labels t))
(setq pre-stmt-found t)
(throw 'loop nil)) ; 1. Start of macro.
;; Do a round through the automaton if we've just passed a
;; statement boundary or passed a "while"-like token.
((or sym
(and (looking-at cond-key)
(setq sym (intern (match-string 1)))))
(when (and (< pos start) (null stack))
(setq pre-stmt-found t)
(throw 'loop nil)) ; 2. Statement boundary.
;; The PDA state handling.
;;
;; Refer to the description of the PDA in the opening
;; comments. In the following OR form, the first leaf
;; attempts to handles one of the specific actions detailed
;; (e.g., finding token "if" whilst in state `else-boundary').
;; We drop through to the second leaf (which handles common
;; state) if no specific handler is found in the first cond.
;; If a parsing error is detected (e.g. an "else" with no
;; preceding "if"), we throw to the enclosing catch.
;;
;; Note that the (eq state 'else) means
;; "we've just passed an else", NOT "we're looking for an
;; else".
(or (cond
((eq state 'else)
(if (eq sym 'boundary)
(setq state 'else-boundary)
(c-bos-report-error)
(c-bos-pop-state-and-retry)))
((eq state 'else-boundary)
(cond ((eq sym 'if)
(c-bos-pop-state (setq ret 'beginning)))
((eq sym 'boundary)
(c-bos-report-error)
(c-bos-pop-state))))
((eq state 'while)
(if (and (eq sym 'boundary)
;; Since this can cause backtracking we do a
;; little more careful analysis to avoid it:
;; If there's a label in front of the while
;; it can't be part of a do-while.
(not after-labels-pos))
(progn (c-bos-save-pos)
(setq state 'while-boundary))
(c-bos-pop-state-and-retry))) ; Can't be a do-while
((eq state 'while-boundary)
(cond ((eq sym 'do)
(c-bos-pop-state (setq ret 'beginning)))
((eq sym 'boundary) ; isn't a do-while
(c-bos-restore-pos) ; the position of the while
(c-bos-pop-state)))) ; no longer searching for do.
((eq state 'catch)
(if (eq sym 'boundary)
(setq state 'catch-boundary)
(c-bos-report-error)
(c-bos-pop-state-and-retry)))
((eq state 'catch-boundary)
(cond
((eq sym 'try)
(c-bos-pop-state (setq ret 'beginning)))
((eq sym 'catch)
(setq state 'catch))
((eq sym 'boundary)
(c-bos-report-error)
(c-bos-pop-state)))))
;; This is state common. We get here when the previous
;; cond statement found no particular state handler.
(cond ((eq sym 'boundary)
;; If we have a boundary at the start
;; position we push a frame to go to the
;; previous statement.
(if (>= pos start)
(c-bos-push-state)
(c-bos-pop-state)))
((eq sym 'else)
(c-bos-push-state)
(c-bos-save-error-info 'if 'else)
(setq state 'else))
((eq sym 'while)
;; Is this a real while, or a do-while?
;; The next `when' triggers unless we are SURE that
;; the `while' is not the tail end of a `do-while'.
(when (or (not pptok)
(memq (char-after pptok) delims)
;; The following kludge is to prevent
;; infinite recursion when called from
;; c-awk-after-if-for-while-condition-p,
;; or the like.
(and (eq (point) start)
(c-vsemi-status-unknown-p))
(c-at-vsemi-p pptok))
;; Since this can cause backtracking we do a
;; little more careful analysis to avoid it: If
;; the while isn't followed by a (possibly
;; virtual) semicolon it can't be a do-while.
(c-bos-push-state)
(setq state 'while)))
((memq sym '(catch finally))
(c-bos-push-state)
(c-bos-save-error-info 'try sym)
(setq state 'catch))))
(when c-maybe-labelp
;; We're either past a statement boundary or at the
;; start of a statement, so throw away any label data
;; for the previous one.
(setq after-labels-pos nil
last-label-pos nil
c-maybe-labelp nil))))
;; Step to the previous sexp, but not if we crossed a
;; boundary, since that doesn't consume an sexp.
(if (eq sym 'boundary)
(when (>= (point) lim)
(setq ret 'previous))
;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
;; BACKWARDS THROUGH THE SOURCE.
(c-backward-syntactic-ws lim)
(let ((before-sws-pos (point))
;; The end position of the area to search for statement
;; barriers in this round.
(maybe-after-boundary-pos pos)
comma-delimited)
;; Go back over exactly one logical sexp, taking proper
;; account of macros and escaped EOLs.
(while
(progn
(setq comma-delimited (and (not comma-delim)
(eq (char-before) ?\,)))
(unless (c-safe (c-backward-sexp) t)
;; Give up if we hit an unbalanced block. Since the
;; stack won't be empty the code below will report a
;; suitable error.
(setq pre-stmt-found t)
(throw 'loop nil))
(cond
;; Have we moved into a macro?
((and (not macro-start)
(c-beginning-of-macro))
(save-excursion
(c-backward-syntactic-ws lim)
(setq before-sws-pos (point)))
;; Have we crossed a statement boundary? If not,
;; keep going back until we find one or a "real" sexp.
(and
(save-excursion
(c-end-of-macro)
(not (c-crosses-statement-barrier-p
(point) maybe-after-boundary-pos)))
(setq maybe-after-boundary-pos (point))))
;; Have we just gone back over an escaped NL? This
;; doesn't count as a sexp.
((looking-at "\\\\$")))))
;; Have we crossed a statement boundary?
(setq boundary-pos
(cond
;; Are we at a macro beginning?
((and (not macro-start)
c-opt-cpp-prefix
(looking-at c-opt-cpp-prefix))
(save-excursion
(c-end-of-macro)
(c-crosses-statement-barrier-p
(point) maybe-after-boundary-pos)))
;; Just gone back over a brace block?
((and
(eq (char-after) ?{)
(not comma-delimited)
(not (c-looking-at-inexpr-block lim nil t))
(save-excursion
(c-backward-token-2 1 t nil)
(not (looking-at "=\\([^=]\\|$\\)")))
(or
(not c-opt-block-decls-with-vars-key)
(save-excursion
(c-backward-token-2 1 t nil)
(if (and (looking-at c-symbol-start)
(not (looking-at c-keywords-regexp)))
(c-backward-token-2 1 t nil))
(and
(not (looking-at
c-opt-block-decls-with-vars-key))
(or comma-delim
(not (eq (char-after) ?\,))))))
;; Is the {..} followed by an operator which
;; prevents it being a statement in its own right?
(save-excursion
(and
(c-go-list-forward)
(progn
(c-forward-syntactic-ws)
(or
(not (looking-at c-non-after-{}-ops-re))
(let
((bad-op-len
(- (match-end 0) (match-beginning 0))))
(and
(looking-at c-operator-re)
(> (- (match-end 0) (match-beginning 0))
bad-op-len))))))))
(save-excursion
(c-forward-sexp) (point)))
;; Just gone back over some paren block?
((looking-at "\\s(")
(save-excursion
(goto-char (1+ (c-down-list-backward
before-sws-pos)))
(c-crosses-statement-barrier-p
(point) maybe-after-boundary-pos)))
;; Just gone back over an ordinary symbol of some sort?
(t (c-crosses-statement-barrier-p
(point) maybe-after-boundary-pos))))
(when boundary-pos
(setq pptok ptok
ptok tok
tok boundary-pos
sym 'boundary)
;; Like a C "continue". Analyze the next sexp.
(throw 'loop t))))
;; Have we gone past the limit?
(when (< (point) lim)
(throw 'loop nil)) ; 3. Gone back over the limit.
;; ObjC method def?
(when (and c-opt-method-key
(setq saved (c-in-method-def-p)))
(setq pos saved
pre-stmt-found t
ignore-labels t) ; Avoid the label check on exit.
(throw 'loop nil)) ; 4. ObjC method def.
;; Might we have a bitfield declaration, "<type> <id> : <size>"?
(if c-has-bitfields
(cond
;; The : <size> and <id> fields?
((and (numberp c-maybe-labelp)
(not bitfield-size-pos)
(save-excursion
(goto-char (or tok start))
(not (looking-at c-keywords-regexp)))
(not (looking-at c-keywords-regexp))
(not (c-punctuation-in (point) c-maybe-labelp)))
(setq bitfield-size-pos (or tok start)
bitfield-id-pos (point)))
;; The <type> field?
((and bitfield-id-pos
(not bitfield-type-pos))
(if (and (looking-at c-symbol-key) ; Can only be an integer type. :-)
(not (looking-at c-not-primitive-type-keywords-regexp))
(not (c-punctuation-in (point) tok)))
(setq bitfield-type-pos (point))
(setq bitfield-size-pos nil
bitfield-id-pos nil)))))
;; Handle labels.
(unless (eq ignore-labels t)
(when (numberp c-maybe-labelp)
;; `c-crosses-statement-barrier-p' has found a colon, so we
;; might be in a label now. Have we got a real label
;; (including a case label) or something like C++'s "public:"?
;; A case label might use an expression rather than a token.
(setq after-case:-pos (or tok start))
(if (or (looking-at c-nonlabel-nonparen-token-key)
; e.g. "while" or "'a'"
;; Catch C++'s inheritance construct "class foo : bar".
(save-excursion
(and
(c-safe (c-backward-sexp) t)
(looking-at c-nonlabel-token-2-key)))
;; Catch C++'s "case a(1):"
(and (c-major-mode-is 'c++-mode)
(eq (char-after) ?\()
(save-excursion
(not (and
(zerop (c-backward-token-2 2))
(looking-at c-case-kwds-regexp))))))
(setq c-maybe-labelp nil)
(if after-labels-pos ; Have we already encountered a label?
(if (not last-label-pos)
(setq last-label-pos (or tok start)))
(setq after-labels-pos (or tok start)))
(setq c-maybe-labelp t
label-good-pos nil))) ; bogus "label"
(when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
; been found.
(looking-at c-nonlabel-token-key)) ; e.g. "while :"
;; We're in a potential label and it's the first
;; time we've found something that isn't allowed in
;; one.
(setq label-good-pos (or tok start))))
;; We've moved back by a sexp, so update the token positions.
(setq sym nil
pptok ptok
ptok tok
tok (point)
pos tok) ; always non-nil
) ; end of (catch 'loop ....)
) ; end of sexp-at-a-time (while ....)
(when (and hit-lim
(or (not pre-stmt-found)
(< pos lim)
(>= pos start)))
(setq ret nil))
;; If the stack isn't empty there might be errors to report.
(while stack
(if (and (vectorp saved-pos) (eq (length saved-pos) 3))
(c-bos-report-error))
(setq saved-pos (cdr (car stack))
stack (cdr stack)))
(when (and (eq ret 'same)
(not (memq sym '(boundary ignore nil))))
;; Need to investigate closer whether we've crossed
;; between a substatement and its containing statement.
(if (setq saved
(cond ((and (looking-at c-block-stmt-1-2-key)
(eq (char-after ptok) ?\())
pptok)
((looking-at c-block-stmt-1-key)
ptok)
(t pptok)))
(cond ((> start saved) (setq pos saved))
((= start saved) (setq ret 'up)))))
(when (and (not ignore-labels)
(eq c-maybe-labelp t)
(not (eq ret 'beginning))
after-labels-pos
(not bitfield-type-pos) ; Bitfields take precedence over labels.
(or (not label-good-pos)
(<= label-good-pos pos)
(progn
(goto-char (if (and last-label-pos
(< last-label-pos start))
last-label-pos
pos))
(looking-at c-label-kwds-regexp))))
;; We're in a label. Maybe we should step to the statement
;; after it.
(if (< after-labels-pos start)
(setq pos after-labels-pos)
(setq ret 'label)
(if (and last-label-pos (< last-label-pos start))
;; Might have jumped over several labels. Go to the last one.
(setq pos last-label-pos)))))
;; Have we got "case <expression>:"?
(goto-char pos)
(when (and after-case:-pos
(not (eq ret 'beginning))
(looking-at c-case-kwds-regexp))
(if (< after-case:-pos start)
(setq pos after-case:-pos))
(if (eq ret 'same)
(setq ret 'label)))
;; Skip over the unary operators that can start the statement.
(while (progn
(c-backward-syntactic-ws lim)
;; protect AWK post-inc/decrement operators, etc.
(and (not (c-at-vsemi-p (point)))
(/= (skip-chars-backward "-.+!*&~@`#") 0)))
(setq pos (point)))
(goto-char pos)
ret)))
(defun c-punctuation-in (from to)
"Return non-nil if there is a non-comment non-macro punctuation character
between FROM and TO. FROM must not be in a string or comment. The returned
value is the position of the first such character."
(save-excursion
(goto-char from)
(let ((pos (point)))
(while (progn (skip-chars-forward c-symbol-chars to)
(c-forward-syntactic-ws to)
(> (point) pos))
(setq pos (point))))
(and (< (point) to) (point))))
(defun c-crosses-statement-barrier-p (from to)
"Return non-nil if buffer positions FROM to TO cross one or more
statement or declaration boundaries. The returned value is actually
the position of the earliest boundary char. FROM must not be within
a string or comment.
The variable `c-maybe-labelp' is set to the position of the first `:' that
might start a label (i.e. not part of `::' and not preceded by `?'). If a
single `?' is found, then `c-maybe-labelp' is cleared.
For AWK, a statement which is terminated by an EOL (not a ; or a }) is
regarded as having a \"virtual semicolon\" immediately after the last token on
the line. If this virtual semicolon is _at_ from, the function recognizes it.
Note that this function might do hidden buffer changes. See the
comment at the start of cc-engine.el for more info."
(let* ((skip-chars (if c-commas-bound-stmts
c-stmt-boundary-skip-chars-with-comma
c-stmt-boundary-skip-chars)) ; e.g. "^#;{}?:"
(non-skip-list (if c-commas-bound-stmts
c-stmt-boundary-skip-list-with-comma
c-stmt-boundary-skip-list)) ; e.g. (?# ?\; ?{ ?} ?? ?:)
lit-range lit-start vsemi-pos attr-end)
(save-restriction
(widen)
(save-excursion
(catch 'done
(goto-char from)
(while (progn (skip-chars-forward
skip-chars
(min to (c-point 'bonl)))
(< (point) to))
(cond
;; Virtual semicolon?
((and (bolp)
(save-excursion
(progn
(if (setq lit-start (c-literal-start from)) ; Have we landed in a string/comment?
(goto-char lit-start))
(c-backward-syntactic-ws (c-point 'bopl))
(setq vsemi-pos (point))
(c-at-vsemi-p))))
(throw 'done vsemi-pos))
;; Optimize for large blocks of comments.
((progn (c-forward-syntactic-ws to)
(>= (point) to))
(throw 'done nil))
;; In a string/comment?
((setq lit-range (c-literal-limits from))
(goto-char (cdr lit-range)))
;; Skip over a C++ attribute?
((eq (char-after) ?\[)
(if (setq attr-end (c-looking-at-c++-attribute))
(goto-char attr-end)
(forward-char)))
((eq (char-after) ?:)
(forward-char)
(if (and (eq (char-after) ?:)
(< (point) to))
;; Ignore scope operators.
(forward-char)
(setq c-maybe-labelp (1- (point)))))
((eq (char-after) ??)
;; A question mark. Can't be a label, so stop
;; looking for more : and ?.
(setq c-maybe-labelp nil
skip-chars
(substring (if c-commas-bound-stmts
c-stmt-delim-chars-with-comma
c-stmt-delim-chars)
0 -2)))
;; At a CPP construct or a "#" or "##" operator?
((and c-opt-cpp-symbol (looking-at c-opt-cpp-symbol))
(if (save-excursion
(skip-chars-backward " \t")
(and (bolp)
(or (bobp)
(not (eq (char-before (1- (point))) ?\\)))))
(c-end-of-macro)
(skip-chars-forward c-opt-cpp-symbol)))
((memq (char-after) non-skip-list)
(throw 'done (point)))))
;; In trailing space after an as yet undetected virtual semicolon?
(c-backward-syntactic-ws from)
(when (and (bolp) (not (bobp))) ; Can happen in AWK Mode with an
; unterminated string/regexp.
(backward-char))
(if (and (< (point) to)
(c-at-vsemi-p))
(point)
nil))))))
(defun c-at-statement-start-p ()
"Return non-nil if the point is at the first token in a statement
or somewhere in the syntactic whitespace before it.
A \"statement\" here is not restricted to those inside code blocks.
Any kind of declaration-like construct that occur outside function
bodies is also considered a \"statement\".
Note that this function might do hidden buffer changes. See the
comment at the start of cc-engine.el for more info."
(save-excursion
(let ((end (point))
c-maybe-labelp)
(c-syntactic-skip-backward
(substring
(if c-commas-bound-stmts
c-stmt-delim-chars-with-comma
c-stmt-delim-chars)
1)
nil t)
(or (bobp)
(eq (char-before) ?})
(and (eq (char-before) ?{)
(not (and c-special-brace-lists
(progn (backward-char)
(c-looking-at-special-brace-list)))))
(c-crosses-statement-barrier-p (point) end)))))
(defun c-at-expression-start-p ()
"Return non-nil if the point is at the first token in an expression or
statement, or somewhere in the syntactic whitespace before it.
An \"expression\" here is a bit different from the normal language
grammar sense: It's any sequence of expression tokens except commas,
unless they are enclosed inside parentheses of some kind. Also, an
expression never continues past an enclosing parenthesis, but it might
contain parenthesis pairs of any sort except braces.
Since expressions never cross statement boundaries, this function also
recognizes statement beginnings, just like `c-at-statement-start-p'.
Note that this function might do hidden buffer changes. See the
comment at the start of cc-engine.el for more info."
(save-excursion
(let ((end (point))
(c-commas-bound-stmts t)
c-maybe-labelp)
(c-syntactic-skip-backward (substring c-stmt-delim-chars-with-comma 1)
nil t)
(or (bobp)
(memq (char-before) '(?{ ?}))
(save-excursion (backward-char)
(looking-at "\\s("))
(c-crosses-statement-barrier-p (point) end)))))
(make-obsolete 'c-at-expression-start-p nil "CC mode 5.35")
\f
;; A set of functions that covers various idiosyncrasies in
;; implementations of `forward-comment'.
;; Note: Some emacsen considers incorrectly that any line comment
;; ending with a backslash continues to the next line. I can't think
;; of any way to work around that in a reliable way without changing
;; the buffer, though. Suggestions welcome. ;) (No, temporarily
;; changing the syntax for backslash doesn't work since we must treat
;; escapes in string literals correctly.)
(defun c-forward-single-comment ()
"Move forward past whitespace and the closest following comment, if any.
Return t if a comment was found, nil otherwise. In either case, the
point is moved past the following whitespace. Line continuations,
i.e. a backslashes followed by line breaks, are treated as whitespace.
The line breaks that end line comments are considered to be the
comment enders, so the point will be put on the beginning of the next
line if it moved past a line comment.
This function does not do any hidden buffer changes."
(let ((start (point)))
(when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
(goto-char (match-end 0)))
(when (forward-comment 1)
(if (eobp)
;; Some emacsen (e.g. XEmacs 21) return t when moving
;; forwards at eob.
nil
;; Emacs includes the ending newline in a b-style (c++)
;; comment, but XEmacs doesn't. We depend on the Emacs
;; behavior (which also is symmetric).
(if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
(condition-case nil (forward-char 1)))
t))))
(defsubst c-forward-comments ()
"Move forward past all following whitespace and comments.
Line continuations, i.e. a backslashes followed by line breaks, are
treated as whitespace.
Note that this function might do hidden buffer changes. See the
comment at the start of cc-engine.el for more info."
(while (or
;; If forward-comment in at least XEmacs 21 is given a large
;; positive value, it'll loop all the way through if it hits
;; eob.
(and (forward-comment 5)
;; Some emacsen (e.g. XEmacs 21) return t when moving
;; forwards at eob.
(not (eobp)))
(when (looking-at "\\\\[\n\r]")
(forward-char 2)
t))))
(defmacro c-forward-comment-minus-1 ()
"Call (forward-comment -1), taking care of escaped newlines.
Return the result of `forward-comment' if it gets called, nil otherwise."
`(if (not comment-end-can-be-escaped)
(forward-comment -1)
(when (and (< (skip-syntax-backward " >") 0)
(eq (char-after) ?\n))
(forward-char))
(cond
((and (eq (char-before) ?\n)
(eq (char-before (1- (point))) ?\\))
(backward-char)
nil)
(t (forward-comment -1)))))
(defun c-backward-single-comment ()
"Move backward past whitespace and the closest preceding comment, if any.
Return t if a comment was found, nil otherwise. In either case, the
point is moved past the preceding whitespace. Line continuations,
i.e. a backslashes followed by line breaks, are treated as whitespace.
The line breaks that end line comments are considered to be the
comment enders, so the point cannot be at the end of the same line to
move over a line comment.
This function does not do any hidden buffer changes."
(let ((start (point)))
;; When we got newline terminated comments, forward-comment in all
;; supported emacsen so far will stop at eol of each line not
;; ending with a comment when moving backwards. This corrects for
;; that, and at the same time handles line continuations.
(while (progn
(skip-chars-backward " \t\n\r\f\v")
(and (looking-at "[\n\r]")
(eq (char-before) ?\\)))
(backward-char))
(if (bobp)
;; Some emacsen (e.g. Emacs 19.34) return t when moving
;; backwards at bob.
nil
;; Leave point after the closest following newline if we've
;; backed up over any above, since forward-comment won't move
;; backward over a line comment if point is at the end of the
;; same line.
(re-search-forward "\\=\\s *[\n\r]" start t)
(if (if (c-forward-comment-minus-1)
(if (eolp)
;; If forward-comment above succeeded and we're at eol
;; then the newline we moved over above didn't end a
;; line comment, so we give it another go.
(c-forward-comment-minus-1)
t))
;; Emacs <= 20 and XEmacs move back over the closer of a
;; block comment that lacks an opener.
(if (looking-at "\\*/")
(progn (forward-char 2) nil)
t)))))
(defsubst c-backward-comments ()
"Move backward past all preceding whitespace and comments.
Line continuations, i.e. a backslashes followed by line breaks, are
treated as whitespace. The line breaks that end line comments are
considered to be the comment enders, so the point cannot be at the end
of the same line to move over a line comment. Unlike
c-backward-syntactic-ws, this function doesn't move back over
preprocessor directives.
Note that this function might do hidden buffer changes. See the
comment at the start of cc-engine.el for more info."
(let ((start (point)))
(while (and
;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
;; return t when moving backwards at bob.
(not (bobp))
(if (let (moved-comment)
(while
(and (not (setq moved-comment (c-forward-comment-minus-1)))
;; Cope specifically with ^M^J here -
;; forward-comment sometimes gets stuck after ^Ms,
;; sometimes after ^M^J.
(or
(when (eq (char-before) ?\r)
(backward-char)
t)
(when (and (eq (char-before) ?\n)
(eq (char-before (1- (point))) ?\r))
(backward-char 2)
t))))
moved-comment)
(if (looking-at "\\*/")
;; Emacs <= 20 and XEmacs move back over the
;; closer of a block comment that lacks an opener.
(progn (forward-char 2) nil)
t)
;; XEmacs treats line continuations as whitespace but
;; only in the backward direction, which seems a bit
;; odd. Anyway, this is necessary for Emacs.
(when (and (looking-at "[\n\r]")
(eq (char-before) ?\\)
(< (point) start))
(backward-char)
t))))))
\f
;; Tools for skipping over syntactic whitespace.
;; The following functions use text properties to cache searches over
;; large regions of syntactic whitespace. It works as follows:
;;
;; o If a syntactic whitespace region contains anything but simple
;; whitespace (i.e. space, tab and line breaks), the text property
;; `c-in-sws' is put over it. At places where we have stopped
;; within that region there's also a `c-is-sws' text property.
;; That since there typically are nested whitespace inside that
;; must be handled separately, e.g. whitespace inside a comment or
;; cpp directive. Thus, from one point with `c-is-sws' it's safe
;; to jump to another point with that property within the same
;; `c-in-sws' region. It can be likened to a ladder where
;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
;;
;; o The `c-is-sws' property is put on the simple whitespace chars at
;; a "rung position" and also maybe on the first following char.
;; As many characters as can be conveniently found in this range
;; are marked, but no assumption can be made that the whole range
;; is marked (it could be clobbered by later changes, for
;; instance).
;;
;; Note that some part of the beginning of a sequence of simple
;; whitespace might be part of the end of a preceding line comment
;; or cpp directive and must not be considered part of the "rung".
;; Such whitespace is some amount of horizontal whitespace followed
;; by a newline. In the case of cpp directives it could also be
;; two newlines with horizontal whitespace between them.
;;
;; The reason to include the first following char is to cope with
;; "rung positions" that don't have any ordinary whitespace. If
;; `c-is-sws' is put on a token character it does not have
;; `c-in-sws' set simultaneously. That's the only case when that
;; can occur, and the reason for not extending the `c-in-sws'
;; region to cover it is that the `c-in-sws' region could then be
;; accidentally merged with a following one if the token is only
;; one character long.
;;
;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
;; removed in the changed region. If the change was inside
;; syntactic whitespace that means that the "ladder" is broken, but
;; a later call to `c-forward-sws' or `c-backward-sws' will use the
;; parts on either side and use an ordinary search only to "repair"
;; the gap.
;;
;; Special care needs to be taken if a region is removed: If there
;; are `c-in-sws' on both sides of it which do not connect inside
;; the region then they can't be joined. If e.g. a marked macro is
;; broken, syntactic whitespace inside the new text might be
;; marked. If those marks would become connected with the old
;; `c-in-sws' range around the macro then we could get a ladder
;; with one end outside the macro and the other at some whitespace
;; within it.
;;
;; The main motivation for this system is to increase the speed in
;; skipping over the large whitespace regions that can occur at the
;; top level in e.g. header files that contain a lot of comments and
;; cpp directives. For small comments inside code it's probably
;; slower than using `forward-comment' straightforwardly, but speed is
;; not a significant factor there anyway.
; (defface c-debug-is-sws-face
; '((t (:background "GreenYellow")))
; "Debug face to mark the `c-is-sws' property.")
; (defface c-debug-in-sws-face
; '((t (:underline t)))
; "Debug face to mark the `c-in-sws' property.")
; (defun c-debug-put-sws-faces ()
; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
; ;; properties in the buffer.
; (interactive)
; (save-excursion
; (c-save-buffer-state (in-face)
; (goto-char (point-min))
; (setq in-face (if (get-text-property (point) 'c-is-sws)
; (point)))
; (while (progn
; (goto-char (next-single-property-change
; (point) 'c-is-sws nil (point-max)))
; (if in-face
; (progn
; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
; (setq in-face nil))
; (setq in-face (point)))
; (not (eobp))))
; (goto-char (point-min))
; (setq in-face (if (get-text-property (point) 'c-in-sws)
; (point)))
; (while (progn
; (goto-char (next-single-property-change
; (point) 'c-in-sws nil (point-max)))
; (if in-face
; (progn
; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
; (setq in-face nil))
; (setq in-face (point)))
; (not (eobp)))))))
(defmacro c-debug-sws-msg (&rest args)
(ignore args)
;;`(message ,@args)
)
(defmacro c-put-is-sws (beg end)
;; This macro does a hidden buffer change.
`(let ((beg ,beg) (end ,end))
(put-text-property beg end 'c-is-sws t)
,@(when (facep 'c-debug-is-sws-face)
'((c-debug-add-face beg end 'c-debug-is-sws-face)))))
(def-edebug-spec c-put-is-sws t)
(defmacro c-put-in-sws (beg end)
;; This macro does a hidden buffer change.
`(let ((beg ,beg) (end ,end))
(put-text-property beg end 'c-in-sws t)
,@(when (facep 'c-debug-is-sws-face)
'((c-debug-add-face beg end 'c-debug-in-sws-face)))))
(def-edebug-spec c-put-in-sws t)
(defmacro c-remove-is-sws (beg end)
;; This macro does a hidden buffer change.
`(let ((beg ,beg) (end ,end))
(remove-text-properties beg end '(c-is-sws nil))
,@(when (facep 'c-debug-is-sws-face)
'((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
(def-edebug-spec c-remove-is-sws t)
(defmacro c-remove-in-sws (beg end)
;; This macro does a hidden buffer change.
`(let ((beg ,beg) (end ,end))
(remove-text-properties beg end '(c-in-sws nil))
,@(when (facep 'c-debug-is-sws-face)
'((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
(def-edebug-spec c-remove-in-sws t)
(defmacro c-remove-is-and-in-sws (beg end)
;; This macro does a hidden buffer change.
`(let ((beg ,beg) (end ,end))
(remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
,@(when (facep 'c-debug-is-sws-face)
'((c-debug-remove-face beg end 'c-debug-is-sws-face)
(c-debug-remove-face beg end 'c-debug-in-sws-face)))))
(def-edebug-spec c-remove-is-and-in-sws t)
;; The type of literal position `end' is in a `before-change-functions'
;; function - one of `c', `c++', `pound', `noise', `attribute' or nil (but NOT
;; `string').
(defvar c-sws-lit-type nil)
;; A cons (START . STOP) of the bounds of the comment or CPP construct, etc.,
;; enclosing END, if any, else nil.
(defvar c-sws-lit-limits nil)
(defun c-enclosing-c++-attribute ()
;; If we're in C++ Mode, and point is within a correctly balanced [[ ... ]]
;; attribute structure, return a cons of its starting and ending positions.
;; Otherwise, return nil.
(and
(c-major-mode-is 'c++-mode)
(save-excursion
(let ((lim (max (- (point) 200) (point-min)))
cand)
(while
(and
(progn
(skip-chars-backward "^[;{}" lim)
(eq (char-before) ?\[))
(not (eq (char-before (1- (point))) ?\[))
(> (point) lim))
(backward-char))
(and (eq (char-before) ?\[)
(eq (char-before (1- (point))) ?\[)
(progn (backward-char 2) t)
(setq cand (point))
(c-go-list-forward nil (min (+ (point) 200) (point-max)))
(eq (char-before) ?\])
(eq (char-before (1- (point))) ?\])
(not (c-literal-limits))
(cons cand (point)))))))
(defun c-invalidate-sws-region-before (beg end)
;; Called from c-before-change. BEG and END are the bounds of the change
;; region, the standard parameters given to all before-change-functions.
;;
;; Note whether END is inside a comment, CPP construct, or noise macro, and
;; if so note its bounds in `c-sws-lit-limits' and type in `c-sws-lit-type'.
(setq c-sws-lit-type nil
c-sws-lit-limits nil)
(save-match-data
(save-excursion
(goto-char end)
(let* ((limits (c-literal-limits))
(lit-type (c-literal-type limits)))
(cond
((memq lit-type '(c c++))
(setq c-sws-lit-type lit-type
c-sws-lit-limits limits))
((c-beginning-of-macro)
(setq c-sws-lit-type 'pound
c-sws-lit-limits (cons (point)
(progn (c-end-of-macro) (point)))))
((eq lit-type 'string))
((setq c-sws-lit-limits (c-enclosing-c++-attribute))
(setq c-sws-lit-type 'attribute))
((progn (skip-syntax-backward "w_")
(looking-at c-noise-macro-name-re))
(setq c-sws-lit-type 'noise
c-sws-lit-limits (cons (match-beginning 1) (match-end 1))))
(t))))
(save-excursion
(goto-char beg)
(let ((attr-limits (c-enclosing-c++-attribute)))
(if attr-limits
(if (consp c-sws-lit-limits)
(setcar c-sws-lit-limits (car attr-limits))
(setq c-sws-lit-limits attr-limits))
(skip-syntax-backward "w_")
(when (looking-at c-noise-macro-name-re)
(setq c-sws-lit-type 'noise)
(if (consp c-sws-lit-limits)
(setcar c-sws-lit-limits (match-beginning 1))
(setq c-sws-lit-limits (cons (match-beginning 1)
(match-end 1))))))))))
(defun c-invalidate-sws-region-after-del (beg end _old-len)
;; Text has been deleted, OLD-LEN characters of it starting from position
;; BEG. END is typically eq to BEG. Should there have been a comment or
;; CPP construct open at END before the deletion, check whether this
;; deletion deleted or "damaged" its opening delimiter. If so, return the
;; current position of where the construct ended, otherwise return nil.
(when c-sws-lit-limits
(if (and (< beg (+ (car c-sws-lit-limits) 2)) ; A lazy assumption that
; comment delimiters are 2
; chars long.
(or (get-text-property end 'c-in-sws)
(next-single-property-change end 'c-in-sws nil
(cdr c-sws-lit-limits))
(get-text-property end 'c-is-sws)
(next-single-property-change end 'c-is-sws nil
(cdr c-sws-lit-limits))))
(cdr c-sws-lit-limits))))
(defun c-invalidate-sws-region-after-ins (end)
;; Text has been inserted, ending at buffer position END. Should there be a
;; literal or CPP construct open at END, check whether there are `c-in-sws'
;; or `c-is-sws' text properties inside this literal. If there are, return
;; the buffer position of the end of the literal, else return nil.
(save-excursion
(goto-char end)
(let* ((limits (c-literal-limits))
(lit-type (c-literal-type limits)))
(when (and (not (memq lit-type '(c c++)))
(c-beginning-of-macro))
(setq lit-type 'pound
limits (cons (point)
(progn (c-end-of-macro) (point)))))
(when (memq lit-type '(c c++ pound))
(let ((next-in (next-single-property-change (car limits) 'c-in-sws
nil (cdr limits)))
(next-is (next-single-property-change (car limits) 'c-is-sws
nil (cdr limits))))
(and (or next-in next-is)
(cdr limits)))))))
(defun c-invalidate-sws-region-after (beg end old-len)
;; Called from `after-change-functions'. Remove any stale `c-in-sws' or
;; `c-is-sws' text properties from the vicinity of the change. BEG, END,
;; and OLD-LEN are the standard arguments given to after-change functions.
;;
;; Note that if `c-forward-sws' or `c-backward-sws' are used outside
;; `c-save-buffer-state' or similar then this will remove the cache
;; properties right after they're added.
;;
;; This function does hidden buffer changes.
(when c-sws-lit-limits
(setcar c-sws-lit-limits (min beg (car c-sws-lit-limits)))
(setcdr c-sws-lit-limits
(max end (- (+ (cdr c-sws-lit-limits) (- end beg)) old-len))))
(let ((del-end
(and (> old-len 0)
(c-invalidate-sws-region-after-del beg end old-len)))
(ins-end
(and (> end beg)
(c-invalidate-sws-region-after-ins end))))
(save-excursion
;; Adjust the end to remove the properties in any following simple
;; ws up to and including the next line break, if there is any
;; after the changed region. This is necessary e.g. when a rung
;; marked empty line is converted to a line comment by inserting
;; "//" before the line break. In that case the line break would
;; keep the rung mark which could make a later `c-backward-sws'
;; move into the line comment instead of over it.
(goto-char end)
(skip-chars-forward " \t\f\v")
(when (and (eolp) (not (eobp)))
(setq end (1+ (point)))))
(when (memq c-sws-lit-type '(noise attribute))
(setq beg (car c-sws-lit-limits)
end (cdr c-sws-lit-limits))) ; This last setting may be redundant.
(when (and (= beg end)
(get-text-property beg 'c-in-sws)
(> beg (point-min))
(get-text-property (1- beg) 'c-in-sws))
;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
;; safe to keep a range that was continuous before the change. E.g:
;;
;; #define foo
;; \
;; bar
;;
;; There can be a "ladder" between "#" and "b". Now, if the newline
;; after "foo" is removed then "bar" will become part of the cpp
;; directive instead of a syntactically relevant token. In that
;; case there's no longer syntactic ws from "#" to "b".
(setq beg (1- beg)))
(setq end (max (or del-end end)
(or ins-end end)
(or (cdr c-sws-lit-limits) end)
end))
(c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
(c-remove-is-and-in-sws beg end)))
(defun c-forward-sws ()
;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
;;
;; This function might do hidden buffer changes.
(let (;; `rung-pos' is set to a position as early as possible in the
;; unmarked part of the simple ws region.
(rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
rung-is-marked next-rung-is-marked simple-ws-end macro-start macro-end
;; `safe-start' is set when it's safe to cache the start position.
;; This is the case except when we have an unterminated block comment
;; within a macro.
safe-start)
;; Skip simple ws and do a quick check on the following character to see
;; if it's anything that can't start syntactic ws, so we can bail out
;; early in the majority of cases when there just are a few ws chars.
(skip-chars-forward " \t\n\r\f\v")
(when (or (looking-at c-syntactic-ws-start)
(and c-opt-cpp-prefix
(looking-at c-noise-macro-name-re))
(and (c-major-mode-is 'c++-mode)
(looking-at "\\[\\["))
(looking-at c-doc-line-join-re))
(setq rung-end-pos (min (1+ (point)) (point-max)))
(if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
'c-is-sws t))
;; Find the last rung position to avoid setting properties in all
;; the cases when the marked rung is complete.
;; (`next-single-property-change' is certain to move at least one
;; step forward.)
(setq rung-pos (1- (c-next-single-property-change
rung-is-marked 'c-is-sws nil rung-end-pos)))
;; Got no marked rung here. Since the simple ws might have started
;; inside a line comment or cpp directive we must set `rung-pos' as
;; high as possible.
(setq rung-pos (point)))
(with-silent-modifications
(while
(progn
;; In the following while form, we move over a "ladder" and
;; following simple WS each time round the loop, appending the WS
;; onto the ladder, joining adjacent ladders, and terminating when
;; there is no more WS or we reach EOB.
(while
(when (and rung-is-marked
(get-text-property (point) 'c-in-sws))
;; The following search is the main reason that `c-in-sws'
;; and `c-is-sws' aren't combined to one property.
(goto-char (c-next-single-property-change
(point) 'c-in-sws nil (point-max)))
(unless (get-text-property (point) 'c-is-sws)
;; If the `c-in-sws' region extended past the last
;; `c-is-sws' char we have to go back a bit.
(or (get-text-property (1- (point)) 'c-is-sws)
(goto-char (previous-single-property-change
(point) 'c-is-sws)))
(backward-char))
(c-debug-sws-msg
"c-forward-sws cached move %s -> %s (max %s)"
rung-pos (point) (point-max))
(setq rung-pos (point))
(and (> (skip-chars-forward " \t\n\r\f\v") 0)
(not (eobp))))
;; We'll loop here if there is simple ws after the last rung.
;; That means that there's been some change in it and it's
;; possible that we've stepped into another ladder, so extend
;; the previous one to join with it if there is one, and try to
;; use the cache again.
(c-debug-sws-msg
"c-forward-sws extending rung with [%s..%s] (max %s)"
(1+ rung-pos) (1+ (point)) (point-max))
(unless (get-text-property (point) 'c-is-sws)
;; Remove any `c-in-sws' property from the last char of
;; the rung before we mark it with `c-is-sws', so that we
;; won't connect with the remains of a broken "ladder".
(c-remove-in-sws (point) (1+ (point))))
(c-put-is-sws (1+ rung-pos)
(1+ (point)))
(c-put-in-sws rung-pos
(setq rung-pos (point)
last-put-in-sws-pos rung-pos)))
;; Now move over any comments (x)or a CPP construct.
(setq simple-ws-end (point))
(setq safe-start t)
;; Take elaborate precautions to detect an open block comment at
;; the end of a macro. If we find one, we set `safe-start' to nil
;; and break off any further scanning of comments.
;;
;; (2019-05-02): `c-end-of-macro' now moves completely over block
;; comments, even multiline ones lacking \s at their EOLs. So a
;; lot of the following is probably redundant now.
(let ((com-begin (point)) com-end in-macro)
(when (and (c-forward-single-comment)
(setq com-end (point))
(save-excursion
(goto-char com-begin)
(c-beginning-of-macro)))
(setq in-macro t)
(goto-char com-begin)
(if (progn (c-end-of-macro com-end)
(< (point) com-end))
(setq safe-start nil)))
(if in-macro
(while (and safe-start
com-end (> com-end com-begin)
(setq com-begin (point))
(when (and (c-forward-single-comment)
(setq com-end (point)))
(goto-char com-begin)
(if (progn (c-end-of-macro com-end)
(< (point) com-end))
(setq safe-start nil))
safe-start)))
(c-forward-comments)))
(cond
((/= (point) simple-ws-end)
;; Skipped over comments. Don't cache at eob in case the buffer
;; is narrowed.
(not (eobp)))
((save-excursion
(and c-opt-cpp-prefix
(looking-at c-opt-cpp-start)
(setq macro-start (point))
(progn (skip-chars-backward " \t")
(bolp))
(or (bobp)
(progn (backward-char)
(not (eq (char-before) ?\\))))))
;; Skip a preprocessor directive.
(end-of-line)
(while (and (eq (char-before) ?\\)
(= (forward-line 1) 0))
(end-of-line))
(setq macro-end (point))
;; Check for an open block comment at the end of the macro.
(let ((s (parse-partial-sexp macro-start macro-end)))
(if (and (elt s 4) ; in a comment
(null (elt s 7))) ; a block comment
(setq safe-start nil)))
(forward-line 1)
;; Don't cache at eob in case the buffer is narrowed.
(not (eobp)))
((and c-opt-cpp-prefix
(looking-at c-noise-macro-name-re))
;; Skip over a noise macro without parens.
(goto-char (match-end 1))
(not (eobp)))
((setq next-rung-pos (c-looking-at-c++-attribute))
(goto-char next-rung-pos)
(not (eobp)))
((looking-at c-doc-line-join-re)
;; Skip over a line join in (e.g.) Pike autodoc.
(goto-char (match-end 0))
(setq safe-start nil) ; Never cache this; the doc style could be
; changed at any time.
(not (eobp)))))
;; We've searched over a piece of non-white syntactic ws. See if this
;; can be cached.
(setq next-rung-pos (point))
(skip-chars-forward " \t\n\r\f\v")
(setq rung-end-pos (min (1+ (point)) (point-max)))
(if (or
;; Cache if we haven't skipped comments only, and if we started
;; either from a marked rung or from a completely uncached
;; position.
(and safe-start
(or rung-is-marked
(not (get-text-property simple-ws-end 'c-in-sws))))
;; See if there's a marked rung in the encountered simple ws. If
;; so then we can cache, unless `safe-start' is nil. Even then
;; we need to do this to check if the cache can be used for the
;; next step.
(and (setq next-rung-is-marked
(text-property-any next-rung-pos rung-end-pos
'c-is-sws t))
safe-start))
(progn
(c-debug-sws-msg
"c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
(point-max))
;; Remove the properties for any nested ws that might be cached.
;; Only necessary for `c-is-sws' since `c-in-sws' will be set
;; anyway.
(c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
(unless (and rung-is-marked (= rung-pos simple-ws-end))
(c-put-is-sws rung-pos
(1+ simple-ws-end))
(setq rung-is-marked t))
(c-put-in-sws rung-pos
(setq rung-pos (point)
last-put-in-sws-pos rung-pos))
(unless (get-text-property (1- rung-end-pos) 'c-is-sws)
;; Remove any `c-in-sws' property from the last char of
;; the rung before we mark it with `c-is-sws', so that we
;; won't connect with the remains of a broken "ladder".
(c-remove-in-sws (1- rung-end-pos) rung-end-pos))
(c-put-is-sws next-rung-pos
rung-end-pos))
(c-debug-sws-msg
"c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
(point-max))
;; Set `rung-pos' for the next rung. It's the same thing here as
;; initially, except that the rung position is set as early as
;; possible since we can't be in the ending ws of a line comment or
;; cpp directive now.
(if (setq rung-is-marked next-rung-is-marked)
(setq rung-pos (1- (c-next-single-property-change
rung-is-marked 'c-is-sws nil rung-end-pos)))
(setq rung-pos next-rung-pos))))
;; Make sure that the newly marked `c-in-sws' region doesn't connect to
;; another one after the point (which might occur when editing inside a
;; comment or macro).
(when (eq last-put-in-sws-pos (point))
(cond ((< last-put-in-sws-pos (point-max))
(c-debug-sws-msg
"c-forward-sws clearing at %s for cache separation"
last-put-in-sws-pos)
(c-remove-in-sws last-put-in-sws-pos
(1+ last-put-in-sws-pos)))
(t
;; If at eob we have to clear the last character before the end
;; instead since the buffer might be narrowed and there might
;; be a `c-in-sws' after (point-max). In this case it's
;; necessary to clear both properties.
(c-debug-sws-msg
"c-forward-sws clearing thoroughly at %s for cache separation"
(1- last-put-in-sws-pos))
(c-remove-is-and-in-sws (1- last-put-in-sws-pos)
last-put-in-sws-pos))))))))
(defun c-backward-sws ()
;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
;;
;; This function might do hidden buffer changes.
(let (;; `rung-pos' is set to a position as late as possible in the unmarked
;; part of the simple ws region.
(rung-pos (point)) next-rung-pos last-put-in-sws-pos
rung-is-marked simple-ws-beg cmt-skip-pos
(doc-line-join-here (concat c-doc-line-join-re "\\="))
attr-end)
;; Skip simple horizontal ws and do a quick check on the preceding
;; character to see if it's anything that can't end syntactic ws, so we can
;; bail out early in the majority of cases when there just are a few ws
;; chars. Newlines are complicated in the backward direction, so we can't
;; skip over them.
(skip-chars-backward " \t\f")
(when (and (not (bobp))
(save-excursion
(or (and
(memq (char-before) c-doc-line-join-end-ch) ; For speed.
(re-search-backward doc-line-join-here
(c-point 'bopl) t))
(and
(c-major-mode-is 'c++-mode)
(eq (char-before) ?\])
(eq (char-before (1- (point))) ?\])
(save-excursion
(and (c-go-list-backward)
(looking-at "\\[\\[")))
(setq attr-end (point)))
(progn
(backward-char)
(or (looking-at c-syntactic-ws-end)
(and c-opt-cpp-prefix
(looking-at c-symbol-char-key)
(progn (c-beginning-of-current-token)
(looking-at c-noise-macro-name-re))))))))
;; Try to find a rung position in the simple ws preceding point, so that
;; we can get a cache hit even if the last bit of the simple ws has
;; changed recently.
(setq simple-ws-beg (or attr-end ; After attribute.
(match-end 1) ; Noise macro, etc.
(match-end 0))) ; c-syntactic-ws-end
(skip-chars-backward " \t\n\r\f\v")
(if (setq rung-is-marked (text-property-any
(point) (min (1+ rung-pos) (point-max))
'c-is-sws t))
;; `rung-pos' will be the earliest marked position, which means that
;; there might be later unmarked parts in the simple ws region.
;; It's not worth the effort to fix that; the last part of the
;; simple ws is also typically edited often, so it could be wasted.
(goto-char (setq rung-pos rung-is-marked))
(goto-char simple-ws-beg))
(with-silent-modifications
(while
(progn
;; Each time round the next while form, we move back over a ladder
;; and append any simple WS preceding it, if possible joining with
;; the previous ladder.
(while
(when (and rung-is-marked
(not (bobp))
(get-text-property (1- (point)) 'c-in-sws))
;; The following search is the main reason that `c-in-sws'
;; and `c-is-sws' aren't combined to one property.
(goto-char (previous-single-property-change
(point) 'c-in-sws nil (point-min)))
(unless (get-text-property (point) 'c-is-sws)
;; If the `c-in-sws' region extended past the first
;; `c-is-sws' char we have to go forward a bit.
(goto-char (c-next-single-property-change
(point) 'c-is-sws)))
(c-debug-sws-msg
"c-backward-sws cached move %s <- %s (min %s)"
(point) rung-pos (point-min))
(setq rung-pos (point))
(if (and (< (min (skip-chars-backward " \t\f\v")
(progn
(setq simple-ws-beg (point))
(skip-chars-backward " \t\n\r\f\v")))
0)
(setq rung-is-marked
(text-property-any (point) rung-pos
'c-is-sws t)))
t
(goto-char simple-ws-beg)
nil))
;; We'll loop here if there is simple ws before the first rung.
;; That means that there's been some change in it and it's
;; possible that we've stepped into another ladder, so extend
;; the previous one to join with it if there is one, and try to
;; use the cache again.
(c-debug-sws-msg
"c-backward-sws extending rung with [%s..%s] (min %s)"
rung-is-marked rung-pos (point-min))
(unless (get-text-property (1- rung-pos) 'c-is-sws)
;; Remove any `c-in-sws' property from the last char of
;; the rung before we mark it with `c-is-sws', so that we
;; won't connect with the remains of a broken "ladder".
(c-remove-in-sws (1- rung-pos) rung-pos))
(c-put-is-sws rung-is-marked
rung-pos)
(c-put-in-sws rung-is-marked
(1- rung-pos))
(setq rung-pos rung-is-marked
last-put-in-sws-pos rung-pos))
(c-backward-comments)
(setq cmt-skip-pos (point))
(cond
((and c-opt-cpp-prefix
(/= cmt-skip-pos simple-ws-beg)
(c-beginning-of-macro))
;; Inside a cpp directive. See if it should be skipped over.
(let ((cpp-beg (point))
pause pos)
;; Move back over all line continuations and block comments in
;; the region skipped over by `c-backward-comments'. If we go
;; past it then we started inside the cpp directive.
(goto-char simple-ws-beg)
(beginning-of-line)
;; Note the similarity of the code here to some in
;; `c-beginning-of-macro'.
(setq pause (point))
(while
(progn
(while (and (> (point) cmt-skip-pos)
(progn (backward-char)
(eq (char-before) ?\\)))
(beginning-of-line))
(setq pos (point))
(when (and c-last-c-comment-end-on-line-re
(re-search-forward
c-last-c-comment-end-on-line-re pause t))
(goto-char (match-end 1))
(if (c-backward-single-comment)
(progn
(beginning-of-line)
(setq pause (point)))
(goto-char pos)
nil))))
(if (< (point) cmt-skip-pos)
;; Don't move past the cpp directive if we began inside
;; it. Note that the position at the end of the last line
;; of the macro is also considered to be within it.
(progn (goto-char cmt-skip-pos)
nil)
;; It's worthwhile to spend a little bit of effort on finding
;; the end of the macro, to get a good `simple-ws-beg'
;; position for the cache. Note that `c-backward-comments'
;; could have stepped over some comments before going into
;; the macro, and then `simple-ws-beg' must be kept on the
;; same side of those comments.
(goto-char simple-ws-beg)
(skip-chars-backward " \t\n\r\f\v")
(if (eq (char-before) ?\\)
(forward-char))
(forward-line 1)
(if (< (point) simple-ws-beg)
;; Might happen if comments after the macro were skipped
;; over.
(setq simple-ws-beg (point)))
(goto-char cpp-beg)
t)))
((/= (save-excursion
(skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
(setq next-rung-pos (point)))
simple-ws-beg)
;; Skipped over comments. Must put point at the end of
;; the simple ws at point since we might be after a line
;; comment or cpp directive that's been partially
;; narrowed out, and we can't risk marking the simple ws
;; at the end of it.
(goto-char next-rung-pos)
t)
((and c-opt-cpp-prefix
(save-excursion
(and (< (skip-syntax-backward "w_") 0)
(progn (setq next-rung-pos (point))
(looking-at c-noise-macro-name-re)))))
;; Skipped over a noise macro
(goto-char next-rung-pos)
t)
((and (c-major-mode-is 'c++-mode)
(eq (char-before) ?\])
(eq (char-before (1- (point))) ?\])
(save-excursion
(and (c-go-list-backward)
(setq next-rung-pos (point))
(looking-at "\\[\\["))))
(goto-char next-rung-pos)
t)
((and
(memq (char-before) c-doc-line-join-end-ch) ; For speed.
(re-search-backward doc-line-join-here (c-point 'bopl) t)))))
;; We've searched over a piece of non-white syntactic ws. See if this
;; can be cached.
(setq next-rung-pos (point))
(skip-chars-backward " \t\f\v")
(if (or
;; Cache if we started either from a marked rung or from a
;; completely uncached position.
rung-is-marked
(not (get-text-property (1- simple-ws-beg) 'c-in-sws))
;; Cache if there's a marked rung in the encountered simple ws.
(save-excursion
(skip-chars-backward " \t\n\r\f\v")
(text-property-any (point) (min (1+ next-rung-pos) (point-max))
'c-is-sws t)))
(progn
(c-debug-sws-msg
"c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
(point) (1+ next-rung-pos)
simple-ws-beg (min (1+ rung-pos) (point-max))
(point-min))
;; Remove the properties for any nested ws that might be cached.
;; Only necessary for `c-is-sws' since `c-in-sws' will be set
;; anyway.
(c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
(unless (and rung-is-marked (= simple-ws-beg rung-pos))
(let ((rung-end-pos (min (1+ rung-pos) (point-max))))
(unless (get-text-property (1- rung-end-pos) 'c-is-sws)
;; Remove any `c-in-sws' property from the last char of
;; the rung before we mark it with `c-is-sws', so that we
;; won't connect with the remains of a broken "ladder".
(c-remove-in-sws (1- rung-end-pos) rung-end-pos))
(c-put-is-sws simple-ws-beg
rung-end-pos)
(setq rung-is-marked t)))
(c-put-in-sws (setq simple-ws-beg (point)
last-put-in-sws-pos simple-ws-beg)
rung-pos)
(c-put-is-sws (setq rung-pos simple-ws-beg)
(1+ next-rung-pos)))
(c-debug-sws-msg
"c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
(point) (1+ next-rung-pos)
simple-ws-beg (min (1+ rung-pos) (point-max))
(point-min))
(setq rung-pos next-rung-pos
simple-ws-beg (point))
))
;; Make sure that the newly marked `c-in-sws' region doesn't connect to
;; another one before the point (which might occur when editing inside a
;; comment or macro).
(when (eq last-put-in-sws-pos (point))
(cond ((< (point-min) last-put-in-sws-pos)
(c-debug-sws-msg
"c-backward-sws clearing at %s for cache separation"
(1- last-put-in-sws-pos))
(c-remove-in-sws (1- last-put-in-sws-pos)
last-put-in-sws-pos))
((> (point-min) 1)
;; If at bob and the buffer is narrowed, we have to clear the
;; character we're standing on instead since there might be a
;; `c-in-sws' before (point-min). In this case it's necessary
;; to clear both properties.
(c-debug-sws-msg
"c-backward-sws clearing thoroughly at %s for cache separation"
last-put-in-sws-pos)
(c-remove-is-and-in-sws last-put-in-sws-pos
(1+ last-put-in-sws-pos)))))
))))
\f
;; Other whitespace tools
(defun c-partial-ws-p (beg end)
;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
;; region? This is a "heuristic" function. .....
;;
;; The motivation for the second bit is to check whether removing this
;; region would coalesce two symbols.
;;
;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
;; careful about using this function for, e.g. AWK. (2007/3/7)
(save-excursion
(let ((end+1 (min (1+ end) (point-max))))
(or (progn (goto-char (max (point-min) (1- beg)))
(c-skip-ws-forward end)
(eq (point) end))
(progn (goto-char beg)
(c-skip-ws-forward end+1)
(eq (point) end+1))))))
\f
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; We maintain a sopisticated cache of positions which are in a literal,
;; disregarding macros (i.e. we don't distinguish between "in a macro" and
;; not).
;;
;; This cache is in three parts: two "near" caches, which are association
;; lists of a small number (currently six) of positions and the parser states
;; there; the "far" cache (also known as "the cache"), a list of compressed
;; parser states going back to the beginning of the buffer, one entry every
;; 3000 characters.
;;
;; The two main callable functions embodying this cache are
;; `c-semi-pp-to-literal', which returns a `parse-partial-sexp' state at a
;; given position, together with the start of any literal enclosing it, and
;; `c-full-pp-to-literal', which additionally returns the end of such literal.
;; One of the above "near" caches is associated with each of these functions.
;;
;; When searching this cache, these functions first seek an exact match, then
;; a "close" match from the assiciated near cache. If neither of these
;; succeed, the nearest preceding entry in the far cache is used.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar c-lit-pos-cache nil)
(make-variable-buffer-local 'c-lit-pos-cache)
;; A list of elements in descending order of POS of one of the forms:
;; o - POS (when point is not in a literal);
;; o - (POS CHAR-1) (when the last character before point is potentially
;; the first of a two-character construct
;; o - (POS TYPE STARTING-POS) (when in a literal);
;; o - (POS TYPE STARTING-POS CHAR-1) (Combination of the previous two),
;;
;; where POS is the position for which the entry is valid, TYPE is the type of
;; the comment ('c or 'c++) or the character which should close the string
;; (e.g. ?\") or t for a generic string. STARTING-POS is the starting
;; position of the comment or string. CHAR-1 is either the character
;; potentially forming the first half of a two-char construct (in Emacs <= 25
;; and XEmacs) or the syntax of the character (Emacs >= 26).
(defvar c-lit-pos-cache-limit 1)
(make-variable-buffer-local 'c-lit-pos-cache-limit)
;; An upper limit on valid entries in `c-lit-pos-cache'. This
;; is reduced by buffer changes, and increased by invocations of
;; `c-parse-ps-state-below'.
;; Note that as of 2019-05-27, the forms involving CHAR-1 are no longer used.
(defun c-cache-to-parse-ps-state (elt)
;; Create a list suitable to use as the old-state parameter to
;; `parse-partial-sexp', out of ELT, a member of
;; `c-lit-pos-cache'. ELT is either just a number, or a list
;; with 2, 3, or 4 members (See `c-parse-ps-state-to-cache'). That number
;; or the car of the list is the "position element" of ELT, the position
;; where ELT is valid.
;;
;; POINT is left at the position for which the returned state is valid. It
;; will be either the position element of ELT, or one character before
;; that. (The latter happens in Emacs <= 25 and XEmacs, when ELT indicates
;; its position element directly follows a potential first character of a
;; two char construct (such as a comment opener or an escaped character).)
(if (and (consp elt) (>= (length elt) 3))
;; Inside a string or comment
(let ((depth 0) (containing nil) (last nil)
in-string in-comment
(min-depth 0) com-style com-str-start (intermediate nil)
(char-1 (nth 3 elt)) ; first char of poss. 2-char construct
(pos (car elt))
(type (cadr elt)))
(setq com-str-start (car (cddr elt)))
(cond
((or (numberp type) (eq type t)) ; A string
(setq in-string type))
((memq type '(c c++)) ; A comment
(setq in-comment t
com-style (if (eq type 'c++) 1 nil)))
(t (c-benign-error "Invalid type %s in c-cache-to-parse-ps-state"
elt)))
(goto-char (if char-1
(1- pos)
pos))
(if (memq 'pps-extended-state c-emacs-features)
(list depth containing last
in-string in-comment nil
min-depth com-style com-str-start
intermediate nil)
(list depth containing last
in-string in-comment nil
min-depth com-style com-str-start
intermediate)))
;; Not in a string or comment.
(if (memq 'pps-extended-state c-emacs-features)
(progn
(goto-char (if (consp elt) (car elt) elt))
(list 0 nil nil nil nil
(and (consp elt) (eq (nth 1 elt) 9)) ; 9 is syntax code for "escape".
0 nil nil nil
(and (consp elt) (nth 1 elt))))
(goto-char (if (consp elt) (car elt) elt))
(if (and (consp elt) (cdr elt)) (backward-char))
(copy-tree '(0 nil nil nil nil
nil
0 nil nil nil)))))
;; Note that as of 2019-05-27, the forms involving CHAR-1 are no longer used.
(defun c-parse-ps-state-to-cache (state)
;; Convert STATE, a `parse-partial-sexp' state valid at POINT, to an element
;; for the `c-lit-pos-cache' cache. This is one of
;; o - POINT (when point is not in a literal);
;; o - (POINT CHAR-1) (when the last character before point is potentially
;; the first of a two-character construct
;; o - (POINT TYPE STARTING-POS) (when in a literal);
;; o - (POINT TYPE STARTING-POS CHAR-1) (Combination of the previous two),
;; where TYPE is the type of the literal (either 'c, or 'c++, or the
;; character which closes the string), STARTING-POS is the starting
;; position of the comment or string. CHAR-1 is either the character
;; potentially forming the first half of a two-char construct (in Emacs <=
;; 25 and XEmacs) or the syntax of the character (in Emacs >= 26).
(if (memq 'pps-extended-state c-emacs-features)
;; Emacs >= 26.
(let ((basic
(cond
((nth 3 state) ; A string
(list (point) (nth 3 state) (nth 8 state)))
((and (nth 4 state) ; A comment
(not (eq (nth 7 state) 'syntax-table))) ; but not a pseudo comment.
(list (point)
(if (eq (nth 7 state) 1) 'c++ 'c)
(nth 8 state)))
(t ; Neither string nor comment.
(point)))))
(if (nth 10 state)
(append (if (consp basic)
basic
(list basic))
(list (nth 10 state)))
basic))
;; Emacs <= 25, XEmacs.
(cond
((nth 3 state) ; A string
(if (eq (char-before) ?\\)
(list (point) (nth 3 state) (nth 8 state) ?\\)
(list (point) (nth 3 state) (nth 8 state))))
((and (nth 4 state) ; comment
(not (eq (nth 7 state) 'syntax-table)))
(if (and (eq (char-before) ?*)
(> (- (point) (nth 8 state)) 2)) ; not "/*/".
(list (point)
(if (eq (nth 7 state) 1) 'c++ 'c)
(nth 8 state)
?*)
(list (point)
(if (eq (nth 7 state) 1) 'c++ 'c)
(nth 8 state))))
(t (if (memq (char-before) '(?/ ?\\))
(list (point) (char-before))
(point))))))
(defsubst c-ps-state-cache-pos (elt)
;; Get the buffer position from ELT, an element from the cache
;; `c-lit-pos-cache'.
(if (atom elt)
elt
(car elt)))
(defun c-trim-lit-pos-cache ()
;; Trim the `c-lit-pos-cache' to take account of buffer
;; changes, indicated by `c-lit-pos-cache-limit'.
(while (and c-lit-pos-cache
(> (c-ps-state-cache-pos (car c-lit-pos-cache))
c-lit-pos-cache-limit))
(setq c-lit-pos-cache (cdr c-lit-pos-cache))))
(defconst c-state-nonlit-pos-interval 3000)
;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
(defun c-parse-ps-state-below (here)
;; Given a buffer position HERE, Return a cons (CACHE-POS . STATE), where
;; CACHE-POS is a position not very far before HERE for which the
;; parse-partial-sexp STATE is valid. Note that the only valid elements of
;; STATE are those concerning comments and strings; STATE is the state of a
;; null `parse-partial-sexp' scan when CACHE-POS is not in a comment or
;; string.
(save-excursion
(save-restriction
(widen)
(c-trim-lit-pos-cache)
(let ((c c-lit-pos-cache)
elt state npos high-elt)
(while (and c (> (c-ps-state-cache-pos (car c)) here))
(setq high-elt (car c))
(setq c (cdr c)))
(goto-char (or (and c (c-ps-state-cache-pos (car c)))
(point-min)))
(setq state
(if c
(c-cache-to-parse-ps-state (car c))
(copy-tree '(0 nil nil nil nil nil 0 nil nil nil nil))))
(when (not high-elt)
;; We need to extend the cache. Add an element to
;; `c-lit-pos-cache' each iteration of the following.
(while
(<= (setq npos (+ (point) c-state-nonlit-pos-interval)) here)
(setq state (parse-partial-sexp (point) npos nil nil state))
;; If we're after a \ or a / or * which might be a comment
;; delimiter half, move back a character.
(when (or (nth 5 state) ; After a quote character
(and (memq 'pps-extended-state c-emacs-features)
(nth 10 state))) ; in the middle of a 2-char seq.
(setq npos (1- npos))
(backward-char)
(when (nth 10 state)
(setcar (nthcdr 10 state) nil))
(when (nth 5 state)
(setcar (nthcdr 5 state) nil)))
(setq elt (c-parse-ps-state-to-cache state))
(setq c-lit-pos-cache
(cons elt c-lit-pos-cache))))
(if (> (point) c-lit-pos-cache-limit)
(setq c-lit-pos-cache-limit (point)))
(cons (point) state)))))
(defvar c-semi-lit-near-cache nil)
(make-variable-buffer-local 'c-semi-lit-near-cache)
;; A list of up to six recent results from `c-semi-pp-to-literal'. Each
;; element is a cons of the buffer position and the `parse-partial-sexp' state
;; at that position.
(defvar c-semi-near-cache-limit 1)
(make-variable-buffer-local 'c-semi-near-cache-limit)
;; An upper limit on valid entries in `c-semi-lit-near-cache'. This is
;; reduced by buffer changes, and increased by invocations of
;; `c-semi-pp-to-literal'.
(defun c-semi-trim-near-cache ()
;; Remove stale entries in `c-semi-lit-near-cache', i.e. those
;; whose positions are above `c-lit-pos-cache-limit'.
(let ((nc-list c-semi-lit-near-cache))
(while nc-list
(if (> (caar nc-list) c-semi-near-cache-limit)
(setq c-semi-lit-near-cache
(delq (car nc-list) c-semi-lit-near-cache)
nc-list c-semi-lit-near-cache) ; start again in case
; of list breakage.
(setq nc-list (cdr nc-list))))))
(defun c-semi-get-near-cache-entry (here)
;; Return the near cache entry at the highest position before HERE, if any,
;; or nil. The near cache entry is of the form (POSITION . STATE), where
;; STATE has the form of a result of `parse-partial-sexp'.
(let ((nc-pos-state
(or (assq here c-semi-lit-near-cache)
(let ((nc-list c-semi-lit-near-cache)
pos (nc-pos 0) cand-pos-state)
(catch 'found
(while nc-list
(setq pos (caar nc-list))
(when (>= here pos)
(cond
((and (cdar nc-list)
(nth 8 (cdar nc-list))
(< here (nth 8 (cdar nc-list))))
(throw 'found (car nc-list)))
((> pos nc-pos)
(setq nc-pos pos
cand-pos-state (car nc-list)))))
(setq nc-list (cdr nc-list)))
cand-pos-state)))))
(when (and nc-pos-state
(not (eq nc-pos-state (car c-semi-lit-near-cache))))
;; Move the found cache entry to the front of the list.
(setq c-semi-lit-near-cache
(delq nc-pos-state c-semi-lit-near-cache))
(push nc-pos-state c-semi-lit-near-cache))
(copy-tree nc-pos-state)))
(defun c-semi-put-near-cache-entry (here state)
;; Put a new near cache entry into the near cache.
(while (>= (length c-semi-lit-near-cache) 6)
(setq c-semi-lit-near-cache
(delq (car (last c-semi-lit-near-cache))
c-semi-lit-near-cache)))
(push (cons here state) c-semi-lit-near-cache)
(setq c-semi-near-cache-limit
(max c-semi-near-cache-limit here)))
(defun c-semi-pp-to-literal (here &optional not-in-delimiter)
;; Do a parse-partial-sexp from a position in the buffer before HERE which
;; isn't in a literal, and return information about HERE, either:
;; (STATE TYPE BEG) if HERE is in a literal; or
;; (STATE) otherwise,
;; where STATE is the parsing state at HERE, TYPE is the type of the literal
;; enclosing HERE, (one of 'string, 'c, 'c++) and BEG is the starting
;; position of that literal (including the delimiter).
;;
;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
;; comment opener, this is recognized as being in a comment literal.
;;
;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote), 7
;; (comment type), and 8 (start of comment/string), and possibly 10 (in
;; newer Emacsen only, the syntax of a position after a potential first char
;; of a two char construct) of STATE are valid.
(save-excursion
(save-restriction
(widen)
(c-trim-lit-pos-cache)
(c-semi-trim-near-cache)
(save-match-data
(let* ((pos-and-state (c-semi-get-near-cache-entry here))
(pos (car pos-and-state))
(near-pos pos)
(s (cdr pos-and-state))
far-pos-and-state far-pos far-s ty)
(if (or (not pos)
(< pos (- here 100)))
(progn
(setq far-pos-and-state (c-parse-ps-state-below here)
far-pos (car far-pos-and-state)
far-s (cdr far-pos-and-state))
(when (or (not pos) (> far-pos pos))
(setq pos far-pos
s far-s))))
(when
(or
(> here pos)
(null (nth 8 s))
(< here (nth 8 s)) ; Can't happen, can it?
(not
(or
(and (nth 3 s) ; string
(not (eq (char-before here) ?\\)))
(and (nth 4 s) (not (nth 7 s)) ; Block comment
(not (memq (char-before here)
c-block-comment-awkward-chars)))
(and (nth 4 s) (nth 7 s) ; Line comment
(not (memq (char-before here) '(?\\ ?\n)))))))
(setq s (parse-partial-sexp pos here nil nil s)))
(when (not (eq near-pos here))
(c-semi-put-near-cache-entry here s))
(cond
((or (nth 3 s)
(and (nth 4 s)
(not (eq (nth 7 s) 'syntax-table)))) ; in a string or comment
(setq ty (cond
((nth 3 s) 'string)
((nth 7 s) 'c++)
(t 'c)))
(list s ty (nth 8 s)))
((and (not not-in-delimiter) ; inside a comment starter
(not (bobp))
(progn (backward-char)
(and (not (and (memq 'category-properties c-emacs-features)
(looking-at "\\s!")))
(looking-at c-comment-start-regexp))))
(setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++))
(list s ty (point)))
(t (list s))))))))
(defvar c-full-near-cache-limit 1)
(make-variable-buffer-local 'c-full-near-cache-limit)
;; An upper limit on valid entries in `c-full-lit-near-cache'. This
;; is reduced by buffer changes, and increased by invocations of
;; `c-full-pp-to-literal'.
(defvar c-full-lit-near-cache nil)
(make-variable-buffer-local 'c-full-lit-near-cache)
;; A list of up to six recent results from `c-full-pp-to-literal'. Each
;; element is a list (HERE STATE END)), where HERE is the buffer position the
;; function was called for, STATE is the `parse-partial-sexp' state there, and
;; END is the end of the literal enclosing HERE, if any, or nil otherwise.
;; N.B. END will be nil if the literal ends at EOB without a delimiter.
(defun c-full-trim-near-cache ()
;; Remove stale entries in `c-full-lit-near-cache', i.e. those whose END
;; entries, or positions, are above `c-full-near-cache-limit'.
(let ((nc-list c-full-lit-near-cache))
(while nc-list
(let ((elt (car nc-list)))
(if (if (car (cddr elt))
(< c-full-near-cache-limit (car (cddr elt)))
(< c-full-near-cache-limit (car elt)))
(setq c-full-lit-near-cache
(delq elt c-full-lit-near-cache)
nc-list c-full-lit-near-cache) ; start again in
; case of list breakage.
(setq nc-list (cdr nc-list)))))))
(defun c-full-get-near-cache-entry (here)
;; Return a near cache entry which either represents a literal which
;; encloses HERE, or is at the highest position before HERE. The returned
;; cache entry is of the form (POSITION STATE END), where STATE has the form
;; of a result from `parse-partial-sexp' which is valid at POSITION and END
;; is the end of any enclosing literal, or nil.
(let ((nc-pos-state
(or (assq here c-full-lit-near-cache)
(let ((nc-list c-full-lit-near-cache)
elt (nc-pos 0) cand-pos-state)
(catch 'found
(while nc-list
(setq elt (car nc-list))
(when
(and (car (cddr elt))
(> here (nth 8 (cadr elt)))
(< here (car (cddr elt))))
(throw 'found elt))
(when
(and (< (car elt) here)
(> (car elt) nc-pos))
(setq nc-pos (car elt)
cand-pos-state elt))
(setq nc-list (cdr nc-list)))
cand-pos-state)))))
;; Move the found cache entry, if any, to the front of the list.
(when (and nc-pos-state
(not (eq nc-pos-state (car c-full-lit-near-cache))))
(setq c-full-lit-near-cache
(delq nc-pos-state c-full-lit-near-cache))
(push nc-pos-state c-full-lit-near-cache))
(copy-tree nc-pos-state)))
(defun c-full-put-near-cache-entry (here state end)
;; Put a new near cache entry into the near cache.
(while (>= (length c-full-lit-near-cache) 6)
(setq c-full-lit-near-cache
(delq (car (last c-full-lit-near-cache))
c-full-lit-near-cache)))
(push (list here state end) c-full-lit-near-cache)
(setq c-full-near-cache-limit
(max c-full-near-cache-limit (or end here))))
(defun c-full-pp-to-literal (here &optional not-in-delimiter)
;; This function will supersede c-state-pp-to-literal.
;;
;; Do a parse-partial-sexp from a position in the buffer before HERE which
;; isn't in a literal, and return information about HERE, either:
;; (STATE TYPE (BEG . END)) if HERE is in a literal; or
;; (STATE) otherwise,
;; where STATE is the parsing state at HERE, TYPE is the type of the literal
;; enclosing HERE, (one of 'string, 'c, 'c++) and (BEG . END) is the
;; boundaries of that literal (including the delimiters), with END being nil
;; if there is no end delimiter (i.e. the literal ends at EOB).
;;
;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
;; comment opener, this is recognized as being in a comment literal.
;;
;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote), 7
;; (comment type), and 8 (start of comment/string), and possibly 10 (in
;; newer Emacsen only, the syntax of a position after a potential first char
;; of a two char construct) of STATE are valid.
(save-excursion
(save-restriction
(widen)
(c-trim-lit-pos-cache)
(c-full-trim-near-cache)
(save-match-data
(let* ((elt (c-full-get-near-cache-entry here))
(base (car elt))
(near-base base)
(s (cadr elt))
s1
(end (car (cddr elt)))
far-base-and-state far-base far-s ty start)
(if (or
(not base) ; FIXME!!! Compare base and far-base??
; (2019-05-21)
(not end)
(>= here end))
(progn
(setq far-base-and-state (c-parse-ps-state-below here)
far-base (car far-base-and-state)
far-s (cdr far-base-and-state))
(when (or (not base) (> far-base base))
(setq base far-base
s far-s
end nil))))
(when
(or
(and (> here base) (null end))
(null (nth 8 s))
(and end (>= here end))
(not
(or
(and (nth 3 s) ; string
(not (eq (char-before here) ?\\)))
(and (nth 4 s) (not (nth 7 s)) ; Block comment
(not (memq (char-before here)
c-block-comment-awkward-chars)))
(and (nth 4 s) (nth 7 s) ; Line comment
(not (memq (char-before here) '(?\\ ?\n)))))))
(setq s (parse-partial-sexp base here nil nil s)))
(cond
((or (nth 3 s)
(and (nth 4 s)
(not (eq (nth 7 s) 'syntax-table)))) ; in a string or comment
(setq ty (cond
((nth 3 s) 'string)
((nth 7 s) 'c++)
(t 'c)))
(setq start (nth 8 s))
(unless end
(setq s1 (parse-partial-sexp here (point-max)
nil ; TARGETDEPTH
nil ; STOPBEFORE
s ; OLDSTATE
'syntax-table)); stop at EO literal
(unless (or (nth 3 s1) ; still in a string
(and (nth 4 s1)
(not (eq (nth 7 s1) 'syntax-table)))) ; still
; in a
; comment
(setq end (point))))
(unless (eq near-base here)
(c-full-put-near-cache-entry here s end))
(list s ty (cons start end)))
((and (not not-in-delimiter) ; inside a comment starter
(not (bobp))
(progn (backward-char)
(and (not (and (memq 'category-properties c-emacs-features)
(looking-at "\\s!")))
(looking-at c-comment-start-regexp))))
(setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++)
start (point))
(forward-comment 1)
(list s ty (cons start (point))))
(t
(unless (eq near-base here)
(c-full-put-near-cache-entry here s nil))
(list s))))))))
(defsubst c-truncate-lit-pos-cache (pos)
;; Truncate the upper bound of each of the three caches to POS, if it is
;; higher than that position.
(setq c-lit-pos-cache-limit (min c-lit-pos-cache-limit pos)
c-semi-near-cache-limit (min c-semi-near-cache-limit pos)
c-full-near-cache-limit (min c-full-near-cache-limit pos)))
(defun c-foreign-truncate-lit-pos-cache (beg _end)
"Truncate CC Mode's literal cache.
This function should be added to the `before-change-functions'
hook by major modes that use CC Mode's filling functionality
without initializing CC Mode. Currently (2020-06) these are
js-mode and mhtml-mode."
(c-truncate-lit-pos-cache beg))
(defun c-foreign-init-lit-pos-cache ()
"Initialize CC Mode's literal cache.
This function should be called from the mode functions of major
modes which use CC Mode's filling functionality without
initializing CC Mode. Currently (2020-06) these are js-mode and
mhtml-mode."
(c-truncate-lit-pos-cache 1))
\f
;; A system for finding noteworthy parens before the point.
(defconst c-state-cache-too-far 5000)
;; A maximum comfortable scanning distance, e.g. between
;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
;; this distance is exceeded, we take "emergency measures", e.g. by clearing
;; the cache and starting again from point-min or a beginning of defun. This
;; value can be tuned for efficiency or set to a lower value for testing.
(defvar c-state-cache nil)
(make-variable-buffer-local 'c-state-cache)
;; The state cache used by `c-parse-state' to cut down the amount of
;; searching. It's the result from some earlier `c-parse-state' call. See
;; `c-parse-state''s doc string for details of its structure.
;;
;; The use of the cached info is more effective if the next
;; `c-parse-state' call is on a line close by the one the cached state
;; was made at; the cache can actually slow down a little if the
;; cached state was made very far back in the buffer. The cache is
;; most effective if `c-parse-state' is used on each line while moving
;; forward.
(defvar c-state-cache-good-pos 1)
(make-variable-buffer-local 'c-state-cache-good-pos)
;; This is a position where `c-state-cache' is known to be correct, or
;; nil (see below). It's a position inside one of the recorded unclosed
;; parens or the top level, but not further nested inside any literal or
;; subparen that is closed before the last recorded position.
;;
;; The exact position is chosen to try to be close to yet earlier than
;; the position where `c-state-cache' will be called next. Right now
;; the heuristic is to set it to the position after the last found
;; closing paren (of any type) before the line on which
;; `c-parse-state' was called. That is chosen primarily to work well
;; with refontification of the current line.
;;
;; 2009-07-28: When `c-state-point-min' and the last position where
;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
;; both in the same literal, there is no such "good position", and
;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
;;
;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
;; the middle of the desert, as long as it is not within a brace pair
;; recorded in `c-state-cache' or a paren/bracket pair.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; We maintain a simple cache of positions which aren't in a literal, so as to
;; speed up testing for non-literality.
(defvar c-state-nonlit-pos-cache nil)
(make-variable-buffer-local 'c-state-nonlit-pos-cache)
;; A list of buffer positions which are known not to be in a literal or a cpp
;; construct. This is ordered with higher positions at the front of the list.
;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
(defvar c-state-nonlit-pos-cache-limit 1)
(make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
;; reduced by buffer changes, and increased by invocations of
;; `c-state-literal-at'.
(defun c-state-pp-to-literal (from to &optional not-in-delimiter)
;; Do a parse-partial-sexp from FROM to TO, returning either
;; (STATE TYPE (BEG . END)) if TO is in a literal; or
;; (STATE) otherwise,
;; where STATE is the parsing state at TO, TYPE is the type of the literal
;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal,
;; including the delimiters.
;;
;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
;; comment opener, this is recognized as being in a comment literal.
;;
;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote),
;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of
;; STATE are valid.
(save-excursion
(save-match-data
(let ((s (parse-partial-sexp from to))
ty co-st)
(cond
((or (nth 3 s)
(and (nth 4 s)
(not (eq (nth 7 s) 'syntax-table)))) ; in a string or comment
(setq ty (cond
((nth 3 s) 'string)
((nth 7 s) 'c++)
(t 'c)))
(parse-partial-sexp (point) (point-max)
nil ; TARGETDEPTH
nil ; STOPBEFORE
s ; OLDSTATE
'syntax-table) ; stop at end of literal
`(,s ,ty (,(nth 8 s) . ,(point))))
((and (not not-in-delimiter) ; inside a comment starter
(not (bobp))
(progn (backward-char)
(and (not (looking-at "\\s!"))
(looking-at c-comment-start-regexp))))
(setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++)
co-st (point))
(forward-comment 1)
`(,s ,ty (,co-st . ,(point))))
(t `(,s)))))))
(defun c-state-safe-place (here)
;; Return a buffer position before HERE which is "safe", i.e. outside any
;; string, comment, or macro.
;;
;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
;; MAY NOT contain any positions within macros, since macros are frequently
;; turned into comments by use of the `c-cpp-delimiter' category properties.
;; We cannot rely on this mechanism whilst determining a cache pos since
;; this function is also called from outwith `c-parse-state'.
(save-restriction
(widen)
(save-excursion
(let ((c c-state-nonlit-pos-cache)
pos npos high-pos lit macro-beg macro-end)
;; Trim the cache to take account of buffer changes.
(while (and c (> (car c) c-state-nonlit-pos-cache-limit))
(setq c (cdr c)))
(setq c-state-nonlit-pos-cache c)
(while (and c (> (car c) here))
(setq high-pos (car c))
(setq c (cdr c)))
(setq pos (or (car c) (point-min)))
(unless high-pos
(while
;; Add an element to `c-state-nonlit-pos-cache' each iteration.
(and
(setq npos
(when (<= (+ pos c-state-nonlit-pos-interval) here)
(+ pos c-state-nonlit-pos-interval)))
;; Test for being in a literal. If so, go to after it.
(progn
(setq lit (car (cddr (c-state-pp-to-literal pos npos))))
(or (null lit)
(prog1 (<= (cdr lit) here)
(setq npos (cdr lit)))))
;; Test for being in a macro. If so, go to after it.
(progn
(goto-char npos)
(setq macro-beg
(and (c-beginning-of-macro) (/= (point) npos) (point)))
(when macro-beg
(c-syntactic-end-of-macro)
(or (eobp) (forward-char))
(setq macro-end (point)))
(or (null macro-beg)
(prog1 (<= macro-end here)
(setq npos macro-end)))))
(setq pos npos)
(setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
;; Add one extra element above HERE so as to avoid the previous
;; expensive calculation when the next call is close to the current
;; one. This is especially useful when inside a large macro.
(when npos
(setq c-state-nonlit-pos-cache
(cons npos c-state-nonlit-pos-cache))))
(if (> pos c-state-nonlit-pos-cache-limit)
(setq c-state-nonlit-pos-cache-limit pos))
pos))))
(defun c-state-literal-at (here)
;; If position HERE is inside a literal, return (START . END), the
;; boundaries of the literal (which may be outside the accessible bit of the
;; buffer). Otherwise, return nil.
;;
;; This function is almost the same as `c-literal-limits'. Previously, it
;; differed in that it was a lower level function, and that it rigorously
;; followed the syntax from BOB. `c-literal-limits' is now (2011-12)
;; virtually identical to this function.
(save-restriction
(widen)
(save-excursion
(let ((pos (c-state-safe-place here)))
(car (cddr (c-state-pp-to-literal pos here)))))))
(defsubst c-state-lit-beg (pos)
;; Return the start of the literal containing POS, or POS itself.
(or (car (c-state-literal-at pos))
pos))
(defun c-state-cache-lower-good-pos (here pos state)
;; Return a good pos (in the sense of `c-state-cache-good-pos') at the
;; lowest[*] position between POS and HERE which is syntactically equivalent
;; to HERE. This position may be HERE itself. POS is before HERE in the
;; buffer.
;; [*] We don't actually always determine this exact position, since this
;; would require a disproportionate amount of work, given that this function
;; deals only with a corner condition, and POS and HERE are typically on
;; adjacent lines. We actually return either POS, when POS is a good
;; position, HERE otherwise. Exceptionally, when POS is in a comment, but
;; HERE not, we can return the position of the end of the comment.
(let (s)
(save-excursion
(goto-char pos)
(when (nth 8 state) ; POS in a comment or string. Move out of it.
(setq s (parse-partial-sexp pos here nil nil state 'syntax-table))
(when (< (point) here)
(setq pos (point)
state s)))
(if (eq (point) here) ; HERE is in the same literal as POS
pos
(setq s (parse-partial-sexp pos here (1+ (car state)) nil state nil))
(cond
((> (car s) (car state)) ; Moved into a paren between POS and HERE
here)
((not (eq (nth 6 s) (car state))) ; Moved out of a paren between POS
; and HERE
here)
(t pos))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stuff to do with point-min, and coping with any literal there.
(defvar c-state-point-min 1)
(make-variable-buffer-local 'c-state-point-min)
;; This is (point-min) when `c-state-cache' was last calculated. A change of
;; narrowing is likely to affect the parens that are visible before the point.
(defvar c-state-point-min-lit-type nil)
(make-variable-buffer-local 'c-state-point-min-lit-type)
(defvar c-state-point-min-lit-start nil)
(make-variable-buffer-local 'c-state-point-min-lit-start)
;; These two variables define the literal, if any, containing point-min.
;; Their values are, respectively, 'string, c, or c++, and the start of the
;; literal. If there's no literal there, they're both nil.
(defvar c-state-min-scan-pos 1)
(make-variable-buffer-local 'c-state-min-scan-pos)
;; This is the earliest buffer-pos from which scanning can be done. It is
;; either the end of the literal containing point-min, or point-min itself.
;; It becomes nil if the buffer is changed earlier than this point.
(defun c-state-get-min-scan-pos ()
;; Return the lowest valid scanning pos. This will be the end of the
;; literal enclosing point-min, or point-min itself.
(or c-state-min-scan-pos
(save-restriction
(save-excursion
(widen)
(goto-char c-state-point-min-lit-start)
(if (eq c-state-point-min-lit-type 'string)
(forward-sexp)
(forward-comment 1))
(setq c-state-min-scan-pos (point))))))
(defun c-state-mark-point-min-literal ()
;; Determine the properties of any literal containing POINT-MIN, setting the
;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
(let ((p-min (point-min))
lit)
(save-restriction
(widen)
(setq lit (c-state-literal-at p-min))
(if lit
(setq c-state-point-min-lit-type
(save-excursion
(goto-char (car lit))
(cond
((looking-at c-block-comment-start-regexp) 'c)
((looking-at c-line-comment-starter) 'c++)
(t 'string)))
c-state-point-min-lit-start (car lit)
c-state-min-scan-pos (cdr lit))
(setq c-state-point-min-lit-type nil
c-state-point-min-lit-start nil
c-state-min-scan-pos p-min)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A variable which signals a brace dessert - helpful for reducing the number
;; of fruitless backward scans.
(defvar c-state-brace-pair-desert nil)
(make-variable-buffer-local 'c-state-brace-pair-desert)
;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
;; that defun has searched backwards for a brace pair and not found one. Its
;; value is either nil or a cons (PA . FROM), where PA is the position of the
;; enclosing opening paren/brace/bracket which bounds the backwards search (or
;; nil when at top level) and FROM is where the backward search started. It
;; is reset to nil in `c-invalidate-state-cache'.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Lowish level functions/macros which work directly on `c-state-cache', or a
;; list of like structure.
(defmacro c-state-cache-top-lparen (&optional cache)
;; Return the address of the top left brace/bracket/paren recorded in CACHE
;; (default `c-state-cache') (or nil).
(let ((cash (or cache 'c-state-cache)))
`(if (consp (car ,cash))
(caar ,cash)
(car ,cash))))
(defmacro c-state-cache-top-paren (&optional cache)
;; Return the address of the latest brace/bracket/paren (whether left or
;; right) recorded in CACHE (default `c-state-cache') or nil.
(let ((cash (or cache 'c-state-cache)))
`(if (consp (car ,cash))
(cdar ,cash)
(car ,cash))))
(defmacro c-state-cache-after-top-paren (&optional cache)
;; Return the position just after the latest brace/bracket/paren (whether
;; left or right) recorded in CACHE (default `c-state-cache') or nil.
(let ((cash (or cache 'c-state-cache)))
`(if (consp (car ,cash))
(cdar ,cash)
(and (car ,cash)
(1+ (car ,cash))))))
(defun c-get-cache-scan-pos (here)
;; From the state-cache, determine the buffer position from which we might
;; scan forward to HERE to update this cache. This position will be just
;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
;; return the earliest position in the accessible region which isn't within
;; a literal. If the visible portion of the buffer is entirely within a
;; literal, return NIL.
(let ((c c-state-cache) elt)
;(while (>= (or (c-state-cache-top-lparen c) 1) here)
(while (and c
(>= (c-state-cache-top-lparen c) here))
(setq c (cdr c)))
(setq elt (car c))
(cond
((consp elt)
(if (> (cdr elt) here)
(1+ (car elt))
(cdr elt)))
(elt (1+ elt))
((<= (c-state-get-min-scan-pos) here)
(c-state-get-min-scan-pos))
(t nil))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Variables which keep track of preprocessor constructs.
(defvar c-state-old-cpp-beg-marker nil)
(make-variable-buffer-local 'c-state-old-cpp-beg-marker)
(defvar c-state-old-cpp-beg nil)
(make-variable-buffer-local 'c-state-old-cpp-beg)
(defvar c-state-old-cpp-end-marker nil)
(make-variable-buffer-local 'c-state-old-cpp-end-marker)
(defvar c-state-old-cpp-end nil)
(make-variable-buffer-local 'c-state-old-cpp-end)
;; These are the limits of the macro containing point at the previous call of
;; `c-parse-state', or nil.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Defuns which analyze the buffer, yet don't change `c-state-cache'.
(defun c-get-fallback-scan-pos (here)
;; Return a start position for building `c-state-cache' from
;; scratch. This will be at the top level, 2 defuns back.
(save-excursion
(save-restriction
(when (> here (* 10 c-state-cache-too-far))
(narrow-to-region (- here (* 10 c-state-cache-too-far)) here))
;; Go back 2 bods, but ignore any bogus positions returned by
;; beginning-of-defun (i.e. open paren in column zero).
(goto-char here)
(let ((cnt 2))
(while (not (or (bobp) (zerop cnt)))
(c-beginning-of-defun-1) ; Pure elisp BOD.
(if (eq (char-after) ?\{)
(setq cnt (1- cnt)))))
(point))))
(defun c-state-balance-parens-backwards (here- here+ top)
;; Return the position of the opening paren/brace/bracket before HERE- which
;; matches the outermost close p/b/b between HERE+ and TOP. Except when
;; there's a macro, HERE- and HERE+ are the same. Like this:
;;
;; ............................................
;; | |
;; ( [ ( .........#macro.. ) ( ) ] )
;; ^ ^ ^ ^
;; | | | |
;; return HERE- HERE+ TOP
;;
;; If there aren't enough opening paren/brace/brackets, return the position
;; of the outermost one found, or HERE- if there are none. If there are no
;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
;; must not be inside literals. Only the accessible portion of the buffer
;; will be scanned.
;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
;; `here'. Go round the next loop each time we pass over such a ")". These
;; probably match "("s before `here-'.
(let (pos pa ren+1 lonely-rens)
(save-excursion
(save-restriction
(narrow-to-region (point-min) top) ; This can move point, sometimes.
(setq pos here+)
(c-safe
(while
(setq ren+1 (c-sc-scan-lists pos 1 1)) ; might signal
(setq lonely-rens (cons ren+1 lonely-rens)
pos ren+1)))))
;; PART 2: Scan back before `here-' searching for the "("s
;; matching/mismatching the ")"s found above. We only need to direct the
;; caller to scan when we've encountered unmatched right parens.
(setq pos here-)
(when lonely-rens
(c-safe
(while
(and lonely-rens ; actual values aren't used.
(setq pa (c-sc-scan-lists pos -1 1)))
(setq pos pa)
(setq lonely-rens (cdr lonely-rens)))))
pos))
(defun c-parse-state-get-strategy (here good-pos)
;; Determine the scanning strategy for adjusting `c-parse-state', attempting
;; to minimize the amount of scanning. HERE is the pertinent position in
;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
;; its head trimmed) is known to be good, or nil if there is no such
;; position.
;;
;; The return value is a list, one of the following:
;;
;; o - ('forward START-POINT) - scan forward from START-POINT,
;; which is not less than the highest position in `c-state-cache' below HERE,
;; which is after GOOD-POS.
;; o - ('backward nil) - scan backwards (from HERE).
;; o - ('back-and-forward START-POINT) - like 'forward, but when HERE is earlier
;; than GOOD-POS.
;; o - ('BOD START-POINT) - scan forwards from START-POINT, which is at the
;; top level.
;; o - ('IN-LIT nil) - point is inside the literal containing point-min.
(let* ((in-macro-start ; start of macro containing HERE or nil.
(save-excursion
(goto-char here)
(and (c-beginning-of-macro)
(point))))
(changed-macro-start
(and in-macro-start
(not (and c-state-old-cpp-beg
(= in-macro-start c-state-old-cpp-beg)))
in-macro-start))
(cache-pos (c-get-cache-scan-pos (if changed-macro-start
(min changed-macro-start here)
here))) ; highest suitable position in cache (or 1)
BOD-pos ; position of 2nd BOD before HERE.
strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
start-point
how-far) ; putative scanning distance.
(setq good-pos (or good-pos (c-state-get-min-scan-pos)))
(cond
((< here (c-state-get-min-scan-pos))
(setq strategy 'IN-LIT
start-point nil
cache-pos nil
how-far 0))
((<= good-pos here)
(setq strategy 'forward
start-point (max good-pos cache-pos)
how-far (- here start-point)))
((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
(setq strategy 'backward
how-far (- good-pos here)))
(t
(setq strategy 'back-and-forward
start-point cache-pos
how-far (- here start-point))))
;; Might we be better off starting from the top level, two defuns back,
;; instead? This heuristic no longer works well in C++, where
;; declarations inside namespace brace blocks are frequently placed at
;; column zero. (2015-11-10): Remove the condition on C++ Mode.
(when (and (or (not (memq 'col-0-paren c-emacs-features))
open-paren-in-column-0-is-defun-start)
;; (not (c-major-mode-is 'c++-mode))
(> how-far c-state-cache-too-far))
(setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
(if (< (- here BOD-pos) how-far)
(setq strategy 'BOD
start-point BOD-pos)))
(list strategy start-point)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Routines which change `c-state-cache' and associated values.
(defun c-renarrow-state-cache ()
;; The region (more precisely, point-min) has changed since we
;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
(if (< (point-min) c-state-point-min)
;; If point-min has MOVED BACKWARDS then we drop the state completely.
;; It would be possible to do a better job here and recalculate the top
;; only.
(progn
(c-state-mark-point-min-literal)
(setq c-state-cache nil
c-state-cache-good-pos c-state-min-scan-pos
c-state-brace-pair-desert nil))
;; point-min has MOVED FORWARD.
;; Is the new point-min inside a (different) literal?
(unless (and c-state-point-min-lit-start ; at prev. point-min
(< (point-min) (c-state-get-min-scan-pos)))
(c-state-mark-point-min-literal))
;; Cut off a bit of the tail from `c-state-cache'.
(let ((ptr (cons nil c-state-cache))
pa)
(while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
(>= pa (point-min)))
(setq ptr (cdr ptr)))
(when (consp ptr)
(if (or (eq (cdr ptr) c-state-cache)
(and (consp (cadr ptr))
(> (cdr (cadr ptr)) (point-min)))) ; Our new point-min is
; inside a recorded
; brace pair.
(setq c-state-cache nil
c-state-cache-good-pos c-state-min-scan-pos)
;; Do not alter the original `c-state-cache' structure, since there
;; may be a loop suspended which is looping through that structure.
;; This may have been the cause of bug #37910.
(let ((cdr-ptr (cdr ptr)))
(setcdr ptr nil)
(setq c-state-cache (copy-sequence c-state-cache))
(setcdr ptr cdr-ptr))
(setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
)))
(setq c-state-point-min (point-min)))
(defun c-append-lower-brace-pair-to-state-cache (from here &optional upper-lim)
;; If there is a brace pair preceding FROM in the buffer, at the same level
;; of nesting (not necessarily immediately preceding), push a cons onto
;; `c-state-cache' to represent it. FROM must not be inside a literal. If
;; UPPER-LIM is non-nil, we append the highest brace pair whose "}" is below
;; UPPER-LIM.
;;
;; Return non-nil when this has been done.
;;
;; The situation it copes with is this transformation:
;;
;; OLD: { (.) {...........}
;; ^ ^
;; FROM HERE
;;
;; NEW: { {....} (.) {.........
;; ^ ^ ^
;; LOWER BRACE PAIR HERE or HERE
;;
;; This routine should be fast. Since it can get called a LOT, we maintain
;; `c-state-brace-pair-desert', a small cache of "failures", such that we
;; reduce the time wasted in repeated fruitless searches in brace deserts.
(save-excursion
(save-restriction
(let* (new-cons
(cache-pos (c-state-cache-top-lparen)) ; might be nil.
(macro-start-or-from
(progn (goto-char from)
(c-beginning-of-macro)
(point)))
(bra ; Position of "{".
;; Don't start scanning in the middle of a CPP construct unless
;; it contains HERE - these constructs, in Emacs, are "commented
;; out" with category properties.
(if (eq (c-get-char-property macro-start-or-from 'category)
'c-cpp-delimiter)
macro-start-or-from
from))
ce) ; Position of "}"
(or upper-lim (setq upper-lim from))
;; If we're essentially repeating a fruitless search, just give up.
(unless (and c-state-brace-pair-desert
(eq cache-pos (car c-state-brace-pair-desert))
(or (null (car c-state-brace-pair-desert))
(> from (car c-state-brace-pair-desert)))
(<= from (cdr c-state-brace-pair-desert)))
;; DESERT-LIM. Avoid repeated searching through the cached desert.
(let ((desert-lim
(and c-state-brace-pair-desert
(eq cache-pos (car c-state-brace-pair-desert))
(>= from (cdr c-state-brace-pair-desert))
(cdr c-state-brace-pair-desert)))
;; CACHE-LIM. This limit will be necessary when an opening
;; paren at `cache-pos' has just had its matching close paren
;; inserted into the buffer. `cache-pos' continues to be a
;; search bound, even though the algorithm below would skip
;; over the new paren pair.
(cache-lim (and cache-pos (< cache-pos from) cache-pos)))
(narrow-to-region
(cond
((and desert-lim cache-lim)
(max desert-lim cache-lim))
(desert-lim)
(cache-lim)
((point-min)))
;; The top limit is EOB to ensure that `bra' is inside the
;; accessible part of the buffer at the next scan operation.
(1+ (buffer-size))))
;; In the next pair of nested loops, the inner one moves back past a
;; pair of (mis-)matching parens or brackets; the outer one moves
;; back over a sequence of unmatched close brace/paren/bracket each
;; time round.
(while
(progn
(c-safe
(while
(and (setq ce (c-sc-scan-lists bra -1 -1)) ; back past )/]/}; might signal
(setq bra (c-sc-scan-lists ce -1 1)) ; back past (/[/{; might signal
(or (> bra here) ;(> ce here)
(and
(< ce here)
(or (not (eq (char-after bra) ?\{))
(and (goto-char bra)
(c-beginning-of-macro)
(< (point) macro-start-or-from))))))))
(and ce (< ce bra)))
(setq bra ce)) ; If we just backed over an unbalanced closing
; brace, ignore it.
(if (and ce (< ce here) (< bra ce) (eq (char-after bra) ?\{))
;; We've found the desired brace-pair.
(progn
(setq new-cons (cons bra (1+ ce)))
(cond
((consp (car c-state-cache))
(setq c-state-cache (cons new-cons (cdr c-state-cache))))
((and (numberp (car c-state-cache)) ; probably never happens
(< ce (car c-state-cache)))
(setq c-state-cache
(cons (car c-state-cache)
(cons new-cons (cdr c-state-cache)))))
(t (setq c-state-cache (cons new-cons c-state-cache)))))
;; We haven't found a brace pair. Record this in the cache.
(setq c-state-brace-pair-desert
(cons (if (and ce (< bra ce) (> ce here)) ; {..} straddling HERE?
bra
(point-min))
(min here from)))))))))
(defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
;; following a {, and that brace has a (mis-)matching } (or ]), and we
;; "push" "a" brace pair onto `c-state-cache'.
;;
;; Here "push" means overwrite the top element if it's itself a brace-pair,
;; otherwise push it normally.
;;
;; The brace pair we push is normally the one surrounding BRA+1, but if the
;; latter is inside a macro, not being a macro containing
;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
;; base pair. This latter case is assumed to be rare.
;;
;; Note: POINT is not preserved in this routine.
(if bra+1
(if (or (> bra+1 macro-start-or-here)
(progn (goto-char bra+1)
(not (c-beginning-of-macro))))
(setq c-state-cache
(cons (cons (1- bra+1)
(c-sc-scan-lists bra+1 1 1))
(if (consp (car c-state-cache))
(cdr c-state-cache)
c-state-cache)))
;; N.B. This defsubst codes one method for the simple, normal case,
;; and a more sophisticated, slower way for the general case. Don't
;; eliminate this defsubst - it's a speed optimization.
(c-append-lower-brace-pair-to-state-cache (1- bra+1) (point-max)))))
(defun c-append-to-state-cache (from here)
;; Scan the buffer from FROM to HERE, adding elements into `c-state-cache'
;; for braces etc. Return a candidate for `c-state-cache-good-pos'.
;;
;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
;; any. Typically, it is immediately after it. It must not be inside a
;; literal.
(let ((here-bol (c-point 'bol here))
(macro-start-or-here
(save-excursion (goto-char here)
(if (c-beginning-of-macro)
(point)
here)))
pa+1 ; pos just after an opening PAren (or brace).
(ren+1 from) ; usually a pos just after a closing paREN etc.
; Is actually the pos. to scan for a (/{/[ from,
; which sometimes is after a silly )/}/].
paren+1 ; Pos after some opening or closing paren.
paren+1s ; A list of `paren+1's; used to determine a
; good-pos.
bra+1 ; just after L bra-ce.
mstart) ; start of a macro.
(save-excursion
(save-restriction
(narrow-to-region (point-min) here)
;; Each time round the following loop, we enter a successively deeper
;; level of brace/paren nesting. (Except sometimes we "continue at
;; the existing level".) `pa+1' is a pos inside an opening
;; brace/paren/bracket, usually just after it.
(while
(progn
;; Each time round the next loop moves forward over an opening then
;; a closing brace/bracket/paren. This loop is white hot, so it
;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
;; call of `scan-lists' signals an error, which happens when there
;; are no more b/b/p's to scan.
(c-safe
(while t
(setq pa+1 (c-sc-scan-lists ren+1 1 -1) ; Into (/{/[; might signal
paren+1s (cons pa+1 paren+1s))
(setq ren+1 (c-sc-scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
(if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
(setq bra+1 pa+1))
(setcar paren+1s ren+1)))
(if (and pa+1 (> pa+1 ren+1))
;; We've just entered a deeper nesting level.
(progn
;; Insert the brace pair (if present) and the single open
;; paren/brace/bracket into `c-state-cache' It cannot be
;; inside a macro, except one around point, because of what
;; `c-neutralize-syntax-in-CPP' has done.
(c-state-push-any-brace-pair bra+1 macro-start-or-here)
;; Insert the opening brace/bracket/paren position.
(setq c-state-cache (cons (1- pa+1) c-state-cache))
;; Clear admin stuff for the next more nested part of the scan.
(setq ren+1 pa+1 pa+1 nil bra+1 nil)
t) ; Carry on the loop
;; All open p/b/b's at this nesting level, if any, have probably
;; been closed by matching/mismatching ones. We're probably
;; finished - we just need to check for having found an
;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
;; macro, due the action of `c-neutralize-syntax-in-CPP'.
(c-safe (setq ren+1 (c-sc-scan-lists ren+1 1 1)))))) ; acts as loop control.
;; Record the final, innermost, brace-pair if there is one.
(c-state-push-any-brace-pair bra+1 macro-start-or-here)
;; Determine a good pos
(while (and (setq paren+1 (car paren+1s))
(> (if (> paren+1 macro-start-or-here)
paren+1
(goto-char paren+1)
(setq mstart (and (c-beginning-of-macro)
(point)))
(or mstart paren+1))
here-bol))
(setq paren+1s (cdr paren+1s)))
(cond
((and paren+1 mstart)
(min paren+1 mstart))
(paren+1)
(t from))))))
(defun c-remove-stale-state-cache (start-point here pps-point)
;; Remove stale entries from the `c-cache-state', i.e. those which will
;; not be in it when it is amended for position HERE. This may involve
;; replacing a CONS element for a brace pair containing HERE with its car.
;; Additionally, the "outermost" open-brace entry before HERE will be
;; converted to a cons if the matching close-brace is below HERE.
;;
;; START-POINT is a "maximal" "safe position" - there must be no open
;; parens/braces/brackets between START-POINT and HERE.
;;
;; As a second thing, calculate the result of parse-partial-sexp at
;; PPS-POINT, w.r.t. START-POINT. The motivation here is that
;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
;; adjust it to get outside a string/comment. (Sorry about this! The code
;; needs to be FAST).
;;
;; Return a list (GOOD-POS SCAN-BACK-POS CONS-SEPARATED PPS-STATE), where
;; o - GOOD-POS is a position where the new value `c-state-cache' is known
;; to be good (we aim for this to be as high as possible);
;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
;; preceding POS which needs to be recorded in `c-state-cache'. It is a
;; position to scan backwards from. It is the position of the "{" of the
;; last element to be removed from `c-state-cache', when that elt is a
;; cons, otherwise nil.
;; o - CONS-SEPARATED is t when a cons element in `c-state-cache' has been
;; replaced by its car because HERE lies inside the brace pair represented
;; by the cons.
;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
(save-excursion
(save-restriction
(narrow-to-region 1 (point-max))
(let* ((in-macro-start ; start of macro containing HERE or nil.
(save-excursion
(goto-char here)
(and (c-beginning-of-macro)
(point))))
(start-point-actual-macro-start ; Start of macro containing
; start-point or nil
(and (< start-point here)
(save-excursion
(goto-char start-point)
(and (c-beginning-of-macro)
(point)))))
(start-point-actual-macro-end ; End of this macro, (maybe
; HERE), or nil.
(and start-point-actual-macro-start
(save-excursion
(goto-char start-point-actual-macro-start)
(c-end-of-macro)
(point))))
pps-state ; Will be 9 or 10 elements long.
pos
upper-lim ; ,beyond which `c-state-cache' entries are removed
scan-back-pos
cons-separated
pair-beg target-depth)
;; Remove entries beyond HERE. Also remove any entries inside
;; a macro, unless HERE is in the same macro.
(setq upper-lim
(if (or (null c-state-old-cpp-beg)
(and (> here c-state-old-cpp-beg)
(< here c-state-old-cpp-end)))
here
(min here c-state-old-cpp-beg)))
(while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
(setq scan-back-pos (car-safe (car c-state-cache)))
(setq c-state-cache (cdr c-state-cache)))
;; If `upper-lim' is inside the last recorded brace pair, remove its
;; RBrace and indicate we'll need to search backwards for a previous
;; brace pair.
(when (and c-state-cache
(consp (car c-state-cache))
(> (cdar c-state-cache) upper-lim))
(setq c-state-cache (cons (caar c-state-cache) (cdr c-state-cache)))
(setq scan-back-pos (car c-state-cache)
cons-separated t))
;; The next loop jumps forward out of a nested level of parens each
;; time round; the corresponding elements in `c-state-cache' are
;; removed. `pos' is just after the brace-pair or the open paren at
;; (car c-state-cache). There can be no open parens/braces/brackets
;; between `start-point'/`start-point-actual-macro-start' and HERE,
;; due to the interface spec to this function.
(setq pos (if (and start-point-actual-macro-end
(not (eq start-point-actual-macro-start
in-macro-start)))
(1+ start-point-actual-macro-end) ; get outside the macro as
; marked by a `category' text property.
start-point))
(goto-char pos)
(while (and c-state-cache
(or (numberp (car c-state-cache)) ; Have we a { at all?
(cdr c-state-cache))
(< (point) here))
(cond
((null pps-state) ; first time through
(setq target-depth -1))
((eq (car pps-state) target-depth) ; found closing ),},]
(setq target-depth (1- (car pps-state))))
;; Do nothing when we've merely reached pps-point.
)
;; Scan!
(setq pps-state
(c-sc-parse-partial-sexp
(point) (if (< (point) pps-point) pps-point here)
target-depth
nil pps-state))
(when (eq (car pps-state) target-depth)
(setq pos (point)) ; POS is now just after an R-paren/brace.
(cond
((and (consp (car c-state-cache))
(eq (point) (cdar c-state-cache)))
;; We've just moved out of the paren pair containing the brace-pair
;; at (car c-state-cache). `pair-beg' is where the open paren is,
;; and is potentially where the open brace of a cons in
;; c-state-cache will be.
(setq pair-beg (car-safe (cdr c-state-cache))
c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
((numberp (car c-state-cache))
(setq pair-beg (car c-state-cache)
c-state-cache (cdr c-state-cache))) ; remove this
; containing Lparen
((numberp (cadr c-state-cache))
(setq pair-beg (cadr c-state-cache)
c-state-cache (cddr c-state-cache))) ; Remove a paren pair
; together with enclosed brace pair.
;; (t nil) ; Ignore an unmated Rparen.
)))
(if (< (point) pps-point)
(setq pps-state (c-sc-parse-partial-sexp
(point) pps-point
nil nil ; TARGETDEPTH, STOPBEFORE
pps-state)))
;; If the last paren pair we moved out of was actually a brace pair,
;; insert it into `c-state-cache'.
(when (and pair-beg (eq (char-after pair-beg) ?{))
(if (consp (car-safe c-state-cache))
(setq c-state-cache (cdr c-state-cache)))
(setq c-state-cache (cons (cons pair-beg pos)
c-state-cache)))
(list pos scan-back-pos cons-separated pps-state)))))
(defun c-remove-stale-state-cache-backwards (here)
;; Strip stale elements of `c-state-cache' by moving backwards through the
;; buffer, and inform the caller of the scenario detected.
;;
;; HERE is the position we're setting `c-state-cache' for.
;; CACHE-POS (a locally bound variable) is just after the latest recorded
;; position in `c-state-cache' before HERE, or a position at or near
;; point-min which isn't in a literal.
;;
;; This function must only be called only when (> `c-state-cache-good-pos'
;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
;; optimized to eliminate (or minimize) scanning between these two
;; positions.
;;
;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
;; could become so after missing elements are inserted into
;; `c-state-cache'. This is JUST AFTER an opening or closing
;; brace/paren/bracket which is already in `c-state-cache' or just before
;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
;; before `here''s line, or the start of the literal containing it.
;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
;; preceding POS which isn't recorded in `c-state-cache'. It is a position
;; to scan backwards from.
;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
;; POS and HERE which aren't recorded in `c-state-cache'.
;;
;; The comments in this defun use "paren" to mean parenthesis or square
;; bracket (as contrasted with a brace), and "(" and ")" likewise.
;;
;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
;; | | | | | |
;; CP E here D C good
(let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
(pos c-state-cache-good-pos)
pa ren ; positions of "(" and ")"
dropped-cons ; whether the last element dropped from `c-state-cache'
; was a cons (representing a brace-pair)
good-pos ; see above.
lit ; (START . END) of a literal containing some point.
here-lit-start here-lit-end ; bounds of literal containing `here'
; or `here' itself.
here- here+ ; start/end of macro around HERE, or HERE
(here-bol (c-point 'bol here))
(too-far-back (max (- here c-state-cache-too-far) (point-min))))
;; Remove completely irrelevant entries from `c-state-cache'.
(while (and c-state-cache
(>= (setq pa (c-state-cache-top-lparen)) here))
(setq dropped-cons (consp (car c-state-cache)))
(setq c-state-cache (cdr c-state-cache))
(setq pos pa))
;; At this stage, (>= pos here);
;; (< (c-state-cache-top-lparen) here) (or is nil).
(cond
((and (consp (car c-state-cache))
(> (cdar c-state-cache) here))
;; CASE 1: The top of the cache is a brace pair which now encloses
;; `here'. As good-pos, return the address of the "{". Since we've no
;; knowledge of what's inside these braces, we have no alternative but
;; to direct the caller to scan the buffer from the opening brace.
(setq pos (caar c-state-cache))
(setq c-state-cache (cons pos (cdr c-state-cache)))
(list (1+ pos) pos t)) ; return value. We've just converted a brace pair
; entry into a { entry, so the caller needs to
; search for a brace pair before the {.
;; `here' might be inside a literal. Check for this.
((progn
(setq lit (c-state-literal-at here)
here-lit-start (or (car lit) here)
here-lit-end (or (cdr lit) here))
;; Has `here' just "newly entered" a macro?
(save-excursion
(goto-char here-lit-start)
(if (and (c-beginning-of-macro)
(or (null c-state-old-cpp-beg)
(not (= (point) c-state-old-cpp-beg))))
(progn
(setq here- (point))
(c-end-of-macro)
(setq here+ (point)))
(setq here- here-lit-start
here+ here-lit-end)))
;; `here' might be nested inside any depth of parens (or brackets but
;; not braces). Scan backwards to find the outermost such opening
;; paren, if there is one. This will be the scan position to return.
(save-restriction
(narrow-to-region cache-pos (point-max))
(setq pos (c-state-balance-parens-backwards here- here+ pos)))
nil)) ; for the cond
((< pos here-lit-start)
;; CASE 2: Address of outermost ( or [ which now encloses `here', but
;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
;; a brace pair preceding this, it will already be in `c-state-cache',
;; unless there was a brace pair after it, i.e. there'll only be one to
;; scan for if we've just deleted one.
(list pos (and dropped-cons pos) t)) ; Return value.
;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
;; Further forward scanning isn't needed, but we still need to find a
;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
((progn
(save-restriction
(narrow-to-region here-bol (point-max))
(setq pos here-lit-start)
(c-safe (while (setq pa (c-sc-scan-lists pos -1 1))
(setq pos pa)))) ; might signal
nil)) ; for the cond
((save-restriction
(narrow-to-region too-far-back (point-max))
(setq ren (c-safe (c-sc-scan-lists pos -1 -1))))
;; CASE 3: After a }/)/] before `here''s BOL.
(list (1+ ren) (and dropped-cons pos) nil)) ; Return value
((progn (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
(>= cache-pos good-pos))
;; CASE 3.5: Just after an existing entry in `c-state-cache' on `here''s
;; line or the previous line.
(list cache-pos nil nil))
(t
;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
;; literal containing it.
(list good-pos (and dropped-cons good-pos) nil)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Externally visible routines.
(defun c-state-cache-init ()
(setq c-state-cache nil
c-state-cache-good-pos 1
c-state-nonlit-pos-cache nil
c-state-nonlit-pos-cache-limit 1
c-state-brace-pair-desert nil
c-state-point-min 1
c-state-point-min-lit-type nil
c-state-point-min-lit-start nil
c-state-min-scan-pos 1
c-state-old-cpp-beg nil
c-state-old-cpp-end nil)
(c-state-mark-point-min-literal))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Debugging routines to dump `c-state-cache' in a "replayable" form.
;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
;; (defun c-state-dump ()
;; ;; For debugging.
;; ;(message
;; (concat
;; (c-sc-qde c-state-cache)
;; (c-sc-de c-state-cache-good-pos)
;; (c-sc-qde c-state-nonlit-pos-cache)
;; (c-sc-de c-state-nonlit-pos-cache-limit)
;; (c-sc-qde c-state-brace-pair-desert)
;; (c-sc-de c-state-point-min)
;; (c-sc-de c-state-point-min-lit-type)
;; (c-sc-de c-state-point-min-lit-start)
;; (c-sc-de c-state-min-scan-pos)
;; (c-sc-de c-state-old-cpp-beg)
;; (c-sc-de c-state-old-cpp-end)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c-invalidate-state-cache-1 (here)
;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
;; left in a consistent state.
;;
;; This is much like `c-whack-state-after', but it never changes a paren
;; pair element into an open paren element. Doing that would mean that the
;; new open paren wouldn't have the required preceding paren pair element.
;;
;; This function is called from c-before-change.
;; The caches of non-literals:
;; Note that we use "<=" for the possibility of the second char of a two-char
;; comment opener being typed; this would invalidate any cache position at
;; HERE.
(if (<= here c-state-nonlit-pos-cache-limit)
(setq c-state-nonlit-pos-cache-limit (1- here)))
(c-truncate-lit-pos-cache here)
;; `c-state-cache':
;; Case 1: if `here' is in a literal containing point-min, everything
;; becomes (or is already) nil.
(if (or (null c-state-cache-good-pos)
(< here (c-state-get-min-scan-pos)))
(setq c-state-cache nil
c-state-cache-good-pos nil
c-state-min-scan-pos nil)
;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
;; below `here'. To maintain its consistency, we may need to insert a new
;; brace pair.
(let ((here-bol (c-point 'bol here))
too-high-pa ; recorded {/(/[ next above or just below here, or nil.
dropped-cons) ; was the last removed element a brace pair?
;; The easy bit - knock over-the-top bits off `c-state-cache'.
(while (and c-state-cache
(>= (c-state-cache-top-paren) here))
(setq dropped-cons (consp (car c-state-cache))
too-high-pa (c-state-cache-top-lparen)
c-state-cache (cdr c-state-cache)))
;; Do we need to add in an earlier brace pair, having lopped one off?
(if (and dropped-cons
(<= too-high-pa here))
(c-append-lower-brace-pair-to-state-cache too-high-pa here here-bol))
(if (and c-state-cache-good-pos (< here c-state-cache-good-pos))
(setq c-state-cache-good-pos
(or (save-excursion
(goto-char here)
(c-literal-start))
here)))))
;; The brace-pair desert marker:
(when (car c-state-brace-pair-desert)
(if (< here (car c-state-brace-pair-desert))
(setq c-state-brace-pair-desert nil)
(if (< here (cdr c-state-brace-pair-desert))
(setcdr c-state-brace-pair-desert here)))))
(defun c-parse-state-1 ()
;; Find and record all noteworthy parens between some good point earlier in
;; the file and point. That good point is at least the beginning of the
;; top-level construct we are in, or the beginning of the preceding
;; top-level construct if we aren't in one.
;;
;; The returned value is a list of the noteworthy parens with the last one
;; first. If an element in the list is an integer, it's the position of an
;; open paren (of any type) which has not been closed before the point. If
;; an element is a cons, it gives the position of a closed BRACE paren
;; pair[*]; the car is the start brace position and the cdr is the position
;; following the closing brace. Only the last closed brace paren pair
;; before each open paren and before the point is recorded, and thus the
;; state never contains two cons elements in succession. When a close brace
;; has no matching open brace (e.g., the matching brace is outside the
;; visible region), it is not represented in the returned value.
;;
;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
;; This defun explicitly treats mismatching parens/braces/brackets as
;; matching. It is the open brace which makes it a "brace" pair.
;;
;; If POINT is within a macro, open parens and brace pairs within
;; THIS macro MIGHT be recorded. This depends on whether their
;; syntactic properties have been suppressed by
;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
;;
;; Currently no characters which are given paren syntax with the
;; syntax-table property are recorded, i.e. angle bracket arglist
;; parens are never present here. Note that this might change.
;;
;; BUG: This function doesn't cope entirely well with unbalanced
;; parens in macros. (2008-12-11: this has probably been resolved
;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
;; following case the brace before the macro isn't balanced with the
;; one after it:
;;
;; {
;; #define X {
;; }
;;
;; Note to maintainers: this function DOES get called with point
;; within comments and strings, so don't assume it doesn't!
;;
;; This function might do hidden buffer changes.
(let* ((here (point))
(here-bopl (c-point 'bopl))
strategy ; 'forward, 'backward etc..
;; Candidate positions to start scanning from:
cache-pos ; highest position below HERE already existing in
; cache (or 1).
good-pos
start-point ; (when scanning forward) a place below HERE where there
; are no open parens/braces between it and HERE.
bopl-state
res
cons-separated
scan-backward-pos scan-forward-p) ; used for 'backward.
;; If POINT-MIN has changed, adjust the cache
(unless (= (point-min) c-state-point-min)
(c-renarrow-state-cache))
;; Strategy?
(setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
strategy (car res)
start-point (cadr res))
(when (eq strategy 'BOD)
(setq c-state-cache nil
c-state-cache-good-pos start-point))
;; SCAN!
(cond
((memq strategy '(forward back-and-forward BOD))
(setq res (c-remove-stale-state-cache start-point here here-bopl))
(setq cache-pos (car res)
scan-backward-pos (cadr res)
cons-separated (car (cddr res))
bopl-state (cadr (cddr res))) ; will be nil if (< here-bopl
; start-point)
(if (and scan-backward-pos
(or cons-separated (eq strategy 'forward))) ;scan-backward-pos
(c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
(setq good-pos
(c-append-to-state-cache cache-pos here))
(setq c-state-cache-good-pos
(if (and bopl-state
(< good-pos (- here c-state-cache-too-far)))
(c-state-cache-lower-good-pos here here-bopl bopl-state)
good-pos)))
((eq strategy 'backward)
(setq res (c-remove-stale-state-cache-backwards here)
good-pos (car res)
scan-backward-pos (cadr res)
scan-forward-p (car (cddr res)))
(if scan-backward-pos
(c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
(setq c-state-cache-good-pos
(if scan-forward-p
(c-append-to-state-cache good-pos here)
good-pos)))
(t ; (eq strategy 'IN-LIT)
(setq c-state-cache nil
c-state-cache-good-pos nil))))
c-state-cache)
(defun c-invalidate-state-cache (here)
;; This is a wrapper over `c-invalidate-state-cache-1'.
;;
;; It suppresses the syntactic effect of the < and > (template) brackets and
;; of all parens in preprocessor constructs, except for any such construct
;; containing point. We can then call `c-invalidate-state-cache-1' without
;; worrying further about macros and template delimiters.
(if (eval-when-compile (memq 'category-properties c-emacs-features))
;; Emacs
(c-with-<->-as-parens-suppressed
(c-invalidate-state-cache-1 here))
;; XEmacs
(c-invalidate-state-cache-1 here)))
(defmacro c-state-maybe-marker (place marker)
;; If PLACE is non-nil, return a marker marking it, otherwise nil.
;; We (re)use MARKER.
`(let ((-place- ,place))
(and -place-
(or ,marker (setq ,marker (make-marker)))
(set-marker ,marker -place-))))
(defun c-parse-state ()
;; This is a wrapper over `c-parse-state-1'. See that function for a
;; description of the functionality and return value.
;;
;; It suppresses the syntactic effect of the < and > (template) brackets and
;; of all parens in preprocessor constructs, except for any such construct
;; containing point. We can then call `c-parse-state-1' without worrying
;; further about macros and template delimiters.
(let (here-cpp-beg here-cpp-end)
(save-excursion
(when (c-beginning-of-macro)
(setq here-cpp-beg (point))
(unless
(> (setq here-cpp-end (c-syntactic-end-of-macro))
here-cpp-beg)
(setq here-cpp-beg nil here-cpp-end nil))))
;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
;; subsystem.
(prog1
(if (eval-when-compile (memq 'category-properties c-emacs-features))
;; Emacs
(c-with-<->-as-parens-suppressed
(c-parse-state-1))
;; XEmacs
(c-parse-state-1))
(setq c-state-old-cpp-beg
(c-state-maybe-marker here-cpp-beg c-state-old-cpp-beg-marker)
c-state-old-cpp-end
(c-state-maybe-marker here-cpp-end c-state-old-cpp-end-marker)))))
;; Debug tool to catch cache inconsistencies. This is called from
;; 000tests.el.
(defvar c-debug-parse-state nil)
(unless (fboundp 'c-real-parse-state)
(fset 'c-real-parse-state (symbol-function 'c-parse-state)))
(cc-bytecomp-defun c-real-parse-state)
(defvar c-parse-state-point nil)
(defvar c-parse-state-state nil)
(make-variable-buffer-local 'c-parse-state-state)
(defun c-record-parse-state-state ()
(setq c-parse-state-point (point))
(when (markerp (cdr (assq 'c-state-old-cpp-beg c-parse-state-state)))
(move-marker (cdr (assq 'c-state-old-cpp-beg c-parse-state-state)) nil)
(move-marker (cdr (assq 'c-state-old-cpp-end c-parse-state-state)) nil))
(setq c-parse-state-state
(mapcar
(lambda (arg)
(let ((val (symbol-value arg)))
(cons arg
(cond ((consp val) (copy-tree val))
((markerp val) (copy-marker val))
(t val)))))
'(c-state-cache
c-state-cache-good-pos
c-state-nonlit-pos-cache
c-state-nonlit-pos-cache-limit
c-state-brace-pair-desert
c-state-point-min
c-state-point-min-lit-type
c-state-point-min-lit-start
c-state-min-scan-pos
c-state-old-cpp-beg
c-state-old-cpp-end
c-parse-state-point))))
(defun c-replay-parse-state-state ()
(message "%s"
(concat "(setq "
(mapconcat
(lambda (arg)
(format "%s %s%s" (car arg)
(if (atom (cdr arg)) "" "'")
(if (markerp (cdr arg))
(format "(copy-marker %s)" (marker-position (cdr arg)))
(cdr arg))))
c-parse-state-state " ")
")")))
(defun c-debug-parse-state-double-cons (state)
(let (state-car conses-not-ok)
(while state
(setq state-car (car state)
state (cdr state))
(if (and (consp state-car)
(consp (car state)))
(setq conses-not-ok t)))
conses-not-ok))
(defun c-debug-parse-state ()
(let ((here (point)) (min-point (point-min)) (res1 (c-real-parse-state)) res2)
(let ((c-state-cache nil)
(c-state-cache-good-pos 1)
(c-state-nonlit-pos-cache nil)
(c-state-nonlit-pos-cache-limit 1)
(c-state-brace-pair-desert nil)
(c-state-point-min 1)
(c-state-point-min-lit-type nil)
(c-state-point-min-lit-start nil)
(c-state-min-scan-pos 1)
(c-state-old-cpp-beg nil)
(c-state-old-cpp-end nil))
(setq res2 (c-real-parse-state)))
(unless (equal res1 res2)
;; The cache can actually go further back due to the ad-hoc way
;; the first paren is found, so try to whack off a bit of its
;; start before complaining.
;; (save-excursion
;; (goto-char (or (c-least-enclosing-brace res2) (point)))
;; (c-beginning-of-defun-1)
;; (while (not (or (bobp) (eq (char-after) ?{)))
;; (c-beginning-of-defun-1))
;; (unless (equal (c-whack-state-before (point) res1) res2)
;; (message (concat "c-parse-state inconsistency at %s: "
;; "using cache: %s, from scratch: %s")
;; here res1 res2)))
(message (concat "c-parse-state inconsistency at %s: "
"using cache: %s, from scratch: %s. POINT-MIN: %s")
here res1 res2 min-point)
(message "Old state:")
(c-replay-parse-state-state))
(when (c-debug-parse-state-double-cons res1)
(message "c-parse-state INVALIDITY at %s: %s"
here res1)
(message "Old state:")
(c-replay-parse-state-state))
(c-record-parse-state-state)
res2 ; res1 correct a cascading series of errors ASAP
))
(defun c-toggle-parse-state-debug (&optional arg)
(interactive "P")
(setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
(fset 'c-parse-state (symbol-function (if c-debug-parse-state
'c-debug-parse-state
'c-real-parse-state)))
(c-keep-region-active)
(message "c-debug-parse-state %sabled"
(if c-debug-parse-state "en" "dis")))
(when c-debug-parse-state
(c-toggle-parse-state-debug 1))
\f
(defun c-whack-state-before (bufpos paren-state)
;; Whack off any state information from PAREN-STATE which lies
;; before BUFPOS. Not destructive on PAREN-STATE.
(let* ((newstate (list nil))
(ptr newstate)
car)
(while paren-state
(setq car (car paren-state)
paren-state (cdr paren-state))
(if (< (if (consp car) (car car) car) bufpos)
(setq paren-state nil)
(setcdr ptr (list car))
(setq ptr (cdr ptr))))
(cdr newstate)))
(defun c-whack-state-after (bufpos paren-state)
;; Whack off any state information from PAREN-STATE which lies at or
;; after BUFPOS. Not destructive on PAREN-STATE.
(catch 'done
(while paren-state
(let ((car (car paren-state)))
(if (consp car)
;; just check the car, because in a balanced brace
;; expression, it must be impossible for the corresponding
;; close brace to be before point, but the open brace to
;; be after.
(if (<= bufpos (car car))
nil ; whack it off
(if (< bufpos (cdr car))
;; its possible that the open brace is before
;; bufpos, but the close brace is after. In that
;; case, convert this to a non-cons element. The
;; rest of the state is before bufpos, so we're
;; done.
(throw 'done (cons (car car) (cdr paren-state)))
;; we know that both the open and close braces are
;; before bufpos, so we also know that everything else
;; on state is before bufpos.
(throw 'done paren-state)))
(if (<= bufpos car)
nil ; whack it off
;; it's before bufpos, so everything else should too.
(throw 'done paren-state)))
(setq paren-state (cdr paren-state)))
nil)))
(defun c-most-enclosing-brace (paren-state &optional bufpos)
;; Return the bufpos of the innermost enclosing open paren before
;; bufpos, or nil if none was found.
(let (enclosingp)
(or bufpos (setq bufpos 134217727))
(while paren-state
(setq enclosingp (car paren-state)
paren-state (cdr paren-state))
(if (or (consp enclosingp)
(>= enclosingp bufpos))
(setq enclosingp nil)
(setq paren-state nil)))
enclosingp))
(defun c-least-enclosing-brace (paren-state)
;; Return the bufpos of the outermost enclosing open paren, or nil
;; if none was found.
(let (pos elem)
(while paren-state
(setq elem (car paren-state)
paren-state (cdr paren-state))
(if (integerp elem)
(setq pos elem)))
pos))
(defun c-safe-position (bufpos paren-state)
;; Return the closest "safe" position recorded on PAREN-STATE that
;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
;; contain any. Return nil if BUFPOS is nil, which is useful to
;; find the closest limit before a given limit that might be nil.
;;
;; A "safe" position is a position at or after a recorded open
;; paren, or after a recorded close paren. The returned position is
;; thus either the first position after a close brace, or the first
;; position after an enclosing paren, or at the enclosing paren in
;; case BUFPOS is immediately after it.
(when bufpos
(let (elem)
(catch 'done
(while paren-state
(setq elem (car paren-state))
(if (consp elem)
(cond ((< (cdr elem) bufpos)
(throw 'done (cdr elem)))
((< (car elem) bufpos)
;; See below.
(throw 'done (min (1+ (car elem)) bufpos))))
(if (< elem bufpos)
;; elem is the position at and not after the opening paren, so
;; we can go forward one more step unless it's equal to
;; bufpos. This is useful in some cases avoid an extra paren
;; level between the safe position and bufpos.
(throw 'done (min (1+ elem) bufpos))))
(setq paren-state (cdr paren-state)))))))
(defun c-beginning-of-syntax ()
;; This is used for `font-lock-beginning-of-syntax-function'. It
;; goes to the closest previous point that is known to be outside
;; any string literal or comment. `c-state-cache' is used if it has
;; a position in the vicinity.
(let* ((paren-state c-state-cache)
elem
(pos (catch 'done
;; Note: Similar code in `c-safe-position'. The
;; difference is that we accept a safe position at
;; the point and don't bother to go forward past open
;; parens.
(while paren-state
(setq elem (car paren-state))
(if (consp elem)
(cond ((<= (cdr elem) (point))
(throw 'done (cdr elem)))
((<= (car elem) (point))
(throw 'done (car elem))))
(if (<= elem (point))
(throw 'done elem)))
(setq paren-state (cdr paren-state)))
(point-min))))
(if (> pos (- (point) 4000))
(goto-char pos)
;; The position is far back. Try `c-beginning-of-defun-1'
;; (although we can't be entirely sure it will go to a position
;; outside a comment or string in current emacsen). FIXME:
;; Consult `syntax-ppss' here.
(c-beginning-of-defun-1)
(if (< (point) pos)
(goto-char pos)))))
\f
;; Tools for scanning identifiers and other tokens.
(defun c-on-identifier ()
"Return non-nil if the point is on or directly after an identifier.
Keywords are recognized and not considered identifiers. If an
identifier is detected, the returned value is its starting position.
If an identifier ends at the point and another begins at it (can only
happen in Pike) then the point for the preceding one is returned.
Note that this function might do hidden buffer changes. See the
comment at the start of cc-engine.el for more info."
;; FIXME: Shouldn't this function handle "operator" in C++?
(save-excursion
(skip-syntax-backward "w_")
(or
;; Check for a normal (non-keyword) identifier.
(and (looking-at c-symbol-start)
(not (looking-at c-keywords-regexp))
(point))
(when (c-major-mode-is 'pike-mode)
;; Handle the `<operator> syntax in Pike.
(let ((pos (point)))
(skip-chars-backward "-!%&*+/<=>^|~[]()")
(and (if (< (skip-chars-backward "`") 0)
t
(goto-char pos)
(eq (char-after) ?\`))
(looking-at c-symbol-key)
(>= (match-end 0) pos)
(point))))
;; Handle the "operator +" syntax in C++.
(when (and c-overloadable-operators-regexp
(= (c-backward-token-2 0) 0))
(cond ((and (looking-at c-overloadable-operators-regexp)
(or (not c-opt-op-identifier-prefix)
(and (= (c-backward-token-2 1) 0)
(looking-at c-opt-op-identifier-prefix))))
(point))
((save-excursion
(and c-opt-op-identifier-prefix
(looking-at c-opt-op-identifier-prefix)
(= (c-forward-token-2 1) 0)
(looking-at c-overloadable-operators-regexp)))
(point))))
)))
(defsubst c-simple-skip-symbol-backward ()
;; If the point is at the end of a symbol then skip backward to the
;; beginning of it. Don't move otherwise. Return non-nil if point
;; moved.
;;
;; This function might do hidden buffer changes.
(or (< (skip-syntax-backward "w_") 0)
(and (c-major-mode-is 'pike-mode)
;; Handle the `<operator> syntax in Pike.
(let ((pos (point)))
(if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
(< (skip-chars-backward "`") 0)
(looking-at c-symbol-key)
(>= (match-end 0) pos))
t
(goto-char pos)
nil)))))
(defun c-beginning-of-current-token (&optional back-limit)
;; Move to the beginning of the current token. Do not move if not
;; in the middle of one. BACK-LIMIT may be used to bound the
;; backward search; if given it's assumed to be at the boundary
;; between two tokens. Return non-nil if the point is moved, nil
;; otherwise.
;;
;; This function might do hidden buffer changes.
(let ((start (point)))
(if (looking-at "\\w\\|\\s_")
(skip-syntax-backward "w_" back-limit)
(when (< (skip-syntax-backward ".()" back-limit) 0)
(while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
(match-end 0))
;; `c-nonsymbol-token-regexp' should always match
;; since we've skipped backward over punctuation
;; or paren syntax, but consume one char in case
;; it doesn't so that we don't leave point before
;; some earlier incorrect token.
(1+ (point)))))
(if (<= pos start)
(goto-char pos))))))
(< (point) start)))
(defun c-end-of-token (&optional back-limit)
;; Move to the end of the token we're just before or in the middle of.
;; BACK-LIMIT may be used to bound the backward search; if given it's
;; assumed to be at the boundary between two tokens. Return non-nil if the
;; point is moved, nil otherwise.
;;
;; This function might do hidden buffer changes.
(let ((start (point)))
(cond ;; ((< (skip-syntax-backward "w_" (1- start)) 0)
;; (skip-syntax-forward "w_"))
((> (skip-syntax-forward "w_") 0))
((< (skip-syntax-backward ".()" back-limit) 0)
(while (< (point) start)
(if (looking-at c-nonsymbol-token-regexp)
(goto-char (match-end 0))
;; `c-nonsymbol-token-regexp' should always match since
;; we've skipped backward over punctuation or paren
;; syntax, but move forward in case it doesn't so that
;; we don't leave point earlier than we started with.
(forward-char))))
(t (if (looking-at c-nonsymbol-token-regexp)
(goto-char (match-end 0)))))
(> (point) start)))
(defun c-end-of-current-token (&optional back-limit)
;; Move to the end of the current token. Do not move if not in the
;; middle of one. BACK-LIMIT may be used to bound the backward
;; search; if given it's assumed to be at the boundary between two
;; tokens. Return non-nil if the point is moved, nil otherwise.
;;
;; This function might do hidden buffer changes.
(let ((start (point)))
(cond ((< (skip-syntax-backward "w_" (1- start)) 0)
(skip-syntax-forward "w_"))
((< (skip-syntax-backward ".()" back-limit) 0)
(while (progn
(if (looking-at c-nonsymbol-token-regexp)
(goto-char (match-end 0))
;; `c-nonsymbol-token-regexp' should always match since
;; we've skipped backward over punctuation or paren
;; syntax, but move forward in case it doesn't so that
;; we don't leave point earlier than we started with.
(forward-char))
(< (point) start)))))
(> (point) start)))
(defconst c-jump-syntax-balanced
(if (memq 'gen-string-delim c-emacs-features)
"\\w\\|\\s_\\|\\s(\\|\\s)\\|\\s\"\\|\\s|"
"\\w\\|\\s_\\|\\s(\\|\\s)\\|\\s\""))
(defconst c-jump-syntax-unbalanced
(if (memq 'gen-string-delim c-emacs-features)
"\\w\\|\\s_\\|\\s\"\\|\\s|"
"\\w\\|\\s_\\|\\s\""))
(defun c-forward-over-token (&optional balanced)
"Move forward over a token.
Return t if we moved, nil otherwise (i.e. we were at EOB, or a
non-token or BALANCED is non-nil and we can't move). If we
are at syntactic whitespace, move over this in place of a token.
If BALANCED is non-nil move over any balanced parens we are at, and never move
out of an enclosing paren."
(let ((jump-syntax (if balanced
c-jump-syntax-balanced
c-jump-syntax-unbalanced))
(here (point)))
(condition-case nil
(cond
((/= (point)
(progn (c-forward-syntactic-ws) (point)))
;; If we're at whitespace, count this as the token.
t)
((eobp) nil)
((looking-at jump-syntax)
(goto-char (scan-sexps (point) 1))
t)
((looking-at c-nonsymbol-token-regexp)
(goto-char (match-end 0))
t)
((save-restriction
(widen)
(looking-at c-nonsymbol-token-regexp))
nil)
(t
(forward-char)
t))
(error (goto-char here)
nil))))
(defun c-forward-over-token-and-ws (&optional balanced)
"Move forward over a token and any following whitespace
Return t if we moved, nil otherwise (i.e. we were at EOB, or a
non-token or BALANCED is non-nil and we can't move). If we
are at syntactic whitespace, move over this in place of a token.
If BALANCED is non-nil move over any balanced parens we are at, and never move
out of an enclosing paren.
This function differs from `c-forward-token-2' in that it will move forward
over the final token in a buffer, up to EOB."
(prog1 (c-forward-over-token balanced)
(c-forward-syntactic-ws)))
(defun c-forward-token-2 (&optional count balanced limit)
"Move forward by tokens.
A token is defined as all symbols and identifiers which aren't
syntactic whitespace (note that multicharacter tokens like \"==\" are
treated properly). Point is always either left at the beginning of a
token or not moved at all. COUNT specifies the number of tokens to
move; a negative COUNT moves in the opposite direction. A COUNT of 0
moves to the next token beginning only if not already at one. If
BALANCED is true, move over balanced parens, otherwise move into them.
Also, if BALANCED is true, never move out of an enclosing paren.
LIMIT sets the limit for the movement and defaults to the point limit.
The case when LIMIT is set in the middle of a token, comment or macro
is handled correctly, i.e. the point won't be left there.
Return the number of tokens left to move (positive or negative). If
BALANCED is true, a move over a balanced paren counts as one. Note
that if COUNT is 0 and no appropriate token beginning is found, 1 will
be returned. Thus, a return value of 0 guarantees that point is at
the requested position and a return value less (without signs) than
COUNT guarantees that point is at the beginning of some token.
Note that this function might do hidden buffer changes. See the
comment at the start of cc-engine.el for more info."
(or count (setq count 1))
(if (< count 0)
(- (c-backward-token-2 (- count) balanced limit))
(let ((here (point))
(last (point)))
(when (zerop count)
;; If count is zero we should jump if in the middle of a token.
(c-end-of-current-token))
(save-restriction
(if limit (narrow-to-region (point-min) limit))
(if (/= (point)
(progn (c-forward-syntactic-ws) (point)))
;; Skip whitespace. Count this as a move if we did in
;; fact move.
(setq count (max (1- count) 0)))
(if (eobp)
;; Moved out of bounds. Make sure the returned count isn't zero.
(progn
(if (zerop count) (setq count 1))
(goto-char here))
(while (and
(> count 0)
(c-forward-over-token-and-ws balanced)
(not (eobp)))
(setq last (point)
count (1- count)))
(if (eobp)
(goto-char last))))
count)))
(defun c-backward-token-2 (&optional count balanced limit)
"Move backward by tokens.
See `c-forward-token-2' for details."
(or count (setq count 1))
(if (< count 0)
(- (c-forward-token-2 (- count) balanced limit))
(or limit (setq limit (point-min)))
(let ((jump-syntax (if balanced
c-jump-syntax-balanced
c-jump-syntax-unbalanced))
(last (point)))
(if (zerop count)
;; The count is zero so try to skip to the beginning of the
;; current token.
(if (> (point)
(progn (c-beginning-of-current-token) (point)))
(if (< (point) limit)
;; The limit is inside the same token, so return 1.
(setq count 1))
;; We're not in the middle of a token. If there's
;; whitespace after the point then we must move backward,
;; so set count to 1 in that case.
(and (looking-at c-syntactic-ws-start)
;; If we're looking at a '#' that might start a cpp
;; directive then we have to do a more elaborate check.
(or (/= (char-after) ?#)
(not c-opt-cpp-prefix)
(save-excursion
(and (= (point)
(progn (beginning-of-line)
(looking-at "[ \t]*")
(match-end 0)))
(or (bobp)
(progn (backward-char)
(not (eq (char-before) ?\\)))))))
(setq count 1))))
;; Use `condition-case' to avoid having to check for buffer
;; limits in `backward-char', `scan-sexps' and `goto-char' below.
(condition-case nil
(while (and
(> count 0)
(progn
(c-backward-syntactic-ws)
(backward-char)
(if (looking-at jump-syntax)
(goto-char (scan-sexps (1+ (point)) -1))
;; This can be very inefficient if there's a long
;; sequence of operator tokens without any separation.
;; That doesn't happen in practice, anyway.
(c-beginning-of-current-token))
(>= (point) limit)))
(setq last (point)
count (1- count)))
(error (goto-char last)))
(if (< (point) limit)
(goto-char last))
count)))
(defun c-forward-token-1 (&optional count balanced limit)
"Like `c-forward-token-2' but doesn't treat multicharacter operator
tokens like \"==\" as single tokens, i.e. all sequences of symbol
characters are jumped over character by character. This function is
for compatibility only; it's only a wrapper over `c-forward-token-2'."
(let ((c-nonsymbol-token-regexp "\\s."))
(c-forward-token-2 count balanced limit)))
(defun c-backward-token-1 (&optional count balanced limit)
"Like `c-backward-token-2' but doesn't treat multicharacter operator
tokens like \"==\" as single tokens, i.e. all sequences of symbol
characters are jumped over character by character. This function is
for compatibility only; it's only a wrapper over `c-backward-token-2'."
(let ((c-nonsymbol-token-regexp "\\s."))
(c-backward-token-2 count balanced limit)))
\f
;; Tools for doing searches restricted to syntactically relevant text.
(defun c-syntactic-re-search-forward (regexp &optional bound noerror
paren-level not-inside-token
lookbehind-submatch)
"Like `re-search-forward', but only report matches that are found
in syntactically significant text. I.e. matches in comments, macros
or string literals are ignored. The start point is assumed to be
outside any comment, macro or string literal, or else the content of
that region is taken as syntactically significant text.
NOERROR, in addition to the values nil, t, and <anything else>
used in `re-search-forward' can also take the values
`before-literal' and `after-literal'. In these cases, when BOUND
is also given and is inside a literal, and a search fails, point
will be left, respectively before or after the literal. Be aware
that with `after-literal', if a string or comment is unclosed at
the end of the buffer, point may be left there, even though it is
inside a literal there.
If PAREN-LEVEL is non-nil, an additional restriction is added to
ignore matches in nested paren sexps. The search will also not go
outside the current list sexp, which has the effect that if the point
should be moved to BOUND when no match is found (i.e. NOERROR is
neither nil nor t), then it will be at the closing paren if the end of
the current list sexp is encountered first.
If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
ignored. Things like multicharacter operators and special symbols
\(e.g. \"`()\" in Pike) are handled but currently not floating point
constants.
If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
subexpression in REGEXP. The end of that submatch is used as the
position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
isn't used or if that subexpression didn't match then the start
position of the whole match is used instead. The \"look behind\"
subexpression is never tested before the starting position, so it
might be a good idea to include \\=\\= as a match alternative in it.
Optimization note: Matches might be missed if the \"look behind\"
subexpression can match the end of nonwhite syntactic whitespace,
i.e. the end of comments or cpp directives. This since the function
skips over such things before resuming the search. It's on the other
hand not safe to assume that the \"look behind\" subexpression never
matches syntactic whitespace.
Bug: Unbalanced parens inside cpp directives are currently not handled
correctly (i.e. they don't get ignored as they should) when
PAREN-LEVEL is set.
Note that this function might do hidden buffer changes. See the
comment at the start of cc-engine.el for more info."
(or bound (setq bound (point-max)))
(if paren-level (setq paren-level -1))
;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
(let ((start (point))
tmp
;; Start position for the last search.
search-pos
;; The `parse-partial-sexp' state between the start position
;; and the point.
state
;; The current position after the last state update. The next
;; `parse-partial-sexp' continues from here.
(state-pos (point))
;; The position at which to check the state and the state
;; there. This is separate from `state-pos' since we might
;; need to back up before doing the next search round.
check-pos check-state
;; Last position known to end a token.
(last-token-end-pos (point-min))
;; Set when a valid match is found.
found)
(condition-case err
(while
(and
(progn
(setq search-pos (point))
(if (re-search-forward regexp bound noerror)
t
;; Without the following, when PAREN-LEVEL is non-nil, and
;; NOERROR is not nil or t, and the very first search above
;; has just failed, point would end up at BOUND rather than
;; just before the next close paren.
(when (and (eq search-pos start)
paren-level
(not (memq noerror '(nil t))))
(setq state (parse-partial-sexp start bound -1))
(if (eq (car state) -1)
(setq bound (1- (point)))))
nil))
(progn
(setq state (parse-partial-sexp
state-pos (match-beginning 0) paren-level nil state)
state-pos (point))
(if (setq check-pos (and lookbehind-submatch
(or (not paren-level)
(>= (car state) 0))
(match-end lookbehind-submatch)))
(setq check-state (parse-partial-sexp
state-pos check-pos paren-level nil state))
(setq check-pos state-pos
check-state state))
;; NOTE: If we got a look behind subexpression and get
;; an insignificant match in something that isn't
;; syntactic whitespace (i.e. strings or in nested
;; parentheses), then we can never skip more than a
;; single character from the match start position
;; (i.e. `state-pos' here) before continuing the
;; search. That since the look behind subexpression
;; might match the end of the insignificant region in
;; the next search.
(cond
((elt check-state 7)
;; Match inside a line comment. Skip to eol. Use
;; `re-search-forward' instead of `skip-chars-forward' to get
;; the right bound behavior.
(re-search-forward "[\n\r]" bound noerror))
((elt check-state 4)
;; Match inside a block comment. Skip to the '*/'.
(search-forward "*/" bound noerror))
((and (not (elt check-state 5))
(eq (char-before check-pos) ?/)
(not (c-get-char-property (1- check-pos) 'syntax-table))
(memq (char-after check-pos) '(?/ ?*)))
;; Match in the middle of the opener of a block or line
;; comment.
(if (= (char-after check-pos) ?/)
(re-search-forward "[\n\r]" bound noerror)
(search-forward "*/" bound noerror)))
;; The last `parse-partial-sexp' above might have
;; stopped short of the real check position if the end
;; of the current sexp was encountered in paren-level
;; mode. The checks above are always false in that
;; case, and since they can do better skipping in
;; lookbehind-submatch mode, we do them before
;; checking the paren level.
((and paren-level
(/= (setq tmp (car check-state)) 0))
;; Check the paren level first since we're short of the
;; syntactic checking position if the end of the
;; current sexp was encountered by `parse-partial-sexp'.
(if (> tmp 0)
;; Inside a nested paren sexp.
(if lookbehind-submatch
;; See the NOTE above.
(progn (goto-char state-pos) t)
;; Skip out of the paren quickly.
(setq state (parse-partial-sexp state-pos bound 0 nil state)
state-pos (point)))
;; Have exited the current paren sexp.
(if noerror
(progn
;; The last `parse-partial-sexp' call above
;; has left us just after the closing paren
;; in this case, so we can modify the bound
;; to leave the point at the right position
;; upon return.
(setq bound (1- (point)))
nil)
(signal 'search-failed (list regexp)))))
((setq tmp (elt check-state 3))
;; Match inside a string.
(if (or lookbehind-submatch
(not (integerp tmp)))
;; See the NOTE above.
(progn (goto-char state-pos) t)
;; Skip to the end of the string before continuing.
(let ((ender (make-string 1 tmp)) (continue t))
(while (if (search-forward ender bound noerror)
(progn
(setq state (parse-partial-sexp
state-pos (point) nil nil state)
state-pos (point))
(elt state 3))
(setq continue nil)))
continue)))
((save-excursion
(save-match-data
(c-beginning-of-macro start)))
;; Match inside a macro. Skip to the end of it.
(c-end-of-macro)
(cond ((<= (point) bound) t)
(noerror nil)
(t (signal 'search-failed (list regexp)))))
((and not-inside-token
(or (< check-pos last-token-end-pos)
(< check-pos
(save-excursion
(goto-char check-pos)
(save-match-data
(c-end-of-current-token last-token-end-pos))
(setq last-token-end-pos (point))))))
;; Inside a token.
(if lookbehind-submatch
;; See the NOTE above.
(goto-char state-pos)
(goto-char (min last-token-end-pos bound))))
(t
;; A real match.
(setq found t)
nil)))
;; Should loop to search again, but take care to avoid
;; looping on the same spot.
(or (/= search-pos (point))
(if (= (point) bound)
(if noerror
nil
(signal 'search-failed (list regexp)))
(forward-char)
t))))
(error
(goto-char start)
(signal (car err) (cdr err))))
;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
(if found
(progn
(goto-char (match-end 0))
(match-end 0))
;; Search failed. Set point as appropriate.
(cond
((eq noerror t)
(goto-char start))
((not (memq noerror '(before-literal after-literal)))
(goto-char bound))
(t (setq state (parse-partial-sexp state-pos bound nil nil state))
(if (or (elt state 3) (elt state 4))
(if (eq noerror 'before-literal)
(goto-char (elt state 8))
(parse-partial-sexp bound (point-max) nil nil
state 'syntax-table))
(goto-char bound))))
nil)))
(defvar safe-pos-list) ; bound in c-syntactic-skip-backward
(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
"Like `skip-chars-backward' but only look at syntactically relevant chars,
i.e. don't stop at positions inside syntactic whitespace or string
literals. Preprocessor directives are also ignored, with the exception
of the one that the point starts within, if any. If LIMIT is given,
it's assumed to be at a syntactically relevant position.
If PAREN-LEVEL is non-nil, the function won't stop in nested paren
sexps, and the search will also not go outside the current paren sexp.
However, if LIMIT or the buffer limit is reached inside a nested paren
then the point will be left at the limit.
Non-nil is returned if the point moved, nil otherwise.
Note that this function might do hidden buffer changes. See the
comment at the start of cc-engine.el for more info."
(let* ((start (point))
;; The result from `c-beginning-of-macro' at the start position or the
;; start position itself if it isn't within a macro.
(start-macro-beg
(save-excursion
(goto-char start)
(c-beginning-of-macro limit)
(point)))
lit-beg
;; The earliest position after the current one with the same paren
;; level. Used only when `paren-level' is set.
(paren-level-pos (point))
;; Whether we can optimize with an early `c-backward-syntactic-ws'.
(opt-ws (string-match "^\\^[^ \t\n\r]+$" skip-chars)))
;; In the next while form, we only loop when `skip-chars' is something
;; like "^/" and we've stopped at the end of a block comment.
(while
(progn
;; The next loop "tries" to find the end point each time round,
;; loops when it's ended up at the wrong level of nesting.
(while
(and
;; Optimize for, in particular, large blocks of comments from
;; `comment-region'.
(progn (when opt-ws
(let ((opt-pos (point)))
(c-backward-syntactic-ws limit)
(if (> (point) limit)
(setq paren-level-pos (point))
(goto-char opt-pos))))
t)
;; Move back to a candidate end point which isn't in a literal
;; or in a macro we didn't start in.
(let ((pos (point))
macro-start)
(while (and
(< (skip-chars-backward skip-chars limit) 0)
(or
(when (setq lit-beg (c-literal-start))
(goto-char lit-beg)
t)
;; Don't stop inside a macro we didn't start in.
(when
(save-excursion
(and (c-beginning-of-macro limit)
(< (point) start-macro-beg)
(setq macro-start (point))))
(goto-char macro-start))))
(when opt-ws
(let ((opt-pos (point)))
(c-backward-syntactic-ws limit)
(if (<= (point) limit)
(goto-char opt-pos)))))
(< (point) pos))
;; Check whether we're at the wrong level of nesting (when
;; `paren-level' is non-nil).
(let ((pos (point)) state-2 pps-end-pos)
(when
(and paren-level
(save-excursion
(setq state-2 (parse-partial-sexp
pos paren-level-pos -1)
pps-end-pos (point))
(/= (car state-2) 0)))
;; Not at the right level.
(if (and (< (car state-2) 0)
;; We stop above if we go out of a paren.
;; Now check whether it precedes or is
;; nested in the starting sexp.
(save-excursion
(setq state-2
(parse-partial-sexp
pps-end-pos paren-level-pos
nil nil state-2))
(< (car state-2) 0)))
;; We've stopped short of the starting position
;; so the hit was inside a nested list. Go up
;; until we are at the right level.
(condition-case nil
(progn
(goto-char (scan-lists pos -1
(- (car state-2))))
(setq paren-level-pos (point))
(if (and limit (>= limit paren-level-pos))
(progn
(goto-char limit)
nil)
t))
(error
(goto-char (or limit (point-min)))
nil))
;; The hit was outside the list at the start
;; position. Go to the start of the list and exit.
(goto-char (1+ (elt state-2 1)))
nil)))))
(> (point)
(progn
;; Skip syntactic ws afterwards so that we don't stop at the
;; end of a comment if `skip-chars' is something like "^/".
(c-backward-syntactic-ws)
(point)))))
;; We might want to extend this with more useful return values in
;; the future.
(/= (point) start)))
;; The following is an alternative implementation of
;; `c-syntactic-skip-backward' that uses backward movement to keep
;; track of the syntactic context. It turned out to be generally
;; slower than the one above which uses forward checks from earlier
;; safe positions.
;;
;;(defconst c-ssb-stop-re
;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
;; ;; stop at to avoid going into comments and literals.
;; (concat
;; ;; Match comment end syntax and string literal syntax. Also match
;; ;; '/' for block comment endings (not covered by comment end
;; ;; syntax).
;; "\\s>\\|/\\|\\s\""
;; (if (memq 'gen-string-delim c-emacs-features)
;; "\\|\\s|"
;; "")
;; (if (memq 'gen-comment-delim c-emacs-features)
;; "\\|\\s!"
;; "")))
;;
;;(defconst c-ssb-stop-paren-re
;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
;;
;;(defconst c-ssb-sexp-end-re
;; ;; Regexp matching the ending syntax of a complex sexp.
;; (concat c-string-limit-regexp "\\|\\s)"))
;;
;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
;;i.e. don't stop at positions inside syntactic whitespace or string
;;literals. Preprocessor directives are also ignored. However, if the
;;point is within a comment, string literal or preprocessor directory to
;;begin with, its contents is treated as syntactically relevant chars.
;;If LIMIT is given, it limits the backward search and the point will be
;;left there if no earlier position is found.
;;
;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
;;sexps, and the search will also not go outside the current paren sexp.
;;However, if LIMIT or the buffer limit is reached inside a nested paren
;;then the point will be left at the limit.
;;
;;Non-nil is returned if the point moved, nil otherwise.
;;
;;Note that this function might do hidden buffer changes. See the
;;comment at the start of cc-engine.el for more info."
;;
;; (save-restriction
;; (when limit
;; (narrow-to-region limit (point-max)))
;;
;; (let ((start (point)))
;; (catch 'done
;; (while (let ((last-pos (point))
;; (stop-pos (progn
;; (skip-chars-backward skip-chars)
;; (point))))
;;
;; ;; Skip back over the same region as
;; ;; `skip-chars-backward' above, but keep to
;; ;; syntactically relevant positions.
;; (goto-char last-pos)
;; (while (and
;; ;; `re-search-backward' with a single char regexp
;; ;; should be fast.
;; (re-search-backward
;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
;; stop-pos 'move)
;;
;; (progn
;; (cond
;; ((looking-at "\\s(")
;; ;; `paren-level' is set and we've found the
;; ;; start of the containing paren.
;; (forward-char)
;; (throw 'done t))
;;
;; ((looking-at c-ssb-sexp-end-re)
;; ;; We're at the end of a string literal or paren
;; ;; sexp (if `paren-level' is set).
;; (forward-char)
;; (condition-case nil
;; (c-backward-sexp)
;; (error
;; (goto-char limit)
;; (throw 'done t))))
;;
;; (t
;; (forward-char)
;; ;; At the end of some syntactic ws or possibly
;; ;; after a plain '/' operator.
;; (let ((pos (point)))
;; (c-backward-syntactic-ws)
;; (if (= pos (point))
;; ;; Was a plain '/' operator. Go past it.
;; (backward-char)))))
;;
;; (> (point) stop-pos))))
;;
;; ;; Now the point is either at `stop-pos' or at some
;; ;; position further back if `stop-pos' was at a
;; ;; syntactically irrelevant place.
;;
;; ;; Skip additional syntactic ws so that we don't stop
;; ;; at the end of a comment if `skip-chars' is
;; ;; something like "^/".
;; (c-backward-syntactic-ws)
;;
;; (< (point) stop-pos))))
;;
;; ;; We might want to extend this with more useful return values
;; ;; in the future.
;; (/= (point) start))))
\f
;; Tools for handling comments and string literals.
(defun c-in-literal (&optional _lim detect-cpp)
"Return the type of literal point is in, if any.
The return value is `c' if in a C-style comment, `c++' if in a C++
style comment, `string' if in a string literal, `pound' if DETECT-CPP
is non-nil and in a preprocessor line, or nil if somewhere else.
Optional LIM is used as the backward limit of the search. If omitted,
or nil, `c-beginning-of-defun' is used.
Note that this function might do hidden buffer changes. See the
comment at the start of cc-engine.el for more info."
(save-restriction
(widen)
(let ((lit (c-semi-pp-to-literal (point))))
(or (cadr lit)
(and detect-cpp
(save-excursion (c-beginning-of-macro))
'pound)))))
(defun c-literal-limits (&optional lim near not-in-delimiter)
"Return a cons of the beginning and end positions of the comment or
string surrounding point (including both delimiters), or nil if point
isn't in one. If LIM is non-nil, it's used as the \"safe\" position
to start parsing from. If NEAR is non-nil, then the limits of any
literal next to point is returned. \"Next to\" means there's only
spaces and tabs between point and the literal. The search for such a
literal is done first in forward direction. If NOT-IN-DELIMITER is
non-nil, the case when point is inside a starting delimiter won't be
recognized. This only has effect for comments which have starting
delimiters with more than one character.
Note that this function might do hidden buffer changes. See the
comment at the start of cc-engine.el for more info."
(save-excursion
(let*
((pos (point))
(lit-limits
(if lim
(let ((s (parse-partial-sexp lim (point))))
(when (or (nth 3 s)
(and (nth 4 s) (not (eq (nth 7 s) 'syntax-table))))
(cons (nth 8 s)
(progn (parse-partial-sexp (point) (point-max)
nil nil
s
'syntax-table)
(point)))))
(let* ((pp-to-lit (c-full-pp-to-literal pos not-in-delimiter))
(limits (car (cddr pp-to-lit))))
(if (and limits (null (cdr limits)))
(cons (car limits) (point-max))
limits)))))
(cond
(lit-limits)
(near
(goto-char pos)
;; Search forward for a literal.
(skip-chars-forward " \t")
(cond
((looking-at c-string-limit-regexp) ; String.
(cons (point) (or (c-safe (c-forward-sexp 1) (point))
(point-max))))
((looking-at c-comment-start-regexp) ; Line or block comment.
(cons (point) (progn (c-forward-single-comment) (point))))
(t
;; Search backward.
(skip-chars-backward " \t")
(let ((end (point)) beg)
(cond
((save-excursion
(< (skip-syntax-backward c-string-syntax) 0)) ; String.
(setq beg (c-safe (c-backward-sexp 1) (point))))
((and (c-safe (forward-char -2) t)
(looking-at "\\*/"))
;; Block comment. Due to the nature of line
;; comments, they will always be covered by the
;; normal case above.
(goto-char end)
(c-backward-single-comment)
;; If LIM is bogus, beg will be bogus.
(setq beg (point))))
(if beg (cons beg end))))))
))))
(defun c-literal-start (&optional safe-pos)
"Return the start of the string or comment surrounding point, or nil if
point isn't in one. SAFE-POS, if non-nil, is a position before point which is
a known \"safe position\", i.e. outside of any string or comment."
(if safe-pos
(let ((s (parse-partial-sexp safe-pos (point))))
(and (or (nth 3 s)
(and (nth 4 s) (not (eq (nth 7 s) 'syntax-table))))
(nth 8 s)))
(car (cddr (c-semi-pp-to-literal (point))))))
;; In case external callers use this; it did have a docstring.
(defalias 'c-literal-limits-fast 'c-literal-limits)
(defun c-collect-line-comments (range)
"If the argument is a cons of two buffer positions (such as returned by
`c-literal-limits'), and that range contains a C++ style line comment,
then an extended range is returned that contains all adjacent line
comments (i.e. all comments that starts in the same column with no
empty lines or non-whitespace characters between them). Otherwise the
argument is returned.
Note that this function might do hidden buffer changes. See the
comment at the start of cc-engine.el for more info."
(save-excursion
(condition-case nil
(if (and (consp range) (progn
(goto-char (car range))
(looking-at c-line-comment-starter)))
(let ((col (current-column))
(beg (point))
(bopl (c-point 'bopl))
(end (cdr range)))
;; Got to take care in the backward direction to handle
;; comments which are preceded by code.
(while (and (c-backward-single-comment)
(>= (point) bopl)
(looking-at c-line-comment-starter)
(= col (current-column)))
(setq beg (point)
bopl (c-point 'bopl)))
(goto-char end)
(while (and (progn (skip-chars-forward " \t")
(looking-at c-line-comment-starter))
(= col (current-column))
(prog1 (zerop (forward-line 1))
(setq end (point)))))
(cons beg end))
range)
(error range))))
(defun c-literal-type (range)
"Convenience function that given the result of `c-literal-limits',
returns nil or the type of literal that the range surrounds, one
of the symbols `c', `c++' or `string'. It's much faster than using
`c-in-literal' and is intended to be used when you need both the
type of a literal and its limits.
Note that this function might do hidden buffer changes. See the
comment at the start of cc-engine.el for more info."
(if (consp range)
(save-excursion
(goto-char (car range))
(cond ((looking-at c-string-limit-regexp) 'string)
((or (looking-at "//") ; c++ line comment
(and (looking-at "\\s<") ; comment starter
(looking-at "#"))) ; awk comment.
'c++)
(t 'c))) ; Assuming the range is valid.
range))
(defsubst c-determine-limit-get-base (start try-size)
;; Get a "safe place" approximately TRY-SIZE characters before START.
;; This defsubst doesn't preserve point.
(goto-char start)
(let* ((pos (max (- start try-size) (point-min)))
(s (c-semi-pp-to-literal pos))
(cand (or (car (cddr s)) pos)))
(if (>= cand (point-min))
cand
(parse-partial-sexp pos start nil nil (car s) 'syntax-table)
(point))))
(defun c-determine-limit (how-far-back &optional start try-size)
;; Return a buffer position approximately HOW-FAR-BACK non-literal
;; characters from START (default point). The starting position, either
;; point or START may not be in a comment or string.
;;
;; The position found will not be before POINT-MIN and won't be in a
;; literal.
;;
;; We start searching for the sought position TRY-SIZE (default
;; twice HOW-FAR-BACK) bytes back from START.
;;
;; This function must be fast. :-)
(save-excursion
(let* ((start (or start (point)))
(try-size (or try-size (* 2 how-far-back)))
(base (c-determine-limit-get-base start try-size))
(pos base)
(s (parse-partial-sexp pos pos)) ; null state.
stack elt size
(count 0))
;; Optimization for large blocks of comments, particularly those being
;; created by `comment-region'.
(goto-char pos)
(forward-comment try-size)
(setq pos (point))
(while (< pos start)
;; Move forward one literal each time round this loop.
;; Move forward to the start of a comment or string.
(setq s (parse-partial-sexp
pos
start
nil ; target-depth
nil ; stop-before
s ; state
'syntax-table)) ; stop-comment
;; Gather details of the non-literal-bit - starting pos and size.
(setq size (- (if (or (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table)))
(nth 3 s))
(nth 8 s)
(point))
pos))
(if (> size 0)
(setq stack (cons (cons pos size) stack)))
;; Move forward to the end of the comment/string.
(if (or (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table)))
(nth 3 s))
(setq s (parse-partial-sexp
(point)
start
nil ; target-depth
nil ; stop-before
s ; state
'syntax-table))) ; stop-comment
(setq pos (point)))
;; Now try and find enough non-literal characters recorded on the stack.
;; Go back one recorded literal each time round this loop.
(while (and (< count how-far-back)
stack)
(setq elt (car stack)
stack (cdr stack))
(setq count (+ count (cdr elt))))
;; Have we found enough yet?
(cond
((null elt) ; No non-literal characters found.
(if (> base (point-min))
(c-determine-limit how-far-back base (* 2 try-size))
(point-min)))
((>= count how-far-back)
(+ (car elt) (- count how-far-back)))
((eq base (point-min))
(point-min))
((> base (- start try-size)) ; Can only happen if we hit point-min.
(car elt))
(t
(c-determine-limit (- how-far-back count) base (* 2 try-size)))))))
(defun c-determine-+ve-limit (how-far &optional start-pos)
;; Return a buffer position about HOW-FAR non-literal characters forward
;; from START-POS (default point), which must not be inside a literal.
(save-excursion
(let ((pos (or start-pos (point)))
(count how-far)
(s (parse-partial-sexp (point) (point)))) ; null state
(goto-char pos)
(while (and (not (eobp))
(> count 0))
;; Scan over counted characters.
(setq s (parse-partial-sexp
pos
(min (+ pos count) (point-max))
nil ; target-depth
nil ; stop-before
s ; state
'syntax-table)) ; stop-comment
(setq count (- count (- (point) pos) 1)
pos (point))
;; Scan over literal characters.
(if (nth 8 s)
(setq s (parse-partial-sexp
pos
(point-max)
nil ; target-depth
nil ; stop-before
s ; state
'syntax-table) ; stop-comment
pos (point))))
(point))))
\f
;; `c-find-decl-spots' and accompanying stuff.
;; Variables used in `c-find-decl-spots' to cache the search done for
;; the first declaration in the last call. When that function starts,
;; it needs to back up over syntactic whitespace to look at the last
;; token before the region being searched. That can sometimes cause
;; moves back and forth over a quite large region of comments and
;; macros, which would be repeated for each changed character when
;; we're called during fontification, since font-lock refontifies the
;; current line for each change. Thus it's worthwhile to cache the
;; first match.
;;
;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
;; the syntactic whitespace less or equal to some start position.
;; There's no cached value if it's nil.
;;
;; `c-find-decl-match-pos' is the match position if
;; `c-find-decl-prefix-search' matched before the syntactic whitespace
;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
(defvar c-find-decl-syntactic-pos nil)
(make-variable-buffer-local 'c-find-decl-syntactic-pos)
(defvar c-find-decl-match-pos nil)
(make-variable-buffer-local 'c-find-decl-match-pos)
(defsubst c-invalidate-find-decl-cache (change-min-pos)
(and c-find-decl-syntactic-pos
(< change-min-pos c-find-decl-syntactic-pos)
(setq c-find-decl-syntactic-pos nil)))
; (defface c-debug-decl-spot-face
; '((t (:background "Turquoise")))
; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
; (defface c-debug-decl-sws-face
; '((t (:background "Khaki")))
; "Debug face to mark the syntactic whitespace between the declaration
; spots and the preceding token end.")
(defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
(when (facep 'c-debug-decl-spot-face)
`(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
(c-debug-add-face (max match-pos (point-min)) decl-pos
'c-debug-decl-sws-face)
(c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
'c-debug-decl-spot-face))))
(defmacro c-debug-remove-decl-spot-faces (beg end)
(when (facep 'c-debug-decl-spot-face)
`(c-save-buffer-state ()
(c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
(c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Machinery for determining when we're at top level (this including being
;; directly inside a class or namespace, etc.)
;;
;; We maintain a stack of brace depths in structures like classes and
;; namespaces. The car of this structure, when non-nil, indicates that the
;; associated position is within a template (etc.) structure, and the value is
;; the position where the (outermost) template ends. The other elements in
;; the structure are stacked elements, one each for each enclosing "top level"
;; structure.
;;
;; At the very outermost level, the value of the stack would be (nil 1), the
;; "1" indicating an enclosure in a notional all-enclosing block. After
;; passing a keyword such as "namespace", the value would become (nil 0 1).
;; At this point, passing a semicolon would cause the 0 to be dropped from the
;; stack (at any other time, a semicolon is ignored). Alternatively, on
;; passing an opening brace, the stack would become (nil 1 1). Each opening
;; brace passed causes the cadr to be incremented, and passing closing braces
;; causes it to be decremented until it reaches 1. On passing a closing brace
;; when the cadr of the stack is at 1, this causes it to be removed from the
;; stack, the corresponding namespace (etc.) structure having been closed.
;;
;; There is a special stack value -1 which means the C++ colon operator
;; introducing a list of inherited classes has just been parsed. The value
;; persists on the stack until the next open brace or semicolon.
;;
;; When the car of the stack is non-nil, i.e. when we're in a template (etc.)
;; structure, braces are not counted. The counting resumes only after passing
;; the template's closing position, which is recorded in the car of the stack.
;;
;; The test for being at top level consists of the cadr being 0 or 1.
;;
;; The values of this stack throughout a buffer are cached in a simple linear
;; cache, every 5000 characters.
;;
;; Note to maintainers: This cache mechanism is MUCH faster than recalculating
;; the stack at every entry to `c-find-decl-spots' using `c-at-toplevel-p' or
;; the like.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The approximate interval at which we cache the value of the brace stack.
(defconst c-bs-interval 5000)
;; The list of cached values of the brace stack. Each value in the list is a
;; cons of the position it is valid for and the value of the stack as
;; described above.
(defvar c-bs-cache nil)
(make-variable-buffer-local 'c-bs-cache)
;; The position of the buffer at and below which entries in `c-bs-cache' are
;; valid.
(defvar c-bs-cache-limit 1)
(make-variable-buffer-local 'c-bs-cache-limit)
;; The previous buffer position for which the brace stack value was
;; determined.
(defvar c-bs-prev-pos most-positive-fixnum)
(make-variable-buffer-local 'c-bs-prev-pos)
;; The value of the brace stack at `c-bs-prev-pos'.
(defvar c-bs-prev-stack nil)
(make-variable-buffer-local 'c-bs-prev-stack)
(defun c-init-bs-cache ()
;; Initialize the cache in `c-bs-cache' and related variables.
(setq c-bs-cache nil
c-bs-cache-limit 1
c-bs-prev-pos most-positive-fixnum
c-bs-prev-stack nil))
(defun c-truncate-bs-cache (pos &rest _ignore)
;; Truncate the upper bound of the cache `c-bs-cache' to POS, if it is
;; higher than that position. This is called as either a before- or
;; after-change-function.
(setq c-bs-cache-limit
(min c-bs-cache-limit pos)))
(defvar c-restricted-<>-arglists) ;FIXME: Move definition here?
(defvar c-parse-and-markup-<>-arglists) ;FIXME: Move definition here?
(defun c-update-brace-stack (stack from to)
;; Given a brace-stack which has the value STACK at position FROM, update it
;; to its value at position TO, where TO is after (or equal to) FROM.
;; Return a cons of either TO (if it is outside a literal) and this new
;; value, or of the next position after TO outside a literal and the new
;; value.
(let (match kwd-sym (prev-match-pos 1)
(s (cdr stack))
(bound-<> (car stack)))
(save-excursion
(cond
((and bound-<> (<= to bound-<>))
(goto-char to)) ; Nothing to do.
(bound-<>
(goto-char bound-<>)
(setq bound-<> nil))
(t (goto-char from)))
(while (and (< (point) to)
(c-syntactic-re-search-forward
(if (<= (car s) 0)
c-brace-stack-thing-key
c-brace-stack-no-semi-key)
to 'after-literal)
(> (point) prev-match-pos)) ; prevent infinite loop.
(setq prev-match-pos (point))
(setq match (match-string-no-properties 1)
kwd-sym (c-keyword-sym match))
(cond
((and (equal match "{")
(progn (backward-char)
(prog1 (looking-at "\\s(")
(forward-char))))
(setq s (if s
(cons (if (<= (car s) 0)
1
(1+ (car s)))
(cdr s))
(list 1))))
((and (equal match "}")
(progn (backward-char)
(prog1 (looking-at "\\s)")
(forward-char))))
(setq s
(cond
((and s (> (car s) 1))
(cons (1- (car s)) (cdr s)))
((and (cdr s) (eq (car s) 1))
(cdr s))
(t s))))
((and (equal match "<")
(progn (backward-char)
(prog1 (looking-at "\\s(")
(forward-char))))
(backward-char)
(if (let ((c-parse-and-markup-<>-arglists t)
(c-restricted-<>-arglists t))
(c-forward-<>-arglist nil)) ; Should always work.
(when (> (point) to)
(setq bound-<> (point)))
(forward-char)))
((and (equal match ":")
s
(eq (car s) 0))
(setq s (cons -1 (cdr s))))
((and (equal match ",")
(eq (car s) -1))) ; at "," in "class foo : bar, ..."
((member match '(";" "," ")"))
(when (and s (cdr s) (<= (car s) 0))
(setq s (cdr s))))
((c-keyword-member kwd-sym 'c-flat-decl-block-kwds)
(push 0 s))))
;; The failing `c-syntactic-re-search-forward' may have left us in the
;; middle of a token, which might be a significant token. Fix this!
(c-beginning-of-current-token)
(cons (point)
(cons bound-<> s)))))
(defun c-brace-stack-at (here)
;; Given a buffer position HERE, Return the value of the brace stack there.
(save-excursion
(save-restriction
(widen)
(let ((c c-bs-cache)
(can-use-prev (<= c-bs-prev-pos c-bs-cache-limit))
elt stack pos npos high-elt)
;; Trim the cache to take account of buffer changes.
(while (and c
(> (caar c) c-bs-cache-limit))
(setq c (cdr c)))
(setq c-bs-cache c)
(while (and c
(> (caar c) here))
(setq high-elt (car c))
(setq c (cdr c)))
(setq pos (or (and c (caar c))
(point-min)))
(setq elt (if c
(car c)
(cons (point-min)
(cons nil (list 1)))))
(when (not high-elt)
(setq stack (cdr elt))
(while
;; Add an element to `c-bs-cache' each iteration.
(<= (setq npos (+ pos c-bs-interval)) here)
(setq elt (c-update-brace-stack stack pos npos))
(setq npos (car elt))
(setq stack (cdr elt))
(unless (eq npos (point-max)) ; NPOS could be in a literal at EOB.
(setq c-bs-cache (cons elt c-bs-cache)))
(setq pos npos)))
(if (> pos c-bs-cache-limit)
(setq c-bs-cache-limit pos))
;; Can we just use the previous value?
(if (and can-use-prev
(<= c-bs-prev-pos here)
(> c-bs-prev-pos (car elt)))
(setq pos c-bs-prev-pos
stack c-bs-prev-stack)
(setq pos (car elt)
stack (cdr elt)))
(if (> here c-bs-cache-limit)
(setq c-bs-cache-limit here))
(setq elt (c-update-brace-stack stack pos here)
c-bs-prev-pos (car elt)
c-bs-prev-stack (cdr elt))))))
(defun c-bs-at-toplevel-p (here)
;; Is position HERE at the top level, as indicated by the brace stack?
(let ((stack (c-brace-stack-at here)))
(or (null stack) ; Probably unnecessary.
(<= (cadr stack) 1))))
(defmacro c-find-decl-prefix-search ()
;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
;; but it contains lots of free variables that refer to things
;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
;; if there is a match, otherwise at `cfd-limit'.
;;
;; The macro moves point forward to the next putative start of a declaration
;; or cfd-limit. This decl start is the next token after a "declaration
;; prefix". The declaration prefix is the earlier of `cfd-prop-match' and
;; `cfd-re-match'. `cfd-match-pos' is set to the decl prefix.
;;
;; This macro might do hidden buffer changes.
'(progn
;; Find the next property match position if we haven't got one already.
(unless cfd-prop-match
(save-excursion
(while (progn
(goto-char (c-next-single-property-change
(point) 'c-type nil cfd-limit))
(and (< (point) cfd-limit)
(not (eq (c-get-char-property (1- (point)) 'c-type)
'c-decl-end)))))
(setq cfd-prop-match (point))))
;; Find the next `c-decl-prefix-or-start-re' match if we haven't
;; got one already.
(unless cfd-re-match
(if (> cfd-re-match-end (point))
(goto-char cfd-re-match-end))
;; Each time round, the next `while' moves forward over a pseudo match
;; of `c-decl-prefix-or-start-re' which is either inside a literal, or
;; is a ":" not preceded by "public", etc.. `cfd-re-match' and
;; `cfd-re-match-end' get set.
(while
(progn
(setq cfd-re-match-end (re-search-forward c-decl-prefix-or-start-re
cfd-limit 'move))
(cond
((null cfd-re-match-end)
;; No match. Finish up and exit the loop.
(setq cfd-re-match cfd-limit)
nil)
((c-got-face-at
(if (setq cfd-re-match
(or (match-end 1)
(and c-dposr-cpp-macro-depth
(match-end (1+ c-dposr-cpp-macro-depth)))))
;; Matched the end of a token preceding a decl spot.
(progn
(goto-char cfd-re-match)
(1- cfd-re-match))
;; Matched a token that start a decl spot.
(goto-char (match-beginning 0))
(point))
c-literal-faces)
;; Pseudo match inside a comment or string literal. Skip out
;; of comments and string literals.
(while
(progn
(unless
(and
(or (match-end 1)
(and c-dposr-cpp-macro-depth
(match-end (1+ c-dposr-cpp-macro-depth))))
(c-got-face-at (1- (point)) c-literal-faces)
(not (c-got-face-at (point) c-literal-faces)))
(goto-char (c-next-single-property-change
(point) 'face nil cfd-limit)))
(and (< (point) cfd-limit)
(c-got-face-at (point) c-literal-faces))))
t) ; Continue the loop over pseudo matches.
((and c-opt-identifier-concat-key
(match-string 1)
(save-excursion
(goto-char (match-beginning 1))
(save-match-data
(looking-at c-opt-identifier-concat-key))))
;; Found, e.g., "::" in C++
t)
((and (match-string 1)
(string= (match-string 1) ":")
(save-excursion
(or (/= (c-backward-token-2 2) 0) ; no search limit. :-(
(not (looking-at c-decl-start-colon-kwd-re)))))
;; Found a ":" which isn't part of "public:", etc.
t)
(t nil)))) ;; Found a real match. Exit the pseudo-match loop.
;; If our match was at the decl start, we have to back up over the
;; preceding syntactic ws to set `cfd-match-pos' and to catch
;; any decl spots in the syntactic ws.
(unless cfd-re-match
(let ((cfd-cbsw-lim (- (point) 1000)))
(c-backward-syntactic-ws cfd-cbsw-lim)
(setq cfd-re-match
(if (> (point) cfd-cbsw-lim)
(point)
0))) ; Set BOB case if the token's too far back.
))
;; Choose whichever match is closer to the start.
(if (< cfd-re-match cfd-prop-match)
(setq cfd-match-pos cfd-re-match
cfd-re-match nil)
(setq cfd-match-pos cfd-prop-match
cfd-prop-match nil))
(setq cfd-top-level (c-bs-at-toplevel-p cfd-match-pos))
(goto-char cfd-match-pos)
(when (< cfd-match-pos cfd-limit)
;; Skip forward past comments only so we don't skip macros.
(while
(progn
(c-forward-comments)
;; The following is of use within a doc comment when a doc
;; comment style has removed face properties from a construct,
;; and is relying on `c-font-lock-declarations' to add them
;; again.
(cond
((looking-at c-noise-macro-name-re)
(c-forward-noise-clause-not-macro-decl nil)) ; Returns t.
((looking-at c-noise-macro-with-parens-name-re)
(c-forward-noise-clause-not-macro-decl t)) ; Always returns t.
((and (< (point) cfd-limit)
(looking-at c-doc-line-join-re))
(goto-char (match-end 0))))))
;; Set the position to continue at. We can avoid going over
;; the comments skipped above a second time, but it's possible
;; that the comment skipping has taken us past `cfd-prop-match'
;; since the property might be used inside comments.
(setq cfd-continue-pos (if cfd-prop-match
(min cfd-prop-match (point))
(point))))))
(defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
;; Call CFD-FUN for each possible spot for a declaration, cast or
;; label from the point to CFD-LIMIT.
;;
;; CFD-FUN is called with point at the start of the spot. It's passed three
;; arguments: The first is the end position of the token preceding the spot,
;; or 0 for the implicit match at bob. The second is a flag that is t when
;; the match is inside a macro. The third is a flag that is t when the
;; match is at "top level", i.e. outside any brace block, or directly inside
;; a class or namespace, etc. Point should be moved forward by at least one
;; token.
;;
;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
;; it should return non-nil to ensure that the next search will find them.
;;
;; Such a spot is:
;; o The first token after bob.
;; o The first token after the end of submatch 1 in
;; `c-decl-prefix-or-start-re' when that submatch matches. This
;; submatch is typically a (L or R) brace or paren, a ;, or a ,.
;; As a special case, noise macros are skipped over and the next
;; token regarded as the spot.
;; o The start of each `c-decl-prefix-or-start-re' match when
;; submatch 1 doesn't match. This is, for example, the keyword
;; "class" in Pike.
;; o The start of a previously recognized declaration; "recognized"
;; means that the last char of the previous token has a `c-type'
;; text property with the value `c-decl-end'; this only holds
;; when `c-type-decl-end-used' is set.
;;
;; Only a spot that match CFD-DECL-RE and whose face is in the
;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
;; check is disabled if CFD-FACE-CHECKLIST is nil.
;;
;; If the match is inside a macro then the buffer is narrowed to the
;; end of it, so that CFD-FUN can investigate the following tokens
;; without matching something that begins inside a macro and ends
;; outside it. It's to avoid this work that the CFD-DECL-RE and
;; CFD-FACE-CHECKLIST checks exist.
;;
;; The spots are visited approximately in order from top to bottom.
;; It's however the positions where `c-decl-prefix-or-start-re'
;; matches and where `c-decl-end' properties are found that are in
;; order. Since the spots often are at the following token, they
;; might be visited out of order insofar as more spots are reported
;; later on within the syntactic whitespace between the match
;; positions and their spots.
;;
;; It's assumed that comments and strings are fontified in the
;; searched range.
;;
;; This is mainly used in fontification, and so has an elaborate
;; cache to handle repeated calls from the same start position; see
;; the variables above.
;;
;; All variables in this function begin with `cfd-' to avoid name
;; collision with the (dynamically bound) variables used in CFD-FUN.
;;
;; This function might do hidden buffer changes.
(let ((cfd-start-pos (point)) ; never changed
(cfd-buffer-end (point-max))
;; The end of the token preceding the decl spot last found
;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
;; no match.
cfd-re-match
;; The end position of the last `c-decl-prefix-or-start-re'
;; match. If this is greater than `cfd-continue-pos', the
;; next regexp search is started here instead.
(cfd-re-match-end (point-min))
;; The end of the last `c-decl-end' found by
;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
;; match. If searching for the property isn't needed then we
;; disable it by setting it to `cfd-limit' directly.
(cfd-prop-match (unless c-type-decl-end-used cfd-limit))
;; The end of the token preceding the decl spot last found by
;; `c-find-decl-prefix-search'. 0 for the implicit match at
;; bob. `cfd-limit' if there's no match. In other words,
;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
(cfd-match-pos cfd-limit)
;; The position to continue searching at.
cfd-continue-pos
;; The position of the last "real" token we've stopped at.
;; This can be greater than `cfd-continue-pos' when we get
;; hits inside macros or at `c-decl-end' positions inside
;; comments.
(cfd-token-pos 0)
;; The end position of the last entered macro.
(cfd-macro-end 0)
;; Whether the last position returned from `c-find-decl-prefix-search'
;; is at the top-level (including directly in a class or namespace,
;; etc.).
(cfd-top-level (c-bs-at-toplevel-p (point))))
;; Initialize by finding a syntactically relevant start position
;; before the point, and do the first `c-decl-prefix-or-start-re'
;; search unless we're at bob.
(let (start-in-literal start-in-macro syntactic-pos hash-define-pos)
;; Must back up a bit since we look for the end of the previous
;; statement or declaration, which is earlier than the first
;; returned match.
;; This `cond' moves back over any literals or macros. It has special
;; handling for when the region being searched is entirely within a
;; macro. It sets `cfd-continue-pos' (unless we've reached
;; `cfd-limit').
(cond
;; First we need to move to a syntactically relevant position.
;; Begin by backing out of comment or string literals.
;;
;; This arm of the cond actually triggers if we're in a literal,
;; and cfd-limit is at most at BONL.
((and
;; This arm of the `and' moves backwards out of a literal when
;; the face at point is a literal face. In this case, its value
;; is always non-nil.
(when (c-got-face-at (point) c-literal-faces)
;; Try to use the faces to back up to the start of the
;; literal. FIXME: What if the point is on a declaration
;; inside a comment?
(while (and (not (bobp))
(c-got-face-at (1- (point)) c-literal-faces))
(goto-char (previous-single-property-change
(point) 'face nil (point-min))))
;; XEmacs doesn't fontify the quotes surrounding string
;; literals.
(and (featurep 'xemacs)
(eq (get-text-property (point) 'face)
'font-lock-string-face)
(not (bobp))
(progn (backward-char)
(not (looking-at c-string-limit-regexp)))
(forward-char))
;; Don't trust the literal to contain only literal faces
;; (the font lock package might not have fontified the
;; start of it at all, for instance) so check that we have
;; arrived at something that looks like a start or else
;; resort to `c-literal-limits'.
(unless (looking-at c-literal-start-regexp)
(let ((lit-start (c-literal-start)))
(if lit-start (goto-char lit-start)))
)
(setq start-in-literal (point))) ; end of `and' arm.
;; The start is in a literal. If the limit is in the same
;; one we don't have to find a syntactic position etc. We
;; only check that if the limit is at or before bonl to save
;; time; it covers the by far most common case when font-lock
;; refontifies the current line only.
(<= cfd-limit (c-point 'bonl cfd-start-pos))
(save-excursion
(goto-char cfd-start-pos)
(while (progn
(goto-char (c-next-single-property-change
(point) 'face nil cfd-limit))
(and (< (point) cfd-limit)
(c-got-face-at (point) c-literal-faces))))
(= (point) cfd-limit))) ; end of `cond' arm condition
;; Completely inside a literal. Set up variables to trig the
;; (< cfd-continue-pos cfd-start-pos) case below and it'll
;; find a suitable start position.
(setq cfd-continue-pos start-in-literal)) ; end of `cond' arm
;; Check if the region might be completely inside a macro, to
;; optimize that like the completely-inside-literal above.
((save-excursion
(and (= (forward-line 1) 0)
(bolp) ; forward-line has funny behavior at eob.
(>= (point) cfd-limit)
(progn (backward-char)
(eq (char-before) ?\\))))
;; (Maybe) completely inside a macro. Only need to trig the
;; (< cfd-continue-pos cfd-start-pos) case below to make it
;; set things up.
(setq cfd-continue-pos (1- cfd-start-pos)
start-in-macro t))
;; The default arm of the `cond' moves back over any macro we're in
;; and over any syntactic WS. It sets `c-find-decl-syntactic-pos'.
(t
;; Back out of any macro so we don't miss any declaration
;; that could follow after it.
(when (c-beginning-of-macro)
(setq start-in-macro t))
;; Now we're at a proper syntactically relevant position so we
;; can use the cache. But first clear it if it applied
;; further down.
(c-invalidate-find-decl-cache cfd-start-pos)
(setq syntactic-pos (point))
(unless
(or (eq syntactic-pos c-find-decl-syntactic-pos)
(null c-find-decl-syntactic-pos)
(< c-find-decl-syntactic-pos (- (point) 10000)))
;; Don't have to do this if the cache is relevant here,
;; typically if the same line is refontified again. If
;; we're just some syntactic whitespace further down we can
;; still use the cache to limit the skipping.
(c-backward-syntactic-ws c-find-decl-syntactic-pos))
;; If we hit `c-find-decl-syntactic-pos' and
;; `c-find-decl-match-pos' is set then we install the cached
;; values. If we hit `c-find-decl-syntactic-pos' and
;; `c-find-decl-match-pos' is nil then we know there's no decl
;; prefix in the whitespace before `c-find-decl-syntactic-pos'
;; and so we can continue the search from this point. If we
;; didn't hit `c-find-decl-syntactic-pos' then we're now in
;; the right spot to begin searching anyway.
(if (and (eq (point) c-find-decl-syntactic-pos)
c-find-decl-match-pos)
(setq cfd-match-pos c-find-decl-match-pos
cfd-continue-pos syntactic-pos)
(setq c-find-decl-syntactic-pos syntactic-pos)
(when (if (bobp)
;; Always consider bob a match to get the first
;; declaration in the file. Do this separately instead of
;; letting `c-decl-prefix-or-start-re' match bob, so that
;; regexp always can consume at least one character to
;; ensure that we won't get stuck in an infinite loop.
(setq cfd-re-match 0)
(backward-char)
(c-beginning-of-current-token)
(< (point) cfd-limit))
;; Do an initial search now. In the bob case above it's
;; only done to search for a `c-decl-end' spot.
(c-find-decl-prefix-search)) ; sets cfd-continue-pos
(setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
cfd-match-pos))))) ; end of `cond'
;; Advance `cfd-continue-pos' if it's before the start position.
;; The closest continue position that might have effect at or
;; after the start depends on what we started in. This also
;; finds a suitable start position in the special cases when the
;; region is completely within a literal or macro.
(when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
(cond
(start-in-macro
;; If we're in a macro then it's the closest preceding token
;; in the macro. Check this before `start-in-literal',
;; since if we're inside a literal in a macro, the preceding
;; token is earlier than any `c-decl-end' spot inside the
;; literal (comment).
(goto-char (or start-in-literal cfd-start-pos))
;; The only syntactic ws in macros are comments.
(c-backward-comments)
(or (bobp) (backward-char))
(c-beginning-of-current-token)
;; If we're in a macro without argument parentheses, we could have
;; now ended up at the macro's identifier. We need to be at #define
;; for `c-find-decl-prefix-search' to find the first token of the
;; macro's expansion.
(when (and (c-on-identifier)
(setq hash-define-pos
(save-excursion
(and
(zerop (c-backward-token-2 2)) ; over define, #
(save-excursion
(beginning-of-line)
(looking-at c-opt-cpp-macro-define-id))
(point)))))
(goto-char hash-define-pos)))
(start-in-literal
;; If we're in a comment it can only be the closest
;; preceding `c-decl-end' position within that comment, if
;; any. Go back to the beginning of such a property so that
;; `c-find-decl-prefix-search' will find the end of it.
;; (Can't stop at the end and install it directly on
;; `cfd-prop-match' since that variable might be cleared
;; after `cfd-fun' below.)
;;
;; Note that if the literal is a string then the property
;; search will simply skip to the beginning of it right
;; away.
(if (not c-type-decl-end-used)
(goto-char start-in-literal)
(goto-char cfd-start-pos)
(while (progn
(goto-char (previous-single-property-change
(point) 'c-type nil start-in-literal))
(and (> (point) start-in-literal)
(not (eq (c-get-char-property (point) 'c-type)
'c-decl-end))))))
(when (and (= (point) start-in-literal)
(not (looking-at c-doc-bright-comment-start-re)))
;; Didn't find any property inside the comment, so we can
;; skip it entirely. (This won't skip past a string, but
;; that'll be handled quickly by the next
;; `c-find-decl-prefix-search' anyway.)
(c-forward-single-comment)
(if (> (point) cfd-limit)
(goto-char cfd-limit))))
(t
;; If we started in normal code, the only match that might
;; apply before the start is what we already got in
;; `cfd-match-pos' so we can continue at the start position.
;; (Note that we don't get here if the first match is below
;; it.)
(goto-char cfd-start-pos))) ; end of `cond'
;; Delete found matches if they are before our new continue
;; position, so that `c-find-decl-prefix-search' won't back up
;; to them later on.
(setq cfd-continue-pos (point))
(when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
(setq cfd-re-match nil))
(when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
(setq cfd-prop-match nil))) ; end of `when'
(if syntactic-pos
;; This is the normal case and we got a proper syntactic
;; position. If there's a match then it's always outside
;; macros and comments, so advance to the next token and set
;; `cfd-token-pos'. The loop below will later go back using
;; `cfd-continue-pos' to fix declarations inside the
;; syntactic ws.
(when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
(goto-char syntactic-pos)
(c-forward-syntactic-ws)
(and cfd-continue-pos
(< cfd-continue-pos (point))
(setq cfd-token-pos (point))))
;; Have one of the special cases when the region is completely
;; within a literal or macro. `cfd-continue-pos' is set to a
;; good start position for the search, so do it.
(c-find-decl-prefix-search)))
;; Now loop, one decl spot per iteration. We already have the first
;; match in `cfd-match-pos'.
(while (progn
;; Go forward over "false matches", one per iteration.
(while (and
(< cfd-match-pos cfd-limit)
(or
;; Kludge to filter out matches on the "<" that
;; aren't open parens, for the sake of languages
;; that got `c-recognize-<>-arglists' set.
(and (eq (char-before cfd-match-pos) ?<)
(not (c-get-char-property (1- cfd-match-pos)
'syntax-table)))
;; If `cfd-continue-pos' is less or equal to
;; `cfd-token-pos', we've got a hit inside a macro
;; that's in the syntactic whitespace before the last
;; "real" declaration we've checked. If they're equal
;; we've arrived at the declaration a second time, so
;; there's nothing to do.
(= cfd-continue-pos cfd-token-pos)
(progn
;; If `cfd-continue-pos' is less than `cfd-token-pos'
;; we're still searching for declarations embedded in
;; the syntactic whitespace. In that case we need
;; only to skip comments and not macros, since they
;; can't be nested, and that's already been done in
;; `c-find-decl-prefix-search'.
(when (> cfd-continue-pos cfd-token-pos)
(c-forward-syntactic-ws)
(setq cfd-token-pos (point)))
;; Continue if the following token fails the
;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
(when (or (>= (point) cfd-limit)
(not (looking-at cfd-decl-re))
(and cfd-face-checklist
(not (c-got-face-at
(point) cfd-face-checklist))))
(goto-char cfd-continue-pos)
t)))
(< (point) cfd-limit)) ; end of "false matches" condition
(c-find-decl-prefix-search)) ; end of "false matches" loop
(< (point) cfd-limit)) ; end of condition for "decl-spot" while
(when (and
(>= (point) cfd-start-pos)
(progn
;; Narrow to the end of the macro if we got a hit inside
;; one, to avoid recognizing things that start inside the
;; macro and end outside it.
(when (> cfd-match-pos cfd-macro-end)
;; Not in the same macro as in the previous round.
(save-excursion
(goto-char cfd-match-pos)
(setq cfd-macro-end
(if (save-excursion (and (c-beginning-of-macro)
(< (point) cfd-match-pos)))
(progn (c-end-of-macro)
(point))
0))))
(if (zerop cfd-macro-end)
t
(if (> cfd-macro-end (point))
(progn (narrow-to-region (point-min) cfd-macro-end)
t)
;; The matched token was the last thing in the macro,
;; so the whole match is bogus.
(setq cfd-macro-end 0)
nil)))) ; end of when condition
(when (> cfd-macro-end 0)
(setq cfd-top-level nil)) ; In a macro is "never" at top level.
(c-debug-put-decl-spot-faces cfd-match-pos (point))
(if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0) cfd-top-level)
(setq cfd-prop-match nil))
(when (/= cfd-macro-end 0)
;; Restore limits if we did macro narrowing above.
(narrow-to-region (point-min) cfd-buffer-end)))
(goto-char cfd-continue-pos)
(if (= cfd-continue-pos cfd-limit)
(setq cfd-match-pos cfd-limit)
(c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
; cfd-match-pos, etc.
\f
;; A cache for found types.
;; Buffer local variable that contains an obarray with the types we've
;; found. If a declaration is recognized somewhere we record the
;; fully qualified identifier in it to recognize it as a type
;; elsewhere in the file too. This is not accurate since we do not
;; bother with the scoping rules of the languages, but in practice the
;; same name is seldom used as both a type and something else in a
;; file, and we only use this as a last resort in ambiguous cases (see
;; `c-forward-decl-or-cast-1').
;;
;; Not every type need be in this cache. However, things which have
;; ceased to be types must be removed from it.
;;
;; Template types in C++ are added here too but with the template
;; arglist replaced with "<>" in references or "<" for the one in the
;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
;; "Foo<>::Bar<". This avoids storing very long strings (since C++
;; template specs can be fairly sized programs in themselves) and
;; improves the hit ratio (it's a type regardless of the template
;; args; it's just not the same type, but we're only interested in
;; recognizing types, not telling distinct types apart). Note that
;; template types in references are added here too; from the example
;; above there will also be an entry "Foo<".
(defvar c-found-types nil)
(make-variable-buffer-local 'c-found-types)
(defsubst c-clear-found-types ()
;; Clears `c-found-types'.
(setq c-found-types
(make-hash-table :test #'equal :weakness nil)))
(defun c-add-type (from to)
;; Add the given region as a type in `c-found-types'. If the region
;; doesn't match an existing type but there is a type which is equal
;; to the given one except that the last character is missing, then
;; the shorter type is removed. That's done to avoid adding all
;; prefixes of a type as it's being entered and font locked. This
;; doesn't cover cases like when characters are removed from a type
;; or added in the middle. We'd need the position of point when the
;; font locking is invoked to solve this well.
;;
;; This function might do hidden buffer changes.
(let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
(unless (gethash type c-found-types)
(remhash (substring type 0 -1) c-found-types)
(puthash type t c-found-types))))
(defun c-unfind-type (name)
;; Remove the "NAME" from c-found-types, if present.
(remhash name c-found-types))
(defsubst c-check-type (from to)
;; Return non-nil if the given region contains a type in
;; `c-found-types'.
;;
;; This function might do hidden buffer changes.
(gethash (c-syntactic-content from to c-recognize-<>-arglists) c-found-types))
(defun c-list-found-types ()
;; Return all the types in `c-found-types' as a sorted list of
;; strings.
(let (type-list)
(maphash (lambda (type _)
(setq type-list (cons type type-list)))
c-found-types)
(sort type-list 'string-lessp)))
;; Shut up the byte compiler.
(defvar c-maybe-stale-found-type)
(defun c-trim-found-types (beg end _old-len)
;; An after change function which, in conjunction with the info in
;; c-maybe-stale-found-type (set in c-before-change), removes a type
;; from `c-found-types', should this type have become stale. For
;; example, this happens to "foo" when "foo \n bar();" becomes
;; "foo(); \n bar();". Such stale types, if not removed, foul up
;; the fontification.
;;
;; Have we, perhaps, added non-ws characters to the front/back of a found
;; type?
(when (> end beg)
(save-excursion
(when (< end (point-max))
(goto-char end)
(if (and (c-beginning-of-current-token) ; only moves when we started in the middle
(progn (goto-char end)
(c-end-of-current-token)))
(c-unfind-type (buffer-substring-no-properties
end (point)))))
(when (> beg (point-min))
(goto-char beg)
(if (and (c-end-of-current-token) ; only moves when we started in the middle
(progn (goto-char beg)
(c-beginning-of-current-token)))
(c-unfind-type (buffer-substring-no-properties
(point) beg))))))
(if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
(cond
;; Changing the amount of (already existing) whitespace - don't do anything.
((and (c-partial-ws-p beg end)
(or (= beg end) ; removal of WS
(string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
;; The syntactic relationship which defined a "found type" has been
;; destroyed.
((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
(c-unfind-type (cadr c-maybe-stale-found-type)))
;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
)))
\f
;; Setting and removing syntax properties on < and > in languages (C++
;; and Java) where they can be template/generic delimiters as well as
;; their normal meaning of "less/greater than".
;; Normally, < and > have syntax 'punctuation'. When they are found to
;; be delimiters, they are marked as such with the category properties
;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
;; STRATEGY:
;;
;; It is impossible to determine with certainty whether a <..> pair in
;; C++ is two comparison operators or is template delimiters, unless
;; one duplicates a lot of a C++ compiler. For example, the following
;; code fragment:
;;
;; foo (a < b, c > d) ;
;;
;; could be a function call with two integer parameters (each a
;; relational expression), or it could be a constructor for class foo
;; taking one parameter d of templated type "a < b, c >". They are
;; somewhat easier to distinguish in Java.
;;
;; The strategy now (2010-01) adopted is to mark and unmark < and
;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
;; individually when their context so indicated. This gave rise to
;; intractable problems when one of a matching pair was deleted, or
;; pulled into a literal.]
;;
;; At each buffer change, the syntax-table properties are removed in a
;; before-change function and reapplied, when needed, in an
;; after-change function. It is far more important that the
;; properties get removed when they they are spurious than that they
;; be present when wanted.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c-clear-<-pair-props (&optional pos)
;; POS (default point) is at a < character. If it is marked with
;; open paren syntax-table text property, remove the property,
;; together with the close paren property on the matching > (if
;; any).
(save-excursion
(if pos
(goto-char pos)
(setq pos (point)))
(when (equal (c-get-char-property (point) 'syntax-table)
c-<-as-paren-syntax)
(with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
(c-go-list-forward))
(when (equal (c-get-char-property (1- (point)) 'syntax-table)
c->-as-paren-syntax) ; should always be true.
(c-unmark-<->-as-paren (1- (point))))
(c-unmark-<->-as-paren pos))))
(defun c-clear->-pair-props (&optional pos)
;; POS (default point) is at a > character. If it is marked with
;; close paren syntax-table property, remove the property, together
;; with the open paren property on the matching < (if any).
(save-excursion
(if pos
(goto-char pos)
(setq pos (point)))
(when (equal (c-get-char-property (point) 'syntax-table)
c->-as-paren-syntax)
(with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
(c-go-up-list-backward))
(when (equal (c-get-char-property (point) 'syntax-table)
c-<-as-paren-syntax) ; should always be true.
(c-unmark-<->-as-paren (point)))
(c-unmark-<->-as-paren pos))))
(defun c-clear-<>-pair-props (&optional pos)
;; POS (default point) is at a < or > character. If it has an
;; open/close paren syntax-table property, remove this property both
;; from the current character and its partner (which will also be
;; thusly marked).
(cond
((eq (char-after) ?\<)
(c-clear-<-pair-props pos))
((eq (char-after) ?\>)
(c-clear->-pair-props pos))
(t (c-benign-error
"c-clear-<>-pair-props called from wrong position"))))
(defun c-clear-<-pair-props-if-match-after (lim &optional pos)
;; POS (default point) is at a < character. If it is both marked
;; with open/close paren syntax-table property, and has a matching >
;; (also marked) which is after LIM, remove the property both from
;; the current > and its partner. Return t when this happens, nil
;; when it doesn't.
(save-excursion
(if pos
(goto-char pos)
(setq pos (point)))
(when (equal (c-get-char-property (point) 'syntax-table)
c-<-as-paren-syntax)
(with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
(c-go-list-forward))
(when (and (>= (point) lim)
(equal (c-get-char-property (1- (point)) 'syntax-table)
c->-as-paren-syntax)) ; should always be true.
(c-unmark-<->-as-paren (1- (point)))
(c-unmark-<->-as-paren pos))
t)))
(defun c-clear->-pair-props-if-match-before (lim &optional pos)
;; POS (default point) is at a > character. If it is both marked
;; with open/close paren syntax-table property, and has a matching <
;; (also marked) which is before LIM, remove the property both from
;; the current < and its partner. Return t when this happens, nil
;; when it doesn't.
(save-excursion
(if pos
(goto-char pos)
(setq pos (point)))
(when (equal (c-get-char-property (point) 'syntax-table)
c->-as-paren-syntax)
(with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
(c-go-up-list-backward))
(when (and (<= (point) lim)
(equal (c-get-char-property (point) 'syntax-table)
c-<-as-paren-syntax)) ; should always be true.
(c-unmark-<->-as-paren (point))
(c-unmark-<->-as-paren pos))
t)))
;; Set by c-common-init in cc-mode.el.
(defvar c-new-BEG)
(defvar c-new-END)
;; Set by c-before-change-check-raw-strings.
(defvar c-old-END-literality)
(defun c-before-change-check-<>-operators (beg end)
;; Unmark certain pairs of "< .... >" which are currently marked as
;; template/generic delimiters. (This marking is via syntax-table text
;; properties), and expand the (c-new-BEG c-new-END) region to include all
;; unmarked < and > operators within the certain bounds (see below).
;;
;; These pairs are those which are in the current "statement" (i.e.,
;; the region between the {, }, or ; before BEG and the one after
;; END), and which enclose any part of the interval (BEG END).
;;
;; Note that in C++ (?and Java), template/generic parens cannot
;; enclose a brace or semicolon, so we use these as bounds on the
;; region we must work on.
;;
;; This function is called from before-change-functions (via
;; c-get-state-before-change-functions). Thus the buffer is widened,
;; and point is undefined, both at entry and exit.
;;
;; FIXME!!! This routine ignores the possibility of macros entirely.
;; 2010-01-29.
(when (and (or (> end beg)
(and (> c-<-pseudo-digraph-cont-len 0)
(goto-char beg)
(progn
(skip-chars-backward
"^<" (max (- (point) c-<-pseudo-digraph-cont-len)
(point-min)))
(eq (char-before) ?<))
(looking-at c-<-pseudo-digraph-cont-regexp)))
(or
(progn
(goto-char beg)
(search-backward "<" (max (- (point) 1024) (point-min)) t))
(progn
(goto-char end)
(search-forward ">" (min (+ (point) 1024) (point-max)) t))))
(save-excursion
(c-save-buffer-state
((beg-lit-start (progn (goto-char beg) (c-literal-start)))
(end-lit-limits (progn (goto-char end) (c-literal-limits)))
new-beg new-end beg-limit end-limit)
;; Locate the earliest < after the barrier before the changed region,
;; which isn't already marked as a paren.
(goto-char (or beg-lit-start beg))
(setq beg-limit (c-determine-limit 512))
;; Remove the syntax-table/category properties from each pertinent <...>
;; pair. Firstly, the ones with the < before beg and > after beg....
(while (progn (c-syntactic-skip-backward "^;{}<" beg-limit)
(eq (char-before) ?<))
(c-backward-token-2)
(when (eq (char-after) ?<)
(c-clear-<-pair-props-if-match-after beg)
(setq new-beg (point))))
(c-forward-syntactic-ws)
;; ...Then the ones with < before end and > after end.
(goto-char (if end-lit-limits (cdr end-lit-limits) end))
(setq end-limit (c-determine-+ve-limit 512))
(while (and (c-syntactic-re-search-forward "[;{}>]" end-limit 'end)
(eq (char-before) ?>))
(c-end-of-current-token)
(when (eq (char-before) ?>)
(c-clear->-pair-props-if-match-before end (1- (point)))
(setq new-end (point))))
(c-backward-syntactic-ws)
;; Extend the fontification region, if needed.
(and new-beg
(< new-beg c-new-BEG)
(setq c-new-BEG new-beg))
(and new-end
(> new-end c-new-END)
(setq c-new-END new-end))))))
(defun c-after-change-check-<>-operators (beg end)
;; This is called from `after-change-functions' when
;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
;; chars with paren syntax become part of another operator like "<<"
;; or ">=".
;;
;; This function might do hidden buffer changes.
(save-excursion
(goto-char beg)
(when (or (looking-at "[<>]")
(< (skip-chars-backward "<>") 0))
(goto-char beg)
(c-beginning-of-current-token)
(when (and (< (point) beg)
(looking-at c-<>-multichar-token-regexp)
(< beg (setq beg (match-end 0))))
(while (progn (skip-chars-forward "^<>" beg)
(< (point) beg))
(c-clear-<>-pair-props)
(forward-char))))
(when (< beg end)
(goto-char end)
(when (or (looking-at "[<>]")
(< (skip-chars-backward "<>") 0))
(goto-char end)
(c-beginning-of-current-token)
(when (and (< (point) end)
(looking-at c-<>-multichar-token-regexp)
(< end (setq end (match-end 0))))
(while (progn (skip-chars-forward "^<>" end)
(< (point) end))
(c-clear-<>-pair-props)
(forward-char)))))))
(defun c-restore-<>-properties (_beg _end _old-len)
;; This function is called as an after-change function. It restores the
;; category/syntax-table properties on template/generic <..> pairs between
;; c-new-BEG and c-new-END. It may do hidden buffer changes.
(c-save-buffer-state ((c-parse-and-markup-<>-arglists t)
c-restricted-<>-arglists lit-limits)
(goto-char c-new-BEG)
(if (setq lit-limits (c-literal-limits))
(goto-char (cdr lit-limits)))
(while (and (< (point) c-new-END)
(c-syntactic-re-search-forward "<" c-new-END 'bound))
(backward-char)
(save-excursion
(c-backward-token-2)
(setq c-restricted-<>-arglists
(and (not (looking-at c-opt-<>-sexp-key))
(progn (c-backward-syntactic-ws) ; to ( or ,
(and (memq (char-before) '(?\( ?,)) ; what about <?
(not (eq (c-get-char-property (point) 'c-type)
'c-decl-arg-start)))))))
(or (c-forward-<>-arglist nil)
(c-forward-over-token-and-ws)
(goto-char c-new-END)))))
\f
;; Functions to handle C++ raw strings.
;;
;; A valid C++ raw string looks like
;; R"<id>(<contents>)<id>"
;; , where <id> is an identifier from 0 to 16 characters long, not containing
;; spaces, control characters, or left/right paren. <contents> can include
;; anything which isn't the terminating )<id>", including new lines, "s,
;; parentheses, etc.
;;
;; CC Mode handles C++ raw strings by the use of `syntax-table' text
;; properties as follows:
;;
;; (i) On a validly terminated raw string, no `syntax-table' text properties
;; are applied to the opening and closing delimiters, but any " in the
;; contents is given the property value "punctuation" (`(1)') to prevent it
;; interacting with the "s in the delimiters.
;;
;; The font locking routine `c-font-lock-raw-strings' (in cc-fonts.el)
;; recognizes valid raw strings, and fontifies the delimiters (apart from
;; the parentheses) with the default face and the parentheses and the
;; <contents> with font-lock-string-face.
;;
;; (ii) A valid, but unterminated, raw string opening delimiter gets the
;; "punctuation" value (`(1)') of the `syntax-table' text property, and the
;; open parenthesis gets the "string fence" value (`(15)'). When such a
;; delimiter is found, no attempt is made in any way to "correct" any text
;; properties after the delimiter.
;;
;; `c-font-lock-raw-strings' puts c-font-lock-warning-face on the entire
;; unmatched opening delimiter (from the R up to the open paren), and allows
;; the rest of the buffer to get font-lock-string-face, caused by the
;; unmatched "string fence" `syntax-table' text property value.
;;
;; (iii) Inside a macro, a valid raw string is handled as in (i). An
;; unmatched opening delimiter is handled slightly differently. In addition
;; to the "punctuation" and "string fence" properties on the delimiter,
;; another "string fence" `syntax-table' property is applied to the last
;; possible character of the macro before the terminating linefeed (if there
;; is such a character after the "("). This "last possible" character is
;; never a backslash escaping the end of line. If the character preceding
;; this "last possible" character is itself a backslash, this preceding
;; character gets a "punctuation" `syntax-table' value. If the "(" is
;; already at the end of the macro, it gets the "punctuation" value, and no
;; "string fence"s are used.
;;
;; The effect on the fontification of either of these tactics is that the
;; rest of the macro (if any) after the "(" gets font-lock-string-face, but
;; the rest of the file is fontified normally.
;; The values of the function `c-raw-string-pos' at before-change-functions'
;; BEG and END.
(defvar c-old-beg-rs nil)
(defvar c-old-end-rs nil)
;; Whether a buffer change has disrupted or will disrupt the terminating id of
;; a raw string.
(defvar c-raw-string-end-delim-disrupted nil)
(defun c-raw-string-pos ()
;; Get POINT's relationship to any containing raw string.
;; If point isn't in a raw string, return nil.
;; Otherwise, return the following list:
;;
;; (POS B\" B\( E\) E\")
;;
;; , where POS is the symbol `open-delim' if point is in the opening
;; delimiter, the symbol `close-delim' if it's in the closing delimiter, and
;; nil if it's in the string body. B\", B\(, E\), E\" are the positions of
;; the opening and closing quotes and parentheses of a correctly terminated
;; raw string. (N.B.: E\) and E\" are NOT on the "outside" of these
;; characters.) If the raw string is not terminated, E\) and E\" are set to
;; nil.
;;
;; Note: this function is dependent upon the correct syntax-table text
;; properties being set.
(let ((state (c-semi-pp-to-literal (point)))
open-quote-pos open-paren-pos close-paren-pos close-quote-pos id)
(save-excursion
(when
(and
(cond
((null (cadr state))
(or (eq (char-after) ?\")
(search-backward "\"" (max (- (point) 17) (point-min)) t)))
((and (eq (cadr state) 'string)
(goto-char (nth 2 state))
(cond
((eq (char-after) ?\"))
((eq (char-after) ?\()
(let ((here (point)))
(goto-char (max (- (point) 18) (point-min)))
(while
(and
(search-forward-regexp
c-c++-raw-string-opener-re
(1+ here) 'limit)
(< (point) here)))
(and (eq (point) (1+ here))
(match-beginning 1)
(goto-char (1- (match-beginning 1)))))))
(not (bobp)))))
(c-at-c++-raw-string-opener))
(setq open-quote-pos (point)
open-paren-pos (match-end 1)
id (match-string-no-properties 1))
(goto-char (1+ open-paren-pos))
(when (and (not (c-get-char-property open-paren-pos 'syntax-table))
(search-forward (concat ")" id "\"") nil t))
(setq close-paren-pos (match-beginning 0)
close-quote-pos (1- (point))))))
(and open-quote-pos
(list
(cond
((<= (point) open-paren-pos)
'open-delim)
((and close-paren-pos
(> (point) close-paren-pos))
'close-delim)
(t nil))
open-quote-pos open-paren-pos close-paren-pos close-quote-pos))))
(defun c-raw-string-in-end-delim (beg end)
;; If the region (BEG END) intersects a possible raw string terminator,
;; return a cons of the position of the ) and the position of the " in the
;; first one found.
(save-excursion
(goto-char (max (- beg 17) (point-min)))
(while
(and
(search-forward-regexp ")\\([^ ()\\\n\r\t]\\{0,16\\}\\)\""
(min (+ end 17) (point-max)) t)
(<= (point) beg)))
(unless (or (<= (point) beg)
(>= (match-beginning 0) end))
(cons (match-beginning 0) (match-end 1)))))
(defun c-depropertize-raw-string (id open-quote open-paren bound)
;; Point is immediately after a raw string opening delimiter. Remove any
;; `syntax-table' text properties associated with the delimiter (if it's
;; unmatched) or the raw string.
;;
;; ID, a string, is the delimiter's identifier. OPEN-QUOTE and OPEN-PAREN
;; are the buffer positions of the delimiter's components. BOUND is the
;; bound for searching for a matching closing delimiter; it is usually nil,
;; but if we're inside a macro, it's the end of the macro (i.e. just before
;; the terminating \n).
;;
;; Point is moved to after the (terminated) raw string, or left after the
;; unmatched opening delimiter, as the case may be. The return value is of
;; no significance.
(let ((open-paren-prop (c-get-char-property open-paren 'syntax-table))
first)
;; If the delimiter is "unclosed", or sombody's used " in their id, clear
;; the 'syntax-table property from all of them.
(setq first (c-clear-char-property-with-value-on-char
open-quote open-paren 'syntax-table '(1) ?\"))
(if first (c-truncate-lit-pos-cache first))
(cond
((null open-paren-prop)
;; Should be a terminated raw string...
(when (search-forward (concat ")" id "\"") nil t)
;; Yes, it is. :-)
;; Clear any '(1)s from "s in the identifier.
(setq first (c-clear-char-property-with-value-on-char
(1+ (match-beginning 0)) (1- (match-end 0))
'syntax-table '(1) ?\"))
(if first (c-truncate-lit-pos-cache first))
;; Clear any random `syntax-table' text properties from the contents.
(let* ((closing-paren (match-beginning 0))
(first-st
(and
(< (1+ open-paren) closing-paren)
(or
(and (c-get-char-property (1+ open-paren) 'syntax-table)
(1+ open-paren))
(and
(setq first
(c-next-single-property-change
(1+ open-paren) 'syntax-table nil closing-paren))
(< first closing-paren)
first)))))
(when first-st
(c-clear-char-properties first-st (match-beginning 0)
'syntax-table)
(c-truncate-lit-pos-cache first-st))
(when (c-get-char-property (1- (match-end 0)) 'syntax-table)
;; Was previously an unterminated (ordinary) string
(save-excursion
(goto-char (1- (match-end 0)))
(when (c-safe (c-forward-sexp)) ; to '(1) at EOL.
(c-clear-char-property (1- (point)) 'syntax-table))
(c-clear-char-property (1- (match-end 0)) 'syntax-table)
(c-truncate-lit-pos-cache (1- (match-end 0))))))))
((or (and (equal open-paren-prop '(15)) (null bound))
(equal open-paren-prop '(1)))
;; An unterminated raw string either not in a macro, or in a macro with
;; the open parenthesis right up against the end of macro
(c-clear-char-property open-quote 'syntax-table)
(c-truncate-lit-pos-cache open-quote)
(c-clear-char-property open-paren 'syntax-table))
(t
;; An unterminated string in a macro, with at least one char after the
;; open paren
(c-clear-char-property open-quote 'syntax-table)
(c-truncate-lit-pos-cache open-quote)
(c-clear-char-property open-paren 'syntax-table)
(c-clear-char-property-with-value (1+ open-paren) bound 'syntax-table
'(15))))))
(defun c-depropertize-raw-strings-in-region (start finish)
;; Remove any `syntax-table' text properties associated with C++ raw strings
;; contained in the region (START FINISH). Point is undefined at entry and
;; exit, and the return value has no significance.
(goto-char start)
(while (and (< (point) finish)
(re-search-forward
(concat "\\(" ; 1
c-anchored-cpp-prefix ; 2
"\\)\\|\\(" ; 3
c-c++-raw-string-opener-re ; 4
"\\)")
finish t))
(when (save-excursion
(goto-char (match-beginning 0)) (not (c-in-literal)))
(if (match-beginning 4) ; the id
;; We've found a raw string
(c-depropertize-raw-string
(match-string-no-properties 4) ; id
(1+ (match-beginning 3)) ; open quote
(match-end 4) ; open paren
nil) ; bound
;; We've found a CPP construct. Search for raw strings within it.
(goto-char (match-beginning 2)) ; the "#"
(c-end-of-macro)
(let ((eom (point)))
(goto-char (match-end 2)) ; after the "#".
(while (and (< (point) eom)
(c-syntactic-re-search-forward
c-c++-raw-string-opener-re eom t))
(c-depropertize-raw-string
(match-string-no-properties 1) ; id
(1+ (match-beginning 0)) ; open quote
(match-end 1) ; open paren
eom))))))) ; bound.
(defun c-before-change-check-raw-strings (beg end)
;; This function clears `syntax-table' text properties from C++ raw strings
;; whose delimiters are about to change in the region (c-new-BEG c-new-END).
;; BEG and END are the standard arguments supplied to any before-change
;; function.
;;
;; Point is undefined on both entry and exit, and the return value has no
;; significance.
;;
;; This function is called as a before-change function solely due to its
;; membership of the C++ value of `c-get-state-before-change-functions'.
(goto-char end)
(setq c-raw-string-end-delim-disrupted nil)
;; We use the following to detect a R"<id>( being swallowed into a string by
;; the pending change.
(setq c-old-END-literality (c-in-literal))
(c-save-buffer-state
((term-del (c-raw-string-in-end-delim beg end))
Rquote close-quote)
(setq c-old-beg-rs (progn (goto-char beg) (c-raw-string-pos))
c-old-end-rs (progn (goto-char end) (c-raw-string-pos)))
(cond
;; We're not changing, or we're obliterating raw strings.
((and (null c-old-beg-rs) (null c-old-end-rs)))
;; We're changing the putative terminating delimiter of a raw string
;; containing BEG.
((and c-old-beg-rs term-del
(or (null (nth 3 c-old-beg-rs))
(<= (car term-del) (nth 3 c-old-beg-rs))))
(setq Rquote (1- (cadr c-old-beg-rs))
close-quote (1+ (cdr term-del)))
(setq c-raw-string-end-delim-disrupted t)
(c-depropertize-raw-strings-in-region Rquote close-quote)
(setq c-new-BEG (min c-new-BEG Rquote)
c-new-END (max c-new-END close-quote)))
;; We're breaking an escaped NL in a raw string in a macro.
((and c-old-end-rs
(< beg end)
(goto-char end) (eq (char-before) ?\\)
(c-beginning-of-macro))
(let ((bom (point))
(eom (progn (c-end-of-macro) (point))))
(c-depropertize-raw-strings-in-region bom eom)
(setq c-new-BEG (min c-new-BEG bom)
c-new-END (max c-new-END eom))))
;; We're changing only the contents of a raw string.
((and (equal (cdr c-old-beg-rs) (cdr c-old-end-rs))
(null (car c-old-beg-rs)) (null (car c-old-end-rs))))
((or
;; We're removing (at least part of) the R" of the starting delim of a
;; raw string:
(null c-old-beg-rs)
(and (eq beg (cadr c-old-beg-rs))
(< beg end))
;; Or we're removing the ( of the starting delim of a raw string.
(and (eq (car c-old-beg-rs) 'open-delim)
(or (null c-old-end-rs)
(not (eq (car c-old-end-rs) 'open-delim))
(not (equal (cdr c-old-beg-rs) (cdr c-old-end-rs))))))
(let ((close (nth 4 (or c-old-end-rs c-old-beg-rs))))
(setq Rquote (1- (cadr (or c-old-end-rs c-old-beg-rs)))
close-quote (if close (1+ close) (point-max))))
(c-depropertize-raw-strings-in-region Rquote close-quote)
(setq c-new-BEG (min c-new-BEG Rquote)
c-new-END (max c-new-END close-quote)))
;; We're changing only the text of the identifier of the opening
;; delimiter of a raw string.
((and (eq (car c-old-beg-rs) 'open-delim)
(equal c-old-beg-rs c-old-end-rs))))))
(defun c-propertize-raw-string-id (start end)
;; If the raw string identifier between buffer positions START and END
;; contains any double quote characters, put a punctuation syntax-table text
;; property on them. The return value is of no significance.
(save-excursion
(goto-char start)
(while (and (skip-chars-forward "^\"" end)
(< (point) end))
(c-put-char-property (point) 'syntax-table '(1))
(c-truncate-lit-pos-cache (point))
(forward-char))))
(defun c-propertize-raw-string-opener (id open-quote open-paren bound)
;; Point is immediately after a raw string opening delimiter. Apply any
;; pertinent `syntax-table' text properties to the delimiter and also the
;; raw string, should there be a valid matching closing delimiter.
;;
;; ID, a string, is the delimiter's identifier. OPEN-QUOTE and OPEN-PAREN
;; are the buffer positions of the delimiter's components. BOUND is the
;; bound for searching for a matching closing delimiter; it is usually nil,
;; but if we're inside a macro, it's the end of the macro (i.e. the position
;; of the closing newline).
;;
;; Point is moved to after the (terminated) raw string and t is returned, or
;; it is left after the unmatched opening delimiter and nil is returned.
(c-propertize-raw-string-id (1+ open-quote) open-paren)
(prog1
(if (search-forward (concat ")" id "\"") bound t)
(let ((end-string (match-beginning 0))
(after-quote (match-end 0)))
(c-propertize-raw-string-id
(1+ (match-beginning 0)) (1- (match-end 0)))
(goto-char open-paren)
(while (progn (skip-syntax-forward "^\"" end-string)
(< (point) end-string))
(c-put-char-property (point) 'syntax-table '(1)) ; punctuation
(c-truncate-lit-pos-cache (point))
(forward-char))
(goto-char after-quote)
t)
(c-put-char-property open-quote 'syntax-table '(1)) ; punctuation
(c-truncate-lit-pos-cache open-quote)
(c-put-char-property open-paren 'syntax-table '(15)) ; generic string
(when bound
;; In a CPP construct, we try to apply a generic-string
;; `syntax-table' text property to the last possible character in
;; the string, so that only characters within the macro get
;; "stringed out".
(goto-char bound)
(if (save-restriction
(narrow-to-region (1+ open-paren) (point-max))
(re-search-backward
(eval-when-compile
;; This regular expression matches either an escape pair
;; (which isn't an escaped NL) (submatch 5) or a
;; non-escaped character (which isn't itself a backslash)
;; (submatch 10). The long preambles to these
;; (respectively submatches 2-4 and 6-9) ensure that we
;; have the correct parity for sequences of backslashes,
;; etc..
(concat "\\(" ; 1
"\\(\\`[^\\]?\\|[^\\][^\\]\\)\\(\\\\\\(.\\|\n\\)\\)*" ; 2-4
"\\(\\\\.\\)" ; 5
"\\|"
"\\(\\`\\|[^\\]\\|\\(\\`[^\\]?\\|[^\\][^\\]\\)\\(\\\\\\(.\\|\n\\)\\)+\\)" ; 6-9
"\\([^\\]\\)" ; 10
"\\)"
"\\(\\\\\n\\)*\\=")) ; 11
(1+ open-paren) t))
(if (match-beginning 10)
(progn
(c-put-char-property (match-beginning 10) 'syntax-table '(15))
(c-truncate-lit-pos-cache (match-beginning 10)))
(c-put-char-property (match-beginning 5) 'syntax-table '(1))
(c-put-char-property (1+ (match-beginning 5)) 'syntax-table '(15))
(c-truncate-lit-pos-cache (1+ (match-beginning 5))))
;; (c-put-char-property open-paren 'syntax-table '(1))
)
(goto-char bound))
nil)))
(defun c-after-change-unmark-raw-strings (beg end _old-len)
;; This function removes `syntax-table' text properties from any raw strings
;; which have been affected by the current change. These are those which
;; have been "stringed out" and from newly formed raw strings, or any
;; existing raw string which the new text terminates. BEG, END, and
;; _OLD-LEN are the standard arguments supplied to any
;; after-change-function.
;;
;; Point is undefined on both entry and exit, and the return value has no
;; significance.
;;
;; This functions is called as an after-change function by virtue of its
;; membership of the C++ value of `c-before-font-lock-functions'.
;; (when (< beg end)
(c-save-buffer-state (found eoll state id found-beg)
;; Has an inserted " swallowed up a R"(, turning it into "...R"(?
(goto-char end)
(setq eoll (c-point 'eoll))
(when (and (null c-old-END-literality)
(search-forward-regexp c-c++-raw-string-opener-re eoll t))
(setq state (c-semi-pp-to-literal end))
(when (eq (cadr state) 'string)
(unwind-protect
;; Temporarily insert a closing string delimiter....
(progn
(goto-char end)
(cond
((c-characterp (nth 3 (car state)))
(insert (nth 3 (car state))))
((eq (nth 3 (car state)) t)
(insert ?\")
(c-put-char-property end 'syntax-table '(15))))
(c-truncate-lit-pos-cache end)
;; ....ensure c-new-END extends right to the end of the about
;; to be un-stringed raw string....
(save-excursion
(goto-char (match-beginning 1))
(let ((end-bs (c-raw-string-pos)))
(setq c-new-END
(max c-new-END
(if (nth 4 end-bs)
(1+ (nth 4 end-bs))
eoll)))))
;; ...and clear `syntax-table' text propertes from the
;; following raw strings.
(c-depropertize-raw-strings-in-region (point) (1+ eoll)))
;; Remove the temporary string delimiter.
(goto-char end)
(delete-char 1))))
;; Have we just created a new starting id?
(goto-char (max (- beg 18) (point-min)))
(while
(and
(setq found
(search-forward-regexp c-c++-raw-string-opener-re
c-new-END 'bound))
(<= (match-end 0) beg)))
(when (and found (<= (match-beginning 0) end))
(setq c-new-BEG (min c-new-BEG (match-beginning 0)))
(c-depropertize-raw-strings-in-region c-new-BEG c-new-END))
;; Have we invalidated an opening delimiter by typing into it?
(when (and c-old-beg-rs
(eq (car c-old-beg-rs) 'open-delim)
(equal (c-get-char-property (cadr c-old-beg-rs)
'syntax-table)
'(1)))
(goto-char (1- (cadr c-old-beg-rs)))
(unless (looking-at c-c++-raw-string-opener-re)
(c-clear-char-property (1+ (point)) 'syntax-table)
(c-truncate-lit-pos-cache (1+ (point)))
(if (c-search-forward-char-property 'syntax-table '(15)
(c-point 'eol))
(c-clear-char-property (1- (point)) 'syntax-table))))
;; Have we matched up with an existing terminator by typing into an
;; opening delimiter? ... or by messing up a raw string's terminator so
;; that it now matches a later terminator?
(when
(or c-raw-string-end-delim-disrupted
(and c-old-beg-rs
(eq (car c-old-beg-rs) 'open-delim)))
(goto-char (cadr c-old-beg-rs))
(when (looking-at c-c++-raw-string-opener-1-re)
(setq id (match-string-no-properties 1))
(when (search-forward (concat ")" id "\"") nil t) ; No bound.
(setq c-new-END (point-max))
(c-clear-char-properties (cadr c-old-beg-rs) c-new-END
'syntax-table)
(c-truncate-lit-pos-cache (cadr c-old-beg-rs)))))
;; Have we terminated an existing raw string by inserting or removing
;; text?
(when (eq c-old-END-literality 'string)
;; Have we just made or modified a closing delimiter?
(goto-char (max (- beg 18) (point-min)))
(while
(and
(setq found
(search-forward-regexp ")\\([^ ()\\\n\r\t]\\{0,16\\}\\)\""
(+ end 17) t))
(< (match-end 0) beg)))
(when (and found (<= (match-beginning 0) end))
(setq id (match-string-no-properties 1))
(goto-char (match-beginning 0))
(while
(and
(setq found (search-backward (concat "R\"" id "(") nil t))
(setq state (c-semi-pp-to-literal (point)))
(memq (nth 3 (car state)) '(t ?\"))))
(when found
(setq c-new-BEG (min (point) c-new-BEG)
c-new-END (point-max))
(c-clear-syn-tab-properties (point) c-new-END)
(c-truncate-lit-pos-cache (point)))))
;; Are there any raw strings in a newly created macro?
(when (< beg end)
(goto-char beg)
(setq found-beg (point))
(when (search-forward-regexp c-anchored-cpp-prefix end t)
(c-end-of-macro)
(c-depropertize-raw-strings-in-region found-beg (point))))))
(defun c-maybe-re-mark-raw-string ()
;; When this function is called, point is immediately after a " which opens
;; a string. If this " is the characteristic " of a raw string
;; opener, apply the pertinent `syntax-table' text properties to the
;; entire raw string (when properly terminated) or just the delimiter
;; (otherwise). In either of these cases, return t, otherwise return nil.
;;
(let (in-macro macro-end)
(when
(and
(eq (char-before (1- (point))) ?R)
(looking-at "\\([^ ()\\\n\r\t]\\{0,16\\}\\)("))
(save-excursion
(setq in-macro (c-beginning-of-macro))
(setq macro-end (when in-macro
(c-end-of-macro)
(point) ;; (min (1+ (point)) (point-max))
)))
(when
(not
(c-propertize-raw-string-opener
(match-string-no-properties 1) ; id
(1- (point)) ; open quote
(match-end 1) ; open paren
macro-end)) ; bound (end of macro) or nil.
(goto-char (or macro-end (point-max))))
t)))
\f
;; Handling of small scale constructs like types and names.
;; Dynamically bound variable that instructs `c-forward-type' to also
;; treat possible types (i.e. those that it normally returns 'maybe or
;; 'found for) as actual types (and always return 'found for them).
;; This means that it records them in `c-record-type-identifiers' if
;; that is set, and that it adds them to `c-found-types'.
(defvar c-promote-possible-types nil)
;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
;; mark up successfully parsed arglists with paren syntax properties on
;; the surrounding angle brackets and with `c-<>-arg-sep' in the
;; `c-type' property of each argument separating comma.
;;
;; Setting this variable also makes `c-forward-<>-arglist' recurse into
;; all arglists for side effects (i.e. recording types), otherwise it
;; exploits any existing paren syntax properties to quickly jump to the
;; end of already parsed arglists.
;;
;; Marking up the arglists is not the default since doing that correctly
;; depends on a proper value for `c-restricted-<>-arglists'.
(defvar c-parse-and-markup-<>-arglists nil)
;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
;; not accept arglists that contain binary operators.
;;
;; This is primarily used to handle C++ template arglists. C++
;; disambiguates them by checking whether the preceding name is a
;; template or not. We can't do that, so we assume it is a template
;; if it can be parsed as one. That usually works well since
;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
;; in almost all cases would be pointless.
;;
;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
;; should let the comma separate the function arguments instead. And
;; in a context where the value of the expression is taken, e.g. in
;; "if (a < b || c > d)", it's probably not a template.
(defvar c-restricted-<>-arglists nil)
;; Dynamically bound variables that instructs
;; `c-forward-keyword-clause', `c-forward-<>-arglist',
;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
;; `c-forward-label' to record the ranges of all the type and
;; reference identifiers they encounter. They will build lists on
;; these variables where each element is a cons of the buffer
;; positions surrounding each identifier. This recording is only
;; activated when `c-record-type-identifiers' is non-nil.
;;
;; All known types that can't be identifiers are recorded, and also
;; other possible types if `c-promote-possible-types' is set.
;; Recording is however disabled inside angle bracket arglists that
;; are encountered inside names and other angle bracket arglists.
;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
;; instead.
;;
;; Only the names in C++ template style references (e.g. "tmpl" in
;; "tmpl<a,b>::foo") are recorded as references, other references
;; aren't handled here.
;;
;; `c-forward-label' records the label identifier(s) on
;; `c-record-ref-identifiers'.
(defvar c-record-type-identifiers nil)
(defvar c-record-ref-identifiers nil)
;; This variable will receive a cons cell of the range of the last
;; single identifier symbol stepped over by `c-forward-name' if it's
;; successful. This is the range that should be put on one of the
;; record lists above by the caller. It's assigned nil if there's no
;; such symbol in the name.
(defvar c-last-identifier-range nil)
(defmacro c-record-type-id (range)
(if (eq (car-safe range) 'cons)
;; Always true.
`(setq c-record-type-identifiers
(cons ,range c-record-type-identifiers))
`(let ((range ,range))
(if range
(setq c-record-type-identifiers
(cons range c-record-type-identifiers))))))
(defmacro c-record-ref-id (range)
(if (eq (car-safe range) 'cons)
;; Always true.
`(setq c-record-ref-identifiers
(cons ,range c-record-ref-identifiers))
`(let ((range ,range))
(if range
(setq c-record-ref-identifiers
(cons range c-record-ref-identifiers))))))
;; Dynamically bound variable that instructs `c-forward-type' to
;; record the ranges of types that only are found. Behaves otherwise
;; like `c-record-type-identifiers'.
(defvar c-record-found-types nil)
(defmacro c-forward-keyword-prefixed-id (type)
;; Used internally in `c-forward-keyword-clause' to move forward
;; over a type (if TYPE is 'type) or a name (otherwise) which
;; possibly is prefixed by keywords and their associated clauses.
;; Try with a type/name first to not trip up on those that begin
;; with a keyword. Return t if a known or found type is moved
;; over. The point is clobbered if nil is returned. If range
;; recording is enabled, the identifier is recorded on as a type
;; if TYPE is 'type or as a reference if TYPE is 'ref.
;;
;; This macro might do hidden buffer changes.
`(let (res)
(setq c-last-identifier-range nil)
(while (if (setq res ,(if (eq type 'type)
'(c-forward-type)
'(c-forward-name)))
nil
(cond ((looking-at c-keywords-regexp)
(c-forward-keyword-clause 1))
((and c-opt-cpp-prefix
(looking-at c-noise-macro-with-parens-name-re))
(c-forward-noise-clause)))))
(when (memq res '(t known found prefix maybe))
(when c-record-type-identifiers
,(if (eq type 'type)
'(c-record-type-id c-last-identifier-range)
'(c-record-ref-id c-last-identifier-range)))
t)))
(defmacro c-forward-id-comma-list (type update-safe-pos)
;; Used internally in `c-forward-keyword-clause' to move forward
;; over a comma separated list of types or names using
;; `c-forward-keyword-prefixed-id'.
;;
;; This macro might do hidden buffer changes.
`(while (and (progn
,(when update-safe-pos
'(setq safe-pos (point)))
(eq (char-after) ?,))
(progn
(forward-char)
(c-forward-syntactic-ws)
(c-forward-keyword-prefixed-id ,type)))))
(defun c-forward-noise-clause ()
;; Point is at a c-noise-macro-with-parens-names macro identifier. Go
;; forward over this name, any parenthesis expression which follows it, and
;; any syntactic WS, ending up at the next token. If there is an unbalanced
;; paren expression, leave point at it. Always Return t.
(c-forward-token-2)
(if (and (eq (char-after) ?\()
(c-go-list-forward))
(c-forward-syntactic-ws))
t)
(defun c-forward-noise-clause-not-macro-decl (maybe-parens)
;; Point is at a noise macro identifier, which, when MAYBE-PARENS is
;; non-nil, optionally takes paren arguments. Go forward over this name,
;; and when there may be optional parens, any parenthesis expression which
;; follows it, but DO NOT go over any macro declaration which may come
;; between them. Always return t.
(c-end-of-token)
(when maybe-parens
(let ((here (point)))
(c-forward-comments)
(if (not (and (eq (char-after) ?\()
(c-go-list-forward)))
(goto-char here))))
t)
(defun c-forward-keyword-clause (match)
;; Submatch MATCH in the current match data is assumed to surround a
;; token. If it's a keyword, move over it and any immediately
;; following clauses associated with it, stopping at the start of
;; the next token. t is returned in that case, otherwise the point
;; stays and nil is returned. The kind of clauses that are
;; recognized are those specified by `c-type-list-kwds',
;; `c-ref-list-kwds', `c-colon-type-list-kwds',
;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
;; `c-<>-arglist-kwds', and `c-protection-kwds'.
;;
;; This function records identifier ranges on
;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
;; `c-record-type-identifiers' is non-nil.
;;
;; Note that for `c-colon-type-list-kwds', which doesn't necessary
;; apply directly after the keyword, the type list is moved over
;; only when there is no unaccounted token before it (i.e. a token
;; that isn't moved over due to some other keyword list). The
;; identifier ranges in the list are still recorded if that should
;; be done, though.
;;
;; This function might do hidden buffer changes.
(let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
;; The call to `c-forward-<>-arglist' below is made after
;; `c-<>-sexp-kwds' keywords, so we're certain they actually
;; are angle bracket arglists and `c-restricted-<>-arglists'
;; should therefore be nil.
(c-parse-and-markup-<>-arglists t)
c-restricted-<>-arglists)
(when kwd-sym
(goto-char (match-end match))
(c-forward-syntactic-ws)
(setq safe-pos (point))
(cond
((and (c-keyword-member kwd-sym 'c-type-list-kwds)
(c-forward-keyword-prefixed-id type))
;; There's a type directly after a keyword in `c-type-list-kwds'.
(c-forward-id-comma-list type t))
((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
(c-forward-keyword-prefixed-id ref))
;; There's a name directly after a keyword in `c-ref-list-kwds'.
(c-forward-id-comma-list ref t))
((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
(eq (char-after) ?\())
;; There's an open paren after a keyword in `c-paren-any-kwds'.
(forward-char)
(when (and (setq pos (c-up-list-forward))
(eq (char-before pos) ?\)))
(when (and c-record-type-identifiers
(c-keyword-member kwd-sym 'c-paren-type-kwds))
;; Use `c-forward-type' on every identifier we can find
;; inside the paren, to record the types.
(while (c-syntactic-re-search-forward c-symbol-start pos t)
(goto-char (match-beginning 0))
(unless (c-forward-type)
(looking-at c-symbol-key) ; Always matches.
(goto-char (match-end 0)))))
(goto-char pos)
(c-forward-syntactic-ws)
(setq safe-pos (point))))
((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
(eq (char-after) ?<)
(c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
(c-forward-syntactic-ws)
(setq safe-pos (point)))
((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
(not (looking-at c-symbol-start))
(c-safe (c-forward-sexp) t))
(c-forward-syntactic-ws)
(setq safe-pos (point)))
((and (c-keyword-member kwd-sym 'c-protection-kwds)
(or (null c-post-protection-token)
(and (looking-at c-post-protection-token)
(save-excursion
(goto-char (match-end 0))
(not (c-end-of-current-token))))))
(if c-post-protection-token
(goto-char (match-end 0)))
(c-forward-syntactic-ws)
(setq safe-pos (point))))
(when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
(if (eq (char-after) ?:)
;; If we are at the colon already, we move over the type
;; list after it.
(progn
(forward-char)
(c-forward-syntactic-ws)
(when (c-forward-keyword-prefixed-id type)
(c-forward-id-comma-list type t)))
;; Not at the colon, so stop here. But the identifier
;; ranges in the type list later on should still be
;; recorded.
(and c-record-type-identifiers
(progn
;; If a keyword matched both one of the types above and
;; this one, we match `c-colon-type-list-re' after the
;; clause matched above.
(goto-char safe-pos)
(looking-at c-colon-type-list-re))
(progn
(goto-char (match-end 0))
(c-forward-syntactic-ws)
(c-forward-keyword-prefixed-id type))
;; There's a type after the `c-colon-type-list-re' match
;; after a keyword in `c-colon-type-list-kwds'.
(c-forward-id-comma-list type nil))))
(goto-char safe-pos)
t)))
;; cc-mode requires cc-fonts.
(declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
(defun c-forward-<>-arglist (all-types)
;; The point is assumed to be at a "<". Try to treat it as the open
;; paren of an angle bracket arglist and move forward to the
;; corresponding ">". If successful, the point is left after the
;; ">" and t is returned, otherwise the point isn't moved and nil is
;; returned. If ALL-TYPES is t then all encountered arguments in
;; the arglist that might be types are treated as found types.
;;
;; The variable `c-parse-and-markup-<>-arglists' controls how this
;; function handles text properties on the angle brackets and argument
;; separating commas.
;;
;; `c-restricted-<>-arglists' controls how lenient the template
;; arglist recognition should be.
;;
;; This function records identifier ranges on
;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
;; `c-record-type-identifiers' is non-nil.
;;
;; This function might do hidden buffer changes.
(let ((start (point))
(old-found-types (copy-hash-table c-found-types))
;; If `c-record-type-identifiers' is set then activate
;; recording of any found types that constitute an argument in
;; the arglist.
(c-record-found-types (if c-record-type-identifiers t)))
(if (catch 'angle-bracket-arglist-escape
(setq c-record-found-types
(c-forward-<>-arglist-recur all-types)))
(progn
(when (consp c-record-found-types)
(setq c-record-type-identifiers
;; `nconc' doesn't mind that the tail of
;; `c-record-found-types' is t.
(nconc c-record-found-types c-record-type-identifiers)))
t)
(setq c-found-types old-found-types)
(goto-char start)
nil)))
(defun c-forward-<>-arglist-recur (all-types)
;; Recursive part of `c-forward-<>-arglist'.
;;
;; This function might do hidden buffer changes.
(let ((start (point)) res pos
;; Cover this so that any recorded found type ranges are
;; automatically lost if it turns out to not be an angle
;; bracket arglist. It's propagated through the return value
;; on successful completion.
(c-record-found-types c-record-found-types)
;; List that collects the positions after the argument
;; separating ',' in the arglist.
arg-start-pos)
;; If the '<' has paren open syntax then we've marked it as an angle
;; bracket arglist before, so skip to the end.
(if (and (not c-parse-and-markup-<>-arglists)
(c-get-char-property (point) 'syntax-table))
(progn
(forward-char)
(if (and (c-go-up-list-forward)
(eq (char-before) ?>))
t
;; Got unmatched paren angle brackets. We don't clear the paren
;; syntax properties and retry, on the basis that it's very
;; unlikely that paren angle brackets become operators by code
;; manipulation. It's far more likely that it doesn't match due
;; to narrowing or some temporary change.
(goto-char start)
nil))
(forward-char) ; Forward over the opening '<'.
(unless (and (looking-at c-<-op-cont-regexp)
(not (looking-at c-<-pseudo-digraph-cont-regexp)))
;; go forward one non-alphanumeric character (group) per iteration of
;; this loop.
(while (and
(progn
(c-forward-syntactic-ws)
(when (or (and c-record-type-identifiers all-types)
(not (equal c-inside-<>-type-key
(concat
"\\(" regexp-unmatchable "\\)"))))
(c-forward-syntactic-ws)
(cond
((eq (char-after) ??)
(forward-char))
((and (looking-at c-identifier-start)
(not (looking-at c-keywords-regexp)))
(if (or (and all-types c-record-type-identifiers)
(c-major-mode-is 'java-mode))
;; All encountered identifiers are types, so set the
;; promote flag and parse the type.
(let ((c-promote-possible-types t)
(c-record-found-types t))
(c-forward-type))
(c-forward-over-token-and-ws))))
(c-forward-syntactic-ws)
(when (looking-at c-inside-<>-type-key)
(goto-char (match-end 1))
(c-forward-syntactic-ws)
(let ((c-promote-possible-types t)
(c-record-found-types t))
(c-forward-type))
(c-forward-syntactic-ws)))
(setq pos (point)) ; e.g. first token inside the '<'
;; Note: These regexps exploit the match order in \| so
;; that "<>" is matched by "<" rather than "[^>:-]>".
(c-syntactic-re-search-forward
;; Stop on ',', '|', '&', '+' and '-' to catch
;; common binary operators that could be between
;; two comparison expressions "a<b" and "c>d".
;; 2016-02-11: C++11 templates can now contain arithmetic
;; expressions, so template detection in C++ is now less
;; robust than it was.
c-<>-notable-chars-re
nil t t))
(cond
((eq (char-before) ?>)
;; Either an operator starting with '>' or the end of
;; the angle bracket arglist.
(if (save-excursion
(c-backward-token-2)
(looking-at c-multichar->-op-not->>->>>-regexp))
(progn
(goto-char (match-end 0))
t) ; Continue the loop.
;; The angle bracket arglist is finished.
(when c-parse-and-markup-<>-arglists
(while arg-start-pos
(c-put-c-type-property (1- (car arg-start-pos))
'c-<>-arg-sep)
(setq arg-start-pos (cdr arg-start-pos)))
(c-mark-<-as-paren start)
(c-mark->-as-paren (1- (point))))
(setq res t)
nil)) ; Exit the loop.
((eq (char-before) ?<)
;; Either an operator starting with '<' or a nested arglist.
(setq pos (point))
(let (id-start id-end subres keyword-match)
(cond
;; The '<' begins a multi-char operator.
((and (looking-at c-<-op-cont-regexp)
(not (looking-at c-<-pseudo-digraph-cont-regexp)))
(goto-char (match-end 0)))
;; We're at a nested <.....>
((progn
(backward-char) ; to the '<'
(and
(save-excursion
;; There's always an identifier before an angle
;; bracket arglist, or a keyword in `c-<>-type-kwds'
;; or `c-<>-arglist-kwds'.
(c-backward-syntactic-ws)
(setq id-end (point))
(c-simple-skip-symbol-backward)
(when (or (setq keyword-match
(looking-at c-opt-<>-sexp-key))
(not (looking-at c-keywords-regexp)))
(setq id-start (point))))
(setq subres
(let ((c-promote-possible-types t)
(c-record-found-types t))
(c-forward-<>-arglist-recur
(and keyword-match
(c-keyword-member
(c-keyword-sym (match-string 1))
'c-<>-type-kwds))))))
(or subres (goto-char pos))
subres)
;; It was an angle bracket arglist.
(setq c-record-found-types subres)
;; Record the identifier before the template as a type
;; or reference depending on whether the arglist is last
;; in a qualified identifier.
(when (and c-record-type-identifiers
(not keyword-match))
(if (and c-opt-identifier-concat-key
(progn
(c-forward-syntactic-ws)
(looking-at c-opt-identifier-concat-key)))
(c-record-ref-id (cons id-start id-end))
(c-record-type-id (cons id-start id-end)))))
;; At a "less than" operator.
(t
;; (forward-char) ; NO! We've already gone over the <.
)))
t) ; carry on looping.
((and
(eq (char-before) ?\()
(c-go-up-list-forward)
(eq (char-before) ?\))))
((and (not c-restricted-<>-arglists)
(or (and (eq (char-before) ?&)
(not (eq (char-after) ?&)))
(eq (char-before) ?,)))
;; Just another argument. Record the position. The
;; type check stuff that made us stop at it is at
;; the top of the loop.
(setq arg-start-pos (cons (point) arg-start-pos)))
(t
;; Got a character that can't be in an angle bracket
;; arglist argument. Abort using `throw', since
;; it's useless to try to find a surrounding arglist
;; if we're nested.
(throw 'angle-bracket-arglist-escape nil))))))
(if res
(or c-record-found-types t)))))
(defun c-backward-<>-arglist (all-types &optional limit)
;; The point is assumed to be directly after a ">". Try to treat it
;; as the close paren of an angle bracket arglist and move back to
;; the corresponding "<". If successful, the point is left at
;; the "<" and t is returned, otherwise the point isn't moved and
;; nil is returned. ALL-TYPES is passed on to
;; `c-forward-<>-arglist'.
;;
;; If the optional LIMIT is given, it bounds the backward search.
;; It's then assumed to be at a syntactically relevant position.
;;
;; This is a wrapper around `c-forward-<>-arglist'. See that
;; function for more details.
(let ((start (point)))
(backward-char)
(if (and (not c-parse-and-markup-<>-arglists)
(c-get-char-property (point) 'syntax-table))
(if (and (c-go-up-list-backward)
(eq (char-after) ?<))
t
;; See corresponding note in `c-forward-<>-arglist'.
(goto-char start)
nil)
(while (progn
(c-syntactic-skip-backward "^<;{}" limit t)
(and
(if (eq (char-before) ?<)
t
;; Stopped at bob or a char that isn't allowed in an
;; arglist, so we've failed.
(goto-char start)
nil)
(if (> (point)
(progn (c-beginning-of-current-token)
(point)))
;; If we moved then the "<" was part of some
;; multicharacter token.
t
(backward-char)
(let ((beg-pos (point)))
(if (c-forward-<>-arglist all-types)
(cond ((= (point) start)
;; Matched the arglist. Break the while.
(goto-char beg-pos)
nil)
((> (point) start)
;; We started from a non-paren ">" inside an
;; arglist.
(goto-char start)
nil)
(t
;; Matched a shorter arglist. Can be a nested
;; one so continue looking.
(goto-char beg-pos)
t))
t))))))
(/= (point) start))))
(defun c-forward-name ()
;; Move forward over a complete name if at the beginning of one,
;; stopping at the next following token. A keyword, as such,
;; doesn't count as a name. If the point is not at something that
;; is recognized as a name then it stays put.
;;
;; A name could be something as simple as "foo" in C or something as
;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
;; int>::*volatile const" in C++ (this function is actually little
;; more than a `looking-at' call in all modes except those that,
;; like C++, have `c-recognize-<>-arglists' set).
;;
;; Return
;; o - nil if no name is found;
;; o - 'template if it's an identifier ending with an angle bracket
;; arglist;
;; o - 'operator of it's an operator identifier;
;; o - t if it's some other kind of name.
;;
;; This function records identifier ranges on
;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
;; `c-record-type-identifiers' is non-nil.
;;
;; This function might do hidden buffer changes.
(let ((pos (point)) (start (point)) res id-start id-end
;; Turn off `c-promote-possible-types' here since we might
;; call `c-forward-<>-arglist' and we don't want it to promote
;; every suspect thing in the arglist to a type. We're
;; typically called from `c-forward-type' in this case, and
;; the caller only wants the top level type that it finds to
;; be promoted.
c-promote-possible-types)
(while
(and
(looking-at c-identifier-key)
(progn
;; Check for keyword. We go to the last symbol in
;; `c-identifier-key' first.
(goto-char (setq id-end (match-end 0)))
(c-simple-skip-symbol-backward)
(setq id-start (point))
(if (looking-at c-keywords-regexp)
(when (and (c-major-mode-is 'c++-mode)
(looking-at
(cc-eval-when-compile
(concat "\\(operator\\|\\(template\\)\\)"
"\\(" (c-lang-const c-nonsymbol-key c++)
"\\|$\\)")))
(if (match-beginning 2)
;; "template" is only valid inside an
;; identifier if preceded by "::".
(save-excursion
(c-backward-syntactic-ws)
(and (c-safe (backward-char 2) t)
(looking-at "::")))
t))
;; Handle a C++ operator or template identifier.
(goto-char id-end)
(c-forward-syntactic-ws)
(cond ((eq (char-before id-end) ?e)
;; Got "... ::template".
(let ((subres (c-forward-name)))
(when subres
(setq pos (point)
res subres))))
((and (looking-at c-identifier-start)
(or (not (looking-at
c-ambiguous-overloadable-or-identifier-prefix-re))
(save-excursion
(and (eq (c-forward-token-2) 0)
(not (eq (char-after) ?\())))))
;; Got a cast operator.
(when (c-forward-type)
(setq pos (point)
res 'operator)
;; Now we should match a sequence of either
;; '*', '&' or a name followed by ":: *",
;; where each can be followed by a sequence
;; of `c-opt-type-modifier-key'.
(while (cond ((looking-at "[*&]")
(goto-char (match-end 0))
t)
((looking-at c-identifier-start)
(and (c-forward-name)
(looking-at "::")
(progn
(goto-char (match-end 0))
(c-forward-syntactic-ws)
(eq (char-after) ?*))
(progn
(forward-char)
t))))
(while (progn
(c-forward-syntactic-ws)
(setq pos (point))
(looking-at c-opt-type-modifier-key))
(goto-char (match-end 1))))))
((looking-at c-overloadable-operators-regexp)
;; Got some other operator.
(setq c-last-identifier-range
(cons (point) (match-end 0)))
(goto-char (match-end 0))
(c-forward-syntactic-ws)
(setq pos (point)
res 'operator)))
nil)
;; `id-start' is equal to `id-end' if we've jumped over
;; an identifier that doesn't end with a symbol token.
;; That can occur e.g. for Java import directives on the
;; form "foo.bar.*".
(when (and id-start (/= id-start id-end))
(setq c-last-identifier-range
(cons id-start id-end)))
(goto-char id-end)
(c-forward-syntactic-ws)
(setq pos (point)
res t)))
(progn
(goto-char pos)
(when (or c-opt-identifier-concat-key
c-recognize-<>-arglists)
(cond
((and c-opt-identifier-concat-key
(looking-at c-opt-identifier-concat-key))
;; Got a concatenated identifier. This handles the
;; cases with tricky syntactic whitespace that aren't
;; covered in `c-identifier-key'.
(goto-char (match-end 0))
(c-forward-syntactic-ws)
t)
((and c-recognize-<>-arglists
(eq (char-after) ?<))
;; Maybe an angle bracket arglist.
(when (let (c-last-identifier-range)
(c-forward-<>-arglist nil))
(c-forward-syntactic-ws)
(unless (eq (char-after) ?\()
(setq c-last-identifier-range nil)
(c-add-type start (1+ pos)))
(setq pos (point))
(if (and c-opt-identifier-concat-key
(looking-at c-opt-identifier-concat-key))
;; Continue if there's an identifier concatenation
;; operator after the template argument.
(progn
(when (and c-record-type-identifiers id-start)
(c-record-ref-id (cons id-start id-end)))
(forward-char 2)
(c-forward-syntactic-ws)
t)
(when (and c-record-type-identifiers id-start
(not (eq (char-after) ?\()))
(c-record-type-id (cons id-start id-end)))
(setq res 'template)
nil)))
)))))
(goto-char pos)
res))
(defun c-forward-type (&optional brace-block-too)
;; Move forward over a type spec if at the beginning of one,
;; stopping at the next following token. The keyword "typedef"
;; isn't part of a type spec here.
;;
;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
;; The current (2009-03-10) intention is to convert all uses of
;; `c-forward-type' to call with this parameter set, then to
;; eliminate it.
;;
;; Return
;; o - t if it's a known type that can't be a name or other
;; expression;
;; o - 'known if it's an otherwise known type (according to
;; `*-font-lock-extra-types');
;; o - 'prefix if it's a known prefix of a type;
;; o - 'found if it's a type that matches one in `c-found-types';
;; o - 'maybe if it's an identifier that might be a type;
;; o - 'decltype if it's a decltype(variable) declaration; - or
;; o - 'no-id if "auto" precluded parsing a type identifier.
;; o - nil if it can't be a type (the point isn't moved then).
;;
;; The point is assumed to be at the beginning of a token.
;;
;; Note that this function doesn't skip past the brace definition
;; that might be considered part of the type, e.g.
;; "enum {a, b, c} foo".
;;
;; This function records identifier ranges on
;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
;; `c-record-type-identifiers' is non-nil.
;;
;; This function might do hidden buffer changes.
(when (and c-recognize-<>-arglists
(looking-at "<"))
(c-forward-<>-arglist t)
(c-forward-syntactic-ws))
(let ((start (point)) pos res name-res id-start id-end id-range)
;; Skip leading type modifiers. If any are found we know it's a
;; prefix of a type.
(when c-opt-type-modifier-prefix-key ; e.g. "const" "volatile", but NOT "typedef"
(while (looking-at c-opt-type-modifier-prefix-key)
(when (looking-at c-no-type-key)
(setq res 'no-id))
(goto-char (match-end 1))
(c-forward-syntactic-ws)
(or (eq res 'no-id)
(setq res 'prefix))))
(cond
((looking-at c-typeof-key) ; e.g. C++'s "decltype".
(goto-char (match-end 1))
(c-forward-syntactic-ws)
(setq res (and (eq (char-after) ?\()
(c-safe (c-forward-sexp))
'decltype))
(if res
(c-forward-syntactic-ws)
(goto-char start)))
((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
; "typedef".
(goto-char (match-end 1))
(c-forward-syntactic-ws)
(while (cond
((looking-at c-decl-hangon-key)
(c-forward-keyword-clause 1))
((looking-at c-pack-key)
(goto-char (match-end 1))
(c-forward-syntactic-ws))
((and c-opt-cpp-prefix
(looking-at c-noise-macro-with-parens-name-re))
(c-forward-noise-clause))))
(setq pos (point))
(setq name-res (c-forward-name))
(setq res (not (null name-res)))
(when (eq name-res t)
;; In many languages the name can be used without the
;; prefix, so we add it to `c-found-types'.
(c-add-type pos (point))
(when (and c-record-type-identifiers
c-last-identifier-range)
(c-record-type-id c-last-identifier-range)))
(when (and brace-block-too
(memq res '(t nil))
(eq (char-after) ?\{)
(save-excursion
(c-safe
(progn (c-forward-sexp)
(c-forward-syntactic-ws)
(setq pos (point))))))
(goto-char pos)
(setq res t))
(unless res (goto-char start))) ; invalid syntax
((and
(not (eq res 'no-id))
(progn
(setq pos nil)
(if (looking-at c-identifier-start)
(save-excursion
(setq id-start (point)
name-res (c-forward-name))
(when name-res
(setq id-end (point)
id-range c-last-identifier-range))))
(and (cond ((looking-at c-primitive-type-key)
(setq res t))
((c-with-syntax-table c-identifier-syntax-table
(looking-at c-known-type-key))
(setq res 'known)))
(or (not id-end)
(>= (save-excursion
(save-match-data
(goto-char (match-end 1))
(c-forward-syntactic-ws)
(setq pos (point))))
id-end)
(setq res nil)))))
;; Looking at a primitive or known type identifier. We've
;; checked for a name first so that we don't go here if the
;; known type match only is a prefix of another name.
(setq id-end (match-end 1))
(when (and c-record-type-identifiers
(or c-promote-possible-types (eq res t)))
(c-record-type-id (cons (match-beginning 1) (match-end 1))))
(if (and c-opt-type-component-key
(save-match-data
(looking-at c-opt-type-component-key)))
;; There might be more keywords for the type.
(let (safe-pos)
(c-forward-keyword-clause 1)
(while (progn
(setq safe-pos (point))
(looking-at c-opt-type-component-key))
(when (and c-record-type-identifiers
(looking-at c-primitive-type-key))
(c-record-type-id (cons (match-beginning 1)
(match-end 1))))
(c-forward-keyword-clause 1))
(if (looking-at c-primitive-type-key)
(progn
(when c-record-type-identifiers
(c-record-type-id (cons (match-beginning 1)
(match-end 1))))
(c-forward-keyword-clause 1)
(setq res t))
(goto-char safe-pos)
(setq res 'prefix)))
(unless (save-match-data (c-forward-keyword-clause 1))
(if pos
(goto-char pos)
(goto-char (match-end 1))
(c-forward-syntactic-ws)))))
(name-res
(cond ((eq name-res t)
;; A normal identifier.
(goto-char id-end)
(if (or res c-promote-possible-types)
(progn
(c-add-type id-start id-end)
(when (and c-record-type-identifiers id-range)
(c-record-type-id id-range))
(unless res
(setq res 'found)))
(setq res (if (c-check-qualified-type id-start)
;; It's an identifier that has been used as
;; a type somewhere else.
'found
;; It's an identifier that might be a type.
'maybe))))
((eq name-res 'template)
;; A template is sometimes a type.
(goto-char id-end)
(c-forward-syntactic-ws)
(setq res
(if (eq (char-after) ?\()
(if (c-check-qualified-type id-start)
;; It's an identifier that has been used as
;; a type somewhere else.
'found
;; It's an identifier that might be a type.
'maybe)
t)))
(t
;; Otherwise it's an operator identifier, which is not a type.
(goto-char start)
(setq res nil)))))
(when (not (memq res '(nil no-id)))
;; Skip trailing type modifiers. If any are found we know it's
;; a type.
(when c-opt-type-modifier-key
(while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
(goto-char (match-end 1))
(c-forward-syntactic-ws)
(setq res t)))
;; Step over any type suffix operator. Do not let the existence
;; of these alter the classification of the found type, since
;; these operators typically are allowed in normal expressions
;; too.
(when c-opt-type-suffix-key ; e.g. "..."
(while (looking-at c-opt-type-suffix-key)
(goto-char (match-end 1))
(c-forward-syntactic-ws)))
;; Skip any "WS" identifiers (e.g. "final" or "override" in C++)
(while (looking-at c-type-decl-suffix-ws-ids-key)
(goto-char (match-end 1))
(c-forward-syntactic-ws)
(setq res t))
(when c-opt-type-concat-key ; Only/mainly for pike.
;; Look for a trailing operator that concatenates the type
;; with a following one, and if so step past that one through
;; a recursive call. Note that we don't record concatenated
;; types in `c-found-types' - it's the component types that
;; are recorded when appropriate.
(setq pos (point))
(let* ((c-promote-possible-types (or (memq res '(t known))
c-promote-possible-types))
;; If we can't promote then set `c-record-found-types' so that
;; we can merge in the types from the second part afterwards if
;; it turns out to be a known type there.
(c-record-found-types (and c-record-type-identifiers
(not c-promote-possible-types)))
subres)
(if (and (looking-at c-opt-type-concat-key)
(progn
(goto-char (match-end 1))
(c-forward-syntactic-ws)
(setq subres (c-forward-type))))
(progn
;; If either operand certainly is a type then both are, but we
;; don't let the existence of the operator itself promote two
;; uncertain types to a certain one.
(cond ((eq res t))
((eq subres t)
(unless (eq name-res 'template)
(c-add-type id-start id-end))
(when (and c-record-type-identifiers id-range)
(c-record-type-id id-range))
(setq res t))
((eq res 'known))
((eq subres 'known)
(setq res 'known))
((eq res 'found))
((eq subres 'found)
(setq res 'found))
(t
(setq res 'maybe)))
(when (and (eq res t)
(consp c-record-found-types))
;; Merge in the ranges of any types found by the second
;; `c-forward-type'.
(setq c-record-type-identifiers
;; `nconc' doesn't mind that the tail of
;; `c-record-found-types' is t.
(nconc c-record-found-types
c-record-type-identifiers))))
(goto-char pos))))
(when (and c-record-found-types (memq res '(known found)) id-range)
(setq c-record-found-types
(cons id-range c-record-found-types))))
;;(message "c-forward-type %s -> %s: %s" start (point) res)
res))
(defun c-forward-annotation ()
;; Used for Java code only at the moment. Assumes point is on the @, moves
;; forward an annotation and returns t. Leaves point unmoved and returns
;; nil if there is no annotation at point.
(let ((pos (point)))
(or
(and (looking-at "@")
(not (looking-at c-keywords-regexp))
(progn (forward-char) t)
(looking-at c-symbol-key)
(progn (goto-char (match-end 0))
(c-forward-syntactic-ws)
t)
(if (looking-at "(")
(c-go-list-forward)
t))
(progn (goto-char pos) nil))))
(defmacro c-pull-open-brace (ps)
;; Pull the next open brace from PS (which has the form of paren-state),
;; skipping over any brace pairs. Returns NIL when PS is exhausted.
`(progn
(while (consp (car ,ps))
(setq ,ps (cdr ,ps)))
(prog1 (car ,ps)
(setq ,ps (cdr ,ps)))))
(defun c-forward-over-compound-identifier ()
;; Go over a possibly compound identifier, such as C++'s Foo::Bar::Baz,
;; returning that identifier (with any syntactic WS removed). Return nil if
;; we're not at an identifier.
(when (c-on-identifier)
(let ((consolidated "") (consolidated-:: "")
start end)
(while
(progn
(setq start (point))
(c-forward-over-token)
(setq consolidated
(concat consolidated-::
(buffer-substring-no-properties start (point))))
(c-forward-syntactic-ws)
(and c-opt-identifier-concat-key
(looking-at c-opt-identifier-concat-key)
(progn
(setq start (point))
(c-forward-over-token)
(setq end (point))
(c-forward-syntactic-ws)
(and
(c-on-identifier)
(setq consolidated-::
(concat consolidated
(buffer-substring-no-properties start end))))))))
(if (equal consolidated "")
nil
consolidated))))
(defun c-back-over-compound-identifier ()
;; Point is putatively just after a "compound identifier", i.e. something
;; looking (in C++) like this "FQN::of::base::Class". Move to the start of
;; this construct and return t. If the parsing fails, return nil, leaving
;; point unchanged.
(let (end)
(if (not (c-on-identifier))
nil
(c-simple-skip-symbol-backward)
(while
(progn
(setq end (point))
(c-backward-syntactic-ws)
(c-backward-token-2)
(and
c-opt-identifier-concat-key
(looking-at c-opt-identifier-concat-key)
(progn
(c-backward-syntactic-ws)
(c-simple-skip-symbol-backward))))
(setq end (point)))
(goto-char end)
t)))
(defun c-check-qualified-type (from)
;; Look up successive tails of a (possibly) qualified type in
;; `c-found-types'. If one of them matches, return it, else return nil.
(save-excursion
(goto-char from)
(let ((compound (c-forward-over-compound-identifier)))
(when compound
(while (and c-opt-identifier-concat-key
(> (length compound) 0)
(not (gethash compound c-found-types))
(string-match c-opt-identifier-concat-key compound))
(setq compound (substring compound (match-end 0))))
(and (gethash compound c-found-types)
compound)))))
(defun c-back-over-member-initializer-braces ()
;; Point is just after a closing brace/parenthesis. Try to parse this as a
;; C++ member initializer list, going back to just after the introducing ":"
;; and returning t. Otherwise return nil, leaving point unchanged.
(let ((here (point)) res)
(setq res
(catch 'done
(when (not (c-go-list-backward))
(throw 'done nil))
(c-backward-syntactic-ws)
(when (not (c-back-over-compound-identifier))
(throw 'done nil))
(c-backward-syntactic-ws)
(while (eq (char-before) ?,)
(backward-char)
(c-backward-syntactic-ws)
(when (not (memq (char-before) '(?\) ?})))
(throw 'done nil))
(when (not (c-go-list-backward))
(throw 'done nil))
(c-backward-syntactic-ws)
(when (not (c-back-over-compound-identifier))
(throw 'done nil))
(c-backward-syntactic-ws))
(eq (char-before) ?:)))
(or res (goto-char here))
res))
(defmacro c-back-over-list-of-member-inits ()
;; Go back over a list of elements, each looking like:
;; <symbol> (<expression>) ,
;; or <symbol> {<expression>} , (with possibly a <....> expressions
;; following the <symbol>).
;; when we are putatively immediately after a comma. Stop when we don't see
;; a comma. If either of <symbol> or bracketed <expression> is missing,
;; throw nil to 'level. If the terminating } or ) is unmatched, throw nil
;; to 'done. This is not a general purpose macro!
'(while (eq (char-before) ?,)
(backward-char)
(c-backward-syntactic-ws)
(when (not (memq (char-before) '(?\) ?})))
(throw 'level nil))
(when (not (c-go-list-backward))
(throw 'done nil))
(c-backward-syntactic-ws)
(while (eq (char-before) ?>)
(when (not (c-backward-<>-arglist nil))
(throw 'done nil))
(c-backward-syntactic-ws))
(when (not (c-back-over-compound-identifier))
(throw 'level nil))
(c-backward-syntactic-ws)))
(defun c-back-over-member-initializers (&optional limit)
;; Test whether we are in a C++ member initializer list, and if so, go back
;; to the introducing ":", returning the position of the opening paren of
;; the function's arglist. Otherwise return nil, leaving point unchanged.
;; LIMIT, if non-nil, is a limit for the backward search.
(save-restriction
(let ((here (point))
(paren-state (c-parse-state)) ; Do this outside the narrowing for
; performance reasons.
pos level-plausible at-top-level res)
(if limit (narrow-to-region limit (point)))
;; Assume tentatively that we're at the top level. Try to go back to the
;; colon we seek.
(setq res
(catch 'done
(setq level-plausible
(catch 'level
(c-backward-syntactic-ws)
(when (memq (char-before) '(?\) ?}))
(when (not (c-go-list-backward))
(throw 'done nil))
(c-backward-syntactic-ws))
(when (c-back-over-compound-identifier)
(c-backward-syntactic-ws))
(c-back-over-list-of-member-inits)
(and (eq (char-before) ?:)
(save-excursion
(c-backward-token-2)
(not (looking-at c-:$-multichar-token-regexp)))
(c-just-after-func-arglist-p))))
(while (and (not (and level-plausible
(setq at-top-level (c-at-toplevel-p))))
(setq pos (c-pull-open-brace paren-state)) ; might be a paren.
(or (null limit) (>= pos limit)))
(setq level-plausible
(catch 'level
(goto-char pos)
(c-backward-syntactic-ws)
(when (not (c-back-over-compound-identifier))
(throw 'level nil))
(c-backward-syntactic-ws)
(c-back-over-list-of-member-inits)
(and (eq (char-before) ?:)
(save-excursion
(c-backward-token-2)
(not (looking-at c-:$-multichar-token-regexp)))
(c-just-after-func-arglist-p)))))
(and at-top-level level-plausible)))
(or res (goto-char here))
res)))
(defun c-forward-class-decl ()
"From the beginning of a struct/union, etc. move forward to
after the brace block which defines it, leaving point at the
start of the next token and returning point. On failure leave
point unchanged and return nil."
(let ((here (point)))
(if
(and
(looking-at c-class-key)
(eq (c-forward-token-2) 0)
(c-on-identifier)
(eq (c-forward-token-2) 0)
(eq (char-after) ?{)
(c-go-list-forward))
(progn
(c-forward-syntactic-ws)
(point))
(goto-char here)
nil)))
\f
;; Handling of large scale constructs like statements and declarations.
(defun c-forward-declarator (&optional limit accept-anon)
;; Assuming point is at the start of a declarator, move forward over it,
;; leaving point at the next token after it (e.g. a ) or a ; or a ,).
;;
;; Return a list (ID-START ID-END BRACKETS-AFTER-ID GOT-INIT DECORATED),
;; where ID-START and ID-END are the bounds of the declarator's identifier,
;; and BRACKETS-AFTER-ID is non-nil if a [...] pair is present after the id.
;; GOT-INIT is non-nil when the declarator is followed by "=" or "(",
;; DECORATED is non-nil when the identifier is embellished by an operator,
;; like "*x", or "(*x)".
;;
;; If ACCEPT-ANON is non-nil, move forward over any "anonymous declarator",
;; i.e. something like the (*) in int (*), such as might be found in a
;; declaration. In such a case ID-START and ID-END in the return value are
;; both set to nil. A "null" "anonymous declarator" gives a non-nil result.
;;
;; If no declarator is found, leave point unmoved and return nil. LIMIT is
;; an optional limit for forward searching.
;;
;; Note that the global variable `c-last-identifier-range' is written to, so
;; the caller should bind it if necessary.
;; Inside the following "condition form", we move forward over the
;; declarator's identifier up as far as any opening bracket (for array
;; size) or paren (for parameters of function-type) or brace (for
;; array/struct initialization) or "=" or terminating delimiter
;; (e.g. "," or ";" or "}").
(let ((here (point))
id-start id-end brackets-after-id paren-depth decorated)
(or limit (setq limit (point-max)))
(if (and
(< (point) limit)
;; The following form moves forward over the declarator's
;; identifier (and what precedes it), returning t. If there
;; wasn't one, it returns nil.
(let (got-identifier)
(setq paren-depth 0)
;; Skip over type decl prefix operators, one for each iteration
;; of the while. These are, e.g. "*" in "int *foo" or "(" and
;; "*" in "int (*foo) (void)" (Note similar code in
;; `c-forward-decl-or-cast-1'.)
(while
(cond
((looking-at c-decl-hangon-key)
(c-forward-keyword-clause 1))
((and c-opt-cpp-prefix
(looking-at c-noise-macro-with-parens-name-re))
(c-forward-noise-clause))
((and (looking-at c-type-decl-prefix-key)
(if (and (c-major-mode-is 'c++-mode)
(match-beginning 3))
;; If the third submatch matches in C++ then
;; we're looking at an identifier that's a
;; prefix only if it specifies a member pointer.
(progn
(setq id-start (point))
(c-forward-name)
(if (looking-at "\\(::\\)")
;; We only check for a trailing "::" and
;; let the "*" that should follow be
;; matched in the next round.
t
;; It turned out to be the real identifier,
;; so flag that and stop.
(setq got-identifier t)
nil))
t))
(if (looking-at c-type-decl-operator-prefix-key)
(setq decorated t))
(if (eq (char-after) ?\()
(progn
(setq paren-depth (1+ paren-depth))
(forward-char))
(goto-char (match-end 1)))
(c-forward-syntactic-ws)
t)))
;; If we haven't passed the identifier already, do it now.
(unless got-identifier
(setq id-start (point)))
(cond
((or got-identifier
(c-forward-name))
(save-excursion
(c-backward-syntactic-ws)
(setq id-end (point))))
(accept-anon
(setq id-start nil id-end nil)
t)
(t (/= (point) here))))
;; Skip out of the parens surrounding the identifier. If closing
;; parens are missing, this form returns nil.
(or (= paren-depth 0)
(c-safe (goto-char (scan-lists (point) 1 paren-depth))))
(<= (point) limit)
;; Skip over any trailing bit, such as "__attribute__".
(progn
(while (cond
((looking-at c-decl-hangon-key)
(c-forward-keyword-clause 1))
((and c-opt-cpp-prefix
(looking-at c-noise-macro-with-parens-name-re))
(c-forward-noise-clause))))
(<= (point) limit))
;; Search syntactically to the end of the declarator (";",
;; ",", a closing paren, eob etc) or to the beginning of an
;; initializer or function prototype ("=" or "\\s(").
;; Note that square brackets are now not also treated as
;; initializers, since this broke when there were also
;; initializing brace lists.
(let (found)
(while
(and (< (point) limit)
(progn
;; In the next loop, we keep searching forward whilst
;; we find ":"s which aren't single colons inside C++
;; "for" statements.
(while
(and
(< (point) limit)
(setq found
(c-syntactic-re-search-forward
"[;:,]\\|\\s)\\|\\(=\\|\\s(\\)"
limit t t))
(eq (char-before) ?:)
(if (looking-at c-:-op-cont-regexp)
(progn (goto-char (match-end 0)) t)
(not
(and (c-major-mode-is '(c++-mode java-mode))
(save-excursion
(and
(c-go-up-list-backward)
(eq (char-after) ?\()
(progn (c-backward-syntactic-ws)
(c-simple-skip-symbol-backward))
(looking-at c-paren-stmt-key))))))))
found)
(eq (char-before) ?\[)
(c-go-up-list-forward))
(setq brackets-after-id t))
(when found (backward-char))
(<= (point) limit)))
(list id-start id-end brackets-after-id (match-beginning 1) decorated)
(goto-char here)
nil)))
(defun c-do-declarators
(cdd-limit cdd-list cdd-not-top cdd-comma-prop cdd-function)
"Assuming point is at the start of a comma separated list of declarators,
apply CDD-FUNCTION to each declarator (when CDD-LIST is non-nil) or just the
first declarator (when CDD-LIST is nil). When CDD-FUNCTION is nil, no
function is applied.
CDD-FUNCTION is supplied with 6 arguments:
0. The start position of the declarator's identifier;
1. The end position of this identifier;
\[Note: if there is no identifier, as in int (*);, both of these are nil.]
2. The position of the next token after the declarator (CLARIFY!!!).
3. CDD-NOT-TOP;
4. Non-nil if the identifier is of a function.
5. When there is an initialization following the declarator (such as \"=
....\" or \"( ....\".), the character which introduces this initialization,
otherwise nil.
Additionally, if CDD-COMMA-PROP is non-nil, mark the separating commas with
this value of the c-type property, when CDD-LIST is non-nil.
Stop at or before CDD-LIMIT (which may NOT be nil).
If CDD-NOT-TOP is non-nil, we are not at the top-level (\"top-level\" includes
being directly inside a class or namespace, etc.).
Return non-nil if we've reached the token after the last declarator (often a
semicolon, or a comma when CDD-LIST is nil); otherwise (when we hit CDD-LIMIT,
or fail otherwise) return nil, leaving point at the beginning of the putative
declarator that could not be processed.
This function might do hidden buffer changes."
;; N.B.: We use the "cdd-" prefix in this routine to try to prevent
;; confusion with possible reference to common variable names from within
;; CDD-FUNCTION.
(let
((cdd-pos (point)) cdd-next-pos cdd-id-start cdd-id-end
cdd-decl-res cdd-got-func cdd-got-type cdd-got-init
c-last-identifier-range cdd-exhausted cdd-after-block)
;; The following `while' applies `cdd-function' to a single declarator id
;; each time round. It loops only when CDD-LIST is non-nil.
(while
(and (not cdd-exhausted)
(setq cdd-decl-res (c-forward-declarator cdd-limit)))
(setq cdd-next-pos (point)
cdd-id-start (car cdd-decl-res)
cdd-id-end (cadr cdd-decl-res)
cdd-got-func (and (eq (char-after) ?\()
(or (not (c-major-mode-is 'c++-mode))
(not cdd-not-top)
(car (cddr (cddr cdd-decl-res))) ; Id is in
; parens, etc.
(save-excursion
(forward-char)
(c-forward-syntactic-ws)
(looking-at "[*&]")))
(not (car (cddr cdd-decl-res)))
(or (not (c-major-mode-is 'c++-mode))
(save-excursion
(let (c-last-identifier-range)
(forward-char)
(c-forward-syntactic-ws)
(catch 'is-function
(while
(progn
(while
(cond
((looking-at c-decl-hangon-key)
(c-forward-keyword-clause 1))
((looking-at c-noise-macro-with-parens-name-re)
(c-forward-noise-clause))))
(if (eq (char-after) ?\))
(throw 'is-function t))
(setq cdd-got-type (c-forward-type))
(cond
((null cdd-got-type)
(throw 'is-function nil))
((not (eq cdd-got-type 'maybe))
(throw 'is-function t)))
(c-forward-declarator nil t)
(eq (char-after) ?,))
(forward-char)
(c-forward-syntactic-ws))
t)))))
cdd-got-init (and (cadr (cddr cdd-decl-res))
(char-after)))
;; Jump past any initializer or function prototype to see if
;; there's a ',' to continue at.
(cond (cdd-got-func
;; Skip a parenthesized initializer (C++) or a function
;; prototype.
(if (c-go-list-forward (point) cdd-limit) ; over the parameter list.
(c-forward-syntactic-ws cdd-limit)
(setq cdd-exhausted t))) ; unbalanced parens
(cdd-got-init ; "=" sign OR opening "(", "[", or "("
;; Skip an initializer expression in braces, whether or not (in
;; C++ Mode) preceded by an "=". Be careful that the brace list
;; isn't a code block or a struct (etc.) block.
(cond
((and (eq cdd-got-init ?=)
(zerop (c-forward-token-2 1 nil cdd-limit))
(eq (char-after) ?{)
(c-go-list-forward (point) cdd-limit)))
((and (eq cdd-got-init ?{)
c-recognize-bare-brace-inits
(setq cdd-after-block
(save-excursion
(c-go-list-forward (point) cdd-limit)))
(not (c-looking-at-statement-block)))
(goto-char cdd-after-block)))
(if (c-syntactic-re-search-forward "[;,{]" cdd-limit 'move t)
(backward-char)
(setq cdd-exhausted t)))
(t (c-forward-syntactic-ws cdd-limit)))
(if cdd-function
(funcall cdd-function cdd-id-start cdd-id-end cdd-next-pos
cdd-not-top cdd-got-func cdd-got-init))
;; If a ',' is found we set cdd-pos to the next declarator and iterate.
(if (and cdd-list (< (point) cdd-limit) (looking-at ","))
(progn
(when cdd-comma-prop
(c-put-char-property (point) 'c-type cdd-comma-prop))
(forward-char)
(c-forward-syntactic-ws cdd-limit)
(setq cdd-pos (point)))
(setq cdd-exhausted t)))
(if (> (point) cdd-pos)
t
(goto-char cdd-pos)
nil)))
;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
;; defsubst or perhaps even a defun, but it contains lots of free
;; variables that refer to things inside `c-forward-decl-or-cast-1'.
(defmacro c-fdoc-shift-type-backward (&optional short)
;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
;; of types when parsing a declaration, which means that it
;; sometimes consumes the identifier in the declaration as a type.
;; This is used to "backtrack" and make the last type be treated as
;; an identifier instead.
`(progn
,(unless short
;; These identifiers are bound only in the inner let.
'(setq identifier-type at-type
identifier-start type-start
got-parens nil
got-identifier t
got-suffix t
got-suffix-after-parens id-start
paren-depth 0))
(if (setq at-type (if (eq backup-at-type 'prefix)
t
backup-at-type))
(setq type-start backup-type-start
id-start backup-id-start)
(setq type-start start-pos
id-start start-pos))
;; When these flags already are set we've found specifiers that
;; unconditionally signal these attributes - backtracking doesn't
;; change that. So keep them set in that case.
(or at-type-decl
(setq at-type-decl backup-at-type-decl))
(or maybe-typeless
(setq maybe-typeless backup-maybe-typeless))
,(unless short
;; This identifier is bound only in the inner let.
'(setq start id-start))))
(defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
;; Move forward over a declaration or a cast if at the start of one.
;; The point is assumed to be at the start of some token. Nil is
;; returned if no declaration or cast is recognized, and the point
;; is clobbered in that case.
;;
;; If a declaration is parsed:
;;
;; The point is left at the first token after the first complete
;; declarator, if there is one. The return value is a list of 5 elements,
;; where the first is the position of the first token in the declarator.
;; (See below for the other four.)
;; Some examples:
;;
;; void foo (int a, char *b) stuff ...
;; car ^ ^ point
;; float (*a)[], b;
;; car ^ ^ point
;; unsigned int a = c_style_initializer, b;
;; car ^ ^ point
;; unsigned int a (cplusplus_style_initializer), b;
;; car ^ ^ point (might change)
;; class Foo : public Bar {}
;; car ^ ^ point
;; class PikeClass (int a, string b) stuff ...
;; car ^ ^ point
;; enum bool;
;; car ^ ^ point
;; enum bool flag;
;; car ^ ^ point
;; void cplusplus_function (int x) throw (Bad);
;; car ^ ^ point
;; Foo::Foo (int b) : Base (b) {}
;; car ^ ^ point
;;
;; auto foo = 5;
;; car ^ ^ point
;; auto cplusplus_11 (int a, char *b) -> decltype (bar):
;; car ^ ^ point
;;
;;
;;
;; The second element of the return value is non-nil when a
;; `c-typedef-decl-kwds' specifier is found in the declaration.
;; Specifically it is a dotted pair (A . B) where B is t when a
;; `c-typedef-kwds' ("typedef") is present, and A is t when some
;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
;; specifier is present. I.e., (some of) the declared
;; identifier(s) are types.
;;
;; The third element of the return value is non-nil when the declaration
;; parsed might be an expression. The fourth element is the position of
;; the start of the type identifier, or the same as the first element when
;; there is no type identifier. The fifth element is t if either CONTEXT
;; was 'top, or the declaration is detected to be treated as top level
;; (e.g. with the keyword "extern").
;;
;; If a cast is parsed:
;;
;; The point is left at the first token after the closing paren of
;; the cast. The return value is `cast'. Note that the start
;; position must be at the first token inside the cast parenthesis
;; to recognize it.
;;
;; PRECEDING-TOKEN-END is the first position after the preceding
;; token, i.e. on the other side of the syntactic ws from the point.
;; Use a value less than or equal to (point-min) if the point is at
;; the first token in (the visible part of) the buffer.
;;
;; CONTEXT is a symbol that describes the context at the point:
;; 'decl In a comma-separated declaration context (typically
;; inside a function declaration arglist).
;; '<> In an angle bracket arglist.
;; 'arglist Some other type of arglist.
;; 'top Some other context and point is at the top-level (either
;; outside any braces or directly inside a class or namespace,
;; etc.)
;; nil Some other context or unknown context. Includes
;; within the parens of an if, for, ... construct.
;; 'not-decl This value is never supplied to this function. It
;; would mean we're definitely not in a declaration.
;;
;; LAST-CAST-END is the first token after the closing paren of a
;; preceding cast, or nil if none is known. If
;; `c-forward-decl-or-cast-1' is used in succession, it should be
;; the position after the closest preceding call where a cast was
;; matched. In that case it's used to discover chains of casts like
;; "(a) (b) c".
;;
;; This function records identifier ranges on
;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
;; `c-record-type-identifiers' is non-nil.
;;
;; This function might do hidden buffer changes.
(let (;; `start-pos' is used below to point to the start of the
;; first type, i.e. after any leading specifiers. It might
;; also point at the beginning of the preceding syntactic
;; whitespace.
(start-pos (point))
;; Set to the result of `c-forward-type'.
at-type
;; The position of the first token in what we currently
;; believe is the type in the declaration or cast, after any
;; specifiers and their associated clauses.
type-start
;; The position of the first token in what we currently
;; believe is the declarator for the first identifier. Set
;; when the type is found, and moved forward over any
;; `c-decl-hangon-kwds' and their associated clauses that
;; occurs after the type.
id-start
;; These store `at-type', `type-start' and `id-start' of the
;; identifier before the one in those variables. The previous
;; identifier might turn out to be the real type in a
;; declaration if the last one has to be the declarator in it.
;; If `backup-at-type' is nil then the other variables have
;; undefined values.
backup-at-type backup-type-start backup-id-start
;; Set if we've found a specifier (apart from "typedef") that makes
;; the defined identifier(s) types.
at-type-decl
;; Set if we've a "typedef" keyword.
at-typedef
;; Set if we've found a specifier that can start a declaration
;; where there's no type.
maybe-typeless
;; Save the value of kwd-sym between loops of the "Check for a
;; type" loop. Needed to distinguish a C++11 "auto" from a pre
;; C++11 one. (Commented out, 2020-11-01).
;; prev-kwd-sym
;; If a specifier is found that also can be a type prefix,
;; these flags are set instead of those above. If we need to
;; back up an identifier, they are copied to the real flag
;; variables. Thus they only take effect if we fail to
;; interpret it as a type.
backup-at-type-decl backup-maybe-typeless
;; Whether we've found a declaration or a cast. We might know
;; this before we've found the type in it. It's 'ids if we've
;; found two consecutive identifiers (usually a sure sign, but
;; we should allow that in labels too), and t if we've found a
;; specifier keyword (a 100% sure sign).
at-decl-or-cast
;; Set when we need to back up to parse this as a declaration
;; but not as a cast.
backup-if-not-cast
;; For casts, the return position.
cast-end
;; Have we got a new-style C++11 "auto"?
new-style-auto
;; Set when the symbol before `preceding-token-end' is known to
;; terminate the previous construct, or when we're at point-min.
at-decl-start
;; Set when we have encountered a keyword (e.g. "extern") which
;; causes the following declaration to be treated as though top-level.
make-top
;; Save `c-record-type-identifiers' and
;; `c-record-ref-identifiers' since ranges are recorded
;; speculatively and should be thrown away if it turns out
;; that it isn't a declaration or cast.
(save-rec-type-ids c-record-type-identifiers)
(save-rec-ref-ids c-record-ref-identifiers)
;; Set when we parse a declaration which might also be an expression,
;; such as "a *b". See CASE 16 and CASE 17.
maybe-expression)
(save-excursion
(goto-char preceding-token-end)
(setq at-decl-start
(or (bobp)
(let ((tok-end (point)))
(c-backward-token-2)
(member (buffer-substring-no-properties (point) tok-end)
c-pre-start-tokens)))))
(while (c-forward-annotation)
(c-forward-syntactic-ws))
;; Check for a type. Unknown symbols are treated as possible
;; types, but they could also be specifiers disguised through
;; macros like __INLINE__, so we recognize both types and known
;; specifiers after them too.
(while
(let* ((start (point)) kwd-sym kwd-clause-end found-type noise-start)
(cond
;; Look for a specifier keyword clause.
((or (and (looking-at c-make-top-level-key)
(setq make-top t))
(looking-at c-prefix-spec-kwds-re)
(and (c-major-mode-is 'java-mode)
(looking-at "@[A-Za-z0-9]+")))
(save-match-data
(if (looking-at c-typedef-key)
(setq at-typedef t)))
(setq kwd-sym (c-keyword-sym (match-string 1)))
(save-excursion
(c-forward-keyword-clause 1)
(setq kwd-clause-end (point))))
((and c-opt-cpp-prefix
(looking-at c-noise-macro-with-parens-name-re))
(setq noise-start (point))
(while
(and
(c-forward-noise-clause)
(looking-at c-noise-macro-with-parens-name-re)))
(setq kwd-clause-end (point))))
(when (setq found-type (c-forward-type t)) ; brace-block-too
;; Found a known or possible type or a prefix of a known type.
(when (and (eq found-type 'no-id)
(save-excursion
(and (c-forward-name) ; over the identifier
(looking-at "[=(]")))) ; FIXME!!! proper regexp.
(setq new-style-auto t)) ; position of foo in "auto foo"
(when at-type
;; Got two identifiers with nothing but whitespace
;; between them. That can only happen in declarations.
(setq at-decl-or-cast 'ids)
(when (eq at-type 'found)
;; If the previous identifier is a found type we
;; record it as a real one; it might be some sort of
;; alias for a prefix like "unsigned".
(save-excursion
(goto-char type-start)
(let ((c-promote-possible-types t))
(c-forward-type)))))
(setq backup-at-type at-type
backup-type-start type-start
backup-id-start id-start
at-type found-type
type-start start
id-start (point)
;; The previous ambiguous specifier/type turned out
;; to be a type since we've parsed another one after
;; it, so clear these backup flags.
backup-at-type-decl nil
backup-maybe-typeless nil))
(if (or kwd-sym noise-start)
(progn
;; Handle known specifier keywords and
;; `c-decl-hangon-kwds' which can occur after known
;; types.
(if (or (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
noise-start)
;; It's a hang-on keyword or noise clause that can occur
;; anywhere.
(progn
(if at-type
;; Move the identifier start position if
;; we've passed a type.
(setq id-start kwd-clause-end)
;; Otherwise treat this as a specifier and
;; move the fallback position.
(setq start-pos kwd-clause-end))
(goto-char kwd-clause-end))
;; It's an ordinary specifier so we know that
;; anything before this can't be the type.
(setq backup-at-type nil
start-pos kwd-clause-end)
(if found-type
;; It's ambiguous whether this keyword is a
;; specifier or a type prefix, so set the backup
;; flags. (It's assumed that `c-forward-type'
;; moved further than `c-forward-keyword-clause'.)
(progn
(when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
(setq backup-at-type-decl t))
(when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
(setq backup-maybe-typeless t)))
(when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
;; This test only happens after we've scanned a type.
;; So, with valid syntax, kwd-sym can't be 'typedef.
(setq at-type-decl t))
(when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
(setq maybe-typeless t))
;; Haven't matched a type so it's an unambiguous
;; specifier keyword and we know we're in a
;; declaration.
(setq at-decl-or-cast t)
;; (setq prev-kwd-sym kwd-sym)
(goto-char kwd-clause-end))))
;; If the type isn't known we continue so that we'll jump
;; over all specifiers and type identifiers. The reason
;; to do this for a known type prefix is to make things
;; like "unsigned INT16" work.
(and found-type (not (memq found-type '(t no-id)))))))
(cond
((eq at-type t)
;; If a known type was found, we still need to skip over any
;; hangon keyword clauses after it. Otherwise it has already
;; been done in the loop above.
(while
(cond ((looking-at c-decl-hangon-key)
(c-forward-keyword-clause 1))
((and c-opt-cpp-prefix
(looking-at c-noise-macro-with-parens-name-re))
(c-forward-noise-clause))))
(setq id-start (point)))
((eq at-type 'prefix)
;; A prefix type is itself a primitive type when it's not
;; followed by another type.
(setq at-type t))
((eq at-type 'no-id)
;; For an auto type, we assume we definitely have a type construct.
(setq at-type t))
((not at-type)
;; Got no type but set things up to continue anyway to handle
;; the various cases when a declaration doesn't start with a
;; type.
(setq id-start start-pos))
((and (eq at-type 'maybe)
(c-major-mode-is 'c++-mode))
;; If it's C++ then check if the last "type" ends on the form
;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
;; (con|de)structor.
(save-excursion
(let (name end-2 end-1)
(goto-char id-start)
(c-backward-syntactic-ws)
(setq end-2 (point))
(when (and
(c-simple-skip-symbol-backward)
(progn
(setq name
(buffer-substring-no-properties (point) end-2))
;; Cheating in the handling of syntactic ws below.
(< (skip-chars-backward ":~ \t\n\r\v\f") 0))
(progn
(setq end-1 (point))
(c-simple-skip-symbol-backward))
(>= (point) type-start)
(equal (buffer-substring-no-properties (point) end-1)
name)
(goto-char end-2)
(progn
(c-forward-syntactic-ws)
(eq (char-after) ?\()))
;; It is a (con|de)structor name. In that case the
;; declaration is typeless so zap out any preceding
;; identifier(s) that we might have taken as types.
(goto-char type-start)
(setq at-type nil
backup-at-type nil
id-start type-start))))))
;; Check for and step over a type decl expression after the thing
;; that is or might be a type. This can't be skipped since we
;; need the correct end position of the declarator for
;; `max-type-decl-end-*'.
(let ((start (point)) (paren-depth 0) pos
;; True if there's a non-open-paren match of
;; `c-type-decl-prefix-key'.
got-prefix
;; True if the declarator is surrounded by a parenthesis pair.
got-parens
;; True if there is an identifier in the declarator.
got-identifier
;; True if we find a number where an identifier was expected.
got-number
;; True if there's a non-close-paren match of
;; `c-type-decl-suffix-key'.
got-suffix
;; True if there's a prefix match outside the outermost
;; paren pair that surrounds the declarator.
got-prefix-before-parens
;; True if there's a prefix, such as "*" which might precede the
;; identifier in a function declaration.
got-function-name-prefix
;; True if there's a suffix match outside the outermost
;; paren pair that surrounds the declarator. The value is
;; the position of the first suffix match.
got-suffix-after-parens
;; True if we've parsed the type decl to a token that is
;; known to end declarations in this context.
at-decl-end
;; The earlier values of `at-type' and `type-start' if we've
;; shifted the type backwards.
identifier-type identifier-start
;; If `c-parse-and-markup-<>-arglists' is set we need to
;; turn it off during the name skipping below to avoid
;; getting `c-type' properties that might be bogus. That
;; can happen since we don't know if
;; `c-restricted-<>-arglists' will be correct inside the
;; arglist paren that gets entered.
c-parse-and-markup-<>-arglists
;; Start of the identifier for which `got-identifier' was set.
name-start
;; Position after (innermost) open parenthesis encountered in the
;; prefix operators.
after-paren-pos)
(goto-char id-start)
;; Skip over type decl prefix operators. (Note similar code in
;; `c-forward-declarator'.)
(if (and c-recognize-typeless-decls
(equal c-type-decl-prefix-key regexp-unmatchable))
(when (eq (char-after) ?\()
(progn
(setq paren-depth (1+ paren-depth))
(forward-char)
(setq after-paren-pos (point))))
(while (and (looking-at c-type-decl-prefix-key)
(if (and (c-major-mode-is 'c++-mode)
(match-beginning 3))
;; If the third submatch matches in C++ then
;; we're looking at an identifier that's a
;; prefix only if it specifies a member pointer.
(when (progn (setq pos (point))
(setq got-identifier (c-forward-name)))
(setq name-start pos)
(if (looking-at "\\(::\\)")
;; We only check for a trailing "::" and
;; let the "*" that should follow be
;; matched in the next round.
(progn (setq got-identifier nil) t)
;; It turned out to be the real identifier,
;; so stop.
nil))
t))
(if (eq (char-after) ?\()
(progn
(setq paren-depth (1+ paren-depth))
(forward-char)
(setq after-paren-pos (point)))
(unless got-prefix-before-parens
(setq got-prefix-before-parens (= paren-depth 0)))
(setq got-prefix t)
(when (save-match-data
(looking-at c-type-decl-operator-prefix-key))
(setq got-function-name-prefix t))
(goto-char (match-end 1)))
(c-forward-syntactic-ws)))
(setq got-parens (> paren-depth 0))
;; Try to skip over an identifier.
(or got-identifier
(and (looking-at c-identifier-start)
(setq pos (point))
(setq got-identifier (c-forward-name))
(setq name-start pos))
(when (looking-at "[0-9]")
(setq got-number t)) ; We probably have an arithmetic expression.
(and maybe-typeless
(or (eq at-type 'maybe)
(when (eq at-type 'found)
;; Remove the ostensible type from the found types list.
(when type-start
(c-unfind-type
(buffer-substring-no-properties
type-start
(save-excursion
(goto-char type-start)
(c-end-of-token)
(point)))))
t))
;; The token which we assumed to be a type is actually the
;; identifier, and we have no explicit type.
(setq at-type nil
name-start type-start
id-start type-start
got-identifier t)))
;; Skip over type decl suffix operators and trailing noise macros.
(while
(cond
((and c-opt-cpp-prefix
(looking-at c-noise-macro-with-parens-name-re))
(c-forward-noise-clause))
((and (looking-at c-type-decl-suffix-key)
;; We avoid recognizing foo(bar) or foo() at top level as a
;; construct here in C, since we want to recognize this as a
;; typeless function declaration.
(not (and (c-major-mode-is 'c-mode)
(not got-prefix)
(or (eq context 'top) make-top)
(eq (char-after) ?\))
after-paren-pos
(or (memq at-type '(nil maybe))
(not got-identifier)
(save-excursion
(goto-char after-paren-pos)
(c-forward-syntactic-ws)
;; Prevent the symbol being recorded as a type.
(let (c-record-type-identifiers)
(not (memq (c-forward-type)
'(nil maybe)))))))))
(if (eq (char-after) ?\))
(when (> paren-depth 0)
(setq paren-depth (1- paren-depth))
(forward-char)
t)
(when (if (save-match-data (looking-at "\\s("))
(c-safe (c-forward-sexp 1) t)
(goto-char (match-end 1))
t)
(when (and (not got-suffix-after-parens)
(= paren-depth 0))
(setq got-suffix-after-parens (match-beginning 0)))
(setq got-suffix t))))
(t
;; No suffix matched. We might have matched the
;; identifier as a type and the open paren of a
;; function arglist as a type decl prefix. In that
;; case we should "backtrack": Reinterpret the last
;; type as the identifier, move out of the arglist and
;; continue searching for suffix operators.
;;
;; Do this even if there's no preceding type, to cope
;; with old style function declarations in K&R C,
;; (con|de)structors in C++ and `c-typeless-decl-kwds'
;; style declarations. That isn't applicable in an
;; arglist context, though.
(when (and (> paren-depth 0) ; ensures `after-paren-pos' is non-nil
(not got-prefix-before-parens)
(not (eq at-type t))
(or backup-at-type
maybe-typeless
backup-maybe-typeless
(when c-recognize-typeless-decls
(and (memq context '(nil top))
;; Deal with C++11's "copy-initialization"
;; where we have <type>(<constant>), by
;; contrasting with a typeless
;; <name>(<type><parameter>, ...).
(save-excursion
(goto-char after-paren-pos)
(c-forward-syntactic-ws)
(progn
(while
(cond
((and
c-opt-cpp-prefix
(looking-at c-noise-macro-with-parens-name-re))
(c-forward-noise-clause))
((looking-at c-decl-hangon-key)
(c-forward-keyword-clause 1))))
t)
(or (c-forward-type)
;; Recognize a top-level typeless
;; function declaration in C.
(and (c-major-mode-is 'c-mode)
(or (eq context 'top) make-top)
(eq (char-after) ?\))))))))
(let ((pd paren-depth))
(setq pos (point))
(catch 'pd
(while (> pd 0)
(setq pos (c-up-list-forward pos))
(when (or (null pos)
(not (eq (char-before pos) ?\))))
(throw 'pd nil))
(goto-char pos)
(setq pd (1- pd)))
t)))
(c-fdoc-shift-type-backward)
t)))
(c-forward-syntactic-ws))
(when (and (not got-identifier)
(or (and new-style-auto
(looking-at c-auto-ops-re))
(and (or maybe-typeless backup-maybe-typeless)
(not got-prefix)
at-type)))
;; Have found no identifier but `c-typeless-decl-kwds' has
;; matched so we know we're inside a declaration. The
;; preceding type must be the identifier instead.
(c-fdoc-shift-type-backward))
;; Prepare the "-> type;" for fontification later on.
(when (and new-style-auto
(looking-at c-haskell-op-re))
(save-excursion
(goto-char (match-end 0))
(c-forward-syntactic-ws)
(setq type-start (point))
(setq at-type (c-forward-type))))
;; Move forward over any "WS" ids (like "final" or "override" in C++)
(while (looking-at c-type-decl-suffix-ws-ids-key)
(goto-char (match-end 1))
(c-forward-syntactic-ws))
(setq
at-decl-or-cast
(catch 'at-decl-or-cast
;; CASE 1
(when (> paren-depth 0)
;; Encountered something inside parens that isn't matched by
;; the `c-type-decl-*' regexps, so it's not a type decl
;; expression. Try to skip out to the same paren depth to
;; not confuse the cast check below. If we don't manage this and
;; `at-decl-or-cast' is 'ids we might have an expression like
;; "foo bar ({ ..." which is a valid C++11 initialization.
(if (and (not (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
(eq at-decl-or-cast 'ids))
(c-fdoc-shift-type-backward))
;; If we've found a specifier keyword then it's a
;; declaration regardless.
(throw 'at-decl-or-cast (memq at-decl-or-cast '(t ids))))
(setq at-decl-end
(looking-at (cond ((eq context '<>) "[,>]")
((not (memq context '(nil top))) "[,)]")
(t "[,;]"))))
;; Now we've collected info about various characteristics of
;; the construct we're looking at. Below follows a decision
;; tree based on that. It's ordered to check more certain
;; signs before less certain ones.
(if got-identifier
(progn
;; CASE 2
(when (and (or at-type maybe-typeless)
(not (or got-prefix got-parens)))
;; Got another identifier directly after the type, so it's a
;; declaration.
(throw 'at-decl-or-cast t))
(when (and got-parens
(or (not got-function-name-prefix)
(and (not got-suffix-after-parens)
at-decl-end))
(or backup-at-type
maybe-typeless
backup-maybe-typeless
(eq at-decl-or-cast t)
;; Check whether we have "bar (gnu);" where we
;; are directly inside a class (etc.) called "bar".
(save-excursion
(and
(progn
(goto-char name-start)
(not (memq (c-forward-type) '(nil maybe))))
(progn
(goto-char id-start)
(c-directly-in-class-called-p
(buffer-substring
type-start
(progn
(goto-char type-start)
(c-forward-type)
(c-backward-syntactic-ws)
(point)))))))))
;; Got a declaration of the form "foo bar (gnu);" or "bar
;; (gnu);" where we've recognized "bar" as the type and "gnu"
;; as the declarator, and in the latter case, checked that
;; "bar (gnu)" appears directly inside the class "bar". In
;; this case it's however more likely that "bar" is the
;; declarator and "gnu" a function argument or initializer
;; (if `c-recognize-paren-inits' is set), since the parens
;; around "gnu" would be superfluous if it's a declarator.
;; Shift the type one step backward.
(c-fdoc-shift-type-backward)))
;; Found no identifier.
(if backup-at-type
(progn
;; CASE 3
(when (= (point) start)
;; Got a plain list of identifiers. If a colon follows it's
;; a valid label, or maybe a bitfield. Otherwise the last
;; one probably is the declared identifier and we should
;; back up to the previous type, providing it isn't a cast.
(if (and (eq (char-after) ?:)
(not (c-major-mode-is 'java-mode)))
(cond
;; If we've found a specifier keyword then it's a
;; declaration regardless.
((eq at-decl-or-cast t)
(throw 'at-decl-or-cast t))
((and c-has-bitfields
(eq at-decl-or-cast 'ids)) ; bitfield.
(setq backup-if-not-cast t)
(throw 'at-decl-or-cast t)))
(setq backup-if-not-cast t)
(throw 'at-decl-or-cast t)))
;; CASE 4
(when (and got-suffix
(not got-prefix)
(not got-parens))
;; Got a plain list of identifiers followed by some suffix.
;; If this isn't a cast then the last identifier probably is
;; the declared one and we should back up to the previous
;; type.
(setq backup-if-not-cast t)
(throw 'at-decl-or-cast t)))
;; CASE 5
(when (eq at-type t)
;; If the type is known we know that there can't be any
;; identifier somewhere else, and it's only in declarations in
;; e.g. function prototypes and in casts that the identifier may
;; be left out.
(throw 'at-decl-or-cast t))
(when (= (point) start)
;; Only got a single identifier (parsed as a type so far).
;; CASE 6
(if (and
;; Check that the identifier isn't at the start of an
;; expression.
at-decl-end
(cond
((eq context 'decl)
;; Inside an arglist that contains declarations. If K&R
;; style declarations and parenthesis style initializers
;; aren't allowed then the single identifier must be a
;; type, else we require that it's known or found
;; (primitive types are handled above).
(or (and (not c-recognize-knr-p)
(not c-recognize-paren-inits))
(memq at-type '(known found))))
((eq context '<>)
;; Inside a template arglist. Accept known and found
;; types; other identifiers could just as well be
;; constants in C++.
(memq at-type '(known found)))))
(throw 'at-decl-or-cast t)
;; CASE 7
;; Can't be a valid declaration or cast, but if we've found a
;; specifier it can't be anything else either, so treat it as
;; an invalid/unfinished declaration or cast.
(throw 'at-decl-or-cast at-decl-or-cast))))
(if (and got-parens
(not got-prefix)
(memq context '(nil top))
(not (eq at-type t))
(or backup-at-type
maybe-typeless
backup-maybe-typeless
(when c-recognize-typeless-decls
(or (not got-suffix)
(not (looking-at
c-after-suffixed-type-maybe-decl-key))))))
;; Got an empty paren pair and a preceding type that probably
;; really is the identifier. Shift the type backwards to make
;; the last one the identifier. This is analogous to the
;; "backtracking" done inside the `c-type-decl-suffix-key' loop
;; above.
;;
;; Exception: In addition to the conditions in that
;; "backtracking" code, do not shift backward if we're not
;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
;; Since there's no preceding type, the shift would mean that
;; the declaration is typeless. But if the regexp doesn't match
;; then we will simply fall through in the tests below and not
;; recognize it at all, so it's better to try it as an abstract
;; declarator instead.
(c-fdoc-shift-type-backward)
;; Still no identifier.
;; CASE 8
(when (and got-prefix (or got-parens got-suffix))
;; Require `got-prefix' together with either `got-parens' or
;; `got-suffix' to recognize it as an abstract declarator:
;; `got-parens' only is probably an empty function call.
;; `got-suffix' only can build an ordinary expression together
;; with the preceding identifier which we've taken as a type.
;; We could actually accept on `got-prefix' only, but that can
;; easily occur temporarily while writing an expression so we
;; avoid that case anyway. We could do a better job if we knew
;; the point when the fontification was invoked.
(throw 'at-decl-or-cast t))
;; CASE 9
(when (and at-type
(not got-prefix)
(not got-parens)
got-suffix-after-parens
(eq (char-after got-suffix-after-parens) ?\())
;; Got a type, no declarator but a paren suffix. I.e. it's a
;; normal function call after all (or perhaps a C++ style object
;; instantiation expression).
(throw 'at-decl-or-cast nil))))
;; CASE 9.5
(when (and (not context) ; i.e. not at top level.
(c-major-mode-is 'c++-mode)
(eq at-decl-or-cast 'ids)
after-paren-pos)
;; We've got something like "foo bar (...)" in C++ which isn't at
;; the top level. This is probably a uniform initialization of bar
;; to the contents of the parens. In this case the declarator ends
;; at the open paren.
(goto-char (1- after-paren-pos))
(throw 'at-decl-or-cast t))
;; CASE 10
(when at-decl-or-cast
;; By now we've located the type in the declaration that we know
;; we're in.
(throw 'at-decl-or-cast t))
;; CASE 11
(when (and got-identifier
(looking-at c-after-suffixed-type-decl-key)
(or (eq context 'top)
make-top
(and (eq context nil)
(match-beginning 1)))
(if (and got-parens
(not got-prefix)
(not got-suffix)
(not (eq at-type t)))
;; Shift the type backward in the case that there's a
;; single identifier inside parens. That can only
;; occur in K&R style function declarations so it's
;; more likely that it really is a function call.
;; Therefore we only do this after
;; `c-after-suffixed-type-decl-key' has matched.
(progn (c-fdoc-shift-type-backward) t)
got-suffix-after-parens))
;; A declaration according to `c-after-suffixed-type-decl-key'.
(throw 'at-decl-or-cast t))
;; CASE 12
(when (and (or got-prefix (not got-parens))
(memq at-type '(t known)))
;; It's a declaration if a known type precedes it and it can't be a
;; function call.
(throw 'at-decl-or-cast t))
;; If we get here we can't tell if this is a type decl or a normal
;; expression by looking at it alone. (That's under the assumption
;; that normal expressions always can look like type decl expressions,
;; which isn't really true but the cases where it doesn't hold are so
;; uncommon (e.g. some placements of "const" in C++) it's not worth
;; the effort to look for them.)
;;; 2008-04-16: commented out the next form, to allow the function to recognize
;;; "foo (int bar)" in CC (an implicit type (in class foo) without a semicolon)
;;; as a(n almost complete) declaration, enabling it to be fontified.
;; CASE 13
;; (unless (or at-decl-end (looking-at "=[^=]"))
;; If this is a declaration it should end here or its initializer(*)
;; should start here, so check for allowed separation tokens. Note
;; that this rule doesn't work e.g. with a K&R arglist after a
;; function header.
;;
;; *) Don't check for C++ style initializers using parens
;; since those already have been matched as suffixes.
;;
;; If `at-decl-or-cast' is then we've found some other sign that
;; it's a declaration or cast, so then it's probably an
;; invalid/unfinished one.
;; (throw 'at-decl-or-cast at-decl-or-cast))
;; Below are tests that only should be applied when we're certain to
;; not have parsed halfway through an expression.
;; CASE 14
(when (memq at-type '(t known))
;; The expression starts with a known type so treat it as a
;; declaration.
(throw 'at-decl-or-cast t))
;; CASE 15
(when (and (c-major-mode-is 'c++-mode)
;; In C++ we check if the identifier is a known type, since
;; (con|de)structors use the class name as identifier.
;; We've always shifted over the identifier as a type and
;; then backed up again in this case.
identifier-type
(or (memq identifier-type '(found known))
(and (eq (char-after identifier-start) ?~)
;; `at-type' probably won't be 'found for
;; destructors since the "~" is then part of the
;; type name being checked against the list of
;; known types, so do a check without that
;; operator.
(or (save-excursion
(goto-char (1+ identifier-start))
(c-forward-syntactic-ws)
(c-with-syntax-table
c-identifier-syntax-table
(looking-at c-known-type-key)))
(save-excursion
(goto-char (1+ identifier-start))
;; We have already parsed the type earlier,
;; so it'd be possible to cache the end
;; position instead of redoing it here, but
;; then we'd need to keep track of another
;; position everywhere.
(c-check-type (point)
(progn (c-forward-type)
(point))))))))
(throw 'at-decl-or-cast t))
(if got-identifier
(progn
;; CASE 16
(when (and got-prefix-before-parens
at-type
(or at-decl-end (looking-at "=[^=]"))
(memq context '(nil top))
(or (not got-suffix)
at-decl-start))
;; Got something like "foo * bar;". Since we're not inside
;; an arglist it would be a meaningless expression because
;; the result isn't used. We therefore choose to recognize
;; it as a declaration. We only allow a suffix (which makes
;; the construct look like a function call) when
;; `at-decl-start' provides additional evidence that we do
;; have a declaration.
(setq maybe-expression t)
(throw 'at-decl-or-cast t))
;; CASE 17
(when (and (or got-suffix-after-parens
(looking-at "=[^=]"))
(eq at-type 'found)
(not (eq context 'arglist)))
;; Got something like "a (*b) (c);" or "a (b) = c;". It could
;; be an odd expression or it could be a declaration. Treat
;; it as a declaration if "a" has been used as a type
;; somewhere else (if it's a known type we won't get here).
(setq maybe-expression t)
(throw 'at-decl-or-cast t))
;; CASE 17.5
(when (and c-asymmetry-fontification-flag
got-prefix-before-parens
at-type
(or (not got-suffix)
at-decl-start))
(let ((space-before-id
(save-excursion
(goto-char name-start)
(or (bolp) (memq (char-before) '(?\ ?\t)))))
(space-after-type
(save-excursion
(goto-char type-start)
(and (c-forward-type)
(progn (c-backward-syntactic-ws) t)
(or (eolp)
(memq (char-after) '(?\ ?\t)))))))
(when (not (eq (not space-before-id)
(not space-after-type)))
(setq maybe-expression t)
(throw 'at-decl-or-cast t)))))
;; CASE 18
(when (and at-decl-end
(not (memq context '(nil top)))
(or (and got-prefix (not got-number))
(and (eq context 'decl)
(not c-recognize-paren-inits)
(or got-parens got-suffix))))
;; Got a type followed by an abstract declarator. If `got-prefix'
;; is set it's something like "a *" without anything after it. If
;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
;; or similar, which we accept only if the context rules out
;; expressions.
(throw 'at-decl-or-cast t)))
;; If we had a complete symbol table here (which rules out
;; `c-found-types') we should return t due to the disambiguation rule
;; (in at least C++) that anything that can be parsed as a declaration
;; is a declaration. Now we're being more defensive and prefer to
;; highlight things like "foo (bar);" as a declaration only if we're
;; inside an arglist that contains declarations. Update (2017-09): We
;; now recognize a top-level "foo(bar);" as a declaration in C.
;; CASE 19
(or (eq context 'decl)
(and (c-major-mode-is 'c-mode)
(or (eq context 'top) make-top))))))
;; The point is now after the type decl expression.
(cond
;; Check for a cast.
((save-excursion
(and
c-cast-parens
;; Should be the first type/identifier in a cast paren.
(> preceding-token-end (point-min))
(memq (char-before preceding-token-end) c-cast-parens)
;; The closing paren should follow.
(progn
(c-forward-syntactic-ws)
(looking-at "\\s)"))
;; There should be a primary expression after it.
(let (pos)
(forward-char)
(c-forward-syntactic-ws)
(setq cast-end (point))
(and (looking-at c-primary-expr-regexp)
(progn
(setq pos (match-end 0))
(or
;; Check if the expression begins with a prefix keyword.
(match-beginning 2)
(if (match-beginning 1)
;; Expression begins with an ambiguous operator. Treat
;; it as a cast if it's a type decl or if we've
;; recognized the type somewhere else.
(or at-decl-or-cast
(memq at-type '(t known found)))
;; Unless it's a keyword, it's the beginning of a primary
;; expression.
(not (looking-at c-keywords-regexp)))))
;; If `c-primary-expr-regexp' matched a nonsymbol token, check
;; that it matched a whole one so that we don't e.g. confuse
;; the operator '-' with '->'. It's ok if it matches further,
;; though, since it e.g. can match the float '.5' while the
;; operator regexp only matches '.'.
(or (not (looking-at c-nonsymbol-token-regexp))
(<= (match-end 0) pos))))
;; There should either be a cast before it or something that isn't an
;; identifier or close paren.
(> preceding-token-end (point-min))
(progn
(goto-char (1- preceding-token-end))
(or (eq (point) last-cast-end)
(progn
(c-backward-syntactic-ws)
(if (< (skip-syntax-backward "w_") 0)
;; It's a symbol. Accept it only if it's one of the
;; keywords that can precede an expression (without
;; surrounding parens).
(looking-at c-simple-stmt-key)
(and
;; Check that it isn't a close paren (block close is ok,
;; though).
(not (memq (char-before) '(?\) ?\])))
;; Check that it isn't a nonsymbol identifier.
(not (c-on-identifier)))))))))
;; Handle the cast.
(when (and c-record-type-identifiers at-type (not (eq at-type t)))
(let ((c-promote-possible-types t))
(goto-char type-start)
(c-forward-type)))
(goto-char cast-end)
'cast)
(at-decl-or-cast
;; We're at a declaration. Highlight the type and the following
;; declarators.
(when backup-if-not-cast
(c-fdoc-shift-type-backward t))
(when (and (eq context 'decl) (looking-at ","))
;; Make sure to propagate the `c-decl-arg-start' property to
;; the next argument if it's set in this one, to cope with
;; interactive refontification.
(c-put-c-type-property (point) 'c-decl-arg-start))
;; Record the type's coordinates in `c-record-type-identifiers' for
;; later fontification.
(when (and c-record-type-identifiers at-type ;; (not (eq at-type t))
;; There seems no reason to exclude a token from
;; fontification just because it's "a known type that can't
;; be a name or other expression". 2013-09-18.
)
(let ((c-promote-possible-types t))
(save-excursion
(goto-char type-start)
(c-forward-type))))
(list id-start
(and (or at-type-decl at-typedef)
(cons at-type-decl at-typedef))
maybe-expression
type-start
(or (eq context 'top) make-top)))
(t
;; False alarm. Restore the recorded ranges.
(setq c-record-type-identifiers save-rec-type-ids
c-record-ref-identifiers save-rec-ref-ids)
nil))))
(defun c-forward-label (&optional assume-markup preceding-token-end limit)
;; Assuming that point is at the beginning of a token, check if it starts a
;; label and if so move over it and return non-nil (t in default situations,
;; specific symbols (see below) for interesting situations), otherwise don't
;; move and return nil. "Label" here means "most things with a colon".
;;
;; More precisely, a "label" is regarded as one of:
;; (i) a goto target like "foo:" - returns the symbol `goto-target';
;; (ii) A case label - either the entire construct "case FOO:", or just the
;; bare "case", should the colon be missing. We return t;
;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
;; return t;
;; (iv) One of QT's "extended" C++ variants of
;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
;; Returns the symbol `qt-2kwds-colon'.
;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
;; colon). Currently (2006-03), this applies only to Objective C's
;; keywords "@private", "@protected", and "@public". Returns t.
;;
;; One of the things which will NOT be recognized as a label is a bit-field
;; element of a struct, something like "int foo:5".
;;
;; The end of the label is taken to be just after the colon, or the end of
;; the first submatch in `c-opt-extra-label-key'. The point is directly
;; after the end on return. The terminating char gets marked with
;; `c-decl-end' to improve recognition of the following declaration or
;; statement.
;;
;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
;; label, if any, has already been marked up like that.
;;
;; If PRECEDING-TOKEN-END is given, it should be the first position
;; after the preceding token, i.e. on the other side of the
;; syntactic ws from the point. Use a value less than or equal to
;; (point-min) if the point is at the first token in (the visible
;; part of) the buffer.
;;
;; The optional LIMIT limits the forward scan for the colon.
;;
;; This function records the ranges of the label symbols on
;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
;; non-nil.
;;
;; This function might do hidden buffer changes.
(let ((start (point))
label-end
qt-symbol-idx
macro-start ; if we're in one.
label-type
kwd)
(cond
;; "case" or "default" (Doesn't apply to AWK).
((looking-at c-label-kwds-regexp)
(let ((kwd-end (match-end 1)))
;; Record only the keyword itself for fontification, since in
;; case labels the following is a constant expression and not
;; a label.
(when c-record-type-identifiers
(c-record-ref-id (cons (match-beginning 1) kwd-end)))
;; Find the label end.
(goto-char kwd-end)
(setq label-type
(if (and (c-syntactic-re-search-forward
;; Stop on chars that aren't allowed in expressions,
;; and on operator chars that would be meaningless
;; there. FIXME: This doesn't cope with ?: operators.
"[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
limit t t nil 1)
(match-beginning 2))
(progn ; there's a proper :
(goto-char (match-beginning 2)) ; just after the :
(c-put-c-type-property (1- (point)) 'c-decl-end)
t)
;; It's an unfinished label. We consider the keyword enough
;; to recognize it as a label, so that it gets fontified.
;; Leave the point at the end of it, but don't put any
;; `c-decl-end' marker.
(goto-char kwd-end)
t))))
;; @private, @protected, @public, in Objective C, or similar.
((and c-opt-extra-label-key
(looking-at c-opt-extra-label-key))
;; For a `c-opt-extra-label-key' match, we record the whole
;; thing for fontification. That's to get the leading '@' in
;; Objective-C protection labels fontified.
(goto-char (match-end 1))
(when c-record-type-identifiers
(c-record-ref-id (cons (match-beginning 1) (point))))
(c-put-c-type-property (1- (point)) 'c-decl-end)
(setq label-type t))
;; All other cases of labels.
((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
;; A colon label must have something before the colon.
(not (eq (char-after) ?:))
;; Check that we're not after a token that can't precede a label.
(or
;; Trivially succeeds when there's no preceding token.
;; Succeeds when we're at a virtual semicolon.
(if preceding-token-end
(<= preceding-token-end (point-min))
(save-excursion
(c-backward-syntactic-ws)
(setq preceding-token-end (point))
(or (bobp)
(c-at-vsemi-p))))
;; Check if we're after a label, if we're after a closing
;; paren that belong to statement, and with
;; `c-label-prefix-re'. It's done in different order
;; depending on `assume-markup' since the checks have
;; different expensiveness.
(if assume-markup
(or
(eq (c-get-char-property (1- preceding-token-end) 'c-type)
'c-decl-end)
(save-excursion
(goto-char (1- preceding-token-end))
(c-beginning-of-current-token)
(or (looking-at c-label-prefix-re)
(looking-at c-block-stmt-1-key)))
(and (eq (char-before preceding-token-end) ?\))
(c-after-conditional)))
(or
(save-excursion
(goto-char (1- preceding-token-end))
(c-beginning-of-current-token)
(or (looking-at c-label-prefix-re)
(looking-at c-block-stmt-1-key)))
(cond
((eq (char-before preceding-token-end) ?\))
(c-after-conditional))
((eq (char-before preceding-token-end) ?:)
;; Might be after another label, so check it recursively.
(save-restriction
(save-excursion
(goto-char (1- preceding-token-end))
;; Essentially the same as the
;; `c-syntactic-re-search-forward' regexp below.
(setq macro-start
(save-excursion (and (c-beginning-of-macro)
(point))))
(if macro-start (narrow-to-region macro-start (point-max)))
(c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
;; Note: the following should work instead of the
;; narrow-to-region above. Investigate why not,
;; sometime. ACM, 2006-03-31.
;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
;; macro-start t)
(let ((pte (point))
;; If the caller turned on recording for us,
;; it shouldn't apply when we check the
;; preceding label.
c-record-type-identifiers)
;; A label can't start at a cpp directive. Check for
;; this, since c-forward-syntactic-ws would foul up on it.
(unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
(c-forward-syntactic-ws)
(c-forward-label nil pte start))))))))))
;; Point is still at the beginning of the possible label construct.
;;
;; Check that the next nonsymbol token is ":", or that we're in one
;; of QT's "slots" declarations. Allow '(' for the sake of macro
;; arguments. FIXME: Should build this regexp from the language
;; constants.
(cond
;; public: protected: private:
((and
(c-major-mode-is 'c++-mode)
(search-forward-regexp
"\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
(progn (backward-char)
(c-forward-syntactic-ws limit)
(looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
(forward-char)
(setq label-type t))
;; QT double keyword like "protected slots:" or goto target.
((progn (goto-char start) nil))
((when (c-syntactic-re-search-forward
"[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
(backward-char)
(setq label-end (point))
(setq qt-symbol-idx
(and (c-major-mode-is 'c++-mode)
(string-match
"\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
(buffer-substring start (point)))))
(c-forward-syntactic-ws limit)
(cond
((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
(forward-char)
(setq label-type
(if (or (string= "signals" ; Special QT macro
(setq kwd (buffer-substring-no-properties start label-end)))
(string= "Q_SIGNALS" kwd))
'qt-1kwd-colon
'goto-target)))
((and qt-symbol-idx
(search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
(progn (c-forward-syntactic-ws limit)
(looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
(forward-char)
(setq label-type 'qt-2kwds-colon)))))))
(save-restriction
(narrow-to-region start (point))
;; Check that `c-nonlabel-token-key' doesn't match anywhere.
(catch 'check-label
(goto-char start)
(while (progn
(when (looking-at c-nonlabel-token-key)
(goto-char start)
(setq label-type nil)
(throw 'check-label nil))
(and (c-safe (c-forward-sexp)
(c-forward-syntactic-ws)
t)
(not (eobp)))))
;; Record the identifiers in the label for fontification, unless
;; it begins with `c-label-kwds' in which case the following
;; identifiers are part of a (constant) expression that
;; shouldn't be fontified.
(when (and c-record-type-identifiers
(progn (goto-char start)
(not (looking-at c-label-kwds-regexp))))
(while (c-syntactic-re-search-forward c-symbol-key nil t)
(c-record-ref-id (cons (match-beginning 0)
(match-end 0)))))
(c-put-c-type-property (1- (point-max)) 'c-decl-end)
(goto-char (point-max)))))
(t
;; Not a label.
(goto-char start)))
label-type))
(defun c-forward-objc-directive ()
;; Assuming the point is at the beginning of a token, try to move
;; forward to the end of the Objective-C directive that starts
;; there. Return t if a directive was fully recognized, otherwise
;; the point is moved as far as one could be successfully parsed and
;; nil is returned.
;;
;; This function records identifier ranges on
;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
;; `c-record-type-identifiers' is non-nil.
;;
;; This function might do hidden buffer changes.
(let ((start (point))
start-char
(c-promote-possible-types t)
lim
;; Turn off recognition of angle bracket arglists while parsing
;; types here since the protocol reference list might then be
;; considered part of the preceding name or superclass-name.
c-recognize-<>-arglists)
(if (or
(when (looking-at
(eval-when-compile
(c-make-keywords-re t
(append (c-lang-const c-protection-kwds objc)
'("@end"))
'objc-mode)))
(goto-char (match-end 1))
t)
(and
(looking-at
(eval-when-compile
(c-make-keywords-re t
'("@interface" "@implementation" "@protocol")
'objc-mode)))
;; Handle the name of the class itself.
(progn
;; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
;; at EOB.
(goto-char (match-end 0))
(setq lim (point))
(c-skip-ws-forward)
(c-forward-type))
(catch 'break
;; Look for ": superclass-name" or "( category-name )".
(when (looking-at "[:(]")
(setq start-char (char-after))
(forward-char)
(c-forward-syntactic-ws)
(unless (c-forward-type) (throw 'break nil))
(when (eq start-char ?\()
(unless (eq (char-after) ?\)) (throw 'break nil))
(forward-char)
(c-forward-syntactic-ws)))
;; Look for a protocol reference list.
(if (eq (char-after) ?<)
(let ((c-recognize-<>-arglists t)
(c-parse-and-markup-<>-arglists t)
c-restricted-<>-arglists)
(c-forward-<>-arglist t))
t))))
(progn
(c-backward-syntactic-ws lim)
(c-clear-c-type-property start (1- (point)) 'c-decl-end)
(c-put-c-type-property (1- (point)) 'c-decl-end)
t)
(c-clear-c-type-property start (point) 'c-decl-end)
nil)))
(defun c-beginning-of-inheritance-list (&optional lim)
;; Go to the first non-whitespace after the colon that starts a
;; multiple inheritance introduction. Optional LIM is the farthest
;; back we should search.
;;
;; This function might do hidden buffer changes.
(c-backward-token-2 0 t lim)
(while (and (or (looking-at c-symbol-start)
(looking-at "[<,]\\|::"))
(zerop (c-backward-token-2 1 t lim)))))
(defun c-in-method-def-p ()
;; Return nil if we aren't in a method definition, otherwise the
;; position of the initial [+-].
;;
;; This function might do hidden buffer changes.
(save-excursion
(beginning-of-line)
(and c-opt-method-key
(looking-at c-opt-method-key)
(point))
))
;; Contributed by Kevin Ryde <user42@zip.com.au>.
(defun c-in-gcc-asm-p ()
;; Return non-nil if point is within a gcc \"asm\" block.
;;
;; This should be called with point inside an argument list.
;;
;; Only one level of enclosing parentheses is considered, so for
;; instance nil is returned when in a function call within an asm
;; operand.
;;
;; This function might do hidden buffer changes.
(and c-opt-asm-stmt-key
(save-excursion
(beginning-of-line)
(backward-up-list 1)
(c-beginning-of-statement-1 (point-min) nil t)
(looking-at c-opt-asm-stmt-key))))
(defun c-at-toplevel-p ()
"Return a determination as to whether point is \"at the top level\".
Informally, \"at the top level\" is anywhere where you can write
a function.
More precisely, being at the top-level means that point is either
outside any enclosing block (such as a function definition), or
directly inside a class, namespace or other block that contains
another declaration level.
If point is not at the top-level (e.g. it is inside a method
definition), then nil is returned. Otherwise, if point is at a
top-level not enclosed within a class definition, t is returned.
Otherwise, a 2-vector is returned where the zeroth element is the
buffer position of the start of the class declaration, and the first
element is the buffer position of the enclosing class's opening
brace.
Note that this function might do hidden buffer changes. See the
comment at the start of cc-engine.el for more info."
;; Note to maintainers: this function consumes a great mass of CPU cycles.
;; Its use should thus be minimized as far as possible.
;; Consider instead using `c-bs-at-toplevel-p'.
(let ((paren-state (c-parse-state)))
(or (not (c-most-enclosing-brace paren-state))
(c-search-uplist-for-classkey paren-state))))
(defun c-just-after-func-arglist-p (&optional lim)
;; Return non-nil if the point is in the region after the argument
;; list of a function and its opening brace (or semicolon in case it
;; got no body). If there are K&R style argument declarations in
;; that region, the point has to be inside the first one for this
;; function to recognize it.
;;
;; If successful, the point is moved to the first token after the
;; function header (see `c-forward-decl-or-cast-1' for details) and
;; the position of the opening paren of the function arglist is
;; returned.
;;
;; The point is clobbered if not successful.
;;
;; LIM is used as bound for backward buffer searches.
;;
;; This function might do hidden buffer changes.
(let ((beg (point)) id-start)
(and
(eq (c-beginning-of-statement-1 lim nil nil nil t) 'same)
(not (and (c-major-mode-is 'objc-mode)
(c-forward-objc-directive)))
;; Don't confuse #if .... defined(foo) for a function arglist.
(not (and (looking-at c-cpp-expr-functions-key)
(save-excursion
(save-restriction
(widen)
(c-beginning-of-macro lim)))))
(setq id-start
(car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) 'top nil)))
(numberp id-start)
(< id-start beg)
;; There should not be a '=' or ',' between beg and the
;; start of the declaration since that means we were in the
;; "expression part" of the declaration.
(or (> (point) beg)
(not (looking-at "[=,]")))
(save-excursion
;; Check that there's an arglist paren in the
;; declaration.
(goto-char id-start)
(cond ((eq (char-after) ?\()
;; The declarator is a paren expression, so skip past it
;; so that we don't get stuck on that instead of the
;; function arglist.
(c-forward-sexp))
((and c-opt-op-identifier-prefix
(looking-at c-opt-op-identifier-prefix))
;; Don't trip up on "operator ()".
(c-forward-token-2 2 t)))
(and (< (point) beg)
(c-syntactic-re-search-forward "(" beg t t)
(1- (point)))))))
(defun c-in-knr-argdecl (&optional lim)
;; Return the position of the first argument declaration if point is
;; inside a K&R style argument declaration list, nil otherwise.
;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
;; position that bounds the backward search for the argument list. This
;; function doesn't move point.
;;
;; Point must be within a possible K&R region, e.g. just before a top-level
;; "{". It must be outside of parens and brackets. The test can return
;; false positives otherwise.
;;
;; This function might do hidden buffer changes.
(save-excursion
(save-restriction
;; If we're in a macro, our search range is restricted to it. Narrow to
;; the searchable range.
(let* ((macro-start (save-excursion (and (c-beginning-of-macro) (point))))
(macro-end (save-excursion (and macro-start (c-end-of-macro) (point))))
(low-lim (max (or lim (point-min)) (or macro-start (point-min))))
before-lparen after-rparen
(here (point))
(pp-count-out 20) ; Max number of paren/brace constructs before
; we give up.
ids ; List of identifiers in the parenthesized list.
id-start after-prec-token decl-or-cast decl-res
c-last-identifier-range identifier-ok)
(narrow-to-region low-lim (or macro-end (point-max)))
;; Search backwards for the defun's argument list. We give up if we
;; encounter a "}" (end of a previous defun) an "=" (which can't be in
;; a knr region) or BOB.
;;
;; The criterion for a paren structure being the arg list is:
;; o - there is non-WS stuff after it but before any "{"; AND
;; o - the token after it isn't a ";" AND
;; o - it is preceded by either an identifier (the function name) or
;; a macro expansion like "DEFUN (...)"; AND
;; o - its content is a non-empty comma-separated list of identifiers
;; (an empty arg list won't have a knr region).
;;
;; The following snippet illustrates these rules:
;; int foo (bar, baz, yuk)
;; int bar [] ;
;; int (*baz) (my_type) ;
;; int (*(* yuk) (void)) (void) ;
;; {
;;
;; Additionally, for a knr list to be recognized:
;; o - The identifier of each declarator up to and including the
;; one "near" point must be contained in the arg list.
(catch 'knr
(while (> pp-count-out 0) ; go back one paren/bracket pair each time.
(setq pp-count-out (1- pp-count-out))
(c-syntactic-skip-backward "^)]}=")
(cond ((eq (char-before) ?\))
(setq after-rparen (point)))
((eq (char-before) ?\])
(setq after-rparen nil))
(t ; either } (hit previous defun) or = or no more
; parens/brackets.
(throw 'knr nil)))
(if after-rparen
;; We're inside a paren. Could it be our argument list....?
(if
(and
(progn
(goto-char after-rparen)
(unless (c-go-list-backward) (throw 'knr nil)) ;
;; FIXME!!! What about macros between the parens? 2007/01/20
(setq before-lparen (point)))
;; It can't be the arg list if next token is ; or {
(progn (goto-char after-rparen)
(c-forward-syntactic-ws)
(not (memq (char-after) '(?\; ?\{ ?\=))))
;; Is the thing preceding the list an identifier (the
;; function name), or a macro expansion?
(progn
(goto-char before-lparen)
(eq (c-backward-token-2) 0)
(or (eq (c-on-identifier) (point))
(and (eq (char-after) ?\))
(c-go-up-list-backward)
(eq (c-backward-token-2) 0)
(eq (c-on-identifier) (point)))))
;; Have we got a non-empty list of comma-separated
;; identifiers?
(progn
(goto-char before-lparen)
(and
(c-forward-over-token-and-ws) ; to first token inside parens
(setq id-start (c-on-identifier)) ; Must be at least one.
(catch 'id-list
(while
(progn
(forward-char)
(c-end-of-current-token)
(push (buffer-substring-no-properties id-start
(point))
ids)
(c-forward-syntactic-ws)
(eq (char-after) ?\,))
(c-forward-over-token-and-ws)
(unless (setq id-start (c-on-identifier))
(throw 'id-list nil)))
(eq (char-after) ?\)))))
;; Are all the identifiers in the k&r list up to the
;; current one also in the argument list?
(progn
(forward-char) ; over the )
(setq after-prec-token after-rparen)
(c-forward-syntactic-ws)
(while (and
(or (consp (setq decl-or-cast
(c-forward-decl-or-cast-1
after-prec-token
nil ; Or 'arglist ???
nil)))
(progn
(goto-char after-prec-token)
(c-forward-syntactic-ws)
(setq identifier-ok (eq (char-after) ?{))
nil))
(eq (char-after) ?\;)
(setq after-prec-token (1+ (point)))
(goto-char (car decl-or-cast))
(setq decl-res (c-forward-declarator))
(setq identifier-ok
(member (buffer-substring-no-properties
(car decl-res) (cadr decl-res))
ids))
(progn
(goto-char after-prec-token)
(prog1 (< (point) here)
(c-forward-syntactic-ws))))
(setq identifier-ok nil))
identifier-ok))
;; ...Yes. We've identified the function's argument list.
(throw 'knr
(progn (goto-char after-rparen)
(c-forward-syntactic-ws)
(point)))
;; ...No. The current parens aren't the function's arg list.
(goto-char before-lparen))
(or (c-go-list-backward) ; backwards over [ .... ]
(throw 'knr nil)))))))))
(defun c-skip-conditional ()
;; skip forward over conditional at point, including any predicate
;; statements in parentheses. No error checking is performed.
;;
;; This function might do hidden buffer changes.
(c-forward-sexp (cond
;; else if()
((looking-at (concat "\\<else"
"\\([ \t\n]\\|\\\\\n\\)+"
"if\\>\\([^_]\\|$\\)"))
3)
;; do, else, try, finally
((looking-at (concat "\\<\\("
"do\\|else\\|try\\|finally"
"\\)\\>\\([^_]\\|$\\)"))
1)
;; for, if, while, switch, catch, synchronized, foreach
(t 2))))
(defun c-after-conditional (&optional lim)
;; If looking at the token after a conditional then return the
;; position of its start, otherwise return nil.
;;
;; This function might do hidden buffer changes.
(save-excursion
(and (zerop (c-backward-token-2 1 t lim))
(if (looking-at c-block-stmt-hangon-key)
(zerop (c-backward-token-2 1 t lim))
t)
(or (looking-at c-block-stmt-1-key)
(and (eq (char-after) ?\()
(zerop (c-backward-token-2 1 t lim))
(if (looking-at c-block-stmt-hangon-key)
(zerop (c-backward-token-2 1 t lim))
t)
(or (looking-at c-block-stmt-2-key)
(looking-at c-block-stmt-1-2-key))))
(point))))
(defun c-after-special-operator-id (&optional lim)
;; If the point is after an operator identifier that isn't handled
;; like an ordinary symbol (i.e. like "operator =" in C++) then the
;; position of the start of that identifier is returned. nil is
;; returned otherwise. The point may be anywhere in the syntactic
;; whitespace after the last token of the operator identifier.
;;
;; This function might do hidden buffer changes.
(save-excursion
(and c-overloadable-operators-regexp
(zerop (c-backward-token-2 1 nil lim))
(looking-at c-overloadable-operators-regexp)
(or (not c-opt-op-identifier-prefix)
(and
(zerop (c-backward-token-2 1 nil lim))
(looking-at c-opt-op-identifier-prefix)))
(point))))
(defsubst c-backward-to-block-anchor (&optional lim)
;; Assuming point is at a brace that opens a statement block of some
;; kind, move to the proper anchor point for that block. It might
;; need to be adjusted further by c-add-stmt-syntax, but the
;; position at return is suitable as start position for that
;; function.
;;
;; This function might do hidden buffer changes.
(unless (= (point) (c-point 'boi))
(let ((start (c-after-conditional lim)))
(if start
(goto-char start)))))
(defsubst c-backward-to-decl-anchor (&optional lim)
;; Assuming point is at a brace that opens the block of a top level
;; declaration of some kind, move to the proper anchor point for
;; that block.
;;
;; This function might do hidden buffer changes.
(unless (= (point) (c-point 'boi))
(c-beginning-of-statement-1 lim)))
(defun c-search-decl-header-end ()
;; Search forward for the end of the "header" of the current
;; declaration. That's the position where the definition body
;; starts, or the first variable initializer, or the ending
;; semicolon. I.e. search forward for the closest following
;; (syntactically relevant) '{', '=' or ';' token. Point is left
;; _after_ the first found token, or at point-max if none is found.
;;
;; This function might do hidden buffer changes.
(let ((base (point)))
(if (c-major-mode-is 'c++-mode)
;; In C++ we need to take special care to handle operator
;; tokens and those pesky template brackets.
(while (and
(c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
(or
(c-end-of-current-token base)
;; Handle operator identifiers, i.e. ignore any
;; operator token preceded by "operator".
(save-excursion
(and (c-safe (c-backward-sexp) t)
(looking-at c-opt-op-identifier-prefix)))
(and (eq (char-before) ?<)
(if (c-safe (goto-char (c-up-list-forward (point))))
t
(goto-char (point-max))
nil))))
(setq base (point)))
(while (and
(c-syntactic-re-search-forward "[;{=]" nil 'move t t)
(c-end-of-current-token base))
(setq base (point))))))
(defun c-beginning-of-decl-1 (&optional lim)
;; Go to the beginning of the current declaration, or the beginning
;; of the previous one if already at the start of it. Point won't
;; be moved out of any surrounding paren. Return a cons cell of the
;; form (MOVE . KNR-POS). MOVE is like the return value from
;; `c-beginning-of-statement-1'. If point skipped over some K&R
;; style argument declarations (and they are to be recognized) then
;; KNR-POS is set to the start of the first such argument
;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
;; position that bounds the backward search.
;;
;; NB: Cases where the declaration continues after the block, as in
;; "struct foo { ... } bar;", are currently recognized as two
;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
;;
;; This function might do hidden buffer changes.
(catch 'return
(let* ((start (point))
(last-stmt-start (point))
(move (c-beginning-of-statement-1 lim nil t)))
;; `c-beginning-of-statement-1' stops at a block start, but we
;; want to continue if the block doesn't begin a top level
;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
;; or an open paren.
(let ((beg (point)) tentative-move)
;; Go back one "statement" each time round the loop until we're just
;; after a ;, }, or :, or at BOB or the start of a macro or start of
;; an ObjC method. This will move over a multiple declaration whose
;; components are comma separated.
(while (and
;; Must check with c-opt-method-key in ObjC mode.
(not (and c-opt-method-key
(looking-at c-opt-method-key)))
(/= last-stmt-start (point))
(progn
(c-backward-syntactic-ws lim)
(not (or (memq (char-before) '(?\; ?} ?: nil))
(c-at-vsemi-p))))
(save-excursion
(backward-char)
(not (looking-at "\\s(")))
;; Check that we don't move from the first thing in a
;; macro to its header.
(not (eq (setq tentative-move
(c-beginning-of-statement-1 lim nil t))
'macro)))
(setq last-stmt-start beg
beg (point)
move tentative-move))
(goto-char beg))
(when c-recognize-knr-p
(let ((fallback-pos (point)) knr-argdecl-start)
;; Handle K&R argdecls. Back up after the "statement" jumped
;; over by `c-beginning-of-statement-1', unless it was the
;; function body, in which case we're sitting on the opening
;; brace now. Then test if we're in a K&R argdecl region and
;; that we started at the other side of the first argdecl in
;; it.
(unless (eq (char-after) ?{)
(goto-char last-stmt-start))
(if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
(< knr-argdecl-start start)
(progn
(goto-char knr-argdecl-start)
(not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
(throw 'return
(cons (if (eq (char-after fallback-pos) ?{)
'previous
'same)
knr-argdecl-start))
(goto-char fallback-pos))))
;; `c-beginning-of-statement-1' counts each brace block as a separate
;; statement, so the result will be 'previous if we've moved over any.
;; So change our result back to 'same if necessary.
;;
;; If they were brace list initializers we might not have moved over a
;; declaration boundary though, so change it to 'same if we've moved
;; past a '=' before '{', but not ';'. (This ought to be integrated
;; into `c-beginning-of-statement-1', so we avoid this extra pass which
;; potentially can search over a large amount of text.). Take special
;; pains not to get mislead by C++'s "operator=", and the like.
(if (and (eq move 'previous)
(save-excursion
(and
(progn
(while ; keep going back to "[;={"s until we either find
; no more, or get to one which isn't an "operator ="
(and (c-syntactic-re-search-forward "[;={]" start t t t)
(eq (char-before) ?=)
c-overloadable-operators-regexp
c-opt-op-identifier-prefix
(save-excursion
(eq (c-backward-token-2) 0)
(looking-at c-overloadable-operators-regexp)
(eq (c-backward-token-2) 0)
(looking-at c-opt-op-identifier-prefix))))
(eq (char-before) ?=))
(c-syntactic-re-search-forward "[;{]" start t t)
(eq (char-before) ?{)
(c-safe (goto-char (c-up-list-forward (point))) t)
(not (c-syntactic-re-search-forward ";" start t t)))))
(cons 'same nil)
(cons move nil)))))
(defun c-end-of-decl-1 ()
;; Assuming point is at the start of a declaration (as detected by
;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
;; `c-beginning-of-decl-1', this function handles the case when a
;; block is followed by identifiers in e.g. struct declarations in C
;; or C++. If a proper end was found then t is returned, otherwise
;; point is moved as far as possible within the current sexp and nil
;; is returned. This function doesn't handle macros; use
;; `c-end-of-macro' instead in those cases.
;;
;; This function might do hidden buffer changes.
(let ((start (point)))
(catch 'return
(c-search-decl-header-end)
(when (and c-recognize-knr-p
(eq (char-before) ?\;)
(c-in-knr-argdecl start))
;; Stopped at the ';' in a K&R argdecl section which is
;; detected using the same criteria as in
;; `c-beginning-of-decl-1'. Move to the following block
;; start.
(c-syntactic-re-search-forward "{" nil 'move t))
(when (eq (char-before) ?{)
;; Encountered a block in the declaration. Jump over it.
(condition-case nil
(goto-char (c-up-list-forward (point)))
(error (goto-char (point-max))
(throw 'return nil)))
(if (or (not c-opt-block-decls-with-vars-key)
(save-excursion
(let ((lim (point)))
(goto-char start)
(not (and
;; Check for `c-opt-block-decls-with-vars-key'
;; before the first paren.
(c-syntactic-re-search-forward
(concat "[;=([{]\\|\\("
c-opt-block-decls-with-vars-key
"\\)")
lim t t t)
(match-beginning 1)
(not (eq (char-before) ?_))
;; Check that the first following paren is
;; the block.
(c-syntactic-re-search-forward "[;=([{]"
lim t t t)
(eq (char-before) ?{))))))
;; The declaration doesn't have any of the
;; `c-opt-block-decls-with-vars' keywords in the
;; beginning, so it ends here at the end of the block.
(throw 'return t)))
(while (progn
(if (eq (char-before) ?\;)
(throw 'return t))
(c-syntactic-re-search-forward ";" nil 'move t)))
nil)))
(defun c-looking-at-decl-block (goto-start &optional limit)
;; Assuming the point is at an open brace, check if it starts a
;; block that contains another declaration level, i.e. that isn't a
;; statement block or a brace list, and if so return non-nil.
;;
;; If the check is successful, the return value is the start of the
;; keyword that tells what kind of construct it is, i.e. typically
;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
;; the point will be at the start of the construct, before any
;; leading specifiers, otherwise it's at the returned position.
;;
;; The point is clobbered if the check is unsuccessful.
;;
;; CONTAINING-SEXP is the position of the open of the surrounding
;; paren, or nil if none.
;;
;; The optional LIMIT limits the backward search for the start of
;; the construct. It's assumed to be at a syntactically relevant
;; position.
;;
;; If any template arglists are found in the searched region before
;; the open brace, they get marked with paren syntax.
;;
;; This function might do hidden buffer changes.
(let ((open-brace (point)) kwd-start first-specifier-pos)
(c-syntactic-skip-backward c-block-prefix-charset limit t)
(when (and c-recognize-<>-arglists
(eq (char-before) ?>))
;; Could be at the end of a template arglist.
(let ((c-parse-and-markup-<>-arglists t))
(while (and
(c-backward-<>-arglist nil limit)
(progn
(c-syntactic-skip-backward c-block-prefix-charset limit t)
(eq (char-before) ?>))))))
;; Skip back over noise clauses.
(while (and
c-opt-cpp-prefix
(eq (char-before) ?\))
(let ((after-paren (point)))
(if (and (c-go-list-backward)
(progn (c-backward-syntactic-ws)
(c-simple-skip-symbol-backward))
(or (looking-at c-paren-nontype-key)
(looking-at c-noise-macro-with-parens-name-re)))
(progn
(c-syntactic-skip-backward c-block-prefix-charset limit t)
t)
(goto-char after-paren)
nil))))
;; Note: Can't get bogus hits inside template arglists below since they
;; have gotten paren syntax above.
(when (and
;; If `goto-start' is set we begin by searching for the
;; first possible position of a leading specifier list.
;; The `c-decl-block-key' search continues from there since
;; we know it can't match earlier.
(if goto-start
(when (c-syntactic-re-search-forward c-symbol-start
open-brace t t)
(goto-char (setq first-specifier-pos (match-beginning 0)))
t)
t)
(cond
((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
(goto-char (setq kwd-start (match-beginning 0)))
(and
;; Exclude cases where we matched what would ordinarily
;; be a block declaration keyword, except where it's not
;; legal because it's part of a "compound keyword" like
;; "enum class". Of course, if c-after-brace-list-key
;; is nil, we can skip the test.
(or (equal c-after-brace-list-key regexp-unmatchable)
(save-match-data
(save-excursion
(not
(and
(looking-at c-after-brace-list-key)
(= (c-backward-token-2 1 t) 0)
(looking-at c-brace-list-key))))))
(or
;; Found a keyword that can't be a type?
(match-beginning 1)
;; Can be a type too, in which case it's the return type of a
;; function (under the assumption that no declaration level
;; block construct starts with a type).
(not (c-forward-type))
;; Jumped over a type, but it could be a declaration keyword
;; followed by the declared identifier that we've jumped over
;; instead (e.g. in "class Foo {"). If it indeed is a type
;; then we should be at the declarator now, so check for a
;; valid declarator start.
;;
;; Note: This doesn't cope with the case when a declared
;; identifier is followed by e.g. '(' in a language where '('
;; also might be part of a declarator expression. Currently
;; there's no such language.
(not (or (looking-at c-symbol-start)
(looking-at c-type-decl-prefix-key))))))
;; In Pike a list of modifiers may be followed by a brace
;; to make them apply to many identifiers. Note that the
;; match data will be empty on return in this case.
((and (c-major-mode-is 'pike-mode)
(progn
(goto-char open-brace)
(= (c-backward-token-2) 0))
(looking-at c-specifier-key)
;; Use this variant to avoid yet another special regexp.
(c-keyword-member (c-keyword-sym (match-string 1))
'c-modifier-kwds))
(setq kwd-start (point))
t)))
;; Got a match.
(if goto-start
;; Back up over any preceding specifiers and their clauses
;; by going forward from `first-specifier-pos', which is the
;; earliest possible position where the specifier list can
;; start.
(progn
(goto-char first-specifier-pos)
(while (< (point) kwd-start)
(if (looking-at c-symbol-key)
;; Accept any plain symbol token on the ground that
;; it's a specifier masked through a macro (just
;; like `c-forward-decl-or-cast-1' skip forward over
;; such tokens).
;;
;; Could be more restrictive wrt invalid keywords,
;; but that'd only occur in invalid code so there's
;; no use spending effort on it.
(let ((end (match-end 0))
(kwd-sym (c-keyword-sym (match-string 0))))
(unless
(and kwd-sym
;; Moving over a protection kwd and the following
;; ":" (in C++ Mode) to the next token could take
;; us all the way up to `kwd-start', leaving us
;; no chance to update `first-specifier-pos'.
(not (c-keyword-member kwd-sym 'c-protection-kwds))
(c-forward-keyword-clause 0))
(goto-char end)
(c-forward-syntactic-ws)))
;; Can't parse a declaration preamble and is still
;; before `kwd-start'. That means `first-specifier-pos'
;; was in some earlier construct. Search again.
(if (c-syntactic-re-search-forward c-symbol-start
kwd-start 'move t)
(goto-char (setq first-specifier-pos (match-beginning 0)))
;; Got no preamble before the block declaration keyword.
(setq first-specifier-pos kwd-start))))
(goto-char first-specifier-pos))
(goto-char kwd-start))
kwd-start)))
(defun c-directly-in-class-called-p (name)
;; Check whether point is directly inside a brace block which is the brace
;; block of a class, struct, or union which is called NAME, a string.
(let* ((paren-state (c-parse-state))
(brace-pos (c-pull-open-brace paren-state))
)
(when (eq (char-after brace-pos) ?{)
(goto-char brace-pos)
(save-excursion
; *c-looking-at-decl-block
; containing-sexp goto-start &optional
; limit)
(when (and (c-looking-at-decl-block nil)
(looking-at c-class-key))
(goto-char (match-end 1))
(c-forward-syntactic-ws)
(looking-at name))))))
(defun c-search-uplist-for-classkey (paren-state)
;; Check if the closest containing paren sexp is a declaration
;; block, returning a 2 element vector in that case. Aref 0
;; contains the bufpos at boi of the class key line, and aref 1
;; contains the bufpos of the open brace. This function is an
;; obsolete wrapper for `c-looking-at-decl-block'.
;;
;; This function might do hidden buffer changes.
(let ((open-paren-pos (c-most-enclosing-brace paren-state)))
(when open-paren-pos
(save-excursion
(goto-char open-paren-pos)
(when (and (eq (char-after) ?{)
(c-looking-at-decl-block nil))
(back-to-indentation)
(vector (point) open-paren-pos))))))
(defun c-most-enclosing-decl-block (paren-state)
;; Return the buffer position of the most enclosing decl-block brace (in the
;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
;; none was found.
(let* ((open-brace (c-pull-open-brace paren-state))
(next-open-brace (c-pull-open-brace paren-state)))
(while (and open-brace
(save-excursion
(goto-char open-brace)
(not (c-looking-at-decl-block nil))))
(setq open-brace next-open-brace
next-open-brace (c-pull-open-brace paren-state)))
open-brace))
(defun c-cheap-inside-bracelist-p (paren-state)
;; Return the position of the L-brace if point is inside a brace list
;; initialization of an array, etc. This is an approximate function,
;; designed for speed over accuracy. It will not find every bracelist, but
;; a non-nil result is reliable. We simply search for "= {" (naturally with
;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
;; is everywhere else.
(let (b-pos)
(save-excursion
(while
(and (setq b-pos (c-pull-open-brace paren-state))
(progn (goto-char b-pos)
(c-backward-sws)
(c-backward-token-2)
(not (looking-at "=")))))
b-pos)))
(defun c-backward-typed-enum-colon ()
;; We're at a "{" which might be the opening brace of an enum which is
;; strongly typed (by a ":" followed by a type). If this is the case, leave
;; point before the colon and return t. Otherwise leave point unchanged and return nil.
;; Match data will be clobbered.
(let ((here (point))
(colon-pos nil))
(save-excursion
(while
(and (eql (c-backward-token-2) 0)
(or (not (looking-at "\\s)"))
(c-go-up-list-backward))
(cond
((looking-at "::")
t)
((and (eql (char-after) ?:)
(save-excursion
(c-backward-syntactic-ws)
(or (c-on-identifier)
(progn
(c-backward-token-2)
(looking-at c-brace-list-key)))))
(setq colon-pos (point))
(forward-char)
(c-forward-syntactic-ws)
(or (and (c-forward-type)
(progn (c-forward-syntactic-ws)
(eq (point) here)))
(setq colon-pos nil))
nil)
((eql (char-after) ?\()
t)
((looking-at c-symbol-key)
t)
(t nil)))))
(when colon-pos
(goto-char colon-pos)
t)))
(defun c-backward-over-enum-header ()
;; We're at a "{". Move back to the enum-like keyword that starts this
;; declaration and return t, otherwise don't move and return nil.
(let ((here (point))
before-identifier)
(when c-recognize-post-brace-list-type-p
(c-backward-typed-enum-colon))
(while
(and
(eq (c-backward-token-2) 0)
(or (not (looking-at "\\s)"))
(c-go-up-list-backward))
(cond
((and (looking-at c-symbol-key) (c-on-identifier)
(not before-identifier))
(setq before-identifier t))
((and before-identifier
(or (eql (char-after) ?,)
(looking-at c-postfix-decl-spec-key)))
(setq before-identifier nil)
t)
((looking-at c-after-brace-list-key) t)
((looking-at c-brace-list-key) nil)
((eq (char-after) ?\()
(and (eq (c-backward-token-2) 0)
(or (looking-at c-decl-hangon-key)
(and c-opt-cpp-prefix
(looking-at c-noise-macro-with-parens-name-re)))))
((and c-recognize-<>-arglists
(eq (char-after) ?<)
(looking-at "\\s("))
t)
(t nil))))
(or (looking-at c-brace-list-key)
(progn (goto-char here) nil))))
(defun c-looking-at-or-maybe-in-bracelist (&optional containing-sexp lim)
;; Point is at an open brace. If this starts a brace list, return a list
;; whose car is the buffer position of the start of the construct which
;; introduces the list, and whose cdr is the symbol `in-paren' if the brace
;; is directly enclosed in a parenthesis form (i.e. an arglist), t if we
;; have parsed a keyword matching `c-opt-inexpr-brace-list-key' (e.g. Java's
;; "new"), nil otherwise. Otherwise, if point might be inside an enclosing
;; brace list, return t. If point is definitely neither at nor in a brace
;; list, return nil.
;;
;; CONTAINING-SEXP is the position of the brace/paren/bracket enclosing
;; POINT, or nil if there is no such position, or we do not know it. LIM is
;; a backward search limit.
;;
;; The determination of whether the brace starts a brace list is solely by
;; the context of the brace, not by its contents.
;;
;; Here, "brace list" does not include the body of an enum.
(save-excursion
(let ((start (point))
(class-key
;; Pike can have class definitions anywhere, so we must
;; check for the class key here.
(and (c-major-mode-is 'pike-mode)
c-decl-block-key))
(braceassignp 'dontknow)
inexpr-brace-list bufpos macro-start res pos after-type-id-pos
in-paren parens-before-brace)
(setq res (c-backward-token-2 1 t lim))
;; Checks to do only on the first sexp before the brace.
;; Have we a C++ initialization, without an "="?
(if (and (c-major-mode-is 'c++-mode)
(cond
((and (or (not (eq res 0))
(eq (char-after) ?,))
(c-go-up-list-backward nil lim) ; FIXME!!! Check ; `lim' 2016-07-12.
(eq (char-after) ?\())
(setq braceassignp 'c++-noassign
in-paren 'in-paren))
((looking-at c-pre-id-bracelist-key)
(setq braceassignp nil))
((looking-at c-return-key))
((and (looking-at c-symbol-start)
(not (looking-at c-keywords-regexp)))
(setq after-type-id-pos (point)))
((eq (char-after) ?\()
(setq parens-before-brace t)
nil)
(t nil))
(save-excursion
(cond
((or (not (eq res 0))
(eq (char-after) ?,))
(and (c-go-up-list-backward nil lim) ; FIXME!!! Check `lim' 2016-07-12.
(eq (char-after) ?\()
(setq in-paren 'in-paren)))
((looking-at c-pre-id-bracelist-key))
((looking-at c-return-key))
(t (setq after-type-id-pos (point))
nil))))
(setq braceassignp 'c++-noassign))
(when (and c-opt-inexpr-brace-list-key
(eq (char-after) ?\[))
;; In Java, an initialization brace list may follow
;; directly after "new Foo[]", so check for a "new"
;; earlier.
(while (eq braceassignp 'dontknow)
(setq braceassignp
(cond ((/= (c-backward-token-2 1 t lim) 0) nil)
((looking-at c-opt-inexpr-brace-list-key)
(setq inexpr-brace-list t)
t)
((looking-at "\\sw\\|\\s_\\|[.[]")
;; Carry on looking if this is an
;; identifier (may contain "." in Java)
;; or another "[]" sexp.
'dontknow)
(t nil)))))
(setq pos (point))
(cond
((not braceassignp)
nil)
((and after-type-id-pos
(goto-char after-type-id-pos)
(setq res (c-back-over-member-initializers))
(goto-char res)
(eq (car (c-beginning-of-decl-1 lim)) 'same))
(cons (point) nil)) ; Return value.
((and after-type-id-pos
(progn
(c-backward-syntactic-ws)
(eq (char-before) ?\()))
;; Single identifier between '(' and '{'. We have a bracelist.
(cons after-type-id-pos 'in-paren))
;; Are we at the parens of a C++ lambda expression?
((and parens-before-brace
(save-excursion
(and
(zerop (c-backward-token-2 1 t lim))
(c-looking-at-c++-lambda-capture-list))))
nil) ; a lambda expression isn't a brace list.
(t
(goto-char pos)
;; Checks to do on all sexps before the brace, up to the
;; beginning of the statement.
(while (eq braceassignp 'dontknow)
(cond ((eq (char-after) ?\;)
(setq braceassignp nil))
((and class-key
(looking-at class-key))
(setq braceassignp nil))
((and c-has-compound-literals
(looking-at c-return-key))
(setq braceassignp t)
nil)
((eq (char-after) ?=)
;; We've seen a =, but must check earlier tokens so
;; that it isn't something that should be ignored.
(setq braceassignp 'maybe)
(while (and (eq braceassignp 'maybe)
(zerop (c-backward-token-2 1 t lim)))
(setq braceassignp
(cond
;; Check for operator =
((and c-opt-op-identifier-prefix
(looking-at c-opt-op-identifier-prefix))
nil)
;; Check for `<opchar>= in Pike.
((and (c-major-mode-is 'pike-mode)
(or (eq (char-after) ?`)
;; Special case for Pikes
;; `[]=, since '[' is not in
;; the punctuation class.
(and (eq (char-after) ?\[)
(eq (char-before) ?`))))
nil)
((looking-at "\\s.") 'maybe)
;; make sure we're not in a C++ template
;; argument assignment
((and
(c-major-mode-is 'c++-mode)
(save-excursion
(let ((here (point))
(pos< (progn
(skip-chars-backward "^<>")
(point))))
(and (eq (char-before) ?<)
(not (c-crosses-statement-barrier-p
pos< here))
(not (c-in-literal))
))))
nil)
(t t)))))
((and
(c-major-mode-is 'c++-mode)
(eq (char-after) ?\[)
;; Be careful of "operator []"
(not (save-excursion
(c-backward-token-2 1 nil lim)
(looking-at c-opt-op-identifier-prefix))))
(setq braceassignp t)
nil))
(when (eq braceassignp 'dontknow)
(cond ((and
(not (eq (char-after) ?,))
(save-excursion
(c-backward-syntactic-ws)
(eq (char-before) ?})))
(setq braceassignp nil))
((/= (c-backward-token-2 1 t lim) 0)
(if (save-excursion
(and c-has-compound-literals
(eq (c-backward-token-2 1 nil lim) 0)
(eq (char-after) ?\()))
(setq braceassignp t)
(setq braceassignp nil))))))
(cond
(braceassignp
;; We've hit the beginning of the aggregate list.
(c-beginning-of-statement-1 containing-sexp)
(cons (point) (or in-paren inexpr-brace-list)))
((and after-type-id-pos
(save-excursion
(when (eq (char-after) ?\;)
(c-forward-over-token-and-ws t))
(setq bufpos (point))
(when (looking-at c-opt-<>-sexp-key)
(c-forward-over-token-and-ws)
(when (and (eq (char-after) ?<)
(c-get-char-property (point) 'syntax-table))
(c-go-list-forward nil after-type-id-pos)
(c-forward-syntactic-ws)))
(and
(or (not (looking-at c-class-key))
(save-excursion
(goto-char (match-end 1))
(c-forward-syntactic-ws)
(not (eq (point) after-type-id-pos))))
(progn
(setq res
(c-forward-decl-or-cast-1
(save-excursion (c-backward-syntactic-ws) (point))
nil nil))
(and (consp res)
(cond
((eq (car res) after-type-id-pos))
((> (car res) after-type-id-pos) nil)
(t
(catch 'find-decl
(save-excursion
(goto-char (car res))
(c-do-declarators
(point-max) t nil nil
(lambda (id-start _id-end _tok _not-top _func _init)
(cond
((> id-start after-type-id-pos)
(throw 'find-decl nil))
((eq id-start after-type-id-pos)
(throw 'find-decl t)))))
nil)))))))))
(cons bufpos (or in-paren inexpr-brace-list)))
((or (eq (char-after) ?\;)
;; Brace lists can't contain a semicolon, so we're done.
(save-excursion
(c-backward-syntactic-ws)
(eq (char-before) ?}))
;; They also can't contain a bare }, which is probably the end
;; of a function.
)
nil)
((and (setq macro-start (point))
(c-forward-to-cpp-define-body)
(eq (point) start))
;; We've a macro whose expansion starts with the '{'.
;; Heuristically, if we have a ';' in it we've not got a
;; brace list, otherwise we have.
(let ((macro-end (progn (c-end-of-macro) (point))))
(goto-char start)
(forward-char)
(if (and (c-syntactic-re-search-forward "[;,]" macro-end t t)
(eq (char-before) ?\;))
nil
(cons macro-start nil)))) ; (2016-08-30): Lazy! We have no
; languages where
; `c-opt-inexpr-brace-list-key' is
; non-nil and we have macros.
(t t)))) ;; The caller can go up one level.
)))
(defun c-inside-bracelist-p (containing-sexp paren-state accept-in-paren)
;; return the buffer position of the beginning of the brace list statement
;; if CONTAINING-SEXP is inside a brace list, otherwise return nil.
;;
;; CONTAINING-SEXP is the buffer pos of the innermost containing paren. NO
;; IT ISN'T!!! [This function is badly designed, and probably needs
;; reformulating without its first argument, and the critical position being
;; at point.]
;;
;; PAREN-STATE is the remainder of the state of enclosing braces.
;; ACCEPT-IN-PAREN is non-nil iff we will accept as a brace list a brace
;; directly enclosed in a parenthesis.
;;
;; The "brace list" here is recognized solely by its context, not by
;; its contents.
;;
;; N.B.: This algorithm can potentially get confused by cpp macros
;; placed in inconvenient locations. It's a trade-off we make for
;; speed.
;;
;; This function might do hidden buffer changes.
(or
;; This will pick up brace list declarations.
(save-excursion
(goto-char containing-sexp)
(and (c-backward-over-enum-header)
(point)))
;; this will pick up array/aggregate init lists, even if they are nested.
(save-excursion
(let ((bufpos t)
next-containing)
(while (and (eq bufpos t)
containing-sexp)
(when paren-state
(setq next-containing (c-pull-open-brace paren-state)))
(goto-char containing-sexp)
(if (c-looking-at-inexpr-block next-containing next-containing)
;; We're in an in-expression block of some kind. Do not
;; check nesting. We deliberately set the limit to the
;; containing sexp, so that c-looking-at-inexpr-block
;; doesn't check for an identifier before it.
(setq bufpos nil)
(if (not (eq (char-after) ?{))
(setq bufpos nil)
(when (eq (setq bufpos (c-looking-at-or-maybe-in-bracelist
next-containing next-containing))
t)
(setq containing-sexp next-containing
next-containing nil)))))
(and (consp bufpos)
(or accept-in-paren (not (eq (cdr bufpos) 'in-paren)))
(car bufpos))))))
(defun c-looking-at-special-brace-list ()
;; If we're looking at the start of a pike-style list, i.e., `({ })',
;; `([ ])', `(< >)', etc., a cons of a cons of its starting and ending
;; positions and its entry in c-special-brace-lists is returned, nil
;; otherwise. The ending position is nil if the list is still open.
;; LIM is the limit for forward search. The point may either be at
;; the `(' or at the following paren character. Tries to check the
;; matching closer, but assumes it's correct if no balanced paren is
;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
;; a special brace list).
;;
;; This function might do hidden buffer changes.
(if c-special-brace-lists
(condition-case ()
(save-excursion
(let ((beg (point))
inner-beg end type)
(c-forward-syntactic-ws)
(if (eq (char-after) ?\()
(progn
(forward-char 1)
(c-forward-syntactic-ws)
(setq inner-beg (point))
(setq type (assq (char-after) c-special-brace-lists)))
(if (setq type (assq (char-after) c-special-brace-lists))
(progn
(setq inner-beg (point))
(c-backward-syntactic-ws)
(forward-char -1)
(setq beg (if (eq (char-after) ?\()
(point)
nil)))))
(if (and beg type)
(if (and (c-safe
(goto-char beg)
(c-forward-sexp 1)
(setq end (point))
(= (char-before) ?\)))
(c-safe
(goto-char inner-beg)
(if (looking-at "\\s(")
;; Check balancing of the inner paren
;; below.
(progn
(c-forward-sexp 1)
t)
;; If the inner char isn't a paren then
;; we can't check balancing, so just
;; check the char before the outer
;; closing paren.
(goto-char end)
(backward-char)
(c-backward-syntactic-ws)
(= (char-before) (cdr type)))))
(if (or (/= (char-syntax (char-before)) ?\))
(= (progn
(c-forward-syntactic-ws)
(point))
(1- end)))
(cons (cons beg end) type))
(cons (list beg) type)))))
(error nil))))
(defun c-looking-at-statement-block ()
;; Point is at an opening brace. If this is a statement block (i.e. the
;; elements in the block are terminated by semicolons, or the block is
;; empty, or the block contains a keyword) return non-nil. Otherwise,
;; return nil.
(let ((here (point)))
(prog1
(if (c-go-list-forward)
(let ((there (point)))
(backward-char)
(c-syntactic-skip-backward "^;," here t)
(cond
((eq (char-before) ?\;) t)
((eq (char-before) ?,) nil)
(t ; We're at (1+ here).
(cond
((progn (c-forward-syntactic-ws)
(eq (point) (1- there))))
((c-syntactic-re-search-forward c-keywords-regexp there t))
((c-syntactic-re-search-forward "{" there t t)
(backward-char)
(c-looking-at-statement-block))
(t nil)))))
(forward-char)
(cond
((c-syntactic-re-search-forward "[;,]" nil t t)
(eq (char-before) ?\;))
((progn (c-forward-syntactic-ws)
(eobp)))
((c-syntactic-re-search-forward c-keywords-regexp nil t t))
((c-syntactic-re-search-forward "{" nil t t)
(backward-char)
(c-looking-at-statement-block))
(t nil)))
(goto-char here))))
(defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
;; Return non-nil if we're looking at the beginning of a block
;; inside an expression. The value returned is actually a cons of
;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
;; position of the beginning of the construct.
;;
;; LIM limits the backward search. CONTAINING-SEXP is the start
;; position of the closest containing list. If it's nil, the
;; containing paren isn't used to decide whether we're inside an
;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
;; needs to be farther back.
;;
;; If CHECK-AT-END is non-nil then extra checks at the end of the
;; brace block might be done. It should only be used when the
;; construct can be assumed to be complete, i.e. when the original
;; starting position was further down than that.
;;
;; This function might do hidden buffer changes.
(save-excursion
(let ((res 'maybe) (passed-bracket-pairs 0) bracket-pos passed-paren
haskell-op-pos
(closest-lim (or containing-sexp lim (point-min)))
;; Look at the character after point only as a last resort
;; when we can't disambiguate.
(block-follows (and (eq (char-after) ?{) (point))))
;; Search for a C++11 "->" which suggests a lambda declaration.
(when (and (c-major-mode-is 'c++-mode)
(setq haskell-op-pos
(save-excursion
(while
(progn
(c-syntactic-skip-backward "^;=}>" closest-lim t)
(and (eq (char-before) ?>)
(c-backward-token-2)
(not (looking-at c-haskell-op-re)))))
(and (looking-at c-haskell-op-re)
(point)))))
(goto-char haskell-op-pos))
(while (and (eq res 'maybe)
(progn (c-backward-syntactic-ws)
(> (point) closest-lim))
(not (bobp))
(progn (backward-char)
(looking-at "[]).]\\|\\w\\|\\s_"))
(c-safe (forward-char)
(goto-char (scan-sexps (point) -1))))
(setq res
(if (looking-at c-keywords-regexp)
(let ((kw-sym (c-keyword-sym (match-string 1))))
(cond
((and block-follows
(c-keyword-member kw-sym 'c-inexpr-class-kwds))
(and (not (eq passed-paren ?\[))
(or (not (looking-at c-class-key))
;; If the class definition is at the start of
;; a statement, we don't consider it an
;; in-expression class.
(let ((prev (point)))
(while (and
(= (c-backward-token-2 1 nil closest-lim) 0)
(eq (char-syntax (char-after)) ?w))
(setq prev (point)))
(goto-char prev)
(not (c-at-statement-start-p)))
;; Also, in Pike we treat it as an
;; in-expression class if it's used in an
;; object clone expression.
(save-excursion
(and check-at-end
(c-major-mode-is 'pike-mode)
(progn (goto-char block-follows)
(zerop (c-forward-token-2 1 t)))
(eq (char-after) ?\())))
(cons 'inexpr-class (point))))
((c-keyword-member kw-sym 'c-paren-any-kwds) ; e.g. C++11 "throw" or "noexcept"
(setq passed-paren nil)
(setq passed-bracket-pairs 0)
(setq bracket-pos nil)
'maybe)
((c-keyword-member kw-sym 'c-inexpr-block-kwds)
(when (not passed-paren)
(cons 'inexpr-statement (point))))
((c-keyword-member kw-sym 'c-lambda-kwds)
(when (or (not passed-paren)
(eq passed-paren ?\())
(cons 'inlambda (point))))
((c-keyword-member kw-sym 'c-block-stmt-kwds)
nil)
(t
'maybe)))
(if (looking-at "\\s(")
(if passed-paren
(cond
((and (eq passed-paren ?\[)
(eq (char-after) ?\[)
(not (eq (char-after (1+ (point))) ?\[))) ; C++ attribute.
;; Accept several square bracket sexps for
;; Java array initializations.
(setq passed-bracket-pairs (1+ passed-bracket-pairs))
'maybe)
((and (eq passed-paren ?\()
(eq (char-after) ?\[)
(not (eq (char-after (1+ (point))) ?\[))
(eq passed-bracket-pairs 0))
;; C++11 lambda function declaration
(setq passed-bracket-pairs 1)
(setq bracket-pos (point))
'maybe)
(t nil))
(when (not (looking-at "\\[\\["))
(setq passed-paren (char-after))
(when (eq passed-paren ?\[)
(setq passed-bracket-pairs 1)
(setq bracket-pos (point))))
'maybe)
'maybe))))
(if (eq res 'maybe)
(cond
((and (c-major-mode-is 'c++-mode)
block-follows
(eq passed-bracket-pairs 1)
(save-excursion
(goto-char bracket-pos)
(or (<= (point) (or lim (point-min)))
(progn
(c-backward-token-2 1 nil lim)
(and
(not (and (c-on-identifier)
(looking-at c-symbol-char-key)))
(not (looking-at c-opt-op-identifier-prefix)))))))
(cons 'inlambda bracket-pos))
((and c-recognize-paren-inexpr-blocks
block-follows
containing-sexp
(eq (char-after containing-sexp) ?\())
(goto-char containing-sexp)
(if (or (save-excursion
(c-backward-syntactic-ws lim)
(while (and (eq (char-before) ?>)
(c-get-char-property (1- (point))
'syntax-table)
(c-go-list-backward nil lim))
(c-backward-syntactic-ws lim))
(and (> (point) (or lim (point-min)))
(c-on-identifier)))
(and c-special-brace-lists
(c-looking-at-special-brace-list))
(and c-has-compound-literals
(save-excursion
(goto-char block-follows)
(not (c-looking-at-statement-block)))))
nil
(cons 'inexpr-statement (point)))))
res))))
(defun c-looking-at-inexpr-block-backward (paren-state)
;; Returns non-nil if we're looking at the end of an in-expression
;; block, otherwise the same as `c-looking-at-inexpr-block'.
;; PAREN-STATE is the paren state relevant at the current position.
;;
;; This function might do hidden buffer changes.
(save-excursion
;; We currently only recognize a block.
(let ((here (point))
(elem (car-safe paren-state))
containing-sexp)
(when (and (consp elem)
(progn (goto-char (cdr elem))
(c-forward-syntactic-ws here)
(= (point) here)))
(goto-char (car elem))
(if (setq paren-state (cdr paren-state))
(setq containing-sexp (car-safe paren-state)))
(c-looking-at-inexpr-block (c-safe-position containing-sexp
paren-state)
containing-sexp)))))
(defun c-looking-at-c++-lambda-capture-list ()
;; Return non-nil if we're at the opening "[" of the capture list of a C++
;; lambda function, nil otherwise.
(and
(eq (char-after) ?\[)
(not (eq (char-before) ?\[))
(not (eq (char-after (1+ (point))) ?\[))
(save-excursion
(or (eq (c-backward-token-2 1) 1)
(looking-at c-pre-lambda-tokens-re)))
(not (c-in-literal))))
(defun c-at-macro-vsemi-p (&optional pos)
;; Is there a "virtual semicolon" at POS or point?
;; (See cc-defs.el for full details of "virtual semicolons".)
;;
;; This is true when point is at the last non syntactic WS position on the
;; line, there is a macro call last on the line, and this particular macro's
;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
;; semicolon.
(save-excursion
(save-restriction
(widen)
(if pos
(goto-char pos)
(setq pos (point)))
(and
c-macro-with-semi-re
(eq (skip-chars-backward " \t") 0)
;; Check we've got nothing after this except comments and empty lines
;; joined by escaped EOLs.
(skip-chars-forward " \t") ; always returns non-nil.
(progn
(while ; go over 1 block comment per iteration.
(and
(looking-at "\\(\\\\[\n\r][ \t]*\\)*")
(goto-char (match-end 0))
(cond
((looking-at c-block-comment-start-regexp)
(and (forward-comment 1)
(skip-chars-forward " \t"))) ; always returns non-nil
((looking-at c-line-comment-start-regexp)
(end-of-line)
nil)
(t nil))))
(eolp))
(goto-char pos)
(progn (c-backward-syntactic-ws)
(eq (point) pos))
;; Check for one of the listed macros being before point.
(or (not (eq (char-before) ?\)))
(when (c-go-list-backward)
(c-backward-syntactic-ws)
t))
(c-simple-skip-symbol-backward)
(looking-at c-macro-with-semi-re)
(goto-char pos)
(not (c-in-literal)))))) ; The most expensive check last.
(defun c-macro-vsemi-status-unknown-p () t) ; See cc-defs.el.
\f
;; `c-guess-basic-syntax' and the functions that precedes it below
;; implements the main decision tree for determining the syntactic
;; analysis of the current line of code.
;; Dynamically bound to t when `c-guess-basic-syntax' is called during
;; auto newline analysis.
(defvar c-auto-newline-analysis nil)
(defun c-brace-anchor-point (bracepos)
;; BRACEPOS is the position of a brace in a construct like "namespace
;; Bar {". Return the anchor point in this construct; this is the
;; earliest symbol on the brace's line which isn't earlier than
;; "namespace".
;;
;; Currently (2007-08-17), "like namespace" means "matches
;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
;; or anything like that.
(save-excursion
(let ((boi (c-point 'boi bracepos)))
(goto-char bracepos)
(while (and (> (point) boi)
(not (looking-at c-other-decl-block-key)))
(c-backward-token-2))
(if (> (point) boi) (point) boi))))
(defsubst c-add-syntax (symbol &rest args)
;; A simple function to prepend a new syntax element to
;; `c-syntactic-context'. Using `setq' on it is unsafe since it
;; should always be dynamically bound but since we read it first
;; we'll fail properly anyway if this function is misused.
(setq c-syntactic-context (cons (cons symbol args)
c-syntactic-context)))
(defsubst c-append-syntax (symbol &rest args)
;; Like `c-add-syntax' but appends to the end of the syntax list.
;; (Normally not necessary.)
(setq c-syntactic-context (nconc c-syntactic-context
(list (cons symbol args)))))
(defun c-add-stmt-syntax (syntax-symbol
syntax-extra-args
stop-at-boi-only
containing-sexp
paren-state
&optional fixed-anchor)
;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
;; needed with further syntax elements of the types `substatement',
;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro',
;; `defun-block-intro', and `brace-list-intro'.
;;
;; Do the generic processing to anchor the given syntax symbol on the
;; preceding statement: First skip over any labels and containing statements
;; on the same line. If FIXED-ANCHOR is non-nil, use this as the
;; anchor-point for the given syntactic symbol, and don't make syntactic
;; entries for constructs beginning on lines before that containing
;; ANCHOR-POINT. Otherwise search backward until we find a statement or
;; block start that begins at boi without a label or comment.
;;
;; Point is assumed to be at the prospective anchor point for the
;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
;; skip past open parens and containing statements. Most of the added
;; syntax elements will get the same anchor point - the exception is
;; for an anchor in a construct like "namespace"[*] - this is as early
;; as possible in the construct but on the same line as the {.
;;
;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
;;
;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
;; syntax symbol. They are appended after the anchor point.
;;
;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
;; if the current statement starts there.
;;
;; Note: It's not a problem if PAREN-STATE "overshoots"
;; CONTAINING-SEXP, i.e. contains info about parens further down.
;;
;; This function might do hidden buffer changes.
(if (= (point) (c-point 'boi))
;; This is by far the most common case, so let's give it special
;; treatment.
(apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
(let ((syntax-last c-syntactic-context)
(boi (c-point 'boi))
(anchor-boi (c-point 'boi))
;; Set when we're on a label, so that we don't stop there.
;; FIXME: To be complete we should check if we're on a label
;; now at the start.
on-label)
;; Use point as the anchor point for "namespace", "extern", etc.
(apply 'c-add-syntax syntax-symbol
(if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
(point) nil)
syntax-extra-args)
;; Loop while we have to back out of containing blocks.
(while
(and
(catch 'back-up-block
;; Loop while we have to back up statements.
(while (or (/= (point) boi)
on-label
(looking-at c-comment-start-regexp))
;; Skip past any comments that stands between the
;; statement start and boi.
(let ((savepos (point)))
(while (and (/= savepos boi)
(c-backward-single-comment))
(setq savepos (point)
boi (c-point 'boi)))
(goto-char savepos))
;; Skip to the beginning of this statement or backward
;; another one.
(let ((old-pos (point))
(old-boi boi)
(step-type (c-beginning-of-statement-1 containing-sexp)))
(setq boi (c-point 'boi)
on-label (eq step-type 'label))
(cond ((= (point) old-pos)
;; If we didn't move we're at the start of a block and
;; have to continue outside it.
(throw 'back-up-block t))
((and (eq step-type 'up)
(>= (point) old-boi)
(looking-at "else\\>[^_]")
(save-excursion
(goto-char old-pos)
(looking-at "if\\>[^_]")))
;; Special case to avoid deeper and deeper indentation
;; of "else if" clauses.
)
((and (not stop-at-boi-only)
(/= old-pos old-boi)
(memq step-type '(up previous)))
;; If stop-at-boi-only is nil, we shouldn't back up
;; over previous or containing statements to try to
;; reach boi, so go back to the last position and
;; exit.
(goto-char old-pos)
(throw 'back-up-block nil))
(t
(if (and (not stop-at-boi-only)
(memq step-type '(up previous beginning)))
;; If we've moved into another statement then we
;; should no longer try to stop in the middle of a
;; line.
(setq stop-at-boi-only t))
;; Record this as a substatement if we skipped up one
;; level.
(when (eq step-type 'up)
(c-add-syntax 'substatement nil))))
)))
containing-sexp
(or (null fixed-anchor)
(> containing-sexp anchor-boi)))
;; Now we have to go out of this block.
(goto-char containing-sexp)
;; Don't stop in the middle of a special brace list opener
;; like "({".
(when c-special-brace-lists
(let ((special-list (c-looking-at-special-brace-list)))
(when (and special-list
(< (car (car special-list)) (point)))
(setq containing-sexp (car (car special-list)))
(goto-char containing-sexp))))
(setq paren-state (c-whack-state-after containing-sexp paren-state)
containing-sexp (c-most-enclosing-brace paren-state)
boi (c-point 'boi))
;; Analyze the construct in front of the block we've stepped out
;; from and add the right syntactic element for it.
(let ((paren-pos (point))
(paren-char (char-after))
step-type)
(if (eq paren-char ?\()
;; Stepped out of a parenthesis block, so we're in an
;; expression now.
(progn
(when (/= paren-pos boi)
(if (and c-recognize-paren-inexpr-blocks
(progn
(c-backward-syntactic-ws containing-sexp)
(or (not (looking-at "\\>"))
(not (c-on-identifier))))
(save-excursion
(goto-char (1+ paren-pos))
(c-forward-syntactic-ws)
(eq (char-after) ?{)))
;; Stepped out of an in-expression statement. This
;; syntactic element won't get an anchor pos.
(c-add-syntax 'inexpr-statement)
;; A parenthesis normally belongs to an arglist.
(c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
(goto-char (max boi
(if containing-sexp
(1+ containing-sexp)
(point-min))))
(setq step-type 'same
on-label nil))
;; Stepped out of a brace block.
(setq step-type (c-beginning-of-statement-1 containing-sexp)
on-label (eq step-type 'label))
(if (and (eq step-type 'same)
(/= paren-pos (point)))
(let (inexpr bspec)
(cond
((save-excursion
(goto-char paren-pos)
(setq inexpr (c-looking-at-inexpr-block
(c-safe-position containing-sexp paren-state)
containing-sexp)))
(c-add-syntax (if (eq (car inexpr) 'inlambda)
'defun-block-intro
'statement-block-intro)
nil))
((looking-at c-other-decl-block-key)
(c-add-syntax
(cdr (assoc (match-string 1)
c-other-decl-block-key-in-symbols-alist))
(max (c-point 'boi paren-pos) (point))))
((c-inside-bracelist-p paren-pos paren-state nil)
(if (save-excursion
(goto-char paren-pos)
(c-looking-at-statement-block))
(c-add-syntax 'defun-block-intro nil)
(c-add-syntax 'brace-list-intro nil)))
((save-excursion
(goto-char paren-pos)
(setq bspec (c-looking-at-or-maybe-in-bracelist
containing-sexp containing-sexp))
(and (consp bspec)
(eq (cdr bspec) 'in-paren)))
(c-add-syntax 'brace-list-intro (car bspec)))
(t (c-add-syntax 'defun-block-intro nil))))
(c-add-syntax 'statement-block-intro nil)))
(if (= paren-pos boi)
;; Always done if the open brace was at boi. The
;; c-beginning-of-statement-1 call above is necessary
;; anyway, to decide the type of block-intro to add.
(goto-char paren-pos)
(setq boi (c-point 'boi)))
))
;; Fill in the current point as the anchor for all the symbols
;; added above.
(let ((p c-syntactic-context) q)
(while (not (eq p syntax-last))
(setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
(while q
(unless (car q)
(setcar q (if (or (cdr p)
(null fixed-anchor))
(point)
fixed-anchor)))
(setq q (cdr q)))
(setq p (cdr p))))
)))
(defun c-add-class-syntax (symbol
containing-decl-open
containing-decl-start
containing-decl-kwd)
;; The inclass and class-close syntactic symbols are added in
;; several places and some work is needed to fix everything.
;; Therefore it's collected here.
;;
;; This function might do hidden buffer changes.
(goto-char containing-decl-open)
(if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
(progn
(c-add-syntax symbol containing-decl-open)
containing-decl-open)
(goto-char containing-decl-start)
;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
;; here, but we have to do like this for compatibility.
(back-to-indentation)
(c-add-syntax symbol (point))
(if (and (c-keyword-member containing-decl-kwd
'c-inexpr-class-kwds)
(/= containing-decl-start (c-point 'boi containing-decl-start)))
(c-add-syntax 'inexpr-class))
(point)))
(defun c-guess-continued-construct (indent-point
char-after-ip
beg-of-same-or-containing-stmt
containing-sexp
paren-state)
;; This function contains the decision tree reached through both
;; cases 18 and 10. It's a continued statement or top level
;; construct of some kind.
;;
;; This function might do hidden buffer changes.
(let (special-brace-list placeholder)
(goto-char indent-point)
(skip-chars-forward " \t")
(cond
;; (CASE A removed.)
;; CASE B: open braces for class or brace-lists
((setq special-brace-list
(or (and c-special-brace-lists
(c-looking-at-special-brace-list))
(eq char-after-ip ?{)))
(cond
;; CASE B.1: class-open
((save-excursion
(and (eq (char-after) ?{)
(c-looking-at-decl-block t)
(setq beg-of-same-or-containing-stmt (point))))
(c-add-syntax 'class-open beg-of-same-or-containing-stmt))
;; CASE B.2: brace-list-open
((or (consp special-brace-list)
(c-inside-bracelist-p (point)
(cons containing-sexp paren-state)
nil))
;; The most semantically accurate symbol here is
;; brace-list-open, but we normally report it simply as a
;; statement-cont. The reason is that one normally adjusts
;; brace-list-open for brace lists as top-level constructs,
;; and brace lists inside statements is a completely different
;; context. C.f. case 5A.3.
(c-beginning-of-statement-1 containing-sexp)
(c-add-stmt-syntax (if c-auto-newline-analysis
;; Turn off the dwim above when we're
;; analyzing the nature of the brace
;; for the auto newline feature.
'brace-list-open
'statement-cont)
nil nil
containing-sexp paren-state))
;; CASE B.3: The body of a function declared inside a normal
;; block. Can occur e.g. in Pike and when using gcc
;; extensions, but watch out for macros followed by blocks.
;; C.f. cases E, 16F and 17G.
((and (not (c-at-statement-start-p))
(eq (c-beginning-of-statement-1 containing-sexp nil nil t)
'same)
(save-excursion
(let ((c-recognize-typeless-decls nil))
;; Turn off recognition of constructs that lacks a
;; type in this case, since that's more likely to be
;; a macro followed by a block.
(c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
(c-add-stmt-syntax 'defun-open nil t
containing-sexp paren-state))
;; CASE B.5: We have a C++11 "return \n { ..... }" Note that we're
;; not at the "{", currently.
((progn (goto-char indent-point)
(backward-sexp)
(looking-at c-return-key))
(c-add-stmt-syntax 'statement-cont nil t
containing-sexp paren-state))
;; CASE B.4: Continued statement with block open. The most
;; accurate analysis is perhaps `statement-cont' together with
;; `block-open' but we play DWIM and use `substatement-open'
;; instead. The rationale is that this typically is a macro
;; followed by a block which makes it very similar to a
;; statement with a substatement block.
(t
(c-add-stmt-syntax 'substatement-open nil nil
containing-sexp paren-state))
))
;; CASE C: iostream insertion or extraction operator
((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
(save-excursion
(goto-char beg-of-same-or-containing-stmt)
;; If there is no preceding streamop in the statement
;; then indent this line as a normal statement-cont.
(when (c-syntactic-re-search-forward
"\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
(c-add-syntax 'stream-op (c-point 'boi))
t))))
;; CASE E: In the "K&R region" of a function declared inside a
;; normal block. C.f. case B.3.
((and (save-excursion
;; Check that the next token is a '{'. This works as
;; long as no language that allows nested function
;; definitions allows stuff like member init lists, K&R
;; declarations or throws clauses there.
;;
;; Note that we do a forward search for something ahead
;; of the indentation line here. That's not good since
;; the user might not have typed it yet. Unfortunately
;; it's exceedingly tricky to recognize a function
;; prototype in a code block without resorting to this.
(c-forward-syntactic-ws)
(eq (char-after) ?{))
(not (c-at-statement-start-p))
(eq (c-beginning-of-statement-1 containing-sexp nil nil t)
'same)
(save-excursion
(let ((c-recognize-typeless-decls nil))
;; Turn off recognition of constructs that lacks a
;; type in this case, since that's more likely to be
;; a macro followed by a block.
(c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
(c-add-stmt-syntax 'func-decl-cont nil t
containing-sexp paren-state))
;;CASE F: continued statement and the only preceding items are
;;annotations.
((and (c-major-mode-is 'java-mode)
(setq placeholder (point))
(c-beginning-of-statement-1)
(progn
(while (and (c-forward-annotation)
(< (point) placeholder))
(c-forward-syntactic-ws))
t)
(prog1
(>= (point) placeholder)
(goto-char placeholder)))
(c-beginning-of-statement-1 containing-sexp)
(c-add-syntax 'annotation-var-cont (point)))
;; CASE G: a template list continuation?
;; Mostly a duplication of case 5D.3 to fix templates-19:
((and (c-major-mode-is 'c++-mode)
(save-excursion
(goto-char indent-point)
(setq placeholder (c-up-list-backward))
(and placeholder
(eq (char-after placeholder) ?<)
(/= (char-before placeholder) ?<)
(progn
(goto-char (1+ placeholder))
(or (not (looking-at c-<-op-cont-regexp))
(looking-at c-<-pseudo-digraph-cont-regexp))))))
(goto-char placeholder)
(c-beginning-of-statement-1 containing-sexp t)
(if (save-excursion
(c-backward-syntactic-ws containing-sexp)
(eq (char-before) ?<))
;; In a nested template arglist.
(progn
(goto-char placeholder)
(c-syntactic-skip-backward "^,;" containing-sexp t)
(c-forward-syntactic-ws))
(back-to-indentation))
;; FIXME: Should use c-add-stmt-syntax, but it's not yet
;; template aware.
(c-add-syntax 'template-args-cont (point) placeholder))
;; CASE D: continued statement.
(t
(c-beginning-of-statement-1 containing-sexp)
(c-add-stmt-syntax 'statement-cont nil nil
containing-sexp paren-state))
)))
;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
;; 2005/11/29).
;;;###autoload
(defun c-guess-basic-syntax ()
"Return the syntactic context of the current line."
(save-excursion
(beginning-of-line)
(c-save-buffer-state
((indent-point (point))
(case-fold-search nil)
;; A whole ugly bunch of various temporary variables. Have
;; to declare them here since it's not possible to declare
;; a variable with only the scope of a cond test and the
;; following result clauses, and most of this function is a
;; single gigantic cond. :P
literal char-before-ip before-ws-ip char-after-ip macro-start
in-macro-expr c-syntactic-context placeholder
step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
containing-<
;; The following record some positions for the containing
;; declaration block if we're directly within one:
;; `containing-decl-open' is the position of the open
;; brace. `containing-decl-start' is the start of the
;; declaration. `containing-decl-kwd' is the keyword
;; symbol of the keyword that tells what kind of block it
;; is.
containing-decl-open
containing-decl-start
containing-decl-kwd
;; The open paren of the closest surrounding sexp or nil if
;; there is none.
containing-sexp
;; The position after the closest preceding brace sexp
;; (nested sexps are ignored), or the position after
;; `containing-sexp' if there is none, or (point-min) if
;; `containing-sexp' is nil.
lim
;; The paren state outside `containing-sexp', or at
;; `indent-point' if `containing-sexp' is nil.
(paren-state (c-parse-state))
(state-cache (copy-tree paren-state))
;; There's always at most one syntactic element which got
;; an anchor pos. It's stored in syntactic-relpos.
syntactic-relpos
(c-commas-bound-stmts c-commas-bound-stmts))
;; Check if we're directly inside an enclosing declaration
;; level block.
(when (and (setq containing-sexp
(c-most-enclosing-brace paren-state))
(progn
(goto-char containing-sexp)
(eq (char-after) ?{))
(setq placeholder
(c-looking-at-decl-block t)))
(setq containing-decl-open containing-sexp
containing-decl-start (point)
containing-sexp nil)
(goto-char placeholder)
(setq containing-decl-kwd (and (looking-at c-keywords-regexp)
(c-keyword-sym (match-string 1)))))
;; Init some position variables.
(if paren-state
(progn
(setq containing-sexp (car paren-state)
paren-state (cdr paren-state))
(if (consp containing-sexp)
(save-excursion
(goto-char (cdr containing-sexp))
(if (and (c-major-mode-is 'c++-mode)
(c-back-over-member-initializer-braces))
(c-syntactic-skip-backward "^}" nil t))
(setq lim (point))
(if paren-state
;; Ignore balanced paren. The next entry
;; can't be another one.
(setq containing-sexp (car paren-state)
paren-state (cdr paren-state))
;; If there is no surrounding open paren then
;; put the last balanced pair back on paren-state.
(setq paren-state (cons containing-sexp paren-state)
containing-sexp nil)))
(setq lim (1+ containing-sexp))))
(setq lim (point-min)))
;; If we're in a parenthesis list then ',' delimits the
;; "statements" rather than being an operator (with the
;; exception of the "for" clause). This difference is
;; typically only noticeable when statements are used in macro
;; arglists.
(when (and containing-sexp
(eq (char-after containing-sexp) ?\())
(setq c-commas-bound-stmts t))
;; cache char before and after indent point, and move point to
;; the most likely position to perform the majority of tests
(goto-char indent-point)
(c-backward-syntactic-ws lim)
(setq before-ws-ip (point)
char-before-ip (char-before))
(goto-char indent-point)
(skip-chars-forward " \t")
(setq char-after-ip (char-after))
;; are we in a literal?
(setq literal (c-in-literal lim))
;; now figure out syntactic qualities of the current line
(cond
;; CASE 1: in a string.
((eq literal 'string)
(c-add-syntax 'string (c-point 'bopl)))
;; CASE 2: in a C or C++ style comment.
((and (memq literal '(c c++))
;; This is a kludge for XEmacs where we use
;; `buffer-syntactic-context', which doesn't correctly
;; recognize "\*/" to end a block comment.
;; `parse-partial-sexp' which is used by
;; `c-literal-limits' will however do that in most
;; versions, which results in that we get nil from
;; `c-literal-limits' even when `c-in-literal' claims
;; we're inside a comment.
(setq placeholder (c-literal-start lim)))
(c-add-syntax literal placeholder))
;; CASE 3: in a cpp preprocessor macro continuation.
((and (save-excursion
(when (c-beginning-of-macro)
(setq macro-start (point))))
(/= macro-start (c-point 'boi))
(progn
(setq tmpsymbol 'cpp-macro-cont)
(or (not c-syntactic-indentation-in-macros)
(save-excursion
(goto-char macro-start)
;; If at the beginning of the body of a #define
;; directive then analyze as cpp-define-intro
;; only. Go on with the syntactic analysis
;; otherwise. in-macro-expr is set if we're in a
;; cpp expression, i.e. before the #define body
;; or anywhere in a non-#define directive.
(if (c-forward-to-cpp-define-body)
(let ((indent-boi (c-point 'boi indent-point)))
(setq in-macro-expr (> (point) indent-boi)
tmpsymbol 'cpp-define-intro)
(= (point) indent-boi))
(setq in-macro-expr t)
nil)))))
(c-add-syntax tmpsymbol macro-start)
(setq macro-start nil))
;; CASE 11: an else clause?
((looking-at "else\\>[^_]")
(c-beginning-of-statement-1 containing-sexp)
(c-add-stmt-syntax 'else-clause nil t
containing-sexp paren-state))
;; CASE 12: while closure of a do/while construct?
((and (looking-at "while\\>[^_]")
(save-excursion
(prog1 (eq (c-beginning-of-statement-1 containing-sexp)
'beginning)
(setq placeholder (point)))))
(goto-char placeholder)
(c-add-stmt-syntax 'do-while-closure nil t
containing-sexp paren-state))
;; CASE 13: A catch or finally clause? This case is simpler
;; than if-else and do-while, because a block is required
;; after every try, catch and finally.
((save-excursion
(and (cond ((c-major-mode-is 'c++-mode)
(looking-at "catch\\>[^_]"))
((c-major-mode-is 'java-mode)
(looking-at "\\(catch\\|finally\\)\\>[^_]")))
(and (c-safe (c-backward-syntactic-ws)
(c-backward-sexp)
t)
(eq (char-after) ?{)
(c-safe (c-backward-syntactic-ws)
(c-backward-sexp)
t)
(if (eq (char-after) ?\()
(c-safe (c-backward-sexp) t)
t))
(looking-at "\\(try\\|catch\\)\\>[^_]")
(setq placeholder (point))))
(goto-char placeholder)
(c-add-stmt-syntax 'catch-clause nil t
containing-sexp paren-state))
;; CASE 18: A substatement we can recognize by keyword.
((save-excursion
(and c-opt-block-stmt-key
(not (eq char-before-ip ?\;))
(not (c-at-vsemi-p before-ws-ip))
(not (memq char-after-ip '(?\) ?\] ?,)))
(or (not (eq char-before-ip ?}))
(c-looking-at-inexpr-block-backward state-cache))
(> (point)
(progn
;; Ought to cache the result from the
;; c-beginning-of-statement-1 calls here.
(setq placeholder (point))
(while (eq (setq step-type
(c-beginning-of-statement-1 lim))
'label))
(if (eq step-type 'previous)
(goto-char placeholder)
(setq placeholder (point))
(if (and (eq step-type 'same)
(not (looking-at c-opt-block-stmt-key)))
;; Step up to the containing statement if we
;; stayed in the same one.
(let (step)
(while (eq
(setq step
(c-beginning-of-statement-1 lim))
'label))
(if (eq step 'up)
(setq placeholder (point))
;; There was no containing statement after all.
(goto-char placeholder)))))
placeholder))
(if (looking-at c-block-stmt-2-key)
;; Require a parenthesis after these keywords.
;; Necessary to catch e.g. synchronized in Java,
;; which can be used both as statement and
;; modifier.
(and (zerop (c-forward-token-2 1 nil))
(eq (char-after) ?\())
(looking-at c-opt-block-stmt-key))))
(if (eq step-type 'up)
;; CASE 18A: Simple substatement.
(progn
(goto-char placeholder)
(cond
((eq char-after-ip ?{)
(c-add-stmt-syntax 'substatement-open nil nil
containing-sexp paren-state))
((save-excursion
(goto-char indent-point)
(back-to-indentation)
(c-forward-label))
(c-add-stmt-syntax 'substatement-label nil nil
containing-sexp paren-state))
(t
(c-add-stmt-syntax 'substatement nil nil
containing-sexp paren-state))))
;; CASE 18B: Some other substatement. This is shared
;; with case 10.
(c-guess-continued-construct indent-point
char-after-ip
placeholder
lim
paren-state)))
;; CASE 14: A case or default label
((save-excursion
(and (looking-at c-label-kwds-regexp)
(or (c-major-mode-is 'idl-mode)
(and
containing-sexp
(goto-char containing-sexp)
(eq (char-after) ?{)
(progn (c-backward-syntactic-ws) t)
(eq (char-before) ?\))
(c-go-list-backward)
(progn (c-backward-syntactic-ws) t)
(c-simple-skip-symbol-backward)
(looking-at c-block-stmt-2-key)))))
(if containing-sexp
(progn
(goto-char containing-sexp)
(setq lim (c-most-enclosing-brace state-cache
containing-sexp))
(c-backward-to-block-anchor lim)
(c-add-stmt-syntax 'case-label nil t lim paren-state))
;; Got a bogus label at the top level. In lack of better
;; alternatives, anchor it on (point-min).
(c-add-syntax 'case-label (point-min))))
;; CASE 15: any other label
((save-excursion
(back-to-indentation)
(and (not (looking-at c-syntactic-ws-start))
(not (looking-at c-label-kwds-regexp))
(c-forward-label)))
(cond (containing-decl-open
(setq placeholder (c-add-class-syntax 'inclass
containing-decl-open
containing-decl-start
containing-decl-kwd))
;; Append access-label with the same anchor point as
;; inclass gets.
(c-append-syntax 'access-label placeholder))
(containing-sexp
(goto-char containing-sexp)
(setq lim (c-most-enclosing-brace state-cache
containing-sexp))
(save-excursion
(setq tmpsymbol
(if (and (eq (c-beginning-of-statement-1 lim) 'up)
(looking-at "switch\\>[^_]"))
;; If the surrounding statement is a switch then
;; let's analyze all labels as switch labels, so
;; that they get lined up consistently.
'case-label
'label)))
(c-backward-to-block-anchor lim)
(c-add-stmt-syntax tmpsymbol nil t lim paren-state))
(t
;; A label on the top level. Treat it as a class
;; context. (point-min) is the closest we get to the
;; class open brace.
(c-add-syntax 'access-label (point-min)))))
;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
;; 17E.
((setq placeholder (c-looking-at-inexpr-block
(c-safe-position containing-sexp paren-state)
containing-sexp
;; Have to turn on the heuristics after
;; the point even though it doesn't work
;; very well. C.f. test case class-16.pike.
t))
(setq tmpsymbol (assq (car placeholder)
'((inexpr-class . class-open)
(inexpr-statement . block-open))))
(if tmpsymbol
;; It's a statement block or an anonymous class.
(setq tmpsymbol (cdr tmpsymbol))
;; It's a Pike lambda. Check whether we are between the
;; lambda keyword and the argument list or at the defun
;; opener.
(setq tmpsymbol (if (eq char-after-ip ?{)
'inline-open
'lambda-intro-cont)))
(goto-char (cdr placeholder))
(back-to-indentation)
(c-add-stmt-syntax tmpsymbol nil t
(c-most-enclosing-brace state-cache (point))
paren-state)
(unless (eq (point) (cdr placeholder))
(c-add-syntax (car placeholder))))
;; CASE 5: Line is inside a declaration level block or at top level.
((or containing-decl-open (null containing-sexp))
(cond
;; CASE 5A: we are looking at a defun, brace list, class,
;; or inline-inclass method opening brace
((setq special-brace-list
(or (and c-special-brace-lists
(c-looking-at-special-brace-list))
(eq char-after-ip ?{)))
(cond
;; CASE 5A.1: Non-class declaration block open.
((save-excursion
(let (tmp)
(and (eq char-after-ip ?{)
(setq tmp (c-looking-at-decl-block t))
(progn
(setq placeholder (point))
(goto-char tmp)
(looking-at c-symbol-key))
(c-keyword-member
(c-keyword-sym (setq keyword (match-string 0)))
'c-other-block-decl-kwds))))
(goto-char placeholder)
(c-add-stmt-syntax
(if (string-equal keyword "extern")
;; Special case for extern-lang-open.
'extern-lang-open
(intern (concat keyword "-open")))
nil t containing-sexp paren-state))
;; CASE 5A.2: we are looking at a class opening brace
((save-excursion
(goto-char indent-point)
(skip-chars-forward " \t")
(and (eq (char-after) ?{)
(c-looking-at-decl-block t)
(setq placeholder (point))))
(c-add-syntax 'class-open placeholder))
;; CASE 5A.3: brace list open
((save-excursion
(goto-char indent-point)
(skip-chars-forward " \t")
(cond
((c-backward-over-enum-header)
(setq placeholder (c-point 'boi)))
((consp (setq placeholder
(c-looking-at-or-maybe-in-bracelist
containing-sexp lim)))
(setq tmpsymbol (and (cdr placeholder) 'topmost-intro-cont))
(setq placeholder (c-point 'boi (car placeholder))))))
(if (and (not c-auto-newline-analysis)
;(c-major-mode-is 'java-mode) ; Not needed anymore (2016-08-30).
(eq tmpsymbol 'topmost-intro-cont))
;; We're in Java and have found that the open brace
;; belongs to a "new Foo[]" initialization list,
;; which means the brace list is part of an
;; expression and not a top level definition. We
;; therefore treat it as any topmost continuation
;; even though the semantically correct symbol still
;; is brace-list-open, on the same grounds as in
;; case B.2.
(progn
(c-beginning-of-statement-1 lim)
(c-add-syntax 'topmost-intro-cont (c-point 'boi)))
(c-add-syntax 'brace-list-open placeholder)))
;; CASE 5A.4: inline defun open
((and containing-decl-open
(not (c-keyword-member containing-decl-kwd
'c-other-block-decl-kwds)))
(c-add-syntax 'inline-open)
(c-add-class-syntax 'inclass
containing-decl-open
containing-decl-start
containing-decl-kwd))
;; CASE 5A.5: ordinary defun open
(t
(save-excursion
(c-beginning-of-decl-1 lim)
(while (cond
((looking-at c-specifier-key)
(c-forward-keyword-clause 1))
((and c-opt-cpp-prefix
(looking-at c-noise-macro-with-parens-name-re))
(c-forward-noise-clause))))
(c-add-syntax 'defun-open (c-point 'boi))
;; Bogus to use bol here, but it's the legacy. (Resolved,
;; 2007-11-09)
))))
;; CASE 5R: Member init list. (Used to be part of CASE 5B.1)
;; Note there is no limit on the backward search here, since member
;; init lists can, in practice, be very large.
((save-excursion
(when (and (c-major-mode-is 'c++-mode)
(setq placeholder (c-back-over-member-initializers)))
(setq tmp-pos (point))))
(if (= (c-point 'bosws) (1+ tmp-pos))
(progn
;; There is no preceding member init clause.
;; Indent relative to the beginning of indentation
;; for the topmost-intro line that contains the
;; prototype's open paren.
(goto-char placeholder)
(c-add-syntax 'member-init-intro (c-point 'boi)))
;; Indent relative to the first member init clause.
(goto-char (1+ tmp-pos))
(c-forward-syntactic-ws)
(c-add-syntax 'member-init-cont (point))))
;; CASE 5B: After a function header but before the body (or
;; the ending semicolon if there's no body).
((save-excursion
(when (setq placeholder (c-just-after-func-arglist-p
(max lim (c-determine-limit 500))))
(setq tmp-pos (point))))
(cond
;; CASE 5B.1: Member init list.
((eq (char-after tmp-pos) ?:)
;; There is no preceding member init clause.
;; Indent relative to the beginning of indentation
;; for the topmost-intro line that contains the
;; prototype's open paren.
(goto-char placeholder)
(c-add-syntax 'member-init-intro (c-point 'boi)))
;; CASE 5B.2: K&R arg decl intro
((and c-recognize-knr-p
(c-in-knr-argdecl lim))
(c-beginning-of-statement-1 lim)
(c-add-syntax 'knr-argdecl-intro (c-point 'boi))
(if containing-decl-open
(c-add-class-syntax 'inclass
containing-decl-open
containing-decl-start
containing-decl-kwd)))
;; CASE 5B.4: Nether region after a C++ or Java func
;; decl, which could include a `throws' declaration.
(t
(c-beginning-of-statement-1 lim)
(c-add-syntax 'func-decl-cont (c-point 'boi))
)))
;; CASE 5C: inheritance line. could be first inheritance
;; line, or continuation of a multiple inheritance
((or (and (c-major-mode-is 'c++-mode)
(progn
(when (eq char-after-ip ?,)
(skip-chars-forward " \t")
(forward-char))
(looking-at c-opt-postfix-decl-spec-key)))
(and (or (eq char-before-ip ?:)
;; watch out for scope operator
(save-excursion
(and (eq char-after-ip ?:)
(c-safe (forward-char 1) t)
(not (eq (char-after) ?:))
)))
(save-excursion
(c-beginning-of-statement-1 lim)
(when (looking-at c-opt-<>-sexp-key)
(goto-char (match-end 1))
(c-forward-syntactic-ws)
(c-forward-<>-arglist nil)
(c-forward-syntactic-ws))
(looking-at c-class-key)))
;; for Java
(and (c-major-mode-is 'java-mode)
(let ((fence (save-excursion
(c-beginning-of-statement-1 lim)
(point)))
cont done)
(save-excursion
(while (not done)
(cond ((looking-at c-opt-postfix-decl-spec-key)
(setq injava-inher (cons cont (point))
done t))
((or (not (c-safe (c-forward-sexp -1) t))
(<= (point) fence))
(setq done t))
)
(setq cont t)))
injava-inher)
(not (c-crosses-statement-barrier-p (cdr injava-inher)
(point)))
))
(cond
;; CASE 5C.1: non-hanging colon on an inher intro
((eq char-after-ip ?:)
(c-beginning-of-statement-1 lim)
(c-add-syntax 'inher-intro (c-point 'boi))
;; don't add inclass symbol since relative point already
;; contains any class offset
)
;; CASE 5C.2: hanging colon on an inher intro
((eq char-before-ip ?:)
(c-beginning-of-statement-1 lim)
(c-add-syntax 'inher-intro (c-point 'boi))
(if containing-decl-open
(c-add-class-syntax 'inclass
containing-decl-open
containing-decl-start
containing-decl-kwd)))
;; CASE 5C.3: in a Java implements/extends
(injava-inher
(let ((where (cdr injava-inher))
(cont (car injava-inher)))
(goto-char where)
(cond ((looking-at "throws\\>[^_]")
(c-add-syntax 'func-decl-cont
(progn (c-beginning-of-statement-1 lim)
(c-point 'boi))))
(cont (c-add-syntax 'inher-cont where))
(t (c-add-syntax 'inher-intro
(progn (goto-char (cdr injava-inher))
(c-beginning-of-statement-1 lim)
(point))))
)))
;; CASE 5C.4: a continued inheritance line
(t
(c-beginning-of-inheritance-list lim)
(c-add-syntax 'inher-cont (point))
;; don't add inclass symbol since relative point already
;; contains any class offset
)))
;; CASE 5P: AWK pattern or function or continuation
;; thereof.
((c-major-mode-is 'awk-mode)
(setq placeholder (point))
(c-add-stmt-syntax
(if (and (eq (c-beginning-of-statement-1) 'same)
(/= (point) placeholder))
'topmost-intro-cont
'topmost-intro)
nil nil
containing-sexp paren-state))
;; CASE 5D: this could be a top-level initialization, a
;; member init list continuation, or a template argument
;; list continuation.
((save-excursion
;; Note: We use the fact that lim is always after any
;; preceding brace sexp.
(if c-recognize-<>-arglists
(while (and
(progn
(c-syntactic-skip-backward "^;,=<>" lim t)
(> (point) lim))
(or
(when c-overloadable-operators-regexp
(when (setq placeholder (c-after-special-operator-id lim))
(goto-char placeholder)
t))
(cond
((eq (char-before) ?>)
(or (c-backward-<>-arglist nil lim)
(backward-char))
t)
((eq (char-before) ?<)
(backward-char)
(if (save-excursion
(c-forward-<>-arglist nil))
(progn (forward-char)
nil)
t))
(t nil)))))
;; NB: No c-after-special-operator-id stuff in this
;; clause - we assume only C++ needs it.
(c-syntactic-skip-backward "^;,=" lim t))
(setq placeholder (point))
(memq (char-before) '(?, ?= ?<)))
(cond
;; CASE 5D.6: Something like C++11's "using foo = <type-exp>"
((save-excursion
(and (eq (char-before placeholder) ?=)
(goto-char placeholder)
(eq (c-backward-token-2 1 nil lim) 0)
(eq (point) (1- placeholder))
(eq (c-beginning-of-statement-1 lim) 'same)
(looking-at c-equals-type-clause-key)
(let ((preserve-point (point)))
(when
(and
(eq (c-forward-token-2 1 nil nil) 0)
(c-on-identifier))
(setq placeholder preserve-point)))))
(c-add-syntax
'statement-cont placeholder)
)
;; CASE 5D.3: perhaps a template list continuation?
((and (c-major-mode-is 'c++-mode)
(save-excursion
(save-restriction
(goto-char indent-point)
(setq placeholder (c-up-list-backward))
(and placeholder
(eq (char-after placeholder) ?<)))))
(goto-char placeholder)
(c-beginning-of-statement-1 lim t)
(if (save-excursion
(c-backward-syntactic-ws lim)
(eq (char-before) ?<))
;; In a nested template arglist.
(progn
(goto-char placeholder)
(c-syntactic-skip-backward "^,;" lim t)
(c-forward-syntactic-ws))
(back-to-indentation))
;; FIXME: Should use c-add-stmt-syntax, but it's not yet
;; template aware.
(c-add-syntax 'template-args-cont (point) placeholder))
;; CASE 5D.4: perhaps a multiple inheritance line?
((and (c-major-mode-is 'c++-mode)
(save-excursion
(c-beginning-of-statement-1 lim)
(setq placeholder (point))
(if (looking-at "static\\>[^_]")
(c-forward-token-2 1 nil indent-point))
(and (looking-at c-class-key)
(zerop (c-forward-token-2 2 nil indent-point))
(if (eq (char-after) ?<)
(zerop (c-forward-token-2 1 t indent-point))
t)
(progn
(while
(and
(< (point) indent-point)
(looking-at c-class-id-suffix-ws-ids-key)
(zerop (c-forward-token-2 1 nil indent-point))))
t)
(eq (char-after) ?:))))
(goto-char placeholder)
(c-add-syntax 'inher-cont (c-point 'boi)))
;; CASE 5D.5: Continuation of the "expression part" of a
;; top level construct. Or, perhaps, an unrecognized construct.
(t
(while (and (setq placeholder (point))
(eq (car (c-beginning-of-decl-1 containing-sexp)) ; Can't use `lim' here.
'same)
(save-excursion
(c-backward-syntactic-ws)
(eq (char-before) ?}))
(< (point) placeholder)))
(c-add-stmt-syntax
(cond
((eq (point) placeholder) 'statement) ; unrecognized construct
;; A preceding comma at the top level means that a
;; new variable declaration starts here. Use
;; topmost-intro-cont for it, for consistency with
;; the first variable declaration. C.f. case 5N.
((eq char-before-ip ?,) 'topmost-intro-cont)
(t 'statement-cont))
nil nil containing-sexp paren-state))
))
;; CASE 5F: Close of a non-class declaration level block.
((and (eq char-after-ip ?})
(c-keyword-member containing-decl-kwd
'c-other-block-decl-kwds))
;; This is inconsistent: Should use `containing-decl-open'
;; here if it's at boi, like in case 5J.
(goto-char containing-decl-start)
(c-add-stmt-syntax
(if (string-equal (symbol-name containing-decl-kwd) "extern")
;; Special case for compatibility with the
;; extern-lang syntactic symbols.
'extern-lang-close
(intern (concat (symbol-name containing-decl-kwd)
"-close")))
nil t
(c-most-enclosing-brace paren-state (point))
paren-state))
;; CASE 5G: we are looking at the brace which closes the
;; enclosing nested class decl
((and containing-sexp
(eq char-after-ip ?})
(eq containing-decl-open containing-sexp))
(c-add-class-syntax 'class-close
containing-decl-open
containing-decl-start
containing-decl-kwd))
;; CASE 5H: we could be looking at subsequent knr-argdecls
((and c-recognize-knr-p
(not containing-sexp) ; can't be knr inside braces.
(not (eq char-before-ip ?}))
(save-excursion
(setq placeholder (cdr (c-beginning-of-decl-1 lim)))
(and placeholder
;; Do an extra check to avoid tripping up on
;; statements that occur in invalid contexts
;; (e.g. in macro bodies where we don't really
;; know the context of what we're looking at).
(not (and c-opt-block-stmt-key
(looking-at c-opt-block-stmt-key)))))
(< placeholder indent-point))
(goto-char placeholder)
(c-add-syntax 'knr-argdecl (point)))
;; CASE 5I: ObjC method definition.
((and c-opt-method-key
(looking-at c-opt-method-key))
(c-beginning-of-statement-1 nil t)
(if (= (point) indent-point)
;; Handle the case when it's the first (non-comment)
;; thing in the buffer. Can't look for a 'same return
;; value from cbos1 since ObjC directives currently
;; aren't recognized fully, so that we get 'same
;; instead of 'previous if it moved over a preceding
;; directive.
(goto-char (point-min)))
(c-add-syntax 'objc-method-intro (c-point 'boi)))
;; CASE 5N: At a variable declaration that follows a class
;; definition or some other block declaration that doesn't
;; end at the closing '}'. C.f. case 5D.5.
((progn
(c-backward-syntactic-ws lim)
(and (eq (char-before) ?})
(save-excursion
(let ((start (point)))
(if (and state-cache
(consp (car state-cache))
(eq (cdar state-cache) (point)))
;; Speed up the backward search a bit.
(goto-char (caar state-cache)))
(c-beginning-of-decl-1 containing-sexp) ; Can't use `lim' here.
(setq placeholder (point))
(if (= start (point))
;; The '}' is unbalanced.
nil
(c-end-of-decl-1)
(>= (point) indent-point))))
;; Check that we only have one brace block here, i.e. that we
;; don't have something like a function with a struct
;; declaration as its type.
(save-excursion
(or (not (and state-cache (consp (car state-cache))))
;; The above probably can't happen.
(progn
(goto-char placeholder)
(and (c-syntactic-re-search-forward
"{" indent-point t)
(eq (1- (point)) (caar state-cache))))))))
(goto-char placeholder)
(c-add-stmt-syntax 'topmost-intro-cont nil nil
containing-sexp paren-state))
;; NOTE: The point is at the end of the previous token here.
;; CASE 5J: we are at the topmost level, make
;; sure we skip back past any access specifiers
((and
;; A macro continuation line is never at top level.
(not (and macro-start
(> indent-point macro-start)))
(save-excursion
(setq placeholder (point))
(or (memq char-before-ip '(?\; ?{ ?} nil))
(c-at-vsemi-p before-ws-ip)
(when (and (eq char-before-ip ?:)
(eq (c-beginning-of-statement-1 lim)
'label))
(c-backward-syntactic-ws lim)
(setq placeholder (point)))
(and (c-major-mode-is 'objc-mode)
(catch 'not-in-directive
(c-beginning-of-statement-1 lim)
(setq placeholder (point))
(while (and (c-forward-objc-directive)
(< (point) indent-point))
(c-forward-syntactic-ws)
(if (>= (point) indent-point)
(throw 'not-in-directive t))
(setq placeholder (point)))
nil)))))
;; For historic reasons we anchor at bol of the last
;; line of the previous declaration. That's clearly
;; highly bogus and useless, and it makes our lives hard
;; to remain compatible. :P
(goto-char placeholder)
(c-add-syntax 'topmost-intro (c-point 'bol))
(if containing-decl-open
(if (c-keyword-member containing-decl-kwd
'c-other-block-decl-kwds)
(progn
(goto-char (c-brace-anchor-point containing-decl-open))
(c-add-stmt-syntax
(if (string-equal (symbol-name containing-decl-kwd)
"extern")
;; Special case for compatibility with the
;; extern-lang syntactic symbols.
'inextern-lang
(intern (concat "in"
(symbol-name containing-decl-kwd))))
nil t
(c-most-enclosing-brace paren-state (point))
paren-state))
(c-add-class-syntax 'inclass
containing-decl-open
containing-decl-start
containing-decl-kwd)))
(when (and c-syntactic-indentation-in-macros
macro-start
(/= macro-start (c-point 'boi indent-point)))
(c-add-syntax 'cpp-define-intro)
(setq macro-start nil)))
;; CASE 5K: we are at an ObjC method definition
;; continuation line.
((and c-opt-method-key
(save-excursion
(c-beginning-of-statement-1 lim)
(beginning-of-line)
(when (looking-at c-opt-method-key)
(setq placeholder (point)))))
(c-add-syntax 'objc-method-args-cont placeholder))
;; CASE 5L: we are at the first argument of a template
;; arglist that begins on the previous line.
((and c-recognize-<>-arglists
(eq (char-before) ?<)
(not (and c-overloadable-operators-regexp
(c-after-special-operator-id lim))))
(c-beginning-of-statement-1 (c-safe-position (point) paren-state))
(c-add-syntax 'template-args-cont (c-point 'boi)))
;; CASE 5Q: we are at a statement within a macro.
(macro-start
(c-beginning-of-statement-1 containing-sexp)
(c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
;;CASE 5N: We are at a topmost continuation line and the only
;;preceding items are annotations.
((and (c-major-mode-is 'java-mode)
(setq placeholder (point))
(c-beginning-of-statement-1)
(progn
(while (and (c-forward-annotation))
(c-forward-syntactic-ws))
t)
(prog1
(>= (point) placeholder)
(goto-char placeholder)))
(c-add-syntax 'annotation-top-cont (c-point 'boi)))
;; CASE 5M: we are at a topmost continuation line
(t
(c-beginning-of-statement-1 (c-safe-position (point) paren-state))
(when (c-major-mode-is 'objc-mode)
(setq placeholder (point))
(while (and (c-forward-objc-directive)
(< (point) indent-point))
(c-forward-syntactic-ws)
(setq placeholder (point)))
(goto-char placeholder))
(c-add-syntax 'topmost-intro-cont (c-point 'boi)))
))
;; (CASE 6 has been removed.)
;; CASE 7: line is an expression, not a statement. Most
;; likely we are either in a function prototype or a function
;; call argument list
((not (or (and c-special-brace-lists
(save-excursion
(goto-char containing-sexp)
(c-looking-at-special-brace-list)))
(eq (char-after containing-sexp) ?{)))
(cond
;; CASE 7A: we are looking at the arglist closing paren.
;; C.f. case 7F.
((memq char-after-ip '(?\) ?\]))
(goto-char containing-sexp)
(setq placeholder (c-point 'boi))
(if (and (c-safe (backward-up-list 1) t)
(>= (point) placeholder))
(progn
(forward-char)
(skip-chars-forward " \t"))
(goto-char placeholder))
(c-add-stmt-syntax 'arglist-close (list containing-sexp) t
(c-most-enclosing-brace paren-state (point))
paren-state))
;; CASE 7B: Looking at the opening brace of an
;; in-expression block or brace list. C.f. cases 4, 16A
;; and 17E.
((and (eq char-after-ip ?{)
(or (not (eq (char-after containing-sexp) ?\())
(save-excursion
(and c-opt-inexpr-brace-list-key
(eq (c-beginning-of-statement-1 lim t nil t) 'same)
(looking-at c-opt-inexpr-brace-list-key))))
(progn
(setq placeholder (c-inside-bracelist-p (point)
paren-state
nil))
(if placeholder
(setq tmpsymbol '(brace-list-open . inexpr-class))
(setq tmpsymbol '(block-open . inexpr-statement)
placeholder
(cdr-safe (c-looking-at-inexpr-block
(c-safe-position containing-sexp
paren-state)
containing-sexp)))
;; placeholder is nil if it's a block directly in
;; a function arglist. That makes us skip out of
;; this case.
)))
(goto-char placeholder)
(back-to-indentation)
(c-add-stmt-syntax (car tmpsymbol) nil t
(c-most-enclosing-brace paren-state (point))
paren-state)
(if (/= (point) placeholder)
(c-add-syntax (cdr tmpsymbol))))
;; CASE 7C: we are looking at the first argument in an empty
;; argument list. Use arglist-close if we're actually
;; looking at a close paren or bracket.
((memq char-before-ip '(?\( ?\[))
(goto-char containing-sexp)
(setq placeholder (c-point 'boi))
(if (and (c-safe (backward-up-list 1) t)
(>= (point) placeholder))
(progn
(forward-char)
(skip-chars-forward " \t"))
(goto-char placeholder))
(c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
(c-most-enclosing-brace paren-state (point))
paren-state))
;; CASE 7D: we are inside a conditional test clause. treat
;; these things as statements
((progn
(goto-char containing-sexp)
(and (c-safe (c-forward-sexp -1) t)
(looking-at "\\<for\\>[^_]")))
(goto-char (1+ containing-sexp))
(c-forward-syntactic-ws indent-point)
(if (eq char-before-ip ?\;)
(c-add-syntax 'statement (point))
(c-add-syntax 'statement-cont (point))
))
;; CASE 7E: maybe a continued ObjC method call. This is the
;; case when we are inside a [] bracketed exp, and what
;; precede the opening bracket is not an identifier.
((and c-opt-method-key
(eq (char-after containing-sexp) ?\[)
(progn
(goto-char (1- containing-sexp))
(c-backward-syntactic-ws (c-point 'bod))
(if (not (looking-at c-symbol-key))
(c-add-syntax 'objc-method-call-cont containing-sexp))
)))
;; CASE 7F: we are looking at an arglist continuation line,
;; but the preceding argument is on the same line as the
;; opening paren. This case includes multi-line
;; mathematical paren groupings, but we could be on a
;; for-list continuation line. C.f. case 7A.
((progn
(goto-char (1+ containing-sexp))
(< (save-excursion
(c-forward-syntactic-ws)
(point))
(c-point 'bonl)))
(goto-char containing-sexp) ; paren opening the arglist
(setq placeholder (c-point 'boi))
(if (and (c-safe (backward-up-list 1) t)
(>= (point) placeholder))
(progn
(forward-char)
(skip-chars-forward " \t"))
(goto-char placeholder))
(c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
(c-most-enclosing-brace state-cache (point))
paren-state))
;; CASE 7G: we are looking at just a normal arglist
;; continuation line
(t (c-forward-syntactic-ws indent-point)
(c-add-syntax 'arglist-cont (c-point 'boi)))
))
;; CASE 8: func-local multi-inheritance line
((and (c-major-mode-is 'c++-mode)
(save-excursion
(goto-char indent-point)
(skip-chars-forward " \t")
(looking-at c-opt-postfix-decl-spec-key)))
(goto-char indent-point)
(skip-chars-forward " \t")
(cond
;; CASE 8A: non-hanging colon on an inher intro
((eq char-after-ip ?:)
(c-backward-syntactic-ws lim)
(c-add-syntax 'inher-intro (c-point 'boi)))
;; CASE 8B: hanging colon on an inher intro
((eq char-before-ip ?:)
(c-add-syntax 'inher-intro (c-point 'boi)))
;; CASE 8C: a continued inheritance line
(t
(c-beginning-of-inheritance-list lim)
(c-add-syntax 'inher-cont (point))
)))
;; CASE 9: we are inside a brace-list
((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
(setq special-brace-list
(or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
(save-excursion
(goto-char containing-sexp)
(c-looking-at-special-brace-list)))
(c-inside-bracelist-p containing-sexp paren-state t))))
(cond
;; CASE 9A: In the middle of a special brace list opener.
((and (consp special-brace-list)
(save-excursion
(goto-char containing-sexp)
(eq (char-after) ?\())
(eq char-after-ip (car (cdr special-brace-list))))
(goto-char (car (car special-brace-list)))
(skip-chars-backward " \t")
(if (and (bolp)
(assoc 'statement-cont
(setq placeholder (c-guess-basic-syntax))))
(setq c-syntactic-context placeholder)
(c-beginning-of-statement-1
(c-safe-position (1- containing-sexp) paren-state))
(c-forward-token-2 0)
(while (cond
((looking-at c-specifier-key)
(c-forward-keyword-clause 1))
((and c-opt-cpp-prefix
(looking-at c-noise-macro-with-parens-name-re))
(c-forward-noise-clause))))
(c-add-syntax 'brace-list-open (c-point 'boi))))
;; CASE 9B: brace-list-close brace
((if (consp special-brace-list)
;; Check special brace list closer.
(progn
(goto-char (car (car special-brace-list)))
(save-excursion
(goto-char indent-point)
(back-to-indentation)
(or
;; We were between the special close char and the `)'.
(and (eq (char-after) ?\))
(eq (1+ (point)) (cdr (car special-brace-list))))
;; We were before the special close char.
(and (eq (char-after) (cdr (cdr special-brace-list)))
(zerop (c-forward-token-2))
(eq (1+ (point)) (cdr (car special-brace-list)))))))
;; Normal brace list check.
(and (eq char-after-ip ?})
(c-safe (goto-char (c-up-list-backward (point))) t)
(= (point) containing-sexp)))
(if (eq (point) (c-point 'boi))
(c-add-syntax 'brace-list-close (point))
(setq lim (or (save-excursion
(and
(c-back-over-member-initializers)
(point)))
(c-most-enclosing-brace state-cache (point))))
(c-beginning-of-statement-1 lim nil nil t)
(c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
(t
;; Prepare for the rest of the cases below by going back to the
;; previous entry, or BOI before that, providing that this is
;; inside the enclosing brace.
(goto-char indent-point)
(c-beginning-of-statement-1 containing-sexp nil nil t)
(when (/= (point) indent-point)
(if (> (c-point 'boi) containing-sexp)
(goto-char (c-point 'boi))
(if (consp special-brace-list)
(progn
(goto-char (caar special-brace-list))
(c-forward-token-2 1 nil indent-point))
(goto-char containing-sexp))
(forward-char)
(c-skip-ws-forward indent-point)))
(cond
;; CASE 9C: we're looking at the first line in a brace-list
((= (point) indent-point)
(if (consp special-brace-list)
(goto-char (car (car special-brace-list)))
(goto-char containing-sexp))
(if (eq (point) (c-point 'boi))
(c-add-syntax 'brace-list-intro (point))
(setq lim (or (save-excursion
(and
(c-back-over-member-initializers)
(point)))
(c-most-enclosing-brace state-cache (point))))
(c-beginning-of-statement-1 lim nil nil t)
(c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
;; CASE 9D: this is just a later brace-list-entry or
;; brace-entry-open
(t (if (or (eq char-after-ip ?{)
(and c-special-brace-lists
(save-excursion
(goto-char indent-point)
(c-forward-syntactic-ws (c-point 'eol))
(c-looking-at-special-brace-list))))
(c-add-syntax 'brace-entry-open (point))
(c-add-stmt-syntax 'brace-list-entry nil t containing-sexp
paren-state (point))
))
))))
;; CASE 10: A continued statement or top level construct.
((and (not (memq char-before-ip '(?\; ?:)))
(not (c-at-vsemi-p before-ws-ip))
(or (not (eq char-before-ip ?}))
(c-looking-at-inexpr-block-backward state-cache))
(> (point)
(save-excursion
(c-beginning-of-statement-1 containing-sexp)
(setq placeholder (point))))
(/= placeholder containing-sexp))
;; This is shared with case 18.
(c-guess-continued-construct indent-point
char-after-ip
placeholder
containing-sexp
paren-state))
;; CASE 16: block close brace, possibly closing the defun or
;; the class
((eq char-after-ip ?})
;; From here on we have the next containing sexp in lim.
(setq lim (c-most-enclosing-brace paren-state))
(goto-char containing-sexp)
(cond
;; CASE 16E: Closing a statement block? This catches
;; cases where it's preceded by a statement keyword,
;; which works even when used in an "invalid" context,
;; e.g. a macro argument.
((c-after-conditional)
(c-backward-to-block-anchor lim)
(c-add-stmt-syntax 'block-close nil t lim paren-state))
;; CASE 16A: closing a lambda defun or an in-expression
;; block? C.f. cases 4, 7B and 17E.
((setq placeholder (c-looking-at-inexpr-block
(c-safe-position containing-sexp paren-state)
nil))
(setq tmpsymbol (if (eq (car placeholder) 'inlambda)
'inline-close
'block-close))
(goto-char containing-sexp)
(back-to-indentation)
(if (= containing-sexp (point))
(c-add-syntax tmpsymbol (point))
(goto-char (cdr placeholder))
(back-to-indentation)
(c-add-stmt-syntax tmpsymbol nil t
(c-most-enclosing-brace paren-state (point))
paren-state)
(if (/= (point) (cdr placeholder))
(c-add-syntax (car placeholder)))))
;; CASE 16B: does this close an inline or a function in
;; a non-class declaration level block?
((save-excursion
(and lim
(progn
(goto-char lim)
(c-looking-at-decl-block nil))
(setq placeholder (point))))
(c-backward-to-decl-anchor lim)
(back-to-indentation)
(if (save-excursion
(goto-char placeholder)
(looking-at c-other-decl-block-key))
(c-add-syntax 'defun-close (point))
(c-add-syntax 'inline-close (point))))
;; CASE 16F: Can be a defun-close of a function declared
;; in a statement block, e.g. in Pike or when using gcc
;; extensions, but watch out for macros followed by
;; blocks. Let it through to be handled below.
;; C.f. cases B.3 and 17G.
((save-excursion
(and (not (c-at-statement-start-p))
(eq (c-beginning-of-statement-1 lim nil nil t) 'same)
(setq placeholder (point))
(let ((c-recognize-typeless-decls nil))
;; Turn off recognition of constructs that
;; lacks a type in this case, since that's more
;; likely to be a macro followed by a block.
(c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
(back-to-indentation)
(if (/= (point) containing-sexp)
(goto-char placeholder))
(c-add-stmt-syntax 'defun-close nil t lim paren-state))
;; CASE 16C: If there is an enclosing brace then this is
;; a block close since defun closes inside declaration
;; level blocks have been handled above.
(lim
;; If the block is preceded by a case/switch label on
;; the same line, we anchor at the first preceding label
;; at boi. The default handling in c-add-stmt-syntax
;; really fixes it better, but we do like this to keep
;; the indentation compatible with version 5.28 and
;; earlier. C.f. case 17H.
(while (and (/= (setq placeholder (point)) (c-point 'boi))
(eq (c-beginning-of-statement-1 lim) 'label)))
(goto-char placeholder)
(if (looking-at c-label-kwds-regexp)
(c-add-syntax 'block-close (point))
(goto-char containing-sexp)
;; c-backward-to-block-anchor not necessary here; those
;; situations are handled in case 16E above.
(c-add-stmt-syntax 'block-close nil t lim paren-state)))
;; CASE 16D: Only top level defun close left.
(t
(goto-char containing-sexp)
(c-backward-to-decl-anchor lim)
(c-add-stmt-syntax 'defun-close nil nil
(c-most-enclosing-brace paren-state)
paren-state))
))
;; CASE 19: line is an expression, not a statement, and is directly
;; contained by a template delimiter. Most likely, we are in a
;; template arglist within a statement. This case is based on CASE
;; 7. At some point in the future, we may wish to create more
;; syntactic symbols such as `template-intro',
;; `template-cont-nonempty', etc., and distinguish between them as we
;; do for `arglist-intro' etc. (2009-12-07).
((and c-recognize-<>-arglists
(setq containing-< (c-up-list-backward indent-point containing-sexp))
(eq (char-after containing-<) ?\<))
(setq placeholder (c-point 'boi containing-<))
(goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
; '<') before indent-point.
(if (>= (point) placeholder)
(progn
(forward-char)
(skip-chars-forward " \t"))
(goto-char placeholder))
(c-add-stmt-syntax 'template-args-cont (list containing-<) t
(c-most-enclosing-brace state-cache (point))
paren-state))
;; CASE 17: Statement or defun catchall.
(t
(goto-char indent-point)
;; Back up statements until we find one that starts at boi.
(while (let* ((prev-point (point))
(last-step-type (c-beginning-of-statement-1
containing-sexp)))
(if (= (point) prev-point)
(progn
(setq step-type (or step-type last-step-type))
nil)
(setq step-type last-step-type)
(/= (point) (c-point 'boi)))))
(cond
;; CASE 17B: continued statement
((and (eq step-type 'same)
(/= (point) indent-point))
(c-add-stmt-syntax 'statement-cont nil nil
containing-sexp paren-state))
;; CASE 17A: After a case/default label?
((progn
(while (and (eq step-type 'label)
(not (looking-at c-label-kwds-regexp)))
(setq step-type
(c-beginning-of-statement-1 containing-sexp)))
(eq step-type 'label))
(c-add-stmt-syntax (if (eq char-after-ip ?{)
'statement-case-open
'statement-case-intro)
nil t containing-sexp paren-state))
;; CASE 17D: any old statement
((progn
(while (eq step-type 'label)
(setq step-type
(c-beginning-of-statement-1 containing-sexp)))
(eq step-type 'previous))
(c-add-stmt-syntax 'statement nil t
containing-sexp paren-state)
(if (eq char-after-ip ?{)
(c-add-syntax 'block-open)))
;; CASE 17I: Inside a substatement block.
((progn
;; The following tests are all based on containing-sexp.
(goto-char containing-sexp)
;; From here on we have the next containing sexp in lim.
(setq lim (c-most-enclosing-brace paren-state containing-sexp))
(c-after-conditional))
(c-backward-to-block-anchor lim)
(c-add-stmt-syntax 'statement-block-intro nil t
lim paren-state)
(if (eq char-after-ip ?{)
(c-add-syntax 'block-open)))
;; CASE 17E: first statement in an in-expression block.
;; C.f. cases 4, 7B and 16A.
((setq placeholder (c-looking-at-inexpr-block
(c-safe-position containing-sexp paren-state)
nil))
(setq tmpsymbol (if (eq (car placeholder) 'inlambda)
'defun-block-intro
'statement-block-intro))
(back-to-indentation)
(if (= containing-sexp (point))
(c-add-syntax tmpsymbol (point))
(goto-char (cdr placeholder))
(back-to-indentation)
(c-add-stmt-syntax tmpsymbol nil t
(c-most-enclosing-brace state-cache (point))
paren-state)
(if (/= (point) (cdr placeholder))
(c-add-syntax (car placeholder))))
(if (eq char-after-ip ?{)
(c-add-syntax 'block-open)))
;; CASE 17F: first statement in an inline, or first
;; statement in a top-level defun. we can tell this is it
;; if there are no enclosing braces that haven't been
;; narrowed out by a class (i.e. don't use bod here).
((save-excursion
(or (not (setq placeholder (c-most-enclosing-brace
paren-state)))
(and (progn
(goto-char placeholder)
(eq (char-after) ?{))
(c-looking-at-decl-block nil))))
(c-backward-to-decl-anchor lim)
(back-to-indentation)
(c-add-syntax 'defun-block-intro (point)))
;; CASE 17G: First statement in a function declared inside
;; a normal block. This can occur in Pike and with
;; e.g. the gcc extensions, but watch out for macros
;; followed by blocks. C.f. cases B.3 and 16F.
((save-excursion
(and (not (c-at-statement-start-p))
(eq (c-beginning-of-statement-1 lim nil nil t) 'same)
(setq placeholder (point))
(let ((c-recognize-typeless-decls nil))
;; Turn off recognition of constructs that lacks
;; a type in this case, since that's more likely
;; to be a macro followed by a block.
(c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
(back-to-indentation)
(if (/= (point) containing-sexp)
(goto-char placeholder))
(c-add-stmt-syntax 'defun-block-intro nil t
lim paren-state))
;; CASE 17H: First statement in a block.
(t
;; If the block is preceded by a case/switch label on the
;; same line, we anchor at the first preceding label at
;; boi. The default handling in c-add-stmt-syntax is
;; really fixes it better, but we do like this to keep the
;; indentation compatible with version 5.28 and earlier.
;; C.f. case 16C.
(while (and (/= (setq placeholder (point)) (c-point 'boi))
(eq (c-beginning-of-statement-1 lim) 'label)))
(goto-char placeholder)
(if (looking-at c-label-kwds-regexp)
(c-add-syntax 'statement-block-intro (point))
(goto-char containing-sexp)
;; c-backward-to-block-anchor not necessary here; those
;; situations are handled in case 17I above.
(c-add-stmt-syntax 'statement-block-intro nil t
lim paren-state))
(if (eq char-after-ip ?{)
(c-add-syntax 'block-open)))
))
)
;; now we need to look at any modifiers
(goto-char indent-point)
(skip-chars-forward " \t")
;; are we looking at a comment only line?
(when (and (looking-at c-comment-start-regexp)
(/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
(c-append-syntax 'comment-intro))
;; we might want to give additional offset to friends (in C++).
(when (and c-opt-friend-key
(looking-at c-opt-friend-key))
(c-append-syntax 'friend))
;; Set syntactic-relpos.
(let ((p c-syntactic-context))
(while (and p
(if (integerp (c-langelem-pos (car p)))
(progn
(setq syntactic-relpos (c-langelem-pos (car p)))
nil)
t))
(setq p (cdr p))))
;; Start of or a continuation of a preprocessor directive?
(if (and macro-start
(eq macro-start (c-point 'boi))
(not (and (c-major-mode-is 'pike-mode)
(eq (char-after (1+ macro-start)) ?\"))))
(c-append-syntax 'cpp-macro)
(when (and c-syntactic-indentation-in-macros macro-start)
(if in-macro-expr
(when (or
(< syntactic-relpos macro-start)
(not (or
(assq 'arglist-intro c-syntactic-context)
(assq 'arglist-cont c-syntactic-context)
(assq 'arglist-cont-nonempty c-syntactic-context)
(assq 'arglist-close c-syntactic-context))))
;; If inside a cpp expression, i.e. anywhere in a
;; cpp directive except a #define body, we only let
;; through the syntactic analysis that is internal
;; in the expression. That means the arglist
;; elements, if they are anchored inside the cpp
;; expression.
(setq c-syntactic-context nil)
(c-add-syntax 'cpp-macro-cont macro-start))
(when (and (eq macro-start syntactic-relpos)
(not (assq 'cpp-define-intro c-syntactic-context))
(save-excursion
(goto-char macro-start)
(or (not (c-forward-to-cpp-define-body))
(<= (point) (c-point 'boi indent-point)))))
;; Inside a #define body and the syntactic analysis is
;; anchored on the start of the #define. In this case
;; we add cpp-define-intro to get the extra
;; indentation of the #define body.
(c-add-syntax 'cpp-define-intro)))))
;; return the syntax
c-syntactic-context)))
\f
;; Indentation calculation.
(defun c-evaluate-offset (offset langelem symbol)
;; offset can be a number, a function, a variable, a list, or one of
;; the symbols + or -
;;
;; This function might do hidden buffer changes.
(let ((res
(cond
((numberp offset) offset)
((vectorp offset) offset)
((null offset) nil)
((eq offset '+) c-basic-offset)
((eq offset '-) (- c-basic-offset))
((eq offset '++) (* 2 c-basic-offset))
((eq offset '--) (* 2 (- c-basic-offset)))
((eq offset '*) (/ c-basic-offset 2))
((eq offset '/) (/ (- c-basic-offset) 2))
((functionp offset)
(c-evaluate-offset
(funcall offset
(cons (c-langelem-sym langelem)
(c-langelem-pos langelem)))
langelem symbol))
((listp offset)
(cond
((eq (car offset) 'quote)
(c-benign-error "The offset %S for %s was mistakenly quoted"
offset symbol)
nil)
((memq (car offset) '(min max))
(let (res val (method (car offset)))
(setq offset (cdr offset))
(while offset
(setq val (c-evaluate-offset (car offset) langelem symbol))
(cond
((not val))
((not res)
(setq res val))
((integerp val)
(if (vectorp res)
(c-benign-error "\
Error evaluating offset %S for %s: \
Cannot combine absolute offset %S with relative %S in `%s' method"
(car offset) symbol res val method)
(setq res (funcall method res val))))
(t
(if (integerp res)
(c-benign-error "\
Error evaluating offset %S for %s: \
Cannot combine relative offset %S with absolute %S in `%s' method"
(car offset) symbol res val method)
(setq res (vector (funcall method (aref res 0)
(aref val 0)))))))
(setq offset (cdr offset)))
res))
((eq (car offset) 'add)
(let (res val)
(setq offset (cdr offset))
(while offset
(setq val (c-evaluate-offset (car offset) langelem symbol))
(cond
((not val))
((not res)
(setq res val))
((integerp val)
(if (vectorp res)
(setq res (vector (+ (aref res 0) val)))
(setq res (+ res val))))
(t
(if (vectorp res)
(c-benign-error "\
Error evaluating offset %S for %s: \
Cannot combine absolute offsets %S and %S in `add' method"
(car offset) symbol res val)
(setq res val)))) ; Override.
(setq offset (cdr offset)))
res))
(t
(let (res)
(when (eq (car offset) 'first)
(setq offset (cdr offset)))
(while (and (not res) offset)
(setq res (c-evaluate-offset (car offset) langelem symbol)
offset (cdr offset)))
res))))
((and (symbolp offset) (boundp offset))
(symbol-value offset))
(t
(c-benign-error "Unknown offset format %S for %s" offset symbol)
nil))))
(if (or (null res) (integerp res)
(and (vectorp res) (>= (length res) 1) (integerp (aref res 0))))
res
(c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
offset symbol res)
nil)))
(defun c-calc-offset (langelem)
;; Get offset from LANGELEM which is a list beginning with the
;; syntactic symbol and followed by any analysis data it provides.
;; That data may be zero or more elements, but if at least one is
;; given then the first is the anchor position (or nil). The symbol
;; is matched against `c-offsets-alist' and the offset calculated
;; from that is returned.
;;
;; This function might do hidden buffer changes.
(let* ((symbol (c-langelem-sym langelem))
(match (assq symbol c-offsets-alist))
(offset (cdr-safe match)))
(if match
(setq offset (c-evaluate-offset offset langelem symbol))
(if c-strict-syntax-p
(c-benign-error "No offset found for syntactic symbol %s" symbol))
(setq offset 0))
(cond
((or (vectorp offset) (numberp offset))
offset)
((and (symbolp offset) (symbol-value offset)))
(t 0))))
(defun c-get-offset (langelem)
;; This is a compatibility wrapper for `c-calc-offset' in case
;; someone is calling it directly. It takes an old style syntactic
;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
;; new list form.
;;
;; This function might do hidden buffer changes.
(if (c-langelem-pos langelem)
(c-calc-offset (list (c-langelem-sym langelem)
(c-langelem-pos langelem)))
(c-calc-offset langelem)))
(defun c-get-syntactic-indentation (langelems)
;; Calculate the syntactic indentation from a syntactic description
;; as returned by `c-guess-syntax'.
;;
;; Note that topmost-intro always has an anchor position at bol, for
;; historical reasons. It's often used together with other symbols
;; that have more sane positions. Since we always use the first
;; found anchor position, we rely on that these other symbols always
;; precede topmost-intro in the LANGELEMS list.
;;
;; This function might do hidden buffer changes.
(let ((indent 0) anchor)
(while langelems
(let* ((c-syntactic-element (car langelems))
(res (c-calc-offset c-syntactic-element)))
(if (vectorp res)
;; Got an absolute column that overrides any indentation
;; we've collected so far, but not the relative
;; indentation we might get for the nested structures
;; further down the langelems list.
(setq indent (elt res 0)
anchor (point-min)) ; A position at column 0.
;; Got a relative change of the current calculated
;; indentation.
(setq indent (+ indent res))
;; Use the anchor position from the first syntactic
;; element with one.
(unless anchor
(setq anchor (c-langelem-pos (car langelems)))))
(setq langelems (cdr langelems))))
(if anchor
(+ indent (save-excursion
(goto-char anchor)
(current-column)))
indent)))
\f
(def-edebug-spec c-bos-pop-state t)
(def-edebug-spec c-bos-save-error-info t)
(def-edebug-spec c-state-cache-top-lparen t)
(def-edebug-spec c-state-cache-top-paren t)
(def-edebug-spec c-state-cache-after-top-paren t)
(def-edebug-spec c-state-maybe-marker (form symbolp))
(def-edebug-spec c-record-type-id t)
(def-edebug-spec c-record-ref-id t)
(def-edebug-spec c-forward-keyword-prefixed-id t)
(def-edebug-spec c-forward-id-comma-list t)
(def-edebug-spec c-pull-open-brace (symbolp))
\f
(cc-provide 'cc-engine)
;; Local Variables:
;; indent-tabs-mode: t
;; tab-width: 8
;; End:
;;; cc-engine.el ends here
|