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
|
/* vi:set ts=8 sts=4 sw=4:
*
* 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
*
* 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.
*
* There are two 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.
*
* Thanks to Olaf Seibert for providing an example implementation of this tree
* and the compression mechanism.
*
* Matching involves checking the caps type: Onecap ALLCAP KeepCap.
*
* Why doesn't Vim use aspell/ispell/myspell/etc.?
* See ":help develop-spell".
*/
/*
* Vim spell file format: <HEADER> <SUGGEST> <LWORDTREE> <KWORDTREE>
*
* <HEADER>: <fileID> <regioncnt> <regionname> ...
* <charflagslen> <charflags> <fcharslen> <fchars>
*
* <fileID> 10 bytes "VIMspell05"
* <regioncnt> 1 byte number of regions following (8 supported)
* <regionname> 2 bytes Region name: ca, au, etc. Lower case.
* First <regionname> is region 1.
*
* <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
* <charflags> N bytes List of flags (first one is for character 128):
* 0x01 word character
* 0x02 upper-case character
* <fcharslen> 2 bytes Number of bytes in <fchars>.
* <fchars> N bytes Folded characters, first one is for character 128.
*
*
* <SUGGEST> : <suggestlen> <more> ...
*
* <suggestlen> 4 bytes Length of <SUGGEST> in bytes, excluding
* <suggestlen>. MSB first.
* <more> To be defined.
*
*
* <LWORDTREE>: <wordtree>
*
* <wordtree>: <nodecount> <nodedata> ...
*
* <nodecount> 4 bytes Number of nodes following. MSB first.
*
* <nodedata>: <siblingcount> <sibling> ...
*
* <siblingcount> 1 byte Number of siblings in this node. The siblings
* follow in sorted order.
*
* <sibling>: <byte> [<nodeidx> <xbyte> | <flags> [<region>]]
*
* <byte> 1 byte Byte value of the sibling. Special cases:
* BY_NOFLAGS: End of word without flags and for all
* regions.
* BY_FLAGS: End of word, <flags> follow.
* BY_INDEX: Child of sibling is shared, <nodeidx>
* and <xbyte> follow.
*
* <nodeidx> 3 bytes Index of child for this sibling, MSB first.
*
* <xbyte> 1 byte byte value of the sibling.
*
* <flags> 1 byte bitmask of:
* WF_ALLCAP word must have only capitals
* WF_ONECAP first char of word must be capital
* WF_RARE rare word
* WF_REGION <region> follows
*
* <region> 1 byte Bitmask for regions in which word is valid. When
* omitted it's valid in all regions.
* Lowest bit is for region 1.
*
* <KWORDTREE>: <wordtree>
*
*
* All text characters are in 'encoding', but stored as single bytes.
* The region name is ASCII.
*/
#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
# include <io.h> /* for lseek(), must be before vim.h */
#endif
#include "vim.h"
#if defined(FEAT_SYN_HL) || defined(PROTO)
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#define MAXWLEN 250 /* assume max. word len is this many bytes */
/* Flags used for a word. */
#define WF_REGION 0x01 /* region byte follows */
#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
#define WF_ALLCAP 0x04 /* word must be all capitals */
#define WF_RARE 0x08 /* rare word */
#define WF_BANNED 0x10 /* bad word */
#define WF_KEEPCAP 0x100 /* keep-case word (not stored in file) */
#define BY_NOFLAGS 0 /* end of word without flags or region */
#define BY_FLAGS 1 /* end of word, flag byte follows */
#define BY_INDEX 2 /* child is shared, index follows */
#define BY_SPECIAL BY_INDEX /* hightest special byte value */
/* Info from "REP" entries in ".aff" file used in af_rep.
* TODO: This is not used yet. Either use it or remove it. */
typedef struct repentry_S
{
char_u *re_from;
char_u *re_to;
} repentry_T;
/*
* Structure used to store words and other info for one language, loaded from
* a .spl file.
* The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
* case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
*
* The "byts" array stores the possible bytes in each tree node, preceded by
* the number of possible bytes, sorted on byte value:
* <len> <byte1> <byte2> ...
* The "idxs" array stores the index of the child node corresponding to the
* byte in "byts".
* Exception: when the byte is zero, the word may end here and "idxs" holds
* the flags and region for the word. There may be several zeros in sequence
* for alternative flag/region combinations.
*/
typedef struct slang_S slang_T;
struct slang_S
{
slang_T *sl_next; /* next language */
char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
char_u *sl_fbyts; /* case-folded word bytes */
int *sl_fidxs; /* case-folded word indexes */
char_u *sl_kbyts; /* keep-case word bytes */
int *sl_kidxs; /* keep-case word indexes */
char_u *sl_try; /* "TRY" from .aff file TODO: not used */
garray_T sl_rep; /* list of repentry_T entries from REP lines
* TODO not used */
char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
int sl_error; /* error while loading */
};
/* First language that is loaded, start of the linked list of loaded
* languages. */
static slang_T *first_lang = NULL;
/*
* Structure used in "b_langp", filled from 'spelllang'.
*/
typedef struct langp_S
{
slang_T *lp_slang; /* info for this language (NULL for last one) */
int lp_region; /* bitmask for region or REGION_ALL */
} langp_T;
#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
#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
#define VIMSPELLMAGIC "VIMspell05" /* string at start of Vim spell file */
#define VIMSPELLMAGICL 10
/*
* Structure to store info for word matching.
*/
typedef struct matchinf_S
{
langp_T *mi_lp; /* info for language and region */
slang_T *mi_slang; /* info for the language */
/* pointers to original text to be checked */
char_u *mi_word; /* start of word being checked */
char_u *mi_end; /* end of matching word */
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 */
/* others */
int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
} matchinf_T;
/*
* The tables used for recognizing word characters according to spelling.
* These are only used for the first 256 characters of 'encoding'.
*/
typedef struct spelltab_S
{
char_u st_isw[256]; /* flags: is word char */
char_u st_isu[256]; /* flags: is uppercase char */
char_u st_fold[256]; /* chars: folded case */
} spelltab_T;
static spelltab_T spelltab;
static int did_set_spelltab;
#define SPELL_ISWORD 1
#define SPELL_ISUPPER 2
static void clear_spell_chartab __ARGS((spelltab_T *sp));
static int set_spell_finish __ARGS((spelltab_T *new_st));
/*
* Return TRUE if "p" points to a word character or "c" is a word character
* for spelling.
* Checking for a word character is done very often, avoid the function call
* overhead.
*/
#ifdef FEAT_MBYTE
# define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \
? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)])
#else
# define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)])
#endif
static slang_T *slang_alloc __ARGS((char_u *lang));
static void slang_free __ARGS((slang_T *lp));
static void find_word __ARGS((matchinf_T *mip, int keepcap));
static void spell_load_lang __ARGS((char_u *lang));
static void spell_load_file __ARGS((char_u *fname, void *cookie));
static int read_tree __ARGS((FILE *fd, char_u *byts, int *idxs, int maxidx, int startidx));
static int find_region __ARGS((char_u *rp, char_u *region));
static int captype __ARGS((char_u *word, char_u *end));
static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
#ifdef FEAT_MBYTE
static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
static void write_spell_chartab __ARGS((FILE *fd));
#endif
static int spell_isupper __ARGS((int c));
static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
static char *e_format = N_("E759: Format error in spell file");
/*
* Main spell-checking function.
* "ptr" points to a character that could be the start of a word.
* "*attrp" is set to the attributes 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.
* 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(wp, ptr, attrp)
win_T *wp; /* current window */
char_u *ptr;
int *attrp;
{
matchinf_T mi; /* Most things are put in "mi" so that it can
be passed to functions quickly. */
/* A word never starts at a space or a control character. Return quickly
* then, skipping over the character. */
if (*ptr <= ' ')
return 1;
/* A word starting with a number is always OK. Also skip hexadecimal
* numbers 0xFF99 and 0X99FF. */
if (*ptr >= '0' && *ptr <= '9')
{
if (*ptr == '0' && (ptr[1] == 'x' || ptr[2] == 'X'))
mi.mi_end = skiphex(ptr);
else
mi.mi_end = skipdigits(ptr);
}
else
{
/* Find the end of the word. */
mi.mi_word = ptr;
mi.mi_fend = ptr;
if (SPELL_ISWORDP(mi.mi_fend))
{
/* Make case-folded copy of the characters until the next non-word
* character. */
do
{
mb_ptr_adv(mi.mi_fend);
} while (*mi.mi_fend != NUL && SPELL_ISWORDP(mi.mi_fend));
/* Check the caps type of the word. */
mi.mi_capflags = captype(ptr, mi.mi_fend);
}
else
/* No word characters, caps type is always zero. */
mi.mi_capflags = 0;
/* We always use the characters up to the next non-word character,
* also for bad words. */
mi.mi_end = mi.mi_fend;
mi.mi_cend = mi.mi_fend;
/* Include 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(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
MAXWLEN + 1);
mi.mi_fwordlen = STRLEN(mi.mi_fword);
/* The word is bad unless we recognize it. */
mi.mi_result = SP_BAD;
/*
* Loop over the languages specified in 'spelllang'.
* We check them all, because a matching word may be longer than an
* already found matching word.
*/
for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
{
/* Check for a matching word in case-folded words. */
find_word(&mi, FALSE);
/* Try keep-case words. */
find_word(&mi, TRUE);
}
if (mi.mi_result != SP_OK)
{
/* When we are at a non-word character there is no error, just
* skip over the character (try looking for a word after it). */
if (!SPELL_ISWORDP(ptr))
{
#ifdef FEAT_MBYTE
if (has_mbyte)
return mb_ptr2len_check(ptr);
#endif
return 1;
}
if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
*attrp = highlight_attr[HLF_SPB];
else if (mi.mi_result == SP_RARE)
*attrp = highlight_attr[HLF_SPR];
else
*attrp = highlight_attr[HLF_SPL];
}
}
return (int)(mi.mi_end - ptr);
}
/*
* Check if the word at "mip->mi_word" is in the tree.
* When "keepcap" is TRUE check in keep-case word tree.
*
* For a match mip->mi_result is updated.
*/
static void
find_word(mip, keepcap)
matchinf_T *mip;
int keepcap;
{
int arridx = 0;
int endlen[MAXWLEN]; /* length at possible word endings */
int endidx[MAXWLEN]; /* possible word endings */
int endidxcnt = 0;
int len;
int wlen = 0;
int flen;
int c;
char_u *ptr;
unsigned lo, hi, m;
#ifdef FEAT_MBYTE
char_u *s;
#endif
char_u *p;
int res = SP_BAD;
int valid;
slang_T *slang = mip->mi_lp->lp_slang;
unsigned flags;
char_u *byts;
int *idxs;
if (keepcap)
{
/* 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;
}
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 (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)
{
/* Need to fold at least one more character. Do until next
* non-word character for efficiency. */
p = mip->mi_fend;
do
{
mb_ptr_adv(mip->mi_fend);
} while (*mip->mi_fend != NUL && SPELL_ISWORDP(mip->mi_fend));
/* 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(p, (int)(mip->mi_fend - p),
mip->mi_fword + mip->mi_fwordlen,
MAXWLEN - mip->mi_fwordlen);
flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
mip->mi_fwordlen += flen;
}
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];
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;
}
/*
* Verify that one of the possible endings is valid. Try the longest
* first.
*/
while (endidxcnt > 0)
{
--endidxcnt;
arridx = endidx[endidxcnt];
wlen = endlen[endidxcnt];
#ifdef FEAT_MBYTE
if ((*mb_head_off)(ptr, ptr + wlen) > 0)
continue; /* not at first byte of character */
#endif
if (SPELL_ISWORDP(ptr + wlen))
continue; /* next char is a word character */
#ifdef FEAT_MBYTE
if (!keepcap && has_mbyte)
{
/* Compute byte length in original word, length may change
* when folding case. */
p = mip->mi_word;
for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
mb_ptr_adv(p);
wlen = p - mip->mi_word;
}
#endif
/* Check flags and region. Repeat this if there are more
* flags/region alternatives until there is a match. */
for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; --len)
{
flags = idxs[arridx];
if (keepcap)
{
/* For "keepcap" tree the case is always right. */
valid = TRUE;
}
else
{
/* Check that the word is in the required case. */
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);
}
valid = (mip->mi_capflags == WF_ALLCAP
|| ((flags & WF_ALLCAP) == 0
&& ((flags & WF_ONECAP) == 0
|| mip->mi_capflags == WF_ONECAP)));
}
if (valid)
{
if (flags & WF_BANNED)
res = SP_BANNED;
else if (flags & WF_REGION)
{
/* Check region. */
if ((mip->mi_lp->lp_region & (flags >> 8)) != 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. */
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 (res == SP_OK)
break;
}
else
res = SP_BAD;
++arridx;
}
if (res == SP_OK)
break;
}
}
/*
* Move to next spell error.
* Return OK if found, FAIL otherwise.
*/
int
spell_move_to(dir, allwords)
int dir; /* FORWARD or BACKWARD */
int allwords; /* TRUE for "[s" and "]s" */
{
linenr_T lnum;
pos_T found_pos;
char_u *line;
char_u *p;
int attr = 0;
int len;
int has_syntax = syntax_present(curbuf);
int col;
int can_spell;
if (!curwin->w_p_spell || *curwin->w_buffer->b_p_spl == NUL)
{
EMSG(_("E756: Spell checking not enabled"));
return FAIL;
}
/*
* 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).
*/
lnum = curwin->w_cursor.lnum;
found_pos.lnum = 0;
while (!got_int)
{
line = ml_get(lnum);
p = line;
while (*p != NUL)
{
/* When searching backward don't search after the cursor. */
if (dir == BACKWARD
&& lnum == curwin->w_cursor.lnum
&& (colnr_T)(p - line) >= curwin->w_cursor.col)
break;
/* start of word */
len = spell_check(curwin, p, &attr);
if (attr != 0)
{
/* We found a bad word. Check the attribute. */
/* TODO: check for syntax @Spell cluster. */
if (allwords || attr == highlight_attr[HLF_SPB])
{
/* When searching forward only accept a bad word after
* the cursor. */
if (dir == BACKWARD
|| lnum > curwin->w_cursor.lnum
|| (lnum == curwin->w_cursor.lnum
&& (colnr_T)(p - line)
> curwin->w_cursor.col))
{
if (has_syntax)
{
col = p - line;
(void)syn_get_id(lnum, (colnr_T)col,
FALSE, &can_spell);
/* have to get the line again, a multi-line
* regexp may make it invalid */
line = ml_get(lnum);
p = line + col;
}
else
can_spell = TRUE;
if (can_spell)
{
found_pos.lnum = lnum;
found_pos.col = p - line;
#ifdef FEAT_VIRTUALEDIT
found_pos.coladd = 0;
#endif
if (dir == FORWARD)
{
/* No need to search further. */
curwin->w_cursor = found_pos;
return OK;
}
}
}
}
attr = 0;
}
/* advance to character after the word */
p += len;
if (*p == NUL)
break;
}
/* Advance to next line. */
if (dir == BACKWARD)
{
if (found_pos.lnum != 0)
{
/* Use the last match in the line. */
curwin->w_cursor = found_pos;
return OK;
}
if (lnum == 1)
return FAIL;
--lnum;
}
else
{
if (lnum == curbuf->b_ml.ml_line_count)
return FAIL;
++lnum;
}
line_breakcheck();
}
return FAIL; /* interrupted */
}
/*
* Load word list(s) for "lang" from Vim spell file(s).
* "lang" must be the language without the region: "en".
*/
static void
spell_load_lang(lang)
char_u *lang;
{
char_u fname_enc[80];
char_u *p;
int r;
char_u langcp[MAXWLEN + 1];
/* Copy the language name to pass it to spell_load_file() as a cookie.
* It's truncated when an error is detected. */
STRCPY(langcp, lang);
/* Find all spell files for "lang" in 'runtimepath' and load them.
* Use 'encoding', except that we use "latin1" for "latin9". */
#ifdef FEAT_MBYTE
if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
p = p_enc;
else
#endif
p = (char_u *)"latin1";
vim_snprintf((char *)fname_enc, sizeof(fname_enc),
"spell/%s.%s.spl", lang, p);
r = do_in_runtimepath(fname_enc, TRUE, spell_load_file, &langcp);
if (r == FAIL && *langcp != NUL)
{
/* Try loading the ASCII version. */
vim_snprintf((char *)fname_enc, sizeof(fname_enc),
"spell/%s.ascii.spl", lang);
r = do_in_runtimepath(fname_enc, TRUE, spell_load_file, &langcp);
}
if (r == FAIL)
smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
fname_enc + 6);
}
/*
* Allocate a new slang_T.
* Caller must fill "sl_next".
*/
static slang_T *
slang_alloc(lang)
char_u *lang;
{
slang_T *lp;
lp = (slang_T *)alloc_clear(sizeof(slang_T));
if (lp != NULL)
{
lp->sl_name = vim_strsave(lang);
ga_init2(&lp->sl_rep, sizeof(repentry_T), 4);
}
return lp;
}
/*
* Free the contents of an slang_T and the structure itself.
*/
static void
slang_free(lp)
slang_T *lp;
{
vim_free(lp->sl_name);
vim_free(lp->sl_fbyts);
vim_free(lp->sl_kbyts);
vim_free(lp->sl_fidxs);
vim_free(lp->sl_kidxs);
ga_clear(&lp->sl_rep);
vim_free(lp->sl_try);
vim_free(lp);
}
/*
* Load one spell file and store the info into a slang_T.
* Invoked through do_in_runtimepath().
*/
static void
spell_load_file(fname, cookie)
char_u *fname;
void *cookie; /* points to the language name */
{
char_u *lang = cookie;
FILE *fd;
char_u buf[MAXWLEN + 1];
char_u *p;
int i;
int len;
int round;
char_u *save_sourcing_name = sourcing_name;
linenr_T save_sourcing_lnum = sourcing_lnum;
int cnt, ccnt;
char_u *fol;
slang_T *lp = NULL;
fd = fopen((char *)fname, "r");
if (fd == NULL)
{
EMSG2(_(e_notopen), fname);
goto endFAIL;
}
lp = slang_alloc(lang);
if (lp == NULL)
goto endFAIL;
/* Set sourcing_name, so that error messages mention the file name. */
sourcing_name = fname;
sourcing_lnum = 0;
/* <HEADER>: <fileID> <regioncnt> <regionname> ...
* <charflagslen> <charflags> <fcharslen> <fchars> */
for (i = 0; i < VIMSPELLMAGICL; ++i)
buf[i] = getc(fd); /* <fileID> */
if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
{
EMSG(_("E757: Wrong file ID in spell file"));
goto endFAIL;
}
cnt = getc(fd); /* <regioncnt> */
if (cnt < 0)
{
truncerr:
EMSG(_("E758: Truncated spell file"));
goto endFAIL;
}
if (cnt > 8)
{
formerr:
EMSG(_(e_format));
goto endFAIL;
}
for (i = 0; i < cnt; ++i)
{
lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
lp->sl_regions[i * 2 + 1] = getc(fd);
}
lp->sl_regions[cnt * 2] = NUL;
cnt = getc(fd); /* <charflagslen> */
if (cnt > 0)
{
p = alloc((unsigned)cnt);
if (p == NULL)
goto endFAIL;
for (i = 0; i < cnt; ++i)
p[i] = getc(fd); /* <charflags> */
ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */
if (ccnt <= 0)
{
vim_free(p);
goto formerr;
}
fol = alloc((unsigned)ccnt + 1);
if (fol == NULL)
{
vim_free(p);
goto endFAIL;
}
for (i = 0; i < ccnt; ++i)
fol[i] = getc(fd); /* <fchars> */
fol[i] = NUL;
/* Set the word-char flags and fill spell_isupper() table. */
i = set_spell_charflags(p, cnt, fol);
vim_free(p);
vim_free(fol);
if (i == FAIL)
goto formerr;
}
else
{
/* When <charflagslen> is zero then <fcharlen> must also be zero. */
cnt = (getc(fd) << 8) + getc(fd);
if (cnt != 0)
goto formerr;
}
/* <SUGGEST> : <suggestlen> <more> ... */
/* TODO, just skip this for now */
i = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
while (i-- > 0)
if (getc(fd) == EOF) /* <suggestlen> */
goto truncerr;
/* round 1: <LWORDTREE>
* round 2: <KWORDTREE> */
for (round = 1; round <= 2; ++round)
{
/* The tree size was computed when writing the file, so that we can
* allocate it as one long block. <nodecount> */
len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
if (len < 0)
goto truncerr;
if (len > 0)
{
/* Allocate the byte array. */
p = lalloc((long_u)len, TRUE);
if (p == NULL)
goto endFAIL;
if (round == 1)
lp->sl_fbyts = p;
else
lp->sl_kbyts = p;
/* Allocate the index array. */
p = lalloc_clear((long_u)(len * sizeof(int)), TRUE);
if (p == NULL)
goto endFAIL;
if (round == 1)
lp->sl_fidxs = (int *)p;
else
lp->sl_kidxs = (int *)p;
/* Read the tree and store it in the array. */
i = read_tree(fd,
round == 1 ? lp->sl_fbyts : lp->sl_kbyts,
round == 1 ? lp->sl_fidxs : lp->sl_kidxs,
len, 0);
if (i == -1)
goto truncerr;
if (i < 0)
goto formerr;
}
}
lp->sl_next = first_lang;
first_lang = lp;
goto endOK;
endFAIL:
/* truncating the name signals the error to spell_load_lang() */
*lang = NUL;
if (lp != NULL)
slang_free(lp);
endOK:
if (fd != NULL)
fclose(fd);
sourcing_name = save_sourcing_name;
sourcing_lnum = save_sourcing_lnum;
}
/*
* Read one row of siblings from the spell file and store it in the byte array
* "byts" and index array "idxs". Recursively read the children.
*
* NOTE: The code here must match put_tree().
*
* Returns the index follosing the siblings.
* Returns -1 if the file is shorter than expected.
* Returns -2 if there is a format error.
*/
static int
read_tree(fd, byts, idxs, maxidx, startidx)
FILE *fd;
char_u *byts;
int *idxs;
int maxidx; /* size of arrays */
int startidx; /* current index in "byts" and "idxs" */
{
int len;
int i;
int n;
int idx = startidx;
int c;
#define SHARED_MASK 0x8000000
len = getc(fd); /* <siblingcount> */
if (len <= 0)
return -1;
if (startidx + len >= maxidx)
return -2;
byts[idx++] = len;
/* Read the byte values, flag/region bytes and shared indexes. */
for (i = 1; i <= len; ++i)
{
c = getc(fd); /* <byte> */
if (c < 0)
return -1;
if (c <= BY_SPECIAL)
{
if (c == BY_NOFLAGS)
{
/* No flags, all regions. */
idxs[idx] = 0;
c = 0;
}
else if (c == BY_FLAGS)
{
/* Read flags and option region. */
c = getc(fd); /* <flags> */
if (c & WF_REGION)
c = (getc(fd) << 8) + c; /* <region> */
idxs[idx] = c;
c = 0;
}
else /* c == BY_INDEX */
{
/* <nodeidx> */
n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
if (n < 0 || n >= maxidx)
return -2;
idxs[idx] = n + SHARED_MASK;
c = getc(fd); /* <xbyte> */
}
}
byts[idx++] = c;
}
/* Recursively read the children for non-shared siblings.
* Skip the end-of-word ones (zero byte value) and the shared ones (and
* remove SHARED_MASK) */
for (i = 1; i <= len; ++i)
if (byts[startidx + i] != 0)
{
if (idxs[startidx + i] & SHARED_MASK)
idxs[startidx + i] &= ~SHARED_MASK;
else
{
idxs[startidx + i] = idx;
idx = read_tree(fd, byts, idxs, maxidx, idx);
if (idx < 0)
break;
}
}
return idx;
}
/*
* Parse 'spelllang' and set buf->b_langp accordingly.
* Returns an error message or NULL.
*/
char_u *
did_set_spelllang(buf)
buf_T *buf;
{
garray_T ga;
char_u *lang;
char_u *e;
char_u *region;
int region_mask;
slang_T *lp;
int c;
char_u lbuf[MAXWLEN + 1];
ga_init2(&ga, sizeof(langp_T), 2);
/* loop over comma separated languages. */
for (lang = buf->b_p_spl; *lang != NUL; lang = e)
{
e = vim_strchr(lang, ',');
if (e == NULL)
e = lang + STRLEN(lang);
region = NULL;
if (e > lang + 2)
{
if (e - lang >= MAXWLEN)
{
ga_clear(&ga);
return e_invarg;
}
if (lang[2] == '_')
region = lang + 3;
}
/* Check if we loaded this language before. */
for (lp = first_lang; lp != NULL; lp = lp->sl_next)
if (STRNICMP(lp->sl_name, lang, 2) == 0)
break;
if (lp == NULL)
{
/* Not found, load the language. */
STRNCPY(lbuf, lang, e - lang);
lbuf[e - lang] = NUL;
if (region != NULL)
mch_memmove(lbuf + 2, lbuf + 5, e - lang - 4);
spell_load_lang(lbuf);
}
/*
* Loop over the languages, there can be several files for each.
*/
for (lp = first_lang; lp != NULL; lp = lp->sl_next)
if (STRNICMP(lp->sl_name, lang, 2) == 0)
{
if (region == NULL)
region_mask = REGION_ALL;
else
{
/* find region in sl_regions */
c = find_region(lp->sl_regions, region);
if (c == REGION_ALL)
{
c = *e;
*e = NUL;
smsg((char_u *)_("Warning: region %s not supported"),
lang);
*e = c;
region_mask = REGION_ALL;
}
else
region_mask = 1 << c;
}
if (ga_grow(&ga, 1) == FAIL)
{
ga_clear(&ga);
return e_outofmem;
}
LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
++ga.ga_len;
}
if (*e == ',')
++e;
}
/* Add a NULL entry to mark the end of the list. */
if (ga_grow(&ga, 1) == FAIL)
{
ga_clear(&ga);
return e_outofmem;
}
LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
++ga.ga_len;
/* Everything is fine, store the new b_langp value. */
ga_clear(&buf->b_langp);
buf->b_langp = ga;
return NULL;
}
/*
* Find the region "region[2]" in "rp" (points to "sl_regions").
* Each region is simply stored as the two characters of it's name.
* Returns the index if found, REGION_ALL if not found.
*/
static int
find_region(rp, 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 type of word:
* w word 0
* Word WF_ONECAP
* W WORD WF_ALLCAP
* WoRd wOrd WF_KEEPCAP
*/
static int
captype(word, end)
char_u *word;
char_u *end;
{
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(p); mb_ptr_adv(p))
if (p >= end)
return 0; /* only non-word characters, illegal word */
#ifdef FEAT_MBYTE
c = mb_ptr2char_adv(&p);
#else
c = *p++;
#endif
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 ( ; p < end; mb_ptr_adv(p))
if (SPELL_ISWORDP(p))
{
#ifdef FEAT_MBYTE
c = mb_ptr2char(p);
#else
c = *p;
#endif
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;
}
# if defined(FEAT_MBYTE) || defined(PROTO)
/*
* Clear all spelling tables and reload them.
* Used after 'encoding' is set and when ":mkspell" was used.
*/
void
spell_reload()
{
buf_T *buf;
slang_T *lp;
/* Initialize the table for SPELL_ISWORDP(). */
init_spell_chartab();
/* Unload all allocated memory. */
while (first_lang != NULL)
{
lp = first_lang;
first_lang = lp->sl_next;
slang_free(lp);
}
/* Go through all buffers and handle 'spelllang'. */
for (buf = firstbuf; buf != NULL; buf = buf->b_next)
{
ga_clear(&buf->b_langp);
if (*buf->b_p_spl != NUL)
did_set_spelllang(buf);
}
}
# endif
#if defined(FEAT_MBYTE) || defined(PROTO)
/*
* Functions for ":mkspell".
* Only possible with the multi-byte feature.
*/
#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
and .dic file. */
/*
* Main structure to store the contents of a ".aff" file.
*/
typedef struct afffile_S
{
char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
char_u *af_try; /* "TRY" line in "af_enc" encoding */
int af_rar; /* ID for rare word */
int af_huh; /* ID for keep-case word */
hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
garray_T af_rep; /* list of repentry_T entries from REP lines */
} afffile_T;
typedef struct affentry_S affentry_T;
/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
struct affentry_S
{
affentry_T *ae_next; /* next affix with same name/number */
char_u *ae_chop; /* text to chop off basic word (can be NULL) */
char_u *ae_add; /* text to add to basic word (can be NULL) */
char_u *ae_cond; /* condition (NULL for ".") */
regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
};
/* Affix header from ".aff" file. Used for af_pref and af_suff. */
typedef struct affheader_S
{
char_u ah_key[2]; /* key for hashtable == name of affix entry */
int ah_combine; /* suffix may combine with prefix */
affentry_T *ah_first; /* first affix entry */
} affheader_T;
#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
/*
* Structure that is used to store the items in the word tree. This avoids
* the need to keep track of each allocated thing, it's freed all at once
* after ":mkspell" is done.
*/
#define SBLOCKSIZE 16000 /* size of sb_data */
typedef struct sblock_S sblock_T;
struct sblock_S
{
sblock_T *sb_next; /* next block in list */
int sb_used; /* nr of bytes already in use */
char_u sb_data[1]; /* data, actually longer */
};
/*
* A node in the tree.
*/
typedef struct wordnode_S wordnode_T;
struct wordnode_S
{
char_u wn_hashkey[6]; /* room for the hash key */
wordnode_T *wn_next; /* next node with same hash key */
wordnode_T *wn_child; /* child (next byte in word) */
wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
always sorted) */
wordnode_T *wn_wnode; /* parent node that will write this node */
int wn_index; /* index in written nodes (valid after first
round) */
char_u wn_byte; /* Byte for this node. NUL for word end */
char_u wn_flags; /* when wn_byte is NUL: WF_ flags */
char_u wn_region; /* when wn_byte is NUL: region mask */
};
#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
/*
* Info used while reading the spell files.
*/
typedef struct spellinfo_S
{
wordnode_T *si_foldroot; /* tree with case-folded words */
wordnode_T *si_keeproot; /* tree with keep-case words */
sblock_T *si_blocks; /* memory blocks used */
int si_ascii; /* handling only ASCII words */
int si_region; /* region mask */
vimconv_T si_conv; /* for conversion to 'encoding' */
int si_memtot; /* runtime memory used */
} spellinfo_T;
static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
static int has_non_ascii __ARGS((char_u *s));
static void spell_free_aff __ARGS((afffile_T *aff));
static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
static int store_aff_word __ARGS((char_u *word, spellinfo_T *spin, char_u *afflist, hashtab_T *ht, hashtab_T *xht, int comb, int flags));
static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
static void *getroom __ARGS((sblock_T **blp, size_t len));
static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
static void free_blocks __ARGS((sblock_T *bl));
static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags));
static int tree_add_word __ARGS((char_u *word, wordnode_T *tree, int flags, int region, sblock_T **blp));
static void wordtree_compress __ARGS((wordnode_T *root));
static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin, int regcount, char_u *regchars));
static int put_tree __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask));
/*
* Read an affix ".aff" file.
* Returns an afffile_T, NULL for failure.
*/
static afffile_T *
spell_read_aff(fname, spin)
char_u *fname;
spellinfo_T *spin;
{
FILE *fd;
afffile_T *aff;
char_u rline[MAXLINELEN];
char_u *line;
char_u *pc = NULL;
char_u *(items[6]);
int itemcnt;
char_u *p;
int lnum = 0;
affheader_T *cur_aff = NULL;
int aff_todo = 0;
hashtab_T *tp;
char_u *low = NULL;
char_u *fol = NULL;
char_u *upp = NULL;
static char *e_affname = N_("Affix name too long in %s line %d: %s");
/*
* Open the file.
*/
fd = fopen((char *)fname, "r");
if (fd == NULL)
{
EMSG2(_(e_notopen), fname);
return NULL;
}
smsg((char_u *)_("Reading affix file %s..."), fname);
out_flush();
/*
* Allocate and init the afffile_T structure.
*/
aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
if (aff == NULL)
return NULL;
hash_init(&aff->af_pref);
hash_init(&aff->af_suff);
ga_init2(&aff->af_rep, (int)sizeof(repentry_T), 20);
/*
* Read all the lines in the file one by one.
*/
while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
{
line_breakcheck();
++lnum;
/* Skip comment lines. */
if (*rline == '#')
continue;
/* Convert from "SET" to 'encoding' when needed. */
vim_free(pc);
if (spin->si_conv.vc_type != CONV_NONE)
{
pc = string_convert(&spin->si_conv, rline, NULL);
if (pc == NULL)
{
smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
fname, lnum, rline);
continue;
}
line = pc;
}
else
{
pc = NULL;
line = rline;
}
/* Split the line up in white separated items. Put a NUL after each
* item. */
itemcnt = 0;
for (p = line; ; )
{
while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
++p;
if (*p == NUL)
break;
if (itemcnt == 6) /* too many items */
break;
items[itemcnt++] = p;
while (*p > ' ') /* skip until white space or CR/NL */
++p;
if (*p == NUL)
break;
*p++ = NUL;
}
/* Handle non-empty lines. */
if (itemcnt > 0)
{
if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
&& aff->af_enc == NULL)
{
/* Setup for conversion from "ENC" to 'encoding'. */
aff->af_enc = enc_canonize(items[1]);
if (aff->af_enc != NULL && !spin->si_ascii
&& convert_setup(&spin->si_conv, aff->af_enc,
p_enc) == FAIL)
smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
fname, aff->af_enc, p_enc);
}
else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
{
/* ignored */
}
else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2
&& aff->af_try == NULL)
{
aff->af_try = getroom_save(&spin->si_blocks, items[1]);
}
else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
&& aff->af_rar == 0)
{
aff->af_rar = items[1][0];
if (items[1][1] != NUL)
smsg((char_u *)_(e_affname), fname, lnum, items[1]);
}
else if (STRCMP(items[0], "HUH") == 0 && itemcnt == 2
&& aff->af_huh == 0)
{
aff->af_huh = items[1][0];
if (items[1][1] != NUL)
smsg((char_u *)_(e_affname), fname, lnum, items[1]);
}
else if ((STRCMP(items[0], "PFX") == 0
|| STRCMP(items[0], "SFX") == 0)
&& aff_todo == 0
&& itemcnt == 4)
{
/* New affix letter. */
cur_aff = (affheader_T *)getroom(&spin->si_blocks,
sizeof(affheader_T));
if (cur_aff == NULL)
break;
cur_aff->ah_key[0] = *items[1];
cur_aff->ah_key[1] = NUL;
if (items[1][1] != NUL)
smsg((char_u *)_(e_affname), fname, lnum, items[1]);
if (*items[2] == 'Y')
cur_aff->ah_combine = TRUE;
else if (*items[2] != 'N')
smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
fname, lnum, items[2]);
if (*items[0] == 'P')
tp = &aff->af_pref;
else
tp = &aff->af_suff;
aff_todo = atoi((char *)items[3]);
if (!HASHITEM_EMPTY(hash_find(tp, cur_aff->ah_key)))
{
smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
fname, lnum, items[1]);
aff_todo = 0;
}
else
hash_add(tp, cur_aff->ah_key);
}
else if ((STRCMP(items[0], "PFX") == 0
|| STRCMP(items[0], "SFX") == 0)
&& aff_todo > 0
&& STRCMP(cur_aff->ah_key, items[1]) == 0
&& itemcnt == 5)
{
affentry_T *aff_entry;
/* New item for an affix letter. */
--aff_todo;
aff_entry = (affentry_T *)getroom(&spin->si_blocks,
sizeof(affentry_T));
if (aff_entry == NULL)
break;
if (STRCMP(items[2], "0") != 0)
aff_entry->ae_chop = getroom_save(&spin->si_blocks,
items[2]);
if (STRCMP(items[3], "0") != 0)
aff_entry->ae_add = getroom_save(&spin->si_blocks,
items[3]);
/* Don't use an affix entry with non-ASCII characters when
* "spin->si_ascii" is TRUE. */
if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
|| has_non_ascii(aff_entry->ae_add)))
{
aff_entry->ae_next = cur_aff->ah_first;
cur_aff->ah_first = aff_entry;
if (STRCMP(items[4], ".") != 0)
{
char_u buf[MAXLINELEN];
aff_entry->ae_cond = getroom_save(&spin->si_blocks,
items[4]);
if (*items[0] == 'P')
sprintf((char *)buf, "^%s", items[4]);
else
sprintf((char *)buf, "%s$", items[4]);
aff_entry->ae_prog = vim_regcomp(buf,
RE_MAGIC + RE_STRING);
}
}
}
else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
{
if (fol != NULL)
smsg((char_u *)_("Duplicate FOL in %s line %d"),
fname, lnum);
else
fol = vim_strsave(items[1]);
}
else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
{
if (low != NULL)
smsg((char_u *)_("Duplicate LOW in %s line %d"),
fname, lnum);
else
low = vim_strsave(items[1]);
}
else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
{
if (upp != NULL)
smsg((char_u *)_("Duplicate UPP in %s line %d"),
fname, lnum);
else
upp = vim_strsave(items[1]);
}
else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
/* Ignore REP count */;
else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
{
repentry_T *rp;
/* REP item */
if (ga_grow(&aff->af_rep, 1) == FAIL)
break;
rp = ((repentry_T *)aff->af_rep.ga_data) + aff->af_rep.ga_len;
rp->re_from = getroom_save(&spin->si_blocks, items[1]);
rp->re_to = getroom_save(&spin->si_blocks, items[2]);
++aff->af_rep.ga_len;
}
else
smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
fname, lnum, items[0]);
}
}
if (fol != NULL || low != NULL || upp != NULL)
{
/* Don't write a word table for an ASCII file, so that we don't check
* for conflicts with a word table that matches 'encoding'. */
if (!spin->si_ascii)
{
if (fol == NULL || low == NULL || upp == NULL)
smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
else
set_spell_chartab(fol, low, upp);
}
vim_free(fol);
vim_free(low);
vim_free(upp);
}
vim_free(pc);
fclose(fd);
return aff;
}
/*
* Return TRUE if string "s" contains a non-ASCII character (128 or higher).
* When "s" is NULL FALSE is returned.
*/
static int
has_non_ascii(s)
char_u *s;
{
char_u *p;
if (s != NULL)
for (p = s; *p != NUL; ++p)
if (*p >= 128)
return TRUE;
return FALSE;
}
/*
* Free the structure filled by spell_read_aff().
*/
static void
spell_free_aff(aff)
afffile_T *aff;
{
hashtab_T *ht;
hashitem_T *hi;
int todo;
affheader_T *ah;
affentry_T *ae;
vim_free(aff->af_enc);
/* All this trouble to foree the "ae_prog" items... */
for (ht = &aff->af_pref; ; ht = &aff->af_suff)
{
todo = ht->ht_used;
for (hi = ht->ht_array; todo > 0; ++hi)
{
if (!HASHITEM_EMPTY(hi))
{
--todo;
ah = HI2AH(hi);
for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
vim_free(ae->ae_prog);
}
}
if (ht == &aff->af_suff)
break;
}
hash_clear(&aff->af_pref);
hash_clear(&aff->af_suff);
ga_clear(&aff->af_rep);
}
/*
* Read dictionary file "fname".
* Returns OK or FAIL;
*/
static int
spell_read_dic(fname, spin, affile)
char_u *fname;
spellinfo_T *spin;
afffile_T *affile;
{
hashtab_T ht;
char_u line[MAXLINELEN];
char_u *afflist;
char_u *dw;
char_u *pc;
char_u *w;
int l;
hash_T hash;
hashitem_T *hi;
FILE *fd;
int lnum = 1;
int non_ascii = 0;
int retval = OK;
char_u message[MAXLINELEN + MAXWLEN];
int flags;
/*
* Open the file.
*/
fd = fopen((char *)fname, "r");
if (fd == NULL)
{
EMSG2(_(e_notopen), fname);
return FAIL;
}
/* The hashtable is only used to detect duplicated words. */
hash_init(&ht);
smsg((char_u *)_("Reading dictionary file %s..."), fname);
out_flush();
/* Read and ignore the first line: word count. */
(void)vim_fgets(line, MAXLINELEN, fd);
if (!isdigit(*skipwhite(line)))
EMSG2(_("E760: No word count in %s"), fname);
/*
* Read all the lines in the file one by one.
* The words are converted to 'encoding' here, before being added to
* the hashtable.
*/
while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
{
line_breakcheck();
++lnum;
/* Remove CR, LF and white space from the end. White space halfway
* the word is kept to allow e.g., "et al.". */
l = STRLEN(line);
while (l > 0 && line[l - 1] <= ' ')
--l;
if (l == 0)
continue; /* empty line */
line[l] = NUL;
/* This takes time, print a message now and then. */
if ((lnum & 0x3ff) == 0)
{
vim_snprintf((char *)message, sizeof(message),
_("line %6d - %s"), lnum, line);
msg_start();
msg_outtrans_attr(message, 0);
msg_clr_eos();
msg_didout = FALSE;
msg_col = 0;
out_flush();
}
/* Find the optional affix names. */
afflist = vim_strchr(line, '/');
if (afflist != NULL)
*afflist++ = NUL;
/* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
if (spin->si_ascii && has_non_ascii(line))
{
++non_ascii;
continue;
}
/* Convert from "SET" to 'encoding' when needed. */
if (spin->si_conv.vc_type != CONV_NONE)
{
pc = string_convert(&spin->si_conv, line, NULL);
if (pc == NULL)
{
smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
fname, lnum, line);
continue;
}
w = pc;
}
else
{
pc = NULL;
w = line;
}
/* Store the word in the hashtable to be able to find duplicates. */
dw = (char_u *)getroom_save(&spin->si_blocks, w);
if (dw == NULL)
retval = FAIL;
vim_free(pc);
if (retval == FAIL)
break;
hash = hash_hash(dw);
hi = hash_lookup(&ht, dw, hash);
if (!HASHITEM_EMPTY(hi))
smsg((char_u *)_("Duplicate word in %s line %d: %s"),
fname, lnum, line);
else
hash_add_item(&ht, hi, dw, hash);
flags = 0;
if (afflist != NULL)
{
/* Check for affix name that stands for keep-case word and stands
* for rare word (if defined). */
if (affile->af_huh != NUL
&& vim_strchr(afflist, affile->af_huh) != NULL)
flags |= WF_KEEPCAP;
if (affile->af_rar != NUL
&& vim_strchr(afflist, affile->af_rar) != NULL)
flags |= WF_RARE;
}
/* Add the word to the word tree(s). */
if (store_word(dw, spin, flags) == FAIL)
retval = FAIL;
if (afflist != NULL)
{
/* Find all matching suffixes and add the resulting words.
* Additionally do matching prefixes that combine. */
if (store_aff_word(dw, spin, afflist,
&affile->af_suff, &affile->af_pref,
FALSE, flags) == FAIL)
retval = FAIL;
/* Find all matching prefixes and add the resulting words. */
if (store_aff_word(dw, spin, afflist,
&affile->af_pref, NULL, FALSE, flags) == FAIL)
retval = FAIL;
}
}
if (spin->si_ascii && non_ascii > 0)
smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
non_ascii);
hash_clear(&ht);
fclose(fd);
return retval;
}
/*
* Apply affixes to a word and store the resulting words.
* "ht" is the hashtable with affentry_T that need to be applied, either
* prefixes or suffixes.
* "xht", when not NULL, is the prefix hashtable, to be used additionally on
* the resulting words for combining affixes.
*
* Returns FAIL when out of memory.
*/
static int
store_aff_word(word, spin, afflist, ht, xht, comb, flags)
char_u *word; /* basic word start */
spellinfo_T *spin; /* spell info */
char_u *afflist; /* list of names of supported affixes */
hashtab_T *ht;
hashtab_T *xht;
int comb; /* only use affixes that combine */
int flags; /* flags for the word */
{
int todo;
hashitem_T *hi;
affheader_T *ah;
affentry_T *ae;
regmatch_T regmatch;
char_u newword[MAXWLEN];
int retval = OK;
int i;
char_u *p;
todo = ht->ht_used;
for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
{
if (!HASHITEM_EMPTY(hi))
{
--todo;
ah = HI2AH(hi);
/* Check that the affix combines, if required, and that the word
* supports this affix. */
if ((!comb || ah->ah_combine)
&& vim_strchr(afflist, *ah->ah_key) != NULL)
{
/* Loop over all affix entries with this name. */
for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
{
/* Check the condition. It's not logical to match case
* here, but it is required for compatibility with
* Myspell. */
regmatch.regprog = ae->ae_prog;
regmatch.rm_ic = FALSE;
if (ae->ae_prog == NULL
|| vim_regexec(®match, word, (colnr_T)0))
{
/* Match. Remove the chop and add the affix. */
if (xht == NULL)
{
/* prefix: chop/add at the start of the word */
if (ae->ae_add == NULL)
*newword = NUL;
else
STRCPY(newword, ae->ae_add);
p = word;
if (ae->ae_chop != NULL)
/* Skip chop string. */
for (i = mb_charlen(ae->ae_chop); i > 0; --i)
mb_ptr_adv(p);
STRCAT(newword, p);
}
else
{
/* suffix: chop/add at the end of the word */
STRCPY(newword, word);
if (ae->ae_chop != NULL)
{
/* Remove chop string. */
p = newword + STRLEN(newword);
for (i = mb_charlen(ae->ae_chop); i > 0; --i)
mb_ptr_back(newword, p);
*p = NUL;
}
if (ae->ae_add != NULL)
STRCAT(newword, ae->ae_add);
}
/* Store the modified word. */
if (store_word(newword, spin, flags) == FAIL)
retval = FAIL;
/* When added a suffix and combining is allowed also
* try adding prefixes additionally. */
if (xht != NULL && ah->ah_combine)
if (store_aff_word(newword, spin, afflist,
xht, NULL, TRUE, flags) == FAIL)
retval = FAIL;
}
}
}
}
}
return retval;
}
/*
* Read a file with a list of words.
*/
static int
spell_read_wordfile(fname, spin)
char_u *fname;
spellinfo_T *spin;
{
FILE *fd;
long lnum = 0;
char_u rline[MAXLINELEN];
char_u *line;
char_u *pc = NULL;
int l;
int retval = OK;
int did_word = FALSE;
int non_ascii = 0;
char_u *enc;
int flags;
/*
* Open the file.
*/
fd = fopen((char *)fname, "r");
if (fd == NULL)
{
EMSG2(_(e_notopen), fname);
return FAIL;
}
smsg((char_u *)_("Reading word file %s..."), fname);
out_flush();
/*
* Read all the lines in the file one by one.
*/
while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
{
line_breakcheck();
++lnum;
/* Skip comment lines. */
if (*rline == '#')
continue;
/* Remove CR, LF and white space from the end. */
l = STRLEN(rline);
while (l > 0 && rline[l - 1] <= ' ')
--l;
if (l == 0)
continue; /* empty or blank line */
rline[l] = NUL;
/* Convert from "=encoding={encoding}" to 'encoding' when needed. */
vim_free(pc);
if (spin->si_conv.vc_type != CONV_NONE)
{
pc = string_convert(&spin->si_conv, rline, NULL);
if (pc == NULL)
{
smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
fname, lnum, rline);
continue;
}
line = pc;
}
else
{
pc = NULL;
line = rline;
}
flags = 0;
if (*line == '/')
{
++line;
if (STRNCMP(line, "encoding=", 9) == 0)
{
if (spin->si_conv.vc_type != CONV_NONE)
smsg((char_u *)_("Duplicate =encoding= line ignored in %s line %d: %s"),
fname, lnum, line);
else if (did_word)
smsg((char_u *)_("=encoding= line after word ignored in %s line %d: %s"),
fname, lnum, line);
else
{
/* Setup for conversion to 'encoding'. */
enc = enc_canonize(line + 10);
if (enc != NULL && !spin->si_ascii
&& convert_setup(&spin->si_conv, enc,
p_enc) == FAIL)
smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
fname, line + 10, p_enc);
vim_free(enc);
}
continue;
}
if (*line == '=')
{
/* keep-case word */
flags |= WF_KEEPCAP;
++line;
}
if (*line == '!')
{
/* Bad, bad, wicked word. */
flags |= WF_BANNED;
++line;
}
else if (*line == '?')
{
/* Rare word. */
flags |= WF_RARE;
++line;
}
if (flags == 0)
{
smsg((char_u *)_("/ line ignored in %s line %d: %s"),
fname, lnum, line);
continue;
}
}
/* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
if (spin->si_ascii && has_non_ascii(line))
{
++non_ascii;
continue;
}
/* Normal word: store it. */
if (store_word(line, spin, flags) == FAIL)
{
retval = FAIL;
break;
}
did_word = TRUE;
}
vim_free(pc);
fclose(fd);
if (spin->si_ascii && non_ascii > 0)
smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
non_ascii);
return retval;
}
/*
* Get part of an sblock_T, "len" bytes long.
* This avoids calling free() for every little struct we use.
* The memory is cleared to all zeros.
* Returns NULL when out of memory.
*/
static void *
getroom(blp, len)
sblock_T **blp;
size_t len; /* length needed */
{
char_u *p;
sblock_T *bl = *blp;
if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
{
/* Allocate a block of memory. This is not freed until much later. */
bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
if (bl == NULL)
return NULL;
bl->sb_next = *blp;
*blp = bl;
bl->sb_used = 0;
}
p = bl->sb_data + bl->sb_used;
bl->sb_used += len;
return p;
}
/*
* Make a copy of a string into memory allocated with getroom().
*/
static char_u *
getroom_save(blp, s)
sblock_T **blp;
char_u *s;
{
char_u *sc;
sc = (char_u *)getroom(blp, STRLEN(s) + 1);
if (sc != NULL)
STRCPY(sc, s);
return sc;
}
/*
* Free the list of allocated sblock_T.
*/
static void
free_blocks(bl)
sblock_T *bl;
{
sblock_T *next;
while (bl != NULL)
{
next = bl->sb_next;
vim_free(bl);
bl = next;
}
}
/*
* Allocate the root of a word tree.
*/
static wordnode_T *
wordtree_alloc(blp)
sblock_T **blp;
{
return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
}
/*
* Store a word in the tree(s).
* Always store it in the case-folded tree. A keep-case word can also be used
* with all caps.
* For a keep-case word also store it in the keep-case tree.
*/
static int
store_word(word, spin, flags)
char_u *word;
spellinfo_T *spin;
int flags; /* extra flags, WF_BANNED */
{
int len = STRLEN(word);
int ct = captype(word, word + len);
char_u foldword[MAXWLEN];
int res;
if (flags & WF_KEEPCAP)
res = OK; /* keep-case specified, don't add as fold-case */
else
{
(void)spell_casefold(word, len, foldword, MAXWLEN);
res = tree_add_word(foldword, spin->si_foldroot,
(ct == WF_KEEPCAP ? WF_ALLCAP : ct) | flags,
spin->si_region, &spin->si_blocks);
}
if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
res = tree_add_word(word, spin->si_keeproot, flags,
spin->si_region, &spin->si_blocks);
return res;
}
/*
* Add word "word" to a word tree at "root".
* Returns FAIL when out of memory.
*/
static int
tree_add_word(word, root, flags, region, blp)
char_u *word;
wordnode_T *root;
int flags;
int region;
sblock_T **blp;
{
wordnode_T *node = root;
wordnode_T *np;
wordnode_T **prev = NULL;
int i;
/* Add each byte of the word to the tree, including the NUL at the end. */
for (i = 0; ; ++i)
{
/* Look for the sibling that has the same character. They are sorted
* on byte value, thus stop searching when a sibling is found with a
* higher byte value. For zero bytes (end of word) check that the
* flags are equal, there is a separate zero byte for each flag value.
*/
while (node != NULL && (node->wn_byte < word[i]
|| (node->wn_byte == 0 && node->wn_flags != (flags & 0xff))))
{
prev = &node->wn_sibling;
node = *prev;
}
if (node == NULL || node->wn_byte != word[i])
{
/* Allocate a new node. */
np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
if (np == NULL)
return FAIL;
np->wn_byte = word[i];
*prev = np;
np->wn_sibling = node;
node = np;
}
if (word[i] == NUL)
{
node->wn_flags = flags;
node->wn_region |= region;
break;
}
prev = &node->wn_child;
node = *prev;
}
return OK;
}
/*
* Compress a tree: find tails that are identical and can be shared.
*/
static void
wordtree_compress(root)
wordnode_T *root;
{
hashtab_T ht;
int n;
int tot = 0;
if (root != NULL)
{
hash_init(&ht);
n = node_compress(root, &ht, &tot);
smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
n, tot, (tot - n) * 100 / tot);
hash_clear(&ht);
}
}
/*
* Compress a node, its siblings and its children, depth first.
* Returns the number of compressed nodes.
*/
static int
node_compress(node, ht, tot)
wordnode_T *node;
hashtab_T *ht;
int *tot; /* total count of nodes before compressing,
incremented while going through the tree */
{
wordnode_T *np;
wordnode_T *tp;
wordnode_T *child;
hash_T hash;
hashitem_T *hi;
int len = 0;
unsigned nr, n;
int compressed = 0;
/*
* Go through the list of siblings. Compress each child and then try
* finding an identical child to replace it.
* Note that with "child" we mean not just the node that is pointed to,
* but the whole list of siblings, of which the node is the first.
*/
for (np = node; np != NULL; np = np->wn_sibling)
{
++len;
if ((child = np->wn_child) != NULL)
{
/* Compress the child. This fills wn_hashkey. */
compressed += node_compress(child, ht, tot);
/* Try to find an identical child. */
hash = hash_hash(child->wn_hashkey);
hi = hash_lookup(ht, child->wn_hashkey, hash);
tp = NULL;
if (!HASHITEM_EMPTY(hi))
{
/* There are children with an identical hash value. Now check
* if there is one that is really identical. */
for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_next)
if (node_equal(child, tp))
{
/* Found one! Now use that child in place of the
* current one. This means the current child is
* dropped from the tree. */
np->wn_child = tp;
++compressed;
break;
}
if (tp == NULL)
{
/* No other child with this hash value equals the child of
* the node, add it to the linked list after the first
* item. */
tp = HI2WN(hi);
child->wn_next = tp->wn_next;
tp->wn_next = child;
}
}
else
/* No other child has this hash value, add it to the
* hashtable. */
hash_add_item(ht, hi, child->wn_hashkey, hash);
}
}
*tot += len;
/*
* Make a hash key for the node and its siblings, so that we can quickly
* find a lookalike node. This must be done after compressing the sibling
* list, otherwise the hash key would become invalid by the compression.
*/
node->wn_hashkey[0] = len;
nr = 0;
for (np = node; np != NULL; np = np->wn_sibling)
{
if (np->wn_byte == NUL)
/* end node: only use wn_flags and wn_region */
n = np->wn_flags + (np->wn_region << 8);
else
/* byte node: use the byte value and the child pointer */
n = np->wn_byte + ((long_u)np->wn_child << 8);
nr = nr * 101 + n;
}
/* Avoid NUL bytes, it terminates the hash key. */
n = nr & 0xff;
node->wn_hashkey[1] = n == 0 ? 1 : n;
n = (nr >> 8) & 0xff;
node->wn_hashkey[2] = n == 0 ? 1 : n;
n = (nr >> 16) & 0xff;
node->wn_hashkey[3] = n == 0 ? 1 : n;
n = (nr >> 24) & 0xff;
node->wn_hashkey[4] = n == 0 ? 1 : n;
node->wn_hashkey[5] = NUL;
return compressed;
}
/*
* Return TRUE when two nodes have identical siblings and children.
*/
static int
node_equal(n1, n2)
wordnode_T *n1;
wordnode_T *n2;
{
wordnode_T *p1;
wordnode_T *p2;
for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
p1 = p1->wn_sibling, p2 = p2->wn_sibling)
if (p1->wn_byte != p2->wn_byte
|| (p1->wn_byte == NUL
? (p1->wn_flags != p2->wn_flags
|| p1->wn_region != p2->wn_region)
: (p1->wn_child != p2->wn_child)))
break;
return p1 == NULL && p2 == NULL;
}
/*
* Write a number to file "fd", MSB first, in "len" bytes.
*/
void
put_bytes(fd, nr, len)
FILE *fd;
long_u nr;
int len;
{
int i;
for (i = len - 1; i >= 0; --i)
putc((int)(nr >> (i * 8)), fd);
}
/*
* Write the Vim spell file "fname".
*/
static void
write_vim_spell(fname, spin, regcount, regchars)
char_u *fname;
spellinfo_T *spin;
int regcount; /* number of regions */
char_u *regchars; /* region names */
{
FILE *fd;
int regionmask;
int round;
wordnode_T *tree;
int nodecount;
fd = fopen((char *)fname, "w");
if (fd == NULL)
{
EMSG2(_(e_notopen), fname);
return;
}
/* <HEADER>: <fileID> <regioncnt> <regionname> ...
* <charflagslen> <charflags> <fcharslen> <fchars> */
/* <fileID> */
if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
EMSG(_(e_write));
/* write the region names if there is more than one */
if (regcount > 1)
{
putc(regcount, fd); /* <regioncnt> <regionname> ... */
fwrite(regchars, (size_t)(regcount * 2), (size_t)1, fd);
regionmask = (1 << regcount) - 1;
}
else
{
putc(0, fd);
regionmask = 0;
}
/* Write the table with character flags and table for case folding.
* <charflagslen> <charflags> <fcharlen> <fchars>
* Skip this for ASCII, the table may conflict with the one used for
* 'encoding'. */
if (spin->si_ascii)
{
putc(0, fd);
putc(0, fd);
putc(0, fd);
}
else
write_spell_chartab(fd);
/* <SUGGEST> : <suggestlen> <more> ...
* TODO. Only write a zero length for now. */
put_bytes(fd, 0L, 4); /* <suggestlen> */
spin->si_memtot = 0;
/*
* <LWORDTREE> <KWORDTREE>
*/
for (round = 1; round <= 2; ++round)
{
tree = (round == 1) ? spin->si_foldroot : spin->si_keeproot;
/* Count the number of nodes. Needed to be able to allocate the
* memory when reading the nodes. Also fills in the index for shared
* nodes. */
nodecount = put_tree(NULL, tree, 0, regionmask);
/* number of nodes in 4 bytes */
put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
spin->si_memtot += nodecount + nodecount * sizeof(int);
/* Write the nodes. */
(void)put_tree(fd, tree, 0, regionmask);
}
fclose(fd);
}
/*
* Dump a word tree at node "node".
*
* This first writes the list of possible bytes (siblings). Then for each
* byte recursively write the children.
*
* NOTE: The code here must match the code in read_tree(), since assumptions
* are made about the indexes (so that we don't have to write them in the
* file).
*
* Returns the number of nodes used.
*/
static int
put_tree(fd, node, index, regionmask)
FILE *fd; /* NULL when only counting */
wordnode_T *node;
int index;
int regionmask;
{
int newindex = index;
int siblingcount = 0;
wordnode_T *np;
int flags;
/* If "node" is zero the tree is empty. */
if (node == NULL)
return 0;
/* Store the index where this node is written. */
node->wn_index = index;
/* Count the number of siblings. */
for (np = node; np != NULL; np = np->wn_sibling)
++siblingcount;
/* Write the sibling count. */
if (fd != NULL)
putc(siblingcount, fd); /* <siblingcount> */
/* Write each sibling byte and optionally extra info. */
for (np = node; np != NULL; np = np->wn_sibling)
{
if (np->wn_byte == 0)
{
if (fd != NULL)
{
/* For a NUL byte (end of word) instead of the byte itself
* we write the flag/region items. */
flags = np->wn_flags;
if (regionmask != 0 && np->wn_region != regionmask)
flags |= WF_REGION;
if (flags == 0)
{
/* word without flags or region */
putc(BY_NOFLAGS, fd); /* <byte> */
}
else
{
putc(BY_FLAGS, fd); /* <byte> */
putc(flags, fd); /* <flags> */
if (flags & WF_REGION)
putc(np->wn_region, fd); /* <regionmask> */
}
}
}
else
{
if (np->wn_child->wn_index != 0 && np->wn_child->wn_wnode != node)
{
/* The child is written elsewhere, write the reference. */
if (fd != NULL)
{
putc(BY_INDEX, fd); /* <byte> */
/* <nodeidx> */
put_bytes(fd, (long_u)np->wn_child->wn_index, 3);
}
}
else if (np->wn_child->wn_wnode == NULL)
/* We will write the child below and give it an index. */
np->wn_child->wn_wnode = node;
if (fd != NULL)
if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
{
EMSG(_(e_write));
return 0;
}
}
}
/* Space used in the array when reading: one for each sibling and one for
* the count. */
newindex += siblingcount + 1;
/* Recursively dump the children of each sibling. */
for (np = node; np != NULL; np = np->wn_sibling)
if (np->wn_byte != 0 && np->wn_child->wn_wnode == node)
newindex = put_tree(fd, np->wn_child, newindex, regionmask);
return newindex;
}
/*
* ":mkspell outfile infile ..."
*/
void
ex_mkspell(eap)
exarg_T *eap;
{
int fcount;
char_u **fnames;
char_u fname[MAXPATHL];
char_u wfname[MAXPATHL];
afffile_T *(afile[8]);
int i;
int len;
char_u region_name[16];
struct stat st;
char_u *arg = eap->arg;
int error = FALSE;
spellinfo_T spin;
vim_memset(&spin, 0, sizeof(spin));
if (STRNCMP(arg, "-ascii", 6) == 0)
{
spin.si_ascii = TRUE;
arg = skipwhite(arg + 6);
}
/* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
if (get_arglist_exp(arg, &fcount, &fnames) == FAIL)
return;
if (fcount < 2)
EMSG(_(e_invarg)); /* need at least output and input names */
else if (fcount > 9)
EMSG(_("E754: Only up to 8 regions supported"));
else
{
/* Check for overwriting before doing things that may take a lot of
* time. */
vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
spin.si_ascii ? (char_u *)"ascii" : p_enc);
if (!eap->forceit && mch_stat((char *)wfname, &st) >= 0)
{
EMSG(_(e_exists));
goto theend;
}
if (mch_isdir(fnames[0]))
{
EMSG2(_(e_isadir2), fnames[0]);
goto theend;
}
/*
* Init the aff and dic pointers.
* Get the region names if there are more than 2 arguments.
*/
for (i = 1; i < fcount; ++i)
{
afile[i - 1] = NULL;
if (fcount > 2)
{
len = STRLEN(fnames[i]);
if (STRLEN(gettail(fnames[i])) < 5 || fnames[i][len - 3] != '_')
{
EMSG2(_("E755: Invalid region in %s"), fnames[i]);
goto theend;
}
else
{
region_name[(i - 1) * 2] = TOLOWER_ASC(fnames[i][len - 2]);
region_name[(i - 1) * 2 + 1] =
TOLOWER_ASC(fnames[i][len - 1]);
}
}
}
/* Clear the char type tables, don't want to use any of the currently
* used spell properties. */
init_spell_chartab();
spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
if (spin.si_foldroot == NULL || spin.si_keeproot == NULL)
{
error = TRUE;
goto theend;
}
/*
* Read all the .aff and .dic files.
* Text is converted to 'encoding'.
* Words are stored in the case-folded and keep-case trees.
*/
for (i = 1; i < fcount && !error; ++i)
{
spin.si_conv.vc_type = CONV_NONE;
spin.si_region = 1 << (i - 1);
vim_snprintf((char *)fname, sizeof(fname), "%s.aff", fnames[i]);
if (mch_stat((char *)fname, &st) >= 0)
{
/* Read the .aff file. Will init "spin->si_conv" based on the
* "SET" line. */
afile[i - 1] = spell_read_aff(fname, &spin);
if (afile[i - 1] == NULL)
error = TRUE;
else
{
/* Read the .dic file and store the words in the trees. */
vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
fnames[i]);
if (spell_read_dic(fname, &spin, afile[i - 1]) == FAIL)
error = TRUE;
}
}
else
{
/* No .aff file, try reading the file as a word list. Store
* the words in the trees. */
if (spell_read_wordfile(fnames[i], &spin) == FAIL)
error = TRUE;
}
/* Free any conversion stuff. */
convert_setup(&spin.si_conv, NULL, NULL);
}
if (!error)
{
/*
* Remove the dummy NUL from the start of the tree root.
*/
spin.si_foldroot = spin.si_foldroot->wn_sibling;
spin.si_keeproot = spin.si_keeproot->wn_sibling;
/*
* Combine tails in the tree.
*/
MSG(_("Compressing word tree..."));
out_flush();
wordtree_compress(spin.si_foldroot);
wordtree_compress(spin.si_keeproot);
}
if (!error)
{
/*
* Write the info in the spell file.
*/
smsg((char_u *)_("Writing spell file %s..."), wfname);
out_flush();
write_vim_spell(wfname, &spin, fcount - 1, region_name);
MSG(_("Done!"));
smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
spin.si_memtot);
out_flush();
/* May need to reload spell dictionaries */
spell_reload();
}
/* Free the allocated memory. */
free_blocks(spin.si_blocks);
/* Free the .aff file structures. */
for (i = 1; i < fcount; ++i)
if (afile[i - 1] != NULL)
spell_free_aff(afile[i - 1]);
}
theend:
FreeWild(fcount, fnames);
}
#endif /* FEAT_MBYTE */
/*
* Init the chartab used for spelling for ASCII.
* EBCDIC is not supported!
*/
static void
clear_spell_chartab(sp)
spelltab_T *sp;
{
int i;
/* Init everything to FALSE. */
vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
for (i = 0; i < 256; ++i)
sp->st_fold[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;
}
/*
* 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.
*/
void
init_spell_chartab()
{
int i;
did_set_spelltab = FALSE;
clear_spell_chartab(&spelltab);
#ifdef FEAT_MBYTE
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
#endif
{
/* Rough guess: use isalpha() and isupper() for characters above 128. */
for (i = 128; i < 256; ++i)
{
spelltab.st_isw[i] = MB_ISUPPER(i) || MB_ISLOWER(i);
if (MB_ISUPPER(i))
{
spelltab.st_isu[i] = TRUE;
spelltab.st_fold[i] = MB_TOLOWER(i);
}
}
}
}
#if defined(FEAT_MBYTE) || defined(PROTO)
static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
/*
* Set the spell character tables from strings in the affix file.
*/
static int
set_spell_chartab(fol, low, upp)
char_u *fol;
char_u *low;
char_u *upp;
{
/* We build the new tables here first, so that we can compare with the
* previous one. */
spelltab_T new_st;
char_u *pf = fol, *pl = low, *pu = upp;
int f, l, u;
clear_spell_chartab(&new_st);
while (*pf != NUL)
{
if (*pl == NUL || *pu == NUL)
{
EMSG(_(e_affform));
return FAIL;
}
#ifdef FEAT_MBYTE
f = mb_ptr2char_adv(&pf);
l = mb_ptr2char_adv(&pl);
u = mb_ptr2char_adv(&pu);
#else
f = *pf++;
l = *pl++;
u = *pu++;
#endif
/* Every character that appears is a word character. */
if (f < 256)
new_st.st_isw[f] = TRUE;
if (l < 256)
new_st.st_isw[l] = TRUE;
if (u < 256)
new_st.st_isw[u] = TRUE;
/* if "LOW" and "FOL" are not the same the "LOW" char needs
* case-folding */
if (l < 256 && l != f)
{
if (f >= 256)
{
EMSG(_(e_affrange));
return FAIL;
}
new_st.st_fold[l] = f;
}
/* if "UPP" and "FOL" are not the same the "UPP" char needs
* case-folding and it's upper case. */
if (u < 256 && u != f)
{
if (f >= 256)
{
EMSG(_(e_affrange));
return FAIL;
}
new_st.st_fold[u] = f;
new_st.st_isu[u] = TRUE;
}
}
if (*pl != NUL || *pu != NUL)
{
EMSG(_(e_affform));
return FAIL;
}
return set_spell_finish(&new_st);
}
#endif
/*
* Set the spell character tables from strings in the .spl file.
*/
static int
set_spell_charflags(flags, cnt, upp)
char_u *flags;
int cnt;
char_u *upp;
{
/* We build the new tables here first, so that we can compare with the
* previous one. */
spelltab_T new_st;
int i;
char_u *p = upp;
clear_spell_chartab(&new_st);
for (i = 0; i < cnt; ++i)
{
new_st.st_isw[i + 128] = (flags[i] & SPELL_ISWORD) != 0;
new_st.st_isu[i + 128] = (flags[i] & SPELL_ISUPPER) != 0;
if (*p == NUL)
return FAIL;
#ifdef FEAT_MBYTE
new_st.st_fold[i + 128] = mb_ptr2char_adv(&p);
#else
new_st.st_fold[i + 128] = *p++;
#endif
}
return set_spell_finish(&new_st);
}
static int
set_spell_finish(new_st)
spelltab_T *new_st;
{
int i;
if (did_set_spelltab)
{
/* check that it's the same table */
for (i = 0; i < 256; ++i)
{
if (spelltab.st_isw[i] != new_st->st_isw[i]
|| spelltab.st_isu[i] != new_st->st_isu[i]
|| spelltab.st_fold[i] != new_st->st_fold[i])
{
EMSG(_("E763: Word characters differ between spell files"));
return FAIL;
}
}
}
else
{
/* copy the new spelltab into the one being used */
spelltab = *new_st;
did_set_spelltab = TRUE;
}
return OK;
}
#if defined(FEAT_MBYTE) || defined(PROTO)
/*
* Write the current tables into the .spl file.
* This makes sure the same characters are recognized as word characters when
* generating an when using a spell file.
*/
static void
write_spell_chartab(fd)
FILE *fd;
{
char_u charbuf[256 * 4];
int len = 0;
int flags;
int i;
fputc(128, fd); /* <charflagslen> */
for (i = 128; i < 256; ++i)
{
flags = 0;
if (spelltab.st_isw[i])
flags |= SPELL_ISWORD;
if (spelltab.st_isu[i])
flags |= SPELL_ISUPPER;
fputc(flags, fd); /* <charflags> */
len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
}
put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
}
#endif
/*
* Return TRUE if "c" is an upper-case character for spelling.
*/
static int
spell_isupper(c)
int c;
{
# ifdef FEAT_MBYTE
if (enc_utf8)
{
/* For Unicode we can call utf_isupper(), but don't do that for ASCII,
* because we don't want to use 'casemap' here. */
if (c >= 128)
return utf_isupper(c);
}
else if (has_mbyte && c > 256)
{
/* For characters above 255 we don't have something specfied.
* Fall back to locale-dependent iswupper(). If not available
* simply return FALSE. */
# ifdef HAVE_ISWUPPER
return iswupper(c);
# else
return FALSE;
# endif
}
# endif
return spelltab.st_isu[c];
}
/*
* Case-fold "p[len]" into "buf[buflen]". Used for spell checking.
* When using a multi-byte 'encoding' the length may change!
* Returns FAIL when something wrong.
*/
static int
spell_casefold(p, len, buf, buflen)
char_u *p;
int len;
char_u *buf;
int buflen;
{
int i;
if (len >= buflen)
{
buf[0] = NUL;
return FAIL; /* result will not fit */
}
#ifdef FEAT_MBYTE
if (has_mbyte)
{
int c;
int outi = 0;
/* Fold one character at a time. */
for (i = 0; i < len; i += mb_ptr2len_check(p + i))
{
c = mb_ptr2char(p + i);
if (enc_utf8)
/* For Unicode case folding is always the same, no need to use
* the table from the spell file. */
c = utf_fold(c);
else if (c < 256)
/* Use the table from the spell file. */
c = spelltab.st_fold[c];
# ifdef HAVE_TOWLOWER
else
/* We don't know what to do, fall back to towlower(), it
* depends on the current locale. */
c = towlower(c);
# endif
if (outi + MB_MAXBYTES > buflen)
{
buf[outi] = NUL;
return FAIL;
}
outi += mb_char2bytes(c, buf + outi);
}
buf[outi] = NUL;
}
else
#endif
{
/* Be quick for non-multibyte encodings. */
for (i = 0; i < len; ++i)
buf[i] = spelltab.st_fold[p[i]];
buf[i] = NUL;
}
return OK;
}
#endif /* FEAT_SYN_HL */
|