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
|
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* spell.c: code for spell checking
*
* See spellfile.c for the Vim spell file format.
*
* The spell checking mechanism uses a tree (aka trie). Each node in the tree
* has a list of bytes that can appear (siblings). For each byte there is a
* pointer to the node with the byte that follows in the word (child).
*
* A NUL byte is used where the word may end. The bytes are sorted, so that
* binary searching can be used and the NUL bytes are at the start. The
* number of possible bytes is stored before the list of bytes.
*
* The tree uses two arrays: "byts" stores the characters, "idxs" stores
* either the next index or flags. The tree starts at index 0. For example,
* to lookup "vi" this sequence is followed:
* i = 0
* len = byts[i]
* n = where "v" appears in byts[i + 1] to byts[i + len]
* i = idxs[n]
* len = byts[i]
* n = where "i" appears in byts[i + 1] to byts[i + len]
* i = idxs[n]
* len = byts[i]
* find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
*
* There are two word trees: one with case-folded words and one with words in
* original case. The second one is only used for keep-case words and is
* usually small.
*
* There is one additional tree for when not all prefixes are applied when
* generating the .spl file. This tree stores all the possible prefixes, as
* if they were words. At each word (prefix) end the prefix nr is stored, the
* following word must support this prefix nr. And the condition nr is
* stored, used to lookup the condition that the word must match with.
*
* Thanks to Olaf Seibert for providing an example implementation of this tree
* and the compression mechanism.
* LZ trie ideas:
* http://www.irb.hr/hr/home/ristov/papers/RistovLZtrieRevision1.pdf
* More papers: http://www-igm.univ-mlv.fr/~laporte/publi_en.html
*
* Matching involves checking the caps type: Onecap ALLCAP KeepCap.
*
* Why doesn't Vim use aspell/ispell/myspell/etc.?
* See ":help develop-spell".
*/
#define IN_SPELL_C
#include "vim.h"
#if defined(FEAT_SPELL) || defined(PROTO)
#ifndef UNIX // it's in os_unix.h for Unix
# include <time.h> // for time_t
#endif
#define REGION_ALL 0xff // word valid in all regions
// Result values. Lower number is accepted over higher one.
#define SP_BANNED -1
#define SP_OK 0
#define SP_RARE 1
#define SP_LOCAL 2
#define SP_BAD 3
/*
* Structure to store info for word matching.
*/
typedef struct matchinf_S
{
langp_T *mi_lp; // info for language and region
// pointers to original text to be checked
char_u *mi_word; // start of word being checked
char_u *mi_end; // end of matching word so far
char_u *mi_fend; // next char to be added to mi_fword
char_u *mi_cend; // char after what was used for
// mi_capflags
// case-folded text
char_u mi_fword[MAXWLEN + 1]; // mi_word case-folded
int mi_fwordlen; // nr of valid bytes in mi_fword
// for when checking word after a prefix
int mi_prefarridx; // index in sl_pidxs with list of
// affixID/condition
int mi_prefcnt; // number of entries at mi_prefarridx
int mi_prefixlen; // byte length of prefix
int mi_cprefixlen; // byte length of prefix in original
// case
// for when checking a compound word
int mi_compoff; // start of following word offset
char_u mi_compflags[MAXWLEN]; // flags for compound words used
int mi_complen; // nr of compound words used
int mi_compextra; // nr of COMPOUNDROOT words
// others
int mi_result; // result so far: SP_BAD, SP_OK, etc.
int mi_capflags; // WF_ONECAP WF_ALLCAP WF_KEEPCAP
win_T *mi_win; // buffer being checked
// for NOBREAK
int mi_result2; // "mi_resul" without following word
char_u *mi_end2; // "mi_end" without following word
} matchinf_T;
static int spell_mb_isword_class(int cl, win_T *wp);
// mode values for find_word
#define FIND_FOLDWORD 0 // find word case-folded
#define FIND_KEEPWORD 1 // find keep-case word
#define FIND_PREFIX 2 // find word after prefix
#define FIND_COMPOUND 3 // find case-folded compound word
#define FIND_KEEPCOMPOUND 4 // find keep-case compound word
static void find_word(matchinf_T *mip, int mode);
static void find_prefix(matchinf_T *mip, int mode);
static int fold_more(matchinf_T *mip);
static void spell_load_cb(char_u *fname, void *cookie);
static int count_syllables(slang_T *slang, char_u *word);
static void clear_midword(win_T *buf);
static void use_midword(slang_T *lp, win_T *buf);
static int find_region(char_u *rp, char_u *region);
static void spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res);
static void spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res);
static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res);
static void dump_word(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum);
static linenr_T dump_prefixes(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum);
/*
* Main spell-checking function.
* "ptr" points to a character that could be the start of a word.
* "*attrp" is set to the highlight index for a badly spelled word. For a
* non-word or when it's OK it remains unchanged.
* This must only be called when 'spelllang' is not empty.
*
* "capcol" is used to check for a Capitalised word after the end of a
* sentence. If it's zero then perform the check. Return the column where to
* check next, or -1 when no sentence end was found. If it's NULL then don't
* worry.
*
* Returns the length of the word in bytes, also when it's OK, so that the
* caller can skip over the word.
*/
int
spell_check(
win_T *wp, // current window
char_u *ptr,
hlf_T *attrp,
int *capcol, // column to check for Capital
int docount) // count good words
{
matchinf_T mi; // Most things are put in "mi" so that it can
// be passed to functions quickly.
int nrlen = 0; // found a number first
int c;
int wrongcaplen = 0;
int lpi;
int count_word = docount;
int use_camel_case = *wp->w_s->b_p_spo != NUL;
int camel_case = 0;
// A word never starts at a space or a control character. Return quickly
// then, skipping over the character.
if (*ptr <= ' ')
return 1;
// Return here when loading language files failed.
if (wp->w_s->b_langp.ga_len == 0)
return 1;
CLEAR_FIELD(mi);
// A number is always OK. Also skip hexadecimal numbers 0xFF99 and
// 0X99FF. But always do check spelling to find "3GPP" and "11
// julifeest".
if (*ptr >= '0' && *ptr <= '9')
{
if (*ptr == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
mi.mi_end = skipbin(ptr + 2);
else if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
mi.mi_end = skiphex(ptr + 2);
else
mi.mi_end = skipdigits(ptr);
nrlen = (int)(mi.mi_end - ptr);
}
// Find the normal end of the word (until the next non-word character).
mi.mi_word = ptr;
mi.mi_fend = ptr;
if (spell_iswordp(mi.mi_fend, wp))
{
int prev_upper;
int this_upper = FALSE; // init for gcc
if (use_camel_case)
{
c = PTR2CHAR(mi.mi_fend);
this_upper = SPELL_ISUPPER(c);
}
do
{
MB_PTR_ADV(mi.mi_fend);
if (use_camel_case)
{
prev_upper = this_upper;
c = PTR2CHAR(mi.mi_fend);
this_upper = SPELL_ISUPPER(c);
camel_case = !prev_upper && this_upper;
}
} while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp)
&& !camel_case);
if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
{
// Check word starting with capital letter.
c = PTR2CHAR(ptr);
if (!SPELL_ISUPPER(c))
wrongcaplen = (int)(mi.mi_fend - ptr);
}
}
if (capcol != NULL)
*capcol = -1;
// We always use the characters up to the next non-word character,
// also for bad words.
mi.mi_end = mi.mi_fend;
// Check caps type later.
mi.mi_capflags = 0;
mi.mi_cend = NULL;
mi.mi_win = wp;
// case-fold the word with one non-word character, so that we can check
// for the word end.
if (*mi.mi_fend != NUL)
MB_PTR_ADV(mi.mi_fend);
(void)spell_casefold(wp, ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
MAXWLEN + 1);
mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
if (camel_case)
// Introduce a fake word end space into the folded word.
mi.mi_fword[mi.mi_fwordlen - 1] = ' ';
// The word is bad unless we recognize it.
mi.mi_result = SP_BAD;
mi.mi_result2 = SP_BAD;
/*
* Loop over the languages specified in 'spelllang'.
* We check them all, because a word may be matched longer in another
* language.
*/
for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
{
mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
// If reloading fails the language is still in the list but everything
// has been cleared.
if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
continue;
// Check for a matching word in case-folded words.
find_word(&mi, FIND_FOLDWORD);
// Check for a matching word in keep-case words.
find_word(&mi, FIND_KEEPWORD);
// Check for matching prefixes.
find_prefix(&mi, FIND_FOLDWORD);
// For a NOBREAK language, may want to use a word without a following
// word as a backup.
if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
&& mi.mi_result2 != SP_BAD)
{
mi.mi_result = mi.mi_result2;
mi.mi_end = mi.mi_end2;
}
// Count the word in the first language where it's found to be OK.
if (count_word && mi.mi_result == SP_OK)
{
count_common_word(mi.mi_lp->lp_slang, ptr,
(int)(mi.mi_end - ptr), 1);
count_word = FALSE;
}
}
if (mi.mi_result != SP_OK)
{
// If we found a number skip over it. Allows for "42nd". Do flag
// rare and local words, e.g., "3GPP".
if (nrlen > 0)
{
if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
return nrlen;
}
// When we are at a non-word character there is no error, just
// skip over the character (try looking for a word after it).
else if (!spell_iswordp_nmw(ptr, wp))
{
if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
{
regmatch_T regmatch;
int r;
// Check for end of sentence.
regmatch.regprog = wp->w_s->b_cap_prog;
regmatch.rm_ic = FALSE;
r = vim_regexec(®match, ptr, 0);
wp->w_s->b_cap_prog = regmatch.regprog;
if (r)
*capcol = (int)(regmatch.endp[0] - ptr);
}
if (has_mbyte)
return (*mb_ptr2len)(ptr);
return 1;
}
else if (mi.mi_end == ptr)
// Always include at least one character. Required for when there
// is a mixup in "midword".
MB_PTR_ADV(mi.mi_end);
else if (mi.mi_result == SP_BAD
&& LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
{
char_u *p, *fp;
int save_result = mi.mi_result;
// First language in 'spelllang' is NOBREAK. Find first position
// at which any word would be valid.
mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
{
p = mi.mi_word;
fp = mi.mi_fword;
for (;;)
{
MB_PTR_ADV(p);
MB_PTR_ADV(fp);
if (p >= mi.mi_end)
break;
mi.mi_compoff = (int)(fp - mi.mi_fword);
find_word(&mi, FIND_COMPOUND);
if (mi.mi_result != SP_BAD)
{
mi.mi_end = p;
break;
}
}
mi.mi_result = save_result;
}
}
if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
*attrp = HLF_SPB;
else if (mi.mi_result == SP_RARE)
*attrp = HLF_SPR;
else
*attrp = HLF_SPL;
}
if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
{
// Report SpellCap only when the word isn't badly spelled.
*attrp = HLF_SPC;
return wrongcaplen;
}
return (int)(mi.mi_end - ptr);
}
/*
* Check if the word at "mip->mi_word" is in the tree.
* When "mode" is FIND_FOLDWORD check in fold-case word tree.
* When "mode" is FIND_KEEPWORD check in keep-case word tree.
* When "mode" is FIND_PREFIX check for word after prefix in fold-case word
* tree.
*
* For a match mip->mi_result is updated.
*/
static void
find_word(matchinf_T *mip, int mode)
{
idx_T arridx = 0;
int endlen[MAXWLEN]; // length at possible word endings
idx_T endidx[MAXWLEN]; // possible word endings
int endidxcnt = 0;
int len;
int wlen = 0;
int flen;
int c;
char_u *ptr;
idx_T lo, hi, m;
char_u *s;
char_u *p;
int res = SP_BAD;
slang_T *slang = mip->mi_lp->lp_slang;
unsigned flags;
char_u *byts;
idx_T *idxs;
int word_ends;
int prefix_found;
int nobreak_result;
if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
{
// Check for word with matching case in keep-case tree.
ptr = mip->mi_word;
flen = 9999; // no case folding, always enough bytes
byts = slang->sl_kbyts;
idxs = slang->sl_kidxs;
if (mode == FIND_KEEPCOMPOUND)
// Skip over the previously found word(s).
wlen += mip->mi_compoff;
}
else
{
// Check for case-folded in case-folded tree.
ptr = mip->mi_fword;
flen = mip->mi_fwordlen; // available case-folded bytes
byts = slang->sl_fbyts;
idxs = slang->sl_fidxs;
if (mode == FIND_PREFIX)
{
// Skip over the prefix.
wlen = mip->mi_prefixlen;
flen -= mip->mi_prefixlen;
}
else if (mode == FIND_COMPOUND)
{
// Skip over the previously found word(s).
wlen = mip->mi_compoff;
flen -= mip->mi_compoff;
}
}
if (byts == NULL)
return; // array is empty
/*
* Repeat advancing in the tree until:
* - there is a byte that doesn't match,
* - we reach the end of the tree,
* - or we reach the end of the line.
*/
for (;;)
{
if (flen <= 0 && *mip->mi_fend != NUL)
flen = fold_more(mip);
len = byts[arridx++];
// If the first possible byte is a zero the word could end here.
// Remember this index, we first check for the longest word.
if (byts[arridx] == 0)
{
if (endidxcnt == MAXWLEN)
{
// Must be a corrupted spell file.
emsg(_(e_format));
return;
}
endlen[endidxcnt] = wlen;
endidx[endidxcnt++] = arridx++;
--len;
// Skip over the zeros, there can be several flag/region
// combinations.
while (len > 0 && byts[arridx] == 0)
{
++arridx;
--len;
}
if (len == 0)
break; // no children, word must end here
}
// Stop looking at end of the line.
if (ptr[wlen] == NUL)
break;
// Perform a binary search in the list of accepted bytes.
c = ptr[wlen];
if (c == TAB) // <Tab> is handled like <Space>
c = ' ';
lo = arridx;
hi = arridx + len - 1;
while (lo < hi)
{
m = (lo + hi) / 2;
if (byts[m] > c)
hi = m - 1;
else if (byts[m] < c)
lo = m + 1;
else
{
lo = hi = m;
break;
}
}
// Stop if there is no matching byte.
if (hi < lo || byts[lo] != c)
break;
// Continue at the child (if there is one).
arridx = idxs[lo];
++wlen;
--flen;
// One space in the good word may stand for several spaces in the
// checked word.
if (c == ' ')
{
for (;;)
{
if (flen <= 0 && *mip->mi_fend != NUL)
flen = fold_more(mip);
if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
break;
++wlen;
--flen;
}
}
}
/*
* Verify that one of the possible endings is valid. Try the longest
* first.
*/
while (endidxcnt > 0)
{
--endidxcnt;
arridx = endidx[endidxcnt];
wlen = endlen[endidxcnt];
if ((*mb_head_off)(ptr, ptr + wlen) > 0)
continue; // not at first byte of character
if (spell_iswordp(ptr + wlen, mip->mi_win))
{
if (slang->sl_compprog == NULL && !slang->sl_nobreak)
continue; // next char is a word character
word_ends = FALSE;
}
else
word_ends = TRUE;
// The prefix flag is before compound flags. Once a valid prefix flag
// has been found we try compound flags.
prefix_found = FALSE;
if (mode != FIND_KEEPWORD && has_mbyte)
{
// Compute byte length in original word, length may change
// when folding case. This can be slow, take a shortcut when the
// case-folded word is equal to the keep-case word.
p = mip->mi_word;
if (STRNCMP(ptr, p, wlen) != 0)
{
for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
MB_PTR_ADV(p);
wlen = (int)(p - mip->mi_word);
}
}
// Check flags and region. For FIND_PREFIX check the condition and
// prefix ID.
// Repeat this if there are more flags/region alternatives until there
// is a match.
res = SP_BAD;
for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
--len, ++arridx)
{
flags = idxs[arridx];
// For the fold-case tree check that the case of the checked word
// matches with what the word in the tree requires.
// For keep-case tree the case is always right. For prefixes we
// don't bother to check.
if (mode == FIND_FOLDWORD)
{
if (mip->mi_cend != mip->mi_word + wlen)
{
// mi_capflags was set for a different word length, need
// to do it again.
mip->mi_cend = mip->mi_word + wlen;
mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
}
if (mip->mi_capflags == WF_KEEPCAP
|| !spell_valid_case(mip->mi_capflags, flags))
continue;
}
// When mode is FIND_PREFIX the word must support the prefix:
// check the prefix ID and the condition. Do that for the list at
// mip->mi_prefarridx that find_prefix() filled.
else if (mode == FIND_PREFIX && !prefix_found)
{
c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
flags,
mip->mi_word + mip->mi_cprefixlen, slang,
FALSE);
if (c == 0)
continue;
// Use the WF_RARE flag for a rare prefix.
if (c & WF_RAREPFX)
flags |= WF_RARE;
prefix_found = TRUE;
}
if (slang->sl_nobreak)
{
if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
&& (flags & WF_BANNED) == 0)
{
// NOBREAK: found a valid following word. That's all we
// need to know, so return.
mip->mi_result = SP_OK;
break;
}
}
else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
|| !word_ends))
{
// If there is no compound flag or the word is shorter than
// COMPOUNDMIN reject it quickly.
// Makes you wonder why someone puts a compound flag on a word
// that's too short... Myspell compatibility requires this
// anyway.
if (((unsigned)flags >> 24) == 0
|| wlen - mip->mi_compoff < slang->sl_compminlen)
continue;
// For multi-byte chars check character length against
// COMPOUNDMIN.
if (has_mbyte
&& slang->sl_compminlen > 0
&& mb_charlen_len(mip->mi_word + mip->mi_compoff,
wlen - mip->mi_compoff) < slang->sl_compminlen)
continue;
// Limit the number of compound words to COMPOUNDWORDMAX if no
// maximum for syllables is specified.
if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
> slang->sl_compmax
&& slang->sl_compsylmax == MAXWLEN)
continue;
// Don't allow compounding on a side where an affix was added,
// unless COMPOUNDPERMITFLAG was used.
if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
continue;
if (!word_ends && (flags & WF_NOCOMPAFT))
continue;
// Quickly check if compounding is possible with this flag.
if (!byte_in_str(mip->mi_complen == 0
? slang->sl_compstartflags
: slang->sl_compallflags,
((unsigned)flags >> 24)))
continue;
// If there is a match with a CHECKCOMPOUNDPATTERN rule
// discard the compound word.
if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
continue;
if (mode == FIND_COMPOUND)
{
int capflags;
// Need to check the caps type of the appended compound
// word.
if (has_mbyte && STRNCMP(ptr, mip->mi_word,
mip->mi_compoff) != 0)
{
// case folding may have changed the length
p = mip->mi_word;
for (s = ptr; s < ptr + mip->mi_compoff; MB_PTR_ADV(s))
MB_PTR_ADV(p);
}
else
p = mip->mi_word + mip->mi_compoff;
capflags = captype(p, mip->mi_word + wlen);
if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
&& (flags & WF_FIXCAP) != 0))
continue;
if (capflags != WF_ALLCAP)
{
// When the character before the word is a word
// character we do not accept a Onecap word. We do
// accept a no-caps word, even when the dictionary
// word specifies ONECAP.
MB_PTR_BACK(mip->mi_word, p);
if (spell_iswordp_nmw(p, mip->mi_win)
? capflags == WF_ONECAP
: (flags & WF_ONECAP) != 0
&& capflags != WF_ONECAP)
continue;
}
}
// If the word ends the sequence of compound flags of the
// words must match with one of the COMPOUNDRULE items and
// the number of syllables must not be too large.
mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
mip->mi_compflags[mip->mi_complen + 1] = NUL;
if (word_ends)
{
char_u fword[MAXWLEN];
if (slang->sl_compsylmax < MAXWLEN)
{
// "fword" is only needed for checking syllables.
if (ptr == mip->mi_word)
(void)spell_casefold(mip->mi_win,
ptr, wlen, fword, MAXWLEN);
else
vim_strncpy(fword, ptr, endlen[endidxcnt]);
}
if (!can_compound(slang, fword, mip->mi_compflags))
continue;
}
else if (slang->sl_comprules != NULL
&& !match_compoundrule(slang, mip->mi_compflags))
// The compound flags collected so far do not match any
// COMPOUNDRULE, discard the compounded word.
continue;
}
// Check NEEDCOMPOUND: can't use word without compounding.
else if (flags & WF_NEEDCOMP)
continue;
nobreak_result = SP_OK;
if (!word_ends)
{
int save_result = mip->mi_result;
char_u *save_end = mip->mi_end;
langp_T *save_lp = mip->mi_lp;
int lpi;
// Check that a valid word follows. If there is one and we
// are compounding, it will set "mi_result", thus we are
// always finished here. For NOBREAK we only check that a
// valid word follows.
// Recursive!
if (slang->sl_nobreak)
mip->mi_result = SP_BAD;
// Find following word in case-folded tree.
mip->mi_compoff = endlen[endidxcnt];
if (has_mbyte && mode == FIND_KEEPWORD)
{
// Compute byte length in case-folded word from "wlen":
// byte length in keep-case word. Length may change when
// folding case. This can be slow, take a shortcut when
// the case-folded word is equal to the keep-case word.
p = mip->mi_fword;
if (STRNCMP(ptr, p, wlen) != 0)
{
for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
MB_PTR_ADV(p);
mip->mi_compoff = (int)(p - mip->mi_fword);
}
}
#if 0 // Disabled, see below
c = mip->mi_compoff;
#endif
++mip->mi_complen;
if (flags & WF_COMPROOT)
++mip->mi_compextra;
// For NOBREAK we need to try all NOBREAK languages, at least
// to find the ".add" file(s).
for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
{
if (slang->sl_nobreak)
{
mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
if (mip->mi_lp->lp_slang->sl_fidxs == NULL
|| !mip->mi_lp->lp_slang->sl_nobreak)
continue;
}
find_word(mip, FIND_COMPOUND);
// When NOBREAK any word that matches is OK. Otherwise we
// need to find the longest match, thus try with keep-case
// and prefix too.
if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
{
// Find following word in keep-case tree.
mip->mi_compoff = wlen;
find_word(mip, FIND_KEEPCOMPOUND);
#if 0 // Disabled, a prefix must not appear halfway a compound word,
// unless the COMPOUNDPERMITFLAG is used and then it can't be a
// postponed prefix.
if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
{
// Check for following word with prefix.
mip->mi_compoff = c;
find_prefix(mip, FIND_COMPOUND);
}
#endif
}
if (!slang->sl_nobreak)
break;
}
--mip->mi_complen;
if (flags & WF_COMPROOT)
--mip->mi_compextra;
mip->mi_lp = save_lp;
if (slang->sl_nobreak)
{
nobreak_result = mip->mi_result;
mip->mi_result = save_result;
mip->mi_end = save_end;
}
else
{
if (mip->mi_result == SP_OK)
break;
continue;
}
}
if (flags & WF_BANNED)
res = SP_BANNED;
else if (flags & WF_REGION)
{
// Check region.
if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
res = SP_OK;
else
res = SP_LOCAL;
}
else if (flags & WF_RARE)
res = SP_RARE;
else
res = SP_OK;
// Always use the longest match and the best result. For NOBREAK
// we separately keep the longest match without a following good
// word as a fall-back.
if (nobreak_result == SP_BAD)
{
if (mip->mi_result2 > res)
{
mip->mi_result2 = res;
mip->mi_end2 = mip->mi_word + wlen;
}
else if (mip->mi_result2 == res
&& mip->mi_end2 < mip->mi_word + wlen)
mip->mi_end2 = mip->mi_word + wlen;
}
else if (mip->mi_result > res)
{
mip->mi_result = res;
mip->mi_end = mip->mi_word + wlen;
}
else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
mip->mi_end = mip->mi_word + wlen;
if (mip->mi_result == SP_OK)
break;
}
if (mip->mi_result == SP_OK)
break;
}
}
/*
* Return TRUE if there is a match between the word ptr[wlen] and
* CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
* word.
* A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
* end of ptr[wlen] and the second part matches after it.
*/
int
match_checkcompoundpattern(
char_u *ptr,
int wlen,
garray_T *gap) // &sl_comppat
{
int i;
char_u *p;
int len;
for (i = 0; i + 1 < gap->ga_len; i += 2)
{
p = ((char_u **)gap->ga_data)[i + 1];
if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
{
// Second part matches at start of following compound word, now
// check if first part matches at end of previous word.
p = ((char_u **)gap->ga_data)[i];
len = (int)STRLEN(p);
if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
return TRUE;
}
}
return FALSE;
}
/*
* Return TRUE if "flags" is a valid sequence of compound flags and "word"
* does not have too many syllables.
*/
int
can_compound(slang_T *slang, char_u *word, char_u *flags)
{
char_u uflags[MAXWLEN * 2];
int i;
char_u *p;
if (slang->sl_compprog == NULL)
return FALSE;
if (enc_utf8)
{
// Need to convert the single byte flags to utf8 characters.
p = uflags;
for (i = 0; flags[i] != NUL; ++i)
p += utf_char2bytes(flags[i], p);
*p = NUL;
p = uflags;
}
else
p = flags;
if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0))
return FALSE;
// Count the number of syllables. This may be slow, do it last. If there
// are too many syllables AND the number of compound words is above
// COMPOUNDWORDMAX then compounding is not allowed.
if (slang->sl_compsylmax < MAXWLEN
&& count_syllables(slang, word) > slang->sl_compsylmax)
return (int)STRLEN(flags) < slang->sl_compmax;
return TRUE;
}
/*
* Return TRUE if the compound flags in compflags[] match the start of any
* compound rule. This is used to stop trying a compound if the flags
* collected so far can't possibly match any compound rule.
* Caller must check that slang->sl_comprules is not NULL.
*/
int
match_compoundrule(slang_T *slang, char_u *compflags)
{
char_u *p;
int i;
int c;
// loop over all the COMPOUNDRULE entries
for (p = slang->sl_comprules; *p != NUL; ++p)
{
// loop over the flags in the compound word we have made, match
// them against the current rule entry
for (i = 0; ; ++i)
{
c = compflags[i];
if (c == NUL)
// found a rule that matches for the flags we have so far
return TRUE;
if (*p == '/' || *p == NUL)
break; // end of rule, it's too short
if (*p == '[')
{
int match = FALSE;
// compare against all the flags in []
++p;
while (*p != ']' && *p != NUL)
if (*p++ == c)
match = TRUE;
if (!match)
break; // none matches
}
else if (*p != c)
break; // flag of word doesn't match flag in pattern
++p;
}
// Skip to the next "/", where the next pattern starts.
p = vim_strchr(p, '/');
if (p == NULL)
break;
}
// Checked all the rules and none of them match the flags, so there
// can't possibly be a compound starting with these flags.
return FALSE;
}
/*
* Return non-zero if the prefix indicated by "arridx" matches with the prefix
* ID in "flags" for the word "word".
* The WF_RAREPFX flag is included in the return value for a rare prefix.
*/
int
valid_word_prefix(
int totprefcnt, // nr of prefix IDs
int arridx, // idx in sl_pidxs[]
int flags,
char_u *word,
slang_T *slang,
int cond_req) // only use prefixes with a condition
{
int prefcnt;
int pidx;
regprog_T **rp;
int prefid;
prefid = (unsigned)flags >> 24;
for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
{
pidx = slang->sl_pidxs[arridx + prefcnt];
// Check the prefix ID.
if (prefid != (pidx & 0xff))
continue;
// Check if the prefix doesn't combine and the word already has a
// suffix.
if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
continue;
// Check the condition, if there is one. The condition index is
// stored in the two bytes above the prefix ID byte.
rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
if (*rp != NULL)
{
if (!vim_regexec_prog(rp, FALSE, word, 0))
continue;
}
else if (cond_req)
continue;
// It's a match! Return the WF_ flags.
return pidx;
}
return 0;
}
/*
* Check if the word at "mip->mi_word" has a matching prefix.
* If it does, then check the following word.
*
* If "mode" is "FIND_COMPOUND" then do the same after another word, find a
* prefix in a compound word.
*
* For a match mip->mi_result is updated.
*/
static void
find_prefix(matchinf_T *mip, int mode)
{
idx_T arridx = 0;
int len;
int wlen = 0;
int flen;
int c;
char_u *ptr;
idx_T lo, hi, m;
slang_T *slang = mip->mi_lp->lp_slang;
char_u *byts;
idx_T *idxs;
byts = slang->sl_pbyts;
if (byts == NULL)
return; // array is empty
// We use the case-folded word here, since prefixes are always
// case-folded.
ptr = mip->mi_fword;
flen = mip->mi_fwordlen; // available case-folded bytes
if (mode == FIND_COMPOUND)
{
// Skip over the previously found word(s).
ptr += mip->mi_compoff;
flen -= mip->mi_compoff;
}
idxs = slang->sl_pidxs;
/*
* Repeat advancing in the tree until:
* - there is a byte that doesn't match,
* - we reach the end of the tree,
* - or we reach the end of the line.
*/
for (;;)
{
if (flen == 0 && *mip->mi_fend != NUL)
flen = fold_more(mip);
len = byts[arridx++];
// If the first possible byte is a zero the prefix could end here.
// Check if the following word matches and supports the prefix.
if (byts[arridx] == 0)
{
// There can be several prefixes with different conditions. We
// try them all, since we don't know which one will give the
// longest match. The word is the same each time, pass the list
// of possible prefixes to find_word().
mip->mi_prefarridx = arridx;
mip->mi_prefcnt = len;
while (len > 0 && byts[arridx] == 0)
{
++arridx;
--len;
}
mip->mi_prefcnt -= len;
// Find the word that comes after the prefix.
mip->mi_prefixlen = wlen;
if (mode == FIND_COMPOUND)
// Skip over the previously found word(s).
mip->mi_prefixlen += mip->mi_compoff;
if (has_mbyte)
{
// Case-folded length may differ from original length.
mip->mi_cprefixlen = nofold_len(mip->mi_fword,
mip->mi_prefixlen, mip->mi_word);
}
else
mip->mi_cprefixlen = mip->mi_prefixlen;
find_word(mip, FIND_PREFIX);
if (len == 0)
break; // no children, word must end here
}
// Stop looking at end of the line.
if (ptr[wlen] == NUL)
break;
// Perform a binary search in the list of accepted bytes.
c = ptr[wlen];
lo = arridx;
hi = arridx + len - 1;
while (lo < hi)
{
m = (lo + hi) / 2;
if (byts[m] > c)
hi = m - 1;
else if (byts[m] < c)
lo = m + 1;
else
{
lo = hi = m;
break;
}
}
// Stop if there is no matching byte.
if (hi < lo || byts[lo] != c)
break;
// Continue at the child (if there is one).
arridx = idxs[lo];
++wlen;
--flen;
}
}
/*
* Need to fold at least one more character. Do until next non-word character
* for efficiency. Include the non-word character too.
* Return the length of the folded chars in bytes.
*/
static int
fold_more(matchinf_T *mip)
{
int flen;
char_u *p;
p = mip->mi_fend;
do
MB_PTR_ADV(mip->mi_fend);
while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
// Include the non-word character so that we can check for the word end.
if (*mip->mi_fend != NUL)
MB_PTR_ADV(mip->mi_fend);
(void)spell_casefold(mip->mi_win, p, (int)(mip->mi_fend - p),
mip->mi_fword + mip->mi_fwordlen,
MAXWLEN - mip->mi_fwordlen);
flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
mip->mi_fwordlen += flen;
return flen;
}
/*
* Check case flags for a word. Return TRUE if the word has the requested
* case.
*/
int
spell_valid_case(
int wordflags, // flags for the checked word.
int treeflags) // flags for the word in the spell tree
{
return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
|| ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
&& ((treeflags & WF_ONECAP) == 0
|| (wordflags & WF_ONECAP) != 0)));
}
/*
* Return TRUE if spell checking is not enabled.
*/
static int
no_spell_checking(win_T *wp)
{
if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
|| wp->w_s->b_langp.ga_len == 0)
{
emsg(_(e_spell_checking_is_not_possible));
return TRUE;
}
return FALSE;
}
/*
* Move to next spell error.
* "curline" is FALSE for "[s", "]s", "[S" and "]S".
* "curline" is TRUE to find word under/after cursor in the same line.
* For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
* to after badly spelled word before the cursor.
* Return 0 if not found, length of the badly spelled word otherwise.
*/
int
spell_move_to(
win_T *wp,
int dir, // FORWARD or BACKWARD
int allwords, // TRUE for "[s"/"]s", FALSE for "[S"/"]S"
int curline,
hlf_T *attrp) // return: attributes of bad word or NULL
// (only when "dir" is FORWARD)
{
linenr_T lnum;
pos_T found_pos;
int found_len = 0;
char_u *line;
char_u *p;
char_u *endp;
hlf_T attr;
int len;
#ifdef FEAT_SYN_HL
int has_syntax = syntax_present(wp);
#endif
int col;
int can_spell;
char_u *buf = NULL;
int buflen = 0;
int skip = 0;
int capcol = -1;
int found_one = FALSE;
int wrapped = FALSE;
if (no_spell_checking(wp))
return 0;
/*
* Start looking for bad word at the start of the line, because we can't
* start halfway a word, we don't know where it starts or ends.
*
* When searching backwards, we continue in the line to find the last
* bad word (in the cursor line: before the cursor).
*
* We concatenate the start of the next line, so that wrapped words work
* (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
* though...
*/
lnum = wp->w_cursor.lnum;
CLEAR_POS(&found_pos);
while (!got_int)
{
line = ml_get_buf(wp->w_buffer, lnum, FALSE);
len = (int)STRLEN(line);
if (buflen < len + MAXWLEN + 2)
{
vim_free(buf);
buflen = len + MAXWLEN + 2;
buf = alloc(buflen);
if (buf == NULL)
break;
}
// In first line check first word for Capital.
if (lnum == 1)
capcol = 0;
// For checking first word with a capital skip white space.
if (capcol == 0)
capcol = getwhitecols(line);
else if (curline && wp == curwin)
{
// For spellbadword(): check if first word needs a capital.
col = getwhitecols(line);
if (check_need_cap(lnum, col))
capcol = col;
// Need to get the line again, may have looked at the previous
// one.
line = ml_get_buf(wp->w_buffer, lnum, FALSE);
}
// Copy the line into "buf" and append the start of the next line if
// possible.
STRCPY(buf, line);
if (lnum < wp->w_buffer->b_ml.ml_line_count)
spell_cat_line(buf + STRLEN(buf),
ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
p = buf + skip;
endp = buf + len;
while (p < endp)
{
// When searching backward don't search after the cursor. Unless
// we wrapped around the end of the buffer.
if (dir == BACKWARD
&& lnum == wp->w_cursor.lnum
&& !wrapped
&& (colnr_T)(p - buf) >= wp->w_cursor.col)
break;
// start of word
attr = HLF_COUNT;
len = spell_check(wp, p, &attr, &capcol, FALSE);
if (attr != HLF_COUNT)
{
// We found a bad word. Check the attribute.
if (allwords || attr == HLF_SPB)
{
// When searching forward only accept a bad word after
// the cursor.
if (dir == BACKWARD
|| lnum != wp->w_cursor.lnum
|| (lnum == wp->w_cursor.lnum
&& (wrapped
|| (colnr_T)(curline ? p - buf + len
: p - buf)
> wp->w_cursor.col)))
{
#ifdef FEAT_SYN_HL
if (has_syntax)
{
col = (int)(p - buf);
(void)syn_get_id(wp, lnum, (colnr_T)col,
FALSE, &can_spell, FALSE);
if (!can_spell)
attr = HLF_COUNT;
}
else
#endif
can_spell = TRUE;
if (can_spell)
{
found_one = TRUE;
found_pos.lnum = lnum;
found_pos.col = (int)(p - buf);
found_pos.coladd = 0;
if (dir == FORWARD)
{
// No need to search further.
wp->w_cursor = found_pos;
vim_free(buf);
if (attrp != NULL)
*attrp = attr;
return len;
}
else if (curline)
// Insert mode completion: put cursor after
// the bad word.
found_pos.col += len;
found_len = len;
}
}
else
found_one = TRUE;
}
}
// advance to character after the word
p += len;
capcol -= len;
}
if (dir == BACKWARD && found_pos.lnum != 0)
{
// Use the last match in the line (before the cursor).
wp->w_cursor = found_pos;
vim_free(buf);
return found_len;
}
if (curline)
break; // only check cursor line
// If we are back at the starting line and searched it again there
// is no match, give up.
if (lnum == wp->w_cursor.lnum && wrapped)
break;
// Advance to next line.
if (dir == BACKWARD)
{
if (lnum > 1)
--lnum;
else if (!p_ws)
break; // at first line and 'nowrapscan'
else
{
// Wrap around to the end of the buffer. May search the
// starting line again and accept the last match.
lnum = wp->w_buffer->b_ml.ml_line_count;
wrapped = TRUE;
if (!shortmess(SHM_SEARCH))
give_warning((char_u *)_(top_bot_msg), TRUE);
}
capcol = -1;
}
else
{
if (lnum < wp->w_buffer->b_ml.ml_line_count)
++lnum;
else if (!p_ws)
break; // at first line and 'nowrapscan'
else
{
// Wrap around to the start of the buffer. May search the
// starting line again and accept the first match.
lnum = 1;
wrapped = TRUE;
if (!shortmess(SHM_SEARCH))
give_warning((char_u *)_(bot_top_msg), TRUE);
}
// If we are back at the starting line and there is no match then
// give up.
if (lnum == wp->w_cursor.lnum && !found_one)
break;
// Skip the characters at the start of the next line that were
// included in a match crossing line boundaries.
if (attr == HLF_COUNT)
skip = (int)(p - endp);
else
skip = 0;
// Capcol skips over the inserted space.
--capcol;
// But after empty line check first word in next line
if (*skipwhite(line) == NUL)
capcol = 0;
}
line_breakcheck();
}
vim_free(buf);
return 0;
}
/*
* For spell checking: concatenate the start of the following line "line" into
* "buf", blanking-out special characters. Copy less than "maxlen" bytes.
* Keep the blanks at the start of the next line, this is used in win_line()
* to skip those bytes if the word was OK.
*/
void
spell_cat_line(char_u *buf, char_u *line, int maxlen)
{
char_u *p;
int n;
p = skipwhite(line);
while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
p = skipwhite(p + 1);
if (*p != NUL)
{
// Only worth concatenating if there is something else than spaces to
// concatenate.
n = (int)(p - line) + 1;
if (n < maxlen - 1)
{
vim_memset(buf, ' ', n);
vim_strncpy(buf + n, p, maxlen - 1 - n);
}
}
}
/*
* Structure used for the cookie argument of do_in_runtimepath().
*/
typedef struct spelload_S
{
char_u sl_lang[MAXWLEN + 1]; // language name
slang_T *sl_slang; // resulting slang_T struct
int sl_nobreak; // NOBREAK language found
} spelload_T;
/*
* Load word list(s) for "lang" from Vim spell file(s).
* "lang" must be the language without the region: e.g., "en".
*/
static void
spell_load_lang(char_u *lang)
{
char_u fname_enc[85];
int r;
spelload_T sl;
int round;
// Copy the language name to pass it to spell_load_cb() as a cookie.
// It's truncated when an error is detected.
STRCPY(sl.sl_lang, lang);
sl.sl_slang = NULL;
sl.sl_nobreak = FALSE;
// We may retry when no spell file is found for the language, an
// autocommand may load it then.
for (round = 1; round <= 2; ++round)
{
/*
* Find the first spell file for "lang" in 'runtimepath' and load it.
*/
vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
#ifdef VMS
"spell/%s_%s.spl",
#else
"spell/%s.%s.spl",
#endif
lang, spell_enc());
r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
if (r == FAIL && *sl.sl_lang != NUL)
{
// Try loading the ASCII version.
vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
#ifdef VMS
"spell/%s_ascii.spl",
#else
"spell/%s.ascii.spl",
#endif
lang);
r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
if (r == FAIL && *sl.sl_lang != NUL && round == 1
&& apply_autocmds(EVENT_SPELLFILEMISSING, lang,
curbuf->b_fname, FALSE, curbuf))
continue;
break;
}
break;
}
if (r == FAIL)
{
smsg(
#ifdef VMS
_("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
#else
_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
#endif
lang, spell_enc(), lang);
}
else if (sl.sl_slang != NULL)
{
// At least one file was loaded, now load ALL the additions.
STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl);
}
}
/*
* Return the encoding used for spell checking: Use 'encoding', except that we
* use "latin1" for "latin9". And limit to 60 characters (just in case).
*/
char_u *
spell_enc(void)
{
if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
return p_enc;
return (char_u *)"latin1";
}
/*
* Get the name of the .spl file for the internal wordlist into
* "fname[MAXPATHL]".
*/
static void
int_wordlist_spl(char_u *fname)
{
vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
int_wordlist, spell_enc());
}
/*
* Allocate a new slang_T for language "lang". "lang" can be NULL.
* Caller must fill "sl_next".
*/
slang_T *
slang_alloc(char_u *lang)
{
slang_T *lp;
lp = ALLOC_CLEAR_ONE(slang_T);
if (lp != NULL)
{
if (lang != NULL)
lp->sl_name = vim_strsave(lang);
ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
lp->sl_compmax = MAXWLEN;
lp->sl_compsylmax = MAXWLEN;
hash_init(&lp->sl_wordcount);
}
return lp;
}
/*
* Free the contents of an slang_T and the structure itself.
*/
void
slang_free(slang_T *lp)
{
vim_free(lp->sl_name);
vim_free(lp->sl_fname);
slang_clear(lp);
vim_free(lp);
}
/*
* Clear an slang_T so that the file can be reloaded.
*/
void
slang_clear(slang_T *lp)
{
garray_T *gap;
fromto_T *ftp;
salitem_T *smp;
int i;
int round;
VIM_CLEAR(lp->sl_fbyts);
VIM_CLEAR(lp->sl_kbyts);
VIM_CLEAR(lp->sl_pbyts);
VIM_CLEAR(lp->sl_fidxs);
VIM_CLEAR(lp->sl_kidxs);
VIM_CLEAR(lp->sl_pidxs);
for (round = 1; round <= 2; ++round)
{
gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
while (gap->ga_len > 0)
{
ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
vim_free(ftp->ft_from);
vim_free(ftp->ft_to);
}
ga_clear(gap);
}
gap = &lp->sl_sal;
if (lp->sl_sofo)
{
// "ga_len" is set to 1 without adding an item for latin1
if (gap->ga_data != NULL)
// SOFOFROM and SOFOTO items: free lists of wide characters.
for (i = 0; i < gap->ga_len; ++i)
vim_free(((int **)gap->ga_data)[i]);
}
else
// SAL items: free salitem_T items
while (gap->ga_len > 0)
{
smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
vim_free(smp->sm_lead);
// Don't free sm_oneof and sm_rules, they point into sm_lead.
vim_free(smp->sm_to);
vim_free(smp->sm_lead_w);
vim_free(smp->sm_oneof_w);
vim_free(smp->sm_to_w);
}
ga_clear(gap);
for (i = 0; i < lp->sl_prefixcnt; ++i)
vim_regfree(lp->sl_prefprog[i]);
lp->sl_prefixcnt = 0;
VIM_CLEAR(lp->sl_prefprog);
VIM_CLEAR(lp->sl_info);
VIM_CLEAR(lp->sl_midword);
vim_regfree(lp->sl_compprog);
lp->sl_compprog = NULL;
VIM_CLEAR(lp->sl_comprules);
VIM_CLEAR(lp->sl_compstartflags);
VIM_CLEAR(lp->sl_compallflags);
VIM_CLEAR(lp->sl_syllable);
ga_clear(&lp->sl_syl_items);
ga_clear_strings(&lp->sl_comppat);
hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
hash_init(&lp->sl_wordcount);
hash_clear_all(&lp->sl_map_hash, 0);
// Clear info from .sug file.
slang_clear_sug(lp);
lp->sl_compmax = MAXWLEN;
lp->sl_compminlen = 0;
lp->sl_compsylmax = MAXWLEN;
lp->sl_regions[0] = NUL;
}
/*
* Clear the info from the .sug file in "lp".
*/
void
slang_clear_sug(slang_T *lp)
{
VIM_CLEAR(lp->sl_sbyts);
VIM_CLEAR(lp->sl_sidxs);
close_spellbuf(lp->sl_sugbuf);
lp->sl_sugbuf = NULL;
lp->sl_sugloaded = FALSE;
lp->sl_sugtime = 0;
}
/*
* Load one spell file and store the info into a slang_T.
* Invoked through do_in_runtimepath().
*/
static void
spell_load_cb(char_u *fname, void *cookie)
{
spelload_T *slp = (spelload_T *)cookie;
slang_T *slang;
slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
if (slang != NULL)
{
// When a previously loaded file has NOBREAK also use it for the
// ".add" files.
if (slp->sl_nobreak && slang->sl_add)
slang->sl_nobreak = TRUE;
else if (slang->sl_nobreak)
slp->sl_nobreak = TRUE;
slp->sl_slang = slang;
}
}
/*
* Add a word to the hashtable of common words.
* If it's already there then the counter is increased.
*/
void
count_common_word(
slang_T *lp,
char_u *word,
int len, // word length, -1 for up to NUL
int count) // 1 to count once, 10 to init
{
hash_T hash;
hashitem_T *hi;
wordcount_T *wc;
char_u buf[MAXWLEN];
char_u *p;
if (len == -1)
p = word;
else if (len >= MAXWLEN)
return;
else
{
vim_strncpy(buf, word, len);
p = buf;
}
hash = hash_hash(p);
hi = hash_lookup(&lp->sl_wordcount, p, hash);
if (HASHITEM_EMPTY(hi))
{
wc = alloc(sizeof(wordcount_T) + STRLEN(p));
if (wc == NULL)
return;
STRCPY(wc->wc_word, p);
wc->wc_count = count;
hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
}
else
{
wc = HI2WC(hi);
if ((wc->wc_count += count) < (unsigned)count) // check for overflow
wc->wc_count = MAXWORDCOUNT;
}
}
/*
* Return TRUE if byte "n" appears in "str".
* Like strchr() but independent of locale.
*/
int
byte_in_str(char_u *str, int n)
{
char_u *p;
for (p = str; *p != NUL; ++p)
if (*p == n)
return TRUE;
return FALSE;
}
#define SY_MAXLEN 30
typedef struct syl_item_S
{
char_u sy_chars[SY_MAXLEN]; // the sequence of chars
int sy_len;
} syl_item_T;
/*
* Truncate "slang->sl_syllable" at the first slash and put the following items
* in "slang->sl_syl_items".
*/
int
init_syl_tab(slang_T *slang)
{
char_u *p;
char_u *s;
int l;
syl_item_T *syl;
ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
p = vim_strchr(slang->sl_syllable, '/');
while (p != NULL)
{
*p++ = NUL;
if (*p == NUL) // trailing slash
break;
s = p;
p = vim_strchr(p, '/');
if (p == NULL)
l = (int)STRLEN(s);
else
l = (int)(p - s);
if (l >= SY_MAXLEN)
return SP_FORMERROR;
if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
return SP_OTHERERROR;
syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
+ slang->sl_syl_items.ga_len++;
vim_strncpy(syl->sy_chars, s, l);
syl->sy_len = l;
}
return OK;
}
/*
* Count the number of syllables in "word".
* When "word" contains spaces the syllables after the last space are counted.
* Returns zero if syllables are not defines.
*/
static int
count_syllables(slang_T *slang, char_u *word)
{
int cnt = 0;
int skip = FALSE;
char_u *p;
int len;
int i;
syl_item_T *syl;
int c;
if (slang->sl_syllable == NULL)
return 0;
for (p = word; *p != NUL; p += len)
{
// When running into a space reset counter.
if (*p == ' ')
{
len = 1;
cnt = 0;
continue;
}
// Find longest match of syllable items.
len = 0;
for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
{
syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
if (syl->sy_len > len
&& STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
len = syl->sy_len;
}
if (len != 0) // found a match, count syllable
{
++cnt;
skip = FALSE;
}
else
{
// No recognized syllable item, at least a syllable char then?
c = mb_ptr2char(p);
len = (*mb_ptr2len)(p);
if (vim_strchr(slang->sl_syllable, c) == NULL)
skip = FALSE; // No, search for next syllable
else if (!skip)
{
++cnt; // Yes, count it
skip = TRUE; // don't count following syllable chars
}
}
}
return cnt;
}
/*
* Parse 'spelllang' and set w_s->b_langp accordingly.
* Returns NULL if it's OK, an error message otherwise.
*/
char *
did_set_spelllang(win_T *wp)
{
garray_T ga;
char_u *splp;
char_u *region;
char_u region_cp[3];
int filename;
int region_mask;
slang_T *slang;
int c;
char_u lang[MAXWLEN + 1];
char_u spf_name[MAXPATHL];
int len;
char_u *p;
int round;
char_u *spf;
char_u *use_region = NULL;
int dont_use_region = FALSE;
int nobreak = FALSE;
int i, j;
langp_T *lp, *lp2;
static int recursive = FALSE;
char *ret_msg = NULL;
char_u *spl_copy;
bufref_T bufref;
set_bufref(&bufref, wp->w_buffer);
// We don't want to do this recursively. May happen when a language is
// not available and the SpellFileMissing autocommand opens a new buffer
// in which 'spell' is set.
if (recursive)
return NULL;
recursive = TRUE;
ga_init2(&ga, sizeof(langp_T), 2);
clear_midword(wp);
// Make a copy of 'spelllang', the SpellFileMissing autocommands may change
// it under our fingers.
spl_copy = vim_strsave(wp->w_s->b_p_spl);
if (spl_copy == NULL)
goto theend;
wp->w_s->b_cjk = 0;
// Loop over comma separated language names.
for (splp = spl_copy; *splp != NUL; )
{
// Get one language name.
copy_option_part(&splp, lang, MAXWLEN, ",");
region = NULL;
len = (int)STRLEN(lang);
if (!valid_spelllang(lang))
continue;
if (STRCMP(lang, "cjk") == 0)
{
wp->w_s->b_cjk = 1;
continue;
}
// If the name ends in ".spl" use it as the name of the spell file.
// If there is a region name let "region" point to it and remove it
// from the name.
if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
{
filename = TRUE;
// Locate a region and remove it from the file name.
p = vim_strchr(gettail(lang), '_');
if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
&& !ASCII_ISALPHA(p[3]))
{
vim_strncpy(region_cp, p + 1, 2);
mch_memmove(p, p + 3, len - (p - lang) - 2);
region = region_cp;
}
else
dont_use_region = TRUE;
// Check if we loaded this language before.
FOR_ALL_SPELL_LANGS(slang)
if (fullpathcmp(lang, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
break;
}
else
{
filename = FALSE;
if (len > 3 && lang[len - 3] == '_')
{
region = lang + len - 2;
len -= 3;
lang[len] = NUL;
}
else
dont_use_region = TRUE;
// Check if we loaded this language before.
FOR_ALL_SPELL_LANGS(slang)
if (STRICMP(lang, slang->sl_name) == 0)
break;
}
if (region != NULL)
{
// If the region differs from what was used before then don't
// use it for 'spellfile'.
if (use_region != NULL && STRCMP(region, use_region) != 0)
dont_use_region = TRUE;
use_region = region;
}
// If not found try loading the language now.
if (slang == NULL)
{
if (filename)
(void)spell_load_file(lang, lang, NULL, FALSE);
else
{
spell_load_lang(lang);
// SpellFileMissing autocommands may do anything, including
// destroying the buffer we are using...
if (!bufref_valid(&bufref))
{
ret_msg = N_("E797: SpellFileMissing autocommand deleted buffer");
goto theend;
}
}
}
/*
* Loop over the languages, there can be several files for "lang".
*/
FOR_ALL_SPELL_LANGS(slang)
if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE, TRUE)
== FPC_SAME
: STRICMP(lang, slang->sl_name) == 0)
{
region_mask = REGION_ALL;
if (!filename && region != NULL)
{
// find region in sl_regions
c = find_region(slang->sl_regions, region);
if (c == REGION_ALL)
{
if (slang->sl_add)
{
if (*slang->sl_regions != NUL)
// This addition file is for other regions.
region_mask = 0;
}
else
// This is probably an error. Give a warning and
// accept the words anyway.
smsg(_("Warning: region %s not supported"),
region);
}
else
region_mask = 1 << c;
}
if (region_mask != 0)
{
if (ga_grow(&ga, 1) == FAIL)
{
ga_clear(&ga);
ret_msg = e_out_of_memory;
goto theend;
}
LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
++ga.ga_len;
use_midword(slang, wp);
if (slang->sl_nobreak)
nobreak = TRUE;
}
}
}
// round 0: load int_wordlist, if possible.
// round 1: load first name in 'spellfile'.
// round 2: load second name in 'spellfile.
// etc.
spf = curwin->w_s->b_p_spf;
for (round = 0; round == 0 || *spf != NUL; ++round)
{
if (round == 0)
{
// Internal wordlist, if there is one.
if (int_wordlist == NULL)
continue;
int_wordlist_spl(spf_name);
}
else
{
// One entry in 'spellfile'.
copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
STRCAT(spf_name, ".spl");
// If it was already found above then skip it.
for (c = 0; c < ga.ga_len; ++c)
{
p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
if (p != NULL && fullpathcmp(spf_name, p, FALSE, TRUE)
== FPC_SAME)
break;
}
if (c < ga.ga_len)
continue;
}
// Check if it was loaded already.
FOR_ALL_SPELL_LANGS(slang)
if (fullpathcmp(spf_name, slang->sl_fname, FALSE, TRUE)
== FPC_SAME)
break;
if (slang == NULL)
{
// Not loaded, try loading it now. The language name includes the
// region name, the region is ignored otherwise. for int_wordlist
// use an arbitrary name.
if (round == 0)
STRCPY(lang, "internal wordlist");
else
{
vim_strncpy(lang, gettail(spf_name), MAXWLEN);
p = vim_strchr(lang, '.');
if (p != NULL)
*p = NUL; // truncate at ".encoding.add"
}
slang = spell_load_file(spf_name, lang, NULL, TRUE);
// If one of the languages has NOBREAK we assume the addition
// files also have this.
if (slang != NULL && nobreak)
slang->sl_nobreak = TRUE;
}
if (slang != NULL && ga_grow(&ga, 1) == OK)
{
region_mask = REGION_ALL;
if (use_region != NULL && !dont_use_region)
{
// find region in sl_regions
c = find_region(slang->sl_regions, use_region);
if (c != REGION_ALL)
region_mask = 1 << c;
else if (*slang->sl_regions != NUL)
// This spell file is for other regions.
region_mask = 0;
}
if (region_mask != 0)
{
LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
++ga.ga_len;
use_midword(slang, wp);
}
}
}
// Everything is fine, store the new b_langp value.
ga_clear(&wp->w_s->b_langp);
wp->w_s->b_langp = ga;
// For each language figure out what language to use for sound folding and
// REP items. If the language doesn't support it itself use another one
// with the same name. E.g. for "en-math" use "en".
for (i = 0; i < ga.ga_len; ++i)
{
lp = LANGP_ENTRY(ga, i);
// sound folding
if (lp->lp_slang->sl_sal.ga_len > 0)
// language does sound folding itself
lp->lp_sallang = lp->lp_slang;
else
// find first similar language that does sound folding
for (j = 0; j < ga.ga_len; ++j)
{
lp2 = LANGP_ENTRY(ga, j);
if (lp2->lp_slang->sl_sal.ga_len > 0
&& STRNCMP(lp->lp_slang->sl_name,
lp2->lp_slang->sl_name, 2) == 0)
{
lp->lp_sallang = lp2->lp_slang;
break;
}
}
// REP items
if (lp->lp_slang->sl_rep.ga_len > 0)
// language has REP items itself
lp->lp_replang = lp->lp_slang;
else
// find first similar language that has REP items
for (j = 0; j < ga.ga_len; ++j)
{
lp2 = LANGP_ENTRY(ga, j);
if (lp2->lp_slang->sl_rep.ga_len > 0
&& STRNCMP(lp->lp_slang->sl_name,
lp2->lp_slang->sl_name, 2) == 0)
{
lp->lp_replang = lp2->lp_slang;
break;
}
}
}
redraw_win_later(wp, NOT_VALID);
theend:
vim_free(spl_copy);
recursive = FALSE;
return ret_msg;
}
/*
* Clear the midword characters for buffer "buf".
*/
static void
clear_midword(win_T *wp)
{
CLEAR_FIELD(wp->w_s->b_spell_ismw);
VIM_CLEAR(wp->w_s->b_spell_ismw_mb);
}
/*
* Use the "sl_midword" field of language "lp" for buffer "buf".
* They add up to any currently used midword characters.
*/
static void
use_midword(slang_T *lp, win_T *wp)
{
char_u *p;
if (lp->sl_midword == NULL) // there aren't any
return;
for (p = lp->sl_midword; *p != NUL; )
if (has_mbyte)
{
int c, l, n;
char_u *bp;
c = mb_ptr2char(p);
l = (*mb_ptr2len)(p);
if (c < 256 && l <= 2)
wp->w_s->b_spell_ismw[c] = TRUE;
else if (wp->w_s->b_spell_ismw_mb == NULL)
// First multi-byte char in "b_spell_ismw_mb".
wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
else
{
// Append multi-byte chars to "b_spell_ismw_mb".
n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
if (bp != NULL)
{
vim_free(wp->w_s->b_spell_ismw_mb);
wp->w_s->b_spell_ismw_mb = bp;
vim_strncpy(bp + n, p, l);
}
}
p += l;
}
else
wp->w_s->b_spell_ismw[*p++] = TRUE;
}
/*
* Find the region "region[2]" in "rp" (points to "sl_regions").
* Each region is simply stored as the two characters of its name.
* Returns the index if found (first is 0), REGION_ALL if not found.
*/
static int
find_region(char_u *rp, char_u *region)
{
int i;
for (i = 0; ; i += 2)
{
if (rp[i] == NUL)
return REGION_ALL;
if (rp[i] == region[0] && rp[i + 1] == region[1])
break;
}
return i / 2;
}
/*
* Return case type of word:
* w word 0
* Word WF_ONECAP
* W WORD WF_ALLCAP
* WoRd wOrd WF_KEEPCAP
*/
int
captype(
char_u *word,
char_u *end) // When NULL use up to NUL byte.
{
char_u *p;
int c;
int firstcap;
int allcap;
int past_second = FALSE; // past second word char
// find first letter
for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
if (end == NULL ? *p == NUL : p >= end)
return 0; // only non-word characters, illegal word
if (has_mbyte)
c = mb_ptr2char_adv(&p);
else
c = *p++;
firstcap = allcap = SPELL_ISUPPER(c);
/*
* Need to check all letters to find a word with mixed upper/lower.
* But a word with an upper char only at start is a ONECAP.
*/
for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
if (spell_iswordp_nmw(p, curwin))
{
c = PTR2CHAR(p);
if (!SPELL_ISUPPER(c))
{
// UUl -> KEEPCAP
if (past_second && allcap)
return WF_KEEPCAP;
allcap = FALSE;
}
else if (!allcap)
// UlU -> KEEPCAP
return WF_KEEPCAP;
past_second = TRUE;
}
if (allcap)
return WF_ALLCAP;
if (firstcap)
return WF_ONECAP;
return 0;
}
/*
* Delete the internal wordlist and its .spl file.
*/
void
spell_delete_wordlist(void)
{
char_u fname[MAXPATHL];
if (int_wordlist != NULL)
{
mch_remove(int_wordlist);
int_wordlist_spl(fname);
mch_remove(fname);
VIM_CLEAR(int_wordlist);
}
}
/*
* Free all languages.
*/
void
spell_free_all(void)
{
slang_T *slang;
buf_T *buf;
// Go through all buffers and handle 'spelllang'. <VN>
FOR_ALL_BUFFERS(buf)
ga_clear(&buf->b_s.b_langp);
while (first_lang != NULL)
{
slang = first_lang;
first_lang = slang->sl_next;
slang_free(slang);
}
spell_delete_wordlist();
VIM_CLEAR(repl_to);
VIM_CLEAR(repl_from);
}
/*
* Clear all spelling tables and reload them.
* Used after 'encoding' is set and when ":mkspell" was used.
*/
void
spell_reload(void)
{
win_T *wp;
// Initialize the table for spell_iswordp().
init_spell_chartab();
// Unload all allocated memory.
spell_free_all();
// Go through all buffers and handle 'spelllang'.
FOR_ALL_WINDOWS(wp)
{
// Only load the wordlists when 'spelllang' is set and there is a
// window for this buffer in which 'spell' is set.
if (*wp->w_s->b_p_spl != NUL)
{
if (wp->w_p_spell)
{
(void)did_set_spelllang(wp);
break;
}
}
}
}
/*
* Open a spell buffer. This is a nameless buffer that is not in the buffer
* list and only contains text lines. Can use a swapfile to reduce memory
* use.
* Most other fields are invalid! Esp. watch out for string options being
* NULL and there is no undo info.
* Returns NULL when out of memory.
*/
buf_T *
open_spellbuf(void)
{
buf_T *buf;
buf = ALLOC_CLEAR_ONE(buf_T);
if (buf != NULL)
{
buf->b_spell = TRUE;
buf->b_p_swf = TRUE; // may create a swap file
#ifdef FEAT_CRYPT
buf->b_p_key = empty_option;
#endif
ml_open(buf);
ml_open_file(buf); // create swap file now
}
return buf;
}
/*
* Close the buffer used for spell info.
*/
void
close_spellbuf(buf_T *buf)
{
if (buf != NULL)
{
ml_close(buf, TRUE);
vim_free(buf);
}
}
/*
* Init the chartab used for spelling for ASCII.
* EBCDIC is not supported!
*/
void
clear_spell_chartab(spelltab_T *sp)
{
int i;
// Init everything to FALSE (zero).
CLEAR_FIELD(sp->st_isw);
CLEAR_FIELD(sp->st_isu);
for (i = 0; i < 256; ++i)
{
sp->st_fold[i] = i;
sp->st_upper[i] = i;
}
// We include digits. A word shouldn't start with a digit, but handling
// that is done separately.
for (i = '0'; i <= '9'; ++i)
sp->st_isw[i] = TRUE;
for (i = 'A'; i <= 'Z'; ++i)
{
sp->st_isw[i] = TRUE;
sp->st_isu[i] = TRUE;
sp->st_fold[i] = i + 0x20;
}
for (i = 'a'; i <= 'z'; ++i)
{
sp->st_isw[i] = TRUE;
sp->st_upper[i] = i - 0x20;
}
}
/*
* Init the chartab used for spelling. Only depends on 'encoding'.
* Called once while starting up and when 'encoding' changes.
* The default is to use isalpha(), but the spell file should define the word
* characters to make it possible that 'encoding' differs from the current
* locale. For utf-8 we don't use isalpha() but our own functions.
*/
void
init_spell_chartab(void)
{
int i;
did_set_spelltab = FALSE;
clear_spell_chartab(&spelltab);
if (enc_dbcs)
{
// DBCS: assume double-wide characters are word characters.
for (i = 128; i <= 255; ++i)
if (MB_BYTE2LEN(i) == 2)
spelltab.st_isw[i] = TRUE;
}
else if (enc_utf8)
{
for (i = 128; i < 256; ++i)
{
int f = utf_fold(i);
int u = utf_toupper(i);
spelltab.st_isu[i] = utf_isupper(i);
spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
// The folded/upper-cased value is different between latin1 and
// utf8 for 0xb5, causing E763 for no good reason. Use the latin1
// value for utf-8 to avoid this.
spelltab.st_fold[i] = (f < 256) ? f : i;
spelltab.st_upper[i] = (u < 256) ? u : i;
}
}
else
{
// Rough guess: use locale-dependent library functions.
for (i = 128; i < 256; ++i)
{
if (MB_ISUPPER(i))
{
spelltab.st_isw[i] = TRUE;
spelltab.st_isu[i] = TRUE;
spelltab.st_fold[i] = MB_TOLOWER(i);
}
else if (MB_ISLOWER(i))
{
spelltab.st_isw[i] = TRUE;
spelltab.st_upper[i] = MB_TOUPPER(i);
}
}
}
}
/*
* Return TRUE if "p" points to a word character.
* As a special case we see "midword" characters as word character when it is
* followed by a word character. This finds they'there but not 'they there'.
* Thus this only works properly when past the first character of the word.
*/
int
spell_iswordp(
char_u *p,
win_T *wp) // buffer used
{
char_u *s;
int l;
int c;
if (has_mbyte)
{
l = mb_ptr2len(p);
s = p;
if (l == 1)
{
// be quick for ASCII
if (wp->w_s->b_spell_ismw[*p])
s = p + 1; // skip a mid-word character
}
else
{
c = mb_ptr2char(p);
if (c < 256 ? wp->w_s->b_spell_ismw[c]
: (wp->w_s->b_spell_ismw_mb != NULL
&& vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
s = p + l;
}
c = mb_ptr2char(s);
if (c > 255)
return spell_mb_isword_class(mb_get_class(s), wp);
return spelltab.st_isw[c];
}
return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
}
/*
* Return TRUE if "p" points to a word character.
* Unlike spell_iswordp() this doesn't check for "midword" characters.
*/
int
spell_iswordp_nmw(char_u *p, win_T *wp)
{
int c;
if (has_mbyte)
{
c = mb_ptr2char(p);
if (c > 255)
return spell_mb_isword_class(mb_get_class(p), wp);
return spelltab.st_isw[c];
}
return spelltab.st_isw[*p];
}
/*
* Return TRUE if word class indicates a word character.
* Only for characters above 255.
* Unicode subscript and superscript are not considered word characters.
* See also dbcs_class() and utf_class() in mbyte.c.
*/
static int
spell_mb_isword_class(int cl, win_T *wp)
{
if (wp->w_s->b_cjk)
// East Asian characters are not considered word characters.
return cl == 2 || cl == 0x2800;
return cl >= 2 && cl != 0x2070 && cl != 0x2080 && cl != 3;
}
/*
* Return TRUE if "p" points to a word character.
* Wide version of spell_iswordp().
*/
static int
spell_iswordp_w(int *p, win_T *wp)
{
int *s;
if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
: (wp->w_s->b_spell_ismw_mb != NULL
&& vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
s = p + 1;
else
s = p;
if (*s > 255)
{
if (enc_utf8)
return spell_mb_isword_class(utf_class(*s), wp);
if (enc_dbcs)
return spell_mb_isword_class(
dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
return 0;
}
return spelltab.st_isw[*s];
}
/*
* Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
* Uses the character definitions from the .spl file.
* When using a multi-byte 'encoding' the length may change!
* Returns FAIL when something wrong.
*/
int
spell_casefold(
win_T *wp,
char_u *str,
int len,
char_u *buf,
int buflen)
{
int i;
if (len >= buflen)
{
buf[0] = NUL;
return FAIL; // result will not fit
}
if (has_mbyte)
{
int outi = 0;
char_u *p;
int c;
// Fold one character at a time.
for (p = str; p < str + len; )
{
if (outi + MB_MAXBYTES > buflen)
{
buf[outi] = NUL;
return FAIL;
}
c = mb_cptr2char_adv(&p);
// Exception: greek capital sigma 0x03A3 folds to 0x03C3, except
// when it is the last character in a word, then it folds to
// 0x03C2.
if (c == 0x03a3 || c == 0x03c2)
{
if (p == str + len || !spell_iswordp(p, wp))
c = 0x03c2;
else
c = 0x03c3;
}
else
c = SPELL_TOFOLD(c);
outi += mb_char2bytes(c, buf + outi);
}
buf[outi] = NUL;
}
else
{
// Be quick for non-multibyte encodings.
for (i = 0; i < len; ++i)
buf[i] = spelltab.st_fold[str[i]];
buf[i] = NUL;
}
return OK;
}
/*
* Check if the word at line "lnum" column "col" is required to start with a
* capital. This uses 'spellcapcheck' of the current buffer.
*/
int
check_need_cap(linenr_T lnum, colnr_T col)
{
int need_cap = FALSE;
char_u *line;
char_u *line_copy = NULL;
char_u *p;
colnr_T endcol;
regmatch_T regmatch;
if (curwin->w_s->b_cap_prog == NULL)
return FALSE;
line = ml_get_curline();
endcol = 0;
if (getwhitecols(line) >= (int)col)
{
// At start of line, check if previous line is empty or sentence
// ends there.
if (lnum == 1)
need_cap = TRUE;
else
{
line = ml_get(lnum - 1);
if (*skipwhite(line) == NUL)
need_cap = TRUE;
else
{
// Append a space in place of the line break.
line_copy = concat_str(line, (char_u *)" ");
line = line_copy;
endcol = (colnr_T)STRLEN(line);
}
}
}
else
endcol = col;
if (endcol > 0)
{
// Check if sentence ends before the bad word.
regmatch.regprog = curwin->w_s->b_cap_prog;
regmatch.rm_ic = FALSE;
p = line + endcol;
for (;;)
{
MB_PTR_BACK(line, p);
if (p == line || spell_iswordp_nmw(p, curwin))
break;
if (vim_regexec(®match, p, 0)
&& regmatch.endp[0] == line + endcol)
{
need_cap = TRUE;
break;
}
}
curwin->w_s->b_cap_prog = regmatch.regprog;
}
vim_free(line_copy);
return need_cap;
}
/*
* ":spellrepall"
*/
void
ex_spellrepall(exarg_T *eap UNUSED)
{
pos_T pos = curwin->w_cursor;
char_u *frompat;
int addlen;
char_u *line;
char_u *p;
int save_ws = p_ws;
linenr_T prev_lnum = 0;
if (repl_from == NULL || repl_to == NULL)
{
emsg(_("E752: No previous spell replacement"));
return;
}
addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
frompat = alloc(STRLEN(repl_from) + 7);
if (frompat == NULL)
return;
sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
p_ws = FALSE;
sub_nsubs = 0;
sub_nlines = 0;
curwin->w_cursor.lnum = 0;
while (!got_int)
{
if (do_search(NULL, '/', '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
|| u_save_cursor() == FAIL)
break;
// Only replace when the right word isn't there yet. This happens
// when changing "etc" to "etc.".
line = ml_get_curline();
if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
repl_to, STRLEN(repl_to)) != 0)
{
p = alloc(STRLEN(line) + addlen + 1);
if (p == NULL)
break;
mch_memmove(p, line, curwin->w_cursor.col);
STRCPY(p + curwin->w_cursor.col, repl_to);
STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
ml_replace(curwin->w_cursor.lnum, p, FALSE);
changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
if (curwin->w_cursor.lnum != prev_lnum)
{
++sub_nlines;
prev_lnum = curwin->w_cursor.lnum;
}
++sub_nsubs;
}
curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
}
p_ws = save_ws;
curwin->w_cursor = pos;
vim_free(frompat);
if (sub_nsubs == 0)
semsg(_("E753: Not found: %s"), repl_from);
else
do_sub_msg(FALSE);
}
/*
* Make a copy of "word", with the first letter upper or lower cased, to
* "wcopy[MAXWLEN]". "word" must not be empty.
* The result is NUL terminated.
*/
void
onecap_copy(
char_u *word,
char_u *wcopy,
int upper) // TRUE: first letter made upper case
{
char_u *p;
int c;
int l;
p = word;
if (has_mbyte)
c = mb_cptr2char_adv(&p);
else
c = *p++;
if (upper)
c = SPELL_TOUPPER(c);
else
c = SPELL_TOFOLD(c);
if (has_mbyte)
l = mb_char2bytes(c, wcopy);
else
{
l = 1;
wcopy[0] = c;
}
vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
}
/*
* Make a copy of "word" with all the letters upper cased into
* "wcopy[MAXWLEN]". The result is NUL terminated.
*/
void
allcap_copy(char_u *word, char_u *wcopy)
{
char_u *s;
char_u *d;
int c;
d = wcopy;
for (s = word; *s != NUL; )
{
if (has_mbyte)
c = mb_cptr2char_adv(&s);
else
c = *s++;
// We only change 0xdf to SS when we are certain latin1 is used. It
// would cause weird errors in other 8-bit encodings.
if (enc_latin1like && c == 0xdf)
{
c = 'S';
if (d - wcopy >= MAXWLEN - 1)
break;
*d++ = c;
}
else
c = SPELL_TOUPPER(c);
if (has_mbyte)
{
if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
break;
d += mb_char2bytes(c, d);
}
else
{
if (d - wcopy >= MAXWLEN - 1)
break;
*d++ = c;
}
}
*d = NUL;
}
/*
* Case-folding may change the number of bytes: Count nr of chars in
* fword[flen] and return the byte length of that many chars in "word".
*/
int
nofold_len(char_u *fword, int flen, char_u *word)
{
char_u *p;
int i = 0;
for (p = fword; p < fword + flen; MB_PTR_ADV(p))
++i;
for (p = word; i > 0; MB_PTR_ADV(p))
--i;
return (int)(p - word);
}
/*
* Copy "fword" to "cword", fixing case according to "flags".
*/
void
make_case_word(char_u *fword, char_u *cword, int flags)
{
if (flags & WF_ALLCAP)
// Make it all upper-case
allcap_copy(fword, cword);
else if (flags & WF_ONECAP)
// Make the first letter upper-case
onecap_copy(fword, cword, TRUE);
else
// Use goodword as-is.
STRCPY(cword, fword);
}
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Soundfold a string, for soundfold().
* Result is in allocated memory, NULL for an error.
*/
char_u *
eval_soundfold(char_u *word)
{
langp_T *lp;
char_u sound[MAXWLEN];
int lpi;
if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
// Use the sound-folding of the first language that supports it.
for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
{
lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
if (lp->lp_slang->sl_sal.ga_len > 0)
{
// soundfold the word
spell_soundfold(lp->lp_slang, word, FALSE, sound);
return vim_strsave(sound);
}
}
// No language with sound folding, return word as-is.
return vim_strsave(word);
}
#endif
/*
* Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
*
* There are many ways to turn a word into a sound-a-like representation. The
* oldest is Soundex (1918!). A nice overview can be found in "Approximate
* swedish name matching - survey and test of different algorithms" by Klas
* Erikson.
*
* We support two methods:
* 1. SOFOFROM/SOFOTO do a simple character mapping.
* 2. SAL items define a more advanced sound-folding (and much slower).
*/
void
spell_soundfold(
slang_T *slang,
char_u *inword,
int folded, // "inword" is already case-folded
char_u *res)
{
char_u fword[MAXWLEN];
char_u *word;
if (slang->sl_sofo)
// SOFOFROM and SOFOTO used
spell_soundfold_sofo(slang, inword, res);
else
{
// SAL items used. Requires the word to be case-folded.
if (folded)
word = inword;
else
{
(void)spell_casefold(curwin,
inword, (int)STRLEN(inword), fword, MAXWLEN);
word = fword;
}
if (has_mbyte)
spell_soundfold_wsal(slang, word, res);
else
spell_soundfold_sal(slang, word, res);
}
}
/*
* Perform sound folding of "inword" into "res" according to SOFOFROM and
* SOFOTO lines.
*/
static void
spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
{
char_u *s;
int ri = 0;
int c;
if (has_mbyte)
{
int prevc = 0;
int *ip;
// The sl_sal_first[] table contains the translation for chars up to
// 255, sl_sal the rest.
for (s = inword; *s != NUL; )
{
c = mb_cptr2char_adv(&s);
if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
c = ' ';
else if (c < 256)
c = slang->sl_sal_first[c];
else
{
ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
if (ip == NULL) // empty list, can't match
c = NUL;
else
for (;;) // find "c" in the list
{
if (*ip == 0) // not found
{
c = NUL;
break;
}
if (*ip == c) // match!
{
c = ip[1];
break;
}
ip += 2;
}
}
if (c != NUL && c != prevc)
{
ri += mb_char2bytes(c, res + ri);
if (ri + MB_MAXBYTES > MAXWLEN)
break;
prevc = c;
}
}
}
else
{
// The sl_sal_first[] table contains the translation.
for (s = inword; (c = *s) != NUL; ++s)
{
if (VIM_ISWHITE(c))
c = ' ';
else
c = slang->sl_sal_first[c];
if (c != NUL && (ri == 0 || res[ri - 1] != c))
res[ri++] = c;
}
}
res[ri] = NUL;
}
static void
spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
{
salitem_T *smp;
char_u word[MAXWLEN];
char_u *s = inword;
char_u *t;
char_u *pf;
int i, j, z;
int reslen;
int n, k = 0;
int z0;
int k0;
int n0;
int c;
int pri;
int p0 = -333;
int c0;
// Remove accents, if wanted. We actually remove all non-word characters.
// But keep white space. We need a copy, the word may be changed here.
if (slang->sl_rem_accents)
{
t = word;
while (*s != NUL)
{
if (VIM_ISWHITE(*s))
{
*t++ = ' ';
s = skipwhite(s);
}
else
{
if (spell_iswordp_nmw(s, curwin))
*t++ = *s;
++s;
}
}
*t = NUL;
}
else
vim_strncpy(word, s, MAXWLEN - 1);
smp = (salitem_T *)slang->sl_sal.ga_data;
/*
* This comes from Aspell phonet.cpp. Converted from C++ to C.
* Changed to keep spaces.
*/
i = reslen = z = 0;
while ((c = word[i]) != NUL)
{
// Start with the first rule that has the character in the word.
n = slang->sl_sal_first[c];
z0 = 0;
if (n >= 0)
{
// check all rules for the same letter
for (; (s = smp[n].sm_lead)[0] == c; ++n)
{
// Quickly skip entries that don't match the word. Most
// entries are less than three chars, optimize for that.
k = smp[n].sm_leadlen;
if (k > 1)
{
if (word[i + 1] != s[1])
continue;
if (k > 2)
{
for (j = 2; j < k; ++j)
if (word[i + j] != s[j])
break;
if (j < k)
continue;
}
}
if ((pf = smp[n].sm_oneof) != NULL)
{
// Check for match with one of the chars in "sm_oneof".
while (*pf != NUL && *pf != word[i + k])
++pf;
if (*pf == NUL)
continue;
++k;
}
s = smp[n].sm_rules;
pri = 5; // default priority
p0 = *s;
k0 = k;
while (*s == '-' && k > 1)
{
k--;
s++;
}
if (*s == '<')
s++;
if (VIM_ISDIGIT(*s))
{
// determine priority
pri = *s - '0';
s++;
}
if (*s == '^' && *(s + 1) == '^')
s++;
if (*s == NUL
|| (*s == '^'
&& (i == 0 || !(word[i - 1] == ' '
|| spell_iswordp(word + i - 1, curwin)))
&& (*(s + 1) != '$'
|| (!spell_iswordp(word + i + k0, curwin))))
|| (*s == '$' && i > 0
&& spell_iswordp(word + i - 1, curwin)
&& (!spell_iswordp(word + i + k0, curwin))))
{
// search for followup rules, if:
// followup and k > 1 and NO '-' in searchstring
c0 = word[i + k - 1];
n0 = slang->sl_sal_first[c0];
if (slang->sl_followup && k > 1 && n0 >= 0
&& p0 != '-' && word[i + k] != NUL)
{
// test follow-up rule for "word[i + k]"
for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
{
// Quickly skip entries that don't match the word.
//
k0 = smp[n0].sm_leadlen;
if (k0 > 1)
{
if (word[i + k] != s[1])
continue;
if (k0 > 2)
{
pf = word + i + k + 1;
for (j = 2; j < k0; ++j)
if (*pf++ != s[j])
break;
if (j < k0)
continue;
}
}
k0 += k - 1;
if ((pf = smp[n0].sm_oneof) != NULL)
{
// Check for match with one of the chars in
// "sm_oneof".
while (*pf != NUL && *pf != word[i + k0])
++pf;
if (*pf == NUL)
continue;
++k0;
}
p0 = 5;
s = smp[n0].sm_rules;
while (*s == '-')
{
// "k0" gets NOT reduced because
// "if (k0 == k)"
s++;
}
if (*s == '<')
s++;
if (VIM_ISDIGIT(*s))
{
p0 = *s - '0';
s++;
}
if (*s == NUL
// *s == '^' cuts
|| (*s == '$'
&& !spell_iswordp(word + i + k0,
curwin)))
{
if (k0 == k)
// this is just a piece of the string
continue;
if (p0 < pri)
// priority too low
continue;
// rule fits; stop search
break;
}
}
if (p0 >= pri && smp[n0].sm_lead[0] == c0)
continue;
}
// replace string
s = smp[n].sm_to;
if (s == NULL)
s = (char_u *)"";
pf = smp[n].sm_rules;
p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
if (p0 == 1 && z == 0)
{
// rule with '<' is used
if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
|| res[reslen - 1] == *s))
reslen--;
z0 = 1;
z = 1;
k0 = 0;
while (*s != NUL && word[i + k0] != NUL)
{
word[i + k0] = *s;
k0++;
s++;
}
if (k > k0)
STRMOVE(word + i + k0, word + i + k);
// new "actual letter"
c = word[i];
}
else
{
// no '<' rule used
i += k - 1;
z = 0;
while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
{
if (reslen == 0 || res[reslen - 1] != *s)
res[reslen++] = *s;
s++;
}
// new "actual letter"
c = *s;
if (strstr((char *)pf, "^^") != NULL)
{
if (c != NUL)
res[reslen++] = c;
STRMOVE(word, word + i + 1);
i = 0;
z0 = 1;
}
}
break;
}
}
}
else if (VIM_ISWHITE(c))
{
c = ' ';
k = 1;
}
if (z0 == 0)
{
if (k && !p0 && reslen < MAXWLEN && c != NUL
&& (!slang->sl_collapse || reslen == 0
|| res[reslen - 1] != c))
// condense only double letters
res[reslen++] = c;
i++;
z = 0;
k = 0;
}
}
res[reslen] = NUL;
}
/*
* Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
* Multi-byte version of spell_soundfold().
*/
static void
spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
{
salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
int word[MAXWLEN];
int wres[MAXWLEN];
int l;
char_u *s;
int *ws;
char_u *t;
int *pf;
int i, j, z;
int reslen;
int n, k = 0;
int z0;
int k0;
int n0;
int c;
int pri;
int p0 = -333;
int c0;
int did_white = FALSE;
int wordlen;
/*
* Convert the multi-byte string to a wide-character string.
* Remove accents, if wanted. We actually remove all non-word characters.
* But keep white space.
*/
wordlen = 0;
for (s = inword; *s != NUL; )
{
t = s;
c = mb_cptr2char_adv(&s);
if (slang->sl_rem_accents)
{
if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
{
if (did_white)
continue;
c = ' ';
did_white = TRUE;
}
else
{
did_white = FALSE;
if (!spell_iswordp_nmw(t, curwin))
continue;
}
}
word[wordlen++] = c;
}
word[wordlen] = NUL;
/*
* This algorithm comes from Aspell phonet.cpp.
* Converted from C++ to C. Added support for multi-byte chars.
* Changed to keep spaces.
*/
i = reslen = z = 0;
while ((c = word[i]) != NUL)
{
// Start with the first rule that has the character in the word.
n = slang->sl_sal_first[c & 0xff];
z0 = 0;
if (n >= 0)
{
// Check all rules for the same index byte.
// If c is 0x300 need extra check for the end of the array, as
// (c & 0xff) is NUL.
for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
&& ws[0] != NUL; ++n)
{
// Quickly skip entries that don't match the word. Most
// entries are less than three chars, optimize for that.
if (c != ws[0])
continue;
k = smp[n].sm_leadlen;
if (k > 1)
{
if (word[i + 1] != ws[1])
continue;
if (k > 2)
{
for (j = 2; j < k; ++j)
if (word[i + j] != ws[j])
break;
if (j < k)
continue;
}
}
if ((pf = smp[n].sm_oneof_w) != NULL)
{
// Check for match with one of the chars in "sm_oneof".
while (*pf != NUL && *pf != word[i + k])
++pf;
if (*pf == NUL)
continue;
++k;
}
s = smp[n].sm_rules;
pri = 5; // default priority
p0 = *s;
k0 = k;
while (*s == '-' && k > 1)
{
k--;
s++;
}
if (*s == '<')
s++;
if (VIM_ISDIGIT(*s))
{
// determine priority
pri = *s - '0';
s++;
}
if (*s == '^' && *(s + 1) == '^')
s++;
if (*s == NUL
|| (*s == '^'
&& (i == 0 || !(word[i - 1] == ' '
|| spell_iswordp_w(word + i - 1, curwin)))
&& (*(s + 1) != '$'
|| (!spell_iswordp_w(word + i + k0, curwin))))
|| (*s == '$' && i > 0
&& spell_iswordp_w(word + i - 1, curwin)
&& (!spell_iswordp_w(word + i + k0, curwin))))
{
// search for followup rules, if:
// followup and k > 1 and NO '-' in searchstring
c0 = word[i + k - 1];
n0 = slang->sl_sal_first[c0 & 0xff];
if (slang->sl_followup && k > 1 && n0 >= 0
&& p0 != '-' && word[i + k] != NUL)
{
// Test follow-up rule for "word[i + k]"; loop over
// all entries with the same index byte.
for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
== (c0 & 0xff); ++n0)
{
// Quickly skip entries that don't match the word.
if (c0 != ws[0])
continue;
k0 = smp[n0].sm_leadlen;
if (k0 > 1)
{
if (word[i + k] != ws[1])
continue;
if (k0 > 2)
{
pf = word + i + k + 1;
for (j = 2; j < k0; ++j)
if (*pf++ != ws[j])
break;
if (j < k0)
continue;
}
}
k0 += k - 1;
if ((pf = smp[n0].sm_oneof_w) != NULL)
{
// Check for match with one of the chars in
// "sm_oneof".
while (*pf != NUL && *pf != word[i + k0])
++pf;
if (*pf == NUL)
continue;
++k0;
}
p0 = 5;
s = smp[n0].sm_rules;
while (*s == '-')
{
// "k0" gets NOT reduced because
// "if (k0 == k)"
s++;
}
if (*s == '<')
s++;
if (VIM_ISDIGIT(*s))
{
p0 = *s - '0';
s++;
}
if (*s == NUL
// *s == '^' cuts
|| (*s == '$'
&& !spell_iswordp_w(word + i + k0,
curwin)))
{
if (k0 == k)
// this is just a piece of the string
continue;
if (p0 < pri)
// priority too low
continue;
// rule fits; stop search
break;
}
}
if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
== (c0 & 0xff))
continue;
}
// replace string
ws = smp[n].sm_to_w;
s = smp[n].sm_rules;
p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
if (p0 == 1 && z == 0)
{
// rule with '<' is used
if (reslen > 0 && ws != NULL && *ws != NUL
&& (wres[reslen - 1] == c
|| wres[reslen - 1] == *ws))
reslen--;
z0 = 1;
z = 1;
k0 = 0;
if (ws != NULL)
while (*ws != NUL && word[i + k0] != NUL)
{
word[i + k0] = *ws;
k0++;
ws++;
}
if (k > k0)
mch_memmove(word + i + k0, word + i + k,
sizeof(int) * (wordlen - (i + k) + 1));
// new "actual letter"
c = word[i];
}
else
{
// no '<' rule used
i += k - 1;
z = 0;
if (ws != NULL)
while (*ws != NUL && ws[1] != NUL
&& reslen < MAXWLEN)
{
if (reslen == 0 || wres[reslen - 1] != *ws)
wres[reslen++] = *ws;
ws++;
}
// new "actual letter"
if (ws == NULL)
c = NUL;
else
c = *ws;
if (strstr((char *)s, "^^") != NULL)
{
if (c != NUL)
wres[reslen++] = c;
mch_memmove(word, word + i + 1,
sizeof(int) * (wordlen - (i + 1) + 1));
i = 0;
z0 = 1;
}
}
break;
}
}
}
else if (VIM_ISWHITE(c))
{
c = ' ';
k = 1;
}
if (z0 == 0)
{
if (k && !p0 && reslen < MAXWLEN && c != NUL
&& (!slang->sl_collapse || reslen == 0
|| wres[reslen - 1] != c))
// condense only double letters
wres[reslen++] = c;
i++;
z = 0;
k = 0;
}
}
// Convert wide characters in "wres" to a multi-byte string in "res".
l = 0;
for (n = 0; n < reslen; ++n)
{
l += mb_char2bytes(wres[n], res + l);
if (l + MB_MAXBYTES > MAXWLEN)
break;
}
res[l] = NUL;
}
/*
* ":spellinfo"
*/
void
ex_spellinfo(exarg_T *eap UNUSED)
{
int lpi;
langp_T *lp;
char_u *p;
if (no_spell_checking(curwin))
return;
msg_start();
for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
{
lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
msg_puts("file: ");
msg_puts((char *)lp->lp_slang->sl_fname);
msg_putchar('\n');
p = lp->lp_slang->sl_info;
if (p != NULL)
{
msg_puts((char *)p);
msg_putchar('\n');
}
}
msg_end();
}
#define DUMPFLAG_KEEPCASE 1 // round 2: keep-case tree
#define DUMPFLAG_COUNT 2 // include word count
#define DUMPFLAG_ICASE 4 // ignore case when finding matches
#define DUMPFLAG_ONECAP 8 // pattern starts with capital
#define DUMPFLAG_ALLCAP 16 // pattern is all capitals
/*
* ":spelldump"
*/
void
ex_spelldump(exarg_T *eap)
{
char_u *spl;
long dummy;
if (no_spell_checking(curwin))
return;
(void)get_option_value((char_u*)"spl", &dummy, &spl, NULL, OPT_LOCAL);
// Create a new empty buffer in a new window.
do_cmdline_cmd((char_u *)"new");
// enable spelling locally in the new window
set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
vim_free(spl);
if (!BUFEMPTY())
return;
spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
// Delete the empty line that we started with.
if (curbuf->b_ml.ml_line_count > 1)
ml_delete(curbuf->b_ml.ml_line_count);
redraw_later(NOT_VALID);
}
/*
* Go through all possible words and:
* 1. When "pat" is NULL: dump a list of all words in the current buffer.
* "ic" and "dir" are not used.
* 2. When "pat" is not NULL: add matching words to insert mode completion.
*/
void
spell_dump_compl(
char_u *pat, // leading part of the word
int ic, // ignore case
int *dir, // direction for adding matches
int dumpflags_arg) // DUMPFLAG_*
{
langp_T *lp;
slang_T *slang;
idx_T arridx[MAXWLEN];
int curi[MAXWLEN];
char_u word[MAXWLEN];
int c;
char_u *byts;
idx_T *idxs;
linenr_T lnum = 0;
int round;
int depth;
int n;
int flags;
char_u *region_names = NULL; // region names being used
int do_region = TRUE; // dump region names and numbers
char_u *p;
int lpi;
int dumpflags = dumpflags_arg;
int patlen;
// When ignoring case or when the pattern starts with capital pass this on
// to dump_word().
if (pat != NULL)
{
if (ic)
dumpflags |= DUMPFLAG_ICASE;
else
{
n = captype(pat, NULL);
if (n == WF_ONECAP)
dumpflags |= DUMPFLAG_ONECAP;
else if (n == WF_ALLCAP && (int)STRLEN(pat) > mb_ptr2len(pat))
dumpflags |= DUMPFLAG_ALLCAP;
}
}
// Find out if we can support regions: All languages must support the same
// regions or none at all.
for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
{
lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
p = lp->lp_slang->sl_regions;
if (p[0] != 0)
{
if (region_names == NULL) // first language with regions
region_names = p;
else if (STRCMP(region_names, p) != 0)
{
do_region = FALSE; // region names are different
break;
}
}
}
if (do_region && region_names != NULL)
{
if (pat == NULL)
{
vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
}
}
else
do_region = FALSE;
/*
* Loop over all files loaded for the entries in 'spelllang'.
*/
for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
{
lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
slang = lp->lp_slang;
if (slang->sl_fbyts == NULL) // reloading failed
continue;
if (pat == NULL)
{
vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
}
// When matching with a pattern and there are no prefixes only use
// parts of the tree that match "pat".
if (pat != NULL && slang->sl_pbyts == NULL)
patlen = (int)STRLEN(pat);
else
patlen = -1;
// round 1: case-folded tree
// round 2: keep-case tree
for (round = 1; round <= 2; ++round)
{
if (round == 1)
{
dumpflags &= ~DUMPFLAG_KEEPCASE;
byts = slang->sl_fbyts;
idxs = slang->sl_fidxs;
}
else
{
dumpflags |= DUMPFLAG_KEEPCASE;
byts = slang->sl_kbyts;
idxs = slang->sl_kidxs;
}
if (byts == NULL)
continue; // array is empty
depth = 0;
arridx[0] = 0;
curi[0] = 1;
while (depth >= 0 && !got_int
&& (pat == NULL || !ins_compl_interrupted()))
{
if (curi[depth] > byts[arridx[depth]])
{
// Done all bytes at this node, go up one level.
--depth;
line_breakcheck();
ins_compl_check_keys(50, FALSE);
}
else
{
// Do one more byte at this node.
n = arridx[depth] + curi[depth];
++curi[depth];
c = byts[n];
if (c == 0)
{
// End of word, deal with the word.
// Don't use keep-case words in the fold-case tree,
// they will appear in the keep-case tree.
// Only use the word when the region matches.
flags = (int)idxs[n];
if ((round == 2 || (flags & WF_KEEPCAP) == 0)
&& (flags & WF_NEEDCOMP) == 0
&& (do_region
|| (flags & WF_REGION) == 0
|| (((unsigned)flags >> 16)
& lp->lp_region) != 0))
{
word[depth] = NUL;
if (!do_region)
flags &= ~WF_REGION;
// Dump the basic word if there is no prefix or
// when it's the first one.
c = (unsigned)flags >> 24;
if (c == 0 || curi[depth] == 2)
{
dump_word(slang, word, pat, dir,
dumpflags, flags, lnum);
if (pat == NULL)
++lnum;
}
// Apply the prefix, if there is one.
if (c != 0)
lnum = dump_prefixes(slang, word, pat, dir,
dumpflags, flags, lnum);
}
}
else
{
// Normal char, go one level deeper.
word[depth++] = c;
arridx[depth] = idxs[n];
curi[depth] = 1;
// Check if this character matches with the pattern.
// If not skip the whole tree below it.
// Always ignore case here, dump_word() will check
// proper case later. This isn't exactly right when
// length changes for multi-byte characters with
// ignore case...
if (depth <= patlen
&& MB_STRNICMP(word, pat, depth) != 0)
--depth;
}
}
}
}
}
}
/*
* Dump one word: apply case modifications and append a line to the buffer.
* When "lnum" is zero add insert mode completion.
*/
static void
dump_word(
slang_T *slang,
char_u *word,
char_u *pat,
int *dir,
int dumpflags,
int wordflags,
linenr_T lnum)
{
int keepcap = FALSE;
char_u *p;
char_u *tw;
char_u cword[MAXWLEN];
char_u badword[MAXWLEN + 10];
int i;
int flags = wordflags;
if (dumpflags & DUMPFLAG_ONECAP)
flags |= WF_ONECAP;
if (dumpflags & DUMPFLAG_ALLCAP)
flags |= WF_ALLCAP;
if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
{
// Need to fix case according to "flags".
make_case_word(word, cword, flags);
p = cword;
}
else
{
p = word;
if ((dumpflags & DUMPFLAG_KEEPCASE)
&& ((captype(word, NULL) & WF_KEEPCAP) == 0
|| (flags & WF_FIXCAP) != 0))
keepcap = TRUE;
}
tw = p;
if (pat == NULL)
{
// Add flags and regions after a slash.
if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
{
STRCPY(badword, p);
STRCAT(badword, "/");
if (keepcap)
STRCAT(badword, "=");
if (flags & WF_BANNED)
STRCAT(badword, "!");
else if (flags & WF_RARE)
STRCAT(badword, "?");
if (flags & WF_REGION)
for (i = 0; i < 7; ++i)
if (flags & (0x10000 << i))
sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
p = badword;
}
if (dumpflags & DUMPFLAG_COUNT)
{
hashitem_T *hi;
// Include the word count for ":spelldump!".
hi = hash_find(&slang->sl_wordcount, tw);
if (!HASHITEM_EMPTY(hi))
{
vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
tw, HI2WC(hi)->wc_count);
p = IObuff;
}
}
ml_append(lnum, p, (colnr_T)0, FALSE);
}
else if (((dumpflags & DUMPFLAG_ICASE)
? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
: STRNCMP(p, pat, STRLEN(pat)) == 0)
&& ins_compl_add_infercase(p, (int)STRLEN(p),
p_ic, NULL, *dir, FALSE) == OK)
// if dir was BACKWARD then honor it just once
*dir = FORWARD;
}
/*
* For ":spelldump": Find matching prefixes for "word". Prepend each to
* "word" and append a line to the buffer.
* When "lnum" is zero add insert mode completion.
* Return the updated line number.
*/
static linenr_T
dump_prefixes(
slang_T *slang,
char_u *word, // case-folded word
char_u *pat,
int *dir,
int dumpflags,
int flags, // flags with prefix ID
linenr_T startlnum)
{
idx_T arridx[MAXWLEN];
int curi[MAXWLEN];
char_u prefix[MAXWLEN];
char_u word_up[MAXWLEN];
int has_word_up = FALSE;
int c;
char_u *byts;
idx_T *idxs;
linenr_T lnum = startlnum;
int depth;
int n;
int len;
int i;
// If the word starts with a lower-case letter make the word with an
// upper-case letter in word_up[].
c = PTR2CHAR(word);
if (SPELL_TOUPPER(c) != c)
{
onecap_copy(word, word_up, TRUE);
has_word_up = TRUE;
}
byts = slang->sl_pbyts;
idxs = slang->sl_pidxs;
if (byts != NULL) // array not is empty
{
/*
* Loop over all prefixes, building them byte-by-byte in prefix[].
* When at the end of a prefix check that it supports "flags".
*/
depth = 0;
arridx[0] = 0;
curi[0] = 1;
while (depth >= 0 && !got_int)
{
n = arridx[depth];
len = byts[n];
if (curi[depth] > len)
{
// Done all bytes at this node, go up one level.
--depth;
line_breakcheck();
}
else
{
// Do one more byte at this node.
n += curi[depth];
++curi[depth];
c = byts[n];
if (c == 0)
{
// End of prefix, find out how many IDs there are.
for (i = 1; i < len; ++i)
if (byts[n + i] != 0)
break;
curi[depth] += i - 1;
c = valid_word_prefix(i, n, flags, word, slang, FALSE);
if (c != 0)
{
vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
dump_word(slang, prefix, pat, dir, dumpflags,
(c & WF_RAREPFX) ? (flags | WF_RARE)
: flags, lnum);
if (lnum != 0)
++lnum;
}
// Check for prefix that matches the word when the
// first letter is upper-case, but only if the prefix has
// a condition.
if (has_word_up)
{
c = valid_word_prefix(i, n, flags, word_up, slang,
TRUE);
if (c != 0)
{
vim_strncpy(prefix + depth, word_up,
MAXWLEN - depth - 1);
dump_word(slang, prefix, pat, dir, dumpflags,
(c & WF_RAREPFX) ? (flags | WF_RARE)
: flags, lnum);
if (lnum != 0)
++lnum;
}
}
}
else
{
// Normal char, go one level deeper.
prefix[depth++] = c;
arridx[depth] = idxs[n];
curi[depth] = 1;
}
}
}
}
return lnum;
}
/*
* Move "p" to the end of word "start".
* Uses the spell-checking word characters.
*/
char_u *
spell_to_word_end(char_u *start, win_T *win)
{
char_u *p = start;
while (*p != NUL && spell_iswordp(p, win))
MB_PTR_ADV(p);
return p;
}
/*
* For Insert mode completion CTRL-X s:
* Find start of the word in front of column "startcol".
* We don't check if it is badly spelled, with completion we can only change
* the word in front of the cursor.
* Returns the column number of the word.
*/
int
spell_word_start(int startcol)
{
char_u *line;
char_u *p;
int col = 0;
if (no_spell_checking(curwin))
return startcol;
// Find a word character before "startcol".
line = ml_get_curline();
for (p = line + startcol; p > line; )
{
MB_PTR_BACK(line, p);
if (spell_iswordp_nmw(p, curwin))
break;
}
// Go back to start of the word.
while (p > line)
{
col = (int)(p - line);
MB_PTR_BACK(line, p);
if (!spell_iswordp(p, curwin))
break;
col = 0;
}
return col;
}
/*
* Need to check for 'spellcapcheck' now, the word is removed before
* expand_spelling() is called. Therefore the ugly global variable.
*/
static int spell_expand_need_cap;
void
spell_expand_check_cap(colnr_T col)
{
spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
}
/*
* Get list of spelling suggestions.
* Used for Insert mode completion CTRL-X ?.
* Returns the number of matches. The matches are in "matchp[]", array of
* allocated strings.
*/
int
expand_spelling(
linenr_T lnum UNUSED,
char_u *pat,
char_u ***matchp)
{
garray_T ga;
spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
*matchp = ga.ga_data;
return ga.ga_len;
}
/*
* Return TRUE if "val" is a valid 'spelllang' value.
*/
int
valid_spelllang(char_u *val)
{
return valid_name(val, ".-_,@");
}
/*
* Return TRUE if "val" is a valid 'spellfile' value.
*/
int
valid_spellfile(char_u *val)
{
char_u *s;
for (s = val; *s != NUL; ++s)
if (!vim_isfilec(*s) && *s != ',' && *s != ' ')
return FALSE;
return TRUE;
}
/*
* Handle side effects of setting 'spell'.
* Return an error message or NULL for success.
*/
char *
did_set_spell_option(int is_spellfile)
{
char *errmsg = NULL;
win_T *wp;
int l;
if (is_spellfile)
{
l = (int)STRLEN(curwin->w_s->b_p_spf);
if (l > 0 && (l < 4
|| STRCMP(curwin->w_s->b_p_spf + l - 4, ".add") != 0))
errmsg = e_invalid_argument;
}
if (errmsg == NULL)
{
FOR_ALL_WINDOWS(wp)
if (wp->w_buffer == curbuf && wp->w_p_spell)
{
errmsg = did_set_spelllang(wp);
break;
}
}
return errmsg;
}
/*
* Set curbuf->b_cap_prog to the regexp program for 'spellcapcheck'.
* Return error message when failed, NULL when OK.
*/
char *
compile_cap_prog(synblock_T *synblock)
{
regprog_T *rp = synblock->b_cap_prog;
char_u *re;
if (synblock->b_p_spc == NULL || *synblock->b_p_spc == NUL)
synblock->b_cap_prog = NULL;
else
{
// Prepend a ^ so that we only match at one column
re = concat_str((char_u *)"^", synblock->b_p_spc);
if (re != NULL)
{
synblock->b_cap_prog = vim_regcomp(re, RE_MAGIC);
vim_free(re);
if (synblock->b_cap_prog == NULL)
{
synblock->b_cap_prog = rp; // restore the previous program
return e_invalid_argument;
}
}
}
vim_regfree(rp);
return NULL;
}
#endif // FEAT_SPELL
|