blob: a12c0cc55520b430457370d7de01aeabd67cb282 [file] [log] [blame]
Bram Moolenaare19defe2005-03-21 08:23:33 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000013 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
14 * has a list of bytes that can appear (siblings). For each byte there is a
15 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000016 *
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
18 * binary searching can be used and the NUL bytes are at the start. The
19 * number of possible bytes is stored before the list of bytes.
20 *
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
22 * either the next index or flags. The tree starts at index 0. For example,
23 * to lookup "vi" this sequence is followed:
24 * i = 0
25 * len = byts[i]
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
27 * i = idxs[n]
28 * len = byts[i]
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
30 * i = idxs[n]
31 * len = byts[i]
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000033 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000034 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 * original case. The second one is only used for keep-case words and is
36 * usually small.
37 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000038 * There is one additional tree for when prefixes are not applied when
39 * generating the .spl file. This tree stores all the possible prefixes, as
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
41 * following word must support this prefix nr. And the condition nr is
42 * stored, used to lookup the condition that the word must match with.
43 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000044 * Thanks to Olaf Seibert for providing an example implementation of this tree
45 * and the compression mechanism.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000046 *
47 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000048 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000049 * Why doesn't Vim use aspell/ispell/myspell/etc.?
50 * See ":help develop-spell".
51 */
52
Bram Moolenaar51485f02005-06-04 21:55:20 +000053/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000054 * Use this to let the score depend in how much a suggestion sounds like the
Bram Moolenaar9f30f502005-06-14 22:01:04 +000055 * bad word. It's quite slow and only occasionally makes the sorting better.
56#define SOUNDFOLD_SCORE
57 */
58
59/*
60 * Use this to adjust the score after finding suggestions, based on the
61 * suggested word sounding like the bad word. This is much faster than doing
62 * it for every possible suggestion.
63 * Disadvantage: When "the" is typed as "hte" it sounds different and goes
64 * down in the list.
65#define RESCORE(word_score, sound_score) ((2 * word_score + sound_score) / 3)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000066 */
67
68/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000069 * Vim spell file format: <HEADER>
70 * <SUGGEST>
71 * <LWORDTREE>
72 * <KWORDTREE>
73 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000074 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000075 * <HEADER>: <fileID>
76 * <regioncnt> <regionname> ...
77 * <charflagslen> <charflags>
78 * <fcharslen> <fchars>
79 * <prefcondcnt> <prefcond> ...
Bram Moolenaar51485f02005-06-04 21:55:20 +000080 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000081 * <fileID> 10 bytes "VIMspell07"
Bram Moolenaar51485f02005-06-04 21:55:20 +000082 * <regioncnt> 1 byte number of regions following (8 supported)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000083 * <regionname> 2 bytes Region name: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +000084 * First <regionname> is region 1.
85 *
86 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
87 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +000088 * 0x01 word character CF_WORD
89 * 0x02 upper-case character CF_UPPER
Bram Moolenaar51485f02005-06-04 21:55:20 +000090 * <fcharslen> 2 bytes Number of bytes in <fchars>.
91 * <fchars> N bytes Folded characters, first one is for character 128.
92 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000093 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
94 *
95 * <prefcond> : <condlen> <condstr>
96 *
97 * <condlen> 1 byte Length of <condstr>.
98 *
99 * <condstr> N bytes Condition for the prefix.
100 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000101 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000102 * <SUGGEST> : <repcount> <rep> ...
103 * <salflags> <salcount> <sal> ...
104 * <maplen> <mapstr>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000105 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000106 * <repcount> 2 bytes number of <rep> items, MSB first.
107 *
108 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
109 *
110 * <repfromlen> 1 byte length of <repfrom>
111 *
112 * <repfrom> N bytes "from" part of replacement
113 *
114 * <reptolen> 1 byte length of <repto>
115 *
116 * <repto> N bytes "to" part of replacement
117 *
118 * <salflags> 1 byte flags for soundsalike conversion:
119 * SAL_F0LLOWUP
120 * SAL_COLLAPSE
121 * SAL_REM_ACCENTS
122 *
123 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
124 *
125 * <salfromlen> 1 byte length of <salfrom>
126 *
127 * <salfrom> N bytes "from" part of soundsalike
128 *
129 * <saltolen> 1 byte length of <salto>
130 *
131 * <salto> N bytes "to" part of soundsalike
132 *
133 * <maplen> 2 bytes length of <mapstr>, MSB first
134 *
135 * <mapstr> N bytes String with sequences of similar characters,
136 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000137 *
138 *
139 * <LWORDTREE>: <wordtree>
140 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000141 * <KWORDTREE>: <wordtree>
142 *
143 * <PREFIXTREE>: <wordtree>
144 *
145 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000146 * <wordtree>: <nodecount> <nodedata> ...
147 *
148 * <nodecount> 4 bytes Number of nodes following. MSB first.
149 *
150 * <nodedata>: <siblingcount> <sibling> ...
151 *
152 * <siblingcount> 1 byte Number of siblings in this node. The siblings
153 * follow in sorted order.
154 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000155 * <sibling>: <byte> [ <nodeidx> <xbyte>
156 * | <flags> [<region>] [<prefixID>]
157 * | <prefixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000158 *
159 * <byte> 1 byte Byte value of the sibling. Special cases:
160 * BY_NOFLAGS: End of word without flags and for all
161 * regions.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000162 * BY_FLAGS: End of word, <flags> follow. For
163 * PREFIXTREE <prefixID> and <prefcondnr>
164 * follow.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000165 * BY_INDEX: Child of sibling is shared, <nodeidx>
166 * and <xbyte> follow.
167 *
168 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
169 *
170 * <xbyte> 1 byte byte value of the sibling.
171 *
172 * <flags> 1 byte bitmask of:
173 * WF_ALLCAP word must have only capitals
174 * WF_ONECAP first char of word must be capital
175 * WF_RARE rare word
176 * WF_REGION <region> follows
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000177 * WF_PFX <prefixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000178 *
179 * <region> 1 byte Bitmask for regions in which word is valid. When
180 * omitted it's valid in all regions.
181 * Lowest bit is for region 1.
182 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000183 * <prefixID> 1 byte ID of prefix that can be used with this word. For
184 * PREFIXTREE used for the required prefix ID.
185 *
186 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
187 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000188 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000189 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000190 */
191
Bram Moolenaare19defe2005-03-21 08:23:33 +0000192#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
193# include <io.h> /* for lseek(), must be before vim.h */
194#endif
195
196#include "vim.h"
197
198#if defined(FEAT_SYN_HL) || defined(PROTO)
199
200#ifdef HAVE_FCNTL_H
201# include <fcntl.h>
202#endif
203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000204#define MAXWLEN 250 /* Assume max. word len is this many bytes.
205 Some places assume a word length fits in a
206 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000207
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000208/* Type used for indexes in the word tree need to be at least 3 bytes. If int
209 * is 8 bytes we could use something smaller, but what? */
210#if SIZEOF_INT > 2
211typedef int idx_T;
212#else
213typedef long idx_T;
214#endif
215
216/* Flags used for a word. Only the lowest byte can be used, the region byte
217 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000218#define WF_REGION 0x01 /* region byte follows */
219#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
220#define WF_ALLCAP 0x04 /* word must be all capitals */
221#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000222#define WF_BANNED 0x10 /* bad word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000223#define WF_PFX 0x20 /* prefix ID list follows */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000224#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000225
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000226#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000227
228#define BY_NOFLAGS 0 /* end of word without flags or region */
229#define BY_FLAGS 1 /* end of word, flag byte follows */
230#define BY_INDEX 2 /* child is shared, index follows */
231#define BY_SPECIAL BY_INDEX /* hightest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000232
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000233/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
234 * si_sal and sl_sal.
235 * One replacement: from "ft_from" to "ft_to". */
236typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000237{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000238 char_u *ft_from;
239 char_u *ft_to;
240} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000241
242/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000243 * Structure used to store words and other info for one language, loaded from
244 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000245 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
246 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
247 *
248 * The "byts" array stores the possible bytes in each tree node, preceded by
249 * the number of possible bytes, sorted on byte value:
250 * <len> <byte1> <byte2> ...
251 * The "idxs" array stores the index of the child node corresponding to the
252 * byte in "byts".
253 * Exception: when the byte is zero, the word may end here and "idxs" holds
254 * the flags and region for the word. There may be several zeros in sequence
255 * for alternative flag/region combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000256 */
257typedef struct slang_S slang_T;
258struct slang_S
259{
260 slang_T *sl_next; /* next language */
261 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000262 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000263 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000264
Bram Moolenaar51485f02005-06-04 21:55:20 +0000265 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000266 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000267 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000268 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000269 char_u *sl_pbyts; /* prefix tree word bytes */
270 idx_T *sl_pidxs; /* prefix tree word indexes */
271
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000272 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000273
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000274 int sl_prefixcnt; /* number of items in "sl_prefprog" */
275 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
276
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000277 garray_T sl_rep; /* list of fromto_T entries from REP lines */
278 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
279 there is none */
280 garray_T sl_sal; /* list of fromto_T entries from SAL lines */
281 short sl_sal_first[256]; /* indexes where byte first appears, -1 if
282 there is none */
283 int sl_followup; /* SAL followup */
284 int sl_collapse; /* SAL collapse_result */
285 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000286 int sl_has_map; /* TRUE if there is a MAP line */
287#ifdef FEAT_MBYTE
288 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
289 int sl_map_array[256]; /* MAP for first 256 chars */
290#else
291 char_u sl_map_array[256]; /* MAP for first 256 chars */
292#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000293};
294
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000295/* First language that is loaded, start of the linked list of loaded
296 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000297static slang_T *first_lang = NULL;
298
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000299/* Flags used in .spl file for soundsalike flags. */
300#define SAL_F0LLOWUP 1
301#define SAL_COLLAPSE 2
302#define SAL_REM_ACCENTS 4
303
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000304/*
305 * Structure used in "b_langp", filled from 'spelllang'.
306 */
307typedef struct langp_S
308{
309 slang_T *lp_slang; /* info for this language (NULL for last one) */
310 int lp_region; /* bitmask for region or REGION_ALL */
311} langp_T;
312
313#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
314
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000315#define REGION_ALL 0xff /* word valid in all regions */
316
317/* Result values. Lower number is accepted over higher one. */
318#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000319#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000320#define SP_RARE 1
321#define SP_LOCAL 2
322#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000323
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000324#define VIMSPELLMAGIC "VIMspell07" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000325#define VIMSPELLMAGICL 10
326
327/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000328 * Information used when looking for suggestions.
329 */
330typedef struct suginfo_S
331{
332 garray_T su_ga; /* suggestions, contains "suggest_T" */
333 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000334 char_u *su_badptr; /* start of bad word in line */
335 int su_badlen; /* length of detected bad word in line */
336 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
337 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
338 hashtab_T su_banned; /* table with banned words */
339#ifdef SOUNDFOLD_SCORE
340 slang_T *su_slang; /* currently used slang_T */
341 char_u su_salword[MAXWLEN]; /* soundfolded badword */
342#endif
343} suginfo_T;
344
345/* One word suggestion. Used in "si_ga". */
346typedef struct suggest_S
347{
348 char_u *st_word; /* suggested word, allocated string */
349 int st_orglen; /* length of replaced text */
350 int st_score; /* lower is better */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000351#ifdef RESCORE
352 int st_had_bonus; /* bonus already included in score */
353#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000354} suggest_T;
355
356#define SUG(sup, i) (((suggest_T *)(sup)->su_ga.ga_data)[i])
357
358/* Number of suggestions displayed. */
359#define SUG_PROMPT_COUNT ((int)Rows - 2)
360
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000361/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
362 * called the score may change, thus we need to keep more than what is
363 * displayed. */
364#define SUG_CLEAN_COUNT (SUG_PROMPT_COUNT < 25 ? 25 : SUG_PROMPT_COUNT)
365
366/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
367 * of suggestions that are not going to be displayed. */
368#define SUG_MAX_COUNT (SUG_PROMPT_COUNT + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000369
370/* score for various changes */
371#define SCORE_SPLIT 99 /* split bad word */
372#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000373#define SCORE_REGION 70 /* word is for different region */
374#define SCORE_RARE 180 /* rare word */
375
376/* score for edit distance */
377#define SCORE_SWAP 90 /* swap two characters */
378#define SCORE_SWAP3 110 /* swap two characters in three */
379#define SCORE_REP 87 /* REP replacement */
380#define SCORE_SUBST 93 /* substitute a character */
381#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000382#define SCORE_DEL 94 /* delete a character */
383#define SCORE_INS 96 /* insert a character */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000384
385#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
386 * 350 allows for about three changes. */
387#define SCORE_MAXMAX 999999 /* accept any score */
388
389/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000390 * Structure to store info for word matching.
391 */
392typedef struct matchinf_S
393{
394 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000395
396 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000397 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000398 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000399 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000400 char_u *mi_cend; /* char after what was used for
401 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000402
403 /* case-folded text */
404 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000405 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000406
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000407 /* for when checking word after a prefix */
408 int mi_prefarridx; /* index in sl_pidxs with list of
409 prefixID/condition */
410 int mi_prefcnt; /* number of entries at mi_prefarridx */
411 int mi_prefixlen; /* byte length of prefix */
412
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000413 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000414 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000415 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000416} matchinf_T;
417
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000418/*
419 * The tables used for recognizing word characters according to spelling.
420 * These are only used for the first 256 characters of 'encoding'.
421 */
422typedef struct spelltab_S
423{
424 char_u st_isw[256]; /* flags: is word char */
425 char_u st_isu[256]; /* flags: is uppercase char */
426 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000427 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000428} spelltab_T;
429
430static spelltab_T spelltab;
431static int did_set_spelltab;
432
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000433#define CF_WORD 0x01
434#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000435
436static void clear_spell_chartab __ARGS((spelltab_T *sp));
437static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000438static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000439
440/*
441 * Return TRUE if "p" points to a word character or "c" is a word character
442 * for spelling.
443 * Checking for a word character is done very often, avoid the function call
444 * overhead.
445 */
446#ifdef FEAT_MBYTE
447# define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \
448 ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)])
449#else
450# define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)])
451#endif
452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000453/*
Bram Moolenaarea424162005-06-16 21:51:00 +0000454 * For finding suggestion: At each node in the tree these states are tried:
455 */
456typedef enum
457{
458 STATE_START = 0, /* At start of node, check if word may end or
459 * split word. */
460 STATE_SPLITUNDO, /* Undo word split. */
461 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
462 STATE_PLAIN, /* Use each byte of the node. */
463 STATE_DEL, /* Delete a byte from the bad word. */
464 STATE_INS, /* Insert a byte in the bad word. */
465 STATE_SWAP, /* Swap two bytes. */
466 STATE_UNSWAP, /* Undo swap two bytes. */
467 STATE_SWAP3, /* Swap two bytes over three. */
468 STATE_UNSWAP3, /* Undo Swap two bytes over three. */
469 STATE_ROT3L, /* Rotate three bytes left */
470 STATE_UNROT3L, /* Undo rotate three bytes left */
471 STATE_ROT3R, /* Rotate three bytes right */
472 STATE_UNROT3R, /* Undo rotate three bytes right */
473 STATE_REP_INI, /* Prepare for using REP items. */
474 STATE_REP, /* Use matching REP items from the .aff file. */
475 STATE_REP_UNDO, /* Undo a REP item replacement. */
476 STATE_FINAL /* End of this node. */
477} state_T;
478
479/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000480 * Struct to keep the state at each level in spell_try_change().
481 */
482typedef struct trystate_S
483{
Bram Moolenaarea424162005-06-16 21:51:00 +0000484 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000485 int ts_score; /* score */
Bram Moolenaarea424162005-06-16 21:51:00 +0000486 short ts_curi; /* index in list of child nodes */
487 char_u ts_fidx; /* index in fword[], case-folded bad word */
488 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
489 char_u ts_twordlen; /* valid length of tword[] */
490#ifdef FEAT_MBYTE
491 char_u ts_tcharlen; /* number of bytes in tword character */
492 char_u ts_tcharidx; /* current byte index in tword character */
493 char_u ts_isdiff; /* DIFF_ values */
494 char_u ts_fcharstart; /* index in fword where badword char started */
495#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000496 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000497 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000498 char_u ts_save_splitoff; /* su_splitoff saved here */
499 char_u ts_save_badflags; /* badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000500} trystate_T;
501
Bram Moolenaarea424162005-06-16 21:51:00 +0000502/* values for ts_isdiff */
503#define DIFF_NONE 0 /* no different byte (yet) */
504#define DIFF_YES 1 /* different byte found */
505#define DIFF_INSERT 2 /* inserting character */
506
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000507/* mode values for find_word */
508#define FIND_FOLDWORD 0 /* find word case-folded */
509#define FIND_KEEPWORD 1 /* find keep-case word */
510#define FIND_PREFIX 2 /* find word after prefix */
511
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000512static slang_T *slang_alloc __ARGS((char_u *lang));
513static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000514static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000515static void find_word __ARGS((matchinf_T *mip, int mode));
516static void find_prefix __ARGS((matchinf_T *mip));
517static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000518static int spell_valid_case __ARGS((int origflags, int treeflags));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000519static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000520static char_u *spell_enc __ARGS((void));
521static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000522static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000523static idx_T read_tree __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx, int prefixtree, int maxprefcondnr));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000524static int find_region __ARGS((char_u *rp, char_u *region));
525static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000526static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000527static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000528static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
529static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000530static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000531static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000532static void spell_try_change __ARGS((suginfo_T *su));
533static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
534static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
535static void spell_try_soundalike __ARGS((suginfo_T *su));
536static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000537static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000538static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000539#ifdef RESCORE
540static void add_suggestion __ARGS((suginfo_T *su, char_u *goodword, int use_score, int had_bonus));
541#else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000542static void add_suggestion __ARGS((suginfo_T *su, char_u *goodword, int use_score));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000543#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000544static void add_banned __ARGS((suginfo_T *su, char_u *word));
545static int was_banned __ARGS((suginfo_T *su, char_u *word));
546static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000547#ifdef RESCORE
548static void rescore_suggestions __ARGS((suginfo_T *su));
549#endif
550static void cleanup_suggestions __ARGS((suginfo_T *su, int keep));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000551static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000552#if defined(RESCORE) || defined(SOUNDFOLD_SCORE)
553static int spell_sound_score __ARGS((slang_T *slang, char_u *goodword, char_u *badsound));
554#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000555static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
556
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000557/*
558 * Use our own character-case definitions, because the current locale may
559 * differ from what the .spl file uses.
560 * These must not be called with negative number!
561 */
562#ifndef FEAT_MBYTE
563/* Non-multi-byte implementation. */
564# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
565# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
566# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
567#else
568/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
569 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
570 * the "w" library function for characters above 255 if available. */
571# ifdef HAVE_TOWLOWER
572# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
573 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
574# else
575# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
576 : (c) < 256 ? spelltab.st_fold[c] : (c))
577# endif
578
579# ifdef HAVE_TOWUPPER
580# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
581 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
582# else
583# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
584 : (c) < 256 ? spelltab.st_upper[c] : (c))
585# endif
586
587# ifdef HAVE_ISWUPPER
588# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
589 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
590# else
591# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
592 : (c) < 256 ? spelltab.st_isu[c] : (c))
593# endif
594#endif
595
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000596
597static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000598
599/*
600 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000601 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000602 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
603 * or when it's OK it remains unchanged.
604 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000605 *
606 * "sug" is normally NULL. When looking for suggestions it points to
607 * suginfo_T. It's passed as a void pointer to keep the struct local.
608 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000609 * Returns the length of the word in bytes, also when it's OK, so that the
610 * caller can skip over the word.
611 */
612 int
Bram Moolenaar51485f02005-06-04 21:55:20 +0000613spell_check(wp, ptr, attrp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000614 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000615 char_u *ptr;
616 int *attrp;
617{
618 matchinf_T mi; /* Most things are put in "mi" so that it can
619 be passed to functions quickly. */
620
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000621 /* A word never starts at a space or a control character. Return quickly
622 * then, skipping over the character. */
623 if (*ptr <= ' ')
624 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000625
Bram Moolenaar51485f02005-06-04 21:55:20 +0000626 /* A word starting with a number is always OK. Also skip hexadecimal
627 * numbers 0xFF99 and 0X99FF. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000628 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000629 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000630 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
631 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000632 else
633 mi.mi_end = skipdigits(ptr);
634 }
635 else
636 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000637 /* Find the end of the word. */
638 mi.mi_word = ptr;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000639 mi.mi_fend = ptr;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000640
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000641 if (SPELL_ISWORDP(mi.mi_fend))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000642 {
643 /* Make case-folded copy of the characters until the next non-word
644 * character. */
645 do
646 {
647 mb_ptr_adv(mi.mi_fend);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000648 } while (*mi.mi_fend != NUL && SPELL_ISWORDP(mi.mi_fend));
Bram Moolenaar51485f02005-06-04 21:55:20 +0000649 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000650
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000651 /* We always use the characters up to the next non-word character,
652 * also for bad words. */
653 mi.mi_end = mi.mi_fend;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000654
655 /* Check caps type later. */
656 mi.mi_capflags = 0;
657 mi.mi_cend = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000658
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000659 /* Include one non-word character so that we can check for the
660 * word end. */
661 if (*mi.mi_fend != NUL)
662 mb_ptr_adv(mi.mi_fend);
663
664 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
665 MAXWLEN + 1);
666 mi.mi_fwordlen = STRLEN(mi.mi_fword);
667
Bram Moolenaar51485f02005-06-04 21:55:20 +0000668 /* The word is bad unless we recognize it. */
669 mi.mi_result = SP_BAD;
670
671 /*
672 * Loop over the languages specified in 'spelllang'.
673 * We check them all, because a matching word may be longer than an
674 * already found matching word.
675 */
676 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000677 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000678 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000679 /* Check for a matching word in case-folded words. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000680 find_word(&mi, FIND_FOLDWORD);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000681
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000682 /* Check for a matching word in keep-case words. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000683 find_word(&mi, FIND_KEEPWORD);
684
685 /* Check for matching prefixes. */
686 find_prefix(&mi);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000687 }
688
Bram Moolenaar51485f02005-06-04 21:55:20 +0000689 if (mi.mi_result != SP_OK)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000690 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000691 /* When we are at a non-word character there is no error, just
692 * skip over the character (try looking for a word after it). */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000693 if (!SPELL_ISWORDP(ptr))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000694 {
695#ifdef FEAT_MBYTE
696 if (has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000697 return mb_ptr2len_check(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000698#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +0000699 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000700 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000701
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000702 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000703 *attrp = highlight_attr[HLF_SPB];
704 else if (mi.mi_result == SP_RARE)
705 *attrp = highlight_attr[HLF_SPR];
706 else
707 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000708 }
709 }
710
Bram Moolenaar51485f02005-06-04 21:55:20 +0000711 return (int)(mi.mi_end - ptr);
712}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000713
Bram Moolenaar51485f02005-06-04 21:55:20 +0000714/*
715 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000716 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
717 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
718 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
719 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000720 *
721 * For a match mip->mi_result is updated.
722 */
723 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000724find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000725 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000726 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000727{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000728 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000729 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000730 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000731 int endidxcnt = 0;
732 int len;
733 int wlen = 0;
734 int flen;
735 int c;
736 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000737 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000738#ifdef FEAT_MBYTE
739 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000740 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000741#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000742 int res = SP_BAD;
743 int valid;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000744 slang_T *slang = mip->mi_lp->lp_slang;
745 unsigned flags;
746 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000747 idx_T *idxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000748 int prefcnt;
749 int pidx;
750 regmatch_T regmatch;
751 regprog_T *rp;
752 int prefid;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000753
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000754 if (mode == FIND_KEEPWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000755 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000756 /* Check for word with matching case in keep-case tree. */
757 ptr = mip->mi_word;
758 flen = 9999; /* no case folding, always enough bytes */
759 byts = slang->sl_kbyts;
760 idxs = slang->sl_kidxs;
761 }
762 else
763 {
764 /* Check for case-folded in case-folded tree. */
765 ptr = mip->mi_fword;
766 flen = mip->mi_fwordlen; /* available case-folded bytes */
767 byts = slang->sl_fbyts;
768 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000769
770 if (mode == FIND_PREFIX)
771 {
772 /* Skip over the prefix. */
773 wlen = mip->mi_prefixlen;
774 flen -= mip->mi_prefixlen;
775 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000776 }
777
Bram Moolenaar51485f02005-06-04 21:55:20 +0000778 if (byts == NULL)
779 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000780
Bram Moolenaar51485f02005-06-04 21:55:20 +0000781 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000782 * Repeat advancing in the tree until:
783 * - there is a byte that doesn't match,
784 * - we reach the end of the tree,
785 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000786 */
787 for (;;)
788 {
789 if (flen == 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000790 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000791
792 len = byts[arridx++];
793
794 /* If the first possible byte is a zero the word could end here.
795 * Remember this index, we first check for the longest word. */
796 if (byts[arridx] == 0)
797 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000798 if (endidxcnt == MAXWLEN)
799 {
800 /* Must be a corrupted spell file. */
801 EMSG(_(e_format));
802 return;
803 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000804 endlen[endidxcnt] = wlen;
805 endidx[endidxcnt++] = arridx++;
806 --len;
807
808 /* Skip over the zeros, there can be several flag/region
809 * combinations. */
810 while (len > 0 && byts[arridx] == 0)
811 {
812 ++arridx;
813 --len;
814 }
815 if (len == 0)
816 break; /* no children, word must end here */
817 }
818
819 /* Stop looking at end of the line. */
820 if (ptr[wlen] == NUL)
821 break;
822
823 /* Perform a binary search in the list of accepted bytes. */
824 c = ptr[wlen];
825 lo = arridx;
826 hi = arridx + len - 1;
827 while (lo < hi)
828 {
829 m = (lo + hi) / 2;
830 if (byts[m] > c)
831 hi = m - 1;
832 else if (byts[m] < c)
833 lo = m + 1;
834 else
835 {
836 lo = hi = m;
837 break;
838 }
839 }
840
841 /* Stop if there is no matching byte. */
842 if (hi < lo || byts[lo] != c)
843 break;
844
845 /* Continue at the child (if there is one). */
846 arridx = idxs[lo];
847 ++wlen;
848 --flen;
849 }
850
851 /*
852 * Verify that one of the possible endings is valid. Try the longest
853 * first.
854 */
855 while (endidxcnt > 0)
856 {
857 --endidxcnt;
858 arridx = endidx[endidxcnt];
859 wlen = endlen[endidxcnt];
860
861#ifdef FEAT_MBYTE
862 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
863 continue; /* not at first byte of character */
864#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000865 if (SPELL_ISWORDP(ptr + wlen))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000866 continue; /* next char is a word character */
867
868#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000869 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000870 {
871 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000872 * when folding case. This can be slow, take a shortcut when the
873 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000874 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000875 if (STRNCMP(ptr, p, wlen) != 0)
876 {
877 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
878 mb_ptr_adv(p);
879 wlen = p - mip->mi_word;
880 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000881 }
882#endif
883
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000884 /* Check flags and region. For FIND_PREFIX check the condition and
885 * prefix ID.
886 * Repeat this if there are more flags/region alternatives until there
887 * is a match. */
888 res = SP_BAD;
889 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
890 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000891 {
892 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000893
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000894 /* For the fold-case tree check that the case of the checked word
895 * matches with what the word in the tree requires.
896 * For keep-case tree the case is always right. For prefixes we
897 * don't bother to check. */
898 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000899 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000900 if (mip->mi_cend != mip->mi_word + wlen)
901 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000902 /* mi_capflags was set for a different word length, need
903 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000904 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000905 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000906 }
907
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000908 if (!spell_valid_case(mip->mi_capflags, flags))
909 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000910 }
911
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000912 /* When mode is FIND_PREFIX the word must support the prefix:
913 * check the prefix ID and the condition. Do that for the list at
914 * mip->mi_prefarridx. */
915 if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000916 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000917 /* The prefix ID is stored two bytes above the flags. */
918 prefid = (unsigned)flags >> 16;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000919
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000920 valid = FALSE;
921 for (prefcnt = mip->mi_prefcnt - 1; prefcnt >= 0; --prefcnt)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000922 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000923 pidx = slang->sl_pidxs[mip->mi_prefarridx + prefcnt];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000924
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000925 /* Check the prefix ID. */
926 if (prefid != (pidx & 0xff))
927 continue;
928
929 /* Check the condition, if there is one. The
930 * condition index is stored above the prefix ID byte.
931 */
932 rp = slang->sl_prefprog[(unsigned)pidx >> 8];
933 if (rp != NULL)
934 {
935 regmatch.regprog = rp;
936 regmatch.rm_ic = FALSE;
937 if (!vim_regexec(&regmatch, mip->mi_fword,
938 (colnr_T)mip->mi_prefixlen))
939 continue;
940 }
941
942 /* It's a match, use it. */
943 valid = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000944 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000945 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000946
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000947 if (!valid)
948 continue;
949 }
950
951 if (flags & WF_BANNED)
952 res = SP_BANNED;
953 else if (flags & WF_REGION)
954 {
955 /* Check region. */
956 if ((mip->mi_lp->lp_region & (flags >> 8)) != 0)
957 res = SP_OK;
958 else
959 res = SP_LOCAL;
960 }
961 else if (flags & WF_RARE)
962 res = SP_RARE;
963 else
964 res = SP_OK;
965
966 /* Always use the longest match and the best result. */
967 if (mip->mi_result > res)
968 {
969 mip->mi_result = res;
970 mip->mi_end = mip->mi_word + wlen;
971 }
972 else if (mip->mi_result == res
973 && mip->mi_end < mip->mi_word + wlen)
974 mip->mi_end = mip->mi_word + wlen;
975
976 if (res == SP_OK)
977 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000978 }
979
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000980 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000981 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000982 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000983}
984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000985/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000986 * Check if the word at "mip->mi_word" has a matching prefix.
987 * If it does, then check the following word.
988 *
989 * For a match mip->mi_result is updated.
990 */
991 static void
992find_prefix(mip)
993 matchinf_T *mip;
994{
995 idx_T arridx = 0;
996 int len;
997 int wlen = 0;
998 int flen;
999 int c;
1000 char_u *ptr;
1001 idx_T lo, hi, m;
1002 slang_T *slang = mip->mi_lp->lp_slang;
1003 char_u *byts;
1004 idx_T *idxs;
1005
1006 /* We use the case-folded word here, since prefixes are always
1007 * case-folded. */
1008 ptr = mip->mi_fword;
1009 flen = mip->mi_fwordlen; /* available case-folded bytes */
1010 byts = slang->sl_pbyts;
1011 idxs = slang->sl_pidxs;
1012
1013 if (byts == NULL)
1014 return; /* array is empty */
1015
1016 /*
1017 * Repeat advancing in the tree until:
1018 * - there is a byte that doesn't match,
1019 * - we reach the end of the tree,
1020 * - or we reach the end of the line.
1021 */
1022 for (;;)
1023 {
1024 if (flen == 0 && *mip->mi_fend != NUL)
1025 flen = fold_more(mip);
1026
1027 len = byts[arridx++];
1028
1029 /* If the first possible byte is a zero the prefix could end here.
1030 * Check if the following word matches and supports the prefix. */
1031 if (byts[arridx] == 0)
1032 {
1033 /* There can be several prefixes with different conditions. We
1034 * try them all, since we don't know which one will give the
1035 * longest match. The word is the same each time, pass the list
1036 * of possible prefixes to find_word(). */
1037 mip->mi_prefarridx = arridx;
1038 mip->mi_prefcnt = len;
1039 while (len > 0 && byts[arridx] == 0)
1040 {
1041 ++arridx;
1042 --len;
1043 }
1044 mip->mi_prefcnt -= len;
1045
1046 /* Find the word that comes after the prefix. */
1047 mip->mi_prefixlen = wlen;
1048 find_word(mip, FIND_PREFIX);
1049
1050
1051 if (len == 0)
1052 break; /* no children, word must end here */
1053 }
1054
1055 /* Stop looking at end of the line. */
1056 if (ptr[wlen] == NUL)
1057 break;
1058
1059 /* Perform a binary search in the list of accepted bytes. */
1060 c = ptr[wlen];
1061 lo = arridx;
1062 hi = arridx + len - 1;
1063 while (lo < hi)
1064 {
1065 m = (lo + hi) / 2;
1066 if (byts[m] > c)
1067 hi = m - 1;
1068 else if (byts[m] < c)
1069 lo = m + 1;
1070 else
1071 {
1072 lo = hi = m;
1073 break;
1074 }
1075 }
1076
1077 /* Stop if there is no matching byte. */
1078 if (hi < lo || byts[lo] != c)
1079 break;
1080
1081 /* Continue at the child (if there is one). */
1082 arridx = idxs[lo];
1083 ++wlen;
1084 --flen;
1085 }
1086}
1087
1088/*
1089 * Need to fold at least one more character. Do until next non-word character
1090 * for efficiency.
1091 * Return the length of the folded chars in bytes.
1092 */
1093 static int
1094fold_more(mip)
1095 matchinf_T *mip;
1096{
1097 int flen;
1098 char_u *p;
1099
1100 p = mip->mi_fend;
1101 do
1102 {
1103 mb_ptr_adv(mip->mi_fend);
1104 } while (*mip->mi_fend != NUL && SPELL_ISWORDP(mip->mi_fend));
1105
1106 /* Include the non-word character so that we can check for the
1107 * word end. */
1108 if (*mip->mi_fend != NUL)
1109 mb_ptr_adv(mip->mi_fend);
1110
1111 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1112 mip->mi_fword + mip->mi_fwordlen,
1113 MAXWLEN - mip->mi_fwordlen);
1114 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1115 mip->mi_fwordlen += flen;
1116 return flen;
1117}
1118
1119/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001120 * Check case flags for a word. Return TRUE if the word has the requested
1121 * case.
1122 */
1123 static int
1124spell_valid_case(origflags, treeflags)
1125 int origflags; /* flags for the checked word. */
1126 int treeflags; /* flags for the word in the spell tree */
1127{
1128 return (origflags == WF_ALLCAP
1129 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
1130 && ((treeflags & WF_ONECAP) == 0 || origflags == WF_ONECAP)));
1131}
1132
Bram Moolenaar51485f02005-06-04 21:55:20 +00001133
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001134/*
1135 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001136 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001137 * Return OK if found, FAIL otherwise.
1138 */
1139 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001140spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001141 int dir; /* FORWARD or BACKWARD */
1142 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001143 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001144{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001145 linenr_T lnum;
1146 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001147 char_u *line;
1148 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001149 int attr = 0;
1150 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001151 int has_syntax = syntax_present(curbuf);
1152 int col;
1153 int can_spell;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001154
Bram Moolenaarb765d632005-06-07 21:00:02 +00001155 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001156 {
1157 EMSG(_("E756: Spell checking not enabled"));
1158 return FAIL;
1159 }
1160
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001161 /*
1162 * Start looking for bad word at the start of the line, because we can't
1163 * start halfway a word, we don't know where it starts or ends.
1164 *
1165 * When searching backwards, we continue in the line to find the last
1166 * bad word (in the cursor line: before the cursor).
1167 */
1168 lnum = curwin->w_cursor.lnum;
1169 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001170
1171 while (!got_int)
1172 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001173 line = ml_get(lnum);
1174 p = line;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001175
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001176 while (*p != NUL)
1177 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001178 /* When searching backward don't search after the cursor. */
1179 if (dir == BACKWARD
1180 && lnum == curwin->w_cursor.lnum
1181 && (colnr_T)(p - line) >= curwin->w_cursor.col)
1182 break;
1183
1184 /* start of word */
1185 len = spell_check(curwin, p, &attr);
1186
1187 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001188 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001189 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001190 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001191 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001192 /* When searching forward only accept a bad word after
1193 * the cursor. */
1194 if (dir == BACKWARD
1195 || lnum > curwin->w_cursor.lnum
1196 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001197 && (colnr_T)(curline ? p - line + len
1198 : p - line)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001199 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001200 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001201 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001202 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001203 col = p - line;
1204 (void)syn_get_id(lnum, (colnr_T)col,
1205 FALSE, &can_spell);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001206
Bram Moolenaar51485f02005-06-04 21:55:20 +00001207 /* have to get the line again, a multi-line
1208 * regexp may make it invalid */
1209 line = ml_get(lnum);
1210 p = line + col;
1211 }
1212 else
1213 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001214
Bram Moolenaar51485f02005-06-04 21:55:20 +00001215 if (can_spell)
1216 {
1217 found_pos.lnum = lnum;
1218 found_pos.col = p - line;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001219#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001220 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001221#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001222 if (dir == FORWARD)
1223 {
1224 /* No need to search further. */
1225 curwin->w_cursor = found_pos;
1226 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001227 }
1228 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001229 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001230 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001231 attr = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001232 }
1233
Bram Moolenaar51485f02005-06-04 21:55:20 +00001234 /* advance to character after the word */
1235 p += len;
1236 if (*p == NUL)
1237 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001238 }
1239
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001240 if (curline)
1241 return FAIL; /* only check cursor line */
1242
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001243 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001244 if (dir == BACKWARD)
1245 {
1246 if (found_pos.lnum != 0)
1247 {
1248 /* Use the last match in the line. */
1249 curwin->w_cursor = found_pos;
1250 return OK;
1251 }
1252 if (lnum == 1)
1253 return FAIL;
1254 --lnum;
1255 }
1256 else
1257 {
1258 if (lnum == curbuf->b_ml.ml_line_count)
1259 return FAIL;
1260 ++lnum;
1261 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001262
1263 line_breakcheck();
1264 }
1265
1266 return FAIL; /* interrupted */
1267}
1268
1269/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001270 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001271 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001272 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001273 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001274spell_load_lang(lang)
1275 char_u *lang;
1276{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001277 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001278 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001279 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001280
Bram Moolenaarb765d632005-06-07 21:00:02 +00001281 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001282 * It's truncated when an error is detected. */
1283 STRCPY(langcp, lang);
1284
Bram Moolenaarb765d632005-06-07 21:00:02 +00001285 /*
1286 * Find the first spell file for "lang" in 'runtimepath' and load it.
1287 */
1288 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1289 "spell/%s.%s.spl", lang, spell_enc());
1290 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001291
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001292 if (r == FAIL && *langcp != NUL)
1293 {
1294 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001295 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001296 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001297 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001298 }
1299
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001300 if (r == FAIL)
1301 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1302 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001303 else if (*langcp != NUL)
1304 {
1305 /* Load all the additions. */
1306 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1307 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1308 }
1309}
1310
1311/*
1312 * Return the encoding used for spell checking: Use 'encoding', except that we
1313 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1314 */
1315 static char_u *
1316spell_enc()
1317{
1318
1319#ifdef FEAT_MBYTE
1320 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1321 return p_enc;
1322#endif
1323 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001324}
1325
1326/*
1327 * Allocate a new slang_T.
1328 * Caller must fill "sl_next".
1329 */
1330 static slang_T *
1331slang_alloc(lang)
1332 char_u *lang;
1333{
1334 slang_T *lp;
1335
Bram Moolenaar51485f02005-06-04 21:55:20 +00001336 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001337 if (lp != NULL)
1338 {
1339 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001340 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
1341 ga_init2(&lp->sl_sal, sizeof(fromto_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001342 }
1343 return lp;
1344}
1345
1346/*
1347 * Free the contents of an slang_T and the structure itself.
1348 */
1349 static void
1350slang_free(lp)
1351 slang_T *lp;
1352{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001353 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001354 vim_free(lp->sl_fname);
1355 slang_clear(lp);
1356 vim_free(lp);
1357}
1358
1359/*
1360 * Clear an slang_T so that the file can be reloaded.
1361 */
1362 static void
1363slang_clear(lp)
1364 slang_T *lp;
1365{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001366 garray_T *gap;
1367 fromto_T *ftp;
1368 int round;
1369 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001370
Bram Moolenaar51485f02005-06-04 21:55:20 +00001371 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001372 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001373 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001374 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001375 vim_free(lp->sl_pbyts);
1376 lp->sl_pbyts = NULL;
1377
Bram Moolenaar51485f02005-06-04 21:55:20 +00001378 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001379 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001380 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001381 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001382 vim_free(lp->sl_pidxs);
1383 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001384
1385 for (round = 1; round <= 2; ++round)
1386 {
1387 gap = round == 1 ? &lp->sl_rep : &lp->sl_sal;
1388 while (gap->ga_len > 0)
1389 {
1390 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1391 vim_free(ftp->ft_from);
1392 vim_free(ftp->ft_to);
1393 }
1394 ga_clear(gap);
1395 }
1396
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001397 for (i = 0; i < lp->sl_prefixcnt; ++i)
1398 vim_free(lp->sl_prefprog[i]);
1399 vim_free(lp->sl_prefprog);
1400
Bram Moolenaarea424162005-06-16 21:51:00 +00001401#ifdef FEAT_MBYTE
1402 {
1403 int todo = lp->sl_map_hash.ht_used;
1404 hashitem_T *hi;
1405
1406 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1407 if (!HASHITEM_EMPTY(hi))
1408 {
1409 --todo;
1410 vim_free(hi->hi_key);
1411 }
1412 }
1413 hash_clear(&lp->sl_map_hash);
1414#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001415}
1416
1417/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001418 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001419 * Invoked through do_in_runtimepath().
1420 */
1421 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001422spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001423 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001424 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001425{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001426 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001427}
1428
1429/*
1430 * Load one spell file and store the info into a slang_T.
1431 *
1432 * This is invoked in two ways:
1433 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1434 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1435 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1436 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001437 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001438 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001439 static slang_T *
1440spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001441 char_u *fname;
1442 char_u *lang;
1443 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001444 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001445{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001446 FILE *fd;
1447 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001448 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001449 char_u *bp;
1450 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001451 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001452 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001453 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001454 int round;
1455 char_u *save_sourcing_name = sourcing_name;
1456 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001457 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001458 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001459 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001460 garray_T *gap;
1461 fromto_T *ftp;
1462 int rr;
1463 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001464 idx_T idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001465
Bram Moolenaarb765d632005-06-07 21:00:02 +00001466 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001467 if (fd == NULL)
1468 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001469 if (!silent)
1470 EMSG2(_(e_notopen), fname);
1471 else if (p_verbose > 2)
1472 {
1473 verbose_enter();
1474 smsg((char_u *)e_notopen, fname);
1475 verbose_leave();
1476 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001477 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001478 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001479 if (p_verbose > 2)
1480 {
1481 verbose_enter();
1482 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1483 verbose_leave();
1484 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001485
Bram Moolenaarb765d632005-06-07 21:00:02 +00001486 if (old_lp == NULL)
1487 {
1488 lp = slang_alloc(lang);
1489 if (lp == NULL)
1490 goto endFAIL;
1491
1492 /* Remember the file name, used to reload the file when it's updated. */
1493 lp->sl_fname = vim_strsave(fname);
1494 if (lp->sl_fname == NULL)
1495 goto endFAIL;
1496
1497 /* Check for .add.spl. */
1498 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1499 }
1500 else
1501 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001502
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001503 /* Set sourcing_name, so that error messages mention the file name. */
1504 sourcing_name = fname;
1505 sourcing_lnum = 0;
1506
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001507 /* <HEADER>: <fileID>
1508 * <regioncnt> <regionname> ...
1509 * <charflagslen> <charflags>
1510 * <fcharslen> <fchars>
1511 * <prefcondcnt> <prefcond> ...
1512 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001513 for (i = 0; i < VIMSPELLMAGICL; ++i)
1514 buf[i] = getc(fd); /* <fileID> */
1515 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1516 {
1517 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001518 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001519 }
1520
1521 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001522 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001523 {
1524truncerr:
1525 EMSG(_("E758: Truncated spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001526 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001527 }
1528 if (cnt > 8)
1529 {
1530formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001531 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001532 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001533 }
1534 for (i = 0; i < cnt; ++i)
1535 {
1536 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1537 lp->sl_regions[i * 2 + 1] = getc(fd);
1538 }
1539 lp->sl_regions[cnt * 2] = NUL;
1540
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001541 cnt = getc(fd); /* <charflagslen> */
1542 if (cnt > 0)
1543 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001544 p = alloc((unsigned)cnt);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001545 if (p == NULL)
1546 goto endFAIL;
1547 for (i = 0; i < cnt; ++i)
1548 p[i] = getc(fd); /* <charflags> */
1549
1550 ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */
1551 if (ccnt <= 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001552 {
1553 vim_free(p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001554 goto formerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001555 }
1556 fol = alloc((unsigned)ccnt + 1);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001557 if (fol == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001558 {
1559 vim_free(p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001560 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001561 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001562 for (i = 0; i < ccnt; ++i)
1563 fol[i] = getc(fd); /* <fchars> */
1564 fol[i] = NUL;
1565
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001566 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001567 i = set_spell_charflags(p, cnt, fol);
1568 vim_free(p);
1569 vim_free(fol);
1570 if (i == FAIL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001571 goto formerr;
1572 }
1573 else
1574 {
1575 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1576 cnt = (getc(fd) << 8) + getc(fd);
1577 if (cnt != 0)
1578 goto formerr;
1579 }
1580
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001581 /* <prefcondcnt> <prefcond> ... */
1582 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
1583 if (cnt > 0)
1584 {
1585 lp->sl_prefprog = (regprog_T **)alloc_clear(
1586 (unsigned)sizeof(regprog_T *) * cnt);
1587 if (lp->sl_prefprog == NULL)
1588 goto endFAIL;
1589 lp->sl_prefixcnt = cnt;
1590
1591 for (i = 0; i < cnt; ++i)
1592 {
1593 /* <prefcond> : <condlen> <condstr> */
1594 n = getc(fd); /* <condlen> */
1595 if (n < 0)
1596 goto formerr;
1597 /* When <condlen> is zero we have an empty condition. Otherwise
1598 * compile the regexp program used to check for the condition. */
1599 if (n > 0)
1600 {
1601 p = buf;
1602 while (n-- > 0)
1603 *p++ = getc(fd); /* <condstr> */
1604 *p = NUL;
1605 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
1606 }
1607 }
1608 }
1609
1610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001611 /* <SUGGEST> : <repcount> <rep> ...
1612 * <salflags> <salcount> <sal> ...
1613 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001614
1615 /*
1616 * round 1: rep items
1617 * round 2: sal items
1618 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001619 for (round = 1; round <= 2; ++round)
1620 {
1621 if (round == 1)
1622 {
1623 gap = &lp->sl_rep;
1624 first = lp->sl_rep_first;
1625 }
1626 else
1627 {
1628 gap = &lp->sl_sal;
1629 first = lp->sl_sal_first;
1630
1631 i = getc(fd); /* <salflags> */
1632 if (i & SAL_F0LLOWUP)
1633 lp->sl_followup = TRUE;
1634 if (i & SAL_COLLAPSE)
1635 lp->sl_collapse = TRUE;
1636 if (i & SAL_REM_ACCENTS)
1637 lp->sl_rem_accents = TRUE;
1638 }
1639
1640 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> or <salcount> */
1641 if (cnt < 0)
1642 goto formerr;
1643
1644 if (ga_grow(gap, cnt) == FAIL)
1645 goto endFAIL;
1646 for (; gap->ga_len < cnt; ++gap->ga_len)
1647 {
1648 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
1649 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
1650 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
1651 for (rr = 1; rr <= 2; ++rr)
1652 {
1653 ccnt = getc(fd);
1654 if (ccnt < 0)
1655 {
1656 if (rr == 2)
1657 vim_free(ftp->ft_from);
1658 goto formerr;
1659 }
1660 if ((p = alloc(ccnt + 1)) == NULL)
1661 {
1662 if (rr == 2)
1663 vim_free(ftp->ft_from);
1664 goto endFAIL;
1665 }
1666 for (i = 0; i < ccnt; ++i)
1667 p[i] = getc(fd); /* <repfrom> or <salfrom> */
1668 p[i] = NUL;
1669 if (rr == 1)
1670 ftp->ft_from = p;
1671 else
1672 ftp->ft_to = p;
1673 }
1674 }
1675
1676 /* Fill the first-index table. */
1677 for (i = 0; i < 256; ++i)
1678 first[i] = -1;
1679 for (i = 0; i < gap->ga_len; ++i)
1680 {
1681 ftp = &((fromto_T *)gap->ga_data)[i];
1682 if (first[*ftp->ft_from] == -1)
1683 first[*ftp->ft_from] = i;
1684 }
1685 }
1686
1687 cnt = (getc(fd) << 8) + getc(fd); /* <maplen> */
1688 if (cnt < 0)
1689 goto formerr;
1690 p = alloc(cnt + 1);
1691 if (p == NULL)
1692 goto endFAIL;
1693 for (i = 0; i < cnt; ++i)
1694 p[i] = getc(fd); /* <mapstr> */
1695 p[i] = NUL;
Bram Moolenaarea424162005-06-16 21:51:00 +00001696 set_map_str(lp, p);
1697 vim_free(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001698
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001699
Bram Moolenaar51485f02005-06-04 21:55:20 +00001700 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001701 * round 2: <KWORDTREE>
1702 * round 3: <PREFIXTREE> */
1703 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001704 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001705 /* The tree size was computed when writing the file, so that we can
1706 * allocate it as one long block. <nodecount> */
1707 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
1708 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001709 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001710 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001711 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001712 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001713 bp = lalloc((long_u)len, TRUE);
1714 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001715 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001716 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001717 lp->sl_fbyts = bp;
1718 else if (round == 2)
1719 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001720 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001721 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001722
1723 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001724 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
1725 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001726 goto endFAIL;
1727 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001728 lp->sl_fidxs = ip;
1729 else if (round == 2)
1730 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001731 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001732 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001733
1734 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001735 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001736 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001737 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001738 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001739 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001740 }
1741 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001742
Bram Moolenaarb765d632005-06-07 21:00:02 +00001743 /* For a new file link it in the list of spell files. */
1744 if (old_lp == NULL)
1745 {
1746 lp->sl_next = first_lang;
1747 first_lang = lp;
1748 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001749
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001750 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001751
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001752endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00001753 if (lang != NULL)
1754 /* truncating the name signals the error to spell_load_lang() */
1755 *lang = NUL;
1756 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001757 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001758 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001759 lp = NULL;
1760 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001761
1762endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001763 if (fd != NULL)
1764 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001765 sourcing_name = save_sourcing_name;
1766 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001767
1768 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001769}
1770
1771/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00001772 * Read one row of siblings from the spell file and store it in the byte array
1773 * "byts" and index array "idxs". Recursively read the children.
1774 *
1775 * NOTE: The code here must match put_tree().
1776 *
1777 * Returns the index follosing the siblings.
1778 * Returns -1 if the file is shorter than expected.
1779 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001780 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001781 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001782read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001783 FILE *fd;
1784 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001785 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001786 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001787 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001788 int prefixtree; /* TRUE for reading PREFIXTREE */
1789 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001790{
Bram Moolenaar51485f02005-06-04 21:55:20 +00001791 int len;
1792 int i;
1793 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001794 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001795 int c;
1796#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001797
Bram Moolenaar51485f02005-06-04 21:55:20 +00001798 len = getc(fd); /* <siblingcount> */
1799 if (len <= 0)
1800 return -1;
1801
1802 if (startidx + len >= maxidx)
1803 return -2;
1804 byts[idx++] = len;
1805
1806 /* Read the byte values, flag/region bytes and shared indexes. */
1807 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001808 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001809 c = getc(fd); /* <byte> */
1810 if (c < 0)
1811 return -1;
1812 if (c <= BY_SPECIAL)
1813 {
1814 if (c == BY_NOFLAGS)
1815 {
1816 /* No flags, all regions. */
1817 idxs[idx] = 0;
1818 c = 0;
1819 }
1820 else if (c == BY_FLAGS)
1821 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001822 if (prefixtree)
1823 {
1824 /* Read the prefix ID and the condition nr. In idxs[]
1825 * store the prefix ID in the low byte, the condition
1826 * index shifted up 8 bits. */
1827 c = getc(fd); /* <prefixID> */
1828 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
1829 if (n >= maxprefcondnr)
1830 return -2;
1831 c = (n << 8) + c;
1832 }
1833 else
1834 {
1835 /* Read flags and optional region and prefix ID. In
1836 * idxs[] the flags go in the low byte, region above that
1837 * and prefix ID above the region. */
1838 c = getc(fd); /* <flags> */
1839 if (c & WF_REGION)
1840 c = (getc(fd) << 8) + c; /* <region> */
1841 if (c & WF_PFX)
1842 c = (getc(fd) << 16) + c; /* <prefixID> */
1843 }
1844
Bram Moolenaar51485f02005-06-04 21:55:20 +00001845 idxs[idx] = c;
1846 c = 0;
1847 }
1848 else /* c == BY_INDEX */
1849 {
1850 /* <nodeidx> */
1851 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
1852 if (n < 0 || n >= maxidx)
1853 return -2;
1854 idxs[idx] = n + SHARED_MASK;
1855 c = getc(fd); /* <xbyte> */
1856 }
1857 }
1858 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001859 }
1860
Bram Moolenaar51485f02005-06-04 21:55:20 +00001861 /* Recursively read the children for non-shared siblings.
1862 * Skip the end-of-word ones (zero byte value) and the shared ones (and
1863 * remove SHARED_MASK) */
1864 for (i = 1; i <= len; ++i)
1865 if (byts[startidx + i] != 0)
1866 {
1867 if (idxs[startidx + i] & SHARED_MASK)
1868 idxs[startidx + i] &= ~SHARED_MASK;
1869 else
1870 {
1871 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001872 idx = read_tree(fd, byts, idxs, maxidx, idx,
1873 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001874 if (idx < 0)
1875 break;
1876 }
1877 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001878
Bram Moolenaar51485f02005-06-04 21:55:20 +00001879 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001880}
1881
1882/*
1883 * Parse 'spelllang' and set buf->b_langp accordingly.
1884 * Returns an error message or NULL.
1885 */
1886 char_u *
1887did_set_spelllang(buf)
1888 buf_T *buf;
1889{
1890 garray_T ga;
1891 char_u *lang;
1892 char_u *e;
1893 char_u *region;
1894 int region_mask;
1895 slang_T *lp;
1896 int c;
1897 char_u lbuf[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001898 char_u spf_name[MAXPATHL];
1899 int did_spf = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001900
1901 ga_init2(&ga, sizeof(langp_T), 2);
1902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001903 /* Get the name of the .spl file associated with 'spellfile'. */
1904 if (*buf->b_p_spf == NUL)
1905 did_spf = TRUE;
1906 else
1907 vim_snprintf((char *)spf_name, sizeof(spf_name), "%s.spl",
1908 buf->b_p_spf);
1909
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001910 /* loop over comma separated languages. */
1911 for (lang = buf->b_p_spl; *lang != NUL; lang = e)
1912 {
1913 e = vim_strchr(lang, ',');
1914 if (e == NULL)
1915 e = lang + STRLEN(lang);
Bram Moolenaar5482f332005-04-17 20:18:43 +00001916 region = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001917 if (e > lang + 2)
1918 {
1919 if (e - lang >= MAXWLEN)
1920 {
1921 ga_clear(&ga);
1922 return e_invarg;
1923 }
1924 if (lang[2] == '_')
1925 region = lang + 3;
1926 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001927
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001928 /* Check if we loaded this language before. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001929 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
1930 if (STRNICMP(lp->sl_name, lang, 2) == 0)
1931 break;
1932
1933 if (lp == NULL)
1934 {
1935 /* Not found, load the language. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001936 vim_strncpy(lbuf, lang, e - lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001937 if (region != NULL)
1938 mch_memmove(lbuf + 2, lbuf + 5, e - lang - 4);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001939 spell_load_lang(lbuf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001940 }
1941
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001942 /*
1943 * Loop over the languages, there can be several files for each.
1944 */
1945 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
1946 if (STRNICMP(lp->sl_name, lang, 2) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001947 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001948 region_mask = REGION_ALL;
1949 if (region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001950 {
1951 /* find region in sl_regions */
1952 c = find_region(lp->sl_regions, region);
1953 if (c == REGION_ALL)
1954 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001955 if (!lp->sl_add)
1956 {
1957 c = *e;
1958 *e = NUL;
1959 smsg((char_u *)_("Warning: region %s not supported"),
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001960 lang);
Bram Moolenaar3982c542005-06-08 21:56:31 +00001961 *e = c;
1962 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001963 }
1964 else
1965 region_mask = 1 << c;
1966 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001967
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001968 if (ga_grow(&ga, 1) == FAIL)
1969 {
1970 ga_clear(&ga);
1971 return e_outofmem;
1972 }
1973 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
1974 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
1975 ++ga.ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001976
1977 /* Check if this is the 'spellfile' spell file. */
1978 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
1979 did_spf = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001980 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001981
1982 if (*e == ',')
1983 ++e;
1984 }
1985
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001986 /*
1987 * Make sure the 'spellfile' file is loaded. It may be in 'runtimepath',
1988 * then it's probably loaded above already. Otherwise load it here.
1989 */
1990 if (!did_spf)
1991 {
1992 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
1993 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
1994 break;
1995 if (lp == NULL)
1996 {
1997 vim_strncpy(lbuf, gettail(spf_name), 2);
1998 lp = spell_load_file(spf_name, lbuf, NULL, TRUE);
1999 }
2000 if (lp != NULL && ga_grow(&ga, 1) == OK)
2001 {
2002 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2003 LANGP_ENTRY(ga, ga.ga_len)->lp_region = REGION_ALL;
2004 ++ga.ga_len;
2005 }
2006 }
2007
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002008 /* Add a NULL entry to mark the end of the list. */
2009 if (ga_grow(&ga, 1) == FAIL)
2010 {
2011 ga_clear(&ga);
2012 return e_outofmem;
2013 }
2014 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2015 ++ga.ga_len;
2016
2017 /* Everything is fine, store the new b_langp value. */
2018 ga_clear(&buf->b_langp);
2019 buf->b_langp = ga;
2020
2021 return NULL;
2022}
2023
2024/*
2025 * Find the region "region[2]" in "rp" (points to "sl_regions").
2026 * Each region is simply stored as the two characters of it's name.
2027 * Returns the index if found, REGION_ALL if not found.
2028 */
2029 static int
2030find_region(rp, region)
2031 char_u *rp;
2032 char_u *region;
2033{
2034 int i;
2035
2036 for (i = 0; ; i += 2)
2037 {
2038 if (rp[i] == NUL)
2039 return REGION_ALL;
2040 if (rp[i] == region[0] && rp[i + 1] == region[1])
2041 break;
2042 }
2043 return i / 2;
2044}
2045
2046/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002047 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002048 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002049 * Word WF_ONECAP
2050 * W WORD WF_ALLCAP
2051 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002052 */
2053 static int
2054captype(word, end)
2055 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002056 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002057{
2058 char_u *p;
2059 int c;
2060 int firstcap;
2061 int allcap;
2062 int past_second = FALSE; /* past second word char */
2063
2064 /* find first letter */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002065 for (p = word; !SPELL_ISWORDP(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002066 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002067 return 0; /* only non-word characters, illegal word */
2068#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002069 if (has_mbyte)
2070 c = mb_ptr2char_adv(&p);
2071 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002072#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002073 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002074 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002075
2076 /*
2077 * Need to check all letters to find a word with mixed upper/lower.
2078 * But a word with an upper char only at start is a ONECAP.
2079 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002080 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002081 if (SPELL_ISWORDP(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002082 {
2083#ifdef FEAT_MBYTE
2084 c = mb_ptr2char(p);
2085#else
2086 c = *p;
2087#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002088 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002089 {
2090 /* UUl -> KEEPCAP */
2091 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002092 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002093 allcap = FALSE;
2094 }
2095 else if (!allcap)
2096 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002097 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002098 past_second = TRUE;
2099 }
2100
2101 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002102 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002103 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002104 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002105 return 0;
2106}
2107
2108# if defined(FEAT_MBYTE) || defined(PROTO)
2109/*
2110 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002111 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002112 */
2113 void
2114spell_reload()
2115{
2116 buf_T *buf;
2117 slang_T *lp;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002118 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002119
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002120 /* Initialize the table for SPELL_ISWORDP(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002121 init_spell_chartab();
2122
2123 /* Unload all allocated memory. */
2124 while (first_lang != NULL)
2125 {
2126 lp = first_lang;
2127 first_lang = lp->sl_next;
2128 slang_free(lp);
2129 }
2130
2131 /* Go through all buffers and handle 'spelllang'. */
2132 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2133 {
2134 ga_clear(&buf->b_langp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002135
2136 /* Only load the wordlists when 'spelllang' is set and there is a
2137 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002138 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002139 {
2140 FOR_ALL_WINDOWS(wp)
2141 if (wp->w_buffer == buf && wp->w_p_spell)
2142 {
2143 (void)did_set_spelllang(buf);
2144# ifdef FEAT_WINDOWS
2145 break;
2146# endif
2147 }
2148 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002149 }
2150}
2151# endif
2152
Bram Moolenaarb765d632005-06-07 21:00:02 +00002153/*
2154 * Reload the spell file "fname" if it's loaded.
2155 */
2156 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002157spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002158 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002159 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002160{
2161 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002162 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002163
Bram Moolenaarb765d632005-06-07 21:00:02 +00002164 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2165 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
2166 {
2167 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002168 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002169 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002170 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002171 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002172
2173 /* When "zg" was used and the file wasn't loaded yet, should redo
2174 * 'spelllang' to get it loaded. */
2175 if (added_word && !didit)
2176 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002177}
2178
2179
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002180/*
2181 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002182 */
2183
Bram Moolenaar51485f02005-06-04 21:55:20 +00002184#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002185 and .dic file. */
2186/*
2187 * Main structure to store the contents of a ".aff" file.
2188 */
2189typedef struct afffile_S
2190{
2191 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002192 int af_rar; /* RAR ID for rare word */
2193 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002194 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002195 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
2196 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002197} afffile_T;
2198
2199typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002200/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
2201struct affentry_S
2202{
2203 affentry_T *ae_next; /* next affix with same name/number */
2204 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
2205 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002206 char_u *ae_cond; /* condition (NULL for ".") */
2207 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002208};
2209
2210/* Affix header from ".aff" file. Used for af_pref and af_suff. */
2211typedef struct affheader_S
2212{
2213 char_u ah_key[2]; /* key for hashtable == name of affix entry */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002214 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002215 int ah_combine; /* suffix may combine with prefix */
2216 affentry_T *ah_first; /* first affix entry */
2217} affheader_T;
2218
2219#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
2220
2221/*
2222 * Structure that is used to store the items in the word tree. This avoids
2223 * the need to keep track of each allocated thing, it's freed all at once
2224 * after ":mkspell" is done.
2225 */
2226#define SBLOCKSIZE 16000 /* size of sb_data */
2227typedef struct sblock_S sblock_T;
2228struct sblock_S
2229{
2230 sblock_T *sb_next; /* next block in list */
2231 int sb_used; /* nr of bytes already in use */
2232 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002233};
2234
2235/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002236 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002237 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002238typedef struct wordnode_S wordnode_T;
2239struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002240{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002241 char_u wn_hashkey[6]; /* room for the hash key */
2242 wordnode_T *wn_next; /* next node with same hash key */
2243 wordnode_T *wn_child; /* child (next byte in word) */
2244 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
2245 always sorted) */
2246 wordnode_T *wn_wnode; /* parent node that will write this node */
2247 int wn_index; /* index in written nodes (valid after first
2248 round) */
2249 char_u wn_byte; /* Byte for this node. NUL for word end */
2250 char_u wn_flags; /* when wn_byte is NUL: WF_ flags */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002251 short wn_region; /* when wn_byte is NUL: region mask; for
2252 PREFIXTREE it's the prefcondnr */
2253 char_u wn_prefixID; /* supported/required prefix ID or 0 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002254};
2255
Bram Moolenaar51485f02005-06-04 21:55:20 +00002256#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002257
Bram Moolenaar51485f02005-06-04 21:55:20 +00002258/*
2259 * Info used while reading the spell files.
2260 */
2261typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002262{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002263 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00002264 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002265 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00002266 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002267 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002268 sblock_T *si_blocks; /* memory blocks used */
2269 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002270 int si_add; /* addition file */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002271 int si_region; /* region mask */
2272 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00002273 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002274 int si_verbose; /* verbose messages */
Bram Moolenaar3982c542005-06-08 21:56:31 +00002275 int si_region_count; /* number of regions supported (1 when there
2276 are no regions) */
2277 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002278
2279 garray_T si_rep; /* list of fromto_T entries from REP lines */
2280 garray_T si_sal; /* list of fromto_T entries from SAL lines */
2281 int si_followup; /* soundsalike: ? */
2282 int si_collapse; /* soundsalike: ? */
2283 int si_rem_accents; /* soundsalike: remove accents */
2284 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002285 garray_T si_prefcond; /* table with conditions for postponed
2286 * prefixes, each stored as a string */
2287 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002288} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002289
Bram Moolenaar51485f02005-06-04 21:55:20 +00002290static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002291static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002292static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
2293static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00002294static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00002295static void spell_free_aff __ARGS((afffile_T *aff));
2296static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002297static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp));
2298static int store_aff_word __ARGS((char_u *word, spellinfo_T *spin, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int comb, int flags, char_u *pfxlist));
Bram Moolenaar51485f02005-06-04 21:55:20 +00002299static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
2300static void *getroom __ARGS((sblock_T **blp, size_t len));
2301static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
2302static void free_blocks __ARGS((sblock_T *bl));
2303static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002304static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist));
2305static int tree_add_word __ARGS((char_u *word, wordnode_T *tree, int flags, int region, int prefixID, sblock_T **blp));
Bram Moolenaarb765d632005-06-07 21:00:02 +00002306static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00002307static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
2308static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar3982c542005-06-08 21:56:31 +00002309static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002310static int put_tree __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002311static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00002312static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002313
2314/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002315 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00002316 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002317 */
2318 static afffile_T *
Bram Moolenaar51485f02005-06-04 21:55:20 +00002319spell_read_aff(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002320 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002321 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002322{
2323 FILE *fd;
2324 afffile_T *aff;
2325 char_u rline[MAXLINELEN];
2326 char_u *line;
2327 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00002328#define MAXITEMCNT 7
2329 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002330 int itemcnt;
2331 char_u *p;
2332 int lnum = 0;
2333 affheader_T *cur_aff = NULL;
2334 int aff_todo = 0;
2335 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002336 char_u *low = NULL;
2337 char_u *fol = NULL;
2338 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002339 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002340 int do_rep;
2341 int do_sal;
2342 int do_map;
2343 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002344 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002345
Bram Moolenaar51485f02005-06-04 21:55:20 +00002346 /*
2347 * Open the file.
2348 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002349 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002350 if (fd == NULL)
2351 {
2352 EMSG2(_(e_notopen), fname);
2353 return NULL;
2354 }
2355
Bram Moolenaarb765d632005-06-07 21:00:02 +00002356 if (spin->si_verbose || p_verbose > 2)
2357 {
2358 if (!spin->si_verbose)
2359 verbose_enter();
2360 smsg((char_u *)_("Reading affix file %s..."), fname);
2361 out_flush();
2362 if (!spin->si_verbose)
2363 verbose_leave();
2364 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002365
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002366 /* Only do REP lines when not done in another .aff file already. */
2367 do_rep = spin->si_rep.ga_len == 0;
2368
2369 /* Only do SAL lines when not done in another .aff file already. */
2370 do_sal = spin->si_sal.ga_len == 0;
2371
2372 /* Only do MAP lines when not done in another .aff file already. */
2373 do_map = spin->si_map.ga_len == 0;
2374
Bram Moolenaar51485f02005-06-04 21:55:20 +00002375 /*
2376 * Allocate and init the afffile_T structure.
2377 */
2378 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002379 if (aff == NULL)
2380 return NULL;
2381 hash_init(&aff->af_pref);
2382 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002383
2384 /*
2385 * Read all the lines in the file one by one.
2386 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002387 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002388 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002389 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002390 ++lnum;
2391
2392 /* Skip comment lines. */
2393 if (*rline == '#')
2394 continue;
2395
2396 /* Convert from "SET" to 'encoding' when needed. */
2397 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002398#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00002399 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002400 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002401 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002402 if (pc == NULL)
2403 {
2404 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
2405 fname, lnum, rline);
2406 continue;
2407 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002408 line = pc;
2409 }
2410 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002411#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002412 {
2413 pc = NULL;
2414 line = rline;
2415 }
2416
2417 /* Split the line up in white separated items. Put a NUL after each
2418 * item. */
2419 itemcnt = 0;
2420 for (p = line; ; )
2421 {
2422 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
2423 ++p;
2424 if (*p == NUL)
2425 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00002426 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002427 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002428 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002429 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002430 ++p;
2431 if (*p == NUL)
2432 break;
2433 *p++ = NUL;
2434 }
2435
2436 /* Handle non-empty lines. */
2437 if (itemcnt > 0)
2438 {
2439 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
2440 && aff->af_enc == NULL)
2441 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00002442#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00002443 /* Setup for conversion from "ENC" to 'encoding'. */
2444 aff->af_enc = enc_canonize(items[1]);
2445 if (aff->af_enc != NULL && !spin->si_ascii
2446 && convert_setup(&spin->si_conv, aff->af_enc,
2447 p_enc) == FAIL)
2448 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
2449 fname, aff->af_enc, p_enc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002450#else
2451 smsg((char_u *)_("Conversion in %s not supported"), fname);
2452#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002453 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00002454 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
2455 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00002457 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002459 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002460 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002461 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002462 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
2463 && aff->af_rar == 0)
2464 {
2465 aff->af_rar = items[1][0];
2466 if (items[1][1] != NUL)
2467 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
2468 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002469 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
2470 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002471 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00002472 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002473 if (items[1][1] != NUL)
2474 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
2475 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002476 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
2477 {
2478 aff->af_pfxpostpone = TRUE;
2479 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002480 else if ((STRCMP(items[0], "PFX") == 0
2481 || STRCMP(items[0], "SFX") == 0)
2482 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00002483 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002484 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00002485 /* Myspell allows extra text after the item, but that might
2486 * mean mistakes go unnoticed. Require a comment-starter. */
2487 if (itemcnt > 4 && *items[4] != '#')
2488 smsg((char_u *)_("Trailing text in %s line %d: %s"),
2489 fname, lnum, items[4]);
2490
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002491 /* New affix letter. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002492 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
2493 sizeof(affheader_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002494 if (cur_aff == NULL)
2495 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002496 cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002497 cur_aff->ah_key[1] = NUL;
2498 if (items[1][1] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002499 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002500 if (*items[2] == 'Y')
2501 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002502 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002503 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
2504 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002505
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002506 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002507 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002508 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002509 /* Use a new number in the .spl file later, to be able to
2510 * handle multiple .aff files. */
2511 if (aff->af_pfxpostpone)
2512 cur_aff->ah_newID = spin->si_newID++;
2513 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002514 else
2515 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002516 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002517 hi = hash_find(tp, cur_aff->ah_key);
2518 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00002519 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002520 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
2521 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002522 aff_todo = 0;
2523 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002524 else
2525 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002526 }
2527 else if ((STRCMP(items[0], "PFX") == 0
2528 || STRCMP(items[0], "SFX") == 0)
2529 && aff_todo > 0
2530 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00002531 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002532 {
2533 affentry_T *aff_entry;
2534
Bram Moolenaar8db73182005-06-17 21:51:16 +00002535 /* Myspell allows extra text after the item, but that might
2536 * mean mistakes go unnoticed. Require a comment-starter. */
2537 if (itemcnt > 5 && *items[5] != '#')
2538 smsg((char_u *)_("Trailing text in %s line %d: %s"),
2539 fname, lnum, items[5]);
2540
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002541 /* New item for an affix letter. */
2542 --aff_todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002543 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
2544 sizeof(affentry_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002545 if (aff_entry == NULL)
2546 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002547
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002548 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002549 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
2550 items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002551 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002552 aff_entry->ae_add = getroom_save(&spin->si_blocks,
2553 items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002554
Bram Moolenaar51485f02005-06-04 21:55:20 +00002555 /* Don't use an affix entry with non-ASCII characters when
2556 * "spin->si_ascii" is TRUE. */
2557 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00002558 || has_non_ascii(aff_entry->ae_add)))
2559 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00002560 aff_entry->ae_next = cur_aff->ah_first;
2561 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002562
2563 if (STRCMP(items[4], ".") != 0)
2564 {
2565 char_u buf[MAXLINELEN];
2566
2567 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
2568 items[4]);
2569 if (*items[0] == 'P')
2570 sprintf((char *)buf, "^%s", items[4]);
2571 else
2572 sprintf((char *)buf, "%s$", items[4]);
2573 aff_entry->ae_prog = vim_regcomp(buf,
2574 RE_MAGIC + RE_STRING);
2575 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002576
2577 /* For postponed prefixes we need an entry in si_prefcond
2578 * for the condition. Use an existing one if possible. */
2579 if (*items[0] == 'P' && aff->af_pfxpostpone
2580 && aff_entry->ae_chop == NULL)
2581 {
2582 int idx;
2583 char_u **pp;
2584
2585 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
2586 --idx)
2587 {
2588 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
2589 if (str_equal(p, aff_entry->ae_cond))
2590 break;
2591 }
2592 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
2593 {
2594 /* Not found, add a new condition. */
2595 idx = spin->si_prefcond.ga_len++;
2596 pp = ((char_u **)spin->si_prefcond.ga_data) + idx;
2597 if (aff_entry->ae_cond == NULL)
2598 *pp = NULL;
2599 else
2600 *pp = getroom_save(&spin->si_blocks,
2601 aff_entry->ae_cond);
2602 }
2603
2604 /* Add the prefix to the prefix tree. */
2605 if (aff_entry->ae_add == NULL)
2606 p = (char_u *)"";
2607 else
2608 p = aff_entry->ae_add;
2609 tree_add_word(p, spin->si_prefroot, -1, idx,
2610 cur_aff->ah_newID, &spin->si_blocks);
2611 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00002612 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002613 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002614 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
2615 {
2616 if (fol != NULL)
2617 smsg((char_u *)_("Duplicate FOL in %s line %d"),
2618 fname, lnum);
2619 else
2620 fol = vim_strsave(items[1]);
2621 }
2622 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
2623 {
2624 if (low != NULL)
2625 smsg((char_u *)_("Duplicate LOW in %s line %d"),
2626 fname, lnum);
2627 else
2628 low = vim_strsave(items[1]);
2629 }
2630 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
2631 {
2632 if (upp != NULL)
2633 smsg((char_u *)_("Duplicate UPP in %s line %d"),
2634 fname, lnum);
2635 else
2636 upp = vim_strsave(items[1]);
2637 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002638 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002639 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002640 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002641 if (!isdigit(*items[1]))
2642 smsg((char_u *)_("Expected REP count in %s line %d"),
2643 fname, lnum);
2644 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002645 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
2646 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002647 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002648 if (do_rep)
2649 add_fromto(spin, &spin->si_rep, items[1], items[2]);
2650 }
2651 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
2652 {
2653 /* MAP item or count */
2654 if (!found_map)
2655 {
2656 /* First line contains the count. */
2657 found_map = TRUE;
2658 if (!isdigit(*items[1]))
2659 smsg((char_u *)_("Expected MAP count in %s line %d"),
2660 fname, lnum);
2661 }
2662 else if (do_map)
2663 {
2664 /* We simply concatenate all the MAP strings, separated by
2665 * slashes. */
2666 ga_concat(&spin->si_map, items[1]);
2667 ga_append(&spin->si_map, '/');
2668 }
2669 }
2670 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
2671 {
2672 if (do_sal)
2673 {
2674 /* SAL item (sounds-a-like)
2675 * Either one of the known keys or a from-to pair. */
2676 if (STRCMP(items[1], "followup") == 0)
2677 spin->si_followup = sal_to_bool(items[2]);
2678 else if (STRCMP(items[1], "collapse_result") == 0)
2679 spin->si_collapse = sal_to_bool(items[2]);
2680 else if (STRCMP(items[1], "remove_accents") == 0)
2681 spin->si_rem_accents = sal_to_bool(items[2]);
2682 else
2683 /* when "to" is "_" it means empty */
2684 add_fromto(spin, &spin->si_sal, items[1],
2685 STRCMP(items[2], "_") == 0 ? (char_u *)""
2686 : items[2]);
2687 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002688 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002689 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002690 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
2691 fname, lnum, items[0]);
2692 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002693 }
2694
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002695 if (fol != NULL || low != NULL || upp != NULL)
2696 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002697 /*
2698 * Don't write a word table for an ASCII file, so that we don't check
2699 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002700 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00002701 * mb_get_class(), the list of chars in the file will be incomplete.
2702 */
2703 if (!spin->si_ascii
2704#ifdef FEAT_MBYTE
2705 && !enc_utf8
2706#endif
2707 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002708 {
2709 if (fol == NULL || low == NULL || upp == NULL)
2710 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
2711 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00002712 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002713 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002714
2715 vim_free(fol);
2716 vim_free(low);
2717 vim_free(upp);
2718 }
2719
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002720 vim_free(pc);
2721 fclose(fd);
2722 return aff;
2723}
2724
2725/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002726 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
2727 * NULL as equal.
2728 */
2729 static int
2730str_equal(s1, s2)
2731 char_u *s1;
2732 char_u *s2;
2733{
2734 if (s1 == NULL || s2 == NULL)
2735 return s1 == s2;
2736 return STRCMP(s1, s2) == 0;
2737}
2738
2739/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002740 * Add a from-to item to "gap". Used for REP and SAL items.
2741 * They are stored case-folded.
2742 */
2743 static void
2744add_fromto(spin, gap, from, to)
2745 spellinfo_T *spin;
2746 garray_T *gap;
2747 char_u *from;
2748 char_u *to;
2749{
2750 fromto_T *ftp;
2751 char_u word[MAXWLEN];
2752
2753 if (ga_grow(gap, 1) == OK)
2754 {
2755 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
2756 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
2757 ftp->ft_from = getroom_save(&spin->si_blocks, word);
2758 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
2759 ftp->ft_to = getroom_save(&spin->si_blocks, word);
2760 ++gap->ga_len;
2761 }
2762}
2763
2764/*
2765 * Convert a boolean argument in a SAL line to TRUE or FALSE;
2766 */
2767 static int
2768sal_to_bool(s)
2769 char_u *s;
2770{
2771 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
2772}
2773
2774/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00002775 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
2776 * When "s" is NULL FALSE is returned.
2777 */
2778 static int
2779has_non_ascii(s)
2780 char_u *s;
2781{
2782 char_u *p;
2783
2784 if (s != NULL)
2785 for (p = s; *p != NUL; ++p)
2786 if (*p >= 128)
2787 return TRUE;
2788 return FALSE;
2789}
2790
2791/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002792 * Free the structure filled by spell_read_aff().
2793 */
2794 static void
2795spell_free_aff(aff)
2796 afffile_T *aff;
2797{
2798 hashtab_T *ht;
2799 hashitem_T *hi;
2800 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002801 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002802 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002803
2804 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002805
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002806 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002807 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
2808 {
2809 todo = ht->ht_used;
2810 for (hi = ht->ht_array; todo > 0; ++hi)
2811 {
2812 if (!HASHITEM_EMPTY(hi))
2813 {
2814 --todo;
2815 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002816 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
2817 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002818 }
2819 }
2820 if (ht == &aff->af_suff)
2821 break;
2822 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002823
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002824 hash_clear(&aff->af_pref);
2825 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002826}
2827
2828/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002829 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002830 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002831 */
2832 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00002833spell_read_dic(fname, spin, affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002834 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002835 spellinfo_T *spin;
2836 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002837{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002838 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002839 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00002840 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002841 char_u *pfxlist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002842 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002843 char_u *pc;
2844 char_u *w;
2845 int l;
2846 hash_T hash;
2847 hashitem_T *hi;
2848 FILE *fd;
2849 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002850 int non_ascii = 0;
2851 int retval = OK;
2852 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002853 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002854
Bram Moolenaar51485f02005-06-04 21:55:20 +00002855 /*
2856 * Open the file.
2857 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002858 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002859 if (fd == NULL)
2860 {
2861 EMSG2(_(e_notopen), fname);
2862 return FAIL;
2863 }
2864
Bram Moolenaar51485f02005-06-04 21:55:20 +00002865 /* The hashtable is only used to detect duplicated words. */
2866 hash_init(&ht);
2867
Bram Moolenaar8db73182005-06-17 21:51:16 +00002868 spin->si_foldwcount = 0;
2869 spin->si_keepwcount = 0;
2870
Bram Moolenaarb765d632005-06-07 21:00:02 +00002871 if (spin->si_verbose || p_verbose > 2)
2872 {
2873 if (!spin->si_verbose)
2874 verbose_enter();
2875 smsg((char_u *)_("Reading dictionary file %s..."), fname);
2876 out_flush();
2877 if (!spin->si_verbose)
2878 verbose_leave();
2879 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002880
2881 /* Read and ignore the first line: word count. */
2882 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002883 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002884 EMSG2(_("E760: No word count in %s"), fname);
2885
2886 /*
2887 * Read all the lines in the file one by one.
2888 * The words are converted to 'encoding' here, before being added to
2889 * the hashtable.
2890 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002891 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002892 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002893 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002894 ++lnum;
2895
Bram Moolenaar51485f02005-06-04 21:55:20 +00002896 /* Remove CR, LF and white space from the end. White space halfway
2897 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002898 l = STRLEN(line);
2899 while (l > 0 && line[l - 1] <= ' ')
2900 --l;
2901 if (l == 0)
2902 continue; /* empty line */
2903 line[l] = NUL;
2904
Bram Moolenaar51485f02005-06-04 21:55:20 +00002905 /* Find the optional affix names. */
2906 afflist = vim_strchr(line, '/');
2907 if (afflist != NULL)
2908 *afflist++ = NUL;
2909
2910 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
2911 if (spin->si_ascii && has_non_ascii(line))
2912 {
2913 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002914 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002915 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00002916
Bram Moolenaarb765d632005-06-07 21:00:02 +00002917#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002918 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002919 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002920 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002921 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002922 if (pc == NULL)
2923 {
2924 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
2925 fname, lnum, line);
2926 continue;
2927 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002928 w = pc;
2929 }
2930 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002931#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002932 {
2933 pc = NULL;
2934 w = line;
2935 }
2936
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002937 /* This takes time, print a message now and then. */
2938 if (spin->si_verbose && (lnum & 0x3ff) == 0)
2939 {
2940 vim_snprintf((char *)message, sizeof(message),
2941 _("line %6d, word %6d - %s"),
2942 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
2943 msg_start();
2944 msg_puts_long_attr(message, 0);
2945 msg_clr_eos();
2946 msg_didout = FALSE;
2947 msg_col = 0;
2948 out_flush();
2949 }
2950
Bram Moolenaar51485f02005-06-04 21:55:20 +00002951 /* Store the word in the hashtable to be able to find duplicates. */
2952 dw = (char_u *)getroom_save(&spin->si_blocks, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002953 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002954 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002955 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002956 if (retval == FAIL)
2957 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002958
Bram Moolenaar51485f02005-06-04 21:55:20 +00002959 hash = hash_hash(dw);
2960 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002961 if (!HASHITEM_EMPTY(hi))
2962 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002963 fname, lnum, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002964 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00002965 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002966
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002967 flags = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002968 pfxlist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002969 if (afflist != NULL)
2970 {
2971 /* Check for affix name that stands for keep-case word and stands
2972 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002973 if (affile->af_kep != NUL
2974 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002975 flags |= WF_KEEPCAP;
2976 if (affile->af_rar != NUL
2977 && vim_strchr(afflist, affile->af_rar) != NULL)
2978 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002979
2980 if (affile->af_pfxpostpone)
2981 /* Need to store the list of prefix IDs with the word. */
2982 pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002983 }
2984
Bram Moolenaar51485f02005-06-04 21:55:20 +00002985 /* Add the word to the word tree(s). */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002986 if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002987 retval = FAIL;
2988
2989 if (afflist != NULL)
2990 {
2991 /* Find all matching suffixes and add the resulting words.
2992 * Additionally do matching prefixes that combine. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002993 if (store_aff_word(dw, spin, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002994 &affile->af_suff, &affile->af_pref,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002995 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002996 retval = FAIL;
2997
2998 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002999 if (store_aff_word(dw, spin, afflist, affile,
3000 &affile->af_pref, NULL,
3001 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003002 retval = FAIL;
3003 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003004 }
3005
Bram Moolenaar51485f02005-06-04 21:55:20 +00003006 if (spin->si_ascii && non_ascii > 0)
3007 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
3008 non_ascii);
3009 hash_clear(&ht);
3010
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003011 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003012 return retval;
3013}
3014
3015/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003016 * Get the list of prefix IDs from the affix list "afflist".
3017 * Used for PFXPOSTPONE.
3018 * Returns a string allocated with getroom(). NULL when there are no prefixes
3019 * or when out of memory.
3020 */
3021 static char_u *
3022get_pfxlist(affile, afflist, blp)
3023 afffile_T *affile;
3024 char_u *afflist;
3025 sblock_T **blp;
3026{
3027 char_u *p;
3028 int cnt;
3029 int round;
3030 char_u *res = NULL;
3031 char_u key[2];
3032 hashitem_T *hi;
3033
3034 key[1] = NUL;
3035
3036 /* round 1: count the number of prefix IDs.
3037 * round 2: move prefix IDs to "res" */
3038 for (round = 1; round <= 2; ++round)
3039 {
3040 cnt = 0;
3041 for (p = afflist; *p != NUL; ++p)
3042 {
3043 key[0] = *p;
3044 hi = hash_find(&affile->af_pref, key);
3045 if (!HASHITEM_EMPTY(hi))
3046 {
3047 /* This is a prefix ID, use the new number. */
3048 if (round == 2)
3049 res[cnt] = HI2AH(hi)->ah_newID;
3050 ++cnt;
3051 }
3052 }
3053 if (round == 1 && cnt > 0)
3054 res = getroom(blp, cnt + 1);
3055 if (res == NULL)
3056 break;
3057 }
3058
3059 if (res != NULL)
3060 res[cnt] = NUL;
3061 return res;
3062}
3063
3064/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003065 * Apply affixes to a word and store the resulting words.
3066 * "ht" is the hashtable with affentry_T that need to be applied, either
3067 * prefixes or suffixes.
3068 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
3069 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003070 *
3071 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003072 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003073 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003074store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003075 char_u *word; /* basic word start */
3076 spellinfo_T *spin; /* spell info */
3077 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003078 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003079 hashtab_T *ht;
3080 hashtab_T *xht;
3081 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003082 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003083 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003084{
3085 int todo;
3086 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003087 affheader_T *ah;
3088 affentry_T *ae;
3089 regmatch_T regmatch;
3090 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003091 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003092 int i;
3093 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003094
Bram Moolenaar51485f02005-06-04 21:55:20 +00003095 todo = ht->ht_used;
3096 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003097 {
3098 if (!HASHITEM_EMPTY(hi))
3099 {
3100 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003101 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003102
Bram Moolenaar51485f02005-06-04 21:55:20 +00003103 /* Check that the affix combines, if required, and that the word
3104 * supports this affix. */
3105 if ((!comb || ah->ah_combine)
3106 && vim_strchr(afflist, *ah->ah_key) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003107 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003108 /* Loop over all affix entries with this name. */
3109 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003110 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003111 /* Check the condition. It's not logical to match case
3112 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003113 * Myspell.
3114 * For prefixes, when "PFXPOSTPONE" was used, only do
3115 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003116 regmatch.regprog = ae->ae_prog;
3117 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003118 if ((xht != NULL || !affile->af_pfxpostpone
3119 || ae->ae_chop != NULL)
3120 && (ae->ae_prog == NULL
3121 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003122 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003123 /* Match. Remove the chop and add the affix. */
3124 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003125 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003126 /* prefix: chop/add at the start of the word */
3127 if (ae->ae_add == NULL)
3128 *newword = NUL;
3129 else
3130 STRCPY(newword, ae->ae_add);
3131 p = word;
3132 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003133 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003134 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003135#ifdef FEAT_MBYTE
3136 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003137 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003138 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003139 for ( ; i > 0; --i)
3140 mb_ptr_adv(p);
3141 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003142 else
3143#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003144 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003145 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003146 STRCAT(newword, p);
3147 }
3148 else
3149 {
3150 /* suffix: chop/add at the end of the word */
3151 STRCPY(newword, word);
3152 if (ae->ae_chop != NULL)
3153 {
3154 /* Remove chop string. */
3155 p = newword + STRLEN(newword);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003156#ifdef FEAT_MBYTE
3157 if (has_mbyte)
3158 i = mb_charlen(ae->ae_chop);
3159 else
3160#endif
3161 i = STRLEN(ae->ae_chop);
3162 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003163 mb_ptr_back(newword, p);
3164 *p = NUL;
3165 }
3166 if (ae->ae_add != NULL)
3167 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003168 }
3169
Bram Moolenaar51485f02005-06-04 21:55:20 +00003170 /* Store the modified word. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003171 if (store_word(newword, spin,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003172 flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003173 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003174
Bram Moolenaar51485f02005-06-04 21:55:20 +00003175 /* When added a suffix and combining is allowed also
3176 * try adding prefixes additionally. */
3177 if (xht != NULL && ah->ah_combine)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003178 if (store_aff_word(newword, spin, afflist, affile,
3179 xht, NULL, TRUE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003180 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003181 }
3182 }
3183 }
3184 }
3185 }
3186
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003187 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003188}
3189
3190/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003191 * Read a file with a list of words.
3192 */
3193 static int
3194spell_read_wordfile(fname, spin)
3195 char_u *fname;
3196 spellinfo_T *spin;
3197{
3198 FILE *fd;
3199 long lnum = 0;
3200 char_u rline[MAXLINELEN];
3201 char_u *line;
3202 char_u *pc = NULL;
3203 int l;
3204 int retval = OK;
3205 int did_word = FALSE;
3206 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003207 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003208 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003209
3210 /*
3211 * Open the file.
3212 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003213 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00003214 if (fd == NULL)
3215 {
3216 EMSG2(_(e_notopen), fname);
3217 return FAIL;
3218 }
3219
Bram Moolenaarb765d632005-06-07 21:00:02 +00003220 if (spin->si_verbose || p_verbose > 2)
3221 {
3222 if (!spin->si_verbose)
3223 verbose_enter();
3224 smsg((char_u *)_("Reading word file %s..."), fname);
3225 out_flush();
3226 if (!spin->si_verbose)
3227 verbose_leave();
3228 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003229
3230 /*
3231 * Read all the lines in the file one by one.
3232 */
3233 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
3234 {
3235 line_breakcheck();
3236 ++lnum;
3237
3238 /* Skip comment lines. */
3239 if (*rline == '#')
3240 continue;
3241
3242 /* Remove CR, LF and white space from the end. */
3243 l = STRLEN(rline);
3244 while (l > 0 && rline[l - 1] <= ' ')
3245 --l;
3246 if (l == 0)
3247 continue; /* empty or blank line */
3248 rline[l] = NUL;
3249
3250 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
3251 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003252#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003253 if (spin->si_conv.vc_type != CONV_NONE)
3254 {
3255 pc = string_convert(&spin->si_conv, rline, NULL);
3256 if (pc == NULL)
3257 {
3258 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3259 fname, lnum, rline);
3260 continue;
3261 }
3262 line = pc;
3263 }
3264 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003265#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00003266 {
3267 pc = NULL;
3268 line = rline;
3269 }
3270
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003271 flags = 0;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003272 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003273
3274 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00003275 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003276 ++line;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003277
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003278 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003279 {
3280 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003281 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
3282 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003283 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003284 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
3285 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003286 else
3287 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003288#ifdef FEAT_MBYTE
3289 char_u *enc;
3290
Bram Moolenaar51485f02005-06-04 21:55:20 +00003291 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003292 line += 10;
3293 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003294 if (enc != NULL && !spin->si_ascii
3295 && convert_setup(&spin->si_conv, enc,
3296 p_enc) == FAIL)
3297 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00003298 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003299 vim_free(enc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003300#else
3301 smsg((char_u *)_("Conversion in %s not supported"), fname);
3302#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00003303 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003304 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003305 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003306
Bram Moolenaar3982c542005-06-08 21:56:31 +00003307 if (STRNCMP(line, "regions=", 8) == 0)
3308 {
3309 if (spin->si_region_count > 1)
3310 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
3311 fname, lnum, line);
3312 else
3313 {
3314 line += 8;
3315 if (STRLEN(line) > 16)
3316 smsg((char_u *)_("Too many regions in %s line %d: %s"),
3317 fname, lnum, line);
3318 else
3319 {
3320 spin->si_region_count = STRLEN(line) / 2;
3321 STRCPY(spin->si_region_name, line);
3322 }
3323 }
3324 continue;
3325 }
3326
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003327 if (*line == '=')
3328 {
3329 /* keep-case word */
3330 flags |= WF_KEEPCAP;
3331 ++line;
3332 }
3333
3334 if (*line == '!')
3335 {
3336 /* Bad, bad, wicked word. */
3337 flags |= WF_BANNED;
3338 ++line;
3339 }
3340 else if (*line == '?')
3341 {
3342 /* Rare word. */
3343 flags |= WF_RARE;
3344 ++line;
3345 }
3346
Bram Moolenaar3982c542005-06-08 21:56:31 +00003347 if (VIM_ISDIGIT(*line))
3348 {
3349 /* region number(s) */
3350 regionmask = 0;
3351 while (VIM_ISDIGIT(*line))
3352 {
3353 l = *line - '0';
3354 if (l > spin->si_region_count)
3355 {
3356 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
3357 fname, lnum, line);
3358 break;
3359 }
3360 regionmask |= 1 << (l - 1);
3361 ++line;
3362 }
3363 flags |= WF_REGION;
3364 }
3365
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003366 if (flags == 0)
3367 {
3368 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00003369 fname, lnum, line);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003370 continue;
3371 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003372 }
3373
3374 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
3375 if (spin->si_ascii && has_non_ascii(line))
3376 {
3377 ++non_ascii;
3378 continue;
3379 }
3380
3381 /* Normal word: store it. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003382 if (store_word(line, spin, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003383 {
3384 retval = FAIL;
3385 break;
3386 }
3387 did_word = TRUE;
3388 }
3389
3390 vim_free(pc);
3391 fclose(fd);
3392
Bram Moolenaarb765d632005-06-07 21:00:02 +00003393 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
3394 {
3395 if (p_verbose > 2)
3396 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00003397 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
3398 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003399 if (p_verbose > 2)
3400 verbose_leave();
3401 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003402 return retval;
3403}
3404
3405/*
3406 * Get part of an sblock_T, "len" bytes long.
3407 * This avoids calling free() for every little struct we use.
3408 * The memory is cleared to all zeros.
3409 * Returns NULL when out of memory.
3410 */
3411 static void *
3412getroom(blp, len)
3413 sblock_T **blp;
3414 size_t len; /* length needed */
3415{
3416 char_u *p;
3417 sblock_T *bl = *blp;
3418
3419 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
3420 {
3421 /* Allocate a block of memory. This is not freed until much later. */
3422 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
3423 if (bl == NULL)
3424 return NULL;
3425 bl->sb_next = *blp;
3426 *blp = bl;
3427 bl->sb_used = 0;
3428 }
3429
3430 p = bl->sb_data + bl->sb_used;
3431 bl->sb_used += len;
3432
3433 return p;
3434}
3435
3436/*
3437 * Make a copy of a string into memory allocated with getroom().
3438 */
3439 static char_u *
3440getroom_save(blp, s)
3441 sblock_T **blp;
3442 char_u *s;
3443{
3444 char_u *sc;
3445
3446 sc = (char_u *)getroom(blp, STRLEN(s) + 1);
3447 if (sc != NULL)
3448 STRCPY(sc, s);
3449 return sc;
3450}
3451
3452
3453/*
3454 * Free the list of allocated sblock_T.
3455 */
3456 static void
3457free_blocks(bl)
3458 sblock_T *bl;
3459{
3460 sblock_T *next;
3461
3462 while (bl != NULL)
3463 {
3464 next = bl->sb_next;
3465 vim_free(bl);
3466 bl = next;
3467 }
3468}
3469
3470/*
3471 * Allocate the root of a word tree.
3472 */
3473 static wordnode_T *
3474wordtree_alloc(blp)
3475 sblock_T **blp;
3476{
3477 return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
3478}
3479
3480/*
3481 * Store a word in the tree(s).
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003482 * Always store it in the case-folded tree. A keep-case word can also be used
3483 * with all caps.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003484 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003485 * When "pfxlist" is not NULL store the word for each prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003486 */
3487 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003488store_word(word, spin, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003489 char_u *word;
3490 spellinfo_T *spin;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003491 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003492 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003493 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003494{
3495 int len = STRLEN(word);
3496 int ct = captype(word, word + len);
3497 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003498 int res = OK;
3499 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003501 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003502 for (p = pfxlist; res == OK; ++p)
3503 {
3504 res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
3505 region, p == NULL ? 0 : *p, &spin->si_blocks);
3506 if (p == NULL || *p == NUL)
3507 break;
3508 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00003509 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003510
3511 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
Bram Moolenaar8db73182005-06-17 21:51:16 +00003512 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003513 for (p = pfxlist; res == OK; ++p)
3514 {
3515 res = tree_add_word(word, spin->si_keeproot, flags,
3516 region, p == NULL ? 0 : *p, &spin->si_blocks);
3517 if (p == NULL || *p == NUL)
3518 break;
3519 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00003520 ++spin->si_keepwcount;
3521 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003522 return res;
3523}
3524
3525/*
3526 * Add word "word" to a word tree at "root".
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003527 * When "flags" is -1 we are adding to the prefix tree where flags don't
3528 * matter and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003529 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003530 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003531 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003532tree_add_word(word, root, flags, region, prefixID, blp)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003533 char_u *word;
3534 wordnode_T *root;
3535 int flags;
3536 int region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003537 int prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003538 sblock_T **blp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003539{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003540 wordnode_T *node = root;
3541 wordnode_T *np;
3542 wordnode_T **prev = NULL;
3543 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003544
Bram Moolenaar51485f02005-06-04 21:55:20 +00003545 /* Add each byte of the word to the tree, including the NUL at the end. */
3546 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003547 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003548 /* Look for the sibling that has the same character. They are sorted
3549 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003550 * higher byte value. For zero bytes (end of word) the sorting is
3551 * done on flags and then on prefixID
Bram Moolenaar51485f02005-06-04 21:55:20 +00003552 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003553 while (node != NULL
3554 && (node->wn_byte < word[i]
3555 || (node->wn_byte == NUL
3556 && (flags < 0
3557 ? node->wn_prefixID < prefixID
3558 : node->wn_flags < (flags & 0xff)
3559 || (node->wn_flags == (flags & 0xff)
3560 && node->wn_prefixID < prefixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003561 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003562 prev = &node->wn_sibling;
3563 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003564 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003565 if (node == NULL
3566 || node->wn_byte != word[i]
3567 || (word[i] == NUL
3568 && (flags < 0
3569 || node->wn_flags != (flags & 0xff)
3570 || node->wn_prefixID != prefixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003571 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003572 /* Allocate a new node. */
3573 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
3574 if (np == NULL)
3575 return FAIL;
3576 np->wn_byte = word[i];
3577 *prev = np;
3578 np->wn_sibling = node;
3579 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003580 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003581
Bram Moolenaar51485f02005-06-04 21:55:20 +00003582 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003583 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003584 node->wn_flags = flags;
3585 node->wn_region |= region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003586 node->wn_prefixID = prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003587 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003588 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003589 prev = &node->wn_child;
3590 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003591 }
3592
3593 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003594}
3595
3596/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003597 * Compress a tree: find tails that are identical and can be shared.
3598 */
3599 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00003600wordtree_compress(root, spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003601 wordnode_T *root;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003602 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003603{
3604 hashtab_T ht;
3605 int n;
3606 int tot = 0;
3607
3608 if (root != NULL)
3609 {
3610 hash_init(&ht);
3611 n = node_compress(root, &ht, &tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003612 if (spin->si_verbose || p_verbose > 2)
3613 {
3614 if (!spin->si_verbose)
3615 verbose_enter();
3616 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00003617 n, tot, (tot - n) * 100 / tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003618 if (p_verbose > 2)
3619 verbose_leave();
3620 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003621 hash_clear(&ht);
3622 }
3623}
3624
3625/*
3626 * Compress a node, its siblings and its children, depth first.
3627 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003628 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003629 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00003630node_compress(node, ht, tot)
3631 wordnode_T *node;
3632 hashtab_T *ht;
3633 int *tot; /* total count of nodes before compressing,
3634 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003635{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003636 wordnode_T *np;
3637 wordnode_T *tp;
3638 wordnode_T *child;
3639 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003640 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003641 int len = 0;
3642 unsigned nr, n;
3643 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003644
Bram Moolenaar51485f02005-06-04 21:55:20 +00003645 /*
3646 * Go through the list of siblings. Compress each child and then try
3647 * finding an identical child to replace it.
3648 * Note that with "child" we mean not just the node that is pointed to,
3649 * but the whole list of siblings, of which the node is the first.
3650 */
3651 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003652 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003653 ++len;
3654 if ((child = np->wn_child) != NULL)
3655 {
3656 /* Compress the child. This fills wn_hashkey. */
3657 compressed += node_compress(child, ht, tot);
3658
3659 /* Try to find an identical child. */
3660 hash = hash_hash(child->wn_hashkey);
3661 hi = hash_lookup(ht, child->wn_hashkey, hash);
3662 tp = NULL;
3663 if (!HASHITEM_EMPTY(hi))
3664 {
3665 /* There are children with an identical hash value. Now check
3666 * if there is one that is really identical. */
3667 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_next)
3668 if (node_equal(child, tp))
3669 {
3670 /* Found one! Now use that child in place of the
3671 * current one. This means the current child is
3672 * dropped from the tree. */
3673 np->wn_child = tp;
3674 ++compressed;
3675 break;
3676 }
3677 if (tp == NULL)
3678 {
3679 /* No other child with this hash value equals the child of
3680 * the node, add it to the linked list after the first
3681 * item. */
3682 tp = HI2WN(hi);
3683 child->wn_next = tp->wn_next;
3684 tp->wn_next = child;
3685 }
3686 }
3687 else
3688 /* No other child has this hash value, add it to the
3689 * hashtable. */
3690 hash_add_item(ht, hi, child->wn_hashkey, hash);
3691 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003692 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003693 *tot += len;
3694
3695 /*
3696 * Make a hash key for the node and its siblings, so that we can quickly
3697 * find a lookalike node. This must be done after compressing the sibling
3698 * list, otherwise the hash key would become invalid by the compression.
3699 */
3700 node->wn_hashkey[0] = len;
3701 nr = 0;
3702 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003703 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003704 if (np->wn_byte == NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003705 /* end node: use wn_flags, wn_region and wn_prefixID */
3706 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003707 else
3708 /* byte node: use the byte value and the child pointer */
3709 n = np->wn_byte + ((long_u)np->wn_child << 8);
3710 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003711 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003712
3713 /* Avoid NUL bytes, it terminates the hash key. */
3714 n = nr & 0xff;
3715 node->wn_hashkey[1] = n == 0 ? 1 : n;
3716 n = (nr >> 8) & 0xff;
3717 node->wn_hashkey[2] = n == 0 ? 1 : n;
3718 n = (nr >> 16) & 0xff;
3719 node->wn_hashkey[3] = n == 0 ? 1 : n;
3720 n = (nr >> 24) & 0xff;
3721 node->wn_hashkey[4] = n == 0 ? 1 : n;
3722 node->wn_hashkey[5] = NUL;
3723
3724 return compressed;
3725}
3726
3727/*
3728 * Return TRUE when two nodes have identical siblings and children.
3729 */
3730 static int
3731node_equal(n1, n2)
3732 wordnode_T *n1;
3733 wordnode_T *n2;
3734{
3735 wordnode_T *p1;
3736 wordnode_T *p2;
3737
3738 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
3739 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
3740 if (p1->wn_byte != p2->wn_byte
3741 || (p1->wn_byte == NUL
3742 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003743 || p1->wn_region != p2->wn_region
3744 || p1->wn_prefixID != p2->wn_prefixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003745 : (p1->wn_child != p2->wn_child)))
3746 break;
3747
3748 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003749}
3750
3751/*
3752 * Write a number to file "fd", MSB first, in "len" bytes.
3753 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003754 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003755put_bytes(fd, nr, len)
3756 FILE *fd;
3757 long_u nr;
3758 int len;
3759{
3760 int i;
3761
3762 for (i = len - 1; i >= 0; --i)
3763 putc((int)(nr >> (i * 8)), fd);
3764}
3765
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003766static int
3767#ifdef __BORLANDC__
3768_RTLENTRYF
3769#endif
3770rep_compare __ARGS((const void *s1, const void *s2));
3771
3772/*
3773 * Function given to qsort() to sort the REP items on "from" string.
3774 */
3775 static int
3776#ifdef __BORLANDC__
3777_RTLENTRYF
3778#endif
3779rep_compare(s1, s2)
3780 const void *s1;
3781 const void *s2;
3782{
3783 fromto_T *p1 = (fromto_T *)s1;
3784 fromto_T *p2 = (fromto_T *)s2;
3785
3786 return STRCMP(p1->ft_from, p2->ft_from);
3787}
3788
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003789/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003790 * Write the Vim spell file "fname".
3791 */
3792 static void
Bram Moolenaar3982c542005-06-08 21:56:31 +00003793write_vim_spell(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003794 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003795 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003796{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003797 FILE *fd;
3798 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003799 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003800 wordnode_T *tree;
3801 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003802 int i;
3803 int l;
3804 garray_T *gap;
3805 fromto_T *ftp;
3806 char_u *p;
3807 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003808
Bram Moolenaarb765d632005-06-07 21:00:02 +00003809 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00003810 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003811 {
3812 EMSG2(_(e_notopen), fname);
3813 return;
3814 }
3815
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003816 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003817 * <charflagslen> <charflags>
3818 * <fcharslen> <fchars>
3819 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003820
3821 /* <fileID> */
3822 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
3823 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003824
3825 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003826 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003827 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003828 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
3829 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
3830 (size_t)1, fd);
3831 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003832 }
3833 else
3834 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003835 putc(0, fd);
3836 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003837 }
3838
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003839 /*
3840 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003841 * <charflagslen> <charflags> <fcharlen> <fchars>
3842 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003843 * 'encoding'.
3844 * Also skip this for an .add.spl file, the main spell file must contain
3845 * the table (avoids that it conflicts). File is shorter too.
3846 */
3847 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003848 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003849 putc(0, fd);
3850 putc(0, fd);
3851 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003852 }
3853 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00003854 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003855
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003856 /* Write the prefix conditions. */
3857 write_spell_prefcond(fd, &spin->si_prefcond);
3858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003859 /* Sort the REP items. */
3860 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
3861 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003863 /* <SUGGEST> : <repcount> <rep> ...
3864 * <salflags> <salcount> <sal> ...
3865 * <maplen> <mapstr> */
3866 for (round = 1; round <= 2; ++round)
3867 {
3868 if (round == 1)
3869 gap = &spin->si_rep;
3870 else
3871 {
3872 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003874 i = 0;
3875 if (spin->si_followup)
3876 i |= SAL_F0LLOWUP;
3877 if (spin->si_collapse)
3878 i |= SAL_COLLAPSE;
3879 if (spin->si_rem_accents)
3880 i |= SAL_REM_ACCENTS;
3881 putc(i, fd); /* <salflags> */
3882 }
3883
3884 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
3885 for (i = 0; i < gap->ga_len; ++i)
3886 {
3887 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3888 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3889 ftp = &((fromto_T *)gap->ga_data)[i];
3890 for (rr = 1; rr <= 2; ++rr)
3891 {
3892 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
3893 l = STRLEN(p);
3894 putc(l, fd);
3895 fwrite(p, l, (size_t)1, fd);
3896 }
3897 }
3898 }
3899
3900 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
3901 if (spin->si_map.ga_len > 0) /* <mapstr> */
3902 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
3903 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00003904
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003905 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003906 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003907 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003908 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003909 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003910 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003911 if (round == 1)
3912 tree = spin->si_foldroot;
3913 else if (round == 2)
3914 tree = spin->si_keeproot;
3915 else
3916 tree = spin->si_prefroot;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003917
Bram Moolenaar51485f02005-06-04 21:55:20 +00003918 /* Count the number of nodes. Needed to be able to allocate the
3919 * memory when reading the nodes. Also fills in the index for shared
3920 * nodes. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003921 nodecount = put_tree(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003922
Bram Moolenaar51485f02005-06-04 21:55:20 +00003923 /* number of nodes in 4 bytes */
3924 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003925 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003926
Bram Moolenaar51485f02005-06-04 21:55:20 +00003927 /* Write the nodes. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003928 (void)put_tree(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003929 }
3930
Bram Moolenaar51485f02005-06-04 21:55:20 +00003931 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00003932}
3933
3934/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003935 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003936 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00003937 * This first writes the list of possible bytes (siblings). Then for each
3938 * byte recursively write the children.
3939 *
3940 * NOTE: The code here must match the code in read_tree(), since assumptions
3941 * are made about the indexes (so that we don't have to write them in the
3942 * file).
3943 *
3944 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003945 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003946 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003947put_tree(fd, node, index, regionmask, prefixtree)
3948 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003949 wordnode_T *node;
3950 int index;
3951 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003952 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003953{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003954 int newindex = index;
3955 int siblingcount = 0;
3956 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003957 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003958
Bram Moolenaar51485f02005-06-04 21:55:20 +00003959 /* If "node" is zero the tree is empty. */
3960 if (node == NULL)
3961 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003962
Bram Moolenaar51485f02005-06-04 21:55:20 +00003963 /* Store the index where this node is written. */
3964 node->wn_index = index;
3965
3966 /* Count the number of siblings. */
3967 for (np = node; np != NULL; np = np->wn_sibling)
3968 ++siblingcount;
3969
3970 /* Write the sibling count. */
3971 if (fd != NULL)
3972 putc(siblingcount, fd); /* <siblingcount> */
3973
3974 /* Write each sibling byte and optionally extra info. */
3975 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003976 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003977 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00003978 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003979 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003980 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003981 /* For a NUL byte (end of word) write the flags etc. */
3982 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00003983 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003984 /* In PREFIXTREE write the required prefixID and the
3985 * associated condition nr (stored in wn_region). */
3986 putc(BY_FLAGS, fd); /* <byte> */
3987 putc(np->wn_prefixID, fd); /* <prefixID> */
3988 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003989 }
3990 else
3991 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003992 /* For word trees we write the flag/region items. */
3993 flags = np->wn_flags;
3994 if (regionmask != 0 && np->wn_region != regionmask)
3995 flags |= WF_REGION;
3996 if (np->wn_prefixID != 0)
3997 flags |= WF_PFX;
3998 if (flags == 0)
3999 {
4000 /* word without flags or region */
4001 putc(BY_NOFLAGS, fd); /* <byte> */
4002 }
4003 else
4004 {
4005 putc(BY_FLAGS, fd); /* <byte> */
4006 putc(flags, fd); /* <flags> */
4007 if (flags & WF_REGION)
4008 putc(np->wn_region, fd); /* <region> */
4009 if (flags & WF_PFX)
4010 putc(np->wn_prefixID, fd); /* <prefixID> */
4011 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004012 }
4013 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004014 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004015 else
4016 {
4017 if (np->wn_child->wn_index != 0 && np->wn_child->wn_wnode != node)
4018 {
4019 /* The child is written elsewhere, write the reference. */
4020 if (fd != NULL)
4021 {
4022 putc(BY_INDEX, fd); /* <byte> */
4023 /* <nodeidx> */
4024 put_bytes(fd, (long_u)np->wn_child->wn_index, 3);
4025 }
4026 }
4027 else if (np->wn_child->wn_wnode == NULL)
4028 /* We will write the child below and give it an index. */
4029 np->wn_child->wn_wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004030
Bram Moolenaar51485f02005-06-04 21:55:20 +00004031 if (fd != NULL)
4032 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
4033 {
4034 EMSG(_(e_write));
4035 return 0;
4036 }
4037 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004038 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004039
4040 /* Space used in the array when reading: one for each sibling and one for
4041 * the count. */
4042 newindex += siblingcount + 1;
4043
4044 /* Recursively dump the children of each sibling. */
4045 for (np = node; np != NULL; np = np->wn_sibling)
4046 if (np->wn_byte != 0 && np->wn_child->wn_wnode == node)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004047 newindex = put_tree(fd, np->wn_child, newindex, regionmask,
4048 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004049
4050 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004051}
4052
4053
4054/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00004055 * ":mkspell [-ascii] outfile infile ..."
4056 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004057 */
4058 void
4059ex_mkspell(eap)
4060 exarg_T *eap;
4061{
4062 int fcount;
4063 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004064 char_u *arg = eap->arg;
4065 int ascii = FALSE;
4066
4067 if (STRNCMP(arg, "-ascii", 6) == 0)
4068 {
4069 ascii = TRUE;
4070 arg = skipwhite(arg + 6);
4071 }
4072
4073 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
4074 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
4075 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004076 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004077 FreeWild(fcount, fnames);
4078 }
4079}
4080
4081/*
4082 * Create a Vim spell file from one or more word lists.
4083 * "fnames[0]" is the output file name.
4084 * "fnames[fcount - 1]" is the last input file name.
4085 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
4086 * and ".spl" is appended to make the output file name.
4087 */
4088 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004089mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004090 int fcount;
4091 char_u **fnames;
4092 int ascii; /* -ascii argument given */
4093 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004094 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004095{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004096 char_u fname[MAXPATHL];
4097 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00004098 char_u **innames;
4099 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004100 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004101 int i;
4102 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004103 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004104 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004105 spellinfo_T spin;
4106
4107 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004108 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004109 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004110 spin.si_followup = TRUE;
4111 spin.si_rem_accents = TRUE;
4112 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
4113 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
4114 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004115 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004116
Bram Moolenaarb765d632005-06-07 21:00:02 +00004117 /* default: fnames[0] is output file, following are input files */
4118 innames = &fnames[1];
4119 incount = fcount - 1;
4120
4121 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004122 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004123 len = STRLEN(fnames[0]);
4124 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
4125 {
4126 /* For ":mkspell path/en.latin1.add" output file is
4127 * "path/en.latin1.add.spl". */
4128 innames = &fnames[0];
4129 incount = 1;
4130 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
4131 }
4132 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
4133 {
4134 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004135 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004136 }
4137 else
4138 /* Name should be language, make the file name from it. */
4139 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
4140 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
4141
4142 /* Check for .ascii.spl. */
4143 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
4144 spin.si_ascii = TRUE;
4145
4146 /* Check for .add.spl. */
4147 if (strstr((char *)gettail(wfname), ".add.") != NULL)
4148 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004149 }
4150
Bram Moolenaarb765d632005-06-07 21:00:02 +00004151 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004152 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004153 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004154 EMSG(_("E754: Only up to 8 regions supported"));
4155 else
4156 {
4157 /* Check for overwriting before doing things that may take a lot of
4158 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004159 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004160 {
4161 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004162 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004163 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004164 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004165 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004166 EMSG2(_(e_isadir2), wfname);
4167 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004168 }
4169
4170 /*
4171 * Init the aff and dic pointers.
4172 * Get the region names if there are more than 2 arguments.
4173 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004174 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004175 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004176 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004177
Bram Moolenaar3982c542005-06-08 21:56:31 +00004178 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004179 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004180 len = STRLEN(innames[i]);
4181 if (STRLEN(gettail(innames[i])) < 5
4182 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004183 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004184 EMSG2(_("E755: Invalid region in %s"), innames[i]);
4185 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004186 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00004187 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
4188 spin.si_region_name[i * 2 + 1] =
4189 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004190 }
4191 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00004192 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004193
Bram Moolenaarb765d632005-06-07 21:00:02 +00004194 if (!spin.si_add)
4195 /* Clear the char type tables, don't want to use any of the
4196 * currently used spell properties. */
4197 init_spell_chartab();
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004198
Bram Moolenaar51485f02005-06-04 21:55:20 +00004199 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
4200 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004201 spin.si_prefroot = wordtree_alloc(&spin.si_blocks);
4202 if (spin.si_foldroot == NULL
4203 || spin.si_keeproot == NULL
4204 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004205 {
4206 error = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004207 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004208 }
4209
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004210 /*
4211 * Read all the .aff and .dic files.
4212 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004213 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004214 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004215 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004216 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004217 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004218 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004219
Bram Moolenaarb765d632005-06-07 21:00:02 +00004220 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004221 if (mch_stat((char *)fname, &st) >= 0)
4222 {
4223 /* Read the .aff file. Will init "spin->si_conv" based on the
4224 * "SET" line. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004225 afile[i] = spell_read_aff(fname, &spin);
4226 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004227 error = TRUE;
4228 else
4229 {
4230 /* Read the .dic file and store the words in the trees. */
4231 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00004232 innames[i]);
4233 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004234 error = TRUE;
4235 }
4236 }
4237 else
4238 {
4239 /* No .aff file, try reading the file as a word list. Store
4240 * the words in the trees. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004241 if (spell_read_wordfile(innames[i], &spin) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004242 error = TRUE;
4243 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004244
Bram Moolenaarb765d632005-06-07 21:00:02 +00004245#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004246 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004247 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004248#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004249 }
4250
Bram Moolenaar51485f02005-06-04 21:55:20 +00004251 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004252 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004253 /*
4254 * Remove the dummy NUL from the start of the tree root.
4255 */
4256 spin.si_foldroot = spin.si_foldroot->wn_sibling;
4257 spin.si_keeproot = spin.si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004258 spin.si_prefroot = spin.si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004259
4260 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004261 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004262 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004265 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004266 verbose_enter();
4267 MSG(_("Compressing word tree..."));
4268 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004270 verbose_leave();
4271 }
4272 wordtree_compress(spin.si_foldroot, &spin);
4273 wordtree_compress(spin.si_keeproot, &spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004274 wordtree_compress(spin.si_prefroot, &spin);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004275 }
4276
Bram Moolenaar51485f02005-06-04 21:55:20 +00004277 if (!error)
4278 {
4279 /*
4280 * Write the info in the spell file.
4281 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004282 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004284 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004285 verbose_enter();
4286 smsg((char_u *)_("Writing spell file %s..."), wfname);
4287 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004288 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004289 verbose_leave();
4290 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00004291
Bram Moolenaar3982c542005-06-08 21:56:31 +00004292 write_vim_spell(wfname, &spin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004294 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004296 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004297 verbose_enter();
4298 MSG(_("Done!"));
4299 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00004300 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004301 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004302 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004303 verbose_leave();
4304 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004305
Bram Moolenaarb765d632005-06-07 21:00:02 +00004306 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004307 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004308 }
4309
4310 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004311 ga_clear(&spin.si_rep);
4312 ga_clear(&spin.si_sal);
4313 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004314 ga_clear(&spin.si_prefcond);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004315
4316 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004317 for (i = 0; i < incount; ++i)
4318 if (afile[i] != NULL)
4319 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004320
4321 /* Free all the bits and pieces at once. */
4322 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004323 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004324}
4325
Bram Moolenaarb765d632005-06-07 21:00:02 +00004326
4327/*
4328 * ":spellgood {word}"
4329 * ":spellwrong {word}"
4330 */
4331 void
4332ex_spell(eap)
4333 exarg_T *eap;
4334{
4335 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong);
4336}
4337
4338/*
4339 * Add "word[len]" to 'spellfile' as a good or bad word.
4340 */
4341 void
4342spell_add_word(word, len, bad)
4343 char_u *word;
4344 int len;
4345 int bad;
4346{
4347 FILE *fd;
4348 buf_T *buf;
4349
4350 if (*curbuf->b_p_spf == NUL)
4351 init_spellfile();
4352 if (*curbuf->b_p_spf == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004353 EMSG(_("E764: 'spellfile' is not set"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004354 else
4355 {
4356 /* Check that the user isn't editing the .add file somewhere. */
4357 buf = buflist_findname_exp(curbuf->b_p_spf);
4358 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
4359 buf = NULL;
4360 if (buf != NULL && bufIsChanged(buf))
4361 EMSG(_(e_bufloaded));
4362 else
4363 {
4364 fd = mch_fopen((char *)curbuf->b_p_spf, "a");
4365 if (fd == NULL)
4366 EMSG2(_(e_notopen), curbuf->b_p_spf);
4367 else
4368 {
4369 if (bad)
4370 fprintf(fd, "/!%.*s\n", len, word);
4371 else
4372 fprintf(fd, "%.*s\n", len, word);
4373 fclose(fd);
4374
4375 /* Update the .add.spl file. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004376 mkspell(1, &curbuf->b_p_spf, FALSE, TRUE, TRUE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004377
4378 /* If the .add file is edited somewhere, reload it. */
4379 if (buf != NULL)
4380 buf_reload(buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004381
4382 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004383 }
4384 }
4385 }
4386}
4387
4388/*
4389 * Initialize 'spellfile' for the current buffer.
4390 */
4391 static void
4392init_spellfile()
4393{
4394 char_u buf[MAXPATHL];
4395 int l;
4396 slang_T *sl;
4397 char_u *rtp;
4398
4399 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
4400 {
4401 /* Loop over all entries in 'runtimepath'. */
4402 rtp = p_rtp;
4403 while (*rtp != NUL)
4404 {
4405 /* Copy the path from 'runtimepath' to buf[]. */
4406 copy_option_part(&rtp, buf, MAXPATHL, ",");
4407 if (filewritable(buf) == 2)
4408 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004409 /* Use the first language name from 'spelllang' and the
4410 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004411 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
4412 l = STRLEN(buf);
4413 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00004414 "/spell/%.*s.%s.add",
4415 2, curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00004416 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
4417 ? (char_u *)"ascii" : spell_enc());
4418 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
4419 break;
4420 }
4421 }
4422 }
4423}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004424
Bram Moolenaar51485f02005-06-04 21:55:20 +00004425
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004426/*
4427 * Init the chartab used for spelling for ASCII.
4428 * EBCDIC is not supported!
4429 */
4430 static void
4431clear_spell_chartab(sp)
4432 spelltab_T *sp;
4433{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004434 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004435
4436 /* Init everything to FALSE. */
4437 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
4438 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
4439 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004440 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004441 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004442 sp->st_upper[i] = i;
4443 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004444
4445 /* We include digits. A word shouldn't start with a digit, but handling
4446 * that is done separately. */
4447 for (i = '0'; i <= '9'; ++i)
4448 sp->st_isw[i] = TRUE;
4449 for (i = 'A'; i <= 'Z'; ++i)
4450 {
4451 sp->st_isw[i] = TRUE;
4452 sp->st_isu[i] = TRUE;
4453 sp->st_fold[i] = i + 0x20;
4454 }
4455 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004456 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004457 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004458 sp->st_upper[i] = i - 0x20;
4459 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004460}
4461
4462/*
4463 * Init the chartab used for spelling. Only depends on 'encoding'.
4464 * Called once while starting up and when 'encoding' changes.
4465 * The default is to use isalpha(), but the spell file should define the word
4466 * characters to make it possible that 'encoding' differs from the current
4467 * locale.
4468 */
4469 void
4470init_spell_chartab()
4471{
4472 int i;
4473
4474 did_set_spelltab = FALSE;
4475 clear_spell_chartab(&spelltab);
4476
4477#ifdef FEAT_MBYTE
4478 if (enc_dbcs)
4479 {
4480 /* DBCS: assume double-wide characters are word characters. */
4481 for (i = 128; i <= 255; ++i)
4482 if (MB_BYTE2LEN(i) == 2)
4483 spelltab.st_isw[i] = TRUE;
4484 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004485 else if (enc_utf8)
4486 {
4487 for (i = 128; i < 256; ++i)
4488 {
4489 spelltab.st_isu[i] = utf_isupper(i);
4490 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
4491 spelltab.st_fold[i] = utf_fold(i);
4492 spelltab.st_upper[i] = utf_toupper(i);
4493 }
4494 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004495 else
4496#endif
4497 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004498 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004499 for (i = 128; i < 256; ++i)
4500 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004501 if (MB_ISUPPER(i))
4502 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004503 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004504 spelltab.st_isu[i] = TRUE;
4505 spelltab.st_fold[i] = MB_TOLOWER(i);
4506 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004507 else if (MB_ISLOWER(i))
4508 {
4509 spelltab.st_isw[i] = TRUE;
4510 spelltab.st_upper[i] = MB_TOUPPER(i);
4511 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004512 }
4513 }
4514}
4515
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004516static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
4517static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
4518
4519/*
4520 * Set the spell character tables from strings in the affix file.
4521 */
4522 static int
4523set_spell_chartab(fol, low, upp)
4524 char_u *fol;
4525 char_u *low;
4526 char_u *upp;
4527{
4528 /* We build the new tables here first, so that we can compare with the
4529 * previous one. */
4530 spelltab_T new_st;
4531 char_u *pf = fol, *pl = low, *pu = upp;
4532 int f, l, u;
4533
4534 clear_spell_chartab(&new_st);
4535
4536 while (*pf != NUL)
4537 {
4538 if (*pl == NUL || *pu == NUL)
4539 {
4540 EMSG(_(e_affform));
4541 return FAIL;
4542 }
4543#ifdef FEAT_MBYTE
4544 f = mb_ptr2char_adv(&pf);
4545 l = mb_ptr2char_adv(&pl);
4546 u = mb_ptr2char_adv(&pu);
4547#else
4548 f = *pf++;
4549 l = *pl++;
4550 u = *pu++;
4551#endif
4552 /* Every character that appears is a word character. */
4553 if (f < 256)
4554 new_st.st_isw[f] = TRUE;
4555 if (l < 256)
4556 new_st.st_isw[l] = TRUE;
4557 if (u < 256)
4558 new_st.st_isw[u] = TRUE;
4559
4560 /* if "LOW" and "FOL" are not the same the "LOW" char needs
4561 * case-folding */
4562 if (l < 256 && l != f)
4563 {
4564 if (f >= 256)
4565 {
4566 EMSG(_(e_affrange));
4567 return FAIL;
4568 }
4569 new_st.st_fold[l] = f;
4570 }
4571
4572 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004573 * case-folding, it's upper case and the "UPP" is the upper case of
4574 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004575 if (u < 256 && u != f)
4576 {
4577 if (f >= 256)
4578 {
4579 EMSG(_(e_affrange));
4580 return FAIL;
4581 }
4582 new_st.st_fold[u] = f;
4583 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004584 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004585 }
4586 }
4587
4588 if (*pl != NUL || *pu != NUL)
4589 {
4590 EMSG(_(e_affform));
4591 return FAIL;
4592 }
4593
4594 return set_spell_finish(&new_st);
4595}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004596
4597/*
4598 * Set the spell character tables from strings in the .spl file.
4599 */
4600 static int
4601set_spell_charflags(flags, cnt, upp)
4602 char_u *flags;
4603 int cnt;
4604 char_u *upp;
4605{
4606 /* We build the new tables here first, so that we can compare with the
4607 * previous one. */
4608 spelltab_T new_st;
4609 int i;
4610 char_u *p = upp;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004611 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004612
4613 clear_spell_chartab(&new_st);
4614
4615 for (i = 0; i < cnt; ++i)
4616 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004617 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
4618 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004619
4620 if (*p == NUL)
4621 return FAIL;
4622#ifdef FEAT_MBYTE
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004623 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004624#else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004625 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004626#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004627 new_st.st_fold[i + 128] = c;
4628 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
4629 new_st.st_upper[c] = i + 128;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004630 }
4631
4632 return set_spell_finish(&new_st);
4633}
4634
4635 static int
4636set_spell_finish(new_st)
4637 spelltab_T *new_st;
4638{
4639 int i;
4640
4641 if (did_set_spelltab)
4642 {
4643 /* check that it's the same table */
4644 for (i = 0; i < 256; ++i)
4645 {
4646 if (spelltab.st_isw[i] != new_st->st_isw[i]
4647 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004648 || spelltab.st_fold[i] != new_st->st_fold[i]
4649 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004650 {
4651 EMSG(_("E763: Word characters differ between spell files"));
4652 return FAIL;
4653 }
4654 }
4655 }
4656 else
4657 {
4658 /* copy the new spelltab into the one being used */
4659 spelltab = *new_st;
4660 did_set_spelltab = TRUE;
4661 }
4662
4663 return OK;
4664}
4665
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004666/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004667 * Write the table with prefix conditions to the .spl file.
4668 */
4669 static void
4670write_spell_prefcond(fd, gap)
4671 FILE *fd;
4672 garray_T *gap;
4673{
4674 int i;
4675 char_u *p;
4676 int len;
4677
4678 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
4679
4680 for (i = 0; i < gap->ga_len; ++i)
4681 {
4682 /* <prefcond> : <condlen> <condstr> */
4683 p = ((char_u **)gap->ga_data)[i];
4684 if (p == NULL)
4685 fputc(0, fd);
4686 else
4687 {
4688 len = STRLEN(p);
4689 fputc(len, fd);
4690 fwrite(p, (size_t)len, (size_t)1, fd);
4691 }
4692 }
4693}
4694
4695/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004696 * Write the current tables into the .spl file.
4697 * This makes sure the same characters are recognized as word characters when
4698 * generating an when using a spell file.
4699 */
4700 static void
4701write_spell_chartab(fd)
4702 FILE *fd;
4703{
4704 char_u charbuf[256 * 4];
4705 int len = 0;
4706 int flags;
4707 int i;
4708
4709 fputc(128, fd); /* <charflagslen> */
4710 for (i = 128; i < 256; ++i)
4711 {
4712 flags = 0;
4713 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004714 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004715 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004716 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004717 fputc(flags, fd); /* <charflags> */
4718
Bram Moolenaarb765d632005-06-07 21:00:02 +00004719#ifdef FEAT_MBYTE
4720 if (has_mbyte)
4721 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
4722 else
4723#endif
4724 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004725 }
4726
4727 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
4728 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
4729}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004730
4731/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004732 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
4733 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004734 * When using a multi-byte 'encoding' the length may change!
4735 * Returns FAIL when something wrong.
4736 */
4737 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004738spell_casefold(str, len, buf, buflen)
4739 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004740 int len;
4741 char_u *buf;
4742 int buflen;
4743{
4744 int i;
4745
4746 if (len >= buflen)
4747 {
4748 buf[0] = NUL;
4749 return FAIL; /* result will not fit */
4750 }
4751
4752#ifdef FEAT_MBYTE
4753 if (has_mbyte)
4754 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004755 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004756 char_u *p;
4757 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004758
4759 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004760 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004761 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004762 if (outi + MB_MAXBYTES > buflen)
4763 {
4764 buf[outi] = NUL;
4765 return FAIL;
4766 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004767 c = mb_ptr2char_adv(&p);
4768 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004769 }
4770 buf[outi] = NUL;
4771 }
4772 else
4773#endif
4774 {
4775 /* Be quick for non-multibyte encodings. */
4776 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004777 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004778 buf[i] = NUL;
4779 }
4780
4781 return OK;
4782}
4783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004784/*
4785 * "z?": Find badly spelled word under or after the cursor.
4786 * Give suggestions for the properly spelled word.
4787 * This is based on the mechanisms of Aspell, but completely reimplemented.
4788 */
4789 void
4790spell_suggest()
4791{
4792 char_u *line;
4793 pos_T prev_cursor = curwin->w_cursor;
4794 int attr;
4795 char_u wcopy[MAXWLEN + 2];
4796 char_u *p;
4797 int i;
4798 int c;
4799 suginfo_T sug;
4800 suggest_T *stp;
4801
4802 /*
4803 * Find the start of the badly spelled word.
4804 */
4805 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
4806 {
4807 beep_flush();
4808 return;
4809 }
4810
4811 /*
4812 * Set the info in "sug".
4813 */
4814 vim_memset(&sug, 0, sizeof(sug));
4815 ga_init2(&sug.su_ga, (int)sizeof(suggest_T), 10);
4816 hash_init(&sug.su_banned);
4817 line = ml_get_curline();
4818 sug.su_badptr = line + curwin->w_cursor.col;
4819 sug.su_badlen = spell_check(curwin, sug.su_badptr, &attr);
4820 if (sug.su_badlen >= MAXWLEN)
4821 sug.su_badlen = MAXWLEN - 1; /* just in case */
4822 vim_strncpy(sug.su_badword, sug.su_badptr, sug.su_badlen);
4823 (void)spell_casefold(sug.su_badptr, sug.su_badlen,
4824 sug.su_fbadword, MAXWLEN);
4825
4826 /* Ban the bad word itself. It may appear in another region. */
4827 add_banned(&sug, sug.su_badword);
4828
4829 /*
4830 * 1. Try inserting/deleting/swapping/changing a letter, use REP entries
4831 * from the .aff file and inserting a space (split the word).
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004832 *
4833 * Set a maximum score to limit the combination of operations that is
4834 * tried.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004835 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004836 sug.su_maxscore = SCORE_MAXINIT;
4837 spell_try_change(&sug);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004838
4839 /*
4840 * 2. Try finding sound-a-like words.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004841 *
4842 * Only do this when we don't have a lot of suggestions yet, because it's
4843 * very slow and often doesn't find new suggestions.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004844 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004845 if (sug.su_ga.ga_len < SUG_CLEAN_COUNT)
4846 {
4847 /* Allow a higher score now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004848 sug.su_maxscore = SCORE_MAXMAX;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004849 spell_try_soundalike(&sug);
4850 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004851
4852 /* When CTRL-C was hit while searching do show the results. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004853 ui_breakcheck();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004854 if (got_int)
4855 {
4856 (void)vgetc();
4857 got_int = FALSE;
4858 }
4859
4860 if (sug.su_ga.ga_len == 0)
4861 MSG(_("Sorry, no suggestions"));
4862 else
4863 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004864#ifdef RESCORE
4865 /* Do slow but more accurate computation of the word score. */
4866 rescore_suggestions(&sug);
4867#endif
4868
4869 /* Sort the suggestions and truncate at SUG_PROMPT_COUNT. */
4870 cleanup_suggestions(&sug, SUG_PROMPT_COUNT);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004871
4872 /* List the suggestions. */
4873 msg_start();
4874 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
4875 sug.su_badlen, sug.su_badptr);
4876 msg_puts(IObuff);
4877 msg_clr_eos();
4878 msg_putchar('\n');
4879 msg_scroll = TRUE;
4880 for (i = 0; i < sug.su_ga.ga_len; ++i)
4881 {
4882 stp = &SUG(&sug, i);
4883
4884 /* The suggested word may replace only part of the bad word, add
4885 * the not replaced part. */
4886 STRCPY(wcopy, stp->st_word);
4887 if (sug.su_badlen > stp->st_orglen)
4888 vim_strncpy(wcopy + STRLEN(wcopy),
4889 sug.su_badptr + stp->st_orglen,
4890 sug.su_badlen - stp->st_orglen);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004891 if (p_verbose > 0)
4892 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\" (%d)"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004893 i + 1, wcopy, stp->st_score);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004894 else
4895 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""),
4896 i + 1, wcopy);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004897 msg_puts(IObuff);
4898 lines_left = 3; /* avoid more prompt */
4899 msg_putchar('\n');
4900 }
4901
4902 /* Ask for choice. */
4903 i = prompt_for_number();
4904 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor())
4905 {
4906 /* Replace the word. */
4907 stp = &SUG(&sug, i - 1);
4908 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
4909 if (p != NULL)
4910 {
4911 c = sug.su_badptr - line;
4912 mch_memmove(p, line, c);
4913 STRCPY(p + c, stp->st_word);
4914 STRCAT(p, sug.su_badptr + stp->st_orglen);
4915 ml_replace(curwin->w_cursor.lnum, p, FALSE);
4916 curwin->w_cursor.col = c;
4917 changed_bytes(curwin->w_cursor.lnum, c);
4918 }
4919 }
4920 else
4921 curwin->w_cursor = prev_cursor;
4922 }
4923
4924 /* Free the suggestions. */
4925 for (i = 0; i < sug.su_ga.ga_len; ++i)
4926 vim_free(SUG(&sug, i).st_word);
4927 ga_clear(&sug.su_ga);
4928
4929 /* Free the banned words. */
4930 free_banned(&sug);
4931}
4932
4933/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004934 * Make a copy of "word", with the first letter upper or lower cased, to
4935 * "wcopy[MAXWLEN]". "word" must not be empty.
4936 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004937 */
4938 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004939onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004940 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004941 char_u *wcopy;
4942 int upper; /* TRUE: first letter made upper case */
4943{
4944 char_u *p;
4945 int c;
4946 int l;
4947
4948 p = word;
4949#ifdef FEAT_MBYTE
4950 if (has_mbyte)
4951 c = mb_ptr2char_adv(&p);
4952 else
4953#endif
4954 c = *p++;
4955 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004956 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004957 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004958 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004959#ifdef FEAT_MBYTE
4960 if (has_mbyte)
4961 l = mb_char2bytes(c, wcopy);
4962 else
4963#endif
4964 {
4965 l = 1;
4966 wcopy[0] = c;
4967 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004968 vim_strncpy(wcopy + l, p, MAXWLEN - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004969}
4970
4971/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004972 * Make a copy of "word" with all the letters upper cased into
4973 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004974 */
4975 static void
4976allcap_copy(word, wcopy)
4977 char_u *word;
4978 char_u *wcopy;
4979{
4980 char_u *s;
4981 char_u *d;
4982 int c;
4983
4984 d = wcopy;
4985 for (s = word; *s != NUL; )
4986 {
4987#ifdef FEAT_MBYTE
4988 if (has_mbyte)
4989 c = mb_ptr2char_adv(&s);
4990 else
4991#endif
4992 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004993 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004994
4995#ifdef FEAT_MBYTE
4996 if (has_mbyte)
4997 {
4998 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
4999 break;
5000 d += mb_char2bytes(c, d);
5001 }
5002 else
5003#endif
5004 {
5005 if (d - wcopy >= MAXWLEN - 1)
5006 break;
5007 *d++ = c;
5008 }
5009 }
5010 *d = NUL;
5011}
5012
5013/*
5014 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00005015 *
5016 * This uses a state machine. At each node in the tree we try various
5017 * operations. When trying if an operation work "depth" is increased and the
5018 * stack[] is used to store info. This allows combinations, thus insert one
5019 * character, replace one and delete another. The number of changes is
5020 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005021 */
5022 static void
5023spell_try_change(su)
5024 suginfo_T *su;
5025{
5026 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
5027 char_u tword[MAXWLEN]; /* good word collected so far */
5028 trystate_T stack[MAXWLEN];
5029 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
5030 * to for word split) */
5031 char_u prewordlen = 0; /* length of word in "preword" */
5032 int splitoff = 0; /* index in tword after last split */
5033 trystate_T *sp;
5034 int newscore;
5035 langp_T *lp;
5036 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005037 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005038 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005039 int c, c2, c3;
5040 int n = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005041 int flags;
5042 int badflags;
5043 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005044 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005045 int len;
5046 char_u *p;
5047 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00005048 int fl = 0, tl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005049
5050 /* get caps flags for bad word */
5051 badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
5052
5053 /* We make a copy of the case-folded bad word, so that we can modify it
5054 * to find matches (esp. REP items). */
5055 STRCPY(fword, su->su_fbadword);
5056
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005057
5058 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
5059 lp->lp_slang != NULL; ++lp)
5060 {
5061#ifdef SOUNDFOLD_SCORE
5062 su->su_slang = lp->lp_slang;
5063 if (lp->lp_slang->sl_sal.ga_len > 0)
5064 /* soundfold the bad word */
5065 spell_soundfold(lp->lp_slang, su->su_fbadword, su->su_salword);
5066#endif
5067
5068 /*
5069 * Go through the whole case-fold tree, try changes at each node.
5070 * "tword[]" contains the word collected from nodes in the tree.
5071 * "fword[]" the word we are trying to match with (initially the bad
5072 * word).
5073 */
5074 byts = lp->lp_slang->sl_fbyts;
5075 idxs = lp->lp_slang->sl_fidxs;
5076
5077 depth = 0;
5078 stack[0].ts_state = STATE_START;
5079 stack[0].ts_score = 0;
5080 stack[0].ts_curi = 1;
5081 stack[0].ts_fidx = 0;
5082 stack[0].ts_fidxtry = 0;
5083 stack[0].ts_twordlen = 0;
5084 stack[0].ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00005085#ifdef FEAT_MBYTE
5086 stack[0].ts_tcharlen = 0;
5087#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005088
Bram Moolenaarea424162005-06-16 21:51:00 +00005089 /*
5090 * Loop to find all suggestions. At each round we either:
5091 * - For the current state try one operation, advance "ts_curi",
5092 * increase "depth".
5093 * - When a state is done go to the next, set "ts_state".
5094 * - When all states are tried decrease "depth".
5095 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005096 while (depth >= 0 && !got_int)
5097 {
5098 sp = &stack[depth];
5099 switch (sp->ts_state)
5100 {
5101 case STATE_START:
5102 /*
5103 * Start of node: Deal with NUL bytes, which means
5104 * tword[] may end here.
5105 */
5106 arridx = sp->ts_arridx; /* current node in the tree */
5107 len = byts[arridx]; /* bytes in this node */
5108 arridx += sp->ts_curi; /* index of current byte */
5109
5110 if (sp->ts_curi > len || (c = byts[arridx]) != 0)
5111 {
5112 /* Past bytes in node and/or past NUL bytes. */
5113 sp->ts_state = STATE_ENDNUL;
5114 break;
5115 }
5116
5117 /*
5118 * End of word in tree.
5119 */
5120 ++sp->ts_curi; /* eat one NUL byte */
5121
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005122 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005123
5124 /*
5125 * Form the word with proper case in preword.
5126 * If there is a word from a previous split, append.
5127 */
5128 tword[sp->ts_twordlen] = NUL;
5129 if (flags & WF_KEEPCAP)
5130 /* Must find the word in the keep-case tree. */
5131 find_keepcap_word(lp->lp_slang, tword + splitoff,
5132 preword + prewordlen);
5133 else
5134 /* Include badflags: if the badword is onecap or allcap
5135 * use that for the goodword too. */
5136 make_case_word(tword + splitoff,
5137 preword + prewordlen, flags | badflags);
5138
5139 /* Don't use a banned word. It may appear again as a good
5140 * word, thus remember it. */
5141 if (flags & WF_BANNED)
5142 {
5143 add_banned(su, preword + prewordlen);
5144 break;
5145 }
5146 if (was_banned(su, preword + prewordlen))
5147 break;
5148
5149 newscore = 0;
5150 if ((flags & WF_REGION)
5151 && (((unsigned)flags >> 8) & lp->lp_region) == 0)
5152 newscore += SCORE_REGION;
5153 if (flags & WF_RARE)
5154 newscore += SCORE_RARE;
5155
5156 if (!spell_valid_case(badflags,
5157 captype(preword + prewordlen, NULL)))
5158 newscore += SCORE_ICASE;
5159
5160 if (fword[sp->ts_fidx] == 0)
5161 {
5162 /* The badword also ends: add suggestions, */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005163 add_suggestion(su, preword, sp->ts_score + newscore
5164#ifdef RESCORE
5165 , FALSE
5166#endif
5167 );
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005168 }
Bram Moolenaarea424162005-06-16 21:51:00 +00005169 else if (sp->ts_fidx >= sp->ts_fidxtry
5170#ifdef FEAT_MBYTE
5171 /* Don't split halfway a character. */
5172 && (!has_mbyte || sp->ts_tcharlen == 0)
5173#endif
5174 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005175 {
5176 /* The word in the tree ends but the badword
5177 * continues: try inserting a space and check that a valid
5178 * words starts at fword[sp->ts_fidx]. */
5179 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
5180 {
5181 /* Save things to be restored at STATE_SPLITUNDO. */
5182 sp->ts_save_prewordlen = prewordlen;
5183 sp->ts_save_badflags = badflags;
5184 sp->ts_save_splitoff = splitoff;
5185
5186 /* Append a space to preword. */
5187 STRCAT(preword, " ");
5188 prewordlen = STRLEN(preword);
5189 splitoff = sp->ts_twordlen;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005190#ifdef FEAT_MBYTE
5191 if (has_mbyte)
5192 {
5193 int i = 0;
5194
5195 /* Case-folding may change the number of bytes:
5196 * Count nr of chars in fword[sp->ts_fidx] and
5197 * advance that many chars in su->su_badptr. */
5198 for (p = fword; p < fword + sp->ts_fidx;
5199 mb_ptr_adv(p))
5200 ++i;
5201 for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
5202 --i;
5203 }
5204 else
5205#endif
5206 p = su->su_badptr + sp->ts_fidx;
5207 badflags = captype(p, su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208
5209 sp->ts_state = STATE_SPLITUNDO;
5210 ++depth;
5211 /* Restart at top of the tree. */
5212 stack[depth].ts_arridx = 0;
5213 }
5214 }
5215 break;
5216
5217 case STATE_SPLITUNDO:
5218 /* Fixup the changes done for word split. */
5219 badflags = sp->ts_save_badflags;
5220 splitoff = sp->ts_save_splitoff;
5221 prewordlen = sp->ts_save_prewordlen;
5222
5223 /* Continue looking for NUL bytes. */
5224 sp->ts_state = STATE_START;
5225 break;
5226
5227 case STATE_ENDNUL:
5228 /* Past the NUL bytes in the node. */
5229 if (fword[sp->ts_fidx] == 0)
5230 {
5231 /* The badword ends, can't use the bytes in this node. */
5232 sp->ts_state = STATE_DEL;
5233 break;
5234 }
5235 sp->ts_state = STATE_PLAIN;
5236 /*FALLTHROUGH*/
5237
5238 case STATE_PLAIN:
5239 /*
5240 * Go over all possible bytes at this node, add each to
5241 * tword[] and use child node. "ts_curi" is the index.
5242 */
5243 arridx = sp->ts_arridx;
5244 if (sp->ts_curi > byts[arridx])
5245 {
5246 /* Done all bytes at this node, do next state. When still
5247 * at already changed bytes skip the other tricks. */
5248 if (sp->ts_fidx >= sp->ts_fidxtry)
5249 sp->ts_state = STATE_DEL;
5250 else
5251 sp->ts_state = STATE_FINAL;
5252 }
5253 else
5254 {
5255 arridx += sp->ts_curi++;
5256 c = byts[arridx];
5257
5258 /* Normal byte, go one level deeper. If it's not equal to
5259 * the byte in the bad word adjust the score. But don't
5260 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00005261 if (c == fword[sp->ts_fidx]
5262#ifdef FEAT_MBYTE
5263 || (sp->ts_tcharlen > 0
5264 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005265#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00005266 )
5267 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005268 else
5269 newscore = SCORE_SUBST;
5270 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
5271 && try_deeper(su, stack, depth, newscore))
5272 {
5273 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005274 sp = &stack[depth];
5275 ++sp->ts_fidx;
5276 tword[sp->ts_twordlen++] = c;
5277 sp->ts_arridx = idxs[arridx];
5278#ifdef FEAT_MBYTE
5279 if (newscore == SCORE_SUBST)
5280 sp->ts_isdiff = DIFF_YES;
5281 if (has_mbyte)
5282 {
5283 /* Multi-byte characters are a bit complicated to
5284 * handle: They differ when any of the bytes
5285 * differ and then their length may also differ. */
5286 if (sp->ts_tcharlen == 0)
5287 {
5288 /* First byte. */
5289 sp->ts_tcharidx = 0;
5290 sp->ts_tcharlen = MB_BYTE2LEN(c);
5291 sp->ts_fcharstart = sp->ts_fidx - 1;
5292 sp->ts_isdiff = (newscore != 0)
5293 ? DIFF_YES : DIFF_NONE;
5294 }
5295 else if (sp->ts_isdiff == DIFF_INSERT)
5296 /* When inserting trail bytes don't advance in
5297 * the bad word. */
5298 --sp->ts_fidx;
5299 if (++sp->ts_tcharidx == sp->ts_tcharlen)
5300 {
5301 /* Last byte of character. */
5302 if (sp->ts_isdiff == DIFF_YES)
5303 {
5304 /* Correct ts_fidx for the byte length of
5305 * the character (we didn't check that
5306 * before). */
5307 sp->ts_fidx = sp->ts_fcharstart
5308 + MB_BYTE2LEN(
5309 fword[sp->ts_fcharstart]);
5310
5311 /* For a similar character adjust score
5312 * from SCORE_SUBST to SCORE_SIMILAR. */
5313 if (lp->lp_slang->sl_has_map
5314 && similar_chars(lp->lp_slang,
5315 mb_ptr2char(tword
5316 + sp->ts_twordlen
5317 - sp->ts_tcharlen),
5318 mb_ptr2char(fword
5319 + sp->ts_fcharstart)))
5320 sp->ts_score -=
5321 SCORE_SUBST - SCORE_SIMILAR;
5322 }
5323
5324 /* Starting a new char, reset the length. */
5325 sp->ts_tcharlen = 0;
5326 }
5327 }
5328 else
5329#endif
5330 {
5331 /* If we found a similar char adjust the score.
5332 * We do this after calling try_deeper() because
5333 * it's slow. */
5334 if (newscore != 0
5335 && lp->lp_slang->sl_has_map
5336 && similar_chars(lp->lp_slang,
5337 c, fword[sp->ts_fidx - 1]))
5338 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
5339 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005340 }
5341 }
5342 break;
5343
5344 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00005345#ifdef FEAT_MBYTE
5346 /* When past the first byte of a multi-byte char don't try
5347 * delete/insert/swap a character. */
5348 if (has_mbyte && sp->ts_tcharlen > 0)
5349 {
5350 sp->ts_state = STATE_FINAL;
5351 break;
5352 }
5353#endif
5354 /*
5355 * Try skipping one character in the bad word (delete it).
5356 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005357 sp->ts_state = STATE_INS;
5358 sp->ts_curi = 1;
5359 if (fword[sp->ts_fidx] != NUL
5360 && try_deeper(su, stack, depth, SCORE_DEL))
5361 {
5362 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005363#ifdef FEAT_MBYTE
5364 if (has_mbyte)
5365 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
5366 else
5367#endif
5368 ++stack[depth].ts_fidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005369 break;
5370 }
5371 /*FALLTHROUGH*/
5372
5373 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00005374 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005375 * node. */
5376 n = sp->ts_arridx;
5377 if (sp->ts_curi > byts[n])
5378 {
5379 /* Done all bytes at this node, do next state. */
5380 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005381 }
5382 else
5383 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005384 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005385 n += sp->ts_curi++;
5386 c = byts[n];
5387 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
5388 {
5389 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005390 sp = &stack[depth];
5391 tword[sp->ts_twordlen++] = c;
5392 sp->ts_arridx = idxs[n];
5393#ifdef FEAT_MBYTE
5394 if (has_mbyte)
5395 {
5396 fl = MB_BYTE2LEN(c);
5397 if (fl > 1)
5398 {
5399 /* There are following bytes for the same
5400 * character. We must find all bytes before
5401 * trying delete/insert/swap/etc. */
5402 sp->ts_tcharlen = fl;
5403 sp->ts_tcharidx = 1;
5404 sp->ts_isdiff = DIFF_INSERT;
5405 }
5406 }
5407#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005408 }
5409 }
5410 break;
5411
5412 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00005413 /*
5414 * Swap two bytes in the bad word: "12" -> "21".
5415 * We change "fword" here, it's changed back afterwards.
5416 */
5417 p = fword + sp->ts_fidx;
5418 c = *p;
5419 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005420 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005421 /* End of word, can't swap or replace. */
5422 sp->ts_state = STATE_FINAL;
5423 break;
5424 }
5425#ifdef FEAT_MBYTE
5426 if (has_mbyte)
5427 {
5428 n = mb_ptr2len_check(p);
5429 c = mb_ptr2char(p);
5430 c2 = mb_ptr2char(p + n);
5431 }
5432 else
5433#endif
5434 c2 = p[1];
5435 if (c == c2)
5436 {
5437 /* Characters are identical, swap won't do anything. */
5438 sp->ts_state = STATE_SWAP3;
5439 break;
5440 }
5441 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
5442 {
5443 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005444 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005445#ifdef FEAT_MBYTE
5446 if (has_mbyte)
5447 {
5448 fl = mb_char2len(c2);
5449 mch_memmove(p, p + n, fl);
5450 mb_char2bytes(c, p + fl);
5451 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
5452 }
5453 else
5454#endif
5455 {
5456 p[0] = c2;
5457 p[1] = c;
5458 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
5459 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005460 }
5461 else
5462 /* If this swap doesn't work then SWAP3 won't either. */
5463 sp->ts_state = STATE_REP_INI;
5464 break;
5465
Bram Moolenaarea424162005-06-16 21:51:00 +00005466 case STATE_UNSWAP:
5467 /* Undo the STATE_SWAP swap: "21" -> "12". */
5468 p = fword + sp->ts_fidx;
5469#ifdef FEAT_MBYTE
5470 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005471 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005472 n = MB_BYTE2LEN(*p);
5473 c = mb_ptr2char(p + n);
5474 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
5475 mb_char2bytes(c, p);
5476 }
5477 else
5478#endif
5479 {
5480 c = *p;
5481 *p = p[1];
5482 p[1] = c;
5483 }
5484 /*FALLTHROUGH*/
5485
5486 case STATE_SWAP3:
5487 /* Swap two bytes, skipping one: "123" -> "321". We change
5488 * "fword" here, it's changed back afterwards. */
5489 p = fword + sp->ts_fidx;
5490#ifdef FEAT_MBYTE
5491 if (has_mbyte)
5492 {
5493 n = mb_ptr2len_check(p);
5494 c = mb_ptr2char(p);
5495 fl = mb_ptr2len_check(p + n);
5496 c2 = mb_ptr2char(p + n);
5497 c3 = mb_ptr2char(p + n + fl);
5498 }
5499 else
5500#endif
5501 {
5502 c = *p;
5503 c2 = p[1];
5504 c3 = p[2];
5505 }
5506
5507 /* When characters are identical: "121" then SWAP3 result is
5508 * identical, ROT3L result is same as SWAP: "211", ROT3L
5509 * result is same as SWAP on next char: "112". Thus skip all
5510 * swapping. Also skip when c3 is NUL. */
5511 if (c == c3 || c3 == NUL)
5512 {
5513 sp->ts_state = STATE_REP_INI;
5514 break;
5515 }
5516 if (try_deeper(su, stack, depth, SCORE_SWAP3))
5517 {
5518 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005519 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005520#ifdef FEAT_MBYTE
5521 if (has_mbyte)
5522 {
5523 tl = mb_char2len(c3);
5524 mch_memmove(p, p + n + fl, tl);
5525 mb_char2bytes(c2, p + tl);
5526 mb_char2bytes(c, p + fl + tl);
5527 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
5528 }
5529 else
5530#endif
5531 {
5532 p[0] = p[2];
5533 p[2] = c;
5534 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5535 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005536 }
5537 else
5538 sp->ts_state = STATE_REP_INI;
5539 break;
5540
Bram Moolenaarea424162005-06-16 21:51:00 +00005541 case STATE_UNSWAP3:
5542 /* Undo STATE_SWAP3: "321" -> "123" */
5543 p = fword + sp->ts_fidx;
5544#ifdef FEAT_MBYTE
5545 if (has_mbyte)
5546 {
5547 n = MB_BYTE2LEN(*p);
5548 c2 = mb_ptr2char(p + n);
5549 fl = MB_BYTE2LEN(p[n]);
5550 c = mb_ptr2char(p + n + fl);
5551 tl = MB_BYTE2LEN(p[n + fl]);
5552 mch_memmove(p + fl + tl, p, n);
5553 mb_char2bytes(c, p);
5554 mb_char2bytes(c2, p + tl);
5555 }
5556 else
5557#endif
5558 {
5559 c = *p;
5560 *p = p[2];
5561 p[2] = c;
5562 }
5563 /*FALLTHROUGH*/
5564
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005565 case STATE_ROT3L:
Bram Moolenaarea424162005-06-16 21:51:00 +00005566 /* Rotate three characters left: "123" -> "231". We change
5567 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005568 if (try_deeper(su, stack, depth, SCORE_SWAP3))
5569 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005570 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005571 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005572 p = fword + sp->ts_fidx;
5573#ifdef FEAT_MBYTE
5574 if (has_mbyte)
5575 {
5576 n = mb_ptr2len_check(p);
5577 c = mb_ptr2char(p);
5578 fl = mb_ptr2len_check(p + n);
5579 fl += mb_ptr2len_check(p + n + fl);
5580 mch_memmove(p, p + n, fl);
5581 mb_char2bytes(c, p + fl);
5582 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
5583 }
5584 else
5585#endif
5586 {
5587 c = *p;
5588 *p = p[1];
5589 p[1] = p[2];
5590 p[2] = c;
5591 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5592 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005593 }
5594 else
5595 sp->ts_state = STATE_REP_INI;
5596 break;
5597
Bram Moolenaarea424162005-06-16 21:51:00 +00005598 case STATE_UNROT3L:
5599 /* Undo STATE_ROT3L: "231" -> "123" */
5600 p = fword + sp->ts_fidx;
5601#ifdef FEAT_MBYTE
5602 if (has_mbyte)
5603 {
5604 n = MB_BYTE2LEN(*p);
5605 n += MB_BYTE2LEN(p[n]);
5606 c = mb_ptr2char(p + n);
5607 tl = MB_BYTE2LEN(p[n]);
5608 mch_memmove(p + tl, p, n);
5609 mb_char2bytes(c, p);
5610 }
5611 else
5612#endif
5613 {
5614 c = p[2];
5615 p[2] = p[1];
5616 p[1] = *p;
5617 *p = c;
5618 }
5619 /*FALLTHROUGH*/
5620
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005621 case STATE_ROT3R:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005622 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00005623 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005624 if (try_deeper(su, stack, depth, SCORE_SWAP3))
5625 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005626 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005627 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005628 p = fword + sp->ts_fidx;
5629#ifdef FEAT_MBYTE
5630 if (has_mbyte)
5631 {
5632 n = mb_ptr2len_check(p);
5633 n += mb_ptr2len_check(p + n);
5634 c = mb_ptr2char(p + n);
5635 tl = mb_ptr2len_check(p + n);
5636 mch_memmove(p + tl, p, n);
5637 mb_char2bytes(c, p);
5638 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
5639 }
5640 else
5641#endif
5642 {
5643 c = p[2];
5644 p[2] = p[1];
5645 p[1] = *p;
5646 *p = c;
5647 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5648 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005649 }
5650 else
5651 sp->ts_state = STATE_REP_INI;
5652 break;
5653
Bram Moolenaarea424162005-06-16 21:51:00 +00005654 case STATE_UNROT3R:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005655 /* Undo STATE_ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00005656 p = fword + sp->ts_fidx;
5657#ifdef FEAT_MBYTE
5658 if (has_mbyte)
5659 {
5660 c = mb_ptr2char(p);
5661 tl = MB_BYTE2LEN(*p);
5662 n = MB_BYTE2LEN(p[tl]);
5663 n += MB_BYTE2LEN(p[tl + n]);
5664 mch_memmove(p, p + tl, n);
5665 mb_char2bytes(c, p + n);
5666 }
5667 else
5668#endif
5669 {
5670 c = *p;
5671 *p = p[1];
5672 p[1] = p[2];
5673 p[2] = c;
5674 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005675 /*FALLTHROUGH*/
5676
5677 case STATE_REP_INI:
5678 /* Check if matching with REP items from the .aff file would
5679 * work. Quickly skip if there are no REP items or the score
5680 * is going to be too high anyway. */
5681 gap = &lp->lp_slang->sl_rep;
5682 if (gap->ga_len == 0
5683 || sp->ts_score + SCORE_REP >= su->su_maxscore)
5684 {
5685 sp->ts_state = STATE_FINAL;
5686 break;
5687 }
5688
5689 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00005690 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005691 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
5692 if (sp->ts_curi < 0)
5693 {
5694 sp->ts_state = STATE_FINAL;
5695 break;
5696 }
5697
5698 sp->ts_state = STATE_REP;
5699 /*FALLTHROUGH*/
5700
5701 case STATE_REP:
5702 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00005703 * match replace the characters and check if the resulting
5704 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005705 p = fword + sp->ts_fidx;
5706
5707 gap = &lp->lp_slang->sl_rep;
5708 while (sp->ts_curi < gap->ga_len)
5709 {
5710 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
5711 if (*ftp->ft_from != *p)
5712 {
5713 /* past possible matching entries */
5714 sp->ts_curi = gap->ga_len;
5715 break;
5716 }
5717 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
5718 && try_deeper(su, stack, depth, SCORE_REP))
5719 {
5720 /* Need to undo this afterwards. */
5721 sp->ts_state = STATE_REP_UNDO;
5722
5723 /* Change the "from" to the "to" string. */
5724 ++depth;
5725 fl = STRLEN(ftp->ft_from);
5726 tl = STRLEN(ftp->ft_to);
5727 if (fl != tl)
5728 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
5729 mch_memmove(p, ftp->ft_to, tl);
5730 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005731#ifdef FEAT_MBYTE
5732 stack[depth].ts_tcharlen = 0;
5733#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005734 break;
5735 }
5736 }
5737
5738 if (sp->ts_curi >= gap->ga_len)
5739 /* No (more) matches. */
5740 sp->ts_state = STATE_FINAL;
5741
5742 break;
5743
5744 case STATE_REP_UNDO:
5745 /* Undo a REP replacement and continue with the next one. */
5746 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
5747 + sp->ts_curi - 1;
5748 fl = STRLEN(ftp->ft_from);
5749 tl = STRLEN(ftp->ft_to);
5750 p = fword + sp->ts_fidx;
5751 if (fl != tl)
5752 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
5753 mch_memmove(p, ftp->ft_from, fl);
5754 sp->ts_state = STATE_REP;
5755 break;
5756
5757 default:
5758 /* Did all possible states at this level, go up one level. */
5759 --depth;
5760 }
5761
5762 line_breakcheck();
5763 }
5764 }
5765}
5766
5767/*
5768 * Try going one level deeper in the tree.
5769 */
5770 static int
5771try_deeper(su, stack, depth, score_add)
5772 suginfo_T *su;
5773 trystate_T *stack;
5774 int depth;
5775 int score_add;
5776{
5777 int newscore;
5778
5779 /* Refuse to go deeper if the scrore is getting too big. */
5780 newscore = stack[depth].ts_score + score_add;
5781 if (newscore >= su->su_maxscore)
5782 return FALSE;
5783
Bram Moolenaarea424162005-06-16 21:51:00 +00005784 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005785 stack[depth + 1].ts_state = STATE_START;
5786 stack[depth + 1].ts_score = newscore;
5787 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005788 return TRUE;
5789}
5790
5791/*
5792 * "fword" is a good word with case folded. Find the matching keep-case
5793 * words and put it in "kword".
5794 * Theoretically there could be several keep-case words that result in the
5795 * same case-folded word, but we only find one...
5796 */
5797 static void
5798find_keepcap_word(slang, fword, kword)
5799 slang_T *slang;
5800 char_u *fword;
5801 char_u *kword;
5802{
5803 char_u uword[MAXWLEN]; /* "fword" in upper-case */
5804 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005805 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005806
5807 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005808 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005809 int round[MAXWLEN];
5810 int fwordidx[MAXWLEN];
5811 int uwordidx[MAXWLEN];
5812 int kwordlen[MAXWLEN];
5813
5814 int flen, ulen;
5815 int l;
5816 int len;
5817 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005818 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005819 char_u *p;
5820 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005821 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005822
5823 if (byts == NULL)
5824 {
5825 /* array is empty: "cannot happen" */
5826 *kword = NUL;
5827 return;
5828 }
5829
5830 /* Make an all-cap version of "fword". */
5831 allcap_copy(fword, uword);
5832
5833 /*
5834 * Each character needs to be tried both case-folded and upper-case.
5835 * All this gets very complicated if we keep in mind that changing case
5836 * may change the byte length of a multi-byte character...
5837 */
5838 depth = 0;
5839 arridx[0] = 0;
5840 round[0] = 0;
5841 fwordidx[0] = 0;
5842 uwordidx[0] = 0;
5843 kwordlen[0] = 0;
5844 while (depth >= 0)
5845 {
5846 if (fword[fwordidx[depth]] == NUL)
5847 {
5848 /* We are at the end of "fword". If the tree allows a word to end
5849 * here we have found a match. */
5850 if (byts[arridx[depth] + 1] == 0)
5851 {
5852 kword[kwordlen[depth]] = NUL;
5853 return;
5854 }
5855
5856 /* kword is getting too long, continue one level up */
5857 --depth;
5858 }
5859 else if (++round[depth] > 2)
5860 {
5861 /* tried both fold-case and upper-case character, continue one
5862 * level up */
5863 --depth;
5864 }
5865 else
5866 {
5867 /*
5868 * round[depth] == 1: Try using the folded-case character.
5869 * round[depth] == 2: Try using the upper-case character.
5870 */
5871#ifdef FEAT_MBYTE
5872 if (has_mbyte)
5873 {
5874 flen = mb_ptr2len_check(fword + fwordidx[depth]);
5875 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
5876 }
5877 else
5878#endif
5879 ulen = flen = 1;
5880 if (round[depth] == 1)
5881 {
5882 p = fword + fwordidx[depth];
5883 l = flen;
5884 }
5885 else
5886 {
5887 p = uword + uwordidx[depth];
5888 l = ulen;
5889 }
5890
5891 for (tryidx = arridx[depth]; l > 0; --l)
5892 {
5893 /* Perform a binary search in the list of accepted bytes. */
5894 len = byts[tryidx++];
5895 c = *p++;
5896 lo = tryidx;
5897 hi = tryidx + len - 1;
5898 while (lo < hi)
5899 {
5900 m = (lo + hi) / 2;
5901 if (byts[m] > c)
5902 hi = m - 1;
5903 else if (byts[m] < c)
5904 lo = m + 1;
5905 else
5906 {
5907 lo = hi = m;
5908 break;
5909 }
5910 }
5911
5912 /* Stop if there is no matching byte. */
5913 if (hi < lo || byts[lo] != c)
5914 break;
5915
5916 /* Continue at the child (if there is one). */
5917 tryidx = idxs[lo];
5918 }
5919
5920 if (l == 0)
5921 {
5922 /*
5923 * Found the matching char. Copy it to "kword" and go a
5924 * level deeper.
5925 */
5926 if (round[depth] == 1)
5927 {
5928 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
5929 flen);
5930 kwordlen[depth + 1] = kwordlen[depth] + flen;
5931 }
5932 else
5933 {
5934 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
5935 ulen);
5936 kwordlen[depth + 1] = kwordlen[depth] + ulen;
5937 }
5938 fwordidx[depth + 1] = fwordidx[depth] + flen;
5939 uwordidx[depth + 1] = uwordidx[depth] + ulen;
5940
5941 ++depth;
5942 arridx[depth] = tryidx;
5943 round[depth] = 0;
5944 }
5945 }
5946 }
5947
5948 /* Didn't find it: "cannot happen". */
5949 *kword = NUL;
5950}
5951
5952/*
5953 * Find suggestions by comparing the word in a sound-a-like form.
5954 */
5955 static void
5956spell_try_soundalike(su)
5957 suginfo_T *su;
5958{
5959 char_u salword[MAXWLEN];
5960 char_u tword[MAXWLEN];
5961 char_u tfword[MAXWLEN];
5962 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005963 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005964 int curi[MAXWLEN];
5965 langp_T *lp;
5966 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005967 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005968 int depth;
5969 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005970 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005971 int round;
5972 int flags;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005973 int score, sound_score;
5974 char_u *bp, *sp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005975
5976 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
5977 lp->lp_slang != NULL; ++lp)
5978 {
5979 if (lp->lp_slang->sl_sal.ga_len > 0)
5980 {
5981 /* soundfold the bad word */
5982 spell_soundfold(lp->lp_slang, su->su_fbadword, salword);
5983
5984 /*
5985 * Go through the whole tree, soundfold each word and compare.
5986 * round 1: use the case-folded tree.
5987 * round 2: use the keep-case tree.
5988 */
5989 for (round = 1; round <= 2; ++round)
5990 {
5991 if (round == 1)
5992 {
5993 byts = lp->lp_slang->sl_fbyts;
5994 idxs = lp->lp_slang->sl_fidxs;
5995 }
5996 else
5997 {
5998 byts = lp->lp_slang->sl_kbyts;
5999 idxs = lp->lp_slang->sl_kidxs;
6000 }
6001
6002 depth = 0;
6003 arridx[0] = 0;
6004 curi[0] = 1;
6005 while (depth >= 0 && !got_int)
6006 {
6007 if (curi[depth] > byts[arridx[depth]])
6008 /* Done all bytes at this node, go up one level. */
6009 --depth;
6010 else
6011 {
6012 /* Do one more byte at this node. */
6013 n = arridx[depth] + curi[depth];
6014 ++curi[depth];
6015 c = byts[n];
6016 if (c == 0)
6017 {
6018 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006019 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006020 if (round == 2 || (flags & WF_KEEPCAP) == 0)
6021 {
6022 tword[depth] = NUL;
6023 if (round == 1)
6024 spell_soundfold(lp->lp_slang,
6025 tword, tsalword);
6026 else
6027 {
6028 /* In keep-case tree need to case-fold the
6029 * word. */
6030 (void)spell_casefold(tword, depth,
6031 tfword, MAXWLEN);
6032 spell_soundfold(lp->lp_slang,
6033 tfword, tsalword);
6034 }
6035
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006036 /*
6037 * Accept the word if the sound-folded words
6038 * are (almost) equal.
6039 */
6040 for (bp = salword, sp = tsalword; *bp == *sp;
6041 ++bp, ++sp)
6042 if (*bp == NUL)
6043 break;
6044
6045 if (*bp == *sp)
6046 /* equal */
6047 sound_score = 0;
6048 else if (*bp != NUL && bp[1] != NUL
6049 && *bp == sp[1] && bp[1] == *sp
6050 && STRCMP(bp + 2, sp + 2) == 0)
6051 /* swap two bytes */
6052 sound_score = SCORE_SWAP;
6053 else if (STRCMP(bp + 1, sp) == 0)
6054 /* delete byte */
6055 sound_score = SCORE_DEL;
6056 else if (STRCMP(bp, sp + 1) == 0)
6057 /* insert byte */
6058 sound_score = SCORE_INS;
6059 else if (STRCMP(bp + 1, sp + 1) == 0)
6060 /* skip one byte */
6061 sound_score = SCORE_SUBST;
6062 else
6063 /* not equal or similar */
6064 sound_score = SCORE_MAXMAX;
6065
6066 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006067 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006068 char_u cword[MAXWLEN];
6069 char_u *p;
6070
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006071 if (round == 1 && flags != 0)
6072 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006073 /* Need to fix case according to
6074 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006075 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006076 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006077 }
6078 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006079 p = tword;
6080
6081 /* Compute the score. */
6082 score = spell_edit_score(su->su_badword, p);
6083#ifdef RESCORE
6084 /* give a bonus for the good word sounding
6085 * the same as the bad word */
6086 add_suggestion(su, tword,
6087 RESCORE(score, sound_score),
6088 TRUE);
6089#else
6090 add_suggestion(su, tword,
6091 score + sound_score);
6092#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006093 }
6094 }
6095
6096 /* Skip over other NUL bytes. */
6097 while (byts[n + 1] == 0)
6098 {
6099 ++n;
6100 ++curi[depth];
6101 }
6102 }
6103 else
6104 {
6105 /* Normal char, go one level deeper. */
6106 tword[depth++] = c;
6107 arridx[depth] = idxs[n];
6108 curi[depth] = 1;
6109 }
6110 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006111
6112 line_breakcheck();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006113 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006114 }
6115 }
6116 }
6117}
6118
6119/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006120 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006121 */
6122 static void
6123make_case_word(fword, cword, flags)
6124 char_u *fword;
6125 char_u *cword;
6126 int flags;
6127{
6128 if (flags & WF_ALLCAP)
6129 /* Make it all upper-case */
6130 allcap_copy(fword, cword);
6131 else if (flags & WF_ONECAP)
6132 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006133 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006134 else
6135 /* Use goodword as-is. */
6136 STRCPY(cword, fword);
6137}
6138
Bram Moolenaarea424162005-06-16 21:51:00 +00006139/*
6140 * Use map string "map" for languages "lp".
6141 */
6142 static void
6143set_map_str(lp, map)
6144 slang_T *lp;
6145 char_u *map;
6146{
6147 char_u *p;
6148 int headc = 0;
6149 int c;
6150 int i;
6151
6152 if (*map == NUL)
6153 {
6154 lp->sl_has_map = FALSE;
6155 return;
6156 }
6157 lp->sl_has_map = TRUE;
6158
6159 /* Init the array and hash table empty. */
6160 for (i = 0; i < 256; ++i)
6161 lp->sl_map_array[i] = 0;
6162#ifdef FEAT_MBYTE
6163 hash_init(&lp->sl_map_hash);
6164#endif
6165
6166 /*
6167 * The similar characters are stored separated with slashes:
6168 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
6169 * before the same slash. For characters above 255 sl_map_hash is used.
6170 */
6171 for (p = map; *p != NUL; )
6172 {
6173#ifdef FEAT_MBYTE
6174 c = mb_ptr2char_adv(&p);
6175#else
6176 c = *p++;
6177#endif
6178 if (c == '/')
6179 headc = 0;
6180 else
6181 {
6182 if (headc == 0)
6183 headc = c;
6184
6185#ifdef FEAT_MBYTE
6186 /* Characters above 255 don't fit in sl_map_array[], put them in
6187 * the hash table. Each entry is the char, a NUL the headchar and
6188 * a NUL. */
6189 if (c >= 256)
6190 {
6191 int cl = mb_char2len(c);
6192 int headcl = mb_char2len(headc);
6193 char_u *b;
6194 hash_T hash;
6195 hashitem_T *hi;
6196
6197 b = alloc((unsigned)(cl + headcl + 2));
6198 if (b == NULL)
6199 return;
6200 mb_char2bytes(c, b);
6201 b[cl] = NUL;
6202 mb_char2bytes(headc, b + cl + 1);
6203 b[cl + 1 + headcl] = NUL;
6204 hash = hash_hash(b);
6205 hi = hash_lookup(&lp->sl_map_hash, b, hash);
6206 if (HASHITEM_EMPTY(hi))
6207 hash_add_item(&lp->sl_map_hash, hi, b, hash);
6208 else
6209 {
6210 /* This should have been checked when generating the .spl
6211 * file. */
6212 EMSG(_("E999: duplicate char in MAP entry"));
6213 vim_free(b);
6214 }
6215 }
6216 else
6217#endif
6218 lp->sl_map_array[c] = headc;
6219 }
6220 }
6221}
6222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006223/*
6224 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
6225 * lines in the .aff file.
6226 */
6227 static int
6228similar_chars(slang, c1, c2)
6229 slang_T *slang;
6230 int c1;
6231 int c2;
6232{
Bram Moolenaarea424162005-06-16 21:51:00 +00006233 int m1, m2;
6234#ifdef FEAT_MBYTE
6235 char_u buf[MB_MAXBYTES];
6236 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006237
Bram Moolenaarea424162005-06-16 21:51:00 +00006238 if (c1 >= 256)
6239 {
6240 buf[mb_char2bytes(c1, buf)] = 0;
6241 hi = hash_find(&slang->sl_map_hash, buf);
6242 if (HASHITEM_EMPTY(hi))
6243 m1 = 0;
6244 else
6245 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6246 }
6247 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006248#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00006249 m1 = slang->sl_map_array[c1];
6250 if (m1 == 0)
6251 return FALSE;
6252
6253
6254#ifdef FEAT_MBYTE
6255 if (c2 >= 256)
6256 {
6257 buf[mb_char2bytes(c2, buf)] = 0;
6258 hi = hash_find(&slang->sl_map_hash, buf);
6259 if (HASHITEM_EMPTY(hi))
6260 m2 = 0;
6261 else
6262 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6263 }
6264 else
6265#endif
6266 m2 = slang->sl_map_array[c2];
6267
6268 return m1 == m2;
6269}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006270
6271/*
6272 * Add a suggestion to the list of suggestions.
6273 * Do not add a duplicate suggestion or suggestions with a bad score.
6274 * When "use_score" is not zero it's used, otherwise the score is computed
6275 * with spell_edit_score().
6276 */
6277 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006278add_suggestion(su, goodword, score
6279#ifdef RESCORE
6280 , had_bonus
6281#endif
6282 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006283 suginfo_T *su;
6284 char_u *goodword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006285 int score;
6286#ifdef RESCORE
6287 int had_bonus; /* set st_had_bonus */
6288#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006289{
6290 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006291 int i;
6292#ifdef SOUNDFOLD_SCORE
6293 char_u fword[MAXWLEN];
6294 char_u salword[MAXWLEN];
6295#endif
6296
6297 /* Check that the word wasn't banned. */
6298 if (was_banned(su, goodword))
6299 return;
6300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006301 if (score <= su->su_maxscore)
6302 {
6303#ifdef SOUNDFOLD_SCORE
6304 /* Add to the score when the word sounds differently.
6305 * This is slow... */
6306 if (su->su_slang->sl_sal.ga_len > 0)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006307 score += spell_sound_score(su->su_slang, fword, su->su_salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006308#endif
6309
6310 /* Check if the word is already there. */
6311 stp = &SUG(su, 0);
6312 for (i = su->su_ga.ga_len - 1; i >= 0; --i)
6313 if (STRCMP(stp[i].st_word, goodword) == 0)
6314 {
6315 /* Found it. Remember the lowest score. */
6316 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006318 stp[i].st_score = score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006319#ifdef RESCORE
6320 stp[i].st_had_bonus = had_bonus;
6321#endif
6322 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006323 break;
6324 }
6325
6326 if (i < 0 && ga_grow(&su->su_ga, 1) == OK)
6327 {
6328 /* Add a suggestion. */
6329 stp = &SUG(su, su->su_ga.ga_len);
6330 stp->st_word = vim_strsave(goodword);
6331 if (stp->st_word != NULL)
6332 {
6333 stp->st_score = score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006334#ifdef RESCORE
6335 stp->st_had_bonus = had_bonus;
6336#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006337 stp->st_orglen = su->su_badlen;
6338 ++su->su_ga.ga_len;
6339
6340 /* If we have too many suggestions now, sort the list and keep
6341 * the best suggestions. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006342 if (su->su_ga.ga_len > SUG_MAX_COUNT)
6343 cleanup_suggestions(su, SUG_CLEAN_COUNT);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006344 }
6345 }
6346 }
6347}
6348
6349/*
6350 * Add a word to be banned.
6351 */
6352 static void
6353add_banned(su, word)
6354 suginfo_T *su;
6355 char_u *word;
6356{
6357 char_u *s = vim_strsave(word);
6358 hash_T hash;
6359 hashitem_T *hi;
6360
6361 if (s != NULL)
6362 {
6363 hash = hash_hash(s);
6364 hi = hash_lookup(&su->su_banned, s, hash);
6365 if (HASHITEM_EMPTY(hi))
6366 hash_add_item(&su->su_banned, hi, s, hash);
6367 }
6368}
6369
6370/*
6371 * Return TRUE if a word appears in the list of banned words.
6372 */
6373 static int
6374was_banned(su, word)
6375 suginfo_T *su;
6376 char_u *word;
6377{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006378 hashitem_T *hi = hash_find(&su->su_banned, word);
6379
6380 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006381}
6382
6383/*
6384 * Free the banned words in "su".
6385 */
6386 static void
6387free_banned(su)
6388 suginfo_T *su;
6389{
6390 int todo;
6391 hashitem_T *hi;
6392
6393 todo = su->su_banned.ht_used;
6394 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
6395 {
6396 if (!HASHITEM_EMPTY(hi))
6397 {
6398 vim_free(hi->hi_key);
6399 --todo;
6400 }
6401 }
6402 hash_clear(&su->su_banned);
6403}
6404
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006405#ifdef RESCORE
6406/*
6407 * Recompute the score if sound-folding is possible. This is slow,
6408 * thus only done for the final results.
6409 */
6410 static void
6411rescore_suggestions(su)
6412 suginfo_T *su;
6413{
6414 langp_T *lp;
6415 suggest_T *stp;
6416 char_u sal_badword[MAXWLEN];
6417 int score;
6418 int i;
6419
6420 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6421 lp->lp_slang != NULL; ++lp)
6422 {
6423 if (lp->lp_slang->sl_sal.ga_len > 0)
6424 {
6425 /* soundfold the bad word */
6426 spell_soundfold(lp->lp_slang, su->su_fbadword, sal_badword);
6427
6428 for (i = 0; i < su->su_ga.ga_len; ++i)
6429 {
6430 stp = &SUG(su, i);
6431 if (!stp->st_had_bonus)
6432 {
6433 score = spell_sound_score(lp->lp_slang, stp->st_word,
6434 sal_badword);
6435 stp->st_score = RESCORE(stp->st_score, score);
6436 }
6437 }
6438 break;
6439 }
6440 }
6441}
6442#endif
6443
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006444static int
6445#ifdef __BORLANDC__
6446_RTLENTRYF
6447#endif
6448sug_compare __ARGS((const void *s1, const void *s2));
6449
6450/*
6451 * Function given to qsort() to sort the suggestions on st_score.
6452 */
6453 static int
6454#ifdef __BORLANDC__
6455_RTLENTRYF
6456#endif
6457sug_compare(s1, s2)
6458 const void *s1;
6459 const void *s2;
6460{
6461 suggest_T *p1 = (suggest_T *)s1;
6462 suggest_T *p2 = (suggest_T *)s2;
6463
6464 return p1->st_score - p2->st_score;
6465}
6466
6467/*
6468 * Cleanup the suggestions:
6469 * - Sort on score.
6470 * - Remove words that won't be displayed.
6471 */
6472 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006473cleanup_suggestions(su, keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006474 suginfo_T *su;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006475 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006476{
6477 suggest_T *stp = &SUG(su, 0);
6478 int i;
6479
6480 /* Sort the list. */
6481 qsort(su->su_ga.ga_data, (size_t)su->su_ga.ga_len,
6482 sizeof(suggest_T), sug_compare);
6483
6484 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006485 if (su->su_ga.ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006486 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006487 for (i = keep; i < su->su_ga.ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006488 vim_free(stp[i].st_word);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006489 su->su_ga.ga_len = keep;
6490 su->su_maxscore = stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006491 }
6492}
6493
6494/*
6495 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
6496 */
6497 static void
6498spell_soundfold(slang, inword, res)
6499 slang_T *slang;
6500 char_u *inword;
6501 char_u *res;
6502{
6503 fromto_T *ftp;
6504 char_u word[MAXWLEN];
6505#ifdef FEAT_MBYTE
6506 int l;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006507 int found_mbyte = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006508#endif
6509 char_u *s;
6510 char_u *t;
6511 int i, j, z;
6512 int n, k = 0;
6513 int z0;
6514 int k0;
6515 int n0;
6516 int c;
6517 int pri;
6518 int p0 = -333;
6519 int c0;
6520
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006521 /* Remove accents, if wanted. We actually remove all non-word characters.
6522 * But keep white space. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006523 if (slang->sl_rem_accents)
6524 {
6525 t = word;
6526 for (s = inword; *s != NUL; )
6527 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006528 if (vim_iswhite(*s))
6529 *t++ = *s++;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006530#ifdef FEAT_MBYTE
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006531 else if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006532 {
6533 l = mb_ptr2len_check(s);
6534 if (SPELL_ISWORDP(s))
6535 {
6536 mch_memmove(t, s, l);
6537 t += l;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006538 if (l > 1)
6539 found_mbyte = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006540 }
6541 s += l;
6542 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006543#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006544 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006545 {
6546 if (SPELL_ISWORDP(s))
6547 *t++ = *s;
6548 ++s;
6549 }
6550 }
6551 *t = NUL;
6552 }
6553 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006554 {
6555#ifdef FEAT_MBYTE
6556 if (has_mbyte)
6557 for (s = inword; *s != NUL; s += l)
6558 if ((l = mb_ptr2len_check(s)) > 1)
6559 {
6560 found_mbyte = TRUE;
6561 break;
6562 }
6563#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006564 STRCPY(word, inword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006565 }
6566
6567#ifdef FEAT_MBYTE
6568 /* If there are multi-byte characters in the word return it as-is, because
6569 * the following won't work. */
6570 if (found_mbyte)
6571 {
6572 STRCPY(res, word);
6573 return;
6574 }
6575#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006576
6577 ftp = (fromto_T *)slang->sl_sal.ga_data;
6578
6579 /*
6580 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006581 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006582 * TODO: support for multi-byte chars.
6583 */
6584 i = j = z = 0;
6585 while ((c = word[i]) != NUL)
6586 {
6587 n = slang->sl_sal_first[c];
6588 z0 = 0;
6589
6590 if (n >= 0)
6591 {
6592 /* check all rules for the same letter */
6593 while (ftp[n].ft_from[0] == c)
6594 {
6595 /* check whole string */
6596 k = 1; /* number of found letters */
6597 pri = 5; /* default priority */
6598 s = ftp[n].ft_from;
6599 s++; /* important for (see below) "*(s-1)" */
6600
6601 /* Skip over normal letters that match with the word. */
6602 while (*s != NUL && word[i + k] == *s
6603 && !vim_isdigit(*s) && strchr("(-<^$", *s) == NULL)
6604 {
6605 k++;
6606 s++;
6607 }
6608
6609 if (*s == '(')
6610 {
6611 /* check alternate letters in "(..)" */
6612 for (t = s + 1; *t != ')' && *t != NUL; ++t)
6613 if (*t == word[i + k])
6614 {
6615 /* match */
6616 ++k;
6617 for (s = t + 1; *s != NUL; ++s)
6618 if (*s == ')')
6619 {
6620 ++s;
6621 break;
6622 }
6623 break;
6624 }
6625 }
6626
6627 p0 = *s;
6628 k0 = k;
6629 while (*s == '-' && k > 1)
6630 {
6631 k--;
6632 s++;
6633 }
6634 if (*s == '<')
6635 s++;
6636 if (vim_isdigit(*s))
6637 {
6638 /* determine priority */
6639 pri = *s - '0';
6640 s++;
6641 }
6642 if (*s == '^' && *(s + 1) == '^')
6643 s++;
6644
6645 if (*s == NUL
6646 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006647 && (i == 0 || !(word[i - 1] == ' '
6648 || SPELL_ISWORDP(word + i - 1)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006649 && (*(s + 1) != '$'
6650 || (!SPELL_ISWORDP(word + i + k0))))
6651 || (*s == '$' && i > 0
6652 && SPELL_ISWORDP(word + i - 1)
6653 && (!SPELL_ISWORDP(word + i + k0))))
6654 {
6655 /* search for followup rules, if: */
6656 /* followup and k > 1 and NO '-' in searchstring */
6657 c0 = word[i + k - 1];
6658 n0 = slang->sl_sal_first[c0];
6659
6660 if (slang->sl_followup && k > 1 && n0 >= 0
6661 && p0 != '-' && word[i + k] != NUL)
6662 {
6663 /* test follow-up rule for "word[i + k]" */
6664 while (ftp[n0].ft_from[0] == c0)
6665 {
6666
6667 /* check whole string */
6668 k0 = k;
6669 p0 = 5;
6670 s = ftp[n0].ft_from;
6671 s++;
6672 while (*s != NUL && word[i+k0] == *s
6673 && !vim_isdigit(*s)
6674 && strchr("(-<^$",*s) == NULL)
6675 {
6676 k0++;
6677 s++;
6678 }
6679 if (*s == '(')
6680 {
6681 /* check alternate letters in "(..)" */
6682 for (t = s + 1; *t != ')' && *t != NUL; ++t)
6683 if (*t == word[i + k0])
6684 {
6685 /* match */
6686 ++k0;
6687 for (s = t + 1; *s != NUL; ++s)
6688 if (*s == ')')
6689 {
6690 ++s;
6691 break;
6692 }
6693 break;
6694 }
6695 }
6696 while (*s == '-')
6697 {
6698 /* "k0" gets NOT reduced */
6699 /* because "if (k0 == k)" */
6700 s++;
6701 }
6702 if (*s == '<')
6703 s++;
6704 if (vim_isdigit(*s))
6705 {
6706 p0 = *s - '0';
6707 s++;
6708 }
6709
6710 if (*s == NUL
6711 /* *s == '^' cuts */
6712 || (*s == '$'
6713 && !SPELL_ISWORDP(word + i + k0)))
6714 {
6715 if (k0 == k)
6716 {
6717 /* this is just a piece of the string */
6718 ++n0;
6719 continue;
6720 }
6721
6722 if (p0 < pri)
6723 {
6724 /* priority too low */
6725 ++n0;
6726 continue;
6727 }
6728 /* rule fits; stop search */
6729 break;
6730 }
6731 ++n0;
6732 }
6733
6734 if (p0 >= pri && ftp[n0].ft_from[0] == c0)
6735 {
6736 ++n;
6737 continue;
6738 }
6739 }
6740
6741 /* replace string */
6742 s = ftp[n].ft_to;
6743 p0 = (ftp[n].ft_from[0] != NUL
6744 && vim_strchr(ftp[n].ft_from + 1,
6745 '<') != NULL) ? 1 : 0;
6746 if (p0 == 1 && z == 0)
6747 {
6748 /* rule with '<' is used */
6749 if (j > 0 && *s != NUL
6750 && (res[j - 1] == c || res[j - 1] == *s))
6751 j--;
6752 z0 = 1;
6753 z = 1;
6754 k0 = 0;
6755 while (*s != NUL && word[i+k0] != NUL)
6756 {
6757 word[i + k0] = *s;
6758 k0++;
6759 s++;
6760 }
6761 if (k > k0)
6762 mch_memmove(word + i + k0, word + i + k,
6763 STRLEN(word + i + k) + 1);
6764
6765 /* new "actual letter" */
6766 c = word[i];
6767 }
6768 else
6769 {
6770 /* no '<' rule used */
6771 i += k - 1;
6772 z = 0;
6773 while (*s != NUL && s[1] != NUL && j < MAXWLEN)
6774 {
6775 if (j == 0 || res[j - 1] != *s)
6776 {
6777 res[j] = *s;
6778 j++;
6779 }
6780 s++;
6781 }
6782 /* new "actual letter" */
6783 c = *s;
6784 if (ftp[n].ft_from[0] != NUL
6785 && strstr((char *)ftp[n].ft_from + 1,
6786 "^^") != NULL)
6787 {
6788 if (c != NUL)
6789 {
6790 res[j] = c;
6791 j++;
6792 }
6793 mch_memmove(word, word + i + 1,
6794 STRLEN(word + i + 1) + 1);
6795 i = 0;
6796 z0 = 1;
6797 }
6798 }
6799 break;
6800 }
6801 ++n;
6802 }
6803 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006804 else if (vim_iswhite(c))
6805 {
6806 c = ' ';
6807 k = 1;
6808 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006809
6810 if (z0 == 0)
6811 {
6812 if (k && !p0 && j < MAXWLEN && c != NUL
6813 && (!slang->sl_collapse || j == 0 || res[j - 1] != c))
6814 {
6815 /* condense only double letters */
6816 res[j] = c;
6817 j++;
6818 }
6819
6820 i++;
6821 z = 0;
6822 k = 0;
6823 }
6824 }
6825
6826 res[j] = NUL;
6827}
6828
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006829#if defined(RESCORE) || defined(SOUNDFOLD_SCORE)
6830/*
6831 * Return the score for how much words sound different.
6832 */
6833 static int
6834spell_sound_score(slang, goodword, badsound)
6835 slang_T *slang;
6836 char_u *goodword; /* good word */
6837 char_u *badsound; /* sound-folded bad word */
6838{
6839 char_u fword[MAXWLEN];
6840 char_u goodsound[MAXWLEN];
6841 int score;
6842
6843 /* Case-fold the word, needed for sound folding. */
6844 (void)spell_casefold(goodword, STRLEN(goodword), fword, MAXWLEN);
6845
6846 /* sound-fold the good word */
6847 spell_soundfold(slang, fword, goodsound);
6848
6849 /* compute the edit distance-score of the sounds */
6850 score = spell_edit_score(badsound, goodsound);
6851
6852 /* Correction: adding/inserting "*" at the start (word starts with vowel)
6853 * shouldn't be counted so much, vowels halfway the word aren't counted at
6854 * all. */
6855 if (*badsound != *goodsound && (*badsound == '*' || *goodsound == '*'))
6856 score -= SCORE_DEL / 2;
6857
6858 return score;
6859}
6860#endif
6861
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006862/*
6863 * Compute the "edit distance" to turn "badword" into "goodword". The less
6864 * deletes/inserts/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006865 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006866 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006867 * It has been converted from C++ to C and modified to support multi-byte
6868 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006869 */
6870 static int
6871spell_edit_score(badword, goodword)
6872 char_u *badword;
6873 char_u *goodword;
6874{
6875 int *cnt;
6876 int badlen, goodlen;
6877 int j, i;
6878 int t;
6879 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006880 int pbc, pgc;
6881#ifdef FEAT_MBYTE
6882 char_u *p;
6883 int wbadword[MAXWLEN];
6884 int wgoodword[MAXWLEN];
6885
6886 if (has_mbyte)
6887 {
6888 /* Get the characters from the multi-byte strings and put them in an
6889 * int array for easy access. */
6890 for (p = badword, badlen = 0; *p != NUL; )
6891 wbadword[badlen++] = mb_ptr2char_adv(&p);
6892 ++badlen;
6893 for (p = goodword, goodlen = 0; *p != NUL; )
6894 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
6895 ++goodlen;
6896 }
6897 else
6898#endif
6899 {
6900 badlen = STRLEN(badword) + 1;
6901 goodlen = STRLEN(goodword) + 1;
6902 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006903
6904 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
6905#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006906 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
6907 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006908 if (cnt == NULL)
6909 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006910
6911 CNT(0, 0) = 0;
6912 for (j = 1; j <= goodlen; ++j)
6913 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
6914
6915 for (i = 1; i <= badlen; ++i)
6916 {
6917 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
6918 for (j = 1; j <= goodlen; ++j)
6919 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006920#ifdef FEAT_MBYTE
6921 if (has_mbyte)
6922 {
6923 bc = wbadword[i - 1];
6924 gc = wgoodword[j - 1];
6925 }
6926 else
6927#endif
6928 {
6929 bc = badword[i - 1];
6930 gc = goodword[j - 1];
6931 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006932 if (bc == gc)
6933 CNT(i, j) = CNT(i - 1, j - 1);
6934 else
6935 {
6936 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006937 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006938 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
6939 else
6940 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
6941
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006942 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006943 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006944#ifdef FEAT_MBYTE
6945 if (has_mbyte)
6946 {
6947 pbc = wbadword[i - 2];
6948 pgc = wgoodword[j - 2];
6949 }
6950 else
6951#endif
6952 {
6953 pbc = badword[i - 2];
6954 pgc = goodword[j - 2];
6955 }
6956 if (bc == pgc && pbc == gc)
6957 {
6958 t = SCORE_SWAP + CNT(i - 2, j - 2);
6959 if (t < CNT(i, j))
6960 CNT(i, j) = t;
6961 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006962 }
6963 t = SCORE_DEL + CNT(i - 1, j);
6964 if (t < CNT(i, j))
6965 CNT(i, j) = t;
6966 t = SCORE_INS + CNT(i, j - 1);
6967 if (t < CNT(i, j))
6968 CNT(i, j) = t;
6969 }
6970 }
6971 }
6972 return CNT(badlen - 1, goodlen - 1);
6973}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006974
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006975#endif /* FEAT_SYN_HL */