blob: a66ff61b00dd59e5f680deb8c214fa400c3a3b86 [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 Moolenaar9f30f502005-06-14 22:01:04 +000054 * Use this to adjust the score after finding suggestions, based on the
55 * suggested word sounding like the bad word. This is much faster than doing
56 * it for every possible suggestion.
57 * Disadvantage: When "the" is typed as "hte" it sounds different and goes
58 * down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000059 * Used when 'spellsuggest' is set to "best".
60 */
61#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
62
63/*
64 * The double scoring mechanism is based on the principle that there are two
65 * kinds of spelling mistakes:
66 * 1. You know how to spell the word, but mistype something. This results in
67 * a small editing distance (character swapped/omitted/inserted) and
68 * possibly a word that sounds completely different.
69 * 2. You don't know how to spell the word and type something that sounds
70 * right. The edit distance can be big but the word is similar after
71 * sound-folding.
72 * Since scores for these two mistakes will be very different we use a list
73 * for each.
74 * The sound-folding is slow, only do double scoring when 'spellsuggest' is
75 * "double".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000076 */
77
78/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000079 * Vim spell file format: <HEADER>
80 * <SUGGEST>
81 * <LWORDTREE>
82 * <KWORDTREE>
83 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000084 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000085 * <HEADER>: <fileID>
86 * <regioncnt> <regionname> ...
87 * <charflagslen> <charflags>
88 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000089 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000090 * <prefcondcnt> <prefcond> ...
Bram Moolenaar51485f02005-06-04 21:55:20 +000091 *
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000092 * <fileID> 10 bytes "VIMspell08"
Bram Moolenaar51485f02005-06-04 21:55:20 +000093 * <regioncnt> 1 byte number of regions following (8 supported)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000094 * <regionname> 2 bytes Region name: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +000095 * First <regionname> is region 1.
96 *
97 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
98 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +000099 * 0x01 word character CF_WORD
100 * 0x02 upper-case character CF_UPPER
Bram Moolenaar51485f02005-06-04 21:55:20 +0000101 * <fcharslen> 2 bytes Number of bytes in <fchars>.
102 * <fchars> N bytes Folded characters, first one is for character 128.
103 *
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000104 * <midwordlen> 2 bytes Number of bytes in <midword>.
105 * <midword> N bytes Characters that are word characters only when used
106 * in the middle of a word.
107 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000108 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
109 *
110 * <prefcond> : <condlen> <condstr>
111 *
112 * <condlen> 1 byte Length of <condstr>.
113 *
114 * <condstr> N bytes Condition for the prefix.
115 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000116 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000117 * <SUGGEST> : <repcount> <rep> ...
118 * <salflags> <salcount> <sal> ...
119 * <maplen> <mapstr>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000120 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000121 * <repcount> 2 bytes number of <rep> items, MSB first.
122 *
123 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
124 *
125 * <repfromlen> 1 byte length of <repfrom>
126 *
127 * <repfrom> N bytes "from" part of replacement
128 *
129 * <reptolen> 1 byte length of <repto>
130 *
131 * <repto> N bytes "to" part of replacement
132 *
133 * <salflags> 1 byte flags for soundsalike conversion:
134 * SAL_F0LLOWUP
135 * SAL_COLLAPSE
136 * SAL_REM_ACCENTS
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000137 * SAL_SOFO: SOFOFROM and SOFOTO used instead of SAL
138 *
139 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000140 *
141 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
142 *
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000143 * <salfromlen> 1-2 bytes length of <salfrom> (2 bytes for SAL_SOFO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000144 *
145 * <salfrom> N bytes "from" part of soundsalike
146 *
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000147 * <saltolen> 1-2 bytes length of <salto> (2 bytes for SAL_SOFO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000148 *
149 * <salto> N bytes "to" part of soundsalike
150 *
151 * <maplen> 2 bytes length of <mapstr>, MSB first
152 *
153 * <mapstr> N bytes String with sequences of similar characters,
154 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000155 *
156 *
157 * <LWORDTREE>: <wordtree>
158 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000159 * <KWORDTREE>: <wordtree>
160 *
161 * <PREFIXTREE>: <wordtree>
162 *
163 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000164 * <wordtree>: <nodecount> <nodedata> ...
165 *
166 * <nodecount> 4 bytes Number of nodes following. MSB first.
167 *
168 * <nodedata>: <siblingcount> <sibling> ...
169 *
170 * <siblingcount> 1 byte Number of siblings in this node. The siblings
171 * follow in sorted order.
172 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000173 * <sibling>: <byte> [ <nodeidx> <xbyte>
174 * | <flags> [<region>] [<prefixID>]
175 * | <prefixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000176 *
177 * <byte> 1 byte Byte value of the sibling. Special cases:
178 * BY_NOFLAGS: End of word without flags and for all
179 * regions.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000180 * For PREFIXTREE <prefixID> and
181 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000182 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000183 * For PREFIXTREE <prefixID> and
184 * <prefcondnr> follow for rare prefix.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000185 * BY_FLAGS2: End of word, <flags> and <flags2>
186 * follow. Not used in PREFIXTREE.
187 * BY_PFX_NC: For PREFIXTREE <prefixID> and
188 * <prefcondnr> follow for non-combining
189 * prefix.
190 * BY_PFX_RNC: For PREFIXTREE <prefixID> and
191 * <prefcondnr> follow for non-combining
192 * rare prefix.
193 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000194 * and <xbyte> follow.
195 *
196 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
197 *
198 * <xbyte> 1 byte byte value of the sibling.
199 *
200 * <flags> 1 byte bitmask of:
201 * WF_ALLCAP word must have only capitals
202 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000203 * WF_KEEPCAP keep-case word
204 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000205 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000206 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000207 * WF_REGION <region> follows
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000208 * WF_PFX <prefixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000209 *
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000210 * <flags2> 1 byte Only used when there are postponed prefixes.
211 * Bitmask of:
212 * WF_HAS_AFF >> 8 word includes affix
213 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000214 * <region> 1 byte Bitmask for regions in which word is valid. When
215 * omitted it's valid in all regions.
216 * Lowest bit is for region 1.
217 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000218 * <prefixID> 1 byte ID of prefix that can be used with this word. For
219 * PREFIXTREE used for the required prefix ID.
220 *
221 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
222 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000223 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000224 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000225 */
226
Bram Moolenaare19defe2005-03-21 08:23:33 +0000227#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
228# include <io.h> /* for lseek(), must be before vim.h */
229#endif
230
231#include "vim.h"
232
233#if defined(FEAT_SYN_HL) || defined(PROTO)
234
235#ifdef HAVE_FCNTL_H
236# include <fcntl.h>
237#endif
238
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000239#define MAXWLEN 250 /* Assume max. word len is this many bytes.
240 Some places assume a word length fits in a
241 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000242
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000243/* Type used for indexes in the word tree need to be at least 3 bytes. If int
244 * is 8 bytes we could use something smaller, but what? */
245#if SIZEOF_INT > 2
246typedef int idx_T;
247#else
248typedef long idx_T;
249#endif
250
251/* Flags used for a word. Only the lowest byte can be used, the region byte
252 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000253#define WF_REGION 0x01 /* region byte follows */
254#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
255#define WF_ALLCAP 0x04 /* word must be all capitals */
256#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000257#define WF_BANNED 0x10 /* bad word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000258#define WF_PFX 0x20 /* prefix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000259#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000260#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000261
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000262/* for <flags2>, shifted up one byte to be used in wn_flags */
263#define WF_HAS_AFF 0x0100 /* word includes affix */
264
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000265#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000266
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000267/* flags for postponed prefixes. Must be above prefixID (one byte)
268 * and prefcondnr (two bytes). */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000269#define WF_RAREPFX 0x1000000 /* in sl_pidxs: flag for rare postponed
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000270 * prefix */
271#define WF_PFX_NC 0x2000000 /* in sl_pidxs: flag for non-combining
272 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000273
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000274/* Special byte values for <byte>. Some are only used in the tree for
275 * postponed prefixes, some only in the other trees. This is a bit messy... */
276#define BY_NOFLAGS 0 /* end of word without flags or region; for
277 * postponed prefix: not rare, combining */
278#define BY_FLAGS 1 /* end of word, flags byte follows; for
279 * postponed prefix: rare, combining */
280#define BY_INDEX 2 /* child is shared, index follows */
281#define BY_FLAGS2 3 /* end of word, two flags bytes follow; never
282 * used in prefix tree */
283#define BY_PFX_NC 3 /* postponed prefix: not rare, not combining */
284#define BY_PFX_RNC 4 /* postponed prefix: rare, not combining */
285#define BY_SPECIAL BY_PFX_RNC /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000286
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000287/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000288 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000289 * One replacement: from "ft_from" to "ft_to". */
290typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000291{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000292 char_u *ft_from;
293 char_u *ft_to;
294} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000295
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000296/* Info from "SAL" entries in ".aff" file used in sl_sal.
297 * The info is split for quick processing by spell_soundfold().
298 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
299typedef struct salitem_S
300{
301 char_u *sm_lead; /* leading letters */
302 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000303 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000304 char_u *sm_rules; /* rules like ^, $, priority */
305 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000306#ifdef FEAT_MBYTE
307 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000308 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000309 int *sm_to_w; /* wide character copy of "sm_to" */
310#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000311} salitem_T;
312
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000313#ifdef FEAT_MBYTE
314typedef int salfirst_T;
315#else
316typedef short salfirst_T;
317#endif
318
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000319/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000320 * Structure used to store words and other info for one language, loaded from
321 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000322 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
323 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
324 *
325 * The "byts" array stores the possible bytes in each tree node, preceded by
326 * the number of possible bytes, sorted on byte value:
327 * <len> <byte1> <byte2> ...
328 * The "idxs" array stores the index of the child node corresponding to the
329 * byte in "byts".
330 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000331 * the flags, region mask and prefixID for the word. There may be several
332 * zeros in sequence for alternative flag/region combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000333 */
334typedef struct slang_S slang_T;
335struct slang_S
336{
337 slang_T *sl_next; /* next language */
338 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000339 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000340 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000341
Bram Moolenaar51485f02005-06-04 21:55:20 +0000342 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000343 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000344 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000345 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000346 char_u *sl_pbyts; /* prefix tree word bytes */
347 idx_T *sl_pidxs; /* prefix tree word indexes */
348
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000349 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000350
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000351 char_u *sl_midword; /* MIDWORD string or NULL */
352
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000353 int sl_prefixcnt; /* number of items in "sl_prefprog" */
354 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
355
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000356 garray_T sl_rep; /* list of fromto_T entries from REP lines */
357 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
358 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000359 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000360 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000361 there is none */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000362 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
363 * "sl_sal_first" maps chars, when has_mbyte
364 * "sl_sal" is a list of wide char lists. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000365 int sl_followup; /* SAL followup */
366 int sl_collapse; /* SAL collapse_result */
367 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000368 int sl_has_map; /* TRUE if there is a MAP line */
369#ifdef FEAT_MBYTE
370 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
371 int sl_map_array[256]; /* MAP for first 256 chars */
372#else
373 char_u sl_map_array[256]; /* MAP for first 256 chars */
374#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000375};
376
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000377/* First language that is loaded, start of the linked list of loaded
378 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000379static slang_T *first_lang = NULL;
380
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000381/* Flags used in .spl file for soundsalike flags. */
382#define SAL_F0LLOWUP 1
383#define SAL_COLLAPSE 2
384#define SAL_REM_ACCENTS 4
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000385#define SAL_SOFO 8 /* SOFOFROM and SOFOTO instead of SAL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000386
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000387/*
388 * Structure used in "b_langp", filled from 'spelllang'.
389 */
390typedef struct langp_S
391{
392 slang_T *lp_slang; /* info for this language (NULL for last one) */
393 int lp_region; /* bitmask for region or REGION_ALL */
394} langp_T;
395
396#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
397
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000398#define REGION_ALL 0xff /* word valid in all regions */
399
400/* Result values. Lower number is accepted over higher one. */
401#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000402#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000403#define SP_RARE 1
404#define SP_LOCAL 2
405#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000406
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000407#define VIMSPELLMAGIC "VIMspell08" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000408#define VIMSPELLMAGICL 10
409
Bram Moolenaar7887d882005-07-01 22:33:52 +0000410/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000411static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000412
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000413/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000414 * Information used when looking for suggestions.
415 */
416typedef struct suginfo_S
417{
418 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000419 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000420 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000421 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000422 char_u *su_badptr; /* start of bad word in line */
423 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000424 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000425 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
426 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
427 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000428} suginfo_T;
429
430/* One word suggestion. Used in "si_ga". */
431typedef struct suggest_S
432{
433 char_u *st_word; /* suggested word, allocated string */
434 int st_orglen; /* length of replaced text */
435 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000436 int st_altscore; /* used when st_score compares equal */
437 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000438 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000439} suggest_T;
440
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000441#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000442
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000443/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
444 * called the score may change, thus we need to keep more than what is
445 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000446#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000447
448/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
449 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000450#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000451
452/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000453#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000454#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000455#define SCORE_REGION 70 /* word is for different region */
456#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000457#define SCORE_SWAP 90 /* swap two characters */
458#define SCORE_SWAP3 110 /* swap two characters in three */
459#define SCORE_REP 87 /* REP replacement */
460#define SCORE_SUBST 93 /* substitute a character */
461#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000462#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000463#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000464#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000465#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000466#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000467
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000468#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000469#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
470 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000471
472#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000473#define SCORE_MAXMAX 999999 /* accept any score */
474
475/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000476 * Structure to store info for word matching.
477 */
478typedef struct matchinf_S
479{
480 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000481
482 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000483 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000484 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000485 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000486 char_u *mi_cend; /* char after what was used for
487 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000488
489 /* case-folded text */
490 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000491 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000492
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000493 /* for when checking word after a prefix */
494 int mi_prefarridx; /* index in sl_pidxs with list of
495 prefixID/condition */
496 int mi_prefcnt; /* number of entries at mi_prefarridx */
497 int mi_prefixlen; /* byte length of prefix */
498
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000499 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000500 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000501 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000502 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000503} matchinf_T;
504
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000505/*
506 * The tables used for recognizing word characters according to spelling.
507 * These are only used for the first 256 characters of 'encoding'.
508 */
509typedef struct spelltab_S
510{
511 char_u st_isw[256]; /* flags: is word char */
512 char_u st_isu[256]; /* flags: is uppercase char */
513 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000514 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000515} spelltab_T;
516
517static spelltab_T spelltab;
518static int did_set_spelltab;
519
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000520#define CF_WORD 0x01
521#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000522
523static void clear_spell_chartab __ARGS((spelltab_T *sp));
524static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000525static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
526static int spell_iswordp_nmw __ARGS((char_u *p));
527#ifdef FEAT_MBYTE
528static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
529#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000530static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000531
532/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000533 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000534 */
535typedef enum
536{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000537 STATE_START = 0, /* At start of node check for NUL bytes (goodword
538 * ends); if badword ends there is a match, otherwise
539 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000540 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000541 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000542 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
543 STATE_PLAIN, /* Use each byte of the node. */
544 STATE_DEL, /* Delete a byte from the bad word. */
545 STATE_INS, /* Insert a byte in the bad word. */
546 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000547 STATE_UNSWAP, /* Undo swap two characters. */
548 STATE_SWAP3, /* Swap two characters over three. */
549 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000550 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000551 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000552 STATE_REP_INI, /* Prepare for using REP items. */
553 STATE_REP, /* Use matching REP items from the .aff file. */
554 STATE_REP_UNDO, /* Undo a REP item replacement. */
555 STATE_FINAL /* End of this node. */
556} state_T;
557
558/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000559 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000560 */
561typedef struct trystate_S
562{
Bram Moolenaarea424162005-06-16 21:51:00 +0000563 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000564 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000565 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000566 short ts_curi; /* index in list of child nodes */
567 char_u ts_fidx; /* index in fword[], case-folded bad word */
568 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
569 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000570 char_u ts_prefixdepth; /* stack depth for end of prefix or PREFIXTREE
571 * or NOPREFIX */
Bram Moolenaarea424162005-06-16 21:51:00 +0000572#ifdef FEAT_MBYTE
573 char_u ts_tcharlen; /* number of bytes in tword character */
574 char_u ts_tcharidx; /* current byte index in tword character */
575 char_u ts_isdiff; /* DIFF_ values */
576 char_u ts_fcharstart; /* index in fword where badword char started */
577#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000578 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000579 char_u ts_save_splitoff; /* su_splitoff saved here */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000580 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000581} trystate_T;
582
Bram Moolenaarea424162005-06-16 21:51:00 +0000583/* values for ts_isdiff */
584#define DIFF_NONE 0 /* no different byte (yet) */
585#define DIFF_YES 1 /* different byte found */
586#define DIFF_INSERT 2 /* inserting character */
587
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000588/* special values ts_prefixdepth */
589#define PREFIXTREE 0xfe /* walking through the prefix tree */
590#define NOPREFIX 0xff /* not using prefixes */
591
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000592/* mode values for find_word */
593#define FIND_FOLDWORD 0 /* find word case-folded */
594#define FIND_KEEPWORD 1 /* find keep-case word */
595#define FIND_PREFIX 2 /* find word after prefix */
596
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000597static slang_T *slang_alloc __ARGS((char_u *lang));
598static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000599static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000600static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000601static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000602static void find_prefix __ARGS((matchinf_T *mip));
603static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000604static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000605static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000606static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000607static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000608static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000609static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000610static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000611static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000612static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
613static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000614#ifdef FEAT_MBYTE
615static int *mb_str2wide __ARGS((char_u *s));
616#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000617static idx_T read_tree __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx, int prefixtree, int maxprefcondnr));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000618static void clear_midword __ARGS((buf_T *buf));
619static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000620static int find_region __ARGS((char_u *rp, char_u *region));
621static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000622static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000623static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000624static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
625static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000626static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000627static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount, int banbadword, int need_cap));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000628#ifdef FEAT_EVAL
629static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
630#endif
631static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
632static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000633static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000634static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000635static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000636static void suggest_try_special __ARGS((suginfo_T *su));
637static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000638static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
639static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000640static void score_comp_sal __ARGS((suginfo_T *su));
641static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000642static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000643static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000644static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000645static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000646static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000647static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000648static void add_banned __ARGS((suginfo_T *su, char_u *word));
649static int was_banned __ARGS((suginfo_T *su, char_u *word));
650static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000651static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000652static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000653static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
654static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
655static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000656#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000657static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000658#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000659static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000660static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000661static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
662static linenr_T apply_prefixes __ARGS((slang_T *slang, char_u *word, int round, int flags, linenr_T startlnum));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000663
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000664/*
665 * Use our own character-case definitions, because the current locale may
666 * differ from what the .spl file uses.
667 * These must not be called with negative number!
668 */
669#ifndef FEAT_MBYTE
670/* Non-multi-byte implementation. */
671# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
672# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
673# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
674#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000675# if defined(HAVE_WCHAR_H)
676# include <wchar.h> /* for towupper() and towlower() */
677# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000678/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
679 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
680 * the "w" library function for characters above 255 if available. */
681# ifdef HAVE_TOWLOWER
682# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
683 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
684# else
685# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
686 : (c) < 256 ? spelltab.st_fold[c] : (c))
687# endif
688
689# ifdef HAVE_TOWUPPER
690# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
691 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
692# else
693# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
694 : (c) < 256 ? spelltab.st_upper[c] : (c))
695# endif
696
697# ifdef HAVE_ISWUPPER
698# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
699 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
700# else
701# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
702 : (c) < 256 ? spelltab.st_isu[c] : (c))
703# endif
704#endif
705
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000706
707static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000708static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000709
710/*
711 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000712 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000713 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
714 * or when it's OK it remains unchanged.
715 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000716 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000717 * "capcol" is used to check for a Capitalised word after the end of a
718 * sentence. If it's zero then perform the check. Return the column where to
719 * check next, or -1 when no sentence end was found. If it's NULL then don't
720 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000721 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000722 * Returns the length of the word in bytes, also when it's OK, so that the
723 * caller can skip over the word.
724 */
725 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000726spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000727 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000728 char_u *ptr;
729 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000730 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000731{
732 matchinf_T mi; /* Most things are put in "mi" so that it can
733 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000734 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000735 int c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000736
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000737 /* A word never starts at a space or a control character. Return quickly
738 * then, skipping over the character. */
739 if (*ptr <= ' ')
740 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000741
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000742 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000743 * 0X99FF. But when a word character follows do check spelling to find
744 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000745 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000746 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000747 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
748 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000749 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000750 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000751 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000752 nrlen = mi.mi_end - ptr;
753 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000754 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000755 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000756
757 /* Try including the digits in the word. */
758 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000759 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000760 else
761 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000762
Bram Moolenaar0c405862005-06-22 22:26:26 +0000763 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000764 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000765 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000766 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000767 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000768 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000769 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000770 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000771
772 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
773 {
774 /* Check word starting with capital letter. */
775#ifdef FEAT_MBYTE
776 c = mb_ptr2char(ptr);
777#else
778 c = *ptr;
779#endif
780 if (!SPELL_ISUPPER(c))
781 {
782 *attrp = highlight_attr[HLF_SPC];
783 return (int)(mi.mi_fend - ptr);
784 }
785 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000786 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000787 if (capcol != NULL)
788 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000789
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000790 /* We always use the characters up to the next non-word character,
791 * also for bad words. */
792 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000793
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000794 /* Check caps type later. */
795 mi.mi_capflags = 0;
796 mi.mi_cend = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000797 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000798
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000799 /* Include one non-word character so that we can check for the
800 * word end. */
801 if (*mi.mi_fend != NUL)
802 mb_ptr_adv(mi.mi_fend);
803
804 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
805 MAXWLEN + 1);
806 mi.mi_fwordlen = STRLEN(mi.mi_fword);
807
808 /* The word is bad unless we recognize it. */
809 mi.mi_result = SP_BAD;
810
811 /*
812 * Loop over the languages specified in 'spelllang'.
813 * We check them all, because a matching word may be longer than an
814 * already found matching word.
815 */
816 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
817 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
818 {
819 /* Check for a matching word in case-folded words. */
820 find_word(&mi, FIND_FOLDWORD);
821
822 /* Check for a matching word in keep-case words. */
823 find_word(&mi, FIND_KEEPWORD);
824
825 /* Check for matching prefixes. */
826 find_prefix(&mi);
827 }
828
829 if (mi.mi_result != SP_OK)
830 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000831 /* If we found a number skip over it. Allows for "42nd". Do flag
832 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000833 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000834 {
835 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
836 return nrlen;
837 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000838
839 /* When we are at a non-word character there is no error, just
840 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000841 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000842 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000843 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
844 {
845 regmatch_T regmatch;
846
847 /* Check for end of sentence. */
848 regmatch.regprog = wp->w_buffer->b_cap_prog;
849 regmatch.rm_ic = FALSE;
850 if (vim_regexec(&regmatch, ptr, 0))
851 *capcol = (int)(regmatch.endp[0] - ptr);
852 }
853
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000854#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000855 if (has_mbyte)
856 return mb_ptr2len_check(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000857#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000858 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000859 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000860
861 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
862 *attrp = highlight_attr[HLF_SPB];
863 else if (mi.mi_result == SP_RARE)
864 *attrp = highlight_attr[HLF_SPR];
865 else
866 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000867 }
868
Bram Moolenaar51485f02005-06-04 21:55:20 +0000869 return (int)(mi.mi_end - ptr);
870}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000871
Bram Moolenaar51485f02005-06-04 21:55:20 +0000872/*
873 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000874 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
875 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
876 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
877 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000878 *
879 * For a match mip->mi_result is updated.
880 */
881 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000882find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000883 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000884 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000885{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000886 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000887 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000888 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000889 int endidxcnt = 0;
890 int len;
891 int wlen = 0;
892 int flen;
893 int c;
894 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000895 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000896#ifdef FEAT_MBYTE
897 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000898 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000899#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000900 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000901 slang_T *slang = mip->mi_lp->lp_slang;
902 unsigned flags;
903 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000904 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000905
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000906 if (mode == FIND_KEEPWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000907 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000908 /* Check for word with matching case in keep-case tree. */
909 ptr = mip->mi_word;
910 flen = 9999; /* no case folding, always enough bytes */
911 byts = slang->sl_kbyts;
912 idxs = slang->sl_kidxs;
913 }
914 else
915 {
916 /* Check for case-folded in case-folded tree. */
917 ptr = mip->mi_fword;
918 flen = mip->mi_fwordlen; /* available case-folded bytes */
919 byts = slang->sl_fbyts;
920 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000921
922 if (mode == FIND_PREFIX)
923 {
924 /* Skip over the prefix. */
925 wlen = mip->mi_prefixlen;
926 flen -= mip->mi_prefixlen;
927 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000928 }
929
Bram Moolenaar51485f02005-06-04 21:55:20 +0000930 if (byts == NULL)
931 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000932
Bram Moolenaar51485f02005-06-04 21:55:20 +0000933 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000934 * Repeat advancing in the tree until:
935 * - there is a byte that doesn't match,
936 * - we reach the end of the tree,
937 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000938 */
939 for (;;)
940 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000941 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000942 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000943
944 len = byts[arridx++];
945
946 /* If the first possible byte is a zero the word could end here.
947 * Remember this index, we first check for the longest word. */
948 if (byts[arridx] == 0)
949 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000950 if (endidxcnt == MAXWLEN)
951 {
952 /* Must be a corrupted spell file. */
953 EMSG(_(e_format));
954 return;
955 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000956 endlen[endidxcnt] = wlen;
957 endidx[endidxcnt++] = arridx++;
958 --len;
959
960 /* Skip over the zeros, there can be several flag/region
961 * combinations. */
962 while (len > 0 && byts[arridx] == 0)
963 {
964 ++arridx;
965 --len;
966 }
967 if (len == 0)
968 break; /* no children, word must end here */
969 }
970
971 /* Stop looking at end of the line. */
972 if (ptr[wlen] == NUL)
973 break;
974
975 /* Perform a binary search in the list of accepted bytes. */
976 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000977 if (c == TAB) /* <Tab> is handled like <Space> */
978 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000979 lo = arridx;
980 hi = arridx + len - 1;
981 while (lo < hi)
982 {
983 m = (lo + hi) / 2;
984 if (byts[m] > c)
985 hi = m - 1;
986 else if (byts[m] < c)
987 lo = m + 1;
988 else
989 {
990 lo = hi = m;
991 break;
992 }
993 }
994
995 /* Stop if there is no matching byte. */
996 if (hi < lo || byts[lo] != c)
997 break;
998
999 /* Continue at the child (if there is one). */
1000 arridx = idxs[lo];
1001 ++wlen;
1002 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001003
1004 /* One space in the good word may stand for several spaces in the
1005 * checked word. */
1006 if (c == ' ')
1007 {
1008 for (;;)
1009 {
1010 if (flen <= 0 && *mip->mi_fend != NUL)
1011 flen = fold_more(mip);
1012 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1013 break;
1014 ++wlen;
1015 --flen;
1016 }
1017 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001018 }
1019
1020 /*
1021 * Verify that one of the possible endings is valid. Try the longest
1022 * first.
1023 */
1024 while (endidxcnt > 0)
1025 {
1026 --endidxcnt;
1027 arridx = endidx[endidxcnt];
1028 wlen = endlen[endidxcnt];
1029
1030#ifdef FEAT_MBYTE
1031 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1032 continue; /* not at first byte of character */
1033#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001034 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001035 continue; /* next char is a word character */
1036
1037#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001038 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001039 {
1040 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001041 * when folding case. This can be slow, take a shortcut when the
1042 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001043 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001044 if (STRNCMP(ptr, p, wlen) != 0)
1045 {
1046 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1047 mb_ptr_adv(p);
1048 wlen = p - mip->mi_word;
1049 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001050 }
1051#endif
1052
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001053 /* Check flags and region. For FIND_PREFIX check the condition and
1054 * prefix ID.
1055 * Repeat this if there are more flags/region alternatives until there
1056 * is a match. */
1057 res = SP_BAD;
1058 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1059 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001060 {
1061 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001062
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001063 /* For the fold-case tree check that the case of the checked word
1064 * matches with what the word in the tree requires.
1065 * For keep-case tree the case is always right. For prefixes we
1066 * don't bother to check. */
1067 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001068 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001069 if (mip->mi_cend != mip->mi_word + wlen)
1070 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001071 /* mi_capflags was set for a different word length, need
1072 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001073 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001074 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001075 }
1076
Bram Moolenaar0c405862005-06-22 22:26:26 +00001077 if (mip->mi_capflags == WF_KEEPCAP
1078 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001079 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001080 }
1081
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001082 /* When mode is FIND_PREFIX the word must support the prefix:
1083 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001084 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001085 if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001086 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001087 /* The prefix ID is stored two bytes above the flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001088 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001089 flags,
1090 mip->mi_fword + mip->mi_prefixlen, slang);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001091 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001092 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001093
1094 /* Use the WF_RARE flag for a rare prefix. */
1095 if (c & WF_RAREPFX)
1096 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001097 }
1098
1099 if (flags & WF_BANNED)
1100 res = SP_BANNED;
1101 else if (flags & WF_REGION)
1102 {
1103 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001104 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001105 res = SP_OK;
1106 else
1107 res = SP_LOCAL;
1108 }
1109 else if (flags & WF_RARE)
1110 res = SP_RARE;
1111 else
1112 res = SP_OK;
1113
1114 /* Always use the longest match and the best result. */
1115 if (mip->mi_result > res)
1116 {
1117 mip->mi_result = res;
1118 mip->mi_end = mip->mi_word + wlen;
1119 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001120 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001121 mip->mi_end = mip->mi_word + wlen;
1122
1123 if (res == SP_OK)
1124 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001125 }
1126
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001127 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001128 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001129 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001130}
1131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001132/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001133 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1134 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001135 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001136 */
1137 static int
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001138valid_word_prefix(totprefcnt, arridx, flags, word, slang)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001139 int totprefcnt; /* nr of prefix IDs */
1140 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001141 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001142 char_u *word;
1143 slang_T *slang;
1144{
1145 int prefcnt;
1146 int pidx;
1147 regprog_T *rp;
1148 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001149 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001150
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001151 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001152 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1153 {
1154 pidx = slang->sl_pidxs[arridx + prefcnt];
1155
1156 /* Check the prefix ID. */
1157 if (prefid != (pidx & 0xff))
1158 continue;
1159
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001160 /* Check if the prefix doesn't combine and the word already has a
1161 * suffix. */
1162 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1163 continue;
1164
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001165 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001166 * stored in the two bytes above the prefix ID byte. */
1167 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001168 if (rp != NULL)
1169 {
1170 regmatch.regprog = rp;
1171 regmatch.rm_ic = FALSE;
1172 if (!vim_regexec(&regmatch, word, 0))
1173 continue;
1174 }
1175
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001176 /* It's a match! Return the WF_RAREPFX flag. */
1177 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001178 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001179 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001180}
1181
1182/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001183 * Check if the word at "mip->mi_word" has a matching prefix.
1184 * If it does, then check the following word.
1185 *
1186 * For a match mip->mi_result is updated.
1187 */
1188 static void
1189find_prefix(mip)
1190 matchinf_T *mip;
1191{
1192 idx_T arridx = 0;
1193 int len;
1194 int wlen = 0;
1195 int flen;
1196 int c;
1197 char_u *ptr;
1198 idx_T lo, hi, m;
1199 slang_T *slang = mip->mi_lp->lp_slang;
1200 char_u *byts;
1201 idx_T *idxs;
1202
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001203 byts = slang->sl_pbyts;
1204 if (byts == NULL)
1205 return; /* array is empty */
1206
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001207 /* We use the case-folded word here, since prefixes are always
1208 * case-folded. */
1209 ptr = mip->mi_fword;
1210 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001211 idxs = slang->sl_pidxs;
1212
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001213 /*
1214 * Repeat advancing in the tree until:
1215 * - there is a byte that doesn't match,
1216 * - we reach the end of the tree,
1217 * - or we reach the end of the line.
1218 */
1219 for (;;)
1220 {
1221 if (flen == 0 && *mip->mi_fend != NUL)
1222 flen = fold_more(mip);
1223
1224 len = byts[arridx++];
1225
1226 /* If the first possible byte is a zero the prefix could end here.
1227 * Check if the following word matches and supports the prefix. */
1228 if (byts[arridx] == 0)
1229 {
1230 /* There can be several prefixes with different conditions. We
1231 * try them all, since we don't know which one will give the
1232 * longest match. The word is the same each time, pass the list
1233 * of possible prefixes to find_word(). */
1234 mip->mi_prefarridx = arridx;
1235 mip->mi_prefcnt = len;
1236 while (len > 0 && byts[arridx] == 0)
1237 {
1238 ++arridx;
1239 --len;
1240 }
1241 mip->mi_prefcnt -= len;
1242
1243 /* Find the word that comes after the prefix. */
1244 mip->mi_prefixlen = wlen;
1245 find_word(mip, FIND_PREFIX);
1246
1247
1248 if (len == 0)
1249 break; /* no children, word must end here */
1250 }
1251
1252 /* Stop looking at end of the line. */
1253 if (ptr[wlen] == NUL)
1254 break;
1255
1256 /* Perform a binary search in the list of accepted bytes. */
1257 c = ptr[wlen];
1258 lo = arridx;
1259 hi = arridx + len - 1;
1260 while (lo < hi)
1261 {
1262 m = (lo + hi) / 2;
1263 if (byts[m] > c)
1264 hi = m - 1;
1265 else if (byts[m] < c)
1266 lo = m + 1;
1267 else
1268 {
1269 lo = hi = m;
1270 break;
1271 }
1272 }
1273
1274 /* Stop if there is no matching byte. */
1275 if (hi < lo || byts[lo] != c)
1276 break;
1277
1278 /* Continue at the child (if there is one). */
1279 arridx = idxs[lo];
1280 ++wlen;
1281 --flen;
1282 }
1283}
1284
1285/*
1286 * Need to fold at least one more character. Do until next non-word character
1287 * for efficiency.
1288 * Return the length of the folded chars in bytes.
1289 */
1290 static int
1291fold_more(mip)
1292 matchinf_T *mip;
1293{
1294 int flen;
1295 char_u *p;
1296
1297 p = mip->mi_fend;
1298 do
1299 {
1300 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001301 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001302
1303 /* Include the non-word character so that we can check for the
1304 * word end. */
1305 if (*mip->mi_fend != NUL)
1306 mb_ptr_adv(mip->mi_fend);
1307
1308 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1309 mip->mi_fword + mip->mi_fwordlen,
1310 MAXWLEN - mip->mi_fwordlen);
1311 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1312 mip->mi_fwordlen += flen;
1313 return flen;
1314}
1315
1316/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001317 * Check case flags for a word. Return TRUE if the word has the requested
1318 * case.
1319 */
1320 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001321spell_valid_case(wordflags, treeflags)
1322 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001323 int treeflags; /* flags for the word in the spell tree */
1324{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001325 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001326 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001327 && ((treeflags & WF_ONECAP) == 0 || wordflags == WF_ONECAP)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001328}
1329
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001330/*
1331 * Return TRUE if spell checking is not enabled.
1332 */
1333 static int
1334no_spell_checking()
1335{
1336 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1337 {
1338 EMSG(_("E756: Spell checking is not enabled"));
1339 return TRUE;
1340 }
1341 return FALSE;
1342}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001343
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001344/*
1345 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001346 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001347 * Return OK if found, FAIL otherwise.
1348 */
1349 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001350spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001351 int dir; /* FORWARD or BACKWARD */
1352 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001353 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001354{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001355 linenr_T lnum;
1356 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001357 char_u *line;
1358 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001359 char_u *endp;
1360 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001361 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001362 int has_syntax = syntax_present(curbuf);
1363 int col;
1364 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001365 char_u *buf = NULL;
1366 int buflen = 0;
1367 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001368 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001369
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001370 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001371 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001372
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001373 /*
1374 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001375 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001376 *
1377 * When searching backwards, we continue in the line to find the last
1378 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001379 *
1380 * We concatenate the start of the next line, so that wrapped words work
1381 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1382 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001383 */
1384 lnum = curwin->w_cursor.lnum;
1385 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001386
1387 while (!got_int)
1388 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001389 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001390
Bram Moolenaar0c405862005-06-22 22:26:26 +00001391 len = STRLEN(line);
1392 if (buflen < len + MAXWLEN + 2)
1393 {
1394 vim_free(buf);
1395 buflen = len + MAXWLEN + 2;
1396 buf = alloc(buflen);
1397 if (buf == NULL)
1398 break;
1399 }
1400
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001401 /* In first line check first word for Capital. */
1402 if (lnum == 1)
1403 capcol = 0;
1404
1405 /* For checking first word with a capital skip white space. */
1406 if (capcol == 0)
1407 capcol = skipwhite(line) - line;
1408
Bram Moolenaar0c405862005-06-22 22:26:26 +00001409 /* Copy the line into "buf" and append the start of the next line if
1410 * possible. */
1411 STRCPY(buf, line);
1412 if (lnum < curbuf->b_ml.ml_line_count)
1413 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1414
1415 p = buf + skip;
1416 endp = buf + len;
1417 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001418 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001419 /* When searching backward don't search after the cursor. */
1420 if (dir == BACKWARD
1421 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001422 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001423 break;
1424
1425 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001426 attr = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001427 len = spell_check(curwin, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001428
1429 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001430 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001431 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001432 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001433 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001434 /* When searching forward only accept a bad word after
1435 * the cursor. */
1436 if (dir == BACKWARD
1437 || lnum > curwin->w_cursor.lnum
1438 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001439 && (colnr_T)(curline ? p - buf + len
1440 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001441 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001442 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001443 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001444 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001445 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001446 (void)syn_get_id(lnum, (colnr_T)col,
1447 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001448 }
1449 else
1450 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001451
Bram Moolenaar51485f02005-06-04 21:55:20 +00001452 if (can_spell)
1453 {
1454 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001455 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001456#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001457 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001458#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001459 if (dir == FORWARD)
1460 {
1461 /* No need to search further. */
1462 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001463 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001464 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001465 }
1466 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001467 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001468 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001469 }
1470
Bram Moolenaar51485f02005-06-04 21:55:20 +00001471 /* advance to character after the word */
1472 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001473 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001474 }
1475
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001476 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001477 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001478
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001479 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001480 if (dir == BACKWARD)
1481 {
1482 if (found_pos.lnum != 0)
1483 {
1484 /* Use the last match in the line. */
1485 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001486 vim_free(buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001487 return OK;
1488 }
1489 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001490 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001491 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001492 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001493 }
1494 else
1495 {
1496 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001497 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001498 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001499
1500 /* Skip the characters at the start of the next line that were
1501 * included in a match crossing line boundaries. */
1502 if (attr == 0)
1503 skip = p - endp;
1504 else
1505 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001506
1507 /* Capscol skips over the inserted space. */
1508 --capcol;
1509
1510 /* But after empty line check first word in next line */
1511 if (*skipwhite(line) == NUL)
1512 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001513 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001514
1515 line_breakcheck();
1516 }
1517
Bram Moolenaar0c405862005-06-22 22:26:26 +00001518 vim_free(buf);
1519 return FAIL;
1520}
1521
1522/*
1523 * For spell checking: concatenate the start of the following line "line" into
1524 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1525 */
1526 void
1527spell_cat_line(buf, line, maxlen)
1528 char_u *buf;
1529 char_u *line;
1530 int maxlen;
1531{
1532 char_u *p;
1533 int n;
1534
1535 p = skipwhite(line);
1536 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1537 p = skipwhite(p + 1);
1538
1539 if (*p != NUL)
1540 {
1541 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001542 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001543 n = p - line;
1544 if (n >= maxlen)
1545 n = maxlen - 1;
1546 vim_memset(buf + 1, ' ', n);
1547 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001548}
1549
1550/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001551 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001552 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001553 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001554 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001555spell_load_lang(lang)
1556 char_u *lang;
1557{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001558 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001559 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001560 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001561
Bram Moolenaarb765d632005-06-07 21:00:02 +00001562 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001563 * It's truncated when an error is detected. */
1564 STRCPY(langcp, lang);
1565
Bram Moolenaarb765d632005-06-07 21:00:02 +00001566 /*
1567 * Find the first spell file for "lang" in 'runtimepath' and load it.
1568 */
1569 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1570 "spell/%s.%s.spl", lang, spell_enc());
1571 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001572
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001573 if (r == FAIL && *langcp != NUL)
1574 {
1575 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001576 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001577 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001578 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001579 }
1580
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001581 if (r == FAIL)
1582 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1583 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001584 else if (*langcp != NUL)
1585 {
1586 /* Load all the additions. */
1587 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1588 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1589 }
1590}
1591
1592/*
1593 * Return the encoding used for spell checking: Use 'encoding', except that we
1594 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1595 */
1596 static char_u *
1597spell_enc()
1598{
1599
1600#ifdef FEAT_MBYTE
1601 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1602 return p_enc;
1603#endif
1604 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001605}
1606
1607/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001608 * Get the name of the .spl file for the internal wordlist into
1609 * "fname[MAXPATHL]".
1610 */
1611 static void
1612int_wordlist_spl(fname)
1613 char_u *fname;
1614{
1615 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
1616 int_wordlist, spell_enc());
1617}
1618
1619/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001620 * Allocate a new slang_T.
1621 * Caller must fill "sl_next".
1622 */
1623 static slang_T *
1624slang_alloc(lang)
1625 char_u *lang;
1626{
1627 slang_T *lp;
1628
Bram Moolenaar51485f02005-06-04 21:55:20 +00001629 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001630 if (lp != NULL)
1631 {
1632 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001633 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001634 }
1635 return lp;
1636}
1637
1638/*
1639 * Free the contents of an slang_T and the structure itself.
1640 */
1641 static void
1642slang_free(lp)
1643 slang_T *lp;
1644{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001645 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001646 vim_free(lp->sl_fname);
1647 slang_clear(lp);
1648 vim_free(lp);
1649}
1650
1651/*
1652 * Clear an slang_T so that the file can be reloaded.
1653 */
1654 static void
1655slang_clear(lp)
1656 slang_T *lp;
1657{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001658 garray_T *gap;
1659 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001660 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001661 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001662
Bram Moolenaar51485f02005-06-04 21:55:20 +00001663 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001664 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001665 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001666 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001667 vim_free(lp->sl_pbyts);
1668 lp->sl_pbyts = NULL;
1669
Bram Moolenaar51485f02005-06-04 21:55:20 +00001670 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001671 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001672 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001673 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001674 vim_free(lp->sl_pidxs);
1675 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001676
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001677 gap = &lp->sl_rep;
1678 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001679 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001680 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1681 vim_free(ftp->ft_from);
1682 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001683 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001684 ga_clear(gap);
1685
1686 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001687 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001688 {
1689 /* "ga_len" is set to 1 without adding an item for latin1 */
1690 if (gap->ga_data != NULL)
1691 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1692 for (i = 0; i < gap->ga_len; ++i)
1693 vim_free(((int **)gap->ga_data)[i]);
1694 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001695 else
1696 /* SAL items: free salitem_T items */
1697 while (gap->ga_len > 0)
1698 {
1699 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1700 vim_free(smp->sm_lead);
1701 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1702 vim_free(smp->sm_to);
1703#ifdef FEAT_MBYTE
1704 vim_free(smp->sm_lead_w);
1705 vim_free(smp->sm_oneof_w);
1706 vim_free(smp->sm_to_w);
1707#endif
1708 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001709 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001710
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001711 for (i = 0; i < lp->sl_prefixcnt; ++i)
1712 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001713 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001714 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001715 lp->sl_prefprog = NULL;
1716
1717 vim_free(lp->sl_midword);
1718 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001719
Bram Moolenaarea424162005-06-16 21:51:00 +00001720#ifdef FEAT_MBYTE
1721 {
1722 int todo = lp->sl_map_hash.ht_used;
1723 hashitem_T *hi;
1724
1725 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1726 if (!HASHITEM_EMPTY(hi))
1727 {
1728 --todo;
1729 vim_free(hi->hi_key);
1730 }
1731 }
1732 hash_clear(&lp->sl_map_hash);
1733#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001734}
1735
1736/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001737 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001738 * Invoked through do_in_runtimepath().
1739 */
1740 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001741spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001742 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001743 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001744{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001745 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001746}
1747
1748/*
1749 * Load one spell file and store the info into a slang_T.
1750 *
1751 * This is invoked in two ways:
1752 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1753 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1754 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1755 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001756 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001757 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001758 static slang_T *
1759spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001760 char_u *fname;
1761 char_u *lang;
1762 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001763 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001764{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001765 FILE *fd;
1766 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001767 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001768 char_u *bp;
1769 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001770 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001771 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001772 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001773 int round;
1774 char_u *save_sourcing_name = sourcing_name;
1775 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001776 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001777 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001778 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001779 garray_T *gap;
1780 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001781 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001782 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001783 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001784 int c = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001785
Bram Moolenaarb765d632005-06-07 21:00:02 +00001786 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001787 if (fd == NULL)
1788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001789 if (!silent)
1790 EMSG2(_(e_notopen), fname);
1791 else if (p_verbose > 2)
1792 {
1793 verbose_enter();
1794 smsg((char_u *)e_notopen, fname);
1795 verbose_leave();
1796 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001797 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001798 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001799 if (p_verbose > 2)
1800 {
1801 verbose_enter();
1802 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1803 verbose_leave();
1804 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001805
Bram Moolenaarb765d632005-06-07 21:00:02 +00001806 if (old_lp == NULL)
1807 {
1808 lp = slang_alloc(lang);
1809 if (lp == NULL)
1810 goto endFAIL;
1811
1812 /* Remember the file name, used to reload the file when it's updated. */
1813 lp->sl_fname = vim_strsave(fname);
1814 if (lp->sl_fname == NULL)
1815 goto endFAIL;
1816
1817 /* Check for .add.spl. */
1818 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1819 }
1820 else
1821 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001822
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001823 /* Set sourcing_name, so that error messages mention the file name. */
1824 sourcing_name = fname;
1825 sourcing_lnum = 0;
1826
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001827 /* <HEADER>: <fileID>
1828 * <regioncnt> <regionname> ...
1829 * <charflagslen> <charflags>
1830 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001831 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001832 * <prefcondcnt> <prefcond> ...
1833 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001834 for (i = 0; i < VIMSPELLMAGICL; ++i)
1835 buf[i] = getc(fd); /* <fileID> */
1836 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1837 {
1838 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001839 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001840 }
1841
1842 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001843 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001844 {
1845truncerr:
Bram Moolenaar7887d882005-07-01 22:33:52 +00001846 EMSG(_(e_spell_trunc));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001847 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001848 }
1849 if (cnt > 8)
1850 {
1851formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001852 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001853 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001854 }
1855 for (i = 0; i < cnt; ++i)
1856 {
1857 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1858 lp->sl_regions[i * 2 + 1] = getc(fd);
1859 }
1860 lp->sl_regions[cnt * 2] = NUL;
1861
Bram Moolenaar7887d882005-07-01 22:33:52 +00001862 /* <charflagslen> <charflags> */
1863 p = read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001864 if (cnt < 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001865 goto endFAIL;
1866
1867 /* <fcharslen> <fchars> */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001868 fol = read_cnt_string(fd, 2, &ccnt);
1869 if (ccnt < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001870 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001871 vim_free(p);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001872 goto endFAIL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001873 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00001874
1875 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
1876 if (p != NULL && fol != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001877 i = set_spell_charflags(p, cnt, fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001878
1879 vim_free(p);
1880 vim_free(fol);
1881
1882 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1883 if ((p == NULL) != (fol == NULL))
1884 goto formerr;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001885
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001886 /* <midwordlen> <midword> */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001887 lp->sl_midword = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001888 if (cnt < 0)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001889 goto endFAIL;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001890
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001891 /* <prefcondcnt> <prefcond> ... */
1892 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
1893 if (cnt > 0)
1894 {
1895 lp->sl_prefprog = (regprog_T **)alloc_clear(
1896 (unsigned)sizeof(regprog_T *) * cnt);
1897 if (lp->sl_prefprog == NULL)
1898 goto endFAIL;
1899 lp->sl_prefixcnt = cnt;
1900
1901 for (i = 0; i < cnt; ++i)
1902 {
1903 /* <prefcond> : <condlen> <condstr> */
1904 n = getc(fd); /* <condlen> */
1905 if (n < 0)
1906 goto formerr;
1907 /* When <condlen> is zero we have an empty condition. Otherwise
1908 * compile the regexp program used to check for the condition. */
1909 if (n > 0)
1910 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001911 buf[0] = '^'; /* always match at one position only */
1912 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001913 while (n-- > 0)
1914 *p++ = getc(fd); /* <condstr> */
1915 *p = NUL;
1916 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
1917 }
1918 }
1919 }
1920
1921
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001922 /* <SUGGEST> : <repcount> <rep> ...
1923 * <salflags> <salcount> <sal> ...
1924 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001925
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001926 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
1927 if (cnt < 0)
1928 goto formerr;
1929
1930 gap = &lp->sl_rep;
1931 if (ga_grow(gap, cnt) == FAIL)
1932 goto endFAIL;
1933
1934 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
1935 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001936 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001937 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar7887d882005-07-01 22:33:52 +00001938 ftp->ft_from = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001939 if (i <= 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001940 goto endFAIL;
1941 ftp->ft_to = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001942 if (i <= 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001943 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00001944 vim_free(ftp->ft_from);
1945 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001946 }
1947 }
1948
1949 /* Fill the first-index table. */
1950 first = lp->sl_rep_first;
1951 for (i = 0; i < 256; ++i)
1952 first[i] = -1;
1953 for (i = 0; i < gap->ga_len; ++i)
1954 {
1955 ftp = &((fromto_T *)gap->ga_data)[i];
1956 if (first[*ftp->ft_from] == -1)
1957 first[*ftp->ft_from] = i;
1958 }
1959
1960 i = getc(fd); /* <salflags> */
1961 if (i & SAL_F0LLOWUP)
1962 lp->sl_followup = TRUE;
1963 if (i & SAL_COLLAPSE)
1964 lp->sl_collapse = TRUE;
1965 if (i & SAL_REM_ACCENTS)
1966 lp->sl_rem_accents = TRUE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001967 if (i & SAL_SOFO)
1968 lp->sl_sofo = TRUE;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001969 else
1970 lp->sl_sofo = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001971
1972 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
1973 if (cnt < 0)
1974 goto formerr;
1975
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001976 if (lp->sl_sofo)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001977 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001978 /*
1979 * SOFOFROM and SOFOTO items come in one <salfrom> and <salto>
1980 */
1981 if (cnt != 1)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001982 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001983
Bram Moolenaar7887d882005-07-01 22:33:52 +00001984 /* <salfromlen> <salfrom> */
1985 bp = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001986 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001987 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001988
Bram Moolenaar7887d882005-07-01 22:33:52 +00001989 /* <saltolen> <salto> */
1990 fol = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001991 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001992 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001993 vim_free(bp);
1994 goto endFAIL;
1995 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001996
Bram Moolenaar7887d882005-07-01 22:33:52 +00001997 /* Store the info in lp->sl_sal and/or lp->sl_sal_first. */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001998 if (bp != NULL && fol != NULL)
1999 i = set_sofo(lp, bp, fol);
2000 else if (bp != NULL || fol != NULL)
2001 i = FAIL; /* only one of two strings is an error */
2002 else
2003 i = OK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002004
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002005 vim_free(bp);
2006 vim_free(fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002007 if (i == FAIL)
2008 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002009 }
2010 else
2011 {
2012 /*
2013 * SAL items
2014 */
2015 gap = &lp->sl_sal;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002016 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002017 if (ga_grow(gap, cnt) == FAIL)
2018 goto endFAIL;
2019
2020 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2021 for (; gap->ga_len < cnt; ++gap->ga_len)
2022 {
2023 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2024 ccnt = getc(fd); /* <salfromlen> */
2025 if (ccnt < 0)
2026 goto formerr;
2027 if ((p = alloc(ccnt + 2)) == NULL)
2028 goto endFAIL;
2029 smp->sm_lead = p;
2030
2031 /* Read up to the first special char into sm_lead. */
2032 for (i = 0; i < ccnt; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002033 {
2034 c = getc(fd); /* <salfrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002035 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002036 break;
2037 *p++ = c;
2038 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002039 smp->sm_leadlen = p - smp->sm_lead;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002040 *p++ = NUL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002041
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002042 /* Put (abc) chars in sm_oneof, if any. */
2043 if (c == '(')
2044 {
2045 smp->sm_oneof = p;
2046 for (++i; i < ccnt; ++i)
2047 {
2048 c = getc(fd); /* <salfrom> */
2049 if (c == ')')
2050 break;
2051 *p++ = c;
2052 }
2053 *p++ = NUL;
2054 if (++i < ccnt)
2055 c = getc(fd);
2056 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002057 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002058 smp->sm_oneof = NULL;
2059
2060 /* Any following chars go in sm_rules. */
2061 smp->sm_rules = p;
2062 if (i < ccnt)
2063 /* store the char we got while checking for end of sm_lead */
2064 *p++ = c;
2065 for (++i; i < ccnt; ++i)
2066 *p++ = getc(fd); /* <salfrom> */
2067 *p++ = NUL;
2068
Bram Moolenaar7887d882005-07-01 22:33:52 +00002069 /* <saltolen> <salto> */
2070 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002071 if (ccnt < 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002072 {
2073 vim_free(smp->sm_lead);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002074 goto formerr;
2075 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002076
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002077#ifdef FEAT_MBYTE
2078 if (has_mbyte)
2079 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002080 /* convert the multi-byte strings to wide char strings */
2081 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2082 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2083 if (smp->sm_oneof == NULL)
2084 smp->sm_oneof_w = NULL;
2085 else
2086 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002087 if (smp->sm_to == NULL)
2088 smp->sm_to_w = NULL;
2089 else
2090 smp->sm_to_w = mb_str2wide(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002091 if (smp->sm_lead_w == NULL
2092 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002093 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002094 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002095 vim_free(smp->sm_lead);
2096 vim_free(smp->sm_to);
2097 vim_free(smp->sm_lead_w);
2098 vim_free(smp->sm_oneof_w);
2099 vim_free(smp->sm_to_w);
2100 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002101 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002102 }
2103#endif
2104 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002105
2106 /* Fill the first-index table. */
Bram Moolenaar7887d882005-07-01 22:33:52 +00002107 set_sal_first(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002108 }
2109
Bram Moolenaar7887d882005-07-01 22:33:52 +00002110 /* <maplen> <mapstr> */
2111 p = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002112 if (cnt < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002113 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002114 if (p != NULL)
2115 {
2116 set_map_str(lp, p);
2117 vim_free(p);
2118 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002119
Bram Moolenaar51485f02005-06-04 21:55:20 +00002120 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002121 * round 2: <KWORDTREE>
2122 * round 3: <PREFIXTREE> */
2123 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002124 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002125 /* The tree size was computed when writing the file, so that we can
2126 * allocate it as one long block. <nodecount> */
2127 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2128 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002129 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002130 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002131 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002132 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002133 bp = lalloc((long_u)len, TRUE);
2134 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002135 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002136 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002137 lp->sl_fbyts = bp;
2138 else if (round == 2)
2139 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002140 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002141 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002142
2143 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002144 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2145 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002146 goto endFAIL;
2147 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002148 lp->sl_fidxs = ip;
2149 else if (round == 2)
2150 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002151 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002152 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002153
2154 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002155 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002156 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002157 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002158 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002159 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002160 }
2161 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002162
Bram Moolenaarb765d632005-06-07 21:00:02 +00002163 /* For a new file link it in the list of spell files. */
2164 if (old_lp == NULL)
2165 {
2166 lp->sl_next = first_lang;
2167 first_lang = lp;
2168 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002169
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002170 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002171
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002172endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002173 if (lang != NULL)
2174 /* truncating the name signals the error to spell_load_lang() */
2175 *lang = NUL;
2176 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002177 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002178 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002179 lp = NULL;
2180 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002181
2182endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002183 if (fd != NULL)
2184 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002185 sourcing_name = save_sourcing_name;
2186 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002187
2188 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002189}
2190
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002191/*
2192 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002193 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002194 * Returns NULL when the count is zero.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002195 * Sets "*cntp" to -1 when there is an error, length of the result otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002196 */
2197 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002198read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002199 FILE *fd;
2200 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002201 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002202{
2203 int cnt = 0;
2204 int i;
2205 char_u *str;
2206
2207 /* read the length bytes, MSB first */
2208 for (i = 0; i < cnt_bytes; ++i)
2209 cnt = (cnt << 8) + getc(fd);
2210 if (cnt < 0)
2211 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002212 EMSG(_(e_spell_trunc));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002213 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002214 return NULL;
2215 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002216 *cntp = cnt;
2217 if (cnt == 0)
2218 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002219
2220 /* allocate memory */
2221 str = alloc((unsigned)cnt + 1);
2222 if (str == NULL)
2223 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002224 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002225 return NULL;
2226 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002227
2228 /* Read the string. Doesn't check for truncated file. */
2229 for (i = 0; i < cnt; ++i)
2230 str[i] = getc(fd);
2231 str[i] = NUL;
2232
2233 return str;
2234}
2235
Bram Moolenaar7887d882005-07-01 22:33:52 +00002236/*
2237 * Set the SOFOFROM and SOFOTO items in language "lp".
2238 * Returns FAIL when there is something wrong.
2239 */
2240 static int
2241set_sofo(lp, from, to)
2242 slang_T *lp;
2243 char_u *from;
2244 char_u *to;
2245{
2246 int i;
2247
2248#ifdef FEAT_MBYTE
2249 garray_T *gap;
2250 char_u *s;
2251 char_u *p;
2252 int c;
2253 int *inp;
2254
2255 if (has_mbyte)
2256 {
2257 /* Use "sl_sal" as an array with 256 pointers to a list of wide
2258 * characters. The index is the low byte of the character.
2259 * The list contains from-to pairs with a terminating NUL.
2260 * sl_sal_first[] is used for latin1 "from" characters. */
2261 gap = &lp->sl_sal;
2262 ga_init2(gap, sizeof(int *), 1);
2263 if (ga_grow(gap, 256) == FAIL)
2264 return FAIL;
2265 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
2266 gap->ga_len = 256;
2267
2268 /* First count the number of items for each list. Temporarily use
2269 * sl_sal_first[] for this. */
2270 for (p = from, s = to; *p != NUL && *s != NUL; )
2271 {
2272 c = mb_ptr2char_adv(&p);
2273 mb_ptr_adv(s);
2274 if (c >= 256)
2275 ++lp->sl_sal_first[c & 0xff];
2276 }
2277 if (*p != NUL || *s != NUL) /* lengths differ */
2278 return FAIL;
2279
2280 /* Allocate the lists. */
2281 for (i = 0; i < 256; ++i)
2282 if (lp->sl_sal_first[i] > 0)
2283 {
2284 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
2285 if (p == NULL)
2286 return FAIL;
2287 ((int **)gap->ga_data)[i] = (int *)p;
2288 *(int *)p = 0;
2289 }
2290
2291 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
2292 * list. */
2293 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
2294 for (p = from, s = to; *p != NUL && *s != NUL; )
2295 {
2296 c = mb_ptr2char_adv(&p);
2297 i = mb_ptr2char_adv(&s);
2298 if (c >= 256)
2299 {
2300 /* Append the from-to chars at the end of the list with
2301 * the low byte. */
2302 inp = ((int **)gap->ga_data)[c & 0xff];
2303 while (*inp != 0)
2304 ++inp;
2305 *inp++ = c; /* from char */
2306 *inp++ = i; /* to char */
2307 *inp++ = NUL; /* NUL at the end */
2308 }
2309 else
2310 /* mapping byte to char is done in sl_sal_first[] */
2311 lp->sl_sal_first[c] = i;
2312 }
2313 }
2314 else
2315#endif
2316 {
2317 /* mapping bytes to bytes is done in sl_sal_first[] */
2318 if (STRLEN(from) != STRLEN(to))
2319 return FAIL;
2320
2321 for (i = 0; to[i] != NUL; ++i)
2322 lp->sl_sal_first[from[i]] = to[i];
2323 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
2324 }
2325
2326 return OK;
2327}
2328
2329/*
2330 * Fill the first-index table for "lp".
2331 */
2332 static void
2333set_sal_first(lp)
2334 slang_T *lp;
2335{
2336 salfirst_T *sfirst;
2337 int i;
2338 salitem_T *smp;
2339 int c;
2340 garray_T *gap = &lp->sl_sal;
2341
2342 sfirst = lp->sl_sal_first;
2343 for (i = 0; i < 256; ++i)
2344 sfirst[i] = -1;
2345 smp = (salitem_T *)gap->ga_data;
2346 for (i = 0; i < gap->ga_len; ++i)
2347 {
2348#ifdef FEAT_MBYTE
2349 if (has_mbyte)
2350 /* Use the lowest byte of the first character. For latin1 it's
2351 * the character, for other encodings it should differ for most
2352 * characters. */
2353 c = *smp[i].sm_lead_w & 0xff;
2354 else
2355#endif
2356 c = *smp[i].sm_lead;
2357 if (sfirst[c] == -1)
2358 {
2359 sfirst[c] = i;
2360#ifdef FEAT_MBYTE
2361 if (has_mbyte)
2362 {
2363 int n;
2364
2365 /* Make sure all entries with this byte are following each
2366 * other. Move the ones that are in the wrong position. Do
2367 * keep the same ordering! */
2368 while (i + 1 < gap->ga_len
2369 && (*smp[i + 1].sm_lead_w & 0xff) == c)
2370 /* Skip over entry with same index byte. */
2371 ++i;
2372
2373 for (n = 1; i + n < gap->ga_len; ++n)
2374 if ((*smp[i + n].sm_lead_w & 0xff) == c)
2375 {
2376 salitem_T tsal;
2377
2378 /* Move entry with same index byte after the entries
2379 * we already found. */
2380 ++i;
2381 --n;
2382 tsal = smp[i + n];
2383 mch_memmove(smp + i + 1, smp + i,
2384 sizeof(salitem_T) * n);
2385 smp[i] = tsal;
2386 }
2387 }
2388#endif
2389 }
2390 }
2391}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002392
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002393#ifdef FEAT_MBYTE
2394/*
2395 * Turn a multi-byte string into a wide character string.
2396 * Return it in allocated memory (NULL for out-of-memory)
2397 */
2398 static int *
2399mb_str2wide(s)
2400 char_u *s;
2401{
2402 int *res;
2403 char_u *p;
2404 int i = 0;
2405
2406 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
2407 if (res != NULL)
2408 {
2409 for (p = s; *p != NUL; )
2410 res[i++] = mb_ptr2char_adv(&p);
2411 res[i] = NUL;
2412 }
2413 return res;
2414}
2415#endif
2416
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002417/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002418 * Read one row of siblings from the spell file and store it in the byte array
2419 * "byts" and index array "idxs". Recursively read the children.
2420 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00002421 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00002422 *
2423 * Returns the index follosing the siblings.
2424 * Returns -1 if the file is shorter than expected.
2425 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002426 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002427 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002428read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002429 FILE *fd;
2430 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002431 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002432 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002433 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002434 int prefixtree; /* TRUE for reading PREFIXTREE */
2435 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002436{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002437 int len;
2438 int i;
2439 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002440 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002441 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002442 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002443#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002444
Bram Moolenaar51485f02005-06-04 21:55:20 +00002445 len = getc(fd); /* <siblingcount> */
2446 if (len <= 0)
2447 return -1;
2448
2449 if (startidx + len >= maxidx)
2450 return -2;
2451 byts[idx++] = len;
2452
2453 /* Read the byte values, flag/region bytes and shared indexes. */
2454 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002455 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002456 c = getc(fd); /* <byte> */
2457 if (c < 0)
2458 return -1;
2459 if (c <= BY_SPECIAL)
2460 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002461 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002462 {
2463 /* No flags, all regions. */
2464 idxs[idx] = 0;
2465 c = 0;
2466 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002467 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002468 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002469 if (prefixtree)
2470 {
2471 /* Read the prefix ID and the condition nr. In idxs[]
2472 * store the prefix ID in the low byte, the condition
2473 * index shifted up 8 bits. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002474 c2 = getc(fd); /* <prefixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002475 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
2476 if (n >= maxprefcondnr)
2477 return -2;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002478 c2 += (n << 8);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002479
2480 /* the byte value is misused to pass two flags: rare
2481 * and non-combining. */
2482 if (c == BY_FLAGS || c == BY_PFX_RNC)
2483 c2 |= WF_RAREPFX;
2484 if (c == BY_PFX_NC || c == BY_PFX_RNC)
2485 c2 |= WF_PFX_NC;
2486 c = c2;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002487 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002488 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002489 {
2490 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002491 * idxs[] the flags go in the low two bytes, region above
2492 * that and prefix ID above the region. */
2493 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002494 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002495 if (c2 == BY_FLAGS2)
2496 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002497 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002498 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002499 if (c & WF_PFX)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002500 c = (getc(fd) << 24) + c; /* <prefixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002501 }
2502
Bram Moolenaar51485f02005-06-04 21:55:20 +00002503 idxs[idx] = c;
2504 c = 0;
2505 }
2506 else /* c == BY_INDEX */
2507 {
2508 /* <nodeidx> */
2509 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2510 if (n < 0 || n >= maxidx)
2511 return -2;
2512 idxs[idx] = n + SHARED_MASK;
2513 c = getc(fd); /* <xbyte> */
2514 }
2515 }
2516 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002517 }
2518
Bram Moolenaar51485f02005-06-04 21:55:20 +00002519 /* Recursively read the children for non-shared siblings.
2520 * Skip the end-of-word ones (zero byte value) and the shared ones (and
2521 * remove SHARED_MASK) */
2522 for (i = 1; i <= len; ++i)
2523 if (byts[startidx + i] != 0)
2524 {
2525 if (idxs[startidx + i] & SHARED_MASK)
2526 idxs[startidx + i] &= ~SHARED_MASK;
2527 else
2528 {
2529 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002530 idx = read_tree(fd, byts, idxs, maxidx, idx,
2531 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002532 if (idx < 0)
2533 break;
2534 }
2535 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002536
Bram Moolenaar51485f02005-06-04 21:55:20 +00002537 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002538}
2539
2540/*
2541 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002542 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002543 */
2544 char_u *
2545did_set_spelllang(buf)
2546 buf_T *buf;
2547{
2548 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002549 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002550 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002551 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002552 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002553 int region_mask;
2554 slang_T *lp;
2555 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002556 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002557 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002558 int len;
2559 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002560 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002561 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002562 char_u *use_region = NULL;
2563 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002564
2565 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002566 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002567
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002568 /* loop over comma separated language names. */
2569 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002570 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002571 /* Get one language name. */
2572 copy_option_part(&splp, lang, MAXWLEN, ",");
2573
Bram Moolenaar5482f332005-04-17 20:18:43 +00002574 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002575 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002576
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002577 /* If the name ends in ".spl" use it as the name of the spell file.
2578 * If there is a region name let "region" point to it and remove it
2579 * from the name. */
2580 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2581 {
2582 filename = TRUE;
2583
Bram Moolenaarb6356332005-07-18 21:40:44 +00002584 /* Locate a region and remove it from the file name. */
2585 p = vim_strchr(gettail(lang), '_');
2586 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2587 && !ASCII_ISALPHA(p[3]))
2588 {
2589 vim_strncpy(region_cp, p + 1, 2);
2590 mch_memmove(p, p + 3, len - (p - lang) - 2);
2591 len -= 3;
2592 region = region_cp;
2593 }
2594 else
2595 dont_use_region = TRUE;
2596
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002597 /* Check if we loaded this language before. */
2598 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2599 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
2600 break;
2601 }
2602 else
2603 {
2604 filename = FALSE;
2605 if (len > 3 && lang[len - 3] == '_')
2606 {
2607 region = lang + len - 2;
2608 len -= 3;
2609 lang[len] = NUL;
2610 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002611 else
2612 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002613
2614 /* Check if we loaded this language before. */
2615 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2616 if (STRICMP(lang, lp->sl_name) == 0)
2617 break;
2618 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002619
Bram Moolenaarb6356332005-07-18 21:40:44 +00002620 if (region != NULL)
2621 {
2622 /* If the region differs from what was used before then don't
2623 * use it for 'spellfile'. */
2624 if (use_region != NULL && STRCMP(region, use_region) != 0)
2625 dont_use_region = TRUE;
2626 use_region = region;
2627 }
2628
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002629 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002630 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002631 {
2632 if (filename)
2633 (void)spell_load_file(lang, lang, NULL, FALSE);
2634 else
2635 spell_load_lang(lang);
2636 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002637
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002638 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002639 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002640 */
2641 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002642 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
2643 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002644 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002645 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002646 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002647 {
2648 /* find region in sl_regions */
2649 c = find_region(lp->sl_regions, region);
2650 if (c == REGION_ALL)
2651 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002652 if (lp->sl_add)
2653 {
2654 if (*lp->sl_regions != NUL)
2655 /* This addition file is for other regions. */
2656 region_mask = 0;
2657 }
2658 else
2659 /* This is probably an error. Give a warning and
2660 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002661 smsg((char_u *)
2662 _("Warning: region %s not supported"),
2663 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002664 }
2665 else
2666 region_mask = 1 << c;
2667 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002668
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002669 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002670 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002671 if (ga_grow(&ga, 1) == FAIL)
2672 {
2673 ga_clear(&ga);
2674 return e_outofmem;
2675 }
2676 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2677 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2678 ++ga.ga_len;
2679 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002680 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002681 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002682 }
2683
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002684 /* round 0: load int_wordlist, if possible.
2685 * round 1: load first name in 'spellfile'.
2686 * round 2: load second name in 'spellfile.
2687 * etc. */
2688 spf = curbuf->b_p_spf;
2689 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002690 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002691 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002692 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002693 /* Internal wordlist, if there is one. */
2694 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002695 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002696 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002697 }
2698 else
2699 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002700 /* One entry in 'spellfile'. */
2701 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2702 STRCAT(spf_name, ".spl");
2703
2704 /* If it was already found above then skip it. */
2705 for (c = 0; c < ga.ga_len; ++c)
2706 if (fullpathcmp(spf_name,
2707 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
2708 FALSE) == FPC_SAME)
2709 break;
2710 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002711 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002712 }
2713
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002714 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002715 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2716 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2717 break;
2718 if (lp == NULL)
2719 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002720 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002721 * region name, the region is ignored otherwise. for int_wordlist
2722 * use an arbitrary name. */
2723 if (round == 0)
2724 STRCPY(lang, "internal wordlist");
2725 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002726 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002727 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002728 p = vim_strchr(lang, '.');
2729 if (p != NULL)
2730 *p = NUL; /* truncate at ".encoding.add" */
2731 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002732 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002733 }
2734 if (lp != NULL && ga_grow(&ga, 1) == OK)
2735 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002736 region_mask = REGION_ALL;
2737 if (use_region != NULL && !dont_use_region)
2738 {
2739 /* find region in sl_regions */
2740 c = find_region(lp->sl_regions, use_region);
2741 if (c != REGION_ALL)
2742 region_mask = 1 << c;
2743 else if (*lp->sl_regions != NUL)
2744 /* This spell file is for other regions. */
2745 region_mask = 0;
2746 }
2747
2748 if (region_mask != 0)
2749 {
2750 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2751 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2752 ++ga.ga_len;
2753 use_midword(lp, buf);
2754 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002755 }
2756 }
2757
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002758 /* Add a NULL entry to mark the end of the list. */
2759 if (ga_grow(&ga, 1) == FAIL)
2760 {
2761 ga_clear(&ga);
2762 return e_outofmem;
2763 }
2764 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2765 ++ga.ga_len;
2766
2767 /* Everything is fine, store the new b_langp value. */
2768 ga_clear(&buf->b_langp);
2769 buf->b_langp = ga;
2770
2771 return NULL;
2772}
2773
2774/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002775 * Clear the midword characters for buffer "buf".
2776 */
2777 static void
2778clear_midword(buf)
2779 buf_T *buf;
2780{
2781 vim_memset(buf->b_spell_ismw, 0, 256);
2782#ifdef FEAT_MBYTE
2783 vim_free(buf->b_spell_ismw_mb);
2784 buf->b_spell_ismw_mb = NULL;
2785#endif
2786}
2787
2788/*
2789 * Use the "sl_midword" field of language "lp" for buffer "buf".
2790 * They add up to any currently used midword characters.
2791 */
2792 static void
2793use_midword(lp, buf)
2794 slang_T *lp;
2795 buf_T *buf;
2796{
2797 char_u *p;
2798
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002799 if (lp->sl_midword == NULL) /* there aren't any */
2800 return;
2801
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002802 for (p = lp->sl_midword; *p != NUL; )
2803#ifdef FEAT_MBYTE
2804 if (has_mbyte)
2805 {
2806 int c, l, n;
2807 char_u *bp;
2808
2809 c = mb_ptr2char(p);
2810 l = mb_ptr2len_check(p);
2811 if (c < 256)
2812 buf->b_spell_ismw[c] = TRUE;
2813 else if (buf->b_spell_ismw_mb == NULL)
2814 /* First multi-byte char in "b_spell_ismw_mb". */
2815 buf->b_spell_ismw_mb = vim_strnsave(p, l);
2816 else
2817 {
2818 /* Append multi-byte chars to "b_spell_ismw_mb". */
2819 n = STRLEN(buf->b_spell_ismw_mb);
2820 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
2821 if (bp != NULL)
2822 {
2823 vim_free(buf->b_spell_ismw_mb);
2824 buf->b_spell_ismw_mb = bp;
2825 vim_strncpy(bp + n, p, l);
2826 }
2827 }
2828 p += l;
2829 }
2830 else
2831#endif
2832 buf->b_spell_ismw[*p++] = TRUE;
2833}
2834
2835/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002836 * Find the region "region[2]" in "rp" (points to "sl_regions").
2837 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002838 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002839 */
2840 static int
2841find_region(rp, region)
2842 char_u *rp;
2843 char_u *region;
2844{
2845 int i;
2846
2847 for (i = 0; ; i += 2)
2848 {
2849 if (rp[i] == NUL)
2850 return REGION_ALL;
2851 if (rp[i] == region[0] && rp[i + 1] == region[1])
2852 break;
2853 }
2854 return i / 2;
2855}
2856
2857/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002858 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002859 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002860 * Word WF_ONECAP
2861 * W WORD WF_ALLCAP
2862 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002863 */
2864 static int
2865captype(word, end)
2866 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002867 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002868{
2869 char_u *p;
2870 int c;
2871 int firstcap;
2872 int allcap;
2873 int past_second = FALSE; /* past second word char */
2874
2875 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002876 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002877 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002878 return 0; /* only non-word characters, illegal word */
2879#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002880 if (has_mbyte)
2881 c = mb_ptr2char_adv(&p);
2882 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002883#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002884 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002885 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002886
2887 /*
2888 * Need to check all letters to find a word with mixed upper/lower.
2889 * But a word with an upper char only at start is a ONECAP.
2890 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002891 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002892 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002893 {
2894#ifdef FEAT_MBYTE
2895 c = mb_ptr2char(p);
2896#else
2897 c = *p;
2898#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002899 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002900 {
2901 /* UUl -> KEEPCAP */
2902 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002903 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002904 allcap = FALSE;
2905 }
2906 else if (!allcap)
2907 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002908 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002909 past_second = TRUE;
2910 }
2911
2912 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002913 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002914 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002915 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002916 return 0;
2917}
2918
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002919# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
2920/*
2921 * Free all languages.
2922 */
2923 void
2924spell_free_all()
2925{
2926 slang_T *lp;
2927 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002928 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002929
2930 /* Go through all buffers and handle 'spelllang'. */
2931 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2932 ga_clear(&buf->b_langp);
2933
2934 while (first_lang != NULL)
2935 {
2936 lp = first_lang;
2937 first_lang = lp->sl_next;
2938 slang_free(lp);
2939 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002940
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002941 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002942 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002943 /* Delete the internal wordlist and its .spl file */
2944 mch_remove(int_wordlist);
2945 int_wordlist_spl(fname);
2946 mch_remove(fname);
2947 vim_free(int_wordlist);
2948 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002949 }
2950
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002951 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002952}
2953# endif
2954
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002955# if defined(FEAT_MBYTE) || defined(PROTO)
2956/*
2957 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002958 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002959 */
2960 void
2961spell_reload()
2962{
2963 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002964 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002965
Bram Moolenaarea408852005-06-25 22:49:46 +00002966 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002967 init_spell_chartab();
2968
2969 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002970 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002971
2972 /* Go through all buffers and handle 'spelllang'. */
2973 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2974 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002975 /* Only load the wordlists when 'spelllang' is set and there is a
2976 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002977 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002978 {
2979 FOR_ALL_WINDOWS(wp)
2980 if (wp->w_buffer == buf && wp->w_p_spell)
2981 {
2982 (void)did_set_spelllang(buf);
2983# ifdef FEAT_WINDOWS
2984 break;
2985# endif
2986 }
2987 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002988 }
2989}
2990# endif
2991
Bram Moolenaarb765d632005-06-07 21:00:02 +00002992/*
2993 * Reload the spell file "fname" if it's loaded.
2994 */
2995 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002996spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002997 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002998 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002999{
3000 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003001 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003002
Bram Moolenaarb765d632005-06-07 21:00:02 +00003003 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3004 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
3005 {
3006 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003007 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003008 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003009 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003010 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003011
3012 /* When "zg" was used and the file wasn't loaded yet, should redo
3013 * 'spelllang' to get it loaded. */
3014 if (added_word && !didit)
3015 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003016}
3017
3018
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003019/*
3020 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003021 */
3022
Bram Moolenaar51485f02005-06-04 21:55:20 +00003023#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003024 and .dic file. */
3025/*
3026 * Main structure to store the contents of a ".aff" file.
3027 */
3028typedef struct afffile_S
3029{
3030 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003031 int af_rar; /* RAR ID for rare word */
3032 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003033 int af_bad; /* BAD ID for banned word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003034 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003035 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
3036 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003037} afffile_T;
3038
3039typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003040/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
3041struct affentry_S
3042{
3043 affentry_T *ae_next; /* next affix with same name/number */
3044 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
3045 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003046 char_u *ae_cond; /* condition (NULL for ".") */
3047 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003048 int ae_rare; /* rare affix */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003049};
3050
3051/* Affix header from ".aff" file. Used for af_pref and af_suff. */
3052typedef struct affheader_S
3053{
3054 char_u ah_key[2]; /* key for hashtable == name of affix entry */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003055 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003056 int ah_combine; /* suffix may combine with prefix */
3057 affentry_T *ah_first; /* first affix entry */
3058} affheader_T;
3059
3060#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
3061
3062/*
3063 * Structure that is used to store the items in the word tree. This avoids
3064 * the need to keep track of each allocated thing, it's freed all at once
3065 * after ":mkspell" is done.
3066 */
3067#define SBLOCKSIZE 16000 /* size of sb_data */
3068typedef struct sblock_S sblock_T;
3069struct sblock_S
3070{
3071 sblock_T *sb_next; /* next block in list */
3072 int sb_used; /* nr of bytes already in use */
3073 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003074};
3075
3076/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003077 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003078 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003079typedef struct wordnode_S wordnode_T;
3080struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003081{
Bram Moolenaar0c405862005-06-22 22:26:26 +00003082 union /* shared to save space */
3083 {
3084 char_u hashkey[6]; /* room for the hash key */
3085 int index; /* index in written nodes (valid after first
3086 round) */
3087 } wn_u1;
3088 union /* shared to save space */
3089 {
3090 wordnode_T *next; /* next node with same hash key */
3091 wordnode_T *wnode; /* parent node that will write this node */
3092 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003093 wordnode_T *wn_child; /* child (next byte in word) */
3094 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
3095 always sorted) */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003096 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003097 short_u wn_flags; /* when wn_byte is NUL: WF_ flags */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003098 short wn_region; /* when wn_byte is NUL: region mask; for
3099 PREFIXTREE it's the prefcondnr */
3100 char_u wn_prefixID; /* supported/required prefix ID or 0 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003101};
3102
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003103#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
3104
Bram Moolenaar51485f02005-06-04 21:55:20 +00003105#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003106
Bram Moolenaar51485f02005-06-04 21:55:20 +00003107/*
3108 * Info used while reading the spell files.
3109 */
3110typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003111{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003112 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003113 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003114 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003115 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003116 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003117 sblock_T *si_blocks; /* memory blocks used */
3118 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003119 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003120 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003121 int si_region; /* region mask */
3122 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003123 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003124 int si_verbose; /* verbose messages */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003125 int si_region_count; /* number of regions supported (1 when there
3126 are no regions) */
3127 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003128
3129 garray_T si_rep; /* list of fromto_T entries from REP lines */
3130 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003131 char_u *si_sofofr; /* SOFOFROM text */
3132 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003133 int si_followup; /* soundsalike: ? */
3134 int si_collapse; /* soundsalike: ? */
3135 int si_rem_accents; /* soundsalike: remove accents */
3136 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003137 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003138 garray_T si_prefcond; /* table with conditions for postponed
3139 * prefixes, each stored as a string */
3140 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003141} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003142
Bram Moolenaar51485f02005-06-04 21:55:20 +00003143static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003144static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003145static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
3146static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00003147static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003148static void spell_free_aff __ARGS((afffile_T *aff));
3149static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003150static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp));
3151static 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 +00003152static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003153static void *getroom __ARGS((sblock_T **blp, size_t len, int align));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003154static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
3155static void free_blocks __ARGS((sblock_T *bl));
3156static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003157static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist));
3158static 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 +00003159static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003160static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
3161static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar3982c542005-06-08 21:56:31 +00003162static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar0c405862005-06-22 22:26:26 +00003163static void clear_node __ARGS((wordnode_T *node));
3164static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003165static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003166static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003167
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003168/* values for wn_flags used in the postponed prefixes tree */
3169#define PFX_FLAGS -1 /* not rare, combining */
3170#define PFX_FLAGS_R -2 /* rare, combining */
3171#define PFX_FLAGS_NC -3 /* not rare, not combining */
3172#define PFX_FLAGS_RNC -4 /* rare, not combining */
3173
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003174/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003175 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00003176 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003177 */
3178 static afffile_T *
Bram Moolenaar51485f02005-06-04 21:55:20 +00003179spell_read_aff(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003180 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003181 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003182{
3183 FILE *fd;
3184 afffile_T *aff;
3185 char_u rline[MAXLINELEN];
3186 char_u *line;
3187 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003188#define MAXITEMCNT 7
3189 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003190 int itemcnt;
3191 char_u *p;
3192 int lnum = 0;
3193 affheader_T *cur_aff = NULL;
3194 int aff_todo = 0;
3195 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003196 char_u *low = NULL;
3197 char_u *fol = NULL;
3198 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003199 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003200 int do_rep;
3201 int do_sal;
3202 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003203 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003204 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003205 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003206 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003207
Bram Moolenaar51485f02005-06-04 21:55:20 +00003208 /*
3209 * Open the file.
3210 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003211 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003212 if (fd == NULL)
3213 {
3214 EMSG2(_(e_notopen), fname);
3215 return NULL;
3216 }
3217
Bram Moolenaarb765d632005-06-07 21:00:02 +00003218 if (spin->si_verbose || p_verbose > 2)
3219 {
3220 if (!spin->si_verbose)
3221 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003222 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003223 out_flush();
3224 if (!spin->si_verbose)
3225 verbose_leave();
3226 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003227
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003228 /* Only do REP lines when not done in another .aff file already. */
3229 do_rep = spin->si_rep.ga_len == 0;
3230
3231 /* Only do SAL lines when not done in another .aff file already. */
3232 do_sal = spin->si_sal.ga_len == 0;
3233
3234 /* Only do MAP lines when not done in another .aff file already. */
3235 do_map = spin->si_map.ga_len == 0;
3236
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003237 /* Only do MIDWORD line when not done in another .aff file already */
3238 do_midword = spin->si_midword == NULL;
3239
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003240 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
3241 do_sofo = spin->si_sofofr == NULL;
3242
Bram Moolenaar51485f02005-06-04 21:55:20 +00003243 /*
3244 * Allocate and init the afffile_T structure.
3245 */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003246 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003247 if (aff == NULL)
3248 return NULL;
3249 hash_init(&aff->af_pref);
3250 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003251
3252 /*
3253 * Read all the lines in the file one by one.
3254 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003255 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003256 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003257 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003258 ++lnum;
3259
3260 /* Skip comment lines. */
3261 if (*rline == '#')
3262 continue;
3263
3264 /* Convert from "SET" to 'encoding' when needed. */
3265 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003266#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003267 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003268 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003269 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003270 if (pc == NULL)
3271 {
3272 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3273 fname, lnum, rline);
3274 continue;
3275 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003276 line = pc;
3277 }
3278 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003279#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003280 {
3281 pc = NULL;
3282 line = rline;
3283 }
3284
3285 /* Split the line up in white separated items. Put a NUL after each
3286 * item. */
3287 itemcnt = 0;
3288 for (p = line; ; )
3289 {
3290 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
3291 ++p;
3292 if (*p == NUL)
3293 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003294 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003295 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003296 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003297 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003298 ++p;
3299 if (*p == NUL)
3300 break;
3301 *p++ = NUL;
3302 }
3303
3304 /* Handle non-empty lines. */
3305 if (itemcnt > 0)
3306 {
3307 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
3308 && aff->af_enc == NULL)
3309 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003310#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003311 /* Setup for conversion from "ENC" to 'encoding'. */
3312 aff->af_enc = enc_canonize(items[1]);
3313 if (aff->af_enc != NULL && !spin->si_ascii
3314 && convert_setup(&spin->si_conv, aff->af_enc,
3315 p_enc) == FAIL)
3316 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
3317 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003318 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003319#else
3320 smsg((char_u *)_("Conversion in %s not supported"), fname);
3321#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003322 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003323 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
3324 {
3325 if (do_midword)
3326 spin->si_midword = vim_strsave(items[1]);
3327 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00003328 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
3329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003330 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003331 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003332 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003334 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003335 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003336 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
3337 && aff->af_rar == 0)
3338 {
3339 aff->af_rar = items[1][0];
3340 if (items[1][1] != NUL)
3341 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3342 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003343 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
3344 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003345 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003346 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003347 if (items[1][1] != NUL)
3348 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3349 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00003350 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
3351 && aff->af_bad == 0)
3352 {
3353 aff->af_bad = items[1][0];
3354 if (items[1][1] != NUL)
3355 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3356 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003357 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
3358 {
3359 aff->af_pfxpostpone = TRUE;
3360 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003361 else if ((STRCMP(items[0], "PFX") == 0
3362 || STRCMP(items[0], "SFX") == 0)
3363 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003364 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003365 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00003366 /* Myspell allows extra text after the item, but that might
3367 * mean mistakes go unnoticed. Require a comment-starter. */
3368 if (itemcnt > 4 && *items[4] != '#')
3369 smsg((char_u *)_("Trailing text in %s line %d: %s"),
3370 fname, lnum, items[4]);
3371
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003372 /* New affix letter. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003373 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003374 sizeof(affheader_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003375 if (cur_aff == NULL)
3376 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003377 cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003378 cur_aff->ah_key[1] = NUL;
3379 if (items[1][1] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003380 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003381 if (*items[2] == 'Y')
3382 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003383 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003384 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
3385 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003386
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003387 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003388 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003389 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003390 /* Use a new number in the .spl file later, to be able to
3391 * handle multiple .aff files. */
3392 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003393 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003394 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003395 else
3396 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003397 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003398 hi = hash_find(tp, cur_aff->ah_key);
3399 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00003400 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003401 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
3402 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003403 aff_todo = 0;
3404 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003405 else
3406 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003407 }
3408 else if ((STRCMP(items[0], "PFX") == 0
3409 || STRCMP(items[0], "SFX") == 0)
3410 && aff_todo > 0
3411 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003412 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003413 {
3414 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003415 int rare = FALSE;
3416 int lasti = 5;
3417
3418 /* Check for "rare" after the other info. */
3419 if (itemcnt > 5 && STRICMP(items[5], "rare") == 0)
3420 {
3421 rare = TRUE;
3422 lasti = 6;
3423 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003424
Bram Moolenaar8db73182005-06-17 21:51:16 +00003425 /* Myspell allows extra text after the item, but that might
3426 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003427 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00003428 smsg((char_u *)_("Trailing text in %s line %d: %s"),
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003429 fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00003430
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003431 /* New item for an affix letter. */
3432 --aff_todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003433 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003434 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003435 if (aff_entry == NULL)
3436 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003437 aff_entry->ae_rare = rare;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003438
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003439 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003440 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
3441 items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003442 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003443 aff_entry->ae_add = getroom_save(&spin->si_blocks,
3444 items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003445
Bram Moolenaar51485f02005-06-04 21:55:20 +00003446 /* Don't use an affix entry with non-ASCII characters when
3447 * "spin->si_ascii" is TRUE. */
3448 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003449 || has_non_ascii(aff_entry->ae_add)))
3450 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003451 aff_entry->ae_next = cur_aff->ah_first;
3452 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003453
3454 if (STRCMP(items[4], ".") != 0)
3455 {
3456 char_u buf[MAXLINELEN];
3457
3458 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
3459 items[4]);
3460 if (*items[0] == 'P')
3461 sprintf((char *)buf, "^%s", items[4]);
3462 else
3463 sprintf((char *)buf, "%s$", items[4]);
3464 aff_entry->ae_prog = vim_regcomp(buf,
3465 RE_MAGIC + RE_STRING);
3466 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003467
3468 /* For postponed prefixes we need an entry in si_prefcond
3469 * for the condition. Use an existing one if possible. */
3470 if (*items[0] == 'P' && aff->af_pfxpostpone
3471 && aff_entry->ae_chop == NULL)
3472 {
3473 int idx;
3474 char_u **pp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003475 int n;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003476
3477 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
3478 --idx)
3479 {
3480 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
3481 if (str_equal(p, aff_entry->ae_cond))
3482 break;
3483 }
3484 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
3485 {
3486 /* Not found, add a new condition. */
3487 idx = spin->si_prefcond.ga_len++;
3488 pp = ((char_u **)spin->si_prefcond.ga_data) + idx;
3489 if (aff_entry->ae_cond == NULL)
3490 *pp = NULL;
3491 else
3492 *pp = getroom_save(&spin->si_blocks,
3493 aff_entry->ae_cond);
3494 }
3495
3496 /* Add the prefix to the prefix tree. */
3497 if (aff_entry->ae_add == NULL)
3498 p = (char_u *)"";
3499 else
3500 p = aff_entry->ae_add;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003501 if (rare)
3502 {
3503 if (cur_aff->ah_combine)
3504 n = PFX_FLAGS_R;
3505 else
3506 n = PFX_FLAGS_RNC;
3507 }
3508 else
3509 {
3510 if (cur_aff->ah_combine)
3511 n = PFX_FLAGS;
3512 else
3513 n = PFX_FLAGS_NC;
3514 }
3515 tree_add_word(p, spin->si_prefroot, n,
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003516 idx, cur_aff->ah_newID, &spin->si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003517 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003518 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003519 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003520 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
3521 {
3522 if (fol != NULL)
3523 smsg((char_u *)_("Duplicate FOL in %s line %d"),
3524 fname, lnum);
3525 else
3526 fol = vim_strsave(items[1]);
3527 }
3528 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
3529 {
3530 if (low != NULL)
3531 smsg((char_u *)_("Duplicate LOW in %s line %d"),
3532 fname, lnum);
3533 else
3534 low = vim_strsave(items[1]);
3535 }
3536 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
3537 {
3538 if (upp != NULL)
3539 smsg((char_u *)_("Duplicate UPP in %s line %d"),
3540 fname, lnum);
3541 else
3542 upp = vim_strsave(items[1]);
3543 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003544 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003545 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003546 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003547 if (!isdigit(*items[1]))
3548 smsg((char_u *)_("Expected REP count in %s line %d"),
3549 fname, lnum);
3550 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003551 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
3552 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003553 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003554 if (do_rep)
3555 add_fromto(spin, &spin->si_rep, items[1], items[2]);
3556 }
3557 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
3558 {
3559 /* MAP item or count */
3560 if (!found_map)
3561 {
3562 /* First line contains the count. */
3563 found_map = TRUE;
3564 if (!isdigit(*items[1]))
3565 smsg((char_u *)_("Expected MAP count in %s line %d"),
3566 fname, lnum);
3567 }
3568 else if (do_map)
3569 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003570 int c;
3571
3572 /* Check that every character appears only once. */
3573 for (p = items[1]; *p != NUL; )
3574 {
3575#ifdef FEAT_MBYTE
3576 c = mb_ptr2char_adv(&p);
3577#else
3578 c = *p++;
3579#endif
3580 if ((spin->si_map.ga_len > 0
3581 && vim_strchr(spin->si_map.ga_data, c)
3582 != NULL)
3583 || vim_strchr(p, c) != NULL)
3584 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
3585 fname, lnum);
3586 }
3587
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003588 /* We simply concatenate all the MAP strings, separated by
3589 * slashes. */
3590 ga_concat(&spin->si_map, items[1]);
3591 ga_append(&spin->si_map, '/');
3592 }
3593 }
3594 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
3595 {
3596 if (do_sal)
3597 {
3598 /* SAL item (sounds-a-like)
3599 * Either one of the known keys or a from-to pair. */
3600 if (STRCMP(items[1], "followup") == 0)
3601 spin->si_followup = sal_to_bool(items[2]);
3602 else if (STRCMP(items[1], "collapse_result") == 0)
3603 spin->si_collapse = sal_to_bool(items[2]);
3604 else if (STRCMP(items[1], "remove_accents") == 0)
3605 spin->si_rem_accents = sal_to_bool(items[2]);
3606 else
3607 /* when "to" is "_" it means empty */
3608 add_fromto(spin, &spin->si_sal, items[1],
3609 STRCMP(items[2], "_") == 0 ? (char_u *)""
3610 : items[2]);
3611 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003612 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003613 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
3614 && (!do_sofo || spin->si_sofofr == NULL))
3615 {
3616 if (do_sofo)
3617 spin->si_sofofr = vim_strsave(items[1]);
3618 }
3619 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
3620 && (!do_sofo || spin->si_sofoto == NULL))
3621 {
3622 if (do_sofo)
3623 spin->si_sofoto = vim_strsave(items[1]);
3624 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003625 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003626 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
3627 fname, lnum, items[0]);
3628 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003629 }
3630
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003631 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
3632 smsg((char_u *)_("Missing SOFO%s line in %s"),
3633 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
3634 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
3635 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
3636
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003637 if (fol != NULL || low != NULL || upp != NULL)
3638 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003639 if (spin->si_clear_chartab)
3640 {
3641 /* Clear the char type tables, don't want to use any of the
3642 * currently used spell properties. */
3643 init_spell_chartab();
3644 spin->si_clear_chartab = FALSE;
3645 }
3646
Bram Moolenaar3982c542005-06-08 21:56:31 +00003647 /*
3648 * Don't write a word table for an ASCII file, so that we don't check
3649 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003650 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00003651 * mb_get_class(), the list of chars in the file will be incomplete.
3652 */
3653 if (!spin->si_ascii
3654#ifdef FEAT_MBYTE
3655 && !enc_utf8
3656#endif
3657 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003658 {
3659 if (fol == NULL || low == NULL || upp == NULL)
3660 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
3661 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00003662 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003663 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003664
3665 vim_free(fol);
3666 vim_free(low);
3667 vim_free(upp);
3668 }
3669
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003670 vim_free(pc);
3671 fclose(fd);
3672 return aff;
3673}
3674
3675/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003676 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3677 * NULL as equal.
3678 */
3679 static int
3680str_equal(s1, s2)
3681 char_u *s1;
3682 char_u *s2;
3683{
3684 if (s1 == NULL || s2 == NULL)
3685 return s1 == s2;
3686 return STRCMP(s1, s2) == 0;
3687}
3688
3689/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003690 * Add a from-to item to "gap". Used for REP and SAL items.
3691 * They are stored case-folded.
3692 */
3693 static void
3694add_fromto(spin, gap, from, to)
3695 spellinfo_T *spin;
3696 garray_T *gap;
3697 char_u *from;
3698 char_u *to;
3699{
3700 fromto_T *ftp;
3701 char_u word[MAXWLEN];
3702
3703 if (ga_grow(gap, 1) == OK)
3704 {
3705 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3706 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
3707 ftp->ft_from = getroom_save(&spin->si_blocks, word);
3708 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
3709 ftp->ft_to = getroom_save(&spin->si_blocks, word);
3710 ++gap->ga_len;
3711 }
3712}
3713
3714/*
3715 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3716 */
3717 static int
3718sal_to_bool(s)
3719 char_u *s;
3720{
3721 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3722}
3723
3724/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00003725 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
3726 * When "s" is NULL FALSE is returned.
3727 */
3728 static int
3729has_non_ascii(s)
3730 char_u *s;
3731{
3732 char_u *p;
3733
3734 if (s != NULL)
3735 for (p = s; *p != NUL; ++p)
3736 if (*p >= 128)
3737 return TRUE;
3738 return FALSE;
3739}
3740
3741/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003742 * Free the structure filled by spell_read_aff().
3743 */
3744 static void
3745spell_free_aff(aff)
3746 afffile_T *aff;
3747{
3748 hashtab_T *ht;
3749 hashitem_T *hi;
3750 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003751 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003752 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003753
3754 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003755
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003756 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003757 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3758 {
3759 todo = ht->ht_used;
3760 for (hi = ht->ht_array; todo > 0; ++hi)
3761 {
3762 if (!HASHITEM_EMPTY(hi))
3763 {
3764 --todo;
3765 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003766 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3767 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003768 }
3769 }
3770 if (ht == &aff->af_suff)
3771 break;
3772 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003773
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003774 hash_clear(&aff->af_pref);
3775 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003776}
3777
3778/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003779 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003780 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003781 */
3782 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00003783spell_read_dic(fname, spin, affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003784 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003785 spellinfo_T *spin;
3786 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003787{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003788 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003789 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00003790 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003791 char_u *pfxlist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003792 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003793 char_u *pc;
3794 char_u *w;
3795 int l;
3796 hash_T hash;
3797 hashitem_T *hi;
3798 FILE *fd;
3799 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003800 int non_ascii = 0;
3801 int retval = OK;
3802 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003803 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003804
Bram Moolenaar51485f02005-06-04 21:55:20 +00003805 /*
3806 * Open the file.
3807 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003808 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003809 if (fd == NULL)
3810 {
3811 EMSG2(_(e_notopen), fname);
3812 return FAIL;
3813 }
3814
Bram Moolenaar51485f02005-06-04 21:55:20 +00003815 /* The hashtable is only used to detect duplicated words. */
3816 hash_init(&ht);
3817
Bram Moolenaar8db73182005-06-17 21:51:16 +00003818 spin->si_foldwcount = 0;
3819 spin->si_keepwcount = 0;
3820
Bram Moolenaarb765d632005-06-07 21:00:02 +00003821 if (spin->si_verbose || p_verbose > 2)
3822 {
3823 if (!spin->si_verbose)
3824 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003825 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003826 out_flush();
3827 if (!spin->si_verbose)
3828 verbose_leave();
3829 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003830
3831 /* Read and ignore the first line: word count. */
3832 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003833 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003834 EMSG2(_("E760: No word count in %s"), fname);
3835
3836 /*
3837 * Read all the lines in the file one by one.
3838 * The words are converted to 'encoding' here, before being added to
3839 * the hashtable.
3840 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003841 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003842 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003843 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003844 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003845 if (line[0] == '#')
3846 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003847
Bram Moolenaar51485f02005-06-04 21:55:20 +00003848 /* Remove CR, LF and white space from the end. White space halfway
3849 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003850 l = STRLEN(line);
3851 while (l > 0 && line[l - 1] <= ' ')
3852 --l;
3853 if (l == 0)
3854 continue; /* empty line */
3855 line[l] = NUL;
3856
Bram Moolenaar51485f02005-06-04 21:55:20 +00003857 /* Find the optional affix names. */
3858 afflist = vim_strchr(line, '/');
3859 if (afflist != NULL)
3860 *afflist++ = NUL;
3861
3862 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
3863 if (spin->si_ascii && has_non_ascii(line))
3864 {
3865 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003866 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003867 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003868
Bram Moolenaarb765d632005-06-07 21:00:02 +00003869#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003870 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003871 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003872 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003873 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003874 if (pc == NULL)
3875 {
3876 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3877 fname, lnum, line);
3878 continue;
3879 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003880 w = pc;
3881 }
3882 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003883#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003884 {
3885 pc = NULL;
3886 w = line;
3887 }
3888
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003889 /* This takes time, print a message now and then. */
3890 if (spin->si_verbose && (lnum & 0x3ff) == 0)
3891 {
3892 vim_snprintf((char *)message, sizeof(message),
3893 _("line %6d, word %6d - %s"),
3894 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3895 msg_start();
3896 msg_puts_long_attr(message, 0);
3897 msg_clr_eos();
3898 msg_didout = FALSE;
3899 msg_col = 0;
3900 out_flush();
3901 }
3902
Bram Moolenaar51485f02005-06-04 21:55:20 +00003903 /* Store the word in the hashtable to be able to find duplicates. */
3904 dw = (char_u *)getroom_save(&spin->si_blocks, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003905 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003906 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003907 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003908 if (retval == FAIL)
3909 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003910
Bram Moolenaar51485f02005-06-04 21:55:20 +00003911 hash = hash_hash(dw);
3912 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003913 if (!HASHITEM_EMPTY(hi))
3914 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003915 fname, lnum, dw);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003916 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00003917 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003918
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003919 flags = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003920 pfxlist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003921 if (afflist != NULL)
3922 {
3923 /* Check for affix name that stands for keep-case word and stands
3924 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003925 if (affile->af_kep != NUL
3926 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003927 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003928 if (affile->af_rar != NUL
3929 && vim_strchr(afflist, affile->af_rar) != NULL)
3930 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003931 if (affile->af_bad != NUL
3932 && vim_strchr(afflist, affile->af_bad) != NULL)
3933 flags |= WF_BANNED;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003934
3935 if (affile->af_pfxpostpone)
3936 /* Need to store the list of prefix IDs with the word. */
3937 pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003938 }
3939
Bram Moolenaar51485f02005-06-04 21:55:20 +00003940 /* Add the word to the word tree(s). */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003941 if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003942 retval = FAIL;
3943
3944 if (afflist != NULL)
3945 {
3946 /* Find all matching suffixes and add the resulting words.
3947 * Additionally do matching prefixes that combine. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003948 if (store_aff_word(dw, spin, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003949 &affile->af_suff, &affile->af_pref,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003950 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003951 retval = FAIL;
3952
3953 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003954 if (store_aff_word(dw, spin, afflist, affile,
3955 &affile->af_pref, NULL,
3956 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003957 retval = FAIL;
3958 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003959 }
3960
Bram Moolenaar51485f02005-06-04 21:55:20 +00003961 if (spin->si_ascii && non_ascii > 0)
3962 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
3963 non_ascii);
3964 hash_clear(&ht);
3965
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003966 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003967 return retval;
3968}
3969
3970/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003971 * Get the list of prefix IDs from the affix list "afflist".
3972 * Used for PFXPOSTPONE.
3973 * Returns a string allocated with getroom(). NULL when there are no prefixes
3974 * or when out of memory.
3975 */
3976 static char_u *
3977get_pfxlist(affile, afflist, blp)
3978 afffile_T *affile;
3979 char_u *afflist;
3980 sblock_T **blp;
3981{
3982 char_u *p;
3983 int cnt;
3984 int round;
3985 char_u *res = NULL;
3986 char_u key[2];
3987 hashitem_T *hi;
3988
3989 key[1] = NUL;
3990
3991 /* round 1: count the number of prefix IDs.
3992 * round 2: move prefix IDs to "res" */
3993 for (round = 1; round <= 2; ++round)
3994 {
3995 cnt = 0;
3996 for (p = afflist; *p != NUL; ++p)
3997 {
3998 key[0] = *p;
3999 hi = hash_find(&affile->af_pref, key);
4000 if (!HASHITEM_EMPTY(hi))
4001 {
4002 /* This is a prefix ID, use the new number. */
4003 if (round == 2)
4004 res[cnt] = HI2AH(hi)->ah_newID;
4005 ++cnt;
4006 }
4007 }
4008 if (round == 1 && cnt > 0)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004009 res = getroom(blp, cnt + 1, FALSE);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004010 if (res == NULL)
4011 break;
4012 }
4013
4014 if (res != NULL)
4015 res[cnt] = NUL;
4016 return res;
4017}
4018
4019/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004020 * Apply affixes to a word and store the resulting words.
4021 * "ht" is the hashtable with affentry_T that need to be applied, either
4022 * prefixes or suffixes.
4023 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
4024 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004025 *
4026 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004027 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004028 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004029store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004030 char_u *word; /* basic word start */
4031 spellinfo_T *spin; /* spell info */
4032 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004033 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004034 hashtab_T *ht;
4035 hashtab_T *xht;
4036 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004037 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004038 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004039{
4040 int todo;
4041 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004042 affheader_T *ah;
4043 affentry_T *ae;
4044 regmatch_T regmatch;
4045 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004046 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004047 int i;
4048 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004049 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004050 char_u *use_pfxlist;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004051
Bram Moolenaar51485f02005-06-04 21:55:20 +00004052 todo = ht->ht_used;
4053 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004054 {
4055 if (!HASHITEM_EMPTY(hi))
4056 {
4057 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004058 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004059
Bram Moolenaar51485f02005-06-04 21:55:20 +00004060 /* Check that the affix combines, if required, and that the word
4061 * supports this affix. */
4062 if ((!comb || ah->ah_combine)
4063 && vim_strchr(afflist, *ah->ah_key) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004064 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004065 /* Loop over all affix entries with this name. */
4066 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004067 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004068 /* Check the condition. It's not logical to match case
4069 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004070 * Myspell.
4071 * For prefixes, when "PFXPOSTPONE" was used, only do
4072 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004073 regmatch.regprog = ae->ae_prog;
4074 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004075 if ((xht != NULL || !affile->af_pfxpostpone
4076 || ae->ae_chop != NULL)
4077 && (ae->ae_prog == NULL
4078 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004079 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004080 /* Match. Remove the chop and add the affix. */
4081 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004082 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004083 /* prefix: chop/add at the start of the word */
4084 if (ae->ae_add == NULL)
4085 *newword = NUL;
4086 else
4087 STRCPY(newword, ae->ae_add);
4088 p = word;
4089 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004090 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004091 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004092#ifdef FEAT_MBYTE
4093 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004094 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004095 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004096 for ( ; i > 0; --i)
4097 mb_ptr_adv(p);
4098 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004099 else
4100#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004101 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004102 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004103 STRCAT(newword, p);
4104 }
4105 else
4106 {
4107 /* suffix: chop/add at the end of the word */
4108 STRCPY(newword, word);
4109 if (ae->ae_chop != NULL)
4110 {
4111 /* Remove chop string. */
4112 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004113 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004114 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004115 mb_ptr_back(newword, p);
4116 *p = NUL;
4117 }
4118 if (ae->ae_add != NULL)
4119 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004120 }
4121
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004122 /* Obey the "rare" flag of the affix. */
4123 if (ae->ae_rare)
4124 use_flags = flags | WF_RARE;
4125 else
4126 use_flags = flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004127 use_pfxlist = pfxlist;
4128
4129 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00004130 if (spin->si_prefroot != NULL
4131 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004132 {
4133 /* ... add a flag to indicate an affix was used. */
4134 use_flags |= WF_HAS_AFF;
4135
4136 /* ... don't use a prefix list if combining
4137 * affixes is not allowed */
4138 if (!ah->ah_combine || comb)
4139 use_pfxlist = NULL;
4140 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004141
Bram Moolenaar51485f02005-06-04 21:55:20 +00004142 /* Store the modified word. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004143 if (store_word(newword, spin, use_flags,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004144 spin->si_region, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004145 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004146
Bram Moolenaar51485f02005-06-04 21:55:20 +00004147 /* When added a suffix and combining is allowed also
4148 * try adding prefixes additionally. */
4149 if (xht != NULL && ah->ah_combine)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004150 if (store_aff_word(newword, spin, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004151 xht, NULL, TRUE,
4152 use_flags, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004153 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004154 }
4155 }
4156 }
4157 }
4158 }
4159
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004160 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004161}
4162
4163/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004164 * Read a file with a list of words.
4165 */
4166 static int
4167spell_read_wordfile(fname, spin)
4168 char_u *fname;
4169 spellinfo_T *spin;
4170{
4171 FILE *fd;
4172 long lnum = 0;
4173 char_u rline[MAXLINELEN];
4174 char_u *line;
4175 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004176 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004177 int l;
4178 int retval = OK;
4179 int did_word = FALSE;
4180 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004181 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004182 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004183
4184 /*
4185 * Open the file.
4186 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004187 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004188 if (fd == NULL)
4189 {
4190 EMSG2(_(e_notopen), fname);
4191 return FAIL;
4192 }
4193
Bram Moolenaarb765d632005-06-07 21:00:02 +00004194 if (spin->si_verbose || p_verbose > 2)
4195 {
4196 if (!spin->si_verbose)
4197 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004198 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004199 out_flush();
4200 if (!spin->si_verbose)
4201 verbose_leave();
4202 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004203
4204 /*
4205 * Read all the lines in the file one by one.
4206 */
4207 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4208 {
4209 line_breakcheck();
4210 ++lnum;
4211
4212 /* Skip comment lines. */
4213 if (*rline == '#')
4214 continue;
4215
4216 /* Remove CR, LF and white space from the end. */
4217 l = STRLEN(rline);
4218 while (l > 0 && rline[l - 1] <= ' ')
4219 --l;
4220 if (l == 0)
4221 continue; /* empty or blank line */
4222 rline[l] = NUL;
4223
4224 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
4225 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004226#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004227 if (spin->si_conv.vc_type != CONV_NONE)
4228 {
4229 pc = string_convert(&spin->si_conv, rline, NULL);
4230 if (pc == NULL)
4231 {
4232 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4233 fname, lnum, rline);
4234 continue;
4235 }
4236 line = pc;
4237 }
4238 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004239#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004240 {
4241 pc = NULL;
4242 line = rline;
4243 }
4244
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004245 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00004246 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004247 ++line;
4248 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004249 {
4250 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004251 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
4252 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004253 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004254 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
4255 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004256 else
4257 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004258#ifdef FEAT_MBYTE
4259 char_u *enc;
4260
Bram Moolenaar51485f02005-06-04 21:55:20 +00004261 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004262 line += 10;
4263 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004264 if (enc != NULL && !spin->si_ascii
4265 && convert_setup(&spin->si_conv, enc,
4266 p_enc) == FAIL)
4267 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00004268 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004269 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004270 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004271#else
4272 smsg((char_u *)_("Conversion in %s not supported"), fname);
4273#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004274 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004275 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004276 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004277
Bram Moolenaar3982c542005-06-08 21:56:31 +00004278 if (STRNCMP(line, "regions=", 8) == 0)
4279 {
4280 if (spin->si_region_count > 1)
4281 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
4282 fname, lnum, line);
4283 else
4284 {
4285 line += 8;
4286 if (STRLEN(line) > 16)
4287 smsg((char_u *)_("Too many regions in %s line %d: %s"),
4288 fname, lnum, line);
4289 else
4290 {
4291 spin->si_region_count = STRLEN(line) / 2;
4292 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004293
4294 /* Adjust the mask for a word valid in all regions. */
4295 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004296 }
4297 }
4298 continue;
4299 }
4300
Bram Moolenaar7887d882005-07-01 22:33:52 +00004301 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
4302 fname, lnum, line - 1);
4303 continue;
4304 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004305
Bram Moolenaar7887d882005-07-01 22:33:52 +00004306 flags = 0;
4307 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004308
Bram Moolenaar7887d882005-07-01 22:33:52 +00004309 /* Check for flags and region after a slash. */
4310 p = vim_strchr(line, '/');
4311 if (p != NULL)
4312 {
4313 *p++ = NUL;
4314 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004315 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004316 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004317 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004318 else if (*p == '!') /* Bad, bad, wicked word. */
4319 flags |= WF_BANNED;
4320 else if (*p == '?') /* Rare word. */
4321 flags |= WF_RARE;
4322 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004323 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004324 if ((flags & WF_REGION) == 0) /* first one */
4325 regionmask = 0;
4326 flags |= WF_REGION;
4327
4328 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00004329 if (l > spin->si_region_count)
4330 {
4331 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00004332 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004333 break;
4334 }
4335 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004336 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00004337 else
4338 {
4339 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
4340 fname, lnum, p);
4341 break;
4342 }
4343 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004344 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004345 }
4346
4347 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4348 if (spin->si_ascii && has_non_ascii(line))
4349 {
4350 ++non_ascii;
4351 continue;
4352 }
4353
4354 /* Normal word: store it. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004355 if (store_word(line, spin, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004356 {
4357 retval = FAIL;
4358 break;
4359 }
4360 did_word = TRUE;
4361 }
4362
4363 vim_free(pc);
4364 fclose(fd);
4365
Bram Moolenaarb765d632005-06-07 21:00:02 +00004366 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
4367 {
4368 if (p_verbose > 2)
4369 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00004370 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4371 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004372 if (p_verbose > 2)
4373 verbose_leave();
4374 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004375 return retval;
4376}
4377
4378/*
4379 * Get part of an sblock_T, "len" bytes long.
4380 * This avoids calling free() for every little struct we use.
4381 * The memory is cleared to all zeros.
4382 * Returns NULL when out of memory.
4383 */
4384 static void *
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004385getroom(blp, len, align)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004386 sblock_T **blp;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004387 size_t len; /* length needed */
4388 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004389{
4390 char_u *p;
4391 sblock_T *bl = *blp;
4392
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004393 if (align && bl != NULL)
4394 /* Round size up for alignment. On some systems structures need to be
4395 * aligned to the size of a pointer (e.g., SPARC). */
4396 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
4397 & ~(sizeof(char *) - 1);
4398
Bram Moolenaar51485f02005-06-04 21:55:20 +00004399 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4400 {
4401 /* Allocate a block of memory. This is not freed until much later. */
4402 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
4403 if (bl == NULL)
4404 return NULL;
4405 bl->sb_next = *blp;
4406 *blp = bl;
4407 bl->sb_used = 0;
4408 }
4409
4410 p = bl->sb_data + bl->sb_used;
4411 bl->sb_used += len;
4412
4413 return p;
4414}
4415
4416/*
4417 * Make a copy of a string into memory allocated with getroom().
4418 */
4419 static char_u *
4420getroom_save(blp, s)
4421 sblock_T **blp;
4422 char_u *s;
4423{
4424 char_u *sc;
4425
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004426 sc = (char_u *)getroom(blp, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004427 if (sc != NULL)
4428 STRCPY(sc, s);
4429 return sc;
4430}
4431
4432
4433/*
4434 * Free the list of allocated sblock_T.
4435 */
4436 static void
4437free_blocks(bl)
4438 sblock_T *bl;
4439{
4440 sblock_T *next;
4441
4442 while (bl != NULL)
4443 {
4444 next = bl->sb_next;
4445 vim_free(bl);
4446 bl = next;
4447 }
4448}
4449
4450/*
4451 * Allocate the root of a word tree.
4452 */
4453 static wordnode_T *
4454wordtree_alloc(blp)
4455 sblock_T **blp;
4456{
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004457 return (wordnode_T *)getroom(blp, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004458}
4459
4460/*
4461 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004462 * Always store it in the case-folded tree. For a keep-case word this is
4463 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
4464 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004465 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004466 * When "pfxlist" is not NULL store the word for each postponed prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004467 */
4468 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004469store_word(word, spin, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004470 char_u *word;
4471 spellinfo_T *spin;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004472 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004473 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004474 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004475{
4476 int len = STRLEN(word);
4477 int ct = captype(word, word + len);
4478 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004479 int res = OK;
4480 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004481
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004482 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004483 for (p = pfxlist; res == OK; ++p)
4484 {
4485 res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
4486 region, p == NULL ? 0 : *p, &spin->si_blocks);
4487 if (p == NULL || *p == NUL)
4488 break;
4489 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004490 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004491
4492 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
Bram Moolenaar8db73182005-06-17 21:51:16 +00004493 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004494 for (p = pfxlist; res == OK; ++p)
4495 {
4496 res = tree_add_word(word, spin->si_keeproot, flags,
4497 region, p == NULL ? 0 : *p, &spin->si_blocks);
4498 if (p == NULL || *p == NUL)
4499 break;
4500 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004501 ++spin->si_keepwcount;
4502 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004503 return res;
4504}
4505
4506/*
4507 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004508 * When "flags" < 0 we are adding to the prefix tree where flags is used for
4509 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004510 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004511 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004512 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004513tree_add_word(word, root, flags, region, prefixID, blp)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004514 char_u *word;
4515 wordnode_T *root;
4516 int flags;
4517 int region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004518 int prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004519 sblock_T **blp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004520{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004521 wordnode_T *node = root;
4522 wordnode_T *np;
4523 wordnode_T **prev = NULL;
4524 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004525
Bram Moolenaar51485f02005-06-04 21:55:20 +00004526 /* Add each byte of the word to the tree, including the NUL at the end. */
4527 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004528 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004529 /* Look for the sibling that has the same character. They are sorted
4530 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004531 * higher byte value. For zero bytes (end of word) the sorting is
4532 * done on flags and then on prefixID
Bram Moolenaar51485f02005-06-04 21:55:20 +00004533 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004534 while (node != NULL
4535 && (node->wn_byte < word[i]
4536 || (node->wn_byte == NUL
4537 && (flags < 0
4538 ? node->wn_prefixID < prefixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004539 : node->wn_flags < (flags & WN_MASK)
4540 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004541 && node->wn_prefixID < prefixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004542 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004543 prev = &node->wn_sibling;
4544 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004545 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004546 if (node == NULL
4547 || node->wn_byte != word[i]
4548 || (word[i] == NUL
4549 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004550 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004551 || node->wn_prefixID != prefixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004552 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004553 /* Allocate a new node. */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004554 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004555 if (np == NULL)
4556 return FAIL;
4557 np->wn_byte = word[i];
4558 *prev = np;
4559 np->wn_sibling = node;
4560 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004561 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004562
Bram Moolenaar51485f02005-06-04 21:55:20 +00004563 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004564 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004565 node->wn_flags = flags;
4566 node->wn_region |= region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004567 node->wn_prefixID = prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004568 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004569 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004570 prev = &node->wn_child;
4571 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004572 }
4573
4574 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004575}
4576
4577/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004578 * Compress a tree: find tails that are identical and can be shared.
4579 */
4580 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00004581wordtree_compress(root, spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004582 wordnode_T *root;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004583 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004584{
4585 hashtab_T ht;
4586 int n;
4587 int tot = 0;
4588
4589 if (root != NULL)
4590 {
4591 hash_init(&ht);
4592 n = node_compress(root, &ht, &tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004593 if (spin->si_verbose || p_verbose > 2)
4594 {
4595 if (!spin->si_verbose)
4596 verbose_enter();
4597 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00004598 n, tot, (tot - n) * 100 / tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004599 if (p_verbose > 2)
4600 verbose_leave();
4601 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004602 hash_clear(&ht);
4603 }
4604}
4605
4606/*
4607 * Compress a node, its siblings and its children, depth first.
4608 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004609 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004610 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00004611node_compress(node, ht, tot)
4612 wordnode_T *node;
4613 hashtab_T *ht;
4614 int *tot; /* total count of nodes before compressing,
4615 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004616{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004617 wordnode_T *np;
4618 wordnode_T *tp;
4619 wordnode_T *child;
4620 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004621 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004622 int len = 0;
4623 unsigned nr, n;
4624 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004625
Bram Moolenaar51485f02005-06-04 21:55:20 +00004626 /*
4627 * Go through the list of siblings. Compress each child and then try
4628 * finding an identical child to replace it.
4629 * Note that with "child" we mean not just the node that is pointed to,
4630 * but the whole list of siblings, of which the node is the first.
4631 */
4632 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004633 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004634 ++len;
4635 if ((child = np->wn_child) != NULL)
4636 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004637 /* Compress the child. This fills hashkey. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004638 compressed += node_compress(child, ht, tot);
4639
4640 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004641 hash = hash_hash(child->wn_u1.hashkey);
4642 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004643 tp = NULL;
4644 if (!HASHITEM_EMPTY(hi))
4645 {
4646 /* There are children with an identical hash value. Now check
4647 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004648 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004649 if (node_equal(child, tp))
4650 {
4651 /* Found one! Now use that child in place of the
4652 * current one. This means the current child is
4653 * dropped from the tree. */
4654 np->wn_child = tp;
4655 ++compressed;
4656 break;
4657 }
4658 if (tp == NULL)
4659 {
4660 /* No other child with this hash value equals the child of
4661 * the node, add it to the linked list after the first
4662 * item. */
4663 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004664 child->wn_u2.next = tp->wn_u2.next;
4665 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004666 }
4667 }
4668 else
4669 /* No other child has this hash value, add it to the
4670 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004671 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004672 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004673 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004674 *tot += len;
4675
4676 /*
4677 * Make a hash key for the node and its siblings, so that we can quickly
4678 * find a lookalike node. This must be done after compressing the sibling
4679 * list, otherwise the hash key would become invalid by the compression.
4680 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004681 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004682 nr = 0;
4683 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004684 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004685 if (np->wn_byte == NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004686 /* end node: use wn_flags, wn_region and wn_prefixID */
4687 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004688 else
4689 /* byte node: use the byte value and the child pointer */
4690 n = np->wn_byte + ((long_u)np->wn_child << 8);
4691 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004692 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004693
4694 /* Avoid NUL bytes, it terminates the hash key. */
4695 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004696 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004697 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004698 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004699 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004700 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004701 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004702 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
4703 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004704
4705 return compressed;
4706}
4707
4708/*
4709 * Return TRUE when two nodes have identical siblings and children.
4710 */
4711 static int
4712node_equal(n1, n2)
4713 wordnode_T *n1;
4714 wordnode_T *n2;
4715{
4716 wordnode_T *p1;
4717 wordnode_T *p2;
4718
4719 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
4720 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
4721 if (p1->wn_byte != p2->wn_byte
4722 || (p1->wn_byte == NUL
4723 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004724 || p1->wn_region != p2->wn_region
4725 || p1->wn_prefixID != p2->wn_prefixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004726 : (p1->wn_child != p2->wn_child)))
4727 break;
4728
4729 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004730}
4731
4732/*
4733 * Write a number to file "fd", MSB first, in "len" bytes.
4734 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004735 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004736put_bytes(fd, nr, len)
4737 FILE *fd;
4738 long_u nr;
4739 int len;
4740{
4741 int i;
4742
4743 for (i = len - 1; i >= 0; --i)
4744 putc((int)(nr >> (i * 8)), fd);
4745}
4746
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004747static int
4748#ifdef __BORLANDC__
4749_RTLENTRYF
4750#endif
4751rep_compare __ARGS((const void *s1, const void *s2));
4752
4753/*
4754 * Function given to qsort() to sort the REP items on "from" string.
4755 */
4756 static int
4757#ifdef __BORLANDC__
4758_RTLENTRYF
4759#endif
4760rep_compare(s1, s2)
4761 const void *s1;
4762 const void *s2;
4763{
4764 fromto_T *p1 = (fromto_T *)s1;
4765 fromto_T *p2 = (fromto_T *)s2;
4766
4767 return STRCMP(p1->ft_from, p2->ft_from);
4768}
4769
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004770/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004771 * Write the Vim spell file "fname".
4772 */
4773 static void
Bram Moolenaar3982c542005-06-08 21:56:31 +00004774write_vim_spell(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004775 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004776 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004777{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004778 FILE *fd;
4779 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004780 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004781 wordnode_T *tree;
4782 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004783 int i;
4784 int l;
4785 garray_T *gap;
4786 fromto_T *ftp;
4787 char_u *p;
4788 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004789
Bram Moolenaarb765d632005-06-07 21:00:02 +00004790 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004791 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004792 {
4793 EMSG2(_(e_notopen), fname);
4794 return;
4795 }
4796
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004797 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004798 * <charflagslen> <charflags>
4799 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004800 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004801 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004802
4803 /* <fileID> */
4804 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
4805 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004806
4807 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004808 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004809 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004810 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
4811 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
4812 (size_t)1, fd);
4813 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004814 }
4815 else
4816 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004817 putc(0, fd);
4818 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004819 }
4820
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004821 /*
4822 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004823 * <charflagslen> <charflags> <fcharlen> <fchars>
4824 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004825 * 'encoding'.
4826 * Also skip this for an .add.spl file, the main spell file must contain
4827 * the table (avoids that it conflicts). File is shorter too.
4828 */
4829 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004830 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004831 putc(0, fd);
4832 putc(0, fd);
4833 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004834 }
4835 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004836 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004837
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004838
4839 if (spin->si_midword == NULL)
4840 put_bytes(fd, 0L, 2); /* <midwordlen> */
4841 else
4842 {
4843 i = STRLEN(spin->si_midword);
4844 put_bytes(fd, (long_u)i, 2); /* <midwordlen> */
4845 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
4846 }
4847
4848
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004849 /* Write the prefix conditions. */
4850 write_spell_prefcond(fd, &spin->si_prefcond);
4851
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004852 /* <SUGGEST> : <repcount> <rep> ...
4853 * <salflags> <salcount> <sal> ...
4854 * <maplen> <mapstr> */
4855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004856 /* Sort the REP items. */
4857 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
4858 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004859
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004860 /* round 1: REP items
4861 * round 2: SAL items (unless SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004862 for (round = 1; round <= 2; ++round)
4863 {
4864 if (round == 1)
4865 gap = &spin->si_rep;
4866 else
4867 {
4868 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004869
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004870 i = 0;
4871 if (spin->si_followup)
4872 i |= SAL_F0LLOWUP;
4873 if (spin->si_collapse)
4874 i |= SAL_COLLAPSE;
4875 if (spin->si_rem_accents)
4876 i |= SAL_REM_ACCENTS;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004877 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4878 i |= SAL_SOFO;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004879 putc(i, fd); /* <salflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004880 if (i & SAL_SOFO)
4881 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004882 }
4883
4884 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
4885 for (i = 0; i < gap->ga_len; ++i)
4886 {
4887 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
4888 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
4889 ftp = &((fromto_T *)gap->ga_data)[i];
4890 for (rr = 1; rr <= 2; ++rr)
4891 {
4892 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
4893 l = STRLEN(p);
4894 putc(l, fd);
4895 fwrite(p, l, (size_t)1, fd);
4896 }
4897 }
4898 }
4899
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004900 /* SOFOFROM and SOFOTO */
4901 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4902 {
4903 put_bytes(fd, 1L, 2); /* <salcount> */
4904
4905 l = STRLEN(spin->si_sofofr);
4906 put_bytes(fd, (long_u)l, 2); /* <salfromlen> */
4907 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <salfrom> */
4908
4909 l = STRLEN(spin->si_sofoto);
4910 put_bytes(fd, (long_u)l, 2); /* <saltolen> */
4911 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <salto> */
4912 }
4913
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004914 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
4915 if (spin->si_map.ga_len > 0) /* <mapstr> */
4916 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
4917 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00004918
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004919 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004920 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004921 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004922 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004923 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004924 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004925 if (round == 1)
4926 tree = spin->si_foldroot;
4927 else if (round == 2)
4928 tree = spin->si_keeproot;
4929 else
4930 tree = spin->si_prefroot;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004931
Bram Moolenaar0c405862005-06-22 22:26:26 +00004932 /* Clear the index and wnode fields in the tree. */
4933 clear_node(tree);
4934
Bram Moolenaar51485f02005-06-04 21:55:20 +00004935 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00004936 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00004937 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004938 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004939
Bram Moolenaar51485f02005-06-04 21:55:20 +00004940 /* number of nodes in 4 bytes */
4941 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004942 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004943
Bram Moolenaar51485f02005-06-04 21:55:20 +00004944 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004945 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004946 }
4947
Bram Moolenaar51485f02005-06-04 21:55:20 +00004948 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004949}
4950
4951/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004952 * Clear the index and wnode fields of "node", it siblings and its
4953 * children. This is needed because they are a union with other items to save
4954 * space.
4955 */
4956 static void
4957clear_node(node)
4958 wordnode_T *node;
4959{
4960 wordnode_T *np;
4961
4962 if (node != NULL)
4963 for (np = node; np != NULL; np = np->wn_sibling)
4964 {
4965 np->wn_u1.index = 0;
4966 np->wn_u2.wnode = NULL;
4967
4968 if (np->wn_byte != NUL)
4969 clear_node(np->wn_child);
4970 }
4971}
4972
4973
4974/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004975 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004976 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00004977 * This first writes the list of possible bytes (siblings). Then for each
4978 * byte recursively write the children.
4979 *
4980 * NOTE: The code here must match the code in read_tree(), since assumptions
4981 * are made about the indexes (so that we don't have to write them in the
4982 * file).
4983 *
4984 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004985 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004986 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00004987put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004988 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004989 wordnode_T *node;
4990 int index;
4991 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004992 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004993{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004994 int newindex = index;
4995 int siblingcount = 0;
4996 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004997 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004998
Bram Moolenaar51485f02005-06-04 21:55:20 +00004999 /* If "node" is zero the tree is empty. */
5000 if (node == NULL)
5001 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005002
Bram Moolenaar51485f02005-06-04 21:55:20 +00005003 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005004 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005005
5006 /* Count the number of siblings. */
5007 for (np = node; np != NULL; np = np->wn_sibling)
5008 ++siblingcount;
5009
5010 /* Write the sibling count. */
5011 if (fd != NULL)
5012 putc(siblingcount, fd); /* <siblingcount> */
5013
5014 /* Write each sibling byte and optionally extra info. */
5015 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005016 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005017 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005018 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005019 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005020 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005021 /* For a NUL byte (end of word) write the flags etc. */
5022 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005023 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005024 /* In PREFIXTREE write the required prefixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005025 * associated condition nr (stored in wn_region). The
5026 * byte value is misused to store the "rare" and "not
5027 * combining" flags */
5028 if (np->wn_flags == (short_u)PFX_FLAGS_R)
5029 putc(BY_FLAGS, fd); /* <byte> */
5030 else if (np->wn_flags == (short_u)PFX_FLAGS_NC)
5031 putc(BY_PFX_NC, fd);
5032 else if (np->wn_flags == (short_u)PFX_FLAGS_RNC)
5033 putc(BY_PFX_RNC, fd);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005034 else
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005035 putc(BY_NOFLAGS, fd);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005036 putc(np->wn_prefixID, fd); /* <prefixID> */
5037 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005038 }
5039 else
5040 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005041 /* For word trees we write the flag/region items. */
5042 flags = np->wn_flags;
5043 if (regionmask != 0 && np->wn_region != regionmask)
5044 flags |= WF_REGION;
5045 if (np->wn_prefixID != 0)
5046 flags |= WF_PFX;
5047 if (flags == 0)
5048 {
5049 /* word without flags or region */
5050 putc(BY_NOFLAGS, fd); /* <byte> */
5051 }
5052 else
5053 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005054 if (np->wn_flags >= 0x100)
5055 {
5056 putc(BY_FLAGS2, fd); /* <byte> */
5057 putc(flags, fd); /* <flags> */
5058 putc((unsigned)flags >> 8, fd); /* <flags2> */
5059 }
5060 else
5061 {
5062 putc(BY_FLAGS, fd); /* <byte> */
5063 putc(flags, fd); /* <flags> */
5064 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005065 if (flags & WF_REGION)
5066 putc(np->wn_region, fd); /* <region> */
5067 if (flags & WF_PFX)
5068 putc(np->wn_prefixID, fd); /* <prefixID> */
5069 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005070 }
5071 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005072 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005073 else
5074 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005075 if (np->wn_child->wn_u1.index != 0
5076 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005077 {
5078 /* The child is written elsewhere, write the reference. */
5079 if (fd != NULL)
5080 {
5081 putc(BY_INDEX, fd); /* <byte> */
5082 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005083 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005084 }
5085 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005086 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005087 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005088 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005089
Bram Moolenaar51485f02005-06-04 21:55:20 +00005090 if (fd != NULL)
5091 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
5092 {
5093 EMSG(_(e_write));
5094 return 0;
5095 }
5096 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005097 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005098
5099 /* Space used in the array when reading: one for each sibling and one for
5100 * the count. */
5101 newindex += siblingcount + 1;
5102
5103 /* Recursively dump the children of each sibling. */
5104 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005105 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5106 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005107 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005108
5109 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005110}
5111
5112
5113/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00005114 * ":mkspell [-ascii] outfile infile ..."
5115 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005116 */
5117 void
5118ex_mkspell(eap)
5119 exarg_T *eap;
5120{
5121 int fcount;
5122 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005123 char_u *arg = eap->arg;
5124 int ascii = FALSE;
5125
5126 if (STRNCMP(arg, "-ascii", 6) == 0)
5127 {
5128 ascii = TRUE;
5129 arg = skipwhite(arg + 6);
5130 }
5131
5132 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
5133 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
5134 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005135 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005136 FreeWild(fcount, fnames);
5137 }
5138}
5139
5140/*
5141 * Create a Vim spell file from one or more word lists.
5142 * "fnames[0]" is the output file name.
5143 * "fnames[fcount - 1]" is the last input file name.
5144 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5145 * and ".spl" is appended to make the output file name.
5146 */
5147 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005148mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005149 int fcount;
5150 char_u **fnames;
5151 int ascii; /* -ascii argument given */
5152 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005153 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005154{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005155 char_u fname[MAXPATHL];
5156 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00005157 char_u **innames;
5158 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005159 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005160 int i;
5161 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005162 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005163 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005164 spellinfo_T spin;
5165
5166 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005167 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005168 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005169 spin.si_followup = TRUE;
5170 spin.si_rem_accents = TRUE;
5171 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5172 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5173 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005174 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005175
Bram Moolenaarb765d632005-06-07 21:00:02 +00005176 /* default: fnames[0] is output file, following are input files */
5177 innames = &fnames[1];
5178 incount = fcount - 1;
5179
5180 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005181 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005182 len = STRLEN(fnames[0]);
5183 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5184 {
5185 /* For ":mkspell path/en.latin1.add" output file is
5186 * "path/en.latin1.add.spl". */
5187 innames = &fnames[0];
5188 incount = 1;
5189 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
5190 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005191 else if (fcount == 1)
5192 {
5193 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
5194 innames = &fnames[0];
5195 incount = 1;
5196 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5197 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5198 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005199 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5200 {
5201 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005202 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005203 }
5204 else
5205 /* Name should be language, make the file name from it. */
5206 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5207 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5208
5209 /* Check for .ascii.spl. */
5210 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
5211 spin.si_ascii = TRUE;
5212
5213 /* Check for .add.spl. */
5214 if (strstr((char *)gettail(wfname), ".add.") != NULL)
5215 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005216 }
5217
Bram Moolenaarb765d632005-06-07 21:00:02 +00005218 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005219 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005220 else if (vim_strchr(gettail(wfname), '_') != NULL)
5221 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005222 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005223 EMSG(_("E754: Only up to 8 regions supported"));
5224 else
5225 {
5226 /* Check for overwriting before doing things that may take a lot of
5227 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005228 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005229 {
5230 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005231 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005232 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005233 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005234 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005235 EMSG2(_(e_isadir2), wfname);
5236 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005237 }
5238
5239 /*
5240 * Init the aff and dic pointers.
5241 * Get the region names if there are more than 2 arguments.
5242 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005243 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005244 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005245 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005246
Bram Moolenaar3982c542005-06-08 21:56:31 +00005247 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005248 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005249 len = STRLEN(innames[i]);
5250 if (STRLEN(gettail(innames[i])) < 5
5251 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005252 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005253 EMSG2(_("E755: Invalid region in %s"), innames[i]);
5254 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005255 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005256 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5257 spin.si_region_name[i * 2 + 1] =
5258 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005259 }
5260 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005261 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005262
Bram Moolenaar51485f02005-06-04 21:55:20 +00005263 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
5264 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005265 spin.si_prefroot = wordtree_alloc(&spin.si_blocks);
5266 if (spin.si_foldroot == NULL
5267 || spin.si_keeproot == NULL
5268 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005269 {
5270 error = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005271 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005272 }
5273
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005274 /* When not producing a .add.spl file clear the character table when
5275 * we encounter one in the .aff file. This means we dump the current
5276 * one in the .spl file if the .aff file doesn't define one. That's
5277 * better than guessing the contents, the table will match a
5278 * previously loaded spell file. */
5279 if (!spin.si_add)
5280 spin.si_clear_chartab = TRUE;
5281
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005282 /*
5283 * Read all the .aff and .dic files.
5284 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005285 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005286 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005287 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005288 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005289 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005290 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005291
Bram Moolenaarb765d632005-06-07 21:00:02 +00005292 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005293 if (mch_stat((char *)fname, &st) >= 0)
5294 {
5295 /* Read the .aff file. Will init "spin->si_conv" based on the
5296 * "SET" line. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005297 afile[i] = spell_read_aff(fname, &spin);
5298 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005299 error = TRUE;
5300 else
5301 {
5302 /* Read the .dic file and store the words in the trees. */
5303 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00005304 innames[i]);
5305 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005306 error = TRUE;
5307 }
5308 }
5309 else
5310 {
5311 /* No .aff file, try reading the file as a word list. Store
5312 * the words in the trees. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005313 if (spell_read_wordfile(innames[i], &spin) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005314 error = TRUE;
5315 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005316
Bram Moolenaarb765d632005-06-07 21:00:02 +00005317#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005318 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005319 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005320#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005321 }
5322
Bram Moolenaar51485f02005-06-04 21:55:20 +00005323 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005324 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005325 /*
5326 * Remove the dummy NUL from the start of the tree root.
5327 */
5328 spin.si_foldroot = spin.si_foldroot->wn_sibling;
5329 spin.si_keeproot = spin.si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005330 spin.si_prefroot = spin.si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005331
5332 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005333 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005334 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005337 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005338 verbose_enter();
5339 MSG(_("Compressing word tree..."));
5340 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005341 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005342 verbose_leave();
5343 }
5344 wordtree_compress(spin.si_foldroot, &spin);
5345 wordtree_compress(spin.si_keeproot, &spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005346 wordtree_compress(spin.si_prefroot, &spin);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005347 }
5348
Bram Moolenaar51485f02005-06-04 21:55:20 +00005349 if (!error)
5350 {
5351 /*
5352 * Write the info in the spell file.
5353 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005354 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005356 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005357 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005358 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005359 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005360 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005361 verbose_leave();
5362 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00005363
Bram Moolenaar3982c542005-06-08 21:56:31 +00005364 write_vim_spell(wfname, &spin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005365
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005366 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005368 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005369 verbose_enter();
5370 MSG(_("Done!"));
5371 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00005372 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005373 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005374 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005375 verbose_leave();
5376 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005377
Bram Moolenaarb765d632005-06-07 21:00:02 +00005378 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005379 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005380 }
5381
5382 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005383 ga_clear(&spin.si_rep);
5384 ga_clear(&spin.si_sal);
5385 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005386 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005387 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005388 vim_free(spin.si_sofofr);
5389 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005390
5391 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005392 for (i = 0; i < incount; ++i)
5393 if (afile[i] != NULL)
5394 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005395
5396 /* Free all the bits and pieces at once. */
5397 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005398 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005399}
5400
Bram Moolenaarb765d632005-06-07 21:00:02 +00005401
5402/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005403 * ":[count]spellgood {word}"
5404 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00005405 */
5406 void
5407ex_spell(eap)
5408 exarg_T *eap;
5409{
Bram Moolenaar7887d882005-07-01 22:33:52 +00005410 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005411 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005412}
5413
5414/*
5415 * Add "word[len]" to 'spellfile' as a good or bad word.
5416 */
5417 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005418spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005419 char_u *word;
5420 int len;
5421 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005422 int index; /* "zG" and "zW": zero, otherwise index in
5423 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005424{
5425 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005426 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005427 int new_spf = FALSE;
5428 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005429 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005430 char_u fnamebuf[MAXPATHL];
5431 char_u line[MAXWLEN * 2];
5432 long fpos, fpos_next = 0;
5433 int i;
5434 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005435
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005436 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005437 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005438 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005439 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005440 int_wordlist = vim_tempname('s');
5441 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005442 return;
5443 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005444 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005445 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005446 else
5447 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005448 /* If 'spellfile' isn't set figure out a good default value. */
5449 if (*curbuf->b_p_spf == NUL)
5450 {
5451 init_spellfile();
5452 new_spf = TRUE;
5453 }
5454
5455 if (*curbuf->b_p_spf == NUL)
5456 {
5457 EMSG(_("E764: 'spellfile' is not set"));
5458 return;
5459 }
5460
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005461 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
5462 {
5463 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
5464 if (i == index)
5465 break;
5466 if (*spf == NUL)
5467 {
5468 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
5469 return;
5470 }
5471 }
5472
Bram Moolenaarb765d632005-06-07 21:00:02 +00005473 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005474 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005475 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
5476 buf = NULL;
5477 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00005478 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005479 EMSG(_(e_bufloaded));
5480 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005481 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005482
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005483 fname = fnamebuf;
5484 }
5485
5486 if (bad)
5487 {
5488 /* When the word also appears as good word we need to remove that one,
5489 * since its flags sort before the one with WF_BANNED. */
5490 fd = mch_fopen((char *)fname, "r");
5491 if (fd != NULL)
5492 {
5493 while (!vim_fgets(line, MAXWLEN * 2, fd))
5494 {
5495 fpos = fpos_next;
5496 fpos_next = ftell(fd);
5497 if (STRNCMP(word, line, len) == 0
5498 && (line[len] == '/' || line[len] < ' '))
5499 {
5500 /* Found duplicate word. Remove it by writing a '#' at
5501 * the start of the line. Mixing reading and writing
5502 * doesn't work for all systems, close the file first. */
5503 fclose(fd);
5504 fd = mch_fopen((char *)fname, "r+");
5505 if (fd == NULL)
5506 break;
5507 if (fseek(fd, fpos, SEEK_SET) == 0)
5508 fputc('#', fd);
5509 fseek(fd, fpos_next, SEEK_SET);
5510 }
5511 }
5512 fclose(fd);
5513 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005514 }
5515
5516 fd = mch_fopen((char *)fname, "a");
5517 if (fd == NULL && new_spf)
5518 {
5519 /* We just initialized the 'spellfile' option and can't open the file.
5520 * We may need to create the "spell" directory first. We already
5521 * checked the runtime directory is writable in init_spellfile(). */
5522 STRCPY(NameBuff, fname);
5523 *gettail_sep(NameBuff) = NUL;
5524 if (mch_stat((char *)NameBuff, &st) < 0)
5525 {
5526 /* The directory doesn't exist. Try creating it and opening the
5527 * file again. */
5528 vim_mkdir(NameBuff, 0755);
5529 fd = mch_fopen((char *)fname, "a");
5530 }
5531 }
5532
5533 if (fd == NULL)
5534 EMSG2(_(e_notopen), fname);
5535 else
5536 {
5537 if (bad)
5538 fprintf(fd, "%.*s/!\n", len, word);
5539 else
5540 fprintf(fd, "%.*s\n", len, word);
5541 fclose(fd);
5542
5543 /* Update the .add.spl file. */
5544 mkspell(1, &fname, FALSE, TRUE, TRUE);
5545
5546 /* If the .add file is edited somewhere, reload it. */
5547 if (buf != NULL)
5548 buf_reload(buf);
5549
5550 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005551 }
5552}
5553
5554/*
5555 * Initialize 'spellfile' for the current buffer.
5556 */
5557 static void
5558init_spellfile()
5559{
5560 char_u buf[MAXPATHL];
5561 int l;
5562 slang_T *sl;
5563 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005564 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005565
5566 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
5567 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005568 /* Find the end of the language name. Exclude the region. */
5569 for (lend = curbuf->b_p_spl; *lend != NUL
5570 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
5571 ;
5572
5573 /* Loop over all entries in 'runtimepath'. Use the first one where we
5574 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005575 rtp = p_rtp;
5576 while (*rtp != NUL)
5577 {
5578 /* Copy the path from 'runtimepath' to buf[]. */
5579 copy_option_part(&rtp, buf, MAXPATHL, ",");
5580 if (filewritable(buf) == 2)
5581 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00005582 /* Use the first language name from 'spelllang' and the
5583 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005584 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
5585 l = STRLEN(buf);
5586 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00005587 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005588 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00005589 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
5590 ? (char_u *)"ascii" : spell_enc());
5591 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
5592 break;
5593 }
5594 }
5595 }
5596}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005597
Bram Moolenaar51485f02005-06-04 21:55:20 +00005598
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005599/*
5600 * Init the chartab used for spelling for ASCII.
5601 * EBCDIC is not supported!
5602 */
5603 static void
5604clear_spell_chartab(sp)
5605 spelltab_T *sp;
5606{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005607 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005608
5609 /* Init everything to FALSE. */
5610 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
5611 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
5612 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005613 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005614 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005615 sp->st_upper[i] = i;
5616 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005617
5618 /* We include digits. A word shouldn't start with a digit, but handling
5619 * that is done separately. */
5620 for (i = '0'; i <= '9'; ++i)
5621 sp->st_isw[i] = TRUE;
5622 for (i = 'A'; i <= 'Z'; ++i)
5623 {
5624 sp->st_isw[i] = TRUE;
5625 sp->st_isu[i] = TRUE;
5626 sp->st_fold[i] = i + 0x20;
5627 }
5628 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005629 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005630 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005631 sp->st_upper[i] = i - 0x20;
5632 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005633}
5634
5635/*
5636 * Init the chartab used for spelling. Only depends on 'encoding'.
5637 * Called once while starting up and when 'encoding' changes.
5638 * The default is to use isalpha(), but the spell file should define the word
5639 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005640 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005641 */
5642 void
5643init_spell_chartab()
5644{
5645 int i;
5646
5647 did_set_spelltab = FALSE;
5648 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005649#ifdef FEAT_MBYTE
5650 if (enc_dbcs)
5651 {
5652 /* DBCS: assume double-wide characters are word characters. */
5653 for (i = 128; i <= 255; ++i)
5654 if (MB_BYTE2LEN(i) == 2)
5655 spelltab.st_isw[i] = TRUE;
5656 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005657 else if (enc_utf8)
5658 {
5659 for (i = 128; i < 256; ++i)
5660 {
5661 spelltab.st_isu[i] = utf_isupper(i);
5662 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
5663 spelltab.st_fold[i] = utf_fold(i);
5664 spelltab.st_upper[i] = utf_toupper(i);
5665 }
5666 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005667 else
5668#endif
5669 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005670 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005671 for (i = 128; i < 256; ++i)
5672 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005673 if (MB_ISUPPER(i))
5674 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005675 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005676 spelltab.st_isu[i] = TRUE;
5677 spelltab.st_fold[i] = MB_TOLOWER(i);
5678 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005679 else if (MB_ISLOWER(i))
5680 {
5681 spelltab.st_isw[i] = TRUE;
5682 spelltab.st_upper[i] = MB_TOUPPER(i);
5683 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005684 }
5685 }
5686}
5687
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005688static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
5689static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
5690
5691/*
5692 * Set the spell character tables from strings in the affix file.
5693 */
5694 static int
5695set_spell_chartab(fol, low, upp)
5696 char_u *fol;
5697 char_u *low;
5698 char_u *upp;
5699{
5700 /* We build the new tables here first, so that we can compare with the
5701 * previous one. */
5702 spelltab_T new_st;
5703 char_u *pf = fol, *pl = low, *pu = upp;
5704 int f, l, u;
5705
5706 clear_spell_chartab(&new_st);
5707
5708 while (*pf != NUL)
5709 {
5710 if (*pl == NUL || *pu == NUL)
5711 {
5712 EMSG(_(e_affform));
5713 return FAIL;
5714 }
5715#ifdef FEAT_MBYTE
5716 f = mb_ptr2char_adv(&pf);
5717 l = mb_ptr2char_adv(&pl);
5718 u = mb_ptr2char_adv(&pu);
5719#else
5720 f = *pf++;
5721 l = *pl++;
5722 u = *pu++;
5723#endif
5724 /* Every character that appears is a word character. */
5725 if (f < 256)
5726 new_st.st_isw[f] = TRUE;
5727 if (l < 256)
5728 new_st.st_isw[l] = TRUE;
5729 if (u < 256)
5730 new_st.st_isw[u] = TRUE;
5731
5732 /* if "LOW" and "FOL" are not the same the "LOW" char needs
5733 * case-folding */
5734 if (l < 256 && l != f)
5735 {
5736 if (f >= 256)
5737 {
5738 EMSG(_(e_affrange));
5739 return FAIL;
5740 }
5741 new_st.st_fold[l] = f;
5742 }
5743
5744 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005745 * case-folding, it's upper case and the "UPP" is the upper case of
5746 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005747 if (u < 256 && u != f)
5748 {
5749 if (f >= 256)
5750 {
5751 EMSG(_(e_affrange));
5752 return FAIL;
5753 }
5754 new_st.st_fold[u] = f;
5755 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005756 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005757 }
5758 }
5759
5760 if (*pl != NUL || *pu != NUL)
5761 {
5762 EMSG(_(e_affform));
5763 return FAIL;
5764 }
5765
5766 return set_spell_finish(&new_st);
5767}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005768
5769/*
5770 * Set the spell character tables from strings in the .spl file.
5771 */
5772 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005773set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005774 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005775 int cnt; /* length of "flags" */
5776 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005777{
5778 /* We build the new tables here first, so that we can compare with the
5779 * previous one. */
5780 spelltab_T new_st;
5781 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005782 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005783 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005784
5785 clear_spell_chartab(&new_st);
5786
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005787 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005788 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005789 if (i < cnt)
5790 {
5791 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
5792 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
5793 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005794
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005795 if (*p != NUL)
5796 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005797#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005798 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005799#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005800 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005801#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005802 new_st.st_fold[i + 128] = c;
5803 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
5804 new_st.st_upper[c] = i + 128;
5805 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005806 }
5807
5808 return set_spell_finish(&new_st);
5809}
5810
5811 static int
5812set_spell_finish(new_st)
5813 spelltab_T *new_st;
5814{
5815 int i;
5816
5817 if (did_set_spelltab)
5818 {
5819 /* check that it's the same table */
5820 for (i = 0; i < 256; ++i)
5821 {
5822 if (spelltab.st_isw[i] != new_st->st_isw[i]
5823 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005824 || spelltab.st_fold[i] != new_st->st_fold[i]
5825 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005826 {
5827 EMSG(_("E763: Word characters differ between spell files"));
5828 return FAIL;
5829 }
5830 }
5831 }
5832 else
5833 {
5834 /* copy the new spelltab into the one being used */
5835 spelltab = *new_st;
5836 did_set_spelltab = TRUE;
5837 }
5838
5839 return OK;
5840}
5841
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005842/*
Bram Moolenaarea408852005-06-25 22:49:46 +00005843 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005844 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00005845 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005846 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00005847 */
5848 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005849spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00005850 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005851 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00005852{
Bram Moolenaarea408852005-06-25 22:49:46 +00005853#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005854 char_u *s;
5855 int l;
5856 int c;
5857
5858 if (has_mbyte)
5859 {
5860 l = MB_BYTE2LEN(*p);
5861 s = p;
5862 if (l == 1)
5863 {
5864 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005865 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005866 {
5867 s = p + 1; /* skip a mid-word character */
5868 l = MB_BYTE2LEN(*s);
5869 }
5870 }
5871 else
5872 {
5873 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005874 if (c < 256 ? buf->b_spell_ismw[c]
5875 : (buf->b_spell_ismw_mb != NULL
5876 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005877 {
5878 s = p + l;
5879 l = MB_BYTE2LEN(*s);
5880 }
5881 }
5882
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005883 c = mb_ptr2char(s);
5884 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005885 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005886 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005887 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005888#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005889
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005890 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
5891}
5892
5893/*
5894 * Return TRUE if "p" points to a word character.
5895 * Unlike spell_iswordp() this doesn't check for "midword" characters.
5896 */
5897 static int
5898spell_iswordp_nmw(p)
5899 char_u *p;
5900{
5901#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005902 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005903
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005904 if (has_mbyte)
5905 {
5906 c = mb_ptr2char(p);
5907 if (c > 255)
5908 return mb_get_class(p) >= 2;
5909 return spelltab.st_isw[c];
5910 }
5911#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005912 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00005913}
5914
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005915#ifdef FEAT_MBYTE
5916/*
5917 * Return TRUE if "p" points to a word character.
5918 * Wide version of spell_iswordp().
5919 */
5920 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005921spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005922 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005923 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005924{
5925 int *s;
5926
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005927 if (*p < 256 ? buf->b_spell_ismw[*p]
5928 : (buf->b_spell_ismw_mb != NULL
5929 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005930 s = p + 1;
5931 else
5932 s = p;
5933
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005934 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005935 {
5936 if (enc_utf8)
5937 return utf_class(*s) >= 2;
5938 if (enc_dbcs)
5939 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
5940 return 0;
5941 }
5942 return spelltab.st_isw[*s];
5943}
5944#endif
5945
Bram Moolenaarea408852005-06-25 22:49:46 +00005946/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005947 * Write the table with prefix conditions to the .spl file.
5948 */
5949 static void
5950write_spell_prefcond(fd, gap)
5951 FILE *fd;
5952 garray_T *gap;
5953{
5954 int i;
5955 char_u *p;
5956 int len;
5957
5958 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
5959
5960 for (i = 0; i < gap->ga_len; ++i)
5961 {
5962 /* <prefcond> : <condlen> <condstr> */
5963 p = ((char_u **)gap->ga_data)[i];
5964 if (p == NULL)
5965 fputc(0, fd);
5966 else
5967 {
5968 len = STRLEN(p);
5969 fputc(len, fd);
5970 fwrite(p, (size_t)len, (size_t)1, fd);
5971 }
5972 }
5973}
5974
5975/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005976 * Write the current tables into the .spl file.
5977 * This makes sure the same characters are recognized as word characters when
5978 * generating an when using a spell file.
5979 */
5980 static void
5981write_spell_chartab(fd)
5982 FILE *fd;
5983{
5984 char_u charbuf[256 * 4];
5985 int len = 0;
5986 int flags;
5987 int i;
5988
5989 fputc(128, fd); /* <charflagslen> */
5990 for (i = 128; i < 256; ++i)
5991 {
5992 flags = 0;
5993 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005994 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005995 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005996 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005997 fputc(flags, fd); /* <charflags> */
5998
Bram Moolenaarb765d632005-06-07 21:00:02 +00005999#ifdef FEAT_MBYTE
6000 if (has_mbyte)
6001 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
6002 else
6003#endif
6004 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006005 }
6006
6007 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
6008 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
6009}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006010
6011/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006012 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
6013 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006014 * When using a multi-byte 'encoding' the length may change!
6015 * Returns FAIL when something wrong.
6016 */
6017 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006018spell_casefold(str, len, buf, buflen)
6019 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006020 int len;
6021 char_u *buf;
6022 int buflen;
6023{
6024 int i;
6025
6026 if (len >= buflen)
6027 {
6028 buf[0] = NUL;
6029 return FAIL; /* result will not fit */
6030 }
6031
6032#ifdef FEAT_MBYTE
6033 if (has_mbyte)
6034 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006035 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006036 char_u *p;
6037 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006038
6039 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006040 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006041 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006042 if (outi + MB_MAXBYTES > buflen)
6043 {
6044 buf[outi] = NUL;
6045 return FAIL;
6046 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006047 c = mb_ptr2char_adv(&p);
6048 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006049 }
6050 buf[outi] = NUL;
6051 }
6052 else
6053#endif
6054 {
6055 /* Be quick for non-multibyte encodings. */
6056 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006057 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006058 buf[i] = NUL;
6059 }
6060
6061 return OK;
6062}
6063
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006064#define SPS_BEST 1
6065#define SPS_FAST 2
6066#define SPS_DOUBLE 4
6067
6068static int sps_flags = SPS_BEST;
6069
6070/*
6071 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
6072 * Sets "sps_flags".
6073 */
6074 int
6075spell_check_sps()
6076{
6077 char_u *p;
6078 char_u buf[MAXPATHL];
6079 int f;
6080
6081 sps_flags = 0;
6082
6083 for (p = p_sps; *p != NUL; )
6084 {
6085 copy_option_part(&p, buf, MAXPATHL, ",");
6086
6087 f = 0;
6088 if (STRCMP(buf, "best") == 0)
6089 f = SPS_BEST;
6090 else if (STRCMP(buf, "fast") == 0)
6091 f = SPS_FAST;
6092 else if (STRCMP(buf, "double") == 0)
6093 f = SPS_DOUBLE;
6094 else if (STRNCMP(buf, "expr:", 5) != 0
6095 && STRNCMP(buf, "file:", 5) != 0)
6096 f = -1;
6097
6098 if (f == -1 || (sps_flags != 0 && f != 0))
6099 {
6100 sps_flags = SPS_BEST;
6101 return FAIL;
6102 }
6103 if (f != 0)
6104 sps_flags = f;
6105 }
6106
6107 if (sps_flags == 0)
6108 sps_flags = SPS_BEST;
6109
6110 return OK;
6111}
6112
6113/* Remember what "z?" replaced. */
6114static char_u *repl_from = NULL;
6115static char_u *repl_to = NULL;
6116
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006117/*
6118 * "z?": Find badly spelled word under or after the cursor.
6119 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006120 */
6121 void
6122spell_suggest()
6123{
6124 char_u *line;
6125 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006126 char_u wcopy[MAXWLEN + 2];
6127 char_u *p;
6128 int i;
6129 int c;
6130 suginfo_T sug;
6131 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006132 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006133 int need_cap;
6134 regmatch_T regmatch;
6135 int endcol;
6136 char_u *line_copy = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006137
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006138 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006139 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
6140 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006141 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006142 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
6143 return;
6144
6145 /* No bad word or it starts after the cursor: use the word under the
6146 * cursor. */
6147 curwin->w_cursor = prev_cursor;
6148 line = ml_get_curline();
6149 p = line + curwin->w_cursor.col;
6150 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006151 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006152 mb_ptr_back(line, p);
6153 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006154 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006155 mb_ptr_adv(p);
6156
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006157 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006158 {
6159 beep_flush();
6160 return;
6161 }
6162 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006163 }
6164
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006165 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006166 line = ml_get_curline();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006167
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006168 /* Figure out if the word should be capitalised. */
6169 need_cap = FALSE;
6170 if (curbuf->b_cap_prog != NULL)
6171 {
6172 endcol = 0;
Bram Moolenaar97409f12005-07-08 22:17:29 +00006173 if ((int)(skipwhite(line) - line) == (int)curwin->w_cursor.col)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006174 {
6175 /* At start of line, check if previous line is empty or sentence
6176 * ends there. */
6177 if (curwin->w_cursor.lnum == 1)
6178 need_cap = TRUE;
6179 else
6180 {
6181 line = ml_get(curwin->w_cursor.lnum - 1);
6182 if (*skipwhite(line) == NUL)
6183 need_cap = TRUE;
6184 else
6185 {
6186 /* Append a space in place of the line break. */
6187 line_copy = concat_str(line, (char_u *)" ");
6188 line = line_copy;
6189 endcol = STRLEN(line);
6190 }
6191 }
6192 }
6193 else
6194 endcol = curwin->w_cursor.col;
6195
6196 if (endcol > 0)
6197 {
6198 /* Check if sentence ends before the bad word. */
6199 regmatch.regprog = curbuf->b_cap_prog;
6200 regmatch.rm_ic = FALSE;
6201 p = line + endcol;
6202 for (;;)
6203 {
6204 mb_ptr_back(line, p);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006205 if (p == line || spell_iswordp_nmw(p))
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006206 break;
6207 if (vim_regexec(&regmatch, p, 0)
6208 && regmatch.endp[0] == line + endcol)
6209 {
6210 need_cap = TRUE;
6211 break;
6212 }
6213 }
6214 }
6215
6216 /* get the line again, we may have been using the previous one */
6217 line = ml_get_curline();
6218 vim_free(line_copy);
6219 }
6220
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006221 /* Get the list of suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006222 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2,
6223 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006224
6225 if (sug.su_ga.ga_len == 0)
6226 MSG(_("Sorry, no suggestions"));
6227 else
6228 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006229 vim_free(repl_from);
6230 repl_from = NULL;
6231 vim_free(repl_to);
6232 repl_to = NULL;
6233
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006234 /* List the suggestions. */
6235 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006236 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006237 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
6238 sug.su_badlen, sug.su_badptr);
6239 msg_puts(IObuff);
6240 msg_clr_eos();
6241 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00006242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006243 msg_scroll = TRUE;
6244 for (i = 0; i < sug.su_ga.ga_len; ++i)
6245 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006246 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006247
6248 /* The suggested word may replace only part of the bad word, add
6249 * the not replaced part. */
6250 STRCPY(wcopy, stp->st_word);
6251 if (sug.su_badlen > stp->st_orglen)
6252 vim_strncpy(wcopy + STRLEN(wcopy),
6253 sug.su_badptr + stp->st_orglen,
6254 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006255 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy);
6256 msg_puts(IObuff);
6257
6258 /* The word may replace more than "su_badlen". */
6259 if (sug.su_badlen < stp->st_orglen)
6260 {
6261 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
6262 stp->st_orglen, sug.su_badptr);
6263 msg_puts(IObuff);
6264 }
6265
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006266 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006267 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006268 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006269 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006270 vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"),
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006271 stp->st_salscore ? "s " : "",
6272 stp->st_score, stp->st_altscore);
6273 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00006274 vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"),
6275 stp->st_score);
6276 msg_advance(30);
6277 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006278 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006279 msg_putchar('\n');
6280 }
6281
6282 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006283 i = prompt_for_number(&mouse_used);
6284 if (mouse_used)
6285 i -= lines_left;
6286
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006287 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006288 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006289 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006290 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006291 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
6292 repl_to = vim_strsave(stp->st_word);
6293
6294 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006295 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
6296 if (p != NULL)
6297 {
6298 c = sug.su_badptr - line;
6299 mch_memmove(p, line, c);
6300 STRCPY(p + c, stp->st_word);
6301 STRCAT(p, sug.su_badptr + stp->st_orglen);
6302 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6303 curwin->w_cursor.col = c;
6304 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006305
6306 /* For redo we use a change-word command. */
6307 ResetRedobuff();
6308 AppendToRedobuff((char_u *)"ciw");
6309 AppendToRedobuff(stp->st_word);
6310 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006311 }
6312 }
6313 else
6314 curwin->w_cursor = prev_cursor;
6315 }
6316
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006317 spell_find_cleanup(&sug);
6318}
6319
6320/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006321 * ":spellrepall"
6322 */
6323/*ARGSUSED*/
6324 void
6325ex_spellrepall(eap)
6326 exarg_T *eap;
6327{
6328 pos_T pos = curwin->w_cursor;
6329 char_u *frompat;
6330 int addlen;
6331 char_u *line;
6332 char_u *p;
6333 int didone = FALSE;
6334 int save_ws = p_ws;
6335
6336 if (repl_from == NULL || repl_to == NULL)
6337 {
6338 EMSG(_("E752: No previous spell replacement"));
6339 return;
6340 }
6341 addlen = STRLEN(repl_to) - STRLEN(repl_from);
6342
6343 frompat = alloc(STRLEN(repl_from) + 7);
6344 if (frompat == NULL)
6345 return;
6346 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
6347 p_ws = FALSE;
6348
6349 curwin->w_cursor.lnum = 0;
6350 while (!got_int)
6351 {
6352 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
6353 || u_save_cursor() == FAIL)
6354 break;
6355
6356 /* Only replace when the right word isn't there yet. This happens
6357 * when changing "etc" to "etc.". */
6358 line = ml_get_curline();
6359 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
6360 repl_to, STRLEN(repl_to)) != 0)
6361 {
6362 p = alloc(STRLEN(line) + addlen + 1);
6363 if (p == NULL)
6364 break;
6365 mch_memmove(p, line, curwin->w_cursor.col);
6366 STRCPY(p + curwin->w_cursor.col, repl_to);
6367 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
6368 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6369 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6370 didone = TRUE;
6371 }
6372 curwin->w_cursor.col += STRLEN(repl_to);
6373 }
6374
6375 p_ws = save_ws;
6376 curwin->w_cursor = pos;
6377 vim_free(frompat);
6378
6379 if (!didone)
6380 EMSG2(_("E753: Not found: %s"), repl_from);
6381}
6382
6383/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006384 * Find spell suggestions for "word". Return them in the growarray "*gap" as
6385 * a list of allocated strings.
6386 */
6387 void
6388spell_suggest_list(gap, word, maxcount)
6389 garray_T *gap;
6390 char_u *word;
6391 int maxcount; /* maximum nr of suggestions */
6392{
6393 suginfo_T sug;
6394 int i;
6395 suggest_T *stp;
6396 char_u *wcopy;
6397
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006398 spell_find_suggest(word, &sug, maxcount, FALSE, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006399
6400 /* Make room in "gap". */
6401 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
6402 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
6403 return;
6404
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006405 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006406 {
6407 stp = &SUG(sug.su_ga, i);
6408
6409 /* The suggested word may replace only part of "word", add the not
6410 * replaced part. */
6411 wcopy = alloc(STRLEN(stp->st_word)
6412 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
6413 if (wcopy == NULL)
6414 break;
6415 STRCPY(wcopy, stp->st_word);
6416 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
6417 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
6418 }
6419
6420 spell_find_cleanup(&sug);
6421}
6422
6423/*
6424 * Find spell suggestions for the word at the start of "badptr".
6425 * Return the suggestions in "su->su_ga".
6426 * The maximum number of suggestions is "maxcount".
6427 * Note: does use info for the current window.
6428 * This is based on the mechanisms of Aspell, but completely reimplemented.
6429 */
6430 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006431spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006432 char_u *badptr;
6433 suginfo_T *su;
6434 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00006435 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006436 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006437{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006438 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006439 char_u buf[MAXPATHL];
6440 char_u *p;
6441 int do_combine = FALSE;
6442 char_u *sps_copy;
6443#ifdef FEAT_EVAL
6444 static int expr_busy = FALSE;
6445#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006446 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006447
6448 /*
6449 * Set the info in "*su".
6450 */
6451 vim_memset(su, 0, sizeof(suginfo_T));
6452 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
6453 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006454 if (*badptr == NUL)
6455 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006456 hash_init(&su->su_banned);
6457
6458 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006459 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006460 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006461 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006462
6463 if (su->su_badlen >= MAXWLEN)
6464 su->su_badlen = MAXWLEN - 1; /* just in case */
6465 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
6466 (void)spell_casefold(su->su_badptr, su->su_badlen,
6467 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006468 /* get caps flags for bad word */
6469 su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006470 if (need_cap)
6471 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006472
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006473 /* If the word is not capitalised and spell_check() doesn't consider the
6474 * word to be bad then it might need to be capitalised. Add a suggestion
6475 * for that. */
6476#ifdef FEAT_MBYTE
6477 c = mb_ptr2char(su->su_badptr);
6478#else
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006479 c = *su->su_badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006480#endif
6481 if (!SPELL_ISUPPER(c) && attr == 0)
6482 {
6483 make_case_word(su->su_badword, buf, WF_ONECAP);
6484 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
6485 0, TRUE);
6486 }
6487
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006488 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00006489 if (banbadword)
6490 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006491
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006492 /* Make a copy of 'spellsuggest', because the expression may change it. */
6493 sps_copy = vim_strsave(p_sps);
6494 if (sps_copy == NULL)
6495 return;
6496
6497 /* Loop over the items in 'spellsuggest'. */
6498 for (p = sps_copy; *p != NUL; )
6499 {
6500 copy_option_part(&p, buf, MAXPATHL, ",");
6501
6502 if (STRNCMP(buf, "expr:", 5) == 0)
6503 {
6504#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006505 /* Evaluate an expression. Skip this when called recursively,
6506 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006507 if (!expr_busy)
6508 {
6509 expr_busy = TRUE;
6510 spell_suggest_expr(su, buf + 5);
6511 expr_busy = FALSE;
6512 }
6513#endif
6514 }
6515 else if (STRNCMP(buf, "file:", 5) == 0)
6516 /* Use list of suggestions in a file. */
6517 spell_suggest_file(su, buf + 5);
6518 else
6519 {
6520 /* Use internal method. */
6521 spell_suggest_intern(su);
6522 if (sps_flags & SPS_DOUBLE)
6523 do_combine = TRUE;
6524 }
6525 }
6526
6527 vim_free(sps_copy);
6528
6529 if (do_combine)
6530 /* Combine the two list of suggestions. This must be done last,
6531 * because sorting changes the order again. */
6532 score_combine(su);
6533}
6534
6535#ifdef FEAT_EVAL
6536/*
6537 * Find suggestions by evaluating expression "expr".
6538 */
6539 static void
6540spell_suggest_expr(su, expr)
6541 suginfo_T *su;
6542 char_u *expr;
6543{
6544 list_T *list;
6545 listitem_T *li;
6546 int score;
6547 char_u *p;
6548
6549 /* The work is split up in a few parts to avoid having to export
6550 * suginfo_T.
6551 * First evaluate the expression and get the resulting list. */
6552 list = eval_spell_expr(su->su_badword, expr);
6553 if (list != NULL)
6554 {
6555 /* Loop over the items in the list. */
6556 for (li = list->lv_first; li != NULL; li = li->li_next)
6557 if (li->li_tv.v_type == VAR_LIST)
6558 {
6559 /* Get the word and the score from the items. */
6560 score = get_spellword(li->li_tv.vval.v_list, &p);
6561 if (score >= 0)
6562 add_suggestion(su, &su->su_ga, p,
6563 su->su_badlen, score, 0, TRUE);
6564 }
6565 list_unref(list);
6566 }
6567
6568 /* Sort the suggestions and truncate at "maxcount". */
6569 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6570}
6571#endif
6572
6573/*
6574 * Find suggestions a file "fname".
6575 */
6576 static void
6577spell_suggest_file(su, fname)
6578 suginfo_T *su;
6579 char_u *fname;
6580{
6581 FILE *fd;
6582 char_u line[MAXWLEN * 2];
6583 char_u *p;
6584 int len;
6585 char_u cword[MAXWLEN];
6586
6587 /* Open the file. */
6588 fd = mch_fopen((char *)fname, "r");
6589 if (fd == NULL)
6590 {
6591 EMSG2(_(e_notopen), fname);
6592 return;
6593 }
6594
6595 /* Read it line by line. */
6596 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
6597 {
6598 line_breakcheck();
6599
6600 p = vim_strchr(line, '/');
6601 if (p == NULL)
6602 continue; /* No Tab found, just skip the line. */
6603 *p++ = NUL;
6604 if (STRICMP(su->su_badword, line) == 0)
6605 {
6606 /* Match! Isolate the good word, until CR or NL. */
6607 for (len = 0; p[len] >= ' '; ++len)
6608 ;
6609 p[len] = NUL;
6610
6611 /* If the suggestion doesn't have specific case duplicate the case
6612 * of the bad word. */
6613 if (captype(p, NULL) == 0)
6614 {
6615 make_case_word(p, cword, su->su_badflags);
6616 p = cword;
6617 }
6618
6619 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6620 SCORE_FILE, 0, TRUE);
6621 }
6622 }
6623
6624 fclose(fd);
6625
6626 /* Sort the suggestions and truncate at "maxcount". */
6627 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6628}
6629
6630/*
6631 * Find suggestions for the internal method indicated by "sps_flags".
6632 */
6633 static void
6634spell_suggest_intern(su)
6635 suginfo_T *su;
6636{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006637 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006638 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006639 *
6640 * Set a maximum score to limit the combination of operations that is
6641 * tried.
6642 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006643 suggest_try_special(su);
6644
6645 /*
6646 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
6647 * from the .aff file and inserting a space (split the word).
6648 */
6649 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006650
6651 /* For the resulting top-scorers compute the sound-a-like score. */
6652 if (sps_flags & SPS_DOUBLE)
6653 score_comp_sal(su);
6654
6655 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006656 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006657 *
6658 * Only do this when we don't have a lot of suggestions yet, because it's
6659 * very slow and often doesn't find new suggestions.
6660 */
6661 if ((sps_flags & SPS_DOUBLE)
6662 || (!(sps_flags & SPS_FAST)
6663 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
6664 {
6665 /* Allow a higher score now. */
6666 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006667 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006668 }
6669
6670 /* When CTRL-C was hit while searching do show the results. */
6671 ui_breakcheck();
6672 if (got_int)
6673 {
6674 (void)vgetc();
6675 got_int = FALSE;
6676 }
6677
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006678 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006679 {
6680 if (sps_flags & SPS_BEST)
6681 /* Adjust the word score for how it sounds like. */
6682 rescore_suggestions(su);
6683
6684 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006685 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006686 }
6687}
6688
6689/*
6690 * Free the info put in "*su" by spell_find_suggest().
6691 */
6692 static void
6693spell_find_cleanup(su)
6694 suginfo_T *su;
6695{
6696 int i;
6697
6698 /* Free the suggestions. */
6699 for (i = 0; i < su->su_ga.ga_len; ++i)
6700 vim_free(SUG(su->su_ga, i).st_word);
6701 ga_clear(&su->su_ga);
6702 for (i = 0; i < su->su_sga.ga_len; ++i)
6703 vim_free(SUG(su->su_sga, i).st_word);
6704 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006705
6706 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006707 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006708}
6709
6710/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006711 * Make a copy of "word", with the first letter upper or lower cased, to
6712 * "wcopy[MAXWLEN]". "word" must not be empty.
6713 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006714 */
6715 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006716onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006717 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006718 char_u *wcopy;
6719 int upper; /* TRUE: first letter made upper case */
6720{
6721 char_u *p;
6722 int c;
6723 int l;
6724
6725 p = word;
6726#ifdef FEAT_MBYTE
6727 if (has_mbyte)
6728 c = mb_ptr2char_adv(&p);
6729 else
6730#endif
6731 c = *p++;
6732 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006733 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006734 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006735 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006736#ifdef FEAT_MBYTE
6737 if (has_mbyte)
6738 l = mb_char2bytes(c, wcopy);
6739 else
6740#endif
6741 {
6742 l = 1;
6743 wcopy[0] = c;
6744 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006745 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006746}
6747
6748/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006749 * Make a copy of "word" with all the letters upper cased into
6750 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006751 */
6752 static void
6753allcap_copy(word, wcopy)
6754 char_u *word;
6755 char_u *wcopy;
6756{
6757 char_u *s;
6758 char_u *d;
6759 int c;
6760
6761 d = wcopy;
6762 for (s = word; *s != NUL; )
6763 {
6764#ifdef FEAT_MBYTE
6765 if (has_mbyte)
6766 c = mb_ptr2char_adv(&s);
6767 else
6768#endif
6769 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006770 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006771
6772#ifdef FEAT_MBYTE
6773 if (has_mbyte)
6774 {
6775 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
6776 break;
6777 d += mb_char2bytes(c, d);
6778 }
6779 else
6780#endif
6781 {
6782 if (d - wcopy >= MAXWLEN - 1)
6783 break;
6784 *d++ = c;
6785 }
6786 }
6787 *d = NUL;
6788}
6789
6790/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006791 * Try finding suggestions by recognizing specific situations.
6792 */
6793 static void
6794suggest_try_special(su)
6795 suginfo_T *su;
6796{
6797 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006798 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006799 int c;
6800 char_u word[MAXWLEN];
6801
6802 /*
6803 * Recognize a word that is repeated: "the the".
6804 */
6805 p = skiptowhite(su->su_fbadword);
6806 len = p - su->su_fbadword;
6807 p = skipwhite(p);
6808 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
6809 {
6810 /* Include badflags: if the badword is onecap or allcap
6811 * use that for the goodword too: "The the" -> "The". */
6812 c = su->su_fbadword[len];
6813 su->su_fbadword[len] = NUL;
6814 make_case_word(su->su_fbadword, word, su->su_badflags);
6815 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006816 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006817 }
6818}
6819
6820/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006821 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00006822 *
6823 * This uses a state machine. At each node in the tree we try various
6824 * operations. When trying if an operation work "depth" is increased and the
6825 * stack[] is used to store info. This allows combinations, thus insert one
6826 * character, replace one and delete another. The number of changes is
6827 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006828 */
6829 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00006830suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006831 suginfo_T *su;
6832{
6833 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
6834 char_u tword[MAXWLEN]; /* good word collected so far */
6835 trystate_T stack[MAXWLEN];
6836 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
6837 * to for word split) */
6838 char_u prewordlen = 0; /* length of word in "preword" */
6839 int splitoff = 0; /* index in tword after last split */
6840 trystate_T *sp;
6841 int newscore;
6842 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006843 char_u *byts, *fbyts, *pbyts;
6844 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006845 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006846 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006847 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006848 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006849 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006850 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006851 int len;
6852 char_u *p;
6853 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00006854 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006855 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006856
6857 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00006858 * to find matches (esp. REP items). Append some more text, changing
6859 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006860 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006861 n = STRLEN(fword);
6862 p = su->su_badptr + su->su_badlen;
6863 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006864
6865 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6866 lp->lp_slang != NULL; ++lp)
6867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006868 /*
6869 * Go through the whole case-fold tree, try changes at each node.
6870 * "tword[]" contains the word collected from nodes in the tree.
6871 * "fword[]" the word we are trying to match with (initially the bad
6872 * word).
6873 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006874 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006875 sp = &stack[0];
6876 sp->ts_state = STATE_START;
6877 sp->ts_score = 0;
6878 sp->ts_curi = 1;
6879 sp->ts_fidx = 0;
6880 sp->ts_fidxtry = 0;
6881 sp->ts_twordlen = 0;
6882 sp->ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006883#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006884 sp->ts_tcharlen = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006885#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006886
Bram Moolenaarea424162005-06-16 21:51:00 +00006887 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006888 * When there are postponed prefixes we need to use these first. At
6889 * the end of the prefix we continue in the case-fold tree.
6890 */
6891 fbyts = lp->lp_slang->sl_fbyts;
6892 fidxs = lp->lp_slang->sl_fidxs;
6893 pbyts = lp->lp_slang->sl_pbyts;
6894 pidxs = lp->lp_slang->sl_pidxs;
6895 if (pbyts != NULL)
6896 {
6897 byts = pbyts;
6898 idxs = pidxs;
6899 sp->ts_prefixdepth = PREFIXTREE;
6900 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
6901 }
6902 else
6903 {
6904 byts = fbyts;
6905 idxs = fidxs;
6906 sp->ts_prefixdepth = NOPREFIX;
6907 }
6908
6909 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00006910 * Loop to find all suggestions. At each round we either:
6911 * - For the current state try one operation, advance "ts_curi",
6912 * increase "depth".
6913 * - When a state is done go to the next, set "ts_state".
6914 * - When all states are tried decrease "depth".
6915 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006916 while (depth >= 0 && !got_int)
6917 {
6918 sp = &stack[depth];
6919 switch (sp->ts_state)
6920 {
6921 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006922 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006923 /*
6924 * Start of node: Deal with NUL bytes, which means
6925 * tword[] may end here.
6926 */
6927 arridx = sp->ts_arridx; /* current node in the tree */
6928 len = byts[arridx]; /* bytes in this node */
6929 arridx += sp->ts_curi; /* index of current byte */
6930
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006931 if (sp->ts_prefixdepth == PREFIXTREE)
6932 {
6933 /* Skip over the NUL bytes, we use them later. */
6934 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
6935 ;
6936 sp->ts_curi += n;
6937
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006938 /* Always past NUL bytes now. */
6939 n = (int)sp->ts_state;
6940 sp->ts_state = STATE_ENDNUL;
6941
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006942 /* At end of a prefix or at start of prefixtree: check for
6943 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006944 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006945 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006946 ++depth;
6947 stack[depth] = stack[depth - 1];
6948 sp = &stack[depth];
6949 sp->ts_prefixdepth = depth - 1;
6950 byts = fbyts;
6951 idxs = fidxs;
6952 sp->ts_state = STATE_START;
6953 sp->ts_curi = 1; /* start just after length byte */
6954 sp->ts_arridx = 0;
6955
6956 /* Move the prefix to preword[] so that
6957 * find_keepcap_word() works. */
6958 prewordlen = splitoff = sp->ts_twordlen;
6959 mch_memmove(preword, tword, splitoff);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006960 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006961 break;
6962 }
6963
Bram Moolenaar0c405862005-06-22 22:26:26 +00006964 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006965 {
6966 /* Past bytes in node and/or past NUL bytes. */
6967 sp->ts_state = STATE_ENDNUL;
6968 break;
6969 }
6970
6971 /*
6972 * End of word in tree.
6973 */
6974 ++sp->ts_curi; /* eat one NUL byte */
6975
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006976 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006977
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006978 if (sp->ts_prefixdepth < MAXWLEN)
6979 {
6980 /* There was a prefix before the word. Check that the
6981 * prefix can be used with this word. */
6982 /* Count the length of the NULs in the prefix. If there
6983 * are none this must be the first try without a prefix.
6984 */
6985 n = stack[sp->ts_prefixdepth].ts_arridx;
6986 len = pbyts[n++];
6987 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
6988 ;
6989 if (c > 0)
6990 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006991 /* The prefix ID is stored three bytes above the
6992 * flags. */
6993 c = valid_word_prefix(c, n, flags,
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006994 tword + splitoff, lp->lp_slang);
6995 if (c == 0)
6996 break;
6997
6998 /* Use the WF_RARE flag for a rare prefix. */
6999 if (c & WF_RAREPFX)
7000 flags |= WF_RARE;
7001 }
7002 }
7003
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007004 /*
7005 * Form the word with proper case in preword.
7006 * If there is a word from a previous split, append.
7007 */
7008 tword[sp->ts_twordlen] = NUL;
7009 if (flags & WF_KEEPCAP)
7010 /* Must find the word in the keep-case tree. */
7011 find_keepcap_word(lp->lp_slang, tword + splitoff,
7012 preword + prewordlen);
7013 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00007014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007015 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00007016 * use that for the goodword too. But if the badword is
7017 * allcap and it's only one char long use onecap. */
7018 c = su->su_badflags;
7019 if ((c & WF_ALLCAP)
7020#ifdef FEAT_MBYTE
7021 && su->su_badlen == mb_ptr2len_check(su->su_badptr)
7022#else
7023 && su->su_badlen == 1
7024#endif
7025 )
7026 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007027 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007028 preword + prewordlen, flags | c);
7029 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007030
7031 /* Don't use a banned word. It may appear again as a good
7032 * word, thus remember it. */
7033 if (flags & WF_BANNED)
7034 {
7035 add_banned(su, preword + prewordlen);
7036 break;
7037 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007038 if (was_banned(su, preword + prewordlen)
7039 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007040 break;
7041
7042 newscore = 0;
7043 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007044 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007045 newscore += SCORE_REGION;
7046 if (flags & WF_RARE)
7047 newscore += SCORE_RARE;
7048
Bram Moolenaar0c405862005-06-22 22:26:26 +00007049 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007050 captype(preword + prewordlen, NULL)))
7051 newscore += SCORE_ICASE;
7052
Bram Moolenaar0c405862005-06-22 22:26:26 +00007053 if ((fword[sp->ts_fidx] == NUL
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007054 || !spell_iswordp(fword + sp->ts_fidx, curbuf))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007055 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007056 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007057 /* The badword also ends: add suggestions. Give a penalty
7058 * when changing non-word char to word char, e.g., "thes,"
7059 * -> "these". */
7060 p = fword + sp->ts_fidx;
7061#ifdef FEAT_MBYTE
7062 if (has_mbyte)
7063 mb_ptr_back(fword, p);
7064 else
7065#endif
7066 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007067 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007068 {
7069 p = preword + STRLEN(preword);
7070#ifdef FEAT_MBYTE
7071 if (has_mbyte)
7072 mb_ptr_back(preword, p);
7073 else
7074#endif
7075 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007076 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007077 newscore += SCORE_NONWORD;
7078 }
7079
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007080 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007081 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007082 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007083 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007084 else if (sp->ts_fidx >= sp->ts_fidxtry
7085#ifdef FEAT_MBYTE
7086 /* Don't split halfway a character. */
7087 && (!has_mbyte || sp->ts_tcharlen == 0)
7088#endif
7089 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007090 {
7091 /* The word in the tree ends but the badword
7092 * continues: try inserting a space and check that a valid
7093 * words starts at fword[sp->ts_fidx]. */
7094 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
7095 {
7096 /* Save things to be restored at STATE_SPLITUNDO. */
7097 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007098 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007099 sp->ts_save_splitoff = splitoff;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007100 sp->ts_state = STATE_SPLITUNDO;
7101
7102 ++depth;
7103 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007104
7105 /* Append a space to preword. */
7106 STRCAT(preword, " ");
7107 prewordlen = STRLEN(preword);
7108 splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007109
7110 /* If the badword has a non-word character at this
7111 * position skip it. That means replacing the
7112 * non-word character with a space. */
7113 if (!spell_iswordp_nmw(fword + sp->ts_fidx))
7114 {
7115 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
7116#ifdef FEAT_MBYTE
7117 if (has_mbyte)
7118 sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
7119 else
7120#endif
7121 ++sp->ts_fidx;
7122 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007123#ifdef FEAT_MBYTE
7124 if (has_mbyte)
7125 {
7126 int i = 0;
7127
7128 /* Case-folding may change the number of bytes:
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007129 * Count nr of chars in fword[ts_fidx] and
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007130 * advance that many chars in su->su_badptr. */
7131 for (p = fword; p < fword + sp->ts_fidx;
7132 mb_ptr_adv(p))
7133 ++i;
7134 for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
7135 --i;
7136 }
7137 else
7138#endif
7139 p = su->su_badptr + sp->ts_fidx;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007140 su->su_badflags = captype(p, su->su_badptr
7141 + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007142
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007143 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007144 sp->ts_arridx = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007145 }
7146 }
7147 break;
7148
7149 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007150 /* Undo the changes done for word split. */
7151 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007152 splitoff = sp->ts_save_splitoff;
7153 prewordlen = sp->ts_save_prewordlen;
7154
7155 /* Continue looking for NUL bytes. */
7156 sp->ts_state = STATE_START;
7157 break;
7158
7159 case STATE_ENDNUL:
7160 /* Past the NUL bytes in the node. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007161 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007162 {
7163 /* The badword ends, can't use the bytes in this node. */
7164 sp->ts_state = STATE_DEL;
7165 break;
7166 }
7167 sp->ts_state = STATE_PLAIN;
7168 /*FALLTHROUGH*/
7169
7170 case STATE_PLAIN:
7171 /*
7172 * Go over all possible bytes at this node, add each to
7173 * tword[] and use child node. "ts_curi" is the index.
7174 */
7175 arridx = sp->ts_arridx;
7176 if (sp->ts_curi > byts[arridx])
7177 {
7178 /* Done all bytes at this node, do next state. When still
7179 * at already changed bytes skip the other tricks. */
7180 if (sp->ts_fidx >= sp->ts_fidxtry)
7181 sp->ts_state = STATE_DEL;
7182 else
7183 sp->ts_state = STATE_FINAL;
7184 }
7185 else
7186 {
7187 arridx += sp->ts_curi++;
7188 c = byts[arridx];
7189
7190 /* Normal byte, go one level deeper. If it's not equal to
7191 * the byte in the bad word adjust the score. But don't
7192 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00007193 if (c == fword[sp->ts_fidx]
7194#ifdef FEAT_MBYTE
7195 || (sp->ts_tcharlen > 0
7196 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007197#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00007198 )
7199 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007200 else
7201 newscore = SCORE_SUBST;
7202 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
7203 && try_deeper(su, stack, depth, newscore))
7204 {
7205 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007206 sp = &stack[depth];
7207 ++sp->ts_fidx;
7208 tword[sp->ts_twordlen++] = c;
7209 sp->ts_arridx = idxs[arridx];
7210#ifdef FEAT_MBYTE
7211 if (newscore == SCORE_SUBST)
7212 sp->ts_isdiff = DIFF_YES;
7213 if (has_mbyte)
7214 {
7215 /* Multi-byte characters are a bit complicated to
7216 * handle: They differ when any of the bytes
7217 * differ and then their length may also differ. */
7218 if (sp->ts_tcharlen == 0)
7219 {
7220 /* First byte. */
7221 sp->ts_tcharidx = 0;
7222 sp->ts_tcharlen = MB_BYTE2LEN(c);
7223 sp->ts_fcharstart = sp->ts_fidx - 1;
7224 sp->ts_isdiff = (newscore != 0)
7225 ? DIFF_YES : DIFF_NONE;
7226 }
7227 else if (sp->ts_isdiff == DIFF_INSERT)
7228 /* When inserting trail bytes don't advance in
7229 * the bad word. */
7230 --sp->ts_fidx;
7231 if (++sp->ts_tcharidx == sp->ts_tcharlen)
7232 {
7233 /* Last byte of character. */
7234 if (sp->ts_isdiff == DIFF_YES)
7235 {
7236 /* Correct ts_fidx for the byte length of
7237 * the character (we didn't check that
7238 * before). */
7239 sp->ts_fidx = sp->ts_fcharstart
7240 + MB_BYTE2LEN(
7241 fword[sp->ts_fcharstart]);
7242
7243 /* For a similar character adjust score
7244 * from SCORE_SUBST to SCORE_SIMILAR. */
7245 if (lp->lp_slang->sl_has_map
7246 && similar_chars(lp->lp_slang,
7247 mb_ptr2char(tword
7248 + sp->ts_twordlen
7249 - sp->ts_tcharlen),
7250 mb_ptr2char(fword
7251 + sp->ts_fcharstart)))
7252 sp->ts_score -=
7253 SCORE_SUBST - SCORE_SIMILAR;
7254 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007255 else if (sp->ts_isdiff == DIFF_INSERT
7256 && sp->ts_twordlen > sp->ts_tcharlen)
7257 {
7258 /* If the previous character was the same,
7259 * thus doubling a character, give a bonus
7260 * to the score. */
7261 p = tword + sp->ts_twordlen
7262 - sp->ts_tcharlen;
7263 c = mb_ptr2char(p);
7264 mb_ptr_back(tword, p);
7265 if (c == mb_ptr2char(p))
7266 sp->ts_score -= SCORE_INS
7267 - SCORE_INSDUP;
7268 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007269
7270 /* Starting a new char, reset the length. */
7271 sp->ts_tcharlen = 0;
7272 }
7273 }
7274 else
7275#endif
7276 {
7277 /* If we found a similar char adjust the score.
7278 * We do this after calling try_deeper() because
7279 * it's slow. */
7280 if (newscore != 0
7281 && lp->lp_slang->sl_has_map
7282 && similar_chars(lp->lp_slang,
7283 c, fword[sp->ts_fidx - 1]))
7284 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
7285 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007286 }
7287 }
7288 break;
7289
7290 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00007291#ifdef FEAT_MBYTE
7292 /* When past the first byte of a multi-byte char don't try
7293 * delete/insert/swap a character. */
7294 if (has_mbyte && sp->ts_tcharlen > 0)
7295 {
7296 sp->ts_state = STATE_FINAL;
7297 break;
7298 }
7299#endif
7300 /*
7301 * Try skipping one character in the bad word (delete it).
7302 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007303 sp->ts_state = STATE_INS;
7304 sp->ts_curi = 1;
7305 if (fword[sp->ts_fidx] != NUL
7306 && try_deeper(su, stack, depth, SCORE_DEL))
7307 {
7308 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00007309
7310 /* Advance over the character in fword[]. Give a bonus to
7311 * the score if the same character is following "nn" ->
7312 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00007313#ifdef FEAT_MBYTE
7314 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00007315 {
7316 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00007317 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaarea408852005-06-25 22:49:46 +00007318 if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
7319 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7320 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007321 else
7322#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007323 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007324 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00007325 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
7326 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7327 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007328 break;
7329 }
7330 /*FALLTHROUGH*/
7331
7332 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00007333 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007334 * node. */
7335 n = sp->ts_arridx;
7336 if (sp->ts_curi > byts[n])
7337 {
7338 /* Done all bytes at this node, do next state. */
7339 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007340 }
7341 else
7342 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007343 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007344 n += sp->ts_curi++;
7345 c = byts[n];
7346 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
7347 {
7348 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007349 sp = &stack[depth];
7350 tword[sp->ts_twordlen++] = c;
7351 sp->ts_arridx = idxs[n];
7352#ifdef FEAT_MBYTE
7353 if (has_mbyte)
7354 {
7355 fl = MB_BYTE2LEN(c);
7356 if (fl > 1)
7357 {
7358 /* There are following bytes for the same
7359 * character. We must find all bytes before
7360 * trying delete/insert/swap/etc. */
7361 sp->ts_tcharlen = fl;
7362 sp->ts_tcharidx = 1;
7363 sp->ts_isdiff = DIFF_INSERT;
7364 }
7365 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007366 else
7367 fl = 1;
7368 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00007369#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007370 {
7371 /* If the previous character was the same, thus
7372 * doubling a character, give a bonus to the
7373 * score. */
7374 if (sp->ts_twordlen >= 2
7375 && tword[sp->ts_twordlen - 2] == c)
7376 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
7377 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007378 }
7379 }
7380 break;
7381
7382 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00007383 /*
7384 * Swap two bytes in the bad word: "12" -> "21".
7385 * We change "fword" here, it's changed back afterwards.
7386 */
7387 p = fword + sp->ts_fidx;
7388 c = *p;
7389 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007390 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007391 /* End of word, can't swap or replace. */
7392 sp->ts_state = STATE_FINAL;
7393 break;
7394 }
7395#ifdef FEAT_MBYTE
7396 if (has_mbyte)
7397 {
7398 n = mb_ptr2len_check(p);
7399 c = mb_ptr2char(p);
7400 c2 = mb_ptr2char(p + n);
7401 }
7402 else
7403#endif
7404 c2 = p[1];
7405 if (c == c2)
7406 {
7407 /* Characters are identical, swap won't do anything. */
7408 sp->ts_state = STATE_SWAP3;
7409 break;
7410 }
7411 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
7412 {
7413 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007414 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007415#ifdef FEAT_MBYTE
7416 if (has_mbyte)
7417 {
7418 fl = mb_char2len(c2);
7419 mch_memmove(p, p + n, fl);
7420 mb_char2bytes(c, p + fl);
7421 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7422 }
7423 else
7424#endif
7425 {
7426 p[0] = c2;
7427 p[1] = c;
7428 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
7429 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007430 }
7431 else
7432 /* If this swap doesn't work then SWAP3 won't either. */
7433 sp->ts_state = STATE_REP_INI;
7434 break;
7435
Bram Moolenaarea424162005-06-16 21:51:00 +00007436 case STATE_UNSWAP:
7437 /* Undo the STATE_SWAP swap: "21" -> "12". */
7438 p = fword + sp->ts_fidx;
7439#ifdef FEAT_MBYTE
7440 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007441 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007442 n = MB_BYTE2LEN(*p);
7443 c = mb_ptr2char(p + n);
7444 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
7445 mb_char2bytes(c, p);
7446 }
7447 else
7448#endif
7449 {
7450 c = *p;
7451 *p = p[1];
7452 p[1] = c;
7453 }
7454 /*FALLTHROUGH*/
7455
7456 case STATE_SWAP3:
7457 /* Swap two bytes, skipping one: "123" -> "321". We change
7458 * "fword" here, it's changed back afterwards. */
7459 p = fword + sp->ts_fidx;
7460#ifdef FEAT_MBYTE
7461 if (has_mbyte)
7462 {
7463 n = mb_ptr2len_check(p);
7464 c = mb_ptr2char(p);
7465 fl = mb_ptr2len_check(p + n);
7466 c2 = mb_ptr2char(p + n);
7467 c3 = mb_ptr2char(p + n + fl);
7468 }
7469 else
7470#endif
7471 {
7472 c = *p;
7473 c2 = p[1];
7474 c3 = p[2];
7475 }
7476
7477 /* When characters are identical: "121" then SWAP3 result is
7478 * identical, ROT3L result is same as SWAP: "211", ROT3L
7479 * result is same as SWAP on next char: "112". Thus skip all
7480 * swapping. Also skip when c3 is NUL. */
7481 if (c == c3 || c3 == NUL)
7482 {
7483 sp->ts_state = STATE_REP_INI;
7484 break;
7485 }
7486 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7487 {
7488 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007489 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007490#ifdef FEAT_MBYTE
7491 if (has_mbyte)
7492 {
7493 tl = mb_char2len(c3);
7494 mch_memmove(p, p + n + fl, tl);
7495 mb_char2bytes(c2, p + tl);
7496 mb_char2bytes(c, p + fl + tl);
7497 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
7498 }
7499 else
7500#endif
7501 {
7502 p[0] = p[2];
7503 p[2] = c;
7504 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7505 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007506 }
7507 else
7508 sp->ts_state = STATE_REP_INI;
7509 break;
7510
Bram Moolenaarea424162005-06-16 21:51:00 +00007511 case STATE_UNSWAP3:
7512 /* Undo STATE_SWAP3: "321" -> "123" */
7513 p = fword + sp->ts_fidx;
7514#ifdef FEAT_MBYTE
7515 if (has_mbyte)
7516 {
7517 n = MB_BYTE2LEN(*p);
7518 c2 = mb_ptr2char(p + n);
7519 fl = MB_BYTE2LEN(p[n]);
7520 c = mb_ptr2char(p + n + fl);
7521 tl = MB_BYTE2LEN(p[n + fl]);
7522 mch_memmove(p + fl + tl, p, n);
7523 mb_char2bytes(c, p);
7524 mb_char2bytes(c2, p + tl);
7525 }
7526 else
7527#endif
7528 {
7529 c = *p;
7530 *p = p[2];
7531 p[2] = c;
7532 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007533
Bram Moolenaarea424162005-06-16 21:51:00 +00007534 /* Rotate three characters left: "123" -> "231". We change
7535 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007536 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7537 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007538 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007539 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007540 p = fword + sp->ts_fidx;
7541#ifdef FEAT_MBYTE
7542 if (has_mbyte)
7543 {
7544 n = mb_ptr2len_check(p);
7545 c = mb_ptr2char(p);
7546 fl = mb_ptr2len_check(p + n);
7547 fl += mb_ptr2len_check(p + n + fl);
7548 mch_memmove(p, p + n, fl);
7549 mb_char2bytes(c, p + fl);
7550 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7551 }
7552 else
7553#endif
7554 {
7555 c = *p;
7556 *p = p[1];
7557 p[1] = p[2];
7558 p[2] = c;
7559 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7560 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007561 }
7562 else
7563 sp->ts_state = STATE_REP_INI;
7564 break;
7565
Bram Moolenaarea424162005-06-16 21:51:00 +00007566 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007567 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007568 p = fword + sp->ts_fidx;
7569#ifdef FEAT_MBYTE
7570 if (has_mbyte)
7571 {
7572 n = MB_BYTE2LEN(*p);
7573 n += MB_BYTE2LEN(p[n]);
7574 c = mb_ptr2char(p + n);
7575 tl = MB_BYTE2LEN(p[n]);
7576 mch_memmove(p + tl, p, n);
7577 mb_char2bytes(c, p);
7578 }
7579 else
7580#endif
7581 {
7582 c = p[2];
7583 p[2] = p[1];
7584 p[1] = *p;
7585 *p = c;
7586 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007587
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007588 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00007589 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007590 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7591 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007592 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007593 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007594 p = fword + sp->ts_fidx;
7595#ifdef FEAT_MBYTE
7596 if (has_mbyte)
7597 {
7598 n = mb_ptr2len_check(p);
7599 n += mb_ptr2len_check(p + n);
7600 c = mb_ptr2char(p + n);
7601 tl = mb_ptr2len_check(p + n);
7602 mch_memmove(p + tl, p, n);
7603 mb_char2bytes(c, p);
7604 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
7605 }
7606 else
7607#endif
7608 {
7609 c = p[2];
7610 p[2] = p[1];
7611 p[1] = *p;
7612 *p = c;
7613 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7614 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007615 }
7616 else
7617 sp->ts_state = STATE_REP_INI;
7618 break;
7619
Bram Moolenaarea424162005-06-16 21:51:00 +00007620 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007621 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007622 p = fword + sp->ts_fidx;
7623#ifdef FEAT_MBYTE
7624 if (has_mbyte)
7625 {
7626 c = mb_ptr2char(p);
7627 tl = MB_BYTE2LEN(*p);
7628 n = MB_BYTE2LEN(p[tl]);
7629 n += MB_BYTE2LEN(p[tl + n]);
7630 mch_memmove(p, p + tl, n);
7631 mb_char2bytes(c, p + n);
7632 }
7633 else
7634#endif
7635 {
7636 c = *p;
7637 *p = p[1];
7638 p[1] = p[2];
7639 p[2] = c;
7640 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007641 /*FALLTHROUGH*/
7642
7643 case STATE_REP_INI:
7644 /* Check if matching with REP items from the .aff file would
7645 * work. Quickly skip if there are no REP items or the score
7646 * is going to be too high anyway. */
7647 gap = &lp->lp_slang->sl_rep;
7648 if (gap->ga_len == 0
7649 || sp->ts_score + SCORE_REP >= su->su_maxscore)
7650 {
7651 sp->ts_state = STATE_FINAL;
7652 break;
7653 }
7654
7655 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00007656 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007657 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
7658 if (sp->ts_curi < 0)
7659 {
7660 sp->ts_state = STATE_FINAL;
7661 break;
7662 }
7663
7664 sp->ts_state = STATE_REP;
7665 /*FALLTHROUGH*/
7666
7667 case STATE_REP:
7668 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00007669 * match replace the characters and check if the resulting
7670 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007671 p = fword + sp->ts_fidx;
7672
7673 gap = &lp->lp_slang->sl_rep;
7674 while (sp->ts_curi < gap->ga_len)
7675 {
7676 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
7677 if (*ftp->ft_from != *p)
7678 {
7679 /* past possible matching entries */
7680 sp->ts_curi = gap->ga_len;
7681 break;
7682 }
7683 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
7684 && try_deeper(su, stack, depth, SCORE_REP))
7685 {
7686 /* Need to undo this afterwards. */
7687 sp->ts_state = STATE_REP_UNDO;
7688
7689 /* Change the "from" to the "to" string. */
7690 ++depth;
7691 fl = STRLEN(ftp->ft_from);
7692 tl = STRLEN(ftp->ft_to);
7693 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007695 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007696 repextra += tl - fl;
7697 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007698 mch_memmove(p, ftp->ft_to, tl);
7699 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00007700#ifdef FEAT_MBYTE
7701 stack[depth].ts_tcharlen = 0;
7702#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007703 break;
7704 }
7705 }
7706
7707 if (sp->ts_curi >= gap->ga_len)
7708 /* No (more) matches. */
7709 sp->ts_state = STATE_FINAL;
7710
7711 break;
7712
7713 case STATE_REP_UNDO:
7714 /* Undo a REP replacement and continue with the next one. */
7715 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
7716 + sp->ts_curi - 1;
7717 fl = STRLEN(ftp->ft_from);
7718 tl = STRLEN(ftp->ft_to);
7719 p = fword + sp->ts_fidx;
7720 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007721 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007722 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007723 repextra -= tl - fl;
7724 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007725 mch_memmove(p, ftp->ft_from, fl);
7726 sp->ts_state = STATE_REP;
7727 break;
7728
7729 default:
7730 /* Did all possible states at this level, go up one level. */
7731 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007732
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007733 if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE)
7734 {
7735 /* Continue in or go back to the prefix tree. */
7736 byts = pbyts;
7737 idxs = pidxs;
7738 splitoff = 0;
7739 }
7740
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007741 /* Don't check for CTRL-C too often, it takes time. */
7742 line_breakcheck();
7743 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007744 }
7745 }
7746}
7747
7748/*
7749 * Try going one level deeper in the tree.
7750 */
7751 static int
7752try_deeper(su, stack, depth, score_add)
7753 suginfo_T *su;
7754 trystate_T *stack;
7755 int depth;
7756 int score_add;
7757{
7758 int newscore;
7759
7760 /* Refuse to go deeper if the scrore is getting too big. */
7761 newscore = stack[depth].ts_score + score_add;
7762 if (newscore >= su->su_maxscore)
7763 return FALSE;
7764
Bram Moolenaarea424162005-06-16 21:51:00 +00007765 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007766 stack[depth + 1].ts_state = STATE_START;
7767 stack[depth + 1].ts_score = newscore;
7768 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007769 return TRUE;
7770}
7771
7772/*
7773 * "fword" is a good word with case folded. Find the matching keep-case
7774 * words and put it in "kword".
7775 * Theoretically there could be several keep-case words that result in the
7776 * same case-folded word, but we only find one...
7777 */
7778 static void
7779find_keepcap_word(slang, fword, kword)
7780 slang_T *slang;
7781 char_u *fword;
7782 char_u *kword;
7783{
7784 char_u uword[MAXWLEN]; /* "fword" in upper-case */
7785 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007786 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007787
7788 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007789 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007790 int round[MAXWLEN];
7791 int fwordidx[MAXWLEN];
7792 int uwordidx[MAXWLEN];
7793 int kwordlen[MAXWLEN];
7794
7795 int flen, ulen;
7796 int l;
7797 int len;
7798 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007799 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007800 char_u *p;
7801 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007802 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007803
7804 if (byts == NULL)
7805 {
7806 /* array is empty: "cannot happen" */
7807 *kword = NUL;
7808 return;
7809 }
7810
7811 /* Make an all-cap version of "fword". */
7812 allcap_copy(fword, uword);
7813
7814 /*
7815 * Each character needs to be tried both case-folded and upper-case.
7816 * All this gets very complicated if we keep in mind that changing case
7817 * may change the byte length of a multi-byte character...
7818 */
7819 depth = 0;
7820 arridx[0] = 0;
7821 round[0] = 0;
7822 fwordidx[0] = 0;
7823 uwordidx[0] = 0;
7824 kwordlen[0] = 0;
7825 while (depth >= 0)
7826 {
7827 if (fword[fwordidx[depth]] == NUL)
7828 {
7829 /* We are at the end of "fword". If the tree allows a word to end
7830 * here we have found a match. */
7831 if (byts[arridx[depth] + 1] == 0)
7832 {
7833 kword[kwordlen[depth]] = NUL;
7834 return;
7835 }
7836
7837 /* kword is getting too long, continue one level up */
7838 --depth;
7839 }
7840 else if (++round[depth] > 2)
7841 {
7842 /* tried both fold-case and upper-case character, continue one
7843 * level up */
7844 --depth;
7845 }
7846 else
7847 {
7848 /*
7849 * round[depth] == 1: Try using the folded-case character.
7850 * round[depth] == 2: Try using the upper-case character.
7851 */
7852#ifdef FEAT_MBYTE
7853 if (has_mbyte)
7854 {
7855 flen = mb_ptr2len_check(fword + fwordidx[depth]);
7856 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
7857 }
7858 else
7859#endif
7860 ulen = flen = 1;
7861 if (round[depth] == 1)
7862 {
7863 p = fword + fwordidx[depth];
7864 l = flen;
7865 }
7866 else
7867 {
7868 p = uword + uwordidx[depth];
7869 l = ulen;
7870 }
7871
7872 for (tryidx = arridx[depth]; l > 0; --l)
7873 {
7874 /* Perform a binary search in the list of accepted bytes. */
7875 len = byts[tryidx++];
7876 c = *p++;
7877 lo = tryidx;
7878 hi = tryidx + len - 1;
7879 while (lo < hi)
7880 {
7881 m = (lo + hi) / 2;
7882 if (byts[m] > c)
7883 hi = m - 1;
7884 else if (byts[m] < c)
7885 lo = m + 1;
7886 else
7887 {
7888 lo = hi = m;
7889 break;
7890 }
7891 }
7892
7893 /* Stop if there is no matching byte. */
7894 if (hi < lo || byts[lo] != c)
7895 break;
7896
7897 /* Continue at the child (if there is one). */
7898 tryidx = idxs[lo];
7899 }
7900
7901 if (l == 0)
7902 {
7903 /*
7904 * Found the matching char. Copy it to "kword" and go a
7905 * level deeper.
7906 */
7907 if (round[depth] == 1)
7908 {
7909 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
7910 flen);
7911 kwordlen[depth + 1] = kwordlen[depth] + flen;
7912 }
7913 else
7914 {
7915 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
7916 ulen);
7917 kwordlen[depth + 1] = kwordlen[depth] + ulen;
7918 }
7919 fwordidx[depth + 1] = fwordidx[depth] + flen;
7920 uwordidx[depth + 1] = uwordidx[depth] + ulen;
7921
7922 ++depth;
7923 arridx[depth] = tryidx;
7924 round[depth] = 0;
7925 }
7926 }
7927 }
7928
7929 /* Didn't find it: "cannot happen". */
7930 *kword = NUL;
7931}
7932
7933/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007934 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
7935 * su->su_sga.
7936 */
7937 static void
7938score_comp_sal(su)
7939 suginfo_T *su;
7940{
7941 langp_T *lp;
7942 char_u badsound[MAXWLEN];
7943 int i;
7944 suggest_T *stp;
7945 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007946 int score;
7947
7948 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
7949 return;
7950
7951 /* Use the sound-folding of the first language that supports it. */
7952 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7953 lp->lp_slang != NULL; ++lp)
7954 if (lp->lp_slang->sl_sal.ga_len > 0)
7955 {
7956 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007957 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007958
7959 for (i = 0; i < su->su_ga.ga_len; ++i)
7960 {
7961 stp = &SUG(su->su_ga, i);
7962
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007963 /* Case-fold the suggested word, sound-fold it and compute the
7964 * sound-a-like score. */
7965 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007966 if (score < SCORE_MAXMAX)
7967 {
7968 /* Add the suggestion. */
7969 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
7970 sstp->st_word = vim_strsave(stp->st_word);
7971 if (sstp->st_word != NULL)
7972 {
7973 sstp->st_score = score;
7974 sstp->st_altscore = 0;
7975 sstp->st_orglen = stp->st_orglen;
7976 ++su->su_sga.ga_len;
7977 }
7978 }
7979 }
7980 break;
7981 }
7982}
7983
7984/*
7985 * Combine the list of suggestions in su->su_ga and su->su_sga.
7986 * They are intwined.
7987 */
7988 static void
7989score_combine(su)
7990 suginfo_T *su;
7991{
7992 int i;
7993 int j;
7994 garray_T ga;
7995 garray_T *gap;
7996 langp_T *lp;
7997 suggest_T *stp;
7998 char_u *p;
7999 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008000 int round;
8001
8002 /* Add the alternate score to su_ga. */
8003 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8004 lp->lp_slang != NULL; ++lp)
8005 {
8006 if (lp->lp_slang->sl_sal.ga_len > 0)
8007 {
8008 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008009 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008010
8011 for (i = 0; i < su->su_ga.ga_len; ++i)
8012 {
8013 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008014 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
8015 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008016 if (stp->st_altscore == SCORE_MAXMAX)
8017 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
8018 else
8019 stp->st_score = (stp->st_score * 3
8020 + stp->st_altscore) / 4;
8021 stp->st_salscore = FALSE;
8022 }
8023 break;
8024 }
8025 }
8026
8027 /* Add the alternate score to su_sga. */
8028 for (i = 0; i < su->su_sga.ga_len; ++i)
8029 {
8030 stp = &SUG(su->su_sga, i);
8031 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
8032 if (stp->st_score == SCORE_MAXMAX)
8033 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
8034 else
8035 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
8036 stp->st_salscore = TRUE;
8037 }
8038
8039 /* Sort the suggestions and truncate at "maxcount" for both lists. */
8040 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8041 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
8042
8043 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
8044 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
8045 return;
8046
8047 stp = &SUG(ga, 0);
8048 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
8049 {
8050 /* round 1: get a suggestion from su_ga
8051 * round 2: get a suggestion from su_sga */
8052 for (round = 1; round <= 2; ++round)
8053 {
8054 gap = round == 1 ? &su->su_ga : &su->su_sga;
8055 if (i < gap->ga_len)
8056 {
8057 /* Don't add a word if it's already there. */
8058 p = SUG(*gap, i).st_word;
8059 for (j = 0; j < ga.ga_len; ++j)
8060 if (STRCMP(stp[j].st_word, p) == 0)
8061 break;
8062 if (j == ga.ga_len)
8063 stp[ga.ga_len++] = SUG(*gap, i);
8064 else
8065 vim_free(p);
8066 }
8067 }
8068 }
8069
8070 ga_clear(&su->su_ga);
8071 ga_clear(&su->su_sga);
8072
8073 /* Truncate the list to the number of suggestions that will be displayed. */
8074 if (ga.ga_len > su->su_maxcount)
8075 {
8076 for (i = su->su_maxcount; i < ga.ga_len; ++i)
8077 vim_free(stp[i].st_word);
8078 ga.ga_len = su->su_maxcount;
8079 }
8080
8081 su->su_ga = ga;
8082}
8083
8084/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008085 * For the goodword in "stp" compute the soundalike score compared to the
8086 * badword.
8087 */
8088 static int
8089stp_sal_score(stp, su, slang, badsound)
8090 suggest_T *stp;
8091 suginfo_T *su;
8092 slang_T *slang;
8093 char_u *badsound; /* sound-folded badword */
8094{
8095 char_u *p;
8096 char_u badsound2[MAXWLEN];
8097 char_u fword[MAXWLEN];
8098 char_u goodsound[MAXWLEN];
8099
8100 if (stp->st_orglen <= su->su_badlen)
8101 p = badsound;
8102 else
8103 {
8104 /* soundfold the bad word with more characters following */
8105 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
8106
8107 /* When joining two words the sound often changes a lot. E.g., "t he"
8108 * sounds like "t h" while "the" sounds like "@". Avoid that by
8109 * removing the space. Don't do it when the good word also contains a
8110 * space. */
8111 if (vim_iswhite(su->su_badptr[su->su_badlen])
8112 && *skiptowhite(stp->st_word) == NUL)
8113 for (p = fword; *(p = skiptowhite(p)) != NUL; )
8114 mch_memmove(p, p + 1, STRLEN(p));
8115
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008116 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008117 p = badsound2;
8118 }
8119
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008120 /* Sound-fold the word and compute the score for the difference. */
8121 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008122
8123 return soundalike_score(goodsound, p);
8124}
8125
8126/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008127 * Find suggestions by comparing the word in a sound-a-like form.
8128 */
8129 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00008130suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008131 suginfo_T *su;
8132{
8133 char_u salword[MAXWLEN];
8134 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008135 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008136 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008137 int curi[MAXWLEN];
8138 langp_T *lp;
8139 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008140 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008141 int depth;
8142 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008143 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008144 int round;
8145 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008146 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008147
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008148 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008149 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8150 lp->lp_slang != NULL; ++lp)
8151 {
8152 if (lp->lp_slang->sl_sal.ga_len > 0)
8153 {
8154 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008155 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008156
8157 /*
8158 * Go through the whole tree, soundfold each word and compare.
8159 * round 1: use the case-folded tree.
8160 * round 2: use the keep-case tree.
8161 */
8162 for (round = 1; round <= 2; ++round)
8163 {
8164 if (round == 1)
8165 {
8166 byts = lp->lp_slang->sl_fbyts;
8167 idxs = lp->lp_slang->sl_fidxs;
8168 }
8169 else
8170 {
8171 byts = lp->lp_slang->sl_kbyts;
8172 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008173 if (byts == NULL) /* no keep-case words */
8174 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008175 }
8176
8177 depth = 0;
8178 arridx[0] = 0;
8179 curi[0] = 1;
8180 while (depth >= 0 && !got_int)
8181 {
8182 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008184 /* Done all bytes at this node, go up one level. */
8185 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008186 line_breakcheck();
8187 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008188 else
8189 {
8190 /* Do one more byte at this node. */
8191 n = arridx[depth] + curi[depth];
8192 ++curi[depth];
8193 c = byts[n];
8194 if (c == 0)
8195 {
8196 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008197 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008198 if (round == 2 || (flags & WF_KEEPCAP) == 0)
8199 {
8200 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008201 /* Sound-fold. Only in keep-case tree need to
8202 * case-fold the word. */
8203 spell_soundfold(lp->lp_slang, tword,
8204 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008205
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008206 /* Compute the edit distance between the
8207 * sound-a-like words. */
8208 sound_score = soundalike_score(salword,
8209 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008210 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008211 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008212 char_u cword[MAXWLEN];
8213 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008214 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008215
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008216 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008217 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008218 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008219 /* Need to fix case according to
8220 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008221 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008222 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008223 }
8224 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008225 p = tword;
8226
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008227 if (sps_flags & SPS_DOUBLE)
8228 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008229 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008230 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008231 else
8232 {
8233 /* Compute the score. */
8234 score = spell_edit_score(
8235 su->su_badword, p);
8236 if (sps_flags & SPS_BEST)
8237 /* give a bonus for the good word
8238 * sounding the same as the bad
8239 * word */
8240 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008241 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008242 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008243 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008244 else
8245 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008246 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008247 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008248 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008249 }
8250 }
8251
8252 /* Skip over other NUL bytes. */
8253 while (byts[n + 1] == 0)
8254 {
8255 ++n;
8256 ++curi[depth];
8257 }
8258 }
8259 else
8260 {
8261 /* Normal char, go one level deeper. */
8262 tword[depth++] = c;
8263 arridx[depth] = idxs[n];
8264 curi[depth] = 1;
8265 }
8266 }
8267 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008268 }
8269 }
8270 }
8271}
8272
8273/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008274 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008275 */
8276 static void
8277make_case_word(fword, cword, flags)
8278 char_u *fword;
8279 char_u *cword;
8280 int flags;
8281{
8282 if (flags & WF_ALLCAP)
8283 /* Make it all upper-case */
8284 allcap_copy(fword, cword);
8285 else if (flags & WF_ONECAP)
8286 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008287 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008288 else
8289 /* Use goodword as-is. */
8290 STRCPY(cword, fword);
8291}
8292
Bram Moolenaarea424162005-06-16 21:51:00 +00008293/*
8294 * Use map string "map" for languages "lp".
8295 */
8296 static void
8297set_map_str(lp, map)
8298 slang_T *lp;
8299 char_u *map;
8300{
8301 char_u *p;
8302 int headc = 0;
8303 int c;
8304 int i;
8305
8306 if (*map == NUL)
8307 {
8308 lp->sl_has_map = FALSE;
8309 return;
8310 }
8311 lp->sl_has_map = TRUE;
8312
8313 /* Init the array and hash table empty. */
8314 for (i = 0; i < 256; ++i)
8315 lp->sl_map_array[i] = 0;
8316#ifdef FEAT_MBYTE
8317 hash_init(&lp->sl_map_hash);
8318#endif
8319
8320 /*
8321 * The similar characters are stored separated with slashes:
8322 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
8323 * before the same slash. For characters above 255 sl_map_hash is used.
8324 */
8325 for (p = map; *p != NUL; )
8326 {
8327#ifdef FEAT_MBYTE
8328 c = mb_ptr2char_adv(&p);
8329#else
8330 c = *p++;
8331#endif
8332 if (c == '/')
8333 headc = 0;
8334 else
8335 {
8336 if (headc == 0)
8337 headc = c;
8338
8339#ifdef FEAT_MBYTE
8340 /* Characters above 255 don't fit in sl_map_array[], put them in
8341 * the hash table. Each entry is the char, a NUL the headchar and
8342 * a NUL. */
8343 if (c >= 256)
8344 {
8345 int cl = mb_char2len(c);
8346 int headcl = mb_char2len(headc);
8347 char_u *b;
8348 hash_T hash;
8349 hashitem_T *hi;
8350
8351 b = alloc((unsigned)(cl + headcl + 2));
8352 if (b == NULL)
8353 return;
8354 mb_char2bytes(c, b);
8355 b[cl] = NUL;
8356 mb_char2bytes(headc, b + cl + 1);
8357 b[cl + 1 + headcl] = NUL;
8358 hash = hash_hash(b);
8359 hi = hash_lookup(&lp->sl_map_hash, b, hash);
8360 if (HASHITEM_EMPTY(hi))
8361 hash_add_item(&lp->sl_map_hash, hi, b, hash);
8362 else
8363 {
8364 /* This should have been checked when generating the .spl
8365 * file. */
8366 EMSG(_("E999: duplicate char in MAP entry"));
8367 vim_free(b);
8368 }
8369 }
8370 else
8371#endif
8372 lp->sl_map_array[c] = headc;
8373 }
8374 }
8375}
8376
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008377/*
8378 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
8379 * lines in the .aff file.
8380 */
8381 static int
8382similar_chars(slang, c1, c2)
8383 slang_T *slang;
8384 int c1;
8385 int c2;
8386{
Bram Moolenaarea424162005-06-16 21:51:00 +00008387 int m1, m2;
8388#ifdef FEAT_MBYTE
8389 char_u buf[MB_MAXBYTES];
8390 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008391
Bram Moolenaarea424162005-06-16 21:51:00 +00008392 if (c1 >= 256)
8393 {
8394 buf[mb_char2bytes(c1, buf)] = 0;
8395 hi = hash_find(&slang->sl_map_hash, buf);
8396 if (HASHITEM_EMPTY(hi))
8397 m1 = 0;
8398 else
8399 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8400 }
8401 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008402#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008403 m1 = slang->sl_map_array[c1];
8404 if (m1 == 0)
8405 return FALSE;
8406
8407
8408#ifdef FEAT_MBYTE
8409 if (c2 >= 256)
8410 {
8411 buf[mb_char2bytes(c2, buf)] = 0;
8412 hi = hash_find(&slang->sl_map_hash, buf);
8413 if (HASHITEM_EMPTY(hi))
8414 m2 = 0;
8415 else
8416 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8417 }
8418 else
8419#endif
8420 m2 = slang->sl_map_array[c2];
8421
8422 return m1 == m2;
8423}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008424
8425/*
8426 * Add a suggestion to the list of suggestions.
8427 * Do not add a duplicate suggestion or suggestions with a bad score.
8428 * When "use_score" is not zero it's used, otherwise the score is computed
8429 * with spell_edit_score().
8430 */
8431 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008432add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008433 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008434 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008435 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008436 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008437 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008438 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008439 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008440{
8441 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008442 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008443 char_u *p = NULL;
8444 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008445
8446 /* Check that the word wasn't banned. */
8447 if (was_banned(su, goodword))
8448 return;
8449
Bram Moolenaar0c405862005-06-22 22:26:26 +00008450 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
8451 * Remove the common part from "goodword". */
8452 i = badlen - su->su_badlen;
8453 if (i > 0)
8454 {
8455 /* This assumes there was no case folding or it didn't change the
8456 * length... */
8457 p = goodword + STRLEN(goodword) - i;
8458 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
8459 {
8460 badlen = su->su_badlen;
8461 c = *p;
8462 *p = NUL;
8463 }
8464 else
8465 p = NULL;
8466 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008467 else if (i < 0)
8468 {
8469 /* When replacing part of the word check that we actually change
8470 * something. For "the the" a suggestion can be replacing the first
8471 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008472 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008473 && STRNCMP(su->su_badword, goodword, badlen) == 0)
8474 return;
8475 }
8476
Bram Moolenaar0c405862005-06-22 22:26:26 +00008477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008478 if (score <= su->su_maxscore)
8479 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008480 /* Check if the word is already there. Also check the length that is
8481 * being replaced "thes," -> "these" is a different suggestion from
8482 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008483 stp = &SUG(*gap, 0);
8484 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008485 if (STRCMP(stp[i].st_word, goodword) == 0
8486 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008487 {
8488 /* Found it. Remember the lowest score. */
8489 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008490 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008491 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008492 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008493 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008494 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008495 break;
8496 }
8497
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008498 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008499 {
8500 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008501 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008502 stp->st_word = vim_strsave(goodword);
8503 if (stp->st_word != NULL)
8504 {
8505 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008506 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008507 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008508 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008509 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008510
8511 /* If we have too many suggestions now, sort the list and keep
8512 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008513 if (gap->ga_len > SUG_MAX_COUNT(su))
8514 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
8515 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008516 }
8517 }
8518 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008519
8520 if (p != NULL)
8521 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008522}
8523
8524/*
8525 * Add a word to be banned.
8526 */
8527 static void
8528add_banned(su, word)
8529 suginfo_T *su;
8530 char_u *word;
8531{
8532 char_u *s = vim_strsave(word);
8533 hash_T hash;
8534 hashitem_T *hi;
8535
8536 if (s != NULL)
8537 {
8538 hash = hash_hash(s);
8539 hi = hash_lookup(&su->su_banned, s, hash);
8540 if (HASHITEM_EMPTY(hi))
8541 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008542 else
8543 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008544 }
8545}
8546
8547/*
8548 * Return TRUE if a word appears in the list of banned words.
8549 */
8550 static int
8551was_banned(su, word)
8552 suginfo_T *su;
8553 char_u *word;
8554{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008555 hashitem_T *hi = hash_find(&su->su_banned, word);
8556
8557 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008558}
8559
8560/*
8561 * Free the banned words in "su".
8562 */
8563 static void
8564free_banned(su)
8565 suginfo_T *su;
8566{
8567 int todo;
8568 hashitem_T *hi;
8569
8570 todo = su->su_banned.ht_used;
8571 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
8572 {
8573 if (!HASHITEM_EMPTY(hi))
8574 {
8575 vim_free(hi->hi_key);
8576 --todo;
8577 }
8578 }
8579 hash_clear(&su->su_banned);
8580}
8581
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008582/*
8583 * Recompute the score if sound-folding is possible. This is slow,
8584 * thus only done for the final results.
8585 */
8586 static void
8587rescore_suggestions(su)
8588 suginfo_T *su;
8589{
8590 langp_T *lp;
8591 suggest_T *stp;
8592 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008593 int i;
8594
8595 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8596 lp->lp_slang != NULL; ++lp)
8597 {
8598 if (lp->lp_slang->sl_sal.ga_len > 0)
8599 {
8600 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008601 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008602
8603 for (i = 0; i < su->su_ga.ga_len; ++i)
8604 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008605 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008606 if (!stp->st_had_bonus)
8607 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008608 stp->st_altscore = stp_sal_score(stp, su,
8609 lp->lp_slang, sal_badword);
8610 if (stp->st_altscore == SCORE_MAXMAX)
8611 stp->st_altscore = SCORE_BIG;
8612 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008613 }
8614 }
8615 break;
8616 }
8617 }
8618}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008620static int
8621#ifdef __BORLANDC__
8622_RTLENTRYF
8623#endif
8624sug_compare __ARGS((const void *s1, const void *s2));
8625
8626/*
8627 * Function given to qsort() to sort the suggestions on st_score.
8628 */
8629 static int
8630#ifdef __BORLANDC__
8631_RTLENTRYF
8632#endif
8633sug_compare(s1, s2)
8634 const void *s1;
8635 const void *s2;
8636{
8637 suggest_T *p1 = (suggest_T *)s1;
8638 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008639 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008640
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008641 if (n == 0)
8642 return p1->st_altscore - p2->st_altscore;
8643 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008644}
8645
8646/*
8647 * Cleanup the suggestions:
8648 * - Sort on score.
8649 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008650 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008651 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008652 static int
8653cleanup_suggestions(gap, maxscore, keep)
8654 garray_T *gap;
8655 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008656 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008657{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008658 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008659 int i;
8660
8661 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008662 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008663
8664 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008665 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008666 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008667 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008668 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008669 gap->ga_len = keep;
8670 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008671 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008672 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008673}
8674
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008675#if defined(FEAT_EVAL) || defined(PROTO)
8676/*
8677 * Soundfold a string, for soundfold().
8678 * Result is in allocated memory, NULL for an error.
8679 */
8680 char_u *
8681eval_soundfold(word)
8682 char_u *word;
8683{
8684 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008685 char_u sound[MAXWLEN];
8686
8687 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
8688 /* Use the sound-folding of the first language that supports it. */
8689 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8690 lp->lp_slang != NULL; ++lp)
8691 if (lp->lp_slang->sl_sal.ga_len > 0)
8692 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008693 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008694 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008695 return vim_strsave(sound);
8696 }
8697
8698 /* No language with sound folding, return word as-is. */
8699 return vim_strsave(word);
8700}
8701#endif
8702
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008703/*
8704 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
8705 */
8706 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008707spell_soundfold(slang, inword, folded, res)
8708 slang_T *slang;
8709 char_u *inword;
8710 int folded; /* "inword" is already case-folded */
8711 char_u *res;
8712{
8713 char_u fword[MAXWLEN];
8714 char_u *word;
8715
8716 if (slang->sl_sofo)
8717 /* SOFOFROM and SOFOTO used */
8718 spell_soundfold_sofo(slang, inword, res);
8719 else
8720 {
8721 /* SAL items used. Requires the word to be case-folded. */
8722 if (folded)
8723 word = inword;
8724 else
8725 {
8726 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
8727 word = fword;
8728 }
8729
8730#ifdef FEAT_MBYTE
8731 if (has_mbyte)
8732 spell_soundfold_wsal(slang, word, res);
8733 else
8734#endif
8735 spell_soundfold_sal(slang, word, res);
8736 }
8737}
8738
8739/*
8740 * Perform sound folding of "inword" into "res" according to SOFOFROM and
8741 * SOFOTO lines.
8742 */
8743 static void
8744spell_soundfold_sofo(slang, inword, res)
8745 slang_T *slang;
8746 char_u *inword;
8747 char_u *res;
8748{
8749 char_u *s;
8750 int ri = 0;
8751 int c;
8752
8753#ifdef FEAT_MBYTE
8754 if (has_mbyte)
8755 {
8756 int prevc = 0;
8757 int *ip;
8758
8759 /* The sl_sal_first[] table contains the translation for chars up to
8760 * 255, sl_sal the rest. */
8761 for (s = inword; *s != NUL; )
8762 {
8763 c = mb_ptr2char_adv(&s);
8764 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
8765 c = ' ';
8766 else if (c < 256)
8767 c = slang->sl_sal_first[c];
8768 else
8769 {
8770 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
8771 if (ip == NULL) /* empty list, can't match */
8772 c = NUL;
8773 else
8774 for (;;) /* find "c" in the list */
8775 {
8776 if (*ip == 0) /* not found */
8777 {
8778 c = NUL;
8779 break;
8780 }
8781 if (*ip == c) /* match! */
8782 {
8783 c = ip[1];
8784 break;
8785 }
8786 ip += 2;
8787 }
8788 }
8789
8790 if (c != NUL && c != prevc)
8791 {
8792 ri += mb_char2bytes(c, res + ri);
8793 if (ri + MB_MAXBYTES > MAXWLEN)
8794 break;
8795 prevc = c;
8796 }
8797 }
8798 }
8799 else
8800#endif
8801 {
8802 /* The sl_sal_first[] table contains the translation. */
8803 for (s = inword; (c = *s) != NUL; ++s)
8804 {
8805 if (vim_iswhite(c))
8806 c = ' ';
8807 else
8808 c = slang->sl_sal_first[c];
8809 if (c != NUL && (ri == 0 || res[ri - 1] != c))
8810 res[ri++] = c;
8811 }
8812 }
8813
8814 res[ri] = NUL;
8815}
8816
8817 static void
8818spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008819 slang_T *slang;
8820 char_u *inword;
8821 char_u *res;
8822{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008823 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008824 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008825 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008826 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008827 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008828 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008829 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008830 int n, k = 0;
8831 int z0;
8832 int k0;
8833 int n0;
8834 int c;
8835 int pri;
8836 int p0 = -333;
8837 int c0;
8838
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008839 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008840 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008841 if (slang->sl_rem_accents)
8842 {
8843 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008844 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008845 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008846 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008847 {
8848 *t++ = ' ';
8849 s = skipwhite(s);
8850 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008851 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008852 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008853 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008854 *t++ = *s;
8855 ++s;
8856 }
8857 }
8858 *t = NUL;
8859 }
8860 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008861 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008862
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008863 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008864
8865 /*
8866 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008867 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008868 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008869 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008870 while ((c = word[i]) != NUL)
8871 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008872 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008873 n = slang->sl_sal_first[c];
8874 z0 = 0;
8875
8876 if (n >= 0)
8877 {
8878 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008879 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008880 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008881 /* Quickly skip entries that don't match the word. Most
8882 * entries are less then three chars, optimize for that. */
8883 k = smp[n].sm_leadlen;
8884 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008885 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008886 if (word[i + 1] != s[1])
8887 continue;
8888 if (k > 2)
8889 {
8890 for (j = 2; j < k; ++j)
8891 if (word[i + j] != s[j])
8892 break;
8893 if (j < k)
8894 continue;
8895 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008896 }
8897
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008898 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008899 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008900 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008901 while (*pf != NUL && *pf != word[i + k])
8902 ++pf;
8903 if (*pf == NUL)
8904 continue;
8905 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008906 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008907 s = smp[n].sm_rules;
8908 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008909
8910 p0 = *s;
8911 k0 = k;
8912 while (*s == '-' && k > 1)
8913 {
8914 k--;
8915 s++;
8916 }
8917 if (*s == '<')
8918 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008919 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008920 {
8921 /* determine priority */
8922 pri = *s - '0';
8923 s++;
8924 }
8925 if (*s == '^' && *(s + 1) == '^')
8926 s++;
8927
8928 if (*s == NUL
8929 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008930 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008931 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008932 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008933 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008935 && spell_iswordp(word + i - 1, curbuf)
8936 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008937 {
8938 /* search for followup rules, if: */
8939 /* followup and k > 1 and NO '-' in searchstring */
8940 c0 = word[i + k - 1];
8941 n0 = slang->sl_sal_first[c0];
8942
8943 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008944 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008945 {
8946 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008947 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008948 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008949 /* Quickly skip entries that don't match the word.
8950 * */
8951 k0 = smp[n0].sm_leadlen;
8952 if (k0 > 1)
8953 {
8954 if (word[i + k] != s[1])
8955 continue;
8956 if (k0 > 2)
8957 {
8958 pf = word + i + k + 1;
8959 for (j = 2; j < k0; ++j)
8960 if (*pf++ != s[j])
8961 break;
8962 if (j < k0)
8963 continue;
8964 }
8965 }
8966 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008967
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008968 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008969 {
8970 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008971 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008972 while (*pf != NUL && *pf != word[i + k0])
8973 ++pf;
8974 if (*pf == NUL)
8975 continue;
8976 ++k0;
8977 }
8978
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008979 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008980 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008981 while (*s == '-')
8982 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008983 /* "k0" gets NOT reduced because
8984 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008985 s++;
8986 }
8987 if (*s == '<')
8988 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008989 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 {
8991 p0 = *s - '0';
8992 s++;
8993 }
8994
8995 if (*s == NUL
8996 /* *s == '^' cuts */
8997 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008998 && !spell_iswordp(word + i + k0,
8999 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009000 {
9001 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009002 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009003 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009004
9005 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009006 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009007 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009008 /* rule fits; stop search */
9009 break;
9010 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009011 }
9012
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009013 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009014 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009015 }
9016
9017 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009018 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009019 if (s == NULL)
9020 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009021 pf = smp[n].sm_rules;
9022 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009023 if (p0 == 1 && z == 0)
9024 {
9025 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009026 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
9027 || res[reslen - 1] == *s))
9028 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009029 z0 = 1;
9030 z = 1;
9031 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009032 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009033 {
9034 word[i + k0] = *s;
9035 k0++;
9036 s++;
9037 }
9038 if (k > k0)
9039 mch_memmove(word + i + k0, word + i + k,
9040 STRLEN(word + i + k) + 1);
9041
9042 /* new "actual letter" */
9043 c = word[i];
9044 }
9045 else
9046 {
9047 /* no '<' rule used */
9048 i += k - 1;
9049 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009050 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009051 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009052 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009053 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009054 s++;
9055 }
9056 /* new "actual letter" */
9057 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009058 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009059 {
9060 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009061 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009062 mch_memmove(word, word + i + 1,
9063 STRLEN(word + i + 1) + 1);
9064 i = 0;
9065 z0 = 1;
9066 }
9067 }
9068 break;
9069 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009070 }
9071 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009072 else if (vim_iswhite(c))
9073 {
9074 c = ' ';
9075 k = 1;
9076 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009077
9078 if (z0 == 0)
9079 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009080 if (k && !p0 && reslen < MAXWLEN && c != NUL
9081 && (!slang->sl_collapse || reslen == 0
9082 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009083 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009084 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009085
9086 i++;
9087 z = 0;
9088 k = 0;
9089 }
9090 }
9091
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009092 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009093}
9094
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009095#ifdef FEAT_MBYTE
9096/*
9097 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
9098 * Multi-byte version of spell_soundfold().
9099 */
9100 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009101spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009102 slang_T *slang;
9103 char_u *inword;
9104 char_u *res;
9105{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009106 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009107 int word[MAXWLEN];
9108 int wres[MAXWLEN];
9109 int l;
9110 char_u *s;
9111 int *ws;
9112 char_u *t;
9113 int *pf;
9114 int i, j, z;
9115 int reslen;
9116 int n, k = 0;
9117 int z0;
9118 int k0;
9119 int n0;
9120 int c;
9121 int pri;
9122 int p0 = -333;
9123 int c0;
9124 int did_white = FALSE;
9125
9126 /*
9127 * Convert the multi-byte string to a wide-character string.
9128 * Remove accents, if wanted. We actually remove all non-word characters.
9129 * But keep white space.
9130 */
9131 n = 0;
9132 for (s = inword; *s != NUL; )
9133 {
9134 t = s;
9135 c = mb_ptr2char_adv(&s);
9136 if (slang->sl_rem_accents)
9137 {
9138 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
9139 {
9140 if (did_white)
9141 continue;
9142 c = ' ';
9143 did_white = TRUE;
9144 }
9145 else
9146 {
9147 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009148 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009149 continue;
9150 }
9151 }
9152 word[n++] = c;
9153 }
9154 word[n] = NUL;
9155
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009156 /*
9157 * This comes from Aspell phonet.cpp.
9158 * Converted from C++ to C. Added support for multi-byte chars.
9159 * Changed to keep spaces.
9160 */
9161 i = reslen = z = 0;
9162 while ((c = word[i]) != NUL)
9163 {
9164 /* Start with the first rule that has the character in the word. */
9165 n = slang->sl_sal_first[c & 0xff];
9166 z0 = 0;
9167
9168 if (n >= 0)
9169 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009170 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009171 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
9172 {
9173 /* Quickly skip entries that don't match the word. Most
9174 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009175 if (c != ws[0])
9176 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009177 k = smp[n].sm_leadlen;
9178 if (k > 1)
9179 {
9180 if (word[i + 1] != ws[1])
9181 continue;
9182 if (k > 2)
9183 {
9184 for (j = 2; j < k; ++j)
9185 if (word[i + j] != ws[j])
9186 break;
9187 if (j < k)
9188 continue;
9189 }
9190 }
9191
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009192 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009193 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009194 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009195 while (*pf != NUL && *pf != word[i + k])
9196 ++pf;
9197 if (*pf == NUL)
9198 continue;
9199 ++k;
9200 }
9201 s = smp[n].sm_rules;
9202 pri = 5; /* default priority */
9203
9204 p0 = *s;
9205 k0 = k;
9206 while (*s == '-' && k > 1)
9207 {
9208 k--;
9209 s++;
9210 }
9211 if (*s == '<')
9212 s++;
9213 if (VIM_ISDIGIT(*s))
9214 {
9215 /* determine priority */
9216 pri = *s - '0';
9217 s++;
9218 }
9219 if (*s == '^' && *(s + 1) == '^')
9220 s++;
9221
9222 if (*s == NUL
9223 || (*s == '^'
9224 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009225 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009226 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009227 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009228 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009229 && spell_iswordp_w(word + i - 1, curbuf)
9230 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009231 {
9232 /* search for followup rules, if: */
9233 /* followup and k > 1 and NO '-' in searchstring */
9234 c0 = word[i + k - 1];
9235 n0 = slang->sl_sal_first[c0 & 0xff];
9236
9237 if (slang->sl_followup && k > 1 && n0 >= 0
9238 && p0 != '-' && word[i + k] != NUL)
9239 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009240 /* Test follow-up rule for "word[i + k]"; loop over
9241 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009242 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
9243 == (c0 & 0xff); ++n0)
9244 {
9245 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009246 */
9247 if (c0 != ws[0])
9248 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009249 k0 = smp[n0].sm_leadlen;
9250 if (k0 > 1)
9251 {
9252 if (word[i + k] != ws[1])
9253 continue;
9254 if (k0 > 2)
9255 {
9256 pf = word + i + k + 1;
9257 for (j = 2; j < k0; ++j)
9258 if (*pf++ != ws[j])
9259 break;
9260 if (j < k0)
9261 continue;
9262 }
9263 }
9264 k0 += k - 1;
9265
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009266 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009267 {
9268 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009269 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009270 while (*pf != NUL && *pf != word[i + k0])
9271 ++pf;
9272 if (*pf == NUL)
9273 continue;
9274 ++k0;
9275 }
9276
9277 p0 = 5;
9278 s = smp[n0].sm_rules;
9279 while (*s == '-')
9280 {
9281 /* "k0" gets NOT reduced because
9282 * "if (k0 == k)" */
9283 s++;
9284 }
9285 if (*s == '<')
9286 s++;
9287 if (VIM_ISDIGIT(*s))
9288 {
9289 p0 = *s - '0';
9290 s++;
9291 }
9292
9293 if (*s == NUL
9294 /* *s == '^' cuts */
9295 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009296 && !spell_iswordp_w(word + i + k0,
9297 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009298 {
9299 if (k0 == k)
9300 /* this is just a piece of the string */
9301 continue;
9302
9303 if (p0 < pri)
9304 /* priority too low */
9305 continue;
9306 /* rule fits; stop search */
9307 break;
9308 }
9309 }
9310
9311 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
9312 == (c0 & 0xff))
9313 continue;
9314 }
9315
9316 /* replace string */
9317 ws = smp[n].sm_to_w;
9318 s = smp[n].sm_rules;
9319 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
9320 if (p0 == 1 && z == 0)
9321 {
9322 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009323 if (reslen > 0 && ws != NULL && *ws != NUL
9324 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009325 || wres[reslen - 1] == *ws))
9326 reslen--;
9327 z0 = 1;
9328 z = 1;
9329 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009330 if (ws != NULL)
9331 while (*ws != NUL && word[i + k0] != NUL)
9332 {
9333 word[i + k0] = *ws;
9334 k0++;
9335 ws++;
9336 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009337 if (k > k0)
9338 mch_memmove(word + i + k0, word + i + k,
9339 sizeof(int) * (STRLEN(word + i + k) + 1));
9340
9341 /* new "actual letter" */
9342 c = word[i];
9343 }
9344 else
9345 {
9346 /* no '<' rule used */
9347 i += k - 1;
9348 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009349 if (ws != NULL)
9350 while (*ws != NUL && ws[1] != NUL
9351 && reslen < MAXWLEN)
9352 {
9353 if (reslen == 0 || wres[reslen - 1] != *ws)
9354 wres[reslen++] = *ws;
9355 ws++;
9356 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009357 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009358 if (ws == NULL)
9359 c = NUL;
9360 else
9361 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009362 if (strstr((char *)s, "^^") != NULL)
9363 {
9364 if (c != NUL)
9365 wres[reslen++] = c;
9366 mch_memmove(word, word + i + 1,
9367 sizeof(int) * (STRLEN(word + i + 1) + 1));
9368 i = 0;
9369 z0 = 1;
9370 }
9371 }
9372 break;
9373 }
9374 }
9375 }
9376 else if (vim_iswhite(c))
9377 {
9378 c = ' ';
9379 k = 1;
9380 }
9381
9382 if (z0 == 0)
9383 {
9384 if (k && !p0 && reslen < MAXWLEN && c != NUL
9385 && (!slang->sl_collapse || reslen == 0
9386 || wres[reslen - 1] != c))
9387 /* condense only double letters */
9388 wres[reslen++] = c;
9389
9390 i++;
9391 z = 0;
9392 k = 0;
9393 }
9394 }
9395
9396 /* Convert wide characters in "wres" to a multi-byte string in "res". */
9397 l = 0;
9398 for (n = 0; n < reslen; ++n)
9399 {
9400 l += mb_char2bytes(wres[n], res + l);
9401 if (l + MB_MAXBYTES > MAXWLEN)
9402 break;
9403 }
9404 res[l] = NUL;
9405}
9406#endif
9407
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009408/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009409 * Compute a score for two sound-a-like words.
9410 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
9411 * Instead of a generic loop we write out the code. That keeps it fast by
9412 * avoiding checks that will not be possible.
9413 */
9414 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009415soundalike_score(goodstart, badstart)
9416 char_u *goodstart; /* sound-folded good word */
9417 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009418{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009419 char_u *goodsound = goodstart;
9420 char_u *badsound = badstart;
9421 int goodlen;
9422 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009423 int n;
9424 char_u *pl, *ps;
9425 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009426 int score = 0;
9427
9428 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
9429 * counted so much, vowels halfway the word aren't counted at all. */
9430 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
9431 {
9432 score = SCORE_DEL / 2;
9433 if (*badsound == '*')
9434 ++badsound;
9435 else
9436 ++goodsound;
9437 }
9438
9439 goodlen = STRLEN(goodsound);
9440 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009441
9442 /* Return quickly if the lenghts are too different to be fixed by two
9443 * changes. */
9444 n = goodlen - badlen;
9445 if (n < -2 || n > 2)
9446 return SCORE_MAXMAX;
9447
9448 if (n > 0)
9449 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009450 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009451 ps = badsound;
9452 }
9453 else
9454 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009455 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009456 ps = goodsound;
9457 }
9458
9459 /* Skip over the identical part. */
9460 while (*pl == *ps && *pl != NUL)
9461 {
9462 ++pl;
9463 ++ps;
9464 }
9465
9466 switch (n)
9467 {
9468 case -2:
9469 case 2:
9470 /*
9471 * Must delete two characters from "pl".
9472 */
9473 ++pl; /* first delete */
9474 while (*pl == *ps)
9475 {
9476 ++pl;
9477 ++ps;
9478 }
9479 /* strings must be equal after second delete */
9480 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009481 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009482
9483 /* Failed to compare. */
9484 break;
9485
9486 case -1:
9487 case 1:
9488 /*
9489 * Minimal one delete from "pl" required.
9490 */
9491
9492 /* 1: delete */
9493 pl2 = pl + 1;
9494 ps2 = ps;
9495 while (*pl2 == *ps2)
9496 {
9497 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009498 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009499 ++pl2;
9500 ++ps2;
9501 }
9502
9503 /* 2: delete then swap, then rest must be equal */
9504 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9505 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009506 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009507
9508 /* 3: delete then substitute, then the rest must be equal */
9509 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009510 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009511
9512 /* 4: first swap then delete */
9513 if (pl[0] == ps[1] && pl[1] == ps[0])
9514 {
9515 pl2 = pl + 2; /* swap, skip two chars */
9516 ps2 = ps + 2;
9517 while (*pl2 == *ps2)
9518 {
9519 ++pl2;
9520 ++ps2;
9521 }
9522 /* delete a char and then strings must be equal */
9523 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009524 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009525 }
9526
9527 /* 5: first substitute then delete */
9528 pl2 = pl + 1; /* substitute, skip one char */
9529 ps2 = ps + 1;
9530 while (*pl2 == *ps2)
9531 {
9532 ++pl2;
9533 ++ps2;
9534 }
9535 /* delete a char and then strings must be equal */
9536 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009537 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009538
9539 /* Failed to compare. */
9540 break;
9541
9542 case 0:
9543 /*
9544 * Lenghts are equal, thus changes must result in same length: An
9545 * insert is only possible in combination with a delete.
9546 * 1: check if for identical strings
9547 */
9548 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009549 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009550
9551 /* 2: swap */
9552 if (pl[0] == ps[1] && pl[1] == ps[0])
9553 {
9554 pl2 = pl + 2; /* swap, skip two chars */
9555 ps2 = ps + 2;
9556 while (*pl2 == *ps2)
9557 {
9558 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009559 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009560 ++pl2;
9561 ++ps2;
9562 }
9563 /* 3: swap and swap again */
9564 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9565 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009566 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009567
9568 /* 4: swap and substitute */
9569 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009570 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009571 }
9572
9573 /* 5: substitute */
9574 pl2 = pl + 1;
9575 ps2 = ps + 1;
9576 while (*pl2 == *ps2)
9577 {
9578 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009579 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009580 ++pl2;
9581 ++ps2;
9582 }
9583
9584 /* 6: substitute and swap */
9585 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9586 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009587 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009588
9589 /* 7: substitute and substitute */
9590 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009591 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009592
9593 /* 8: insert then delete */
9594 pl2 = pl;
9595 ps2 = ps + 1;
9596 while (*pl2 == *ps2)
9597 {
9598 ++pl2;
9599 ++ps2;
9600 }
9601 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009602 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009603
9604 /* 9: delete then insert */
9605 pl2 = pl + 1;
9606 ps2 = ps;
9607 while (*pl2 == *ps2)
9608 {
9609 ++pl2;
9610 ++ps2;
9611 }
9612 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009613 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009614
9615 /* Failed to compare. */
9616 break;
9617 }
9618
9619 return SCORE_MAXMAX;
9620}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009622/*
9623 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009624 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009625 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009626 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009627 * It has been converted from C++ to C and modified to support multi-byte
9628 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009629 */
9630 static int
9631spell_edit_score(badword, goodword)
9632 char_u *badword;
9633 char_u *goodword;
9634{
9635 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009636 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009637 int j, i;
9638 int t;
9639 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009640 int pbc, pgc;
9641#ifdef FEAT_MBYTE
9642 char_u *p;
9643 int wbadword[MAXWLEN];
9644 int wgoodword[MAXWLEN];
9645
9646 if (has_mbyte)
9647 {
9648 /* Get the characters from the multi-byte strings and put them in an
9649 * int array for easy access. */
9650 for (p = badword, badlen = 0; *p != NUL; )
9651 wbadword[badlen++] = mb_ptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00009652 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009653 for (p = goodword, goodlen = 0; *p != NUL; )
9654 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00009655 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009656 }
9657 else
9658#endif
9659 {
9660 badlen = STRLEN(badword) + 1;
9661 goodlen = STRLEN(goodword) + 1;
9662 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009663
9664 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
9665#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009666 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
9667 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009668 if (cnt == NULL)
9669 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009670
9671 CNT(0, 0) = 0;
9672 for (j = 1; j <= goodlen; ++j)
9673 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
9674
9675 for (i = 1; i <= badlen; ++i)
9676 {
9677 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
9678 for (j = 1; j <= goodlen; ++j)
9679 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009680#ifdef FEAT_MBYTE
9681 if (has_mbyte)
9682 {
9683 bc = wbadword[i - 1];
9684 gc = wgoodword[j - 1];
9685 }
9686 else
9687#endif
9688 {
9689 bc = badword[i - 1];
9690 gc = goodword[j - 1];
9691 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009692 if (bc == gc)
9693 CNT(i, j) = CNT(i - 1, j - 1);
9694 else
9695 {
9696 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009697 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009698 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
9699 else
9700 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
9701
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009702 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009703 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009704#ifdef FEAT_MBYTE
9705 if (has_mbyte)
9706 {
9707 pbc = wbadword[i - 2];
9708 pgc = wgoodword[j - 2];
9709 }
9710 else
9711#endif
9712 {
9713 pbc = badword[i - 2];
9714 pgc = goodword[j - 2];
9715 }
9716 if (bc == pgc && pbc == gc)
9717 {
9718 t = SCORE_SWAP + CNT(i - 2, j - 2);
9719 if (t < CNT(i, j))
9720 CNT(i, j) = t;
9721 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009722 }
9723 t = SCORE_DEL + CNT(i - 1, j);
9724 if (t < CNT(i, j))
9725 CNT(i, j) = t;
9726 t = SCORE_INS + CNT(i, j - 1);
9727 if (t < CNT(i, j))
9728 CNT(i, j) = t;
9729 }
9730 }
9731 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009732
9733 i = CNT(badlen - 1, goodlen - 1);
9734 vim_free(cnt);
9735 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009736}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009737
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009738/*
9739 * ":spelldump"
9740 */
9741/*ARGSUSED*/
9742 void
9743ex_spelldump(eap)
9744 exarg_T *eap;
9745{
9746 buf_T *buf = curbuf;
9747 langp_T *lp;
9748 slang_T *slang;
9749 idx_T arridx[MAXWLEN];
9750 int curi[MAXWLEN];
9751 char_u word[MAXWLEN];
9752 int c;
9753 char_u *byts;
9754 idx_T *idxs;
9755 linenr_T lnum = 0;
9756 int round;
9757 int depth;
9758 int n;
9759 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009760 char_u *region_names = NULL; /* region names being used */
9761 int do_region = TRUE; /* dump region names and numbers */
9762 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009763
9764 if (no_spell_checking())
9765 return;
9766
9767 /* Create a new empty buffer by splitting the window. */
9768 do_cmdline_cmd((char_u *)"new");
9769 if (!bufempty() || !buf_valid(buf))
9770 return;
9771
Bram Moolenaar7887d882005-07-01 22:33:52 +00009772 /* Find out if we can support regions: All languages must support the same
9773 * regions or none at all. */
9774 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9775 {
9776 p = lp->lp_slang->sl_regions;
9777 if (p[0] != 0)
9778 {
9779 if (region_names == NULL) /* first language with regions */
9780 region_names = p;
9781 else if (STRCMP(region_names, p) != 0)
9782 {
9783 do_region = FALSE; /* region names are different */
9784 break;
9785 }
9786 }
9787 }
9788
9789 if (do_region && region_names != NULL)
9790 {
9791 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
9792 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9793 }
9794 else
9795 do_region = FALSE;
9796
9797 /*
9798 * Loop over all files loaded for the entries in 'spelllang'.
9799 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009800 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9801 {
9802 slang = lp->lp_slang;
9803
9804 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
9805 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9806
9807 /* round 1: case-folded tree
9808 * round 2: keep-case tree */
9809 for (round = 1; round <= 2; ++round)
9810 {
9811 if (round == 1)
9812 {
9813 byts = slang->sl_fbyts;
9814 idxs = slang->sl_fidxs;
9815 }
9816 else
9817 {
9818 byts = slang->sl_kbyts;
9819 idxs = slang->sl_kidxs;
9820 }
9821 if (byts == NULL)
9822 continue; /* array is empty */
9823
9824 depth = 0;
9825 arridx[0] = 0;
9826 curi[0] = 1;
9827 while (depth >= 0 && !got_int)
9828 {
9829 if (curi[depth] > byts[arridx[depth]])
9830 {
9831 /* Done all bytes at this node, go up one level. */
9832 --depth;
9833 line_breakcheck();
9834 }
9835 else
9836 {
9837 /* Do one more byte at this node. */
9838 n = arridx[depth] + curi[depth];
9839 ++curi[depth];
9840 c = byts[n];
9841 if (c == 0)
9842 {
9843 /* End of word, deal with the word.
9844 * Don't use keep-case words in the fold-case tree,
9845 * they will appear in the keep-case tree.
9846 * Only use the word when the region matches. */
9847 flags = (int)idxs[n];
9848 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009849 && (do_region
9850 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009851 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009852 & lp->lp_region) != 0))
9853 {
9854 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009855 if (!do_region)
9856 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009857
9858 /* Dump the basic word if there is no prefix or
9859 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009860 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009861 if (c == 0 || curi[depth] == 2)
9862 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009863
9864 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009865 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009866 lnum = apply_prefixes(slang, word, round,
9867 flags, lnum);
9868 }
9869 }
9870 else
9871 {
9872 /* Normal char, go one level deeper. */
9873 word[depth++] = c;
9874 arridx[depth] = idxs[n];
9875 curi[depth] = 1;
9876 }
9877 }
9878 }
9879 }
9880 }
9881
9882 /* Delete the empty line that we started with. */
9883 if (curbuf->b_ml.ml_line_count > 1)
9884 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
9885
9886 redraw_later(NOT_VALID);
9887}
9888
9889/*
9890 * Dump one word: apply case modifications and append a line to the buffer.
9891 */
9892 static void
9893dump_word(word, round, flags, lnum)
9894 char_u *word;
9895 int round;
9896 int flags;
9897 linenr_T lnum;
9898{
9899 int keepcap = FALSE;
9900 char_u *p;
9901 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00009902 char_u badword[MAXWLEN + 10];
9903 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009904
9905 if (round == 1 && (flags & WF_CAPMASK) != 0)
9906 {
9907 /* Need to fix case according to "flags". */
9908 make_case_word(word, cword, flags);
9909 p = cword;
9910 }
9911 else
9912 {
9913 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009914 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
9915 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009916 keepcap = TRUE;
9917 }
9918
Bram Moolenaar7887d882005-07-01 22:33:52 +00009919 /* Add flags and regions after a slash. */
9920 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009921 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009922 STRCPY(badword, p);
9923 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009924 if (keepcap)
9925 STRCAT(badword, "=");
9926 if (flags & WF_BANNED)
9927 STRCAT(badword, "!");
9928 else if (flags & WF_RARE)
9929 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009930 if (flags & WF_REGION)
9931 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009932 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +00009933 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009934 p = badword;
9935 }
9936
9937 ml_append(lnum, p, (colnr_T)0, FALSE);
9938}
9939
9940/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009941 * For ":spelldump": Find matching prefixes for "word". Prepend each to
9942 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009943 * Return the updated line number.
9944 */
9945 static linenr_T
9946apply_prefixes(slang, word, round, flags, startlnum)
9947 slang_T *slang;
9948 char_u *word; /* case-folded word */
9949 int round;
9950 int flags; /* flags with prefix ID */
9951 linenr_T startlnum;
9952{
9953 idx_T arridx[MAXWLEN];
9954 int curi[MAXWLEN];
9955 char_u prefix[MAXWLEN];
9956 int c;
9957 char_u *byts;
9958 idx_T *idxs;
9959 linenr_T lnum = startlnum;
9960 int depth;
9961 int n;
9962 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009963 int i;
9964
9965 byts = slang->sl_pbyts;
9966 idxs = slang->sl_pidxs;
9967 if (byts != NULL) /* array not is empty */
9968 {
9969 /*
9970 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009971 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009972 */
9973 depth = 0;
9974 arridx[0] = 0;
9975 curi[0] = 1;
9976 while (depth >= 0 && !got_int)
9977 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009978 n = arridx[depth];
9979 len = byts[n];
9980 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009981 {
9982 /* Done all bytes at this node, go up one level. */
9983 --depth;
9984 line_breakcheck();
9985 }
9986 else
9987 {
9988 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009989 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009990 ++curi[depth];
9991 c = byts[n];
9992 if (c == 0)
9993 {
9994 /* End of prefix, find out how many IDs there are. */
9995 for (i = 1; i < len; ++i)
9996 if (byts[n + i] != 0)
9997 break;
9998 curi[depth] += i - 1;
9999
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010000 i = valid_word_prefix(i, n, flags, word, slang);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010001 if (i != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010002 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010003 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010004 dump_word(prefix, round,
10005 (i & WF_RAREPFX) ? (flags | WF_RARE)
10006 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010007 }
10008 }
10009 else
10010 {
10011 /* Normal char, go one level deeper. */
10012 prefix[depth++] = c;
10013 arridx[depth] = idxs[n];
10014 curi[depth] = 1;
10015 }
10016 }
10017 }
10018 }
10019
10020 return lnum;
10021}
10022
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000010023#endif /* FEAT_SYN_HL */