blob: 52b7b4aa608cd6d8866f9f703987828727138185 [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
675/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
676 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
677 * the "w" library function for characters above 255 if available. */
678# ifdef HAVE_TOWLOWER
679# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
680 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
681# else
682# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
683 : (c) < 256 ? spelltab.st_fold[c] : (c))
684# endif
685
686# ifdef HAVE_TOWUPPER
687# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
688 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
689# else
690# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
691 : (c) < 256 ? spelltab.st_upper[c] : (c))
692# endif
693
694# ifdef HAVE_ISWUPPER
695# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
696 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
697# else
698# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
699 : (c) < 256 ? spelltab.st_isu[c] : (c))
700# endif
701#endif
702
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000703
704static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000705static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000706
707/*
708 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000709 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000710 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
711 * or when it's OK it remains unchanged.
712 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000713 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000714 * "capcol" is used to check for a Capitalised word after the end of a
715 * sentence. If it's zero then perform the check. Return the column where to
716 * check next, or -1 when no sentence end was found. If it's NULL then don't
717 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000718 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000719 * Returns the length of the word in bytes, also when it's OK, so that the
720 * caller can skip over the word.
721 */
722 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000723spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000724 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000725 char_u *ptr;
726 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000727 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000728{
729 matchinf_T mi; /* Most things are put in "mi" so that it can
730 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000731 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000732 int c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000733
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000734 /* A word never starts at a space or a control character. Return quickly
735 * then, skipping over the character. */
736 if (*ptr <= ' ')
737 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000738
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000739 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000740 * 0X99FF. But when a word character follows do check spelling to find
741 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000742 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000743 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000744 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
745 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000746 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000747 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000748 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000749 nrlen = mi.mi_end - ptr;
750 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000751 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000752 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000753
754 /* Try including the digits in the word. */
755 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000756 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000757 else
758 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000759
Bram Moolenaar0c405862005-06-22 22:26:26 +0000760 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000761 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000762 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000763 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000764 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000765 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000766 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000767 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000768
769 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
770 {
771 /* Check word starting with capital letter. */
772#ifdef FEAT_MBYTE
773 c = mb_ptr2char(ptr);
774#else
775 c = *ptr;
776#endif
777 if (!SPELL_ISUPPER(c))
778 {
779 *attrp = highlight_attr[HLF_SPC];
780 return (int)(mi.mi_fend - ptr);
781 }
782 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000783 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000784 if (capcol != NULL)
785 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000786
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000787 /* We always use the characters up to the next non-word character,
788 * also for bad words. */
789 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000790
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000791 /* Check caps type later. */
792 mi.mi_capflags = 0;
793 mi.mi_cend = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000794 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000795
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000796 /* Include one non-word character so that we can check for the
797 * word end. */
798 if (*mi.mi_fend != NUL)
799 mb_ptr_adv(mi.mi_fend);
800
801 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
802 MAXWLEN + 1);
803 mi.mi_fwordlen = STRLEN(mi.mi_fword);
804
805 /* The word is bad unless we recognize it. */
806 mi.mi_result = SP_BAD;
807
808 /*
809 * Loop over the languages specified in 'spelllang'.
810 * We check them all, because a matching word may be longer than an
811 * already found matching word.
812 */
813 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
814 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
815 {
816 /* Check for a matching word in case-folded words. */
817 find_word(&mi, FIND_FOLDWORD);
818
819 /* Check for a matching word in keep-case words. */
820 find_word(&mi, FIND_KEEPWORD);
821
822 /* Check for matching prefixes. */
823 find_prefix(&mi);
824 }
825
826 if (mi.mi_result != SP_OK)
827 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000828 /* If we found a number skip over it. Allows for "42nd". Do flag
829 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000830 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000831 {
832 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
833 return nrlen;
834 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000835
836 /* When we are at a non-word character there is no error, just
837 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000838 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000839 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000840 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
841 {
842 regmatch_T regmatch;
843
844 /* Check for end of sentence. */
845 regmatch.regprog = wp->w_buffer->b_cap_prog;
846 regmatch.rm_ic = FALSE;
847 if (vim_regexec(&regmatch, ptr, 0))
848 *capcol = (int)(regmatch.endp[0] - ptr);
849 }
850
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000851#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000852 if (has_mbyte)
853 return mb_ptr2len_check(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000854#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000855 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000856 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000857
858 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
859 *attrp = highlight_attr[HLF_SPB];
860 else if (mi.mi_result == SP_RARE)
861 *attrp = highlight_attr[HLF_SPR];
862 else
863 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000864 }
865
Bram Moolenaar51485f02005-06-04 21:55:20 +0000866 return (int)(mi.mi_end - ptr);
867}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000868
Bram Moolenaar51485f02005-06-04 21:55:20 +0000869/*
870 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000871 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
872 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
873 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
874 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000875 *
876 * For a match mip->mi_result is updated.
877 */
878 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000879find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000880 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000881 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000882{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000883 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000884 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000885 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000886 int endidxcnt = 0;
887 int len;
888 int wlen = 0;
889 int flen;
890 int c;
891 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000892 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000893#ifdef FEAT_MBYTE
894 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000895 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000896#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000897 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000898 slang_T *slang = mip->mi_lp->lp_slang;
899 unsigned flags;
900 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000901 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000902
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000903 if (mode == FIND_KEEPWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000904 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000905 /* Check for word with matching case in keep-case tree. */
906 ptr = mip->mi_word;
907 flen = 9999; /* no case folding, always enough bytes */
908 byts = slang->sl_kbyts;
909 idxs = slang->sl_kidxs;
910 }
911 else
912 {
913 /* Check for case-folded in case-folded tree. */
914 ptr = mip->mi_fword;
915 flen = mip->mi_fwordlen; /* available case-folded bytes */
916 byts = slang->sl_fbyts;
917 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000918
919 if (mode == FIND_PREFIX)
920 {
921 /* Skip over the prefix. */
922 wlen = mip->mi_prefixlen;
923 flen -= mip->mi_prefixlen;
924 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000925 }
926
Bram Moolenaar51485f02005-06-04 21:55:20 +0000927 if (byts == NULL)
928 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000929
Bram Moolenaar51485f02005-06-04 21:55:20 +0000930 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000931 * Repeat advancing in the tree until:
932 * - there is a byte that doesn't match,
933 * - we reach the end of the tree,
934 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000935 */
936 for (;;)
937 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000938 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000939 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000940
941 len = byts[arridx++];
942
943 /* If the first possible byte is a zero the word could end here.
944 * Remember this index, we first check for the longest word. */
945 if (byts[arridx] == 0)
946 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000947 if (endidxcnt == MAXWLEN)
948 {
949 /* Must be a corrupted spell file. */
950 EMSG(_(e_format));
951 return;
952 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000953 endlen[endidxcnt] = wlen;
954 endidx[endidxcnt++] = arridx++;
955 --len;
956
957 /* Skip over the zeros, there can be several flag/region
958 * combinations. */
959 while (len > 0 && byts[arridx] == 0)
960 {
961 ++arridx;
962 --len;
963 }
964 if (len == 0)
965 break; /* no children, word must end here */
966 }
967
968 /* Stop looking at end of the line. */
969 if (ptr[wlen] == NUL)
970 break;
971
972 /* Perform a binary search in the list of accepted bytes. */
973 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000974 if (c == TAB) /* <Tab> is handled like <Space> */
975 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000976 lo = arridx;
977 hi = arridx + len - 1;
978 while (lo < hi)
979 {
980 m = (lo + hi) / 2;
981 if (byts[m] > c)
982 hi = m - 1;
983 else if (byts[m] < c)
984 lo = m + 1;
985 else
986 {
987 lo = hi = m;
988 break;
989 }
990 }
991
992 /* Stop if there is no matching byte. */
993 if (hi < lo || byts[lo] != c)
994 break;
995
996 /* Continue at the child (if there is one). */
997 arridx = idxs[lo];
998 ++wlen;
999 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001000
1001 /* One space in the good word may stand for several spaces in the
1002 * checked word. */
1003 if (c == ' ')
1004 {
1005 for (;;)
1006 {
1007 if (flen <= 0 && *mip->mi_fend != NUL)
1008 flen = fold_more(mip);
1009 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1010 break;
1011 ++wlen;
1012 --flen;
1013 }
1014 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001015 }
1016
1017 /*
1018 * Verify that one of the possible endings is valid. Try the longest
1019 * first.
1020 */
1021 while (endidxcnt > 0)
1022 {
1023 --endidxcnt;
1024 arridx = endidx[endidxcnt];
1025 wlen = endlen[endidxcnt];
1026
1027#ifdef FEAT_MBYTE
1028 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1029 continue; /* not at first byte of character */
1030#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001031 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001032 continue; /* next char is a word character */
1033
1034#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001035 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001036 {
1037 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001038 * when folding case. This can be slow, take a shortcut when the
1039 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001040 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001041 if (STRNCMP(ptr, p, wlen) != 0)
1042 {
1043 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1044 mb_ptr_adv(p);
1045 wlen = p - mip->mi_word;
1046 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001047 }
1048#endif
1049
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001050 /* Check flags and region. For FIND_PREFIX check the condition and
1051 * prefix ID.
1052 * Repeat this if there are more flags/region alternatives until there
1053 * is a match. */
1054 res = SP_BAD;
1055 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1056 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001057 {
1058 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001059
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001060 /* For the fold-case tree check that the case of the checked word
1061 * matches with what the word in the tree requires.
1062 * For keep-case tree the case is always right. For prefixes we
1063 * don't bother to check. */
1064 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001065 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001066 if (mip->mi_cend != mip->mi_word + wlen)
1067 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001068 /* mi_capflags was set for a different word length, need
1069 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001070 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001071 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001072 }
1073
Bram Moolenaar0c405862005-06-22 22:26:26 +00001074 if (mip->mi_capflags == WF_KEEPCAP
1075 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001076 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001077 }
1078
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001079 /* When mode is FIND_PREFIX the word must support the prefix:
1080 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001081 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001082 if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001083 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001084 /* The prefix ID is stored two bytes above the flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001085 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001086 flags,
1087 mip->mi_fword + mip->mi_prefixlen, slang);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001088 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001089 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001090
1091 /* Use the WF_RARE flag for a rare prefix. */
1092 if (c & WF_RAREPFX)
1093 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001094 }
1095
1096 if (flags & WF_BANNED)
1097 res = SP_BANNED;
1098 else if (flags & WF_REGION)
1099 {
1100 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001101 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001102 res = SP_OK;
1103 else
1104 res = SP_LOCAL;
1105 }
1106 else if (flags & WF_RARE)
1107 res = SP_RARE;
1108 else
1109 res = SP_OK;
1110
1111 /* Always use the longest match and the best result. */
1112 if (mip->mi_result > res)
1113 {
1114 mip->mi_result = res;
1115 mip->mi_end = mip->mi_word + wlen;
1116 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001117 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001118 mip->mi_end = mip->mi_word + wlen;
1119
1120 if (res == SP_OK)
1121 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001122 }
1123
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001124 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001125 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001126 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001127}
1128
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001129/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001130 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1131 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001132 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001133 */
1134 static int
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001135valid_word_prefix(totprefcnt, arridx, flags, word, slang)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001136 int totprefcnt; /* nr of prefix IDs */
1137 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001138 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001139 char_u *word;
1140 slang_T *slang;
1141{
1142 int prefcnt;
1143 int pidx;
1144 regprog_T *rp;
1145 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001146 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001147
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001148 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001149 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1150 {
1151 pidx = slang->sl_pidxs[arridx + prefcnt];
1152
1153 /* Check the prefix ID. */
1154 if (prefid != (pidx & 0xff))
1155 continue;
1156
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001157 /* Check if the prefix doesn't combine and the word already has a
1158 * suffix. */
1159 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1160 continue;
1161
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001162 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001163 * stored in the two bytes above the prefix ID byte. */
1164 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001165 if (rp != NULL)
1166 {
1167 regmatch.regprog = rp;
1168 regmatch.rm_ic = FALSE;
1169 if (!vim_regexec(&regmatch, word, 0))
1170 continue;
1171 }
1172
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001173 /* It's a match! Return the WF_RAREPFX flag. */
1174 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001175 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001176 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001177}
1178
1179/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001180 * Check if the word at "mip->mi_word" has a matching prefix.
1181 * If it does, then check the following word.
1182 *
1183 * For a match mip->mi_result is updated.
1184 */
1185 static void
1186find_prefix(mip)
1187 matchinf_T *mip;
1188{
1189 idx_T arridx = 0;
1190 int len;
1191 int wlen = 0;
1192 int flen;
1193 int c;
1194 char_u *ptr;
1195 idx_T lo, hi, m;
1196 slang_T *slang = mip->mi_lp->lp_slang;
1197 char_u *byts;
1198 idx_T *idxs;
1199
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001200 byts = slang->sl_pbyts;
1201 if (byts == NULL)
1202 return; /* array is empty */
1203
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001204 /* We use the case-folded word here, since prefixes are always
1205 * case-folded. */
1206 ptr = mip->mi_fword;
1207 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001208 idxs = slang->sl_pidxs;
1209
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001210 /*
1211 * Repeat advancing in the tree until:
1212 * - there is a byte that doesn't match,
1213 * - we reach the end of the tree,
1214 * - or we reach the end of the line.
1215 */
1216 for (;;)
1217 {
1218 if (flen == 0 && *mip->mi_fend != NUL)
1219 flen = fold_more(mip);
1220
1221 len = byts[arridx++];
1222
1223 /* If the first possible byte is a zero the prefix could end here.
1224 * Check if the following word matches and supports the prefix. */
1225 if (byts[arridx] == 0)
1226 {
1227 /* There can be several prefixes with different conditions. We
1228 * try them all, since we don't know which one will give the
1229 * longest match. The word is the same each time, pass the list
1230 * of possible prefixes to find_word(). */
1231 mip->mi_prefarridx = arridx;
1232 mip->mi_prefcnt = len;
1233 while (len > 0 && byts[arridx] == 0)
1234 {
1235 ++arridx;
1236 --len;
1237 }
1238 mip->mi_prefcnt -= len;
1239
1240 /* Find the word that comes after the prefix. */
1241 mip->mi_prefixlen = wlen;
1242 find_word(mip, FIND_PREFIX);
1243
1244
1245 if (len == 0)
1246 break; /* no children, word must end here */
1247 }
1248
1249 /* Stop looking at end of the line. */
1250 if (ptr[wlen] == NUL)
1251 break;
1252
1253 /* Perform a binary search in the list of accepted bytes. */
1254 c = ptr[wlen];
1255 lo = arridx;
1256 hi = arridx + len - 1;
1257 while (lo < hi)
1258 {
1259 m = (lo + hi) / 2;
1260 if (byts[m] > c)
1261 hi = m - 1;
1262 else if (byts[m] < c)
1263 lo = m + 1;
1264 else
1265 {
1266 lo = hi = m;
1267 break;
1268 }
1269 }
1270
1271 /* Stop if there is no matching byte. */
1272 if (hi < lo || byts[lo] != c)
1273 break;
1274
1275 /* Continue at the child (if there is one). */
1276 arridx = idxs[lo];
1277 ++wlen;
1278 --flen;
1279 }
1280}
1281
1282/*
1283 * Need to fold at least one more character. Do until next non-word character
1284 * for efficiency.
1285 * Return the length of the folded chars in bytes.
1286 */
1287 static int
1288fold_more(mip)
1289 matchinf_T *mip;
1290{
1291 int flen;
1292 char_u *p;
1293
1294 p = mip->mi_fend;
1295 do
1296 {
1297 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001298 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001299
1300 /* Include the non-word character so that we can check for the
1301 * word end. */
1302 if (*mip->mi_fend != NUL)
1303 mb_ptr_adv(mip->mi_fend);
1304
1305 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1306 mip->mi_fword + mip->mi_fwordlen,
1307 MAXWLEN - mip->mi_fwordlen);
1308 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1309 mip->mi_fwordlen += flen;
1310 return flen;
1311}
1312
1313/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001314 * Check case flags for a word. Return TRUE if the word has the requested
1315 * case.
1316 */
1317 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001318spell_valid_case(wordflags, treeflags)
1319 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001320 int treeflags; /* flags for the word in the spell tree */
1321{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001322 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001323 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001324 && ((treeflags & WF_ONECAP) == 0 || wordflags == WF_ONECAP)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001325}
1326
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001327/*
1328 * Return TRUE if spell checking is not enabled.
1329 */
1330 static int
1331no_spell_checking()
1332{
1333 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1334 {
1335 EMSG(_("E756: Spell checking is not enabled"));
1336 return TRUE;
1337 }
1338 return FALSE;
1339}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001340
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001341/*
1342 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001343 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001344 * Return OK if found, FAIL otherwise.
1345 */
1346 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001347spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001348 int dir; /* FORWARD or BACKWARD */
1349 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001350 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001351{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001352 linenr_T lnum;
1353 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001354 char_u *line;
1355 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001356 char_u *endp;
1357 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001358 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001359 int has_syntax = syntax_present(curbuf);
1360 int col;
1361 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001362 char_u *buf = NULL;
1363 int buflen = 0;
1364 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001365 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001366
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001367 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001368 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001369
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001370 /*
1371 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001372 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001373 *
1374 * When searching backwards, we continue in the line to find the last
1375 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001376 *
1377 * We concatenate the start of the next line, so that wrapped words work
1378 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1379 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001380 */
1381 lnum = curwin->w_cursor.lnum;
1382 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001383
1384 while (!got_int)
1385 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001386 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001387
Bram Moolenaar0c405862005-06-22 22:26:26 +00001388 len = STRLEN(line);
1389 if (buflen < len + MAXWLEN + 2)
1390 {
1391 vim_free(buf);
1392 buflen = len + MAXWLEN + 2;
1393 buf = alloc(buflen);
1394 if (buf == NULL)
1395 break;
1396 }
1397
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001398 /* In first line check first word for Capital. */
1399 if (lnum == 1)
1400 capcol = 0;
1401
1402 /* For checking first word with a capital skip white space. */
1403 if (capcol == 0)
1404 capcol = skipwhite(line) - line;
1405
Bram Moolenaar0c405862005-06-22 22:26:26 +00001406 /* Copy the line into "buf" and append the start of the next line if
1407 * possible. */
1408 STRCPY(buf, line);
1409 if (lnum < curbuf->b_ml.ml_line_count)
1410 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1411
1412 p = buf + skip;
1413 endp = buf + len;
1414 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001415 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001416 /* When searching backward don't search after the cursor. */
1417 if (dir == BACKWARD
1418 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001419 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001420 break;
1421
1422 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001423 attr = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001424 len = spell_check(curwin, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001425
1426 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001427 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001428 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001429 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001430 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001431 /* When searching forward only accept a bad word after
1432 * the cursor. */
1433 if (dir == BACKWARD
1434 || lnum > curwin->w_cursor.lnum
1435 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001436 && (colnr_T)(curline ? p - buf + len
1437 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001438 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001439 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001440 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001441 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001442 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001443 (void)syn_get_id(lnum, (colnr_T)col,
1444 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001445 }
1446 else
1447 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001448
Bram Moolenaar51485f02005-06-04 21:55:20 +00001449 if (can_spell)
1450 {
1451 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001452 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001453#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001454 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001455#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001456 if (dir == FORWARD)
1457 {
1458 /* No need to search further. */
1459 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001460 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001461 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001462 }
1463 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001464 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001465 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001466 }
1467
Bram Moolenaar51485f02005-06-04 21:55:20 +00001468 /* advance to character after the word */
1469 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001470 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001471 }
1472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001473 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001474 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001475
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001476 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001477 if (dir == BACKWARD)
1478 {
1479 if (found_pos.lnum != 0)
1480 {
1481 /* Use the last match in the line. */
1482 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001483 vim_free(buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001484 return OK;
1485 }
1486 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001487 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001488 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001489 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001490 }
1491 else
1492 {
1493 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001494 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001495 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001496
1497 /* Skip the characters at the start of the next line that were
1498 * included in a match crossing line boundaries. */
1499 if (attr == 0)
1500 skip = p - endp;
1501 else
1502 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001503
1504 /* Capscol skips over the inserted space. */
1505 --capcol;
1506
1507 /* But after empty line check first word in next line */
1508 if (*skipwhite(line) == NUL)
1509 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001510 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001511
1512 line_breakcheck();
1513 }
1514
Bram Moolenaar0c405862005-06-22 22:26:26 +00001515 vim_free(buf);
1516 return FAIL;
1517}
1518
1519/*
1520 * For spell checking: concatenate the start of the following line "line" into
1521 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1522 */
1523 void
1524spell_cat_line(buf, line, maxlen)
1525 char_u *buf;
1526 char_u *line;
1527 int maxlen;
1528{
1529 char_u *p;
1530 int n;
1531
1532 p = skipwhite(line);
1533 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1534 p = skipwhite(p + 1);
1535
1536 if (*p != NUL)
1537 {
1538 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001539 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001540 n = p - line;
1541 if (n >= maxlen)
1542 n = maxlen - 1;
1543 vim_memset(buf + 1, ' ', n);
1544 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001545}
1546
1547/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001548 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001549 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001550 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001551 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001552spell_load_lang(lang)
1553 char_u *lang;
1554{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001555 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001556 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001557 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001558
Bram Moolenaarb765d632005-06-07 21:00:02 +00001559 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001560 * It's truncated when an error is detected. */
1561 STRCPY(langcp, lang);
1562
Bram Moolenaarb765d632005-06-07 21:00:02 +00001563 /*
1564 * Find the first spell file for "lang" in 'runtimepath' and load it.
1565 */
1566 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1567 "spell/%s.%s.spl", lang, spell_enc());
1568 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001569
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001570 if (r == FAIL && *langcp != NUL)
1571 {
1572 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001573 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001574 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001575 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001576 }
1577
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001578 if (r == FAIL)
1579 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1580 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001581 else if (*langcp != NUL)
1582 {
1583 /* Load all the additions. */
1584 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1585 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1586 }
1587}
1588
1589/*
1590 * Return the encoding used for spell checking: Use 'encoding', except that we
1591 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1592 */
1593 static char_u *
1594spell_enc()
1595{
1596
1597#ifdef FEAT_MBYTE
1598 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1599 return p_enc;
1600#endif
1601 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001602}
1603
1604/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001605 * Get the name of the .spl file for the internal wordlist into
1606 * "fname[MAXPATHL]".
1607 */
1608 static void
1609int_wordlist_spl(fname)
1610 char_u *fname;
1611{
1612 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
1613 int_wordlist, spell_enc());
1614}
1615
1616/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001617 * Allocate a new slang_T.
1618 * Caller must fill "sl_next".
1619 */
1620 static slang_T *
1621slang_alloc(lang)
1622 char_u *lang;
1623{
1624 slang_T *lp;
1625
Bram Moolenaar51485f02005-06-04 21:55:20 +00001626 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001627 if (lp != NULL)
1628 {
1629 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001630 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001631 }
1632 return lp;
1633}
1634
1635/*
1636 * Free the contents of an slang_T and the structure itself.
1637 */
1638 static void
1639slang_free(lp)
1640 slang_T *lp;
1641{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001642 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001643 vim_free(lp->sl_fname);
1644 slang_clear(lp);
1645 vim_free(lp);
1646}
1647
1648/*
1649 * Clear an slang_T so that the file can be reloaded.
1650 */
1651 static void
1652slang_clear(lp)
1653 slang_T *lp;
1654{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001655 garray_T *gap;
1656 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001657 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001658 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001659
Bram Moolenaar51485f02005-06-04 21:55:20 +00001660 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001661 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001662 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001663 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001664 vim_free(lp->sl_pbyts);
1665 lp->sl_pbyts = NULL;
1666
Bram Moolenaar51485f02005-06-04 21:55:20 +00001667 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001668 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001669 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001670 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001671 vim_free(lp->sl_pidxs);
1672 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001673
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001674 gap = &lp->sl_rep;
1675 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001676 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001677 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1678 vim_free(ftp->ft_from);
1679 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001680 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001681 ga_clear(gap);
1682
1683 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001684 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001685 {
1686 /* "ga_len" is set to 1 without adding an item for latin1 */
1687 if (gap->ga_data != NULL)
1688 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1689 for (i = 0; i < gap->ga_len; ++i)
1690 vim_free(((int **)gap->ga_data)[i]);
1691 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001692 else
1693 /* SAL items: free salitem_T items */
1694 while (gap->ga_len > 0)
1695 {
1696 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1697 vim_free(smp->sm_lead);
1698 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1699 vim_free(smp->sm_to);
1700#ifdef FEAT_MBYTE
1701 vim_free(smp->sm_lead_w);
1702 vim_free(smp->sm_oneof_w);
1703 vim_free(smp->sm_to_w);
1704#endif
1705 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001706 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001707
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001708 for (i = 0; i < lp->sl_prefixcnt; ++i)
1709 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001710 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001711 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001712 lp->sl_prefprog = NULL;
1713
1714 vim_free(lp->sl_midword);
1715 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001716
Bram Moolenaarea424162005-06-16 21:51:00 +00001717#ifdef FEAT_MBYTE
1718 {
1719 int todo = lp->sl_map_hash.ht_used;
1720 hashitem_T *hi;
1721
1722 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1723 if (!HASHITEM_EMPTY(hi))
1724 {
1725 --todo;
1726 vim_free(hi->hi_key);
1727 }
1728 }
1729 hash_clear(&lp->sl_map_hash);
1730#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001731}
1732
1733/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001734 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001735 * Invoked through do_in_runtimepath().
1736 */
1737 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001738spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001739 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001740 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001741{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001742 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001743}
1744
1745/*
1746 * Load one spell file and store the info into a slang_T.
1747 *
1748 * This is invoked in two ways:
1749 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1750 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1751 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1752 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001753 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001754 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001755 static slang_T *
1756spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001757 char_u *fname;
1758 char_u *lang;
1759 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001760 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001761{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001762 FILE *fd;
1763 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001764 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001765 char_u *bp;
1766 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001767 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001768 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001769 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001770 int round;
1771 char_u *save_sourcing_name = sourcing_name;
1772 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001773 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001774 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001775 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001776 garray_T *gap;
1777 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001778 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001779 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001780 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001781 int c = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001782
Bram Moolenaarb765d632005-06-07 21:00:02 +00001783 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001784 if (fd == NULL)
1785 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001786 if (!silent)
1787 EMSG2(_(e_notopen), fname);
1788 else if (p_verbose > 2)
1789 {
1790 verbose_enter();
1791 smsg((char_u *)e_notopen, fname);
1792 verbose_leave();
1793 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001794 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001795 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001796 if (p_verbose > 2)
1797 {
1798 verbose_enter();
1799 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1800 verbose_leave();
1801 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001802
Bram Moolenaarb765d632005-06-07 21:00:02 +00001803 if (old_lp == NULL)
1804 {
1805 lp = slang_alloc(lang);
1806 if (lp == NULL)
1807 goto endFAIL;
1808
1809 /* Remember the file name, used to reload the file when it's updated. */
1810 lp->sl_fname = vim_strsave(fname);
1811 if (lp->sl_fname == NULL)
1812 goto endFAIL;
1813
1814 /* Check for .add.spl. */
1815 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1816 }
1817 else
1818 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001819
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001820 /* Set sourcing_name, so that error messages mention the file name. */
1821 sourcing_name = fname;
1822 sourcing_lnum = 0;
1823
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001824 /* <HEADER>: <fileID>
1825 * <regioncnt> <regionname> ...
1826 * <charflagslen> <charflags>
1827 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001828 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001829 * <prefcondcnt> <prefcond> ...
1830 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001831 for (i = 0; i < VIMSPELLMAGICL; ++i)
1832 buf[i] = getc(fd); /* <fileID> */
1833 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1834 {
1835 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001836 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001837 }
1838
1839 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001840 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001841 {
1842truncerr:
Bram Moolenaar7887d882005-07-01 22:33:52 +00001843 EMSG(_(e_spell_trunc));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001844 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001845 }
1846 if (cnt > 8)
1847 {
1848formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001849 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001850 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001851 }
1852 for (i = 0; i < cnt; ++i)
1853 {
1854 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1855 lp->sl_regions[i * 2 + 1] = getc(fd);
1856 }
1857 lp->sl_regions[cnt * 2] = NUL;
1858
Bram Moolenaar7887d882005-07-01 22:33:52 +00001859 /* <charflagslen> <charflags> */
1860 p = read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001861 if (cnt < 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001862 goto endFAIL;
1863
1864 /* <fcharslen> <fchars> */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001865 fol = read_cnt_string(fd, 2, &ccnt);
1866 if (ccnt < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001867 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001868 vim_free(p);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001869 goto endFAIL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001870 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00001871
1872 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
1873 if (p != NULL && fol != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001874 i = set_spell_charflags(p, cnt, fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001875
1876 vim_free(p);
1877 vim_free(fol);
1878
1879 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1880 if ((p == NULL) != (fol == NULL))
1881 goto formerr;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001882
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001883 /* <midwordlen> <midword> */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001884 lp->sl_midword = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001885 if (cnt < 0)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001886 goto endFAIL;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001887
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001888 /* <prefcondcnt> <prefcond> ... */
1889 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
1890 if (cnt > 0)
1891 {
1892 lp->sl_prefprog = (regprog_T **)alloc_clear(
1893 (unsigned)sizeof(regprog_T *) * cnt);
1894 if (lp->sl_prefprog == NULL)
1895 goto endFAIL;
1896 lp->sl_prefixcnt = cnt;
1897
1898 for (i = 0; i < cnt; ++i)
1899 {
1900 /* <prefcond> : <condlen> <condstr> */
1901 n = getc(fd); /* <condlen> */
1902 if (n < 0)
1903 goto formerr;
1904 /* When <condlen> is zero we have an empty condition. Otherwise
1905 * compile the regexp program used to check for the condition. */
1906 if (n > 0)
1907 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001908 buf[0] = '^'; /* always match at one position only */
1909 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001910 while (n-- > 0)
1911 *p++ = getc(fd); /* <condstr> */
1912 *p = NUL;
1913 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
1914 }
1915 }
1916 }
1917
1918
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001919 /* <SUGGEST> : <repcount> <rep> ...
1920 * <salflags> <salcount> <sal> ...
1921 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001922
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001923 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
1924 if (cnt < 0)
1925 goto formerr;
1926
1927 gap = &lp->sl_rep;
1928 if (ga_grow(gap, cnt) == FAIL)
1929 goto endFAIL;
1930
1931 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
1932 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001933 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001934 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar7887d882005-07-01 22:33:52 +00001935 ftp->ft_from = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001936 if (i <= 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001937 goto endFAIL;
1938 ftp->ft_to = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001939 if (i <= 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001940 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00001941 vim_free(ftp->ft_from);
1942 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001943 }
1944 }
1945
1946 /* Fill the first-index table. */
1947 first = lp->sl_rep_first;
1948 for (i = 0; i < 256; ++i)
1949 first[i] = -1;
1950 for (i = 0; i < gap->ga_len; ++i)
1951 {
1952 ftp = &((fromto_T *)gap->ga_data)[i];
1953 if (first[*ftp->ft_from] == -1)
1954 first[*ftp->ft_from] = i;
1955 }
1956
1957 i = getc(fd); /* <salflags> */
1958 if (i & SAL_F0LLOWUP)
1959 lp->sl_followup = TRUE;
1960 if (i & SAL_COLLAPSE)
1961 lp->sl_collapse = TRUE;
1962 if (i & SAL_REM_ACCENTS)
1963 lp->sl_rem_accents = TRUE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001964 if (i & SAL_SOFO)
1965 lp->sl_sofo = TRUE;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001966 else
1967 lp->sl_sofo = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001968
1969 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
1970 if (cnt < 0)
1971 goto formerr;
1972
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001973 if (lp->sl_sofo)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001974 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001975 /*
1976 * SOFOFROM and SOFOTO items come in one <salfrom> and <salto>
1977 */
1978 if (cnt != 1)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001979 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001980
Bram Moolenaar7887d882005-07-01 22:33:52 +00001981 /* <salfromlen> <salfrom> */
1982 bp = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001983 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001984 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001985
Bram Moolenaar7887d882005-07-01 22:33:52 +00001986 /* <saltolen> <salto> */
1987 fol = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001988 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001989 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001990 vim_free(bp);
1991 goto endFAIL;
1992 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001993
Bram Moolenaar7887d882005-07-01 22:33:52 +00001994 /* Store the info in lp->sl_sal and/or lp->sl_sal_first. */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001995 if (bp != NULL && fol != NULL)
1996 i = set_sofo(lp, bp, fol);
1997 else if (bp != NULL || fol != NULL)
1998 i = FAIL; /* only one of two strings is an error */
1999 else
2000 i = OK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002001
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002002 vim_free(bp);
2003 vim_free(fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002004 if (i == FAIL)
2005 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002006 }
2007 else
2008 {
2009 /*
2010 * SAL items
2011 */
2012 gap = &lp->sl_sal;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002013 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002014 if (ga_grow(gap, cnt) == FAIL)
2015 goto endFAIL;
2016
2017 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2018 for (; gap->ga_len < cnt; ++gap->ga_len)
2019 {
2020 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2021 ccnt = getc(fd); /* <salfromlen> */
2022 if (ccnt < 0)
2023 goto formerr;
2024 if ((p = alloc(ccnt + 2)) == NULL)
2025 goto endFAIL;
2026 smp->sm_lead = p;
2027
2028 /* Read up to the first special char into sm_lead. */
2029 for (i = 0; i < ccnt; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002030 {
2031 c = getc(fd); /* <salfrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002032 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002033 break;
2034 *p++ = c;
2035 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002036 smp->sm_leadlen = p - smp->sm_lead;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002037 *p++ = NUL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002038
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002039 /* Put (abc) chars in sm_oneof, if any. */
2040 if (c == '(')
2041 {
2042 smp->sm_oneof = p;
2043 for (++i; i < ccnt; ++i)
2044 {
2045 c = getc(fd); /* <salfrom> */
2046 if (c == ')')
2047 break;
2048 *p++ = c;
2049 }
2050 *p++ = NUL;
2051 if (++i < ccnt)
2052 c = getc(fd);
2053 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002054 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002055 smp->sm_oneof = NULL;
2056
2057 /* Any following chars go in sm_rules. */
2058 smp->sm_rules = p;
2059 if (i < ccnt)
2060 /* store the char we got while checking for end of sm_lead */
2061 *p++ = c;
2062 for (++i; i < ccnt; ++i)
2063 *p++ = getc(fd); /* <salfrom> */
2064 *p++ = NUL;
2065
Bram Moolenaar7887d882005-07-01 22:33:52 +00002066 /* <saltolen> <salto> */
2067 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002068 if (ccnt < 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002069 {
2070 vim_free(smp->sm_lead);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002071 goto formerr;
2072 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002073
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002074#ifdef FEAT_MBYTE
2075 if (has_mbyte)
2076 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002077 /* convert the multi-byte strings to wide char strings */
2078 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2079 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2080 if (smp->sm_oneof == NULL)
2081 smp->sm_oneof_w = NULL;
2082 else
2083 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002084 if (smp->sm_to == NULL)
2085 smp->sm_to_w = NULL;
2086 else
2087 smp->sm_to_w = mb_str2wide(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002088 if (smp->sm_lead_w == NULL
2089 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002090 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002091 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002092 vim_free(smp->sm_lead);
2093 vim_free(smp->sm_to);
2094 vim_free(smp->sm_lead_w);
2095 vim_free(smp->sm_oneof_w);
2096 vim_free(smp->sm_to_w);
2097 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002098 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002099 }
2100#endif
2101 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002102
2103 /* Fill the first-index table. */
Bram Moolenaar7887d882005-07-01 22:33:52 +00002104 set_sal_first(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002105 }
2106
Bram Moolenaar7887d882005-07-01 22:33:52 +00002107 /* <maplen> <mapstr> */
2108 p = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002109 if (cnt < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002110 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002111 if (p != NULL)
2112 {
2113 set_map_str(lp, p);
2114 vim_free(p);
2115 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002116
Bram Moolenaar51485f02005-06-04 21:55:20 +00002117 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002118 * round 2: <KWORDTREE>
2119 * round 3: <PREFIXTREE> */
2120 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002121 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002122 /* The tree size was computed when writing the file, so that we can
2123 * allocate it as one long block. <nodecount> */
2124 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2125 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002126 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002127 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002128 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002129 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002130 bp = lalloc((long_u)len, TRUE);
2131 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002132 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002133 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002134 lp->sl_fbyts = bp;
2135 else if (round == 2)
2136 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002137 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002138 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002139
2140 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002141 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2142 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002143 goto endFAIL;
2144 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002145 lp->sl_fidxs = ip;
2146 else if (round == 2)
2147 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002148 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002149 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002150
2151 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002152 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002153 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002154 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002155 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002156 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002157 }
2158 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002159
Bram Moolenaarb765d632005-06-07 21:00:02 +00002160 /* For a new file link it in the list of spell files. */
2161 if (old_lp == NULL)
2162 {
2163 lp->sl_next = first_lang;
2164 first_lang = lp;
2165 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002166
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002167 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002168
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002169endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002170 if (lang != NULL)
2171 /* truncating the name signals the error to spell_load_lang() */
2172 *lang = NUL;
2173 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002174 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002175 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002176 lp = NULL;
2177 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002178
2179endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002180 if (fd != NULL)
2181 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002182 sourcing_name = save_sourcing_name;
2183 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002184
2185 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002186}
2187
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002188/*
2189 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002190 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002191 * Returns NULL when the count is zero.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002192 * Sets "*cntp" to -1 when there is an error, length of the result otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002193 */
2194 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002195read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002196 FILE *fd;
2197 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002198 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002199{
2200 int cnt = 0;
2201 int i;
2202 char_u *str;
2203
2204 /* read the length bytes, MSB first */
2205 for (i = 0; i < cnt_bytes; ++i)
2206 cnt = (cnt << 8) + getc(fd);
2207 if (cnt < 0)
2208 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002209 EMSG(_(e_spell_trunc));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002210 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002211 return NULL;
2212 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002213 *cntp = cnt;
2214 if (cnt == 0)
2215 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002216
2217 /* allocate memory */
2218 str = alloc((unsigned)cnt + 1);
2219 if (str == NULL)
2220 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002221 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002222 return NULL;
2223 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002224
2225 /* Read the string. Doesn't check for truncated file. */
2226 for (i = 0; i < cnt; ++i)
2227 str[i] = getc(fd);
2228 str[i] = NUL;
2229
2230 return str;
2231}
2232
Bram Moolenaar7887d882005-07-01 22:33:52 +00002233/*
2234 * Set the SOFOFROM and SOFOTO items in language "lp".
2235 * Returns FAIL when there is something wrong.
2236 */
2237 static int
2238set_sofo(lp, from, to)
2239 slang_T *lp;
2240 char_u *from;
2241 char_u *to;
2242{
2243 int i;
2244
2245#ifdef FEAT_MBYTE
2246 garray_T *gap;
2247 char_u *s;
2248 char_u *p;
2249 int c;
2250 int *inp;
2251
2252 if (has_mbyte)
2253 {
2254 /* Use "sl_sal" as an array with 256 pointers to a list of wide
2255 * characters. The index is the low byte of the character.
2256 * The list contains from-to pairs with a terminating NUL.
2257 * sl_sal_first[] is used for latin1 "from" characters. */
2258 gap = &lp->sl_sal;
2259 ga_init2(gap, sizeof(int *), 1);
2260 if (ga_grow(gap, 256) == FAIL)
2261 return FAIL;
2262 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
2263 gap->ga_len = 256;
2264
2265 /* First count the number of items for each list. Temporarily use
2266 * sl_sal_first[] for this. */
2267 for (p = from, s = to; *p != NUL && *s != NUL; )
2268 {
2269 c = mb_ptr2char_adv(&p);
2270 mb_ptr_adv(s);
2271 if (c >= 256)
2272 ++lp->sl_sal_first[c & 0xff];
2273 }
2274 if (*p != NUL || *s != NUL) /* lengths differ */
2275 return FAIL;
2276
2277 /* Allocate the lists. */
2278 for (i = 0; i < 256; ++i)
2279 if (lp->sl_sal_first[i] > 0)
2280 {
2281 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
2282 if (p == NULL)
2283 return FAIL;
2284 ((int **)gap->ga_data)[i] = (int *)p;
2285 *(int *)p = 0;
2286 }
2287
2288 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
2289 * list. */
2290 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
2291 for (p = from, s = to; *p != NUL && *s != NUL; )
2292 {
2293 c = mb_ptr2char_adv(&p);
2294 i = mb_ptr2char_adv(&s);
2295 if (c >= 256)
2296 {
2297 /* Append the from-to chars at the end of the list with
2298 * the low byte. */
2299 inp = ((int **)gap->ga_data)[c & 0xff];
2300 while (*inp != 0)
2301 ++inp;
2302 *inp++ = c; /* from char */
2303 *inp++ = i; /* to char */
2304 *inp++ = NUL; /* NUL at the end */
2305 }
2306 else
2307 /* mapping byte to char is done in sl_sal_first[] */
2308 lp->sl_sal_first[c] = i;
2309 }
2310 }
2311 else
2312#endif
2313 {
2314 /* mapping bytes to bytes is done in sl_sal_first[] */
2315 if (STRLEN(from) != STRLEN(to))
2316 return FAIL;
2317
2318 for (i = 0; to[i] != NUL; ++i)
2319 lp->sl_sal_first[from[i]] = to[i];
2320 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
2321 }
2322
2323 return OK;
2324}
2325
2326/*
2327 * Fill the first-index table for "lp".
2328 */
2329 static void
2330set_sal_first(lp)
2331 slang_T *lp;
2332{
2333 salfirst_T *sfirst;
2334 int i;
2335 salitem_T *smp;
2336 int c;
2337 garray_T *gap = &lp->sl_sal;
2338
2339 sfirst = lp->sl_sal_first;
2340 for (i = 0; i < 256; ++i)
2341 sfirst[i] = -1;
2342 smp = (salitem_T *)gap->ga_data;
2343 for (i = 0; i < gap->ga_len; ++i)
2344 {
2345#ifdef FEAT_MBYTE
2346 if (has_mbyte)
2347 /* Use the lowest byte of the first character. For latin1 it's
2348 * the character, for other encodings it should differ for most
2349 * characters. */
2350 c = *smp[i].sm_lead_w & 0xff;
2351 else
2352#endif
2353 c = *smp[i].sm_lead;
2354 if (sfirst[c] == -1)
2355 {
2356 sfirst[c] = i;
2357#ifdef FEAT_MBYTE
2358 if (has_mbyte)
2359 {
2360 int n;
2361
2362 /* Make sure all entries with this byte are following each
2363 * other. Move the ones that are in the wrong position. Do
2364 * keep the same ordering! */
2365 while (i + 1 < gap->ga_len
2366 && (*smp[i + 1].sm_lead_w & 0xff) == c)
2367 /* Skip over entry with same index byte. */
2368 ++i;
2369
2370 for (n = 1; i + n < gap->ga_len; ++n)
2371 if ((*smp[i + n].sm_lead_w & 0xff) == c)
2372 {
2373 salitem_T tsal;
2374
2375 /* Move entry with same index byte after the entries
2376 * we already found. */
2377 ++i;
2378 --n;
2379 tsal = smp[i + n];
2380 mch_memmove(smp + i + 1, smp + i,
2381 sizeof(salitem_T) * n);
2382 smp[i] = tsal;
2383 }
2384 }
2385#endif
2386 }
2387 }
2388}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002389
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002390#ifdef FEAT_MBYTE
2391/*
2392 * Turn a multi-byte string into a wide character string.
2393 * Return it in allocated memory (NULL for out-of-memory)
2394 */
2395 static int *
2396mb_str2wide(s)
2397 char_u *s;
2398{
2399 int *res;
2400 char_u *p;
2401 int i = 0;
2402
2403 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
2404 if (res != NULL)
2405 {
2406 for (p = s; *p != NUL; )
2407 res[i++] = mb_ptr2char_adv(&p);
2408 res[i] = NUL;
2409 }
2410 return res;
2411}
2412#endif
2413
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002414/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002415 * Read one row of siblings from the spell file and store it in the byte array
2416 * "byts" and index array "idxs". Recursively read the children.
2417 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00002418 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00002419 *
2420 * Returns the index follosing the siblings.
2421 * Returns -1 if the file is shorter than expected.
2422 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002423 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002424 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002425read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002426 FILE *fd;
2427 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002428 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002429 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002430 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002431 int prefixtree; /* TRUE for reading PREFIXTREE */
2432 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002433{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002434 int len;
2435 int i;
2436 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002437 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002438 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002439 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002440#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002441
Bram Moolenaar51485f02005-06-04 21:55:20 +00002442 len = getc(fd); /* <siblingcount> */
2443 if (len <= 0)
2444 return -1;
2445
2446 if (startidx + len >= maxidx)
2447 return -2;
2448 byts[idx++] = len;
2449
2450 /* Read the byte values, flag/region bytes and shared indexes. */
2451 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002452 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002453 c = getc(fd); /* <byte> */
2454 if (c < 0)
2455 return -1;
2456 if (c <= BY_SPECIAL)
2457 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002458 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002459 {
2460 /* No flags, all regions. */
2461 idxs[idx] = 0;
2462 c = 0;
2463 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002464 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002465 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002466 if (prefixtree)
2467 {
2468 /* Read the prefix ID and the condition nr. In idxs[]
2469 * store the prefix ID in the low byte, the condition
2470 * index shifted up 8 bits. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002471 c2 = getc(fd); /* <prefixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002472 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
2473 if (n >= maxprefcondnr)
2474 return -2;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002475 c2 += (n << 8);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002476
2477 /* the byte value is misused to pass two flags: rare
2478 * and non-combining. */
2479 if (c == BY_FLAGS || c == BY_PFX_RNC)
2480 c2 |= WF_RAREPFX;
2481 if (c == BY_PFX_NC || c == BY_PFX_RNC)
2482 c2 |= WF_PFX_NC;
2483 c = c2;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002484 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002485 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002486 {
2487 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002488 * idxs[] the flags go in the low two bytes, region above
2489 * that and prefix ID above the region. */
2490 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002491 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002492 if (c2 == BY_FLAGS2)
2493 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002494 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002495 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002496 if (c & WF_PFX)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002497 c = (getc(fd) << 24) + c; /* <prefixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002498 }
2499
Bram Moolenaar51485f02005-06-04 21:55:20 +00002500 idxs[idx] = c;
2501 c = 0;
2502 }
2503 else /* c == BY_INDEX */
2504 {
2505 /* <nodeidx> */
2506 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2507 if (n < 0 || n >= maxidx)
2508 return -2;
2509 idxs[idx] = n + SHARED_MASK;
2510 c = getc(fd); /* <xbyte> */
2511 }
2512 }
2513 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002514 }
2515
Bram Moolenaar51485f02005-06-04 21:55:20 +00002516 /* Recursively read the children for non-shared siblings.
2517 * Skip the end-of-word ones (zero byte value) and the shared ones (and
2518 * remove SHARED_MASK) */
2519 for (i = 1; i <= len; ++i)
2520 if (byts[startidx + i] != 0)
2521 {
2522 if (idxs[startidx + i] & SHARED_MASK)
2523 idxs[startidx + i] &= ~SHARED_MASK;
2524 else
2525 {
2526 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002527 idx = read_tree(fd, byts, idxs, maxidx, idx,
2528 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002529 if (idx < 0)
2530 break;
2531 }
2532 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002533
Bram Moolenaar51485f02005-06-04 21:55:20 +00002534 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002535}
2536
2537/*
2538 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002539 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002540 */
2541 char_u *
2542did_set_spelllang(buf)
2543 buf_T *buf;
2544{
2545 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002546 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002547 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002548 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002549 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002550 int region_mask;
2551 slang_T *lp;
2552 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002553 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002554 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002555 int len;
2556 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002557 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002558 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002559 char_u *use_region = NULL;
2560 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002561
2562 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002563 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002564
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002565 /* loop over comma separated language names. */
2566 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002567 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002568 /* Get one language name. */
2569 copy_option_part(&splp, lang, MAXWLEN, ",");
2570
Bram Moolenaar5482f332005-04-17 20:18:43 +00002571 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002572 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002573
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002574 /* If the name ends in ".spl" use it as the name of the spell file.
2575 * If there is a region name let "region" point to it and remove it
2576 * from the name. */
2577 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2578 {
2579 filename = TRUE;
2580
Bram Moolenaarb6356332005-07-18 21:40:44 +00002581 /* Locate a region and remove it from the file name. */
2582 p = vim_strchr(gettail(lang), '_');
2583 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2584 && !ASCII_ISALPHA(p[3]))
2585 {
2586 vim_strncpy(region_cp, p + 1, 2);
2587 mch_memmove(p, p + 3, len - (p - lang) - 2);
2588 len -= 3;
2589 region = region_cp;
2590 }
2591 else
2592 dont_use_region = TRUE;
2593
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002594 /* Check if we loaded this language before. */
2595 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2596 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
2597 break;
2598 }
2599 else
2600 {
2601 filename = FALSE;
2602 if (len > 3 && lang[len - 3] == '_')
2603 {
2604 region = lang + len - 2;
2605 len -= 3;
2606 lang[len] = NUL;
2607 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002608 else
2609 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002610
2611 /* Check if we loaded this language before. */
2612 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2613 if (STRICMP(lang, lp->sl_name) == 0)
2614 break;
2615 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002616
Bram Moolenaarb6356332005-07-18 21:40:44 +00002617 if (region != NULL)
2618 {
2619 /* If the region differs from what was used before then don't
2620 * use it for 'spellfile'. */
2621 if (use_region != NULL && STRCMP(region, use_region) != 0)
2622 dont_use_region = TRUE;
2623 use_region = region;
2624 }
2625
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002626 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002627 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002628 {
2629 if (filename)
2630 (void)spell_load_file(lang, lang, NULL, FALSE);
2631 else
2632 spell_load_lang(lang);
2633 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002634
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002635 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002636 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002637 */
2638 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002639 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
2640 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002641 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002642 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002643 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002644 {
2645 /* find region in sl_regions */
2646 c = find_region(lp->sl_regions, region);
2647 if (c == REGION_ALL)
2648 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002649 if (lp->sl_add)
2650 {
2651 if (*lp->sl_regions != NUL)
2652 /* This addition file is for other regions. */
2653 region_mask = 0;
2654 }
2655 else
2656 /* This is probably an error. Give a warning and
2657 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002658 smsg((char_u *)
2659 _("Warning: region %s not supported"),
2660 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002661 }
2662 else
2663 region_mask = 1 << c;
2664 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002665
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002666 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002667 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002668 if (ga_grow(&ga, 1) == FAIL)
2669 {
2670 ga_clear(&ga);
2671 return e_outofmem;
2672 }
2673 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2674 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2675 ++ga.ga_len;
2676 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002677 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002678 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002679 }
2680
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002681 /* round 0: load int_wordlist, if possible.
2682 * round 1: load first name in 'spellfile'.
2683 * round 2: load second name in 'spellfile.
2684 * etc. */
2685 spf = curbuf->b_p_spf;
2686 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002687 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002688 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002689 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002690 /* Internal wordlist, if there is one. */
2691 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002692 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002693 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002694 }
2695 else
2696 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002697 /* One entry in 'spellfile'. */
2698 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2699 STRCAT(spf_name, ".spl");
2700
2701 /* If it was already found above then skip it. */
2702 for (c = 0; c < ga.ga_len; ++c)
2703 if (fullpathcmp(spf_name,
2704 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
2705 FALSE) == FPC_SAME)
2706 break;
2707 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002708 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002709 }
2710
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002711 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002712 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2713 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2714 break;
2715 if (lp == NULL)
2716 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002717 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002718 * region name, the region is ignored otherwise. for int_wordlist
2719 * use an arbitrary name. */
2720 if (round == 0)
2721 STRCPY(lang, "internal wordlist");
2722 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002723 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002724 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002725 p = vim_strchr(lang, '.');
2726 if (p != NULL)
2727 *p = NUL; /* truncate at ".encoding.add" */
2728 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002729 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002730 }
2731 if (lp != NULL && ga_grow(&ga, 1) == OK)
2732 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002733 region_mask = REGION_ALL;
2734 if (use_region != NULL && !dont_use_region)
2735 {
2736 /* find region in sl_regions */
2737 c = find_region(lp->sl_regions, use_region);
2738 if (c != REGION_ALL)
2739 region_mask = 1 << c;
2740 else if (*lp->sl_regions != NUL)
2741 /* This spell file is for other regions. */
2742 region_mask = 0;
2743 }
2744
2745 if (region_mask != 0)
2746 {
2747 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2748 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2749 ++ga.ga_len;
2750 use_midword(lp, buf);
2751 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002752 }
2753 }
2754
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002755 /* Add a NULL entry to mark the end of the list. */
2756 if (ga_grow(&ga, 1) == FAIL)
2757 {
2758 ga_clear(&ga);
2759 return e_outofmem;
2760 }
2761 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2762 ++ga.ga_len;
2763
2764 /* Everything is fine, store the new b_langp value. */
2765 ga_clear(&buf->b_langp);
2766 buf->b_langp = ga;
2767
2768 return NULL;
2769}
2770
2771/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002772 * Clear the midword characters for buffer "buf".
2773 */
2774 static void
2775clear_midword(buf)
2776 buf_T *buf;
2777{
2778 vim_memset(buf->b_spell_ismw, 0, 256);
2779#ifdef FEAT_MBYTE
2780 vim_free(buf->b_spell_ismw_mb);
2781 buf->b_spell_ismw_mb = NULL;
2782#endif
2783}
2784
2785/*
2786 * Use the "sl_midword" field of language "lp" for buffer "buf".
2787 * They add up to any currently used midword characters.
2788 */
2789 static void
2790use_midword(lp, buf)
2791 slang_T *lp;
2792 buf_T *buf;
2793{
2794 char_u *p;
2795
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002796 if (lp->sl_midword == NULL) /* there aren't any */
2797 return;
2798
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002799 for (p = lp->sl_midword; *p != NUL; )
2800#ifdef FEAT_MBYTE
2801 if (has_mbyte)
2802 {
2803 int c, l, n;
2804 char_u *bp;
2805
2806 c = mb_ptr2char(p);
2807 l = mb_ptr2len_check(p);
2808 if (c < 256)
2809 buf->b_spell_ismw[c] = TRUE;
2810 else if (buf->b_spell_ismw_mb == NULL)
2811 /* First multi-byte char in "b_spell_ismw_mb". */
2812 buf->b_spell_ismw_mb = vim_strnsave(p, l);
2813 else
2814 {
2815 /* Append multi-byte chars to "b_spell_ismw_mb". */
2816 n = STRLEN(buf->b_spell_ismw_mb);
2817 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
2818 if (bp != NULL)
2819 {
2820 vim_free(buf->b_spell_ismw_mb);
2821 buf->b_spell_ismw_mb = bp;
2822 vim_strncpy(bp + n, p, l);
2823 }
2824 }
2825 p += l;
2826 }
2827 else
2828#endif
2829 buf->b_spell_ismw[*p++] = TRUE;
2830}
2831
2832/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002833 * Find the region "region[2]" in "rp" (points to "sl_regions").
2834 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002835 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002836 */
2837 static int
2838find_region(rp, region)
2839 char_u *rp;
2840 char_u *region;
2841{
2842 int i;
2843
2844 for (i = 0; ; i += 2)
2845 {
2846 if (rp[i] == NUL)
2847 return REGION_ALL;
2848 if (rp[i] == region[0] && rp[i + 1] == region[1])
2849 break;
2850 }
2851 return i / 2;
2852}
2853
2854/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002855 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002856 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002857 * Word WF_ONECAP
2858 * W WORD WF_ALLCAP
2859 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002860 */
2861 static int
2862captype(word, end)
2863 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002864 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002865{
2866 char_u *p;
2867 int c;
2868 int firstcap;
2869 int allcap;
2870 int past_second = FALSE; /* past second word char */
2871
2872 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002873 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002874 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002875 return 0; /* only non-word characters, illegal word */
2876#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002877 if (has_mbyte)
2878 c = mb_ptr2char_adv(&p);
2879 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002880#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002881 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002882 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002883
2884 /*
2885 * Need to check all letters to find a word with mixed upper/lower.
2886 * But a word with an upper char only at start is a ONECAP.
2887 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002888 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002889 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002890 {
2891#ifdef FEAT_MBYTE
2892 c = mb_ptr2char(p);
2893#else
2894 c = *p;
2895#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002896 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002897 {
2898 /* UUl -> KEEPCAP */
2899 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002900 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002901 allcap = FALSE;
2902 }
2903 else if (!allcap)
2904 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002905 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002906 past_second = TRUE;
2907 }
2908
2909 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002910 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002911 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002912 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002913 return 0;
2914}
2915
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002916# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
2917/*
2918 * Free all languages.
2919 */
2920 void
2921spell_free_all()
2922{
2923 slang_T *lp;
2924 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002925 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002926
2927 /* Go through all buffers and handle 'spelllang'. */
2928 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2929 ga_clear(&buf->b_langp);
2930
2931 while (first_lang != NULL)
2932 {
2933 lp = first_lang;
2934 first_lang = lp->sl_next;
2935 slang_free(lp);
2936 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002937
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002938 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002939 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002940 /* Delete the internal wordlist and its .spl file */
2941 mch_remove(int_wordlist);
2942 int_wordlist_spl(fname);
2943 mch_remove(fname);
2944 vim_free(int_wordlist);
2945 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002946 }
2947
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002948 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002949}
2950# endif
2951
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002952# if defined(FEAT_MBYTE) || defined(PROTO)
2953/*
2954 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002955 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002956 */
2957 void
2958spell_reload()
2959{
2960 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002961 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002962
Bram Moolenaarea408852005-06-25 22:49:46 +00002963 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002964 init_spell_chartab();
2965
2966 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002967 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002968
2969 /* Go through all buffers and handle 'spelllang'. */
2970 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2971 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002972 /* Only load the wordlists when 'spelllang' is set and there is a
2973 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002974 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002975 {
2976 FOR_ALL_WINDOWS(wp)
2977 if (wp->w_buffer == buf && wp->w_p_spell)
2978 {
2979 (void)did_set_spelllang(buf);
2980# ifdef FEAT_WINDOWS
2981 break;
2982# endif
2983 }
2984 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002985 }
2986}
2987# endif
2988
Bram Moolenaarb765d632005-06-07 21:00:02 +00002989/*
2990 * Reload the spell file "fname" if it's loaded.
2991 */
2992 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002993spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002994 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002995 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002996{
2997 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002998 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002999
Bram Moolenaarb765d632005-06-07 21:00:02 +00003000 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3001 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
3002 {
3003 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003004 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003005 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003006 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003007 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003008
3009 /* When "zg" was used and the file wasn't loaded yet, should redo
3010 * 'spelllang' to get it loaded. */
3011 if (added_word && !didit)
3012 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003013}
3014
3015
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003016/*
3017 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003018 */
3019
Bram Moolenaar51485f02005-06-04 21:55:20 +00003020#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003021 and .dic file. */
3022/*
3023 * Main structure to store the contents of a ".aff" file.
3024 */
3025typedef struct afffile_S
3026{
3027 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003028 int af_rar; /* RAR ID for rare word */
3029 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003030 int af_bad; /* BAD ID for banned word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003031 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003032 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
3033 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003034} afffile_T;
3035
3036typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003037/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
3038struct affentry_S
3039{
3040 affentry_T *ae_next; /* next affix with same name/number */
3041 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
3042 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003043 char_u *ae_cond; /* condition (NULL for ".") */
3044 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003045 int ae_rare; /* rare affix */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003046};
3047
3048/* Affix header from ".aff" file. Used for af_pref and af_suff. */
3049typedef struct affheader_S
3050{
3051 char_u ah_key[2]; /* key for hashtable == name of affix entry */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003052 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003053 int ah_combine; /* suffix may combine with prefix */
3054 affentry_T *ah_first; /* first affix entry */
3055} affheader_T;
3056
3057#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
3058
3059/*
3060 * Structure that is used to store the items in the word tree. This avoids
3061 * the need to keep track of each allocated thing, it's freed all at once
3062 * after ":mkspell" is done.
3063 */
3064#define SBLOCKSIZE 16000 /* size of sb_data */
3065typedef struct sblock_S sblock_T;
3066struct sblock_S
3067{
3068 sblock_T *sb_next; /* next block in list */
3069 int sb_used; /* nr of bytes already in use */
3070 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003071};
3072
3073/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003074 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003075 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003076typedef struct wordnode_S wordnode_T;
3077struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003078{
Bram Moolenaar0c405862005-06-22 22:26:26 +00003079 union /* shared to save space */
3080 {
3081 char_u hashkey[6]; /* room for the hash key */
3082 int index; /* index in written nodes (valid after first
3083 round) */
3084 } wn_u1;
3085 union /* shared to save space */
3086 {
3087 wordnode_T *next; /* next node with same hash key */
3088 wordnode_T *wnode; /* parent node that will write this node */
3089 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003090 wordnode_T *wn_child; /* child (next byte in word) */
3091 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
3092 always sorted) */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003093 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003094 short_u wn_flags; /* when wn_byte is NUL: WF_ flags */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003095 short wn_region; /* when wn_byte is NUL: region mask; for
3096 PREFIXTREE it's the prefcondnr */
3097 char_u wn_prefixID; /* supported/required prefix ID or 0 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003098};
3099
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003100#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
3101
Bram Moolenaar51485f02005-06-04 21:55:20 +00003102#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003103
Bram Moolenaar51485f02005-06-04 21:55:20 +00003104/*
3105 * Info used while reading the spell files.
3106 */
3107typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003108{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003109 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003110 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003111 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003112 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003113 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003114 sblock_T *si_blocks; /* memory blocks used */
3115 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003116 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003117 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003118 int si_region; /* region mask */
3119 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003120 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003121 int si_verbose; /* verbose messages */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003122 int si_region_count; /* number of regions supported (1 when there
3123 are no regions) */
3124 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003125
3126 garray_T si_rep; /* list of fromto_T entries from REP lines */
3127 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003128 char_u *si_sofofr; /* SOFOFROM text */
3129 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003130 int si_followup; /* soundsalike: ? */
3131 int si_collapse; /* soundsalike: ? */
3132 int si_rem_accents; /* soundsalike: remove accents */
3133 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003134 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003135 garray_T si_prefcond; /* table with conditions for postponed
3136 * prefixes, each stored as a string */
3137 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003138} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003139
Bram Moolenaar51485f02005-06-04 21:55:20 +00003140static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003141static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003142static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
3143static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00003144static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003145static void spell_free_aff __ARGS((afffile_T *aff));
3146static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003147static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp));
3148static 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 +00003149static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
3150static void *getroom __ARGS((sblock_T **blp, size_t len));
3151static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
3152static void free_blocks __ARGS((sblock_T *bl));
3153static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003154static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist));
3155static 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 +00003156static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003157static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
3158static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar3982c542005-06-08 21:56:31 +00003159static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar0c405862005-06-22 22:26:26 +00003160static void clear_node __ARGS((wordnode_T *node));
3161static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003162static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003163static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003164
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003165/* values for wn_flags used in the postponed prefixes tree */
3166#define PFX_FLAGS -1 /* not rare, combining */
3167#define PFX_FLAGS_R -2 /* rare, combining */
3168#define PFX_FLAGS_NC -3 /* not rare, not combining */
3169#define PFX_FLAGS_RNC -4 /* rare, not combining */
3170
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003171/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003172 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00003173 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003174 */
3175 static afffile_T *
Bram Moolenaar51485f02005-06-04 21:55:20 +00003176spell_read_aff(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003177 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003178 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003179{
3180 FILE *fd;
3181 afffile_T *aff;
3182 char_u rline[MAXLINELEN];
3183 char_u *line;
3184 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003185#define MAXITEMCNT 7
3186 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003187 int itemcnt;
3188 char_u *p;
3189 int lnum = 0;
3190 affheader_T *cur_aff = NULL;
3191 int aff_todo = 0;
3192 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003193 char_u *low = NULL;
3194 char_u *fol = NULL;
3195 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003196 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003197 int do_rep;
3198 int do_sal;
3199 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003200 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003201 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003202 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003203 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003204
Bram Moolenaar51485f02005-06-04 21:55:20 +00003205 /*
3206 * Open the file.
3207 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003208 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003209 if (fd == NULL)
3210 {
3211 EMSG2(_(e_notopen), fname);
3212 return NULL;
3213 }
3214
Bram Moolenaarb765d632005-06-07 21:00:02 +00003215 if (spin->si_verbose || p_verbose > 2)
3216 {
3217 if (!spin->si_verbose)
3218 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003219 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003220 out_flush();
3221 if (!spin->si_verbose)
3222 verbose_leave();
3223 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003224
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003225 /* Only do REP lines when not done in another .aff file already. */
3226 do_rep = spin->si_rep.ga_len == 0;
3227
3228 /* Only do SAL lines when not done in another .aff file already. */
3229 do_sal = spin->si_sal.ga_len == 0;
3230
3231 /* Only do MAP lines when not done in another .aff file already. */
3232 do_map = spin->si_map.ga_len == 0;
3233
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003234 /* Only do MIDWORD line when not done in another .aff file already */
3235 do_midword = spin->si_midword == NULL;
3236
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003237 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
3238 do_sofo = spin->si_sofofr == NULL;
3239
Bram Moolenaar51485f02005-06-04 21:55:20 +00003240 /*
3241 * Allocate and init the afffile_T structure.
3242 */
3243 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003244 if (aff == NULL)
3245 return NULL;
3246 hash_init(&aff->af_pref);
3247 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003248
3249 /*
3250 * Read all the lines in the file one by one.
3251 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003252 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003253 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003254 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003255 ++lnum;
3256
3257 /* Skip comment lines. */
3258 if (*rline == '#')
3259 continue;
3260
3261 /* Convert from "SET" to 'encoding' when needed. */
3262 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003263#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003264 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003265 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003266 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003267 if (pc == NULL)
3268 {
3269 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3270 fname, lnum, rline);
3271 continue;
3272 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003273 line = pc;
3274 }
3275 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003276#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003277 {
3278 pc = NULL;
3279 line = rline;
3280 }
3281
3282 /* Split the line up in white separated items. Put a NUL after each
3283 * item. */
3284 itemcnt = 0;
3285 for (p = line; ; )
3286 {
3287 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
3288 ++p;
3289 if (*p == NUL)
3290 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003291 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003292 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003293 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003294 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003295 ++p;
3296 if (*p == NUL)
3297 break;
3298 *p++ = NUL;
3299 }
3300
3301 /* Handle non-empty lines. */
3302 if (itemcnt > 0)
3303 {
3304 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
3305 && aff->af_enc == NULL)
3306 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003307#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003308 /* Setup for conversion from "ENC" to 'encoding'. */
3309 aff->af_enc = enc_canonize(items[1]);
3310 if (aff->af_enc != NULL && !spin->si_ascii
3311 && convert_setup(&spin->si_conv, aff->af_enc,
3312 p_enc) == FAIL)
3313 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
3314 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003315 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003316#else
3317 smsg((char_u *)_("Conversion in %s not supported"), fname);
3318#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003319 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003320 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
3321 {
3322 if (do_midword)
3323 spin->si_midword = vim_strsave(items[1]);
3324 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00003325 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
3326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003327 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003328 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003329 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003331 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003332 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003333 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
3334 && aff->af_rar == 0)
3335 {
3336 aff->af_rar = items[1][0];
3337 if (items[1][1] != NUL)
3338 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3339 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003340 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
3341 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003342 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003343 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003344 if (items[1][1] != NUL)
3345 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3346 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00003347 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
3348 && aff->af_bad == 0)
3349 {
3350 aff->af_bad = items[1][0];
3351 if (items[1][1] != NUL)
3352 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3353 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003354 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
3355 {
3356 aff->af_pfxpostpone = TRUE;
3357 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003358 else if ((STRCMP(items[0], "PFX") == 0
3359 || STRCMP(items[0], "SFX") == 0)
3360 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003361 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003362 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00003363 /* Myspell allows extra text after the item, but that might
3364 * mean mistakes go unnoticed. Require a comment-starter. */
3365 if (itemcnt > 4 && *items[4] != '#')
3366 smsg((char_u *)_("Trailing text in %s line %d: %s"),
3367 fname, lnum, items[4]);
3368
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003369 /* New affix letter. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003370 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
3371 sizeof(affheader_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003372 if (cur_aff == NULL)
3373 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003374 cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003375 cur_aff->ah_key[1] = NUL;
3376 if (items[1][1] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003377 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003378 if (*items[2] == 'Y')
3379 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003380 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003381 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
3382 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003383
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003384 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003385 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003386 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003387 /* Use a new number in the .spl file later, to be able to
3388 * handle multiple .aff files. */
3389 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003390 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003391 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003392 else
3393 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003394 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003395 hi = hash_find(tp, cur_aff->ah_key);
3396 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00003397 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003398 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
3399 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003400 aff_todo = 0;
3401 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003402 else
3403 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003404 }
3405 else if ((STRCMP(items[0], "PFX") == 0
3406 || STRCMP(items[0], "SFX") == 0)
3407 && aff_todo > 0
3408 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003409 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003410 {
3411 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003412 int rare = FALSE;
3413 int lasti = 5;
3414
3415 /* Check for "rare" after the other info. */
3416 if (itemcnt > 5 && STRICMP(items[5], "rare") == 0)
3417 {
3418 rare = TRUE;
3419 lasti = 6;
3420 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003421
Bram Moolenaar8db73182005-06-17 21:51:16 +00003422 /* Myspell allows extra text after the item, but that might
3423 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003424 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00003425 smsg((char_u *)_("Trailing text in %s line %d: %s"),
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003426 fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00003427
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003428 /* New item for an affix letter. */
3429 --aff_todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003430 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
3431 sizeof(affentry_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003432 if (aff_entry == NULL)
3433 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003434 aff_entry->ae_rare = rare;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003435
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003436 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003437 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
3438 items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003439 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003440 aff_entry->ae_add = getroom_save(&spin->si_blocks,
3441 items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003442
Bram Moolenaar51485f02005-06-04 21:55:20 +00003443 /* Don't use an affix entry with non-ASCII characters when
3444 * "spin->si_ascii" is TRUE. */
3445 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003446 || has_non_ascii(aff_entry->ae_add)))
3447 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003448 aff_entry->ae_next = cur_aff->ah_first;
3449 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003450
3451 if (STRCMP(items[4], ".") != 0)
3452 {
3453 char_u buf[MAXLINELEN];
3454
3455 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
3456 items[4]);
3457 if (*items[0] == 'P')
3458 sprintf((char *)buf, "^%s", items[4]);
3459 else
3460 sprintf((char *)buf, "%s$", items[4]);
3461 aff_entry->ae_prog = vim_regcomp(buf,
3462 RE_MAGIC + RE_STRING);
3463 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003464
3465 /* For postponed prefixes we need an entry in si_prefcond
3466 * for the condition. Use an existing one if possible. */
3467 if (*items[0] == 'P' && aff->af_pfxpostpone
3468 && aff_entry->ae_chop == NULL)
3469 {
3470 int idx;
3471 char_u **pp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003472 int n;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003473
3474 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
3475 --idx)
3476 {
3477 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
3478 if (str_equal(p, aff_entry->ae_cond))
3479 break;
3480 }
3481 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
3482 {
3483 /* Not found, add a new condition. */
3484 idx = spin->si_prefcond.ga_len++;
3485 pp = ((char_u **)spin->si_prefcond.ga_data) + idx;
3486 if (aff_entry->ae_cond == NULL)
3487 *pp = NULL;
3488 else
3489 *pp = getroom_save(&spin->si_blocks,
3490 aff_entry->ae_cond);
3491 }
3492
3493 /* Add the prefix to the prefix tree. */
3494 if (aff_entry->ae_add == NULL)
3495 p = (char_u *)"";
3496 else
3497 p = aff_entry->ae_add;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003498 if (rare)
3499 {
3500 if (cur_aff->ah_combine)
3501 n = PFX_FLAGS_R;
3502 else
3503 n = PFX_FLAGS_RNC;
3504 }
3505 else
3506 {
3507 if (cur_aff->ah_combine)
3508 n = PFX_FLAGS;
3509 else
3510 n = PFX_FLAGS_NC;
3511 }
3512 tree_add_word(p, spin->si_prefroot, n,
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003513 idx, cur_aff->ah_newID, &spin->si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003514 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003515 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003516 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003517 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
3518 {
3519 if (fol != NULL)
3520 smsg((char_u *)_("Duplicate FOL in %s line %d"),
3521 fname, lnum);
3522 else
3523 fol = vim_strsave(items[1]);
3524 }
3525 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
3526 {
3527 if (low != NULL)
3528 smsg((char_u *)_("Duplicate LOW in %s line %d"),
3529 fname, lnum);
3530 else
3531 low = vim_strsave(items[1]);
3532 }
3533 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
3534 {
3535 if (upp != NULL)
3536 smsg((char_u *)_("Duplicate UPP in %s line %d"),
3537 fname, lnum);
3538 else
3539 upp = vim_strsave(items[1]);
3540 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003541 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003542 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003543 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003544 if (!isdigit(*items[1]))
3545 smsg((char_u *)_("Expected REP count in %s line %d"),
3546 fname, lnum);
3547 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003548 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
3549 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003550 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003551 if (do_rep)
3552 add_fromto(spin, &spin->si_rep, items[1], items[2]);
3553 }
3554 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
3555 {
3556 /* MAP item or count */
3557 if (!found_map)
3558 {
3559 /* First line contains the count. */
3560 found_map = TRUE;
3561 if (!isdigit(*items[1]))
3562 smsg((char_u *)_("Expected MAP count in %s line %d"),
3563 fname, lnum);
3564 }
3565 else if (do_map)
3566 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003567 int c;
3568
3569 /* Check that every character appears only once. */
3570 for (p = items[1]; *p != NUL; )
3571 {
3572#ifdef FEAT_MBYTE
3573 c = mb_ptr2char_adv(&p);
3574#else
3575 c = *p++;
3576#endif
3577 if ((spin->si_map.ga_len > 0
3578 && vim_strchr(spin->si_map.ga_data, c)
3579 != NULL)
3580 || vim_strchr(p, c) != NULL)
3581 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
3582 fname, lnum);
3583 }
3584
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003585 /* We simply concatenate all the MAP strings, separated by
3586 * slashes. */
3587 ga_concat(&spin->si_map, items[1]);
3588 ga_append(&spin->si_map, '/');
3589 }
3590 }
3591 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
3592 {
3593 if (do_sal)
3594 {
3595 /* SAL item (sounds-a-like)
3596 * Either one of the known keys or a from-to pair. */
3597 if (STRCMP(items[1], "followup") == 0)
3598 spin->si_followup = sal_to_bool(items[2]);
3599 else if (STRCMP(items[1], "collapse_result") == 0)
3600 spin->si_collapse = sal_to_bool(items[2]);
3601 else if (STRCMP(items[1], "remove_accents") == 0)
3602 spin->si_rem_accents = sal_to_bool(items[2]);
3603 else
3604 /* when "to" is "_" it means empty */
3605 add_fromto(spin, &spin->si_sal, items[1],
3606 STRCMP(items[2], "_") == 0 ? (char_u *)""
3607 : items[2]);
3608 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003609 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003610 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
3611 && (!do_sofo || spin->si_sofofr == NULL))
3612 {
3613 if (do_sofo)
3614 spin->si_sofofr = vim_strsave(items[1]);
3615 }
3616 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
3617 && (!do_sofo || spin->si_sofoto == NULL))
3618 {
3619 if (do_sofo)
3620 spin->si_sofoto = vim_strsave(items[1]);
3621 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003622 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003623 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
3624 fname, lnum, items[0]);
3625 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003626 }
3627
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003628 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
3629 smsg((char_u *)_("Missing SOFO%s line in %s"),
3630 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
3631 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
3632 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
3633
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003634 if (fol != NULL || low != NULL || upp != NULL)
3635 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003636 if (spin->si_clear_chartab)
3637 {
3638 /* Clear the char type tables, don't want to use any of the
3639 * currently used spell properties. */
3640 init_spell_chartab();
3641 spin->si_clear_chartab = FALSE;
3642 }
3643
Bram Moolenaar3982c542005-06-08 21:56:31 +00003644 /*
3645 * Don't write a word table for an ASCII file, so that we don't check
3646 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003647 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00003648 * mb_get_class(), the list of chars in the file will be incomplete.
3649 */
3650 if (!spin->si_ascii
3651#ifdef FEAT_MBYTE
3652 && !enc_utf8
3653#endif
3654 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003655 {
3656 if (fol == NULL || low == NULL || upp == NULL)
3657 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
3658 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00003659 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003660 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003661
3662 vim_free(fol);
3663 vim_free(low);
3664 vim_free(upp);
3665 }
3666
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003667 vim_free(pc);
3668 fclose(fd);
3669 return aff;
3670}
3671
3672/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003673 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3674 * NULL as equal.
3675 */
3676 static int
3677str_equal(s1, s2)
3678 char_u *s1;
3679 char_u *s2;
3680{
3681 if (s1 == NULL || s2 == NULL)
3682 return s1 == s2;
3683 return STRCMP(s1, s2) == 0;
3684}
3685
3686/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003687 * Add a from-to item to "gap". Used for REP and SAL items.
3688 * They are stored case-folded.
3689 */
3690 static void
3691add_fromto(spin, gap, from, to)
3692 spellinfo_T *spin;
3693 garray_T *gap;
3694 char_u *from;
3695 char_u *to;
3696{
3697 fromto_T *ftp;
3698 char_u word[MAXWLEN];
3699
3700 if (ga_grow(gap, 1) == OK)
3701 {
3702 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3703 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
3704 ftp->ft_from = getroom_save(&spin->si_blocks, word);
3705 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
3706 ftp->ft_to = getroom_save(&spin->si_blocks, word);
3707 ++gap->ga_len;
3708 }
3709}
3710
3711/*
3712 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3713 */
3714 static int
3715sal_to_bool(s)
3716 char_u *s;
3717{
3718 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3719}
3720
3721/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00003722 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
3723 * When "s" is NULL FALSE is returned.
3724 */
3725 static int
3726has_non_ascii(s)
3727 char_u *s;
3728{
3729 char_u *p;
3730
3731 if (s != NULL)
3732 for (p = s; *p != NUL; ++p)
3733 if (*p >= 128)
3734 return TRUE;
3735 return FALSE;
3736}
3737
3738/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003739 * Free the structure filled by spell_read_aff().
3740 */
3741 static void
3742spell_free_aff(aff)
3743 afffile_T *aff;
3744{
3745 hashtab_T *ht;
3746 hashitem_T *hi;
3747 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003748 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003749 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003750
3751 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003752
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003753 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003754 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3755 {
3756 todo = ht->ht_used;
3757 for (hi = ht->ht_array; todo > 0; ++hi)
3758 {
3759 if (!HASHITEM_EMPTY(hi))
3760 {
3761 --todo;
3762 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003763 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3764 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003765 }
3766 }
3767 if (ht == &aff->af_suff)
3768 break;
3769 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003770
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003771 hash_clear(&aff->af_pref);
3772 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003773}
3774
3775/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003776 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003777 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003778 */
3779 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00003780spell_read_dic(fname, spin, affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003781 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003782 spellinfo_T *spin;
3783 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003784{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003785 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003786 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00003787 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003788 char_u *pfxlist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003789 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003790 char_u *pc;
3791 char_u *w;
3792 int l;
3793 hash_T hash;
3794 hashitem_T *hi;
3795 FILE *fd;
3796 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003797 int non_ascii = 0;
3798 int retval = OK;
3799 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003800 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003801
Bram Moolenaar51485f02005-06-04 21:55:20 +00003802 /*
3803 * Open the file.
3804 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003805 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003806 if (fd == NULL)
3807 {
3808 EMSG2(_(e_notopen), fname);
3809 return FAIL;
3810 }
3811
Bram Moolenaar51485f02005-06-04 21:55:20 +00003812 /* The hashtable is only used to detect duplicated words. */
3813 hash_init(&ht);
3814
Bram Moolenaar8db73182005-06-17 21:51:16 +00003815 spin->si_foldwcount = 0;
3816 spin->si_keepwcount = 0;
3817
Bram Moolenaarb765d632005-06-07 21:00:02 +00003818 if (spin->si_verbose || p_verbose > 2)
3819 {
3820 if (!spin->si_verbose)
3821 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003822 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003823 out_flush();
3824 if (!spin->si_verbose)
3825 verbose_leave();
3826 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003827
3828 /* Read and ignore the first line: word count. */
3829 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003830 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003831 EMSG2(_("E760: No word count in %s"), fname);
3832
3833 /*
3834 * Read all the lines in the file one by one.
3835 * The words are converted to 'encoding' here, before being added to
3836 * the hashtable.
3837 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003838 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003839 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003840 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003841 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003842 if (line[0] == '#')
3843 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003844
Bram Moolenaar51485f02005-06-04 21:55:20 +00003845 /* Remove CR, LF and white space from the end. White space halfway
3846 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003847 l = STRLEN(line);
3848 while (l > 0 && line[l - 1] <= ' ')
3849 --l;
3850 if (l == 0)
3851 continue; /* empty line */
3852 line[l] = NUL;
3853
Bram Moolenaar51485f02005-06-04 21:55:20 +00003854 /* Find the optional affix names. */
3855 afflist = vim_strchr(line, '/');
3856 if (afflist != NULL)
3857 *afflist++ = NUL;
3858
3859 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
3860 if (spin->si_ascii && has_non_ascii(line))
3861 {
3862 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003863 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003864 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003865
Bram Moolenaarb765d632005-06-07 21:00:02 +00003866#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003867 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003868 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003869 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003870 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003871 if (pc == NULL)
3872 {
3873 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3874 fname, lnum, line);
3875 continue;
3876 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003877 w = pc;
3878 }
3879 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003880#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003881 {
3882 pc = NULL;
3883 w = line;
3884 }
3885
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003886 /* This takes time, print a message now and then. */
3887 if (spin->si_verbose && (lnum & 0x3ff) == 0)
3888 {
3889 vim_snprintf((char *)message, sizeof(message),
3890 _("line %6d, word %6d - %s"),
3891 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3892 msg_start();
3893 msg_puts_long_attr(message, 0);
3894 msg_clr_eos();
3895 msg_didout = FALSE;
3896 msg_col = 0;
3897 out_flush();
3898 }
3899
Bram Moolenaar51485f02005-06-04 21:55:20 +00003900 /* Store the word in the hashtable to be able to find duplicates. */
3901 dw = (char_u *)getroom_save(&spin->si_blocks, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003902 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003903 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003904 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003905 if (retval == FAIL)
3906 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003907
Bram Moolenaar51485f02005-06-04 21:55:20 +00003908 hash = hash_hash(dw);
3909 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003910 if (!HASHITEM_EMPTY(hi))
3911 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003912 fname, lnum, dw);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003913 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00003914 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003915
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003916 flags = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003917 pfxlist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003918 if (afflist != NULL)
3919 {
3920 /* Check for affix name that stands for keep-case word and stands
3921 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003922 if (affile->af_kep != NUL
3923 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003924 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003925 if (affile->af_rar != NUL
3926 && vim_strchr(afflist, affile->af_rar) != NULL)
3927 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003928 if (affile->af_bad != NUL
3929 && vim_strchr(afflist, affile->af_bad) != NULL)
3930 flags |= WF_BANNED;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003931
3932 if (affile->af_pfxpostpone)
3933 /* Need to store the list of prefix IDs with the word. */
3934 pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003935 }
3936
Bram Moolenaar51485f02005-06-04 21:55:20 +00003937 /* Add the word to the word tree(s). */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003938 if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003939 retval = FAIL;
3940
3941 if (afflist != NULL)
3942 {
3943 /* Find all matching suffixes and add the resulting words.
3944 * Additionally do matching prefixes that combine. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003945 if (store_aff_word(dw, spin, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003946 &affile->af_suff, &affile->af_pref,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003947 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003948 retval = FAIL;
3949
3950 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003951 if (store_aff_word(dw, spin, afflist, affile,
3952 &affile->af_pref, NULL,
3953 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003954 retval = FAIL;
3955 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003956 }
3957
Bram Moolenaar51485f02005-06-04 21:55:20 +00003958 if (spin->si_ascii && non_ascii > 0)
3959 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
3960 non_ascii);
3961 hash_clear(&ht);
3962
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003963 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003964 return retval;
3965}
3966
3967/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003968 * Get the list of prefix IDs from the affix list "afflist".
3969 * Used for PFXPOSTPONE.
3970 * Returns a string allocated with getroom(). NULL when there are no prefixes
3971 * or when out of memory.
3972 */
3973 static char_u *
3974get_pfxlist(affile, afflist, blp)
3975 afffile_T *affile;
3976 char_u *afflist;
3977 sblock_T **blp;
3978{
3979 char_u *p;
3980 int cnt;
3981 int round;
3982 char_u *res = NULL;
3983 char_u key[2];
3984 hashitem_T *hi;
3985
3986 key[1] = NUL;
3987
3988 /* round 1: count the number of prefix IDs.
3989 * round 2: move prefix IDs to "res" */
3990 for (round = 1; round <= 2; ++round)
3991 {
3992 cnt = 0;
3993 for (p = afflist; *p != NUL; ++p)
3994 {
3995 key[0] = *p;
3996 hi = hash_find(&affile->af_pref, key);
3997 if (!HASHITEM_EMPTY(hi))
3998 {
3999 /* This is a prefix ID, use the new number. */
4000 if (round == 2)
4001 res[cnt] = HI2AH(hi)->ah_newID;
4002 ++cnt;
4003 }
4004 }
4005 if (round == 1 && cnt > 0)
4006 res = getroom(blp, cnt + 1);
4007 if (res == NULL)
4008 break;
4009 }
4010
4011 if (res != NULL)
4012 res[cnt] = NUL;
4013 return res;
4014}
4015
4016/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004017 * Apply affixes to a word and store the resulting words.
4018 * "ht" is the hashtable with affentry_T that need to be applied, either
4019 * prefixes or suffixes.
4020 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
4021 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004022 *
4023 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004024 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004025 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004026store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004027 char_u *word; /* basic word start */
4028 spellinfo_T *spin; /* spell info */
4029 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004030 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004031 hashtab_T *ht;
4032 hashtab_T *xht;
4033 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004034 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004035 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004036{
4037 int todo;
4038 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004039 affheader_T *ah;
4040 affentry_T *ae;
4041 regmatch_T regmatch;
4042 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004043 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004044 int i;
4045 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004046 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004047 char_u *use_pfxlist;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004048
Bram Moolenaar51485f02005-06-04 21:55:20 +00004049 todo = ht->ht_used;
4050 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004051 {
4052 if (!HASHITEM_EMPTY(hi))
4053 {
4054 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004055 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004056
Bram Moolenaar51485f02005-06-04 21:55:20 +00004057 /* Check that the affix combines, if required, and that the word
4058 * supports this affix. */
4059 if ((!comb || ah->ah_combine)
4060 && vim_strchr(afflist, *ah->ah_key) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004061 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004062 /* Loop over all affix entries with this name. */
4063 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004064 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004065 /* Check the condition. It's not logical to match case
4066 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004067 * Myspell.
4068 * For prefixes, when "PFXPOSTPONE" was used, only do
4069 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004070 regmatch.regprog = ae->ae_prog;
4071 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004072 if ((xht != NULL || !affile->af_pfxpostpone
4073 || ae->ae_chop != NULL)
4074 && (ae->ae_prog == NULL
4075 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004076 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004077 /* Match. Remove the chop and add the affix. */
4078 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004079 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004080 /* prefix: chop/add at the start of the word */
4081 if (ae->ae_add == NULL)
4082 *newword = NUL;
4083 else
4084 STRCPY(newword, ae->ae_add);
4085 p = word;
4086 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004087 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004088 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004089#ifdef FEAT_MBYTE
4090 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004091 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004092 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004093 for ( ; i > 0; --i)
4094 mb_ptr_adv(p);
4095 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004096 else
4097#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004098 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004099 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004100 STRCAT(newword, p);
4101 }
4102 else
4103 {
4104 /* suffix: chop/add at the end of the word */
4105 STRCPY(newword, word);
4106 if (ae->ae_chop != NULL)
4107 {
4108 /* Remove chop string. */
4109 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004110 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004111 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004112 mb_ptr_back(newword, p);
4113 *p = NUL;
4114 }
4115 if (ae->ae_add != NULL)
4116 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004117 }
4118
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004119 /* Obey the "rare" flag of the affix. */
4120 if (ae->ae_rare)
4121 use_flags = flags | WF_RARE;
4122 else
4123 use_flags = flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004124 use_pfxlist = pfxlist;
4125
4126 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00004127 if (spin->si_prefroot != NULL
4128 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004129 {
4130 /* ... add a flag to indicate an affix was used. */
4131 use_flags |= WF_HAS_AFF;
4132
4133 /* ... don't use a prefix list if combining
4134 * affixes is not allowed */
4135 if (!ah->ah_combine || comb)
4136 use_pfxlist = NULL;
4137 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004138
Bram Moolenaar51485f02005-06-04 21:55:20 +00004139 /* Store the modified word. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004140 if (store_word(newword, spin, use_flags,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004141 spin->si_region, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004142 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004143
Bram Moolenaar51485f02005-06-04 21:55:20 +00004144 /* When added a suffix and combining is allowed also
4145 * try adding prefixes additionally. */
4146 if (xht != NULL && ah->ah_combine)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004147 if (store_aff_word(newword, spin, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004148 xht, NULL, TRUE,
4149 use_flags, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004150 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004151 }
4152 }
4153 }
4154 }
4155 }
4156
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004157 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004158}
4159
4160/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004161 * Read a file with a list of words.
4162 */
4163 static int
4164spell_read_wordfile(fname, spin)
4165 char_u *fname;
4166 spellinfo_T *spin;
4167{
4168 FILE *fd;
4169 long lnum = 0;
4170 char_u rline[MAXLINELEN];
4171 char_u *line;
4172 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004173 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004174 int l;
4175 int retval = OK;
4176 int did_word = FALSE;
4177 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004178 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004179 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004180
4181 /*
4182 * Open the file.
4183 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004184 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004185 if (fd == NULL)
4186 {
4187 EMSG2(_(e_notopen), fname);
4188 return FAIL;
4189 }
4190
Bram Moolenaarb765d632005-06-07 21:00:02 +00004191 if (spin->si_verbose || p_verbose > 2)
4192 {
4193 if (!spin->si_verbose)
4194 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004195 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004196 out_flush();
4197 if (!spin->si_verbose)
4198 verbose_leave();
4199 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004200
4201 /*
4202 * Read all the lines in the file one by one.
4203 */
4204 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4205 {
4206 line_breakcheck();
4207 ++lnum;
4208
4209 /* Skip comment lines. */
4210 if (*rline == '#')
4211 continue;
4212
4213 /* Remove CR, LF and white space from the end. */
4214 l = STRLEN(rline);
4215 while (l > 0 && rline[l - 1] <= ' ')
4216 --l;
4217 if (l == 0)
4218 continue; /* empty or blank line */
4219 rline[l] = NUL;
4220
4221 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
4222 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004223#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004224 if (spin->si_conv.vc_type != CONV_NONE)
4225 {
4226 pc = string_convert(&spin->si_conv, rline, NULL);
4227 if (pc == NULL)
4228 {
4229 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4230 fname, lnum, rline);
4231 continue;
4232 }
4233 line = pc;
4234 }
4235 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004236#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004237 {
4238 pc = NULL;
4239 line = rline;
4240 }
4241
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004242 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00004243 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004244 ++line;
4245 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004246 {
4247 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004248 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
4249 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004250 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004251 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
4252 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004253 else
4254 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004255#ifdef FEAT_MBYTE
4256 char_u *enc;
4257
Bram Moolenaar51485f02005-06-04 21:55:20 +00004258 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004259 line += 10;
4260 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004261 if (enc != NULL && !spin->si_ascii
4262 && convert_setup(&spin->si_conv, enc,
4263 p_enc) == FAIL)
4264 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00004265 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004266 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004267 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004268#else
4269 smsg((char_u *)_("Conversion in %s not supported"), fname);
4270#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004271 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004272 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004273 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004274
Bram Moolenaar3982c542005-06-08 21:56:31 +00004275 if (STRNCMP(line, "regions=", 8) == 0)
4276 {
4277 if (spin->si_region_count > 1)
4278 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
4279 fname, lnum, line);
4280 else
4281 {
4282 line += 8;
4283 if (STRLEN(line) > 16)
4284 smsg((char_u *)_("Too many regions in %s line %d: %s"),
4285 fname, lnum, line);
4286 else
4287 {
4288 spin->si_region_count = STRLEN(line) / 2;
4289 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004290
4291 /* Adjust the mask for a word valid in all regions. */
4292 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004293 }
4294 }
4295 continue;
4296 }
4297
Bram Moolenaar7887d882005-07-01 22:33:52 +00004298 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
4299 fname, lnum, line - 1);
4300 continue;
4301 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004302
Bram Moolenaar7887d882005-07-01 22:33:52 +00004303 flags = 0;
4304 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004305
Bram Moolenaar7887d882005-07-01 22:33:52 +00004306 /* Check for flags and region after a slash. */
4307 p = vim_strchr(line, '/');
4308 if (p != NULL)
4309 {
4310 *p++ = NUL;
4311 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004312 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004313 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004314 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004315 else if (*p == '!') /* Bad, bad, wicked word. */
4316 flags |= WF_BANNED;
4317 else if (*p == '?') /* Rare word. */
4318 flags |= WF_RARE;
4319 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004320 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004321 if ((flags & WF_REGION) == 0) /* first one */
4322 regionmask = 0;
4323 flags |= WF_REGION;
4324
4325 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00004326 if (l > spin->si_region_count)
4327 {
4328 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00004329 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004330 break;
4331 }
4332 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004333 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00004334 else
4335 {
4336 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
4337 fname, lnum, p);
4338 break;
4339 }
4340 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004341 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004342 }
4343
4344 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4345 if (spin->si_ascii && has_non_ascii(line))
4346 {
4347 ++non_ascii;
4348 continue;
4349 }
4350
4351 /* Normal word: store it. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004352 if (store_word(line, spin, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004353 {
4354 retval = FAIL;
4355 break;
4356 }
4357 did_word = TRUE;
4358 }
4359
4360 vim_free(pc);
4361 fclose(fd);
4362
Bram Moolenaarb765d632005-06-07 21:00:02 +00004363 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
4364 {
4365 if (p_verbose > 2)
4366 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00004367 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4368 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004369 if (p_verbose > 2)
4370 verbose_leave();
4371 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004372 return retval;
4373}
4374
4375/*
4376 * Get part of an sblock_T, "len" bytes long.
4377 * This avoids calling free() for every little struct we use.
4378 * The memory is cleared to all zeros.
4379 * Returns NULL when out of memory.
4380 */
4381 static void *
4382getroom(blp, len)
4383 sblock_T **blp;
4384 size_t len; /* length needed */
4385{
4386 char_u *p;
4387 sblock_T *bl = *blp;
4388
4389 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4390 {
4391 /* Allocate a block of memory. This is not freed until much later. */
4392 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
4393 if (bl == NULL)
4394 return NULL;
4395 bl->sb_next = *blp;
4396 *blp = bl;
4397 bl->sb_used = 0;
4398 }
4399
4400 p = bl->sb_data + bl->sb_used;
4401 bl->sb_used += len;
4402
4403 return p;
4404}
4405
4406/*
4407 * Make a copy of a string into memory allocated with getroom().
4408 */
4409 static char_u *
4410getroom_save(blp, s)
4411 sblock_T **blp;
4412 char_u *s;
4413{
4414 char_u *sc;
4415
4416 sc = (char_u *)getroom(blp, STRLEN(s) + 1);
4417 if (sc != NULL)
4418 STRCPY(sc, s);
4419 return sc;
4420}
4421
4422
4423/*
4424 * Free the list of allocated sblock_T.
4425 */
4426 static void
4427free_blocks(bl)
4428 sblock_T *bl;
4429{
4430 sblock_T *next;
4431
4432 while (bl != NULL)
4433 {
4434 next = bl->sb_next;
4435 vim_free(bl);
4436 bl = next;
4437 }
4438}
4439
4440/*
4441 * Allocate the root of a word tree.
4442 */
4443 static wordnode_T *
4444wordtree_alloc(blp)
4445 sblock_T **blp;
4446{
4447 return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
4448}
4449
4450/*
4451 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004452 * Always store it in the case-folded tree. For a keep-case word this is
4453 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
4454 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004455 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004456 * When "pfxlist" is not NULL store the word for each postponed prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004457 */
4458 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004459store_word(word, spin, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004460 char_u *word;
4461 spellinfo_T *spin;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004462 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004463 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004464 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004465{
4466 int len = STRLEN(word);
4467 int ct = captype(word, word + len);
4468 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004469 int res = OK;
4470 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004471
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004472 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004473 for (p = pfxlist; res == OK; ++p)
4474 {
4475 res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
4476 region, p == NULL ? 0 : *p, &spin->si_blocks);
4477 if (p == NULL || *p == NUL)
4478 break;
4479 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004480 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004481
4482 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
Bram Moolenaar8db73182005-06-17 21:51:16 +00004483 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004484 for (p = pfxlist; res == OK; ++p)
4485 {
4486 res = tree_add_word(word, spin->si_keeproot, flags,
4487 region, p == NULL ? 0 : *p, &spin->si_blocks);
4488 if (p == NULL || *p == NUL)
4489 break;
4490 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004491 ++spin->si_keepwcount;
4492 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004493 return res;
4494}
4495
4496/*
4497 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004498 * When "flags" < 0 we are adding to the prefix tree where flags is used for
4499 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004500 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004501 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004502 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004503tree_add_word(word, root, flags, region, prefixID, blp)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004504 char_u *word;
4505 wordnode_T *root;
4506 int flags;
4507 int region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004508 int prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004509 sblock_T **blp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004510{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004511 wordnode_T *node = root;
4512 wordnode_T *np;
4513 wordnode_T **prev = NULL;
4514 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004515
Bram Moolenaar51485f02005-06-04 21:55:20 +00004516 /* Add each byte of the word to the tree, including the NUL at the end. */
4517 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004518 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004519 /* Look for the sibling that has the same character. They are sorted
4520 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004521 * higher byte value. For zero bytes (end of word) the sorting is
4522 * done on flags and then on prefixID
Bram Moolenaar51485f02005-06-04 21:55:20 +00004523 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004524 while (node != NULL
4525 && (node->wn_byte < word[i]
4526 || (node->wn_byte == NUL
4527 && (flags < 0
4528 ? node->wn_prefixID < prefixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004529 : node->wn_flags < (flags & WN_MASK)
4530 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004531 && node->wn_prefixID < prefixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004532 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004533 prev = &node->wn_sibling;
4534 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004535 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004536 if (node == NULL
4537 || node->wn_byte != word[i]
4538 || (word[i] == NUL
4539 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004540 || 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 /* Allocate a new node. */
4544 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
4545 if (np == NULL)
4546 return FAIL;
4547 np->wn_byte = word[i];
4548 *prev = np;
4549 np->wn_sibling = node;
4550 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004551 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004552
Bram Moolenaar51485f02005-06-04 21:55:20 +00004553 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004554 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004555 node->wn_flags = flags;
4556 node->wn_region |= region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004557 node->wn_prefixID = prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004558 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004559 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004560 prev = &node->wn_child;
4561 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004562 }
4563
4564 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004565}
4566
4567/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004568 * Compress a tree: find tails that are identical and can be shared.
4569 */
4570 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00004571wordtree_compress(root, spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004572 wordnode_T *root;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004573 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004574{
4575 hashtab_T ht;
4576 int n;
4577 int tot = 0;
4578
4579 if (root != NULL)
4580 {
4581 hash_init(&ht);
4582 n = node_compress(root, &ht, &tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004583 if (spin->si_verbose || p_verbose > 2)
4584 {
4585 if (!spin->si_verbose)
4586 verbose_enter();
4587 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00004588 n, tot, (tot - n) * 100 / tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004589 if (p_verbose > 2)
4590 verbose_leave();
4591 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004592 hash_clear(&ht);
4593 }
4594}
4595
4596/*
4597 * Compress a node, its siblings and its children, depth first.
4598 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004599 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004600 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00004601node_compress(node, ht, tot)
4602 wordnode_T *node;
4603 hashtab_T *ht;
4604 int *tot; /* total count of nodes before compressing,
4605 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004606{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004607 wordnode_T *np;
4608 wordnode_T *tp;
4609 wordnode_T *child;
4610 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004611 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004612 int len = 0;
4613 unsigned nr, n;
4614 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004615
Bram Moolenaar51485f02005-06-04 21:55:20 +00004616 /*
4617 * Go through the list of siblings. Compress each child and then try
4618 * finding an identical child to replace it.
4619 * Note that with "child" we mean not just the node that is pointed to,
4620 * but the whole list of siblings, of which the node is the first.
4621 */
4622 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004623 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004624 ++len;
4625 if ((child = np->wn_child) != NULL)
4626 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004627 /* Compress the child. This fills hashkey. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004628 compressed += node_compress(child, ht, tot);
4629
4630 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004631 hash = hash_hash(child->wn_u1.hashkey);
4632 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004633 tp = NULL;
4634 if (!HASHITEM_EMPTY(hi))
4635 {
4636 /* There are children with an identical hash value. Now check
4637 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004638 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004639 if (node_equal(child, tp))
4640 {
4641 /* Found one! Now use that child in place of the
4642 * current one. This means the current child is
4643 * dropped from the tree. */
4644 np->wn_child = tp;
4645 ++compressed;
4646 break;
4647 }
4648 if (tp == NULL)
4649 {
4650 /* No other child with this hash value equals the child of
4651 * the node, add it to the linked list after the first
4652 * item. */
4653 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004654 child->wn_u2.next = tp->wn_u2.next;
4655 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004656 }
4657 }
4658 else
4659 /* No other child has this hash value, add it to the
4660 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004661 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004662 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004663 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004664 *tot += len;
4665
4666 /*
4667 * Make a hash key for the node and its siblings, so that we can quickly
4668 * find a lookalike node. This must be done after compressing the sibling
4669 * list, otherwise the hash key would become invalid by the compression.
4670 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004671 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004672 nr = 0;
4673 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004674 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004675 if (np->wn_byte == NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004676 /* end node: use wn_flags, wn_region and wn_prefixID */
4677 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004678 else
4679 /* byte node: use the byte value and the child pointer */
4680 n = np->wn_byte + ((long_u)np->wn_child << 8);
4681 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004682 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004683
4684 /* Avoid NUL bytes, it terminates the hash key. */
4685 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004686 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004687 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004688 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004689 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004690 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004691 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004692 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
4693 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004694
4695 return compressed;
4696}
4697
4698/*
4699 * Return TRUE when two nodes have identical siblings and children.
4700 */
4701 static int
4702node_equal(n1, n2)
4703 wordnode_T *n1;
4704 wordnode_T *n2;
4705{
4706 wordnode_T *p1;
4707 wordnode_T *p2;
4708
4709 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
4710 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
4711 if (p1->wn_byte != p2->wn_byte
4712 || (p1->wn_byte == NUL
4713 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004714 || p1->wn_region != p2->wn_region
4715 || p1->wn_prefixID != p2->wn_prefixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004716 : (p1->wn_child != p2->wn_child)))
4717 break;
4718
4719 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004720}
4721
4722/*
4723 * Write a number to file "fd", MSB first, in "len" bytes.
4724 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004725 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004726put_bytes(fd, nr, len)
4727 FILE *fd;
4728 long_u nr;
4729 int len;
4730{
4731 int i;
4732
4733 for (i = len - 1; i >= 0; --i)
4734 putc((int)(nr >> (i * 8)), fd);
4735}
4736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004737static int
4738#ifdef __BORLANDC__
4739_RTLENTRYF
4740#endif
4741rep_compare __ARGS((const void *s1, const void *s2));
4742
4743/*
4744 * Function given to qsort() to sort the REP items on "from" string.
4745 */
4746 static int
4747#ifdef __BORLANDC__
4748_RTLENTRYF
4749#endif
4750rep_compare(s1, s2)
4751 const void *s1;
4752 const void *s2;
4753{
4754 fromto_T *p1 = (fromto_T *)s1;
4755 fromto_T *p2 = (fromto_T *)s2;
4756
4757 return STRCMP(p1->ft_from, p2->ft_from);
4758}
4759
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004760/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004761 * Write the Vim spell file "fname".
4762 */
4763 static void
Bram Moolenaar3982c542005-06-08 21:56:31 +00004764write_vim_spell(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004765 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004766 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004767{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004768 FILE *fd;
4769 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004770 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004771 wordnode_T *tree;
4772 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004773 int i;
4774 int l;
4775 garray_T *gap;
4776 fromto_T *ftp;
4777 char_u *p;
4778 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004779
Bram Moolenaarb765d632005-06-07 21:00:02 +00004780 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004781 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004782 {
4783 EMSG2(_(e_notopen), fname);
4784 return;
4785 }
4786
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004787 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004788 * <charflagslen> <charflags>
4789 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004790 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004791 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004792
4793 /* <fileID> */
4794 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
4795 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004796
4797 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004798 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004799 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004800 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
4801 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
4802 (size_t)1, fd);
4803 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004804 }
4805 else
4806 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004807 putc(0, fd);
4808 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004809 }
4810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004811 /*
4812 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004813 * <charflagslen> <charflags> <fcharlen> <fchars>
4814 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004815 * 'encoding'.
4816 * Also skip this for an .add.spl file, the main spell file must contain
4817 * the table (avoids that it conflicts). File is shorter too.
4818 */
4819 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004820 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004821 putc(0, fd);
4822 putc(0, fd);
4823 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004824 }
4825 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004826 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004827
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004828
4829 if (spin->si_midword == NULL)
4830 put_bytes(fd, 0L, 2); /* <midwordlen> */
4831 else
4832 {
4833 i = STRLEN(spin->si_midword);
4834 put_bytes(fd, (long_u)i, 2); /* <midwordlen> */
4835 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
4836 }
4837
4838
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004839 /* Write the prefix conditions. */
4840 write_spell_prefcond(fd, &spin->si_prefcond);
4841
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004842 /* <SUGGEST> : <repcount> <rep> ...
4843 * <salflags> <salcount> <sal> ...
4844 * <maplen> <mapstr> */
4845
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004846 /* Sort the REP items. */
4847 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
4848 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004849
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004850 /* round 1: REP items
4851 * round 2: SAL items (unless SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004852 for (round = 1; round <= 2; ++round)
4853 {
4854 if (round == 1)
4855 gap = &spin->si_rep;
4856 else
4857 {
4858 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004859
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004860 i = 0;
4861 if (spin->si_followup)
4862 i |= SAL_F0LLOWUP;
4863 if (spin->si_collapse)
4864 i |= SAL_COLLAPSE;
4865 if (spin->si_rem_accents)
4866 i |= SAL_REM_ACCENTS;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004867 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4868 i |= SAL_SOFO;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004869 putc(i, fd); /* <salflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004870 if (i & SAL_SOFO)
4871 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004872 }
4873
4874 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
4875 for (i = 0; i < gap->ga_len; ++i)
4876 {
4877 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
4878 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
4879 ftp = &((fromto_T *)gap->ga_data)[i];
4880 for (rr = 1; rr <= 2; ++rr)
4881 {
4882 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
4883 l = STRLEN(p);
4884 putc(l, fd);
4885 fwrite(p, l, (size_t)1, fd);
4886 }
4887 }
4888 }
4889
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004890 /* SOFOFROM and SOFOTO */
4891 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4892 {
4893 put_bytes(fd, 1L, 2); /* <salcount> */
4894
4895 l = STRLEN(spin->si_sofofr);
4896 put_bytes(fd, (long_u)l, 2); /* <salfromlen> */
4897 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <salfrom> */
4898
4899 l = STRLEN(spin->si_sofoto);
4900 put_bytes(fd, (long_u)l, 2); /* <saltolen> */
4901 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <salto> */
4902 }
4903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004904 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
4905 if (spin->si_map.ga_len > 0) /* <mapstr> */
4906 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
4907 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00004908
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004909 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004910 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004911 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004912 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004913 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004914 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004915 if (round == 1)
4916 tree = spin->si_foldroot;
4917 else if (round == 2)
4918 tree = spin->si_keeproot;
4919 else
4920 tree = spin->si_prefroot;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004921
Bram Moolenaar0c405862005-06-22 22:26:26 +00004922 /* Clear the index and wnode fields in the tree. */
4923 clear_node(tree);
4924
Bram Moolenaar51485f02005-06-04 21:55:20 +00004925 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00004926 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00004927 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004928 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004929
Bram Moolenaar51485f02005-06-04 21:55:20 +00004930 /* number of nodes in 4 bytes */
4931 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004932 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004933
Bram Moolenaar51485f02005-06-04 21:55:20 +00004934 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004935 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004936 }
4937
Bram Moolenaar51485f02005-06-04 21:55:20 +00004938 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004939}
4940
4941/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004942 * Clear the index and wnode fields of "node", it siblings and its
4943 * children. This is needed because they are a union with other items to save
4944 * space.
4945 */
4946 static void
4947clear_node(node)
4948 wordnode_T *node;
4949{
4950 wordnode_T *np;
4951
4952 if (node != NULL)
4953 for (np = node; np != NULL; np = np->wn_sibling)
4954 {
4955 np->wn_u1.index = 0;
4956 np->wn_u2.wnode = NULL;
4957
4958 if (np->wn_byte != NUL)
4959 clear_node(np->wn_child);
4960 }
4961}
4962
4963
4964/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004965 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004966 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00004967 * This first writes the list of possible bytes (siblings). Then for each
4968 * byte recursively write the children.
4969 *
4970 * NOTE: The code here must match the code in read_tree(), since assumptions
4971 * are made about the indexes (so that we don't have to write them in the
4972 * file).
4973 *
4974 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004975 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004976 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00004977put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004978 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004979 wordnode_T *node;
4980 int index;
4981 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004982 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004983{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004984 int newindex = index;
4985 int siblingcount = 0;
4986 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004987 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004988
Bram Moolenaar51485f02005-06-04 21:55:20 +00004989 /* If "node" is zero the tree is empty. */
4990 if (node == NULL)
4991 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004992
Bram Moolenaar51485f02005-06-04 21:55:20 +00004993 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004994 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004995
4996 /* Count the number of siblings. */
4997 for (np = node; np != NULL; np = np->wn_sibling)
4998 ++siblingcount;
4999
5000 /* Write the sibling count. */
5001 if (fd != NULL)
5002 putc(siblingcount, fd); /* <siblingcount> */
5003
5004 /* Write each sibling byte and optionally extra info. */
5005 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005006 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005007 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005008 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005009 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005010 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005011 /* For a NUL byte (end of word) write the flags etc. */
5012 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005013 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005014 /* In PREFIXTREE write the required prefixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005015 * associated condition nr (stored in wn_region). The
5016 * byte value is misused to store the "rare" and "not
5017 * combining" flags */
5018 if (np->wn_flags == (short_u)PFX_FLAGS_R)
5019 putc(BY_FLAGS, fd); /* <byte> */
5020 else if (np->wn_flags == (short_u)PFX_FLAGS_NC)
5021 putc(BY_PFX_NC, fd);
5022 else if (np->wn_flags == (short_u)PFX_FLAGS_RNC)
5023 putc(BY_PFX_RNC, fd);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005024 else
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005025 putc(BY_NOFLAGS, fd);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005026 putc(np->wn_prefixID, fd); /* <prefixID> */
5027 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005028 }
5029 else
5030 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005031 /* For word trees we write the flag/region items. */
5032 flags = np->wn_flags;
5033 if (regionmask != 0 && np->wn_region != regionmask)
5034 flags |= WF_REGION;
5035 if (np->wn_prefixID != 0)
5036 flags |= WF_PFX;
5037 if (flags == 0)
5038 {
5039 /* word without flags or region */
5040 putc(BY_NOFLAGS, fd); /* <byte> */
5041 }
5042 else
5043 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005044 if (np->wn_flags >= 0x100)
5045 {
5046 putc(BY_FLAGS2, fd); /* <byte> */
5047 putc(flags, fd); /* <flags> */
5048 putc((unsigned)flags >> 8, fd); /* <flags2> */
5049 }
5050 else
5051 {
5052 putc(BY_FLAGS, fd); /* <byte> */
5053 putc(flags, fd); /* <flags> */
5054 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005055 if (flags & WF_REGION)
5056 putc(np->wn_region, fd); /* <region> */
5057 if (flags & WF_PFX)
5058 putc(np->wn_prefixID, fd); /* <prefixID> */
5059 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005060 }
5061 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005062 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005063 else
5064 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005065 if (np->wn_child->wn_u1.index != 0
5066 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005067 {
5068 /* The child is written elsewhere, write the reference. */
5069 if (fd != NULL)
5070 {
5071 putc(BY_INDEX, fd); /* <byte> */
5072 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005073 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005074 }
5075 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005076 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005077 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005078 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005079
Bram Moolenaar51485f02005-06-04 21:55:20 +00005080 if (fd != NULL)
5081 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
5082 {
5083 EMSG(_(e_write));
5084 return 0;
5085 }
5086 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005087 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005088
5089 /* Space used in the array when reading: one for each sibling and one for
5090 * the count. */
5091 newindex += siblingcount + 1;
5092
5093 /* Recursively dump the children of each sibling. */
5094 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005095 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5096 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005097 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005098
5099 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005100}
5101
5102
5103/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00005104 * ":mkspell [-ascii] outfile infile ..."
5105 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005106 */
5107 void
5108ex_mkspell(eap)
5109 exarg_T *eap;
5110{
5111 int fcount;
5112 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005113 char_u *arg = eap->arg;
5114 int ascii = FALSE;
5115
5116 if (STRNCMP(arg, "-ascii", 6) == 0)
5117 {
5118 ascii = TRUE;
5119 arg = skipwhite(arg + 6);
5120 }
5121
5122 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
5123 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
5124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005125 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005126 FreeWild(fcount, fnames);
5127 }
5128}
5129
5130/*
5131 * Create a Vim spell file from one or more word lists.
5132 * "fnames[0]" is the output file name.
5133 * "fnames[fcount - 1]" is the last input file name.
5134 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5135 * and ".spl" is appended to make the output file name.
5136 */
5137 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005138mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005139 int fcount;
5140 char_u **fnames;
5141 int ascii; /* -ascii argument given */
5142 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005143 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005144{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005145 char_u fname[MAXPATHL];
5146 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00005147 char_u **innames;
5148 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005149 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005150 int i;
5151 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005152 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005153 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005154 spellinfo_T spin;
5155
5156 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005157 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005158 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005159 spin.si_followup = TRUE;
5160 spin.si_rem_accents = TRUE;
5161 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5162 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5163 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005164 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005165
Bram Moolenaarb765d632005-06-07 21:00:02 +00005166 /* default: fnames[0] is output file, following are input files */
5167 innames = &fnames[1];
5168 incount = fcount - 1;
5169
5170 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005171 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005172 len = STRLEN(fnames[0]);
5173 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5174 {
5175 /* For ":mkspell path/en.latin1.add" output file is
5176 * "path/en.latin1.add.spl". */
5177 innames = &fnames[0];
5178 incount = 1;
5179 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
5180 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005181 else if (fcount == 1)
5182 {
5183 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
5184 innames = &fnames[0];
5185 incount = 1;
5186 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5187 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5188 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005189 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5190 {
5191 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005192 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005193 }
5194 else
5195 /* Name should be language, make the file name from it. */
5196 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5197 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5198
5199 /* Check for .ascii.spl. */
5200 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
5201 spin.si_ascii = TRUE;
5202
5203 /* Check for .add.spl. */
5204 if (strstr((char *)gettail(wfname), ".add.") != NULL)
5205 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005206 }
5207
Bram Moolenaarb765d632005-06-07 21:00:02 +00005208 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005209 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005210 else if (vim_strchr(gettail(wfname), '_') != NULL)
5211 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005212 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005213 EMSG(_("E754: Only up to 8 regions supported"));
5214 else
5215 {
5216 /* Check for overwriting before doing things that may take a lot of
5217 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005218 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005219 {
5220 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005221 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005222 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005223 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005224 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005225 EMSG2(_(e_isadir2), wfname);
5226 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005227 }
5228
5229 /*
5230 * Init the aff and dic pointers.
5231 * Get the region names if there are more than 2 arguments.
5232 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005233 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005234 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005235 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005236
Bram Moolenaar3982c542005-06-08 21:56:31 +00005237 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005238 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005239 len = STRLEN(innames[i]);
5240 if (STRLEN(gettail(innames[i])) < 5
5241 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005242 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005243 EMSG2(_("E755: Invalid region in %s"), innames[i]);
5244 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005245 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005246 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5247 spin.si_region_name[i * 2 + 1] =
5248 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005249 }
5250 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005251 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005252
Bram Moolenaar51485f02005-06-04 21:55:20 +00005253 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
5254 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005255 spin.si_prefroot = wordtree_alloc(&spin.si_blocks);
5256 if (spin.si_foldroot == NULL
5257 || spin.si_keeproot == NULL
5258 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005259 {
5260 error = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005261 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005262 }
5263
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005264 /* When not producing a .add.spl file clear the character table when
5265 * we encounter one in the .aff file. This means we dump the current
5266 * one in the .spl file if the .aff file doesn't define one. That's
5267 * better than guessing the contents, the table will match a
5268 * previously loaded spell file. */
5269 if (!spin.si_add)
5270 spin.si_clear_chartab = TRUE;
5271
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005272 /*
5273 * Read all the .aff and .dic files.
5274 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005275 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005276 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005277 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005278 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005279 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005280 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005281
Bram Moolenaarb765d632005-06-07 21:00:02 +00005282 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005283 if (mch_stat((char *)fname, &st) >= 0)
5284 {
5285 /* Read the .aff file. Will init "spin->si_conv" based on the
5286 * "SET" line. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005287 afile[i] = spell_read_aff(fname, &spin);
5288 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005289 error = TRUE;
5290 else
5291 {
5292 /* Read the .dic file and store the words in the trees. */
5293 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00005294 innames[i]);
5295 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005296 error = TRUE;
5297 }
5298 }
5299 else
5300 {
5301 /* No .aff file, try reading the file as a word list. Store
5302 * the words in the trees. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005303 if (spell_read_wordfile(innames[i], &spin) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005304 error = TRUE;
5305 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005306
Bram Moolenaarb765d632005-06-07 21:00:02 +00005307#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005308 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005309 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005310#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005311 }
5312
Bram Moolenaar51485f02005-06-04 21:55:20 +00005313 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005314 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005315 /*
5316 * Remove the dummy NUL from the start of the tree root.
5317 */
5318 spin.si_foldroot = spin.si_foldroot->wn_sibling;
5319 spin.si_keeproot = spin.si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005320 spin.si_prefroot = spin.si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005321
5322 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005323 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005324 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005325 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005327 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005328 verbose_enter();
5329 MSG(_("Compressing word tree..."));
5330 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005331 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005332 verbose_leave();
5333 }
5334 wordtree_compress(spin.si_foldroot, &spin);
5335 wordtree_compress(spin.si_keeproot, &spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005336 wordtree_compress(spin.si_prefroot, &spin);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005337 }
5338
Bram Moolenaar51485f02005-06-04 21:55:20 +00005339 if (!error)
5340 {
5341 /*
5342 * Write the info in the spell file.
5343 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005344 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005346 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005347 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005348 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005349 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005350 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005351 verbose_leave();
5352 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00005353
Bram Moolenaar3982c542005-06-08 21:56:31 +00005354 write_vim_spell(wfname, &spin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005355
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005356 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005358 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005359 verbose_enter();
5360 MSG(_("Done!"));
5361 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00005362 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005363 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005364 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005365 verbose_leave();
5366 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005367
Bram Moolenaarb765d632005-06-07 21:00:02 +00005368 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005369 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005370 }
5371
5372 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005373 ga_clear(&spin.si_rep);
5374 ga_clear(&spin.si_sal);
5375 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005376 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005377 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005378 vim_free(spin.si_sofofr);
5379 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005380
5381 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005382 for (i = 0; i < incount; ++i)
5383 if (afile[i] != NULL)
5384 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005385
5386 /* Free all the bits and pieces at once. */
5387 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005388 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005389}
5390
Bram Moolenaarb765d632005-06-07 21:00:02 +00005391
5392/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005393 * ":[count]spellgood {word}"
5394 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00005395 */
5396 void
5397ex_spell(eap)
5398 exarg_T *eap;
5399{
Bram Moolenaar7887d882005-07-01 22:33:52 +00005400 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005401 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005402}
5403
5404/*
5405 * Add "word[len]" to 'spellfile' as a good or bad word.
5406 */
5407 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005408spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005409 char_u *word;
5410 int len;
5411 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005412 int index; /* "zG" and "zW": zero, otherwise index in
5413 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005414{
5415 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005416 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005417 int new_spf = FALSE;
5418 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005419 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005420 char_u fnamebuf[MAXPATHL];
5421 char_u line[MAXWLEN * 2];
5422 long fpos, fpos_next = 0;
5423 int i;
5424 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005425
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005426 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005427 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005428 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005429 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005430 int_wordlist = vim_tempname('s');
5431 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005432 return;
5433 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005434 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005435 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005436 else
5437 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005438 /* If 'spellfile' isn't set figure out a good default value. */
5439 if (*curbuf->b_p_spf == NUL)
5440 {
5441 init_spellfile();
5442 new_spf = TRUE;
5443 }
5444
5445 if (*curbuf->b_p_spf == NUL)
5446 {
5447 EMSG(_("E764: 'spellfile' is not set"));
5448 return;
5449 }
5450
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005451 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
5452 {
5453 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
5454 if (i == index)
5455 break;
5456 if (*spf == NUL)
5457 {
5458 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
5459 return;
5460 }
5461 }
5462
Bram Moolenaarb765d632005-06-07 21:00:02 +00005463 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005464 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005465 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
5466 buf = NULL;
5467 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00005468 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005469 EMSG(_(e_bufloaded));
5470 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005471 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005472
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005473 fname = fnamebuf;
5474 }
5475
5476 if (bad)
5477 {
5478 /* When the word also appears as good word we need to remove that one,
5479 * since its flags sort before the one with WF_BANNED. */
5480 fd = mch_fopen((char *)fname, "r");
5481 if (fd != NULL)
5482 {
5483 while (!vim_fgets(line, MAXWLEN * 2, fd))
5484 {
5485 fpos = fpos_next;
5486 fpos_next = ftell(fd);
5487 if (STRNCMP(word, line, len) == 0
5488 && (line[len] == '/' || line[len] < ' '))
5489 {
5490 /* Found duplicate word. Remove it by writing a '#' at
5491 * the start of the line. Mixing reading and writing
5492 * doesn't work for all systems, close the file first. */
5493 fclose(fd);
5494 fd = mch_fopen((char *)fname, "r+");
5495 if (fd == NULL)
5496 break;
5497 if (fseek(fd, fpos, SEEK_SET) == 0)
5498 fputc('#', fd);
5499 fseek(fd, fpos_next, SEEK_SET);
5500 }
5501 }
5502 fclose(fd);
5503 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005504 }
5505
5506 fd = mch_fopen((char *)fname, "a");
5507 if (fd == NULL && new_spf)
5508 {
5509 /* We just initialized the 'spellfile' option and can't open the file.
5510 * We may need to create the "spell" directory first. We already
5511 * checked the runtime directory is writable in init_spellfile(). */
5512 STRCPY(NameBuff, fname);
5513 *gettail_sep(NameBuff) = NUL;
5514 if (mch_stat((char *)NameBuff, &st) < 0)
5515 {
5516 /* The directory doesn't exist. Try creating it and opening the
5517 * file again. */
5518 vim_mkdir(NameBuff, 0755);
5519 fd = mch_fopen((char *)fname, "a");
5520 }
5521 }
5522
5523 if (fd == NULL)
5524 EMSG2(_(e_notopen), fname);
5525 else
5526 {
5527 if (bad)
5528 fprintf(fd, "%.*s/!\n", len, word);
5529 else
5530 fprintf(fd, "%.*s\n", len, word);
5531 fclose(fd);
5532
5533 /* Update the .add.spl file. */
5534 mkspell(1, &fname, FALSE, TRUE, TRUE);
5535
5536 /* If the .add file is edited somewhere, reload it. */
5537 if (buf != NULL)
5538 buf_reload(buf);
5539
5540 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005541 }
5542}
5543
5544/*
5545 * Initialize 'spellfile' for the current buffer.
5546 */
5547 static void
5548init_spellfile()
5549{
5550 char_u buf[MAXPATHL];
5551 int l;
5552 slang_T *sl;
5553 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005554 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005555
5556 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
5557 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005558 /* Find the end of the language name. Exclude the region. */
5559 for (lend = curbuf->b_p_spl; *lend != NUL
5560 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
5561 ;
5562
5563 /* Loop over all entries in 'runtimepath'. Use the first one where we
5564 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005565 rtp = p_rtp;
5566 while (*rtp != NUL)
5567 {
5568 /* Copy the path from 'runtimepath' to buf[]. */
5569 copy_option_part(&rtp, buf, MAXPATHL, ",");
5570 if (filewritable(buf) == 2)
5571 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00005572 /* Use the first language name from 'spelllang' and the
5573 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005574 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
5575 l = STRLEN(buf);
5576 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00005577 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005578 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00005579 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
5580 ? (char_u *)"ascii" : spell_enc());
5581 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
5582 break;
5583 }
5584 }
5585 }
5586}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005587
Bram Moolenaar51485f02005-06-04 21:55:20 +00005588
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005589/*
5590 * Init the chartab used for spelling for ASCII.
5591 * EBCDIC is not supported!
5592 */
5593 static void
5594clear_spell_chartab(sp)
5595 spelltab_T *sp;
5596{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005597 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005598
5599 /* Init everything to FALSE. */
5600 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
5601 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
5602 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005603 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005604 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005605 sp->st_upper[i] = i;
5606 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005607
5608 /* We include digits. A word shouldn't start with a digit, but handling
5609 * that is done separately. */
5610 for (i = '0'; i <= '9'; ++i)
5611 sp->st_isw[i] = TRUE;
5612 for (i = 'A'; i <= 'Z'; ++i)
5613 {
5614 sp->st_isw[i] = TRUE;
5615 sp->st_isu[i] = TRUE;
5616 sp->st_fold[i] = i + 0x20;
5617 }
5618 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005619 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005620 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005621 sp->st_upper[i] = i - 0x20;
5622 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005623}
5624
5625/*
5626 * Init the chartab used for spelling. Only depends on 'encoding'.
5627 * Called once while starting up and when 'encoding' changes.
5628 * The default is to use isalpha(), but the spell file should define the word
5629 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005630 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005631 */
5632 void
5633init_spell_chartab()
5634{
5635 int i;
5636
5637 did_set_spelltab = FALSE;
5638 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005639#ifdef FEAT_MBYTE
5640 if (enc_dbcs)
5641 {
5642 /* DBCS: assume double-wide characters are word characters. */
5643 for (i = 128; i <= 255; ++i)
5644 if (MB_BYTE2LEN(i) == 2)
5645 spelltab.st_isw[i] = TRUE;
5646 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005647 else if (enc_utf8)
5648 {
5649 for (i = 128; i < 256; ++i)
5650 {
5651 spelltab.st_isu[i] = utf_isupper(i);
5652 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
5653 spelltab.st_fold[i] = utf_fold(i);
5654 spelltab.st_upper[i] = utf_toupper(i);
5655 }
5656 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005657 else
5658#endif
5659 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005660 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005661 for (i = 128; i < 256; ++i)
5662 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005663 if (MB_ISUPPER(i))
5664 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005665 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005666 spelltab.st_isu[i] = TRUE;
5667 spelltab.st_fold[i] = MB_TOLOWER(i);
5668 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005669 else if (MB_ISLOWER(i))
5670 {
5671 spelltab.st_isw[i] = TRUE;
5672 spelltab.st_upper[i] = MB_TOUPPER(i);
5673 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005674 }
5675 }
5676}
5677
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005678static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
5679static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
5680
5681/*
5682 * Set the spell character tables from strings in the affix file.
5683 */
5684 static int
5685set_spell_chartab(fol, low, upp)
5686 char_u *fol;
5687 char_u *low;
5688 char_u *upp;
5689{
5690 /* We build the new tables here first, so that we can compare with the
5691 * previous one. */
5692 spelltab_T new_st;
5693 char_u *pf = fol, *pl = low, *pu = upp;
5694 int f, l, u;
5695
5696 clear_spell_chartab(&new_st);
5697
5698 while (*pf != NUL)
5699 {
5700 if (*pl == NUL || *pu == NUL)
5701 {
5702 EMSG(_(e_affform));
5703 return FAIL;
5704 }
5705#ifdef FEAT_MBYTE
5706 f = mb_ptr2char_adv(&pf);
5707 l = mb_ptr2char_adv(&pl);
5708 u = mb_ptr2char_adv(&pu);
5709#else
5710 f = *pf++;
5711 l = *pl++;
5712 u = *pu++;
5713#endif
5714 /* Every character that appears is a word character. */
5715 if (f < 256)
5716 new_st.st_isw[f] = TRUE;
5717 if (l < 256)
5718 new_st.st_isw[l] = TRUE;
5719 if (u < 256)
5720 new_st.st_isw[u] = TRUE;
5721
5722 /* if "LOW" and "FOL" are not the same the "LOW" char needs
5723 * case-folding */
5724 if (l < 256 && l != f)
5725 {
5726 if (f >= 256)
5727 {
5728 EMSG(_(e_affrange));
5729 return FAIL;
5730 }
5731 new_st.st_fold[l] = f;
5732 }
5733
5734 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005735 * case-folding, it's upper case and the "UPP" is the upper case of
5736 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005737 if (u < 256 && u != f)
5738 {
5739 if (f >= 256)
5740 {
5741 EMSG(_(e_affrange));
5742 return FAIL;
5743 }
5744 new_st.st_fold[u] = f;
5745 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005746 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005747 }
5748 }
5749
5750 if (*pl != NUL || *pu != NUL)
5751 {
5752 EMSG(_(e_affform));
5753 return FAIL;
5754 }
5755
5756 return set_spell_finish(&new_st);
5757}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005758
5759/*
5760 * Set the spell character tables from strings in the .spl file.
5761 */
5762 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005763set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005764 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005765 int cnt; /* length of "flags" */
5766 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005767{
5768 /* We build the new tables here first, so that we can compare with the
5769 * previous one. */
5770 spelltab_T new_st;
5771 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005772 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005773 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005774
5775 clear_spell_chartab(&new_st);
5776
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005777 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005778 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005779 if (i < cnt)
5780 {
5781 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
5782 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
5783 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005784
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005785 if (*p != NUL)
5786 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005787#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005788 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005789#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005790 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005791#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005792 new_st.st_fold[i + 128] = c;
5793 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
5794 new_st.st_upper[c] = i + 128;
5795 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005796 }
5797
5798 return set_spell_finish(&new_st);
5799}
5800
5801 static int
5802set_spell_finish(new_st)
5803 spelltab_T *new_st;
5804{
5805 int i;
5806
5807 if (did_set_spelltab)
5808 {
5809 /* check that it's the same table */
5810 for (i = 0; i < 256; ++i)
5811 {
5812 if (spelltab.st_isw[i] != new_st->st_isw[i]
5813 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005814 || spelltab.st_fold[i] != new_st->st_fold[i]
5815 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005816 {
5817 EMSG(_("E763: Word characters differ between spell files"));
5818 return FAIL;
5819 }
5820 }
5821 }
5822 else
5823 {
5824 /* copy the new spelltab into the one being used */
5825 spelltab = *new_st;
5826 did_set_spelltab = TRUE;
5827 }
5828
5829 return OK;
5830}
5831
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005832/*
Bram Moolenaarea408852005-06-25 22:49:46 +00005833 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005834 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00005835 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005836 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00005837 */
5838 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005839spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00005840 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005841 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00005842{
Bram Moolenaarea408852005-06-25 22:49:46 +00005843#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005844 char_u *s;
5845 int l;
5846 int c;
5847
5848 if (has_mbyte)
5849 {
5850 l = MB_BYTE2LEN(*p);
5851 s = p;
5852 if (l == 1)
5853 {
5854 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005855 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005856 {
5857 s = p + 1; /* skip a mid-word character */
5858 l = MB_BYTE2LEN(*s);
5859 }
5860 }
5861 else
5862 {
5863 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005864 if (c < 256 ? buf->b_spell_ismw[c]
5865 : (buf->b_spell_ismw_mb != NULL
5866 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005867 {
5868 s = p + l;
5869 l = MB_BYTE2LEN(*s);
5870 }
5871 }
5872
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005873 c = mb_ptr2char(s);
5874 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005875 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005876 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005877 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005878#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005879
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005880 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
5881}
5882
5883/*
5884 * Return TRUE if "p" points to a word character.
5885 * Unlike spell_iswordp() this doesn't check for "midword" characters.
5886 */
5887 static int
5888spell_iswordp_nmw(p)
5889 char_u *p;
5890{
5891#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005892 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005893
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005894 if (has_mbyte)
5895 {
5896 c = mb_ptr2char(p);
5897 if (c > 255)
5898 return mb_get_class(p) >= 2;
5899 return spelltab.st_isw[c];
5900 }
5901#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005902 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00005903}
5904
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005905#ifdef FEAT_MBYTE
5906/*
5907 * Return TRUE if "p" points to a word character.
5908 * Wide version of spell_iswordp().
5909 */
5910 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005911spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005912 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005913 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005914{
5915 int *s;
5916
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005917 if (*p < 256 ? buf->b_spell_ismw[*p]
5918 : (buf->b_spell_ismw_mb != NULL
5919 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005920 s = p + 1;
5921 else
5922 s = p;
5923
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005924 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005925 {
5926 if (enc_utf8)
5927 return utf_class(*s) >= 2;
5928 if (enc_dbcs)
5929 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
5930 return 0;
5931 }
5932 return spelltab.st_isw[*s];
5933}
5934#endif
5935
Bram Moolenaarea408852005-06-25 22:49:46 +00005936/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005937 * Write the table with prefix conditions to the .spl file.
5938 */
5939 static void
5940write_spell_prefcond(fd, gap)
5941 FILE *fd;
5942 garray_T *gap;
5943{
5944 int i;
5945 char_u *p;
5946 int len;
5947
5948 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
5949
5950 for (i = 0; i < gap->ga_len; ++i)
5951 {
5952 /* <prefcond> : <condlen> <condstr> */
5953 p = ((char_u **)gap->ga_data)[i];
5954 if (p == NULL)
5955 fputc(0, fd);
5956 else
5957 {
5958 len = STRLEN(p);
5959 fputc(len, fd);
5960 fwrite(p, (size_t)len, (size_t)1, fd);
5961 }
5962 }
5963}
5964
5965/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005966 * Write the current tables into the .spl file.
5967 * This makes sure the same characters are recognized as word characters when
5968 * generating an when using a spell file.
5969 */
5970 static void
5971write_spell_chartab(fd)
5972 FILE *fd;
5973{
5974 char_u charbuf[256 * 4];
5975 int len = 0;
5976 int flags;
5977 int i;
5978
5979 fputc(128, fd); /* <charflagslen> */
5980 for (i = 128; i < 256; ++i)
5981 {
5982 flags = 0;
5983 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005984 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005985 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005986 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005987 fputc(flags, fd); /* <charflags> */
5988
Bram Moolenaarb765d632005-06-07 21:00:02 +00005989#ifdef FEAT_MBYTE
5990 if (has_mbyte)
5991 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
5992 else
5993#endif
5994 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005995 }
5996
5997 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
5998 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
5999}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006000
6001/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006002 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
6003 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006004 * When using a multi-byte 'encoding' the length may change!
6005 * Returns FAIL when something wrong.
6006 */
6007 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006008spell_casefold(str, len, buf, buflen)
6009 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006010 int len;
6011 char_u *buf;
6012 int buflen;
6013{
6014 int i;
6015
6016 if (len >= buflen)
6017 {
6018 buf[0] = NUL;
6019 return FAIL; /* result will not fit */
6020 }
6021
6022#ifdef FEAT_MBYTE
6023 if (has_mbyte)
6024 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006025 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006026 char_u *p;
6027 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006028
6029 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006030 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006031 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006032 if (outi + MB_MAXBYTES > buflen)
6033 {
6034 buf[outi] = NUL;
6035 return FAIL;
6036 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006037 c = mb_ptr2char_adv(&p);
6038 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006039 }
6040 buf[outi] = NUL;
6041 }
6042 else
6043#endif
6044 {
6045 /* Be quick for non-multibyte encodings. */
6046 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006047 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006048 buf[i] = NUL;
6049 }
6050
6051 return OK;
6052}
6053
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006054#define SPS_BEST 1
6055#define SPS_FAST 2
6056#define SPS_DOUBLE 4
6057
6058static int sps_flags = SPS_BEST;
6059
6060/*
6061 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
6062 * Sets "sps_flags".
6063 */
6064 int
6065spell_check_sps()
6066{
6067 char_u *p;
6068 char_u buf[MAXPATHL];
6069 int f;
6070
6071 sps_flags = 0;
6072
6073 for (p = p_sps; *p != NUL; )
6074 {
6075 copy_option_part(&p, buf, MAXPATHL, ",");
6076
6077 f = 0;
6078 if (STRCMP(buf, "best") == 0)
6079 f = SPS_BEST;
6080 else if (STRCMP(buf, "fast") == 0)
6081 f = SPS_FAST;
6082 else if (STRCMP(buf, "double") == 0)
6083 f = SPS_DOUBLE;
6084 else if (STRNCMP(buf, "expr:", 5) != 0
6085 && STRNCMP(buf, "file:", 5) != 0)
6086 f = -1;
6087
6088 if (f == -1 || (sps_flags != 0 && f != 0))
6089 {
6090 sps_flags = SPS_BEST;
6091 return FAIL;
6092 }
6093 if (f != 0)
6094 sps_flags = f;
6095 }
6096
6097 if (sps_flags == 0)
6098 sps_flags = SPS_BEST;
6099
6100 return OK;
6101}
6102
6103/* Remember what "z?" replaced. */
6104static char_u *repl_from = NULL;
6105static char_u *repl_to = NULL;
6106
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006107/*
6108 * "z?": Find badly spelled word under or after the cursor.
6109 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006110 */
6111 void
6112spell_suggest()
6113{
6114 char_u *line;
6115 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006116 char_u wcopy[MAXWLEN + 2];
6117 char_u *p;
6118 int i;
6119 int c;
6120 suginfo_T sug;
6121 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006122 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006123 int need_cap;
6124 regmatch_T regmatch;
6125 int endcol;
6126 char_u *line_copy = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006127
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006128 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006129 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
6130 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006131 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006132 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
6133 return;
6134
6135 /* No bad word or it starts after the cursor: use the word under the
6136 * cursor. */
6137 curwin->w_cursor = prev_cursor;
6138 line = ml_get_curline();
6139 p = line + curwin->w_cursor.col;
6140 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006141 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006142 mb_ptr_back(line, p);
6143 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006144 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006145 mb_ptr_adv(p);
6146
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006147 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006148 {
6149 beep_flush();
6150 return;
6151 }
6152 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006153 }
6154
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006155 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006156 line = ml_get_curline();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006157
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006158 /* Figure out if the word should be capitalised. */
6159 need_cap = FALSE;
6160 if (curbuf->b_cap_prog != NULL)
6161 {
6162 endcol = 0;
Bram Moolenaar97409f12005-07-08 22:17:29 +00006163 if ((int)(skipwhite(line) - line) == (int)curwin->w_cursor.col)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006164 {
6165 /* At start of line, check if previous line is empty or sentence
6166 * ends there. */
6167 if (curwin->w_cursor.lnum == 1)
6168 need_cap = TRUE;
6169 else
6170 {
6171 line = ml_get(curwin->w_cursor.lnum - 1);
6172 if (*skipwhite(line) == NUL)
6173 need_cap = TRUE;
6174 else
6175 {
6176 /* Append a space in place of the line break. */
6177 line_copy = concat_str(line, (char_u *)" ");
6178 line = line_copy;
6179 endcol = STRLEN(line);
6180 }
6181 }
6182 }
6183 else
6184 endcol = curwin->w_cursor.col;
6185
6186 if (endcol > 0)
6187 {
6188 /* Check if sentence ends before the bad word. */
6189 regmatch.regprog = curbuf->b_cap_prog;
6190 regmatch.rm_ic = FALSE;
6191 p = line + endcol;
6192 for (;;)
6193 {
6194 mb_ptr_back(line, p);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006195 if (p == line || spell_iswordp_nmw(p))
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006196 break;
6197 if (vim_regexec(&regmatch, p, 0)
6198 && regmatch.endp[0] == line + endcol)
6199 {
6200 need_cap = TRUE;
6201 break;
6202 }
6203 }
6204 }
6205
6206 /* get the line again, we may have been using the previous one */
6207 line = ml_get_curline();
6208 vim_free(line_copy);
6209 }
6210
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006211 /* Get the list of suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006212 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2,
6213 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006214
6215 if (sug.su_ga.ga_len == 0)
6216 MSG(_("Sorry, no suggestions"));
6217 else
6218 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006219 vim_free(repl_from);
6220 repl_from = NULL;
6221 vim_free(repl_to);
6222 repl_to = NULL;
6223
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006224 /* List the suggestions. */
6225 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006226 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006227 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
6228 sug.su_badlen, sug.su_badptr);
6229 msg_puts(IObuff);
6230 msg_clr_eos();
6231 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00006232
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006233 msg_scroll = TRUE;
6234 for (i = 0; i < sug.su_ga.ga_len; ++i)
6235 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006236 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006237
6238 /* The suggested word may replace only part of the bad word, add
6239 * the not replaced part. */
6240 STRCPY(wcopy, stp->st_word);
6241 if (sug.su_badlen > stp->st_orglen)
6242 vim_strncpy(wcopy + STRLEN(wcopy),
6243 sug.su_badptr + stp->st_orglen,
6244 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006245 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy);
6246 msg_puts(IObuff);
6247
6248 /* The word may replace more than "su_badlen". */
6249 if (sug.su_badlen < stp->st_orglen)
6250 {
6251 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
6252 stp->st_orglen, sug.su_badptr);
6253 msg_puts(IObuff);
6254 }
6255
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006256 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006257 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006258 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006259 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006260 vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"),
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006261 stp->st_salscore ? "s " : "",
6262 stp->st_score, stp->st_altscore);
6263 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00006264 vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"),
6265 stp->st_score);
6266 msg_advance(30);
6267 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006268 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006269 msg_putchar('\n');
6270 }
6271
6272 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006273 i = prompt_for_number(&mouse_used);
6274 if (mouse_used)
6275 i -= lines_left;
6276
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006277 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006278 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006279 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006280 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006281 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
6282 repl_to = vim_strsave(stp->st_word);
6283
6284 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006285 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
6286 if (p != NULL)
6287 {
6288 c = sug.su_badptr - line;
6289 mch_memmove(p, line, c);
6290 STRCPY(p + c, stp->st_word);
6291 STRCAT(p, sug.su_badptr + stp->st_orglen);
6292 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6293 curwin->w_cursor.col = c;
6294 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006295
6296 /* For redo we use a change-word command. */
6297 ResetRedobuff();
6298 AppendToRedobuff((char_u *)"ciw");
6299 AppendToRedobuff(stp->st_word);
6300 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006301 }
6302 }
6303 else
6304 curwin->w_cursor = prev_cursor;
6305 }
6306
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006307 spell_find_cleanup(&sug);
6308}
6309
6310/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006311 * ":spellrepall"
6312 */
6313/*ARGSUSED*/
6314 void
6315ex_spellrepall(eap)
6316 exarg_T *eap;
6317{
6318 pos_T pos = curwin->w_cursor;
6319 char_u *frompat;
6320 int addlen;
6321 char_u *line;
6322 char_u *p;
6323 int didone = FALSE;
6324 int save_ws = p_ws;
6325
6326 if (repl_from == NULL || repl_to == NULL)
6327 {
6328 EMSG(_("E752: No previous spell replacement"));
6329 return;
6330 }
6331 addlen = STRLEN(repl_to) - STRLEN(repl_from);
6332
6333 frompat = alloc(STRLEN(repl_from) + 7);
6334 if (frompat == NULL)
6335 return;
6336 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
6337 p_ws = FALSE;
6338
6339 curwin->w_cursor.lnum = 0;
6340 while (!got_int)
6341 {
6342 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
6343 || u_save_cursor() == FAIL)
6344 break;
6345
6346 /* Only replace when the right word isn't there yet. This happens
6347 * when changing "etc" to "etc.". */
6348 line = ml_get_curline();
6349 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
6350 repl_to, STRLEN(repl_to)) != 0)
6351 {
6352 p = alloc(STRLEN(line) + addlen + 1);
6353 if (p == NULL)
6354 break;
6355 mch_memmove(p, line, curwin->w_cursor.col);
6356 STRCPY(p + curwin->w_cursor.col, repl_to);
6357 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
6358 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6359 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6360 didone = TRUE;
6361 }
6362 curwin->w_cursor.col += STRLEN(repl_to);
6363 }
6364
6365 p_ws = save_ws;
6366 curwin->w_cursor = pos;
6367 vim_free(frompat);
6368
6369 if (!didone)
6370 EMSG2(_("E753: Not found: %s"), repl_from);
6371}
6372
6373/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006374 * Find spell suggestions for "word". Return them in the growarray "*gap" as
6375 * a list of allocated strings.
6376 */
6377 void
6378spell_suggest_list(gap, word, maxcount)
6379 garray_T *gap;
6380 char_u *word;
6381 int maxcount; /* maximum nr of suggestions */
6382{
6383 suginfo_T sug;
6384 int i;
6385 suggest_T *stp;
6386 char_u *wcopy;
6387
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006388 spell_find_suggest(word, &sug, maxcount, FALSE, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006389
6390 /* Make room in "gap". */
6391 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
6392 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
6393 return;
6394
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006395 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006396 {
6397 stp = &SUG(sug.su_ga, i);
6398
6399 /* The suggested word may replace only part of "word", add the not
6400 * replaced part. */
6401 wcopy = alloc(STRLEN(stp->st_word)
6402 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
6403 if (wcopy == NULL)
6404 break;
6405 STRCPY(wcopy, stp->st_word);
6406 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
6407 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
6408 }
6409
6410 spell_find_cleanup(&sug);
6411}
6412
6413/*
6414 * Find spell suggestions for the word at the start of "badptr".
6415 * Return the suggestions in "su->su_ga".
6416 * The maximum number of suggestions is "maxcount".
6417 * Note: does use info for the current window.
6418 * This is based on the mechanisms of Aspell, but completely reimplemented.
6419 */
6420 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006421spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006422 char_u *badptr;
6423 suginfo_T *su;
6424 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00006425 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006426 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006427{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006428 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006429 char_u buf[MAXPATHL];
6430 char_u *p;
6431 int do_combine = FALSE;
6432 char_u *sps_copy;
6433#ifdef FEAT_EVAL
6434 static int expr_busy = FALSE;
6435#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006436 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006437
6438 /*
6439 * Set the info in "*su".
6440 */
6441 vim_memset(su, 0, sizeof(suginfo_T));
6442 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
6443 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006444 if (*badptr == NUL)
6445 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006446 hash_init(&su->su_banned);
6447
6448 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006449 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006450 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006451 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006452
6453 if (su->su_badlen >= MAXWLEN)
6454 su->su_badlen = MAXWLEN - 1; /* just in case */
6455 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
6456 (void)spell_casefold(su->su_badptr, su->su_badlen,
6457 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006458 /* get caps flags for bad word */
6459 su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006460 if (need_cap)
6461 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006462
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006463 /* If the word is not capitalised and spell_check() doesn't consider the
6464 * word to be bad then it might need to be capitalised. Add a suggestion
6465 * for that. */
6466#ifdef FEAT_MBYTE
6467 c = mb_ptr2char(su->su_badptr);
6468#else
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006469 c = *su->su_badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006470#endif
6471 if (!SPELL_ISUPPER(c) && attr == 0)
6472 {
6473 make_case_word(su->su_badword, buf, WF_ONECAP);
6474 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
6475 0, TRUE);
6476 }
6477
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006478 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00006479 if (banbadword)
6480 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006481
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006482 /* Make a copy of 'spellsuggest', because the expression may change it. */
6483 sps_copy = vim_strsave(p_sps);
6484 if (sps_copy == NULL)
6485 return;
6486
6487 /* Loop over the items in 'spellsuggest'. */
6488 for (p = sps_copy; *p != NUL; )
6489 {
6490 copy_option_part(&p, buf, MAXPATHL, ",");
6491
6492 if (STRNCMP(buf, "expr:", 5) == 0)
6493 {
6494#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006495 /* Evaluate an expression. Skip this when called recursively,
6496 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006497 if (!expr_busy)
6498 {
6499 expr_busy = TRUE;
6500 spell_suggest_expr(su, buf + 5);
6501 expr_busy = FALSE;
6502 }
6503#endif
6504 }
6505 else if (STRNCMP(buf, "file:", 5) == 0)
6506 /* Use list of suggestions in a file. */
6507 spell_suggest_file(su, buf + 5);
6508 else
6509 {
6510 /* Use internal method. */
6511 spell_suggest_intern(su);
6512 if (sps_flags & SPS_DOUBLE)
6513 do_combine = TRUE;
6514 }
6515 }
6516
6517 vim_free(sps_copy);
6518
6519 if (do_combine)
6520 /* Combine the two list of suggestions. This must be done last,
6521 * because sorting changes the order again. */
6522 score_combine(su);
6523}
6524
6525#ifdef FEAT_EVAL
6526/*
6527 * Find suggestions by evaluating expression "expr".
6528 */
6529 static void
6530spell_suggest_expr(su, expr)
6531 suginfo_T *su;
6532 char_u *expr;
6533{
6534 list_T *list;
6535 listitem_T *li;
6536 int score;
6537 char_u *p;
6538
6539 /* The work is split up in a few parts to avoid having to export
6540 * suginfo_T.
6541 * First evaluate the expression and get the resulting list. */
6542 list = eval_spell_expr(su->su_badword, expr);
6543 if (list != NULL)
6544 {
6545 /* Loop over the items in the list. */
6546 for (li = list->lv_first; li != NULL; li = li->li_next)
6547 if (li->li_tv.v_type == VAR_LIST)
6548 {
6549 /* Get the word and the score from the items. */
6550 score = get_spellword(li->li_tv.vval.v_list, &p);
6551 if (score >= 0)
6552 add_suggestion(su, &su->su_ga, p,
6553 su->su_badlen, score, 0, TRUE);
6554 }
6555 list_unref(list);
6556 }
6557
6558 /* Sort the suggestions and truncate at "maxcount". */
6559 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6560}
6561#endif
6562
6563/*
6564 * Find suggestions a file "fname".
6565 */
6566 static void
6567spell_suggest_file(su, fname)
6568 suginfo_T *su;
6569 char_u *fname;
6570{
6571 FILE *fd;
6572 char_u line[MAXWLEN * 2];
6573 char_u *p;
6574 int len;
6575 char_u cword[MAXWLEN];
6576
6577 /* Open the file. */
6578 fd = mch_fopen((char *)fname, "r");
6579 if (fd == NULL)
6580 {
6581 EMSG2(_(e_notopen), fname);
6582 return;
6583 }
6584
6585 /* Read it line by line. */
6586 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
6587 {
6588 line_breakcheck();
6589
6590 p = vim_strchr(line, '/');
6591 if (p == NULL)
6592 continue; /* No Tab found, just skip the line. */
6593 *p++ = NUL;
6594 if (STRICMP(su->su_badword, line) == 0)
6595 {
6596 /* Match! Isolate the good word, until CR or NL. */
6597 for (len = 0; p[len] >= ' '; ++len)
6598 ;
6599 p[len] = NUL;
6600
6601 /* If the suggestion doesn't have specific case duplicate the case
6602 * of the bad word. */
6603 if (captype(p, NULL) == 0)
6604 {
6605 make_case_word(p, cword, su->su_badflags);
6606 p = cword;
6607 }
6608
6609 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6610 SCORE_FILE, 0, TRUE);
6611 }
6612 }
6613
6614 fclose(fd);
6615
6616 /* Sort the suggestions and truncate at "maxcount". */
6617 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6618}
6619
6620/*
6621 * Find suggestions for the internal method indicated by "sps_flags".
6622 */
6623 static void
6624spell_suggest_intern(su)
6625 suginfo_T *su;
6626{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006627 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006628 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006629 *
6630 * Set a maximum score to limit the combination of operations that is
6631 * tried.
6632 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006633 suggest_try_special(su);
6634
6635 /*
6636 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
6637 * from the .aff file and inserting a space (split the word).
6638 */
6639 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006640
6641 /* For the resulting top-scorers compute the sound-a-like score. */
6642 if (sps_flags & SPS_DOUBLE)
6643 score_comp_sal(su);
6644
6645 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006646 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006647 *
6648 * Only do this when we don't have a lot of suggestions yet, because it's
6649 * very slow and often doesn't find new suggestions.
6650 */
6651 if ((sps_flags & SPS_DOUBLE)
6652 || (!(sps_flags & SPS_FAST)
6653 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
6654 {
6655 /* Allow a higher score now. */
6656 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006657 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006658 }
6659
6660 /* When CTRL-C was hit while searching do show the results. */
6661 ui_breakcheck();
6662 if (got_int)
6663 {
6664 (void)vgetc();
6665 got_int = FALSE;
6666 }
6667
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006668 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006669 {
6670 if (sps_flags & SPS_BEST)
6671 /* Adjust the word score for how it sounds like. */
6672 rescore_suggestions(su);
6673
6674 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006675 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006676 }
6677}
6678
6679/*
6680 * Free the info put in "*su" by spell_find_suggest().
6681 */
6682 static void
6683spell_find_cleanup(su)
6684 suginfo_T *su;
6685{
6686 int i;
6687
6688 /* Free the suggestions. */
6689 for (i = 0; i < su->su_ga.ga_len; ++i)
6690 vim_free(SUG(su->su_ga, i).st_word);
6691 ga_clear(&su->su_ga);
6692 for (i = 0; i < su->su_sga.ga_len; ++i)
6693 vim_free(SUG(su->su_sga, i).st_word);
6694 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006695
6696 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006697 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006698}
6699
6700/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006701 * Make a copy of "word", with the first letter upper or lower cased, to
6702 * "wcopy[MAXWLEN]". "word" must not be empty.
6703 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006704 */
6705 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006706onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006707 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006708 char_u *wcopy;
6709 int upper; /* TRUE: first letter made upper case */
6710{
6711 char_u *p;
6712 int c;
6713 int l;
6714
6715 p = word;
6716#ifdef FEAT_MBYTE
6717 if (has_mbyte)
6718 c = mb_ptr2char_adv(&p);
6719 else
6720#endif
6721 c = *p++;
6722 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006723 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006724 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006725 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006726#ifdef FEAT_MBYTE
6727 if (has_mbyte)
6728 l = mb_char2bytes(c, wcopy);
6729 else
6730#endif
6731 {
6732 l = 1;
6733 wcopy[0] = c;
6734 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006735 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006736}
6737
6738/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006739 * Make a copy of "word" with all the letters upper cased into
6740 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006741 */
6742 static void
6743allcap_copy(word, wcopy)
6744 char_u *word;
6745 char_u *wcopy;
6746{
6747 char_u *s;
6748 char_u *d;
6749 int c;
6750
6751 d = wcopy;
6752 for (s = word; *s != NUL; )
6753 {
6754#ifdef FEAT_MBYTE
6755 if (has_mbyte)
6756 c = mb_ptr2char_adv(&s);
6757 else
6758#endif
6759 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006760 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006761
6762#ifdef FEAT_MBYTE
6763 if (has_mbyte)
6764 {
6765 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
6766 break;
6767 d += mb_char2bytes(c, d);
6768 }
6769 else
6770#endif
6771 {
6772 if (d - wcopy >= MAXWLEN - 1)
6773 break;
6774 *d++ = c;
6775 }
6776 }
6777 *d = NUL;
6778}
6779
6780/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006781 * Try finding suggestions by recognizing specific situations.
6782 */
6783 static void
6784suggest_try_special(su)
6785 suginfo_T *su;
6786{
6787 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006788 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006789 int c;
6790 char_u word[MAXWLEN];
6791
6792 /*
6793 * Recognize a word that is repeated: "the the".
6794 */
6795 p = skiptowhite(su->su_fbadword);
6796 len = p - su->su_fbadword;
6797 p = skipwhite(p);
6798 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
6799 {
6800 /* Include badflags: if the badword is onecap or allcap
6801 * use that for the goodword too: "The the" -> "The". */
6802 c = su->su_fbadword[len];
6803 su->su_fbadword[len] = NUL;
6804 make_case_word(su->su_fbadword, word, su->su_badflags);
6805 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006806 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006807 }
6808}
6809
6810/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006811 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00006812 *
6813 * This uses a state machine. At each node in the tree we try various
6814 * operations. When trying if an operation work "depth" is increased and the
6815 * stack[] is used to store info. This allows combinations, thus insert one
6816 * character, replace one and delete another. The number of changes is
6817 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006818 */
6819 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00006820suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006821 suginfo_T *su;
6822{
6823 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
6824 char_u tword[MAXWLEN]; /* good word collected so far */
6825 trystate_T stack[MAXWLEN];
6826 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
6827 * to for word split) */
6828 char_u prewordlen = 0; /* length of word in "preword" */
6829 int splitoff = 0; /* index in tword after last split */
6830 trystate_T *sp;
6831 int newscore;
6832 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006833 char_u *byts, *fbyts, *pbyts;
6834 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006835 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006836 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006837 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006838 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006839 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006840 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006841 int len;
6842 char_u *p;
6843 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00006844 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006845 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006846
6847 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00006848 * to find matches (esp. REP items). Append some more text, changing
6849 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006850 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006851 n = STRLEN(fword);
6852 p = su->su_badptr + su->su_badlen;
6853 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006854
6855 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6856 lp->lp_slang != NULL; ++lp)
6857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006858 /*
6859 * Go through the whole case-fold tree, try changes at each node.
6860 * "tword[]" contains the word collected from nodes in the tree.
6861 * "fword[]" the word we are trying to match with (initially the bad
6862 * word).
6863 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006864 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006865 sp = &stack[0];
6866 sp->ts_state = STATE_START;
6867 sp->ts_score = 0;
6868 sp->ts_curi = 1;
6869 sp->ts_fidx = 0;
6870 sp->ts_fidxtry = 0;
6871 sp->ts_twordlen = 0;
6872 sp->ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006873#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006874 sp->ts_tcharlen = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006875#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006876
Bram Moolenaarea424162005-06-16 21:51:00 +00006877 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006878 * When there are postponed prefixes we need to use these first. At
6879 * the end of the prefix we continue in the case-fold tree.
6880 */
6881 fbyts = lp->lp_slang->sl_fbyts;
6882 fidxs = lp->lp_slang->sl_fidxs;
6883 pbyts = lp->lp_slang->sl_pbyts;
6884 pidxs = lp->lp_slang->sl_pidxs;
6885 if (pbyts != NULL)
6886 {
6887 byts = pbyts;
6888 idxs = pidxs;
6889 sp->ts_prefixdepth = PREFIXTREE;
6890 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
6891 }
6892 else
6893 {
6894 byts = fbyts;
6895 idxs = fidxs;
6896 sp->ts_prefixdepth = NOPREFIX;
6897 }
6898
6899 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00006900 * Loop to find all suggestions. At each round we either:
6901 * - For the current state try one operation, advance "ts_curi",
6902 * increase "depth".
6903 * - When a state is done go to the next, set "ts_state".
6904 * - When all states are tried decrease "depth".
6905 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006906 while (depth >= 0 && !got_int)
6907 {
6908 sp = &stack[depth];
6909 switch (sp->ts_state)
6910 {
6911 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006912 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006913 /*
6914 * Start of node: Deal with NUL bytes, which means
6915 * tword[] may end here.
6916 */
6917 arridx = sp->ts_arridx; /* current node in the tree */
6918 len = byts[arridx]; /* bytes in this node */
6919 arridx += sp->ts_curi; /* index of current byte */
6920
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006921 if (sp->ts_prefixdepth == PREFIXTREE)
6922 {
6923 /* Skip over the NUL bytes, we use them later. */
6924 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
6925 ;
6926 sp->ts_curi += n;
6927
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006928 /* Always past NUL bytes now. */
6929 n = (int)sp->ts_state;
6930 sp->ts_state = STATE_ENDNUL;
6931
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006932 /* At end of a prefix or at start of prefixtree: check for
6933 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006934 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006935 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006936 ++depth;
6937 stack[depth] = stack[depth - 1];
6938 sp = &stack[depth];
6939 sp->ts_prefixdepth = depth - 1;
6940 byts = fbyts;
6941 idxs = fidxs;
6942 sp->ts_state = STATE_START;
6943 sp->ts_curi = 1; /* start just after length byte */
6944 sp->ts_arridx = 0;
6945
6946 /* Move the prefix to preword[] so that
6947 * find_keepcap_word() works. */
6948 prewordlen = splitoff = sp->ts_twordlen;
6949 mch_memmove(preword, tword, splitoff);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006950 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006951 break;
6952 }
6953
Bram Moolenaar0c405862005-06-22 22:26:26 +00006954 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006955 {
6956 /* Past bytes in node and/or past NUL bytes. */
6957 sp->ts_state = STATE_ENDNUL;
6958 break;
6959 }
6960
6961 /*
6962 * End of word in tree.
6963 */
6964 ++sp->ts_curi; /* eat one NUL byte */
6965
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006966 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006967
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006968 if (sp->ts_prefixdepth < MAXWLEN)
6969 {
6970 /* There was a prefix before the word. Check that the
6971 * prefix can be used with this word. */
6972 /* Count the length of the NULs in the prefix. If there
6973 * are none this must be the first try without a prefix.
6974 */
6975 n = stack[sp->ts_prefixdepth].ts_arridx;
6976 len = pbyts[n++];
6977 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
6978 ;
6979 if (c > 0)
6980 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006981 /* The prefix ID is stored three bytes above the
6982 * flags. */
6983 c = valid_word_prefix(c, n, flags,
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006984 tword + splitoff, lp->lp_slang);
6985 if (c == 0)
6986 break;
6987
6988 /* Use the WF_RARE flag for a rare prefix. */
6989 if (c & WF_RAREPFX)
6990 flags |= WF_RARE;
6991 }
6992 }
6993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006994 /*
6995 * Form the word with proper case in preword.
6996 * If there is a word from a previous split, append.
6997 */
6998 tword[sp->ts_twordlen] = NUL;
6999 if (flags & WF_KEEPCAP)
7000 /* Must find the word in the keep-case tree. */
7001 find_keepcap_word(lp->lp_slang, tword + splitoff,
7002 preword + prewordlen);
7003 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00007004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007005 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00007006 * use that for the goodword too. But if the badword is
7007 * allcap and it's only one char long use onecap. */
7008 c = su->su_badflags;
7009 if ((c & WF_ALLCAP)
7010#ifdef FEAT_MBYTE
7011 && su->su_badlen == mb_ptr2len_check(su->su_badptr)
7012#else
7013 && su->su_badlen == 1
7014#endif
7015 )
7016 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007017 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007018 preword + prewordlen, flags | c);
7019 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007020
7021 /* Don't use a banned word. It may appear again as a good
7022 * word, thus remember it. */
7023 if (flags & WF_BANNED)
7024 {
7025 add_banned(su, preword + prewordlen);
7026 break;
7027 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007028 if (was_banned(su, preword + prewordlen)
7029 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007030 break;
7031
7032 newscore = 0;
7033 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007034 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007035 newscore += SCORE_REGION;
7036 if (flags & WF_RARE)
7037 newscore += SCORE_RARE;
7038
Bram Moolenaar0c405862005-06-22 22:26:26 +00007039 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007040 captype(preword + prewordlen, NULL)))
7041 newscore += SCORE_ICASE;
7042
Bram Moolenaar0c405862005-06-22 22:26:26 +00007043 if ((fword[sp->ts_fidx] == NUL
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007044 || !spell_iswordp(fword + sp->ts_fidx, curbuf))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007045 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007046 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007047 /* The badword also ends: add suggestions. Give a penalty
7048 * when changing non-word char to word char, e.g., "thes,"
7049 * -> "these". */
7050 p = fword + sp->ts_fidx;
7051#ifdef FEAT_MBYTE
7052 if (has_mbyte)
7053 mb_ptr_back(fword, p);
7054 else
7055#endif
7056 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007057 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007058 {
7059 p = preword + STRLEN(preword);
7060#ifdef FEAT_MBYTE
7061 if (has_mbyte)
7062 mb_ptr_back(preword, p);
7063 else
7064#endif
7065 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007066 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007067 newscore += SCORE_NONWORD;
7068 }
7069
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007070 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007071 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007072 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007073 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007074 else if (sp->ts_fidx >= sp->ts_fidxtry
7075#ifdef FEAT_MBYTE
7076 /* Don't split halfway a character. */
7077 && (!has_mbyte || sp->ts_tcharlen == 0)
7078#endif
7079 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007080 {
7081 /* The word in the tree ends but the badword
7082 * continues: try inserting a space and check that a valid
7083 * words starts at fword[sp->ts_fidx]. */
7084 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
7085 {
7086 /* Save things to be restored at STATE_SPLITUNDO. */
7087 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007088 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007089 sp->ts_save_splitoff = splitoff;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007090 sp->ts_state = STATE_SPLITUNDO;
7091
7092 ++depth;
7093 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007094
7095 /* Append a space to preword. */
7096 STRCAT(preword, " ");
7097 prewordlen = STRLEN(preword);
7098 splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007099
7100 /* If the badword has a non-word character at this
7101 * position skip it. That means replacing the
7102 * non-word character with a space. */
7103 if (!spell_iswordp_nmw(fword + sp->ts_fidx))
7104 {
7105 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
7106#ifdef FEAT_MBYTE
7107 if (has_mbyte)
7108 sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
7109 else
7110#endif
7111 ++sp->ts_fidx;
7112 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007113#ifdef FEAT_MBYTE
7114 if (has_mbyte)
7115 {
7116 int i = 0;
7117
7118 /* Case-folding may change the number of bytes:
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007119 * Count nr of chars in fword[ts_fidx] and
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007120 * advance that many chars in su->su_badptr. */
7121 for (p = fword; p < fword + sp->ts_fidx;
7122 mb_ptr_adv(p))
7123 ++i;
7124 for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
7125 --i;
7126 }
7127 else
7128#endif
7129 p = su->su_badptr + sp->ts_fidx;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007130 su->su_badflags = captype(p, su->su_badptr
7131 + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007132
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007133 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007134 sp->ts_arridx = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007135 }
7136 }
7137 break;
7138
7139 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007140 /* Undo the changes done for word split. */
7141 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007142 splitoff = sp->ts_save_splitoff;
7143 prewordlen = sp->ts_save_prewordlen;
7144
7145 /* Continue looking for NUL bytes. */
7146 sp->ts_state = STATE_START;
7147 break;
7148
7149 case STATE_ENDNUL:
7150 /* Past the NUL bytes in the node. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007151 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007152 {
7153 /* The badword ends, can't use the bytes in this node. */
7154 sp->ts_state = STATE_DEL;
7155 break;
7156 }
7157 sp->ts_state = STATE_PLAIN;
7158 /*FALLTHROUGH*/
7159
7160 case STATE_PLAIN:
7161 /*
7162 * Go over all possible bytes at this node, add each to
7163 * tword[] and use child node. "ts_curi" is the index.
7164 */
7165 arridx = sp->ts_arridx;
7166 if (sp->ts_curi > byts[arridx])
7167 {
7168 /* Done all bytes at this node, do next state. When still
7169 * at already changed bytes skip the other tricks. */
7170 if (sp->ts_fidx >= sp->ts_fidxtry)
7171 sp->ts_state = STATE_DEL;
7172 else
7173 sp->ts_state = STATE_FINAL;
7174 }
7175 else
7176 {
7177 arridx += sp->ts_curi++;
7178 c = byts[arridx];
7179
7180 /* Normal byte, go one level deeper. If it's not equal to
7181 * the byte in the bad word adjust the score. But don't
7182 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00007183 if (c == fword[sp->ts_fidx]
7184#ifdef FEAT_MBYTE
7185 || (sp->ts_tcharlen > 0
7186 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007187#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00007188 )
7189 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007190 else
7191 newscore = SCORE_SUBST;
7192 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
7193 && try_deeper(su, stack, depth, newscore))
7194 {
7195 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007196 sp = &stack[depth];
7197 ++sp->ts_fidx;
7198 tword[sp->ts_twordlen++] = c;
7199 sp->ts_arridx = idxs[arridx];
7200#ifdef FEAT_MBYTE
7201 if (newscore == SCORE_SUBST)
7202 sp->ts_isdiff = DIFF_YES;
7203 if (has_mbyte)
7204 {
7205 /* Multi-byte characters are a bit complicated to
7206 * handle: They differ when any of the bytes
7207 * differ and then their length may also differ. */
7208 if (sp->ts_tcharlen == 0)
7209 {
7210 /* First byte. */
7211 sp->ts_tcharidx = 0;
7212 sp->ts_tcharlen = MB_BYTE2LEN(c);
7213 sp->ts_fcharstart = sp->ts_fidx - 1;
7214 sp->ts_isdiff = (newscore != 0)
7215 ? DIFF_YES : DIFF_NONE;
7216 }
7217 else if (sp->ts_isdiff == DIFF_INSERT)
7218 /* When inserting trail bytes don't advance in
7219 * the bad word. */
7220 --sp->ts_fidx;
7221 if (++sp->ts_tcharidx == sp->ts_tcharlen)
7222 {
7223 /* Last byte of character. */
7224 if (sp->ts_isdiff == DIFF_YES)
7225 {
7226 /* Correct ts_fidx for the byte length of
7227 * the character (we didn't check that
7228 * before). */
7229 sp->ts_fidx = sp->ts_fcharstart
7230 + MB_BYTE2LEN(
7231 fword[sp->ts_fcharstart]);
7232
7233 /* For a similar character adjust score
7234 * from SCORE_SUBST to SCORE_SIMILAR. */
7235 if (lp->lp_slang->sl_has_map
7236 && similar_chars(lp->lp_slang,
7237 mb_ptr2char(tword
7238 + sp->ts_twordlen
7239 - sp->ts_tcharlen),
7240 mb_ptr2char(fword
7241 + sp->ts_fcharstart)))
7242 sp->ts_score -=
7243 SCORE_SUBST - SCORE_SIMILAR;
7244 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007245 else if (sp->ts_isdiff == DIFF_INSERT
7246 && sp->ts_twordlen > sp->ts_tcharlen)
7247 {
7248 /* If the previous character was the same,
7249 * thus doubling a character, give a bonus
7250 * to the score. */
7251 p = tword + sp->ts_twordlen
7252 - sp->ts_tcharlen;
7253 c = mb_ptr2char(p);
7254 mb_ptr_back(tword, p);
7255 if (c == mb_ptr2char(p))
7256 sp->ts_score -= SCORE_INS
7257 - SCORE_INSDUP;
7258 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007259
7260 /* Starting a new char, reset the length. */
7261 sp->ts_tcharlen = 0;
7262 }
7263 }
7264 else
7265#endif
7266 {
7267 /* If we found a similar char adjust the score.
7268 * We do this after calling try_deeper() because
7269 * it's slow. */
7270 if (newscore != 0
7271 && lp->lp_slang->sl_has_map
7272 && similar_chars(lp->lp_slang,
7273 c, fword[sp->ts_fidx - 1]))
7274 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
7275 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007276 }
7277 }
7278 break;
7279
7280 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00007281#ifdef FEAT_MBYTE
7282 /* When past the first byte of a multi-byte char don't try
7283 * delete/insert/swap a character. */
7284 if (has_mbyte && sp->ts_tcharlen > 0)
7285 {
7286 sp->ts_state = STATE_FINAL;
7287 break;
7288 }
7289#endif
7290 /*
7291 * Try skipping one character in the bad word (delete it).
7292 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007293 sp->ts_state = STATE_INS;
7294 sp->ts_curi = 1;
7295 if (fword[sp->ts_fidx] != NUL
7296 && try_deeper(su, stack, depth, SCORE_DEL))
7297 {
7298 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00007299
7300 /* Advance over the character in fword[]. Give a bonus to
7301 * the score if the same character is following "nn" ->
7302 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00007303#ifdef FEAT_MBYTE
7304 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00007305 {
7306 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00007307 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaarea408852005-06-25 22:49:46 +00007308 if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
7309 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7310 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007311 else
7312#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007313 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007314 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00007315 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
7316 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7317 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007318 break;
7319 }
7320 /*FALLTHROUGH*/
7321
7322 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00007323 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007324 * node. */
7325 n = sp->ts_arridx;
7326 if (sp->ts_curi > byts[n])
7327 {
7328 /* Done all bytes at this node, do next state. */
7329 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007330 }
7331 else
7332 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007333 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007334 n += sp->ts_curi++;
7335 c = byts[n];
7336 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
7337 {
7338 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007339 sp = &stack[depth];
7340 tword[sp->ts_twordlen++] = c;
7341 sp->ts_arridx = idxs[n];
7342#ifdef FEAT_MBYTE
7343 if (has_mbyte)
7344 {
7345 fl = MB_BYTE2LEN(c);
7346 if (fl > 1)
7347 {
7348 /* There are following bytes for the same
7349 * character. We must find all bytes before
7350 * trying delete/insert/swap/etc. */
7351 sp->ts_tcharlen = fl;
7352 sp->ts_tcharidx = 1;
7353 sp->ts_isdiff = DIFF_INSERT;
7354 }
7355 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007356 else
7357 fl = 1;
7358 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00007359#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007360 {
7361 /* If the previous character was the same, thus
7362 * doubling a character, give a bonus to the
7363 * score. */
7364 if (sp->ts_twordlen >= 2
7365 && tword[sp->ts_twordlen - 2] == c)
7366 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
7367 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007368 }
7369 }
7370 break;
7371
7372 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00007373 /*
7374 * Swap two bytes in the bad word: "12" -> "21".
7375 * We change "fword" here, it's changed back afterwards.
7376 */
7377 p = fword + sp->ts_fidx;
7378 c = *p;
7379 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007380 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007381 /* End of word, can't swap or replace. */
7382 sp->ts_state = STATE_FINAL;
7383 break;
7384 }
7385#ifdef FEAT_MBYTE
7386 if (has_mbyte)
7387 {
7388 n = mb_ptr2len_check(p);
7389 c = mb_ptr2char(p);
7390 c2 = mb_ptr2char(p + n);
7391 }
7392 else
7393#endif
7394 c2 = p[1];
7395 if (c == c2)
7396 {
7397 /* Characters are identical, swap won't do anything. */
7398 sp->ts_state = STATE_SWAP3;
7399 break;
7400 }
7401 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
7402 {
7403 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007404 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007405#ifdef FEAT_MBYTE
7406 if (has_mbyte)
7407 {
7408 fl = mb_char2len(c2);
7409 mch_memmove(p, p + n, fl);
7410 mb_char2bytes(c, p + fl);
7411 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7412 }
7413 else
7414#endif
7415 {
7416 p[0] = c2;
7417 p[1] = c;
7418 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
7419 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007420 }
7421 else
7422 /* If this swap doesn't work then SWAP3 won't either. */
7423 sp->ts_state = STATE_REP_INI;
7424 break;
7425
Bram Moolenaarea424162005-06-16 21:51:00 +00007426 case STATE_UNSWAP:
7427 /* Undo the STATE_SWAP swap: "21" -> "12". */
7428 p = fword + sp->ts_fidx;
7429#ifdef FEAT_MBYTE
7430 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007431 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007432 n = MB_BYTE2LEN(*p);
7433 c = mb_ptr2char(p + n);
7434 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
7435 mb_char2bytes(c, p);
7436 }
7437 else
7438#endif
7439 {
7440 c = *p;
7441 *p = p[1];
7442 p[1] = c;
7443 }
7444 /*FALLTHROUGH*/
7445
7446 case STATE_SWAP3:
7447 /* Swap two bytes, skipping one: "123" -> "321". We change
7448 * "fword" here, it's changed back afterwards. */
7449 p = fword + sp->ts_fidx;
7450#ifdef FEAT_MBYTE
7451 if (has_mbyte)
7452 {
7453 n = mb_ptr2len_check(p);
7454 c = mb_ptr2char(p);
7455 fl = mb_ptr2len_check(p + n);
7456 c2 = mb_ptr2char(p + n);
7457 c3 = mb_ptr2char(p + n + fl);
7458 }
7459 else
7460#endif
7461 {
7462 c = *p;
7463 c2 = p[1];
7464 c3 = p[2];
7465 }
7466
7467 /* When characters are identical: "121" then SWAP3 result is
7468 * identical, ROT3L result is same as SWAP: "211", ROT3L
7469 * result is same as SWAP on next char: "112". Thus skip all
7470 * swapping. Also skip when c3 is NUL. */
7471 if (c == c3 || c3 == NUL)
7472 {
7473 sp->ts_state = STATE_REP_INI;
7474 break;
7475 }
7476 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7477 {
7478 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007479 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007480#ifdef FEAT_MBYTE
7481 if (has_mbyte)
7482 {
7483 tl = mb_char2len(c3);
7484 mch_memmove(p, p + n + fl, tl);
7485 mb_char2bytes(c2, p + tl);
7486 mb_char2bytes(c, p + fl + tl);
7487 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
7488 }
7489 else
7490#endif
7491 {
7492 p[0] = p[2];
7493 p[2] = c;
7494 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7495 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007496 }
7497 else
7498 sp->ts_state = STATE_REP_INI;
7499 break;
7500
Bram Moolenaarea424162005-06-16 21:51:00 +00007501 case STATE_UNSWAP3:
7502 /* Undo STATE_SWAP3: "321" -> "123" */
7503 p = fword + sp->ts_fidx;
7504#ifdef FEAT_MBYTE
7505 if (has_mbyte)
7506 {
7507 n = MB_BYTE2LEN(*p);
7508 c2 = mb_ptr2char(p + n);
7509 fl = MB_BYTE2LEN(p[n]);
7510 c = mb_ptr2char(p + n + fl);
7511 tl = MB_BYTE2LEN(p[n + fl]);
7512 mch_memmove(p + fl + tl, p, n);
7513 mb_char2bytes(c, p);
7514 mb_char2bytes(c2, p + tl);
7515 }
7516 else
7517#endif
7518 {
7519 c = *p;
7520 *p = p[2];
7521 p[2] = c;
7522 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007523
Bram Moolenaarea424162005-06-16 21:51:00 +00007524 /* Rotate three characters left: "123" -> "231". We change
7525 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007526 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7527 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007528 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007529 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007530 p = fword + sp->ts_fidx;
7531#ifdef FEAT_MBYTE
7532 if (has_mbyte)
7533 {
7534 n = mb_ptr2len_check(p);
7535 c = mb_ptr2char(p);
7536 fl = mb_ptr2len_check(p + n);
7537 fl += mb_ptr2len_check(p + n + fl);
7538 mch_memmove(p, p + n, fl);
7539 mb_char2bytes(c, p + fl);
7540 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7541 }
7542 else
7543#endif
7544 {
7545 c = *p;
7546 *p = p[1];
7547 p[1] = p[2];
7548 p[2] = c;
7549 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7550 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007551 }
7552 else
7553 sp->ts_state = STATE_REP_INI;
7554 break;
7555
Bram Moolenaarea424162005-06-16 21:51:00 +00007556 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007557 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007558 p = fword + sp->ts_fidx;
7559#ifdef FEAT_MBYTE
7560 if (has_mbyte)
7561 {
7562 n = MB_BYTE2LEN(*p);
7563 n += MB_BYTE2LEN(p[n]);
7564 c = mb_ptr2char(p + n);
7565 tl = MB_BYTE2LEN(p[n]);
7566 mch_memmove(p + tl, p, n);
7567 mb_char2bytes(c, p);
7568 }
7569 else
7570#endif
7571 {
7572 c = p[2];
7573 p[2] = p[1];
7574 p[1] = *p;
7575 *p = c;
7576 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007577
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007578 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00007579 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007580 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7581 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007582 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007583 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007584 p = fword + sp->ts_fidx;
7585#ifdef FEAT_MBYTE
7586 if (has_mbyte)
7587 {
7588 n = mb_ptr2len_check(p);
7589 n += mb_ptr2len_check(p + n);
7590 c = mb_ptr2char(p + n);
7591 tl = mb_ptr2len_check(p + n);
7592 mch_memmove(p + tl, p, n);
7593 mb_char2bytes(c, p);
7594 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
7595 }
7596 else
7597#endif
7598 {
7599 c = p[2];
7600 p[2] = p[1];
7601 p[1] = *p;
7602 *p = c;
7603 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7604 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007605 }
7606 else
7607 sp->ts_state = STATE_REP_INI;
7608 break;
7609
Bram Moolenaarea424162005-06-16 21:51:00 +00007610 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007611 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007612 p = fword + sp->ts_fidx;
7613#ifdef FEAT_MBYTE
7614 if (has_mbyte)
7615 {
7616 c = mb_ptr2char(p);
7617 tl = MB_BYTE2LEN(*p);
7618 n = MB_BYTE2LEN(p[tl]);
7619 n += MB_BYTE2LEN(p[tl + n]);
7620 mch_memmove(p, p + tl, n);
7621 mb_char2bytes(c, p + n);
7622 }
7623 else
7624#endif
7625 {
7626 c = *p;
7627 *p = p[1];
7628 p[1] = p[2];
7629 p[2] = c;
7630 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007631 /*FALLTHROUGH*/
7632
7633 case STATE_REP_INI:
7634 /* Check if matching with REP items from the .aff file would
7635 * work. Quickly skip if there are no REP items or the score
7636 * is going to be too high anyway. */
7637 gap = &lp->lp_slang->sl_rep;
7638 if (gap->ga_len == 0
7639 || sp->ts_score + SCORE_REP >= su->su_maxscore)
7640 {
7641 sp->ts_state = STATE_FINAL;
7642 break;
7643 }
7644
7645 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00007646 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007647 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
7648 if (sp->ts_curi < 0)
7649 {
7650 sp->ts_state = STATE_FINAL;
7651 break;
7652 }
7653
7654 sp->ts_state = STATE_REP;
7655 /*FALLTHROUGH*/
7656
7657 case STATE_REP:
7658 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00007659 * match replace the characters and check if the resulting
7660 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007661 p = fword + sp->ts_fidx;
7662
7663 gap = &lp->lp_slang->sl_rep;
7664 while (sp->ts_curi < gap->ga_len)
7665 {
7666 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
7667 if (*ftp->ft_from != *p)
7668 {
7669 /* past possible matching entries */
7670 sp->ts_curi = gap->ga_len;
7671 break;
7672 }
7673 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
7674 && try_deeper(su, stack, depth, SCORE_REP))
7675 {
7676 /* Need to undo this afterwards. */
7677 sp->ts_state = STATE_REP_UNDO;
7678
7679 /* Change the "from" to the "to" string. */
7680 ++depth;
7681 fl = STRLEN(ftp->ft_from);
7682 tl = STRLEN(ftp->ft_to);
7683 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007685 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007686 repextra += tl - fl;
7687 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007688 mch_memmove(p, ftp->ft_to, tl);
7689 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00007690#ifdef FEAT_MBYTE
7691 stack[depth].ts_tcharlen = 0;
7692#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007693 break;
7694 }
7695 }
7696
7697 if (sp->ts_curi >= gap->ga_len)
7698 /* No (more) matches. */
7699 sp->ts_state = STATE_FINAL;
7700
7701 break;
7702
7703 case STATE_REP_UNDO:
7704 /* Undo a REP replacement and continue with the next one. */
7705 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
7706 + sp->ts_curi - 1;
7707 fl = STRLEN(ftp->ft_from);
7708 tl = STRLEN(ftp->ft_to);
7709 p = fword + sp->ts_fidx;
7710 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007711 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007712 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007713 repextra -= tl - fl;
7714 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007715 mch_memmove(p, ftp->ft_from, fl);
7716 sp->ts_state = STATE_REP;
7717 break;
7718
7719 default:
7720 /* Did all possible states at this level, go up one level. */
7721 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007722
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007723 if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE)
7724 {
7725 /* Continue in or go back to the prefix tree. */
7726 byts = pbyts;
7727 idxs = pidxs;
7728 splitoff = 0;
7729 }
7730
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007731 /* Don't check for CTRL-C too often, it takes time. */
7732 line_breakcheck();
7733 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007734 }
7735 }
7736}
7737
7738/*
7739 * Try going one level deeper in the tree.
7740 */
7741 static int
7742try_deeper(su, stack, depth, score_add)
7743 suginfo_T *su;
7744 trystate_T *stack;
7745 int depth;
7746 int score_add;
7747{
7748 int newscore;
7749
7750 /* Refuse to go deeper if the scrore is getting too big. */
7751 newscore = stack[depth].ts_score + score_add;
7752 if (newscore >= su->su_maxscore)
7753 return FALSE;
7754
Bram Moolenaarea424162005-06-16 21:51:00 +00007755 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007756 stack[depth + 1].ts_state = STATE_START;
7757 stack[depth + 1].ts_score = newscore;
7758 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007759 return TRUE;
7760}
7761
7762/*
7763 * "fword" is a good word with case folded. Find the matching keep-case
7764 * words and put it in "kword".
7765 * Theoretically there could be several keep-case words that result in the
7766 * same case-folded word, but we only find one...
7767 */
7768 static void
7769find_keepcap_word(slang, fword, kword)
7770 slang_T *slang;
7771 char_u *fword;
7772 char_u *kword;
7773{
7774 char_u uword[MAXWLEN]; /* "fword" in upper-case */
7775 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007776 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007777
7778 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007779 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007780 int round[MAXWLEN];
7781 int fwordidx[MAXWLEN];
7782 int uwordidx[MAXWLEN];
7783 int kwordlen[MAXWLEN];
7784
7785 int flen, ulen;
7786 int l;
7787 int len;
7788 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007789 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007790 char_u *p;
7791 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007792 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007793
7794 if (byts == NULL)
7795 {
7796 /* array is empty: "cannot happen" */
7797 *kword = NUL;
7798 return;
7799 }
7800
7801 /* Make an all-cap version of "fword". */
7802 allcap_copy(fword, uword);
7803
7804 /*
7805 * Each character needs to be tried both case-folded and upper-case.
7806 * All this gets very complicated if we keep in mind that changing case
7807 * may change the byte length of a multi-byte character...
7808 */
7809 depth = 0;
7810 arridx[0] = 0;
7811 round[0] = 0;
7812 fwordidx[0] = 0;
7813 uwordidx[0] = 0;
7814 kwordlen[0] = 0;
7815 while (depth >= 0)
7816 {
7817 if (fword[fwordidx[depth]] == NUL)
7818 {
7819 /* We are at the end of "fword". If the tree allows a word to end
7820 * here we have found a match. */
7821 if (byts[arridx[depth] + 1] == 0)
7822 {
7823 kword[kwordlen[depth]] = NUL;
7824 return;
7825 }
7826
7827 /* kword is getting too long, continue one level up */
7828 --depth;
7829 }
7830 else if (++round[depth] > 2)
7831 {
7832 /* tried both fold-case and upper-case character, continue one
7833 * level up */
7834 --depth;
7835 }
7836 else
7837 {
7838 /*
7839 * round[depth] == 1: Try using the folded-case character.
7840 * round[depth] == 2: Try using the upper-case character.
7841 */
7842#ifdef FEAT_MBYTE
7843 if (has_mbyte)
7844 {
7845 flen = mb_ptr2len_check(fword + fwordidx[depth]);
7846 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
7847 }
7848 else
7849#endif
7850 ulen = flen = 1;
7851 if (round[depth] == 1)
7852 {
7853 p = fword + fwordidx[depth];
7854 l = flen;
7855 }
7856 else
7857 {
7858 p = uword + uwordidx[depth];
7859 l = ulen;
7860 }
7861
7862 for (tryidx = arridx[depth]; l > 0; --l)
7863 {
7864 /* Perform a binary search in the list of accepted bytes. */
7865 len = byts[tryidx++];
7866 c = *p++;
7867 lo = tryidx;
7868 hi = tryidx + len - 1;
7869 while (lo < hi)
7870 {
7871 m = (lo + hi) / 2;
7872 if (byts[m] > c)
7873 hi = m - 1;
7874 else if (byts[m] < c)
7875 lo = m + 1;
7876 else
7877 {
7878 lo = hi = m;
7879 break;
7880 }
7881 }
7882
7883 /* Stop if there is no matching byte. */
7884 if (hi < lo || byts[lo] != c)
7885 break;
7886
7887 /* Continue at the child (if there is one). */
7888 tryidx = idxs[lo];
7889 }
7890
7891 if (l == 0)
7892 {
7893 /*
7894 * Found the matching char. Copy it to "kword" and go a
7895 * level deeper.
7896 */
7897 if (round[depth] == 1)
7898 {
7899 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
7900 flen);
7901 kwordlen[depth + 1] = kwordlen[depth] + flen;
7902 }
7903 else
7904 {
7905 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
7906 ulen);
7907 kwordlen[depth + 1] = kwordlen[depth] + ulen;
7908 }
7909 fwordidx[depth + 1] = fwordidx[depth] + flen;
7910 uwordidx[depth + 1] = uwordidx[depth] + ulen;
7911
7912 ++depth;
7913 arridx[depth] = tryidx;
7914 round[depth] = 0;
7915 }
7916 }
7917 }
7918
7919 /* Didn't find it: "cannot happen". */
7920 *kword = NUL;
7921}
7922
7923/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007924 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
7925 * su->su_sga.
7926 */
7927 static void
7928score_comp_sal(su)
7929 suginfo_T *su;
7930{
7931 langp_T *lp;
7932 char_u badsound[MAXWLEN];
7933 int i;
7934 suggest_T *stp;
7935 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007936 int score;
7937
7938 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
7939 return;
7940
7941 /* Use the sound-folding of the first language that supports it. */
7942 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7943 lp->lp_slang != NULL; ++lp)
7944 if (lp->lp_slang->sl_sal.ga_len > 0)
7945 {
7946 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007947 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007948
7949 for (i = 0; i < su->su_ga.ga_len; ++i)
7950 {
7951 stp = &SUG(su->su_ga, i);
7952
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007953 /* Case-fold the suggested word, sound-fold it and compute the
7954 * sound-a-like score. */
7955 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007956 if (score < SCORE_MAXMAX)
7957 {
7958 /* Add the suggestion. */
7959 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
7960 sstp->st_word = vim_strsave(stp->st_word);
7961 if (sstp->st_word != NULL)
7962 {
7963 sstp->st_score = score;
7964 sstp->st_altscore = 0;
7965 sstp->st_orglen = stp->st_orglen;
7966 ++su->su_sga.ga_len;
7967 }
7968 }
7969 }
7970 break;
7971 }
7972}
7973
7974/*
7975 * Combine the list of suggestions in su->su_ga and su->su_sga.
7976 * They are intwined.
7977 */
7978 static void
7979score_combine(su)
7980 suginfo_T *su;
7981{
7982 int i;
7983 int j;
7984 garray_T ga;
7985 garray_T *gap;
7986 langp_T *lp;
7987 suggest_T *stp;
7988 char_u *p;
7989 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007990 int round;
7991
7992 /* Add the alternate score to su_ga. */
7993 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7994 lp->lp_slang != NULL; ++lp)
7995 {
7996 if (lp->lp_slang->sl_sal.ga_len > 0)
7997 {
7998 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007999 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008000
8001 for (i = 0; i < su->su_ga.ga_len; ++i)
8002 {
8003 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008004 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
8005 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008006 if (stp->st_altscore == SCORE_MAXMAX)
8007 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
8008 else
8009 stp->st_score = (stp->st_score * 3
8010 + stp->st_altscore) / 4;
8011 stp->st_salscore = FALSE;
8012 }
8013 break;
8014 }
8015 }
8016
8017 /* Add the alternate score to su_sga. */
8018 for (i = 0; i < su->su_sga.ga_len; ++i)
8019 {
8020 stp = &SUG(su->su_sga, i);
8021 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
8022 if (stp->st_score == SCORE_MAXMAX)
8023 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
8024 else
8025 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
8026 stp->st_salscore = TRUE;
8027 }
8028
8029 /* Sort the suggestions and truncate at "maxcount" for both lists. */
8030 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8031 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
8032
8033 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
8034 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
8035 return;
8036
8037 stp = &SUG(ga, 0);
8038 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
8039 {
8040 /* round 1: get a suggestion from su_ga
8041 * round 2: get a suggestion from su_sga */
8042 for (round = 1; round <= 2; ++round)
8043 {
8044 gap = round == 1 ? &su->su_ga : &su->su_sga;
8045 if (i < gap->ga_len)
8046 {
8047 /* Don't add a word if it's already there. */
8048 p = SUG(*gap, i).st_word;
8049 for (j = 0; j < ga.ga_len; ++j)
8050 if (STRCMP(stp[j].st_word, p) == 0)
8051 break;
8052 if (j == ga.ga_len)
8053 stp[ga.ga_len++] = SUG(*gap, i);
8054 else
8055 vim_free(p);
8056 }
8057 }
8058 }
8059
8060 ga_clear(&su->su_ga);
8061 ga_clear(&su->su_sga);
8062
8063 /* Truncate the list to the number of suggestions that will be displayed. */
8064 if (ga.ga_len > su->su_maxcount)
8065 {
8066 for (i = su->su_maxcount; i < ga.ga_len; ++i)
8067 vim_free(stp[i].st_word);
8068 ga.ga_len = su->su_maxcount;
8069 }
8070
8071 su->su_ga = ga;
8072}
8073
8074/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008075 * For the goodword in "stp" compute the soundalike score compared to the
8076 * badword.
8077 */
8078 static int
8079stp_sal_score(stp, su, slang, badsound)
8080 suggest_T *stp;
8081 suginfo_T *su;
8082 slang_T *slang;
8083 char_u *badsound; /* sound-folded badword */
8084{
8085 char_u *p;
8086 char_u badsound2[MAXWLEN];
8087 char_u fword[MAXWLEN];
8088 char_u goodsound[MAXWLEN];
8089
8090 if (stp->st_orglen <= su->su_badlen)
8091 p = badsound;
8092 else
8093 {
8094 /* soundfold the bad word with more characters following */
8095 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
8096
8097 /* When joining two words the sound often changes a lot. E.g., "t he"
8098 * sounds like "t h" while "the" sounds like "@". Avoid that by
8099 * removing the space. Don't do it when the good word also contains a
8100 * space. */
8101 if (vim_iswhite(su->su_badptr[su->su_badlen])
8102 && *skiptowhite(stp->st_word) == NUL)
8103 for (p = fword; *(p = skiptowhite(p)) != NUL; )
8104 mch_memmove(p, p + 1, STRLEN(p));
8105
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008106 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008107 p = badsound2;
8108 }
8109
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008110 /* Sound-fold the word and compute the score for the difference. */
8111 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008112
8113 return soundalike_score(goodsound, p);
8114}
8115
8116/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008117 * Find suggestions by comparing the word in a sound-a-like form.
8118 */
8119 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00008120suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008121 suginfo_T *su;
8122{
8123 char_u salword[MAXWLEN];
8124 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008125 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008126 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008127 int curi[MAXWLEN];
8128 langp_T *lp;
8129 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008130 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008131 int depth;
8132 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008133 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008134 int round;
8135 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008136 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008137
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008138 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008139 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8140 lp->lp_slang != NULL; ++lp)
8141 {
8142 if (lp->lp_slang->sl_sal.ga_len > 0)
8143 {
8144 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008145 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008146
8147 /*
8148 * Go through the whole tree, soundfold each word and compare.
8149 * round 1: use the case-folded tree.
8150 * round 2: use the keep-case tree.
8151 */
8152 for (round = 1; round <= 2; ++round)
8153 {
8154 if (round == 1)
8155 {
8156 byts = lp->lp_slang->sl_fbyts;
8157 idxs = lp->lp_slang->sl_fidxs;
8158 }
8159 else
8160 {
8161 byts = lp->lp_slang->sl_kbyts;
8162 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008163 if (byts == NULL) /* no keep-case words */
8164 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008165 }
8166
8167 depth = 0;
8168 arridx[0] = 0;
8169 curi[0] = 1;
8170 while (depth >= 0 && !got_int)
8171 {
8172 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008174 /* Done all bytes at this node, go up one level. */
8175 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008176 line_breakcheck();
8177 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008178 else
8179 {
8180 /* Do one more byte at this node. */
8181 n = arridx[depth] + curi[depth];
8182 ++curi[depth];
8183 c = byts[n];
8184 if (c == 0)
8185 {
8186 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008187 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008188 if (round == 2 || (flags & WF_KEEPCAP) == 0)
8189 {
8190 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008191 /* Sound-fold. Only in keep-case tree need to
8192 * case-fold the word. */
8193 spell_soundfold(lp->lp_slang, tword,
8194 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008195
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008196 /* Compute the edit distance between the
8197 * sound-a-like words. */
8198 sound_score = soundalike_score(salword,
8199 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008200 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008201 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008202 char_u cword[MAXWLEN];
8203 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008204 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008205
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008206 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008207 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008208 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008209 /* Need to fix case according to
8210 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008211 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008212 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008213 }
8214 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008215 p = tword;
8216
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008217 if (sps_flags & SPS_DOUBLE)
8218 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008219 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008220 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008221 else
8222 {
8223 /* Compute the score. */
8224 score = spell_edit_score(
8225 su->su_badword, p);
8226 if (sps_flags & SPS_BEST)
8227 /* give a bonus for the good word
8228 * sounding the same as the bad
8229 * word */
8230 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008231 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008232 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008233 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008234 else
8235 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008236 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008237 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008238 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008239 }
8240 }
8241
8242 /* Skip over other NUL bytes. */
8243 while (byts[n + 1] == 0)
8244 {
8245 ++n;
8246 ++curi[depth];
8247 }
8248 }
8249 else
8250 {
8251 /* Normal char, go one level deeper. */
8252 tword[depth++] = c;
8253 arridx[depth] = idxs[n];
8254 curi[depth] = 1;
8255 }
8256 }
8257 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008258 }
8259 }
8260 }
8261}
8262
8263/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008264 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008265 */
8266 static void
8267make_case_word(fword, cword, flags)
8268 char_u *fword;
8269 char_u *cword;
8270 int flags;
8271{
8272 if (flags & WF_ALLCAP)
8273 /* Make it all upper-case */
8274 allcap_copy(fword, cword);
8275 else if (flags & WF_ONECAP)
8276 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008277 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008278 else
8279 /* Use goodword as-is. */
8280 STRCPY(cword, fword);
8281}
8282
Bram Moolenaarea424162005-06-16 21:51:00 +00008283/*
8284 * Use map string "map" for languages "lp".
8285 */
8286 static void
8287set_map_str(lp, map)
8288 slang_T *lp;
8289 char_u *map;
8290{
8291 char_u *p;
8292 int headc = 0;
8293 int c;
8294 int i;
8295
8296 if (*map == NUL)
8297 {
8298 lp->sl_has_map = FALSE;
8299 return;
8300 }
8301 lp->sl_has_map = TRUE;
8302
8303 /* Init the array and hash table empty. */
8304 for (i = 0; i < 256; ++i)
8305 lp->sl_map_array[i] = 0;
8306#ifdef FEAT_MBYTE
8307 hash_init(&lp->sl_map_hash);
8308#endif
8309
8310 /*
8311 * The similar characters are stored separated with slashes:
8312 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
8313 * before the same slash. For characters above 255 sl_map_hash is used.
8314 */
8315 for (p = map; *p != NUL; )
8316 {
8317#ifdef FEAT_MBYTE
8318 c = mb_ptr2char_adv(&p);
8319#else
8320 c = *p++;
8321#endif
8322 if (c == '/')
8323 headc = 0;
8324 else
8325 {
8326 if (headc == 0)
8327 headc = c;
8328
8329#ifdef FEAT_MBYTE
8330 /* Characters above 255 don't fit in sl_map_array[], put them in
8331 * the hash table. Each entry is the char, a NUL the headchar and
8332 * a NUL. */
8333 if (c >= 256)
8334 {
8335 int cl = mb_char2len(c);
8336 int headcl = mb_char2len(headc);
8337 char_u *b;
8338 hash_T hash;
8339 hashitem_T *hi;
8340
8341 b = alloc((unsigned)(cl + headcl + 2));
8342 if (b == NULL)
8343 return;
8344 mb_char2bytes(c, b);
8345 b[cl] = NUL;
8346 mb_char2bytes(headc, b + cl + 1);
8347 b[cl + 1 + headcl] = NUL;
8348 hash = hash_hash(b);
8349 hi = hash_lookup(&lp->sl_map_hash, b, hash);
8350 if (HASHITEM_EMPTY(hi))
8351 hash_add_item(&lp->sl_map_hash, hi, b, hash);
8352 else
8353 {
8354 /* This should have been checked when generating the .spl
8355 * file. */
8356 EMSG(_("E999: duplicate char in MAP entry"));
8357 vim_free(b);
8358 }
8359 }
8360 else
8361#endif
8362 lp->sl_map_array[c] = headc;
8363 }
8364 }
8365}
8366
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008367/*
8368 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
8369 * lines in the .aff file.
8370 */
8371 static int
8372similar_chars(slang, c1, c2)
8373 slang_T *slang;
8374 int c1;
8375 int c2;
8376{
Bram Moolenaarea424162005-06-16 21:51:00 +00008377 int m1, m2;
8378#ifdef FEAT_MBYTE
8379 char_u buf[MB_MAXBYTES];
8380 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008381
Bram Moolenaarea424162005-06-16 21:51:00 +00008382 if (c1 >= 256)
8383 {
8384 buf[mb_char2bytes(c1, buf)] = 0;
8385 hi = hash_find(&slang->sl_map_hash, buf);
8386 if (HASHITEM_EMPTY(hi))
8387 m1 = 0;
8388 else
8389 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8390 }
8391 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008392#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008393 m1 = slang->sl_map_array[c1];
8394 if (m1 == 0)
8395 return FALSE;
8396
8397
8398#ifdef FEAT_MBYTE
8399 if (c2 >= 256)
8400 {
8401 buf[mb_char2bytes(c2, buf)] = 0;
8402 hi = hash_find(&slang->sl_map_hash, buf);
8403 if (HASHITEM_EMPTY(hi))
8404 m2 = 0;
8405 else
8406 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8407 }
8408 else
8409#endif
8410 m2 = slang->sl_map_array[c2];
8411
8412 return m1 == m2;
8413}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008414
8415/*
8416 * Add a suggestion to the list of suggestions.
8417 * Do not add a duplicate suggestion or suggestions with a bad score.
8418 * When "use_score" is not zero it's used, otherwise the score is computed
8419 * with spell_edit_score().
8420 */
8421 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008422add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008423 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008424 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008425 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008426 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008427 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008428 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008429 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008430{
8431 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008432 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008433 char_u *p = NULL;
8434 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008435
8436 /* Check that the word wasn't banned. */
8437 if (was_banned(su, goodword))
8438 return;
8439
Bram Moolenaar0c405862005-06-22 22:26:26 +00008440 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
8441 * Remove the common part from "goodword". */
8442 i = badlen - su->su_badlen;
8443 if (i > 0)
8444 {
8445 /* This assumes there was no case folding or it didn't change the
8446 * length... */
8447 p = goodword + STRLEN(goodword) - i;
8448 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
8449 {
8450 badlen = su->su_badlen;
8451 c = *p;
8452 *p = NUL;
8453 }
8454 else
8455 p = NULL;
8456 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008457 else if (i < 0)
8458 {
8459 /* When replacing part of the word check that we actually change
8460 * something. For "the the" a suggestion can be replacing the first
8461 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008462 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008463 && STRNCMP(su->su_badword, goodword, badlen) == 0)
8464 return;
8465 }
8466
Bram Moolenaar0c405862005-06-22 22:26:26 +00008467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008468 if (score <= su->su_maxscore)
8469 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008470 /* Check if the word is already there. Also check the length that is
8471 * being replaced "thes," -> "these" is a different suggestion from
8472 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008473 stp = &SUG(*gap, 0);
8474 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008475 if (STRCMP(stp[i].st_word, goodword) == 0
8476 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008477 {
8478 /* Found it. Remember the lowest score. */
8479 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008481 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008482 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008483 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008484 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008485 break;
8486 }
8487
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008488 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008489 {
8490 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008491 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008492 stp->st_word = vim_strsave(goodword);
8493 if (stp->st_word != NULL)
8494 {
8495 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008496 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008497 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008498 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008499 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008500
8501 /* If we have too many suggestions now, sort the list and keep
8502 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008503 if (gap->ga_len > SUG_MAX_COUNT(su))
8504 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
8505 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008506 }
8507 }
8508 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008509
8510 if (p != NULL)
8511 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008512}
8513
8514/*
8515 * Add a word to be banned.
8516 */
8517 static void
8518add_banned(su, word)
8519 suginfo_T *su;
8520 char_u *word;
8521{
8522 char_u *s = vim_strsave(word);
8523 hash_T hash;
8524 hashitem_T *hi;
8525
8526 if (s != NULL)
8527 {
8528 hash = hash_hash(s);
8529 hi = hash_lookup(&su->su_banned, s, hash);
8530 if (HASHITEM_EMPTY(hi))
8531 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008532 else
8533 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008534 }
8535}
8536
8537/*
8538 * Return TRUE if a word appears in the list of banned words.
8539 */
8540 static int
8541was_banned(su, word)
8542 suginfo_T *su;
8543 char_u *word;
8544{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008545 hashitem_T *hi = hash_find(&su->su_banned, word);
8546
8547 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008548}
8549
8550/*
8551 * Free the banned words in "su".
8552 */
8553 static void
8554free_banned(su)
8555 suginfo_T *su;
8556{
8557 int todo;
8558 hashitem_T *hi;
8559
8560 todo = su->su_banned.ht_used;
8561 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
8562 {
8563 if (!HASHITEM_EMPTY(hi))
8564 {
8565 vim_free(hi->hi_key);
8566 --todo;
8567 }
8568 }
8569 hash_clear(&su->su_banned);
8570}
8571
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008572/*
8573 * Recompute the score if sound-folding is possible. This is slow,
8574 * thus only done for the final results.
8575 */
8576 static void
8577rescore_suggestions(su)
8578 suginfo_T *su;
8579{
8580 langp_T *lp;
8581 suggest_T *stp;
8582 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008583 int i;
8584
8585 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8586 lp->lp_slang != NULL; ++lp)
8587 {
8588 if (lp->lp_slang->sl_sal.ga_len > 0)
8589 {
8590 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008591 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008592
8593 for (i = 0; i < su->su_ga.ga_len; ++i)
8594 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008595 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008596 if (!stp->st_had_bonus)
8597 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008598 stp->st_altscore = stp_sal_score(stp, su,
8599 lp->lp_slang, sal_badword);
8600 if (stp->st_altscore == SCORE_MAXMAX)
8601 stp->st_altscore = SCORE_BIG;
8602 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008603 }
8604 }
8605 break;
8606 }
8607 }
8608}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008609
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008610static int
8611#ifdef __BORLANDC__
8612_RTLENTRYF
8613#endif
8614sug_compare __ARGS((const void *s1, const void *s2));
8615
8616/*
8617 * Function given to qsort() to sort the suggestions on st_score.
8618 */
8619 static int
8620#ifdef __BORLANDC__
8621_RTLENTRYF
8622#endif
8623sug_compare(s1, s2)
8624 const void *s1;
8625 const void *s2;
8626{
8627 suggest_T *p1 = (suggest_T *)s1;
8628 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008629 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008630
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008631 if (n == 0)
8632 return p1->st_altscore - p2->st_altscore;
8633 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008634}
8635
8636/*
8637 * Cleanup the suggestions:
8638 * - Sort on score.
8639 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008640 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008641 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008642 static int
8643cleanup_suggestions(gap, maxscore, keep)
8644 garray_T *gap;
8645 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008646 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008647{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008648 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008649 int i;
8650
8651 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008652 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008653
8654 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008655 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008656 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008657 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008658 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008659 gap->ga_len = keep;
8660 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008661 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008662 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008663}
8664
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008665#if defined(FEAT_EVAL) || defined(PROTO)
8666/*
8667 * Soundfold a string, for soundfold().
8668 * Result is in allocated memory, NULL for an error.
8669 */
8670 char_u *
8671eval_soundfold(word)
8672 char_u *word;
8673{
8674 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008675 char_u sound[MAXWLEN];
8676
8677 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
8678 /* Use the sound-folding of the first language that supports it. */
8679 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8680 lp->lp_slang != NULL; ++lp)
8681 if (lp->lp_slang->sl_sal.ga_len > 0)
8682 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008683 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008684 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008685 return vim_strsave(sound);
8686 }
8687
8688 /* No language with sound folding, return word as-is. */
8689 return vim_strsave(word);
8690}
8691#endif
8692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008693/*
8694 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
8695 */
8696 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008697spell_soundfold(slang, inword, folded, res)
8698 slang_T *slang;
8699 char_u *inword;
8700 int folded; /* "inword" is already case-folded */
8701 char_u *res;
8702{
8703 char_u fword[MAXWLEN];
8704 char_u *word;
8705
8706 if (slang->sl_sofo)
8707 /* SOFOFROM and SOFOTO used */
8708 spell_soundfold_sofo(slang, inword, res);
8709 else
8710 {
8711 /* SAL items used. Requires the word to be case-folded. */
8712 if (folded)
8713 word = inword;
8714 else
8715 {
8716 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
8717 word = fword;
8718 }
8719
8720#ifdef FEAT_MBYTE
8721 if (has_mbyte)
8722 spell_soundfold_wsal(slang, word, res);
8723 else
8724#endif
8725 spell_soundfold_sal(slang, word, res);
8726 }
8727}
8728
8729/*
8730 * Perform sound folding of "inword" into "res" according to SOFOFROM and
8731 * SOFOTO lines.
8732 */
8733 static void
8734spell_soundfold_sofo(slang, inword, res)
8735 slang_T *slang;
8736 char_u *inword;
8737 char_u *res;
8738{
8739 char_u *s;
8740 int ri = 0;
8741 int c;
8742
8743#ifdef FEAT_MBYTE
8744 if (has_mbyte)
8745 {
8746 int prevc = 0;
8747 int *ip;
8748
8749 /* The sl_sal_first[] table contains the translation for chars up to
8750 * 255, sl_sal the rest. */
8751 for (s = inword; *s != NUL; )
8752 {
8753 c = mb_ptr2char_adv(&s);
8754 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
8755 c = ' ';
8756 else if (c < 256)
8757 c = slang->sl_sal_first[c];
8758 else
8759 {
8760 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
8761 if (ip == NULL) /* empty list, can't match */
8762 c = NUL;
8763 else
8764 for (;;) /* find "c" in the list */
8765 {
8766 if (*ip == 0) /* not found */
8767 {
8768 c = NUL;
8769 break;
8770 }
8771 if (*ip == c) /* match! */
8772 {
8773 c = ip[1];
8774 break;
8775 }
8776 ip += 2;
8777 }
8778 }
8779
8780 if (c != NUL && c != prevc)
8781 {
8782 ri += mb_char2bytes(c, res + ri);
8783 if (ri + MB_MAXBYTES > MAXWLEN)
8784 break;
8785 prevc = c;
8786 }
8787 }
8788 }
8789 else
8790#endif
8791 {
8792 /* The sl_sal_first[] table contains the translation. */
8793 for (s = inword; (c = *s) != NUL; ++s)
8794 {
8795 if (vim_iswhite(c))
8796 c = ' ';
8797 else
8798 c = slang->sl_sal_first[c];
8799 if (c != NUL && (ri == 0 || res[ri - 1] != c))
8800 res[ri++] = c;
8801 }
8802 }
8803
8804 res[ri] = NUL;
8805}
8806
8807 static void
8808spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008809 slang_T *slang;
8810 char_u *inword;
8811 char_u *res;
8812{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008813 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008814 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008815 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008816 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008817 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008818 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008819 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008820 int n, k = 0;
8821 int z0;
8822 int k0;
8823 int n0;
8824 int c;
8825 int pri;
8826 int p0 = -333;
8827 int c0;
8828
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008829 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008830 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008831 if (slang->sl_rem_accents)
8832 {
8833 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008834 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008835 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008836 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008837 {
8838 *t++ = ' ';
8839 s = skipwhite(s);
8840 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008841 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008842 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008843 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008844 *t++ = *s;
8845 ++s;
8846 }
8847 }
8848 *t = NUL;
8849 }
8850 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008851 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008852
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008853 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008854
8855 /*
8856 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008857 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008858 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008859 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008860 while ((c = word[i]) != NUL)
8861 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008862 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008863 n = slang->sl_sal_first[c];
8864 z0 = 0;
8865
8866 if (n >= 0)
8867 {
8868 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008869 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008870 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008871 /* Quickly skip entries that don't match the word. Most
8872 * entries are less then three chars, optimize for that. */
8873 k = smp[n].sm_leadlen;
8874 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008875 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008876 if (word[i + 1] != s[1])
8877 continue;
8878 if (k > 2)
8879 {
8880 for (j = 2; j < k; ++j)
8881 if (word[i + j] != s[j])
8882 break;
8883 if (j < k)
8884 continue;
8885 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008886 }
8887
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008888 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008889 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008890 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008891 while (*pf != NUL && *pf != word[i + k])
8892 ++pf;
8893 if (*pf == NUL)
8894 continue;
8895 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008896 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008897 s = smp[n].sm_rules;
8898 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008899
8900 p0 = *s;
8901 k0 = k;
8902 while (*s == '-' && k > 1)
8903 {
8904 k--;
8905 s++;
8906 }
8907 if (*s == '<')
8908 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008909 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008910 {
8911 /* determine priority */
8912 pri = *s - '0';
8913 s++;
8914 }
8915 if (*s == '^' && *(s + 1) == '^')
8916 s++;
8917
8918 if (*s == NUL
8919 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008920 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008921 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008922 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008923 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008924 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008925 && spell_iswordp(word + i - 1, curbuf)
8926 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008927 {
8928 /* search for followup rules, if: */
8929 /* followup and k > 1 and NO '-' in searchstring */
8930 c0 = word[i + k - 1];
8931 n0 = slang->sl_sal_first[c0];
8932
8933 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008934 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008935 {
8936 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008937 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008938 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008939 /* Quickly skip entries that don't match the word.
8940 * */
8941 k0 = smp[n0].sm_leadlen;
8942 if (k0 > 1)
8943 {
8944 if (word[i + k] != s[1])
8945 continue;
8946 if (k0 > 2)
8947 {
8948 pf = word + i + k + 1;
8949 for (j = 2; j < k0; ++j)
8950 if (*pf++ != s[j])
8951 break;
8952 if (j < k0)
8953 continue;
8954 }
8955 }
8956 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008957
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008958 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008959 {
8960 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008961 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008962 while (*pf != NUL && *pf != word[i + k0])
8963 ++pf;
8964 if (*pf == NUL)
8965 continue;
8966 ++k0;
8967 }
8968
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008969 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008970 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008971 while (*s == '-')
8972 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008973 /* "k0" gets NOT reduced because
8974 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008975 s++;
8976 }
8977 if (*s == '<')
8978 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008979 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008980 {
8981 p0 = *s - '0';
8982 s++;
8983 }
8984
8985 if (*s == NUL
8986 /* *s == '^' cuts */
8987 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008988 && !spell_iswordp(word + i + k0,
8989 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 {
8991 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008992 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008993 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008994
8995 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008996 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008997 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008998 /* rule fits; stop search */
8999 break;
9000 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009001 }
9002
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009003 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009004 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009005 }
9006
9007 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009008 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009009 if (s == NULL)
9010 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009011 pf = smp[n].sm_rules;
9012 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009013 if (p0 == 1 && z == 0)
9014 {
9015 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009016 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
9017 || res[reslen - 1] == *s))
9018 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009019 z0 = 1;
9020 z = 1;
9021 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009022 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009023 {
9024 word[i + k0] = *s;
9025 k0++;
9026 s++;
9027 }
9028 if (k > k0)
9029 mch_memmove(word + i + k0, word + i + k,
9030 STRLEN(word + i + k) + 1);
9031
9032 /* new "actual letter" */
9033 c = word[i];
9034 }
9035 else
9036 {
9037 /* no '<' rule used */
9038 i += k - 1;
9039 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009040 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009041 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009042 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009043 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009044 s++;
9045 }
9046 /* new "actual letter" */
9047 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009048 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009049 {
9050 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009051 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009052 mch_memmove(word, word + i + 1,
9053 STRLEN(word + i + 1) + 1);
9054 i = 0;
9055 z0 = 1;
9056 }
9057 }
9058 break;
9059 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009060 }
9061 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009062 else if (vim_iswhite(c))
9063 {
9064 c = ' ';
9065 k = 1;
9066 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009067
9068 if (z0 == 0)
9069 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009070 if (k && !p0 && reslen < MAXWLEN && c != NUL
9071 && (!slang->sl_collapse || reslen == 0
9072 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009073 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009074 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009075
9076 i++;
9077 z = 0;
9078 k = 0;
9079 }
9080 }
9081
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009082 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009083}
9084
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009085#ifdef FEAT_MBYTE
9086/*
9087 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
9088 * Multi-byte version of spell_soundfold().
9089 */
9090 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009091spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009092 slang_T *slang;
9093 char_u *inword;
9094 char_u *res;
9095{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009096 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009097 int word[MAXWLEN];
9098 int wres[MAXWLEN];
9099 int l;
9100 char_u *s;
9101 int *ws;
9102 char_u *t;
9103 int *pf;
9104 int i, j, z;
9105 int reslen;
9106 int n, k = 0;
9107 int z0;
9108 int k0;
9109 int n0;
9110 int c;
9111 int pri;
9112 int p0 = -333;
9113 int c0;
9114 int did_white = FALSE;
9115
9116 /*
9117 * Convert the multi-byte string to a wide-character string.
9118 * Remove accents, if wanted. We actually remove all non-word characters.
9119 * But keep white space.
9120 */
9121 n = 0;
9122 for (s = inword; *s != NUL; )
9123 {
9124 t = s;
9125 c = mb_ptr2char_adv(&s);
9126 if (slang->sl_rem_accents)
9127 {
9128 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
9129 {
9130 if (did_white)
9131 continue;
9132 c = ' ';
9133 did_white = TRUE;
9134 }
9135 else
9136 {
9137 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009138 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009139 continue;
9140 }
9141 }
9142 word[n++] = c;
9143 }
9144 word[n] = NUL;
9145
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009146 /*
9147 * This comes from Aspell phonet.cpp.
9148 * Converted from C++ to C. Added support for multi-byte chars.
9149 * Changed to keep spaces.
9150 */
9151 i = reslen = z = 0;
9152 while ((c = word[i]) != NUL)
9153 {
9154 /* Start with the first rule that has the character in the word. */
9155 n = slang->sl_sal_first[c & 0xff];
9156 z0 = 0;
9157
9158 if (n >= 0)
9159 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009160 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009161 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
9162 {
9163 /* Quickly skip entries that don't match the word. Most
9164 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009165 if (c != ws[0])
9166 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009167 k = smp[n].sm_leadlen;
9168 if (k > 1)
9169 {
9170 if (word[i + 1] != ws[1])
9171 continue;
9172 if (k > 2)
9173 {
9174 for (j = 2; j < k; ++j)
9175 if (word[i + j] != ws[j])
9176 break;
9177 if (j < k)
9178 continue;
9179 }
9180 }
9181
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009182 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009183 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009184 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009185 while (*pf != NUL && *pf != word[i + k])
9186 ++pf;
9187 if (*pf == NUL)
9188 continue;
9189 ++k;
9190 }
9191 s = smp[n].sm_rules;
9192 pri = 5; /* default priority */
9193
9194 p0 = *s;
9195 k0 = k;
9196 while (*s == '-' && k > 1)
9197 {
9198 k--;
9199 s++;
9200 }
9201 if (*s == '<')
9202 s++;
9203 if (VIM_ISDIGIT(*s))
9204 {
9205 /* determine priority */
9206 pri = *s - '0';
9207 s++;
9208 }
9209 if (*s == '^' && *(s + 1) == '^')
9210 s++;
9211
9212 if (*s == NUL
9213 || (*s == '^'
9214 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009215 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009216 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009217 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009218 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009219 && spell_iswordp_w(word + i - 1, curbuf)
9220 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009221 {
9222 /* search for followup rules, if: */
9223 /* followup and k > 1 and NO '-' in searchstring */
9224 c0 = word[i + k - 1];
9225 n0 = slang->sl_sal_first[c0 & 0xff];
9226
9227 if (slang->sl_followup && k > 1 && n0 >= 0
9228 && p0 != '-' && word[i + k] != NUL)
9229 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009230 /* Test follow-up rule for "word[i + k]"; loop over
9231 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009232 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
9233 == (c0 & 0xff); ++n0)
9234 {
9235 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009236 */
9237 if (c0 != ws[0])
9238 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009239 k0 = smp[n0].sm_leadlen;
9240 if (k0 > 1)
9241 {
9242 if (word[i + k] != ws[1])
9243 continue;
9244 if (k0 > 2)
9245 {
9246 pf = word + i + k + 1;
9247 for (j = 2; j < k0; ++j)
9248 if (*pf++ != ws[j])
9249 break;
9250 if (j < k0)
9251 continue;
9252 }
9253 }
9254 k0 += k - 1;
9255
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009256 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009257 {
9258 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009259 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009260 while (*pf != NUL && *pf != word[i + k0])
9261 ++pf;
9262 if (*pf == NUL)
9263 continue;
9264 ++k0;
9265 }
9266
9267 p0 = 5;
9268 s = smp[n0].sm_rules;
9269 while (*s == '-')
9270 {
9271 /* "k0" gets NOT reduced because
9272 * "if (k0 == k)" */
9273 s++;
9274 }
9275 if (*s == '<')
9276 s++;
9277 if (VIM_ISDIGIT(*s))
9278 {
9279 p0 = *s - '0';
9280 s++;
9281 }
9282
9283 if (*s == NUL
9284 /* *s == '^' cuts */
9285 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009286 && !spell_iswordp_w(word + i + k0,
9287 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009288 {
9289 if (k0 == k)
9290 /* this is just a piece of the string */
9291 continue;
9292
9293 if (p0 < pri)
9294 /* priority too low */
9295 continue;
9296 /* rule fits; stop search */
9297 break;
9298 }
9299 }
9300
9301 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
9302 == (c0 & 0xff))
9303 continue;
9304 }
9305
9306 /* replace string */
9307 ws = smp[n].sm_to_w;
9308 s = smp[n].sm_rules;
9309 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
9310 if (p0 == 1 && z == 0)
9311 {
9312 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009313 if (reslen > 0 && ws != NULL && *ws != NUL
9314 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009315 || wres[reslen - 1] == *ws))
9316 reslen--;
9317 z0 = 1;
9318 z = 1;
9319 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009320 if (ws != NULL)
9321 while (*ws != NUL && word[i + k0] != NUL)
9322 {
9323 word[i + k0] = *ws;
9324 k0++;
9325 ws++;
9326 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009327 if (k > k0)
9328 mch_memmove(word + i + k0, word + i + k,
9329 sizeof(int) * (STRLEN(word + i + k) + 1));
9330
9331 /* new "actual letter" */
9332 c = word[i];
9333 }
9334 else
9335 {
9336 /* no '<' rule used */
9337 i += k - 1;
9338 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009339 if (ws != NULL)
9340 while (*ws != NUL && ws[1] != NUL
9341 && reslen < MAXWLEN)
9342 {
9343 if (reslen == 0 || wres[reslen - 1] != *ws)
9344 wres[reslen++] = *ws;
9345 ws++;
9346 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009347 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009348 if (ws == NULL)
9349 c = NUL;
9350 else
9351 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009352 if (strstr((char *)s, "^^") != NULL)
9353 {
9354 if (c != NUL)
9355 wres[reslen++] = c;
9356 mch_memmove(word, word + i + 1,
9357 sizeof(int) * (STRLEN(word + i + 1) + 1));
9358 i = 0;
9359 z0 = 1;
9360 }
9361 }
9362 break;
9363 }
9364 }
9365 }
9366 else if (vim_iswhite(c))
9367 {
9368 c = ' ';
9369 k = 1;
9370 }
9371
9372 if (z0 == 0)
9373 {
9374 if (k && !p0 && reslen < MAXWLEN && c != NUL
9375 && (!slang->sl_collapse || reslen == 0
9376 || wres[reslen - 1] != c))
9377 /* condense only double letters */
9378 wres[reslen++] = c;
9379
9380 i++;
9381 z = 0;
9382 k = 0;
9383 }
9384 }
9385
9386 /* Convert wide characters in "wres" to a multi-byte string in "res". */
9387 l = 0;
9388 for (n = 0; n < reslen; ++n)
9389 {
9390 l += mb_char2bytes(wres[n], res + l);
9391 if (l + MB_MAXBYTES > MAXWLEN)
9392 break;
9393 }
9394 res[l] = NUL;
9395}
9396#endif
9397
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009398/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009399 * Compute a score for two sound-a-like words.
9400 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
9401 * Instead of a generic loop we write out the code. That keeps it fast by
9402 * avoiding checks that will not be possible.
9403 */
9404 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009405soundalike_score(goodstart, badstart)
9406 char_u *goodstart; /* sound-folded good word */
9407 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009408{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009409 char_u *goodsound = goodstart;
9410 char_u *badsound = badstart;
9411 int goodlen;
9412 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009413 int n;
9414 char_u *pl, *ps;
9415 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009416 int score = 0;
9417
9418 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
9419 * counted so much, vowels halfway the word aren't counted at all. */
9420 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
9421 {
9422 score = SCORE_DEL / 2;
9423 if (*badsound == '*')
9424 ++badsound;
9425 else
9426 ++goodsound;
9427 }
9428
9429 goodlen = STRLEN(goodsound);
9430 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009431
9432 /* Return quickly if the lenghts are too different to be fixed by two
9433 * changes. */
9434 n = goodlen - badlen;
9435 if (n < -2 || n > 2)
9436 return SCORE_MAXMAX;
9437
9438 if (n > 0)
9439 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009440 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009441 ps = badsound;
9442 }
9443 else
9444 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009445 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009446 ps = goodsound;
9447 }
9448
9449 /* Skip over the identical part. */
9450 while (*pl == *ps && *pl != NUL)
9451 {
9452 ++pl;
9453 ++ps;
9454 }
9455
9456 switch (n)
9457 {
9458 case -2:
9459 case 2:
9460 /*
9461 * Must delete two characters from "pl".
9462 */
9463 ++pl; /* first delete */
9464 while (*pl == *ps)
9465 {
9466 ++pl;
9467 ++ps;
9468 }
9469 /* strings must be equal after second delete */
9470 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009471 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009472
9473 /* Failed to compare. */
9474 break;
9475
9476 case -1:
9477 case 1:
9478 /*
9479 * Minimal one delete from "pl" required.
9480 */
9481
9482 /* 1: delete */
9483 pl2 = pl + 1;
9484 ps2 = ps;
9485 while (*pl2 == *ps2)
9486 {
9487 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009488 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009489 ++pl2;
9490 ++ps2;
9491 }
9492
9493 /* 2: delete then swap, then rest must be equal */
9494 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9495 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009496 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009497
9498 /* 3: delete then substitute, then the rest must be equal */
9499 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009500 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009501
9502 /* 4: first swap then delete */
9503 if (pl[0] == ps[1] && pl[1] == ps[0])
9504 {
9505 pl2 = pl + 2; /* swap, skip two chars */
9506 ps2 = ps + 2;
9507 while (*pl2 == *ps2)
9508 {
9509 ++pl2;
9510 ++ps2;
9511 }
9512 /* delete a char and then strings must be equal */
9513 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009514 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009515 }
9516
9517 /* 5: first substitute then delete */
9518 pl2 = pl + 1; /* substitute, skip one char */
9519 ps2 = ps + 1;
9520 while (*pl2 == *ps2)
9521 {
9522 ++pl2;
9523 ++ps2;
9524 }
9525 /* delete a char and then strings must be equal */
9526 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009527 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009528
9529 /* Failed to compare. */
9530 break;
9531
9532 case 0:
9533 /*
9534 * Lenghts are equal, thus changes must result in same length: An
9535 * insert is only possible in combination with a delete.
9536 * 1: check if for identical strings
9537 */
9538 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009539 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009540
9541 /* 2: swap */
9542 if (pl[0] == ps[1] && pl[1] == ps[0])
9543 {
9544 pl2 = pl + 2; /* swap, skip two chars */
9545 ps2 = ps + 2;
9546 while (*pl2 == *ps2)
9547 {
9548 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009549 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009550 ++pl2;
9551 ++ps2;
9552 }
9553 /* 3: swap and swap again */
9554 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9555 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009556 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009557
9558 /* 4: swap and substitute */
9559 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009560 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009561 }
9562
9563 /* 5: substitute */
9564 pl2 = pl + 1;
9565 ps2 = ps + 1;
9566 while (*pl2 == *ps2)
9567 {
9568 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009569 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009570 ++pl2;
9571 ++ps2;
9572 }
9573
9574 /* 6: substitute and swap */
9575 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9576 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009577 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009578
9579 /* 7: substitute and substitute */
9580 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009581 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009582
9583 /* 8: insert then delete */
9584 pl2 = pl;
9585 ps2 = ps + 1;
9586 while (*pl2 == *ps2)
9587 {
9588 ++pl2;
9589 ++ps2;
9590 }
9591 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009592 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009593
9594 /* 9: delete then insert */
9595 pl2 = pl + 1;
9596 ps2 = ps;
9597 while (*pl2 == *ps2)
9598 {
9599 ++pl2;
9600 ++ps2;
9601 }
9602 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009603 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009604
9605 /* Failed to compare. */
9606 break;
9607 }
9608
9609 return SCORE_MAXMAX;
9610}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009612/*
9613 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009614 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009615 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009616 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009617 * It has been converted from C++ to C and modified to support multi-byte
9618 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009619 */
9620 static int
9621spell_edit_score(badword, goodword)
9622 char_u *badword;
9623 char_u *goodword;
9624{
9625 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009626 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009627 int j, i;
9628 int t;
9629 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009630 int pbc, pgc;
9631#ifdef FEAT_MBYTE
9632 char_u *p;
9633 int wbadword[MAXWLEN];
9634 int wgoodword[MAXWLEN];
9635
9636 if (has_mbyte)
9637 {
9638 /* Get the characters from the multi-byte strings and put them in an
9639 * int array for easy access. */
9640 for (p = badword, badlen = 0; *p != NUL; )
9641 wbadword[badlen++] = mb_ptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00009642 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009643 for (p = goodword, goodlen = 0; *p != NUL; )
9644 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00009645 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009646 }
9647 else
9648#endif
9649 {
9650 badlen = STRLEN(badword) + 1;
9651 goodlen = STRLEN(goodword) + 1;
9652 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009653
9654 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
9655#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009656 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
9657 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009658 if (cnt == NULL)
9659 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009660
9661 CNT(0, 0) = 0;
9662 for (j = 1; j <= goodlen; ++j)
9663 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
9664
9665 for (i = 1; i <= badlen; ++i)
9666 {
9667 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
9668 for (j = 1; j <= goodlen; ++j)
9669 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009670#ifdef FEAT_MBYTE
9671 if (has_mbyte)
9672 {
9673 bc = wbadword[i - 1];
9674 gc = wgoodword[j - 1];
9675 }
9676 else
9677#endif
9678 {
9679 bc = badword[i - 1];
9680 gc = goodword[j - 1];
9681 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009682 if (bc == gc)
9683 CNT(i, j) = CNT(i - 1, j - 1);
9684 else
9685 {
9686 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009687 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009688 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
9689 else
9690 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
9691
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009692 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009693 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009694#ifdef FEAT_MBYTE
9695 if (has_mbyte)
9696 {
9697 pbc = wbadword[i - 2];
9698 pgc = wgoodword[j - 2];
9699 }
9700 else
9701#endif
9702 {
9703 pbc = badword[i - 2];
9704 pgc = goodword[j - 2];
9705 }
9706 if (bc == pgc && pbc == gc)
9707 {
9708 t = SCORE_SWAP + CNT(i - 2, j - 2);
9709 if (t < CNT(i, j))
9710 CNT(i, j) = t;
9711 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009712 }
9713 t = SCORE_DEL + CNT(i - 1, j);
9714 if (t < CNT(i, j))
9715 CNT(i, j) = t;
9716 t = SCORE_INS + CNT(i, j - 1);
9717 if (t < CNT(i, j))
9718 CNT(i, j) = t;
9719 }
9720 }
9721 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009722
9723 i = CNT(badlen - 1, goodlen - 1);
9724 vim_free(cnt);
9725 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009726}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009727
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009728/*
9729 * ":spelldump"
9730 */
9731/*ARGSUSED*/
9732 void
9733ex_spelldump(eap)
9734 exarg_T *eap;
9735{
9736 buf_T *buf = curbuf;
9737 langp_T *lp;
9738 slang_T *slang;
9739 idx_T arridx[MAXWLEN];
9740 int curi[MAXWLEN];
9741 char_u word[MAXWLEN];
9742 int c;
9743 char_u *byts;
9744 idx_T *idxs;
9745 linenr_T lnum = 0;
9746 int round;
9747 int depth;
9748 int n;
9749 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009750 char_u *region_names = NULL; /* region names being used */
9751 int do_region = TRUE; /* dump region names and numbers */
9752 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009753
9754 if (no_spell_checking())
9755 return;
9756
9757 /* Create a new empty buffer by splitting the window. */
9758 do_cmdline_cmd((char_u *)"new");
9759 if (!bufempty() || !buf_valid(buf))
9760 return;
9761
Bram Moolenaar7887d882005-07-01 22:33:52 +00009762 /* Find out if we can support regions: All languages must support the same
9763 * regions or none at all. */
9764 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9765 {
9766 p = lp->lp_slang->sl_regions;
9767 if (p[0] != 0)
9768 {
9769 if (region_names == NULL) /* first language with regions */
9770 region_names = p;
9771 else if (STRCMP(region_names, p) != 0)
9772 {
9773 do_region = FALSE; /* region names are different */
9774 break;
9775 }
9776 }
9777 }
9778
9779 if (do_region && region_names != NULL)
9780 {
9781 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
9782 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9783 }
9784 else
9785 do_region = FALSE;
9786
9787 /*
9788 * Loop over all files loaded for the entries in 'spelllang'.
9789 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009790 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9791 {
9792 slang = lp->lp_slang;
9793
9794 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
9795 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9796
9797 /* round 1: case-folded tree
9798 * round 2: keep-case tree */
9799 for (round = 1; round <= 2; ++round)
9800 {
9801 if (round == 1)
9802 {
9803 byts = slang->sl_fbyts;
9804 idxs = slang->sl_fidxs;
9805 }
9806 else
9807 {
9808 byts = slang->sl_kbyts;
9809 idxs = slang->sl_kidxs;
9810 }
9811 if (byts == NULL)
9812 continue; /* array is empty */
9813
9814 depth = 0;
9815 arridx[0] = 0;
9816 curi[0] = 1;
9817 while (depth >= 0 && !got_int)
9818 {
9819 if (curi[depth] > byts[arridx[depth]])
9820 {
9821 /* Done all bytes at this node, go up one level. */
9822 --depth;
9823 line_breakcheck();
9824 }
9825 else
9826 {
9827 /* Do one more byte at this node. */
9828 n = arridx[depth] + curi[depth];
9829 ++curi[depth];
9830 c = byts[n];
9831 if (c == 0)
9832 {
9833 /* End of word, deal with the word.
9834 * Don't use keep-case words in the fold-case tree,
9835 * they will appear in the keep-case tree.
9836 * Only use the word when the region matches. */
9837 flags = (int)idxs[n];
9838 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009839 && (do_region
9840 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009841 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009842 & lp->lp_region) != 0))
9843 {
9844 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009845 if (!do_region)
9846 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009847
9848 /* Dump the basic word if there is no prefix or
9849 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009850 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009851 if (c == 0 || curi[depth] == 2)
9852 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009853
9854 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009855 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009856 lnum = apply_prefixes(slang, word, round,
9857 flags, lnum);
9858 }
9859 }
9860 else
9861 {
9862 /* Normal char, go one level deeper. */
9863 word[depth++] = c;
9864 arridx[depth] = idxs[n];
9865 curi[depth] = 1;
9866 }
9867 }
9868 }
9869 }
9870 }
9871
9872 /* Delete the empty line that we started with. */
9873 if (curbuf->b_ml.ml_line_count > 1)
9874 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
9875
9876 redraw_later(NOT_VALID);
9877}
9878
9879/*
9880 * Dump one word: apply case modifications and append a line to the buffer.
9881 */
9882 static void
9883dump_word(word, round, flags, lnum)
9884 char_u *word;
9885 int round;
9886 int flags;
9887 linenr_T lnum;
9888{
9889 int keepcap = FALSE;
9890 char_u *p;
9891 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00009892 char_u badword[MAXWLEN + 10];
9893 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009894
9895 if (round == 1 && (flags & WF_CAPMASK) != 0)
9896 {
9897 /* Need to fix case according to "flags". */
9898 make_case_word(word, cword, flags);
9899 p = cword;
9900 }
9901 else
9902 {
9903 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009904 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
9905 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009906 keepcap = TRUE;
9907 }
9908
Bram Moolenaar7887d882005-07-01 22:33:52 +00009909 /* Add flags and regions after a slash. */
9910 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009911 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009912 STRCPY(badword, p);
9913 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009914 if (keepcap)
9915 STRCAT(badword, "=");
9916 if (flags & WF_BANNED)
9917 STRCAT(badword, "!");
9918 else if (flags & WF_RARE)
9919 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009920 if (flags & WF_REGION)
9921 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009922 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +00009923 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009924 p = badword;
9925 }
9926
9927 ml_append(lnum, p, (colnr_T)0, FALSE);
9928}
9929
9930/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009931 * For ":spelldump": Find matching prefixes for "word". Prepend each to
9932 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009933 * Return the updated line number.
9934 */
9935 static linenr_T
9936apply_prefixes(slang, word, round, flags, startlnum)
9937 slang_T *slang;
9938 char_u *word; /* case-folded word */
9939 int round;
9940 int flags; /* flags with prefix ID */
9941 linenr_T startlnum;
9942{
9943 idx_T arridx[MAXWLEN];
9944 int curi[MAXWLEN];
9945 char_u prefix[MAXWLEN];
9946 int c;
9947 char_u *byts;
9948 idx_T *idxs;
9949 linenr_T lnum = startlnum;
9950 int depth;
9951 int n;
9952 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009953 int i;
9954
9955 byts = slang->sl_pbyts;
9956 idxs = slang->sl_pidxs;
9957 if (byts != NULL) /* array not is empty */
9958 {
9959 /*
9960 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009961 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009962 */
9963 depth = 0;
9964 arridx[0] = 0;
9965 curi[0] = 1;
9966 while (depth >= 0 && !got_int)
9967 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009968 n = arridx[depth];
9969 len = byts[n];
9970 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009971 {
9972 /* Done all bytes at this node, go up one level. */
9973 --depth;
9974 line_breakcheck();
9975 }
9976 else
9977 {
9978 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009979 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009980 ++curi[depth];
9981 c = byts[n];
9982 if (c == 0)
9983 {
9984 /* End of prefix, find out how many IDs there are. */
9985 for (i = 1; i < len; ++i)
9986 if (byts[n + i] != 0)
9987 break;
9988 curi[depth] += i - 1;
9989
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009990 i = valid_word_prefix(i, n, flags, word, slang);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009991 if (i != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009992 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009993 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009994 dump_word(prefix, round,
9995 (i & WF_RAREPFX) ? (flags | WF_RARE)
9996 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009997 }
9998 }
9999 else
10000 {
10001 /* Normal char, go one level deeper. */
10002 prefix[depth++] = c;
10003 arridx[depth] = idxs[n];
10004 curi[depth] = 1;
10005 }
10006 }
10007 }
10008 }
10009
10010 return lnum;
10011}
10012
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000010013#endif /* FEAT_SYN_HL */